diff options
| author | Aleksa Vučković <aleksav013@gmail.com> | 2021-11-01 23:59:59 +0100 |
|---|---|---|
| committer | Aleksa Vučković <aleksav013@gmail.com> | 2021-11-01 23:59:59 +0100 |
| commit | 2a56405579be6dc341c503420e40cb30c5144a35 (patch) | |
| tree | 6548a001ead2a2df6068e2157a1b15adbe174aff /src | |
| parent | 89f7f1b114c1bbea3ad62808bf3653e1d2337d1f (diff) | |
Temporary heap fix
Diffstat (limited to 'src')
| -rw-r--r-- | src/c/heap.c | 36 | ||||
| -rw-r--r-- | src/c/irq.c | 32 | ||||
| -rw-r--r-- | src/c/kernel.c | 10 | ||||
| -rw-r--r-- | src/c/paging.c | 17 | ||||
| -rw-r--r-- | src/c/string.c | 2 | ||||
| -rw-r--r-- | src/include/heap.h | 25 |
6 files changed, 68 insertions, 54 deletions
diff --git a/src/c/heap.c b/src/c/heap.c index 1bad05e..2683269 100644 --- a/src/c/heap.c +++ b/src/c/heap.c @@ -1,9 +1,5 @@ #include"../include/types.h" -/* - 2014 Leonard Kevin McGuire Jr (www.kmcg3413.net) (kmcg3413@gmail.com) - 2016 Clément Gallet (provided bug fixes) -*/ typedef struct _KHEAPBLOCKBM { struct _KHEAPBLOCKBM *next; uint32_t size; @@ -61,7 +57,7 @@ static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) { } void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { - KHEAPBLOCKBM *b; + KHEAPBLOCKBM *b; uint8_t *bm; uint32_t bcnt; uint32_t x, y, z; @@ -70,14 +66,16 @@ void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { /* iterate blocks */ for (b = heap->fblock; b; b = b->next) { + //printf("size:%d,used:%d,bsize:%d,lfb:%d\n",b->size,b->used,b->bsize,b->lfb); /* check if block has enough room */ if (b->size - (b->used * b->bsize) >= size) { bcnt = b->size / b->bsize; bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; bm = (uint8_t*)&b[1]; + //printf("bcnt:%d,bneed:%d,bm:%d\n",bcnt,bneed,bm); - for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x < b->lfb; ++x) { + for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { /* just wrap around */ if (x >= bcnt) { x = 0; @@ -150,18 +148,22 @@ void k_heapBMFree(KHEAPBM *heap, void *ptr) { return; } -KHEAPBM kheap; +KHEAPBM kheap; -void heap() +void kheapinit() +{ + k_heapBMInit(&kheap); +} +int kheapaddblock(uintptr_t addr,uint32_t size,uint32_t bsize) { - KHEAPBM kheap; - char *ptr; + return k_heapBMAddBlock(&kheap,addr,size,bsize); +} +void *kmalloc(uint32_t size) +{ + return k_heapBMAlloc(&kheap,size); - k_heapBMInit(&kheap); /* initialize the heap */ - k_heapBMAddBlock(&kheap, 0x100000, 0x100000, 16); /* add block to heap - (starting 1MB mark and length of 1MB) - with default block size of 16 bytes - */ - ptr = (char*)k_heapBMAlloc(&kheap, 256); /* allocate 256 bytes (malloc) */ - k_heapBMFree(&kheap, ptr); /* free the pointer (free) */ +} +void kfree(void *ptr) +{ + k_heapBMFree(&kheap,ptr); } diff --git a/src/c/irq.c b/src/c/irq.c index 6b80a52..2a1c4d4 100644 --- a/src/c/irq.c +++ b/src/c/irq.c @@ -15,190 +15,222 @@ void irq0_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 0.\n"); + printf("Divide-by-zero Error\n"); } void irq1_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 1.\n"); + printf("Debug\n"); } void irq2_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 2.\n"); + printf("Non-maskable Interrupt\n"); } void irq3_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 3.\n"); + printf("Breakpoint\n"); } void irq4_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 4.\n"); + printf("Overflow\n"); } void irq5_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 5.\n"); + printf("Bound Range Exceeded\n"); } void irq6_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 6.\n"); + printf("Invalid Opcode\n"); } void irq7_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 7.\n"); + printf("Device Not Available\n"); } void irq8_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 8.\n"); + printf("Double Fault\n"); } void irq9_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 9.\n"); + printf("Coprocessor Segment Overrun\n"); } void irq10_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 10.\n"); + printf("Invalid TSS\n"); } void irq11_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 11.\n"); + printf("Segment Not Present\n"); } void irq12_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 12.\n"); + printf("Stack-Segment Fault\n"); } void irq13_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 13.\n"); + printf("General Protection Fault\n"); } void irq14_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 14.\n"); + printf("Page Fault\n"); } void irq15_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 15.\n"); + printf("Reserved\n"); } void irq16_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 16.\n"); + printf("x87 Floating-Point Exception\n"); } void irq17_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 17.\n"); + printf("Alignment Check\n"); } void irq18_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 18.\n"); + printf("Machine Check\n"); } void irq19_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 19.\n"); + printf("SIMD Floating-Point ExceptionM/#XF\n"); } void irq20_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 20.\n"); + printf("Virtualization Exception\n"); } void irq21_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 21.\n"); + printf("Reserved\n"); } void irq22_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 22.\n"); + printf("Reserved\n"); } void irq23_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 23.\n"); + printf("Reserved\n"); } void irq24_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 24.\n"); + printf("Reserved\n"); } void irq25_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 25.\n"); + printf("Reserved\n"); } void irq26_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 26.\n"); + printf("Reserved\n"); } void irq27_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 27.\n"); + printf("Reserved\n"); } void irq28_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 28.\n"); + printf("Reserved\n"); } void irq29_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 29.\n"); + printf("Reserved\n"); } void irq30_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 30.\n"); + printf("Security Exception\n"); } void irq31_handler() { ioport_out(PIC1_COMMAND_PORT, 0x20); printf("Interrupt 31.\n"); + printf("Reserved\n"); } diff --git a/src/c/kernel.c b/src/c/kernel.c index 48dbccb..a00c242 100644 --- a/src/c/kernel.c +++ b/src/c/kernel.c @@ -1,3 +1,4 @@ +#include"../include/stdio.h" #include"../include/heap.h" void terminal_initialize(void); @@ -9,13 +10,14 @@ void set_paging(); void kernel_main(void) { - terminal_initialize(); set_paging(); init_idt_table(); - init_keyboard(); init_timer(50); - //k_heapBMInit(&kheap); - //k_heapBMAddBlock(&kheap, 0x100000, 0x100000, 16); + init_keyboard(); + kheapinit(); + kheapaddblock(0x00200000, 0x00100000, 16); + + terminal_initialize(); prompt(); while(1) __asm__("hlt\n\t"); diff --git a/src/c/paging.c b/src/c/paging.c index b256b74..becc79d 100644 --- a/src/c/paging.c +++ b/src/c/paging.c @@ -18,34 +18,29 @@ void set_pd() } } -uint32_t first_page_table[1024] __attribute__((aligned(4096))); +uint32_t page_table[1024][1024] __attribute__((aligned(4096))); -void set_pt() +void set_pt(size_t num,uint32_t address) { // holds the physical address where we want to start mapping these pages to. // in this case, we want to map these pages to the very beginning of memory. - unsigned int i; //we will fill all 1024 entries in the table, mapping 4 megabytes - for(i = 0; i < 1024; i++) + for(size_t i=0;i<1024;i++) { // As the address is page aligned, it will always leave 12 bits zeroed. // Those bits are used by the attributes ;) - first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present. + page_table[num][i] = (address + i * 0x1000) | 3; // attributes: supervisor level, read/write, present. } -} -void put_pt() -{ + page_directory[num] = ((uint32_t)page_table[num]) | 3; // attributes: supervisor level, read/write, present - page_directory[0] = ((uint32_t)first_page_table) | 3; } void set_paging() { set_pd(); - set_pt(); - put_pt(); + for(size_t i=0;i<1024;i++) set_pt(i,0x00400000 * i); // all 4GB mapped loadPageDirectory(page_directory); enablePaging(); } diff --git a/src/c/string.c b/src/c/string.c index 5667715..ff2821a 100644 --- a/src/c/string.c +++ b/src/c/string.c @@ -94,7 +94,7 @@ double stof(const char *str) return num/pow; } -const int decimals=7; +const size_t decimals=7; void ftos(double num, char *str) { itos((uint32_t)num,str); diff --git a/src/include/heap.h b/src/include/heap.h index 4eebd04..11478aa 100644 --- a/src/include/heap.h +++ b/src/include/heap.h @@ -3,26 +3,9 @@ #include"types.h" -typedef struct _KHEAPBLOCKBM { - struct _KHEAPBLOCKBM *next; - uint32_t size; - uint32_t used; - uint32_t bsize; - uint32_t lfb; -} KHEAPBLOCKBM; - -typedef struct _KHEAPBM { - KHEAPBLOCKBM *fblock; -} KHEAPBM; - -void k_heapBMInit(KHEAPBM *heap); -int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize); -void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size); -void k_heapBMFree(KHEAPBM *heap, void *ptr); - -extern KHEAPBM kheap; - -#define kmalloc k_heapBMAlloc -#define kfree k_heapBMFree +void kheapinit(); +int kheapaddblock(uintptr_t addr,uint32_t size,uint32_t bsize); +void *kmalloc(uint32_t size); +void kfree(void *ptr); #endif |
