xref: /netbsd-src/share/man/man9/uvm.9 (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1.\"	$NetBSD: uvm.9,v 1.30 2002/01/05 20:35:49 deberg Exp $
2.\"
3.\" Copyright (c) 1998 Matthew R. Green
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. The name of the author may not be used to endorse or promote products
15.\"    derived from this software without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.\" XXX this manual sets nS to 1 or 0 in the description, to obtain
30.\" synopsis-like function prototypes.  any better way?
31.\"
32.Dd December 24, 2001
33.Dt UVM 9
34.Os
35.Sh NAME
36.Nm uvm
37.Nd virtual memory system external interface
38.Sh SYNOPSIS
39.Fd #include <sys/param.h>
40.Fd #include <uvm/uvm.h>
41.Sh DESCRIPTION
42The UVM virtual memory system manages access to the computer's memory
43resources.  User processes and the kernel access these resources through
44UVM's external interface.  UVM's external interface includes functions that:
45.Pp
46.Bl -hyphen -compact
47.It
48initialise UVM sub-systems
49.It
50manage virtual address spaces
51.It
52resolve page faults
53.It
54memory map files and devices
55.It
56perform uio-based I/O to virtual memory
57.It
58allocate and free kernel virtual memory
59.It
60allocate and free physical memory
61.El
62.Pp
63In addition to exporting these services, UVM has two kernel-level processes:
64pagedaemon and swapper.   The pagedaemon process sleeps until physical memory
65becomes scarce.  When that happens, pagedaemon is awoken.   It scans physical
66memory, paging out and freeing memory that has not been recently used.  The
67swapper process swaps in runnable processes that are currently swapped out,
68if there is room.
69.Pp
70There are also several miscellaneous functions.
71.Sh INITIALISATION
72.nr nS 1
73.Pp
74.Ft void
75.Fn uvm_init "void"
76.Ft void
77.Fn uvm_init_limits "struct proc *p"
78.Ft void
79.Fn uvm_setpagesize "void"
80.Ft void
81.Fn uvm_swap_init "void"
82.nr nS 0
83.Pp
84.Fn uvm_init
85sets up the UVM system at system boot time, after the
86copyright has been printed.  It initialises
87global state, the page, map, kernel virtual memory state,
88machine-dependent physical map, kernel memory allocator,
89pager and anonymous memory sub-systems, and then enables
90paging of kernel objects.
91.Pp
92.Fn uvm_init_limits
93initialises process limits for the named process.  This is for use by
94the system startup for process zero, before any other processes are
95created.
96.Pp
97.Fn uvm_setpagesize
98initialises the uvmexp members pagesize (if not already done by
99machine-dependent code), pageshift and pagemask.  It should be called by
100machine-dependent code early in the
101.Fn pmap_init
102call (see
103.Xr pmap 9 ) .
104.Pp
105.Fn uvm_swap_init
106initialises the swap sub-system.
107.Sh VIRTUAL ADDRESS SPACE MANAGEMENT
108.nr nS 1
109.Ft int
110.Fn uvm_map "struct vm_map *map" "vaddr_t *startp" "vsize_t size" "struct uvm_object *uobj" "voff_t uoffset" "uvm_flag_t flags"
111.Ft int
112.Fn uvm_map_pageable "struct vm_map *map" "vaddr_t start" "vaddr_t end" "boolean_t new_pageable" "int lockflags"
113.Ft boolean_t
114.Fn uvm_map_checkprot "struct vm_map *map" "vaddr_t start" "vaddr_t end" "vm_prot_t protection"
115.Ft int
116.Fn uvm_map_protect "struct vm_map *map" "vaddr_t start" "vaddr_t end" "vm_prot_t new_prot" "boolean_t set_max"
117.Ft int
118.Fn uvm_deallocate "struct vm_map *map" "vaddr_t start" "vsize_t size"
119.Pp
120.Ft struct vmspace *
121.Fn uvmspace_alloc "vaddr_t min" "vaddr_t max" "int pageable"
122.Ft void
123.Fn uvmspace_exec "struct proc *p" "vaddr_t start" "vaddr_t end"
124.Ft struct vmspace *
125.Fn uvmspace_fork "struct vmspace *vm"
126.Ft void
127.Fn uvmspace_free "struct vmspace *vm1"
128.Ft void
129.Fn uvmspace_share "struct proc *p1" "struct proc *p2"
130.Ft void
131.Fn uvmspace_unshare "struct proc *p"
132.nr nS 0
133.Pp
134.Fn uvm_map
135establishes a valid mapping in map
136.Fa map ,
137which must be unlocked.  The new mapping has size
138.Fa size ,
139which must be in
140.Dv PAGE_SIZE
141units.  The
142.Fa uobj
143and
144.Fa uoffset
145arguments can have four meanings.  When
146.Fa uobj
147is
148.Dv NULL
149and
150.Fa uoffset
151is
152.Dv UVM_UNKNOWN_OFFSET ,
153.Fn uvm_map
154does not use the machine-dependent
155.Dv PMAP_PREFER
156function.  If
157.Fa uoffset
158is any other value, it is used as the hint to
159.Dv PMAP_PREFER .
160When
161.Fa uobj
162is not
163.Dv NULL
164and
165.Fa uoffset
166is
167.Dv UVM_UNKNOWN_OFFSET ,
168.Fn uvm_map
169finds the offset based upon the virtual address, passed as
170.Fa startp .
171If
172.Fa uoffset
173is any other value, we are doing a normal mapping at this offset.  The
174start address of the map will be returned in
175.Fa startp .
176.Pp
177.Fa flags
178passed to
179.Fn uvm_map
180are typically created using the
181.Fn UVM_MAPFLAG "vm_prot_t prot" "vm_prot_t maxprot" "vm_inherit_t inh" "int advice" "int flags"
182macro, which uses the following values.
183The
184.Fa prot
185and
186.Fa maxprot
187can take are:
188.Bd -literal
189#define UVM_PROT_MASK   0x07    /* protection mask */
190#define UVM_PROT_NONE   0x00    /* protection none */
191#define UVM_PROT_ALL    0x07    /* everything */
192#define UVM_PROT_READ   0x01    /* read */
193#define UVM_PROT_WRITE  0x02    /* write */
194#define UVM_PROT_EXEC   0x04    /* exec */
195#define UVM_PROT_R      0x01    /* read */
196#define UVM_PROT_W      0x02    /* write */
197#define UVM_PROT_RW     0x03    /* read-write */
198#define UVM_PROT_X      0x04    /* exec */
199#define UVM_PROT_RX     0x05    /* read-exec */
200#define UVM_PROT_WX     0x06    /* write-exec */
201#define UVM_PROT_RWX    0x07    /* read-write-exec */
202.Ed
203.Pp
204The values that
205.Fa inh
206can take are:
207.Bd -literal
208#define UVM_INH_MASK    0x30    /* inherit mask */
209#define UVM_INH_SHARE   0x00    /* "share" */
210#define UVM_INH_COPY    0x10    /* "copy" */
211#define UVM_INH_NONE    0x20    /* "none" */
212#define UVM_INH_DONATE  0x30    /* "donate" << not used */
213.Ed
214.Pp
215The values that
216.Fa advice
217can take are:
218.Bd -literal
219#define UVM_ADV_NORMAL     0x0  /* 'normal' */
220#define UVM_ADV_RANDOM     0x1  /* 'random' */
221#define UVM_ADV_SEQUENTIAL 0x2  /* 'sequential' */
222#define UVM_ADV_MASK       0x7  /* mask */
223.Ed
224.Pp
225The values that
226.Fa flags
227can take are:
228.Bd -literal
229#define UVM_FLAG_FIXED   0x010000 /* find space */
230#define UVM_FLAG_OVERLAY 0x020000 /* establish overlay */
231#define UVM_FLAG_NOMERGE 0x040000 /* don't merge map entries */
232#define UVM_FLAG_COPYONW 0x080000 /* set copy_on_write flag */
233#define UVM_FLAG_AMAPPAD 0x100000 /* for bss: pad amap to reduce malloc() */
234#define UVM_FLAG_TRYLOCK 0x200000 /* fail if we can not lock map */
235.Ed
236.Pp
237The
238.Dv UVM_MAPFLAG
239macro arguments can be combined with an or operator.  There are
240several special purpose macros for checking protection combinations, e.g. the
241.Dv UVM_PROT_WX
242macro.  There are also some additional macros to extract bits from
243the flags.  The
244.Dv UVM_PROTECTION ,
245.Dv UVM_INHERIT ,
246.Dv UVM_MAXPROTECTION
247and
248.Dv UVM_ADVICE
249macros return the protection, inheritance, maximum protection and advice,
250respectively.
251.Fn uvm_map
252returns a standard UVM return value.
253.Pp
254.Fn uvm_map_pageable
255changes the pageability of the pages in the range from
256.Fa start
257to
258.Fa end
259in map
260.Fa map
261to
262.Fa new_pageable .
263.Fn uvm_map_pageable
264returns a standard UVM return value.
265.Pp
266.Fn uvm_map_checkprot
267checks the protection of the range from
268.Fa start
269to
270.Fa end
271in map
272.Fa map
273against
274.Fa protection .
275This returns either
276.Dv TRUE
277or
278.Dv FALSE .
279.Pp
280.Fn uvm_map_protect
281changes the protection
282.Fa start
283to
284.Fa end
285in map
286.Fa map
287to
288.Fa new_prot ,
289also setting the maximum protection to the region to
290.Fa new_prot
291if
292.Fa set_max
293is non-zero.  This function returns a standard UVM return value.
294.Pp
295.Fn uvm_deallocate
296deallocates kernel memory in map
297.Fa map
298from address
299.Fa start
300to
301.Fa start + size .
302.Pp
303.Fn uvmspace_alloc
304allocates and returns a new address space, with ranges from
305.Fa min
306to
307.Fa max ,
308setting the pageability of the address space to
309.Fa pageable .
310.Pp
311.Fn uvmspace_exec
312either reuses the address space of process
313.Fa p
314if there are no other references to it, or creates
315a new one with
316.Fn uvmspace_alloc .
317The range of valid addresses in the address space is reset to
318.Fa start
319through
320.Fa end .
321.Pp
322.Fn uvmspace_fork
323creates and returns a new address space based upon the
324.Fa vm1
325address space, typically used when allocating an address space for a
326child process.
327.Pp
328.Fn uvmspace_free
329lowers the reference count on the address space
330.Fa vm ,
331freeing the data structures if there are no other references.
332.Pp
333.Fn uvmspace_share
334causes process
335.Pa p2
336to share the address space of
337.Fa p1 .
338.Pp
339.Fn uvmspace_unshare
340ensures that process
341.Fa p
342has its own, unshared address space, by creating a new one if
343necessary by calling
344.Fn uvmspace_fork .
345.Sh PAGE FAULT HANDLING
346.nr nS 1
347.Ft int
348.Fn uvm_fault "struct vm_map *orig_map" "vaddr_t vaddr" "vm_fault_t fault_type" "vm_prot_t access_type"
349.nr nS 0
350.Pp
351.Fn uvm_fault
352is the main entry point for faults.  It takes
353.Fa orig_map
354as the map the fault originated in, a
355.Fa vaddr
356offset into the map the fault occurred,
357.Fa fault_type
358describing the type of fault, and
359.Fa access_type
360describing the type of access requested.
361.Fn uvm_fault
362returns a standard UVM return value.
363.Sh MEMORY MAPPING FILES AND DEVICES
364.nr nS 1
365.Ft struct uvm_object *
366.Fn uvn_attach "void *arg" "vm_prot_t accessprot"
367.Ft void
368.Fn uvm_vnp_setsize "struct vnode *vp" "voff_t newsize"
369.Ft void *
370.Fn ubc_alloc "struct uvm_object *uobj" "voff_t offset" "vsize_t *lenp" "int flags"
371.Ft void
372.Fn ubc_release "void *va" "int flags"
373.nr nS 0
374.Pp
375.Fn uvn_attach
376attaches a UVM object to vnode
377.Fa arg ,
378creating the object if necessary.  The object is returned.
379.Pp
380.Fn uvm_vnp_setsize
381sets the size of vnode
382.Fa vp
383to
384.Fa newsize .
385Caller must hold a reference to the vnode.  If the vnode shrinks, pages
386no longer used are discarded.
387.Pp
388.Fn ubc_alloc
389creates a kernel mappings of
390.Fa uobj
391starting at offset
392.Fa offset .
393the desired length of the mapping is pointed to by
394.Fa lenp ,
395but the actual mapping may be smaller than this.
396.Fa lenp
397is updated to contain the actual length mapped.
398The flags must be one of
399.Bd -literal
400#define UBC_READ        0x01    /* mapping will be accessed for read */
401#define UBC_WRITE       0x02    /* mapping will be accessed for write */
402.Ed
403.Pp
404Currently,
405.Fa uobj
406must actually be a vnode object.
407Once the mapping is created, it must be accessed only by methods that can
408handle faults, such as
409.Fn uiomove
410or
411.Fn kcopy .
412Page faults on the mapping will result in the vnode's
413.Fn VOP_GETPAGES
414method being called to resolve the fault.
415.Pp
416.Fn ubc_release
417frees the mapping at
418.Fa va
419for reuse.  The mapping may be cached to speed future accesses to the same
420region of the object.  The flags are currently unused.
421.Sh VIRTUAL MEMORY I/O
422.nr nS 1
423.Ft int
424.Fn uvm_io "struct vm_map *map" "struct uio *uio"
425.nr nS 0
426.Pp
427.Fn uvm_io
428performs the I/O described in
429.Fa uio
430on the memory described in
431.Fa map .
432.Sh ALLOCATION OF KERNEL MEMORY
433.nr nS 1
434.Ft vaddr_t
435.Fn uvm_km_alloc "struct vm_map *map" "vsize_t size"
436.Ft vaddr_t
437.Fn uvm_km_zalloc "struct vm_map *map" "vsize_t size"
438.Ft vaddr_t
439.Fn uvm_km_alloc1 "struct vm_map *map" "vsize_t size" "boolean_t zeroit"
440.Ft vaddr_t
441.Fn uvm_km_kmemalloc "struct vm_map *map" "struct uvm_object *obj" "vsize_t size" "int flags"
442.Ft vaddr_t
443.Fn uvm_km_valloc "struct vm_map *map" "vsize_t size"
444.Ft vaddr_t
445.Fn uvm_km_valloc_wait "struct vm_map *map" "vsize_t size"
446.Ft struct vm_map *
447.Fn uvm_km_suballoc "struct vm_map *map" "vaddr_t *min" "vaddr_t *max " "vsize_t size" "boolean_t pageable" "boolean_t fixed" "struct vm_map *submap"
448.Ft void
449.Fn uvm_km_free "struct vm_map *map" "vaddr_t addr" "vsize_t size"
450.Ft void
451.Fn uvm_km_free_wakeup "struct vm_map *map" "vaddr_t addr" "vsize_t size"
452.nr nS 0
453.Pp
454.Fn uvm_km_alloc
455and
456.Fn uvm_km_zalloc
457allocate
458.Fa size
459bytes of wired kernel memory in map
460.Fa map .
461In addition to allocation,
462.Fn uvm_km_zalloc
463zeros the memory.  Both of these functions are defined as macros in
464terms of
465.Fn uvm_km_alloc1 ,
466and should almost always be used in preference to
467.Fn uvm_km_alloc1 .
468.Pp
469.Fn uvm_km_alloc1
470allocates and returns
471.Fa size
472bytes of wired memory in the kernel map, zeroing the memory if the
473.Fa zeroit
474argument is non-zero.
475.Pp
476.Fn uvm_km_kmemalloc
477allocates and returns
478.Fa size
479bytes of wired kernel memory into
480.Fa obj .
481The flags can be any of:
482.Bd -literal
483#define UVM_KMF_NOWAIT  0x1                     /* matches M_NOWAIT */
484#define UVM_KMF_VALLOC  0x2                     /* allocate VA only */
485#define UVM_KMF_TRYLOCK UVM_FLAG_TRYLOCK        /* try locking only */
486.Ed
487.Pp
488.Dv UVM_KMF_NOWAIT
489causes
490.Fn uvm_km_kmemalloc
491to return immediately if no memory is available.
492.Dv UVM_KMF_VALLOC
493causes no pages to be allocated, only a virtual address.
494.Dv UVM_KMF_TRYLOCK
495causes
496.Fn uvm_km_kmemalloc
497to use
498.Fn simple_lock_try
499when locking maps.
500.Pp
501.Fn uvm_km_valloc
502and
503.Fn uvm_km_valloc_wait
504return a newly allocated zero-filled address in the kernel map of size
505.Fa size .
506.Fn uvm_km_valloc_wait
507will also wait for kernel memory to become available, if there is a
508memory shortage.
509.Pp
510.Fn uvm_km_free
511and
512.Fn uvm_km_free_wakeup
513free
514.Fa size
515bytes of memory in the kernel map, starting at address
516.Fa addr .
517.Fn uvm_km_free_wakeup
518calls
519.Fn wakeup
520on the map before unlocking the map.
521.Pp
522.Fn uvm_km_suballoc
523allocates submap from
524.Fa map ,
525creating a new map if
526.Fa submap
527is
528.Dv NULL .
529The addresses of the submap can be specified exactly by setting the
530.Fa fixed
531argument to non-zero, which causes the
532.Fa min
533argument specify the beginning of the address in the submap.  If
534.Fa fixed
535is zero, any address of size
536.Fa size
537will be allocated from
538.Fa map
539and the start and end addresses returned in
540.Fa min
541and
542.Fa max .
543If
544.Fa pageable
545is non-zero, entries in the map may be paged out.
546.Sh ALLOCATION OF PHYSICAL MEMORY
547.nr nS 1
548.Ft struct vm_page *
549.Fn uvm_pagealloc "struct uvm_object *uobj" "voff_t off" "struct vm_anon *anon" "int flags"
550.Ft void
551.Fn uvm_pagerealloc "struct vm_page *pg" "struct uvm_object *newobj" "voff_t newoff"
552.Ft void
553.Fn uvm_pagefree "struct vm_page *pg"
554.Ft int
555.Fn uvm_pglistalloc "psize_t size" "paddr_t low" "paddr_t high" "paddr_t alignment" "paddr_t boundary" "struct pglist *rlist" "int nsegs" "int waitok"
556.Ft void
557.Fn uvm_pglistfree "struct pglist *list"
558.Ft void
559.Fn uvm_page_physload "vaddr_t start" "vaddr_t end" "vaddr_t avail_start" "vaddr_t avail_end"
560.nr nS 0
561.Pp
562.Fn uvm_pagealloc
563allocates a page of memory at virtual address
564.Fa off
565in either the object
566.Fa uobj
567or the anonymous memory
568.Fa anon ,
569which must be locked by the caller.
570Only one of
571.Fa uobj
572and
573.Fa anon
574can be non
575.Dv NULL .
576Returns
577.Dv NULL
578when no page can be found.
579The flags can be any of
580.Bd -literal
581#define UVM_PGA_USERESERVE      0x0001  /* ok to use reserve pages */
582#define UVM_PGA_ZERO            0x0002  /* returned page must be zero'd */
583.Ed
584.Pp
585.Dv UVM_PGA_USERESERVE
586means to allocate a page even if that will result in the number of free pages
587being lower than
588.Dv uvmexp.reserve_pagedaemon
589(if the current thread is the pagedaemon) or
590.Dv uvmexp.reserve_kernel
591(if the current thread is not the pagedaemon).
592.Dv UVM_PGA_ZERO
593causes the returned page to be filled with zeroes, either by allocating it
594from a pool of pre-zeroed pages or by zeroing it in-line as necessary.
595.Pp
596.Fn uvm_pagerealloc
597reallocates page
598.Fa pg
599to a new object
600.Fa newobj ,
601at a new offset
602.Fa newoff .
603.Pp
604.Fn uvm_pagefree
605frees the physical page
606.Fa pg .
607.Pp
608.Fn uvm_pglistalloc
609allocates a list of pages for size
610.Fa size
611byte under various constraints.
612.Fa low
613and
614.Fa high
615describe the lowest and highest addresses acceptable for the list.  If
616.Fa alignment
617is non-zero, it describes the required alignment of the list, in
618power-of-two notation.  If
619.Fa boundary
620is non-zero, no segment of the list may cross this power-of-two
621boundary, relative to zero.
622The
623.Fa nsegs
624and
625.Fa waitok
626arguments are currently ignored.
627.Pp
628.Fn uvm_pglistfree
629frees the list of pages pointed to by
630.Fa list .
631.Pp
632.Fn uvm_page_physload
633loads physical memory segments into VM space.  It must be called at system
634boot time to setup physical memory management pages.  The arguments describe
635the
636.Fa start
637and
638.Fa end
639of the physical addresses of the segment, and the available start and end
640addresses of pages not already in use.
641.\" XXX expand on "system boot time"!
642.Sh PROCESSES
643.nr nS 1
644.Ft void
645.Fn uvm_pageout "void"
646.Ft void
647.Fn uvm_scheduler "void"
648.Ft void
649.Fn uvm_swapin "struct proc *p"
650.nr nS 0
651.Pp
652.Fn uvm_pageout
653is the main loop for the page daemon.
654.Pp
655.Fn uvm_scheduler
656is the process zero main loop, which is to be called after the
657system has finished starting other processes.  It handles the
658swapping in of runnable, swapped out processes in priority
659order.
660.Pp
661.Fn uvm_swapin
662swaps in the named process.
663.Sh PAGE LOAN
664.nr nS 1
665.Ft int
666.Fn uvm_loan "struct vm_map *map" "vaddr_t start" "vsize_t len" "void *v" "int flags"
667.Ft void
668.Fn uvm_unloan "void *v" "int npages" "int flags"
669.nr nS 0
670.Pp
671.Fn uvm_loan
672loans pages in a map out to anons or to the kernel.
673.Fa map
674should be unlocked ,
675.Fa start
676and
677.Fa len
678should be multiples of
679.Dv PAGE_SIZE .
680Argument
681.Fa flags
682should be one of
683.Bd -literal
684#define UVM_LOAN_TOANON       0x01    /* loan to anons */
685#define UVM_LOAN_TOPAGE       0x02    /* loan to kernel */
686.Ed
687.Pp
688.Fa v
689should be pointer to array of pointers to
690.Li struct anon
691or
692.Li struct vm_page ,
693as appropriate. The caller has to allocate memory for the array and
694ensure it's big enough to hold
695.Fa len / PAGE_SIZE
696pointers.
697Returns 0 for success, or appropriate error number otherwise.
698.Pp
699.Fn uvm_unloan
700kills loans on pages or anons. The
701.Fa v
702must point to the array of pointers initialized by previous call to
703.Fn uvm_loan .
704.Fa npages
705should match number of pages allocated for loan, this also matches
706number of items in the array.
707Argument
708.Fa flags
709should be one of
710.Bd -literal
711#define UVM_LOAN_TOANON       0x01    /* loan to anons */
712#define UVM_LOAN_TOPAGE       0x02    /* loan to kernel */
713.Ed
714.Pp
715and should match what was used for previous call to
716.Fn uvm_loan .
717.Sh MISCELLANEOUS FUNCTIONS
718.nr nS 1
719.Ft struct uvm_object *
720.Fn uao_create "vsize_t size" "int flags"
721.Ft void
722.Fn uao_detach "struct uvm_object *uobj"
723.Ft void
724.Fn uao_reference "struct uvm_object *uobj"
725.Pp
726.Ft boolean_t
727.Fn uvm_chgkprot "caddr_t addr" "size_t len" "int rw"
728.Ft void
729.Fn uvm_kernacc "caddr_t addr" "size_t len" "int rw"
730.Ft boolean_t
731.Fn uvm_useracc "caddr_t addr" "size_t len" "int rw"
732.Pp
733.Ft int
734.Fn uvm_vslock "struct proc *p" "caddr_t addr" "size_t len" "vm_prot_t prot"
735.Ft void
736.Fn uvm_vsunlock "struct proc *p" "caddr_t addr" "size_t len"
737.Pp
738.Ft void
739.Fn uvm_meter "void"
740.Ft int
741.Fn uvm_sysctl "int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "void *newp " "size_t newlen" "struct proc *p"
742.Pp
743.Ft void
744.Fn uvm_fork "struct proc *p1" "struct proc *p2" "boolean_t shared"
745.Ft int
746.Fn uvm_grow "struct proc *p" "vaddr_t sp"
747.Ft int
748.Fn uvm_coredump "struct proc *p" "struct vnode *vp" "struct ucred *cred" "struct core *chdr"
749.Pp
750.Ft void
751.Fn uvn_findpages "struct uvm_object *uobj" "voff_t offset" "int *npagesp" "struct vm_page **pps" "int flags"
752.Pp
753.nr nS 0
754.Pp
755The
756.Fn uao_create ,
757.Fn uao_detach
758and
759.Fn uao_reference
760functions operate on anonymous memory objects, such as those used to support
761System V shared memory.
762.Fn uao_create
763returns an object of size
764.Fa size
765with flags:
766.Bd -literal
767#define UAO_FLAG_KERNOBJ        0x1     /* create kernel object */
768#define UAO_FLAG_KERNSWAP       0x2     /* enable kernel swap */
769.Ed
770.Pp
771which can only be used once each at system boot time.
772.Fn uao_reference
773creates an additional reference to the named anonymous memory object.
774.Fn uao_detach
775removes a reference from the named anonymous memory object, destroying
776it if removing the last reference.
777.Pp
778.Fn uvm_chgkprot
779changes the protection of kernel memory from
780.Fa addr
781to
782.Fa addr + len
783to the value of
784.Fa rw .
785This is primarily useful for debuggers, for setting breakpoints.
786This function is only available with options
787.Dv KGDB .
788.Pp
789.Fn uvm_kernacc
790and
791.Fn uvm_useracc
792check the access at address
793.Fa addr
794to
795.Fa addr + len
796for
797.Fa rw
798access, in the kernel address space, and the current process'
799address space respectively.
800.Pp
801.Fn uvm_vslock
802and
803.Fn uvm_vsunlock
804control the wiring and unwiring of pages for process
805.Fa p
806from
807.Fa addr
808to
809.Fa addr + len .
810These functions are normally used to wire memory for I/O.
811.Pp
812.Fn uvm_meter
813calculates the load average and wakes up the swapper if necessary.
814.Pp
815.Fn uvm_sysctl
816provides support for the
817.Dv CTL_VM
818domain of the
819.Xr sysctl 3
820hierarchy.
821.Fn uvm_sysctl
822handles the
823.Dv VM_LOADAVG ,
824.Dv VM_METER
825and
826.Dv VM_UVMEXP
827calls, which return the current load averages, calculates current VM
828totals, and returns the uvmexp structure respectively.  The load averages
829are access from userland using the
830.Xr getloadavg 3
831function.  The uvmexp structure has all global state of the UVM system,
832and has the following members:
833.Bd -literal
834/* vm_page constants */
835int pagesize;   /* size of a page (PAGE_SIZE): must be power of 2 */
836int pagemask;   /* page mask */
837int pageshift;  /* page shift */
838
839/* vm_page counters */
840int npages;     /* number of pages we manage */
841int free;       /* number of free pages */
842int active;     /* number of active pages */
843int inactive;   /* number of pages that we free'd but may want back */
844int paging;     /* number of pages in the process of being paged out */
845int wired;      /* number of wired pages */
846int reserve_pagedaemon; /* number of pages reserved for pagedaemon */
847int reserve_kernel; /* number of pages reserved for kernel */
848
849/* pageout params */
850int freemin;    /* min number of free pages */
851int freetarg;   /* target number of free pages */
852int inactarg;   /* target number of inactive pages */
853int wiredmax;   /* max number of wired pages */
854
855/* swap */
856int nswapdev;   /* number of configured swap devices in system */
857int swpages;    /* number of PAGE_SIZE'ed swap pages */
858int swpginuse;  /* number of swap pages in use */
859int nswget;     /* number of times fault calls uvm_swap_get() */
860int nanon;      /* number total of anon's in system */
861int nfreeanon;  /* number of free anon's */
862
863/* stat counters */
864int faults;             /* page fault count */
865int traps;              /* trap count */
866int intrs;              /* interrupt count */
867int swtch;              /* context switch count */
868int softs;              /* software interrupt count */
869int syscalls;           /* system calls */
870int pageins;            /* pagein operation count */
871                        /* pageouts are in pdpageouts below */
872int swapins;            /* swapins */
873int swapouts;           /* swapouts */
874int pgswapin;           /* pages swapped in */
875int pgswapout;          /* pages swapped out */
876int forks;              /* forks */
877int forks_ppwait;       /* forks where parent waits */
878int forks_sharevm;      /* forks where vmspace is shared */
879
880/* fault subcounters */
881int fltnoram;   /* number of times fault was out of ram */
882int fltnoanon;  /* number of times fault was out of anons */
883int fltpgwait;  /* number of times fault had to wait on a page */
884int fltpgrele;  /* number of times fault found a released page */
885int fltrelck;   /* number of times fault relock called */
886int fltrelckok; /* number of times fault relock is a success */
887int fltanget;   /* number of times fault gets anon page */
888int fltanretry; /* number of times fault retrys an anon get */
889int fltamcopy;  /* number of times fault clears "needs copy" */
890int fltnamap;   /* number of times fault maps a neighbor anon page */
891int fltnomap;   /* number of times fault maps a neighbor obj page */
892int fltlget;    /* number of times fault does a locked pgo_get */
893int fltget;     /* number of times fault does an unlocked get */
894int flt_anon;   /* number of times fault anon (case 1a) */
895int flt_acow;   /* number of times fault anon cow (case 1b) */
896int flt_obj;    /* number of times fault is on object page (2a) */
897int flt_prcopy; /* number of times fault promotes with copy (2b) */
898int flt_przero; /* number of times fault promotes with zerofill (2b) */
899
900/* daemon counters */
901int pdwoke;     /* number of times daemon woke up */
902int pdrevs;     /* number of times daemon rev'd clock hand */
903int pdswout;    /* number of times daemon called for swapout */
904int pdfreed;    /* number of pages daemon freed since boot */
905int pdscans;    /* number of pages daemon scanned since boot */
906int pdanscan;   /* number of anonymous pages scanned by daemon */
907int pdobscan;   /* number of object pages scanned by daemon */
908int pdreact;    /* number of pages daemon reactivated since boot */
909int pdbusy;     /* number of times daemon found a busy page */
910int pdpageouts; /* number of times daemon started a pageout */
911int pdpending;  /* number of times daemon got a pending pageout */
912int pddeact;    /* number of pages daemon deactivates */
913.Ed
914.Pp
915.Fn uvm_fork
916forks a virtual address space for process' (old)
917.Fa p1
918and (new)
919.Fa p2 .
920If the
921.Fa shared
922argument is non zero, p1 shares its address space with p2,
923otherwise a new address space is created.  This function
924currently has no return value, and thus cannot fail.  In
925the future, this function will be changed to allow it to
926fail in low memory conditions.
927.Pp
928.Fn uvm_grow
929increases the stack segment of process
930.Fa p
931to include
932.Fa sp .
933.Pp
934.Fn uvm_coredump
935generates a coredump on vnode
936.Fa vp
937for process
938.Fa p
939with credentials
940.Fa cred
941and core header description in
942.Fa chdr .
943.Pp
944.Fn uvn_findpages
945looks up or creates pages in
946.Fa uobj
947at offset
948.Fa offset ,
949marks them busy and returns them in the
950.Fa pps
951array.
952Currently
953.Fa uobj
954must be a vnode object.
955The number of pages requested is pointed to by
956.Fa npagesp ,
957and this value is updated with the actual number of pages returned.
958The flags can be
959.Bd -literal
960#define UFP_ALL         0x00    /* return all pages requested */
961#define UFP_NOWAIT      0x01    /* don't sleep */
962#define UFP_NOALLOC     0x02    /* don't allocate new pages */
963#define UFP_NOCACHE     0x04    /* don't return pages which already exist */
964#define UFP_NORDONLY    0x08    /* don't return PG_READONLY pages */
965.Ed
966.Pp
967.Dv UFP_ALL
968is a pseudo-flag meaning all requested pages should be returned.
969.Dv UFP_NOWAIT
970means that we must not sleep.
971.Dv UFP_NOALLOC
972causes any pages which do not already exist to be skipped.
973.Dv UFP_NOCACHE
974causes any pages which do already exist to be skipped.
975.Dv UFP_NORDONLY
976causes any pages which are marked PG_READONLY to be skipped.
977.Sh NOTES
978.Fn uvm_chgkprot
979is only available if the kernel has been compiled with options
980.Dv KGDB .
981.Pp
982All structure and types whose names begin with
983.Dq vm_
984will be renamed to
985.Dq uvm_ .
986.Sh SEE ALSO
987.Xr getloadavg 3 ,
988.Xr kvm 3 ,
989.Xr sysctl 3 ,
990.Xr ddb 4 ,
991.Xr options 4 ,
992.Xr pmap 9
993.Sh HISTORY
994UVM is a new VM system developed at Washington University in St. Louis
995(Missouri).  UVM's roots lie partly in the Mach-based
996.Bx 4.4
997VM system, the
998.Fx
999VM system, and the SunOS4 VM system.  UVM's basic structure is based on the
1000.Bx 4.4
1001VM system.  UVM's new anonymous memory system is based on the
1002anonymous memory system found in the SunOS4 VM (as described in papers
1003published by Sun Microsystems, Inc.).  UVM also includes a number of feature
1004new to
1005.Bx
1006including page loanout, map entry passing, simplified
1007copy-on-write, and clustered anonymous memory pageout.  UVM is also
1008further documented in a August 1998 dissertation by Charles D. Cranor.
1009.Pp
1010UVM appeared in
1011.Nx 1.4 .
1012.Sh AUTHORS
1013Charles D. Cranor <chuck@ccrc.wustl.edu> designed and implemented UVM.
1014.Pp
1015Matthew Green <mrg@eterna.com.au> wrote the swap-space management code
1016and handled the logistical issues involved with merging UVM into the
1017.Nx
1018source tree.
1019.Pp
1020Chuck Silvers <chuq@chuq.com> implemented the aobj pager, thus allowing
1021UVM to support System V shared memory and process swapping.  He also
1022designed and implemented the UBC part of UVM, which uses UVM pages to
1023cache vnode data rather than the traditional buffer cache buffers.
1024