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