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