diff options
Diffstat (limited to 'sys/vm')
-rw-r--r-- | sys/vm/tlsf.c | 3 | ||||
-rw-r--r-- | sys/vm/vm_device.c | 78 | ||||
-rw-r--r-- | sys/vm/vm_init.c | 1 | ||||
-rw-r--r-- | sys/vm/vm_map.c | 207 | ||||
-rw-r--r-- | sys/vm/vm_physmem.c | 112 | ||||
-rw-r--r-- | sys/vm/vm_stat.c | 95 | ||||
-rw-r--r-- | sys/vm/vm_vnode.c | 3 |
7 files changed, 453 insertions, 46 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_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 3dd4d01..bb9df83 100644 --- a/sys/vm/vm_map.c +++ b/sys/vm/vm_map.c @@ -31,11 +31,14 @@ #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> @@ -77,6 +80,22 @@ mmap_add(struct proc *td, struct mmap_entry *ep) } /* + * 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 * address space. * @@ -140,51 +159,113 @@ vm_map_modify(struct vas vas, vaddr_t va, paddr_t pa, vm_prot_t prot, bool unmap * crashes. */ void * -mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off) +mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) { - struct vm_object *map_obj; + 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; - - if (addr == NULL) { - pr_error("mmap: NULL addr not supported\n"); - return NULL; - } + vas = pmap_read_vas(); /* Validate flags */ - if (ISSET(flags, MAP_FIXED | MAP_SHARED)) { - pr_error("mmap: fixed/shared mappings not yet supported\n"); + if (ISSET(flags, MAP_FIXED)) { + pr_error("mmap: fixed 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; + + /* + * Attempt to open the file if mapping + * is shared. + */ + if (ISSET(flags, MAP_SHARED)) { + fdp = fd_get(NULL, 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; } - 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; + + /* 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 */ - 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); + page_off = i * DEFAULT_PAGESIZE; if (pg == NULL) { /* TODO */ @@ -192,15 +273,21 @@ mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off) 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, pa, prot, len); - pr_trace("va=%p, len=%d\n", va, len); + 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)); @@ -217,6 +304,61 @@ mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off) } /* + * 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 @@ -227,7 +369,7 @@ mmap_at(void *addr, size_t len, int prot, int flags, int fildes, off_t off) * arg5 -> off */ scret_t -mmap(struct syscall_args *scargs) +sys_mmap(struct syscall_args *scargs) { void *addr; size_t len; @@ -236,11 +378,28 @@ mmap(struct syscall_args *scargs) addr = (void *)scargs->arg0; len = scargs->arg1; - prot = scargs->arg2; + prot = scargs->arg2 | PROT_USER; flags = scargs->arg3; fildes = scargs->arg4; off = scargs->arg5; - return (scret_t)mmap_at(addr, len, prot, flags, fildes, off); + 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); } /* diff --git a/sys/vm/vm_physmem.c b/sys/vm/vm_physmem.c index c7fcedb..42b7a31 100644 --- a/sys/vm/vm_physmem.c +++ b/sys/vm/vm_physmem.c @@ -32,15 +32,22 @@ #include <sys/limine.h> #include <sys/syslog.h> #include <sys/spinlock.h> +#include <sys/panic.h> #include <vm/physmem.h> #include <vm/vm.h> #include <string.h> -size_t highest_frame_idx = 0; -size_t bitmap_size = 0; -size_t bitmap_free_start = 0; +#define BYTES_PER_MIB 8388608 -uint8_t *bitmap; +static size_t pages_free = 0; +static size_t pages_used = 0; +static size_t pages_total = 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; + +static uint8_t *bitmap; static struct limine_memmap_response *resp = NULL; static struct spinlock lock = {0}; @@ -59,9 +66,11 @@ physmem_populate_bitmap(void) for (size_t i = 0; i < resp->entry_count; ++i) { ent = resp->entries[i]; + pages_total += ent->length / DEFAULT_PAGESIZE; if (ent->type != LIMINE_MEMMAP_USABLE) { /* This memory is not usable */ + pages_used += ent->length / DEFAULT_PAGESIZE; continue; } @@ -72,6 +81,8 @@ physmem_populate_bitmap(void) for (size_t j = 0; j < ent->length; j += DEFAULT_PAGESIZE) { clrbit(bitmap, (ent->base + j) / DEFAULT_PAGESIZE); } + + pages_free += ent->length / DEFAULT_PAGESIZE; } } @@ -105,7 +116,6 @@ physmem_alloc_bitmap(void) } } - /* * Init the physical memory bitmap. */ @@ -137,29 +147,59 @@ 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; - } - - for (size_t j = i; j < i + count; ++j) { - setbit(bitmap, j); - } + if (idx < 0) + idx = i; + if (++frames >= count) + break; - 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); } + if (ret == 0) { + panic("out of memory\n"); + } + + pages_used += count; + pages_free -= count; spinlock_release(&lock); return ret; } @@ -169,13 +209,47 @@ 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); } + pages_used -= count; + pages_free += count; spinlock_release(&lock); } +/* + * Return the amount of memory in MiB that is + * currently allocated. + */ +uint32_t +vm_mem_used(void) +{ + return (pages_used * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + +/* + * Return the amount of memory in MiB that is + * currently free. + */ +uint32_t +vm_mem_free(void) +{ + return (pages_free * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + +/* + * Return the total amount of memory supported + * by the machine. + */ +size_t +vm_mem_total(void) +{ + return (pages_total * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + void vm_physmem_init(void) { diff --git a/sys/vm/vm_stat.c b/sys/vm/vm_stat.c new file mode 100644 index 0000000..3e39047 --- /dev/null +++ b/sys/vm/vm_stat.c @@ -0,0 +1,95 @@ +/* + * 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/errno.h> +#include <fs/ctlfs.h> +#include <vm/physmem.h> +#include <vm/vm.h> +#include <vm/stat.h> +#include <string.h> + +#include <sys/syslog.h> + +static struct ctlops vm_stat_ctl; + +/* + * ctlfs hook to read the virtual memory + * statistics. + */ +static int +vm_stat_read(struct ctlfs_dev *cdp, struct sio_txn *sio) +{ + struct vm_stat stat; + int error; + + if (sio->len > sizeof(stat)) { + sio->len = sizeof(stat); + } + + error = vm_stat_get(&stat); + if (error < 0) { + return error; + } + + memcpy(sio->buf, &stat, sio->len); + return sio->len; +} + +int +vm_stat_get(struct vm_stat *vmstat) +{ + if (vmstat == NULL) { + return -EINVAL; + } + + vmstat->mem_avail = vm_mem_free(); + vmstat->mem_used = vm_mem_used(); + vmstat->mem_total = vm_mem_total(); + return 0; +} + +void +vm_stat_init(void) +{ + char devname[] = "vm"; + struct ctlfs_dev ctl; + + /* Register a stat control file */ + ctl.mode = 0444; + ctlfs_create_node(devname, &ctl); + ctl.devname = devname; + ctl.ops = &vm_stat_ctl; + ctlfs_create_entry("stat", &ctl); +} + +static struct ctlops vm_stat_ctl = { + .read = vm_stat_read, + .write = NULL +}; diff --git a/sys/vm/vm_vnode.c b/sys/vm/vm_vnode.c index 2457c97..777b382 100644 --- a/sys/vm/vm_vnode.c +++ b/sys/vm/vm_vnode.c @@ -73,7 +73,7 @@ vn_io(struct vnode *vp, struct vm_page **pgs, unsigned int npages, int rw) args.res = &vattr; c = MAX(vattr.size / DEFAULT_PAGESIZE, 1); - if ((err = vfs_vop_getattr(vp, &args)) != 0) { + if ((err = vfs_vop_getattr(&args)) != 0) { return err; } @@ -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; } |