diff 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
line wrap: on
line diff
--- a/cos/kernel/kernel.h	Wed Dec 28 13:38:43 2011 +0100
+++ b/cos/kernel/kernel.h	Thu Dec 29 19:34:01 2011 +0100
@@ -4,6 +4,8 @@
 // Include common functions, available to all!
 #define NULL ((void*)0)
 
+/* Types */
+
 // Type defs:
 typedef unsigned char       uint8_t;
 typedef unsigned short      uint16_t;
@@ -26,75 +28,6 @@
       uint64_t   base;
 } __attribute__((packed)) idtPointer;
 
-// memory alloc functions:
-void init_heap();
-void* kmalloc(uint64_t size);
-void kfree(void* ptr);
-
-void* kmalloc_int(uint64_t size);
-
-// task related functions:
-void initialize_tasking();
-void new_task();
-void task_scheduler();
-
-// STDout funcs:
-void printf(const char* fmt, ... );
-void memset(void* ptr, uint8_t value, uint64_t num);
-void memcpy(void* dst, void* src, uint64_t num);
-int strncmp(const char* s1, const char* s2, int size); 
-
-// Screen related:
-void clear_screen();
-void init_screen();
-void print_string(const char *);
-void set_cursor(int newrow, int newcol);
-void get_cursor(int *therow, int *thecol);
-void set_color(int forecolor, int backcolor);
-
-// For IO ports:
-uint8_t inb(uint16_t);
-uint16_t inw(uint16_t);
-void outb(uint16_t, uint8_t);
-
-// Interrupt functions:
-void setupIDT(void);
-void PICremap(void);
-
-// ASM helper:
-void loadIDT(void);
-uint64_t read_rip();
-
-// Panic exit:
-void halt(void);
-void panic(char *msg);
-void reboot(void);
-
-// Bochs xchg bx,bx breakpoint:
-void magicBochsBreak();
-
-// Assembler util functions:
-void doCPUID(int eax, int *ebx, int *ecx, int *edx);
-
-// Keyboard driver:
-void keyboardDriverUpdate(unsigned char scancode);
-void getline(char *buffer, int len);
-
-// Timer:
-void timerDriverUpdate(void);
-uint64_t getTimeMS();
-
-// Memory functions:
-void mappage(uint64_t address);
-
-void loadPageTable(void* tableAddress);
-
-// Variable argument list things:
-#define va_start(v,l)	__builtin_va_start(v,l)
-#define va_end(v)	__builtin_va_end(v)
-#define va_arg(v,l)	__builtin_va_arg(v,l)
-typedef __builtin_va_list va_list;
-
 // Multiboot structs:
 struct multiboot_aout_symbol_table {
   uint32_t tabsize;
@@ -124,6 +57,108 @@
   uint32_t type;
 };
 
+// Memory manager structures:
+typedef struct
+{
+   uint64_t present : 1;
+   uint64_t rw : 1;
+   uint64_t us : 1; // user or supervisor
+   uint64_t pwt : 1; // page level write-through
+   uint64_t pcd : 1; // page cache disable
+   uint64_t accessed : 1;
+   uint64_t ignored : 1;
+   uint64_t ps : 1; // must be 0.
+   uint64_t ignored2 : 4;
+   // 12 bits so far
+   uint64_t address : 48; // address of page directory pointer table.
+   uint64_t ignored3 : 11;
+   uint64_t xd : 1; // execute disable
+} PML4E_t; // 64 bits wide, PML4 table must be 4096 byte aligned.
+
+typedef struct
+{
+   // Must be 12 bits aligned!
+   PML4E_t table[512];
+   uint64_t physicalAddress; // Physical address of the table above
+} PML4_t;
+
+typedef struct
+{
+   uint64_t present : 1;
+   uint64_t rw : 1;
+   uint64_t us : 1; // user or supervisor
+   uint64_t pwt : 1; // page level write-through
+   uint64_t pcd : 1; // page cache disable
+   uint64_t accessed : 1;
+   uint64_t ignored : 1;
+   uint64_t ps : 1; // page size, must be 0, otherwise maps a 1 GB page.
+   uint64_t ignored2 : 4;
+   // 12 bits so far
+   uint64_t address : 48; // address of page directory table.
+   uint64_t ignored3 : 11;
+   uint64_t xd : 1; // execute disable
+} PDPTE_t; // Page directory pointer table entry, 64 bits wide. 4-kB aligned.
+
+// Page directory pointer table:
+typedef struct
+{
+   PDPTE_t table[512];
+   uint64_t physicalAddress;
+} PDPT_t;
+
+typedef struct
+{
+   uint64_t present : 1;
+   uint64_t rw : 1;
+   uint64_t us : 1; // user or supervisor
+   uint64_t pwt : 1;
+   uint64_t pcd : 1; // page cache disable
+   uint64_t accessed : 1;
+   uint64_t ignored : 1;
+   uint64_t ps : 1; // page size, must be 0, otherwise maps a 2-MB page.
+   uint64_t ignored2 : 4;
+   // 12 bits so far
+   uint64_t address : 48; // address of page table.
+   uint64_t ignored3 : 11;
+   uint64_t xd : 1; // execute disable
+} PDE_t;
+
+// Page directory:
+typedef struct
+{
+   PDE_t table[512];
+   uint64_t physicalAddress;
+} PD_t;
+
+typedef struct
+{
+   uint64_t present : 1;
+   uint64_t rw : 1;
+   uint64_t us : 1; // user or supervisor
+   uint64_t pwt : 1;
+   uint64_t pcd : 1; // page cache disable
+   uint64_t accessed : 1;
+   uint64_t dirty : 1;
+   uint64_t pat : 1; // memory type?
+   uint64_t g : 1; // Global?
+   uint64_t ignored : 3;
+
+   uint64_t address : 48;
+   uint64_t ignored2 : 11;
+   uint64_t xd : 1;
+} page_t;
+
+// Page table:
+typedef struct
+{
+   page_t table[512];
+   uint64_t physicalAddress;
+} PT_t;
+
+// Make memmap a PML4 type:
+typedef PML4_t memmap_t;
+
+// Task related types:
 typedef struct 
 {
   char name[32]; // Name of the console
@@ -135,6 +170,7 @@
   uint32_t kstack;
   uint32_t ustack;
 
+  // For task switching:
   uint64_t cr3;
   uint64_t rip;
   uint64_t rsp;
@@ -153,12 +189,78 @@
   console_t *console;
 } task_t;
 
-// Memory manager functions:
-typedef struct
-{
-   // TODO: other members here.
-   uint64_t frame;
-} page_t;
+// Variable argument list things:
+#define va_start(v,l)	__builtin_va_start(v,l)
+#define va_end(v)	__builtin_va_end(v)
+#define va_arg(v,l)	__builtin_va_arg(v,l)
+typedef __builtin_va_list va_list;
+
+/* Global variables */
+extern uint64_t kernel_end;
+extern uint64_t placement_address;
+
+/* Procedures */
+
+// memory functions:
+// TODO: remove some redundant API functions:
+void init_heap();
+void init_memory(uint64_t total_mem_size);
+page_t* get_page(uint64_t address, memmap_t*);
+void* kmalloc(uint64_t size);
+void kfree(void* ptr);
+void* kmalloc_int(uint64_t size);
+void* kmalloc_aligned_int(uint64_t size);
+void mappage(uint64_t address);
+void loadPageTable(void* tableAddress);
+void switch_mapping(memmap_t* mapping);
+void enablePaging();
+
+// task related functions:
+void initialize_tasking();
+void new_task();
+void task_scheduler();
+
+// STDout funcs:
+void printf(const char* fmt, ... );
+void memset(void* ptr, uint8_t value, uint64_t num);
+void memcpy(void* dst, void* src, uint64_t num);
+int strncmp(const char* s1, const char* s2, int size); 
+
+// Screen related:
+void clear_screen();
+void init_screen();
+void print_string(const char *);
+void set_cursor(int newrow, int newcol);
+void get_cursor(int *therow, int *thecol);
+void set_color(int forecolor, int backcolor);
+
+// For IO ports:
+uint8_t inb(uint16_t);
+uint16_t inw(uint16_t);
+void outb(uint16_t, uint8_t);
+
+// Interrupt functions:
+void setupIDT(void);
+void PICremap(void);
+void loadIDT(void);
+
+// ASM helper:
+uint64_t read_rip();
+
+// Helpers:
+void halt(void);
+void panic(char *msg);
+void reboot(void);
+void magicBochsBreak();
+void doCPUID(int eax, int *ebx, int *ecx, int *edx);
+
+// Keyboard driver:
+void keyboardDriverUpdate(unsigned char scancode);
+void getline(char *buffer, int len);
+
+// Timer:
+void timerDriverUpdate(void);
+uint64_t getTimeMS();
 
 #endif