pmap.c revision 1.27 1 /* $NetBSD: pmap.c,v 1.27 2003/10/20 05:47:52 simonb Exp $ */
2
3 /*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
40 * Copyright (C) 1995, 1996 TooLs GmbH.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by TooLs GmbH.
54 * 4. The name of TooLs GmbH may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.27 2003/10/20 05:47:52 simonb Exp $");
71
72 #include <sys/param.h>
73 #include <sys/malloc.h>
74 #include <sys/proc.h>
75 #include <sys/user.h>
76 #include <sys/queue.h>
77 #include <sys/systm.h>
78 #include <sys/pool.h>
79 #include <sys/device.h>
80
81 #include <uvm/uvm.h>
82
83 #include <machine/cpu.h>
84 #include <machine/pcb.h>
85 #include <machine/powerpc.h>
86
87 #include <powerpc/spr.h>
88 #include <machine/tlb.h>
89
90 /*
91 * kernmap is an array of PTEs large enough to map in
92 * 4GB. At 16KB/page it is 256K entries or 2MB.
93 */
94 #define KERNMAP_SIZE ((0xffffffffU/PAGE_SIZE)+1)
95 caddr_t kernmap;
96
97 #define MINCTX 2
98 #define NUMCTX 256
99 volatile struct pmap *ctxbusy[NUMCTX];
100
101 #define TLBF_USED 0x1
102 #define TLBF_REF 0x2
103 #define TLBF_LOCKED 0x4
104 #define TLB_LOCKED(i) (tlb_info[(i)].ti_flags & TLBF_LOCKED)
105 typedef struct tlb_info_s {
106 char ti_flags;
107 char ti_ctx; /* TLB_PID assiciated with the entry */
108 u_int ti_va;
109 } tlb_info_t;
110
111 volatile tlb_info_t tlb_info[NTLB];
112 /* We'll use a modified FIFO replacement policy cause it's cheap */
113 volatile int tlbnext = TLB_NRESERVED;
114
115 u_long dtlb_miss_count = 0;
116 u_long itlb_miss_count = 0;
117 u_long ktlb_miss_count = 0;
118 u_long utlb_miss_count = 0;
119
120 /* Event counters */
121 struct evcnt tlbmiss_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
122 NULL, "cpu", "tlbmiss");
123 struct evcnt tlbhit_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
124 NULL, "cpu", "tlbhit");
125 struct evcnt tlbflush_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
126 NULL, "cpu", "tlbflush");
127 struct evcnt tlbenter_ev = EVCNT_INITIALIZER(EVCNT_TYPE_TRAP,
128 NULL, "cpu", "tlbenter");
129
130 struct pmap kernel_pmap_;
131
132 int physmem;
133 static int npgs;
134 static u_int nextavail;
135 #ifndef MSGBUFADDR
136 extern paddr_t msgbuf_paddr;
137 #endif
138
139 static struct mem_region *mem, *avail;
140
141 /*
142 * This is a cache of referenced/modified bits.
143 * Bits herein are shifted by ATTRSHFT.
144 */
145 static char *pmap_attrib;
146
147 #define PV_WIRED 0x1
148 #define PV_WIRE(pv) ((pv)->pv_va |= PV_WIRED)
149 #define PV_CMPVA(va,pv) (!(((pv)->pv_va^(va))&(~PV_WIRED)))
150
151 struct pv_entry {
152 struct pv_entry *pv_next; /* Linked list of mappings */
153 vaddr_t pv_va; /* virtual address of mapping */
154 struct pmap *pv_pm;
155 };
156
157 struct pv_entry *pv_table;
158 static struct pool pv_pool;
159
160 static int pmap_initialized;
161
162 static int ctx_flush(int);
163
164 inline struct pv_entry *pa_to_pv(paddr_t);
165 static inline char *pa_to_attr(paddr_t);
166
167 static inline volatile u_int *pte_find(struct pmap *, vaddr_t);
168 static inline int pte_enter(struct pmap *, vaddr_t, u_int);
169
170 static void pmap_pinit(pmap_t);
171 static void pmap_release(pmap_t);
172 static inline int pmap_enter_pv(struct pmap *, vaddr_t, paddr_t);
173 static void pmap_remove_pv(struct pmap *, vaddr_t, paddr_t);
174
175
176 inline struct pv_entry *
177 pa_to_pv(paddr_t pa)
178 {
179 int bank, pg;
180
181 bank = vm_physseg_find(atop(pa), &pg);
182 if (bank == -1)
183 return NULL;
184 return &vm_physmem[bank].pmseg.pvent[pg];
185 }
186
187 static inline char *
188 pa_to_attr(paddr_t pa)
189 {
190 int bank, pg;
191
192 bank = vm_physseg_find(atop(pa), &pg);
193 if (bank == -1)
194 return NULL;
195 return &vm_physmem[bank].pmseg.attrs[pg];
196 }
197
198 /*
199 * Insert PTE into page table.
200 */
201 int
202 pte_enter(struct pmap *pm, vaddr_t va, u_int pte)
203 {
204 int seg = STIDX(va);
205 int ptn = PTIDX(va);
206 u_int oldpte;
207
208 if (!pm->pm_ptbl[seg]) {
209 /* Don't allocate a page to clear a non-existent mapping. */
210 if (!pte) return (0);
211 /* Allocate a page XXXX this will sleep! */
212 pm->pm_ptbl[seg] =
213 (uint *)uvm_km_alloc1(kernel_map, PAGE_SIZE, 1);
214 }
215 oldpte = pm->pm_ptbl[seg][ptn];
216 pm->pm_ptbl[seg][ptn] = pte;
217
218 /* Flush entry. */
219 ppc4xx_tlb_flush(va, pm->pm_ctx);
220 if (oldpte != pte) {
221 if (pte == 0)
222 pm->pm_stats.resident_count--;
223 else
224 pm->pm_stats.resident_count++;
225 }
226 return (1);
227 }
228
229 /*
230 * Get a pointer to a PTE in a page table.
231 */
232 volatile u_int *
233 pte_find(struct pmap *pm, vaddr_t va)
234 {
235 int seg = STIDX(va);
236 int ptn = PTIDX(va);
237
238 if (pm->pm_ptbl[seg])
239 return (&pm->pm_ptbl[seg][ptn]);
240
241 return (NULL);
242 }
243
244 /*
245 * This is called during initppc, before the system is really initialized.
246 */
247 void
248 pmap_bootstrap(u_int kernelstart, u_int kernelend)
249 {
250 struct mem_region *mp, *mp1;
251 int cnt, i;
252 u_int s, e, sz;
253
254 /*
255 * Allocate the kernel page table at the end of
256 * kernel space so it's in the locked TTE.
257 */
258 kernmap = (caddr_t)kernelend;
259
260 /*
261 * Initialize kernel page table.
262 */
263 for (i = 0; i < STSZ; i++) {
264 pmap_kernel()->pm_ptbl[i] = 0;
265 }
266 ctxbusy[0] = ctxbusy[1] = pmap_kernel();
267
268 /*
269 * Announce page-size to the VM-system
270 */
271 uvmexp.pagesize = NBPG;
272 uvm_setpagesize();
273
274 /*
275 * Get memory.
276 */
277 mem_regions(&mem, &avail);
278 for (mp = mem; mp->size; mp++) {
279 physmem += btoc(mp->size);
280 printf("+%lx,",mp->size);
281 }
282 printf("\n");
283 ppc4xx_tlb_init();
284 /*
285 * Count the number of available entries.
286 */
287 for (cnt = 0, mp = avail; mp->size; mp++)
288 cnt++;
289
290 /*
291 * Page align all regions.
292 * Non-page aligned memory isn't very interesting to us.
293 * Also, sort the entries for ascending addresses.
294 */
295 kernelstart &= ~PGOFSET;
296 kernelend = (kernelend + PGOFSET) & ~PGOFSET;
297 for (mp = avail; mp->size; mp++) {
298 s = mp->start;
299 e = mp->start + mp->size;
300 printf("%08x-%08x -> ",s,e);
301 /*
302 * Check whether this region holds all of the kernel.
303 */
304 if (s < kernelstart && e > kernelend) {
305 avail[cnt].start = kernelend;
306 avail[cnt++].size = e - kernelend;
307 e = kernelstart;
308 }
309 /*
310 * Look whether this regions starts within the kernel.
311 */
312 if (s >= kernelstart && s < kernelend) {
313 if (e <= kernelend)
314 goto empty;
315 s = kernelend;
316 }
317 /*
318 * Now look whether this region ends within the kernel.
319 */
320 if (e > kernelstart && e <= kernelend) {
321 if (s >= kernelstart)
322 goto empty;
323 e = kernelstart;
324 }
325 /*
326 * Now page align the start and size of the region.
327 */
328 s = round_page(s);
329 e = trunc_page(e);
330 if (e < s)
331 e = s;
332 sz = e - s;
333 printf("%08x-%08x = %x\n",s,e,sz);
334 /*
335 * Check whether some memory is left here.
336 */
337 if (sz == 0) {
338 empty:
339 memmove(mp, mp + 1,
340 (cnt - (mp - avail)) * sizeof *mp);
341 cnt--;
342 mp--;
343 continue;
344 }
345 /*
346 * Do an insertion sort.
347 */
348 npgs += btoc(sz);
349 for (mp1 = avail; mp1 < mp; mp1++)
350 if (s < mp1->start)
351 break;
352 if (mp1 < mp) {
353 memmove(mp1 + 1, mp1, (char *)mp - (char *)mp1);
354 mp1->start = s;
355 mp1->size = sz;
356 } else {
357 mp->start = s;
358 mp->size = sz;
359 }
360 }
361
362 /*
363 * We cannot do pmap_steal_memory here,
364 * since we don't run with translation enabled yet.
365 */
366 #ifndef MSGBUFADDR
367 /*
368 * allow for msgbuf
369 */
370 sz = round_page(MSGBUFSIZE);
371 mp = NULL;
372 for (mp1 = avail; mp1->size; mp1++)
373 if (mp1->size >= sz)
374 mp = mp1;
375 if (mp == NULL)
376 panic("not enough memory?");
377
378 npgs -= btoc(sz);
379 msgbuf_paddr = mp->start + mp->size - sz;
380 mp->size -= sz;
381 if (mp->size <= 0)
382 memmove(mp, mp + 1, (cnt - (mp - avail)) * sizeof *mp);
383 #endif
384
385 printf("Loading pages\n");
386 for (mp = avail; mp->size; mp++)
387 uvm_page_physload(atop(mp->start), atop(mp->start + mp->size),
388 atop(mp->start), atop(mp->start + mp->size),
389 VM_FREELIST_DEFAULT);
390
391 /*
392 * Initialize kernel pmap and hardware.
393 */
394 /* Setup TLB pid allocator so it knows we alreadu using PID 1 */
395 pmap_kernel()->pm_ctx = KERNEL_PID;
396 nextavail = avail->start;
397
398
399 evcnt_attach_static(&tlbhit_ev);
400 evcnt_attach_static(&tlbmiss_ev);
401 evcnt_attach_static(&tlbflush_ev);
402 evcnt_attach_static(&tlbenter_ev);
403 printf("Done\n");
404 }
405
406 /*
407 * Restrict given range to physical memory
408 *
409 * (Used by /dev/mem)
410 */
411 void
412 pmap_real_memory(paddr_t *start, psize_t *size)
413 {
414 struct mem_region *mp;
415
416 for (mp = mem; mp->size; mp++) {
417 if (*start + *size > mp->start &&
418 *start < mp->start + mp->size) {
419 if (*start < mp->start) {
420 *size -= mp->start - *start;
421 *start = mp->start;
422 }
423 if (*start + *size > mp->start + mp->size)
424 *size = mp->start + mp->size - *start;
425 return;
426 }
427 }
428 *size = 0;
429 }
430
431 /*
432 * Initialize anything else for pmap handling.
433 * Called during vm_init().
434 */
435 void
436 pmap_init(void)
437 {
438 struct pv_entry *pv;
439 vsize_t sz;
440 vaddr_t addr;
441 int i, s;
442 int bank;
443 char *attr;
444
445 sz = (vsize_t)((sizeof(struct pv_entry) + 1) * npgs);
446 sz = round_page(sz);
447 addr = uvm_km_zalloc(kernel_map, sz);
448 s = splvm();
449 pv = pv_table = (struct pv_entry *)addr;
450 for (i = npgs; --i >= 0;)
451 pv++->pv_pm = NULL;
452 pmap_attrib = (char *)pv;
453 memset(pv, 0, npgs);
454
455 pv = pv_table;
456 attr = pmap_attrib;
457 for (bank = 0; bank < vm_nphysseg; bank++) {
458 sz = vm_physmem[bank].end - vm_physmem[bank].start;
459 vm_physmem[bank].pmseg.pvent = pv;
460 vm_physmem[bank].pmseg.attrs = attr;
461 pv += sz;
462 attr += sz;
463 }
464
465 pmap_initialized = 1;
466 splx(s);
467
468 /* Setup a pool for additional pvlist structures */
469 pool_init(&pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pv_entry", NULL);
470 }
471
472 /*
473 * How much virtual space is available to the kernel?
474 */
475 void
476 pmap_virtual_space(vaddr_t *start, vaddr_t *end)
477 {
478
479 #if 0
480 /*
481 * Reserve one segment for kernel virtual memory
482 */
483 *start = (vaddr_t)(KERNEL_SR << ADDR_SR_SHFT);
484 *end = *start + SEGMENT_LENGTH;
485 #else
486 *start = (vaddr_t) VM_MIN_KERNEL_ADDRESS;
487 *end = (vaddr_t) VM_MAX_KERNEL_ADDRESS;
488 #endif
489 }
490
491 #ifdef PMAP_GROWKERNEL
492 /*
493 * Preallocate kernel page tables to a specified VA.
494 * This simply loops through the first TTE for each
495 * page table from the beginning of the kernel pmap,
496 * reads the entry, and if the result is
497 * zero (either invalid entry or no page table) it stores
498 * a zero there, populating page tables in the process.
499 * This is not the most efficient technique but i don't
500 * expect it to be called that often.
501 */
502 extern struct vm_page *vm_page_alloc1 __P((void));
503 extern void vm_page_free1 __P((struct vm_page *));
504
505 vaddr_t kbreak = VM_MIN_KERNEL_ADDRESS;
506
507 vaddr_t
508 pmap_growkernel(maxkvaddr)
509 vaddr_t maxkvaddr;
510 {
511 int s;
512 int seg;
513 paddr_t pg;
514 struct pmap *pm = pmap_kernel();
515
516 s = splvm();
517
518 /* Align with the start of a page table */
519 for (kbreak &= ~(PTMAP-1); kbreak < maxkvaddr;
520 kbreak += PTMAP) {
521 seg = STIDX(kbreak);
522
523 if (pte_find(pm, kbreak)) continue;
524
525 if (uvm.page_init_done) {
526 pg = (paddr_t)VM_PAGE_TO_PHYS(vm_page_alloc1());
527 } else {
528 if (!uvm_page_physget(&pg))
529 panic("pmap_growkernel: no memory");
530 }
531 if (!pg) panic("pmap_growkernel: no pages");
532 pmap_zero_page((paddr_t)pg);
533
534 /* XXX This is based on all phymem being addressable */
535 pm->pm_ptbl[seg] = (u_int *)pg;
536 }
537 splx(s);
538 return (kbreak);
539 }
540
541 /*
542 * vm_page_alloc1:
543 *
544 * Allocate and return a memory cell with no associated object.
545 */
546 struct vm_page *
547 vm_page_alloc1()
548 {
549 struct vm_page *pg = uvm_pagealloc(NULL, 0, NULL, UVM_PGA_USERESERVE);
550 if (pg) {
551 pg->wire_count = 1; /* no mappings yet */
552 pg->flags &= ~PG_BUSY; /* never busy */
553 }
554 return pg;
555 }
556
557 /*
558 * vm_page_free1:
559 *
560 * Returns the given page to the free list,
561 * disassociating it with any VM object.
562 *
563 * Object and page must be locked prior to entry.
564 */
565 void
566 vm_page_free1(mem)
567 struct vm_page *mem;
568 {
569 #ifdef DIAGNOSTIC
570 if (mem->flags != (PG_CLEAN|PG_FAKE)) {
571 printf("Freeing invalid page %p\n", mem);
572 printf("pa = %llx\n", (unsigned long long)VM_PAGE_TO_PHYS(mem));
573 #ifdef DDB
574 Debugger();
575 #endif
576 return;
577 }
578 #endif
579 mem->flags |= PG_BUSY;
580 mem->wire_count = 0;
581 uvm_pagefree(mem);
582 }
583 #endif
584
585 /*
586 * Create and return a physical map.
587 */
588 struct pmap *
589 pmap_create(void)
590 {
591 struct pmap *pm;
592
593 pm = (struct pmap *)malloc(sizeof *pm, M_VMPMAP, M_WAITOK);
594 memset((caddr_t)pm, 0, sizeof *pm);
595 pmap_pinit(pm);
596 return pm;
597 }
598
599 /*
600 * Initialize a preallocated and zeroed pmap structure.
601 */
602 void
603 pmap_pinit(struct pmap *pm)
604 {
605 int i;
606
607 /*
608 * Allocate some segment registers for this pmap.
609 */
610 pm->pm_refs = 1;
611 for (i = 0; i < STSZ; i++)
612 pm->pm_ptbl[i] = NULL;
613 }
614
615 /*
616 * Add a reference to the given pmap.
617 */
618 void
619 pmap_reference(struct pmap *pm)
620 {
621
622 pm->pm_refs++;
623 }
624
625 /*
626 * Retire the given pmap from service.
627 * Should only be called if the map contains no valid mappings.
628 */
629 void
630 pmap_destroy(struct pmap *pm)
631 {
632
633 if (--pm->pm_refs == 0) {
634 pmap_release(pm);
635 free((caddr_t)pm, M_VMPMAP);
636 }
637 }
638
639 /*
640 * Release any resources held by the given physical map.
641 * Called when a pmap initialized by pmap_pinit is being released.
642 */
643 static void
644 pmap_release(struct pmap *pm)
645 {
646 int i;
647
648 for (i = 0; i < STSZ; i++)
649 if (pm->pm_ptbl[i]) {
650 uvm_km_free(kernel_map, (vaddr_t)pm->pm_ptbl[i],
651 PAGE_SIZE);
652 pm->pm_ptbl[i] = NULL;
653 }
654 if (pm->pm_ctx) ctx_free(pm);
655 }
656
657 /*
658 * Copy the range specified by src_addr/len
659 * from the source map to the range dst_addr/len
660 * in the destination map.
661 *
662 * This routine is only advisory and need not do anything.
663 */
664 void
665 pmap_copy(struct pmap *dst_pmap, struct pmap *src_pmap, vaddr_t dst_addr,
666 vsize_t len, vaddr_t src_addr)
667 {
668 }
669
670 /*
671 * Require that all active physical maps contain no
672 * incorrect entries NOW.
673 */
674 void
675 pmap_update(struct pmap *pmap)
676 {
677 }
678
679 /*
680 * Garbage collects the physical map system for
681 * pages which are no longer used.
682 * Success need not be guaranteed -- that is, there
683 * may well be pages which are not referenced, but
684 * others may be collected.
685 * Called by the pageout daemon when pages are scarce.
686 */
687 void
688 pmap_collect(struct pmap *pm)
689 {
690 }
691
692 /*
693 * Fill the given physical page with zeroes.
694 */
695 void
696 pmap_zero_page(paddr_t pa)
697 {
698
699 #ifdef PPC_4XX_NOCACHE
700 memset((caddr_t)pa, 0, PAGE_SIZE);
701 #else
702 int i;
703
704 for (i = PAGE_SIZE/CACHELINESIZE; i > 0; i--) {
705 __asm __volatile ("dcbz 0,%0" :: "r"(pa));
706 pa += CACHELINESIZE;
707 }
708 #endif
709 }
710
711 /*
712 * Copy the given physical source page to its destination.
713 */
714 void
715 pmap_copy_page(paddr_t src, paddr_t dst)
716 {
717
718 memcpy((caddr_t)dst, (caddr_t)src, PAGE_SIZE);
719 dcache_flush_page(dst);
720 }
721
722 /*
723 * This returns whether this is the first mapping of a page.
724 */
725 static inline int
726 pmap_enter_pv(struct pmap *pm, vaddr_t va, paddr_t pa)
727 {
728 struct pv_entry *pv, *npv = NULL;
729 int s;
730
731 if (!pmap_initialized)
732 return 0;
733
734 s = splvm();
735
736 pv = pa_to_pv(pa);
737 for (npv = pv; npv; npv = npv->pv_next)
738 if (npv->pv_va == va && npv->pv_pm == pm) {
739 printf("Duplicate pv: va %lx pm %p\n", va, pm);
740 #ifdef DDB
741 Debugger();
742 #endif
743 return (1);
744 }
745
746 if (!pv->pv_pm) {
747 /*
748 * No entries yet, use header as the first entry.
749 */
750 pv->pv_va = va;
751 pv->pv_pm = pm;
752 pv->pv_next = NULL;
753 } else {
754 /*
755 * There is at least one other VA mapping this page.
756 * Place this entry after the header.
757 */
758 npv = pool_get(&pv_pool, PR_WAITOK);
759 if (!npv) return (0);
760 npv->pv_va = va;
761 npv->pv_pm = pm;
762 npv->pv_next = pv->pv_next;
763 pv->pv_next = npv;
764 }
765 splx(s);
766 return (1);
767 }
768
769 static void
770 pmap_remove_pv(struct pmap *pm, vaddr_t va, paddr_t pa)
771 {
772 struct pv_entry *pv, *npv;
773
774 /*
775 * Remove from the PV table.
776 */
777 pv = pa_to_pv(pa);
778 if (!pv) return;
779
780 /*
781 * If it is the first entry on the list, it is actually
782 * in the header and we must copy the following entry up
783 * to the header. Otherwise we must search the list for
784 * the entry. In either case we free the now unused entry.
785 */
786 if (pm == pv->pv_pm && PV_CMPVA(va, pv)) {
787 if ((npv = pv->pv_next)) {
788 *pv = *npv;
789 pool_put(&pv_pool, npv);
790 } else
791 pv->pv_pm = NULL;
792 } else {
793 for (; (npv = pv->pv_next) != NULL; pv = npv)
794 if (pm == npv->pv_pm && PV_CMPVA(va, npv))
795 break;
796 if (npv) {
797 pv->pv_next = npv->pv_next;
798 pool_put(&pv_pool, npv);
799 }
800 }
801 }
802
803 /*
804 * Insert physical page at pa into the given pmap at virtual address va.
805 */
806 int
807 pmap_enter(struct pmap *pm, vaddr_t va, paddr_t pa, vm_prot_t prot, int flags)
808 {
809 int s;
810 u_int tte;
811 int managed;
812
813 /*
814 * Have to remove any existing mapping first.
815 */
816 pmap_remove(pm, va, va + PAGE_SIZE);
817
818 if (flags & PMAP_WIRED) flags |= prot;
819
820 /* If it has no protections don't bother w/the rest */
821 if (!(flags & VM_PROT_ALL))
822 return (0);
823
824 managed = 0;
825 if (vm_physseg_find(atop(pa), NULL) != -1)
826 managed = 1;
827
828 /*
829 * Generate TTE.
830 */
831 tte = TTE_PA(pa);
832 /* XXXX -- need to support multiple page sizes. */
833 tte |= TTE_SZ_16K;
834 #ifdef DIAGNOSTIC
835 if ((flags & (PME_NOCACHE | PME_WRITETHROUG)) ==
836 (PME_NOCACHE | PME_WRITETHROUG))
837 panic("pmap_enter: uncached & writethrough");
838 #endif
839 if (flags & PME_NOCACHE)
840 /* Must be I/O mapping */
841 tte |= TTE_I | TTE_G;
842 #ifdef PPC_4XX_NOCACHE
843 tte |= TTE_I;
844 #else
845 else if (flags & PME_WRITETHROUG)
846 /* Uncached and writethrough are not compatible */
847 tte |= TTE_W;
848 #endif
849 if (pm == pmap_kernel())
850 tte |= TTE_ZONE(ZONE_PRIV);
851 else
852 tte |= TTE_ZONE(ZONE_USER);
853
854 if (flags & VM_PROT_WRITE)
855 tte |= TTE_WR;
856
857 if (flags & VM_PROT_EXECUTE)
858 tte |= TTE_EX;
859
860 /*
861 * Now record mapping for later back-translation.
862 */
863 if (pmap_initialized && managed) {
864 char *attr;
865
866 if (!pmap_enter_pv(pm, va, pa)) {
867 /* Could not enter pv on a managed page */
868 return 1;
869 }
870
871 /* Now set attributes. */
872 attr = pa_to_attr(pa);
873 #ifdef DIAGNOSTIC
874 if (!attr)
875 panic("managed but no attr");
876 #endif
877 if (flags & VM_PROT_ALL)
878 *attr |= PTE_HI_REF;
879 if (flags & VM_PROT_WRITE)
880 *attr |= PTE_HI_CHG;
881 }
882
883 s = splvm();
884
885 /* Insert page into page table. */
886 pte_enter(pm, va, tte);
887
888 /* If this is a real fault, enter it in the tlb */
889 if (tte && ((flags & PMAP_WIRED) == 0)) {
890 ppc4xx_tlb_enter(pm->pm_ctx, va, tte);
891 }
892 splx(s);
893
894 /* Flush the real memory from the instruction cache. */
895 if ((prot & VM_PROT_EXECUTE) && (tte & TTE_I) == 0)
896 __syncicache((void *)pa, PAGE_SIZE);
897
898 return 0;
899 }
900
901 void
902 pmap_unwire(struct pmap *pm, vaddr_t va)
903 {
904 struct pv_entry *pv, *npv;
905 paddr_t pa;
906 int s = splvm();
907
908 if (pm == NULL) {
909 return;
910 }
911
912 if (!pmap_extract(pm, va, &pa)) {
913 return;
914 }
915
916 va |= PV_WIRED;
917
918 pv = pa_to_pv(pa);
919 if (!pv) return;
920
921 /*
922 * If it is the first entry on the list, it is actually
923 * in the header and we must copy the following entry up
924 * to the header. Otherwise we must search the list for
925 * the entry. In either case we free the now unused entry.
926 */
927 for (npv = pv; (npv = pv->pv_next) != NULL; pv = npv) {
928 if (pm == npv->pv_pm && PV_CMPVA(va, npv)) {
929 npv->pv_va &= ~PV_WIRED;
930 break;
931 }
932 }
933 splx(s);
934 }
935
936 void
937 pmap_kenter_pa(vaddr_t va, paddr_t pa, vm_prot_t prot)
938 {
939 int s;
940 u_int tte;
941 struct pmap *pm = pmap_kernel();
942
943 /*
944 * Have to remove any existing mapping first.
945 */
946
947 /*
948 * Generate TTE.
949 *
950 * XXXX
951 *
952 * Since the kernel does not handle execution privileges properly,
953 * we will handle read and execute permissions together.
954 */
955 tte = 0;
956 if (prot & VM_PROT_ALL) {
957
958 tte = TTE_PA(pa) | TTE_EX | TTE_ZONE(ZONE_PRIV);
959 /* XXXX -- need to support multiple page sizes. */
960 tte |= TTE_SZ_16K;
961 #ifdef DIAGNOSTIC
962 if ((prot & (PME_NOCACHE | PME_WRITETHROUG)) ==
963 (PME_NOCACHE | PME_WRITETHROUG))
964 panic("pmap_kenter_pa: uncached & writethrough");
965 #endif
966 if (prot & PME_NOCACHE)
967 /* Must be I/O mapping */
968 tte |= TTE_I | TTE_G;
969 #ifdef PPC_4XX_NOCACHE
970 tte |= TTE_I;
971 #else
972 else if (prot & PME_WRITETHROUG)
973 /* Uncached and writethrough are not compatible */
974 tte |= TTE_W;
975 #endif
976 if (prot & VM_PROT_WRITE)
977 tte |= TTE_WR;
978 }
979
980 s = splvm();
981
982 /* Insert page into page table. */
983 pte_enter(pm, va, tte);
984 splx(s);
985 }
986
987 void
988 pmap_kremove(vaddr_t va, vsize_t len)
989 {
990
991 while (len > 0) {
992 pte_enter(pmap_kernel(), va, 0);
993 va += PAGE_SIZE;
994 len -= PAGE_SIZE;
995 }
996 }
997
998 /*
999 * Remove the given range of mapping entries.
1000 */
1001 void
1002 pmap_remove(struct pmap *pm, vaddr_t va, vaddr_t endva)
1003 {
1004 int s;
1005 paddr_t pa;
1006 volatile u_int *ptp;
1007
1008 s = splvm();
1009 while (va < endva) {
1010
1011 if ((ptp = pte_find(pm, va)) && (pa = *ptp)) {
1012 pa = TTE_PA(pa);
1013 pmap_remove_pv(pm, va, pa);
1014 *ptp = 0;
1015 ppc4xx_tlb_flush(va, pm->pm_ctx);
1016 pm->pm_stats.resident_count--;
1017 }
1018 va += PAGE_SIZE;
1019 }
1020
1021 splx(s);
1022 }
1023
1024 /*
1025 * Get the physical page address for the given pmap/virtual address.
1026 */
1027 boolean_t
1028 pmap_extract(struct pmap *pm, vaddr_t va, paddr_t *pap)
1029 {
1030 int seg = STIDX(va);
1031 int ptn = PTIDX(va);
1032 u_int pa = 0;
1033 int s = splvm();
1034
1035 if (pm->pm_ptbl[seg] && (pa = pm->pm_ptbl[seg][ptn])) {
1036 *pap = TTE_PA(pa) | (va & PGOFSET);
1037 }
1038 splx(s);
1039 return (pa != 0);
1040 }
1041
1042 /*
1043 * Lower the protection on the specified range of this pmap.
1044 *
1045 * There are only two cases: either the protection is going to 0,
1046 * or it is going to read-only.
1047 */
1048 void
1049 pmap_protect(struct pmap *pm, vaddr_t sva, vaddr_t eva, vm_prot_t prot)
1050 {
1051 volatile u_int *ptp;
1052 int s, bic;
1053
1054 if ((prot & VM_PROT_READ) == 0) {
1055 pmap_remove(pm, sva, eva);
1056 return;
1057 }
1058 bic = 0;
1059 if ((prot & VM_PROT_WRITE) == 0) {
1060 bic |= TTE_WR;
1061 }
1062 if ((prot & VM_PROT_EXECUTE) == 0) {
1063 bic |= TTE_EX;
1064 }
1065 if (bic == 0) {
1066 return;
1067 }
1068 s = splvm();
1069 while (sva < eva) {
1070 if ((ptp = pte_find(pm, sva)) != NULL) {
1071 *ptp &= ~bic;
1072 ppc4xx_tlb_flush(sva, pm->pm_ctx);
1073 }
1074 sva += PAGE_SIZE;
1075 }
1076 splx(s);
1077 }
1078
1079 boolean_t
1080 check_attr(struct vm_page *pg, u_int mask, int clear)
1081 {
1082 paddr_t pa = VM_PAGE_TO_PHYS(pg);
1083 int s;
1084 char *attr;
1085 int rv;
1086
1087 /*
1088 * First modify bits in cache.
1089 */
1090 s = splvm();
1091 attr = pa_to_attr(pa);
1092 if (attr == NULL)
1093 return FALSE;
1094
1095 rv = ((*attr & mask) != 0);
1096 if (clear) {
1097 *attr &= ~mask;
1098 pmap_page_protect(pg, (mask == PTE_HI_CHG) ? VM_PROT_READ : 0);
1099 }
1100 splx(s);
1101 return rv;
1102 }
1103
1104
1105 /*
1106 * Lower the protection on the specified physical page.
1107 *
1108 * There are only two cases: either the protection is going to 0,
1109 * or it is going to read-only.
1110 */
1111 void
1112 pmap_page_protect(struct vm_page *pg, vm_prot_t prot)
1113 {
1114 paddr_t pa = VM_PAGE_TO_PHYS(pg);
1115 vaddr_t va;
1116 struct pv_entry *pvh, *pv, *npv;
1117 struct pmap *pm;
1118
1119 pvh = pa_to_pv(pa);
1120 if (pvh == NULL)
1121 return;
1122
1123 /* Handle extra pvs which may be deleted in the operation */
1124 for (pv = pvh->pv_next; pv; pv = npv) {
1125 npv = pv->pv_next;
1126
1127 pm = pv->pv_pm;
1128 va = pv->pv_va;
1129 pmap_protect(pm, va, va + PAGE_SIZE, prot);
1130 }
1131 /* Now check the head pv */
1132 if (pvh->pv_pm) {
1133 pv = pvh;
1134 pm = pv->pv_pm;
1135 va = pv->pv_va;
1136 pmap_protect(pm, va, va + PAGE_SIZE, prot);
1137 }
1138 }
1139
1140 /*
1141 * Activate the address space for the specified process. If the process
1142 * is the current process, load the new MMU context.
1143 */
1144 void
1145 pmap_activate(struct lwp *l)
1146 {
1147 #if 0
1148 struct pcb *pcb = &l->l_proc->p_addr->u_pcb;
1149 pmap_t pmap = l->l_proc->p_vmspace->vm_map.pmap;
1150
1151 /*
1152 * XXX Normally performed in cpu_fork().
1153 */
1154 printf("pmap_activate(%p), pmap=%p\n",l,pmap);
1155 pcb->pcb_pm = pmap;
1156 #endif
1157 }
1158
1159 /*
1160 * Deactivate the specified process's address space.
1161 */
1162 void
1163 pmap_deactivate(struct lwp *l)
1164 {
1165 }
1166
1167 /*
1168 * Synchronize caches corresponding to [addr, addr+len) in p.
1169 */
1170 void
1171 pmap_procwr(struct proc *p, vaddr_t va, size_t len)
1172 {
1173 struct pmap *pm = p->p_vmspace->vm_map.pmap;
1174 int msr, ctx, opid, step;
1175
1176 step = CACHELINESIZE;
1177
1178 /*
1179 * Need to turn off IMMU and switch to user context.
1180 * (icbi uses DMMU).
1181 */
1182 if (!(ctx = pm->pm_ctx)) {
1183 /* No context -- assign it one */
1184 ctx_alloc(pm);
1185 ctx = pm->pm_ctx;
1186 }
1187 __asm __volatile("mfmsr %0;"
1188 "li %1, %7;"
1189 "andc %1,%0,%1;"
1190 "mtmsr %1;"
1191 "sync;isync;"
1192 "mfpid %1;"
1193 "mtpid %2;"
1194 "sync; isync;"
1195 "1:"
1196 "dcbf 0,%3;"
1197 "icbi 0,%3;"
1198 "add %3,%3,%5;"
1199 "addc. %4,%4,%6;"
1200 "bge 1b;"
1201 "mtpid %1;"
1202 "mtmsr %0;"
1203 "sync; isync"
1204 : "=&r" (msr), "=&r" (opid)
1205 : "r" (ctx), "r" (va), "r" (len), "r" (step), "r" (-step),
1206 "K" (PSL_IR | PSL_DR));
1207 }
1208
1209
1210 /* This has to be done in real mode !!! */
1211 void
1212 ppc4xx_tlb_flush(vaddr_t va, int pid)
1213 {
1214 u_long i, found;
1215 u_long msr;
1216
1217 /* If there's no context then it can't be mapped. */
1218 if (!pid)
1219 return;
1220
1221 asm("mfpid %1;" /* Save PID */
1222 "mfmsr %2;" /* Save MSR */
1223 "li %0,0;" /* Now clear MSR */
1224 "mtmsr %0;"
1225 "mtpid %4;" /* Set PID */
1226 "sync;"
1227 "tlbsx. %0,0,%3;" /* Search TLB */
1228 "sync;"
1229 "mtpid %1;" /* Restore PID */
1230 "mtmsr %2;" /* Restore MSR */
1231 "sync;isync;"
1232 "li %1,1;"
1233 "beq 1f;"
1234 "li %1,0;"
1235 "1:"
1236 : "=&r" (i), "=&r" (found), "=&r" (msr)
1237 : "r" (va), "r" (pid));
1238 if (found && !TLB_LOCKED(i)) {
1239
1240 /* Now flush translation */
1241 asm volatile(
1242 "tlbwe %0,%1,0;"
1243 "sync;isync;"
1244 : : "r" (0), "r" (i));
1245
1246 tlb_info[i].ti_ctx = 0;
1247 tlb_info[i].ti_flags = 0;
1248 tlbnext = i;
1249 /* Successful flushes */
1250 tlbflush_ev.ev_count++;
1251 }
1252 }
1253
1254 void
1255 ppc4xx_tlb_flush_all(void)
1256 {
1257 u_long i;
1258
1259 for (i = 0; i < NTLB; i++)
1260 if (!TLB_LOCKED(i)) {
1261 asm volatile(
1262 "tlbwe %0,%1,0;"
1263 "sync;isync;"
1264 : : "r" (0), "r" (i));
1265 tlb_info[i].ti_ctx = 0;
1266 tlb_info[i].ti_flags = 0;
1267 }
1268
1269 asm volatile("sync;isync");
1270 }
1271
1272 /* Find a TLB entry to evict. */
1273 static int
1274 ppc4xx_tlb_find_victim(void)
1275 {
1276 int flags;
1277
1278 for (;;) {
1279 if (++tlbnext >= NTLB)
1280 tlbnext = TLB_NRESERVED;
1281 flags = tlb_info[tlbnext].ti_flags;
1282 if (!(flags & TLBF_USED) ||
1283 (flags & (TLBF_LOCKED | TLBF_REF)) == 0) {
1284 u_long va, stack = (u_long)&va;
1285
1286 if (!((tlb_info[tlbnext].ti_va ^ stack) & (~PGOFSET)) &&
1287 (tlb_info[tlbnext].ti_ctx == KERNEL_PID) &&
1288 (flags & TLBF_USED)) {
1289 /* Kernel stack page */
1290 flags |= TLBF_USED;
1291 tlb_info[tlbnext].ti_flags = flags;
1292 } else {
1293 /* Found it! */
1294 return (tlbnext);
1295 }
1296 } else {
1297 tlb_info[tlbnext].ti_flags = (flags & ~TLBF_REF);
1298 }
1299 }
1300 }
1301
1302 void
1303 ppc4xx_tlb_enter(int ctx, vaddr_t va, u_int pte)
1304 {
1305 u_long th, tl, idx;
1306 tlbpid_t pid;
1307 u_short msr;
1308 paddr_t pa;
1309 int s, sz;
1310
1311 tlbenter_ev.ev_count++;
1312
1313 sz = (pte & TTE_SZ_MASK) >> TTE_SZ_SHIFT;
1314 pa = (pte & TTE_RPN_MASK(sz));
1315 th = (va & TLB_EPN_MASK) | (sz << TLB_SIZE_SHFT) | TLB_VALID;
1316 tl = (pte & ~TLB_RPN_MASK) | pa;
1317 tl |= ppc4xx_tlbflags(va, pa);
1318
1319 s = splhigh();
1320 idx = ppc4xx_tlb_find_victim();
1321
1322 #ifdef DIAGNOSTIC
1323 if ((idx < TLB_NRESERVED) || (idx >= NTLB)) {
1324 panic("ppc4xx_tlb_enter: repacing entry %ld", idx);
1325 }
1326 #endif
1327
1328 tlb_info[idx].ti_va = (va & TLB_EPN_MASK);
1329 tlb_info[idx].ti_ctx = ctx;
1330 tlb_info[idx].ti_flags = TLBF_USED | TLBF_REF;
1331
1332 asm volatile(
1333 "mfmsr %0;" /* Save MSR */
1334 "li %1,0;"
1335 "tlbwe %1,%3,0;" /* Invalidate old entry. */
1336 "mtmsr %1;" /* Clear MSR */
1337 "mfpid %1;" /* Save old PID */
1338 "mtpid %2;" /* Load translation ctx */
1339 "sync; isync;"
1340 #ifdef DEBUG
1341 "andi. %3,%3,63;"
1342 "tweqi %3,0;" /* XXXXX DEBUG trap on index 0 */
1343 #endif
1344 "tlbwe %4,%3,1; tlbwe %5,%3,0;" /* Set TLB */
1345 "sync; isync;"
1346 "mtpid %1; mtmsr %0;" /* Restore PID and MSR */
1347 "sync; isync;"
1348 : "=&r" (msr), "=&r" (pid)
1349 : "r" (ctx), "r" (idx), "r" (tl), "r" (th));
1350 splx(s);
1351 }
1352
1353 void
1354 ppc4xx_tlb_unpin(int i)
1355 {
1356
1357 if (i == -1)
1358 for (i = 0; i < TLB_NRESERVED; i++)
1359 tlb_info[i].ti_flags &= ~TLBF_LOCKED;
1360 else
1361 tlb_info[i].ti_flags &= ~TLBF_LOCKED;
1362 }
1363
1364 void
1365 ppc4xx_tlb_init(void)
1366 {
1367 int i;
1368
1369 /* Mark reserved TLB entries */
1370 for (i = 0; i < TLB_NRESERVED; i++) {
1371 tlb_info[i].ti_flags = TLBF_LOCKED | TLBF_USED;
1372 tlb_info[i].ti_ctx = KERNEL_PID;
1373 }
1374
1375 /* Setup security zones */
1376 /* Z0 - accessible by kernel only if TLB entry permissions allow
1377 * Z1,Z2 - access is controlled by TLB entry permissions
1378 * Z3 - full access regardless of TLB entry permissions
1379 */
1380
1381 asm volatile(
1382 "mtspr %0,%1;"
1383 "sync;"
1384 :: "K"(SPR_ZPR), "r" (0x1b000000));
1385 }
1386
1387
1388 /*
1389 * We should pass the ctx in from trap code.
1390 */
1391 int
1392 pmap_tlbmiss(vaddr_t va, int ctx)
1393 {
1394 volatile u_int *pte;
1395 u_long tte;
1396
1397 tlbmiss_ev.ev_count++;
1398
1399 /*
1400 * XXXX We will reserve 0-0x80000000 for va==pa mappings.
1401 */
1402 if (ctx != KERNEL_PID || (va & 0x80000000)) {
1403 pte = pte_find((struct pmap *)ctxbusy[ctx], va);
1404 if (pte == NULL) {
1405 /* Map unmanaged addresses directly for kernel access */
1406 return 1;
1407 }
1408 tte = *pte;
1409 if (tte == 0) {
1410 return 1;
1411 }
1412 } else {
1413 /* Create a 16MB writable mapping. */
1414 #ifdef PPC_4XX_NOCACHE
1415 tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_I | TTE_WR;
1416 #else
1417 tte = TTE_PA(va) | TTE_ZONE(ZONE_PRIV) | TTE_SZ_16M | TTE_WR;
1418 #endif
1419 }
1420 tlbhit_ev.ev_count++;
1421 ppc4xx_tlb_enter(ctx, va, tte);
1422
1423 return 0;
1424 }
1425
1426 /*
1427 * Flush all the entries matching a context from the TLB.
1428 */
1429 static int
1430 ctx_flush(int cnum)
1431 {
1432 int i;
1433
1434 /* We gotta steal this context */
1435 for (i = TLB_NRESERVED; i < NTLB; i++) {
1436 if (tlb_info[i].ti_ctx == cnum) {
1437 /* Can't steal ctx if it has a locked entry. */
1438 if (TLB_LOCKED(i)) {
1439 #ifdef DIAGNOSTIC
1440 printf("ctx_flush: can't invalidate "
1441 "locked mapping %d "
1442 "for context %d\n", i, cnum);
1443 #ifdef DDB
1444 Debugger();
1445 #endif
1446 #endif
1447 return (1);
1448 }
1449 #ifdef DIAGNOSTIC
1450 if (i < TLB_NRESERVED)
1451 panic("TLB entry %d not locked", i);
1452 #endif
1453 /* Invalidate particular TLB entry regardless of locked status */
1454 asm volatile("tlbwe %0,%1,0" : :"r"(0),"r"(i));
1455 tlb_info[i].ti_flags = 0;
1456 }
1457 }
1458 return (0);
1459 }
1460
1461 /*
1462 * Allocate a context. If necessary, steal one from someone else.
1463 *
1464 * The new context is flushed from the TLB before returning.
1465 */
1466 int
1467 ctx_alloc(struct pmap *pm)
1468 {
1469 int s, cnum;
1470 static int next = MINCTX;
1471
1472 if (pm == pmap_kernel()) {
1473 #ifdef DIAGNOSTIC
1474 printf("ctx_alloc: kernel pmap!\n");
1475 #endif
1476 return (0);
1477 }
1478 s = splvm();
1479
1480 /* Find a likely context. */
1481 cnum = next;
1482 do {
1483 if ((++cnum) > NUMCTX)
1484 cnum = MINCTX;
1485 } while (ctxbusy[cnum] != NULL && cnum != next);
1486
1487 /* Now clean it out */
1488 oops:
1489 if (cnum < MINCTX)
1490 cnum = MINCTX; /* Never steal ctx 0 or 1 */
1491 if (ctx_flush(cnum)) {
1492 /* oops -- something's wired. */
1493 if ((++cnum) > NUMCTX)
1494 cnum = MINCTX;
1495 goto oops;
1496 }
1497
1498 if (ctxbusy[cnum]) {
1499 #ifdef DEBUG
1500 /* We should identify this pmap and clear it */
1501 printf("Warning: stealing context %d\n", cnum);
1502 #endif
1503 ctxbusy[cnum]->pm_ctx = 0;
1504 }
1505 ctxbusy[cnum] = pm;
1506 next = cnum;
1507 splx(s);
1508 pm->pm_ctx = cnum;
1509
1510 return cnum;
1511 }
1512
1513 /*
1514 * Give away a context.
1515 */
1516 void
1517 ctx_free(struct pmap *pm)
1518 {
1519 int oldctx;
1520
1521 oldctx = pm->pm_ctx;
1522
1523 if (oldctx == 0)
1524 panic("ctx_free: freeing kernel context");
1525 #ifdef DIAGNOSTIC
1526 if (ctxbusy[oldctx] == 0)
1527 printf("ctx_free: freeing free context %d\n", oldctx);
1528 if (ctxbusy[oldctx] != pm) {
1529 printf("ctx_free: freeing someone esle's context\n "
1530 "ctxbusy[%d] = %p, pm->pm_ctx = %p\n",
1531 oldctx, (void *)(u_long)ctxbusy[oldctx], pm);
1532 #ifdef DDB
1533 Debugger();
1534 #endif
1535 }
1536 #endif
1537 /* We should verify it has not been stolen and reallocated... */
1538 ctxbusy[oldctx] = NULL;
1539 ctx_flush(oldctx);
1540 }
1541
1542
1543 #ifdef DEBUG
1544 /*
1545 * Test ref/modify handling.
1546 */
1547 void pmap_testout __P((void));
1548 void
1549 pmap_testout()
1550 {
1551 vaddr_t va;
1552 volatile int *loc;
1553 int val = 0;
1554 paddr_t pa;
1555 struct vm_page *pg;
1556 int ref, mod;
1557
1558 /* Allocate a page */
1559 va = (vaddr_t)uvm_km_alloc1(kernel_map, PAGE_SIZE, 1);
1560 loc = (int*)va;
1561
1562 pmap_extract(pmap_kernel(), va, &pa);
1563 pg = PHYS_TO_VM_PAGE(pa);
1564 pmap_unwire(pmap_kernel(), va);
1565
1566 pmap_remove(pmap_kernel(), va, va+1);
1567 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1568 pmap_update(pmap_kernel());
1569
1570 /* Now clear reference and modify */
1571 ref = pmap_clear_reference(pg);
1572 mod = pmap_clear_modify(pg);
1573 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1574 (void *)(u_long)va, (long)pa,
1575 ref, mod);
1576
1577 /* Check it's properly cleared */
1578 ref = pmap_is_referenced(pg);
1579 mod = pmap_is_modified(pg);
1580 printf("Checking cleared page: ref %d, mod %d\n",
1581 ref, mod);
1582
1583 /* Reference page */
1584 val = *loc;
1585
1586 ref = pmap_is_referenced(pg);
1587 mod = pmap_is_modified(pg);
1588 printf("Referenced page: ref %d, mod %d val %x\n",
1589 ref, mod, val);
1590
1591 /* Now clear reference and modify */
1592 ref = pmap_clear_reference(pg);
1593 mod = pmap_clear_modify(pg);
1594 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1595 (void *)(u_long)va, (long)pa,
1596 ref, mod);
1597
1598 /* Modify page */
1599 *loc = 1;
1600
1601 ref = pmap_is_referenced(pg);
1602 mod = pmap_is_modified(pg);
1603 printf("Modified page: ref %d, mod %d\n",
1604 ref, mod);
1605
1606 /* Now clear reference and modify */
1607 ref = pmap_clear_reference(pg);
1608 mod = pmap_clear_modify(pg);
1609 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1610 (void *)(u_long)va, (long)pa,
1611 ref, mod);
1612
1613 /* Check it's properly cleared */
1614 ref = pmap_is_referenced(pg);
1615 mod = pmap_is_modified(pg);
1616 printf("Checking cleared page: ref %d, mod %d\n",
1617 ref, mod);
1618
1619 /* Modify page */
1620 *loc = 1;
1621
1622 ref = pmap_is_referenced(pg);
1623 mod = pmap_is_modified(pg);
1624 printf("Modified page: ref %d, mod %d\n",
1625 ref, mod);
1626
1627 /* Check pmap_protect() */
1628 pmap_protect(pmap_kernel(), va, va+1, VM_PROT_READ);
1629 pmap_update(pmap_kernel());
1630 ref = pmap_is_referenced(pg);
1631 mod = pmap_is_modified(pg);
1632 printf("pmap_protect(VM_PROT_READ): ref %d, mod %d\n",
1633 ref, mod);
1634
1635 /* Now clear reference and modify */
1636 ref = pmap_clear_reference(pg);
1637 mod = pmap_clear_modify(pg);
1638 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1639 (void *)(u_long)va, (long)pa,
1640 ref, mod);
1641
1642 /* Reference page */
1643 val = *loc;
1644
1645 ref = pmap_is_referenced(pg);
1646 mod = pmap_is_modified(pg);
1647 printf("Referenced page: ref %d, mod %d val %x\n",
1648 ref, mod, val);
1649
1650 /* Now clear reference and modify */
1651 ref = pmap_clear_reference(pg);
1652 mod = pmap_clear_modify(pg);
1653 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1654 (void *)(u_long)va, (long)pa,
1655 ref, mod);
1656
1657 /* Modify page */
1658 #if 0
1659 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1660 pmap_update(pmap_kernel());
1661 #endif
1662 *loc = 1;
1663
1664 ref = pmap_is_referenced(pg);
1665 mod = pmap_is_modified(pg);
1666 printf("Modified page: ref %d, mod %d\n",
1667 ref, mod);
1668
1669 /* Check pmap_protect() */
1670 pmap_protect(pmap_kernel(), va, va+1, VM_PROT_NONE);
1671 pmap_update(pmap_kernel());
1672 ref = pmap_is_referenced(pg);
1673 mod = pmap_is_modified(pg);
1674 printf("pmap_protect(): ref %d, mod %d\n",
1675 ref, mod);
1676
1677 /* Now clear reference and modify */
1678 ref = pmap_clear_reference(pg);
1679 mod = pmap_clear_modify(pg);
1680 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1681 (void *)(u_long)va, (long)pa,
1682 ref, mod);
1683
1684 /* Reference page */
1685 val = *loc;
1686
1687 ref = pmap_is_referenced(pg);
1688 mod = pmap_is_modified(pg);
1689 printf("Referenced page: ref %d, mod %d val %x\n",
1690 ref, mod, val);
1691
1692 /* Now clear reference and modify */
1693 ref = pmap_clear_reference(pg);
1694 mod = pmap_clear_modify(pg);
1695 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1696 (void *)(u_long)va, (long)pa,
1697 ref, mod);
1698
1699 /* Modify page */
1700 #if 0
1701 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1702 pmap_update(pmap_kernel());
1703 #endif
1704 *loc = 1;
1705
1706 ref = pmap_is_referenced(pg);
1707 mod = pmap_is_modified(pg);
1708 printf("Modified page: ref %d, mod %d\n",
1709 ref, mod);
1710
1711 /* Check pmap_pag_protect() */
1712 pmap_page_protect(pg, VM_PROT_READ);
1713 ref = pmap_is_referenced(pg);
1714 mod = pmap_is_modified(pg);
1715 printf("pmap_page_protect(VM_PROT_READ): ref %d, mod %d\n",
1716 ref, mod);
1717
1718 /* Now clear reference and modify */
1719 ref = pmap_clear_reference(pg);
1720 mod = pmap_clear_modify(pg);
1721 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1722 (void *)(u_long)va, (long)pa,
1723 ref, mod);
1724
1725 /* Reference page */
1726 val = *loc;
1727
1728 ref = pmap_is_referenced(pg);
1729 mod = pmap_is_modified(pg);
1730 printf("Referenced page: ref %d, mod %d val %x\n",
1731 ref, mod, val);
1732
1733 /* Now clear reference and modify */
1734 ref = pmap_clear_reference(pg);
1735 mod = pmap_clear_modify(pg);
1736 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1737 (void *)(u_long)va, (long)pa,
1738 ref, mod);
1739
1740 /* Modify page */
1741 #if 0
1742 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1743 pmap_update(pmap_kernel());
1744 #endif
1745 *loc = 1;
1746
1747 ref = pmap_is_referenced(pg);
1748 mod = pmap_is_modified(pg);
1749 printf("Modified page: ref %d, mod %d\n",
1750 ref, mod);
1751
1752 /* Check pmap_pag_protect() */
1753 pmap_page_protect(pg, VM_PROT_NONE);
1754 ref = pmap_is_referenced(pg);
1755 mod = pmap_is_modified(pg);
1756 printf("pmap_page_protect(): ref %d, mod %d\n",
1757 ref, mod);
1758
1759 /* Now clear reference and modify */
1760 ref = pmap_clear_reference(pg);
1761 mod = pmap_clear_modify(pg);
1762 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1763 (void *)(u_long)va, (long)pa,
1764 ref, mod);
1765
1766
1767 /* Reference page */
1768 val = *loc;
1769
1770 ref = pmap_is_referenced(pg);
1771 mod = pmap_is_modified(pg);
1772 printf("Referenced page: ref %d, mod %d val %x\n",
1773 ref, mod, val);
1774
1775 /* Now clear reference and modify */
1776 ref = pmap_clear_reference(pg);
1777 mod = pmap_clear_modify(pg);
1778 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1779 (void *)(u_long)va, (long)pa,
1780 ref, mod);
1781
1782 /* Modify page */
1783 #if 0
1784 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL, 0);
1785 pmap_update(pmap_kernel());
1786 #endif
1787 *loc = 1;
1788
1789 ref = pmap_is_referenced(pg);
1790 mod = pmap_is_modified(pg);
1791 printf("Modified page: ref %d, mod %d\n",
1792 ref, mod);
1793
1794 /* Unmap page */
1795 pmap_remove(pmap_kernel(), va, va+1);
1796 pmap_update(pmap_kernel());
1797 ref = pmap_is_referenced(pg);
1798 mod = pmap_is_modified(pg);
1799 printf("Unmapped page: ref %d, mod %d\n", ref, mod);
1800
1801 /* Now clear reference and modify */
1802 ref = pmap_clear_reference(pg);
1803 mod = pmap_clear_modify(pg);
1804 printf("Clearing page va %p pa %lx: ref %d, mod %d\n",
1805 (void *)(u_long)va, (long)pa, ref, mod);
1806
1807 /* Check it's properly cleared */
1808 ref = pmap_is_referenced(pg);
1809 mod = pmap_is_modified(pg);
1810 printf("Checking cleared page: ref %d, mod %d\n",
1811 ref, mod);
1812
1813 pmap_enter(pmap_kernel(), va, pa, VM_PROT_ALL,
1814 VM_PROT_ALL|PMAP_WIRED);
1815 uvm_km_free(kernel_map, (vaddr_t)va, PAGE_SIZE);
1816 }
1817 #endif
1818