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