]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/db_interface.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / osfmk / ppc / db_interface.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, 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
1c79356b
A
32#include <platforms.h>
33#include <time_stamp.h>
34#include <mach_mp_debug.h>
35#include <mach_ldebug.h>
36#include <db_machine_commands.h>
37
38#include <kern/spl.h>
39#include <kern/cpu_number.h>
40#include <kern/kern_types.h>
41#include <kern/misc_protos.h>
42#include <vm/pmap.h>
43
44#include <ppc/mem.h>
1c79356b
A
45#include <ppc/db_machdep.h>
46#include <ppc/trap.h>
47#include <ppc/setjmp.h>
48#include <ppc/pmap.h>
49#include <ppc/misc_protos.h>
91447636 50#include <ppc/cpu_internal.h>
1c79356b
A
51#include <ppc/exception.h>
52#include <ppc/db_machdep.h>
53#include <ppc/mappings.h>
54#include <ppc/Firmware.h>
2d21ac55 55#include <ppc/serial_io.h> /* for switch_to_serial_console */
1c79356b
A
56
57#include <mach/vm_param.h>
58#include <mach/machine/vm_types.h>
59#include <vm/vm_map.h>
60#include <kern/thread.h>
61#include <kern/task.h>
62#include <kern/debug.h>
2d21ac55 63#include <kern/machine.h> /* for halt_all_cpus() */
91447636
A
64#include <pexpert/pexpert.h>
65#include <IOKit/IOPlatformExpert.h>
1c79356b
A
66
67#include <ddb/db_command.h>
68#include <ddb/db_task_thread.h>
69#include <ddb/db_run.h>
70#include <ddb/db_trap.h>
71#include <ddb/db_output.h>
72#include <ddb/db_access.h>
73#include <ddb/db_sym.h>
74#include <ddb/db_break.h>
75#include <ddb/db_watch.h>
76
9bccf70c
A
77struct savearea *ppc_last_saved_statep;
78struct savearea ppc_nested_saved_state;
1c79356b 79unsigned ppc_last_kdb_sp;
b0d623f7 80db_regs_t ddb_regs; /* register state */
1c79356b 81
1c79356b
A
82extern int debugger_cpu; /* Current cpu running debugger */
83
84int db_all_set_up = 0;
85
86
87#if !MACH_KDP
88void kdp_register_send_receive(void);
89#endif
90
91/*
92 * Enter KDB through a keyboard trap.
93 * We show the registers as of the keyboard interrupt
94 * instead of those at its call to KDB.
95 */
96struct int_regs {
97 /* XXX more registers ? */
98 struct ppc_interrupt_state *is;
99};
100
1c79356b
A
101extern int TRAP_TYPES;
102
103/*
104 * Code used to synchronize kdb among all cpus, one active at a time, switch
105 * from on to another using kdb_on! #cpu or cpu #cpu
106 */
107
108decl_simple_lock_data(, kdb_lock) /* kdb lock */
109
110#define db_simple_lock_init(l, e) hw_lock_init(&((l)->interlock))
111#define db_simple_lock_try(l) hw_lock_try(&((l)->interlock))
112#define db_simple_unlock(l) hw_lock_unlock(&((l)->interlock))
113
114extern volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding
115 breakpoints (ie: cpus that did not
116 insert back breakpoints) */
117extern boolean_t db_breakpoints_inserted;
118
119/* Forward */
120
121extern void kdbprinttrap(
122 int type,
123 int code,
124 int *pc,
125 int sp);
1c79356b
A
126extern void db_write_bytes_user_space(
127 vm_offset_t addr,
128 int size,
129 char *data,
130 task_t task);
131extern int db_search_null(
132 task_t task,
133 unsigned *svaddr,
134 unsigned evaddr,
135 unsigned *skaddr,
136 int flag);
137extern int kdb_enter(int);
138extern void kdb_leave(void);
139extern void lock_kdb(void);
140extern void unlock_kdb(void);
141
142#if DB_MACHINE_COMMANDS
143struct db_command ppc_db_commands[] = {
144 { "lt", db_low_trace, CS_MORE|CS_SET_DOT, 0 },
145 { (char *)0, 0, 0, 0 }
146};
147#endif /* DB_MACHINE_COMMANDS */
148
149#if !MACH_KDP
150void kdp_register_send_receive(void) {}
151#endif
152
153extern jmp_buf_t *db_recover;
1c79356b
A
154
155/*
156 * kdb_trap - field a TRACE or BPT trap
157 */
158void
159kdb_trap(
160 int type,
9bccf70c 161 struct savearea *regs)
1c79356b
A
162{
163 boolean_t trap_from_user;
164 int previous_console_device;
165 int code=0;
166
167 previous_console_device=switch_to_serial_console();
168
169 switch (type) {
170 case T_TRACE: /* single_step */
171 case T_PROGRAM: /* breakpoint */
172#if 0
173 case T_WATCHPOINT: /* watchpoint */
174#endif
175 case -1: /* keyboard interrupt */
176 break;
177
178 default:
179 if (db_recover) {
180 ppc_nested_saved_state = *regs;
181 db_printf("Caught ");
182 if (type > TRAP_TYPES)
183 db_printf("type %d", type);
184 else
185 db_printf("%s", trap_type[type]);
91447636 186 db_printf(" trap, pc = %llx\n",
9bccf70c 187 regs->save_srr0);
1c79356b
A
188 db_error("");
189 /*NOTREACHED*/
190 }
9bccf70c 191 kdbprinttrap(type, code, (int *)&regs->save_srr0, regs->save_r1);
1c79356b
A
192 }
193
91447636 194 getPerProc()->db_saved_state = regs;
1c79356b
A
195
196 ppc_last_saved_statep = regs;
197 ppc_last_kdb_sp = (unsigned) &type;
198
199 if (!IS_USER_TRAP(regs)) {
200 bzero((char *)&ddb_regs, sizeof (ddb_regs));
201 ddb_regs = *regs;
202 trap_from_user = FALSE;
203
204 }
205 else {
206 ddb_regs = *regs;
207 trap_from_user = TRUE;
208 }
209
210 db_task_trap(type, code, trap_from_user);
211
212 *regs = ddb_regs;
213
214 if ((type == T_PROGRAM) &&
9bccf70c 215 (db_get_task_value(regs->save_srr0,
1c79356b
A
216 BKPT_SIZE,
217 FALSE,
91447636 218 db_target_space(current_thread(),
1c79356b
A
219 trap_from_user))
220 == BKPT_INST))
9bccf70c 221 regs->save_srr0 += BKPT_SIZE;
1c79356b 222
91447636 223 getPerProc()->db_saved_state = 0;
1c79356b
A
224 switch_to_old_console(previous_console_device);
225
226}
227
228
229/*
230 * Print trap reason.
231 */
232
233void
234kdbprinttrap(
235 int type,
236 int code,
237 int *pc,
238 int sp)
239{
240 printf("kernel: ");
241 if (type > TRAP_TYPES)
242 db_printf("type %d", type);
243 else
244 db_printf("%s", trap_type[type]);
245 db_printf(" trap, code=%x pc@%x = %x sp=%x\n",
246 code, pc, *(int *)pc, sp);
247 db_run_mode = STEP_CONTINUE;
248}
249
250/*
251 *
252 */
2d21ac55
A
253static addr64_t
254db_vtophys(pmap_t pmap, vm_offset_t va)
1c79356b 255{
55e303ae
A
256 ppnum_t pp;
257 addr64_t pa;
1c79356b 258
55e303ae 259 pp = pmap_find_phys(pmap, (addr64_t)va);
de355530 260
55e303ae
A
261 if (pp == 0) return(0); /* Couldn't find it */
262
263 pa = ((addr64_t)pp << 12) | (addr64_t)(va & 0xFFF); /* Get physical address */
1c79356b
A
264
265 return(pa);
266}
267
1c79356b
A
268/*
269 * Read bytes from task address space for debugger.
270 */
271void
272db_read_bytes(
273 vm_offset_t addr,
274 int size,
275 char *data,
276 task_t task)
277{
278 int n,max;
55e303ae
A
279 addr64_t phys_dst;
280 addr64_t phys_src;
1c79356b
A
281 pmap_t pmap;
282
283 while (size > 0) {
284 if (task != NULL)
285 pmap = task->map->pmap;
286 else
287 pmap = kernel_pmap;
288
55e303ae 289 phys_src = db_vtophys(pmap, (vm_offset_t)addr);
1c79356b
A
290 if (phys_src == 0) {
291 db_printf("\nno memory is assigned to src address %08x\n",
292 addr);
293 db_error(0);
294 /* NOTREACHED */
295 }
1c79356b 296
55e303ae 297 phys_dst = db_vtophys(kernel_pmap, (vm_offset_t)data);
1c79356b
A
298 if (phys_dst == 0) {
299 db_printf("\nno memory is assigned to dst address %08x\n",
300 data);
301 db_error(0);
302 /* NOTREACHED */
303 }
304
1c79356b 305 /* don't over-run any page boundaries - check src range */
55e303ae 306 max = round_page_64(phys_src + 1) - phys_src;
1c79356b
A
307 if (max > size)
308 max = size;
309 /* Check destination won't run over boundary either */
55e303ae
A
310 n = round_page_64(phys_dst + 1) - phys_dst;
311
312 if (n < max) max = n;
1c79356b
A
313 size -= max;
314 addr += max;
315 phys_copy(phys_src, phys_dst, max);
316
317 /* resync I+D caches */
55e303ae 318 sync_cache64(phys_dst, max);
1c79356b
A
319
320 phys_src += max;
321 phys_dst += max;
322 }
323}
324
325/*
326 * Write bytes to task address space for debugger.
327 */
328void
329db_write_bytes(
330 vm_offset_t addr,
331 int size,
332 char *data,
333 task_t task)
334{
335 int n,max;
55e303ae
A
336 addr64_t phys_dst;
337 addr64_t phys_src;
1c79356b
A
338 pmap_t pmap;
339
340 while (size > 0) {
341
55e303ae 342 phys_src = db_vtophys(kernel_pmap, (vm_offset_t)data);
1c79356b
A
343 if (phys_src == 0) {
344 db_printf("\nno memory is assigned to src address %08x\n",
345 data);
346 db_error(0);
347 /* NOTREACHED */
348 }
349
1c79356b
A
350 /* space stays as kernel space unless in another task */
351 if (task == NULL) pmap = kernel_pmap;
352 else pmap = task->map->pmap;
353
55e303ae 354 phys_dst = db_vtophys(pmap, (vm_offset_t)addr);
1c79356b
A
355 if (phys_dst == 0) {
356 db_printf("\nno memory is assigned to dst address %08x\n",
357 addr);
358 db_error(0);
359 /* NOTREACHED */
360 }
1c79356b
A
361
362 /* don't over-run any page boundaries - check src range */
55e303ae 363 max = round_page_64(phys_src + 1) - phys_src;
1c79356b
A
364 if (max > size)
365 max = size;
366 /* Check destination won't run over boundary either */
55e303ae 367 n = round_page_64(phys_dst + 1) - phys_dst;
1c79356b
A
368 if (n < max)
369 max = n;
370 size -= max;
371 addr += max;
372 phys_copy(phys_src, phys_dst, max);
373
374 /* resync I+D caches */
55e303ae 375 sync_cache64(phys_dst, max);
1c79356b
A
376
377 phys_src += max;
378 phys_dst += max;
379 }
380}
381
382boolean_t
383db_check_access(
384 vm_offset_t addr,
385 int size,
386 task_t task)
387{
388 register int n;
1c79356b
A
389
390 if (task == kernel_task || task == TASK_NULL) {
55e303ae 391 if (kernel_task == TASK_NULL) return(TRUE);
1c79356b
A
392 task = kernel_task;
393 } else if (task == TASK_NULL) {
91447636
A
394 if (current_thread() == THR_ACT_NULL) return(FALSE);
395 task = current_thread()->task;
1c79356b 396 }
55e303ae 397
1c79356b 398 while (size > 0) {
55e303ae
A
399 if(!pmap_find_phys(task->map->pmap, (addr64_t)addr)) return (FALSE); /* Fail if page not mapped */
400 n = trunc_page_32(addr+PPC_PGBYTES) - addr;
1c79356b
A
401 if (n > size)
402 n = size;
403 size -= n;
404 addr += n;
405 }
406 return(TRUE);
407}
408
409boolean_t
410db_phys_eq(
411 task_t task1,
412 vm_offset_t addr1,
413 task_t task2,
414 vm_offset_t addr2)
415{
55e303ae 416 addr64_t physa, physb;
1c79356b
A
417
418 if ((addr1 & (PPC_PGBYTES-1)) != (addr2 & (PPC_PGBYTES-1))) /* Is byte displacement the same? */
419 return FALSE;
420
421 if (task1 == TASK_NULL) { /* See if there is a task active */
91447636 422 if (current_thread() == THR_ACT_NULL) /* See if there is a current task */
1c79356b 423 return FALSE;
91447636 424 task1 = current_thread()->task; /* If so, use that one */
1c79356b
A
425 }
426
55e303ae
A
427 if(!(physa = db_vtophys(task1->map->pmap, (vm_offset_t)trunc_page_32(addr1)))) return FALSE; /* Get real address of the first */
428 if(!(physb = db_vtophys(task2->map->pmap, (vm_offset_t)trunc_page_32(addr2)))) return FALSE; /* Get real address of the second */
1c79356b
A
429
430 return (physa == physb); /* Check if they are equal, then return... */
431}
432
433#define DB_USER_STACK_ADDR (0xc0000000)
434#define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(PPC_PGBYTES*3))
435
2d21ac55
A
436boolean_t
437db_phys_cmp(__unused vm_offset_t a1, __unused vm_offset_t a2,
438 __unused vm_size_t s1)
439{
55e303ae
A
440 db_printf("db_phys_cmp: not implemented\n");
441 return 0;
442}
443
444
1c79356b 445int
2d21ac55
A
446db_search_null(__unused task_t task, __unused unsigned *svaddr,
447 __unused unsigned evaddr, __unused unsigned *skaddr,
448 __unused int flag)
1c79356b 449{
55e303ae 450 db_printf("db_search_null: not implemented\n");
1c79356b
A
451 return(-1);
452}
453
2d21ac55 454struct proc;
55e303ae
A
455unsigned char *getProcName(struct proc *proc);
456
1c79356b
A
457void
458db_task_name(
459 task_t task)
460{
55e303ae 461 register unsigned char *p;
55e303ae
A
462 unsigned char tname[33];
463 int i;
1c79356b 464
55e303ae
A
465 p = 0;
466 tname[0] = 0;
467
468 if(task->bsd_info) p = getProcName((struct proc *)(task->bsd_info)); /* Point to task name */
469
470 if(p) {
471 for(i = 0; i < 32; i++) { /* Move no more than 32 bytes */
472 tname[i] = p[i];
473 if(p[i] == 0) break;
474 }
475 tname[i] = 0;
476 db_printf("%s", tname);
1c79356b 477 }
55e303ae 478 else db_printf("no name");
1c79356b
A
479}
480
2d21ac55 481extern int kdb_flag;
1c79356b 482void
2d21ac55
A
483db_machdep_init(void)
484{
1c79356b 485#define KDB_READY 0x1
1c79356b
A
486 kdb_flag |= KDB_READY;
487}
488
489
490#ifdef __STDC__
2d21ac55
A
491//#define KDB_SAVE(type, name) extern type name; type name##_save = name
492#define KDB_SAVE(type, name) type name##_save = name
1c79356b
A
493#define KDB_RESTORE(name) name = name##_save
494#else /* __STDC__ */
2d21ac55
A
495#define KDB_SAVE(type, name) type name/**/_save = name
496//#define KDB_SAVE(type, name) extern type name; type name/**/_save = name
1c79356b
A
497#define KDB_RESTORE(name) name = name/**/_save
498#endif /* __STDC__ */
499
500#define KDB_SAVE_CTXT() \
501 KDB_SAVE(int, db_run_mode); \
502 KDB_SAVE(boolean_t, db_sstep_print); \
503 KDB_SAVE(int, db_loop_count); \
504 KDB_SAVE(int, db_call_depth); \
505 KDB_SAVE(int, db_inst_count); \
506 KDB_SAVE(int, db_last_inst_count); \
507 KDB_SAVE(int, db_load_count); \
508 KDB_SAVE(int, db_store_count); \
509 KDB_SAVE(boolean_t, db_cmd_loop_done); \
510 KDB_SAVE(jmp_buf_t *, db_recover); \
511 KDB_SAVE(db_addr_t, db_dot); \
512 KDB_SAVE(db_addr_t, db_last_addr); \
513 KDB_SAVE(db_addr_t, db_prev); \
514 KDB_SAVE(db_addr_t, db_next); \
515 KDB_SAVE(db_regs_t, ddb_regs);
516
517#define KDB_RESTORE_CTXT() \
518 KDB_RESTORE(db_run_mode); \
519 KDB_RESTORE(db_sstep_print); \
520 KDB_RESTORE(db_loop_count); \
521 KDB_RESTORE(db_call_depth); \
522 KDB_RESTORE(db_inst_count); \
523 KDB_RESTORE(db_last_inst_count); \
524 KDB_RESTORE(db_load_count); \
525 KDB_RESTORE(db_store_count); \
526 KDB_RESTORE(db_cmd_loop_done); \
527 KDB_RESTORE(db_recover); \
528 KDB_RESTORE(db_dot); \
529 KDB_RESTORE(db_last_addr); \
530 KDB_RESTORE(db_prev); \
531 KDB_RESTORE(db_next); \
532 KDB_RESTORE(ddb_regs);
533
2d21ac55
A
534extern boolean_t db_sstep_print;
535extern int db_loop_count;
536extern int db_call_depth;
537extern int db_inst_count;
538extern int db_last_inst_count;
539extern int db_load_count;
540extern int db_store_count;
541extern boolean_t db_cmd_loop_done;
542extern void unlock_debugger(void);
543extern void lock_debugger(void);
1c79356b
A
544/*
545 * switch to another cpu
546 */
547void
548kdb_on(
549 int cpu)
550{
551 KDB_SAVE_CTXT();
2d21ac55 552 if (cpu < 0 || cpu >= (int)real_ncpus || !PerProcTable[cpu].ppe_vaddr->debugger_active)
1c79356b
A
553 return;
554 db_set_breakpoints();
555 db_set_watchpoints();
556 debugger_cpu = cpu;
557 unlock_debugger();
558 lock_debugger();
559 db_clear_breakpoints();
560 db_clear_watchpoints();
561 KDB_RESTORE_CTXT();
562 if (debugger_cpu == -1) {/* someone continued */
563 debugger_cpu = cpu_number();
2d21ac55 564 db_continue_cmd(0, 0, 0, NULL);
1c79356b
A
565 }
566}
567
568/*
569 * system reboot
570 */
91447636 571
2d21ac55
A
572void
573db_reboot(__unused db_expr_t addr, __unused boolean_t have_addr,
574 __unused db_expr_t count, char *modif)
1c79356b
A
575{
576 boolean_t reboot = TRUE;
577 char *cp, c;
578
579 cp = modif;
580 while ((c = *cp++) != 0) {
581 if (c == 'r') /* reboot */
582 reboot = TRUE;
583 if (c == 'h') /* halt */
584 reboot = FALSE;
585 }
91447636
A
586 if(!reboot) halt_all_cpus(FALSE); /* If no reboot, try to be clean about it */
587
2d21ac55
A
588 if (PE_halt_restart)
589 (*PE_halt_restart)(kPERestartCPU);
91447636
A
590 db_printf("Sorry, system can't reboot automatically yet... You need to do it by hand...\n");
591
1c79356b 592}