# HG changeset patch # User windel # Date 1326746335 -3600 # Node ID 5c20bd53cccd22ec02b239882e14d4cfc9252296 # Parent 91f91ff07ea832490e1135e6919637c4ca2ae1a7 Cleanup diff -r 91f91ff07ea8 -r 5c20bd53cccd cos/kernel/Makefile --- a/cos/kernel/Makefile Mon Jan 16 20:47:05 2012 +0100 +++ b/cos/kernel/Makefile Mon Jan 16 21:38:55 2012 +0100 @@ -8,7 +8,8 @@ -fno-builtin -mcmodel=large -Wall -Wextra -Werror OBJECTS = video.o snprintf.o kernel.o asmcode.o handlers.o keyboard.o \ - klib.o malloc.o task.o mm.o timer.o fs.o initrd.o + klib.o malloc.o task.o mm.o timer.o fs.o initrd.o multiboot.o \ + shell.o lcfos.bin: $(CRT0) $(OBJECTS) kernel.ld ld -g -T kernel.ld -s --cref -Map=kernel.map -o lcfos.bin $(CRT0) $(OBJECTS) diff -r 91f91ff07ea8 -r 5c20bd53cccd cos/kernel/kernel.c --- a/cos/kernel/kernel.c Mon Jan 16 20:47:05 2012 +0100 +++ b/cos/kernel/kernel.c Mon Jan 16 21:38:55 2012 +0100 @@ -1,21 +1,5 @@ #include "kernel.h" -static void testMalloc() -{ - char *a, *b; - - printf("Testing malloc\n"); - a = kmalloc(100); - printf("Got a at %x\n", a); - a[0] = 'A'; - b = kmalloc(22); - printf("Got b at %x\n", b); - b[0] = 'B'; - kfree(a); -} - -multiboot_info_t *multiboot_info = 0; // Set by startup code. - /* This routine initializes the kernel. * We are left here in 64-bit long mode with the first 6 MB identity mapped. * */ @@ -24,60 +8,18 @@ // No kmalloc required here yet: init_screen(); setupIDT(); - - /* Retrieve memory information from multiboot header */ - uint64_t available_memory = 0; - if ((multiboot_info->flags & (1 << 6)) == (1 << 6)) - { - multiboot_memory_map_t *mmap; - for (mmap = (multiboot_memory_map_t*)(uint64_t)multiboot_info->mmap_addr; - (uint64_t)mmap < multiboot_info->mmap_addr + multiboot_info->mmap_length; - mmap = (multiboot_memory_map_t*) ( (uint64_t)mmap + mmap->size + sizeof(mmap->size)) ) - { - /* - printf("size: %d, start: 0x%x, length=0x%x, type=%d\n", mmap->size, - mmap->base, mmap->length, mmap->type); - */ - - if ( (mmap->type == 1) && (mmap->base == 0x100000) ) - { - available_memory = mmap->length; - } - } - } - - if (available_memory == 0) - { - panic("Found no usable memory in grub's memory map\n"); - } - - printf("Running with %d MB ram\n", available_memory / 1000000); - - init_memory(available_memory); // Setup paging and memory manager - + read_multiboot_info(); // Parse the GRUB multiboot header. + init_memory(available_memory); // Setup a new paging scheme and memory manager + // From here kmalloc can be used. keyboard_init(); timer_init(); + printf("Ramdisk location: %p\n", ramdisk_location); /* fs_node_t *fs_root = 0; - if ( (multiboot_info->flags & (1<<3)) == (1<<3)) - { - printf("Mod count: %d\n", multiboot_info->mods_count); - uint64_t i; - multiboot_module_t *mod; - for (i=0, mod=(multiboot_module_t*)(uint64_t)multiboot_info->mods_addr; imods_count; i++, mod++) - { - printf("Mod start: %x, end: %x\n", mod->mod_start, mod->mod_end); - if (i == 0) - { - uint64_t ramdisk_location = ((uint32_t*)((uint64_t)multiboot_info->mods_addr))[0]; - fs_root = initialize_initrd(ramdisk_location); - } - // TODO: Make sure that placement malloc does not overwrite the ramdisk. - printf("Mod size: %d", mod->mod_end - mod->mod_start); - } - } + fs_root = initialize_initrd(ramdisk_location); + if (fs_root != 0) { fs_dirent_t *node = 0; @@ -89,49 +31,7 @@ } */ - - // TODO: make below a user space program! - printf("Welcome!\n"); - - while (1) - { - char buffer[70]; - printf(">"); - getline(buffer, 70); - // TODO: interpret this line with python :) - printf("\n"); - if (buffer[0] == 'x') - { - printf("System time in ms: %d\n", getTimeMS()); - } - if (buffer[0] == 't') - { - testMalloc(); - } - if ( strncmp(buffer, "help", 4)) - { - printf("Help\n Try one of these commands:\n"); - printf(" x: print system time in ms\n"); - printf(" r: reboot\n"); - printf(" t: test\n"); - printf(" b: break\n"); - } - if (strncmp(buffer, "r", 1)) - { - reboot(); - } - if (strncmp(buffer, "b", 1)) - { - magicBochsBreak(); - } - if (strncmp(buffer, "pf", 2)) - { - /* Test general protection exception */ - uint64_t *x; - x = (uint64_t*)0x4000000; // Address that is not mapped - *x = 0x2; // trigger paging exception - } - } + // TODO: make shell a user space program! + shell(); // Start user shell } - diff -r 91f91ff07ea8 -r 5c20bd53cccd cos/kernel/kernel.h --- a/cos/kernel/kernel.h Mon Jan 16 20:47:05 2012 +0100 +++ b/cos/kernel/kernel.h Mon Jan 16 21:38:55 2012 +0100 @@ -329,5 +329,13 @@ uint64_t getTimeMS(); void timer_init(void); +// Multiboot: +extern uint64_t available_memory; +extern uint64_t ramdisk_location; +void read_multiboot_info(void); + +// Shell +void shell(void); + #endif diff -r 91f91ff07ea8 -r 5c20bd53cccd cos/kernel/mm.c --- a/cos/kernel/mm.c Mon Jan 16 20:47:05 2012 +0100 +++ b/cos/kernel/mm.c Mon Jan 16 21:38:55 2012 +0100 @@ -6,6 +6,7 @@ #include "kernel.h" +// Bitmap that keeps track of all the 4 kB memory pages in the system: static uint64_t *frames = 0; static uint64_t nframes = 0; @@ -55,13 +56,17 @@ return (uint64_t) -1; } -// Memory manager functions: +/* + * Initializes the memory manager, allocating a bitmap once. + */ void init_memory(uint64_t total_mem_size) { + printf("Running with %d MB ram\n", total_mem_size / 1000000); + // Only here placement malloc is used! // - // Allocate and clear bits to remember which 4kb-frames are in use: - nframes = (total_mem_size / 0x1000); + // Allocate and clear bits to remember which 4KiB-frames are in use: + nframes = (total_mem_size / 0x1000); // Calculate number of frames frames = (uint64_t*)kmalloc_int( (nframes / 64) * sizeof(uint64_t) ); memset(frames, 0, (nframes / 64) * sizeof(uint64_t)); diff -r 91f91ff07ea8 -r 5c20bd53cccd cos/kernel/multiboot.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cos/kernel/multiboot.c Mon Jan 16 21:38:55 2012 +0100 @@ -0,0 +1,60 @@ +#include "kernel.h" + +multiboot_info_t *multiboot_info = 0; // Set by startup code. +uint64_t available_memory = 0; +uint64_t ramdisk_location = 0; + +/* Retrieve memory information from multiboot header */ +void read_multiboot_info() +{ + if ((multiboot_info->flags & (1 << 6)) == (1 << 6)) + { + multiboot_memory_map_t *mmap; + for (mmap = (multiboot_memory_map_t*)(uint64_t)multiboot_info->mmap_addr; + (uint64_t)mmap < multiboot_info->mmap_addr + multiboot_info->mmap_length; + mmap = (multiboot_memory_map_t*) ( (uint64_t)mmap + mmap->size + sizeof(mmap->size)) ) + { + /* + printf("size: %d, start: 0x%x, length=0x%x, type=%d\n", mmap->size, + mmap->base, mmap->length, mmap->type); + */ + + if ( (mmap->type == 1) && (mmap->base == 0x100000) ) + { + available_memory = mmap->length; + } + } + } + + if (available_memory == 0) + { + panic("Found no usable memory in grub's memory map\n"); + } + + if ((multiboot_info->flags & (1 << 3)) == (1 << 3)) + { + uint64_t i; + multiboot_module_t *mod = (multiboot_module_t*)(uint64_t)multiboot_info->mods_addr; + for (i=0; imods_count; i++) + { + // TODO: determine better way of finding the initrd module. + if (i == 0) + { + ramdisk_location = mod->mod_start; + } + + // Make sure that placement malloc does not overwrite the ramdisk. + uint64_t new_placement = (uint64_t)mod->mod_end; + if (new_placement > placement_address) + { + if ((new_placement & 0xFFF) != 0) + { + new_placement = (new_placement & (~0xFFF)) + 0x1000; // Align to 4 KiB page. + } + placement_address = new_placement; + } + mod++; // Go to next module. + } + } +} + diff -r 91f91ff07ea8 -r 5c20bd53cccd cos/kernel/shell.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cos/kernel/shell.c Mon Jan 16 21:38:55 2012 +0100 @@ -0,0 +1,66 @@ +#include "kernel.h" + +/* Interactive shell, should be eventually a user space + program */ + +static void testMalloc() +{ + char *a, *b; + + printf("Testing malloc\n"); + a = kmalloc(100); + printf("Got a at %x\n", a); + a[0] = 'A'; + b = kmalloc(22); + printf("Got b at %x\n", b); + b[0] = 'B'; + kfree(a); +} + +void shell() +{ + printf("Welcome!\n"); + while (1) + { + char buffer[70]; + printf(">"); + getline(buffer, 70); + // TODO: interpret this line with python :) + printf("\n"); + if (buffer[0] == 'x') + { + printf("System time in ms: %d\n", getTimeMS()); + } + if (buffer[0] == 't') + { + testMalloc(); + } + if ( strncmp(buffer, "help", 4)) + { + printf("Help\n Try one of these commands:\n"); + printf(" x: print system time in ms\n"); + printf(" r: reboot\n"); + printf(" t: test\n"); + printf(" b: break\n"); + } + + if (strncmp(buffer, "r", 1)) + { + reboot(); + } + + if (strncmp(buffer, "b", 1)) + { + magicBochsBreak(); + } + + if (strncmp(buffer, "pf", 2)) + { + /* Test general protection exception */ + uint64_t *x; + x = (uint64_t*)0x4000000; // Address that is not mapped + *x = 0x2; // trigger paging exception + } + } +} +