]> git.proxmox.com Git - mirror_qemu.git/blame - hw/s390x/s390-skeys.c
migration/tls: Use qcrypto_tls_creds_check_endpoint()
[mirror_qemu.git] / hw / s390x / s390-skeys.c
CommitLineData
0efe406c
JH
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
9615495a 12#include "qemu/osdep.h"
393fc4c7 13#include "qemu/units.h"
0efe406c
JH
14#include "hw/boards.h"
15#include "hw/s390x/storage-keys.h"
e688df6b 16#include "qapi/error.h"
b0227cdb 17#include "qapi/qapi-commands-misc-target.h"
452fcdbc 18#include "qapi/qmp/qdict.h"
0efe406c 19#include "qemu/error-report.h"
bd3f16ac 20#include "sysemu/kvm.h"
ca77ee28 21#include "migration/qemu-file-types.h"
f2a8f0a6 22#include "migration/register.h"
0efe406c 23
393fc4c7 24#define S390_SKEYS_BUFFER_SIZE (128 * KiB) /* Room for 128k storage keys */
186208fa
JH
25#define S390_SKEYS_SAVE_FLAG_EOS 0x01
26#define S390_SKEYS_SAVE_FLAG_SKEYS 0x02
27#define S390_SKEYS_SAVE_FLAG_ERROR 0x04
7ee0c3e3 28
0efe406c
JH
29S390SKeysState *s390_get_skeys_device(void)
30{
31 S390SKeysState *ss;
32
33 ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
34 assert(ss);
35 return ss;
36}
37
38void s390_skeys_init(void)
39{
40 Object *obj;
41
42 if (kvm_enabled()) {
43 obj = object_new(TYPE_KVM_S390_SKEYS);
44 } else {
45 obj = object_new(TYPE_QEMU_S390_SKEYS);
46 }
47 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
d2623129 48 obj);
0efe406c
JH
49 object_unref(obj);
50
ce189ab2 51 qdev_realize(DEVICE(obj), NULL, &error_fatal);
0efe406c
JH
52}
53
1fd791f0 54static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
7ee0c3e3
JH
55 uint64_t count, Error **errp)
56{
57 uint64_t curpage = startgfn;
58 uint64_t maxpage = curpage + count - 1;
7ee0c3e3
JH
59
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);
66
1fd791f0
DB
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);
7ee0c3e3
JH
70 keys++;
71 }
72}
73
a08f0081
JH
74void hmp_info_skeys(Monitor *mon, const QDict *qdict)
75{
76 S390SKeysState *ss = s390_get_skeys_device();
77 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
78 uint64_t addr = qdict_get_int(qdict, "addr");
79 uint8_t key;
80 int r;
81
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");
85 return;
86 }
87
88 r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
89 if (r < 0) {
90 monitor_printf(mon, "Error: %s\n", strerror(-r));
91 return;
92 }
93
94 monitor_printf(mon, " key: 0x%X\n", key);
95}
96
a4538a5c
JH
97void hmp_dump_skeys(Monitor *mon, const QDict *qdict)
98{
99 const char *filename = qdict_get_str(qdict, "filename");
100 Error *err = NULL;
101
102 qmp_dump_skeys(filename, &err);
103 if (err) {
193227f9 104 error_report_err(err);
a4538a5c
JH
105 }
106}
107
7ee0c3e3
JH
108void qmp_dump_skeys(const char *filename, Error **errp)
109{
110 S390SKeysState *ss = s390_get_skeys_device();
111 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
382a04af
PB
112 MachineState *ms = MACHINE(qdev_get_machine());
113 const uint64_t total_count = ms->ram_size / TARGET_PAGE_SIZE;
7ee0c3e3
JH
114 uint64_t handled_count = 0, cur_count;
115 Error *lerr = NULL;
116 vaddr cur_gfn = 0;
117 uint8_t *buf;
118 int ret;
1fd791f0
DB
119 int fd;
120 FILE *f;
7ee0c3e3
JH
121
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 - "
125 "nothing to dump");
126 return;
127 }
128
448058aa 129 fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1fd791f0
DB
130 if (fd < 0) {
131 error_setg_file_open(errp, errno, filename);
132 return;
133 }
134 f = fdopen(fd, "wb");
7ee0c3e3 135 if (!f) {
1fd791f0 136 close(fd);
7ee0c3e3
JH
137 error_setg_file_open(errp, errno, filename);
138 return;
139 }
140
141 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
142 if (!buf) {
143 error_setg(errp, "Could not allocate memory");
144 goto out;
145 }
146
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);
151
152 ret = skeyclass->get_skeys(ss, cur_gfn, cur_count, buf);
153 if (ret < 0) {
154 error_setg(errp, "get_keys error %d", ret);
155 goto out_free;
156 }
157
158 /* write keys to stream */
159 write_keys(f, buf, cur_gfn, cur_count, &lerr);
160 if (lerr) {
161 goto out_free;
162 }
163
164 cur_gfn += cur_count;
165 handled_count += cur_count;
166 }
167
168out_free:
169 error_propagate(errp, lerr);
170 g_free(buf);
171out:
1fd791f0 172 fclose(f);
7ee0c3e3
JH
173}
174
0efe406c
JH
175static void qemu_s390_skeys_init(Object *obj)
176{
177 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(obj);
178 MachineState *machine = MACHINE(qdev_get_machine());
179
5c30ef93 180 skeys->key_count = machine->ram_size / TARGET_PAGE_SIZE;
0efe406c
JH
181 skeys->keydata = g_malloc0(skeys->key_count);
182}
183
184static int qemu_s390_skeys_enabled(S390SKeysState *ss)
185{
186 return 1;
187}
188
189/*
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
192 * a memory hole.
193 */
194static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
195 uint64_t count, uint8_t *keys)
196{
197 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
198 int i;
199
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 "
9af9e0fe
MA
203 "of memory: gfn=%" PRIx64 " count=%" PRId64,
204 start_gfn, count);
0efe406c
JH
205 return -EINVAL;
206 }
207
208 for (i = 0; i < count; i++) {
209 skeydev->keydata[start_gfn + i] = keys[i];
210 }
211 return 0;
212}
213
214static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
215 uint64_t count, uint8_t *keys)
216{
217 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
218 int i;
219
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 "
9af9e0fe
MA
223 "of memory: gfn=%" PRIx64 " count=%" PRId64,
224 start_gfn, count);
0efe406c
JH
225 return -EINVAL;
226 }
227
228 for (i = 0; i < count; i++) {
229 keys[i] = skeydev->keydata[start_gfn + i];
230 }
231 return 0;
232}
233
234static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
235{
236 S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
574ee06d 237 DeviceClass *dc = DEVICE_CLASS(oc);
0efe406c
JH
238
239 skeyclass->skeys_enabled = qemu_s390_skeys_enabled;
240 skeyclass->get_skeys = qemu_s390_skeys_get;
241 skeyclass->set_skeys = qemu_s390_skeys_set;
574ee06d
TH
242
243 /* Reason: Internal device (only one skeys device for the whole memory) */
244 dc->user_creatable = false;
0efe406c
JH
245}
246
247static 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,
2f2b0c66 253 .class_size = sizeof(S390SKeysClass),
0efe406c
JH
254};
255
186208fa
JH
256static void s390_storage_keys_save(QEMUFile *f, void *opaque)
257{
258 S390SKeysState *ss = S390_SKEYS(opaque);
259 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
382a04af
PB
260 MachineState *ms = MACHINE(qdev_get_machine());
261 uint64_t pages_left = ms->ram_size / TARGET_PAGE_SIZE;
186208fa
JH
262 uint64_t read_count, eos = S390_SKEYS_SAVE_FLAG_EOS;
263 vaddr cur_gfn = 0;
264 int error = 0;
265 uint8_t *buf;
266
267 if (!skeyclass->skeys_enabled(ss)) {
268 goto end_stream;
269 }
270
271 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
272 if (!buf) {
9af9e0fe 273 error_report("storage key save could not allocate memory");
186208fa
JH
274 goto end_stream;
275 }
276
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);
280
281 while (pages_left) {
282 read_count = MIN(pages_left, S390_SKEYS_BUFFER_SIZE);
283
284 if (!error) {
285 error = skeyclass->get_skeys(ss, cur_gfn, read_count, buf);
286 if (error) {
287 /*
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
291 * reading side.
292 */
9af9e0fe 293 error_report("S390_GET_KEYS error %d", error);
186208fa
JH
294 memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
295 eos = S390_SKEYS_SAVE_FLAG_ERROR;
296 }
297 }
298
299 qemu_put_buffer(f, buf, read_count);
300 cur_gfn += read_count;
301 pages_left -= read_count;
302 }
303
304 g_free(buf);
305end_stream:
306 qemu_put_be64(f, eos);
307}
308
309static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
310{
311 S390SKeysState *ss = S390_SKEYS(opaque);
312 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
313 int ret = 0;
314
315 while (!ret) {
316 ram_addr_t addr;
317 int flags;
318
319 addr = qemu_get_be64(f);
320 flags = addr & ~TARGET_PAGE_MASK;
321 addr &= TARGET_PAGE_MASK;
322
323 switch (flags) {
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);
329
330 if (!buf) {
9af9e0fe 331 error_report("storage key load could not allocate memory");
186208fa
JH
332 ret = -ENOMEM;
333 break;
334 }
335
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);
340
341 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
342 if (ret < 0) {
9af9e0fe 343 error_report("S390_SET_KEYS error %d", ret);
186208fa
JH
344 break;
345 }
346 handled_count += cur_count;
347 cur_gfn += cur_count;
348 }
349 g_free(buf);
350 break;
351 }
352 case S390_SKEYS_SAVE_FLAG_ERROR: {
353 error_report("Storage key data is incomplete");
354 ret = -EINVAL;
355 break;
356 }
357 case S390_SKEYS_SAVE_FLAG_EOS:
358 /* normal exit */
359 return 0;
360 default:
361 error_report("Unexpected storage key flag data: %#x", flags);
362 ret = -EINVAL;
363 }
364 }
365
366 return ret;
367}
368
9ef40173
JH
369static inline bool s390_skeys_get_migration_enabled(Object *obj, Error **errp)
370{
371 S390SKeysState *ss = S390_SKEYS(obj);
372
373 return ss->migration_enabled;
374}
375
1b6e7482
LV
376static SaveVMHandlers savevm_s390_storage_keys = {
377 .save_state = s390_storage_keys_save,
378 .load_state = s390_storage_keys_load,
379};
380
9ef40173
JH
381static inline void s390_skeys_set_migration_enabled(Object *obj, bool value,
382 Error **errp)
186208fa
JH
383{
384 S390SKeysState *ss = S390_SKEYS(obj);
385
9ef40173
JH
386 /* Prevent double registration of savevm handler */
387 if (ss->migration_enabled == value) {
388 return;
389 }
390
391 ss->migration_enabled = value;
392
393 if (ss->migration_enabled) {
ce62df53 394 register_savevm_live(TYPE_S390_SKEYS, 0, 1,
1b6e7482 395 &savevm_s390_storage_keys, ss);
9ef40173 396 } else {
3cad405b 397 unregister_savevm(VMSTATE_IF(ss), TYPE_S390_SKEYS, ss);
9ef40173
JH
398 }
399}
400
401static void s390_skeys_instance_init(Object *obj)
402{
403 object_property_add_bool(obj, "migration-enabled",
404 s390_skeys_get_migration_enabled,
d2623129 405 s390_skeys_set_migration_enabled);
5325cc34 406 object_property_set_bool(obj, "migration-enabled", true, NULL);
186208fa
JH
407}
408
0efe406c
JH
409static void s390_skeys_class_init(ObjectClass *oc, void *data)
410{
411 DeviceClass *dc = DEVICE_CLASS(oc);
412
413 dc->hotpluggable = false;
414 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
415}
416
417static const TypeInfo s390_skeys_info = {
418 .name = TYPE_S390_SKEYS,
419 .parent = TYPE_DEVICE,
186208fa 420 .instance_init = s390_skeys_instance_init,
0efe406c
JH
421 .instance_size = sizeof(S390SKeysState),
422 .class_init = s390_skeys_class_init,
423 .class_size = sizeof(S390SKeysClass),
424 .abstract = true,
425};
426
427static void qemu_s390_skeys_register_types(void)
428{
429 type_register_static(&s390_skeys_info);
430 type_register_static(&qemu_s390_skeys_info);
431}
432
433type_init(qemu_s390_skeys_register_types)