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