76951c5f78ffb5222660083e88bf4925d26123d8
[dragonfly.git] / sys / platform / pc32 / i386 / db_interface.c
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: src/sys/i386/i386/db_interface.c,v 1.48.2.1 2000/07/07 00:38:46 obrien Exp $
27  * $DragonFly: src/sys/platform/pc32/i386/db_interface.c,v 1.16 2007/01/08 03:33:42 dillon Exp $
28  */
29
30 /*
31  * Interface to new debugger.
32  */
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/reboot.h>
36 #include <sys/cons.h>
37 #include <sys/thread.h>
38
39 #include <machine/cpu.h>
40 #include <machine/smp.h>
41 #include <machine/globaldata.h>
42
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45
46 #include <ddb/ddb.h>
47
48 #include <sys/thread2.h>
49
50 #include <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 static int      db_global_jmpbuf_valid;
62
63 #ifdef __GNUC__
64 #define rss() ({u_short ss; __asm __volatile("mov %%ss,%0" : "=r" (ss)); ss;})
65 #endif
66
67 /*
68  *  kdb_trap - field a TRACE or BPT trap
69  */
70 int
71 kdb_trap(int type, int code, struct i386_saved_state *regs)
72 {
73         volatile int ddb_mode = !(boothowto & RB_GDB);
74
75         /*
76          * XXX try to do nothing if the console is in graphics mode.
77          * Handle trace traps (and hardware breakpoints...) by ignoring
78          * them except for forgetting about them.  Return 0 for other
79          * traps to say that we haven't done anything.  The trap handler
80          * will usually panic.  We should handle breakpoint traps for
81          * our breakpoints by disarming our breakpoints and fixing up
82          * %eip.
83          */
84         if (cons_unavail && ddb_mode) {
85             if (type == T_TRCTRAP) {
86                 regs->tf_eflags &= ~PSL_T;
87                 return (1);
88             }
89             return (0);
90         }
91
92         switch (type) {
93             case T_BPTFLT:      /* breakpoint */
94             case T_TRCTRAP:     /* debug exception */
95                 break;
96
97             default:
98                 /*
99                  * XXX this is almost useless now.  In most cases,
100                  * trap_fatal() has already printed a much more verbose
101                  * message.  However, it is dangerous to print things in
102                  * trap_fatal() - kprintf() might be reentered and trap.
103                  * The debugger should be given control first.
104                  */
105                 if (ddb_mode)
106                     db_printf("kernel: type %d trap, code=%x\n", type, code);
107
108                 if (db_nofault) {
109                     jmp_buf *no_fault = db_nofault;
110                     db_nofault = 0;
111                     longjmp(*no_fault, 1);
112                 }
113         }
114
115         /*
116          * This handles unexpected traps in ddb commands, including calls to
117          * non-ddb functions.  db_nofault only applies to memory accesses by
118          * internal ddb commands.
119          */
120         if (db_global_jmpbuf_valid)
121             longjmp(db_global_jmpbuf, 1);
122
123         /*
124          * XXX We really should switch to a local stack here.
125          */
126         ddb_regs = *regs;
127
128         /*
129          * If in kernel mode, esp and ss are not saved, so dummy them up.
130          */
131         if (ISPL(regs->tf_cs) == 0) {
132             ddb_regs.tf_esp = (int)&regs->tf_esp;
133             ddb_regs.tf_ss = rss();
134         }
135
136         crit_enter();
137 #ifdef SMP
138         db_printf("\nCPU%d stopping CPUs: 0x%08x\n", 
139             mycpu->gd_cpuid, mycpu->gd_other_cpus);
140
141         /* We stop all CPUs except ourselves (obviously) */
142         stop_cpus(mycpu->gd_other_cpus);
143
144         db_printf(" stopped\n");
145 #endif /* SMP */
146
147         setjmp(db_global_jmpbuf);
148         db_global_jmpbuf_valid = TRUE;
149         db_active++;
150         if (ddb_mode) {
151             cndbctl(TRUE);
152             db_trap(type, code);
153             cndbctl(FALSE);
154         } else
155             gdb_handle_exception(&ddb_regs, type, code);
156         db_active--;
157         db_global_jmpbuf_valid = FALSE;
158
159 #ifdef SMP
160         db_printf("\nCPU%d restarting CPUs: 0x%08x\n",
161             mycpu->gd_cpuid, stopped_cpus);
162
163         /* Restart all the CPUs we previously stopped */
164         if (stopped_cpus != mycpu->gd_other_cpus) {
165                 db_printf("whoa, other_cpus: 0x%08x, stopped_cpus: 0x%08x\n",
166                           mycpu->gd_other_cpus, stopped_cpus);
167                 panic("stop_cpus() failed");
168         }
169         restart_cpus(stopped_cpus);
170
171         db_printf(" restarted\n");
172 #endif /* SMP */
173         crit_exit();
174
175         regs->tf_eip    = ddb_regs.tf_eip;
176         regs->tf_eflags = ddb_regs.tf_eflags;
177         regs->tf_eax    = ddb_regs.tf_eax;
178         regs->tf_ecx    = ddb_regs.tf_ecx;
179         regs->tf_edx    = ddb_regs.tf_edx;
180         regs->tf_ebx    = ddb_regs.tf_ebx;
181
182         /*
183          * If in user mode, the saved ESP and SS were valid, restore them.
184          */
185         if (ISPL(regs->tf_cs)) {
186             regs->tf_esp = ddb_regs.tf_esp;
187             regs->tf_ss  = ddb_regs.tf_ss & 0xffff;
188         }
189
190         regs->tf_ebp    = ddb_regs.tf_ebp;
191         regs->tf_esi    = ddb_regs.tf_esi;
192         regs->tf_edi    = ddb_regs.tf_edi;
193         regs->tf_es     = ddb_regs.tf_es & 0xffff;
194         regs->tf_fs     = ddb_regs.tf_fs & 0xffff;
195         regs->tf_gs     = ddb_regs.tf_gs & 0xffff;
196         regs->tf_cs     = ddb_regs.tf_cs & 0xffff;
197         regs->tf_ds     = ddb_regs.tf_ds & 0xffff;
198         return (1);
199 }
200
201 /*
202  * Read bytes from kernel address space for debugger.
203  */
204 void
205 db_read_bytes(vm_offset_t addr, size_t size, char *data)
206 {
207         char    *src;
208
209         db_nofault = &db_jmpbuf;
210
211         src = (char *)addr;
212         while (size-- > 0)
213             *data++ = *src++;
214
215         db_nofault = 0;
216 }
217
218 /*
219  * Write bytes to kernel address space for debugger.
220  */
221 void
222 db_write_bytes(vm_offset_t addr, size_t size, char *data)
223 {
224         char    *dst;
225
226         unsigned        *ptep0 = NULL;
227         unsigned        oldmap0 = 0;
228         vm_offset_t     addr1;
229         unsigned        *ptep1 = NULL;
230         unsigned        oldmap1 = 0;
231
232         db_nofault = &db_jmpbuf;
233
234         if (addr > trunc_page((vm_offset_t)btext) - size &&
235             addr < round_page((vm_offset_t)etext)) {
236
237             ptep0 = pmap_kernel_pte(addr);
238             oldmap0 = *ptep0;
239             *ptep0 |= PG_RW;
240
241             /* Map another page if the data crosses a page boundary. */
242             if ((*ptep0 & PG_PS) == 0) {
243                 addr1 = trunc_page(addr + size - 1);
244                 if (trunc_page(addr) != addr1) {
245                     ptep1 = pmap_kernel_pte(addr1);
246                     oldmap1 = *ptep1;
247                     *ptep1 |= PG_RW;
248                 }
249             } else {
250                 addr1 = trunc_4mpage(addr + size - 1);
251                 if (trunc_4mpage(addr) != addr1) {
252                     ptep1 = pmap_kernel_pte(addr1);
253                     oldmap1 = *ptep1;
254                     *ptep1 |= PG_RW;
255                 }
256             }
257
258             cpu_invltlb();
259         }
260
261         dst = (char *)addr;
262
263         while (size-- > 0)
264             *dst++ = *data++;
265
266         db_nofault = 0;
267
268         if (ptep0) {
269             *ptep0 = oldmap0;
270
271             if (ptep1)
272                 *ptep1 = oldmap1;
273
274             cpu_invltlb();
275         }
276 }
277
278 /*
279  * The debugger sometimes needs to know the actual KVM address represented
280  * by the instruction pointer, stack pointer, or base pointer.  Normally
281  * the actual KVM address is simply the contents of the register.  However,
282  * if the debugger is entered from the BIOS or VM86 we need to figure out
283  * the offset from the segment register.
284  */
285 db_addr_t
286 PC_REGS(db_regs_t *regs)
287 {
288     struct soft_segment_descriptor softseg;
289
290     sdtossd(&gdt[mycpu->gd_cpuid * NGDT + IDXSEL(regs->tf_cs & 0xffff)].sd, &softseg);
291     return(regs->tf_eip + softseg.ssd_base);
292 }
293
294 db_addr_t
295 SP_REGS(db_regs_t *regs)
296 {
297     struct soft_segment_descriptor softseg;
298
299     sdtossd(&gdt[mycpu->gd_cpuid * NGDT + IDXSEL(regs->tf_ss & 0xffff)].sd, &softseg);
300     return(regs->tf_esp + softseg.ssd_base);
301 }
302
303 db_addr_t
304 BP_REGS(db_regs_t *regs)
305 {
306     struct soft_segment_descriptor softseg;
307
308     sdtossd(&gdt[mycpu->gd_cpuid * NGDT + IDXSEL(regs->tf_ds & 0xffff)].sd, &softseg);
309     return(regs->tf_ebp + softseg.ssd_base);
310 }
311
312 /*
313  * XXX
314  * Move this to machdep.c and allow it to be called if any debugger is
315  * installed.
316  */
317 void
318 Debugger(const char *msg)
319 {
320         static volatile u_char in_Debugger;
321
322         /*
323          * XXX
324          * Do nothing if the console is in graphics mode.  This is
325          * OK if the call is for the debugger hotkey but not if the call
326          * is a weak form of panicing.
327          */
328         if (cons_unavail && !(boothowto & RB_GDB))
329             return;
330
331         if (!in_Debugger) {
332             in_Debugger = 1;
333             db_printf("Debugger(\"%s\")\n", msg);
334             breakpoint();
335             in_Debugger = 0;
336         }
337 }