xref: /openbmc/qemu/hw/s390x/s390-skeys.c (revision d341d9f3)
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 "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"
17 
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
22 
23 S390SKeysState *s390_get_skeys_device(void)
24 {
25     S390SKeysState *ss;
26 
27     ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
28     assert(ss);
29     return ss;
30 }
31 
32 void s390_skeys_init(void)
33 {
34     Object *obj;
35 
36     if (kvm_enabled()) {
37         obj = object_new(TYPE_KVM_S390_SKEYS);
38     } else {
39         obj = object_new(TYPE_QEMU_S390_SKEYS);
40     }
41     object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
42                               obj, NULL);
43     object_unref(obj);
44 
45     qdev_init_nofail(DEVICE(obj));
46 }
47 
48 static void write_keys(QEMUFile *f, uint8_t *keys, uint64_t startgfn,
49                        uint64_t count, Error **errp)
50 {
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";
55     char buf[128];
56     int len;
57 
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);
64 
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);
69         keys++;
70     }
71 }
72 
73 void hmp_info_skeys(Monitor *mon, const QDict *qdict)
74 {
75     S390SKeysState *ss = s390_get_skeys_device();
76     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
77     uint64_t addr = qdict_get_int(qdict, "addr");
78     uint8_t key;
79     int r;
80 
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");
84         return;
85     }
86 
87     r = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
88     if (r < 0) {
89         monitor_printf(mon, "Error: %s\n", strerror(-r));
90         return;
91     }
92 
93     monitor_printf(mon, "  key: 0x%X\n", key);
94 }
95 
96 void hmp_dump_skeys(Monitor *mon, const QDict *qdict)
97 {
98     const char *filename = qdict_get_str(qdict, "filename");
99     Error *err = NULL;
100 
101     qmp_dump_skeys(filename, &err);
102     if (err) {
103         error_report_err(err);
104     }
105 }
106 
107 void qmp_dump_skeys(const char *filename, Error **errp)
108 {
109     S390SKeysState *ss = s390_get_skeys_device();
110     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
111     const uint64_t total_count = ram_size / TARGET_PAGE_SIZE;
112     uint64_t handled_count = 0, cur_count;
113     Error *lerr = NULL;
114     vaddr cur_gfn = 0;
115     uint8_t *buf;
116     int ret;
117     QEMUFile *f;
118 
119     /* Quick check to see if guest is using storage keys*/
120     if (!skeyclass->skeys_enabled(ss)) {
121         error_setg(errp, "This guest is not using storage keys - "
122                          "nothing to dump");
123         return;
124     }
125 
126     f = qemu_fopen(filename, "wb");
127     if (!f) {
128         error_setg_file_open(errp, errno, filename);
129         return;
130     }
131 
132     buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
133     if (!buf) {
134         error_setg(errp, "Could not allocate memory");
135         goto out;
136     }
137 
138     /* we'll only dump initial memory for now */
139     while (handled_count < total_count) {
140         /* Calculate how many keys to ask for & handle overflow case */
141         cur_count = MIN(total_count - handled_count, S390_SKEYS_BUFFER_SIZE);
142 
143         ret = skeyclass->get_skeys(ss, cur_gfn, cur_count, buf);
144         if (ret < 0) {
145             error_setg(errp, "get_keys error %d", ret);
146             goto out_free;
147         }
148 
149         /* write keys to stream */
150         write_keys(f, buf, cur_gfn, cur_count, &lerr);
151         if (lerr) {
152             goto out_free;
153         }
154 
155         cur_gfn += cur_count;
156         handled_count += cur_count;
157     }
158 
159 out_free:
160     error_propagate(errp, lerr);
161     g_free(buf);
162 out:
163     qemu_fclose(f);
164 }
165 
166 static void qemu_s390_skeys_init(Object *obj)
167 {
168     QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(obj);
169     MachineState *machine = MACHINE(qdev_get_machine());
170 
171     skeys->key_count = machine->maxram_size / TARGET_PAGE_SIZE;
172     skeys->keydata = g_malloc0(skeys->key_count);
173 }
174 
175 static int qemu_s390_skeys_enabled(S390SKeysState *ss)
176 {
177     return 1;
178 }
179 
180 /*
181  * TODO: for memory hotplug support qemu_s390_skeys_set and qemu_s390_skeys_get
182  * will have to make sure that the given gfn belongs to a memory region and not
183  * a memory hole.
184  */
185 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
186                               uint64_t count, uint8_t *keys)
187 {
188     QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
189     int i;
190 
191     /* Check for uint64 overflow and access beyond end of key data */
192     if (start_gfn + count > skeydev->key_count || start_gfn + count < count) {
193         error_report("Error: Setting storage keys for page beyond the end "
194                      "of memory: gfn=%" PRIx64 " count=%" PRId64,
195                      start_gfn, count);
196         return -EINVAL;
197     }
198 
199     for (i = 0; i < count; i++) {
200         skeydev->keydata[start_gfn + i] = keys[i];
201     }
202     return 0;
203 }
204 
205 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
206                                uint64_t count, uint8_t *keys)
207 {
208     QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
209     int i;
210 
211     /* Check for uint64 overflow and access beyond end of key data */
212     if (start_gfn + count > skeydev->key_count || start_gfn + count < count) {
213         error_report("Error: Getting storage keys for page beyond the end "
214                      "of memory: gfn=%" PRIx64 " count=%" PRId64,
215                      start_gfn, count);
216         return -EINVAL;
217     }
218 
219     for (i = 0; i < count; i++) {
220         keys[i] = skeydev->keydata[start_gfn + i];
221     }
222     return 0;
223 }
224 
225 static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
226 {
227     S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
228 
229     skeyclass->skeys_enabled = qemu_s390_skeys_enabled;
230     skeyclass->get_skeys = qemu_s390_skeys_get;
231     skeyclass->set_skeys = qemu_s390_skeys_set;
232 }
233 
234 static const TypeInfo qemu_s390_skeys_info = {
235     .name          = TYPE_QEMU_S390_SKEYS,
236     .parent        = TYPE_S390_SKEYS,
237     .instance_init = qemu_s390_skeys_init,
238     .instance_size = sizeof(QEMUS390SKeysState),
239     .class_init    = qemu_s390_skeys_class_init,
240     .instance_size = sizeof(S390SKeysClass),
241 };
242 
243 static void s390_storage_keys_save(QEMUFile *f, void *opaque)
244 {
245     S390SKeysState *ss = S390_SKEYS(opaque);
246     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
247     uint64_t pages_left = ram_size / TARGET_PAGE_SIZE;
248     uint64_t read_count, eos = S390_SKEYS_SAVE_FLAG_EOS;
249     vaddr cur_gfn = 0;
250     int error = 0;
251     uint8_t *buf;
252 
253     if (!skeyclass->skeys_enabled(ss)) {
254         goto end_stream;
255     }
256 
257     buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
258     if (!buf) {
259         error_report("storage key save could not allocate memory");
260         goto end_stream;
261     }
262 
263     /* We only support initial memory. Standby memory is not handled yet. */
264     qemu_put_be64(f, (cur_gfn * TARGET_PAGE_SIZE) | S390_SKEYS_SAVE_FLAG_SKEYS);
265     qemu_put_be64(f, pages_left);
266 
267     while (pages_left) {
268         read_count = MIN(pages_left, S390_SKEYS_BUFFER_SIZE);
269 
270         if (!error) {
271             error = skeyclass->get_skeys(ss, cur_gfn, read_count, buf);
272             if (error) {
273                 /*
274                  * If error: we want to fill the stream with valid data instead
275                  * of stopping early so we pad the stream with 0x00 values and
276                  * use S390_SKEYS_SAVE_FLAG_ERROR to indicate failure to the
277                  * reading side.
278                  */
279                 error_report("S390_GET_KEYS error %d", error);
280                 memset(buf, 0, S390_SKEYS_BUFFER_SIZE);
281                 eos = S390_SKEYS_SAVE_FLAG_ERROR;
282             }
283         }
284 
285         qemu_put_buffer(f, buf, read_count);
286         cur_gfn += read_count;
287         pages_left -= read_count;
288     }
289 
290     g_free(buf);
291 end_stream:
292     qemu_put_be64(f, eos);
293 }
294 
295 static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id)
296 {
297     S390SKeysState *ss = S390_SKEYS(opaque);
298     S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
299     int ret = 0;
300 
301     while (!ret) {
302         ram_addr_t addr;
303         int flags;
304 
305         addr = qemu_get_be64(f);
306         flags = addr & ~TARGET_PAGE_MASK;
307         addr &= TARGET_PAGE_MASK;
308 
309         switch (flags) {
310         case S390_SKEYS_SAVE_FLAG_SKEYS: {
311             const uint64_t total_count = qemu_get_be64(f);
312             uint64_t handled_count = 0, cur_count;
313             uint64_t cur_gfn = addr / TARGET_PAGE_SIZE;
314             uint8_t *buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
315 
316             if (!buf) {
317                 error_report("storage key load could not allocate memory");
318                 ret = -ENOMEM;
319                 break;
320             }
321 
322             while (handled_count < total_count) {
323                 cur_count = MIN(total_count - handled_count,
324                                 S390_SKEYS_BUFFER_SIZE);
325                 qemu_get_buffer(f, buf, cur_count);
326 
327                 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf);
328                 if (ret < 0) {
329                     error_report("S390_SET_KEYS error %d", ret);
330                     break;
331                 }
332                 handled_count += cur_count;
333                 cur_gfn += cur_count;
334             }
335             g_free(buf);
336             break;
337         }
338         case S390_SKEYS_SAVE_FLAG_ERROR: {
339             error_report("Storage key data is incomplete");
340             ret = -EINVAL;
341             break;
342         }
343         case S390_SKEYS_SAVE_FLAG_EOS:
344             /* normal exit */
345             return 0;
346         default:
347             error_report("Unexpected storage key flag data: %#x", flags);
348             ret = -EINVAL;
349         }
350     }
351 
352     return ret;
353 }
354 
355 static inline bool s390_skeys_get_migration_enabled(Object *obj, Error **errp)
356 {
357     S390SKeysState *ss = S390_SKEYS(obj);
358 
359     return ss->migration_enabled;
360 }
361 
362 static inline void s390_skeys_set_migration_enabled(Object *obj, bool value,
363                                             Error **errp)
364 {
365     S390SKeysState *ss = S390_SKEYS(obj);
366 
367     /* Prevent double registration of savevm handler */
368     if (ss->migration_enabled == value) {
369         return;
370     }
371 
372     ss->migration_enabled = value;
373 
374     if (ss->migration_enabled) {
375         register_savevm(NULL, TYPE_S390_SKEYS, 0, 1, s390_storage_keys_save,
376                         s390_storage_keys_load, ss);
377     } else {
378         unregister_savevm(DEVICE(ss), TYPE_S390_SKEYS, ss);
379     }
380 }
381 
382 static void s390_skeys_instance_init(Object *obj)
383 {
384     object_property_add_bool(obj, "migration-enabled",
385                              s390_skeys_get_migration_enabled,
386                              s390_skeys_set_migration_enabled, NULL);
387     object_property_set_bool(obj, true, "migration-enabled", NULL);
388 }
389 
390 static void s390_skeys_class_init(ObjectClass *oc, void *data)
391 {
392     DeviceClass *dc = DEVICE_CLASS(oc);
393 
394     dc->hotpluggable = false;
395     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
396 }
397 
398 static const TypeInfo s390_skeys_info = {
399     .name          = TYPE_S390_SKEYS,
400     .parent        = TYPE_DEVICE,
401     .instance_init = s390_skeys_instance_init,
402     .instance_size = sizeof(S390SKeysState),
403     .class_init    = s390_skeys_class_init,
404     .class_size    = sizeof(S390SKeysClass),
405     .abstract = true,
406 };
407 
408 static void qemu_s390_skeys_register_types(void)
409 {
410     type_register_static(&s390_skeys_info);
411     type_register_static(&qemu_s390_skeys_info);
412 }
413 
414 type_init(qemu_s390_skeys_register_types)
415