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