From dcc05f2741f62ef9b942adfa8ee88538756c0631 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 7 Jun 2023 10:33:10 -0700 Subject: [PATCH] Add a libc skeleton with a primitive malloc. --- lib/CMakeLists.txt | 1 + lib/libc/CMakeLists.txt | 15 +++++++++++++ lib/libc/include/stddef.h | 5 +++++ lib/libc/include/stdlib.h | 5 +++++ lib/libc/src/malloc.cpp | 44 +++++++++++++++++++++++++++++++++++++++ sys/CMakeLists.txt | 1 + sys/test2.cpp | 3 ++- 7 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 lib/libc/CMakeLists.txt create mode 100644 lib/libc/include/stddef.h create mode 100644 lib/libc/include/stdlib.h create mode 100644 lib/libc/src/malloc.cpp diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index a9f2ea1..e727efa 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(libc) add_subdirectory(mammoth) diff --git a/lib/libc/CMakeLists.txt b/lib/libc/CMakeLists.txt new file mode 100644 index 0000000..7188b73 --- /dev/null +++ b/lib/libc/CMakeLists.txt @@ -0,0 +1,15 @@ + +add_library(libc STATIC + src/malloc.cpp + ) + +target_include_directories(libc + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +target_link_libraries(libc + zion_lib + ) + +set_target_properties(libc PROPERTIES + COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -ffreestanding -mgeneral-regs-only") diff --git a/lib/libc/include/stddef.h b/lib/libc/include/stddef.h new file mode 100644 index 0000000..92a84ac --- /dev/null +++ b/lib/libc/include/stddef.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +typedef uint64_t size_t; diff --git a/lib/libc/include/stdlib.h b/lib/libc/include/stdlib.h new file mode 100644 index 0000000..3634d1f --- /dev/null +++ b/lib/libc/include/stdlib.h @@ -0,0 +1,5 @@ +#pragma once + +#include "stddef.h" + +void* malloc(size_t size); diff --git a/lib/libc/src/malloc.cpp b/lib/libc/src/malloc.cpp new file mode 100644 index 0000000..fc8393c --- /dev/null +++ b/lib/libc/src/malloc.cpp @@ -0,0 +1,44 @@ +#include + +#include "stdlib.h" + +namespace { +class NaiveAllocator { + public: + constexpr static uint64_t kSize = 0x4000; + NaiveAllocator() {} + bool is_init() { return next_addr_ != 0; } + void Init() { + uint64_t vmmo_cap; + uint64_t err = ZMemoryObjectCreate(kSize, &vmmo_cap); + if (err != 0) { + ZProcessExit(err); + } + err = ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, vmmo_cap, &next_addr_); + max_addr_ = next_addr_ + kSize; + } + + void* Allocate(size_t size) { + uint64_t addr = next_addr_; + next_addr_ += size; + if (next_addr_ >= max_addr_) { + return 0; + } + return reinterpret_cast(addr); + } + + private: + uint64_t next_addr_ = 0; + uint64_t max_addr_ = 0; +}; + +NaiveAllocator gAlloc; + +} // namespace + +void* malloc(size_t size) { + if (!gAlloc.is_init()) { + gAlloc.Init(); + } + return gAlloc.Allocate(size); +} diff --git a/sys/CMakeLists.txt b/sys/CMakeLists.txt index 9a6c0af..adb8d91 100644 --- a/sys/CMakeLists.txt +++ b/sys/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable(test2 test2.cpp) target_link_libraries(test2 + libc mammoth_lib) set_target_properties(test2 diff --git a/sys/test2.cpp b/sys/test2.cpp index 45d3616..d2c8398 100644 --- a/sys/test2.cpp +++ b/sys/test2.cpp @@ -1,6 +1,7 @@ #include #include #include +#include void thread_entry(void* a) { dbgln("In thread"); @@ -18,7 +19,7 @@ int main(uint64_t bootstrap_cap) { Thread t2(thread_entry, b); uint64_t size = 10; - char buff[10]; + char* buff = (char*)malloc(size); Channel c1; c1.adopt_cap(bootstrap_cap); check(c1.ReadStr(buff, &size));