db_interface.c revision 1.51 1 1.51 matt /* $NetBSD: db_interface.c,v 1.51 2014/03/28 21:54:12 matt Exp $ */
2 1.1 matt
3 1.50 skrll /*
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.50 skrll *
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.50 skrll *
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.50 skrll *
20 1.1 matt * Carnegie Mellon requests users of this software to return to
21 1.50 skrll *
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.50 skrll *
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.32 lukem
37 1.32 lukem #include <sys/cdefs.h>
38 1.51 matt __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.51 2014/03/28 21:54:12 matt Exp $");
39 1.32 lukem
40 1.1 matt #include "opt_ddb.h"
41 1.24 briggs #include "opt_kgdb.h"
42 1.1 matt
43 1.1 matt #include <sys/param.h>
44 1.1 matt #include <sys/proc.h>
45 1.1 matt #include <sys/reboot.h>
46 1.1 matt #include <sys/systm.h> /* just for boothowto */
47 1.1 matt #include <sys/exec.h>
48 1.51 matt #include <sys/atomic.h>
49 1.51 matt #include <sys/intr.h>
50 1.1 matt
51 1.1 matt #include <uvm/uvm_extern.h>
52 1.1 matt
53 1.13 chris #include <arm/arm32/db_machdep.h>
54 1.9 thorpej #include <arm/arm32/katelib.h>
55 1.11 thorpej #include <arm/undefined.h>
56 1.13 chris #include <ddb/db_access.h>
57 1.1 matt #include <ddb/db_command.h>
58 1.1 matt #include <ddb/db_output.h>
59 1.1 matt #include <ddb/db_variables.h>
60 1.1 matt #include <ddb/db_sym.h>
61 1.1 matt #include <ddb/db_extern.h>
62 1.1 matt #include <ddb/db_interface.h>
63 1.1 matt #include <dev/cons.h>
64 1.1 matt
65 1.24 briggs #if defined(KGDB) || !defined(DDB)
66 1.24 briggs #define db_printf printf
67 1.24 briggs #endif
68 1.24 briggs
69 1.46 dsl u_int db_fetch_reg(int, db_regs_t *);
70 1.1 matt
71 1.46 dsl int db_trapper(u_int, u_int, trapframe_t *, int);
72 1.13 chris
73 1.1 matt int db_active = 0;
74 1.51 matt #ifdef MULTIPROCESSOR
75 1.51 matt volatile struct cpu_info *db_onproc;
76 1.51 matt volatile struct cpu_info *db_newcpu;
77 1.51 matt #endif
78 1.51 matt
79 1.51 matt
80 1.1 matt
81 1.1 matt
82 1.24 briggs #ifdef DDB
83 1.1 matt /*
84 1.1 matt * kdb_trap - field a TRACE or BPT trap
85 1.1 matt */
86 1.1 matt int
87 1.15 thorpej kdb_trap(int type, db_regs_t *regs)
88 1.1 matt {
89 1.51 matt struct cpu_info * const ci = curcpu();
90 1.1 matt int s;
91 1.1 matt
92 1.1 matt switch (type) {
93 1.1 matt case T_BREAKPOINT: /* breakpoint */
94 1.1 matt case -1: /* keyboard interrupt */
95 1.1 matt break;
96 1.51 matt #ifdef MULTIPROCESSOR
97 1.51 matt case -2:
98 1.51 matt /*
99 1.51 matt * We called to enter ddb from another process but by the time
100 1.51 matt * we got here, no one was in ddb. So ignore the request.
101 1.51 matt */
102 1.51 matt if (db_onproc == NULL)
103 1.51 matt return 1;
104 1.51 matt break;
105 1.51 matt #endif
106 1.1 matt default:
107 1.1 matt if (db_recover != 0) {
108 1.31 thorpej /* This will longjmp back into db_command_loop() */
109 1.1 matt db_error("Faulted in DDB; continuing...\n");
110 1.1 matt /*NOTREACHED*/
111 1.1 matt }
112 1.1 matt }
113 1.1 matt
114 1.1 matt /* Should switch to kdb`s own stack here. */
115 1.1 matt
116 1.51 matt #ifdef MULTIPROCESSOR
117 1.51 matt const bool is_mp_p = ncpu > 1;
118 1.51 matt if (is_mp_p) {
119 1.51 matt /*
120 1.51 matt * Try to take ownership of DDB. If we do, tell all other
121 1.51 matt * CPUs to enter DDB too.
122 1.51 matt */
123 1.51 matt if (atomic_cas_ptr(&db_onproc, NULL, ci) == NULL) {
124 1.51 matt intr_ipi_send(NULL, IPI_DDB);
125 1.51 matt }
126 1.51 matt }
127 1.51 matt for (;;) {
128 1.51 matt if (is_mp_p) {
129 1.51 matt /*
130 1.51 matt * While we aren't the master, wait until the master
131 1.51 matt * gives control to us or exits. If it exited, we
132 1.51 matt * just exit to. Otherwise this cpu will enter DDB.
133 1.51 matt */
134 1.51 matt membar_consumer();
135 1.51 matt while (db_onproc != ci) {
136 1.51 matt if (db_onproc == NULL)
137 1.51 matt return 1;
138 1.51 matt #ifdef _ARM_ARCH_6
139 1.51 matt __asm __volatile("wfe");
140 1.51 matt membar_consumer();
141 1.51 matt #endif
142 1.51 matt if (db_onproc == ci) {
143 1.51 matt printf("%s: switching to %s\n",
144 1.51 matt __func__, ci->ci_cpuname);
145 1.51 matt }
146 1.51 matt }
147 1.51 matt }
148 1.51 matt #endif
149 1.1 matt
150 1.51 matt s = splhigh();
151 1.51 matt ci->ci_ddb_regs = regs;
152 1.51 matt atomic_inc_32(&db_active);
153 1.51 matt cnpollc(true);
154 1.51 matt db_trap(type, 0/*code*/);
155 1.51 matt cnpollc(false);
156 1.51 matt atomic_dec_32(&db_active);
157 1.51 matt ci->ci_ddb_regs = NULL;
158 1.51 matt splx(s);
159 1.51 matt
160 1.51 matt #ifdef MULTIPROCESSOR
161 1.51 matt if (is_mp_p && db_newcpu != NULL) {
162 1.51 matt db_onproc = db_newcpu;
163 1.51 matt db_newcpu = NULL;
164 1.51 matt #ifdef _ARM_ARCH_6
165 1.51 matt membar_producer();
166 1.51 matt __asm __volatile("sev; sev");
167 1.51 matt #endif
168 1.51 matt continue;
169 1.51 matt }
170 1.51 matt break;
171 1.51 matt }
172 1.1 matt
173 1.51 matt if (is_mp_p) {
174 1.51 matt /*
175 1.51 matt * We are exiting DDB so there is noone onproc. Tell
176 1.51 matt * the other CPUs to exit.
177 1.51 matt */
178 1.51 matt db_onproc = NULL;
179 1.51 matt #ifdef _ARM_ARCH_6
180 1.51 matt __asm __volatile("sev; sev");
181 1.51 matt #endif
182 1.51 matt }
183 1.51 matt #endif
184 1.1 matt
185 1.1 matt return (1);
186 1.1 matt }
187 1.24 briggs #endif
188 1.1 matt
189 1.24 briggs int
190 1.15 thorpej db_validate_address(vaddr_t addr)
191 1.1 matt {
192 1.1 matt struct proc *p = curproc;
193 1.14 thorpej struct pmap *pmap;
194 1.1 matt
195 1.23 scw if (!p || !p->p_vmspace || !p->p_vmspace->vm_map.pmap ||
196 1.23 scw #ifndef ARM32_NEW_VM_LAYOUT
197 1.29 thorpej addr >= VM_MAXUSER_ADDRESS
198 1.23 scw #else
199 1.29 thorpej addr >= VM_MIN_KERNEL_ADDRESS
200 1.23 scw #endif
201 1.29 thorpej )
202 1.14 thorpej pmap = pmap_kernel();
203 1.1 matt else
204 1.14 thorpej pmap = p->p_vmspace->vm_map.pmap;
205 1.1 matt
206 1.42 thorpej return (pmap_extract(pmap, addr, NULL) == false);
207 1.1 matt }
208 1.1 matt
209 1.1 matt /*
210 1.1 matt * Read bytes from kernel address space for debugger.
211 1.1 matt */
212 1.1 matt void
213 1.47 dsl db_read_bytes(vaddr_t addr, size_t size, char *data)
214 1.1 matt {
215 1.30 scw char *src = (char *)addr;
216 1.1 matt
217 1.30 scw if (db_validate_address((u_int)src)) {
218 1.30 scw db_printf("address %p is invalid\n", src);
219 1.30 scw return;
220 1.30 scw }
221 1.30 scw
222 1.30 scw if (size == 4 && (addr & 3) == 0 && ((uintptr_t)data & 3) == 0) {
223 1.30 scw *((int*)data) = *((int*)src);
224 1.30 scw return;
225 1.30 scw }
226 1.30 scw
227 1.30 scw if (size == 2 && (addr & 1) == 0 && ((uintptr_t)data & 1) == 0) {
228 1.30 scw *((short*)data) = *((short*)src);
229 1.30 scw return;
230 1.30 scw }
231 1.14 thorpej
232 1.14 thorpej while (size-- > 0) {
233 1.1 matt if (db_validate_address((u_int)src)) {
234 1.1 matt db_printf("address %p is invalid\n", src);
235 1.1 matt return;
236 1.1 matt }
237 1.1 matt *data++ = *src++;
238 1.1 matt }
239 1.1 matt }
240 1.1 matt
241 1.1 matt static void
242 1.37 uwe db_write_text(vaddr_t addr, size_t size, const char *data)
243 1.50 skrll {
244 1.15 thorpej struct pmap *pmap = pmap_kernel();
245 1.15 thorpej pd_entry_t *pde, oldpde, tmppde;
246 1.15 thorpej pt_entry_t *pte, oldpte, tmppte;
247 1.15 thorpej vaddr_t pgva;
248 1.15 thorpej size_t limit, savesize;
249 1.15 thorpej char *dst;
250 1.34 chris
251 1.34 chris /* XXX: gcc */
252 1.34 chris oldpte = 0;
253 1.1 matt
254 1.15 thorpej if ((savesize = size) == 0)
255 1.15 thorpej return;
256 1.15 thorpej
257 1.15 thorpej dst = (char *) addr;
258 1.1 matt
259 1.15 thorpej do {
260 1.15 thorpej /* Get the PDE of the current VA. */
261 1.42 thorpej if (pmap_get_pde_pte(pmap, (vaddr_t) dst, &pde, &pte) == false)
262 1.23 scw goto no_mapping;
263 1.18 thorpej switch ((oldpde = *pde) & L1_TYPE_MASK) {
264 1.18 thorpej case L1_TYPE_S:
265 1.18 thorpej pgva = (vaddr_t)dst & L1_S_FRAME;
266 1.18 thorpej limit = L1_S_SIZE - ((vaddr_t)dst & L1_S_OFFSET);
267 1.15 thorpej
268 1.48 jmcneill tmppde = l1pte_set_writable(oldpde);
269 1.15 thorpej *pde = tmppde;
270 1.22 thorpej PTE_SYNC(pde);
271 1.15 thorpej break;
272 1.15 thorpej
273 1.18 thorpej case L1_TYPE_C:
274 1.18 thorpej pgva = (vaddr_t)dst & L2_S_FRAME;
275 1.18 thorpej limit = L2_S_SIZE - ((vaddr_t)dst & L2_S_OFFSET);
276 1.15 thorpej
277 1.23 scw if (pte == NULL)
278 1.23 scw goto no_mapping;
279 1.15 thorpej oldpte = *pte;
280 1.48 jmcneill tmppte = l2pte_set_writable(oldpte);
281 1.15 thorpej *pte = tmppte;
282 1.22 thorpej PTE_SYNC(pte);
283 1.15 thorpej break;
284 1.15 thorpej
285 1.15 thorpej default:
286 1.23 scw no_mapping:
287 1.15 thorpej printf(" address 0x%08lx not a valid page\n",
288 1.15 thorpej (vaddr_t) dst);
289 1.15 thorpej return;
290 1.15 thorpej }
291 1.15 thorpej cpu_tlb_flushD_SE(pgva);
292 1.16 thorpej cpu_cpwait();
293 1.1 matt
294 1.15 thorpej if (limit > size)
295 1.15 thorpej limit = size;
296 1.15 thorpej size -= limit;
297 1.1 matt
298 1.15 thorpej /*
299 1.15 thorpej * Page is now writable. Do as much access as we
300 1.15 thorpej * can in this page.
301 1.15 thorpej */
302 1.15 thorpej for (; limit > 0; limit--)
303 1.15 thorpej *dst++ = *data++;
304 1.1 matt
305 1.15 thorpej /*
306 1.15 thorpej * Restore old mapping permissions.
307 1.15 thorpej */
308 1.18 thorpej switch (oldpde & L1_TYPE_MASK) {
309 1.18 thorpej case L1_TYPE_S:
310 1.15 thorpej *pde = oldpde;
311 1.22 thorpej PTE_SYNC(pde);
312 1.15 thorpej break;
313 1.15 thorpej
314 1.18 thorpej case L1_TYPE_C:
315 1.15 thorpej *pte = oldpte;
316 1.22 thorpej PTE_SYNC(pte);
317 1.15 thorpej break;
318 1.15 thorpej }
319 1.15 thorpej cpu_tlb_flushD_SE(pgva);
320 1.16 thorpej cpu_cpwait();
321 1.15 thorpej } while (size != 0);
322 1.1 matt
323 1.15 thorpej /* Sync the I-cache. */
324 1.17 thorpej cpu_icache_sync_range(addr, savesize);
325 1.1 matt }
326 1.1 matt
327 1.1 matt /*
328 1.1 matt * Write bytes to kernel address space for debugger.
329 1.1 matt */
330 1.1 matt void
331 1.37 uwe db_write_bytes(vaddr_t addr, size_t size, const char *data)
332 1.1 matt {
333 1.27 thorpej extern char kernel_text[];
334 1.15 thorpej extern char etext[];
335 1.15 thorpej char *dst;
336 1.15 thorpej size_t loop;
337 1.15 thorpej
338 1.15 thorpej /* If any part is in kernel text, use db_write_text() */
339 1.27 thorpej if (addr >= (vaddr_t) kernel_text && addr < (vaddr_t) etext) {
340 1.15 thorpej db_write_text(addr, size, data);
341 1.15 thorpej return;
342 1.15 thorpej }
343 1.1 matt
344 1.1 matt dst = (char *)addr;
345 1.30 scw if (db_validate_address((u_int)dst)) {
346 1.30 scw db_printf("address %p is invalid\n", dst);
347 1.30 scw return;
348 1.30 scw }
349 1.30 scw
350 1.30 scw if (size == 4 && (addr & 3) == 0 && ((uintptr_t)data & 3) == 0)
351 1.37 uwe *((int*)dst) = *((const int *)data);
352 1.30 scw else
353 1.30 scw if (size == 2 && (addr & 1) == 0 && ((uintptr_t)data & 1) == 0)
354 1.37 uwe *((short*)dst) = *((const short *)data);
355 1.30 scw else {
356 1.30 scw loop = size;
357 1.30 scw while (loop-- > 0) {
358 1.30 scw if (db_validate_address((u_int)dst)) {
359 1.30 scw db_printf("address %p is invalid\n", dst);
360 1.30 scw return;
361 1.30 scw }
362 1.30 scw *dst++ = *data++;
363 1.1 matt }
364 1.1 matt }
365 1.30 scw
366 1.1 matt /* make sure the caches and memory are in sync */
367 1.17 thorpej cpu_icache_sync_range(addr, size);
368 1.1 matt
369 1.1 matt /* In case the current page tables have been modified ... */
370 1.1 matt cpu_tlb_flushID();
371 1.16 thorpej cpu_cpwait();
372 1.1 matt }
373 1.1 matt
374 1.28 bsh #ifdef DDB
375 1.1 matt void
376 1.15 thorpej cpu_Debugger(void)
377 1.1 matt {
378 1.39 perry __asm(".word 0xe7ffffff");
379 1.1 matt }
380 1.1 matt
381 1.1 matt int
382 1.15 thorpej db_trapper(u_int addr, u_int inst, trapframe_t *frame, int fault_code)
383 1.1 matt {
384 1.15 thorpej
385 1.1 matt if (fault_code == 0) {
386 1.1 matt if ((inst & ~INSN_COND_MASK) == (BKPT_INST & ~INSN_COND_MASK))
387 1.1 matt kdb_trap(T_BREAKPOINT, frame);
388 1.1 matt else
389 1.1 matt kdb_trap(-1, frame);
390 1.1 matt } else
391 1.1 matt return (1);
392 1.1 matt return (0);
393 1.1 matt }
394 1.1 matt
395 1.1 matt extern u_int esym;
396 1.1 matt extern u_int end;
397 1.1 matt
398 1.3 bjh21 static struct undefined_handler db_uh;
399 1.3 bjh21
400 1.1 matt void
401 1.15 thorpej db_machine_init(void)
402 1.1 matt {
403 1.1 matt
404 1.3 bjh21 /*
405 1.3 bjh21 * We get called before malloc() is available, so supply a static
406 1.3 bjh21 * struct undefined_handler.
407 1.3 bjh21 */
408 1.3 bjh21 db_uh.uh_handler = db_trapper;
409 1.35 rearnsha install_coproc_handler_static(CORE_UNKNOWN_HANDLER, &db_uh);
410 1.1 matt }
411 1.24 briggs #endif
412 1.1 matt
413 1.1 matt u_int
414 1.36 he db_fetch_reg(int reg, db_regs_t *regs)
415 1.1 matt {
416 1.1 matt
417 1.1 matt switch (reg) {
418 1.1 matt case 0:
419 1.36 he return (regs->tf_r0);
420 1.1 matt case 1:
421 1.36 he return (regs->tf_r1);
422 1.1 matt case 2:
423 1.36 he return (regs->tf_r2);
424 1.1 matt case 3:
425 1.36 he return (regs->tf_r3);
426 1.1 matt case 4:
427 1.36 he return (regs->tf_r4);
428 1.1 matt case 5:
429 1.36 he return (regs->tf_r5);
430 1.1 matt case 6:
431 1.36 he return (regs->tf_r6);
432 1.1 matt case 7:
433 1.36 he return (regs->tf_r7);
434 1.1 matt case 8:
435 1.36 he return (regs->tf_r8);
436 1.1 matt case 9:
437 1.36 he return (regs->tf_r9);
438 1.1 matt case 10:
439 1.36 he return (regs->tf_r10);
440 1.1 matt case 11:
441 1.36 he return (regs->tf_r11);
442 1.1 matt case 12:
443 1.36 he return (regs->tf_r12);
444 1.1 matt case 13:
445 1.36 he return (regs->tf_svc_sp);
446 1.1 matt case 14:
447 1.36 he return (regs->tf_svc_lr);
448 1.1 matt case 15:
449 1.36 he return (regs->tf_pc);
450 1.1 matt default:
451 1.1 matt panic("db_fetch_reg: botch");
452 1.1 matt }
453 1.1 matt }
454 1.1 matt
455 1.1 matt u_int
456 1.36 he branch_taken(u_int insn, u_int pc, db_regs_t *regs)
457 1.1 matt {
458 1.1 matt u_int addr, nregs;
459 1.1 matt
460 1.1 matt switch ((insn >> 24) & 0xf) {
461 1.1 matt case 0xa: /* b ... */
462 1.1 matt case 0xb: /* bl ... */
463 1.1 matt addr = ((insn << 2) & 0x03ffffff);
464 1.1 matt if (addr & 0x02000000)
465 1.1 matt addr |= 0xfc000000;
466 1.1 matt return (pc + 8 + addr);
467 1.1 matt case 0x7: /* ldr pc, [pc, reg, lsl #2] */
468 1.36 he addr = db_fetch_reg(insn & 0xf, regs);
469 1.1 matt addr = pc + 8 + (addr << 2);
470 1.1 matt db_read_bytes(addr, 4, (char *)&addr);
471 1.1 matt return (addr);
472 1.41 christos case 0x5: /* ldr pc, [reg] */
473 1.41 christos addr = db_fetch_reg((insn >> 16) & 0xf, regs);
474 1.41 christos db_read_bytes(addr, 4, (char *)&addr);
475 1.41 christos return (addr);
476 1.1 matt case 0x1: /* mov pc, reg */
477 1.36 he addr = db_fetch_reg(insn & 0xf, regs);
478 1.1 matt return (addr);
479 1.1 matt case 0x8: /* ldmxx reg, {..., pc} */
480 1.1 matt case 0x9:
481 1.36 he addr = db_fetch_reg((insn >> 16) & 0xf, regs);
482 1.1 matt nregs = (insn & 0x5555) + ((insn >> 1) & 0x5555);
483 1.1 matt nregs = (nregs & 0x3333) + ((nregs >> 2) & 0x3333);
484 1.1 matt nregs = (nregs + (nregs >> 4)) & 0x0f0f;
485 1.1 matt nregs = (nregs + (nregs >> 8)) & 0x001f;
486 1.1 matt switch ((insn >> 23) & 0x3) {
487 1.1 matt case 0x0: /* ldmda */
488 1.1 matt addr = addr - 0;
489 1.1 matt break;
490 1.1 matt case 0x1: /* ldmia */
491 1.1 matt addr = addr + 0 + ((nregs - 1) << 2);
492 1.1 matt break;
493 1.1 matt case 0x2: /* ldmdb */
494 1.1 matt addr = addr - 4;
495 1.1 matt break;
496 1.1 matt case 0x3: /* ldmib */
497 1.1 matt addr = addr + 4 + ((nregs - 1) << 2);
498 1.1 matt break;
499 1.1 matt }
500 1.1 matt db_read_bytes(addr, 4, (char *)&addr);
501 1.1 matt return (addr);
502 1.1 matt default:
503 1.1 matt panic("branch_taken: botch");
504 1.1 matt }
505 1.1 matt }
506