9
|
1 #ifndef KERNEL_H
|
|
2 #define KERNEL_H
|
|
3
|
|
4 // Include common functions, available to all!
|
|
5 #define NULL ((void*)0)
|
|
6
|
29
|
7 /* Types */
|
|
8
|
9
|
9 // Type defs:
|
17
|
10 typedef unsigned char uint8_t;
|
|
11 typedef unsigned short uint16_t;
|
9
|
12 typedef unsigned int uint32_t;
|
17
|
13 typedef unsigned long int uint64_t;
|
9
|
14
|
17
|
15 // IDT related structures:
|
|
16 typedef struct {
|
|
17 uint16_t baseLow;
|
|
18 uint16_t selector;
|
|
19 uint8_t reserved1;
|
|
20 uint8_t flags;
|
|
21 uint16_t baseMid;
|
|
22 uint32_t baseHigh;
|
|
23 uint32_t reserved2;
|
|
24 } __attribute__((packed)) IDT_entry;
|
|
25
|
|
26 typedef struct {
|
|
27 uint16_t limit;
|
|
28 uint64_t base;
|
|
29 } __attribute__((packed)) idtPointer;
|
|
30
|
20
|
31 // Multiboot structs:
|
9
|
32 struct multiboot_aout_symbol_table {
|
|
33 uint32_t tabsize;
|
|
34 uint32_t strsize, addr, reserved;
|
|
35 };
|
|
36
|
|
37 struct multiboot_info {
|
|
38 uint32_t flags; // Multiboot flags / version
|
|
39 uint32_t mem_lower; // available memory from BIOS
|
|
40 uint32_t mem_upper;
|
|
41 uint32_t boot_device;
|
|
42 uint32_t cmdline; // COmmand line
|
|
43 uint32_t mods_count;
|
|
44 uint32_t mods_addr;
|
|
45 union {
|
|
46 struct multiboot_aout_symbol_table aout_sym;
|
|
47 } u;
|
|
48
|
|
49 uint32_t mmap_length;
|
|
50 uint32_t mmap_addr;
|
|
51 };
|
|
52
|
|
53 struct memory_map {
|
|
54 uint32_t size;
|
|
55 uint32_t baselow, basehigh;
|
|
56 uint32_t lenlow, lenhigh;
|
|
57 uint32_t type;
|
|
58 };
|
|
59
|
29
|
60 // Memory manager structures:
|
|
61 typedef struct
|
|
62 {
|
|
63 uint64_t present : 1;
|
|
64 uint64_t rw : 1;
|
|
65 uint64_t us : 1; // user or supervisor
|
|
66 uint64_t pwt : 1; // page level write-through
|
|
67 uint64_t pcd : 1; // page cache disable
|
|
68 uint64_t accessed : 1;
|
|
69 uint64_t ignored : 1;
|
|
70 uint64_t ps : 1; // must be 0.
|
|
71 uint64_t ignored2 : 4;
|
|
72 // 12 bits so far
|
30
|
73 uint64_t address : 40; // address of page directory pointer table.
|
29
|
74 uint64_t ignored3 : 11;
|
|
75 uint64_t xd : 1; // execute disable
|
|
76 } PML4E_t; // 64 bits wide, PML4 table must be 4096 byte aligned.
|
|
77
|
|
78 typedef struct
|
|
79 {
|
|
80 // Must be 12 bits aligned!
|
|
81 PML4E_t table[512];
|
|
82 uint64_t physicalAddress; // Physical address of the table above
|
|
83 } PML4_t;
|
|
84
|
|
85 typedef struct
|
|
86 {
|
|
87 uint64_t present : 1;
|
|
88 uint64_t rw : 1;
|
|
89 uint64_t us : 1; // user or supervisor
|
|
90 uint64_t pwt : 1; // page level write-through
|
|
91 uint64_t pcd : 1; // page cache disable
|
|
92 uint64_t accessed : 1;
|
|
93 uint64_t ignored : 1;
|
|
94 uint64_t ps : 1; // page size, must be 0, otherwise maps a 1 GB page.
|
|
95 uint64_t ignored2 : 4;
|
|
96 // 12 bits so far
|
30
|
97 uint64_t address : 40; // address of page directory table.
|
29
|
98 uint64_t ignored3 : 11;
|
|
99 uint64_t xd : 1; // execute disable
|
|
100 } PDPTE_t; // Page directory pointer table entry, 64 bits wide. 4-kB aligned.
|
|
101
|
|
102 // Page directory pointer table:
|
|
103 typedef struct
|
|
104 {
|
|
105 PDPTE_t table[512];
|
|
106 uint64_t physicalAddress;
|
|
107 } PDPT_t;
|
|
108
|
|
109 typedef struct
|
|
110 {
|
|
111 uint64_t present : 1;
|
|
112 uint64_t rw : 1;
|
|
113 uint64_t us : 1; // user or supervisor
|
|
114 uint64_t pwt : 1;
|
|
115 uint64_t pcd : 1; // page cache disable
|
|
116 uint64_t accessed : 1;
|
|
117 uint64_t ignored : 1;
|
|
118 uint64_t ps : 1; // page size, must be 0, otherwise maps a 2-MB page.
|
|
119 uint64_t ignored2 : 4;
|
|
120 // 12 bits so far
|
30
|
121 uint64_t address : 40; // address of page table.
|
29
|
122 uint64_t ignored3 : 11;
|
|
123 uint64_t xd : 1; // execute disable
|
|
124 } PDE_t;
|
|
125
|
|
126 // Page directory:
|
|
127 typedef struct
|
|
128 {
|
|
129 PDE_t table[512];
|
|
130 uint64_t physicalAddress;
|
|
131 } PD_t;
|
|
132
|
30
|
133
|
29
|
134 typedef struct
|
|
135 {
|
30
|
136 unsigned present : 1;
|
29
|
137 uint64_t rw : 1;
|
|
138 uint64_t us : 1; // user or supervisor
|
|
139 uint64_t pwt : 1;
|
|
140 uint64_t pcd : 1; // page cache disable
|
|
141 uint64_t accessed : 1;
|
|
142 uint64_t dirty : 1;
|
|
143 uint64_t pat : 1; // memory type?
|
|
144 uint64_t g : 1; // Global?
|
|
145 uint64_t ignored : 3;
|
|
146
|
30
|
147 uint64_t address : 40;
|
29
|
148 uint64_t ignored2 : 11;
|
|
149 uint64_t xd : 1;
|
|
150 } page_t;
|
|
151
|
30
|
152 _Static_assert(sizeof(page_t) == 8, "sizeof(page_t) != 8");
|
|
153 _Static_assert(sizeof(PDE_t) == 8, "sizeof(PDE_t) != 8");
|
|
154 _Static_assert(sizeof(PDPTE_t) == 8, "sizeof(PDPTE_t) != 8");
|
|
155 _Static_assert(sizeof(PML4E_t) == 8, "sizeof(PML4E_t) != 8");
|
|
156
|
29
|
157 // Page table:
|
|
158 typedef struct
|
|
159 {
|
|
160 page_t table[512];
|
|
161 uint64_t physicalAddress;
|
|
162 } PT_t;
|
|
163
|
|
164 // Make memmap a PML4 type:
|
|
165 typedef PML4_t memmap_t;
|
|
166
|
|
167 // Task related types:
|
24
|
168 typedef struct
|
|
169 {
|
9
|
170 char name[32]; // Name of the console
|
|
171 unsigned char screendata[80*25]; // All chars in the console!
|
|
172 } console_t;
|
|
173
|
25
|
174 typedef struct task_t {
|
|
175 struct task_t* next;
|
9
|
176 uint32_t kstack;
|
|
177 uint32_t ustack;
|
|
178
|
29
|
179 // For task switching:
|
25
|
180 uint64_t cr3;
|
|
181 uint64_t rip;
|
|
182 uint64_t rsp;
|
|
183 uint64_t rbp;
|
|
184
|
|
185 uint32_t pid;
|
9
|
186 uint32_t parent;
|
|
187 uint32_t owner;
|
|
188 uint32_t groups;
|
|
189 uint32_t timetorun;
|
|
190 uint32_t sleep;
|
|
191 uint32_t priority;
|
|
192 uint32_t filehandle;
|
|
193 char naam[32];
|
|
194
|
|
195 console_t *console;
|
25
|
196 } task_t;
|
9
|
197
|
29
|
198 // Variable argument list things:
|
|
199 #define va_start(v,l) __builtin_va_start(v,l)
|
|
200 #define va_end(v) __builtin_va_end(v)
|
|
201 #define va_arg(v,l) __builtin_va_arg(v,l)
|
|
202 typedef __builtin_va_list va_list;
|
|
203
|
|
204 /* Global variables */
|
|
205 extern uint64_t kernel_end;
|
|
206 extern uint64_t placement_address;
|
|
207
|
|
208 /* Procedures */
|
|
209
|
|
210 // memory functions:
|
|
211 // TODO: remove some redundant API functions:
|
|
212 void init_heap();
|
|
213 void init_memory(uint64_t total_mem_size);
|
|
214 page_t* get_page(uint64_t address, memmap_t*);
|
|
215 void* kmalloc(uint64_t size);
|
|
216 void kfree(void* ptr);
|
|
217 void* kmalloc_int(uint64_t size);
|
|
218 void* kmalloc_aligned_int(uint64_t size);
|
|
219 void mappage(uint64_t address);
|
|
220 void loadPageTable(void* tableAddress);
|
|
221 void switch_mapping(memmap_t* mapping);
|
|
222 void enablePaging();
|
|
223
|
|
224 // task related functions:
|
|
225 void initialize_tasking();
|
|
226 void new_task();
|
|
227 void task_scheduler();
|
|
228
|
|
229 // STDout funcs:
|
|
230 void printf(const char* fmt, ... );
|
|
231 void memset(void* ptr, uint8_t value, uint64_t num);
|
|
232 void memcpy(void* dst, void* src, uint64_t num);
|
|
233 int strncmp(const char* s1, const char* s2, int size);
|
|
234
|
|
235 // Screen related:
|
|
236 void clear_screen();
|
|
237 void init_screen();
|
|
238 void print_string(const char *);
|
|
239 void set_cursor(int newrow, int newcol);
|
|
240 void get_cursor(int *therow, int *thecol);
|
|
241 void set_color(int forecolor, int backcolor);
|
|
242
|
|
243 // For IO ports:
|
|
244 uint8_t inb(uint16_t);
|
|
245 uint16_t inw(uint16_t);
|
|
246 void outb(uint16_t, uint8_t);
|
|
247
|
|
248 // Interrupt functions:
|
|
249 void setupIDT(void);
|
|
250 void PICremap(void);
|
|
251 void loadIDT(void);
|
|
252
|
|
253 // ASM helper:
|
|
254 uint64_t read_rip();
|
|
255
|
|
256 // Helpers:
|
|
257 void halt(void);
|
|
258 void panic(char *msg);
|
|
259 void reboot(void);
|
|
260 void magicBochsBreak();
|
|
261 void doCPUID(int eax, int *ebx, int *ecx, int *edx);
|
|
262
|
|
263 // Keyboard driver:
|
|
264 void keyboardDriverUpdate(unsigned char scancode);
|
|
265 void getline(char *buffer, int len);
|
|
266
|
|
267 // Timer:
|
|
268 void timerDriverUpdate(void);
|
|
269 uint64_t getTimeMS();
|
28
|
270
|
9
|
271 #endif
|
|
272
|