Mercurial > lcfOS
annotate cos/kernel/kernel.h @ 219:1fa3e0050b49
Expanded ad hoc code generator
author | Windel Bouwman |
---|---|
date | Sat, 06 Jul 2013 12:38:09 +0200 |
parents | 35cc54e078dd |
children |
rev | line source |
---|---|
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; |
34
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
14 typedef long int int64_t; |
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
15 |
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
16 _Static_assert(sizeof(uint8_t) == 1, "sizeof(uint8_t) != 1"); |
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
17 _Static_assert(sizeof(uint16_t) == 2, "sizeof(uint16_t) != 2"); |
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
18 _Static_assert(sizeof(uint32_t) == 4, "sizeof(uint32_t) != 4"); |
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
19 _Static_assert(sizeof(uint64_t) == 8, "sizeof(uint64_t) != 8"); |
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
20 _Static_assert(sizeof(int64_t) == 8, "sizeof(int64_t) != 8"); |
9 | 21 |
17 | 22 // IDT related structures: |
23 typedef struct { | |
24 uint16_t baseLow; | |
25 uint16_t selector; | |
26 uint8_t reserved1; | |
27 uint8_t flags; | |
28 uint16_t baseMid; | |
29 uint32_t baseHigh; | |
30 uint32_t reserved2; | |
31 } __attribute__((packed)) IDT_entry; | |
32 | |
33 typedef struct { | |
34 uint16_t limit; | |
35 uint64_t base; | |
36 } __attribute__((packed)) idtPointer; | |
37 | |
20 | 38 // Multiboot structs: |
9 | 39 struct multiboot_aout_symbol_table { |
40 uint32_t tabsize; | |
41 uint32_t strsize, addr, reserved; | |
42 }; | |
43 | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
44 typedef struct multiboot_info_tag |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
45 { |
9 | 46 uint32_t flags; // Multiboot flags / version |
47 uint32_t mem_lower; // available memory from BIOS | |
48 uint32_t mem_upper; | |
49 uint32_t boot_device; | |
50 uint32_t cmdline; // COmmand line | |
51 uint32_t mods_count; | |
52 uint32_t mods_addr; | |
53 union { | |
54 struct multiboot_aout_symbol_table aout_sym; | |
55 } u; | |
56 | |
57 uint32_t mmap_length; | |
58 uint32_t mmap_addr; | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
59 } multiboot_info_t; |
9 | 60 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
61 typedef struct memory_map_tag |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
62 { |
9 | 63 uint32_t size; |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
64 uint64_t base; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
65 uint64_t length; |
9 | 66 uint32_t type; |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
67 } __attribute__((packed)) multiboot_memory_map_t; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
68 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
69 typedef struct |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
70 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
71 uint32_t mod_start; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
72 uint32_t mod_end; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
73 uint32_t cmdline; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
74 uint32_t pad; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
75 } multiboot_module_t; |
9 | 76 |
29 | 77 // Memory manager structures: |
78 typedef struct | |
79 { | |
80 uint64_t present : 1; | |
81 uint64_t rw : 1; | |
82 uint64_t us : 1; // user or supervisor | |
83 uint64_t pwt : 1; // page level write-through | |
84 uint64_t pcd : 1; // page cache disable | |
85 uint64_t accessed : 1; | |
86 uint64_t ignored : 1; | |
87 uint64_t ps : 1; // must be 0. | |
88 uint64_t ignored2 : 4; | |
89 // 12 bits so far | |
30 | 90 uint64_t address : 40; // address of page directory pointer table. |
29 | 91 uint64_t ignored3 : 11; |
92 uint64_t xd : 1; // execute disable | |
93 } PML4E_t; // 64 bits wide, PML4 table must be 4096 byte aligned. | |
94 | |
95 typedef struct | |
96 { | |
97 // Must be 12 bits aligned! | |
98 PML4E_t table[512]; | |
99 uint64_t physicalAddress; // Physical address of the table above | |
100 } PML4_t; | |
101 | |
102 typedef struct | |
103 { | |
104 uint64_t present : 1; | |
105 uint64_t rw : 1; | |
106 uint64_t us : 1; // user or supervisor | |
107 uint64_t pwt : 1; // page level write-through | |
108 uint64_t pcd : 1; // page cache disable | |
109 uint64_t accessed : 1; | |
110 uint64_t ignored : 1; | |
111 uint64_t ps : 1; // page size, must be 0, otherwise maps a 1 GB page. | |
112 uint64_t ignored2 : 4; | |
113 // 12 bits so far | |
30 | 114 uint64_t address : 40; // address of page directory table. |
29 | 115 uint64_t ignored3 : 11; |
116 uint64_t xd : 1; // execute disable | |
117 } PDPTE_t; // Page directory pointer table entry, 64 bits wide. 4-kB aligned. | |
118 | |
119 // Page directory pointer table: | |
120 typedef struct | |
121 { | |
122 PDPTE_t table[512]; | |
123 uint64_t physicalAddress; | |
124 } PDPT_t; | |
125 | |
126 typedef struct | |
127 { | |
128 uint64_t present : 1; | |
129 uint64_t rw : 1; | |
130 uint64_t us : 1; // user or supervisor | |
131 uint64_t pwt : 1; | |
132 uint64_t pcd : 1; // page cache disable | |
133 uint64_t accessed : 1; | |
134 uint64_t ignored : 1; | |
135 uint64_t ps : 1; // page size, must be 0, otherwise maps a 2-MB page. | |
136 uint64_t ignored2 : 4; | |
137 // 12 bits so far | |
30 | 138 uint64_t address : 40; // address of page table. |
29 | 139 uint64_t ignored3 : 11; |
140 uint64_t xd : 1; // execute disable | |
141 } PDE_t; | |
142 | |
143 // Page directory: | |
144 typedef struct | |
145 { | |
146 PDE_t table[512]; | |
147 uint64_t physicalAddress; | |
148 } PD_t; | |
149 | |
150 typedef struct | |
151 { | |
30 | 152 unsigned present : 1; |
29 | 153 uint64_t rw : 1; |
154 uint64_t us : 1; // user or supervisor | |
155 uint64_t pwt : 1; | |
156 uint64_t pcd : 1; // page cache disable | |
157 uint64_t accessed : 1; | |
158 uint64_t dirty : 1; | |
159 uint64_t pat : 1; // memory type? | |
160 uint64_t g : 1; // Global? | |
161 uint64_t ignored : 3; | |
162 | |
30 | 163 uint64_t address : 40; |
29 | 164 uint64_t ignored2 : 11; |
165 uint64_t xd : 1; | |
166 } page_t; | |
167 | |
30 | 168 _Static_assert(sizeof(page_t) == 8, "sizeof(page_t) != 8"); |
169 _Static_assert(sizeof(PDE_t) == 8, "sizeof(PDE_t) != 8"); | |
170 _Static_assert(sizeof(PDPTE_t) == 8, "sizeof(PDPTE_t) != 8"); | |
171 _Static_assert(sizeof(PML4E_t) == 8, "sizeof(PML4E_t) != 8"); | |
172 | |
29 | 173 // Page table: |
174 typedef struct | |
175 { | |
176 page_t table[512]; | |
177 uint64_t physicalAddress; | |
178 } PT_t; | |
179 | |
180 // Make memmap a PML4 type: | |
181 typedef PML4_t memmap_t; | |
182 | |
40 | 183 // Malloc related functions |
184 typedef struct { | |
185 uint64_t magic; | |
186 uint64_t state; | |
187 uint64_t size; | |
188 } heap_header_t; | |
189 | |
190 typedef struct | |
191 { | |
192 heap_header_t *first_block; | |
193 uint64_t start_address; | |
194 uint64_t end_address; | |
195 } heap_t; | |
196 | |
29 | 197 // Task related types: |
24 | 198 typedef struct |
199 { | |
9 | 200 char name[32]; // Name of the console |
201 unsigned char screendata[80*25]; // All chars in the console! | |
202 } console_t; | |
203 | |
25 | 204 typedef struct task_t { |
205 struct task_t* next; | |
9 | 206 uint32_t kstack; |
207 uint32_t ustack; | |
208 | |
29 | 209 // For task switching: |
25 | 210 uint64_t cr3; |
211 uint64_t rip; | |
212 uint64_t rsp; | |
213 uint64_t rbp; | |
214 | |
215 uint32_t pid; | |
9 | 216 uint32_t parent; |
217 uint32_t owner; | |
218 uint32_t groups; | |
219 uint32_t timetorun; | |
220 uint32_t sleep; | |
221 uint32_t priority; | |
222 uint32_t filehandle; | |
223 char naam[32]; | |
224 | |
225 console_t *console; | |
25 | 226 } task_t; |
9 | 227 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
228 /* Filesystem related types: */ |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
229 struct fs_node_tag; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
230 struct fs_dirent_tag; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
231 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
232 typedef uint64_t (*read_type_t)(struct fs_node_tag*, uint64_t, uint64_t, uint8_t*); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
233 typedef uint64_t (*write_type_t)(struct fs_node_tag*, uint64_t, uint64_t, uint8_t*); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
234 typedef void (*open_type_t)(struct fs_node_tag*); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
235 typedef void (*close_type_t)(struct fs_node_tag*); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
236 typedef struct fs_dirent_tag* (*readdir_type_t)(struct fs_node_tag*, int); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
237 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
238 typedef struct fs_node_tag |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
239 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
240 char name[129]; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
241 uint64_t flags; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
242 uint64_t length; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
243 uint64_t inode; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
244 // Accessor functions: |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
245 read_type_t read; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
246 write_type_t write; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
247 open_type_t open; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
248 close_type_t close; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
249 readdir_type_t readdir; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
250 } fs_node_t; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
251 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
252 typedef struct fs_dirent_tag |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
253 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
254 char name[65]; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
255 uint64_t inode; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
256 } fs_dirent_t; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
257 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
258 #define FS_FILE 0x1 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
259 #define FS_DIRECTORY 0x2 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
260 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
261 uint64_t read_fs(fs_node_t *node, uint64_t offset, uint64_t size, uint8_t *buffer); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
262 uint64_t write_fs(fs_node_t *node, uint64_t offset, uint64_t size, uint8_t *buffer); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
263 void open_fs(fs_node_t *node); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
264 void close_fs(fs_node_t *node); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
265 fs_dirent_t* readdir_fs(fs_node_t *node, int i); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
266 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
267 // Initial ramdisk functions: |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
268 fs_node_t* initialize_initrd(uint64_t location); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
269 |
41 | 270 void load_ramdisk(void); |
271 | |
29 | 272 // Variable argument list things: |
273 #define va_start(v,l) __builtin_va_start(v,l) | |
274 #define va_end(v) __builtin_va_end(v) | |
275 #define va_arg(v,l) __builtin_va_arg(v,l) | |
276 typedef __builtin_va_list va_list; | |
277 | |
278 /* Global variables */ | |
279 extern uint64_t kernel_end; | |
280 extern uint64_t placement_address; | |
40 | 281 extern memmap_t* kernel_map; |
29 | 282 |
283 /* Procedures */ | |
284 | |
285 // memory functions: | |
286 // TODO: remove some redundant API functions: | |
287 void init_memory(uint64_t total_mem_size); | |
288 page_t* get_page(uint64_t address, memmap_t*); | |
40 | 289 void alloc_frame(page_t *page); |
29 | 290 void loadPageTable(void* tableAddress); |
291 void switch_mapping(memmap_t* mapping); | |
292 void enablePaging(); | |
293 | |
40 | 294 // Malloc related: |
295 void* kmalloc(uint64_t size); | |
296 void* kmalloc_a(uint64_t size); // Aligned on a 4 KiB page. | |
297 void kfree(void* ptr); | |
298 heap_t* create_heap(uint64_t location, uint64_t size); | |
299 | |
29 | 300 // task related functions: |
301 void initialize_tasking(); | |
302 void new_task(); | |
303 void task_scheduler(); | |
304 | |
305 // STDout funcs: | |
306 void printf(const char* fmt, ... ); | |
307 void memset(void* ptr, uint8_t value, uint64_t num); | |
308 void memcpy(void* dst, void* src, uint64_t num); | |
309 int strncmp(const char* s1, const char* s2, int size); | |
310 | |
311 // Screen related: | |
312 void clear_screen(); | |
313 void init_screen(); | |
314 void print_string(const char *); | |
315 void set_cursor(int newrow, int newcol); | |
316 void get_cursor(int *therow, int *thecol); | |
317 void set_color(int forecolor, int backcolor); | |
318 | |
319 // For IO ports: | |
320 uint8_t inb(uint16_t); | |
321 uint16_t inw(uint16_t); | |
322 void outb(uint16_t, uint8_t); | |
323 | |
324 // Interrupt functions: | |
325 void setupIDT(void); | |
326 void PICremap(void); | |
327 void loadIDT(void); | |
328 | |
329 // ASM helper: | |
330 uint64_t read_rip(); | |
331 | |
332 // Helpers: | |
333 void halt(void); | |
334 void panic(char *msg); | |
335 void reboot(void); | |
336 void magicBochsBreak(); | |
337 void doCPUID(int eax, int *ebx, int *ecx, int *edx); | |
338 | |
339 // Keyboard driver: | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
340 void keyboardDriverUpdate(void); |
29 | 341 void getline(char *buffer, int len); |
34
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
342 void keyboard_init(void); |
29 | 343 |
344 // Timer: | |
345 void timerDriverUpdate(void); | |
346 uint64_t getTimeMS(); | |
34
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
347 void timer_init(void); |
28 | 348 |
37 | 349 // Multiboot: |
350 extern uint64_t available_memory; | |
351 extern uint64_t ramdisk_location; | |
352 void read_multiboot_info(void); | |
353 | |
354 // Shell | |
355 void shell(void); | |
356 | |
9 | 357 #endif |
358 |