1.\" $OpenBSD: pmap.9,v 1.16 2016/03/21 21:14:42 natano Exp $ 2.\" 3.\" Copyright (c) 2001, 2002, 2003 CubeSoft Communications, Inc. 4.\" <http://www.csoft.org> 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.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20.\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 24.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE 25.\" POSSIBILITY OF SUCH DAMAGE. 26.\" 27.Dd $Mdocdate: March 21 2016 $ 28.Dt PMAP_INIT 9 29.Os 30.Sh NAME 31.Nm pmap_init , 32.Nm pmap_enter , 33.Nm pmap_kenter_pa , 34.Nm pmap_remove , 35.Nm pmap_kremove 36.Nm pmap_unwire , 37.Nm pmap_protect , 38.Nm pmap_page_protect , 39.Nm pmap_is_modified , 40.Nm pmap_clear_modify , 41.Nm pmap_is_referenced , 42.Nm pmap_clear_reference , 43.Nm pmap_copy_page , 44.Nm pmap_zero_page , 45.Nm pmap_create , 46.Nm pmap_reference , 47.Nm pmap_destroy , 48.Nm pmap_steal_memory , 49.Nm pmap_growkernel , 50.Nm pmap_update , 51.Nm pmap_collect , 52.Nm pmap_virtual_space , 53.Nm pmap_copy , 54.Nd machine dependent interface to the MMU 55.Sh SYNOPSIS 56.In machine/pmap.h 57.Sh DESCRIPTION 58The architecture-dependent 59.Nm pmap 60module describes how the physical mapping is done between the user-processes 61and kernel virtual addresses and the physical addresses of the main memory, 62providing machine-dependent translation and access tables that are used 63directly or indirectly by the memory-management hardware. 64The 65.Nm pmap 66layer can be viewed as a big array of mapping entries that are indexed by 67virtual address to produce a physical address and flags. 68These flags describe 69the page's protection, whether the page has been referenced or modified and 70other characteristics. 71.Pp 72The 73.Nm pmap 74interface is consistent across all platforms and hides the way page mappings 75are stored. 76.Sh INITIALIZATION 77.nr nS 1 78.Ft void 79.Fn pmap_init "void" 80.nr nS 0 81.Pp 82The 83.Fn pmap_init 84function is called from the machine-independent 85.Xr uvm 9 86initialization code, when the MMU is enabled. 87.Sh PAGE MANAGEMENT 88Modified/referenced information is only tracked for pages managed by 89.Xr uvm 9 90(pages for which a vm_page structure exists). 91Only managed mappings of those pages have modified/referenced tracking. 92The use of unmanaged mappings should be limited to code which may execute 93in interrupt context (such as 94.Xr malloc 9 ) 95or to enter mappings for physical addresses which are not managed by 96.Xr uvm 9 . 97This allows 98.Nm pmap 99modules to avoid blocking interrupts when manipulating data structures or 100holding locks. 101Unmanaged mappings may only be entered into the kernel's virtual address space. 102The modified/referenced bits must be tracked on a per-page basis, as they 103are not attributes of a mapping, but attributes of a page. 104Therefore, even after all mappings for a given page have been removed, the 105modified/referenced bits for that page must be preserved. 106The only time the modified/referenced bits may be cleared is when 107.Xr uvm 9 108explicitly calls the 109.Fn pmap_clear_modify 110and 111.Fn pmap_clear_reference 112functions. 113These functions must also change any internal state necessary to detect 114the page being modified or referenced again after the modified/referenced 115state is cleared. 116.Pp 117Mappings entered by 118.Fn pmap_enter 119are managed, mappings entered by 120.Fn pmap_kenter_pa 121are not. 122.Sh MAPPING ALLOCATION 123.nr nS 1 124.Ft int 125.Fn pmap_enter "pmap_t pmap" "vaddr_t va" "paddr_t pa" "vm_prot_t prot" \ 126 "int flags" 127.Ft void 128.Fn pmap_kenter_pa "vaddr_t va" "paddr_t pa" "vm_prot_t prot" 129.Ft void 130.Fn pmap_remove "pmap_t pmap" "vaddr_t sva" "vaddr_t eva" 131.Ft void 132.Fn pmap_kremove "vaddr_t va" "vsize_t size" 133.nr nS 0 134.Pp 135The 136.Fn pmap_enter 137function creates a managed mapping for physical page 138.Fa pa 139at the specified virtual address 140.Fa va 141in the target physical map 142.Fa pmap 143with protection specified by 144.Fa prot : 145.Bl -tag -width "PROT_WRITE" 146.It PROT_READ 147The mapping must allow reading. 148.It PROT_WRITE 149The mapping must allow writing. 150.It PROT_EXEC 151The page mapped contains instructions that will be executed by the 152processor. 153.El 154.Pp 155The 156.Fa flags 157argument contains protection bits (the same bits used in the 158.Fa prot 159argument) indicating the type of access that caused the mapping to 160be created. 161This information may be used to seed modified/referenced 162information for the page being mapped, possibly avoiding redundant 163faults on platforms that track modified/referenced information in 164software. 165Other information provided by 166.Fa flags : 167.Bl -tag -width "PMAP_CANFAIL" 168.It PMAP_WIRED 169The mapping being created is a wired mapping. 170.It PMAP_CANFAIL 171The call to 172.Fn pmap_enter 173is allowed to fail. 174If this flag is not set, and the 175.Fn pmap_enter 176call is unable to create the mapping, perhaps due to insufficient 177resources, the 178.Nm pmap 179module must panic. 180.El 181.Pp 182The access type provided in the 183.Fa flags 184argument will never exceed the protection specified by 185.Fa prot . 186.Pp 187The 188.Fn pmap_enter 189function is called by the fault routine to establish a mapping for 190the page being faulted in. 191If 192.Fn pmap_enter 193is called to enter a mapping at a virtual address for which a mapping 194already exists, the previous mapping must be invalidated. 195.Fn pmap_enter 196is sometimes called to change the protection for a pre-existing mapping, 197or to change the 198.Dq wired 199attribute for a pre-existing mapping. 200.Pp 201The 202.Fn pmap_kenter_pa 203function creates an unmanaged mapping of physical address 204.Fa pa 205at the specified virtual address 206.Fa va 207with the protection specified by 208.Fa prot . 209.Pp 210The 211.Fn pmap_remove 212function removes the range of virtual addresses 213.Fa sva 214to 215.Fa eva 216from 217.Fa pmap , 218assuming proper alignment. 219.Fn pmap_remove 220is called during an unmap 221operation to remove low-level machine dependent mappings. 222.Pp 223The 224.Fn pmap_kremove 225function removes an unmanaged mapping at virtual address 226.Fa va 227of size 228.Fa size . 229.Pp 230A call to 231.Fn pmap_update 232must be made after 233.Fn pmap_kenter_pa 234or 235.Fn pmap_kremove 236to notify the 237.Nm pmap 238layer that the mappings need to be made correct. 239.Sh ACCESS PROTECTION 240.nr nS 1 241.Ft void 242.Fn pmap_unwire "pmap_t pmap" "vaddr_t va" 243.Ft void 244.Fn pmap_protect "pmap_t pmap" "vaddr_t sva" "vaddr_t eva" "vm_prot_t prot" 245.Ft void 246.Fn pmap_page_protect "struct vm_page *pg" "vm_prot_t prot" 247.nr nS 0 248.Pp 249The 250.Fn pmap_unwire 251function clears the wired attribute for a map/virtual-address pair. 252The mapping must already exist in 253.Fa pmap . 254.Pp 255The 256.Fn pmap_protect 257function sets the physical protection on range 258.Fa sva 259to 260.Fa eva , 261in 262.Fa pmap . 263.Pp 264The 265.Fn pmap_protect 266function is called during a copy-on-write operation to write protect 267copy-on-write memory, and when paging out a page to remove all mappings 268of a page. 269The 270.Fn pmap_page_protect 271function sets the permission for all mapping to page 272.Fa pg . 273The 274.Fn pmap_page_protect 275function is called before a pageout operation to ensure that all pmap 276references to a page are removed. 277.Sh PHYSICAL PAGE-USAGE INFORMATION 278.nr nS 1 279.Ft boolean_t 280.Fn pmap_is_modified "struct vm_page *pg" 281.Ft boolean_t 282.Fn pmap_clear_modify "struct vm_page *pg" 283.Ft boolean_t 284.Fn pmap_is_referenced "struct vm_page *pg" 285.Ft boolean_t 286.Fn pmap_clear_reference "struct vm_page *pg" 287.nr nS 0 288.Pp 289The 290.Fn pmap_is_modified 291and 292.Fn pmap_clear_modify 293functions read/set the modify bits on the specified physical page 294.Fa pg . 295The 296.Fn pmap_is_referenced 297and 298.Fn pmap_clear_reference 299functions read/set the reference bits on the specified physical page 300.Fa pg . 301.Pp 302The 303.Fn pmap_is_referenced 304and 305.Fn pmap_is_modified 306functions are called by the pagedaemon when looking for pages to free. 307The 308.Fn pmap_clear_referenced 309and 310.Fn pmap_clear_modify 311functions are called by the pagedaemon to help identification of pages 312that are no longer in demand. 313.Sh PHYSICAL PAGE INITIALIZATION 314.nr nS 1 315.Ft void 316.Fn pmap_copy_page "struct vm_page *src" "struct vm_page *dst" 317.Ft void 318.Fn pmap_zero_page "struct vm_page *page" 319.nr nS 0 320.Pp 321The 322.Fn pmap_copy_page 323function copies the content of the physical page 324.Fa src 325to physical page 326.Fa dst . 327.Pp 328The 329.Fn pmap_zero_page 330function fills 331.Fa page 332with zeroes. 333.Sh INTERNAL DATA STRUCTURE MANAGEMENT 334.nr nS 1 335.Ft pmap_t 336.Fn pmap_create "void" 337.Ft void 338.Fn pmap_reference "pmap_t pmap" 339.Ft void 340.Fn pmap_destroy "pmap_t pmap" 341.nr nS 0 342.Pp 343The 344.Fn pmap_create 345function creates an instance of the 346.Em pmap 347structure. 348.Pp 349The 350.Fn pmap_reference 351function increments the reference count on 352.Fa pmap . 353.Pp 354The 355.Fn pmap_destroy 356function decrements the reference count on physical map 357.Fa pmap 358and retires it from service if the count drops to zero, assuming 359it contains no valid mappings. 360.Sh OPTIONAL FUNCTIONS 361.nr nS 1 362.Ft vaddr_t 363.Fn pmap_steal_memory "vsize_t size" "vaddr_t *vstartp" "vaddr_t *vendp" 364.Ft vaddr_t 365.Fn pmap_growkernel "vaddr_t maxkvaddr" 366.Ft void 367.Fn pmap_update "pmap_t pmap" 368.Ft void 369.Fn pmap_collect "pmap_t pmap" 370.Ft void 371.Fn pmap_virtual_space "vaddr_t *vstartp" "vaddr_t *vendp" 372.Ft void 373.Fn pmap_copy "pmap_t dst_pmap" "pmap_t src_pmap" "vaddr_t dst_addr" \ 374 "vsize_t len" "vaddr_t src_addr" 375.nr nS 0 376.Pp 377Wired memory allocation before the virtual memory system is bootstrapped 378is accomplished by the 379.Fn pmap_steal_memory 380function. 381After that point, the kernel memory allocation routines should be used. 382.Pp 383The 384.Fn pmap_growkernel 385function can preallocate kernel page tables to a specified virtual address. 386.Pp 387The 388.Fn pmap_update 389function notifies the 390.Nm pmap 391module to force processing of all delayed actions for all pmaps. 392.Pp 393The 394.Fn pmap_collect 395function informs the 396.Nm pmap 397module that the given 398.Em pmap 399is not expected to be used for some time, giving the 400.Nm pmap 401module a chance to prioritize. 402The initial bounds of the kernel virtual address space are returned by 403.Fn pmap_virtual_space . 404.Pp 405The 406.Fn pmap_copy 407function copies the range specified by 408.Fa src_addr 409and 410.Fa src_len 411from 412.Fa src_pmap 413to the range described by 414.Fa dst_addr 415and 416.Fa dst_len 417in 418.Fa dst_map . 419.Fn pmap_copy 420is called during a 421.Xr fork 2 422operation to give the child process an initial set of low-level 423mappings. 424.Sh SEE ALSO 425.Xr fork 2 , 426.Xr uvm 9 427.Sh HISTORY 428The 429.Bx 4.4 430.Nm pmap 431module is based on Mach 3.0. 432The introduction of 433.Xr uvm 9 434left the 435.Nm pmap 436interface unchanged for the most part. 437.Sh BUGS 438Ifdefs must be documented. 439.Pp 440.Fn pmap_update 441should be mandatory. 442