summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-03-25 10:24:49 -0400
committerIan Moffett <ian@osmora.org>2025-03-25 10:48:34 -0400
commit3d99d2fbd600091a3dec0697e35dddfca17621ab (patch)
tree83d1fa02223696e772fb3c96d2bdc8134cb69134
parent7497caf0fc2ca740229205bc857ce5fb2ff698be (diff)
kernel: vm: Add initial mmap() impl
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--sys/include/sys/mman.h94
-rw-r--r--sys/include/sys/proc.h2
-rw-r--r--sys/kern/kern_fork.c12
-rw-r--r--sys/vm/vm_map.c168
4 files changed, 276 insertions, 0 deletions
diff --git a/sys/include/sys/mman.h b/sys/include/sys/mman.h
new file mode 100644
index 0000000..98ff8fe
--- /dev/null
+++ b/sys/include/sys/mman.h
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _SYS_MMAN_H_
+#define _SYS_MMAN_H_
+
+#include <sys/types.h>
+#include <sys/syscall.h>
+#if defined(_KERNEL)
+#include <sys/tree.h>
+#include <vm/vm_obj.h>
+#endif /* _KERNEL */
+
+/*
+ * If we are in kernel space, all defines we want are
+ * in vm/pmap.h
+ */
+#if !defined(_KERNEL)
+#define PROT_WRITE 0x00000001
+#define PROT_EXEC 0x00000002
+#define PROT_NONE 0x00000004
+#define PROT_READ PROT_NONE
+#endif /* !_KERNEL */
+
+/* mmap() flags */
+#define MAP_SHARED 0x0001
+#define MAP_PRIVATE 0x0002
+#define MAP_FIXED 0x0004
+#define MAP_ANON 0x0008
+
+#if defined(_KERNEL)
+/*
+ * The mmap ledger entry
+ *
+ * @va_start: Starting virtual address.
+ * @obj: VM object representing this entry.
+ */
+struct mmap_entry {
+ vaddr_t va_start;
+ struct vm_object *obj;
+ size_t size;
+ RBT_ENTRY(mmap_entry) hd;
+};
+
+/*
+ * The mmap ledger is a per-process structure that
+ * describes memory mappings made using mmap()
+ *
+ * @hd: Red-black tree of mmap_entry structures
+ * @nbytes: Total bytes mapped.
+ */
+struct mmap_lgdr {
+ RBT_HEAD(lgdr_entries, mmap_entry) hd;
+ size_t nbytes;
+};
+
+/* Kernel mmap() routine */
+void *mmap_at(void *addr, size_t len, int prot, int flags,
+ int fildes, off_t off);
+
+int mmap_entrycmp(const struct mmap_entry *a, const struct mmap_entry *b);
+RBT_PROTOTYPE(lgdr_entries, mmap_entry, hd, mmap_entrycmp)
+#endif /* _KERNEL */
+
+/* Syscall layer */
+scret_t mmap(struct syscall_args *scargs);
+
+#endif /* !_SYS_MMAN_H_ */
diff --git a/sys/include/sys/proc.h b/sys/include/sys/proc.h
index 5e0e927..c561e91 100644
--- a/sys/include/sys/proc.h
+++ b/sys/include/sys/proc.h
@@ -34,6 +34,7 @@
#include <sys/spinlock.h>
#include <sys/queue.h>
#include <sys/param.h>
+#include <sys/mman.h>
#include <sys/cdefs.h>
#include <sys/syscall.h>
#include <sys/exec.h>
@@ -57,6 +58,7 @@ struct proc {
struct exec_prog exec;
struct ksiginfo *ksig_list[PROC_SIGMAX];
struct filedesc *fds[PROC_MAX_FILEDES];
+ struct mmap_lgdr *mlgdr;
struct vcache *vcache;
struct spinlock vcache_lock;
struct trapframe tf;
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 6feeee2..abb7707 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -27,6 +27,8 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include <sys/mman.h>
+#include <sys/tree.h>
#include <sys/types.h>
#include <sys/proc.h>
#include <sys/errno.h>
@@ -49,12 +51,17 @@ int
fork1(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp)
{
struct proc *newproc;
+ struct mmap_lgdr *mlgdr;
int status = 0;
newproc = dynalloc(sizeof(*newproc));
if (newproc == NULL)
return -ENOMEM;
+ mlgdr = dynalloc(sizeof(*mlgdr));
+ if (mlgdr == NULL)
+ return -ENOMEM;
+
memset(newproc, 0, sizeof(*newproc));
status = md_fork(newproc, cur, (uintptr_t)ip);
if (status != 0)
@@ -64,6 +71,11 @@ fork1(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp)
if (newprocp != NULL)
*newprocp = newproc;
+ /* Initialize the mmap ledger */
+ mlgdr->nbytes = 0;
+ RBT_INIT(lgdr_entries, &mlgdr->hd);
+ newproc->mlgdr = mlgdr;
+
newproc->pid = ++nthreads;
signals_init(newproc);
sched_enqueue_td(newproc);
diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c
index 7b0656b..3dd4d01 100644
--- a/sys/vm/vm_map.c
+++ b/sys/vm/vm_map.c
@@ -30,9 +30,51 @@
#include <sys/param.h>
#include <sys/types.h>
#include <sys/errno.h>
+#include <sys/proc.h>
+#include <sys/syscall.h>
+#include <sys/syslog.h>
+#include <sys/mman.h>
+#include <vm/dynalloc.h>
+#include <vm/vm_pager.h>
#include <vm/pmap.h>
#include <vm/map.h>
#include <vm/vm.h>
+#include <assert.h>
+
+#define pr_trace(fmt, ...) kprintf("vm_map: " fmt, ##__VA_ARGS__)
+#define pr_error(...) pr_trace(__VA_ARGS__)
+
+RBT_GENERATE(lgdr_entries, mmap_entry, hd, mmap_entrycmp);
+
+static inline void
+mmap_dbg(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+ pr_trace("addr=%p, len=%d, prot=%x\nflags=%x, fildes=%d, off=%d\n",
+ addr, len, prot, flags, fildes, off);
+}
+
+/*
+ * Add a memory mapping to the mmap ledger.
+ *
+ * @td: Process to add mapping to.
+ * @ep: Memory map entry to add.
+ * @len: Length of memory mapping in bytes.
+ */
+static inline int
+mmap_add(struct proc *td, struct mmap_entry *ep)
+{
+ struct mmap_entry *tmp;
+ struct mmap_lgdr *lp = td->mlgdr;
+
+ if (ep->size == 0) {
+ return -EINVAL;
+ }
+
+ tmp = RBT_INSERT(lgdr_entries, &lp->hd, ep);
+ __assert(tmp == NULL);
+ lp->nbytes += ep->size;
+ return 0;
+}
/*
* Create/destroy virtual memory mappings in a specific
@@ -85,6 +127,123 @@ vm_map_modify(struct vas vas, vaddr_t va, paddr_t pa, vm_prot_t prot, bool unmap
}
/*
+ * Create a physical to virtual memory mapping.
+ *
+ * @addr: Virtual address to map (NULL to be any).
+ * @len: The amount of bytes to map (must be page aligned)
+ * @prot: Protection flags (PROT_*)
+ * @fildes: File descriptor.
+ * @off: Offset.
+ *
+ * TODO: Fields to use: `fildes' and `off'
+ * XXX: Must be called after pid 1 is up and running to avoid
+ * crashes.
+ */
+void *
+mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+ struct vm_object *map_obj;
+ struct vm_page *pg;
+ struct mmap_entry *ep;
+ struct proc *td;
+ struct vas vas;
+ int error, npgs;
+ paddr_t pa;
+ vaddr_t va;
+ size_t misalign;
+
+ misalign = len & (DEFAULT_PAGESIZE - 1);
+ len = ALIGN_UP(len + misalign, DEFAULT_PAGESIZE);
+ npgs = len / DEFAULT_PAGESIZE;
+
+ if (addr == NULL) {
+ pr_error("mmap: NULL addr not supported\n");
+ return NULL;
+ }
+
+ /* Validate flags */
+ if (ISSET(flags, MAP_FIXED | MAP_SHARED)) {
+ pr_error("mmap: fixed/shared mappings not yet supported\n");
+ mmap_dbg(addr, len, prot, flags, fildes, off);
+ return NULL;
+ }
+ map_obj = dynalloc(sizeof(*map_obj));
+ if (map_obj == NULL) {
+ kprintf("mmap: failed to allocate map object\n");
+ return NULL;
+ }
+ error = vm_obj_init(map_obj, &vm_anonops, 1);
+ if (error < 0) {
+ kprintf("mmap: vm_obj_init() returned %d\n", error);
+ kprintf("mmap: failed to init object\n");
+ return NULL;
+ }
+
+ /* XXX: Assuming private */
+ vas = pmap_read_vas();
+ va = ALIGN_DOWN((vaddr_t)addr, DEFAULT_PAGESIZE);
+
+ for (int i = 0; i < npgs; ++i) {
+ pg = vm_pagealloc(map_obj, PALLOC_ZERO);
+
+ if (pg == NULL) {
+ /* TODO */
+ pr_error("mmap: failed to allocate page %d\n");
+ return NULL;
+ }
+
+ pa = pg->phys_addr;
+ error = vm_map(vas, va, pa, prot, len);
+ pr_trace("va=%p, len=%d\n", va, len);
+ if (error < 0) {
+ pr_error("mmap: failed to map page (retval=%x)\n", error);
+ return NULL;
+ }
+ }
+
+ /* Add entry to ledger */
+ td = this_td();
+ ep = dynalloc(sizeof(*ep));
+ if (ep == NULL) {
+ pr_error("mmap: failed to allocate mmap ledger entry\n");
+ return NULL;
+ }
+
+ ep->va_start = va;
+ ep->obj = map_obj;
+ ep->size = len;
+ mmap_add(td, ep);
+ return addr;
+}
+
+/*
+ * mmap() syscall
+ *
+ * arg0 -> addr
+ * arg1 -> len
+ * arg2 -> prot
+ * arg3 -> flags
+ * arg4 -> fildes
+ * arg5 -> off
+ */
+scret_t
+mmap(struct syscall_args *scargs)
+{
+ void *addr;
+ size_t len;
+ int prot, flags;
+ int fildes, off;
+
+ addr = (void *)scargs->arg0;
+ len = scargs->arg1;
+ prot = scargs->arg2;
+ flags = scargs->arg3;
+ fildes = scargs->arg4;
+ off = scargs->arg5;
+ return (scret_t)mmap_at(addr, len, prot, flags, fildes, off);
+}
+
+/*
* Create a virtual memory mapping in a specific
* address space.
*
@@ -130,3 +289,12 @@ vm_unmap(struct vas vas, vaddr_t va, size_t count)
{
return vm_map_modify(vas, va, 0, 0, true, count);
}
+
+/*
+ * Helper for tree(3) and the mmap ledger.
+ */
+int
+mmap_entrycmp(const struct mmap_entry *a, const struct mmap_entry *b)
+{
+ return (a->va_start < b->va_start) ? -1 : a->va_start > b->va_start;
+}