2
0

arch_init.c 39 KB

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