]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/pmap.c
xnu-517.3.15.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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1990,1991,1992 The University of Utah and
31 * the Center for Software Science (CSS).
32 * Copyright (c) 1991,1987 Carnegie Mellon University.
33 * All rights reserved.
34 *
35 * Permission to use, copy, modify and distribute this software and its
36 * documentation is hereby granted, provided that both the copyright
37 * notice and this permission notice appear in all copies of the
38 * software, derivative works or modified versions, and any portions
39 * thereof, and that both notices appear in supporting documentation,
40 * and that all advertising materials mentioning features or use of
41 * this software display the following acknowledgement: ``This product
42 * includes software developed by the Center for Software Science at
43 * the University of Utah.''
44 *
45 * CARNEGIE MELLON, THE UNIVERSITY OF UTAH AND CSS ALLOW FREE USE OF
46 * THIS SOFTWARE IN ITS "AS IS" CONDITION, AND DISCLAIM ANY LIABILITY
47 * OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF
48 * THIS SOFTWARE.
49 *
50 * CSS requests users of this software to return to css-dist@cs.utah.edu any
51 * improvements that they make and grant CSS redistribution rights.
52 *
53 * Carnegie Mellon requests users of this software to return to
54 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55 * School of Computer Science
56 * Carnegie Mellon University
57 * Pittsburgh PA 15213-3890
58 * any improvements or extensions that they make and grant Carnegie Mellon
59 * the rights to redistribute these changes.
60 *
61 * Utah $Hdr: pmap.c 1.28 92/06/23$
62 * Author: Mike Hibler, Bob Wheeler, University of Utah CSS, 10/90
63 */
64
65 /*
66 * Manages physical address maps for powerpc.
67 *
68 * In addition to hardware address maps, this
69 * module is called upon to provide software-use-only
70 * maps which may or may not be stored in the same
71 * form as hardware maps. These pseudo-maps are
72 * used to store intermediate results from copy
73 * operations to and from address spaces.
74 *
75 * Since the information managed by this module is
76 * also stored by the logical address mapping module,
77 * this module may throw away valid virtual-to-physical
78 * mappings at almost any time. However, invalidations
79 * of virtual-to-physical mappings must be done as
80 * requested.
81 *
82 * In order to cope with hardware architectures which
83 * make virtual-to-physical map invalidates expensive,
84 * this module may delay invalidate or reduced protection
85 * operations until such time as they are actually
86 * necessary. This module is given full information to
87 * when physical maps must be made correct.
88 *
89 */
90
91 #include <zone_debug.h>
92 #include <cpus.h>
93 #include <debug.h>
94 #include <mach_kgdb.h>
95 #include <mach_vm_debug.h>
96 #include <db_machine_commands.h>
97
98 #include <kern/thread.h>
99 #include <kern/simple_lock.h>
100 #include <mach/vm_attributes.h>
101 #include <mach/vm_param.h>
102 #include <vm/vm_kern.h>
103 #include <kern/spl.h>
104
105 #include <kern/misc_protos.h>
106 #include <ppc/misc_protos.h>
107 #include <ppc/proc_reg.h>
108
109 #include <vm/pmap.h>
110 #include <vm/vm_map.h>
111 #include <vm/vm_page.h>
112
113 #include <ppc/pmap.h>
114 #include <ppc/mem.h>
115 #include <ppc/mappings.h>
116
117 #include <ppc/new_screen.h>
118 #include <ppc/Firmware.h>
119 #include <ppc/savearea.h>
120 #include <ppc/exception.h>
121 #include <ppc/low_trace.h>
122 #include <ddb/db_output.h>
123
124 extern unsigned int avail_remaining;
125 extern unsigned int mappingdeb0;
126 extern struct Saveanchor saveanchor; /* Aliged savearea anchor */
127 extern int real_ncpus; /* Number of actual CPUs */
128 unsigned int debugbackpocket; /* (TEST/DEBUG) */
129
130 vm_offset_t first_free_virt;
131 int current_free_region; /* Used in pmap_next_page */
132
133 pmapTransTab *pmapTrans; /* Point to the hash to pmap translations */
134 struct phys_entry *phys_table;
135
136 /* forward */
137 void pmap_activate(pmap_t pmap, thread_t th, int which_cpu);
138 void pmap_deactivate(pmap_t pmap, thread_t th, int which_cpu);
139 void copy_to_phys(vm_offset_t sva, vm_offset_t dpa, int bytecount);
140
141 #if MACH_VM_DEBUG
142 int pmap_list_resident_pages(pmap_t pmap, vm_offset_t *listp, int space);
143 #endif
144
145 /* NOTE: kernel_pmap_store must be in V=R storage and aligned!!!!!!!!!!!!!! */
146
147 extern struct pmap kernel_pmap_store;
148 pmap_t kernel_pmap; /* Pointer to kernel pmap and anchor for in-use pmaps */
149 addr64_t kernel_pmap_phys; /* Pointer to kernel pmap and anchor for in-use pmaps, physical address */
150 pmap_t cursor_pmap; /* Pointer to last pmap allocated or previous if removed from in-use list */
151 pmap_t sharedPmap; /* Pointer to common pmap for 64-bit address spaces */
152 struct zone *pmap_zone; /* zone of pmap structures */
153 boolean_t pmap_initialized = FALSE;
154
155 int ppc_max_pmaps; /* Maximum number of concurrent address spaces allowed. This is machine dependent */
156 addr64_t vm_max_address; /* Maximum effective address supported */
157 addr64_t vm_max_physical; /* Maximum physical address supported */
158
159 /*
160 * Physical-to-virtual translations are handled by inverted page table
161 * structures, phys_tables. Multiple mappings of a single page are handled
162 * by linking the affected mapping structures. We initialise one region
163 * for phys_tables of the physical memory we know about, but more may be
164 * added as it is discovered (eg. by drivers).
165 */
166
167 /*
168 * free pmap list. caches the first free_pmap_max pmaps that are freed up
169 */
170 int free_pmap_max = 32;
171 int free_pmap_count;
172 pmap_t free_pmap_list;
173 decl_simple_lock_data(,free_pmap_lock)
174
175 /*
176 * Function to get index into phys_table for a given physical address
177 */
178
179 struct phys_entry *pmap_find_physentry(ppnum_t pa)
180 {
181 int i;
182 unsigned int entry;
183
184 for (i = pmap_mem_regions_count - 1; i >= 0; i--) {
185 if (pa < pmap_mem_regions[i].mrStart) continue; /* See if we fit in this region */
186 if (pa > pmap_mem_regions[i].mrEnd) continue; /* Check the end too */
187
188 entry = (unsigned int)pmap_mem_regions[i].mrPhysTab + ((pa - pmap_mem_regions[i].mrStart) * sizeof(phys_entry));
189 return (struct phys_entry *)entry;
190 }
191 // kprintf("DEBUG - pmap_find_physentry: page 0x%08X not found\n", pa);
192 return 0;
193 }
194
195 /*
196 * kern_return_t
197 * pmap_add_physical_memory(vm_offset_t spa, vm_offset_t epa,
198 * boolean_t available, unsigned int attr)
199 *
200 * THIS IS NOT SUPPORTED
201 */
202 kern_return_t pmap_add_physical_memory(vm_offset_t spa, vm_offset_t epa,
203 boolean_t available, unsigned int attr)
204 {
205
206 panic("Forget it! You can't map no more memory, you greedy puke!\n");
207 return KERN_SUCCESS;
208 }
209
210 /*
211 * pmap_map(va, spa, epa, prot)
212 * is called during boot to map memory in the kernel's address map.
213 * A virtual address range starting at "va" is mapped to the physical
214 * address range "spa" to "epa" with machine independent protection
215 * "prot".
216 *
217 * "va", "spa", and "epa" are byte addresses and must be on machine
218 * independent page boundaries.
219 *
220 * Pages with a contiguous virtual address range, the same protection, and attributes.
221 * therefore, we map it with a single block.
222 *
223 * Note that this call will only map into 32-bit space
224 *
225 */
226
227 vm_offset_t
228 pmap_map(
229 vm_offset_t va,
230 vm_offset_t spa,
231 vm_offset_t epa,
232 vm_prot_t prot)
233 {
234
235 addr64_t colladr;
236
237 if (spa == epa) return(va);
238
239 assert(epa > spa);
240
241 colladr = mapping_make(kernel_pmap, (addr64_t)va, (ppnum_t)(spa >> 12), (mmFlgBlock | mmFlgPerm), (epa - spa) >> 12, prot & VM_PROT_ALL);
242
243 if(colladr) { /* Was something already mapped in the range? */
244 panic("pmap_map: attempt to map previously mapped range - va = %08X, pa = %08X, epa = %08X, collision = %016llX\n",
245 va, spa, epa, colladr);
246 }
247 return(va);
248 }
249
250 /*
251 * Bootstrap the system enough to run with virtual memory.
252 * Map the kernel's code and data, and allocate the system page table.
253 * Called with mapping done by BATs. Page_size must already be set.
254 *
255 * Parameters:
256 * msize: Total memory present
257 * first_avail: First virtual address available
258 * kmapsize: Size of kernel text and data
259 */
260 void
261 pmap_bootstrap(uint64_t msize, vm_offset_t *first_avail, unsigned int kmapsize)
262 {
263 register struct mapping *mp;
264 vm_offset_t addr;
265 vm_size_t size;
266 int i, num, j, rsize, mapsize, vmpagesz, vmmapsz, bank, nbits;
267 uint64_t tmemsize;
268 uint_t htslop;
269 vm_offset_t first_used_addr, PCAsize;
270 struct phys_entry *phys_table;
271
272 *first_avail = round_page_32(*first_avail); /* Make sure we start out on a page boundary */
273 vm_last_addr = VM_MAX_KERNEL_ADDRESS; /* Set the highest address know to VM */
274
275 /*
276 * Initialize kernel pmap
277 */
278 kernel_pmap = &kernel_pmap_store;
279 kernel_pmap_phys = (addr64_t)&kernel_pmap_store;
280 cursor_pmap = &kernel_pmap_store;
281
282 simple_lock_init(&kernel_pmap->lock, ETAP_VM_PMAP_KERNEL);
283
284 kernel_pmap->pmap_link.next = (queue_t)kernel_pmap; /* Set up anchor forward */
285 kernel_pmap->pmap_link.prev = (queue_t)kernel_pmap; /* Set up anchor reverse */
286 kernel_pmap->ref_count = 1;
287 kernel_pmap->pmapFlags = pmapKeyDef; /* Set the default keys */
288 kernel_pmap->pmapCCtl = pmapCCtlVal; /* Initialize cache control */
289 kernel_pmap->space = PPC_SID_KERNEL;
290 kernel_pmap->pmapvr = 0; /* Virtual = Real */
291
292 /*
293 * The hash table wants to have one pteg for every 2 physical pages.
294 * We will allocate this in physical RAM, outside of kernel virtual memory,
295 * at the top of the highest bank that will contain it.
296 * Note that "bank" doesn't refer to a physical memory slot here, it is a range of
297 * physically contiguous memory.
298 *
299 * The PCA will go there as well, immediately before the hash table.
300 */
301
302 nbits = cntlzw(((msize << 1) - 1) >> 32); /* Get first bit in upper half */
303 if(nbits == 32) nbits = nbits + cntlzw((uint_t)((msize << 1) - 1)); /* If upper half was empty, find bit in bottom half */
304 tmemsize = 0x8000000000000000ULL >> nbits; /* Get memory size rounded up to power of 2 */
305
306 if(tmemsize > 0x0000002000000000ULL) tmemsize = 0x0000002000000000ULL; /* Make sure we don't make an unsupported hash table size */
307
308 hash_table_size = (uint_t)(tmemsize >> 13) * per_proc_info[0].pf.pfPTEG; /* Get provisional hash_table_size */
309 if(hash_table_size < (256 * 1024)) hash_table_size = (256 * 1024); /* Make sure we are at least minimum size */
310
311 while(1) { /* Try to fit hash table in PCA into contiguous memory */
312
313 if(hash_table_size < (256 * 1024)) { /* Have we dropped too short? This should never, ever happen */
314 panic("pmap_bootstrap: Can't find space for hash table\n"); /* This will never print, system isn't up far enough... */
315 }
316
317 PCAsize = (hash_table_size / per_proc_info[0].pf.pfPTEG) * sizeof(PCA); /* Get total size of PCA table */
318 PCAsize = round_page_32(PCAsize); /* Make sure it is at least a page long */
319
320 for(bank = pmap_mem_regions_count - 1; bank >= 0; bank--) { /* Search backwards through banks */
321
322 hash_table_base = ((addr64_t)pmap_mem_regions[bank].mrEnd << 12) - hash_table_size + PAGE_SIZE; /* Get tenative address */
323
324 htslop = hash_table_base & (hash_table_size - 1); /* Get the extra that we will round down when we align */
325 hash_table_base = hash_table_base & -(addr64_t)hash_table_size; /* Round down to correct boundary */
326
327 if((hash_table_base - round_page_32(PCAsize)) >= ((addr64_t)pmap_mem_regions[bank].mrStart << 12)) break; /* Leave if we fit */
328 }
329
330 if(bank >= 0) break; /* We are done if we found a suitable bank */
331
332 hash_table_size = hash_table_size >> 1; /* Try the next size down */
333 }
334
335 if(htslop) { /* If there was slop (i.e., wasted pages for alignment) add a new region */
336 for(i = pmap_mem_regions_count - 1; i >= bank; i--) { /* Copy from end to our bank, including our bank */
337 pmap_mem_regions[i + 1].mrStart = pmap_mem_regions[i].mrStart; /* Set the start of the bank */
338 pmap_mem_regions[i + 1].mrAStart = pmap_mem_regions[i].mrAStart; /* Set the start of allocatable area */
339 pmap_mem_regions[i + 1].mrEnd = pmap_mem_regions[i].mrEnd; /* Set the end address of bank */
340 pmap_mem_regions[i + 1].mrAEnd = pmap_mem_regions[i].mrAEnd; /* Set the end address of allocatable area */
341 }
342
343 pmap_mem_regions[i + 1].mrStart = (hash_table_base + hash_table_size) >> 12; /* Set the start of the next bank to the start of the slop area */
344 pmap_mem_regions[i + 1].mrAStart = (hash_table_base + hash_table_size) >> 12; /* Set the start of allocatable area to the start of the slop area */
345 pmap_mem_regions[i].mrEnd = (hash_table_base + hash_table_size - 4096) >> 12; /* Set the end of our bank to the end of the hash table */
346
347 }
348
349 pmap_mem_regions[bank].mrAEnd = (hash_table_base - PCAsize - 4096) >> 12; /* Set the maximum allocatable in this bank */
350
351 hw_hash_init(); /* Initiaize the hash table and PCA */
352 hw_setup_trans(); /* Set up hardware registers needed for translation */
353
354 /*
355 * The hash table is now all initialized and so is the PCA. Go on to do the rest of it.
356 * This allocation is from the bottom up.
357 */
358
359 num = atop_64(msize); /* Get number of pages in all of memory */
360
361 /* Figure out how much we need to allocate */
362
363 size = (vm_size_t) (
364 (InitialSaveBloks * PAGE_SIZE) + /* Allow space for the initial context saveareas */
365 (BackPocketSaveBloks * PAGE_SIZE) + /* For backpocket saveareas */
366 trcWork.traceSize + /* Size of trace table */
367 ((((1 << maxAdrSpb) * sizeof(pmapTransTab)) + 4095) & -4096) + /* Size of pmap translate table */
368 (((num * sizeof(struct phys_entry)) + 4095) & -4096) /* For the physical entries */
369 );
370
371 mapsize = size = round_page_32(size); /* Get size of area to map that we just calculated */
372 mapsize = mapsize + kmapsize; /* Account for the kernel text size */
373
374 vmpagesz = round_page_32(num * sizeof(struct vm_page)); /* Allow for all vm_pages needed to map physical mem */
375 vmmapsz = round_page_32((num / 8) * sizeof(struct vm_map_entry)); /* Allow for vm_maps */
376
377 mapsize = mapsize + vmpagesz + vmmapsz; /* Add the VM system estimates into the grand total */
378
379 mapsize = mapsize + (4 * 1024 * 1024); /* Allow for 4 meg of extra mappings */
380 mapsize = ((mapsize / PAGE_SIZE) + MAPPERBLOK - 1) / MAPPERBLOK; /* Get number of blocks of mappings we need */
381 mapsize = mapsize + ((mapsize + MAPPERBLOK - 1) / MAPPERBLOK); /* Account for the mappings themselves */
382
383 size = size + (mapsize * PAGE_SIZE); /* Get the true size we need */
384
385 /* hash table must be aligned to its size */
386
387 addr = *first_avail; /* Set the address to start allocations */
388 first_used_addr = addr; /* Remember where we started */
389
390 bzero((char *)addr, size); /* Clear everything that we are allocating */
391
392 savearea_init(addr); /* Initialize the savearea chains and data */
393
394 addr = (vm_offset_t)((unsigned int)addr + ((InitialSaveBloks + BackPocketSaveBloks) * PAGE_SIZE)); /* Point past saveareas */
395
396 trcWork.traceCurr = (unsigned int)addr; /* Set first trace slot to use */
397 trcWork.traceStart = (unsigned int)addr; /* Set start of trace table */
398 trcWork.traceEnd = (unsigned int)addr + trcWork.traceSize; /* Set end of trace table */
399
400 addr = (vm_offset_t)trcWork.traceEnd; /* Set next allocatable location */
401
402 pmapTrans = (pmapTransTab *)addr; /* Point to the pmap to hash translation table */
403
404 pmapTrans[PPC_SID_KERNEL].pmapPAddr = (addr64_t)((uintptr_t)kernel_pmap); /* Initialize the kernel pmap in the translate table */
405 pmapTrans[PPC_SID_KERNEL].pmapVAddr = CAST_DOWN(unsigned int, kernel_pmap); /* Initialize the kernel pmap in the translate table */
406
407 addr += ((((1 << maxAdrSpb) * sizeof(pmapTransTab)) + 4095) & -4096); /* Point past pmap translate table */
408
409 /* NOTE: the phys_table must be within the first 2GB of physical RAM. This makes sure we only need to do 32-bit arithmetic */
410
411 phys_table = (struct phys_entry *) addr; /* Get pointer to physical table */
412
413 for (bank = 0; bank < pmap_mem_regions_count; bank++) { /* Set pointer and initialize all banks of ram */
414
415 pmap_mem_regions[bank].mrPhysTab = phys_table; /* Set pointer to the physical table for this bank */
416
417 phys_table = phys_table + (pmap_mem_regions[bank].mrEnd - pmap_mem_regions[bank].mrStart + 1); /* Point to the next */
418 }
419
420 addr += (((num * sizeof(struct phys_entry)) + 4095) & -4096); /* Step on past the physical entries */
421
422 /*
423 * Remaining space is for mapping entries. Tell the initializer routine that
424 * the mapping system can't release this block because it's permanently assigned
425 */
426
427 mapping_init(); /* Initialize the mapping tables */
428
429 for(i = addr; i < first_used_addr + size; i += PAGE_SIZE) { /* Add initial mapping blocks */
430 mapping_free_init(i, 1, 0); /* Pass block address and say that this one is not releasable */
431 }
432 mapCtl.mapcmin = MAPPERBLOK; /* Make sure we only adjust one at a time */
433
434 /* Map V=R the page tables */
435 pmap_map(first_used_addr, first_used_addr,
436 round_page_32(first_used_addr + size), VM_PROT_READ | VM_PROT_WRITE);
437
438 *first_avail = round_page_32(first_used_addr + size); /* Set next available page */
439 first_free_virt = *first_avail; /* Ditto */
440
441 /* All the rest of memory is free - add it to the free
442 * regions so that it can be allocated by pmap_steal
443 */
444
445 pmap_mem_regions[0].mrAStart = (*first_avail >> 12); /* Set up the free area to start allocations (always in the first bank) */
446
447 current_free_region = 0; /* Set that we will start allocating in bank 0 */
448 avail_remaining = 0; /* Clear free page count */
449 for(bank = 0; bank < pmap_mem_regions_count; bank++) { /* Total up all of the pages in the system that are available */
450 avail_remaining += (pmap_mem_regions[bank].mrAEnd - pmap_mem_regions[bank].mrAStart) + 1; /* Add in allocatable pages in this bank */
451 }
452
453
454 }
455
456 /*
457 * pmap_init(spa, epa)
458 * finishes the initialization of the pmap module.
459 * This procedure is called from vm_mem_init() in vm/vm_init.c
460 * to initialize any remaining data structures that the pmap module
461 * needs to map virtual memory (VM is already ON).
462 *
463 * Note that the pmap needs to be sized and aligned to
464 * a power of two. This is because it is used both in virtual and
465 * real so it can't span a page boundary.
466 */
467
468 void
469 pmap_init(void)
470 {
471
472 addr64_t cva;
473
474 pmap_zone = zinit(pmapSize, 400 * pmapSize, 4096, "pmap");
475 #if ZONE_DEBUG
476 zone_debug_disable(pmap_zone); /* Can't debug this one 'cause it messes with size and alignment */
477 #endif /* ZONE_DEBUG */
478
479 pmap_initialized = TRUE;
480
481 /*
482 * Initialize list of freed up pmaps
483 */
484 free_pmap_list = 0; /* Set that there are no free pmaps */
485 free_pmap_count = 0;
486 simple_lock_init(&free_pmap_lock, ETAP_VM_PMAP_CACHE);
487
488 }
489
490 unsigned int pmap_free_pages(void)
491 {
492 return avail_remaining;
493 }
494
495 /*
496 * This function allocates physical pages.
497 */
498
499 /* Non-optimal, but only used for virtual memory startup.
500 * Allocate memory from a table of free physical addresses
501 * If there are no more free entries, too bad.
502 */
503
504 boolean_t pmap_next_page(ppnum_t *addrp)
505 {
506 int i;
507
508 if(current_free_region >= pmap_mem_regions_count) return FALSE; /* Return failure if we have used everything... */
509
510 for(i = current_free_region; i < pmap_mem_regions_count; i++) { /* Find the next bank with free pages */
511 if(pmap_mem_regions[i].mrAStart <= pmap_mem_regions[i].mrAEnd) break; /* Found one */
512 }
513
514 current_free_region = i; /* Set our current bank */
515 if(i >= pmap_mem_regions_count) return FALSE; /* Couldn't find a free page */
516
517 *addrp = pmap_mem_regions[i].mrAStart; /* Allocate the page */
518 pmap_mem_regions[i].mrAStart = pmap_mem_regions[i].mrAStart + 1; /* Set the next one to go */
519 avail_remaining--; /* Drop free count */
520
521 return TRUE;
522 }
523
524 void pmap_virtual_space(
525 vm_offset_t *startp,
526 vm_offset_t *endp)
527 {
528 *startp = round_page_32(first_free_virt);
529 *endp = vm_last_addr;
530 }
531
532 /*
533 * pmap_create
534 *
535 * Create and return a physical map.
536 *
537 * If the size specified for the map is zero, the map is an actual physical
538 * map, and may be referenced by the hardware.
539 *
540 * A pmap is either in the free list or in the in-use list. The only use
541 * of the in-use list (aside from debugging) is to handle the VSID wrap situation.
542 * Whenever a new pmap is allocated (i.e., not recovered from the free list). The
543 * in-use list is matched until a hole in the VSID sequence is found. (Note
544 * that the in-use pmaps are queued in VSID sequence order.) This is all done
545 * while free_pmap_lock is held.
546 *
547 * If the size specified is non-zero, the map will be used in software
548 * only, and is bounded by that size.
549 */
550 pmap_t
551 pmap_create(vm_size_t size)
552 {
553 pmap_t pmap, ckpmap, fore, aft;
554 int s, i;
555 unsigned int currSID, hspace;
556 addr64_t physpmap;
557
558 /*
559 * A software use-only map doesn't even need a pmap structure.
560 */
561 if (size)
562 return(PMAP_NULL);
563
564 /*
565 * If there is a pmap in the pmap free list, reuse it.
566 * Note that we use free_pmap_list for all chaining of pmaps, both to
567 * the free list and the in use chain (anchored from kernel_pmap).
568 */
569 s = splhigh();
570 simple_lock(&free_pmap_lock);
571
572 if(free_pmap_list) { /* Any free? */
573 pmap = free_pmap_list; /* Yes, allocate it */
574 free_pmap_list = (pmap_t)pmap->freepmap; /* Dequeue this one (we chain free ones through freepmap) */
575 free_pmap_count--;
576 }
577 else {
578 simple_unlock(&free_pmap_lock); /* Unlock just in case */
579 splx(s);
580
581 pmap = (pmap_t) zalloc(pmap_zone); /* Get one */
582 if (pmap == PMAP_NULL) return(PMAP_NULL); /* Handle out-of-memory condition */
583
584 bzero((char *)pmap, pmapSize); /* Clean up the pmap */
585
586 s = splhigh();
587 simple_lock(&free_pmap_lock); /* Lock it back up */
588
589 ckpmap = cursor_pmap; /* Get starting point for free ID search */
590 currSID = ckpmap->spaceNum; /* Get the actual space ID number */
591
592 while(1) { /* Keep trying until something happens */
593
594 currSID = (currSID + 1) & (maxAdrSp - 1); /* Get the next in the sequence */
595 if(((currSID * incrVSID) & (maxAdrSp - 1)) == invalSpace) continue; /* Skip the space we have reserved */
596 ckpmap = (pmap_t)ckpmap->pmap_link.next; /* On to the next in-use pmap */
597
598 if(ckpmap->spaceNum != currSID) break; /* If we are out of sequence, this is free */
599
600 if(ckpmap == cursor_pmap) { /* See if we have 2^20 already allocated */
601 panic("pmap_create: Maximum number (%d) active address spaces reached\n", maxAdrSp); /* Die pig dog */
602 }
603 }
604
605 pmap->space = (currSID * incrVSID) & (maxAdrSp - 1); /* Calculate the actual VSID */
606 pmap->spaceNum = currSID; /* Set the space ID number */
607 /*
608 * Now we link into the chain just before the out of sequence guy.
609 */
610
611 fore = (pmap_t)ckpmap->pmap_link.prev; /* Get the current's previous */
612 pmap->pmap_link.next = (queue_t)ckpmap; /* My next points to the current */
613 fore->pmap_link.next = (queue_t)pmap; /* Current's previous's next points to me */
614 pmap->pmap_link.prev = (queue_t)fore; /* My prev points to what the current pointed to */
615 ckpmap->pmap_link.prev = (queue_t)pmap; /* Current's prev points to me */
616
617 simple_lock_init(&pmap->lock, ETAP_VM_PMAP);
618
619 physpmap = ((addr64_t)pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)pmap)) << 12) | (addr64_t)((unsigned int)pmap & 0xFFF); /* Get the physical address of the pmap */
620
621 pmap->pmapvr = (addr64_t)((uintptr_t)pmap) ^ physpmap; /* Make V to R translation mask */
622
623 pmapTrans[pmap->space].pmapPAddr = physpmap; /* Set translate table physical to point to us */
624 pmapTrans[pmap->space].pmapVAddr = CAST_DOWN(unsigned int, pmap); /* Set translate table virtual to point to us */
625 }
626
627 pmap->pmapFlags = pmapKeyDef; /* Set default key */
628 pmap->pmapCCtl = pmapCCtlVal; /* Initialize cache control */
629 pmap->ref_count = 1;
630 pmap->stats.resident_count = 0;
631 pmap->stats.wired_count = 0;
632 pmap->pmapSCSubTag = 0x0000000000000000ULL; /* Make sure this is clean an tidy */
633 simple_unlock(&free_pmap_lock);
634
635 splx(s);
636 return(pmap);
637 }
638
639 /*
640 * pmap_destroy
641 *
642 * Gives up a reference to the specified pmap. When the reference count
643 * reaches zero the pmap structure is added to the pmap free list.
644 *
645 * Should only be called if the map contains no valid mappings.
646 */
647 void
648 pmap_destroy(pmap_t pmap)
649 {
650 int ref_count;
651 spl_t s;
652 pmap_t fore, aft;
653
654 if (pmap == PMAP_NULL)
655 return;
656
657 ref_count=hw_atomic_sub(&pmap->ref_count, 1); /* Back off the count */
658 if(ref_count>0) return; /* Still more users, leave now... */
659
660 if(ref_count < 0) /* Did we go too far? */
661 panic("pmap_destroy(): ref_count < 0");
662
663 #ifdef notdef
664 if(pmap->stats.resident_count != 0)
665 panic("PMAP_DESTROY: pmap not empty");
666 #else
667 if(pmap->stats.resident_count != 0) {
668 pmap_remove(pmap, 0, 0xFFFFFFFFFFFFF000ULL);
669 }
670 #endif
671
672 /*
673 * Add the pmap to the pmap free list.
674 */
675
676 s = splhigh();
677 /*
678 * Add the pmap to the pmap free list.
679 */
680 simple_lock(&free_pmap_lock);
681
682 if (free_pmap_count <= free_pmap_max) { /* Do we have enough spares? */
683
684 pmap->freepmap = free_pmap_list; /* Queue in front */
685 free_pmap_list = pmap;
686 free_pmap_count++;
687 simple_unlock(&free_pmap_lock);
688
689 } else {
690 if(cursor_pmap == pmap) cursor_pmap = (pmap_t)pmap->pmap_link.prev; /* If we are releasing the cursor, back up */
691 fore = (pmap_t)pmap->pmap_link.prev;
692 aft = (pmap_t)pmap->pmap_link.next;
693 fore->pmap_link.next = pmap->pmap_link.next; /* My previous's next is my next */
694 aft->pmap_link.prev = pmap->pmap_link.prev; /* My next's previous is my previous */
695 simple_unlock(&free_pmap_lock);
696 pmapTrans[pmap->space].pmapPAddr = -1; /* Invalidate the translate table physical */
697 pmapTrans[pmap->space].pmapVAddr = -1; /* Invalidate the translate table virtual */
698 zfree(pmap_zone, (vm_offset_t) pmap);
699 }
700 splx(s);
701 }
702
703 /*
704 * pmap_reference(pmap)
705 * gains a reference to the specified pmap.
706 */
707 void
708 pmap_reference(pmap_t pmap)
709 {
710 spl_t s;
711
712 if (pmap != PMAP_NULL) hw_atomic_add(&pmap->ref_count, 1); /* Bump the count */
713 }
714
715 /*
716 * pmap_remove_some_phys
717 *
718 * Removes mappings of the associated page from the specified pmap
719 *
720 */
721 void pmap_remove_some_phys(
722 pmap_t pmap,
723 vm_offset_t pa)
724 {
725 register struct phys_entry *pp;
726 register struct mapping *mp;
727 unsigned int pindex;
728
729 if (pmap == PMAP_NULL) { /* This should never be called with a null pmap */
730 panic("pmap_remove_some_phys: null pmap\n");
731 }
732
733 pp = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
734 if (pp == 0) return; /* Leave if not in physical RAM */
735
736 while(1) { /* Keep going until we toss all pages from this pmap */
737 if (pmap->pmapFlags & pmapVMhost) {
738 mp = hw_purge_phys(pp); /* Toss a map */
739 if(!mp ) return;
740 if((unsigned int)mp & mapRetCode) { /* Was there a failure? */
741 panic("pmap_remove_some_phys: hw_purge_phys failed - pp = %08X, pmap = %08X, code = %08X\n",
742 pp, pmap, mp);
743 }
744 } else {
745 mp = hw_purge_space(pp, pmap); /* Toss a map */
746 if(!mp ) return;
747 if((unsigned int)mp & mapRetCode) { /* Was there a failure? */
748 panic("pmap_remove_some_phys: hw_purge_pmap failed - pp = %08X, pmap = %08X, code = %08X\n",
749 pp, pmap, mp);
750 }
751 }
752 mapping_free(mp); /* Toss the mapping */
753 }
754
755 return; /* Leave... */
756 }
757
758 /*
759 * pmap_remove(pmap, s, e)
760 * unmaps all virtual addresses v in the virtual address
761 * range determined by [s, e) and pmap.
762 * s and e must be on machine independent page boundaries and
763 * s must be less than or equal to e.
764 *
765 * Note that pmap_remove does not remove any mappings in nested pmaps. We just
766 * skip those segments.
767 */
768 void
769 pmap_remove(
770 pmap_t pmap,
771 addr64_t sva,
772 addr64_t eva)
773 {
774 addr64_t va, endva;
775
776 if (pmap == PMAP_NULL) return; /* Leave if software pmap */
777
778
779 /* It is just possible that eva might have wrapped around to zero,
780 * and sometimes we get asked to liberate something of size zero
781 * even though it's dumb (eg. after zero length read_overwrites)
782 */
783 assert(eva >= sva);
784
785 /* If these are not page aligned the loop might not terminate */
786 assert((sva == trunc_page_64(sva)) && (eva == trunc_page_64(eva)));
787
788 va = sva & -4096LL; /* Round start down to a page */
789 endva = eva & -4096LL; /* Round end down to a page */
790
791 while(1) { /* Go until we finish the range */
792 va = mapping_remove(pmap, va); /* Remove the mapping and see what's next */
793 va = va & -4096LL; /* Make sure the "not found" indication is clear */
794 if((va == 0) || (va >= endva)) break; /* End loop if we finish range or run off the end */
795 }
796
797 }
798
799 /*
800 * Routine:
801 * pmap_page_protect
802 *
803 * Function:
804 * Lower the permission for all mappings to a given page.
805 */
806 void
807 pmap_page_protect(
808 ppnum_t pa,
809 vm_prot_t prot)
810 {
811 register struct phys_entry *pp;
812 boolean_t remove;
813 unsigned int pindex;
814 mapping *mp;
815
816
817 switch (prot) {
818 case VM_PROT_READ:
819 case VM_PROT_READ|VM_PROT_EXECUTE:
820 remove = FALSE;
821 break;
822 case VM_PROT_ALL:
823 return;
824 default:
825 remove = TRUE;
826 break;
827 }
828
829
830 pp = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
831 if (pp == 0) return; /* Leave if not in physical RAM */
832
833 if (remove) { /* If the protection was set to none, we'll remove all mappings */
834
835 while(1) { /* Keep going until we toss all pages from this physical page */
836 mp = hw_purge_phys(pp); /* Toss a map */
837 if(!mp ) return;
838 if((unsigned int)mp & mapRetCode) { /* Was there a failure? */
839 panic("pmap_page_protect: hw_purge_phys failed - pp = %08X, code = %08X\n",
840 pp, mp);
841 }
842 mapping_free(mp); /* Toss the mapping */
843 }
844
845 return; /* Leave... */
846 }
847
848 /* When we get here, it means that we are to change the protection for a
849 * physical page.
850 */
851
852 mapping_protect_phys(pa, prot & VM_PROT_ALL); /* Change protection of all mappings to page. */
853
854 }
855
856 /*
857 * pmap_protect(pmap, s, e, prot)
858 * changes the protection on all virtual addresses v in the
859 * virtual address range determined by [s, e] and pmap to prot.
860 * s and e must be on machine independent page boundaries and
861 * s must be less than or equal to e.
862 *
863 * Note that any requests to change the protection of a nested pmap are
864 * ignored. Those changes MUST be done by calling this with the correct pmap.
865 */
866 void pmap_protect(
867 pmap_t pmap,
868 vm_offset_t sva,
869 vm_offset_t eva,
870 vm_prot_t prot)
871 {
872
873 addr64_t va, endva, nextva;
874
875 if (pmap == PMAP_NULL) return; /* Do nothing if no pmap */
876
877 if (prot == VM_PROT_NONE) { /* Should we kill the address range?? */
878 pmap_remove(pmap, (addr64_t)sva, (addr64_t)eva); /* Yeah, dump 'em */
879 return; /* Leave... */
880 }
881
882 va = sva & -4096LL; /* Round start down to a page */
883 endva = eva & -4096LL; /* Round end down to a page */
884
885 while(1) { /* Go until we finish the range */
886 (void)mapping_protect(pmap, va, prot & VM_PROT_ALL, &va); /* Change the protection and see what's next */
887 if((va == 0) || (va >= endva)) break; /* End loop if we finish range or run off the end */
888 }
889
890 }
891
892
893
894 /*
895 * pmap_enter
896 *
897 * Create a translation for the virtual address (virt) to the physical
898 * address (phys) in the pmap with the protection requested. If the
899 * translation is wired then we can not allow a full page fault, i.e.,
900 * the mapping control block is not eligible to be stolen in a low memory
901 * condition.
902 *
903 * NB: This is the only routine which MAY NOT lazy-evaluate
904 * or lose information. That is, this routine must actually
905 * insert this page into the given map NOW.
906 */
907 void
908 pmap_enter(pmap_t pmap, vm_offset_t va, ppnum_t pa, vm_prot_t prot,
909 unsigned int flags, boolean_t wired)
910 {
911 int memattr;
912 pmap_t opmap;
913 unsigned int mflags;
914 addr64_t colva;
915
916 if (pmap == PMAP_NULL) return; /* Leave if software pmap */
917
918 disable_preemption(); /* Don't change threads */
919
920 mflags = 0; /* Make sure this is initialized to nothing special */
921 if(!(flags & VM_WIMG_USE_DEFAULT)) { /* Are they supplying the attributes? */
922 mflags = mmFlgUseAttr | (flags & VM_MEM_GUARDED) | ((flags & VM_MEM_NOT_CACHEABLE) >> 1); /* Convert to our mapping_make flags */
923 }
924
925 /*
926 * It is possible to hang here if another processor is remapping any pages we collide with and are removing
927 */
928
929 while(1) { /* Keep trying the enter until it goes in */
930
931 colva = mapping_make(pmap, va, pa, mflags, 1, prot & VM_PROT_ALL); /* Enter the mapping into the pmap */
932
933 if(!colva) break; /* If there were no collisions, we are done... */
934
935 mapping_remove(pmap, colva); /* Remove the mapping that collided */
936 }
937
938 enable_preemption(); /* Thread change ok */
939
940 }
941
942 /*
943 * Enters translations for odd-sized V=F blocks.
944 *
945 * The higher level VM map should be locked to insure that we don't have a
946 * double diddle here.
947 *
948 * We panic if we get a block that overlaps with another. We do not merge adjacent
949 * blocks because removing any address within a block removes the entire block and if
950 * would really mess things up if we trashed too much.
951 *
952 * Once a block is mapped, it is unmutable, that is, protection, catch mode, etc. can
953 * not be changed. The block must be unmapped and then remapped with the new stuff.
954 * We also do not keep track of reference or change flags.
955 *
956 * Note that pmap_map_block_rc is the same but doesn't panic if collision.
957 *
958 */
959
960 void pmap_map_block(pmap_t pmap, addr64_t va, ppnum_t pa, vm_size_t size, vm_prot_t prot, int attr, unsigned int flags) { /* Map an autogenned block */
961
962 int memattr;
963 unsigned int mflags;
964 addr64_t colva;
965
966
967 if (pmap == PMAP_NULL) { /* Did they give us a pmap? */
968 panic("pmap_map_block: null pmap\n"); /* No, like that's dumb... */
969 }
970
971 // kprintf("pmap_map_block: (%08X) va = %016llX, pa = %08X, size = %08X, prot = %08X, attr = %08X, flags = %08X\n", /* (BRINGUP) */
972 // current_act(), va, pa, size, prot, attr, flags); /* (BRINGUP) */
973
974
975 mflags = mmFlgBlock | mmFlgUseAttr | (attr & VM_MEM_GUARDED) | ((attr & VM_MEM_NOT_CACHEABLE) >> 1); /* Convert to our mapping_make flags */
976 if(flags) mflags |= mmFlgPerm; /* Mark permanent if requested */
977
978 colva = mapping_make(pmap, va, pa, mflags, (size >> 12), prot); /* Enter the mapping into the pmap */
979
980 if(colva) { /* If there was a collision, panic */
981 panic("pmap_map_block: collision at %016llX, pmap = %08X\n", colva, pmap);
982 }
983
984 return; /* Return */
985 }
986
987 int pmap_map_block_rc(pmap_t pmap, addr64_t va, ppnum_t pa, vm_size_t size, vm_prot_t prot, int attr, unsigned int flags) { /* Map an autogenned block */
988
989 int memattr;
990 unsigned int mflags;
991 addr64_t colva;
992
993
994 if (pmap == PMAP_NULL) { /* Did they give us a pmap? */
995 panic("pmap_map_block_rc: null pmap\n"); /* No, like that's dumb... */
996 }
997
998 mflags = mmFlgBlock | mmFlgUseAttr | (attr & VM_MEM_GUARDED) | ((attr & VM_MEM_NOT_CACHEABLE) >> 1); /* Convert to our mapping_make flags */
999 if(flags) mflags |= mmFlgPerm; /* Mark permanent if requested */
1000
1001 colva = mapping_make(pmap, va, pa, mflags, (size >> 12), prot); /* Enter the mapping into the pmap */
1002
1003 if(colva) return 0; /* If there was a collision, fail */
1004
1005 return 1; /* Return true of we worked */
1006 }
1007
1008 /*
1009 * pmap_extract(pmap, va)
1010 * returns the physical address corrsponding to the
1011 * virtual address specified by pmap and va if the
1012 * virtual address is mapped and 0 if it is not.
1013 * Note: we assume nothing is ever mapped to phys 0.
1014 *
1015 * NOTE: This call always will fail for physical addresses greater than 0xFFFFF000.
1016 */
1017 vm_offset_t pmap_extract(pmap_t pmap, vm_offset_t va) {
1018
1019 spl_t spl;
1020 register struct mapping *mp;
1021 register vm_offset_t pa;
1022 addr64_t nextva;
1023 ppnum_t ppoffset;
1024 unsigned int gva;
1025
1026 #ifdef BOGUSCOMPAT
1027 panic("pmap_extract: THIS CALL IS BOGUS. NEVER USE IT EVER. So there...\n"); /* Don't use this */
1028 #else
1029
1030 gva = (unsigned int)va; /* Make sure we don't have a sign */
1031
1032 spl = splhigh(); /* We can't allow any loss of control here */
1033
1034 mp = mapping_find(pmap, (addr64_t)gva, &nextva,1); /* Find the mapping for this address */
1035
1036 if(!mp) { /* Is the page mapped? */
1037 splx(spl); /* Enable interrupts */
1038 return 0; /* Pass back 0 if not found */
1039 }
1040
1041 ppoffset = (ppnum_t)(((gva & -4096LL) - (mp->mpVAddr & -4096LL)) >> 12); /* Get offset from va to base va */
1042
1043
1044 pa = mp->mpPAddr + ppoffset; /* Remember ppage because mapping may vanish after drop call */
1045
1046 mapping_drop_busy(mp); /* We have everything we need from the mapping */
1047 splx(spl); /* Restore 'rupts */
1048
1049 if(pa > maxPPage32) return 0; /* Force large addresses to fail */
1050
1051 pa = (pa << 12) | (va & 0xFFF); /* Convert physical page number to address */
1052
1053 #endif
1054 return pa; /* Return physical address or 0 */
1055 }
1056
1057 /*
1058 * ppnum_t pmap_find_phys(pmap, addr64_t va)
1059 * returns the physical page corrsponding to the
1060 * virtual address specified by pmap and va if the
1061 * virtual address is mapped and 0 if it is not.
1062 * Note: we assume nothing is ever mapped to phys 0.
1063 *
1064 */
1065 ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va) {
1066
1067 spl_t spl;
1068 register struct mapping *mp;
1069 ppnum_t pa, ppoffset;
1070 addr64_t nextva, curva;
1071
1072 spl = splhigh(); /* We can't allow any loss of control here */
1073
1074 mp = mapping_find(pmap, va, &nextva, 1); /* Find the mapping for this address */
1075
1076 if(!mp) { /* Is the page mapped? */
1077 splx(spl); /* Enable interrupts */
1078 return 0; /* Pass back 0 if not found */
1079 }
1080
1081
1082 ppoffset = (ppnum_t)(((va & -4096LL) - (mp->mpVAddr & -4096LL)) >> 12); /* Get offset from va to base va */
1083
1084 pa = mp->mpPAddr + ppoffset; /* Get the actual physical address */
1085
1086 mapping_drop_busy(mp); /* We have everything we need from the mapping */
1087
1088 splx(spl); /* Restore 'rupts */
1089 return pa; /* Return physical address or 0 */
1090 }
1091
1092
1093 /*
1094 * pmap_attributes:
1095 *
1096 * Set/Get special memory attributes; not implemented.
1097 *
1098 * Note: 'VAL_GET_INFO' is used to return info about a page.
1099 * If less than 1 page is specified, return the physical page
1100 * mapping and a count of the number of mappings to that page.
1101 * If more than one page is specified, return the number
1102 * of resident pages and the number of shared (more than
1103 * one mapping) pages in the range;
1104 *
1105 *
1106 */
1107 kern_return_t
1108 pmap_attribute(pmap, address, size, attribute, value)
1109 pmap_t pmap;
1110 vm_offset_t address;
1111 vm_size_t size;
1112 vm_machine_attribute_t attribute;
1113 vm_machine_attribute_val_t* value;
1114 {
1115
1116 return KERN_INVALID_ARGUMENT;
1117
1118 }
1119
1120 /*
1121 * pmap_attribute_cache_sync(vm_offset_t pa)
1122 *
1123 * Invalidates all of the instruction cache on a physical page and
1124 * pushes any dirty data from the data cache for the same physical page
1125 */
1126
1127 kern_return_t pmap_attribute_cache_sync(ppnum_t pp, vm_size_t size,
1128 vm_machine_attribute_t attribute,
1129 vm_machine_attribute_val_t* value) {
1130
1131 spl_t s;
1132 unsigned int i, npages;
1133
1134 npages = round_page_32(size) >> 12; /* Get the number of pages to do */
1135
1136 for(i = 0; i < npages; i++) { /* Do all requested pages */
1137 s = splhigh(); /* No interruptions here */
1138 sync_ppage(pp + i); /* Go flush data cache and invalidate icache */
1139 splx(s); /* Allow interruptions */
1140 }
1141
1142 return KERN_SUCCESS;
1143 }
1144
1145 /*
1146 * pmap_sync_caches_phys(ppnum_t pa)
1147 *
1148 * Invalidates all of the instruction cache on a physical page and
1149 * pushes any dirty data from the data cache for the same physical page
1150 */
1151
1152 void pmap_sync_caches_phys(ppnum_t pa) {
1153
1154 spl_t s;
1155
1156 s = splhigh(); /* No interruptions here */
1157 sync_ppage(pa); /* Sync up dem caches */
1158 splx(s); /* Allow interruptions */
1159 return;
1160 }
1161
1162 /*
1163 * pmap_collect
1164 *
1165 * Garbage collects the physical map system for pages that are no longer used.
1166 * It isn't implemented or needed or wanted.
1167 */
1168 void
1169 pmap_collect(pmap_t pmap)
1170 {
1171 return;
1172 }
1173
1174 /*
1175 * Routine: pmap_activate
1176 * Function:
1177 * Binds the given physical map to the given
1178 * processor, and returns a hardware map description.
1179 * It isn't implemented or needed or wanted.
1180 */
1181 void
1182 pmap_activate(
1183 pmap_t pmap,
1184 thread_t th,
1185 int which_cpu)
1186 {
1187 return;
1188 }
1189 /*
1190 * pmap_deactivate:
1191 * It isn't implemented or needed or wanted.
1192 */
1193 void
1194 pmap_deactivate(
1195 pmap_t pmap,
1196 thread_t th,
1197 int which_cpu)
1198 {
1199 return;
1200 }
1201
1202
1203 /*
1204 * pmap_pageable(pmap, s, e, pageable)
1205 * Make the specified pages (by pmap, offset)
1206 * pageable (or not) as requested.
1207 *
1208 * A page which is not pageable may not take
1209 * a fault; therefore, its page table entry
1210 * must remain valid for the duration.
1211 *
1212 * This routine is merely advisory; pmap_enter()
1213 * will specify that these pages are to be wired
1214 * down (or not) as appropriate.
1215 *
1216 * (called from vm/vm_fault.c).
1217 */
1218 void
1219 pmap_pageable(
1220 pmap_t pmap,
1221 vm_offset_t start,
1222 vm_offset_t end,
1223 boolean_t pageable)
1224 {
1225
1226 return; /* This is not used... */
1227
1228 }
1229 /*
1230 * Routine: pmap_change_wiring
1231 * NOT USED ANYMORE.
1232 */
1233 void
1234 pmap_change_wiring(
1235 register pmap_t pmap,
1236 vm_offset_t va,
1237 boolean_t wired)
1238 {
1239 return; /* This is not used... */
1240 }
1241
1242 /*
1243 * pmap_modify_pages(pmap, s, e)
1244 * sets the modified bit on all virtual addresses v in the
1245 * virtual address range determined by [s, e] and pmap,
1246 * s and e must be on machine independent page boundaries and
1247 * s must be less than or equal to e.
1248 *
1249 * Note that this function will not descend nested pmaps.
1250 */
1251 void
1252 pmap_modify_pages(
1253 pmap_t pmap,
1254 vm_offset_t sva,
1255 vm_offset_t eva)
1256 {
1257 spl_t spl;
1258 mapping *mp;
1259 ppnum_t pa;
1260 addr64_t va, endva, nextva;
1261 unsigned int saveflags;
1262
1263 if (pmap == PMAP_NULL) return; /* If no pmap, can't do it... */
1264
1265 va = sva & -4096; /* Round to page */
1266 endva = eva & -4096; /* Round to page */
1267
1268 while (va < endva) { /* Walk through all pages */
1269
1270 spl = splhigh(); /* We can't allow any loss of control here */
1271
1272 mp = mapping_find(pmap, (addr64_t)va, &va, 0); /* Find the mapping for this address */
1273
1274 if(!mp) { /* Is the page mapped? */
1275 splx(spl); /* Page not mapped, restore interruptions */
1276 if((va == 0) || (va >= endva)) break; /* We are done if there are no more or we hit the end... */
1277 continue; /* We are not done and there is more to check... */
1278 }
1279
1280 saveflags = mp->mpFlags; /* Remember the flags */
1281 pa = mp->mpPAddr; /* Remember ppage because mapping may vanish after drop call */
1282
1283 mapping_drop_busy(mp); /* We have everything we need from the mapping */
1284
1285 splx(spl); /* Restore 'rupts */
1286
1287 if(saveflags & (mpNest | mpBlock)) continue; /* Can't mess around with these guys... */
1288
1289 mapping_set_mod(pa); /* Set the modfied bit for this page */
1290
1291 if(va == 0) break; /* We hit the end of the pmap, might as well leave now... */
1292 }
1293 return; /* Leave... */
1294 }
1295
1296 /*
1297 * pmap_clear_modify(phys)
1298 * clears the hardware modified ("dirty") bit for one
1299 * machine independant page starting at the given
1300 * physical address. phys must be aligned on a machine
1301 * independant page boundary.
1302 */
1303 void
1304 pmap_clear_modify(vm_offset_t pa)
1305 {
1306
1307 mapping_clr_mod((ppnum_t)pa); /* Clear all change bits for physical page */
1308
1309 }
1310
1311 /*
1312 * pmap_is_modified(phys)
1313 * returns TRUE if the given physical page has been modified
1314 * since the last call to pmap_clear_modify().
1315 */
1316 boolean_t
1317 pmap_is_modified(register vm_offset_t pa)
1318 {
1319 return mapping_tst_mod((ppnum_t)pa); /* Check for modified */
1320
1321 }
1322
1323 /*
1324 * pmap_clear_reference(phys)
1325 * clears the hardware referenced bit in the given machine
1326 * independant physical page.
1327 *
1328 */
1329 void
1330 pmap_clear_reference(vm_offset_t pa)
1331 {
1332 mapping_clr_ref((ppnum_t)pa); /* Check for modified */
1333 }
1334
1335 /*
1336 * pmap_is_referenced(phys)
1337 * returns TRUE if the given physical page has been referenced
1338 * since the last call to pmap_clear_reference().
1339 */
1340 boolean_t
1341 pmap_is_referenced(vm_offset_t pa)
1342 {
1343 return mapping_tst_ref((ppnum_t)pa); /* Check for referenced */
1344 }
1345
1346 /*
1347 * pmap_canExecute(ppnum_t pa)
1348 * returns 1 if instructions can execute
1349 * returns 0 if know not (i.e. guarded and/or non-executable set)
1350 * returns -1 if we don't know (i.e., the page is no RAM)
1351 */
1352 int
1353 pmap_canExecute(ppnum_t pa)
1354 {
1355 phys_entry *physent;
1356 unsigned int pindex;
1357
1358 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
1359
1360 if(!physent) return -1; /* If there is no physical entry, we don't know... */
1361
1362 if((physent->ppLink & (ppN | ppG))) return 0; /* If we are marked non-executable or guarded, say we can not execute */
1363 return 1; /* Good to go... */
1364 }
1365
1366 #if MACH_VM_DEBUG
1367 int
1368 pmap_list_resident_pages(
1369 register pmap_t pmap,
1370 register vm_offset_t *listp,
1371 register int space)
1372 {
1373 return 0;
1374 }
1375 #endif /* MACH_VM_DEBUG */
1376
1377 /*
1378 * Locking:
1379 * spl: VM
1380 */
1381 void
1382 pmap_copy_part_page(
1383 vm_offset_t src,
1384 vm_offset_t src_offset,
1385 vm_offset_t dst,
1386 vm_offset_t dst_offset,
1387 vm_size_t len)
1388 {
1389 register struct phys_entry *pp_src, *pp_dst;
1390 spl_t s;
1391 addr64_t fsrc, fdst;
1392
1393 assert(((dst <<12) & PAGE_MASK+dst_offset+len) <= PAGE_SIZE);
1394 assert(((src <<12) & PAGE_MASK+src_offset+len) <= PAGE_SIZE);
1395
1396 fsrc = ((addr64_t)src << 12) + src_offset;
1397 fdst = ((addr64_t)dst << 12) + dst_offset;
1398
1399 phys_copy(fsrc, fdst, len); /* Copy the stuff physically */
1400 }
1401
1402 void
1403 pmap_zero_part_page(
1404 vm_offset_t p,
1405 vm_offset_t offset,
1406 vm_size_t len)
1407 {
1408 panic("pmap_zero_part_page");
1409 }
1410
1411 boolean_t pmap_verify_free(ppnum_t pa) {
1412
1413 struct phys_entry *pp;
1414 unsigned int pindex;
1415
1416 pp = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
1417 if (pp == 0) return FALSE; /* If there isn't one, show no mapping... */
1418
1419 if(pp->ppLink & ~(ppLock | ppN | ppFlags)) return TRUE; /* We have at least one mapping */
1420 return FALSE; /* No mappings */
1421 }
1422
1423
1424 /* Determine if we need to switch space and set up for it if so */
1425
1426 void pmap_switch(pmap_t map)
1427 {
1428 unsigned int i;
1429
1430
1431 hw_blow_seg(copyIOaddr); /* Blow off the first segment */
1432 hw_blow_seg(copyIOaddr + 0x10000000ULL); /* Blow off the second segment */
1433
1434 /* when changing to kernel space, don't bother
1435 * doing anything, the kernel is mapped from here already.
1436 */
1437 if (map->space == PPC_SID_KERNEL) { /* Are we switching into kernel space? */
1438 return; /* If so, we don't do anything... */
1439 }
1440
1441 hw_set_user_space(map); /* Indicate if we need to load the SRs or not */
1442 return; /* Bye, bye, butterfly... */
1443 }
1444
1445 /*
1446 * kern_return_t pmap_nest(grand, subord, vstart, size)
1447 *
1448 * grand = the pmap that we will nest subord into
1449 * subord = the pmap that goes into the grand
1450 * vstart = start of range in pmap to be inserted
1451 * nstart = start of range in pmap nested pmap
1452 * size = Size of nest area (up to 16TB)
1453 *
1454 * Inserts a pmap into another. This is used to implement shared segments.
1455 * On the current PPC processors, this is limited to segment (256MB) aligned
1456 * segment sized ranges.
1457 *
1458 * We actually kinda allow recursive nests. The gating factor is that we do not allow
1459 * nesting on top of something that is already mapped, i.e., the range must be empty.
1460 *
1461 *
1462 *
1463 * Note that we depend upon higher level VM locks to insure that things don't change while
1464 * we are doing this. For example, VM should not be doing any pmap enters while it is nesting
1465 * or do 2 nests at once.
1466 */
1467
1468 kern_return_t pmap_nest(pmap_t grand, pmap_t subord, addr64_t vstart, addr64_t nstart, uint64_t size) {
1469
1470 addr64_t nextva, vend, colladdr;
1471 unsigned int msize;
1472 int i, nlists, asize;
1473 spl_t s;
1474 mapping *mp;
1475
1476
1477 if(size & 0x0FFFFFFFULL) return KERN_INVALID_VALUE; /* We can only do this for multiples of 256MB */
1478 if((size >> 28) > 65536) return KERN_INVALID_VALUE; /* Max size we can nest is 16TB */
1479 if(vstart & 0x0FFFFFFFULL) return KERN_INVALID_VALUE; /* We can only do this aligned to 256MB */
1480 if(nstart & 0x0FFFFFFFULL) return KERN_INVALID_VALUE; /* We can only do this aligned to 256MB */
1481
1482 if(size == 0) { /* Is the size valid? */
1483 panic("pmap_nest: size is invalid - %016llX\n", size);
1484 }
1485
1486 msize = (size >> 28) - 1; /* Change size to blocks of 256MB */
1487
1488 nlists = mapSetLists(grand); /* Set number of lists this will be on */
1489
1490 mp = mapping_alloc(nlists); /* Get a spare mapping block */
1491
1492 mp->mpFlags = 0x01000000 | mpNest | nlists; /* Set the flags. Make sure busy count is 1 */
1493 mp->mpSpace = subord->space; /* Set the address space/pmap lookup ID */
1494 mp->mpBSize = msize; /* Set the size */
1495 mp->mpPte = 0; /* Set the PTE invalid */
1496 mp->mpPAddr = 0; /* Set the physical page number */
1497 mp->mpVAddr = vstart; /* Set the address */
1498 mp->mpNestReloc = nstart - vstart; /* Set grand to nested vaddr relocation value */
1499
1500 colladdr = hw_add_map(grand, mp); /* Go add the mapping to the pmap */
1501
1502 if(colladdr) { /* Did it collide? */
1503 vend = vstart + size - 4096; /* Point to the last page we would cover in nest */
1504 panic("pmap_nest: attempt to nest into a non-empty range - pmap = %08X, start = %016llX, end = %016llX\n",
1505 grand, vstart, vend);
1506 }
1507
1508 return KERN_SUCCESS;
1509 }
1510
1511 /*
1512 * kern_return_t pmap_unnest(grand, vaddr)
1513 *
1514 * grand = the pmap that we will nest subord into
1515 * vaddr = start of range in pmap to be unnested
1516 *
1517 * Removes a pmap from another. This is used to implement shared segments.
1518 * On the current PPC processors, this is limited to segment (256MB) aligned
1519 * segment sized ranges.
1520 */
1521
1522 kern_return_t pmap_unnest(pmap_t grand, addr64_t vaddr) {
1523
1524 unsigned int oflags, seg, grandr, tstamp;
1525 int i, tcpu, mycpu;
1526 addr64_t nextva;
1527 spl_t s;
1528 mapping *mp;
1529
1530 s = splhigh(); /* Make sure interruptions are disabled */
1531
1532 mp = mapping_find(grand, vaddr, &nextva, 0); /* Find the nested map */
1533
1534 if(((unsigned int)mp & mapRetCode) != mapRtOK) { /* See if it was even nested */
1535 panic("pmap_unnest: Attempt to unnest an unnested segment - va = %016llX\n", vaddr);
1536 }
1537
1538 if(!(mp->mpFlags & mpNest)) { /* Did we find something other than a nest? */
1539 panic("pmap_unnest: Attempt to unnest something that is not a nest - va = %016llX\n", vaddr);
1540 }
1541
1542 if(mp->mpVAddr != vaddr) { /* Make sure the address is the same */
1543 panic("pmap_unnest: Attempt to unnest something that is not at start of nest - va = %016llX\n", vaddr);
1544 }
1545
1546 (void)hw_atomic_or(&mp->mpFlags, mpRemovable); /* Show that this mapping is now removable */
1547
1548 mapping_drop_busy(mp); /* Go ahead and relase the mapping now */
1549
1550 disable_preemption(); /* It's all for me! */
1551 splx(s); /* Restore 'rupts */
1552
1553 (void)mapping_remove(grand, vaddr); /* Toss the nested pmap mapping */
1554
1555 invalidateSegs(grand); /* Invalidate the pmap segment cache */
1556
1557 /*
1558 * Note that the following will force the segment registers to be reloaded
1559 * on all processors (if they are using the pmap we just changed) before returning.
1560 *
1561 * This is needed. The reason is that until the segment register is
1562 * reloaded, another thread in the same task on a different processor will
1563 * be able to access memory that it isn't allowed to anymore. That can happen
1564 * because access to the subordinate pmap is being removed, but the pmap is still
1565 * valid.
1566 *
1567 * Note that we only kick the other processor if we see that it was using the pmap while we
1568 * were changing it.
1569 */
1570
1571
1572 mycpu = cpu_number(); /* Who am I? Am I just a dream? */
1573 for(i=0; i < real_ncpus; i++) { /* Cycle through processors */
1574 if((unsigned int)grand == per_proc_info[i].ppUserPmapVirt) { /* Is this guy using the changed pmap? */
1575
1576 per_proc_info[i].ppInvSeg = 1; /* Show that we need to invalidate the segments */
1577
1578 if(i == mycpu) continue; /* Don't diddle ourselves */
1579
1580 tstamp = per_proc_info[i].ruptStamp[1]; /* Save the processor's last interrupt time stamp */
1581 if(cpu_signal(i, SIGPcpureq, CPRQsegload, 0) != KERN_SUCCESS) { /* Make sure we see the pmap change */
1582 continue;
1583 }
1584
1585 if(!hw_cpu_wcng(&per_proc_info[i].ruptStamp[1], tstamp, LockTimeOut)) { /* Wait for the other processors to enter debug */
1586 panic("pmap_unnest: Other processor (%d) did not see interruption request\n", i);
1587 }
1588 }
1589 }
1590
1591 enable_preemption(); /* Others can run now */
1592 return KERN_SUCCESS; /* Bye, bye, butterfly... */
1593 }
1594
1595
1596 /*
1597 * void MapUserAddressSpaceInit(void)
1598 *
1599 * Initialized anything we need to in order to map user address space slices into
1600 * the kernel. Primarily used for copy in/out.
1601 *
1602 * Currently we only support one 512MB slot for this purpose. There are two special
1603 * mappings defined for the purpose: the special pmap nest, and linkage mapping.
1604 *
1605 * The special pmap nest (which is allocated in this function) is used as a place holder
1606 * in the kernel's pmap search list. It is 512MB long and covers the address range
1607 * starting at copyIOaddr. It points to no actual memory and when the fault handler
1608 * hits in it, it knows to look in the per_proc and start using the linkage
1609 * mapping contained therin.
1610 *
1611 * The linkage mapping is used to glue the user address space slice into the
1612 * kernel. It contains the relocation information used to transform the faulting
1613 * kernel address into the user address space. It also provides the link to the
1614 * user's pmap. This is pointed to by the per_proc and is switched in and out
1615 * whenever there is a context switch.
1616 *
1617 */
1618
1619 void MapUserAddressSpaceInit(void) {
1620
1621 addr64_t colladdr;
1622 int nlists, asize;
1623 mapping *mp;
1624
1625 nlists = mapSetLists(kernel_pmap); /* Set number of lists this will be on */
1626
1627 mp = mapping_alloc(nlists); /* Get a spare mapping block */
1628
1629 mp->mpFlags = 0x01000000 |mpNest | mpSpecial | nlists; /* Set the flags. Make sure busy count is 1 */
1630 mp->mpSpace = kernel_pmap->space; /* Set the address space/pmap lookup ID */
1631 mp->mpBSize = 1; /* Set the size to 2 segments */
1632 mp->mpPte = 0; /* Means nothing */
1633 mp->mpPAddr = 0; /* Means nothing */
1634 mp->mpVAddr = copyIOaddr; /* Set the address range we cover */
1635 mp->mpNestReloc = 0; /* Means nothing */
1636
1637 colladdr = hw_add_map(kernel_pmap, mp); /* Go add the mapping to the pmap */
1638
1639 if(colladdr) { /* Did it collide? */
1640 panic("MapUserAddressSpaceInit: MapUserAddressSpace range already mapped\n");
1641 }
1642
1643 return;
1644 }
1645
1646 /*
1647 * addr64_t MapUserAddressSpace(vm_map_t map, vm_offset_t va, size)
1648 *
1649 * map = the vm_map that we are mapping into the kernel
1650 * va = start of the address range we are mapping
1651 * size = size of the range. No greater than 256MB and not 0.
1652 * Note that we do not test validty, we chose to trust our fellows...
1653 *
1654 * Maps a slice of a user address space into a predefined kernel range
1655 * on a per-thread basis. In the future, the restriction of a predefined
1656 * range will be loosened.
1657 *
1658 * Builds the proper linkage map to map the user range
1659 * We will round this down to the previous segment boundary and calculate
1660 * the relocation to the kernel slot
1661 *
1662 * We always make a segment table entry here if we need to. This is mainly because of
1663 * copyin/out and if we don't, there will be multiple segment faults for
1664 * each system call. I have seen upwards of 30000 per second.
1665 *
1666 * We do check, however, to see if the slice is already mapped and if so,
1667 * we just exit. This is done for performance reasons. It was found that
1668 * there was a considerable boost in copyin/out performance if we did not
1669 * invalidate the segment at ReleaseUserAddressSpace time, so we dumped the
1670 * restriction that you had to bracket MapUserAddressSpace. Further, there
1671 * is a yet further boost if you didn't need to map it each time. The theory
1672 * behind this is that many times copies are to or from the same segment and
1673 * done multiple times within the same system call. To take advantage of that,
1674 * we check cioSpace and cioRelo to see if we've already got it.
1675 *
1676 * We also need to half-invalidate the slice when we context switch or go
1677 * back to user state. A half-invalidate does not clear the actual mapping,
1678 * but it does force the MapUserAddressSpace function to reload the segment
1679 * register/SLBE. If this is not done, we can end up some pretty severe
1680 * performance penalties. If we map a slice, and the cached space/relocation is
1681 * the same, we won't reload the segment registers. Howver, since we ran someone else,
1682 * our SR is cleared and we will take a fault. This is reasonable if we block
1683 * while copying (e.g., we took a page fault), but it is not reasonable when we
1684 * just start. For this reason, we half-invalidate to make sure that the SR is
1685 * explicitly reloaded.
1686 *
1687 * Note that we do not go to the trouble of making a pmap segment cache
1688 * entry for these guys because they are very short term -- 99.99% of the time
1689 * they will be unmapped before the next context switch.
1690 *
1691 */
1692
1693 addr64_t MapUserAddressSpace(vm_map_t map, addr64_t va, unsigned int size) {
1694
1695 addr64_t baddrs, reladd;
1696 thread_act_t act;
1697 mapping *mp;
1698 struct per_proc_info *perproc;
1699
1700 baddrs = va & 0xFFFFFFFFF0000000ULL; /* Isolate the segment */
1701 act = current_act(); /* Remember our activation */
1702
1703 reladd = baddrs - copyIOaddr; /* Get the relocation from user to kernel */
1704
1705 if((act->mact.cioSpace == map->pmap->space) && (act->mact.cioRelo == reladd)) { /* Already mapped? */
1706 return ((va & 0x0FFFFFFFULL) | copyIOaddr); /* Pass back the kernel address we are to use */
1707 }
1708
1709 disable_preemption(); /* Don't move... */
1710 perproc = getPerProc(); /* Get our per_proc_block */
1711
1712 mp = (mapping *)&perproc->ppCIOmp; /* Make up for C */
1713 act->mact.cioRelo = reladd; /* Relocation from user to kernel */
1714 mp->mpNestReloc = reladd; /* Relocation from user to kernel */
1715
1716 act->mact.cioSpace = map->pmap->space; /* Set the address space/pmap lookup ID */
1717 mp->mpSpace = map->pmap->space; /* Set the address space/pmap lookup ID */
1718
1719 /*
1720 * Here we make an assumption that we are going to be using the base pmap's address space.
1721 * If we are wrong, and that would be very, very, very rare, the fault handler will fix us up.
1722 */
1723
1724 hw_map_seg(map->pmap, copyIOaddr, baddrs); /* Make the entry for the first segment */
1725
1726 enable_preemption(); /* Let's move */
1727 return ((va & 0x0FFFFFFFULL) | copyIOaddr); /* Pass back the kernel address we are to use */
1728 }
1729
1730 /*
1731 * void ReleaseUserAddressMapping(addr64_t kva)
1732 *
1733 * kva = kernel address of the user copy in/out slice
1734 *
1735 */
1736
1737 void ReleaseUserAddressSpace(addr64_t kva) {
1738
1739 int i;
1740 addr64_t nextva, vend, kaddr, baddrs;
1741 unsigned int msize;
1742 thread_act_t act;
1743 mapping *mp;
1744
1745 if(kva == 0) return; /* Handle a 0 */
1746
1747 disable_preemption(); /* Don't move... */
1748
1749 act = current_act(); /* Remember our activation */
1750
1751 if(act->mact.cioSpace == invalSpace) { /* We only support one at a time */
1752 panic("ReleaseUserAddressMapping: attempt release undefined copy in/out user address space slice\n");
1753 }
1754
1755 act->mact.cioSpace = invalSpace; /* Invalidate space */
1756 mp = (mapping *)&per_proc_info[cpu_number()].ppCIOmp; /* Make up for C */
1757 mp->mpSpace = invalSpace; /* Trash it in the per_proc as well */
1758
1759 hw_blow_seg(copyIOaddr); /* Blow off the first segment */
1760 hw_blow_seg(copyIOaddr + 0x10000000ULL); /* Blow off the second segment */
1761
1762 enable_preemption(); /* Let's move */
1763
1764 return; /* Let's leave */
1765 }
1766
1767
1768
1769 /*
1770 * kern_return_t pmap_boot_map(size)
1771 *
1772 * size = size of virtual address range to be mapped
1773 *
1774 * This function is used to assign a range of virtual addresses before VM in
1775 * initialized. It starts at VM_MAX_KERNEL_ADDRESS and works downward.
1776 * The variable vm_last_addr contains the current highest possible VM
1777 * assignable address. It is a panic to attempt to call this after VM has
1778 * started up. The only problem is, is that we may not have the serial or
1779 * framebuffer mapped, so we'll never know we died.........
1780 */
1781
1782 vm_offset_t pmap_boot_map(vm_size_t size) {
1783
1784 if(kernel_map != VM_MAP_NULL) { /* Has VM already started? */
1785 panic("pmap_boot_map: VM started\n");
1786 }
1787
1788 size = round_page_32(size); /* Make sure this is in pages */
1789 vm_last_addr = vm_last_addr - size; /* Allocate the memory */
1790 return (vm_last_addr + 1); /* Return the vaddr we just allocated */
1791
1792 }
1793
1794
1795
1796 /* temporary workaround */
1797 boolean_t
1798 coredumpok(vm_map_t map, vm_offset_t va)
1799 {
1800 return TRUE;
1801 }