pmap.c revision 1.55 1 /* $NetBSD: pmap.c,v 1.55 2000/10/21 14:10:25 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jeremy Cooper.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * XXX These comments aren't quite accurate. Need to change.
41 * The sun3x uses the MC68851 Memory Management Unit, which is built
42 * into the CPU. The 68851 maps virtual to physical addresses using
43 * a multi-level table lookup, which is stored in the very memory that
44 * it maps. The number of levels of lookup is configurable from one
45 * to four. In this implementation, we use three, named 'A' through 'C'.
46 *
47 * The MMU translates virtual addresses into physical addresses by
48 * traversing these tables in a proccess called a 'table walk'. The most
49 * significant 7 bits of the Virtual Address ('VA') being translated are
50 * used as an index into the level A table, whose base in physical memory
51 * is stored in a special MMU register, the 'CPU Root Pointer' or CRP. The
52 * address found at that index in the A table is used as the base
53 * address for the next table, the B table. The next six bits of the VA are
54 * used as an index into the B table, which in turn gives the base address
55 * of the third and final C table.
56 *
57 * The next six bits of the VA are used as an index into the C table to
58 * locate a Page Table Entry (PTE). The PTE is a physical address in memory
59 * to which the remaining 13 bits of the VA are added, producing the
60 * mapped physical address.
61 *
62 * To map the entire memory space in this manner would require 2114296 bytes
63 * of page tables per process - quite expensive. Instead we will
64 * allocate a fixed but considerably smaller space for the page tables at
65 * the time the VM system is initialized. When the pmap code is asked by
66 * the kernel to map a VA to a PA, it allocates tables as needed from this
67 * pool. When there are no more tables in the pool, tables are stolen
68 * from the oldest mapped entries in the tree. This is only possible
69 * because all memory mappings are stored in the kernel memory map
70 * structures, independent of the pmap structures. A VA which references
71 * one of these invalidated maps will cause a page fault. The kernel
72 * will determine that the page fault was caused by a task using a valid
73 * VA, but for some reason (which does not concern it), that address was
74 * not mapped. It will ask the pmap code to re-map the entry and then
75 * it will resume executing the faulting task.
76 *
77 * In this manner the most efficient use of the page table space is
78 * achieved. Tasks which do not execute often will have their tables
79 * stolen and reused by tasks which execute more frequently. The best
80 * size for the page table pool will probably be determined by
81 * experimentation.
82 *
83 * You read all of the comments so far. Good for you.
84 * Now go play!
85 */
86
87 /*** A Note About the 68851 Address Translation Cache
88 * The MC68851 has a 64 entry cache, called the Address Translation Cache
89 * or 'ATC'. This cache stores the most recently used page descriptors
90 * accessed by the MMU when it does translations. Using a marker called a
91 * 'task alias' the MMU can store the descriptors from 8 different table
92 * spaces concurrently. The task alias is associated with the base
93 * address of the level A table of that address space. When an address
94 * space is currently active (the CRP currently points to its A table)
95 * the only cached descriptors that will be obeyed are ones which have a
96 * matching task alias of the current space associated with them.
97 *
98 * Since the cache is always consulted before any table lookups are done,
99 * it is important that it accurately reflect the state of the MMU tables.
100 * Whenever a change has been made to a table that has been loaded into
101 * the MMU, the code must be sure to flush any cached entries that are
102 * affected by the change. These instances are documented in the code at
103 * various points.
104 */
105 /*** A Note About the Note About the 68851 Address Translation Cache
106 * 4 months into this code I discovered that the sun3x does not have
107 * a MC68851 chip. Instead, it has a version of this MMU that is part of the
108 * the 68030 CPU.
109 * All though it behaves very similarly to the 68851, it only has 1 task
110 * alias and a 22 entry cache. So sadly (or happily), the first paragraph
111 * of the previous note does not apply to the sun3x pmap.
112 */
113
114 #include "opt_ddb.h"
115
116 #include <sys/param.h>
117 #include <sys/systm.h>
118 #include <sys/proc.h>
119 #include <sys/malloc.h>
120 #include <sys/user.h>
121 #include <sys/queue.h>
122 #include <sys/kcore.h>
123
124 #include <uvm/uvm.h>
125
126 #define PAGER_SVA (uvm.pager_sva)
127 #define PAGER_EVA (uvm.pager_eva)
128
129 #include <machine/cpu.h>
130 #include <machine/kcore.h>
131 #include <machine/mon.h>
132 #include <machine/pmap.h>
133 #include <machine/pte.h>
134 #include <machine/vmparam.h>
135
136 #include <sun3/sun3/cache.h>
137 #include <sun3/sun3/machdep.h>
138
139 #include "pmap_pvt.h"
140
141 /* XXX - What headers declare these? */
142 extern struct pcb *curpcb;
143 extern int physmem;
144
145 extern void copypage __P((const void*, void*));
146 extern void zeropage __P((void*));
147
148 /* Defined in locore.s */
149 extern char kernel_text[];
150
151 /* Defined by the linker */
152 extern char etext[], edata[], end[];
153 extern char *esym; /* DDB */
154
155 /*************************** DEBUGGING DEFINITIONS ***********************
156 * Macros, preprocessor defines and variables used in debugging can make *
157 * code hard to read. Anything used exclusively for debugging purposes *
158 * is defined here to avoid having such mess scattered around the file. *
159 *************************************************************************/
160 #ifdef PMAP_DEBUG
161 /*
162 * To aid the debugging process, macros should be expanded into smaller steps
163 * that accomplish the same goal, yet provide convenient places for placing
164 * breakpoints. When this code is compiled with PMAP_DEBUG mode defined, the
165 * 'INLINE' keyword is defined to an empty string. This way, any function
166 * defined to be a 'static INLINE' will become 'outlined' and compiled as
167 * a separate function, which is much easier to debug.
168 */
169 #define INLINE /* nothing */
170
171 /*
172 * It is sometimes convenient to watch the activity of a particular table
173 * in the system. The following variables are used for that purpose.
174 */
175 a_tmgr_t *pmap_watch_atbl = 0;
176 b_tmgr_t *pmap_watch_btbl = 0;
177 c_tmgr_t *pmap_watch_ctbl = 0;
178
179 int pmap_debug = 0;
180 #define DPRINT(args) if (pmap_debug) printf args
181
182 #else /********** Stuff below is defined if NOT debugging **************/
183
184 #define INLINE inline
185 #define DPRINT(args) /* nada */
186
187 #endif /* PMAP_DEBUG */
188 /*********************** END OF DEBUGGING DEFINITIONS ********************/
189
190 /*** Management Structure - Memory Layout
191 * For every MMU table in the sun3x pmap system there must be a way to
192 * manage it; we must know which process is using it, what other tables
193 * depend on it, and whether or not it contains any locked pages. This
194 * is solved by the creation of 'table management' or 'tmgr'
195 * structures. One for each MMU table in the system.
196 *
197 * MAP OF MEMORY USED BY THE PMAP SYSTEM
198 *
199 * towards lower memory
200 * kernAbase -> +-------------------------------------------------------+
201 * | Kernel MMU A level table |
202 * kernBbase -> +-------------------------------------------------------+
203 * | Kernel MMU B level tables |
204 * kernCbase -> +-------------------------------------------------------+
205 * | |
206 * | Kernel MMU C level tables |
207 * | |
208 * mmuCbase -> +-------------------------------------------------------+
209 * | User MMU C level tables |
210 * mmuAbase -> +-------------------------------------------------------+
211 * | |
212 * | User MMU A level tables |
213 * | |
214 * mmuBbase -> +-------------------------------------------------------+
215 * | User MMU B level tables |
216 * tmgrAbase -> +-------------------------------------------------------+
217 * | TMGR A level table structures |
218 * tmgrBbase -> +-------------------------------------------------------+
219 * | TMGR B level table structures |
220 * tmgrCbase -> +-------------------------------------------------------+
221 * | TMGR C level table structures |
222 * pvbase -> +-------------------------------------------------------+
223 * | Physical to Virtual mapping table (list heads) |
224 * pvebase -> +-------------------------------------------------------+
225 * | Physical to Virtual mapping table (list elements) |
226 * | |
227 * +-------------------------------------------------------+
228 * towards higher memory
229 *
230 * For every A table in the MMU A area, there will be a corresponding
231 * a_tmgr structure in the TMGR A area. The same will be true for
232 * the B and C tables. This arrangement will make it easy to find the
233 * controling tmgr structure for any table in the system by use of
234 * (relatively) simple macros.
235 */
236
237 /*
238 * Global variables for storing the base addresses for the areas
239 * labeled above.
240 */
241 static vm_offset_t kernAphys;
242 static mmu_long_dte_t *kernAbase;
243 static mmu_short_dte_t *kernBbase;
244 static mmu_short_pte_t *kernCbase;
245 static mmu_short_pte_t *mmuCbase;
246 static mmu_short_dte_t *mmuBbase;
247 static mmu_long_dte_t *mmuAbase;
248 static a_tmgr_t *Atmgrbase;
249 static b_tmgr_t *Btmgrbase;
250 static c_tmgr_t *Ctmgrbase;
251 static pv_t *pvbase;
252 static pv_elem_t *pvebase;
253 struct pmap kernel_pmap;
254
255 /*
256 * This holds the CRP currently loaded into the MMU.
257 */
258 struct mmu_rootptr kernel_crp;
259
260 /*
261 * Just all around global variables.
262 */
263 static TAILQ_HEAD(a_pool_head_struct, a_tmgr_struct) a_pool;
264 static TAILQ_HEAD(b_pool_head_struct, b_tmgr_struct) b_pool;
265 static TAILQ_HEAD(c_pool_head_struct, c_tmgr_struct) c_pool;
266
267
268 /*
269 * Flags used to mark the safety/availability of certain operations or
270 * resources.
271 */
272 static boolean_t pv_initialized = FALSE, /* PV system has been initialized. */
273 bootstrap_alloc_enabled = FALSE; /*Safe to use pmap_bootstrap_alloc().*/
274 int tmp_vpages_inuse; /* Temporary virtual pages are in use */
275
276 /*
277 * XXX: For now, retain the traditional variables that were
278 * used in the old pmap/vm interface (without NONCONTIG).
279 */
280 /* Kernel virtual address space available: */
281 vm_offset_t virtual_avail, virtual_end;
282 /* Physical address space available: */
283 vm_offset_t avail_start, avail_end;
284
285 /* This keep track of the end of the contiguously mapped range. */
286 vm_offset_t virtual_contig_end;
287
288 /* Physical address used by pmap_next_page() */
289 vm_offset_t avail_next;
290
291 /* These are used by pmap_copy_page(), etc. */
292 vm_offset_t tmp_vpages[2];
293
294 /*
295 * The 3/80 is the only member of the sun3x family that has non-contiguous
296 * physical memory. Memory is divided into 4 banks which are physically
297 * locatable on the system board. Although the size of these banks varies
298 * with the size of memory they contain, their base addresses are
299 * permenently fixed. The following structure, which describes these
300 * banks, is initialized by pmap_bootstrap() after it reads from a similar
301 * structure provided by the ROM Monitor.
302 *
303 * For the other machines in the sun3x architecture which do have contiguous
304 * RAM, this list will have only one entry, which will describe the entire
305 * range of available memory.
306 */
307 struct pmap_physmem_struct avail_mem[SUN3X_NPHYS_RAM_SEGS];
308 u_int total_phys_mem;
309
310 /*************************************************************************/
311
312 /*
313 * XXX - Should "tune" these based on statistics.
314 *
315 * My first guess about the relative numbers of these needed is
316 * based on the fact that a "typical" process will have several
317 * pages mapped at low virtual addresses (text, data, bss), then
318 * some mapped shared libraries, and then some stack pages mapped
319 * near the high end of the VA space. Each process can use only
320 * one A table, and most will use only two B tables (maybe three)
321 * and probably about four C tables. Therefore, the first guess
322 * at the relative numbers of these needed is 1:2:4 -gwr
323 *
324 * The number of C tables needed is closely related to the amount
325 * of physical memory available plus a certain amount attributable
326 * to the use of double mappings. With a few simulation statistics
327 * we can find a reasonably good estimation of this unknown value.
328 * Armed with that and the above ratios, we have a good idea of what
329 * is needed at each level. -j
330 *
331 * Note: It is not physical memory memory size, but the total mapped
332 * virtual space required by the combined working sets of all the
333 * currently _runnable_ processes. (Sleeping ones don't count.)
334 * The amount of physical memory should be irrelevant. -gwr
335 */
336 #ifdef FIXED_NTABLES
337 #define NUM_A_TABLES 16
338 #define NUM_B_TABLES 32
339 #define NUM_C_TABLES 64
340 #else
341 unsigned int NUM_A_TABLES, NUM_B_TABLES, NUM_C_TABLES;
342 #endif /* FIXED_NTABLES */
343
344 /*
345 * This determines our total virtual mapping capacity.
346 * Yes, it is a FIXED value so we can pre-allocate.
347 */
348 #define NUM_USER_PTES (NUM_C_TABLES * MMU_C_TBL_SIZE)
349
350 /*
351 * The size of the Kernel Virtual Address Space (KVAS)
352 * for purposes of MMU table allocation is -KERNBASE
353 * (length from KERNBASE to 0xFFFFffff)
354 */
355 #define KVAS_SIZE (-KERNBASE)
356
357 /* Numbers of kernel MMU tables to support KVAS_SIZE. */
358 #define KERN_B_TABLES (KVAS_SIZE >> MMU_TIA_SHIFT)
359 #define KERN_C_TABLES (KVAS_SIZE >> MMU_TIB_SHIFT)
360 #define NUM_KERN_PTES (KVAS_SIZE >> MMU_TIC_SHIFT)
361
362 /*************************** MISCELANEOUS MACROS *************************/
363 #define pmap_lock(pmap) simple_lock(&pmap->pm_lock)
364 #define pmap_unlock(pmap) simple_unlock(&pmap->pm_lock)
365 #define pmap_add_ref(pmap) ++pmap->pm_refcount
366 #define pmap_del_ref(pmap) --pmap->pm_refcount
367 #define pmap_refcount(pmap) pmap->pm_refcount
368
369 static INLINE void * mmu_ptov __P((vm_offset_t pa));
370 static INLINE vm_offset_t mmu_vtop __P((void * va));
371
372 #if 0
373 static INLINE a_tmgr_t * mmuA2tmgr __P((mmu_long_dte_t *));
374 #endif /* 0 */
375 static INLINE b_tmgr_t * mmuB2tmgr __P((mmu_short_dte_t *));
376 static INLINE c_tmgr_t * mmuC2tmgr __P((mmu_short_pte_t *));
377
378 static INLINE pv_t *pa2pv __P((vm_offset_t pa));
379 static INLINE int pteidx __P((mmu_short_pte_t *));
380 static INLINE pmap_t current_pmap __P((void));
381
382 /*
383 * We can always convert between virtual and physical addresses
384 * for anything in the range [KERNBASE ... avail_start] because
385 * that range is GUARANTEED to be mapped linearly.
386 * We rely heavily upon this feature!
387 */
388 static INLINE void *
389 mmu_ptov(pa)
390 vm_offset_t pa;
391 {
392 register vm_offset_t va;
393
394 va = (pa + KERNBASE);
395 #ifdef PMAP_DEBUG
396 if ((va < KERNBASE) || (va >= virtual_contig_end))
397 panic("mmu_ptov");
398 #endif
399 return ((void*)va);
400 }
401 static INLINE vm_offset_t
402 mmu_vtop(vva)
403 void *vva;
404 {
405 register vm_offset_t va;
406
407 va = (vm_offset_t)vva;
408 #ifdef PMAP_DEBUG
409 if ((va < KERNBASE) || (va >= virtual_contig_end))
410 panic("mmu_ptov");
411 #endif
412 return (va - KERNBASE);
413 }
414
415 /*
416 * These macros map MMU tables to their corresponding manager structures.
417 * They are needed quite often because many of the pointers in the pmap
418 * system reference MMU tables and not the structures that control them.
419 * There needs to be a way to find one when given the other and these
420 * macros do so by taking advantage of the memory layout described above.
421 * Here's a quick step through the first macro, mmuA2tmgr():
422 *
423 * 1) find the offset of the given MMU A table from the base of its table
424 * pool (table - mmuAbase).
425 * 2) convert this offset into a table index by dividing it by the
426 * size of one MMU 'A' table. (sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE)
427 * 3) use this index to select the corresponding 'A' table manager
428 * structure from the 'A' table manager pool (Atmgrbase[index]).
429 */
430 /* This function is not currently used. */
431 #if 0
432 static INLINE a_tmgr_t *
433 mmuA2tmgr(mmuAtbl)
434 mmu_long_dte_t *mmuAtbl;
435 {
436 register int idx;
437
438 /* Which table is this in? */
439 idx = (mmuAtbl - mmuAbase) / MMU_A_TBL_SIZE;
440 #ifdef PMAP_DEBUG
441 if ((idx < 0) || (idx >= NUM_A_TABLES))
442 panic("mmuA2tmgr");
443 #endif
444 return (&Atmgrbase[idx]);
445 }
446 #endif /* 0 */
447
448 static INLINE b_tmgr_t *
449 mmuB2tmgr(mmuBtbl)
450 mmu_short_dte_t *mmuBtbl;
451 {
452 register int idx;
453
454 /* Which table is this in? */
455 idx = (mmuBtbl - mmuBbase) / MMU_B_TBL_SIZE;
456 #ifdef PMAP_DEBUG
457 if ((idx < 0) || (idx >= NUM_B_TABLES))
458 panic("mmuB2tmgr");
459 #endif
460 return (&Btmgrbase[idx]);
461 }
462
463 /* mmuC2tmgr INTERNAL
464 **
465 * Given a pte known to belong to a C table, return the address of
466 * that table's management structure.
467 */
468 static INLINE c_tmgr_t *
469 mmuC2tmgr(mmuCtbl)
470 mmu_short_pte_t *mmuCtbl;
471 {
472 register int idx;
473
474 /* Which table is this in? */
475 idx = (mmuCtbl - mmuCbase) / MMU_C_TBL_SIZE;
476 #ifdef PMAP_DEBUG
477 if ((idx < 0) || (idx >= NUM_C_TABLES))
478 panic("mmuC2tmgr");
479 #endif
480 return (&Ctmgrbase[idx]);
481 }
482
483 /* This is now a function call below.
484 * #define pa2pv(pa) \
485 * (&pvbase[(unsigned long)\
486 * m68k_btop(pa)\
487 * ])
488 */
489
490 /* pa2pv INTERNAL
491 **
492 * Return the pv_list_head element which manages the given physical
493 * address.
494 */
495 static INLINE pv_t *
496 pa2pv(pa)
497 vm_offset_t pa;
498 {
499 register struct pmap_physmem_struct *bank;
500 register int idx;
501
502 bank = &avail_mem[0];
503 while (pa >= bank->pmem_end)
504 bank = bank->pmem_next;
505
506 pa -= bank->pmem_start;
507 idx = bank->pmem_pvbase + m68k_btop(pa);
508 #ifdef PMAP_DEBUG
509 if ((idx < 0) || (idx >= physmem))
510 panic("pa2pv");
511 #endif
512 return &pvbase[idx];
513 }
514
515 /* pteidx INTERNAL
516 **
517 * Return the index of the given PTE within the entire fixed table of
518 * PTEs.
519 */
520 static INLINE int
521 pteidx(pte)
522 mmu_short_pte_t *pte;
523 {
524 return (pte - kernCbase);
525 }
526
527 /*
528 * This just offers a place to put some debugging checks,
529 * and reduces the number of places "curproc" appears...
530 */
531 static INLINE pmap_t
532 current_pmap()
533 {
534 struct proc *p;
535 struct vmspace *vm;
536 vm_map_t map;
537 pmap_t pmap;
538
539 p = curproc; /* XXX */
540 if (p == NULL)
541 pmap = &kernel_pmap;
542 else {
543 vm = p->p_vmspace;
544 map = &vm->vm_map;
545 pmap = vm_map_pmap(map);
546 }
547
548 return (pmap);
549 }
550
551
552 /*************************** FUNCTION DEFINITIONS ************************
553 * These appear here merely for the compiler to enforce type checking on *
554 * all function calls. *
555 *************************************************************************/
556
557 /** External functions
558 ** - functions used within this module but written elsewhere.
559 ** both of these functions are in locore.s
560 ** XXX - These functions were later replaced with their more cryptic
561 ** hp300 counterparts. They may be removed now.
562 **/
563 #if 0 /* deprecated mmu */
564 void mmu_seturp __P((vm_offset_t));
565 void mmu_flush __P((int, vm_offset_t));
566 void mmu_flusha __P((void));
567 #endif /* 0 */
568
569 /** Internal functions
570 ** Most functions used only within this module are defined in
571 ** pmap_pvt.h (why not here if used only here?)
572 **/
573 static void pmap_page_upload __P((void));
574
575 /** Interface functions
576 ** - functions required by the Mach VM Pmap interface, with MACHINE_CONTIG
577 ** defined.
578 **/
579 int pmap_page_index __P((vm_offset_t));
580 void pmap_pinit __P((pmap_t));
581 void pmap_release __P((pmap_t));
582
583 /********************************** CODE ********************************
584 * Functions that are called from other parts of the kernel are labeled *
585 * as 'INTERFACE' functions. Functions that are only called from *
586 * within the pmap module are labeled as 'INTERNAL' functions. *
587 * Functions that are internal, but are not (currently) used at all are *
588 * labeled 'INTERNAL_X'. *
589 ************************************************************************/
590
591 /* pmap_bootstrap INTERNAL
592 **
593 * Initializes the pmap system. Called at boot time from
594 * locore2.c:_vm_init()
595 *
596 * Reminder: having a pmap_bootstrap_alloc() and also having the VM
597 * system implement pmap_steal_memory() is redundant.
598 * Don't release this code without removing one or the other!
599 */
600 void
601 pmap_bootstrap(nextva)
602 vm_offset_t nextva;
603 {
604 struct physmemory *membank;
605 struct pmap_physmem_struct *pmap_membank;
606 vm_offset_t va, pa, eva;
607 int b, c, i, j; /* running table counts */
608 int size, resvmem;
609
610 /*
611 * This function is called by __bootstrap after it has
612 * determined the type of machine and made the appropriate
613 * patches to the ROM vectors (XXX- I don't quite know what I meant
614 * by that.) It allocates and sets up enough of the pmap system
615 * to manage the kernel's address space.
616 */
617
618 /*
619 * Determine the range of kernel virtual and physical
620 * space available. Note that we ABSOLUTELY DEPEND on
621 * the fact that the first bank of memory (4MB) is
622 * mapped linearly to KERNBASE (which we guaranteed in
623 * the first instructions of locore.s).
624 * That is plenty for our bootstrap work.
625 */
626 virtual_avail = m68k_round_page(nextva);
627 virtual_contig_end = KERNBASE + 0x400000; /* +4MB */
628 virtual_end = VM_MAX_KERNEL_ADDRESS;
629 /* Don't need avail_start til later. */
630
631 /* We may now call pmap_bootstrap_alloc(). */
632 bootstrap_alloc_enabled = TRUE;
633
634 /*
635 * This is a somewhat unwrapped loop to deal with
636 * copying the PROM's 'phsymem' banks into the pmap's
637 * banks. The following is always assumed:
638 * 1. There is always at least one bank of memory.
639 * 2. There is always a last bank of memory, and its
640 * pmem_next member must be set to NULL.
641 */
642 membank = romVectorPtr->v_physmemory;
643 pmap_membank = avail_mem;
644 total_phys_mem = 0;
645
646 for (;;) { /* break on !membank */
647 pmap_membank->pmem_start = membank->address;
648 pmap_membank->pmem_end = membank->address + membank->size;
649 total_phys_mem += membank->size;
650 membank = membank->next;
651 if (!membank)
652 break;
653 /* This silly syntax arises because pmap_membank
654 * is really a pre-allocated array, but it is put into
655 * use as a linked list.
656 */
657 pmap_membank->pmem_next = pmap_membank + 1;
658 pmap_membank = pmap_membank->pmem_next;
659 }
660 /* This is the last element. */
661 pmap_membank->pmem_next = NULL;
662
663 /*
664 * Note: total_phys_mem, physmem represent
665 * actual physical memory, including that
666 * reserved for the PROM monitor.
667 */
668 physmem = btoc(total_phys_mem);
669
670 /*
671 * The last bank of memory should be reduced to prevent the
672 * physical pages needed by the PROM monitor from being used
673 * in the VM system.
674 */
675 resvmem = total_phys_mem - *(romVectorPtr->memoryAvail);
676 resvmem = m68k_round_page(resvmem);
677 pmap_membank->pmem_end -= resvmem;
678
679 /*
680 * Avail_end is set to the first byte of physical memory
681 * after the end of the last bank. We use this only to
682 * determine if a physical address is "managed" memory.
683 */
684 avail_end = pmap_membank->pmem_end;
685
686 /*
687 * First allocate enough kernel MMU tables to map all
688 * of kernel virtual space from KERNBASE to 0xFFFFFFFF.
689 * Note: All must be aligned on 256 byte boundaries.
690 * Start with the level-A table (one of those).
691 */
692 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE;
693 kernAbase = pmap_bootstrap_alloc(size);
694 bzero(kernAbase, size);
695
696 /* Now the level-B kernel tables... */
697 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * KERN_B_TABLES;
698 kernBbase = pmap_bootstrap_alloc(size);
699 bzero(kernBbase, size);
700
701 /* Now the level-C kernel tables... */
702 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * KERN_C_TABLES;
703 kernCbase = pmap_bootstrap_alloc(size);
704 bzero(kernCbase, size);
705 /*
706 * Note: In order for the PV system to work correctly, the kernel
707 * and user-level C tables must be allocated contiguously.
708 * Nothing should be allocated between here and the allocation of
709 * mmuCbase below. XXX: Should do this as one allocation, and
710 * then compute a pointer for mmuCbase instead of this...
711 *
712 * Allocate user MMU tables.
713 * These must be contiguous with the preceeding.
714 */
715
716 #ifndef FIXED_NTABLES
717 /*
718 * The number of user-level C tables that should be allocated is
719 * related to the size of physical memory. In general, there should
720 * be enough tables to map four times the amount of available RAM.
721 * The extra amount is needed because some table space is wasted by
722 * fragmentation.
723 */
724 NUM_C_TABLES = (total_phys_mem * 4) / (MMU_C_TBL_SIZE * MMU_PAGE_SIZE);
725 NUM_B_TABLES = NUM_C_TABLES / 2;
726 NUM_A_TABLES = NUM_B_TABLES / 2;
727 #endif /* !FIXED_NTABLES */
728
729 size = sizeof(mmu_short_pte_t) * MMU_C_TBL_SIZE * NUM_C_TABLES;
730 mmuCbase = pmap_bootstrap_alloc(size);
731
732 size = sizeof(mmu_short_dte_t) * MMU_B_TBL_SIZE * NUM_B_TABLES;
733 mmuBbase = pmap_bootstrap_alloc(size);
734
735 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE * NUM_A_TABLES;
736 mmuAbase = pmap_bootstrap_alloc(size);
737
738 /*
739 * Fill in the never-changing part of the kernel tables.
740 * For simplicity, the kernel's mappings will be editable as a
741 * flat array of page table entries at kernCbase. The
742 * higher level 'A' and 'B' tables must be initialized to point
743 * to this lower one.
744 */
745 b = c = 0;
746
747 /*
748 * Invalidate all mappings below KERNBASE in the A table.
749 * This area has already been zeroed out, but it is good
750 * practice to explicitly show that we are interpreting
751 * it as a list of A table descriptors.
752 */
753 for (i = 0; i < MMU_TIA(KERNBASE); i++) {
754 kernAbase[i].addr.raw = 0;
755 }
756
757 /*
758 * Set up the kernel A and B tables so that they will reference the
759 * correct spots in the contiguous table of PTEs allocated for the
760 * kernel's virtual memory space.
761 */
762 for (i = MMU_TIA(KERNBASE); i < MMU_A_TBL_SIZE; i++) {
763 kernAbase[i].attr.raw =
764 MMU_LONG_DTE_LU | MMU_LONG_DTE_SUPV | MMU_DT_SHORT;
765 kernAbase[i].addr.raw = mmu_vtop(&kernBbase[b]);
766
767 for (j=0; j < MMU_B_TBL_SIZE; j++) {
768 kernBbase[b + j].attr.raw = mmu_vtop(&kernCbase[c])
769 | MMU_DT_SHORT;
770 c += MMU_C_TBL_SIZE;
771 }
772 b += MMU_B_TBL_SIZE;
773 }
774
775 /* XXX - Doing kernel_pmap a little further down. */
776
777 pmap_alloc_usermmu(); /* Allocate user MMU tables. */
778 pmap_alloc_usertmgr(); /* Allocate user MMU table managers.*/
779 pmap_alloc_pv(); /* Allocate physical->virtual map. */
780
781 /*
782 * We are now done with pmap_bootstrap_alloc(). Round up
783 * `virtual_avail' to the nearest page, and set the flag
784 * to prevent use of pmap_bootstrap_alloc() hereafter.
785 */
786 pmap_bootstrap_aalign(NBPG);
787 bootstrap_alloc_enabled = FALSE;
788
789 /*
790 * Now that we are done with pmap_bootstrap_alloc(), we
791 * must save the virtual and physical addresses of the
792 * end of the linearly mapped range, which are stored in
793 * virtual_contig_end and avail_start, respectively.
794 * These variables will never change after this point.
795 */
796 virtual_contig_end = virtual_avail;
797 avail_start = virtual_avail - KERNBASE;
798
799 /*
800 * `avail_next' is a running pointer used by pmap_next_page() to
801 * keep track of the next available physical page to be handed
802 * to the VM system during its initialization, in which it
803 * asks for physical pages, one at a time.
804 */
805 avail_next = avail_start;
806
807 /*
808 * Now allocate some virtual addresses, but not the physical pages
809 * behind them. Note that virtual_avail is already page-aligned.
810 *
811 * tmp_vpages[] is an array of two virtual pages used for temporary
812 * kernel mappings in the pmap module to facilitate various physical
813 * address-oritented operations.
814 */
815 tmp_vpages[0] = virtual_avail;
816 virtual_avail += NBPG;
817 tmp_vpages[1] = virtual_avail;
818 virtual_avail += NBPG;
819
820 /** Initialize the PV system **/
821 pmap_init_pv();
822
823 /*
824 * Fill in the kernel_pmap structure and kernel_crp.
825 */
826 kernAphys = mmu_vtop(kernAbase);
827 kernel_pmap.pm_a_tmgr = NULL;
828 kernel_pmap.pm_a_phys = kernAphys;
829 kernel_pmap.pm_refcount = 1; /* always in use */
830 simple_lock_init(&kernel_pmap.pm_lock);
831
832 kernel_crp.rp_attr = MMU_LONG_DTE_LU | MMU_DT_LONG;
833 kernel_crp.rp_addr = kernAphys;
834
835 /*
836 * Now pmap_enter_kernel() may be used safely and will be
837 * the main interface used hereafter to modify the kernel's
838 * virtual address space. Note that since we are still running
839 * under the PROM's address table, none of these table modifications
840 * actually take effect until pmap_takeover_mmu() is called.
841 *
842 * Note: Our tables do NOT have the PROM linear mappings!
843 * Only the mappings created here exist in our tables, so
844 * remember to map anything we expect to use.
845 */
846 va = (vm_offset_t) KERNBASE;
847 pa = 0;
848
849 /*
850 * The first page of the kernel virtual address space is the msgbuf
851 * page. The page attributes (data, non-cached) are set here, while
852 * the address is assigned to this global pointer in cpu_startup().
853 * It is non-cached, mostly due to paranoia.
854 */
855 pmap_enter_kernel(va, pa|PMAP_NC, VM_PROT_ALL);
856 va += NBPG; pa += NBPG;
857
858 /* Next page is used as the temporary stack. */
859 pmap_enter_kernel(va, pa, VM_PROT_ALL);
860 va += NBPG; pa += NBPG;
861
862 /*
863 * Map all of the kernel's text segment as read-only and cacheable.
864 * (Cacheable is implied by default). Unfortunately, the last bytes
865 * of kernel text and the first bytes of kernel data will often be
866 * sharing the same page. Therefore, the last page of kernel text
867 * has to be mapped as read/write, to accomodate the data.
868 */
869 eva = m68k_trunc_page((vm_offset_t)etext);
870 for (; va < eva; va += NBPG, pa += NBPG)
871 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_EXECUTE);
872
873 /*
874 * Map all of the kernel's data as read/write and cacheable.
875 * This includes: data, BSS, symbols, and everything in the
876 * contiguous memory used by pmap_bootstrap_alloc()
877 */
878 for (; pa < avail_start; va += NBPG, pa += NBPG)
879 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE);
880
881 /*
882 * At this point we are almost ready to take over the MMU. But first
883 * we must save the PROM's address space in our map, as we call its
884 * routines and make references to its data later in the kernel.
885 */
886 pmap_bootstrap_copyprom();
887 pmap_takeover_mmu();
888 pmap_bootstrap_setprom();
889
890 /* Notify the VM system of our page size. */
891 PAGE_SIZE = NBPG;
892 uvm_setpagesize();
893
894 pmap_page_upload();
895 }
896
897
898 /* pmap_alloc_usermmu INTERNAL
899 **
900 * Called from pmap_bootstrap() to allocate MMU tables that will
901 * eventually be used for user mappings.
902 */
903 void
904 pmap_alloc_usermmu()
905 {
906 /* XXX: Moved into caller. */
907 }
908
909 /* pmap_alloc_pv INTERNAL
910 **
911 * Called from pmap_bootstrap() to allocate the physical
912 * to virtual mapping list. Each physical page of memory
913 * in the system has a corresponding element in this list.
914 */
915 void
916 pmap_alloc_pv()
917 {
918 int i;
919 unsigned int total_mem;
920
921 /*
922 * Allocate a pv_head structure for every page of physical
923 * memory that will be managed by the system. Since memory on
924 * the 3/80 is non-contiguous, we cannot arrive at a total page
925 * count by subtraction of the lowest available address from the
926 * highest, but rather we have to step through each memory
927 * bank and add the number of pages in each to the total.
928 *
929 * At this time we also initialize the offset of each bank's
930 * starting pv_head within the pv_head list so that the physical
931 * memory state routines (pmap_is_referenced(),
932 * pmap_is_modified(), et al.) can quickly find coresponding
933 * pv_heads in spite of the non-contiguity.
934 */
935 total_mem = 0;
936 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) {
937 avail_mem[i].pmem_pvbase = m68k_btop(total_mem);
938 total_mem += avail_mem[i].pmem_end -
939 avail_mem[i].pmem_start;
940 if (avail_mem[i].pmem_next == NULL)
941 break;
942 }
943 pvbase = (pv_t *) pmap_bootstrap_alloc(sizeof(pv_t) *
944 m68k_btop(total_phys_mem));
945 }
946
947 /* pmap_alloc_usertmgr INTERNAL
948 **
949 * Called from pmap_bootstrap() to allocate the structures which
950 * facilitate management of user MMU tables. Each user MMU table
951 * in the system has one such structure associated with it.
952 */
953 void
954 pmap_alloc_usertmgr()
955 {
956 /* Allocate user MMU table managers */
957 /* It would be a lot simpler to just make these BSS, but */
958 /* we may want to change their size at boot time... -j */
959 Atmgrbase = (a_tmgr_t *) pmap_bootstrap_alloc(sizeof(a_tmgr_t)
960 * NUM_A_TABLES);
961 Btmgrbase = (b_tmgr_t *) pmap_bootstrap_alloc(sizeof(b_tmgr_t)
962 * NUM_B_TABLES);
963 Ctmgrbase = (c_tmgr_t *) pmap_bootstrap_alloc(sizeof(c_tmgr_t)
964 * NUM_C_TABLES);
965
966 /*
967 * Allocate PV list elements for the physical to virtual
968 * mapping system.
969 */
970 pvebase = (pv_elem_t *) pmap_bootstrap_alloc(
971 sizeof(pv_elem_t) * (NUM_USER_PTES + NUM_KERN_PTES));
972 }
973
974 /* pmap_bootstrap_copyprom() INTERNAL
975 **
976 * Copy the PROM mappings into our own tables. Note, we
977 * can use physical addresses until __bootstrap returns.
978 */
979 void
980 pmap_bootstrap_copyprom()
981 {
982 struct sunromvec *romp;
983 int *mon_ctbl;
984 mmu_short_pte_t *kpte;
985 int i, len;
986
987 romp = romVectorPtr;
988
989 /*
990 * Copy the mappings in SUN3X_MON_KDB_BASE...SUN3X_MONEND
991 * Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE
992 */
993 mon_ctbl = *romp->monptaddr;
994 i = m68k_btop(SUN3X_MON_KDB_BASE - KERNBASE);
995 kpte = &kernCbase[i];
996 len = m68k_btop(SUN3X_MONEND - SUN3X_MON_KDB_BASE);
997
998 for (i = 0; i < len; i++) {
999 kpte[i].attr.raw = mon_ctbl[i];
1000 }
1001
1002 /*
1003 * Copy the mappings at MON_DVMA_BASE (to the end).
1004 * Note, in here, mon_ctbl[0] maps MON_DVMA_BASE.
1005 * Actually, we only want the last page, which the
1006 * PROM has set up for use by the "ie" driver.
1007 * (The i82686 needs its SCP there.)
1008 * If we copy all the mappings, pmap_enter_kernel
1009 * may complain about finding valid PTEs that are
1010 * not recorded in our PV lists...
1011 */
1012 mon_ctbl = *romp->shadowpteaddr;
1013 i = m68k_btop(SUN3X_MON_DVMA_BASE - KERNBASE);
1014 kpte = &kernCbase[i];
1015 len = m68k_btop(SUN3X_MON_DVMA_SIZE);
1016 for (i = (len-1); i < len; i++) {
1017 kpte[i].attr.raw = mon_ctbl[i];
1018 }
1019 }
1020
1021 /* pmap_takeover_mmu INTERNAL
1022 **
1023 * Called from pmap_bootstrap() after it has copied enough of the
1024 * PROM mappings into the kernel map so that we can use our own
1025 * MMU table.
1026 */
1027 void
1028 pmap_takeover_mmu()
1029 {
1030
1031 loadcrp(&kernel_crp);
1032 }
1033
1034 /* pmap_bootstrap_setprom() INTERNAL
1035 **
1036 * Set the PROM mappings so it can see kernel space.
1037 * Note that physical addresses are used here, which
1038 * we can get away with because this runs with the
1039 * low 1GB set for transparent translation.
1040 */
1041 void
1042 pmap_bootstrap_setprom()
1043 {
1044 mmu_long_dte_t *mon_dte;
1045 extern struct mmu_rootptr mon_crp;
1046 int i;
1047
1048 mon_dte = (mmu_long_dte_t *) mon_crp.rp_addr;
1049 for (i = MMU_TIA(KERNBASE); i < MMU_TIA(KERN_END); i++) {
1050 mon_dte[i].attr.raw = kernAbase[i].attr.raw;
1051 mon_dte[i].addr.raw = kernAbase[i].addr.raw;
1052 }
1053 }
1054
1055
1056 /* pmap_init INTERFACE
1057 **
1058 * Called at the end of vm_init() to set up the pmap system to go
1059 * into full time operation. All initialization of kernel_pmap
1060 * should be already done by now, so this should just do things
1061 * needed for user-level pmaps to work.
1062 */
1063 void
1064 pmap_init()
1065 {
1066 /** Initialize the manager pools **/
1067 TAILQ_INIT(&a_pool);
1068 TAILQ_INIT(&b_pool);
1069 TAILQ_INIT(&c_pool);
1070
1071 /**************************************************************
1072 * Initialize all tmgr structures and MMU tables they manage. *
1073 **************************************************************/
1074 /** Initialize A tables **/
1075 pmap_init_a_tables();
1076 /** Initialize B tables **/
1077 pmap_init_b_tables();
1078 /** Initialize C tables **/
1079 pmap_init_c_tables();
1080 }
1081
1082 /* pmap_init_a_tables() INTERNAL
1083 **
1084 * Initializes all A managers, their MMU A tables, and inserts
1085 * them into the A manager pool for use by the system.
1086 */
1087 void
1088 pmap_init_a_tables()
1089 {
1090 int i;
1091 a_tmgr_t *a_tbl;
1092
1093 for (i=0; i < NUM_A_TABLES; i++) {
1094 /* Select the next available A manager from the pool */
1095 a_tbl = &Atmgrbase[i];
1096
1097 /*
1098 * Clear its parent entry. Set its wired and valid
1099 * entry count to zero.
1100 */
1101 a_tbl->at_parent = NULL;
1102 a_tbl->at_wcnt = a_tbl->at_ecnt = 0;
1103
1104 /* Assign it the next available MMU A table from the pool */
1105 a_tbl->at_dtbl = &mmuAbase[i * MMU_A_TBL_SIZE];
1106
1107 /*
1108 * Initialize the MMU A table with the table in the `proc0',
1109 * or kernel, mapping. This ensures that every process has
1110 * the kernel mapped in the top part of its address space.
1111 */
1112 bcopy(kernAbase, a_tbl->at_dtbl, MMU_A_TBL_SIZE *
1113 sizeof(mmu_long_dte_t));
1114
1115 /*
1116 * Finally, insert the manager into the A pool,
1117 * making it ready to be used by the system.
1118 */
1119 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
1120 }
1121 }
1122
1123 /* pmap_init_b_tables() INTERNAL
1124 **
1125 * Initializes all B table managers, their MMU B tables, and
1126 * inserts them into the B manager pool for use by the system.
1127 */
1128 void
1129 pmap_init_b_tables()
1130 {
1131 int i,j;
1132 b_tmgr_t *b_tbl;
1133
1134 for (i=0; i < NUM_B_TABLES; i++) {
1135 /* Select the next available B manager from the pool */
1136 b_tbl = &Btmgrbase[i];
1137
1138 b_tbl->bt_parent = NULL; /* clear its parent, */
1139 b_tbl->bt_pidx = 0; /* parent index, */
1140 b_tbl->bt_wcnt = 0; /* wired entry count, */
1141 b_tbl->bt_ecnt = 0; /* valid entry count. */
1142
1143 /* Assign it the next available MMU B table from the pool */
1144 b_tbl->bt_dtbl = &mmuBbase[i * MMU_B_TBL_SIZE];
1145
1146 /* Invalidate every descriptor in the table */
1147 for (j=0; j < MMU_B_TBL_SIZE; j++)
1148 b_tbl->bt_dtbl[j].attr.raw = MMU_DT_INVALID;
1149
1150 /* Insert the manager into the B pool */
1151 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
1152 }
1153 }
1154
1155 /* pmap_init_c_tables() INTERNAL
1156 **
1157 * Initializes all C table managers, their MMU C tables, and
1158 * inserts them into the C manager pool for use by the system.
1159 */
1160 void
1161 pmap_init_c_tables()
1162 {
1163 int i,j;
1164 c_tmgr_t *c_tbl;
1165
1166 for (i=0; i < NUM_C_TABLES; i++) {
1167 /* Select the next available C manager from the pool */
1168 c_tbl = &Ctmgrbase[i];
1169
1170 c_tbl->ct_parent = NULL; /* clear its parent, */
1171 c_tbl->ct_pidx = 0; /* parent index, */
1172 c_tbl->ct_wcnt = 0; /* wired entry count, */
1173 c_tbl->ct_ecnt = 0; /* valid entry count, */
1174 c_tbl->ct_pmap = NULL; /* parent pmap, */
1175 c_tbl->ct_va = 0; /* base of managed range */
1176
1177 /* Assign it the next available MMU C table from the pool */
1178 c_tbl->ct_dtbl = &mmuCbase[i * MMU_C_TBL_SIZE];
1179
1180 for (j=0; j < MMU_C_TBL_SIZE; j++)
1181 c_tbl->ct_dtbl[j].attr.raw = MMU_DT_INVALID;
1182
1183 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
1184 }
1185 }
1186
1187 /* pmap_init_pv() INTERNAL
1188 **
1189 * Initializes the Physical to Virtual mapping system.
1190 */
1191 void
1192 pmap_init_pv()
1193 {
1194 int i;
1195
1196 /* Initialize every PV head. */
1197 for (i = 0; i < m68k_btop(total_phys_mem); i++) {
1198 pvbase[i].pv_idx = PVE_EOL; /* Indicate no mappings */
1199 pvbase[i].pv_flags = 0; /* Zero out page flags */
1200 }
1201
1202 pv_initialized = TRUE;
1203 }
1204
1205 /* get_a_table INTERNAL
1206 **
1207 * Retrieve and return a level A table for use in a user map.
1208 */
1209 a_tmgr_t *
1210 get_a_table()
1211 {
1212 a_tmgr_t *tbl;
1213 pmap_t pmap;
1214
1215 /* Get the top A table in the pool */
1216 tbl = a_pool.tqh_first;
1217 if (tbl == NULL) {
1218 /*
1219 * XXX - Instead of panicing here and in other get_x_table
1220 * functions, we do have the option of sleeping on the head of
1221 * the table pool. Any function which updates the table pool
1222 * would then issue a wakeup() on the head, thus waking up any
1223 * processes waiting for a table.
1224 *
1225 * Actually, the place to sleep would be when some process
1226 * asks for a "wired" mapping that would run us short of
1227 * mapping resources. This design DEPENDS on always having
1228 * some mapping resources in the pool for stealing, so we
1229 * must make sure we NEVER let the pool become empty. -gwr
1230 */
1231 panic("get_a_table: out of A tables.");
1232 }
1233
1234 TAILQ_REMOVE(&a_pool, tbl, at_link);
1235 /*
1236 * If the table has a non-null parent pointer then it is in use.
1237 * Forcibly abduct it from its parent and clear its entries.
1238 * No re-entrancy worries here. This table would not be in the
1239 * table pool unless it was available for use.
1240 *
1241 * Note that the second argument to free_a_table() is FALSE. This
1242 * indicates that the table should not be relinked into the A table
1243 * pool. That is a job for the function that called us.
1244 */
1245 if (tbl->at_parent) {
1246 pmap = tbl->at_parent;
1247 free_a_table(tbl, FALSE);
1248 pmap->pm_a_tmgr = NULL;
1249 pmap->pm_a_phys = kernAphys;
1250 }
1251 #ifdef NON_REENTRANT
1252 /*
1253 * If the table isn't to be wired down, re-insert it at the
1254 * end of the pool.
1255 */
1256 if (!wired)
1257 /*
1258 * Quandary - XXX
1259 * Would it be better to let the calling function insert this
1260 * table into the queue? By inserting it here, we are allowing
1261 * it to be stolen immediately. The calling function is
1262 * probably not expecting to use a table that it is not
1263 * assured full control of.
1264 * Answer - In the intrest of re-entrancy, it is best to let
1265 * the calling function determine when a table is available
1266 * for use. Therefore this code block is not used.
1267 */
1268 TAILQ_INSERT_TAIL(&a_pool, tbl, at_link);
1269 #endif /* NON_REENTRANT */
1270 return tbl;
1271 }
1272
1273 /* get_b_table INTERNAL
1274 **
1275 * Return a level B table for use.
1276 */
1277 b_tmgr_t *
1278 get_b_table()
1279 {
1280 b_tmgr_t *tbl;
1281
1282 /* See 'get_a_table' for comments. */
1283 tbl = b_pool.tqh_first;
1284 if (tbl == NULL)
1285 panic("get_b_table: out of B tables.");
1286 TAILQ_REMOVE(&b_pool, tbl, bt_link);
1287 if (tbl->bt_parent) {
1288 tbl->bt_parent->at_dtbl[tbl->bt_pidx].attr.raw = MMU_DT_INVALID;
1289 tbl->bt_parent->at_ecnt--;
1290 free_b_table(tbl, FALSE);
1291 }
1292 #ifdef NON_REENTRANT
1293 if (!wired)
1294 /* XXX see quandary in get_b_table */
1295 /* XXX start lock */
1296 TAILQ_INSERT_TAIL(&b_pool, tbl, bt_link);
1297 /* XXX end lock */
1298 #endif /* NON_REENTRANT */
1299 return tbl;
1300 }
1301
1302 /* get_c_table INTERNAL
1303 **
1304 * Return a level C table for use.
1305 */
1306 c_tmgr_t *
1307 get_c_table()
1308 {
1309 c_tmgr_t *tbl;
1310
1311 /* See 'get_a_table' for comments */
1312 tbl = c_pool.tqh_first;
1313 if (tbl == NULL)
1314 panic("get_c_table: out of C tables.");
1315 TAILQ_REMOVE(&c_pool, tbl, ct_link);
1316 if (tbl->ct_parent) {
1317 tbl->ct_parent->bt_dtbl[tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1318 tbl->ct_parent->bt_ecnt--;
1319 free_c_table(tbl, FALSE);
1320 }
1321 #ifdef NON_REENTRANT
1322 if (!wired)
1323 /* XXX See quandary in get_a_table */
1324 /* XXX start lock */
1325 TAILQ_INSERT_TAIL(&c_pool, tbl, c_link);
1326 /* XXX end lock */
1327 #endif /* NON_REENTRANT */
1328
1329 return tbl;
1330 }
1331
1332 /*
1333 * The following 'free_table' and 'steal_table' functions are called to
1334 * detach tables from their current obligations (parents and children) and
1335 * prepare them for reuse in another mapping.
1336 *
1337 * Free_table is used when the calling function will handle the fate
1338 * of the parent table, such as returning it to the free pool when it has
1339 * no valid entries. Functions that do not want to handle this should
1340 * call steal_table, in which the parent table's descriptors and entry
1341 * count are automatically modified when this table is removed.
1342 */
1343
1344 /* free_a_table INTERNAL
1345 **
1346 * Unmaps the given A table and all child tables from their current
1347 * mappings. Returns the number of pages that were invalidated.
1348 * If 'relink' is true, the function will return the table to the head
1349 * of the available table pool.
1350 *
1351 * Cache note: The MC68851 will automatically flush all
1352 * descriptors derived from a given A table from its
1353 * Automatic Translation Cache (ATC) if we issue a
1354 * 'PFLUSHR' instruction with the base address of the
1355 * table. This function should do, and does so.
1356 * Note note: We are using an MC68030 - there is no
1357 * PFLUSHR.
1358 */
1359 int
1360 free_a_table(a_tbl, relink)
1361 a_tmgr_t *a_tbl;
1362 boolean_t relink;
1363 {
1364 int i, removed_cnt;
1365 mmu_long_dte_t *dte;
1366 mmu_short_dte_t *dtbl;
1367 b_tmgr_t *tmgr;
1368
1369 /*
1370 * Flush the ATC cache of all cached descriptors derived
1371 * from this table.
1372 * Sun3x does not use 68851's cached table feature
1373 * flush_atc_crp(mmu_vtop(a_tbl->dte));
1374 */
1375
1376 /*
1377 * Remove any pending cache flushes that were designated
1378 * for the pmap this A table belongs to.
1379 * a_tbl->parent->atc_flushq[0] = 0;
1380 * Not implemented in sun3x.
1381 */
1382
1383 /*
1384 * All A tables in the system should retain a map for the
1385 * kernel. If the table contains any valid descriptors
1386 * (other than those for the kernel area), invalidate them all,
1387 * stopping short of the kernel's entries.
1388 */
1389 removed_cnt = 0;
1390 if (a_tbl->at_ecnt) {
1391 dte = a_tbl->at_dtbl;
1392 for (i=0; i < MMU_TIA(KERNBASE); i++) {
1393 /*
1394 * If a table entry points to a valid B table, free
1395 * it and its children.
1396 */
1397 if (MMU_VALID_DT(dte[i])) {
1398 /*
1399 * The following block does several things,
1400 * from innermost expression to the
1401 * outermost:
1402 * 1) It extracts the base (cc 1996)
1403 * address of the B table pointed
1404 * to in the A table entry dte[i].
1405 * 2) It converts this base address into
1406 * the virtual address it can be
1407 * accessed with. (all MMU tables point
1408 * to physical addresses.)
1409 * 3) It finds the corresponding manager
1410 * structure which manages this MMU table.
1411 * 4) It frees the manager structure.
1412 * (This frees the MMU table and all
1413 * child tables. See 'free_b_table' for
1414 * details.)
1415 */
1416 dtbl = mmu_ptov(dte[i].addr.raw);
1417 tmgr = mmuB2tmgr(dtbl);
1418 removed_cnt += free_b_table(tmgr, TRUE);
1419 dte[i].attr.raw = MMU_DT_INVALID;
1420 }
1421 }
1422 a_tbl->at_ecnt = 0;
1423 }
1424 if (relink) {
1425 a_tbl->at_parent = NULL;
1426 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
1427 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link);
1428 }
1429 return removed_cnt;
1430 }
1431
1432 /* free_b_table INTERNAL
1433 **
1434 * Unmaps the given B table and all its children from their current
1435 * mappings. Returns the number of pages that were invalidated.
1436 * (For comments, see 'free_a_table()').
1437 */
1438 int
1439 free_b_table(b_tbl, relink)
1440 b_tmgr_t *b_tbl;
1441 boolean_t relink;
1442 {
1443 int i, removed_cnt;
1444 mmu_short_dte_t *dte;
1445 mmu_short_pte_t *dtbl;
1446 c_tmgr_t *tmgr;
1447
1448 removed_cnt = 0;
1449 if (b_tbl->bt_ecnt) {
1450 dte = b_tbl->bt_dtbl;
1451 for (i=0; i < MMU_B_TBL_SIZE; i++) {
1452 if (MMU_VALID_DT(dte[i])) {
1453 dtbl = mmu_ptov(MMU_DTE_PA(dte[i]));
1454 tmgr = mmuC2tmgr(dtbl);
1455 removed_cnt += free_c_table(tmgr, TRUE);
1456 dte[i].attr.raw = MMU_DT_INVALID;
1457 }
1458 }
1459 b_tbl->bt_ecnt = 0;
1460 }
1461
1462 if (relink) {
1463 b_tbl->bt_parent = NULL;
1464 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
1465 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link);
1466 }
1467 return removed_cnt;
1468 }
1469
1470 /* free_c_table INTERNAL
1471 **
1472 * Unmaps the given C table from use and returns it to the pool for
1473 * re-use. Returns the number of pages that were invalidated.
1474 *
1475 * This function preserves any physical page modification information
1476 * contained in the page descriptors within the C table by calling
1477 * 'pmap_remove_pte().'
1478 */
1479 int
1480 free_c_table(c_tbl, relink)
1481 c_tmgr_t *c_tbl;
1482 boolean_t relink;
1483 {
1484 int i, removed_cnt;
1485
1486 removed_cnt = 0;
1487 if (c_tbl->ct_ecnt) {
1488 for (i=0; i < MMU_C_TBL_SIZE; i++) {
1489 if (MMU_VALID_DT(c_tbl->ct_dtbl[i])) {
1490 pmap_remove_pte(&c_tbl->ct_dtbl[i]);
1491 removed_cnt++;
1492 }
1493 }
1494 c_tbl->ct_ecnt = 0;
1495 }
1496
1497 if (relink) {
1498 c_tbl->ct_parent = NULL;
1499 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1500 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
1501 }
1502 return removed_cnt;
1503 }
1504
1505 #if 0
1506 /* free_c_table_novalid INTERNAL
1507 **
1508 * Frees the given C table manager without checking to see whether
1509 * or not it contains any valid page descriptors as it is assumed
1510 * that it does not.
1511 */
1512 void
1513 free_c_table_novalid(c_tbl)
1514 c_tmgr_t *c_tbl;
1515 {
1516 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1517 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
1518 c_tbl->ct_parent->bt_dtbl[c_tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1519 c_tbl->ct_parent->bt_ecnt--;
1520 /*
1521 * XXX - Should call equiv. of 'free_b_table_novalid' here if
1522 * we just removed the last entry of the parent B table.
1523 * But I want to insure that this will not endanger pmap_enter()
1524 * with sudden removal of tables it is working with.
1525 *
1526 * We should probably add another field to each table, indicating
1527 * whether or not it is 'locked', ie. in the process of being
1528 * modified.
1529 */
1530 c_tbl->ct_parent = NULL;
1531 }
1532 #endif
1533
1534 /* pmap_remove_pte INTERNAL
1535 **
1536 * Unmap the given pte and preserve any page modification
1537 * information by transfering it to the pv head of the
1538 * physical page it maps to. This function does not update
1539 * any reference counts because it is assumed that the calling
1540 * function will do so.
1541 */
1542 void
1543 pmap_remove_pte(pte)
1544 mmu_short_pte_t *pte;
1545 {
1546 u_short pv_idx, targ_idx;
1547 int s;
1548 vm_offset_t pa;
1549 pv_t *pv;
1550
1551 pa = MMU_PTE_PA(*pte);
1552 if (is_managed(pa)) {
1553 pv = pa2pv(pa);
1554 targ_idx = pteidx(pte); /* Index of PTE being removed */
1555
1556 /*
1557 * If the PTE being removed is the first (or only) PTE in
1558 * the list of PTEs currently mapped to this page, remove the
1559 * PTE by changing the index found on the PV head. Otherwise
1560 * a linear search through the list will have to be executed
1561 * in order to find the PVE which points to the PTE being
1562 * removed, so that it may be modified to point to its new
1563 * neighbor.
1564 */
1565 s = splimp();
1566 pv_idx = pv->pv_idx; /* Index of first PTE in PV list */
1567 if (pv_idx == targ_idx) {
1568 pv->pv_idx = pvebase[targ_idx].pve_next;
1569 } else {
1570 /*
1571 * Find the PV element pointing to the target
1572 * element. Note: may have pv_idx==PVE_EOL
1573 */
1574 for (;;) {
1575 if (pv_idx == PVE_EOL) {
1576 #ifdef PMAP_DEBUG
1577 printf("pmap_remove_pte: PVE_EOL\n");
1578 Debugger();
1579 #endif
1580 goto pv_not_found;
1581 }
1582 if (pvebase[pv_idx].pve_next == targ_idx)
1583 break;
1584 pv_idx = pvebase[pv_idx].pve_next;
1585 }
1586 /*
1587 * At this point, pv_idx is the index of the PV
1588 * element just before the target element in the list.
1589 * Unlink the target.
1590 */
1591 pvebase[pv_idx].pve_next = pvebase[targ_idx].pve_next;
1592 pv_not_found:
1593 }
1594 /*
1595 * Save the mod/ref bits of the pte by simply
1596 * ORing the entire pte onto the pv_flags member
1597 * of the pv structure.
1598 * There is no need to use a separate bit pattern
1599 * for usage information on the pv head than that
1600 * which is used on the MMU ptes.
1601 */
1602 pv->pv_flags |= (u_short) pte->attr.raw;
1603 splx(s);
1604 }
1605
1606 pte->attr.raw = MMU_DT_INVALID;
1607 }
1608
1609 /* pmap_stroll INTERNAL
1610 **
1611 * Retrieve the addresses of all table managers involved in the mapping of
1612 * the given virtual address. If the table walk completed sucessfully,
1613 * return TRUE. If it was only partially sucessful, return FALSE.
1614 * The table walk performed by this function is important to many other
1615 * functions in this module.
1616 *
1617 * Note: This function ought to be easier to read.
1618 */
1619 boolean_t
1620 pmap_stroll(pmap, va, a_tbl, b_tbl, c_tbl, pte, a_idx, b_idx, pte_idx)
1621 pmap_t pmap;
1622 vm_offset_t va;
1623 a_tmgr_t **a_tbl;
1624 b_tmgr_t **b_tbl;
1625 c_tmgr_t **c_tbl;
1626 mmu_short_pte_t **pte;
1627 int *a_idx, *b_idx, *pte_idx;
1628 {
1629 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1630 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1631
1632 if (pmap == pmap_kernel())
1633 return FALSE;
1634
1635 /* Does the given pmap have its own A table? */
1636 *a_tbl = pmap->pm_a_tmgr;
1637 if (*a_tbl == NULL)
1638 return FALSE; /* No. Return unknown. */
1639 /* Does the A table have a valid B table
1640 * under the corresponding table entry?
1641 */
1642 *a_idx = MMU_TIA(va);
1643 a_dte = &((*a_tbl)->at_dtbl[*a_idx]);
1644 if (!MMU_VALID_DT(*a_dte))
1645 return FALSE; /* No. Return unknown. */
1646 /* Yes. Extract B table from the A table. */
1647 *b_tbl = mmuB2tmgr(mmu_ptov(a_dte->addr.raw));
1648 /* Does the B table have a valid C table
1649 * under the corresponding table entry?
1650 */
1651 *b_idx = MMU_TIB(va);
1652 b_dte = &((*b_tbl)->bt_dtbl[*b_idx]);
1653 if (!MMU_VALID_DT(*b_dte))
1654 return FALSE; /* No. Return unknown. */
1655 /* Yes. Extract C table from the B table. */
1656 *c_tbl = mmuC2tmgr(mmu_ptov(MMU_DTE_PA(*b_dte)));
1657 *pte_idx = MMU_TIC(va);
1658 *pte = &((*c_tbl)->ct_dtbl[*pte_idx]);
1659
1660 return TRUE;
1661 }
1662
1663 /* pmap_enter INTERFACE
1664 **
1665 * Called by the kernel to map a virtual address
1666 * to a physical address in the given process map.
1667 *
1668 * Note: this function should apply an exclusive lock
1669 * on the pmap system for its duration. (it certainly
1670 * would save my hair!!)
1671 * This function ought to be easier to read.
1672 */
1673 int
1674 pmap_enter(pmap, va, pa, prot, flags)
1675 pmap_t pmap;
1676 vm_offset_t va;
1677 vm_offset_t pa;
1678 vm_prot_t prot;
1679 int flags;
1680 {
1681 boolean_t insert, managed; /* Marks the need for PV insertion.*/
1682 u_short nidx; /* PV list index */
1683 int s; /* Used for splimp()/splx() */
1684 int mapflags; /* Flags for the mapping (see NOTE1) */
1685 u_int a_idx, b_idx, pte_idx; /* table indices */
1686 a_tmgr_t *a_tbl; /* A: long descriptor table manager */
1687 b_tmgr_t *b_tbl; /* B: short descriptor table manager */
1688 c_tmgr_t *c_tbl; /* C: short page table manager */
1689 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1690 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1691 mmu_short_pte_t *c_pte; /* C: short page descriptor table */
1692 pv_t *pv; /* pv list head */
1693 boolean_t wired; /* is the mapping to be wired? */
1694 enum {NONE, NEWA, NEWB, NEWC} llevel; /* used at end */
1695
1696 if (pmap == NULL)
1697 return (KERN_SUCCESS);
1698 if (pmap == pmap_kernel()) {
1699 pmap_enter_kernel(va, pa, prot);
1700 return (KERN_SUCCESS);
1701 }
1702
1703 /*
1704 * Determine if the mapping should be wired.
1705 */
1706 wired = ((flags & PMAP_WIRED) != 0);
1707
1708 /*
1709 * NOTE1:
1710 *
1711 * On November 13, 1999, someone changed the pmap_enter() API such
1712 * that it now accepts a 'flags' argument. This new argument
1713 * contains bit-flags for the architecture-independent (UVM) system to
1714 * use in signalling certain mapping requirements to the architecture-
1715 * dependent (pmap) system. The argument it replaces, 'wired', is now
1716 * one of the flags within it.
1717 *
1718 * In addition to flags signaled by the architecture-independent
1719 * system, parts of the architecture-dependent section of the sun3x
1720 * kernel pass their own flags in the lower, unused bits of the
1721 * physical address supplied to this function. These flags are
1722 * extracted and stored in the temporary variable 'mapflags'.
1723 *
1724 * Extract sun3x specific flags from the physical address.
1725 */
1726 mapflags = (pa & ~MMU_PAGE_MASK);
1727 pa &= MMU_PAGE_MASK;
1728
1729 /*
1730 * Determine if the physical address being mapped is on-board RAM.
1731 * Any other area of the address space is likely to belong to a
1732 * device and hence it would be disasterous to cache its contents.
1733 */
1734 if ((managed = is_managed(pa)) == FALSE)
1735 mapflags |= PMAP_NC;
1736
1737 /*
1738 * For user mappings we walk along the MMU tables of the given
1739 * pmap, reaching a PTE which describes the virtual page being
1740 * mapped or changed. If any level of the walk ends in an invalid
1741 * entry, a table must be allocated and the entry must be updated
1742 * to point to it.
1743 * There is a bit of confusion as to whether this code must be
1744 * re-entrant. For now we will assume it is. To support
1745 * re-entrancy we must unlink tables from the table pool before
1746 * we assume we may use them. Tables are re-linked into the pool
1747 * when we are finished with them at the end of the function.
1748 * But I don't feel like doing that until we have proof that this
1749 * needs to be re-entrant.
1750 * 'llevel' records which tables need to be relinked.
1751 */
1752 llevel = NONE;
1753
1754 /*
1755 * Step 1 - Retrieve the A table from the pmap. If it has no
1756 * A table, allocate a new one from the available pool.
1757 */
1758
1759 a_tbl = pmap->pm_a_tmgr;
1760 if (a_tbl == NULL) {
1761 /*
1762 * This pmap does not currently have an A table. Allocate
1763 * a new one.
1764 */
1765 a_tbl = get_a_table();
1766 a_tbl->at_parent = pmap;
1767
1768 /*
1769 * Assign this new A table to the pmap, and calculate its
1770 * physical address so that loadcrp() can be used to make
1771 * the table active.
1772 */
1773 pmap->pm_a_tmgr = a_tbl;
1774 pmap->pm_a_phys = mmu_vtop(a_tbl->at_dtbl);
1775
1776 /*
1777 * If the process receiving a new A table is the current
1778 * process, we are responsible for setting the MMU so that
1779 * it becomes the current address space. This only adds
1780 * new mappings, so no need to flush anything.
1781 */
1782 if (pmap == current_pmap()) {
1783 kernel_crp.rp_addr = pmap->pm_a_phys;
1784 loadcrp(&kernel_crp);
1785 }
1786
1787 if (!wired)
1788 llevel = NEWA;
1789 } else {
1790 /*
1791 * Use the A table already allocated for this pmap.
1792 * Unlink it from the A table pool if necessary.
1793 */
1794 if (wired && !a_tbl->at_wcnt)
1795 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
1796 }
1797
1798 /*
1799 * Step 2 - Walk into the B table. If there is no valid B table,
1800 * allocate one.
1801 */
1802
1803 a_idx = MMU_TIA(va); /* Calculate the TIA of the VA. */
1804 a_dte = &a_tbl->at_dtbl[a_idx]; /* Retrieve descriptor from table */
1805 if (MMU_VALID_DT(*a_dte)) { /* Is the descriptor valid? */
1806 /* The descriptor is valid. Use the B table it points to. */
1807 /*************************************
1808 * a_idx *
1809 * v *
1810 * a_tbl -> +-+-+-+-+-+-+-+-+-+-+-+- *
1811 * | | | | | | | | | | | | *
1812 * +-+-+-+-+-+-+-+-+-+-+-+- *
1813 * | *
1814 * \- b_tbl -> +-+- *
1815 * | | *
1816 * +-+- *
1817 *************************************/
1818 b_dte = mmu_ptov(a_dte->addr.raw);
1819 b_tbl = mmuB2tmgr(b_dte);
1820
1821 /*
1822 * If the requested mapping must be wired, but this table
1823 * being used to map it is not, the table must be removed
1824 * from the available pool and its wired entry count
1825 * incremented.
1826 */
1827 if (wired && !b_tbl->bt_wcnt) {
1828 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
1829 a_tbl->at_wcnt++;
1830 }
1831 } else {
1832 /* The descriptor is invalid. Allocate a new B table. */
1833 b_tbl = get_b_table();
1834
1835 /* Point the parent A table descriptor to this new B table. */
1836 a_dte->addr.raw = mmu_vtop(b_tbl->bt_dtbl);
1837 a_dte->attr.raw = MMU_LONG_DTE_LU | MMU_DT_SHORT;
1838 a_tbl->at_ecnt++; /* Update parent's valid entry count */
1839
1840 /* Create the necessary back references to the parent table */
1841 b_tbl->bt_parent = a_tbl;
1842 b_tbl->bt_pidx = a_idx;
1843
1844 /*
1845 * If this table is to be wired, make sure the parent A table
1846 * wired count is updated to reflect that it has another wired
1847 * entry.
1848 */
1849 if (wired)
1850 a_tbl->at_wcnt++;
1851 else if (llevel == NONE)
1852 llevel = NEWB;
1853 }
1854
1855 /*
1856 * Step 3 - Walk into the C table, if there is no valid C table,
1857 * allocate one.
1858 */
1859
1860 b_idx = MMU_TIB(va); /* Calculate the TIB of the VA */
1861 b_dte = &b_tbl->bt_dtbl[b_idx]; /* Retrieve descriptor from table */
1862 if (MMU_VALID_DT(*b_dte)) { /* Is the descriptor valid? */
1863 /* The descriptor is valid. Use the C table it points to. */
1864 /**************************************
1865 * c_idx *
1866 * | v *
1867 * \- b_tbl -> +-+-+-+-+-+-+-+-+-+-+- *
1868 * | | | | | | | | | | | *
1869 * +-+-+-+-+-+-+-+-+-+-+- *
1870 * | *
1871 * \- c_tbl -> +-+-- *
1872 * | | | *
1873 * +-+-- *
1874 **************************************/
1875 c_pte = mmu_ptov(MMU_PTE_PA(*b_dte));
1876 c_tbl = mmuC2tmgr(c_pte);
1877
1878 /* If mapping is wired and table is not */
1879 if (wired && !c_tbl->ct_wcnt) {
1880 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1881 b_tbl->bt_wcnt++;
1882 }
1883 } else {
1884 /* The descriptor is invalid. Allocate a new C table. */
1885 c_tbl = get_c_table();
1886
1887 /* Point the parent B table descriptor to this new C table. */
1888 b_dte->attr.raw = mmu_vtop(c_tbl->ct_dtbl);
1889 b_dte->attr.raw |= MMU_DT_SHORT;
1890 b_tbl->bt_ecnt++; /* Update parent's valid entry count */
1891
1892 /* Create the necessary back references to the parent table */
1893 c_tbl->ct_parent = b_tbl;
1894 c_tbl->ct_pidx = b_idx;
1895 /*
1896 * Store the pmap and base virtual managed address for faster
1897 * retrieval in the PV functions.
1898 */
1899 c_tbl->ct_pmap = pmap;
1900 c_tbl->ct_va = (va & (MMU_TIA_MASK|MMU_TIB_MASK));
1901
1902 /*
1903 * If this table is to be wired, make sure the parent B table
1904 * wired count is updated to reflect that it has another wired
1905 * entry.
1906 */
1907 if (wired)
1908 b_tbl->bt_wcnt++;
1909 else if (llevel == NONE)
1910 llevel = NEWC;
1911 }
1912
1913 /*
1914 * Step 4 - Deposit a page descriptor (PTE) into the appropriate
1915 * slot of the C table, describing the PA to which the VA is mapped.
1916 */
1917
1918 pte_idx = MMU_TIC(va);
1919 c_pte = &c_tbl->ct_dtbl[pte_idx];
1920 if (MMU_VALID_DT(*c_pte)) { /* Is the entry currently valid? */
1921 /*
1922 * The PTE is currently valid. This particular call
1923 * is just a synonym for one (or more) of the following
1924 * operations:
1925 * change protection of a page
1926 * change wiring status of a page
1927 * remove the mapping of a page
1928 *
1929 * XXX - Semi critical: This code should unwire the PTE
1930 * and, possibly, associated parent tables if this is a
1931 * change wiring operation. Currently it does not.
1932 *
1933 * This may be ok if pmap_unwire() is the only
1934 * interface used to UNWIRE a page.
1935 */
1936
1937 /* First check if this is a wiring operation. */
1938 if (wired && (c_pte->attr.raw & MMU_SHORT_PTE_WIRED)) {
1939 /*
1940 * The PTE is already wired. To prevent it from being
1941 * counted as a new wiring operation, reset the 'wired'
1942 * variable.
1943 */
1944 wired = FALSE;
1945 }
1946
1947 /* Is the new address the same as the old? */
1948 if (MMU_PTE_PA(*c_pte) == pa) {
1949 /*
1950 * Yes, mark that it does not need to be reinserted
1951 * into the PV list.
1952 */
1953 insert = FALSE;
1954
1955 /*
1956 * Clear all but the modified, referenced and wired
1957 * bits on the PTE.
1958 */
1959 c_pte->attr.raw &= (MMU_SHORT_PTE_M
1960 | MMU_SHORT_PTE_USED | MMU_SHORT_PTE_WIRED);
1961 } else {
1962 /* No, remove the old entry */
1963 pmap_remove_pte(c_pte);
1964 insert = TRUE;
1965 }
1966
1967 /*
1968 * TLB flush is only necessary if modifying current map.
1969 * However, in pmap_enter(), the pmap almost always IS
1970 * the current pmap, so don't even bother to check.
1971 */
1972 TBIS(va);
1973 } else {
1974 /*
1975 * The PTE is invalid. Increment the valid entry count in
1976 * the C table manager to reflect the addition of a new entry.
1977 */
1978 c_tbl->ct_ecnt++;
1979
1980 /* XXX - temporarily make sure the PTE is cleared. */
1981 c_pte->attr.raw = 0;
1982
1983 /* It will also need to be inserted into the PV list. */
1984 insert = TRUE;
1985 }
1986
1987 /*
1988 * If page is changing from unwired to wired status, set an unused bit
1989 * within the PTE to indicate that it is wired. Also increment the
1990 * wired entry count in the C table manager.
1991 */
1992 if (wired) {
1993 c_pte->attr.raw |= MMU_SHORT_PTE_WIRED;
1994 c_tbl->ct_wcnt++;
1995 }
1996
1997 /*
1998 * Map the page, being careful to preserve modify/reference/wired
1999 * bits. At this point it is assumed that the PTE either has no bits
2000 * set, or if there are set bits, they are only modified, reference or
2001 * wired bits. If not, the following statement will cause erratic
2002 * behavior.
2003 */
2004 #ifdef PMAP_DEBUG
2005 if (c_pte->attr.raw & ~(MMU_SHORT_PTE_M |
2006 MMU_SHORT_PTE_USED | MMU_SHORT_PTE_WIRED)) {
2007 printf("pmap_enter: junk left in PTE at %p\n", c_pte);
2008 Debugger();
2009 }
2010 #endif
2011 c_pte->attr.raw |= ((u_long) pa | MMU_DT_PAGE);
2012
2013 /*
2014 * If the mapping should be read-only, set the write protect
2015 * bit in the PTE.
2016 */
2017 if (!(prot & VM_PROT_WRITE))
2018 c_pte->attr.raw |= MMU_SHORT_PTE_WP;
2019
2020 /*
2021 * If the mapping should be cache inhibited (indicated by the flag
2022 * bits found on the lower order of the physical address.)
2023 * mark the PTE as a cache inhibited page.
2024 */
2025 if (mapflags & PMAP_NC)
2026 c_pte->attr.raw |= MMU_SHORT_PTE_CI;
2027
2028 /*
2029 * If the physical address being mapped is managed by the PV
2030 * system then link the pte into the list of pages mapped to that
2031 * address.
2032 */
2033 if (insert && managed) {
2034 pv = pa2pv(pa);
2035 nidx = pteidx(c_pte);
2036
2037 s = splimp();
2038 pvebase[nidx].pve_next = pv->pv_idx;
2039 pv->pv_idx = nidx;
2040 splx(s);
2041 }
2042
2043 /* Move any allocated tables back into the active pool. */
2044
2045 switch (llevel) {
2046 case NEWA:
2047 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
2048 /* FALLTHROUGH */
2049 case NEWB:
2050 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
2051 /* FALLTHROUGH */
2052 case NEWC:
2053 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
2054 /* FALLTHROUGH */
2055 default:
2056 break;
2057 }
2058
2059 return (KERN_SUCCESS);
2060 }
2061
2062 /* pmap_enter_kernel INTERNAL
2063 **
2064 * Map the given virtual address to the given physical address within the
2065 * kernel address space. This function exists because the kernel map does
2066 * not do dynamic table allocation. It consists of a contiguous array of ptes
2067 * and can be edited directly without the need to walk through any tables.
2068 *
2069 * XXX: "Danger, Will Robinson!"
2070 * Note that the kernel should never take a fault on any page
2071 * between [ KERNBASE .. virtual_avail ] and this is checked in
2072 * trap.c for kernel-mode MMU faults. This means that mappings
2073 * created in that range must be implicily wired. -gwr
2074 */
2075 void
2076 pmap_enter_kernel(va, pa, prot)
2077 vm_offset_t va;
2078 vm_offset_t pa;
2079 vm_prot_t prot;
2080 {
2081 boolean_t was_valid, insert;
2082 u_short pte_idx;
2083 int s, flags;
2084 mmu_short_pte_t *pte;
2085 pv_t *pv;
2086 vm_offset_t old_pa;
2087
2088 flags = (pa & ~MMU_PAGE_MASK);
2089 pa &= MMU_PAGE_MASK;
2090
2091 if (is_managed(pa))
2092 insert = TRUE;
2093 else
2094 insert = FALSE;
2095
2096 /*
2097 * Calculate the index of the PTE being modified.
2098 */
2099 pte_idx = (u_long) m68k_btop(va - KERNBASE);
2100
2101 /* This array is traditionally named "Sysmap" */
2102 pte = &kernCbase[pte_idx];
2103
2104 s = splimp();
2105 if (MMU_VALID_DT(*pte)) {
2106 was_valid = TRUE;
2107 /*
2108 * If the PTE already maps a different
2109 * physical address, umap and pv_unlink.
2110 */
2111 old_pa = MMU_PTE_PA(*pte);
2112 if (pa != old_pa)
2113 pmap_remove_pte(pte);
2114 else {
2115 /*
2116 * Old PA and new PA are the same. No need to
2117 * relink the mapping within the PV list.
2118 */
2119 insert = FALSE;
2120
2121 /*
2122 * Save any mod/ref bits on the PTE.
2123 */
2124 pte->attr.raw &= (MMU_SHORT_PTE_USED|MMU_SHORT_PTE_M);
2125 }
2126 } else {
2127 pte->attr.raw = MMU_DT_INVALID;
2128 was_valid = FALSE;
2129 }
2130
2131 /*
2132 * Map the page. Being careful to preserve modified/referenced bits
2133 * on the PTE.
2134 */
2135 pte->attr.raw |= (pa | MMU_DT_PAGE);
2136
2137 if (!(prot & VM_PROT_WRITE)) /* If access should be read-only */
2138 pte->attr.raw |= MMU_SHORT_PTE_WP;
2139 if (flags & PMAP_NC)
2140 pte->attr.raw |= MMU_SHORT_PTE_CI;
2141 if (was_valid)
2142 TBIS(va);
2143
2144 /*
2145 * Insert the PTE into the PV system, if need be.
2146 */
2147 if (insert) {
2148 pv = pa2pv(pa);
2149 pvebase[pte_idx].pve_next = pv->pv_idx;
2150 pv->pv_idx = pte_idx;
2151 }
2152 splx(s);
2153
2154 }
2155
2156 void
2157 pmap_kenter_pa(va, pa, prot)
2158 vaddr_t va;
2159 paddr_t pa;
2160 vm_prot_t prot;
2161 {
2162 pmap_enter(pmap_kernel(), va, pa, prot, PMAP_WIRED);
2163 }
2164
2165 void
2166 pmap_kenter_pgs(va, pgs, npgs)
2167 vaddr_t va;
2168 struct vm_page **pgs;
2169 int npgs;
2170 {
2171 int i;
2172
2173 for (i = 0; i < npgs; i++, va += PAGE_SIZE) {
2174 pmap_enter(pmap_kernel(), va, VM_PAGE_TO_PHYS(pgs[i]),
2175 VM_PROT_READ|VM_PROT_WRITE, PMAP_WIRED);
2176 }
2177 }
2178
2179 void
2180 pmap_kremove(va, len)
2181 vaddr_t va;
2182 vsize_t len;
2183 {
2184 for (len >>= PAGE_SHIFT; len > 0; len--, va += PAGE_SIZE) {
2185 pmap_remove(pmap_kernel(), va, va + PAGE_SIZE);
2186 }
2187 }
2188
2189 /* pmap_map INTERNAL
2190 **
2191 * Map a contiguous range of physical memory into a contiguous range of
2192 * the kernel virtual address space.
2193 *
2194 * Used for device mappings and early mapping of the kernel text/data/bss.
2195 * Returns the first virtual address beyond the end of the range.
2196 */
2197 vm_offset_t
2198 pmap_map(va, pa, endpa, prot)
2199 vm_offset_t va;
2200 vm_offset_t pa;
2201 vm_offset_t endpa;
2202 int prot;
2203 {
2204 int sz;
2205
2206 sz = endpa - pa;
2207 do {
2208 pmap_enter_kernel(va, pa, prot);
2209 va += NBPG;
2210 pa += NBPG;
2211 sz -= NBPG;
2212 } while (sz > 0);
2213 return(va);
2214 }
2215
2216 /* pmap_protect INTERFACE
2217 **
2218 * Apply the given protection to the given virtual address range within
2219 * the given map.
2220 *
2221 * It is ok for the protection applied to be stronger than what is
2222 * specified. We use this to our advantage when the given map has no
2223 * mapping for the virtual address. By skipping a page when this
2224 * is discovered, we are effectively applying a protection of VM_PROT_NONE,
2225 * and therefore do not need to map the page just to apply a protection
2226 * code. Only pmap_enter() needs to create new mappings if they do not exist.
2227 *
2228 * XXX - This function could be speeded up by using pmap_stroll() for inital
2229 * setup, and then manual scrolling in the for() loop.
2230 */
2231 void
2232 pmap_protect(pmap, startva, endva, prot)
2233 pmap_t pmap;
2234 vm_offset_t startva, endva;
2235 vm_prot_t prot;
2236 {
2237 boolean_t iscurpmap;
2238 int a_idx, b_idx, c_idx;
2239 a_tmgr_t *a_tbl;
2240 b_tmgr_t *b_tbl;
2241 c_tmgr_t *c_tbl;
2242 mmu_short_pte_t *pte;
2243
2244 if (pmap == NULL)
2245 return;
2246 if (pmap == pmap_kernel()) {
2247 pmap_protect_kernel(startva, endva, prot);
2248 return;
2249 }
2250
2251 /*
2252 * In this particular pmap implementation, there are only three
2253 * types of memory protection: 'all' (read/write/execute),
2254 * 'read-only' (read/execute) and 'none' (no mapping.)
2255 * It is not possible for us to treat 'executable' as a separate
2256 * protection type. Therefore, protection requests that seek to
2257 * remove execute permission while retaining read or write, and those
2258 * that make little sense (write-only for example) are ignored.
2259 */
2260 switch (prot) {
2261 case VM_PROT_NONE:
2262 /*
2263 * A request to apply the protection code of
2264 * 'VM_PROT_NONE' is a synonym for pmap_remove().
2265 */
2266 pmap_remove(pmap, startva, endva);
2267 return;
2268 case VM_PROT_EXECUTE:
2269 case VM_PROT_READ:
2270 case VM_PROT_READ|VM_PROT_EXECUTE:
2271 /* continue */
2272 break;
2273 case VM_PROT_WRITE:
2274 case VM_PROT_WRITE|VM_PROT_READ:
2275 case VM_PROT_WRITE|VM_PROT_EXECUTE:
2276 case VM_PROT_ALL:
2277 /* None of these should happen in a sane system. */
2278 return;
2279 }
2280
2281 /*
2282 * If the pmap has no A table, it has no mappings and therefore
2283 * there is nothing to protect.
2284 */
2285 if ((a_tbl = pmap->pm_a_tmgr) == NULL)
2286 return;
2287
2288 a_idx = MMU_TIA(startva);
2289 b_idx = MMU_TIB(startva);
2290 c_idx = MMU_TIC(startva);
2291 b_tbl = (b_tmgr_t *) c_tbl = NULL;
2292
2293 iscurpmap = (pmap == current_pmap());
2294 while (startva < endva) {
2295 if (b_tbl || MMU_VALID_DT(a_tbl->at_dtbl[a_idx])) {
2296 if (b_tbl == NULL) {
2297 b_tbl = (b_tmgr_t *) a_tbl->at_dtbl[a_idx].addr.raw;
2298 b_tbl = mmu_ptov((vm_offset_t) b_tbl);
2299 b_tbl = mmuB2tmgr((mmu_short_dte_t *) b_tbl);
2300 }
2301 if (c_tbl || MMU_VALID_DT(b_tbl->bt_dtbl[b_idx])) {
2302 if (c_tbl == NULL) {
2303 c_tbl = (c_tmgr_t *) MMU_DTE_PA(b_tbl->bt_dtbl[b_idx]);
2304 c_tbl = mmu_ptov((vm_offset_t) c_tbl);
2305 c_tbl = mmuC2tmgr((mmu_short_pte_t *) c_tbl);
2306 }
2307 if (MMU_VALID_DT(c_tbl->ct_dtbl[c_idx])) {
2308 pte = &c_tbl->ct_dtbl[c_idx];
2309 /* make the mapping read-only */
2310 pte->attr.raw |= MMU_SHORT_PTE_WP;
2311 /*
2312 * If we just modified the current address space,
2313 * flush any translations for the modified page from
2314 * the translation cache and any data from it in the
2315 * data cache.
2316 */
2317 if (iscurpmap)
2318 TBIS(startva);
2319 }
2320 startva += NBPG;
2321
2322 if (++c_idx >= MMU_C_TBL_SIZE) { /* exceeded C table? */
2323 c_tbl = NULL;
2324 c_idx = 0;
2325 if (++b_idx >= MMU_B_TBL_SIZE) { /* exceeded B table? */
2326 b_tbl = NULL;
2327 b_idx = 0;
2328 }
2329 }
2330 } else { /* C table wasn't valid */
2331 c_tbl = NULL;
2332 c_idx = 0;
2333 startva += MMU_TIB_RANGE;
2334 if (++b_idx >= MMU_B_TBL_SIZE) { /* exceeded B table? */
2335 b_tbl = NULL;
2336 b_idx = 0;
2337 }
2338 } /* C table */
2339 } else { /* B table wasn't valid */
2340 b_tbl = NULL;
2341 b_idx = 0;
2342 startva += MMU_TIA_RANGE;
2343 a_idx++;
2344 } /* B table */
2345 }
2346 }
2347
2348 /* pmap_protect_kernel INTERNAL
2349 **
2350 * Apply the given protection code to a kernel address range.
2351 */
2352 void
2353 pmap_protect_kernel(startva, endva, prot)
2354 vm_offset_t startva, endva;
2355 vm_prot_t prot;
2356 {
2357 vm_offset_t va;
2358 mmu_short_pte_t *pte;
2359
2360 pte = &kernCbase[(unsigned long) m68k_btop(startva - KERNBASE)];
2361 for (va = startva; va < endva; va += NBPG, pte++) {
2362 if (MMU_VALID_DT(*pte)) {
2363 switch (prot) {
2364 case VM_PROT_ALL:
2365 break;
2366 case VM_PROT_EXECUTE:
2367 case VM_PROT_READ:
2368 case VM_PROT_READ|VM_PROT_EXECUTE:
2369 pte->attr.raw |= MMU_SHORT_PTE_WP;
2370 break;
2371 case VM_PROT_NONE:
2372 /* this is an alias for 'pmap_remove_kernel' */
2373 pmap_remove_pte(pte);
2374 break;
2375 default:
2376 break;
2377 }
2378 /*
2379 * since this is the kernel, immediately flush any cached
2380 * descriptors for this address.
2381 */
2382 TBIS(va);
2383 }
2384 }
2385 }
2386
2387 /* pmap_unwire INTERFACE
2388 **
2389 * Clear the wired attribute of the specified page.
2390 *
2391 * This function is called from vm_fault.c to unwire
2392 * a mapping.
2393 */
2394 void
2395 pmap_unwire(pmap, va)
2396 pmap_t pmap;
2397 vm_offset_t va;
2398 {
2399 int a_idx, b_idx, c_idx;
2400 a_tmgr_t *a_tbl;
2401 b_tmgr_t *b_tbl;
2402 c_tmgr_t *c_tbl;
2403 mmu_short_pte_t *pte;
2404
2405 /* Kernel mappings always remain wired. */
2406 if (pmap == pmap_kernel())
2407 return;
2408
2409 /*
2410 * Walk through the tables. If the walk terminates without
2411 * a valid PTE then the address wasn't wired in the first place.
2412 * Return immediately.
2413 */
2414 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte, &a_idx,
2415 &b_idx, &c_idx) == FALSE)
2416 return;
2417
2418
2419 /* Is the PTE wired? If not, return. */
2420 if (!(pte->attr.raw & MMU_SHORT_PTE_WIRED))
2421 return;
2422
2423 /* Remove the wiring bit. */
2424 pte->attr.raw &= ~(MMU_SHORT_PTE_WIRED);
2425
2426 /*
2427 * Decrement the wired entry count in the C table.
2428 * If it reaches zero the following things happen:
2429 * 1. The table no longer has any wired entries and is considered
2430 * unwired.
2431 * 2. It is placed on the available queue.
2432 * 3. The parent table's wired entry count is decremented.
2433 * 4. If it reaches zero, this process repeats at step 1 and
2434 * stops at after reaching the A table.
2435 */
2436 if (--c_tbl->ct_wcnt == 0) {
2437 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
2438 if (--b_tbl->bt_wcnt == 0) {
2439 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
2440 if (--a_tbl->at_wcnt == 0) {
2441 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
2442 }
2443 }
2444 }
2445 }
2446
2447 /* pmap_copy INTERFACE
2448 **
2449 * Copy the mappings of a range of addresses in one pmap, into
2450 * the destination address of another.
2451 *
2452 * This routine is advisory. Should we one day decide that MMU tables
2453 * may be shared by more than one pmap, this function should be used to
2454 * link them together. Until that day however, we do nothing.
2455 */
2456 void
2457 pmap_copy(pmap_a, pmap_b, dst, len, src)
2458 pmap_t pmap_a, pmap_b;
2459 vm_offset_t dst;
2460 vm_size_t len;
2461 vm_offset_t src;
2462 {
2463 /* not implemented. */
2464 }
2465
2466 /* pmap_copy_page INTERFACE
2467 **
2468 * Copy the contents of one physical page into another.
2469 *
2470 * This function makes use of two virtual pages allocated in pmap_bootstrap()
2471 * to map the two specified physical pages into the kernel address space.
2472 *
2473 * Note: We could use the transparent translation registers to make the
2474 * mappings. If we do so, be sure to disable interrupts before using them.
2475 */
2476 void
2477 pmap_copy_page(srcpa, dstpa)
2478 vm_offset_t srcpa, dstpa;
2479 {
2480 vm_offset_t srcva, dstva;
2481 int s;
2482
2483 srcva = tmp_vpages[0];
2484 dstva = tmp_vpages[1];
2485
2486 s = splimp();
2487 if (tmp_vpages_inuse++)
2488 panic("pmap_copy_page: temporary vpages are in use.");
2489
2490 /* Map pages as non-cacheable to avoid cache polution? */
2491 pmap_enter_kernel(srcva, srcpa, VM_PROT_READ);
2492 pmap_enter_kernel(dstva, dstpa, VM_PROT_READ|VM_PROT_WRITE);
2493
2494 /* Hand-optimized version of bcopy(src, dst, NBPG) */
2495 copypage((char *) srcva, (char *) dstva);
2496
2497 pmap_remove_kernel(srcva, srcva + NBPG);
2498 pmap_remove_kernel(dstva, dstva + NBPG);
2499
2500 --tmp_vpages_inuse;
2501 splx(s);
2502 }
2503
2504 /* pmap_zero_page INTERFACE
2505 **
2506 * Zero the contents of the specified physical page.
2507 *
2508 * Uses one of the virtual pages allocated in pmap_boostrap()
2509 * to map the specified page into the kernel address space.
2510 */
2511 void
2512 pmap_zero_page(dstpa)
2513 vm_offset_t dstpa;
2514 {
2515 vm_offset_t dstva;
2516 int s;
2517
2518 dstva = tmp_vpages[1];
2519 s = splimp();
2520 if (tmp_vpages_inuse++)
2521 panic("pmap_zero_page: temporary vpages are in use.");
2522
2523 /* The comments in pmap_copy_page() above apply here also. */
2524 pmap_enter_kernel(dstva, dstpa, VM_PROT_READ|VM_PROT_WRITE);
2525
2526 /* Hand-optimized version of bzero(ptr, NBPG) */
2527 zeropage((char *) dstva);
2528
2529 pmap_remove_kernel(dstva, dstva + NBPG);
2530
2531 --tmp_vpages_inuse;
2532 splx(s);
2533 }
2534
2535 /* pmap_collect INTERFACE
2536 **
2537 * Called from the VM system when we are about to swap out
2538 * the process using this pmap. This should give up any
2539 * resources held here, including all its MMU tables.
2540 */
2541 void
2542 pmap_collect(pmap)
2543 pmap_t pmap;
2544 {
2545 /* XXX - todo... */
2546 }
2547
2548 /* pmap_create INTERFACE
2549 **
2550 * Create and return a pmap structure.
2551 */
2552 pmap_t
2553 pmap_create()
2554 {
2555 pmap_t pmap;
2556
2557 pmap = (pmap_t) malloc(sizeof(struct pmap), M_VMPMAP, M_WAITOK);
2558 pmap_pinit(pmap);
2559 return pmap;
2560 }
2561
2562 /* pmap_pinit INTERNAL
2563 **
2564 * Initialize a pmap structure.
2565 */
2566 void
2567 pmap_pinit(pmap)
2568 pmap_t pmap;
2569 {
2570 bzero(pmap, sizeof(struct pmap));
2571 pmap->pm_a_tmgr = NULL;
2572 pmap->pm_a_phys = kernAphys;
2573 pmap->pm_refcount = 1;
2574 simple_lock_init(&pmap->pm_lock);
2575 }
2576
2577 /* pmap_release INTERFACE
2578 **
2579 * Release any resources held by the given pmap.
2580 *
2581 * This is the reverse analog to pmap_pinit. It does not
2582 * necessarily mean for the pmap structure to be deallocated,
2583 * as in pmap_destroy.
2584 */
2585 void
2586 pmap_release(pmap)
2587 pmap_t pmap;
2588 {
2589 /*
2590 * As long as the pmap contains no mappings,
2591 * which always should be the case whenever
2592 * this function is called, there really should
2593 * be nothing to do.
2594 */
2595 #ifdef PMAP_DEBUG
2596 if (pmap == NULL)
2597 return;
2598 if (pmap == pmap_kernel())
2599 panic("pmap_release: kernel pmap");
2600 #endif
2601 /*
2602 * XXX - If this pmap has an A table, give it back.
2603 * The pmap SHOULD be empty by now, and pmap_remove
2604 * should have already given back the A table...
2605 * However, I see: pmap->pm_a_tmgr->at_ecnt == 1
2606 * at this point, which means some mapping was not
2607 * removed when it should have been. -gwr
2608 */
2609 if (pmap->pm_a_tmgr != NULL) {
2610 /* First make sure we are not using it! */
2611 if (kernel_crp.rp_addr == pmap->pm_a_phys) {
2612 kernel_crp.rp_addr = kernAphys;
2613 loadcrp(&kernel_crp);
2614 }
2615 #ifdef PMAP_DEBUG /* XXX - todo! */
2616 /* XXX - Now complain... */
2617 printf("pmap_release: still have table\n");
2618 Debugger();
2619 #endif
2620 free_a_table(pmap->pm_a_tmgr, TRUE);
2621 pmap->pm_a_tmgr = NULL;
2622 pmap->pm_a_phys = kernAphys;
2623 }
2624 }
2625
2626 /* pmap_reference INTERFACE
2627 **
2628 * Increment the reference count of a pmap.
2629 */
2630 void
2631 pmap_reference(pmap)
2632 pmap_t pmap;
2633 {
2634 if (pmap == NULL)
2635 return;
2636
2637 pmap_lock(pmap);
2638 pmap_add_ref(pmap);
2639 pmap_unlock(pmap);
2640 }
2641
2642 /* pmap_dereference INTERNAL
2643 **
2644 * Decrease the reference count on the given pmap
2645 * by one and return the current count.
2646 */
2647 int
2648 pmap_dereference(pmap)
2649 pmap_t pmap;
2650 {
2651 int rtn;
2652
2653 if (pmap == NULL)
2654 return 0;
2655
2656 pmap_lock(pmap);
2657 rtn = pmap_del_ref(pmap);
2658 pmap_unlock(pmap);
2659
2660 return rtn;
2661 }
2662
2663 /* pmap_destroy INTERFACE
2664 **
2665 * Decrement a pmap's reference count and delete
2666 * the pmap if it becomes zero. Will be called
2667 * only after all mappings have been removed.
2668 */
2669 void
2670 pmap_destroy(pmap)
2671 pmap_t pmap;
2672 {
2673 if (pmap == NULL)
2674 return;
2675 if (pmap == &kernel_pmap)
2676 panic("pmap_destroy: kernel_pmap!");
2677 if (pmap_dereference(pmap) == 0) {
2678 pmap_release(pmap);
2679 free(pmap, M_VMPMAP);
2680 }
2681 }
2682
2683 /* pmap_is_referenced INTERFACE
2684 **
2685 * Determine if the given physical page has been
2686 * referenced (read from [or written to.])
2687 */
2688 boolean_t
2689 pmap_is_referenced(pg)
2690 struct vm_page *pg;
2691 {
2692 paddr_t pa = VM_PAGE_TO_PHYS(pg);
2693 pv_t *pv;
2694 int idx, s;
2695
2696 if (!pv_initialized)
2697 return FALSE;
2698 /* XXX - this may be unecessary. */
2699 if (!is_managed(pa))
2700 return FALSE;
2701
2702 pv = pa2pv(pa);
2703 /*
2704 * Check the flags on the pv head. If they are set,
2705 * return immediately. Otherwise a search must be done.
2706 */
2707 if (pv->pv_flags & PV_FLAGS_USED)
2708 return TRUE;
2709
2710 s = splimp();
2711 /*
2712 * Search through all pv elements pointing
2713 * to this page and query their reference bits
2714 */
2715 for (idx = pv->pv_idx;
2716 idx != PVE_EOL;
2717 idx = pvebase[idx].pve_next) {
2718
2719 if (MMU_PTE_USED(kernCbase[idx])) {
2720 splx(s);
2721 return TRUE;
2722 }
2723 }
2724 splx(s);
2725
2726 return FALSE;
2727 }
2728
2729 /* pmap_is_modified INTERFACE
2730 **
2731 * Determine if the given physical page has been
2732 * modified (written to.)
2733 */
2734 boolean_t
2735 pmap_is_modified(pg)
2736 struct vm_page *pg;
2737 {
2738 paddr_t pa = VM_PAGE_TO_PHYS(pg);
2739 pv_t *pv;
2740 int idx, s;
2741
2742 if (!pv_initialized)
2743 return FALSE;
2744 /* XXX - this may be unecessary. */
2745 if (!is_managed(pa))
2746 return FALSE;
2747
2748 /* see comments in pmap_is_referenced() */
2749 pv = pa2pv(pa);
2750 if (pv->pv_flags & PV_FLAGS_MDFY)
2751 return TRUE;
2752
2753 s = splimp();
2754 for (idx = pv->pv_idx;
2755 idx != PVE_EOL;
2756 idx = pvebase[idx].pve_next) {
2757
2758 if (MMU_PTE_MODIFIED(kernCbase[idx])) {
2759 splx(s);
2760 return TRUE;
2761 }
2762 }
2763 splx(s);
2764
2765 return FALSE;
2766 }
2767
2768 /* pmap_page_protect INTERFACE
2769 **
2770 * Applies the given protection to all mappings to the given
2771 * physical page.
2772 */
2773 void
2774 pmap_page_protect(pg, prot)
2775 struct vm_page *pg;
2776 vm_prot_t prot;
2777 {
2778 paddr_t pa = VM_PAGE_TO_PHYS(pg);
2779 pv_t *pv;
2780 int idx, s;
2781 vm_offset_t va;
2782 struct mmu_short_pte_struct *pte;
2783 c_tmgr_t *c_tbl;
2784 pmap_t pmap, curpmap;
2785
2786 if (!is_managed(pa))
2787 return;
2788
2789 curpmap = current_pmap();
2790 pv = pa2pv(pa);
2791 s = splimp();
2792
2793 for (idx = pv->pv_idx;
2794 idx != PVE_EOL;
2795 idx = pvebase[idx].pve_next) {
2796
2797 pte = &kernCbase[idx];
2798 switch (prot) {
2799 case VM_PROT_ALL:
2800 /* do nothing */
2801 break;
2802 case VM_PROT_EXECUTE:
2803 case VM_PROT_READ:
2804 case VM_PROT_READ|VM_PROT_EXECUTE:
2805 /*
2806 * Determine the virtual address mapped by
2807 * the PTE and flush ATC entries if necessary.
2808 */
2809 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
2810 /* XXX don't write protect pager mappings */
2811 if (va >= PAGER_SVA && va < PAGER_EVA) {
2812 #ifdef PMAP_DEBUG
2813 /* XXX - Does this actually happen? */
2814 printf("pmap_page_protect: in pager!\n");
2815 Debugger();
2816 #endif
2817 } else
2818 pte->attr.raw |= MMU_SHORT_PTE_WP;
2819 if (pmap == curpmap || pmap == pmap_kernel())
2820 TBIS(va);
2821 break;
2822 case VM_PROT_NONE:
2823 /* Save the mod/ref bits. */
2824 pv->pv_flags |= pte->attr.raw;
2825 /* Invalidate the PTE. */
2826 pte->attr.raw = MMU_DT_INVALID;
2827
2828 /*
2829 * Update table counts. And flush ATC entries
2830 * if necessary.
2831 */
2832 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
2833
2834 /*
2835 * If the PTE belongs to the kernel map,
2836 * be sure to flush the page it maps.
2837 */
2838 if (pmap == pmap_kernel()) {
2839 TBIS(va);
2840 } else {
2841 /*
2842 * The PTE belongs to a user map.
2843 * update the entry count in the C
2844 * table to which it belongs and flush
2845 * the ATC if the mapping belongs to
2846 * the current pmap.
2847 */
2848 c_tbl->ct_ecnt--;
2849 if (pmap == curpmap)
2850 TBIS(va);
2851 }
2852 break;
2853 default:
2854 break;
2855 }
2856 }
2857
2858 /*
2859 * If the protection code indicates that all mappings to the page
2860 * be removed, truncate the PV list to zero entries.
2861 */
2862 if (prot == VM_PROT_NONE)
2863 pv->pv_idx = PVE_EOL;
2864 splx(s);
2865 }
2866
2867 /* pmap_get_pteinfo INTERNAL
2868 **
2869 * Called internally to find the pmap and virtual address within that
2870 * map to which the pte at the given index maps. Also includes the PTE's C
2871 * table manager.
2872 *
2873 * Returns the pmap in the argument provided, and the virtual address
2874 * by return value.
2875 */
2876 vm_offset_t
2877 pmap_get_pteinfo(idx, pmap, tbl)
2878 u_int idx;
2879 pmap_t *pmap;
2880 c_tmgr_t **tbl;
2881 {
2882 vm_offset_t va = 0;
2883
2884 /*
2885 * Determine if the PTE is a kernel PTE or a user PTE.
2886 */
2887 if (idx >= NUM_KERN_PTES) {
2888 /*
2889 * The PTE belongs to a user mapping.
2890 */
2891 /* XXX: Would like an inline for this to validate idx... */
2892 *tbl = &Ctmgrbase[(idx - NUM_KERN_PTES) / MMU_C_TBL_SIZE];
2893
2894 *pmap = (*tbl)->ct_pmap;
2895 /*
2896 * To find the va to which the PTE maps, we first take
2897 * the table's base virtual address mapping which is stored
2898 * in ct_va. We then increment this address by a page for
2899 * every slot skipped until we reach the PTE.
2900 */
2901 va = (*tbl)->ct_va;
2902 va += m68k_ptob(idx % MMU_C_TBL_SIZE);
2903 } else {
2904 /*
2905 * The PTE belongs to the kernel map.
2906 */
2907 *pmap = pmap_kernel();
2908
2909 va = m68k_ptob(idx);
2910 va += KERNBASE;
2911 }
2912
2913 return va;
2914 }
2915
2916 /* pmap_clear_modify INTERFACE
2917 **
2918 * Clear the modification bit on the page at the specified
2919 * physical address.
2920 *
2921 */
2922 boolean_t
2923 pmap_clear_modify(pg)
2924 struct vm_page *pg;
2925 {
2926 paddr_t pa = VM_PAGE_TO_PHYS(pg);
2927 boolean_t rv;
2928
2929 if (!is_managed(pa))
2930 return FALSE;
2931 rv = pmap_is_modified(pg);
2932 pmap_clear_pv(pa, PV_FLAGS_MDFY);
2933 return rv;
2934 }
2935
2936 /* pmap_clear_reference INTERFACE
2937 **
2938 * Clear the referenced bit on the page at the specified
2939 * physical address.
2940 */
2941 boolean_t
2942 pmap_clear_reference(pg)
2943 struct vm_page *pg;
2944 {
2945 paddr_t pa = VM_PAGE_TO_PHYS(pg);
2946 boolean_t rv;
2947
2948 if (!is_managed(pa))
2949 return FALSE;
2950 rv = pmap_is_referenced(pg);
2951 pmap_clear_pv(pa, PV_FLAGS_USED);
2952 return rv;
2953 }
2954
2955 /* pmap_clear_pv INTERNAL
2956 **
2957 * Clears the specified flag from the specified physical address.
2958 * (Used by pmap_clear_modify() and pmap_clear_reference().)
2959 *
2960 * Flag is one of:
2961 * PV_FLAGS_MDFY - Page modified bit.
2962 * PV_FLAGS_USED - Page used (referenced) bit.
2963 *
2964 * This routine must not only clear the flag on the pv list
2965 * head. It must also clear the bit on every pte in the pv
2966 * list associated with the address.
2967 */
2968 void
2969 pmap_clear_pv(pa, flag)
2970 vm_offset_t pa;
2971 int flag;
2972 {
2973 pv_t *pv;
2974 int idx, s;
2975 vm_offset_t va;
2976 pmap_t pmap;
2977 mmu_short_pte_t *pte;
2978 c_tmgr_t *c_tbl;
2979
2980 pv = pa2pv(pa);
2981
2982 s = splimp();
2983 pv->pv_flags &= ~(flag);
2984
2985 for (idx = pv->pv_idx;
2986 idx != PVE_EOL;
2987 idx = pvebase[idx].pve_next) {
2988
2989 pte = &kernCbase[idx];
2990 pte->attr.raw &= ~(flag);
2991 /*
2992 * The MC68030 MMU will not set the modified or
2993 * referenced bits on any MMU tables for which it has
2994 * a cached descriptor with its modify bit set. To insure
2995 * that it will modify these bits on the PTE during the next
2996 * time it is written to or read from, we must flush it from
2997 * the ATC.
2998 *
2999 * Ordinarily it is only necessary to flush the descriptor
3000 * if it is used in the current address space. But since I
3001 * am not sure that there will always be a notion of
3002 * 'the current address space' when this function is called,
3003 * I will skip the test and always flush the address. It
3004 * does no harm.
3005 */
3006 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
3007 TBIS(va);
3008 }
3009 splx(s);
3010 }
3011
3012 /* pmap_extract INTERFACE
3013 **
3014 * Return the physical address mapped by the virtual address
3015 * in the specified pmap.
3016 *
3017 * Note: this function should also apply an exclusive lock
3018 * on the pmap system during its duration.
3019 */
3020 boolean_t
3021 pmap_extract(pmap, va, pap)
3022 pmap_t pmap;
3023 vaddr_t va;
3024 paddr_t *pap;
3025 {
3026 int a_idx, b_idx, pte_idx;
3027 a_tmgr_t *a_tbl;
3028 b_tmgr_t *b_tbl;
3029 c_tmgr_t *c_tbl;
3030 mmu_short_pte_t *c_pte;
3031
3032 if (pmap == pmap_kernel())
3033 return pmap_extract_kernel(va, pap);
3034 if (pmap == NULL)
3035 return FALSE;
3036
3037 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl,
3038 &c_pte, &a_idx, &b_idx, &pte_idx) == FALSE)
3039 return FALSE;
3040
3041 if (!MMU_VALID_DT(*c_pte))
3042 return FALSE;
3043
3044 if (pap != NULL)
3045 *pap = MMU_PTE_PA(*c_pte);
3046 return (TRUE);
3047 }
3048
3049 /* pmap_extract_kernel INTERNAL
3050 **
3051 * Extract a translation from the kernel address space.
3052 */
3053 boolean_t
3054 pmap_extract_kernel(va, pap)
3055 vaddr_t va;
3056 paddr_t *pap;
3057 {
3058 mmu_short_pte_t *pte;
3059
3060 pte = &kernCbase[(u_int) m68k_btop(va - KERNBASE)];
3061 if (!MMU_VALID_DT(*pte))
3062 return (FALSE);
3063 if (pap != NULL)
3064 *pap = MMU_PTE_PA(*pte);
3065 return (TRUE);
3066 }
3067
3068 /* pmap_remove_kernel INTERNAL
3069 **
3070 * Remove the mapping of a range of virtual addresses from the kernel map.
3071 * The arguments are already page-aligned.
3072 */
3073 void
3074 pmap_remove_kernel(sva, eva)
3075 vm_offset_t sva;
3076 vm_offset_t eva;
3077 {
3078 int idx, eidx;
3079
3080 #ifdef PMAP_DEBUG
3081 if ((sva & PGOFSET) || (eva & PGOFSET))
3082 panic("pmap_remove_kernel: alignment");
3083 #endif
3084
3085 idx = m68k_btop(sva - KERNBASE);
3086 eidx = m68k_btop(eva - KERNBASE);
3087
3088 while (idx < eidx) {
3089 pmap_remove_pte(&kernCbase[idx++]);
3090 TBIS(sva);
3091 sva += NBPG;
3092 }
3093 }
3094
3095 /* pmap_remove INTERFACE
3096 **
3097 * Remove the mapping of a range of virtual addresses from the given pmap.
3098 *
3099 * If the range contains any wired entries, this function will probably create
3100 * disaster.
3101 */
3102 void
3103 pmap_remove(pmap, start, end)
3104 pmap_t pmap;
3105 vm_offset_t start;
3106 vm_offset_t end;
3107 {
3108
3109 if (pmap == pmap_kernel()) {
3110 pmap_remove_kernel(start, end);
3111 return;
3112 }
3113
3114 /*
3115 * XXX - Temporary(?) statement to prevent panic caused
3116 * by vm_alloc_with_pager() handing us a software map (ie NULL)
3117 * to remove because it couldn't get backing store.
3118 * (I guess.)
3119 */
3120 if (pmap == NULL)
3121 return;
3122
3123 /*
3124 * If the pmap doesn't have an A table of its own, it has no mappings
3125 * that can be removed.
3126 */
3127 if (pmap->pm_a_tmgr == NULL)
3128 return;
3129
3130 /*
3131 * Remove the specified range from the pmap. If the function
3132 * returns true, the operation removed all the valid mappings
3133 * in the pmap and freed its A table. If this happened to the
3134 * currently loaded pmap, the MMU root pointer must be reloaded
3135 * with the default 'kernel' map.
3136 */
3137 if (pmap_remove_a(pmap->pm_a_tmgr, start, end)) {
3138 if (kernel_crp.rp_addr == pmap->pm_a_phys) {
3139 kernel_crp.rp_addr = kernAphys;
3140 loadcrp(&kernel_crp);
3141 /* will do TLB flush below */
3142 }
3143 pmap->pm_a_tmgr = NULL;
3144 pmap->pm_a_phys = kernAphys;
3145 }
3146
3147 /*
3148 * If we just modified the current address space,
3149 * make sure to flush the MMU cache.
3150 *
3151 * XXX - this could be an unecessarily large flush.
3152 * XXX - Could decide, based on the size of the VA range
3153 * to be removed, whether to flush "by pages" or "all".
3154 */
3155 if (pmap == current_pmap())
3156 TBIAU();
3157 }
3158
3159 /* pmap_remove_a INTERNAL
3160 **
3161 * This is function number one in a set of three that removes a range
3162 * of memory in the most efficient manner by removing the highest possible
3163 * tables from the memory space. This particular function attempts to remove
3164 * as many B tables as it can, delegating the remaining fragmented ranges to
3165 * pmap_remove_b().
3166 *
3167 * If the removal operation results in an empty A table, the function returns
3168 * TRUE.
3169 *
3170 * It's ugly but will do for now.
3171 */
3172 boolean_t
3173 pmap_remove_a(a_tbl, start, end)
3174 a_tmgr_t *a_tbl;
3175 vm_offset_t start;
3176 vm_offset_t end;
3177 {
3178 boolean_t empty;
3179 int idx;
3180 vm_offset_t nstart, nend;
3181 b_tmgr_t *b_tbl;
3182 mmu_long_dte_t *a_dte;
3183 mmu_short_dte_t *b_dte;
3184
3185 /*
3186 * The following code works with what I call a 'granularity
3187 * reduction algorithim'. A range of addresses will always have
3188 * the following properties, which are classified according to
3189 * how the range relates to the size of the current granularity
3190 * - an A table entry:
3191 *
3192 * 1 2 3 4
3193 * -+---+---+---+---+---+---+---+-
3194 * -+---+---+---+---+---+---+---+-
3195 *
3196 * A range will always start on a granularity boundary, illustrated
3197 * by '+' signs in the table above, or it will start at some point
3198 * inbetween a granularity boundary, as illustrated by point 1.
3199 * The first step in removing a range of addresses is to remove the
3200 * range between 1 and 2, the nearest granularity boundary. This
3201 * job is handled by the section of code governed by the
3202 * 'if (start < nstart)' statement.
3203 *
3204 * A range will always encompass zero or more intergral granules,
3205 * illustrated by points 2 and 3. Integral granules are easy to
3206 * remove. The removal of these granules is the second step, and
3207 * is handled by the code block 'if (nstart < nend)'.
3208 *
3209 * Lastly, a range will always end on a granularity boundary,
3210 * ill. by point 3, or it will fall just beyond one, ill. by point
3211 * 4. The last step involves removing this range and is handled by
3212 * the code block 'if (nend < end)'.
3213 */
3214 nstart = MMU_ROUND_UP_A(start);
3215 nend = MMU_ROUND_A(end);
3216
3217 if (start < nstart) {
3218 /*
3219 * This block is executed if the range starts between
3220 * a granularity boundary.
3221 *
3222 * First find the DTE which is responsible for mapping
3223 * the start of the range.
3224 */
3225 idx = MMU_TIA(start);
3226 a_dte = &a_tbl->at_dtbl[idx];
3227
3228 /*
3229 * If the DTE is valid then delegate the removal of the sub
3230 * range to pmap_remove_b(), which can remove addresses at
3231 * a finer granularity.
3232 */
3233 if (MMU_VALID_DT(*a_dte)) {
3234 b_dte = mmu_ptov(a_dte->addr.raw);
3235 b_tbl = mmuB2tmgr(b_dte);
3236
3237 /*
3238 * The sub range to be removed starts at the start
3239 * of the full range we were asked to remove, and ends
3240 * at the greater of:
3241 * 1. The end of the full range, -or-
3242 * 2. The end of the full range, rounded down to the
3243 * nearest granularity boundary.
3244 */
3245 if (end < nstart)
3246 empty = pmap_remove_b(b_tbl, start, end);
3247 else
3248 empty = pmap_remove_b(b_tbl, start, nstart);
3249
3250 /*
3251 * If the removal resulted in an empty B table,
3252 * invalidate the DTE that points to it and decrement
3253 * the valid entry count of the A table.
3254 */
3255 if (empty) {
3256 a_dte->attr.raw = MMU_DT_INVALID;
3257 a_tbl->at_ecnt--;
3258 }
3259 }
3260 /*
3261 * If the DTE is invalid, the address range is already non-
3262 * existant and can simply be skipped.
3263 */
3264 }
3265 if (nstart < nend) {
3266 /*
3267 * This block is executed if the range spans a whole number
3268 * multiple of granules (A table entries.)
3269 *
3270 * First find the DTE which is responsible for mapping
3271 * the start of the first granule involved.
3272 */
3273 idx = MMU_TIA(nstart);
3274 a_dte = &a_tbl->at_dtbl[idx];
3275
3276 /*
3277 * Remove entire sub-granules (B tables) one at a time,
3278 * until reaching the end of the range.
3279 */
3280 for (; nstart < nend; a_dte++, nstart += MMU_TIA_RANGE)
3281 if (MMU_VALID_DT(*a_dte)) {
3282 /*
3283 * Find the B table manager for the
3284 * entry and free it.
3285 */
3286 b_dte = mmu_ptov(a_dte->addr.raw);
3287 b_tbl = mmuB2tmgr(b_dte);
3288 free_b_table(b_tbl, TRUE);
3289
3290 /*
3291 * Invalidate the DTE that points to the
3292 * B table and decrement the valid entry
3293 * count of the A table.
3294 */
3295 a_dte->attr.raw = MMU_DT_INVALID;
3296 a_tbl->at_ecnt--;
3297 }
3298 }
3299 if (nend < end) {
3300 /*
3301 * This block is executed if the range ends beyond a
3302 * granularity boundary.
3303 *
3304 * First find the DTE which is responsible for mapping
3305 * the start of the nearest (rounded down) granularity
3306 * boundary.
3307 */
3308 idx = MMU_TIA(nend);
3309 a_dte = &a_tbl->at_dtbl[idx];
3310
3311 /*
3312 * If the DTE is valid then delegate the removal of the sub
3313 * range to pmap_remove_b(), which can remove addresses at
3314 * a finer granularity.
3315 */
3316 if (MMU_VALID_DT(*a_dte)) {
3317 /*
3318 * Find the B table manager for the entry
3319 * and hand it to pmap_remove_b() along with
3320 * the sub range.
3321 */
3322 b_dte = mmu_ptov(a_dte->addr.raw);
3323 b_tbl = mmuB2tmgr(b_dte);
3324
3325 empty = pmap_remove_b(b_tbl, nend, end);
3326
3327 /*
3328 * If the removal resulted in an empty B table,
3329 * invalidate the DTE that points to it and decrement
3330 * the valid entry count of the A table.
3331 */
3332 if (empty) {
3333 a_dte->attr.raw = MMU_DT_INVALID;
3334 a_tbl->at_ecnt--;
3335 }
3336 }
3337 }
3338
3339 /*
3340 * If there are no more entries in the A table, release it
3341 * back to the available pool and return TRUE.
3342 */
3343 if (a_tbl->at_ecnt == 0) {
3344 a_tbl->at_parent = NULL;
3345 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
3346 TAILQ_INSERT_HEAD(&a_pool, a_tbl, at_link);
3347 empty = TRUE;
3348 } else {
3349 empty = FALSE;
3350 }
3351
3352 return empty;
3353 }
3354
3355 /* pmap_remove_b INTERNAL
3356 **
3357 * Remove a range of addresses from an address space, trying to remove entire
3358 * C tables if possible.
3359 *
3360 * If the operation results in an empty B table, the function returns TRUE.
3361 */
3362 boolean_t
3363 pmap_remove_b(b_tbl, start, end)
3364 b_tmgr_t *b_tbl;
3365 vm_offset_t start;
3366 vm_offset_t end;
3367 {
3368 boolean_t empty;
3369 int idx;
3370 vm_offset_t nstart, nend, rstart;
3371 c_tmgr_t *c_tbl;
3372 mmu_short_dte_t *b_dte;
3373 mmu_short_pte_t *c_dte;
3374
3375
3376 nstart = MMU_ROUND_UP_B(start);
3377 nend = MMU_ROUND_B(end);
3378
3379 if (start < nstart) {
3380 idx = MMU_TIB(start);
3381 b_dte = &b_tbl->bt_dtbl[idx];
3382 if (MMU_VALID_DT(*b_dte)) {
3383 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte));
3384 c_tbl = mmuC2tmgr(c_dte);
3385 if (end < nstart)
3386 empty = pmap_remove_c(c_tbl, start, end);
3387 else
3388 empty = pmap_remove_c(c_tbl, start, nstart);
3389 if (empty) {
3390 b_dte->attr.raw = MMU_DT_INVALID;
3391 b_tbl->bt_ecnt--;
3392 }
3393 }
3394 }
3395 if (nstart < nend) {
3396 idx = MMU_TIB(nstart);
3397 b_dte = &b_tbl->bt_dtbl[idx];
3398 rstart = nstart;
3399 while (rstart < nend) {
3400 if (MMU_VALID_DT(*b_dte)) {
3401 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte));
3402 c_tbl = mmuC2tmgr(c_dte);
3403 free_c_table(c_tbl, TRUE);
3404 b_dte->attr.raw = MMU_DT_INVALID;
3405 b_tbl->bt_ecnt--;
3406 }
3407 b_dte++;
3408 rstart += MMU_TIB_RANGE;
3409 }
3410 }
3411 if (nend < end) {
3412 idx = MMU_TIB(nend);
3413 b_dte = &b_tbl->bt_dtbl[idx];
3414 if (MMU_VALID_DT(*b_dte)) {
3415 c_dte = mmu_ptov(MMU_DTE_PA(*b_dte));
3416 c_tbl = mmuC2tmgr(c_dte);
3417 empty = pmap_remove_c(c_tbl, nend, end);
3418 if (empty) {
3419 b_dte->attr.raw = MMU_DT_INVALID;
3420 b_tbl->bt_ecnt--;
3421 }
3422 }
3423 }
3424
3425 if (b_tbl->bt_ecnt == 0) {
3426 b_tbl->bt_parent = NULL;
3427 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
3428 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link);
3429 empty = TRUE;
3430 } else {
3431 empty = FALSE;
3432 }
3433
3434 return empty;
3435 }
3436
3437 /* pmap_remove_c INTERNAL
3438 **
3439 * Remove a range of addresses from the given C table.
3440 */
3441 boolean_t
3442 pmap_remove_c(c_tbl, start, end)
3443 c_tmgr_t *c_tbl;
3444 vm_offset_t start;
3445 vm_offset_t end;
3446 {
3447 boolean_t empty;
3448 int idx;
3449 mmu_short_pte_t *c_pte;
3450
3451 idx = MMU_TIC(start);
3452 c_pte = &c_tbl->ct_dtbl[idx];
3453 for (;start < end; start += MMU_PAGE_SIZE, c_pte++) {
3454 if (MMU_VALID_DT(*c_pte)) {
3455 pmap_remove_pte(c_pte);
3456 c_tbl->ct_ecnt--;
3457 }
3458 }
3459
3460 if (c_tbl->ct_ecnt == 0) {
3461 c_tbl->ct_parent = NULL;
3462 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
3463 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
3464 empty = TRUE;
3465 } else {
3466 empty = FALSE;
3467 }
3468
3469 return empty;
3470 }
3471
3472 /* is_managed INTERNAL
3473 **
3474 * Determine if the given physical address is managed by the PV system.
3475 * Note that this logic assumes that no one will ask for the status of
3476 * addresses which lie in-between the memory banks on the 3/80. If they
3477 * do so, it will falsely report that it is managed.
3478 *
3479 * Note: A "managed" address is one that was reported to the VM system as
3480 * a "usable page" during system startup. As such, the VM system expects the
3481 * pmap module to keep an accurate track of the useage of those pages.
3482 * Any page not given to the VM system at startup does not exist (as far as
3483 * the VM system is concerned) and is therefore "unmanaged." Examples are
3484 * those pages which belong to the ROM monitor and the memory allocated before
3485 * the VM system was started.
3486 */
3487 boolean_t
3488 is_managed(pa)
3489 vm_offset_t pa;
3490 {
3491 if (pa >= avail_start && pa < avail_end)
3492 return TRUE;
3493 else
3494 return FALSE;
3495 }
3496
3497 /* pmap_bootstrap_alloc INTERNAL
3498 **
3499 * Used internally for memory allocation at startup when malloc is not
3500 * available. This code will fail once it crosses the first memory
3501 * bank boundary on the 3/80. Hopefully by then however, the VM system
3502 * will be in charge of allocation.
3503 */
3504 void *
3505 pmap_bootstrap_alloc(size)
3506 int size;
3507 {
3508 void *rtn;
3509
3510 #ifdef PMAP_DEBUG
3511 if (bootstrap_alloc_enabled == FALSE) {
3512 mon_printf("pmap_bootstrap_alloc: disabled\n");
3513 sunmon_abort();
3514 }
3515 #endif
3516
3517 rtn = (void *) virtual_avail;
3518 virtual_avail += size;
3519
3520 #ifdef PMAP_DEBUG
3521 if (virtual_avail > virtual_contig_end) {
3522 mon_printf("pmap_bootstrap_alloc: out of mem\n");
3523 sunmon_abort();
3524 }
3525 #endif
3526
3527 return rtn;
3528 }
3529
3530 /* pmap_bootstap_aalign INTERNAL
3531 **
3532 * Used to insure that the next call to pmap_bootstrap_alloc() will
3533 * return a chunk of memory aligned to the specified size.
3534 *
3535 * Note: This function will only support alignment sizes that are powers
3536 * of two.
3537 */
3538 void
3539 pmap_bootstrap_aalign(size)
3540 int size;
3541 {
3542 int off;
3543
3544 off = virtual_avail & (size - 1);
3545 if (off) {
3546 (void) pmap_bootstrap_alloc(size - off);
3547 }
3548 }
3549
3550 /* pmap_pa_exists
3551 **
3552 * Used by the /dev/mem driver to see if a given PA is memory
3553 * that can be mapped. (The PA is not in a hole.)
3554 */
3555 int
3556 pmap_pa_exists(pa)
3557 vm_offset_t pa;
3558 {
3559 register int i;
3560
3561 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) {
3562 if ((pa >= avail_mem[i].pmem_start) &&
3563 (pa < avail_mem[i].pmem_end))
3564 return (1);
3565 if (avail_mem[i].pmem_next == NULL)
3566 break;
3567 }
3568 return (0);
3569 }
3570
3571 /* Called only from locore.s and pmap.c */
3572 void _pmap_switch __P((pmap_t pmap));
3573
3574 /*
3575 * _pmap_switch INTERNAL
3576 *
3577 * This is called by locore.s:cpu_switch() when it is
3578 * switching to a new process. Load new translations.
3579 * Note: done in-line by locore.s unless PMAP_DEBUG
3580 *
3581 * Note that we do NOT allocate a context here, but
3582 * share the "kernel only" context until we really
3583 * need our own context for user-space mappings in
3584 * pmap_enter_user(). [ s/context/mmu A table/ ]
3585 */
3586 void
3587 _pmap_switch(pmap)
3588 pmap_t pmap;
3589 {
3590 u_long rootpa;
3591
3592 /*
3593 * Only do reload/flush if we have to.
3594 * Note that if the old and new process
3595 * were BOTH using the "null" context,
3596 * then this will NOT flush the TLB.
3597 */
3598 rootpa = pmap->pm_a_phys;
3599 if (kernel_crp.rp_addr != rootpa) {
3600 DPRINT(("pmap_activate(%p)\n", pmap));
3601 kernel_crp.rp_addr = rootpa;
3602 loadcrp(&kernel_crp);
3603 TBIAU();
3604 }
3605 }
3606
3607 /*
3608 * Exported version of pmap_activate(). This is called from the
3609 * machine-independent VM code when a process is given a new pmap.
3610 * If (p == curproc) do like cpu_switch would do; otherwise just
3611 * take this as notification that the process has a new pmap.
3612 */
3613 void
3614 pmap_activate(p)
3615 struct proc *p;
3616 {
3617 pmap_t pmap = p->p_vmspace->vm_map.pmap;
3618 int s;
3619
3620 if (p == curproc) {
3621 s = splimp();
3622 _pmap_switch(pmap);
3623 splx(s);
3624 }
3625 }
3626
3627 /*
3628 * pmap_deactivate INTERFACE
3629 **
3630 * This is called to deactivate the specified process's address space.
3631 * XXX The semantics of this function are currently not well-defined.
3632 */
3633 void
3634 pmap_deactivate(p)
3635 struct proc *p;
3636 {
3637 /* not implemented. */
3638 }
3639
3640 /* pmap_update
3641 **
3642 * Apply any delayed changes scheduled for all pmaps immediately.
3643 *
3644 * No delayed operations are currently done in this pmap.
3645 */
3646 void
3647 pmap_update()
3648 {
3649 /* not implemented. */
3650 }
3651
3652 /*
3653 * Fill in the sun3x-specific part of the kernel core header
3654 * for dumpsys(). (See machdep.c for the rest.)
3655 */
3656 void
3657 pmap_kcore_hdr(sh)
3658 struct sun3x_kcore_hdr *sh;
3659 {
3660 u_long spa, len;
3661 int i;
3662
3663 sh->pg_frame = MMU_SHORT_PTE_BASEADDR;
3664 sh->pg_valid = MMU_DT_PAGE;
3665 sh->contig_end = virtual_contig_end;
3666 sh->kernCbase = (u_long) kernCbase;
3667 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) {
3668 spa = avail_mem[i].pmem_start;
3669 spa = m68k_trunc_page(spa);
3670 len = avail_mem[i].pmem_end - spa;
3671 len = m68k_round_page(len);
3672 sh->ram_segs[i].start = spa;
3673 sh->ram_segs[i].size = len;
3674 }
3675 }
3676
3677
3678 /* pmap_virtual_space INTERFACE
3679 **
3680 * Return the current available range of virtual addresses in the
3681 * arguuments provided. Only really called once.
3682 */
3683 void
3684 pmap_virtual_space(vstart, vend)
3685 vm_offset_t *vstart, *vend;
3686 {
3687 *vstart = virtual_avail;
3688 *vend = virtual_end;
3689 }
3690
3691 /*
3692 * Provide memory to the VM system.
3693 *
3694 * Assume avail_start is always in the
3695 * first segment as pmap_bootstrap does.
3696 */
3697 static void
3698 pmap_page_upload()
3699 {
3700 vm_offset_t a, b; /* memory range */
3701 int i;
3702
3703 /* Supply the memory in segments. */
3704 for (i = 0; i < SUN3X_NPHYS_RAM_SEGS; i++) {
3705 a = atop(avail_mem[i].pmem_start);
3706 b = atop(avail_mem[i].pmem_end);
3707 if (i == 0)
3708 a = atop(avail_start);
3709
3710 uvm_page_physload(a, b, a, b, VM_FREELIST_DEFAULT);
3711
3712 if (avail_mem[i].pmem_next == NULL)
3713 break;
3714 }
3715 }
3716
3717 /* pmap_page_index INTERFACE
3718 **
3719 * Return the index of the given physical page in a list of useable
3720 * physical pages in the system. Holes in physical memory may be counted
3721 * if so desired. As long as pmap_free_pages() and pmap_page_index()
3722 * agree as to whether holes in memory do or do not count as valid pages,
3723 * it really doesn't matter. However, if you like to save a little
3724 * memory, don't count holes as valid pages. This is even more true when
3725 * the holes are large.
3726 *
3727 * We will not count holes as valid pages. We can generate page indices
3728 * that conform to this by using the memory bank structures initialized
3729 * in pmap_alloc_pv().
3730 */
3731 int
3732 pmap_page_index(pa)
3733 vm_offset_t pa;
3734 {
3735 struct pmap_physmem_struct *bank = avail_mem;
3736 vm_offset_t off;
3737
3738 /* Search for the memory bank with this page. */
3739 /* XXX - What if it is not physical memory? */
3740 while (pa > bank->pmem_end)
3741 bank = bank->pmem_next;
3742 off = pa - bank->pmem_start;
3743
3744 return (bank->pmem_pvbase + m68k_btop(off));
3745 }
3746
3747 /* pmap_count INTERFACE
3748 **
3749 * Return the number of resident (valid) pages in the given pmap.
3750 *
3751 * Note: If this function is handed the kernel map, it will report
3752 * that it has no mappings. Hopefully the VM system won't ask for kernel
3753 * map statistics.
3754 */
3755 segsz_t
3756 pmap_count(pmap, type)
3757 pmap_t pmap;
3758 int type;
3759 {
3760 u_int count;
3761 int a_idx, b_idx;
3762 a_tmgr_t *a_tbl;
3763 b_tmgr_t *b_tbl;
3764 c_tmgr_t *c_tbl;
3765
3766 /*
3767 * If the pmap does not have its own A table manager, it has no
3768 * valid entires.
3769 */
3770 if (pmap->pm_a_tmgr == NULL)
3771 return 0;
3772
3773 a_tbl = pmap->pm_a_tmgr;
3774
3775 count = 0;
3776 for (a_idx = 0; a_idx < MMU_TIA(KERNBASE); a_idx++) {
3777 if (MMU_VALID_DT(a_tbl->at_dtbl[a_idx])) {
3778 b_tbl = mmuB2tmgr(mmu_ptov(a_tbl->at_dtbl[a_idx].addr.raw));
3779 for (b_idx = 0; b_idx < MMU_B_TBL_SIZE; b_idx++) {
3780 if (MMU_VALID_DT(b_tbl->bt_dtbl[b_idx])) {
3781 c_tbl = mmuC2tmgr(
3782 mmu_ptov(MMU_DTE_PA(b_tbl->bt_dtbl[b_idx])));
3783 if (type == 0)
3784 /*
3785 * A resident entry count has been requested.
3786 */
3787 count += c_tbl->ct_ecnt;
3788 else
3789 /*
3790 * A wired entry count has been requested.
3791 */
3792 count += c_tbl->ct_wcnt;
3793 }
3794 }
3795 }
3796 }
3797
3798 return count;
3799 }
3800
3801 /************************ SUN3 COMPATIBILITY ROUTINES ********************
3802 * The following routines are only used by DDB for tricky kernel text *
3803 * text operations in db_memrw.c. They are provided for sun3 *
3804 * compatibility. *
3805 *************************************************************************/
3806 /* get_pte INTERNAL
3807 **
3808 * Return the page descriptor the describes the kernel mapping
3809 * of the given virtual address.
3810 */
3811 extern u_long ptest_addr __P((u_long)); /* XXX: locore.s */
3812 u_int
3813 get_pte(va)
3814 vm_offset_t va;
3815 {
3816 u_long pte_pa;
3817 mmu_short_pte_t *pte;
3818
3819 /* Get the physical address of the PTE */
3820 pte_pa = ptest_addr(va & ~PGOFSET);
3821
3822 /* Convert to a virtual address... */
3823 pte = (mmu_short_pte_t *) (KERNBASE + pte_pa);
3824
3825 /* Make sure it is in our level-C tables... */
3826 if ((pte < kernCbase) ||
3827 (pte >= &mmuCbase[NUM_USER_PTES]))
3828 return 0;
3829
3830 /* ... and just return its contents. */
3831 return (pte->attr.raw);
3832 }
3833
3834
3835 /* set_pte INTERNAL
3836 **
3837 * Set the page descriptor that describes the kernel mapping
3838 * of the given virtual address.
3839 */
3840 void
3841 set_pte(va, pte)
3842 vm_offset_t va;
3843 u_int pte;
3844 {
3845 u_long idx;
3846
3847 if (va < KERNBASE)
3848 return;
3849
3850 idx = (unsigned long) m68k_btop(va - KERNBASE);
3851 kernCbase[idx].attr.raw = pte;
3852 TBIS(va);
3853 }
3854
3855 /*
3856 * Routine: pmap_procwr
3857 *
3858 * Function:
3859 * Synchronize caches corresponding to [addr, addr+len) in p.
3860 */
3861 void
3862 pmap_procwr(p, va, len)
3863 struct proc *p;
3864 vaddr_t va;
3865 size_t len;
3866 {
3867 (void)cachectl1(0x80000004, va, len, p);
3868 }
3869
3870
3871 #ifdef PMAP_DEBUG
3872 /************************** DEBUGGING ROUTINES **************************
3873 * The following routines are meant to be an aid to debugging the pmap *
3874 * system. They are callable from the DDB command line and should be *
3875 * prepared to be handed unstable or incomplete states of the system. *
3876 ************************************************************************/
3877
3878 /* pv_list
3879 **
3880 * List all pages found on the pv list for the given physical page.
3881 * To avoid endless loops, the listing will stop at the end of the list
3882 * or after 'n' entries - whichever comes first.
3883 */
3884 void
3885 pv_list(pa, n)
3886 vm_offset_t pa;
3887 int n;
3888 {
3889 int idx;
3890 vm_offset_t va;
3891 pv_t *pv;
3892 c_tmgr_t *c_tbl;
3893 pmap_t pmap;
3894
3895 pv = pa2pv(pa);
3896 idx = pv->pv_idx;
3897
3898 for (;idx != PVE_EOL && n > 0;
3899 idx=pvebase[idx].pve_next, n--) {
3900
3901 va = pmap_get_pteinfo(idx, &pmap, &c_tbl);
3902 printf("idx %d, pmap 0x%x, va 0x%x, c_tbl %x\n",
3903 idx, (u_int) pmap, (u_int) va, (u_int) c_tbl);
3904 }
3905 }
3906 #endif /* PMAP_DEBUG */
3907
3908 #ifdef NOT_YET
3909 /* and maybe not ever */
3910 /************************** LOW-LEVEL ROUTINES **************************
3911 * These routines will eventualy be re-written into assembly and placed *
3912 * in locore.s. They are here now as stubs so that the pmap module can *
3913 * be linked as a standalone user program for testing. *
3914 ************************************************************************/
3915 /* flush_atc_crp INTERNAL
3916 **
3917 * Flush all page descriptors derived from the given CPU Root Pointer
3918 * (CRP), or 'A' table as it is known here, from the 68851's automatic
3919 * cache.
3920 */
3921 void
3922 flush_atc_crp(a_tbl)
3923 {
3924 mmu_long_rp_t rp;
3925
3926 /* Create a temporary root table pointer that points to the
3927 * given A table.
3928 */
3929 rp.attr.raw = ~MMU_LONG_RP_LU;
3930 rp.addr.raw = (unsigned int) a_tbl;
3931
3932 mmu_pflushr(&rp);
3933 /* mmu_pflushr:
3934 * movel sp(4)@,a0
3935 * pflushr a0@
3936 * rts
3937 */
3938 }
3939 #endif /* NOT_YET */
3940