kernel: Make SMP support default (and non-optional).
[dragonfly.git] / sys / platform / vkernel64 / x86_64 / db_interface.c
CommitLineData
da673940
JG
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * 3. Neither the name of The DragonFly Project nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific, prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * --
32 *
33 * Mach Operating System
34 * Copyright (c) 1991,1990 Carnegie Mellon University
35 * All Rights Reserved.
36 *
37 * Permission to use, copy, modify and distribute this software and its
38 * documentation is hereby granted, provided that both the copyright
39 * notice and this permission notice appear in all copies of the
40 * software, derivative works or modified versions, and any portions
41 * thereof, and that both notices appear in supporting documentation.
42 *
43 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
44 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
45 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 *
47 * Carnegie Mellon requests users of this software to return to
48 *
49 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
50 * School of Computer Science
51 * Carnegie Mellon University
52 * Pittsburgh PA 15213-3890
53 *
54 * any improvements or extensions that they make and grant Carnegie the
55 * rights to redistribute these changes.
56 *
57 * $FreeBSD: src/sys/i386/i386/db_interface.c,v 1.48.2.1 2000/07/07 00:38:46 obrien Exp $
da673940
JG
58 */
59
60/*
61 * Interface to new debugger.
62 */
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/reboot.h>
66#include <sys/cons.h>
67#include <sys/thread.h>
68
69#include <machine/cpu.h>
70#include <machine/smp.h>
71#include <machine/globaldata.h>
72#include <machine/md_var.h>
73
74#include <vm/vm.h>
75#include <vm/pmap.h>
76
77#include <ddb/ddb.h>
78
79#include <sys/thread2.h>
80
81#include <setjmp.h>
82
4090d6ff 83static jmp_buf *db_nofault = NULL;
da673940
JG
84extern jmp_buf db_jmpbuf;
85
86extern void gdb_handle_exception (db_regs_t *, int, int);
87
88int db_active;
89db_regs_t ddb_regs;
90
91static jmp_buf db_global_jmpbuf;
92static int db_global_jmpbuf_valid;
93
94#ifdef __GNUC__
95#define rss() ({u_short ss; __asm __volatile("mov %%ss,%0" : "=r" (ss)); ss;})
96#endif
97
98/*
99 * kdb_trap - field a TRACE or BPT trap
100 */
101int
0e6594a8 102kdb_trap(int type, int code, struct x86_64_saved_state *regs)
da673940
JG
103{
104 volatile int ddb_mode = !(boothowto & RB_GDB);
105
106 /*
107 * XXX try to do nothing if the console is in graphics mode.
108 * Handle trace traps (and hardware breakpoints...) by ignoring
109 * them except for forgetting about them. Return 0 for other
110 * traps to say that we haven't done anything. The trap handler
111 * will usually panic. We should handle breakpoint traps for
112 * our breakpoints by disarming our breakpoints and fixing up
113 * %eip.
114 */
115 if (cons_unavail && ddb_mode) {
116 if (type == T_TRCTRAP) {
117 regs->tf_rflags &= ~PSL_T;
118 return (1);
119 }
120 return (0);
121 }
122
123 switch (type) {
124 case T_BPTFLT: /* breakpoint */
125 case T_TRCTRAP: /* debug exception */
126 break;
127
128 default:
129 /*
130 * XXX this is almost useless now. In most cases,
131 * trap_fatal() has already printed a much more verbose
132 * message. However, it is dangerous to print things in
133 * trap_fatal() - kprintf() might be reentered and trap.
134 * The debugger should be given control first.
135 */
136 if (ddb_mode)
137 db_printf("kernel: type %d trap, code=%x\n", type, code);
138
139 if (db_nofault) {
140 jmp_buf *no_fault = db_nofault;
4090d6ff 141 db_nofault = NULL;
da673940
JG
142 longjmp(*no_fault, 1);
143 }
144 }
145
146 /*
147 * This handles unexpected traps in ddb commands, including calls to
148 * non-ddb functions. db_nofault only applies to memory accesses by
149 * internal ddb commands.
150 */
151 if (db_global_jmpbuf_valid)
152 longjmp(db_global_jmpbuf, 1);
153
154 /*
155 * XXX We really should switch to a local stack here.
156 */
157 ddb_regs = *regs;
158
159 crit_enter();
b6bf0651
MD
160 db_printf("\nCPU%d stopping CPUs: 0x%016jx\n",
161 mycpu->gd_cpuid, (uintmax_t)mycpu->gd_other_cpus);
da673940
JG
162
163 /* We stop all CPUs except ourselves (obviously) */
164 stop_cpus(mycpu->gd_other_cpus);
165
166 db_printf(" stopped\n");
da673940
JG
167
168 setjmp(db_global_jmpbuf);
169 db_global_jmpbuf_valid = TRUE;
170 db_active++;
171 vcons_set_mode(1);
172 if (ddb_mode) {
173 cndbctl(TRUE);
174 db_trap(type, code);
175 cndbctl(FALSE);
176 } else
177 gdb_handle_exception(&ddb_regs, type, code);
178 db_active--;
179 vcons_set_mode(0);
180 db_global_jmpbuf_valid = FALSE;
181
da23a592
MD
182 db_printf("\nCPU%d restarting CPUs: 0x%016jx\n",
183 mycpu->gd_cpuid, (uintmax_t)stopped_cpus);
da673940
JG
184
185 /* Restart all the CPUs we previously stopped */
186 if (stopped_cpus != mycpu->gd_other_cpus) {
b6bf0651
MD
187 db_printf("whoa, other_cpus: 0x%016jx, "
188 "stopped_cpus: 0x%016jx\n",
189 (uintmax_t)mycpu->gd_other_cpus,
190 (uintmax_t)stopped_cpus);
da673940
JG
191 panic("stop_cpus() failed");
192 }
193 restart_cpus(stopped_cpus);
194
195 db_printf(" restarted\n");
da673940
JG
196 crit_exit();
197
198 regs->tf_rip = ddb_regs.tf_rip;
199 regs->tf_rflags = ddb_regs.tf_rflags;
200 regs->tf_rax = ddb_regs.tf_rax;
201 regs->tf_rcx = ddb_regs.tf_rcx;
202 regs->tf_rdx = ddb_regs.tf_rdx;
203 regs->tf_rbx = ddb_regs.tf_rbx;
204
205 regs->tf_rsp = ddb_regs.tf_rsp;
206 regs->tf_ss = ddb_regs.tf_ss & 0xffff;
207
208 regs->tf_rbp = ddb_regs.tf_rbp;
209 regs->tf_rsi = ddb_regs.tf_rsi;
210 regs->tf_rdi = ddb_regs.tf_rdi;
211
212 regs->tf_r8 = ddb_regs.tf_r8;
213 regs->tf_r9 = ddb_regs.tf_r9;
214 regs->tf_r10 = ddb_regs.tf_r10;
215 regs->tf_r11 = ddb_regs.tf_r11;
216 regs->tf_r12 = ddb_regs.tf_r12;
217 regs->tf_r13 = ddb_regs.tf_r13;
218 regs->tf_r14 = ddb_regs.tf_r14;
219 regs->tf_r15 = ddb_regs.tf_r15;
220
221 /* regs->tf_es = ddb_regs.tf_es & 0xffff; */
222 /* regs->tf_fs = ddb_regs.tf_fs & 0xffff; */
223 /* regs->tf_gs = ddb_regs.tf_gs & 0xffff; */
224 regs->tf_cs = ddb_regs.tf_cs & 0xffff;
225 /* regs->tf_ds = ddb_regs.tf_ds & 0xffff; */
226 return (1);
227}
228
229/*
230 * Read bytes from kernel address space for debugger.
231 */
232void
233db_read_bytes(vm_offset_t addr, size_t size, char *data)
234{
235 char *src;
236
237 db_nofault = &db_jmpbuf;
238
239 src = (char *)addr;
240 while (size-- > 0)
241 *data++ = *src++;
242
4090d6ff 243 db_nofault = NULL;
da673940
JG
244}
245
246/*
247 * Write bytes to kernel address space for debugger.
248 */
249void
250db_write_bytes(vm_offset_t addr, size_t size, char *data)
251{
252 char *dst;
253#if 0
254 vpte_t *ptep0 = NULL;
255 vpte_t oldmap0 = 0;
256 vm_offset_t addr1;
257 vpte_t *ptep1 = NULL;
258 vpte_t oldmap1 = 0;
259#endif
260
261 db_nofault = &db_jmpbuf;
262#if 0
263 if (addr > trunc_page((vm_offset_t)btext) - size &&
264 addr < round_page((vm_offset_t)etext)) {
265
266 ptep0 = pmap_kpte(addr);
267 oldmap0 = *ptep0;
268 *ptep0 |= VPTE_W;
269
270 /* Map another page if the data crosses a page boundary. */
271 if ((*ptep0 & PG_PS) == 0) {
272 addr1 = trunc_page(addr + size - 1);
273 if (trunc_page(addr) != addr1) {
274 ptep1 = pmap_kpte(addr1);
275 oldmap1 = *ptep1;
276 *ptep1 |= VPTE_W;
277 }
278 } else {
279 addr1 = trunc_4mpage(addr + size - 1);
280 if (trunc_4mpage(addr) != addr1) {
281 ptep1 = pmap_kpte(addr1);
282 oldmap1 = *ptep1;
283 *ptep1 |= VPTE_W;
284 }
285 }
286
287 cpu_invltlb();
288 }
289#endif
290
291 dst = (char *)addr;
292
293 while (size-- > 0)
294 *dst++ = *data++;
295
4090d6ff 296 db_nofault = NULL;
da673940
JG
297
298#if 0
299 if (ptep0) {
300 *ptep0 = oldmap0;
301
302 if (ptep1)
303 *ptep1 = oldmap1;
304
305 cpu_invltlb();
306 }
307#endif
308}
309
310/*
311 * The debugger sometimes needs to know the actual KVM address represented
312 * by the instruction pointer, stack pointer, or base pointer. Normally
313 * the actual KVM address is simply the contents of the register. However,
314 * if the debugger is entered from the BIOS or VM86 we need to figure out
315 * the offset from the segment register.
316 */
317db_addr_t
318PC_REGS(db_regs_t *regs)
319{
320 return(regs->tf_rip);
321}
322
323db_addr_t
324SP_REGS(db_regs_t *regs)
325{
326 return(regs->tf_rsp);
327}
328
329db_addr_t
330BP_REGS(db_regs_t *regs)
331{
332 return(regs->tf_rbp);
333}
334
335/*
336 * XXX
337 * Move this to machdep.c and allow it to be called if any debugger is
338 * installed.
339 */
340void
341Debugger(const char *msg)
342{
343 static volatile u_char in_Debugger;
344
345 /*
346 * XXX
347 * Do nothing if the console is in graphics mode. This is
348 * OK if the call is for the debugger hotkey but not if the call
349 * is a weak form of panicing.
350 */
351 if (cons_unavail && !(boothowto & RB_GDB))
352 return;
353
354 if (!in_Debugger) {
355 in_Debugger = 1;
356 db_printf("Debugger(\"%s\")\n", msg);
357 breakpoint();
358 in_Debugger = 0;
359 }
360}