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