1 /* 2 * s390 storage attributes device 3 * 4 * Copyright 2016 IBM Corp. 5 * Author(s): Claudio Imbrenda <imbrenda@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 "cpu.h" 15 #include "migration/qemu-file.h" 16 #include "migration/register.h" 17 #include "hw/s390x/storage-attributes.h" 18 #include "qemu/error-report.h" 19 #include "exec/ram_addr.h" 20 #include "qapi/error.h" 21 22 #define CMMA_BLOCK_SIZE (1 << 10) 23 24 #define STATTR_FLAG_EOS 0x01ULL 25 #define STATTR_FLAG_MORE 0x02ULL 26 #define STATTR_FLAG_ERROR 0x04ULL 27 #define STATTR_FLAG_DONE 0x08ULL 28 29 static S390StAttribState *s390_get_stattrib_device(void) 30 { 31 S390StAttribState *sas; 32 33 sas = S390_STATTRIB(object_resolve_path_type("", TYPE_S390_STATTRIB, NULL)); 34 assert(sas); 35 return sas; 36 } 37 38 void s390_stattrib_init(void) 39 { 40 Object *obj; 41 42 obj = kvm_s390_stattrib_create(); 43 if (!obj) { 44 obj = object_new(TYPE_QEMU_S390_STATTRIB); 45 } 46 47 object_property_add_child(qdev_get_machine(), TYPE_S390_STATTRIB, 48 obj, NULL); 49 object_unref(obj); 50 51 qdev_init_nofail(DEVICE(obj)); 52 } 53 54 /* Console commands: */ 55 56 void hmp_migrationmode(Monitor *mon, const QDict *qdict) 57 { 58 S390StAttribState *sas = s390_get_stattrib_device(); 59 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 60 uint64_t what = qdict_get_int(qdict, "mode"); 61 int r; 62 63 r = sac->set_migrationmode(sas, what); 64 if (r < 0) { 65 monitor_printf(mon, "Error: %s", strerror(-r)); 66 } 67 } 68 69 void hmp_info_cmma(Monitor *mon, const QDict *qdict) 70 { 71 S390StAttribState *sas = s390_get_stattrib_device(); 72 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 73 uint64_t addr = qdict_get_int(qdict, "addr"); 74 uint64_t buflen = qdict_get_try_int(qdict, "count", 8); 75 uint8_t *vals; 76 int cx, len; 77 78 vals = g_try_malloc(buflen); 79 if (!vals) { 80 monitor_printf(mon, "Error: %s\n", strerror(errno)); 81 return; 82 } 83 84 len = sac->peek_stattr(sas, addr / TARGET_PAGE_SIZE, buflen, vals); 85 if (len < 0) { 86 monitor_printf(mon, "Error: %s", strerror(-len)); 87 goto out; 88 } 89 90 monitor_printf(mon, " CMMA attributes, " 91 "pages %" PRIu64 "+%d (0x%" PRIx64 "):\n", 92 addr / TARGET_PAGE_SIZE, len, addr & ~TARGET_PAGE_MASK); 93 for (cx = 0; cx < len; cx++) { 94 if (cx % 8 == 7) { 95 monitor_printf(mon, "%02x\n", vals[cx]); 96 } else { 97 monitor_printf(mon, "%02x", vals[cx]); 98 } 99 } 100 monitor_printf(mon, "\n"); 101 102 out: 103 g_free(vals); 104 } 105 106 /* Migration support: */ 107 108 static int cmma_load(QEMUFile *f, void *opaque, int version_id) 109 { 110 S390StAttribState *sas = S390_STATTRIB(opaque); 111 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 112 uint64_t count, cur_gfn; 113 int flags, ret = 0; 114 ram_addr_t addr; 115 uint8_t *buf; 116 117 while (!ret) { 118 addr = qemu_get_be64(f); 119 flags = addr & ~TARGET_PAGE_MASK; 120 addr &= TARGET_PAGE_MASK; 121 122 switch (flags) { 123 case STATTR_FLAG_MORE: { 124 cur_gfn = addr / TARGET_PAGE_SIZE; 125 count = qemu_get_be64(f); 126 buf = g_try_malloc(count); 127 if (!buf) { 128 error_report("cmma_load could not allocate memory"); 129 ret = -ENOMEM; 130 break; 131 } 132 133 qemu_get_buffer(f, buf, count); 134 ret = sac->set_stattr(sas, cur_gfn, count, buf); 135 if (ret < 0) { 136 error_report("Error %d while setting storage attributes", ret); 137 } 138 g_free(buf); 139 break; 140 } 141 case STATTR_FLAG_ERROR: { 142 error_report("Storage attributes data is incomplete"); 143 ret = -EINVAL; 144 break; 145 } 146 case STATTR_FLAG_DONE: 147 /* This is after the last pre-copied value has been sent, nothing 148 * more will be sent after this. Pre-copy has finished, and we 149 * are done flushing all the remaining values. Now the target 150 * system is about to take over. We synchronize the buffer to 151 * apply the actual correct values where needed. 152 */ 153 sac->synchronize(sas); 154 break; 155 case STATTR_FLAG_EOS: 156 /* Normal exit */ 157 return 0; 158 default: 159 error_report("Unexpected storage attribute flag data: %#x", flags); 160 ret = -EINVAL; 161 } 162 } 163 164 return ret; 165 } 166 167 static int cmma_save_setup(QEMUFile *f, void *opaque) 168 { 169 S390StAttribState *sas = S390_STATTRIB(opaque); 170 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 171 int res; 172 /* 173 * Signal that we want to start a migration, thus needing PGSTE dirty 174 * tracking. 175 */ 176 res = sac->set_migrationmode(sas, 1); 177 if (res) { 178 return res; 179 } 180 qemu_put_be64(f, STATTR_FLAG_EOS); 181 return 0; 182 } 183 184 static void cmma_save_pending(QEMUFile *f, void *opaque, uint64_t max_size, 185 uint64_t *non_postcopiable_pending, 186 uint64_t *postcopiable_pending) 187 { 188 S390StAttribState *sas = S390_STATTRIB(opaque); 189 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 190 long long res = sac->get_dirtycount(sas); 191 192 if (res >= 0) { 193 *non_postcopiable_pending += res; 194 } 195 } 196 197 static int cmma_save(QEMUFile *f, void *opaque, int final) 198 { 199 S390StAttribState *sas = S390_STATTRIB(opaque); 200 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 201 uint8_t *buf; 202 int r, cx, reallen = 0, ret = 0; 203 uint32_t buflen = 1 << 19; /* 512kB cover 2GB of guest memory */ 204 uint64_t start_gfn = sas->migration_cur_gfn; 205 206 buf = g_try_malloc(buflen); 207 if (!buf) { 208 error_report("Could not allocate memory to save storage attributes"); 209 return -ENOMEM; 210 } 211 212 while (final ? 1 : qemu_file_rate_limit(f) == 0) { 213 reallen = sac->get_stattr(sas, &start_gfn, buflen, buf); 214 if (reallen < 0) { 215 g_free(buf); 216 return reallen; 217 } 218 219 ret = 1; 220 if (!reallen) { 221 break; 222 } 223 qemu_put_be64(f, (start_gfn << TARGET_PAGE_BITS) | STATTR_FLAG_MORE); 224 qemu_put_be64(f, reallen); 225 for (cx = 0; cx < reallen; cx++) { 226 qemu_put_byte(f, buf[cx]); 227 } 228 if (!sac->get_dirtycount(sas)) { 229 break; 230 } 231 } 232 233 sas->migration_cur_gfn = start_gfn + reallen; 234 g_free(buf); 235 if (final) { 236 qemu_put_be64(f, STATTR_FLAG_DONE); 237 } 238 qemu_put_be64(f, STATTR_FLAG_EOS); 239 240 r = qemu_file_get_error(f); 241 if (r < 0) { 242 return r; 243 } 244 245 return ret; 246 } 247 248 static int cmma_save_iterate(QEMUFile *f, void *opaque) 249 { 250 return cmma_save(f, opaque, 0); 251 } 252 253 static int cmma_save_complete(QEMUFile *f, void *opaque) 254 { 255 return cmma_save(f, opaque, 1); 256 } 257 258 static void cmma_save_cleanup(void *opaque) 259 { 260 S390StAttribState *sas = S390_STATTRIB(opaque); 261 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 262 sac->set_migrationmode(sas, 0); 263 } 264 265 static bool cmma_active(void *opaque) 266 { 267 S390StAttribState *sas = S390_STATTRIB(opaque); 268 S390StAttribClass *sac = S390_STATTRIB_GET_CLASS(sas); 269 return sac->get_active(sas); 270 } 271 272 /* QEMU object: */ 273 274 static void qemu_s390_stattrib_instance_init(Object *obj) 275 { 276 } 277 278 static int qemu_s390_peek_stattr_stub(S390StAttribState *sa, uint64_t start_gfn, 279 uint32_t count, uint8_t *values) 280 { 281 return 0; 282 } 283 static void qemu_s390_synchronize_stub(S390StAttribState *sa) 284 { 285 } 286 static int qemu_s390_get_stattr_stub(S390StAttribState *sa, uint64_t *start_gfn, 287 uint32_t count, uint8_t *values) 288 { 289 return 0; 290 } 291 static long long qemu_s390_get_dirtycount_stub(S390StAttribState *sa) 292 { 293 return 0; 294 } 295 static int qemu_s390_set_migrationmode_stub(S390StAttribState *sa, bool value) 296 { 297 return 0; 298 } 299 300 static int qemu_s390_get_active(S390StAttribState *sa) 301 { 302 return sa->migration_enabled; 303 } 304 305 static void qemu_s390_stattrib_class_init(ObjectClass *oc, void *data) 306 { 307 S390StAttribClass *sa_cl = S390_STATTRIB_CLASS(oc); 308 DeviceClass *dc = DEVICE_CLASS(oc); 309 310 sa_cl->synchronize = qemu_s390_synchronize_stub; 311 sa_cl->get_stattr = qemu_s390_get_stattr_stub; 312 sa_cl->set_stattr = qemu_s390_peek_stattr_stub; 313 sa_cl->peek_stattr = qemu_s390_peek_stattr_stub; 314 sa_cl->set_migrationmode = qemu_s390_set_migrationmode_stub; 315 sa_cl->get_dirtycount = qemu_s390_get_dirtycount_stub; 316 sa_cl->get_active = qemu_s390_get_active; 317 318 /* Reason: Can only be instantiated one time (internally) */ 319 dc->user_creatable = false; 320 } 321 322 static const TypeInfo qemu_s390_stattrib_info = { 323 .name = TYPE_QEMU_S390_STATTRIB, 324 .parent = TYPE_S390_STATTRIB, 325 .instance_init = qemu_s390_stattrib_instance_init, 326 .instance_size = sizeof(QEMUS390StAttribState), 327 .class_init = qemu_s390_stattrib_class_init, 328 .class_size = sizeof(S390StAttribClass), 329 }; 330 331 /* Generic abstract object: */ 332 333 static void s390_stattrib_realize(DeviceState *dev, Error **errp) 334 { 335 bool ambiguous = false; 336 337 object_resolve_path_type("", TYPE_S390_STATTRIB, &ambiguous); 338 if (ambiguous) { 339 error_setg(errp, "storage_attributes device already exists"); 340 } 341 } 342 343 static void s390_stattrib_class_init(ObjectClass *oc, void *data) 344 { 345 DeviceClass *dc = DEVICE_CLASS(oc); 346 347 dc->hotpluggable = false; 348 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 349 dc->realize = s390_stattrib_realize; 350 } 351 352 static inline bool s390_stattrib_get_migration_enabled(Object *obj, Error **e) 353 { 354 S390StAttribState *s = S390_STATTRIB(obj); 355 356 return s->migration_enabled; 357 } 358 359 static inline void s390_stattrib_set_migration_enabled(Object *obj, bool value, 360 Error **errp) 361 { 362 S390StAttribState *s = S390_STATTRIB(obj); 363 364 s->migration_enabled = value; 365 } 366 367 static void s390_stattrib_instance_init(Object *obj) 368 { 369 S390StAttribState *sas = S390_STATTRIB(obj); 370 SaveVMHandlers *ops; 371 372 /* ops will always be freed by qemu when unregistering */ 373 ops = g_new0(SaveVMHandlers, 1); 374 375 ops->save_setup = cmma_save_setup; 376 ops->save_live_iterate = cmma_save_iterate; 377 ops->save_live_complete_precopy = cmma_save_complete; 378 ops->save_live_pending = cmma_save_pending; 379 ops->save_cleanup = cmma_save_cleanup; 380 ops->load_state = cmma_load; 381 ops->is_active = cmma_active; 382 register_savevm_live(NULL, TYPE_S390_STATTRIB, 0, 0, ops, sas); 383 384 object_property_add_bool(obj, "migration-enabled", 385 s390_stattrib_get_migration_enabled, 386 s390_stattrib_set_migration_enabled, NULL); 387 object_property_set_bool(obj, true, "migration-enabled", NULL); 388 sas->migration_cur_gfn = 0; 389 } 390 391 static const TypeInfo s390_stattrib_info = { 392 .name = TYPE_S390_STATTRIB, 393 .parent = TYPE_DEVICE, 394 .instance_init = s390_stattrib_instance_init, 395 .instance_size = sizeof(S390StAttribState), 396 .class_init = s390_stattrib_class_init, 397 .class_size = sizeof(S390StAttribClass), 398 .abstract = true, 399 }; 400 401 static void s390_stattrib_register_types(void) 402 { 403 type_register_static(&s390_stattrib_info); 404 type_register_static(&qemu_s390_stattrib_info); 405 } 406 407 type_init(s390_stattrib_register_types) 408