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