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