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