]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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 License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | ||
57 | #include <mach_assert.h> | |
58 | #include <mach_kdb.h> | |
59 | #include <mach_kgdb.h> | |
60 | #include <mach_kdp.h> | |
1c79356b A |
61 | |
62 | #include <kern/cpu_number.h> | |
63 | #include <kern/lock.h> | |
64 | #include <kern/spl.h> | |
65 | #include <kern/thread.h> | |
66 | #include <kern/assert.h> | |
67 | #include <kern/sched_prim.h> | |
68 | #include <kern/misc_protos.h> | |
9bccf70c | 69 | #include <vm/vm_kern.h> |
91447636 | 70 | #include <vm/pmap.h> |
1c79356b | 71 | #include <stdarg.h> |
2d21ac55 A |
72 | #if !MACH_KDP |
73 | #include <kdp/kdp_udp.h> | |
74 | #endif | |
1c79356b | 75 | |
9bccf70c A |
76 | #ifdef __ppc__ |
77 | #include <ppc/Firmware.h> | |
78 | #include <ppc/low_trace.h> | |
79 | #endif | |
80 | ||
2d21ac55 A |
81 | #ifdef __i386__ |
82 | #include <i386/cpu_threads.h> | |
83 | #include <i386/pmCPU.h> | |
84 | #endif | |
85 | ||
86 | #include <IOKit/IOPlatformExpert.h> | |
87 | ||
1c79356b A |
88 | unsigned int halt_in_debugger = 0; |
89 | unsigned int switch_debugger = 0; | |
90 | unsigned int current_debugger = 0; | |
91 | unsigned int active_debugger = 0; | |
92 | unsigned int debug_mode=0; | |
2d21ac55 | 93 | unsigned int disable_debug_output = TRUE; |
1c79356b | 94 | unsigned int systemLogDiags = FALSE; |
91447636 | 95 | unsigned int panicDebugging = FALSE; |
2d21ac55 | 96 | unsigned int logPanicDataToScreen = FALSE; |
1c79356b A |
97 | |
98 | int mach_assert = 1; | |
99 | ||
55e303ae | 100 | const char *panicstr = (char *) 0; |
1c79356b A |
101 | decl_simple_lock_data(,panic_lock) |
102 | int paniccpu; | |
103 | volatile int panicwait; | |
91447636 | 104 | volatile unsigned int nestedpanic= 0; |
1c79356b A |
105 | unsigned int panic_is_inited = 0; |
106 | unsigned int return_on_panic = 0; | |
91447636 | 107 | unsigned long panic_caller; |
1c79356b | 108 | |
9bccf70c | 109 | char *debug_buf; |
91447636 | 110 | ppnum_t debug_buf_page; |
9bccf70c | 111 | char *debug_buf_ptr; |
2d21ac55 A |
112 | unsigned int debug_buf_size; |
113 | ||
114 | static char model_name[64]; | |
115 | ||
116 | struct pasc { | |
117 | unsigned a: 7; | |
118 | unsigned b: 7; | |
119 | unsigned c: 7; | |
120 | unsigned d: 7; | |
121 | unsigned e: 7; | |
122 | unsigned f: 7; | |
123 | unsigned g: 7; | |
124 | unsigned h: 7; | |
125 | } __attribute__((packed)); | |
126 | ||
127 | typedef struct pasc pasc_t; | |
9bccf70c | 128 | |
1c79356b A |
129 | void |
130 | Assert( | |
131 | const char *file, | |
132 | int line, | |
2d21ac55 A |
133 | #if CONFIG_NO_PANIC_STRINGS |
134 | __unused const char *expression | |
135 | #else | |
136 | const char *expression | |
137 | #endif | |
138 | ) | |
1c79356b | 139 | { |
2d21ac55 A |
140 | int saved_return_on_panic; |
141 | ||
1c79356b A |
142 | if (!mach_assert) { |
143 | return; | |
144 | } | |
2d21ac55 A |
145 | |
146 | saved_return_on_panic = return_on_panic; | |
147 | return_on_panic = 1; | |
148 | ||
149 | panic_plain("%s:%d Assertion failed: %s", file, line, expression); | |
150 | ||
151 | return_on_panic = saved_return_on_panic; | |
1c79356b A |
152 | } |
153 | ||
154 | /* | |
155 | * Carefully use the panic_lock. There's always a chance that | |
156 | * somehow we'll call panic before getting to initialize the | |
157 | * panic_lock -- in this case, we'll assume that the world is | |
158 | * in uniprocessor mode and just avoid using the panic lock. | |
159 | */ | |
160 | #define PANIC_LOCK() \ | |
161 | MACRO_BEGIN \ | |
162 | if (panic_is_inited) \ | |
163 | simple_lock(&panic_lock); \ | |
164 | MACRO_END | |
165 | ||
166 | #define PANIC_UNLOCK() \ | |
167 | MACRO_BEGIN \ | |
168 | if (panic_is_inited) \ | |
169 | simple_unlock(&panic_lock); \ | |
170 | MACRO_END | |
171 | ||
172 | ||
173 | void | |
174 | panic_init(void) | |
175 | { | |
91447636 | 176 | simple_lock_init(&panic_lock, 0); |
1c79356b | 177 | panic_is_inited = 1; |
91447636 | 178 | panic_caller = 0; |
1c79356b A |
179 | } |
180 | ||
2d21ac55 A |
181 | void |
182 | debug_log_init(void) | |
183 | { | |
184 | if (debug_buf_size != 0) | |
185 | return; | |
186 | if (kmem_alloc(kernel_map, (vm_offset_t *) &debug_buf, PAGE_SIZE) | |
187 | != KERN_SUCCESS) | |
188 | panic("cannot allocate debug_buf\n"); | |
189 | debug_buf_ptr = debug_buf; | |
190 | debug_buf_size = PAGE_SIZE; | |
191 | debug_buf_page = pmap_find_phys(kernel_pmap, | |
192 | (addr64_t)(uintptr_t)debug_buf_ptr); | |
193 | } | |
194 | ||
195 | #if __i386__ | |
196 | #define panic_stop() pmCPUHalt(PM_HALT_PANIC) | |
197 | #define panic_safe() pmSafeMode(x86_lcpu(), PM_SAFE_FL_SAFE) | |
198 | #define panic_normal() pmSafeMode(x86_lcpu(), PM_SAFE_FL_NORMAL) | |
199 | #else | |
200 | #define panic_stop() { while (1) ; } | |
201 | #define panic_safe() | |
202 | #define panic_normal() | |
203 | #endif | |
204 | ||
205 | #undef panic(...) | |
1c79356b A |
206 | void |
207 | panic(const char *str, ...) | |
208 | { | |
209 | va_list listp; | |
210 | spl_t s; | |
211 | thread_t thread; | |
91447636 | 212 | wait_queue_t wq; |
1c79356b A |
213 | |
214 | s = splhigh(); | |
55e303ae | 215 | disable_preemption(); |
1c79356b | 216 | |
2d21ac55 A |
217 | panic_safe(); |
218 | ||
9bccf70c A |
219 | #ifdef __ppc__ |
220 | lastTrace = LLTraceSet(0); /* Disable low-level tracing */ | |
221 | #endif | |
222 | ||
1c79356b | 223 | thread = current_thread(); /* Get failing thread */ |
91447636 | 224 | wq = thread->wait_queue; /* Save the old value */ |
2d21ac55 | 225 | thread->wait_queue = NULL; /* Clear the wait so we do not get double panics when we try locks */ |
1c79356b | 226 | |
9bccf70c | 227 | if( logPanicDataToScreen ) |
2d21ac55 | 228 | disable_debug_output = FALSE; |
9bccf70c | 229 | |
1c79356b | 230 | debug_mode = TRUE; |
91447636 A |
231 | |
232 | /* panic_caller is initialized to 0. If set, don't change it */ | |
233 | if ( ! panic_caller ) | |
2d21ac55 | 234 | panic_caller = (unsigned long)(char *)__builtin_return_address(0); |
91447636 | 235 | |
1c79356b A |
236 | restart: |
237 | PANIC_LOCK(); | |
238 | if (panicstr) { | |
239 | if (cpu_number() != paniccpu) { | |
240 | PANIC_UNLOCK(); | |
241 | /* | |
242 | * Wait until message has been printed to identify correct | |
243 | * cpu that made the first panic. | |
244 | */ | |
245 | while (panicwait) | |
246 | continue; | |
247 | goto restart; | |
248 | } else { | |
249 | nestedpanic +=1; | |
250 | PANIC_UNLOCK(); | |
251 | Debugger("double panic"); | |
1c79356b | 252 | printf("double panic: We are hanging here...\n"); |
2d21ac55 | 253 | panic_stop(); |
1c79356b A |
254 | /* NOTREACHED */ |
255 | } | |
256 | } | |
257 | panicstr = str; | |
258 | paniccpu = cpu_number(); | |
259 | panicwait = 1; | |
260 | ||
261 | PANIC_UNLOCK(); | |
2d21ac55 A |
262 | kdb_printf("panic(cpu %d caller 0x%08lX): ", (unsigned) paniccpu, panic_caller); |
263 | if (str) { | |
264 | va_start(listp, str); | |
265 | _doprnt(str, &listp, consdebug_putc, 0); | |
266 | va_end(listp); | |
267 | } | |
9bccf70c | 268 | kdb_printf("\n"); |
1c79356b A |
269 | |
270 | /* | |
271 | * Release panicwait indicator so that other cpus may call Debugger(). | |
272 | */ | |
273 | panicwait = 0; | |
274 | Debugger("panic"); | |
275 | /* | |
276 | * Release panicstr so that we can handle normally other panics. | |
277 | */ | |
278 | PANIC_LOCK(); | |
279 | panicstr = (char *)0; | |
280 | PANIC_UNLOCK(); | |
91447636 | 281 | thread->wait_queue = wq; /* Restore the wait queue */ |
2d21ac55 | 282 | |
55e303ae | 283 | if (return_on_panic) { |
2d21ac55 | 284 | panic_normal(); |
55e303ae A |
285 | enable_preemption(); |
286 | splx(s); | |
1c79356b | 287 | return; |
55e303ae | 288 | } |
2d21ac55 | 289 | |
9bccf70c | 290 | kdb_printf("panic: We are hanging here...\n"); |
2d21ac55 | 291 | panic_stop(); |
1c79356b A |
292 | /* NOTREACHED */ |
293 | } | |
294 | ||
295 | void | |
2d21ac55 | 296 | log(__unused int level, char *fmt, ...) |
1c79356b A |
297 | { |
298 | va_list listp; | |
1c79356b A |
299 | |
300 | #ifdef lint | |
301 | level++; | |
302 | #endif /* lint */ | |
303 | #ifdef MACH_BSD | |
304 | disable_preemption(); | |
305 | va_start(listp, fmt); | |
306 | _doprnt(fmt, &listp, conslog_putc, 0); | |
307 | va_end(listp); | |
308 | enable_preemption(); | |
309 | #endif | |
310 | } | |
9bccf70c | 311 | |
9bccf70c A |
312 | void |
313 | debug_putc(char c) | |
314 | { | |
2d21ac55 A |
315 | if ((debug_buf_size != 0) && |
316 | ((debug_buf_ptr-debug_buf) < (int)debug_buf_size)) { | |
9bccf70c A |
317 | *debug_buf_ptr=c; |
318 | debug_buf_ptr++; | |
319 | } | |
320 | } | |
2d21ac55 A |
321 | |
322 | /* In-place packing routines -- inefficient, but they're called at most once. | |
323 | */ | |
324 | ||
325 | int packA(char *inbuf, uint32_t length, uint32_t buflen) | |
326 | { | |
327 | unsigned int i, j = 0; | |
328 | pasc_t pack; | |
329 | ||
330 | length = MIN(((length & ~7) +8), buflen); | |
331 | ||
332 | for (i = 0; i < length; i+=8) | |
333 | { | |
334 | pack.a = inbuf[i]; | |
335 | pack.b = inbuf[i+1]; | |
336 | pack.c = inbuf[i+2]; | |
337 | pack.d = inbuf[i+3]; | |
338 | pack.e = inbuf[i+4]; | |
339 | pack.f = inbuf[i+5]; | |
340 | pack.g = inbuf[i+6]; | |
341 | pack.h = inbuf[i+7]; | |
342 | bcopy ((char *) &pack, inbuf + j, 7); | |
343 | j += 7; | |
344 | } | |
345 | return ((length * 7)/8); | |
346 | } | |
347 | ||
348 | void unpackA(char *inbuf, uint32_t length) | |
349 | { | |
350 | pasc_t packs; | |
351 | unsigned i = 0; | |
352 | length = (length * 8)/7; | |
353 | ||
354 | while (i < length) { | |
355 | packs = *(pasc_t *)&inbuf[i]; | |
356 | bcopy(&inbuf[i+7], &inbuf[i+8], MAX(0, (int) (length - i - 8))); | |
357 | inbuf[i++] = packs.a; | |
358 | inbuf[i++] = packs.b; | |
359 | inbuf[i++] = packs.c; | |
360 | inbuf[i++] = packs.d; | |
361 | inbuf[i++] = packs.e; | |
362 | inbuf[i++] = packs.f; | |
363 | inbuf[i++] = packs.g; | |
364 | inbuf[i++] = packs.h; | |
365 | } | |
366 | } | |
367 | ||
368 | extern void *proc_name_address(void *p); | |
369 | ||
370 | static void | |
371 | panic_display_process_name(void) { | |
372 | char proc_name[32] = "Unknown"; | |
373 | task_t ctask = 0; | |
374 | void *cbsd_info = 0; | |
375 | ||
376 | if (ml_nofault_copy((vm_offset_t)¤t_thread()->task, (vm_offset_t) &ctask, sizeof(task_t)) == sizeof(task_t)) | |
377 | if(ml_nofault_copy((vm_offset_t)&ctask->bsd_info, (vm_offset_t)&cbsd_info, sizeof(&ctask->bsd_info)) == sizeof(&ctask->bsd_info)) | |
378 | if (cbsd_info && (ml_nofault_copy((vm_offset_t) proc_name_address(cbsd_info), (vm_offset_t) &proc_name, sizeof(proc_name)) > 0)) | |
379 | proc_name[sizeof(proc_name) - 1] = '\0'; | |
380 | kdb_printf("\nBSD process name corresponding to current thread: %s\n", proc_name); | |
381 | } | |
382 | ||
383 | unsigned panic_active(void) { | |
384 | return ((panicstr != (char *) 0)); | |
385 | } | |
386 | ||
387 | void populate_model_name(char *model_string) { | |
388 | strlcpy(model_name, model_string, sizeof(model_name)); | |
389 | } | |
390 | ||
391 | static void panic_display_model_name(void) { | |
392 | char tmp_model_name[sizeof(model_name)]; | |
393 | ||
394 | if (ml_nofault_copy((vm_offset_t) &model_name, (vm_offset_t) &tmp_model_name, sizeof(model_name)) != sizeof(model_name)) | |
395 | return; | |
396 | ||
397 | model_name[sizeof(model_name) - 1] = '\0'; | |
398 | ||
399 | if (model_name[0] != 0) | |
400 | kdb_printf("System model name: %s\n", model_name); | |
401 | } | |
402 | ||
403 | extern const char version[]; | |
404 | extern char osversion[]; | |
405 | ||
406 | __private_extern__ void panic_display_system_configuration(void) { | |
407 | static boolean_t config_displayed = FALSE; | |
408 | ||
409 | panic_display_process_name(); | |
410 | if (config_displayed == FALSE) { | |
411 | kdb_printf("\nMac OS version:\n%s\n", | |
412 | (osversion[0] != 0) ? osversion : "Not yet set"); | |
413 | kdb_printf("\nKernel version:\n%s\n",version); | |
414 | panic_display_model_name(); | |
415 | config_displayed = TRUE; | |
416 | } | |
417 | } | |
418 | ||
419 | #if !MACH_KDP | |
420 | static struct ether_addr kdp_current_mac_address = {{0, 0, 0, 0, 0, 0}}; | |
421 | unsigned int not_in_kdp = 1; | |
422 | ||
423 | /* XXX ugly forward declares to stop warnings */ | |
424 | void *kdp_get_interface(void); | |
425 | void kdp_set_ip_and_mac_addresses(struct in_addr *, struct ether_addr *); | |
426 | void kdp_set_gateway_mac(void *); | |
427 | void kdp_set_interface(void *); | |
428 | void kdp_register_send_receive(void *, void *); | |
429 | void kdp_unregister_send_receive(void *, void *); | |
430 | void kdp_snapshot_preflight(int, void *, uint32_t, uint32_t); | |
431 | int kdp_stack_snapshot_geterror(void); | |
432 | int kdp_stack_snapshot_bytes_traced(void); | |
433 | ||
434 | void * | |
435 | kdp_get_interface( void) | |
436 | { | |
437 | return(void *)0; | |
438 | } | |
439 | ||
440 | unsigned int | |
441 | kdp_get_ip_address(void ) | |
442 | { return 0; } | |
443 | ||
444 | struct ether_addr | |
445 | kdp_get_mac_addr(void) | |
446 | { | |
447 | return kdp_current_mac_address; | |
448 | } | |
449 | ||
450 | void | |
451 | kdp_set_ip_and_mac_addresses( | |
452 | __unused struct in_addr *ipaddr, | |
453 | __unused struct ether_addr *macaddr) | |
454 | {} | |
455 | ||
456 | void | |
457 | kdp_set_gateway_mac(__unused void *gatewaymac) | |
458 | {} | |
459 | ||
460 | void | |
461 | kdp_set_interface(__unused void *ifp) | |
462 | {} | |
463 | ||
464 | void | |
465 | kdp_register_send_receive(__unused void *send, __unused void *receive) | |
466 | {} | |
467 | ||
468 | void | |
469 | kdp_unregister_send_receive(__unused void *send, __unused void *receive) | |
470 | {} | |
471 | ||
472 | void | |
473 | kdp_snapshot_preflight(__unused int pid, __unused void * tracebuf, | |
474 | __unused uint32_t tracebuf_size, __unused uint32_t options) | |
475 | {} | |
476 | ||
477 | int | |
478 | kdp_stack_snapshot_geterror(void) | |
479 | { | |
480 | return -1; | |
481 | } | |
482 | ||
483 | int | |
484 | kdp_stack_snapshot_bytes_traced(void) | |
485 | { | |
486 | return 0; | |
487 | } | |
488 | ||
489 | #endif |