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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Dispatch MI pmap calls to the appropriate MMU implementation
33 * through a previously registered kernel object.
35 * Before pmap_bootstrap() can be called, a CPU module must have
36 * called pmap_mmu_install(). This may be called multiple times:
37 * the highest priority call will be installed as the default
38 * MMU handler when pmap_bootstrap() is called.
40 * It is required that mutex_init() be called before pmap_bootstrap(),
41 * as the PMAP layer makes extensive use of mutexes.
44 #include <sys/param.h>
45 #include <sys/kernel.h>
48 #include <sys/mutex.h>
49 #include <sys/systm.h>
52 #include <vm/vm_page.h>
54 #include <machine/mmuvar.h>
55 #include <machine/smp.h>
59 static mmu_def_t *mmu_def_impl;
61 static struct mmu_kobj mmu_kernel_obj;
62 static struct kobj_ops mmu_kernel_kops;
67 struct pmap kernel_pmap_store;
69 struct msgbuf *msgbufp;
70 vm_offset_t msgbuf_phys;
72 vm_offset_t kernel_vm_end;
73 vm_offset_t phys_avail[PHYS_AVAIL_SZ];
74 vm_offset_t virtual_avail;
75 vm_offset_t virtual_end;
77 int pmap_bootstrapped;
81 pvo_vaddr_compare(struct pvo_entry *a, struct pvo_entry *b)
83 if (PVO_VADDR(a) < PVO_VADDR(b))
85 else if (PVO_VADDR(a) > PVO_VADDR(b))
89 RB_GENERATE(pvo_tree, pvo_entry, pvo_plink, pvo_vaddr_compare);
94 pmap_advise(pmap_t pmap, vm_offset_t start, vm_offset_t end, int advice)
97 CTR5(KTR_PMAP, "%s(%p, %#x, %#x, %d)", __func__, pmap, start, end,
99 MMU_ADVISE(mmu_obj, pmap, start, end, advice);
103 pmap_change_wiring(pmap_t pmap, vm_offset_t va, boolean_t wired)
106 CTR4(KTR_PMAP, "%s(%p, %#x, %u)", __func__, pmap, va, wired);
107 MMU_CHANGE_WIRING(mmu_obj, pmap, va, wired);
111 pmap_clear_modify(vm_page_t m)
114 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
115 MMU_CLEAR_MODIFY(mmu_obj, m);
119 pmap_clear_reference(vm_page_t m)
122 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
123 MMU_CLEAR_REFERENCE(mmu_obj, m);
127 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr,
128 vm_size_t len, vm_offset_t src_addr)
131 CTR6(KTR_PMAP, "%s(%p, %p, %#x, %#x, %#x)", __func__, dst_pmap,
132 src_pmap, dst_addr, len, src_addr);
133 MMU_COPY(mmu_obj, dst_pmap, src_pmap, dst_addr, len, src_addr);
137 pmap_copy_page(vm_page_t src, vm_page_t dst)
140 CTR3(KTR_PMAP, "%s(%p, %p)", __func__, src, dst);
141 MMU_COPY_PAGE(mmu_obj, src, dst);
145 pmap_copy_pages(vm_page_t ma[], vm_offset_t a_offset, vm_page_t mb[],
146 vm_offset_t b_offset, int xfersize)
149 CTR6(KTR_PMAP, "%s(%p, %#x, %p, %#x, %#x)", __func__, ma,
150 a_offset, mb, b_offset, xfersize);
151 MMU_COPY_PAGES(mmu_obj, ma, a_offset, mb, b_offset, xfersize);
155 pmap_enter(pmap_t pmap, vm_offset_t va, vm_prot_t access, vm_page_t p,
156 vm_prot_t prot, boolean_t wired)
159 CTR6(KTR_PMAP, "pmap_enter(%p, %#x, %#x, %p, %#x, %u)", pmap, va,
160 access, p, prot, wired);
161 MMU_ENTER(mmu_obj, pmap, va, p, prot, wired);
165 pmap_enter_object(pmap_t pmap, vm_offset_t start, vm_offset_t end,
166 vm_page_t m_start, vm_prot_t prot)
169 CTR6(KTR_PMAP, "%s(%p, %#x, %#x, %p, %#x)", __func__, pmap, start,
171 MMU_ENTER_OBJECT(mmu_obj, pmap, start, end, m_start, prot);
175 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot)
178 CTR5(KTR_PMAP, "%s(%p, %#x, %p, %#x)", __func__, pmap, va, m, prot);
179 MMU_ENTER_QUICK(mmu_obj, pmap, va, m, prot);
183 pmap_extract(pmap_t pmap, vm_offset_t va)
186 CTR3(KTR_PMAP, "%s(%p, %#x)", __func__, pmap, va);
187 return (MMU_EXTRACT(mmu_obj, pmap, va));
191 pmap_extract_and_hold(pmap_t pmap, vm_offset_t va, vm_prot_t prot)
194 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, pmap, va, prot);
195 return (MMU_EXTRACT_AND_HOLD(mmu_obj, pmap, va, prot));
199 pmap_growkernel(vm_offset_t va)
202 CTR2(KTR_PMAP, "%s(%#x)", __func__, va);
203 MMU_GROWKERNEL(mmu_obj, va);
210 CTR1(KTR_PMAP, "%s()", __func__);
215 pmap_is_modified(vm_page_t m)
218 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
219 return (MMU_IS_MODIFIED(mmu_obj, m));
223 pmap_is_prefaultable(pmap_t pmap, vm_offset_t va)
226 CTR3(KTR_PMAP, "%s(%p, %#x)", __func__, pmap, va);
227 return (MMU_IS_PREFAULTABLE(mmu_obj, pmap, va));
231 pmap_is_referenced(vm_page_t m)
234 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
235 return (MMU_IS_REFERENCED(mmu_obj, m));
239 pmap_ts_referenced(vm_page_t m)
242 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
243 return (MMU_TS_REFERENCED(mmu_obj, m));
247 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
250 CTR5(KTR_PMAP, "%s(%p, %#x, %#x, %#x)", __func__, virt, start, end,
252 return (MMU_MAP(mmu_obj, virt, start, end, prot));
256 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr, vm_object_t object,
257 vm_pindex_t pindex, vm_size_t size)
260 CTR6(KTR_PMAP, "%s(%p, %#x, %p, %u, %#x)", __func__, pmap, addr,
261 object, pindex, size);
262 MMU_OBJECT_INIT_PT(mmu_obj, pmap, addr, object, pindex, size);
266 pmap_page_exists_quick(pmap_t pmap, vm_page_t m)
269 CTR3(KTR_PMAP, "%s(%p, %p)", __func__, pmap, m);
270 return (MMU_PAGE_EXISTS_QUICK(mmu_obj, pmap, m));
274 pmap_page_init(vm_page_t m)
277 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
278 MMU_PAGE_INIT(mmu_obj, m);
282 pmap_page_wired_mappings(vm_page_t m)
285 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
286 return (MMU_PAGE_WIRED_MAPPINGS(mmu_obj, m));
290 pmap_pinit(pmap_t pmap)
293 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap);
294 MMU_PINIT(mmu_obj, pmap);
299 pmap_pinit0(pmap_t pmap)
302 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap);
303 MMU_PINIT0(mmu_obj, pmap);
307 pmap_protect(pmap_t pmap, vm_offset_t start, vm_offset_t end, vm_prot_t prot)
310 CTR5(KTR_PMAP, "%s(%p, %#x, %#x, %#x)", __func__, pmap, start, end,
312 MMU_PROTECT(mmu_obj, pmap, start, end, prot);
316 pmap_qenter(vm_offset_t start, vm_page_t *m, int count)
319 CTR4(KTR_PMAP, "%s(%#x, %p, %d)", __func__, start, m, count);
320 MMU_QENTER(mmu_obj, start, m, count);
324 pmap_qremove(vm_offset_t start, int count)
327 CTR3(KTR_PMAP, "%s(%#x, %d)", __func__, start, count);
328 MMU_QREMOVE(mmu_obj, start, count);
332 pmap_release(pmap_t pmap)
335 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap);
336 MMU_RELEASE(mmu_obj, pmap);
340 pmap_remove(pmap_t pmap, vm_offset_t start, vm_offset_t end)
343 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, pmap, start, end);
344 MMU_REMOVE(mmu_obj, pmap, start, end);
348 pmap_remove_all(vm_page_t m)
351 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
352 MMU_REMOVE_ALL(mmu_obj, m);
356 pmap_remove_pages(pmap_t pmap)
359 CTR2(KTR_PMAP, "%s(%p)", __func__, pmap);
360 MMU_REMOVE_PAGES(mmu_obj, pmap);
364 pmap_remove_write(vm_page_t m)
367 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
368 MMU_REMOVE_WRITE(mmu_obj, m);
372 pmap_zero_page(vm_page_t m)
375 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
376 MMU_ZERO_PAGE(mmu_obj, m);
380 pmap_zero_page_area(vm_page_t m, int off, int size)
383 CTR4(KTR_PMAP, "%s(%p, %d, %d)", __func__, m, off, size);
384 MMU_ZERO_PAGE_AREA(mmu_obj, m, off, size);
388 pmap_zero_page_idle(vm_page_t m)
391 CTR2(KTR_PMAP, "%s(%p)", __func__, m);
392 MMU_ZERO_PAGE_IDLE(mmu_obj, m);
396 pmap_mincore(pmap_t pmap, vm_offset_t addr, vm_paddr_t *locked_pa)
399 CTR3(KTR_PMAP, "%s(%p, %#x)", __func__, pmap, addr);
400 return (MMU_MINCORE(mmu_obj, pmap, addr, locked_pa));
404 pmap_activate(struct thread *td)
407 CTR2(KTR_PMAP, "%s(%p)", __func__, td);
408 MMU_ACTIVATE(mmu_obj, td);
412 pmap_deactivate(struct thread *td)
415 CTR2(KTR_PMAP, "%s(%p)", __func__, td);
416 MMU_DEACTIVATE(mmu_obj, td);
420 * Increase the starting virtual address of the given mapping if a
421 * different alignment might result in more superpage mappings.
424 pmap_align_superpage(vm_object_t object, vm_ooffset_t offset,
425 vm_offset_t *addr, vm_size_t size)
428 CTR5(KTR_PMAP, "%s(%p, %#x, %p, %#x)", __func__, object, offset, addr,
430 MMU_ALIGN_SUPERPAGE(mmu_obj, object, offset, addr, size);
434 * Routines used in machine-dependent code
437 pmap_bootstrap(vm_offset_t start, vm_offset_t end)
439 mmu_obj = &mmu_kernel_obj;
442 * Take care of compiling the selected class, and
443 * then statically initialise the MMU object
445 kobj_class_compile_static(mmu_def_impl, &mmu_kernel_kops);
446 kobj_init_static((kobj_t)mmu_obj, mmu_def_impl);
448 MMU_BOOTSTRAP(mmu_obj, start, end);
452 pmap_cpu_bootstrap(int ap)
455 * No KTR here because our console probably doesn't work yet
458 return (MMU_CPU_BOOTSTRAP(mmu_obj, ap));
462 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
465 CTR3(KTR_PMAP, "%s(%#x, %#x)", __func__, pa, size);
466 return (MMU_MAPDEV(mmu_obj, pa, size));
470 pmap_mapdev_attr(vm_offset_t pa, vm_size_t size, vm_memattr_t attr)
473 CTR4(KTR_PMAP, "%s(%#x, %#x, %#x)", __func__, pa, size, attr);
474 return (MMU_MAPDEV_ATTR(mmu_obj, pa, size, attr));
478 pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma)
481 CTR3(KTR_PMAP, "%s(%p, %#x)", __func__, m, ma);
482 return (MMU_PAGE_SET_MEMATTR(mmu_obj, m, ma));
486 pmap_unmapdev(vm_offset_t va, vm_size_t size)
489 CTR3(KTR_PMAP, "%s(%#x, %#x)", __func__, va, size);
490 MMU_UNMAPDEV(mmu_obj, va, size);
494 pmap_kextract(vm_offset_t va)
497 CTR2(KTR_PMAP, "%s(%#x)", __func__, va);
498 return (MMU_KEXTRACT(mmu_obj, va));
502 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
505 CTR3(KTR_PMAP, "%s(%#x, %#x)", __func__, va, pa);
506 MMU_KENTER(mmu_obj, va, pa);
510 pmap_kenter_attr(vm_offset_t va, vm_offset_t pa, vm_memattr_t ma)
513 CTR4(KTR_PMAP, "%s(%#x, %#x, %#x)", __func__, va, pa, ma);
514 MMU_KENTER_ATTR(mmu_obj, va, pa, ma);
518 pmap_dev_direct_mapped(vm_paddr_t pa, vm_size_t size)
521 CTR3(KTR_PMAP, "%s(%#x, %#x)", __func__, pa, size);
522 return (MMU_DEV_DIRECT_MAPPED(mmu_obj, pa, size));
526 pmap_sync_icache(pmap_t pm, vm_offset_t va, vm_size_t sz)
529 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, pm, va, sz);
530 return (MMU_SYNC_ICACHE(mmu_obj, pm, va, sz));
534 pmap_dumpsys_map(struct pmap_md *md, vm_size_t ofs, vm_size_t *sz)
537 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, md, ofs, *sz);
538 return (MMU_DUMPSYS_MAP(mmu_obj, md, ofs, sz));
542 pmap_dumpsys_unmap(struct pmap_md *md, vm_size_t ofs, vm_offset_t va)
545 CTR4(KTR_PMAP, "%s(%p, %#x, %#x)", __func__, md, ofs, va);
546 return (MMU_DUMPSYS_UNMAP(mmu_obj, md, ofs, va));
550 pmap_scan_md(struct pmap_md *prev)
553 CTR2(KTR_PMAP, "%s(%p)", __func__, prev);
554 return (MMU_SCAN_MD(mmu_obj, prev));
558 * MMU install routines. Highest priority wins, equal priority also
559 * overrides allowing last-set to win.
561 SET_DECLARE(mmu_set, mmu_def_t);
564 pmap_mmu_install(char *name, int prio)
566 mmu_def_t **mmupp, *mmup;
567 static int curr_prio = 0;
570 * Try and locate the MMU kobj corresponding to the name
572 SET_FOREACH(mmupp, mmu_set) {
576 !strcmp(mmup->name, name) &&
577 (prio >= curr_prio || mmu_def_impl == NULL)) {
587 int unmapped_buf_allowed;