savevm.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config-host.h"
  25. #include "qemu-common.h"
  26. #include "hw/boards.h"
  27. #include "hw/hw.h"
  28. #include "hw/qdev.h"
  29. #include "net/net.h"
  30. #include "monitor/monitor.h"
  31. #include "sysemu/sysemu.h"
  32. #include "qemu/timer.h"
  33. #include "audio/audio.h"
  34. #include "migration/migration.h"
  35. #include "qemu/sockets.h"
  36. #include "qemu/queue.h"
  37. #include "sysemu/cpus.h"
  38. #include "exec/memory.h"
  39. #include "qmp-commands.h"
  40. #include "trace.h"
  41. #include "qemu/iov.h"
  42. #include "block/snapshot.h"
  43. #include "block/qapi.h"
  44. #ifndef ETH_P_RARP
  45. #define ETH_P_RARP 0x8035
  46. #endif
  47. #define ARP_HTYPE_ETH 0x0001
  48. #define ARP_PTYPE_IP 0x0800
  49. #define ARP_OP_REQUEST_REV 0x3
  50. static int announce_self_create(uint8_t *buf,
  51. uint8_t *mac_addr)
  52. {
  53. /* Ethernet header. */
  54. memset(buf, 0xff, 6); /* destination MAC addr */
  55. memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
  56. *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
  57. /* RARP header. */
  58. *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
  59. *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
  60. *(buf + 18) = 6; /* hardware addr length (ethernet) */
  61. *(buf + 19) = 4; /* protocol addr length (IPv4) */
  62. *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
  63. memcpy(buf + 22, mac_addr, 6); /* source hw addr */
  64. memset(buf + 28, 0x00, 4); /* source protocol addr */
  65. memcpy(buf + 32, mac_addr, 6); /* target hw addr */
  66. memset(buf + 38, 0x00, 4); /* target protocol addr */
  67. /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
  68. memset(buf + 42, 0x00, 18);
  69. return 60; /* len (FCS will be added by hardware) */
  70. }
  71. static void qemu_announce_self_iter(NICState *nic, void *opaque)
  72. {
  73. uint8_t buf[60];
  74. int len;
  75. trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
  76. len = announce_self_create(buf, nic->conf->macaddr.a);
  77. qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
  78. }
  79. static void qemu_announce_self_once(void *opaque)
  80. {
  81. static int count = SELF_ANNOUNCE_ROUNDS;
  82. QEMUTimer *timer = *(QEMUTimer **)opaque;
  83. qemu_foreach_nic(qemu_announce_self_iter, NULL);
  84. if (--count) {
  85. /* delay 50ms, 150ms, 250ms, ... */
  86. timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
  87. self_announce_delay(count));
  88. } else {
  89. timer_del(timer);
  90. timer_free(timer);
  91. }
  92. }
  93. void qemu_announce_self(void)
  94. {
  95. static QEMUTimer *timer;
  96. timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
  97. qemu_announce_self_once(&timer);
  98. }
  99. /***********************************************************/
  100. /* savevm/loadvm support */
  101. static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
  102. int64_t pos)
  103. {
  104. int ret;
  105. QEMUIOVector qiov;
  106. qemu_iovec_init_external(&qiov, iov, iovcnt);
  107. ret = bdrv_writev_vmstate(opaque, &qiov, pos);
  108. if (ret < 0) {
  109. return ret;
  110. }
  111. return qiov.size;
  112. }
  113. static int block_put_buffer(void *opaque, const uint8_t *buf,
  114. int64_t pos, int size)
  115. {
  116. bdrv_save_vmstate(opaque, buf, pos, size);
  117. return size;
  118. }
  119. static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  120. {
  121. return bdrv_load_vmstate(opaque, buf, pos, size);
  122. }
  123. static int bdrv_fclose(void *opaque)
  124. {
  125. return bdrv_flush(opaque);
  126. }
  127. static const QEMUFileOps bdrv_read_ops = {
  128. .get_buffer = block_get_buffer,
  129. .close = bdrv_fclose
  130. };
  131. static const QEMUFileOps bdrv_write_ops = {
  132. .put_buffer = block_put_buffer,
  133. .writev_buffer = block_writev_buffer,
  134. .close = bdrv_fclose
  135. };
  136. static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
  137. {
  138. if (is_writable) {
  139. return qemu_fopen_ops(bs, &bdrv_write_ops);
  140. }
  141. return qemu_fopen_ops(bs, &bdrv_read_ops);
  142. }
  143. /* QEMUFile timer support.
  144. * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
  145. */
  146. void timer_put(QEMUFile *f, QEMUTimer *ts)
  147. {
  148. uint64_t expire_time;
  149. expire_time = timer_expire_time_ns(ts);
  150. qemu_put_be64(f, expire_time);
  151. }
  152. void timer_get(QEMUFile *f, QEMUTimer *ts)
  153. {
  154. uint64_t expire_time;
  155. expire_time = qemu_get_be64(f);
  156. if (expire_time != -1) {
  157. timer_mod_ns(ts, expire_time);
  158. } else {
  159. timer_del(ts);
  160. }
  161. }
  162. /* VMState timer support.
  163. * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
  164. */
  165. static int get_timer(QEMUFile *f, void *pv, size_t size)
  166. {
  167. QEMUTimer *v = pv;
  168. timer_get(f, v);
  169. return 0;
  170. }
  171. static void put_timer(QEMUFile *f, void *pv, size_t size)
  172. {
  173. QEMUTimer *v = pv;
  174. timer_put(f, v);
  175. }
  176. const VMStateInfo vmstate_info_timer = {
  177. .name = "timer",
  178. .get = get_timer,
  179. .put = put_timer,
  180. };
  181. typedef struct CompatEntry {
  182. char idstr[256];
  183. int instance_id;
  184. } CompatEntry;
  185. typedef struct SaveStateEntry {
  186. QTAILQ_ENTRY(SaveStateEntry) entry;
  187. char idstr[256];
  188. int instance_id;
  189. int alias_id;
  190. int version_id;
  191. int section_id;
  192. SaveVMHandlers *ops;
  193. const VMStateDescription *vmsd;
  194. void *opaque;
  195. CompatEntry *compat;
  196. int is_ram;
  197. } SaveStateEntry;
  198. static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
  199. QTAILQ_HEAD_INITIALIZER(savevm_handlers);
  200. static int global_section_id;
  201. static void dump_vmstate_vmsd(FILE *out_file,
  202. const VMStateDescription *vmsd, int indent,
  203. bool is_subsection);
  204. static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
  205. int indent)
  206. {
  207. fprintf(out_file, "%*s{\n", indent, "");
  208. indent += 2;
  209. fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
  210. fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
  211. field->version_id);
  212. fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
  213. field->field_exists ? "true" : "false");
  214. fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
  215. if (field->vmsd != NULL) {
  216. fprintf(out_file, ",\n");
  217. dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
  218. }
  219. fprintf(out_file, "\n%*s}", indent - 2, "");
  220. }
  221. static void dump_vmstate_vmss(FILE *out_file,
  222. const VMStateSubsection *subsection,
  223. int indent)
  224. {
  225. if (subsection->vmsd != NULL) {
  226. dump_vmstate_vmsd(out_file, subsection->vmsd, indent, true);
  227. }
  228. }
  229. static void dump_vmstate_vmsd(FILE *out_file,
  230. const VMStateDescription *vmsd, int indent,
  231. bool is_subsection)
  232. {
  233. if (is_subsection) {
  234. fprintf(out_file, "%*s{\n", indent, "");
  235. } else {
  236. fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
  237. }
  238. indent += 2;
  239. fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
  240. fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
  241. vmsd->version_id);
  242. fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
  243. vmsd->minimum_version_id);
  244. if (vmsd->fields != NULL) {
  245. const VMStateField *field = vmsd->fields;
  246. bool first;
  247. fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
  248. first = true;
  249. while (field->name != NULL) {
  250. if (field->flags & VMS_MUST_EXIST) {
  251. /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
  252. field++;
  253. continue;
  254. }
  255. if (!first) {
  256. fprintf(out_file, ",\n");
  257. }
  258. dump_vmstate_vmsf(out_file, field, indent + 2);
  259. field++;
  260. first = false;
  261. }
  262. fprintf(out_file, "\n%*s]", indent, "");
  263. }
  264. if (vmsd->subsections != NULL) {
  265. const VMStateSubsection *subsection = vmsd->subsections;
  266. bool first;
  267. fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
  268. first = true;
  269. while (subsection->vmsd != NULL) {
  270. if (!first) {
  271. fprintf(out_file, ",\n");
  272. }
  273. dump_vmstate_vmss(out_file, subsection, indent + 2);
  274. subsection++;
  275. first = false;
  276. }
  277. fprintf(out_file, "\n%*s]", indent, "");
  278. }
  279. fprintf(out_file, "\n%*s}", indent - 2, "");
  280. }
  281. static void dump_machine_type(FILE *out_file)
  282. {
  283. MachineClass *mc;
  284. mc = MACHINE_GET_CLASS(current_machine);
  285. fprintf(out_file, " \"vmschkmachine\": {\n");
  286. fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
  287. fprintf(out_file, " },\n");
  288. }
  289. void dump_vmstate_json_to_file(FILE *out_file)
  290. {
  291. GSList *list, *elt;
  292. bool first;
  293. fprintf(out_file, "{\n");
  294. dump_machine_type(out_file);
  295. first = true;
  296. list = object_class_get_list(TYPE_DEVICE, true);
  297. for (elt = list; elt; elt = elt->next) {
  298. DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
  299. TYPE_DEVICE);
  300. const char *name;
  301. int indent = 2;
  302. if (!dc->vmsd) {
  303. continue;
  304. }
  305. if (!first) {
  306. fprintf(out_file, ",\n");
  307. }
  308. name = object_class_get_name(OBJECT_CLASS(dc));
  309. fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
  310. indent += 2;
  311. fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
  312. fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
  313. dc->vmsd->version_id);
  314. fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
  315. dc->vmsd->minimum_version_id);
  316. dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
  317. fprintf(out_file, "\n%*s}", indent - 2, "");
  318. first = false;
  319. }
  320. fprintf(out_file, "\n}\n");
  321. fclose(out_file);
  322. }
  323. static int calculate_new_instance_id(const char *idstr)
  324. {
  325. SaveStateEntry *se;
  326. int instance_id = 0;
  327. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  328. if (strcmp(idstr, se->idstr) == 0
  329. && instance_id <= se->instance_id) {
  330. instance_id = se->instance_id + 1;
  331. }
  332. }
  333. return instance_id;
  334. }
  335. static int calculate_compat_instance_id(const char *idstr)
  336. {
  337. SaveStateEntry *se;
  338. int instance_id = 0;
  339. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  340. if (!se->compat) {
  341. continue;
  342. }
  343. if (strcmp(idstr, se->compat->idstr) == 0
  344. && instance_id <= se->compat->instance_id) {
  345. instance_id = se->compat->instance_id + 1;
  346. }
  347. }
  348. return instance_id;
  349. }
  350. /* TODO: Individual devices generally have very little idea about the rest
  351. of the system, so instance_id should be removed/replaced.
  352. Meanwhile pass -1 as instance_id if you do not already have a clearly
  353. distinguishing id for all instances of your device class. */
  354. int register_savevm_live(DeviceState *dev,
  355. const char *idstr,
  356. int instance_id,
  357. int version_id,
  358. SaveVMHandlers *ops,
  359. void *opaque)
  360. {
  361. SaveStateEntry *se;
  362. se = g_malloc0(sizeof(SaveStateEntry));
  363. se->version_id = version_id;
  364. se->section_id = global_section_id++;
  365. se->ops = ops;
  366. se->opaque = opaque;
  367. se->vmsd = NULL;
  368. /* if this is a live_savem then set is_ram */
  369. if (ops->save_live_setup != NULL) {
  370. se->is_ram = 1;
  371. }
  372. if (dev) {
  373. char *id = qdev_get_dev_path(dev);
  374. if (id) {
  375. pstrcpy(se->idstr, sizeof(se->idstr), id);
  376. pstrcat(se->idstr, sizeof(se->idstr), "/");
  377. g_free(id);
  378. se->compat = g_malloc0(sizeof(CompatEntry));
  379. pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
  380. se->compat->instance_id = instance_id == -1 ?
  381. calculate_compat_instance_id(idstr) : instance_id;
  382. instance_id = -1;
  383. }
  384. }
  385. pstrcat(se->idstr, sizeof(se->idstr), idstr);
  386. if (instance_id == -1) {
  387. se->instance_id = calculate_new_instance_id(se->idstr);
  388. } else {
  389. se->instance_id = instance_id;
  390. }
  391. assert(!se->compat || se->instance_id == 0);
  392. /* add at the end of list */
  393. QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
  394. return 0;
  395. }
  396. int register_savevm(DeviceState *dev,
  397. const char *idstr,
  398. int instance_id,
  399. int version_id,
  400. SaveStateHandler *save_state,
  401. LoadStateHandler *load_state,
  402. void *opaque)
  403. {
  404. SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
  405. ops->save_state = save_state;
  406. ops->load_state = load_state;
  407. return register_savevm_live(dev, idstr, instance_id, version_id,
  408. ops, opaque);
  409. }
  410. void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
  411. {
  412. SaveStateEntry *se, *new_se;
  413. char id[256] = "";
  414. if (dev) {
  415. char *path = qdev_get_dev_path(dev);
  416. if (path) {
  417. pstrcpy(id, sizeof(id), path);
  418. pstrcat(id, sizeof(id), "/");
  419. g_free(path);
  420. }
  421. }
  422. pstrcat(id, sizeof(id), idstr);
  423. QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
  424. if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
  425. QTAILQ_REMOVE(&savevm_handlers, se, entry);
  426. if (se->compat) {
  427. g_free(se->compat);
  428. }
  429. g_free(se->ops);
  430. g_free(se);
  431. }
  432. }
  433. }
  434. int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
  435. const VMStateDescription *vmsd,
  436. void *opaque, int alias_id,
  437. int required_for_version)
  438. {
  439. SaveStateEntry *se;
  440. /* If this triggers, alias support can be dropped for the vmsd. */
  441. assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
  442. se = g_malloc0(sizeof(SaveStateEntry));
  443. se->version_id = vmsd->version_id;
  444. se->section_id = global_section_id++;
  445. se->opaque = opaque;
  446. se->vmsd = vmsd;
  447. se->alias_id = alias_id;
  448. if (dev) {
  449. char *id = qdev_get_dev_path(dev);
  450. if (id) {
  451. pstrcpy(se->idstr, sizeof(se->idstr), id);
  452. pstrcat(se->idstr, sizeof(se->idstr), "/");
  453. g_free(id);
  454. se->compat = g_malloc0(sizeof(CompatEntry));
  455. pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
  456. se->compat->instance_id = instance_id == -1 ?
  457. calculate_compat_instance_id(vmsd->name) : instance_id;
  458. instance_id = -1;
  459. }
  460. }
  461. pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
  462. if (instance_id == -1) {
  463. se->instance_id = calculate_new_instance_id(se->idstr);
  464. } else {
  465. se->instance_id = instance_id;
  466. }
  467. assert(!se->compat || se->instance_id == 0);
  468. /* add at the end of list */
  469. QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
  470. return 0;
  471. }
  472. void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
  473. void *opaque)
  474. {
  475. SaveStateEntry *se, *new_se;
  476. QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
  477. if (se->vmsd == vmsd && se->opaque == opaque) {
  478. QTAILQ_REMOVE(&savevm_handlers, se, entry);
  479. if (se->compat) {
  480. g_free(se->compat);
  481. }
  482. g_free(se);
  483. }
  484. }
  485. }
  486. static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
  487. {
  488. trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
  489. if (!se->vmsd) { /* Old style */
  490. return se->ops->load_state(f, se->opaque, version_id);
  491. }
  492. return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
  493. }
  494. static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
  495. {
  496. trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
  497. if (!se->vmsd) { /* Old style */
  498. se->ops->save_state(f, se->opaque);
  499. return;
  500. }
  501. vmstate_save_state(f, se->vmsd, se->opaque);
  502. }
  503. bool qemu_savevm_state_blocked(Error **errp)
  504. {
  505. SaveStateEntry *se;
  506. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  507. if (se->vmsd && se->vmsd->unmigratable) {
  508. error_setg(errp, "State blocked by non-migratable device '%s'",
  509. se->idstr);
  510. return true;
  511. }
  512. }
  513. return false;
  514. }
  515. void qemu_savevm_state_begin(QEMUFile *f,
  516. const MigrationParams *params)
  517. {
  518. SaveStateEntry *se;
  519. int ret;
  520. trace_savevm_state_begin();
  521. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  522. if (!se->ops || !se->ops->set_params) {
  523. continue;
  524. }
  525. se->ops->set_params(params, se->opaque);
  526. }
  527. qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  528. qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  529. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  530. int len;
  531. if (!se->ops || !se->ops->save_live_setup) {
  532. continue;
  533. }
  534. if (se->ops && se->ops->is_active) {
  535. if (!se->ops->is_active(se->opaque)) {
  536. continue;
  537. }
  538. }
  539. /* Section type */
  540. qemu_put_byte(f, QEMU_VM_SECTION_START);
  541. qemu_put_be32(f, se->section_id);
  542. /* ID string */
  543. len = strlen(se->idstr);
  544. qemu_put_byte(f, len);
  545. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  546. qemu_put_be32(f, se->instance_id);
  547. qemu_put_be32(f, se->version_id);
  548. ret = se->ops->save_live_setup(f, se->opaque);
  549. if (ret < 0) {
  550. qemu_file_set_error(f, ret);
  551. break;
  552. }
  553. }
  554. }
  555. /*
  556. * this function has three return values:
  557. * negative: there was one error, and we have -errno.
  558. * 0 : We haven't finished, caller have to go again
  559. * 1 : We have finished, we can go to complete phase
  560. */
  561. int qemu_savevm_state_iterate(QEMUFile *f)
  562. {
  563. SaveStateEntry *se;
  564. int ret = 1;
  565. trace_savevm_state_iterate();
  566. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  567. if (!se->ops || !se->ops->save_live_iterate) {
  568. continue;
  569. }
  570. if (se->ops && se->ops->is_active) {
  571. if (!se->ops->is_active(se->opaque)) {
  572. continue;
  573. }
  574. }
  575. if (qemu_file_rate_limit(f)) {
  576. return 0;
  577. }
  578. trace_savevm_section_start(se->idstr, se->section_id);
  579. /* Section type */
  580. qemu_put_byte(f, QEMU_VM_SECTION_PART);
  581. qemu_put_be32(f, se->section_id);
  582. ret = se->ops->save_live_iterate(f, se->opaque);
  583. trace_savevm_section_end(se->idstr, se->section_id);
  584. if (ret < 0) {
  585. qemu_file_set_error(f, ret);
  586. }
  587. if (ret <= 0) {
  588. /* Do not proceed to the next vmstate before this one reported
  589. completion of the current stage. This serializes the migration
  590. and reduces the probability that a faster changing state is
  591. synchronized over and over again. */
  592. break;
  593. }
  594. }
  595. return ret;
  596. }
  597. void qemu_savevm_state_complete(QEMUFile *f)
  598. {
  599. SaveStateEntry *se;
  600. int ret;
  601. trace_savevm_state_complete();
  602. cpu_synchronize_all_states();
  603. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  604. if (!se->ops || !se->ops->save_live_complete) {
  605. continue;
  606. }
  607. if (se->ops && se->ops->is_active) {
  608. if (!se->ops->is_active(se->opaque)) {
  609. continue;
  610. }
  611. }
  612. trace_savevm_section_start(se->idstr, se->section_id);
  613. /* Section type */
  614. qemu_put_byte(f, QEMU_VM_SECTION_END);
  615. qemu_put_be32(f, se->section_id);
  616. ret = se->ops->save_live_complete(f, se->opaque);
  617. trace_savevm_section_end(se->idstr, se->section_id);
  618. if (ret < 0) {
  619. qemu_file_set_error(f, ret);
  620. return;
  621. }
  622. }
  623. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  624. int len;
  625. if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
  626. continue;
  627. }
  628. trace_savevm_section_start(se->idstr, se->section_id);
  629. /* Section type */
  630. qemu_put_byte(f, QEMU_VM_SECTION_FULL);
  631. qemu_put_be32(f, se->section_id);
  632. /* ID string */
  633. len = strlen(se->idstr);
  634. qemu_put_byte(f, len);
  635. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  636. qemu_put_be32(f, se->instance_id);
  637. qemu_put_be32(f, se->version_id);
  638. vmstate_save(f, se);
  639. trace_savevm_section_end(se->idstr, se->section_id);
  640. }
  641. qemu_put_byte(f, QEMU_VM_EOF);
  642. qemu_fflush(f);
  643. }
  644. uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
  645. {
  646. SaveStateEntry *se;
  647. uint64_t ret = 0;
  648. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  649. if (!se->ops || !se->ops->save_live_pending) {
  650. continue;
  651. }
  652. if (se->ops && se->ops->is_active) {
  653. if (!se->ops->is_active(se->opaque)) {
  654. continue;
  655. }
  656. }
  657. ret += se->ops->save_live_pending(f, se->opaque, max_size);
  658. }
  659. return ret;
  660. }
  661. void qemu_savevm_state_cancel(void)
  662. {
  663. SaveStateEntry *se;
  664. trace_savevm_state_cancel();
  665. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  666. if (se->ops && se->ops->cancel) {
  667. se->ops->cancel(se->opaque);
  668. }
  669. }
  670. }
  671. static int qemu_savevm_state(QEMUFile *f)
  672. {
  673. int ret;
  674. MigrationParams params = {
  675. .blk = 0,
  676. .shared = 0
  677. };
  678. if (qemu_savevm_state_blocked(NULL)) {
  679. return -EINVAL;
  680. }
  681. qemu_mutex_unlock_iothread();
  682. qemu_savevm_state_begin(f, &params);
  683. qemu_mutex_lock_iothread();
  684. while (qemu_file_get_error(f) == 0) {
  685. if (qemu_savevm_state_iterate(f) > 0) {
  686. break;
  687. }
  688. }
  689. ret = qemu_file_get_error(f);
  690. if (ret == 0) {
  691. qemu_savevm_state_complete(f);
  692. ret = qemu_file_get_error(f);
  693. }
  694. if (ret != 0) {
  695. qemu_savevm_state_cancel();
  696. }
  697. return ret;
  698. }
  699. static int qemu_save_device_state(QEMUFile *f)
  700. {
  701. SaveStateEntry *se;
  702. qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  703. qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  704. cpu_synchronize_all_states();
  705. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  706. int len;
  707. if (se->is_ram) {
  708. continue;
  709. }
  710. if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
  711. continue;
  712. }
  713. /* Section type */
  714. qemu_put_byte(f, QEMU_VM_SECTION_FULL);
  715. qemu_put_be32(f, se->section_id);
  716. /* ID string */
  717. len = strlen(se->idstr);
  718. qemu_put_byte(f, len);
  719. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  720. qemu_put_be32(f, se->instance_id);
  721. qemu_put_be32(f, se->version_id);
  722. vmstate_save(f, se);
  723. }
  724. qemu_put_byte(f, QEMU_VM_EOF);
  725. return qemu_file_get_error(f);
  726. }
  727. static SaveStateEntry *find_se(const char *idstr, int instance_id)
  728. {
  729. SaveStateEntry *se;
  730. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  731. if (!strcmp(se->idstr, idstr) &&
  732. (instance_id == se->instance_id ||
  733. instance_id == se->alias_id))
  734. return se;
  735. /* Migrating from an older version? */
  736. if (strstr(se->idstr, idstr) && se->compat) {
  737. if (!strcmp(se->compat->idstr, idstr) &&
  738. (instance_id == se->compat->instance_id ||
  739. instance_id == se->alias_id))
  740. return se;
  741. }
  742. }
  743. return NULL;
  744. }
  745. typedef struct LoadStateEntry {
  746. QLIST_ENTRY(LoadStateEntry) entry;
  747. SaveStateEntry *se;
  748. int section_id;
  749. int version_id;
  750. } LoadStateEntry;
  751. int qemu_loadvm_state(QEMUFile *f)
  752. {
  753. QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
  754. QLIST_HEAD_INITIALIZER(loadvm_handlers);
  755. LoadStateEntry *le, *new_le;
  756. uint8_t section_type;
  757. unsigned int v;
  758. int ret;
  759. if (qemu_savevm_state_blocked(NULL)) {
  760. return -EINVAL;
  761. }
  762. v = qemu_get_be32(f);
  763. if (v != QEMU_VM_FILE_MAGIC) {
  764. return -EINVAL;
  765. }
  766. v = qemu_get_be32(f);
  767. if (v == QEMU_VM_FILE_VERSION_COMPAT) {
  768. fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
  769. return -ENOTSUP;
  770. }
  771. if (v != QEMU_VM_FILE_VERSION) {
  772. return -ENOTSUP;
  773. }
  774. while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
  775. uint32_t instance_id, version_id, section_id;
  776. SaveStateEntry *se;
  777. char idstr[257];
  778. int len;
  779. switch (section_type) {
  780. case QEMU_VM_SECTION_START:
  781. case QEMU_VM_SECTION_FULL:
  782. /* Read section start */
  783. section_id = qemu_get_be32(f);
  784. len = qemu_get_byte(f);
  785. qemu_get_buffer(f, (uint8_t *)idstr, len);
  786. idstr[len] = 0;
  787. instance_id = qemu_get_be32(f);
  788. version_id = qemu_get_be32(f);
  789. /* Find savevm section */
  790. se = find_se(idstr, instance_id);
  791. if (se == NULL) {
  792. fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
  793. ret = -EINVAL;
  794. goto out;
  795. }
  796. /* Validate version */
  797. if (version_id > se->version_id) {
  798. fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
  799. version_id, idstr, se->version_id);
  800. ret = -EINVAL;
  801. goto out;
  802. }
  803. /* Add entry */
  804. le = g_malloc0(sizeof(*le));
  805. le->se = se;
  806. le->section_id = section_id;
  807. le->version_id = version_id;
  808. QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
  809. ret = vmstate_load(f, le->se, le->version_id);
  810. if (ret < 0) {
  811. fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
  812. instance_id, idstr);
  813. goto out;
  814. }
  815. break;
  816. case QEMU_VM_SECTION_PART:
  817. case QEMU_VM_SECTION_END:
  818. section_id = qemu_get_be32(f);
  819. QLIST_FOREACH(le, &loadvm_handlers, entry) {
  820. if (le->section_id == section_id) {
  821. break;
  822. }
  823. }
  824. if (le == NULL) {
  825. fprintf(stderr, "Unknown savevm section %d\n", section_id);
  826. ret = -EINVAL;
  827. goto out;
  828. }
  829. ret = vmstate_load(f, le->se, le->version_id);
  830. if (ret < 0) {
  831. fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
  832. section_id);
  833. goto out;
  834. }
  835. break;
  836. default:
  837. fprintf(stderr, "Unknown savevm section type %d\n", section_type);
  838. ret = -EINVAL;
  839. goto out;
  840. }
  841. }
  842. cpu_synchronize_all_post_init();
  843. ret = 0;
  844. out:
  845. QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
  846. QLIST_REMOVE(le, entry);
  847. g_free(le);
  848. }
  849. if (ret == 0) {
  850. ret = qemu_file_get_error(f);
  851. }
  852. return ret;
  853. }
  854. static BlockDriverState *find_vmstate_bs(void)
  855. {
  856. BlockDriverState *bs = NULL;
  857. while ((bs = bdrv_next(bs))) {
  858. if (bdrv_can_snapshot(bs)) {
  859. return bs;
  860. }
  861. }
  862. return NULL;
  863. }
  864. /*
  865. * Deletes snapshots of a given name in all opened images.
  866. */
  867. static int del_existing_snapshots(Monitor *mon, const char *name)
  868. {
  869. BlockDriverState *bs;
  870. QEMUSnapshotInfo sn1, *snapshot = &sn1;
  871. Error *err = NULL;
  872. bs = NULL;
  873. while ((bs = bdrv_next(bs))) {
  874. if (bdrv_can_snapshot(bs) &&
  875. bdrv_snapshot_find(bs, snapshot, name) >= 0) {
  876. bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
  877. if (err) {
  878. monitor_printf(mon,
  879. "Error while deleting snapshot on device '%s':"
  880. " %s\n",
  881. bdrv_get_device_name(bs),
  882. error_get_pretty(err));
  883. error_free(err);
  884. return -1;
  885. }
  886. }
  887. }
  888. return 0;
  889. }
  890. void do_savevm(Monitor *mon, const QDict *qdict)
  891. {
  892. BlockDriverState *bs, *bs1;
  893. QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
  894. int ret;
  895. QEMUFile *f;
  896. int saved_vm_running;
  897. uint64_t vm_state_size;
  898. qemu_timeval tv;
  899. struct tm tm;
  900. const char *name = qdict_get_try_str(qdict, "name");
  901. /* Verify if there is a device that doesn't support snapshots and is writable */
  902. bs = NULL;
  903. while ((bs = bdrv_next(bs))) {
  904. if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
  905. continue;
  906. }
  907. if (!bdrv_can_snapshot(bs)) {
  908. monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
  909. bdrv_get_device_name(bs));
  910. return;
  911. }
  912. }
  913. bs = find_vmstate_bs();
  914. if (!bs) {
  915. monitor_printf(mon, "No block device can accept snapshots\n");
  916. return;
  917. }
  918. saved_vm_running = runstate_is_running();
  919. vm_stop(RUN_STATE_SAVE_VM);
  920. memset(sn, 0, sizeof(*sn));
  921. /* fill auxiliary fields */
  922. qemu_gettimeofday(&tv);
  923. sn->date_sec = tv.tv_sec;
  924. sn->date_nsec = tv.tv_usec * 1000;
  925. sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  926. if (name) {
  927. ret = bdrv_snapshot_find(bs, old_sn, name);
  928. if (ret >= 0) {
  929. pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
  930. pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
  931. } else {
  932. pstrcpy(sn->name, sizeof(sn->name), name);
  933. }
  934. } else {
  935. /* cast below needed for OpenBSD where tv_sec is still 'long' */
  936. localtime_r((const time_t *)&tv.tv_sec, &tm);
  937. strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
  938. }
  939. /* Delete old snapshots of the same name */
  940. if (name && del_existing_snapshots(mon, name) < 0) {
  941. goto the_end;
  942. }
  943. /* save the VM state */
  944. f = qemu_fopen_bdrv(bs, 1);
  945. if (!f) {
  946. monitor_printf(mon, "Could not open VM state file\n");
  947. goto the_end;
  948. }
  949. ret = qemu_savevm_state(f);
  950. vm_state_size = qemu_ftell(f);
  951. qemu_fclose(f);
  952. if (ret < 0) {
  953. monitor_printf(mon, "Error %d while writing VM\n", ret);
  954. goto the_end;
  955. }
  956. /* create the snapshots */
  957. bs1 = NULL;
  958. while ((bs1 = bdrv_next(bs1))) {
  959. if (bdrv_can_snapshot(bs1)) {
  960. /* Write VM state size only to the image that contains the state */
  961. sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
  962. ret = bdrv_snapshot_create(bs1, sn);
  963. if (ret < 0) {
  964. monitor_printf(mon, "Error while creating snapshot on '%s'\n",
  965. bdrv_get_device_name(bs1));
  966. }
  967. }
  968. }
  969. the_end:
  970. if (saved_vm_running) {
  971. vm_start();
  972. }
  973. }
  974. void qmp_xen_save_devices_state(const char *filename, Error **errp)
  975. {
  976. QEMUFile *f;
  977. int saved_vm_running;
  978. int ret;
  979. saved_vm_running = runstate_is_running();
  980. vm_stop(RUN_STATE_SAVE_VM);
  981. f = qemu_fopen(filename, "wb");
  982. if (!f) {
  983. error_setg_file_open(errp, errno, filename);
  984. goto the_end;
  985. }
  986. ret = qemu_save_device_state(f);
  987. qemu_fclose(f);
  988. if (ret < 0) {
  989. error_set(errp, QERR_IO_ERROR);
  990. }
  991. the_end:
  992. if (saved_vm_running) {
  993. vm_start();
  994. }
  995. }
  996. int load_vmstate(const char *name)
  997. {
  998. BlockDriverState *bs, *bs_vm_state;
  999. QEMUSnapshotInfo sn;
  1000. QEMUFile *f;
  1001. int ret;
  1002. bs_vm_state = find_vmstate_bs();
  1003. if (!bs_vm_state) {
  1004. error_report("No block device supports snapshots");
  1005. return -ENOTSUP;
  1006. }
  1007. /* Don't even try to load empty VM states */
  1008. ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
  1009. if (ret < 0) {
  1010. return ret;
  1011. } else if (sn.vm_state_size == 0) {
  1012. error_report("This is a disk-only snapshot. Revert to it offline "
  1013. "using qemu-img.");
  1014. return -EINVAL;
  1015. }
  1016. /* Verify if there is any device that doesn't support snapshots and is
  1017. writable and check if the requested snapshot is available too. */
  1018. bs = NULL;
  1019. while ((bs = bdrv_next(bs))) {
  1020. if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
  1021. continue;
  1022. }
  1023. if (!bdrv_can_snapshot(bs)) {
  1024. error_report("Device '%s' is writable but does not support snapshots.",
  1025. bdrv_get_device_name(bs));
  1026. return -ENOTSUP;
  1027. }
  1028. ret = bdrv_snapshot_find(bs, &sn, name);
  1029. if (ret < 0) {
  1030. error_report("Device '%s' does not have the requested snapshot '%s'",
  1031. bdrv_get_device_name(bs), name);
  1032. return ret;
  1033. }
  1034. }
  1035. /* Flush all IO requests so they don't interfere with the new state. */
  1036. bdrv_drain_all();
  1037. bs = NULL;
  1038. while ((bs = bdrv_next(bs))) {
  1039. if (bdrv_can_snapshot(bs)) {
  1040. ret = bdrv_snapshot_goto(bs, name);
  1041. if (ret < 0) {
  1042. error_report("Error %d while activating snapshot '%s' on '%s'",
  1043. ret, name, bdrv_get_device_name(bs));
  1044. return ret;
  1045. }
  1046. }
  1047. }
  1048. /* restore the VM state */
  1049. f = qemu_fopen_bdrv(bs_vm_state, 0);
  1050. if (!f) {
  1051. error_report("Could not open VM state file");
  1052. return -EINVAL;
  1053. }
  1054. qemu_system_reset(VMRESET_SILENT);
  1055. ret = qemu_loadvm_state(f);
  1056. qemu_fclose(f);
  1057. if (ret < 0) {
  1058. error_report("Error %d while loading VM state", ret);
  1059. return ret;
  1060. }
  1061. return 0;
  1062. }
  1063. void do_delvm(Monitor *mon, const QDict *qdict)
  1064. {
  1065. BlockDriverState *bs;
  1066. Error *err;
  1067. const char *name = qdict_get_str(qdict, "name");
  1068. if (!find_vmstate_bs()) {
  1069. monitor_printf(mon, "No block device supports snapshots\n");
  1070. return;
  1071. }
  1072. bs = NULL;
  1073. while ((bs = bdrv_next(bs))) {
  1074. if (bdrv_can_snapshot(bs)) {
  1075. err = NULL;
  1076. bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
  1077. if (err) {
  1078. monitor_printf(mon,
  1079. "Error while deleting snapshot on device '%s':"
  1080. " %s\n",
  1081. bdrv_get_device_name(bs),
  1082. error_get_pretty(err));
  1083. error_free(err);
  1084. }
  1085. }
  1086. }
  1087. }
  1088. void do_info_snapshots(Monitor *mon, const QDict *qdict)
  1089. {
  1090. BlockDriverState *bs, *bs1;
  1091. QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
  1092. int nb_sns, i, ret, available;
  1093. int total;
  1094. int *available_snapshots;
  1095. bs = find_vmstate_bs();
  1096. if (!bs) {
  1097. monitor_printf(mon, "No available block device supports snapshots\n");
  1098. return;
  1099. }
  1100. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  1101. if (nb_sns < 0) {
  1102. monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
  1103. return;
  1104. }
  1105. if (nb_sns == 0) {
  1106. monitor_printf(mon, "There is no snapshot available.\n");
  1107. return;
  1108. }
  1109. available_snapshots = g_malloc0(sizeof(int) * nb_sns);
  1110. total = 0;
  1111. for (i = 0; i < nb_sns; i++) {
  1112. sn = &sn_tab[i];
  1113. available = 1;
  1114. bs1 = NULL;
  1115. while ((bs1 = bdrv_next(bs1))) {
  1116. if (bdrv_can_snapshot(bs1) && bs1 != bs) {
  1117. ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
  1118. if (ret < 0) {
  1119. available = 0;
  1120. break;
  1121. }
  1122. }
  1123. }
  1124. if (available) {
  1125. available_snapshots[total] = i;
  1126. total++;
  1127. }
  1128. }
  1129. if (total > 0) {
  1130. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
  1131. monitor_printf(mon, "\n");
  1132. for (i = 0; i < total; i++) {
  1133. sn = &sn_tab[available_snapshots[i]];
  1134. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
  1135. monitor_printf(mon, "\n");
  1136. }
  1137. } else {
  1138. monitor_printf(mon, "There is no suitable snapshot available\n");
  1139. }
  1140. g_free(sn_tab);
  1141. g_free(available_snapshots);
  1142. }
  1143. void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
  1144. {
  1145. qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
  1146. memory_region_name(mr), dev);
  1147. }
  1148. void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
  1149. {
  1150. qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
  1151. }
  1152. void vmstate_register_ram_global(MemoryRegion *mr)
  1153. {
  1154. vmstate_register_ram(mr, NULL);
  1155. }