2
0

vmstate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * VMState interpreter
  3. *
  4. * Copyright (c) 2009-2017 Red Hat Inc
  5. *
  6. * Authors:
  7. * Juan Quintela <quintela@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "migration.h"
  14. #include "migration/vmstate.h"
  15. #include "savevm.h"
  16. #include "qapi/qmp/json-writer.h"
  17. #include "qemu-file.h"
  18. #include "qemu/bitops.h"
  19. #include "qemu/error-report.h"
  20. #include "trace.h"
  21. static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
  22. void *opaque, JSONWriter *vmdesc);
  23. static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
  24. void *opaque);
  25. static int vmstate_n_elems(void *opaque, const VMStateField *field)
  26. {
  27. int n_elems = 1;
  28. if (field->flags & VMS_ARRAY) {
  29. n_elems = field->num;
  30. } else if (field->flags & VMS_VARRAY_INT32) {
  31. n_elems = *(int32_t *)(opaque + field->num_offset);
  32. } else if (field->flags & VMS_VARRAY_UINT32) {
  33. n_elems = *(uint32_t *)(opaque + field->num_offset);
  34. } else if (field->flags & VMS_VARRAY_UINT16) {
  35. n_elems = *(uint16_t *)(opaque + field->num_offset);
  36. } else if (field->flags & VMS_VARRAY_UINT8) {
  37. n_elems = *(uint8_t *)(opaque + field->num_offset);
  38. }
  39. if (field->flags & VMS_MULTIPLY_ELEMENTS) {
  40. n_elems *= field->num;
  41. }
  42. trace_vmstate_n_elems(field->name, n_elems);
  43. return n_elems;
  44. }
  45. static int vmstate_size(void *opaque, const VMStateField *field)
  46. {
  47. int size = field->size;
  48. if (field->flags & VMS_VBUFFER) {
  49. size = *(int32_t *)(opaque + field->size_offset);
  50. if (field->flags & VMS_MULTIPLY) {
  51. size *= field->size;
  52. }
  53. }
  54. return size;
  55. }
  56. static void vmstate_handle_alloc(void *ptr, const VMStateField *field,
  57. void *opaque)
  58. {
  59. if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) {
  60. gsize size = vmstate_size(opaque, field);
  61. size *= vmstate_n_elems(opaque, field);
  62. if (size) {
  63. *(void **)ptr = g_malloc(size);
  64. }
  65. }
  66. }
  67. int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
  68. void *opaque, int version_id)
  69. {
  70. const VMStateField *field = vmsd->fields;
  71. int ret = 0;
  72. trace_vmstate_load_state(vmsd->name, version_id);
  73. if (version_id > vmsd->version_id) {
  74. error_report("%s: incoming version_id %d is too new "
  75. "for local version_id %d",
  76. vmsd->name, version_id, vmsd->version_id);
  77. trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
  78. return -EINVAL;
  79. }
  80. if (version_id < vmsd->minimum_version_id) {
  81. error_report("%s: incoming version_id %d is too old "
  82. "for local minimum version_id %d",
  83. vmsd->name, version_id, vmsd->minimum_version_id);
  84. trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
  85. return -EINVAL;
  86. }
  87. if (vmsd->pre_load) {
  88. int ret = vmsd->pre_load(opaque);
  89. if (ret) {
  90. return ret;
  91. }
  92. }
  93. while (field->name) {
  94. trace_vmstate_load_state_field(vmsd->name, field->name);
  95. if ((field->field_exists &&
  96. field->field_exists(opaque, version_id)) ||
  97. (!field->field_exists &&
  98. field->version_id <= version_id)) {
  99. void *first_elem = opaque + field->offset;
  100. int i, n_elems = vmstate_n_elems(opaque, field);
  101. int size = vmstate_size(opaque, field);
  102. vmstate_handle_alloc(first_elem, field, opaque);
  103. if (field->flags & VMS_POINTER) {
  104. first_elem = *(void **)first_elem;
  105. assert(first_elem || !n_elems || !size);
  106. }
  107. for (i = 0; i < n_elems; i++) {
  108. void *curr_elem = first_elem + size * i;
  109. if (field->flags & VMS_ARRAY_OF_POINTER) {
  110. curr_elem = *(void **)curr_elem;
  111. }
  112. if (!curr_elem && size) {
  113. /* if null pointer check placeholder and do not follow */
  114. assert(field->flags & VMS_ARRAY_OF_POINTER);
  115. ret = vmstate_info_nullptr.get(f, curr_elem, size, NULL);
  116. } else if (field->flags & VMS_STRUCT) {
  117. ret = vmstate_load_state(f, field->vmsd, curr_elem,
  118. field->vmsd->version_id);
  119. } else if (field->flags & VMS_VSTRUCT) {
  120. ret = vmstate_load_state(f, field->vmsd, curr_elem,
  121. field->struct_version_id);
  122. } else {
  123. ret = field->info->get(f, curr_elem, size, field);
  124. }
  125. if (ret >= 0) {
  126. ret = qemu_file_get_error(f);
  127. }
  128. if (ret < 0) {
  129. qemu_file_set_error(f, ret);
  130. error_report("Failed to load %s:%s", vmsd->name,
  131. field->name);
  132. trace_vmstate_load_field_error(field->name, ret);
  133. return ret;
  134. }
  135. }
  136. } else if (field->flags & VMS_MUST_EXIST) {
  137. error_report("Input validation failed: %s/%s",
  138. vmsd->name, field->name);
  139. return -1;
  140. }
  141. field++;
  142. }
  143. assert(field->flags == VMS_END);
  144. ret = vmstate_subsection_load(f, vmsd, opaque);
  145. if (ret != 0) {
  146. return ret;
  147. }
  148. if (vmsd->post_load) {
  149. ret = vmsd->post_load(opaque, version_id);
  150. }
  151. trace_vmstate_load_state_end(vmsd->name, "end", ret);
  152. return ret;
  153. }
  154. static int vmfield_name_num(const VMStateField *start,
  155. const VMStateField *search)
  156. {
  157. const VMStateField *field;
  158. int found = 0;
  159. for (field = start; field->name; field++) {
  160. if (!strcmp(field->name, search->name)) {
  161. if (field == search) {
  162. return found;
  163. }
  164. found++;
  165. }
  166. }
  167. return -1;
  168. }
  169. static bool vmfield_name_is_unique(const VMStateField *start,
  170. const VMStateField *search)
  171. {
  172. const VMStateField *field;
  173. int found = 0;
  174. for (field = start; field->name; field++) {
  175. if (!strcmp(field->name, search->name)) {
  176. found++;
  177. /* name found more than once, so it's not unique */
  178. if (found > 1) {
  179. return false;
  180. }
  181. }
  182. }
  183. return true;
  184. }
  185. static const char *vmfield_get_type_name(const VMStateField *field)
  186. {
  187. const char *type = "unknown";
  188. if (field->flags & VMS_STRUCT) {
  189. type = "struct";
  190. } else if (field->flags & VMS_VSTRUCT) {
  191. type = "vstruct";
  192. } else if (field->info->name) {
  193. type = field->info->name;
  194. }
  195. return type;
  196. }
  197. static bool vmsd_can_compress(const VMStateField *field)
  198. {
  199. if (field->field_exists) {
  200. /* Dynamically existing fields mess up compression */
  201. return false;
  202. }
  203. if (field->flags & VMS_STRUCT) {
  204. const VMStateField *sfield = field->vmsd->fields;
  205. while (sfield->name) {
  206. if (!vmsd_can_compress(sfield)) {
  207. /* Child elements can't compress, so can't we */
  208. return false;
  209. }
  210. sfield++;
  211. }
  212. if (field->vmsd->subsections) {
  213. /* Subsections may come and go, better don't compress */
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. static void vmsd_desc_field_start(const VMStateDescription *vmsd,
  220. JSONWriter *vmdesc,
  221. const VMStateField *field, int i, int max)
  222. {
  223. char *name, *old_name;
  224. bool is_array = max > 1;
  225. bool can_compress = vmsd_can_compress(field);
  226. if (!vmdesc) {
  227. return;
  228. }
  229. name = g_strdup(field->name);
  230. /* Field name is not unique, need to make it unique */
  231. if (!vmfield_name_is_unique(vmsd->fields, field)) {
  232. int num = vmfield_name_num(vmsd->fields, field);
  233. old_name = name;
  234. name = g_strdup_printf("%s[%d]", name, num);
  235. g_free(old_name);
  236. }
  237. json_writer_start_object(vmdesc, NULL);
  238. json_writer_str(vmdesc, "name", name);
  239. if (is_array) {
  240. if (can_compress) {
  241. json_writer_int64(vmdesc, "array_len", max);
  242. } else {
  243. json_writer_int64(vmdesc, "index", i);
  244. }
  245. }
  246. json_writer_str(vmdesc, "type", vmfield_get_type_name(field));
  247. if (field->flags & VMS_STRUCT) {
  248. json_writer_start_object(vmdesc, "struct");
  249. }
  250. g_free(name);
  251. }
  252. static void vmsd_desc_field_end(const VMStateDescription *vmsd,
  253. JSONWriter *vmdesc,
  254. const VMStateField *field, size_t size, int i)
  255. {
  256. if (!vmdesc) {
  257. return;
  258. }
  259. if (field->flags & VMS_STRUCT) {
  260. /* We printed a struct in between, close its child object */
  261. json_writer_end_object(vmdesc);
  262. }
  263. json_writer_int64(vmdesc, "size", size);
  264. json_writer_end_object(vmdesc);
  265. }
  266. bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
  267. {
  268. if (vmsd->needed && !vmsd->needed(opaque)) {
  269. /* optional section not needed */
  270. return false;
  271. }
  272. return true;
  273. }
  274. int vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
  275. void *opaque, JSONWriter *vmdesc_id)
  276. {
  277. return vmstate_save_state_v(f, vmsd, opaque, vmdesc_id, vmsd->version_id);
  278. }
  279. int vmstate_save_state_v(QEMUFile *f, const VMStateDescription *vmsd,
  280. void *opaque, JSONWriter *vmdesc, int version_id)
  281. {
  282. int ret = 0;
  283. const VMStateField *field = vmsd->fields;
  284. trace_vmstate_save_state_top(vmsd->name);
  285. if (vmsd->pre_save) {
  286. ret = vmsd->pre_save(opaque);
  287. trace_vmstate_save_state_pre_save_res(vmsd->name, ret);
  288. if (ret) {
  289. error_report("pre-save failed: %s", vmsd->name);
  290. return ret;
  291. }
  292. }
  293. if (vmdesc) {
  294. json_writer_str(vmdesc, "vmsd_name", vmsd->name);
  295. json_writer_int64(vmdesc, "version", version_id);
  296. json_writer_start_array(vmdesc, "fields");
  297. }
  298. while (field->name) {
  299. if ((field->field_exists &&
  300. field->field_exists(opaque, version_id)) ||
  301. (!field->field_exists &&
  302. field->version_id <= version_id)) {
  303. void *first_elem = opaque + field->offset;
  304. int i, n_elems = vmstate_n_elems(opaque, field);
  305. int size = vmstate_size(opaque, field);
  306. int64_t old_offset, written_bytes;
  307. JSONWriter *vmdesc_loop = vmdesc;
  308. trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
  309. if (field->flags & VMS_POINTER) {
  310. first_elem = *(void **)first_elem;
  311. assert(first_elem || !n_elems || !size);
  312. }
  313. for (i = 0; i < n_elems; i++) {
  314. void *curr_elem = first_elem + size * i;
  315. vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
  316. old_offset = qemu_file_total_transferred_fast(f);
  317. if (field->flags & VMS_ARRAY_OF_POINTER) {
  318. assert(curr_elem);
  319. curr_elem = *(void **)curr_elem;
  320. }
  321. if (!curr_elem && size) {
  322. /* if null pointer write placeholder and do not follow */
  323. assert(field->flags & VMS_ARRAY_OF_POINTER);
  324. ret = vmstate_info_nullptr.put(f, curr_elem, size, NULL,
  325. NULL);
  326. } else if (field->flags & VMS_STRUCT) {
  327. ret = vmstate_save_state(f, field->vmsd, curr_elem,
  328. vmdesc_loop);
  329. } else if (field->flags & VMS_VSTRUCT) {
  330. ret = vmstate_save_state_v(f, field->vmsd, curr_elem,
  331. vmdesc_loop,
  332. field->struct_version_id);
  333. } else {
  334. ret = field->info->put(f, curr_elem, size, field,
  335. vmdesc_loop);
  336. }
  337. if (ret) {
  338. error_report("Save of field %s/%s failed",
  339. vmsd->name, field->name);
  340. if (vmsd->post_save) {
  341. vmsd->post_save(opaque);
  342. }
  343. return ret;
  344. }
  345. written_bytes = qemu_file_total_transferred_fast(f) -
  346. old_offset;
  347. vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
  348. /* Compressed arrays only care about the first element */
  349. if (vmdesc_loop && vmsd_can_compress(field)) {
  350. vmdesc_loop = NULL;
  351. }
  352. }
  353. } else {
  354. if (field->flags & VMS_MUST_EXIST) {
  355. error_report("Output state validation failed: %s/%s",
  356. vmsd->name, field->name);
  357. assert(!(field->flags & VMS_MUST_EXIST));
  358. }
  359. }
  360. field++;
  361. }
  362. assert(field->flags == VMS_END);
  363. if (vmdesc) {
  364. json_writer_end_array(vmdesc);
  365. }
  366. ret = vmstate_subsection_save(f, vmsd, opaque, vmdesc);
  367. if (vmsd->post_save) {
  368. int ps_ret = vmsd->post_save(opaque);
  369. if (!ret) {
  370. ret = ps_ret;
  371. }
  372. }
  373. return ret;
  374. }
  375. static const VMStateDescription *
  376. vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
  377. {
  378. while (sub && *sub) {
  379. if (strcmp(idstr, (*sub)->name) == 0) {
  380. return *sub;
  381. }
  382. sub++;
  383. }
  384. return NULL;
  385. }
  386. static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
  387. void *opaque)
  388. {
  389. trace_vmstate_subsection_load(vmsd->name);
  390. while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
  391. char idstr[256], *idstr_ret;
  392. int ret;
  393. uint8_t version_id, len, size;
  394. const VMStateDescription *sub_vmsd;
  395. len = qemu_peek_byte(f, 1);
  396. if (len < strlen(vmsd->name) + 1) {
  397. /* subsection name has be be "section_name/a" */
  398. trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
  399. return 0;
  400. }
  401. size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
  402. if (size != len) {
  403. trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
  404. return 0;
  405. }
  406. memcpy(idstr, idstr_ret, size);
  407. idstr[size] = 0;
  408. if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
  409. trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
  410. /* it doesn't have a valid subsection name */
  411. return 0;
  412. }
  413. sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
  414. if (sub_vmsd == NULL) {
  415. trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
  416. return -ENOENT;
  417. }
  418. qemu_file_skip(f, 1); /* subsection */
  419. qemu_file_skip(f, 1); /* len */
  420. qemu_file_skip(f, len); /* idstr */
  421. version_id = qemu_get_be32(f);
  422. ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
  423. if (ret) {
  424. trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
  425. return ret;
  426. }
  427. }
  428. trace_vmstate_subsection_load_good(vmsd->name);
  429. return 0;
  430. }
  431. static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
  432. void *opaque, JSONWriter *vmdesc)
  433. {
  434. const VMStateDescription **sub = vmsd->subsections;
  435. bool vmdesc_has_subsections = false;
  436. int ret = 0;
  437. trace_vmstate_subsection_save_top(vmsd->name);
  438. while (sub && *sub) {
  439. if (vmstate_save_needed(*sub, opaque)) {
  440. const VMStateDescription *vmsdsub = *sub;
  441. uint8_t len;
  442. trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
  443. if (vmdesc) {
  444. /* Only create subsection array when we have any */
  445. if (!vmdesc_has_subsections) {
  446. json_writer_start_array(vmdesc, "subsections");
  447. vmdesc_has_subsections = true;
  448. }
  449. json_writer_start_object(vmdesc, NULL);
  450. }
  451. qemu_put_byte(f, QEMU_VM_SUBSECTION);
  452. len = strlen(vmsdsub->name);
  453. qemu_put_byte(f, len);
  454. qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
  455. qemu_put_be32(f, vmsdsub->version_id);
  456. ret = vmstate_save_state(f, vmsdsub, opaque, vmdesc);
  457. if (ret) {
  458. return ret;
  459. }
  460. if (vmdesc) {
  461. json_writer_end_object(vmdesc);
  462. }
  463. }
  464. sub++;
  465. }
  466. if (vmdesc_has_subsections) {
  467. json_writer_end_array(vmdesc);
  468. }
  469. return ret;
  470. }