]>
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 | * @APPLE_FREE_COPYRIGHT@ | |
33 | */ | |
34 | /* | |
35 | * (c) Copyright 1988 HEWLETT-PACKARD COMPANY | |
36 | * | |
37 | * To anyone who acknowledges that this file is provided "AS IS" | |
38 | * without any express or implied warranty: | |
39 | * permission to use, copy, modify, and distribute this file | |
40 | * for any purpose is hereby granted without fee, provided that | |
41 | * the above copyright notice and this notice appears in all | |
42 | * copies, and that the name of Hewlett-Packard Company not be | |
43 | * used in advertising or publicity pertaining to distribution | |
44 | * of the software without specific, written prior permission. | |
45 | * Hewlett-Packard Company makes no representations about the | |
46 | * suitability of this software for any purpose. | |
47 | */ | |
48 | /* | |
49 | * Copyright (c) 1990,1991,1992,1994 The University of Utah and | |
50 | * the Computer Systems Laboratory (CSL). All rights reserved. | |
51 | * | |
52 | * THE UNIVERSITY OF UTAH AND CSL PROVIDE THIS SOFTWARE IN ITS "AS IS" | |
53 | * CONDITION, AND DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES | |
54 | * WHATSOEVER RESULTING FROM ITS USE. | |
55 | * | |
56 | * CSL requests users of this software to return to csl-dist@cs.utah.edu any | |
57 | * improvements that they make and grant CSL redistribution rights. | |
58 | * | |
59 | * Utah $Hdr: model_dep.c 1.34 94/12/14$ | |
60 | */ | |
2d21ac55 A |
61 | /* |
62 | * NOTICE: This file was modified by McAfee Research in 2004 to introduce | |
63 | * support for mandatory and extensible security protections. This notice | |
64 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
65 | * Version 2.0. | |
66 | */ | |
1c79356b A |
67 | |
68 | #include <debug.h> | |
69 | #include <mach_kdb.h> | |
70 | #include <mach_kdp.h> | |
71 | #include <db_machine_commands.h> | |
1c79356b A |
72 | |
73 | #include <kern/thread.h> | |
74 | #include <machine/pmap.h> | |
1c79356b A |
75 | #include <device/device_types.h> |
76 | ||
77 | #include <mach/vm_param.h> | |
78 | #include <mach/clock_types.h> | |
79 | #include <mach/machine.h> | |
80 | #include <mach/kmod.h> | |
81 | #include <ppc/boot.h> | |
82 | ||
83 | #include <kern/misc_protos.h> | |
84 | #include <kern/startup.h> | |
85 | #include <ppc/misc_protos.h> | |
86 | #include <ppc/proc_reg.h> | |
87 | #include <ppc/thread.h> | |
88 | #include <ppc/asm.h> | |
89 | #include <ppc/mem.h> | |
90 | #include <ppc/Firmware.h> | |
91 | #include <ppc/low_trace.h> | |
92 | #include <ppc/mappings.h> | |
93 | #include <ppc/FirmwareCalls.h> | |
91447636 | 94 | #include <ppc/cpu_internal.h> |
1c79356b | 95 | #include <ppc/exception.h> |
55e303ae | 96 | #include <ppc/hw_perfmon.h> |
91447636 | 97 | #include <ppc/lowglobals.h> |
2d21ac55 A |
98 | #include <ppc/machine_cpu.h> |
99 | #include <ppc/db_machdep.h> | |
1c79356b A |
100 | |
101 | #include <kern/clock.h> | |
102 | #include <kern/debug.h> | |
103 | #include <machine/trap.h> | |
104 | #include <kern/spl.h> | |
105 | #include <pexpert/pexpert.h> | |
2d21ac55 A |
106 | #include <kern/sched.h> |
107 | #include <kern/task.h> | |
108 | #include <kern/machine.h> | |
109 | #include <vm/vm_map.h> | |
1c79356b A |
110 | |
111 | #include <IOKit/IOPlatformExpert.h> | |
112 | ||
113 | #include <mach/vm_prot.h> | |
114 | #include <vm/pmap.h> | |
115 | #include <mach/time_value.h> | |
2d21ac55 A |
116 | #include <mach/mach_types.h> |
117 | #include <mach/mach_vm.h> | |
1c79356b A |
118 | #include <machine/machparam.h> /* for btop */ |
119 | ||
120 | #if MACH_KDB | |
121 | #include <ddb/db_aout.h> | |
122 | #include <ddb/db_output.h> | |
123 | #include <ddb/db_command.h> | |
124 | #include <machine/db_machdep.h> | |
125 | ||
126 | extern struct db_command ppc_db_commands[]; | |
127 | #endif /* MACH_KDB */ | |
128 | ||
129 | char kernel_args_buf[256] = "/mach_kernel"; | |
130 | char boot_args_buf[256] = "/mach_servers/bootstrap"; | |
131 | char env_buf[256]; | |
132 | ||
133 | #define TRAP_DEBUGGER __asm__ volatile("tw 4,r3,r3"); | |
134 | #define TRAP_DEBUGGER_INST 0x7c831808 | |
135 | #define TRAP_DIRECT __asm__ volatile("tw 4,r4,r4"); | |
136 | #define TRAP_DIRECT_INST 0x7c842008 | |
137 | #define TRAP_INST_SIZE 4 | |
138 | #define BREAK_TO_KDP0 0x7fe00008 | |
139 | #define BREAK_TO_KDP1 0x7c800008 | |
140 | #define BREAK_TO_KDB0 0x7c810808 | |
141 | ||
142 | /* | |
143 | * Code used to synchronize debuggers among all cpus, one active at a time, switch | |
144 | * from on to another using kdb_on! #cpu or cpu #cpu | |
145 | */ | |
146 | ||
9bccf70c A |
147 | hw_lock_data_t debugger_lock; /* debugger lock */ |
148 | hw_lock_data_t pbtlock; /* backtrace print lock */ | |
1c79356b | 149 | |
2d21ac55 | 150 | unsigned int debugger_cpu = (unsigned)-1; /* current cpu running debugger */ |
1c79356b | 151 | int debugger_debug = 0; /* Debug debugger */ |
1c79356b A |
152 | int db_run_mode; /* Debugger run mode */ |
153 | unsigned int debugger_sync = 0; /* Cross processor debugger entry sync */ | |
154 | extern unsigned int NMIss; /* NMI debounce switch */ | |
155 | ||
0b4e3aa0 A |
156 | extern volatile int panicwait; |
157 | volatile unsigned int pbtcnt = 0; | |
158 | volatile unsigned int pbtcpu = -1; | |
159 | ||
1c79356b A |
160 | unsigned int lastTrace; /* Value of low-level exception trace controls */ |
161 | ||
a3d08fcd | 162 | |
1c79356b A |
163 | volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding |
164 | breakpoints (ie: cpus that did not | |
165 | insert back breakpoints) */ | |
166 | void unlock_debugger(void); | |
167 | void lock_debugger(void); | |
2d21ac55 A |
168 | void dump_backtrace(struct savearea *sv, |
169 | unsigned int stackptr, | |
170 | unsigned int fence); | |
171 | void dump_savearea(struct savearea *sv, | |
172 | unsigned int fence); | |
55e303ae | 173 | |
1c79356b A |
174 | #if !MACH_KDB |
175 | boolean_t db_breakpoints_inserted = TRUE; | |
2d21ac55 | 176 | jmp_buf_t *db_recover; |
1c79356b A |
177 | #endif |
178 | ||
179 | #if MACH_KDB | |
180 | #include <ddb/db_run.h> | |
181 | int kdb_flag=0; | |
182 | extern boolean_t db_breakpoints_inserted; | |
183 | extern jmp_buf_t *db_recover; | |
184 | #define KDB_READY 0x1 | |
185 | #endif | |
186 | ||
187 | #if MACH_KDP | |
188 | extern int kdp_flag; | |
189 | #define KDP_READY 0x1 | |
190 | #endif | |
191 | ||
2d21ac55 | 192 | unsigned int db_im_stepping = 0xFFFFFFFF; /* Remember if we were stepping */ |
0b4e3aa0 | 193 | |
0b4e3aa0 | 194 | |
2d21ac55 | 195 | const char *failNames[] = { |
0b4e3aa0 A |
196 | "Debugging trap", /* failDebug */ |
197 | "Corrupt stack", /* failStack */ | |
198 | "Corrupt mapping tables", /* failMapping */ | |
199 | "Corrupt context", /* failContext */ | |
9bccf70c A |
200 | "No saveareas", /* failNoSavearea */ |
201 | "Savearea corruption", /* failSaveareaCorr */ | |
202 | "Invalid live context", /* failBadLiveContext */ | |
91447636 | 203 | "Corrupt skip lists", /* failSkipLists */ |
55e303ae | 204 | "Unaligned stack", /* failUnalignedStk */ |
91447636 | 205 | "Invalid pmap", /* failPmap */ |
b36670ce | 206 | "Lock timeout", /* failTimeout */ |
9bccf70c | 207 | "Unknown failure code" /* Unknown failure code - must always be last */ |
0b4e3aa0 A |
208 | }; |
209 | ||
2d21ac55 | 210 | const char *invxcption = "Unknown code"; |
0b4e3aa0 | 211 | |
2d21ac55 | 212 | static unsigned commit_paniclog_to_nvram; |
1c79356b A |
213 | |
214 | #if !MACH_KDB | |
2d21ac55 A |
215 | void kdb_trap(__unused int type, __unused struct savearea *regs) {} |
216 | #endif /* !MACH_KDB */ | |
1c79356b A |
217 | |
218 | #if !MACH_KDP | |
2d21ac55 A |
219 | void kdp_trap(__unused int type, __unused struct savearea *regs) {} |
220 | #endif /* !MACH_KDP */ | |
221 | ||
222 | extern int default_preemption_rate; | |
223 | extern int max_unsafe_quanta; | |
224 | extern int max_poll_quanta; | |
1c79356b A |
225 | |
226 | void | |
2d21ac55 | 227 | machine_startup(void) |
1c79356b A |
228 | { |
229 | int boot_arg; | |
91447636 | 230 | unsigned int wncpu; |
1c79356b A |
231 | |
232 | if (PE_parse_boot_arg("cpus", &wncpu)) { | |
91447636 A |
233 | if ((wncpu > 0) && (wncpu < MAX_CPUS)) |
234 | max_ncpus = wncpu; | |
235 | } | |
1c79356b A |
236 | |
237 | if( PE_get_hotkey( kPEControlKey )) | |
238 | halt_in_debugger = halt_in_debugger ? 0 : 1; | |
239 | ||
240 | if (PE_parse_boot_arg("debug", &boot_arg)) { | |
241 | if (boot_arg & DB_HALT) halt_in_debugger=1; | |
2d21ac55 | 242 | if (boot_arg & DB_PRT) disable_debug_output=FALSE; |
1c79356b | 243 | if (boot_arg & DB_SLOG) systemLogDiags=TRUE; |
9bccf70c A |
244 | if (boot_arg & DB_NMI) panicDebugging=TRUE; |
245 | if (boot_arg & DB_LOG_PI_SCRN) logPanicDataToScreen=TRUE; | |
1c79356b | 246 | } |
91447636 | 247 | |
2d21ac55 A |
248 | if (!PE_parse_boot_arg("nvram_paniclog", &commit_paniclog_to_nvram)) |
249 | commit_paniclog_to_nvram = 1; | |
250 | ||
91447636 | 251 | PE_parse_boot_arg("vmmforce", &lowGlo.lgVMMforcedFeats); |
1c79356b | 252 | |
0b4e3aa0 A |
253 | hw_lock_init(&debugger_lock); /* initialize debugger lock */ |
254 | hw_lock_init(&pbtlock); /* initialize print backtrace lock */ | |
1c79356b A |
255 | |
256 | #if MACH_KDB | |
257 | /* | |
258 | * Initialize KDB | |
259 | */ | |
260 | #if DB_MACHINE_COMMANDS | |
261 | db_machine_commands_install(ppc_db_commands); | |
262 | #endif /* DB_MACHINE_COMMANDS */ | |
263 | ddb_init(); | |
264 | ||
265 | if (boot_arg & DB_KDB) | |
266 | current_debugger = KDB_CUR_DB; | |
267 | ||
268 | /* | |
269 | * Cause a breakpoint trap to the debugger before proceeding | |
270 | * any further if the proper option bit was specified in | |
271 | * the boot flags. | |
272 | */ | |
273 | if (halt_in_debugger && (current_debugger == KDB_CUR_DB)) { | |
274 | Debugger("inline call to debugger(machine_startup)"); | |
275 | halt_in_debugger = 0; | |
276 | active_debugger =1; | |
277 | } | |
278 | #endif /* MACH_KDB */ | |
279 | if (PE_parse_boot_arg("preempt", &boot_arg)) { | |
1c79356b A |
280 | default_preemption_rate = boot_arg; |
281 | } | |
0b4e3aa0 | 282 | if (PE_parse_boot_arg("unsafe", &boot_arg)) { |
0b4e3aa0 A |
283 | max_unsafe_quanta = boot_arg; |
284 | } | |
285 | if (PE_parse_boot_arg("poll", &boot_arg)) { | |
0b4e3aa0 A |
286 | max_poll_quanta = boot_arg; |
287 | } | |
288 | if (PE_parse_boot_arg("yield", &boot_arg)) { | |
0b4e3aa0 A |
289 | sched_poll_yield_shift = boot_arg; |
290 | } | |
1c79356b A |
291 | |
292 | machine_conf(); | |
293 | ||
1c79356b | 294 | /* |
91447636 | 295 | * Kick off the kernel bootstrap. |
1c79356b | 296 | */ |
91447636 A |
297 | kernel_bootstrap(); |
298 | /*NOTREACHED*/ | |
1c79356b A |
299 | } |
300 | ||
301 | char * | |
2d21ac55 | 302 | machine_boot_info(__unused char *buf, __unused vm_size_t size) |
1c79356b A |
303 | { |
304 | return(PE_boot_args()); | |
305 | } | |
306 | ||
307 | void | |
308 | machine_conf(void) | |
309 | { | |
55e303ae | 310 | machine_info.memory_size = mem_size; /* Note that this will be 2 GB for >= 2 GB machines */ |
1c79356b A |
311 | } |
312 | ||
313 | void | |
314 | machine_init(void) | |
315 | { | |
2d21ac55 | 316 | debug_log_init(); |
1c79356b | 317 | clock_config(); |
3a60a9f5 A |
318 | /* Note that we must initialize the stepper tables AFTER the clock is configured!!!!! */ |
319 | if(pmsExperimental & 1) pmsCPUConf(); /* (EXPERIMENTAL) Initialize the stepper tables */ | |
55e303ae | 320 | perfmon_init(); |
3a60a9f5 A |
321 | return; |
322 | ||
1c79356b A |
323 | } |
324 | ||
325 | void slave_machine_init(void) | |
326 | { | |
1c79356b | 327 | cpu_machine_init(); /* Initialize the processor */ |
91447636 | 328 | clock_init(); /* Init the clock */ |
1c79356b A |
329 | } |
330 | ||
331 | void | |
332 | halt_all_cpus(boolean_t reboot) | |
333 | { | |
334 | if(reboot) | |
335 | { | |
336 | printf("MACH Reboot\n"); | |
337 | PEHaltRestart(kPERestartCPU); | |
338 | } | |
339 | else | |
340 | { | |
341 | printf("CPU halted\n"); | |
342 | PEHaltRestart(kPEHaltCPU); | |
343 | } | |
344 | while(1); | |
345 | } | |
346 | ||
347 | void | |
348 | halt_cpu(void) | |
349 | { | |
350 | halt_all_cpus(FALSE); | |
351 | } | |
352 | ||
353 | #if MACH_ASSERT | |
354 | /* | |
355 | * Machine-dependent routine to fill in an array with up to callstack_max | |
356 | * levels of return pc information. | |
357 | */ | |
2d21ac55 A |
358 | void |
359 | machine_callstack(__unused natural_t *buf, __unused vm_size_t callstack_max) | |
1c79356b A |
360 | { |
361 | } | |
362 | #endif /* MACH_ASSERT */ | |
363 | ||
1c79356b | 364 | void |
9bccf70c | 365 | print_backtrace(struct savearea *ssp) |
1c79356b | 366 | { |
2d21ac55 A |
367 | unsigned int stackptr, fence; |
368 | struct savearea *sv, *svssp, *psv; | |
369 | unsigned int cpu; | |
0b4e3aa0 A |
370 | |
371 | /* | |
372 | * We need this lock to make sure we don't hang up when we double panic on an MP. | |
373 | */ | |
374 | ||
375 | cpu = cpu_number(); /* Just who are we anyways? */ | |
376 | if(pbtcpu != cpu) { /* Allow recursion */ | |
2d21ac55 | 377 | (void)hw_atomic_add(&pbtcnt, 1); /* Remember we are trying */ |
0b4e3aa0 A |
378 | while(!hw_lock_try(&pbtlock)); /* Spin here until we can get in. If we never do, well, we're crashing anyhow... */ |
379 | pbtcpu = cpu; /* Mark it as us */ | |
380 | } | |
1c79356b | 381 | |
2d21ac55 A |
382 | svssp = (struct savearea *)ssp; /* Make this easier */ |
383 | sv = NULL; | |
384 | if(current_thread()) | |
385 | sv = (struct savearea *)current_thread()->machine.pcb; /* Find most current savearea if system has started */ | |
0b4e3aa0 A |
386 | |
387 | fence = 0xFFFFFFFF; /* Show we go all the way */ | |
55e303ae | 388 | if(sv) fence = (unsigned int)sv->save_r1; /* Stop at previous exception point */ |
0b4e3aa0 A |
389 | |
390 | if(!svssp) { /* Should we start from stack? */ | |
9bccf70c | 391 | kdb_printf("Latest stack backtrace for cpu %d:\n", cpu_number()); |
0b4e3aa0 | 392 | __asm__ volatile("mr %0,r1" : "=r" (stackptr)); /* Get current stack */ |
2d21ac55 | 393 | dump_backtrace((struct savearea *)0,stackptr, fence); /* Dump the backtrace */ |
0b4e3aa0 | 394 | if(!sv) { /* Leave if no saveareas */ |
0b4e3aa0 | 395 | hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */ |
2d21ac55 | 396 | goto pbt_exit; |
0b4e3aa0 A |
397 | } |
398 | } | |
399 | else { /* Were we passed an exception? */ | |
400 | fence = 0xFFFFFFFF; /* Show we go all the way */ | |
9bccf70c | 401 | if(svssp->save_hdr.save_prev) { |
55e303ae | 402 | if((svssp->save_hdr.save_prev <= vm_last_addr) && ((unsigned int)pmap_find_phys(kernel_pmap, (addr64_t)svssp->save_hdr.save_prev))) { /* Valid address? */ |
2d21ac55 | 403 | psv = (struct savearea *)((unsigned int)svssp->save_hdr.save_prev); /* Get the 64-bit back chain converted to a regualr pointer */ |
55e303ae | 404 | fence = (unsigned int)psv->save_r1; /* Stop at previous exception point */ |
9bccf70c A |
405 | } |
406 | } | |
0b4e3aa0 | 407 | |
9bccf70c | 408 | kdb_printf("Latest crash info for cpu %d:\n", cpu_number()); |
2d21ac55 | 409 | kdb_printf(" Exception state (sv=%p)\n", svssp); |
0b4e3aa0 A |
410 | dump_savearea(svssp, fence); /* Dump this savearea */ |
411 | } | |
412 | ||
413 | if(!sv) { /* Leave if no saveareas */ | |
0b4e3aa0 | 414 | hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */ |
2d21ac55 | 415 | goto pbt_exit; |
0b4e3aa0 A |
416 | } |
417 | ||
9bccf70c | 418 | kdb_printf("Proceeding back via exception chain:\n"); |
0b4e3aa0 A |
419 | |
420 | while(sv) { /* Do them all... */ | |
55e303ae A |
421 | if(!(((addr64_t)((uintptr_t)sv) <= vm_last_addr) && |
422 | (unsigned int)pmap_find_phys(kernel_pmap, (addr64_t)((uintptr_t)sv)))) { /* Valid address? */ | |
2d21ac55 | 423 | kdb_printf(" Exception state (sv=%p) Not mapped or invalid. stopping...\n", sv); |
9bccf70c A |
424 | break; |
425 | } | |
426 | ||
2d21ac55 | 427 | kdb_printf(" Exception state (sv=%p)\n", sv); |
0b4e3aa0 | 428 | if(sv == svssp) { /* Did we dump it already? */ |
9bccf70c | 429 | kdb_printf(" previously dumped as \"Latest\" state. skipping...\n"); |
0b4e3aa0 A |
430 | } |
431 | else { | |
432 | fence = 0xFFFFFFFF; /* Show we go all the way */ | |
9bccf70c | 433 | if(sv->save_hdr.save_prev) { |
55e303ae | 434 | if((sv->save_hdr.save_prev <= vm_last_addr) && ((unsigned int)pmap_find_phys(kernel_pmap, (addr64_t)sv->save_hdr.save_prev))) { /* Valid address? */ |
2d21ac55 | 435 | psv = (struct savearea *)((unsigned int)sv->save_hdr.save_prev); /* Get the 64-bit back chain converted to a regualr pointer */ |
55e303ae | 436 | fence = (unsigned int)psv->save_r1; /* Stop at previous exception point */ |
9bccf70c A |
437 | } |
438 | } | |
0b4e3aa0 A |
439 | dump_savearea(sv, fence); /* Dump this savearea */ |
440 | } | |
441 | ||
2d21ac55 | 442 | sv = CAST_DOWN(struct savearea *, sv->save_hdr.save_prev); /* Back chain */ |
0b4e3aa0 A |
443 | } |
444 | ||
0b4e3aa0 A |
445 | |
446 | pbtcpu = -1; /* Mark as unowned */ | |
447 | hw_lock_unlock(&pbtlock); /* Allow another back trace to happen */ | |
2d21ac55 | 448 | (void)hw_atomic_sub(&pbtcnt, 1); /* Show we are done */ |
0b4e3aa0 A |
449 | |
450 | while(pbtcnt); /* Wait for completion */ | |
2d21ac55 A |
451 | pbt_exit: |
452 | panic_display_system_configuration(); | |
0b4e3aa0 A |
453 | |
454 | return; | |
455 | } | |
456 | ||
2d21ac55 A |
457 | void |
458 | dump_savearea(struct savearea *sv, unsigned int fence) | |
459 | { | |
460 | const char *xcode; | |
0b4e3aa0 | 461 | |
2d21ac55 A |
462 | if(sv->save_exception > T_MAX) |
463 | xcode = invxcption; /* Too big for table */ | |
464 | else | |
465 | xcode = trap_type[sv->save_exception / 4]; /* Point to the type */ | |
0b4e3aa0 | 466 | |
9bccf70c | 467 | kdb_printf(" PC=0x%08X; MSR=0x%08X; DAR=0x%08X; DSISR=0x%08X; LR=0x%08X; R1=0x%08X; XCP=0x%08X (%s)\n", |
55e303ae A |
468 | (unsigned int)sv->save_srr0, (unsigned int)sv->save_srr1, (unsigned int)sv->save_dar, sv->save_dsisr, |
469 | (unsigned int)sv->save_lr, (unsigned int)sv->save_r1, sv->save_exception, xcode); | |
0b4e3aa0 A |
470 | |
471 | if(!(sv->save_srr1 & MASK(MSR_PR))) { /* Are we in the kernel? */ | |
91447636 | 472 | dump_backtrace(sv, (unsigned int)sv->save_r1, fence); /* Dump the stack back trace from here if not user state */ |
1c79356b | 473 | } |
0b4e3aa0 A |
474 | |
475 | return; | |
476 | } | |
477 | ||
91447636 | 478 | #define DUMPFRAMES 34 |
0b4e3aa0 | 479 | #define LRindex 2 |
1c79356b | 480 | |
2d21ac55 | 481 | void dump_backtrace(struct savearea *sv, unsigned int stackptr, unsigned int fence) { |
1c79356b | 482 | |
0b4e3aa0 A |
483 | unsigned int bframes[DUMPFRAMES]; |
484 | unsigned int sframe[8], raddr, dumbo; | |
91447636 | 485 | int i, index=0; |
2d21ac55 | 486 | // char syminfo[80]; |
0b4e3aa0 | 487 | |
9bccf70c | 488 | kdb_printf(" Backtrace:\n"); |
2d21ac55 | 489 | if (sv != (struct savearea *)0) { |
91447636 A |
490 | bframes[0] = (unsigned int)sv->save_srr0; |
491 | bframes[1] = (unsigned int)sv->save_lr; | |
492 | index = 2; | |
493 | } | |
494 | for(i = index; i < DUMPFRAMES; i++) { /* Dump up to max frames */ | |
0b4e3aa0 A |
495 | |
496 | if(!stackptr || (stackptr == fence)) break; /* Hit stop point or end... */ | |
497 | ||
55e303ae | 498 | if(stackptr & 0x0000000F) { /* Is stack pointer valid? */ |
9bccf70c | 499 | kdb_printf("\n backtrace terminated - unaligned frame address: 0x%08X\n", stackptr); /* No, tell 'em */ |
0b4e3aa0 A |
500 | break; |
501 | } | |
502 | ||
55e303ae A |
503 | raddr = (unsigned int)pmap_find_phys(kernel_pmap, (addr64_t)stackptr); /* Get physical frame address */ |
504 | if(!raddr || (stackptr > vm_last_addr)) { /* Is it mapped? */ | |
9bccf70c | 505 | kdb_printf("\n backtrace terminated - frame not mapped or invalid: 0x%08X\n", stackptr); /* No, tell 'em */ |
1c79356b | 506 | break; |
1c79356b | 507 | } |
0b4e3aa0 | 508 | |
55e303ae | 509 | if(!mapping_phys_lookup(raddr, &dumbo)) { /* Is it within physical RAM? */ |
9bccf70c | 510 | kdb_printf("\n backtrace terminated - frame outside of RAM: v=0x%08X, p=%08X\n", stackptr, raddr); /* No, tell 'em */ |
1c79356b | 511 | break; |
0b4e3aa0 A |
512 | } |
513 | ||
55e303ae | 514 | ReadReal((addr64_t)((raddr << 12) | (stackptr & 4095)), &sframe[0]); /* Fetch the stack frame */ |
0b4e3aa0 | 515 | |
9bccf70c | 516 | bframes[i] = sframe[LRindex]; /* Save the link register */ |
0b4e3aa0 | 517 | |
2d21ac55 A |
518 | // syms_formataddr((vm_offset_t)bframes[i], syminfo, sizeof (syminfo)); |
519 | // kdb_printf(" %s\n", syminfo); | |
9bccf70c A |
520 | if(!i) kdb_printf(" "); /* Indent first time */ |
521 | else if(!(i & 7)) kdb_printf("\n "); /* Skip to new line every 8 */ | |
522 | kdb_printf("0x%08X ", bframes[i]); /* Dump the link register */ | |
0b4e3aa0 | 523 | |
9bccf70c | 524 | stackptr = sframe[0]; /* Chain back */ |
1c79356b | 525 | } |
9bccf70c A |
526 | kdb_printf("\n"); |
527 | if(i >= DUMPFRAMES) kdb_printf(" backtrace continues...\n"); /* Say we terminated early */ | |
0b4e3aa0 A |
528 | if(i) kmod_dump((vm_offset_t *)&bframes[0], i); /* Show what kmods are in trace */ |
529 | ||
1c79356b | 530 | } |
0b4e3aa0 | 531 | |
2d21ac55 A |
532 | void commit_paniclog(void) { |
533 | unsigned long pi_size = 0; | |
0b4e3aa0 | 534 | |
2d21ac55 A |
535 | if (debug_buf_size > 0) { |
536 | if (commit_paniclog_to_nvram) { | |
537 | unsigned int bufpos; | |
538 | ||
539 | /* XXX Consider using the WKdm compressor in the | |
540 | * future, rather than just packing - would need to | |
541 | * be co-ordinated with crashreporter, which decodes | |
542 | * this post-restart. The compressor should be | |
543 | * capable of in-place compression. | |
544 | */ | |
545 | bufpos = packA(debug_buf, (unsigned) (debug_buf_ptr - debug_buf), debug_buf_size); | |
546 | /* If compression was successful, | |
547 | * use the compressed length | |
548 | */ | |
549 | pi_size = bufpos ? bufpos : (unsigned) (debug_buf_ptr - debug_buf); | |
550 | ||
551 | /* Truncate if the buffer is larger than a | |
552 | * certain magic size - this really ought to | |
553 | * be some appropriate fraction of the NVRAM | |
554 | * image buffer, and is best done in the | |
555 | * savePanicInfo() or PESavePanicInfo() calls | |
556 | * This call must save data synchronously, | |
557 | * since we can subsequently halt the system. | |
558 | */ | |
559 | kprintf("Attempting to commit panic log to NVRAM\n"); | |
560 | /* N.B.: This routine (currently an IOKit wrapper that | |
561 | * calls through to the appropriate platform NVRAM | |
562 | * driver, must be panic context safe, i.e. | |
563 | * acquire no locks or require kernel services. | |
564 | * This does not appear to be the case currently | |
565 | * on some platforms, unfortunately (the driver | |
566 | * on command gate serialization). | |
567 | */ | |
568 | pi_size = PESavePanicInfo((unsigned char *)debug_buf, | |
569 | ((pi_size > 2040) ? 2040 : pi_size)); | |
570 | /* Uncompress in-place, to allow debuggers to examine | |
571 | * the panic log. | |
572 | */ | |
573 | if (bufpos) | |
574 | unpackA(debug_buf, bufpos); | |
575 | } | |
576 | } | |
577 | } | |
1c79356b A |
578 | |
579 | void | |
580 | Debugger(const char *message) { | |
581 | ||
1c79356b A |
582 | spl_t spl; |
583 | ||
584 | spl = splhigh(); /* No interruptions from here on */ | |
0b4e3aa0 | 585 | |
1c79356b A |
586 | /* |
587 | * backtrace for Debugger() call from panic() if no current debugger | |
588 | * backtrace and return for double panic() call | |
589 | */ | |
590 | if ((panicstr != (char *)0) && | |
591 | (((nestedpanic != 0) && (current_debugger == 1)) || (active_debugger == 0))) { | |
592 | print_backtrace(NULL); | |
593 | if (nestedpanic != 0) { | |
594 | splx(spl); | |
0b4e3aa0 | 595 | return; /* Yeah, don't enter again... */ |
1c79356b A |
596 | } |
597 | } | |
0b4e3aa0 | 598 | |
91447636 | 599 | if (debug_mode && getPerProc()->debugger_active) { /* Are we already on debugger on this processor? */ |
1c79356b A |
600 | splx(spl); |
601 | return; /* Yeah, don't do it again... */ | |
602 | } | |
603 | ||
9bccf70c A |
604 | |
605 | /* | |
606 | * The above stuff catches the double panic case so we shouldn't have to worry about that here. | |
607 | */ | |
608 | if ( panicstr != (char *)0 ) | |
609 | { | |
9bccf70c | 610 | disable_preemption(); |
2d21ac55 A |
611 | /* Commit the panic log buffer to NVRAM, unless otherwise |
612 | * specified via a boot-arg. | |
613 | */ | |
614 | commit_paniclog(); | |
615 | if(!panicDebugging) { | |
616 | unsigned int my_cpu, tcpu; | |
9bccf70c A |
617 | |
618 | my_cpu = cpu_number(); | |
619 | debugger_cpu = my_cpu; | |
620 | ||
2d21ac55 | 621 | (void)hw_atomic_add(&debug_mode, 1); |
91447636 | 622 | PerProcTable[my_cpu].ppe_vaddr->debugger_active++; |
9bccf70c A |
623 | lock_debugger(); |
624 | ||
91447636 | 625 | for(tcpu = 0; tcpu < real_ncpus; tcpu++) { |
9bccf70c | 626 | if(tcpu == my_cpu) continue; |
2d21ac55 | 627 | (void)hw_atomic_add(&debugger_sync, 1); |
9bccf70c A |
628 | (void)cpu_signal(tcpu, SIGPdebug, 0 ,0); |
629 | } | |
630 | (void)hw_cpu_sync(&debugger_sync, LockTimeOut); | |
631 | debugger_sync = 0; | |
632 | } | |
633 | ||
634 | draw_panic_dialog(); | |
635 | ||
2d21ac55 A |
636 | if(!panicDebugging) { |
637 | #if CONFIG_EMBEDDED | |
638 | PEHaltRestart(kPEPanicRestartCPU); | |
639 | #else | |
9bccf70c | 640 | PEHaltRestart( kPEHangCPU ); |
2d21ac55 A |
641 | #endif |
642 | } | |
9bccf70c A |
643 | |
644 | enable_preemption(); | |
645 | } | |
646 | ||
1c79356b A |
647 | if ((current_debugger != NO_CUR_DB)) { /* If there is a debugger configured, enter it */ |
648 | printf("Debugger(%s)\n", message); | |
649 | TRAP_DEBUGGER; | |
650 | splx(spl); | |
651 | return; /* Done debugging for a while */ | |
652 | } | |
653 | ||
654 | printf("\nNo debugger configured - dumping debug information\n"); | |
1c79356b A |
655 | printf("MSR=%08X\n",mfmsr()); |
656 | print_backtrace(NULL); | |
657 | splx(spl); | |
658 | return; | |
659 | } | |
660 | ||
0b4e3aa0 A |
661 | /* |
662 | * Here's where we attempt to get some diagnostic information dumped out | |
663 | * when the system is really confused. We will try to get into the | |
664 | * debugger as well. | |
665 | * | |
666 | * We are here with interrupts disabled and on the debug stack. The savearea | |
667 | * that was passed in is NOT chained to the activation. | |
668 | * | |
669 | * save_r3 contains the failure reason code. | |
670 | */ | |
671 | ||
2d21ac55 A |
672 | void |
673 | SysChoked(unsigned int type, struct savearea *sv) | |
674 | { | |
0b4e3aa0 | 675 | unsigned int failcode; |
2d21ac55 | 676 | const char * const pmsg = "System Failure: cpu=%d; code=%08X (%s)\n"; |
0b4e3aa0 | 677 | mp_disable_preemption(); |
2d21ac55 | 678 | disable_debug_output = FALSE; |
0b4e3aa0 A |
679 | debug_mode = TRUE; |
680 | ||
55e303ae | 681 | failcode = (unsigned int)sv->save_r3; /* Get the failure code */ |
0b4e3aa0 A |
682 | if(failcode > failUnknown) failcode = failUnknown; /* Set unknown code code */ |
683 | ||
2d21ac55 A |
684 | kprintf(pmsg, cpu_number(), (unsigned int)sv->save_r3, failNames[failcode]); |
685 | kdb_printf(pmsg, cpu_number(), (unsigned int)sv->save_r3, failNames[failcode]); | |
0b4e3aa0 | 686 | |
9bccf70c | 687 | print_backtrace(sv); /* Attempt to print backtrace */ |
0b4e3aa0 | 688 | |
2d21ac55 A |
689 | /* Commit the panic log buffer to NVRAM, unless otherwise |
690 | * specified via a boot-arg. For certain types of panics | |
691 | * which result in a "choke" exception, this may well | |
692 | * be inadvisable, and setting the nvram_paniclog=0 | |
693 | * boot-arg may be useful. | |
694 | */ | |
695 | ||
696 | if (panicDebugging) | |
697 | commit_paniclog(); | |
698 | ||
699 | Call_DebuggerC(type, sv); /* Attempt to get into debugger */ | |
0b4e3aa0 | 700 | |
2d21ac55 A |
701 | if ((current_debugger != NO_CUR_DB)) |
702 | Call_DebuggerC(type, sv); /* Attempt to get into debugger */ | |
703 | panic_plain(pmsg, cpu_number(), (unsigned int)sv->save_r3, failNames[failcode]); | |
0b4e3aa0 A |
704 | } |
705 | ||
706 | ||
707 | ||
1c79356b A |
708 | /* |
709 | * When we get here, interruptions are disabled and we are on the debugger stack | |
710 | * Never, ever, ever, ever enable interruptions from here on | |
711 | */ | |
712 | ||
2d21ac55 A |
713 | int |
714 | Call_DebuggerC(unsigned int type, struct savearea *saved_state) | |
1c79356b A |
715 | { |
716 | int directcall, wait; | |
2d21ac55 | 717 | addr64_t instr_ptr = 0ULL; |
55e303ae | 718 | ppnum_t instr_pp; |
2d21ac55 A |
719 | unsigned int instr, tcpu, my_cpu; |
720 | int wasdebugger; | |
1c79356b A |
721 | |
722 | my_cpu = cpu_number(); /* Get our CPU */ | |
723 | ||
724 | #if MACH_KDB | |
725 | if((debugger_cpu == my_cpu) && /* Do we already own debugger? */ | |
91447636 | 726 | PerProcTable[my_cpu].ppe_vaddr->debugger_active && /* and are we really active? */ |
1c79356b A |
727 | db_recover && /* and have we set up recovery? */ |
728 | (current_debugger == KDB_CUR_DB)) { /* and are we in KDB (only it handles recovery) */ | |
729 | kdb_trap(type, saved_state); /* Then reenter it... */ | |
730 | } | |
731 | #endif | |
732 | ||
2d21ac55 | 733 | (void)hw_atomic_add(&debug_mode, 1); /* Indicate we are in debugger */ |
91447636 A |
734 | PerProcTable[my_cpu].ppe_vaddr->debugger_active++; /* Show active on our CPU */ |
735 | ||
1c79356b A |
736 | lock_debugger(); /* Insure that only one CPU is in debugger */ |
737 | ||
738 | if(db_im_stepping == my_cpu) { /* Are we just back from a step? */ | |
739 | enable_preemption_no_check(); /* Enable preemption now */ | |
740 | db_im_stepping = 0xFFFFFFFF; /* Nobody stepping right now */ | |
741 | } | |
742 | ||
743 | if (debugger_debug) { | |
744 | #if 0 | |
2d21ac55 | 745 | kprintf("Call_DebuggerC(%d): %08X %08X, debact = %d\n", my_cpu, type, (uint32_t)saved_state, debug_mode); /* (TEST/DEBUG) */ |
1c79356b | 746 | #endif |
2d21ac55 | 747 | printf("Call_Debugger: enter - cpu %d, is_slave %d, debugger_cpu %d, pc %08llX\n", |
91447636 | 748 | my_cpu, PerProcTable[my_cpu].ppe_vaddr->debugger_is_slave, debugger_cpu, saved_state->save_srr0); |
1c79356b A |
749 | } |
750 | ||
55e303ae A |
751 | instr_pp = (vm_offset_t)pmap_find_phys(kernel_pmap, (addr64_t)(saved_state->save_srr0)); |
752 | ||
753 | if (instr_pp) { | |
754 | instr_ptr = (addr64_t)(((addr64_t)instr_pp << 12) | (saved_state->save_srr0 & 0xFFF)); /* Make physical address */ | |
755 | instr = ml_phys_read_64(instr_ptr); /* Get the trap that caused entry */ | |
1c79356b A |
756 | } |
757 | else instr = 0; | |
758 | ||
759 | #if 0 | |
55e303ae | 760 | if (debugger_debug) kprintf("Call_DebuggerC(%d): instr_pp = %08X, instr_ptr = %016llX, instr = %08X\n", my_cpu, instr_pp, instr_ptr, instr); /* (TEST/DEBUG) */ |
1c79356b A |
761 | #endif |
762 | ||
763 | if (db_breakpoints_inserted) cpus_holding_bkpts++; /* Bump up the holding count */ | |
2d21ac55 A |
764 | if ((debugger_cpu == (unsigned)-1) && |
765 | !PerProcTable[my_cpu].ppe_vaddr->debugger_is_slave) { | |
1c79356b A |
766 | #if 0 |
767 | if (debugger_debug) kprintf("Call_DebuggerC(%d): lasttrace = %08X\n", my_cpu, lastTrace); /* (TEST/DEBUG) */ | |
768 | #endif | |
769 | debugger_cpu = my_cpu; /* Show that we are debugger */ | |
a3d08fcd A |
770 | |
771 | ||
1c79356b A |
772 | lastTrace = LLTraceSet(0); /* Disable low-level tracing */ |
773 | ||
91447636 | 774 | for(tcpu = 0; tcpu < real_ncpus; tcpu++) { /* Stop all the other guys */ |
1c79356b | 775 | if(tcpu == my_cpu) continue; /* Don't diddle ourselves */ |
2d21ac55 | 776 | (void)hw_atomic_add(&debugger_sync, 1); /* Count signal sent */ |
1c79356b A |
777 | (void)cpu_signal(tcpu, SIGPdebug, 0 ,0); /* Tell 'em to enter debugger */ |
778 | } | |
779 | (void)hw_cpu_sync(&debugger_sync, LockTimeOut); /* Wait for the other processors to enter debug */ | |
780 | debugger_sync = 0; /* We're done with it */ | |
781 | } | |
782 | else if (debugger_cpu != my_cpu) goto debugger_exit; /* We are not debugger, don't continue... */ | |
783 | ||
784 | ||
785 | if (instr == TRAP_DIRECT_INST) { | |
2d21ac55 | 786 | disable_debug_output = FALSE; |
1c79356b A |
787 | print_backtrace(saved_state); |
788 | } | |
789 | ||
790 | switch_debugger = 0; /* Make sure switch request is off */ | |
791 | directcall = 1; /* Assume direct call */ | |
792 | ||
9bccf70c | 793 | if (saved_state->save_srr1 & MASK(SRR1_PRG_TRAP)) { /* Trap instruction? */ |
1c79356b A |
794 | |
795 | directcall = 0; /* We had a trap not a direct call */ | |
796 | ||
797 | switch (instr) { /* Select trap type */ | |
798 | ||
799 | #if MACH_KDP | |
800 | case BREAK_TO_KDP0: /* Breakpoint into KDP? */ | |
801 | case BREAK_TO_KDP1: /* Breakpoint into KDP? */ | |
802 | current_debugger = KDP_CUR_DB; /* Yes, set KDP */ | |
803 | kdp_trap(type, saved_state); /* Enter it */ | |
804 | break; | |
805 | #endif | |
806 | ||
807 | #if MACH_KDB | |
808 | case BREAK_TO_KDB0: /* Breakpoint to KDB (the "good" debugger)? */ | |
809 | current_debugger = KDB_CUR_DB; /* Yes, set it */ | |
810 | kdb_trap(type, saved_state); /* Enter it */ | |
811 | break; | |
812 | #endif | |
813 | ||
814 | case TRAP_DEBUGGER_INST: /* Should we enter the current debugger? */ | |
815 | case TRAP_DIRECT_INST: /* Should we enter the current debugger? */ | |
816 | if (current_debugger == KDP_CUR_DB) /* Is current KDP? */ | |
817 | kdp_trap(type, saved_state); /* Yes, enter it */ | |
818 | else if (current_debugger == KDB_CUR_DB) /* Is this KDB? */ | |
819 | kdb_trap(type, saved_state); /* Yes, go ahead and enter */ | |
820 | else goto debugger_error; /* No debugger active */ | |
821 | break; | |
822 | ||
823 | default: /* Unknown/bogus trap type */ | |
824 | goto debugger_error; | |
825 | } | |
826 | } | |
827 | ||
828 | while(1) { /* We are here to handle debugger switches */ | |
829 | ||
830 | if(!directcall) { /* Was this a direct call? */ | |
831 | if(!switch_debugger) break; /* No, then leave if no switch requested... */ | |
832 | ||
833 | /* | |
834 | * Note: we can only switch to a debugger we have. Ignore bogus switch requests. | |
835 | */ | |
836 | #if 0 | |
837 | if (debugger_debug) kprintf("Call_DebuggerC(%d): switching debuggers\n", my_cpu); /* (TEST/DEBUG) */ | |
838 | #endif | |
839 | #if MACH_KDB | |
840 | if(current_debugger == KDP_CUR_DB) current_debugger = KDB_CUR_DB; /* Switch to KDB */ | |
841 | #if MACH_KDP | |
842 | else | |
843 | #endif | |
844 | #endif | |
845 | #if MACH_KDP | |
846 | if(current_debugger == KDB_CUR_DB) current_debugger = KDP_CUR_DB; /* Switch to KDP */ | |
847 | #endif | |
848 | } | |
849 | ||
850 | switch_debugger = 0; /* Clear request */ | |
851 | directcall = 0; /* Clear first-time direct call indication */ | |
852 | ||
853 | switch (current_debugger) { /* Enter correct debugger */ | |
854 | ||
855 | case KDP_CUR_DB: /* Enter KDP */ | |
856 | kdp_trap(type, saved_state); | |
857 | break; | |
858 | ||
859 | case KDB_CUR_DB: /* Enter KDB */ | |
860 | kdb_trap(type, saved_state); | |
861 | break; | |
862 | ||
863 | default: /* No debugger installed */ | |
864 | goto debugger_error; | |
865 | break; | |
866 | } | |
867 | } | |
868 | ||
869 | debugger_exit: | |
870 | #if 0 | |
871 | if (debugger_debug) kprintf("Call_DebuggerC(%d): exit - inst = %08X, cpu=%d(%d), run=%d\n", my_cpu, | |
91447636 | 872 | instr, my_cpu, debugger_cpu, db_run_mode); /* (TEST/DEBUG) */ |
1c79356b A |
873 | #endif |
874 | if ((instr == TRAP_DEBUGGER_INST) || /* Did we trap to enter debugger? */ | |
9bccf70c | 875 | (instr == TRAP_DIRECT_INST)) saved_state->save_srr0 += TRAP_INST_SIZE; /* Yes, point past trap */ |
1c79356b | 876 | |
a3d08fcd A |
877 | wasdebugger = 0; /* Assume not debugger */ |
878 | if(debugger_cpu == my_cpu) { /* Are the debugger processor? */ | |
879 | wasdebugger = 1; /* Remember that we were the debugger */ | |
880 | LLTraceSet(lastTrace); /* Enable tracing on the way out if we are debugger */ | |
881 | } | |
1c79356b A |
882 | |
883 | wait = FALSE; /* Assume we are not going to wait */ | |
884 | if (db_run_mode == STEP_CONTINUE) { /* Are we going to run? */ | |
885 | wait = TRUE; /* Yeah, remember to wait for breakpoints to clear */ | |
886 | debugger_cpu = -1; /* Release other processor's debuggers */ | |
91447636 A |
887 | for(tcpu = 0; tcpu < real_ncpus; tcpu++) |
888 | PerProcTable[tcpu].ppe_vaddr->debugger_pending = 0; /* Release request (this is a HACK) */ | |
1c79356b A |
889 | NMIss = 0; /* Let NMI bounce */ |
890 | } | |
891 | ||
892 | if(db_run_mode == STEP_ONCE) { /* Are we about to step? */ | |
893 | disable_preemption(); /* Disable preemption for the step */ | |
894 | db_im_stepping = my_cpu; /* Remember that I am about to step */ | |
895 | } | |
896 | ||
897 | if (db_breakpoints_inserted) cpus_holding_bkpts--; /* If any breakpoints, back off count */ | |
91447636 | 898 | if (PerProcTable[my_cpu].ppe_vaddr->debugger_is_slave) PerProcTable[my_cpu].ppe_vaddr->debugger_is_slave--; /* If we were a slove, uncount us */ |
1c79356b A |
899 | if (debugger_debug) |
900 | printf("Call_Debugger: exit - cpu %d, debugger_cpu %d, run_mode %d holds %d\n", | |
901 | my_cpu, debugger_cpu, db_run_mode, | |
902 | cpus_holding_bkpts); | |
903 | ||
904 | unlock_debugger(); /* Release the lock */ | |
91447636 | 905 | PerProcTable[my_cpu].ppe_vaddr->debugger_active--; /* Say we aren't active anymore */ |
1c79356b A |
906 | |
907 | if (wait) while(cpus_holding_bkpts); /* Wait for breakpoints to clear */ | |
908 | ||
a3d08fcd | 909 | |
2d21ac55 | 910 | (void)hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */ |
1c79356b A |
911 | |
912 | return(1); /* Exit debugger normally */ | |
913 | ||
914 | debugger_error: | |
915 | if(db_run_mode != STEP_ONCE) enable_preemption_no_check(); /* Enable preemption, but don't preempt here */ | |
2d21ac55 | 916 | (void)hw_atomic_sub(&debug_mode, 1); /* Set out of debug now */ |
1c79356b A |
917 | return(0); /* Return in shame... */ |
918 | ||
919 | } | |
920 | ||
2d21ac55 A |
921 | void |
922 | lock_debugger(void) | |
923 | { | |
924 | unsigned int my_cpu; | |
1c79356b A |
925 | |
926 | my_cpu = cpu_number(); /* Get our CPU number */ | |
927 | ||
2d21ac55 A |
928 | while(1) { /* Check until we get it */ |
929 | if (debugger_cpu != (unsigned)-1 && debugger_cpu != my_cpu) | |
930 | continue; /* Someone, not us, is debugger... */ | |
931 | if (hw_lock_try(&debugger_lock)) { /* Get the debug lock */ | |
932 | if (debugger_cpu == (unsigned)-1 || debugger_cpu == my_cpu) | |
933 | break; /* Is it us? */ | |
934 | hw_lock_unlock(&debugger_lock); /* Not us, release lock */ | |
1c79356b A |
935 | } |
936 | } | |
937 | } | |
938 | ||
939 | void unlock_debugger(void) { | |
940 | ||
941 | hw_lock_unlock(&debugger_lock); | |
942 | ||
943 | } | |
944 | ||
2d21ac55 A |
945 | int patchInst(task_t task, addr64_t vaddr, uint32_t inst); |
946 | int patchInst(task_t task, addr64_t vaddr, uint32_t inst) | |
947 | { | |
948 | vm_map_t map; | |
949 | addr64_t paddr; | |
950 | uint32_t instr, nestingDepth; | |
951 | kern_return_t ret; | |
952 | vm_region_submap_short_info_data_64_t info; | |
953 | mach_msg_type_number_t count; | |
954 | mach_vm_address_t address; | |
955 | mach_vm_size_t sizeOfRegion; | |
956 | vm_prot_t reprotect; | |
957 | ||
958 | if(task == TASK_NULL) return -1; /* Leave if task is bogus... */ | |
959 | ||
960 | task_lock(task); /* Make sure the task doesn't go anywhaere */ | |
961 | if (!task->active) { /* Is is alive? */ | |
962 | task_unlock(task); /* Nope, unlock */ | |
963 | return -1; /* Not a active task, fail... */ | |
964 | } | |
965 | map = task->map; /* Get his map */ | |
966 | vm_map_reference_swap(map); /* Don't let it go away */ | |
967 | task_unlock(task); /* Unleash the task */ | |
55e303ae | 968 | |
2d21ac55 A |
969 | /* Find the memory permissions. */ |
970 | nestingDepth=999999; /* Limit recursion */ | |
971 | ||
972 | count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; | |
973 | address = (mach_vm_address_t)vaddr; | |
974 | sizeOfRegion = (mach_vm_size_t)4; | |
975 | ||
976 | ret = mach_vm_region_recurse(map, &address, &sizeOfRegion, &nestingDepth, (vm_region_recurse_info_t)&info, &count); | |
977 | if (ret != KERN_SUCCESS) { /* Leave if it didn't work */ | |
978 | vm_map_deallocate(map); /* Drop reference on map */ | |
979 | return (-1); | |
980 | } | |
1c79356b | 981 | |
2d21ac55 A |
982 | /* |
983 | * We need to check if there could be a problem if the dtrace probes are being removed and the code is being | |
984 | * executed at the same time. This sequence may leave us with no-execute turned on temporarily when we execute | |
985 | * through it. | |
986 | */ | |
987 | ||
988 | if (!(info.protection & VM_PROT_WRITE)) { | |
989 | /* Save the original protection values for restoration later */ | |
990 | reprotect = info.protection; | |
991 | ||
992 | if (info.max_protection & VM_PROT_WRITE) { | |
993 | /* The memory is not currently writable, but can be made writable. */ | |
994 | ret = mach_vm_protect(map, (mach_vm_offset_t)vaddr, (mach_vm_size_t)4, 0, reprotect | VM_PROT_WRITE); | |
995 | } | |
996 | else { | |
997 | /* | |
998 | * The memory is not currently writable, and cannot be made writable. We need to COW this memory. | |
999 | * | |
1000 | * Strange, we can't just say "reprotect | VM_PROT_COPY", that fails. | |
1001 | */ | |
1002 | ret = mach_vm_protect(map, (mach_vm_offset_t)vaddr, (mach_vm_size_t)4, 0, VM_PROT_COPY | VM_PROT_READ | VM_PROT_WRITE); | |
1003 | } | |
1004 | ||
1005 | if (ret != KERN_SUCCESS) { | |
1006 | vm_map_deallocate(map); /* Drop reference on map */ | |
1007 | return (-1); | |
1008 | } | |
1009 | ||
1010 | } | |
1011 | else { | |
1012 | /* The memory was already writable. */ | |
1013 | reprotect = VM_PROT_NONE; | |
1014 | } | |
1015 | ||
1016 | instr = inst; /* Place instruction in local memory */ | |
1017 | ret = vm_map_write_user(map, &inst, (vm_map_address_t)vaddr, (vm_size_t)4); /* Write the instruction */ | |
1018 | if (ret != KERN_SUCCESS) { /* Leave if it didn't work */ | |
1019 | ||
1020 | if (reprotect != VM_PROT_NONE) { | |
1021 | ret = mach_vm_protect (map, (mach_vm_offset_t)vaddr, (mach_vm_size_t)4, 0, reprotect); | |
1022 | } | |
1023 | ||
1024 | vm_map_deallocate(map); /* Drop reference on map */ | |
1025 | return (-1); | |
1026 | } | |
1027 | ||
1028 | paddr = (addr64_t)pmap_find_phys(map->pmap, vaddr) << 12; /* Find the physical address of the patched address */ | |
1029 | if(!paddr) { /* Is address mapped now? */ | |
1030 | vm_map_deallocate(map); /* Drop reference on map */ | |
1031 | return 0; /* Leave... */ | |
1032 | } | |
1033 | paddr = paddr | (vaddr & 4095); /* Construct physical address */ | |
1034 | invalidate_icache64(paddr, 4, 1); /* Flush out the instruction cache here */ | |
1035 | ||
1036 | if (reprotect != VM_PROT_NONE) { | |
1037 | ret = mach_vm_protect(map, (mach_vm_offset_t)vaddr, (mach_vm_size_t)4, 0, reprotect); | |
1038 | } | |
1039 | ||
1040 | vm_map_deallocate(map); | |
1041 | ||
1042 | return (0); | |
55e303ae | 1043 | } |