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