target/rx: CPU definitions
[qemu/ar7.git] / backends / dbus-vmstate.c
blobcc594a722e061d984a23a4a06060d0637a404f99
1 /*
2 * QEMU dbus-vmstate
4 * Copyright (C) 2019 Red Hat Inc
6 * Authors:
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/units.h"
15 #include "qemu/dbus.h"
16 #include "qemu/error-report.h"
17 #include "qapi/error.h"
18 #include "qom/object_interfaces.h"
19 #include "qapi/qmp/qerror.h"
20 #include "migration/vmstate.h"
21 #include "trace.h"
23 typedef struct DBusVMState DBusVMState;
24 typedef struct DBusVMStateClass DBusVMStateClass;
26 #define TYPE_DBUS_VMSTATE "dbus-vmstate"
27 #define DBUS_VMSTATE(obj) \
28 OBJECT_CHECK(DBusVMState, (obj), TYPE_DBUS_VMSTATE)
29 #define DBUS_VMSTATE_GET_CLASS(obj) \
30 OBJECT_GET_CLASS(DBusVMStateClass, (obj), TYPE_DBUS_VMSTATE)
31 #define DBUS_VMSTATE_CLASS(klass) \
32 OBJECT_CLASS_CHECK(DBusVMStateClass, (klass), TYPE_DBUS_VMSTATE)
34 struct DBusVMStateClass {
35 ObjectClass parent_class;
38 struct DBusVMState {
39 Object parent;
41 GDBusConnection *bus;
42 char *dbus_addr;
43 char *id_list;
45 uint32_t data_size;
46 uint8_t *data;
49 static const GDBusPropertyInfo vmstate_property_info[] = {
50 { -1, (char *) "Id", (char *) "s",
51 G_DBUS_PROPERTY_INFO_FLAGS_READABLE, NULL },
54 static const GDBusPropertyInfo * const vmstate_property_info_pointers[] = {
55 &vmstate_property_info[0],
56 NULL
59 static const GDBusInterfaceInfo vmstate1_interface_info = {
60 -1,
61 (char *) "org.qemu.VMState1",
62 (GDBusMethodInfo **) NULL,
63 (GDBusSignalInfo **) NULL,
64 (GDBusPropertyInfo **) &vmstate_property_info_pointers,
65 NULL,
68 #define DBUS_VMSTATE_SIZE_LIMIT (1 * MiB)
70 static GHashTable *
71 get_id_list_set(DBusVMState *self)
73 g_auto(GStrv) ids = NULL;
74 g_autoptr(GHashTable) set = NULL;
75 int i;
77 if (!self->id_list) {
78 return NULL;
81 ids = g_strsplit(self->id_list, ",", -1);
82 set = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
83 for (i = 0; ids[i]; i++) {
84 g_hash_table_add(set, ids[i]);
85 ids[i] = NULL;
88 return g_steal_pointer(&set);
91 static GHashTable *
92 dbus_get_proxies(DBusVMState *self, GError **err)
94 g_autoptr(GHashTable) proxies = NULL;
95 g_autoptr(GHashTable) ids = NULL;
96 g_auto(GStrv) names = NULL;
97 Error *error = NULL;
98 size_t i;
100 ids = get_id_list_set(self);
101 proxies = g_hash_table_new_full(g_str_hash, g_str_equal,
102 g_free, g_object_unref);
104 names = qemu_dbus_get_queued_owners(self->bus, "org.qemu.VMState1", &error);
105 if (!names) {
106 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED, "%s",
107 error_get_pretty(error));
108 error_free(error);
109 return NULL;
112 for (i = 0; names[i]; i++) {
113 g_autoptr(GDBusProxy) proxy = NULL;
114 g_autoptr(GVariant) result = NULL;
115 g_autofree char *id = NULL;
116 size_t size;
118 proxy = g_dbus_proxy_new_sync(self->bus, G_DBUS_PROXY_FLAGS_NONE,
119 (GDBusInterfaceInfo *) &vmstate1_interface_info,
120 names[i],
121 "/org/qemu/VMState1",
122 "org.qemu.VMState1",
123 NULL, err);
124 if (!proxy) {
125 return NULL;
128 result = g_dbus_proxy_get_cached_property(proxy, "Id");
129 if (!result) {
130 g_set_error_literal(err, G_IO_ERROR, G_IO_ERROR_FAILED,
131 "VMState Id property is missing.");
132 return NULL;
135 id = g_variant_dup_string(result, &size);
136 if (ids && !g_hash_table_remove(ids, id)) {
137 g_clear_pointer(&id, g_free);
138 g_clear_object(&proxy);
139 continue;
141 if (size == 0 || size >= 256) {
142 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
143 "VMState Id '%s' is invalid.", id);
144 return NULL;
147 if (!g_hash_table_insert(proxies, id, proxy)) {
148 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
149 "Duplicated VMState Id '%s'", id);
150 return NULL;
152 id = NULL;
153 proxy = NULL;
155 g_clear_pointer(&result, g_variant_unref);
158 if (ids) {
159 g_autofree char **left = NULL;
161 left = (char **)g_hash_table_get_keys_as_array(ids, NULL);
162 if (*left) {
163 g_autofree char *leftids = g_strjoinv(",", left);
164 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
165 "Required VMState Id are missing: %s", leftids);
166 return NULL;
170 return g_steal_pointer(&proxies);
173 static int
174 dbus_load_state_proxy(GDBusProxy *proxy, const uint8_t *data, size_t size)
176 g_autoptr(GError) err = NULL;
177 g_autoptr(GVariant) result = NULL;
178 g_autoptr(GVariant) value = NULL;
180 value = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
181 data, size, sizeof(char));
182 result = g_dbus_proxy_call_sync(proxy, "Load",
183 g_variant_new("(@ay)",
184 g_steal_pointer(&value)),
185 G_DBUS_CALL_FLAGS_NO_AUTO_START,
186 -1, NULL, &err);
187 if (!result) {
188 error_report("%s: Failed to Load: %s", __func__, err->message);
189 return -1;
192 return 0;
195 static int dbus_vmstate_post_load(void *opaque, int version_id)
197 DBusVMState *self = DBUS_VMSTATE(opaque);
198 g_autoptr(GInputStream) m = NULL;
199 g_autoptr(GDataInputStream) s = NULL;
200 g_autoptr(GError) err = NULL;
201 g_autoptr(GHashTable) proxies = NULL;
202 uint32_t nelem;
204 trace_dbus_vmstate_post_load(version_id);
206 proxies = dbus_get_proxies(self, &err);
207 if (!proxies) {
208 error_report("%s: Failed to get proxies: %s", __func__, err->message);
209 return -1;
212 m = g_memory_input_stream_new_from_data(self->data, self->data_size, NULL);
213 s = g_data_input_stream_new(m);
214 g_data_input_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
216 nelem = g_data_input_stream_read_uint32(s, NULL, &err);
217 if (err) {
218 goto error;
221 while (nelem > 0) {
222 GDBusProxy *proxy = NULL;
223 uint32_t len;
224 gsize bytes_read, avail;
225 char id[256];
227 len = g_data_input_stream_read_uint32(s, NULL, &err);
228 if (err) {
229 goto error;
231 if (len >= 256) {
232 error_report("%s: Invalid DBus vmstate proxy name %u",
233 __func__, len);
234 return -1;
236 if (!g_input_stream_read_all(G_INPUT_STREAM(s), id, len,
237 &bytes_read, NULL, &err)) {
238 goto error;
240 g_return_val_if_fail(bytes_read == len, -1);
241 id[len] = 0;
243 trace_dbus_vmstate_loading(id);
245 proxy = g_hash_table_lookup(proxies, id);
246 if (!proxy) {
247 error_report("%s: Failed to find proxy Id '%s'", __func__, id);
248 return -1;
251 len = g_data_input_stream_read_uint32(s, NULL, &err);
252 avail = g_buffered_input_stream_get_available(
253 G_BUFFERED_INPUT_STREAM(s));
255 if (len > DBUS_VMSTATE_SIZE_LIMIT || len > avail) {
256 error_report("%s: Invalid vmstate size: %u", __func__, len);
257 return -1;
260 if (dbus_load_state_proxy(proxy,
261 g_buffered_input_stream_peek_buffer(G_BUFFERED_INPUT_STREAM(s),
262 NULL),
263 len) < 0) {
264 error_report("%s: Failed to restore Id '%s'", __func__, id);
265 return -1;
268 if (!g_seekable_seek(G_SEEKABLE(s), len, G_SEEK_CUR, NULL, &err)) {
269 goto error;
272 nelem -= 1;
275 return 0;
277 error:
278 error_report("%s: Failed to read from stream: %s", __func__, err->message);
279 return -1;
282 static void
283 dbus_save_state_proxy(gpointer key,
284 gpointer value,
285 gpointer user_data)
287 GDataOutputStream *s = user_data;
288 const char *id = key;
289 GDBusProxy *proxy = value;
290 g_autoptr(GVariant) result = NULL;
291 g_autoptr(GVariant) child = NULL;
292 g_autoptr(GError) err = NULL;
293 const uint8_t *data;
294 gsize size;
296 trace_dbus_vmstate_saving(id);
298 result = g_dbus_proxy_call_sync(proxy, "Save",
299 NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START,
300 -1, NULL, &err);
301 if (!result) {
302 error_report("%s: Failed to Save: %s", __func__, err->message);
303 return;
306 child = g_variant_get_child_value(result, 0);
307 data = g_variant_get_fixed_array(child, &size, sizeof(char));
308 if (!data) {
309 error_report("%s: Failed to Save: not a byte array", __func__);
310 return;
312 if (size > DBUS_VMSTATE_SIZE_LIMIT) {
313 error_report("%s: Too large vmstate data to save: %zu",
314 __func__, (size_t)size);
315 return;
318 if (!g_data_output_stream_put_uint32(s, strlen(id), NULL, &err) ||
319 !g_data_output_stream_put_string(s, id, NULL, &err) ||
320 !g_data_output_stream_put_uint32(s, size, NULL, &err) ||
321 !g_output_stream_write_all(G_OUTPUT_STREAM(s),
322 data, size, NULL, NULL, &err)) {
323 error_report("%s: Failed to write to stream: %s",
324 __func__, err->message);
328 static int dbus_vmstate_pre_save(void *opaque)
330 DBusVMState *self = DBUS_VMSTATE(opaque);
331 g_autoptr(GOutputStream) m = NULL;
332 g_autoptr(GDataOutputStream) s = NULL;
333 g_autoptr(GHashTable) proxies = NULL;
334 g_autoptr(GError) err = NULL;
336 trace_dbus_vmstate_pre_save();
338 proxies = dbus_get_proxies(self, &err);
339 if (!proxies) {
340 error_report("%s: Failed to get proxies: %s", __func__, err->message);
341 return -1;
344 m = g_memory_output_stream_new_resizable();
345 s = g_data_output_stream_new(m);
346 g_data_output_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
348 if (!g_data_output_stream_put_uint32(s, g_hash_table_size(proxies),
349 NULL, &err)) {
350 error_report("%s: Failed to write to stream: %s",
351 __func__, err->message);
352 return -1;
355 g_hash_table_foreach(proxies, dbus_save_state_proxy, s);
357 if (g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m))
358 > UINT32_MAX) {
359 error_report("%s: DBus vmstate buffer is too large", __func__);
360 return -1;
363 if (!g_output_stream_close(G_OUTPUT_STREAM(m), NULL, &err)) {
364 error_report("%s: Failed to close stream: %s", __func__, err->message);
365 return -1;
368 g_free(self->data);
369 self->data_size =
370 g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m));
371 self->data =
372 g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(m));
374 return 0;
377 static const VMStateDescription dbus_vmstate = {
378 .name = TYPE_DBUS_VMSTATE,
379 .version_id = 0,
380 .pre_save = dbus_vmstate_pre_save,
381 .post_load = dbus_vmstate_post_load,
382 .fields = (VMStateField[]) {
383 VMSTATE_UINT32(data_size, DBusVMState),
384 VMSTATE_VBUFFER_ALLOC_UINT32(data, DBusVMState, 0, 0, data_size),
385 VMSTATE_END_OF_LIST()
389 static void
390 dbus_vmstate_complete(UserCreatable *uc, Error **errp)
392 DBusVMState *self = DBUS_VMSTATE(uc);
393 g_autoptr(GError) err = NULL;
395 if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE, NULL)) {
396 error_setg(errp, "There is already an instance of %s",
397 TYPE_DBUS_VMSTATE);
398 return;
401 if (!self->dbus_addr) {
402 error_setg(errp, QERR_MISSING_PARAMETER, "addr");
403 return;
406 self->bus = g_dbus_connection_new_for_address_sync(self->dbus_addr,
407 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
408 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
409 NULL, NULL, &err);
410 if (err) {
411 error_setg(errp, "failed to connect to DBus: '%s'", err->message);
412 return;
415 if (vmstate_register(VMSTATE_IF(self), VMSTATE_INSTANCE_ID_ANY,
416 &dbus_vmstate, self) < 0) {
417 error_setg(errp, "Failed to register vmstate");
421 static void
422 dbus_vmstate_finalize(Object *o)
424 DBusVMState *self = DBUS_VMSTATE(o);
426 vmstate_unregister(VMSTATE_IF(self), &dbus_vmstate, self);
428 g_clear_object(&self->bus);
429 g_free(self->dbus_addr);
430 g_free(self->id_list);
431 g_free(self->data);
434 static char *
435 get_dbus_addr(Object *o, Error **errp)
437 DBusVMState *self = DBUS_VMSTATE(o);
439 return g_strdup(self->dbus_addr);
442 static void
443 set_dbus_addr(Object *o, const char *str, Error **errp)
445 DBusVMState *self = DBUS_VMSTATE(o);
447 g_free(self->dbus_addr);
448 self->dbus_addr = g_strdup(str);
451 static char *
452 get_id_list(Object *o, Error **errp)
454 DBusVMState *self = DBUS_VMSTATE(o);
456 return g_strdup(self->id_list);
459 static void
460 set_id_list(Object *o, const char *str, Error **errp)
462 DBusVMState *self = DBUS_VMSTATE(o);
464 g_free(self->id_list);
465 self->id_list = g_strdup(str);
468 static char *
469 dbus_vmstate_get_id(VMStateIf *vmif)
471 return g_strdup(TYPE_DBUS_VMSTATE);
474 static void
475 dbus_vmstate_class_init(ObjectClass *oc, void *data)
477 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
478 VMStateIfClass *vc = VMSTATE_IF_CLASS(oc);
480 ucc->complete = dbus_vmstate_complete;
481 vc->get_id = dbus_vmstate_get_id;
483 object_class_property_add_str(oc, "addr",
484 get_dbus_addr, set_dbus_addr,
485 &error_abort);
486 object_class_property_add_str(oc, "id-list",
487 get_id_list, set_id_list,
488 &error_abort);
491 static const TypeInfo dbus_vmstate_info = {
492 .name = TYPE_DBUS_VMSTATE,
493 .parent = TYPE_OBJECT,
494 .instance_size = sizeof(DBusVMState),
495 .instance_finalize = dbus_vmstate_finalize,
496 .class_size = sizeof(DBusVMStateClass),
497 .class_init = dbus_vmstate_class_init,
498 .interfaces = (InterfaceInfo[]) {
499 { TYPE_USER_CREATABLE },
500 { TYPE_VMSTATE_IF },
505 static void
506 register_types(void)
508 type_register_static(&dbus_vmstate_info);
511 type_init(register_types);