UVM_INIT(9)

πŸ“¦ in nixpkgs unstable

πŸ’– NAME πŸ’–

okay so this is UVM_INIT(9), straight out of the Kernel Developer's Manual, and it's literally the virtual memory system external interface. bestie, the whole squad on this page is uvm_init, uvm_init_limits, uvm_setpagesize, uvm_swap_init, uvm_io, uvm_pageout, uao_create, uao_detach, uao_reference, uvm_chgkprot, uvm_kernacc, uvm_meter, uvm_sysctl, uvm_grow, and uvm_coredump_walkmap. iconic lineup, no notes πŸ’…


✨ SYNOPSIS ✨

you start with these includes, periodt:

#include <sys/param.h>
#include <uvm/uvm.h>

🌸 DESCRIPTION 🌸

the UVM virtual memory system manages access to the computer's memory resources. user processes and the kernel access these resources through UVM's external interface. and UVM's external interface includes functions that:

  • initialise UVM subsystems
  • manage virtual address spaces
  • resolve page faults
  • memory map files and devices
  • perform uio-based I/O to virtual memory
  • allocate and free kernel virtual memory
  • allocate and free physical memory

so yeah, it's giving memory management main character energy 🫢

in addition to exporting these services, UVM has two kernel-level processes: pagedaemon and swapper. the pagedaemon process sleeps until physical memory becomes scarce. when that happens, pagedaemon is awoken. it scans physical memory, paging out and freeing memory that has not been recently used. the swapper process swaps in runnable processes that are currently swapped out, if there is room. slay, honestly πŸ’€βœ¨

UVM has a machine independent and a machine dependent layer. see pmap(9) for the machine dependent layer πŸ‘‘


πŸŽ€ INITIALISATION πŸŽ€

here's the exact prototype lineup, bestie:

void uvm_init(void) void uvm_init_limits(struct plimit *limit0) void uvm_setpagesize(void) void uvm_swap_init(void)

the uvm_init() function sets up the UVM system at system boot time, after the copyright has been printed. it initialises global state, the page, map, kernel virtual memory state, machine-dependent physical map, kernel memory allocator, pager and anonymous memory subsystems, and then enables paging of kernel objects.

uvm_init() must be called after machine-dependent code has registered some free RAM with the uvm_page_physload() function. like, that's the rule, no cap 🚨

the uvm_init_limits() function initialises process limits in the given limit structure. this is for use by the system startup for process zero, before any other processes are created. early bird energy fr fr πŸŒ…

the uvm_setpagesize() function initialises the uvmexp members pagesize (if not already done by machine-dependent code), pageshift and pagemask. it should be called by machine-dependent code early in the pmap_init(9) call. lowkey crucial πŸ’–

the uvm_swap_init() function initialises the swap subsystem. that's it, that's the vibe πŸŒ€


πŸ¦‹ VIRTUAL MEMORY I/O πŸ¦‹

the signature is exactly this:

int uvm_io(vm_map_t map, struct uio *uio)

the uvm_io() function performs the I/O described in uio on the memory described in map. literally doing I/O on virtual memory, and ate πŸ’…


πŸ”₯ PROCESSES πŸ”₯

and here's the page daemon entry point:

void uvm_pageout(void *arg)

the uvm_pageout() function is the main loop for the page daemon. the arg argument is ignored. not me pointing out that arg is just there vibing πŸ’€

πŸ’« MISCELLANEOUS FUNCTIONS πŸ’«

okay bestie, here's the last batch of signatures β€” the grab-bag of helpers that don't fit anywhere else. Dropping them exactly as they come:

struct uvm_object * uao_create(vsize_t size, int flags)
void uao_detach(struct uvm_object *uobj)
void uao_reference(struct uvm_object *uobj)
boolean_t uvm_chgkprot(caddr_t addr, size_t len, int rw)
void uvm_kernacc(caddr_t addr, size_t len, int rw)
void uvm_meter()
int uvm_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen, struct proc *p)
int uvm_grow(struct proc *p, vaddr_t sp)
int uvm_coredump_walkmap(struct proc *p, uvm_coredump_setup_cb *setup, struct uvm_coredump_walk_cb *walk, void *cookie)
side note: uvm_chgkprot() shows up in that signature list and then the page just… doesn't say anything else about it πŸ™ˆ no cap, that's all we get.

🌸 the anonymous object girlies: uao_create(), uao_detach(), uao_reference()

uao_create(), uao_detach() and uao_reference() are the trio that operates on anonymous memory objects β€” think the stuff that backs System V shared memory. iconic behaviour honestly πŸ’…

uao_create() hands you back an object of size size, and the flags it takes are literally these:

#define UAO_FLAG_KERNOBJ        0x1     /* create kernel object */
#define UAO_FLAG_KERNSWAP       0x2     /* enable kernel swap */
🫢 real talk: each of those flags can only be used once each, at system boot time. Boot-time-only, bestie. Do not be out here trying to use them later.

uao_reference() adds one more reference to the named anonymous memory object. uao_detach() drops a reference from that named anonymous memory object, and if you just removed the last reference β€” congrats, it gets destroyed πŸŽ€


πŸ”₯ uvm_kernacc()

uvm_kernacc() does the vibe check on access in the kernel address space: it checks the access at address addr through addr + len for rw access. that's it, that's the whole gig ✨


⭐ uvm_meter()

uvm_meter() calculates the load average and wakes up the swapper if that's necessary. literally the alarm clock of the kernel 😭


πŸ‘‘ uvm_sysctl()

uvm_sysctl() is the support system for the CTL_VM domain of the sysctl(2) hierarchy. It handles the VM_LOADAVG, VM_METER and VM_UVMEXP calls β€” which respectively return the current load averages, calculate current VM totals, and return the uvmexp structure.

The load averages get accessed from userland through the getloadavg(3) function, iykyk. 🧚

The uvmexp structure holds all the global state of the UVM system. It has these members, and I'm giving you every single one because we do not skip:

/* vm_page constants */
int pagesize;   /* size of a page (PAGE_SIZE): must be power of 2 */
int pagemask;   /* page mask */
int pageshift;  /* page shift */
/* vm_page counters */
int npages;     /* number of pages we manage */
int free;       /* number of free pages */
int active;     /* number of active pages */
int inactive;   /* number of pages that we free'd but may want back */
int paging;     /* number of pages in the process of being paged out */
int wired;      /* number of wired pages */

int zeropages;          /* number of zero'd pages */
int reserve_pagedaemon; /* number of pages reserved for pagedaemon */
int reserve_kernel;     /* number of pages reserved for kernel */
int unused01;           /* formerly anonpages */
int vnodepages;         /* XXX # of pages used by vnode page cache */
int vtextpages;         /* XXX # of pages used by vtext vnodes */
/* pageout params */
int freemin;    /* min number of free pages */
int freetarg;   /* target number of free pages */
int inactarg;   /* target number of inactive pages */
int wiredmax;   /* max number of wired pages */
int anonmin;    /* min threshold for anon pages */
int vtextmin;   /* min threshold for vtext pages */
int vnodemin;   /* min threshold for vnode pages */
int anonminpct; /* min percent anon pages */
int vtextminpct;/* min percent vtext pages */
int vnodeminpct;/* min percent vnode pages */
/* swap */
int nswapdev;   /* number of configured swap devices in system */
int swpages;    /* number of PAGE_SIZE'ed swap pages */
int swpginuse;  /* number of swap pages in use */
int swpgonly;   /* number of swap pages in use, not also in RAM */
int nswget;     /* number of swap pages moved from disk to RAM */
int nanon;      /* XXX number total of anon's in system */
int unused05;   /* formerly nanonneeded */
int unused06;   /* formerly nfreeanon */
/* stat counters */
int faults;             /* page fault count */
int traps;              /* trap count */
int intrs;              /* interrupt count */
int swtch;              /* context switch count */
int softs;              /* software interrupt count */
int syscalls;           /* system calls */
int pageins;            /* pagein operation count */
                        /* pageouts are in pdpageouts below */
int unused07;           /* formerly obsolete_swapins */
int unused08;           /* formerly obsolete_swapouts */
int pgswapin;           /* pages swapped in */
int pgswapout;          /* pages swapped out */
int forks;              /* forks */
int forks_ppwait;       /* forks where parent waits */
int forks_sharevm;      /* forks where vmspace is shared */
int pga_zerohit;        /* pagealloc where zero wanted and zero
                                was available */
int pga_zeromiss;       /* pagealloc where zero wanted and zero
                                not available */
int unused09;           /* formerly zeroaborts */
/* fault subcounters */
int fltnoram;   /* number of times fault was out of ram */
int fltnoanon;  /* number of times fault was out of anons */
int fltnoamap;  /* number of times fault was out of amap chunks */
int fltpgwait;  /* number of times fault had to wait on a page */
int fltpgrele;  /* number of times fault found a released page */
int fltrelck;   /* number of times fault relock called */
int fltrelckok; /* number of times fault relock is a success */
int fltanget;   /* number of times fault gets anon page */
int fltanretry; /* number of times fault retrys an anon get */
int fltamcopy;  /* number of times fault clears "needs copy" */
int fltnamap;   /* number of times fault maps a neighbor anon page */
int fltnomap;   /* number of times fault maps a neighbor obj page */
int fltlget;    /* number of times fault does a locked pgo_get */
int fltget;     /* number of times fault does an unlocked get */
int flt_anon;   /* number of times fault anon (case 1a) */
int flt_acow;   /* number of times fault anon cow (case 1b) */
int flt_obj;    /* number of times fault is on object page (2a) */
int flt_prcopy; /* number of times fault promotes with copy (2b) */
int flt_przero; /* number of times fault promotes with zerofill (2b) */
/* daemon counters */
int pdwoke;     /* number of times daemon woke up */
int pdrevs;     /* number of times daemon rev'd clock hand */
int pdswout;    /* number of times daemon called for swapout */
int pdfreed;    /* number of pages daemon freed since boot */
int pdscans;    /* number of pages daemon scanned since boot */
int pdanscan;   /* number of anonymous pages scanned by daemon */
int pdobscan;   /* number of object pages scanned by daemon */
int pdreact;    /* number of pages daemon reactivated since boot */
int pdbusy;     /* number of times daemon found a busy page */
int pdpageouts; /* number of times daemon started a pageout */
int pdpending;  /* number of times daemon got a pending pagout */
int pddeact;    /* number of pages daemon deactivates */
int unused11;   /* formerly pdreanon */
int unused12;   /* formerly pdrevnode */
int unused13;   /* formerly pdrevtext */
int fpswtch;    /* FPU context switches */
int kmapent;    /* number of kernel map entries */
πŸ’€ not the graveyard of unused01 through unused13 β€” every one of those is a fossil of something that used to live there (anonpages, nanonneeded, nfreeanon, obsolete_swapins, obsolete_swapouts, zeroaborts, pdreanon, pdrevnode, pdrevtext). respect the ghosts πŸ•―οΈ

πŸ¦‹ uvm_grow()

uvm_grow() increases the stack segment of process p so it includes sp. stack go brrr πŸ“ˆ


πŸ’œ uvm_coredump_walkmap()

uvm_coredump_walkmap() is the one that supports writing out the memory image of a process into a core file. Here's the tea:

It walks the address space for process p and counts how many segments are necessary to hold the dumpable memory β€” and a segment means either one or more present pages followed by zero or more mapped but not present (zero-fill) pages, or just one or more mapped but not present pages. Then it invokes setup with that segment count and the cookie argument.

typedef int uvm_coredump_setup_cb(int nsegment, void *cookie);

If that returns non-zero, then uvm_coredump_walkmap() returns that value immediately β€” no further questions, we're done here. Otherwise it invokes walk once for each segment, in ascending address order, passing it:

  • the start of the segment
  • the start of the mapped-but-not-present pages
  • one past the last address in the segment
  • the protection on the segment
  • the index of the segment
  • the cookie
typedef int uvm_coredump_walk_cb(vaddr_t start, vaddr_t realend,
    vaddr_t end, vm_prot_t prot, int nsegment, void *cookie);

And if a call to walk returns non-zero, then uvm_coredump_walkmap returns that value immediately too. same energy, main character exit πŸšͺ✨


πŸŽ€ NOTES πŸŽ€

Fun lore drop: the structure and types whose names begin with vm_ were named that way so UVM could coexist with BSD VM during the early development stages. historical bestie behaviour, honestly 🫢


🌈 SEE ALSO 🌈

Charles D. Cranor, Design and Implementation of the UVM Virtual Memory System, D.Sc. dissertation, Department of Computer Science, Sever Institute of Technology, Washington University, St. Louis, Missouri, August 1998.

πŸ“š yes bestie, a whole dissertation. the receipts are real.

🌸 HISTORY 🌸

The UVM virtual memory system was developed at Washington University in St. Louis. Its roots lie partly in the Mach-based 4.4BSD VM system, the FreeBSD VM system, and the SunOS4 VM system. UVM's basic structure is based on the 4.4BSD VM system. UVM's new anonymous memory system is based on the anonymous memory system found in the SunOS4 VM (as described in papers published by Sun Microsystems, Inc.).

UVM also includes a number of features new to BSD, including:

  • page loanout
  • map entry passing
  • simplified copy-on-write
  • clustered anonymous memory pageout

UVM appeared in OpenBSD 2.9. periodt. ✨


πŸ‘‘ AUTHORS πŸ‘‘

  • Charles D. Cranor <chuck@ccrc.wustl.edu> β€” designed and implemented UVM.
  • Matthew Green <mrg@eterna.com.au> β€” wrote the swap-space management code.
  • Chuck Silvers <chuq@chuq.com> β€” implemented the aobj pager, thus allowing UVM to support System V shared memory and process swapping.
  • Artur Grabowski <art@openbsd.org> β€” handled the logistical issues involved with merging UVM into the OpenBSD source tree.

okay that's the whole page, you're officially UVM-literate πŸ’…βœ¨ this slice came from the Debian build, dated June 21, 2023, and it's the UVM_INIT(9) man page β€” go forth and understand your kernel, bestie, i'm so proud of you πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from