From 5bca438d98fb18b7f56773232be35d594bcb4700 Mon Sep 17 00:00:00 2001 From: pjht Date: Sat, 23 Mar 2019 07:30:00 -0500 Subject: [PATCH 1/6] Add kmalloc --- cpu/i386/kmalloc.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++ cpu/i386/kmalloc.h | 9 +++++ cpu/i386/paging.c | 7 ++++ 3 files changed, 108 insertions(+) create mode 100644 cpu/i386/kmalloc.c create mode 100644 cpu/i386/kmalloc.h 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..782add0 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,6 +103,7 @@ 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)|0x3; 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; @@ -115,12 +117,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 Date: Sat, 23 Mar 2019 07:30:18 -0500 Subject: [PATCH 2/6] Kmalloc is now used to allocate task structures --- cpu/i386/tasking.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cpu/i386/tasking.c b/cpu/i386/tasking.c index 8721439..5502e54 100644 --- a/cpu/i386/tasking.c +++ b/cpu/i386/tasking.c @@ -7,8 +7,8 @@ push %eax; \ #include "tasking.h" #include "../tasking.h" #include "isr.h" -#include "../../libc/stdlib.h" -#include "../../libc/stdio.h" +#include +#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; From 2b65782369a4bbf8cf2dd83976c2d468d522df63 Mon Sep 17 00:00:00 2001 From: pjht Date: Sat, 23 Mar 2019 10:35:11 -0500 Subject: [PATCH 3/6] Tasks now have separate address spaces When switchTask loads the stack, the next push intruction fails, but the stack can be manually accessed fine. --- cpu/i386/tasking.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cpu/i386/tasking.c b/cpu/i386/tasking.c index 5502e54..b60408a 100644 --- a/cpu/i386/tasking.c +++ b/cpu/i386/tasking.c @@ -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.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); + load_address_space(cr3); task->regs.ebp=0; task->msg_store=NULL; task->rd=0; @@ -119,7 +123,7 @@ void tasking_yield() { } Task* oldCurr=currentTask; currentTask=task; - asm("mov %%eax,%%cr3":: "a"(task->regs.cr3)); + // load_address_space(task->regs.cr3); if (!task->kmode) { asm volatile(" \ cli; \ From e1579b5f2a61fc2796ac0d0106f1bb6c6f17dcd1 Mon Sep 17 00:00:00 2001 From: pjht Date: Sat, 23 Mar 2019 11:14:59 -0500 Subject: [PATCH 4/6] Yield now loads the address space --- cpu/i386/tasking.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu/i386/tasking.c b/cpu/i386/tasking.c index b60408a..3b1c861 100644 --- a/cpu/i386/tasking.c +++ b/cpu/i386/tasking.c @@ -123,7 +123,7 @@ void tasking_yield() { } Task* oldCurr=currentTask; currentTask=task; - // load_address_space(task->regs.cr3); + load_address_space(task->regs.cr3); if (!task->kmode) { asm volatile(" \ cli; \ From 5618dd683432272b42ed224f82d079a358e5edc4 Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 24 Mar 2019 13:42:00 -0500 Subject: [PATCH 5/6] Fixed reboot problem STACK GROWS DOWN, NOT UP! --- cpu/i386/paging.c | 3 +++ cpu/i386/tasking.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cpu/i386/paging.c b/cpu/i386/paging.c index 782add0..1f0bd86 100644 --- a/cpu/i386/paging.c +++ b/cpu/i386/paging.c @@ -109,6 +109,9 @@ void* new_address_space() { 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; } diff --git a/cpu/i386/tasking.c b/cpu/i386/tasking.c index 3b1c861..8d10f57 100644 --- a/cpu/i386/tasking.c +++ b/cpu/i386/tasking.c @@ -42,7 +42,7 @@ static Task* createTaskKmode(void* eip,char kmode) { 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); + task->regs.esp=((uint32_t)alloc_memory(1))+0xfff; load_address_space(cr3); task->regs.ebp=0; task->msg_store=NULL; From 0c50e1eb8c761e6502aab78f2bb66c80b23c65d2 Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 24 Mar 2019 14:25:32 -0500 Subject: [PATCH 6/6] Fixed interrupts using wrong stack Tasks were running in kernel mode, so the tss stack was not being used. Unfortunatley, I had to set kmalloc to user r/w so switchTask can save and load registers. --- cpu/i386/paging.c | 6 +++--- cpu/i386/tasking.c | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cpu/i386/paging.c b/cpu/i386/paging.c index 1f0bd86..d991fea 100644 --- a/cpu/i386/paging.c +++ b/cpu/i386/paging.c @@ -103,7 +103,7 @@ 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)|0x3; + 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; @@ -128,7 +128,7 @@ void paging_init() { kern_page_tables[i]=(i<<12)|0x7; } for (uint32_t i=0;i<1024;i++) { - kmalloc_page_tables[i]=(uint32_t)pmem_alloc(1)|0x3; + kmalloc_page_tables[i]=(uint32_t)pmem_alloc(1)|0x7; } smap_page_tables[0]=(((uint32_t)&(page_directory))-0xC0000000)|0x3; for (uint32_t i=1;i<2048;i++) { @@ -138,7 +138,7 @@ void paging_init() { uint32_t entry_virt=(uint32_t)&(kern_page_tables[i*1024]); page_directory[i+768]=(entry_virt-0xC0000000)|0x7; } - page_directory[1021]=(((uint32_t)kmalloc_page_tables)-0xC0000000)|0x3; + page_directory[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]); page_directory[i+1022]=(entry_virt-0xC0000000)|0x3; diff --git a/cpu/i386/tasking.c b/cpu/i386/tasking.c index 8d10f57..64f4f4c 100644 --- a/cpu/i386/tasking.c +++ b/cpu/i386/tasking.c @@ -74,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) { @@ -124,6 +124,11 @@ void tasking_yield() { Task* oldCurr=currentTask; currentTask=task; load_address_space(task->regs.cr3); + if (task->priv) { + allow_all_ports(); + } else { + block_all_ports(); + } if (!task->kmode) { asm volatile(" \ cli; \ @@ -143,10 +148,5 @@ void tasking_yield() { 1: \ "); } - if (task->priv) { - allow_all_ports(); - } else { - block_all_ports(); - } switchTask(&oldCurr->regs, ¤tTask->regs); }