summaryrefslogtreecommitdiff
path: root/sys/vm
diff options
context:
space:
mode:
Diffstat (limited to 'sys/vm')
-rw-r--r--sys/vm/tlsf.c3
-rw-r--r--sys/vm/vm_anon.c5
-rw-r--r--sys/vm/vm_device.c78
-rw-r--r--sys/vm/vm_init.c1
-rw-r--r--sys/vm/vm_map.c327
-rw-r--r--sys/vm/vm_page.c10
-rw-r--r--sys/vm/vm_physmem.c63
-rw-r--r--sys/vm/vm_vnode.c1
8 files changed, 461 insertions, 27 deletions
diff --git a/sys/vm/tlsf.c b/sys/vm/tlsf.c
index d4a6ddf..8e425a1 100644
--- a/sys/vm/tlsf.c
+++ b/sys/vm/tlsf.c
@@ -1,7 +1,8 @@
#include <sys/syslog.h>
+#define _HAVE_PTRDIFF_T
#include <sys/types.h>
+#include <sys/limits.h>
#include <assert.h>
-#include <limits.h>
#include <string.h>
#include <vm/tlsf.h>
diff --git a/sys/vm/vm_anon.c b/sys/vm/vm_anon.c
index 9ca8842..2110ecd 100644
--- a/sys/vm/vm_anon.c
+++ b/sys/vm/vm_anon.c
@@ -87,6 +87,8 @@ anon_get(struct vm_object *obp, struct vm_page **pgs, off_t off, size_t len)
continue;
}
+ *pgres = *pgtmp;
+
/*
* We are *just* populating `pgs' and therefore nobody
* should even attempt to acquire this lock... Shit
@@ -99,9 +101,6 @@ anon_get(struct vm_object *obp, struct vm_page **pgs, off_t off, size_t len)
return -ETIMEDOUT;
}
- /* Hold pgres before configuring it */
- spinlock_acquire(&pgres->lock);
- *pgres = *pgtmp;
pgres->flags |= (PG_VALID | PG_CLEAN);
spinlock_release(&pgres->lock);
diff --git a/sys/vm/vm_device.c b/sys/vm/vm_device.c
new file mode 100644
index 0000000..e990b47
--- /dev/null
+++ b/sys/vm/vm_device.c
@@ -0,0 +1,78 @@
+/*
+ * 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.
+ */
+
+#include <sys/types.h>
+#include <sys/device.h>
+#include <sys/syslog.h>
+#include <vm/vm_device.h>
+
+#define pr_trace(fmt, ...) kprintf("vm_device: " fmt, ##__VA_ARGS__)
+#define pr_error(...) pr_trace(__VA_ARGS__)
+
+const struct vm_pagerops dv_vnops;
+
+/*
+ * Attach a cdev to a vm_object
+ *
+ * @major: Char device major
+ * @minor: Char device minor.
+ */
+struct vm_object *
+dv_attach(devmajor_t major, dev_t dev, vm_prot_t prot)
+{
+ int error;
+ struct cdevsw *cdevp;
+ struct vm_object *vmobj;
+
+ if ((cdevp = dev_get(major, dev)) == NULL) {
+ pr_error("bad attach (major=%d, dev=%d)\n", major, dev);
+ return NULL;
+ }
+
+ if (cdevp->mmap == NULL) {
+ pr_error("cdev lacks mmap() (major=%d, dev=%d)\n", major, dev);
+ return NULL;
+ }
+
+ error = vm_obj_init(&cdevp->vmobj, &dv_vnops, 1);
+ if (error != 0) {
+ return NULL;
+ }
+
+ vmobj = &cdevp->vmobj;
+ vmobj->prot = prot;
+ vmobj->data = cdevp;
+ vmobj->pgops = &dv_vnops;
+ return vmobj;
+}
+
+/* TODO */
+const struct vm_pagerops dv_vnops = {
+ .get = NULL,
+};
diff --git a/sys/vm/vm_init.c b/sys/vm/vm_init.c
index 2846a69..7518838 100644
--- a/sys/vm/vm_init.c
+++ b/sys/vm/vm_init.c
@@ -56,6 +56,7 @@ vm_init(void)
void *pool;
vm_physmem_init();
+ pmap_init();
g_kvas = pmap_read_vas();
vm_ctx.dynalloc_pool_sz = DYNALLOC_POOL_SZ;
diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c
index 7b0656b..26effdb 100644
--- a/sys/vm/vm_map.c
+++ b/sys/vm/vm_map.c
@@ -30,9 +30,70 @@
#include <sys/param.h>
#include <sys/types.h>
#include <sys/errno.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+#include <sys/syscall.h>
+#include <sys/syslog.h>
+#include <sys/mman.h>
+#include <sys/filedesc.h>
+#include <vm/dynalloc.h>
+#include <vm/vm_pager.h>
+#include <vm/vm_device.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;
+}
+
+/*
+ * Remove memory mapping from mmap ledger
+ *
+ * @td: Process to remove mapping from.
+ * @ep: Memory map entry to remove.
+ */
+static inline void
+mmap_remove(struct proc *td, struct mmap_entry *ep)
+{
+ struct mmap_lgdr *lp = td->mlgdr;
+
+ RBT_REMOVE(lgdr_entries, &lp->hd, ep);
+ lp->nbytes -= ep->size;
+ dynfree(ep);
+}
/*
* Create/destroy virtual memory mappings in a specific
@@ -85,6 +146,263 @@ 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(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
+{
+ struct vm_object *map_obj = NULL;
+ struct cdevsw *cdevp;
+ struct vm_page *pg;
+ struct mmap_entry *ep;
+ struct vnode *vp;
+ struct filedesc *fdp;
+ struct proc *td;
+ struct vas vas;
+ int error, npgs;
+ paddr_t pa;
+ vaddr_t va;
+ size_t misalign;
+ off_t page_off;
+
+ misalign = len & (DEFAULT_PAGESIZE - 1);
+ len = ALIGN_UP(len + misalign, DEFAULT_PAGESIZE);
+ npgs = len / DEFAULT_PAGESIZE;
+ vas = pmap_read_vas();
+
+ /* Validate flags */
+ if (ISSET(flags, MAP_FIXED)) {
+ pr_error("mmap: fixed mappings not yet supported\n");
+ mmap_dbg(addr, len, prot, flags, fildes, off);
+ return NULL;
+ }
+
+ /*
+ * Attempt to open the file if mapping
+ * is shared.
+ */
+ if (ISSET(flags, MAP_SHARED)) {
+ fdp = fd_get(fildes);
+ if (fdp == NULL) {
+ pr_error("mmap: no such fd (fd=%d)\n", fildes);
+ return NULL;
+ }
+
+ vp = fdp->vp;
+ if (vp->type != VCHR) {
+ /* TODO */
+ pr_error("mmap: only device files supported\n");
+ return NULL;
+ }
+
+ map_obj = dv_attach(vp->major, vp->dev, prot);
+ if (map_obj == NULL) {
+ kprintf("mmap: dv_attach() failure\n");
+ return NULL;
+ }
+
+ cdevp = map_obj->data;
+ if ((pa = cdevp->mmap(vp->dev, len, off, 0)) == 0) {
+ kprintf("mmap: dev mmap() gave 0\n");
+ return NULL;
+ }
+
+ /*
+ * If the address passed is NULL, just identity
+ * map everything.
+ *
+ * XXX: This is why the bounds check done in the
+ * cdev mmap() *must* be correct.
+ *
+ * TODO: Use copy-on-write for this instead. Since mapping
+ * certain devices may required a lot of memory to
+ * be referenced anyways, we could use a buffered
+ * copy-on-write technique where only a window of
+ * pages can be mapped on-demand and other pages
+ * freed when that window is exceeded.
+ */
+ if (addr == NULL) {
+ addr = (void *)pa;
+ }
+
+ va = ALIGN_DOWN((vaddr_t)addr, DEFAULT_PAGESIZE);
+ error = vm_map(vas, va, pa, prot, len);
+ if (error != 0) {
+ kprintf("mmap: map failed (error=%d)\n", error);
+ return NULL;
+ }
+
+ goto done;
+ }
+
+ /* Only allocate new obj if needed */
+ if (map_obj == 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 */
+ va = ALIGN_DOWN((vaddr_t)addr, DEFAULT_PAGESIZE);
+
+ for (int i = 0; i < npgs; ++i) {
+ pg = vm_pagealloc(map_obj, PALLOC_ZERO);
+ page_off = i * DEFAULT_PAGESIZE;
+
+ if (pg == NULL) {
+ /* TODO */
+ pr_error("mmap: failed to allocate page %d\n");
+ return NULL;
+ }
+
+ /* TODO: copy-on-write */
+ if (addr == NULL) {
+ va = pg->phys_addr;
+ addr = (void *)va;
+ }
+
+ pa = pg->phys_addr;
+ error = vm_map(vas, va + page_off, pa, prot, len);
+ if (error < 0) {
+ pr_error("mmap: failed to map page (retval=%x)\n", error);
+ return NULL;
+ }
+ }
+
+done:
+ /* 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;
+}
+
+/*
+ * Remove mappings for entire pages that
+ * belong to the current process.
+ *
+ * XXX: POSIX munmap(3) requires `addr' to be page-aligned
+ * and will return -EINVAL if otherwise. However, with
+ * OUSI munmap(3), `addr' is rounded down to the nearest
+ * multiple of the machine page size.
+ */
+int
+munmap(void *addr, size_t len)
+{
+ int pgno;
+ vaddr_t va;
+ struct proc *td;
+ struct mmap_lgdr *lp;
+ struct mmap_entry find, *res;
+ struct vas vas;
+
+ if (addr == NULL || len == 0) {
+ return -EINVAL;
+ }
+
+ /* Apply machine specific addr/len adjustments */
+ va = ALIGN_DOWN((vaddr_t)addr, DEFAULT_PAGESIZE);
+ len = ALIGN_UP(len, DEFAULT_PAGESIZE);
+ pgno = va >> 12;
+
+ td = this_td();
+ __assert(td != NULL && "no pid 1");
+ vas = pmap_read_vas();
+
+ /*
+ * Try to get the mmap ledger, should not run into
+ * any issues as long as the PCB isn't borked. However,
+ * if it somehow is, just segfault ourselves.
+ */
+ if ((lp = td->mlgdr) == NULL) {
+ __sigraise(SIGSEGV);
+ return -EFAULT; /* Unreachable */
+ }
+
+ /* Lookup entry in ledger with virtual address */
+ find.va_start = va;
+ res = RBT_FIND(lgdr_entries, &lp->hd, &find);
+ if (res == NULL) {
+ pr_error("munmap: page %d not in ledger\n", pgno);
+ return -EINVAL;
+ }
+
+ vm_unmap(vas, va, len);
+ mmap_remove(td, res);
+ return 0;
+}
+
+/*
+ * mmap() syscall
+ *
+ * arg0 -> addr
+ * arg1 -> len
+ * arg2 -> prot
+ * arg3 -> flags
+ * arg4 -> fildes
+ * arg5 -> off
+ */
+scret_t
+sys_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 | PROT_USER;
+ flags = scargs->arg3;
+ fildes = scargs->arg4;
+ off = scargs->arg5;
+ return (scret_t)mmap(addr, len, prot, flags, fildes, off);
+}
+
+/*
+ * munmap() syscall
+ *
+ * arg0 -> addr
+ * arg1 -> len
+ */
+scret_t
+sys_munmap(struct syscall_args *scargs)
+{
+ void *addr;
+ size_t len;
+
+ addr = (void *)scargs->arg0;
+ len = scargs->arg1;
+ return (scret_t)munmap(addr, len);
+}
+
+/*
* Create a virtual memory mapping in a specific
* address space.
*
@@ -130,3 +448,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;
+}
diff --git a/sys/vm/vm_page.c b/sys/vm/vm_page.c
index 52ded0e..3403a49 100644
--- a/sys/vm/vm_page.c
+++ b/sys/vm/vm_page.c
@@ -43,11 +43,12 @@ RBT_GENERATE(vm_objtree, vm_page, objt, vm_pagecmp);
static inline void
vm_pageinsert(struct vm_page *pg, struct vm_object *obp)
{
- struct vm_page *tmp;
+ struct vm_page *tmp;
- tmp = RBT_INSERT(vm_objtree, &obp->objt, pg);
- __assert(tmp == NULL);
- ++obp->npages;
+ tmp = RBT_INSERT(vm_objtree, &obp->objt, pg);
+ if (tmp != NULL)
+ return;
+ ++obp->npages;
}
static inline void
@@ -79,6 +80,7 @@ vm_pagealloc(struct vm_object *obj, int flags)
memset(tmp, 0, sizeof(*tmp));
tmp->phys_addr = vm_alloc_frame(1);
tmp->flags |= (PG_VALID | PG_CLEAN);
+ tmp->offset = tmp->phys_addr >> 12;
__assert(tmp->phys_addr != 0);
if (ISSET(flags, PALLOC_ZERO)) {
diff --git a/sys/vm/vm_physmem.c b/sys/vm/vm_physmem.c
index c7fcedb..89f9ee6 100644
--- a/sys/vm/vm_physmem.c
+++ b/sys/vm/vm_physmem.c
@@ -36,11 +36,12 @@
#include <vm/vm.h>
#include <string.h>
-size_t highest_frame_idx = 0;
-size_t bitmap_size = 0;
-size_t bitmap_free_start = 0;
+static size_t highest_frame_idx = 0;
+static size_t bitmap_size = 0;
+static size_t bitmap_free_start = 0;
+static ssize_t last_idx = 0;
-uint8_t *bitmap;
+static uint8_t *bitmap;
static struct limine_memmap_response *resp = NULL;
static struct spinlock lock = {0};
@@ -137,27 +138,51 @@ physmem_init_bitmap(void)
*
* @count: Number of frames to allocate.
*/
-uintptr_t
-vm_alloc_frame(size_t count)
+static uintptr_t
+__vm_alloc_frame(size_t count)
{
size_t frames = 0;
+ ssize_t idx = -1;
uintptr_t ret = 0;
- spinlock_acquire(&lock);
- for (size_t i = 0; i < highest_frame_idx; ++i) {
+ for (size_t i = last_idx; i < highest_frame_idx; ++i) {
if (!testbit(bitmap, i)) {
- /* We have a free page */
- if (++frames != count) {
- continue;
- }
+ if (idx < 0)
+ idx = i;
+ if (++frames >= count)
+ break;
- for (size_t j = i; j < i + count; ++j) {
- setbit(bitmap, j);
- }
-
- ret = i * DEFAULT_PAGESIZE;
- break;
+ continue;
}
+
+ idx = -1;
+ frames = 0;
+ }
+
+ if (idx < 0 || frames != count) {
+ ret = 0;
+ goto done;
+ }
+
+ for (size_t i = idx; i < idx + count; ++i) {
+ setbit(bitmap, i);
+ }
+ ret = idx * DEFAULT_PAGESIZE;
+ last_idx = idx;
+ memset(PHYS_TO_VIRT(ret), 0, count * DEFAULT_PAGESIZE);
+done:
+ return ret;
+}
+
+uintptr_t
+vm_alloc_frame(size_t count)
+{
+ uintptr_t ret;
+
+ spinlock_acquire(&lock);
+ if ((ret = __vm_alloc_frame(count)) == 0) {
+ last_idx = 0;
+ ret = __vm_alloc_frame(count);
}
spinlock_release(&lock);
@@ -169,6 +194,8 @@ vm_free_frame(uintptr_t base, size_t count)
{
size_t stop_at = base + (count * DEFAULT_PAGESIZE);
+ base = ALIGN_UP(base, DEFAULT_PAGESIZE);
+
spinlock_acquire(&lock);
for (uintptr_t p = base; p < stop_at; p += DEFAULT_PAGESIZE) {
clrbit(bitmap, p / DEFAULT_PAGESIZE);
diff --git a/sys/vm/vm_vnode.c b/sys/vm/vm_vnode.c
index 2457c97..27defc9 100644
--- a/sys/vm/vm_vnode.c
+++ b/sys/vm/vm_vnode.c
@@ -162,7 +162,6 @@ vn_attach(struct vnode *vp, vm_prot_t prot)
if (vp->type != VREG) {
pr_error("vn_attach: vp=%p, prot=%x\n", vp, prot);
- pr_error("vn_attach: Special files not supported yet!\n");
return NULL;
}