2 # Copyright (c) 2005 Peter Grehan
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
31 #include <sys/mutex.h>
32 #include <sys/systm.h>
35 #include <vm/vm_page.h>
37 #include <machine/mmuvar.h>
40 * @defgroup MMU mmu - KObj methods for PowerPC MMU implementations
41 * @brief A set of methods required by all MMU implementations. These
42 * are basically direct call-thru's from the pmap machine-dependent
44 * Thanks to Bruce M Simpson's pmap man pages for routine descriptions.
51 # Default implementations of some methods
54 static void mmu_null_copy(mmu_t mmu, pmap_t dst_pmap, pmap_t src_pmap,
55 vm_offset_t dst_addr, vm_size_t len, vm_offset_t src_addr)
60 static void mmu_null_growkernel(mmu_t mmu, vm_offset_t addr)
65 static void mmu_null_init(mmu_t mmu)
70 static boolean_t mmu_null_is_prefaultable(mmu_t mmu, pmap_t pmap,
76 static void mmu_null_object_init_pt(mmu_t mmu, pmap_t pmap,
77 vm_offset_t addr, vm_object_t object, vm_pindex_t index,
83 static void mmu_null_page_init(mmu_t mmu, vm_page_t m)
88 static void mmu_null_remove_pages(mmu_t mmu, pmap_t pmap)
93 static int mmu_null_mincore(mmu_t mmu, pmap_t pmap, vm_offset_t addr)
98 static void mmu_null_deactivate(struct thread *td)
103 static void mmu_null_align_superpage(mmu_t mmu, vm_object_t object,
104 vm_ooffset_t offset, vm_offset_t *addr, vm_size_t size)
109 static struct pmap_md *mmu_null_scan_md(mmu_t mmu, struct pmap_md *p)
114 static void *mmu_null_mapdev_attr(mmu_t mmu, vm_offset_t pa,
115 vm_size_t size, vm_memattr_t ma)
117 return MMU_MAPDEV(mmu, pa, size);
120 static void mmu_null_kenter_attr(mmu_t mmu, vm_offset_t va,
121 vm_offset_t pa, vm_memattr_t ma)
123 MMU_KENTER(mmu, va, pa);
126 static void mmu_null_page_set_memattr(mmu_t mmu, vm_page_t m,
135 * @brief Change the wiring attribute for the page in the given physical
136 * map and virtual address.
138 * @param _pmap physical map of page
139 * @param _va page virtual address
140 * @param _wired TRUE to increment wired count, FALSE to decrement
142 METHOD void change_wiring {
151 * @brief Clear the 'modified' bit on the given physical page
153 * @param _pg physical page
155 METHOD void clear_modify {
162 * @brief Clear the 'referenced' bit on the given physical page
164 * @param _pg physical page
166 METHOD void clear_reference {
173 * @brief Clear the write and modified bits in each of the given
174 * physical page's mappings
176 * @param _pg physical page
178 METHOD void remove_write {
185 * @brief Copy the address range given by the source physical map, virtual
186 * address and length to the destination physical map and virtual address.
187 * This routine is optional (xxx default null implementation ?)
189 * @param _dst_pmap destination physical map
190 * @param _src_pmap source physical map
191 * @param _dst_addr destination virtual address
192 * @param _len size of range
193 * @param _src_addr source virtual address
199 vm_offset_t _dst_addr;
201 vm_offset_t _src_addr;
202 } DEFAULT mmu_null_copy;
206 * @brief Copy the source physical page to the destination physical page
208 * @param _src source physical page
209 * @param _dst destination physical page
211 METHOD void copy_page {
219 * @brief Create a mapping between a virtual/physical address pair in the
220 * passed physical map with the specified protection and wiring
222 * @param _pmap physical map
223 * @param _va mapping virtual address
224 * @param _p mapping physical page
225 * @param _prot mapping page protection
226 * @param _wired TRUE if page will be wired
239 * @brief Maps a sequence of resident pages belonging to the same object.
241 * @param _pmap physical map
242 * @param _start virtual range start
243 * @param _end virtual range end
244 * @param _m_start physical page mapped at start
245 * @param _prot mapping page protection
247 METHOD void enter_object {
258 * @brief A faster entry point for page mapping where it is possible
259 * to short-circuit some of the tests in pmap_enter.
261 * @param _pmap physical map (and also currently active pmap)
262 * @param _va mapping virtual address
263 * @param _pg mapping physical page
264 * @param _prot new page protection - used to see if page is exec.
266 METHOD void enter_quick {
276 * @brief Reverse map the given virtual address, returning the physical
277 * page associated with the address if a mapping exists.
279 * @param _pmap physical map
280 * @param _va mapping virtual address
282 * @retval 0 No mapping found
283 * @retval addr The mapping physical address
285 METHOD vm_paddr_t extract {
293 * @brief Reverse map the given virtual address, returning the
294 * physical page if found. The page must be held (by calling
295 * vm_page_hold) if the page protection matches the given protection
297 * @param _pmap physical map
298 * @param _va mapping virtual address
299 * @param _prot protection used to determine if physical page
302 * @retval NULL No mapping found
303 * @retval page Pointer to physical page. Held if protections match
305 METHOD vm_page_t extract_and_hold {
314 * @brief Increase kernel virtual address space to the given virtual address.
315 * Not really required for PowerPC, so optional unless the MMU implementation
318 * @param _va new upper limit for kernel virtual address space
320 METHOD void growkernel {
323 } DEFAULT mmu_null_growkernel;
327 * @brief Called from vm_mem_init. Zone allocation is available at
328 * this stage so a convenient time to create zones. This routine is
329 * for MMU-implementation convenience and is optional.
333 } DEFAULT mmu_null_init;
337 * @brief Return if the page has been marked by MMU hardware to have been
340 * @param _pg physical page to test
342 * @retval boolean TRUE if page has been modified
344 METHOD boolean_t is_modified {
351 * @brief Return whether the specified virtual address is a candidate to be
352 * prefaulted in. This routine is optional.
354 * @param _pmap physical map
355 * @param _va virtual address to test
357 * @retval boolean TRUE if the address is a candidate.
359 METHOD boolean_t is_prefaultable {
363 } DEFAULT mmu_null_is_prefaultable;
367 * @brief Return a count of referenced bits for a page, clearing those bits.
368 * Not all referenced bits need to be cleared, but it is necessary that 0
369 * only be returned when there are none set.
371 * @params _m physical page
373 * @retval int count of referenced bits
375 METHOD boolean_t ts_referenced {
382 * @brief Map the requested physical address range into kernel virtual
383 * address space. The value in _virt is taken as a hint. The virtual
384 * address of the range is returned, or NULL if the mapping could not
385 * be created. The range can be direct-mapped if that is supported.
387 * @param *_virt Hint for start virtual address, and also return
389 * @param _start physical address range start
390 * @param _end physical address range end
391 * @param _prot protection of range (currently ignored)
393 * @retval NULL could not map the area
394 * @retval addr, *_virt mapping start virtual address
396 METHOD vm_offset_t map {
406 * @brief Used to create a contiguous set of read-only mappings for a
407 * given object to try and eliminate a cascade of on-demand faults as
408 * the object is accessed sequentially. This routine is optional.
410 * @param _pmap physical map
411 * @param _addr mapping start virtual address
412 * @param _object device-backed V.M. object to be mapped
413 * @param _pindex page-index within object of mapping start
414 * @param _size size in bytes of mapping
416 METHOD void object_init_pt {
423 } DEFAULT mmu_null_object_init_pt;
427 * @brief Used to determine if the specified page has a mapping for the
428 * given physical map, by scanning the list of reverse-mappings from the
429 * page. The list is scanned to a maximum of 16 entries.
431 * @param _pmap physical map
432 * @param _pg physical page
434 * @retval bool TRUE if the physical map was found in the first 16
435 * reverse-map list entries off the physical page.
437 METHOD boolean_t page_exists_quick {
445 * @brief Initialise the machine-dependent section of the physical page
446 * data structure. This routine is optional.
448 * @param _pg physical page
450 METHOD void page_init {
453 } DEFAULT mmu_null_page_init;
457 * @brief Count the number of managed mappings to the given physical
458 * page that are wired.
460 * @param _pg physical page
462 * @retval int the number of wired, managed mappings to the
463 * given physical page
465 METHOD int page_wired_mappings {
472 * @brief Initialise a physical map data structure
474 * @param _pmap physical map
483 * @brief Initialise the physical map for process 0, the initial process
485 * XXX default to pinit ?
487 * @param _pmap physical map
496 * @brief Set the protection for physical pages in the given virtual address
497 * range to the given value.
499 * @param _pmap physical map
500 * @param _start virtual range start
501 * @param _end virtual range end
502 * @param _prot new page protection
504 METHOD void protect {
514 * @brief Create a mapping in kernel virtual address space for the given array
515 * of wired physical pages.
517 * @param _start mapping virtual address start
518 * @param *_m array of physical page pointers
519 * @param _count array elements
530 * @brief Remove the temporary mappings created by qenter.
532 * @param _start mapping virtual address start
533 * @param _count number of pages in mapping
535 METHOD void qremove {
543 * @brief Release per-pmap resources, e.g. mutexes, allocated memory etc. There
544 * should be no existing mappings for the physical map at this point
546 * @param _pmap physical map
548 METHOD void release {
555 * @brief Remove all mappings in the given physical map for the start/end
556 * virtual address range. The range will be page-aligned.
558 * @param _pmap physical map
559 * @param _start mapping virtual address start
560 * @param _end mapping virtual address end
571 * @brief Traverse the reverse-map list off the given physical page and
572 * remove all mappings. Clear the PG_WRITEABLE attribute from the page.
574 * @param _pg physical page
576 METHOD void remove_all {
583 * @brief Remove all mappings in the given start/end virtual address range
584 * for the given physical map. Similar to the remove method, but it used
585 * when tearing down all mappings in an address space. This method is
586 * optional, since pmap_remove will be called for each valid vm_map in
587 * the address space later.
589 * @param _pmap physical map
590 * @param _start mapping virtual address start
591 * @param _end mapping virtual address end
593 METHOD void remove_pages {
596 } DEFAULT mmu_null_remove_pages;
600 * @brief Zero a physical page. It is not assumed that the page is mapped,
601 * so a temporary (or direct) mapping may need to be used.
603 * @param _pg physical page
605 METHOD void zero_page {
612 * @brief Zero a portion of a physical page, starting at a given offset and
613 * for a given size (multiples of 512 bytes for 4k pages).
615 * @param _pg physical page
616 * @param _off byte offset from start of page
617 * @param _size size of area to zero
619 METHOD void zero_page_area {
628 * @brief Called from the idle loop to zero pages. XXX I think locking
629 * constraints might be different here compared to zero_page.
631 * @param _pg physical page
633 METHOD void zero_page_idle {
640 * @brief Extract mincore(2) information from a mapping. This routine is
641 * optional and is an optimisation: the mincore code will call is_modified
642 * and ts_referenced if no result is returned.
644 * @param _pmap physical map
645 * @param _addr page virtual address
647 * @retval 0 no result
648 * @retval non-zero mincore(2) flag values
654 } DEFAULT mmu_null_mincore;
658 * @brief Perform any operations required to allow a physical map to be used
659 * before it's address space is accessed.
661 * @param _td thread associated with physical map
663 METHOD void activate {
669 * @brief Perform any operations required to deactivate a physical map,
670 * for instance as it is context-switched out.
672 * @param _td thread associated with physical map
674 METHOD void deactivate {
677 } DEFAULT mmu_null_deactivate;
680 * @brief Return a hint for the best virtual address to map a tentative
681 * virtual address range in a given VM object. The default is to just
682 * return the given tentative start address.
684 * @param _obj VM backing object
685 * @param _offset starting offset with the VM object
686 * @param _addr initial guess at virtual address
687 * @param _size size of virtual address range
689 METHOD void align_superpage {
692 vm_ooffset_t _offset;
695 } DEFAULT mmu_null_align_superpage;
701 * INTERNAL INTERFACES
705 * @brief Bootstrap the VM system. At the completion of this routine, the
706 * kernel will be running in it's own address space with full control over
709 * @param _start start of reserved memory (obsolete ???)
710 * @param _end end of reserved memory (obsolete ???)
711 * XXX I think the intent of these was to allow
712 * the memory used by kernel text+data+bss and
713 * loader variables/load-time kld's to be carved out
714 * of available physical mem.
717 METHOD void bootstrap {
724 * @brief Set up the MMU on the current CPU. Only called by the PMAP layer
725 * for alternate CPUs on SMP systems.
727 * @param _ap Set to 1 if the CPU being set up is an AP
730 METHOD void cpu_bootstrap {
737 * @brief Create a kernel mapping for a given physical address range.
738 * Called by bus code on behalf of device drivers. The mapping does not
739 * have to be a virtual address: it can be a direct-mapped physical address
740 * if that is supported by the MMU.
742 * @param _pa start physical address
743 * @param _size size in bytes of mapping
745 * @retval addr address of mapping.
747 METHOD void * mapdev {
754 * @brief Create a kernel mapping for a given physical address range.
755 * Called by bus code on behalf of device drivers. The mapping does not
756 * have to be a virtual address: it can be a direct-mapped physical address
757 * if that is supported by the MMU.
759 * @param _pa start physical address
760 * @param _size size in bytes of mapping
761 * @param _attr cache attributes
763 * @retval addr address of mapping.
765 METHOD void * mapdev_attr {
770 } DEFAULT mmu_null_mapdev_attr;
773 * @brief Change cache control attributes for a page. Should modify all
774 * mappings for that page.
776 * @param _m page to modify
777 * @param _ma new cache control attributes
779 METHOD void page_set_memattr {
783 } DEFAULT mmu_null_page_set_memattr;
786 * @brief Remove the mapping created by mapdev. Called when a driver
789 * @param _va Mapping address returned from mapdev
790 * @param _size size in bytes of mapping
792 METHOD void unmapdev {
800 * @brief Reverse-map a kernel virtual address
802 * @param _va kernel virtual address to reverse-map
804 * @retval pa physical address corresponding to mapping
806 METHOD vm_offset_t kextract {
813 * @brief Map a wired page into kernel virtual address space
815 * @param _va mapping virtual address
816 * @param _pa mapping physical address
825 * @brief Map a wired page into kernel virtual address space
827 * @param _va mapping virtual address
828 * @param _pa mapping physical address
829 * @param _ma mapping cache control attributes
831 METHOD void kenter_attr {
836 } DEFAULT mmu_null_kenter_attr;
839 * @brief Determine if the given physical address range has been direct-mapped.
841 * @param _pa physical address start
842 * @param _size physical address range size
844 * @retval bool TRUE if the range is direct-mapped.
846 METHOD boolean_t dev_direct_mapped {
854 * @brief Enforce instruction cache coherency. Typically called after a
855 * region of memory has been modified and before execution of or within
856 * that region is attempted. Setting breakpoints in a process through
857 * ptrace(2) is one example of when the instruction cache needs to be
860 * @param _pm the physical map of the virtual address
861 * @param _va the virtual address of the modified region
862 * @param _sz the size of the modified region
864 METHOD void sync_icache {
873 * @brief Create temporary memory mapping for use by dumpsys().
875 * @param _md The memory chunk in which the mapping lies.
876 * @param _ofs The offset within the chunk of the mapping.
877 * @param _sz The requested size of the mapping.
879 * @retval vm_offset_t The virtual address of the mapping.
881 * The sz argument is modified to reflect the actual size of the
884 METHOD vm_offset_t dumpsys_map {
893 * @brief Remove temporary dumpsys() mapping.
895 * @param _md The memory chunk in which the mapping lies.
896 * @param _ofs The offset within the chunk of the mapping.
897 * @param _va The virtual address of the mapping.
899 METHOD void dumpsys_unmap {
908 * @brief Scan/iterate memory chunks.
910 * @param _prev The previously returned chunk or NULL.
912 * @retval The next (or first when _prev is NULL) chunk.
914 METHOD struct pmap_md * scan_md {
916 struct pmap_md *_prev;
917 } DEFAULT mmu_null_scan_md;