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 MachineState
*ms
= MACHINE(qdev_get_machine());
113 const uint64_t total_count
= ms
->ram_size
/ TARGET_PAGE_SIZE
;
114 uint64_t handled_count
= 0, cur_count
;
122 /* Quick check to see if guest is using storage keys*/
123 if (!skeyclass
->skeys_enabled(ss
)) {
124 error_setg(errp
, "This guest is not using storage keys - "
129 fd
= qemu_open_old(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
131 error_setg_file_open(errp
, errno
, filename
);
134 f
= fdopen(fd
, "wb");
137 error_setg_file_open(errp
, errno
, filename
);
141 buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
143 error_setg(errp
, "Could not allocate memory");
147 /* we'll only dump initial memory for now */
148 while (handled_count
< total_count
) {
149 /* Calculate how many keys to ask for & handle overflow case */
150 cur_count
= MIN(total_count
- handled_count
, S390_SKEYS_BUFFER_SIZE
);
152 ret
= skeyclass
->get_skeys(ss
, cur_gfn
, cur_count
, buf
);
154 error_setg(errp
, "get_keys error %d", ret
);
158 /* write keys to stream */
159 write_keys(f
, buf
, cur_gfn
, cur_count
, &lerr
);
164 cur_gfn
+= cur_count
;
165 handled_count
+= cur_count
;
169 error_propagate(errp
, lerr
);
175 static void qemu_s390_skeys_init(Object
*obj
)
177 QEMUS390SKeysState
*skeys
= QEMU_S390_SKEYS(obj
);
178 MachineState
*machine
= MACHINE(qdev_get_machine());
180 skeys
->key_count
= machine
->ram_size
/ TARGET_PAGE_SIZE
;
181 skeys
->keydata
= g_malloc0(skeys
->key_count
);
184 static int qemu_s390_skeys_enabled(S390SKeysState
*ss
)
190 * TODO: for memory hotplug support qemu_s390_skeys_set and qemu_s390_skeys_get
191 * will have to make sure that the given gfn belongs to a memory region and not
194 static int qemu_s390_skeys_set(S390SKeysState
*ss
, uint64_t start_gfn
,
195 uint64_t count
, uint8_t *keys
)
197 QEMUS390SKeysState
*skeydev
= QEMU_S390_SKEYS(ss
);
200 /* Check for uint64 overflow and access beyond end of key data */
201 if (start_gfn
+ count
> skeydev
->key_count
|| start_gfn
+ count
< count
) {
202 error_report("Error: Setting storage keys for page beyond the end "
203 "of memory: gfn=%" PRIx64
" count=%" PRId64
,
208 for (i
= 0; i
< count
; i
++) {
209 skeydev
->keydata
[start_gfn
+ i
] = keys
[i
];
214 static int qemu_s390_skeys_get(S390SKeysState
*ss
, uint64_t start_gfn
,
215 uint64_t count
, uint8_t *keys
)
217 QEMUS390SKeysState
*skeydev
= QEMU_S390_SKEYS(ss
);
220 /* Check for uint64 overflow and access beyond end of key data */
221 if (start_gfn
+ count
> skeydev
->key_count
|| start_gfn
+ count
< count
) {
222 error_report("Error: Getting storage keys for page beyond the end "
223 "of memory: gfn=%" PRIx64
" count=%" PRId64
,
228 for (i
= 0; i
< count
; i
++) {
229 keys
[i
] = skeydev
->keydata
[start_gfn
+ i
];
234 static void qemu_s390_skeys_class_init(ObjectClass
*oc
, void *data
)
236 S390SKeysClass
*skeyclass
= S390_SKEYS_CLASS(oc
);
237 DeviceClass
*dc
= DEVICE_CLASS(oc
);
239 skeyclass
->skeys_enabled
= qemu_s390_skeys_enabled
;
240 skeyclass
->get_skeys
= qemu_s390_skeys_get
;
241 skeyclass
->set_skeys
= qemu_s390_skeys_set
;
243 /* Reason: Internal device (only one skeys device for the whole memory) */
244 dc
->user_creatable
= false;
247 static const TypeInfo qemu_s390_skeys_info
= {
248 .name
= TYPE_QEMU_S390_SKEYS
,
249 .parent
= TYPE_S390_SKEYS
,
250 .instance_init
= qemu_s390_skeys_init
,
251 .instance_size
= sizeof(QEMUS390SKeysState
),
252 .class_init
= qemu_s390_skeys_class_init
,
253 .class_size
= sizeof(S390SKeysClass
),
256 static void s390_storage_keys_save(QEMUFile
*f
, void *opaque
)
258 S390SKeysState
*ss
= S390_SKEYS(opaque
);
259 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
260 MachineState
*ms
= MACHINE(qdev_get_machine());
261 uint64_t pages_left
= ms
->ram_size
/ TARGET_PAGE_SIZE
;
262 uint64_t read_count
, eos
= S390_SKEYS_SAVE_FLAG_EOS
;
267 if (!skeyclass
->skeys_enabled(ss
)) {
271 buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
273 error_report("storage key save could not allocate memory");
277 /* We only support initial memory. Standby memory is not handled yet. */
278 qemu_put_be64(f
, (cur_gfn
* TARGET_PAGE_SIZE
) | S390_SKEYS_SAVE_FLAG_SKEYS
);
279 qemu_put_be64(f
, pages_left
);
282 read_count
= MIN(pages_left
, S390_SKEYS_BUFFER_SIZE
);
285 error
= skeyclass
->get_skeys(ss
, cur_gfn
, read_count
, buf
);
288 * If error: we want to fill the stream with valid data instead
289 * of stopping early so we pad the stream with 0x00 values and
290 * use S390_SKEYS_SAVE_FLAG_ERROR to indicate failure to the
293 error_report("S390_GET_KEYS error %d", error
);
294 memset(buf
, 0, S390_SKEYS_BUFFER_SIZE
);
295 eos
= S390_SKEYS_SAVE_FLAG_ERROR
;
299 qemu_put_buffer(f
, buf
, read_count
);
300 cur_gfn
+= read_count
;
301 pages_left
-= read_count
;
306 qemu_put_be64(f
, eos
);
309 static int s390_storage_keys_load(QEMUFile
*f
, void *opaque
, int version_id
)
311 S390SKeysState
*ss
= S390_SKEYS(opaque
);
312 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
319 addr
= qemu_get_be64(f
);
320 flags
= addr
& ~TARGET_PAGE_MASK
;
321 addr
&= TARGET_PAGE_MASK
;
324 case S390_SKEYS_SAVE_FLAG_SKEYS
: {
325 const uint64_t total_count
= qemu_get_be64(f
);
326 uint64_t handled_count
= 0, cur_count
;
327 uint64_t cur_gfn
= addr
/ TARGET_PAGE_SIZE
;
328 uint8_t *buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
331 error_report("storage key load could not allocate memory");
336 while (handled_count
< total_count
) {
337 cur_count
= MIN(total_count
- handled_count
,
338 S390_SKEYS_BUFFER_SIZE
);
339 qemu_get_buffer(f
, buf
, cur_count
);
341 ret
= skeyclass
->set_skeys(ss
, cur_gfn
, cur_count
, buf
);
343 error_report("S390_SET_KEYS error %d", ret
);
346 handled_count
+= cur_count
;
347 cur_gfn
+= cur_count
;
352 case S390_SKEYS_SAVE_FLAG_ERROR
: {
353 error_report("Storage key data is incomplete");
357 case S390_SKEYS_SAVE_FLAG_EOS
:
361 error_report("Unexpected storage key flag data: %#x", flags
);
369 static inline bool s390_skeys_get_migration_enabled(Object
*obj
, Error
**errp
)
371 S390SKeysState
*ss
= S390_SKEYS(obj
);
373 return ss
->migration_enabled
;
376 static SaveVMHandlers savevm_s390_storage_keys
= {
377 .save_state
= s390_storage_keys_save
,
378 .load_state
= s390_storage_keys_load
,
381 static inline void s390_skeys_set_migration_enabled(Object
*obj
, bool value
,
384 S390SKeysState
*ss
= S390_SKEYS(obj
);
386 /* Prevent double registration of savevm handler */
387 if (ss
->migration_enabled
== value
) {
391 ss
->migration_enabled
= value
;
393 if (ss
->migration_enabled
) {
394 register_savevm_live(TYPE_S390_SKEYS
, 0, 1,
395 &savevm_s390_storage_keys
, ss
);
397 unregister_savevm(VMSTATE_IF(ss
), TYPE_S390_SKEYS
, ss
);
401 static void s390_skeys_instance_init(Object
*obj
)
403 object_property_add_bool(obj
, "migration-enabled",
404 s390_skeys_get_migration_enabled
,
405 s390_skeys_set_migration_enabled
);
406 object_property_set_bool(obj
, "migration-enabled", true, NULL
);
409 static void s390_skeys_class_init(ObjectClass
*oc
, void *data
)
411 DeviceClass
*dc
= DEVICE_CLASS(oc
);
413 dc
->hotpluggable
= false;
414 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
417 static const TypeInfo s390_skeys_info
= {
418 .name
= TYPE_S390_SKEYS
,
419 .parent
= TYPE_DEVICE
,
420 .instance_init
= s390_skeys_instance_init
,
421 .instance_size
= sizeof(S390SKeysState
),
422 .class_init
= s390_skeys_class_init
,
423 .class_size
= sizeof(S390SKeysClass
),
427 static void qemu_s390_skeys_register_types(void)
429 type_register_static(&s390_skeys_info
);
430 type_register_static(&qemu_s390_skeys_info
);
433 type_init(qemu_s390_skeys_register_types
)