diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | fs/elf.c | 7 | ||||
| -rw-r--r-- | fs/ext2.c | 9 | ||||
| -rw-r--r-- | fs/fs.c | 19 | ||||
| -rw-r--r-- | fs/fs.h | 6 | ||||
| -rw-r--r-- | kernel/kernel.c | 9 | ||||
| -rw-r--r-- | kernel/kmalloc.c | 48 | ||||
| -rw-r--r-- | kernel/kmalloc.h | 2 | ||||
| -rw-r--r-- | kernel/mem.c | 10 | ||||
| -rw-r--r-- | kernel/multiboot.c | 2 | ||||
| -rw-r--r-- | kernel/multiboot.h | 3 | ||||
| -rw-r--r-- | kernel/vmem.c | 56 | ||||
| -rw-r--r-- | lib/logger/log.c | 8 | ||||
| -rw-r--r-- | terminal/vt52.c | 4 |
14 files changed, 133 insertions, 52 deletions
@@ -158,7 +158,7 @@ run-qemu: all run-qemu-debug: all # qemu -enable-kvm -s -S ~/temp/FoolOs/disk.img - qemu -enable-kvm -s disk.img + qemu -enable-kvm -s -singlestep disk.img stop: killall qemu @@ -69,8 +69,9 @@ Elf32_Phdr; // returns elf entry point uint32_t load_elf(char *name, uint32_t *alloc) { + uint32_t ext2_ramimage= fs_get_root_ext2_ramimage(); - int inode_nr=ext2_filename_to_inode(EXT2_RAM_ADDRESS,name); + int inode_nr=ext2_filename_to_inode(ext2_ramimage,name); if(inode_nr<1)return 0; //TODO: load ELF binary and move this to own compilation unit @@ -80,8 +81,8 @@ uint32_t load_elf(char *name, uint32_t *alloc) uint32_t vaddr=0x08000000; log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"loading %s",name); - ext2_check(EXT2_RAM_ADDRESS); - ext2_inode_content(EXT2_RAM_ADDRESS,inode_nr,vaddr,0x100000); // load 1mb; + ext2_check(ext2_ramimage); + ext2_inode_content(ext2_ramimage,inode_nr,vaddr,0x100000); // load 1mb; log(FOOLOS_MODULE_NAME,FOOLOS_LOG_DEBUG,"ELF File loaded to final destination."); Elf32_Ehdr *elf; @@ -120,7 +120,8 @@ int ext2_check(uint8_t *ram) ram_read(ptr,&super,sizeof(super),1); if(super.ext2_sig!=0xef53){ - panic(FOOLOS_MODULE_NAME,"no ext2 signature found, where ram-image expected!"); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"addr: 0x%08X",ram); + panic(FOOLOS_MODULE_NAME,"no ext2 signature found, where ram-image expected"); } } @@ -134,7 +135,11 @@ ext2_inode ext2_get_inode(uint8_t *ram,int inode_num) uint8_t *ptr=ram+1024; ram_read(ptr,&super,sizeof(super),1); - if(super.ext2_sig!=0xef53)panic(FOOLOS_MODULE_NAME,"no ext2 signature found, where ram-image expected!"); + if(super.ext2_sig!=0xef53) + { + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"addr: 0x%08X",ram); + panic(FOOLOS_MODULE_NAME,"no ext2 signature found, where ram-image expected!"); + } int block_group=(inode_num-1)/super.inodes_per_group; @@ -2,17 +2,28 @@ #include "fs.h" #include "ext2.h" + + +static uint32_t root_ext2_ramimage=0; + // // returns number of entries in the directory specified by name. // fills 0-max into *dirs int fs_readdir(const char *name,fs_dirent *dirs,int max) { - int inode_nr=ext2_filename_to_inode(EXT2_RAM_ADDRESS,name); + + int inode_nr=ext2_filename_to_inode(root_ext2_ramimage,name); if(inode_nr<1)return -1; - return ext2_read_dir(EXT2_RAM_ADDRESS, inode_nr,dirs,max); // TODO: hardcoded, fix this + return ext2_read_dir(root_ext2_ramimage, inode_nr,dirs,max); // TODO: hardcoded, fix this } +void fs_mount(uint32_t ext2_ramimage) +{ + root_ext2_ramimage=ext2_ramimage; +} - - +uint32_t fs_get_root_ext2_ramimage() +{ + return root_ext2_ramimage; +} @@ -3,9 +3,6 @@ #include <stdint.h> - -#define EXT2_RAM_ADDRESS 0x124000 // here the bootloader puts our image - enum FS_FILE_TYPE{ FS_FILE_TYPE_DIR = 1, @@ -20,6 +17,9 @@ typedef struct fs_dirent_struct uint8_t type; char name[256]; }fs_dirent; + int fs_readdir(const char *name,fs_dirent *dirs,int max); +void fs_mount(uint32_t ext2_ramimage); // all you can mount so far is just one single + // ext2 ramimage as root #endif 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 <stddef.h> +#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 <stdint.h> -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 <stdbool.h> #include <stdint.h> + 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; -} - diff --git a/lib/logger/log.c b/lib/logger/log.c index a51ccf5..bcfedf0 100644 --- a/lib/logger/log.c +++ b/lib/logger/log.c @@ -59,7 +59,13 @@ void panic(char *module_name, char *message) char buf_log[256]; tfp_sprintf(buf_log,"KERNEL PANIC !! %s: %s\n",module_name,message); console_put_str_red(buf_log); - while(1); // halt + + while(1) + { + asm volatile("cli"); + asm volatile("hlt"); + } + } diff --git a/terminal/vt52.c b/terminal/vt52.c index 813cf40..aa24435 100644 --- a/terminal/vt52.c +++ b/terminal/vt52.c @@ -11,7 +11,7 @@ // REQUIREMENTS -// * kmalloc +// * kballoc // block alloc #include <stdint.h> #include "kernel/kmalloc.h" @@ -33,7 +33,7 @@ typedef struct vt52_tty_struct vt52_tty *vt52_init() { - vt52_tty *tty=kmalloc(sizeof(vt52_tty)); + vt52_tty *tty=kballoc(sizeof(vt52_tty)); return tty; } |
