xref: /freebsd/sys/dev/xen/debug/debug.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*
2  * Copyright (c) 2015 Roger Pau Monné <roger.pau@citrix.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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 the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include "opt_stack.h"
29 #include "opt_ddb.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/pcpu.h>
39 #include <sys/smp.h>
40 #include <sys/stack.h>
41 #include <sys/sbuf.h>
42 
43 #include <xen/xen-os.h>
44 #include <xen/xen_intr.h>
45 #include <xen/hypervisor.h>
46 
47 /*
48  * Xen debug device
49  *
50  * Handles the VIRQ_DEBUG interrupt and prints the backtrace of each
51  * vCPU on the Xen console.
52  */
53 
54 DPCPU_DEFINE(xen_intr_handle_t, xendebug_handler);
55 static struct mtx lock;
56 static struct sbuf *buf;
57 
58 static int
59 xendebug_drain(void *arg, const char *str, int len)
60 {
61 	/*
62 	 * Use xen_emergency_print() instead of xc_printf() to avoid the
63 	 * overhead of parsing a format string when it's not needed.
64 	 */
65 	xen_emergency_print(str, len);
66 	return (len);
67 }
68 
69 extern void
70 stack_capture(struct stack *st, register_t rbp);
71 
72 static int
73 xendebug_filter(void *arg __unused)
74 {
75 #if defined(STACK) && defined(DDB)
76 	struct stack st;
77 
78 	stack_save(&st);
79 
80 	mtx_lock_spin(&lock);
81 	xc_printf("Printing stack trace vCPU%u\n", XEN_VCPUID());
82 	stack_sbuf_print_ddb(buf, &st);
83 	sbuf_drain(buf);
84 	mtx_unlock_spin(&lock);
85 #endif
86 
87 	return (FILTER_HANDLED);
88 }
89 
90 static void
91 xendebug_identify(driver_t *driver, device_t parent)
92 {
93 
94 	KASSERT(xen_domain(),
95 	    ("Trying to add Xen debug device to non-xen guest"));
96 
97 	if (!xen_has_percpu_evtchn())
98 		return;
99 
100 	if (BUS_ADD_CHILD(parent, 0, "debug", 0) == NULL)
101 		panic("Unable to add Xen debug device.");
102 }
103 
104 static int
105 xendebug_probe(device_t dev)
106 {
107 
108 	device_set_desc(dev, "Xen debug handler");
109 	return (BUS_PROBE_NOWILDCARD);
110 }
111 
112 static int
113 xendebug_attach(device_t dev)
114 {
115 	int i, error;
116 
117 	mtx_init(&lock, "xen-dbg", NULL, MTX_SPIN);
118 	buf = sbuf_new(NULL, NULL, 1024, SBUF_FIXEDLEN);
119 	if (buf == NULL)
120 		panic("Unable to create sbuf for stack dump");
121 	sbuf_set_drain(buf, xendebug_drain, NULL);
122 
123 	/* Bind an event channel to a VIRQ on each VCPU. */
124 	CPU_FOREACH(i) {
125 		error = xen_intr_bind_virq(dev, VIRQ_DEBUG, i, xendebug_filter,
126 		    NULL, NULL, INTR_TYPE_TTY,
127 		    DPCPU_ID_PTR(i, xendebug_handler));
128 		if (error != 0) {
129 			printf("Failed to bind VIRQ_DEBUG to vCPU %d: %d",
130 			    i, error);
131 			continue;
132 		}
133 		xen_intr_describe(DPCPU_ID_GET(i, xendebug_handler), "d%d", i);
134 	}
135 
136 	return (0);
137 }
138 
139 static device_method_t xendebug_methods[] = {
140 	DEVMETHOD(device_identify, xendebug_identify),
141 	DEVMETHOD(device_probe, xendebug_probe),
142 	DEVMETHOD(device_attach, xendebug_attach),
143 
144 	DEVMETHOD_END
145 };
146 
147 static driver_t xendebug_driver = {
148 	"debug",
149 	xendebug_methods,
150 	0,
151 };
152 
153 DRIVER_MODULE(xendebug, xenpv, xendebug_driver, 0, 0);
154 MODULE_DEPEND(xendebug, xenpv, 1, 1, 1);
155