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