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