1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 *
26 * $FreeBSD: releng/5.0/sys/i386/i386/db_interface.c 103753 2002-09-21 18:53:58Z markm $
27 */
28
29 /*
30 * Interface to new debugger.
31 */
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/reboot.h>
35 #include <sys/cons.h>
36 #include <sys/pcpu.h>
37 #include <sys/proc.h>
38 #include <sys/smp.h>
39
40 #include <machine/cpu.h>
41 #ifdef SMP
42 #include <machine/smptests.h> /** CPUSTOP_ON_DDBBREAK */
43 #endif
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47
48 #include <ddb/ddb.h>
49
50 #include <machine/setjmp.h>
51
52 static jmp_buf *db_nofault = 0;
53 extern jmp_buf db_jmpbuf;
54
55 extern void gdb_handle_exception(db_regs_t *, int, int);
56
57 int db_active;
58 db_regs_t ddb_regs;
59
60 static jmp_buf db_global_jmpbuf;
61
62 static __inline u_short
63 rss(void)
64 {
65 u_short ss;
66 #ifdef __GNUC__
67 __asm __volatile("mov %%ss,%0" : "=r" (ss));
68 #else
69 ss = 0; /* XXXX Fix for other compilers. */
70 #endif
71 return ss;
72 }
73
74 /*
75 * kdb_trap - field a TRACE or BPT trap
76 */
77 int
78 kdb_trap(int type, int code, struct i386_saved_state *regs)
79 {
80 volatile int ddb_mode = !(boothowto & RB_GDB);
81
82 /*
83 * XXX try to do nothing if the console is in graphics mode.
84 * Handle trace traps (and hardware breakpoints...) by ignoring
85 * them except for forgetting about them. Return 0 for other
86 * traps to say that we haven't done anything. The trap handler
87 * will usually panic. We should handle breakpoint traps for
88 * our breakpoints by disarming our breakpoints and fixing up
89 * %eip.
90 */
91 if (cons_unavail && ddb_mode) {
92 if (type == T_TRCTRAP) {
93 regs->tf_eflags &= ~PSL_T;
94 return (1);
95 }
96 return (0);
97 }
98
99 switch (type) {
100 case T_BPTFLT: /* breakpoint */
101 case T_TRCTRAP: /* debug exception */
102 break;
103
104 default:
105 /*
106 * XXX this is almost useless now. In most cases,
107 * trap_fatal() has already printed a much more verbose
108 * message. However, it is dangerous to print things in
109 * trap_fatal() - printf() might be reentered and trap.
110 * The debugger should be given control first.
111 */
112 if (ddb_mode)
113 db_printf("kernel: type %d trap, code=%x\n", type, code);
114
115 if (db_nofault) {
116 jmp_buf *no_fault = db_nofault;
117 db_nofault = 0;
118 longjmp(*no_fault, 1);
119 }
120 }
121
122 /*
123 * This handles unexpected traps in ddb commands, including calls to
124 * non-ddb functions. db_nofault only applies to memory accesses by
125 * internal ddb commands.
126 */
127 if (db_active)
128 longjmp(db_global_jmpbuf, 1);
129
130 /*
131 * XXX We really should switch to a local stack here.
132 */
133 ddb_regs = *regs;
134
135 /*
136 * If in kernel mode, esp and ss are not saved, so dummy them up.
137 */
138 if (ISPL(regs->tf_cs) == 0) {
139 ddb_regs.tf_esp = (int)®s->tf_esp;
140 ddb_regs.tf_ss = rss();
141 }
142
143 #ifdef SMP
144 #ifdef CPUSTOP_ON_DDBBREAK
145
146 #if defined(VERBOSE_CPUSTOP_ON_DDBBREAK)
147 db_printf("\nCPU%d stopping CPUs: 0x%08x...", PCPU_GET(cpuid),
148 PCPU_GET(other_cpus));
149 #endif /* VERBOSE_CPUSTOP_ON_DDBBREAK */
150
151 /* We stop all CPUs except ourselves (obviously) */
152 stop_cpus(PCPU_GET(other_cpus));
153
154 #if defined(VERBOSE_CPUSTOP_ON_DDBBREAK)
155 db_printf(" stopped.\n");
156 #endif /* VERBOSE_CPUSTOP_ON_DDBBREAK */
157
158 #endif /* CPUSTOP_ON_DDBBREAK */
159 #endif /* SMP */
160
161 (void) setjmp(db_global_jmpbuf);
162 if (ddb_mode) {
163 if (!db_active)
164 cndbctl(TRUE);
165 db_active = 1;
166 db_trap(type, code);
167 cndbctl(FALSE);
168 } else {
169 db_active = 1;
170 gdb_handle_exception(&ddb_regs, type, code);
171 }
172 db_active = 0;
173
174 #ifdef SMP
175 #ifdef CPUSTOP_ON_DDBBREAK
176
177 #if defined(VERBOSE_CPUSTOP_ON_DDBBREAK)
178 db_printf("\nCPU%d restarting CPUs: 0x%08x...", PCPU_GET(cpuid),
179 stopped_cpus);
180 #endif /* VERBOSE_CPUSTOP_ON_DDBBREAK */
181
182 /* Restart all the CPUs we previously stopped */
183 if (stopped_cpus != PCPU_GET(other_cpus) && smp_started != 0) {
184 db_printf("whoa, other_cpus: 0x%08x, stopped_cpus: 0x%08x\n",
185 PCPU_GET(other_cpus), stopped_cpus);
186 panic("stop_cpus() failed");
187 }
188 restart_cpus(stopped_cpus);
189
190 #if defined(VERBOSE_CPUSTOP_ON_DDBBREAK)
191 db_printf(" restarted.\n");
192 #endif /* VERBOSE_CPUSTOP_ON_DDBBREAK */
193
194 #endif /* CPUSTOP_ON_DDBBREAK */
195 #endif /* SMP */
196
197 regs->tf_eip = ddb_regs.tf_eip;
198 regs->tf_eflags = ddb_regs.tf_eflags;
199 regs->tf_eax = ddb_regs.tf_eax;
200 regs->tf_ecx = ddb_regs.tf_ecx;
201 regs->tf_edx = ddb_regs.tf_edx;
202 regs->tf_ebx = ddb_regs.tf_ebx;
203
204 /*
205 * If in user mode, the saved ESP and SS were valid, restore them.
206 */
207 if (ISPL(regs->tf_cs)) {
208 regs->tf_esp = ddb_regs.tf_esp;
209 regs->tf_ss = ddb_regs.tf_ss & 0xffff;
210 }
211
212 regs->tf_ebp = ddb_regs.tf_ebp;
213 regs->tf_esi = ddb_regs.tf_esi;
214 regs->tf_edi = ddb_regs.tf_edi;
215 regs->tf_es = ddb_regs.tf_es & 0xffff;
216 regs->tf_fs = ddb_regs.tf_fs & 0xffff;
217 regs->tf_cs = ddb_regs.tf_cs & 0xffff;
218 regs->tf_ds = ddb_regs.tf_ds & 0xffff;
219 return (1);
220 }
221
222 /*
223 * Read bytes from kernel address space for debugger.
224 */
225 void
226 db_read_bytes(vm_offset_t addr, size_t size, char *data)
227 {
228 char *src;
229
230 db_nofault = &db_jmpbuf;
231
232 src = (char *)addr;
233 while (size-- > 0)
234 *data++ = *src++;
235
236 db_nofault = 0;
237 }
238
239 /*
240 * Write bytes to kernel address space for debugger.
241 */
242 void
243 db_write_bytes(vm_offset_t addr, size_t size, char *data)
244 {
245 char *dst;
246
247 unsigned *ptep0 = NULL;
248 unsigned oldmap0 = 0;
249 vm_offset_t addr1;
250 unsigned *ptep1 = NULL;
251 unsigned oldmap1 = 0;
252
253 db_nofault = &db_jmpbuf;
254
255 if (addr > trunc_page((vm_offset_t)btext) - size &&
256 addr < round_page((vm_offset_t)etext)) {
257
258 ptep0 = pmap_pte(kernel_pmap, addr);
259 oldmap0 = *ptep0;
260 *ptep0 |= PG_RW;
261
262 /* Map another page if the data crosses a page boundary. */
263 if ((*ptep0 & PG_PS) == 0) {
264 addr1 = trunc_page(addr + size - 1);
265 if (trunc_page(addr) != addr1) {
266 ptep1 = pmap_pte(kernel_pmap, addr1);
267 oldmap1 = *ptep1;
268 *ptep1 |= PG_RW;
269 }
270 } else {
271 addr1 = trunc_4mpage(addr + size - 1);
272 if (trunc_4mpage(addr) != addr1) {
273 ptep1 = pmap_pte(kernel_pmap, addr1);
274 oldmap1 = *ptep1;
275 *ptep1 |= PG_RW;
276 }
277 }
278
279 invltlb();
280 }
281
282 dst = (char *)addr;
283
284 while (size-- > 0)
285 *dst++ = *data++;
286
287 db_nofault = 0;
288
289 if (ptep0) {
290 *ptep0 = oldmap0;
291
292 if (ptep1)
293 *ptep1 = oldmap1;
294
295 invltlb();
296 }
297 }
298
299 /*
300 * XXX
301 * Move this to machdep.c and allow it to be called if any debugger is
302 * installed.
303 */
304 void
305 Debugger(const char *msg)
306 {
307 static volatile u_int in_Debugger;
308
309 /*
310 * XXX
311 * Do nothing if the console is in graphics mode. This is
312 * OK if the call is for the debugger hotkey but not if the call
313 * is a weak form of panicing.
314 */
315 if (cons_unavail && !(boothowto & RB_GDB))
316 return;
317
318 if (atomic_cmpset_acq_int(&in_Debugger, 0, 1)) {
319 db_printf("Debugger(\"%s\")\n", msg);
320 breakpoint();
321 atomic_store_rel_int(&in_Debugger, 0);
322 }
323 }
324
325 void
326 db_show_mdpcpu(struct pcpu *pc)
327 {
328
329 db_printf("currentldt = 0x%x\n", pc->pc_currentldt);
330 }
Cache object: 4b8e0a531bdb9c7a026c7e7032e49737
|