linux-user: Use do_munmap for target_mmap failure
[qemu/kevin.git] / hw / s390x / s390-skeys.c
blob5c535d483e9f7bb0e665330c6b694cc19cc8904c
1 /*
2 * s390 storage key device
4 * Copyright 2015 IBM Corp.
5 * Author(s): Jason J. Herne <jjherne@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 "hw/boards.h"
15 #include "hw/qdev-properties.h"
16 #include "hw/s390x/storage-keys.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-commands-misc-target.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qemu/error-report.h"
21 #include "sysemu/memory_mapping.h"
22 #include "exec/address-spaces.h"
23 #include "sysemu/kvm.h"
24 #include "migration/qemu-file-types.h"
25 #include "migration/register.h"
27 #define S390_SKEYS_BUFFER_SIZE (128 * KiB) /* Room for 128k storage keys */
28 #define S390_SKEYS_SAVE_FLAG_EOS 0x01
29 #define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
30 #define S390_SKEYS_SAVE_FLAG_ERROR 0x04
32 S390SKeysState *s390_get_skeys_device(void)
34 S390SKeysState *ss;
36 ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
37 assert(ss);
38 return ss;
41 void s390_skeys_init(void)
43 Object *obj;
45 if (kvm_enabled()) {
46 obj = object_new(TYPE_KVM_S390_SKEYS);
47 } else {
48 obj = object_new(TYPE_QEMU_S390_SKEYS);
50 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
51 obj);
52 object_unref(obj);
54 qdev_realize(DEVICE(obj), NULL, &error_fatal);
57 static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
58 uint64_t count, Error **errp)
60 uint64_t curpage = startgfn;
61 uint64_t maxpage = curpage + count - 1;
63 for (; curpage <= maxpage; curpage++) {
64 uint8_t acc = (*keys & 0xF0) >> 4;
65 int fp = (*keys & 0x08);
66 int ref = (*keys & 0x04);
67 int ch = (*keys & 0x02);
68 int res = (*keys & 0x01);
70 fprintf(f, "page=%03" PRIx64 ": key(%d) => ACC=%X, FP=%d, REF=%d,"
71 " ch=%d, reserved=%d\n",
72 curpage, *keys, acc, fp, ref, ch, res);
73 keys++;
77 void hmp_info_skeys(Monitor *mon, const QDict *qdict)
79 S390SKeysState *ss = s390_get_skeys_device();
80 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
81 uint64_t addr = qdict_get_int(qdict, "addr");
82 uint8_t key;
83 int r;
85 /* Quick check to see if guest is using storage keys*/
86 if (!skeyclass->skeys_are_enabled(ss)) {
87 monitor_printf(mon, "Error: This guest is not using storage keys\n");
88 return;
91 if (!address_space_access_valid(&address_space_memory,
92 addr & TARGET_PAGE_MASK, TARGET_PAGE_SIZE,
93 false, MEMTXATTRS_UNSPECIFIED)) {
94 monitor_printf(mon, "Error: The given address is not valid\n");
95 return;
98 r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
99 if (r < 0) {
100 monitor_printf(mon, "Error: %s\n", strerror(-r));
101 return;
104 monitor_printf(mon, " key: 0x%X\n", key);
107 void hmp_dump_skeys(Monitor *mon, const QDict *qdict)
109 const char *filename = qdict_get_str(qdict, "filename");
110 Error *err = NULL;
112 qmp_dump_skeys(filename, &err);
113 if (err) {
114 error_report_err(err);
118 void qmp_dump_skeys(const char *filename, Error **errp)
120 S390SKeysState *ss = s390_get_skeys_device();
121 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
122 GuestPhysBlockList guest_phys_blocks;
123 GuestPhysBlock *block;
124 uint64_t pages, gfn;
125 Error *lerr = NULL;
126 uint8_t *buf;
127 int ret;
128 int fd;
129 FILE *f;
131 /* Quick check to see if guest is using storage keys*/
132 if (!skeyclass->skeys_are_enabled(ss)) {
133 error_setg(errp, "This guest is not using storage keys - "
134 "nothing to dump");
135 return;
138 fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
139 if (fd < 0) {
140 error_setg_file_open(errp, errno, filename);
141 return;
143 f = fdopen(fd, "wb");
144 if (!f) {
145 close(fd);
146 error_setg_file_open(errp, errno, filename);
147 return;
150 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
151 if (!buf) {
152 error_setg(errp, "Could not allocate memory");
153 goto out;
156 assert(bql_locked());
157 guest_phys_blocks_init(&guest_phys_blocks);
158 guest_phys_blocks_append(&guest_phys_blocks);
160 QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
161 assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
162 assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
164 gfn = block->target_start / TARGET_PAGE_SIZE;
165 pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
167 while (pages) {
168 const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
170 ret = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
171 if (ret < 0) {
172 error_setg_errno(errp, -ret, "get_keys error");
173 goto out_free;
176 /* write keys to stream */
177 write_keys(f, buf, gfn, cur_pages, &lerr);
178 if (lerr) {
179 goto out_free;
182 gfn += cur_pages;
183 pages -= cur_pages;
187 out_free:
188 guest_phys_blocks_free(&guest_phys_blocks);
189 error_propagate(errp, lerr);
190 g_free(buf);
191 out:
192 fclose(f);
195 static bool qemu_s390_skeys_are_enabled(S390SKeysState *ss)
197 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
199 /* Lockless check is sufficient. */
200 return !!skeys->keydata;
203 static bool qemu_s390_enable_skeys(S390SKeysState *ss)
205 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
206 static gsize initialized;
208 if (likely(skeys->keydata)) {
209 return true;
213 * TODO: Modern Linux doesn't use storage keys unless running KVM guests
214 * that use storage keys. Therefore, we keep it simple for now.
216 * 1) We should initialize to "referenced+changed" for an initial
217 * over-indication. Let's avoid touching megabytes of data for now and
218 * assume that any sane user will issue a storage key instruction before
219 * actually relying on this data.
220 * 2) Relying on ram_size and allocating a big array is ugly. We should
221 * allocate and manage storage key data per RAMBlock or optimally using
222 * some sparse data structure.
223 * 3) We only ever have a single S390SKeysState, so relying on
224 * g_once_init_enter() is good enough.
226 if (g_once_init_enter(&initialized)) {
227 MachineState *machine = MACHINE(qdev_get_machine());
229 skeys->key_count = machine->ram_size / TARGET_PAGE_SIZE;
230 skeys->keydata = g_malloc0(skeys->key_count);
231 g_once_init_leave(&initialized, 1);
233 return false;
236 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
237 uint64_t count, uint8_t *keys)
239 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
240 int i;
242 /* Check for uint64 overflow and access beyond end of key data */
243 if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
244 start_gfn + count < count)) {
245 error_report("Error: Setting storage keys for pages with unallocated "
246 "storage key memory: gfn=%" PRIx64 " count=%" PRId64,
247 start_gfn, count);
248 return -EINVAL;
251 for (i = 0; i < count; i++) {
252 skeydev->keydata[start_gfn + i] = keys[i];
254 return 0;
257 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
258 uint64_t count, uint8_t *keys)
260 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
261 int i;
263 /* Check for uint64 overflow and access beyond end of key data */
264 if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
265 start_gfn + count < count)) {
266 error_report("Error: Getting storage keys for pages with unallocated "
267 "storage key memory: gfn=%" PRIx64 " count=%" PRId64,
268 start_gfn, count);
269 return -EINVAL;
272 for (i = 0; i < count; i++) {
273 keys[i] = skeydev->keydata[start_gfn + i];
275 return 0;
278 static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
280 S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
281 DeviceClass *dc = DEVICE_CLASS(oc);
283 skeyclass->skeys_are_enabled = qemu_s390_skeys_are_enabled;
284 skeyclass->enable_skeys = qemu_s390_enable_skeys;
285 skeyclass->get_skeys = qemu_s390_skeys_get;
286 skeyclass->set_skeys = qemu_s390_skeys_set;
288 /* Reason: Internal device (only one skeys device for the whole memory) */
289 dc->user_creatable = false;
292 static const TypeInfo qemu_s390_skeys_info = {
293 .name = TYPE_QEMU_S390_SKEYS,
294 .parent = TYPE_S390_SKEYS,
295 .instance_size = sizeof(QEMUS390SKeysState),
296 .class_init = qemu_s390_skeys_class_init,
297 .class_size = sizeof(S390SKeysClass),
300 static void s390_storage_keys_save(QEMUFile *f, void *opaque)
302 S390SKeysState *ss = S390_SKEYS(opaque);
303 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
304 GuestPhysBlockList guest_phys_blocks;
305 GuestPhysBlock *block;
306 uint64_t pages, gfn;
307 int error = 0;
308 uint8_t *buf;
310 if (!skeyclass->skeys_are_enabled(ss)) {
311 goto end_stream;
314 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
315 if (!buf) {
316 error_report("storage key save could not allocate memory");
317 goto end_stream;
320 guest_phys_blocks_init(&guest_phys_blocks);
321 guest_phys_blocks_append(&guest_phys_blocks);
323 /* Send each contiguous physical memory range separately. */
324 QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
325 assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
326 assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
328 gfn = block->target_start / TARGET_PAGE_SIZE;
329 pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
330 qemu_put_be64(f, block->target_start | S390_SKEYS_SAVE_FLAG_SKEYS);
331 qemu_put_be64(f, pages);
333 while (pages) {
334 const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
336 if (!error) {
337 error = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
338 if (error) {
340 * Create a valid stream with all 0x00 and indicate
341 * S390_SKEYS_SAVE_FLAG_ERROR to the destination.
343 error_report("S390_GET_KEYS error %d", error);
344 memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
348 qemu_put_buffer(f, buf, cur_pages);
349 gfn += cur_pages;
350 pages -= cur_pages;
353 if (error) {
354 break;
358 guest_phys_blocks_free(&guest_phys_blocks);
359 g_free(buf);
360 end_stream:
361 if (error) {
362 qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_ERROR);
363 } else {
364 qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_EOS);
368 static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
370 S390SKeysState *ss = S390_SKEYS(opaque);
371 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
372 int ret = 0;
375 * Make sure to lazy-enable if required to be done explicitly. No need to
376 * flush any TLB as the VM is not running yet.
378 if (skeyclass->enable_skeys) {
379 skeyclass->enable_skeys(ss);
382 while (!ret) {
383 ram_addr_t addr;
384 int flags;
386 addr = qemu_get_be64(f);
387 flags = addr & ~TARGET_PAGE_MASK;
388 addr &= TARGET_PAGE_MASK;
390 switch (flags) {
391 case S390_SKEYS_SAVE_FLAG_SKEYS: {
392 const uint64_t total_count = qemu_get_be64(f);
393 uint64_t handled_count = 0, cur_count;
394 uint64_t cur_gfn = addr / TARGET_PAGE_SIZE;
395 uint8_t *buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
397 if (!buf) {
398 error_report("storage key load could not allocate memory");
399 ret = -ENOMEM;
400 break;
403 while (handled_count < total_count) {
404 cur_count = MIN(total_count - handled_count,
405 S390_SKEYS_BUFFER_SIZE);
406 qemu_get_buffer(f, buf, cur_count);
408 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
409 if (ret < 0) {
410 error_report("S390_SET_KEYS error %d", ret);
411 break;
413 handled_count += cur_count;
414 cur_gfn += cur_count;
416 g_free(buf);
417 break;
419 case S390_SKEYS_SAVE_FLAG_ERROR: {
420 error_report("Storage key data is incomplete");
421 ret = -EINVAL;
422 break;
424 case S390_SKEYS_SAVE_FLAG_EOS:
425 /* normal exit */
426 return 0;
427 default:
428 error_report("Unexpected storage key flag data: %#x", flags);
429 ret = -EINVAL;
433 return ret;
436 static SaveVMHandlers savevm_s390_storage_keys = {
437 .save_state = s390_storage_keys_save,
438 .load_state = s390_storage_keys_load,
441 static void s390_skeys_realize(DeviceState *dev, Error **errp)
443 S390SKeysState *ss = S390_SKEYS(dev);
445 if (ss->migration_enabled) {
446 register_savevm_live(TYPE_S390_SKEYS, 0, 1,
447 &savevm_s390_storage_keys, ss);
451 static Property s390_skeys_props[] = {
452 DEFINE_PROP_BOOL("migration-enabled", S390SKeysState, migration_enabled, true),
453 DEFINE_PROP_END_OF_LIST(),
456 static void s390_skeys_class_init(ObjectClass *oc, void *data)
458 DeviceClass *dc = DEVICE_CLASS(oc);
460 dc->hotpluggable = false;
461 dc->realize = s390_skeys_realize;
462 device_class_set_props(dc, s390_skeys_props);
463 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
466 static const TypeInfo s390_skeys_info = {
467 .name = TYPE_S390_SKEYS,
468 .parent = TYPE_DEVICE,
469 .instance_size = sizeof(S390SKeysState),
470 .class_init = s390_skeys_class_init,
471 .class_size = sizeof(S390SKeysClass),
472 .abstract = true,
475 static void qemu_s390_skeys_register_types(void)
477 type_register_static(&s390_skeys_info);
478 type_register_static(&qemu_s390_skeys_info);
481 type_init(qemu_s390_skeys_register_types)