s390x: Dump storage keys qmp command
[qemu/ar7.git] / hw / s390x / s390-skeys.c
bloba1d10a9897492690e274c6fd6712660ffff66a44
1 /*
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
9 * directory.
12 #include "hw/boards.h"
13 #include "qmp-commands.h"
14 #include "hw/s390x/storage-keys.h"
15 #include "qemu/error-report.h"
17 #define S390_SKEYS_BUFFER_SIZE 131072 /* Room for 128k storage keys */
19 S390SKeysState *s390_get_skeys_device(void)
21 S390SKeysState *ss;
23 ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL));
24 assert(ss);
25 return ss;
28 void s390_skeys_init(void)
30 Object *obj;
32 if (kvm_enabled()) {
33 obj = object_new(TYPE_KVM_S390_SKEYS);
34 } else {
35 obj = object_new(TYPE_QEMU_S390_SKEYS);
37 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS,
38 obj, NULL);
39 object_unref(obj);
41 qdev_init_nofail(DEVICE(obj));
44 static void write_keys(QEMUFile *f, uint8_t *keys, uint64_t startgfn,
45 uint64_t count, Error **errp)
47 uint64_t curpage = startgfn;
48 uint64_t maxpage = curpage + count - 1;
49 const char *fmt = "page=%03" PRIx64 ": key(%d) => ACC=%X, FP=%d, REF=%d,"
50 " ch=%d, reserved=%d\n";
51 char buf[128];
52 int len;
54 for (; curpage <= maxpage; curpage++) {
55 uint8_t acc = (*keys & 0xF0) >> 4;
56 int fp = (*keys & 0x08);
57 int ref = (*keys & 0x04);
58 int ch = (*keys & 0x02);
59 int res = (*keys & 0x01);
61 len = snprintf(buf, sizeof(buf), fmt, curpage,
62 *keys, acc, fp, ref, ch, res);
63 assert(len < sizeof(buf));
64 qemu_put_buffer(f, (uint8_t *)buf, len);
65 keys++;
69 void qmp_dump_skeys(const char *filename, Error **errp)
71 S390SKeysState *ss = s390_get_skeys_device();
72 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss);
73 const uint64_t total_count = ram_size / TARGET_PAGE_SIZE;
74 uint64_t handled_count = 0, cur_count;
75 Error *lerr = NULL;
76 vaddr cur_gfn = 0;
77 uint8_t *buf;
78 int ret;
79 QEMUFile *f;
81 /* Quick check to see if guest is using storage keys*/
82 if (!skeyclass->skeys_enabled(ss)) {
83 error_setg(errp, "This guest is not using storage keys - "
84 "nothing to dump");
85 return;
88 f = qemu_fopen(filename, "wb");
89 if (!f) {
90 error_setg_file_open(errp, errno, filename);
91 return;
94 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE);
95 if (!buf) {
96 error_setg(errp, "Could not allocate memory");
97 goto out;
100 /* we'll only dump initial memory for now */
101 while (handled_count < total_count) {
102 /* Calculate how many keys to ask for & handle overflow case */
103 cur_count = MIN(total_count - handled_count, S390_SKEYS_BUFFER_SIZE);
105 ret = skeyclass->get_skeys(ss, cur_gfn, cur_count, buf);
106 if (ret < 0) {
107 error_setg(errp, "get_keys error %d", ret);
108 goto out_free;
111 /* write keys to stream */
112 write_keys(f, buf, cur_gfn, cur_count, &lerr);
113 if (lerr) {
114 goto out_free;
117 cur_gfn += cur_count;
118 handled_count += cur_count;
121 out_free:
122 error_propagate(errp, lerr);
123 g_free(buf);
124 out:
125 qemu_fclose(f);
128 static void qemu_s390_skeys_init(Object *obj)
130 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(obj);
131 MachineState *machine = MACHINE(qdev_get_machine());
133 skeys->key_count = machine->maxram_size / TARGET_PAGE_SIZE;
134 skeys->keydata = g_malloc0(skeys->key_count);
137 static int qemu_s390_skeys_enabled(S390SKeysState *ss)
139 return 1;
143 * TODO: for memory hotplug support qemu_s390_skeys_set and qemu_s390_skeys_get
144 * will have to make sure that the given gfn belongs to a memory region and not
145 * a memory hole.
147 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn,
148 uint64_t count, uint8_t *keys)
150 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
151 int i;
153 /* Check for uint64 overflow and access beyond end of key data */
154 if (start_gfn + count > skeydev->key_count || start_gfn + count < count) {
155 error_report("Error: Setting storage keys for page beyond the end "
156 "of memory: gfn=%" PRIx64 " count=%" PRId64 "\n", start_gfn,
157 count);
158 return -EINVAL;
161 for (i = 0; i < count; i++) {
162 skeydev->keydata[start_gfn + i] = keys[i];
164 return 0;
167 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn,
168 uint64_t count, uint8_t *keys)
170 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss);
171 int i;
173 /* Check for uint64 overflow and access beyond end of key data */
174 if (start_gfn + count > skeydev->key_count || start_gfn + count < count) {
175 error_report("Error: Getting storage keys for page beyond the end "
176 "of memory: gfn=%" PRIx64 " count=%" PRId64 "\n", start_gfn,
177 count);
178 return -EINVAL;
181 for (i = 0; i < count; i++) {
182 keys[i] = skeydev->keydata[start_gfn + i];
184 return 0;
187 static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data)
189 S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc);
191 skeyclass->skeys_enabled = qemu_s390_skeys_enabled;
192 skeyclass->get_skeys = qemu_s390_skeys_get;
193 skeyclass->set_skeys = qemu_s390_skeys_set;
196 static const TypeInfo qemu_s390_skeys_info = {
197 .name = TYPE_QEMU_S390_SKEYS,
198 .parent = TYPE_S390_SKEYS,
199 .instance_init = qemu_s390_skeys_init,
200 .instance_size = sizeof(QEMUS390SKeysState),
201 .class_init = qemu_s390_skeys_class_init,
202 .instance_size = sizeof(S390SKeysClass),
205 static void s390_skeys_class_init(ObjectClass *oc, void *data)
207 DeviceClass *dc = DEVICE_CLASS(oc);
209 dc->hotpluggable = false;
210 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
213 static const TypeInfo s390_skeys_info = {
214 .name = TYPE_S390_SKEYS,
215 .parent = TYPE_DEVICE,
216 .instance_size = sizeof(S390SKeysState),
217 .class_init = s390_skeys_class_init,
218 .class_size = sizeof(S390SKeysClass),
219 .abstract = true,
222 static void qemu_s390_skeys_register_types(void)
224 type_register_static(&s390_skeys_info);
225 type_register_static(&qemu_s390_skeys_info);
228 type_init(qemu_s390_skeys_register_types)