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