diff --git a/kernel/arch/interrupts.cpp b/kernel/arch/interrupts.cpp index 51f7b48..e202a40 100644 --- a/kernel/arch/interrupts.cpp +++ b/kernel/arch/interrupts.cpp @@ -246,13 +246,13 @@ void Interrupts::SetupIdt() SetIdtEntry(46, TYPE_INTERRUPT_GATE, (unsigned long)irq14); SetIdtEntry(47, TYPE_INTERRUPT_GATE, (unsigned long)irq15); - /* fill the IDT descriptor */ + // build idt descriptor unsigned long idt_ptr[2]; unsigned long idt_address = (unsigned long)IDT; idt_ptr[0] = (sizeof(struct IDT_entry) * 256) + ((idt_address & 0xffff) << 16); idt_ptr[1] = idt_address >> 16; - /* load the IDT */ + // load idt load_idt(idt_ptr); } @@ -292,7 +292,7 @@ void Interrupts::HandleException(unsigned int vector, struct interrupt_frame *fr void Interrupts::HandleInterrupt(unsigned int interrupt) { - for (int i = 0; i < entries.Size(); i++) { + for (size_t i = 0; i < entries.Size(); i++) { InterruptHandlerEntry &entry = entries.At(i); if (entry.interrupt == interrupt) entry.handler->HandleInterrupt(interrupt); diff --git a/kernel/device/cmos.cpp b/kernel/device/cmos.cpp new file mode 100644 index 0000000..c242ce6 --- /dev/null +++ b/kernel/device/cmos.cpp @@ -0,0 +1,20 @@ +#include +#include + +using namespace Device; +using namespace Kernel; + +#define PORT_ADDR 0x70 +#define PORT_DATA 0x71 + +uint8_t CMOS::Read(uint8_t addr) +{ + IO::Out8(PORT_ADDR, addr); + return IO::In8(PORT_DATA); +} + +void CMOS::Write(uint8_t addr, uint8_t value) +{ + IO::Out8(PORT_ADDR, addr); + IO::Out8(PORT_DATA, value); +} \ No newline at end of file diff --git a/kernel/device/keyboard.cpp b/kernel/device/keyboard.cpp index f5c1fea..cb9d45e 100644 --- a/kernel/device/keyboard.cpp +++ b/kernel/device/keyboard.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using namespace Kernel; using namespace Device; @@ -75,7 +76,9 @@ void Keyboard::HandleInterrupt(unsigned int interrupt) char chr = MapChar(scancode); if (chr != 0) - Kernel::TTY::Write(&chr, 1); + { + putchar(chr); + } } void Keyboard::NewScancode(unsigned int scancode, char c) diff --git a/kernel/heap.cpp b/kernel/heap.cpp index 21a9d3d..17bb175 100644 --- a/kernel/heap.cpp +++ b/kernel/heap.cpp @@ -45,6 +45,14 @@ extern "C" return (allocation_map[a] & (0x1 << b)) == 0; } + size_t get_free_heap() { + size_t freeHeap = 0; + for (size_t i = 0; i < ALLOCATION_MAP_ENTRIES; i++) + if (is_free(i)) + freeHeap += HEAP_SEG_SIZE; + return freeHeap; + } + void set_free(size_t alloc_unit, bool free) { size_t a = alloc_unit / 8; diff --git a/kernel/include/device/cmos.h b/kernel/include/device/cmos.h new file mode 100644 index 0000000..1d92f02 --- /dev/null +++ b/kernel/include/device/cmos.h @@ -0,0 +1,19 @@ +#ifndef _CMOS_H +#define _CMOS_H + +#include + +namespace Device +{ + + class CMOS + { + public: + static uint8_t Read(uint8_t addr); + + static void Write(uint8_t addr, uint8_t value); + }; + +} // namespace Device + +#endif \ No newline at end of file diff --git a/kernel/include/device/devicemanager.h b/kernel/include/device/devicemanager.h index 7971cf3..f4d9fe4 100644 --- a/kernel/include/device/devicemanager.h +++ b/kernel/include/device/devicemanager.h @@ -4,6 +4,7 @@ #include "cpu.h" #include "keyboard.h" #include "pic.h" +#include "cmos.h" namespace Device { diff --git a/kernel/include/device/keyboard.h b/kernel/include/device/keyboard.h index e7b4a04..32a8746 100644 --- a/kernel/include/device/keyboard.h +++ b/kernel/include/device/keyboard.h @@ -9,7 +9,7 @@ namespace Device { private: char scancode_map[128]; - + public: void Initialize(); diff --git a/kernel/include/kernel/heap.h b/kernel/include/kernel/heap.h index 8a37497..ec42519 100644 --- a/kernel/include/kernel/heap.h +++ b/kernel/include/kernel/heap.h @@ -11,6 +11,8 @@ extern "C" void heap_init(); + size_t get_free_heap(); + void *kmalloc(size_t size); void kfree(void *ptr); diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index 8750553..ac1ef40 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -9,27 +9,34 @@ using namespace Device; /* nekosys Kernel entry point */ extern "C" void nkmain() { - // Welcome message + Interrupts::Disable(); + + // Banner TTY::Clear(); TTY::SetColor(0x0b); printf("nekosys 0.01 \n"); - TTY::SetColor(0x0f); + TTY::SetColor(0x07); + + // Init + printf("Initializing...\n"); + uint16_t base_memory = (CMOS::Read(0x16) << 8) | CMOS::Read(0x15); + uint32_t ext_memory = (CMOS::Read(0x31) << 8 | CMOS::Read(0x30)); + + printf("Base Memory: %d KB\n", base_memory); + printf("Extended Memory: %d KB\n", ext_memory); - printf("Initializing heap...\n"); heap_init(); - printf("Initializing devices...\n"); - DeviceManager::Initialize(); - // Set up environment - printf("Setting up interrupts...\n"); - Interrupts::Disable(); Interrupts::SetupIdt(); + DeviceManager::Initialize(); + Interrupts::Enable(); // Dummy terminal printf("Initialized.\n\n"); printf("$ "); + // Idle Device::CPU::Halt(); } \ No newline at end of file diff --git a/libc/cpplib.cpp b/libc/cpplib.cpp deleted file mode 100644 index 5013230..0000000 --- a/libc/cpplib.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -extern "C" void __cxa_pure_virtual() -{ - // Do nothing or print an error message. -} - -void *operator new(size_t size) -{ - return malloc(size); -} - -void *operator new[](size_t size) -{ - return malloc(size); -} - -void operator delete(void *p) -{ - free(p); -} - -void operator delete[](void *p) -{ - free(p); -} \ No newline at end of file diff --git a/libc/icxxabi.cpp b/libc/icxxabi.cpp new file mode 100644 index 0000000..28c27cc --- /dev/null +++ b/libc/icxxabi.cpp @@ -0,0 +1,63 @@ +#include +#include +#include "icxxabi.h" + +extern "C" void __cxa_pure_virtual() +{ + // Do nothing or print an error message. +} + +atexitFuncEntry_t __atexitFuncs[ATEXIT_FUNC_MAX]; +uarch_t __atexitFuncCount = 0; + +void *__dso_handle = 0; + +int __cxa_atexit(void (*f)(void *), void *objptr, void *dso){ + if(__atexitFuncCount >= ATEXIT_FUNC_MAX){ + return -1; + } + __atexitFuncs[__atexitFuncCount].destructorFunc = f; + __atexitFuncs[__atexitFuncCount].objPtr = objptr; + __atexitFuncs[__atexitFuncCount].dsoHandle = dso; + __atexitFuncCount++; + return 0; +} + +void __cxa_finalize(void *f){ + signed i = __atexitFuncCount; + if(!f){ + while(i--){ + if(__atexitFuncs[i].destructorFunc){ + (*__atexitFuncs[i].destructorFunc)(__atexitFuncs[i].objPtr); + } + } + return; + } + + for(; i >= 0; i--){ + if(__atexitFuncs[i].destructorFunc == f){ + (*__atexitFuncs[i].destructorFunc)(__atexitFuncs[i].objPtr); + __atexitFuncs[i].destructorFunc = 0; + } + } +} + +void *operator new(size_t size) +{ + return malloc(size); +} + +void *operator new[](size_t size) +{ + return malloc(size); +} + +void operator delete(void *p) +{ + free(p); +} + +void operator delete[](void *p) +{ + free(p); +} \ No newline at end of file diff --git a/libc/icxxabi.h b/libc/icxxabi.h new file mode 100644 index 0000000..ac9ea20 --- /dev/null +++ b/libc/icxxabi.h @@ -0,0 +1,25 @@ +#ifndef _ICXXABI_H +#define _ICXXABI_H +#define ATEXIT_FUNC_MAX 128 + +extern "C" +{ + + typedef unsigned uarch_t; + + struct atexitFuncEntry_t + { + void (*destructorFunc)(void *); + void *objPtr; + void *dsoHandle; + }; + + extern void *__dso_handle; + + int __cxa_atexit(void (*f)(void *), void *objptr, void *dso); + void __cxa_finalize(void *f); + + void __cxa_pure_virtual(); +}; + +#endif \ No newline at end of file diff --git a/libc/include/stdio.h b/libc/include/stdio.h index b3be14e..44afc85 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -12,6 +12,7 @@ extern "C" { int printf(const char* __restrict, ...); int putchar(int); int puts(const char*); +int getchar(void); #ifdef __cplusplus } diff --git a/libc/stdio/getchar.c b/libc/stdio/getchar.c new file mode 100644 index 0000000..6724ae3 --- /dev/null +++ b/libc/stdio/getchar.c @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/nekolib/nk/vector.h b/nekolib/nk/vector.h index 01e8fef..14225a2 100644 --- a/nekolib/nk/vector.h +++ b/nekolib/nk/vector.h @@ -12,15 +12,30 @@ namespace nk { private: T *data = nullptr; - size_t index = 0; + size_t size = 0; size_t capacity = 0; public: void Add(T obj) { - EnsureCapacity(index + 1); - data[index] = obj; - index++; + EnsureCapacity(size + 1); + data[size] = obj; + size++; + } + + void Remove(size_t idx) + { + for (size_t i = idx + 1; i < size; i++) + { + data[i - 1] = data[i]; + } + + size--; + } + + const T &At(size_t index) const + { + return data[index]; } T &At(size_t index) @@ -30,7 +45,7 @@ namespace nk size_t Size() { - return index; + return size; } size_t Capacity() @@ -38,18 +53,23 @@ namespace nk return capacity; } + ~Vector() + { + free((void *)data); + } + private: void EnsureCapacity(size_t min) { if (min > capacity) { size_t newCapacity = min * 2; - T *newData = (T*) malloc(sizeof(T) * newCapacity); + T *newData = (T *)malloc(sizeof(T) * newCapacity); if (this->data != nullptr) { - memcpy(newData, data, sizeof(T) * index); + memcpy(newData, data, sizeof(T) * size); } - free((void*) this->data); + free((void *)this->data); this->data = newData; this->capacity = newCapacity; }