]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/db_interface.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / ppc / db_interface.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
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>
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>
91447636
A
62#include <pexpert/pexpert.h>
63#include <IOKit/IOPlatformExpert.h>
1c79356b
A
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
9bccf70c
A
75struct savearea *ppc_last_saved_statep;
76struct savearea ppc_nested_saved_state;
1c79356b
A
77unsigned ppc_last_kdb_sp;
78
1c79356b
A
79extern int debugger_cpu; /* Current cpu running debugger */
80
81int db_all_set_up = 0;
82
83
84#if !MACH_KDP
85void 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 */
93struct int_regs {
94 /* XXX more registers ? */
95 struct ppc_interrupt_state *is;
96};
97
98extern char * trap_type[];
99extern 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
106decl_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
112extern volatile unsigned int cpus_holding_bkpts; /* counter for number of cpus holding
113 breakpoints (ie: cpus that did not
114 insert back breakpoints) */
115extern boolean_t db_breakpoints_inserted;
116
117/* Forward */
118
119extern void kdbprinttrap(
120 int type,
121 int code,
122 int *pc,
123 int sp);
1c79356b
A
124extern void db_write_bytes_user_space(
125 vm_offset_t addr,
126 int size,
127 char *data,
128 task_t task);
129extern int db_search_null(
130 task_t task,
131 unsigned *svaddr,
132 unsigned evaddr,
133 unsigned *skaddr,
134 int flag);
135extern int kdb_enter(int);
136extern void kdb_leave(void);
137extern void lock_kdb(void);
138extern void unlock_kdb(void);
139
140#if DB_MACHINE_COMMANDS
141struct 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
148void kdp_register_send_receive(void) {}
149#endif
150
151extern jmp_buf_t *db_recover;
1c79356b
A
152
153/*
154 * kdb_trap - field a TRACE or BPT trap
155 */
156void
157kdb_trap(
158 int type,
9bccf70c 159 struct savearea *regs)
1c79356b
A
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]);
91447636 184 db_printf(" trap, pc = %llx\n",
9bccf70c 185 regs->save_srr0);
1c79356b
A
186 db_error("");
187 /*NOTREACHED*/
188 }
9bccf70c 189 kdbprinttrap(type, code, (int *)&regs->save_srr0, regs->save_r1);
1c79356b
A
190 }
191
91447636 192 getPerProc()->db_saved_state = regs;
1c79356b
A
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) &&
9bccf70c 213 (db_get_task_value(regs->save_srr0,
1c79356b
A
214 BKPT_SIZE,
215 FALSE,
91447636 216 db_target_space(current_thread(),
1c79356b
A
217 trap_from_user))
218 == BKPT_INST))
9bccf70c 219 regs->save_srr0 += BKPT_SIZE;
1c79356b
A
220
221kdb_exit:
91447636 222 getPerProc()->db_saved_state = 0;
1c79356b
A
223 switch_to_old_console(previous_console_device);
224
225}
226
227
228/*
229 * Print trap reason.
230 */
231
232void
233kdbprinttrap(
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 */
55e303ae 252addr64_t db_vtophys(
1c79356b
A
253 pmap_t pmap,
254 vm_offset_t va)
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;
389 unsigned int kern_addr;
390
391 if (task == kernel_task || task == TASK_NULL) {
55e303ae 392 if (kernel_task == TASK_NULL) return(TRUE);
1c79356b
A
393 task = kernel_task;
394 } else if (task == TASK_NULL) {
91447636
A
395 if (current_thread() == THR_ACT_NULL) return(FALSE);
396 task = current_thread()->task;
1c79356b 397 }
55e303ae 398
1c79356b 399 while (size > 0) {
55e303ae
A
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;
1c79356b
A
402 if (n > size)
403 n = size;
404 size -= n;
405 addr += n;
406 }
407 return(TRUE);
408}
409
410boolean_t
411db_phys_eq(
412 task_t task1,
413 vm_offset_t addr1,
414 task_t task2,
415 vm_offset_t addr2)
416{
55e303ae 417 addr64_t physa, physb;
1c79356b
A
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 */
91447636 423 if (current_thread() == THR_ACT_NULL) /* See if there is a current task */
1c79356b 424 return FALSE;
91447636 425 task1 = current_thread()->task; /* If so, use that one */
1c79356b
A
426 }
427
55e303ae
A
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 */
1c79356b
A
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
55e303ae
A
437boolean_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
1c79356b
A
447int
448db_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
55e303ae
A
458 db_printf("db_search_null: not implemented\n");
459
1c79356b
A
460 return(-1);
461}
462
55e303ae
A
463unsigned char *getProcName(struct proc *proc);
464
1c79356b
A
465void
466db_task_name(
467 task_t task)
468{
55e303ae 469 register unsigned char *p;
1c79356b
A
470 register int n;
471 unsigned int vaddr, kaddr;
55e303ae
A
472 unsigned char tname[33];
473 int i;
1c79356b 474
55e303ae
A
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);
1c79356b 487 }
55e303ae 488 else db_printf("no name");
1c79356b
A
489}
490
491void
492db_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 */
545void
546kdb_on(
547 int cpu)
548{
549 KDB_SAVE_CTXT();
91447636 550 if (cpu < 0 || cpu >= real_ncpus || !PerProcTable[cpu].ppe_vaddr->debugger_active)
1c79356b
A
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 */
91447636
A
569
570extern int (*PE_halt_restart)(unsigned int type);
571
1c79356b
A
572void 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 }
91447636
A
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
1c79356b 593}
8f6c56a5
A
594
595/*
596 * Switch to gdb
597 */
598void
599db_to_gdb(
600 void)
601{
602 extern unsigned int switch_debugger;
603
604 switch_debugger=1;
605}