2016-10-25 08:50:09 +03:00
|
|
|
/*
|
|
|
|
* Xen para-virtualization device
|
|
|
|
*
|
|
|
|
* (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 15:44:24 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2016-10-25 08:50:09 +03:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2016-11-02 13:19:18 +03:00
|
|
|
#include "qemu/log.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 08:23:50 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2016-11-22 09:10:58 +03:00
|
|
|
#include "hw/qdev-core.h"
|
2019-01-08 17:48:46 +03:00
|
|
|
#include "hw/xen/xen-legacy-backend.h"
|
2016-10-25 08:50:09 +03:00
|
|
|
#include "hw/xen/xen_pvdev.h"
|
|
|
|
|
2016-10-25 08:50:12 +03:00
|
|
|
/* private */
|
2016-10-25 08:50:09 +03:00
|
|
|
static int debug;
|
2016-10-25 08:50:13 +03:00
|
|
|
|
|
|
|
struct xs_dirs {
|
|
|
|
char *xs_dir;
|
|
|
|
QTAILQ_ENTRY(xs_dirs) list;
|
|
|
|
};
|
|
|
|
|
2018-12-06 13:58:10 +03:00
|
|
|
static QTAILQ_HEAD(, xs_dirs) xs_cleanup =
|
2016-10-25 08:50:13 +03:00
|
|
|
QTAILQ_HEAD_INITIALIZER(xs_cleanup);
|
|
|
|
|
2019-01-08 17:48:46 +03:00
|
|
|
static QTAILQ_HEAD(, XenLegacyDevice) xendevs =
|
2016-10-25 08:50:12 +03:00
|
|
|
QTAILQ_HEAD_INITIALIZER(xendevs);
|
|
|
|
|
2016-10-25 08:50:09 +03:00
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
2016-10-25 08:50:13 +03:00
|
|
|
static void xenstore_cleanup_dir(char *dir)
|
|
|
|
{
|
|
|
|
struct xs_dirs *d;
|
|
|
|
|
|
|
|
d = g_malloc(sizeof(*d));
|
|
|
|
d->xs_dir = dir;
|
|
|
|
QTAILQ_INSERT_TAIL(&xs_cleanup, d, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xen_config_cleanup(void)
|
|
|
|
{
|
|
|
|
struct xs_dirs *d;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(d, &xs_cleanup, list) {
|
2023-01-02 14:05:16 +03:00
|
|
|
qemu_xen_xs_destroy(xenstore, 0, d->xs_dir);
|
2016-10-25 08:50:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int xenstore_mkdir(char *path, int p)
|
|
|
|
{
|
2023-01-02 14:05:16 +03:00
|
|
|
if (!qemu_xen_xs_create(xenstore, 0, 0, xen_domid, p, path)) {
|
2016-10-25 08:50:14 +03:00
|
|
|
xen_pv_printf(NULL, 0, "xs_mkdir %s: failed\n", path);
|
2016-10-25 08:50:13 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
xenstore_cleanup_dir(g_strdup(path));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-25 08:50:09 +03:00
|
|
|
int xenstore_write_str(const char *base, const char *node, const char *val)
|
|
|
|
{
|
|
|
|
char abspath[XEN_BUFSIZE];
|
|
|
|
|
|
|
|
snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
|
2023-01-02 14:05:16 +03:00
|
|
|
if (!qemu_xen_xs_write(xenstore, 0, abspath, val, strlen(val))) {
|
2016-10-25 08:50:09 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xenstore_read_str(const char *base, const char *node)
|
|
|
|
{
|
|
|
|
char abspath[XEN_BUFSIZE];
|
|
|
|
unsigned int len;
|
|
|
|
char *str, *ret = NULL;
|
|
|
|
|
|
|
|
snprintf(abspath, sizeof(abspath), "%s/%s", base, node);
|
2023-01-02 14:05:16 +03:00
|
|
|
str = qemu_xen_xs_read(xenstore, 0, abspath, &len);
|
2016-10-25 08:50:09 +03:00
|
|
|
if (str != NULL) {
|
|
|
|
/* move to qemu-allocated memory to make sure
|
|
|
|
* callers can savely g_free() stuff. */
|
|
|
|
ret = g_strdup(str);
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xenstore_write_int(const char *base, const char *node, int ival)
|
|
|
|
{
|
|
|
|
char val[12];
|
|
|
|
|
|
|
|
snprintf(val, sizeof(val), "%d", ival);
|
|
|
|
return xenstore_write_str(base, node, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xenstore_write_int64(const char *base, const char *node, int64_t ival)
|
|
|
|
{
|
|
|
|
char val[21];
|
|
|
|
|
|
|
|
snprintf(val, sizeof(val), "%"PRId64, ival);
|
|
|
|
return xenstore_write_str(base, node, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
int xenstore_read_int(const char *base, const char *node, int *ival)
|
|
|
|
{
|
|
|
|
char *val;
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
val = xenstore_read_str(base, node);
|
|
|
|
if (val && 1 == sscanf(val, "%d", ival)) {
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
g_free(val);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xenstore_read_uint64(const char *base, const char *node, uint64_t *uval)
|
|
|
|
{
|
|
|
|
char *val;
|
|
|
|
int rc = -1;
|
|
|
|
|
|
|
|
val = xenstore_read_str(base, node);
|
|
|
|
if (val && 1 == sscanf(val, "%"SCNu64, uval)) {
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
g_free(val);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *xenbus_strstate(enum xenbus_state state)
|
|
|
|
{
|
|
|
|
static const char *const name[] = {
|
|
|
|
[XenbusStateUnknown] = "Unknown",
|
|
|
|
[XenbusStateInitialising] = "Initialising",
|
|
|
|
[XenbusStateInitWait] = "InitWait",
|
|
|
|
[XenbusStateInitialised] = "Initialised",
|
|
|
|
[XenbusStateConnected] = "Connected",
|
|
|
|
[XenbusStateClosing] = "Closing",
|
|
|
|
[XenbusStateClosed] = "Closed",
|
|
|
|
};
|
|
|
|
return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* msg_level:
|
|
|
|
* 0 == errors (stderr + logfile).
|
|
|
|
* 1 == informative debug messages (logfile only).
|
|
|
|
* 2 == noisy debug messages (logfile only).
|
|
|
|
* 3 == will flood your log (logfile only).
|
|
|
|
*/
|
2022-12-19 16:02:01 +03:00
|
|
|
G_GNUC_PRINTF(3, 0)
|
2022-04-17 21:29:48 +03:00
|
|
|
static void xen_pv_output_msg(struct XenLegacyDevice *xendev,
|
|
|
|
FILE *f, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
if (xendev) {
|
|
|
|
fprintf(f, "xen be: %s: ", xendev->name);
|
|
|
|
} else {
|
|
|
|
fprintf(f, "xen be core: ");
|
|
|
|
}
|
|
|
|
vfprintf(f, fmt, args);
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:46 +03:00
|
|
|
void xen_pv_printf(struct XenLegacyDevice *xendev, int msg_level,
|
2016-10-25 08:50:09 +03:00
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
2022-04-17 21:29:48 +03:00
|
|
|
FILE *logfile;
|
2016-10-25 08:50:09 +03:00
|
|
|
va_list args;
|
|
|
|
|
2022-04-17 21:29:48 +03:00
|
|
|
if (msg_level > (xendev ? xendev->debug : debug)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logfile = qemu_log_trylock();
|
|
|
|
if (logfile) {
|
|
|
|
va_start(args, fmt);
|
|
|
|
xen_pv_output_msg(xendev, logfile, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
qemu_log_unlock(logfile);
|
2016-10-25 08:50:09 +03:00
|
|
|
}
|
2022-04-17 21:29:48 +03:00
|
|
|
|
2016-10-25 08:50:09 +03:00
|
|
|
if (msg_level == 0) {
|
|
|
|
va_start(args, fmt);
|
2022-04-17 21:29:48 +03:00
|
|
|
xen_pv_output_msg(xendev, stderr, fmt, args);
|
2016-10-25 08:50:09 +03:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 08:50:11 +03:00
|
|
|
|
2016-10-25 08:50:17 +03:00
|
|
|
void xen_pv_evtchn_event(void *opaque)
|
2016-10-25 08:50:11 +03:00
|
|
|
{
|
2019-01-08 17:48:46 +03:00
|
|
|
struct XenLegacyDevice *xendev = opaque;
|
2016-10-25 08:50:11 +03:00
|
|
|
evtchn_port_t port;
|
|
|
|
|
2023-01-01 20:54:41 +03:00
|
|
|
port = qemu_xen_evtchn_pending(xendev->evtchndev);
|
2016-10-25 08:50:11 +03:00
|
|
|
if (port != xendev->local_port) {
|
2016-10-25 08:50:14 +03:00
|
|
|
xen_pv_printf(xendev, 0,
|
2016-10-25 08:50:11 +03:00
|
|
|
"xenevtchn_pending returned %d (expected %d)\n",
|
|
|
|
port, xendev->local_port);
|
|
|
|
return;
|
|
|
|
}
|
2023-01-01 20:54:41 +03:00
|
|
|
qemu_xen_evtchn_unmask(xendev->evtchndev, port);
|
2016-10-25 08:50:11 +03:00
|
|
|
|
|
|
|
if (xendev->ops->event) {
|
|
|
|
xendev->ops->event(xendev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:46 +03:00
|
|
|
void xen_pv_unbind_evtchn(struct XenLegacyDevice *xendev)
|
2016-10-25 08:50:11 +03:00
|
|
|
{
|
|
|
|
if (xendev->local_port == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-01 20:54:41 +03:00
|
|
|
qemu_set_fd_handler(qemu_xen_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
|
|
|
|
qemu_xen_evtchn_unbind(xendev->evtchndev, xendev->local_port);
|
2016-10-25 08:50:14 +03:00
|
|
|
xen_pv_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
|
2016-10-25 08:50:11 +03:00
|
|
|
xendev->local_port = -1;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:46 +03:00
|
|
|
int xen_pv_send_notify(struct XenLegacyDevice *xendev)
|
2016-10-25 08:50:11 +03:00
|
|
|
{
|
2023-01-01 20:54:41 +03:00
|
|
|
return qemu_xen_evtchn_notify(xendev->evtchndev, xendev->local_port);
|
2016-10-25 08:50:11 +03:00
|
|
|
}
|
2016-10-25 08:50:12 +03:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------- */
|
|
|
|
|
2019-01-08 17:48:46 +03:00
|
|
|
struct XenLegacyDevice *xen_pv_find_xendev(const char *type, int dom, int dev)
|
2016-10-25 08:50:12 +03:00
|
|
|
{
|
2019-01-08 17:48:46 +03:00
|
|
|
struct XenLegacyDevice *xendev;
|
2016-10-25 08:50:12 +03:00
|
|
|
|
|
|
|
QTAILQ_FOREACH(xendev, &xendevs, next) {
|
|
|
|
if (xendev->dom != dom) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (xendev->dev != dev) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(xendev->type, type) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return xendev;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* release xen backend device.
|
|
|
|
*/
|
2019-01-08 17:48:46 +03:00
|
|
|
void xen_pv_del_xendev(struct XenLegacyDevice *xendev)
|
2016-10-25 08:50:12 +03:00
|
|
|
{
|
|
|
|
if (xendev->ops->free) {
|
|
|
|
xendev->ops->free(xendev);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xendev->fe) {
|
2023-01-02 14:05:16 +03:00
|
|
|
qemu_xen_xs_unwatch(xenstore, xendev->watch);
|
2016-10-25 08:50:12 +03:00
|
|
|
g_free(xendev->fe);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xendev->evtchndev != NULL) {
|
2023-01-01 20:54:41 +03:00
|
|
|
qemu_xen_evtchn_close(xendev->evtchndev);
|
2016-10-25 08:50:12 +03:00
|
|
|
}
|
|
|
|
if (xendev->gnttabdev != NULL) {
|
2023-01-02 00:31:37 +03:00
|
|
|
qemu_xen_gnttab_close(xendev->gnttabdev);
|
2016-10-25 08:50:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QTAILQ_REMOVE(&xendevs, xendev, next);
|
2016-11-22 09:10:58 +03:00
|
|
|
|
|
|
|
qdev_unplug(&xendev->qdev, NULL);
|
2016-10-25 08:50:12 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:48:46 +03:00
|
|
|
void xen_pv_insert_xendev(struct XenLegacyDevice *xendev)
|
2016-10-25 08:50:12 +03:00
|
|
|
{
|
|
|
|
QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
|
|
|
|
}
|