s390-stattrib.c 11 KB

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