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