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.h" 18 #include "qapi/qmp/qdict.h" 19 #include "qemu/error-report.h" 20 #include "sysemu/kvm.h" 21 #include "migration/register.h" 22 23 #define S390_SKEYS_BUFFER_SIZE (128 * KiB) /* Room for 128k storage keys */ 24 #define S390_SKEYS_SAVE_FLAG_EOS 0x01 25 #define S390_SKEYS_SAVE_FLAG_SKEYS 0x02 26 #define S390_SKEYS_SAVE_FLAG_ERROR 0x04 27 28 S390SKeysState *s390_get_skeys_device(void) 29 { 30 S390SKeysState *ss; 31 32 ss = S390_SKEYS(object_resolve_path_type("", TYPE_S390_SKEYS, NULL)); 33 assert(ss); 34 return ss; 35 } 36 37 void s390_skeys_init(void) 38 { 39 Object *obj; 40 41 if (kvm_enabled()) { 42 obj = object_new(TYPE_KVM_S390_SKEYS); 43 } else { 44 obj = object_new(TYPE_QEMU_S390_SKEYS); 45 } 46 object_property_add_child(qdev_get_machine(), TYPE_S390_SKEYS, 47 obj, NULL); 48 object_unref(obj); 49 50 qdev_init_nofail(DEVICE(obj)); 51 } 52 53 static void write_keys(FILE *f, uint8_t *keys, uint64_t startgfn, 54 uint64_t count, Error **errp) 55 { 56 uint64_t curpage = startgfn; 57 uint64_t maxpage = curpage + count - 1; 58 59 for (; curpage <= maxpage; curpage++) { 60 uint8_t acc = (*keys & 0xF0) >> 4; 61 int fp = (*keys & 0x08); 62 int ref = (*keys & 0x04); 63 int ch = (*keys & 0x02); 64 int res = (*keys & 0x01); 65 66 fprintf(f, "page=%03" PRIx64 ": key(%d) => ACC=%X, FP=%d, REF=%d," 67 " ch=%d, reserved=%d\n", 68 curpage, *keys, acc, fp, ref, ch, res); 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 int fd; 118 FILE *f; 119 120 /* Quick check to see if guest is using storage keys*/ 121 if (!skeyclass->skeys_enabled(ss)) { 122 error_setg(errp, "This guest is not using storage keys - " 123 "nothing to dump"); 124 return; 125 } 126 127 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); 128 if (fd < 0) { 129 error_setg_file_open(errp, errno, filename); 130 return; 131 } 132 f = fdopen(fd, "wb"); 133 if (!f) { 134 close(fd); 135 error_setg_file_open(errp, errno, filename); 136 return; 137 } 138 139 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE); 140 if (!buf) { 141 error_setg(errp, "Could not allocate memory"); 142 goto out; 143 } 144 145 /* we'll only dump initial memory for now */ 146 while (handled_count < total_count) { 147 /* Calculate how many keys to ask for & handle overflow case */ 148 cur_count = MIN(total_count - handled_count, S390_SKEYS_BUFFER_SIZE); 149 150 ret = skeyclass->get_skeys(ss, cur_gfn, cur_count, buf); 151 if (ret < 0) { 152 error_setg(errp, "get_keys error %d", ret); 153 goto out_free; 154 } 155 156 /* write keys to stream */ 157 write_keys(f, buf, cur_gfn, cur_count, &lerr); 158 if (lerr) { 159 goto out_free; 160 } 161 162 cur_gfn += cur_count; 163 handled_count += cur_count; 164 } 165 166 out_free: 167 error_propagate(errp, lerr); 168 g_free(buf); 169 out: 170 fclose(f); 171 } 172 173 static void qemu_s390_skeys_init(Object *obj) 174 { 175 QEMUS390SKeysState *skeys = QEMU_S390_SKEYS(obj); 176 MachineState *machine = MACHINE(qdev_get_machine()); 177 178 skeys->key_count = machine->maxram_size / TARGET_PAGE_SIZE; 179 skeys->keydata = g_malloc0(skeys->key_count); 180 } 181 182 static int qemu_s390_skeys_enabled(S390SKeysState *ss) 183 { 184 return 1; 185 } 186 187 /* 188 * TODO: for memory hotplug support qemu_s390_skeys_set and qemu_s390_skeys_get 189 * will have to make sure that the given gfn belongs to a memory region and not 190 * a memory hole. 191 */ 192 static int qemu_s390_skeys_set(S390SKeysState *ss, uint64_t start_gfn, 193 uint64_t count, uint8_t *keys) 194 { 195 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss); 196 int i; 197 198 /* Check for uint64 overflow and access beyond end of key data */ 199 if (start_gfn + count > skeydev->key_count || start_gfn + count < count) { 200 error_report("Error: Setting storage keys for page beyond the end " 201 "of memory: gfn=%" PRIx64 " count=%" PRId64, 202 start_gfn, count); 203 return -EINVAL; 204 } 205 206 for (i = 0; i < count; i++) { 207 skeydev->keydata[start_gfn + i] = keys[i]; 208 } 209 return 0; 210 } 211 212 static int qemu_s390_skeys_get(S390SKeysState *ss, uint64_t start_gfn, 213 uint64_t count, uint8_t *keys) 214 { 215 QEMUS390SKeysState *skeydev = QEMU_S390_SKEYS(ss); 216 int i; 217 218 /* Check for uint64 overflow and access beyond end of key data */ 219 if (start_gfn + count > skeydev->key_count || start_gfn + count < count) { 220 error_report("Error: Getting storage keys for page beyond the end " 221 "of memory: gfn=%" PRIx64 " count=%" PRId64, 222 start_gfn, count); 223 return -EINVAL; 224 } 225 226 for (i = 0; i < count; i++) { 227 keys[i] = skeydev->keydata[start_gfn + i]; 228 } 229 return 0; 230 } 231 232 static void qemu_s390_skeys_class_init(ObjectClass *oc, void *data) 233 { 234 S390SKeysClass *skeyclass = S390_SKEYS_CLASS(oc); 235 DeviceClass *dc = DEVICE_CLASS(oc); 236 237 skeyclass->skeys_enabled = qemu_s390_skeys_enabled; 238 skeyclass->get_skeys = qemu_s390_skeys_get; 239 skeyclass->set_skeys = qemu_s390_skeys_set; 240 241 /* Reason: Internal device (only one skeys device for the whole memory) */ 242 dc->user_creatable = false; 243 } 244 245 static const TypeInfo qemu_s390_skeys_info = { 246 .name = TYPE_QEMU_S390_SKEYS, 247 .parent = TYPE_S390_SKEYS, 248 .instance_init = qemu_s390_skeys_init, 249 .instance_size = sizeof(QEMUS390SKeysState), 250 .class_init = qemu_s390_skeys_class_init, 251 .class_size = sizeof(S390SKeysClass), 252 }; 253 254 static void s390_storage_keys_save(QEMUFile *f, void *opaque) 255 { 256 S390SKeysState *ss = S390_SKEYS(opaque); 257 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss); 258 uint64_t pages_left = ram_size / TARGET_PAGE_SIZE; 259 uint64_t read_count, eos = S390_SKEYS_SAVE_FLAG_EOS; 260 vaddr cur_gfn = 0; 261 int error = 0; 262 uint8_t *buf; 263 264 if (!skeyclass->skeys_enabled(ss)) { 265 goto end_stream; 266 } 267 268 buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE); 269 if (!buf) { 270 error_report("storage key save could not allocate memory"); 271 goto end_stream; 272 } 273 274 /* We only support initial memory. Standby memory is not handled yet. */ 275 qemu_put_be64(f, (cur_gfn * TARGET_PAGE_SIZE) | S390_SKEYS_SAVE_FLAG_SKEYS); 276 qemu_put_be64(f, pages_left); 277 278 while (pages_left) { 279 read_count = MIN(pages_left, S390_SKEYS_BUFFER_SIZE); 280 281 if (!error) { 282 error = skeyclass->get_skeys(ss, cur_gfn, read_count, buf); 283 if (error) { 284 /* 285 * If error: we want to fill the stream with valid data instead 286 * of stopping early so we pad the stream with 0x00 values and 287 * use S390_SKEYS_SAVE_FLAG_ERROR to indicate failure to the 288 * reading side. 289 */ 290 error_report("S390_GET_KEYS error %d", error); 291 memset(buf, 0, S390_SKEYS_BUFFER_SIZE); 292 eos = S390_SKEYS_SAVE_FLAG_ERROR; 293 } 294 } 295 296 qemu_put_buffer(f, buf, read_count); 297 cur_gfn += read_count; 298 pages_left -= read_count; 299 } 300 301 g_free(buf); 302 end_stream: 303 qemu_put_be64(f, eos); 304 } 305 306 static int s390_storage_keys_load(QEMUFile *f, void *opaque, int version_id) 307 { 308 S390SKeysState *ss = S390_SKEYS(opaque); 309 S390SKeysClass *skeyclass = S390_SKEYS_GET_CLASS(ss); 310 int ret = 0; 311 312 while (!ret) { 313 ram_addr_t addr; 314 int flags; 315 316 addr = qemu_get_be64(f); 317 flags = addr & ~TARGET_PAGE_MASK; 318 addr &= TARGET_PAGE_MASK; 319 320 switch (flags) { 321 case S390_SKEYS_SAVE_FLAG_SKEYS: { 322 const uint64_t total_count = qemu_get_be64(f); 323 uint64_t handled_count = 0, cur_count; 324 uint64_t cur_gfn = addr / TARGET_PAGE_SIZE; 325 uint8_t *buf = g_try_malloc(S390_SKEYS_BUFFER_SIZE); 326 327 if (!buf) { 328 error_report("storage key load could not allocate memory"); 329 ret = -ENOMEM; 330 break; 331 } 332 333 while (handled_count < total_count) { 334 cur_count = MIN(total_count - handled_count, 335 S390_SKEYS_BUFFER_SIZE); 336 qemu_get_buffer(f, buf, cur_count); 337 338 ret = skeyclass->set_skeys(ss, cur_gfn, cur_count, buf); 339 if (ret < 0) { 340 error_report("S390_SET_KEYS error %d", ret); 341 break; 342 } 343 handled_count += cur_count; 344 cur_gfn += cur_count; 345 } 346 g_free(buf); 347 break; 348 } 349 case S390_SKEYS_SAVE_FLAG_ERROR: { 350 error_report("Storage key data is incomplete"); 351 ret = -EINVAL; 352 break; 353 } 354 case S390_SKEYS_SAVE_FLAG_EOS: 355 /* normal exit */ 356 return 0; 357 default: 358 error_report("Unexpected storage key flag data: %#x", flags); 359 ret = -EINVAL; 360 } 361 } 362 363 return ret; 364 } 365 366 static inline bool s390_skeys_get_migration_enabled(Object *obj, Error **errp) 367 { 368 S390SKeysState *ss = S390_SKEYS(obj); 369 370 return ss->migration_enabled; 371 } 372 373 static SaveVMHandlers savevm_s390_storage_keys = { 374 .save_state = s390_storage_keys_save, 375 .load_state = s390_storage_keys_load, 376 }; 377 378 static inline void s390_skeys_set_migration_enabled(Object *obj, bool value, 379 Error **errp) 380 { 381 S390SKeysState *ss = S390_SKEYS(obj); 382 383 /* Prevent double registration of savevm handler */ 384 if (ss->migration_enabled == value) { 385 return; 386 } 387 388 ss->migration_enabled = value; 389 390 if (ss->migration_enabled) { 391 register_savevm_live(NULL, TYPE_S390_SKEYS, 0, 1, 392 &savevm_s390_storage_keys, ss); 393 } else { 394 unregister_savevm(DEVICE(ss), TYPE_S390_SKEYS, ss); 395 } 396 } 397 398 static void s390_skeys_instance_init(Object *obj) 399 { 400 object_property_add_bool(obj, "migration-enabled", 401 s390_skeys_get_migration_enabled, 402 s390_skeys_set_migration_enabled, NULL); 403 object_property_set_bool(obj, true, "migration-enabled", NULL); 404 } 405 406 static void s390_skeys_class_init(ObjectClass *oc, void *data) 407 { 408 DeviceClass *dc = DEVICE_CLASS(oc); 409 410 dc->hotpluggable = false; 411 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 412 } 413 414 static const TypeInfo s390_skeys_info = { 415 .name = TYPE_S390_SKEYS, 416 .parent = TYPE_DEVICE, 417 .instance_init = s390_skeys_instance_init, 418 .instance_size = sizeof(S390SKeysState), 419 .class_init = s390_skeys_class_init, 420 .class_size = sizeof(S390SKeysClass), 421 .abstract = true, 422 }; 423 424 static void qemu_s390_skeys_register_types(void) 425 { 426 type_register_static(&s390_skeys_info); 427 type_register_static(&qemu_s390_skeys_info); 428 } 429 430 type_init(qemu_s390_skeys_register_types) 431