Porting of apps to userspace

This commit is contained in:
boreddevnl
2026-02-26 20:07:21 +01:00
parent c2ead0d6a7
commit 786eac0345
84 changed files with 2887 additions and 2392 deletions

View File

@@ -20,10 +20,15 @@
#define GUI_EVENT_CLOSE 4
#define GUI_EVENT_KEY 5
#define GUI_EVENT_MOUSE_DOWN 6
#define GUI_EVENT_MOUSE_UP 7
#define GUI_EVENT_MOUSE_MOVE 8
typedef struct {
int type;
int arg1; // For click: x
int arg2; // For click: y
int arg3; // For click: button state
} gui_event_t;
// Window Handle

Binary file not shown.

View File

@@ -0,0 +1,140 @@
#include "stdlib.h"
#include "syscall.h"
// Simple block allocator over sys_sbrk
typedef struct BlockMeta {
size_t size;
int free;
struct BlockMeta *next;
} BlockMeta;
#define META_SIZE sizeof(BlockMeta)
static void *global_base = NULL;
static BlockMeta *find_free_block(BlockMeta **last, size_t size) {
BlockMeta *current = global_base;
while (current && !(current->free && current->size >= size)) {
*last = current;
current = current->next;
}
return current;
}
static BlockMeta *request_space(BlockMeta* last, size_t size) {
BlockMeta *block;
block = (BlockMeta *)sys_sbrk(0);
// Ask for space, ensuring everything stays 8-byte aligned if sizes are odd.
// For simplicity, we just request exactly what is needed,
// but typically `size` should be aligned.
size_t align = 8;
if (size % align != 0) {
size += align - (size % align);
}
void *request = sys_sbrk(size + META_SIZE);
if (request == (void*)-1) {
return NULL; // sbrk failed
}
if (last) { // NULL on first request
last->next = block;
}
block->size = size;
block->next = NULL;
block->free = 0;
return block;
}
void *malloc(size_t size) {
BlockMeta *block;
if (size <= 0) {
return NULL;
}
// Align size to 8 bytes for safety
size_t align = 8;
if (size % align != 0) {
size += align - (size % align);
}
if (!global_base) { // First call
block = request_space(NULL, size);
if (!block) return NULL;
global_base = block;
} else {
BlockMeta *last = global_base;
block = find_free_block(&last, size);
if (!block) { // Failed to find free block
block = request_space(last, size);
if (!block) return NULL;
} else { // Found free block
block->free = 0;
// We could split the block here if block->size is much larger than size...
}
}
return (block + 1);
}
void free(void *ptr) {
if (!ptr) {
return;
}
BlockMeta *block = (BlockMeta*)ptr - 1;
block->free = 1;
}
void *calloc(size_t nelem, size_t elsize) {
size_t size = nelem * elsize;
void *ptr = malloc(size);
if (ptr) {
char *p = ptr;
for (size_t i = 0; i < size; i++) {
p[i] = 0;
}
}
return ptr;
}
void *realloc(void *ptr, size_t size) {
if (!ptr) {
return malloc(size);
}
if (size == 0) {
free(ptr);
return NULL;
}
BlockMeta *block = (BlockMeta*)ptr - 1;
if (block->size >= size) {
return ptr;
}
void *new_ptr = malloc(size);
if (!new_ptr) {
return NULL;
}
char *src = ptr;
char *dst = new_ptr;
for (size_t i = 0; i < block->size; i++) {
dst[i] = src[i];
}
free(ptr);
return new_ptr;
}
void *memset(void *s, int c, size_t n) {
unsigned char *p = s;
while (n--) *p++ = (unsigned char)c;
return s;
}
void *memcpy(void *dest, const void *src, size_t n) {
unsigned char *d = dest;
const unsigned char *s = src;
while (n--) *d++ = *s++;
return dest;
}

View File

@@ -0,0 +1,14 @@
#ifndef STDLIB_H
#define STDLIB_H
#include <stddef.h>
#include <stdint.h>
void* malloc(size_t size);
void free(void* ptr);
void* calloc(size_t nmemb, size_t size);
void* realloc(void* ptr, size_t size);
void *memset(void *s, int c, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
#endif

Binary file not shown.

View File

@@ -70,6 +70,14 @@ int sys_write(int fd, const char *buf, int len) {
return (int)syscall3(SYS_WRITE, (uint64_t)fd, (uint64_t)buf, (uint64_t)len);
}
void *sys_sbrk(int incr) {
return (void *)syscall1(SYS_SBRK, (uint64_t)incr);
}
int sys_system(int cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) {
return (int)syscall5(SYS_SYSTEM, (uint64_t)cmd, arg1, arg2, arg3, arg4);
}
int sys_open(const char *path, const char *mode) {
return (int)syscall3(SYS_FS, FS_CMD_OPEN, (uint64_t)path, (uint64_t)mode);
}

View File

@@ -8,6 +8,8 @@
#define SYS_WRITE 1
#define SYS_GUI 3
#define SYS_FS 4
#define SYS_SYSTEM 5
#define SYS_SBRK 9
// FS Commands
#define FS_CMD_OPEN 1
@@ -33,6 +35,8 @@ extern uint64_t syscall5(uint64_t sys_num, uint64_t arg1, uint64_t arg2, uint64_
// Public API
void sys_exit(int status);
int sys_write(int fd, const char *buf, int len);
void *sys_sbrk(int incr);
int sys_system(int cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4);
// FS API
int sys_open(const char *path, const char *mode);

Binary file not shown.