summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-10-19 23:14:23 -0400
committerIan Moffett <ian@osmora.org>2025-10-19 23:22:21 -0400
commitf8411d1f1a4765ce38e63f4b3304d72cbfddad6f (patch)
tree4fe1f21427e834636f601b3958c88f25464bce49
parent6b2d1153852a7f0121d6514cb4d7e5dc6baa7e39 (diff)
kern: vm: Introduce initial mmap() impldev
- Adds kernel mmap() function - Adds SYS_mmap syscall Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--src/sys/include/compat/unix/syscall.h4
-rw-r--r--src/sys/include/sys/mman.h5
-rw-r--r--src/sys/include/sys/syscall.h1
-rw-r--r--src/sys/include/vm/map.h6
-rw-r--r--src/sys/vm/vm_map.c57
5 files changed, 72 insertions, 1 deletions
diff --git a/src/sys/include/compat/unix/syscall.h b/src/sys/include/compat/unix/syscall.h
index 9b76139..5df3c51 100644
--- a/src/sys/include/compat/unix/syscall.h
+++ b/src/sys/include/compat/unix/syscall.h
@@ -39,6 +39,7 @@
#include <os/iotap.h>
#include <os/reboot.h>
#include <dms/dms.h>
+#include <vm/map.h>
/*
* Exit the current process - exit(2) syscall
@@ -100,7 +101,8 @@ scret_t(*g_unix_sctab[])(struct syscall_args *) = {
[SYS_lseek] = sys_lseek,
[SYS_socket] = sys_socket,
[SYS_listen] = sys_listen,
- [SYS_seteuid] = sys_seteuid
+ [SYS_seteuid] = sys_seteuid,
+ [SYS_mmap] = sys_mmap
};
#endif /* !_NEED_UNIX_SCTAB */
diff --git a/src/sys/include/sys/mman.h b/src/sys/include/sys/mman.h
index 7ae1ed2..b688fdd 100644
--- a/src/sys/include/sys/mman.h
+++ b/src/sys/include/sys/mman.h
@@ -39,4 +39,9 @@
#define PROT_USER BIT(3)
#endif /* _KERNEL */
+/*
+ * Map memory pages
+ */
+void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
+
#endif /* !_SYS_MMAN_H_ */
diff --git a/src/sys/include/sys/syscall.h b/src/sys/include/sys/syscall.h
index d148405..ee6ef48 100644
--- a/src/sys/include/sys/syscall.h
+++ b/src/sys/include/sys/syscall.h
@@ -62,6 +62,7 @@
#define SYS_socket 0x11 /* get a socket fd */
#define SYS_listen 0x12 /* listen on a socket */
#define SYS_seteuid 0x13 /* set effective UID */
+#define SYS_mmap 0x14 /* map a virtual address */
typedef __ssize_t scret_t;
typedef __ssize_t scarg_t;
diff --git a/src/sys/include/vm/map.h b/src/sys/include/vm/map.h
index 69d7d73..8183b62 100644
--- a/src/sys/include/vm/map.h
+++ b/src/sys/include/vm/map.h
@@ -31,6 +31,7 @@
#define _VM_MAP_H_ 1
#include <sys/types.h>
+#include <sys/syscall.h>
#include <sys/mman.h>
#include <machine/vas.h> /* standard */
#include <vm/mmu.h>
@@ -52,4 +53,9 @@
*/
int vm_map(struct vm_vas *vas, struct mmu_map *spec, size_t len, int prot);
+/*
+ * POSIX mmap syscall
+ */
+scret_t sys_mmap(struct syscall_args *scargs);
+
#endif /* !_VM_MAP_H_ */
diff --git a/src/sys/vm/vm_map.c b/src/sys/vm/vm_map.c
index f299b34..1d801af 100644
--- a/src/sys/vm/vm_map.c
+++ b/src/sys/vm/vm_map.c
@@ -30,12 +30,16 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/errno.h>
+#include <sys/param.h>
#include <sys/syslog.h>
#include <vm/physseg.h>
#include <vm/mmu.h>
#include <vm/map.h>
#include <vm/vm.h>
+#define MMAP_START 0x6F3C8E0C0000
+#define MMAP_END 0x6F3C90000000
+
/*
* Create a virtual to physical memory
* mapping
@@ -137,3 +141,56 @@ vm_map(struct vm_vas *vas, struct mmu_map *spec, size_t len, int prot)
__vm_map(vas, &spec_cpy, DEFAULT_PAGESIZE, 0);
return 0;
}
+
+void *
+mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+ struct mmu_map spec;
+ struct vm_vas vas;
+ paddr_t pa;
+ int error;
+
+ /* TODO: Support address allocation */
+ if (len == 0 || addr == NULL) {
+ return NULL;
+ }
+
+ spec.va = (uintptr_t)addr;
+ spec.pa = 0;
+ error = mmu_this_vas(&vas);
+ if (error < 0) {
+ return NULL;
+ }
+
+ /* Create the mapping */
+ error = vm_map(&vas, &spec, len, prot);
+ if (error < 0) {
+ return NULL;
+ }
+
+ return (void *)spec.va;
+}
+
+/*
+ * ARG0: Address
+ * ARG1: Length
+ * ARG2: Protection flags
+ * ARG3: Flags
+ * ARG4: Fildes
+ * ARG5: Offset
+ */
+scret_t
+sys_mmap(struct syscall_args *scargs)
+{
+ void *address = SCARG(scargs, void *, 0);
+ size_t length = SCARG(scargs, size_t, 1);
+ int prot = SCARG(scargs, int, 2);
+
+ prot |= PROT_USER;
+ if (address != NULL) {
+ address = PTR_OFFSET(address, MMAP_START);
+ }
+
+ /* XXX: Should use rest of the args */
+ return (uintptr_t)mmap(address, length, prot, 0, 0, 0);
+}