syscall: add mmap and munmap

This commit is contained in:
Michael Shaw 2025-03-05 20:43:14 -05:00
commit 0870bd8737
6 changed files with 41 additions and 4 deletions

View file

@ -17,6 +17,8 @@
#define PAGE_PWT 0x8 #define PAGE_PWT 0x8
#define PAGE_PCD 0x10 #define PAGE_PCD 0x10
#define MAP_ANONYMOUS 0x20
#ifndef __ASM__ #ifndef __ASM__
#define ALIGN(_a) __attribute__((aligned(_a))) #define ALIGN(_a) __attribute__((aligned(_a)))

View file

@ -56,7 +56,7 @@ void *kmalloc(size_t size, uint64_t flags);
void *kzalloc(size_t size, uint64_t flags); void *kzalloc(size_t size, uint64_t flags);
void *krealloc(void *ptr, size_t size, uint64_t flags); void *krealloc(void *ptr, size_t size, uint64_t flags);
void kfree(void *ptr); void kfree(void *ptr);
void *umalloc(struct proc *proc, size_t size, uint64_t opts); void *umalloc(struct proc *proc, size_t size, uint64_t opts, uintptr_t req_addr);
void ufree(struct proc *proc, void *ptr); void ufree(struct proc *proc, void *ptr);
void slabtypes_init(); void slabtypes_init();

View file

@ -5,11 +5,14 @@
#define SYS_READ 0 #define SYS_READ 0
#define SYS_WRITE 1 #define SYS_WRITE 1
#define SYS_OPEN 2 #define SYS_OPEN 2
#define SYS_MMAP 9
#define SYS_MUNMAP 11
#define SYS_FORK 57 #define SYS_FORK 57
#define SYS_EXIT 60 #define SYS_EXIT 60
#define SYS_MKDIR 83 #define SYS_MKDIR 83
#define SYS_MKNOD 133 #define SYS_MKNOD 133
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/proc.h> #include <kernel/proc.h>

View file

@ -332,7 +332,7 @@ void ufree(struct proc *proc, void *addr)
} }
/* It is implied that when you're calling this function, the process is not running */ /* It is implied that when you're calling this function, the process is not running */
void *umalloc(struct proc *proc, size_t size, uint64_t opts) void *umalloc(struct proc *proc, size_t size, uint64_t opts, uintptr_t req_addr)
{ {
struct proc_allocation *a = kzalloc(sizeof(struct proc_allocation), 0); struct proc_allocation *a = kzalloc(sizeof(struct proc_allocation), 0);
@ -350,7 +350,11 @@ void *umalloc(struct proc *proc, size_t size, uint64_t opts)
} }
proc->stack_start = vaddr; proc->stack_start = vaddr;
} else { } else {
vaddr = mmap_find_unmapped(&proc->page_map, &proc->page_map_lock, USER_HEAP_BASE, size); if (req_addr) {
vaddr = mmap_find_unmapped(&proc->page_map, &proc->page_map_lock, req_addr, size);
} else {
vaddr = mmap_find_unmapped(&proc->page_map, &proc->page_map_lock, USER_HEAP_BASE, size);
}
} }
uint64_t attr; uint64_t attr;

View file

@ -118,7 +118,7 @@ pid_t elf_load_proc(char *fname)
/* allocate user stack */ /* allocate user stack */
proc->stack_start = USER_STACK_BASE; proc->stack_start = USER_STACK_BASE;
proc->stack_size = 0; /* umalloc will grow the stack */ proc->stack_size = 0; /* umalloc will grow the stack */
void *ustack = umalloc(proc, 0x10000, ALLOC_USER_STACK); void *ustack = umalloc(proc, 0x10000, ALLOC_USER_STACK, 0);
/* set stack pointer */ /* set stack pointer */
proc->regs.rsp = proc->stack_start + proc->stack_size; proc->regs.rsp = proc->stack_start + proc->stack_size;

View file

@ -234,6 +234,34 @@ int sys_mknod(const char *pathname, mode_t mode, dev_t dev)
return 0; return 0;
} }
void *sys_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
{
if (!(flags & MAP_ANONYMOUS))
return (void *)-ENOSYS;
struct proc *proc = proc_find(getupid());
if (proc == NULL)
return (void *)-1;
void *ret = umalloc(proc, len, UA_SLAB, (uintptr_t)addr);
if (ret == NULL)
return (void *)-ENOMEM;
return ret;
}
void *sys_munmap(void *addr, size_t len)
{
struct proc *proc = proc_find(getupid());
if (proc == NULL)
return (void *)-1;
ufree(proc, addr);
return 0;
}
static void syscall_insert(uint64_t syscall_no, syscall_t syscall) static void syscall_insert(uint64_t syscall_no, syscall_t syscall)
{ {
syscall_table[syscall_no] = syscall; syscall_table[syscall_no] = syscall;