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