source: libcaca/trunk/kernel/boot/idt.c @ 4154

Last change on this file since 4154 was 4154, checked in by Jean-Yves Lamoureux, 10 years ago
  • Total kernel rewrite
File size: 3.6 KB
Line 
1/*
2 *'freely' inspired by http://jojo.ouvaton.org/dossiers/boot_sector/tutorial02.html
3 * (actually, that's mostly copied, with minor compilation fixes)
4 */
5#include "kernel.h"
6#include "klibc.h"
7
8#define IDTBASE 0               /* GDT Physical address */
9#define IDTSIZE 256             /* Max descriptor count */
10#define INTGATE 0x8E00  /* Interruptions */
11#define TRAPGATE 0x8F00 /* Syscalls */
12#define TASKGATE 0x8500 /* Task switching */
13
14/* segment descriptor */
15struct idtdesc {
16        u16 offset0_15;
17        u16 select;
18    u16 type;
19        u16 offset16_31;       
20}  __attribute__ ((packed));
21
22/* IDTR register */
23struct idtr {
24        u16 limite;
25        u32 base;
26}  __attribute__ ((packed));
27
28struct idtr kidtr;
29
30/* IDT table */
31struct idtdesc kidt[IDTSIZE] = {{0, 0, 0, 0}};
32
33/* pointer on a free IDT entry */
34unsigned int kidtptr = 0;
35
36void default_int(void);
37void k_int0(void);
38void k_int1(void);
39void k_int2(void);
40void k_int3(void);
41void k_int4(void);
42void k_int5(void);
43void k_int6(void);
44void k_int7(void);
45void k_int8(void);
46void k_int9(void);
47void k_int10(void);
48void k_int11(void);
49void k_int12(void);
50void k_int13(void);
51void k_int14(void);
52void k_int15(void);
53void k_int16(void);
54void k_int17(void);
55void k_int18(void);
56void k_irq0(void);
57void k_irq1(void);
58void k_irq2(void);
59void k_irq3(void);
60void k_irq4(void);
61void k_irq5(void);
62void k_irq6(void);
63void k_irq7(void);
64void k_irq8(void);
65
66
67void init_idt_desc(u32 offset, u16 select, u16 type, struct idtdesc* desc) {
68        desc->offset0_15 = (offset & 0xffff);
69        desc->select = select;
70        desc->type = type;
71        desc->offset16_31 = (offset & 0xffff0000) >> 16;
72        return;
73}
74
75
76void add_idt_desc(struct idtdesc desc) {
77        kidt[kidtptr++] = desc;
78        return;
79}
80
81
82void init_idt(void) {
83        struct idtdesc desc;
84        int i;
85
86   
87        for(i=0;i<IDTSIZE;i++) {
88                init_idt_desc((u32)default_int, 0x08, INTGATE, &desc);
89                add_idt_desc(desc);
90        }
91
92        init_idt_desc((u32)k_int0, 0x08, INTGATE, &kidt[0]);
93        init_idt_desc((u32)k_int1, 0x08, INTGATE, &kidt[1]);
94        init_idt_desc((u32)k_int2, 0x08, INTGATE, &kidt[2]);
95        init_idt_desc((u32)k_int3, 0x08, INTGATE, &kidt[3]);
96        init_idt_desc((u32)k_int4, 0x08, INTGATE, &kidt[4]);
97        init_idt_desc((u32)k_int5, 0x08, INTGATE, &kidt[5]);
98        init_idt_desc((u32)k_int6, 0x08, INTGATE, &kidt[6]);
99        init_idt_desc((u32)k_int7, 0x08, INTGATE, &kidt[7]);
100        init_idt_desc((u32)k_int8, 0x08, INTGATE, &kidt[8]);
101        init_idt_desc((u32)k_int9, 0x08, INTGATE, &kidt[9]);
102        init_idt_desc((u32)k_int10, 0x08, INTGATE, &kidt[10]);
103        init_idt_desc((u32)k_int11, 0x08, INTGATE, &kidt[11]);
104        init_idt_desc((u32)k_int12, 0x08, INTGATE, &kidt[12]);
105        init_idt_desc((u32)k_int13, 0x08, INTGATE, &kidt[13]);
106        init_idt_desc((u32)k_int14, 0x08, INTGATE, &kidt[14]);
107        init_idt_desc((u32)k_int15, 0x08, INTGATE, &kidt[15]);
108        init_idt_desc((u32)k_int16, 0x08, INTGATE, &kidt[16]);
109        init_idt_desc((u32)k_int17, 0x08, INTGATE, &kidt[17]);
110        init_idt_desc((u32)k_int18, 0x08, INTGATE, &kidt[18]);
111   
112        init_idt_desc((u32)k_irq0, 0x08, INTGATE, &kidt[32]);
113        init_idt_desc((u32)k_irq1, 0x08, INTGATE, &kidt[33]);
114        init_idt_desc((u32)k_irq2, 0x08, INTGATE, &kidt[34]);
115        init_idt_desc((u32)k_irq3, 0x08, INTGATE, &kidt[35]);
116        init_idt_desc((u32)k_irq4, 0x08, INTGATE, &kidt[36]);
117        init_idt_desc((u32)k_irq5, 0x08, INTGATE, &kidt[37]);
118        init_idt_desc((u32)k_irq6, 0x08, INTGATE, &kidt[38]);
119        init_idt_desc((u32)k_irq7, 0x08, INTGATE, &kidt[39]);
120        init_idt_desc((u32)k_irq8, 0x08, INTGATE, &kidt[40]);
121   
122    kidtr.limite = IDTSIZE*8;
123        kidtr.base = IDTBASE;
124   
125        memcpy((void*)kidtr.base, kidt, kidtr.limite);
126#define printf(format, ...) { char __str[255]; sprintf (__str, format, __VA_ARGS__); print(__str);}
127
128    printf("Loading IDT from 0x%x\n", kidtr);
129#undef printf
130   
131    asm("lidtl (kidtr)");
132}
Note: See TracBrowser for help on using the repository browser.