From 06e382afcbf199e5e4ec92574a3872ab04fb6e9e Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Thu, 4 Sep 2014 15:16:13 +0200 Subject: Improved physical memory manager and cleaning up. --- Makefile | 12 +-- bochsrc | 2 +- kernel/interrupts.c | 29 +++++- kernel/interrupts.h | 2 - kernel/kernel.c | 105 ++++++-------------- kernel/mem.c | 269 +++++++++++++++++++++------------------------------- kernel/timer.c | 34 ------- kernel/vesa.c | 15 +-- lib/logger/log.c | 68 +++++++++---- 9 files changed, 220 insertions(+), 316 deletions(-) diff --git a/Makefile b/Makefile index 928a9fb..d59d676 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,6 @@ USB_STICK=/dev/sdg #take care! -MP_IMG_START=26112 #code for the other processors intialllly at 0x6600 - -FONT_DATA_START=26624 #fool font initially at 0x6800 - #here our kernel will be loaded by the bootloader. KERNEL_START=0x10000 @@ -48,7 +44,6 @@ KERNEL_ENTRY=./boot/kernel_entry.o MBR=./boot/mbr.bin - #multiprocessor binary entry MP_BIN=./boot/mp.bin @@ -63,7 +58,6 @@ FILLUP=./data/fill.bin all: FoolOS.img FoolData.img - ############ nasm assembling rules ############ %.o: %.asm @@ -90,17 +84,15 @@ binfont.bin: data/binfont.src # master boot record, kernel binary and fool-font -FoolOS.img: $(MBR) kernel.bin $(MP_BIN) binfont.img $(FILLUP) +FoolOS.img: $(MBR) kernel.bin $(FILLUP) cp $(FILLUP) $@ dd if=$(MBR) of=$@ bs=1 seek=0 conv=notrunc dd if=kernel.bin of=$@ bs=1 seek=512 conv=notrunc - dd if=$(MP_BIN) of=$@ bs=1 seek=$(MP_IMG_START) conv=notrunc - dd if=binfont.img of=$@ bs=1 seek=$(FONT_DATA_START) conv=notrunc binfont.img: binfont.bin cat $^ > $@ -FoolData.img: data/testdata +FoolData.img: binfont.bin cp $^ $@ ############ vm stuff ############ diff --git a/bochsrc b/bochsrc index 45fb444..5ebd7ad 100644 --- a/bochsrc +++ b/bochsrc @@ -242,7 +242,7 @@ cpu: count=2, ips=10000000 #, reset_on_triple_fault=1, ignore_bad_msrs=1, msrs=" #optromimage3: file=optionalrom.bin, address=0xd2000 #optromimage4: file=optionalrom.bin, address=0xd3000 -optramimage1: file=FoolData.img, address=0x80400 +optramimage1: file=FoolData.img, address=0x90000 #optramimage2: file=/path/file2.img, address=0x0020000 #optramimage3: file=/path/file3.img, address=0x0030000 #optramimage4: file=/path/file4.img, address=0x0040000 diff --git a/kernel/interrupts.c b/kernel/interrupts.c index c6acf61..902538b 100644 --- a/kernel/interrupts.c +++ b/kernel/interrupts.c @@ -1,13 +1,17 @@ +#define FOOLOS_MODULE_NAME "interrupts" + +#include "lib/logger/log.h" // logger facilities + #include "interrupts.h" #include "lib/int/stdint.h" #include "x86.h" -#include "../lib/logger/log.h" // logger facilities -#define FOOLOS_MODULE_NAME "interrupts" +void int_clock_handler(); +void int_kb_handler(); +void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr); void int_default_handler(); -void int_install_ir(int irq, uint16_t flags, uint16_t sel, void *addr); // the interrupt descriptor table static struct int_desc @@ -125,6 +129,25 @@ void int_init(uint16_t sel) int_install_ir(17, 0b10001110, 0x08,&int_irq17); int_install_ir(18, 0b10001110, 0x08,&int_irq18); + // setup some custom interrupts + // remember that we shifted all interrupts with the pic by 32 + + // install PIT interrupt handler (irq 0 => 32) + int_install_ir(32, 0b10001110, 0x08,&int_clock_handler); + + // install keyboard interrupt handler (irq 1 => 33) + int_install_ir(33, 0b10001110, 0x08,&int_kb_handler); + + #ifdef FOOLOS_COMPILE_FLOPPY + // install floppy interrupt handler (irq 6 => 38) + int_install_ir(38, 0b10001110, 0x08,&int_floppy_handler); + #endif + + int_install(); + + // now we can enable interrupts back again + x86_int_enable(); + } void int_install() diff --git a/kernel/interrupts.h b/kernel/interrupts.h index 6483347..16eb7a6 100644 --- a/kernel/interrupts.h +++ b/kernel/interrupts.h @@ -3,6 +3,4 @@ #define INT_MAX 255 // size of our interrupts table -int int_unhandled; // counting unhandled interrupts - #endif diff --git a/kernel/kernel.c b/kernel/kernel.c index 0b87b64..1688017 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -11,18 +11,10 @@ #include "floppy.h" #endif -/////// -// interrupt handler prototypes -// todo: move somewhere else!? -void int_clock_handler(); -void int_kb_handler(); - -uint32_t read_eip(); +// some multiprocessor shit that should move away uint32_t c1,c2,c3; - volatile uint8_t proc; - uint32_t cpu_counter[SMP_MAX_PROC]; void kernel_ap() @@ -31,6 +23,7 @@ void kernel_ap() uint8_t p=proc; while(1)cpu_counter[p]++; } + // // KERNEL MAIN @@ -52,29 +45,18 @@ void kernel_main(uint32_t initial_stack, int mp) proc=c1=c2=c3=0; for(int i=0;i 32) - int_install_ir(32, 0b10001110, 0x08,&int_clock_handler); - - // install keyboard interrupt handler (irq 1 => 33) - int_install_ir(33, 0b10001110, 0x08,&int_kb_handler); - -#ifdef FOOLOS_COMPILE_FLOPPY - // install floppy interrupt handler (irq 6 => 38) - int_install_ir(38, 0b10001110, 0x08,&int_floppy_handler); -#endif - - // now we can enable interrupts back again - x86_int_enable(); // // Memory Init // + // after this is set up we can allocate and deallocate blocks + // of physical memory :) + // - // we know that here, the bootloader placed the mamory map! + // we know that here, the bootloader placed the mamory map and + // number of entries. mem_init(0x7c00+0x400,*((uint16_t *)(0x7c00+0x600))); @@ -174,13 +131,13 @@ void kernel_main(uint32_t initial_stack, int mp) panic(FOOLOS_MODULE_NAME,"ACPI and MP search failed! I do not want to continue!"); // Start the other Processors (also before paging !) - smp_start_aps(&procdata); + //smp_start_aps(&procdata); ///////////////////// // paging (pass the vesa physbase address for identity mapping) - //vmem_init(vesa_physbase); + // vmem_init(vesa_physbase); ////////////////////// @@ -190,17 +147,15 @@ void kernel_main(uint32_t initial_stack, int mp) // We are interested in the E1000 Network Adapter in particular // Its driver will be hopefully implemented one day ;) // - - pci_init(); + // pci_init(); // - // Initialize Floppy Disk - // - - #ifdef FOOLOS_COMPILE_FLOPPY - floppy_init(); - #endif + // Initialize Floppy Disk if activated in config.h + // + //#ifdef FOOLOS_COMPILE_FLOPPY + //floppy_init(); + //#endif // @@ -209,7 +164,6 @@ void kernel_main(uint32_t initial_stack, int mp) // Will process input from the keyboard but will be completely // redesigned soon. // - shell_init(); @@ -219,7 +173,6 @@ void kernel_main(uint32_t initial_stack, int mp) // For now this starts two tasks which are scheduled // round robin style. // - task_init(); diff --git a/kernel/mem.c b/kernel/mem.c index 9899822..0b9dab7 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -1,9 +1,9 @@ +#define FOOLOS_MODULE_NAME "mem" #define MEM_PRINT_MEMORYMAP #include "lib/int/stdint.h" #include "lib/logger/log.h" // logger facilities -#define FOOLOS_MODULE_NAME "mem" //! 8 blocks per byte #define PMMNGR_BLOCKS_PER_BYTE 8 @@ -14,14 +14,11 @@ //! block alignment #define PMMNGR_BLOCK_ALIGN PMMNGR_BLOCK_SIZE -#define MEM_BITMAP_SIZE 32768 -//#define MEM_BITMAP_SIZE 327 - -// memory map bit array. Each bit represents a memory block -//uint32_t _mmngr_memory_map[MEM_BITMAP_SIZE]; -uint32_t _mmngr_memory_map[MEM_BITMAP_SIZE]; +//memory map bit array. Each bit represents a 4KB memory block +static uint32_t *_mmngr_memory_map; static uint32_t mem_free_blocks; +static uint32_t mem_array_size; // bit funcs! void mmap_set(int bit) @@ -39,127 +36,79 @@ int mmap_test(int bit) return _mmngr_memory_map[bit / 32] & (1 << (bit % 32)); } -void mmap_show_free () +void pmmngr_init () { - - int last_pos=0; - uint32_t last=_mmngr_memory_map[0]; - - for (int i=1; i< MEM_BITMAP_SIZE ; i++) + // By default, all of memory is in use + for(int i=0;i0; blocks--) { - _mmngr_memory_map[i]=0xffffffff; + mmap_unset (align++); + mem_free_blocks++; } - - } -void pmmngr_init_region (uint32_t base, uint32_t size) +void pmmngr_deinit_region (uint32_t base, uint32_t size) { - - uint32_t align = base / PMMNGR_BLOCK_SIZE; uint32_t blocks = size / PMMNGR_BLOCK_SIZE; for (; blocks>0; blocks--) { - // hack to lock first ~4MB of memory - if(align<1000) - { - align++; - continue; - } - - - mmap_unset (align++); - mem_free_blocks++; - //_mmngr_used_blocks--; + mmap_set (align++); + mem_free_blocks--; } - //mmap_set (0); //first block is always set. This insures allocs cant be 0 } void* pmmngr_alloc_block () { - int frame = mmap_first_free (); - + if (frame == -1) { log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"OUT OF MEMORY"); return 0; //out of memory } -// scr_put_string_nl("MEM ALLOC OK!"); -// scr_put_hex32(frame); - // scr_put_string_nl(""); - mmap_set (frame); - + uint32_t addr = frame * PMMNGR_BLOCK_SIZE; mem_free_blocks--; - + return (void*)addr; } -void pmmngr_free_block (void* p) { - +void pmmngr_free_block (void* p) +{ uint32_t addr = (uint32_t*)p; int frame = addr / PMMNGR_BLOCK_SIZE; @@ -168,125 +117,121 @@ void pmmngr_free_block (void* p) { mmap_unset (frame); mem_free_blocks++; } - } -/* -void mem_test_3() +void mem_init(uint16_t *memmap,uint16_t entries) { - uint32_t *addr; - addr=pmmngr_alloc_block(); - scr_put_string("alloc 1: "); - scr_put_hex32(addr); - scr_put_string_nl(""); + // init free blocks counter + mem_free_blocks=0; - addr=pmmngr_alloc_block(); - scr_put_string("alloc 2: "); - scr_put_hex32(addr); - scr_put_string_nl(""); -} + // count available mem + uint32_t total_mem=0, highest_end=0, high_end_low=0; -void mem_test_2() -{ - //crossing 512MB boundary! - // (do not do this in real, coz it probably is reserved) - //mem_test(0x1fee1000,0x2000f000,0x800); + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"the memory map contains %d entries.",entries); - int cnt=-1; - uint32_t *addr; - uint32_t *lastaddr; + // preserve pointer + uint16_t save=memmap; - do{ - lastaddr=addr; - addr=pmmngr_alloc_block(); - cnt++; - }while(addr!=0); + //print memory map and calc blocks. + for(int i=0;ihighest_end){ + highest_end=high_end; + high_end_low=low_end; + } + } + memmap+=12; + + } + // restore pointer + memmap=save; - cnt=-1; - do{ - lastaddr=addr; - addr=pmmngr_alloc_block(); - cnt++; - }while(addr!=0); + int blocks=highest_end/4096+1; + mem_array_size=blocks/32+1; - scr_put_string("no mem after : "); - scr_put_hex32(cnt); - scr_put_string_nl(""); -} - -void mem_test(int start, int end, int steps) -{ - int *p=start; - for(;pphysbase=addr; } - uint32_t vesa_init(vbeinfo *info,vbemodeinfo *mode,foolfont *rawfont) { //the only functionallu important init lines! (rest is log) @@ -165,13 +164,9 @@ void PutString(char *str, int x,int y, int color, va_list va) } -void PutConsole(char *str, int color, va_list va) +void PutConsole(char *str, int color) { - char buff[256]; - tfp_vsprintf(buff,str,va); - str=buff; - while((*str)!=0) { PutFont(*str, console_x*10,console_y*12, color); diff --git a/lib/logger/log.c b/lib/logger/log.c index c9bb652..4a510f9 100644 --- a/lib/logger/log.c +++ b/lib/logger/log.c @@ -1,44 +1,76 @@ -#include "log.h" +#define FOOLOS_MODULE_NAME "log" + +#include "lib/logger/log.h" #include + +#include "log.h" #include "lib/int/stdint.h" +#include "kernel/time.h" + +#define BUF_SIZE 4069 -void PutConsole(char *str, int color, va_list va); +static char buffer[BUF_SIZE]; +static int first; +static int last; -volatile uint64_t task_system_clock; // in task.c +void PutConsole(char *str, int color); void log(char *module_name, int log_level, char *format_string, ...) { - if(log_level=BUF_SIZE)first=(first+1)%BUF_SIZE; + if(last>first)if(BUF_SIZE-last+first>=BUF_SIZE)first=(first+1)%BUF_SIZE; + } + + } void panic(char *module_name, char *format_string) { - - PutConsole("!! KERNEL PANIC !! ",0b1111100000000000,0); - PutConsole(module_name,0b1111100000000000,0); - PutConsole(" : ",0b0000011111100000,0); - PutConsole(format_string,0b1111100000000000,0); + PutConsole("!! KERNEL PANIC !! ",0b1111100000000000); + PutConsole(module_name,0b1111100000000000); + PutConsole(" : ",0b0000011111100000); + PutConsole(format_string,0b1111100000000000); while(1); // halt - } + +void log_init() +{ + first=last=0; +} + +void log_log() +{ + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"init buffer at: 0x%08X, first=%08X, last=%08X",buffer,&first,&last); +} + -- cgit v1.2.3