Mercurial > lcfOS
changeset 23:5dd47d6eebac
Added ubersimple malloc algorithm
author | windel |
---|---|
date | Thu, 01 Dec 2011 21:42:59 +0100 |
parents | 69bc6d477b38 |
children | d8627924d40d |
files | cos/bochsrc.txt cos/kernel/goto64.asm cos/kernel/handlers.c cos/kernel/kernel.c cos/kernel/video.c |
diffstat | 5 files changed, 91 insertions(+), 19 deletions(-) [+] |
line wrap: on
line diff
--- a/cos/bochsrc.txt Wed Nov 30 22:41:51 2011 +0100 +++ b/cos/bochsrc.txt Thu Dec 01 21:42:59 2011 +0100 @@ -1,4 +1,4 @@ -display_library: x, options="gui_debug" # use GTK debugger gui +#display_library: x, options="gui_debug" # use GTK debugger gui romimage: file=$BXSHARE/BIOS-bochs-latest cpu: count=1, ips=500000, reset_on_triple_fault=1, ignore_bad_msrs=1, msrs="msrs.def" cpuid: mmx=1, sep=1, sse=sse4_2, xapic=1, aes=1, movbe=1, xsave=1, cpuid_limit_winnt=0
--- a/cos/kernel/goto64.asm Wed Nov 30 22:41:51 2011 +0100 +++ b/cos/kernel/goto64.asm Thu Dec 01 21:42:59 2011 +0100 @@ -88,6 +88,9 @@ dw gdt64end - gdt64 - 1 ; Limit (size) dq gdt64 ; Base +hltmessage: +db "Long mode not supported", 0x0 + ; Start of loader code: loader: @@ -103,6 +106,19 @@ jmp cpu_has_long_mode no_long_mode: +; Print long mode not supported +mov edi, 0xb8000 +mov esi, hltmessage +xor eax,eax +loop1: +lodsb +mov dl, al +stosb +mov al, 0x1f +stosb +cmp dl, 0 +jne loop1 + hlt cpu_has_long_mode:
--- a/cos/kernel/handlers.c Wed Nov 30 22:41:51 2011 +0100 +++ b/cos/kernel/handlers.c Thu Dec 01 21:42:59 2011 +0100 @@ -101,10 +101,7 @@ loadIDT(); PICremap(); - magicBochsBreak(); - printf("enable ints\n"); asm("sti"); - printf("Done!\n"); } // PIC functions:
--- a/cos/kernel/kernel.c Wed Nov 30 22:41:51 2011 +0100 +++ b/cos/kernel/kernel.c Thu Dec 01 21:42:59 2011 +0100 @@ -126,34 +126,93 @@ buffer[i] = 0; } +#define HEAP_MAGIC 0xc0ffee +#define HEAP_START 0x400000 +#define HEAP_SIZE 0x200000 +#define HEAP_INUSE 1 +#define HEAP_FREE 0 + +typedef struct { + uint64_t magic; + uint64_t state; + uint64_t size; +} heap_t; + /* malloc and free divide the chunks of memory present at the heap of the kernel into smaller parts. The heap is located at: 0x */ -static void* kernel_heap = (void*) 0x400000; // 4 MB - 6 MB is heap +static heap_t* kernel_heap = (heap_t*) 0x400000; // 4 MB - 6 MB is heap /* Allocates 'size' bytes and returns the pointer if succesfull. Kernelpanic in case of failure.. */ + void* kmalloc(uint64_t size) { - printf("Malloc %d bytes\n", size); - return kernel_heap; + // printf("Malloc %d bytes\n", size); + + // Start at the beginning of our heap and search a free block: + heap_t *current = kernel_heap; + while (current->magic == HEAP_MAGIC) + { + if ((current->state == HEAP_FREE) && (current->size >= size)) + { + // Mark block as used: + current->state = HEAP_INUSE; + + // Insert a heap header if required: + if (current->size > size + sizeof(heap_t) + 1) + { + // Calculate location of the inserted header: + heap_t *newheader = (heap_t*) (((char*)current)+size+sizeof(heap_t)); + + // Set the new header fields: + newheader->size = current->size - size - sizeof(heap_t); + newheader->state = HEAP_FREE; + newheader->magic = HEAP_MAGIC; + + // Set the size of this block + current->size = size; + } + else + { + // We allocate this whole block + } + // Calculate the size of the block: + char *address = ((char*)current)+sizeof(heap_t); + return address; + + } + // Goto next heap block: + current = (heap_t*)(((char*) current) + current->size + sizeof(heap_t)); + } + return 0x0; } void kfree(void* ptr) { printf("Free address %x\n", ptr); } +void init_heap(void) +{ + // Initialize the kernel heap: + kernel_heap->magic = HEAP_MAGIC; + kernel_heap->state = HEAP_FREE; + kernel_heap->size = HEAP_SIZE - sizeof(heap_t); +} + void startPython() { // TODO: connect to Py_Main - PyRun_SimpleString("print('hello world')"); + //PyRun_SimpleString("print('hello world')"); } void testMalloc() { char *a, *b; + + printf("Testing malloc\n"); a = kmalloc(100); printf("Got a at %x\n", a); a[0] = 'A'; @@ -161,18 +220,15 @@ printf("Got b at %x\n", b); b[0] = 'B'; kfree(a); - } void kmain() { init_screen(); setupIDT(); + init_heap(); - printf("Welcome! .. "); - printf("Testing malloc"); - testMalloc(); - printf("Entering mainloop!\n"); + printf("Welcome!\n"); while (1==1) { @@ -186,6 +242,10 @@ { 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");
--- a/cos/kernel/video.c Wed Nov 30 22:41:51 2011 +0100 +++ b/cos/kernel/video.c Thu Dec 01 21:42:59 2011 +0100 @@ -6,9 +6,8 @@ #define NUM_ROWS 25 #define SCREEN_SIZE (NUM_COLS * NUM_ROWS) -#define SCREEN_ATTR 0x0F - static int row, col; +uint8_t video_color = 0x1F; void move_cursor() { @@ -27,7 +26,7 @@ for (loop = 0; loop < SCREEN_SIZE; loop++) { *vidmem++ = 0x20; - *vidmem++ = SCREEN_ATTR; + *vidmem++ = video_color; } } @@ -53,7 +52,7 @@ } for (v = (unsigned short*) VIDMEM + n, i = 0; i < NUM_COLS; i++) { - *v++ = SCREEN_ATTR & (0x20 << 8); + *v++ = (video_color << 8) & 0x20; } } @@ -76,7 +75,7 @@ for (loop = col; loop < NUM_COLS; loop++) { *v++ = ' '; - *v++ = SCREEN_ATTR; + *v++ = video_color; } } @@ -92,7 +91,7 @@ break; default: *v++ = (unsigned char) c; - *v = SCREEN_ATTR; + *v = video_color; if (col < NUM_COLS - 1) ++col;