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