xref: /openbmc/qemu/hw/s390x/s390-skeys.c (revision 354908ce)
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/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"
23 
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
28 
29 S390SKeysState *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 
38 void 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,
48                               obj);
49     object_unref(obj);
50 
51     qdev_realize(DEVICE(obj), NULL, &error_fatal);
52 }
53 
54 static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn,
55                        uint64_t count, Error **errp)
56 {
57     uint64_t curpage = startgfn;
58     uint64_t maxpage = curpage + count - 1;
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 
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);
70         keys++;
71     }
72 }
73 
74 void 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 
97 void 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) {
104         error_report_err(err);
105     }
106 }
107 
108 void qmp_dump_skeys(const char *filename, Error **errp)
109 {
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;
114     Error *lerr = NULL;
115     vaddr cur_gfn = 0;
116     uint8_t *buf;
117     int ret;
118     int fd;
119     FILE *f;
120 
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 - "
124                          "nothing to dump");
125         return;
126     }
127 
128     fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
129     if (fd < 0) {
130         error_setg_file_open(errp, errno, filename);
131         return;
132     }
133     f = fdopen(fd, "wb");
134     if (!f) {
135         close(fd);
136         error_setg_file_open(errp, errno, filename);
137         return;
138     }
139 
140     buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
141     if (!buf) {
142         error_setg(errp, "Could not allocate memory");
143         goto out;
144     }
145 
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);
150 
151         ret = skeyclass->get_skeys(ss, cur_gfn, cur_count, buf);
152         if (ret < 0) {
153             error_setg(errp, "get_keys error %d", ret);
154             goto out_free;
155         }
156 
157         /* write keys to stream */
158         write_keys(f, buf, cur_gfn, cur_count, &lerr);
159         if (lerr) {
160             goto out_free;
161         }
162 
163         cur_gfn += cur_count;
164         handled_count += cur_count;
165     }
166 
167 out_free:
168     error_propagate(errp, lerr);
169     g_free(buf);
170 out:
171     fclose(f);
172 }
173 
174 static void qemu_s390_skeys_init(Object *obj)
175 {
176     QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(obj);
177     MachineState *machine = MACHINE(qdev_get_machine());
178 
179     skeys->key_count = machine->ram_size / TARGET_PAGE_SIZE;
180     skeys->keydata = g_malloc0(skeys->key_count);
181 }
182 
183 static int qemu_s390_skeys_enabled(S390SKeysState *ss)
184 {
185     return 1;
186 }
187 
188 /*
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
191  * a memory hole.
192  */
193 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
194                               uint64_t count, uint8_t *keys)
195 {
196     QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
197     int i;
198 
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,
203                      start_gfn, count);
204         return -EINVAL;
205     }
206 
207     for (i = 0; i < count; i++) {
208         skeydev->keydata[start_gfn + i] = keys[i];
209     }
210     return 0;
211 }
212 
213 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
214                                uint64_t count, uint8_t *keys)
215 {
216     QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
217     int i;
218 
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,
223                      start_gfn, count);
224         return -EINVAL;
225     }
226 
227     for (i = 0; i < count; i++) {
228         keys[i] = skeydev->keydata[start_gfn + i];
229     }
230     return 0;
231 }
232 
233 static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
234 {
235     S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
236     DeviceClass *dc = DEVICE_CLASS(oc);
237 
238     skeyclass->skeys_enabled = qemu_s390_skeys_enabled;
239     skeyclass->get_skeys = qemu_s390_skeys_get;
240     skeyclass->set_skeys = qemu_s390_skeys_set;
241 
242     /* Reason: Internal device (only one skeys device for the whole memory) */
243     dc->user_creatable = false;
244 }
245 
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),
253 };
254 
255 static void s390_storage_keys_save(QEMUFile *f, void *opaque)
256 {
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;
261     vaddr cur_gfn = 0;
262     int error = 0;
263     uint8_t *buf;
264 
265     if (!skeyclass->skeys_enabled(ss)) {
266         goto end_stream;
267     }
268 
269     buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
270     if (!buf) {
271         error_report("storage key save could not allocate memory");
272         goto end_stream;
273     }
274 
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);
278 
279     while (pages_left) {
280         read_count = MIN(pages_left, S390_SKEYS_BUFFER_SIZE);
281 
282         if (!error) {
283             error = skeyclass->get_skeys(ss, cur_gfn, read_count, buf);
284             if (error) {
285                 /*
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
289                  * reading side.
290                  */
291                 error_report("S390_GET_KEYS error %d", error);
292                 memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
293                 eos = S390_SKEYS_SAVE_FLAG_ERROR;
294             }
295         }
296 
297         qemu_put_buffer(f, buf, read_count);
298         cur_gfn += read_count;
299         pages_left -= read_count;
300     }
301 
302     g_free(buf);
303 end_stream:
304     qemu_put_be64(f, eos);
305 }
306 
307 static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
308 {
309     S390SKeysState *ss = S390_SKEYS(opaque);
310     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
311     int ret = 0;
312 
313     while (!ret) {
314         ram_addr_t addr;
315         int flags;
316 
317         addr = qemu_get_be64(f);
318         flags = addr & ~TARGET_PAGE_MASK;
319         addr &= TARGET_PAGE_MASK;
320 
321         switch (flags) {
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);
327 
328             if (!buf) {
329                 error_report("storage key load could not allocate memory");
330                 ret = -ENOMEM;
331                 break;
332             }
333 
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);
338 
339                 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
340                 if (ret < 0) {
341                     error_report("S390_SET_KEYS error %d", ret);
342                     break;
343                 }
344                 handled_count += cur_count;
345                 cur_gfn += cur_count;
346             }
347             g_free(buf);
348             break;
349         }
350         case S390_SKEYS_SAVE_FLAG_ERROR: {
351             error_report("Storage key data is incomplete");
352             ret = -EINVAL;
353             break;
354         }
355         case S390_SKEYS_SAVE_FLAG_EOS:
356             /* normal exit */
357             return 0;
358         default:
359             error_report("Unexpected storage key flag data: %#x", flags);
360             ret = -EINVAL;
361         }
362     }
363 
364     return ret;
365 }
366 
367 static inline bool s390_skeys_get_migration_enabled(Object *obj, Error **errp)
368 {
369     S390SKeysState *ss = S390_SKEYS(obj);
370 
371     return ss->migration_enabled;
372 }
373 
374 static SaveVMHandlers savevm_s390_storage_keys = {
375     .save_state = s390_storage_keys_save,
376     .load_state = s390_storage_keys_load,
377 };
378 
379 static inline void s390_skeys_set_migration_enabled(Object *obj, bool value,
380                                             Error **errp)
381 {
382     S390SKeysState *ss = S390_SKEYS(obj);
383 
384     /* Prevent double registration of savevm handler */
385     if (ss->migration_enabled == value) {
386         return;
387     }
388 
389     ss->migration_enabled = value;
390 
391     if (ss->migration_enabled) {
392         register_savevm_live(TYPE_S390_SKEYS, 0, 1,
393                              &savevm_s390_storage_keys, ss);
394     } else {
395         unregister_savevm(VMSTATE_IF(ss), TYPE_S390_SKEYS, ss);
396     }
397 }
398 
399 static void s390_skeys_instance_init(Object *obj)
400 {
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, true, "migration-enabled", NULL);
405 }
406 
407 static void s390_skeys_class_init(ObjectClass *oc, void *data)
408 {
409     DeviceClass *dc = DEVICE_CLASS(oc);
410 
411     dc->hotpluggable = false;
412     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
413 }
414 
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),
422     .abstract = true,
423 };
424 
425 static void qemu_s390_skeys_register_types(void)
426 {
427     type_register_static(&s390_skeys_info);
428     type_register_static(&qemu_s390_skeys_info);
429 }
430 
431 type_init(qemu_s390_skeys_register_types)
432