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
+14
View File
@@ -31,6 +31,20 @@ namespace Zenith {
static constexpr uint64_t SYS_GETKEY = 17;
static constexpr uint64_t SYS_GETCHAR = 18;
static constexpr uint64_t SYS_PING = 19;
static constexpr uint64_t SYS_SPAWN = 20;
static constexpr uint64_t SYS_FBINFO = 21;
static constexpr uint64_t SYS_FBMAP = 22;
static constexpr uint64_t SYS_WAITPID = 23;
static constexpr uint64_t SYS_TERMSIZE = 24;
static constexpr uint64_t SYS_GETARGS = 25;
struct FbInfo {
uint64_t width;
uint64_t height;
uint64_t pitch; // bytes per scanline
uint64_t bpp; // bits per pixel (32)
uint64_t userAddr; // filled by SYS_FBMAP (0 until mapped)
};
struct SysInfo {
char osName[32];
+23
View File
@@ -0,0 +1,23 @@
#ifndef _LIBC_ASSERT_H
#define _LIBC_ASSERT_H
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void __assert_fail(const char *expr, const char *file, int line, const char *func);
#ifdef NDEBUG
#define assert(expr) ((void)0)
#else
#define assert(expr) \
((expr) ? (void)0 : __assert_fail(#expr, __FILE__, __LINE__, __func__))
#endif
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_ASSERT_H */
+28
View File
@@ -0,0 +1,28 @@
#ifndef _LIBC_CTYPE_H
#define _LIBC_CTYPE_H
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
int isalpha(int c);
int isdigit(int c);
int isalnum(int c);
int isspace(int c);
int isupper(int c);
int islower(int c);
int isprint(int c);
int ispunct(int c);
int isxdigit(int c);
int iscntrl(int c);
int isgraph(int c);
int toupper(int c);
int tolower(int c);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_CTYPE_H */
+29
View File
@@ -0,0 +1,29 @@
#ifndef _LIBC_ERRNO_H
#define _LIBC_ERRNO_H
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
extern int errno;
#define ENOENT 2
#define EIO 5
#define ENOMEM 12
#define EACCES 13
#define EINVAL 22
#define ERANGE 34
#define ENOSYS 38
#define EISDIR 21
#define ENOTDIR 20
#define EEXIST 17
#define EBADF 9
#define EPERM 1
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_ERRNO_H */
+22
View File
@@ -0,0 +1,22 @@
#ifndef _LIBC_FCNTL_H
#define _LIBC_FCNTL_H
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#define O_CREAT 0x40
#define O_TRUNC 0x200
int open(const char *path, int flags, ...);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_FCNTL_H */
+27
View File
@@ -0,0 +1,27 @@
#ifndef _LIBC_INTTYPES_H
#define _LIBC_INTTYPES_H
#include <stdint.h>
#define PRId8 "d"
#define PRId16 "d"
#define PRId32 "d"
#define PRId64 "ld"
#define PRIi8 "i"
#define PRIi16 "i"
#define PRIi32 "i"
#define PRIi64 "li"
#define PRIu8 "u"
#define PRIu16 "u"
#define PRIu32 "u"
#define PRIu64 "lu"
#define PRIx8 "x"
#define PRIx16 "x"
#define PRIx32 "x"
#define PRIx64 "lx"
#define PRIX8 "X"
#define PRIX16 "X"
#define PRIX32 "X"
#define PRIX64 "lX"
#endif /* _LIBC_INTTYPES_H */
+31
View File
@@ -0,0 +1,31 @@
#ifndef _LIBC_LIMITS_H
#define _LIBC_LIMITS_H
#pragma once
#define CHAR_BIT 8
#define SCHAR_MIN (-128)
#define SCHAR_MAX 127
#define UCHAR_MAX 255
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 65535
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 4294967295U
#define LONG_MIN (-9223372036854775807L - 1)
#define LONG_MAX 9223372036854775807L
#define ULONG_MAX 18446744073709551615UL
#define LLONG_MIN (-9223372036854775807LL - 1)
#define LLONG_MAX 9223372036854775807LL
#define ULLONG_MAX 18446744073709551615ULL
#define PATH_MAX 4096
#define NAME_MAX 256
#endif /* _LIBC_LIMITS_H */
+31
View File
@@ -0,0 +1,31 @@
#ifndef _LIBC_MATH_H
#define _LIBC_MATH_H
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#define HUGE_VAL __builtin_huge_val()
#define INFINITY __builtin_inff()
#define NAN __builtin_nanf("")
double fabs(double x);
double floor(double x);
double ceil(double x);
double sqrt(double x);
double sin(double x);
double cos(double x);
double atan2(double y, double x);
double pow(double base, double exp);
double log(double x);
double exp(double x);
double fmod(double x, double y);
double round(double x);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_MATH_H */
+77
View File
@@ -0,0 +1,77 @@
#ifndef _LIBC_STDIO_H
#define _LIBC_STDIO_H
#pragma once
#include <stdarg.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#define EOF (-1)
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define BUFSIZ 1024
#define FILENAME_MAX 256
typedef struct _FILE {
int handle;
unsigned long pos;
unsigned long size;
int eof;
int error;
int is_std;
int ungetc_buf;
} FILE;
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
int printf(const char *fmt, ...);
int fprintf(FILE *stream, const char *fmt, ...);
int sprintf(char *str, const char *fmt, ...);
int snprintf(char *str, size_t size, const char *fmt, ...);
int vprintf(const char *fmt, va_list ap);
int vfprintf(FILE *stream, const char *fmt, va_list ap);
int vsprintf(char *str, const char *fmt, va_list ap);
int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
int puts(const char *s);
int putchar(int c);
FILE *fopen(const char *path, const char *mode);
int fclose(FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
int fflush(FILE *stream);
int rename(const char *oldpath, const char *newpath);
int remove(const char *path);
int sscanf(const char *str, const char *fmt, ...);
int feof(FILE *stream);
int ferror(FILE *stream);
void clearerr(FILE *stream);
int fgetc(FILE *stream);
int getc(FILE *stream);
int ungetc(int c, FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int fputs(const char *s, FILE *stream);
void perror(const char *s);
FILE *tmpfile(void);
char *tmpnam(char *s);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_STDIO_H */
+66
View File
@@ -0,0 +1,66 @@
#ifndef _LIBC_STDLIB_H
#define _LIBC_STDLIB_H
#pragma once
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef NULL
#define NULL ((void *)0)
#endif
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#define RAND_MAX 0x7fffffff
typedef struct {
int quot;
int rem;
} div_t;
typedef struct {
long quot;
long rem;
} ldiv_t;
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
int atoi(const char *s);
long atol(const char *s);
double atof(const char *s);
int abs(int j);
long labs(long j);
void exit(int status);
void abort(void);
int atexit(void (*func)(void));
char *getenv(const char *name);
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
long strtol(const char *nptr, char **endptr, int base);
unsigned long strtoul(const char *nptr, char **endptr, int base);
int rand(void);
void srand(unsigned int seed);
div_t div(int numer, int denom);
ldiv_t ldiv(long numer, long denom);
int system(const char *command);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_STDLIB_H */
+35
View File
@@ -0,0 +1,35 @@
#ifndef _LIBC_STRING_H
#define _LIBC_STRING_H
#pragma once
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
void *memcpy(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
void *memmove(void *dest, const void *src, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
size_t strlen(const char *s);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
char *strdup(const char *s);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
int strcasecmp(const char *s1, const char *s2);
int strncasecmp(const char *s1, const char *s2, size_t n);
char *strstr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_STRING_H */
+17
View File
@@ -0,0 +1,17 @@
#ifndef _LIBC_STRINGS_H
#define _LIBC_STRINGS_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
int strcasecmp(const char *s1, const char *s2);
int strncasecmp(const char *s1, const char *s2, size_t n);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_STRINGS_H */
+24
View File
@@ -0,0 +1,24 @@
#ifndef _LIBC_SYS_STAT_H
#define _LIBC_SYS_STAT_H
#pragma once
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
struct stat {
unsigned long st_size;
};
int mkdir(const char *path, unsigned int mode);
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_SYS_STAT_H */
+24
View File
@@ -0,0 +1,24 @@
#ifndef _LIBC_SYS_TIME_H
#define _LIBC_SYS_TIME_H
/* Stub header for ZenithOS */
#ifdef __cplusplus
extern "C" {
#endif
struct timeval {
long tv_sec;
long tv_usec;
};
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_SYS_TIME_H */
+23
View File
@@ -0,0 +1,23 @@
#ifndef _LIBC_SYS_TYPES_H
#define _LIBC_SYS_TYPES_H
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned long size_t;
typedef long ssize_t;
typedef long off_t;
typedef int pid_t;
typedef unsigned int mode_t;
typedef unsigned int uid_t;
typedef unsigned int gid_t;
typedef long time_t;
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_SYS_TYPES_H */
+20
View File
@@ -0,0 +1,20 @@
#ifndef _LIBC_UNISTD_H
#define _LIBC_UNISTD_H
#pragma once
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
int read(int fd, void *buf, size_t count);
int write(int fd, const void *buf, size_t count);
int close(int fd);
#ifdef __cplusplus
}
#endif
#endif /* _LIBC_UNISTD_H */
+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);
}
}