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