diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/src/kernel/drivers/pci/hardDrive/ahciController.c b/src/kernel/drivers/pci/hardDrive/ahciController.c index ea1c706..9f0058d 100644 --- a/src/kernel/drivers/pci/hardDrive/ahciController.c +++ b/src/kernel/drivers/pci/hardDrive/ahciController.c @@ -84,18 +84,18 @@ HBAPort *HBAPortPtr = port->HBAPortPtr; StopCMD(HBAPortPtr); - HBACommandHeader *CommandHeader = mallocAligned(1024, 10); + HBACommandHeader *CommandHeader = malloc(1024); HBAPortPtr->CommandListBase = LOWER(CommandHeader); HBAPortPtr->CommandListBaseUpper = UPPER(CommandHeader); memset(CommandHeader, 0, 1024); - void *fis = mallocAligned(1024, 10); + void *fis = malloc(1024); HBAPortPtr->FISBaseAddress = LOWER(fis); HBAPortPtr->FISBaseAddressUpper = UPPER(fis); memset(fis, 0, 256); for (int i = 0; i < 32; i++) { CommandHeader[i].PRDTLength = 8; - void *CommandTable = mallocAligned(sizeof(HBACommandTable), 8); + void *CommandTable = malloc(sizeof(HBACommandTable)); CommandHeader[i].CommandTableBaseAddress = LOWER(CommandTable); CommandHeader[i].CommandTableBaseAddressUpper = UPPER(CommandTable); memset(CommandTable, 0, 256); diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/src/kernel/drivers/pci/hardDrive/ahciController.c b/src/kernel/drivers/pci/hardDrive/ahciController.c index ea1c706..9f0058d 100644 --- a/src/kernel/drivers/pci/hardDrive/ahciController.c +++ b/src/kernel/drivers/pci/hardDrive/ahciController.c @@ -84,18 +84,18 @@ HBAPort *HBAPortPtr = port->HBAPortPtr; StopCMD(HBAPortPtr); - HBACommandHeader *CommandHeader = mallocAligned(1024, 10); + HBACommandHeader *CommandHeader = malloc(1024); HBAPortPtr->CommandListBase = LOWER(CommandHeader); HBAPortPtr->CommandListBaseUpper = UPPER(CommandHeader); memset(CommandHeader, 0, 1024); - void *fis = mallocAligned(1024, 10); + void *fis = malloc(1024); HBAPortPtr->FISBaseAddress = LOWER(fis); HBAPortPtr->FISBaseAddressUpper = UPPER(fis); memset(fis, 0, 256); for (int i = 0; i < 32; i++) { CommandHeader[i].PRDTLength = 8; - void *CommandTable = mallocAligned(sizeof(HBACommandTable), 8); + void *CommandTable = malloc(sizeof(HBACommandTable)); CommandHeader[i].CommandTableBaseAddress = LOWER(CommandTable); CommandHeader[i].CommandTableBaseAddressUpper = UPPER(CommandTable); memset(CommandTable, 0, 256); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 541259e..b2fb943 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -18,7 +18,6 @@ currentTask->timerTicks = 1000; currentTask->ticksLeft = 1000; drawLogo(); - yields(); printCPUData(); setupDevices(); while (1) { diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/src/kernel/drivers/pci/hardDrive/ahciController.c b/src/kernel/drivers/pci/hardDrive/ahciController.c index ea1c706..9f0058d 100644 --- a/src/kernel/drivers/pci/hardDrive/ahciController.c +++ b/src/kernel/drivers/pci/hardDrive/ahciController.c @@ -84,18 +84,18 @@ HBAPort *HBAPortPtr = port->HBAPortPtr; StopCMD(HBAPortPtr); - HBACommandHeader *CommandHeader = mallocAligned(1024, 10); + HBACommandHeader *CommandHeader = malloc(1024); HBAPortPtr->CommandListBase = LOWER(CommandHeader); HBAPortPtr->CommandListBaseUpper = UPPER(CommandHeader); memset(CommandHeader, 0, 1024); - void *fis = mallocAligned(1024, 10); + void *fis = malloc(1024); HBAPortPtr->FISBaseAddress = LOWER(fis); HBAPortPtr->FISBaseAddressUpper = UPPER(fis); memset(fis, 0, 256); for (int i = 0; i < 32; i++) { CommandHeader[i].PRDTLength = 8; - void *CommandTable = mallocAligned(sizeof(HBACommandTable), 8); + void *CommandTable = malloc(sizeof(HBACommandTable)); CommandHeader[i].CommandTableBaseAddress = LOWER(CommandTable); CommandHeader[i].CommandTableBaseAddressUpper = UPPER(CommandTable); memset(CommandTable, 0, 256); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 541259e..b2fb943 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -18,7 +18,6 @@ currentTask->timerTicks = 1000; currentTask->ticksLeft = 1000; drawLogo(); - yields(); printCPUData(); setupDevices(); while (1) { diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c index 301f1c2..b29cb96 100644 --- a/src/kernel/memory/alloc.c +++ b/src/kernel/memory/alloc.c @@ -4,24 +4,83 @@ #include #include -void *mallocTaskAligned(uintptr_t size, uint8_t alignment, Task *task) { - // todo: do this nicer : have different 'buckets' for different allocation - // sizes - return getPage(); +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +AllocationData allocationData; + +void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { + block->allocatedFine[coarse] |= 1 << fine; + block->allocatedCoarse |= 1 << coarse; + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & 1 << i) { + continue; + } + block->allocatedCoarse &= ~(1 << coarse); + break; + } + return ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); } -void *mallocTask(uintptr_t size, Task *task) { - return mallocTaskAligned(size, 0, task); +void *mallocTask(uintptr_t size, void *task) { + uint32_t sizeBit = LOG2(size) + 1; + if (sizeBit > 10) { + return getPage(); + } + AllocationBlock *block = allocationData[sizeBit], *last = 0; + while (1) { + if (!block) { + block = getPage(); + memset(block, 0, 4096); + block->blockSize = 1 << sizeBit; + if (last) { + block->previous = last; + last->next = block; + } else { + allocationData[sizeBit] = block; + block->previous = (void *)(uintptr_t)sizeBit; + } + block->magic = ALLOCATION_MAGIC; + } + if (block->allocatedCoarse == ~0) { + goto end; + } + for (uint8_t coarse = 0; coarse < 32; coarse++) { + for (uint8_t fine = 0; fine < 32; fine++) { + if (block->allocatedFine[coarse] & (1 << fine)) { + continue; + } + return reserveBlock(block, coarse, fine); + } + } + end: + last = block; + block = block->next; + } + // unreachable } -void *malloc(uintptr_t size) { return mallocTaskAligned(size, 0, currentTask); } +void *malloc(uintptr_t size) { return mallocTask(size, currentTask); } -void *mallocAligned(uintptr_t size, uint8_t alignment) { - return mallocTaskAligned(size, alignment, currentTask); +void free(void *location) { + AllocationBlock *block = (void *)((uintptr_t)location & ~0xFFF); + if (block->magic != ALLOCATION_MAGIC) { + freePage(location); + return; + } + uint16_t index = (uint16_t)((uintptr_t)location & 0xFFF) / block->blockSize; + uint8_t coarse = index / 32; + uint8_t fine = index % 32; + block->allocatedFine[coarse] &= ~(1 << fine); + block->allocatedCoarse &= ~(1 << coarse); + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & (1 << i)) { + continue; + } + block->allocatedCoarse |= 1 << coarse; + return; + } } -void free(void *location) { freePage(location); } - -void freeTaskAllocations(Task *task) { +void freeTaskAllocations(void *task) { // todo } diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/src/kernel/drivers/pci/hardDrive/ahciController.c b/src/kernel/drivers/pci/hardDrive/ahciController.c index ea1c706..9f0058d 100644 --- a/src/kernel/drivers/pci/hardDrive/ahciController.c +++ b/src/kernel/drivers/pci/hardDrive/ahciController.c @@ -84,18 +84,18 @@ HBAPort *HBAPortPtr = port->HBAPortPtr; StopCMD(HBAPortPtr); - HBACommandHeader *CommandHeader = mallocAligned(1024, 10); + HBACommandHeader *CommandHeader = malloc(1024); HBAPortPtr->CommandListBase = LOWER(CommandHeader); HBAPortPtr->CommandListBaseUpper = UPPER(CommandHeader); memset(CommandHeader, 0, 1024); - void *fis = mallocAligned(1024, 10); + void *fis = malloc(1024); HBAPortPtr->FISBaseAddress = LOWER(fis); HBAPortPtr->FISBaseAddressUpper = UPPER(fis); memset(fis, 0, 256); for (int i = 0; i < 32; i++) { CommandHeader[i].PRDTLength = 8; - void *CommandTable = mallocAligned(sizeof(HBACommandTable), 8); + void *CommandTable = malloc(sizeof(HBACommandTable)); CommandHeader[i].CommandTableBaseAddress = LOWER(CommandTable); CommandHeader[i].CommandTableBaseAddressUpper = UPPER(CommandTable); memset(CommandTable, 0, 256); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 541259e..b2fb943 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -18,7 +18,6 @@ currentTask->timerTicks = 1000; currentTask->ticksLeft = 1000; drawLogo(); - yields(); printCPUData(); setupDevices(); while (1) { diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c index 301f1c2..b29cb96 100644 --- a/src/kernel/memory/alloc.c +++ b/src/kernel/memory/alloc.c @@ -4,24 +4,83 @@ #include #include -void *mallocTaskAligned(uintptr_t size, uint8_t alignment, Task *task) { - // todo: do this nicer : have different 'buckets' for different allocation - // sizes - return getPage(); +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +AllocationData allocationData; + +void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { + block->allocatedFine[coarse] |= 1 << fine; + block->allocatedCoarse |= 1 << coarse; + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & 1 << i) { + continue; + } + block->allocatedCoarse &= ~(1 << coarse); + break; + } + return ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); } -void *mallocTask(uintptr_t size, Task *task) { - return mallocTaskAligned(size, 0, task); +void *mallocTask(uintptr_t size, void *task) { + uint32_t sizeBit = LOG2(size) + 1; + if (sizeBit > 10) { + return getPage(); + } + AllocationBlock *block = allocationData[sizeBit], *last = 0; + while (1) { + if (!block) { + block = getPage(); + memset(block, 0, 4096); + block->blockSize = 1 << sizeBit; + if (last) { + block->previous = last; + last->next = block; + } else { + allocationData[sizeBit] = block; + block->previous = (void *)(uintptr_t)sizeBit; + } + block->magic = ALLOCATION_MAGIC; + } + if (block->allocatedCoarse == ~0) { + goto end; + } + for (uint8_t coarse = 0; coarse < 32; coarse++) { + for (uint8_t fine = 0; fine < 32; fine++) { + if (block->allocatedFine[coarse] & (1 << fine)) { + continue; + } + return reserveBlock(block, coarse, fine); + } + } + end: + last = block; + block = block->next; + } + // unreachable } -void *malloc(uintptr_t size) { return mallocTaskAligned(size, 0, currentTask); } +void *malloc(uintptr_t size) { return mallocTask(size, currentTask); } -void *mallocAligned(uintptr_t size, uint8_t alignment) { - return mallocTaskAligned(size, alignment, currentTask); +void free(void *location) { + AllocationBlock *block = (void *)((uintptr_t)location & ~0xFFF); + if (block->magic != ALLOCATION_MAGIC) { + freePage(location); + return; + } + uint16_t index = (uint16_t)((uintptr_t)location & 0xFFF) / block->blockSize; + uint8_t coarse = index / 32; + uint8_t fine = index % 32; + block->allocatedFine[coarse] &= ~(1 << fine); + block->allocatedCoarse &= ~(1 << coarse); + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & (1 << i)) { + continue; + } + block->allocatedCoarse |= 1 << coarse; + return; + } } -void free(void *location) { freePage(location); } - -void freeTaskAllocations(Task *task) { +void freeTaskAllocations(void *task) { // todo } diff --git a/src/kernel/memory/fileSystem.c b/src/kernel/memory/fileSystem.c index 10b742b..9d4b9f7 100644 --- a/src/kernel/memory/fileSystem.c +++ b/src/kernel/memory/fileSystem.c @@ -229,7 +229,7 @@ yields(); return; } - void *bootSector = mallocAligned(512, 1); + void *bootSector = malloc(512); drive->access(drive, 0, bootSector, 1, 0); FatBootSector *boot = bootSector; info = malloc(sizeof(FatInfo)); diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/src/kernel/drivers/pci/hardDrive/ahciController.c b/src/kernel/drivers/pci/hardDrive/ahciController.c index ea1c706..9f0058d 100644 --- a/src/kernel/drivers/pci/hardDrive/ahciController.c +++ b/src/kernel/drivers/pci/hardDrive/ahciController.c @@ -84,18 +84,18 @@ HBAPort *HBAPortPtr = port->HBAPortPtr; StopCMD(HBAPortPtr); - HBACommandHeader *CommandHeader = mallocAligned(1024, 10); + HBACommandHeader *CommandHeader = malloc(1024); HBAPortPtr->CommandListBase = LOWER(CommandHeader); HBAPortPtr->CommandListBaseUpper = UPPER(CommandHeader); memset(CommandHeader, 0, 1024); - void *fis = mallocAligned(1024, 10); + void *fis = malloc(1024); HBAPortPtr->FISBaseAddress = LOWER(fis); HBAPortPtr->FISBaseAddressUpper = UPPER(fis); memset(fis, 0, 256); for (int i = 0; i < 32; i++) { CommandHeader[i].PRDTLength = 8; - void *CommandTable = mallocAligned(sizeof(HBACommandTable), 8); + void *CommandTable = malloc(sizeof(HBACommandTable)); CommandHeader[i].CommandTableBaseAddress = LOWER(CommandTable); CommandHeader[i].CommandTableBaseAddressUpper = UPPER(CommandTable); memset(CommandTable, 0, 256); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 541259e..b2fb943 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -18,7 +18,6 @@ currentTask->timerTicks = 1000; currentTask->ticksLeft = 1000; drawLogo(); - yields(); printCPUData(); setupDevices(); while (1) { diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c index 301f1c2..b29cb96 100644 --- a/src/kernel/memory/alloc.c +++ b/src/kernel/memory/alloc.c @@ -4,24 +4,83 @@ #include #include -void *mallocTaskAligned(uintptr_t size, uint8_t alignment, Task *task) { - // todo: do this nicer : have different 'buckets' for different allocation - // sizes - return getPage(); +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +AllocationData allocationData; + +void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { + block->allocatedFine[coarse] |= 1 << fine; + block->allocatedCoarse |= 1 << coarse; + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & 1 << i) { + continue; + } + block->allocatedCoarse &= ~(1 << coarse); + break; + } + return ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); } -void *mallocTask(uintptr_t size, Task *task) { - return mallocTaskAligned(size, 0, task); +void *mallocTask(uintptr_t size, void *task) { + uint32_t sizeBit = LOG2(size) + 1; + if (sizeBit > 10) { + return getPage(); + } + AllocationBlock *block = allocationData[sizeBit], *last = 0; + while (1) { + if (!block) { + block = getPage(); + memset(block, 0, 4096); + block->blockSize = 1 << sizeBit; + if (last) { + block->previous = last; + last->next = block; + } else { + allocationData[sizeBit] = block; + block->previous = (void *)(uintptr_t)sizeBit; + } + block->magic = ALLOCATION_MAGIC; + } + if (block->allocatedCoarse == ~0) { + goto end; + } + for (uint8_t coarse = 0; coarse < 32; coarse++) { + for (uint8_t fine = 0; fine < 32; fine++) { + if (block->allocatedFine[coarse] & (1 << fine)) { + continue; + } + return reserveBlock(block, coarse, fine); + } + } + end: + last = block; + block = block->next; + } + // unreachable } -void *malloc(uintptr_t size) { return mallocTaskAligned(size, 0, currentTask); } +void *malloc(uintptr_t size) { return mallocTask(size, currentTask); } -void *mallocAligned(uintptr_t size, uint8_t alignment) { - return mallocTaskAligned(size, alignment, currentTask); +void free(void *location) { + AllocationBlock *block = (void *)((uintptr_t)location & ~0xFFF); + if (block->magic != ALLOCATION_MAGIC) { + freePage(location); + return; + } + uint16_t index = (uint16_t)((uintptr_t)location & 0xFFF) / block->blockSize; + uint8_t coarse = index / 32; + uint8_t fine = index % 32; + block->allocatedFine[coarse] &= ~(1 << fine); + block->allocatedCoarse &= ~(1 << coarse); + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & (1 << i)) { + continue; + } + block->allocatedCoarse |= 1 << coarse; + return; + } } -void free(void *location) { freePage(location); } - -void freeTaskAllocations(Task *task) { +void freeTaskAllocations(void *task) { // todo } diff --git a/src/kernel/memory/fileSystem.c b/src/kernel/memory/fileSystem.c index 10b742b..9d4b9f7 100644 --- a/src/kernel/memory/fileSystem.c +++ b/src/kernel/memory/fileSystem.c @@ -229,7 +229,7 @@ yields(); return; } - void *bootSector = mallocAligned(512, 1); + void *bootSector = malloc(512); drive->access(drive, 0, bootSector, 1, 0); FatBootSector *boot = bootSector; info = malloc(sizeof(FatInfo)); diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index f6e90eb..8ad9b14 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -1,11 +1,12 @@ #include #include +#include #include #include extern uintptr_t kernelEnd; -PageTablePointer *kernelPageTables; +PageTablePointer *kernelPageTable; PagePointer *kernelPages; uint32_t *allocatedPages; // uint32_t *freePageChunks; // todo: hierarchical structure to find a free page @@ -23,7 +24,7 @@ } void enablePaging() { - asm volatile("mov %%eax, %%cr3" : : "a"(kernelPageTables)); + asm volatile("mov %%eax, %%cr3" : : "a"(kernelPageTable)); asm volatile("mov %cr0, %eax"); asm volatile("orl $0x80000000, %eax"); asm volatile("mov %eax, %cr0"); @@ -31,7 +32,7 @@ void initializePaging() { void *currentPosition = (void *)((kernelEnd - 1) & ~0xFFF) + 0x1000; - kernelPageTables = currentPosition; + kernelPageTable = currentPosition; currentPosition += 0x4000; kernelPages = currentPosition; currentPosition += sizeof(uint32_t) * 1024 * 1024; @@ -39,9 +40,9 @@ currentPosition += sizeof(uint32_t) * 1024 * 1024 / 32; // 1024 tables with 1024 pages each for (int i = 0; i < 1024; i++) { - kernelPageTables[i].present = 1; - kernelPageTables[i].writable = 1; - kernelPageTables[i].pagePointerAddress = + kernelPageTable[i].present = 1; + kernelPageTable[i].writable = 1; + kernelPageTable[i].pagePointerAddress = (uint32_t)(uintptr_t)(kernelPages + 1024 * i) >> 12; } for (uint32_t i = 0; i < 0x100000; i++) { diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/src/kernel/drivers/pci/hardDrive/ahciController.c b/src/kernel/drivers/pci/hardDrive/ahciController.c index ea1c706..9f0058d 100644 --- a/src/kernel/drivers/pci/hardDrive/ahciController.c +++ b/src/kernel/drivers/pci/hardDrive/ahciController.c @@ -84,18 +84,18 @@ HBAPort *HBAPortPtr = port->HBAPortPtr; StopCMD(HBAPortPtr); - HBACommandHeader *CommandHeader = mallocAligned(1024, 10); + HBACommandHeader *CommandHeader = malloc(1024); HBAPortPtr->CommandListBase = LOWER(CommandHeader); HBAPortPtr->CommandListBaseUpper = UPPER(CommandHeader); memset(CommandHeader, 0, 1024); - void *fis = mallocAligned(1024, 10); + void *fis = malloc(1024); HBAPortPtr->FISBaseAddress = LOWER(fis); HBAPortPtr->FISBaseAddressUpper = UPPER(fis); memset(fis, 0, 256); for (int i = 0; i < 32; i++) { CommandHeader[i].PRDTLength = 8; - void *CommandTable = mallocAligned(sizeof(HBACommandTable), 8); + void *CommandTable = malloc(sizeof(HBACommandTable)); CommandHeader[i].CommandTableBaseAddress = LOWER(CommandTable); CommandHeader[i].CommandTableBaseAddressUpper = UPPER(CommandTable); memset(CommandTable, 0, 256); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 541259e..b2fb943 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -18,7 +18,6 @@ currentTask->timerTicks = 1000; currentTask->ticksLeft = 1000; drawLogo(); - yields(); printCPUData(); setupDevices(); while (1) { diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c index 301f1c2..b29cb96 100644 --- a/src/kernel/memory/alloc.c +++ b/src/kernel/memory/alloc.c @@ -4,24 +4,83 @@ #include #include -void *mallocTaskAligned(uintptr_t size, uint8_t alignment, Task *task) { - // todo: do this nicer : have different 'buckets' for different allocation - // sizes - return getPage(); +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +AllocationData allocationData; + +void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { + block->allocatedFine[coarse] |= 1 << fine; + block->allocatedCoarse |= 1 << coarse; + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & 1 << i) { + continue; + } + block->allocatedCoarse &= ~(1 << coarse); + break; + } + return ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); } -void *mallocTask(uintptr_t size, Task *task) { - return mallocTaskAligned(size, 0, task); +void *mallocTask(uintptr_t size, void *task) { + uint32_t sizeBit = LOG2(size) + 1; + if (sizeBit > 10) { + return getPage(); + } + AllocationBlock *block = allocationData[sizeBit], *last = 0; + while (1) { + if (!block) { + block = getPage(); + memset(block, 0, 4096); + block->blockSize = 1 << sizeBit; + if (last) { + block->previous = last; + last->next = block; + } else { + allocationData[sizeBit] = block; + block->previous = (void *)(uintptr_t)sizeBit; + } + block->magic = ALLOCATION_MAGIC; + } + if (block->allocatedCoarse == ~0) { + goto end; + } + for (uint8_t coarse = 0; coarse < 32; coarse++) { + for (uint8_t fine = 0; fine < 32; fine++) { + if (block->allocatedFine[coarse] & (1 << fine)) { + continue; + } + return reserveBlock(block, coarse, fine); + } + } + end: + last = block; + block = block->next; + } + // unreachable } -void *malloc(uintptr_t size) { return mallocTaskAligned(size, 0, currentTask); } +void *malloc(uintptr_t size) { return mallocTask(size, currentTask); } -void *mallocAligned(uintptr_t size, uint8_t alignment) { - return mallocTaskAligned(size, alignment, currentTask); +void free(void *location) { + AllocationBlock *block = (void *)((uintptr_t)location & ~0xFFF); + if (block->magic != ALLOCATION_MAGIC) { + freePage(location); + return; + } + uint16_t index = (uint16_t)((uintptr_t)location & 0xFFF) / block->blockSize; + uint8_t coarse = index / 32; + uint8_t fine = index % 32; + block->allocatedFine[coarse] &= ~(1 << fine); + block->allocatedCoarse &= ~(1 << coarse); + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & (1 << i)) { + continue; + } + block->allocatedCoarse |= 1 << coarse; + return; + } } -void free(void *location) { freePage(location); } - -void freeTaskAllocations(Task *task) { +void freeTaskAllocations(void *task) { // todo } diff --git a/src/kernel/memory/fileSystem.c b/src/kernel/memory/fileSystem.c index 10b742b..9d4b9f7 100644 --- a/src/kernel/memory/fileSystem.c +++ b/src/kernel/memory/fileSystem.c @@ -229,7 +229,7 @@ yields(); return; } - void *bootSector = mallocAligned(512, 1); + void *bootSector = malloc(512); drive->access(drive, 0, bootSector, 1, 0); FatBootSector *boot = bootSector; info = malloc(sizeof(FatInfo)); diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index f6e90eb..8ad9b14 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -1,11 +1,12 @@ #include #include +#include #include #include extern uintptr_t kernelEnd; -PageTablePointer *kernelPageTables; +PageTablePointer *kernelPageTable; PagePointer *kernelPages; uint32_t *allocatedPages; // uint32_t *freePageChunks; // todo: hierarchical structure to find a free page @@ -23,7 +24,7 @@ } void enablePaging() { - asm volatile("mov %%eax, %%cr3" : : "a"(kernelPageTables)); + asm volatile("mov %%eax, %%cr3" : : "a"(kernelPageTable)); asm volatile("mov %cr0, %eax"); asm volatile("orl $0x80000000, %eax"); asm volatile("mov %eax, %cr0"); @@ -31,7 +32,7 @@ void initializePaging() { void *currentPosition = (void *)((kernelEnd - 1) & ~0xFFF) + 0x1000; - kernelPageTables = currentPosition; + kernelPageTable = currentPosition; currentPosition += 0x4000; kernelPages = currentPosition; currentPosition += sizeof(uint32_t) * 1024 * 1024; @@ -39,9 +40,9 @@ currentPosition += sizeof(uint32_t) * 1024 * 1024 / 32; // 1024 tables with 1024 pages each for (int i = 0; i < 1024; i++) { - kernelPageTables[i].present = 1; - kernelPageTables[i].writable = 1; - kernelPageTables[i].pagePointerAddress = + kernelPageTable[i].present = 1; + kernelPageTable[i].writable = 1; + kernelPageTable[i].pagePointerAddress = (uint32_t)(uintptr_t)(kernelPages + 1024 * i) >> 12; } for (uint32_t i = 0; i < 0x100000; i++) { diff --git a/src/kernel/task/osTasks.c b/src/kernel/task/osTasks.c index 5e06d2b..f781f2e 100644 --- a/src/kernel/task/osTasks.c +++ b/src/kernel/task/osTasks.c @@ -24,11 +24,11 @@ } } -extern uint32_t *kernelPageTables; +extern uint32_t *kernelPageTable; void initOSTasks() { setRunningTask(&mainTask); - createTask(&printerTask, (uint32_t)printLoop, 0x0, kernelPageTables); + createTask(&printerTask, (uint32_t)printLoop, 0x0, kernelPageTable); } inline Task *getPrinterTask() { return &printerTask; } diff --git a/.gitignore b/.gitignore index dba3c4d..3a42211 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ iso/boot/tree-os.bin .vscode disk.img +*.log diff --git a/Makefile b/Makefile index e9c02be..de1f638 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ EMU = qemu-system-x86_64 EMUFLAGS = -m 256M -drive format=raw,file=tree-os.iso -drive format=raw,file=disk.img,id=disk,if=none -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 - BUILD_FOLDER = build SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) diff --git a/src/include/alloc.h b/src/include/alloc.h index 91b1fc0..bff2b3b 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -2,12 +2,25 @@ #define ALLOC_H #include -#include + +#define ALLOCATION_MAGIC 0x44BB33DD + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; + +typedef AllocationBlock *AllocationData[12]; extern void *malloc(uintptr_t size); extern void *mallocAligned(uintptr_t size, uint8_t alignment); -extern void *mallocTask(uintptr_t size, Task *task); +extern void *mallocTask(uintptr_t size, void *task); extern void free(void *location); -extern void freeTaskAllocations(Task *task); +extern void freeTaskAllocations(void *task); #endif diff --git a/src/include/task.h b/src/include/task.h index 5445516..fade243 100644 --- a/src/include/task.h +++ b/src/include/task.h @@ -1,6 +1,7 @@ #ifndef TASK_H #define TASK_H +#include #include #include #include @@ -18,6 +19,7 @@ ListElement *messages; uint32_t ticksLeft; uint32_t timerTicks; + AllocationData *allocationData; } Task; extern Task *currentTask; diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index c24dda6..c84c41e 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -71,7 +71,10 @@ } void handleException(regs *registers) { + writeChar(EXCEPTION_MESSAGES[registers->int_no][0]); + writeChar(EXCEPTION_MESSAGES[registers->int_no][1]); printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); - yield(); + while (1) + ; } diff --git a/src/kernel/drivers/interrupts/interrupts.c b/src/kernel/drivers/interrupts/interrupts.c index 6d8eefd..6e45e15 100644 --- a/src/kernel/drivers/interrupts/interrupts.c +++ b/src/kernel/drivers/interrupts/interrupts.c @@ -8,8 +8,7 @@ #include IDTR idtr; -IDTEntry *entries; -void *buffer; +IDTEntry entries[256]; void setInterrupt(uint8_t vector, void *callback) { IDTEntry *descriptor = &entries[vector]; @@ -21,12 +20,10 @@ } void setupInterrupts() { - buffer = malloc(sizeof(IDTEntry) * 257); - entries = (IDTEntry *)(((int)buffer + sizeof(IDTEntry) >> 3) << 3); idtr.limit = sizeof(IDTEntry) * 256 - 1; idtr.base = (uint32_t)(uintptr_t)entries; setupExceptions(); setupIRQs(); - __asm__ volatile("lidt %0" : : "memory"(idtr)); - __asm__ volatile("sti"); + asm("lidt %0" : : "memory"(idtr)); + asm("sti"); } diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 12e7979..27ef6a4 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -39,6 +39,8 @@ setInterrupt(OFFSET_2 + 6, &irqHandler14); setInterrupt(OFFSET_2 + 7, &irqHandler15); remapPIC(OFFSET_1, OFFSET_2); + outb(0xA0, 0x20); + outb(0x20, 0x20); } void *irqHandlers[16]; diff --git a/src/kernel/drivers/pci/hardDrive/ahciController.c b/src/kernel/drivers/pci/hardDrive/ahciController.c index ea1c706..9f0058d 100644 --- a/src/kernel/drivers/pci/hardDrive/ahciController.c +++ b/src/kernel/drivers/pci/hardDrive/ahciController.c @@ -84,18 +84,18 @@ HBAPort *HBAPortPtr = port->HBAPortPtr; StopCMD(HBAPortPtr); - HBACommandHeader *CommandHeader = mallocAligned(1024, 10); + HBACommandHeader *CommandHeader = malloc(1024); HBAPortPtr->CommandListBase = LOWER(CommandHeader); HBAPortPtr->CommandListBaseUpper = UPPER(CommandHeader); memset(CommandHeader, 0, 1024); - void *fis = mallocAligned(1024, 10); + void *fis = malloc(1024); HBAPortPtr->FISBaseAddress = LOWER(fis); HBAPortPtr->FISBaseAddressUpper = UPPER(fis); memset(fis, 0, 256); for (int i = 0; i < 32; i++) { CommandHeader[i].PRDTLength = 8; - void *CommandTable = mallocAligned(sizeof(HBACommandTable), 8); + void *CommandTable = malloc(sizeof(HBACommandTable)); CommandHeader[i].CommandTableBaseAddress = LOWER(CommandTable); CommandHeader[i].CommandTableBaseAddressUpper = UPPER(CommandTable); memset(CommandTable, 0, 256); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 541259e..b2fb943 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -18,7 +18,6 @@ currentTask->timerTicks = 1000; currentTask->ticksLeft = 1000; drawLogo(); - yields(); printCPUData(); setupDevices(); while (1) { diff --git a/src/kernel/memory/alloc.c b/src/kernel/memory/alloc.c index 301f1c2..b29cb96 100644 --- a/src/kernel/memory/alloc.c +++ b/src/kernel/memory/alloc.c @@ -4,24 +4,83 @@ #include #include -void *mallocTaskAligned(uintptr_t size, uint8_t alignment, Task *task) { - // todo: do this nicer : have different 'buckets' for different allocation - // sizes - return getPage(); +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +AllocationData allocationData; + +void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { + block->allocatedFine[coarse] |= 1 << fine; + block->allocatedCoarse |= 1 << coarse; + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & 1 << i) { + continue; + } + block->allocatedCoarse &= ~(1 << coarse); + break; + } + return ((uint8_t *)block) + block->blockSize * (32 * coarse + fine); } -void *mallocTask(uintptr_t size, Task *task) { - return mallocTaskAligned(size, 0, task); +void *mallocTask(uintptr_t size, void *task) { + uint32_t sizeBit = LOG2(size) + 1; + if (sizeBit > 10) { + return getPage(); + } + AllocationBlock *block = allocationData[sizeBit], *last = 0; + while (1) { + if (!block) { + block = getPage(); + memset(block, 0, 4096); + block->blockSize = 1 << sizeBit; + if (last) { + block->previous = last; + last->next = block; + } else { + allocationData[sizeBit] = block; + block->previous = (void *)(uintptr_t)sizeBit; + } + block->magic = ALLOCATION_MAGIC; + } + if (block->allocatedCoarse == ~0) { + goto end; + } + for (uint8_t coarse = 0; coarse < 32; coarse++) { + for (uint8_t fine = 0; fine < 32; fine++) { + if (block->allocatedFine[coarse] & (1 << fine)) { + continue; + } + return reserveBlock(block, coarse, fine); + } + } + end: + last = block; + block = block->next; + } + // unreachable } -void *malloc(uintptr_t size) { return mallocTaskAligned(size, 0, currentTask); } +void *malloc(uintptr_t size) { return mallocTask(size, currentTask); } -void *mallocAligned(uintptr_t size, uint8_t alignment) { - return mallocTaskAligned(size, alignment, currentTask); +void free(void *location) { + AllocationBlock *block = (void *)((uintptr_t)location & ~0xFFF); + if (block->magic != ALLOCATION_MAGIC) { + freePage(location); + return; + } + uint16_t index = (uint16_t)((uintptr_t)location & 0xFFF) / block->blockSize; + uint8_t coarse = index / 32; + uint8_t fine = index % 32; + block->allocatedFine[coarse] &= ~(1 << fine); + block->allocatedCoarse &= ~(1 << coarse); + for (uint8_t i = 0; i < 32; i++) { + if (block->allocatedFine[coarse] & (1 << i)) { + continue; + } + block->allocatedCoarse |= 1 << coarse; + return; + } } -void free(void *location) { freePage(location); } - -void freeTaskAllocations(Task *task) { +void freeTaskAllocations(void *task) { // todo } diff --git a/src/kernel/memory/fileSystem.c b/src/kernel/memory/fileSystem.c index 10b742b..9d4b9f7 100644 --- a/src/kernel/memory/fileSystem.c +++ b/src/kernel/memory/fileSystem.c @@ -229,7 +229,7 @@ yields(); return; } - void *bootSector = mallocAligned(512, 1); + void *bootSector = malloc(512); drive->access(drive, 0, bootSector, 1, 0); FatBootSector *boot = bootSector; info = malloc(sizeof(FatInfo)); diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c index f6e90eb..8ad9b14 100644 --- a/src/kernel/memory/paging.c +++ b/src/kernel/memory/paging.c @@ -1,11 +1,12 @@ #include #include +#include #include #include extern uintptr_t kernelEnd; -PageTablePointer *kernelPageTables; +PageTablePointer *kernelPageTable; PagePointer *kernelPages; uint32_t *allocatedPages; // uint32_t *freePageChunks; // todo: hierarchical structure to find a free page @@ -23,7 +24,7 @@ } void enablePaging() { - asm volatile("mov %%eax, %%cr3" : : "a"(kernelPageTables)); + asm volatile("mov %%eax, %%cr3" : : "a"(kernelPageTable)); asm volatile("mov %cr0, %eax"); asm volatile("orl $0x80000000, %eax"); asm volatile("mov %eax, %cr0"); @@ -31,7 +32,7 @@ void initializePaging() { void *currentPosition = (void *)((kernelEnd - 1) & ~0xFFF) + 0x1000; - kernelPageTables = currentPosition; + kernelPageTable = currentPosition; currentPosition += 0x4000; kernelPages = currentPosition; currentPosition += sizeof(uint32_t) * 1024 * 1024; @@ -39,9 +40,9 @@ currentPosition += sizeof(uint32_t) * 1024 * 1024 / 32; // 1024 tables with 1024 pages each for (int i = 0; i < 1024; i++) { - kernelPageTables[i].present = 1; - kernelPageTables[i].writable = 1; - kernelPageTables[i].pagePointerAddress = + kernelPageTable[i].present = 1; + kernelPageTable[i].writable = 1; + kernelPageTable[i].pagePointerAddress = (uint32_t)(uintptr_t)(kernelPages + 1024 * i) >> 12; } for (uint32_t i = 0; i < 0x100000; i++) { diff --git a/src/kernel/task/osTasks.c b/src/kernel/task/osTasks.c index 5e06d2b..f781f2e 100644 --- a/src/kernel/task/osTasks.c +++ b/src/kernel/task/osTasks.c @@ -24,11 +24,11 @@ } } -extern uint32_t *kernelPageTables; +extern uint32_t *kernelPageTable; void initOSTasks() { setRunningTask(&mainTask); - createTask(&printerTask, (uint32_t)printLoop, 0x0, kernelPageTables); + createTask(&printerTask, (uint32_t)printLoop, 0x0, kernelPageTable); } inline Task *getPrinterTask() { return &printerTask; } diff --git a/src/kernel/task/task.c b/src/kernel/task/task.c index 9ace674..57e9437 100644 --- a/src/kernel/task/task.c +++ b/src/kernel/task/task.c @@ -1,5 +1,6 @@ #include #include +#include #include #include // one page @@ -9,6 +10,7 @@ Task *currentTask; ListElement *scheduledTasks = NULL; ListElement *allTasks = NULL; +extern uint32_t *kernelPageTable; void createTask(Task *task, uint32_t mainFunction, uint32_t flags, uint32_t *pagedir) { @@ -22,7 +24,7 @@ task->registers.edi = 0; task->registers.eflags = flags; task->registers.eip = mainFunction; - task->registers.cr3 = (uintptr_t)pagedir; + task->registers.cr3 = kernelPageTable; task->registers.esp = STACK_SIZE + (uintptr_t)stack; task->stack = stack; task->messages = NULL;