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
|
|
73 uint64_t address : 48; // address of page directory pointer table.
|
|
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
|
|
97 uint64_t address : 48; // address of page directory table.
|
|
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
|
|
121 uint64_t address : 48; // address of page table.
|
|
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
|
|
133 typedef struct
|
|
134 {
|
|
135 uint64_t present : 1;
|
|
136 uint64_t rw : 1;
|
|
137 uint64_t us : 1; // user or supervisor
|
|
138 uint64_t pwt : 1;
|
|
139 uint64_t pcd : 1; // page cache disable
|
|
140 uint64_t accessed : 1;
|
|
141 uint64_t dirty : 1;
|
|
142 uint64_t pat : 1; // memory type?
|
|
143 uint64_t g : 1; // Global?
|
|
144 uint64_t ignored : 3;
|
|
145
|
|
146 uint64_t address : 48;
|
|
147 uint64_t ignored2 : 11;
|
|
148 uint64_t xd : 1;
|
|
149 } page_t;
|
|
150
|
|
151 // Page table:
|
|
152 typedef struct
|
|
153 {
|
|
154 page_t table[512];
|
|
155 uint64_t physicalAddress;
|
|
156 } PT_t;
|
|
157
|
|
158 // Make memmap a PML4 type:
|
|
159 typedef PML4_t memmap_t;
|
|
160
|
|
161 // Task related types:
|
24
|
162 typedef struct
|
|
163 {
|
9
|
164 char name[32]; // Name of the console
|
|
165 unsigned char screendata[80*25]; // All chars in the console!
|
|
166 } console_t;
|
|
167
|
25
|
168 typedef struct task_t {
|
|
169 struct task_t* next;
|
9
|
170 uint32_t kstack;
|
|
171 uint32_t ustack;
|
|
172
|
29
|
173 // For task switching:
|
25
|
174 uint64_t cr3;
|
|
175 uint64_t rip;
|
|
176 uint64_t rsp;
|
|
177 uint64_t rbp;
|
|
178
|
|
179 uint32_t pid;
|
9
|
180 uint32_t parent;
|
|
181 uint32_t owner;
|
|
182 uint32_t groups;
|
|
183 uint32_t timetorun;
|
|
184 uint32_t sleep;
|
|
185 uint32_t priority;
|
|
186 uint32_t filehandle;
|
|
187 char naam[32];
|
|
188
|
|
189 console_t *console;
|
25
|
190 } task_t;
|
9
|
191
|
29
|
192 // Variable argument list things:
|
|
193 #define va_start(v,l) __builtin_va_start(v,l)
|
|
194 #define va_end(v) __builtin_va_end(v)
|
|
195 #define va_arg(v,l) __builtin_va_arg(v,l)
|
|
196 typedef __builtin_va_list va_list;
|
|
197
|
|
198 /* Global variables */
|
|
199 extern uint64_t kernel_end;
|
|
200 extern uint64_t placement_address;
|
|
201
|
|
202 /* Procedures */
|
|
203
|
|
204 // memory functions:
|
|
205 // TODO: remove some redundant API functions:
|
|
206 void init_heap();
|
|
207 void init_memory(uint64_t total_mem_size);
|
|
208 page_t* get_page(uint64_t address, memmap_t*);
|
|
209 void* kmalloc(uint64_t size);
|
|
210 void kfree(void* ptr);
|
|
211 void* kmalloc_int(uint64_t size);
|
|
212 void* kmalloc_aligned_int(uint64_t size);
|
|
213 void mappage(uint64_t address);
|
|
214 void loadPageTable(void* tableAddress);
|
|
215 void switch_mapping(memmap_t* mapping);
|
|
216 void enablePaging();
|
|
217
|
|
218 // task related functions:
|
|
219 void initialize_tasking();
|
|
220 void new_task();
|
|
221 void task_scheduler();
|
|
222
|
|
223 // STDout funcs:
|
|
224 void printf(const char* fmt, ... );
|
|
225 void memset(void* ptr, uint8_t value, uint64_t num);
|
|
226 void memcpy(void* dst, void* src, uint64_t num);
|
|
227 int strncmp(const char* s1, const char* s2, int size);
|
|
228
|
|
229 // Screen related:
|
|
230 void clear_screen();
|
|
231 void init_screen();
|
|
232 void print_string(const char *);
|
|
233 void set_cursor(int newrow, int newcol);
|
|
234 void get_cursor(int *therow, int *thecol);
|
|
235 void set_color(int forecolor, int backcolor);
|
|
236
|
|
237 // For IO ports:
|
|
238 uint8_t inb(uint16_t);
|
|
239 uint16_t inw(uint16_t);
|
|
240 void outb(uint16_t, uint8_t);
|
|
241
|
|
242 // Interrupt functions:
|
|
243 void setupIDT(void);
|
|
244 void PICremap(void);
|
|
245 void loadIDT(void);
|
|
246
|
|
247 // ASM helper:
|
|
248 uint64_t read_rip();
|
|
249
|
|
250 // Helpers:
|
|
251 void halt(void);
|
|
252 void panic(char *msg);
|
|
253 void reboot(void);
|
|
254 void magicBochsBreak();
|
|
255 void doCPUID(int eax, int *ebx, int *ecx, int *edx);
|
|
256
|
|
257 // Keyboard driver:
|
|
258 void keyboardDriverUpdate(unsigned char scancode);
|
|
259 void getline(char *buffer, int len);
|
|
260
|
|
261 // Timer:
|
|
262 void timerDriverUpdate(void);
|
|
263 uint64_t getTimeMS();
|
28
|
264
|
9
|
265 #endif
|
|
266
|