1.\" $NetBSD: pmap.9,v 1.44 2012/02/16 12:10:10 yamt Exp $ 2.\" 3.\" Copyright (c) 2000, 2001, 2002 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Jason R. Thorpe. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd February 16, 2012 31.Dt PMAP 9 32.Os 33.Sh NAME 34.Nm pmap 35.Nd machine-dependent portion of the virtual memory system 36.Sh SYNOPSIS 37.In sys/param.h 38.In uvm/uvm_extern.h 39.Ft void 40.Fn "pmap_init" "void" 41.Ft void 42.Fn "pmap_virtual_space" "vaddr_t *vstartp" "vaddr_t *vendp" 43.Ft vaddr_t 44.Fn "pmap_steal_memory" "vsize_t size" "vaddr_t *vstartp" "vaddr_t *vendp" 45.Ft pmap_t 46.Fn "pmap_kernel" "void" 47.Ft pmap_t 48.Fn "pmap_create" "void" 49.Ft void 50.Fn "pmap_destroy" "pmap_t pmap" 51.Ft void 52.Fn "pmap_reference" "pmap_t pmap" 53.Ft void 54.Fn "pmap_fork" "pmap_t src_map" "pmap_t dst_map" 55.Ft long 56.Fn "pmap_resident_count" "pmap_t pmap" 57.Ft long 58.Fn "pmap_wired_count" "pmap_t pmap" 59.Ft vaddr_t 60.Fn "pmap_growkernel" "vaddr_t maxkvaddr" 61.Ft int 62.Fn "pmap_enter" "pmap_t pmap" "vaddr_t va" "paddr_t pa" "vm_prot_t prot" \ 63 "u_int flags" 64.Ft void 65.Fn "pmap_remove" "pmap_t pmap" "vaddr_t sva" "vaddr_t eva" 66.Ft void 67.Fn "pmap_remove_all" "pmap_t pmap" 68.Ft void 69.Fn "pmap_protect" "pmap_t pmap" "vaddr_t sva" "vaddr_t eva" "vm_prot_t prot" 70.Ft void 71.Fn "pmap_unwire" "pmap_t pmap" "vaddr_t va" 72.Ft bool 73.Fn "pmap_extract" "pmap_t pmap" "vaddr_t va" "paddr_t *pap" 74.Ft void 75.Fn "pmap_kenter_pa" "vaddr_t va" "paddr_t pa" "vm_prot_t prot" "u_int flags" 76.Ft void 77.Fn "pmap_kremove" "vaddr_t va" "vsize_t size" 78.Ft void 79.Fn "pmap_copy" "pmap_t dst_map" "pmap_t src_map" "vaddr_t dst_addr" \ 80 "vsize_t len" "vaddr_t src_addr" 81.Ft void 82.Fn "pmap_update" "pmap_t pmap" 83.Ft void 84.Fn "pmap_activate" "struct lwp *l" 85.Ft void 86.Fn "pmap_deactivate" "struct lwp *l" 87.Ft void 88.Fn "pmap_zero_page" "paddr_t pa" 89.Ft void 90.Fn "pmap_copy_page" "paddr_t src" "paddr_t dst" 91.Ft void 92.Fn "pmap_page_protect" "struct vm_page *pg" "vm_prot_t prot" 93.Ft bool 94.Fn "pmap_clear_modify" "struct vm_page *pg" 95.Ft bool 96.Fn "pmap_clear_reference" "struct vm_page *pg" 97.Ft bool 98.Fn "pmap_is_modified" "struct vm_page *pg" 99.Ft bool 100.Fn "pmap_is_referenced" "struct vm_page *pg" 101.Ft paddr_t 102.Fn "pmap_phys_address" "paddr_t cookie" 103.Ft vaddr_t 104.Fn "PMAP_MAP_POOLPAGE" "paddr_t pa" 105.Ft paddr_t 106.Fn "PMAP_UNMAP_POOLPAGE" "vaddr_t va" 107.Ft void 108.Fn "PMAP_PREFER" "vaddr_t hint" "vaddr_t *vap" "vsize_t sz" "int td" 109.Sh DESCRIPTION 110The 111.Nm 112module is the machine-dependent portion of the 113.Nx 114virtual memory system 115.Xr uvm 9 . 116The purpose of the 117.Nm 118module is to manage physical address maps, to program the 119memory management hardware on the system, and perform any 120cache operations necessary to ensure correct operation of 121the virtual memory system. 122The 123.Nm 124module is also responsible for maintaining certain information 125required by 126.Xr uvm 9 . 127.Pp 128In order to cope with hardware architectures that make the 129invalidation of virtual address mappings expensive (e.g., 130TLB invalidations, TLB shootdown operations for multiple 131processors), the 132.Nm 133module is allowed to delay mapping invalidation or protection 134operations until such time as they are actually necessary. 135The functions that are allowed to delay such actions are 136.Fn pmap_enter , 137.Fn pmap_remove , 138.Fn pmap_protect , 139.Fn pmap_kenter_pa , 140and 141.Fn pmap_kremove . 142Callers of these functions must use the 143.Fn pmap_update 144function to notify the 145.Nm 146module that the mappings need to be made correct. 147Since the 148.Nm 149module is provided with information as to which processors are 150using a given physical map, the 151.Nm 152module may use whatever optimizations it has available to reduce 153the expense of virtual-to-physical mapping synchronization. 154.Ss HEADER FILES AND DATA STRUCTURES 155Machine-dependent code must provide the header file 156.In machine/pmap.h . 157This file contains the definition of the 158.Dv pmap 159structure: 160.Bd -literal -offset indent 161struct pmap { 162 /* Contents defined by pmap implementation. */ 163}; 164typedef struct pmap *pmap_t; 165.Ed 166.Pp 167This header file may also define other data structures that the 168.Nm 169implementation uses. 170.Pp 171Note that all prototypes for 172.Nm 173interface functions are provided by the header file 174.In uvm/uvm_pmap.h . 175It is possible to override this behavior by defining the 176C pre-processor macro 177.Dv PMAP_EXCLUDE_DECLS . 178This may be used to add a layer of indirection to 179.Nm 180API calls, for handling different MMU types in a single 181.Nm 182module, for example. 183If the 184.Dv PMAP_EXCLUDE_DECLS 185macro is defined, 186.In machine/pmap.h 187.Em must 188provide function prototypes in a block like so: 189.Bd -literal -offset indent 190#ifdef _KERNEL /* not exposed to user namespace */ 191__BEGIN_DECLS /* make safe for C++ */ 192/* Prototypes go here. */ 193__END_DECLS 194#endif /* _KERNEL */ 195.Ed 196.Pp 197The header file 198.In uvm/uvm_pmap.h 199defines a structure for tracking 200.Nm 201statistics (see below). 202This structure is defined as: 203.Bd -literal -offset indent 204struct pmap_statistics { 205 long resident_count; /* number of mapped pages */ 206 long wired_count; /* number of wired pages */ 207}; 208.Ed 209.Ss WIRED MAPPINGS 210The 211.Nm 212module is based on the premise that all information contained 213in the physical maps it manages is redundant. 214That is, physical map information may be 215.Dq forgotten 216by the 217.Nm 218module in the event that it is necessary to do so; it can be rebuilt 219by 220.Xr uvm 9 221by taking a page fault. 222There is one exception to this rule: so-called 223.Dq wired 224mappings may not be forgotten. 225Wired mappings are those for which either no high-level information 226exists with which to rebuild the mapping, or mappings which are needed 227by critical sections of code where taking a page fault is unacceptable. 228Information about which mappings are wired is provided to the 229.Nm 230module when a mapping is established. 231.Ss MODIFIED/REFERENCED INFORMATION 232The 233.Nm 234module is required to keep track of whether or not a page managed 235by the virtual memory system has been referenced or modified. 236This information is used by 237.Xr uvm 9 238to determine what happens to the page when scanned by the 239pagedaemon. 240.Pp 241Many CPUs provide hardware support for tracking 242modified/referenced information. 243However, many CPUs, particularly modern RISC CPUs, do not. 244On CPUs which lack hardware support for modified/referenced tracking, the 245.Nm 246module must emulate it in software. 247There are several strategies for doing this, and the best strategy 248depends on the CPU. 249.Pp 250The 251.Dq referenced 252attribute is used by the pagedaemon to determine if a page is 253.Dq active . 254Active pages are not candidates for re-use in the page replacement algorithm. 255Accurate referenced information is not required for correct operation; if 256supplying referenced information for a page is not feasible, then the 257.Nm 258implementation should always consider the 259.Dq referenced 260attribute to be 261.Dv false . 262.Pp 263The 264.Dq modified 265attribute is used by the pagedaemon to determine if a page needs 266to be cleaned (written to backing store; swap space, a regular file, etc.). 267Accurate modified information 268.Em must 269be provided by the 270.Nm 271module for correct operation of the virtual memory system. 272.Pp 273Note that modified/referenced information is only tracked for 274pages managed by the virtual memory system (i.e., pages for 275which a vm_page structure exists). 276In addition, only 277.Dq managed 278mappings of those pages have modified/referenced tracking. 279Mappings entered with the 280.Fn pmap_enter 281function are 282.Dq managed 283mappings. 284It is possible for 285.Dq unmanaged 286mappings of a page to be created, using the 287.Fn pmap_kenter_pa 288function. 289The use of 290.Dq unmanaged 291mappings should be limited to code which may execute in interrupt context 292(for example, the kernel memory allocator), or to enter mappings for 293physical addresses which are not managed by the virtual memory system. 294.Dq Unmanaged 295mappings may only be entered into the kernel's virtual address space. 296This constraint is placed on the callers of the 297.Fn pmap_kenter_pa 298and 299.Fn pmap_kremove 300functions so that the 301.Nm 302implementation need not block interrupts when manipulating data 303structures or holding locks. 304.Pp 305Also note that the modified/referenced information must be tracked 306on a per-page basis; they are not attributes of a mapping, but attributes 307of a page. 308Therefore, even after all mappings for a given page have 309been removed, the modified/referenced information for that page 310.Em must 311be preserved. 312The only time the modified/referenced attributes may 313be cleared is when the virtual memory system explicitly calls the 314.Fn pmap_clear_modify 315and 316.Fn pmap_clear_reference 317functions. 318These functions must also change any internal state necessary to detect 319the page being modified or referenced again after the modified or 320referenced state is cleared. 321(Prior to 322.Nx 1.6 , 323.Nm 324implementations could get away without this because UVM (and Mach VM 325before that) always called 326.Fn pmap_page_protect 327before clearing the modified or referenced state, but UVM has been changed 328to not do this anymore, so all 329.Nm 330implementations must now handle this.) 331.Ss STATISTICS 332The 333.Nm 334is required to keep statistics as to the number of 335.Dq resident 336pages and the number of 337.Dq wired 338pages. 339.Pp 340A 341.Dq resident 342page is one for which a mapping exists. 343This statistic is used to compute the resident size of a process and 344enforce resource limits. 345Only pages (whether managed by the virtual memory system or not) 346which are mapped into a physical map should be counted in the resident 347count. 348.Pp 349A 350.Dq wired 351page is one for which a wired mapping exists. 352This statistic is used to enforce resource limits. 353.Pp 354Note that it is recommended (though not required) that the 355.Nm 356implementation use the 357.Dv pmap_statistics 358structure in the tracking of 359.Nm 360statistics by placing it inside the 361.Dv pmap 362structure and adjusting the counts when mappings are established, changed, 363or removed. 364This avoids potentially expensive data structure traversals when the 365statistics are queried. 366.Ss REQUIRED FUNCTIONS 367This section describes functions that a 368.Nm 369module must provide to the virtual memory system. 370.Bl -tag -width indent -offset indent 371.It void Fn "pmap_init" "void" 372This function initializes the 373.Nm 374module. 375It is called by 376.Fn uvm_init 377to initialize any data structures that the module needs to 378manage physical maps. 379.It pmap_t Fn "pmap_kernel" "void" 380A machine independent macro which expands to 381.Va kernel_pmap_ptr . 382This variable must be exported by the platform's pmap module and it 383must point to the kernel pmap. 384.It void Fn "pmap_virtual_space" "vaddr_t *vstartp" "vaddr_t *vendp" 385The 386.Fn pmap_virtual_space 387function is called to determine the initial kernel virtual address 388space beginning and end. 389These values are used to create the kernel's virtual memory map. 390The function must set 391.Fa *vstartp 392to the first kernel virtual address that will be managed by 393.Xr uvm 9 , 394and must set 395.Fa *vendp 396to the last kernel virtual address that will be managed by 397.Xr uvm 9 . 398.Pp 399If the 400.Fn pmap_growkernel 401feature is used by a 402.Nm 403implementation, then 404.Fa *vendp 405should be set to the maximum kernel virtual address allowed by the 406implementation. 407If 408.Fn pmap_growkernel 409is not used, then 410.Fa *vendp 411.Em must 412be set to the maximum kernel virtual address that can be mapped with 413the resources currently allocated to map the kernel virtual address 414space. 415.It pmap_t Fn "pmap_create" "void" 416Create a physical map and return it to the caller. 417The reference count on the new map is 1. 418.It void Fn "pmap_destroy" "pmap_t pmap" 419Drop the reference count on the specified physical map. 420If the reference count drops to 0, all resources associated with the 421physical map are released and the physical map destroyed. 422In the case of a drop-to-0, no mappings will exist in the map. 423The 424.Nm 425implementation may assert this. 426.It void Fn "pmap_reference" "pmap_t pmap" 427Increment the reference count on the specified physical map. 428.It long Fn "pmap_resident_count" "pmap_t pmap" 429Query the 430.Dq resident pages 431statistic for 432.Fa pmap . 433.Pp 434Note that this function may be provided as a C pre-processor macro. 435.It long Fn "pmap_wired_count" "pmap_t pmap" 436Query the 437.Dq wired pages 438statistic for 439.Fa pmap . 440.Pp 441Note that this function may be provided as a C pre-processor macro. 442.It int Fn "pmap_enter" "pmap_t pmap" "vaddr_t va" "paddr_t pa" \ 443 "vm_prot_t prot" "u_int flags" 444Create a mapping in physical map 445.Fa pmap 446for the physical address 447.Fa pa 448at the virtual address 449.Fa va 450with protection specified by bits in 451.Fa prot : 452.Bl -tag -width "VM_PROT_EXECUTE " -offset indent 453.It VM_PROT_READ 454The mapping must allow reading. 455.It VM_PROT_WRITE 456The mapping must allow writing. 457.It VM_PROT_EXECUTE 458The page mapped contains instructions that will be executed by the 459processor. 460.El 461.Pp 462The 463.Fa flags 464argument contains protection bits (the same bits as used in the 465.Fa prot 466argument) indicating the type of access that caused the mapping to 467be created. 468This information may be used to seed modified/referenced 469information for the page being mapped, possibly avoiding redundant faults 470on platforms that track modified/referenced information in software. 471Other information provided by 472.Fa flags : 473.Bl -tag -width "PMAP_CANFAIL " -offset indent 474.It PMAP_WIRED 475The mapping being created is a wired mapping. 476.It PMAP_CANFAIL 477The call to 478.Fn pmap_enter 479is allowed to fail. 480If this flag is 481.Em not 482set, and the 483.Fn pmap_enter 484call is unable to create the mapping, perhaps due to insufficient 485resources, the 486.Nm 487module must panic. 488.It PMAP_NOCACHE 489The mapping being created is 490.Em not 491cached. 492Write accesses have a write-through policy. 493No speculative memory accesses. 494.It PMAP_WRITE_COMBINE 495The mapping being created is 496.Em not 497cached. 498Writes are combined and done in one burst. 499Speculative read accesses may be allowed. 500.It PMAP_WRITE_BACK 501All accesses to the created mapping are cached. 502On reads, cachelines become shared or exclusive if allocated on cache miss. 503On writes, cachelines become modified on a cache miss. 504.It PMAP_NOCACHE_OVR 505Same as PMAP_NOCACHE but mapping is overrideable (e.g. on x86 by MTRRs). 506.El 507.Pp 508The access type provided in the 509.Fa flags 510argument will never exceed the protection specified by 511.Fa prot . 512The 513.Nm 514implementation may assert this. 515Note that on systems that do not provide hardware support for 516tracking modified/referenced information, modified/referenced 517information for the page 518.Em must 519be seeded with the access type provided in 520.Fa flags 521if the 522.Dv PMAP_WIRED 523flag is set. 524This is to prevent a fault for the purpose of tracking 525modified/referenced information from occurring while the system is in 526a critical section where a fault would be unacceptable. 527.Pp 528Note that 529.Fn pmap_enter 530is sometimes called to enter a mapping at a virtual address 531for which a mapping already exists. 532In this situation, the implementation must take whatever action is 533necessary to invalidate the previous mapping before entering the new one. 534.Pp 535Also note that 536.Fn pmap_enter 537is sometimes called to change the protection for a pre-existing 538mapping, or to change the 539.Dq wired 540attribute for a pre-existing mapping. 541.Pp 542The 543.Fn pmap_enter 544function returns 0 on success or an error code indicating the mode 545of failure. 546.It void Fn "pmap_remove" "pmap_t pmap" "vaddr_t sva" "vaddr_t eva" 547Remove mappings from the virtual address range 548.Fa sva 549to 550.Fa eva 551from the specified physical map. 552.It void Fn "pmap_remove_all" "pmap_t pmap" 553This function is a hint to the 554.Nm pmap 555implementation that all entries in 556.Fa pmap 557will be removed before any more entries are entered. 558Following this call, there will be 559.Fn pmap_remove 560calls resulting in every mapping being removed, followed by either 561.Fn pmap_destroy 562or 563.Fn pmap_update . 564No other 565.Nm pmap 566interfaces which take 567.Fa pmap 568as an argument will be called during this process. 569Other interfaces which might need to access 570.Fa pmap 571(such as 572.Fn pmap_page_protect ) 573are permitted during this process. 574.Pp 575The 576.Nm pmap 577implementation is free to either remove all the 578.Nm pmap Ns 's 579mappings immediately in 580.Fn pmap_remove_all , 581or to use the knowledge of the upcoming 582.Fn pmap_remove 583calls to optimize the removals (or to just ignore this call). 584.Pp 585.It void Fn "pmap_protect" "pmap_t pmap" "vaddr_t sva" "vaddr_t eva" \ 586 "vm_prot_t prot" 587Set the protection of the mappings in the virtual address range 588.Fa sva 589to 590.Fa eva 591in the specified physical map. 592.It void Fn "pmap_unwire" "pmap_t pmap" "vaddr_t va" 593Clear the 594.Dq wired 595attribute on the mapping for virtual address 596.Fa va . 597.It bool Fn "pmap_extract" "pmap_t pmap" "vaddr_t va" "paddr_t *pap" 598This function extracts a mapping from the specified physical map. 599It serves two purposes: to determine if a mapping exists for the specified 600virtual address, and to determine what physical address is mapped at the 601specified virtual address. 602The 603.Fn pmap_extract 604should return the physical address for any kernel-accessible address, 605including KSEG-style direct-mapped kernel addresses. 606.Pp 607The 608.Fn pmap_extract 609function returns 610.Dv false 611if a mapping for 612.Fa va 613does not exist. 614Otherwise, it returns 615.Dv true 616and places the physical address mapped at 617.Fa va 618into 619.Fa *pap 620if the 621.Fa pap 622argument is non-NULL. 623.It void Fn "pmap_kenter_pa" "vaddr_t va" "paddr_t pa" "vm_prot_t prot" \ 624 "u_int flags" 625Enter an 626.Dq unmanaged 627mapping for physical address 628.Fa pa 629at virtual address 630.Fa va 631with protection specified by bits in 632.Fa prot : 633.Bl -tag -width "VM_PROT_EXECUTE " -offset indent 634.It VM_PROT_READ 635The mapping must allow reading. 636.It VM_PROT_WRITE 637The mapping must allow writing. 638.It VM_PROT_EXECUTE 639The page mapped contains instructions that will be executed by the 640processor. 641.El 642.Pp 643Information provided by 644.Fa flags : 645.Bl -tag -width "PMAP_NOCACHE " -offset indent 646.It PMAP_NOCACHE 647The mapping being created is 648.Em not 649cached. 650Write accesses have a write-through policy. 651No speculative memory accesses. 652.It PMAP_WRITE_COMBINE 653The mapping being created is 654.Em not 655cached. 656Writes are combined and done in one burst. 657Speculative read accesses may be allowed. 658.It PMAP_WRITE_BACK 659All accesses to the created mapping are cached. 660On reads, cachelines become shared or exclusive if allocated on cache miss. 661On writes, cachelines become modified on a cache miss. 662.It PMAP_NOCACHE_OVR 663Same as PMAP_NOCACHE but mapping is overrideable (e.g. on x86 by MTRRs). 664.El 665.Pp 666Mappings of this type are always 667.Dq wired , 668and are unaffected by routines that alter the protection of pages 669(such as 670.Fn pmap_page_protect ) . 671Such mappings are also not included in the gathering of modified/referenced 672information about a page. 673Mappings entered with 674.Fn pmap_kenter_pa 675by machine-independent code 676.Em must not 677have execute permission, as the 678data structures required to track execute permission of a page may not 679be available to 680.Fn pmap_kenter_pa . 681Machine-independent code is not allowed to enter a mapping with 682.Fn pmap_kenter_pa 683at a virtual address for which a valid mapping already exists. 684Mappings created with 685.Fn pmap_kenter_pa 686may be removed only with a call to 687.Fn pmap_kremove . 688.Pp 689Note that 690.Fn pmap_kenter_pa 691must be safe for use in interrupt context. 692.Fn splvm 693blocks interrupts that might cause 694.Fn pmap_kenter_pa 695to be called. 696.It void Fn "pmap_kremove" "vaddr_t va" "vsize_t size" 697Remove all mappings starting at virtual address 698.Fa va 699for 700.Fa size 701bytes from the kernel physical map. 702All mappings that are removed must be the 703.Dq unmanaged 704type created with 705.Fn pmap_kenter_pa . 706The implementation may assert this. 707.It void Fn "pmap_copy" "pmap_t dst_map" "pmap_t src_map" "vaddr_t dst_addr" \ 708 "vsize_t len" "vaddr_t src_addr" 709This function copies the mappings starting at 710.Fa src_addr 711in 712.Fa src_map 713for 714.Fa len 715bytes into 716.Fa dst_map 717starting at 718.Fa dst_addr . 719.Pp 720Note that while this function is required to be provided by a 721.Nm 722implementation, it is not actually required to do anything. 723.Fn pmap_copy 724is merely advisory (it is used in the 725.Xr fork 2 726path to 727.Dq pre-fault 728the child's address space). 729.It void Fn "pmap_update" "pmap_t pmap" 730This function is used to inform the 731.Nm 732module that all physical mappings, for the specified pmap, must now be 733correct. 734That is, all delayed virtual-to-physical mappings updates (such as TLB 735invalidation or address space identifier updates) must be completed. 736This routine must be used after calls to 737.Fn pmap_enter , 738.Fn pmap_remove , 739.Fn pmap_protect , 740.Fn pmap_kenter_pa , 741and 742.Fn pmap_kremove 743in order to ensure correct operation of the virtual memory system. 744.Pp 745If a 746.Nm 747implementation does not delay virtual-to-physical mapping updates, 748.Fn pmap_update 749has no operation. 750In this case, the call may be deleted using a C pre-processor macro in 751.In machine/pmap.h . 752.It void Fn "pmap_activate" "struct lwp *l" 753Activate the physical map used by the process behind lwp 754.Fa l . 755This is called by the virtual memory system when the 756virtual memory context for a process is changed, and is also 757often used by machine-dependent context switch code to program 758the memory management hardware with the process's page table 759base, etc. 760Note that 761.Fn pmap_activate 762may not always be called when 763.Fa l 764is the current lwp. 765.Fn pmap_activate 766must be able to handle this scenario. 767.It void Fn "pmap_deactivate" "struct lwp *l" 768Deactivate the physical map used by the process behind lwp 769.Fa l . 770It is generally used in conjunction with 771.Fn pmap_activate . 772Like 773.Fn pmap_activate , 774.Fn pmap_deactivate 775may not always be called when 776.Fa l 777is the current lwp. 778.It void Fn "pmap_zero_page" "paddr_t pa" 779Zero the PAGE_SIZE sized region starting at physical address 780.Fa pa . 781The 782.Nm 783implementation must take whatever steps are necessary to map the 784page to a kernel-accessible address and zero the page. 785It is suggested that implementations use an optimized zeroing algorithm, 786as the performance of this function directly impacts page fault performance. 787The implementation may assume that the region is 788PAGE_SIZE aligned and exactly PAGE_SIZE bytes in length. 789.Pp 790Note that the cache configuration of the platform should also be 791considered in the implementation of 792.Fn pmap_zero_page . 793For example, on systems with a physically-addressed cache, the cache 794load caused by zeroing the page will not be wasted, as the zeroing is 795usually done on-demand. 796However, on systems with a virtually-addressed cached, the cache load 797caused by zeroing the page 798.Em will 799be wasted, as the page will be mapped at a virtual address which is 800different from that used to zero the page. 801In the virtually-addressed cache case, care should also be taken to 802avoid cache alias problems. 803.It void Fn "pmap_copy_page" "paddr_t src" "paddr_t dst" 804Copy the PAGE_SIZE sized region starting at physical address 805.Fa src 806to the same sized region starting at physical address 807.Fa dst . 808The 809.Nm 810implementation must take whatever steps are necessary to map the 811source and destination pages to a kernel-accessible address and 812perform the copy. 813It is suggested that implementations use an optimized copy algorithm, 814as the performance of this function directly impacts page fault performance. 815The implementation may assume that both regions are PAGE_SIZE aligned 816and exactly PAGE_SIZE bytes in length. 817.Pp 818The same cache considerations that apply to 819.Fn pmap_zero_page 820apply to 821.Fn pmap_copy_page . 822.It void Fn "pmap_page_protect" "struct vm_page *pg" "vm_prot_t prot" 823Lower the permissions for all mappings of the page 824.Fa pg 825to 826.Fa prot . 827This function is used by the virtual memory system to implement 828copy-on-write (called with VM_PROT_READ set in 829.Fa prot ) 830and to revoke all mappings when cleaning a page (called with 831no bits set in 832.Fa prot ) . 833Access permissions must never be added to a page as a result of 834this call. 835.It bool Fn "pmap_clear_modify" "struct vm_page *pg" 836Clear the 837.Dq modified 838attribute on the page 839.Fa pg . 840.Pp 841The 842.Fn pmap_clear_modify 843function returns 844.Dv true 845or 846.Dv false 847indicating whether or not the 848.Dq modified 849attribute was set on the page before it was cleared. 850.Pp 851Note that this function may be provided as a C pre-processor macro. 852.It bool Fn "pmap_clear_reference" "struct vm_page *pg" 853Clear the 854.Dq referenced 855attribute on the page 856.Fa pg . 857.Pp 858The 859.Fn pmap_clear_reference 860function returns 861.Dv true 862or 863.Dv false 864indicating whether or not the 865.Dq referenced 866attribute was set on the page before it was cleared. 867.Pp 868Note that this function may be provided as a C pre-processor macro. 869.It bool Fn "pmap_is_modified" "struct vm_page *pg" 870Test whether or not the 871.Dq modified 872attribute is set on page 873.Fa pg . 874.Pp 875Note that this function may be provided as a C pre-processor macro. 876.It bool Fn "pmap_is_referenced" "struct vm_page *pg" 877Test whether or not the 878.Dq referenced 879attribute is set on page 880.Fa pg . 881.Pp 882Note that this function may be provided as a C pre-processor macro. 883.It paddr_t Fn "pmap_phys_address" "paddr_t cookie" 884Convert a cookie returned by a device 885.Fn mmap 886function into a physical address. 887This function is provided to accommodate systems which have physical 888address spaces larger than can be directly addressed by the platform's 889.Fa paddr_t 890type. 891The existence of this function is highly dubious, and it is 892expected that this function will be removed from the 893.Nm pmap 894API in a future release of 895.Nx . 896.Pp 897Note that this function may be provided as a C pre-processor macro. 898.El 899.Ss OPTIONAL FUNCTIONS 900This section describes several optional functions in the 901.Nm 902API. 903.Bl -tag -width indent -offset indent 904.It vaddr_t Fn "pmap_steal_memory" "vsize_t size" "vaddr_t *vstartp" \ 905 "vaddr_t *vendp" 906This function is a bootstrap memory allocator, which may be provided 907as an alternative to the bootstrap memory allocator used within 908.Xr uvm 9 909itself. 910It is particularly useful on systems which provide for example a direct-mapped 911memory segment. 912This function works by stealing pages from the (to be) managed memory 913pool, which has already been provided to 914.Xr uvm 9 915in the vm_physmem[] array. 916The pages are then mapped, or otherwise made accessible to the kernel, 917in a machine-dependent way. 918The memory must be zeroed by 919.Fn pmap_steal_memory . 920Note that memory allocated with 921.Fn pmap_steal_memory 922will never be freed, and mappings made by 923.Fn pmap_steal_memory 924must never be 925.Dq forgotten . 926.Pp 927Note that 928.Fn pmap_steal_memory 929should not be used as a general-purpose early-startup memory 930allocation routine. 931It is intended to be used only by the 932.Fn uvm_pageboot_alloc 933routine and its supporting routines. 934If you need to allocate memory before the virtual memory system is 935initialized, use 936.Fn uvm_pageboot_alloc . 937See 938.Xr uvm 9 939for more information. 940.Pp 941The 942.Fn pmap_steal_memory 943function returns the kernel-accessible address of the allocated memory. 944If no memory can be allocated, or if allocated memory cannot be mapped, 945the function must panic. 946.Pp 947If the 948.Fn pmap_steal_memory 949function uses address space from the range provided to 950.Xr uvm 9 951by the 952.Fn pmap_virtual_space 953call, then 954.Fn pmap_steal_memory 955must adjust 956.Fa *vstartp 957and 958.Fa *vendp 959upon return. 960.Pp 961The 962.Fn pmap_steal_memory 963function is enabled by defining the C pre-processor macro 964.Dv PMAP_STEAL_MEMORY 965in 966.In machine/pmap.h . 967.It vaddr_t Fn "pmap_growkernel" "vaddr_t maxkvaddr" 968Management of the kernel virtual address space is complicated by the 969fact that it is not always safe to wait for resources with which to 970map a kernel virtual address. 971However, it is not always desirable to pre-allocate all resources 972necessary to map the entire kernel virtual address space. 973.Pp 974The 975.Fn pmap_growkernel 976interface is designed to help alleviate this problem. 977The virtual memory startup code may choose to allocate an initial set 978of mapping resources (e.g., page tables) and set an internal variable 979indicating how much kernel virtual address space can be mapped using 980those initial resources. 981Then, when the virtual memory system wishes to map something 982at an address beyond that initial limit, it calls 983.Fn pmap_growkernel 984to pre-allocate more sources with which to create the mapping. 985Note that once additional kernel virtual address space mapping resources 986have been allocated, they should not be freed; it is likely they will 987be needed again. 988.Pp 989The 990.Fn pmap_growkernel 991function returns the new maximum kernel virtual address that can be mapped 992with the resources it has available. 993If new resources cannot be allocated, 994.Fn pmap_growkernel 995must panic. 996.Pp 997The 998.Fn pmap_growkernel 999function is enabled by defining the C pre-processor macro 1000.Dv PMAP_GROWKERNEL 1001in 1002.In machine/pmap.h . 1003.It void Fn "pmap_fork" "pmap_t src_map" "pmap_t dst_map" 1004Some 1005.Nm 1006implementations may need to keep track of other information not 1007directly related to the virtual address space. 1008For example, on the i386 port, the Local Descriptor Table state of a 1009process is associated with the pmap (this is due to the fact that 1010applications manipulate the Local Descriptor Table directly expect it 1011to be logically associated with the virtual memory state of the process). 1012.Pp 1013The 1014.Fn pmap_fork 1015function is provided as a way to associate information from 1016.Fa src_map 1017with 1018.Fa dst_map 1019when a 1020.Dv vmspace 1021is forked. 1022.Fn pmap_fork 1023is called from 1024.Fn uvmspace_fork . 1025.Pp 1026The 1027.Fn pmap_fork 1028function is enabled by defining the C pre-processor macro 1029.Dv PMAP_FORK 1030in 1031.In machine/pmap.h . 1032.It vaddr_t Fn "PMAP_MAP_POOLPAGE" "paddr_t pa" 1033This function is used by the 1034.Xr pool 9 1035memory pool manager. 1036Pools allocate backing pages one at a time. 1037This is provided as a means to use hardware features such as a 1038direct-mapped memory segment to map the pages used by the 1039.Xr pool 9 1040allocator. 1041This can lead to better performance by e.g. reducing TLB contention. 1042.Pp 1043.Fn PMAP_MAP_POOLPAGE 1044returns the kernel-accessible address of the page being mapped. 1045It must always succeed. 1046.Pp 1047The use of 1048.Fn PMAP_MAP_POOLPAGE 1049is enabled by defining it as a C pre-processor macro in 1050.In machine/pmap.h . 1051If 1052.Fn PMAP_MAP_POOLPAGE 1053is defined, 1054.Fn PMAP_UNMAP_POOLPAGE 1055must also be defined. 1056.Pp 1057The following is an example of how to define 1058.Fn PMAP_MAP_POOLPAGE : 1059.Bd -literal -offset indent 1060#define PMAP_MAP_POOLPAGE(pa) MIPS_PHYS_TO_KSEG0((pa)) 1061.Ed 1062.Pp 1063This takes the physical address of a page and returns the KSEG0 1064address of that page on a MIPS processor. 1065.It paddr_t Fn "PMAP_UNMAP_POOLPAGE" "vaddr_t va" 1066This function is the inverse of 1067.Fn PMAP_MAP_POOLPAGE . 1068.Pp 1069.Fn PMAP_UNMAP_POOLPAGE 1070returns the physical address of the page corresponding to the 1071provided kernel-accessible address. 1072.Pp 1073The use of 1074.Fn PMAP_UNMAP_POOLPAGE 1075is enabled by defining it as a C pre-processor macro in 1076.In machine/pmap.h . 1077If 1078.Fn PMAP_UNMAP_POOLPAGE 1079is defined, 1080.Fn PMAP_MAP_POOLPAGE 1081must also be defined. 1082.Pp 1083The following is an example of how to define 1084.Fn PMAP_UNMAP_POOLPAGE : 1085.Bd -literal -offset indent 1086#define PMAP_UNMAP_POOLPAGE(pa) MIPS_KSEG0_TO_PHYS((va)) 1087.Ed 1088.Pp 1089This takes the KSEG0 address of a previously-mapped pool page 1090and returns the physical address of that page on a MIPS processor. 1091.It void Fn "PMAP_PREFER" "vaddr_t hint" "vaddr_t *vap" "vsize_t sz" "int td" 1092This function is used by 1093.Xr uvm_map 9 1094to adjust a virtual address being allocated in order to avoid 1095cache alias problems. 1096If necessary, the virtual address pointed by 1097.Fa vap 1098will be advanced. 1099.Fa hint 1100is an object offset which will be mapped into the resulting virtual address, and 1101.Fa sz 1102is size of the region being mapped in bytes. 1103.Fa td 1104indicates if the machine dependent pmap uses the topdown VM. 1105.Pp 1106The use of 1107.Fn PMAP_PREFER 1108is enabled by defining it as a C pre-processor macro in 1109.In machine/pmap.h . 1110.Pp 1111.It void Fn "pmap_procwr" "struct proc *p" "vaddr_t va" "vsize_t size" 1112Synchronize CPU instruction caches of the specified range. 1113The address space is designated by 1114.Fa p . 1115This function is typically used to flush instruction caches 1116after code modification. 1117.Pp 1118The use of 1119.Fn pmap_procwr 1120is enabled by defining a C pre-processor macro 1121.Dv PMAP_NEED_PROCWR 1122in 1123.In machine/pmap.h . 1124.El 1125.Sh SEE ALSO 1126.Xr uvm 9 1127.Sh HISTORY 1128The 1129.Nm 1130module was originally part of the design of the virtual memory system 1131in the Mach Operating System. 1132The goal was to provide a clean separation between the machine-independent 1133and the machine-dependent portions of the virtual memory system, in 1134stark contrast to the original 1135.Bx 3 1136virtual memory system, which was specific to the VAX. 1137.Pp 1138Between 1139.Bx 4.3 1140and 1141.Bx 4.4 , 1142the Mach virtual memory system, including the 1143.Nm 1144API, was ported to 1145.Bx 1146and included in the 1147.Bx 4.4 1148release. 1149.Pp 1150.Nx 1151inherited the 1152.Bx 1153version of the Mach virtual memory system. 1154.Nx 1.4 1155was the first 1156.Nx 1157release with the new 1158.Xr uvm 9 1159virtual memory system, which included several changes to the 1160.Nm 1161API. 1162Since the introduction of 1163.Xr uvm 9 , 1164the 1165.Nm 1166API has evolved further. 1167.Sh AUTHORS 1168The original Mach VAX 1169.Nm 1170module was written by 1171.An Avadis Tevanian, Jr. 1172and 1173.An Michael Wayne Young . 1174.Pp 1175.An Mike Hibler 1176did the integration of the Mach virtual memory system into 1177.Bx 4.4 1178and implemented a 1179.Nm 1180module for the Motorola 68020+68851/68030/68040. 1181.Pp 1182The 1183.Nm 1184API as it exists in 1185.Nx 1186is derived from 1187.Bx 4.4 , 1188and has been modified by 1189.An Chuck Cranor , 1190.An Charles M. Hannum , 1191.An Chuck Silvers , 1192.An Wolfgang Solfrank , 1193.An Bill Sommerfeld , 1194and 1195.An Jason R. Thorpe . 1196.Pp 1197The author of this document is 1198.An Jason R. Thorpe 1199.Aq thorpej@NetBSD.org . 1200.Sh BUGS 1201The use and definition of 1202.Fn pmap_activate 1203and 1204.Fn pmap_deactivate 1205needs to be reexamined. 1206.Pp 1207The use of 1208.Fn pmap_copy 1209needs to be reexamined. 1210Empirical evidence suggests that performance of the system suffers when 1211.Fn pmap_copy 1212actually performs its defined function. 1213This is largely due to the fact that the copy of the virtual-to-physical 1214mappings is wasted if the process calls 1215.Xr execve 2 1216after 1217.Xr fork 2 . 1218For this reason, it is recommended that 1219.Nm 1220implementations leave the body of the 1221.Fn pmap_copy 1222function empty for now. 1223