My favorites | Sign in
Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "typedefs.h"
#include "config.h"
#include "string.h"
#include "video.h"
#include "pic.h"

static inline void outb(uint16_t port, uint8_t data)
{
__asm__ __volatile__("out %b1, %0"::"d"(port),"a"(data));
}

/*
; dw Limit L0-L15(16)
; dw Base B0-B15(16)
; db Base B16-B23(8)
; db P(1)/DPL(2)/S(1=codi/dades)/E(1)/X(1)/RW(1)/0
; db G(1)/OpSize(1)/0/0/Limit L16-L19(4)
; db Base B24-B31
*/

void install_idt_handler(uint8_t index, uint32_t handler);

void default_idt_handler()
{
__asm__ __volatile__("pusha");
__asm__ __volatile__("cli");
//video_printstring(7, "Interrupts has me! ");
outb(PIC1_CMD, PIC_EOI);
outb(PIC2_CMD, PIC_EOI);
__asm__ __volatile__("sti");
__asm__ __volatile__("popa; leave; iret"); /* BLACK MAGIC! */
}

struct segment_descriptor
{
uint16_t limit_15_0;
uint16_t base_addr_15_0;
uint8_t base_addr_23_16;
uint8_t type:4; // 0000 Reserved
// 0001 16bit TTS (Available)
// 0010 LDT
// 0011 16bit TTS (Busy)
// 0100 16bit call gate
// 0101 Task gate
// 0110 16bit Interrupt Gate
// 0111 16bit Trap gate
// 1000 Reserved
// 1001 32bit TSS (Available)
// 1010 Reserved
// 1011 32bit TSS (Busy)
// 1100 32bit Call Gate
// 1101 Reserved
// 1110 32bit Interrupt Gate
// 1111 32bit Trap Gate
uint8_t s:1; // Type of descriptor (1=data/0=sys)
uint8_t dpl:2; // Privilege level
uint8_t p:1; // Segment is present
uint8_t limit_19_16:4;
uint8_t avl:1; // Available
uint8_t l:1; // 64bit segment
uint8_t db:1; // Operation size
uint8_t g:1; // Granularity
uint8_t base_addr_31_24;
} __attribute__((__packed__));

struct idt_descriptor
{
uint16_t offset_0_15;
uint16_t segment_selector; // 0x8 normaly
uint8_t reserved; // 00000000b
uint8_t reserved2; // 01110001b
uint16_t offset_16_31;
} __attribute__((__packed__));

static struct segment_descriptor gdt[] =
{
NULL_DESCRIPTOR,
BUILD_4K_CS_RE_SEG_DESC(0,0xffffffff),
BUILD_4K_SS_RW_SEG_DESC(0x100000,0xffff),
BUILD_4K_DS_RW_SEG_DESC(0,0xffffffff),
};

struct gdtInfo
{
uint16_t size;
uint32_t addr;
} __attribute__((__packed__));

void gdt_init(void)
{
struct gdtInfo gdtdesc;

gdtdesc.size = sizeof(gdt) -1;
gdtdesc.addr = (uint32_t) gdt;

__asm__ __volatile__("lgdt %0;"
"ljmp $0x8, $pmode;"
"pmode: mov $0x18, %%eax;"
"movl %%eax, %%ds;"
"mov $0x10, %%eax;"
"movl %%eax, %%ss;"
:"=m"(gdtdesc));

video_printstring(7, "GDT(0 = ");
video_print_uint32(7, gdtdesc.addr);
video_printstring(7, ":");
video_print_uint16(7, gdtdesc.size);
video_printstring(7, ")\n");

}

static struct idt_descriptor idt[255];

struct idtInfo
{
uint16_t size;
uint32_t addr;
} __attribute__((__packed__));

void idt_init(void)
{
struct idtInfo idtdesc;
uint8_t i;

memset(idt, 0, sizeof(idt));

idtdesc.size = sizeof(idt) -1;
idtdesc.addr = (uint32_t) idt;

__asm__ __volatile("cli");
outb(PIC1_CMD, PIC_ICW1);
outb(PIC2_CMD, PIC_ICW1);
__asm__ __volatile__("lidt %0;":"=m"(idtdesc));
outb(PIC1_DATA, PIC_ICW4);
outb(PIC2_DATA, PIC_ICW4);
__asm__ __volatile("sti");

video_printstring(7, "IDT(0 = ");
video_print_uint32(7, idtdesc.addr);
video_printstring(7, ":");
video_print_uint16(7, idtdesc.size);
video_printstring(7, ")\n");

// 32 intel default interrupts
// + 16 IRQ's
for(i=0;i<=48;i++)
{
install_idt_handler(i, (uint32_t)default_idt_handler);
}

__asm__("sti");
}

void install_idt_handler(uint8_t index, uint32_t handler)
{
__asm__ __volatile("cli");

if ( handler == 0x0 )
{
memset(idt + index, 0, sizeof(struct idt_descriptor));
}
else
{
idt[index] = BUILD_IDT_DESCRIPTOR(handler);
}

__asm__ __volatile("sti");
}

Show details Hide details

Change log

r12 by caragot on Sep 16, 2009   Diff
CMOS dump function added, RTC structure...
Go to: 
Project members, sign in to write a code review

Older revisions

r7 by caragot on Sep 13, 2009   Diff
devel structure added with headers
r4 by caragot on Sep 13, 2009   Diff
Added driver tree
r2 by caragot on Sep 13, 2009   Diff
Initial release, bootstrap loads
kernel, makefile generate floppy image
All revisions of this file

File info

Size: 3689 bytes, 169 lines
Hosted by Google Code