2 * Copyright (c) 2015 Roger Pau Monné <roger.pau@citrix.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_stack.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
40 #include <sys/stack.h>
43 #include <xen/xen-os.h>
44 #include <xen/xen_intr.h>
45 #include <xen/hypervisor.h>
50 * Handles the VIRQ_DEBUG interrupt and prints the backtrace of each
51 * vCPU on the Xen console.
54 DPCPU_DEFINE(xen_intr_handle_t, xendebug_handler);
55 static struct mtx lock;
56 static struct sbuf *buf;
59 xendebug_drain(void *arg, const char *str, int len)
62 HYPERVISOR_console_write(__DECONST(char *, str), len);
67 stack_capture(struct stack *st, register_t rbp);
70 xendebug_filter(void *arg)
72 #if defined(STACK) && defined(DDB)
74 struct trapframe *frame;
82 xc_printf("Printing stack trace vCPU%d\n", PCPU_GET(vcpu_id));
83 stack_sbuf_print_ddb(buf, &st);
85 mtx_unlock_spin(&lock);
88 return (FILTER_HANDLED);
92 xendebug_identify(driver_t *driver, device_t parent)
96 ("Trying to add Xen debug device to non-xen guest"));
98 if (xen_hvm_domain() && !xen_vector_callback_enabled)
101 if (BUS_ADD_CHILD(parent, 0, "debug", 0) == NULL)
102 panic("Unable to add Xen debug device.");
106 xendebug_probe(device_t dev)
109 device_set_desc(dev, "Xen debug handler");
110 return (BUS_PROBE_NOWILDCARD);
114 xendebug_attach(device_t dev)
118 mtx_init(&lock, "xen-dbg", NULL, MTX_SPIN);
119 buf = sbuf_new(NULL, NULL, 1024, SBUF_FIXEDLEN);
121 panic("Unable to create sbuf for stack dump");
122 sbuf_set_drain(buf, xendebug_drain, NULL);
124 /* Bind an event channel to a VIRQ on each VCPU. */
126 error = xen_intr_bind_virq(dev, VIRQ_DEBUG, i, xendebug_filter,
127 NULL, NULL, INTR_TYPE_TTY,
128 DPCPU_ID_PTR(i, xendebug_handler));
130 printf("Failed to bind VIRQ_DEBUG to vCPU %d: %d",
134 xen_intr_describe(DPCPU_ID_GET(i, xendebug_handler), "d%d", i);
140 static device_method_t xendebug_methods[] = {
141 DEVMETHOD(device_identify, xendebug_identify),
142 DEVMETHOD(device_probe, xendebug_probe),
143 DEVMETHOD(device_attach, xendebug_attach),
148 static driver_t xendebug_driver = {
154 devclass_t xendebug_devclass;
156 DRIVER_MODULE(xendebug, xenpv, xendebug_driver, xendebug_devclass, 0, 0);
157 MODULE_DEPEND(xendebug, xenpv, 1, 1, 1);