master
c 504 lines 14.8 KB
Raw
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 "qemu/units.h"
14 #include "exec/target_page.h"
15 #include "hw/s390x/s390-virtio-ccw.h"
16 #include "hw/core/qdev-properties.h"
17 #include "hw/s390x/storage-keys.h"
18 #include "qapi/error.h"
19 #include "qapi/qapi-commands-machine.h"
20 #include "qobject/qdict.h"
21 #include "qemu/error-report.h"
22 #include "system/memory_mapping.h"
23 #include "system/address-spaces.h"
24 #include "system/kvm.h"
25 #include "migration/qemu-file-types.h"
26 #include "migration/register.h"
27 #include "monitor/hmp.h"
28 #include "monitor/monitor.h"
29 #include "trace.h"
30
31 #define S390_SKEYS_BUFFER_SIZE (128 * KiB) /* Room for 128k storage keys */
32 #define S390_SKEYS_SAVE_FLAG_EOS 0x01
33 #define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
34 #define S390_SKEYS_SAVE_FLAG_ERROR 0x04
35
36 S390SKeysState *s390_get_skeys_device(void)
37 {
38 S390SKeysState *ss;
39
40 ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
41 assert(ss);
42 return ss;
43 }
44
45 void s390_skeys_init(void)
46 {
47 Object *obj;
48
49 if (kvm_enabled()) {
50 obj = object_new(TYPE_KVM_S390_SKEYS);
51 } else {
52 obj = object_new(TYPE_QEMU_S390_SKEYS);
53 }
54 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
55 obj);
56 object_unref(obj);
57
58 qdev_realize(DEVICE(obj), NULL, &error_fatal);
59 }
60
61 int s390_skeys_get(S390SKeysState *ks, uint64_t start_gfn,
62 uint64_t count, uint8_t *keys)
63 {
64 S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
65 int rc;
66
67 rc = kc->get_skeys(ks, start_gfn, count, keys);
68 if (rc) {
69 trace_s390_skeys_get_nonzero(rc);
70 }
71 return rc;
72 }
73
74 int s390_skeys_set(S390SKeysState *ks, uint64_t start_gfn,
75 uint64_t count, uint8_t *keys)
76 {
77 S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
78 int rc;
79
80 rc = kc->set_skeys(ks, start_gfn, count, keys);
81 if (rc) {
82 trace_s390_skeys_set_nonzero(rc);
83 }
84 return rc;
85 }
86
87 static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
88 uint64_t count, Error **errp)
89 {
90 uint64_t curpage = startgfn;
91 uint64_t maxpage = curpage + count - 1;
92
93 for (; curpage <= maxpage; curpage++) {
94 uint8_t acc = (*keys & 0xF0) >> 4;
95 int fp = (*keys & 0x08);
96 int ref = (*keys & 0x04);
97 int ch = (*keys & 0x02);
98 int res = (*keys & 0x01);
99
100 fprintf(f, "page=%03" PRIx64 ": key(%d) => ACC=%X, FP=%d, REF=%d,"
101 " ch=%d, reserved=%d\n",
102 curpage, *keys, acc, fp, ref, ch, res);
103 keys++;
104 }
105 }
106
107 #ifdef CONFIG_HMP
108 void hmp_info_skeys(MonitorHMP *hmp, const QDict *qdict)
109 {
110 S390SKeysState *ss = s390_get_skeys_device();
111 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
112 uint64_t addr = qdict_get_int(qdict, "addr");
113 uint8_t key;
114 int r;
115
116 /* Quick check to see if guest is using storage keys*/
117 if (!skeyclass->skeys_are_enabled(ss)) {
118 monitor_hmp_printf(hmp, "Error: This guest is not using storage keys\n");
119 return;
120 }
121
122 if (!address_space_access_valid(&address_space_memory,
123 addr & TARGET_PAGE_MASK, TARGET_PAGE_SIZE,
124 false, MEMTXATTRS_UNSPECIFIED)) {
125 monitor_hmp_printf(hmp, "Error: The given address is not valid\n");
126 return;
127 }
128
129 r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
130 if (r < 0) {
131 monitor_hmp_printf(hmp, "Error: %s\n", strerror(-r));
132 return;
133 }
134
135 monitor_hmp_printf(hmp, " key: 0x%X\n", key);
136 }
137
138 void hmp_dump_skeys(MonitorHMP *hmp, const QDict *qdict)
139 {
140 const char *filename = qdict_get_str(qdict, "filename");
141 Error *err = NULL;
142
143 qmp_dump_skeys(filename, &err);
144 if (err) {
145 error_report_err(err);
146 }
147 }
148 #endif
149
150 void s390_qmp_dump_skeys(const char *filename, Error **errp)
151 {
152 S390SKeysState *ss = s390_get_skeys_device();
153 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
154 GuestPhysBlockList guest_phys_blocks;
155 GuestPhysBlock *block;
156 uint64_t pages, gfn;
157 Error *lerr = NULL;
158 uint8_t *buf;
159 int ret;
160 int fd;
161 FILE *f;
162
163 /* Quick check to see if guest is using storage keys*/
164 if (!skeyclass->skeys_are_enabled(ss)) {
165 error_setg(errp, "This guest is not using storage keys - "
166 "nothing to dump");
167 return;
168 }
169
170 fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
171 if (fd < 0) {
172 error_setg_file_open(errp, errno, filename);
173 return;
174 }
175 f = fdopen(fd, "wb");
176 if (!f) {
177 close(fd);
178 error_setg_file_open(errp, errno, filename);
179 return;
180 }
181
182 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
183 if (!buf) {
184 error_setg(errp, "Could not allocate memory");
185 goto out;
186 }
187
188 assert(bql_locked());
189 guest_phys_blocks_init(&guest_phys_blocks);
190 guest_phys_blocks_append(&guest_phys_blocks);
191
192 QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
193 assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
194 assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
195
196 gfn = block->target_start / TARGET_PAGE_SIZE;
197 pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
198
199 while (pages) {
200 const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
201
202 ret = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
203 if (ret < 0) {
204 error_setg_errno(errp, -ret, "get_keys error");
205 goto out_free;
206 }
207
208 /* write keys to stream */
209 write_keys(f, buf, gfn, cur_pages, &lerr);
210 if (lerr) {
211 goto out_free;
212 }
213
214 gfn += cur_pages;
215 pages -= cur_pages;
216 }
217 }
218
219 out_free:
220 guest_phys_blocks_free(&guest_phys_blocks);
221 error_propagate(errp, lerr);
222 g_free(buf);
223 out:
224 fclose(f);
225 }
226
227 static bool qemu_s390_skeys_are_enabled(S390SKeysState *ss)
228 {
229 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
230
231 /* Lockless check is sufficient. */
232 return !!skeys->keydata;
233 }
234
235 static bool qemu_s390_enable_skeys(S390SKeysState *ss)
236 {
237 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
238 static gsize initialized;
239
240 if (likely(skeys->keydata)) {
241 return true;
242 }
243
244 /*
245 * TODO: Modern Linux doesn't use storage keys unless running KVM guests
246 * that use storage keys. Therefore, we keep it simple for now.
247 *
248 * 1) We should initialize to "referenced+changed" for an initial
249 * over-indication. Let's avoid touching megabytes of data for now and
250 * assume that any sane user will issue a storage key instruction before
251 * actually relying on this data.
252 * 2) Relying on ram_size and allocating a big array is ugly. We should
253 * allocate and manage storage key data per RAMBlock or optimally using
254 * some sparse data structure.
255 * 3) We only ever have a single S390SKeysState, so relying on
256 * g_once_init_enter() is good enough.
257 */
258 if (g_once_init_enter(&initialized)) {
259 S390CcwMachineState *s390ms = S390_CCW_MACHINE(qdev_get_machine());
260
261 skeys->key_count = s390_get_memory_limit(s390ms) / TARGET_PAGE_SIZE;
262 skeys->keydata = g_malloc0(skeys->key_count);
263 g_once_init_leave(&initialized, 1);
264 }
265 return false;
266 }
267
268 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
269 uint64_t count, uint8_t *keys)
270 {
271 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
272 int i;
273
274 /* Check for uint64 overflow and access beyond end of key data */
275 if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
276 start_gfn + count < count)) {
277 error_report("Error: Setting storage keys for pages with unallocated "
278 "storage key memory: gfn=%" PRIx64 " count=%" PRId64,
279 start_gfn, count);
280 return -EINVAL;
281 }
282
283 for (i = 0; i < count; i++) {
284 skeydev->keydata[start_gfn + i] = keys[i];
285 }
286 return 0;
287 }
288
289 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
290 uint64_t count, uint8_t *keys)
291 {
292 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
293 int i;
294
295 /* Check for uint64 overflow and access beyond end of key data */
296 if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
297 start_gfn + count < count)) {
298 error_report("Error: Getting storage keys for pages with unallocated "
299 "storage key memory: gfn=%" PRIx64 " count=%" PRId64,
300 start_gfn, count);
301 return -EINVAL;
302 }
303
304 for (i = 0; i < count; i++) {
305 keys[i] = skeydev->keydata[start_gfn + i];
306 }
307 return 0;
308 }
309
310 static void qemu_s390_skeys_class_init(ObjectClass *oc, const void *data)
311 {
312 S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
313 DeviceClass *dc = DEVICE_CLASS(oc);
314
315 skeyclass->skeys_are_enabled = qemu_s390_skeys_are_enabled;
316 skeyclass->enable_skeys = qemu_s390_enable_skeys;
317 skeyclass->get_skeys = qemu_s390_skeys_get;
318 skeyclass->set_skeys = qemu_s390_skeys_set;
319
320 /* Reason: Internal device (only one skeys device for the whole memory) */
321 dc->user_creatable = false;
322 }
323
324 static void s390_storage_keys_save(QEMUFile *f, void *opaque)
325 {
326 S390SKeysState *ss = S390_SKEYS(opaque);
327 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
328 GuestPhysBlockList guest_phys_blocks;
329 GuestPhysBlock *block;
330 uint64_t pages, gfn;
331 int error = 0;
332 uint8_t *buf;
333
334 if (!skeyclass->skeys_are_enabled(ss)) {
335 goto end_stream;
336 }
337
338 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
339 if (!buf) {
340 error_report("storage key save could not allocate memory");
341 goto end_stream;
342 }
343
344 guest_phys_blocks_init(&guest_phys_blocks);
345 guest_phys_blocks_append(&guest_phys_blocks);
346
347 /* Send each contiguous physical memory range separately. */
348 QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
349 assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
350 assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
351
352 gfn = block->target_start / TARGET_PAGE_SIZE;
353 pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
354 qemu_put_be64(f, block->target_start | S390_SKEYS_SAVE_FLAG_SKEYS);
355 qemu_put_be64(f, pages);
356
357 while (pages) {
358 const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
359
360 if (!error) {
361 error = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
362 if (error) {
363 /*
364 * Create a valid stream with all 0x00 and indicate
365 * S390_SKEYS_SAVE_FLAG_ERROR to the destination.
366 */
367 error_report("S390_GET_KEYS error %d", error);
368 memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
369 }
370 }
371
372 qemu_put_buffer(f, buf, cur_pages);
373 gfn += cur_pages;
374 pages -= cur_pages;
375 }
376
377 if (error) {
378 break;
379 }
380 }
381
382 guest_phys_blocks_free(&guest_phys_blocks);
383 g_free(buf);
384 end_stream:
385 if (error) {
386 qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_ERROR);
387 } else {
388 qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_EOS);
389 }
390 }
391
392 static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
393 {
394 S390SKeysState *ss = S390_SKEYS(opaque);
395 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
396 int ret = 0;
397
398 /*
399 * Make sure to lazy-enable if required to be done explicitly. No need to
400 * flush any TLB as the VM is not running yet.
401 */
402 if (skeyclass->enable_skeys) {
403 skeyclass->enable_skeys(ss);
404 }
405
406 while (!ret) {
407 ram_addr_t addr;
408 int flags;
409
410 addr = qemu_get_be64(f);
411 flags = addr & ~TARGET_PAGE_MASK;
412 addr &= TARGET_PAGE_MASK;
413
414 switch (flags) {
415 case S390_SKEYS_SAVE_FLAG_SKEYS: {
416 const uint64_t total_count = qemu_get_be64(f);
417 uint64_t handled_count = 0, cur_count;
418 uint64_t cur_gfn = addr / TARGET_PAGE_SIZE;
419 uint8_t *buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
420
421 if (!buf) {
422 error_report("storage key load could not allocate memory");
423 ret = -ENOMEM;
424 break;
425 }
426
427 while (handled_count < total_count) {
428 cur_count = MIN(total_count - handled_count,
429 S390_SKEYS_BUFFER_SIZE);
430 qemu_get_buffer(f, buf, cur_count);
431
432 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
433 if (ret < 0) {
434 error_report("S390_SET_KEYS error %d", ret);
435 break;
436 }
437 handled_count += cur_count;
438 cur_gfn += cur_count;
439 }
440 g_free(buf);
441 break;
442 }
443 case S390_SKEYS_SAVE_FLAG_ERROR: {
444 error_report("Storage key data is incomplete");
445 ret = -EINVAL;
446 break;
447 }
448 case S390_SKEYS_SAVE_FLAG_EOS:
449 /* normal exit */
450 return 0;
451 default:
452 error_report("Unexpected storage key flag data: %#x", flags);
453 ret = -EINVAL;
454 }
455 }
456
457 return ret;
458 }
459
460 static SaveVMHandlers savevm_s390_storage_keys = {
461 .save_state = s390_storage_keys_save,
462 .load_state = s390_storage_keys_load,
463 };
464
465 static void s390_skeys_realize(DeviceState *dev, Error **errp)
466 {
467 S390SKeysState *ss = S390_SKEYS(dev);
468
469 register_savevm_live(TYPE_S390_SKEYS, 0, 1, &savevm_s390_storage_keys, ss);
470 }
471
472 static void s390_skeys_class_init(ObjectClass *oc, const void *data)
473 {
474 DeviceClass *dc = DEVICE_CLASS(oc);
475
476 dc->hotpluggable = false;
477 dc->realize = s390_skeys_realize;
478 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
479 }
480
481 static const TypeInfo s390_skeys_types[] = {
482 {
483 .name = TYPE_DUMP_SKEYS_INTERFACE,
484 .parent = TYPE_INTERFACE,
485 .class_size = sizeof(DumpSKeysInterface),
486 },
487 {
488 .name = TYPE_S390_SKEYS,
489 .parent = TYPE_DEVICE,
490 .instance_size = sizeof(S390SKeysState),
491 .class_init = s390_skeys_class_init,
492 .class_size = sizeof(S390SKeysClass),
493 .abstract = true,
494 },
495 {
496 .name = TYPE_QEMU_S390_SKEYS,
497 .parent = TYPE_S390_SKEYS,
498 .instance_size = sizeof(QEMUS390SKeysState),
499 .class_init = qemu_s390_skeys_class_init,
500 .class_size = sizeof(S390SKeysClass),
501 },
502 };
503
504 DEFINE_TYPES(s390_skeys_types)