]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/mappings.c
42992cb24635cc7240a30208bb34b8e237407ff0
[apple/xnu.git] / osfmk / ppc / mappings.c
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * This file is used to maintain the virtual to real mappings for a PowerPC machine.
32 * The code herein is primarily used to bridge between the pmap layer and the hardware layer.
33 * Currently, some of the function of this module is contained within pmap.c. We may want to move
34 * all of this into it (or most anyway) for the sake of performance. We shall see as we write it.
35 *
36 * We also depend upon the structure of the phys_entry control block. We do put some processor
37 * specific stuff in there.
38 *
39 */
40
41 #include <debug.h>
42 #include <mach_kgdb.h>
43 #include <mach_vm_debug.h>
44 #include <db_machine_commands.h>
45
46 #include <mach/mach_types.h>
47 #include <mach/vm_attributes.h>
48 #include <mach/vm_param.h>
49
50 #include <kern/kern_types.h>
51 #include <kern/thread.h>
52 #include <kern/spl.h>
53 #include <kern/misc_protos.h>
54
55 #include <vm/vm_fault.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59 #include <vm/pmap.h>
60
61 #include <ppc/exception.h>
62 #include <ppc/misc_protos.h>
63 #include <ppc/proc_reg.h>
64 #include <ppc/pmap.h>
65 #include <ppc/mem.h>
66 #include <ppc/new_screen.h>
67 #include <ppc/Firmware.h>
68 #include <ppc/mappings.h>
69 #include <ddb/db_output.h>
70
71 #include <console/video_console.h> /* (TEST/DEBUG) */
72
73 #define PERFTIMES 0
74
75 vm_map_t mapping_map = VM_MAP_NULL;
76
77 unsigned int incrVSID = 0; /* VSID increment value */
78 unsigned int mappingdeb0 = 0;
79 unsigned int mappingdeb1 = 0;
80 int ppc_max_adrsp; /* Maximum address spaces */
81
82 addr64_t *mapdebug; /* (BRINGUP) */
83 extern unsigned int DebugWork; /* (BRINGUP) */
84
85 void mapping_verify(void);
86 void mapping_phys_unused(ppnum_t pa);
87
88 int nx_enabled = 0; /* enable no-execute protection */
89
90 /*
91 * ppc_prot translates Mach's representation of protections to that of the PPC hardware.
92 * For Virtual Machines (VMM), we also provide translation entries where the output is
93 * the same as the input, allowing direct specification of PPC protections. Mach's
94 * representations are always in the range 0..7, so they always fall into the first
95 * 8 table entries; direct translations are placed in the range 8..16, so they fall into
96 * the second half of the table.
97 *
98 */
99
100 unsigned char ppc_prot[16] = { 4, 7, 6, 6, 3, 3, 2, 2, /* Mach -> PPC translations */
101 0, 1, 2, 3, 4, 5, 6, 7 }; /* VMM direct translations */
102
103
104
105 vm_prot_t getProtPPC(int key, boolean_t disable_NX) {
106 vm_prot_t prot;
107
108 prot = ppc_prot[key & 0xF];
109
110 if (key <= 7 && disable_NX == TRUE)
111 prot &= ~mpN;
112
113 return (prot);
114 }
115
116
117 /*
118 * About PPC VSID generation:
119 *
120 * This function is called to generate an address space ID. This space ID must be unique within
121 * the system. For the PowerPC, it is used to build the VSID. We build a VSID in the following
122 * way: space ID << 4 | segment. Since a VSID is 24 bits, and out of that, we reserve the last
123 * 4, so, we can have 2^20 (2M) unique IDs. Each pmap has a unique space ID, so we should be able
124 * to have 2M pmaps at a time, which we couldn't, we'd run out of memory way before then. The
125 * problem is that only a certain number of pmaps are kept in a free list and if that is full,
126 * they are release. This causes us to lose track of what space IDs are free to be reused.
127 * We can do 4 things: 1) not worry about it, 2) keep all free pmaps, 3) rebuild all mappings
128 * when the space ID wraps, or 4) scan the list of pmaps and find a free one.
129 *
130 * Yet another consideration is the hardware use of the VSID. It is used as part of the hash
131 * calculation for virtual address lookup. An improperly chosen value could potentially cause
132 * too many hashes to hit the same bucket, causing PTEG overflows. The actual hash function
133 * is (page index XOR vsid) mod number of ptegs. For a 32MB machine, using the suggested
134 * hash table size, there are 2^12 (8192) PTEGs. Remember, though, that the bottom 4 bits
135 * are reserved for the segment number, which means that we really have 2^(12-4) 512 space IDs
136 * before we start hashing to the same buckets with the same vaddrs. Also, within a space ID,
137 * every 8192 pages (32MB) within a segment will hash to the same bucket. That's 8 collisions
138 * per segment. So, a scan of every page for 256MB would fill 32 PTEGs completely, but
139 * with no overflow. I don't think that this is a problem.
140 *
141 * There may be a problem with the space ID, though. A new space ID is generate (mainly)
142 * whenever there is a fork. There shouldn't really be any problem because (for a 32MB
143 * machine) we can have 512 pmaps and still not have hash collisions for the same address.
144 * The potential problem, though, is if we get long-term pmaps that have space IDs that are
145 * the same modulo 512. We can reduce this problem by having the segment number be bits
146 * 0-3 of the space ID rather than 20-23. Doing this means that, in effect, corresponding
147 * vaddrs in different segments hash to the same PTEG. While this is somewhat of a problem,
148 * I don't think that it is as signifigant as the other, so, I'll make the space ID
149 * with segment first.
150 *
151 * The final, and biggest problem is the wrap, which will happen every 2^20 space IDs.
152 * While this is a problem that should only happen in periods counted in weeks, it can and
153 * will happen. This is assuming a monotonically increasing space ID. If we were to search
154 * for an inactive space ID, there could not be a wrap until there was 2^20 concurrent space IDs.
155 * That's pretty unlikely to happen. There couldn't be enough storage to support a million tasks.
156 *
157 * So, what we do is to keep all active pmaps in a chain (anchored from kernel_pmap and
158 * locked by free_pmap_lock) that is sorted in VSID sequence order.
159 *
160 * Whenever we need a VSID, we walk the list looking for the next in the sequence from
161 * the last that was freed. The we allocate that.
162 *
163 * NOTE: We must be called with interruptions off and free_pmap_lock held.
164 *
165 */
166
167 /*
168 * mapping_init();
169 * Do anything that needs to be done before the mapping system can be used.
170 * Hash table must be initialized before we call this.
171 *
172 * Calculate the SID increment. Currently we use size^(1/2) + size^(1/4) + 1;
173 */
174
175 void mapping_init(void) {
176
177 unsigned int tmp, maxeff, rwidth;
178
179 ppc_max_adrsp = maxAdrSp; /* Set maximum address spaces */
180
181 maxeff = 32; /* Assume 32-bit */
182 if(PerProcTable[0].ppe_vaddr->pf.Available & pf64Bit) maxeff = 64; /* Is this a 64-bit machine? */
183
184 rwidth = PerProcTable[0].ppe_vaddr->pf.pfMaxVAddr - maxAdrSpb; /* Reduce address width by width of address space ID */
185 if(rwidth > maxeff) rwidth = maxeff; /* If we still have more virtual than effective, clamp at effective */
186
187 vm_max_address = 0xFFFFFFFFFFFFFFFFULL >> (64 - rwidth); /* Get maximum effective address supported */
188 vm_max_physical = 0xFFFFFFFFFFFFFFFFULL >> (64 - PerProcTable[0].ppe_vaddr->pf.pfMaxPAddr); /* Get maximum physical address supported */
189
190 if(PerProcTable[0].ppe_vaddr->pf.Available & pf64Bit) { /* Are we 64 bit? */
191 tmp = 12; /* Size of hash space */
192 }
193 else {
194 __asm__ volatile("cntlzw %0, %1" : "=r" (tmp) : "r" (hash_table_size)); /* Get number of leading 0s */
195 tmp = 32 - tmp; /* Size of hash space */
196 }
197
198 incrVSID = 1 << ((tmp + 1) >> 1); /* Get ceiling of sqrt of table size */
199 incrVSID |= 1 << ((tmp + 1) >> 2); /* Get ceiling of quadroot of table size */
200 incrVSID |= 1; /* Set bit and add 1 */
201
202 return;
203
204 }
205
206
207 /*
208 * mapping_remove(pmap_t pmap, addr64_t va);
209 * Given a pmap and virtual address, this routine finds the mapping and unmaps it.
210 * The mapping block will be added to
211 * the free list. If the free list threshold is reached, garbage collection will happen.
212 *
213 * We also pass back the next higher mapped address. This is done so that the higher level
214 * pmap_remove function can release a range of addresses simply by calling mapping_remove
215 * in a loop until it finishes the range or is returned a vaddr of 0.
216 *
217 * Note that if the mapping is not found, we return the next VA ORed with 1
218 *
219 */
220
221 addr64_t mapping_remove(pmap_t pmap, addr64_t va) { /* Remove a single mapping for this VADDR
222 Returns TRUE if a mapping was found to remove */
223
224 mapping_t *mp;
225 addr64_t nextva;
226 ppnum_t pgaddr;
227
228 va &= ~PAGE_MASK; /* Scrub noise bits */
229
230 do { /* Keep trying until we truely fail */
231 mp = hw_rem_map(pmap, va, &nextva); /* Remove a mapping from this pmap */
232 } while (mapRtRemove == ((unsigned int)mp & mapRetCode));
233
234 switch ((unsigned int)mp & mapRetCode) {
235 case mapRtOK:
236 break; /* Mapping removed */
237 case mapRtNotFnd:
238 return (nextva | 1); /* Nothing found to unmap */
239 default:
240 panic("mapping_remove: hw_rem_map failed - pmap = %08X, va = %016llX, code = %08X\n",
241 pmap, va, mp);
242 break;
243 }
244
245 pgaddr = mp->mpPAddr; /* Get page number from mapping */
246
247 mapping_free(mp); /* Add mapping to the free list */
248
249 if ((pmap->pmapFlags & pmapVMhost) && pmap->pmapVmmExt) {
250 /* If this is an assisted host, scrub any guest mappings */
251 unsigned int idx;
252 phys_entry_t *physent = mapping_phys_lookup(pgaddr, &idx);
253 /* Get physent for our physical page */
254 if (!physent) { /* No physent, could be in I/O area, so exit */
255 return (nextva);
256 }
257
258 do { /* Iterate 'till all guest mappings are gone */
259 mp = hw_scrub_guest(physent, pmap); /* Attempt to scrub a guest mapping */
260 switch ((unsigned int)mp & mapRetCode) {
261 case mapRtGuest: /* Found a guest mapping */
262 case mapRtNotFnd: /* Mapping was there, but disappeared, must retry */
263 case mapRtEmpty: /* No guest mappings left to scrub */
264 break;
265 default:
266 panic("mapping_remove: hw_scrub_guest failed - physent = %08X, code = %08X\n",
267 physent, mp); /* Cry havoc, cry wrack,
268 at least we die with harness on our backs */
269 break;
270 }
271 } while (mapRtEmpty != ((unsigned int)mp & mapRetCode));
272 }
273
274 return nextva; /* Tell them we did it */
275 }
276
277 /*
278 * mapping_make(pmap, va, pa, flags, size, prot) - map a virtual address to a real one
279 *
280 * This routine takes the given parameters, builds a mapping block, and queues it into the
281 * correct lists.
282 *
283 * pmap (virtual address) is the pmap to map into
284 * va (virtual address) is the 64-bit virtual address that is being mapped
285 * pa (physical page number) is the physical page number (i.e., physcial address >> 12). This is
286 * a 32-bit quantity.
287 * Flags:
288 * block if 1, mapping is a block, size parameter is used. Note: we do not keep
289 * reference and change information or allow protection changes of blocks.
290 * any changes must first unmap and then remap the area.
291 * use attribute Use specified attributes for map, not defaults for physical page
292 * perm Mapping is permanent
293 * cache inhibited Cache inhibited (used if use attribute or block set )
294 * guarded Guarded access (used if use attribute or block set )
295 * size size of block in pages - 1 (not used if not block)
296 * prot VM protection bits
297 * attr Cachability/Guardedness
298 *
299 * Returns 0 if mapping was successful. Returns vaddr that overlaps/collides.
300 * Returns 1 for any other failure.
301 *
302 * Note that we make an assumption that all memory in the range 0f 0x0000000080000000 to 0x00000000FFFFFFFF is reserved
303 * for I/O and default the cache attrubutes appropriately. The caller is free to set whatever they want however.
304 *
305 * If there is any physical page that is not found in the physent table, the mapping is forced to be a
306 * block mapping of length 1. This keeps us from trying to update a physent during later mapping use,
307 * e.g., fault handling.
308 *
309 *
310 */
311
312 addr64_t mapping_make(pmap_t pmap, addr64_t va, ppnum_t pa, unsigned int flags, unsigned int size, vm_prot_t prot) { /* Make an address mapping */
313
314 register mapping_t *mp;
315 addr64_t colladdr, psmask;
316 unsigned int pindex, mflags, pattr, wimg, rc;
317 phys_entry_t *physent;
318 int nlists, pcf;
319 boolean_t disable_NX = FALSE;
320
321 pindex = 0;
322
323 mflags = 0x01000000; /* Start building mpFlags field (busy count = 1) */
324
325 pcf = (flags & mmFlgPcfg) >> 24; /* Get the physical page config index */
326 if(!(pPcfg[pcf].pcfFlags)) { /* Validate requested physical page configuration */
327 panic("mapping_make: invalid physical page configuration request - pmap = %08X, va = %016llX, cfg = %d\n",
328 pmap, va, pcf);
329 }
330
331 psmask = (1ULL << pPcfg[pcf].pcfPSize) - 1; /* Mask to isolate any offset into a page */
332 if(va & psmask) { /* Make sure we are page aligned on virtual */
333 panic("mapping_make: attempt to map unaligned vaddr - pmap = %08X, va = %016llX, cfg = %d\n",
334 pmap, va, pcf);
335 }
336 if(((addr64_t)pa << 12) & psmask) { /* Make sure we are page aligned on physical */
337 panic("mapping_make: attempt to map unaligned paddr - pmap = %08X, pa = %016llX, cfg = %d\n",
338 pmap, pa, pcf);
339 }
340
341 mflags |= (pcf << (31-mpPcfgb)); /* Insert physical page configuration index */
342
343 if(!(flags & mmFlgBlock)) { /* Is this a block map? */
344
345 size = 1; /* Set size to 1 page if not block */
346
347 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
348 if(!physent) { /* Did we find the physical page? */
349 mflags |= mpBlock; /* Force this to a block if no physent */
350 pattr = 0; /* Assume normal, non-I/O memory */
351 if((pa & 0xFFF80000) == 0x00080000) pattr = mmFlgCInhib | mmFlgGuarded; /* If this page is in I/O range, set I/O attributes */
352 }
353 else pattr = ((physent->ppLink & (ppI | ppG)) >> 60); /* Get the default attributes from physent */
354
355 if(flags & mmFlgUseAttr) pattr = flags & (mmFlgCInhib | mmFlgGuarded); /* Use requested attributes */
356 }
357 else { /* This is a block */
358
359 pattr = flags & (mmFlgCInhib | mmFlgGuarded); /* Use requested attributes */
360 mflags |= mpBlock; /* Show that this is a block */
361
362 if(size > pmapSmallBlock) { /* Is it one? */
363 if(size & 0x00001FFF) return mapRtBadSz; /* Fail if bigger than 256MB and not a 32MB multiple */
364 size = size >> 13; /* Convert to 32MB chunks */
365 mflags = mflags | mpBSu; /* Show 32MB basic size unit */
366 }
367 }
368
369 wimg = 0x2; /* Set basic PPC wimg to 0b0010 - Coherent */
370 if(pattr & mmFlgCInhib) wimg |= 0x4; /* Add cache inhibited if we need to */
371 if(pattr & mmFlgGuarded) wimg |= 0x1; /* Add guarded if we need to */
372
373 mflags = mflags | (pindex << 16); /* Stick in the physical entry table index */
374
375 if(flags & mmFlgPerm) mflags |= mpPerm; /* Set permanent mapping */
376
377 size = size - 1; /* Change size to offset */
378 if(size > 0xFFFF) return mapRtBadSz; /* Leave if size is too big */
379
380 nlists = mapSetLists(pmap); /* Set number of lists this will be on */
381
382 mp = mapping_alloc(nlists); /* Get a spare mapping block with this many lists */
383
384 /* the mapping is zero except that the mpLists field is set */
385 mp->mpFlags |= mflags; /* Add in the rest of the flags to mpLists */
386 mp->mpSpace = pmap->space; /* Set the address space/pmap lookup ID */
387 mp->u.mpBSize = size; /* Set the size */
388 mp->mpPte = 0; /* Set the PTE invalid */
389 mp->mpPAddr = pa; /* Set the physical page number */
390
391 if ( !nx_enabled || (pmap->pmapFlags & pmapNXdisabled) )
392 disable_NX = TRUE;
393
394 mp->mpVAddr = (va & ~mpHWFlags) | (wimg << 3) | getProtPPC(prot, disable_NX); /* Add the protection and attributes to the field */
395
396 while(1) { /* Keep trying... */
397 colladdr = hw_add_map(pmap, mp); /* Go add the mapping to the pmap */
398 rc = colladdr & mapRetCode; /* Separate return code */
399 colladdr &= ~mapRetCode; /* Clean up collision effective address */
400
401 switch (rc) {
402 case mapRtOK:
403 return mapRtOK; /* Mapping added successfully */
404
405 case mapRtRemove: /* Remove in progress */
406 (void)mapping_remove(pmap, colladdr); /* Lend a helping hand to another CPU doing block removal */
407 continue; /* Retry mapping add */
408
409 case mapRtMapDup: /* Identical mapping already present */
410 mapping_free(mp); /* Free duplicate mapping */
411 return mapRtOK; /* Return success */
412
413 case mapRtSmash: /* Mapping already present but does not match new mapping */
414 mapping_free(mp); /* Free duplicate mapping */
415 return (colladdr | mapRtSmash); /* Return colliding address, with some dirt added to avoid
416 confusion if effective address is 0 */
417 default:
418 panic("mapping_make: hw_add_map failed - collision addr = %016llX, code = %02X, pmap = %08X, va = %016llX, mapping = %08X\n",
419 colladdr, rc, pmap, va, mp); /* Die dead */
420 }
421
422 }
423
424 return 1; /* Unreachable, but pleases compiler */
425 }
426
427
428 /*
429 * mapping *mapping_find(pmap, va, *nextva, full) - Finds a mapping
430 *
431 * Looks up the vaddr and returns the mapping and the next mapped va
432 * If full is true, it will descend through all nested pmaps to find actual mapping
433 *
434 * Must be called with interruptions disabled or we can hang trying to remove found mapping.
435 *
436 * Returns 0 if not found and the virtual address of the mapping if it is
437 * Note that the mappings busy count is bumped. It is the responsibility of the caller
438 * to drop the count. If this is not done, any attempt to remove the mapping will hang.
439 *
440 * NOTE: The nextva field is not valid when full is TRUE.
441 *
442 *
443 */
444
445 mapping_t *mapping_find(pmap_t pmap, addr64_t va, addr64_t *nextva, int full) { /* Make an address mapping */
446
447 register mapping_t *mp;
448 addr64_t curva;
449 pmap_t curpmap;
450 int nestdepth;
451
452 curpmap = pmap; /* Remember entry */
453 nestdepth = 0; /* Set nest depth */
454 curva = (addr64_t)va; /* Set current va */
455
456 while(1) {
457
458 mp = hw_find_map(curpmap, curva, nextva); /* Find the mapping for this address */
459 if((unsigned int)mp == mapRtBadLk) { /* Did we lock up ok? */
460 panic("mapping_find: pmap lock failure - rc = %08X, pmap = %08X\n", mp, curpmap); /* Die... */
461 }
462
463 if(!mp || ((mp->mpFlags & mpType) < mpMinSpecial) || !full) break; /* Are we done looking? */
464
465 if((mp->mpFlags & mpType) != mpNest) { /* Don't chain through anything other than a nested pmap */
466 mapping_drop_busy(mp); /* We have everything we need from the mapping */
467 mp = 0; /* Set not found */
468 break;
469 }
470
471 if(nestdepth++ > 64) { /* Have we nested too far down? */
472 panic("mapping_find: too many nested pmaps - va = %016llX, curva = %016llX, pmap = %08X, curpmap = %08X\n",
473 va, curva, pmap, curpmap);
474 }
475
476 curva = curva + mp->mpNestReloc; /* Relocate va to new pmap */
477 curpmap = (pmap_t) pmapTrans[mp->mpSpace].pmapVAddr; /* Get the address of the nested pmap */
478 mapping_drop_busy(mp); /* We have everything we need from the mapping */
479
480 }
481
482 return mp; /* Return the mapping if we found one */
483 }
484
485 /*
486 * void mapping_protect(pmap_t pmap, addt_t va, vm_prot_t prot, addr64_t *nextva) - change the protection of a virtual page
487 *
488 * This routine takes a pmap and virtual address and changes
489 * the protection. If there are PTEs associated with the mappings, they will be invalidated before
490 * the protection is changed.
491 *
492 * We return success if we change the protection or if there is no page mapped at va. We return failure if
493 * the va corresponds to a block mapped area or the mapping is permanant.
494 *
495 *
496 */
497
498 void
499 mapping_protect(pmap_t pmap, addr64_t va, vm_prot_t prot, addr64_t *nextva) { /* Change protection of a virtual page */
500
501 int ret;
502 boolean_t disable_NX = FALSE;
503
504 if ( !nx_enabled || (pmap->pmapFlags & pmapNXdisabled) )
505 disable_NX = TRUE;
506
507 ret = hw_protect(pmap, va, getProtPPC(prot, disable_NX), nextva); /* Try to change the protect here */
508
509 switch (ret) { /* Decode return code */
510
511 case mapRtOK: /* Changed */
512 case mapRtNotFnd: /* Didn't find it */
513 case mapRtBlock: /* Block map, just ignore request */
514 case mapRtNest: /* Nested pmap, just ignore request */
515 break;
516
517 default:
518 panic("mapping_protect: hw_protect failed - rc = %d, pmap = %08X, va = %016llX\n", ret, pmap, va);
519
520 }
521
522 }
523
524 /*
525 * void mapping_protect_phys(ppnum_t pa, vm_prot_t prot) - change the protection of a physical page
526 *
527 * This routine takes a physical entry and runs through all mappings attached to it and changes
528 * the protection. If there are PTEs associated with the mappings, they will be invalidated before
529 * the protection is changed. There is no limitation on changes, e.g., higher to lower, lower to
530 * higher; however, changes to execute protection are ignored.
531 *
532 * Any mapping that is marked permanent is not changed
533 *
534 * Phys_entry is unlocked.
535 */
536
537 void mapping_protect_phys(ppnum_t pa, vm_prot_t prot) { /* Change protection of all mappings to page */
538
539 unsigned int pindex;
540 phys_entry_t *physent;
541
542 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
543 if(!physent) { /* Did we find the physical page? */
544 panic("mapping_protect_phys: invalid physical page %08X\n", pa);
545 }
546
547 hw_walk_phys(physent, hwpNoop, hwpSPrtMap, hwpNoop,
548 getProtPPC(prot, FALSE), hwpPurgePTE); /* Set the new protection for page and mappings */
549
550 return; /* Leave... */
551 }
552
553
554 /*
555 * void mapping_clr_mod(ppnum_t pa) - clears the change bit of a physical page
556 *
557 * This routine takes a physical entry and runs through all mappings attached to it and turns
558 * off the change bit.
559 */
560
561 void mapping_clr_mod(ppnum_t pa) { /* Clears the change bit of a physical page */
562
563 unsigned int pindex;
564 phys_entry_t *physent;
565
566 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
567 if(!physent) { /* Did we find the physical page? */
568 panic("mapping_clr_mod: invalid physical page %08X\n", pa);
569 }
570
571 hw_walk_phys(physent, hwpNoop, hwpCCngMap, hwpCCngPhy,
572 0, hwpPurgePTE); /* Clear change for page and mappings */
573 return; /* Leave... */
574 }
575
576
577 /*
578 * void mapping_set_mod(ppnum_t pa) - set the change bit of a physical page
579 *
580 * This routine takes a physical entry and runs through all mappings attached to it and turns
581 * on the change bit.
582 */
583
584 void mapping_set_mod(ppnum_t pa) { /* Sets the change bit of a physical page */
585
586 unsigned int pindex;
587 phys_entry_t *physent;
588
589 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
590 if(!physent) { /* Did we find the physical page? */
591 panic("mapping_set_mod: invalid physical page %08X\n", pa);
592 }
593
594 hw_walk_phys(physent, hwpNoop, hwpSCngMap, hwpSCngPhy,
595 0, hwpNoopPTE); /* Set change for page and mappings */
596 return; /* Leave... */
597 }
598
599
600 /*
601 * void mapping_clr_ref(ppnum_t pa) - clears the reference bit of a physical page
602 *
603 * This routine takes a physical entry and runs through all mappings attached to it and turns
604 * off the reference bit.
605 */
606
607 void mapping_clr_ref(ppnum_t pa) { /* Clears the reference bit of a physical page */
608
609 unsigned int pindex;
610 phys_entry_t *physent;
611
612 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
613 if(!physent) { /* Did we find the physical page? */
614 panic("mapping_clr_ref: invalid physical page %08X\n", pa);
615 }
616
617 hw_walk_phys(physent, hwpNoop, hwpCRefMap, hwpCRefPhy,
618 0, hwpPurgePTE); /* Clear reference for page and mappings */
619 return; /* Leave... */
620 }
621
622
623 /*
624 * void mapping_set_ref(ppnum_t pa) - set the reference bit of a physical page
625 *
626 * This routine takes a physical entry and runs through all mappings attached to it and turns
627 * on the reference bit.
628 */
629
630 void mapping_set_ref(ppnum_t pa) { /* Sets the reference bit of a physical page */
631
632 unsigned int pindex;
633 phys_entry_t *physent;
634
635 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
636 if(!physent) { /* Did we find the physical page? */
637 panic("mapping_set_ref: invalid physical page %08X\n", pa);
638 }
639
640 hw_walk_phys(physent, hwpNoop, hwpSRefMap, hwpSRefPhy,
641 0, hwpNoopPTE); /* Set reference for page and mappings */
642 return; /* Leave... */
643 }
644
645
646 /*
647 * boolean_t mapping_tst_mod(ppnum_t pa) - test the change bit of a physical page
648 *
649 * This routine takes a physical entry and runs through all mappings attached to it and tests
650 * the changed bit.
651 */
652
653 boolean_t mapping_tst_mod(ppnum_t pa) { /* Tests the change bit of a physical page */
654
655 unsigned int pindex, rc;
656 phys_entry_t *physent;
657
658 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
659 if(!physent) { /* Did we find the physical page? */
660 panic("mapping_tst_mod: invalid physical page %08X\n", pa);
661 }
662
663 rc = hw_walk_phys(physent, hwpTCngPhy, hwpTCngMap, hwpNoop,
664 0, hwpMergePTE); /* Set change for page and mappings */
665 return ((rc & (unsigned long)ppC) != 0); /* Leave with change bit */
666 }
667
668
669 /*
670 * boolean_t mapping_tst_ref(ppnum_t pa) - tests the reference bit of a physical page
671 *
672 * This routine takes a physical entry and runs through all mappings attached to it and tests
673 * the reference bit.
674 */
675
676 boolean_t mapping_tst_ref(ppnum_t pa) { /* Tests the reference bit of a physical page */
677
678 unsigned int pindex, rc;
679 phys_entry_t *physent;
680
681 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
682 if(!physent) { /* Did we find the physical page? */
683 panic("mapping_tst_ref: invalid physical page %08X\n", pa);
684 }
685
686 rc = hw_walk_phys(physent, hwpTRefPhy, hwpTRefMap, hwpNoop,
687 0, hwpMergePTE); /* Test reference for page and mappings */
688 return ((rc & (unsigned long)ppR) != 0); /* Leave with reference bit */
689 }
690
691
692 /*
693 * unsigned int mapping_tst_refmod(ppnum_t pa) - tests the reference and change bits of a physical page
694 *
695 * This routine takes a physical entry and runs through all mappings attached to it and tests
696 * their reference and changed bits.
697 */
698
699 unsigned int mapping_tst_refmod(ppnum_t pa) { /* Tests the reference and change bits of a physical page */
700
701 unsigned int pindex, rc;
702 phys_entry_t *physent;
703
704 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
705 if (!physent) { /* Did we find the physical page? */
706 panic("mapping_tst_refmod: invalid physical page %08X\n", pa);
707 }
708
709 rc = hw_walk_phys(physent, hwpTRefCngPhy, hwpTRefCngMap, hwpNoop,
710 0, hwpMergePTE); /* Test reference and change bits in page and mappings */
711 return (((rc & ppC)? VM_MEM_MODIFIED : 0) | ((rc & ppR)? VM_MEM_REFERENCED : 0));
712 /* Convert bits to generic format and return */
713
714 }
715
716
717 /*
718 * void mapping_clr_refmod(ppnum_t pa, unsigned int mask) - clears the reference and change bits specified
719 * by mask of a physical page
720 *
721 * This routine takes a physical entry and runs through all mappings attached to it and turns
722 * off all the reference and change bits.
723 */
724
725 void mapping_clr_refmod(ppnum_t pa, unsigned int mask) { /* Clears the reference and change bits of a physical page */
726
727 unsigned int pindex;
728 phys_entry_t *physent;
729 unsigned int ppcMask;
730
731 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
732 if(!physent) { /* Did we find the physical page? */
733 panic("mapping_clr_refmod: invalid physical page %08X\n", pa);
734 }
735
736 ppcMask = (((mask & VM_MEM_MODIFIED)? ppC : 0) | ((mask & VM_MEM_REFERENCED)? ppR : 0));
737 /* Convert mask bits to PPC-specific format */
738 hw_walk_phys(physent, hwpNoop, hwpCRefCngMap, hwpCRefCngPhy,
739 ppcMask, hwpPurgePTE); /* Clear reference and change bits for page and mappings */
740 return; /* Leave... */
741 }
742
743
744
745 /*
746 * phys_ent *mapping_phys_lookup(ppnum_t pp, unsigned int *pindex) - tests the reference bit of a physical page
747 *
748 * This routine takes a physical page number and returns the phys_entry associated with it. It also
749 * calculates the bank address associated with the entry
750 * the reference bit.
751 */
752
753 phys_entry_t *mapping_phys_lookup(ppnum_t pp, unsigned int *pindex) { /* Finds the physical entry for the page */
754
755 int i;
756
757 for(i = 0; i < pmap_mem_regions_count; i++) { /* Walk through the list */
758 if(!(unsigned int)pmap_mem_regions[i].mrPhysTab) continue; /* Skip any empty lists */
759 if((pp < pmap_mem_regions[i].mrStart) || (pp > pmap_mem_regions[i].mrEnd)) continue; /* This isn't ours */
760
761 *pindex = (i * sizeof(mem_region_t)) / 4; /* Make the word index to this list */
762
763 return &pmap_mem_regions[i].mrPhysTab[pp - pmap_mem_regions[i].mrStart]; /* Return the physent pointer */
764 }
765
766 return (phys_entry_t *)0; /* Shucks, can't find it... */
767
768 }
769
770
771
772
773 /*
774 * mapping_adjust(void) - Releases free mapping blocks and/or allocates new ones
775 *
776 * This routine frees any mapping blocks queued to mapCtl.mapcrel. It also checks
777 * the number of free mappings remaining, and if below a threshold, replenishes them.
778 * The list will be replenshed from mapCtl.mapcrel if there are enough. Otherwise,
779 * a new one is allocated.
780 *
781 * This routine allocates and/or frees memory and must be called from a safe place.
782 * Currently, vm_pageout_scan is the safest place.
783 */
784
785 thread_call_t mapping_adjust_call;
786 static thread_call_data_t mapping_adjust_call_data;
787
788 void mapping_adjust(void) { /* Adjust free mappings */
789
790 kern_return_t retr = KERN_SUCCESS;
791 mappingblok_t *mb, *mbn;
792 spl_t s;
793 int allocsize;
794
795 if(mapCtl.mapcmin <= MAPPERBLOK) {
796 mapCtl.mapcmin = (sane_size / PAGE_SIZE) / 16;
797
798 #if DEBUG
799 kprintf("mapping_adjust: minimum entries rqrd = %08X\n", mapCtl.mapcmin);
800 kprintf("mapping_adjust: free = %08X; in use = %08X; release = %08X\n",
801 mapCtl.mapcfree, mapCtl.mapcinuse, mapCtl.mapcreln);
802 #endif
803 }
804
805 s = splhigh(); /* Don't bother from now on */
806 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
807 panic("mapping_adjust - timeout getting control lock (1)\n"); /* Tell all and die */
808 }
809
810 if (mapping_adjust_call == NULL) {
811 thread_call_setup(&mapping_adjust_call_data,
812 (thread_call_func_t)mapping_adjust,
813 (thread_call_param_t)NULL);
814 mapping_adjust_call = &mapping_adjust_call_data;
815 }
816
817 while(1) { /* Keep going until we've got enough */
818
819 allocsize = mapCtl.mapcmin - mapCtl.mapcfree; /* Figure out how much we need */
820 if(allocsize < 1) break; /* Leave if we have all we need */
821
822 if((unsigned int)(mbn = mapCtl.mapcrel)) { /* Can we rescue a free one? */
823 mapCtl.mapcrel = mbn->nextblok; /* Dequeue it */
824 mapCtl.mapcreln--; /* Back off the count */
825 allocsize = MAPPERBLOK; /* Show we allocated one block */
826 }
827 else { /* No free ones, try to get it */
828
829 allocsize = (allocsize + MAPPERBLOK - 1) / MAPPERBLOK; /* Get the number of pages we need */
830
831 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
832 splx(s); /* Restore 'rupts */
833
834 for(; allocsize > 0; allocsize >>= 1) { /* Try allocating in descending halves */
835 retr = kmem_alloc_wired(mapping_map, (vm_offset_t *)&mbn, PAGE_SIZE * allocsize); /* Find a virtual address to use */
836 if((retr != KERN_SUCCESS) && (allocsize == 1)) { /* Did we find any memory at all? */
837 break;
838 }
839 if(retr == KERN_SUCCESS) break; /* We got some memory, bail out... */
840 }
841
842 allocsize = allocsize * MAPPERBLOK; /* Convert pages to number of maps allocated */
843 s = splhigh(); /* Don't bother from now on */
844 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
845 panic("mapping_adjust - timeout getting control lock (2)\n"); /* Tell all and die */
846 }
847 }
848
849 if (retr != KERN_SUCCESS)
850 break; /* Fail to alocate, bail out... */
851 for(; allocsize > 0; allocsize -= MAPPERBLOK) { /* Release one block at a time */
852 mapping_free_init((vm_offset_t)mbn, 0, 1); /* Initialize a non-permanent block */
853 mbn = (mappingblok_t *)((unsigned int)mbn + PAGE_SIZE); /* Point to the next slot */
854 }
855
856 if ((mapCtl.mapcinuse + mapCtl.mapcfree + (mapCtl.mapcreln * (MAPPERBLOK + 1))) > mapCtl.mapcmaxalloc)
857 mapCtl.mapcmaxalloc = mapCtl.mapcinuse + mapCtl.mapcfree + (mapCtl.mapcreln * (MAPPERBLOK + 1));
858 }
859
860 if(mapCtl.mapcholdoff) { /* Should we hold off this release? */
861 mapCtl.mapcrecurse = 0; /* We are done now */
862 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
863 splx(s); /* Restore 'rupts */
864 return; /* Return... */
865 }
866
867 mbn = mapCtl.mapcrel; /* Get first pending release block */
868 mapCtl.mapcrel = 0; /* Dequeue them */
869 mapCtl.mapcreln = 0; /* Set count to 0 */
870
871 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
872 splx(s); /* Restore 'rupts */
873
874 while((unsigned int)mbn) { /* Toss 'em all */
875 mb = mbn->nextblok; /* Get the next */
876
877 kmem_free(mapping_map, (vm_offset_t) mbn, PAGE_SIZE); /* Release this mapping block */
878
879 mbn = mb; /* Chain to the next */
880 }
881
882 __asm__ volatile("eieio"); /* Make sure all is well */
883 mapCtl.mapcrecurse = 0; /* We are done now */
884 return;
885 }
886
887 /*
888 * mapping_free(mapping *mp) - release a mapping to the free list
889 *
890 * This routine takes a mapping and adds it to the free list.
891 * If this mapping make the block non-empty, we queue it to the free block list.
892 * NOTE: we might want to queue it to the end to keep quelch the pathalogical
893 * case when we get a mapping and free it repeatedly causing the block to chain and unchain.
894 * If this release fills a block and we are above the threshold, we release the block
895 */
896
897 void mapping_free(struct mapping *mp) { /* Release a mapping */
898
899 mappingblok_t *mb, *mbn;
900 spl_t s;
901 unsigned int full, mindx, lists;
902
903 mindx = ((unsigned int)mp & (PAGE_SIZE - 1)) >> 6; /* Get index to mapping */
904 mb = (mappingblok_t *)((unsigned int)mp & -PAGE_SIZE); /* Point to the mapping block */
905 lists = (mp->mpFlags & mpLists); /* get #lists */
906 if ((lists == 0) || (lists > kSkipListMaxLists)) /* panic if out of range */
907 panic("mapping_free: mpLists invalid\n");
908
909 #if 0
910 mp->mpFlags = 0x99999999; /* (BRINGUP) */
911 mp->mpSpace = 0x9999; /* (BRINGUP) */
912 mp->u.mpBSize = 0x9999; /* (BRINGUP) */
913 mp->mpPte = 0x99999998; /* (BRINGUP) */
914 mp->mpPAddr = 0x99999999; /* (BRINGUP) */
915 mp->mpVAddr = 0x9999999999999999ULL; /* (BRINGUP) */
916 mp->mpAlias = 0x9999999999999999ULL; /* (BRINGUP) */
917 mp->mpList0 = 0x9999999999999999ULL; /* (BRINGUP) */
918 mp->mpList[0] = 0x9999999999999999ULL; /* (BRINGUP) */
919 mp->mpList[1] = 0x9999999999999999ULL; /* (BRINGUP) */
920 mp->mpList[2] = 0x9999999999999999ULL; /* (BRINGUP) */
921
922 if(lists > mpBasicLists) { /* (BRINGUP) */
923 mp->mpList[3] = 0x9999999999999999ULL; /* (BRINGUP) */
924 mp->mpList[4] = 0x9999999999999999ULL; /* (BRINGUP) */
925 mp->mpList[5] = 0x9999999999999999ULL; /* (BRINGUP) */
926 mp->mpList[6] = 0x9999999999999999ULL; /* (BRINGUP) */
927 mp->mpList[7] = 0x9999999999999999ULL; /* (BRINGUP) */
928 mp->mpList[8] = 0x9999999999999999ULL; /* (BRINGUP) */
929 mp->mpList[9] = 0x9999999999999999ULL; /* (BRINGUP) */
930 mp->mpList[10] = 0x9999999999999999ULL; /* (BRINGUP) */
931 }
932 #endif
933
934
935 s = splhigh(); /* Don't bother from now on */
936 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
937 panic("mapping_free - timeout getting control lock\n"); /* Tell all and die */
938 }
939
940 full = !(mb->mapblokfree[0] | mb->mapblokfree[1]); /* See if full now */
941 mb->mapblokfree[mindx >> 5] |= (0x80000000 >> (mindx & 31)); /* Flip on the free bit */
942 if ( lists > mpBasicLists ) { /* if big block, lite the 2nd bit too */
943 mindx++;
944 mb->mapblokfree[mindx >> 5] |= (0x80000000 >> (mindx & 31));
945 mapCtl.mapcfree++;
946 mapCtl.mapcinuse--;
947 }
948
949 if(full) { /* If it was full before this: */
950 mb->nextblok = mapCtl.mapcnext; /* Move head of list to us */
951 mapCtl.mapcnext = mb; /* Chain us to the head of the list */
952 if(!((unsigned int)mapCtl.mapclast))
953 mapCtl.mapclast = mb;
954 }
955
956 mapCtl.mapcfree++; /* Bump free count */
957 mapCtl.mapcinuse--; /* Decriment in use count */
958
959 mapCtl.mapcfreec++; /* Count total calls */
960
961 if(mapCtl.mapcfree > mapCtl.mapcmin) { /* Should we consider releasing this? */
962 if(((mb->mapblokfree[0] | 0x80000000) & mb->mapblokfree[1]) == 0xFFFFFFFF) { /* See if empty now */
963
964 if(mapCtl.mapcnext == mb) { /* Are we first on the list? */
965 mapCtl.mapcnext = mb->nextblok; /* Unchain us */
966 if(!((unsigned int)mapCtl.mapcnext)) mapCtl.mapclast = 0; /* If last, remove last */
967 }
968 else { /* We're not first */
969 for(mbn = mapCtl.mapcnext; mbn != 0; mbn = mbn->nextblok) { /* Search for our block */
970 if(mbn->nextblok == mb) break; /* Is the next one our's? */
971 }
972 if(!mbn) panic("mapping_free: attempt to release mapping block (%08X) not on list\n", mp);
973 mbn->nextblok = mb->nextblok; /* Dequeue us */
974 if(mapCtl.mapclast == mb) mapCtl.mapclast = mbn; /* If last, make our predecessor last */
975 }
976
977 if(mb->mapblokflags & mbPerm) { /* Is this permanently assigned? */
978 mb->nextblok = mapCtl.mapcnext; /* Move chain head to us */
979 mapCtl.mapcnext = mb; /* Chain us to the head */
980 if(!((unsigned int)mb->nextblok)) mapCtl.mapclast = mb; /* If last, make us so */
981 }
982 else {
983 mapCtl.mapcfree -= MAPPERBLOK; /* Remove the block from the free count */
984 mapCtl.mapcreln++; /* Count on release list */
985 mb->nextblok = mapCtl.mapcrel; /* Move pointer */
986 mapCtl.mapcrel = mb; /* Chain us in front */
987 }
988 }
989 }
990
991 if(mapCtl.mapcreln > MAPFRTHRSH) { /* Do we have way too many releasable mappings? */
992 if(hw_compare_and_store(0, 1, &mapCtl.mapcrecurse)) { /* Make sure we aren't recursing */
993 thread_call_enter(mapping_adjust_call); /* Go toss some */
994 }
995 }
996 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
997 splx(s); /* Restore 'rupts */
998
999 return; /* Bye, dude... */
1000 }
1001
1002
1003 /*
1004 * mapping_alloc(lists) - obtain a mapping from the free list
1005 *
1006 * This routine takes a mapping off of the free list and returns its address.
1007 * The mapping is zeroed, and its mpLists count is set. The caller passes in
1008 * the number of skiplists it would prefer; if this number is greater than
1009 * mpBasicLists (ie, 4) then we need to allocate a 128-byte mapping, which is
1010 * just two consequtive free entries coallesced into one. If we cannot find
1011 * two consequtive free entries, we clamp the list count down to mpBasicLists
1012 * and return a basic 64-byte node. Our caller never knows the difference.
1013 *
1014 * If this allocation empties a block, we remove it from the free list.
1015 * If this allocation drops the total number of free entries below a threshold,
1016 * we allocate a new block.
1017 *
1018 */
1019 decl_simple_lock_data(extern,free_pmap_lock)
1020
1021 mapping_t *
1022 mapping_alloc(int lists) { /* Obtain a mapping */
1023
1024 register mapping_t *mp;
1025 mappingblok_t *mb, *mbn;
1026 spl_t s;
1027 int mindx;
1028 int big = (lists > mpBasicLists); /* set flag if big block req'd */
1029 pmap_t refpmap, ckpmap;
1030 unsigned int space, i;
1031 addr64_t va, nextva;
1032 boolean_t found_mapping;
1033 boolean_t do_rescan;
1034
1035 s = splhigh(); /* Don't bother from now on */
1036 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
1037 panic("mapping_alloc - timeout getting control lock\n"); /* Tell all and die */
1038 }
1039
1040 if(!((unsigned int)mapCtl.mapcnext)) { /* Are there any free mappings? */
1041
1042 /*
1043 * No free mappings. First, there may be some mapping blocks on the "to be released"
1044 * list. If so, rescue one. Otherwise, try to steal a couple blocks worth.
1045 */
1046
1047 if((mbn = mapCtl.mapcrel) != 0) { /* Try to rescue a block from impending doom */
1048 mapCtl.mapcrel = mbn->nextblok; /* Pop the queue */
1049 mapCtl.mapcreln--; /* Back off the count */
1050 mapping_free_init((vm_offset_t)mbn, 0, 1); /* Initialize a non-permanent block */
1051 goto rescued;
1052 }
1053
1054 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock);
1055
1056 simple_lock(&free_pmap_lock);
1057
1058 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
1059 panic("mapping_alloc - timeout getting control lock\n"); /* Tell all and die */
1060 }
1061
1062 if (!((unsigned int)mapCtl.mapcnext)) {
1063
1064 refpmap = (pmap_t)cursor_pmap->pmap_link.next;
1065 space = mapCtl.mapcflush.spacenum;
1066 while (refpmap != cursor_pmap) {
1067 if(((pmap_t)(refpmap->pmap_link.next))->spaceNum > space) break;
1068 refpmap = (pmap_t)refpmap->pmap_link.next;
1069 }
1070
1071 ckpmap = refpmap;
1072 va = mapCtl.mapcflush.addr;
1073 found_mapping = FALSE;
1074
1075 while (mapCtl.mapcfree <= (MAPPERBLOK*2)) {
1076
1077 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock);
1078
1079 ckpmap = (pmap_t)ckpmap->pmap_link.next;
1080
1081 /* We don't steal mappings from the kernel pmap, a VMM host pmap, or a VMM guest pmap with guest
1082 shadow assist active.
1083 */
1084 if ((ckpmap->stats.resident_count != 0) && (ckpmap != kernel_pmap)
1085 && !(ckpmap->pmapFlags & (pmapVMgsaa|pmapVMhost))) {
1086 do_rescan = TRUE;
1087 for (i=0;i<8;i++) {
1088 mp = hw_purge_map(ckpmap, va, &nextva);
1089
1090 switch ((unsigned int)mp & mapRetCode) {
1091 case mapRtOK:
1092 mapping_free(mp);
1093 found_mapping = TRUE;
1094 break;
1095 case mapRtNotFnd:
1096 break;
1097 default:
1098 panic("mapping_alloc: hw_purge_map failed - pmap = %08X, va = %16llX, code = %08X\n", ckpmap, va, mp);
1099 break;
1100 }
1101
1102 if (mapRtNotFnd == ((unsigned int)mp & mapRetCode))
1103 if (do_rescan)
1104 do_rescan = FALSE;
1105 else
1106 break;
1107
1108 va = nextva;
1109 }
1110 }
1111
1112 if (ckpmap == refpmap) {
1113 if (found_mapping == FALSE)
1114 panic("no valid pmap to purge mappings\n");
1115 else
1116 found_mapping = FALSE;
1117 }
1118
1119 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
1120 panic("mapping_alloc - timeout getting control lock\n"); /* Tell all and die */
1121 }
1122
1123 }
1124
1125 mapCtl.mapcflush.spacenum = ckpmap->spaceNum;
1126 mapCtl.mapcflush.addr = nextva;
1127 }
1128
1129 simple_unlock(&free_pmap_lock);
1130 }
1131
1132 rescued:
1133
1134 mb = mapCtl.mapcnext;
1135
1136 if ( big ) { /* if we need a big (128-byte) mapping */
1137 mapCtl.mapcbig++; /* count attempts to allocate a big mapping */
1138 mbn = NULL; /* this will be prev ptr */
1139 mindx = 0;
1140 while( mb ) { /* loop over mapping blocks with free entries */
1141 mindx = mapalc2(mb); /* try for 2 consequtive free bits in this block */
1142
1143 if ( mindx ) break; /* exit loop if we found them */
1144 mbn = mb; /* remember previous block */
1145 mb = mb->nextblok; /* move on to next block */
1146 }
1147 if ( mindx == 0 ) { /* if we couldn't find 2 consequtive bits... */
1148 mapCtl.mapcbigfails++; /* count failures */
1149 big = 0; /* forget that we needed a big mapping */
1150 lists = mpBasicLists; /* clamp list count down to the max in a 64-byte mapping */
1151 mb = mapCtl.mapcnext; /* back to the first block with a free entry */
1152 }
1153 else { /* if we did find a big mapping */
1154 mapCtl.mapcfree--; /* Decrement free count twice */
1155 mapCtl.mapcinuse++; /* Bump in use count twice */
1156 if ( mindx < 0 ) { /* if we just used the last 2 free bits in this block */
1157 if (mbn) { /* if this wasn't the first block */
1158 mindx = -mindx; /* make positive */
1159 mbn->nextblok = mb->nextblok; /* unlink this one from the middle of block list */
1160 if (mb == mapCtl.mapclast) { /* if we emptied last block */
1161 mapCtl.mapclast = mbn; /* then prev block is now last */
1162 }
1163 }
1164 }
1165 }
1166 }
1167
1168 if ( !big ) { /* if we need a small (64-byte) mapping */
1169 if(!(mindx = mapalc1(mb))) /* Allocate a 1-bit slot */
1170 panic("mapping_alloc - empty mapping block detected at %08X\n", mb);
1171 }
1172
1173 if(mindx < 0) { /* Did we just take the last one */
1174 mindx = -mindx; /* Make positive */
1175 mapCtl.mapcnext = mb->nextblok; /* Remove us from the list */
1176 if(!((unsigned int)mapCtl.mapcnext)) mapCtl.mapclast = 0; /* Removed the last one */
1177 }
1178
1179 mapCtl.mapcfree--; /* Decrement free count */
1180 mapCtl.mapcinuse++; /* Bump in use count */
1181
1182 mapCtl.mapcallocc++; /* Count total calls */
1183
1184 /*
1185 * Note: in the following code, we will attempt to rescue blocks only one at a time.
1186 * Eventually, after a few more mapping_alloc calls, we will catch up. If there are none
1187 * rescueable, we will kick the misc scan who will allocate some for us. We only do this
1188 * if we haven't already done it.
1189 * For early boot, we are set up to only rescue one block at a time. This is because we prime
1190 * the release list with as much as we need until threads start.
1191 */
1192
1193 if(mapCtl.mapcfree < mapCtl.mapcmin) { /* See if we need to replenish */
1194 if((mbn = mapCtl.mapcrel) != 0) { /* Try to rescue a block from impending doom */
1195 mapCtl.mapcrel = mbn->nextblok; /* Pop the queue */
1196 mapCtl.mapcreln--; /* Back off the count */
1197 mapping_free_init((vm_offset_t)mbn, 0, 1); /* Initialize a non-permanent block */
1198 }
1199 else { /* We need to replenish */
1200 if (mapCtl.mapcfree < (mapCtl.mapcmin / 4)) {
1201 if(hw_compare_and_store(0, 1, &mapCtl.mapcrecurse)) { /* Make sure we aren't recursing */
1202 thread_call_enter(mapping_adjust_call); /* Go allocate some more */
1203 }
1204 }
1205 }
1206 }
1207
1208 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
1209 splx(s); /* Restore 'rupts */
1210
1211 mp = &((mapping_t *)mb)[mindx]; /* Point to the allocated mapping */
1212 mp->mpFlags = lists; /* set the list count */
1213
1214
1215 return mp; /* Send it back... */
1216 }
1217
1218
1219 void
1220 consider_mapping_adjust(void)
1221 {
1222 spl_t s;
1223
1224 s = splhigh(); /* Don't bother from now on */
1225 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
1226 panic("consider_mapping_adjust -- lock timeout\n");
1227 }
1228
1229 if (mapCtl.mapcfree < (mapCtl.mapcmin / 4)) {
1230 if(hw_compare_and_store(0, 1, &mapCtl.mapcrecurse)) { /* Make sure we aren't recursing */
1231 thread_call_enter(mapping_adjust_call); /* Go allocate some more */
1232 }
1233 }
1234
1235 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
1236 splx(s); /* Restore 'rupts */
1237
1238 }
1239
1240
1241
1242 /*
1243 * void mapping_free_init(mb, perm) - Adds a block of storage to the free mapping list
1244 *
1245 * The mapping block is a page size area on a page boundary. It contains 1 header and 63
1246 * mappings. This call adds and initializes a block for use. Mappings come in two sizes,
1247 * 64 and 128 bytes (the only difference is the number of skip-lists.) When we allocate a
1248 * 128-byte mapping we just look for two consequtive free 64-byte mappings, so most of the
1249 * code only deals with "basic" 64-byte mappings. This works for two reasons:
1250 * - Only one in 256 mappings is big, so they are rare.
1251 * - If we cannot find two consequtive free mappings, we just return a small one.
1252 * There is no problem with doing this, except a minor performance degredation.
1253 * Therefore, all counts etc in the mapping control structure are in units of small blocks.
1254 *
1255 * The header contains a chain link, bit maps, a virtual to real translation mask, and
1256 * some statistics. Bit maps map each slot on the page (bit 0 is not used because it
1257 * corresponds to the header). The translation mask is the XOR of the virtual and real
1258 * addresses (needless to say, the block must be wired).
1259 *
1260 * We handle these mappings the same way as saveareas: the block is only on the chain so
1261 * long as there are free entries in it.
1262 *
1263 * Empty blocks are garbage collected when there are at least mapCtl.mapcmin pages worth of free
1264 * mappings. Blocks marked PERM won't ever be released.
1265 *
1266 * If perm is negative, the mapping is initialized, but immediately queued to the mapCtl.mapcrel
1267 * list. We do this only at start up time. This is done because we only allocate blocks
1268 * in the pageout scan and it doesn't start up until after we run out of the initial mappings.
1269 * Therefore, we need to preallocate a bunch, but we don't want them to be permanent. If we put
1270 * them on the release queue, the allocate routine will rescue them. Then when the
1271 * pageout scan starts, all extra ones will be released.
1272 *
1273 */
1274
1275
1276 void mapping_free_init(vm_offset_t mbl, int perm, boolean_t locked) {
1277 /* Set's start and end of a block of mappings
1278 perm indicates if the block can be released
1279 or goes straight to the release queue .
1280 locked indicates if the lock is held already */
1281
1282 mappingblok_t *mb;
1283 spl_t s;
1284 addr64_t raddr;
1285 ppnum_t pp;
1286
1287 mb = (mappingblok_t *)mbl; /* Start of area */
1288
1289 if(perm >= 0) { /* See if we need to initialize the block */
1290 if(perm) {
1291 raddr = (addr64_t)((unsigned int)mbl); /* Perm means V=R */
1292 mb->mapblokflags = mbPerm; /* Set perm */
1293 // mb->mapblokflags |= (unsigned int)mb; /* (BRINGUP) */
1294 }
1295 else {
1296 pp = pmap_find_phys(kernel_pmap, (addr64_t)mbl); /* Get the physical page */
1297 if(!pp) { /* What gives? Where's the page? */
1298 panic("mapping_free_init: could not find translation for vaddr %016llX\n", (addr64_t)mbl);
1299 }
1300
1301 raddr = (addr64_t)pp << 12; /* Convert physical page to physical address */
1302 mb->mapblokflags = 0; /* Set not perm */
1303 // mb->mapblokflags |= (unsigned int)mb; /* (BRINGUP) */
1304 }
1305
1306 mb->mapblokvrswap = raddr ^ (addr64_t)((unsigned int)mbl); /* Form translation mask */
1307
1308 mb->mapblokfree[0] = 0x7FFFFFFF; /* Set first 32 (minus 1) free */
1309 mb->mapblokfree[1] = 0xFFFFFFFF; /* Set next 32 free */
1310 }
1311
1312 s = splhigh(); /* Don't bother from now on */
1313 if(!locked) { /* Do we need the lock? */
1314 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
1315 panic("mapping_free_init: timeout getting control lock\n"); /* Tell all and die */
1316 }
1317 }
1318
1319 if(perm < 0) { /* Direct to release queue? */
1320 mb->nextblok = mapCtl.mapcrel; /* Move forward pointer */
1321 mapCtl.mapcrel = mb; /* Queue us on in */
1322 mapCtl.mapcreln++; /* Count the free block */
1323 }
1324 else { /* Add to the free list */
1325
1326 mb->nextblok = 0; /* We always add to the end */
1327 mapCtl.mapcfree += MAPPERBLOK; /* Bump count */
1328
1329 if(!((unsigned int)mapCtl.mapcnext)) { /* First entry on list? */
1330 mapCtl.mapcnext = mapCtl.mapclast = mb; /* Chain to us */
1331 }
1332 else { /* We are not the first */
1333 mapCtl.mapclast->nextblok = mb; /* Point the last to us */
1334 mapCtl.mapclast = mb; /* We are now last */
1335 }
1336 }
1337
1338 if(!locked) { /* Do we need to unlock? */
1339 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
1340 }
1341
1342 splx(s); /* Restore 'rupts */
1343 return; /* All done, leave... */
1344 }
1345
1346
1347 /*
1348 * void mapping_prealloc(unsigned int) - Preallocates mapppings for large request
1349 *
1350 * No locks can be held, because we allocate memory here.
1351 * This routine needs a corresponding mapping_relpre call to remove the
1352 * hold off flag so that the adjust routine will free the extra mapping
1353 * blocks on the release list. I don't like this, but I don't know
1354 * how else to do this for now...
1355 *
1356 */
1357
1358 void mapping_prealloc(unsigned int size) { /* Preallocates mapppings for large request */
1359
1360 int nmapb, i;
1361 kern_return_t retr;
1362 mappingblok_t *mbn;
1363 spl_t s;
1364
1365 s = splhigh(); /* Don't bother from now on */
1366 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
1367 panic("mapping_prealloc - timeout getting control lock\n"); /* Tell all and die */
1368 }
1369
1370 nmapb = (size >> 12) + mapCtl.mapcmin; /* Get number of entries needed for this and the minimum */
1371
1372 mapCtl.mapcholdoff++; /* Bump the hold off count */
1373
1374 if((nmapb = (nmapb - mapCtl.mapcfree)) <= 0) { /* Do we already have enough? */
1375 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
1376 splx(s); /* Restore 'rupts */
1377 return;
1378 }
1379 if (!hw_compare_and_store(0, 1, &mapCtl.mapcrecurse)) { /* Make sure we aren't recursing */
1380 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
1381 splx(s); /* Restore 'rupts */
1382 return;
1383 }
1384 nmapb = (nmapb + MAPPERBLOK - 1) / MAPPERBLOK; /* Get number of blocks to get */
1385
1386 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
1387 splx(s); /* Restore 'rupts */
1388
1389 for(i = 0; i < nmapb; i++) { /* Allocate 'em all */
1390 retr = kmem_alloc_wired(mapping_map, (vm_offset_t *)&mbn, PAGE_SIZE); /* Find a virtual address to use */
1391 if(retr != KERN_SUCCESS) /* Did we get some memory? */
1392 break;
1393 mapping_free_init((vm_offset_t)mbn, -1, 0); /* Initialize on to the release queue */
1394 }
1395 if ((mapCtl.mapcinuse + mapCtl.mapcfree + (mapCtl.mapcreln * (MAPPERBLOK + 1))) > mapCtl.mapcmaxalloc)
1396 mapCtl.mapcmaxalloc = mapCtl.mapcinuse + mapCtl.mapcfree + (mapCtl.mapcreln * (MAPPERBLOK + 1));
1397
1398 mapCtl.mapcrecurse = 0; /* We are done now */
1399 }
1400
1401 /*
1402 * void mapping_relpre(void) - Releases preallocation release hold off
1403 *
1404 * This routine removes the
1405 * hold off flag so that the adjust routine will free the extra mapping
1406 * blocks on the release list. I don't like this, but I don't know
1407 * how else to do this for now...
1408 *
1409 */
1410
1411 void mapping_relpre(void) { /* Releases release hold off */
1412
1413 spl_t s;
1414
1415 s = splhigh(); /* Don't bother from now on */
1416 if(!hw_lock_to((hw_lock_t)&mapCtl.mapclock, LockTimeOut)) { /* Lock the control header */
1417 panic("mapping_relpre - timeout getting control lock\n"); /* Tell all and die */
1418 }
1419 if(--mapCtl.mapcholdoff < 0) { /* Back down the hold off count */
1420 panic("mapping_relpre: hold-off count went negative\n");
1421 }
1422
1423 hw_lock_unlock((hw_lock_t)&mapCtl.mapclock); /* Unlock our stuff */
1424 splx(s); /* Restore 'rupts */
1425 }
1426
1427 /*
1428 * void mapping_free_prime(void) - Primes the mapping block release list
1429 *
1430 * See mapping_free_init.
1431 * No locks can be held, because we allocate memory here.
1432 * One processor running only.
1433 *
1434 */
1435
1436 void mapping_free_prime(void) { /* Primes the mapping block release list */
1437
1438 int nmapb, i;
1439 kern_return_t retr;
1440 mappingblok_t *mbn;
1441 vm_offset_t mapping_min;
1442
1443 retr = kmem_suballoc(kernel_map, &mapping_min, sane_size / 16,
1444 FALSE, VM_FLAGS_ANYWHERE, &mapping_map);
1445
1446 if (retr != KERN_SUCCESS)
1447 panic("mapping_free_prime: kmem_suballoc failed");
1448
1449
1450 nmapb = (mapCtl.mapcfree + mapCtl.mapcinuse + MAPPERBLOK - 1) / MAPPERBLOK; /* Get permanent allocation */
1451 nmapb = nmapb * 4; /* Get 4 times our initial allocation */
1452
1453 #if DEBUG
1454 kprintf("mapping_free_prime: free = %08X; in use = %08X; priming = %08X\n",
1455 mapCtl.mapcfree, mapCtl.mapcinuse, nmapb);
1456 #endif
1457
1458 for(i = 0; i < nmapb; i++) { /* Allocate 'em all */
1459 retr = kmem_alloc_wired(mapping_map, (vm_offset_t *)&mbn, PAGE_SIZE); /* Find a virtual address to use */
1460 if(retr != KERN_SUCCESS) { /* Did we get some memory? */
1461 panic("Whoops... Not a bit of wired memory left for anyone\n");
1462 }
1463 mapping_free_init((vm_offset_t)mbn, -1, 0); /* Initialize onto release queue */
1464 }
1465 if ((mapCtl.mapcinuse + mapCtl.mapcfree + (mapCtl.mapcreln * (MAPPERBLOK + 1))) > mapCtl.mapcmaxalloc)
1466 mapCtl.mapcmaxalloc = mapCtl.mapcinuse + mapCtl.mapcfree + (mapCtl.mapcreln * (MAPPERBLOK + 1));
1467 }
1468
1469
1470 void
1471 mapping_fake_zone_info(int *count, vm_size_t *cur_size, vm_size_t *max_size, vm_size_t *elem_size,
1472 vm_size_t *alloc_size, int *collectable, int *exhaustable)
1473 {
1474 *count = mapCtl.mapcinuse;
1475 *cur_size = ((PAGE_SIZE / (MAPPERBLOK + 1)) * (mapCtl.mapcinuse + mapCtl.mapcfree)) + (PAGE_SIZE * mapCtl.mapcreln);
1476 *max_size = (PAGE_SIZE / (MAPPERBLOK + 1)) * mapCtl.mapcmaxalloc;
1477 *elem_size = (PAGE_SIZE / (MAPPERBLOK + 1));
1478 *alloc_size = PAGE_SIZE;
1479
1480 *collectable = 1;
1481 *exhaustable = 0;
1482 }
1483
1484
1485 /*
1486 * addr64_t mapping_p2v(pmap_t pmap, ppnum_t pa) - Finds first virtual mapping of a physical page in a space
1487 *
1488 * First looks up the physical entry associated witht the physical page. Then searches the alias
1489 * list for a matching pmap. It grabs the virtual address from the mapping, drops busy, and returns
1490 * that.
1491 *
1492 */
1493
1494 addr64_t mapping_p2v(pmap_t pmap, ppnum_t pa) { /* Finds first virtual mapping of a physical page in a space */
1495
1496 spl_t s;
1497 mapping_t *mp;
1498 unsigned int pindex;
1499 phys_entry_t *physent;
1500 addr64_t va;
1501
1502 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
1503 if(!physent) { /* Did we find the physical page? */
1504 panic("mapping_p2v: invalid physical page %08X\n", pa);
1505 }
1506
1507 s = splhigh(); /* Make sure interruptions are disabled */
1508
1509 mp = hw_find_space(physent, pmap->space); /* Go find the first mapping to the page from the requested pmap */
1510
1511 if(mp) { /* Did we find one? */
1512 va = mp->mpVAddr & -4096; /* If so, get the cleaned up vaddr */
1513 mapping_drop_busy(mp); /* Go ahead and relase the mapping now */
1514 }
1515 else va = 0; /* Return failure */
1516
1517 splx(s); /* Restore 'rupts */
1518
1519 return va; /* Bye, bye... */
1520
1521 }
1522
1523
1524 /*
1525 * kvtophys(addr)
1526 *
1527 * Convert a kernel virtual address to a physical address
1528 */
1529 addr64_t kvtophys(vm_offset_t va) {
1530
1531 return pmap_extract(kernel_pmap, va); /* Find mapping and lock the physical entry for this mapping */
1532
1533 }
1534
1535 /*
1536 * void ignore_zero_fault(boolean_t) - Sets up to ignore or honor any fault on
1537 * page 0 access for the current thread.
1538 *
1539 * If parameter is TRUE, faults are ignored
1540 * If parameter is FALSE, faults are honored
1541 *
1542 */
1543
1544 void ignore_zero_fault(boolean_t type) { /* Sets up to ignore or honor any fault on page 0 access for the current thread */
1545
1546 if(type) current_thread()->machine.specFlags |= ignoreZeroFault; /* Ignore faults on page 0 */
1547 else current_thread()->machine.specFlags &= ~ignoreZeroFault; /* Honor faults on page 0 */
1548
1549 return; /* Return the result or 0... */
1550 }
1551
1552 /*
1553 * nop in current ppc implementation
1554 */
1555 void inval_copy_windows(__unused thread_t t)
1556 {
1557 }
1558
1559
1560 /*
1561 * Copies data between a physical page and a virtual page, or 2 physical. This is used to
1562 * move data from the kernel to user state. Note that the "which" parm
1563 * says which of the parameters is physical and if we need to flush sink/source.
1564 * Note that both addresses may be physical, but only one may be virtual.
1565 *
1566 * The rules are that the size can be anything. Either address can be on any boundary
1567 * and span pages. The physical data must be contiguous as must the virtual.
1568 *
1569 * We can block when we try to resolve the virtual address at each page boundary.
1570 * We don't check protection on the physical page.
1571 *
1572 * Note that we will not check the entire range and if a page translation fails,
1573 * we will stop with partial contents copied.
1574 *
1575 */
1576
1577 kern_return_t hw_copypv_32(addr64_t source, addr64_t sink, unsigned int size, int which) {
1578
1579 vm_map_t map;
1580 kern_return_t ret;
1581 addr64_t nextva, vaddr, paddr;
1582 register mapping_t *mp;
1583 spl_t s;
1584 unsigned int lop, csize;
1585 int needtran, bothphys;
1586 unsigned int pindex;
1587 phys_entry_t *physent;
1588 vm_prot_t prot;
1589 int orig_which;
1590
1591 orig_which = which;
1592
1593 map = (which & cppvKmap) ? kernel_map : current_map_fast();
1594
1595 if((which & (cppvPsrc | cppvPsnk)) == 0 ) { /* Make sure that only one is virtual */
1596 panic("copypv: no more than 1 parameter may be virtual\n"); /* Not allowed */
1597 }
1598
1599 bothphys = 1; /* Assume both are physical */
1600
1601 if(!(which & cppvPsnk)) { /* Is sink page virtual? */
1602 vaddr = sink; /* Sink side is virtual */
1603 bothphys = 0; /* Show both aren't physical */
1604 prot = VM_PROT_READ | VM_PROT_WRITE; /* Sink always must be read/write */
1605 } else if (!(which & cppvPsrc)) { /* Is source page virtual? */
1606 vaddr = source; /* Source side is virtual */
1607 bothphys = 0; /* Show both aren't physical */
1608 prot = VM_PROT_READ; /* Virtual source is always read only */
1609 }
1610
1611 needtran = 1; /* Show we need to map the virtual the first time */
1612 s = splhigh(); /* Don't bother me */
1613
1614 while(size) {
1615
1616 if(!bothphys && (needtran || !(vaddr & 4095LL))) { /* If first time or we stepped onto a new page, we need to translate */
1617 if(!needtran) { /* If this is not the first translation, we need to drop the old busy */
1618 mapping_drop_busy(mp); /* Release the old mapping now */
1619 }
1620 needtran = 0;
1621
1622 while(1) {
1623 mp = mapping_find(map->pmap, vaddr, &nextva, 1); /* Find and busy the mapping */
1624 if(!mp) { /* Was it there? */
1625 if(getPerProc()->istackptr == 0)
1626 panic("copypv: No vaild mapping on memory %s %x", "RD", vaddr);
1627
1628 splx(s); /* Restore the interrupt level */
1629 ret = vm_fault(map, vm_map_trunc_page(vaddr), prot, FALSE, THREAD_UNINT, NULL, 0); /* Didn't find it, try to fault it in... */
1630
1631 if(ret != KERN_SUCCESS)return KERN_FAILURE; /* Didn't find any, return no good... */
1632
1633 s = splhigh(); /* Don't bother me */
1634 continue; /* Go try for the map again... */
1635
1636 }
1637 if (mp->mpVAddr & mpI) { /* cache inhibited, so force the appropriate page to be flushed before */
1638 if (which & cppvPsrc) /* and after the copy to avoid cache paradoxes */
1639 which |= cppvFsnk;
1640 else
1641 which |= cppvFsrc;
1642 } else
1643 which = orig_which;
1644
1645 /* Note that we have to have the destination writable. So, if we already have it, or we are mapping the source,
1646 we can just leave.
1647 */
1648 if((which & cppvPsnk) || !(mp->mpVAddr & 1)) break; /* We got it mapped R/W or the source is not virtual, leave... */
1649
1650 mapping_drop_busy(mp); /* Go ahead and release the mapping for now */
1651 if(getPerProc()->istackptr == 0)
1652 panic("copypv: No vaild mapping on memory %s %x", "RDWR", vaddr);
1653 splx(s); /* Restore the interrupt level */
1654
1655 ret = vm_fault(map, vm_map_trunc_page(vaddr), VM_PROT_READ | VM_PROT_WRITE, FALSE, THREAD_UNINT, NULL, 0); /* check for a COW area */
1656 if (ret != KERN_SUCCESS) return KERN_FAILURE; /* We couldn't get it R/W, leave in disgrace... */
1657 s = splhigh(); /* Don't bother me */
1658 }
1659 paddr = ((addr64_t)mp->mpPAddr << 12) + (vaddr - (mp->mpVAddr & -4096LL)); /* construct the physical address... this calculation works */
1660 /* properly on both single page and block mappings */
1661 if(which & cppvPsrc) sink = paddr; /* If source is physical, then the sink is virtual */
1662 else source = paddr; /* Otherwise the source is */
1663 }
1664
1665 lop = (unsigned int)(4096LL - (sink & 4095LL)); /* Assume sink smallest */
1666 if(lop > (unsigned int)(4096LL - (source & 4095LL))) lop = (unsigned int)(4096LL - (source & 4095LL)); /* No, source is smaller */
1667
1668 csize = size; /* Assume we can copy it all */
1669 if(lop < size) csize = lop; /* Nope, we can't do it all */
1670
1671 if(which & cppvFsrc) flush_dcache64(source, csize, 1); /* If requested, flush source before move */
1672 if(which & cppvFsnk) flush_dcache64(sink, csize, 1); /* If requested, flush sink before move */
1673
1674 bcopy_physvir_32(source, sink, csize); /* Do a physical copy, virtually */
1675
1676 if(which & cppvFsrc) flush_dcache64(source, csize, 1); /* If requested, flush source after move */
1677 if(which & cppvFsnk) flush_dcache64(sink, csize, 1); /* If requested, flush sink after move */
1678
1679 /*
1680 * Note that for certain ram disk flavors, we may be copying outside of known memory.
1681 * Therefore, before we try to mark it modifed, we check if it exists.
1682 */
1683
1684 if( !(which & cppvNoModSnk)) {
1685 physent = mapping_phys_lookup(sink >> 12, &pindex); /* Get physical entry for sink */
1686 if(physent) mapping_set_mod((ppnum_t)(sink >> 12)); /* Make sure we know that it is modified */
1687 }
1688 if( !(which & cppvNoRefSrc)) {
1689 physent = mapping_phys_lookup(source >> 12, &pindex); /* Get physical entry for source */
1690 if(physent) mapping_set_ref((ppnum_t)(source >> 12)); /* Make sure we know that it is modified */
1691 }
1692 size = size - csize; /* Calculate what is left */
1693 vaddr = vaddr + csize; /* Move to next sink address */
1694 source = source + csize; /* Bump source to next physical address */
1695 sink = sink + csize; /* Bump sink to next physical address */
1696 }
1697
1698 if(!bothphys) mapping_drop_busy(mp); /* Go ahead and release the mapping of the virtual page if any */
1699 splx(s); /* Open up for interrupts */
1700
1701 return KERN_SUCCESS;
1702 }
1703
1704
1705 /*
1706 * Debug code
1707 */
1708
1709 void mapping_verify(void) {
1710
1711 spl_t s;
1712 mappingblok_t *mb, *mbn;
1713 unsigned int relncnt;
1714 unsigned int dumbodude;
1715
1716 dumbodude = 0;
1717
1718 s = splhigh(); /* Don't bother from now on */
1719
1720 mbn = 0; /* Start with none */
1721 for(mb = mapCtl.mapcnext; mb; mb = mb->nextblok) { /* Walk the free chain */
1722 if((mappingblok_t *)(mb->mapblokflags & 0x7FFFFFFF) != mb) { /* Is tag ok? */
1723 panic("mapping_verify: flags tag bad, free chain; mb = %08X, tag = %08X\n", mb, mb->mapblokflags);
1724 }
1725 mbn = mb; /* Remember the last one */
1726 }
1727
1728 if(mapCtl.mapcnext && (mapCtl.mapclast != mbn)) { /* Do we point to the last one? */
1729 panic("mapping_verify: last pointer bad; mb = %08X, mapclast = %08X\n", mb, mapCtl.mapclast);
1730 }
1731
1732 relncnt = 0; /* Clear count */
1733 for(mb = mapCtl.mapcrel; mb; mb = mb->nextblok) { /* Walk the release chain */
1734 dumbodude |= mb->mapblokflags; /* Just touch it to make sure it is mapped */
1735 relncnt++; /* Count this one */
1736 }
1737
1738 if(mapCtl.mapcreln != relncnt) { /* Is the count on release queue ok? */
1739 panic("mapping_verify: bad release queue count; mapcreln = %d, cnt = %d, ignore this = %08X\n", mapCtl.mapcreln, relncnt, dumbodude);
1740 }
1741
1742 splx(s); /* Restore 'rupts */
1743
1744 return;
1745 }
1746
1747 void mapping_phys_unused(ppnum_t pa) {
1748
1749 unsigned int pindex;
1750 phys_entry_t *physent;
1751
1752 physent = mapping_phys_lookup(pa, &pindex); /* Get physical entry */
1753 if(!physent) return; /* Did we find the physical page? */
1754
1755 if(!(physent->ppLink & ~(ppLock | ppFlags))) return; /* No one else is here */
1756
1757 panic("mapping_phys_unused: physical page (%08X) in use, physent = %08X\n", pa, physent);
1758
1759 }
1760
1761 void mapping_hibernate_flush(void)
1762 {
1763 int bank;
1764 unsigned int page;
1765 struct phys_entry * entry;
1766
1767 for (bank = 0; bank < pmap_mem_regions_count; bank++)
1768 {
1769 entry = (struct phys_entry *) pmap_mem_regions[bank].mrPhysTab;
1770 for (page = pmap_mem_regions[bank].mrStart; page <= pmap_mem_regions[bank].mrEnd; page++)
1771 {
1772 hw_walk_phys(entry, hwpNoop, hwpNoop, hwpNoop, 0, hwpPurgePTE);
1773 entry++;
1774 }
1775 }
1776 }
1777
1778
1779
1780
1781
1782