]>
Commit | Line | Data |
---|---|---|
903fd80b CI |
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 "hw/boards.h" | |
14 | #include "migration/qemu-file.h" | |
15 | #include "migration/register.h" | |
16 | #include "hw/s390x/storage-attributes.h" | |
17 | #include "qemu/error-report.h" | |
18 | #include "sysemu/kvm.h" | |
19 | #include "exec/ram_addr.h" | |
20 | #include "qapi/error.h" | |
21 | ||
22 | #define CMMA_BLOCK_SIZE (1 << 10) | |
23 | ||
24 | #define STATTR_FLAG_EOS 0x01ULL | |
25 | #define STATTR_FLAG_MORE 0x02ULL | |
26 | #define STATTR_FLAG_ERROR 0x04ULL | |
27 | #define STATTR_FLAG_DONE 0x08ULL | |
28 | ||
29 | void s390_stattrib_init(void) | |
30 | { | |
31 | Object *obj; | |
32 | ||
33 | obj = kvm_s390_stattrib_create(); | |
34 | if (!obj) { | |
35 | obj = object_new(TYPE_QEMU_S390_STATTRIB); | |
36 | } | |
37 | ||
38 | object_property_add_child(qdev_get_machine(), TYPE_S390_STATTRIB, | |
39 | obj, NULL); | |
40 | object_unref(obj); | |
41 | ||
42 | qdev_init_nofail(DEVICE(obj)); | |
43 | } | |
44 | ||
45 | /* Migration support: */ | |
46 | ||
47 | static int cmma_load(QEMUFile *f, void *opaque, int version_id) | |
48 | { | |
49 | S390StAttribState *sas = S390_STATTRIB(opaque); | |
50 | S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); | |
51 | uint64_t count, cur_gfn; | |
52 | int flags, ret = 0; | |
53 | ram_addr_t addr; | |
54 | uint8_t *buf; | |
55 | ||
56 | while (!ret) { | |
57 | addr = qemu_get_be64(f); | |
58 | flags = addr & ~TARGET_PAGE_MASK; | |
59 | addr &= TARGET_PAGE_MASK; | |
60 | ||
61 | switch (flags) { | |
62 | case STATTR_FLAG_MORE: { | |
63 | cur_gfn = addr / TARGET_PAGE_SIZE; | |
64 | count = qemu_get_be64(f); | |
65 | buf = g_try_malloc(count); | |
66 | if (!buf) { | |
67 | error_report("cmma_load could not allocate memory"); | |
68 | ret = -ENOMEM; | |
69 | break; | |
70 | } | |
71 | ||
72 | qemu_get_buffer(f, buf, count); | |
73 | ret = sac->set_stattr(sas, cur_gfn, count, buf); | |
74 | if (ret < 0) { | |
75 | error_report("Error %d while setting storage attributes", ret); | |
76 | } | |
77 | g_free(buf); | |
78 | break; | |
79 | } | |
80 | case STATTR_FLAG_ERROR: { | |
81 | error_report("Storage attributes data is incomplete"); | |
82 | ret = -EINVAL; | |
83 | break; | |
84 | } | |
85 | case STATTR_FLAG_DONE: | |
86 | /* This is after the last pre-copied value has been sent, nothing | |
87 | * more will be sent after this. Pre-copy has finished, and we | |
88 | * are done flushing all the remaining values. Now the target | |
89 | * system is about to take over. We synchronize the buffer to | |
90 | * apply the actual correct values where needed. | |
91 | */ | |
92 | sac->synchronize(sas); | |
93 | break; | |
94 | case STATTR_FLAG_EOS: | |
95 | /* Normal exit */ | |
96 | return 0; | |
97 | default: | |
98 | error_report("Unexpected storage attribute flag data: %#x", flags); | |
99 | ret = -EINVAL; | |
100 | } | |
101 | } | |
102 | ||
103 | return ret; | |
104 | } | |
105 | ||
106 | static int cmma_save_setup(QEMUFile *f, void *opaque) | |
107 | { | |
108 | S390StAttribState *sas = S390_STATTRIB(opaque); | |
109 | S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); | |
110 | int res; | |
111 | /* | |
112 | * Signal that we want to start a migration, thus needing PGSTE dirty | |
113 | * tracking. | |
114 | */ | |
115 | res = sac->set_migrationmode(sas, 1); | |
116 | if (res) { | |
117 | return res; | |
118 | } | |
119 | qemu_put_be64(f, STATTR_FLAG_EOS); | |
120 | return 0; | |
121 | } | |
122 | ||
123 | static void cmma_save_pending(QEMUFile *f, void *opaque, uint64_t max_size, | |
124 | uint64_t *non_postcopiable_pending, | |
125 | uint64_t *postcopiable_pending) | |
126 | { | |
127 | S390StAttribState *sas = S390_STATTRIB(opaque); | |
128 | S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); | |
129 | long long res = sac->get_dirtycount(sas); | |
130 | ||
131 | if (res >= 0) { | |
132 | *non_postcopiable_pending += res; | |
133 | } | |
134 | } | |
135 | ||
136 | static int cmma_save(QEMUFile *f, void *opaque, int final) | |
137 | { | |
138 | S390StAttribState *sas = S390_STATTRIB(opaque); | |
139 | S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); | |
140 | uint8_t *buf; | |
141 | int r, cx, reallen = 0, ret = 0; | |
142 | uint32_t buflen = 1 << 19; /* 512kB cover 2GB of guest memory */ | |
143 | uint64_t start_gfn = sas->migration_cur_gfn; | |
144 | ||
145 | buf = g_try_malloc(buflen); | |
146 | if (!buf) { | |
147 | error_report("Could not allocate memory to save storage attributes"); | |
148 | return -ENOMEM; | |
149 | } | |
150 | ||
151 | while (final ? 1 : qemu_file_rate_limit(f) == 0) { | |
152 | reallen = sac->get_stattr(sas, &start_gfn, buflen, buf); | |
153 | if (reallen < 0) { | |
154 | g_free(buf); | |
155 | return reallen; | |
156 | } | |
157 | ||
158 | ret = 1; | |
159 | if (!reallen) { | |
160 | break; | |
161 | } | |
162 | qemu_put_be64(f, (start_gfn << TARGET_PAGE_BITS) | STATTR_FLAG_MORE); | |
163 | qemu_put_be64(f, reallen); | |
164 | for (cx = 0; cx < reallen; cx++) { | |
165 | qemu_put_byte(f, buf[cx]); | |
166 | } | |
167 | if (!sac->get_dirtycount(sas)) { | |
168 | break; | |
169 | } | |
170 | } | |
171 | ||
172 | sas->migration_cur_gfn = start_gfn + reallen; | |
173 | g_free(buf); | |
174 | if (final) { | |
175 | qemu_put_be64(f, STATTR_FLAG_DONE); | |
176 | } | |
177 | qemu_put_be64(f, STATTR_FLAG_EOS); | |
178 | ||
179 | r = qemu_file_get_error(f); | |
180 | if (r < 0) { | |
181 | return r; | |
182 | } | |
183 | ||
184 | return ret; | |
185 | } | |
186 | ||
187 | static int cmma_save_iterate(QEMUFile *f, void *opaque) | |
188 | { | |
189 | return cmma_save(f, opaque, 0); | |
190 | } | |
191 | ||
192 | static int cmma_save_complete(QEMUFile *f, void *opaque) | |
193 | { | |
194 | return cmma_save(f, opaque, 1); | |
195 | } | |
196 | ||
197 | static void cmma_save_cleanup(void *opaque) | |
198 | { | |
199 | S390StAttribState *sas = S390_STATTRIB(opaque); | |
200 | S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); | |
201 | sac->set_migrationmode(sas, 0); | |
202 | } | |
203 | ||
204 | static bool cmma_active(void *opaque) | |
205 | { | |
206 | S390StAttribState *sas = S390_STATTRIB(opaque); | |
207 | S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); | |
208 | return sac->get_active(sas); | |
209 | } | |
210 | ||
211 | /* QEMU object: */ | |
212 | ||
213 | static void qemu_s390_stattrib_instance_init(Object *obj) | |
214 | { | |
215 | } | |
216 | ||
217 | static int qemu_s390_peek_stattr_stub(S390StAttribState *sa, uint64_t start_gfn, | |
218 | uint32_t count, uint8_t *values) | |
219 | { | |
220 | return 0; | |
221 | } | |
222 | static void qemu_s390_synchronize_stub(S390StAttribState *sa) | |
223 | { | |
224 | } | |
225 | static int qemu_s390_get_stattr_stub(S390StAttribState *sa, uint64_t *start_gfn, | |
226 | uint32_t count, uint8_t *values) | |
227 | { | |
228 | return 0; | |
229 | } | |
230 | static long long qemu_s390_get_dirtycount_stub(S390StAttribState *sa) | |
231 | { | |
232 | return 0; | |
233 | } | |
234 | static int qemu_s390_set_migrationmode_stub(S390StAttribState *sa, bool value) | |
235 | { | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static int qemu_s390_get_active(S390StAttribState *sa) | |
240 | { | |
241 | return sa->migration_enabled; | |
242 | } | |
243 | ||
244 | static void qemu_s390_stattrib_class_init(ObjectClass *oc, void *data) | |
245 | { | |
246 | S390StAttribClass *sa_cl = S390_STATTRIB_CLASS(oc); | |
247 | ||
248 | sa_cl->synchronize = qemu_s390_synchronize_stub; | |
249 | sa_cl->get_stattr = qemu_s390_get_stattr_stub; | |
250 | sa_cl->set_stattr = qemu_s390_peek_stattr_stub; | |
251 | sa_cl->peek_stattr = qemu_s390_peek_stattr_stub; | |
252 | sa_cl->set_migrationmode = qemu_s390_set_migrationmode_stub; | |
253 | sa_cl->get_dirtycount = qemu_s390_get_dirtycount_stub; | |
254 | sa_cl->get_active = qemu_s390_get_active; | |
255 | } | |
256 | ||
257 | static const TypeInfo qemu_s390_stattrib_info = { | |
258 | .name = TYPE_QEMU_S390_STATTRIB, | |
259 | .parent = TYPE_S390_STATTRIB, | |
260 | .instance_init = qemu_s390_stattrib_instance_init, | |
261 | .instance_size = sizeof(QEMUS390StAttribState), | |
262 | .class_init = qemu_s390_stattrib_class_init, | |
263 | .class_size = sizeof(S390StAttribClass), | |
264 | }; | |
265 | ||
266 | /* Generic abstract object: */ | |
267 | ||
268 | static void s390_stattrib_realize(DeviceState *dev, Error **errp) | |
269 | { | |
270 | bool ambiguous = false; | |
271 | ||
272 | object_resolve_path_type("", TYPE_S390_STATTRIB, &ambiguous); | |
273 | if (ambiguous) { | |
274 | error_setg(errp, "storage_attributes device already exists"); | |
275 | } | |
276 | } | |
277 | ||
278 | static void s390_stattrib_class_init(ObjectClass *oc, void *data) | |
279 | { | |
280 | DeviceClass *dc = DEVICE_CLASS(oc); | |
281 | ||
282 | dc->hotpluggable = false; | |
283 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); | |
284 | dc->realize = s390_stattrib_realize; | |
285 | } | |
286 | ||
287 | static inline bool s390_stattrib_get_migration_enabled(Object *obj, Error **e) | |
288 | { | |
289 | S390StAttribState *s = S390_STATTRIB(obj); | |
290 | ||
291 | return s->migration_enabled; | |
292 | } | |
293 | ||
294 | static inline void s390_stattrib_set_migration_enabled(Object *obj, bool value, | |
295 | Error **errp) | |
296 | { | |
297 | S390StAttribState *s = S390_STATTRIB(obj); | |
298 | ||
299 | s->migration_enabled = value; | |
300 | } | |
301 | ||
302 | static void s390_stattrib_instance_init(Object *obj) | |
303 | { | |
304 | S390StAttribState *sas = S390_STATTRIB(obj); | |
305 | SaveVMHandlers *ops; | |
306 | ||
307 | /* ops will always be freed by qemu when unregistering */ | |
308 | ops = g_new0(SaveVMHandlers, 1); | |
309 | ||
310 | ops->save_setup = cmma_save_setup; | |
311 | ops->save_live_iterate = cmma_save_iterate; | |
312 | ops->save_live_complete_precopy = cmma_save_complete; | |
313 | ops->save_live_pending = cmma_save_pending; | |
314 | ops->save_cleanup = cmma_save_cleanup; | |
315 | ops->load_state = cmma_load; | |
316 | ops->is_active = cmma_active; | |
317 | register_savevm_live(NULL, TYPE_S390_STATTRIB, 0, 0, ops, sas); | |
318 | ||
319 | object_property_add_bool(obj, "migration-enabled", | |
320 | s390_stattrib_get_migration_enabled, | |
321 | s390_stattrib_set_migration_enabled, NULL); | |
322 | object_property_set_bool(obj, true, "migration-enabled", NULL); | |
323 | sas->migration_cur_gfn = 0; | |
324 | } | |
325 | ||
326 | static const TypeInfo s390_stattrib_info = { | |
327 | .name = TYPE_S390_STATTRIB, | |
328 | .parent = TYPE_DEVICE, | |
329 | .instance_init = s390_stattrib_instance_init, | |
330 | .instance_size = sizeof(S390StAttribState), | |
331 | .class_init = s390_stattrib_class_init, | |
332 | .class_size = sizeof(S390StAttribClass), | |
333 | .abstract = true, | |
334 | }; | |
335 | ||
336 | static void s390_stattrib_register_types(void) | |
337 | { | |
338 | type_register_static(&s390_stattrib_info); | |
339 | type_register_static(&qemu_s390_stattrib_info); | |
340 | } | |
341 | ||
342 | type_init(s390_stattrib_register_types) |