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