linux-user: Use do_munmap for target_mmap failure
[qemu/kevin.git] / hw / s390x / s390-stattrib.c
blobc483b62a9b5f71772639fc180bdad15ecb6711cb
1 /*
2 * s390 storage attributes device
4 * Copyright 2016 IBM Corp.
5 * Author(s): Claudio Imbrenda <imbrenda@linux.vnet.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
9 * directory.
12 #include "qemu/osdep.h"
13 #include "qemu/units.h"
14 #include "migration/qemu-file.h"
15 #include "migration/register.h"
16 #include "hw/qdev-properties.h"
17 #include "hw/s390x/storage-attributes.h"
18 #include "qemu/error-report.h"
19 #include "exec/ram_addr.h"
20 #include "qapi/error.h"
21 #include "qapi/qmp/qdict.h"
23 /* 512KiB cover 2GB of guest memory */
24 #define CMMA_BLOCK_SIZE (512 * KiB)
26 #define STATTR_FLAG_EOS 0x01ULL
27 #define STATTR_FLAG_MORE 0x02ULL
28 #define STATTR_FLAG_ERROR 0x04ULL
29 #define STATTR_FLAG_DONE 0x08ULL
31 static S390StAttribState *s390_get_stattrib_device(void)
33 S390StAttribState *sas;
35 sas = S390_STATTRIB(object_resolve_path_type("", TYPE_S390_STATTRIB, NULL));
36 assert(sas);
37 return sas;
40 void s390_stattrib_init(void)
42 Object *obj;
44 obj = kvm_s390_stattrib_create();
45 if (!obj) {
46 obj = object_new(TYPE_QEMU_S390_STATTRIB);
49 object_property_add_child(qdev_get_machine(), TYPE_S390_STATTRIB,
50 obj);
51 object_unref(obj);
53 qdev_realize(DEVICE(obj), NULL, &error_fatal);
56 /* Console commands: */
58 void hmp_migrationmode(Monitor *mon, const QDict *qdict)
60 S390StAttribState *sas = s390_get_stattrib_device();
61 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
62 uint64_t what = qdict_get_int(qdict, "mode");
63 int r;
65 r = sac->set_migrationmode(sas, what);
66 if (r < 0) {
67 monitor_printf(mon, "Error: %s", strerror(-r));
71 void hmp_info_cmma(Monitor *mon, const QDict *qdict)
73 S390StAttribState *sas = s390_get_stattrib_device();
74 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
75 uint64_t addr = qdict_get_int(qdict, "addr");
76 uint64_t buflen = qdict_get_try_int(qdict, "count", 8);
77 uint8_t *vals;
78 int cx, len;
80 vals = g_try_malloc(buflen);
81 if (!vals) {
82 monitor_printf(mon, "Error: %s\n", strerror(errno));
83 return;
86 len = sac->peek_stattr(sas, addr / TARGET_PAGE_SIZE, buflen, vals);
87 if (len < 0) {
88 monitor_printf(mon, "Error: %s", strerror(-len));
89 goto out;
92 monitor_printf(mon, " CMMA attributes, "
93 "pages %" PRIu64 "+%d (0x%" PRIx64 "):\n",
94 addr / TARGET_PAGE_SIZE, len, addr & ~TARGET_PAGE_MASK);
95 for (cx = 0; cx < len; cx++) {
96 if (cx % 8 == 7) {
97 monitor_printf(mon, "%02x\n", vals[cx]);
98 } else {
99 monitor_printf(mon, "%02x", vals[cx]);
102 monitor_printf(mon, "\n");
104 out:
105 g_free(vals);
108 /* Migration support: */
110 static int cmma_load(QEMUFile *f, void *opaque, int version_id)
112 S390StAttribState *sas = S390_STATTRIB(opaque);
113 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
114 uint64_t count, cur_gfn;
115 int flags, ret = 0;
116 ram_addr_t addr;
117 uint8_t *buf;
119 while (!ret) {
120 addr = qemu_get_be64(f);
121 flags = addr & ~TARGET_PAGE_MASK;
122 addr &= TARGET_PAGE_MASK;
124 switch (flags) {
125 case STATTR_FLAG_MORE: {
126 cur_gfn = addr / TARGET_PAGE_SIZE;
127 count = qemu_get_be64(f);
128 buf = g_try_malloc(count);
129 if (!buf) {
130 error_report("cmma_load could not allocate memory");
131 ret = -ENOMEM;
132 break;
135 qemu_get_buffer(f, buf, count);
136 ret = sac->set_stattr(sas, cur_gfn, count, buf);
137 if (ret < 0) {
138 error_report("Error %d while setting storage attributes", ret);
140 g_free(buf);
141 break;
143 case STATTR_FLAG_ERROR: {
144 error_report("Storage attributes data is incomplete");
145 ret = -EINVAL;
146 break;
148 case STATTR_FLAG_DONE:
149 /* This is after the last pre-copied value has been sent, nothing
150 * more will be sent after this. Pre-copy has finished, and we
151 * are done flushing all the remaining values. Now the target
152 * system is about to take over. We synchronize the buffer to
153 * apply the actual correct values where needed.
155 sac->synchronize(sas);
156 break;
157 case STATTR_FLAG_EOS:
158 /* Normal exit */
159 return 0;
160 default:
161 error_report("Unexpected storage attribute flag data: %#x", flags);
162 ret = -EINVAL;
166 return ret;
169 static int cmma_save_setup(QEMUFile *f, void *opaque)
171 S390StAttribState *sas = S390_STATTRIB(opaque);
172 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
173 int res;
175 * Signal that we want to start a migration, thus needing PGSTE dirty
176 * tracking.
178 res = sac->set_migrationmode(sas, 1);
179 if (res) {
180 return res;
182 qemu_put_be64(f, STATTR_FLAG_EOS);
183 return 0;
186 static void cmma_state_pending(void *opaque, uint64_t *must_precopy,
187 uint64_t *can_postcopy)
189 S390StAttribState *sas = S390_STATTRIB(opaque);
190 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
191 long long res = sac->get_dirtycount(sas);
193 if (res >= 0) {
194 *must_precopy += res;
198 static int cmma_save(QEMUFile *f, void *opaque, int final)
200 S390StAttribState *sas = S390_STATTRIB(opaque);
201 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
202 uint8_t *buf;
203 int r, cx, reallen = 0, ret = 0;
204 uint32_t buflen = CMMA_BLOCK_SIZE;
205 uint64_t start_gfn = sas->migration_cur_gfn;
207 buf = g_try_malloc(buflen);
208 if (!buf) {
209 error_report("Could not allocate memory to save storage attributes");
210 return -ENOMEM;
213 while (final ? 1 : migration_rate_exceeded(f) == 0) {
214 reallen = sac->get_stattr(sas, &start_gfn, buflen, buf);
215 if (reallen < 0) {
216 g_free(buf);
217 return reallen;
220 ret = 1;
221 if (!reallen) {
222 break;
224 qemu_put_be64(f, (start_gfn << TARGET_PAGE_BITS) | STATTR_FLAG_MORE);
225 qemu_put_be64(f, reallen);
226 for (cx = 0; cx < reallen; cx++) {
227 qemu_put_byte(f, buf[cx]);
229 if (!sac->get_dirtycount(sas)) {
230 break;
234 sas->migration_cur_gfn = start_gfn + reallen;
235 g_free(buf);
236 if (final) {
237 qemu_put_be64(f, STATTR_FLAG_DONE);
239 qemu_put_be64(f, STATTR_FLAG_EOS);
241 r = qemu_file_get_error(f);
242 if (r < 0) {
243 return r;
246 return ret;
249 static int cmma_save_iterate(QEMUFile *f, void *opaque)
251 return cmma_save(f, opaque, 0);
254 static int cmma_save_complete(QEMUFile *f, void *opaque)
256 return cmma_save(f, opaque, 1);
259 static void cmma_save_cleanup(void *opaque)
261 S390StAttribState *sas = S390_STATTRIB(opaque);
262 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
263 sac->set_migrationmode(sas, 0);
266 static bool cmma_active(void *opaque)
268 S390StAttribState *sas = S390_STATTRIB(opaque);
269 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
270 return sac->get_active(sas);
273 /* QEMU object: */
275 static void qemu_s390_stattrib_instance_init(Object *obj)
279 static int qemu_s390_peek_stattr_stub(S390StAttribState *sa, uint64_t start_gfn,
280 uint32_t count, uint8_t *values)
282 return 0;
284 static void qemu_s390_synchronize_stub(S390StAttribState *sa)
287 static int qemu_s390_get_stattr_stub(S390StAttribState *sa, uint64_t *start_gfn,
288 uint32_t count, uint8_t *values)
290 return 0;
292 static long long qemu_s390_get_dirtycount_stub(S390StAttribState *sa)
294 return 0;
296 static int qemu_s390_set_migrationmode_stub(S390StAttribState *sa, bool value)
298 return 0;
301 static int qemu_s390_get_active(S390StAttribState *sa)
303 return sa->migration_enabled;
306 static void qemu_s390_stattrib_class_init(ObjectClass *oc, void *data)
308 S390StAttribClass *sa_cl = S390_STATTRIB_CLASS(oc);
309 DeviceClass *dc = DEVICE_CLASS(oc);
311 sa_cl->synchronize = qemu_s390_synchronize_stub;
312 sa_cl->get_stattr = qemu_s390_get_stattr_stub;
313 sa_cl->set_stattr = qemu_s390_peek_stattr_stub;
314 sa_cl->peek_stattr = qemu_s390_peek_stattr_stub;
315 sa_cl->set_migrationmode = qemu_s390_set_migrationmode_stub;
316 sa_cl->get_dirtycount = qemu_s390_get_dirtycount_stub;
317 sa_cl->get_active = qemu_s390_get_active;
319 /* Reason: Can only be instantiated one time (internally) */
320 dc->user_creatable = false;
323 static const TypeInfo qemu_s390_stattrib_info = {
324 .name = TYPE_QEMU_S390_STATTRIB,
325 .parent = TYPE_S390_STATTRIB,
326 .instance_init = qemu_s390_stattrib_instance_init,
327 .instance_size = sizeof(QEMUS390StAttribState),
328 .class_init = qemu_s390_stattrib_class_init,
329 .class_size = sizeof(S390StAttribClass),
332 /* Generic abstract object: */
334 static SaveVMHandlers savevm_s390_stattrib_handlers = {
335 .save_setup = cmma_save_setup,
336 .save_live_iterate = cmma_save_iterate,
337 .save_live_complete_precopy = cmma_save_complete,
338 .state_pending_exact = cmma_state_pending,
339 .state_pending_estimate = cmma_state_pending,
340 .save_cleanup = cmma_save_cleanup,
341 .load_state = cmma_load,
342 .is_active = cmma_active,
345 static void s390_stattrib_realize(DeviceState *dev, Error **errp)
347 bool ambiguous = false;
349 object_resolve_path_type("", TYPE_S390_STATTRIB, &ambiguous);
350 if (ambiguous) {
351 error_setg(errp, "storage_attributes device already exists");
352 return;
355 register_savevm_live(TYPE_S390_STATTRIB, 0, 0,
356 &savevm_s390_stattrib_handlers, dev);
359 static Property s390_stattrib_props[] = {
360 DEFINE_PROP_BOOL("migration-enabled", S390StAttribState, migration_enabled, true),
361 DEFINE_PROP_END_OF_LIST(),
364 static void s390_stattrib_class_init(ObjectClass *oc, void *data)
366 DeviceClass *dc = DEVICE_CLASS(oc);
368 dc->hotpluggable = false;
369 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
370 dc->realize = s390_stattrib_realize;
371 device_class_set_props(dc, s390_stattrib_props);
374 static void s390_stattrib_instance_init(Object *obj)
376 S390StAttribState *sas = S390_STATTRIB(obj);
378 sas->migration_cur_gfn = 0;
381 static const TypeInfo s390_stattrib_info = {
382 .name = TYPE_S390_STATTRIB,
383 .parent = TYPE_DEVICE,
384 .instance_init = s390_stattrib_instance_init,
385 .instance_size = sizeof(S390StAttribState),
386 .class_init = s390_stattrib_class_init,
387 .class_size = sizeof(S390StAttribClass),
388 .abstract = true,
391 static void s390_stattrib_register_types(void)
393 type_register_static(&s390_stattrib_info);
394 type_register_static(&qemu_s390_stattrib_info);
397 type_init(s390_stattrib_register_types)