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