pmap.c revision 1.3 1 /* $NetBSD: pmap.c,v 1.3 1997/01/16 22:12:50 gwr Exp $ */
2
3 /*-
4 * Copyright (c) 1996 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 previous note
111 * does not apply to the sun3x pmap.
112 */
113
114 #include <sys/param.h>
115 #include <sys/systm.h>
116 #include <sys/proc.h>
117 #include <sys/malloc.h>
118 #include <sys/user.h>
119 #include <sys/queue.h>
120
121 #include <vm/vm.h>
122 #include <vm/vm_kern.h>
123 #include <vm/vm_page.h>
124
125 #include <machine/cpu.h>
126 #include <machine/pmap.h>
127 #include <machine/pte.h>
128 #include <machine/mon.h>
129
130 #include "machdep.h"
131 #include "pmap_pvt.h"
132
133 /* XXX - What headers declare these? */
134 extern struct pcb *curpcb;
135 extern int physmem;
136
137 /* Defined in locore.s */
138 extern char kernel_text[];
139
140 /* Defined by the linker */
141 extern char etext[], edata[], end[];
142 extern char *esym; /* DDB */
143
144 /*
145 * I think it might be cleaner to have one of these in each of
146 * the a_tmgr_t structures... -gwr
147 */
148 struct mmu_rootptr proc0crp;
149
150 /* This is set by locore.s with the monitor's root ptr. */
151 extern struct mmu_rootptr mon_crp;
152
153 /*** Management Structure - Memory Layout
154 * For every MMU table in the sun3x pmap system there must be a way to
155 * manage it; we must know which process is using it, what other tables
156 * depend on it, and whether or not it contains any locked pages. This
157 * is solved by the creation of 'table management' or 'tmgr'
158 * structures. One for each MMU table in the system.
159 *
160 * MAP OF MEMORY USED BY THE PMAP SYSTEM
161 *
162 * towards lower memory
163 * kernAbase -> +-------------------------------------------------------+
164 * | Kernel MMU A level table |
165 * kernBbase -> +-------------------------------------------------------+
166 * | Kernel MMU B level tables |
167 * kernCbase -> +-------------------------------------------------------+
168 * | |
169 * | Kernel MMU C level tables |
170 * | |
171 * mmuAbase -> +-------------------------------------------------------+
172 * | |
173 * | User MMU A level tables |
174 * | |
175 * mmuBbase -> +-------------------------------------------------------+
176 * | User MMU B level tables |
177 * mmuCbase -> +-------------------------------------------------------+
178 * | User MMU C level tables |
179 * tmgrAbase -> +-------------------------------------------------------+
180 * | TMGR A level table structures |
181 * tmgrBbase -> +-------------------------------------------------------+
182 * | TMGR B level table structures |
183 * tmgrCbase -> +-------------------------------------------------------+
184 * | TMGR C level table structures |
185 * pvbase -> +-------------------------------------------------------+
186 * | Physical to Virtual mapping table (list heads) |
187 * pvebase -> +-------------------------------------------------------+
188 * | Physical to Virtual mapping table (list elements) |
189 * | |
190 * +-------------------------------------------------------+
191 * towards higher memory
192 *
193 * For every A table in the MMU A area, there will be a corresponding
194 * a_tmgr structure in the TMGR A area. The same will be true for
195 * the B and C tables. This arrangement will make it easy to find the
196 * controling tmgr structure for any table in the system by use of
197 * (relatively) simple macros.
198 */
199 /* Global variables for storing the base addresses for the areas
200 * labeled above.
201 */
202 static mmu_long_dte_t *kernAbase;
203 static mmu_short_dte_t *kernBbase;
204 static mmu_short_pte_t *kernCbase;
205 static mmu_long_dte_t *mmuAbase;
206 static mmu_short_dte_t *mmuBbase;
207 static mmu_short_pte_t *mmuCbase;
208 static a_tmgr_t *Atmgrbase;
209 static b_tmgr_t *Btmgrbase;
210 static c_tmgr_t *Ctmgrbase;
211 static pv_t *pvbase;
212 static pv_elem_t *pvebase;
213
214 /* Just all around global variables.
215 */
216 static TAILQ_HEAD(a_pool_head_struct, a_tmgr_struct) a_pool;
217 static TAILQ_HEAD(b_pool_head_struct, b_tmgr_struct) b_pool;
218 static TAILQ_HEAD(c_pool_head_struct, c_tmgr_struct) c_pool;
219 struct pmap kernel_pmap;
220 static a_tmgr_t *proc0Atmgr;
221 a_tmgr_t *curatbl;
222 static boolean_t pv_initialized = 0;
223 static vm_offset_t last_mapped = 0;
224 int tmp_vpages_inuse = 0;
225
226 /*
227 * XXX: For now, retain the traditional variables that were
228 * used in the old pmap/vm interface (without NONCONTIG).
229 */
230 /* Kernel virtual address space available: */
231 vm_offset_t virtual_avail, virtual_end;
232 /* Physical address space available: */
233 vm_offset_t avail_start, avail_end;
234
235 vm_offset_t tmp_vpages[2];
236
237
238 /* The 3/80 is the only member of the sun3x family that has non-contiguous
239 * physical memory. Memory is divided into 4 banks which are physically
240 * locatable on the system board. Although the size of these banks varies
241 * with the size of memory they contain, their base addresses are
242 * permenently fixed. The following structure, which describes these
243 * banks, is initialized by pmap_bootstrap() after it reads from a similar
244 * structure provided by the ROM Monitor.
245 *
246 * For the other machines in the sun3x architecture which do have contiguous
247 * RAM, this list will have only one entry, which will describe the entire
248 * range of available memory.
249 */
250 struct pmap_physmem_struct avail_mem[SUN3X_80_MEM_BANKS];
251 u_int total_phys_mem;
252
253 /* These macros map MMU tables to their corresponding manager structures.
254 * They are needed quite often because many of the pointers in the pmap
255 * system reference MMU tables and not the structures that control them.
256 * There needs to be a way to find one when given the other and these
257 * macros do so by taking advantage of the memory layout described above.
258 * Here's a quick step through the first macro, mmuA2tmgr():
259 *
260 * 1) find the offset of the given MMU A table from the base of its table
261 * pool (table - mmuAbase).
262 * 2) convert this offset into a table index by dividing it by the
263 * size of one MMU 'A' table. (sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE)
264 * 3) use this index to select the corresponding 'A' table manager
265 * structure from the 'A' table manager pool (Atmgrbase[index]).
266 */
267 #define mmuA2tmgr(table) \
268 (&Atmgrbase[\
269 ((mmu_long_dte_t *)(table) - mmuAbase)\
270 / MMU_A_TBL_SIZE\
271 ])
272 #define mmuB2tmgr(table) \
273 (&Btmgrbase[\
274 ((mmu_short_dte_t *)(table) - mmuBbase)\
275 / MMU_B_TBL_SIZE\
276 ])
277 #define mmuC2tmgr(table) \
278 (&Ctmgrbase[\
279 ((mmu_short_pte_t *)(table) - mmuCbase)\
280 / MMU_C_TBL_SIZE\
281 ])
282 #define pte2pve(pte) \
283 (&pvebase[\
284 ((mmu_short_pte_t *)(pte) - mmuCbase)\
285 ])
286 /* I don't think this is actually used.
287 * #define pte2pv(pte) \
288 * (pa2pv(\
289 * (pte)->attr.raw & MMU_SHORT_PTE_BASEADDR\
290 * ))
291 */
292 /* This is now a function call
293 * #define pa2pv(pa) \
294 * (&pvbase[(unsigned long)\
295 * sun3x_btop(pa)\
296 * ])
297 */
298 #define pve2pte(pve) \
299 (&mmuCbase[(unsigned long)\
300 (((pv_elem_t *)(pve)) - pvebase)\
301 / sizeof(mmu_short_pte_t)\
302 ])
303
304 /*************************** TEMPORARY STATMENTS *************************
305 * These statements will disappear once this code is integrated into the *
306 * system. They are here only to make the code `stand alone'. *
307 *************************************************************************/
308 #define mmu_ptov(pa) ((unsigned long) KERNBASE + (unsigned long) (pa))
309 #define mmu_vtop(va) ((unsigned long) (va) - (unsigned long) KERNBASE)
310 #define NULL 0
311
312 #define NUM_A_TABLES 20
313 #define NUM_B_TABLES 60
314 #define NUM_C_TABLES 60
315
316 /*************************** MISCELANEOUS MACROS *************************/
317 #define PMAP_LOCK() ; /* Nothing, for now */
318 #define PMAP_UNLOCK() ; /* same. */
319 /*************************** FUNCTION DEFINITIONS ************************
320 * These appear here merely for the compiler to enforce type checking on *
321 * all function calls. *
322 *************************************************************************
323 */
324
325 /** External functions
326 ** - functions used within this module but written elsewhere.
327 ** both of these functions are in locore.s
328 */
329 void mmu_seturp __P((vm_offset_t));
330 void mmu_flush __P((int, vm_offset_t));
331 void mmu_flusha __P((void));
332
333 /** Internal functions
334 ** - all functions used only within this module are defined in
335 ** pmap_pvt.h
336 **/
337
338 /** Interface functions
339 ** - functions required by the Mach VM Pmap interface, with MACHINE_CONTIG
340 ** defined.
341 **/
342 #ifdef INCLUDED_IN_PMAP_H
343 void pmap_bootstrap __P((void));
344 void *pmap_bootstrap_alloc __P((int));
345 void pmap_enter __P((pmap_t, vm_offset_t, vm_offset_t, vm_prot_t, boolean_t));
346 pmap_t pmap_create __P((vm_size_t));
347 void pmap_destroy __P((pmap_t));
348 void pmap_reference __P((pmap_t));
349 boolean_t pmap_is_referenced __P((vm_offset_t));
350 boolean_t pmap_is_modified __P((vm_offset_t));
351 void pmap_clear_modify __P((vm_offset_t));
352 vm_offset_t pmap_extract __P((pmap_t, vm_offset_t));
353 void pmap_activate __P((pmap_t, struct pcb *));
354 int pmap_page_index __P((vm_offset_t));
355 u_int pmap_free_pages __P((void));
356 #endif /* INCLUDED_IN_PMAP_H */
357
358 /********************************** CODE ********************************
359 * Functions that are called from other parts of the kernel are labeled *
360 * as 'INTERFACE' functions. Functions that are only called from *
361 * within the pmap module are labeled as 'INTERNAL' functions. *
362 * Functions that are internal, but are not (currently) used at all are *
363 * labeled 'INTERNAL_X'. *
364 ************************************************************************/
365
366 /* pmap_bootstrap INTERNAL
367 **
368 * Initializes the pmap system. Called at boot time from sun3x_vm_init()
369 * in _startup.c.
370 *
371 * Reminder: having a pmap_bootstrap_alloc() and also having the VM
372 * system implement pmap_steal_memory() is redundant.
373 * Don't release this code without removing one or the other!
374 */
375 void
376 pmap_bootstrap(nextva)
377 vm_offset_t nextva;
378 {
379 struct physmemory *membank;
380 struct pmap_physmem_struct *pmap_membank;
381 vm_offset_t va, pa, eva;
382 int b, c, i, j; /* running table counts */
383 int size;
384
385 /*
386 * This function is called by __bootstrap after it has
387 * determined the type of machine and made the appropriate
388 * patches to the ROM vectors (XXX- I don't quite know what I meant
389 * by that.) It allocates and sets up enough of the pmap system
390 * to manage the kernel's address space.
391 */
392
393 /* XXX - Attention: moved stuff. */
394
395 /*
396 * Determine the range of kernel virtual space available.
397 */
398 virtual_avail = sun3x_round_page(nextva);
399 virtual_end = VM_MAX_KERNEL_ADDRESS;
400
401 /*
402 * Determine the range of physical memory available and
403 * relay this information to the pmap via the avail_mem[]
404 * array of physical memory segment structures.
405 *
406 * Avail_end is set to the first byte of physical memory
407 * outside the last bank.
408 */
409 avail_start = virtual_avail - KERNBASE;
410
411 /*
412 * This is a somewhat unwrapped loop to deal with
413 * copying the PROM's 'phsymem' banks into the pmap's
414 * banks. The following is always assumed:
415 * 1. There is always at least one bank of memory.
416 * 2. There is always a last bank of memory, and its
417 * pmem_next member must be set to NULL.
418 * XXX - Use: do { ... } while (membank->next) instead?
419 * XXX - Why copy this stuff at all? -gwr
420 */
421 membank = romVectorPtr->v_physmemory;
422 pmap_membank = avail_mem;
423 total_phys_mem = 0;
424
425 while (membank->next) {
426 pmap_membank->pmem_start = membank->address;
427 pmap_membank->pmem_end = membank->address + membank->size;
428 total_phys_mem += membank->size;
429 /* This silly syntax arises because pmap_membank
430 * is really a pre-allocated array, but it is put into
431 * use as a linked list.
432 */
433 pmap_membank->pmem_next = pmap_membank + 1;
434 pmap_membank = pmap_membank->pmem_next;
435 membank = membank->next;
436 }
437
438 /*
439 * XXX The last bank of memory should be reduced to exclude the
440 * physical pages needed by the PROM monitor from being used
441 * in the VM system. XXX - See below - Fix!
442 */
443 pmap_membank->pmem_start = membank->address;
444 pmap_membank->pmem_end = membank->address + membank->size;
445 pmap_membank->pmem_next = NULL;
446
447 #if 0 /* XXX - Need to integrate this! */
448 /*
449 * The last few pages of physical memory are "owned" by
450 * the PROM. The total amount of memory we are allowed
451 * to use is given by the romvec pointer. -gwr
452 *
453 * We should dedicate different variables for 'useable'
454 * and 'physically available'. Most users are used to the
455 * kernel reporting the amount of memory 'physically available'
456 * as opposed to 'useable by the kernel' at boot time. -j
457 */
458 total_phys_mem = *romVectorPtr->memoryAvail;
459 #endif /* XXX */
460
461 total_phys_mem += membank->size; /* XXX see above */
462 physmem = btoc(total_phys_mem);
463 avail_end = pmap_membank->pmem_end;
464 avail_end = sun3x_trunc_page(avail_end);
465
466 /* XXX - End moved stuff. */
467
468 /*
469 * The first step is to allocate MMU tables.
470 * Note: All must be aligned on 256 byte boundaries.
471 *
472 * Start with the top level, or 'A' table.
473 */
474 kernAbase = (mmu_long_dte_t *) virtual_avail;
475 size = sizeof(mmu_long_dte_t) * MMU_A_TBL_SIZE;
476 bzero(kernAbase, size);
477 avail_start += size;
478 virtual_avail += size;
479
480 /* Allocate enough B tables to map from KERNBASE to
481 * the end of VM.
482 */
483 kernBbase = (mmu_short_dte_t *) virtual_avail;
484 size = sizeof(mmu_short_dte_t) *
485 (MMU_A_TBL_SIZE - MMU_TIA(KERNBASE)) * MMU_B_TBL_SIZE;
486 bzero(kernBbase, size);
487 avail_start += size;
488 virtual_avail += size;
489
490 /* Allocate enough C tables. */
491 kernCbase = (mmu_short_pte_t *) virtual_avail;
492 size = sizeof (mmu_short_pte_t) *
493 (MMU_A_TBL_SIZE - MMU_TIA(KERNBASE))
494 * MMU_B_TBL_SIZE * MMU_C_TBL_SIZE;
495 bzero(kernCbase, size);
496 avail_start += size;
497 virtual_avail += size;
498
499 /* For simplicity, the kernel's mappings will be editable as a
500 * flat array of page table entries at kernCbase. The
501 * higher level 'A' and 'B' tables must be initialized to point
502 * to this lower one.
503 */
504 b = c = 0;
505
506 /* Invalidate all mappings below KERNBASE in the A table.
507 * This area has already been zeroed out, but it is good
508 * practice to explicitly show that we are interpreting
509 * it as a list of A table descriptors.
510 */
511 for (i = 0; i < MMU_TIA(KERNBASE); i++) {
512 kernAbase[i].addr.raw = 0;
513 }
514
515 /* Set up the kernel A and B tables so that they will reference the
516 * correct spots in the contiguous table of PTEs allocated for the
517 * kernel's virtual memory space.
518 */
519 for (i = MMU_TIA(KERNBASE); i < MMU_A_TBL_SIZE; i++) {
520 kernAbase[i].attr.raw =
521 MMU_LONG_DTE_LU | MMU_LONG_DTE_SUPV | MMU_DT_SHORT;
522 kernAbase[i].addr.raw = (unsigned long) mmu_vtop(&kernBbase[b]);
523
524 for (j=0; j < MMU_B_TBL_SIZE; j++) {
525 kernBbase[b + j].attr.raw =
526 (unsigned long) mmu_vtop(&kernCbase[c])
527 | MMU_DT_SHORT;
528 c += MMU_C_TBL_SIZE;
529 }
530 b += MMU_B_TBL_SIZE;
531 }
532
533 /*
534 * Now pmap_enter_kernel() may be used safely and will be
535 * the main interface used by _startup.c and other various
536 * modules to modify kernel mappings.
537 *
538 * Note: Our tables will NOT have the default linear mappings!
539 */
540 va = (vm_offset_t) KERNBASE;
541 pa = mmu_vtop(KERNBASE);
542
543 /*
544 * The first page is the msgbuf page (data, non-cached).
545 * Just fixup the mapping here; setup is in cpu_startup().
546 * XXX - Make it non-cached?
547 */
548 pmap_enter_kernel(va, pa|PMAP_NC, VM_PROT_ALL);
549 va += NBPG; pa += NBPG;
550
551 /* The tmporary stack page. */
552 pmap_enter_kernel(va, pa, VM_PROT_ALL);
553 va += NBPG; pa += NBPG;
554
555 /*
556 * Map all of the kernel's text segment as read-only and cacheable.
557 * (Cacheable is implied by default). Unfortunately, the last bytes
558 * of kernel text and the first bytes of kernel data will often be
559 * sharing the same page. Therefore, the last page of kernel text
560 * has to be mapped as read/write, to accomodate the data.
561 */
562 eva = sun3x_trunc_page((vm_offset_t)etext);
563 for (; va < eva; pa += NBPG, va += NBPG)
564 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_EXECUTE);
565
566 /* Map all of the kernel's data (including BSS) segment as read/write
567 * and cacheable.
568 */
569 for (; va < (vm_offset_t) esym; pa += NBPG, va += NBPG)
570 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE);
571
572 /* Map all of the data we have allocated since the start of this
573 * function.
574 */
575 for (; va < virtual_avail; va += NBPG, pa += NBPG)
576 pmap_enter_kernel(va, pa, VM_PROT_READ|VM_PROT_WRITE);
577
578 /* Set 'last_mapped' to the address of the last physical page
579 * that was mapped in the kernel. This variable is used by
580 * pmap_bootstrap_alloc() to determine when it needs to map
581 * a new page.
582 *
583 * XXX - This can be a lot simpler. We already know that the
584 * first 4MB of memory (at least) is mapped PA=VA-KERNBASE,
585 * so we should never need to creat any new mappings. -gwr
586 *
587 * True, but it only remains so as long as we are using the
588 * ROM's CRP. Unless, of course, we copy these mappings into
589 * our table. -j
590 */
591 last_mapped = sun3x_trunc_page(pa - (NBPG - 1));
592
593 /* It is now safe to use pmap_bootstrap_alloc(). */
594
595 pmap_alloc_usermmu(); /* Allocate user MMU tables. */
596 pmap_alloc_usertmgr(); /* Allocate user MMU table managers.*/
597 pmap_alloc_pv(); /* Allocate physical->virtual map. */
598 pmap_alloc_etc(); /* Allocate miscelaneous things. */
599
600 /* Notify the VM system of our page size. */
601 PAGE_SIZE = NBPG;
602 vm_set_page_size();
603
604 /* XXX - Attention: moved stuff. */
605
606 /*
607 * XXX - Make sure avail_start is within the low 4M range
608 * that the Sun PROM guarantees will be mapped in?
609 * Make sure it is below avail_end as well?
610 */
611
612 /*
613 * Now steal some virtual addresses, but
614 * not the physical pages behind them.
615 */
616
617 /*
618 * vpages array: just some virtual addresses for
619 * temporary mappings in the pmap module (two pages)
620 */
621 pmap_bootstrap_aalign(NBPG);
622 tmp_vpages[0] = virtual_avail;
623 virtual_avail += NBPG;
624 tmp_vpages[1] = virtual_avail;
625 virtual_avail += NBPG;
626
627 /* XXX - End moved stuff. */
628
629 /* It should be noted that none of these mappings take
630 * effect until the MMU's root pointer is
631 * is changed from the PROM map, to our own.
632 */
633 pmap_bootstrap_copyprom();
634 pmap_takeover_mmu();
635 }
636
637
638 /* pmap_alloc_usermmu INTERNAL
639 **
640 * Called from pmap_bootstrap() to allocate MMU tables that will
641 * eventually be used for user mappings.
642 */
643 void
644 pmap_alloc_usermmu()
645 {
646 /* Allocate user MMU tables.
647 * These must be aligned on 256 byte boundaries.
648 */
649 pmap_bootstrap_aalign(256);
650 mmuAbase = (mmu_long_dte_t *)
651 pmap_bootstrap_alloc(sizeof(mmu_long_dte_t)
652 * MMU_A_TBL_SIZE
653 * NUM_A_TABLES);
654 mmuBbase = (mmu_short_dte_t *)
655 pmap_bootstrap_alloc(sizeof(mmu_short_dte_t)
656 * MMU_B_TBL_SIZE
657 * NUM_B_TABLES);
658 mmuCbase = (mmu_short_pte_t *)
659 pmap_bootstrap_alloc(sizeof(mmu_short_pte_t)
660 * MMU_C_TBL_SIZE
661 * NUM_C_TABLES);
662 }
663
664 /* pmap_alloc_pv INTERNAL
665 **
666 * Called from pmap_bootstrap() to allocate the physical
667 * to virtual mapping list. Each physical page of memory
668 * in the system has a corresponding element in this list.
669 */
670 void
671 pmap_alloc_pv()
672 {
673 int i;
674 unsigned int total_mem;
675
676 /* Allocate a pv_head structure for every page of physical
677 * memory that will be managed by the system. Since memory on
678 * the 3/80 is non-contiguous, we cannot arrive at a total page
679 * count by subtraction of the lowest available address from the
680 * highest, but rather we have to step through each memory
681 * bank and add the number of pages in each to the total.
682 *
683 * At this time we also initialize the offset of each bank's
684 * starting pv_head within the pv_head list so that the physical
685 * memory state routines (pmap_is_referenced(),
686 * pmap_is_modified(), et al.) can quickly find coresponding
687 * pv_heads in spite of the non-contiguity.
688 */
689
690 total_mem = 0;
691 for (i = 0; i < SUN3X_80_MEM_BANKS; i++) {
692 avail_mem[i].pmem_pvbase = sun3x_btop(total_mem);
693 total_mem += avail_mem[i].pmem_end -
694 avail_mem[i].pmem_start;
695 if (avail_mem[i].pmem_next == NULL)
696 break;
697 }
698 #ifdef PMAP_DEBUG
699 if (total_mem != total_phys_mem)
700 panic("pmap_alloc_pv did not arrive at correct page count");
701 #endif
702
703 pvbase = (pv_t *) pmap_bootstrap_alloc(sizeof(pv_t) *
704 sun3x_btop(total_phys_mem));
705 }
706
707 /* pmap_alloc_usertmgr INTERNAL
708 **
709 * Called from pmap_bootstrap() to allocate the structures which
710 * facilitate management of user MMU tables. Each user MMU table
711 * in the system has one such structure associated with it.
712 */
713 void
714 pmap_alloc_usertmgr()
715 {
716 /* Allocate user MMU table managers */
717 /* XXX - It would be a lot simpler to just make these BSS. -gwr */
718 Atmgrbase = (a_tmgr_t *) pmap_bootstrap_alloc(sizeof(a_tmgr_t)
719 * NUM_A_TABLES);
720 Btmgrbase = (b_tmgr_t *) pmap_bootstrap_alloc(sizeof(b_tmgr_t)
721 * NUM_B_TABLES);
722 Ctmgrbase = (c_tmgr_t *) pmap_bootstrap_alloc(sizeof(c_tmgr_t)
723 * NUM_C_TABLES);
724
725 /* Allocate PV list elements for the physical to virtual
726 * mapping system.
727 */
728 pvebase = (pv_elem_t *) pmap_bootstrap_alloc(
729 sizeof(struct pv_elem_struct)
730 * MMU_C_TBL_SIZE
731 * NUM_C_TABLES );
732 }
733
734 /* pmap_alloc_etc INTERNAL
735 **
736 * Called from pmap_bootstrap() to allocate any remaining pieces
737 * that didn't fit neatly into any of the other pmap_alloc
738 * functions.
739 */
740 void
741 pmap_alloc_etc()
742 {
743 /* Allocate an A table manager for the kernel_pmap */
744 proc0Atmgr = (a_tmgr_t *) pmap_bootstrap_alloc(sizeof(a_tmgr_t));
745 }
746
747 /* pmap_bootstrap_copyprom() INTERNAL
748 **
749 * Copy the PROM mappings into our own tables. Note, we
750 * can use physical addresses until __bootstrap returns.
751 */
752 void
753 pmap_bootstrap_copyprom()
754 {
755 MachMonRomVector *romp;
756 int *mon_ctbl;
757 mmu_short_pte_t *kpte;
758 int i, len;
759
760 romp = romVectorPtr;
761
762 /*
763 * Copy the mappings in MON_KDB_START...MONEND
764 * Note: mon_ctbl[0] maps MON_KDB_START
765 */
766 mon_ctbl = *romp->monptaddr;
767 i = sun3x_btop(MON_KDB_START - KERNBASE);
768 kpte = &kernCbase[i];
769 len = sun3x_btop(MONEND - MON_KDB_START);
770
771 for (i = 0; i < len; i++) {
772 kpte[i].attr.raw = mon_ctbl[i];
773 }
774
775 /*
776 * Copy the mappings at MON_DVMA_BASE (to the end).
777 * Note, in here, mon_ctbl[0] maps MON_DVMA_BASE.
778 * XXX - This does not appear to be necessary, but
779 * I'm not sure yet if it is or not. -gwr
780 */
781 mon_ctbl = *romp->shadowpteaddr;
782 i = sun3x_btop(MON_DVMA_BASE - KERNBASE);
783 kpte = &kernCbase[i];
784 len = sun3x_btop(MON_DVMA_SIZE);
785
786 for (i = 0; i < len; i++) {
787 kpte[i].attr.raw = mon_ctbl[i];
788 }
789 }
790
791 /* pmap_takeover_mmu INTERNAL
792 **
793 * Called from pmap_bootstrap() after it has copied enough of the
794 * PROM mappings into the kernel map so that we can use our own
795 * MMU table.
796 */
797 void
798 pmap_takeover_mmu()
799 {
800 vm_offset_t tbladdr;
801
802 tbladdr = mmu_vtop((vm_offset_t) kernAbase);
803 mon_printf("pmap_takeover_mmu: tbladdr=0x%x\n", tbladdr);
804
805 /* Initialize the CPU Root Pointer (CRP) for proc0. */
806 /* XXX: I'd prefer per-process CRP storage. -gwr */
807 proc0crp.limit = 0x80000003; /* limit and type */
808 proc0crp.paddr = tbladdr; /* phys. addr. */
809 curpcb->pcb_mmuctx = (int) &proc0crp;
810
811 mon_printf("pmap_takeover_mmu: loadcrp...\n");
812 loadcrp(curpcb->pcb_mmuctx);
813 mon_printf("pmap_takeover_mmu: survived!\n");
814 }
815
816 /* pmap_init INTERFACE
817 **
818 * Called at the end of vm_init() to set up the pmap system to go
819 * into full time operation.
820 */
821 void
822 pmap_init()
823 {
824 /** Initialize the manager pools **/
825 TAILQ_INIT(&a_pool);
826 TAILQ_INIT(&b_pool);
827 TAILQ_INIT(&c_pool);
828
829 /** Initialize the PV system **/
830 pmap_init_pv();
831
832 /** Zero out the kernel's pmap **/
833 bzero(&kernel_pmap, sizeof(struct pmap));
834
835 /* Initialize the A table manager that is used in pmaps which
836 * do not have an A table of their own. This table uses the
837 * kernel, or 'proc0' level A MMU table, which contains no valid
838 * user space mappings. Any user process that attempts to execute
839 * using this A table will fault. At which point the VM system will
840 * call pmap_enter, which will then allocate it an A table of its own
841 * from the pool.
842 */
843 proc0Atmgr->at_dtbl = kernAbase;
844 proc0Atmgr->at_parent = &kernel_pmap;
845 kernel_pmap.pm_a_tbl = proc0Atmgr;
846
847 /**************************************************************
848 * Initialize all tmgr structures and MMU tables they manage. *
849 **************************************************************/
850 /** Initialize A tables **/
851 pmap_init_a_tables();
852 /** Initialize B tables **/
853 pmap_init_b_tables();
854 /** Initialize C tables **/
855 pmap_init_c_tables();
856 }
857
858 /* pmap_init_a_tables() INTERNAL
859 **
860 * Initializes all A managers, their MMU A tables, and inserts
861 * them into the A manager pool for use by the system.
862 */
863 void
864 pmap_init_a_tables()
865 {
866 int i;
867 a_tmgr_t *a_tbl;
868
869 for (i=0; i < NUM_A_TABLES; i++) {
870 /* Select the next available A manager from the pool */
871 a_tbl = &Atmgrbase[i];
872
873 /* Clear its parent entry. Set its wired and valid
874 * entry count to zero.
875 */
876 a_tbl->at_parent = NULL;
877 a_tbl->at_wcnt = a_tbl->at_ecnt = 0;
878
879 /* Assign it the next available MMU A table from the pool */
880 a_tbl->at_dtbl = &mmuAbase[i * MMU_A_TBL_SIZE];
881
882 /* Initialize the MMU A table with the table in the `proc0',
883 * or kernel, mapping. This ensures that every process has
884 * the kernel mapped in the top part of its address space.
885 */
886 bcopy(kernAbase, a_tbl->at_dtbl, MMU_A_TBL_SIZE *
887 sizeof(mmu_long_dte_t));
888
889 /* Finally, insert the manager into the A pool,
890 * making it ready to be used by the system.
891 */
892 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
893 }
894 }
895
896 /* pmap_init_b_tables() INTERNAL
897 **
898 * Initializes all B table managers, their MMU B tables, and
899 * inserts them into the B manager pool for use by the system.
900 */
901 void
902 pmap_init_b_tables()
903 {
904 int i,j;
905 b_tmgr_t *b_tbl;
906
907 for (i=0; i < NUM_B_TABLES; i++) {
908 /* Select the next available B manager from the pool */
909 b_tbl = &Btmgrbase[i];
910
911 b_tbl->bt_parent = NULL; /* clear its parent, */
912 b_tbl->bt_pidx = 0; /* parent index, */
913 b_tbl->bt_wcnt = 0; /* wired entry count, */
914 b_tbl->bt_ecnt = 0; /* valid entry count. */
915
916 /* Assign it the next available MMU B table from the pool */
917 b_tbl->bt_dtbl = &mmuBbase[i * MMU_B_TBL_SIZE];
918
919 /* Invalidate every descriptor in the table */
920 for (j=0; j < MMU_B_TBL_SIZE; j++)
921 b_tbl->bt_dtbl[j].attr.raw = MMU_DT_INVALID;
922
923 /* Insert the manager into the B pool */
924 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
925 }
926 }
927
928 /* pmap_init_c_tables() INTERNAL
929 **
930 * Initializes all C table managers, their MMU C tables, and
931 * inserts them into the C manager pool for use by the system.
932 */
933 void
934 pmap_init_c_tables()
935 {
936 int i,j;
937 c_tmgr_t *c_tbl;
938
939 for (i=0; i < NUM_C_TABLES; i++) {
940 /* Select the next available C manager from the pool */
941 c_tbl = &Ctmgrbase[i];
942
943 c_tbl->ct_parent = NULL; /* clear its parent, */
944 c_tbl->ct_pidx = 0; /* parent index, */
945 c_tbl->ct_wcnt = 0; /* wired entry count, */
946 c_tbl->ct_ecnt = 0; /* valid entry count. */
947
948 /* Assign it the next available MMU C table from the pool */
949 c_tbl->ct_dtbl = &mmuCbase[i * MMU_C_TBL_SIZE];
950
951 for (j=0; j < MMU_C_TBL_SIZE; j++)
952 c_tbl->ct_dtbl[j].attr.raw = MMU_DT_INVALID;
953
954 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
955 }
956 }
957
958 /* pmap_init_pv() INTERNAL
959 **
960 * Initializes the Physical to Virtual mapping system.
961 */
962 void
963 pmap_init_pv()
964 {
965 bzero(pvbase, sizeof(pv_t) * sun3x_btop(total_phys_mem));
966 pv_initialized = TRUE;
967 }
968
969 /* get_a_table INTERNAL
970 **
971 * Retrieve and return a level A table for use in a user map.
972 */
973 a_tmgr_t *
974 get_a_table()
975 {
976 a_tmgr_t *tbl;
977
978 /* Get the top A table in the pool */
979 tbl = a_pool.tqh_first;
980 if (tbl == NULL)
981 panic("get_a_table: out of A tables.");
982 TAILQ_REMOVE(&a_pool, tbl, at_link);
983 /* If the table has a non-null parent pointer then it is in use.
984 * Forcibly abduct it from its parent and clear its entries.
985 * No re-entrancy worries here. This table would not be in the
986 * table pool unless it was available for use.
987 */
988 if (tbl->at_parent) {
989 tbl->at_parent->pm_stats.resident_count -= free_a_table(tbl);
990 tbl->at_parent->pm_a_tbl = proc0Atmgr;
991 }
992 #ifdef NON_REENTRANT
993 /* If the table isn't to be wired down, re-insert it at the
994 * end of the pool.
995 */
996 if (!wired)
997 /* Quandary - XXX
998 * Would it be better to let the calling function insert this
999 * table into the queue? By inserting it here, we are allowing
1000 * it to be stolen immediately. The calling function is
1001 * probably not expecting to use a table that it is not
1002 * assured full control of.
1003 * Answer - In the intrest of re-entrancy, it is best to let
1004 * the calling function determine when a table is available
1005 * for use. Therefore this code block is not used.
1006 */
1007 TAILQ_INSERT_TAIL(&a_pool, tbl, at_link);
1008 #endif /* NON_REENTRANT */
1009 return tbl;
1010 }
1011
1012 /* get_b_table INTERNAL
1013 **
1014 * Return a level B table for use.
1015 */
1016 b_tmgr_t *
1017 get_b_table()
1018 {
1019 b_tmgr_t *tbl;
1020
1021 /* See 'get_a_table' for comments. */
1022 tbl = b_pool.tqh_first;
1023 if (tbl == NULL)
1024 panic("get_b_table: out of B tables.");
1025 TAILQ_REMOVE(&b_pool, tbl, bt_link);
1026 if (tbl->bt_parent) {
1027 tbl->bt_parent->at_dtbl[tbl->bt_pidx].attr.raw = MMU_DT_INVALID;
1028 tbl->bt_parent->at_ecnt--;
1029 tbl->bt_parent->at_parent->pm_stats.resident_count -=
1030 free_b_table(tbl);
1031 }
1032 #ifdef NON_REENTRANT
1033 if (!wired)
1034 /* XXX see quandary in get_b_table */
1035 /* XXX start lock */
1036 TAILQ_INSERT_TAIL(&b_pool, tbl, bt_link);
1037 /* XXX end lock */
1038 #endif /* NON_REENTRANT */
1039 return tbl;
1040 }
1041
1042 /* get_c_table INTERNAL
1043 **
1044 * Return a level C table for use.
1045 */
1046 c_tmgr_t *
1047 get_c_table()
1048 {
1049 c_tmgr_t *tbl;
1050
1051 /* See 'get_a_table' for comments */
1052 tbl = c_pool.tqh_first;
1053 if (tbl == NULL)
1054 panic("get_c_table: out of C tables.");
1055 TAILQ_REMOVE(&c_pool, tbl, ct_link);
1056 if (tbl->ct_parent) {
1057 tbl->ct_parent->bt_dtbl[tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1058 tbl->ct_parent->bt_ecnt--;
1059 tbl->ct_parent->bt_parent->at_parent->pm_stats.resident_count
1060 -= free_c_table(tbl);
1061 }
1062 #ifdef NON_REENTRANT
1063 if (!wired)
1064 /* XXX See quandary in get_a_table */
1065 /* XXX start lock */
1066 TAILQ_INSERT_TAIL(&c_pool, tbl, c_link);
1067 /* XXX end lock */
1068 #endif /* NON_REENTRANT */
1069
1070 return tbl;
1071 }
1072
1073 /* The following 'free_table' and 'steal_table' functions are called to
1074 * detach tables from their current obligations (parents and children) and
1075 * prepare them for reuse in another mapping.
1076 *
1077 * Free_table is used when the calling function will handle the fate
1078 * of the parent table, such as returning it to the free pool when it has
1079 * no valid entries. Functions that do not want to handle this should
1080 * call steal_table, in which the parent table's descriptors and entry
1081 * count are automatically modified when this table is removed.
1082 */
1083
1084 /* free_a_table INTERNAL
1085 **
1086 * Unmaps the given A table and all child tables from their current
1087 * mappings. Returns the number of pages that were invalidated.
1088 *
1089 * Cache note: The MC68851 will automatically flush all
1090 * descriptors derived from a given A table from its
1091 * Automatic Translation Cache (ATC) if we issue a
1092 * 'PFLUSHR' instruction with the base address of the
1093 * table. This function should do, and does so.
1094 * Note note: We are using an MC68030 - there is no
1095 * PFLUSHR.
1096 */
1097 int
1098 free_a_table(a_tbl)
1099 a_tmgr_t *a_tbl;
1100 {
1101 int i, removed_cnt;
1102 mmu_long_dte_t *dte;
1103 mmu_short_dte_t *dtbl;
1104 b_tmgr_t *tmgr;
1105
1106 /* Flush the ATC cache of all cached descriptors derived
1107 * from this table.
1108 * XXX - Sun3x does not use 68851's cached table feature
1109 * flush_atc_crp(mmu_vtop(a_tbl->dte));
1110 */
1111
1112 /* Remove any pending cache flushes that were designated
1113 * for the pmap this A table belongs to.
1114 * a_tbl->parent->atc_flushq[0] = 0;
1115 * XXX - Not implemented in sun3x.
1116 */
1117
1118 /* All A tables in the system should retain a map for the
1119 * kernel. If the table contains any valid descriptors
1120 * (other than those for the kernel area), invalidate them all,
1121 * stopping short of the kernel's entries.
1122 */
1123 removed_cnt = 0;
1124 if (a_tbl->at_ecnt) {
1125 dte = a_tbl->at_dtbl;
1126 for (i=0; i < MMU_TIA(KERNBASE); i++)
1127 /* If a table entry points to a valid B table, free
1128 * it and its children.
1129 */
1130 if (MMU_VALID_DT(dte[i])) {
1131 /* The following block does several things,
1132 * from innermost expression to the
1133 * outermost:
1134 * 1) It extracts the base (cc 1996)
1135 * address of the B table pointed
1136 * to in the A table entry dte[i].
1137 * 2) It converts this base address into
1138 * the virtual address it can be
1139 * accessed with. (all MMU tables point
1140 * to physical addresses.)
1141 * 3) It finds the corresponding manager
1142 * structure which manages this MMU table.
1143 * 4) It frees the manager structure.
1144 * (This frees the MMU table and all
1145 * child tables. See 'free_b_table' for
1146 * details.)
1147 */
1148 dtbl = (mmu_short_dte_t *) MMU_DTE_PA(dte[i]);
1149 dtbl = (mmu_short_dte_t *) mmu_ptov(dtbl);
1150 tmgr = mmuB2tmgr(dtbl);
1151 removed_cnt += free_b_table(tmgr);
1152 }
1153 }
1154 a_tbl->at_ecnt = 0;
1155 return removed_cnt;
1156 }
1157
1158 /* free_b_table INTERNAL
1159 **
1160 * Unmaps the given B table and all its children from their current
1161 * mappings. Returns the number of pages that were invalidated.
1162 * (For comments, see 'free_a_table()').
1163 */
1164 int
1165 free_b_table(b_tbl)
1166 b_tmgr_t *b_tbl;
1167 {
1168 int i, removed_cnt;
1169 mmu_short_dte_t *dte;
1170 mmu_short_pte_t *dtbl;
1171 c_tmgr_t *tmgr;
1172
1173 removed_cnt = 0;
1174 if (b_tbl->bt_ecnt) {
1175 dte = b_tbl->bt_dtbl;
1176 for (i=0; i < MMU_B_TBL_SIZE; i++)
1177 if (MMU_VALID_DT(dte[i])) {
1178 dtbl = (mmu_short_pte_t *) MMU_DTE_PA(dte[i]);
1179 dtbl = (mmu_short_pte_t *) mmu_ptov(dtbl);
1180 tmgr = mmuC2tmgr(dtbl);
1181 removed_cnt += free_c_table(tmgr);
1182 }
1183 }
1184
1185 b_tbl->bt_ecnt = 0;
1186 return removed_cnt;
1187 }
1188
1189 /* free_c_table INTERNAL
1190 **
1191 * Unmaps the given C table from use and returns it to the pool for
1192 * re-use. Returns the number of pages that were invalidated.
1193 *
1194 * This function preserves any physical page modification information
1195 * contained in the page descriptors within the C table by calling
1196 * 'pmap_remove_pte().'
1197 */
1198 int
1199 free_c_table(c_tbl)
1200 c_tmgr_t *c_tbl;
1201 {
1202 int i, removed_cnt;
1203
1204 removed_cnt = 0;
1205 if (c_tbl->ct_ecnt)
1206 for (i=0; i < MMU_C_TBL_SIZE; i++)
1207 if (MMU_VALID_DT(c_tbl->ct_dtbl[i])) {
1208 pmap_remove_pte(&c_tbl->ct_dtbl[i]);
1209 removed_cnt++;
1210 }
1211 c_tbl->ct_ecnt = 0;
1212 return removed_cnt;
1213 }
1214
1215 /* free_c_table_novalid INTERNAL
1216 **
1217 * Frees the given C table manager without checking to see whether
1218 * or not it contains any valid page descriptors as it is assumed
1219 * that it does not.
1220 */
1221 void
1222 free_c_table_novalid(c_tbl)
1223 c_tmgr_t *c_tbl;
1224 {
1225 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1226 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
1227 c_tbl->ct_parent->bt_dtbl[c_tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1228 }
1229
1230 /* pmap_remove_pte INTERNAL
1231 **
1232 * Unmap the given pte and preserve any page modification
1233 * information by transfering it to the pv head of the
1234 * physical page it maps to. This function does not update
1235 * any reference counts because it is assumed that the calling
1236 * function will do so. If the calling function does not have the
1237 * ability to do so, the function pmap_dereference_pte() exists
1238 * for this purpose.
1239 */
1240 void
1241 pmap_remove_pte(pte)
1242 mmu_short_pte_t *pte;
1243 {
1244 vm_offset_t pa;
1245 pv_t *pv;
1246 pv_elem_t *pve;
1247
1248 pa = MMU_PTE_PA(*pte);
1249 if (is_managed(pa)) {
1250 pv = pa2pv(pa);
1251 /* Save the mod/ref bits of the pte by simply
1252 * ORing the entire pte onto the pv_flags member
1253 * of the pv structure.
1254 * There is no need to use a separate bit pattern
1255 * for usage information on the pv head than that
1256 * which is used on the MMU ptes.
1257 */
1258 pv->pv_flags |= pte->attr.raw;
1259
1260 pve = pte2pve(pte);
1261 if (pve == pv->pv_head.lh_first)
1262 pv->pv_head.lh_first = pve->pve_link.le_next;
1263 LIST_REMOVE(pve, pve_link);
1264 }
1265
1266 pte->attr.raw = MMU_DT_INVALID;
1267 }
1268
1269 /* pmap_dereference_pte INTERNAL
1270 **
1271 * Update the necessary reference counts in any tables and pmaps to
1272 * reflect the removal of the given pte. Only called when no knowledge of
1273 * the pte's associated pmap is unknown. This only occurs in the PV call
1274 * 'pmap_page_protect()' with a protection of VM_PROT_NONE, which means
1275 * that all references to a given physical page must be removed.
1276 */
1277 void
1278 pmap_dereference_pte(pte)
1279 mmu_short_pte_t *pte;
1280 {
1281 c_tmgr_t *c_tbl;
1282
1283 c_tbl = pmap_find_c_tmgr(pte);
1284 c_tbl->ct_parent->bt_parent->at_parent->pm_stats.resident_count--;
1285 if (--c_tbl->ct_ecnt == 0)
1286 free_c_table_novalid(c_tbl);
1287 }
1288
1289 /* pmap_stroll INTERNAL
1290 **
1291 * Retrieve the addresses of all table managers involved in the mapping of
1292 * the given virtual address. If the table walk completed sucessfully,
1293 * return TRUE. If it was only partial sucessful, return FALSE.
1294 * The table walk performed by this function is important to many other
1295 * functions in this module.
1296 */
1297 boolean_t
1298 pmap_stroll(pmap, va, a_tbl, b_tbl, c_tbl, pte, a_idx, b_idx, pte_idx)
1299 pmap_t pmap;
1300 vm_offset_t va;
1301 a_tmgr_t **a_tbl;
1302 b_tmgr_t **b_tbl;
1303 c_tmgr_t **c_tbl;
1304 mmu_short_pte_t **pte;
1305 int *a_idx, *b_idx, *pte_idx;
1306 {
1307 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1308 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1309
1310 if (pmap == pmap_kernel())
1311 return FALSE;
1312
1313 /* Does the given pmap have an A table? */
1314 *a_tbl = pmap->pm_a_tbl;
1315 if (*a_tbl == NULL)
1316 return FALSE; /* No. Return unknown. */
1317 /* Does the A table have a valid B table
1318 * under the corresponding table entry?
1319 */
1320 *a_idx = MMU_TIA(va);
1321 a_dte = &((*a_tbl)->at_dtbl[*a_idx]);
1322 if (!MMU_VALID_DT(*a_dte))
1323 return FALSE; /* No. Return unknown. */
1324 /* Yes. Extract B table from the A table. */
1325 *b_tbl = pmap_find_b_tmgr(
1326 (mmu_short_dte_t *) mmu_ptov(
1327 MMU_DTE_PA(*a_dte)
1328 )
1329 );
1330 /* Does the B table have a valid C table
1331 * under the corresponding table entry?
1332 */
1333 *b_idx = MMU_TIB(va);
1334 b_dte = &((*b_tbl)->bt_dtbl[*b_idx]);
1335 if (!MMU_VALID_DT(*b_dte))
1336 return FALSE; /* No. Return unknown. */
1337 /* Yes. Extract C table from the B table. */
1338 *c_tbl = pmap_find_c_tmgr(
1339 (mmu_short_pte_t *) mmu_ptov(
1340 MMU_DTE_PA(*b_dte)
1341 )
1342 );
1343 *pte_idx = MMU_TIC(va);
1344 *pte = &((*c_tbl)->ct_dtbl[*pte_idx]);
1345
1346 return TRUE;
1347 }
1348
1349 /* pmap_enter INTERFACE
1350 **
1351 * Called by the kernel to map a virtual address
1352 * to a physical address in the given process map.
1353 *
1354 * Note: this function should apply an exclusive lock
1355 * on the pmap system for its duration. (it certainly
1356 * would save my hair!!)
1357 */
1358 void
1359 pmap_enter(pmap, va, pa, prot, wired)
1360 pmap_t pmap;
1361 vm_offset_t va;
1362 vm_offset_t pa;
1363 vm_prot_t prot;
1364 boolean_t wired;
1365 {
1366 u_int a_idx, b_idx, pte_idx; /* table indexes (fix grammar) */
1367 a_tmgr_t *a_tbl; /* A: long descriptor table manager */
1368 b_tmgr_t *b_tbl; /* B: short descriptor table manager */
1369 c_tmgr_t *c_tbl; /* C: short page table manager */
1370 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1371 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1372 mmu_short_pte_t *c_pte; /* C: short page descriptor table */
1373 pv_t *pv; /* pv list head */
1374 pv_elem_t *pve; /* pv element */
1375 enum {NONE, NEWA, NEWB, NEWC} llevel; /* used at end */
1376
1377 if (pmap == NULL)
1378 return;
1379 if (pmap == pmap_kernel()) {
1380 pmap_enter_kernel(va, pa, prot);
1381 return;
1382 }
1383
1384 /* For user mappings we walk along the MMU tables of the given
1385 * pmap, reaching a PTE which describes the virtual page being
1386 * mapped or changed. If any level of the walk ends in an invalid
1387 * entry, a table must be allocated and the entry must be updated
1388 * to point to it.
1389 * There is a bit of confusion as to whether this code must be
1390 * re-entrant. For now we will assume it is. To support
1391 * re-entrancy we must unlink tables from the table pool before
1392 * we assume we may use them. Tables are re-linked into the pool
1393 * when we are finished with them at the end of the function.
1394 * But I don't feel like doing that until we have proof that this
1395 * needs to be re-entrant.
1396 * 'llevel' records which tables need to be relinked.
1397 */
1398 llevel = NONE;
1399
1400 /* Step 1 - Retrieve the A table from the pmap. If it is the default
1401 * A table (commonly known as the 'proc0' A table), allocate a new one.
1402 */
1403
1404 a_tbl = pmap->pm_a_tbl;
1405 if (a_tbl == proc0Atmgr) {
1406 pmap->pm_a_tbl = a_tbl = get_a_table();
1407 if (!wired)
1408 llevel = NEWA;
1409 } else {
1410 /* Use the A table already allocated for this pmap.
1411 * Unlink it from the A table pool if necessary.
1412 */
1413 if (wired && !a_tbl->at_wcnt)
1414 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
1415 }
1416
1417 /* Step 2 - Walk into the B table. If there is no valid B table,
1418 * allocate one.
1419 */
1420
1421 a_idx = MMU_TIA(va); /* Calculate the TIA of the VA. */
1422 a_dte = &a_tbl->at_dtbl[a_idx]; /* Retrieve descriptor from table */
1423 if (MMU_VALID_DT(*a_dte)) { /* Is the descriptor valid? */
1424 /* Yes, it points to a valid B table. Use it. */
1425 /*************************************
1426 * a_idx *
1427 * v *
1428 * a_tbl -> +-+-+-+-+-+-+-+-+-+-+-+- *
1429 * | | | | | | | | | | | | *
1430 * +-+-+-+-+-+-+-+-+-+-+-+- *
1431 * | *
1432 * \- b_tbl -> +-+- *
1433 * | | *
1434 * +-+- *
1435 *************************************/
1436 b_dte = (mmu_short_dte_t *) mmu_ptov(a_dte->addr.raw);
1437 b_tbl = mmuB2tmgr(b_dte);
1438 if (wired && !b_tbl->bt_wcnt) {
1439 /* If mapping is wired and table is not */
1440 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
1441 a_tbl->at_wcnt++; /* Update parent table's wired
1442 * entry count. */
1443 }
1444 } else {
1445 b_tbl = get_b_table(); /* No, need to allocate a new B table */
1446 /* Point the parent A table descriptor to this new B table. */
1447 a_dte->addr.raw = (unsigned long) mmu_vtop(b_tbl->bt_dtbl);
1448 a_dte->attr.attr_struct.dt = MMU_DT_SHORT;
1449 /* Create the necessary back references to the parent table */
1450 b_tbl->bt_parent = a_tbl;
1451 b_tbl->bt_pidx = a_idx;
1452 /* If this table is to be wired, make sure the parent A table
1453 * wired count is updated to reflect that it has another wired
1454 * entry.
1455 */
1456 a_tbl->at_ecnt++; /* Update parent's valid entry count */
1457 if (wired)
1458 a_tbl->at_wcnt++;
1459 else if (llevel == NONE)
1460 llevel = NEWB;
1461 }
1462
1463 /* Step 3 - Walk into the C table, if there is no valid C table,
1464 * allocate one.
1465 */
1466
1467 b_idx = MMU_TIB(va); /* Calculate the TIB of the VA */
1468 b_dte = &b_tbl->bt_dtbl[b_idx]; /* Retrieve descriptor from table */
1469 if (MMU_VALID_DT(*b_dte)) { /* Is the descriptor valid? */
1470 /* Yes, it points to a valid C table. Use it. */
1471 /**************************************
1472 * c_idx *
1473 * | v *
1474 * \- b_tbl -> +-+-+-+-+-+-+-+-+-+-+- *
1475 * | | | | | | | | | | | *
1476 * +-+-+-+-+-+-+-+-+-+-+- *
1477 * | *
1478 * \- c_tbl -> +-+-- *
1479 * | | | *
1480 * +-+-- *
1481 **************************************/
1482 c_pte = (mmu_short_pte_t *) MMU_PTE_PA(*b_dte);
1483 c_pte = (mmu_short_pte_t *) mmu_ptov(c_pte);
1484 c_tbl = mmuC2tmgr(c_pte);
1485 if (wired && !c_tbl->ct_wcnt) {
1486 /* If mapping is wired and table is not */
1487 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1488 b_tbl->bt_wcnt++;
1489 }
1490 } else {
1491 c_tbl = get_c_table(); /* No, need to allocate a new C table */
1492 /* Point the parent B table descriptor to this new C table. */
1493 b_dte->attr.raw = (unsigned long) mmu_vtop(c_tbl->ct_dtbl);
1494 b_dte->attr.attr_struct.dt = MMU_DT_SHORT;
1495 /* Create the necessary back references to the parent table */
1496 c_tbl->ct_parent = b_tbl;
1497 c_tbl->ct_pidx = b_idx;
1498 /* If this table is to be wired, make sure the parent B table
1499 * wired count is updated to reflect that it has another wired
1500 * entry.
1501 */
1502 b_tbl->bt_ecnt++; /* Update parent's valid entry count */
1503 if (wired)
1504 b_tbl->bt_wcnt++;
1505 else if (llevel == NONE)
1506 llevel = NEWC;
1507 }
1508
1509 /* Step 4 - Deposit a page descriptor (PTE) into the appropriate
1510 * slot of the C table, describing the PA to which the VA is mapped.
1511 */
1512
1513 pte_idx = MMU_TIC(va);
1514 c_pte = &c_tbl->ct_dtbl[pte_idx];
1515 if (MMU_VALID_DT(*c_pte)) { /* Is the entry currently valid? */
1516 /* If the PTE is currently valid, then this function call
1517 * is just a synonym for one (or more) of the following
1518 * operations:
1519 * change protections on a page
1520 * change wiring status of a page
1521 * remove the mapping of a page
1522 */
1523 /* Is the new address the same as the old? */
1524 if (MMU_PTE_PA(*c_pte) == pa) {
1525 /* Yes, do nothing. */
1526 } else {
1527 /* No, remove the old entry */
1528 pmap_remove_pte(c_pte);
1529 }
1530 } else {
1531 /* No, update the valid entry count in the C table */
1532 c_tbl->ct_ecnt++;
1533 /* and in pmap */
1534 pmap->pm_stats.resident_count++;
1535 }
1536 /* Map the page. */
1537 c_pte->attr.raw = ((unsigned long) pa | MMU_DT_PAGE);
1538
1539 if (wired) /* Does the entry need to be wired? */ {
1540 c_pte->attr.raw |= MMU_SHORT_PTE_WIRED;
1541 }
1542
1543 /* If the physical address being mapped is managed by the PV
1544 * system then link the pte into the list of pages mapped to that
1545 * address.
1546 */
1547 if (is_managed(pa)) {
1548 pv = pa2pv(pa);
1549 pve = pte2pve(c_pte);
1550 LIST_INSERT_HEAD(&pv->pv_head, pve, pve_link);
1551 }
1552
1553 /* Move any allocated tables back into the active pool. */
1554
1555 switch (llevel) {
1556 case NEWA:
1557 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
1558 /* FALLTHROUGH */
1559 case NEWB:
1560 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
1561 /* FALLTHROUGH */
1562 case NEWC:
1563 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
1564 /* FALLTHROUGH */
1565 default:
1566 break;
1567 }
1568 }
1569
1570 /* pmap_enter_kernel INTERNAL
1571 **
1572 * Map the given virtual address to the given physical address within the
1573 * kernel address space. This function exists because the kernel map does
1574 * not do dynamic table allocation. It consists of a contiguous array of ptes
1575 * and can be edited directly without the need to walk through any tables.
1576 *
1577 * XXX: "Danger, Will Robinson!"
1578 * Note that the kernel should never take a fault on any page
1579 * between [ KERNBASE .. virtual_avail ] and this is checked in
1580 * trap.c for kernel-mode MMU faults. This means that mappings
1581 * created in that range must be implicily wired. -gwr
1582 */
1583 void
1584 pmap_enter_kernel(va, pa, prot)
1585 vm_offset_t va;
1586 vm_offset_t pa;
1587 vm_prot_t prot;
1588 {
1589 boolean_t was_valid = FALSE;
1590 mmu_short_pte_t *pte;
1591
1592 /* XXX - This array is traditionally named "Sysmap" */
1593 pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
1594 if (MMU_VALID_DT(*pte))
1595 was_valid = TRUE;
1596
1597 pte->attr.raw = (pa | MMU_DT_PAGE);
1598
1599 if (!(prot & VM_PROT_WRITE)) /* If access should be read-only */
1600 pte->attr.raw |= MMU_SHORT_PTE_WP;
1601 if (pa & PMAP_NC)
1602 pte->attr.raw |= MMU_SHORT_PTE_CI;
1603 if (was_valid) {
1604 /* mmu_flusha(FC_SUPERD, va); */
1605 /* mmu_flusha(); */
1606 TBIA();
1607 }
1608
1609 }
1610
1611 /* pmap_protect INTERFACE
1612 **
1613 * Apply the given protection to the given virtual address within
1614 * the given map.
1615 *
1616 * It is ok for the protection applied to be stronger than what is
1617 * specified. We use this to our advantage when the given map has no
1618 * mapping for the virtual address. By returning immediately when this
1619 * is discovered, we are effectively applying a protection of VM_PROT_NONE,
1620 * and therefore do not need to map the page just to apply a protection
1621 * code. Only pmap_enter() needs to create new mappings if they do not exist.
1622 */
1623 void
1624 pmap_protect(pmap, va, pa, prot)
1625 pmap_t pmap;
1626 vm_offset_t va, pa;
1627 vm_prot_t prot;
1628 {
1629 int a_idx, b_idx, c_idx;
1630 a_tmgr_t *a_tbl;
1631 b_tmgr_t *b_tbl;
1632 c_tmgr_t *c_tbl;
1633 mmu_short_pte_t *pte;
1634
1635 if (pmap == NULL)
1636 return;
1637 if (pmap == pmap_kernel()) {
1638 pmap_protect_kernel(va, pa, prot);
1639 return;
1640 }
1641
1642 /* Retrieve the mapping from the given pmap. If it does
1643 * not exist then we need not do anything more.
1644 */
1645 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte,
1646 &a_idx, &b_idx, &c_idx) == FALSE) {
1647 return;
1648 }
1649
1650 switch (prot) {
1651 case VM_PROT_ALL:
1652 /* this should never happen in a sane system */
1653 break;
1654 case VM_PROT_READ:
1655 case VM_PROT_READ|VM_PROT_EXECUTE:
1656 /* make the mapping read-only */
1657 pte->attr.raw |= MMU_SHORT_PTE_WP;
1658 break;
1659 case VM_PROT_NONE:
1660 /* this is an alias for 'pmap_remove' */
1661 pmap_dereference_pte(pte);
1662 break;
1663 default:
1664 break;
1665 }
1666 }
1667
1668 /* pmap_protect_kernel INTERNAL
1669 **
1670 * Apply the given protection code to a kernel address mapping.
1671 */
1672 void
1673 pmap_protect_kernel(va, pa, prot)
1674 vm_offset_t va, pa;
1675 vm_prot_t prot;
1676 {
1677 mmu_short_pte_t *pte;
1678
1679 pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
1680 if (MMU_VALID_DT(*pte)) {
1681 switch (prot) {
1682 case VM_PROT_ALL:
1683 break;
1684 case VM_PROT_READ:
1685 case VM_PROT_READ|VM_PROT_EXECUTE:
1686 pte->attr.raw |= MMU_SHORT_PTE_WP;
1687 break;
1688 case VM_PROT_NONE:
1689 /* this is an alias for 'pmap_remove_kernel' */
1690 pte->attr.raw = MMU_DT_INVALID;
1691 break;
1692 default:
1693 break;
1694 }
1695 }
1696 /* since this is the kernel, immediately flush any cached
1697 * descriptors for this address.
1698 */
1699 /* mmu_flush(FC_SUPERD, va); */
1700 TBIS(va);
1701 }
1702
1703 /* pmap_change_wiring INTERFACE
1704 **
1705 * Changes the wiring of the specified page.
1706 *
1707 * This function is called from vm_fault.c to unwire
1708 * a mapping. It really should be called 'pmap_unwire'
1709 * because it is never asked to do anything but remove
1710 * wirings.
1711 */
1712 void
1713 pmap_change_wiring(pmap, va, wire)
1714 pmap_t pmap;
1715 vm_offset_t va;
1716 boolean_t wire;
1717 {
1718 int a_idx, b_idx, c_idx;
1719 a_tmgr_t *a_tbl;
1720 b_tmgr_t *b_tbl;
1721 c_tmgr_t *c_tbl;
1722 mmu_short_pte_t *pte;
1723
1724 /* Kernel mappings always remain wired. */
1725 if (pmap == pmap_kernel())
1726 return;
1727
1728 #ifdef PMAP_DEBUG
1729 if (wire == TRUE)
1730 panic("pmap_change_wiring: wire requested.");
1731 #endif
1732
1733 /* Walk through the tables. If the walk terminates without
1734 * a valid PTE then the address wasn't wired in the first place.
1735 * Return immediately.
1736 */
1737 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte, &a_idx,
1738 &b_idx, &c_idx) == FALSE)
1739 return;
1740
1741
1742 /* Is the PTE wired? If not, return. */
1743 if (!(pte->attr.raw & MMU_SHORT_PTE_WIRED))
1744 return;
1745
1746 /* Remove the wiring bit. */
1747 pte->attr.raw &= ~(MMU_SHORT_PTE_WIRED);
1748
1749 /* Decrement the wired entry count in the C table.
1750 * If it reaches zero the following things happen:
1751 * 1. The table no longer has any wired entries and is considered
1752 * unwired.
1753 * 2. It is placed on the available queue.
1754 * 3. The parent table's wired entry count is decremented.
1755 * 4. If it reaches zero, this process repeats at step 1 and
1756 * stops at after reaching the A table.
1757 */
1758 if (c_tbl->ct_wcnt-- == 0) {
1759 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
1760 if (b_tbl->bt_wcnt-- == 0) {
1761 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
1762 if (a_tbl->at_wcnt-- == 0) {
1763 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
1764 }
1765 }
1766 }
1767
1768 pmap->pm_stats.wired_count--;
1769 }
1770
1771 /* pmap_pageable INTERFACE
1772 **
1773 * Make the specified range of addresses within the given pmap,
1774 * 'pageable' or 'not-pageable'. A pageable page must not cause
1775 * any faults when referenced. A non-pageable page may.
1776 *
1777 * This routine is only advisory. The VM system will call pmap_enter()
1778 * to wire or unwire pages that are going to be made pageable before calling
1779 * this function. By the time this routine is called, everything that needs
1780 * to be done has already been done.
1781 */
1782 void
1783 pmap_pageable(pmap, start, end, pageable)
1784 pmap_t pmap;
1785 vm_offset_t start, end;
1786 boolean_t pageable;
1787 {
1788 /* not implemented. */
1789 }
1790
1791 /* pmap_copy INTERFACE
1792 **
1793 * Copy the mappings of a range of addresses in one pmap, into
1794 * the destination address of another.
1795 *
1796 * This routine is advisory. Should we one day decide that MMU tables
1797 * may be shared by more than one pmap, this function should be used to
1798 * link them together. Until that day however, we do nothing.
1799 */
1800 void
1801 pmap_copy(pmap_a, pmap_b, dst, len, src)
1802 pmap_t pmap_a, pmap_b;
1803 vm_offset_t dst;
1804 vm_size_t len;
1805 vm_offset_t src;
1806 {
1807 /* not implemented. */
1808 }
1809
1810 /* pmap_copy_page INTERFACE
1811 **
1812 * Copy the contents of one physical page into another.
1813 *
1814 * This function makes use of two virtual pages allocated in sun3x_vm_init()
1815 * (found in _startup.c) to map the two specified physical pages into the
1816 * kernel address space. It then uses bcopy() to copy one into the other.
1817 */
1818 void
1819 pmap_copy_page(src, dst)
1820 vm_offset_t src, dst;
1821 {
1822 PMAP_LOCK();
1823 if (tmp_vpages_inuse)
1824 panic("pmap_copy_page: temporary vpages are in use.");
1825 tmp_vpages_inuse++;
1826
1827 pmap_enter_kernel(tmp_vpages[0], src, VM_PROT_READ);
1828 pmap_enter_kernel(tmp_vpages[1], dst, VM_PROT_READ|VM_PROT_WRITE);
1829 bcopy((char *) tmp_vpages[1], (char *) tmp_vpages[0], NBPG);
1830 /* xxx - there's no real need to unmap the mappings is there? */
1831
1832 tmp_vpages_inuse--;
1833 PMAP_UNLOCK();
1834 }
1835
1836 /* pmap_zero_page INTERFACE
1837 **
1838 * Zero the contents of the specified physical page.
1839 *
1840 * Uses one of the virtual pages allocated in sun3x_vm_init() (_startup.c)
1841 * to map the specified page into the kernel address space. Then uses
1842 * bzero() to zero out the page.
1843 */
1844 void
1845 pmap_zero_page(pa)
1846 vm_offset_t pa;
1847 {
1848 PMAP_LOCK();
1849 if (tmp_vpages_inuse)
1850 panic("pmap_zero_page: temporary vpages are in use.");
1851 tmp_vpages_inuse++;
1852
1853 pmap_enter_kernel(tmp_vpages[0], pa, VM_PROT_READ|VM_PROT_WRITE);
1854 bzero((char *) tmp_vpages[0], NBPG);
1855 /* xxx - there's no real need to unmap the mapping is there? */
1856
1857 tmp_vpages_inuse--;
1858 PMAP_UNLOCK();
1859 }
1860
1861 /* pmap_collect INTERFACE
1862 **
1863 * Called from the VM system to collect unused pages in the given
1864 * pmap.
1865 *
1866 * No one implements it, so I'm not even sure how it is supposed to
1867 * 'collect' anything anyways. There's nothing to do but do what everyone
1868 * else does..
1869 */
1870 void
1871 pmap_collect(pmap)
1872 pmap_t pmap;
1873 {
1874 /* not implemented. */
1875 }
1876
1877 /* pmap_create INTERFACE
1878 **
1879 * Create and return a pmap structure.
1880 */
1881 pmap_t
1882 pmap_create(size)
1883 vm_size_t size;
1884 {
1885 pmap_t pmap;
1886
1887 if (size)
1888 return NULL;
1889
1890 pmap = (pmap_t) malloc(sizeof(struct pmap), M_VMPMAP, M_WAITOK);
1891 pmap_pinit(pmap);
1892
1893 return pmap;
1894 }
1895
1896 /* pmap_pinit INTERNAL
1897 **
1898 * Initialize a pmap structure.
1899 */
1900 void
1901 pmap_pinit(pmap)
1902 pmap_t pmap;
1903 {
1904 bzero(pmap, sizeof(struct pmap));
1905 pmap->pm_a_tbl = proc0Atmgr;
1906 }
1907
1908 /* pmap_release INTERFACE
1909 **
1910 * Release any resources held by the given pmap.
1911 *
1912 * This is the reverse analog to pmap_pinit. It does not
1913 * necessarily mean for the pmap structure to be deallocated,
1914 * as in pmap_destroy.
1915 */
1916 void
1917 pmap_release(pmap)
1918 pmap_t pmap;
1919 {
1920 /* As long as the pmap contains no mappings,
1921 * which always should be the case whenever
1922 * this function is called, there really should
1923 * be nothing to do.
1924 */
1925 #ifdef PMAP_DEBUG
1926 if (pmap == NULL)
1927 return;
1928 if (pmap == pmap_kernel())
1929 panic("pmap_release: kernel pmap release requested.");
1930 if (pmap->pm_a_tbl != proc0Atmgr)
1931 panic("pmap_release: pmap not empty.");
1932 #endif
1933 }
1934
1935 /* pmap_reference INTERFACE
1936 **
1937 * Increment the reference count of a pmap.
1938 */
1939 void
1940 pmap_reference(pmap)
1941 pmap_t pmap;
1942 {
1943 if (pmap == NULL)
1944 return;
1945
1946 /* pmap_lock(pmap); */
1947 pmap->pm_refcount++;
1948 /* pmap_unlock(pmap); */
1949 }
1950
1951 /* pmap_dereference INTERNAL
1952 **
1953 * Decrease the reference count on the given pmap
1954 * by one and return the current count.
1955 */
1956 int
1957 pmap_dereference(pmap)
1958 pmap_t pmap;
1959 {
1960 int rtn;
1961
1962 if (pmap == NULL)
1963 return 0;
1964
1965 /* pmap_lock(pmap); */
1966 rtn = --pmap->pm_refcount;
1967 /* pmap_unlock(pmap); */
1968
1969 return rtn;
1970 }
1971
1972 /* pmap_destroy INTERFACE
1973 **
1974 * Decrement a pmap's reference count and delete
1975 * the pmap if it becomes zero. Will be called
1976 * only after all mappings have been removed.
1977 */
1978 void
1979 pmap_destroy(pmap)
1980 pmap_t pmap;
1981 {
1982 if (pmap == NULL)
1983 return;
1984 if (pmap == &kernel_pmap)
1985 panic("pmap_destroy: kernel_pmap!");
1986 if (pmap_dereference(pmap) == 0) {
1987 pmap_release(pmap);
1988 free(pmap, M_VMPMAP);
1989 }
1990 }
1991
1992 /* pmap_is_referenced INTERFACE
1993 **
1994 * Determine if the given physical page has been
1995 * referenced (read from [or written to.])
1996 */
1997 boolean_t
1998 pmap_is_referenced(pa)
1999 vm_offset_t pa;
2000 {
2001 pv_t *pv;
2002 pv_elem_t *pve;
2003 struct mmu_short_pte_struct *pte;
2004
2005 if (!pv_initialized)
2006 return FALSE;
2007 if (!is_managed(pa))
2008 return FALSE;
2009
2010 pv = pa2pv(pa);
2011 /* Check the flags on the pv head. If they are set,
2012 * return immediately. Otherwise a search must be done.
2013 */
2014 if (pv->pv_flags & PV_FLAGS_USED)
2015 return TRUE;
2016 else
2017 /* Search through all pv elements pointing
2018 * to this page and query their reference bits
2019 */
2020 for (pve = pv->pv_head.lh_first;
2021 pve != NULL;
2022 pve = pve->pve_link.le_next) {
2023 pte = pve2pte(pve);
2024 if (MMU_PTE_USED(*pte))
2025 return TRUE;
2026 }
2027
2028 return FALSE;
2029 }
2030
2031 /* pmap_is_modified INTERFACE
2032 **
2033 * Determine if the given physical page has been
2034 * modified (written to.)
2035 */
2036 boolean_t
2037 pmap_is_modified(pa)
2038 vm_offset_t pa;
2039 {
2040 pv_t *pv;
2041 pv_elem_t *pve;
2042
2043 if (!pv_initialized)
2044 return FALSE;
2045 if (!is_managed(pa))
2046 return FALSE;
2047
2048 /* see comments in pmap_is_referenced() */
2049 pv = pa2pv(pa);
2050 if (pv->pv_flags & PV_FLAGS_MDFY)
2051 return TRUE;
2052 else
2053 for (pve = pv->pv_head.lh_first; pve != NULL;
2054 pve = pve->pve_link.le_next) {
2055 struct mmu_short_pte_struct *pte;
2056 pte = pve2pte(pve);
2057 if (MMU_PTE_MODIFIED(*pte))
2058 return TRUE;
2059 }
2060 return FALSE;
2061 }
2062
2063 /* pmap_page_protect INTERFACE
2064 **
2065 * Applies the given protection to all mappings to the given
2066 * physical page.
2067 */
2068 void
2069 pmap_page_protect(pa, prot)
2070 vm_offset_t pa;
2071 vm_prot_t prot;
2072 {
2073 pv_t *pv;
2074 pv_elem_t *pve;
2075 struct mmu_short_pte_struct *pte;
2076
2077 if (!is_managed(pa))
2078 return;
2079
2080 pv = pa2pv(pa);
2081 for (pve = pv->pv_head.lh_first; pve != NULL;
2082 pve = pve->pve_link.le_next) {
2083 pte = pve2pte(pve);
2084 switch (prot) {
2085 case VM_PROT_ALL:
2086 /* do nothing */
2087 break;
2088 case VM_PROT_READ:
2089 case VM_PROT_READ|VM_PROT_EXECUTE:
2090 pte->attr.raw |= MMU_SHORT_PTE_WP;
2091 break;
2092 case VM_PROT_NONE:
2093 pmap_dereference_pte(pte);
2094 break;
2095 default:
2096 break;
2097 }
2098 }
2099 }
2100
2101 /* pmap_who_owns_pte INTERNAL
2102 **
2103 * Called internally to find which pmap the given pte is
2104 * a member of.
2105 */
2106 pmap_t
2107 pmap_who_owns_pte(pte)
2108 mmu_short_pte_t *pte;
2109 {
2110 c_tmgr_t *c_tbl;
2111
2112 c_tbl = pmap_find_c_tmgr(pte);
2113
2114 return c_tbl->ct_parent->bt_parent->at_parent;
2115 }
2116
2117 /* pmap_find_va INTERNAL_X
2118 **
2119 * Called internally to find the virtual address that the
2120 * given pte maps.
2121 *
2122 * Note: I don't know if this function will ever be used, but I've
2123 * implemented it just in case.
2124 */
2125 vm_offset_t
2126 pmap_find_va(pte)
2127 mmu_short_pte_t *pte;
2128 {
2129 a_tmgr_t *a_tbl;
2130 b_tmgr_t *b_tbl;
2131 c_tmgr_t *c_tbl;
2132 vm_offset_t va = 0;
2133
2134 /* Find the virtual address by decoding table indexes.
2135 * Each successive decode will reveal the address from
2136 * least to most significant bit fashion.
2137 *
2138 * 31 0
2139 * +-------------------------------+
2140 * |AAAAAAABBBBBBCCCCCCxxxxxxxxxxxx|
2141 * +-------------------------------+
2142 *
2143 * Start with the 'C' bits.
2144 */
2145 va |= (pmap_find_tic(pte) << MMU_TIC_SHIFT);
2146 c_tbl = pmap_find_c_tmgr(pte);
2147 b_tbl = c_tbl->ct_parent;
2148
2149 /* Add the 'B' bits. */
2150 va |= (c_tbl->ct_pidx << MMU_TIB_SHIFT);
2151 a_tbl = b_tbl->bt_parent;
2152
2153 /* Add the 'A' bits. */
2154 va |= (b_tbl->bt_pidx << MMU_TIA_SHIFT);
2155
2156 return va;
2157 }
2158
2159 /**** These functions should be removed. Structures have changed, making ****
2160 **** them uneccessary. ****/
2161
2162 /* pmap_find_tic INTERNAL
2163 **
2164 * Given the address of a pte, find the TIC (level 'C' table index) for
2165 * the pte within its C table.
2166 */
2167 char
2168 pmap_find_tic(pte)
2169 mmu_short_pte_t *pte;
2170 {
2171 return ((mmuCbase - pte) % MMU_C_TBL_SIZE);
2172 }
2173
2174 /* pmap_find_tib INTERNAL
2175 **
2176 * Given the address of dte known to belong to a B table, find the TIB
2177 * (level 'B' table index) for the dte within its table.
2178 */
2179 char
2180 pmap_find_tib(dte)
2181 mmu_short_dte_t *dte;
2182 {
2183 return ((mmuBbase - dte) % MMU_B_TBL_SIZE);
2184 }
2185
2186 /* pmap_find_tia INTERNAL
2187 **
2188 * Given the address of a dte known to belong to an A table, find the
2189 * TIA (level 'C' table index) for the dte withing its table.
2190 */
2191 char
2192 pmap_find_tia(dte)
2193 mmu_long_dte_t *dte;
2194 {
2195 return ((mmuAbase - dte) % MMU_A_TBL_SIZE);
2196 }
2197
2198 /**** This one should stay ****/
2199
2200 /* pmap_find_c_tmgr INTERNAL
2201 **
2202 * Given a pte known to belong to a C table, return the address of that
2203 * table's management structure.
2204 */
2205 c_tmgr_t *
2206 pmap_find_c_tmgr(pte)
2207 mmu_short_pte_t *pte;
2208 {
2209 return &Ctmgrbase[
2210 ((mmuCbase - pte) / sizeof(*pte) / MMU_C_TBL_SIZE)
2211 ];
2212 }
2213
2214 /* pmap_find_b_tmgr INTERNAL
2215 **
2216 * Given a dte known to belong to a B table, return the address of that
2217 * table's management structure.
2218 */
2219 b_tmgr_t *
2220 pmap_find_b_tmgr(dte)
2221 mmu_short_dte_t *dte;
2222 {
2223 return &Btmgrbase[
2224 ((mmuBbase - dte) / sizeof(*dte) / MMU_B_TBL_SIZE)
2225 ];
2226 }
2227
2228 /* pmap_find_a_tmgr INTERNAL
2229 **
2230 * Given a dte known to belong to an A table, return the address of that
2231 * table's management structure.
2232 */
2233 a_tmgr_t *
2234 pmap_find_a_tmgr(dte)
2235 mmu_long_dte_t *dte;
2236 {
2237 return &Atmgrbase[
2238 ((mmuAbase - dte) / sizeof(*dte) / MMU_A_TBL_SIZE)
2239 ];
2240 }
2241
2242 /**** End of functions that should be removed. ****
2243 **** ****/
2244
2245 /* pmap_clear_modify INTERFACE
2246 **
2247 * Clear the modification bit on the page at the specified
2248 * physical address.
2249 *
2250 */
2251 void
2252 pmap_clear_modify(pa)
2253 vm_offset_t pa;
2254 {
2255 pmap_clear_pv(pa, PV_FLAGS_MDFY);
2256 }
2257
2258 /* pmap_clear_reference INTERFACE
2259 **
2260 * Clear the referenced bit on the page at the specified
2261 * physical address.
2262 */
2263 void
2264 pmap_clear_reference(pa)
2265 vm_offset_t pa;
2266 {
2267 pmap_clear_pv(pa, PV_FLAGS_USED);
2268 }
2269
2270 /* pmap_clear_pv INTERNAL
2271 **
2272 * Clears the specified flag from the specified physical address.
2273 * (Used by pmap_clear_modify() and pmap_clear_reference().)
2274 *
2275 * Flag is one of:
2276 * PV_FLAGS_MDFY - Page modified bit.
2277 * PV_FLAGS_USED - Page used (referenced) bit.
2278 *
2279 * This routine must not only clear the flag on the pv list
2280 * head. It must also clear the bit on every pte in the pv
2281 * list associated with the address.
2282 */
2283 void
2284 pmap_clear_pv(pa, flag)
2285 vm_offset_t pa;
2286 int flag;
2287 {
2288 pv_t *pv;
2289 pv_elem_t *pve;
2290 mmu_short_pte_t *pte;
2291
2292 pv = pa2pv(pa);
2293 pv->pv_flags &= ~(flag);
2294 for (pve = pv->pv_head.lh_first; pve != NULL;
2295 pve = pve->pve_link.le_next) {
2296 pte = pve2pte(pve);
2297 pte->attr.raw &= ~(flag);
2298 }
2299 }
2300
2301 /* pmap_extract INTERFACE
2302 **
2303 * Return the physical address mapped by the virtual address
2304 * in the specified pmap or 0 if it is not known.
2305 *
2306 * Note: this function should also apply an exclusive lock
2307 * on the pmap system during its duration.
2308 */
2309 vm_offset_t
2310 pmap_extract(pmap, va)
2311 pmap_t pmap;
2312 vm_offset_t va;
2313 {
2314 int a_idx, b_idx, pte_idx;
2315 a_tmgr_t *a_tbl;
2316 b_tmgr_t *b_tbl;
2317 c_tmgr_t *c_tbl;
2318 mmu_short_pte_t *c_pte;
2319
2320 if (pmap == pmap_kernel())
2321 return pmap_extract_kernel(va);
2322 if (pmap == NULL)
2323 return 0;
2324
2325 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl,
2326 &c_pte, &a_idx, &b_idx, &pte_idx) == FALSE);
2327 return 0;
2328
2329 if (MMU_VALID_DT(*c_pte))
2330 return MMU_PTE_PA(*c_pte);
2331 else
2332 return 0;
2333 }
2334
2335 /* pmap_extract_kernel INTERNAL
2336 **
2337 * Extract a traslation from the kernel address space.
2338 */
2339 vm_offset_t
2340 pmap_extract_kernel(va)
2341 vm_offset_t va;
2342 {
2343 mmu_short_pte_t *pte;
2344
2345 pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
2346 return MMU_PTE_PA(*pte);
2347 }
2348
2349 /* pmap_remove_kernel INTERNAL
2350 **
2351 * Remove the mapping of a range of virtual addresses from the kernel map.
2352 */
2353 void
2354 pmap_remove_kernel(start, end)
2355 vm_offset_t start;
2356 vm_offset_t end;
2357 {
2358 start -= KERNBASE;
2359 end -= KERNBASE;
2360 start = sun3x_round_page(start); /* round down */
2361 start = sun3x_btop(start);
2362 end += MMU_PAGE_SIZE - 1; /* next round operation will be up */
2363 end = sun3x_round_page(end); /* round */
2364 end = sun3x_btop(end);
2365
2366 while (start < end)
2367 kernCbase[start++].attr.raw = MMU_DT_INVALID;
2368 }
2369
2370 /* pmap_remove INTERFACE
2371 **
2372 * Remove the mapping of a range of virtual addresses from the given pmap.
2373 */
2374 void
2375 pmap_remove(pmap, start, end)
2376 pmap_t pmap;
2377 vm_offset_t start;
2378 vm_offset_t end;
2379 {
2380 if (pmap == pmap_kernel()) {
2381 pmap_remove_kernel(start, end);
2382 return;
2383 }
2384 pmap_remove_a(pmap->pm_a_tbl, start, end);
2385
2386 /* If we just modified the current address space,
2387 * make sure to flush the MMU cache.
2388 */
2389 if (curatbl == pmap->pm_a_tbl) {
2390 /* mmu_flusha(); */
2391 TBIA();
2392 }
2393 }
2394
2395 /* pmap_remove_a INTERNAL
2396 **
2397 * This is function number one in a set of three that removes a range
2398 * of memory in the most efficient manner by removing the highest possible
2399 * tables from the memory space. This particular function attempts to remove
2400 * as many B tables as it can, delegating the remaining fragmented ranges to
2401 * pmap_remove_b().
2402 *
2403 * It's ugly but will do for now.
2404 */
2405 void
2406 pmap_remove_a(a_tbl, start, end)
2407 a_tmgr_t *a_tbl;
2408 vm_offset_t start;
2409 vm_offset_t end;
2410 {
2411 int idx;
2412 vm_offset_t nstart, nend, rstart;
2413 b_tmgr_t *b_tbl;
2414 mmu_long_dte_t *a_dte;
2415 mmu_short_dte_t *b_dte;
2416
2417
2418 if (a_tbl == proc0Atmgr) /* If the pmap has no A table, return */
2419 return;
2420
2421 nstart = MMU_ROUND_UP_A(start);
2422 nend = MMU_ROUND_A(end);
2423
2424 if (start < nstart) {
2425 idx = MMU_TIA(start);
2426 a_dte = &a_tbl->at_dtbl[idx];
2427 if (MMU_VALID_DT(*a_dte)) {
2428 b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
2429 b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
2430 b_tbl = mmuB2tmgr(b_dte);
2431 if (end < nstart) {
2432 pmap_remove_b(b_tbl, start, end);
2433 return;
2434 } else {
2435 pmap_remove_b(b_tbl, start, nstart);
2436 }
2437 } else if (end < nstart) {
2438 return;
2439 }
2440 }
2441 if (nstart < nend) {
2442 idx = MMU_TIA(nstart);
2443 a_dte = &a_tbl->at_dtbl[idx];
2444 rstart = nstart;
2445 while (rstart < nend) {
2446 if (MMU_VALID_DT(*a_dte)) {
2447 b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
2448 b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
2449 b_tbl = mmuB2tmgr(b_dte);
2450 a_dte->attr.raw = MMU_DT_INVALID;
2451 a_tbl->at_ecnt--;
2452 free_b_table(b_tbl);
2453 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
2454 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link);
2455 }
2456 a_dte++;
2457 rstart += MMU_TIA_RANGE;
2458 }
2459 }
2460 if (nend < end) {
2461 idx = MMU_TIA(nend);
2462 a_dte = &a_tbl->at_dtbl[idx];
2463 if (MMU_VALID_DT(*a_dte)) {
2464 b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
2465 b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
2466 b_tbl = mmuB2tmgr(b_dte);
2467 pmap_remove_b(b_tbl, nend, end);
2468 }
2469 }
2470 }
2471
2472 /* pmap_remove_b INTERNAL
2473 **
2474 * Remove a range of addresses from an address space, trying to remove entire
2475 * C tables if possible.
2476 */
2477 void
2478 pmap_remove_b(b_tbl, start, end)
2479 b_tmgr_t *b_tbl;
2480 vm_offset_t start;
2481 vm_offset_t end;
2482 {
2483 int idx;
2484 vm_offset_t nstart, nend, rstart;
2485 c_tmgr_t *c_tbl;
2486 mmu_short_dte_t *b_dte;
2487 mmu_short_pte_t *c_dte;
2488
2489
2490 nstart = MMU_ROUND_UP_B(start);
2491 nend = MMU_ROUND_B(end);
2492
2493 if (start < nstart) {
2494 idx = MMU_TIB(start);
2495 b_dte = &b_tbl->bt_dtbl[idx];
2496 if (MMU_VALID_DT(*b_dte)) {
2497 c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
2498 c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
2499 c_tbl = mmuC2tmgr(c_dte);
2500 if (end < nstart) {
2501 pmap_remove_c(c_tbl, start, end);
2502 return;
2503 } else {
2504 pmap_remove_c(c_tbl, start, nstart);
2505 }
2506 } else if (end < nstart) {
2507 return;
2508 }
2509 }
2510 if (nstart < nend) {
2511 idx = MMU_TIB(nstart);
2512 b_dte = &b_tbl->bt_dtbl[idx];
2513 rstart = nstart;
2514 while (rstart < nend) {
2515 if (MMU_VALID_DT(*b_dte)) {
2516 c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
2517 c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
2518 c_tbl = mmuC2tmgr(c_dte);
2519 b_dte->attr.raw = MMU_DT_INVALID;
2520 b_tbl->bt_ecnt--;
2521 free_c_table(c_tbl);
2522 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
2523 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
2524 }
2525 b_dte++;
2526 rstart += MMU_TIB_RANGE;
2527 }
2528 }
2529 if (nend < end) {
2530 idx = MMU_TIB(nend);
2531 b_dte = &b_tbl->bt_dtbl[idx];
2532 if (MMU_VALID_DT(*b_dte)) {
2533 c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
2534 c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
2535 c_tbl = mmuC2tmgr(c_dte);
2536 pmap_remove_c(c_tbl, nend, end);
2537 }
2538 }
2539 }
2540
2541 /* pmap_remove_c INTERNAL
2542 **
2543 * Remove a range of addresses from the given C table.
2544 */
2545 void
2546 pmap_remove_c(c_tbl, start, end)
2547 c_tmgr_t *c_tbl;
2548 vm_offset_t start;
2549 vm_offset_t end;
2550 {
2551 int idx;
2552 mmu_short_pte_t *c_pte;
2553
2554 idx = MMU_TIC(start);
2555 c_pte = &c_tbl->ct_dtbl[idx];
2556 while (start < end) {
2557 if (MMU_VALID_DT(*c_pte))
2558 pmap_remove_pte(c_pte);
2559 c_tbl->ct_ecnt--;
2560 start += MMU_PAGE_SIZE;
2561 c_pte++;
2562 }
2563 }
2564
2565 /* is_managed INTERNAL
2566 **
2567 * Determine if the given physical address is managed by the PV system.
2568 * Note that this logic assumes that no one will ask for the status of
2569 * addresses which lie in-between the memory banks on the 3/80. If they
2570 * do so, it will falsely report that it is managed.
2571 */
2572 boolean_t
2573 is_managed(pa)
2574 vm_offset_t pa;
2575 {
2576 if (pa >= avail_start && pa < avail_end)
2577 return TRUE;
2578 else
2579 return FALSE;
2580 }
2581
2582 /* pa2pv INTERNAL
2583 **
2584 * Return the pv_list_head element which manages the given physical
2585 * address.
2586 */
2587 pv_t *
2588 pa2pv(pa)
2589 vm_offset_t pa;
2590 {
2591 struct pmap_physmem_struct *bank = &avail_mem[0];
2592
2593 while (pa >= bank->pmem_end)
2594 bank = bank->pmem_next;
2595
2596 pa -= bank->pmem_start;
2597 return &pvbase[bank->pmem_pvbase + sun3x_btop(pa)];
2598 }
2599
2600 /* pmap_bootstrap_alloc INTERNAL
2601 **
2602 * Used internally for memory allocation at startup when malloc is not
2603 * available. This code will fail once it crosses the first memory
2604 * bank boundary on the 3/80. Hopefully by then however, the VM system
2605 * will be in charge of allocation.
2606 */
2607 void *
2608 pmap_bootstrap_alloc(size)
2609 int size;
2610 {
2611 void *rtn;
2612
2613 rtn = (void *) virtual_avail;
2614
2615 /* While the size is greater than a page, map single pages,
2616 * decreasing size until it is less than a page.
2617 */
2618 while (size > NBPG) {
2619 (void) pmap_bootstrap_alloc(NBPG);
2620
2621 /* If the above code is ok, let's keep it.
2622 * It looks cooler than:
2623 * virtual_avail += NBPG;
2624 * avail_start += NBPG;
2625 * last_mapped = sun3x_trunc_page(avail_start);
2626 * pmap_enter_kernel(last_mapped, last_mapped + KERNBASE,
2627 * VM_PROT_READ|VM_PROT_WRITE);
2628 */
2629
2630 size -= NBPG;
2631 }
2632 avail_start += size;
2633 virtual_avail += size;
2634
2635 /* did the allocation cross a page boundary? */
2636 if (last_mapped != sun3x_trunc_page(avail_start)) {
2637 last_mapped = sun3x_trunc_page(avail_start);
2638 pmap_enter_kernel(last_mapped + KERNBASE, last_mapped,
2639 VM_PROT_READ|VM_PROT_WRITE);
2640 }
2641
2642 return rtn;
2643 }
2644
2645 /* pmap_bootstap_aalign INTERNAL
2646 **
2647 * Used to insure that the next call to pmap_bootstrap_alloc() will return
2648 * a chunk of memory aligned to the specified size.
2649 */
2650 void
2651 pmap_bootstrap_aalign(size)
2652 int size;
2653 {
2654 if (((unsigned int) avail_start % size) != 0) {
2655 (void) pmap_bootstrap_alloc(size -
2656 ((unsigned int) (avail_start % size)));
2657 }
2658 }
2659
2660 #if 0
2661 /* pmap_activate INTERFACE
2662 **
2663 * Make the virtual to physical mappings contained in the given
2664 * pmap the current map used by the system.
2665 */
2666 void
2667 pmap_activate(pmap, pcbp)
2668 pmap_t pmap;
2669 struct pcb *pcbp;
2670 {
2671 vm_offset_t pa;
2672 /* Save the A table being loaded in 'curatbl'.
2673 * pmap_remove() uses this variable to determine if a given A
2674 * table is currently being used as the system map. If so, it
2675 * will issue an MMU cache flush whenever mappings are removed.
2676 */
2677 curatbl = pmap->pm_a_tbl;
2678 /* call the locore routine to set the user root pointer table */
2679 pa = mmu_vtop(pmap->pm_a_tbl->at_dtbl);
2680 mmu_seturp(pa);
2681 }
2682 #endif
2683
2684 /* pmap_pa_exists
2685 **
2686 * Used by the /dev/mem driver to see if a given PA is memory
2687 * that can be mapped. (The PA is not in a hole.)
2688 */
2689 int
2690 pmap_pa_exists(pa)
2691 vm_offset_t pa;
2692 {
2693 /* XXX - NOTYET */
2694 return (0);
2695 }
2696
2697
2698 /* pmap_update
2699 **
2700 * Apply any delayed changes scheduled for all pmaps immediately.
2701 *
2702 * No delayed operations are currently done in this pmap.
2703 */
2704 void
2705 pmap_update()
2706 {
2707 /* not implemented. */
2708 }
2709
2710 /* pmap_virtual_space INTERFACE
2711 **
2712 * Return the current available range of virtual addresses in the
2713 * arguuments provided. Only really called once.
2714 */
2715 void
2716 pmap_virtual_space(vstart, vend)
2717 vm_offset_t *vstart, *vend;
2718 {
2719 *vstart = virtual_avail;
2720 *vend = virtual_end;
2721 }
2722
2723 /* pmap_free_pages INTERFACE
2724 **
2725 * Return the number of physical pages still available.
2726 *
2727 * This is probably going to be a mess, but it's only called
2728 * once and it's the only function left that I have to implement!
2729 */
2730 u_int
2731 pmap_free_pages()
2732 {
2733 int i;
2734 u_int left;
2735 vm_offset_t avail;
2736
2737 avail = sun3x_round_up_page(avail_start);
2738
2739 left = 0;
2740 i = 0;
2741 while (avail >= avail_mem[i].pmem_end) {
2742 if (avail_mem[i].pmem_next == NULL)
2743 return 0;
2744 i++;
2745 }
2746 while (i < SUN3X_80_MEM_BANKS) {
2747 if (avail < avail_mem[i].pmem_start) {
2748 /* Avail is inside a hole, march it
2749 * up to the next bank.
2750 */
2751 avail = avail_mem[i].pmem_start;
2752 }
2753 left += sun3x_btop(avail_mem[i].pmem_end - avail);
2754 if (avail_mem[i].pmem_next == NULL)
2755 break;
2756 i++;
2757 }
2758
2759 return left;
2760 }
2761
2762 /* pmap_page_index INTERFACE
2763 **
2764 * Return the index of the given physical page in a list of useable
2765 * physical pages in the system. Holes in physical memory may be counted
2766 * if so desired. As long as pmap_free_pages() and pmap_page_index()
2767 * agree as to whether holes in memory do or do not count as valid pages,
2768 * it really doesn't matter. However, if you like to save a little
2769 * memory, don't count holes as valid pages. This is even more true when
2770 * the holes are large.
2771 *
2772 * We will not count holes as valid pages. We can generate page indexes
2773 * that conform to this by using the memory bank structures initialized
2774 * in pmap_alloc_pv().
2775 */
2776 int
2777 pmap_page_index(pa)
2778 vm_offset_t pa;
2779 {
2780 struct pmap_physmem_struct *bank = avail_mem;
2781
2782 while (pa > bank->pmem_end)
2783 bank = bank->pmem_next;
2784 pa -= bank->pmem_start;
2785
2786 return (bank->pmem_pvbase + sun3x_btop(pa));
2787 }
2788
2789 /* pmap_next_page INTERFACE
2790 **
2791 * Place the physical address of the next available page in the
2792 * argument given. Returns FALSE if there are no more pages left.
2793 *
2794 * This function must jump over any holes in physical memory.
2795 * Once this function is used, any use of pmap_bootstrap_alloc()
2796 * is a sin. Sinners will be punished with erratic behavior.
2797 */
2798 boolean_t
2799 pmap_next_page(pa)
2800 vm_offset_t *pa;
2801 {
2802 static boolean_t initialized = FALSE;
2803 static struct pmap_physmem_struct *curbank = avail_mem;
2804
2805 if (!initialized) {
2806 pmap_bootstrap_aalign(NBPG);
2807 initialized = TRUE;
2808 }
2809
2810 if (avail_start >= curbank->pmem_end)
2811 if (curbank->pmem_next == NULL)
2812 return FALSE;
2813 else {
2814 curbank = curbank->pmem_next;
2815 avail_start = curbank->pmem_start;
2816 }
2817
2818 *pa = avail_start;
2819 avail_start += NBPG;
2820 return TRUE;
2821 }
2822
2823 /************************ SUN3 COMPATIBILITY ROUTINES ********************
2824 * The following routines are only used by DDB for tricky kernel text *
2825 * text operations in db_memrw.c. They are provided for sun3 *
2826 * compatibility. *
2827 *************************************************************************/
2828 /* get_pte INTERNAL
2829 **
2830 * Return the page descriptor the describes the kernel mapping
2831 * of the given virtual address.
2832 *
2833 * XXX - It might be nice if this worked outside of the MMU
2834 * structures we manage. (Could do it with ptest). -gwr
2835 */
2836 vm_offset_t
2837 get_pte(va)
2838 vm_offset_t va;
2839 {
2840 u_long idx;
2841
2842 idx = (unsigned long) sun3x_btop(mmu_vtop(va));
2843 return (kernCbase[idx].attr.raw);
2844 }
2845
2846 /* set_pte INTERNAL
2847 **
2848 * Set the page descriptor that describes the kernel mapping
2849 * of the given virtual address.
2850 */
2851 void
2852 set_pte(va, pte)
2853 vm_offset_t va;
2854 vm_offset_t pte;
2855 {
2856 u_long idx;
2857
2858 idx = (unsigned long) sun3x_btop(mmu_vtop(va));
2859 kernCbase[idx].attr.raw = pte;
2860 }
2861
2862 #ifdef NOT_YET
2863 /* and maybe not ever */
2864 /************************** LOW-LEVEL ROUTINES **************************
2865 * These routines will eventualy be re-written into assembly and placed *
2866 * in locore.s. They are here now as stubs so that the pmap module can *
2867 * be linked as a standalone user program for testing. *
2868 ************************************************************************/
2869 /* flush_atc_crp INTERNAL
2870 **
2871 * Flush all page descriptors derived from the given CPU Root Pointer
2872 * (CRP), or 'A' table as it is known here, from the 68851's automatic
2873 * cache.
2874 */
2875 void
2876 flush_atc_crp(a_tbl)
2877 {
2878 mmu_long_rp_t rp;
2879
2880 /* Create a temporary root table pointer that points to the
2881 * given A table.
2882 */
2883 rp.attr.raw = ~MMU_LONG_RP_LU;
2884 rp.addr.raw = (unsigned int) a_tbl;
2885
2886 mmu_pflushr(&rp);
2887 /* mmu_pflushr:
2888 * movel sp(4)@,a0
2889 * pflushr a0@
2890 * rts
2891 */
2892 }
2893 #endif /* NOT_YET */
2894