Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / remote / remote-obj.c
blobdc27cc8da1f3666013c72a795c301abeb51c13bc
1 /*
2 * Copyright © 2020, 2021 Oracle and/or its affiliates.
4 * This work is licensed under the terms of the GNU GPL-v2, version 2 or later.
6 * See the COPYING file in the top-level directory.
8 */
10 #include "qemu/osdep.h"
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/qdev-core.h"
17 #include "hw/remote/machine.h"
18 #include "io/channel-util.h"
19 #include "qapi/error.h"
20 #include "sysemu/sysemu.h"
21 #include "hw/pci/pci.h"
22 #include "qemu/sockets.h"
23 #include "monitor/monitor.h"
25 #define TYPE_REMOTE_OBJECT "x-remote-object"
26 OBJECT_DECLARE_TYPE(RemoteObject, RemoteObjectClass, REMOTE_OBJECT)
28 struct RemoteObjectClass {
29 ObjectClass parent_class;
31 unsigned int nr_devs;
32 unsigned int max_devs;
35 struct RemoteObject {
36 /* private */
37 Object parent;
39 Notifier machine_done;
41 int32_t fd;
42 char *devid;
44 QIOChannel *ioc;
46 DeviceState *dev;
47 DeviceListener listener;
50 static void remote_object_set_fd(Object *obj, const char *str, Error **errp)
52 ERRP_GUARD();
53 RemoteObject *o = REMOTE_OBJECT(obj);
54 int fd = -1;
56 fd = monitor_fd_param(monitor_cur(), str, errp);
57 if (fd == -1) {
58 error_prepend(errp, "Could not parse remote object fd %s:", str);
59 return;
62 if (!fd_is_socket(fd)) {
63 error_setg(errp, "File descriptor '%s' is not a socket", str);
64 close(fd);
65 return;
68 o->fd = fd;
71 static void remote_object_set_devid(Object *obj, const char *str, Error **errp)
73 RemoteObject *o = REMOTE_OBJECT(obj);
75 g_free(o->devid);
77 o->devid = g_strdup(str);
80 static void remote_object_unrealize_listener(DeviceListener *listener,
81 DeviceState *dev)
83 RemoteObject *o = container_of(listener, RemoteObject, listener);
85 if (o->dev == dev) {
86 object_unref(OBJECT(o));
90 static void remote_object_machine_done(Notifier *notifier, void *data)
92 RemoteObject *o = container_of(notifier, RemoteObject, machine_done);
93 DeviceState *dev = NULL;
94 QIOChannel *ioc = NULL;
95 Coroutine *co = NULL;
96 RemoteCommDev *comdev = NULL;
97 Error *err = NULL;
99 dev = qdev_find_recursive(sysbus_get_default(), o->devid);
100 if (!dev || !object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
101 error_report("%s is not a PCI device", o->devid);
102 return;
105 ioc = qio_channel_new_fd(o->fd, &err);
106 if (!ioc) {
107 error_report_err(err);
108 return;
110 qio_channel_set_blocking(ioc, false, NULL);
112 o->dev = dev;
114 o->listener.unrealize = remote_object_unrealize_listener;
115 device_listener_register(&o->listener);
117 /* co-routine should free this. */
118 comdev = g_new0(RemoteCommDev, 1);
119 *comdev = (RemoteCommDev) {
120 .ioc = ioc,
121 .dev = PCI_DEVICE(dev),
124 co = qemu_coroutine_create(mpqemu_remote_msg_loop_co, comdev);
125 qemu_coroutine_enter(co);
128 static void remote_object_init(Object *obj)
130 RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
131 RemoteObject *o = REMOTE_OBJECT(obj);
133 if (k->nr_devs >= k->max_devs) {
134 error_report("Reached maximum number of devices: %u", k->max_devs);
135 return;
138 o->ioc = NULL;
139 o->fd = -1;
140 o->devid = NULL;
142 k->nr_devs++;
144 o->machine_done.notify = remote_object_machine_done;
145 qemu_add_machine_init_done_notifier(&o->machine_done);
148 static void remote_object_finalize(Object *obj)
150 RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
151 RemoteObject *o = REMOTE_OBJECT(obj);
153 device_listener_unregister(&o->listener);
155 if (o->ioc) {
156 qio_channel_shutdown(o->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
157 qio_channel_close(o->ioc, NULL);
160 object_unref(OBJECT(o->ioc));
162 k->nr_devs--;
163 g_free(o->devid);
166 static void remote_object_class_init(ObjectClass *klass, void *data)
168 RemoteObjectClass *k = REMOTE_OBJECT_CLASS(klass);
171 * Limit number of supported devices to 1. This is done to avoid devices
172 * from one VM accessing the RAM of another VM. This is done until we
173 * start using separate address spaces for individual devices.
175 k->max_devs = 1;
176 k->nr_devs = 0;
178 object_class_property_add_str(klass, "fd", NULL, remote_object_set_fd);
179 object_class_property_add_str(klass, "devid", NULL,
180 remote_object_set_devid);
183 static const TypeInfo remote_object_info = {
184 .name = TYPE_REMOTE_OBJECT,
185 .parent = TYPE_OBJECT,
186 .instance_size = sizeof(RemoteObject),
187 .instance_init = remote_object_init,
188 .instance_finalize = remote_object_finalize,
189 .class_size = sizeof(RemoteObjectClass),
190 .class_init = remote_object_class_init,
191 .interfaces = (InterfaceInfo[]) {
192 { TYPE_USER_CREATABLE },
197 static void register_types(void)
199 type_register_static(&remote_object_info);
202 type_init(register_types);