Mercurial > lcfOS
annotate cos/kernel/kernel.h @ 34:8012221dd740
Fixes for uninitialized data. This causes problems on real machines
author | windel |
---|---|
date | Mon, 16 Jan 2012 13:46:06 +0100 |
parents | 3a6a9b929db0 |
children | 5c20bd53cccd |
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 | |
30 | 150 |
29 | 151 typedef struct |
152 { | |
30 | 153 unsigned present : 1; |
29 | 154 uint64_t rw : 1; |
155 uint64_t us : 1; // user or supervisor | |
156 uint64_t pwt : 1; | |
157 uint64_t pcd : 1; // page cache disable | |
158 uint64_t accessed : 1; | |
159 uint64_t dirty : 1; | |
160 uint64_t pat : 1; // memory type? | |
161 uint64_t g : 1; // Global? | |
162 uint64_t ignored : 3; | |
163 | |
30 | 164 uint64_t address : 40; |
29 | 165 uint64_t ignored2 : 11; |
166 uint64_t xd : 1; | |
167 } page_t; | |
168 | |
30 | 169 _Static_assert(sizeof(page_t) == 8, "sizeof(page_t) != 8"); |
170 _Static_assert(sizeof(PDE_t) == 8, "sizeof(PDE_t) != 8"); | |
171 _Static_assert(sizeof(PDPTE_t) == 8, "sizeof(PDPTE_t) != 8"); | |
172 _Static_assert(sizeof(PML4E_t) == 8, "sizeof(PML4E_t) != 8"); | |
173 | |
29 | 174 // Page table: |
175 typedef struct | |
176 { | |
177 page_t table[512]; | |
178 uint64_t physicalAddress; | |
179 } PT_t; | |
180 | |
181 // Make memmap a PML4 type: | |
182 typedef PML4_t memmap_t; | |
183 | |
184 // Task related types: | |
24 | 185 typedef struct |
186 { | |
9 | 187 char name[32]; // Name of the console |
188 unsigned char screendata[80*25]; // All chars in the console! | |
189 } console_t; | |
190 | |
25 | 191 typedef struct task_t { |
192 struct task_t* next; | |
9 | 193 uint32_t kstack; |
194 uint32_t ustack; | |
195 | |
29 | 196 // For task switching: |
25 | 197 uint64_t cr3; |
198 uint64_t rip; | |
199 uint64_t rsp; | |
200 uint64_t rbp; | |
201 | |
202 uint32_t pid; | |
9 | 203 uint32_t parent; |
204 uint32_t owner; | |
205 uint32_t groups; | |
206 uint32_t timetorun; | |
207 uint32_t sleep; | |
208 uint32_t priority; | |
209 uint32_t filehandle; | |
210 char naam[32]; | |
211 | |
212 console_t *console; | |
25 | 213 } task_t; |
9 | 214 |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
215 /* Filesystem related types: */ |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
216 struct fs_node_tag; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
217 struct fs_dirent_tag; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
218 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
219 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
|
220 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
|
221 typedef void (*open_type_t)(struct fs_node_tag*); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
222 typedef void (*close_type_t)(struct fs_node_tag*); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
223 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
|
224 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
225 typedef struct fs_node_tag |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
226 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
227 char name[129]; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
228 uint64_t flags; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
229 uint64_t length; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
230 uint64_t inode; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
231 // Accessor functions: |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
232 read_type_t read; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
233 write_type_t write; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
234 open_type_t open; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
235 close_type_t close; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
236 readdir_type_t readdir; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
237 } fs_node_t; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
238 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
239 typedef struct fs_dirent_tag |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
240 { |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
241 char name[65]; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
242 uint64_t inode; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
243 } fs_dirent_t; |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
244 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
245 #define FS_FILE 0x1 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
246 #define FS_DIRECTORY 0x2 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
247 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
248 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
|
249 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
|
250 void open_fs(fs_node_t *node); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
251 void close_fs(fs_node_t *node); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
252 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
|
253 |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
254 // Initial ramdisk functions: |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
255 fs_node_t* initialize_initrd(uint64_t location); |
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
256 |
29 | 257 // Variable argument list things: |
258 #define va_start(v,l) __builtin_va_start(v,l) | |
259 #define va_end(v) __builtin_va_end(v) | |
260 #define va_arg(v,l) __builtin_va_arg(v,l) | |
261 typedef __builtin_va_list va_list; | |
262 | |
263 /* Global variables */ | |
264 extern uint64_t kernel_end; | |
265 extern uint64_t placement_address; | |
266 | |
267 /* Procedures */ | |
268 | |
269 // memory functions: | |
270 // TODO: remove some redundant API functions: | |
271 void init_heap(); | |
272 void init_memory(uint64_t total_mem_size); | |
273 page_t* get_page(uint64_t address, memmap_t*); | |
274 void* kmalloc(uint64_t size); | |
275 void kfree(void* ptr); | |
276 void* kmalloc_int(uint64_t size); | |
277 void* kmalloc_aligned_int(uint64_t size); | |
278 void mappage(uint64_t address); | |
279 void loadPageTable(void* tableAddress); | |
280 void switch_mapping(memmap_t* mapping); | |
281 void enablePaging(); | |
282 | |
283 // task related functions: | |
284 void initialize_tasking(); | |
285 void new_task(); | |
286 void task_scheduler(); | |
287 | |
288 // STDout funcs: | |
289 void printf(const char* fmt, ... ); | |
290 void memset(void* ptr, uint8_t value, uint64_t num); | |
291 void memcpy(void* dst, void* src, uint64_t num); | |
292 int strncmp(const char* s1, const char* s2, int size); | |
293 | |
294 // Screen related: | |
295 void clear_screen(); | |
296 void init_screen(); | |
297 void print_string(const char *); | |
298 void set_cursor(int newrow, int newcol); | |
299 void get_cursor(int *therow, int *thecol); | |
300 void set_color(int forecolor, int backcolor); | |
301 | |
302 // For IO ports: | |
303 uint8_t inb(uint16_t); | |
304 uint16_t inw(uint16_t); | |
305 void outb(uint16_t, uint8_t); | |
306 | |
307 // Interrupt functions: | |
308 void setupIDT(void); | |
309 void PICremap(void); | |
310 void loadIDT(void); | |
311 | |
312 // ASM helper: | |
313 uint64_t read_rip(); | |
314 | |
315 // Helpers: | |
316 void halt(void); | |
317 void panic(char *msg); | |
318 void reboot(void); | |
319 void magicBochsBreak(); | |
320 void doCPUID(int eax, int *ebx, int *ecx, int *edx); | |
321 | |
322 // Keyboard driver: | |
32
3a6a9b929db0
Added initial ramdisk and some virtual file system functions
windel
parents:
30
diff
changeset
|
323 void keyboardDriverUpdate(void); |
29 | 324 void getline(char *buffer, int len); |
34
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
325 void keyboard_init(void); |
29 | 326 |
327 // Timer: | |
328 void timerDriverUpdate(void); | |
329 uint64_t getTimeMS(); | |
34
8012221dd740
Fixes for uninitialized data. This causes problems on real machines
windel
parents:
32
diff
changeset
|
330 void timer_init(void); |
28 | 331 |
9 | 332 #endif |
333 |