Redesign userspace memory management
All checks were successful
Build documentation / build-and-deploy (push) Successful in 44s

This commit is contained in:
2026-01-27 17:04:08 +01:00
parent 600886a7ee
commit b388b30b24
23 changed files with 195 additions and 484 deletions

View File

@@ -6,8 +6,6 @@
#define LIBALLOC_MUTEX 500
static uintptr_t liballoc_map_base = PROC_MAP_BASE;
static int mem_rid_base = 1000000;
static int liballoc_mutex;
void liballoc_init (void) { liballoc_mutex = create_mutex (LIBALLOC_MUTEX); }
@@ -18,30 +16,9 @@ int liballoc_lock (void) { return lock_mutex (liballoc_mutex); }
int liballoc_unlock (void) { return unlock_mutex (liballoc_mutex); }
void* liballoc_alloc (int pages, int* mem_rid) {
uintptr_t current_base = liballoc_map_base;
void* liballoc_alloc (int pages) { return map (0, pages, MAP_FLAGS | MAP_RW); }
*mem_rid = create_mem (mem_rid_base++, pages);
if (*mem_rid < 0) {
return NULL;
}
if (map (*mem_rid, current_base, MAP_FLAGS | MAP_RW) < 0) {
unlink_mem (*mem_rid, pages);
return NULL;
}
uintptr_t old_base = current_base;
current_base += pages * PAGE_SIZE;
return (void*)old_base;
}
int liballoc_free (void* ptr, int pages, int mem_rid) {
unmap ((uintptr_t)ptr, pages);
unlink_mem (mem_rid, pages);
return 0;
}
int liballoc_free (void* ptr, int pages) { return unmap ((uintptr_t)ptr, pages); }
/** Durand's Ridiculously Amazing Super Duper Memory functions. */
@@ -208,7 +185,6 @@ 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);
@@ -222,7 +198,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, &mem_rid);
tag = (struct boundary_tag*)liballoc_alloc (pages);
if (tag == NULL)
return NULL; // uh oh, we ran out of memory.
@@ -231,7 +207,6 @@ 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;
@@ -354,7 +329,7 @@ void free (void* ptr) {
if (pages < (unsigned int)l_pageCount)
pages = l_pageCount;
liballoc_free (tag, pages, tag->mem_rid);
liballoc_free (tag, pages);
liballoc_unlock ();
return;

View File

@@ -41,8 +41,6 @@ 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
@@ -69,7 +67,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 pages, int* mem_rid);
extern void* liballoc_alloc (int pages);
/** This frees previously allocated memory. The void* parameter passed
* to the function is the exact same value returned from a previous
@@ -79,7 +77,7 @@ extern void* liballoc_alloc (int pages, int* mem_rid);
*
* \return 0 if the memory was successfully freed.
*/
extern int liballoc_free (void* ptr, int pages, int mem_rid);
extern int liballoc_free (void* ptr, int pages);
void* malloc (size_t); //< The standard function.
void* realloc (void*, size_t); //< The standard function.

View File

@@ -2,8 +2,8 @@
#include <stddef.h>
#include <stdint.h>
int amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
uintptr_t a5, uintptr_t a6) {
uintptr_t amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
uintptr_t a5, uintptr_t a6) {
uint64_t result;
__asm__ volatile ("movq %[a4], %%r10\n"
"movq %[a5], %%r8\n"
@@ -13,5 +13,5 @@ int amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, ui
: "a"(syscall_num), "D"(a1), "S"(a2),
"d"(a3), [a4] "r"(a4), [a5] "r"(a5), [a6] "r"(a6)
: "r10", "r8", "r9", "r11", "rcx", "cc", "memory");
return (int)result;
return result;
}

View File

@@ -3,7 +3,7 @@
#include <stdint.h>
int amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
uintptr_t a5, uintptr_t a6);
uintptr_t amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
uintptr_t a5, uintptr_t a6);
#endif // _LIBMSL_AMD64_SYSCALL_H

View File

@@ -15,8 +15,8 @@ int test (char c) { return do_syscall (SYS_TEST, c); }
int sched (void) { return do_syscall (SYS_SCHED, 0); }
int map (int mem_rid, uintptr_t vaddr, uint32_t flags) {
return do_syscall (SYS_MAP, mem_rid, vaddr, flags);
void* map (uintptr_t vaddr, size_t pages, uint32_t flags) {
return (void*)do_syscall (SYS_MAP, vaddr, pages, flags);
}
int unmap (uintptr_t vaddr, size_t pages) { return do_syscall (SYS_UNMAP, vaddr, pages); }

View File

@@ -5,8 +5,7 @@
#include <stdint.h>
#if defined(__x86_64__)
#define PROC_MAP_BASE 0x0000700000000000
#define PAGE_SIZE 4096
#define PAGE_SIZE 4096
#endif
#define MAP_PRESENT (1 << 0)
@@ -17,10 +16,8 @@
int quit (void);
int test (char c);
int sched (void);
int map (int mem_rid, uintptr_t vaddr, uint32_t flags);
void* map (uintptr_t vaddr, size_t pages, uint32_t flags);
int unmap (uintptr_t vaddr, size_t pages);
int create_mem (int mem_rid, size_t pages);
int unlink_mem (int mem_rid, size_t pages);
int clone (uintptr_t vstack_top, size_t stack_size, void (*entry) (void));
int create_mutex (int mutex_rid);
int unlink_mutex (int mutex_rid);