]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ppc/db_interface.c
xnu-344.23.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 *
de355530
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 *
de355530
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,
de355530
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);
de355530
A
118extern int db_user_to_kernel_address(
119 task_t task,
120 vm_offset_t addr,
121 unsigned *kaddr,
122 int flag);
1c79356b
A
123extern void db_write_bytes_user_space(
124 vm_offset_t addr,
125 int size,
126 char *data,
127 task_t task);
128extern int db_search_null(
129 task_t task,
130 unsigned *svaddr,
131 unsigned evaddr,
132 unsigned *skaddr,
133 int flag);
134extern int kdb_enter(int);
135extern void kdb_leave(void);
136extern void lock_kdb(void);
137extern void unlock_kdb(void);
138
139#if DB_MACHINE_COMMANDS
140struct db_command ppc_db_commands[] = {
141 { "lt", db_low_trace, CS_MORE|CS_SET_DOT, 0 },
142 { (char *)0, 0, 0, 0 }
143};
144#endif /* DB_MACHINE_COMMANDS */
145
146#if !MACH_KDP
147void kdp_register_send_receive(void) {}
148#endif
149
150extern jmp_buf_t *db_recover;
151spl_t saved_ipl[NCPUS]; /* just to know what IPL was before trap */
9bccf70c 152struct savearea *saved_state[NCPUS];
1c79356b
A
153
154/*
155 * kdb_trap - field a TRACE or BPT trap
156 */
157void
158kdb_trap(
159 int type,
9bccf70c 160 struct savearea *regs)
1c79356b
A
161{
162 boolean_t trap_from_user;
163 int previous_console_device;
164 int code=0;
165
166 previous_console_device=switch_to_serial_console();
167
168 switch (type) {
169 case T_TRACE: /* single_step */
170 case T_PROGRAM: /* breakpoint */
171#if 0
172 case T_WATCHPOINT: /* watchpoint */
173#endif
174 case -1: /* keyboard interrupt */
175 break;
176
177 default:
178 if (db_recover) {
179 ppc_nested_saved_state = *regs;
180 db_printf("Caught ");
181 if (type > TRAP_TYPES)
182 db_printf("type %d", type);
183 else
184 db_printf("%s", trap_type[type]);
185 db_printf(" trap, pc = %x\n",
9bccf70c 186 regs->save_srr0);
1c79356b
A
187 db_error("");
188 /*NOTREACHED*/
189 }
9bccf70c 190 kdbprinttrap(type, code, (int *)&regs->save_srr0, regs->save_r1);
1c79356b
A
191 }
192
193 saved_state[cpu_number()] = regs;
194
195 ppc_last_saved_statep = regs;
196 ppc_last_kdb_sp = (unsigned) &type;
197
198 if (!IS_USER_TRAP(regs)) {
199 bzero((char *)&ddb_regs, sizeof (ddb_regs));
200 ddb_regs = *regs;
201 trap_from_user = FALSE;
202
203 }
204 else {
205 ddb_regs = *regs;
206 trap_from_user = TRUE;
207 }
208
209 db_task_trap(type, code, trap_from_user);
210
211 *regs = ddb_regs;
212
213 if ((type == T_PROGRAM) &&
9bccf70c 214 (db_get_task_value(regs->save_srr0,
1c79356b
A
215 BKPT_SIZE,
216 FALSE,
217 db_target_space(current_act(),
218 trap_from_user))
219 == BKPT_INST))
9bccf70c 220 regs->save_srr0 += BKPT_SIZE;
1c79356b
A
221
222kdb_exit:
223 saved_state[cpu_number()] = 0;
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 */
de355530 253vm_offset_t db_vtophys(
1c79356b
A
254 pmap_t pmap,
255 vm_offset_t va)
256{
de355530
A
257 register mapping *mp;
258 register vm_offset_t pa;
1c79356b 259
de355530 260 pa = (vm_offset_t)LRA(pmap->space,(void *)va);
1c79356b 261
de355530
A
262 if (pa != 0)
263 return(pa);
264
265 mp = hw_lock_phys_vir(pmap->space, va);
266 if((unsigned int)mp&1) {
267 return 0;
268 }
269
270 if(!mp) { /* If it was not a normal page */
271 pa = hw_cvp_blk(pmap, va); /* Try to convert odd-sized page (returns 0 if not found) */
272 return pa; /* Return physical address */
273 }
274
275 mp = hw_cpv(mp); /* Convert to virtual address */
276
277 if(!mp->physent) {
278 pa = (vm_offset_t)((mp->PTEr & -PAGE_SIZE) | ((unsigned int)va & (PAGE_SIZE-1)));
279 } else {
280 pa = (vm_offset_t)((mp->physent->pte1 & -PAGE_SIZE) | ((unsigned int)va & (PAGE_SIZE-1)));
281 hw_unlock_bit((unsigned int *)&mp->physent->phys_link, PHYS_LOCK);
282 }
1c79356b
A
283
284 return(pa);
285}
286
de355530
A
287int
288db_user_to_kernel_address(
289 task_t task,
290 vm_offset_t addr,
291 unsigned *kaddr,
292 int flag)
293{
294 unsigned int sr_val, raddr;
295
296 raddr = (unsigned int)db_vtophys(task->map->pmap, trunc_page(addr)); /* Get the real address */
297
298 if (!raddr) {
299 if (flag) {
300 db_printf("\nno memory is assigned to address %08x\n", addr);
301 db_error(0);
302 /* NOTREACHED */
303 }
304 return -1;
305 }
306 sr_val = SEG_REG_PROT | task->map->pmap->space
307 | ((addr >> 8) & 0x00F00000);
308
309 mtsr(SR_COPYIN_NUM, sr_val);
310 sync();
311 *kaddr = (addr & 0x0fffffff) | (SR_COPYIN_NUM << 28);
312 return(0);
313}
314
1c79356b
A
315/*
316 * Read bytes from task address space for debugger.
317 */
318void
319db_read_bytes(
320 vm_offset_t addr,
321 int size,
322 char *data,
323 task_t task)
324{
325 int n,max;
de355530
A
326 unsigned phys_dst;
327 unsigned phys_src;
1c79356b
A
328 pmap_t pmap;
329
330 while (size > 0) {
331 if (task != NULL)
332 pmap = task->map->pmap;
333 else
334 pmap = kernel_pmap;
335
de355530 336 phys_src = (unsigned int)db_vtophys(pmap, trunc_page(addr));
1c79356b
A
337 if (phys_src == 0) {
338 db_printf("\nno memory is assigned to src address %08x\n",
339 addr);
340 db_error(0);
341 /* NOTREACHED */
342 }
de355530 343 phys_src = phys_src| (addr & page_mask);
1c79356b 344
de355530 345 phys_dst = (unsigned int)db_vtophys(kernel_pmap, trunc_page(data));
1c79356b
A
346 if (phys_dst == 0) {
347 db_printf("\nno memory is assigned to dst address %08x\n",
348 data);
349 db_error(0);
350 /* NOTREACHED */
351 }
352
de355530
A
353 phys_dst = phys_dst | (((vm_offset_t) data) & page_mask);
354
1c79356b 355 /* don't over-run any page boundaries - check src range */
de355530 356 max = ppc_round_page(phys_src) - phys_src;
1c79356b
A
357 if (max > size)
358 max = size;
359 /* Check destination won't run over boundary either */
de355530
A
360 n = ppc_round_page(phys_dst) - phys_dst;
361 if (n < max)
362 max = n;
1c79356b
A
363 size -= max;
364 addr += max;
365 phys_copy(phys_src, phys_dst, max);
366
367 /* resync I+D caches */
de355530 368 sync_cache(phys_dst, max);
1c79356b
A
369
370 phys_src += max;
371 phys_dst += max;
372 }
373}
374
375/*
376 * Write bytes to task address space for debugger.
377 */
378void
379db_write_bytes(
380 vm_offset_t addr,
381 int size,
382 char *data,
383 task_t task)
384{
385 int n,max;
de355530
A
386 unsigned phys_dst;
387 unsigned phys_src;
1c79356b
A
388 pmap_t pmap;
389
390 while (size > 0) {
391
de355530 392 phys_src = (unsigned int)db_vtophys(kernel_pmap, trunc_page(data));
1c79356b
A
393 if (phys_src == 0) {
394 db_printf("\nno memory is assigned to src address %08x\n",
395 data);
396 db_error(0);
397 /* NOTREACHED */
398 }
399
de355530
A
400 phys_src = phys_src | (((vm_offset_t) data) & page_mask);
401
1c79356b
A
402 /* space stays as kernel space unless in another task */
403 if (task == NULL) pmap = kernel_pmap;
404 else pmap = task->map->pmap;
405
de355530 406 phys_dst = (unsigned int)db_vtophys(pmap, trunc_page(addr));
1c79356b
A
407 if (phys_dst == 0) {
408 db_printf("\nno memory is assigned to dst address %08x\n",
409 addr);
410 db_error(0);
411 /* NOTREACHED */
412 }
de355530 413 phys_dst = phys_dst| (addr & page_mask);
1c79356b
A
414
415 /* don't over-run any page boundaries - check src range */
de355530 416 max = ppc_round_page(phys_src) - phys_src;
1c79356b
A
417 if (max > size)
418 max = size;
419 /* Check destination won't run over boundary either */
de355530 420 n = ppc_round_page(phys_dst) - phys_dst;
1c79356b
A
421 if (n < max)
422 max = n;
423 size -= max;
424 addr += max;
425 phys_copy(phys_src, phys_dst, max);
426
427 /* resync I+D caches */
de355530 428 sync_cache(phys_dst, max);
1c79356b
A
429
430 phys_src += max;
431 phys_dst += max;
432 }
433}
434
435boolean_t
436db_check_access(
437 vm_offset_t addr,
438 int size,
439 task_t task)
440{
441 register int n;
442 unsigned int kern_addr;
443
444 if (task == kernel_task || task == TASK_NULL) {
de355530
A
445 if (kernel_task == TASK_NULL)
446 return(TRUE);
1c79356b
A
447 task = kernel_task;
448 } else if (task == TASK_NULL) {
de355530
A
449 if (current_act() == THR_ACT_NULL)
450 return(FALSE);
1c79356b
A
451 task = current_act()->task;
452 }
453 while (size > 0) {
de355530
A
454 if (db_user_to_kernel_address(task, addr, &kern_addr, 0) < 0)
455 return(FALSE);
456 n = ppc_trunc_page(addr+PPC_PGBYTES) - addr;
1c79356b
A
457 if (n > size)
458 n = size;
459 size -= n;
460 addr += n;
461 }
462 return(TRUE);
463}
464
465boolean_t
466db_phys_eq(
467 task_t task1,
468 vm_offset_t addr1,
469 task_t task2,
470 vm_offset_t addr2)
471{
de355530 472 vm_offset_t physa, physb;
1c79356b
A
473
474 if ((addr1 & (PPC_PGBYTES-1)) != (addr2 & (PPC_PGBYTES-1))) /* Is byte displacement the same? */
475 return FALSE;
476
477 if (task1 == TASK_NULL) { /* See if there is a task active */
478 if (current_act() == THR_ACT_NULL) /* See if there is a current task */
479 return FALSE;
480 task1 = current_act()->task; /* If so, use that one */
481 }
482
de355530
A
483 if(!(physa = db_vtophys(task1->map->pmap, trunc_page(addr1)))) return FALSE; /* Get real address of the first */
484 if(!(physb = db_vtophys(task2->map->pmap, trunc_page(addr2)))) return FALSE; /* Get real address of the second */
1c79356b
A
485
486 return (physa == physb); /* Check if they are equal, then return... */
487}
488
489#define DB_USER_STACK_ADDR (0xc0000000)
490#define DB_NAME_SEARCH_LIMIT (DB_USER_STACK_ADDR-(PPC_PGBYTES*3))
491
492int
493db_search_null(
494 task_t task,
495 unsigned *svaddr,
496 unsigned evaddr,
497 unsigned *skaddr,
498 int flag)
499{
500 register unsigned vaddr;
501 register unsigned *kaddr;
502
de355530
A
503 kaddr = (unsigned *)*skaddr;
504 for (vaddr = *svaddr; vaddr > evaddr; ) {
505 if (vaddr % PPC_PGBYTES == 0) {
506 vaddr -= sizeof(unsigned);
507 if (db_user_to_kernel_address(task, vaddr, skaddr, 0) < 0)
508 return(-1);
509 kaddr = (unsigned *)*skaddr;
510 } else {
511 vaddr -= sizeof(unsigned);
512 kaddr--;
513 }
514 if ((*kaddr == 0) ^ (flag == 0)) {
515 *svaddr = vaddr;
516 *skaddr = (unsigned)kaddr;
517 return(0);
518 }
519 }
1c79356b
A
520 return(-1);
521}
522
523void
524db_task_name(
525 task_t task)
526{
de355530 527 register char *p;
1c79356b
A
528 register int n;
529 unsigned int vaddr, kaddr;
530
de355530
A
531 vaddr = DB_USER_STACK_ADDR;
532 kaddr = 0;
533
534 /*
535 * skip nulls at the end
536 */
537 if (db_search_null(task, &vaddr, DB_NAME_SEARCH_LIMIT, &kaddr, 0) < 0) {
538 db_printf(DB_NULL_TASK_NAME);
539 return;
540 }
541 /*
542 * search start of args
543 */
544 if (db_search_null(task, &vaddr, DB_NAME_SEARCH_LIMIT, &kaddr, 1) < 0) {
545 db_printf(DB_NULL_TASK_NAME);
546 return;
547 }
548
549 n = DB_TASK_NAME_LEN-1;
550 p = (char *)kaddr + sizeof(unsigned);
551 for (vaddr += sizeof(int); vaddr < DB_USER_STACK_ADDR && n > 0;
552 vaddr++, p++, n--) {
553 if (vaddr % PPC_PGBYTES == 0) {
554 if (db_user_to_kernel_address(task, vaddr, &kaddr, 0) <0)
555 return;
556 p = (char*)kaddr;
557 }
558 db_printf("%c", (*p < ' ' || *p > '~')? ' ': *p);
1c79356b 559 }
de355530
A
560 while (n-- >= 0) /* compare with >= 0 for one more space */
561 db_printf(" ");
1c79356b
A
562}
563
564void
565db_machdep_init(void) {
566#define KDB_READY 0x1
567 extern int kdb_flag;
568
569 kdb_flag |= KDB_READY;
570}
571
572
573#ifdef __STDC__
574#define KDB_SAVE(type, name) extern type name; type name##_save = name
575#define KDB_RESTORE(name) name = name##_save
576#else /* __STDC__ */
577#define KDB_SAVE(type, name) extern type name; type name/**/_save = name
578#define KDB_RESTORE(name) name = name/**/_save
579#endif /* __STDC__ */
580
581#define KDB_SAVE_CTXT() \
582 KDB_SAVE(int, db_run_mode); \
583 KDB_SAVE(boolean_t, db_sstep_print); \
584 KDB_SAVE(int, db_loop_count); \
585 KDB_SAVE(int, db_call_depth); \
586 KDB_SAVE(int, db_inst_count); \
587 KDB_SAVE(int, db_last_inst_count); \
588 KDB_SAVE(int, db_load_count); \
589 KDB_SAVE(int, db_store_count); \
590 KDB_SAVE(boolean_t, db_cmd_loop_done); \
591 KDB_SAVE(jmp_buf_t *, db_recover); \
592 KDB_SAVE(db_addr_t, db_dot); \
593 KDB_SAVE(db_addr_t, db_last_addr); \
594 KDB_SAVE(db_addr_t, db_prev); \
595 KDB_SAVE(db_addr_t, db_next); \
596 KDB_SAVE(db_regs_t, ddb_regs);
597
598#define KDB_RESTORE_CTXT() \
599 KDB_RESTORE(db_run_mode); \
600 KDB_RESTORE(db_sstep_print); \
601 KDB_RESTORE(db_loop_count); \
602 KDB_RESTORE(db_call_depth); \
603 KDB_RESTORE(db_inst_count); \
604 KDB_RESTORE(db_last_inst_count); \
605 KDB_RESTORE(db_load_count); \
606 KDB_RESTORE(db_store_count); \
607 KDB_RESTORE(db_cmd_loop_done); \
608 KDB_RESTORE(db_recover); \
609 KDB_RESTORE(db_dot); \
610 KDB_RESTORE(db_last_addr); \
611 KDB_RESTORE(db_prev); \
612 KDB_RESTORE(db_next); \
613 KDB_RESTORE(ddb_regs);
614
615/*
616 * switch to another cpu
617 */
618void
619kdb_on(
620 int cpu)
621{
622 KDB_SAVE_CTXT();
623 if (cpu < 0 || cpu >= NCPUS || !debugger_active[cpu])
624 return;
625 db_set_breakpoints();
626 db_set_watchpoints();
627 debugger_cpu = cpu;
628 unlock_debugger();
629 lock_debugger();
630 db_clear_breakpoints();
631 db_clear_watchpoints();
632 KDB_RESTORE_CTXT();
633 if (debugger_cpu == -1) {/* someone continued */
634 debugger_cpu = cpu_number();
635 db_continue_cmd(0, 0, 0, "");
636 }
637}
638
639/*
640 * system reboot
641 */
642void db_reboot(
643 db_expr_t addr,
644 boolean_t have_addr,
645 db_expr_t count,
646 char *modif)
647{
648 boolean_t reboot = TRUE;
649 char *cp, c;
650
651 cp = modif;
652 while ((c = *cp++) != 0) {
653 if (c == 'r') /* reboot */
654 reboot = TRUE;
655 if (c == 'h') /* halt */
656 reboot = FALSE;
657 }
658 halt_all_cpus(reboot);
659}
660
661/*
662 * Switch to gdb
663 */
664void
665db_to_gdb(
666 void)
667{
668 extern unsigned int switch_debugger;
669
670 switch_debugger=1;
671}