From b1a912a8a6ff472a49b2e0a09cfd433adfc2cb24 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 18 May 2024 21:53:38 -0400 Subject: reorganization, cross compiler --- libraries/euler/allocator.cpp | 147 ------------------------------ libraries/euler/cassert.cpp | 16 ---- libraries/euler/cctype.cpp | 11 --- libraries/euler/cstdio.cpp | 102 --------------------- libraries/euler/entry.cpp | 19 ---- libraries/euler/include/cassert | 13 --- libraries/euler/include/cctype | 7 -- libraries/euler/include/cstddef | 1 - libraries/euler/include/cstdint | 1 - libraries/euler/include/cstdio | 19 ---- libraries/euler/include/cstring | 14 --- libraries/euler/include/euler/syscall.hpp | 51 ----------- libraries/euler/syscall.asm | 88 ------------------ 13 files changed, 489 deletions(-) delete mode 100644 libraries/euler/allocator.cpp delete mode 100644 libraries/euler/cassert.cpp delete mode 100644 libraries/euler/cctype.cpp delete mode 100644 libraries/euler/cstdio.cpp delete mode 100644 libraries/euler/entry.cpp delete mode 100644 libraries/euler/include/cassert delete mode 100644 libraries/euler/include/cctype delete mode 120000 libraries/euler/include/cstddef delete mode 120000 libraries/euler/include/cstdint delete mode 100644 libraries/euler/include/cstdio delete mode 100644 libraries/euler/include/cstring delete mode 100644 libraries/euler/include/euler/syscall.hpp delete mode 100644 libraries/euler/syscall.asm (limited to 'libraries/euler') diff --git a/libraries/euler/allocator.cpp b/libraries/euler/allocator.cpp deleted file mode 100644 index 5242df1..0000000 --- a/libraries/euler/allocator.cpp +++ /dev/null @@ -1,147 +0,0 @@ -#include - -//i guess we could figure out which parts of the pages the kernel allocated to -//load the application were not actually needed and add those to the memory -//that is available to be allocated, but whatever. - -namespace euler::allocator { - - struct block_info { - //vaddr, divisible by size - uint64_t start; - //power of 2, 0 for unused - uint64_t size; - }; - - struct block_info_page { - block_info infos[255]; - block_info_page *next; - }; - - static_assert(sizeof(block_info_page) == 4088); - - static block_info_page *first_bi_page = 0; - - static block_info *try_find_block(uint64_t start, uint64_t size) { - for (block_info_page *bip = first_bi_page; bip; bip = bip->next) - for (int i = 0; i < 255; ++i) - if (bip->infos[i].start == start && bip->infos[i].size == size) - return bip->infos + i; - return 0; - } - - static block_info *add_block(uint64_t start, uint64_t size) { - for (block_info_page *bip = first_bi_page; bip; bip = bip->next) - for (int i = 0; i < 255; ++i) - if (bip->infos[i].size == 0) { - bip->infos[i].start = start; - bip->infos[i].size = size; - return bip->infos + i; - } - block_info_page *new_bip = (block_info_page *)_syscall_get_new_pages(1); - new_bip->infos[0].start = start; - new_bip->infos[0].size = size; - for (int i = 1; i < 255; ++i) - new_bip->infos[i].size = 0; - new_bip->next = first_bi_page; - first_bi_page = new_bip; - return new_bip->infos; - } - - static block_info *find_minimal_block(uint64_t minimum_length) { - block_info *minimal_so_far = 0; - uint64_t length_so_far = UINT64_MAX; - for (block_info_page *bip = first_bi_page; bip; bip = bip->next) - for (int i = 0; i < 255; ++i) - if (bip->infos[i].size == minimum_length) - return bip->infos + i; - else if ( - bip->infos[i].size > minimum_length && - bip->infos[i].size < length_so_far - ) { - minimal_so_far = bip->infos + i; - length_so_far = bip->infos[i].size; - } - if (minimal_so_far != 0) - return minimal_so_far; - uint64_t pages_needed = (minimum_length - 1) / 4096 + 1; - void *block_start = _syscall_get_new_pages(pages_needed); - return add_block((uint64_t)block_start, pages_needed * 4096); - } - - static void deallocate_aligned(uint64_t start, uint64_t length) { - block_info *buddy = try_find_block(start ^ length, length); - if (buddy) { - buddy->size = 0; - deallocate_aligned(start & ~length, length * 2); - return; - } - add_block(start, length); - } - - static void deallocate(void *start, uint64_t length) { - uint64_t at = (uint64_t)start; - uint64_t left = length; - uint64_t i; - for (i = 1; i <= left; i *= 2) - if (at & i) { - deallocate_aligned(at, i); - at += i; - left -= i; - } - for (i /= 2; left > 0; i /= 2) - if (i <= left) { - deallocate_aligned(at, i); - at += i; - left -= i; - } - } - - [[nodiscard]] static void *allocate(uint64_t length) { - block_info *bi = find_minimal_block(length); - void *to_return = (void *)bi->start; - void *new_start = (void *)(bi->start + length); - uint64_t new_length = bi->size - length; - bi->size = 0; - deallocate(new_start, new_length); - return to_return; - } - - static void deallocate_with_length(void *start) { - uint64_t real_start = (uint64_t)start - 8; - deallocate((void *)real_start, *(uint64_t *)real_start); - } - - [[nodiscard]] static void *allocate_with_length(uint64_t length) { - uint64_t *real_start = (uint64_t *)allocate(length + 8); - *real_start = length + 8; - return real_start + 1; - } - -} - -using namespace euler::allocator; - -void operator delete[](void *start) { - deallocate_with_length(start); -} - -void operator delete(void *start) { - deallocate_with_length(start); -} - -void operator delete[](void *start, uint64_t) { - deallocate_with_length(start); -} - -void operator delete(void *start, uint64_t) { - deallocate_with_length(start); -} - -void *operator new[](uint64_t size) { - return allocate_with_length(size); -} - -void *operator new(uint64_t size) { - return allocate_with_length(size); -} diff --git a/libraries/euler/cassert.cpp b/libraries/euler/cassert.cpp deleted file mode 100644 index e811b38..0000000 --- a/libraries/euler/cassert.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include - -namespace euler { - - [[noreturn]] void assert_failed( - const char *, const char *, int, const char * - ) { - //TODO: print error and abort - //we could just exit right now but i want to keep us in - //the application so we can get a stack trace in gdb. - while (1) - ; - } - - -} diff --git a/libraries/euler/cctype.cpp b/libraries/euler/cctype.cpp deleted file mode 100644 index 98234bb..0000000 --- a/libraries/euler/cctype.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -namespace std { - - int isspace(int ch) { - return - ch == ' ' || ch == '\n' || ch == '\t' || - ch == '\r' || ch == '\v' || ch == '\f'; - } - -} diff --git a/libraries/euler/cstdio.cpp b/libraries/euler/cstdio.cpp deleted file mode 100644 index 367e7e0..0000000 --- a/libraries/euler/cstdio.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include -#include -#include -#include - -namespace euler { - - //read-only with no error bits for now - struct file_t { - - syscall::file_handle handle; - - //TODO: variable size buffer? maybe a multiple of block size for device? - char buffer[1024]; - //in bytes, aligned to buffer size; 1 for no buffer loaded - uint64_t buffer_start; - - bool is_offset_in_buffer() { - return - buffer_start != 1 && offset >= buffer_start && - offset < buffer_start + 1024; - } - - syscall::file_result ensure_offset_in_buffer() { - if (is_offset_in_buffer()) - return syscall::file_result::success; - uint64_t new_buffer_start = (offset / 1024) * 1024; - uint64_t new_buffer_end = new_buffer_start + 1024; - if (length < new_buffer_end) - new_buffer_end = length; - syscall::file_result result = _syscall_read_from_file( - handle, new_buffer_start, new_buffer_end - new_buffer_start, buffer); - if (result == syscall::file_result::success) - buffer_start = new_buffer_start; - return result; - } - - uint64_t offset; - uint64_t length; - - }; - -} - -namespace std { - - FILE *fopen(const char *path, const char *mode) { - - assert(mode[0] == 'r' && mode[1] == '\0'); - - euler::syscall::file_handle handle; - euler::syscall::file_result result = - _syscall_open_file(path, strlen(path), handle); - if (result != euler::syscall::file_result::success) - return 0; - - uint64_t length; - result = _syscall_get_file_length(handle, length); - if (result != euler::syscall::file_result::success) { - _syscall_close_file(handle); - return 0; - } - - return new FILE { - .handle = handle, - .buffer = {}, - .buffer_start = 1, - .offset = 0, - .length = length - }; - - } - - int fclose(FILE *file) { - _syscall_close_file(file->handle); - delete file; - return 0; - } - - int fgetc(FILE *from) { - if (from->offset >= from->length) - return EOF; - assert( - from->ensure_offset_in_buffer() == euler::syscall::file_result::success); - char ch = from->buffer[from->offset - from->buffer_start]; - ++from->offset; - return ch; - } - - int ungetc(int ch, FILE *from) { - if (ch == EOF || from->offset == 0) - return EOF; - --from->offset; - if (!from->is_offset_in_buffer()) { - ++from->offset; - return EOF; - } - from->buffer[from->offset - from->buffer_start] = ch; - return ch; - } - -} diff --git a/libraries/euler/entry.cpp b/libraries/euler/entry.cpp deleted file mode 100644 index 0bbffda..0000000 --- a/libraries/euler/entry.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include - -int main(int argc, char **argv); - -extern "C" [[noreturn]] void _entry() { - - //TODO: static constructors - - //TODO: get command line via system call and populate argc and argv. - int argc = 0; - char **argv = 0; - - int result = main(argc, argv); - - //TODO: static destructors - - _syscall_end_this_process(result); - -} diff --git a/libraries/euler/include/cassert b/libraries/euler/include/cassert deleted file mode 100644 index bc716a0..0000000 --- a/libraries/euler/include/cassert +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -namespace euler { - [[noreturn]] void assert_failed( - const char *file, const char *function, int line, const char *condition); -} - -#ifdef NDEBUG -#define assert(condition) ((void)0) -#else -#define assert(condition) ((condition) ? ((void)0) : \ - euler::assert_failed(__FILE__, __func__, __LINE__, #condition)) -#endif diff --git a/libraries/euler/include/cctype b/libraries/euler/include/cctype deleted file mode 100644 index 087b191..0000000 --- a/libraries/euler/include/cctype +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -namespace std { - - int isspace(int ch); - -} diff --git a/libraries/euler/include/cstddef b/libraries/euler/include/cstddef deleted file mode 120000 index 9eac9b6..0000000 --- a/libraries/euler/include/cstddef +++ /dev/null @@ -1 +0,0 @@ -../../../mintsuki-freestanding-headers/stddef.h \ No newline at end of file diff --git a/libraries/euler/include/cstdint b/libraries/euler/include/cstdint deleted file mode 120000 index b087235..0000000 --- a/libraries/euler/include/cstdint +++ /dev/null @@ -1 +0,0 @@ -../../../mintsuki-freestanding-headers/stdint.h \ No newline at end of file diff --git a/libraries/euler/include/cstdio b/libraries/euler/include/cstdio deleted file mode 100644 index 3bb293b..0000000 --- a/libraries/euler/include/cstdio +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#define EOF (-1) - -namespace euler { - struct file_t; -} - -namespace std { - - typedef euler::file_t FILE; - - FILE *fopen(const char *path, const char *mode); - int fclose(FILE *file); - - int fgetc(FILE *from); - int ungetc(int ch, FILE *from); - -} diff --git a/libraries/euler/include/cstring b/libraries/euler/include/cstring deleted file mode 100644 index 65d00dc..0000000 --- a/libraries/euler/include/cstring +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include - -namespace std { - - static inline size_t strlen(const char *str) { - size_t i = 0; - while (str[i]) - ++i; - return i; - } - -} diff --git a/libraries/euler/include/euler/syscall.hpp b/libraries/euler/include/euler/syscall.hpp deleted file mode 100644 index 2dc88b8..0000000 --- a/libraries/euler/include/euler/syscall.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include - -namespace euler::syscall { - - typedef uint32_t encoded_color; - typedef int32_t exit_code; - typedef uint64_t file_handle; - typedef uint32_t key_packet; - - enum [[nodiscard]] file_result : uint64_t { - success, - bad_file_handle, - device_error, - file_system_corrupt, - out_of_bounds, - does_not_exist, - directory - }; - -} - -extern "C" { - - euler::syscall::encoded_color _syscall_encode_color( - uint8_t red, uint8_t green, uint8_t blue); - - void _syscall_get_framebuffer( - euler::syscall::encoded_color *&ptr_out, uint32_t &width_out, - uint32_t &height_out, uint32_t &pitch_out); - - euler::syscall::file_result _syscall_open_file( - const char *path, uint64_t path_length, euler::syscall::file_handle &out); - - euler::syscall::file_result _syscall_get_file_length( - euler::syscall::file_handle file, uint64_t &out); - - euler::syscall::file_result _syscall_read_from_file( - euler::syscall::file_handle file, - uint64_t start_offset, uint64_t length, void *into); - - [[noreturn]] void _syscall_end_this_process(euler::syscall::exit_code code); - - [[nodiscard]] void *_syscall_get_new_pages(uint64_t count); - - void _syscall_close_file(euler::syscall::file_handle file); - - euler::syscall::key_packet _syscall_read_key_packet(); - -} diff --git a/libraries/euler/syscall.asm b/libraries/euler/syscall.asm deleted file mode 100644 index c76a641..0000000 --- a/libraries/euler/syscall.asm +++ /dev/null @@ -1,88 +0,0 @@ -bits 64 - -section .text - -global _syscall_encode_color -_syscall_encode_color: - xor rax, rax - and edi, 0xff - and dx, 0xff - shl si, 8 - shl edx, 16 - or di, si - or edi, edx - syscall - ret - -global _syscall_get_framebuffer -_syscall_get_framebuffer: - push rcx - push rdx - push rsi - push rdi - mov rax, 1 - syscall - pop rcx - mov qword [rcx], rax - pop rcx - mov dword [rcx], edi - pop rcx - shr rdi, 32 - mov dword [rcx], edi - pop rcx - mov dword [rcx], esi - ret - -global _syscall_open_file -_syscall_open_file: - mov rax, 2 - push rdx - syscall - pop rdx - mov qword [rdx], rdi - ret - -global _syscall_get_file_length -_syscall_get_file_length: - mov rax, 3 - push rsi - syscall - pop rsi - mov qword [rsi], rdi - ret - -global _syscall_read_from_file -_syscall_read_from_file: - mov rax, 4 - push rcx - push rdx - push rsi - push rdi - mov rdi, rsp - syscall - add rsp, 32 - ret - -global _syscall_end_this_process -_syscall_end_this_process: - mov rax, 5 - syscall - ;does not return - -global _syscall_get_new_pages -_syscall_get_new_pages: - mov rax, 6 - syscall - ret - -global _syscall_close_file -_syscall_close_file: - mov rax, 7 - syscall - ret - -global _syscall_read_key_packet -_syscall_read_key_packet: - mov rax, 8 - syscall - ret -- cgit v1.2.3