2
0

arch_init.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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/i386/pc.h"
  39. #include "hw/pci/pci.h"
  40. #include "hw/audio/audio.h"
  41. #include "sysemu/kvm.h"
  42. #include "migration/migration.h"
  43. #include "hw/i386/smbios.h"
  44. #include "exec/address-spaces.h"
  45. #include "hw/audio/pcspk.h"
  46. #include "migration/page_cache.h"
  47. #include "qemu/config-file.h"
  48. #include "qmp-commands.h"
  49. #include "trace.h"
  50. #include "exec/cpu-all.h"
  51. #include "exec/ram_addr.h"
  52. #include "hw/acpi/acpi.h"
  53. #include "qemu/host-utils.h"
  54. #ifdef DEBUG_ARCH_INIT
  55. #define DPRINTF(fmt, ...) \
  56. do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
  57. #else
  58. #define DPRINTF(fmt, ...) \
  59. do { } while (0)
  60. #endif
  61. #ifdef TARGET_SPARC
  62. int graphic_width = 1024;
  63. int graphic_height = 768;
  64. int graphic_depth = 8;
  65. #else
  66. int graphic_width = 800;
  67. int graphic_height = 600;
  68. int graphic_depth = 32;
  69. #endif
  70. #if defined(TARGET_ALPHA)
  71. #define QEMU_ARCH QEMU_ARCH_ALPHA
  72. #elif defined(TARGET_ARM)
  73. #define QEMU_ARCH QEMU_ARCH_ARM
  74. #elif defined(TARGET_CRIS)
  75. #define QEMU_ARCH QEMU_ARCH_CRIS
  76. #elif defined(TARGET_I386)
  77. #define QEMU_ARCH QEMU_ARCH_I386
  78. #elif defined(TARGET_M68K)
  79. #define QEMU_ARCH QEMU_ARCH_M68K
  80. #elif defined(TARGET_LM32)
  81. #define QEMU_ARCH QEMU_ARCH_LM32
  82. #elif defined(TARGET_MICROBLAZE)
  83. #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
  84. #elif defined(TARGET_MIPS)
  85. #define QEMU_ARCH QEMU_ARCH_MIPS
  86. #elif defined(TARGET_MOXIE)
  87. #define QEMU_ARCH QEMU_ARCH_MOXIE
  88. #elif defined(TARGET_OPENRISC)
  89. #define QEMU_ARCH QEMU_ARCH_OPENRISC
  90. #elif defined(TARGET_PPC)
  91. #define QEMU_ARCH QEMU_ARCH_PPC
  92. #elif defined(TARGET_S390X)
  93. #define QEMU_ARCH QEMU_ARCH_S390X
  94. #elif defined(TARGET_SH4)
  95. #define QEMU_ARCH QEMU_ARCH_SH4
  96. #elif defined(TARGET_SPARC)
  97. #define QEMU_ARCH QEMU_ARCH_SPARC
  98. #elif defined(TARGET_XTENSA)
  99. #define QEMU_ARCH QEMU_ARCH_XTENSA
  100. #elif defined(TARGET_UNICORE32)
  101. #define QEMU_ARCH QEMU_ARCH_UNICORE32
  102. #endif
  103. const uint32_t arch_type = QEMU_ARCH;
  104. static bool mig_throttle_on;
  105. static int dirty_rate_high_cnt;
  106. static void check_guest_throttling(void);
  107. /***********************************************************/
  108. /* ram save/restore */
  109. #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
  110. #define RAM_SAVE_FLAG_COMPRESS 0x02
  111. #define RAM_SAVE_FLAG_MEM_SIZE 0x04
  112. #define RAM_SAVE_FLAG_PAGE 0x08
  113. #define RAM_SAVE_FLAG_EOS 0x10
  114. #define RAM_SAVE_FLAG_CONTINUE 0x20
  115. #define RAM_SAVE_FLAG_XBZRLE 0x40
  116. /* 0x80 is reserved in migration.h start with 0x100 next */
  117. static struct defconfig_file {
  118. const char *filename;
  119. /* Indicates it is an user config file (disabled by -no-user-config) */
  120. bool userconfig;
  121. } default_config_files[] = {
  122. { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
  123. { CONFIG_QEMU_CONFDIR "/target-" TARGET_NAME ".conf", true },
  124. { NULL }, /* end of list */
  125. };
  126. static const uint8_t ZERO_TARGET_PAGE[TARGET_PAGE_SIZE];
  127. int qemu_read_default_config_files(bool userconfig)
  128. {
  129. int ret;
  130. struct defconfig_file *f;
  131. for (f = default_config_files; f->filename; f++) {
  132. if (!userconfig && f->userconfig) {
  133. continue;
  134. }
  135. ret = qemu_read_config_file(f->filename);
  136. if (ret < 0 && ret != -ENOENT) {
  137. return ret;
  138. }
  139. }
  140. return 0;
  141. }
  142. static inline bool is_zero_range(uint8_t *p, uint64_t size)
  143. {
  144. return buffer_find_nonzero_offset(p, size) == size;
  145. }
  146. /* struct contains XBZRLE cache and a static page
  147. used by the compression */
  148. static struct {
  149. /* buffer used for XBZRLE encoding */
  150. uint8_t *encoded_buf;
  151. /* buffer for storing page content */
  152. uint8_t *current_buf;
  153. /* Cache for XBZRLE, Protected by lock. */
  154. PageCache *cache;
  155. QemuMutex lock;
  156. } XBZRLE = {
  157. .encoded_buf = NULL,
  158. .current_buf = NULL,
  159. .cache = NULL,
  160. };
  161. /* buffer used for XBZRLE decoding */
  162. static uint8_t *xbzrle_decoded_buf;
  163. static void XBZRLE_cache_lock(void)
  164. {
  165. if (migrate_use_xbzrle())
  166. qemu_mutex_lock(&XBZRLE.lock);
  167. }
  168. static void XBZRLE_cache_unlock(void)
  169. {
  170. if (migrate_use_xbzrle())
  171. qemu_mutex_unlock(&XBZRLE.lock);
  172. }
  173. int64_t xbzrle_cache_resize(int64_t new_size)
  174. {
  175. PageCache *new_cache, *cache_to_free;
  176. if (new_size < TARGET_PAGE_SIZE) {
  177. return -1;
  178. }
  179. /* no need to lock, the current thread holds qemu big lock */
  180. if (XBZRLE.cache != NULL) {
  181. /* check XBZRLE.cache again later */
  182. if (pow2floor(new_size) == migrate_xbzrle_cache_size()) {
  183. return pow2floor(new_size);
  184. }
  185. new_cache = cache_init(new_size / TARGET_PAGE_SIZE,
  186. TARGET_PAGE_SIZE);
  187. if (!new_cache) {
  188. DPRINTF("Error creating cache\n");
  189. return -1;
  190. }
  191. XBZRLE_cache_lock();
  192. /* the XBZRLE.cache may have be destroyed, check it again */
  193. if (XBZRLE.cache != NULL) {
  194. cache_to_free = XBZRLE.cache;
  195. XBZRLE.cache = new_cache;
  196. } else {
  197. cache_to_free = new_cache;
  198. }
  199. XBZRLE_cache_unlock();
  200. cache_fini(cache_to_free);
  201. }
  202. return pow2floor(new_size);
  203. }
  204. /* accounting for migration statistics */
  205. typedef struct AccountingInfo {
  206. uint64_t dup_pages;
  207. uint64_t skipped_pages;
  208. uint64_t norm_pages;
  209. uint64_t iterations;
  210. uint64_t xbzrle_bytes;
  211. uint64_t xbzrle_pages;
  212. uint64_t xbzrle_cache_miss;
  213. uint64_t xbzrle_overflows;
  214. } AccountingInfo;
  215. static AccountingInfo acct_info;
  216. static void acct_clear(void)
  217. {
  218. memset(&acct_info, 0, sizeof(acct_info));
  219. }
  220. uint64_t dup_mig_bytes_transferred(void)
  221. {
  222. return acct_info.dup_pages * TARGET_PAGE_SIZE;
  223. }
  224. uint64_t dup_mig_pages_transferred(void)
  225. {
  226. return acct_info.dup_pages;
  227. }
  228. uint64_t skipped_mig_bytes_transferred(void)
  229. {
  230. return acct_info.skipped_pages * TARGET_PAGE_SIZE;
  231. }
  232. uint64_t skipped_mig_pages_transferred(void)
  233. {
  234. return acct_info.skipped_pages;
  235. }
  236. uint64_t norm_mig_bytes_transferred(void)
  237. {
  238. return acct_info.norm_pages * TARGET_PAGE_SIZE;
  239. }
  240. uint64_t norm_mig_pages_transferred(void)
  241. {
  242. return acct_info.norm_pages;
  243. }
  244. uint64_t xbzrle_mig_bytes_transferred(void)
  245. {
  246. return acct_info.xbzrle_bytes;
  247. }
  248. uint64_t xbzrle_mig_pages_transferred(void)
  249. {
  250. return acct_info.xbzrle_pages;
  251. }
  252. uint64_t xbzrle_mig_pages_cache_miss(void)
  253. {
  254. return acct_info.xbzrle_cache_miss;
  255. }
  256. uint64_t xbzrle_mig_pages_overflow(void)
  257. {
  258. return acct_info.xbzrle_overflows;
  259. }
  260. static size_t save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
  261. int cont, int flag)
  262. {
  263. size_t size;
  264. qemu_put_be64(f, offset | cont | flag);
  265. size = 8;
  266. if (!cont) {
  267. qemu_put_byte(f, strlen(block->idstr));
  268. qemu_put_buffer(f, (uint8_t *)block->idstr,
  269. strlen(block->idstr));
  270. size += 1 + strlen(block->idstr);
  271. }
  272. return size;
  273. }
  274. /* This is the last block that we have visited serching for dirty pages
  275. */
  276. static RAMBlock *last_seen_block;
  277. /* This is the last block from where we have sent data */
  278. static RAMBlock *last_sent_block;
  279. static ram_addr_t last_offset;
  280. static unsigned long *migration_bitmap;
  281. static uint64_t migration_dirty_pages;
  282. static uint32_t last_version;
  283. static bool ram_bulk_stage;
  284. /* Update the xbzrle cache to reflect a page that's been sent as all 0.
  285. * The important thing is that a stale (not-yet-0'd) page be replaced
  286. * by the new data.
  287. * As a bonus, if the page wasn't in the cache it gets added so that
  288. * when a small write is made into the 0'd page it gets XBZRLE sent
  289. */
  290. static void xbzrle_cache_zero_page(ram_addr_t current_addr)
  291. {
  292. if (ram_bulk_stage || !migrate_use_xbzrle()) {
  293. return;
  294. }
  295. /* We don't care if this fails to allocate a new cache page
  296. * as long as it updated an old one */
  297. cache_insert(XBZRLE.cache, current_addr, ZERO_TARGET_PAGE);
  298. }
  299. #define ENCODING_FLAG_XBZRLE 0x1
  300. static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
  301. ram_addr_t current_addr, RAMBlock *block,
  302. ram_addr_t offset, int cont, bool last_stage)
  303. {
  304. int encoded_len = 0, bytes_sent = -1;
  305. uint8_t *prev_cached_page;
  306. if (!cache_is_cached(XBZRLE.cache, current_addr)) {
  307. if (!last_stage) {
  308. if (cache_insert(XBZRLE.cache, current_addr, current_data) == -1) {
  309. return -1;
  310. }
  311. }
  312. acct_info.xbzrle_cache_miss++;
  313. return -1;
  314. }
  315. prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
  316. /* save current buffer into memory */
  317. memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
  318. /* XBZRLE encoding (if there is no overflow) */
  319. encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
  320. TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
  321. TARGET_PAGE_SIZE);
  322. if (encoded_len == 0) {
  323. DPRINTF("Skipping unmodified page\n");
  324. return 0;
  325. } else if (encoded_len == -1) {
  326. DPRINTF("Overflow\n");
  327. acct_info.xbzrle_overflows++;
  328. /* update data in the cache */
  329. memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
  330. return -1;
  331. }
  332. /* we need to update the data in the cache, in order to get the same data */
  333. if (!last_stage) {
  334. memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
  335. }
  336. /* Send XBZRLE based compressed page */
  337. bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
  338. qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
  339. qemu_put_be16(f, encoded_len);
  340. qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
  341. bytes_sent += encoded_len + 1 + 2;
  342. acct_info.xbzrle_pages++;
  343. acct_info.xbzrle_bytes += bytes_sent;
  344. return bytes_sent;
  345. }
  346. static inline
  347. ram_addr_t migration_bitmap_find_and_reset_dirty(MemoryRegion *mr,
  348. ram_addr_t start)
  349. {
  350. unsigned long base = mr->ram_addr >> TARGET_PAGE_BITS;
  351. unsigned long nr = base + (start >> TARGET_PAGE_BITS);
  352. uint64_t mr_size = TARGET_PAGE_ALIGN(memory_region_size(mr));
  353. unsigned long size = base + (mr_size >> TARGET_PAGE_BITS);
  354. unsigned long next;
  355. if (ram_bulk_stage && nr > base) {
  356. next = nr + 1;
  357. } else {
  358. next = find_next_bit(migration_bitmap, size, nr);
  359. }
  360. if (next < size) {
  361. clear_bit(next, migration_bitmap);
  362. migration_dirty_pages--;
  363. }
  364. return (next - base) << TARGET_PAGE_BITS;
  365. }
  366. static inline bool migration_bitmap_set_dirty(ram_addr_t addr)
  367. {
  368. bool ret;
  369. int nr = addr >> TARGET_PAGE_BITS;
  370. ret = test_and_set_bit(nr, migration_bitmap);
  371. if (!ret) {
  372. migration_dirty_pages++;
  373. }
  374. return ret;
  375. }
  376. static void migration_bitmap_sync_range(ram_addr_t start, ram_addr_t length)
  377. {
  378. ram_addr_t addr;
  379. unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
  380. /* start address is aligned at the start of a word? */
  381. if (((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) {
  382. int k;
  383. int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
  384. unsigned long *src = ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION];
  385. for (k = page; k < page + nr; k++) {
  386. if (src[k]) {
  387. unsigned long new_dirty;
  388. new_dirty = ~migration_bitmap[k];
  389. migration_bitmap[k] |= src[k];
  390. new_dirty &= src[k];
  391. migration_dirty_pages += ctpopl(new_dirty);
  392. src[k] = 0;
  393. }
  394. }
  395. } else {
  396. for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
  397. if (cpu_physical_memory_get_dirty(start + addr,
  398. TARGET_PAGE_SIZE,
  399. DIRTY_MEMORY_MIGRATION)) {
  400. cpu_physical_memory_reset_dirty(start + addr,
  401. TARGET_PAGE_SIZE,
  402. DIRTY_MEMORY_MIGRATION);
  403. migration_bitmap_set_dirty(start + addr);
  404. }
  405. }
  406. }
  407. }
  408. /* Needs iothread lock! */
  409. static void migration_bitmap_sync(void)
  410. {
  411. RAMBlock *block;
  412. uint64_t num_dirty_pages_init = migration_dirty_pages;
  413. MigrationState *s = migrate_get_current();
  414. static int64_t start_time;
  415. static int64_t bytes_xfer_prev;
  416. static int64_t num_dirty_pages_period;
  417. int64_t end_time;
  418. int64_t bytes_xfer_now;
  419. if (!bytes_xfer_prev) {
  420. bytes_xfer_prev = ram_bytes_transferred();
  421. }
  422. if (!start_time) {
  423. start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  424. }
  425. trace_migration_bitmap_sync_start();
  426. address_space_sync_dirty_bitmap(&address_space_memory);
  427. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  428. migration_bitmap_sync_range(block->mr->ram_addr, block->length);
  429. }
  430. trace_migration_bitmap_sync_end(migration_dirty_pages
  431. - num_dirty_pages_init);
  432. num_dirty_pages_period += migration_dirty_pages - num_dirty_pages_init;
  433. end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  434. /* more than 1 second = 1000 millisecons */
  435. if (end_time > start_time + 1000) {
  436. if (migrate_auto_converge()) {
  437. /* The following detection logic can be refined later. For now:
  438. Check to see if the dirtied bytes is 50% more than the approx.
  439. amount of bytes that just got transferred since the last time we
  440. were in this routine. If that happens >N times (for now N==4)
  441. we turn on the throttle down logic */
  442. bytes_xfer_now = ram_bytes_transferred();
  443. if (s->dirty_pages_rate &&
  444. (num_dirty_pages_period * TARGET_PAGE_SIZE >
  445. (bytes_xfer_now - bytes_xfer_prev)/2) &&
  446. (dirty_rate_high_cnt++ > 4)) {
  447. trace_migration_throttle();
  448. mig_throttle_on = true;
  449. dirty_rate_high_cnt = 0;
  450. }
  451. bytes_xfer_prev = bytes_xfer_now;
  452. } else {
  453. mig_throttle_on = false;
  454. }
  455. s->dirty_pages_rate = num_dirty_pages_period * 1000
  456. / (end_time - start_time);
  457. s->dirty_bytes_rate = s->dirty_pages_rate * TARGET_PAGE_SIZE;
  458. start_time = end_time;
  459. num_dirty_pages_period = 0;
  460. }
  461. }
  462. /*
  463. * ram_save_block: Writes a page of memory to the stream f
  464. *
  465. * Returns: The number of bytes written.
  466. * 0 means no dirty pages
  467. */
  468. static int ram_save_block(QEMUFile *f, bool last_stage)
  469. {
  470. RAMBlock *block = last_seen_block;
  471. ram_addr_t offset = last_offset;
  472. bool complete_round = false;
  473. int bytes_sent = 0;
  474. MemoryRegion *mr;
  475. ram_addr_t current_addr;
  476. if (!block)
  477. block = QTAILQ_FIRST(&ram_list.blocks);
  478. while (true) {
  479. mr = block->mr;
  480. offset = migration_bitmap_find_and_reset_dirty(mr, offset);
  481. if (complete_round && block == last_seen_block &&
  482. offset >= last_offset) {
  483. break;
  484. }
  485. if (offset >= block->length) {
  486. offset = 0;
  487. block = QTAILQ_NEXT(block, next);
  488. if (!block) {
  489. block = QTAILQ_FIRST(&ram_list.blocks);
  490. complete_round = true;
  491. ram_bulk_stage = false;
  492. }
  493. } else {
  494. int ret;
  495. uint8_t *p;
  496. bool send_async = true;
  497. int cont = (block == last_sent_block) ?
  498. RAM_SAVE_FLAG_CONTINUE : 0;
  499. p = memory_region_get_ram_ptr(mr) + offset;
  500. /* In doubt sent page as normal */
  501. bytes_sent = -1;
  502. ret = ram_control_save_page(f, block->offset,
  503. offset, TARGET_PAGE_SIZE, &bytes_sent);
  504. XBZRLE_cache_lock();
  505. current_addr = block->offset + offset;
  506. if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
  507. if (ret != RAM_SAVE_CONTROL_DELAYED) {
  508. if (bytes_sent > 0) {
  509. acct_info.norm_pages++;
  510. } else if (bytes_sent == 0) {
  511. acct_info.dup_pages++;
  512. }
  513. }
  514. } else if (is_zero_range(p, TARGET_PAGE_SIZE)) {
  515. acct_info.dup_pages++;
  516. bytes_sent = save_block_hdr(f, block, offset, cont,
  517. RAM_SAVE_FLAG_COMPRESS);
  518. qemu_put_byte(f, 0);
  519. bytes_sent++;
  520. /* Must let xbzrle know, otherwise a previous (now 0'd) cached
  521. * page would be stale
  522. */
  523. xbzrle_cache_zero_page(current_addr);
  524. } else if (!ram_bulk_stage && migrate_use_xbzrle()) {
  525. bytes_sent = save_xbzrle_page(f, p, current_addr, block,
  526. offset, cont, last_stage);
  527. if (!last_stage) {
  528. /* We must send exactly what's in the xbzrle cache
  529. * even if the page wasn't xbzrle compressed, so that
  530. * it's right next time.
  531. */
  532. p = get_cached_data(XBZRLE.cache, current_addr);
  533. /* Can't send this cached data async, since the cache page
  534. * might get updated before it gets to the wire
  535. */
  536. send_async = false;
  537. }
  538. }
  539. /* XBZRLE overflow or normal page */
  540. if (bytes_sent == -1) {
  541. bytes_sent = save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
  542. if (send_async) {
  543. qemu_put_buffer_async(f, p, TARGET_PAGE_SIZE);
  544. } else {
  545. qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
  546. }
  547. bytes_sent += TARGET_PAGE_SIZE;
  548. acct_info.norm_pages++;
  549. }
  550. XBZRLE_cache_unlock();
  551. /* if page is unmodified, continue to the next */
  552. if (bytes_sent > 0) {
  553. last_sent_block = block;
  554. break;
  555. }
  556. }
  557. }
  558. last_seen_block = block;
  559. last_offset = offset;
  560. return bytes_sent;
  561. }
  562. static uint64_t bytes_transferred;
  563. void acct_update_position(QEMUFile *f, size_t size, bool zero)
  564. {
  565. uint64_t pages = size / TARGET_PAGE_SIZE;
  566. if (zero) {
  567. acct_info.dup_pages += pages;
  568. } else {
  569. acct_info.norm_pages += pages;
  570. bytes_transferred += size;
  571. qemu_update_position(f, size);
  572. }
  573. }
  574. static ram_addr_t ram_save_remaining(void)
  575. {
  576. return migration_dirty_pages;
  577. }
  578. uint64_t ram_bytes_remaining(void)
  579. {
  580. return ram_save_remaining() * TARGET_PAGE_SIZE;
  581. }
  582. uint64_t ram_bytes_transferred(void)
  583. {
  584. return bytes_transferred;
  585. }
  586. uint64_t ram_bytes_total(void)
  587. {
  588. RAMBlock *block;
  589. uint64_t total = 0;
  590. QTAILQ_FOREACH(block, &ram_list.blocks, next)
  591. total += block->length;
  592. return total;
  593. }
  594. void free_xbzrle_decoded_buf(void)
  595. {
  596. g_free(xbzrle_decoded_buf);
  597. xbzrle_decoded_buf = NULL;
  598. }
  599. static void migration_end(void)
  600. {
  601. if (migration_bitmap) {
  602. memory_global_dirty_log_stop();
  603. g_free(migration_bitmap);
  604. migration_bitmap = NULL;
  605. }
  606. XBZRLE_cache_lock();
  607. if (XBZRLE.cache) {
  608. cache_fini(XBZRLE.cache);
  609. g_free(XBZRLE.cache);
  610. g_free(XBZRLE.encoded_buf);
  611. g_free(XBZRLE.current_buf);
  612. XBZRLE.cache = NULL;
  613. XBZRLE.encoded_buf = NULL;
  614. XBZRLE.current_buf = NULL;
  615. }
  616. XBZRLE_cache_unlock();
  617. }
  618. static void ram_migration_cancel(void *opaque)
  619. {
  620. migration_end();
  621. }
  622. static void reset_ram_globals(void)
  623. {
  624. last_seen_block = NULL;
  625. last_sent_block = NULL;
  626. last_offset = 0;
  627. last_version = ram_list.version;
  628. ram_bulk_stage = true;
  629. }
  630. #define MAX_WAIT 50 /* ms, half buffered_file limit */
  631. static int ram_save_setup(QEMUFile *f, void *opaque)
  632. {
  633. RAMBlock *block;
  634. int64_t ram_pages = last_ram_offset() >> TARGET_PAGE_BITS;
  635. migration_bitmap = bitmap_new(ram_pages);
  636. bitmap_set(migration_bitmap, 0, ram_pages);
  637. migration_dirty_pages = ram_pages;
  638. mig_throttle_on = false;
  639. dirty_rate_high_cnt = 0;
  640. if (migrate_use_xbzrle()) {
  641. qemu_mutex_lock_iothread();
  642. XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
  643. TARGET_PAGE_SIZE,
  644. TARGET_PAGE_SIZE);
  645. if (!XBZRLE.cache) {
  646. qemu_mutex_unlock_iothread();
  647. DPRINTF("Error creating cache\n");
  648. return -1;
  649. }
  650. qemu_mutex_init(&XBZRLE.lock);
  651. qemu_mutex_unlock_iothread();
  652. /* We prefer not to abort if there is no memory */
  653. XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
  654. if (!XBZRLE.encoded_buf) {
  655. DPRINTF("Error allocating encoded_buf\n");
  656. return -1;
  657. }
  658. XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
  659. if (!XBZRLE.current_buf) {
  660. DPRINTF("Error allocating current_buf\n");
  661. g_free(XBZRLE.encoded_buf);
  662. XBZRLE.encoded_buf = NULL;
  663. return -1;
  664. }
  665. acct_clear();
  666. }
  667. qemu_mutex_lock_iothread();
  668. qemu_mutex_lock_ramlist();
  669. bytes_transferred = 0;
  670. reset_ram_globals();
  671. memory_global_dirty_log_start();
  672. migration_bitmap_sync();
  673. qemu_mutex_unlock_iothread();
  674. qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
  675. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  676. qemu_put_byte(f, strlen(block->idstr));
  677. qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
  678. qemu_put_be64(f, block->length);
  679. }
  680. qemu_mutex_unlock_ramlist();
  681. ram_control_before_iterate(f, RAM_CONTROL_SETUP);
  682. ram_control_after_iterate(f, RAM_CONTROL_SETUP);
  683. qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
  684. return 0;
  685. }
  686. static int ram_save_iterate(QEMUFile *f, void *opaque)
  687. {
  688. int ret;
  689. int i;
  690. int64_t t0;
  691. int total_sent = 0;
  692. qemu_mutex_lock_ramlist();
  693. if (ram_list.version != last_version) {
  694. reset_ram_globals();
  695. }
  696. ram_control_before_iterate(f, RAM_CONTROL_ROUND);
  697. t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  698. i = 0;
  699. while ((ret = qemu_file_rate_limit(f)) == 0) {
  700. int bytes_sent;
  701. bytes_sent = ram_save_block(f, false);
  702. /* no more blocks to sent */
  703. if (bytes_sent == 0) {
  704. break;
  705. }
  706. total_sent += bytes_sent;
  707. acct_info.iterations++;
  708. check_guest_throttling();
  709. /* we want to check in the 1st loop, just in case it was the 1st time
  710. and we had to sync the dirty bitmap.
  711. qemu_get_clock_ns() is a bit expensive, so we only check each some
  712. iterations
  713. */
  714. if ((i & 63) == 0) {
  715. uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
  716. if (t1 > MAX_WAIT) {
  717. DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
  718. t1, i);
  719. break;
  720. }
  721. }
  722. i++;
  723. }
  724. qemu_mutex_unlock_ramlist();
  725. /*
  726. * Must occur before EOS (or any QEMUFile operation)
  727. * because of RDMA protocol.
  728. */
  729. ram_control_after_iterate(f, RAM_CONTROL_ROUND);
  730. bytes_transferred += total_sent;
  731. /*
  732. * Do not count these 8 bytes into total_sent, so that we can
  733. * return 0 if no page had been dirtied.
  734. */
  735. qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
  736. bytes_transferred += 8;
  737. ret = qemu_file_get_error(f);
  738. if (ret < 0) {
  739. return ret;
  740. }
  741. return total_sent;
  742. }
  743. static int ram_save_complete(QEMUFile *f, void *opaque)
  744. {
  745. qemu_mutex_lock_ramlist();
  746. migration_bitmap_sync();
  747. ram_control_before_iterate(f, RAM_CONTROL_FINISH);
  748. /* try transferring iterative blocks of memory */
  749. /* flush all remaining blocks regardless of rate limiting */
  750. while (true) {
  751. int bytes_sent;
  752. bytes_sent = ram_save_block(f, true);
  753. /* no more blocks to sent */
  754. if (bytes_sent == 0) {
  755. break;
  756. }
  757. bytes_transferred += bytes_sent;
  758. }
  759. ram_control_after_iterate(f, RAM_CONTROL_FINISH);
  760. migration_end();
  761. qemu_mutex_unlock_ramlist();
  762. qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
  763. return 0;
  764. }
  765. static uint64_t ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
  766. {
  767. uint64_t remaining_size;
  768. remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
  769. if (remaining_size < max_size) {
  770. qemu_mutex_lock_iothread();
  771. migration_bitmap_sync();
  772. qemu_mutex_unlock_iothread();
  773. remaining_size = ram_save_remaining() * TARGET_PAGE_SIZE;
  774. }
  775. return remaining_size;
  776. }
  777. static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
  778. {
  779. int ret, rc = 0;
  780. unsigned int xh_len;
  781. int xh_flags;
  782. if (!xbzrle_decoded_buf) {
  783. xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
  784. }
  785. /* extract RLE header */
  786. xh_flags = qemu_get_byte(f);
  787. xh_len = qemu_get_be16(f);
  788. if (xh_flags != ENCODING_FLAG_XBZRLE) {
  789. fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
  790. return -1;
  791. }
  792. if (xh_len > TARGET_PAGE_SIZE) {
  793. fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
  794. return -1;
  795. }
  796. /* load data and decode */
  797. qemu_get_buffer(f, xbzrle_decoded_buf, xh_len);
  798. /* decode RLE */
  799. ret = xbzrle_decode_buffer(xbzrle_decoded_buf, xh_len, host,
  800. TARGET_PAGE_SIZE);
  801. if (ret == -1) {
  802. fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
  803. rc = -1;
  804. } else if (ret > TARGET_PAGE_SIZE) {
  805. fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
  806. ret, TARGET_PAGE_SIZE);
  807. abort();
  808. }
  809. return rc;
  810. }
  811. static inline void *host_from_stream_offset(QEMUFile *f,
  812. ram_addr_t offset,
  813. int flags)
  814. {
  815. static RAMBlock *block = NULL;
  816. char id[256];
  817. uint8_t len;
  818. if (flags & RAM_SAVE_FLAG_CONTINUE) {
  819. if (!block) {
  820. fprintf(stderr, "Ack, bad migration stream!\n");
  821. return NULL;
  822. }
  823. return memory_region_get_ram_ptr(block->mr) + offset;
  824. }
  825. len = qemu_get_byte(f);
  826. qemu_get_buffer(f, (uint8_t *)id, len);
  827. id[len] = 0;
  828. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  829. if (!strncmp(id, block->idstr, sizeof(id)))
  830. return memory_region_get_ram_ptr(block->mr) + offset;
  831. }
  832. fprintf(stderr, "Can't find block %s!\n", id);
  833. return NULL;
  834. }
  835. /*
  836. * If a page (or a whole RDMA chunk) has been
  837. * determined to be zero, then zap it.
  838. */
  839. void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
  840. {
  841. if (ch != 0 || !is_zero_range(host, size)) {
  842. memset(host, ch, size);
  843. }
  844. }
  845. static int ram_load(QEMUFile *f, void *opaque, int version_id)
  846. {
  847. ram_addr_t addr;
  848. int flags, ret = 0;
  849. static uint64_t seq_iter;
  850. seq_iter++;
  851. if (version_id != 4) {
  852. ret = -EINVAL;
  853. }
  854. while (!ret) {
  855. addr = qemu_get_be64(f);
  856. flags = addr & ~TARGET_PAGE_MASK;
  857. addr &= TARGET_PAGE_MASK;
  858. if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
  859. /* Synchronize RAM block list */
  860. char id[256];
  861. ram_addr_t length;
  862. ram_addr_t total_ram_bytes = addr;
  863. while (total_ram_bytes) {
  864. RAMBlock *block;
  865. uint8_t len;
  866. len = qemu_get_byte(f);
  867. qemu_get_buffer(f, (uint8_t *)id, len);
  868. id[len] = 0;
  869. length = qemu_get_be64(f);
  870. QTAILQ_FOREACH(block, &ram_list.blocks, next) {
  871. if (!strncmp(id, block->idstr, sizeof(id))) {
  872. if (block->length != length) {
  873. fprintf(stderr,
  874. "Length mismatch: %s: " RAM_ADDR_FMT
  875. " in != " RAM_ADDR_FMT "\n", id, length,
  876. block->length);
  877. ret = -EINVAL;
  878. }
  879. break;
  880. }
  881. }
  882. if (!block) {
  883. fprintf(stderr, "Unknown ramblock \"%s\", cannot "
  884. "accept migration\n", id);
  885. ret = -EINVAL;
  886. }
  887. if (ret) {
  888. break;
  889. }
  890. total_ram_bytes -= length;
  891. }
  892. } else if (flags & RAM_SAVE_FLAG_COMPRESS) {
  893. void *host;
  894. uint8_t ch;
  895. host = host_from_stream_offset(f, addr, flags);
  896. if (!host) {
  897. error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
  898. ret = -EINVAL;
  899. break;
  900. }
  901. ch = qemu_get_byte(f);
  902. ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
  903. } else if (flags & RAM_SAVE_FLAG_PAGE) {
  904. void *host;
  905. host = host_from_stream_offset(f, addr, flags);
  906. if (!host) {
  907. error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
  908. ret = -EINVAL;
  909. break;
  910. }
  911. qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
  912. } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
  913. void *host = host_from_stream_offset(f, addr, flags);
  914. if (!host) {
  915. error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
  916. ret = -EINVAL;
  917. break;
  918. }
  919. if (load_xbzrle(f, addr, host) < 0) {
  920. error_report("Failed to decompress XBZRLE page at "
  921. RAM_ADDR_FMT, addr);
  922. ret = -EINVAL;
  923. break;
  924. }
  925. } else if (flags & RAM_SAVE_FLAG_HOOK) {
  926. ram_control_load_hook(f, flags);
  927. } else if (flags & RAM_SAVE_FLAG_EOS) {
  928. /* normal exit */
  929. break;
  930. } else {
  931. error_report("Unknown migration flags: %#x", flags);
  932. ret = -EINVAL;
  933. break;
  934. }
  935. ret = qemu_file_get_error(f);
  936. }
  937. DPRINTF("Completed load of VM with exit code %d seq iteration "
  938. "%" PRIu64 "\n", ret, seq_iter);
  939. return ret;
  940. }
  941. SaveVMHandlers savevm_ram_handlers = {
  942. .save_live_setup = ram_save_setup,
  943. .save_live_iterate = ram_save_iterate,
  944. .save_live_complete = ram_save_complete,
  945. .save_live_pending = ram_save_pending,
  946. .load_state = ram_load,
  947. .cancel = ram_migration_cancel,
  948. };
  949. struct soundhw {
  950. const char *name;
  951. const char *descr;
  952. int enabled;
  953. int isa;
  954. union {
  955. int (*init_isa) (ISABus *bus);
  956. int (*init_pci) (PCIBus *bus);
  957. } init;
  958. };
  959. static struct soundhw soundhw[9];
  960. static int soundhw_count;
  961. void isa_register_soundhw(const char *name, const char *descr,
  962. int (*init_isa)(ISABus *bus))
  963. {
  964. assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
  965. soundhw[soundhw_count].name = name;
  966. soundhw[soundhw_count].descr = descr;
  967. soundhw[soundhw_count].isa = 1;
  968. soundhw[soundhw_count].init.init_isa = init_isa;
  969. soundhw_count++;
  970. }
  971. void pci_register_soundhw(const char *name, const char *descr,
  972. int (*init_pci)(PCIBus *bus))
  973. {
  974. assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
  975. soundhw[soundhw_count].name = name;
  976. soundhw[soundhw_count].descr = descr;
  977. soundhw[soundhw_count].isa = 0;
  978. soundhw[soundhw_count].init.init_pci = init_pci;
  979. soundhw_count++;
  980. }
  981. void select_soundhw(const char *optarg)
  982. {
  983. struct soundhw *c;
  984. if (is_help_option(optarg)) {
  985. show_valid_cards:
  986. if (soundhw_count) {
  987. printf("Valid sound card names (comma separated):\n");
  988. for (c = soundhw; c->name; ++c) {
  989. printf ("%-11s %s\n", c->name, c->descr);
  990. }
  991. printf("\n-soundhw all will enable all of the above\n");
  992. } else {
  993. printf("Machine has no user-selectable audio hardware "
  994. "(it may or may not have always-present audio hardware).\n");
  995. }
  996. exit(!is_help_option(optarg));
  997. }
  998. else {
  999. size_t l;
  1000. const char *p;
  1001. char *e;
  1002. int bad_card = 0;
  1003. if (!strcmp(optarg, "all")) {
  1004. for (c = soundhw; c->name; ++c) {
  1005. c->enabled = 1;
  1006. }
  1007. return;
  1008. }
  1009. p = optarg;
  1010. while (*p) {
  1011. e = strchr(p, ',');
  1012. l = !e ? strlen(p) : (size_t) (e - p);
  1013. for (c = soundhw; c->name; ++c) {
  1014. if (!strncmp(c->name, p, l) && !c->name[l]) {
  1015. c->enabled = 1;
  1016. break;
  1017. }
  1018. }
  1019. if (!c->name) {
  1020. if (l > 80) {
  1021. fprintf(stderr,
  1022. "Unknown sound card name (too big to show)\n");
  1023. }
  1024. else {
  1025. fprintf(stderr, "Unknown sound card name `%.*s'\n",
  1026. (int) l, p);
  1027. }
  1028. bad_card = 1;
  1029. }
  1030. p += l + (e != NULL);
  1031. }
  1032. if (bad_card) {
  1033. goto show_valid_cards;
  1034. }
  1035. }
  1036. }
  1037. void audio_init(void)
  1038. {
  1039. struct soundhw *c;
  1040. ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
  1041. PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
  1042. for (c = soundhw; c->name; ++c) {
  1043. if (c->enabled) {
  1044. if (c->isa) {
  1045. if (!isa_bus) {
  1046. fprintf(stderr, "ISA bus not available for %s\n", c->name);
  1047. exit(1);
  1048. }
  1049. c->init.init_isa(isa_bus);
  1050. } else {
  1051. if (!pci_bus) {
  1052. fprintf(stderr, "PCI bus not available for %s\n", c->name);
  1053. exit(1);
  1054. }
  1055. c->init.init_pci(pci_bus);
  1056. }
  1057. }
  1058. }
  1059. }
  1060. int qemu_uuid_parse(const char *str, uint8_t *uuid)
  1061. {
  1062. int ret;
  1063. if (strlen(str) != 36) {
  1064. return -1;
  1065. }
  1066. ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
  1067. &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
  1068. &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
  1069. &uuid[15]);
  1070. if (ret != 16) {
  1071. return -1;
  1072. }
  1073. return 0;
  1074. }
  1075. void do_acpitable_option(const QemuOpts *opts)
  1076. {
  1077. #ifdef TARGET_I386
  1078. Error *err = NULL;
  1079. acpi_table_add(opts, &err);
  1080. if (err) {
  1081. error_report("Wrong acpi table provided: %s",
  1082. error_get_pretty(err));
  1083. error_free(err);
  1084. exit(1);
  1085. }
  1086. #endif
  1087. }
  1088. void do_smbios_option(QemuOpts *opts)
  1089. {
  1090. #ifdef TARGET_I386
  1091. smbios_entry_add(opts);
  1092. #endif
  1093. }
  1094. void cpudef_init(void)
  1095. {
  1096. #if defined(cpudef_setup)
  1097. cpudef_setup(); /* parse cpu definitions in target config file */
  1098. #endif
  1099. }
  1100. int tcg_available(void)
  1101. {
  1102. return 1;
  1103. }
  1104. int kvm_available(void)
  1105. {
  1106. #ifdef CONFIG_KVM
  1107. return 1;
  1108. #else
  1109. return 0;
  1110. #endif
  1111. }
  1112. int xen_available(void)
  1113. {
  1114. #ifdef CONFIG_XEN
  1115. return 1;
  1116. #else
  1117. return 0;
  1118. #endif
  1119. }
  1120. TargetInfo *qmp_query_target(Error **errp)
  1121. {
  1122. TargetInfo *info = g_malloc0(sizeof(*info));
  1123. info->arch = g_strdup(TARGET_NAME);
  1124. return info;
  1125. }
  1126. /* Stub function that's gets run on the vcpu when its brought out of the
  1127. VM to run inside qemu via async_run_on_cpu()*/
  1128. static void mig_sleep_cpu(void *opq)
  1129. {
  1130. qemu_mutex_unlock_iothread();
  1131. g_usleep(30*1000);
  1132. qemu_mutex_lock_iothread();
  1133. }
  1134. /* To reduce the dirty rate explicitly disallow the VCPUs from spending
  1135. much time in the VM. The migration thread will try to catchup.
  1136. Workload will experience a performance drop.
  1137. */
  1138. static void mig_throttle_guest_down(void)
  1139. {
  1140. CPUState *cpu;
  1141. qemu_mutex_lock_iothread();
  1142. CPU_FOREACH(cpu) {
  1143. async_run_on_cpu(cpu, mig_sleep_cpu, NULL);
  1144. }
  1145. qemu_mutex_unlock_iothread();
  1146. }
  1147. static void check_guest_throttling(void)
  1148. {
  1149. static int64_t t0;
  1150. int64_t t1;
  1151. if (!mig_throttle_on) {
  1152. return;
  1153. }
  1154. if (!t0) {
  1155. t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  1156. return;
  1157. }
  1158. t1 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  1159. /* If it has been more than 40 ms since the last time the guest
  1160. * was throttled then do it again.
  1161. */
  1162. if (40 < (t1-t0)/1000000) {
  1163. mig_throttle_guest_down();
  1164. t0 = t1;
  1165. }
  1166. }