xref: /openbmc/qemu/hw/s390x/s390-stattrib.c (revision 6c7937ec)
1 /*
2  * s390 storage attributes device
3  *
4  * Copyright 2016 IBM Corp.
5  * Author(s): Claudio Imbrenda <imbrenda@linux.vnet.ibm.com>
6  *
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.
10  */
11 
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"
22 
23 /* 512KiB cover 2GB of guest memory */
24 #define CMMA_BLOCK_SIZE  (512 * KiB)
25 
26 #define STATTR_FLAG_EOS     0x01ULL
27 #define STATTR_FLAG_MORE    0x02ULL
28 #define STATTR_FLAG_ERROR   0x04ULL
29 #define STATTR_FLAG_DONE    0x08ULL
30 
31 static S390StAttribState *s390_get_stattrib_device(void)
32 {
33     S390StAttribState *sas;
34 
35     sas = S390_STATTRIB(object_resolve_path_type("", TYPE_S390_STATTRIB, NULL));
36     assert(sas);
37     return sas;
38 }
39 
40 void s390_stattrib_init(void)
41 {
42     Object *obj;
43 
44     obj = kvm_s390_stattrib_create();
45     if (!obj) {
46         obj = object_new(TYPE_QEMU_S390_STATTRIB);
47     }
48 
49     object_property_add_child(qdev_get_machine(), TYPE_S390_STATTRIB,
50                               obj);
51     object_unref(obj);
52 
53     qdev_realize(DEVICE(obj), NULL, &error_fatal);
54 }
55 
56 /* Console commands: */
57 
58 void hmp_migrationmode(Monitor *mon, const QDict *qdict)
59 {
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;
64 
65     r = sac->set_migrationmode(sas, what);
66     if (r < 0) {
67         monitor_printf(mon, "Error: %s", strerror(-r));
68     }
69 }
70 
71 void hmp_info_cmma(Monitor *mon, const QDict *qdict)
72 {
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;
79 
80     vals = g_try_malloc(buflen);
81     if (!vals) {
82         monitor_printf(mon, "Error: %s\n", strerror(errno));
83         return;
84     }
85 
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;
90     }
91 
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]);
100         }
101     }
102     monitor_printf(mon, "\n");
103 
104 out:
105     g_free(vals);
106 }
107 
108 /* Migration support: */
109 
110 static int cmma_load(QEMUFile *f, void *opaque, int version_id)
111 {
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;
118 
119     while (!ret) {
120         addr = qemu_get_be64(f);
121         flags = addr & ~TARGET_PAGE_MASK;
122         addr &= TARGET_PAGE_MASK;
123 
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;
133             }
134 
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);
139             }
140             g_free(buf);
141             break;
142         }
143         case STATTR_FLAG_ERROR: {
144             error_report("Storage attributes data is incomplete");
145             ret = -EINVAL;
146             break;
147         }
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.
154              */
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;
163         }
164     }
165 
166     return ret;
167 }
168 
169 static int cmma_save_setup(QEMUFile *f, void *opaque)
170 {
171     S390StAttribState *sas = S390_STATTRIB(opaque);
172     S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
173     int res;
174     /*
175      * Signal that we want to start a migration, thus needing PGSTE dirty
176      * tracking.
177      */
178     res = sac->set_migrationmode(sas, 1);
179     if (res) {
180         return res;
181     }
182     qemu_put_be64(f, STATTR_FLAG_EOS);
183     return 0;
184 }
185 
186 static void cmma_state_pending(void *opaque, uint64_t *must_precopy,
187                                uint64_t *can_postcopy)
188 {
189     S390StAttribState *sas = S390_STATTRIB(opaque);
190     S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
191     long long res = sac->get_dirtycount(sas);
192 
193     if (res >= 0) {
194         *must_precopy += res;
195     }
196 }
197 
198 static int cmma_save(QEMUFile *f, void *opaque, int final)
199 {
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;
206 
207     buf = g_try_malloc(buflen);
208     if (!buf) {
209         error_report("Could not allocate memory to save storage attributes");
210         return -ENOMEM;
211     }
212 
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;
218         }
219 
220         ret = 1;
221         if (!reallen) {
222             break;
223         }
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]);
228         }
229         if (!sac->get_dirtycount(sas)) {
230             break;
231         }
232     }
233 
234     sas->migration_cur_gfn = start_gfn + reallen;
235     g_free(buf);
236     if (final) {
237         qemu_put_be64(f, STATTR_FLAG_DONE);
238     }
239     qemu_put_be64(f, STATTR_FLAG_EOS);
240 
241     r = qemu_file_get_error(f);
242     if (r < 0) {
243         return r;
244     }
245 
246     return ret;
247 }
248 
249 static int cmma_save_iterate(QEMUFile *f, void *opaque)
250 {
251     return cmma_save(f, opaque, 0);
252 }
253 
254 static int cmma_save_complete(QEMUFile *f, void *opaque)
255 {
256     return cmma_save(f, opaque, 1);
257 }
258 
259 static void cmma_save_cleanup(void *opaque)
260 {
261     S390StAttribState *sas = S390_STATTRIB(opaque);
262     S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
263     sac->set_migrationmode(sas, 0);
264 }
265 
266 static bool cmma_active(void *opaque)
267 {
268     S390StAttribState *sas = S390_STATTRIB(opaque);
269     S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas);
270     return sac->get_active(sas);
271 }
272 
273 /* QEMU object: */
274 
275 static void qemu_s390_stattrib_instance_init(Object *obj)
276 {
277 }
278 
279 static int qemu_s390_peek_stattr_stub(S390StAttribState *sa, uint64_t start_gfn,
280                                      uint32_t count, uint8_t *values)
281 {
282     return 0;
283 }
284 static void qemu_s390_synchronize_stub(S390StAttribState *sa)
285 {
286 }
287 static int qemu_s390_get_stattr_stub(S390StAttribState *sa, uint64_t *start_gfn,
288                                      uint32_t count, uint8_t *values)
289 {
290     return 0;
291 }
292 static long long qemu_s390_get_dirtycount_stub(S390StAttribState *sa)
293 {
294     return 0;
295 }
296 static int qemu_s390_set_migrationmode_stub(S390StAttribState *sa, bool value)
297 {
298     return 0;
299 }
300 
301 static int qemu_s390_get_active(S390StAttribState *sa)
302 {
303     return sa->migration_enabled;
304 }
305 
306 static void qemu_s390_stattrib_class_init(ObjectClass *oc, void *data)
307 {
308     S390StAttribClass *sa_cl = S390_STATTRIB_CLASS(oc);
309     DeviceClass *dc = DEVICE_CLASS(oc);
310 
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;
318 
319     /* Reason: Can only be instantiated one time (internally) */
320     dc->user_creatable = false;
321 }
322 
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),
330 };
331 
332 /* Generic abstract object: */
333 
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,
343 };
344 
345 static void s390_stattrib_realize(DeviceState *dev, Error **errp)
346 {
347     bool ambiguous = false;
348 
349     object_resolve_path_type("", TYPE_S390_STATTRIB, &ambiguous);
350     if (ambiguous) {
351         error_setg(errp, "storage_attributes device already exists");
352         return;
353     }
354 
355     register_savevm_live(TYPE_S390_STATTRIB, 0, 0,
356                          &savevm_s390_stattrib_handlers, dev);
357 }
358 
359 static Property s390_stattrib_props[] = {
360     DEFINE_PROP_BOOL("migration-enabled", S390StAttribState, migration_enabled, true),
361     DEFINE_PROP_END_OF_LIST(),
362 };
363 
364 static void s390_stattrib_class_init(ObjectClass *oc, void *data)
365 {
366     DeviceClass *dc = DEVICE_CLASS(oc);
367 
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);
372 }
373 
374 static void s390_stattrib_instance_init(Object *obj)
375 {
376     S390StAttribState *sas = S390_STATTRIB(obj);
377 
378     sas->migration_cur_gfn = 0;
379 }
380 
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,
389 };
390 
391 static void s390_stattrib_register_types(void)
392 {
393     type_register_static(&s390_stattrib_info);
394     type_register_static(&qemu_s390_stattrib_info);
395 }
396 
397 type_init(s390_stattrib_register_types)
398