1 /* $NetBSD: db_interface.c,v 1.46 2009/03/14 14:45:55 dsl Exp $ */
4 * Copyright (c) 1996 Scott K. Stevens
6 * Mach Operating System
7 * Copyright (c) 1991,1990 Carnegie Mellon University
10 * Permission to use, copy, modify and distribute this software and its
11 * documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
18 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 * Carnegie Mellon requests users of this software to return to
22 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
23 * School of Computer Science
24 * Carnegie Mellon University
25 * Pittsburgh PA 15213-3890
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
30 * From: db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
34 * Interface to new debugger.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.46 2009/03/14 14:45:55 dsl Exp $");
43 #include <sys/param.h>
45 #include <sys/reboot.h>
46 #include <sys/systm.h> /* just for boothowto */
49 #include <uvm/uvm_extern.h>
51 #include <arm/arm32/db_machdep.h>
52 #include <arm/arm32/katelib.h>
53 #include <arm/undefined.h>
54 #include <ddb/db_access.h>
55 #include <ddb/db_command.h>
56 #include <ddb/db_output.h>
57 #include <ddb/db_variables.h>
58 #include <ddb/db_sym.h>
59 #include <ddb/db_extern.h>
60 #include <ddb/db_interface.h>
63 #if defined(KGDB) || !defined(DDB)
64 #define db_printf printf
69 int db_access_und_sp(const struct db_variable
*, db_expr_t
*, int);
70 int db_access_abt_sp(const struct db_variable
*, db_expr_t
*, int);
71 int db_access_irq_sp(const struct db_variable
*, db_expr_t
*, int);
72 u_int
db_fetch_reg(int, db_regs_t
*);
74 int db_trapper(u_int
, u_int
, trapframe_t
*, int);
76 const struct db_variable db_regs
[] = {
77 { "spsr", (long *)&DDB_REGS
->tf_spsr
, FCN_NULL
, },
78 { "r0", (long *)&DDB_REGS
->tf_r0
, FCN_NULL
, },
79 { "r1", (long *)&DDB_REGS
->tf_r1
, FCN_NULL
, },
80 { "r2", (long *)&DDB_REGS
->tf_r2
, FCN_NULL
, },
81 { "r3", (long *)&DDB_REGS
->tf_r3
, FCN_NULL
, },
82 { "r4", (long *)&DDB_REGS
->tf_r4
, FCN_NULL
, },
83 { "r5", (long *)&DDB_REGS
->tf_r5
, FCN_NULL
, },
84 { "r6", (long *)&DDB_REGS
->tf_r6
, FCN_NULL
, },
85 { "r7", (long *)&DDB_REGS
->tf_r7
, FCN_NULL
, },
86 { "r8", (long *)&DDB_REGS
->tf_r8
, FCN_NULL
, },
87 { "r9", (long *)&DDB_REGS
->tf_r9
, FCN_NULL
, },
88 { "r10", (long *)&DDB_REGS
->tf_r10
, FCN_NULL
, },
89 { "r11", (long *)&DDB_REGS
->tf_r11
, FCN_NULL
, },
90 { "r12", (long *)&DDB_REGS
->tf_r12
, FCN_NULL
, },
91 { "usr_sp", (long *)&DDB_REGS
->tf_usr_sp
, FCN_NULL
, },
92 { "usr_lr", (long *)&DDB_REGS
->tf_usr_lr
, FCN_NULL
, },
93 { "svc_sp", (long *)&DDB_REGS
->tf_svc_sp
, FCN_NULL
, },
94 { "svc_lr", (long *)&DDB_REGS
->tf_svc_lr
, FCN_NULL
, },
95 { "pc", (long *)&DDB_REGS
->tf_pc
, FCN_NULL
, },
96 { "und_sp", &nil
, db_access_und_sp
, },
97 { "abt_sp", &nil
, db_access_abt_sp
, },
98 { "irq_sp", &nil
, db_access_irq_sp
, },
101 const struct db_variable
* const db_eregs
= db_regs
+ sizeof(db_regs
)/sizeof(db_regs
[0]);
104 db_regs_t ddb_regs
; /* register state */
107 db_access_und_sp(const struct db_variable
*vp
, db_expr_t
*valp
, int rw
)
110 if (rw
== DB_VAR_GET
)
111 *valp
= get_stackptr(PSR_UND32_MODE
);
116 db_access_abt_sp(const struct db_variable
*vp
, db_expr_t
*valp
, int rw
)
119 if (rw
== DB_VAR_GET
)
120 *valp
= get_stackptr(PSR_ABT32_MODE
);
125 db_access_irq_sp(const struct db_variable
*vp
, db_expr_t
*valp
, int rw
)
128 if (rw
== DB_VAR_GET
)
129 *valp
= get_stackptr(PSR_IRQ32_MODE
);
135 * kdb_trap - field a TRACE or BPT trap
138 kdb_trap(int type
, db_regs_t
*regs
)
143 case T_BREAKPOINT
: /* breakpoint */
144 case -1: /* keyboard interrupt */
147 if (db_recover
!= 0) {
148 /* This will longjmp back into db_command_loop() */
149 db_error("Faulted in DDB; continuing...\n");
154 /* Should switch to kdb`s own stack here. */
161 db_trap(type
, 0/*code*/);
173 db_validate_address(vaddr_t addr
)
175 struct proc
*p
= curproc
;
178 if (!p
|| !p
->p_vmspace
|| !p
->p_vmspace
->vm_map
.pmap
||
179 #ifndef ARM32_NEW_VM_LAYOUT
180 addr
>= VM_MAXUSER_ADDRESS
182 addr
>= VM_MIN_KERNEL_ADDRESS
185 pmap
= pmap_kernel();
187 pmap
= p
->p_vmspace
->vm_map
.pmap
;
189 return (pmap_extract(pmap
, addr
, NULL
) == false);
193 * Read bytes from kernel address space for debugger.
196 db_read_bytes(vaddr_t addr
, size_t size
, char *data
)
198 char *src
= (char *)addr
;
200 if (db_validate_address((u_int
)src
)) {
201 db_printf("address %p is invalid\n", src
);
205 if (size
== 4 && (addr
& 3) == 0 && ((uintptr_t)data
& 3) == 0) {
206 *((int*)data
) = *((int*)src
);
210 if (size
== 2 && (addr
& 1) == 0 && ((uintptr_t)data
& 1) == 0) {
211 *((short*)data
) = *((short*)src
);
216 if (db_validate_address((u_int
)src
)) {
217 db_printf("address %p is invalid\n", src
);
225 db_write_text(vaddr_t addr
, size_t size
, const char *data
)
227 struct pmap
*pmap
= pmap_kernel();
228 pd_entry_t
*pde
, oldpde
, tmppde
;
229 pt_entry_t
*pte
, oldpte
, tmppte
;
231 size_t limit
, savesize
;
237 if ((savesize
= size
) == 0)
243 /* Get the PDE of the current VA. */
244 if (pmap_get_pde_pte(pmap
, (vaddr_t
) dst
, &pde
, &pte
) == false)
246 switch ((oldpde
= *pde
) & L1_TYPE_MASK
) {
248 pgva
= (vaddr_t
)dst
& L1_S_FRAME
;
249 limit
= L1_S_SIZE
- ((vaddr_t
)dst
& L1_S_OFFSET
);
251 tmppde
= oldpde
| L1_S_PROT_W
;
257 pgva
= (vaddr_t
)dst
& L2_S_FRAME
;
258 limit
= L2_S_SIZE
- ((vaddr_t
)dst
& L2_S_OFFSET
);
263 tmppte
= oldpte
| L2_S_PROT_W
;
270 printf(" address 0x%08lx not a valid page\n",
274 cpu_tlb_flushD_SE(pgva
);
282 * Page is now writable. Do as much access as we
285 for (; limit
> 0; limit
--)
289 * Restore old mapping permissions.
291 switch (oldpde
& L1_TYPE_MASK
) {
302 cpu_tlb_flushD_SE(pgva
);
306 /* Sync the I-cache. */
307 cpu_icache_sync_range(addr
, savesize
);
311 * Write bytes to kernel address space for debugger.
314 db_write_bytes(vaddr_t addr
, size_t size
, const char *data
)
316 extern char kernel_text
[];
321 /* If any part is in kernel text, use db_write_text() */
322 if (addr
>= (vaddr_t
) kernel_text
&& addr
< (vaddr_t
) etext
) {
323 db_write_text(addr
, size
, data
);
328 if (db_validate_address((u_int
)dst
)) {
329 db_printf("address %p is invalid\n", dst
);
333 if (size
== 4 && (addr
& 3) == 0 && ((uintptr_t)data
& 3) == 0)
334 *((int*)dst
) = *((const int *)data
);
336 if (size
== 2 && (addr
& 1) == 0 && ((uintptr_t)data
& 1) == 0)
337 *((short*)dst
) = *((const short *)data
);
341 if (db_validate_address((u_int
)dst
)) {
342 db_printf("address %p is invalid\n", dst
);
349 /* make sure the caches and memory are in sync */
350 cpu_icache_sync_range(addr
, size
);
352 /* In case the current page tables have been modified ... */
361 __asm(".word 0xe7ffffff");
364 const struct db_command db_machine_command_table
[] = {
365 { DDB_ADD_CMD("frame", db_show_frame_cmd
, 0,
366 "Displays the contents of a trapframe",
368 " address:\taddress of trapfame to display")},
369 { DDB_ADD_CMD("panic", db_show_panic_cmd
, 0,
370 "Displays the last panic string",
372 #ifdef ARM32_DB_COMMANDS
375 { DDB_ADD_CMD(NULL
, NULL
, 0,NULL
,NULL
,NULL
) }
379 db_trapper(u_int addr
, u_int inst
, trapframe_t
*frame
, int fault_code
)
382 if (fault_code
== 0) {
383 if ((inst
& ~INSN_COND_MASK
) == (BKPT_INST
& ~INSN_COND_MASK
))
384 kdb_trap(T_BREAKPOINT
, frame
);
395 static struct undefined_handler db_uh
;
398 db_machine_init(void)
402 * We get called before malloc() is available, so supply a static
403 * struct undefined_handler.
405 db_uh
.uh_handler
= db_trapper
;
406 install_coproc_handler_static(CORE_UNKNOWN_HANDLER
, &db_uh
);
411 db_fetch_reg(int reg
, db_regs_t
*regs
)
416 return (regs
->tf_r0
);
418 return (regs
->tf_r1
);
420 return (regs
->tf_r2
);
422 return (regs
->tf_r3
);
424 return (regs
->tf_r4
);
426 return (regs
->tf_r5
);
428 return (regs
->tf_r6
);
430 return (regs
->tf_r7
);
432 return (regs
->tf_r8
);
434 return (regs
->tf_r9
);
436 return (regs
->tf_r10
);
438 return (regs
->tf_r11
);
440 return (regs
->tf_r12
);
442 return (regs
->tf_svc_sp
);
444 return (regs
->tf_svc_lr
);
446 return (regs
->tf_pc
);
448 panic("db_fetch_reg: botch");
453 branch_taken(u_int insn
, u_int pc
, db_regs_t
*regs
)
457 switch ((insn
>> 24) & 0xf) {
458 case 0xa: /* b ... */
459 case 0xb: /* bl ... */
460 addr
= ((insn
<< 2) & 0x03ffffff);
461 if (addr
& 0x02000000)
463 return (pc
+ 8 + addr
);
464 case 0x7: /* ldr pc, [pc, reg, lsl #2] */
465 addr
= db_fetch_reg(insn
& 0xf, regs
);
466 addr
= pc
+ 8 + (addr
<< 2);
467 db_read_bytes(addr
, 4, (char *)&addr
);
469 case 0x5: /* ldr pc, [reg] */
470 addr
= db_fetch_reg((insn
>> 16) & 0xf, regs
);
471 db_read_bytes(addr
, 4, (char *)&addr
);
473 case 0x1: /* mov pc, reg */
474 addr
= db_fetch_reg(insn
& 0xf, regs
);
476 case 0x8: /* ldmxx reg, {..., pc} */
478 addr
= db_fetch_reg((insn
>> 16) & 0xf, regs
);
479 nregs
= (insn
& 0x5555) + ((insn
>> 1) & 0x5555);
480 nregs
= (nregs
& 0x3333) + ((nregs
>> 2) & 0x3333);
481 nregs
= (nregs
+ (nregs
>> 4)) & 0x0f0f;
482 nregs
= (nregs
+ (nregs
>> 8)) & 0x001f;
483 switch ((insn
>> 23) & 0x3) {
484 case 0x0: /* ldmda */
487 case 0x1: /* ldmia */
488 addr
= addr
+ 0 + ((nregs
- 1) << 2);
490 case 0x2: /* ldmdb */
493 case 0x3: /* ldmib */
494 addr
= addr
+ 4 + ((nregs
- 1) << 2);
497 db_read_bytes(addr
, 4, (char *)&addr
);
500 panic("branch_taken: botch");