1.\" $NetBSD: uvm.9,v 1.33 2002/03/29 08:56:31 manu 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 \*[Lt]sys/param.h\*[Gt] 40.Fd #include \*[Lt]uvm/uvm.h\*[Gt] 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" \*[Lt]\*[Lt] 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" "int free_list" 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 on the specified 634.Fa free_list . 635It must be called at system boot time to setup physical memory 636management pages. The arguments describe the 637.Fa start 638and 639.Fa end 640of the physical addresses of the segment, and the available start and end 641addresses of pages not already in use. 642.\" XXX expand on "system boot time"! 643.Sh PROCESSES 644.nr nS 1 645.Ft void 646.Fn uvm_pageout "void" 647.Ft void 648.Fn uvm_scheduler "void" 649.Ft void 650.Fn uvm_swapin "struct proc *p" 651.nr nS 0 652.Pp 653.Fn uvm_pageout 654is the main loop for the page daemon. 655.Pp 656.Fn uvm_scheduler 657is the process zero main loop, which is to be called after the 658system has finished starting other processes. It handles the 659swapping in of runnable, swapped out processes in priority 660order. 661.Pp 662.Fn uvm_swapin 663swaps in the named process. 664.Sh PAGE LOAN 665.nr nS 1 666.Ft int 667.Fn uvm_loan "struct vm_map *map" "vaddr_t start" "vsize_t len" "void *v" "int flags" 668.Ft void 669.Fn uvm_unloan "void *v" "int npages" "int flags" 670.nr nS 0 671.Pp 672.Fn uvm_loan 673loans pages in a map out to anons or to the kernel. 674.Fa map 675should be unlocked , 676.Fa start 677and 678.Fa len 679should be multiples of 680.Dv PAGE_SIZE . 681Argument 682.Fa flags 683should be one of 684.Bd -literal 685#define UVM_LOAN_TOANON 0x01 /* loan to anons */ 686#define UVM_LOAN_TOPAGE 0x02 /* loan to kernel */ 687.Ed 688.Pp 689.Fa v 690should be pointer to array of pointers to 691.Li struct anon 692or 693.Li struct vm_page , 694as appropriate. The caller has to allocate memory for the array and 695ensure it's big enough to hold 696.Fa len / PAGE_SIZE 697pointers. 698Returns 0 for success, or appropriate error number otherwise. 699.Pp 700.Fn uvm_unloan 701kills loans on pages or anons. The 702.Fa v 703must point to the array of pointers initialized by previous call to 704.Fn uvm_loan . 705.Fa npages 706should match number of pages allocated for loan, this also matches 707number of items in the array. 708Argument 709.Fa flags 710should be one of 711.Bd -literal 712#define UVM_LOAN_TOANON 0x01 /* loan to anons */ 713#define UVM_LOAN_TOPAGE 0x02 /* loan to kernel */ 714.Ed 715.Pp 716and should match what was used for previous call to 717.Fn uvm_loan . 718.Sh MISCELLANEOUS FUNCTIONS 719.nr nS 1 720.Ft struct uvm_object * 721.Fn uao_create "vsize_t size" "int flags" 722.Ft void 723.Fn uao_detach "struct uvm_object *uobj" 724.Ft void 725.Fn uao_reference "struct uvm_object *uobj" 726.Pp 727.Ft boolean_t 728.Fn uvm_chgkprot "caddr_t addr" "size_t len" "int rw" 729.Ft void 730.Fn uvm_kernacc "caddr_t addr" "size_t len" "int rw" 731.Ft boolean_t 732.Fn uvm_useracc "caddr_t addr" "size_t len" "int rw" 733.Pp 734.Ft int 735.Fn uvm_vslock "struct proc *p" "caddr_t addr" "size_t len" "vm_prot_t prot" 736.Ft void 737.Fn uvm_vsunlock "struct proc *p" "caddr_t addr" "size_t len" 738.Pp 739.Ft void 740.Fn uvm_meter "void" 741.Ft int 742.Fn uvm_sysctl "int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "void *newp " "size_t newlen" "struct proc *p" 743.Pp 744.Ft void 745.Fn uvm_fork "struct proc *p1" "struct proc *p2" "boolean_t shared" 746.Ft int 747.Fn uvm_grow "struct proc *p" "vaddr_t sp" 748.Ft int 749.Fn uvm_coredump "struct proc *p" "struct vnode *vp" "struct ucred *cred" "struct core *chdr" 750.Pp 751.Ft void 752.Fn uvn_findpages "struct uvm_object *uobj" "voff_t offset" "int *npagesp" "struct vm_page **pps" "int flags" 753.Pp 754.Ft void 755.Fn uvm_swap_stats "int cmd" "struct swapent *sep" "int sec" "register_t *retval" 756.Pp 757.nr nS 0 758.Pp 759The 760.Fn uao_create , 761.Fn uao_detach 762and 763.Fn uao_reference 764functions operate on anonymous memory objects, such as those used to support 765System V shared memory. 766.Fn uao_create 767returns an object of size 768.Fa size 769with flags: 770.Bd -literal 771#define UAO_FLAG_KERNOBJ 0x1 /* create kernel object */ 772#define UAO_FLAG_KERNSWAP 0x2 /* enable kernel swap */ 773.Ed 774.Pp 775which can only be used once each at system boot time. 776.Fn uao_reference 777creates an additional reference to the named anonymous memory object. 778.Fn uao_detach 779removes a reference from the named anonymous memory object, destroying 780it if removing the last reference. 781.Pp 782.Fn uvm_chgkprot 783changes the protection of kernel memory from 784.Fa addr 785to 786.Fa addr + len 787to the value of 788.Fa rw . 789This is primarily useful for debuggers, for setting breakpoints. 790This function is only available with options 791.Dv KGDB . 792.Pp 793.Fn uvm_kernacc 794and 795.Fn uvm_useracc 796check the access at address 797.Fa addr 798to 799.Fa addr + len 800for 801.Fa rw 802access, in the kernel address space, and the current process' 803address space respectively. 804.Pp 805.Fn uvm_vslock 806and 807.Fn uvm_vsunlock 808control the wiring and unwiring of pages for process 809.Fa p 810from 811.Fa addr 812to 813.Fa addr + len . 814These functions are normally used to wire memory for I/O. 815.Pp 816.Fn uvm_meter 817calculates the load average and wakes up the swapper if necessary. 818.Pp 819.Fn uvm_sysctl 820provides support for the 821.Dv CTL_VM 822domain of the 823.Xr sysctl 3 824hierarchy. 825.Fn uvm_sysctl 826handles the 827.Dv VM_LOADAVG , 828.Dv VM_METER 829and 830.Dv VM_UVMEXP 831calls, which return the current load averages, calculates current VM 832totals, and returns the uvmexp structure respectively. The load averages 833are access from userland using the 834.Xr getloadavg 3 835function. The uvmexp structure has all global state of the UVM system, 836and has the following members: 837.Bd -literal 838/* vm_page constants */ 839int pagesize; /* size of a page (PAGE_SIZE): must be power of 2 */ 840int pagemask; /* page mask */ 841int pageshift; /* page shift */ 842 843/* vm_page counters */ 844int npages; /* number of pages we manage */ 845int free; /* number of free pages */ 846int active; /* number of active pages */ 847int inactive; /* number of pages that we free'd but may want back */ 848int paging; /* number of pages in the process of being paged out */ 849int wired; /* number of wired pages */ 850int reserve_pagedaemon; /* number of pages reserved for pagedaemon */ 851int reserve_kernel; /* number of pages reserved for kernel */ 852 853/* pageout params */ 854int freemin; /* min number of free pages */ 855int freetarg; /* target number of free pages */ 856int inactarg; /* target number of inactive pages */ 857int wiredmax; /* max number of wired pages */ 858 859/* swap */ 860int nswapdev; /* number of configured swap devices in system */ 861int swpages; /* number of PAGE_SIZE'ed swap pages */ 862int swpginuse; /* number of swap pages in use */ 863int nswget; /* number of times fault calls uvm_swap_get() */ 864int nanon; /* number total of anon's in system */ 865int nfreeanon; /* number of free anon's */ 866 867/* stat counters */ 868int faults; /* page fault count */ 869int traps; /* trap count */ 870int intrs; /* interrupt count */ 871int swtch; /* context switch count */ 872int softs; /* software interrupt count */ 873int syscalls; /* system calls */ 874int pageins; /* pagein operation count */ 875 /* pageouts are in pdpageouts below */ 876int swapins; /* swapins */ 877int swapouts; /* swapouts */ 878int pgswapin; /* pages swapped in */ 879int pgswapout; /* pages swapped out */ 880int forks; /* forks */ 881int forks_ppwait; /* forks where parent waits */ 882int forks_sharevm; /* forks where vmspace is shared */ 883 884/* fault subcounters */ 885int fltnoram; /* number of times fault was out of ram */ 886int fltnoanon; /* number of times fault was out of anons */ 887int fltpgwait; /* number of times fault had to wait on a page */ 888int fltpgrele; /* number of times fault found a released page */ 889int fltrelck; /* number of times fault relock called */ 890int fltrelckok; /* number of times fault relock is a success */ 891int fltanget; /* number of times fault gets anon page */ 892int fltanretry; /* number of times fault retrys an anon get */ 893int fltamcopy; /* number of times fault clears "needs copy" */ 894int fltnamap; /* number of times fault maps a neighbor anon page */ 895int fltnomap; /* number of times fault maps a neighbor obj page */ 896int fltlget; /* number of times fault does a locked pgo_get */ 897int fltget; /* number of times fault does an unlocked get */ 898int flt_anon; /* number of times fault anon (case 1a) */ 899int flt_acow; /* number of times fault anon cow (case 1b) */ 900int flt_obj; /* number of times fault is on object page (2a) */ 901int flt_prcopy; /* number of times fault promotes with copy (2b) */ 902int flt_przero; /* number of times fault promotes with zerofill (2b) */ 903 904/* daemon counters */ 905int pdwoke; /* number of times daemon woke up */ 906int pdrevs; /* number of times daemon rev'd clock hand */ 907int pdswout; /* number of times daemon called for swapout */ 908int pdfreed; /* number of pages daemon freed since boot */ 909int pdscans; /* number of pages daemon scanned since boot */ 910int pdanscan; /* number of anonymous pages scanned by daemon */ 911int pdobscan; /* number of object pages scanned by daemon */ 912int pdreact; /* number of pages daemon reactivated since boot */ 913int pdbusy; /* number of times daemon found a busy page */ 914int pdpageouts; /* number of times daemon started a pageout */ 915int pdpending; /* number of times daemon got a pending pageout */ 916int pddeact; /* number of pages daemon deactivates */ 917.Ed 918.Pp 919.Fn uvm_fork 920forks a virtual address space for process' (old) 921.Fa p1 922and (new) 923.Fa p2 . 924If the 925.Fa shared 926argument is non zero, p1 shares its address space with p2, 927otherwise a new address space is created. This function 928currently has no return value, and thus cannot fail. In 929the future, this function will be changed to allow it to 930fail in low memory conditions. 931.Pp 932.Fn uvm_grow 933increases the stack segment of process 934.Fa p 935to include 936.Fa sp . 937.Pp 938.Fn uvm_coredump 939generates a coredump on vnode 940.Fa vp 941for process 942.Fa p 943with credentials 944.Fa cred 945and core header description in 946.Fa chdr . 947.Pp 948.Fn uvn_findpages 949looks up or creates pages in 950.Fa uobj 951at offset 952.Fa offset , 953marks them busy and returns them in the 954.Fa pps 955array. 956Currently 957.Fa uobj 958must be a vnode object. 959The number of pages requested is pointed to by 960.Fa npagesp , 961and this value is updated with the actual number of pages returned. 962The flags can be 963.Bd -literal 964#define UFP_ALL 0x00 /* return all pages requested */ 965#define UFP_NOWAIT 0x01 /* don't sleep */ 966#define UFP_NOALLOC 0x02 /* don't allocate new pages */ 967#define UFP_NOCACHE 0x04 /* don't return pages which already exist */ 968#define UFP_NORDONLY 0x08 /* don't return PG_READONLY pages */ 969.Ed 970.Pp 971.Dv UFP_ALL 972is a pseudo-flag meaning all requested pages should be returned. 973.Dv UFP_NOWAIT 974means that we must not sleep. 975.Dv UFP_NOALLOC 976causes any pages which do not already exist to be skipped. 977.Dv UFP_NOCACHE 978causes any pages which do already exist to be skipped. 979.Dv UFP_NORDONLY 980causes any pages which are marked PG_READONLY to be skipped. 981.Pp 982.Fn uvm_swap_stats 983implements the 984.Dv SWAP_STATS 985and 986.Dv SWAP_OSTATS 987operation of the 988.Xr swapctl 2 989system call. 990.Fa cmd 991is the requested command, 992.Dv SWAP_STATS 993or 994.Dv SWAP_OSTATS . 995The function will copy no more than 996.Fa sec 997entries in the array pointed by 998.Fa sep . 999On return, 1000.Fa retval 1001holds the actual number of entries copied in the array. 1002.Sh NOTES 1003.Fn uvm_chgkprot 1004is only available if the kernel has been compiled with options 1005.Dv KGDB . 1006.Pp 1007All structure and types whose names begin with 1008.Dq vm_ 1009will be renamed to 1010.Dq uvm_ . 1011.Sh SEE ALSO 1012.Xr swapctl 2 , 1013.Xr getloadavg 3 , 1014.Xr kvm 3 , 1015.Xr sysctl 3 , 1016.Xr ddb 4 , 1017.Xr options 4 , 1018.Xr pmap 9 1019.Sh HISTORY 1020UVM is a new VM system developed at Washington University in St. Louis 1021(Missouri). UVM's roots lie partly in the Mach-based 1022.Bx 4.4 1023VM system, the 1024.Fx 1025VM system, and the SunOS4 VM system. UVM's basic structure is based on the 1026.Bx 4.4 1027VM system. UVM's new anonymous memory system is based on the 1028anonymous memory system found in the SunOS4 VM (as described in papers 1029published by Sun Microsystems, Inc.). UVM also includes a number of feature 1030new to 1031.Bx 1032including page loanout, map entry passing, simplified 1033copy-on-write, and clustered anonymous memory pageout. UVM is also 1034further documented in a August 1998 dissertation by Charles D. Cranor. 1035.Pp 1036UVM appeared in 1037.Nx 1.4 . 1038.Sh AUTHORS 1039Charles D. Cranor \*[Lt]chuck@ccrc.wustl.edu\*[Gt] designed and implemented UVM. 1040.Pp 1041Matthew Green \*[Lt]mrg@eterna.com.au\*[Gt] wrote the swap-space management code 1042and handled the logistical issues involved with merging UVM into the 1043.Nx 1044source tree. 1045.Pp 1046Chuck Silvers \*[Lt]chuq@chuq.com\*[Gt] implemented the aobj pager, thus allowing 1047UVM to support System V shared memory and process swapping. He also 1048designed and implemented the UBC part of UVM, which uses UVM pages to 1049cache vnode data rather than the traditional buffer cache buffers. 1050