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
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)
23 ss
= S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS
, NULL
));
28 void s390_skeys_init(void)
33 obj
= object_new(TYPE_KVM_S390_SKEYS
);
35 obj
= object_new(TYPE_QEMU_S390_SKEYS
);
37 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS
,
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";
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
);
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
;
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 - "
88 f
= qemu_fopen(filename
, "wb");
90 error_setg_file_open(errp
, errno
, filename
);
94 buf
= g_try_malloc(S390_SKEYS_BUFFER_SIZE
);
96 error_setg(errp
, "Could not allocate memory");
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
);
107 error_setg(errp
, "get_keys error %d", ret
);
111 /* write keys to stream */
112 write_keys(f
, buf
, cur_gfn
, cur_count
, &lerr
);
117 cur_gfn
+= cur_count
;
118 handled_count
+= cur_count
;
122 error_propagate(errp
, lerr
);
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
)
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
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
);
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
,
161 for (i
= 0; i
< count
; i
++) {
162 skeydev
->keydata
[start_gfn
+ i
] = keys
[i
];
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
);
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
,
181 for (i
= 0; i
< count
; i
++) {
182 keys
[i
] = skeydev
->keydata
[start_gfn
+ i
];
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
),
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
)