- Implemented execp*.
[planlOS.git] / system / kernel / ke / gdt.c
blob1f01878c8b61ee92232ea77320f9142276c1a0c0
1 /*
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.
22 #include "ke/gdt.h"
23 #include "ke/cpu.h"
24 #include "ke/apic.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
29 TSS *tss = 0;
31 static GDTEntry gdt[32];
32 GDTPtr gp;
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;
49 if (tss) {
50 gdt[index].access |= 0x9;
51 gdt[index].access &= ~0x10;
55 void keInstallStartupGDT(void)
57 // Pointer to the GDT
58 gp.limit = sizeof(GDTEntry) * 5;
59 gp.base = (unsigned int)&gdt;
61 // Null selector
62 memset(gdt, 0, sizeof(GDTEntry));
63 // Kernel selectors
64 keSetGDTEntry(1, 0, 0xFFFFF, 0, 0, 1, 0, 0);
65 keSetGDTEntry(2, 0, 0xFFFFF, 0, 1, 0, 0, 0);
66 // User selectors
67 keSetGDTEntry(3, 0, 0xFFFFF, 0, 0, 1, 3, 0);
68 keSetGDTEntry(4, 0, 0xFFFFF, 0, 1, 0, 3, 0);
69 keFlushGDT();
72 void keInstallGDT(void)
74 // We need one TSS per CPU
75 uint8_t cpucount = keGetCPUCount();
77 // Pointer to the GDT
78 gp.limit = sizeof(GDTEntry) * (5 + cpucount);
79 gp.base = (unsigned int)&gdt;
81 // Null selector
82 memset(gdt, 0, sizeof(GDTEntry));
83 // Kernel selectors
84 keSetGDTEntry(1, 0, 0xFFFFF, 0, 0, 1, 0, 0);
85 keSetGDTEntry(2, 0, 0xFFFFF, 0, 1, 0, 0, 0);
86 // User selectors
87 keSetGDTEntry(3, 0, 0xFFFFF, 0, 0, 1, 3, 0);
88 keSetGDTEntry(4, 0, 0xFFFFF, 0, 1, 0, 3, 0);
89 // TSS
90 tss = malloc(sizeof(TSS) * cpucount);
91 memset(tss, 0, sizeof(TSS) * cpucount);
92 uint32_t i;
93 for (i = 0; i < cpucount; i++)
95 tss[i].ss0 = 0x10;
96 tss[i].es = 0x23;
97 tss[i].cs = 0x1b;
98 tss[i].ss = 0x23;
99 tss[i].ds = 0x23;
100 tss[i].fs = 0x23;
101 tss[i].gs = 0x23;
102 tss[i].iobitmap[0] = 0xFF;
103 keSetGDTEntry(5 + i, (unsigned int)&tss[i], sizeof(TSS) - 1, 1, 0, 0, 3, 1);
106 keFlushGDT();
107 asm volatile("ltrw %%ax"::"a"(0x28 + keAPICGetCPU() * 8));
110 void keFlushGDT(void)
112 asm volatile(
113 "lgdt (gp)\n"
114 "mov $0x10, %ax\n"
115 "mov %ax, %ds\n"
116 "mov %ax, %es\n"
117 "mov %ax, %fs\n"
118 "mov %ax, %gs\n"
119 "mov %ax, %ss\n"
120 "ljmpl *(target_addr)\n"
121 "target_addr:\n"
122 ".long keFlushGDT2\n"
123 ".word 0x08, 0\n"
124 "keFlushGDT2:\n");