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