feat: expand user mode, add DOOM game, add manpages

This commit is contained in:
2026-02-18 15:13:53 +01:00
parent 605fbcbe42
commit 24af60d669
51 changed files with 4484 additions and 43 deletions
+133
View File
@@ -0,0 +1,133 @@
/*
* heap.h
* Userspace heap allocator for ZenithOS programs
* Free-list allocator backed by SYS_ALLOC page requests.
* Adapted from the kernel HeapAllocator.
* Copyright (c) 2025 Daniel Hammer
*/
#pragma once
#include <zenith/syscall.h>
namespace zenith {
namespace heap_detail {
static constexpr uint64_t HEADER_MAGIC = 0x5A484541; // "ZHEA"
struct Header {
uint64_t magic;
uint64_t size; // user-requested size
} __attribute__((packed));
struct FreeNode {
uint64_t size; // total size of this free block (including node)
FreeNode* next;
};
// Per-process heap state (single TU per program, so static is fine)
static FreeNode g_head{0, nullptr};
static bool g_initialized = false;
static inline Header* get_header(void* block) {
return (Header*)((uint8_t*)block - sizeof(Header));
}
static inline void insert_free(void* ptr, uint64_t size) {
auto* node = (FreeNode*)ptr;
node->size = size;
node->next = g_head.next;
g_head.next = node;
}
static inline void grow(uint64_t bytes) {
uint64_t pages = (bytes + 0xFFF) / 0x1000;
if (pages < 4) pages = 4; // grow at least 16 KiB at a time
void* mem = zenith::alloc(pages * 0x1000);
if (mem != nullptr)
insert_free(mem, pages * 0x1000);
}
} // namespace heap_detail
// ---- Public API ----
inline void* malloc(uint64_t size) {
using namespace heap_detail;
if (!g_initialized) {
grow(16 * 0x1000); // seed with 64 KiB
g_initialized = true;
}
uint64_t needed = size + sizeof(Header);
needed = (needed + 15) & ~15ULL; // 16-byte alignment
FreeNode* prev = &g_head;
FreeNode* current = g_head.next;
while (current != nullptr) {
if (current->size >= needed) {
uint64_t blockSize = current->size;
// Unlink
prev->next = current->next;
// Split if worthwhile
if (blockSize > needed + sizeof(FreeNode) + 16) {
void* rest = (void*)((uint8_t*)current + needed);
uint64_t restSize = blockSize - needed;
insert_free(rest, restSize);
}
// Write allocation header
Header* header = (Header*)current;
header->magic = HEADER_MAGIC;
header->size = size;
return (void*)((uint8_t*)header + sizeof(Header));
}
prev = current;
current = current->next;
}
// No fit — grow and retry
grow(needed);
return malloc(size);
}
inline void mfree(void* ptr) {
using namespace heap_detail;
if (ptr == nullptr) return;
Header* header = get_header(ptr);
uint64_t blockSize = header->size + sizeof(Header);
blockSize = (blockSize + 15) & ~15ULL;
insert_free((void*)header, blockSize);
}
inline void* realloc(void* ptr, uint64_t size) {
if (ptr == nullptr) return malloc(size);
auto* header = heap_detail::get_header(ptr);
uint64_t old = header->size;
void* newBlock = malloc(size);
if (newBlock == nullptr) return nullptr;
// Copy the smaller of old/new sizes
uint64_t copySize = (old < size) ? old : size;
uint8_t* dst = (uint8_t*)newBlock;
uint8_t* src = (uint8_t*)ptr;
for (uint64_t i = 0; i < copySize; i++)
dst[i] = src[i];
mfree(ptr);
return newBlock;
}
} // namespace zenith
+22
View File
@@ -117,6 +117,9 @@ namespace zenith {
inline void yield() { syscall0(Zenith::SYS_YIELD); }
inline void sleep_ms(uint64_t ms) { syscall1(Zenith::SYS_SLEEP_MS, ms); }
inline int getpid() { return (int)syscall0(Zenith::SYS_GETPID); }
inline int spawn(const char* path, const char* args = nullptr) {
return (int)syscall2(Zenith::SYS_SPAWN, (uint64_t)path, (uint64_t)args);
}
// Console
inline void print(const char* text) { syscall1(Zenith::SYS_PRINT, (uint64_t)text); }
@@ -154,4 +157,23 @@ namespace zenith {
return (int32_t)syscall2(Zenith::SYS_PING, (uint64_t)ip, (uint64_t)timeoutMs);
}
// Process management
inline void waitpid(int pid) { syscall1(Zenith::SYS_WAITPID, (uint64_t)pid); }
// Framebuffer
inline void fb_info(Zenith::FbInfo* info) { syscall1(Zenith::SYS_FBINFO, (uint64_t)info); }
inline void* fb_map() { return (void*)syscall0(Zenith::SYS_FBMAP); }
// Arguments
inline int getargs(char* buf, uint64_t maxLen) {
return (int)syscall2(Zenith::SYS_GETARGS, (uint64_t)buf, maxLen);
}
// Terminal
inline void termsize(int* cols, int* rows) {
uint64_t r = (uint64_t)syscall0(Zenith::SYS_TERMSIZE);
if (cols) *cols = (int)(r & 0xFFFFFFFF);
if (rows) *rows = (int)(r >> 32);
}
}