From 0365bbb5c58912fd24b3d33b90477d3de5d46d96 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Fri, 15 May 2015 02:06:48 +0200 Subject: fixes and imporvements --- kernel/kernel.c | 9 ++++++--- kernel/kmalloc.c | 48 +++++++++++++++++++++++++++++++++++++++++++--- kernel/kmalloc.h | 2 +- kernel/mem.c | 10 +++++++--- kernel/multiboot.c | 2 +- kernel/multiboot.h | 3 +++ kernel/vmem.c | 56 ++++++++++++++++++++++++++++++------------------------ 7 files changed, 94 insertions(+), 36 deletions(-) (limited to 'kernel') diff --git a/kernel/kernel.c b/kernel/kernel.c index 486ec19..bdadd45 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -62,8 +62,7 @@ void kernel_main(uint32_t eax,uint32_t ebx) // // Activate Virtual Memory (paging) // - pdirectory *dir=vmem_init(0); - + pdirectory *dir=vmem_new_space_dir(NULL); // // Setup Interrupts (code segment: 0x08) @@ -71,7 +70,6 @@ void kernel_main(uint32_t eax,uint32_t ebx) int_init(0x08); - // // Scan the PCI Bus // @@ -99,6 +97,11 @@ void kernel_main(uint32_t eax,uint32_t ebx) //smp_log_procdata(&procdata); //smp_start_aps(&procdata,0x80000); // starts at 0x80000 // but it will be copied over mbr + // + // Mount Root EXT2 ramimage + // + fs_mount(0x945000); + // // Initialize Multitasking diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c index 4939e05..a3b4ab6 100644 --- a/kernel/kmalloc.c +++ b/kernel/kmalloc.c @@ -1,9 +1,51 @@ +#define FOOLOS_MODULE_NAME "kmalloc" #include "kmalloc.h" #include +#include "lib/logger/log.h" -static uint8_t data[1024*1024*8]; //8MB kernel memory managed by kmalloc +#define MEM_SIZE 1024*1024*8 -uint32_t kmalloc(uint32_t size) +static uint8_t data[MEM_SIZE]; //8MB kernel memory managed by kmalloc +static uint32_t next; +static uint32_t first; +static uint8_t init=0; + +void kmallocinit() +{ + next=&(data[0]); + first=next; + + if(next%4096) //align (TODO: check how to tell gcc to do that) + { + next+=4096; + next/=4096; + next*=4096; + } + + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"kmalloc_init: 0x%08X",next); + init=1; +} + +// kernel block memory allocation +uint32_t kballoc(uint32_t size) { - return NULL; + size*=4096; + if(!init)kmallocinit(); + uint32_t old=next; + next+=size; + + if(next>=first+MEM_SIZE) + { + panic(FOOLOS_MODULE_NAME,"kballoc ran out of memory! maybe increase MEM_SIZE in kmalloc.c?"); + } + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"(%d) : 0x%08X (~%dKB left)",size,old,(MEM_SIZE-next+first)/1024); + + return old; +} + +//TODO! +uint32_t kbfree(uint32_t pos) +{ + } + diff --git a/kernel/kmalloc.h b/kernel/kmalloc.h index 330546b..2f89f8a 100644 --- a/kernel/kmalloc.h +++ b/kernel/kmalloc.h @@ -1,2 +1,2 @@ #include -uint32_t kmalloc(uint32_t size); +uint32_t kballoc(uint32_t size); diff --git a/kernel/mem.c b/kernel/mem.c index f33b5d8..7c55a67 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -25,7 +25,7 @@ static uint32_t _mmngr_memory_map[PMMNGR_MAP_SIZE]; static uint32_t mem_free_blocks; //number of free blocks static uint32_t mem_max_block; //index of highest usable block -static uint32_t mem_min_block; //index of lowset block available to userspace +static uint32_t mem_min_block; //index of lowset block available for alloc char *memmap_type_to_string[]= { @@ -208,14 +208,18 @@ void mem_init(multiboot_information *info) mem_min_block=mod->mod_end/PMMNGR_BLOCK_SIZE+1; - pmmngr_deinit_region(mod->mod_start,((uint32_t)mod->mod_end-(uint32_t)mod->mod_start)+1); + //pmmngr_deinit_region(mod->mod_start,((uint32_t)mod->mod_end-(uint32_t)mod->mod_start)+1); mod++; } } // deinitialize kernel - pmmngr_deinit_region(kernel_start,((uint32_t)kernel_end-(uint32_t)kernel_start)+1); + //pmmngr_deinit_region(kernel_start,((uint32_t)kernel_end-(uint32_t)kernel_start)+1); + + // we deinit everything below mem_min_block anyway + pmmngr_deinit_region(0,mem_min_block*PMMNGR_BLOCK_SIZE); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"Usable ~%d / %d MB ",mem_free_blocks*4096/1024/1024,total_mem/1024/1024); diff --git a/kernel/multiboot.c b/kernel/multiboot.c index 4272f0f..3132717 100644 --- a/kernel/multiboot.c +++ b/kernel/multiboot.c @@ -2,7 +2,7 @@ #include "multiboot.h" #include "lib/logger/log.h" -uint32_t* get_multiboot(uint32_t eax, uint32_t ebx) +multiboot_information* get_multiboot(uint32_t eax, uint32_t ebx) { if(eax!=0x2badb002)panic(FOOLOS_MODULE_NAME,"EAX was not set properly by your bootlaoder!"); diff --git a/kernel/multiboot.h b/kernel/multiboot.h index 292b6f3..bb31a2e 100644 --- a/kernel/multiboot.h +++ b/kernel/multiboot.h @@ -6,6 +6,7 @@ #include #include + typedef struct multiboot_information_struct { uint32_t flags; @@ -52,4 +53,6 @@ typedef struct multiboot_mod_struct }multiboot_mod; +multiboot_information* get_multiboot(uint32_t eax, uint32_t ebx); + #endif diff --git a/kernel/vmem.c b/kernel/vmem.c index 402580a..491f486 100644 --- a/kernel/vmem.c +++ b/kernel/vmem.c @@ -224,25 +224,33 @@ void vmem_free_dir(pdirectory *dir) x86_paging_enable(); } + + // -// vmem init and also copies all the shit for FORK -// for now it allocates always 3 * 1024 * 4096 bytes at for the kernel -// virtual / physical 0x0-0xc00000 (~12MB) -// 2*1024*4096 for the loaded prog -// virtual 0x8000000-0x8800000 (~8MB) -// and 1*1024*4096 for the loaded programms stack -// 0x8c00000-0x9000000 (~4MB) +// vmem init / also copies all the shit over for FORK +// if copy_dir==NULL creates brandnew dir +// otherwise copies kernel pages and copies +// programm pages procreates new programmspace // +// TODO: FIX +// KERNEL SPACE HARDCODED TO 5 first PAGES +// PROGRAMM SPACE HARDCODED TO 0x8000000+2 pages and 0x8c00000+1 pages +// + pdirectory* vmem_new_space_dir(pdirectory *copy_dir) { x86_paging_disable(); - - pdirectory* dir = (pdirectory*) pmmngr_alloc_block (); - if (!dir)panic(FOOLOS_MODULE_NAME,"unable to alloc pdirectory"); + + // + pdirectory* dir = (pdirectory*) kballoc(1); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"new pdirectory: 0x%X",dir); + if (!dir)panic(FOOLOS_MODULE_NAME,"unable to alloc pdirectory"); + // first of all let's zero all the entries for(int i=0;i<1024;i++)dir->m_entries [i]=0; + // identity mapping for first blocks uint32_t phys_addr=0; uint32_t virt_addr=0; @@ -257,7 +265,7 @@ pdirectory* vmem_new_space_dir(pdirectory *copy_dir) if(copy_dir==NULL) { // alloc space for our new table - table = (ptable*) pmmngr_alloc_block (); + table = (ptable*) kballoc(1); if (!table)panic(FOOLOS_MODULE_NAME,"unable to alloc table"); //! idenitity mapping @@ -280,6 +288,7 @@ pdirectory* vmem_new_space_dir(pdirectory *copy_dir) pd_entry_set_frame (entry, (physical_addr)table); } + // otherwise simply take existing stuff from pdir 0 else { @@ -292,12 +301,13 @@ pdirectory* vmem_new_space_dir(pdirectory *copy_dir) virt_addr+=1024*4096; } + // programm space virt_addr=0x8000000; for(int j=0;j<2;j++) { - ptable* table = (ptable*) pmmngr_alloc_block (); + ptable* table = (ptable*) kballoc (1); pd_entry *oldentry=NULL; ptable* oldtable=NULL; @@ -355,7 +365,7 @@ pdirectory* vmem_new_space_dir(pdirectory *copy_dir) for(int j=0;j<1;j++) { - ptable* table = (ptable*) pmmngr_alloc_block (); + ptable* table = (ptable*) kballoc (1); pd_entry *oldentry=NULL; ptable* oldtable=NULL; @@ -407,7 +417,14 @@ pdirectory* vmem_new_space_dir(pdirectory *copy_dir) } - if(copy_dir!=NULL)x86_paging_enable(); + if(copy_dir==NULL) // this happens only on init + { + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"initializing virtual memory (paging)"); + vmem_set_dir(dir); + } + +//while(1); + x86_paging_enable(); return dir; } @@ -417,14 +434,3 @@ void vmem_set_dir(pdirectory *dir) x86_set_pdbr(dir); } - - -pdirectory* vmem_init(uint32_t mem_block_start) -{ - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"init paging"); - pdirectory *dir=vmem_new_space_dir(0); - vmem_set_dir(dir); - x86_paging_enable(); - return dir; -} - -- cgit v1.2.3