vmstate.c 17 KB

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