]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/pmap.c
xnu-344.12.2.tar.gz
[apple/xnu.git] / osfmk / ppc / pmap.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1990,1991,1992 The University of Utah and
28 * the Center for Software Science (CSS).
29 * Copyright (c) 1991,1987 Carnegie Mellon University.
30 * All rights reserved.
31 *
32 * Permission to use, copy, modify and distribute this software and its
33 * documentation is hereby granted, provided that both the copyright
34 * notice and this permission notice appear in all copies of the
35 * software, derivative works or modified versions, and any portions
36 * thereof, and that both notices appear in supporting documentation,
37 * and that all advertising materials mentioning features or use of
38 * this software display the following acknowledgement: ``This product
39 * includes software developed by the Center for Software Science at
40 * the University of Utah.''
41 *
42 * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSS ALLOW FREE USE OF
43 * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
44 * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
45 * THIS SOFTWARE.
46 *
47 * CSS requests users of this software to return to css-dist@cs.utah.edu any
48 * improvements that they make and grant CSS redistribution rights.
49 *
50 * Carnegie Mellon requests users of this software to return to
51 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 *
58 * Utah $Hdr: pmap.c 1.28 92/06/23$
59 * Author: Mike Hibler, Bob Wheeler, University of Utah CSS, 10/90
60 */
61
62 /*
63 * Manages physical address maps for powerpc.
64 *
65 * In addition to hardware address maps, this
66 * module is called upon to provide software-use-only
67 * maps which may or may not be stored in the same
68 * form as hardware maps. These pseudo-maps are
69 * used to store intermediate results from copy
70 * operations to and from address spaces.
71 *
72 * Since the information managed by this module is
73 * also stored by the logical address mapping module,
74 * this module may throw away valid virtual-to-physical
75 * mappings at almost any time. However, invalidations
76 * of virtual-to-physical mappings must be done as
77 * requested.
78 *
79 * In order to cope with hardware architectures which
80 * make virtual-to-physical map invalidates expensive,
81 * this module may delay invalidate or reduced protection
82 * operations until such time as they are actually
83 * necessary. This module is given full information to
84 * when physical maps must be made correct.
85 *
86 */
87
88 #include <zone_debug.h>
89 #include <cpus.h>
90 #include <debug.h>
91 #include <mach_kgdb.h>
92 #include <mach_vm_debug.h>
93 #include <db_machine_commands.h>
94
95 #include <kern/thread.h>
96 #include <kern/simple_lock.h>
97 #include <mach/vm_attributes.h>
98 #include <mach/vm_param.h>
99 #include <kern/spl.h>
100
101 #include <kern/misc_protos.h>
102 #include <ppc/misc_protos.h>
103 #include <ppc/proc_reg.h>
104
105 #include <vm/pmap.h>
106 #include <vm/vm_map.h>
107 #include <vm/vm_page.h>
108
109 #include <ppc/pmap.h>
110 #include <ppc/pmap_internals.h>
111 #include <ppc/mem.h>
112 #include <ppc/mappings.h>
113
114 #include <ppc/new_screen.h>
115 #include <ppc/Firmware.h>
116 #include <ppc/savearea.h>
117 #include <ppc/exception.h>
118 #include <ddb/db_output.h>
119
120 #if DB_MACHINE_COMMANDS
121 /* optionally enable traces of pmap operations in post-mortem trace table */
122 /* #define PMAP_LOWTRACE 1 */
123 #define PMAP_LOWTRACE 0
124 #else /* DB_MACHINE_COMMANDS */
125 /* Can not trace even if we wanted to */
126 #define PMAP_LOWTRACE 0
127 #endif /* DB_MACHINE_COMMANDS */
128
129 #define PERFTIMES 0
130
131 #if PERFTIMES && DEBUG
132 #define debugLog2(a, b, c) dbgLog2(a, b, c)
133 #else
134 #define debugLog2(a, b, c)
135 #endif
136
137 extern unsigned int avail_remaining;
138 extern unsigned int mappingdeb0;
139 extern struct Saveanchor saveanchor; /* Aliged savearea anchor */
140 extern int real_ncpus; /* Number of actual CPUs */
141 unsigned int debugbackpocket; /* (TEST/DEBUG) */
142
143 vm_offset_t avail_next;
144 vm_offset_t first_free_virt;
145 int current_free_region; /* Used in pmap_next_page */
146
147 /* forward */
148 void pmap_activate(pmap_t pmap, thread_t th, int which_cpu);
149 void pmap_deactivate(pmap_t pmap, thread_t th, int which_cpu);
150 void copy_to_phys(vm_offset_t sva, vm_offset_t dpa, int bytecount);
151
152 #if MACH_VM_DEBUG
153 int pmap_list_resident_pages(pmap_t pmap, vm_offset_t *listp, int space);
154 #endif
155
156 #if DEBUG
157 #define PDB_USER 0x01 /* exported functions */
158 #define PDB_MAPPING 0x02 /* low-level mapping routines */
159 #define PDB_ENTER 0x04 /* pmap_enter specifics */
160 #define PDB_COPY 0x08 /* copy page debugging */
161 #define PDB_ZERO 0x10 /* zero page debugging */
162 #define PDB_WIRED 0x20 /* things concerning wired entries */
163 #define PDB_PTEG 0x40 /* PTEG overflows */
164 #define PDB_LOCK 0x100 /* locks */
165 #define PDB_IO 0x200 /* Improper use of WIMG_IO checks - PCI machines */
166
167 int pmdebug=0;
168 #endif
169
170 /* NOTE: kernel_pmap_store must be in V=R storage and aligned!!!!!!!!!!!!!! */
171
172 extern struct pmap kernel_pmap_store;
173 pmap_t kernel_pmap; /* Pointer to kernel pmap and anchor for in-use pmaps */
174 pmap_t cursor_pmap; /* Pointer to last pmap allocated or previous if removed from in-use list */
175 struct zone *pmap_zone; /* zone of pmap structures */
176 boolean_t pmap_initialized = FALSE;
177
178 /*
179 * Physical-to-virtual translations are handled by inverted page table
180 * structures, phys_tables. Multiple mappings of a single page are handled
181 * by linking the affected mapping structures. We initialise one region
182 * for phys_tables of the physical memory we know about, but more may be
183 * added as it is discovered (eg. by drivers).
184 */
185 struct phys_entry *phys_table; /* For debugging */
186
187 lock_t pmap_system_lock;
188
189 decl_simple_lock_data(,tlb_system_lock)
190
191 /*
192 * free pmap list. caches the first free_pmap_max pmaps that are freed up
193 */
194 int free_pmap_max = 32;
195 int free_pmap_count;
196 pmap_t free_pmap_list;
197 decl_simple_lock_data(,free_pmap_lock)
198
199 /*
200 * Function to get index into phys_table for a given physical address
201 */
202
203 struct phys_entry *pmap_find_physentry(vm_offset_t pa)
204 {
205 int i;
206 struct phys_entry *entry;
207
208 for (i = pmap_mem_regions_count-1; i >= 0; i--) {
209 if (pa < pmap_mem_regions[i].start)
210 continue;
211 if (pa >= pmap_mem_regions[i].end)
212 return PHYS_NULL;
213
214 entry = &pmap_mem_regions[i].phys_table[(pa - pmap_mem_regions[i].start) >> PPC_PGSHIFT];
215 __asm__ volatile("dcbt 0,%0" : : "r" (entry)); /* We will use this in a little bit */
216 return entry;
217 }
218 kprintf("DEBUG : pmap_find_physentry 0x%08x out of range\n",pa);
219 return PHYS_NULL;
220 }
221
222 /*
223 * kern_return_t
224 * pmap_add_physical_memory(vm_offset_t spa, vm_offset_t epa,
225 * boolean_t available, unsigned int attr)
226 * Allocate some extra physentries for the physical addresses given,
227 * specifying some default attribute that on the powerpc specifies
228 * the default cachability for any mappings using these addresses
229 * If the memory is marked as available, it is added to the general
230 * VM pool, otherwise it is not (it is reserved for card IO etc).
231 */
232 kern_return_t pmap_add_physical_memory(vm_offset_t spa, vm_offset_t epa,
233 boolean_t available, unsigned int attr)
234 {
235 int i,j;
236 spl_t s;
237
238 /* Only map whole pages */
239
240 panic("Forget it! You can't map no more memory, you greedy puke!\n");
241
242 spa = trunc_page(spa);
243 epa = round_page(epa);
244
245 /* First check that the region doesn't already exist */
246
247 assert (epa >= spa);
248 for (i = 0; i < pmap_mem_regions_count; i++) {
249 /* If we're below the next region, then no conflict */
250 if (epa < pmap_mem_regions[i].start)
251 break;
252 if (spa < pmap_mem_regions[i].end) {
253 #if DEBUG
254 kprintf("pmap_add_physical_memory(0x%08x,0x%08x,0x%08x) - memory already present\n",spa,epa,attr);
255 #endif /* DEBUG */
256 return KERN_NO_SPACE;
257 }
258 }
259
260 #if DEBUG
261 kprintf("pmap_add_physical_memory; region insert spot: %d out of %d\n", i, pmap_mem_regions_count); /* (TEST/DEBUG) */
262 #endif
263
264 /* Check that we've got enough space for another region */
265 if (pmap_mem_regions_count == PMAP_MEM_REGION_MAX)
266 return KERN_RESOURCE_SHORTAGE;
267
268 /* Once here, i points to the mem_region above ours in physical mem */
269
270 /* allocate a new phys_table for this new region */
271 #if DEBUG
272 kprintf("pmap_add_physical_memory; kalloc\n"); /* (TEST/DEBUG) */
273 #endif
274
275 phys_table = (struct phys_entry *)
276 kalloc(sizeof(struct phys_entry) * atop(epa-spa));
277 #if DEBUG
278 kprintf("pmap_add_physical_memory; new phys_table: %08X\n", phys_table); /* (TEST/DEBUG) */
279 #endif
280
281 /* Initialise the new phys_table entries */
282 for (j = 0; j < atop(epa-spa); j++) {
283
284 phys_table[j].phys_link = MAPPING_NULL;
285
286 mapping_phys_init(&phys_table[j], spa+(j*PAGE_SIZE), attr); /* Initialize the hardware specific portions */
287
288 }
289 s = splhigh();
290
291 /* Move all the phys_table entries up some to make room in
292 * the ordered list.
293 */
294 for (j = pmap_mem_regions_count; j > i ; j--)
295 pmap_mem_regions[j] = pmap_mem_regions[j-1];
296
297 /* Insert a new entry with some memory to back it */
298
299 pmap_mem_regions[i].start = spa;
300 pmap_mem_regions[i].end = epa;
301 pmap_mem_regions[i].phys_table = phys_table;
302
303 pmap_mem_regions_count++;
304 splx(s);
305
306 #if DEBUG
307 for(i=0; i<pmap_mem_regions_count; i++) { /* (TEST/DEBUG) */
308 kprintf("region %d: %08X %08X %08X\n", i, pmap_mem_regions[i].start,
309 pmap_mem_regions[i].end, pmap_mem_regions[i].phys_table); /* (TEST/DEBUG) */
310 }
311 #endif
312
313 if (available) {
314 kprintf("warning : pmap_add_physical_mem() "
315 "available not yet supported\n");
316 }
317
318 return KERN_SUCCESS;
319 }
320
321 /*
322 * pmap_map(va, spa, epa, prot)
323 * is called during boot to map memory in the kernel's address map.
324 * A virtual address range starting at "va" is mapped to the physical
325 * address range "spa" to "epa" with machine independent protection
326 * "prot".
327 *
328 * "va", "spa", and "epa" are byte addresses and must be on machine
329 * independent page boundaries.
330 *
331 * Pages with a contiguous virtual address range, the same protection, and attributes.
332 * therefore, we map it with a single block.
333 *
334 */
335 vm_offset_t
336 pmap_map(
337 vm_offset_t va,
338 vm_offset_t spa,
339 vm_offset_t epa,
340 vm_prot_t prot)
341 {
342
343
344 if (spa == epa)
345 return(va);
346
347 assert(epa > spa);
348 debugLog2(40, va, spa); /* Log pmap_map call */
349
350 pmap_map_block(kernel_pmap, va, spa, epa - spa, prot, PTE_WIMG_DEFAULT, blkPerm); /* Set up a permanent block mapped area */
351
352 debugLog2(41, epa, prot); /* Log pmap_map call */
353
354 return(va);
355 }
356
357 /*
358 * pmap_map_bd(va, spa, epa, prot)
359 * Back-door routine for mapping kernel VM at initialisation.
360 * Used for mapping memory outside the known physical memory
361 * space, with caching disabled. Designed for use by device probes.
362 *
363 * A virtual address range starting at "va" is mapped to the physical
364 * address range "spa" to "epa" with machine independent protection
365 * "prot".
366 *
367 * "va", "spa", and "epa" are byte addresses and must be on machine
368 * independent page boundaries.
369 *
370 * WARNING: The current version of memcpy() can use the dcbz instruction
371 * on the destination addresses. This will cause an alignment exception
372 * and consequent overhead if the destination is caching-disabled. So
373 * avoid memcpy()ing into the memory mapped by this function.
374 *
375 * also, many other pmap_ routines will misbehave if you try and change
376 * protections or remove these mappings, they are designed to be permanent.
377 *
378 * These areas will be added to the autogen list, if possible. Existing translations
379 * are overridden and their mapping stuctures are released. This takes place in
380 * the autogen_map function.
381 *
382 * Locking:
383 * this routine is called only during system initialization when only
384 * one processor is active, so no need to take locks...
385 */
386 vm_offset_t
387 pmap_map_bd(
388 vm_offset_t va,
389 vm_offset_t spa,
390 vm_offset_t epa,
391 vm_prot_t prot)
392 {
393 register struct mapping *mp;
394 register struct phys_entry *pp;
395
396
397 if (spa == epa)
398 return(va);
399
400 assert(epa > spa);
401
402 debugLog2(42, va, epa); /* Log pmap_map_bd call */
403
404 pmap_map_block(kernel_pmap, va, spa, epa - spa, prot, PTE_WIMG_IO, blkPerm); /* Set up autogen area */
405
406 debugLog2(43, epa, prot); /* Log pmap_map_bd exit */
407
408 return(va);
409 }
410
411 /*
412 * Bootstrap the system enough to run with virtual memory.
413 * Map the kernel's code and data, and allocate the system page table.
414 * Called with mapping done by BATs. Page_size must already be set.
415 *
416 * Parameters:
417 * mem_size: Total memory present
418 * first_avail: First virtual address available
419 * first_phys_avail: First physical address available
420 */
421 void
422 pmap_bootstrap(unsigned int mem_size, vm_offset_t *first_avail, vm_offset_t *first_phys_avail, unsigned int kmapsize)
423 {
424 register struct mapping *mp;
425 vm_offset_t addr;
426 vm_size_t size;
427 int i, num, j, rsize, mapsize, vmpagesz, vmmapsz;
428 unsigned int mask;
429 vm_offset_t first_used_addr;
430 PCA *pcaptr;
431
432 *first_avail = round_page(*first_avail);
433
434 #if DEBUG
435 kprintf("first_avail=%08X; first_phys_avail=%08X; avail_remaining=%d\n",
436 *first_avail, *first_phys_avail, avail_remaining);
437 #endif
438
439 assert(PAGE_SIZE == PPC_PGBYTES);
440
441 /*
442 * Initialize kernel pmap
443 */
444 kernel_pmap = &kernel_pmap_store;
445 cursor_pmap = &kernel_pmap_store;
446
447 lock_init(&pmap_system_lock,
448 FALSE, /* NOT a sleep lock */
449 ETAP_VM_PMAP_SYS,
450 ETAP_VM_PMAP_SYS_I);
451
452 simple_lock_init(&kernel_pmap->lock, ETAP_VM_PMAP_KERNEL);
453
454 kernel_pmap->pmap_link.next = (queue_t)kernel_pmap; /* Set up anchor forward */
455 kernel_pmap->pmap_link.prev = (queue_t)kernel_pmap; /* Set up anchor reverse */
456 kernel_pmap->ref_count = 1;
457 kernel_pmap->space = PPC_SID_KERNEL;
458 kernel_pmap->pmapvr = 0; /* Virtual = Real */
459 kernel_pmap->bmaps = 0; /* No block pages just yet */
460 for(i=0; i < 128; i++) { /* Clear usage slots */
461 kernel_pmap->pmapUsage[i] = 0;
462 }
463 for(i=0; i < 16; i++) { /* Initialize for laughs */
464 kernel_pmap->pmapSegs[i] = SEG_REG_PROT | (i << 20) | PPC_SID_KERNEL;
465 }
466
467 /*
468 * Allocate: (from first_avail up)
469 * Aligned to its own size:
470 * hash table (for mem size 2**x, allocate 2**(x-10) entries)
471 * mapping table (same size and immediatly following hash table)
472 */
473 /* hash_table_size must be a power of 2, recommended sizes are
474 * taken from PPC601 User Manual, table 6-19. We take the next
475 * highest size if mem_size is not a power of two.
476 * TODO NMGS make this configurable at boot time.
477 */
478
479 num = sizeof(pte_t) * (mem_size >> 10);
480
481 for (hash_table_size = 64 * 1024; /* minimum size = 64Kbytes */
482 hash_table_size < num;
483 hash_table_size *= 2)
484 continue;
485
486 if (num > (sizeof(pte_t) * 524288))
487 hash_table_size = hash_table_size/2; /* reduce by half above 512MB */
488
489 /* Scale to within any physical memory layout constraints */
490 do {
491 num = atop(mem_size); /* num now holds mem_size in pages */
492
493 /* size of all structures that we're going to allocate */
494
495 size = (vm_size_t) (
496 (InitialSaveBloks * PAGE_SIZE) + /* Allow space for the initial context saveareas */
497 ((InitialSaveBloks / 2) * PAGE_SIZE) + /* For backpocket saveareas */
498 hash_table_size + /* For hash table */
499 hash_table_size + /* For PTEG allocation table */
500 (num * sizeof(struct phys_entry)) /* For the physical entries */
501 );
502
503 mapsize = size = round_page(size); /* Get size of area to map that we just calculated */
504 mapsize = mapsize + kmapsize; /* Account for the kernel text size */
505
506 vmpagesz = round_page(num * sizeof(struct vm_page)); /* Allow for all vm_pages needed to map physical mem */
507 vmmapsz = round_page((num / 8) * sizeof(struct vm_map_entry)); /* Allow for vm_maps */
508
509 mapsize = mapsize + vmpagesz + vmmapsz; /* Add the VM system estimates into the grand total */
510
511 mapsize = mapsize + (4 * 1024 * 1024); /* Allow for 4 meg of extra mappings */
512 mapsize = ((mapsize / PAGE_SIZE) + MAPPERBLOK - 1) / MAPPERBLOK; /* Get number of blocks of mappings we need */
513 mapsize = mapsize + ((mapsize + MAPPERBLOK - 1) / MAPPERBLOK); /* Account for the mappings themselves */
514
515 #if DEBUG
516 kprintf("pmap_bootstrap: initial vm_pages = %08X\n", vmpagesz);
517 kprintf("pmap_bootstrap: initial vm_maps = %08X\n", vmmapsz);
518 kprintf("pmap_bootstrap: size before mappings = %08X\n", size);
519 kprintf("pmap_bootstrap: kernel map size = %08X\n", kmapsize);
520 kprintf("pmap_bootstrap: mapping blocks rqrd = %08X\n", mapsize);
521 #endif
522
523 size = size + (mapsize * PAGE_SIZE); /* Get the true size we need */
524
525 /* hash table must be aligned to its size */
526
527 addr = (*first_avail +
528 (hash_table_size-1)) & ~(hash_table_size-1);
529
530 if (addr + size > pmap_mem_regions[0].end) {
531 hash_table_size /= 2;
532 } else {
533 break;
534 }
535 /* If we have had to shrink hash table to too small, panic */
536 if (hash_table_size == 32 * 1024)
537 panic("cannot lay out pmap memory map correctly");
538 } while (1);
539
540 #if DEBUG
541 kprintf("hash table size=%08X, total size of area=%08X, addr=%08X\n",
542 hash_table_size, size, addr);
543 #endif
544 if (round_page(*first_phys_avail) < trunc_page(addr)) {
545 /* We are stepping over at least one page here, so
546 * add this region to the free regions so that it can
547 * be allocated by pmap_steal
548 */
549 free_regions[free_regions_count].start = round_page(*first_phys_avail);
550 free_regions[free_regions_count].end = trunc_page(addr);
551
552 avail_remaining += (free_regions[free_regions_count].end -
553 free_regions[free_regions_count].start) /
554 PPC_PGBYTES;
555 #if DEBUG
556 kprintf("ADDED FREE REGION from 0x%08x to 0x%08x, avail_remaining = %d\n",
557 free_regions[free_regions_count].start,free_regions[free_regions_count].end,
558 avail_remaining);
559 #endif /* DEBUG */
560 free_regions_count++;
561 }
562
563 /* Zero everything - this also invalidates the hash table entries */
564 bzero((char *)addr, size);
565
566 /* Set up some pointers to our new structures */
567
568 /* from here, addr points to the next free address */
569
570 first_used_addr = addr; /* remember where we started */
571
572 /* Set up hash table address and dma buffer address, keeping
573 * alignment. These mappings are all 1-1, so dma_r == dma_v
574 *
575 * If hash_table_size == dma_buffer_alignment, then put hash_table
576 * first, since dma_buffer_size may be smaller than alignment, but
577 * hash table alignment==hash_table_size.
578 */
579 hash_table_base = addr;
580
581 addr += hash_table_size;
582 addr += hash_table_size; /* Add another for the PTEG Control Area */
583 assert((hash_table_base & (hash_table_size-1)) == 0);
584
585 pcaptr = (PCA *)(hash_table_base+hash_table_size); /* Point to the PCA table */
586 mapCtl.mapcflush.pcaptr = pcaptr;
587
588 for(i=0; i < (hash_table_size/64) ; i++) { /* For all of PTEG control areas: */
589 pcaptr[i].flgs.PCAalflgs.PCAfree=0xFF; /* Mark all slots free */
590 pcaptr[i].flgs.PCAalflgs.PCAsteal=0x01; /* Initialize steal position */
591 }
592
593 savearea_init(&addr); /* Initialize the savearea chains and data */
594
595 /* phys_table is static to help debugging,
596 * this variable is no longer actually used
597 * outside of this scope
598 */
599
600 phys_table = (struct phys_entry *) addr;
601
602 #if DEBUG
603 kprintf("hash_table_base =%08X\n", hash_table_base);
604 kprintf("phys_table =%08X\n", phys_table);
605 kprintf("pmap_mem_regions_count =%08X\n", pmap_mem_regions_count);
606 #endif
607
608 for (i = 0; i < pmap_mem_regions_count; i++) {
609
610 pmap_mem_regions[i].phys_table = phys_table;
611 rsize = (pmap_mem_regions[i].end - (unsigned int)pmap_mem_regions[i].start)/PAGE_SIZE;
612
613 #if DEBUG
614 kprintf("Initializing physical table for region %d\n", i);
615 kprintf(" table=%08X, size=%08X, start=%08X, end=%08X\n",
616 phys_table, rsize, pmap_mem_regions[i].start,
617 (unsigned int)pmap_mem_regions[i].end);
618 #endif
619
620 for (j = 0; j < rsize; j++) {
621 phys_table[j].phys_link = MAPPING_NULL;
622 mapping_phys_init(&phys_table[j], (unsigned int)pmap_mem_regions[i].start+(j*PAGE_SIZE),
623 PTE_WIMG_DEFAULT); /* Initializes hw specific storage attributes */
624 }
625 phys_table = phys_table +
626 atop(pmap_mem_regions[i].end - pmap_mem_regions[i].start);
627 }
628
629 /* restore phys_table for debug */
630 phys_table = (struct phys_entry *) addr;
631
632 addr += sizeof(struct phys_entry) * num;
633
634 simple_lock_init(&tlb_system_lock, ETAP_VM_PMAP_TLB);
635
636 /* Initialise the registers necessary for supporting the hashtable */
637 #if DEBUG
638 kprintf("*** hash_table_init: base=%08X, size=%08X\n", hash_table_base, hash_table_size);
639 #endif
640
641 hash_table_init(hash_table_base, hash_table_size);
642
643 /*
644 * Remaining space is for mapping entries. Tell the initializer routine that
645 * the mapping system can't release this block because it's permanently assigned
646 */
647
648 mapping_init(); /* Initialize the mapping tables */
649
650 for(i = addr; i < first_used_addr + size; i += PAGE_SIZE) { /* Add initial mapping blocks */
651 mapping_free_init(i, 1, 0); /* Pass block address and say that this one is not releasable */
652 }
653 mapCtl.mapcmin = MAPPERBLOK; /* Make sure we only adjust one at a time */
654
655 #if DEBUG
656
657 kprintf("mapping kernel memory from 0x%08x to 0x%08x, to address 0x%08x\n",
658 first_used_addr, round_page(first_used_addr+size),
659 first_used_addr);
660 #endif /* DEBUG */
661
662 /* Map V=R the page tables */
663 pmap_map(first_used_addr, first_used_addr,
664 round_page(first_used_addr+size), VM_PROT_READ | VM_PROT_WRITE);
665
666 #if DEBUG
667
668 for(i=first_used_addr; i < round_page(first_used_addr+size); i+=PAGE_SIZE) { /* Step through all these mappings */
669 if(i != (j = kvtophys(i))) { /* Verify that the mapping was made V=R */
670 kprintf("*** V=R mapping failed to verify: V=%08X; R=%08X\n", i, j);
671 }
672 }
673 #endif
674
675 *first_avail = round_page(first_used_addr + size);
676 first_free_virt = round_page(first_used_addr + size);
677
678 /* All the rest of memory is free - add it to the free
679 * regions so that it can be allocated by pmap_steal
680 */
681 free_regions[free_regions_count].start = *first_avail;
682 free_regions[free_regions_count].end = pmap_mem_regions[0].end;
683
684 avail_remaining += (free_regions[free_regions_count].end -
685 free_regions[free_regions_count].start) /
686 PPC_PGBYTES;
687
688 #if DEBUG
689 kprintf("ADDED FREE REGION from 0x%08x to 0x%08x, avail_remaining = %d\n",
690 free_regions[free_regions_count].start,free_regions[free_regions_count].end,
691 avail_remaining);
692 #endif /* DEBUG */
693
694 free_regions_count++;
695
696 current_free_region = 0;
697
698 avail_next = free_regions[current_free_region].start;
699
700 #if DEBUG
701 kprintf("Number of free regions=%d\n",free_regions_count); /* (TEST/DEBUG) */
702 kprintf("Current free region=%d\n",current_free_region); /* (TEST/DEBUG) */
703 for(i=0;i<free_regions_count; i++) { /* (TEST/DEBUG) */
704 kprintf("Free region %3d - from %08X to %08X\n", i, free_regions[i].start,
705 free_regions[i].end); /* (TEST/DEBUG) */
706 }
707 for (i = 0; i < pmap_mem_regions_count; i++) { /* (TEST/DEBUG) */
708 kprintf("PMAP region %3d - from %08X to %08X; phys=%08X\n", i, /* (TEST/DEBUG) */
709 pmap_mem_regions[i].start, /* (TEST/DEBUG) */
710 pmap_mem_regions[i].end, /* (TEST/DEBUG) */
711 pmap_mem_regions[i].phys_table); /* (TEST/DEBUG) */
712 }
713 #endif
714
715 }
716
717 /*
718 * pmap_init(spa, epa)
719 * finishes the initialization of the pmap module.
720 * This procedure is called from vm_mem_init() in vm/vm_init.c
721 * to initialize any remaining data structures that the pmap module
722 * needs to map virtual memory (VM is already ON).
723 *
724 * Note that the pmap needs to be sized and aligned to
725 * a power of two. This is because it is used both in virtual and
726 * real so it can't span a page boundary.
727 */
728
729 void
730 pmap_init(void)
731 {
732
733
734 pmap_zone = zinit(pmapSize, 400 * pmapSize, 4096, "pmap");
735 #if ZONE_DEBUG
736 zone_debug_disable(pmap_zone); /* Can't debug this one 'cause it messes with size and alignment */
737 #endif /* ZONE_DEBUG */
738
739 pmap_initialized = TRUE;
740
741 /*
742 * Initialize list of freed up pmaps
743 */
744 free_pmap_list = 0; /* Set that there are no free pmaps */
745 free_pmap_count = 0;
746 simple_lock_init(&free_pmap_lock, ETAP_VM_PMAP_CACHE);
747 }
748
749 unsigned int pmap_free_pages(void)
750 {
751 return avail_remaining;
752 }
753
754 boolean_t pmap_next_page(vm_offset_t *addrp)
755 {
756 /* Non optimal, but only used for virtual memory startup.
757 * Allocate memory from a table of free physical addresses
758 * If there are no more free entries, too bad. We have two
759 * tables to look through, free_regions[] which holds free
760 * regions from inside pmap_mem_regions[0], and the others...
761 * pmap_mem_regions[1..]
762 */
763
764 /* current_free_region indicates the next free entry,
765 * if it's less than free_regions_count, then we're still
766 * in free_regions, otherwise we're in pmap_mem_regions
767 */
768
769 if (current_free_region >= free_regions_count) {
770 /* We're into the pmap_mem_regions, handle this
771 * separately to free_regions
772 */
773
774 int current_pmap_mem_region = current_free_region -
775 free_regions_count + 1;
776 if (current_pmap_mem_region > pmap_mem_regions_count)
777 return FALSE;
778 *addrp = avail_next;
779 avail_next += PAGE_SIZE;
780 avail_remaining--;
781 if (avail_next >= pmap_mem_regions[current_pmap_mem_region].end) {
782 current_free_region++;
783 current_pmap_mem_region++;
784 avail_next = pmap_mem_regions[current_pmap_mem_region].start;
785 #if DEBUG
786 kprintf("pmap_next_page : next region start=0x%08x\n",avail_next);
787 #endif /* DEBUG */
788 }
789 return TRUE;
790 }
791
792 /* We're in the free_regions, allocate next page and increment
793 * counters
794 */
795 *addrp = avail_next;
796
797 avail_next += PAGE_SIZE;
798 avail_remaining--;
799
800 if (avail_next >= free_regions[current_free_region].end) {
801 current_free_region++;
802 if (current_free_region < free_regions_count)
803 avail_next = free_regions[current_free_region].start;
804 else
805 avail_next = pmap_mem_regions[current_free_region -
806 free_regions_count + 1].start;
807 #if DEBUG
808 kprintf("pmap_next_page : next region start=0x%08x\n",avail_next);
809 #endif
810 }
811 return TRUE;
812 }
813
814 void pmap_virtual_space(
815 vm_offset_t *startp,
816 vm_offset_t *endp)
817 {
818 *startp = round_page(first_free_virt);
819 *endp = VM_MAX_KERNEL_ADDRESS;
820 }
821
822 /*
823 * pmap_create
824 *
825 * Create and return a physical map.
826 *
827 * If the size specified for the map is zero, the map is an actual physical
828 * map, and may be referenced by the hardware.
829 *
830 * A pmap is either in the free list or in the in-use list. The only use
831 * of the in-use list (aside from debugging) is to handle the VSID wrap situation.
832 * Whenever a new pmap is allocated (i.e., not recovered from the free list). The
833 * in-use list is matched until a hole in the VSID sequence is found. (Note
834 * that the in-use pmaps are queued in VSID sequence order.) This is all done
835 * while free_pmap_lock is held.
836 *
837 * If the size specified is non-zero, the map will be used in software
838 * only, and is bounded by that size.
839 */
840 pmap_t
841 pmap_create(vm_size_t size)
842 {
843 pmap_t pmap, ckpmap, fore, aft;
844 int s, i;
845 space_t sid;
846 unsigned int currSID;
847
848 #if PMAP_LOWTRACE
849 dbgTrace(0xF1D00001, size, 0); /* (TEST/DEBUG) */
850 #endif
851
852 #if DEBUG
853 if (pmdebug & PDB_USER)
854 kprintf("pmap_create(size=%x)%c", size, size ? '\n' : ' ');
855 #endif
856
857 /*
858 * A software use-only map doesn't even need a pmap structure.
859 */
860 if (size)
861 return(PMAP_NULL);
862
863 /*
864 * If there is a pmap in the pmap free list, reuse it.
865 * Note that we use free_pmap_list for all chaining of pmaps, both to
866 * the free list and the in use chain (anchored from kernel_pmap).
867 */
868 s = splhigh();
869 simple_lock(&free_pmap_lock);
870
871 if(free_pmap_list) { /* Any free? */
872 pmap = free_pmap_list; /* Yes, allocate it */
873 free_pmap_list = (pmap_t)pmap->bmaps; /* Dequeue this one (we chain free ones through bmaps) */
874 free_pmap_count--;
875 }
876 else {
877 simple_unlock(&free_pmap_lock); /* Unlock just in case */
878 splx(s);
879
880 pmap = (pmap_t) zalloc(pmap_zone); /* Get one */
881 if (pmap == PMAP_NULL) return(PMAP_NULL); /* Handle out-of-memory condition */
882
883 bzero((char *)pmap, pmapSize); /* Clean up the pmap */
884
885 s = splhigh();
886 simple_lock(&free_pmap_lock); /* Lock it back up */
887
888 ckpmap = cursor_pmap; /* Get starting point for free ID search */
889 currSID = ckpmap->spaceNum; /* Get the actual space ID number */
890
891 while(1) { /* Keep trying until something happens */
892
893 currSID = (currSID + 1) & SID_MAX; /* Get the next in the sequence */
894 ckpmap = (pmap_t)ckpmap->pmap_link.next; /* On to the next in-use pmap */
895
896 if(ckpmap->spaceNum != currSID) break; /* If we are out of sequence, this is free */
897
898 if(ckpmap == cursor_pmap) { /* See if we have 2^20 already allocated */
899 panic("pmap_create: Maximum number (2^20) active address spaces reached\n"); /* Die pig dog */
900 }
901 }
902
903 pmap->space = (currSID * incrVSID) & SID_MAX; /* Calculate the actual VSID */
904 pmap->spaceNum = currSID; /* Set the space ID number */
905
906 /*
907 * Now we link into the chain just before the out of sequence guy.
908 */
909
910 fore = (pmap_t)ckpmap->pmap_link.prev; /* Get the current's previous */
911 pmap->pmap_link.next = (queue_t)ckpmap; /* My next points to the current */
912 fore->pmap_link.next = (queue_t)pmap; /* Current's previous's next points to me */
913 pmap->pmap_link.prev = (queue_t)fore; /* My prev points to what the current pointed to */
914 ckpmap->pmap_link.prev = (queue_t)pmap; /* Current's prev points to me */
915
916 simple_lock_init(&pmap->lock, ETAP_VM_PMAP);
917 pmap->pmapvr = (unsigned int)pmap ^ (unsigned int)pmap_extract(kernel_pmap, (vm_offset_t)pmap); /* Get physical pointer to the pmap and make mask */
918 }
919 pmap->ref_count = 1;
920 pmap->stats.resident_count = 0;
921 pmap->stats.wired_count = 0;
922 pmap->bmaps = 0; /* Clear block map pointer to 0 */
923 pmap->vflags = 0; /* Mark all alternates invalid for now */
924 for(i=0; i < 128; i++) { /* Clean out usage slots */
925 pmap->pmapUsage[i] = 0;
926 }
927 for(i=0; i < 16; i++) { /* Initialize for laughs */
928 pmap->pmapSegs[i] = SEG_REG_PROT | (i << 20) | pmap->space;
929 }
930
931 #if PMAP_LOWTRACE
932 dbgTrace(0xF1D00002, (unsigned int)pmap, (unsigned int)pmap->space); /* (TEST/DEBUG) */
933 #endif
934
935 #if DEBUG
936 if (pmdebug & PDB_USER)
937 kprintf("-> %x, space id = %d\n", pmap, pmap->space);
938 #endif
939
940 simple_unlock(&free_pmap_lock);
941 splx(s);
942 return(pmap);
943 }
944
945 /*
946 * pmap_destroy
947 *
948 * Gives up a reference to the specified pmap. When the reference count
949 * reaches zero the pmap structure is added to the pmap free list.
950 *
951 * Should only be called if the map contains no valid mappings.
952 */
953 void
954 pmap_destroy(pmap_t pmap)
955 {
956 int ref_count;
957 spl_t s;
958 pmap_t fore, aft;
959
960 #if PMAP_LOWTRACE
961 dbgTrace(0xF1D00003, (unsigned int)pmap, 0); /* (TEST/DEBUG) */
962 #endif
963
964 #if DEBUG
965 if (pmdebug & PDB_USER)
966 kprintf("pmap_destroy(pmap=%x)\n", pmap);
967 #endif
968
969 if (pmap == PMAP_NULL)
970 return;
971
972 ref_count=hw_atomic_sub(&pmap->ref_count, 1); /* Back off the count */
973 if(ref_count>0) return; /* Still more users, leave now... */
974
975 if(ref_count < 0) /* Did we go too far? */
976 panic("pmap_destroy(): ref_count < 0");
977
978 #ifdef notdef
979 if(pmap->stats.resident_count != 0)
980 panic("PMAP_DESTROY: pmap not empty");
981 #else
982 if(pmap->stats.resident_count != 0) {
983 pmap_remove(pmap, 0, 0xFFFFF000);
984 }
985 #endif
986
987 /*
988 * Add the pmap to the pmap free list.
989 */
990
991 s = splhigh();
992 /*
993 * Add the pmap to the pmap free list.
994 */
995 simple_lock(&free_pmap_lock);
996
997 if (free_pmap_count <= free_pmap_max) { /* Do we have enough spares? */
998
999 pmap->bmaps = (struct blokmap *)free_pmap_list; /* Queue in front */
1000 free_pmap_list = pmap;
1001 free_pmap_count++;
1002 simple_unlock(&free_pmap_lock);
1003
1004 } else {
1005 if(cursor_pmap == pmap) cursor_pmap = (pmap_t)pmap->pmap_link.prev; /* If we are releasing the cursor, back up */
1006 fore = (pmap_t)pmap->pmap_link.prev;
1007 aft = (pmap_t)pmap->pmap_link.next;
1008 fore->pmap_link.next = pmap->pmap_link.next; /* My previous's next is my next */
1009 aft->pmap_link.prev = pmap->pmap_link.prev; /* My next's previous is my previous */
1010 simple_unlock(&free_pmap_lock);
1011 zfree(pmap_zone, (vm_offset_t) pmap);
1012 }
1013 splx(s);
1014 }
1015
1016 /*
1017 * pmap_reference(pmap)
1018 * gains a reference to the specified pmap.
1019 */
1020 void
1021 pmap_reference(pmap_t pmap)
1022 {
1023 spl_t s;
1024
1025 #if PMAP_LOWTRACE
1026 dbgTrace(0xF1D00004, (unsigned int)pmap, 0); /* (TEST/DEBUG) */
1027 #endif
1028
1029 #if DEBUG
1030 if (pmdebug & PDB_USER)
1031 kprintf("pmap_reference(pmap=%x)\n", pmap);
1032 #endif
1033
1034 if (pmap != PMAP_NULL) hw_atomic_add(&pmap->ref_count, 1); /* Bump the count */
1035 }
1036
1037 /*
1038 * pmap_remove_some_phys
1039 *
1040 * Removes mappings of the associated page from the specified pmap
1041 *
1042 */
1043 void pmap_remove_some_phys(
1044 pmap_t pmap,
1045 vm_offset_t pa)
1046 {
1047 register struct phys_entry *pp;
1048 register struct mapping *mp, *mpv;
1049
1050
1051 if (pmap == PMAP_NULL) return; /* Do nothing if no pmap */
1052
1053 pp = pmap_find_physentry(pa); /* Get the physent for this page */
1054 if (pp == PHYS_NULL) return; /* Leave if not in physical RAM */
1055
1056 mapping_purge_pmap(pp, pmap);
1057
1058 return; /* Leave... */
1059 }
1060
1061 /*
1062 * pmap_remove(pmap, s, e)
1063 * unmaps all virtual addresses v in the virtual address
1064 * range determined by [s, e) and pmap.
1065 * s and e must be on machine independent page boundaries and
1066 * s must be less than or equal to e.
1067 *
1068 * Note that pmap_remove does not remove any mappings in nested pmaps. We just
1069 * skip those segments.
1070 */
1071 void
1072 pmap_remove(
1073 pmap_t pmap,
1074 vm_offset_t sva,
1075 vm_offset_t eva)
1076 {
1077 spl_t spl;
1078 struct mapping *mp, *blm;
1079 vm_offset_t lpage;
1080
1081 #if PMAP_LOWTRACE
1082 dbgTrace(0xF1D00005, (unsigned int)pmap, sva|((eva-sva)>>12)); /* (TEST/DEBUG) */
1083 #endif
1084
1085 #if DEBUG
1086 if (pmdebug & PDB_USER)
1087 kprintf("pmap_remove(pmap=%x, sva=%x, eva=%x)\n",
1088 pmap, sva, eva);
1089 #endif
1090
1091 if (pmap == PMAP_NULL)
1092 return;
1093
1094 /* It is just possible that eva might have wrapped around to zero,
1095 * and sometimes we get asked to liberate something of size zero
1096 * even though it's dumb (eg. after zero length read_overwrites)
1097 */
1098 assert(eva >= sva);
1099
1100 /* If these are not page aligned the loop might not terminate */
1101 assert((sva == trunc_page(sva)) && (eva == trunc_page(eva)));
1102
1103 /* We liberate addresses from high to low, since the stack grows
1104 * down. This means that we won't need to test addresses below
1105 * the limit of stack growth
1106 */
1107
1108 debugLog2(44, sva, eva); /* Log pmap_map call */
1109
1110 sva = trunc_page(sva); /* Make it clean */
1111 lpage = trunc_page(eva) - PAGE_SIZE; /* Point to the last page contained in the range */
1112
1113 /*
1114 * Here we will remove all of the block mappings that overlap this range.
1115 * hw_rem_blk removes one mapping in the range and returns. If it returns
1116 * 0, there are no blocks in the range.
1117 */
1118
1119 while(mp = (mapping *)hw_rem_blk(pmap, sva, lpage)) { /* Keep going until no more */
1120 if((unsigned int)mp & 1) { /* Make sure we don't unmap a permanent one */
1121 blm = (struct mapping *)hw_cpv((mapping *)((unsigned int)mp & 0xFFFFFFFC)); /* Get virtual address */
1122 panic("mapping_remove: attempt to unmap a permanent mapping - pmap = %08X, va = %08X, mapping = %08X\n",
1123 pmap, sva, blm);
1124 }
1125 if (!((unsigned int)mp & 2))
1126 mapping_free(hw_cpv(mp)); /* Release it */
1127 }
1128 while (pmap->stats.resident_count && (eva > sva)) {
1129
1130 eva -= PAGE_SIZE; /* Back up a page */
1131
1132 #if 1
1133 if((0x00008000 >> (sva >> 28)) & pmap->vflags)
1134 panic("pmap_remove: attempt to remove nested vaddr; pmap = %08X, vaddr = %08X\n", pmap, sva); /* (TEST/DEBUG) panic */
1135 #endif
1136 if(!(pmap->pmapUsage[(eva >> pmapUsageShft) & pmapUsageMask])) { /* See if this chunk has anything in it */
1137 eva = eva & (-pmapUsageSize); /* Back up into the previous slot */
1138 continue; /* Check the next... */
1139 }
1140 mapping_remove(pmap, eva); /* Remove the mapping for this address */
1141 }
1142
1143 debugLog2(45, 0, 0); /* Log pmap_map call */
1144 }
1145
1146 /*
1147 * Routine:
1148 * pmap_page_protect
1149 *
1150 * Function:
1151 * Lower the permission for all mappings to a given page.
1152 */
1153 void
1154 pmap_page_protect(
1155 vm_offset_t pa,
1156 vm_prot_t prot)
1157 {
1158 register struct phys_entry *pp;
1159 boolean_t remove;
1160
1161
1162 #if PMAP_LOWTRACE
1163 dbgTrace(0xF1D00006, (unsigned int)pa, (unsigned int)prot); /* (TEST/DEBUG) */
1164 #endif
1165
1166 #if DEBUG
1167 if (pmdebug & PDB_USER)
1168 kprintf("pmap_page_protect(pa=%x, prot=%x)\n", pa, prot);
1169 #endif
1170
1171 debugLog2(46, pa, prot); /* Log pmap_page_protect call */
1172
1173 switch (prot) {
1174 case VM_PROT_READ:
1175 case VM_PROT_READ|VM_PROT_EXECUTE:
1176 remove = FALSE;
1177 break;
1178 case VM_PROT_ALL:
1179 return;
1180 default:
1181 remove = TRUE;
1182 break;
1183 }
1184
1185 pp = pmap_find_physentry(pa); /* Get the physent for this page */
1186 if (pp == PHYS_NULL) return; /* Leave if not in physical RAM */
1187
1188 if (remove) { /* If the protection was set to none, we'll remove all mappings */
1189 mapping_purge(pp); /* Get rid of them all */
1190
1191 debugLog2(47, 0, 0); /* Log pmap_map call */
1192 return; /* Leave... */
1193 }
1194
1195 /* When we get here, it means that we are to change the protection for a
1196 * physical page.
1197 */
1198
1199 mapping_protect_phys(pp, prot, 0); /* Change protection of all mappings to page. */
1200
1201 debugLog2(47, 1, 0); /* Log pmap_map call */
1202 }
1203
1204 /*
1205 * pmap_protect(pmap, s, e, prot)
1206 * changes the protection on all virtual addresses v in the
1207 * virtual address range determined by [s, e] and pmap to prot.
1208 * s and e must be on machine independent page boundaries and
1209 * s must be less than or equal to e.
1210 *
1211 * Note that any requests to change the protection of a nested pmap are
1212 * ignored. Those changes MUST be done by calling this with the correct pmap.
1213 */
1214 void pmap_protect(
1215 pmap_t pmap,
1216 vm_offset_t sva,
1217 vm_offset_t eva,
1218 vm_prot_t prot)
1219 {
1220 spl_t spl;
1221 register struct phys_entry *pp;
1222 register struct mapping *mp, *mpv;
1223
1224 #if PMAP_LOWTRACE
1225 dbgTrace(0xF1D00008, (unsigned int)pmap, (unsigned int)(sva|((eva-sva)>>12))); /* (TEST/DEBUG) */
1226 #endif
1227
1228 #if DEBUG
1229 if (pmdebug & PDB_USER)
1230 kprintf("pmap_protect(pmap=%x, sva=%x, eva=%x, prot=%x)\n", pmap, sva, eva, prot);
1231
1232 assert(sva < eva);
1233 #endif
1234
1235 if (pmap == PMAP_NULL) return; /* Do nothing if no pmap */
1236
1237 debugLog2(48, sva, eva); /* Log pmap_map call */
1238
1239 if (prot == VM_PROT_NONE) { /* Should we kill the address range?? */
1240 pmap_remove(pmap, sva, eva); /* Yeah, dump 'em */
1241
1242 debugLog2(49, prot, 0); /* Log pmap_map call */
1243
1244 return; /* Leave... */
1245 }
1246
1247 sva = trunc_page(sva); /* Start up a page boundary */
1248
1249 while(sva < eva) { /* Step through */
1250
1251 if(!(pmap->pmapUsage[(sva >> pmapUsageShft) & pmapUsageMask])) { /* See if this chunk has anything in it */
1252 sva = (sva + pmapUsageSize) &(-pmapUsageSize); /* Jump up into the next slot if nothing here */
1253 if(!sva) break; /* We tried to wrap, kill loop... */
1254 continue; /* Check the next... */
1255 }
1256
1257 #if 1
1258 if((0x00008000 >> (sva >> 28)) & pmap->vflags)
1259 panic("pmap_protect: attempt to protect nested vaddr; pmap = %08X, vaddr = %08X\n", pmap, sva); /* (TEST/DEBUG) panic */
1260 #endif
1261
1262 mapping_protect(pmap, sva, prot); /* Change the protection on the page */
1263 sva += PAGE_SIZE; /* On to the next page */
1264 }
1265
1266 debugLog2(49, prot, 1); /* Log pmap_map call */
1267 return; /* Leave... */
1268 }
1269
1270
1271
1272 /*
1273 * pmap_enter
1274 *
1275 * Create a translation for the virtual address (virt) to the physical
1276 * address (phys) in the pmap with the protection requested. If the
1277 * translation is wired then we can not allow a full page fault, i.e.,
1278 * the mapping control block is not eligible to be stolen in a low memory
1279 * condition.
1280 *
1281 * NB: This is the only routine which MAY NOT lazy-evaluate
1282 * or lose information. That is, this routine must actually
1283 * insert this page into the given map NOW.
1284 */
1285 void
1286 pmap_enter(pmap_t pmap, vm_offset_t va, vm_offset_t pa, vm_prot_t prot,
1287 unsigned int flags, boolean_t wired)
1288 {
1289 spl_t spl;
1290 struct mapping *mp;
1291 struct phys_entry *pp;
1292 int memattr;
1293
1294 #if PMAP_LOWTRACE
1295 dbgTrace(0xF1D00009, (unsigned int)pmap, (unsigned int)va); /* (TEST/DEBUG) */
1296 dbgTrace(0xF1D04009, (unsigned int)pa, (unsigned int)prot); /* (TEST/DEBUG) */
1297 #endif
1298
1299 if (pmap == PMAP_NULL) return; /* If they gave us no pmap, just leave... */
1300
1301 debugLog2(50, va, pa); /* Log pmap_map call */
1302
1303 pp = pmap_find_physentry(pa); /* Get the physent for this physical page */
1304
1305 if((0x00008000 >> (va >> 28)) & pmap->vflags)
1306 panic("pmap_enter: attempt to map into nested vaddr; pmap = %08X, vaddr = %08X\n", pmap, va); /* (TEST/DEBUG) panic */
1307
1308 spl=splhigh(); /* Have to disallow interrupts between the
1309 time we possibly clear a mapping and the time
1310 we get it remapped again. An I/O SLIH could
1311 try to drive an IOR using the page before
1312 we get it mapped (Dude! This was a tough
1313 bug!!!!) */
1314
1315 mapping_remove(pmap, va); /* Remove any other mapping at this address */
1316
1317 if(flags & VM_WIMG_USE_DEFAULT) {
1318 if(pp) {
1319 /* Set attr to the phys default */
1320 memattr = ((pp->pte1&0x00000078) >> 3);
1321 } else {
1322 memattr = PTE_WIMG_UNCACHED_COHERENT_GUARDED;
1323 }
1324 } else {
1325 memattr = flags & VM_WIMG_MASK;
1326 }
1327
1328
1329 /* Make the address mapping */
1330 mp=mapping_make(pmap, pp, va, pa, prot, memattr, 0);
1331
1332 splx(spl); /* I'm not busy no more - come what may */
1333
1334 debugLog2(51, prot, 0); /* Log pmap_map call */
1335
1336 #if DEBUG
1337 if (pmdebug & (PDB_USER|PDB_ENTER))
1338 kprintf("leaving pmap_enter\n");
1339 #endif
1340
1341 }
1342
1343 /*
1344 * pmap_extract(pmap, va)
1345 * returns the physical address corrsponding to the
1346 * virtual address specified by pmap and va if the
1347 * virtual address is mapped and 0 if it is not.
1348 */
1349 vm_offset_t pmap_extract(pmap_t pmap, vm_offset_t va) {
1350
1351 spl_t spl;
1352 register struct mapping *mp, *mpv;
1353 register vm_offset_t pa;
1354 unsigned int seg;
1355 pmap_t actpmap;
1356
1357
1358 #if PMAP_LOWTRACE
1359 dbgTrace(0xF1D0000B, (unsigned int)pmap, (unsigned int)va); /* (TEST/DEBUG) */
1360 #endif
1361 #if DEBUG
1362 if (pmdebug & PDB_USER)
1363 kprintf("pmap_extract(pmap=%x, va=%x)\n", pmap, va);
1364 #endif
1365
1366 seg = va >> 28; /* Isolate segment */
1367 if((0x00008000 >> seg) & pmap->vflags) actpmap = pmap->pmapPmaps[seg]; /* Set nested pmap if there is one */
1368 else actpmap = pmap; /* Otherwise use the one passed in */
1369
1370 pa = (vm_offset_t) 0; /* Clear this to 0 */
1371
1372 debugLog2(52, actpmap->space, va); /* Log pmap_map call */
1373
1374 spl = splhigh(); /* We can't allow any loss of control here */
1375
1376 if(mp=hw_lock_phys_vir(actpmap->space, va)) { /* Find the mapping for this vaddr and lock physent */
1377 if((unsigned int)mp&1) { /* Did the lock on the phys entry time out? */
1378 panic("pmap_extract: timeout obtaining lock on physical entry\n"); /* Scream bloody murder! */
1379 splx(spl); /* Interruptions are cool now */
1380 return 0;
1381 }
1382
1383 mpv = hw_cpv(mp); /* Get virtual address of mapping */
1384 pa = (vm_offset_t)((mpv->PTEr & -PAGE_SIZE) | ((unsigned int)va & (PAGE_SIZE-1))); /* Build the physical address */
1385 if(mpv->physent) hw_unlock_bit((unsigned int *)&mpv->physent->phys_link, PHYS_LOCK); /* Unlock the physical entry */
1386 splx(spl); /* Interruptions are cool now */
1387
1388 debugLog2(53, pa, 0); /* Log pmap_map call */
1389
1390 return pa; /* Return the physical address... */
1391 }
1392
1393 pa = hw_cvp_blk(pmap, va); /* Try to convert odd-sized page (returns 0 if not found) */
1394 /* Note no nested pmaps here */
1395 splx(spl); /* Restore 'rupts */
1396 debugLog2(53, pa, 0); /* Log pmap_map call */
1397 return pa; /* Return physical address or 0 */
1398 }
1399
1400 /*
1401 * pmap_attribute_cache_sync
1402 * Handle the machine attribute calls which involve sync the prcessor
1403 * cache.
1404 */
1405 kern_return_t
1406 pmap_attribute_cache_sync(address, size, attribute, value)
1407 vm_offset_t address;
1408 vm_size_t size;
1409 vm_machine_attribute_t attribute;
1410 vm_machine_attribute_val_t* value;
1411 {
1412 while(size) {
1413 switch (*value) { /* What type was that again? */
1414 case MATTR_VAL_CACHE_SYNC: /* It is sync I+D caches */
1415 sync_cache(address, PAGE_SIZE); /* Sync up dem caches */
1416 break; /* Done with this one here... */
1417
1418 case MATTR_VAL_CACHE_FLUSH: /* It is flush from all caches */
1419 flush_dcache(address, PAGE_SIZE, TRUE); /* Flush out the data cache */
1420 invalidate_icache(address,
1421 PAGE_SIZE, TRUE); /* Flush out the instruction cache */
1422 break; /* Done with this one here... */
1423
1424 case MATTR_VAL_DCACHE_FLUSH: /* It is flush from data cache(s) */
1425 flush_dcache(address, PAGE_SIZE, TRUE); /* Flush out the data cache */
1426 break; /* Done with this one here... */
1427
1428 case MATTR_VAL_ICACHE_FLUSH: /* It is flush from instr cache(s) */
1429 invalidate_icache(address,
1430 PAGE_SIZE, TRUE); /* Flush out the instruction cache */
1431 break; /* Done with this one here... */
1432 }
1433 size -= PAGE_SIZE;
1434 }
1435 return KERN_SUCCESS;;
1436 }
1437
1438
1439 /*
1440 * pmap_attributes:
1441 *
1442 * Set/Get special memory attributes; Set is not implemented.
1443 *
1444 * Note: 'VAL_GET_INFO' is used to return info about a page.
1445 * If less than 1 page is specified, return the physical page
1446 * mapping and a count of the number of mappings to that page.
1447 * If more than one page is specified, return the number
1448 * of resident pages and the number of shared (more than
1449 * one mapping) pages in the range;
1450 *
1451 */
1452 kern_return_t
1453 pmap_attribute(pmap, address, size, attribute, value)
1454 pmap_t pmap;
1455 vm_offset_t address;
1456 vm_size_t size;
1457 vm_machine_attribute_t attribute;
1458 vm_machine_attribute_val_t* value;
1459 {
1460 spl_t s;
1461 vm_offset_t sva, eva;
1462 vm_offset_t pa;
1463 kern_return_t ret;
1464 register struct mapping *mp, *mpv;
1465 register struct phys_entry *pp;
1466 int total, seg;
1467 pmap_t actpmap;
1468
1469 if (attribute != MATTR_CACHE)
1470 return KERN_INVALID_ARGUMENT;
1471
1472 /* We can't get the caching attribute for more than one page
1473 * at a time
1474 */
1475 if ((*value == MATTR_VAL_GET) &&
1476 (trunc_page(address) != trunc_page(address+size-1)))
1477 return KERN_INVALID_ARGUMENT;
1478
1479 if (pmap == PMAP_NULL)
1480 return KERN_SUCCESS;
1481
1482 sva = trunc_page(address);
1483 eva = round_page(address + size);
1484 ret = KERN_SUCCESS;
1485
1486 debugLog2(54, address, attribute); /* Log pmap_map call */
1487
1488 switch (*value) {
1489 case MATTR_VAL_CACHE_SYNC: /* sync I+D caches */
1490 case MATTR_VAL_CACHE_FLUSH: /* flush from all caches */
1491 case MATTR_VAL_DCACHE_FLUSH: /* flush from data cache(s) */
1492 case MATTR_VAL_ICACHE_FLUSH: /* flush from instr cache(s) */
1493 sva = trunc_page(sva);
1494 s = splhigh();
1495
1496 while (sva < eva) {
1497 seg = sva >> 28; /* Isolate segment */
1498 if((0x00008000 >> seg) & pmap->vflags) actpmap = pmap->pmapPmaps[seg]; /* Set nested pmap if there is one */
1499 else actpmap = pmap; /* Otherwise use the one passed in */
1500
1501 /*
1502 * Note: the following should work ok with nested pmaps because there are not overlayed mappings
1503 */
1504 if(!(actpmap->pmapUsage[(sva >> pmapUsageShft) & pmapUsageMask])) { /* See if this chunk has anything in it */
1505 sva = (sva + pmapUsageSize) & (-pmapUsageSize); /* Jump up into the next slot if nothing here */
1506 if(!sva) break; /* We tried to wrap, kill loop... */
1507 continue; /* Check the next... */
1508 }
1509
1510 if(!(mp = hw_lock_phys_vir(actpmap->space, sva))) { /* Find the mapping for this vaddr and lock physent */
1511 sva += PAGE_SIZE; /* Point to the next page */
1512 continue; /* Skip if the page is not mapped... */
1513 }
1514
1515 if((unsigned int)mp&1) { /* Did the lock on the phys entry time out? */
1516 panic("pmap_attribute: timeout obtaining lock on physical entry\n"); /* Scream bloody murder! */
1517 continue;
1518 }
1519
1520 mpv = hw_cpv(mp); /* Get virtual address of mapping */
1521 if((unsigned int)mpv->physent) { /* Is there a physical entry? */
1522 pa = (vm_offset_t)mpv->physent->pte1 & -PAGE_SIZE; /* Yes, get the physical address from there */
1523 }
1524 else {
1525 pa = (vm_offset_t)(mpv->PTEr & PAGE_SIZE); /* Otherwise from the mapping */
1526 }
1527
1528 switch (*value) { /* What type was that again? */
1529 case MATTR_VAL_CACHE_SYNC: /* It is sync I+D caches */
1530 sync_cache(pa, PAGE_SIZE); /* Sync up dem caches */
1531 break; /* Done with this one here... */
1532
1533 case MATTR_VAL_CACHE_FLUSH: /* It is flush from all caches */
1534 flush_dcache(pa, PAGE_SIZE, TRUE); /* Flush out the data cache */
1535 invalidate_icache(pa, PAGE_SIZE, TRUE); /* Flush out the instruction cache */
1536 break; /* Done with this one here... */
1537
1538 case MATTR_VAL_DCACHE_FLUSH: /* It is flush from data cache(s) */
1539 flush_dcache(pa, PAGE_SIZE, TRUE); /* Flush out the data cache */
1540 break; /* Done with this one here... */
1541
1542 case MATTR_VAL_ICACHE_FLUSH: /* It is flush from instr cache(s) */
1543 invalidate_icache(pa, PAGE_SIZE, TRUE); /* Flush out the instruction cache */
1544 break; /* Done with this one here... */
1545 }
1546 if(mpv->physent) hw_unlock_bit((unsigned int *)&mpv->physent->phys_link, PHYS_LOCK); /* Unlock the physical entry if it exists*/
1547
1548 sva += PAGE_SIZE; /* Point to the next page */
1549 }
1550 splx(s);
1551 break;
1552
1553 case MATTR_VAL_GET_INFO: /* Get info */
1554 total = 0;
1555 s = splhigh(); /* Lock 'em out */
1556
1557 if (size <= PAGE_SIZE) { /* Do they want just one page */
1558 seg = sva >> 28; /* Isolate segment */
1559 if((0x00008000 >> seg) & pmap->vflags) actpmap = pmap->pmapPmaps[seg]; /* Set nested pmap if there is one */
1560 else actpmap = pmap; /* Otherwise use the one passed in */
1561 if(!(mp = hw_lock_phys_vir(actpmap->space, sva))) { /* Find the mapping for this vaddr and lock physent */
1562 *value = 0; /* Return nothing if no mapping */
1563 }
1564 else {
1565 if((unsigned int)mp&1) { /* Did the lock on the phys entry time out? */
1566 panic("pmap_attribute: timeout obtaining lock on physical entry\n"); /* Scream bloody murder! */
1567 }
1568 mpv = hw_cpv(mp); /* Get virtual address of mapping */
1569 if(pp = mpv->physent) { /* Check for a physical entry */
1570 total = 0; /* Clear the count */
1571 for (mpv = (mapping *)hw_cpv((mapping *)((unsigned int)pp->phys_link & ~PHYS_FLAGS)); mpv != NULL; mpv = hw_cpv(mp->next)) total++; /* Count the mapping */
1572 *value = (vm_machine_attribute_val_t) ((pp->pte1 & -PAGE_SIZE) | total); /* Pass back the physical address and the count of mappings */
1573 hw_unlock_bit((unsigned int *)&pp->phys_link, PHYS_LOCK); /* Clear the physical entry lock */
1574 }
1575 else { /* This is the case for an I/O mapped area */
1576 *value = (vm_machine_attribute_val_t) ((mpv->PTEr & -PAGE_SIZE) | 1); /* Pass back the physical address and the count of mappings */
1577 }
1578 }
1579 }
1580 else {
1581 total = 0;
1582 while (sva < eva) {
1583 seg = sva >> 28; /* Isolate segment */
1584 if((0x00008000 >> seg) & pmap->vflags) actpmap = pmap->pmapPmaps[seg]; /* Set nested pmap if there is one */
1585 else actpmap = pmap; /* Otherwise use the one passed in */
1586
1587 if(!(actpmap->pmapUsage[(sva >> pmapUsageShft) & pmapUsageMask])) { /* See if this chunk has anything in it */
1588 sva = (sva + pmapUsageSize) & (-pmapUsageSize); /* Jump up into the next slot if nothing here */
1589 if(!sva) break; /* We tried to wrap, kill loop... */
1590 continue; /* Check the next... */
1591 }
1592 if(mp = hw_lock_phys_vir(actpmap->space, sva)) { /* Find the mapping for this vaddr and lock physent */
1593 if((unsigned int)mp&1) { /* Did the lock on the phys entry time out? */
1594 panic("pmap_attribute: timeout obtaining lock on physical entry\n"); /* Scream bloody murder! */
1595 continue;
1596 }
1597 mpv = hw_cpv(mp); /* Get virtual address of mapping */
1598 total += 65536 + (mpv->physent && ((mapping *)((unsigned int)mpv->physent->phys_link & -32))->next); /* Count the "resident" and shared pages */
1599 hw_unlock_bit((unsigned int *)&mpv->physent->phys_link, PHYS_LOCK); /* Clear the physical entry lock */
1600 }
1601 sva += PAGE_SIZE;
1602 }
1603 *value = total;
1604 }
1605 splx(s);
1606 break;
1607
1608 case MATTR_VAL_GET: /* return current value */
1609 case MATTR_VAL_OFF: /* turn attribute off */
1610 case MATTR_VAL_ON: /* turn attribute on */
1611 default:
1612 ret = KERN_INVALID_ARGUMENT;
1613 break;
1614 }
1615
1616 debugLog2(55, 0, 0); /* Log pmap_map call */
1617
1618 return ret;
1619 }
1620
1621 /*
1622 * pmap_sync_caches_phys(vm_offset_t pa)
1623 *
1624 * Invalidates all of the instruction cache on a physical page and
1625 * pushes any dirty data from the data cache for the same physical page
1626 */
1627
1628 void pmap_sync_caches_phys(vm_offset_t pa) {
1629
1630 spl_t s;
1631
1632 s = splhigh(); /* No interruptions here */
1633 sync_cache(trunc_page(pa), PAGE_SIZE); /* Sync up dem caches */
1634 splx(s); /* Allow interruptions */
1635 return;
1636 }
1637
1638 /*
1639 * pmap_collect
1640 *
1641 * Garbage collects the physical map system for pages that are no longer used.
1642 * It isn't implemented or needed or wanted.
1643 */
1644 void
1645 pmap_collect(pmap_t pmap)
1646 {
1647 return;
1648 }
1649
1650 /*
1651 * Routine: pmap_activate
1652 * Function:
1653 * Binds the given physical map to the given
1654 * processor, and returns a hardware map description.
1655 * It isn't implemented or needed or wanted.
1656 */
1657 void
1658 pmap_activate(
1659 pmap_t pmap,
1660 thread_t th,
1661 int which_cpu)
1662 {
1663 return;
1664 }
1665 /*
1666 * pmap_deactivate:
1667 * It isn't implemented or needed or wanted.
1668 */
1669 void
1670 pmap_deactivate(
1671 pmap_t pmap,
1672 thread_t th,
1673 int which_cpu)
1674 {
1675 return;
1676 }
1677
1678 #if DEBUG
1679
1680 /*
1681 * pmap_zero_page
1682 * pmap_copy page
1683 *
1684 * are implemented in movc.s, these
1685 * are just wrappers to help debugging
1686 */
1687
1688 extern void pmap_zero_page_assembler(vm_offset_t p);
1689 extern void pmap_copy_page_assembler(vm_offset_t src, vm_offset_t dst);
1690
1691 /*
1692 * pmap_zero_page(pa)
1693 *
1694 * pmap_zero_page zeros the specified (machine independent) page pa.
1695 */
1696 void
1697 pmap_zero_page(
1698 vm_offset_t p)
1699 {
1700 register struct mapping *mp;
1701 register struct phys_entry *pp;
1702
1703 if (pmdebug & (PDB_USER|PDB_ZERO))
1704 kprintf("pmap_zero_page(pa=%x)\n", p);
1705
1706 /*
1707 * XXX can these happen?
1708 */
1709 if (pmap_find_physentry(p) == PHYS_NULL)
1710 panic("zero_page: physaddr out of range");
1711
1712 pmap_zero_page_assembler(p);
1713 }
1714
1715 /*
1716 * pmap_copy_page(src, dst)
1717 *
1718 * pmap_copy_page copies the specified (machine independent)
1719 * page from physical address src to physical address dst.
1720 *
1721 * We need to invalidate the cache for address dst before
1722 * we do the copy. Apparently there won't be any mappings
1723 * to the dst address normally.
1724 */
1725 void
1726 pmap_copy_page(
1727 vm_offset_t src,
1728 vm_offset_t dst)
1729 {
1730 register struct phys_entry *pp;
1731
1732 if (pmdebug & (PDB_USER|PDB_COPY))
1733 kprintf("pmap_copy_page(spa=%x, dpa=%x)\n", src, dst);
1734 if (pmdebug & PDB_COPY)
1735 kprintf("pmap_copy_page: phys_copy(%x, %x, %x)\n",
1736 src, dst, PAGE_SIZE);
1737
1738 pmap_copy_page_assembler(src, dst);
1739 }
1740 #endif /* DEBUG */
1741
1742 /*
1743 * pmap_pageable(pmap, s, e, pageable)
1744 * Make the specified pages (by pmap, offset)
1745 * pageable (or not) as requested.
1746 *
1747 * A page which is not pageable may not take
1748 * a fault; therefore, its page table entry
1749 * must remain valid for the duration.
1750 *
1751 * This routine is merely advisory; pmap_enter()
1752 * will specify that these pages are to be wired
1753 * down (or not) as appropriate.
1754 *
1755 * (called from vm/vm_fault.c).
1756 */
1757 void
1758 pmap_pageable(
1759 pmap_t pmap,
1760 vm_offset_t start,
1761 vm_offset_t end,
1762 boolean_t pageable)
1763 {
1764
1765 return; /* This is not used... */
1766
1767 }
1768 /*
1769 * Routine: pmap_change_wiring
1770 * NOTE USED ANYMORE.
1771 */
1772 void
1773 pmap_change_wiring(
1774 register pmap_t pmap,
1775 vm_offset_t va,
1776 boolean_t wired)
1777 {
1778 return; /* This is not used... */
1779 }
1780
1781 /*
1782 * pmap_modify_pages(pmap, s, e)
1783 * sets the modified bit on all virtual addresses v in the
1784 * virtual address range determined by [s, e] and pmap,
1785 * s and e must be on machine independent page boundaries and
1786 * s must be less than or equal to e.
1787 */
1788 void
1789 pmap_modify_pages(
1790 pmap_t pmap,
1791 vm_offset_t sva,
1792 vm_offset_t eva)
1793 {
1794 spl_t spl;
1795 mapping *mp;
1796
1797 #if PMAP_LOWTRACE
1798 dbgTrace(0xF1D00010, (unsigned int)pmap, (unsigned int)(sva|((eva-sva)>>12))); /* (TEST/DEBUG) */
1799 #endif
1800
1801 #if DEBUG
1802 if (pmdebug & PDB_USER) kprintf("pmap_modify_pages(pmap=%x, sva=%x, eva=%x)\n", pmap, sva, eva);
1803 #endif
1804
1805 if (pmap == PMAP_NULL) return; /* If no pmap, can't do it... */
1806
1807 debugLog2(56, sva, eva); /* Log pmap_map call */
1808
1809 spl=splhigh(); /* Don't bother me */
1810
1811 for ( ; sva < eva; sva += PAGE_SIZE) { /* Cycle through the whole range */
1812 mp = hw_lock_phys_vir(pmap->space, sva); /* Lock the physical entry for this mapping */
1813 if(mp) { /* Did we find one? */
1814 if((unsigned int)mp&1) { /* Did the lock on the phys entry time out? */
1815 panic("pmap_modify_pages: timeout obtaining lock on physical entry\n"); /* Scream bloody murder! */
1816 continue;
1817 }
1818 mp = hw_cpv(mp); /* Convert to virtual addressing */
1819 if(!mp->physent) continue; /* No physical entry means an I/O page, we can't set attributes */
1820 mapping_set_mod(mp->physent); /* Set the modfied bit for this page */
1821 hw_unlock_bit((unsigned int *)&mp->physent->phys_link, PHYS_LOCK); /* Unlock the physical entry */
1822 }
1823 }
1824 splx(spl); /* Restore the interrupt level */
1825
1826 debugLog2(57, 0, 0); /* Log pmap_map call */
1827 return; /* Leave... */
1828 }
1829
1830 /*
1831 * pmap_clear_modify(phys)
1832 * clears the hardware modified ("dirty") bit for one
1833 * machine independant page starting at the given
1834 * physical address. phys must be aligned on a machine
1835 * independant page boundary.
1836 */
1837 void
1838 pmap_clear_modify(vm_offset_t pa)
1839 {
1840 register struct phys_entry *pp;
1841 spl_t spl;
1842
1843 #if PMAP_LOWTRACE
1844 dbgTrace(0xF1D00011, (unsigned int)pa, 0); /* (TEST/DEBUG) */
1845 #endif
1846 #if DEBUG
1847 if (pmdebug & PDB_USER)
1848 kprintf("pmap_clear_modify(pa=%x)\n", pa);
1849 #endif
1850
1851 pp = pmap_find_physentry(pa); /* Find the physent for this page */
1852 if (pp == PHYS_NULL) return; /* If there isn't one, just leave... */
1853
1854 debugLog2(58, pa, 0); /* Log pmap_map call */
1855
1856 spl=splhigh(); /* Don't bother me */
1857
1858 if(!hw_lock_bit((unsigned int *)&pp->phys_link, PHYS_LOCK, LockTimeOut)) { /* Try to get the lock on the physical entry */
1859 panic("pmap_clear_modify: Timeout getting lock on physent at %08X\n", pp); /* Arrrgghhhh! */
1860 splx(spl); /* Restore 'rupts */
1861 return; /* Should die before here */
1862 }
1863
1864 mapping_clr_mod(pp); /* Clear all change bits for physical page */
1865
1866 hw_unlock_bit((unsigned int *)&pp->phys_link, PHYS_LOCK); /* Unlock the physical entry */
1867 splx(spl); /* Restore the interrupt level */
1868
1869 debugLog2(59, 0, 0); /* Log pmap_map call */
1870 }
1871
1872 /*
1873 * pmap_is_modified(phys)
1874 * returns TRUE if the given physical page has been modified
1875 * since the last call to pmap_clear_modify().
1876 */
1877 boolean_t
1878 pmap_is_modified(register vm_offset_t pa)
1879 {
1880 register struct phys_entry *pp;
1881 spl_t spl;
1882 boolean_t ret;
1883
1884
1885 #if PMAP_LOWTRACE
1886 dbgTrace(0xF1D00012, (unsigned int)pa, 0); /* (TEST/DEBUG) */
1887 #endif
1888 #if DEBUG
1889 if (pmdebug & PDB_USER)
1890 kprintf("pmap_is_modified(pa=%x)\n", pa);
1891 #endif
1892
1893 pp = pmap_find_physentry(pa); /* Find the physent for this page */
1894 if (pp == PHYS_NULL) return(FALSE); /* Just indicate not set... */
1895
1896 debugLog2(60, pa, 0); /* Log pmap_map call */
1897
1898 spl=splhigh(); /* Don't bother me */
1899
1900 if(!hw_lock_bit((unsigned int *)&pp->phys_link, PHYS_LOCK, LockTimeOut)) { /* Try to get the lock on the physical entry */
1901 panic("pmap_is_modified: Timeout getting lock on physent at %08X\n", pp); /* Arrrgghhhh! */
1902 splx(spl); /* Restore 'rupts */
1903 return 0; /* Should die before here */
1904 }
1905
1906 ret = mapping_tst_mod(pp); /* Check for modified */
1907
1908 hw_unlock_bit((unsigned int *)&pp->phys_link, PHYS_LOCK); /* Unlock the physical entry */
1909 splx(spl); /* Restore the interrupt level */
1910
1911 debugLog2(61, ret, 0); /* Log pmap_map call */
1912
1913 return ret;
1914 }
1915
1916 /*
1917 * pmap_clear_reference(phys)
1918 * clears the hardware referenced bit in the given machine
1919 * independant physical page.
1920 *
1921 */
1922 void
1923 pmap_clear_reference(vm_offset_t pa)
1924 {
1925 register struct phys_entry *pp;
1926 spl_t spl;
1927
1928
1929 #if PMAP_LOWTRACE
1930 dbgTrace(0xF1D00013, (unsigned int)pa, 0); /* (TEST/DEBUG) */
1931 #endif
1932 #if DEBUG
1933 if (pmdebug & PDB_USER)
1934 kprintf("pmap_clear_reference(pa=%x)\n", pa);
1935 #endif
1936
1937 pp = pmap_find_physentry(pa); /* Find the physent for this page */
1938 if (pp == PHYS_NULL) return; /* If there isn't one, just leave... */
1939
1940 debugLog2(62, pa, 0); /* Log pmap_map call */
1941
1942 spl=splhigh(); /* Don't bother me */
1943 mapping_clr_ref(pp); /* Clear all reference bits for physical page */
1944 splx(spl); /* Restore the interrupt level */
1945
1946 debugLog2(63, 0, 0); /* Log pmap_map call */
1947
1948 }
1949
1950 /*
1951 * pmap_is_referenced(phys)
1952 * returns TRUE if the given physical page has been referenced
1953 * since the last call to pmap_clear_reference().
1954 */
1955 boolean_t
1956 pmap_is_referenced(vm_offset_t pa)
1957 {
1958 register struct phys_entry *pp;
1959 spl_t spl;
1960 boolean_t ret;
1961
1962
1963 #if PMAP_LOWTRACE
1964 dbgTrace(0xF1D00014, (unsigned int)pa, 0); /* (TEST/DEBUG) */
1965 #endif
1966 #if DEBUG
1967 if (pmdebug & PDB_USER)
1968 kprintf("pmap_is_referenced(pa=%x)\n", pa);
1969 #endif
1970
1971 pp = pmap_find_physentry(pa); /* Find the physent for this page */
1972 if (pp == PHYS_NULL) return(FALSE); /* Just indicate not set... */
1973
1974 debugLog2(64, pa, 0); /* Log pmap_map call */
1975
1976 spl=splhigh(); /* Don't bother me */
1977
1978 if(!hw_lock_bit((unsigned int *)&pp->phys_link, PHYS_LOCK, LockTimeOut)) { /* Try to get the lock on the physical entry */
1979 panic("pmap_is_referenced: Timeout getting lock on physent at %08X\n", pp); /* Arrrgghhhh! */
1980 splx(spl); /* Restore 'rupts */
1981 return 0; /* Should die before here */
1982 }
1983
1984 ret = mapping_tst_ref(pp); /* Check for referenced */
1985
1986 hw_unlock_bit((unsigned int *)&pp->phys_link, PHYS_LOCK); /* Unlock the physical entry */
1987 splx(spl); /* Restore the interrupt level */
1988
1989 debugLog2(65, ret, 0); /* Log pmap_map call */
1990
1991 return ret;
1992 }
1993
1994 #if MACH_VM_DEBUG
1995 int
1996 pmap_list_resident_pages(
1997 register pmap_t pmap,
1998 register vm_offset_t *listp,
1999 register int space)
2000 {
2001 return 0;
2002 }
2003 #endif /* MACH_VM_DEBUG */
2004
2005 /*
2006 * Locking:
2007 * spl: VM
2008 */
2009 void
2010 pmap_copy_part_page(
2011 vm_offset_t src,
2012 vm_offset_t src_offset,
2013 vm_offset_t dst,
2014 vm_offset_t dst_offset,
2015 vm_size_t len)
2016 {
2017 register struct phys_entry *pp_src, *pp_dst;
2018 spl_t s;
2019
2020
2021 #if PMAP_LOWTRACE
2022 dbgTrace(0xF1D00019, (unsigned int)src+src_offset, (unsigned int)dst+dst_offset); /* (TEST/DEBUG) */
2023 dbgTrace(0xF1D04019, (unsigned int)len, 0); /* (TEST/DEBUG) */
2024 #endif
2025 s = splhigh();
2026
2027 assert(((dst & PAGE_MASK)+dst_offset+len) <= PAGE_SIZE);
2028 assert(((src & PAGE_MASK)+src_offset+len) <= PAGE_SIZE);
2029
2030 /*
2031 * Since the source and destination are physical addresses,
2032 * turn off data translation to perform a bcopy() in bcopy_phys().
2033 */
2034 phys_copy((vm_offset_t) src+src_offset,
2035 (vm_offset_t) dst+dst_offset, len);
2036
2037 splx(s);
2038 }
2039
2040 void
2041 pmap_zero_part_page(
2042 vm_offset_t p,
2043 vm_offset_t offset,
2044 vm_size_t len)
2045 {
2046 panic("pmap_zero_part_page");
2047 }
2048
2049 boolean_t pmap_verify_free(vm_offset_t pa) {
2050
2051 struct phys_entry *pp;
2052
2053 #if PMAP_LOWTRACE
2054 dbgTrace(0xF1D00007, (unsigned int)pa, 0); /* (TEST/DEBUG) */
2055 #endif
2056
2057 #if DEBUG
2058 if (pmdebug & PDB_USER)
2059 kprintf("pmap_verify_free(pa=%x)\n", pa);
2060 #endif
2061
2062 if (!pmap_initialized) return(TRUE);
2063
2064 pp = pmap_find_physentry(pa); /* Look up the physical entry */
2065 if (pp == PHYS_NULL) return FALSE; /* If there isn't one, show no mapping... */
2066 return ((mapping *)((unsigned int)pp->phys_link & ~PHYS_FLAGS) == MAPPING_NULL); /* Otherwise, return TRUE if mapping exists... */
2067 }
2068
2069
2070 /* Determine if we need to switch space and set up for it if so */
2071
2072 void pmap_switch(pmap_t map)
2073 {
2074 unsigned int i;
2075
2076 #if DEBUG
2077 if (watchacts & WA_PCB) {
2078 kprintf("Switching to map at 0x%08x, space=%d\n",
2079 map,map->space);
2080 }
2081 #endif /* DEBUG */
2082
2083
2084 /* when changing to kernel space, don't bother
2085 * doing anything, the kernel is mapped from here already.
2086 */
2087 if (map->space == PPC_SID_KERNEL) { /* Are we switching into kernel space? */
2088 return; /* If so, we don't do anything... */
2089 }
2090
2091 hw_set_user_space(map); /* Indicate if we need to load the SRs or not */
2092 return; /* Bye, bye, butterfly... */
2093 }
2094
2095 /*
2096 * kern_return_t pmap_nest(grand, subord, vaddr, size)
2097 *
2098 * grand = the pmap that we will nest subord into
2099 * subord = the pmap that goes into the grand
2100 * vaddr = start of range in pmap to be inserted
2101 * size = size of range in pmap to be inserted
2102 *
2103 * Inserts a pmap into another. This is used to implement shared segments.
2104 * On the current PPC processors, this is limited to segment (256MB) aligned
2105 * segment sized ranges.
2106 */
2107
2108 kern_return_t pmap_nest(pmap_t grand, pmap_t subord, vm_offset_t vaddr, vm_size_t size) {
2109
2110 unsigned int oflags, seg, grandr;
2111 int i;
2112
2113 if(size != 0x10000000) return KERN_INVALID_VALUE; /* We can only do this for 256MB for now */
2114 if(vaddr & 0x0FFFFFFF) return KERN_INVALID_VALUE; /* We can only do this aligned to 256MB */
2115
2116 while(1) { /* Test and set the subordinate flag */
2117 oflags = subord->vflags & ~pmapAltSeg; /* Get old unset value */
2118 if(subord->vflags & pmapAltSeg) { /* Are trying to nest one already nested? */
2119 panic("pmap_nest: Attempt to nest an already nested pmap\n");
2120 }
2121 if(hw_compare_and_store(oflags, oflags | pmapSubord, &subord->vflags)) break; /* Done if we got it set */
2122 }
2123
2124 simple_lock(&grand->lock); /* Lock the superior pmap */
2125
2126 if(grand->vflags & pmapSubord) { /* Are we only one level deep? */
2127 simple_unlock(&grand->lock); /* Unlock the superior pmap */
2128 panic("pmap_nest: Attempt to nest into subordinate pmap\n");
2129 return KERN_FAILURE; /* Shame on you */
2130 }
2131
2132 seg = vaddr >> 28; /* Isolate the segment number */
2133 if((0x00008000 >> seg) & grand->vflags) { /* See if it is already in use */
2134 simple_unlock(&grand->lock); /* Unlock the superior pmap */
2135 panic("pmap_nest: Attempt to nest into already nested segment\n");
2136 return KERN_FAILURE; /* Shame on you */
2137 }
2138
2139 grand->pmapPmaps[seg] = subord; /* Set the pointer to the subordinate */
2140 grand->pmapSegs[seg] = SEG_REG_PROT | (seg << 20) | subord->space; /* Set the vsid to the subordinate's vsid */
2141 grand->vflags |= (0x00008000 >> seg); /* Set in-use bit */
2142
2143 grandr = (unsigned int)grand ^ grand->pmapvr; /* Get real address of the grand pmap */
2144
2145 simple_unlock(&grand->lock); /* Unlock the grand pmap */
2146
2147
2148 /*
2149 * Note that the following will force the segment registers to be reloaded following
2150 * the next interrupt on all processors if they are using the pmap we just changed.
2151 *
2152 */
2153
2154
2155 for(i=0; i < real_ncpus; i++) { /* Cycle through processors */
2156 (void)hw_compare_and_store((unsigned int)grandr, 0, &per_proc_info[i].Lastpmap); /* Clear if ours */
2157 }
2158
2159 return KERN_SUCCESS; /* Bye, bye, butterfly... */
2160 }
2161
2162
2163 /*
2164 * kern_return_t pmap_unnest(grand, vaddr, size)
2165 *
2166 * grand = the pmap that we will nest subord into
2167 * vaddr = start of range in pmap to be inserted
2168 * size = size of range in pmap to be inserted
2169 *
2170 * Removes a pmap from another. This is used to implement shared segments.
2171 * On the current PPC processors, this is limited to segment (256MB) aligned
2172 * segment sized ranges.
2173 */
2174
2175 kern_return_t pmap_unnest(pmap_t grand, vm_offset_t vaddr, vm_size_t size) {
2176
2177 unsigned int oflags, seg, grandr, tstamp;
2178 int i, tcpu, mycpu;
2179
2180 if(size != 0x10000000) return KERN_INVALID_VALUE; /* We can only do this for 256MB for now */
2181 if(vaddr & 0x0FFFFFFF) return KERN_INVALID_VALUE; /* We can only do this aligned to 256MB */
2182
2183 simple_lock(&grand->lock); /* Lock the superior pmap */
2184 disable_preemption(); /* It's all for me! */
2185
2186 seg = vaddr >> 28; /* Isolate the segment number */
2187 if(!((0x00008000 >> seg) & grand->vflags)) { /* See if it is already in use */
2188 enable_preemption(); /* Ok, your turn */
2189 simple_unlock(&grand->lock); /* Unlock the superior pmap */
2190 panic("pmap_unnest: Attempt to unnest an unnested segment\n");
2191 return KERN_FAILURE; /* Shame on you */
2192 }
2193
2194 grand->pmapPmaps[seg] = (pmap_t)0; /* Clear the pointer to the subordinate */
2195 grand->pmapSegs[seg] = grand->space; /* Set the pointer to the subordinate's vsid */
2196 grand->pmapSegs[seg] = SEG_REG_PROT | (seg << 20) | grand->space; /* Set the vsid to the grand's vsid */
2197 grand->vflags &= ~(0x00008000 >> seg); /* Clear in-use bit */
2198
2199 grandr = (unsigned int)grand ^ grand->pmapvr; /* Get real address of the grand pmap */
2200
2201 simple_unlock(&grand->lock); /* Unlock the superior pmap */
2202
2203 /*
2204 * Note that the following will force the segment registers to be reloaded
2205 * on all processors (if they are using the pmap we just changed) before returning.
2206 *
2207 * This is needed. The reason is that until the segment register is
2208 * reloaded, another thread in the same task on a different processor will
2209 * be able to access memory that it isn't allowed to anymore. That can happen
2210 * because access to the subordinate pmap is being removed, but the pmap is still
2211 * valid.
2212 *
2213 * Note that we only kick the other processor if we see that it was using the pmap while we
2214 * were changing it.
2215 */
2216
2217
2218 mycpu = cpu_number(); /* Who am I? Am I just a dream? */
2219 for(i=0; i < real_ncpus; i++) { /* Cycle through processors */
2220 if(hw_compare_and_store((unsigned int)grandr, 0, &per_proc_info[i].Lastpmap)) { /* Clear if ours and kick the other guy if he was using it */
2221 if(i == mycpu) continue; /* Don't diddle ourselves */
2222 tstamp = per_proc_info[i].ruptStamp[1]; /* Save the processor's last interrupt time stamp */
2223 if(cpu_signal(i, SIGPwake, 0, 0) != KERN_SUCCESS) { /* Make sure we see the pmap change */
2224 continue;
2225 }
2226 if(!hw_cpu_wcng(&per_proc_info[i].ruptStamp[1], tstamp, LockTimeOut)) { /* Wait for the other processors to enter debug */
2227 panic("pmap_unnest: Other processor (%d) did not see interruption request\n", i);
2228 }
2229 }
2230 }
2231
2232 enable_preemption(); /* Others can run now */
2233 return KERN_SUCCESS; /* Bye, bye, butterfly... */
2234 }
2235
2236
2237 void pmap_ver(pmap_t pmap, vm_offset_t sva, vm_offset_t eva) {
2238
2239 int cnt, i, j, k;
2240 vm_offset_t xx;
2241
2242 if(!pmap) return;
2243
2244 sva = trunc_page(sva);
2245 eva = trunc_page(eva);
2246
2247 for(i = 0; i < (pmapUsageMask + 1); i++) { /* Step through them all */
2248 if((pmap->pmapUsage[i]) > 8192) { /* See if this is a sane number */
2249 panic("pmap_ver: pmapUsage slot for %08X has invalid count (%d) for pmap %08X\n",
2250 i * pmapUsageSize, pmap->pmapUsage[i], pmap);
2251 }
2252 }
2253 j = 0;
2254 while(1) { /* Try multiple times */
2255 cnt = 0;
2256 for(i = 0; i < (pmapUsageMask + 1); i++) { /* Step through them all */
2257 cnt = cnt + pmap->pmapUsage[i]; /* Sum all slots */
2258 }
2259 if(cnt == pmap->stats.resident_count) break; /* We're ok if we match... */
2260
2261 j++;
2262 for(i = 0; i < 100000; i++) {
2263 k = j + i;
2264 }
2265 if(j >= 10) {
2266 panic("pmap_ver: pmapUsage total (%d) does not match resident count (%d) for pmap %08X\n",
2267 cnt, pmap->stats.resident_count, pmap);
2268 }
2269 }
2270
2271 for(xx = sva; xx < eva; xx += PAGE_SIZE) { /* See if any slots not clear */
2272 if(pmap_extract(pmap, xx)) {
2273 panic("pmap_ver: range (%08X to %08X) not empty at %08X for pmap %08X\n",
2274 sva, eva, xx, pmap);
2275 }
2276 }
2277 }
2278
2279
2280 /* temporary workaround */
2281 boolean_t
2282 coredumpok(vm_map_t map, vm_offset_t va)
2283 {
2284 return TRUE;
2285 }
2286