Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/remote/remote-obj.c
Line
Count
Source
1
/*
2
 * Copyright © 2020, 2021 Oracle and/or its affiliates.
3
 *
4
 * This work is licensed under the terms of the GNU GPL-v2, version 2 or later.
5
 *
6
 * See the COPYING file in the top-level directory.
7
 *
8
 */
9
10
#include "qemu/osdep.h"
11
12
#include "qemu/error-report.h"
13
#include "qemu/notify.h"
14
#include "qom/object_interfaces.h"
15
#include "io/channel.h"
16
#include "hw/core/qdev.h"
17
#include "hw/remote/machine.h"
18
#include "io/channel-util.h"
19
#include "qapi/error.h"
20
#include "system/system.h"
21
#include "hw/pci/pci.h"
22
#include "qemu/sockets.h"
23
#include "monitor/monitor.h"
24
25
#define TYPE_REMOTE_OBJECT "x-remote-object"
26
OBJECT_DECLARE_TYPE(RemoteObject, RemoteObjectClass, REMOTE_OBJECT)
27
28
struct RemoteObjectClass {
29
    ObjectClass parent_class;
30
31
    unsigned int nr_devs;
32
    unsigned int max_devs;
33
};
34
35
struct RemoteObject {
36
    /* private */
37
    Object parent;
38
39
    Notifier machine_done;
40
41
    int32_t fd;
42
    char *devid;
43
44
    QIOChannel *ioc;
45
46
    DeviceState *dev;
47
    DeviceListener listener;
48
};
49
50
static void remote_object_set_fd(Object *obj, const char *str, Error **errp)
51
0
{
52
0
    ERRP_GUARD();
53
0
    RemoteObject *o = REMOTE_OBJECT(obj);
54
0
    int fd = -1;
55
56
0
    fd = monitor_fd_param(monitor_cur(), str, errp);
57
0
    if (fd == -1) {
58
0
        error_prepend(errp, "Could not parse remote object fd %s:", str);
59
0
        return;
60
0
    }
61
62
0
    if (!fd_is_socket(fd)) {
63
0
        error_setg(errp, "File descriptor '%s' is not a socket", str);
64
0
        close(fd);
65
0
        return;
66
0
    }
67
68
0
    o->fd = fd;
69
0
}
70
71
static void remote_object_set_devid(Object *obj, const char *str, Error **errp)
72
0
{
73
0
    RemoteObject *o = REMOTE_OBJECT(obj);
74
75
0
    g_free(o->devid);
76
77
0
    o->devid = g_strdup(str);
78
0
}
79
80
static void remote_object_unrealize_listener(DeviceListener *listener,
81
                                             DeviceState *dev)
82
0
{
83
0
    RemoteObject *o = container_of(listener, RemoteObject, listener);
84
85
0
    if (o->dev == dev) {
86
0
        object_unref(OBJECT(o));
87
0
    }
88
0
}
89
90
static void remote_object_machine_done(Notifier *notifier, void *data)
91
0
{
92
0
    RemoteObject *o = container_of(notifier, RemoteObject, machine_done);
93
0
    DeviceState *dev = NULL;
94
0
    QIOChannel *ioc = NULL;
95
0
    Coroutine *co = NULL;
96
0
    RemoteCommDev *comdev = NULL;
97
0
    Error *err = NULL;
98
99
0
    dev = qdev_find_recursive(sysbus_get_default(), o->devid);
100
0
    if (!dev || !object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
101
0
        error_report("%s is not a PCI device", o->devid);
102
0
        return;
103
0
    }
104
105
0
    ioc = qio_channel_new_fd(o->fd, &err);
106
0
    if (!ioc) {
107
0
        error_report_err(err);
108
0
        return;
109
0
    }
110
0
    if (!qio_channel_set_blocking(ioc, false, &err)) {
111
0
        error_report_err(err);
112
0
        object_unref(OBJECT(ioc));
113
0
        return;
114
0
    }
115
116
0
    o->dev = dev;
117
118
0
    o->listener.unrealize = remote_object_unrealize_listener;
119
0
    device_listener_register(&o->listener);
120
121
    /* co-routine should free this. */
122
0
    comdev = g_new0(RemoteCommDev, 1);
123
0
    *comdev = (RemoteCommDev) {
124
0
        .ioc = ioc,
125
0
        .dev = PCI_DEVICE(dev),
126
0
    };
127
128
0
    co = qemu_coroutine_create(mpqemu_remote_msg_loop_co, comdev);
129
0
    qemu_coroutine_enter(co);
130
0
}
131
132
static void remote_object_init(Object *obj)
133
0
{
134
0
    RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
135
0
    RemoteObject *o = REMOTE_OBJECT(obj);
136
137
0
    if (k->nr_devs >= k->max_devs) {
138
0
        error_report("Reached maximum number of devices: %u", k->max_devs);
139
0
        return;
140
0
    }
141
142
0
    o->ioc = NULL;
143
0
    o->fd = -1;
144
0
    o->devid = NULL;
145
146
0
    k->nr_devs++;
147
148
0
    o->machine_done.notify = remote_object_machine_done;
149
0
    qemu_add_machine_init_done_notifier(&o->machine_done);
150
0
}
151
152
static void remote_object_finalize(Object *obj)
153
0
{
154
0
    RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
155
0
    RemoteObject *o = REMOTE_OBJECT(obj);
156
157
0
    if (o->listener.unrealize) {
158
0
        device_listener_unregister(&o->listener);
159
0
    }
160
161
0
    if (o->ioc) {
162
0
        qio_channel_shutdown(o->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
163
0
        qio_channel_close(o->ioc, NULL);
164
0
    }
165
166
0
    object_unref(OBJECT(o->ioc));
167
168
0
    k->nr_devs--;
169
0
    g_free(o->devid);
170
0
}
171
172
static void remote_object_class_init(ObjectClass *klass, const void *data)
173
61
{
174
61
    RemoteObjectClass *k = REMOTE_OBJECT_CLASS(klass);
175
176
    /*
177
     * Limit number of supported devices to 1. This is done to avoid devices
178
     * from one VM accessing the RAM of another VM. This is done until we
179
     * start using separate address spaces for individual devices.
180
     */
181
61
    k->max_devs = 1;
182
61
    k->nr_devs = 0;
183
184
61
    object_class_property_add_str(klass, "fd", NULL, remote_object_set_fd);
185
61
    object_class_property_add_str(klass, "devid", NULL,
186
61
                                  remote_object_set_devid);
187
61
}
188
189
static const TypeInfo remote_object_info = {
190
    .name = TYPE_REMOTE_OBJECT,
191
    .parent = TYPE_OBJECT,
192
    .instance_size = sizeof(RemoteObject),
193
    .instance_init = remote_object_init,
194
    .instance_finalize = remote_object_finalize,
195
    .class_size = sizeof(RemoteObjectClass),
196
    .class_init = remote_object_class_init,
197
    .interfaces = (const InterfaceInfo[]) {
198
        { TYPE_USER_CREATABLE },
199
        { }
200
    }
201
};
202
203
static void register_types(void)
204
61
{
205
61
    type_register_static(&remote_object_info);
206
61
}
207
208
type_init(register_types);