kgdb_machdep.c revision 1.1 1 /* $NetBSD: kgdb_machdep.c,v 1.1 2003/04/28 01:54:50 briggs Exp $ */
2
3 /*
4 * Copyright (c) 1996 Matthias Pfaller.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Matthias Pfaller.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "opt_ddb.h"
34 #include "opt_kgdb.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kgdb.h>
39
40 #include <uvm/uvm_extern.h>
41
42 #include <machine/frame.h>
43 #include <machine/reg.h>
44 #include <machine/trap.h>
45
46 /*
47 * Determine if the memory at va..(va+len) is valid.
48 */
49 int
50 kgdb_acc(va, len)
51 vaddr_t va;
52 size_t len;
53 {
54 vaddr_t last_va;
55
56 last_va = va + len;
57 va &= ~PGOFSET;
58 last_va &= ~PGOFSET;
59
60 do {
61 if (db_validate_address(va))
62 return (0);
63 va += PAGE_SIZE;
64 } while (va < last_va);
65
66 return (1);
67 }
68
69 /*
70 * Translate a trap number into a unix compatible signal value.
71 * (gdb only understands unix signal numbers).
72 */
73 int
74 kgdb_signal(type)
75 int type;
76 {
77
78 switch (type) {
79 case T_BREAKPOINT:
80 return(SIGTRAP);
81 case -1:
82 return(SIGSEGV);
83 default:
84 return(SIGINT);
85 }
86 }
87
88 /*
89 * Definitions exported from gdb.
90 */
91
92 /*
93 * Translate the values stored in the kernel regs struct to the format
94 * understood by gdb.
95 */
96 void
97 kgdb_getregs(regs, gdb_regs)
98 db_regs_t *regs;
99 kgdb_reg_t *gdb_regs;
100 {
101
102 gdb_regs[KGDB_REGNUM_R0 + 0] = regs->tf_r0;
103 gdb_regs[KGDB_REGNUM_R0 + 1] = regs->tf_r1;
104 gdb_regs[KGDB_REGNUM_R0 + 2] = regs->tf_r2;
105 gdb_regs[KGDB_REGNUM_R0 + 3] = regs->tf_r3;
106 gdb_regs[KGDB_REGNUM_R0 + 4] = regs->tf_r4;
107 gdb_regs[KGDB_REGNUM_R0 + 5] = regs->tf_r5;
108 gdb_regs[KGDB_REGNUM_R0 + 6] = regs->tf_r6;
109 gdb_regs[KGDB_REGNUM_R0 + 7] = regs->tf_r7;
110 gdb_regs[KGDB_REGNUM_R0 + 8] = regs->tf_r8;
111 gdb_regs[KGDB_REGNUM_R0 + 9] = regs->tf_r9;
112 gdb_regs[KGDB_REGNUM_R0 + 10] = regs->tf_r10;
113 gdb_regs[KGDB_REGNUM_R0 + 11] = regs->tf_r11;
114 gdb_regs[KGDB_REGNUM_R0 + 12] = regs->tf_r12;
115 gdb_regs[KGDB_REGNUM_R0 + 13] = regs->tf_svc_sp;
116 gdb_regs[KGDB_REGNUM_R0 + 14] = regs->tf_svc_lr;
117 gdb_regs[KGDB_REGNUM_R0 + 15] = regs->tf_pc;
118
119 gdb_regs[KGDB_REGNUM_SPSR] = regs->tf_spsr;
120 }
121
122 /*
123 * Reverse the above.
124 */
125 void
126 kgdb_setregs(regs, gdb_regs)
127 db_regs_t *regs;
128 kgdb_reg_t *gdb_regs;
129 {
130 regs->tf_r0 = gdb_regs[KGDB_REGNUM_R0 + 0];
131 regs->tf_r1 = gdb_regs[KGDB_REGNUM_R0 + 1];
132 regs->tf_r2 = gdb_regs[KGDB_REGNUM_R0 + 2];
133 regs->tf_r3 = gdb_regs[KGDB_REGNUM_R0 + 3];
134 regs->tf_r4 = gdb_regs[KGDB_REGNUM_R0 + 4];
135 regs->tf_r5 = gdb_regs[KGDB_REGNUM_R0 + 5];
136 regs->tf_r6 = gdb_regs[KGDB_REGNUM_R0 + 6];
137 regs->tf_r7 = gdb_regs[KGDB_REGNUM_R0 + 7];
138 regs->tf_r8 = gdb_regs[KGDB_REGNUM_R0 + 8];
139 regs->tf_r9 = gdb_regs[KGDB_REGNUM_R0 + 9];
140 regs->tf_r10 = gdb_regs[KGDB_REGNUM_R0 + 10];
141 regs->tf_r11 = gdb_regs[KGDB_REGNUM_R0 + 11];
142 regs->tf_r12 = gdb_regs[KGDB_REGNUM_R0 + 12];
143 regs->tf_svc_sp = gdb_regs[KGDB_REGNUM_R0 + 13];
144 regs->tf_svc_lr = gdb_regs[KGDB_REGNUM_R0 + 14];
145 regs->tf_pc = gdb_regs[KGDB_REGNUM_R0 + 15];
146
147 regs->tf_spsr = gdb_regs[KGDB_REGNUM_SPSR];
148 }
149
150 /*
151 * Trap into kgdb to wait for debugger to connect,
152 * noting on the console why nothing else is going on.
153 */
154 void
155 kgdb_connect(verbose)
156 int verbose;
157 {
158
159 if (kgdb_dev < 0)
160 return;
161
162 if (verbose)
163 printf("kgdb waiting...");
164
165 asm volatile(KBPT_ASM);
166
167 if (verbose)
168 printf("connected.\n");
169
170 kgdb_debug_panic = 1;
171 }
172
173 /*
174 * Decide what to do on panic.
175 * (This is called by panic, like Debugger())
176 */
177 void
178 kgdb_panic()
179 {
180
181 if (kgdb_dev >= 0 && kgdb_debug_panic) {
182 printf("entering kgdb\n");
183 kgdb_connect(kgdb_active == 0);
184 }
185 }
186