comparison cos/kernel/kernel.h @ 29:7e3bdcb391dc

Added get_page function to mm
author windel
date Thu, 29 Dec 2011 19:34:01 +0100
parents 47b7df514243
children 0148f55bfe24
comparison
equal deleted inserted replaced
28:47b7df514243 29:7e3bdcb391dc
1 #ifndef KERNEL_H 1 #ifndef KERNEL_H
2 #define KERNEL_H 2 #define KERNEL_H
3 3
4 // Include common functions, available to all! 4 // Include common functions, available to all!
5 #define NULL ((void*)0) 5 #define NULL ((void*)0)
6
7 /* Types */
6 8
7 // Type defs: 9 // Type defs:
8 typedef unsigned char uint8_t; 10 typedef unsigned char uint8_t;
9 typedef unsigned short uint16_t; 11 typedef unsigned short uint16_t;
10 typedef unsigned int uint32_t; 12 typedef unsigned int uint32_t;
23 25
24 typedef struct { 26 typedef struct {
25 uint16_t limit; 27 uint16_t limit;
26 uint64_t base; 28 uint64_t base;
27 } __attribute__((packed)) idtPointer; 29 } __attribute__((packed)) idtPointer;
28
29 // memory alloc functions:
30 void init_heap();
31 void* kmalloc(uint64_t size);
32 void kfree(void* ptr);
33
34 void* kmalloc_int(uint64_t size);
35
36 // task related functions:
37 void initialize_tasking();
38 void new_task();
39 void task_scheduler();
40
41 // STDout funcs:
42 void printf(const char* fmt, ... );
43 void memset(void* ptr, uint8_t value, uint64_t num);
44 void memcpy(void* dst, void* src, uint64_t num);
45 int strncmp(const char* s1, const char* s2, int size);
46
47 // Screen related:
48 void clear_screen();
49 void init_screen();
50 void print_string(const char *);
51 void set_cursor(int newrow, int newcol);
52 void get_cursor(int *therow, int *thecol);
53 void set_color(int forecolor, int backcolor);
54
55 // For IO ports:
56 uint8_t inb(uint16_t);
57 uint16_t inw(uint16_t);
58 void outb(uint16_t, uint8_t);
59
60 // Interrupt functions:
61 void setupIDT(void);
62 void PICremap(void);
63
64 // ASM helper:
65 void loadIDT(void);
66 uint64_t read_rip();
67
68 // Panic exit:
69 void halt(void);
70 void panic(char *msg);
71 void reboot(void);
72
73 // Bochs xchg bx,bx breakpoint:
74 void magicBochsBreak();
75
76 // Assembler util functions:
77 void doCPUID(int eax, int *ebx, int *ecx, int *edx);
78
79 // Keyboard driver:
80 void keyboardDriverUpdate(unsigned char scancode);
81 void getline(char *buffer, int len);
82
83 // Timer:
84 void timerDriverUpdate(void);
85 uint64_t getTimeMS();
86
87 // Memory functions:
88 void mappage(uint64_t address);
89
90 void loadPageTable(void* tableAddress);
91
92 // Variable argument list things:
93 #define va_start(v,l) __builtin_va_start(v,l)
94 #define va_end(v) __builtin_va_end(v)
95 #define va_arg(v,l) __builtin_va_arg(v,l)
96 typedef __builtin_va_list va_list;
97 30
98 // Multiboot structs: 31 // Multiboot structs:
99 struct multiboot_aout_symbol_table { 32 struct multiboot_aout_symbol_table {
100 uint32_t tabsize; 33 uint32_t tabsize;
101 uint32_t strsize, addr, reserved; 34 uint32_t strsize, addr, reserved;
122 uint32_t baselow, basehigh; 55 uint32_t baselow, basehigh;
123 uint32_t lenlow, lenhigh; 56 uint32_t lenlow, lenhigh;
124 uint32_t type; 57 uint32_t type;
125 }; 58 };
126 59
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:
127 typedef struct 162 typedef struct
128 { 163 {
129 char name[32]; // Name of the console 164 char name[32]; // Name of the console
130 unsigned char screendata[80*25]; // All chars in the console! 165 unsigned char screendata[80*25]; // All chars in the console!
131 } console_t; 166 } console_t;
133 typedef struct task_t { 168 typedef struct task_t {
134 struct task_t* next; 169 struct task_t* next;
135 uint32_t kstack; 170 uint32_t kstack;
136 uint32_t ustack; 171 uint32_t ustack;
137 172
173 // For task switching:
138 uint64_t cr3; 174 uint64_t cr3;
139 uint64_t rip; 175 uint64_t rip;
140 uint64_t rsp; 176 uint64_t rsp;
141 uint64_t rbp; 177 uint64_t rbp;
142 178
151 char naam[32]; 187 char naam[32];
152 188
153 console_t *console; 189 console_t *console;
154 } task_t; 190 } task_t;
155 191
156 // Memory manager functions: 192 // Variable argument list things:
157 typedef struct 193 #define va_start(v,l) __builtin_va_start(v,l)
158 { 194 #define va_end(v) __builtin_va_end(v)
159 // TODO: other members here. 195 #define va_arg(v,l) __builtin_va_arg(v,l)
160 uint64_t frame; 196 typedef __builtin_va_list va_list;
161 } page_t; 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();
162 264
163 #endif 265 #endif
164 266