Kernel: lol

This commit is contained in:
Bananymous 2022-12-13 12:10:50 +02:00
parent 68e88d9413
commit 334abe6b27
11 changed files with 63 additions and 39 deletions

3
BAN/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.a
*.d
*.o

View File

@ -15,7 +15,7 @@ LIBDIR?=$(EXEC_PREFIX)/lib
CFLAGS:=$(CFLAGS) -D__is_ban -Iinclude -ffreestanding -Wall -Wextra CFLAGS:=$(CFLAGS) -D__is_ban -Iinclude -ffreestanding -Wall -Wextra
CPPFLAGS:=$(CPPFLAGS) CPPFLAGS:=$(CPPFLAGS)
LIBK_CFLAGS:=$(CFLAGS) -D__is_bank LIBK_CFLAGS:=$(CFLAGS) -D__is_kernel
LIBK_CPPFLAGS:=$(CPPFLAGS) LIBK_CPPFLAGS:=$(CPPFLAGS)
ARCHDIR=arch/$(HOSTARCH) ARCHDIR=arch/$(HOSTARCH)
@ -41,17 +41,17 @@ $(HOSTEDOBJS) \
BANK_OBJS=$(FREEOBJS:.o=.bank.o) BANK_OBJS=$(FREEOBJS:.o=.bank.o)
BINARIES=bank.a BINARIES=libbank.a
.PHONY: all always clean install install-headers install-libs .PHONY: all always clean install install-headers install-libs
.SUFFIXES: .o .bank.o .cpp .S .SUFFIXES: .o .bank.o .cpp .S
all: $(BINARIES) all: $(BINARIES)
ban.a: always $(OBJS) libban.a: always $(OBJS)
cd $(BUILDDIR) && $(AR) rcs $@ $(OBJS) cd $(BUILDDIR) && $(AR) rcs $@ $(OBJS)
bank.a: always $(LIBK_OBJS) libbank.a: always $(LIBK_OBJS)
cd $(BUILDDIR) && $(AR) rcs $@ $(LIBK_OBJS) cd $(BUILDDIR) && $(AR) rcs $@ $(LIBK_OBJS)
.cpp.o: .cpp.o:

View File

@ -1 +0,0 @@
!<arch>

View File

@ -2,14 +2,16 @@
#include <BAN/Formatter.h> #include <BAN/Formatter.h>
#if defined(__is_bank) #include <string.h>
#if defined(__is_kernel)
#include <kernel/panic.h> #include <kernel/panic.h>
#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 #else
#error "NOT IMPLEMENTED" #error "NOT IMPLEMENTED"
#endif #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 class Error
@ -59,6 +61,7 @@ public:
bool IsError() const { return m_error; } bool IsError() const { return m_error; }
const Error& GetError() const { return *m_error; } const Error& GetError() const { return *m_error; }
void Value() { }
private: private:
Error* m_error; Error* m_error;

View File

@ -1,9 +1,8 @@
#pragma once #pragma once
#include <BAN/Errors.h> #include <BAN/Errors.h>
#include <kernel/kmalloc.h>
#if defined(__is_bank) #if defined(__is_kernel)
#include <kernel/kmalloc.h> #include <kernel/kmalloc.h>
#else #else
#include <stdlib.h> #include <stdlib.h>
@ -21,12 +20,12 @@ namespace BAN
class Queue class Queue
{ {
private: private:
#if defined(__is_bank) #if defined(__is_kernel)
using allocator = kmalloc; static constexpr auto& allocator = kmalloc;
using deallocator = kfree; static constexpr auto& deallocator = kfree;
#else #else
using allocator = malloc; static constexpr auto& allocator = malloc;
using deallocator = free; static constexpr auto& deallocator = free;
#endif #endif
@ -59,13 +58,13 @@ namespace BAN
template<typename T> template<typename T>
Queue<T>::~Queue() Queue<T>::~Queue()
{ {
deallocator(m_data); Queue<T>::deallocator(m_data);
} }
template<typename T> template<typename T>
ErrorOr<void> Queue<T>::Push(const T& value) ErrorOr<void> Queue<T>::Push(const T& value)
{ {
VerifyCapacity(m_size + 1); TRY(VerifyCapacity(m_size + 1));
m_data[m_size++] = value; m_data[m_size++] = value;
return {}; return {};
} }
@ -110,16 +109,18 @@ namespace BAN
if (m_capacity > size) if (m_capacity > size)
return {}; return {};
size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1) * sizeof(T); size_type new_cap = MAX(m_capacity * 1.5f, m_capacity + 1);
void* new_data = allocator(new_cap); void* new_data = Queue<T>::allocator(new_cap * sizeof(T));
if (new_data == nullptr) 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)); memcpy(new_data, m_data, m_size * sizeof(T));
deallocator(m_data); Queue<T>::deallocator(m_data);
m_data = (T*)new_data; m_data = (T*)new_data;
m_capacity = new_cap; m_capacity = new_cap;
return {};
} }
} }

View File

@ -16,7 +16,7 @@ INCLUDEDIR?=$(PREFIX)/include
CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -fstack-protector -ffreestanding -Wall -Wextra -Wno-unused-function CFLAGS:=$(CFLAGS) -D__is_kernel -Iinclude -fstack-protector -ffreestanding -Wall -Wextra -Wno-unused-function
CPPFLAGS:=$(CPPFLAGS) -fno-rtti -fno-exceptions CPPFLAGS:=$(CPPFLAGS) -fno-rtti -fno-exceptions
LDFLAGS:=$(LDFLAGS) LDFLAGS:=$(LDFLAGS)
LIBS:=$(LIBS) -nostdlib -lk -lgcc LIBS:=$(LIBS) -nostdlib -lk -lbank -lgcc
ARCHDIR=arch/$(HOSTARCH) ARCHDIR=arch/$(HOSTARCH)

View File

@ -1,9 +1,27 @@
#pragma once #pragma once
#include <BAN/Formatter.h> #include <BAN/Formatter.h>
#include <kernel/PIT.h>
#define dprint BAN::Formatter::print<Serial::serial_putc> #define dprintln(...) \
#define dprintln BAN::Formatter::println<Serial::serial_putc> do { \
BAN::Formatter::print<Serial::serial_putc>("[{5.3}] {}({}): ", (float)PIT::ms_since_boot(), __FILE__, __LINE__); \
BAN::Formatter::println<Serial::serial_putc>(__VA_ARGS__); \
} while(false)
#define dwarnln(...) \
do { \
BAN::Formatter::print<Serial::serial_putc>("\e[33m"); \
dprintln(__VA_ARGS__); \
BAN::Formatter::print<Serial::serial_putc>("\e[m"); \
} while(false)
#define derrorln(...) \
do { \
BAN::Formatter::print<Serial::serial_putc>("\e[31m"); \
dprintln(__VA_ARGS__); \
BAN::Formatter::print<Serial::serial_putc>("\e[m"); \
} while(false)
namespace Serial namespace Serial
{ {

View File

@ -3,5 +3,5 @@
#include <BAN/Formatter.h> #include <BAN/Formatter.h>
#include <kernel/tty.h> #include <kernel/tty.h>
#define kprint BAN::Formatter::print<TTY::putchar> #define kprint(...) BAN::Formatter::print<TTY::putchar>(__VA_ARGS__)
#define kprintln BAN::Formatter::println<TTY::putchar> #define kprintln(...) BAN::Formatter::println<TTY::putchar>(__VA_ARGS__)

View File

@ -1,10 +1,10 @@
#include <BAN/Queue.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/IO.h> #include <kernel/IO.h>
#include <kernel/Keyboard.h> #include <kernel/Keyboard.h>
#include <kernel/kprint.h> #include <kernel/kprint.h>
#include <kernel/PIC.h> #include <kernel/PIC.h>
#include <kernel/PIT.h> #include <kernel/PIT.h>
#include <kernel/Queue.h>
#include <kernel/Serial.h> #include <kernel/Serial.h>
#include <kernel/KeyboardLayout/FI.h> #include <kernel/KeyboardLayout/FI.h>
@ -61,7 +61,7 @@
namespace Keyboard namespace Keyboard
{ {
static bool s_keyboard_state[0xFF] = {}; static bool s_keyboard_state[0xFF] = {};
struct Command struct Command
{ {
@ -73,10 +73,10 @@ namespace Keyboard
uint8_t _ack = 0; uint8_t _ack = 0;
bool _done = false; bool _done = false;
}; };
static Queue<Command> s_keyboard_command_queue; static BAN::Queue<Command> s_keyboard_command_queue;
static uint8_t s_keyboard_command_extra = 0x00; static uint8_t s_keyboard_command_extra = 0x00;
static Queue<KeyEvent> s_key_event_queue; static BAN::Queue<KeyEvent> s_key_event_queue;
static uint8_t s_keyboard_key_buffer[10] = {}; static uint8_t s_keyboard_key_buffer[10] = {};
static uint8_t s_keyboard_key_buffer_size = 0; static uint8_t s_keyboard_key_buffer_size = 0;
@ -290,8 +290,8 @@ namespace Keyboard
if (key != Key::INVALID) if (key != Key::INVALID)
{ {
auto error_or = s_key_event_queue.Push({ .key = key, .modifiers = modifiers, .pressed = pressed }); auto error_or = s_key_event_queue.Push({ .key = key, .modifiers = modifiers, .pressed = pressed });
if (error_or.HasError()) if (error_or.IsError())
dprintln("PS/2 Keyboard: {}", error_or.GetError().message); dwarnln("{}", error_or.GetError());
} }
s_keyboard_key_buffer_size -= index + 1; s_keyboard_key_buffer_size -= index + 1;
memmove(s_keyboard_key_buffer, s_keyboard_key_buffer + index, s_keyboard_key_buffer_size); 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); i8042_controller_command(I8042_TEST_CONTROLLER);
if (wait_and_read() != I8042_TEST_CONTROLLER_PASS) 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; return false;
} }
@ -394,7 +394,7 @@ namespace Keyboard
i8042_controller_command(I8042_TEST_FIRST_PORT); i8042_controller_command(I8042_TEST_FIRST_PORT);
if (wait_and_read() != I8042_TEST_FIRST_PORT_PASS) 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; return false;
} }

View File

@ -1,6 +1,7 @@
#include <kernel/panic.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#if UINT32_MAX == UINTPTR_MAX #if UINT32_MAX == UINTPTR_MAX
#define STACK_CHK_GUARD 0xe2dee396 #define STACK_CHK_GUARD 0xe2dee396
@ -15,7 +16,7 @@ __BEGIN_DECLS
__attribute__((noreturn)) __attribute__((noreturn))
void __stack_chk_fail(void) void __stack_chk_fail(void)
{ {
printf("Stack smashing detected\n"); Kernel::panic("Stack smashing detected");
abort(); abort();
} }

View File

@ -73,8 +73,7 @@ void kmalloc_dump_nodes()
for (size_t i = 0; i < s_kmalloc_node_count; i++) for (size_t i = 0; i < s_kmalloc_node_count; i++)
{ {
kmalloc_node& node = s_kmalloc_node_head[i]; kmalloc_node& node = s_kmalloc_node_head[i];
if (i < 10) dprint(" "); dprintln(" ({3}) {}, node at {}, free: {}, size: {}", i, (void*)&node, (void*)node.addr, node.free, node.size);
dprintln(" ({}) {}, node at {}, free: {}, size: {}", i, (void*)&node, (void*)node.addr, node.free, node.size);
} }
} }