diff --git a/cpu/i386/kmalloc.c b/cpu/i386/kmalloc.c new file mode 100644 index 0000000..8e657e8 --- /dev/null +++ b/cpu/i386/kmalloc.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +static char bitmap[1048576]; +static void* data=(void*)0xFF400000; + + +static char get_bmap_bit(uint32_t index) { + uint32_t byte=index/8; + uint32_t bit=index%8; + char entry=bitmap[byte]; + return (entry&(1<0; +} + +static void set_bmap_bit(uint32_t index) { + uint32_t byte=index/8; + uint32_t bit=index%8; + bitmap[byte]=bitmap[byte]|(1< + +void* kmalloc(size_t size); +void kfree(void* mem); + +#endif diff --git a/cpu/i386/paging.c b/cpu/i386/paging.c index 6c89de3..d991fea 100644 --- a/cpu/i386/paging.c +++ b/cpu/i386/paging.c @@ -7,6 +7,7 @@ static uint32_t page_directory[1024] __attribute__((aligned(4096))); static uint32_t kern_page_tables[NUM_KERN_DIRS*1024] __attribute__((aligned(4096))); +static uint32_t kmalloc_page_tables[1024] __attribute__((aligned(4096))); static uint32_t smap_page_tables[2048] __attribute__((aligned(4096))); static uint32_t* smap=(uint32_t*)0xFF800000; @@ -102,11 +103,15 @@ void* new_address_space() { uint32_t entry_virt=(uint32_t)&(kern_page_tables[i*1024]); smap[i+768]=(entry_virt-0xC0000000)|0x7; } + smap[1021]=(((uint32_t)kmalloc_page_tables)-0xC0000000)|0x7; for (uint32_t i=0;i<2;i++) { uint32_t entry_virt=(uint32_t)&(smap_page_tables[i*1024]); smap[i+1022]=(entry_virt-0xC0000000)|0x3; } smap_page_tables[0]=(((uint32_t)&(page_directory))-0xC0000000)|0x3; + for (uint32_t i=1;i<2048;i++) { + smap_page_tables[i]=0; + } return dir; } @@ -115,12 +120,16 @@ void load_address_space(uint32_t cr3) { for (uint32_t i=1;i<2048;i++) { smap_page_tables[i]=0; } + load_page_directory((uint32_t*)cr3); } void paging_init() { for (uint32_t i=0;i +#include "kmalloc.h" #include "memory.h" #include "gdt.h" #include @@ -28,7 +28,7 @@ void tasking_init() { } static Task* createTaskKmode(void* eip,char kmode) { - Task* task=malloc(sizeof(Task)); + Task* task=kmalloc(sizeof(Task)); task->kmode=kmode; task->regs.eax=0; task->regs.ebx=0; @@ -38,8 +38,12 @@ static Task* createTaskKmode(void* eip,char kmode) { task->regs.edi=0; asm volatile("pushfl; movl (%%esp), %%eax; movl %%eax, %0; popfl;":"=m"(task->regs.eflags)::"%eax"); task->regs.eip=(uint32_t)eip; - asm volatile("movl %%cr3, %%eax; movl %%eax, %0;":"=m"(task->regs.cr3)::"%eax"); - task->regs.esp=(uint32_t)alloc_memory(1); + task->regs.cr3=new_address_space(); + uint32_t cr3; + asm volatile("movl %%cr3, %%eax; movl %%eax, %0;":"=m"(cr3)::"%eax"); + load_address_space(task->regs.cr3); + task->regs.esp=((uint32_t)alloc_memory(1))+0xfff; + load_address_space(cr3); task->regs.ebp=0; task->msg_store=NULL; task->rd=0; @@ -70,7 +74,7 @@ char isPrivleged(uint32_t pid) { } Task* tasking_createTask(void* eip) { - return createTaskKmode(eip,1); + return createTaskKmode(eip,0); } void send_msg(uint32_t pid,void* msg) { @@ -119,7 +123,12 @@ void tasking_yield() { } Task* oldCurr=currentTask; currentTask=task; - asm("mov %%eax,%%cr3":: "a"(task->regs.cr3)); + load_address_space(task->regs.cr3); + if (task->priv) { + allow_all_ports(); + } else { + block_all_ports(); + } if (!task->kmode) { asm volatile(" \ cli; \ @@ -139,10 +148,5 @@ void tasking_yield() { 1: \ "); } - if (task->priv) { - allow_all_ports(); - } else { - block_all_ports(); - } switchTask(&oldCurr->regs, ¤tTask->regs); }