diff --git a/BAN/.gitignore b/BAN/.gitignore new file mode 100644 index 0000000000..268e074888 --- /dev/null +++ b/BAN/.gitignore @@ -0,0 +1,3 @@ +*.a +*.d +*.o diff --git a/BAN/Makefile b/BAN/Makefile index 2fe06d79a9..a97a49566d 100644 --- a/BAN/Makefile +++ b/BAN/Makefile @@ -15,7 +15,7 @@ LIBDIR?=$(EXEC_PREFIX)/lib CFLAGS:=$(CFLAGS) -D__is_ban -Iinclude -ffreestanding -Wall -Wextra CPPFLAGS:=$(CPPFLAGS) -LIBK_CFLAGS:=$(CFLAGS) -D__is_bank +LIBK_CFLAGS:=$(CFLAGS) -D__is_kernel LIBK_CPPFLAGS:=$(CPPFLAGS) ARCHDIR=arch/$(HOSTARCH) @@ -41,17 +41,17 @@ $(HOSTEDOBJS) \ BANK_OBJS=$(FREEOBJS:.o=.bank.o) -BINARIES=bank.a +BINARIES=libbank.a .PHONY: all always clean install install-headers install-libs .SUFFIXES: .o .bank.o .cpp .S all: $(BINARIES) -ban.a: always $(OBJS) +libban.a: always $(OBJS) cd $(BUILDDIR) && $(AR) rcs $@ $(OBJS) -bank.a: always $(LIBK_OBJS) +libbank.a: always $(LIBK_OBJS) cd $(BUILDDIR) && $(AR) rcs $@ $(LIBK_OBJS) .cpp.o: diff --git a/BAN/build/bank.a b/BAN/build/bank.a deleted file mode 100644 index 8b277f0dd5..0000000000 --- a/BAN/build/bank.a +++ /dev/null @@ -1 +0,0 @@ -! diff --git a/BAN/include/BAN/Errors.h b/BAN/include/BAN/Errors.h index 9acfdb4345..2c836e715e 100644 --- a/BAN/include/BAN/Errors.h +++ b/BAN/include/BAN/Errors.h @@ -2,14 +2,16 @@ #include -#if defined(__is_bank) +#include + +#if defined(__is_kernel) #include - #define MUST(error) { decltype(error) e = error; if (e.HasError()) { Kernel::panic("{}", e.GetError()); } } + #define MUST(error) ({ auto e = error; if (e.IsError()) Kernel::panic("{}", e.GetError()); }) #else #error "NOT IMPLEMENTED" #endif -#define TRY(error) { decltype(error) e = error; if (e.HasError()) return e; } +#define TRY(error) ({ auto e = error; if (e.IsError()) return e; e.Value(); }) class Error @@ -59,6 +61,7 @@ public: bool IsError() const { return m_error; } const Error& GetError() const { return *m_error; } + void Value() { } private: Error* m_error; diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index d35bc84e3f..9b469a0e07 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -1,9 +1,8 @@ #pragma once #include -#include -#if defined(__is_bank) +#if defined(__is_kernel) #include #else #include @@ -21,12 +20,12 @@ namespace BAN class Queue { private: - #if defined(__is_bank) - using allocator = kmalloc; - using deallocator = kfree; + #if defined(__is_kernel) + static constexpr auto& allocator = kmalloc; + static constexpr auto& deallocator = kfree; #else - using allocator = malloc; - using deallocator = free; + static constexpr auto& allocator = malloc; + static constexpr auto& deallocator = free; #endif @@ -59,13 +58,13 @@ namespace BAN template Queue::~Queue() { - deallocator(m_data); + Queue::deallocator(m_data); } template ErrorOr Queue::Push(const T& value) { - VerifyCapacity(m_size + 1); + TRY(VerifyCapacity(m_size + 1)); m_data[m_size++] = value; return {}; } @@ -110,16 +109,18 @@ namespace BAN if (m_capacity > size) return {}; - size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1) * sizeof(T); - void* new_data = allocator(new_cap); + size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1); + void* new_data = Queue::allocator(new_cap * sizeof(T)); if (new_data == nullptr) - return Error { .message = "Queue: out of memory", .error_code = ErrorCode::OutOfMemory }; + return Error::FromString("Queue: out of memory"); memcpy(new_data, m_data, m_size * sizeof(T)); - deallocator(m_data); + Queue::deallocator(m_data); m_data = (T*)new_data; m_capacity = new_cap; + + return {}; } } \ No newline at end of file diff --git a/kernel/Makefile b/kernel/Makefile index 2f9b482c05..177db3b2ab 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -16,7 +16,7 @@ INCLUDEDIR?=$(PREFIX)/include CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -fstack-protector -ffreestanding -Wall -Wextra -Wno-unused-function CPPFLAGS:=$(CPPFLAGS) -fno-rtti -fno-exceptions LDFLAGS:=$(LDFLAGS) -LIBS:=$(LIBS) -nostdlib -lk -lgcc +LIBS:=$(LIBS) -nostdlib -lk -lbank -lgcc ARCHDIR=arch/$(HOSTARCH) diff --git a/kernel/include/kernel/Serial.h b/kernel/include/kernel/Serial.h index 6f4e1c0720..8e384b219c 100644 --- a/kernel/include/kernel/Serial.h +++ b/kernel/include/kernel/Serial.h @@ -1,9 +1,27 @@ #pragma once #include +#include -#define dprint BAN::Formatter::print -#define dprintln BAN::Formatter::println +#define dprintln(...) \ + do { \ + BAN::Formatter::print("[{5.3}] {}({}): ", (float)PIT::ms_since_boot(), __FILE__, __LINE__); \ + BAN::Formatter::println(__VA_ARGS__); \ + } while(false) + +#define dwarnln(...) \ + do { \ + BAN::Formatter::print("\e[33m"); \ + dprintln(__VA_ARGS__); \ + BAN::Formatter::print("\e[m"); \ + } while(false) + +#define derrorln(...) \ + do { \ + BAN::Formatter::print("\e[31m"); \ + dprintln(__VA_ARGS__); \ + BAN::Formatter::print("\e[m"); \ + } while(false) namespace Serial { diff --git a/kernel/include/kernel/kprint.h b/kernel/include/kernel/kprint.h index 4e6fdc23fa..f73583f826 100644 --- a/kernel/include/kernel/kprint.h +++ b/kernel/include/kernel/kprint.h @@ -3,5 +3,5 @@ #include #include -#define kprint BAN::Formatter::print -#define kprintln BAN::Formatter::println +#define kprint(...) BAN::Formatter::print(__VA_ARGS__) +#define kprintln(...) BAN::Formatter::println(__VA_ARGS__) diff --git a/kernel/kernel/Keyboard.cpp b/kernel/kernel/Keyboard.cpp index 23c062857c..d38858ce81 100644 --- a/kernel/kernel/Keyboard.cpp +++ b/kernel/kernel/Keyboard.cpp @@ -1,10 +1,10 @@ +#include #include #include #include #include #include #include -#include #include #include @@ -61,7 +61,7 @@ namespace Keyboard { - static bool s_keyboard_state[0xFF] = {}; + static bool s_keyboard_state[0xFF] = {}; struct Command { @@ -73,10 +73,10 @@ namespace Keyboard uint8_t _ack = 0; bool _done = false; }; - static Queue s_keyboard_command_queue; - static uint8_t s_keyboard_command_extra = 0x00; + static BAN::Queue s_keyboard_command_queue; + static uint8_t s_keyboard_command_extra = 0x00; - static Queue s_key_event_queue; + static BAN::Queue s_key_event_queue; static uint8_t s_keyboard_key_buffer[10] = {}; static uint8_t s_keyboard_key_buffer_size = 0; @@ -290,8 +290,8 @@ namespace Keyboard if (key != Key::INVALID) { auto error_or = s_key_event_queue.Push({ .key = key, .modifiers = modifiers, .pressed = pressed }); - if (error_or.HasError()) - dprintln("PS/2 Keyboard: {}", error_or.GetError().message); + if (error_or.IsError()) + dwarnln("{}", error_or.GetError()); } s_keyboard_key_buffer_size -= index + 1; memmove(s_keyboard_key_buffer, s_keyboard_key_buffer + index, s_keyboard_key_buffer_size); @@ -384,7 +384,7 @@ namespace Keyboard i8042_controller_command(I8042_TEST_CONTROLLER); if (wait_and_read() != I8042_TEST_CONTROLLER_PASS) { - kprintln("\e[33mERROR: PS/2 controller self test failed\e[m"); + derrorln("PS/2 controller self test failed"); return false; } @@ -394,7 +394,7 @@ namespace Keyboard i8042_controller_command(I8042_TEST_FIRST_PORT); if (wait_and_read() != I8042_TEST_FIRST_PORT_PASS) { - kprintln("\e[33mERROR: PS/2 first port test failed\e[m"); + derrorln("PS/2 first port test failed"); return false; } diff --git a/kernel/kernel/SSP.cpp b/kernel/kernel/SSP.cpp index 0f3d3b77ad..8c5bef3745 100644 --- a/kernel/kernel/SSP.cpp +++ b/kernel/kernel/SSP.cpp @@ -1,6 +1,7 @@ +#include + #include #include -#include #if UINT32_MAX == UINTPTR_MAX #define STACK_CHK_GUARD 0xe2dee396 @@ -15,7 +16,7 @@ __BEGIN_DECLS __attribute__((noreturn)) void __stack_chk_fail(void) { - printf("Stack smashing detected\n"); + Kernel::panic("Stack smashing detected"); abort(); } diff --git a/kernel/kernel/kmalloc.cpp b/kernel/kernel/kmalloc.cpp index e20cc5af50..5bf79fe35c 100644 --- a/kernel/kernel/kmalloc.cpp +++ b/kernel/kernel/kmalloc.cpp @@ -73,8 +73,7 @@ void kmalloc_dump_nodes() for (size_t i = 0; i < s_kmalloc_node_count; i++) { kmalloc_node& node = s_kmalloc_node_head[i]; - if (i < 10) dprint(" "); - dprintln(" ({}) {}, node at {}, free: {}, size: {}", i, (void*)&node, (void*)node.addr, node.free, node.size); + dprintln(" ({3}) {}, node at {}, free: {}, size: {}", i, (void*)&node, (void*)node.addr, node.free, node.size); } }