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 "hw/boards.h"
13 #include "qmp-commands.h"
14 #include "migration/qemu-file.h"
15 #include "hw/s390x/storage-keys.h"
16 #include "qemu/error-report.h"
18 #define S390_SKEYS_BUFFER_SIZE 131072 /* Room for 128k storage keys */
19 #define S390_SKEYS_SAVE_FLAG_EOS 0x01
20 #define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
21 #define S390_SKEYS_SAVE_FLAG_ERROR 0x04
23 S390SKeysState
*s390_get_skeys_device(void)
27 ss
= S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS
, NULL
));
32 void s390_skeys_init(void)
37 obj
= object_new(TYPE_KVM_S390_SKEYS
);
39 obj
= object_new(TYPE_QEMU_S390_SKEYS
);
41 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS
,
45 qdev_init_nofail(DEVICE(obj
));
48 static void write_keys(QEMUFile
*f
, uint8_t *keys
, uint64_t startgfn
,
49 uint64_t count
, Error
**errp
)
51 uint64_t curpage
= startgfn
;
52 uint64_t maxpage
= curpage
+ count
- 1;
53 const char *fmt
= "page=%03" PRIx64
": key(%d) => ACC=%X, FP=%d, REF=%d,"
54 " ch=%d, reserved=%d\n";
58 for (; curpage
<= maxpage
; curpage
++) {
59 uint8_t acc
= (*keys
& 0xF0) >> 4;
60 int fp
= (*keys
& 0x08);
61 int ref
= (*keys
& 0x04);
62 int ch
= (*keys
& 0x02);
63 int res
= (*keys
& 0x01);
65 len
= snprintf(buf
, sizeof(buf
), fmt
, curpage
,
66 *keys
, acc
, fp
, ref
, ch
, res
);
67 assert(len
< sizeof(buf
));
68 qemu_put_buffer(f
, (uint8_t *)buf
, len
);
73 void hmp_info_skeys(Monitor
*mon
, const QDict
*qdict
)
75 S390SKeysState
*ss
= s390_get_skeys_device();
76 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
77 uint64_t addr
= qdict_get_int(qdict
, "addr");
81 /* Quick check to see if guest is using storage keys*/
82 if (!skeyclass
->skeys_enabled(ss
)) {
83 monitor_printf(mon
, "Error: This guest is not using storage keys\n");
87 r
= skeyclass
->get_skeys(ss
, addr
/ TARGET_PAGE_SIZE
, 1, &key
);
89 monitor_printf(mon
, "Error: %s\n", strerror(-r
));
93 monitor_printf(mon
, " key: 0x%X\n", key
);
96 void hmp_dump_skeys(Monitor
*mon
, const QDict
*qdict
)
98 const char *filename
= qdict_get_str(qdict
, "filename");
101 qmp_dump_skeys(filename
, &err
);
103 monitor_printf(mon
, "%s\n", error_get_pretty(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
;
120 /* Quick check to see if guest is using storage keys*/
121 if (!skeyclass
->skeys_enabled(ss
)) {
122 error_setg(errp
, "This guest is not using storage keys - "
127 f
= qemu_fopen(filename
, "wb");
129 error_setg_file_open(errp
, errno
, filename
);
133 buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
135 error_setg(errp
, "Could not allocate memory");
139 /* we'll only dump initial memory for now */
140 while (handled_count
< total_count
) {
141 /* Calculate how many keys to ask for & handle overflow case */
142 cur_count
= MIN(total_count
- handled_count
, S390_SKEYS_BUFFER_SIZE
);
144 ret
= skeyclass
->get_skeys(ss
, cur_gfn
, cur_count
, buf
);
146 error_setg(errp
, "get_keys error %d", ret
);
150 /* write keys to stream */
151 write_keys(f
, buf
, cur_gfn
, cur_count
, &lerr
);
156 cur_gfn
+= cur_count
;
157 handled_count
+= cur_count
;
161 error_propagate(errp
, lerr
);
167 static void qemu_s390_skeys_init(Object
*obj
)
169 QEMUS390SKeysState
*skeys
= QEMU_S390_SKEYS(obj
);
170 MachineState
*machine
= MACHINE(qdev_get_machine());
172 skeys
->key_count
= machine
->maxram_size
/ TARGET_PAGE_SIZE
;
173 skeys
->keydata
= g_malloc0(skeys
->key_count
);
176 static int qemu_s390_skeys_enabled(S390SKeysState
*ss
)
182 * TODO: for memory hotplug support qemu_s390_skeys_set and qemu_s390_skeys_get
183 * will have to make sure that the given gfn belongs to a memory region and not
186 static int qemu_s390_skeys_set(S390SKeysState
*ss
, uint64_t start_gfn
,
187 uint64_t count
, uint8_t *keys
)
189 QEMUS390SKeysState
*skeydev
= QEMU_S390_SKEYS(ss
);
192 /* Check for uint64 overflow and access beyond end of key data */
193 if (start_gfn
+ count
> skeydev
->key_count
|| start_gfn
+ count
< count
) {
194 error_report("Error: Setting storage keys for page beyond the end "
195 "of memory: gfn=%" PRIx64
" count=%" PRId64
"\n", start_gfn
,
200 for (i
= 0; i
< count
; i
++) {
201 skeydev
->keydata
[start_gfn
+ i
] = keys
[i
];
206 static int qemu_s390_skeys_get(S390SKeysState
*ss
, uint64_t start_gfn
,
207 uint64_t count
, uint8_t *keys
)
209 QEMUS390SKeysState
*skeydev
= QEMU_S390_SKEYS(ss
);
212 /* Check for uint64 overflow and access beyond end of key data */
213 if (start_gfn
+ count
> skeydev
->key_count
|| start_gfn
+ count
< count
) {
214 error_report("Error: Getting storage keys for page beyond the end "
215 "of memory: gfn=%" PRIx64
" count=%" PRId64
"\n", start_gfn
,
220 for (i
= 0; i
< count
; i
++) {
221 keys
[i
] = skeydev
->keydata
[start_gfn
+ i
];
226 static void qemu_s390_skeys_class_init(ObjectClass
*oc
, void *data
)
228 S390SKeysClass
*skeyclass
= S390_SKEYS_CLASS(oc
);
230 skeyclass
->skeys_enabled
= qemu_s390_skeys_enabled
;
231 skeyclass
->get_skeys
= qemu_s390_skeys_get
;
232 skeyclass
->set_skeys
= qemu_s390_skeys_set
;
235 static const TypeInfo qemu_s390_skeys_info
= {
236 .name
= TYPE_QEMU_S390_SKEYS
,
237 .parent
= TYPE_S390_SKEYS
,
238 .instance_init
= qemu_s390_skeys_init
,
239 .instance_size
= sizeof(QEMUS390SKeysState
),
240 .class_init
= qemu_s390_skeys_class_init
,
241 .instance_size
= sizeof(S390SKeysClass
),
244 static void s390_storage_keys_save(QEMUFile
*f
, void *opaque
)
246 S390SKeysState
*ss
= S390_SKEYS(opaque
);
247 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
248 uint64_t pages_left
= ram_size
/ TARGET_PAGE_SIZE
;
249 uint64_t read_count
, eos
= S390_SKEYS_SAVE_FLAG_EOS
;
254 if (!skeyclass
->skeys_enabled(ss
)) {
258 buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
260 error_report("storage key save could not allocate memory\n");
264 /* We only support initial memory. Standby memory is not handled yet. */
265 qemu_put_be64(f
, (cur_gfn
* TARGET_PAGE_SIZE
) | S390_SKEYS_SAVE_FLAG_SKEYS
);
266 qemu_put_be64(f
, pages_left
);
269 read_count
= MIN(pages_left
, S390_SKEYS_BUFFER_SIZE
);
272 error
= skeyclass
->get_skeys(ss
, cur_gfn
, read_count
, buf
);
275 * If error: we want to fill the stream with valid data instead
276 * of stopping early so we pad the stream with 0x00 values and
277 * use S390_SKEYS_SAVE_FLAG_ERROR to indicate failure to the
280 error_report("S390_GET_KEYS error %d\n", error
);
281 memset(buf
, 0, S390_SKEYS_BUFFER_SIZE
);
282 eos
= S390_SKEYS_SAVE_FLAG_ERROR
;
286 qemu_put_buffer(f
, buf
, read_count
);
287 cur_gfn
+= read_count
;
288 pages_left
-= read_count
;
293 qemu_put_be64(f
, eos
);
296 static int s390_storage_keys_load(QEMUFile
*f
, void *opaque
, int version_id
)
298 S390SKeysState
*ss
= S390_SKEYS(opaque
);
299 S390SKeysClass
*skeyclass
= S390_SKEYS_GET_CLASS(ss
);
306 addr
= qemu_get_be64(f
);
307 flags
= addr
& ~TARGET_PAGE_MASK
;
308 addr
&= TARGET_PAGE_MASK
;
311 case S390_SKEYS_SAVE_FLAG_SKEYS
: {
312 const uint64_t total_count
= qemu_get_be64(f
);
313 uint64_t handled_count
= 0, cur_count
;
314 uint64_t cur_gfn
= addr
/ TARGET_PAGE_SIZE
;
315 uint8_t *buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
318 error_report("storage key load could not allocate memory\n");
323 while (handled_count
< total_count
) {
324 cur_count
= MIN(total_count
- handled_count
,
325 S390_SKEYS_BUFFER_SIZE
);
326 qemu_get_buffer(f
, buf
, cur_count
);
328 ret
= skeyclass
->set_skeys(ss
, cur_gfn
, cur_count
, buf
);
330 error_report("S390_SET_KEYS error %d\n", ret
);
333 handled_count
+= cur_count
;
334 cur_gfn
+= cur_count
;
339 case S390_SKEYS_SAVE_FLAG_ERROR
: {
340 error_report("Storage key data is incomplete");
344 case S390_SKEYS_SAVE_FLAG_EOS
:
348 error_report("Unexpected storage key flag data: %#x", flags
);
356 static inline bool s390_skeys_get_migration_enabled(Object
*obj
, Error
**errp
)
358 S390SKeysState
*ss
= S390_SKEYS(obj
);
360 return ss
->migration_enabled
;
363 static inline void s390_skeys_set_migration_enabled(Object
*obj
, bool value
,
366 S390SKeysState
*ss
= S390_SKEYS(obj
);
368 /* Prevent double registration of savevm handler */
369 if (ss
->migration_enabled
== value
) {
373 ss
->migration_enabled
= value
;
375 if (ss
->migration_enabled
) {
376 register_savevm(NULL
, TYPE_S390_SKEYS
, 0, 1, s390_storage_keys_save
,
377 s390_storage_keys_load
, ss
);
379 unregister_savevm(DEVICE(ss
), TYPE_S390_SKEYS
, ss
);
383 static void s390_skeys_instance_init(Object
*obj
)
385 object_property_add_bool(obj
, "migration-enabled",
386 s390_skeys_get_migration_enabled
,
387 s390_skeys_set_migration_enabled
, NULL
);
388 object_property_set_bool(obj
, true, "migration-enabled", NULL
);
391 static void s390_skeys_class_init(ObjectClass
*oc
, void *data
)
393 DeviceClass
*dc
= DEVICE_CLASS(oc
);
395 dc
->hotpluggable
= false;
396 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
399 static const TypeInfo s390_skeys_info
= {
400 .name
= TYPE_S390_SKEYS
,
401 .parent
= TYPE_DEVICE
,
402 .instance_init
= s390_skeys_instance_init
,
403 .instance_size
= sizeof(S390SKeysState
),
404 .class_init
= s390_skeys_class_init
,
405 .class_size
= sizeof(S390SKeysClass
),
409 static void qemu_s390_skeys_register_types(void)
411 type_register_static(&s390_skeys_info
);
412 type_register_static(&qemu_s390_skeys_info
);
415 type_init(qemu_s390_skeys_register_types
)