xref: /openbmc/qemu/hw/s390x/s390-skeys.c (revision b9268953)
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 
12 #include "qemu/osdep.h"
13 #include "qemu/units.h"
14 #include "hw/boards.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"
26 #include "trace.h"
27 
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
32 
s390_get_skeys_device(void)33 S390SKeysState *s390_get_skeys_device(void)
34 {
35     S390SKeysState *ss;
36 
37     ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
38     assert(ss);
39     return ss;
40 }
41 
s390_skeys_init(void)42 void s390_skeys_init(void)
43 {
44     Object *obj;
45 
46     if (kvm_enabled()) {
47         obj = object_new(TYPE_KVM_S390_SKEYS);
48     } else {
49         obj = object_new(TYPE_QEMU_S390_SKEYS);
50     }
51     object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
52                               obj);
53     object_unref(obj);
54 
55     qdev_realize(DEVICE(obj), NULL, &error_fatal);
56 }
57 
s390_skeys_get(S390SKeysState * ks,uint64_t start_gfn,uint64_t count,uint8_t * keys)58 int s390_skeys_get(S390SKeysState *ks, uint64_t start_gfn,
59                    uint64_t count, uint8_t *keys)
60 {
61     S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
62     int rc;
63 
64     rc = kc->get_skeys(ks, start_gfn, count, keys);
65     if (rc) {
66         trace_s390_skeys_get_nonzero(rc);
67     }
68     return rc;
69 }
70 
s390_skeys_set(S390SKeysState * ks,uint64_t start_gfn,uint64_t count,uint8_t * keys)71 int s390_skeys_set(S390SKeysState *ks, uint64_t start_gfn,
72                    uint64_t count, uint8_t *keys)
73 {
74     S390SKeysClass *kc = S390_SKEYS_GET_CLASS(ks);
75     int rc;
76 
77     rc = kc->set_skeys(ks, start_gfn, count, keys);
78     if (rc) {
79         trace_s390_skeys_set_nonzero(rc);
80     }
81     return rc;
82 }
83 
write_keys(FILE * f,uint8_t * keys,uint64_t startgfn,uint64_t count,Error ** errp)84 static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
85                        uint64_t count, Error **errp)
86 {
87     uint64_t curpage = startgfn;
88     uint64_t maxpage = curpage + count - 1;
89 
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);
96 
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);
100         keys++;
101     }
102 }
103 
hmp_info_skeys(Monitor * mon,const QDict * qdict)104 void hmp_info_skeys(Monitor *mon, const QDict *qdict)
105 {
106     S390SKeysState *ss = s390_get_skeys_device();
107     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
108     uint64_t addr = qdict_get_int(qdict, "addr");
109     uint8_t key;
110     int r;
111 
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");
115         return;
116     }
117 
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");
122         return;
123     }
124 
125     r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
126     if (r < 0) {
127         monitor_printf(mon, "Error: %s\n", strerror(-r));
128         return;
129     }
130 
131     monitor_printf(mon, "  key: 0x%X\n", key);
132 }
133 
hmp_dump_skeys(Monitor * mon,const QDict * qdict)134 void hmp_dump_skeys(Monitor *mon, const QDict *qdict)
135 {
136     const char *filename = qdict_get_str(qdict, "filename");
137     Error *err = NULL;
138 
139     qmp_dump_skeys(filename, &err);
140     if (err) {
141         error_report_err(err);
142     }
143 }
144 
qmp_dump_skeys(const char * filename,Error ** errp)145 void qmp_dump_skeys(const char *filename, Error **errp)
146 {
147     S390SKeysState *ss = s390_get_skeys_device();
148     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
149     GuestPhysBlockList guest_phys_blocks;
150     GuestPhysBlock *block;
151     uint64_t pages, gfn;
152     Error *lerr = NULL;
153     uint8_t *buf;
154     int ret;
155     int fd;
156     FILE *f;
157 
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 - "
161                          "nothing to dump");
162         return;
163     }
164 
165     fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
166     if (fd < 0) {
167         error_setg_file_open(errp, errno, filename);
168         return;
169     }
170     f = fdopen(fd, "wb");
171     if (!f) {
172         close(fd);
173         error_setg_file_open(errp, errno, filename);
174         return;
175     }
176 
177     buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
178     if (!buf) {
179         error_setg(errp, "Could not allocate memory");
180         goto out;
181     }
182 
183     assert(bql_locked());
184     guest_phys_blocks_init(&guest_phys_blocks);
185     guest_phys_blocks_append(&guest_phys_blocks);
186 
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));
190 
191         gfn = block->target_start / TARGET_PAGE_SIZE;
192         pages = (block->target_end - block->target_start) / TARGET_PAGE_SIZE;
193 
194         while (pages) {
195             const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
196 
197             ret = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
198             if (ret < 0) {
199                 error_setg_errno(errp, -ret, "get_keys error");
200                 goto out_free;
201             }
202 
203             /* write keys to stream */
204             write_keys(f, buf, gfn, cur_pages, &lerr);
205             if (lerr) {
206                 goto out_free;
207             }
208 
209             gfn += cur_pages;
210             pages -= cur_pages;
211         }
212     }
213 
214 out_free:
215     guest_phys_blocks_free(&guest_phys_blocks);
216     error_propagate(errp, lerr);
217     g_free(buf);
218 out:
219     fclose(f);
220 }
221 
qemu_s390_skeys_are_enabled(S390SKeysState * ss)222 static bool qemu_s390_skeys_are_enabled(S390SKeysState *ss)
223 {
224     QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
225 
226     /* Lockless check is sufficient. */
227     return !!skeys->keydata;
228 }
229 
qemu_s390_enable_skeys(S390SKeysState * ss)230 static bool qemu_s390_enable_skeys(S390SKeysState *ss)
231 {
232     QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(ss);
233     static gsize initialized;
234 
235     if (likely(skeys->keydata)) {
236         return true;
237     }
238 
239     /*
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.
242      *
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.
252      */
253     if (g_once_init_enter(&initialized)) {
254         MachineState *machine = MACHINE(qdev_get_machine());
255 
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);
259     }
260     return false;
261 }
262 
qemu_s390_skeys_set(S390SKeysState * ss,uint64_t start_gfn,uint64_t count,uint8_t * keys)263 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
264                               uint64_t count, uint8_t *keys)
265 {
266     QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
267     int i;
268 
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,
274                      start_gfn, count);
275         return -EINVAL;
276     }
277 
278     for (i = 0; i < count; i++) {
279         skeydev->keydata[start_gfn + i] = keys[i];
280     }
281     return 0;
282 }
283 
qemu_s390_skeys_get(S390SKeysState * ss,uint64_t start_gfn,uint64_t count,uint8_t * keys)284 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
285                                uint64_t count, uint8_t *keys)
286 {
287     QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
288     int i;
289 
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,
295                      start_gfn, count);
296         return -EINVAL;
297     }
298 
299     for (i = 0; i < count; i++) {
300         keys[i] = skeydev->keydata[start_gfn + i];
301     }
302     return 0;
303 }
304 
qemu_s390_skeys_class_init(ObjectClass * oc,void * data)305 static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
306 {
307     S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
308     DeviceClass *dc = DEVICE_CLASS(oc);
309 
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;
314 
315     /* Reason: Internal device (only one skeys device for the whole memory) */
316     dc->user_creatable = false;
317 }
318 
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),
325 };
326 
s390_storage_keys_save(QEMUFile * f,void * opaque)327 static void s390_storage_keys_save(QEMUFile *f, void *opaque)
328 {
329     S390SKeysState *ss = S390_SKEYS(opaque);
330     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
331     GuestPhysBlockList guest_phys_blocks;
332     GuestPhysBlock *block;
333     uint64_t pages, gfn;
334     int error = 0;
335     uint8_t *buf;
336 
337     if (!skeyclass->skeys_are_enabled(ss)) {
338         goto end_stream;
339     }
340 
341     buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
342     if (!buf) {
343         error_report("storage key save could not allocate memory");
344         goto end_stream;
345     }
346 
347     guest_phys_blocks_init(&guest_phys_blocks);
348     guest_phys_blocks_append(&guest_phys_blocks);
349 
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));
354 
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);
359 
360         while (pages) {
361             const uint64_t cur_pages = MIN(pages, S390_SKEYS_BUFFER_SIZE);
362 
363             if (!error) {
364                 error = skeyclass->get_skeys(ss, gfn, cur_pages, buf);
365                 if (error) {
366                     /*
367                      * Create a valid stream with all 0x00 and indicate
368                      * S390_SKEYS_SAVE_FLAG_ERROR to the destination.
369                      */
370                     error_report("S390_GET_KEYS error %d", error);
371                     memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
372                 }
373             }
374 
375             qemu_put_buffer(f, buf, cur_pages);
376             gfn += cur_pages;
377             pages -= cur_pages;
378         }
379 
380         if (error) {
381             break;
382         }
383     }
384 
385     guest_phys_blocks_free(&guest_phys_blocks);
386     g_free(buf);
387 end_stream:
388     if (error) {
389         qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_ERROR);
390     } else {
391         qemu_put_be64(f, S390_SKEYS_SAVE_FLAG_EOS);
392     }
393 }
394 
s390_storage_keys_load(QEMUFile * f,void * opaque,int version_id)395 static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
396 {
397     S390SKeysState *ss = S390_SKEYS(opaque);
398     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
399     int ret = 0;
400 
401     /*
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.
404      */
405     if (skeyclass->enable_skeys) {
406         skeyclass->enable_skeys(ss);
407     }
408 
409     while (!ret) {
410         ram_addr_t addr;
411         int flags;
412 
413         addr = qemu_get_be64(f);
414         flags = addr & ~TARGET_PAGE_MASK;
415         addr &= TARGET_PAGE_MASK;
416 
417         switch (flags) {
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);
423 
424             if (!buf) {
425                 error_report("storage key load could not allocate memory");
426                 ret = -ENOMEM;
427                 break;
428             }
429 
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);
434 
435                 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
436                 if (ret < 0) {
437                     error_report("S390_SET_KEYS error %d", ret);
438                     break;
439                 }
440                 handled_count += cur_count;
441                 cur_gfn += cur_count;
442             }
443             g_free(buf);
444             break;
445         }
446         case S390_SKEYS_SAVE_FLAG_ERROR: {
447             error_report("Storage key data is incomplete");
448             ret = -EINVAL;
449             break;
450         }
451         case S390_SKEYS_SAVE_FLAG_EOS:
452             /* normal exit */
453             return 0;
454         default:
455             error_report("Unexpected storage key flag data: %#x", flags);
456             ret = -EINVAL;
457         }
458     }
459 
460     return ret;
461 }
462 
463 static SaveVMHandlers savevm_s390_storage_keys = {
464     .save_state = s390_storage_keys_save,
465     .load_state = s390_storage_keys_load,
466 };
467 
s390_skeys_realize(DeviceState * dev,Error ** errp)468 static void s390_skeys_realize(DeviceState *dev, Error **errp)
469 {
470     S390SKeysState *ss = S390_SKEYS(dev);
471 
472     if (ss->migration_enabled) {
473         register_savevm_live(TYPE_S390_SKEYS, 0, 1,
474                              &savevm_s390_storage_keys, ss);
475     }
476 }
477 
478 static Property s390_skeys_props[] = {
479     DEFINE_PROP_BOOL("migration-enabled", S390SKeysState, migration_enabled, true),
480     DEFINE_PROP_END_OF_LIST(),
481 };
482 
s390_skeys_class_init(ObjectClass * oc,void * data)483 static void s390_skeys_class_init(ObjectClass *oc, void *data)
484 {
485     DeviceClass *dc = DEVICE_CLASS(oc);
486 
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);
491 }
492 
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),
499     .abstract = true,
500 };
501 
qemu_s390_skeys_register_types(void)502 static void qemu_s390_skeys_register_types(void)
503 {
504     type_register_static(&s390_skeys_info);
505     type_register_static(&qemu_s390_skeys_info);
506 }
507 
508 type_init(qemu_s390_skeys_register_types)
509