arch_init.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 <stdint.h>
  25. #include <stdarg.h>
  26. #include <stdlib.h>
  27. #ifndef _WIN32
  28. #include <sys/types.h>
  29. #include <sys/mman.h>
  30. #endif
  31. #include "config.h"
  32. #include "monitor/monitor.h"
  33. #include "sysemu/sysemu.h"
  34. #include "qemu/bitops.h"
  35. #include "qemu/bitmap.h"
  36. #include "sysemu/arch_init.h"
  37. #include "audio/audio.h"
  38. #include "hw/pc.h"
  39. #include "hw/pci/pci.h"
  40. #include "hw/audiodev.h"
  41. #include "sysemu/kvm.h"
  42. #include "migration/migration.h"
  43. #include "exec/gdbstub.h"
  44. #include "hw/smbios.h"
  45. #include "exec/address-spaces.h"
  46. #include "hw/pcspk.h"
  47. #include "migration/page_cache.h"
  48. #include "qemu/config-file.h"
  49. #include "qmp-commands.h"
  50. #include "trace.h"
  51. #include "exec/cpu-all.h"
  52. #ifdef DEBUG_ARCH_INIT
  53. #define DPRINTF(fmt, ...) \
  54. do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
  55. #else
  56. #define DPRINTF(fmt, ...) \
  57. do { } while (0)
  58. #endif
  59. #ifdef TARGET_SPARC
  60. int graphic_width = 1024;
  61. int graphic_height = 768;
  62. int graphic_depth = 8;
  63. #else
  64. int graphic_width = 800;
  65. int graphic_height = 600;
  66. int graphic_depth = 15;
  67. #endif
  68. #if defined(TARGET_ALPHA)
  69. #define QEMU_ARCH QEMU_ARCH_ALPHA
  70. #elif defined(TARGET_ARM)
  71. #define QEMU_ARCH QEMU_ARCH_ARM
  72. #elif defined(TARGET_CRIS)
  73. #define QEMU_ARCH QEMU_ARCH_CRIS
  74. #elif defined(TARGET_I386)
  75. #define QEMU_ARCH QEMU_ARCH_I386
  76. #elif defined(TARGET_M68K)
  77. #define QEMU_ARCH QEMU_ARCH_M68K
  78. #elif defined(TARGET_LM32)
  79. #define QEMU_ARCH QEMU_ARCH_LM32
  80. #elif defined(TARGET_MICROBLAZE)
  81. #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
  82. #elif defined(TARGET_MIPS)
  83. #define QEMU_ARCH QEMU_ARCH_MIPS
  84. #elif defined(TARGET_OPENRISC)
  85. #define QEMU_ARCH QEMU_ARCH_OPENRISC
  86. #elif defined(TARGET_PPC)
  87. #define QEMU_ARCH QEMU_ARCH_PPC
  88. #elif defined(TARGET_S390X)
  89. #define QEMU_ARCH QEMU_ARCH_S390X
  90. #elif defined(TARGET_SH4)
  91. #define QEMU_ARCH QEMU_ARCH_SH4
  92. #elif defined(TARGET_SPARC)
  93. #define QEMU_ARCH QEMU_ARCH_SPARC
  94. #elif defined(TARGET_XTENSA)
  95. #define QEMU_ARCH QEMU_ARCH_XTENSA
  96. #elif defined(TARGET_UNICORE32)
  97. #define QEMU_ARCH QEMU_ARCH_UNICORE32
  98. #endif
  99. const uint32_t arch_type = QEMU_ARCH;
  100. /***********************************************************/
  101. /* ram save/restore */
  102. #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
  103. #define RAM_SAVE_FLAG_COMPRESS 0x02
  104. #define RAM_SAVE_FLAG_MEM_SIZE 0x04
  105. #define RAM_SAVE_FLAG_PAGE 0x08
  106. #define RAM_SAVE_FLAG_EOS 0x10
  107. #define RAM_SAVE_FLAG_CONTINUE 0x20
  108. #define RAM_SAVE_FLAG_XBZRLE 0x40
  109. #ifdef __ALTIVEC__
  110. #include <altivec.h>
  111. #define VECTYPE vector unsigned char
  112. #define SPLAT(p) vec_splat(vec_ld(0, p), 0)
  113. #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
  114. /* altivec.h may redefine the bool macro as vector type.
  115. * Reset it to POSIX semantics. */
  116. #undef bool
  117. #define bool _Bool
  118. #elif defined __SSE2__
  119. #include <emmintrin.h>
  120. #define VECTYPE __m128i
  121. #define SPLAT(p) _mm_set1_epi8(*(p))
  122. #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
  123. #else
  124. #define VECTYPE unsigned long
  125. #define SPLAT(p) (*(p) * (~0UL / 255))
  126. #define ALL_EQ(v1, v2) ((v1) == (v2))
  127. #endif
  128. static struct defconfig_file {
  129. const char *filename;
  130. /* Indicates it is an user config file (disabled by -no-user-config) */
  131. bool userconfig;
  132. } default_config_files[] = {
  133. { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
  134. { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
  135. { NULL }, /* end of list */
  136. };
  137. int qemu_read_default_config_files(bool userconfig)
  138. {
  139. int ret;
  140. struct defconfig_file *f;
  141. for (f = default_config_files; f->filename; f++) {
  142. if (!userconfig && f->userconfig) {
  143. continue;
  144. }
  145. ret = qemu_read_config_file(f->filename);
  146. if (ret < 0 && ret != -ENOENT) {
  147. return ret;
  148. }
  149. }
  150. return 0;
  151. }
  152. static int is_dup_page(uint8_t *page)
  153. {
  154. VECTYPE *p = (VECTYPE *)page;
  155. VECTYPE val = SPLAT(page);
  156. int i;
  157. for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
  158. if (!ALL_EQ(val, p[i])) {
  159. return 0;
  160. }
  161. }
  162. return 1;
  163. }
  164. /* struct contains XBZRLE cache and a static page
  165. used by the compression */
  166. static struct {
  167. /* buffer used for XBZRLE encoding */
  168. uint8_t *encoded_buf;
  169. /* buffer for storing page content */
  170. uint8_t *current_buf;
  171. /* buffer used for XBZRLE decoding */
  172. uint8_t *decoded_buf;
  173. /* Cache for XBZRLE */
  174. PageCache *cache;
  175. } XBZRLE = {
  176. .encoded_buf = NULL,
  177. .current_buf = NULL,
  178. .decoded_buf = NULL,
  179. .cache = NULL,
  180. };
  181. int64_t xbzrle_cache_resize(int64_t new_size)
  182. {
  183. if (XBZRLE.cache != NULL) {
  184. return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
  185. TARGET_PAGE_SIZE;
  186. }
  187. return pow2floor(new_size);
  188. }
  189. /* accounting for migration statistics */
  190. typedef struct AccountingInfo {
  191. uint64_t dup_pages;
  192. uint64_t norm_pages;
  193. uint64_t iterations;
  194. uint64_t xbzrle_bytes;
  195. uint64_t xbzrle_pages;
  196. uint64_t xbzrle_cache_miss;
  197. uint64_t xbzrle_overflows;
  198. } AccountingInfo;
  199. static AccountingInfo acct_info;
  200. static void acct_clear(void)
  201. {
  202. memset(&acct_info, 0, sizeof(acct_info));
  203. }
  204. uint64_t dup_mig_bytes_transferred(void)
  205. {
  206. return acct_info.dup_pages * TARGET_PAGE_SIZE;
  207. }
  208. uint64_t dup_mig_pages_transferred(void)
  209. {
  210. return acct_info.dup_pages;
  211. }
  212. uint64_t norm_mig_bytes_transferred(void)
  213. {
  214. return acct_info.norm_pages * TARGET_PAGE_SIZE;
  215. }
  216. uint64_t norm_mig_pages_transferred(void)
  217. {
  218. return acct_info.norm_pages;
  219. }
  220. uint64_t xbzrle_mig_bytes_transferred(void)
  221. {
  222. return acct_info.xbzrle_bytes;
  223. }
  224. uint64_t xbzrle_mig_pages_transferred(void)
  225. {
  226. return acct_info.xbzrle_pages;
  227. }
  228. uint64_t xbzrle_mig_pages_cache_miss(void)
  229. {
  230. return acct_info.xbzrle_cache_miss;
  231. }
  232. uint64_t xbzrle_mig_pages_overflow(void)
  233. {
  234. return acct_info.xbzrle_overflows;
  235. }
  236. static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
  237. int cont, int flag)
  238. {
  239. size_t size;
  240. qemu_put_be64(f, offset | cont | flag);
  241. size = 8;
  242. if (!cont) {
  243. qemu_put_byte(f, strlen(block->idstr));
  244. qemu_put_buffer(f, (uint8_t *)block->idstr,
  245. strlen(block->idstr));
  246. size += 1 + strlen(block->idstr);
  247. }
  248. return size;
  249. }
  250. #define ENCODING_FLAG_XBZRLE 0x1
  251. static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
  252. ram_addr_t current_addr, RAMBlock *block,
  253. ram_addr_t offset, int cont, bool last_stage)
  254. {
  255. int encoded_len = 0, bytes_sent = -1;
  256. uint8_t *prev_cached_page;
  257. if (!cache_is_cached(XBZRLE.cache, current_addr)) {
  258. if (!last_stage) {
  259. cache_insert(XBZRLE.cache, current_addr,
  260. g_memdup(current_data, TARGET_PAGE_SIZE));
  261. }
  262. acct_info.xbzrle_cache_miss++;
  263. return -1;
  264. }
  265. prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
  266. /* save current buffer into memory */
  267. memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
  268. /* XBZRLE encoding (if there is no overflow) */
  269. encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
  270. TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
  271. TARGET_PAGE_SIZE);
  272. if (encoded_len == 0) {
  273. DPRINTF("Skipping unmodified page\n");
  274. return 0;
  275. } else if (encoded_len == -1) {
  276. DPRINTF("Overflow\n");
  277. acct_info.xbzrle_overflows++;
  278. /* update data in the cache */
  279. memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
  280. return -1;
  281. }
  282. /* we need to update the data in the cache, in order to get the same data */
  283. if (!last_stage) {
  284. memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
  285. }
  286. /* Send XBZRLE based compressed page */
  287. bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
  288. qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
  289. qemu_put_be16(f, encoded_len);
  290. qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
  291. bytes_sent += encoded_len + 1 + 2;
  292. acct_info.xbzrle_pages++;
  293. acct_info.xbzrle_bytes += bytes_sent;
  294. return bytes_sent;
  295. }
  296. /* This is the last block that we have visited serching for dirty pages
  297. */
  298. static RAMBlock *last_seen_block;
  299. /* This is the last block from where we have sent data */
  300. static RAMBlock *last_sent_block;
  301. static ram_addr_t last_offset;
  302. static unsigned long *migration_bitmap;
  303. static uint64_t migration_dirty_pages;
  304. static uint32_t last_version;
  305. static inline
  306. ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
  307. ram_addr_t start)
  308. {
  309. unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
  310. unsigned long nr = base + (start >> TARGET_PAGE_BITS);
  311. unsigned long size = base + (int128_get64(mr->size) >> TARGET_PAGE_BITS);
  312. unsigned long next = find_next_bit(migration_bitmap, size, nr);
  313. if (next < size) {
  314. clear_bit(next, migration_bitmap);
  315. migration_dirty_pages--;
  316. }
  317. return (next - base) << TARGET_PAGE_BITS;
  318. }
  319. static inline bool migration_bitmap_set_dirty(MemoryRegion *mr,
  320. ram_addr_t offset)
  321. {
  322. bool ret;
  323. int nr = (mr->ram_addr + offset) >> TARGET_PAGE_BITS;
  324. ret = test_and_set_bit(nr, migration_bitmap);
  325. if (!ret) {
  326. migration_dirty_pages++;
  327. }
  328. return ret;
  329. }
  330. static void migration_bitmap_sync(void)
  331. {
  332. RAMBlock *block;
  333. ram_addr_t addr;
  334. uint64_t num_dirty_pages_init = migration_dirty_pages;
  335. MigrationState *s = migrate_get_current();
  336. static int64_t start_time;
  337. static int64_t num_dirty_pages_period;
  338. int64_t end_time;
  339. if (!start_time) {
  340. start_time = qemu_get_clock_ms(rt_clock);
  341. }
  342. trace_migration_bitmap_sync_start();
  343. memory_global_sync_dirty_bitmap(get_system_memory());
  344. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  345. for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
  346. if (memory_region_test_and_clear_dirty(block->mr,
  347. addr, TARGET_PAGE_SIZE,
  348. DIRTY_MEMORY_MIGRATION)) {
  349. migration_bitmap_set_dirty(block->mr, addr);
  350. }
  351. }
  352. }
  353. trace_migration_bitmap_sync_end(migration_dirty_pages
  354. - num_dirty_pages_init);
  355. num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
  356. end_time = qemu_get_clock_ms(rt_clock);
  357. /* more than 1 second = 1000 millisecons */
  358. if (end_time > start_time + 1000) {
  359. s->dirty_pages_rate = num_dirty_pages_period * 1000
  360. / (end_time - start_time);
  361. start_time = end_time;
  362. num_dirty_pages_period = 0;
  363. }
  364. }
  365. /*
  366. * ram_save_block: Writes a page of memory to the stream f
  367. *
  368. * Returns: The number of bytes written.
  369. * 0 means no dirty pages
  370. */
  371. static int ram_save_block(QEMUFile *f, bool last_stage)
  372. {
  373. RAMBlock *block = last_seen_block;
  374. ram_addr_t offset = last_offset;
  375. bool complete_round = false;
  376. int bytes_sent = 0;
  377. MemoryRegion *mr;
  378. ram_addr_t current_addr;
  379. if (!block)
  380. block = QTAILQ_FIRST(&ram_list.blocks);
  381. while (true) {
  382. mr = block->mr;
  383. offset = migration_bitmap_find_and_reset_dirty(mr, offset);
  384. if (complete_round && block == last_seen_block &&
  385. offset >= last_offset) {
  386. break;
  387. }
  388. if (offset >= block->length) {
  389. offset = 0;
  390. block = QTAILQ_NEXT(block, next);
  391. if (!block) {
  392. block = QTAILQ_FIRST(&ram_list.blocks);
  393. complete_round = true;
  394. }
  395. } else {
  396. uint8_t *p;
  397. int cont = (block == last_sent_block) ?
  398. RAM_SAVE_FLAG_CONTINUE : 0;
  399. p = memory_region_get_ram_ptr(mr) + offset;
  400. /* In doubt sent page as normal */
  401. bytes_sent = -1;
  402. if (is_dup_page(p)) {
  403. acct_info.dup_pages++;
  404. bytes_sent = save_block_hdr(f, block, offset, cont,
  405. RAM_SAVE_FLAG_COMPRESS);
  406. qemu_put_byte(f, *p);
  407. bytes_sent += 1;
  408. } else if (migrate_use_xbzrle()) {
  409. current_addr = block->offset + offset;
  410. bytes_sent = save_xbzrle_page(f, p, current_addr, block,
  411. offset, cont, last_stage);
  412. if (!last_stage) {
  413. p = get_cached_data(XBZRLE.cache, current_addr);
  414. }
  415. }
  416. /* XBZRLE overflow or normal page */
  417. if (bytes_sent == -1) {
  418. bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
  419. qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
  420. bytes_sent += TARGET_PAGE_SIZE;
  421. acct_info.norm_pages++;
  422. }
  423. /* if page is unmodified, continue to the next */
  424. if (bytes_sent > 0) {
  425. last_sent_block = block;
  426. break;
  427. }
  428. }
  429. }
  430. last_seen_block = block;
  431. last_offset = offset;
  432. return bytes_sent;
  433. }
  434. static uint64_t bytes_transferred;
  435. static ram_addr_t ram_save_remaining(void)
  436. {
  437. return migration_dirty_pages;
  438. }
  439. uint64_t ram_bytes_remaining(void)
  440. {
  441. return ram_save_remaining() * TARGET_PAGE_SIZE;
  442. }
  443. uint64_t ram_bytes_transferred(void)
  444. {
  445. return bytes_transferred;
  446. }
  447. uint64_t ram_bytes_total(void)
  448. {
  449. RAMBlock *block;
  450. uint64_t total = 0;
  451. QTAILQ_FOREACH(block, &ram_list.blocks, next)
  452. total += block->length;
  453. return total;
  454. }
  455. static void migration_end(void)
  456. {
  457. if (migration_bitmap) {
  458. memory_global_dirty_log_stop();
  459. g_free(migration_bitmap);
  460. migration_bitmap = NULL;
  461. }
  462. if (XBZRLE.cache) {
  463. cache_fini(XBZRLE.cache);
  464. g_free(XBZRLE.cache);
  465. g_free(XBZRLE.encoded_buf);
  466. g_free(XBZRLE.current_buf);
  467. g_free(XBZRLE.decoded_buf);
  468. XBZRLE.cache = NULL;
  469. }
  470. }
  471. static void ram_migration_cancel(void *opaque)
  472. {
  473. migration_end();
  474. }
  475. static void reset_ram_globals(void)
  476. {
  477. last_seen_block = NULL;
  478. last_sent_block = NULL;
  479. last_offset = 0;
  480. last_version = ram_list.version;
  481. }
  482. #define MAX_WAIT 50 /* ms, half buffered_file limit */
  483. static int ram_save_setup(QEMUFile *f, void *opaque)
  484. {
  485. RAMBlock *block;
  486. int64_t ram_pages = last_ram_offset() >> TARGET_PAGE_BITS;
  487. migration_bitmap = bitmap_new(ram_pages);
  488. bitmap_set(migration_bitmap, 0, ram_pages);
  489. migration_dirty_pages = ram_pages;
  490. qemu_mutex_lock_ramlist();
  491. bytes_transferred = 0;
  492. reset_ram_globals();
  493. if (migrate_use_xbzrle()) {
  494. XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
  495. TARGET_PAGE_SIZE,
  496. TARGET_PAGE_SIZE);
  497. if (!XBZRLE.cache) {
  498. DPRINTF("Error creating cache\n");
  499. return -1;
  500. }
  501. XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
  502. XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
  503. acct_clear();
  504. }
  505. memory_global_dirty_log_start();
  506. migration_bitmap_sync();
  507. qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
  508. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  509. qemu_put_byte(f, strlen(block->idstr));
  510. qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
  511. qemu_put_be64(f, block->length);
  512. }
  513. qemu_mutex_unlock_ramlist();
  514. qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
  515. return 0;
  516. }
  517. static int ram_save_iterate(QEMUFile *f, void *opaque)
  518. {
  519. int ret;
  520. int i;
  521. int64_t t0;
  522. int total_sent = 0;
  523. qemu_mutex_lock_ramlist();
  524. if (ram_list.version != last_version) {
  525. reset_ram_globals();
  526. }
  527. t0 = qemu_get_clock_ns(rt_clock);
  528. i = 0;
  529. while ((ret = qemu_file_rate_limit(f)) == 0) {
  530. int bytes_sent;
  531. bytes_sent = ram_save_block(f, false);
  532. /* no more blocks to sent */
  533. if (bytes_sent == 0) {
  534. break;
  535. }
  536. total_sent += bytes_sent;
  537. acct_info.iterations++;
  538. /* we want to check in the 1st loop, just in case it was the 1st time
  539. and we had to sync the dirty bitmap.
  540. qemu_get_clock_ns() is a bit expensive, so we only check each some
  541. iterations
  542. */
  543. if ((i & 63) == 0) {
  544. uint64_t t1 = (qemu_get_clock_ns(rt_clock) - t0) / 1000000;
  545. if (t1 > MAX_WAIT) {
  546. DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
  547. t1, i);
  548. break;
  549. }
  550. }
  551. i++;
  552. }
  553. qemu_mutex_unlock_ramlist();
  554. if (ret < 0) {
  555. bytes_transferred += total_sent;
  556. return ret;
  557. }
  558. qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
  559. total_sent += 8;
  560. bytes_transferred += total_sent;
  561. return total_sent;
  562. }
  563. static int ram_save_complete(QEMUFile *f, void *opaque)
  564. {
  565. qemu_mutex_lock_ramlist();
  566. migration_bitmap_sync();
  567. /* try transferring iterative blocks of memory */
  568. /* flush all remaining blocks regardless of rate limiting */
  569. while (true) {
  570. int bytes_sent;
  571. bytes_sent = ram_save_block(f, true);
  572. /* no more blocks to sent */
  573. if (bytes_sent == 0) {
  574. break;
  575. }
  576. bytes_transferred += bytes_sent;
  577. }
  578. migration_end();
  579. qemu_mutex_unlock_ramlist();
  580. qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
  581. return 0;
  582. }
  583. static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
  584. {
  585. uint64_t remaining_size;
  586. remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
  587. if (remaining_size < max_size) {
  588. migration_bitmap_sync();
  589. remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
  590. }
  591. return remaining_size;
  592. }
  593. static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
  594. {
  595. int ret, rc = 0;
  596. unsigned int xh_len;
  597. int xh_flags;
  598. if (!XBZRLE.decoded_buf) {
  599. XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
  600. }
  601. /* extract RLE header */
  602. xh_flags = qemu_get_byte(f);
  603. xh_len = qemu_get_be16(f);
  604. if (xh_flags != ENCODING_FLAG_XBZRLE) {
  605. fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
  606. return -1;
  607. }
  608. if (xh_len > TARGET_PAGE_SIZE) {
  609. fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
  610. return -1;
  611. }
  612. /* load data and decode */
  613. qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
  614. /* decode RLE */
  615. ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
  616. TARGET_PAGE_SIZE);
  617. if (ret == -1) {
  618. fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
  619. rc = -1;
  620. } else if (ret > TARGET_PAGE_SIZE) {
  621. fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
  622. ret, TARGET_PAGE_SIZE);
  623. abort();
  624. }
  625. return rc;
  626. }
  627. static inline void *host_from_stream_offset(QEMUFile *f,
  628. ram_addr_t offset,
  629. int flags)
  630. {
  631. static RAMBlock *block = NULL;
  632. char id[256];
  633. uint8_t len;
  634. if (flags & RAM_SAVE_FLAG_CONTINUE) {
  635. if (!block) {
  636. fprintf(stderr, "Ack, bad migration stream!\n");
  637. return NULL;
  638. }
  639. return memory_region_get_ram_ptr(block->mr) + offset;
  640. }
  641. len = qemu_get_byte(f);
  642. qemu_get_buffer(f, (uint8_t *)id, len);
  643. id[len] = 0;
  644. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  645. if (!strncmp(id, block->idstr, sizeof(id)))
  646. return memory_region_get_ram_ptr(block->mr) + offset;
  647. }
  648. fprintf(stderr, "Can't find block %s!\n", id);
  649. return NULL;
  650. }
  651. static int ram_load(QEMUFile *f, void *opaque, int version_id)
  652. {
  653. ram_addr_t addr;
  654. int flags, ret = 0;
  655. int error;
  656. static uint64_t seq_iter;
  657. seq_iter++;
  658. if (version_id < 4 || version_id > 4) {
  659. return -EINVAL;
  660. }
  661. do {
  662. addr = qemu_get_be64(f);
  663. flags = addr & ~TARGET_PAGE_MASK;
  664. addr &= TARGET_PAGE_MASK;
  665. if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
  666. if (version_id == 4) {
  667. /* Synchronize RAM block list */
  668. char id[256];
  669. ram_addr_t length;
  670. ram_addr_t total_ram_bytes = addr;
  671. while (total_ram_bytes) {
  672. RAMBlock *block;
  673. uint8_t len;
  674. len = qemu_get_byte(f);
  675. qemu_get_buffer(f, (uint8_t *)id, len);
  676. id[len] = 0;
  677. length = qemu_get_be64(f);
  678. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  679. if (!strncmp(id, block->idstr, sizeof(id))) {
  680. if (block->length != length) {
  681. ret = -EINVAL;
  682. goto done;
  683. }
  684. break;
  685. }
  686. }
  687. if (!block) {
  688. fprintf(stderr, "Unknown ramblock \"%s\", cannot "
  689. "accept migration\n", id);
  690. ret = -EINVAL;
  691. goto done;
  692. }
  693. total_ram_bytes -= length;
  694. }
  695. }
  696. }
  697. if (flags & RAM_SAVE_FLAG_COMPRESS) {
  698. void *host;
  699. uint8_t ch;
  700. host = host_from_stream_offset(f, addr, flags);
  701. if (!host) {
  702. return -EINVAL;
  703. }
  704. ch = qemu_get_byte(f);
  705. memset(host, ch, TARGET_PAGE_SIZE);
  706. #ifndef _WIN32
  707. if (ch == 0 &&
  708. (!kvm_enabled() || kvm_has_sync_mmu()) &&
  709. getpagesize() <= TARGET_PAGE_SIZE) {
  710. qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
  711. }
  712. #endif
  713. } else if (flags & RAM_SAVE_FLAG_PAGE) {
  714. void *host;
  715. host = host_from_stream_offset(f, addr, flags);
  716. if (!host) {
  717. return -EINVAL;
  718. }
  719. qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
  720. } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
  721. void *host = host_from_stream_offset(f, addr, flags);
  722. if (!host) {
  723. return -EINVAL;
  724. }
  725. if (load_xbzrle(f, addr, host) < 0) {
  726. ret = -EINVAL;
  727. goto done;
  728. }
  729. }
  730. error = qemu_file_get_error(f);
  731. if (error) {
  732. ret = error;
  733. goto done;
  734. }
  735. } while (!(flags & RAM_SAVE_FLAG_EOS));
  736. done:
  737. DPRINTF("Completed load of VM with exit code %d seq iteration "
  738. "%" PRIu64 "\n", ret, seq_iter);
  739. return ret;
  740. }
  741. SaveVMHandlers savevm_ram_handlers = {
  742. .save_live_setup = ram_save_setup,
  743. .save_live_iterate = ram_save_iterate,
  744. .save_live_complete = ram_save_complete,
  745. .save_live_pending = ram_save_pending,
  746. .load_state = ram_load,
  747. .cancel = ram_migration_cancel,
  748. };
  749. #ifdef HAS_AUDIO
  750. struct soundhw {
  751. const char *name;
  752. const char *descr;
  753. int enabled;
  754. int isa;
  755. union {
  756. int (*init_isa) (ISABus *bus);
  757. int (*init_pci) (PCIBus *bus);
  758. } init;
  759. };
  760. static struct soundhw soundhw[] = {
  761. #ifdef HAS_AUDIO_CHOICE
  762. #ifdef CONFIG_PCSPK
  763. {
  764. "pcspk",
  765. "PC speaker",
  766. 0,
  767. 1,
  768. { .init_isa = pcspk_audio_init }
  769. },
  770. #endif
  771. #ifdef CONFIG_SB16
  772. {
  773. "sb16",
  774. "Creative Sound Blaster 16",
  775. 0,
  776. 1,
  777. { .init_isa = SB16_init }
  778. },
  779. #endif
  780. #ifdef CONFIG_CS4231A
  781. {
  782. "cs4231a",
  783. "CS4231A",
  784. 0,
  785. 1,
  786. { .init_isa = cs4231a_init }
  787. },
  788. #endif
  789. #ifdef CONFIG_ADLIB
  790. {
  791. "adlib",
  792. #ifdef HAS_YMF262
  793. "Yamaha YMF262 (OPL3)",
  794. #else
  795. "Yamaha YM3812 (OPL2)",
  796. #endif
  797. 0,
  798. 1,
  799. { .init_isa = Adlib_init }
  800. },
  801. #endif
  802. #ifdef CONFIG_GUS
  803. {
  804. "gus",
  805. "Gravis Ultrasound GF1",
  806. 0,
  807. 1,
  808. { .init_isa = GUS_init }
  809. },
  810. #endif
  811. #ifdef CONFIG_AC97
  812. {
  813. "ac97",
  814. "Intel 82801AA AC97 Audio",
  815. 0,
  816. 0,
  817. { .init_pci = ac97_init }
  818. },
  819. #endif
  820. #ifdef CONFIG_ES1370
  821. {
  822. "es1370",
  823. "ENSONIQ AudioPCI ES1370",
  824. 0,
  825. 0,
  826. { .init_pci = es1370_init }
  827. },
  828. #endif
  829. #ifdef CONFIG_HDA
  830. {
  831. "hda",
  832. "Intel HD Audio",
  833. 0,
  834. 0,
  835. { .init_pci = intel_hda_and_codec_init }
  836. },
  837. #endif
  838. #endif /* HAS_AUDIO_CHOICE */
  839. { NULL, NULL, 0, 0, { NULL } }
  840. };
  841. void select_soundhw(const char *optarg)
  842. {
  843. struct soundhw *c;
  844. if (is_help_option(optarg)) {
  845. show_valid_cards:
  846. #ifdef HAS_AUDIO_CHOICE
  847. printf("Valid sound card names (comma separated):\n");
  848. for (c = soundhw; c->name; ++c) {
  849. printf ("%-11s %s\n", c->name, c->descr);
  850. }
  851. printf("\n-soundhw all will enable all of the above\n");
  852. #else
  853. printf("Machine has no user-selectable audio hardware "
  854. "(it may or may not have always-present audio hardware).\n");
  855. #endif
  856. exit(!is_help_option(optarg));
  857. }
  858. else {
  859. size_t l;
  860. const char *p;
  861. char *e;
  862. int bad_card = 0;
  863. if (!strcmp(optarg, "all")) {
  864. for (c = soundhw; c->name; ++c) {
  865. c->enabled = 1;
  866. }
  867. return;
  868. }
  869. p = optarg;
  870. while (*p) {
  871. e = strchr(p, ',');
  872. l = !e ? strlen(p) : (size_t) (e - p);
  873. for (c = soundhw; c->name; ++c) {
  874. if (!strncmp(c->name, p, l) && !c->name[l]) {
  875. c->enabled = 1;
  876. break;
  877. }
  878. }
  879. if (!c->name) {
  880. if (l > 80) {
  881. fprintf(stderr,
  882. "Unknown sound card name (too big to show)\n");
  883. }
  884. else {
  885. fprintf(stderr, "Unknown sound card name `%.*s'\n",
  886. (int) l, p);
  887. }
  888. bad_card = 1;
  889. }
  890. p += l + (e != NULL);
  891. }
  892. if (bad_card) {
  893. goto show_valid_cards;
  894. }
  895. }
  896. }
  897. void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
  898. {
  899. struct soundhw *c;
  900. for (c = soundhw; c->name; ++c) {
  901. if (c->enabled) {
  902. if (c->isa) {
  903. if (isa_bus) {
  904. c->init.init_isa(isa_bus);
  905. }
  906. } else {
  907. if (pci_bus) {
  908. c->init.init_pci(pci_bus);
  909. }
  910. }
  911. }
  912. }
  913. }
  914. #else
  915. void select_soundhw(const char *optarg)
  916. {
  917. }
  918. void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
  919. {
  920. }
  921. #endif
  922. int qemu_uuid_parse(const char *str, uint8_t *uuid)
  923. {
  924. int ret;
  925. if (strlen(str) != 36) {
  926. return -1;
  927. }
  928. ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
  929. &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
  930. &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
  931. &uuid[15]);
  932. if (ret != 16) {
  933. return -1;
  934. }
  935. #ifdef TARGET_I386
  936. smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
  937. #endif
  938. return 0;
  939. }
  940. void do_acpitable_option(const char *optarg)
  941. {
  942. #ifdef TARGET_I386
  943. if (acpi_table_add(optarg) < 0) {
  944. fprintf(stderr, "Wrong acpi table provided\n");
  945. exit(1);
  946. }
  947. #endif
  948. }
  949. void do_smbios_option(const char *optarg)
  950. {
  951. #ifdef TARGET_I386
  952. if (smbios_entry_add(optarg) < 0) {
  953. fprintf(stderr, "Wrong smbios provided\n");
  954. exit(1);
  955. }
  956. #endif
  957. }
  958. void cpudef_init(void)
  959. {
  960. #if defined(cpudef_setup)
  961. cpudef_setup(); /* parse cpu definitions in target config file */
  962. #endif
  963. }
  964. int audio_available(void)
  965. {
  966. #ifdef HAS_AUDIO
  967. return 1;
  968. #else
  969. return 0;
  970. #endif
  971. }
  972. int tcg_available(void)
  973. {
  974. return 1;
  975. }
  976. int kvm_available(void)
  977. {
  978. #ifdef CONFIG_KVM
  979. return 1;
  980. #else
  981. return 0;
  982. #endif
  983. }
  984. int xen_available(void)
  985. {
  986. #ifdef CONFIG_XEN
  987. return 1;
  988. #else
  989. return 0;
  990. #endif
  991. }
  992. TargetInfo *qmp_query_target(Error **errp)
  993. {
  994. TargetInfo *info = g_malloc0(sizeof(*info));
  995. info->arch = TARGET_TYPE;
  996. return info;
  997. }