FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/pmap.c
1 /*
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
5 * All rights reserved.
6 * Copyright (c) 1994 David Greenman
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department and William Jolitz of UUNET Technologies Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * from: @(#)pmap.c 7.7 (Berkeley) 5/12/91
42 * $FreeBSD: releng/5.1/sys/i386/i386/pmap.c 114177 2003-04-28 20:35:36Z jake $
43 */
44 /*-
45 * Copyright (c) 2003 Networks Associates Technology, Inc.
46 * All rights reserved.
47 *
48 * This software was developed for the FreeBSD Project by Jake Burkholder,
49 * Safeport Network Services, and Network Associates Laboratories, the
50 * Security Research Division of Network Associates, Inc. under
51 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
52 * CHATS research program.
53 *
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 */
75
76 /*
77 * Manages physical address maps.
78 *
79 * In addition to hardware address maps, this
80 * module is called upon to provide software-use-only
81 * maps which may or may not be stored in the same
82 * form as hardware maps. These pseudo-maps are
83 * used to store intermediate results from copy
84 * operations to and from address spaces.
85 *
86 * Since the information managed by this module is
87 * also stored by the logical address mapping module,
88 * this module may throw away valid virtual-to-physical
89 * mappings at almost any time. However, invalidations
90 * of virtual-to-physical mappings must be done as
91 * requested.
92 *
93 * In order to cope with hardware architectures which
94 * make virtual-to-physical map invalidates expensive,
95 * this module may delay invalidate or reduced protection
96 * operations until such time as they are actually
97 * necessary. This module is given full information as
98 * to which processors are currently using which maps,
99 * and to when physical maps must be made correct.
100 */
101
102 #include "opt_pmap.h"
103 #include "opt_msgbuf.h"
104 #include "opt_kstack_pages.h"
105 #include "opt_swtch.h"
106
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/kernel.h>
110 #include <sys/lock.h>
111 #include <sys/mman.h>
112 #include <sys/msgbuf.h>
113 #include <sys/mutex.h>
114 #include <sys/proc.h>
115 #include <sys/sx.h>
116 #include <sys/user.h>
117 #include <sys/vmmeter.h>
118 #include <sys/sysctl.h>
119 #ifdef SMP
120 #include <sys/smp.h>
121 #endif
122
123 #include <vm/vm.h>
124 #include <vm/vm_param.h>
125 #include <vm/vm_kern.h>
126 #include <vm/vm_page.h>
127 #include <vm/vm_map.h>
128 #include <vm/vm_object.h>
129 #include <vm/vm_extern.h>
130 #include <vm/vm_pageout.h>
131 #include <vm/vm_pager.h>
132 #include <vm/uma.h>
133
134 #include <machine/cpu.h>
135 #include <machine/cputypes.h>
136 #include <machine/md_var.h>
137 #include <machine/specialreg.h>
138 #if defined(SMP) || defined(APIC_IO)
139 #include <machine/smp.h>
140 #include <machine/apic.h>
141 #include <machine/segments.h>
142 #include <machine/tss.h>
143 #endif /* SMP || APIC_IO */
144
145 #define PMAP_KEEP_PDIRS
146 #ifndef PMAP_SHPGPERPROC
147 #define PMAP_SHPGPERPROC 200
148 #endif
149
150 #if defined(DIAGNOSTIC)
151 #define PMAP_DIAGNOSTIC
152 #endif
153
154 #define MINPV 2048
155
156 #if !defined(PMAP_DIAGNOSTIC)
157 #define PMAP_INLINE __inline
158 #else
159 #define PMAP_INLINE
160 #endif
161
162 /*
163 * Get PDEs and PTEs for user/kernel address space
164 */
165 #define pmap_pde(m, v) (&((m)->pm_pdir[(vm_offset_t)(v) >> PDRSHIFT]))
166 #define pdir_pde(m, v) (m[(vm_offset_t)(v) >> PDRSHIFT])
167
168 #define pmap_pde_v(pte) ((*(int *)pte & PG_V) != 0)
169 #define pmap_pte_w(pte) ((*(int *)pte & PG_W) != 0)
170 #define pmap_pte_m(pte) ((*(int *)pte & PG_M) != 0)
171 #define pmap_pte_u(pte) ((*(int *)pte & PG_A) != 0)
172 #define pmap_pte_v(pte) ((*(int *)pte & PG_V) != 0)
173
174 #define pmap_pte_set_w(pte, v) ((v)?(*(int *)pte |= PG_W):(*(int *)pte &= ~PG_W))
175 #define pmap_pte_set_prot(pte, v) ((*(int *)pte &= ~PG_PROT), (*(int *)pte |= (v)))
176
177 /*
178 * Given a map and a machine independent protection code,
179 * convert to a vax protection code.
180 */
181 #define pte_prot(m, p) (protection_codes[p])
182 static int protection_codes[8];
183
184 struct pmap kernel_pmap_store;
185 LIST_HEAD(pmaplist, pmap);
186 static struct pmaplist allpmaps;
187 static struct mtx allpmaps_lock;
188 #if defined(SMP) && defined(LAZY_SWITCH)
189 static struct mtx lazypmap_lock;
190 #endif
191
192 vm_paddr_t avail_start; /* PA of first available physical page */
193 vm_paddr_t avail_end; /* PA of last available physical page */
194 vm_offset_t virtual_avail; /* VA of first avail page (after kernel bss) */
195 vm_offset_t virtual_end; /* VA of last avail page (end of kernel AS) */
196 static boolean_t pmap_initialized = FALSE; /* Has pmap_init completed? */
197 static int pgeflag; /* PG_G or-in */
198 static int pseflag; /* PG_PS or-in */
199
200 static int nkpt;
201 vm_offset_t kernel_vm_end;
202 extern u_int32_t KERNend;
203
204 #ifdef PAE
205 static uma_zone_t pdptzone;
206 #endif
207
208 /*
209 * Data for the pv entry allocation mechanism
210 */
211 static uma_zone_t pvzone;
212 static struct vm_object pvzone_obj;
213 static int pv_entry_count = 0, pv_entry_max = 0, pv_entry_high_water = 0;
214 int pmap_pagedaemon_waken;
215
216 /*
217 * All those kernel PT submaps that BSD is so fond of
218 */
219 pt_entry_t *CMAP1 = 0;
220 static pt_entry_t *CMAP2, *CMAP3, *ptmmap;
221 caddr_t CADDR1 = 0, ptvmmap = 0;
222 static caddr_t CADDR2, CADDR3;
223 static struct mtx CMAPCADDR12_lock;
224 static pt_entry_t *msgbufmap;
225 struct msgbuf *msgbufp = 0;
226
227 /*
228 * Crashdump maps.
229 */
230 static pt_entry_t *pt_crashdumpmap;
231 static caddr_t crashdumpmap;
232
233 #ifdef SMP
234 extern pt_entry_t *SMPpt;
235 #endif
236 static pt_entry_t *PMAP1 = 0;
237 static pt_entry_t *PADDR1 = 0;
238
239 static PMAP_INLINE void free_pv_entry(pv_entry_t pv);
240 static pv_entry_t get_pv_entry(void);
241 static void i386_protection_init(void);
242 static __inline void pmap_changebit(vm_page_t m, int bit, boolean_t setem);
243
244 static vm_page_t pmap_enter_quick(pmap_t pmap, vm_offset_t va,
245 vm_page_t m, vm_page_t mpte);
246 static int pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t sva);
247 static void pmap_remove_page(struct pmap *pmap, vm_offset_t va);
248 static int pmap_remove_entry(struct pmap *pmap, vm_page_t m,
249 vm_offset_t va);
250 static void pmap_insert_entry(pmap_t pmap, vm_offset_t va,
251 vm_page_t mpte, vm_page_t m);
252
253 static vm_page_t pmap_allocpte(pmap_t pmap, vm_offset_t va);
254
255 static vm_page_t _pmap_allocpte(pmap_t pmap, unsigned ptepindex);
256 static vm_page_t pmap_page_lookup(vm_object_t object, vm_pindex_t pindex);
257 static int pmap_unuse_pt(pmap_t, vm_offset_t, vm_page_t);
258 static vm_offset_t pmap_kmem_choose(vm_offset_t addr);
259 static void *pmap_pv_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
260 #ifdef PAE
261 static void *pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait);
262 #endif
263
264 static pd_entry_t pdir4mb;
265
266 CTASSERT(1 << PDESHIFT == sizeof(pd_entry_t));
267 CTASSERT(1 << PTESHIFT == sizeof(pt_entry_t));
268
269 /*
270 * Move the kernel virtual free pointer to the next
271 * 4MB. This is used to help improve performance
272 * by using a large (4MB) page for much of the kernel
273 * (.text, .data, .bss)
274 */
275 static vm_offset_t
276 pmap_kmem_choose(vm_offset_t addr)
277 {
278 vm_offset_t newaddr = addr;
279
280 #ifdef I686_CPU_not /* Problem seems to have gone away */
281 /* Deal with un-resolved Pentium4 issues */
282 if (cpu_class == CPUCLASS_686 &&
283 strcmp(cpu_vendor, "GenuineIntel") == 0 &&
284 (cpu_id & 0xf00) == 0xf00)
285 return newaddr;
286 #endif
287 #ifndef DISABLE_PSE
288 if (cpu_feature & CPUID_PSE)
289 newaddr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
290 #endif
291 return newaddr;
292 }
293
294 /*
295 * Bootstrap the system enough to run with virtual memory.
296 *
297 * On the i386 this is called after mapping has already been enabled
298 * and just syncs the pmap module with what has already been done.
299 * [We can't call it easily with mapping off since the kernel is not
300 * mapped with PA == VA, hence we would have to relocate every address
301 * from the linked base (virtual) address "KERNBASE" to the actual
302 * (physical) address starting relative to 0]
303 */
304 void
305 pmap_bootstrap(firstaddr, loadaddr)
306 vm_paddr_t firstaddr;
307 vm_paddr_t loadaddr;
308 {
309 vm_offset_t va;
310 pt_entry_t *pte;
311 int i;
312
313 avail_start = firstaddr;
314
315 /*
316 * XXX The calculation of virtual_avail is wrong. It's NKPT*PAGE_SIZE too
317 * large. It should instead be correctly calculated in locore.s and
318 * not based on 'first' (which is a physical address, not a virtual
319 * address, for the start of unused physical memory). The kernel
320 * page tables are NOT double mapped and thus should not be included
321 * in this calculation.
322 */
323 virtual_avail = (vm_offset_t) KERNBASE + firstaddr;
324 virtual_avail = pmap_kmem_choose(virtual_avail);
325
326 virtual_end = VM_MAX_KERNEL_ADDRESS;
327
328 /*
329 * Initialize protection array.
330 */
331 i386_protection_init();
332
333 /*
334 * Initialize the kernel pmap (which is statically allocated).
335 */
336 kernel_pmap->pm_pdir = (pd_entry_t *) (KERNBASE + (u_int)IdlePTD);
337 #ifdef PAE
338 kernel_pmap->pm_pdpt = (pdpt_entry_t *) (KERNBASE + (u_int)IdlePDPT);
339 #endif
340 kernel_pmap->pm_active = -1; /* don't allow deactivation */
341 TAILQ_INIT(&kernel_pmap->pm_pvlist);
342 LIST_INIT(&allpmaps);
343 #if defined(SMP) && defined(LAZY_SWITCH)
344 mtx_init(&lazypmap_lock, "lazypmap", NULL, MTX_SPIN);
345 #endif
346 mtx_init(&allpmaps_lock, "allpmaps", NULL, MTX_SPIN);
347 mtx_lock_spin(&allpmaps_lock);
348 LIST_INSERT_HEAD(&allpmaps, kernel_pmap, pm_list);
349 mtx_unlock_spin(&allpmaps_lock);
350 nkpt = NKPT;
351
352 /*
353 * Reserve some special page table entries/VA space for temporary
354 * mapping of pages.
355 */
356 #define SYSMAP(c, p, v, n) \
357 v = (c)va; va += ((n)*PAGE_SIZE); p = pte; pte += (n);
358
359 va = virtual_avail;
360 pte = vtopte(va);
361
362 /*
363 * CMAP1/CMAP2 are used for zeroing and copying pages.
364 * CMAP3 is used for the idle process page zeroing.
365 */
366 SYSMAP(caddr_t, CMAP1, CADDR1, 1)
367 SYSMAP(caddr_t, CMAP2, CADDR2, 1)
368 SYSMAP(caddr_t, CMAP3, CADDR3, 1)
369
370 mtx_init(&CMAPCADDR12_lock, "CMAPCADDR12", NULL, MTX_DEF);
371
372 /*
373 * Crashdump maps.
374 */
375 SYSMAP(caddr_t, pt_crashdumpmap, crashdumpmap, MAXDUMPPGS);
376
377 /*
378 * ptvmmap is used for reading arbitrary physical pages via /dev/mem.
379 * XXX ptmmap is not used.
380 */
381 SYSMAP(caddr_t, ptmmap, ptvmmap, 1)
382
383 /*
384 * msgbufp is used to map the system message buffer.
385 * XXX msgbufmap is not used.
386 */
387 SYSMAP(struct msgbuf *, msgbufmap, msgbufp,
388 atop(round_page(MSGBUF_SIZE)))
389
390 /*
391 * ptemap is used for pmap_pte_quick
392 */
393 SYSMAP(pt_entry_t *, PMAP1, PADDR1, 1);
394
395 virtual_avail = va;
396
397 *CMAP1 = *CMAP2 = 0;
398 for (i = 0; i < NKPT; i++)
399 PTD[i] = 0;
400
401 pgeflag = 0;
402 #ifndef DISABLE_PG_G
403 if (cpu_feature & CPUID_PGE)
404 pgeflag = PG_G;
405 #endif
406 #ifdef I686_CPU_not /* Problem seems to have gone away */
407 /* Deal with un-resolved Pentium4 issues */
408 if (cpu_class == CPUCLASS_686 &&
409 strcmp(cpu_vendor, "GenuineIntel") == 0 &&
410 (cpu_id & 0xf00) == 0xf00) {
411 printf("Warning: Pentium 4 cpu: PG_G disabled (global flag)\n");
412 pgeflag = 0;
413 }
414 #endif
415
416 /*
417 * Initialize the 4MB page size flag
418 */
419 pseflag = 0;
420 /*
421 * The 4MB page version of the initial
422 * kernel page mapping.
423 */
424 pdir4mb = 0;
425
426 #ifndef DISABLE_PSE
427 if (cpu_feature & CPUID_PSE)
428 pseflag = PG_PS;
429 #endif
430 #ifdef I686_CPU_not /* Problem seems to have gone away */
431 /* Deal with un-resolved Pentium4 issues */
432 if (cpu_class == CPUCLASS_686 &&
433 strcmp(cpu_vendor, "GenuineIntel") == 0 &&
434 (cpu_id & 0xf00) == 0xf00) {
435 printf("Warning: Pentium 4 cpu: PG_PS disabled (4MB pages)\n");
436 pseflag = 0;
437 }
438 #endif
439 #ifndef DISABLE_PSE
440 if (pseflag) {
441 pd_entry_t ptditmp;
442 /*
443 * Note that we have enabled PSE mode
444 */
445 ptditmp = *(PTmap + i386_btop(KERNBASE));
446 ptditmp &= ~(NBPDR - 1);
447 ptditmp |= PG_V | PG_RW | PG_PS | PG_U | pgeflag;
448 pdir4mb = ptditmp;
449 }
450 #endif
451 #ifndef SMP
452 /*
453 * Turn on PGE/PSE. SMP does this later on since the
454 * 4K page tables are required for AP boot (for now).
455 * XXX fixme.
456 */
457 pmap_set_opt();
458 #endif
459 #ifdef SMP
460 if (cpu_apic_address == 0)
461 panic("pmap_bootstrap: no local apic! (non-SMP hardware?)");
462
463 /* local apic is mapped on last page */
464 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N | pgeflag |
465 (cpu_apic_address & PG_FRAME));
466 #endif
467 invltlb();
468 }
469
470 /*
471 * Enable 4MB page mode for MP startup. Turn on PG_G support.
472 * BSP will run this after all the AP's have started up.
473 */
474 void
475 pmap_set_opt(void)
476 {
477 pt_entry_t *pte;
478 vm_offset_t va, endva;
479
480 if (pgeflag && (cpu_feature & CPUID_PGE)) {
481 load_cr4(rcr4() | CR4_PGE);
482 invltlb(); /* Insurance */
483 }
484 #ifndef DISABLE_PSE
485 if (pseflag && (cpu_feature & CPUID_PSE)) {
486 load_cr4(rcr4() | CR4_PSE);
487 invltlb(); /* Insurance */
488 }
489 #endif
490 if (PCPU_GET(cpuid) == 0) {
491 #ifndef DISABLE_PSE
492 if (pdir4mb) {
493 kernel_pmap->pm_pdir[KPTDI] = PTD[KPTDI] = pdir4mb;
494 invltlb(); /* Insurance */
495 }
496 #endif
497 if (pgeflag) {
498 /* Turn on PG_G for text, data, bss pages. */
499 va = (vm_offset_t)btext;
500 #ifndef DISABLE_PSE
501 if (pseflag && (cpu_feature & CPUID_PSE)) {
502 if (va < KERNBASE + (1 << PDRSHIFT))
503 va = KERNBASE + (1 << PDRSHIFT);
504 }
505 #endif
506 endva = KERNBASE + KERNend;
507 while (va < endva) {
508 pte = vtopte(va);
509 if (*pte)
510 *pte |= pgeflag;
511 va += PAGE_SIZE;
512 }
513 invltlb(); /* Insurance */
514 }
515 /*
516 * We do not need to broadcast the invltlb here, because
517 * each AP does it the moment it is released from the boot
518 * lock. See ap_init().
519 */
520 }
521 }
522
523 static void *
524 pmap_pv_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
525 {
526 *flags = UMA_SLAB_PRIV;
527 return (void *)kmem_alloc(kernel_map, bytes);
528 }
529
530 #ifdef PAE
531 static void *
532 pmap_pdpt_allocf(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
533 {
534 *flags = UMA_SLAB_PRIV;
535 return (contigmalloc(PAGE_SIZE, NULL, 0, 0x0ULL, 0xffffffffULL, 1, 0));
536 }
537 #endif
538
539 /*
540 * Initialize the pmap module.
541 * Called by vm_init, to initialize any structures that the pmap
542 * system needs to map virtual memory.
543 * pmap_init has been enhanced to support in a fairly consistant
544 * way, discontiguous physical memory.
545 */
546 void
547 pmap_init(phys_start, phys_end)
548 vm_paddr_t phys_start, phys_end;
549 {
550 int i;
551 int initial_pvs;
552
553 /*
554 * Allocate memory for random pmap data structures. Includes the
555 * pv_head_table.
556 */
557
558 for(i = 0; i < vm_page_array_size; i++) {
559 vm_page_t m;
560
561 m = &vm_page_array[i];
562 TAILQ_INIT(&m->md.pv_list);
563 m->md.pv_list_count = 0;
564 }
565
566 /*
567 * init the pv free list
568 */
569 initial_pvs = vm_page_array_size;
570 if (initial_pvs < MINPV)
571 initial_pvs = MINPV;
572 pvzone = uma_zcreate("PV ENTRY", sizeof (struct pv_entry), NULL, NULL,
573 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_VM);
574 uma_zone_set_allocf(pvzone, pmap_pv_allocf);
575 uma_prealloc(pvzone, initial_pvs);
576
577 #ifdef PAE
578 pdptzone = uma_zcreate("PDPT", NPGPTD * sizeof(pdpt_entry_t), NULL,
579 NULL, NULL, NULL, (NPGPTD * sizeof(pdpt_entry_t)) - 1, 0);
580 uma_zone_set_allocf(pdptzone, pmap_pdpt_allocf);
581 #endif
582
583 /*
584 * Now it is safe to enable pv_table recording.
585 */
586 pmap_initialized = TRUE;
587 }
588
589 /*
590 * Initialize the address space (zone) for the pv_entries. Set a
591 * high water mark so that the system can recover from excessive
592 * numbers of pv entries.
593 */
594 void
595 pmap_init2()
596 {
597 int shpgperproc = PMAP_SHPGPERPROC;
598
599 TUNABLE_INT_FETCH("vm.pmap.shpgperproc", &shpgperproc);
600 pv_entry_max = shpgperproc * maxproc + vm_page_array_size;
601 TUNABLE_INT_FETCH("vm.pmap.pv_entries", &pv_entry_max);
602 pv_entry_high_water = 9 * (pv_entry_max / 10);
603 uma_zone_set_obj(pvzone, &pvzone_obj, pv_entry_max);
604 }
605
606
607 /***************************************************
608 * Low level helper routines.....
609 ***************************************************/
610
611 #if defined(PMAP_DIAGNOSTIC)
612
613 /*
614 * This code checks for non-writeable/modified pages.
615 * This should be an invalid condition.
616 */
617 static int
618 pmap_nw_modified(pt_entry_t ptea)
619 {
620 int pte;
621
622 pte = (int) ptea;
623
624 if ((pte & (PG_M|PG_RW)) == PG_M)
625 return 1;
626 else
627 return 0;
628 }
629 #endif
630
631
632 /*
633 * this routine defines the region(s) of memory that should
634 * not be tested for the modified bit.
635 */
636 static PMAP_INLINE int
637 pmap_track_modified(vm_offset_t va)
638 {
639 if ((va < kmi.clean_sva) || (va >= kmi.clean_eva))
640 return 1;
641 else
642 return 0;
643 }
644
645 #ifdef I386_CPU
646 /*
647 * i386 only has "invalidate everything" and no SMP to worry about.
648 */
649 PMAP_INLINE void
650 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
651 {
652
653 if (pmap == kernel_pmap || pmap->pm_active)
654 invltlb();
655 }
656
657 PMAP_INLINE void
658 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
659 {
660
661 if (pmap == kernel_pmap || pmap->pm_active)
662 invltlb();
663 }
664
665 PMAP_INLINE void
666 pmap_invalidate_all(pmap_t pmap)
667 {
668
669 if (pmap == kernel_pmap || pmap->pm_active)
670 invltlb();
671 }
672 #else /* !I386_CPU */
673 #ifdef SMP
674 /*
675 * For SMP, these functions have to use the IPI mechanism for coherence.
676 */
677 void
678 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
679 {
680 u_int cpumask;
681 u_int other_cpus;
682
683 critical_enter();
684 /*
685 * We need to disable interrupt preemption but MUST NOT have
686 * interrupts disabled here.
687 * XXX we may need to hold schedlock to get a coherent pm_active
688 */
689 if (pmap->pm_active == -1 || pmap->pm_active == all_cpus) {
690 invlpg(va);
691 smp_invlpg(va);
692 } else {
693 cpumask = PCPU_GET(cpumask);
694 other_cpus = PCPU_GET(other_cpus);
695 if (pmap->pm_active & cpumask)
696 invlpg(va);
697 if (pmap->pm_active & other_cpus)
698 smp_masked_invlpg(pmap->pm_active & other_cpus, va);
699 }
700 critical_exit();
701 }
702
703 void
704 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
705 {
706 u_int cpumask;
707 u_int other_cpus;
708 vm_offset_t addr;
709
710 critical_enter();
711 /*
712 * We need to disable interrupt preemption but MUST NOT have
713 * interrupts disabled here.
714 * XXX we may need to hold schedlock to get a coherent pm_active
715 */
716 if (pmap->pm_active == -1 || pmap->pm_active == all_cpus) {
717 for (addr = sva; addr < eva; addr += PAGE_SIZE)
718 invlpg(addr);
719 smp_invlpg_range(sva, eva);
720 } else {
721 cpumask = PCPU_GET(cpumask);
722 other_cpus = PCPU_GET(other_cpus);
723 if (pmap->pm_active & cpumask)
724 for (addr = sva; addr < eva; addr += PAGE_SIZE)
725 invlpg(addr);
726 if (pmap->pm_active & other_cpus)
727 smp_masked_invlpg_range(pmap->pm_active & other_cpus,
728 sva, eva);
729 }
730 critical_exit();
731 }
732
733 void
734 pmap_invalidate_all(pmap_t pmap)
735 {
736 u_int cpumask;
737 u_int other_cpus;
738
739 #ifdef SWTCH_OPTIM_STATS
740 tlb_flush_count++;
741 #endif
742 critical_enter();
743 /*
744 * We need to disable interrupt preemption but MUST NOT have
745 * interrupts disabled here.
746 * XXX we may need to hold schedlock to get a coherent pm_active
747 */
748 if (pmap->pm_active == -1 || pmap->pm_active == all_cpus) {
749 invltlb();
750 smp_invltlb();
751 } else {
752 cpumask = PCPU_GET(cpumask);
753 other_cpus = PCPU_GET(other_cpus);
754 if (pmap->pm_active & cpumask)
755 invltlb();
756 if (pmap->pm_active & other_cpus)
757 smp_masked_invltlb(pmap->pm_active & other_cpus);
758 }
759 critical_exit();
760 }
761 #else /* !SMP */
762 /*
763 * Normal, non-SMP, 486+ invalidation functions.
764 * We inline these within pmap.c for speed.
765 */
766 PMAP_INLINE void
767 pmap_invalidate_page(pmap_t pmap, vm_offset_t va)
768 {
769
770 if (pmap == kernel_pmap || pmap->pm_active)
771 invlpg(va);
772 }
773
774 PMAP_INLINE void
775 pmap_invalidate_range(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
776 {
777 vm_offset_t addr;
778
779 if (pmap == kernel_pmap || pmap->pm_active)
780 for (addr = sva; addr < eva; addr += PAGE_SIZE)
781 invlpg(addr);
782 }
783
784 PMAP_INLINE void
785 pmap_invalidate_all(pmap_t pmap)
786 {
787
788 if (pmap == kernel_pmap || pmap->pm_active)
789 invltlb();
790 }
791 #endif /* !SMP */
792 #endif /* !I386_CPU */
793
794 /*
795 * Are we current address space or kernel?
796 */
797 static __inline int
798 pmap_is_current(pmap_t pmap)
799 {
800 return (pmap == kernel_pmap ||
801 (pmap->pm_pdir[PTDPTDI] & PG_FRAME) == (PTDpde[0] & PG_FRAME));
802 }
803
804 /*
805 * Super fast pmap_pte routine best used when scanning
806 * the pv lists. This eliminates many coarse-grained
807 * invltlb calls. Note that many of the pv list
808 * scans are across different pmaps. It is very wasteful
809 * to do an entire invltlb for checking a single mapping.
810 */
811 pt_entry_t *
812 pmap_pte_quick(pmap, va)
813 register pmap_t pmap;
814 vm_offset_t va;
815 {
816 pd_entry_t newpf;
817 pd_entry_t *pde;
818
819 pde = pmap_pde(pmap, va);
820 if (*pde & PG_PS)
821 return (pde);
822 if (*pde != 0) {
823 /* are we current address space or kernel? */
824 if (pmap_is_current(pmap))
825 return vtopte(va);
826 newpf = *pde & PG_FRAME;
827 if (((*PMAP1) & PG_FRAME) != newpf) {
828 *PMAP1 = newpf | PG_RW | PG_V;
829 pmap_invalidate_page(kernel_pmap, (vm_offset_t)PADDR1);
830 }
831 return PADDR1 + (i386_btop(va) & (NPTEPG - 1));
832 }
833 return (0);
834 }
835
836 /*
837 * Routine: pmap_extract
838 * Function:
839 * Extract the physical page address associated
840 * with the given map/virtual_address pair.
841 */
842 vm_paddr_t
843 pmap_extract(pmap, va)
844 register pmap_t pmap;
845 vm_offset_t va;
846 {
847 vm_paddr_t rtval;
848 pt_entry_t *pte;
849 pd_entry_t pde;
850
851 if (pmap == 0)
852 return 0;
853 pde = pmap->pm_pdir[va >> PDRSHIFT];
854 if (pde != 0) {
855 if ((pde & PG_PS) != 0) {
856 rtval = (pde & ~PDRMASK) | (va & PDRMASK);
857 return rtval;
858 }
859 pte = pmap_pte_quick(pmap, va);
860 rtval = ((*pte & PG_FRAME) | (va & PAGE_MASK));
861 return rtval;
862 }
863 return 0;
864
865 }
866
867 /***************************************************
868 * Low level mapping routines.....
869 ***************************************************/
870
871 /*
872 * Add a wired page to the kva.
873 * Note: not SMP coherent.
874 */
875 PMAP_INLINE void
876 pmap_kenter(vm_offset_t va, vm_paddr_t pa)
877 {
878 pt_entry_t *pte;
879
880 pte = vtopte(va);
881 pte_store(pte, pa | PG_RW | PG_V | pgeflag);
882 }
883
884 /*
885 * Remove a page from the kernel pagetables.
886 * Note: not SMP coherent.
887 */
888 PMAP_INLINE void
889 pmap_kremove(vm_offset_t va)
890 {
891 pt_entry_t *pte;
892
893 pte = vtopte(va);
894 pte_clear(pte);
895 }
896
897 /*
898 * Used to map a range of physical addresses into kernel
899 * virtual address space.
900 *
901 * The value passed in '*virt' is a suggested virtual address for
902 * the mapping. Architectures which can support a direct-mapped
903 * physical to virtual region can return the appropriate address
904 * within that region, leaving '*virt' unchanged. Other
905 * architectures should map the pages starting at '*virt' and
906 * update '*virt' with the first usable address after the mapped
907 * region.
908 */
909 vm_offset_t
910 pmap_map(vm_offset_t *virt, vm_paddr_t start, vm_paddr_t end, int prot)
911 {
912 vm_offset_t va, sva;
913
914 va = sva = *virt;
915 while (start < end) {
916 pmap_kenter(va, start);
917 va += PAGE_SIZE;
918 start += PAGE_SIZE;
919 }
920 pmap_invalidate_range(kernel_pmap, sva, va);
921 *virt = va;
922 return (sva);
923 }
924
925
926 /*
927 * Add a list of wired pages to the kva
928 * this routine is only used for temporary
929 * kernel mappings that do not need to have
930 * page modification or references recorded.
931 * Note that old mappings are simply written
932 * over. The page *must* be wired.
933 * Note: SMP coherent. Uses a ranged shootdown IPI.
934 */
935 void
936 pmap_qenter(vm_offset_t sva, vm_page_t *m, int count)
937 {
938 vm_offset_t va;
939
940 va = sva;
941 while (count-- > 0) {
942 pmap_kenter(va, VM_PAGE_TO_PHYS(*m));
943 va += PAGE_SIZE;
944 m++;
945 }
946 pmap_invalidate_range(kernel_pmap, sva, va);
947 }
948
949 /*
950 * This routine tears out page mappings from the
951 * kernel -- it is meant only for temporary mappings.
952 * Note: SMP coherent. Uses a ranged shootdown IPI.
953 */
954 void
955 pmap_qremove(vm_offset_t sva, int count)
956 {
957 vm_offset_t va;
958
959 va = sva;
960 while (count-- > 0) {
961 pmap_kremove(va);
962 va += PAGE_SIZE;
963 }
964 pmap_invalidate_range(kernel_pmap, sva, va);
965 }
966
967 static vm_page_t
968 pmap_page_lookup(vm_object_t object, vm_pindex_t pindex)
969 {
970 vm_page_t m;
971
972 retry:
973 m = vm_page_lookup(object, pindex);
974 if (m != NULL) {
975 vm_page_lock_queues();
976 if (vm_page_sleep_if_busy(m, FALSE, "pplookp"))
977 goto retry;
978 vm_page_unlock_queues();
979 }
980 return m;
981 }
982
983 #ifndef KSTACK_MAX_PAGES
984 #define KSTACK_MAX_PAGES 32
985 #endif
986
987 /*
988 * Create the kernel stack (including pcb for i386) for a new thread.
989 * This routine directly affects the fork perf for a process and
990 * create performance for a thread.
991 */
992 void
993 pmap_new_thread(struct thread *td, int pages)
994 {
995 int i;
996 vm_page_t ma[KSTACK_MAX_PAGES];
997 vm_object_t ksobj;
998 vm_page_t m;
999 vm_offset_t ks;
1000
1001 /* Bounds check */
1002 if (pages <= 1)
1003 pages = KSTACK_PAGES;
1004 else if (pages > KSTACK_MAX_PAGES)
1005 pages = KSTACK_MAX_PAGES;
1006
1007 /*
1008 * allocate object for the kstack
1009 */
1010 ksobj = vm_object_allocate(OBJT_DEFAULT, pages);
1011 td->td_kstack_obj = ksobj;
1012
1013 /* get a kernel virtual address for the kstack for this thread */
1014 #ifdef KSTACK_GUARD
1015 ks = kmem_alloc_nofault(kernel_map, (pages + 1) * PAGE_SIZE);
1016 if (ks == 0)
1017 panic("pmap_new_thread: kstack allocation failed");
1018 if (*vtopte(ks) != 0)
1019 pmap_qremove(ks, 1);
1020 ks += PAGE_SIZE;
1021 td->td_kstack = ks;
1022 #else
1023 /* get a kernel virtual address for the kstack for this thread */
1024 ks = kmem_alloc_nofault(kernel_map, pages * PAGE_SIZE);
1025 if (ks == 0)
1026 panic("pmap_new_thread: kstack allocation failed");
1027 td->td_kstack = ks;
1028 #endif
1029 /*
1030 * Knowing the number of pages allocated is useful when you
1031 * want to deallocate them.
1032 */
1033 td->td_kstack_pages = pages;
1034
1035 /*
1036 * For the length of the stack, link in a real page of ram for each
1037 * page of stack.
1038 */
1039 for (i = 0; i < pages; i++) {
1040 /*
1041 * Get a kernel stack page
1042 */
1043 m = vm_page_grab(ksobj, i,
1044 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED);
1045 ma[i] = m;
1046
1047 vm_page_lock_queues();
1048 vm_page_wakeup(m);
1049 vm_page_flag_clear(m, PG_ZERO);
1050 m->valid = VM_PAGE_BITS_ALL;
1051 vm_page_unlock_queues();
1052 }
1053 pmap_qenter(ks, ma, pages);
1054 }
1055
1056 /*
1057 * Dispose the kernel stack for a thread that has exited.
1058 * This routine directly impacts the exit perf of a process and thread.
1059 */
1060 void
1061 pmap_dispose_thread(td)
1062 struct thread *td;
1063 {
1064 int i;
1065 int pages;
1066 vm_object_t ksobj;
1067 vm_offset_t ks;
1068 vm_page_t m;
1069
1070 pages = td->td_kstack_pages;
1071 ksobj = td->td_kstack_obj;
1072 ks = td->td_kstack;
1073 pmap_qremove(ks, pages);
1074 for (i = 0; i < pages; i++) {
1075 m = vm_page_lookup(ksobj, i);
1076 if (m == NULL)
1077 panic("pmap_dispose_thread: kstack already missing?");
1078 vm_page_lock_queues();
1079 vm_page_busy(m);
1080 vm_page_unwire(m, 0);
1081 vm_page_free(m);
1082 vm_page_unlock_queues();
1083 }
1084 /*
1085 * Free the space that this stack was mapped to in the kernel
1086 * address map.
1087 */
1088 #ifdef KSTACK_GUARD
1089 kmem_free(kernel_map, ks - PAGE_SIZE, (pages + 1) * PAGE_SIZE);
1090 #else
1091 kmem_free(kernel_map, ks, pages * PAGE_SIZE);
1092 #endif
1093 vm_object_deallocate(ksobj);
1094 }
1095
1096 /*
1097 * Set up a variable sized alternate kstack. Though it may look MI, it may
1098 * need to be different on certain arches like ia64.
1099 */
1100 void
1101 pmap_new_altkstack(struct thread *td, int pages)
1102 {
1103 /* shuffle the original stack */
1104 td->td_altkstack_obj = td->td_kstack_obj;
1105 td->td_altkstack = td->td_kstack;
1106 td->td_altkstack_pages = td->td_kstack_pages;
1107
1108 pmap_new_thread(td, pages);
1109 }
1110
1111 void
1112 pmap_dispose_altkstack(td)
1113 struct thread *td;
1114 {
1115 pmap_dispose_thread(td);
1116
1117 /* restore the original kstack */
1118 td->td_kstack = td->td_altkstack;
1119 td->td_kstack_obj = td->td_altkstack_obj;
1120 td->td_kstack_pages = td->td_altkstack_pages;
1121 td->td_altkstack = 0;
1122 td->td_altkstack_obj = NULL;
1123 td->td_altkstack_pages = 0;
1124 }
1125
1126 /*
1127 * Allow the Kernel stack for a thread to be prejudicially paged out.
1128 */
1129 void
1130 pmap_swapout_thread(td)
1131 struct thread *td;
1132 {
1133 int i;
1134 int pages;
1135 vm_object_t ksobj;
1136 vm_offset_t ks;
1137 vm_page_t m;
1138
1139 pages = td->td_kstack_pages;
1140 ksobj = td->td_kstack_obj;
1141 ks = td->td_kstack;
1142 pmap_qremove(ks, pages);
1143 for (i = 0; i < pages; i++) {
1144 m = vm_page_lookup(ksobj, i);
1145 if (m == NULL)
1146 panic("pmap_swapout_thread: kstack already missing?");
1147 vm_page_lock_queues();
1148 vm_page_dirty(m);
1149 vm_page_unwire(m, 0);
1150 vm_page_unlock_queues();
1151 }
1152 }
1153
1154 /*
1155 * Bring the kernel stack for a specified thread back in.
1156 */
1157 void
1158 pmap_swapin_thread(td)
1159 struct thread *td;
1160 {
1161 int i, rv;
1162 int pages;
1163 vm_page_t ma[KSTACK_MAX_PAGES];
1164 vm_object_t ksobj;
1165 vm_offset_t ks;
1166 vm_page_t m;
1167
1168 pages = td->td_kstack_pages;
1169 ksobj = td->td_kstack_obj;
1170 ks = td->td_kstack;
1171 for (i = 0; i < pages; i++) {
1172 m = vm_page_grab(ksobj, i, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
1173 if (m->valid != VM_PAGE_BITS_ALL) {
1174 rv = vm_pager_get_pages(ksobj, &m, 1, 0);
1175 if (rv != VM_PAGER_OK)
1176 panic("pmap_swapin_thread: cannot get kstack for proc: %d\n", td->td_proc->p_pid);
1177 m = vm_page_lookup(ksobj, i);
1178 m->valid = VM_PAGE_BITS_ALL;
1179 }
1180 ma[i] = m;
1181 vm_page_lock_queues();
1182 vm_page_wire(m);
1183 vm_page_wakeup(m);
1184 vm_page_unlock_queues();
1185 }
1186 pmap_qenter(ks, ma, pages);
1187 }
1188
1189 /***************************************************
1190 * Page table page management routines.....
1191 ***************************************************/
1192
1193 /*
1194 * This routine unholds page table pages, and if the hold count
1195 * drops to zero, then it decrements the wire count.
1196 */
1197 static int
1198 _pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1199 {
1200
1201 while (vm_page_sleep_if_busy(m, FALSE, "pmuwpt"))
1202 vm_page_lock_queues();
1203
1204 if (m->hold_count == 0) {
1205 vm_offset_t pteva;
1206 /*
1207 * unmap the page table page
1208 */
1209 pmap->pm_pdir[m->pindex] = 0;
1210 --pmap->pm_stats.resident_count;
1211 if (pmap_is_current(pmap)) {
1212 /*
1213 * Do an invltlb to make the invalidated mapping
1214 * take effect immediately.
1215 */
1216 pteva = VM_MAXUSER_ADDRESS + i386_ptob(m->pindex);
1217 pmap_invalidate_page(pmap, pteva);
1218 }
1219
1220 /*
1221 * If the page is finally unwired, simply free it.
1222 */
1223 --m->wire_count;
1224 if (m->wire_count == 0) {
1225 vm_page_busy(m);
1226 vm_page_free_zero(m);
1227 atomic_subtract_int(&cnt.v_wire_count, 1);
1228 }
1229 return 1;
1230 }
1231 return 0;
1232 }
1233
1234 static PMAP_INLINE int
1235 pmap_unwire_pte_hold(pmap_t pmap, vm_page_t m)
1236 {
1237 vm_page_unhold(m);
1238 if (m->hold_count == 0)
1239 return _pmap_unwire_pte_hold(pmap, m);
1240 else
1241 return 0;
1242 }
1243
1244 /*
1245 * After removing a page table entry, this routine is used to
1246 * conditionally free the page, and manage the hold/wire counts.
1247 */
1248 static int
1249 pmap_unuse_pt(pmap_t pmap, vm_offset_t va, vm_page_t mpte)
1250 {
1251 unsigned ptepindex;
1252 if (va >= VM_MAXUSER_ADDRESS)
1253 return 0;
1254
1255 if (mpte == NULL) {
1256 ptepindex = (va >> PDRSHIFT);
1257 if (pmap->pm_pteobj->root &&
1258 (pmap->pm_pteobj->root->pindex == ptepindex)) {
1259 mpte = pmap->pm_pteobj->root;
1260 } else {
1261 while ((mpte = vm_page_lookup(pmap->pm_pteobj, ptepindex)) != NULL &&
1262 vm_page_sleep_if_busy(mpte, FALSE, "pulook"))
1263 vm_page_lock_queues();
1264 }
1265 }
1266
1267 return pmap_unwire_pte_hold(pmap, mpte);
1268 }
1269
1270 void
1271 pmap_pinit0(pmap)
1272 struct pmap *pmap;
1273 {
1274
1275 pmap->pm_pdir = (pd_entry_t *)(KERNBASE + (vm_offset_t)IdlePTD);
1276 #ifdef PAE
1277 pmap->pm_pdpt = (pdpt_entry_t *)(KERNBASE + (vm_offset_t)IdlePDPT);
1278 #endif
1279 pmap->pm_active = 0;
1280 TAILQ_INIT(&pmap->pm_pvlist);
1281 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1282 mtx_lock_spin(&allpmaps_lock);
1283 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1284 mtx_unlock_spin(&allpmaps_lock);
1285 }
1286
1287 /*
1288 * Initialize a preallocated and zeroed pmap structure,
1289 * such as one in a vmspace structure.
1290 */
1291 void
1292 pmap_pinit(pmap)
1293 register struct pmap *pmap;
1294 {
1295 vm_page_t ptdpg[NPGPTD];
1296 vm_paddr_t pa;
1297 int i;
1298
1299 /*
1300 * No need to allocate page table space yet but we do need a valid
1301 * page directory table.
1302 */
1303 if (pmap->pm_pdir == NULL) {
1304 pmap->pm_pdir = (pd_entry_t *)kmem_alloc_pageable(kernel_map,
1305 NBPTD);
1306 #ifdef PAE
1307 pmap->pm_pdpt = uma_zalloc(pdptzone, M_WAITOK | M_ZERO);
1308 KASSERT(((vm_offset_t)pmap->pm_pdpt &
1309 ((NPGPTD * sizeof(pdpt_entry_t)) - 1)) == 0,
1310 ("pmap_pinit: pdpt misaligned"));
1311 KASSERT(pmap_kextract((vm_offset_t)pmap->pm_pdpt) < (4ULL<<30),
1312 ("pmap_pinit: pdpt above 4g"));
1313 #endif
1314 }
1315
1316 /*
1317 * allocate object for the ptes
1318 */
1319 if (pmap->pm_pteobj == NULL)
1320 pmap->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI +
1321 NPGPTD);
1322
1323 /*
1324 * allocate the page directory page(s)
1325 */
1326 for (i = 0; i < NPGPTD; i++) {
1327 ptdpg[i] = vm_page_grab(pmap->pm_pteobj, PTDPTDI + i,
1328 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_WIRED |
1329 VM_ALLOC_ZERO);
1330 vm_page_lock_queues();
1331 vm_page_flag_clear(ptdpg[i], PG_BUSY);
1332 ptdpg[i]->valid = VM_PAGE_BITS_ALL;
1333 vm_page_unlock_queues();
1334 }
1335
1336 pmap_qenter((vm_offset_t)pmap->pm_pdir, ptdpg, NPGPTD);
1337
1338 for (i = 0; i < NPGPTD; i++) {
1339 if ((ptdpg[i]->flags & PG_ZERO) == 0)
1340 bzero(pmap->pm_pdir + (i * NPDEPG), PAGE_SIZE);
1341 }
1342
1343 mtx_lock_spin(&allpmaps_lock);
1344 LIST_INSERT_HEAD(&allpmaps, pmap, pm_list);
1345 mtx_unlock_spin(&allpmaps_lock);
1346 /* Wire in kernel global address entries. */
1347 /* XXX copies current process, does not fill in MPPTDI */
1348 bcopy(PTD + KPTDI, pmap->pm_pdir + KPTDI, nkpt * sizeof(pd_entry_t));
1349 #ifdef SMP
1350 pmap->pm_pdir[MPPTDI] = PTD[MPPTDI];
1351 #endif
1352
1353 /* install self-referential address mapping entry(s) */
1354 for (i = 0; i < NPGPTD; i++) {
1355 pa = VM_PAGE_TO_PHYS(ptdpg[i]);
1356 pmap->pm_pdir[PTDPTDI + i] = pa | PG_V | PG_RW | PG_A | PG_M;
1357 #ifdef PAE
1358 pmap->pm_pdpt[i] = pa | PG_V;
1359 #endif
1360 }
1361
1362 pmap->pm_active = 0;
1363 TAILQ_INIT(&pmap->pm_pvlist);
1364 bzero(&pmap->pm_stats, sizeof pmap->pm_stats);
1365 }
1366
1367 /*
1368 * Wire in kernel global address entries. To avoid a race condition
1369 * between pmap initialization and pmap_growkernel, this procedure
1370 * should be called after the vmspace is attached to the process
1371 * but before this pmap is activated.
1372 */
1373 void
1374 pmap_pinit2(pmap)
1375 struct pmap *pmap;
1376 {
1377 /* XXX: Remove this stub when no longer called */
1378 }
1379
1380 /*
1381 * this routine is called if the page table page is not
1382 * mapped correctly.
1383 */
1384 static vm_page_t
1385 _pmap_allocpte(pmap, ptepindex)
1386 pmap_t pmap;
1387 unsigned ptepindex;
1388 {
1389 vm_paddr_t ptepa;
1390 vm_offset_t pteva;
1391 vm_page_t m;
1392
1393 /*
1394 * Find or fabricate a new pagetable page
1395 */
1396 m = vm_page_grab(pmap->pm_pteobj, ptepindex,
1397 VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
1398
1399 KASSERT(m->queue == PQ_NONE,
1400 ("_pmap_allocpte: %p->queue != PQ_NONE", m));
1401
1402 /*
1403 * Increment the hold count for the page table page
1404 * (denoting a new mapping.)
1405 */
1406 m->hold_count++;
1407
1408 /*
1409 * Map the pagetable page into the process address space, if
1410 * it isn't already there.
1411 */
1412
1413 pmap->pm_stats.resident_count++;
1414
1415 ptepa = VM_PAGE_TO_PHYS(m);
1416 pmap->pm_pdir[ptepindex] =
1417 (pd_entry_t) (ptepa | PG_U | PG_RW | PG_V | PG_A | PG_M);
1418
1419 /*
1420 * Try to use the new mapping, but if we cannot, then
1421 * do it with the routine that maps the page explicitly.
1422 */
1423 if ((m->flags & PG_ZERO) == 0) {
1424 if (pmap_is_current(pmap)) {
1425 pteva = VM_MAXUSER_ADDRESS + i386_ptob(ptepindex);
1426 bzero((caddr_t) pteva, PAGE_SIZE);
1427 } else {
1428 pmap_zero_page(m);
1429 }
1430 }
1431 vm_page_lock_queues();
1432 m->valid = VM_PAGE_BITS_ALL;
1433 vm_page_flag_clear(m, PG_ZERO);
1434 vm_page_wakeup(m);
1435 vm_page_unlock_queues();
1436
1437 return m;
1438 }
1439
1440 static vm_page_t
1441 pmap_allocpte(pmap_t pmap, vm_offset_t va)
1442 {
1443 unsigned ptepindex;
1444 pd_entry_t ptepa;
1445 vm_page_t m;
1446
1447 /*
1448 * Calculate pagetable page index
1449 */
1450 ptepindex = va >> PDRSHIFT;
1451
1452 /*
1453 * Get the page directory entry
1454 */
1455 ptepa = pmap->pm_pdir[ptepindex];
1456
1457 /*
1458 * This supports switching from a 4MB page to a
1459 * normal 4K page.
1460 */
1461 if (ptepa & PG_PS) {
1462 pmap->pm_pdir[ptepindex] = 0;
1463 ptepa = 0;
1464 pmap_invalidate_all(kernel_pmap);
1465 }
1466
1467 /*
1468 * If the page table page is mapped, we just increment the
1469 * hold count, and activate it.
1470 */
1471 if (ptepa) {
1472 /*
1473 * In order to get the page table page, try the
1474 * hint first.
1475 */
1476 if (pmap->pm_pteobj->root &&
1477 (pmap->pm_pteobj->root->pindex == ptepindex)) {
1478 m = pmap->pm_pteobj->root;
1479 } else {
1480 m = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
1481 }
1482 m->hold_count++;
1483 return m;
1484 }
1485 /*
1486 * Here if the pte page isn't mapped, or if it has been deallocated.
1487 */
1488 return _pmap_allocpte(pmap, ptepindex);
1489 }
1490
1491
1492 /***************************************************
1493 * Pmap allocation/deallocation routines.
1494 ***************************************************/
1495
1496 #ifdef LAZY_SWITCH
1497 #ifdef SMP
1498 /*
1499 * Deal with a SMP shootdown of other users of the pmap that we are
1500 * trying to dispose of. This can be a bit hairy.
1501 */
1502 static u_int *lazymask;
1503 static u_int lazyptd;
1504 static volatile u_int lazywait;
1505
1506 void pmap_lazyfix_action(void);
1507
1508 void
1509 pmap_lazyfix_action(void)
1510 {
1511 u_int mymask = PCPU_GET(cpumask);
1512
1513 if (rcr3() == lazyptd) {
1514 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1515 #ifdef SWTCH_OPTIM_STATS
1516 atomic_add_int(&lazy_flush_smpfixup, 1);
1517 } else {
1518 if (*lazymask & mymask)
1519 lazy_flush_smpbadcr3++;
1520 else
1521 lazy_flush_smpmiss++;
1522 #endif
1523 }
1524 atomic_clear_int(lazymask, mymask);
1525 atomic_store_rel_int(&lazywait, 1);
1526 }
1527
1528 static void
1529 pmap_lazyfix_self(u_int mymask)
1530 {
1531
1532 if (rcr3() == lazyptd) {
1533 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1534 #ifdef SWTCH_OPTIM_STATS
1535 lazy_flush_fixup++;
1536 } else {
1537 if (*lazymask & mymask)
1538 lazy_flush_smpbadcr3++;
1539 else
1540 lazy_flush_smpmiss++;
1541 #endif
1542 }
1543 atomic_clear_int(lazymask, mymask);
1544 }
1545
1546
1547 static void
1548 pmap_lazyfix(pmap_t pmap)
1549 {
1550 u_int mymask = PCPU_GET(cpumask);
1551 u_int mask;
1552 register u_int spins;
1553
1554 while ((mask = pmap->pm_active) != 0) {
1555 spins = 50000000;
1556 mask = mask & -mask; /* Find least significant set bit */
1557 mtx_lock_spin(&lazypmap_lock);
1558 #ifdef PAE
1559 lazyptd = vtophys(pmap->pm_pdpt);
1560 #else
1561 lazyptd = vtophys(pmap->pm_pdir);
1562 #endif
1563 if (mask == mymask) {
1564 lazymask = &pmap->pm_active;
1565 pmap_lazyfix_self(mymask);
1566 } else {
1567 atomic_store_rel_int((u_int *)&lazymask,
1568 (u_int)&pmap->pm_active);
1569 atomic_store_rel_int(&lazywait, 0);
1570 ipi_selected(mask, IPI_LAZYPMAP);
1571 while (lazywait == 0) {
1572 ia32_pause();
1573 if (--spins == 0)
1574 break;
1575 }
1576 #ifdef SWTCH_OPTIM_STATS
1577 lazy_flush_smpipi++;
1578 #endif
1579 }
1580 mtx_unlock_spin(&lazypmap_lock);
1581 if (spins == 0)
1582 printf("pmap_lazyfix: spun for 50000000\n");
1583 }
1584 }
1585
1586 #else /* SMP */
1587
1588 /*
1589 * Cleaning up on uniprocessor is easy. For various reasons, we're
1590 * unlikely to have to even execute this code, including the fact
1591 * that the cleanup is deferred until the parent does a wait(2), which
1592 * means that another userland process has run.
1593 */
1594 static void
1595 pmap_lazyfix(pmap_t pmap)
1596 {
1597 u_int cr3;
1598
1599 cr3 = vtophys(pmap->pm_pdir);
1600 if (cr3 == rcr3()) {
1601 load_cr3(PCPU_GET(curpcb)->pcb_cr3);
1602 pmap->pm_active &= ~(PCPU_GET(cpumask));
1603 #ifdef SWTCH_OPTIM_STATS
1604 lazy_flush_fixup++;
1605 #endif
1606 }
1607 }
1608 #endif /* SMP */
1609 #endif /* LAZY_SWITCH */
1610
1611 /*
1612 * Release any resources held by the given physical map.
1613 * Called when a pmap initialized by pmap_pinit is being released.
1614 * Should only be called if the map contains no valid mappings.
1615 */
1616 void
1617 pmap_release(pmap_t pmap)
1618 {
1619 vm_object_t object;
1620 vm_page_t m;
1621 int i;
1622
1623 object = pmap->pm_pteobj;
1624
1625 KASSERT(object->ref_count == 1,
1626 ("pmap_release: pteobj reference count %d != 1",
1627 object->ref_count));
1628 KASSERT(pmap->pm_stats.resident_count == 0,
1629 ("pmap_release: pmap resident count %ld != 0",
1630 pmap->pm_stats.resident_count));
1631
1632 #ifdef LAZY_SWITCH
1633 pmap_lazyfix(pmap);
1634 #endif
1635 mtx_lock_spin(&allpmaps_lock);
1636 LIST_REMOVE(pmap, pm_list);
1637 mtx_unlock_spin(&allpmaps_lock);
1638
1639 bzero(pmap->pm_pdir + PTDPTDI, (nkpt + NPGPTD) *
1640 sizeof(*pmap->pm_pdir));
1641 #ifdef SMP
1642 pmap->pm_pdir[MPPTDI] = 0;
1643 #endif
1644
1645 pmap_qremove((vm_offset_t)pmap->pm_pdir, NPGPTD);
1646
1647 vm_page_lock_queues();
1648 for (i = 0; i < NPGPTD; i++) {
1649 m = TAILQ_FIRST(&object->memq);
1650 #ifdef PAE
1651 KASSERT(VM_PAGE_TO_PHYS(m) == (pmap->pm_pdpt[i] & PG_FRAME),
1652 ("pmap_release: got wrong ptd page"));
1653 #endif
1654 m->wire_count--;
1655 atomic_subtract_int(&cnt.v_wire_count, 1);
1656 vm_page_busy(m);
1657 vm_page_free_zero(m);
1658 }
1659 KASSERT(TAILQ_EMPTY(&object->memq),
1660 ("pmap_release: leaking page table pages"));
1661 vm_page_unlock_queues();
1662 }
1663
1664 static int
1665 kvm_size(SYSCTL_HANDLER_ARGS)
1666 {
1667 unsigned long ksize = VM_MAX_KERNEL_ADDRESS - KERNBASE;
1668
1669 return sysctl_handle_long(oidp, &ksize, 0, req);
1670 }
1671 SYSCTL_PROC(_vm, OID_AUTO, kvm_size, CTLTYPE_LONG|CTLFLAG_RD,
1672 0, 0, kvm_size, "IU", "Size of KVM");
1673
1674 static int
1675 kvm_free(SYSCTL_HANDLER_ARGS)
1676 {
1677 unsigned long kfree = VM_MAX_KERNEL_ADDRESS - kernel_vm_end;
1678
1679 return sysctl_handle_long(oidp, &kfree, 0, req);
1680 }
1681 SYSCTL_PROC(_vm, OID_AUTO, kvm_free, CTLTYPE_LONG|CTLFLAG_RD,
1682 0, 0, kvm_free, "IU", "Amount of KVM free");
1683
1684 /*
1685 * grow the number of kernel page table entries, if needed
1686 */
1687 void
1688 pmap_growkernel(vm_offset_t addr)
1689 {
1690 struct pmap *pmap;
1691 int s;
1692 vm_paddr_t ptppaddr;
1693 vm_page_t nkpg;
1694 pd_entry_t newpdir;
1695 pt_entry_t *pde;
1696
1697 s = splhigh();
1698 mtx_assert(&kernel_map->system_mtx, MA_OWNED);
1699 if (kernel_vm_end == 0) {
1700 kernel_vm_end = KERNBASE;
1701 nkpt = 0;
1702 while (pdir_pde(PTD, kernel_vm_end)) {
1703 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1704 nkpt++;
1705 }
1706 }
1707 addr = roundup2(addr, PAGE_SIZE * NPTEPG);
1708 while (kernel_vm_end < addr) {
1709 if (pdir_pde(PTD, kernel_vm_end)) {
1710 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1711 continue;
1712 }
1713
1714 /*
1715 * This index is bogus, but out of the way
1716 */
1717 nkpg = vm_page_alloc(NULL, nkpt,
1718 VM_ALLOC_NOOBJ | VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
1719 if (!nkpg)
1720 panic("pmap_growkernel: no memory to grow kernel");
1721
1722 nkpt++;
1723
1724 pmap_zero_page(nkpg);
1725 ptppaddr = VM_PAGE_TO_PHYS(nkpg);
1726 newpdir = (pd_entry_t) (ptppaddr | PG_V | PG_RW | PG_A | PG_M);
1727 pdir_pde(PTD, kernel_vm_end) = newpdir;
1728
1729 mtx_lock_spin(&allpmaps_lock);
1730 LIST_FOREACH(pmap, &allpmaps, pm_list) {
1731 pde = pmap_pde(pmap, kernel_vm_end);
1732 pde_store(pde, newpdir);
1733 }
1734 mtx_unlock_spin(&allpmaps_lock);
1735 kernel_vm_end = (kernel_vm_end + PAGE_SIZE * NPTEPG) & ~(PAGE_SIZE * NPTEPG - 1);
1736 }
1737 splx(s);
1738 }
1739
1740
1741 /***************************************************
1742 * page management routines.
1743 ***************************************************/
1744
1745 /*
1746 * free the pv_entry back to the free list
1747 */
1748 static PMAP_INLINE void
1749 free_pv_entry(pv_entry_t pv)
1750 {
1751 pv_entry_count--;
1752 uma_zfree(pvzone, pv);
1753 }
1754
1755 /*
1756 * get a new pv_entry, allocating a block from the system
1757 * when needed.
1758 * the memory allocation is performed bypassing the malloc code
1759 * because of the possibility of allocations at interrupt time.
1760 */
1761 static pv_entry_t
1762 get_pv_entry(void)
1763 {
1764 pv_entry_count++;
1765 if (pv_entry_high_water &&
1766 (pv_entry_count > pv_entry_high_water) &&
1767 (pmap_pagedaemon_waken == 0)) {
1768 pmap_pagedaemon_waken = 1;
1769 wakeup (&vm_pages_needed);
1770 }
1771 return uma_zalloc(pvzone, M_NOWAIT);
1772 }
1773
1774 /*
1775 * If it is the first entry on the list, it is actually
1776 * in the header and we must copy the following entry up
1777 * to the header. Otherwise we must search the list for
1778 * the entry. In either case we free the now unused entry.
1779 */
1780
1781 static int
1782 pmap_remove_entry(pmap_t pmap, vm_page_t m, vm_offset_t va)
1783 {
1784 pv_entry_t pv;
1785 int rtval;
1786 int s;
1787
1788 s = splvm();
1789 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
1790 if (m->md.pv_list_count < pmap->pm_stats.resident_count) {
1791 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
1792 if (pmap == pv->pv_pmap && va == pv->pv_va)
1793 break;
1794 }
1795 } else {
1796 TAILQ_FOREACH(pv, &pmap->pm_pvlist, pv_plist) {
1797 if (va == pv->pv_va)
1798 break;
1799 }
1800 }
1801
1802 rtval = 0;
1803 if (pv) {
1804 rtval = pmap_unuse_pt(pmap, va, pv->pv_ptem);
1805 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
1806 m->md.pv_list_count--;
1807 if (TAILQ_FIRST(&m->md.pv_list) == NULL)
1808 vm_page_flag_clear(m, PG_WRITEABLE);
1809
1810 TAILQ_REMOVE(&pmap->pm_pvlist, pv, pv_plist);
1811 free_pv_entry(pv);
1812 }
1813
1814 splx(s);
1815 return rtval;
1816 }
1817
1818 /*
1819 * Create a pv entry for page at pa for
1820 * (pmap, va).
1821 */
1822 static void
1823 pmap_insert_entry(pmap_t pmap, vm_offset_t va, vm_page_t mpte, vm_page_t m)
1824 {
1825
1826 int s;
1827 pv_entry_t pv;
1828
1829 s = splvm();
1830 pv = get_pv_entry();
1831 pv->pv_va = va;
1832 pv->pv_pmap = pmap;
1833 pv->pv_ptem = mpte;
1834
1835 TAILQ_INSERT_TAIL(&pmap->pm_pvlist, pv, pv_plist);
1836 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
1837 m->md.pv_list_count++;
1838
1839 splx(s);
1840 }
1841
1842 /*
1843 * pmap_remove_pte: do the things to unmap a page in a process
1844 */
1845 static int
1846 pmap_remove_pte(pmap_t pmap, pt_entry_t *ptq, vm_offset_t va)
1847 {
1848 pt_entry_t oldpte;
1849 vm_page_t m;
1850
1851 oldpte = pte_load_clear(ptq);
1852 if (oldpte & PG_W)
1853 pmap->pm_stats.wired_count -= 1;
1854 /*
1855 * Machines that don't support invlpg, also don't support
1856 * PG_G.
1857 */
1858 if (oldpte & PG_G)
1859 pmap_invalidate_page(kernel_pmap, va);
1860 pmap->pm_stats.resident_count -= 1;
1861 if (oldpte & PG_MANAGED) {
1862 m = PHYS_TO_VM_PAGE(oldpte);
1863 if (oldpte & PG_M) {
1864 #if defined(PMAP_DIAGNOSTIC)
1865 if (pmap_nw_modified((pt_entry_t) oldpte)) {
1866 printf(
1867 "pmap_remove: modified page not writable: va: 0x%x, pte: 0x%x\n",
1868 va, oldpte);
1869 }
1870 #endif
1871 if (pmap_track_modified(va))
1872 vm_page_dirty(m);
1873 }
1874 if (oldpte & PG_A)
1875 vm_page_flag_set(m, PG_REFERENCED);
1876 return pmap_remove_entry(pmap, m, va);
1877 } else {
1878 return pmap_unuse_pt(pmap, va, NULL);
1879 }
1880
1881 return 0;
1882 }
1883
1884 /*
1885 * Remove a single page from a process address space
1886 */
1887 static void
1888 pmap_remove_page(pmap_t pmap, vm_offset_t va)
1889 {
1890 pt_entry_t *pte;
1891
1892 if ((pte = pmap_pte_quick(pmap, va)) == NULL || *pte == 0)
1893 return;
1894 pmap_remove_pte(pmap, pte, va);
1895 pmap_invalidate_page(pmap, va);
1896 }
1897
1898 /*
1899 * Remove the given range of addresses from the specified map.
1900 *
1901 * It is assumed that the start and end are properly
1902 * rounded to the page size.
1903 */
1904 void
1905 pmap_remove(pmap_t pmap, vm_offset_t sva, vm_offset_t eva)
1906 {
1907 vm_offset_t pdnxt;
1908 pd_entry_t ptpaddr;
1909 pt_entry_t *pte;
1910 int anyvalid;
1911
1912 if (pmap == NULL)
1913 return;
1914
1915 if (pmap->pm_stats.resident_count == 0)
1916 return;
1917
1918 /*
1919 * special handling of removing one page. a very
1920 * common operation and easy to short circuit some
1921 * code.
1922 */
1923 if ((sva + PAGE_SIZE == eva) &&
1924 ((pmap->pm_pdir[(sva >> PDRSHIFT)] & PG_PS) == 0)) {
1925 pmap_remove_page(pmap, sva);
1926 return;
1927 }
1928
1929 anyvalid = 0;
1930
1931 for (; sva < eva; sva = pdnxt) {
1932 unsigned pdirindex;
1933
1934 /*
1935 * Calculate index for next page table.
1936 */
1937 pdnxt = (sva + NBPDR) & ~PDRMASK;
1938 if (pmap->pm_stats.resident_count == 0)
1939 break;
1940
1941 pdirindex = sva >> PDRSHIFT;
1942 ptpaddr = pmap->pm_pdir[pdirindex];
1943
1944 /*
1945 * Weed out invalid mappings. Note: we assume that the page
1946 * directory table is always allocated, and in kernel virtual.
1947 */
1948 if (ptpaddr == 0)
1949 continue;
1950
1951 /*
1952 * Check for large page.
1953 */
1954 if ((ptpaddr & PG_PS) != 0) {
1955 pmap->pm_pdir[pdirindex] = 0;
1956 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
1957 anyvalid = 1;
1958 continue;
1959 }
1960
1961 /*
1962 * Limit our scan to either the end of the va represented
1963 * by the current page table page, or to the end of the
1964 * range being removed.
1965 */
1966 if (pdnxt > eva)
1967 pdnxt = eva;
1968
1969 for (; sva != pdnxt; sva += PAGE_SIZE) {
1970 if ((pte = pmap_pte_quick(pmap, sva)) == NULL ||
1971 *pte == 0)
1972 continue;
1973 anyvalid = 1;
1974 if (pmap_remove_pte(pmap, pte, sva))
1975 break;
1976 }
1977 }
1978
1979 if (anyvalid)
1980 pmap_invalidate_all(pmap);
1981 }
1982
1983 /*
1984 * Routine: pmap_remove_all
1985 * Function:
1986 * Removes this physical page from
1987 * all physical maps in which it resides.
1988 * Reflects back modify bits to the pager.
1989 *
1990 * Notes:
1991 * Original versions of this routine were very
1992 * inefficient because they iteratively called
1993 * pmap_remove (slow...)
1994 */
1995
1996 void
1997 pmap_remove_all(vm_page_t m)
1998 {
1999 register pv_entry_t pv;
2000 pt_entry_t *pte, tpte;
2001 int s;
2002
2003 #if defined(PMAP_DIAGNOSTIC)
2004 /*
2005 * XXX This makes pmap_remove_all() illegal for non-managed pages!
2006 */
2007 if (!pmap_initialized || (m->flags & PG_FICTITIOUS)) {
2008 panic("pmap_remove_all: illegal for unmanaged page, va: 0x%x",
2009 VM_PAGE_TO_PHYS(m));
2010 }
2011 #endif
2012 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2013 s = splvm();
2014 while ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
2015 pv->pv_pmap->pm_stats.resident_count--;
2016 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
2017 tpte = pte_load_clear(pte);
2018 if (tpte & PG_W)
2019 pv->pv_pmap->pm_stats.wired_count--;
2020 if (tpte & PG_A)
2021 vm_page_flag_set(m, PG_REFERENCED);
2022
2023 /*
2024 * Update the vm_page_t clean and reference bits.
2025 */
2026 if (tpte & PG_M) {
2027 #if defined(PMAP_DIAGNOSTIC)
2028 if (pmap_nw_modified((pt_entry_t) tpte)) {
2029 printf(
2030 "pmap_remove_all: modified page not writable: va: 0x%x, pte: 0x%x\n",
2031 pv->pv_va, tpte);
2032 }
2033 #endif
2034 if (pmap_track_modified(pv->pv_va))
2035 vm_page_dirty(m);
2036 }
2037 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
2038 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
2039 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
2040 m->md.pv_list_count--;
2041 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
2042 free_pv_entry(pv);
2043 }
2044 vm_page_flag_clear(m, PG_WRITEABLE);
2045 splx(s);
2046 }
2047
2048 /*
2049 * Set the physical protection on the
2050 * specified range of this map as requested.
2051 */
2052 void
2053 pmap_protect(pmap_t pmap, vm_offset_t sva, vm_offset_t eva, vm_prot_t prot)
2054 {
2055 vm_offset_t pdnxt;
2056 pd_entry_t ptpaddr;
2057 int anychanged;
2058
2059 if (pmap == NULL)
2060 return;
2061
2062 if ((prot & VM_PROT_READ) == VM_PROT_NONE) {
2063 pmap_remove(pmap, sva, eva);
2064 return;
2065 }
2066
2067 if (prot & VM_PROT_WRITE)
2068 return;
2069
2070 anychanged = 0;
2071
2072 for (; sva < eva; sva = pdnxt) {
2073 unsigned pdirindex;
2074
2075 pdnxt = (sva + NBPDR) & ~PDRMASK;
2076
2077 pdirindex = sva >> PDRSHIFT;
2078 ptpaddr = pmap->pm_pdir[pdirindex];
2079
2080 /*
2081 * Weed out invalid mappings. Note: we assume that the page
2082 * directory table is always allocated, and in kernel virtual.
2083 */
2084 if (ptpaddr == 0)
2085 continue;
2086
2087 /*
2088 * Check for large page.
2089 */
2090 if ((ptpaddr & PG_PS) != 0) {
2091 pmap->pm_pdir[pdirindex] &= ~(PG_M|PG_RW);
2092 pmap->pm_stats.resident_count -= NBPDR / PAGE_SIZE;
2093 anychanged = 1;
2094 continue;
2095 }
2096
2097 if (pdnxt > eva)
2098 pdnxt = eva;
2099
2100 for (; sva != pdnxt; sva += PAGE_SIZE) {
2101 pt_entry_t pbits;
2102 pt_entry_t *pte;
2103 vm_page_t m;
2104
2105 if ((pte = pmap_pte_quick(pmap, sva)) == NULL)
2106 continue;
2107 pbits = *pte;
2108 if (pbits & PG_MANAGED) {
2109 m = NULL;
2110 if (pbits & PG_A) {
2111 m = PHYS_TO_VM_PAGE(pbits);
2112 vm_page_flag_set(m, PG_REFERENCED);
2113 pbits &= ~PG_A;
2114 }
2115 if ((pbits & PG_M) != 0 &&
2116 pmap_track_modified(sva)) {
2117 if (m == NULL)
2118 m = PHYS_TO_VM_PAGE(pbits);
2119 vm_page_dirty(m);
2120 pbits &= ~PG_M;
2121 }
2122 }
2123
2124 pbits &= ~PG_RW;
2125
2126 if (pbits != *pte) {
2127 pte_store(pte, pbits);
2128 anychanged = 1;
2129 }
2130 }
2131 }
2132 if (anychanged)
2133 pmap_invalidate_all(pmap);
2134 }
2135
2136 /*
2137 * Insert the given physical page (p) at
2138 * the specified virtual address (v) in the
2139 * target physical map with the protection requested.
2140 *
2141 * If specified, the page will be wired down, meaning
2142 * that the related pte can not be reclaimed.
2143 *
2144 * NB: This is the only routine which MAY NOT lazy-evaluate
2145 * or lose information. That is, this routine must actually
2146 * insert this page into the given map NOW.
2147 */
2148 void
2149 pmap_enter(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_prot_t prot,
2150 boolean_t wired)
2151 {
2152 vm_paddr_t pa;
2153 register pt_entry_t *pte;
2154 vm_paddr_t opa;
2155 pt_entry_t origpte, newpte;
2156 vm_page_t mpte;
2157
2158 if (pmap == NULL)
2159 return;
2160
2161 va &= PG_FRAME;
2162 #ifdef PMAP_DIAGNOSTIC
2163 if (va > VM_MAX_KERNEL_ADDRESS)
2164 panic("pmap_enter: toobig");
2165 if ((va >= UPT_MIN_ADDRESS) && (va < UPT_MAX_ADDRESS))
2166 panic("pmap_enter: invalid to pmap_enter page table pages (va: 0x%x)", va);
2167 #endif
2168
2169 mpte = NULL;
2170 /*
2171 * In the case that a page table page is not
2172 * resident, we are creating it here.
2173 */
2174 if (va < VM_MAXUSER_ADDRESS) {
2175 mpte = pmap_allocpte(pmap, va);
2176 }
2177 #if 0 && defined(PMAP_DIAGNOSTIC)
2178 else {
2179 pd_entry_t *pdeaddr = pmap_pde(pmap, va);
2180 origpte = *pdeaddr;
2181 if ((origpte & PG_V) == 0) {
2182 panic("pmap_enter: invalid kernel page table page, pdir=%p, pde=%p, va=%p\n",
2183 pmap->pm_pdir[PTDPTDI], origpte, va);
2184 }
2185 }
2186 #endif
2187
2188 pte = pmap_pte_quick(pmap, va);
2189
2190 /*
2191 * Page Directory table entry not valid, we need a new PT page
2192 */
2193 if (pte == NULL) {
2194 panic("pmap_enter: invalid page directory pdir=%#jx, va=%#x\n",
2195 (uintmax_t)pmap->pm_pdir[PTDPTDI], va);
2196 }
2197
2198 pa = VM_PAGE_TO_PHYS(m) & PG_FRAME;
2199 origpte = *pte;
2200 opa = origpte & PG_FRAME;
2201
2202 if (origpte & PG_PS)
2203 panic("pmap_enter: attempted pmap_enter on 4MB page");
2204
2205 /*
2206 * Mapping has not changed, must be protection or wiring change.
2207 */
2208 if (origpte && (opa == pa)) {
2209 /*
2210 * Wiring change, just update stats. We don't worry about
2211 * wiring PT pages as they remain resident as long as there
2212 * are valid mappings in them. Hence, if a user page is wired,
2213 * the PT page will be also.
2214 */
2215 if (wired && ((origpte & PG_W) == 0))
2216 pmap->pm_stats.wired_count++;
2217 else if (!wired && (origpte & PG_W))
2218 pmap->pm_stats.wired_count--;
2219
2220 #if defined(PMAP_DIAGNOSTIC)
2221 if (pmap_nw_modified((pt_entry_t) origpte)) {
2222 printf(
2223 "pmap_enter: modified page not writable: va: 0x%x, pte: 0x%x\n",
2224 va, origpte);
2225 }
2226 #endif
2227
2228 /*
2229 * Remove extra pte reference
2230 */
2231 if (mpte)
2232 mpte->hold_count--;
2233
2234 if ((prot & VM_PROT_WRITE) && (origpte & PG_V)) {
2235 if ((origpte & PG_RW) == 0) {
2236 pte_store(pte, origpte | PG_RW);
2237 pmap_invalidate_page(pmap, va);
2238 }
2239 return;
2240 }
2241
2242 /*
2243 * We might be turning off write access to the page,
2244 * so we go ahead and sense modify status.
2245 */
2246 if (origpte & PG_MANAGED) {
2247 if ((origpte & PG_M) && pmap_track_modified(va)) {
2248 vm_page_t om;
2249 om = PHYS_TO_VM_PAGE(opa);
2250 vm_page_dirty(om);
2251 }
2252 pa |= PG_MANAGED;
2253 }
2254 goto validate;
2255 }
2256 /*
2257 * Mapping has changed, invalidate old range and fall through to
2258 * handle validating new mapping.
2259 */
2260 if (opa) {
2261 int err;
2262 vm_page_lock_queues();
2263 err = pmap_remove_pte(pmap, pte, va);
2264 vm_page_unlock_queues();
2265 if (err)
2266 panic("pmap_enter: pte vanished, va: 0x%x", va);
2267 }
2268
2269 /*
2270 * Enter on the PV list if part of our managed memory. Note that we
2271 * raise IPL while manipulating pv_table since pmap_enter can be
2272 * called at interrupt time.
2273 */
2274 if (pmap_initialized &&
2275 (m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0) {
2276 pmap_insert_entry(pmap, va, mpte, m);
2277 pa |= PG_MANAGED;
2278 }
2279
2280 /*
2281 * Increment counters
2282 */
2283 pmap->pm_stats.resident_count++;
2284 if (wired)
2285 pmap->pm_stats.wired_count++;
2286
2287 validate:
2288 /*
2289 * Now validate mapping with desired protection/wiring.
2290 */
2291 newpte = (pt_entry_t)(pa | pte_prot(pmap, prot) | PG_V);
2292
2293 if (wired)
2294 newpte |= PG_W;
2295 if (va < VM_MAXUSER_ADDRESS)
2296 newpte |= PG_U;
2297 if (pmap == kernel_pmap)
2298 newpte |= pgeflag;
2299
2300 /*
2301 * if the mapping or permission bits are different, we need
2302 * to update the pte.
2303 */
2304 if ((origpte & ~(PG_M|PG_A)) != newpte) {
2305 pte_store(pte, newpte | PG_A);
2306 /*if (origpte)*/ {
2307 pmap_invalidate_page(pmap, va);
2308 }
2309 }
2310 }
2311
2312 /*
2313 * this code makes some *MAJOR* assumptions:
2314 * 1. Current pmap & pmap exists.
2315 * 2. Not wired.
2316 * 3. Read access.
2317 * 4. No page table pages.
2318 * 5. Tlbflush is deferred to calling procedure.
2319 * 6. Page IS managed.
2320 * but is *MUCH* faster than pmap_enter...
2321 */
2322
2323 static vm_page_t
2324 pmap_enter_quick(pmap_t pmap, vm_offset_t va, vm_page_t m, vm_page_t mpte)
2325 {
2326 pt_entry_t *pte;
2327 vm_paddr_t pa;
2328
2329 /*
2330 * In the case that a page table page is not
2331 * resident, we are creating it here.
2332 */
2333 if (va < VM_MAXUSER_ADDRESS) {
2334 unsigned ptepindex;
2335 pd_entry_t ptepa;
2336
2337 /*
2338 * Calculate pagetable page index
2339 */
2340 ptepindex = va >> PDRSHIFT;
2341 if (mpte && (mpte->pindex == ptepindex)) {
2342 mpte->hold_count++;
2343 } else {
2344 retry:
2345 /*
2346 * Get the page directory entry
2347 */
2348 ptepa = pmap->pm_pdir[ptepindex];
2349
2350 /*
2351 * If the page table page is mapped, we just increment
2352 * the hold count, and activate it.
2353 */
2354 if (ptepa) {
2355 if (ptepa & PG_PS)
2356 panic("pmap_enter_quick: unexpected mapping into 4MB page");
2357 if (pmap->pm_pteobj->root &&
2358 (pmap->pm_pteobj->root->pindex == ptepindex)) {
2359 mpte = pmap->pm_pteobj->root;
2360 } else {
2361 mpte = pmap_page_lookup(pmap->pm_pteobj, ptepindex);
2362 }
2363 if (mpte == NULL)
2364 goto retry;
2365 mpte->hold_count++;
2366 } else {
2367 mpte = _pmap_allocpte(pmap, ptepindex);
2368 }
2369 }
2370 } else {
2371 mpte = NULL;
2372 }
2373
2374 /*
2375 * This call to vtopte makes the assumption that we are
2376 * entering the page into the current pmap. In order to support
2377 * quick entry into any pmap, one would likely use pmap_pte_quick.
2378 * But that isn't as quick as vtopte.
2379 */
2380 pte = vtopte(va);
2381 if (*pte) {
2382 if (mpte != NULL) {
2383 vm_page_lock_queues();
2384 pmap_unwire_pte_hold(pmap, mpte);
2385 vm_page_unlock_queues();
2386 }
2387 return 0;
2388 }
2389
2390 /*
2391 * Enter on the PV list if part of our managed memory. Note that we
2392 * raise IPL while manipulating pv_table since pmap_enter can be
2393 * called at interrupt time.
2394 */
2395 if ((m->flags & (PG_FICTITIOUS|PG_UNMANAGED)) == 0)
2396 pmap_insert_entry(pmap, va, mpte, m);
2397
2398 /*
2399 * Increment counters
2400 */
2401 pmap->pm_stats.resident_count++;
2402
2403 pa = VM_PAGE_TO_PHYS(m);
2404
2405 /*
2406 * Now validate mapping with RO protection
2407 */
2408 if (m->flags & (PG_FICTITIOUS|PG_UNMANAGED))
2409 pte_store(pte, pa | PG_V | PG_U);
2410 else
2411 pte_store(pte, pa | PG_V | PG_U | PG_MANAGED);
2412
2413 return mpte;
2414 }
2415
2416 /*
2417 * Make a temporary mapping for a physical address. This is only intended
2418 * to be used for panic dumps.
2419 */
2420 void *
2421 pmap_kenter_temporary(vm_offset_t pa, int i)
2422 {
2423 vm_offset_t va;
2424
2425 va = (vm_offset_t)crashdumpmap + (i * PAGE_SIZE);
2426 pmap_kenter(va, pa);
2427 #ifndef I386_CPU
2428 invlpg(va);
2429 #else
2430 invltlb();
2431 #endif
2432 return ((void *)crashdumpmap);
2433 }
2434
2435 #define MAX_INIT_PT (96)
2436 /*
2437 * pmap_object_init_pt preloads the ptes for a given object
2438 * into the specified pmap. This eliminates the blast of soft
2439 * faults on process startup and immediately after an mmap.
2440 */
2441 void
2442 pmap_object_init_pt(pmap_t pmap, vm_offset_t addr,
2443 vm_object_t object, vm_pindex_t pindex,
2444 vm_size_t size, int limit)
2445 {
2446 vm_offset_t tmpidx;
2447 int psize;
2448 vm_page_t p, mpte;
2449
2450 if (pmap == NULL || object == NULL)
2451 return;
2452
2453 /*
2454 * This code maps large physical mmap regions into the
2455 * processor address space. Note that some shortcuts
2456 * are taken, but the code works.
2457 */
2458 if (pseflag && (object->type == OBJT_DEVICE) &&
2459 ((addr & (NBPDR - 1)) == 0) && ((size & (NBPDR - 1)) == 0)) {
2460 int i;
2461 vm_page_t m[1];
2462 unsigned int ptepindex;
2463 int npdes;
2464 pd_entry_t ptepa;
2465
2466 if (pmap->pm_pdir[ptepindex = (addr >> PDRSHIFT)])
2467 return;
2468
2469 retry:
2470 p = vm_page_lookup(object, pindex);
2471 if (p != NULL) {
2472 vm_page_lock_queues();
2473 if (vm_page_sleep_if_busy(p, FALSE, "init4p"))
2474 goto retry;
2475 } else {
2476 p = vm_page_alloc(object, pindex, VM_ALLOC_NORMAL);
2477 if (p == NULL)
2478 return;
2479 m[0] = p;
2480
2481 if (vm_pager_get_pages(object, m, 1, 0) != VM_PAGER_OK) {
2482 vm_page_lock_queues();
2483 vm_page_free(p);
2484 vm_page_unlock_queues();
2485 return;
2486 }
2487
2488 p = vm_page_lookup(object, pindex);
2489 vm_page_lock_queues();
2490 vm_page_wakeup(p);
2491 }
2492 vm_page_unlock_queues();
2493
2494 ptepa = VM_PAGE_TO_PHYS(p);
2495 if (ptepa & (NBPDR - 1)) {
2496 return;
2497 }
2498
2499 p->valid = VM_PAGE_BITS_ALL;
2500
2501 pmap->pm_stats.resident_count += size >> PAGE_SHIFT;
2502 npdes = size >> PDRSHIFT;
2503 for(i = 0; i < npdes; i++) {
2504 pde_store(&pmap->pm_pdir[ptepindex],
2505 ptepa | PG_U | PG_RW | PG_V | PG_PS);
2506 ptepa += NBPDR;
2507 ptepindex += 1;
2508 }
2509 pmap_invalidate_all(kernel_pmap);
2510 return;
2511 }
2512
2513 psize = i386_btop(size);
2514
2515 if ((object->type != OBJT_VNODE) ||
2516 ((limit & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
2517 (object->resident_page_count > MAX_INIT_PT))) {
2518 return;
2519 }
2520
2521 if (psize + pindex > object->size) {
2522 if (object->size < pindex)
2523 return;
2524 psize = object->size - pindex;
2525 }
2526
2527 mpte = NULL;
2528
2529 if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
2530 if (p->pindex < pindex) {
2531 p = vm_page_splay(pindex, object->root);
2532 if ((object->root = p)->pindex < pindex)
2533 p = TAILQ_NEXT(p, listq);
2534 }
2535 }
2536 /*
2537 * Assert: the variable p is either (1) the page with the
2538 * least pindex greater than or equal to the parameter pindex
2539 * or (2) NULL.
2540 */
2541 for (;
2542 p != NULL && (tmpidx = p->pindex - pindex) < psize;
2543 p = TAILQ_NEXT(p, listq)) {
2544 /*
2545 * don't allow an madvise to blow away our really
2546 * free pages allocating pv entries.
2547 */
2548 if ((limit & MAP_PREFAULT_MADVISE) &&
2549 cnt.v_free_count < cnt.v_free_reserved) {
2550 break;
2551 }
2552 vm_page_lock_queues();
2553 if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
2554 (p->busy == 0) &&
2555 (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2556 if ((p->queue - p->pc) == PQ_CACHE)
2557 vm_page_deactivate(p);
2558 vm_page_busy(p);
2559 vm_page_unlock_queues();
2560 mpte = pmap_enter_quick(pmap,
2561 addr + i386_ptob(tmpidx), p, mpte);
2562 vm_page_lock_queues();
2563 vm_page_wakeup(p);
2564 }
2565 vm_page_unlock_queues();
2566 }
2567 return;
2568 }
2569
2570 /*
2571 * pmap_prefault provides a quick way of clustering
2572 * pagefaults into a processes address space. It is a "cousin"
2573 * of pmap_object_init_pt, except it runs at page fault time instead
2574 * of mmap time.
2575 */
2576 #define PFBAK 4
2577 #define PFFOR 4
2578 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2579
2580 static int pmap_prefault_pageorder[] = {
2581 -1 * PAGE_SIZE, 1 * PAGE_SIZE,
2582 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2583 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2584 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2585 };
2586
2587 void
2588 pmap_prefault(pmap, addra, entry)
2589 pmap_t pmap;
2590 vm_offset_t addra;
2591 vm_map_entry_t entry;
2592 {
2593 int i;
2594 vm_offset_t starta;
2595 vm_offset_t addr;
2596 vm_pindex_t pindex;
2597 vm_page_t m, mpte;
2598 vm_object_t object;
2599
2600 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)))
2601 return;
2602
2603 object = entry->object.vm_object;
2604
2605 starta = addra - PFBAK * PAGE_SIZE;
2606 if (starta < entry->start) {
2607 starta = entry->start;
2608 } else if (starta > addra) {
2609 starta = 0;
2610 }
2611
2612 mpte = NULL;
2613 for (i = 0; i < PAGEORDER_SIZE; i++) {
2614 vm_object_t lobject;
2615 pt_entry_t *pte;
2616
2617 addr = addra + pmap_prefault_pageorder[i];
2618 if (addr > addra + (PFFOR * PAGE_SIZE))
2619 addr = 0;
2620
2621 if (addr < starta || addr >= entry->end)
2622 continue;
2623
2624 if ((*pmap_pde(pmap, addr)) == 0)
2625 continue;
2626
2627 pte = vtopte(addr);
2628 if (*pte)
2629 continue;
2630
2631 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2632 lobject = object;
2633 for (m = vm_page_lookup(lobject, pindex);
2634 (!m && (lobject->type == OBJT_DEFAULT) && (lobject->backing_object));
2635 lobject = lobject->backing_object) {
2636 if (lobject->backing_object_offset & PAGE_MASK)
2637 break;
2638 pindex += (lobject->backing_object_offset >> PAGE_SHIFT);
2639 m = vm_page_lookup(lobject->backing_object, pindex);
2640 }
2641
2642 /*
2643 * give-up when a page is not in memory
2644 */
2645 if (m == NULL)
2646 break;
2647 vm_page_lock_queues();
2648 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2649 (m->busy == 0) &&
2650 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2651
2652 if ((m->queue - m->pc) == PQ_CACHE) {
2653 vm_page_deactivate(m);
2654 }
2655 vm_page_busy(m);
2656 vm_page_unlock_queues();
2657 mpte = pmap_enter_quick(pmap, addr, m, mpte);
2658 vm_page_lock_queues();
2659 vm_page_wakeup(m);
2660 }
2661 vm_page_unlock_queues();
2662 }
2663 }
2664
2665 /*
2666 * Routine: pmap_change_wiring
2667 * Function: Change the wiring attribute for a map/virtual-address
2668 * pair.
2669 * In/out conditions:
2670 * The mapping must already exist in the pmap.
2671 */
2672 void
2673 pmap_change_wiring(pmap, va, wired)
2674 register pmap_t pmap;
2675 vm_offset_t va;
2676 boolean_t wired;
2677 {
2678 register pt_entry_t *pte;
2679
2680 if (pmap == NULL)
2681 return;
2682
2683 pte = pmap_pte_quick(pmap, va);
2684
2685 if (wired && !pmap_pte_w(pte))
2686 pmap->pm_stats.wired_count++;
2687 else if (!wired && pmap_pte_w(pte))
2688 pmap->pm_stats.wired_count--;
2689
2690 /*
2691 * Wiring is not a hardware characteristic so there is no need to
2692 * invalidate TLB.
2693 */
2694 pmap_pte_set_w(pte, wired);
2695 }
2696
2697
2698
2699 /*
2700 * Copy the range specified by src_addr/len
2701 * from the source map to the range dst_addr/len
2702 * in the destination map.
2703 *
2704 * This routine is only advisory and need not do anything.
2705 */
2706
2707 void
2708 pmap_copy(pmap_t dst_pmap, pmap_t src_pmap, vm_offset_t dst_addr, vm_size_t len,
2709 vm_offset_t src_addr)
2710 {
2711 vm_offset_t addr;
2712 vm_offset_t end_addr = src_addr + len;
2713 vm_offset_t pdnxt;
2714 vm_page_t m;
2715
2716 if (dst_addr != src_addr)
2717 return;
2718
2719 if (!pmap_is_current(src_pmap))
2720 return;
2721
2722 for (addr = src_addr; addr < end_addr; addr = pdnxt) {
2723 pt_entry_t *src_pte, *dst_pte;
2724 vm_page_t dstmpte, srcmpte;
2725 pd_entry_t srcptepaddr;
2726 unsigned ptepindex;
2727
2728 if (addr >= UPT_MIN_ADDRESS)
2729 panic("pmap_copy: invalid to pmap_copy page tables\n");
2730
2731 /*
2732 * Don't let optional prefaulting of pages make us go
2733 * way below the low water mark of free pages or way
2734 * above high water mark of used pv entries.
2735 */
2736 if (cnt.v_free_count < cnt.v_free_reserved ||
2737 pv_entry_count > pv_entry_high_water)
2738 break;
2739
2740 pdnxt = (addr + NBPDR) & ~PDRMASK;
2741 ptepindex = addr >> PDRSHIFT;
2742
2743 srcptepaddr = src_pmap->pm_pdir[ptepindex];
2744 if (srcptepaddr == 0)
2745 continue;
2746
2747 if (srcptepaddr & PG_PS) {
2748 if (dst_pmap->pm_pdir[ptepindex] == 0) {
2749 dst_pmap->pm_pdir[ptepindex] = srcptepaddr;
2750 dst_pmap->pm_stats.resident_count +=
2751 NBPDR / PAGE_SIZE;
2752 }
2753 continue;
2754 }
2755
2756 srcmpte = vm_page_lookup(src_pmap->pm_pteobj, ptepindex);
2757 if ((srcmpte == NULL) ||
2758 (srcmpte->hold_count == 0) || (srcmpte->flags & PG_BUSY))
2759 continue;
2760
2761 if (pdnxt > end_addr)
2762 pdnxt = end_addr;
2763
2764 src_pte = vtopte(addr);
2765 while (addr < pdnxt) {
2766 pt_entry_t ptetemp;
2767 ptetemp = *src_pte;
2768 /*
2769 * we only virtual copy managed pages
2770 */
2771 if ((ptetemp & PG_MANAGED) != 0) {
2772 /*
2773 * We have to check after allocpte for the
2774 * pte still being around... allocpte can
2775 * block.
2776 */
2777 dstmpte = pmap_allocpte(dst_pmap, addr);
2778 dst_pte = pmap_pte_quick(dst_pmap, addr);
2779 if ((*dst_pte == 0) && (ptetemp = *src_pte)) {
2780 /*
2781 * Clear the modified and
2782 * accessed (referenced) bits
2783 * during the copy.
2784 */
2785 m = PHYS_TO_VM_PAGE(ptetemp);
2786 *dst_pte = ptetemp & ~(PG_M | PG_A);
2787 dst_pmap->pm_stats.resident_count++;
2788 pmap_insert_entry(dst_pmap, addr,
2789 dstmpte, m);
2790 } else {
2791 vm_page_lock_queues();
2792 pmap_unwire_pte_hold(dst_pmap, dstmpte);
2793 vm_page_unlock_queues();
2794 }
2795 if (dstmpte->hold_count >= srcmpte->hold_count)
2796 break;
2797 }
2798 addr += PAGE_SIZE;
2799 src_pte++;
2800 }
2801 }
2802 }
2803
2804 #ifdef SMP
2805
2806 /*
2807 * pmap_zpi_switchin*()
2808 *
2809 * These functions allow us to avoid doing IPIs alltogether in certain
2810 * temporary page-mapping situations (page zeroing). Instead to deal
2811 * with being preempted and moved onto a different cpu we invalidate
2812 * the page when the scheduler switches us in. This does not occur
2813 * very often so we remain relatively optimal with very little effort.
2814 */
2815 static void
2816 pmap_zpi_switchin12(void)
2817 {
2818 invlpg((u_int)CADDR1);
2819 invlpg((u_int)CADDR2);
2820 }
2821
2822 static void
2823 pmap_zpi_switchin2(void)
2824 {
2825 invlpg((u_int)CADDR2);
2826 }
2827
2828 static void
2829 pmap_zpi_switchin3(void)
2830 {
2831 invlpg((u_int)CADDR3);
2832 }
2833
2834 #endif
2835
2836 /*
2837 * pmap_zero_page zeros the specified hardware page by mapping
2838 * the page into KVM and using bzero to clear its contents.
2839 */
2840 void
2841 pmap_zero_page(vm_page_t m)
2842 {
2843
2844 mtx_lock(&CMAPCADDR12_lock);
2845 if (*CMAP2)
2846 panic("pmap_zero_page: CMAP2 busy");
2847 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2848 #ifdef I386_CPU
2849 invltlb();
2850 #else
2851 #ifdef SMP
2852 curthread->td_switchin = pmap_zpi_switchin2;
2853 #endif
2854 invlpg((u_int)CADDR2);
2855 #endif
2856 #if defined(I686_CPU)
2857 if (cpu_class == CPUCLASS_686)
2858 i686_pagezero(CADDR2);
2859 else
2860 #endif
2861 bzero(CADDR2, PAGE_SIZE);
2862 #ifdef SMP
2863 curthread->td_switchin = NULL;
2864 #endif
2865 *CMAP2 = 0;
2866 mtx_unlock(&CMAPCADDR12_lock);
2867 }
2868
2869 /*
2870 * pmap_zero_page_area zeros the specified hardware page by mapping
2871 * the page into KVM and using bzero to clear its contents.
2872 *
2873 * off and size may not cover an area beyond a single hardware page.
2874 */
2875 void
2876 pmap_zero_page_area(vm_page_t m, int off, int size)
2877 {
2878
2879 mtx_lock(&CMAPCADDR12_lock);
2880 if (*CMAP2)
2881 panic("pmap_zero_page: CMAP2 busy");
2882 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2883 #ifdef I386_CPU
2884 invltlb();
2885 #else
2886 #ifdef SMP
2887 curthread->td_switchin = pmap_zpi_switchin2;
2888 #endif
2889 invlpg((u_int)CADDR2);
2890 #endif
2891 #if defined(I686_CPU)
2892 if (cpu_class == CPUCLASS_686 && off == 0 && size == PAGE_SIZE)
2893 i686_pagezero(CADDR2);
2894 else
2895 #endif
2896 bzero((char *)CADDR2 + off, size);
2897 #ifdef SMP
2898 curthread->td_switchin = NULL;
2899 #endif
2900 *CMAP2 = 0;
2901 mtx_unlock(&CMAPCADDR12_lock);
2902 }
2903
2904 /*
2905 * pmap_zero_page_idle zeros the specified hardware page by mapping
2906 * the page into KVM and using bzero to clear its contents. This
2907 * is intended to be called from the vm_pagezero process only and
2908 * outside of Giant.
2909 */
2910 void
2911 pmap_zero_page_idle(vm_page_t m)
2912 {
2913
2914 if (*CMAP3)
2915 panic("pmap_zero_page: CMAP3 busy");
2916 *CMAP3 = PG_V | PG_RW | VM_PAGE_TO_PHYS(m) | PG_A | PG_M;
2917 #ifdef I386_CPU
2918 invltlb();
2919 #else
2920 #ifdef SMP
2921 curthread->td_switchin = pmap_zpi_switchin3;
2922 #endif
2923 invlpg((u_int)CADDR3);
2924 #endif
2925 #if defined(I686_CPU)
2926 if (cpu_class == CPUCLASS_686)
2927 i686_pagezero(CADDR3);
2928 else
2929 #endif
2930 bzero(CADDR3, PAGE_SIZE);
2931 #ifdef SMP
2932 curthread->td_switchin = NULL;
2933 #endif
2934 *CMAP3 = 0;
2935 }
2936
2937 /*
2938 * pmap_copy_page copies the specified (machine independent)
2939 * page by mapping the page into virtual memory and using
2940 * bcopy to copy the page, one machine dependent page at a
2941 * time.
2942 */
2943 void
2944 pmap_copy_page(vm_page_t src, vm_page_t dst)
2945 {
2946
2947 mtx_lock(&CMAPCADDR12_lock);
2948 if (*CMAP1)
2949 panic("pmap_copy_page: CMAP1 busy");
2950 if (*CMAP2)
2951 panic("pmap_copy_page: CMAP2 busy");
2952 *CMAP1 = PG_V | VM_PAGE_TO_PHYS(src) | PG_A;
2953 *CMAP2 = PG_V | PG_RW | VM_PAGE_TO_PHYS(dst) | PG_A | PG_M;
2954 #ifdef I386_CPU
2955 invltlb();
2956 #else
2957 #ifdef SMP
2958 curthread->td_switchin = pmap_zpi_switchin12;
2959 #endif
2960 invlpg((u_int)CADDR1);
2961 invlpg((u_int)CADDR2);
2962 #endif
2963 bcopy(CADDR1, CADDR2, PAGE_SIZE);
2964 #ifdef SMP
2965 curthread->td_switchin = NULL;
2966 #endif
2967 *CMAP1 = 0;
2968 *CMAP2 = 0;
2969 mtx_unlock(&CMAPCADDR12_lock);
2970 }
2971
2972 /*
2973 * Returns true if the pmap's pv is one of the first
2974 * 16 pvs linked to from this page. This count may
2975 * be changed upwards or downwards in the future; it
2976 * is only necessary that true be returned for a small
2977 * subset of pmaps for proper page aging.
2978 */
2979 boolean_t
2980 pmap_page_exists_quick(pmap, m)
2981 pmap_t pmap;
2982 vm_page_t m;
2983 {
2984 pv_entry_t pv;
2985 int loops = 0;
2986 int s;
2987
2988 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
2989 return FALSE;
2990
2991 s = splvm();
2992 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
2993 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
2994 if (pv->pv_pmap == pmap) {
2995 splx(s);
2996 return TRUE;
2997 }
2998 loops++;
2999 if (loops >= 16)
3000 break;
3001 }
3002 splx(s);
3003 return (FALSE);
3004 }
3005
3006 #define PMAP_REMOVE_PAGES_CURPROC_ONLY
3007 /*
3008 * Remove all pages from specified address space
3009 * this aids process exit speeds. Also, this code
3010 * is special cased for current process only, but
3011 * can have the more generic (and slightly slower)
3012 * mode enabled. This is much faster than pmap_remove
3013 * in the case of running down an entire address space.
3014 */
3015 void
3016 pmap_remove_pages(pmap, sva, eva)
3017 pmap_t pmap;
3018 vm_offset_t sva, eva;
3019 {
3020 pt_entry_t *pte, tpte;
3021 vm_page_t m;
3022 pv_entry_t pv, npv;
3023 int s;
3024
3025 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
3026 if (!curthread || (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))) {
3027 printf("warning: pmap_remove_pages called with non-current pmap\n");
3028 return;
3029 }
3030 #endif
3031 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3032 s = splvm();
3033 for (pv = TAILQ_FIRST(&pmap->pm_pvlist); pv; pv = npv) {
3034
3035 if (pv->pv_va >= eva || pv->pv_va < sva) {
3036 npv = TAILQ_NEXT(pv, pv_plist);
3037 continue;
3038 }
3039
3040 #ifdef PMAP_REMOVE_PAGES_CURPROC_ONLY
3041 pte = vtopte(pv->pv_va);
3042 #else
3043 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3044 #endif
3045 tpte = *pte;
3046
3047 if (tpte == 0) {
3048 printf("TPTE at %p IS ZERO @ VA %08x\n",
3049 pte, pv->pv_va);
3050 panic("bad pte");
3051 }
3052
3053 /*
3054 * We cannot remove wired pages from a process' mapping at this time
3055 */
3056 if (tpte & PG_W) {
3057 npv = TAILQ_NEXT(pv, pv_plist);
3058 continue;
3059 }
3060
3061 m = PHYS_TO_VM_PAGE(tpte);
3062 KASSERT(m->phys_addr == (tpte & PG_FRAME),
3063 ("vm_page_t %p phys_addr mismatch %016jx %016jx",
3064 m, (uintmax_t)m->phys_addr, (uintmax_t)tpte));
3065
3066 KASSERT(m < &vm_page_array[vm_page_array_size],
3067 ("pmap_remove_pages: bad tpte %#jx", (uintmax_t)tpte));
3068
3069 pv->pv_pmap->pm_stats.resident_count--;
3070
3071 pte_clear(pte);
3072
3073 /*
3074 * Update the vm_page_t clean and reference bits.
3075 */
3076 if (tpte & PG_M) {
3077 vm_page_dirty(m);
3078 }
3079
3080 npv = TAILQ_NEXT(pv, pv_plist);
3081 TAILQ_REMOVE(&pv->pv_pmap->pm_pvlist, pv, pv_plist);
3082
3083 m->md.pv_list_count--;
3084 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3085 if (TAILQ_FIRST(&m->md.pv_list) == NULL) {
3086 vm_page_flag_clear(m, PG_WRITEABLE);
3087 }
3088
3089 pmap_unuse_pt(pv->pv_pmap, pv->pv_va, pv->pv_ptem);
3090 free_pv_entry(pv);
3091 }
3092 splx(s);
3093 pmap_invalidate_all(pmap);
3094 }
3095
3096 /*
3097 * pmap_is_modified:
3098 *
3099 * Return whether or not the specified physical page was modified
3100 * in any physical maps.
3101 */
3102 boolean_t
3103 pmap_is_modified(vm_page_t m)
3104 {
3105 pv_entry_t pv;
3106 pt_entry_t *pte;
3107 int s;
3108
3109 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3110 return FALSE;
3111
3112 s = splvm();
3113 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3114 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3115 /*
3116 * if the bit being tested is the modified bit, then
3117 * mark clean_map and ptes as never
3118 * modified.
3119 */
3120 if (!pmap_track_modified(pv->pv_va))
3121 continue;
3122 #if defined(PMAP_DIAGNOSTIC)
3123 if (!pv->pv_pmap) {
3124 printf("Null pmap (tb) at va: 0x%x\n", pv->pv_va);
3125 continue;
3126 }
3127 #endif
3128 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3129 if (*pte & PG_M) {
3130 splx(s);
3131 return TRUE;
3132 }
3133 }
3134 splx(s);
3135 return (FALSE);
3136 }
3137
3138 /*
3139 * this routine is used to modify bits in ptes
3140 */
3141 static __inline void
3142 pmap_changebit(vm_page_t m, int bit, boolean_t setem)
3143 {
3144 register pv_entry_t pv;
3145 register pt_entry_t *pte;
3146 int s;
3147
3148 if (!pmap_initialized || (m->flags & PG_FICTITIOUS) ||
3149 (!setem && bit == PG_RW && (m->flags & PG_WRITEABLE) == 0))
3150 return;
3151
3152 s = splvm();
3153 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3154 /*
3155 * Loop over all current mappings setting/clearing as appropos If
3156 * setting RO do we need to clear the VAC?
3157 */
3158 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3159 /*
3160 * don't write protect pager mappings
3161 */
3162 if (!setem && (bit == PG_RW)) {
3163 if (!pmap_track_modified(pv->pv_va))
3164 continue;
3165 }
3166
3167 #if defined(PMAP_DIAGNOSTIC)
3168 if (!pv->pv_pmap) {
3169 printf("Null pmap (cb) at va: 0x%x\n", pv->pv_va);
3170 continue;
3171 }
3172 #endif
3173
3174 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3175
3176 if (setem) {
3177 *pte |= bit;
3178 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
3179 } else {
3180 pt_entry_t pbits = *pte;
3181 if (pbits & bit) {
3182 if (bit == PG_RW) {
3183 if (pbits & PG_M) {
3184 vm_page_dirty(m);
3185 }
3186 pte_store(pte, pbits & ~(PG_M|PG_RW));
3187 } else {
3188 pte_store(pte, pbits & ~bit);
3189 }
3190 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
3191 }
3192 }
3193 }
3194 if (!setem && bit == PG_RW)
3195 vm_page_flag_clear(m, PG_WRITEABLE);
3196 splx(s);
3197 }
3198
3199 /*
3200 * pmap_page_protect:
3201 *
3202 * Lower the permission for all mappings to a given page.
3203 */
3204 void
3205 pmap_page_protect(vm_page_t m, vm_prot_t prot)
3206 {
3207 if ((prot & VM_PROT_WRITE) == 0) {
3208 if (prot & (VM_PROT_READ | VM_PROT_EXECUTE)) {
3209 pmap_changebit(m, PG_RW, FALSE);
3210 } else {
3211 pmap_remove_all(m);
3212 }
3213 }
3214 }
3215
3216 /*
3217 * pmap_ts_referenced:
3218 *
3219 * Return a count of reference bits for a page, clearing those bits.
3220 * It is not necessary for every reference bit to be cleared, but it
3221 * is necessary that 0 only be returned when there are truly no
3222 * reference bits set.
3223 *
3224 * XXX: The exact number of bits to check and clear is a matter that
3225 * should be tested and standardized at some point in the future for
3226 * optimal aging of shared pages.
3227 */
3228 int
3229 pmap_ts_referenced(vm_page_t m)
3230 {
3231 register pv_entry_t pv, pvf, pvn;
3232 pt_entry_t *pte;
3233 pt_entry_t v;
3234 int s;
3235 int rtval = 0;
3236
3237 if (!pmap_initialized || (m->flags & PG_FICTITIOUS))
3238 return (rtval);
3239
3240 s = splvm();
3241 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3242 if ((pv = TAILQ_FIRST(&m->md.pv_list)) != NULL) {
3243
3244 pvf = pv;
3245
3246 do {
3247 pvn = TAILQ_NEXT(pv, pv_list);
3248
3249 TAILQ_REMOVE(&m->md.pv_list, pv, pv_list);
3250
3251 TAILQ_INSERT_TAIL(&m->md.pv_list, pv, pv_list);
3252
3253 if (!pmap_track_modified(pv->pv_va))
3254 continue;
3255
3256 pte = pmap_pte_quick(pv->pv_pmap, pv->pv_va);
3257
3258 if (pte && ((v = pte_load(pte)) & PG_A) != 0) {
3259 pte_store(pte, v & ~PG_A);
3260 pmap_invalidate_page(pv->pv_pmap, pv->pv_va);
3261
3262 rtval++;
3263 if (rtval > 4) {
3264 break;
3265 }
3266 }
3267 } while ((pv = pvn) != NULL && pv != pvf);
3268 }
3269 splx(s);
3270
3271 return (rtval);
3272 }
3273
3274 /*
3275 * Clear the modify bits on the specified physical page.
3276 */
3277 void
3278 pmap_clear_modify(vm_page_t m)
3279 {
3280 pmap_changebit(m, PG_M, FALSE);
3281 }
3282
3283 /*
3284 * pmap_clear_reference:
3285 *
3286 * Clear the reference bit on the specified physical page.
3287 */
3288 void
3289 pmap_clear_reference(vm_page_t m)
3290 {
3291 pmap_changebit(m, PG_A, FALSE);
3292 }
3293
3294 /*
3295 * Miscellaneous support routines follow
3296 */
3297
3298 static void
3299 i386_protection_init()
3300 {
3301 register int *kp, prot;
3302
3303 kp = protection_codes;
3304 for (prot = 0; prot < 8; prot++) {
3305 switch (prot) {
3306 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_NONE:
3307 /*
3308 * Read access is also 0. There isn't any execute bit,
3309 * so just make it readable.
3310 */
3311 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_NONE:
3312 case VM_PROT_READ | VM_PROT_NONE | VM_PROT_EXECUTE:
3313 case VM_PROT_NONE | VM_PROT_NONE | VM_PROT_EXECUTE:
3314 *kp++ = 0;
3315 break;
3316 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_NONE:
3317 case VM_PROT_NONE | VM_PROT_WRITE | VM_PROT_EXECUTE:
3318 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_NONE:
3319 case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
3320 *kp++ = PG_RW;
3321 break;
3322 }
3323 }
3324 }
3325
3326 /*
3327 * Map a set of physical memory pages into the kernel virtual
3328 * address space. Return a pointer to where it is mapped. This
3329 * routine is intended to be used for mapping device memory,
3330 * NOT real memory.
3331 */
3332 void *
3333 pmap_mapdev(pa, size)
3334 vm_paddr_t pa;
3335 vm_size_t size;
3336 {
3337 vm_offset_t va, tmpva, offset;
3338
3339 offset = pa & PAGE_MASK;
3340 size = roundup(offset + size, PAGE_SIZE);
3341
3342 GIANT_REQUIRED;
3343
3344 va = kmem_alloc_pageable(kernel_map, size);
3345 if (!va)
3346 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
3347
3348 pa = pa & PG_FRAME;
3349 for (tmpva = va; size > 0; ) {
3350 pmap_kenter(tmpva, pa);
3351 size -= PAGE_SIZE;
3352 tmpva += PAGE_SIZE;
3353 pa += PAGE_SIZE;
3354 }
3355 pmap_invalidate_range(kernel_pmap, va, tmpva);
3356 return ((void *)(va + offset));
3357 }
3358
3359 void
3360 pmap_unmapdev(va, size)
3361 vm_offset_t va;
3362 vm_size_t size;
3363 {
3364 vm_offset_t base, offset, tmpva;
3365 pt_entry_t *pte;
3366
3367 base = va & PG_FRAME;
3368 offset = va & PAGE_MASK;
3369 size = roundup(offset + size, PAGE_SIZE);
3370 for (tmpva = base; tmpva < (base + size); tmpva += PAGE_SIZE) {
3371 pte = vtopte(tmpva);
3372 pte_clear(pte);
3373 }
3374 pmap_invalidate_range(kernel_pmap, va, tmpva);
3375 kmem_free(kernel_map, base, size);
3376 }
3377
3378 /*
3379 * perform the pmap work for mincore
3380 */
3381 int
3382 pmap_mincore(pmap, addr)
3383 pmap_t pmap;
3384 vm_offset_t addr;
3385 {
3386 pt_entry_t *ptep, pte;
3387 vm_page_t m;
3388 int val = 0;
3389
3390 ptep = pmap_pte_quick(pmap, addr);
3391 if (ptep == 0) {
3392 return 0;
3393 }
3394
3395 if ((pte = *ptep) != 0) {
3396 vm_paddr_t pa;
3397
3398 val = MINCORE_INCORE;
3399 if ((pte & PG_MANAGED) == 0)
3400 return val;
3401
3402 pa = pte & PG_FRAME;
3403
3404 m = PHYS_TO_VM_PAGE(pa);
3405
3406 /*
3407 * Modified by us
3408 */
3409 if (pte & PG_M)
3410 val |= MINCORE_MODIFIED|MINCORE_MODIFIED_OTHER;
3411 else {
3412 /*
3413 * Modified by someone else
3414 */
3415 vm_page_lock_queues();
3416 if (m->dirty || pmap_is_modified(m))
3417 val |= MINCORE_MODIFIED_OTHER;
3418 vm_page_unlock_queues();
3419 }
3420 /*
3421 * Referenced by us
3422 */
3423 if (pte & PG_A)
3424 val |= MINCORE_REFERENCED|MINCORE_REFERENCED_OTHER;
3425 else {
3426 /*
3427 * Referenced by someone else
3428 */
3429 vm_page_lock_queues();
3430 if ((m->flags & PG_REFERENCED) ||
3431 pmap_ts_referenced(m)) {
3432 val |= MINCORE_REFERENCED_OTHER;
3433 vm_page_flag_set(m, PG_REFERENCED);
3434 }
3435 vm_page_unlock_queues();
3436 }
3437 }
3438 return val;
3439 }
3440
3441 void
3442 pmap_activate(struct thread *td)
3443 {
3444 struct proc *p = td->td_proc;
3445 pmap_t pmap;
3446 u_int32_t cr3;
3447
3448 critical_enter();
3449 pmap = vmspace_pmap(td->td_proc->p_vmspace);
3450 #if defined(SMP)
3451 atomic_set_int(&pmap->pm_active, PCPU_GET(cpumask));
3452 #else
3453 pmap->pm_active |= 1;
3454 #endif
3455 #ifdef PAE
3456 cr3 = vtophys(pmap->pm_pdpt);
3457 #else
3458 cr3 = vtophys(pmap->pm_pdir);
3459 #endif
3460 /* XXXKSE this is wrong.
3461 * pmap_activate is for the current thread on the current cpu
3462 */
3463 if (p->p_flag & P_THREADED) {
3464 /* Make sure all other cr3 entries are updated. */
3465 /* what if they are running? XXXKSE (maybe abort them) */
3466 FOREACH_THREAD_IN_PROC(p, td) {
3467 td->td_pcb->pcb_cr3 = cr3;
3468 }
3469 } else {
3470 td->td_pcb->pcb_cr3 = cr3;
3471 }
3472 load_cr3(cr3);
3473 #ifdef SWTCH_OPTIM_STATS
3474 tlb_flush_count++;
3475 #endif
3476 critical_exit();
3477 }
3478
3479 vm_offset_t
3480 pmap_addr_hint(vm_object_t obj, vm_offset_t addr, vm_size_t size)
3481 {
3482
3483 if ((obj == NULL) || (size < NBPDR) || (obj->type != OBJT_DEVICE)) {
3484 return addr;
3485 }
3486
3487 addr = (addr + (NBPDR - 1)) & ~(NBPDR - 1);
3488 return addr;
3489 }
3490
3491
3492 #if defined(PMAP_DEBUG)
3493 pmap_pid_dump(int pid)
3494 {
3495 pmap_t pmap;
3496 struct proc *p;
3497 int npte = 0;
3498 int index;
3499
3500 sx_slock(&allproc_lock);
3501 LIST_FOREACH(p, &allproc, p_list) {
3502 if (p->p_pid != pid)
3503 continue;
3504
3505 if (p->p_vmspace) {
3506 int i,j;
3507 index = 0;
3508 pmap = vmspace_pmap(p->p_vmspace);
3509 for (i = 0; i < NPDEPTD; i++) {
3510 pd_entry_t *pde;
3511 pt_entry_t *pte;
3512 vm_offset_t base = i << PDRSHIFT;
3513
3514 pde = &pmap->pm_pdir[i];
3515 if (pde && pmap_pde_v(pde)) {
3516 for (j = 0; j < NPTEPG; j++) {
3517 vm_offset_t va = base + (j << PAGE_SHIFT);
3518 if (va >= (vm_offset_t) VM_MIN_KERNEL_ADDRESS) {
3519 if (index) {
3520 index = 0;
3521 printf("\n");
3522 }
3523 sx_sunlock(&allproc_lock);
3524 return npte;
3525 }
3526 pte = pmap_pte_quick(pmap, va);
3527 if (pte && pmap_pte_v(pte)) {
3528 pt_entry_t pa;
3529 vm_page_t m;
3530 pa = *pte;
3531 m = PHYS_TO_VM_PAGE(pa);
3532 printf("va: 0x%x, pt: 0x%x, h: %d, w: %d, f: 0x%x",
3533 va, pa, m->hold_count, m->wire_count, m->flags);
3534 npte++;
3535 index++;
3536 if (index >= 2) {
3537 index = 0;
3538 printf("\n");
3539 } else {
3540 printf(" ");
3541 }
3542 }
3543 }
3544 }
3545 }
3546 }
3547 }
3548 sx_sunlock(&allproc_lock);
3549 return npte;
3550 }
3551 #endif
3552
3553 #if defined(DEBUG)
3554
3555 static void pads(pmap_t pm);
3556 void pmap_pvdump(vm_offset_t pa);
3557
3558 /* print address space of pmap*/
3559 static void
3560 pads(pm)
3561 pmap_t pm;
3562 {
3563 int i, j;
3564 vm_paddr_t va;
3565 pt_entry_t *ptep;
3566
3567 if (pm == kernel_pmap)
3568 return;
3569 for (i = 0; i < NPDEPTD; i++)
3570 if (pm->pm_pdir[i])
3571 for (j = 0; j < NPTEPG; j++) {
3572 va = (i << PDRSHIFT) + (j << PAGE_SHIFT);
3573 if (pm == kernel_pmap && va < KERNBASE)
3574 continue;
3575 if (pm != kernel_pmap && va > UPT_MAX_ADDRESS)
3576 continue;
3577 ptep = pmap_pte_quick(pm, va);
3578 if (pmap_pte_v(ptep))
3579 printf("%x:%x ", va, *ptep);
3580 };
3581
3582 }
3583
3584 void
3585 pmap_pvdump(pa)
3586 vm_paddr_t pa;
3587 {
3588 pv_entry_t pv;
3589 vm_page_t m;
3590
3591 printf("pa %x", pa);
3592 m = PHYS_TO_VM_PAGE(pa);
3593 TAILQ_FOREACH(pv, &m->md.pv_list, pv_list) {
3594 printf(" -> pmap %p, va %x", (void *)pv->pv_pmap, pv->pv_va);
3595 pads(pv->pv_pmap);
3596 }
3597 printf(" ");
3598 }
3599 #endif
Cache object: cc455e0b3ae0b49bf8825cd5e7a57017
|