Mercurial > lcfOS
changeset 26:dcce92b1efbc
Added mm.c
author | windel |
---|---|
date | Tue, 27 Dec 2011 17:36:52 +0100 |
parents | d3c4bf3720a3 |
children | 7f74363f4c82 |
files | cos/Makefile cos/kernel/handlers.c cos/kernel/kernel.c cos/kernel/kernel.h cos/kernel/klib.c cos/kernel/malloc.c cos/kernel/mm.c |
diffstat | 7 files changed, 82 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/cos/Makefile Tue Dec 27 13:31:38 2011 +0100 +++ b/cos/Makefile Tue Dec 27 17:36:52 2011 +0100 @@ -27,6 +27,7 @@ kernel/klib.o \ kernel/malloc.o \ kernel/task.o \ + kernel/mm.o \ kernel/timer.o lcfosc.bin: $(CRT0) $(OBJECTS) linker.ld
--- a/cos/kernel/handlers.c Tue Dec 27 13:31:38 2011 +0100 +++ b/cos/kernel/handlers.c Tue Dec 27 17:36:52 2011 +0100 @@ -57,13 +57,11 @@ void setupIDT(void) { int i; - //panic("Just before filling IDT"); - // After this line a crash occurs! + // Fill all vectors with the default handler: for (i=0; i<256; i++) { setIDTentry(i, INTDEF, 0x08, 0x8E); } - //panic("Just after setting defhandlers in IDT"); // Now set other then default handler: setIDTentry(0, INT0, 0x08, 0x8E); @@ -96,11 +94,9 @@ idtP.base = (uint64_t)idt; idtP.limit = (sizeof(IDT_entry) * 256) - 1; // call load IDT asm function: - //panic("Just before LIDT"); loadIDT(); PICremap(); - //panic("Just before sti"); asm("sti"); }
--- a/cos/kernel/kernel.c Tue Dec 27 13:31:38 2011 +0100 +++ b/cos/kernel/kernel.c Tue Dec 27 17:36:52 2011 +0100 @@ -20,19 +20,13 @@ kfree(a); } -void reboot() -{ - while ( (inb(0x64) & 0x02) == 0x02) - { - ; - } - outb(0x64, 0xFE); -} +// A test program that prints 'Hoi' to the screen: +unsigned char hello_program[] = {0x55, 0x48, 0x89, 0xe5, 0x48, 0x83, 0xec, 0x10, 0x48, 0xc7, 0x45, 0xf8, 0x0, 0x80, 0xb, 0x0, 0x48, 0x8b, 0x45, 0xf8, 0xc6, 0x0, 0x48, 0x48, 0x8b, 0x45, 0xf8, 0x48, 0x83, 0xc0, 0x2, 0xc6, 0x0, 0x6f, 0x48, 0x8b, 0x45, 0xf8, 0x48, 0x83, 0xc0, 0x4, 0xc6, 0x0, 0x69, 0xeb, 0xfe, 0x0, 0x14, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x7a, 0x52, 0x0, 0x1, 0x78, 0x10, 0x1, 0x1b, 0xc, 0x7, 0x8, 0x90, 0x1, 0x0, 0x0, 0x1c, 0x0, 0x0, 0x0, 0x1c, 0x0, 0x0, 0x0, 0xb0, 0xff, 0xff, 0xff, 0x2f, 0x0, 0x0, 0x0, 0x0, 0x41, 0xe, 0x10, 0x86, 0x2, 0x43, 0xd, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; void kmain() { init_screen(); - setupIDT(); // This causes error on real hardware + setupIDT(); init_heap(); printf("Welcome!\n");
--- a/cos/kernel/kernel.h Tue Dec 27 13:31:38 2011 +0100 +++ b/cos/kernel/kernel.h Tue Dec 27 17:36:52 2011 +0100 @@ -66,6 +66,7 @@ // Panic exit: void halt(void); void panic(char *msg); +void reboot(void); // Bochs xchg bx,bx breakpoint: void magicBochsBreak();
--- a/cos/kernel/klib.c Tue Dec 27 13:31:38 2011 +0100 +++ b/cos/kernel/klib.c Tue Dec 27 17:36:52 2011 +0100 @@ -8,6 +8,15 @@ halt(); } +void reboot() +{ + while ( (inb(0x64) & 0x02) == 0x02) + { + ; + } + outb(0x64, 0xFE); +} + // IO port helpers: void outb(uint16_t port, uint8_t value) {
--- a/cos/kernel/malloc.c Tue Dec 27 13:31:38 2011 +0100 +++ b/cos/kernel/malloc.c Tue Dec 27 17:36:52 2011 +0100 @@ -22,9 +22,8 @@ Kernelpanic in case of failure.. */ -void* kmalloc(uint64_t size) { - // printf("Malloc %d bytes\n", size); - +void* kmalloc(uint64_t size) +{ // Start at the beginning of our heap and search a free block: heap_t *current = kernel_heap; while (current->magic == HEAP_MAGIC) @@ -63,7 +62,8 @@ return 0x0; } -void kfree(void* ptr) { +void kfree(void* ptr) +{ printf("Free address %x\n", ptr); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cos/kernel/mm.c Tue Dec 27 17:36:52 2011 +0100 @@ -0,0 +1,63 @@ + +/* Memory manager */ + +#include "kernel.h" + +// BITMAP functions: +#define NFRAMES 1000 +// Bit mask to check what frames are in use and which are free: +uint64_t frames[NFRAMES]; // NFRAMES*64*4kB frames. + +void set_frame(uint64_t frame) +{ + uint64_t idx = frame / 64; + uint64_t off = frame % 64; + frames[idx] |= (0x1 << off); +} + +void clear_frame(uint64_t frame) +{ + uint64_t idx = frame / 64; + uint64_t off = frame % 64; + frames[idx] &= ~(0x1 << off); +} + +uint64_t test_frame(uint64_t frame) +{ + uint64_t idx = frame / 64; + uint64_t off = frame % 64; + return (frames[idx] & (0x1 << off)); +} + +uint64_t first_frame() +{ + uint64_t i, j; + for (i = 0; i < NFRAMES / 64; i++) + { + if (frames[i] != 0xFFFFFFFFFFFFFFFF) + { + for (j = 0; j < 64; j++) + { + uint64_t dut = 0x1 << j; + if ((frames[i] & dut) != dut) + { + return i*64+j; + } + } + } + } + return 0xFFFFFFFFFFFFFFFF; +} + +// Memory manager functions: +void alloc_frame() +{ + uint64_t idx = first_frame(); + if (idx == (uint64_t) -1) + { + panic("No more memory!"); + } + set_frame(idx); +} + +