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
12
#include "qemu/osdep.h"
13
#include "qemu/units.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"
28
#define S390_SKEYS_BUFFER_SIZE (128 * KiB) /* Room for 128k storage keys */
29
#define S390_SKEYS_SAVE_FLAG_EOS 0x01
30
#define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
31
#define S390_SKEYS_SAVE_FLAG_ERROR 0x04
33
S390SKeysState *s390_get_skeys_device(void)
37
ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
42
void s390_skeys_init(void)
47
obj = object_new(TYPE_KVM_S390_SKEYS);
49
obj = object_new(TYPE_QEMU_S390_SKEYS);
51
object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
55
qdev_realize(DEVICE(obj), NULL, &error_fatal);
58
int s390_skeys_get(S390SKeysState *ks, uint64_t start_gfn,
59
uint64_t count, uint8_t *keys)
61
S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
64
rc = kc->get_skeys(ks, start_gfn, count, keys);
66
trace_s390_skeys_get_nonzero(rc);
71
int s390_skeys_set(S390SKeysState *ks, uint64_t start_gfn,
72
uint64_t count, uint8_t *keys)
74
S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
77
rc = kc->set_skeys(ks, start_gfn, count, keys);
79
trace_s390_skeys_set_nonzero(rc);
84
static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
85
uint64_t count, Error **errp)
87
uint64_t curpage = startgfn;
88
uint64_t maxpage = curpage + count - 1;
90
for (; curpage <= maxpage; curpage++) {
91
uint8_t acc = (*keys & 0xF0) >> 4;
92
int fp = (*keys & 0x08);
93
int ref = (*keys & 0x04);
94
int ch = (*keys & 0x02);
95
int res = (*keys & 0x01);
97
fprintf(f, "page=%03" PRIx64 ": key(%d) => ACC=%X, FP=%d, REF=%d,"
98
" ch=%d, reserved=%d\n",
99
curpage, *keys, acc, fp, ref, ch, res);
104
void hmp_info_skeys(Monitor *mon, const QDict *qdict)
106
S390SKeysState *ss = s390_get_skeys_device();
107
S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
108
uint64_t addr = qdict_get_int(qdict, "addr");
112
/* Quick check to see if guest is using storage keys*/
113
if (!skeyclass->skeys_are_enabled(ss)) {
114
monitor_printf(mon, "Error: This guest is not using storage keys\n");
118
if (!address_space_access_valid(&address_space_memory,
119
addr & TARGET_PAGE_MASK, TARGET_PAGE_SIZE,
120
false, MEMTXATTRS_UNSPECIFIED)) {
121
monitor_printf(mon, "Error: The given address is not valid\n");
125
r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
127
monitor_printf(mon, "Error: %s\n", strerror(-r));
131
monitor_printf(mon, " key: 0x%X\n", key);
134
void hmp_dump_skeys(Monitor *mon, const QDict *qdict)
136
const char *filename = qdict_get_str(qdict, "filename");
139
qmp_dump_skeys(filename, &err);
141
error_report_err(err);
145
void qmp_dump_skeys(const char *filename, Error **errp)
147
S390SKeysState *ss = s390_get_skeys_device();
148
S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
149
GuestPhysBlockList guest_phys_blocks;
150
GuestPhysBlock *block;
158
/* Quick check to see if guest is using storage keys*/
159
if (!skeyclass->skeys_are_enabled(ss)) {
160
error_setg(errp, "This guest is not using storage keys - "
165
fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
167
error_setg_file_open(errp, errno, filename);
170
f = fdopen(fd, "wb");
173
error_setg_file_open(errp, errno, filename);
177
buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
179
error_setg(errp, "Could not allocate memory");
183
assert(bql_locked());
184
guest_phys_blocks_init(&guest_phys_blocks);
185
guest_phys_blocks_append(&guest_phys_blocks);
187
QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
188
assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
189
assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
191
gfn = block->target_start / TARGET_PAGE_SIZE;
192
pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
195
const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
197
ret = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
199
error_setg_errno(errp, -ret, "get_keys error");
203
/* write keys to stream */
204
write_keys(f, buf, gfn, cur_pages, &lerr);
215
guest_phys_blocks_free(&guest_phys_blocks);
216
error_propagate(errp, lerr);
222
static bool qemu_s390_skeys_are_enabled(S390SKeysState *ss)
224
QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
226
/* Lockless check is sufficient. */
227
return !!skeys->keydata;
230
static bool qemu_s390_enable_skeys(S390SKeysState *ss)
232
QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
233
static gsize initialized;
235
if (likely(skeys->keydata)) {
240
* TODO: Modern Linux doesn't use storage keys unless running KVM guests
241
* that use storage keys. Therefore, we keep it simple for now.
243
* 1) We should initialize to "referenced+changed" for an initial
244
* over-indication. Let's avoid touching megabytes of data for now and
245
* assume that any sane user will issue a storage key instruction before
246
* actually relying on this data.
247
* 2) Relying on ram_size and allocating a big array is ugly. We should
248
* allocate and manage storage key data per RAMBlock or optimally using
249
* some sparse data structure.
250
* 3) We only ever have a single S390SKeysState, so relying on
251
* g_once_init_enter() is good enough.
253
if (g_once_init_enter(&initialized)) {
254
MachineState *machine = MACHINE(qdev_get_machine());
256
skeys->key_count = machine->ram_size / TARGET_PAGE_SIZE;
257
skeys->keydata = g_malloc0(skeys->key_count);
258
g_once_init_leave(&initialized, 1);
263
static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
264
uint64_t count, uint8_t *keys)
266
QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
269
/* Check for uint64 overflow and access beyond end of key data */
270
if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
271
start_gfn + count < count)) {
272
error_report("Error: Setting storage keys for pages with unallocated "
273
"storage key memory: gfn=%" PRIx64 " count=%" PRId64,
278
for (i = 0; i < count; i++) {
279
skeydev->keydata[start_gfn + i] = keys[i];
284
static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
285
uint64_t count, uint8_t *keys)
287
QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
290
/* Check for uint64 overflow and access beyond end of key data */
291
if (unlikely(!skeydev->keydata || start_gfn + count > skeydev->key_count ||
292
start_gfn + count < count)) {
293
error_report("Error: Getting storage keys for pages with unallocated "
294
"storage key memory: gfn=%" PRIx64 " count=%" PRId64,
299
for (i = 0; i < count; i++) {
300
keys[i] = skeydev->keydata[start_gfn + i];
305
static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
307
S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
308
DeviceClass *dc = DEVICE_CLASS(oc);
310
skeyclass->skeys_are_enabled = qemu_s390_skeys_are_enabled;
311
skeyclass->enable_skeys = qemu_s390_enable_skeys;
312
skeyclass->get_skeys = qemu_s390_skeys_get;
313
skeyclass->set_skeys = qemu_s390_skeys_set;
315
/* Reason: Internal device (only one skeys device for the whole memory) */
316
dc->user_creatable = false;
319
static const TypeInfo qemu_s390_skeys_info = {
320
.name = TYPE_QEMU_S390_SKEYS,
321
.parent = TYPE_S390_SKEYS,
322
.instance_size = sizeof(QEMUS390SKeysState),
323
.class_init = qemu_s390_skeys_class_init,
324
.class_size = sizeof(S390SKeysClass),
327
static void s390_storage_keys_save(QEMUFile *f, void *opaque)
329
S390SKeysState *ss = S390_SKEYS(opaque);
330
S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
331
GuestPhysBlockList guest_phys_blocks;
332
GuestPhysBlock *block;
337
if (!skeyclass->skeys_are_enabled(ss)) {
341
buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
343
error_report("storage key save could not allocate memory");
347
guest_phys_blocks_init(&guest_phys_blocks);
348
guest_phys_blocks_append(&guest_phys_blocks);
350
/* Send each contiguous physical memory range separately. */
351
QTAILQ_FOREACH(block, &guest_phys_blocks.head, next) {
352
assert(QEMU_IS_ALIGNED(block->target_start, TARGET_PAGE_SIZE));
353
assert(QEMU_IS_ALIGNED(block->target_end, TARGET_PAGE_SIZE));
355
gfn = block->target_start / TARGET_PAGE_SIZE;
356
pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
357
qemu_put_be64(f, block->target_start | S390_SKEYS_SAVE_FLAG_SKEYS);
358
qemu_put_be64(f, pages);
361
const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
364
error = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
367
* Create a valid stream with all 0x00 and indicate
368
* S390_SKEYS_SAVE_FLAG_ERROR to the destination.
370
error_report("S390_GET_KEYS error %d", error);
371
memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
375
qemu_put_buffer(f, buf, cur_pages);
385
guest_phys_blocks_free(&guest_phys_blocks);
389
qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_ERROR);
391
qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_EOS);
395
static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
397
S390SKeysState *ss = S390_SKEYS(opaque);
398
S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
402
* Make sure to lazy-enable if required to be done explicitly. No need to
403
* flush any TLB as the VM is not running yet.
405
if (skeyclass->enable_skeys) {
406
skeyclass->enable_skeys(ss);
413
addr = qemu_get_be64(f);
414
flags = addr & ~TARGET_PAGE_MASK;
415
addr &= TARGET_PAGE_MASK;
418
case S390_SKEYS_SAVE_FLAG_SKEYS: {
419
const uint64_t total_count = qemu_get_be64(f);
420
uint64_t handled_count = 0, cur_count;
421
uint64_t cur_gfn = addr / TARGET_PAGE_SIZE;
422
uint8_t *buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
425
error_report("storage key load could not allocate memory");
430
while (handled_count < total_count) {
431
cur_count = MIN(total_count - handled_count,
432
S390_SKEYS_BUFFER_SIZE);
433
qemu_get_buffer(f, buf, cur_count);
435
ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
437
error_report("S390_SET_KEYS error %d", ret);
440
handled_count += cur_count;
441
cur_gfn += cur_count;
446
case S390_SKEYS_SAVE_FLAG_ERROR: {
447
error_report("Storage key data is incomplete");
451
case S390_SKEYS_SAVE_FLAG_EOS:
455
error_report("Unexpected storage key flag data: %#x", flags);
463
static SaveVMHandlers savevm_s390_storage_keys = {
464
.save_state = s390_storage_keys_save,
465
.load_state = s390_storage_keys_load,
468
static void s390_skeys_realize(DeviceState *dev, Error **errp)
470
S390SKeysState *ss = S390_SKEYS(dev);
472
if (ss->migration_enabled) {
473
register_savevm_live(TYPE_S390_SKEYS, 0, 1,
474
&savevm_s390_storage_keys, ss);
478
static Property s390_skeys_props[] = {
479
DEFINE_PROP_BOOL("migration-enabled", S390SKeysState, migration_enabled, true),
480
DEFINE_PROP_END_OF_LIST(),
483
static void s390_skeys_class_init(ObjectClass *oc, void *data)
485
DeviceClass *dc = DEVICE_CLASS(oc);
487
dc->hotpluggable = false;
488
dc->realize = s390_skeys_realize;
489
device_class_set_props(dc, s390_skeys_props);
490
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
493
static const TypeInfo s390_skeys_info = {
494
.name = TYPE_S390_SKEYS,
495
.parent = TYPE_DEVICE,
496
.instance_size = sizeof(S390SKeysState),
497
.class_init = s390_skeys_class_init,
498
.class_size = sizeof(S390SKeysClass),
502
static void qemu_s390_skeys_register_types(void)
504
type_register_static(&s390_skeys_info);
505
type_register_static(&qemu_s390_skeys_info);
508
type_init(qemu_s390_skeys_register_types)