vmstate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. ret = 0;
  319. vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
  320. old_offset = qemu_ftell_fast(f);
  321. if (field->flags & VMS_ARRAY_OF_POINTER) {
  322. assert(curr_elem);
  323. curr_elem = *(void **)curr_elem;
  324. }
  325. if (!curr_elem && size) {
  326. /* if null pointer write placeholder and do not follow */
  327. assert(field->flags & VMS_ARRAY_OF_POINTER);
  328. ret = vmstate_info_nullptr.put(f, curr_elem, size, NULL,
  329. NULL);
  330. } else if (field->flags & VMS_STRUCT) {
  331. ret = vmstate_save_state(f, field->vmsd, curr_elem,
  332. vmdesc_loop);
  333. } else if (field->flags & VMS_VSTRUCT) {
  334. ret = vmstate_save_state_v(f, field->vmsd, curr_elem,
  335. vmdesc_loop,
  336. field->struct_version_id);
  337. } else {
  338. ret = field->info->put(f, curr_elem, size, field,
  339. vmdesc_loop);
  340. }
  341. if (ret) {
  342. error_report("Save of field %s/%s failed",
  343. vmsd->name, field->name);
  344. if (vmsd->post_save) {
  345. vmsd->post_save(opaque);
  346. }
  347. return ret;
  348. }
  349. written_bytes = qemu_ftell_fast(f) - old_offset;
  350. vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);
  351. /* Compressed arrays only care about the first element */
  352. if (vmdesc_loop && vmsd_can_compress(field)) {
  353. vmdesc_loop = NULL;
  354. }
  355. }
  356. } else {
  357. if (field->flags & VMS_MUST_EXIST) {
  358. error_report("Output state validation failed: %s/%s",
  359. vmsd->name, field->name);
  360. assert(!(field->flags & VMS_MUST_EXIST));
  361. }
  362. }
  363. field++;
  364. }
  365. if (vmdesc) {
  366. json_end_array(vmdesc);
  367. }
  368. ret = vmstate_subsection_save(f, vmsd, opaque, vmdesc);
  369. if (vmsd->post_save) {
  370. int ps_ret = vmsd->post_save(opaque);
  371. if (!ret) {
  372. ret = ps_ret;
  373. }
  374. }
  375. return ret;
  376. }
  377. static const VMStateDescription *
  378. vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
  379. {
  380. while (sub && *sub) {
  381. if (strcmp(idstr, (*sub)->name) == 0) {
  382. return *sub;
  383. }
  384. sub++;
  385. }
  386. return NULL;
  387. }
  388. static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
  389. void *opaque)
  390. {
  391. trace_vmstate_subsection_load(vmsd->name);
  392. while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
  393. char idstr[256], *idstr_ret;
  394. int ret;
  395. uint8_t version_id, len, size;
  396. const VMStateDescription *sub_vmsd;
  397. len = qemu_peek_byte(f, 1);
  398. if (len < strlen(vmsd->name) + 1) {
  399. /* subsection name has be be "section_name/a" */
  400. trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
  401. return 0;
  402. }
  403. size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
  404. if (size != len) {
  405. trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
  406. return 0;
  407. }
  408. memcpy(idstr, idstr_ret, size);
  409. idstr[size] = 0;
  410. if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
  411. trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
  412. /* it doesn't have a valid subsection name */
  413. return 0;
  414. }
  415. sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
  416. if (sub_vmsd == NULL) {
  417. trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
  418. return -ENOENT;
  419. }
  420. qemu_file_skip(f, 1); /* subsection */
  421. qemu_file_skip(f, 1); /* len */
  422. qemu_file_skip(f, len); /* idstr */
  423. version_id = qemu_get_be32(f);
  424. ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
  425. if (ret) {
  426. trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
  427. return ret;
  428. }
  429. }
  430. trace_vmstate_subsection_load_good(vmsd->name);
  431. return 0;
  432. }
  433. static int vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
  434. void *opaque, QJSON *vmdesc)
  435. {
  436. const VMStateDescription **sub = vmsd->subsections;
  437. bool vmdesc_has_subsections = false;
  438. int ret = 0;
  439. trace_vmstate_subsection_save_top(vmsd->name);
  440. while (sub && *sub) {
  441. if (vmstate_save_needed(*sub, opaque)) {
  442. const VMStateDescription *vmsdsub = *sub;
  443. uint8_t len;
  444. trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
  445. if (vmdesc) {
  446. /* Only create subsection array when we have any */
  447. if (!vmdesc_has_subsections) {
  448. json_start_array(vmdesc, "subsections");
  449. vmdesc_has_subsections = true;
  450. }
  451. json_start_object(vmdesc, NULL);
  452. }
  453. qemu_put_byte(f, QEMU_VM_SUBSECTION);
  454. len = strlen(vmsdsub->name);
  455. qemu_put_byte(f, len);
  456. qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
  457. qemu_put_be32(f, vmsdsub->version_id);
  458. ret = vmstate_save_state(f, vmsdsub, opaque, vmdesc);
  459. if (ret) {
  460. return ret;
  461. }
  462. if (vmdesc) {
  463. json_end_object(vmdesc);
  464. }
  465. }
  466. sub++;
  467. }
  468. if (vmdesc_has_subsections) {
  469. json_end_array(vmdesc);
  470. }
  471. return ret;
  472. }