]> git.proxmox.com Git - mirror_qemu.git/blob - hw/s390x/s390-skeys.c
qapi: Empty out qapi-schema.json
[mirror_qemu.git] / hw / s390x / s390-skeys.c
1 /*
2 * s390 storage key device
3 *
4 * Copyright 2015 IBM Corp.
5 * Author(s): Jason J. Herne <jjherne@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 "hw/s390x/storage-keys.h"
15 #include "qapi/error.h"
16 #include "qapi/qapi-commands-misc.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qemu/error-report.h"
19 #include "sysemu/kvm.h"
20 #include "migration/register.h"
21
22 #define S390_SKEYS_BUFFER_SIZE 131072 /* Room for 128k storage keys */
23 #define S390_SKEYS_SAVE_FLAG_EOS 0x01
24 #define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
25 #define S390_SKEYS_SAVE_FLAG_ERROR 0x04
26
27 S390SKeysState *s390_get_skeys_device(void)
28 {
29 S390SKeysState *ss;
30
31 ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
32 assert(ss);
33 return ss;
34 }
35
36 void s390_skeys_init(void)
37 {
38 Object *obj;
39
40 if (kvm_enabled()) {
41 obj = object_new(TYPE_KVM_S390_SKEYS);
42 } else {
43 obj = object_new(TYPE_QEMU_S390_SKEYS);
44 }
45 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
46 obj, NULL);
47 object_unref(obj);
48
49 qdev_init_nofail(DEVICE(obj));
50 }
51
52 static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
53 uint64_t count, Error **errp)
54 {
55 uint64_t curpage = startgfn;
56 uint64_t maxpage = curpage + count - 1;
57
58 for (; curpage <= maxpage; curpage++) {
59 uint8_t acc = (*keys & 0xF0) >> 4;
60 int fp = (*keys & 0x08);
61 int ref = (*keys & 0x04);
62 int ch = (*keys & 0x02);
63 int res = (*keys & 0x01);
64
65 fprintf(f, "page=%03" PRIx64 ": key(%d) => ACC=%X, FP=%d, REF=%d,"
66 " ch=%d, reserved=%d\n",
67 curpage, *keys, acc, fp, ref, ch, res);
68 keys++;
69 }
70 }
71
72 void hmp_info_skeys(Monitor *mon, const QDict *qdict)
73 {
74 S390SKeysState *ss = s390_get_skeys_device();
75 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
76 uint64_t addr = qdict_get_int(qdict, "addr");
77 uint8_t key;
78 int r;
79
80 /* Quick check to see if guest is using storage keys*/
81 if (!skeyclass->skeys_enabled(ss)) {
82 monitor_printf(mon, "Error: This guest is not using storage keys\n");
83 return;
84 }
85
86 r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
87 if (r < 0) {
88 monitor_printf(mon, "Error: %s\n", strerror(-r));
89 return;
90 }
91
92 monitor_printf(mon, " key: 0x%X\n", key);
93 }
94
95 void hmp_dump_skeys(Monitor *mon, const QDict *qdict)
96 {
97 const char *filename = qdict_get_str(qdict, "filename");
98 Error *err = NULL;
99
100 qmp_dump_skeys(filename, &err);
101 if (err) {
102 error_report_err(err);
103 }
104 }
105
106 void qmp_dump_skeys(const char *filename, Error **errp)
107 {
108 S390SKeysState *ss = s390_get_skeys_device();
109 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
110 const uint64_t total_count = ram_size / TARGET_PAGE_SIZE;
111 uint64_t handled_count = 0, cur_count;
112 Error *lerr = NULL;
113 vaddr cur_gfn = 0;
114 uint8_t *buf;
115 int ret;
116 int fd;
117 FILE *f;
118
119 /* Quick check to see if guest is using storage keys*/
120 if (!skeyclass->skeys_enabled(ss)) {
121 error_setg(errp, "This guest is not using storage keys - "
122 "nothing to dump");
123 return;
124 }
125
126 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
127 if (fd < 0) {
128 error_setg_file_open(errp, errno, filename);
129 return;
130 }
131 f = fdopen(fd, "wb");
132 if (!f) {
133 close(fd);
134 error_setg_file_open(errp, errno, filename);
135 return;
136 }
137
138 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
139 if (!buf) {
140 error_setg(errp, "Could not allocate memory");
141 goto out;
142 }
143
144 /* we'll only dump initial memory for now */
145 while (handled_count < total_count) {
146 /* Calculate how many keys to ask for & handle overflow case */
147 cur_count = MIN(total_count - handled_count, S390_SKEYS_BUFFER_SIZE);
148
149 ret = skeyclass->get_skeys(ss, cur_gfn, cur_count, buf);
150 if (ret < 0) {
151 error_setg(errp, "get_keys error %d", ret);
152 goto out_free;
153 }
154
155 /* write keys to stream */
156 write_keys(f, buf, cur_gfn, cur_count, &lerr);
157 if (lerr) {
158 goto out_free;
159 }
160
161 cur_gfn += cur_count;
162 handled_count += cur_count;
163 }
164
165 out_free:
166 error_propagate(errp, lerr);
167 g_free(buf);
168 out:
169 fclose(f);
170 }
171
172 static void qemu_s390_skeys_init(Object *obj)
173 {
174 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(obj);
175 MachineState *machine = MACHINE(qdev_get_machine());
176
177 skeys->key_count = machine->maxram_size / TARGET_PAGE_SIZE;
178 skeys->keydata = g_malloc0(skeys->key_count);
179 }
180
181 static int qemu_s390_skeys_enabled(S390SKeysState *ss)
182 {
183 return 1;
184 }
185
186 /*
187 * TODO: for memory hotplug support qemu_s390_skeys_set and qemu_s390_skeys_get
188 * will have to make sure that the given gfn belongs to a memory region and not
189 * a memory hole.
190 */
191 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
192 uint64_t count, uint8_t *keys)
193 {
194 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
195 int i;
196
197 /* Check for uint64 overflow and access beyond end of key data */
198 if (start_gfn + count > skeydev->key_count || start_gfn + count < count) {
199 error_report("Error: Setting storage keys for page beyond the end "
200 "of memory: gfn=%" PRIx64 " count=%" PRId64,
201 start_gfn, count);
202 return -EINVAL;
203 }
204
205 for (i = 0; i < count; i++) {
206 skeydev->keydata[start_gfn + i] = keys[i];
207 }
208 return 0;
209 }
210
211 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
212 uint64_t count, uint8_t *keys)
213 {
214 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
215 int i;
216
217 /* Check for uint64 overflow and access beyond end of key data */
218 if (start_gfn + count > skeydev->key_count || start_gfn + count < count) {
219 error_report("Error: Getting storage keys for page beyond the end "
220 "of memory: gfn=%" PRIx64 " count=%" PRId64,
221 start_gfn, count);
222 return -EINVAL;
223 }
224
225 for (i = 0; i < count; i++) {
226 keys[i] = skeydev->keydata[start_gfn + i];
227 }
228 return 0;
229 }
230
231 static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
232 {
233 S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
234 DeviceClass *dc = DEVICE_CLASS(oc);
235
236 skeyclass->skeys_enabled = qemu_s390_skeys_enabled;
237 skeyclass->get_skeys = qemu_s390_skeys_get;
238 skeyclass->set_skeys = qemu_s390_skeys_set;
239
240 /* Reason: Internal device (only one skeys device for the whole memory) */
241 dc->user_creatable = false;
242 }
243
244 static const TypeInfo qemu_s390_skeys_info = {
245 .name = TYPE_QEMU_S390_SKEYS,
246 .parent = TYPE_S390_SKEYS,
247 .instance_init = qemu_s390_skeys_init,
248 .instance_size = sizeof(QEMUS390SKeysState),
249 .class_init = qemu_s390_skeys_class_init,
250 .class_size = sizeof(S390SKeysClass),
251 };
252
253 static void s390_storage_keys_save(QEMUFile *f, void *opaque)
254 {
255 S390SKeysState *ss = S390_SKEYS(opaque);
256 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
257 uint64_t pages_left = ram_size / TARGET_PAGE_SIZE;
258 uint64_t read_count, eos = S390_SKEYS_SAVE_FLAG_EOS;
259 vaddr cur_gfn = 0;
260 int error = 0;
261 uint8_t *buf;
262
263 if (!skeyclass->skeys_enabled(ss)) {
264 goto end_stream;
265 }
266
267 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
268 if (!buf) {
269 error_report("storage key save could not allocate memory");
270 goto end_stream;
271 }
272
273 /* We only support initial memory. Standby memory is not handled yet. */
274 qemu_put_be64(f, (cur_gfn * TARGET_PAGE_SIZE) | S390_SKEYS_SAVE_FLAG_SKEYS);
275 qemu_put_be64(f, pages_left);
276
277 while (pages_left) {
278 read_count = MIN(pages_left, S390_SKEYS_BUFFER_SIZE);
279
280 if (!error) {
281 error = skeyclass->get_skeys(ss, cur_gfn, read_count, buf);
282 if (error) {
283 /*
284 * If error: we want to fill the stream with valid data instead
285 * of stopping early so we pad the stream with 0x00 values and
286 * use S390_SKEYS_SAVE_FLAG_ERROR to indicate failure to the
287 * reading side.
288 */
289 error_report("S390_GET_KEYS error %d", error);
290 memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
291 eos = S390_SKEYS_SAVE_FLAG_ERROR;
292 }
293 }
294
295 qemu_put_buffer(f, buf, read_count);
296 cur_gfn += read_count;
297 pages_left -= read_count;
298 }
299
300 g_free(buf);
301 end_stream:
302 qemu_put_be64(f, eos);
303 }
304
305 static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
306 {
307 S390SKeysState *ss = S390_SKEYS(opaque);
308 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
309 int ret = 0;
310
311 while (!ret) {
312 ram_addr_t addr;
313 int flags;
314
315 addr = qemu_get_be64(f);
316 flags = addr & ~TARGET_PAGE_MASK;
317 addr &= TARGET_PAGE_MASK;
318
319 switch (flags) {
320 case S390_SKEYS_SAVE_FLAG_SKEYS: {
321 const uint64_t total_count = qemu_get_be64(f);
322 uint64_t handled_count = 0, cur_count;
323 uint64_t cur_gfn = addr / TARGET_PAGE_SIZE;
324 uint8_t *buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
325
326 if (!buf) {
327 error_report("storage key load could not allocate memory");
328 ret = -ENOMEM;
329 break;
330 }
331
332 while (handled_count < total_count) {
333 cur_count = MIN(total_count - handled_count,
334 S390_SKEYS_BUFFER_SIZE);
335 qemu_get_buffer(f, buf, cur_count);
336
337 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
338 if (ret < 0) {
339 error_report("S390_SET_KEYS error %d", ret);
340 break;
341 }
342 handled_count += cur_count;
343 cur_gfn += cur_count;
344 }
345 g_free(buf);
346 break;
347 }
348 case S390_SKEYS_SAVE_FLAG_ERROR: {
349 error_report("Storage key data is incomplete");
350 ret = -EINVAL;
351 break;
352 }
353 case S390_SKEYS_SAVE_FLAG_EOS:
354 /* normal exit */
355 return 0;
356 default:
357 error_report("Unexpected storage key flag data: %#x", flags);
358 ret = -EINVAL;
359 }
360 }
361
362 return ret;
363 }
364
365 static inline bool s390_skeys_get_migration_enabled(Object *obj, Error **errp)
366 {
367 S390SKeysState *ss = S390_SKEYS(obj);
368
369 return ss->migration_enabled;
370 }
371
372 static SaveVMHandlers savevm_s390_storage_keys = {
373 .save_state = s390_storage_keys_save,
374 .load_state = s390_storage_keys_load,
375 };
376
377 static inline void s390_skeys_set_migration_enabled(Object *obj, bool value,
378 Error **errp)
379 {
380 S390SKeysState *ss = S390_SKEYS(obj);
381
382 /* Prevent double registration of savevm handler */
383 if (ss->migration_enabled == value) {
384 return;
385 }
386
387 ss->migration_enabled = value;
388
389 if (ss->migration_enabled) {
390 register_savevm_live(NULL, TYPE_S390_SKEYS, 0, 1,
391 &savevm_s390_storage_keys, ss);
392 } else {
393 unregister_savevm(DEVICE(ss), TYPE_S390_SKEYS, ss);
394 }
395 }
396
397 static void s390_skeys_instance_init(Object *obj)
398 {
399 object_property_add_bool(obj, "migration-enabled",
400 s390_skeys_get_migration_enabled,
401 s390_skeys_set_migration_enabled, NULL);
402 object_property_set_bool(obj, true, "migration-enabled", NULL);
403 }
404
405 static void s390_skeys_class_init(ObjectClass *oc, void *data)
406 {
407 DeviceClass *dc = DEVICE_CLASS(oc);
408
409 dc->hotpluggable = false;
410 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
411 }
412
413 static const TypeInfo s390_skeys_info = {
414 .name = TYPE_S390_SKEYS,
415 .parent = TYPE_DEVICE,
416 .instance_init = s390_skeys_instance_init,
417 .instance_size = sizeof(S390SKeysState),
418 .class_init = s390_skeys_class_init,
419 .class_size = sizeof(S390SKeysClass),
420 .abstract = true,
421 };
422
423 static void qemu_s390_skeys_register_types(void)
424 {
425 type_register_static(&s390_skeys_info);
426 type_register_static(&qemu_s390_skeys_info);
427 }
428
429 type_init(qemu_s390_skeys_register_types)