1.\" $NetBSD: uvm.9,v 1.74 2006/06/13 13:23:59 yamt 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.Dd February 27, 2006 30.Dt UVM 9 31.Os 32.Sh NAME 33.Nm uvm 34.Nd virtual memory system external interface 35.Sh SYNOPSIS 36.In sys/param.h 37.In uvm/uvm.h 38.Sh DESCRIPTION 39The UVM virtual memory system manages access to the computer's memory 40resources. 41User processes and the kernel access these resources through 42UVM's external interface. 43UVM's external interface includes functions that: 44.Pp 45.Bl -hyphen -compact 46.It 47initialise UVM sub-systems 48.It 49manage virtual address spaces 50.It 51resolve page faults 52.It 53memory map files and devices 54.It 55perform uio-based I/O to virtual memory 56.It 57allocate and free kernel virtual memory 58.It 59allocate and free physical memory 60.El 61.Pp 62In addition to exporting these services, UVM has two kernel-level processes: 63pagedaemon and swapper. 64The pagedaemon process sleeps until physical memory becomes scarce. 65When that happens, pagedaemon is awoken. 66It scans physical memory, paging out and freeing memory that has not 67been recently used. 68The swapper process swaps in runnable processes that are currently swapped 69out, if there is room. 70.Pp 71There are also several miscellaneous functions. 72.Sh INITIALISATION 73.Ft void 74.br 75.Fn uvm_init "void" ; 76.Pp 77.Ft void 78.br 79.Fn uvm_init_limits "struct lwp *l" ; 80.Pp 81.Ft void 82.br 83.Fn uvm_setpagesize "void" ; 84.Pp 85.Ft void 86.br 87.Fn uvm_swap_init "void" ; 88.Pp 89.Fn uvm_init 90sets up the UVM system at system boot time, after the 91console has been setup. 92It initialises global state, the page, map, kernel virtual memory state, 93machine-dependent physical map, kernel memory allocator, 94pager and anonymous memory sub-systems, and then enables 95paging of kernel objects. 96.Pp 97.Fn uvm_init_limits 98initialises process limits for the named process. 99This is for use by the system startup for process zero, before any 100other processes are created. 101.Pp 102.Fn uvm_setpagesize 103initialises the uvmexp members pagesize (if not already done by 104machine-dependent code), pageshift and pagemask. 105It should be called by machine-dependent code early in the 106.Fn pmap_init 107call (see 108.Xr pmap 9 ) . 109.Pp 110.Fn uvm_swap_init 111initialises the swap sub-system. 112.Sh VIRTUAL ADDRESS SPACE MANAGEMENT 113.Ft int 114.br 115.Fn uvm_map "struct vm_map *map" "vaddr_t *startp" "vsize_t size" "struct uvm_object *uobj" "voff_t uoffset" "vsize_t align" "uvm_flag_t flags" ; 116.Pp 117.Ft void 118.br 119.Fn uvm_unmap "struct vm_map *map" "vaddr_t start" "vaddr_t end" ; 120.Pp 121.Ft int 122.br 123.Fn uvm_map_pageable "struct vm_map *map" "vaddr_t start" "vaddr_t end" "boolean_t new_pageable" "int lockflags" ; 124.Pp 125.Ft boolean_t 126.br 127.Fn uvm_map_checkprot "struct vm_map *map" "vaddr_t start" "vaddr_t end" "vm_prot_t protection" ; 128.Pp 129.Ft int 130.br 131.Fn uvm_map_protect "struct vm_map *map" "vaddr_t start" "vaddr_t end" "vm_prot_t new_prot" "boolean_t set_max" ; 132.Pp 133.Ft int 134.br 135.Fn uvm_deallocate "struct vm_map *map" "vaddr_t start" "vsize_t size" ; 136.Pp 137.Ft struct vmspace * 138.br 139.Fn uvmspace_alloc "vaddr_t min" "vaddr_t max" "int pageable" ; 140.Pp 141.Ft void 142.br 143.Fn uvmspace_exec "struct lwp *l" "vaddr_t start" "vaddr_t end" ; 144.Pp 145.Ft struct vmspace * 146.br 147.Fn uvmspace_fork "struct vmspace *vm" ; 148.Pp 149.Ft void 150.br 151.Fn uvmspace_free "struct vmspace *vm1" ; 152.Pp 153.Ft void 154.br 155.Fn uvmspace_share "struct proc *p1" "struct proc *p2" ; 156.Pp 157.Ft void 158.br 159.Fn uvmspace_unshare "struct lwp *l" ; 160.Pp 161.Ft boolean_t 162.br 163.Fn uvm_uarea_alloc "vaddr_t *uaddrp" ; 164.Pp 165.Ft void 166.br 167.Fn uvm_uarea_free "vaddr_t uaddr" ; 168.Pp 169.Fn uvm_map 170establishes a valid mapping in map 171.Fa map , 172which must be unlocked. 173The new mapping has size 174.Fa size , 175which must be a multiple of 176.Dv PAGE_SIZE . 177The 178.Fa uobj 179and 180.Fa uoffset 181arguments can have four meanings. 182When 183.Fa uobj 184is 185.Dv NULL 186and 187.Fa uoffset 188is 189.Dv UVM_UNKNOWN_OFFSET , 190.Fn uvm_map 191does not use the machine-dependent 192.Dv PMAP_PREFER 193function. 194If 195.Fa uoffset 196is any other value, it is used as the hint to 197.Dv PMAP_PREFER . 198When 199.Fa uobj 200is not 201.Dv NULL 202and 203.Fa uoffset 204is 205.Dv UVM_UNKNOWN_OFFSET , 206.Fn uvm_map 207finds the offset based upon the virtual address, passed as 208.Fa startp . 209If 210.Fa uoffset 211is any other value, we are doing a normal mapping at this offset. 212The start address of the map will be returned in 213.Fa startp . 214.Pp 215.Fa align 216specifies alignment of mapping unless 217.Dv UVM_FLAG_FIXED 218is specified in 219.Fa flags . 220.Fa align 221must be a power of 2. 222.Pp 223.Fa flags 224passed to 225.Fn uvm_map 226are typically created using the 227.Fn UVM_MAPFLAG "vm_prot_t prot" "vm_prot_t maxprot" "vm_inherit_t inh" "int advice" "int flags" 228macro, which uses the following values. 229The 230.Fa prot 231and 232.Fa maxprot 233can take are: 234.Bd -literal 235#define UVM_PROT_MASK 0x07 /* protection mask */ 236#define UVM_PROT_NONE 0x00 /* protection none */ 237#define UVM_PROT_ALL 0x07 /* everything */ 238#define UVM_PROT_READ 0x01 /* read */ 239#define UVM_PROT_WRITE 0x02 /* write */ 240#define UVM_PROT_EXEC 0x04 /* exec */ 241#define UVM_PROT_R 0x01 /* read */ 242#define UVM_PROT_W 0x02 /* write */ 243#define UVM_PROT_RW 0x03 /* read-write */ 244#define UVM_PROT_X 0x04 /* exec */ 245#define UVM_PROT_RX 0x05 /* read-exec */ 246#define UVM_PROT_WX 0x06 /* write-exec */ 247#define UVM_PROT_RWX 0x07 /* read-write-exec */ 248.Ed 249.Pp 250The values that 251.Fa inh 252can take are: 253.Bd -literal 254#define UVM_INH_MASK 0x30 /* inherit mask */ 255#define UVM_INH_SHARE 0x00 /* "share" */ 256#define UVM_INH_COPY 0x10 /* "copy" */ 257#define UVM_INH_NONE 0x20 /* "none" */ 258#define UVM_INH_DONATE 0x30 /* "donate" \*[Lt]\*[Lt] not used */ 259.Ed 260.Pp 261The values that 262.Fa advice 263can take are: 264.Bd -literal 265#define UVM_ADV_NORMAL 0x0 /* 'normal' */ 266#define UVM_ADV_RANDOM 0x1 /* 'random' */ 267#define UVM_ADV_SEQUENTIAL 0x2 /* 'sequential' */ 268#define UVM_ADV_MASK 0x7 /* mask */ 269.Ed 270.Pp 271The values that 272.Fa flags 273can take are: 274.Bd -literal 275#define UVM_FLAG_FIXED 0x010000 /* find space */ 276#define UVM_FLAG_OVERLAY 0x020000 /* establish overlay */ 277#define UVM_FLAG_NOMERGE 0x040000 /* don't merge map entries */ 278#define UVM_FLAG_COPYONW 0x080000 /* set copy_on_write flag */ 279#define UVM_FLAG_AMAPPAD 0x100000 /* for bss: pad amap to reduce malloc() */ 280#define UVM_FLAG_TRYLOCK 0x200000 /* fail if we can not lock map */ 281.Ed 282.Pp 283The 284.Dv UVM_MAPFLAG 285macro arguments can be combined with an or operator. 286There are several special purpose macros for checking protection 287combinations, e.g., the 288.Dv UVM_PROT_WX 289macro. 290There are also some additional macros to extract bits from the flags. 291The 292.Dv UVM_PROTECTION , 293.Dv UVM_INHERIT , 294.Dv UVM_MAXPROTECTION 295and 296.Dv UVM_ADVICE 297macros return the protection, inheritance, maximum protection and advice, 298respectively. 299.Fn uvm_map 300returns a standard UVM return value. 301.Pp 302.Fn uvm_unmap 303removes a valid mapping, 304from 305.Fa start 306to 307.Fa end , 308in map 309.Fa map , 310which must be unlocked. 311.Pp 312.Fn uvm_map_pageable 313changes the pageability of the pages in the range from 314.Fa start 315to 316.Fa end 317in map 318.Fa map 319to 320.Fa new_pageable . 321.Fn uvm_map_pageable 322returns a standard UVM return value. 323.Pp 324.Fn uvm_map_checkprot 325checks the protection of the range from 326.Fa start 327to 328.Fa end 329in map 330.Fa map 331against 332.Fa protection . 333This returns either 334.Dv TRUE 335or 336.Dv FALSE . 337.Pp 338.Fn uvm_map_protect 339changes the protection 340.Fa start 341to 342.Fa end 343in map 344.Fa map 345to 346.Fa new_prot , 347also setting the maximum protection to the region to 348.Fa new_prot 349if 350.Fa set_max 351is non-zero. 352This function returns a standard UVM return value. 353.Pp 354.Fn uvm_deallocate 355deallocates kernel memory in map 356.Fa map 357from address 358.Fa start 359to 360.Fa start + size . 361.Pp 362.Fn uvmspace_alloc 363allocates and returns a new address space, with ranges from 364.Fa min 365to 366.Fa max , 367setting the pageability of the address space to 368.Fa pageable . 369.Pp 370.Fn uvmspace_exec 371either reuses the address space of lwp 372.Fa l 373if there are no other references to it, or creates 374a new one with 375.Fn uvmspace_alloc . 376The range of valid addresses in the address space is reset to 377.Fa start 378through 379.Fa end . 380.Pp 381.Fn uvmspace_fork 382creates and returns a new address space based upon the 383.Fa vm1 384address space, typically used when allocating an address space for a 385child process. 386.Pp 387.Fn uvmspace_free 388lowers the reference count on the address space 389.Fa vm , 390freeing the data structures if there are no other references. 391.Pp 392.Fn uvmspace_share 393causes process 394.Pa p2 395to share the address space of 396.Fa p1 . 397.Pp 398.Fn uvmspace_unshare 399ensures that lwp 400.Fa l 401has its own, unshared address space, by creating a new one if 402necessary by calling 403.Fn uvmspace_fork . 404.Pp 405.Fn uvm_uarea_alloc 406allocates virtual space for a u-area (i.e., a kernel stack) and stores 407its virtual address in 408.Fa *uaddrp . 409The return value is 410.Dv TRUE 411if the u-area is already backed by wired physical memory, otherwise 412.Dv FALSE . 413.Pp 414.Fn uvm_uarea_free 415frees a u-area allocated with 416.Fn uvm_uarea_alloc , 417freeing both the virtual space and any physical pages which may have been 418allocated to back that virtual space later. 419.Sh PAGE FAULT HANDLING 420.Ft int 421.br 422.Fn uvm_fault "struct vm_map *orig_map" "vaddr_t vaddr" "vm_fault_t fault_type" "vm_prot_t access_type" ; 423.Pp 424.Fn uvm_fault 425is the main entry point for faults. 426It takes 427.Fa orig_map 428as the map the fault originated in, a 429.Fa vaddr 430offset into the map the fault occurred, 431.Fa fault_type 432describing the type of fault, and 433.Fa access_type 434describing the type of access requested. 435.Fn uvm_fault 436returns a standard UVM return value. 437.Sh MEMORY MAPPING FILES AND DEVICES 438.Ft struct uvm_object * 439.br 440.Fn uvn_attach "void *arg" "vm_prot_t accessprot" ; 441.Pp 442.Ft void 443.br 444.Fn uvm_vnp_setsize "struct vnode *vp" "voff_t newsize" ; 445.Pp 446.Ft void * 447.br 448.Fn ubc_alloc "struct uvm_object *uobj" "voff_t offset" "vsize_t *lenp" \ 449"int advice" "int flags" ; 450.Pp 451.Ft void 452.br 453.Fn ubc_release "void *va" "int flags" ; 454.Pp 455.Fn uvn_attach 456attaches a UVM object to vnode 457.Fa arg , 458creating the object if necessary. 459The object is returned. 460.Pp 461.Fn uvm_vnp_setsize 462sets the size of vnode 463.Fa vp 464to 465.Fa newsize . 466Caller must hold a reference to the vnode. 467If the vnode shrinks, pages no longer used are discarded. 468.Pp 469.Fn ubc_alloc 470creates a kernel mapping of 471.Fa uobj 472starting at offset 473.Fa offset . 474The desired length of the mapping is pointed to by 475.Fa lenp , 476but the actual mapping may be smaller than this. 477.Fa lenp 478is updated to contain the actual length mapped. 479.Fa advice 480is the access pattern hint, which must be one of 481.Pp 482.Bl -tag -offset indent -width "UVM_ADV_SEQUENTIAL" -compact 483.It UVM_ADV_NORMAL 484No hint 485.It UVM_ADV_RANDOM 486Random access hint 487.It UVM_ADV_SEQUENTIAL 488Sequential access hint (from lower offset to higher offset) 489.El 490.Pp 491The flags must be one of 492.Bd -literal 493#define UBC_READ 0x01 /* mapping will be accessed for read */ 494#define UBC_WRITE 0x02 /* mapping will be accessed for write */ 495.Ed 496.Pp 497Currently, 498.Fa uobj 499must actually be a vnode object. 500Once the mapping is created, it must be accessed only by methods that can 501handle faults, such as 502.Fn uiomove 503or 504.Fn kcopy . 505Page faults on the mapping will result in the vnode's 506.Fn VOP_GETPAGES 507method being called to resolve the fault. 508.Pp 509.Fn ubc_release 510frees the mapping at 511.Fa va 512for reuse. 513The mapping may be cached to speed future accesses to the same region 514of the object. 515The flags can be any of 516.Bd -literal 517#define UBC_UNMAP 0x01 /* do not cache mapping */ 518.Ed 519.Sh VIRTUAL MEMORY I/O 520.Ft int 521.br 522.Fn uvm_io "struct vm_map *map" "struct uio *uio" ; 523.Pp 524.Fn uvm_io 525performs the I/O described in 526.Fa uio 527on the memory described in 528.Fa map . 529.Sh ALLOCATION OF KERNEL MEMORY 530.Ft vaddr_t 531.br 532.Fn uvm_km_alloc "struct vm_map *map" "vsize_t size" "vsize_t align" "uvm_flag_t flags" ; 533.Pp 534.Ft void 535.br 536.Fn uvm_km_free "struct vm_map *map" "vaddr_t addr" "vsize_t size" "uvm_flag_t flags" ; 537.Pp 538.Ft struct vm_map * 539.br 540.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" ; 541.Pp 542.Fn uvm_km_alloc 543allocates 544.Fa size 545bytes of kernel memory in map 546.Fa map . 547The first address of the allocated memory range will be aligned according to the 548.Fa align 549argument 550.Pq specify 0 if no alignment is necessary . 551The alignment must be a multiple of page size. 552The 553.Fa flags 554is a bitwise inclusive OR of the allocation type and operation flags. 555.Pp 556The allocation type should be one of: 557.Bl -tag -width UVM_KMF_PAGEABLE 558.It UVM_KMF_WIRED 559Wired memory. 560.It UVM_KMF_PAGEABLE 561Demand-paged zero-filled memory. 562.It UVM_KMF_VAONLY 563Virtual address only. 564No physical pages are mapped in the allocated region. 565If necessary, it's the caller's responsibility to enter page mappings. 566It's also the caller's responsibility to clean up the mappings before freeing 567the address range. 568.El 569.Pp 570The following operation flags are available: 571.Bl -tag -width UVM_KMF_PAGEABLE 572.It UVM_KMF_CANFAIL 573Can fail even if 574.Dv UVM_KMF_NOWAIT 575is not specified and 576.Dv UVM_KMF_WAITVA 577is specified. 578.It UVM_KMF_ZERO 579Request zero-filled memory. 580Only supported for 581.Dv UVM_KMF_WIRED . 582Shouldn't be used with other types. 583.It UVM_KMF_TRYLOCK 584Fail if we can't lock the map. 585.It UVM_KMF_NOWAIT 586Fail immediately if no memory is available. 587.It UVM_KMF_WAITVA 588Sleep to wait for the virtual address resources if needed. 589.El 590.Pp 591(If neither 592.Dv UVM_KMF_NOWAIT 593nor 594.Dv UVM_KMF_CANFAIL 595are specified and 596.Dv UVM_KMF_WAITVA 597is specified, 598.Fn uvm_km_alloc 599will never fail, but rather sleep indefinitely until the allocation succeeds.) 600.Pp 601Pageability of the pages allocated with 602.Dv UVM_KMF_PAGEABLE 603can be changed by 604.Fn uvm_map_pageable . 605In that case, the entire range must be changed atomically. 606Changing a part of the range is not supported. 607.Pp 608.Fn uvm_km_free 609frees the memory range allocated by 610.Fn uvm_km_alloc . 611.Fa addr 612must be an address returned by 613.Fn uvm_km_alloc . 614.Fa map 615and 616.Fa size 617must be the same as the ones used for the corresponding 618.Fn uvm_km_alloc . 619.Fa flags 620must be the allocation type used for the corresponding 621.Fn uvm_km_alloc . 622.Pp 623.Fn uvm_km_free 624is the only way to free memory ranges allocated by 625.Fn uvm_km_alloc . 626.Fn uvm_unmap 627must not be used. 628.Pp 629.Fn uvm_km_suballoc 630allocates submap from 631.Fa map , 632creating a new map if 633.Fa submap 634is 635.Dv NULL . 636The addresses of the submap can be specified exactly by setting the 637.Fa fixed 638argument to non-zero, which causes the 639.Fa min 640argument to specify the beginning of the address in the submap. 641If 642.Fa fixed 643is zero, any address of size 644.Fa size 645will be allocated from 646.Fa map 647and the start and end addresses returned in 648.Fa min 649and 650.Fa max . 651If 652.Fa pageable 653is non-zero, entries in the map may be paged out. 654.Sh ALLOCATION OF PHYSICAL MEMORY 655.Ft struct vm_page * 656.br 657.Fn uvm_pagealloc "struct uvm_object *uobj" "voff_t off" "struct vm_anon *anon" "int flags" ; 658.Pp 659.Ft void 660.br 661.Fn uvm_pagerealloc "struct vm_page *pg" "struct uvm_object *newobj" "voff_t newoff" ; 662.Pp 663.Ft void 664.br 665.Fn uvm_pagefree "struct vm_page *pg" ; 666.Pp 667.Ft int 668.br 669.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" ; 670.Pp 671.Ft void 672.br 673.Fn uvm_pglistfree "struct pglist *list" ; 674.Pp 675.Ft void 676.br 677.Fn uvm_page_physload "vaddr_t start" "vaddr_t end" "vaddr_t avail_start" "vaddr_t avail_end" "int free_list" ; 678.Pp 679.Fn uvm_pagealloc 680allocates a page of memory at virtual address 681.Fa off 682in either the object 683.Fa uobj 684or the anonymous memory 685.Fa anon , 686which must be locked by the caller. 687Only one of 688.Fa uobj 689and 690.Fa anon 691can be non 692.Dv NULL . 693Returns 694.Dv NULL 695when no page can be found. 696The flags can be any of 697.Bd -literal 698#define UVM_PGA_USERESERVE 0x0001 /* ok to use reserve pages */ 699#define UVM_PGA_ZERO 0x0002 /* returned page must be zero'd */ 700.Ed 701.Pp 702.Dv UVM_PGA_USERESERVE 703means to allocate a page even if that will result in the number of free pages 704being lower than 705.Dv uvmexp.reserve_pagedaemon 706(if the current thread is the pagedaemon) or 707.Dv uvmexp.reserve_kernel 708(if the current thread is not the pagedaemon). 709.Dv UVM_PGA_ZERO 710causes the returned page to be filled with zeroes, either by allocating it 711from a pool of pre-zeroed pages or by zeroing it in-line as necessary. 712.Pp 713.Fn uvm_pagerealloc 714reallocates page 715.Fa pg 716to a new object 717.Fa newobj , 718at a new offset 719.Fa newoff . 720.Pp 721.Fn uvm_pagefree 722frees the physical page 723.Fa pg . 724If the content of the page is known to be zero-filled, 725caller should set 726.Dv PG_ZERO 727in pg-\*[Gt]flags so that the page allocator will use 728the page to serve future 729.Dv UVM_PGA_ZERO 730requests efficiently. 731.Pp 732.Fn uvm_pglistalloc 733allocates a list of pages for size 734.Fa size 735byte under various constraints. 736.Fa low 737and 738.Fa high 739describe the lowest and highest addresses acceptable for the list. 740If 741.Fa alignment 742is non-zero, it describes the required alignment of the list, in 743power-of-two notation. 744If 745.Fa boundary 746is non-zero, no segment of the list may cross this power-of-two 747boundary, relative to zero. 748.Fa nsegs 749is the maximum number of physically contigous segments. 750If 751.Fa waitok 752is non-zero, the function may sleep until enough memory is available. 753(It also may give up in some situations, so a non-zero 754.Fa waitok 755does not imply that 756.Fn uvm_pglistalloc 757cannot return an error.) 758The allocated memory is returned in the 759.Fa rlist 760list; the caller has to provide storage only, the list is initialized by 761.Fn uvm_pglistalloc . 762.Pp 763.Fn uvm_pglistfree 764frees the list of pages pointed to by 765.Fa list . 766If the content of the page is known to be zero-filled, 767caller should set 768.Dv PG_ZERO 769in pg-\*[Gt]flags so that the page allocator will use 770the page to serve future 771.Dv UVM_PGA_ZERO 772requests efficiently. 773.Pp 774.Fn uvm_page_physload 775loads physical memory segments into VM space on the specified 776.Fa free_list . 777It must be called at system boot time to set up physical memory 778management pages. 779The arguments describe the 780.Fa start 781and 782.Fa end 783of the physical addresses of the segment, and the available start and end 784addresses of pages not already in use. 785.\" XXX expand on "system boot time"! 786.Sh PROCESSES 787.Ft void 788.br 789.Fn uvm_pageout "void" ; 790.Pp 791.Ft void 792.br 793.Fn uvm_scheduler "void" ; 794.Pp 795.Ft void 796.br 797.Fn uvm_swapin "struct lwp *l" ; 798.Pp 799.Fn uvm_pageout 800is the main loop for the page daemon. 801.Pp 802.Fn uvm_scheduler 803is the process zero main loop, which is to be called after the 804system has finished starting other processes. 805It handles the swapping in of runnable, swapped out processes in priority 806order. 807.Pp 808.Fn uvm_swapin 809swaps in the named lwp. 810.Sh PAGE LOAN 811.Ft int 812.br 813.Fn uvm_loan "struct vm_map *map" "vaddr_t start" "vsize_t len" "void *v" "int flags" ; 814.Pp 815.Ft void 816.br 817.Fn uvm_unloan "void *v" "int npages" "int flags" ; 818.Pp 819.Fn uvm_loan 820loans pages in a map out to anons or to the kernel. 821.Fa map 822should be unlocked, 823.Fa start 824and 825.Fa len 826should be multiples of 827.Dv PAGE_SIZE . 828Argument 829.Fa flags 830should be one of 831.Bd -literal 832#define UVM_LOAN_TOANON 0x01 /* loan to anons */ 833#define UVM_LOAN_TOPAGE 0x02 /* loan to kernel */ 834.Ed 835.Pp 836.Fa v 837should be pointer to array of pointers to 838.Li struct anon 839or 840.Li struct vm_page , 841as appropriate. 842The caller has to allocate memory for the array and 843ensure it's big enough to hold 844.Fa len / PAGE_SIZE 845pointers. 846Returns 0 for success, or appropriate error number otherwise. 847Note that wired pages can't be loaned out and 848.Fn uvm_loan 849will fail in that case. 850.Pp 851.Fn uvm_unloan 852kills loans on pages or anons. 853The 854.Fa v 855must point to the array of pointers initialized by previous call to 856.Fn uvm_loan . 857.Fa npages 858should match number of pages allocated for loan, this also matches 859number of items in the array. 860Argument 861.Fa flags 862should be one of 863.Bd -literal 864#define UVM_LOAN_TOANON 0x01 /* loan to anons */ 865#define UVM_LOAN_TOPAGE 0x02 /* loan to kernel */ 866.Ed 867.Pp 868and should match what was used for previous call to 869.Fn uvm_loan . 870.Sh MISCELLANEOUS FUNCTIONS 871.Ft struct uvm_object * 872.br 873.Fn uao_create "vsize_t size" "int flags" ; 874.Pp 875.Ft void 876.br 877.Fn uao_detach "struct uvm_object *uobj" ; 878.Pp 879.Ft void 880.br 881.Fn uao_reference "struct uvm_object *uobj" ; 882.Pp 883.Ft boolean_t 884.br 885.Fn uvm_chgkprot "caddr_t addr" "size_t len" "int rw" ; 886.Pp 887.Ft void 888.br 889.Fn uvm_kernacc "caddr_t addr" "size_t len" "int rw" ; 890.Pp 891.Ft int 892.br 893.Fn uvm_vslock "struct proc *l" "caddr_t addr" "size_t len" "vm_prot_t prot" ; 894.Pp 895.Ft void 896.br 897.Fn uvm_vsunlock "struct proc *p" "caddr_t addr" "size_t len" ; 898.Pp 899.Ft void 900.br 901.Fn uvm_meter "void" ; 902.Pp 903.Ft void 904.br 905.Fn uvm_fork "struct lwp *l1" "struct lwp *l2" "boolean_t shared" ; 906.Pp 907.Ft int 908.br 909.Fn uvm_grow "struct proc *p" "vaddr_t sp" ; 910.Pp 911.Ft int 912.br 913.Fn uvm_coredump "struct proc *p" "struct vnode *vp" "struct ucred *cred" "struct core *chdr" ; 914.Pp 915.Ft void 916.br 917.Fn uvn_findpages "struct uvm_object *uobj" "voff_t offset" "int *npagesp" "struct vm_page **pps" "int flags" ; 918.Pp 919.Ft void 920.br 921.Fn uvm_swap_stats "int cmd" "struct swapent *sep" "int sec" "register_t *retval" ; 922.Pp 923The 924.Fn uao_create , 925.Fn uao_detach , 926and 927.Fn uao_reference 928functions operate on anonymous memory objects, such as those used to support 929System V shared memory. 930.Fn uao_create 931returns an object of size 932.Fa size 933with flags: 934.Bd -literal 935#define UAO_FLAG_KERNOBJ 0x1 /* create kernel object */ 936#define UAO_FLAG_KERNSWAP 0x2 /* enable kernel swap */ 937.Ed 938.Pp 939which can only be used once each at system boot time. 940.Fn uao_reference 941creates an additional reference to the named anonymous memory object. 942.Fn uao_detach 943removes a reference from the named anonymous memory object, destroying 944it if removing the last reference. 945.Pp 946.Fn uvm_chgkprot 947changes the protection of kernel memory from 948.Fa addr 949to 950.Fa addr + len 951to the value of 952.Fa rw . 953This is primarily useful for debuggers, for setting breakpoints. 954This function is only available with options 955.Dv KGDB . 956.Pp 957.Fn uvm_kernacc 958checks the access at address 959.Fa addr 960to 961.Fa addr + len 962for 963.Fa rw 964access in the kernel address space. 965.Pp 966.Fn uvm_vslock 967and 968.Fn uvm_vsunlock 969control the wiring and unwiring of pages for process 970.Fa p 971from 972.Fa addr 973to 974.Fa addr + len . 975These functions are normally used to wire memory for I/O. 976.Pp 977.Fn uvm_meter 978calculates the load average and wakes up the swapper if necessary. 979.Pp 980.Fn uvm_fork 981forks a virtual address space for process' (old) 982.Fa p1 983and (new) 984.Fa p2 . 985If the 986.Fa shared 987argument is non zero, p1 shares its address space with p2, 988otherwise a new address space is created. 989This function currently has no return value, and thus cannot fail. 990In the future, this function will be changed to allow it to 991fail in low memory conditions. 992.Pp 993.Fn uvm_grow 994increases the stack segment of process 995.Fa p 996to include 997.Fa sp . 998.Pp 999.Fn uvm_coredump 1000generates a coredump on vnode 1001.Fa vp 1002for process 1003.Fa p 1004with credentials 1005.Fa cred 1006and core header description in 1007.Fa chdr . 1008.Pp 1009.Fn uvn_findpages 1010looks up or creates pages in 1011.Fa uobj 1012at offset 1013.Fa offset , 1014marks them busy and returns them in the 1015.Fa pps 1016array. 1017Currently 1018.Fa uobj 1019must be a vnode object. 1020The number of pages requested is pointed to by 1021.Fa npagesp , 1022and this value is updated with the actual number of pages returned. 1023The flags can be 1024.Bd -literal 1025#define UFP_ALL 0x00 /* return all pages requested */ 1026#define UFP_NOWAIT 0x01 /* don't sleep */ 1027#define UFP_NOALLOC 0x02 /* don't allocate new pages */ 1028#define UFP_NOCACHE 0x04 /* don't return pages which already exist */ 1029#define UFP_NORDONLY 0x08 /* don't return PG_READONLY pages */ 1030.Ed 1031.Pp 1032.Dv UFP_ALL 1033is a pseudo-flag meaning all requested pages should be returned. 1034.Dv UFP_NOWAIT 1035means that we must not sleep. 1036.Dv UFP_NOALLOC 1037causes any pages which do not already exist to be skipped. 1038.Dv UFP_NOCACHE 1039causes any pages which do already exist to be skipped. 1040.Dv UFP_NORDONLY 1041causes any pages which are marked PG_READONLY to be skipped. 1042.Pp 1043.Fn uvm_swap_stats 1044implements the 1045.Dv SWAP_STATS 1046and 1047.Dv SWAP_OSTATS 1048operation of the 1049.Xr swapctl 2 1050system call. 1051.Fa cmd 1052is the requested command, 1053.Dv SWAP_STATS 1054or 1055.Dv SWAP_OSTATS . 1056The function will copy no more than 1057.Fa sec 1058entries in the array pointed by 1059.Fa sep . 1060On return, 1061.Fa retval 1062holds the actual number of entries copied in the array. 1063.Sh SYSCTL 1064UVM provides support for the 1065.Dv CTL_VM 1066domain of the 1067.Xr sysctl 3 1068hierarchy. 1069It handles the 1070.Dv VM_LOADAVG , 1071.Dv VM_METER , 1072.Dv VM_UVMEXP , 1073and 1074.Dv VM_UVMEXP2 1075nodes, which return the current load averages, calculates current VM 1076totals, returns the uvmexp structure, and a kernel version independent 1077view of the uvmexp structure, respectively. 1078It also exports a number of tunables that control how much VM space is 1079allowed to be consumed by various tasks. 1080The load averages are typically accessed from userland using the 1081.Xr getloadavg 3 1082function. 1083The uvmexp structure has all global state of the UVM system, 1084and has the following members: 1085.Bd -literal 1086/* vm_page constants */ 1087int pagesize; /* size of a page (PAGE_SIZE): must be power of 2 */ 1088int pagemask; /* page mask */ 1089int pageshift; /* page shift */ 1090 1091/* vm_page counters */ 1092int npages; /* number of pages we manage */ 1093int free; /* number of free pages */ 1094int active; /* number of active pages */ 1095int inactive; /* number of pages that we free'd but may want back */ 1096int paging; /* number of pages in the process of being paged out */ 1097int wired; /* number of wired pages */ 1098int reserve_pagedaemon; /* number of pages reserved for pagedaemon */ 1099int reserve_kernel; /* number of pages reserved for kernel */ 1100 1101/* pageout params */ 1102int freemin; /* min number of free pages */ 1103int freetarg; /* target number of free pages */ 1104int inactarg; /* target number of inactive pages */ 1105int wiredmax; /* max number of wired pages */ 1106 1107/* swap */ 1108int nswapdev; /* number of configured swap devices in system */ 1109int swpages; /* number of PAGE_SIZE'ed swap pages */ 1110int swpginuse; /* number of swap pages in use */ 1111int nswget; /* number of times fault calls uvm_swap_get() */ 1112int nanon; /* number total of anon's in system */ 1113int nfreeanon; /* number of free anon's */ 1114 1115/* stat counters */ 1116int faults; /* page fault count */ 1117int traps; /* trap count */ 1118int intrs; /* interrupt count */ 1119int swtch; /* context switch count */ 1120int softs; /* software interrupt count */ 1121int syscalls; /* system calls */ 1122int pageins; /* pagein operation count */ 1123 /* pageouts are in pdpageouts below */ 1124int swapins; /* swapins */ 1125int swapouts; /* swapouts */ 1126int pgswapin; /* pages swapped in */ 1127int pgswapout; /* pages swapped out */ 1128int forks; /* forks */ 1129int forks_ppwait; /* forks where parent waits */ 1130int forks_sharevm; /* forks where vmspace is shared */ 1131 1132/* fault subcounters */ 1133int fltnoram; /* number of times fault was out of ram */ 1134int fltnoanon; /* number of times fault was out of anons */ 1135int fltpgwait; /* number of times fault had to wait on a page */ 1136int fltpgrele; /* number of times fault found a released page */ 1137int fltrelck; /* number of times fault relock called */ 1138int fltrelckok; /* number of times fault relock is a success */ 1139int fltanget; /* number of times fault gets anon page */ 1140int fltanretry; /* number of times fault retrys an anon get */ 1141int fltamcopy; /* number of times fault clears "needs copy" */ 1142int fltnamap; /* number of times fault maps a neighbor anon page */ 1143int fltnomap; /* number of times fault maps a neighbor obj page */ 1144int fltlget; /* number of times fault does a locked pgo_get */ 1145int fltget; /* number of times fault does an unlocked get */ 1146int flt_anon; /* number of times fault anon (case 1a) */ 1147int flt_acow; /* number of times fault anon cow (case 1b) */ 1148int flt_obj; /* number of times fault is on object page (2a) */ 1149int flt_prcopy; /* number of times fault promotes with copy (2b) */ 1150int flt_przero; /* number of times fault promotes with zerofill (2b) */ 1151 1152/* daemon counters */ 1153int pdwoke; /* number of times daemon woke up */ 1154int pdrevs; /* number of times daemon rev'd clock hand */ 1155int pdswout; /* number of times daemon called for swapout */ 1156int pdfreed; /* number of pages daemon freed since boot */ 1157int pdscans; /* number of pages daemon scanned since boot */ 1158int pdanscan; /* number of anonymous pages scanned by daemon */ 1159int pdobscan; /* number of object pages scanned by daemon */ 1160int pdreact; /* number of pages daemon reactivated since boot */ 1161int pdbusy; /* number of times daemon found a busy page */ 1162int pdpageouts; /* number of times daemon started a pageout */ 1163int pdpending; /* number of times daemon got a pending pageout */ 1164int pddeact; /* number of pages daemon deactivates */ 1165.Ed 1166.Sh NOTES 1167.Fn uvm_chgkprot 1168is only available if the kernel has been compiled with options 1169.Dv KGDB . 1170.Pp 1171All structure and types whose names begin with 1172.Dq vm_ 1173will be renamed to 1174.Dq uvm_ . 1175.Sh SEE ALSO 1176.Xr swapctl 2 , 1177.Xr getloadavg 3 , 1178.Xr kvm 3 , 1179.Xr sysctl 3 , 1180.Xr ddb 4 , 1181.Xr options 4 , 1182.Xr pmap 9 1183.Sh HISTORY 1184UVM is a new VM system developed at Washington University in St. Louis 1185(Missouri). 1186UVM's roots lie partly in the Mach-based 1187.Bx 4.4 1188VM system, the 1189.Fx 1190VM system, and the SunOS 4 VM system. 1191UVM's basic structure is based on the 1192.Bx 4.4 1193VM system. 1194UVM's new anonymous memory system is based on the 1195anonymous memory system found in the SunOS 4 VM (as described in papers 1196published by Sun Microsystems, Inc.). 1197UVM also includes a number of features new to 1198.Bx 1199including page loanout, map entry passing, simplified 1200copy-on-write, and clustered anonymous memory pageout. 1201UVM is also further documented in an August 1998 dissertation by 1202Charles D. Cranor. 1203.Pp 1204UVM appeared in 1205.Nx 1.4 . 1206.Sh AUTHORS 1207Charles D. Cranor 1208.Aq chuck@ccrc.wustl.edu 1209designed and implemented UVM. 1210.Pp 1211Matthew Green 1212.Aq mrg@eterna.com.au 1213wrote the swap-space management code and handled the logistical issues 1214involved with merging UVM into the 1215.Nx 1216source tree. 1217.Pp 1218Chuck Silvers 1219.Aq chuq@chuq.com 1220implemented the aobj pager, thus allowing UVM to support System V shared 1221memory and process swapping. 1222He also designed and implemented the UBC part of UVM, which uses UVM pages 1223to cache vnode data rather than the traditional buffer cache buffers. 1224