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