2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 static GDTEntry gdt
[32];
35 static void keSetGDTEntry(unsigned int index
, unsigned int base
, unsigned int limit
,
36 int bytegran
, int rw
, int code
, unsigned char privlevel
, int tss
)
38 gdt
[index
].limit_low
= limit
& 0xFFFF;
39 gdt
[index
].base_low
= base
& 0xFFFF;
40 gdt
[index
].base_middle
= (base
& 0xFF0000) >> 16;
41 gdt
[index
].base_high
= (base
& 0xFF000000) >> 24;
42 gdt
[index
].granularity
= ((limit
& 0xF0000) >> 16) | 0x40;
43 gdt
[index
].access
= 0x90; // Present + 00010000b
45 gdt
[index
].access
|= (privlevel
& 0x3) << 5;
46 if (code
) gdt
[index
].access
|= 0x8;
47 if (rw
) gdt
[index
].access
|= 0x2;
48 if (!bytegran
) gdt
[index
].granularity
|= 0x80;
50 gdt
[index
].access
|= 0x9;
51 gdt
[index
].access
&= ~0x10;
55 void keInstallStartupGDT(void)
58 gp
.limit
= sizeof(GDTEntry
) * 5;
59 gp
.base
= (unsigned int)&gdt
;
62 memset(gdt
, 0, sizeof(GDTEntry
));
64 keSetGDTEntry(1, 0, 0xFFFFF, 0, 0, 1, 0, 0);
65 keSetGDTEntry(2, 0, 0xFFFFF, 0, 1, 0, 0, 0);
67 keSetGDTEntry(3, 0, 0xFFFFF, 0, 0, 1, 3, 0);
68 keSetGDTEntry(4, 0, 0xFFFFF, 0, 1, 0, 3, 0);
72 void keInstallGDT(void)
74 // We need one TSS per CPU
75 uint8_t cpucount
= keGetCPUCount();
78 gp
.limit
= sizeof(GDTEntry
) * (5 + cpucount
);
79 gp
.base
= (unsigned int)&gdt
;
82 memset(gdt
, 0, sizeof(GDTEntry
));
84 keSetGDTEntry(1, 0, 0xFFFFF, 0, 0, 1, 0, 0);
85 keSetGDTEntry(2, 0, 0xFFFFF, 0, 1, 0, 0, 0);
87 keSetGDTEntry(3, 0, 0xFFFFF, 0, 0, 1, 3, 0);
88 keSetGDTEntry(4, 0, 0xFFFFF, 0, 1, 0, 3, 0);
90 tss
= malloc(sizeof(TSS
) * cpucount
);
91 memset(tss
, 0, sizeof(TSS
) * cpucount
);
93 for (i
= 0; i
< cpucount
; i
++)
102 tss
[i
].iobitmap
[0] = 0xFF;
103 keSetGDTEntry(5 + i
, (unsigned int)&tss
[i
], sizeof(TSS
) - 1, 1, 0, 0, 3, 1);
107 asm volatile("ltrw %%ax"::"a"(0x28 + keAPICGetCPU() * 8));
110 void keFlushGDT(void)
120 "ljmpl *(target_addr)\n"
122 ".long keFlushGDT2\n"