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