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"
14 #include "hw/boards.h"
15 #include "hw/s390x/storage-keys.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-commands-misc-target.h"
18 #include "qapi/qmp/qdict.h"
19 #include "qemu/error-report.h"
20 #include "sysemu/kvm.h"
21 #include "migration/qemu-file-types.h"
22 #include "migration/register.h"
24 #define S390_SKEYS_BUFFER_SIZE (128 * KiB) /* Room for 128k storage keys */
25 #define S390_SKEYS_SAVE_FLAG_EOS 0x01
26 #define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
27 #define S390_SKEYS_SAVE_FLAG_ERROR 0x04
29 S390SKeysState
*s390_get_skeys_device(void)
33 ss
= S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS
, NULL
));
38 void s390_skeys_init(void)
43 obj
= object_new(TYPE_KVM_S390_SKEYS
);
45 obj
= object_new(TYPE_QEMU_S390_SKEYS
);
47 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS
,
51 qdev_realize(DEVICE(obj
), NULL
, &error_fatal
);
54 static void write_keys(FILE *f
, uint8_t *keys
, uint64_t startgfn
,
55 uint64_t count
, Error
**errp
)
57 uint64_t curpage
= startgfn
;
58 uint64_t maxpage
= curpage
+ count
- 1;
60 for (; curpage
<= maxpage
; curpage
++) {
61 uint8_t acc
= (*keys
& 0xF0) >> 4;
62 int fp
= (*keys
& 0x08);
63 int ref
= (*keys
& 0x04);
64 int ch
= (*keys
& 0x02);
65 int res
= (*keys
& 0x01);
67 fprintf(f
, "page=%03" PRIx64
": key(%d) => ACC=%X, FP=%d, REF=%d,"
68 " ch=%d, reserved=%d\n",
69 curpage
, *keys
, acc
, fp
, ref
, ch
, res
);
74 void hmp_info_skeys(Monitor
*mon
, const QDict
*qdict
)
76 S390SKeysState
*ss
= s390_get_skeys_device();
77 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
78 uint64_t addr
= qdict_get_int(qdict
, "addr");
82 /* Quick check to see if guest is using storage keys*/
83 if (!skeyclass
->skeys_enabled(ss
)) {
84 monitor_printf(mon
, "Error: This guest is not using storage keys\n");
88 r
= skeyclass
->get_skeys(ss
, addr
/ TARGET_PAGE_SIZE
, 1, &key
);
90 monitor_printf(mon
, "Error: %s\n", strerror(-r
));
94 monitor_printf(mon
, " key: 0x%X\n", key
);
97 void hmp_dump_skeys(Monitor
*mon
, const QDict
*qdict
)
99 const char *filename
= qdict_get_str(qdict
, "filename");
102 qmp_dump_skeys(filename
, &err
);
104 error_report_err(err
);
108 void qmp_dump_skeys(const char *filename
, Error
**errp
)
110 S390SKeysState
*ss
= s390_get_skeys_device();
111 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
112 const uint64_t total_count
= ram_size
/ TARGET_PAGE_SIZE
;
113 uint64_t handled_count
= 0, cur_count
;
121 /* Quick check to see if guest is using storage keys*/
122 if (!skeyclass
->skeys_enabled(ss
)) {
123 error_setg(errp
, "This guest is not using storage keys - "
128 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
130 error_setg_file_open(errp
, errno
, filename
);
133 f
= fdopen(fd
, "wb");
136 error_setg_file_open(errp
, errno
, filename
);
140 buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
142 error_setg(errp
, "Could not allocate memory");
146 /* we'll only dump initial memory for now */
147 while (handled_count
< total_count
) {
148 /* Calculate how many keys to ask for & handle overflow case */
149 cur_count
= MIN(total_count
- handled_count
, S390_SKEYS_BUFFER_SIZE
);
151 ret
= skeyclass
->get_skeys(ss
, cur_gfn
, cur_count
, buf
);
153 error_setg(errp
, "get_keys error %d", ret
);
157 /* write keys to stream */
158 write_keys(f
, buf
, cur_gfn
, cur_count
, &lerr
);
163 cur_gfn
+= cur_count
;
164 handled_count
+= cur_count
;
168 error_propagate(errp
, lerr
);
174 static void qemu_s390_skeys_init(Object
*obj
)
176 QEMUS390SKeysState
*skeys
= QEMU_S390_SKEYS(obj
);
177 MachineState
*machine
= MACHINE(qdev_get_machine());
179 skeys
->key_count
= machine
->ram_size
/ TARGET_PAGE_SIZE
;
180 skeys
->keydata
= g_malloc0(skeys
->key_count
);
183 static int qemu_s390_skeys_enabled(S390SKeysState
*ss
)
189 * TODO: for memory hotplug support qemu_s390_skeys_set and qemu_s390_skeys_get
190 * will have to make sure that the given gfn belongs to a memory region and not
193 static int qemu_s390_skeys_set(S390SKeysState
*ss
, uint64_t start_gfn
,
194 uint64_t count
, uint8_t *keys
)
196 QEMUS390SKeysState
*skeydev
= QEMU_S390_SKEYS(ss
);
199 /* Check for uint64 overflow and access beyond end of key data */
200 if (start_gfn
+ count
> skeydev
->key_count
|| start_gfn
+ count
< count
) {
201 error_report("Error: Setting storage keys for page beyond the end "
202 "of memory: gfn=%" PRIx64
" count=%" PRId64
,
207 for (i
= 0; i
< count
; i
++) {
208 skeydev
->keydata
[start_gfn
+ i
] = keys
[i
];
213 static int qemu_s390_skeys_get(S390SKeysState
*ss
, uint64_t start_gfn
,
214 uint64_t count
, uint8_t *keys
)
216 QEMUS390SKeysState
*skeydev
= QEMU_S390_SKEYS(ss
);
219 /* Check for uint64 overflow and access beyond end of key data */
220 if (start_gfn
+ count
> skeydev
->key_count
|| start_gfn
+ count
< count
) {
221 error_report("Error: Getting storage keys for page beyond the end "
222 "of memory: gfn=%" PRIx64
" count=%" PRId64
,
227 for (i
= 0; i
< count
; i
++) {
228 keys
[i
] = skeydev
->keydata
[start_gfn
+ i
];
233 static void qemu_s390_skeys_class_init(ObjectClass
*oc
, void *data
)
235 S390SKeysClass
*skeyclass
= S390_SKEYS_CLASS(oc
);
236 DeviceClass
*dc
= DEVICE_CLASS(oc
);
238 skeyclass
->skeys_enabled
= qemu_s390_skeys_enabled
;
239 skeyclass
->get_skeys
= qemu_s390_skeys_get
;
240 skeyclass
->set_skeys
= qemu_s390_skeys_set
;
242 /* Reason: Internal device (only one skeys device for the whole memory) */
243 dc
->user_creatable
= false;
246 static const TypeInfo qemu_s390_skeys_info
= {
247 .name
= TYPE_QEMU_S390_SKEYS
,
248 .parent
= TYPE_S390_SKEYS
,
249 .instance_init
= qemu_s390_skeys_init
,
250 .instance_size
= sizeof(QEMUS390SKeysState
),
251 .class_init
= qemu_s390_skeys_class_init
,
252 .class_size
= sizeof(S390SKeysClass
),
255 static void s390_storage_keys_save(QEMUFile
*f
, void *opaque
)
257 S390SKeysState
*ss
= S390_SKEYS(opaque
);
258 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
259 uint64_t pages_left
= ram_size
/ TARGET_PAGE_SIZE
;
260 uint64_t read_count
, eos
= S390_SKEYS_SAVE_FLAG_EOS
;
265 if (!skeyclass
->skeys_enabled(ss
)) {
269 buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
271 error_report("storage key save could not allocate memory");
275 /* We only support initial memory. Standby memory is not handled yet. */
276 qemu_put_be64(f
, (cur_gfn
* TARGET_PAGE_SIZE
) | S390_SKEYS_SAVE_FLAG_SKEYS
);
277 qemu_put_be64(f
, pages_left
);
280 read_count
= MIN(pages_left
, S390_SKEYS_BUFFER_SIZE
);
283 error
= skeyclass
->get_skeys(ss
, cur_gfn
, read_count
, buf
);
286 * If error: we want to fill the stream with valid data instead
287 * of stopping early so we pad the stream with 0x00 values and
288 * use S390_SKEYS_SAVE_FLAG_ERROR to indicate failure to the
291 error_report("S390_GET_KEYS error %d", error
);
292 memset(buf
, 0, S390_SKEYS_BUFFER_SIZE
);
293 eos
= S390_SKEYS_SAVE_FLAG_ERROR
;
297 qemu_put_buffer(f
, buf
, read_count
);
298 cur_gfn
+= read_count
;
299 pages_left
-= read_count
;
304 qemu_put_be64(f
, eos
);
307 static int s390_storage_keys_load(QEMUFile
*f
, void *opaque
, int version_id
)
309 S390SKeysState
*ss
= S390_SKEYS(opaque
);
310 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
317 addr
= qemu_get_be64(f
);
318 flags
= addr
& ~TARGET_PAGE_MASK
;
319 addr
&= TARGET_PAGE_MASK
;
322 case S390_SKEYS_SAVE_FLAG_SKEYS
: {
323 const uint64_t total_count
= qemu_get_be64(f
);
324 uint64_t handled_count
= 0, cur_count
;
325 uint64_t cur_gfn
= addr
/ TARGET_PAGE_SIZE
;
326 uint8_t *buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
329 error_report("storage key load could not allocate memory");
334 while (handled_count
< total_count
) {
335 cur_count
= MIN(total_count
- handled_count
,
336 S390_SKEYS_BUFFER_SIZE
);
337 qemu_get_buffer(f
, buf
, cur_count
);
339 ret
= skeyclass
->set_skeys(ss
, cur_gfn
, cur_count
, buf
);
341 error_report("S390_SET_KEYS error %d", ret
);
344 handled_count
+= cur_count
;
345 cur_gfn
+= cur_count
;
350 case S390_SKEYS_SAVE_FLAG_ERROR
: {
351 error_report("Storage key data is incomplete");
355 case S390_SKEYS_SAVE_FLAG_EOS
:
359 error_report("Unexpected storage key flag data: %#x", flags
);
367 static inline bool s390_skeys_get_migration_enabled(Object
*obj
, Error
**errp
)
369 S390SKeysState
*ss
= S390_SKEYS(obj
);
371 return ss
->migration_enabled
;
374 static SaveVMHandlers savevm_s390_storage_keys
= {
375 .save_state
= s390_storage_keys_save
,
376 .load_state
= s390_storage_keys_load
,
379 static inline void s390_skeys_set_migration_enabled(Object
*obj
, bool value
,
382 S390SKeysState
*ss
= S390_SKEYS(obj
);
384 /* Prevent double registration of savevm handler */
385 if (ss
->migration_enabled
== value
) {
389 ss
->migration_enabled
= value
;
391 if (ss
->migration_enabled
) {
392 register_savevm_live(TYPE_S390_SKEYS
, 0, 1,
393 &savevm_s390_storage_keys
, ss
);
395 unregister_savevm(VMSTATE_IF(ss
), TYPE_S390_SKEYS
, ss
);
399 static void s390_skeys_instance_init(Object
*obj
)
401 object_property_add_bool(obj
, "migration-enabled",
402 s390_skeys_get_migration_enabled
,
403 s390_skeys_set_migration_enabled
);
404 object_property_set_bool(obj
, "migration-enabled", true, NULL
);
407 static void s390_skeys_class_init(ObjectClass
*oc
, void *data
)
409 DeviceClass
*dc
= DEVICE_CLASS(oc
);
411 dc
->hotpluggable
= false;
412 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
415 static const TypeInfo s390_skeys_info
= {
416 .name
= TYPE_S390_SKEYS
,
417 .parent
= TYPE_DEVICE
,
418 .instance_init
= s390_skeys_instance_init
,
419 .instance_size
= sizeof(S390SKeysState
),
420 .class_init
= s390_skeys_class_init
,
421 .class_size
= sizeof(S390SKeysClass
),
425 static void qemu_s390_skeys_register_types(void)
427 type_register_static(&s390_skeys_info
);
428 type_register_static(&qemu_s390_skeys_info
);
431 type_init(qemu_s390_skeys_register_types
)