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