]> git.saurik.com Git - apple/xnu.git/blob - osfmk/ppc/savearea.c
xnu-517.7.7.tar.gz
[apple/xnu.git] / osfmk / ppc / savearea.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * This file is used to maintain the exception save areas
24 *
25 */
26
27 #include <cpus.h>
28 #include <debug.h>
29 #include <mach_kgdb.h>
30 #include <mach_vm_debug.h>
31
32 #include <kern/thread.h>
33 #include <mach/vm_attributes.h>
34 #include <mach/vm_param.h>
35 #include <vm/vm_kern.h>
36 #include <vm/vm_map.h>
37 #include <vm/vm_page.h>
38 #include <mach/ppc/thread_status.h>
39 #include <kern/spl.h>
40 #include <kern/simple_lock.h>
41
42 #include <kern/misc_protos.h>
43 #include <ppc/misc_protos.h>
44 #include <ppc/proc_reg.h>
45 #include <ppc/mem.h>
46 #include <ppc/pmap.h>
47 #include <ppc/Firmware.h>
48 #include <ppc/mappings.h>
49 #include <ppc/exception.h>
50 #include <ppc/savearea.h>
51 #include <ddb/db_output.h>
52
53
54 extern struct Saveanchor saveanchor; /* Aliged savearea anchor */
55 struct Saveanchor backpocket; /* Emergency saveareas */
56 unsigned int debsave0 = 0; /* Debug flag */
57 unsigned int backchain = 0; /* Debug flag */
58
59 /*
60 * These routines keep track of exception save areas and keeps the count within specific limits. If there are
61 * too few, more are allocated, too many, and they are released. This savearea is where the PCBs are
62 * stored. They never span a page boundary and are referenced by both virtual and real addresses.
63 * Within the interrupt vectors, the real address is used because at that level, no exceptions
64 * can be tolerated. Save areas can be dynamic or permanent. Permanant saveareas are allocated
65 * at boot time and must be in place before any type of exception occurs. These are never released,
66 * and the number is based upon some arbitrary (yet to be determined) amount times the number of
67 * processors. This represents the minimum number required to process a total system failure without
68 * destroying valuable and ever-so-handy system debugging information.
69 *
70 * We keep two global free lists (the savearea free pool and the savearea free list) and one local
71 * list per processor.
72 *
73 * The local lists are small and require no locked access. They are chained using physical addresses
74 * and no interruptions are allowed when adding to or removing from the list. Also known as the
75 * qfret list. This list is local to a processor and is intended for use only by very low level
76 * context handling code.
77 *
78 * The savearea free list is a medium size list that is globally accessible. It is updated
79 * while holding a simple lock. The length of time that the lock is held is kept short. The
80 * longest period of time is when the list is trimmed. Like the qfret lists, this is chained physically
81 * and must be accessed with translation and interruptions disabled. This is where the bulk
82 * of the free entries are located.
83 *
84 * The saveareas are allocated from full pages. A pool element is marked
85 * with an allocation map that shows which "slots" are free. These pages are allocated via the
86 * normal kernel memory allocation functions. Queueing is with physical addresses. The enqueue,
87 * dequeue, and search for free blocks is done under free list lock.
88 * only if there are empty slots in it.
89 *
90 * Saveareas that are counted as "in use" once they are removed from the savearea free list.
91 * This means that all areas on the local qfret list are considered in use.
92 *
93 * There are two methods of obtaining a savearea. The save_get function (which is also inlined
94 * in the low-level exception handler) attempts to get an area from the local qfret list. This is
95 * done completely without locks. If qfret is exahusted (or maybe just too low) an area is allocated
96 * from the savearea free list. If the free list is empty, we install the back pocket areas and
97 * panic.
98 *
99 * The save_alloc function is designed to be called by high level routines, e.g., thread creation,
100 * etc. It will allocate from the free list. After allocation, it will compare the free count
101 * to the target value. If outside of the range, it will adjust the size either upwards or
102 * downwards.
103 *
104 * If we need to shrink the list, it will be trimmed to the target size and unlocked. The code
105 * will walk the chain and return each savearea to its pool page. If a pool page becomes
106 * completely empty, it is dequeued from the free pool list and enqueued (atomic queue
107 * function) to be released.
108 *
109 * Once the trim list is finished, the pool release queue is checked to see if there are pages
110 * waiting to be released. If so, they are released one at a time.
111 *
112 * If the free list needed to be grown rather than shrunken, we will first attempt to recover
113 * a page from the pending release queue (built when we trim the free list). If we find one,
114 * it is allocated, otherwise, a page of kernel memory is allocated. This loops until there are
115 * enough free saveareas.
116 *
117 */
118
119
120
121 /*
122 * Allocate our initial context save areas. As soon as we do this,
123 * we can take an interrupt. We do the saveareas here, 'cause they're guaranteed
124 * to be at least page aligned.
125 *
126 * Note: these initial saveareas are all to be allocated from V=R, less than 4GB
127 * space.
128 */
129
130
131 void savearea_init(vm_offset_t addr) {
132
133 savearea_comm *savec;
134 vm_offset_t save;
135 int i;
136
137
138 saveanchor.savetarget = InitialSaveTarget; /* Initial target value */
139 saveanchor.saveinuse = 0; /* Number of areas in use */
140
141 saveanchor.savefree = 0; /* Remember the start of the free chain */
142 saveanchor.savefreecnt = 0; /* Remember the length */
143 saveanchor.savepoolfwd = (addr64_t)&saveanchor; /* Remember pool forward */
144 saveanchor.savepoolbwd = (addr64_t)&saveanchor; /* Remember pool backward */
145
146 save = addr; /* Point to the whole block of blocks */
147
148 /*
149 * First we allocate the back pocket in case of emergencies
150 */
151
152
153 for(i=0; i < BackPocketSaveBloks; i++) { /* Initialize the back pocket saveareas */
154
155 savec = (savearea_comm *)save; /* Get the control area for this one */
156
157 savec->sac_alloc = 0; /* Mark it allocated */
158 savec->sac_vrswap = 0; /* V=R, so the translation factor is 0 */
159 savec->sac_flags = sac_perm; /* Mark it permanent */
160 savec->sac_flags |= 0x0000EE00; /* Debug eyecatcher */
161 save_queue((uint32_t)savec >> 12); /* Add page to savearea lists */
162 save += PAGE_SIZE; /* Jump up to the next one now */
163
164 }
165
166 backpocket = saveanchor; /* Save this for emergencies */
167
168
169 /*
170 * We've saved away the back pocket savearea info, so reset it all and
171 * now allocate for real
172 */
173
174
175 saveanchor.savefree = 0; /* Remember the start of the free chain */
176 saveanchor.savefreecnt = 0; /* Remember the length */
177 saveanchor.saveadjust = 0; /* Set none needed yet */
178 saveanchor.savepoolfwd = (addr64_t)&saveanchor; /* Remember pool forward */
179 saveanchor.savepoolbwd = (addr64_t)&saveanchor; /* Remember pool backward */
180
181 for(i=0; i < InitialSaveBloks; i++) { /* Initialize the saveareas */
182
183 savec = (savearea_comm *)save; /* Get the control area for this one */
184
185 savec->sac_alloc = 0; /* Mark it allocated */
186 savec->sac_vrswap = 0; /* V=R, so the translation factor is 0 */
187 savec->sac_flags = sac_perm; /* Mark it permanent */
188 savec->sac_flags |= 0x0000EE00; /* Debug eyecatcher */
189 save_queue((uint32_t)savec >> 12); /* Add page to savearea lists */
190 save += PAGE_SIZE; /* Jump up to the next one now */
191
192 }
193
194 /*
195 * We now have a free list that has our initial number of entries
196 * The local qfret lists is empty. When we call save_get below it will see that
197 * the local list is empty and fill it for us.
198 *
199 * It is ok to call save_get here because all initial saveareas are V=R in less
200 * than 4GB space, so 32-bit addressing is ok.
201 *
202 */
203
204 /*
205 * This will populate the local list and get the first one for the system
206 */
207 per_proc_info[0].next_savearea = (vm_offset_t)save_get();
208
209 /*
210 * The system is now able to take interruptions
211 */
212 return;
213 }
214
215
216
217
218 /*
219 * Obtains a savearea. If the free list needs size adjustment it happens here.
220 * Don't actually allocate the savearea until after the adjustment is done.
221 */
222
223 struct savearea *save_alloc(void) { /* Reserve a save area */
224
225
226 if(saveanchor.saveadjust) save_adjust(); /* If size need adjustment, do it now */
227
228 return save_get(); /* Pass the baby... */
229 }
230
231
232 /*
233 * This routine releases a save area to the free queue. If after that, we have more than our maximum target,
234 * we start releasing what we can until we hit the normal target.
235 */
236
237
238
239 void save_release(struct savearea *save) { /* Release a save area */
240
241 save_ret(save); /* Return a savearea to the free list */
242
243 if(saveanchor.saveadjust) save_adjust(); /* Adjust the savearea free list and pool size if needed */
244
245 return;
246
247 }
248
249
250 /*
251 * Adjusts the size of the free list. Can either release or allocate full pages
252 * of kernel memory. This can block.
253 *
254 * Note that we will only run one adjustment and the amount needed may change
255 * while we are executing.
256 *
257 * Calling this routine is triggered by saveanchor.saveadjust. This value is always calculated just before
258 * we unlock the saveanchor lock (this keeps it pretty accurate). If the total of savefreecnt and saveinuse
259 * is within the hysteresis range, it is set to 0. If outside, it is set to the number needed to bring
260 * the total to the target value. Note that there is a minimum size to the free list (FreeListMin) and if
261 * savefreecnt falls below that, saveadjust is set to the number needed to bring it to that.
262 */
263
264
265 void save_adjust(void) {
266
267 savearea_comm *sctl, *sctlnext, *freepage;
268 kern_return_t ret;
269 uint64_t vtopmask;
270 ppnum_t physpage;
271
272 if(saveanchor.saveadjust < 0) { /* Do we need to adjust down? */
273
274 sctl = (savearea_comm *)save_trim_free(); /* Trim list to the need count, return start of trim list */
275
276 while(sctl) { /* Release the free pages back to the kernel */
277 sctlnext = CAST_DOWN(savearea_comm *, sctl->save_prev); /* Get next in list */
278 kmem_free(kernel_map, (vm_offset_t) sctl, PAGE_SIZE); /* Release the page */
279 sctl = sctlnext; /* Chain onwards */
280 }
281 }
282 else { /* We need more... */
283
284 if(save_recover()) return; /* If we can recover enough from the pool, return */
285
286 while(saveanchor.saveadjust > 0) { /* Keep going until we have enough */
287
288 ret = kmem_alloc_wired(kernel_map, (vm_offset_t *)&freepage, PAGE_SIZE); /* Get a page for free pool */
289 if(ret != KERN_SUCCESS) { /* Did we get some memory? */
290 panic("Whoops... Not a bit of wired memory left for saveareas\n");
291 }
292
293 physpage = pmap_find_phys(kernel_pmap, (vm_offset_t)freepage); /* Find physical page */
294 if(!physpage) { /* See if we actually have this mapped*/
295 panic("save_adjust: wired page not mapped - va = %08X\n", freepage); /* Die */
296 }
297
298 bzero((void *)freepage, PAGE_SIZE); /* Clear it all to zeros */
299 freepage->sac_alloc = 0; /* Mark all entries taken */
300 freepage->sac_vrswap = ((uint64_t)physpage << 12) ^ (uint64_t)((uintptr_t)freepage); /* XOR to calculate conversion mask */
301
302 freepage->sac_flags |= 0x0000EE00; /* Set debug eyecatcher */
303
304 save_queue(physpage); /* Add all saveareas on page to free list */
305 }
306 }
307 }
308
309 /*
310 * Fake up information to make the saveareas look like a zone
311 */
312
313 save_fake_zone_info(int *count, vm_size_t *cur_size, vm_size_t *max_size, vm_size_t *elem_size,
314 vm_size_t *alloc_size, int *collectable, int *exhaustable)
315 {
316 *count = saveanchor.saveinuse;
317 *cur_size = (saveanchor.savefreecnt + saveanchor.saveinuse) * (PAGE_SIZE / sac_cnt);
318 *max_size = saveanchor.savemaxcount * (PAGE_SIZE / sac_cnt);
319 *elem_size = sizeof(savearea);
320 *alloc_size = PAGE_SIZE;
321 *collectable = 1;
322 *exhaustable = 0;
323 }
324
325