2
0

savevm.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  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 <unistd.h>
  25. #include <fcntl.h>
  26. #include <time.h>
  27. #include <errno.h>
  28. #include <sys/time.h>
  29. #include <zlib.h>
  30. /* Needed early for CONFIG_BSD etc. */
  31. #include "config-host.h"
  32. #ifndef _WIN32
  33. #include <sys/times.h>
  34. #include <sys/wait.h>
  35. #include <termios.h>
  36. #include <sys/mman.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/resource.h>
  39. #include <sys/socket.h>
  40. #include <netinet/in.h>
  41. #include <net/if.h>
  42. #include <arpa/inet.h>
  43. #include <dirent.h>
  44. #include <netdb.h>
  45. #include <sys/select.h>
  46. #ifdef CONFIG_BSD
  47. #include <sys/stat.h>
  48. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  49. #include <libutil.h>
  50. #else
  51. #include <util.h>
  52. #endif
  53. #ifdef __linux__
  54. #include <pty.h>
  55. #include <malloc.h>
  56. #include <linux/rtc.h>
  57. #endif
  58. #endif
  59. #endif
  60. #ifdef _WIN32
  61. #include <windows.h>
  62. #include <malloc.h>
  63. #include <sys/timeb.h>
  64. #include <mmsystem.h>
  65. #define getopt_long_only getopt_long
  66. #define memalign(align, size) malloc(size)
  67. #endif
  68. #include "qemu-common.h"
  69. #include "hw/hw.h"
  70. #include "hw/qdev.h"
  71. #include "net.h"
  72. #include "monitor.h"
  73. #include "sysemu.h"
  74. #include "qemu-timer.h"
  75. #include "qemu-char.h"
  76. #include "audio/audio.h"
  77. #include "migration.h"
  78. #include "qemu_socket.h"
  79. #include "qemu-queue.h"
  80. #include "qemu-timer.h"
  81. #include "cpus.h"
  82. #include "memory.h"
  83. #include "qmp-commands.h"
  84. #include "trace.h"
  85. #define SELF_ANNOUNCE_ROUNDS 5
  86. #ifndef ETH_P_RARP
  87. #define ETH_P_RARP 0x8035
  88. #endif
  89. #define ARP_HTYPE_ETH 0x0001
  90. #define ARP_PTYPE_IP 0x0800
  91. #define ARP_OP_REQUEST_REV 0x3
  92. static int announce_self_create(uint8_t *buf,
  93. uint8_t *mac_addr)
  94. {
  95. /* Ethernet header. */
  96. memset(buf, 0xff, 6); /* destination MAC addr */
  97. memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
  98. *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
  99. /* RARP header. */
  100. *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
  101. *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
  102. *(buf + 18) = 6; /* hardware addr length (ethernet) */
  103. *(buf + 19) = 4; /* protocol addr length (IPv4) */
  104. *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
  105. memcpy(buf + 22, mac_addr, 6); /* source hw addr */
  106. memset(buf + 28, 0x00, 4); /* source protocol addr */
  107. memcpy(buf + 32, mac_addr, 6); /* target hw addr */
  108. memset(buf + 38, 0x00, 4); /* target protocol addr */
  109. /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
  110. memset(buf + 42, 0x00, 18);
  111. return 60; /* len (FCS will be added by hardware) */
  112. }
  113. static void qemu_announce_self_iter(NICState *nic, void *opaque)
  114. {
  115. uint8_t buf[60];
  116. int len;
  117. len = announce_self_create(buf, nic->conf->macaddr.a);
  118. qemu_send_packet_raw(&nic->nc, buf, len);
  119. }
  120. static void qemu_announce_self_once(void *opaque)
  121. {
  122. static int count = SELF_ANNOUNCE_ROUNDS;
  123. QEMUTimer *timer = *(QEMUTimer **)opaque;
  124. qemu_foreach_nic(qemu_announce_self_iter, NULL);
  125. if (--count) {
  126. /* delay 50ms, 150ms, 250ms, ... */
  127. qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
  128. 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
  129. } else {
  130. qemu_del_timer(timer);
  131. qemu_free_timer(timer);
  132. }
  133. }
  134. void qemu_announce_self(void)
  135. {
  136. static QEMUTimer *timer;
  137. timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
  138. qemu_announce_self_once(&timer);
  139. }
  140. /***********************************************************/
  141. /* savevm/loadvm support */
  142. #define IO_BUF_SIZE 32768
  143. struct QEMUFile {
  144. QEMUFilePutBufferFunc *put_buffer;
  145. QEMUFileGetBufferFunc *get_buffer;
  146. QEMUFileCloseFunc *close;
  147. QEMUFileRateLimit *rate_limit;
  148. QEMUFileSetRateLimit *set_rate_limit;
  149. QEMUFileGetRateLimit *get_rate_limit;
  150. void *opaque;
  151. int is_write;
  152. int64_t buf_offset; /* start of buffer when writing, end of buffer
  153. when reading */
  154. int buf_index;
  155. int buf_size; /* 0 when writing */
  156. uint8_t buf[IO_BUF_SIZE];
  157. int last_error;
  158. };
  159. typedef struct QEMUFileStdio
  160. {
  161. FILE *stdio_file;
  162. QEMUFile *file;
  163. } QEMUFileStdio;
  164. typedef struct QEMUFileSocket
  165. {
  166. int fd;
  167. QEMUFile *file;
  168. } QEMUFileSocket;
  169. static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  170. {
  171. QEMUFileSocket *s = opaque;
  172. ssize_t len;
  173. do {
  174. len = qemu_recv(s->fd, buf, size, 0);
  175. } while (len == -1 && socket_error() == EINTR);
  176. if (len == -1)
  177. len = -socket_error();
  178. return len;
  179. }
  180. static int socket_close(void *opaque)
  181. {
  182. QEMUFileSocket *s = opaque;
  183. g_free(s);
  184. return 0;
  185. }
  186. static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
  187. {
  188. QEMUFileStdio *s = opaque;
  189. return fwrite(buf, 1, size, s->stdio_file);
  190. }
  191. static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  192. {
  193. QEMUFileStdio *s = opaque;
  194. FILE *fp = s->stdio_file;
  195. int bytes;
  196. do {
  197. clearerr(fp);
  198. bytes = fread(buf, 1, size, fp);
  199. } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
  200. return bytes;
  201. }
  202. static int stdio_pclose(void *opaque)
  203. {
  204. QEMUFileStdio *s = opaque;
  205. int ret;
  206. ret = pclose(s->stdio_file);
  207. if (ret == -1) {
  208. ret = -errno;
  209. }
  210. g_free(s);
  211. return ret;
  212. }
  213. static int stdio_fclose(void *opaque)
  214. {
  215. QEMUFileStdio *s = opaque;
  216. int ret = 0;
  217. if (fclose(s->stdio_file) == EOF) {
  218. ret = -errno;
  219. }
  220. g_free(s);
  221. return ret;
  222. }
  223. QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
  224. {
  225. QEMUFileStdio *s;
  226. if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
  227. fprintf(stderr, "qemu_popen: Argument validity check failed\n");
  228. return NULL;
  229. }
  230. s = g_malloc0(sizeof(QEMUFileStdio));
  231. s->stdio_file = stdio_file;
  232. if(mode[0] == 'r') {
  233. s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
  234. NULL, NULL, NULL);
  235. } else {
  236. s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
  237. NULL, NULL, NULL);
  238. }
  239. return s->file;
  240. }
  241. QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
  242. {
  243. FILE *popen_file;
  244. popen_file = popen(command, mode);
  245. if(popen_file == NULL) {
  246. return NULL;
  247. }
  248. return qemu_popen(popen_file, mode);
  249. }
  250. int qemu_stdio_fd(QEMUFile *f)
  251. {
  252. QEMUFileStdio *p;
  253. int fd;
  254. p = (QEMUFileStdio *)f->opaque;
  255. fd = fileno(p->stdio_file);
  256. return fd;
  257. }
  258. QEMUFile *qemu_fdopen(int fd, const char *mode)
  259. {
  260. QEMUFileStdio *s;
  261. if (mode == NULL ||
  262. (mode[0] != 'r' && mode[0] != 'w') ||
  263. mode[1] != 'b' || mode[2] != 0) {
  264. fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
  265. return NULL;
  266. }
  267. s = g_malloc0(sizeof(QEMUFileStdio));
  268. s->stdio_file = fdopen(fd, mode);
  269. if (!s->stdio_file)
  270. goto fail;
  271. if(mode[0] == 'r') {
  272. s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
  273. NULL, NULL, NULL);
  274. } else {
  275. s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
  276. NULL, NULL, NULL);
  277. }
  278. return s->file;
  279. fail:
  280. g_free(s);
  281. return NULL;
  282. }
  283. QEMUFile *qemu_fopen_socket(int fd)
  284. {
  285. QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
  286. s->fd = fd;
  287. s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
  288. NULL, NULL, NULL);
  289. return s->file;
  290. }
  291. static int file_put_buffer(void *opaque, const uint8_t *buf,
  292. int64_t pos, int size)
  293. {
  294. QEMUFileStdio *s = opaque;
  295. fseek(s->stdio_file, pos, SEEK_SET);
  296. return fwrite(buf, 1, size, s->stdio_file);
  297. }
  298. static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  299. {
  300. QEMUFileStdio *s = opaque;
  301. fseek(s->stdio_file, pos, SEEK_SET);
  302. return fread(buf, 1, size, s->stdio_file);
  303. }
  304. QEMUFile *qemu_fopen(const char *filename, const char *mode)
  305. {
  306. QEMUFileStdio *s;
  307. if (mode == NULL ||
  308. (mode[0] != 'r' && mode[0] != 'w') ||
  309. mode[1] != 'b' || mode[2] != 0) {
  310. fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
  311. return NULL;
  312. }
  313. s = g_malloc0(sizeof(QEMUFileStdio));
  314. s->stdio_file = fopen(filename, mode);
  315. if (!s->stdio_file)
  316. goto fail;
  317. if(mode[0] == 'w') {
  318. s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
  319. NULL, NULL, NULL);
  320. } else {
  321. s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
  322. NULL, NULL, NULL);
  323. }
  324. return s->file;
  325. fail:
  326. g_free(s);
  327. return NULL;
  328. }
  329. static int block_put_buffer(void *opaque, const uint8_t *buf,
  330. int64_t pos, int size)
  331. {
  332. bdrv_save_vmstate(opaque, buf, pos, size);
  333. return size;
  334. }
  335. static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
  336. {
  337. return bdrv_load_vmstate(opaque, buf, pos, size);
  338. }
  339. static int bdrv_fclose(void *opaque)
  340. {
  341. return bdrv_flush(opaque);
  342. }
  343. static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
  344. {
  345. if (is_writable)
  346. return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
  347. NULL, NULL, NULL);
  348. return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
  349. }
  350. QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
  351. QEMUFileGetBufferFunc *get_buffer,
  352. QEMUFileCloseFunc *close,
  353. QEMUFileRateLimit *rate_limit,
  354. QEMUFileSetRateLimit *set_rate_limit,
  355. QEMUFileGetRateLimit *get_rate_limit)
  356. {
  357. QEMUFile *f;
  358. f = g_malloc0(sizeof(QEMUFile));
  359. f->opaque = opaque;
  360. f->put_buffer = put_buffer;
  361. f->get_buffer = get_buffer;
  362. f->close = close;
  363. f->rate_limit = rate_limit;
  364. f->set_rate_limit = set_rate_limit;
  365. f->get_rate_limit = get_rate_limit;
  366. f->is_write = 0;
  367. return f;
  368. }
  369. int qemu_file_get_error(QEMUFile *f)
  370. {
  371. return f->last_error;
  372. }
  373. void qemu_file_set_error(QEMUFile *f, int ret)
  374. {
  375. f->last_error = ret;
  376. }
  377. /** Sets last_error conditionally
  378. *
  379. * Sets last_error only if ret is negative _and_ no error
  380. * was set before.
  381. */
  382. static void qemu_file_set_if_error(QEMUFile *f, int ret)
  383. {
  384. if (ret < 0 && !f->last_error) {
  385. qemu_file_set_error(f, ret);
  386. }
  387. }
  388. /** Flushes QEMUFile buffer
  389. *
  390. * In case of error, last_error is set.
  391. */
  392. void qemu_fflush(QEMUFile *f)
  393. {
  394. if (!f->put_buffer)
  395. return;
  396. if (f->is_write && f->buf_index > 0) {
  397. int len;
  398. len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
  399. if (len > 0)
  400. f->buf_offset += f->buf_index;
  401. else
  402. qemu_file_set_error(f, -EINVAL);
  403. f->buf_index = 0;
  404. }
  405. }
  406. static void qemu_fill_buffer(QEMUFile *f)
  407. {
  408. int len;
  409. int pending;
  410. if (!f->get_buffer)
  411. return;
  412. if (f->is_write)
  413. abort();
  414. pending = f->buf_size - f->buf_index;
  415. if (pending > 0) {
  416. memmove(f->buf, f->buf + f->buf_index, pending);
  417. }
  418. f->buf_index = 0;
  419. f->buf_size = pending;
  420. len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
  421. IO_BUF_SIZE - pending);
  422. if (len > 0) {
  423. f->buf_size += len;
  424. f->buf_offset += len;
  425. } else if (len == 0) {
  426. f->last_error = -EIO;
  427. } else if (len != -EAGAIN)
  428. qemu_file_set_error(f, len);
  429. }
  430. /** Calls close function and set last_error if needed
  431. *
  432. * Internal function. qemu_fflush() must be called before this.
  433. *
  434. * Returns f->close() return value, or 0 if close function is not set.
  435. */
  436. static int qemu_fclose_internal(QEMUFile *f)
  437. {
  438. int ret = 0;
  439. if (f->close) {
  440. ret = f->close(f->opaque);
  441. qemu_file_set_if_error(f, ret);
  442. }
  443. return ret;
  444. }
  445. /** Closes the file
  446. *
  447. * Returns negative error value if any error happened on previous operations or
  448. * while closing the file. Returns 0 or positive number on success.
  449. *
  450. * The meaning of return value on success depends on the specific backend
  451. * being used.
  452. */
  453. int qemu_fclose(QEMUFile *f)
  454. {
  455. int ret;
  456. qemu_fflush(f);
  457. ret = qemu_fclose_internal(f);
  458. /* If any error was spotted before closing, we should report it
  459. * instead of the close() return value.
  460. */
  461. if (f->last_error) {
  462. ret = f->last_error;
  463. }
  464. g_free(f);
  465. return ret;
  466. }
  467. void qemu_file_put_notify(QEMUFile *f)
  468. {
  469. f->put_buffer(f->opaque, NULL, 0, 0);
  470. }
  471. void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
  472. {
  473. int l;
  474. if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
  475. fprintf(stderr,
  476. "Attempted to write to buffer while read buffer is not empty\n");
  477. abort();
  478. }
  479. while (!f->last_error && size > 0) {
  480. l = IO_BUF_SIZE - f->buf_index;
  481. if (l > size)
  482. l = size;
  483. memcpy(f->buf + f->buf_index, buf, l);
  484. f->is_write = 1;
  485. f->buf_index += l;
  486. buf += l;
  487. size -= l;
  488. if (f->buf_index >= IO_BUF_SIZE)
  489. qemu_fflush(f);
  490. }
  491. }
  492. void qemu_put_byte(QEMUFile *f, int v)
  493. {
  494. if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
  495. fprintf(stderr,
  496. "Attempted to write to buffer while read buffer is not empty\n");
  497. abort();
  498. }
  499. f->buf[f->buf_index++] = v;
  500. f->is_write = 1;
  501. if (f->buf_index >= IO_BUF_SIZE)
  502. qemu_fflush(f);
  503. }
  504. static void qemu_file_skip(QEMUFile *f, int size)
  505. {
  506. if (f->buf_index + size <= f->buf_size) {
  507. f->buf_index += size;
  508. }
  509. }
  510. static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
  511. {
  512. int pending;
  513. int index;
  514. if (f->is_write) {
  515. abort();
  516. }
  517. index = f->buf_index + offset;
  518. pending = f->buf_size - index;
  519. if (pending < size) {
  520. qemu_fill_buffer(f);
  521. index = f->buf_index + offset;
  522. pending = f->buf_size - index;
  523. }
  524. if (pending <= 0) {
  525. return 0;
  526. }
  527. if (size > pending) {
  528. size = pending;
  529. }
  530. memcpy(buf, f->buf + index, size);
  531. return size;
  532. }
  533. int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
  534. {
  535. int pending = size;
  536. int done = 0;
  537. while (pending > 0) {
  538. int res;
  539. res = qemu_peek_buffer(f, buf, pending, 0);
  540. if (res == 0) {
  541. return done;
  542. }
  543. qemu_file_skip(f, res);
  544. buf += res;
  545. pending -= res;
  546. done += res;
  547. }
  548. return done;
  549. }
  550. static int qemu_peek_byte(QEMUFile *f, int offset)
  551. {
  552. int index = f->buf_index + offset;
  553. if (f->is_write) {
  554. abort();
  555. }
  556. if (index >= f->buf_size) {
  557. qemu_fill_buffer(f);
  558. index = f->buf_index + offset;
  559. if (index >= f->buf_size) {
  560. return 0;
  561. }
  562. }
  563. return f->buf[index];
  564. }
  565. int qemu_get_byte(QEMUFile *f)
  566. {
  567. int result;
  568. result = qemu_peek_byte(f, 0);
  569. qemu_file_skip(f, 1);
  570. return result;
  571. }
  572. int64_t qemu_ftell(QEMUFile *f)
  573. {
  574. return f->buf_offset - f->buf_size + f->buf_index;
  575. }
  576. int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
  577. {
  578. if (whence == SEEK_SET) {
  579. /* nothing to do */
  580. } else if (whence == SEEK_CUR) {
  581. pos += qemu_ftell(f);
  582. } else {
  583. /* SEEK_END not supported */
  584. return -1;
  585. }
  586. if (f->put_buffer) {
  587. qemu_fflush(f);
  588. f->buf_offset = pos;
  589. } else {
  590. f->buf_offset = pos;
  591. f->buf_index = 0;
  592. f->buf_size = 0;
  593. }
  594. return pos;
  595. }
  596. int qemu_file_rate_limit(QEMUFile *f)
  597. {
  598. if (f->rate_limit)
  599. return f->rate_limit(f->opaque);
  600. return 0;
  601. }
  602. int64_t qemu_file_get_rate_limit(QEMUFile *f)
  603. {
  604. if (f->get_rate_limit)
  605. return f->get_rate_limit(f->opaque);
  606. return 0;
  607. }
  608. int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
  609. {
  610. /* any failed or completed migration keeps its state to allow probing of
  611. * migration data, but has no associated file anymore */
  612. if (f && f->set_rate_limit)
  613. return f->set_rate_limit(f->opaque, new_rate);
  614. return 0;
  615. }
  616. void qemu_put_be16(QEMUFile *f, unsigned int v)
  617. {
  618. qemu_put_byte(f, v >> 8);
  619. qemu_put_byte(f, v);
  620. }
  621. void qemu_put_be32(QEMUFile *f, unsigned int v)
  622. {
  623. qemu_put_byte(f, v >> 24);
  624. qemu_put_byte(f, v >> 16);
  625. qemu_put_byte(f, v >> 8);
  626. qemu_put_byte(f, v);
  627. }
  628. void qemu_put_be64(QEMUFile *f, uint64_t v)
  629. {
  630. qemu_put_be32(f, v >> 32);
  631. qemu_put_be32(f, v);
  632. }
  633. unsigned int qemu_get_be16(QEMUFile *f)
  634. {
  635. unsigned int v;
  636. v = qemu_get_byte(f) << 8;
  637. v |= qemu_get_byte(f);
  638. return v;
  639. }
  640. unsigned int qemu_get_be32(QEMUFile *f)
  641. {
  642. unsigned int v;
  643. v = qemu_get_byte(f) << 24;
  644. v |= qemu_get_byte(f) << 16;
  645. v |= qemu_get_byte(f) << 8;
  646. v |= qemu_get_byte(f);
  647. return v;
  648. }
  649. uint64_t qemu_get_be64(QEMUFile *f)
  650. {
  651. uint64_t v;
  652. v = (uint64_t)qemu_get_be32(f) << 32;
  653. v |= qemu_get_be32(f);
  654. return v;
  655. }
  656. /* timer */
  657. void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
  658. {
  659. uint64_t expire_time;
  660. expire_time = qemu_timer_expire_time_ns(ts);
  661. qemu_put_be64(f, expire_time);
  662. }
  663. void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
  664. {
  665. uint64_t expire_time;
  666. expire_time = qemu_get_be64(f);
  667. if (expire_time != -1) {
  668. qemu_mod_timer_ns(ts, expire_time);
  669. } else {
  670. qemu_del_timer(ts);
  671. }
  672. }
  673. /* bool */
  674. static int get_bool(QEMUFile *f, void *pv, size_t size)
  675. {
  676. bool *v = pv;
  677. *v = qemu_get_byte(f);
  678. return 0;
  679. }
  680. static void put_bool(QEMUFile *f, void *pv, size_t size)
  681. {
  682. bool *v = pv;
  683. qemu_put_byte(f, *v);
  684. }
  685. const VMStateInfo vmstate_info_bool = {
  686. .name = "bool",
  687. .get = get_bool,
  688. .put = put_bool,
  689. };
  690. /* 8 bit int */
  691. static int get_int8(QEMUFile *f, void *pv, size_t size)
  692. {
  693. int8_t *v = pv;
  694. qemu_get_s8s(f, v);
  695. return 0;
  696. }
  697. static void put_int8(QEMUFile *f, void *pv, size_t size)
  698. {
  699. int8_t *v = pv;
  700. qemu_put_s8s(f, v);
  701. }
  702. const VMStateInfo vmstate_info_int8 = {
  703. .name = "int8",
  704. .get = get_int8,
  705. .put = put_int8,
  706. };
  707. /* 16 bit int */
  708. static int get_int16(QEMUFile *f, void *pv, size_t size)
  709. {
  710. int16_t *v = pv;
  711. qemu_get_sbe16s(f, v);
  712. return 0;
  713. }
  714. static void put_int16(QEMUFile *f, void *pv, size_t size)
  715. {
  716. int16_t *v = pv;
  717. qemu_put_sbe16s(f, v);
  718. }
  719. const VMStateInfo vmstate_info_int16 = {
  720. .name = "int16",
  721. .get = get_int16,
  722. .put = put_int16,
  723. };
  724. /* 32 bit int */
  725. static int get_int32(QEMUFile *f, void *pv, size_t size)
  726. {
  727. int32_t *v = pv;
  728. qemu_get_sbe32s(f, v);
  729. return 0;
  730. }
  731. static void put_int32(QEMUFile *f, void *pv, size_t size)
  732. {
  733. int32_t *v = pv;
  734. qemu_put_sbe32s(f, v);
  735. }
  736. const VMStateInfo vmstate_info_int32 = {
  737. .name = "int32",
  738. .get = get_int32,
  739. .put = put_int32,
  740. };
  741. /* 32 bit int. See that the received value is the same than the one
  742. in the field */
  743. static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
  744. {
  745. int32_t *v = pv;
  746. int32_t v2;
  747. qemu_get_sbe32s(f, &v2);
  748. if (*v == v2)
  749. return 0;
  750. return -EINVAL;
  751. }
  752. const VMStateInfo vmstate_info_int32_equal = {
  753. .name = "int32 equal",
  754. .get = get_int32_equal,
  755. .put = put_int32,
  756. };
  757. /* 32 bit int. See that the received value is the less or the same
  758. than the one in the field */
  759. static int get_int32_le(QEMUFile *f, void *pv, size_t size)
  760. {
  761. int32_t *old = pv;
  762. int32_t new;
  763. qemu_get_sbe32s(f, &new);
  764. if (*old <= new)
  765. return 0;
  766. return -EINVAL;
  767. }
  768. const VMStateInfo vmstate_info_int32_le = {
  769. .name = "int32 equal",
  770. .get = get_int32_le,
  771. .put = put_int32,
  772. };
  773. /* 64 bit int */
  774. static int get_int64(QEMUFile *f, void *pv, size_t size)
  775. {
  776. int64_t *v = pv;
  777. qemu_get_sbe64s(f, v);
  778. return 0;
  779. }
  780. static void put_int64(QEMUFile *f, void *pv, size_t size)
  781. {
  782. int64_t *v = pv;
  783. qemu_put_sbe64s(f, v);
  784. }
  785. const VMStateInfo vmstate_info_int64 = {
  786. .name = "int64",
  787. .get = get_int64,
  788. .put = put_int64,
  789. };
  790. /* 8 bit unsigned int */
  791. static int get_uint8(QEMUFile *f, void *pv, size_t size)
  792. {
  793. uint8_t *v = pv;
  794. qemu_get_8s(f, v);
  795. return 0;
  796. }
  797. static void put_uint8(QEMUFile *f, void *pv, size_t size)
  798. {
  799. uint8_t *v = pv;
  800. qemu_put_8s(f, v);
  801. }
  802. const VMStateInfo vmstate_info_uint8 = {
  803. .name = "uint8",
  804. .get = get_uint8,
  805. .put = put_uint8,
  806. };
  807. /* 16 bit unsigned int */
  808. static int get_uint16(QEMUFile *f, void *pv, size_t size)
  809. {
  810. uint16_t *v = pv;
  811. qemu_get_be16s(f, v);
  812. return 0;
  813. }
  814. static void put_uint16(QEMUFile *f, void *pv, size_t size)
  815. {
  816. uint16_t *v = pv;
  817. qemu_put_be16s(f, v);
  818. }
  819. const VMStateInfo vmstate_info_uint16 = {
  820. .name = "uint16",
  821. .get = get_uint16,
  822. .put = put_uint16,
  823. };
  824. /* 32 bit unsigned int */
  825. static int get_uint32(QEMUFile *f, void *pv, size_t size)
  826. {
  827. uint32_t *v = pv;
  828. qemu_get_be32s(f, v);
  829. return 0;
  830. }
  831. static void put_uint32(QEMUFile *f, void *pv, size_t size)
  832. {
  833. uint32_t *v = pv;
  834. qemu_put_be32s(f, v);
  835. }
  836. const VMStateInfo vmstate_info_uint32 = {
  837. .name = "uint32",
  838. .get = get_uint32,
  839. .put = put_uint32,
  840. };
  841. /* 32 bit uint. See that the received value is the same than the one
  842. in the field */
  843. static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
  844. {
  845. uint32_t *v = pv;
  846. uint32_t v2;
  847. qemu_get_be32s(f, &v2);
  848. if (*v == v2) {
  849. return 0;
  850. }
  851. return -EINVAL;
  852. }
  853. const VMStateInfo vmstate_info_uint32_equal = {
  854. .name = "uint32 equal",
  855. .get = get_uint32_equal,
  856. .put = put_uint32,
  857. };
  858. /* 64 bit unsigned int */
  859. static int get_uint64(QEMUFile *f, void *pv, size_t size)
  860. {
  861. uint64_t *v = pv;
  862. qemu_get_be64s(f, v);
  863. return 0;
  864. }
  865. static void put_uint64(QEMUFile *f, void *pv, size_t size)
  866. {
  867. uint64_t *v = pv;
  868. qemu_put_be64s(f, v);
  869. }
  870. const VMStateInfo vmstate_info_uint64 = {
  871. .name = "uint64",
  872. .get = get_uint64,
  873. .put = put_uint64,
  874. };
  875. /* 8 bit int. See that the received value is the same than the one
  876. in the field */
  877. static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
  878. {
  879. uint8_t *v = pv;
  880. uint8_t v2;
  881. qemu_get_8s(f, &v2);
  882. if (*v == v2)
  883. return 0;
  884. return -EINVAL;
  885. }
  886. const VMStateInfo vmstate_info_uint8_equal = {
  887. .name = "uint8 equal",
  888. .get = get_uint8_equal,
  889. .put = put_uint8,
  890. };
  891. /* 16 bit unsigned int int. See that the received value is the same than the one
  892. in the field */
  893. static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
  894. {
  895. uint16_t *v = pv;
  896. uint16_t v2;
  897. qemu_get_be16s(f, &v2);
  898. if (*v == v2)
  899. return 0;
  900. return -EINVAL;
  901. }
  902. const VMStateInfo vmstate_info_uint16_equal = {
  903. .name = "uint16 equal",
  904. .get = get_uint16_equal,
  905. .put = put_uint16,
  906. };
  907. /* timers */
  908. static int get_timer(QEMUFile *f, void *pv, size_t size)
  909. {
  910. QEMUTimer *v = pv;
  911. qemu_get_timer(f, v);
  912. return 0;
  913. }
  914. static void put_timer(QEMUFile *f, void *pv, size_t size)
  915. {
  916. QEMUTimer *v = pv;
  917. qemu_put_timer(f, v);
  918. }
  919. const VMStateInfo vmstate_info_timer = {
  920. .name = "timer",
  921. .get = get_timer,
  922. .put = put_timer,
  923. };
  924. /* uint8_t buffers */
  925. static int get_buffer(QEMUFile *f, void *pv, size_t size)
  926. {
  927. uint8_t *v = pv;
  928. qemu_get_buffer(f, v, size);
  929. return 0;
  930. }
  931. static void put_buffer(QEMUFile *f, void *pv, size_t size)
  932. {
  933. uint8_t *v = pv;
  934. qemu_put_buffer(f, v, size);
  935. }
  936. const VMStateInfo vmstate_info_buffer = {
  937. .name = "buffer",
  938. .get = get_buffer,
  939. .put = put_buffer,
  940. };
  941. /* unused buffers: space that was used for some fields that are
  942. not useful anymore */
  943. static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
  944. {
  945. uint8_t buf[1024];
  946. int block_len;
  947. while (size > 0) {
  948. block_len = MIN(sizeof(buf), size);
  949. size -= block_len;
  950. qemu_get_buffer(f, buf, block_len);
  951. }
  952. return 0;
  953. }
  954. static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
  955. {
  956. static const uint8_t buf[1024];
  957. int block_len;
  958. while (size > 0) {
  959. block_len = MIN(sizeof(buf), size);
  960. size -= block_len;
  961. qemu_put_buffer(f, buf, block_len);
  962. }
  963. }
  964. const VMStateInfo vmstate_info_unused_buffer = {
  965. .name = "unused_buffer",
  966. .get = get_unused_buffer,
  967. .put = put_unused_buffer,
  968. };
  969. typedef struct CompatEntry {
  970. char idstr[256];
  971. int instance_id;
  972. } CompatEntry;
  973. typedef struct SaveStateEntry {
  974. QTAILQ_ENTRY(SaveStateEntry) entry;
  975. char idstr[256];
  976. int instance_id;
  977. int alias_id;
  978. int version_id;
  979. int section_id;
  980. SaveVMHandlers *ops;
  981. const VMStateDescription *vmsd;
  982. void *opaque;
  983. CompatEntry *compat;
  984. int no_migrate;
  985. int is_ram;
  986. } SaveStateEntry;
  987. static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
  988. QTAILQ_HEAD_INITIALIZER(savevm_handlers);
  989. static int global_section_id;
  990. static int calculate_new_instance_id(const char *idstr)
  991. {
  992. SaveStateEntry *se;
  993. int instance_id = 0;
  994. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  995. if (strcmp(idstr, se->idstr) == 0
  996. && instance_id <= se->instance_id) {
  997. instance_id = se->instance_id + 1;
  998. }
  999. }
  1000. return instance_id;
  1001. }
  1002. static int calculate_compat_instance_id(const char *idstr)
  1003. {
  1004. SaveStateEntry *se;
  1005. int instance_id = 0;
  1006. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1007. if (!se->compat)
  1008. continue;
  1009. if (strcmp(idstr, se->compat->idstr) == 0
  1010. && instance_id <= se->compat->instance_id) {
  1011. instance_id = se->compat->instance_id + 1;
  1012. }
  1013. }
  1014. return instance_id;
  1015. }
  1016. /* TODO: Individual devices generally have very little idea about the rest
  1017. of the system, so instance_id should be removed/replaced.
  1018. Meanwhile pass -1 as instance_id if you do not already have a clearly
  1019. distinguishing id for all instances of your device class. */
  1020. int register_savevm_live(DeviceState *dev,
  1021. const char *idstr,
  1022. int instance_id,
  1023. int version_id,
  1024. SaveVMHandlers *ops,
  1025. void *opaque)
  1026. {
  1027. SaveStateEntry *se;
  1028. se = g_malloc0(sizeof(SaveStateEntry));
  1029. se->version_id = version_id;
  1030. se->section_id = global_section_id++;
  1031. se->ops = ops;
  1032. se->opaque = opaque;
  1033. se->vmsd = NULL;
  1034. se->no_migrate = 0;
  1035. /* if this is a live_savem then set is_ram */
  1036. if (ops->save_live_setup != NULL) {
  1037. se->is_ram = 1;
  1038. }
  1039. if (dev) {
  1040. char *id = qdev_get_dev_path(dev);
  1041. if (id) {
  1042. pstrcpy(se->idstr, sizeof(se->idstr), id);
  1043. pstrcat(se->idstr, sizeof(se->idstr), "/");
  1044. g_free(id);
  1045. se->compat = g_malloc0(sizeof(CompatEntry));
  1046. pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
  1047. se->compat->instance_id = instance_id == -1 ?
  1048. calculate_compat_instance_id(idstr) : instance_id;
  1049. instance_id = -1;
  1050. }
  1051. }
  1052. pstrcat(se->idstr, sizeof(se->idstr), idstr);
  1053. if (instance_id == -1) {
  1054. se->instance_id = calculate_new_instance_id(se->idstr);
  1055. } else {
  1056. se->instance_id = instance_id;
  1057. }
  1058. assert(!se->compat || se->instance_id == 0);
  1059. /* add at the end of list */
  1060. QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
  1061. return 0;
  1062. }
  1063. int register_savevm(DeviceState *dev,
  1064. const char *idstr,
  1065. int instance_id,
  1066. int version_id,
  1067. SaveStateHandler *save_state,
  1068. LoadStateHandler *load_state,
  1069. void *opaque)
  1070. {
  1071. SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
  1072. ops->save_state = save_state;
  1073. ops->load_state = load_state;
  1074. return register_savevm_live(dev, idstr, instance_id, version_id,
  1075. ops, opaque);
  1076. }
  1077. void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
  1078. {
  1079. SaveStateEntry *se, *new_se;
  1080. char id[256] = "";
  1081. if (dev) {
  1082. char *path = qdev_get_dev_path(dev);
  1083. if (path) {
  1084. pstrcpy(id, sizeof(id), path);
  1085. pstrcat(id, sizeof(id), "/");
  1086. g_free(path);
  1087. }
  1088. }
  1089. pstrcat(id, sizeof(id), idstr);
  1090. QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
  1091. if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
  1092. QTAILQ_REMOVE(&savevm_handlers, se, entry);
  1093. if (se->compat) {
  1094. g_free(se->compat);
  1095. }
  1096. g_free(se->ops);
  1097. g_free(se);
  1098. }
  1099. }
  1100. }
  1101. int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
  1102. const VMStateDescription *vmsd,
  1103. void *opaque, int alias_id,
  1104. int required_for_version)
  1105. {
  1106. SaveStateEntry *se;
  1107. /* If this triggers, alias support can be dropped for the vmsd. */
  1108. assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
  1109. se = g_malloc0(sizeof(SaveStateEntry));
  1110. se->version_id = vmsd->version_id;
  1111. se->section_id = global_section_id++;
  1112. se->opaque = opaque;
  1113. se->vmsd = vmsd;
  1114. se->alias_id = alias_id;
  1115. se->no_migrate = vmsd->unmigratable;
  1116. if (dev) {
  1117. char *id = qdev_get_dev_path(dev);
  1118. if (id) {
  1119. pstrcpy(se->idstr, sizeof(se->idstr), id);
  1120. pstrcat(se->idstr, sizeof(se->idstr), "/");
  1121. g_free(id);
  1122. se->compat = g_malloc0(sizeof(CompatEntry));
  1123. pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
  1124. se->compat->instance_id = instance_id == -1 ?
  1125. calculate_compat_instance_id(vmsd->name) : instance_id;
  1126. instance_id = -1;
  1127. }
  1128. }
  1129. pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
  1130. if (instance_id == -1) {
  1131. se->instance_id = calculate_new_instance_id(se->idstr);
  1132. } else {
  1133. se->instance_id = instance_id;
  1134. }
  1135. assert(!se->compat || se->instance_id == 0);
  1136. /* add at the end of list */
  1137. QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
  1138. return 0;
  1139. }
  1140. int vmstate_register(DeviceState *dev, int instance_id,
  1141. const VMStateDescription *vmsd, void *opaque)
  1142. {
  1143. return vmstate_register_with_alias_id(dev, instance_id, vmsd,
  1144. opaque, -1, 0);
  1145. }
  1146. void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
  1147. void *opaque)
  1148. {
  1149. SaveStateEntry *se, *new_se;
  1150. QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
  1151. if (se->vmsd == vmsd && se->opaque == opaque) {
  1152. QTAILQ_REMOVE(&savevm_handlers, se, entry);
  1153. if (se->compat) {
  1154. g_free(se->compat);
  1155. }
  1156. g_free(se);
  1157. }
  1158. }
  1159. }
  1160. static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
  1161. void *opaque);
  1162. static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
  1163. void *opaque);
  1164. int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
  1165. void *opaque, int version_id)
  1166. {
  1167. VMStateField *field = vmsd->fields;
  1168. int ret;
  1169. if (version_id > vmsd->version_id) {
  1170. return -EINVAL;
  1171. }
  1172. if (version_id < vmsd->minimum_version_id_old) {
  1173. return -EINVAL;
  1174. }
  1175. if (version_id < vmsd->minimum_version_id) {
  1176. return vmsd->load_state_old(f, opaque, version_id);
  1177. }
  1178. if (vmsd->pre_load) {
  1179. int ret = vmsd->pre_load(opaque);
  1180. if (ret)
  1181. return ret;
  1182. }
  1183. while(field->name) {
  1184. if ((field->field_exists &&
  1185. field->field_exists(opaque, version_id)) ||
  1186. (!field->field_exists &&
  1187. field->version_id <= version_id)) {
  1188. void *base_addr = opaque + field->offset;
  1189. int i, n_elems = 1;
  1190. int size = field->size;
  1191. if (field->flags & VMS_VBUFFER) {
  1192. size = *(int32_t *)(opaque+field->size_offset);
  1193. if (field->flags & VMS_MULTIPLY) {
  1194. size *= field->size;
  1195. }
  1196. }
  1197. if (field->flags & VMS_ARRAY) {
  1198. n_elems = field->num;
  1199. } else if (field->flags & VMS_VARRAY_INT32) {
  1200. n_elems = *(int32_t *)(opaque+field->num_offset);
  1201. } else if (field->flags & VMS_VARRAY_UINT32) {
  1202. n_elems = *(uint32_t *)(opaque+field->num_offset);
  1203. } else if (field->flags & VMS_VARRAY_UINT16) {
  1204. n_elems = *(uint16_t *)(opaque+field->num_offset);
  1205. } else if (field->flags & VMS_VARRAY_UINT8) {
  1206. n_elems = *(uint8_t *)(opaque+field->num_offset);
  1207. }
  1208. if (field->flags & VMS_POINTER) {
  1209. base_addr = *(void **)base_addr + field->start;
  1210. }
  1211. for (i = 0; i < n_elems; i++) {
  1212. void *addr = base_addr + size * i;
  1213. if (field->flags & VMS_ARRAY_OF_POINTER) {
  1214. addr = *(void **)addr;
  1215. }
  1216. if (field->flags & VMS_STRUCT) {
  1217. ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
  1218. } else {
  1219. ret = field->info->get(f, addr, size);
  1220. }
  1221. if (ret < 0) {
  1222. return ret;
  1223. }
  1224. }
  1225. }
  1226. field++;
  1227. }
  1228. ret = vmstate_subsection_load(f, vmsd, opaque);
  1229. if (ret != 0) {
  1230. return ret;
  1231. }
  1232. if (vmsd->post_load) {
  1233. return vmsd->post_load(opaque, version_id);
  1234. }
  1235. return 0;
  1236. }
  1237. void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
  1238. void *opaque)
  1239. {
  1240. VMStateField *field = vmsd->fields;
  1241. if (vmsd->pre_save) {
  1242. vmsd->pre_save(opaque);
  1243. }
  1244. while(field->name) {
  1245. if (!field->field_exists ||
  1246. field->field_exists(opaque, vmsd->version_id)) {
  1247. void *base_addr = opaque + field->offset;
  1248. int i, n_elems = 1;
  1249. int size = field->size;
  1250. if (field->flags & VMS_VBUFFER) {
  1251. size = *(int32_t *)(opaque+field->size_offset);
  1252. if (field->flags & VMS_MULTIPLY) {
  1253. size *= field->size;
  1254. }
  1255. }
  1256. if (field->flags & VMS_ARRAY) {
  1257. n_elems = field->num;
  1258. } else if (field->flags & VMS_VARRAY_INT32) {
  1259. n_elems = *(int32_t *)(opaque+field->num_offset);
  1260. } else if (field->flags & VMS_VARRAY_UINT32) {
  1261. n_elems = *(uint32_t *)(opaque+field->num_offset);
  1262. } else if (field->flags & VMS_VARRAY_UINT16) {
  1263. n_elems = *(uint16_t *)(opaque+field->num_offset);
  1264. } else if (field->flags & VMS_VARRAY_UINT8) {
  1265. n_elems = *(uint8_t *)(opaque+field->num_offset);
  1266. }
  1267. if (field->flags & VMS_POINTER) {
  1268. base_addr = *(void **)base_addr + field->start;
  1269. }
  1270. for (i = 0; i < n_elems; i++) {
  1271. void *addr = base_addr + size * i;
  1272. if (field->flags & VMS_ARRAY_OF_POINTER) {
  1273. addr = *(void **)addr;
  1274. }
  1275. if (field->flags & VMS_STRUCT) {
  1276. vmstate_save_state(f, field->vmsd, addr);
  1277. } else {
  1278. field->info->put(f, addr, size);
  1279. }
  1280. }
  1281. }
  1282. field++;
  1283. }
  1284. vmstate_subsection_save(f, vmsd, opaque);
  1285. }
  1286. static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
  1287. {
  1288. if (!se->vmsd) { /* Old style */
  1289. return se->ops->load_state(f, se->opaque, version_id);
  1290. }
  1291. return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
  1292. }
  1293. static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
  1294. {
  1295. if (!se->vmsd) { /* Old style */
  1296. se->ops->save_state(f, se->opaque);
  1297. return;
  1298. }
  1299. vmstate_save_state(f,se->vmsd, se->opaque);
  1300. }
  1301. #define QEMU_VM_FILE_MAGIC 0x5145564d
  1302. #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
  1303. #define QEMU_VM_FILE_VERSION 0x00000003
  1304. #define QEMU_VM_EOF 0x00
  1305. #define QEMU_VM_SECTION_START 0x01
  1306. #define QEMU_VM_SECTION_PART 0x02
  1307. #define QEMU_VM_SECTION_END 0x03
  1308. #define QEMU_VM_SECTION_FULL 0x04
  1309. #define QEMU_VM_SUBSECTION 0x05
  1310. bool qemu_savevm_state_blocked(Error **errp)
  1311. {
  1312. SaveStateEntry *se;
  1313. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1314. if (se->no_migrate) {
  1315. error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
  1316. return true;
  1317. }
  1318. }
  1319. return false;
  1320. }
  1321. int qemu_savevm_state_begin(QEMUFile *f,
  1322. const MigrationParams *params)
  1323. {
  1324. SaveStateEntry *se;
  1325. int ret;
  1326. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1327. if (!se->ops || !se->ops->set_params) {
  1328. continue;
  1329. }
  1330. se->ops->set_params(params, se->opaque);
  1331. }
  1332. qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  1333. qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  1334. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1335. int len;
  1336. if (!se->ops || !se->ops->save_live_setup) {
  1337. continue;
  1338. }
  1339. if (se->ops && se->ops->is_active) {
  1340. if (!se->ops->is_active(se->opaque)) {
  1341. continue;
  1342. }
  1343. }
  1344. /* Section type */
  1345. qemu_put_byte(f, QEMU_VM_SECTION_START);
  1346. qemu_put_be32(f, se->section_id);
  1347. /* ID string */
  1348. len = strlen(se->idstr);
  1349. qemu_put_byte(f, len);
  1350. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  1351. qemu_put_be32(f, se->instance_id);
  1352. qemu_put_be32(f, se->version_id);
  1353. ret = se->ops->save_live_setup(f, se->opaque);
  1354. if (ret < 0) {
  1355. qemu_savevm_state_cancel(f);
  1356. return ret;
  1357. }
  1358. }
  1359. ret = qemu_file_get_error(f);
  1360. if (ret != 0) {
  1361. qemu_savevm_state_cancel(f);
  1362. }
  1363. return ret;
  1364. }
  1365. /*
  1366. * this function has three return values:
  1367. * negative: there was one error, and we have -errno.
  1368. * 0 : We haven't finished, caller have to go again
  1369. * 1 : We have finished, we can go to complete phase
  1370. */
  1371. int qemu_savevm_state_iterate(QEMUFile *f)
  1372. {
  1373. SaveStateEntry *se;
  1374. int ret = 1;
  1375. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1376. if (!se->ops || !se->ops->save_live_iterate) {
  1377. continue;
  1378. }
  1379. if (se->ops && se->ops->is_active) {
  1380. if (!se->ops->is_active(se->opaque)) {
  1381. continue;
  1382. }
  1383. }
  1384. if (qemu_file_rate_limit(f)) {
  1385. return 0;
  1386. }
  1387. trace_savevm_section_start();
  1388. /* Section type */
  1389. qemu_put_byte(f, QEMU_VM_SECTION_PART);
  1390. qemu_put_be32(f, se->section_id);
  1391. ret = se->ops->save_live_iterate(f, se->opaque);
  1392. trace_savevm_section_end(se->section_id);
  1393. if (ret <= 0) {
  1394. /* Do not proceed to the next vmstate before this one reported
  1395. completion of the current stage. This serializes the migration
  1396. and reduces the probability that a faster changing state is
  1397. synchronized over and over again. */
  1398. break;
  1399. }
  1400. }
  1401. if (ret != 0) {
  1402. return ret;
  1403. }
  1404. ret = qemu_file_get_error(f);
  1405. if (ret != 0) {
  1406. qemu_savevm_state_cancel(f);
  1407. }
  1408. return ret;
  1409. }
  1410. int qemu_savevm_state_complete(QEMUFile *f)
  1411. {
  1412. SaveStateEntry *se;
  1413. int ret;
  1414. cpu_synchronize_all_states();
  1415. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1416. if (!se->ops || !se->ops->save_live_complete) {
  1417. continue;
  1418. }
  1419. if (se->ops && se->ops->is_active) {
  1420. if (!se->ops->is_active(se->opaque)) {
  1421. continue;
  1422. }
  1423. }
  1424. trace_savevm_section_start();
  1425. /* Section type */
  1426. qemu_put_byte(f, QEMU_VM_SECTION_END);
  1427. qemu_put_be32(f, se->section_id);
  1428. ret = se->ops->save_live_complete(f, se->opaque);
  1429. trace_savevm_section_end(se->section_id);
  1430. if (ret < 0) {
  1431. return ret;
  1432. }
  1433. }
  1434. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1435. int len;
  1436. if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
  1437. continue;
  1438. }
  1439. trace_savevm_section_start();
  1440. /* Section type */
  1441. qemu_put_byte(f, QEMU_VM_SECTION_FULL);
  1442. qemu_put_be32(f, se->section_id);
  1443. /* ID string */
  1444. len = strlen(se->idstr);
  1445. qemu_put_byte(f, len);
  1446. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  1447. qemu_put_be32(f, se->instance_id);
  1448. qemu_put_be32(f, se->version_id);
  1449. vmstate_save(f, se);
  1450. trace_savevm_section_end(se->section_id);
  1451. }
  1452. qemu_put_byte(f, QEMU_VM_EOF);
  1453. return qemu_file_get_error(f);
  1454. }
  1455. void qemu_savevm_state_cancel(QEMUFile *f)
  1456. {
  1457. SaveStateEntry *se;
  1458. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1459. if (se->ops && se->ops->cancel) {
  1460. se->ops->cancel(se->opaque);
  1461. }
  1462. }
  1463. }
  1464. static int qemu_savevm_state(QEMUFile *f)
  1465. {
  1466. int ret;
  1467. MigrationParams params = {
  1468. .blk = 0,
  1469. .shared = 0
  1470. };
  1471. if (qemu_savevm_state_blocked(NULL)) {
  1472. ret = -EINVAL;
  1473. goto out;
  1474. }
  1475. ret = qemu_savevm_state_begin(f, &params);
  1476. if (ret < 0)
  1477. goto out;
  1478. do {
  1479. ret = qemu_savevm_state_iterate(f);
  1480. if (ret < 0)
  1481. goto out;
  1482. } while (ret == 0);
  1483. ret = qemu_savevm_state_complete(f);
  1484. out:
  1485. if (ret == 0) {
  1486. ret = qemu_file_get_error(f);
  1487. }
  1488. return ret;
  1489. }
  1490. static int qemu_save_device_state(QEMUFile *f)
  1491. {
  1492. SaveStateEntry *se;
  1493. qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
  1494. qemu_put_be32(f, QEMU_VM_FILE_VERSION);
  1495. cpu_synchronize_all_states();
  1496. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1497. int len;
  1498. if (se->is_ram) {
  1499. continue;
  1500. }
  1501. if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
  1502. continue;
  1503. }
  1504. /* Section type */
  1505. qemu_put_byte(f, QEMU_VM_SECTION_FULL);
  1506. qemu_put_be32(f, se->section_id);
  1507. /* ID string */
  1508. len = strlen(se->idstr);
  1509. qemu_put_byte(f, len);
  1510. qemu_put_buffer(f, (uint8_t *)se->idstr, len);
  1511. qemu_put_be32(f, se->instance_id);
  1512. qemu_put_be32(f, se->version_id);
  1513. vmstate_save(f, se);
  1514. }
  1515. qemu_put_byte(f, QEMU_VM_EOF);
  1516. return qemu_file_get_error(f);
  1517. }
  1518. static SaveStateEntry *find_se(const char *idstr, int instance_id)
  1519. {
  1520. SaveStateEntry *se;
  1521. QTAILQ_FOREACH(se, &savevm_handlers, entry) {
  1522. if (!strcmp(se->idstr, idstr) &&
  1523. (instance_id == se->instance_id ||
  1524. instance_id == se->alias_id))
  1525. return se;
  1526. /* Migrating from an older version? */
  1527. if (strstr(se->idstr, idstr) && se->compat) {
  1528. if (!strcmp(se->compat->idstr, idstr) &&
  1529. (instance_id == se->compat->instance_id ||
  1530. instance_id == se->alias_id))
  1531. return se;
  1532. }
  1533. }
  1534. return NULL;
  1535. }
  1536. static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
  1537. {
  1538. while(sub && sub->needed) {
  1539. if (strcmp(idstr, sub->vmsd->name) == 0) {
  1540. return sub->vmsd;
  1541. }
  1542. sub++;
  1543. }
  1544. return NULL;
  1545. }
  1546. static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
  1547. void *opaque)
  1548. {
  1549. while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
  1550. char idstr[256];
  1551. int ret;
  1552. uint8_t version_id, len, size;
  1553. const VMStateDescription *sub_vmsd;
  1554. len = qemu_peek_byte(f, 1);
  1555. if (len < strlen(vmsd->name) + 1) {
  1556. /* subsection name has be be "section_name/a" */
  1557. return 0;
  1558. }
  1559. size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
  1560. if (size != len) {
  1561. return 0;
  1562. }
  1563. idstr[size] = 0;
  1564. if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
  1565. /* it don't have a valid subsection name */
  1566. return 0;
  1567. }
  1568. sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
  1569. if (sub_vmsd == NULL) {
  1570. return -ENOENT;
  1571. }
  1572. qemu_file_skip(f, 1); /* subsection */
  1573. qemu_file_skip(f, 1); /* len */
  1574. qemu_file_skip(f, len); /* idstr */
  1575. version_id = qemu_get_be32(f);
  1576. ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
  1577. if (ret) {
  1578. return ret;
  1579. }
  1580. }
  1581. return 0;
  1582. }
  1583. static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
  1584. void *opaque)
  1585. {
  1586. const VMStateSubsection *sub = vmsd->subsections;
  1587. while (sub && sub->needed) {
  1588. if (sub->needed(opaque)) {
  1589. const VMStateDescription *vmsd = sub->vmsd;
  1590. uint8_t len;
  1591. qemu_put_byte(f, QEMU_VM_SUBSECTION);
  1592. len = strlen(vmsd->name);
  1593. qemu_put_byte(f, len);
  1594. qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
  1595. qemu_put_be32(f, vmsd->version_id);
  1596. vmstate_save_state(f, vmsd, opaque);
  1597. }
  1598. sub++;
  1599. }
  1600. }
  1601. typedef struct LoadStateEntry {
  1602. QLIST_ENTRY(LoadStateEntry) entry;
  1603. SaveStateEntry *se;
  1604. int section_id;
  1605. int version_id;
  1606. } LoadStateEntry;
  1607. int qemu_loadvm_state(QEMUFile *f)
  1608. {
  1609. QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
  1610. QLIST_HEAD_INITIALIZER(loadvm_handlers);
  1611. LoadStateEntry *le, *new_le;
  1612. uint8_t section_type;
  1613. unsigned int v;
  1614. int ret;
  1615. if (qemu_savevm_state_blocked(NULL)) {
  1616. return -EINVAL;
  1617. }
  1618. v = qemu_get_be32(f);
  1619. if (v != QEMU_VM_FILE_MAGIC)
  1620. return -EINVAL;
  1621. v = qemu_get_be32(f);
  1622. if (v == QEMU_VM_FILE_VERSION_COMPAT) {
  1623. fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
  1624. return -ENOTSUP;
  1625. }
  1626. if (v != QEMU_VM_FILE_VERSION)
  1627. return -ENOTSUP;
  1628. while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
  1629. uint32_t instance_id, version_id, section_id;
  1630. SaveStateEntry *se;
  1631. char idstr[257];
  1632. int len;
  1633. switch (section_type) {
  1634. case QEMU_VM_SECTION_START:
  1635. case QEMU_VM_SECTION_FULL:
  1636. /* Read section start */
  1637. section_id = qemu_get_be32(f);
  1638. len = qemu_get_byte(f);
  1639. qemu_get_buffer(f, (uint8_t *)idstr, len);
  1640. idstr[len] = 0;
  1641. instance_id = qemu_get_be32(f);
  1642. version_id = qemu_get_be32(f);
  1643. /* Find savevm section */
  1644. se = find_se(idstr, instance_id);
  1645. if (se == NULL) {
  1646. fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
  1647. ret = -EINVAL;
  1648. goto out;
  1649. }
  1650. /* Validate version */
  1651. if (version_id > se->version_id) {
  1652. fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
  1653. version_id, idstr, se->version_id);
  1654. ret = -EINVAL;
  1655. goto out;
  1656. }
  1657. /* Add entry */
  1658. le = g_malloc0(sizeof(*le));
  1659. le->se = se;
  1660. le->section_id = section_id;
  1661. le->version_id = version_id;
  1662. QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
  1663. ret = vmstate_load(f, le->se, le->version_id);
  1664. if (ret < 0) {
  1665. fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
  1666. instance_id, idstr);
  1667. goto out;
  1668. }
  1669. break;
  1670. case QEMU_VM_SECTION_PART:
  1671. case QEMU_VM_SECTION_END:
  1672. section_id = qemu_get_be32(f);
  1673. QLIST_FOREACH(le, &loadvm_handlers, entry) {
  1674. if (le->section_id == section_id) {
  1675. break;
  1676. }
  1677. }
  1678. if (le == NULL) {
  1679. fprintf(stderr, "Unknown savevm section %d\n", section_id);
  1680. ret = -EINVAL;
  1681. goto out;
  1682. }
  1683. ret = vmstate_load(f, le->se, le->version_id);
  1684. if (ret < 0) {
  1685. fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
  1686. section_id);
  1687. goto out;
  1688. }
  1689. break;
  1690. default:
  1691. fprintf(stderr, "Unknown savevm section type %d\n", section_type);
  1692. ret = -EINVAL;
  1693. goto out;
  1694. }
  1695. }
  1696. cpu_synchronize_all_post_init();
  1697. ret = 0;
  1698. out:
  1699. QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
  1700. QLIST_REMOVE(le, entry);
  1701. g_free(le);
  1702. }
  1703. if (ret == 0) {
  1704. ret = qemu_file_get_error(f);
  1705. }
  1706. return ret;
  1707. }
  1708. static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
  1709. const char *name)
  1710. {
  1711. QEMUSnapshotInfo *sn_tab, *sn;
  1712. int nb_sns, i, ret;
  1713. ret = -ENOENT;
  1714. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  1715. if (nb_sns < 0)
  1716. return ret;
  1717. for(i = 0; i < nb_sns; i++) {
  1718. sn = &sn_tab[i];
  1719. if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
  1720. *sn_info = *sn;
  1721. ret = 0;
  1722. break;
  1723. }
  1724. }
  1725. g_free(sn_tab);
  1726. return ret;
  1727. }
  1728. /*
  1729. * Deletes snapshots of a given name in all opened images.
  1730. */
  1731. static int del_existing_snapshots(Monitor *mon, const char *name)
  1732. {
  1733. BlockDriverState *bs;
  1734. QEMUSnapshotInfo sn1, *snapshot = &sn1;
  1735. int ret;
  1736. bs = NULL;
  1737. while ((bs = bdrv_next(bs))) {
  1738. if (bdrv_can_snapshot(bs) &&
  1739. bdrv_snapshot_find(bs, snapshot, name) >= 0)
  1740. {
  1741. ret = bdrv_snapshot_delete(bs, name);
  1742. if (ret < 0) {
  1743. monitor_printf(mon,
  1744. "Error while deleting snapshot on '%s'\n",
  1745. bdrv_get_device_name(bs));
  1746. return -1;
  1747. }
  1748. }
  1749. }
  1750. return 0;
  1751. }
  1752. void do_savevm(Monitor *mon, const QDict *qdict)
  1753. {
  1754. BlockDriverState *bs, *bs1;
  1755. QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
  1756. int ret;
  1757. QEMUFile *f;
  1758. int saved_vm_running;
  1759. uint64_t vm_state_size;
  1760. #ifdef _WIN32
  1761. struct _timeb tb;
  1762. struct tm *ptm;
  1763. #else
  1764. struct timeval tv;
  1765. struct tm tm;
  1766. #endif
  1767. const char *name = qdict_get_try_str(qdict, "name");
  1768. /* Verify if there is a device that doesn't support snapshots and is writable */
  1769. bs = NULL;
  1770. while ((bs = bdrv_next(bs))) {
  1771. if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
  1772. continue;
  1773. }
  1774. if (!bdrv_can_snapshot(bs)) {
  1775. monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
  1776. bdrv_get_device_name(bs));
  1777. return;
  1778. }
  1779. }
  1780. bs = bdrv_snapshots();
  1781. if (!bs) {
  1782. monitor_printf(mon, "No block device can accept snapshots\n");
  1783. return;
  1784. }
  1785. saved_vm_running = runstate_is_running();
  1786. vm_stop(RUN_STATE_SAVE_VM);
  1787. memset(sn, 0, sizeof(*sn));
  1788. /* fill auxiliary fields */
  1789. #ifdef _WIN32
  1790. _ftime(&tb);
  1791. sn->date_sec = tb.time;
  1792. sn->date_nsec = tb.millitm * 1000000;
  1793. #else
  1794. gettimeofday(&tv, NULL);
  1795. sn->date_sec = tv.tv_sec;
  1796. sn->date_nsec = tv.tv_usec * 1000;
  1797. #endif
  1798. sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
  1799. if (name) {
  1800. ret = bdrv_snapshot_find(bs, old_sn, name);
  1801. if (ret >= 0) {
  1802. pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
  1803. pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
  1804. } else {
  1805. pstrcpy(sn->name, sizeof(sn->name), name);
  1806. }
  1807. } else {
  1808. #ifdef _WIN32
  1809. time_t t = tb.time;
  1810. ptm = localtime(&t);
  1811. strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
  1812. #else
  1813. /* cast below needed for OpenBSD where tv_sec is still 'long' */
  1814. localtime_r((const time_t *)&tv.tv_sec, &tm);
  1815. strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
  1816. #endif
  1817. }
  1818. /* Delete old snapshots of the same name */
  1819. if (name && del_existing_snapshots(mon, name) < 0) {
  1820. goto the_end;
  1821. }
  1822. /* save the VM state */
  1823. f = qemu_fopen_bdrv(bs, 1);
  1824. if (!f) {
  1825. monitor_printf(mon, "Could not open VM state file\n");
  1826. goto the_end;
  1827. }
  1828. ret = qemu_savevm_state(f);
  1829. vm_state_size = qemu_ftell(f);
  1830. qemu_fclose(f);
  1831. if (ret < 0) {
  1832. monitor_printf(mon, "Error %d while writing VM\n", ret);
  1833. goto the_end;
  1834. }
  1835. /* create the snapshots */
  1836. bs1 = NULL;
  1837. while ((bs1 = bdrv_next(bs1))) {
  1838. if (bdrv_can_snapshot(bs1)) {
  1839. /* Write VM state size only to the image that contains the state */
  1840. sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
  1841. ret = bdrv_snapshot_create(bs1, sn);
  1842. if (ret < 0) {
  1843. monitor_printf(mon, "Error while creating snapshot on '%s'\n",
  1844. bdrv_get_device_name(bs1));
  1845. }
  1846. }
  1847. }
  1848. the_end:
  1849. if (saved_vm_running)
  1850. vm_start();
  1851. }
  1852. void qmp_xen_save_devices_state(const char *filename, Error **errp)
  1853. {
  1854. QEMUFile *f;
  1855. int saved_vm_running;
  1856. int ret;
  1857. saved_vm_running = runstate_is_running();
  1858. vm_stop(RUN_STATE_SAVE_VM);
  1859. f = qemu_fopen(filename, "wb");
  1860. if (!f) {
  1861. error_set(errp, QERR_OPEN_FILE_FAILED, filename);
  1862. goto the_end;
  1863. }
  1864. ret = qemu_save_device_state(f);
  1865. qemu_fclose(f);
  1866. if (ret < 0) {
  1867. error_set(errp, QERR_IO_ERROR);
  1868. }
  1869. the_end:
  1870. if (saved_vm_running)
  1871. vm_start();
  1872. return;
  1873. }
  1874. int load_vmstate(const char *name)
  1875. {
  1876. BlockDriverState *bs, *bs_vm_state;
  1877. QEMUSnapshotInfo sn;
  1878. QEMUFile *f;
  1879. int ret;
  1880. bs_vm_state = bdrv_snapshots();
  1881. if (!bs_vm_state) {
  1882. error_report("No block device supports snapshots");
  1883. return -ENOTSUP;
  1884. }
  1885. /* Don't even try to load empty VM states */
  1886. ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
  1887. if (ret < 0) {
  1888. return ret;
  1889. } else if (sn.vm_state_size == 0) {
  1890. error_report("This is a disk-only snapshot. Revert to it offline "
  1891. "using qemu-img.");
  1892. return -EINVAL;
  1893. }
  1894. /* Verify if there is any device that doesn't support snapshots and is
  1895. writable and check if the requested snapshot is available too. */
  1896. bs = NULL;
  1897. while ((bs = bdrv_next(bs))) {
  1898. if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
  1899. continue;
  1900. }
  1901. if (!bdrv_can_snapshot(bs)) {
  1902. error_report("Device '%s' is writable but does not support snapshots.",
  1903. bdrv_get_device_name(bs));
  1904. return -ENOTSUP;
  1905. }
  1906. ret = bdrv_snapshot_find(bs, &sn, name);
  1907. if (ret < 0) {
  1908. error_report("Device '%s' does not have the requested snapshot '%s'",
  1909. bdrv_get_device_name(bs), name);
  1910. return ret;
  1911. }
  1912. }
  1913. /* Flush all IO requests so they don't interfere with the new state. */
  1914. bdrv_drain_all();
  1915. bs = NULL;
  1916. while ((bs = bdrv_next(bs))) {
  1917. if (bdrv_can_snapshot(bs)) {
  1918. ret = bdrv_snapshot_goto(bs, name);
  1919. if (ret < 0) {
  1920. error_report("Error %d while activating snapshot '%s' on '%s'",
  1921. ret, name, bdrv_get_device_name(bs));
  1922. return ret;
  1923. }
  1924. }
  1925. }
  1926. /* restore the VM state */
  1927. f = qemu_fopen_bdrv(bs_vm_state, 0);
  1928. if (!f) {
  1929. error_report("Could not open VM state file");
  1930. return -EINVAL;
  1931. }
  1932. qemu_system_reset(VMRESET_SILENT);
  1933. ret = qemu_loadvm_state(f);
  1934. qemu_fclose(f);
  1935. if (ret < 0) {
  1936. error_report("Error %d while loading VM state", ret);
  1937. return ret;
  1938. }
  1939. return 0;
  1940. }
  1941. void do_delvm(Monitor *mon, const QDict *qdict)
  1942. {
  1943. BlockDriverState *bs, *bs1;
  1944. int ret;
  1945. const char *name = qdict_get_str(qdict, "name");
  1946. bs = bdrv_snapshots();
  1947. if (!bs) {
  1948. monitor_printf(mon, "No block device supports snapshots\n");
  1949. return;
  1950. }
  1951. bs1 = NULL;
  1952. while ((bs1 = bdrv_next(bs1))) {
  1953. if (bdrv_can_snapshot(bs1)) {
  1954. ret = bdrv_snapshot_delete(bs1, name);
  1955. if (ret < 0) {
  1956. if (ret == -ENOTSUP)
  1957. monitor_printf(mon,
  1958. "Snapshots not supported on device '%s'\n",
  1959. bdrv_get_device_name(bs1));
  1960. else
  1961. monitor_printf(mon, "Error %d while deleting snapshot on "
  1962. "'%s'\n", ret, bdrv_get_device_name(bs1));
  1963. }
  1964. }
  1965. }
  1966. }
  1967. void do_info_snapshots(Monitor *mon)
  1968. {
  1969. BlockDriverState *bs, *bs1;
  1970. QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
  1971. int nb_sns, i, ret, available;
  1972. int total;
  1973. int *available_snapshots;
  1974. char buf[256];
  1975. bs = bdrv_snapshots();
  1976. if (!bs) {
  1977. monitor_printf(mon, "No available block device supports snapshots\n");
  1978. return;
  1979. }
  1980. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  1981. if (nb_sns < 0) {
  1982. monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
  1983. return;
  1984. }
  1985. if (nb_sns == 0) {
  1986. monitor_printf(mon, "There is no snapshot available.\n");
  1987. return;
  1988. }
  1989. available_snapshots = g_malloc0(sizeof(int) * nb_sns);
  1990. total = 0;
  1991. for (i = 0; i < nb_sns; i++) {
  1992. sn = &sn_tab[i];
  1993. available = 1;
  1994. bs1 = NULL;
  1995. while ((bs1 = bdrv_next(bs1))) {
  1996. if (bdrv_can_snapshot(bs1) && bs1 != bs) {
  1997. ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
  1998. if (ret < 0) {
  1999. available = 0;
  2000. break;
  2001. }
  2002. }
  2003. }
  2004. if (available) {
  2005. available_snapshots[total] = i;
  2006. total++;
  2007. }
  2008. }
  2009. if (total > 0) {
  2010. monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
  2011. for (i = 0; i < total; i++) {
  2012. sn = &sn_tab[available_snapshots[i]];
  2013. monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
  2014. }
  2015. } else {
  2016. monitor_printf(mon, "There is no suitable snapshot available\n");
  2017. }
  2018. g_free(sn_tab);
  2019. g_free(available_snapshots);
  2020. }
  2021. void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
  2022. {
  2023. qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
  2024. memory_region_name(mr), dev);
  2025. }
  2026. void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
  2027. {
  2028. /* Nothing do to while the implementation is in RAMBlock */
  2029. }
  2030. void vmstate_register_ram_global(MemoryRegion *mr)
  2031. {
  2032. vmstate_register_ram(mr, NULL);
  2033. }
  2034. /*
  2035. page = zrun nzrun
  2036. | zrun nzrun page
  2037. zrun = length
  2038. nzrun = length byte...
  2039. length = uleb128 encoded integer
  2040. */
  2041. int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
  2042. uint8_t *dst, int dlen)
  2043. {
  2044. uint32_t zrun_len = 0, nzrun_len = 0;
  2045. int d = 0, i = 0;
  2046. long res, xor;
  2047. uint8_t *nzrun_start = NULL;
  2048. g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
  2049. sizeof(long)));
  2050. while (i < slen) {
  2051. /* overflow */
  2052. if (d + 2 > dlen) {
  2053. return -1;
  2054. }
  2055. /* not aligned to sizeof(long) */
  2056. res = (slen - i) % sizeof(long);
  2057. while (res && old_buf[i] == new_buf[i]) {
  2058. zrun_len++;
  2059. i++;
  2060. res--;
  2061. }
  2062. /* word at a time for speed */
  2063. if (!res) {
  2064. while (i < slen &&
  2065. (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
  2066. i += sizeof(long);
  2067. zrun_len += sizeof(long);
  2068. }
  2069. /* go over the rest */
  2070. while (i < slen && old_buf[i] == new_buf[i]) {
  2071. zrun_len++;
  2072. i++;
  2073. }
  2074. }
  2075. /* buffer unchanged */
  2076. if (zrun_len == slen) {
  2077. return 0;
  2078. }
  2079. /* skip last zero run */
  2080. if (i == slen) {
  2081. return d;
  2082. }
  2083. d += uleb128_encode_small(dst + d, zrun_len);
  2084. zrun_len = 0;
  2085. nzrun_start = new_buf + i;
  2086. /* overflow */
  2087. if (d + 2 > dlen) {
  2088. return -1;
  2089. }
  2090. /* not aligned to sizeof(long) */
  2091. res = (slen - i) % sizeof(long);
  2092. while (res && old_buf[i] != new_buf[i]) {
  2093. i++;
  2094. nzrun_len++;
  2095. res--;
  2096. }
  2097. /* word at a time for speed, use of 32-bit long okay */
  2098. if (!res) {
  2099. /* truncation to 32-bit long okay */
  2100. long mask = (long)0x0101010101010101ULL;
  2101. while (i < slen) {
  2102. xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
  2103. if ((xor - mask) & ~xor & (mask << 7)) {
  2104. /* found the end of an nzrun within the current long */
  2105. while (old_buf[i] != new_buf[i]) {
  2106. nzrun_len++;
  2107. i++;
  2108. }
  2109. break;
  2110. } else {
  2111. i += sizeof(long);
  2112. nzrun_len += sizeof(long);
  2113. }
  2114. }
  2115. }
  2116. d += uleb128_encode_small(dst + d, nzrun_len);
  2117. /* overflow */
  2118. if (d + nzrun_len > dlen) {
  2119. return -1;
  2120. }
  2121. memcpy(dst + d, nzrun_start, nzrun_len);
  2122. d += nzrun_len;
  2123. nzrun_len = 0;
  2124. }
  2125. return d;
  2126. }
  2127. int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
  2128. {
  2129. int i = 0, d = 0;
  2130. int ret;
  2131. uint32_t count = 0;
  2132. while (i < slen) {
  2133. /* zrun */
  2134. if ((slen - i) < 2) {
  2135. return -1;
  2136. }
  2137. ret = uleb128_decode_small(src + i, &count);
  2138. if (ret < 0 || (i && !count)) {
  2139. return -1;
  2140. }
  2141. i += ret;
  2142. d += count;
  2143. /* overflow */
  2144. if (d > dlen) {
  2145. return -1;
  2146. }
  2147. /* nzrun */
  2148. if ((slen - i) < 2) {
  2149. return -1;
  2150. }
  2151. ret = uleb128_decode_small(src + i, &count);
  2152. if (ret < 0 || !count) {
  2153. return -1;
  2154. }
  2155. i += ret;
  2156. /* overflow */
  2157. if (d + count > dlen || i + count > slen) {
  2158. return -1;
  2159. }
  2160. memcpy(dst + d, src + i, count);
  2161. d += count;
  2162. i += count;
  2163. }
  2164. return d;
  2165. }