xref: /openbmc/qemu/hw/remote/remote-obj.c (revision b14df228)
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 "hw/qdev-core.h"
16 #include "io/channel.h"
17 #include "hw/qdev-core.h"
18 #include "hw/remote/machine.h"
19 #include "io/channel-util.h"
20 #include "qapi/error.h"
21 #include "sysemu/sysemu.h"
22 #include "hw/pci/pci.h"
23 #include "qemu/sockets.h"
24 #include "monitor/monitor.h"
25 
26 #define TYPE_REMOTE_OBJECT "x-remote-object"
27 OBJECT_DECLARE_TYPE(RemoteObject, RemoteObjectClass, REMOTE_OBJECT)
28 
29 struct RemoteObjectClass {
30     ObjectClass parent_class;
31 
32     unsigned int nr_devs;
33     unsigned int max_devs;
34 };
35 
36 struct RemoteObject {
37     /* private */
38     Object parent;
39 
40     Notifier machine_done;
41 
42     int32_t fd;
43     char *devid;
44 
45     QIOChannel *ioc;
46 
47     DeviceState *dev;
48     DeviceListener listener;
49 };
50 
51 static void remote_object_set_fd(Object *obj, const char *str, Error **errp)
52 {
53     RemoteObject *o = REMOTE_OBJECT(obj);
54     int fd = -1;
55 
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;
60     }
61 
62     if (!fd_is_socket(fd)) {
63         error_setg(errp, "File descriptor '%s' is not a socket", str);
64         close(fd);
65         return;
66     }
67 
68     o->fd = fd;
69 }
70 
71 static void remote_object_set_devid(Object *obj, const char *str, Error **errp)
72 {
73     RemoteObject *o = REMOTE_OBJECT(obj);
74 
75     g_free(o->devid);
76 
77     o->devid = g_strdup(str);
78 }
79 
80 static void remote_object_unrealize_listener(DeviceListener *listener,
81                                              DeviceState *dev)
82 {
83     RemoteObject *o = container_of(listener, RemoteObject, listener);
84 
85     if (o->dev == dev) {
86         object_unref(OBJECT(o));
87     }
88 }
89 
90 static void remote_object_machine_done(Notifier *notifier, void *data)
91 {
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;
98 
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;
103     }
104 
105     ioc = qio_channel_new_fd(o->fd, &err);
106     if (!ioc) {
107         error_report_err(err);
108         return;
109     }
110     qio_channel_set_blocking(ioc, false, NULL);
111 
112     o->dev = dev;
113 
114     o->listener.unrealize = remote_object_unrealize_listener;
115     device_listener_register(&o->listener);
116 
117     /* co-routine should free this. */
118     comdev = g_new0(RemoteCommDev, 1);
119     *comdev = (RemoteCommDev) {
120         .ioc = ioc,
121         .dev = PCI_DEVICE(dev),
122     };
123 
124     co = qemu_coroutine_create(mpqemu_remote_msg_loop_co, comdev);
125     qemu_coroutine_enter(co);
126 }
127 
128 static void remote_object_init(Object *obj)
129 {
130     RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
131     RemoteObject *o = REMOTE_OBJECT(obj);
132 
133     if (k->nr_devs >= k->max_devs) {
134         error_report("Reached maximum number of devices: %u", k->max_devs);
135         return;
136     }
137 
138     o->ioc = NULL;
139     o->fd = -1;
140     o->devid = NULL;
141 
142     k->nr_devs++;
143 
144     o->machine_done.notify = remote_object_machine_done;
145     qemu_add_machine_init_done_notifier(&o->machine_done);
146 }
147 
148 static void remote_object_finalize(Object *obj)
149 {
150     RemoteObjectClass *k = REMOTE_OBJECT_GET_CLASS(obj);
151     RemoteObject *o = REMOTE_OBJECT(obj);
152 
153     device_listener_unregister(&o->listener);
154 
155     if (o->ioc) {
156         qio_channel_shutdown(o->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
157         qio_channel_close(o->ioc, NULL);
158     }
159 
160     object_unref(OBJECT(o->ioc));
161 
162     k->nr_devs--;
163     g_free(o->devid);
164 }
165 
166 static void remote_object_class_init(ObjectClass *klass, void *data)
167 {
168     RemoteObjectClass *k = REMOTE_OBJECT_CLASS(klass);
169 
170     /*
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.
174      */
175     k->max_devs = 1;
176     k->nr_devs = 0;
177 
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);
181 }
182 
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 },
193         { }
194     }
195 };
196 
197 static void register_types(void)
198 {
199     type_register_static(&remote_object_info);
200 }
201 
202 type_init(register_types);
203