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