pmap.c revision 1.6 1 /* $NetBSD: pmap.c,v 1.6 1997/02/02 08:41:10 thorpej 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/machdep.h>
129 #include <machine/mon.h>
130
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
804 /* Initialize the CPU Root Pointer (CRP) for proc0. */
805 /* XXX: I'd prefer per-process CRP storage. -gwr */
806 proc0crp.limit = 0x80000003; /* limit and type */
807 proc0crp.paddr = tbladdr; /* phys. addr. */
808 curpcb->pcb_mmucrp = &proc0crp;
809
810 /* mon_printf("pmap_takeover_mmu: loadcrp...\n"); */
811 loadcrp(curpcb->pcb_mmucrp);
812 /* mon_printf("pmap_takeover_mmu: survived!\n"); */
813 }
814
815 /* pmap_init INTERFACE
816 **
817 * Called at the end of vm_init() to set up the pmap system to go
818 * into full time operation.
819 */
820 void
821 pmap_init()
822 {
823 /** Initialize the manager pools **/
824 TAILQ_INIT(&a_pool);
825 TAILQ_INIT(&b_pool);
826 TAILQ_INIT(&c_pool);
827
828 /** Initialize the PV system **/
829 pmap_init_pv();
830
831 /** Zero out the kernel's pmap **/
832 bzero(&kernel_pmap, sizeof(struct pmap));
833
834 /* Initialize the A table manager that is used in pmaps which
835 * do not have an A table of their own. This table uses the
836 * kernel, or 'proc0' level A MMU table, which contains no valid
837 * user space mappings. Any user process that attempts to execute
838 * using this A table will fault. At which point the VM system will
839 * call pmap_enter, which will then allocate it an A table of its own
840 * from the pool.
841 */
842 proc0Atmgr->at_dtbl = kernAbase;
843 proc0Atmgr->at_parent = &kernel_pmap;
844 kernel_pmap.pm_a_tbl = proc0Atmgr;
845
846 /**************************************************************
847 * Initialize all tmgr structures and MMU tables they manage. *
848 **************************************************************/
849 /** Initialize A tables **/
850 pmap_init_a_tables();
851 /** Initialize B tables **/
852 pmap_init_b_tables();
853 /** Initialize C tables **/
854 pmap_init_c_tables();
855 }
856
857 /* pmap_init_a_tables() INTERNAL
858 **
859 * Initializes all A managers, their MMU A tables, and inserts
860 * them into the A manager pool for use by the system.
861 */
862 void
863 pmap_init_a_tables()
864 {
865 int i;
866 a_tmgr_t *a_tbl;
867
868 for (i=0; i < NUM_A_TABLES; i++) {
869 /* Select the next available A manager from the pool */
870 a_tbl = &Atmgrbase[i];
871
872 /* Clear its parent entry. Set its wired and valid
873 * entry count to zero.
874 */
875 a_tbl->at_parent = NULL;
876 a_tbl->at_wcnt = a_tbl->at_ecnt = 0;
877
878 /* Assign it the next available MMU A table from the pool */
879 a_tbl->at_dtbl = &mmuAbase[i * MMU_A_TBL_SIZE];
880
881 /* Initialize the MMU A table with the table in the `proc0',
882 * or kernel, mapping. This ensures that every process has
883 * the kernel mapped in the top part of its address space.
884 */
885 bcopy(kernAbase, a_tbl->at_dtbl, MMU_A_TBL_SIZE *
886 sizeof(mmu_long_dte_t));
887
888 /* Finally, insert the manager into the A pool,
889 * making it ready to be used by the system.
890 */
891 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
892 }
893 }
894
895 /* pmap_init_b_tables() INTERNAL
896 **
897 * Initializes all B table managers, their MMU B tables, and
898 * inserts them into the B manager pool for use by the system.
899 */
900 void
901 pmap_init_b_tables()
902 {
903 int i,j;
904 b_tmgr_t *b_tbl;
905
906 for (i=0; i < NUM_B_TABLES; i++) {
907 /* Select the next available B manager from the pool */
908 b_tbl = &Btmgrbase[i];
909
910 b_tbl->bt_parent = NULL; /* clear its parent, */
911 b_tbl->bt_pidx = 0; /* parent index, */
912 b_tbl->bt_wcnt = 0; /* wired entry count, */
913 b_tbl->bt_ecnt = 0; /* valid entry count. */
914
915 /* Assign it the next available MMU B table from the pool */
916 b_tbl->bt_dtbl = &mmuBbase[i * MMU_B_TBL_SIZE];
917
918 /* Invalidate every descriptor in the table */
919 for (j=0; j < MMU_B_TBL_SIZE; j++)
920 b_tbl->bt_dtbl[j].attr.raw = MMU_DT_INVALID;
921
922 /* Insert the manager into the B pool */
923 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
924 }
925 }
926
927 /* pmap_init_c_tables() INTERNAL
928 **
929 * Initializes all C table managers, their MMU C tables, and
930 * inserts them into the C manager pool for use by the system.
931 */
932 void
933 pmap_init_c_tables()
934 {
935 int i,j;
936 c_tmgr_t *c_tbl;
937
938 for (i=0; i < NUM_C_TABLES; i++) {
939 /* Select the next available C manager from the pool */
940 c_tbl = &Ctmgrbase[i];
941
942 c_tbl->ct_parent = NULL; /* clear its parent, */
943 c_tbl->ct_pidx = 0; /* parent index, */
944 c_tbl->ct_wcnt = 0; /* wired entry count, */
945 c_tbl->ct_ecnt = 0; /* valid entry count. */
946
947 /* Assign it the next available MMU C table from the pool */
948 c_tbl->ct_dtbl = &mmuCbase[i * MMU_C_TBL_SIZE];
949
950 for (j=0; j < MMU_C_TBL_SIZE; j++)
951 c_tbl->ct_dtbl[j].attr.raw = MMU_DT_INVALID;
952
953 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
954 }
955 }
956
957 /* pmap_init_pv() INTERNAL
958 **
959 * Initializes the Physical to Virtual mapping system.
960 */
961 void
962 pmap_init_pv()
963 {
964 bzero(pvbase, sizeof(pv_t) * sun3x_btop(total_phys_mem));
965 pv_initialized = TRUE;
966 }
967
968 /* get_a_table INTERNAL
969 **
970 * Retrieve and return a level A table for use in a user map.
971 */
972 a_tmgr_t *
973 get_a_table()
974 {
975 a_tmgr_t *tbl;
976
977 /* Get the top A table in the pool */
978 tbl = a_pool.tqh_first;
979 if (tbl == NULL)
980 panic("get_a_table: out of A tables.");
981 TAILQ_REMOVE(&a_pool, tbl, at_link);
982 /* If the table has a non-null parent pointer then it is in use.
983 * Forcibly abduct it from its parent and clear its entries.
984 * No re-entrancy worries here. This table would not be in the
985 * table pool unless it was available for use.
986 */
987 if (tbl->at_parent) {
988 tbl->at_parent->pm_stats.resident_count -= free_a_table(tbl);
989 tbl->at_parent->pm_a_tbl = proc0Atmgr;
990 }
991 #ifdef NON_REENTRANT
992 /* If the table isn't to be wired down, re-insert it at the
993 * end of the pool.
994 */
995 if (!wired)
996 /* Quandary - XXX
997 * Would it be better to let the calling function insert this
998 * table into the queue? By inserting it here, we are allowing
999 * it to be stolen immediately. The calling function is
1000 * probably not expecting to use a table that it is not
1001 * assured full control of.
1002 * Answer - In the intrest of re-entrancy, it is best to let
1003 * the calling function determine when a table is available
1004 * for use. Therefore this code block is not used.
1005 */
1006 TAILQ_INSERT_TAIL(&a_pool, tbl, at_link);
1007 #endif /* NON_REENTRANT */
1008 return tbl;
1009 }
1010
1011 /* get_b_table INTERNAL
1012 **
1013 * Return a level B table for use.
1014 */
1015 b_tmgr_t *
1016 get_b_table()
1017 {
1018 b_tmgr_t *tbl;
1019
1020 /* See 'get_a_table' for comments. */
1021 tbl = b_pool.tqh_first;
1022 if (tbl == NULL)
1023 panic("get_b_table: out of B tables.");
1024 TAILQ_REMOVE(&b_pool, tbl, bt_link);
1025 if (tbl->bt_parent) {
1026 tbl->bt_parent->at_dtbl[tbl->bt_pidx].attr.raw = MMU_DT_INVALID;
1027 tbl->bt_parent->at_ecnt--;
1028 tbl->bt_parent->at_parent->pm_stats.resident_count -=
1029 free_b_table(tbl);
1030 }
1031 #ifdef NON_REENTRANT
1032 if (!wired)
1033 /* XXX see quandary in get_b_table */
1034 /* XXX start lock */
1035 TAILQ_INSERT_TAIL(&b_pool, tbl, bt_link);
1036 /* XXX end lock */
1037 #endif /* NON_REENTRANT */
1038 return tbl;
1039 }
1040
1041 /* get_c_table INTERNAL
1042 **
1043 * Return a level C table for use.
1044 */
1045 c_tmgr_t *
1046 get_c_table()
1047 {
1048 c_tmgr_t *tbl;
1049
1050 /* See 'get_a_table' for comments */
1051 tbl = c_pool.tqh_first;
1052 if (tbl == NULL)
1053 panic("get_c_table: out of C tables.");
1054 TAILQ_REMOVE(&c_pool, tbl, ct_link);
1055 if (tbl->ct_parent) {
1056 tbl->ct_parent->bt_dtbl[tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1057 tbl->ct_parent->bt_ecnt--;
1058 tbl->ct_parent->bt_parent->at_parent->pm_stats.resident_count
1059 -= free_c_table(tbl);
1060 }
1061 #ifdef NON_REENTRANT
1062 if (!wired)
1063 /* XXX See quandary in get_a_table */
1064 /* XXX start lock */
1065 TAILQ_INSERT_TAIL(&c_pool, tbl, c_link);
1066 /* XXX end lock */
1067 #endif /* NON_REENTRANT */
1068
1069 return tbl;
1070 }
1071
1072 /* The following 'free_table' and 'steal_table' functions are called to
1073 * detach tables from their current obligations (parents and children) and
1074 * prepare them for reuse in another mapping.
1075 *
1076 * Free_table is used when the calling function will handle the fate
1077 * of the parent table, such as returning it to the free pool when it has
1078 * no valid entries. Functions that do not want to handle this should
1079 * call steal_table, in which the parent table's descriptors and entry
1080 * count are automatically modified when this table is removed.
1081 */
1082
1083 /* free_a_table INTERNAL
1084 **
1085 * Unmaps the given A table and all child tables from their current
1086 * mappings. Returns the number of pages that were invalidated.
1087 *
1088 * Cache note: The MC68851 will automatically flush all
1089 * descriptors derived from a given A table from its
1090 * Automatic Translation Cache (ATC) if we issue a
1091 * 'PFLUSHR' instruction with the base address of the
1092 * table. This function should do, and does so.
1093 * Note note: We are using an MC68030 - there is no
1094 * PFLUSHR.
1095 */
1096 int
1097 free_a_table(a_tbl)
1098 a_tmgr_t *a_tbl;
1099 {
1100 int i, removed_cnt;
1101 mmu_long_dte_t *dte;
1102 mmu_short_dte_t *dtbl;
1103 b_tmgr_t *tmgr;
1104
1105 /* Flush the ATC cache of all cached descriptors derived
1106 * from this table.
1107 * XXX - Sun3x does not use 68851's cached table feature
1108 * flush_atc_crp(mmu_vtop(a_tbl->dte));
1109 */
1110
1111 /* Remove any pending cache flushes that were designated
1112 * for the pmap this A table belongs to.
1113 * a_tbl->parent->atc_flushq[0] = 0;
1114 * XXX - Not implemented in sun3x.
1115 */
1116
1117 /* All A tables in the system should retain a map for the
1118 * kernel. If the table contains any valid descriptors
1119 * (other than those for the kernel area), invalidate them all,
1120 * stopping short of the kernel's entries.
1121 */
1122 removed_cnt = 0;
1123 if (a_tbl->at_ecnt) {
1124 dte = a_tbl->at_dtbl;
1125 for (i=0; i < MMU_TIA(KERNBASE); i++)
1126 /* If a table entry points to a valid B table, free
1127 * it and its children.
1128 */
1129 if (MMU_VALID_DT(dte[i])) {
1130 /* The following block does several things,
1131 * from innermost expression to the
1132 * outermost:
1133 * 1) It extracts the base (cc 1996)
1134 * address of the B table pointed
1135 * to in the A table entry dte[i].
1136 * 2) It converts this base address into
1137 * the virtual address it can be
1138 * accessed with. (all MMU tables point
1139 * to physical addresses.)
1140 * 3) It finds the corresponding manager
1141 * structure which manages this MMU table.
1142 * 4) It frees the manager structure.
1143 * (This frees the MMU table and all
1144 * child tables. See 'free_b_table' for
1145 * details.)
1146 */
1147 dtbl = (mmu_short_dte_t *) MMU_DTE_PA(dte[i]);
1148 dtbl = (mmu_short_dte_t *) mmu_ptov(dtbl);
1149 tmgr = mmuB2tmgr(dtbl);
1150 removed_cnt += free_b_table(tmgr);
1151 }
1152 }
1153 a_tbl->at_ecnt = 0;
1154 return removed_cnt;
1155 }
1156
1157 /* free_b_table INTERNAL
1158 **
1159 * Unmaps the given B table and all its children from their current
1160 * mappings. Returns the number of pages that were invalidated.
1161 * (For comments, see 'free_a_table()').
1162 */
1163 int
1164 free_b_table(b_tbl)
1165 b_tmgr_t *b_tbl;
1166 {
1167 int i, removed_cnt;
1168 mmu_short_dte_t *dte;
1169 mmu_short_pte_t *dtbl;
1170 c_tmgr_t *tmgr;
1171
1172 removed_cnt = 0;
1173 if (b_tbl->bt_ecnt) {
1174 dte = b_tbl->bt_dtbl;
1175 for (i=0; i < MMU_B_TBL_SIZE; i++)
1176 if (MMU_VALID_DT(dte[i])) {
1177 dtbl = (mmu_short_pte_t *) MMU_DTE_PA(dte[i]);
1178 dtbl = (mmu_short_pte_t *) mmu_ptov(dtbl);
1179 tmgr = mmuC2tmgr(dtbl);
1180 removed_cnt += free_c_table(tmgr);
1181 }
1182 }
1183
1184 b_tbl->bt_ecnt = 0;
1185 return removed_cnt;
1186 }
1187
1188 /* free_c_table INTERNAL
1189 **
1190 * Unmaps the given C table from use and returns it to the pool for
1191 * re-use. Returns the number of pages that were invalidated.
1192 *
1193 * This function preserves any physical page modification information
1194 * contained in the page descriptors within the C table by calling
1195 * 'pmap_remove_pte().'
1196 */
1197 int
1198 free_c_table(c_tbl)
1199 c_tmgr_t *c_tbl;
1200 {
1201 int i, removed_cnt;
1202
1203 removed_cnt = 0;
1204 if (c_tbl->ct_ecnt)
1205 for (i=0; i < MMU_C_TBL_SIZE; i++)
1206 if (MMU_VALID_DT(c_tbl->ct_dtbl[i])) {
1207 pmap_remove_pte(&c_tbl->ct_dtbl[i]);
1208 removed_cnt++;
1209 }
1210 c_tbl->ct_ecnt = 0;
1211 return removed_cnt;
1212 }
1213
1214 /* free_c_table_novalid INTERNAL
1215 **
1216 * Frees the given C table manager without checking to see whether
1217 * or not it contains any valid page descriptors as it is assumed
1218 * that it does not.
1219 */
1220 void
1221 free_c_table_novalid(c_tbl)
1222 c_tmgr_t *c_tbl;
1223 {
1224 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1225 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
1226 c_tbl->ct_parent->bt_dtbl[c_tbl->ct_pidx].attr.raw = MMU_DT_INVALID;
1227 }
1228
1229 /* pmap_remove_pte INTERNAL
1230 **
1231 * Unmap the given pte and preserve any page modification
1232 * information by transfering it to the pv head of the
1233 * physical page it maps to. This function does not update
1234 * any reference counts because it is assumed that the calling
1235 * function will do so. If the calling function does not have the
1236 * ability to do so, the function pmap_dereference_pte() exists
1237 * for this purpose.
1238 */
1239 void
1240 pmap_remove_pte(pte)
1241 mmu_short_pte_t *pte;
1242 {
1243 vm_offset_t pa;
1244 pv_t *pv;
1245 pv_elem_t *pve;
1246
1247 pa = MMU_PTE_PA(*pte);
1248 if (is_managed(pa)) {
1249 pv = pa2pv(pa);
1250 /* Save the mod/ref bits of the pte by simply
1251 * ORing the entire pte onto the pv_flags member
1252 * of the pv structure.
1253 * There is no need to use a separate bit pattern
1254 * for usage information on the pv head than that
1255 * which is used on the MMU ptes.
1256 */
1257 pv->pv_flags |= pte->attr.raw;
1258
1259 pve = pte2pve(pte);
1260 if (pve == pv->pv_head.lh_first)
1261 pv->pv_head.lh_first = pve->pve_link.le_next;
1262 LIST_REMOVE(pve, pve_link);
1263 }
1264
1265 pte->attr.raw = MMU_DT_INVALID;
1266 }
1267
1268 /* pmap_dereference_pte INTERNAL
1269 **
1270 * Update the necessary reference counts in any tables and pmaps to
1271 * reflect the removal of the given pte. Only called when no knowledge of
1272 * the pte's associated pmap is unknown. This only occurs in the PV call
1273 * 'pmap_page_protect()' with a protection of VM_PROT_NONE, which means
1274 * that all references to a given physical page must be removed.
1275 */
1276 void
1277 pmap_dereference_pte(pte)
1278 mmu_short_pte_t *pte;
1279 {
1280 c_tmgr_t *c_tbl;
1281
1282 c_tbl = pmap_find_c_tmgr(pte);
1283 c_tbl->ct_parent->bt_parent->at_parent->pm_stats.resident_count--;
1284 if (--c_tbl->ct_ecnt == 0)
1285 free_c_table_novalid(c_tbl);
1286 }
1287
1288 /* pmap_stroll INTERNAL
1289 **
1290 * Retrieve the addresses of all table managers involved in the mapping of
1291 * the given virtual address. If the table walk completed sucessfully,
1292 * return TRUE. If it was only partial sucessful, return FALSE.
1293 * The table walk performed by this function is important to many other
1294 * functions in this module.
1295 */
1296 boolean_t
1297 pmap_stroll(pmap, va, a_tbl, b_tbl, c_tbl, pte, a_idx, b_idx, pte_idx)
1298 pmap_t pmap;
1299 vm_offset_t va;
1300 a_tmgr_t **a_tbl;
1301 b_tmgr_t **b_tbl;
1302 c_tmgr_t **c_tbl;
1303 mmu_short_pte_t **pte;
1304 int *a_idx, *b_idx, *pte_idx;
1305 {
1306 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1307 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1308
1309 if (pmap == pmap_kernel())
1310 return FALSE;
1311
1312 /* Does the given pmap have an A table? */
1313 *a_tbl = pmap->pm_a_tbl;
1314 if (*a_tbl == NULL)
1315 return FALSE; /* No. Return unknown. */
1316 /* Does the A table have a valid B table
1317 * under the corresponding table entry?
1318 */
1319 *a_idx = MMU_TIA(va);
1320 a_dte = &((*a_tbl)->at_dtbl[*a_idx]);
1321 if (!MMU_VALID_DT(*a_dte))
1322 return FALSE; /* No. Return unknown. */
1323 /* Yes. Extract B table from the A table. */
1324 *b_tbl = pmap_find_b_tmgr(
1325 (mmu_short_dte_t *) mmu_ptov(
1326 MMU_DTE_PA(*a_dte)
1327 )
1328 );
1329 /* Does the B table have a valid C table
1330 * under the corresponding table entry?
1331 */
1332 *b_idx = MMU_TIB(va);
1333 b_dte = &((*b_tbl)->bt_dtbl[*b_idx]);
1334 if (!MMU_VALID_DT(*b_dte))
1335 return FALSE; /* No. Return unknown. */
1336 /* Yes. Extract C table from the B table. */
1337 *c_tbl = pmap_find_c_tmgr(
1338 (mmu_short_pte_t *) mmu_ptov(
1339 MMU_DTE_PA(*b_dte)
1340 )
1341 );
1342 *pte_idx = MMU_TIC(va);
1343 *pte = &((*c_tbl)->ct_dtbl[*pte_idx]);
1344
1345 return TRUE;
1346 }
1347
1348 /* pmap_enter INTERFACE
1349 **
1350 * Called by the kernel to map a virtual address
1351 * to a physical address in the given process map.
1352 *
1353 * Note: this function should apply an exclusive lock
1354 * on the pmap system for its duration. (it certainly
1355 * would save my hair!!)
1356 */
1357 void
1358 pmap_enter(pmap, va, pa, prot, wired)
1359 pmap_t pmap;
1360 vm_offset_t va;
1361 vm_offset_t pa;
1362 vm_prot_t prot;
1363 boolean_t wired;
1364 {
1365 u_int a_idx, b_idx, pte_idx; /* table indexes (fix grammar) */
1366 a_tmgr_t *a_tbl; /* A: long descriptor table manager */
1367 b_tmgr_t *b_tbl; /* B: short descriptor table manager */
1368 c_tmgr_t *c_tbl; /* C: short page table manager */
1369 mmu_long_dte_t *a_dte; /* A: long descriptor table */
1370 mmu_short_dte_t *b_dte; /* B: short descriptor table */
1371 mmu_short_pte_t *c_pte; /* C: short page descriptor table */
1372 pv_t *pv; /* pv list head */
1373 pv_elem_t *pve; /* pv element */
1374 enum {NONE, NEWA, NEWB, NEWC} llevel; /* used at end */
1375
1376 if (pmap == NULL)
1377 return;
1378 if (pmap == pmap_kernel()) {
1379 pmap_enter_kernel(va, pa, prot);
1380 return;
1381 }
1382
1383 /* For user mappings we walk along the MMU tables of the given
1384 * pmap, reaching a PTE which describes the virtual page being
1385 * mapped or changed. If any level of the walk ends in an invalid
1386 * entry, a table must be allocated and the entry must be updated
1387 * to point to it.
1388 * There is a bit of confusion as to whether this code must be
1389 * re-entrant. For now we will assume it is. To support
1390 * re-entrancy we must unlink tables from the table pool before
1391 * we assume we may use them. Tables are re-linked into the pool
1392 * when we are finished with them at the end of the function.
1393 * But I don't feel like doing that until we have proof that this
1394 * needs to be re-entrant.
1395 * 'llevel' records which tables need to be relinked.
1396 */
1397 llevel = NONE;
1398
1399 /* Step 1 - Retrieve the A table from the pmap. If it is the default
1400 * A table (commonly known as the 'proc0' A table), allocate a new one.
1401 */
1402
1403 a_tbl = pmap->pm_a_tbl;
1404 if (a_tbl == proc0Atmgr) {
1405 pmap->pm_a_tbl = a_tbl = get_a_table();
1406 if (!wired)
1407 llevel = NEWA;
1408 } else {
1409 /* Use the A table already allocated for this pmap.
1410 * Unlink it from the A table pool if necessary.
1411 */
1412 if (wired && !a_tbl->at_wcnt)
1413 TAILQ_REMOVE(&a_pool, a_tbl, at_link);
1414 }
1415
1416 /* Step 2 - Walk into the B table. If there is no valid B table,
1417 * allocate one.
1418 */
1419
1420 a_idx = MMU_TIA(va); /* Calculate the TIA of the VA. */
1421 a_dte = &a_tbl->at_dtbl[a_idx]; /* Retrieve descriptor from table */
1422 if (MMU_VALID_DT(*a_dte)) { /* Is the descriptor valid? */
1423 /* Yes, it points to a valid B table. Use it. */
1424 /*************************************
1425 * a_idx *
1426 * v *
1427 * a_tbl -> +-+-+-+-+-+-+-+-+-+-+-+- *
1428 * | | | | | | | | | | | | *
1429 * +-+-+-+-+-+-+-+-+-+-+-+- *
1430 * | *
1431 * \- b_tbl -> +-+- *
1432 * | | *
1433 * +-+- *
1434 *************************************/
1435 b_dte = (mmu_short_dte_t *) mmu_ptov(a_dte->addr.raw);
1436 b_tbl = mmuB2tmgr(b_dte);
1437 if (wired && !b_tbl->bt_wcnt) {
1438 /* If mapping is wired and table is not */
1439 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
1440 a_tbl->at_wcnt++; /* Update parent table's wired
1441 * entry count. */
1442 }
1443 } else {
1444 b_tbl = get_b_table(); /* No, need to allocate a new B table */
1445 /* Point the parent A table descriptor to this new B table. */
1446 a_dte->addr.raw = (unsigned long) mmu_vtop(b_tbl->bt_dtbl);
1447 a_dte->attr.attr_struct.dt = MMU_DT_SHORT;
1448 /* Create the necessary back references to the parent table */
1449 b_tbl->bt_parent = a_tbl;
1450 b_tbl->bt_pidx = a_idx;
1451 /* If this table is to be wired, make sure the parent A table
1452 * wired count is updated to reflect that it has another wired
1453 * entry.
1454 */
1455 a_tbl->at_ecnt++; /* Update parent's valid entry count */
1456 if (wired)
1457 a_tbl->at_wcnt++;
1458 else if (llevel == NONE)
1459 llevel = NEWB;
1460 }
1461
1462 /* Step 3 - Walk into the C table, if there is no valid C table,
1463 * allocate one.
1464 */
1465
1466 b_idx = MMU_TIB(va); /* Calculate the TIB of the VA */
1467 b_dte = &b_tbl->bt_dtbl[b_idx]; /* Retrieve descriptor from table */
1468 if (MMU_VALID_DT(*b_dte)) { /* Is the descriptor valid? */
1469 /* Yes, it points to a valid C table. Use it. */
1470 /**************************************
1471 * c_idx *
1472 * | v *
1473 * \- b_tbl -> +-+-+-+-+-+-+-+-+-+-+- *
1474 * | | | | | | | | | | | *
1475 * +-+-+-+-+-+-+-+-+-+-+- *
1476 * | *
1477 * \- c_tbl -> +-+-- *
1478 * | | | *
1479 * +-+-- *
1480 **************************************/
1481 c_pte = (mmu_short_pte_t *) MMU_PTE_PA(*b_dte);
1482 c_pte = (mmu_short_pte_t *) mmu_ptov(c_pte);
1483 c_tbl = mmuC2tmgr(c_pte);
1484 if (wired && !c_tbl->ct_wcnt) {
1485 /* If mapping is wired and table is not */
1486 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
1487 b_tbl->bt_wcnt++;
1488 }
1489 } else {
1490 c_tbl = get_c_table(); /* No, need to allocate a new C table */
1491 /* Point the parent B table descriptor to this new C table. */
1492 b_dte->attr.raw = (unsigned long) mmu_vtop(c_tbl->ct_dtbl);
1493 b_dte->attr.attr_struct.dt = MMU_DT_SHORT;
1494 /* Create the necessary back references to the parent table */
1495 c_tbl->ct_parent = b_tbl;
1496 c_tbl->ct_pidx = b_idx;
1497 /* If this table is to be wired, make sure the parent B table
1498 * wired count is updated to reflect that it has another wired
1499 * entry.
1500 */
1501 b_tbl->bt_ecnt++; /* Update parent's valid entry count */
1502 if (wired)
1503 b_tbl->bt_wcnt++;
1504 else if (llevel == NONE)
1505 llevel = NEWC;
1506 }
1507
1508 /* Step 4 - Deposit a page descriptor (PTE) into the appropriate
1509 * slot of the C table, describing the PA to which the VA is mapped.
1510 */
1511
1512 pte_idx = MMU_TIC(va);
1513 c_pte = &c_tbl->ct_dtbl[pte_idx];
1514 if (MMU_VALID_DT(*c_pte)) { /* Is the entry currently valid? */
1515 /* If the PTE is currently valid, then this function call
1516 * is just a synonym for one (or more) of the following
1517 * operations:
1518 * change protections on a page
1519 * change wiring status of a page
1520 * remove the mapping of a page
1521 */
1522 /* Is the new address the same as the old? */
1523 if (MMU_PTE_PA(*c_pte) == pa) {
1524 /* Yes, do nothing. */
1525 } else {
1526 /* No, remove the old entry */
1527 pmap_remove_pte(c_pte);
1528 }
1529 } else {
1530 /* No, update the valid entry count in the C table */
1531 c_tbl->ct_ecnt++;
1532 /* and in pmap */
1533 pmap->pm_stats.resident_count++;
1534 }
1535 /* Map the page. */
1536 c_pte->attr.raw = ((unsigned long) pa | MMU_DT_PAGE);
1537
1538 if (wired) /* Does the entry need to be wired? */ {
1539 c_pte->attr.raw |= MMU_SHORT_PTE_WIRED;
1540 }
1541
1542 /* If the physical address being mapped is managed by the PV
1543 * system then link the pte into the list of pages mapped to that
1544 * address.
1545 */
1546 if (is_managed(pa)) {
1547 pv = pa2pv(pa);
1548 pve = pte2pve(c_pte);
1549 LIST_INSERT_HEAD(&pv->pv_head, pve, pve_link);
1550 }
1551
1552 /* Move any allocated tables back into the active pool. */
1553
1554 switch (llevel) {
1555 case NEWA:
1556 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
1557 /* FALLTHROUGH */
1558 case NEWB:
1559 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
1560 /* FALLTHROUGH */
1561 case NEWC:
1562 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
1563 /* FALLTHROUGH */
1564 default:
1565 break;
1566 }
1567 }
1568
1569 /* pmap_enter_kernel INTERNAL
1570 **
1571 * Map the given virtual address to the given physical address within the
1572 * kernel address space. This function exists because the kernel map does
1573 * not do dynamic table allocation. It consists of a contiguous array of ptes
1574 * and can be edited directly without the need to walk through any tables.
1575 *
1576 * XXX: "Danger, Will Robinson!"
1577 * Note that the kernel should never take a fault on any page
1578 * between [ KERNBASE .. virtual_avail ] and this is checked in
1579 * trap.c for kernel-mode MMU faults. This means that mappings
1580 * created in that range must be implicily wired. -gwr
1581 */
1582 void
1583 pmap_enter_kernel(va, pa, prot)
1584 vm_offset_t va;
1585 vm_offset_t pa;
1586 vm_prot_t prot;
1587 {
1588 boolean_t was_valid = FALSE;
1589 mmu_short_pte_t *pte;
1590
1591 /* XXX - This array is traditionally named "Sysmap" */
1592 pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
1593 if (MMU_VALID_DT(*pte))
1594 was_valid = TRUE;
1595
1596 pte->attr.raw = (pa | MMU_DT_PAGE);
1597
1598 if (!(prot & VM_PROT_WRITE)) /* If access should be read-only */
1599 pte->attr.raw |= MMU_SHORT_PTE_WP;
1600 if (pa & PMAP_NC)
1601 pte->attr.raw |= MMU_SHORT_PTE_CI;
1602 if (was_valid) {
1603 /* mmu_flusha(FC_SUPERD, va); */
1604 /* mmu_flusha(); */
1605 TBIA();
1606 }
1607
1608 }
1609
1610 /* pmap_protect INTERFACE
1611 **
1612 * Apply the given protection to the given virtual address within
1613 * the given map.
1614 *
1615 * It is ok for the protection applied to be stronger than what is
1616 * specified. We use this to our advantage when the given map has no
1617 * mapping for the virtual address. By returning immediately when this
1618 * is discovered, we are effectively applying a protection of VM_PROT_NONE,
1619 * and therefore do not need to map the page just to apply a protection
1620 * code. Only pmap_enter() needs to create new mappings if they do not exist.
1621 */
1622 void
1623 pmap_protect(pmap, va, pa, prot)
1624 pmap_t pmap;
1625 vm_offset_t va, pa;
1626 vm_prot_t prot;
1627 {
1628 int a_idx, b_idx, c_idx;
1629 a_tmgr_t *a_tbl;
1630 b_tmgr_t *b_tbl;
1631 c_tmgr_t *c_tbl;
1632 mmu_short_pte_t *pte;
1633
1634 if (pmap == NULL)
1635 return;
1636 if (pmap == pmap_kernel()) {
1637 pmap_protect_kernel(va, pa, prot);
1638 return;
1639 }
1640
1641 /* Retrieve the mapping from the given pmap. If it does
1642 * not exist then we need not do anything more.
1643 */
1644 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte,
1645 &a_idx, &b_idx, &c_idx) == FALSE) {
1646 return;
1647 }
1648
1649 switch (prot) {
1650 case VM_PROT_ALL:
1651 /* this should never happen in a sane system */
1652 break;
1653 case VM_PROT_READ:
1654 case VM_PROT_READ|VM_PROT_EXECUTE:
1655 /* make the mapping read-only */
1656 pte->attr.raw |= MMU_SHORT_PTE_WP;
1657 break;
1658 case VM_PROT_NONE:
1659 /* this is an alias for 'pmap_remove' */
1660 pmap_dereference_pte(pte);
1661 break;
1662 default:
1663 break;
1664 }
1665 }
1666
1667 /* pmap_protect_kernel INTERNAL
1668 **
1669 * Apply the given protection code to a kernel address mapping.
1670 */
1671 void
1672 pmap_protect_kernel(va, pa, prot)
1673 vm_offset_t va, pa;
1674 vm_prot_t prot;
1675 {
1676 mmu_short_pte_t *pte;
1677
1678 pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
1679 if (MMU_VALID_DT(*pte)) {
1680 switch (prot) {
1681 case VM_PROT_ALL:
1682 break;
1683 case VM_PROT_READ:
1684 case VM_PROT_READ|VM_PROT_EXECUTE:
1685 pte->attr.raw |= MMU_SHORT_PTE_WP;
1686 break;
1687 case VM_PROT_NONE:
1688 /* this is an alias for 'pmap_remove_kernel' */
1689 pte->attr.raw = MMU_DT_INVALID;
1690 break;
1691 default:
1692 break;
1693 }
1694 }
1695 /* since this is the kernel, immediately flush any cached
1696 * descriptors for this address.
1697 */
1698 /* mmu_flush(FC_SUPERD, va); */
1699 TBIS(va);
1700 }
1701
1702 /* pmap_change_wiring INTERFACE
1703 **
1704 * Changes the wiring of the specified page.
1705 *
1706 * This function is called from vm_fault.c to unwire
1707 * a mapping. It really should be called 'pmap_unwire'
1708 * because it is never asked to do anything but remove
1709 * wirings.
1710 */
1711 void
1712 pmap_change_wiring(pmap, va, wire)
1713 pmap_t pmap;
1714 vm_offset_t va;
1715 boolean_t wire;
1716 {
1717 int a_idx, b_idx, c_idx;
1718 a_tmgr_t *a_tbl;
1719 b_tmgr_t *b_tbl;
1720 c_tmgr_t *c_tbl;
1721 mmu_short_pte_t *pte;
1722
1723 /* Kernel mappings always remain wired. */
1724 if (pmap == pmap_kernel())
1725 return;
1726
1727 #ifdef PMAP_DEBUG
1728 if (wire == TRUE)
1729 panic("pmap_change_wiring: wire requested.");
1730 #endif
1731
1732 /* Walk through the tables. If the walk terminates without
1733 * a valid PTE then the address wasn't wired in the first place.
1734 * Return immediately.
1735 */
1736 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl, &pte, &a_idx,
1737 &b_idx, &c_idx) == FALSE)
1738 return;
1739
1740
1741 /* Is the PTE wired? If not, return. */
1742 if (!(pte->attr.raw & MMU_SHORT_PTE_WIRED))
1743 return;
1744
1745 /* Remove the wiring bit. */
1746 pte->attr.raw &= ~(MMU_SHORT_PTE_WIRED);
1747
1748 /* Decrement the wired entry count in the C table.
1749 * If it reaches zero the following things happen:
1750 * 1. The table no longer has any wired entries and is considered
1751 * unwired.
1752 * 2. It is placed on the available queue.
1753 * 3. The parent table's wired entry count is decremented.
1754 * 4. If it reaches zero, this process repeats at step 1 and
1755 * stops at after reaching the A table.
1756 */
1757 if (c_tbl->ct_wcnt-- == 0) {
1758 TAILQ_INSERT_TAIL(&c_pool, c_tbl, ct_link);
1759 if (b_tbl->bt_wcnt-- == 0) {
1760 TAILQ_INSERT_TAIL(&b_pool, b_tbl, bt_link);
1761 if (a_tbl->at_wcnt-- == 0) {
1762 TAILQ_INSERT_TAIL(&a_pool, a_tbl, at_link);
1763 }
1764 }
1765 }
1766
1767 pmap->pm_stats.wired_count--;
1768 }
1769
1770 /* pmap_pageable INTERFACE
1771 **
1772 * Make the specified range of addresses within the given pmap,
1773 * 'pageable' or 'not-pageable'. A pageable page must not cause
1774 * any faults when referenced. A non-pageable page may.
1775 *
1776 * This routine is only advisory. The VM system will call pmap_enter()
1777 * to wire or unwire pages that are going to be made pageable before calling
1778 * this function. By the time this routine is called, everything that needs
1779 * to be done has already been done.
1780 */
1781 void
1782 pmap_pageable(pmap, start, end, pageable)
1783 pmap_t pmap;
1784 vm_offset_t start, end;
1785 boolean_t pageable;
1786 {
1787 /* not implemented. */
1788 }
1789
1790 /* pmap_copy INTERFACE
1791 **
1792 * Copy the mappings of a range of addresses in one pmap, into
1793 * the destination address of another.
1794 *
1795 * This routine is advisory. Should we one day decide that MMU tables
1796 * may be shared by more than one pmap, this function should be used to
1797 * link them together. Until that day however, we do nothing.
1798 */
1799 void
1800 pmap_copy(pmap_a, pmap_b, dst, len, src)
1801 pmap_t pmap_a, pmap_b;
1802 vm_offset_t dst;
1803 vm_size_t len;
1804 vm_offset_t src;
1805 {
1806 /* not implemented. */
1807 }
1808
1809 /* pmap_copy_page INTERFACE
1810 **
1811 * Copy the contents of one physical page into another.
1812 *
1813 * This function makes use of two virtual pages allocated in sun3x_vm_init()
1814 * (found in _startup.c) to map the two specified physical pages into the
1815 * kernel address space. It then uses bcopy() to copy one into the other.
1816 */
1817 void
1818 pmap_copy_page(src, dst)
1819 vm_offset_t src, dst;
1820 {
1821 PMAP_LOCK();
1822 if (tmp_vpages_inuse)
1823 panic("pmap_copy_page: temporary vpages are in use.");
1824 tmp_vpages_inuse++;
1825
1826 pmap_enter_kernel(tmp_vpages[0], src, VM_PROT_READ);
1827 pmap_enter_kernel(tmp_vpages[1], dst, VM_PROT_READ|VM_PROT_WRITE);
1828 copypage((char *) tmp_vpages[1], (char *) tmp_vpages[0]);
1829 /* xxx - there's no real need to unmap the mappings is there? */
1830
1831 tmp_vpages_inuse--;
1832 PMAP_UNLOCK();
1833 }
1834
1835 /* pmap_zero_page INTERFACE
1836 **
1837 * Zero the contents of the specified physical page.
1838 *
1839 * Uses one of the virtual pages allocated in sun3x_vm_init() (_startup.c)
1840 * to map the specified page into the kernel address space. Then uses
1841 * bzero() to zero out the page.
1842 */
1843 void
1844 pmap_zero_page(pa)
1845 vm_offset_t pa;
1846 {
1847 PMAP_LOCK();
1848 if (tmp_vpages_inuse)
1849 panic("pmap_zero_page: temporary vpages are in use.");
1850 tmp_vpages_inuse++;
1851
1852 pmap_enter_kernel(tmp_vpages[0], pa, VM_PROT_READ|VM_PROT_WRITE);
1853 zeropage((char *) tmp_vpages[0]);
1854 /* xxx - there's no real need to unmap the mapping is there? */
1855
1856 tmp_vpages_inuse--;
1857 PMAP_UNLOCK();
1858 }
1859
1860 /* pmap_collect INTERFACE
1861 **
1862 * Called from the VM system to collect unused pages in the given
1863 * pmap.
1864 *
1865 * No one implements it, so I'm not even sure how it is supposed to
1866 * 'collect' anything anyways. There's nothing to do but do what everyone
1867 * else does..
1868 */
1869 void
1870 pmap_collect(pmap)
1871 pmap_t pmap;
1872 {
1873 /* not implemented. */
1874 }
1875
1876 /* pmap_create INTERFACE
1877 **
1878 * Create and return a pmap structure.
1879 */
1880 pmap_t
1881 pmap_create(size)
1882 vm_size_t size;
1883 {
1884 pmap_t pmap;
1885
1886 if (size)
1887 return NULL;
1888
1889 pmap = (pmap_t) malloc(sizeof(struct pmap), M_VMPMAP, M_WAITOK);
1890 pmap_pinit(pmap);
1891
1892 return pmap;
1893 }
1894
1895 /* pmap_pinit INTERNAL
1896 **
1897 * Initialize a pmap structure.
1898 */
1899 void
1900 pmap_pinit(pmap)
1901 pmap_t pmap;
1902 {
1903 bzero(pmap, sizeof(struct pmap));
1904 pmap->pm_a_tbl = proc0Atmgr;
1905 }
1906
1907 /* pmap_release INTERFACE
1908 **
1909 * Release any resources held by the given pmap.
1910 *
1911 * This is the reverse analog to pmap_pinit. It does not
1912 * necessarily mean for the pmap structure to be deallocated,
1913 * as in pmap_destroy.
1914 */
1915 void
1916 pmap_release(pmap)
1917 pmap_t pmap;
1918 {
1919 /* As long as the pmap contains no mappings,
1920 * which always should be the case whenever
1921 * this function is called, there really should
1922 * be nothing to do.
1923 */
1924 #ifdef PMAP_DEBUG
1925 if (pmap == NULL)
1926 return;
1927 if (pmap == pmap_kernel())
1928 panic("pmap_release: kernel pmap release requested.");
1929 if (pmap->pm_a_tbl != proc0Atmgr)
1930 panic("pmap_release: pmap not empty.");
1931 #endif
1932 }
1933
1934 /* pmap_reference INTERFACE
1935 **
1936 * Increment the reference count of a pmap.
1937 */
1938 void
1939 pmap_reference(pmap)
1940 pmap_t pmap;
1941 {
1942 if (pmap == NULL)
1943 return;
1944
1945 /* pmap_lock(pmap); */
1946 pmap->pm_refcount++;
1947 /* pmap_unlock(pmap); */
1948 }
1949
1950 /* pmap_dereference INTERNAL
1951 **
1952 * Decrease the reference count on the given pmap
1953 * by one and return the current count.
1954 */
1955 int
1956 pmap_dereference(pmap)
1957 pmap_t pmap;
1958 {
1959 int rtn;
1960
1961 if (pmap == NULL)
1962 return 0;
1963
1964 /* pmap_lock(pmap); */
1965 rtn = --pmap->pm_refcount;
1966 /* pmap_unlock(pmap); */
1967
1968 return rtn;
1969 }
1970
1971 /* pmap_destroy INTERFACE
1972 **
1973 * Decrement a pmap's reference count and delete
1974 * the pmap if it becomes zero. Will be called
1975 * only after all mappings have been removed.
1976 */
1977 void
1978 pmap_destroy(pmap)
1979 pmap_t pmap;
1980 {
1981 if (pmap == NULL)
1982 return;
1983 if (pmap == &kernel_pmap)
1984 panic("pmap_destroy: kernel_pmap!");
1985 if (pmap_dereference(pmap) == 0) {
1986 pmap_release(pmap);
1987 free(pmap, M_VMPMAP);
1988 }
1989 }
1990
1991 /* pmap_is_referenced INTERFACE
1992 **
1993 * Determine if the given physical page has been
1994 * referenced (read from [or written to.])
1995 */
1996 boolean_t
1997 pmap_is_referenced(pa)
1998 vm_offset_t pa;
1999 {
2000 pv_t *pv;
2001 pv_elem_t *pve;
2002 struct mmu_short_pte_struct *pte;
2003
2004 if (!pv_initialized)
2005 return FALSE;
2006 if (!is_managed(pa))
2007 return FALSE;
2008
2009 pv = pa2pv(pa);
2010 /* Check the flags on the pv head. If they are set,
2011 * return immediately. Otherwise a search must be done.
2012 */
2013 if (pv->pv_flags & PV_FLAGS_USED)
2014 return TRUE;
2015 else
2016 /* Search through all pv elements pointing
2017 * to this page and query their reference bits
2018 */
2019 for (pve = pv->pv_head.lh_first;
2020 pve != NULL;
2021 pve = pve->pve_link.le_next) {
2022 pte = pve2pte(pve);
2023 if (MMU_PTE_USED(*pte))
2024 return TRUE;
2025 }
2026
2027 return FALSE;
2028 }
2029
2030 /* pmap_is_modified INTERFACE
2031 **
2032 * Determine if the given physical page has been
2033 * modified (written to.)
2034 */
2035 boolean_t
2036 pmap_is_modified(pa)
2037 vm_offset_t pa;
2038 {
2039 pv_t *pv;
2040 pv_elem_t *pve;
2041
2042 if (!pv_initialized)
2043 return FALSE;
2044 if (!is_managed(pa))
2045 return FALSE;
2046
2047 /* see comments in pmap_is_referenced() */
2048 pv = pa2pv(pa);
2049 if (pv->pv_flags & PV_FLAGS_MDFY)
2050 return TRUE;
2051 else
2052 for (pve = pv->pv_head.lh_first; pve != NULL;
2053 pve = pve->pve_link.le_next) {
2054 struct mmu_short_pte_struct *pte;
2055 pte = pve2pte(pve);
2056 if (MMU_PTE_MODIFIED(*pte))
2057 return TRUE;
2058 }
2059 return FALSE;
2060 }
2061
2062 /* pmap_page_protect INTERFACE
2063 **
2064 * Applies the given protection to all mappings to the given
2065 * physical page.
2066 */
2067 void
2068 pmap_page_protect(pa, prot)
2069 vm_offset_t pa;
2070 vm_prot_t prot;
2071 {
2072 pv_t *pv;
2073 pv_elem_t *pve;
2074 struct mmu_short_pte_struct *pte;
2075
2076 if (!is_managed(pa))
2077 return;
2078
2079 pv = pa2pv(pa);
2080 for (pve = pv->pv_head.lh_first; pve != NULL;
2081 pve = pve->pve_link.le_next) {
2082 pte = pve2pte(pve);
2083 switch (prot) {
2084 case VM_PROT_ALL:
2085 /* do nothing */
2086 break;
2087 case VM_PROT_READ:
2088 case VM_PROT_READ|VM_PROT_EXECUTE:
2089 pte->attr.raw |= MMU_SHORT_PTE_WP;
2090 break;
2091 case VM_PROT_NONE:
2092 pmap_dereference_pte(pte);
2093 break;
2094 default:
2095 break;
2096 }
2097 }
2098 }
2099
2100 /* pmap_who_owns_pte INTERNAL
2101 **
2102 * Called internally to find which pmap the given pte is
2103 * a member of.
2104 */
2105 pmap_t
2106 pmap_who_owns_pte(pte)
2107 mmu_short_pte_t *pte;
2108 {
2109 c_tmgr_t *c_tbl;
2110
2111 c_tbl = pmap_find_c_tmgr(pte);
2112
2113 return c_tbl->ct_parent->bt_parent->at_parent;
2114 }
2115
2116 /* pmap_find_va INTERNAL_X
2117 **
2118 * Called internally to find the virtual address that the
2119 * given pte maps.
2120 *
2121 * Note: I don't know if this function will ever be used, but I've
2122 * implemented it just in case.
2123 */
2124 vm_offset_t
2125 pmap_find_va(pte)
2126 mmu_short_pte_t *pte;
2127 {
2128 a_tmgr_t *a_tbl;
2129 b_tmgr_t *b_tbl;
2130 c_tmgr_t *c_tbl;
2131 vm_offset_t va = 0;
2132
2133 /* Find the virtual address by decoding table indexes.
2134 * Each successive decode will reveal the address from
2135 * least to most significant bit fashion.
2136 *
2137 * 31 0
2138 * +-------------------------------+
2139 * |AAAAAAABBBBBBCCCCCCxxxxxxxxxxxx|
2140 * +-------------------------------+
2141 *
2142 * Start with the 'C' bits.
2143 */
2144 va |= (pmap_find_tic(pte) << MMU_TIC_SHIFT);
2145 c_tbl = pmap_find_c_tmgr(pte);
2146 b_tbl = c_tbl->ct_parent;
2147
2148 /* Add the 'B' bits. */
2149 va |= (c_tbl->ct_pidx << MMU_TIB_SHIFT);
2150 a_tbl = b_tbl->bt_parent;
2151
2152 /* Add the 'A' bits. */
2153 va |= (b_tbl->bt_pidx << MMU_TIA_SHIFT);
2154
2155 return va;
2156 }
2157
2158 /**** These functions should be removed. Structures have changed, making ****
2159 **** them uneccessary. ****/
2160
2161 /* pmap_find_tic INTERNAL
2162 **
2163 * Given the address of a pte, find the TIC (level 'C' table index) for
2164 * the pte within its C table.
2165 */
2166 char
2167 pmap_find_tic(pte)
2168 mmu_short_pte_t *pte;
2169 {
2170 return ((mmuCbase - pte) % MMU_C_TBL_SIZE);
2171 }
2172
2173 /* pmap_find_tib INTERNAL
2174 **
2175 * Given the address of dte known to belong to a B table, find the TIB
2176 * (level 'B' table index) for the dte within its table.
2177 */
2178 char
2179 pmap_find_tib(dte)
2180 mmu_short_dte_t *dte;
2181 {
2182 return ((mmuBbase - dte) % MMU_B_TBL_SIZE);
2183 }
2184
2185 /* pmap_find_tia INTERNAL
2186 **
2187 * Given the address of a dte known to belong to an A table, find the
2188 * TIA (level 'C' table index) for the dte withing its table.
2189 */
2190 char
2191 pmap_find_tia(dte)
2192 mmu_long_dte_t *dte;
2193 {
2194 return ((mmuAbase - dte) % MMU_A_TBL_SIZE);
2195 }
2196
2197 /**** This one should stay ****/
2198
2199 /* pmap_find_c_tmgr INTERNAL
2200 **
2201 * Given a pte known to belong to a C table, return the address of that
2202 * table's management structure.
2203 */
2204 c_tmgr_t *
2205 pmap_find_c_tmgr(pte)
2206 mmu_short_pte_t *pte;
2207 {
2208 return &Ctmgrbase[
2209 ((mmuCbase - pte) / sizeof(*pte) / MMU_C_TBL_SIZE)
2210 ];
2211 }
2212
2213 /* pmap_find_b_tmgr INTERNAL
2214 **
2215 * Given a dte known to belong to a B table, return the address of that
2216 * table's management structure.
2217 */
2218 b_tmgr_t *
2219 pmap_find_b_tmgr(dte)
2220 mmu_short_dte_t *dte;
2221 {
2222 return &Btmgrbase[
2223 ((mmuBbase - dte) / sizeof(*dte) / MMU_B_TBL_SIZE)
2224 ];
2225 }
2226
2227 /* pmap_find_a_tmgr INTERNAL
2228 **
2229 * Given a dte known to belong to an A table, return the address of that
2230 * table's management structure.
2231 */
2232 a_tmgr_t *
2233 pmap_find_a_tmgr(dte)
2234 mmu_long_dte_t *dte;
2235 {
2236 return &Atmgrbase[
2237 ((mmuAbase - dte) / sizeof(*dte) / MMU_A_TBL_SIZE)
2238 ];
2239 }
2240
2241 /**** End of functions that should be removed. ****
2242 **** ****/
2243
2244 /* pmap_clear_modify INTERFACE
2245 **
2246 * Clear the modification bit on the page at the specified
2247 * physical address.
2248 *
2249 */
2250 void
2251 pmap_clear_modify(pa)
2252 vm_offset_t pa;
2253 {
2254 pmap_clear_pv(pa, PV_FLAGS_MDFY);
2255 }
2256
2257 /* pmap_clear_reference INTERFACE
2258 **
2259 * Clear the referenced bit on the page at the specified
2260 * physical address.
2261 */
2262 void
2263 pmap_clear_reference(pa)
2264 vm_offset_t pa;
2265 {
2266 pmap_clear_pv(pa, PV_FLAGS_USED);
2267 }
2268
2269 /* pmap_clear_pv INTERNAL
2270 **
2271 * Clears the specified flag from the specified physical address.
2272 * (Used by pmap_clear_modify() and pmap_clear_reference().)
2273 *
2274 * Flag is one of:
2275 * PV_FLAGS_MDFY - Page modified bit.
2276 * PV_FLAGS_USED - Page used (referenced) bit.
2277 *
2278 * This routine must not only clear the flag on the pv list
2279 * head. It must also clear the bit on every pte in the pv
2280 * list associated with the address.
2281 */
2282 void
2283 pmap_clear_pv(pa, flag)
2284 vm_offset_t pa;
2285 int flag;
2286 {
2287 pv_t *pv;
2288 pv_elem_t *pve;
2289 mmu_short_pte_t *pte;
2290
2291 pv = pa2pv(pa);
2292 pv->pv_flags &= ~(flag);
2293 for (pve = pv->pv_head.lh_first; pve != NULL;
2294 pve = pve->pve_link.le_next) {
2295 pte = pve2pte(pve);
2296 pte->attr.raw &= ~(flag);
2297 }
2298 }
2299
2300 /* pmap_extract INTERFACE
2301 **
2302 * Return the physical address mapped by the virtual address
2303 * in the specified pmap or 0 if it is not known.
2304 *
2305 * Note: this function should also apply an exclusive lock
2306 * on the pmap system during its duration.
2307 */
2308 vm_offset_t
2309 pmap_extract(pmap, va)
2310 pmap_t pmap;
2311 vm_offset_t va;
2312 {
2313 int a_idx, b_idx, pte_idx;
2314 a_tmgr_t *a_tbl;
2315 b_tmgr_t *b_tbl;
2316 c_tmgr_t *c_tbl;
2317 mmu_short_pte_t *c_pte;
2318
2319 if (pmap == pmap_kernel())
2320 return pmap_extract_kernel(va);
2321 if (pmap == NULL)
2322 return 0;
2323
2324 if (pmap_stroll(pmap, va, &a_tbl, &b_tbl, &c_tbl,
2325 &c_pte, &a_idx, &b_idx, &pte_idx) == FALSE);
2326 return 0;
2327
2328 if (MMU_VALID_DT(*c_pte))
2329 return MMU_PTE_PA(*c_pte);
2330 else
2331 return 0;
2332 }
2333
2334 /* pmap_extract_kernel INTERNAL
2335 **
2336 * Extract a traslation from the kernel address space.
2337 */
2338 vm_offset_t
2339 pmap_extract_kernel(va)
2340 vm_offset_t va;
2341 {
2342 mmu_short_pte_t *pte;
2343
2344 pte = &kernCbase[(unsigned long) sun3x_btop(va - KERNBASE)];
2345 return MMU_PTE_PA(*pte);
2346 }
2347
2348 /* pmap_remove_kernel INTERNAL
2349 **
2350 * Remove the mapping of a range of virtual addresses from the kernel map.
2351 */
2352 void
2353 pmap_remove_kernel(start, end)
2354 vm_offset_t start;
2355 vm_offset_t end;
2356 {
2357 start -= KERNBASE;
2358 end -= KERNBASE;
2359 start = sun3x_round_page(start); /* round down */
2360 start = sun3x_btop(start);
2361 end += MMU_PAGE_SIZE - 1; /* next round operation will be up */
2362 end = sun3x_round_page(end); /* round */
2363 end = sun3x_btop(end);
2364
2365 while (start < end)
2366 kernCbase[start++].attr.raw = MMU_DT_INVALID;
2367 }
2368
2369 /* pmap_remove INTERFACE
2370 **
2371 * Remove the mapping of a range of virtual addresses from the given pmap.
2372 */
2373 void
2374 pmap_remove(pmap, start, end)
2375 pmap_t pmap;
2376 vm_offset_t start;
2377 vm_offset_t end;
2378 {
2379 if (pmap == pmap_kernel()) {
2380 pmap_remove_kernel(start, end);
2381 return;
2382 }
2383 pmap_remove_a(pmap->pm_a_tbl, start, end);
2384
2385 /* If we just modified the current address space,
2386 * make sure to flush the MMU cache.
2387 */
2388 if (curatbl == pmap->pm_a_tbl) {
2389 /* mmu_flusha(); */
2390 TBIA();
2391 }
2392 }
2393
2394 /* pmap_remove_a INTERNAL
2395 **
2396 * This is function number one in a set of three that removes a range
2397 * of memory in the most efficient manner by removing the highest possible
2398 * tables from the memory space. This particular function attempts to remove
2399 * as many B tables as it can, delegating the remaining fragmented ranges to
2400 * pmap_remove_b().
2401 *
2402 * It's ugly but will do for now.
2403 */
2404 void
2405 pmap_remove_a(a_tbl, start, end)
2406 a_tmgr_t *a_tbl;
2407 vm_offset_t start;
2408 vm_offset_t end;
2409 {
2410 int idx;
2411 vm_offset_t nstart, nend, rstart;
2412 b_tmgr_t *b_tbl;
2413 mmu_long_dte_t *a_dte;
2414 mmu_short_dte_t *b_dte;
2415
2416
2417 if (a_tbl == proc0Atmgr) /* If the pmap has no A table, return */
2418 return;
2419
2420 nstart = MMU_ROUND_UP_A(start);
2421 nend = MMU_ROUND_A(end);
2422
2423 if (start < nstart) {
2424 idx = MMU_TIA(start);
2425 a_dte = &a_tbl->at_dtbl[idx];
2426 if (MMU_VALID_DT(*a_dte)) {
2427 b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
2428 b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
2429 b_tbl = mmuB2tmgr(b_dte);
2430 if (end < nstart) {
2431 pmap_remove_b(b_tbl, start, end);
2432 return;
2433 } else {
2434 pmap_remove_b(b_tbl, start, nstart);
2435 }
2436 } else if (end < nstart) {
2437 return;
2438 }
2439 }
2440 if (nstart < nend) {
2441 idx = MMU_TIA(nstart);
2442 a_dte = &a_tbl->at_dtbl[idx];
2443 rstart = nstart;
2444 while (rstart < nend) {
2445 if (MMU_VALID_DT(*a_dte)) {
2446 b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
2447 b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
2448 b_tbl = mmuB2tmgr(b_dte);
2449 a_dte->attr.raw = MMU_DT_INVALID;
2450 a_tbl->at_ecnt--;
2451 free_b_table(b_tbl);
2452 TAILQ_REMOVE(&b_pool, b_tbl, bt_link);
2453 TAILQ_INSERT_HEAD(&b_pool, b_tbl, bt_link);
2454 }
2455 a_dte++;
2456 rstart += MMU_TIA_RANGE;
2457 }
2458 }
2459 if (nend < end) {
2460 idx = MMU_TIA(nend);
2461 a_dte = &a_tbl->at_dtbl[idx];
2462 if (MMU_VALID_DT(*a_dte)) {
2463 b_dte = (mmu_short_dte_t *) MMU_DTE_PA(*a_dte);
2464 b_dte = (mmu_short_dte_t *) mmu_ptov(b_dte);
2465 b_tbl = mmuB2tmgr(b_dte);
2466 pmap_remove_b(b_tbl, nend, end);
2467 }
2468 }
2469 }
2470
2471 /* pmap_remove_b INTERNAL
2472 **
2473 * Remove a range of addresses from an address space, trying to remove entire
2474 * C tables if possible.
2475 */
2476 void
2477 pmap_remove_b(b_tbl, start, end)
2478 b_tmgr_t *b_tbl;
2479 vm_offset_t start;
2480 vm_offset_t end;
2481 {
2482 int idx;
2483 vm_offset_t nstart, nend, rstart;
2484 c_tmgr_t *c_tbl;
2485 mmu_short_dte_t *b_dte;
2486 mmu_short_pte_t *c_dte;
2487
2488
2489 nstart = MMU_ROUND_UP_B(start);
2490 nend = MMU_ROUND_B(end);
2491
2492 if (start < nstart) {
2493 idx = MMU_TIB(start);
2494 b_dte = &b_tbl->bt_dtbl[idx];
2495 if (MMU_VALID_DT(*b_dte)) {
2496 c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
2497 c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
2498 c_tbl = mmuC2tmgr(c_dte);
2499 if (end < nstart) {
2500 pmap_remove_c(c_tbl, start, end);
2501 return;
2502 } else {
2503 pmap_remove_c(c_tbl, start, nstart);
2504 }
2505 } else if (end < nstart) {
2506 return;
2507 }
2508 }
2509 if (nstart < nend) {
2510 idx = MMU_TIB(nstart);
2511 b_dte = &b_tbl->bt_dtbl[idx];
2512 rstart = nstart;
2513 while (rstart < nend) {
2514 if (MMU_VALID_DT(*b_dte)) {
2515 c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
2516 c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
2517 c_tbl = mmuC2tmgr(c_dte);
2518 b_dte->attr.raw = MMU_DT_INVALID;
2519 b_tbl->bt_ecnt--;
2520 free_c_table(c_tbl);
2521 TAILQ_REMOVE(&c_pool, c_tbl, ct_link);
2522 TAILQ_INSERT_HEAD(&c_pool, c_tbl, ct_link);
2523 }
2524 b_dte++;
2525 rstart += MMU_TIB_RANGE;
2526 }
2527 }
2528 if (nend < end) {
2529 idx = MMU_TIB(nend);
2530 b_dte = &b_tbl->bt_dtbl[idx];
2531 if (MMU_VALID_DT(*b_dte)) {
2532 c_dte = (mmu_short_pte_t *) MMU_DTE_PA(*b_dte);
2533 c_dte = (mmu_short_pte_t *) mmu_ptov(c_dte);
2534 c_tbl = mmuC2tmgr(c_dte);
2535 pmap_remove_c(c_tbl, nend, end);
2536 }
2537 }
2538 }
2539
2540 /* pmap_remove_c INTERNAL
2541 **
2542 * Remove a range of addresses from the given C table.
2543 */
2544 void
2545 pmap_remove_c(c_tbl, start, end)
2546 c_tmgr_t *c_tbl;
2547 vm_offset_t start;
2548 vm_offset_t end;
2549 {
2550 int idx;
2551 mmu_short_pte_t *c_pte;
2552
2553 idx = MMU_TIC(start);
2554 c_pte = &c_tbl->ct_dtbl[idx];
2555 while (start < end) {
2556 if (MMU_VALID_DT(*c_pte))
2557 pmap_remove_pte(c_pte);
2558 c_tbl->ct_ecnt--;
2559 start += MMU_PAGE_SIZE;
2560 c_pte++;
2561 }
2562 }
2563
2564 /* is_managed INTERNAL
2565 **
2566 * Determine if the given physical address is managed by the PV system.
2567 * Note that this logic assumes that no one will ask for the status of
2568 * addresses which lie in-between the memory banks on the 3/80. If they
2569 * do so, it will falsely report that it is managed.
2570 */
2571 boolean_t
2572 is_managed(pa)
2573 vm_offset_t pa;
2574 {
2575 if (pa >= avail_start && pa < avail_end)
2576 return TRUE;
2577 else
2578 return FALSE;
2579 }
2580
2581 /* pa2pv INTERNAL
2582 **
2583 * Return the pv_list_head element which manages the given physical
2584 * address.
2585 */
2586 pv_t *
2587 pa2pv(pa)
2588 vm_offset_t pa;
2589 {
2590 struct pmap_physmem_struct *bank = &avail_mem[0];
2591
2592 while (pa >= bank->pmem_end)
2593 bank = bank->pmem_next;
2594
2595 pa -= bank->pmem_start;
2596 return &pvbase[bank->pmem_pvbase + sun3x_btop(pa)];
2597 }
2598
2599 /* pmap_bootstrap_alloc INTERNAL
2600 **
2601 * Used internally for memory allocation at startup when malloc is not
2602 * available. This code will fail once it crosses the first memory
2603 * bank boundary on the 3/80. Hopefully by then however, the VM system
2604 * will be in charge of allocation.
2605 */
2606 void *
2607 pmap_bootstrap_alloc(size)
2608 int size;
2609 {
2610 void *rtn;
2611
2612 rtn = (void *) virtual_avail;
2613
2614 /* While the size is greater than a page, map single pages,
2615 * decreasing size until it is less than a page.
2616 */
2617 while (size > NBPG) {
2618 (void) pmap_bootstrap_alloc(NBPG);
2619
2620 /* If the above code is ok, let's keep it.
2621 * It looks cooler than:
2622 * virtual_avail += NBPG;
2623 * avail_start += NBPG;
2624 * last_mapped = sun3x_trunc_page(avail_start);
2625 * pmap_enter_kernel(last_mapped, last_mapped + KERNBASE,
2626 * VM_PROT_READ|VM_PROT_WRITE);
2627 */
2628
2629 size -= NBPG;
2630 }
2631 avail_start += size;
2632 virtual_avail += size;
2633
2634 /* did the allocation cross a page boundary? */
2635 if (last_mapped != sun3x_trunc_page(avail_start)) {
2636 last_mapped = sun3x_trunc_page(avail_start);
2637 pmap_enter_kernel(last_mapped + KERNBASE, last_mapped,
2638 VM_PROT_READ|VM_PROT_WRITE);
2639 }
2640
2641 return rtn;
2642 }
2643
2644 /* pmap_bootstap_aalign INTERNAL
2645 **
2646 * Used to insure that the next call to pmap_bootstrap_alloc() will return
2647 * a chunk of memory aligned to the specified size.
2648 */
2649 void
2650 pmap_bootstrap_aalign(size)
2651 int size;
2652 {
2653 if (((unsigned int) avail_start % size) != 0) {
2654 (void) pmap_bootstrap_alloc(size -
2655 ((unsigned int) (avail_start % size)));
2656 }
2657 }
2658
2659 #if 0
2660 /* pmap_activate INTERFACE
2661 **
2662 * Make the virtual to physical mappings contained in the given
2663 * pmap the current map used by the system.
2664 */
2665 void
2666 pmap_activate(pmap, pcbp)
2667 pmap_t pmap;
2668 struct pcb *pcbp;
2669 {
2670 vm_offset_t pa;
2671 /* Save the A table being loaded in 'curatbl'.
2672 * pmap_remove() uses this variable to determine if a given A
2673 * table is currently being used as the system map. If so, it
2674 * will issue an MMU cache flush whenever mappings are removed.
2675 */
2676 curatbl = pmap->pm_a_tbl;
2677 /* call the locore routine to set the user root pointer table */
2678 pa = mmu_vtop(pmap->pm_a_tbl->at_dtbl);
2679 mmu_seturp(pa);
2680 }
2681 #endif
2682
2683 /* pmap_pa_exists
2684 **
2685 * Used by the /dev/mem driver to see if a given PA is memory
2686 * that can be mapped. (The PA is not in a hole.)
2687 */
2688 int
2689 pmap_pa_exists(pa)
2690 vm_offset_t pa;
2691 {
2692 /* XXX - NOTYET */
2693 return (0);
2694 }
2695
2696
2697 /* pmap_update
2698 **
2699 * Apply any delayed changes scheduled for all pmaps immediately.
2700 *
2701 * No delayed operations are currently done in this pmap.
2702 */
2703 void
2704 pmap_update()
2705 {
2706 /* not implemented. */
2707 }
2708
2709 /* pmap_virtual_space INTERFACE
2710 **
2711 * Return the current available range of virtual addresses in the
2712 * arguuments provided. Only really called once.
2713 */
2714 void
2715 pmap_virtual_space(vstart, vend)
2716 vm_offset_t *vstart, *vend;
2717 {
2718 *vstart = virtual_avail;
2719 *vend = virtual_end;
2720 }
2721
2722 /* pmap_free_pages INTERFACE
2723 **
2724 * Return the number of physical pages still available.
2725 *
2726 * This is probably going to be a mess, but it's only called
2727 * once and it's the only function left that I have to implement!
2728 */
2729 u_int
2730 pmap_free_pages()
2731 {
2732 int i;
2733 u_int left;
2734 vm_offset_t avail;
2735
2736 avail = sun3x_round_up_page(avail_start);
2737
2738 left = 0;
2739 i = 0;
2740 while (avail >= avail_mem[i].pmem_end) {
2741 if (avail_mem[i].pmem_next == NULL)
2742 return 0;
2743 i++;
2744 }
2745 while (i < SUN3X_80_MEM_BANKS) {
2746 if (avail < avail_mem[i].pmem_start) {
2747 /* Avail is inside a hole, march it
2748 * up to the next bank.
2749 */
2750 avail = avail_mem[i].pmem_start;
2751 }
2752 left += sun3x_btop(avail_mem[i].pmem_end - avail);
2753 if (avail_mem[i].pmem_next == NULL)
2754 break;
2755 i++;
2756 }
2757
2758 return left;
2759 }
2760
2761 /* pmap_page_index INTERFACE
2762 **
2763 * Return the index of the given physical page in a list of useable
2764 * physical pages in the system. Holes in physical memory may be counted
2765 * if so desired. As long as pmap_free_pages() and pmap_page_index()
2766 * agree as to whether holes in memory do or do not count as valid pages,
2767 * it really doesn't matter. However, if you like to save a little
2768 * memory, don't count holes as valid pages. This is even more true when
2769 * the holes are large.
2770 *
2771 * We will not count holes as valid pages. We can generate page indexes
2772 * that conform to this by using the memory bank structures initialized
2773 * in pmap_alloc_pv().
2774 */
2775 int
2776 pmap_page_index(pa)
2777 vm_offset_t pa;
2778 {
2779 struct pmap_physmem_struct *bank = avail_mem;
2780
2781 while (pa > bank->pmem_end)
2782 bank = bank->pmem_next;
2783 pa -= bank->pmem_start;
2784
2785 return (bank->pmem_pvbase + sun3x_btop(pa));
2786 }
2787
2788 /* pmap_next_page INTERFACE
2789 **
2790 * Place the physical address of the next available page in the
2791 * argument given. Returns FALSE if there are no more pages left.
2792 *
2793 * This function must jump over any holes in physical memory.
2794 * Once this function is used, any use of pmap_bootstrap_alloc()
2795 * is a sin. Sinners will be punished with erratic behavior.
2796 */
2797 boolean_t
2798 pmap_next_page(pa)
2799 vm_offset_t *pa;
2800 {
2801 static boolean_t initialized = FALSE;
2802 static struct pmap_physmem_struct *curbank = avail_mem;
2803
2804 if (!initialized) {
2805 pmap_bootstrap_aalign(NBPG);
2806 initialized = TRUE;
2807 }
2808
2809 if (avail_start >= curbank->pmem_end)
2810 if (curbank->pmem_next == NULL)
2811 return FALSE;
2812 else {
2813 curbank = curbank->pmem_next;
2814 avail_start = curbank->pmem_start;
2815 }
2816
2817 *pa = avail_start;
2818 avail_start += NBPG;
2819 return TRUE;
2820 }
2821
2822 /************************ SUN3 COMPATIBILITY ROUTINES ********************
2823 * The following routines are only used by DDB for tricky kernel text *
2824 * text operations in db_memrw.c. They are provided for sun3 *
2825 * compatibility. *
2826 *************************************************************************/
2827 /* get_pte INTERNAL
2828 **
2829 * Return the page descriptor the describes the kernel mapping
2830 * of the given virtual address.
2831 *
2832 * XXX - It might be nice if this worked outside of the MMU
2833 * structures we manage. (Could do it with ptest). -gwr
2834 */
2835 vm_offset_t
2836 get_pte(va)
2837 vm_offset_t va;
2838 {
2839 u_long idx;
2840
2841 idx = (unsigned long) sun3x_btop(mmu_vtop(va));
2842 return (kernCbase[idx].attr.raw);
2843 }
2844
2845 /* set_pte INTERNAL
2846 **
2847 * Set the page descriptor that describes the kernel mapping
2848 * of the given virtual address.
2849 */
2850 void
2851 set_pte(va, pte)
2852 vm_offset_t va;
2853 vm_offset_t pte;
2854 {
2855 u_long idx;
2856
2857 idx = (unsigned long) sun3x_btop(mmu_vtop(va));
2858 kernCbase[idx].attr.raw = pte;
2859 }
2860
2861 #ifdef NOT_YET
2862 /* and maybe not ever */
2863 /************************** LOW-LEVEL ROUTINES **************************
2864 * These routines will eventualy be re-written into assembly and placed *
2865 * in locore.s. They are here now as stubs so that the pmap module can *
2866 * be linked as a standalone user program for testing. *
2867 ************************************************************************/
2868 /* flush_atc_crp INTERNAL
2869 **
2870 * Flush all page descriptors derived from the given CPU Root Pointer
2871 * (CRP), or 'A' table as it is known here, from the 68851's automatic
2872 * cache.
2873 */
2874 void
2875 flush_atc_crp(a_tbl)
2876 {
2877 mmu_long_rp_t rp;
2878
2879 /* Create a temporary root table pointer that points to the
2880 * given A table.
2881 */
2882 rp.attr.raw = ~MMU_LONG_RP_LU;
2883 rp.addr.raw = (unsigned int) a_tbl;
2884
2885 mmu_pflushr(&rp);
2886 /* mmu_pflushr:
2887 * movel sp(4)@,a0
2888 * pflushr a0@
2889 * rts
2890 */
2891 }
2892 #endif /* NOT_YET */
2893