db_interface.c revision 1.8 1 1.8 thorpej /* $NetBSD: db_interface.c,v 1.8 2001/11/09 07:21:37 thorpej Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * Copyright (c) 1996 Scott K. Stevens
5 1.1 matt *
6 1.1 matt * Mach Operating System
7 1.1 matt * Copyright (c) 1991,1990 Carnegie Mellon University
8 1.1 matt * All Rights Reserved.
9 1.1 matt *
10 1.1 matt * Permission to use, copy, modify and distribute this software and its
11 1.1 matt * documentation is hereby granted, provided that both the copyright
12 1.1 matt * notice and this permission notice appear in all copies of the
13 1.1 matt * software, derivative works or modified versions, and any portions
14 1.1 matt * thereof, and that both notices appear in supporting documentation.
15 1.1 matt *
16 1.1 matt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 1.1 matt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
18 1.1 matt * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 1.1 matt *
20 1.1 matt * Carnegie Mellon requests users of this software to return to
21 1.1 matt *
22 1.1 matt * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
23 1.1 matt * School of Computer Science
24 1.1 matt * Carnegie Mellon University
25 1.1 matt * Pittsburgh PA 15213-3890
26 1.1 matt *
27 1.1 matt * any improvements or extensions that they make and grant Carnegie the
28 1.1 matt * rights to redistribute these changes.
29 1.1 matt *
30 1.1 matt * From: db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
31 1.1 matt */
32 1.1 matt
33 1.1 matt /*
34 1.1 matt * Interface to new debugger.
35 1.1 matt */
36 1.1 matt #include "opt_ddb.h"
37 1.1 matt
38 1.1 matt #include <sys/param.h>
39 1.1 matt #include <sys/proc.h>
40 1.1 matt #include <sys/reboot.h>
41 1.1 matt #include <sys/systm.h> /* just for boothowto */
42 1.1 matt #include <sys/exec.h>
43 1.1 matt
44 1.1 matt #include <uvm/uvm_extern.h>
45 1.1 matt
46 1.1 matt #include <machine/db_machdep.h>
47 1.1 matt #include <machine/katelib.h>
48 1.1 matt #include <machine/pte.h>
49 1.1 matt #include <machine/undefined.h>
50 1.1 matt #include <ddb/db_command.h>
51 1.1 matt #include <ddb/db_output.h>
52 1.1 matt #include <ddb/db_variables.h>
53 1.1 matt #include <ddb/db_sym.h>
54 1.1 matt #include <ddb/db_extern.h>
55 1.1 matt #include <ddb/db_interface.h>
56 1.1 matt #include <dev/cons.h>
57 1.1 matt
58 1.1 matt static int nil;
59 1.1 matt
60 1.1 matt int db_access_und_sp __P((const struct db_variable *, db_expr_t *, int));
61 1.1 matt int db_access_abt_sp __P((const struct db_variable *, db_expr_t *, int));
62 1.1 matt int db_access_irq_sp __P((const struct db_variable *, db_expr_t *, int));
63 1.1 matt u_int db_fetch_reg __P((int, db_regs_t *));
64 1.1 matt
65 1.1 matt const struct db_variable db_regs[] = {
66 1.1 matt { "spsr", (long *)&DDB_REGS->tf_spsr, FCN_NULL, },
67 1.1 matt { "r0", (long *)&DDB_REGS->tf_r0, FCN_NULL, },
68 1.1 matt { "r1", (long *)&DDB_REGS->tf_r1, FCN_NULL, },
69 1.1 matt { "r2", (long *)&DDB_REGS->tf_r2, FCN_NULL, },
70 1.1 matt { "r3", (long *)&DDB_REGS->tf_r3, FCN_NULL, },
71 1.1 matt { "r4", (long *)&DDB_REGS->tf_r4, FCN_NULL, },
72 1.1 matt { "r5", (long *)&DDB_REGS->tf_r5, FCN_NULL, },
73 1.1 matt { "r6", (long *)&DDB_REGS->tf_r6, FCN_NULL, },
74 1.1 matt { "r7", (long *)&DDB_REGS->tf_r7, FCN_NULL, },
75 1.1 matt { "r8", (long *)&DDB_REGS->tf_r8, FCN_NULL, },
76 1.1 matt { "r9", (long *)&DDB_REGS->tf_r9, FCN_NULL, },
77 1.1 matt { "r10", (long *)&DDB_REGS->tf_r10, FCN_NULL, },
78 1.1 matt { "r11", (long *)&DDB_REGS->tf_r11, FCN_NULL, },
79 1.1 matt { "r12", (long *)&DDB_REGS->tf_r12, FCN_NULL, },
80 1.1 matt { "usr_sp", (long *)&DDB_REGS->tf_usr_sp, FCN_NULL, },
81 1.1 matt { "usr_lr", (long *)&DDB_REGS->tf_usr_lr, FCN_NULL, },
82 1.1 matt { "svc_sp", (long *)&DDB_REGS->tf_svc_sp, FCN_NULL, },
83 1.1 matt { "svc_lr", (long *)&DDB_REGS->tf_svc_lr, FCN_NULL, },
84 1.1 matt { "pc", (long *)&DDB_REGS->tf_pc, FCN_NULL, },
85 1.1 matt { "und_sp", (long *)&nil, db_access_und_sp, },
86 1.1 matt { "abt_sp", (long *)&nil, db_access_abt_sp, },
87 1.1 matt { "irq_sp", (long *)&nil, db_access_irq_sp, },
88 1.1 matt };
89 1.1 matt
90 1.1 matt const struct db_variable * const db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
91 1.1 matt
92 1.1 matt extern label_t *db_recover;
93 1.1 matt
94 1.1 matt int db_active = 0;
95 1.1 matt
96 1.1 matt int db_access_und_sp(vp, valp, rw)
97 1.1 matt const struct db_variable *vp;
98 1.1 matt db_expr_t *valp;
99 1.1 matt int rw;
100 1.1 matt {
101 1.1 matt if (rw == DB_VAR_GET)
102 1.1 matt *valp = get_stackptr(PSR_UND32_MODE);
103 1.1 matt return(0);
104 1.1 matt }
105 1.1 matt
106 1.1 matt int db_access_abt_sp(vp, valp, rw)
107 1.1 matt const struct db_variable *vp;
108 1.1 matt db_expr_t *valp;
109 1.1 matt int rw;
110 1.1 matt {
111 1.1 matt if (rw == DB_VAR_GET)
112 1.1 matt *valp = get_stackptr(PSR_ABT32_MODE);
113 1.1 matt return(0);
114 1.1 matt }
115 1.1 matt
116 1.1 matt int db_access_irq_sp(vp, valp, rw)
117 1.1 matt const struct db_variable *vp;
118 1.1 matt db_expr_t *valp;
119 1.1 matt int rw;
120 1.1 matt {
121 1.1 matt if (rw == DB_VAR_GET)
122 1.1 matt *valp = get_stackptr(PSR_IRQ32_MODE);
123 1.1 matt return(0);
124 1.1 matt }
125 1.1 matt
126 1.1 matt /*
127 1.1 matt * kdb_trap - field a TRACE or BPT trap
128 1.1 matt */
129 1.1 matt int
130 1.1 matt kdb_trap(type, regs)
131 1.1 matt int type;
132 1.1 matt db_regs_t *regs;
133 1.1 matt {
134 1.1 matt int s;
135 1.1 matt
136 1.1 matt switch (type) {
137 1.1 matt case T_BREAKPOINT: /* breakpoint */
138 1.1 matt case -1: /* keyboard interrupt */
139 1.1 matt break;
140 1.1 matt default:
141 1.1 matt db_printf("kernel: trap");
142 1.1 matt if (db_recover != 0) {
143 1.1 matt db_error("Faulted in DDB; continuing...\n");
144 1.1 matt /*NOTREACHED*/
145 1.1 matt }
146 1.1 matt }
147 1.1 matt
148 1.1 matt /* Should switch to kdb`s own stack here. */
149 1.1 matt
150 1.1 matt ddb_regs = *regs;
151 1.1 matt
152 1.1 matt s = splhigh();
153 1.1 matt db_active++;
154 1.1 matt cnpollc(TRUE);
155 1.1 matt db_trap(type, 0/*code*/);
156 1.1 matt cnpollc(FALSE);
157 1.1 matt db_active--;
158 1.1 matt splx(s);
159 1.1 matt
160 1.1 matt *regs = ddb_regs;
161 1.1 matt
162 1.1 matt return (1);
163 1.1 matt }
164 1.1 matt
165 1.1 matt
166 1.1 matt /*
167 1.1 matt * Received keyboard interrupt sequence.
168 1.1 matt */
169 1.1 matt void
170 1.1 matt kdb_kbd_trap(regs)
171 1.1 matt db_regs_t *regs;
172 1.1 matt {
173 1.1 matt if (db_active == 0 && (boothowto & RB_KDB)) {
174 1.1 matt printf("\n\nkernel: keyboard interrupt\n");
175 1.1 matt kdb_trap(-1, regs);
176 1.1 matt }
177 1.1 matt }
178 1.1 matt
179 1.1 matt
180 1.1 matt static int
181 1.1 matt db_validate_address(addr)
182 1.1 matt vm_offset_t addr;
183 1.1 matt {
184 1.1 matt pt_entry_t *ptep;
185 1.1 matt pd_entry_t *pdep;
186 1.1 matt struct proc *p = curproc;
187 1.1 matt
188 1.1 matt /*
189 1.1 matt * If we have a valid pmap for curproc, use it's page directory
190 1.1 matt * otherwise use the kernel pmap's page directory.
191 1.1 matt */
192 1.1 matt if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap)
193 1.7 chris pdep = pmap_kernel()->pm_pdir;
194 1.1 matt else
195 1.1 matt pdep = p->p_vmspace->vm_map.pmap->pm_pdir;
196 1.1 matt
197 1.1 matt /* Make sure the address we are reading is valid */
198 1.1 matt switch ((pdep[(addr >> 20) + 0] & L1_MASK)) {
199 1.1 matt case L1_SECTION:
200 1.1 matt break;
201 1.1 matt case L1_PAGE:
202 1.1 matt /* Check the L2 page table for validity */
203 1.1 matt ptep = vtopte(addr);
204 1.1 matt if ((*ptep & L2_MASK) != L2_INVAL)
205 1.1 matt break;
206 1.1 matt /* FALLTHROUGH */
207 1.1 matt default:
208 1.1 matt return 1;
209 1.1 matt }
210 1.1 matt
211 1.1 matt return 0;
212 1.1 matt }
213 1.1 matt
214 1.1 matt /*
215 1.1 matt * Read bytes from kernel address space for debugger.
216 1.1 matt */
217 1.1 matt void
218 1.1 matt db_read_bytes(addr, size, data)
219 1.1 matt vm_offset_t addr;
220 1.1 matt int size;
221 1.1 matt char *data;
222 1.1 matt {
223 1.1 matt char *src;
224 1.1 matt
225 1.1 matt src = (char *)addr;
226 1.1 matt while (--size >= 0) {
227 1.1 matt if (db_validate_address((u_int)src)) {
228 1.1 matt db_printf("address %p is invalid\n", src);
229 1.1 matt return;
230 1.1 matt }
231 1.1 matt *data++ = *src++;
232 1.1 matt }
233 1.1 matt }
234 1.1 matt
235 1.1 matt static void
236 1.1 matt db_write_text(dst, ch)
237 1.1 matt unsigned char *dst;
238 1.1 matt int ch;
239 1.1 matt {
240 1.1 matt pt_entry_t *ptep, pteo;
241 1.1 matt vm_offset_t va;
242 1.1 matt
243 1.1 matt va = (unsigned long)dst & (~PGOFSET);
244 1.1 matt ptep = vtopte(va);
245 1.1 matt
246 1.1 matt if (db_validate_address((u_int)dst)) {
247 1.1 matt db_printf(" address %p not a valid page\n", dst);
248 1.1 matt return;
249 1.1 matt }
250 1.1 matt
251 1.1 matt pteo = *ptep;
252 1.1 matt *ptep = pteo | PT_AP(AP_KRW);
253 1.1 matt cpu_tlb_flushD_SE(va);
254 1.1 matt
255 1.1 matt *dst = (unsigned char)ch;
256 1.1 matt
257 1.1 matt /* make sure the caches and memory are in sync */
258 1.1 matt cpu_cache_syncI_rng((u_int)dst, 4);
259 1.1 matt
260 1.1 matt *ptep = pteo;
261 1.1 matt cpu_tlb_flushD_SE(va);
262 1.1 matt }
263 1.1 matt
264 1.1 matt /*
265 1.1 matt * Write bytes to kernel address space for debugger.
266 1.1 matt */
267 1.1 matt void
268 1.1 matt db_write_bytes(addr, size, data)
269 1.1 matt vm_offset_t addr;
270 1.1 matt int size;
271 1.1 matt char *data;
272 1.1 matt {
273 1.1 matt extern char etext[];
274 1.1 matt char *dst;
275 1.1 matt int loop;
276 1.1 matt
277 1.1 matt dst = (char *)addr;
278 1.1 matt loop = size;
279 1.1 matt while (--loop >= 0) {
280 1.1 matt if ((dst >= (char *)KERNEL_TEXT_BASE) && (dst < etext))
281 1.1 matt db_write_text(dst, *data);
282 1.1 matt else {
283 1.1 matt if (db_validate_address((u_int)dst)) {
284 1.1 matt db_printf("address %p is invalid\n", dst);
285 1.1 matt return;
286 1.1 matt }
287 1.1 matt *dst = *data;
288 1.1 matt }
289 1.1 matt dst++, data++;
290 1.1 matt }
291 1.1 matt /* make sure the caches and memory are in sync */
292 1.1 matt cpu_cache_syncI_rng(addr, size);
293 1.1 matt
294 1.1 matt /* In case the current page tables have been modified ... */
295 1.1 matt cpu_tlb_flushID();
296 1.1 matt }
297 1.1 matt
298 1.1 matt void
299 1.1 matt cpu_Debugger()
300 1.1 matt {
301 1.1 matt asm(".word 0xe7ffffff");
302 1.1 matt }
303 1.1 matt
304 1.1 matt void db_show_intrchain_cmd __P((db_expr_t addr, int have_addr, db_expr_t count, char *modif));
305 1.1 matt void db_show_panic_cmd __P((db_expr_t addr, int have_addr, db_expr_t count, char *modif));
306 1.1 matt void db_show_frame_cmd __P((db_expr_t addr, int have_addr, db_expr_t count, char *modif));
307 1.1 matt
308 1.1 matt const struct db_command db_machine_command_table[] = {
309 1.1 matt { "frame", db_show_frame_cmd, 0, NULL },
310 1.1 matt { "intrchain", db_show_intrchain_cmd, 0, NULL },
311 1.1 matt { "panic", db_show_panic_cmd, 0, NULL },
312 1.2 matt #ifdef ARM32_DB_COMMANDS
313 1.2 matt ARM32_DB_COMMANDS,
314 1.2 matt #endif
315 1.1 matt { NULL, NULL, 0, NULL }
316 1.1 matt };
317 1.1 matt
318 1.1 matt int
319 1.1 matt db_trapper(addr, inst, frame, fault_code)
320 1.1 matt u_int addr;
321 1.1 matt u_int inst;
322 1.1 matt trapframe_t *frame;
323 1.1 matt int fault_code;
324 1.1 matt {
325 1.1 matt if (fault_code == 0) {
326 1.1 matt if ((inst & ~INSN_COND_MASK) == (BKPT_INST & ~INSN_COND_MASK))
327 1.1 matt kdb_trap(T_BREAKPOINT, frame);
328 1.1 matt else
329 1.1 matt kdb_trap(-1, frame);
330 1.1 matt } else
331 1.1 matt return (1);
332 1.1 matt return (0);
333 1.1 matt }
334 1.1 matt
335 1.1 matt extern u_int esym;
336 1.1 matt extern u_int end;
337 1.1 matt
338 1.3 bjh21 static struct undefined_handler db_uh;
339 1.3 bjh21
340 1.1 matt void
341 1.1 matt db_machine_init()
342 1.1 matt {
343 1.5 bjh21 #ifndef __ELF__
344 1.1 matt struct exec *kernexec = (struct exec *)KERNEL_TEXT_BASE;
345 1.1 matt int len;
346 1.1 matt
347 1.1 matt /*
348 1.1 matt * The boot loader currently loads the kernel with the a.out
349 1.1 matt * header still attached.
350 1.1 matt */
351 1.1 matt
352 1.1 matt if (kernexec->a_syms == 0) {
353 1.8 thorpej printf("ddb: No symbol table\n");
354 1.1 matt } else {
355 1.2 matt /* cover the symbols themselves (what is the int for?? XXX) */
356 1.1 matt esym = (int)&end + kernexec->a_syms + sizeof(int);
357 1.2 matt
358 1.1 matt /*
359 1.1 matt * and the string table. (int containing size of string
360 1.1 matt * table is included in string table size).
361 1.1 matt */
362 1.1 matt len = *((u_int *)esym);
363 1.1 matt esym += (len + (sizeof(u_int) - 1)) & ~(sizeof(u_int) - 1);
364 1.1 matt }
365 1.5 bjh21 #endif
366 1.1 matt
367 1.3 bjh21 /*
368 1.3 bjh21 * We get called before malloc() is available, so supply a static
369 1.3 bjh21 * struct undefined_handler.
370 1.3 bjh21 */
371 1.3 bjh21 db_uh.uh_handler = db_trapper;
372 1.3 bjh21 install_coproc_handler_static(0, &db_uh);
373 1.1 matt }
374 1.1 matt
375 1.1 matt u_int
376 1.1 matt db_fetch_reg(reg, db_regs)
377 1.1 matt int reg;
378 1.1 matt db_regs_t *db_regs;
379 1.1 matt {
380 1.1 matt
381 1.1 matt switch (reg) {
382 1.1 matt case 0:
383 1.1 matt return (db_regs->tf_r0);
384 1.1 matt case 1:
385 1.1 matt return (db_regs->tf_r1);
386 1.1 matt case 2:
387 1.1 matt return (db_regs->tf_r2);
388 1.1 matt case 3:
389 1.1 matt return (db_regs->tf_r3);
390 1.1 matt case 4:
391 1.1 matt return (db_regs->tf_r4);
392 1.1 matt case 5:
393 1.1 matt return (db_regs->tf_r5);
394 1.1 matt case 6:
395 1.1 matt return (db_regs->tf_r6);
396 1.1 matt case 7:
397 1.1 matt return (db_regs->tf_r7);
398 1.1 matt case 8:
399 1.1 matt return (db_regs->tf_r8);
400 1.1 matt case 9:
401 1.1 matt return (db_regs->tf_r9);
402 1.1 matt case 10:
403 1.1 matt return (db_regs->tf_r10);
404 1.1 matt case 11:
405 1.1 matt return (db_regs->tf_r11);
406 1.1 matt case 12:
407 1.1 matt return (db_regs->tf_r12);
408 1.1 matt case 13:
409 1.1 matt return (db_regs->tf_svc_sp);
410 1.1 matt case 14:
411 1.1 matt return (db_regs->tf_svc_lr);
412 1.1 matt case 15:
413 1.1 matt return (db_regs->tf_pc);
414 1.1 matt default:
415 1.1 matt panic("db_fetch_reg: botch");
416 1.1 matt }
417 1.1 matt }
418 1.1 matt
419 1.1 matt u_int
420 1.1 matt branch_taken(insn, pc, db_regs)
421 1.1 matt u_int insn;
422 1.1 matt u_int pc;
423 1.1 matt db_regs_t *db_regs;
424 1.1 matt {
425 1.1 matt u_int addr, nregs;
426 1.1 matt
427 1.1 matt switch ((insn >> 24) & 0xf) {
428 1.1 matt case 0xa: /* b ... */
429 1.1 matt case 0xb: /* bl ... */
430 1.1 matt addr = ((insn << 2) & 0x03ffffff);
431 1.1 matt if (addr & 0x02000000)
432 1.1 matt addr |= 0xfc000000;
433 1.1 matt return (pc + 8 + addr);
434 1.1 matt case 0x7: /* ldr pc, [pc, reg, lsl #2] */
435 1.1 matt addr = db_fetch_reg(insn & 0xf, db_regs);
436 1.1 matt addr = pc + 8 + (addr << 2);
437 1.1 matt db_read_bytes(addr, 4, (char *)&addr);
438 1.1 matt return (addr);
439 1.1 matt case 0x1: /* mov pc, reg */
440 1.1 matt addr = db_fetch_reg(insn & 0xf, db_regs);
441 1.1 matt return (addr);
442 1.1 matt case 0x8: /* ldmxx reg, {..., pc} */
443 1.1 matt case 0x9:
444 1.1 matt addr = db_fetch_reg((insn >> 16) & 0xf, db_regs);
445 1.1 matt nregs = (insn & 0x5555) + ((insn >> 1) & 0x5555);
446 1.1 matt nregs = (nregs & 0x3333) + ((nregs >> 2) & 0x3333);
447 1.1 matt nregs = (nregs + (nregs >> 4)) & 0x0f0f;
448 1.1 matt nregs = (nregs + (nregs >> 8)) & 0x001f;
449 1.1 matt switch ((insn >> 23) & 0x3) {
450 1.1 matt case 0x0: /* ldmda */
451 1.1 matt addr = addr - 0;
452 1.1 matt break;
453 1.1 matt case 0x1: /* ldmia */
454 1.1 matt addr = addr + 0 + ((nregs - 1) << 2);
455 1.1 matt break;
456 1.1 matt case 0x2: /* ldmdb */
457 1.1 matt addr = addr - 4;
458 1.1 matt break;
459 1.1 matt case 0x3: /* ldmib */
460 1.1 matt addr = addr + 4 + ((nregs - 1) << 2);
461 1.1 matt break;
462 1.1 matt }
463 1.1 matt db_read_bytes(addr, 4, (char *)&addr);
464 1.1 matt return (addr);
465 1.1 matt default:
466 1.1 matt panic("branch_taken: botch");
467 1.1 matt }
468 1.1 matt }
469