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