Redesign syscalls
All checks were successful
Build documentation / build-and-deploy (push) Successful in 40s

This commit is contained in:
2026-01-20 20:46:34 +01:00
parent a29233f853
commit fff51321bc
15 changed files with 198 additions and 357 deletions

View File

@@ -2,37 +2,29 @@
#pragma clang optimize off
#include <alloc/liballoc.h>
#include <m/proc.h>
#include <m/resource_buffer.h>
#include <m/system.h>
static int liballoc_mutex;
static uintptr_t liballoc_map_base = PROC_MAP_BASE;
static int mem_rid_base = 1000000;
void liballoc_init (void) {
liballoc_mutex = proc_create_resource (100, PR_MUTEX, RV_PRIVATE, NULL);
}
void liballoc_init (void) {}
int liballoc_lock (void) {
proc_mutex_lock (liballoc_mutex, RV_PRIVATE);
return 0;
}
int liballoc_lock (void) { return 0; }
int liballoc_unlock (void) {
proc_mutex_unlock (liballoc_mutex, RV_PRIVATE);
return 0;
}
int liballoc_unlock (void) { return 0; }
void* liballoc_alloc (int pages) {
void* liballoc_alloc (int pages, int* mem_rid) {
uintptr_t current_base = liballoc_map_base;
struct resource_buffer mem_init = {.type = PR_MEM, .u = {.mem = {.pages = (size_t)pages}}};
int mem_rid = proc_create_resource (mem_rid_base++, PR_MEM, RV_PRIVATE, &mem_init);
if (mem_rid < 0)
*mem_rid = create_mem (mem_rid_base++, RV_PRIVATE, pages);
if (*mem_rid < 0) {
return NULL;
}
proc_map (mem_init.u.mem.paddr, current_base, (size_t)pages, PM_PRESENT | PM_RW | PM_USER);
if (map (*mem_rid, RV_PRIVATE, current_base, MAP_FLAGS | MAP_RW) < 0) {
unlink_mem (*mem_rid, RV_PRIVATE, pages);
return NULL;
}
uintptr_t old_base = current_base;
current_base += pages * PAGE_SIZE;
@@ -40,7 +32,11 @@ void* liballoc_alloc (int pages) {
return (void*)old_base;
}
int liballoc_free (void* ptr, int pages) { return 0; }
int liballoc_free (void* ptr, int pages, int mem_rid) {
unmap ((uintptr_t)ptr, pages);
unlink_mem (mem_rid, RV_PRIVATE, pages);
return 0;
}
/** Durand's Ridiculously Amazing Super Duper Memory functions. */
@@ -207,6 +203,7 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
unsigned int pages;
unsigned int usage;
struct boundary_tag* tag;
int mem_rid;
// This is how much space is required.
usage = size + sizeof (struct boundary_tag);
@@ -220,7 +217,7 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
if (pages < (unsigned int)l_pageCount)
pages = l_pageCount;
tag = (struct boundary_tag*)liballoc_alloc (pages);
tag = (struct boundary_tag*)liballoc_alloc (pages, &mem_rid);
if (tag == NULL)
return NULL; // uh oh, we ran out of memory.
@@ -229,6 +226,7 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
tag->size = size;
tag->real_size = pages * l_pageSize;
tag->index = -1;
tag->mem_rid = mem_rid;
tag->next = NULL;
tag->prev = NULL;
@@ -351,7 +349,7 @@ void free (void* ptr) {
if (pages < (unsigned int)l_pageCount)
pages = l_pageCount;
liballoc_free (tag, pages);
liballoc_free (tag, pages, tag->mem_rid);
liballoc_unlock ();
return;

View File

@@ -41,6 +41,8 @@ struct boundary_tag {
struct boundary_tag* next; //< Linked list info.
struct boundary_tag* prev; //< Linked list info.
int mem_rid;
};
/** This function is supposed to lock the memory data structures. It
@@ -67,7 +69,7 @@ extern int liballoc_unlock (void);
* \return NULL if the pages were not allocated.
* \return A pointer to the allocated memory.
*/
extern void* liballoc_alloc (int);
extern void* liballoc_alloc (int pages, int* mem_rid);
/** This frees previously allocated memory. The void* parameter passed
* to the function is the exact same value returned from a previous
@@ -77,7 +79,7 @@ extern void* liballoc_alloc (int);
*
* \return 0 if the memory was successfully freed.
*/
extern int liballoc_free (void*, int);
extern int liballoc_free (void* ptr, int pages, int mem_rid);
void* malloc (size_t); //< The standard function.
void* realloc (void*, size_t); //< The standard function.

View File

@@ -1,5 +1,5 @@
#include <alloc/liballoc.h>
#include <m/proc.h>
#include <m/system.h>
#include <stdint.h>
extern volatile uint8_t __bss_start[];
@@ -20,5 +20,5 @@ void __premain (void) {
app_main ();
proc_quit ();
quit ();
}

View File

@@ -1,44 +0,0 @@
#include <m/resource_buffer.h>
#include <m/syscall.h>
#include <m/syscall_defs.h>
#include <stddef.h>
#include <stdint.h>
int proc_quit (void) { return syscall (SYS_PROC_QUIT, 0, 0, 0, 0, 0, 0); }
int proc_test (char c) { return syscall (SYS_PROC_TEST, (uintptr_t)c, 0, 0, 0, 0, 0); }
int proc_map (uintptr_t paddr, uintptr_t vaddr, size_t pages, uint32_t flags) {
return syscall (SYS_PROC_MAP, paddr, vaddr, (uintptr_t)pages, (uintptr_t)flags, 0, 0);
}
int proc_unmap (uintptr_t vaddr, size_t pages) {
return syscall (SYS_PROC_UNMAP, vaddr, (uintptr_t)pages, 0, 0, 0, 0);
}
int proc_create_resource (int rid, int type, int vis, void* buffer) {
return syscall (SYS_PROC_CREATE_RESOURCE, (uintptr_t)rid, (uintptr_t)type, (uintptr_t)vis,
(uintptr_t)buffer, 0, 0);
}
int proc_drop_resource (int rid, int vis) {
return syscall (SYS_PROC_DROP_RESOURCE, (uintptr_t)rid, (uintptr_t)vis, 0, 0, 0, 0);
}
int proc_mutex_lock (int mutex_rid, int vis) {
return syscall (SYS_PROC_MUTEX_LOCK, (uintptr_t)mutex_rid, (uintptr_t)vis, 0, 0, 0, 0);
}
int proc_mutex_unlock (int mutex_rid, int vis) {
return syscall (SYS_PROC_MUTEX_UNLOCK, (uintptr_t)mutex_rid, (uintptr_t)vis, 0, 0, 0, 0);
}
int proc_clone (uintptr_t vstack_top, size_t stack_size, void* entry) {
return syscall (SYS_PROC_CLONE, vstack_top, (uintptr_t)stack_size, (uintptr_t)entry, 0, 0, 0);
}
int proc_translate_v2p (uintptr_t vaddr, uintptr_t* out_paddr) {
return syscall (SYS_PROC_TRANSLATE_V2P, vaddr, (uintptr_t)out_paddr, 0, 0, 0, 0);
}
int proc_sched (void) { return syscall (SYS_PROC_SCHED, 0, 0, 0, 0, 0, 0); }

View File

@@ -1,34 +0,0 @@
#ifndef _LIBMSL_M_PROC_H
#define _LIBMSL_M_PROC_H
#if defined(__x86_64__)
#define PROC_MAP_BASE 0x0000700000000000
#define PAGE_SIZE 4096
#endif
#define PM_PRESENT (1 << 0)
#define PM_RW (1 << 1)
#define PM_USER (1 << 2)
#define RV_PRIVATE 0
#define RV_PUBLIC 1
#define PR_MEM 0
#define PR_MUTEX 1
#include <stddef.h>
#include <stdint.h>
int proc_quit (void);
int proc_test (char c);
int proc_map (uintptr_t paddr, uintptr_t vaddr, size_t pages, uint32_t flags);
int proc_unmap (uintptr_t vaddr, size_t pages);
int proc_create_resource (int rid, int type, int vis, void* buffer);
int proc_drop_resource (int rid, int vis);
int proc_mutex_lock (int mutex_rid, int vis);
int proc_mutex_unlock (int mutex_rid, int vis);
int proc_clone (uintptr_t vstack_top, size_t stack_size, void* entry);
int proc_sched (void);
int proc_translate_v2p (uintptr_t vaddr, uintptr_t* out_paddr);
#endif // _LIBMSL_M_PROC_H

View File

@@ -1,3 +1,3 @@
c += m/proc.c
c += m/system.c
o += m/proc.o
o += m/system.o

32
libmsl/m/system.c Normal file
View File

@@ -0,0 +1,32 @@
#include <m/syscall.h>
#include <m/system.h>
#include <stddef.h>
#include <stdint.h>
#define do_syscall1(id, a1, a2, a3, a4, a5, a6, ...) \
syscall (id, (uintptr_t)a1, (uintptr_t)a2, (uintptr_t)a3, (uintptr_t)a4, (uintptr_t)a5, \
(uintptr_t)a6)
#define do_syscall(...) do_syscall1 (__VA_ARGS__, 0, 0, 0, 0, 0, 0)
int quit (void) { return do_syscall (SYS_QUIT); }
int test (char c) { return do_syscall (SYS_TEST, c); }
int map (int mem_rid, int vis, uintptr_t vaddr, uint32_t flags) {
return do_syscall (SYS_MAP, mem_rid, vis, vaddr, flags);
}
int unmap (uintptr_t vaddr, size_t pages) { return do_syscall (SYS_UNMAP, vaddr, pages); }
int create_mem (int mem_rid, int vis, size_t pages) {
return do_syscall (SYS_CREATE_MEM, mem_rid, vis, pages);
}
int unlink_mem (int mem_rid, int vis, size_t pages) {
return do_syscall (SYS_UNLINK_MEM, mem_rid, vis, pages);
}
int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void)) {
return do_syscall (SYS_CLONE, vstack_top, stack_size, entry);
}

28
libmsl/m/system.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef _LIBMSL_M_SYSTEM_H
#define _LIBMSL_M_SYSTEM_H
#include <stddef.h>
#include <stdint.h>
#if defined(__x86_64__)
#define PROC_MAP_BASE 0x0000700000000000
#define PAGE_SIZE 4096
#endif
#define MAP_PRESENT (1 << 0)
#define MAP_RW (1 << 1)
#define MAP_USER (1 << 2)
#define MAP_FLAGS (MAP_PRESENT | MAP_USER)
#define RV_PRIVATE 0
#define RV_PUBLIC 1
int quit (void);
int test (char c);
int map (int mem_rid, int vis, uintptr_t vaddr, uint32_t flags);
int unmap (uintptr_t vaddr, size_t pages);
int create_mem (int mem_rid, int vis, size_t pages);
int unlink_mem (int mem_rid, int vis, size_t pages);
int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void));
#endif // _LIBMSL_M_SYSTEM_H