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