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