You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpus.c 68KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  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 "qemu/osdep.h"
  25. #include "qemu-common.h"
  26. #include "qemu/config-file.h"
  27. #include "cpu.h"
  28. #include "monitor/monitor.h"
  29. #include "qapi/error.h"
  30. #include "qapi/qapi-commands-misc.h"
  31. #include "qapi/qapi-events-run-state.h"
  32. #include "qapi/qmp/qerror.h"
  33. #include "qemu/error-report.h"
  34. #include "qemu/qemu-print.h"
  35. #include "sysemu/sysemu.h"
  36. #include "sysemu/tcg.h"
  37. #include "sysemu/block-backend.h"
  38. #include "exec/gdbstub.h"
  39. #include "sysemu/dma.h"
  40. #include "sysemu/hw_accel.h"
  41. #include "sysemu/kvm.h"
  42. #include "sysemu/hax.h"
  43. #include "sysemu/hvf.h"
  44. #include "sysemu/whpx.h"
  45. #include "exec/exec-all.h"
  46. #include "qemu/thread.h"
  47. #include "sysemu/cpus.h"
  48. #include "sysemu/qtest.h"
  49. #include "qemu/main-loop.h"
  50. #include "qemu/option.h"
  51. #include "qemu/bitmap.h"
  52. #include "qemu/seqlock.h"
  53. #include "qemu/guest-random.h"
  54. #include "tcg.h"
  55. #include "hw/nmi.h"
  56. #include "sysemu/replay.h"
  57. #include "hw/boards.h"
  58. #ifdef CONFIG_LINUX
  59. #include <sys/prctl.h>
  60. #ifndef PR_MCE_KILL
  61. #define PR_MCE_KILL 33
  62. #endif
  63. #ifndef PR_MCE_KILL_SET
  64. #define PR_MCE_KILL_SET 1
  65. #endif
  66. #ifndef PR_MCE_KILL_EARLY
  67. #define PR_MCE_KILL_EARLY 1
  68. #endif
  69. #endif /* CONFIG_LINUX */
  70. int64_t max_delay;
  71. int64_t max_advance;
  72. /* vcpu throttling controls */
  73. static QEMUTimer *throttle_timer;
  74. static unsigned int throttle_percentage;
  75. #define CPU_THROTTLE_PCT_MIN 1
  76. #define CPU_THROTTLE_PCT_MAX 99
  77. #define CPU_THROTTLE_TIMESLICE_NS 10000000
  78. bool cpu_is_stopped(CPUState *cpu)
  79. {
  80. return cpu->stopped || !runstate_is_running();
  81. }
  82. static bool cpu_thread_is_idle(CPUState *cpu)
  83. {
  84. if (cpu->stop || cpu->queued_work_first) {
  85. return false;
  86. }
  87. if (cpu_is_stopped(cpu)) {
  88. return true;
  89. }
  90. if (!cpu->halted || cpu_has_work(cpu) ||
  91. kvm_halt_in_kernel()) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. static bool all_cpu_threads_idle(void)
  97. {
  98. CPUState *cpu;
  99. CPU_FOREACH(cpu) {
  100. if (!cpu_thread_is_idle(cpu)) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. /***********************************************************/
  107. /* guest cycle counter */
  108. /* Protected by TimersState seqlock */
  109. static bool icount_sleep = true;
  110. /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
  111. #define MAX_ICOUNT_SHIFT 10
  112. typedef struct TimersState {
  113. /* Protected by BQL. */
  114. int64_t cpu_ticks_prev;
  115. int64_t cpu_ticks_offset;
  116. /* Protect fields that can be respectively read outside the
  117. * BQL, and written from multiple threads.
  118. */
  119. QemuSeqLock vm_clock_seqlock;
  120. QemuSpin vm_clock_lock;
  121. int16_t cpu_ticks_enabled;
  122. /* Conversion factor from emulated instructions to virtual clock ticks. */
  123. int16_t icount_time_shift;
  124. /* Compensate for varying guest execution speed. */
  125. int64_t qemu_icount_bias;
  126. int64_t vm_clock_warp_start;
  127. int64_t cpu_clock_offset;
  128. /* Only written by TCG thread */
  129. int64_t qemu_icount;
  130. /* for adjusting icount */
  131. QEMUTimer *icount_rt_timer;
  132. QEMUTimer *icount_vm_timer;
  133. QEMUTimer *icount_warp_timer;
  134. } TimersState;
  135. static TimersState timers_state;
  136. bool mttcg_enabled;
  137. /*
  138. * We default to false if we know other options have been enabled
  139. * which are currently incompatible with MTTCG. Otherwise when each
  140. * guest (target) has been updated to support:
  141. * - atomic instructions
  142. * - memory ordering primitives (barriers)
  143. * they can set the appropriate CONFIG flags in ${target}-softmmu.mak
  144. *
  145. * Once a guest architecture has been converted to the new primitives
  146. * there are two remaining limitations to check.
  147. *
  148. * - The guest can't be oversized (e.g. 64 bit guest on 32 bit host)
  149. * - The host must have a stronger memory order than the guest
  150. *
  151. * It may be possible in future to support strong guests on weak hosts
  152. * but that will require tagging all load/stores in a guest with their
  153. * implicit memory order requirements which would likely slow things
  154. * down a lot.
  155. */
  156. static bool check_tcg_memory_orders_compatible(void)
  157. {
  158. #if defined(TCG_GUEST_DEFAULT_MO) && defined(TCG_TARGET_DEFAULT_MO)
  159. return (TCG_GUEST_DEFAULT_MO & ~TCG_TARGET_DEFAULT_MO) == 0;
  160. #else
  161. return false;
  162. #endif
  163. }
  164. static bool default_mttcg_enabled(void)
  165. {
  166. if (use_icount || TCG_OVERSIZED_GUEST) {
  167. return false;
  168. } else {
  169. #ifdef TARGET_SUPPORTS_MTTCG
  170. return check_tcg_memory_orders_compatible();
  171. #else
  172. return false;
  173. #endif
  174. }
  175. }
  176. void qemu_tcg_configure(QemuOpts *opts, Error **errp)
  177. {
  178. const char *t = qemu_opt_get(opts, "thread");
  179. if (t) {
  180. if (strcmp(t, "multi") == 0) {
  181. if (TCG_OVERSIZED_GUEST) {
  182. error_setg(errp, "No MTTCG when guest word size > hosts");
  183. } else if (use_icount) {
  184. error_setg(errp, "No MTTCG when icount is enabled");
  185. } else {
  186. #ifndef TARGET_SUPPORTS_MTTCG
  187. warn_report("Guest not yet converted to MTTCG - "
  188. "you may get unexpected results");
  189. #endif
  190. if (!check_tcg_memory_orders_compatible()) {
  191. warn_report("Guest expects a stronger memory ordering "
  192. "than the host provides");
  193. error_printf("This may cause strange/hard to debug errors\n");
  194. }
  195. mttcg_enabled = true;
  196. }
  197. } else if (strcmp(t, "single") == 0) {
  198. mttcg_enabled = false;
  199. } else {
  200. error_setg(errp, "Invalid 'thread' setting %s", t);
  201. }
  202. } else {
  203. mttcg_enabled = default_mttcg_enabled();
  204. }
  205. }
  206. /* The current number of executed instructions is based on what we
  207. * originally budgeted minus the current state of the decrementing
  208. * icount counters in extra/u16.low.
  209. */
  210. static int64_t cpu_get_icount_executed(CPUState *cpu)
  211. {
  212. return (cpu->icount_budget -
  213. (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
  214. }
  215. /*
  216. * Update the global shared timer_state.qemu_icount to take into
  217. * account executed instructions. This is done by the TCG vCPU
  218. * thread so the main-loop can see time has moved forward.
  219. */
  220. static void cpu_update_icount_locked(CPUState *cpu)
  221. {
  222. int64_t executed = cpu_get_icount_executed(cpu);
  223. cpu->icount_budget -= executed;
  224. atomic_set_i64(&timers_state.qemu_icount,
  225. timers_state.qemu_icount + executed);
  226. }
  227. /*
  228. * Update the global shared timer_state.qemu_icount to take into
  229. * account executed instructions. This is done by the TCG vCPU
  230. * thread so the main-loop can see time has moved forward.
  231. */
  232. void cpu_update_icount(CPUState *cpu)
  233. {
  234. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  235. &timers_state.vm_clock_lock);
  236. cpu_update_icount_locked(cpu);
  237. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  238. &timers_state.vm_clock_lock);
  239. }
  240. static int64_t cpu_get_icount_raw_locked(void)
  241. {
  242. CPUState *cpu = current_cpu;
  243. if (cpu && cpu->running) {
  244. if (!cpu->can_do_io) {
  245. error_report("Bad icount read");
  246. exit(1);
  247. }
  248. /* Take into account what has run */
  249. cpu_update_icount_locked(cpu);
  250. }
  251. /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
  252. return atomic_read_i64(&timers_state.qemu_icount);
  253. }
  254. static int64_t cpu_get_icount_locked(void)
  255. {
  256. int64_t icount = cpu_get_icount_raw_locked();
  257. return atomic_read_i64(&timers_state.qemu_icount_bias) +
  258. cpu_icount_to_ns(icount);
  259. }
  260. int64_t cpu_get_icount_raw(void)
  261. {
  262. int64_t icount;
  263. unsigned start;
  264. do {
  265. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  266. icount = cpu_get_icount_raw_locked();
  267. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  268. return icount;
  269. }
  270. /* Return the virtual CPU time, based on the instruction counter. */
  271. int64_t cpu_get_icount(void)
  272. {
  273. int64_t icount;
  274. unsigned start;
  275. do {
  276. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  277. icount = cpu_get_icount_locked();
  278. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  279. return icount;
  280. }
  281. int64_t cpu_icount_to_ns(int64_t icount)
  282. {
  283. return icount << atomic_read(&timers_state.icount_time_shift);
  284. }
  285. static int64_t cpu_get_ticks_locked(void)
  286. {
  287. int64_t ticks = timers_state.cpu_ticks_offset;
  288. if (timers_state.cpu_ticks_enabled) {
  289. ticks += cpu_get_host_ticks();
  290. }
  291. if (timers_state.cpu_ticks_prev > ticks) {
  292. /* Non increasing ticks may happen if the host uses software suspend. */
  293. timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
  294. ticks = timers_state.cpu_ticks_prev;
  295. }
  296. timers_state.cpu_ticks_prev = ticks;
  297. return ticks;
  298. }
  299. /* return the time elapsed in VM between vm_start and vm_stop. Unless
  300. * icount is active, cpu_get_ticks() uses units of the host CPU cycle
  301. * counter.
  302. */
  303. int64_t cpu_get_ticks(void)
  304. {
  305. int64_t ticks;
  306. if (use_icount) {
  307. return cpu_get_icount();
  308. }
  309. qemu_spin_lock(&timers_state.vm_clock_lock);
  310. ticks = cpu_get_ticks_locked();
  311. qemu_spin_unlock(&timers_state.vm_clock_lock);
  312. return ticks;
  313. }
  314. static int64_t cpu_get_clock_locked(void)
  315. {
  316. int64_t time;
  317. time = timers_state.cpu_clock_offset;
  318. if (timers_state.cpu_ticks_enabled) {
  319. time += get_clock();
  320. }
  321. return time;
  322. }
  323. /* Return the monotonic time elapsed in VM, i.e.,
  324. * the time between vm_start and vm_stop
  325. */
  326. int64_t cpu_get_clock(void)
  327. {
  328. int64_t ti;
  329. unsigned start;
  330. do {
  331. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  332. ti = cpu_get_clock_locked();
  333. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  334. return ti;
  335. }
  336. /* enable cpu_get_ticks()
  337. * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
  338. */
  339. void cpu_enable_ticks(void)
  340. {
  341. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  342. &timers_state.vm_clock_lock);
  343. if (!timers_state.cpu_ticks_enabled) {
  344. timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
  345. timers_state.cpu_clock_offset -= get_clock();
  346. timers_state.cpu_ticks_enabled = 1;
  347. }
  348. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  349. &timers_state.vm_clock_lock);
  350. }
  351. /* disable cpu_get_ticks() : the clock is stopped. You must not call
  352. * cpu_get_ticks() after that.
  353. * Caller must hold BQL which serves as mutex for vm_clock_seqlock.
  354. */
  355. void cpu_disable_ticks(void)
  356. {
  357. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  358. &timers_state.vm_clock_lock);
  359. if (timers_state.cpu_ticks_enabled) {
  360. timers_state.cpu_ticks_offset += cpu_get_host_ticks();
  361. timers_state.cpu_clock_offset = cpu_get_clock_locked();
  362. timers_state.cpu_ticks_enabled = 0;
  363. }
  364. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  365. &timers_state.vm_clock_lock);
  366. }
  367. /* Correlation between real and virtual time is always going to be
  368. fairly approximate, so ignore small variation.
  369. When the guest is idle real and virtual time will be aligned in
  370. the IO wait loop. */
  371. #define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
  372. static void icount_adjust(void)
  373. {
  374. int64_t cur_time;
  375. int64_t cur_icount;
  376. int64_t delta;
  377. /* Protected by TimersState mutex. */
  378. static int64_t last_delta;
  379. /* If the VM is not running, then do nothing. */
  380. if (!runstate_is_running()) {
  381. return;
  382. }
  383. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  384. &timers_state.vm_clock_lock);
  385. cur_time = cpu_get_clock_locked();
  386. cur_icount = cpu_get_icount_locked();
  387. delta = cur_icount - cur_time;
  388. /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
  389. if (delta > 0
  390. && last_delta + ICOUNT_WOBBLE < delta * 2
  391. && timers_state.icount_time_shift > 0) {
  392. /* The guest is getting too far ahead. Slow time down. */
  393. atomic_set(&timers_state.icount_time_shift,
  394. timers_state.icount_time_shift - 1);
  395. }
  396. if (delta < 0
  397. && last_delta - ICOUNT_WOBBLE > delta * 2
  398. && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
  399. /* The guest is getting too far behind. Speed time up. */
  400. atomic_set(&timers_state.icount_time_shift,
  401. timers_state.icount_time_shift + 1);
  402. }
  403. last_delta = delta;
  404. atomic_set_i64(&timers_state.qemu_icount_bias,
  405. cur_icount - (timers_state.qemu_icount
  406. << timers_state.icount_time_shift));
  407. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  408. &timers_state.vm_clock_lock);
  409. }
  410. static void icount_adjust_rt(void *opaque)
  411. {
  412. timer_mod(timers_state.icount_rt_timer,
  413. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
  414. icount_adjust();
  415. }
  416. static void icount_adjust_vm(void *opaque)
  417. {
  418. timer_mod(timers_state.icount_vm_timer,
  419. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  420. NANOSECONDS_PER_SECOND / 10);
  421. icount_adjust();
  422. }
  423. static int64_t qemu_icount_round(int64_t count)
  424. {
  425. int shift = atomic_read(&timers_state.icount_time_shift);
  426. return (count + (1 << shift) - 1) >> shift;
  427. }
  428. static void icount_warp_rt(void)
  429. {
  430. unsigned seq;
  431. int64_t warp_start;
  432. /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
  433. * changes from -1 to another value, so the race here is okay.
  434. */
  435. do {
  436. seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  437. warp_start = timers_state.vm_clock_warp_start;
  438. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
  439. if (warp_start == -1) {
  440. return;
  441. }
  442. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  443. &timers_state.vm_clock_lock);
  444. if (runstate_is_running()) {
  445. int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
  446. cpu_get_clock_locked());
  447. int64_t warp_delta;
  448. warp_delta = clock - timers_state.vm_clock_warp_start;
  449. if (use_icount == 2) {
  450. /*
  451. * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
  452. * far ahead of real time.
  453. */
  454. int64_t cur_icount = cpu_get_icount_locked();
  455. int64_t delta = clock - cur_icount;
  456. warp_delta = MIN(warp_delta, delta);
  457. }
  458. atomic_set_i64(&timers_state.qemu_icount_bias,
  459. timers_state.qemu_icount_bias + warp_delta);
  460. }
  461. timers_state.vm_clock_warp_start = -1;
  462. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  463. &timers_state.vm_clock_lock);
  464. if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
  465. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  466. }
  467. }
  468. static void icount_timer_cb(void *opaque)
  469. {
  470. /* No need for a checkpoint because the timer already synchronizes
  471. * with CHECKPOINT_CLOCK_VIRTUAL_RT.
  472. */
  473. icount_warp_rt();
  474. }
  475. void qtest_clock_warp(int64_t dest)
  476. {
  477. int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  478. AioContext *aio_context;
  479. assert(qtest_enabled());
  480. aio_context = qemu_get_aio_context();
  481. while (clock < dest) {
  482. int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  483. int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
  484. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  485. &timers_state.vm_clock_lock);
  486. atomic_set_i64(&timers_state.qemu_icount_bias,
  487. timers_state.qemu_icount_bias + warp);
  488. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  489. &timers_state.vm_clock_lock);
  490. qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
  491. timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
  492. clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  493. }
  494. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  495. }
  496. void qemu_start_warp_timer(void)
  497. {
  498. int64_t clock;
  499. int64_t deadline;
  500. if (!use_icount) {
  501. return;
  502. }
  503. /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
  504. * do not fire, so computing the deadline does not make sense.
  505. */
  506. if (!runstate_is_running()) {
  507. return;
  508. }
  509. if (replay_mode != REPLAY_MODE_PLAY) {
  510. if (!all_cpu_threads_idle()) {
  511. return;
  512. }
  513. if (qtest_enabled()) {
  514. /* When testing, qtest commands advance icount. */
  515. return;
  516. }
  517. replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
  518. } else {
  519. /* warp clock deterministically in record/replay mode */
  520. if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
  521. /* vCPU is sleeping and warp can't be started.
  522. It is probably a race condition: notification sent
  523. to vCPU was processed in advance and vCPU went to sleep.
  524. Therefore we have to wake it up for doing someting. */
  525. if (replay_has_checkpoint()) {
  526. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  527. }
  528. return;
  529. }
  530. }
  531. /* We want to use the earliest deadline from ALL vm_clocks */
  532. clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
  533. deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  534. if (deadline < 0) {
  535. static bool notified;
  536. if (!icount_sleep && !notified) {
  537. warn_report("icount sleep disabled and no active timers");
  538. notified = true;
  539. }
  540. return;
  541. }
  542. if (deadline > 0) {
  543. /*
  544. * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
  545. * sleep. Otherwise, the CPU might be waiting for a future timer
  546. * interrupt to wake it up, but the interrupt never comes because
  547. * the vCPU isn't running any insns and thus doesn't advance the
  548. * QEMU_CLOCK_VIRTUAL.
  549. */
  550. if (!icount_sleep) {
  551. /*
  552. * We never let VCPUs sleep in no sleep icount mode.
  553. * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
  554. * to the next QEMU_CLOCK_VIRTUAL event and notify it.
  555. * It is useful when we want a deterministic execution time,
  556. * isolated from host latencies.
  557. */
  558. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  559. &timers_state.vm_clock_lock);
  560. atomic_set_i64(&timers_state.qemu_icount_bias,
  561. timers_state.qemu_icount_bias + deadline);
  562. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  563. &timers_state.vm_clock_lock);
  564. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  565. } else {
  566. /*
  567. * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
  568. * "real" time, (related to the time left until the next event) has
  569. * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
  570. * This avoids that the warps are visible externally; for example,
  571. * you will not be sending network packets continuously instead of
  572. * every 100ms.
  573. */
  574. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  575. &timers_state.vm_clock_lock);
  576. if (timers_state.vm_clock_warp_start == -1
  577. || timers_state.vm_clock_warp_start > clock) {
  578. timers_state.vm_clock_warp_start = clock;
  579. }
  580. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  581. &timers_state.vm_clock_lock);
  582. timer_mod_anticipate(timers_state.icount_warp_timer,
  583. clock + deadline);
  584. }
  585. } else if (deadline == 0) {
  586. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  587. }
  588. }
  589. static void qemu_account_warp_timer(void)
  590. {
  591. if (!use_icount || !icount_sleep) {
  592. return;
  593. }
  594. /* Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
  595. * do not fire, so computing the deadline does not make sense.
  596. */
  597. if (!runstate_is_running()) {
  598. return;
  599. }
  600. /* warp clock deterministically in record/replay mode */
  601. if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
  602. return;
  603. }
  604. timer_del(timers_state.icount_warp_timer);
  605. icount_warp_rt();
  606. }
  607. static bool icount_state_needed(void *opaque)
  608. {
  609. return use_icount;
  610. }
  611. static bool warp_timer_state_needed(void *opaque)
  612. {
  613. TimersState *s = opaque;
  614. return s->icount_warp_timer != NULL;
  615. }
  616. static bool adjust_timers_state_needed(void *opaque)
  617. {
  618. TimersState *s = opaque;
  619. return s->icount_rt_timer != NULL;
  620. }
  621. /*
  622. * Subsection for warp timer migration is optional, because may not be created
  623. */
  624. static const VMStateDescription icount_vmstate_warp_timer = {
  625. .name = "timer/icount/warp_timer",
  626. .version_id = 1,
  627. .minimum_version_id = 1,
  628. .needed = warp_timer_state_needed,
  629. .fields = (VMStateField[]) {
  630. VMSTATE_INT64(vm_clock_warp_start, TimersState),
  631. VMSTATE_TIMER_PTR(icount_warp_timer, TimersState),
  632. VMSTATE_END_OF_LIST()
  633. }
  634. };
  635. static const VMStateDescription icount_vmstate_adjust_timers = {
  636. .name = "timer/icount/timers",
  637. .version_id = 1,
  638. .minimum_version_id = 1,
  639. .needed = adjust_timers_state_needed,
  640. .fields = (VMStateField[]) {
  641. VMSTATE_TIMER_PTR(icount_rt_timer, TimersState),
  642. VMSTATE_TIMER_PTR(icount_vm_timer, TimersState),
  643. VMSTATE_END_OF_LIST()
  644. }
  645. };
  646. /*
  647. * This is a subsection for icount migration.
  648. */
  649. static const VMStateDescription icount_vmstate_timers = {
  650. .name = "timer/icount",
  651. .version_id = 1,
  652. .minimum_version_id = 1,
  653. .needed = icount_state_needed,
  654. .fields = (VMStateField[]) {
  655. VMSTATE_INT64(qemu_icount_bias, TimersState),
  656. VMSTATE_INT64(qemu_icount, TimersState),
  657. VMSTATE_END_OF_LIST()
  658. },
  659. .subsections = (const VMStateDescription*[]) {
  660. &icount_vmstate_warp_timer,
  661. &icount_vmstate_adjust_timers,
  662. NULL
  663. }
  664. };
  665. static const VMStateDescription vmstate_timers = {
  666. .name = "timer",
  667. .version_id = 2,
  668. .minimum_version_id = 1,
  669. .fields = (VMStateField[]) {
  670. VMSTATE_INT64(cpu_ticks_offset, TimersState),
  671. VMSTATE_UNUSED(8),
  672. VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
  673. VMSTATE_END_OF_LIST()
  674. },
  675. .subsections = (const VMStateDescription*[]) {
  676. &icount_vmstate_timers,
  677. NULL
  678. }
  679. };
  680. static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
  681. {
  682. double pct;
  683. double throttle_ratio;
  684. long sleeptime_ns;
  685. if (!cpu_throttle_get_percentage()) {
  686. return;
  687. }
  688. pct = (double)cpu_throttle_get_percentage()/100;
  689. throttle_ratio = pct / (1 - pct);
  690. sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
  691. qemu_mutex_unlock_iothread();
  692. g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
  693. qemu_mutex_lock_iothread();
  694. atomic_set(&cpu->throttle_thread_scheduled, 0);
  695. }
  696. static void cpu_throttle_timer_tick(void *opaque)
  697. {
  698. CPUState *cpu;
  699. double pct;
  700. /* Stop the timer if needed */
  701. if (!cpu_throttle_get_percentage()) {
  702. return;
  703. }
  704. CPU_FOREACH(cpu) {
  705. if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
  706. async_run_on_cpu(cpu, cpu_throttle_thread,
  707. RUN_ON_CPU_NULL);
  708. }
  709. }
  710. pct = (double)cpu_throttle_get_percentage()/100;
  711. timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
  712. CPU_THROTTLE_TIMESLICE_NS / (1-pct));
  713. }
  714. void cpu_throttle_set(int new_throttle_pct)
  715. {
  716. /* Ensure throttle percentage is within valid range */
  717. new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
  718. new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
  719. atomic_set(&throttle_percentage, new_throttle_pct);
  720. timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
  721. CPU_THROTTLE_TIMESLICE_NS);
  722. }
  723. void cpu_throttle_stop(void)
  724. {
  725. atomic_set(&throttle_percentage, 0);
  726. }
  727. bool cpu_throttle_active(void)
  728. {
  729. return (cpu_throttle_get_percentage() != 0);
  730. }
  731. int cpu_throttle_get_percentage(void)
  732. {
  733. return atomic_read(&throttle_percentage);
  734. }
  735. void cpu_ticks_init(void)
  736. {
  737. seqlock_init(&timers_state.vm_clock_seqlock);
  738. qemu_spin_init(&timers_state.vm_clock_lock);
  739. vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
  740. throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
  741. cpu_throttle_timer_tick, NULL);
  742. }
  743. void configure_icount(QemuOpts *opts, Error **errp)
  744. {
  745. const char *option;
  746. char *rem_str = NULL;
  747. option = qemu_opt_get(opts, "shift");
  748. if (!option) {
  749. if (qemu_opt_get(opts, "align") != NULL) {
  750. error_setg(errp, "Please specify shift option when using align");
  751. }
  752. return;
  753. }
  754. icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
  755. if (icount_sleep) {
  756. timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
  757. icount_timer_cb, NULL);
  758. }
  759. icount_align_option = qemu_opt_get_bool(opts, "align", false);
  760. if (icount_align_option && !icount_sleep) {
  761. error_setg(errp, "align=on and sleep=off are incompatible");
  762. }
  763. if (strcmp(option, "auto") != 0) {
  764. errno = 0;
  765. timers_state.icount_time_shift = strtol(option, &rem_str, 0);
  766. if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
  767. error_setg(errp, "icount: Invalid shift value");
  768. }
  769. use_icount = 1;
  770. return;
  771. } else if (icount_align_option) {
  772. error_setg(errp, "shift=auto and align=on are incompatible");
  773. } else if (!icount_sleep) {
  774. error_setg(errp, "shift=auto and sleep=off are incompatible");
  775. }
  776. use_icount = 2;
  777. /* 125MIPS seems a reasonable initial guess at the guest speed.
  778. It will be corrected fairly quickly anyway. */
  779. timers_state.icount_time_shift = 3;
  780. /* Have both realtime and virtual time triggers for speed adjustment.
  781. The realtime trigger catches emulated time passing too slowly,
  782. the virtual time trigger catches emulated time passing too fast.
  783. Realtime triggers occur even when idle, so use them less frequently
  784. than VM triggers. */
  785. timers_state.vm_clock_warp_start = -1;
  786. timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
  787. icount_adjust_rt, NULL);
  788. timer_mod(timers_state.icount_rt_timer,
  789. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
  790. timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  791. icount_adjust_vm, NULL);
  792. timer_mod(timers_state.icount_vm_timer,
  793. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  794. NANOSECONDS_PER_SECOND / 10);
  795. }
  796. /***********************************************************/
  797. /* TCG vCPU kick timer
  798. *
  799. * The kick timer is responsible for moving single threaded vCPU
  800. * emulation on to the next vCPU. If more than one vCPU is running a
  801. * timer event with force a cpu->exit so the next vCPU can get
  802. * scheduled.
  803. *
  804. * The timer is removed if all vCPUs are idle and restarted again once
  805. * idleness is complete.
  806. */
  807. static QEMUTimer *tcg_kick_vcpu_timer;
  808. static CPUState *tcg_current_rr_cpu;
  809. #define TCG_KICK_PERIOD (NANOSECONDS_PER_SECOND / 10)
  810. static inline int64_t qemu_tcg_next_kick(void)
  811. {
  812. return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + TCG_KICK_PERIOD;
  813. }
  814. /* Kick the currently round-robin scheduled vCPU */
  815. static void qemu_cpu_kick_rr_cpu(void)
  816. {
  817. CPUState *cpu;
  818. do {
  819. cpu = atomic_mb_read(&tcg_current_rr_cpu);
  820. if (cpu) {
  821. cpu_exit(cpu);
  822. }
  823. } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
  824. }
  825. static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
  826. {
  827. }
  828. void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
  829. {
  830. if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
  831. qemu_notify_event();
  832. return;
  833. }
  834. if (qemu_in_vcpu_thread()) {
  835. /* A CPU is currently running; kick it back out to the
  836. * tcg_cpu_exec() loop so it will recalculate its
  837. * icount deadline immediately.
  838. */
  839. qemu_cpu_kick(current_cpu);
  840. } else if (first_cpu) {
  841. /* qemu_cpu_kick is not enough to kick a halted CPU out of
  842. * qemu_tcg_wait_io_event. async_run_on_cpu, instead,
  843. * causes cpu_thread_is_idle to return false. This way,
  844. * handle_icount_deadline can run.
  845. * If we have no CPUs at all for some reason, we don't
  846. * need to do anything.
  847. */
  848. async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
  849. }
  850. }
  851. static void kick_tcg_thread(void *opaque)
  852. {
  853. timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
  854. qemu_cpu_kick_rr_cpu();
  855. }
  856. static void start_tcg_kick_timer(void)
  857. {
  858. assert(!mttcg_enabled);
  859. if (!tcg_kick_vcpu_timer && CPU_NEXT(first_cpu)) {
  860. tcg_kick_vcpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  861. kick_tcg_thread, NULL);
  862. }
  863. if (tcg_kick_vcpu_timer && !timer_pending(tcg_kick_vcpu_timer)) {
  864. timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
  865. }
  866. }
  867. static void stop_tcg_kick_timer(void)
  868. {
  869. assert(!mttcg_enabled);
  870. if (tcg_kick_vcpu_timer && timer_pending(tcg_kick_vcpu_timer)) {
  871. timer_del(tcg_kick_vcpu_timer);
  872. }
  873. }
  874. /***********************************************************/
  875. void hw_error(const char *fmt, ...)
  876. {
  877. va_list ap;
  878. CPUState *cpu;
  879. va_start(ap, fmt);
  880. fprintf(stderr, "qemu: hardware error: ");
  881. vfprintf(stderr, fmt, ap);
  882. fprintf(stderr, "\n");
  883. CPU_FOREACH(cpu) {
  884. fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
  885. cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
  886. }
  887. va_end(ap);
  888. abort();
  889. }
  890. void cpu_synchronize_all_states(void)
  891. {
  892. CPUState *cpu;
  893. CPU_FOREACH(cpu) {
  894. cpu_synchronize_state(cpu);
  895. /* TODO: move to cpu_synchronize_state() */
  896. if (hvf_enabled()) {
  897. hvf_cpu_synchronize_state(cpu);
  898. }
  899. }
  900. }
  901. void cpu_synchronize_all_post_reset(void)
  902. {
  903. CPUState *cpu;
  904. CPU_FOREACH(cpu) {
  905. cpu_synchronize_post_reset(cpu);
  906. /* TODO: move to cpu_synchronize_post_reset() */
  907. if (hvf_enabled()) {
  908. hvf_cpu_synchronize_post_reset(cpu);
  909. }
  910. }
  911. }
  912. void cpu_synchronize_all_post_init(void)
  913. {
  914. CPUState *cpu;
  915. CPU_FOREACH(cpu) {
  916. cpu_synchronize_post_init(cpu);
  917. /* TODO: move to cpu_synchronize_post_init() */
  918. if (hvf_enabled()) {
  919. hvf_cpu_synchronize_post_init(cpu);
  920. }
  921. }
  922. }
  923. void cpu_synchronize_all_pre_loadvm(void)
  924. {
  925. CPUState *cpu;
  926. CPU_FOREACH(cpu) {
  927. cpu_synchronize_pre_loadvm(cpu);
  928. }
  929. }
  930. static int do_vm_stop(RunState state, bool send_stop)
  931. {
  932. int ret = 0;
  933. if (runstate_is_running()) {
  934. cpu_disable_ticks();
  935. pause_all_vcpus();
  936. runstate_set(state);
  937. vm_state_notify(0, state);
  938. if (send_stop) {
  939. qapi_event_send_stop();
  940. }
  941. }
  942. bdrv_drain_all();
  943. replay_disable_events();
  944. ret = bdrv_flush_all();
  945. return ret;
  946. }
  947. /* Special vm_stop() variant for terminating the process. Historically clients
  948. * did not expect a QMP STOP event and so we need to retain compatibility.
  949. */
  950. int vm_shutdown(void)
  951. {
  952. return do_vm_stop(RUN_STATE_SHUTDOWN, false);
  953. }
  954. static bool cpu_can_run(CPUState *cpu)
  955. {
  956. if (cpu->stop) {
  957. return false;
  958. }
  959. if (cpu_is_stopped(cpu)) {
  960. return false;
  961. }
  962. return true;
  963. }
  964. static void cpu_handle_guest_debug(CPUState *cpu)
  965. {
  966. gdb_set_stop_cpu(cpu);
  967. qemu_system_debug_request();
  968. cpu->stopped = true;
  969. }
  970. #ifdef CONFIG_LINUX
  971. static void sigbus_reraise(void)
  972. {
  973. sigset_t set;
  974. struct sigaction action;
  975. memset(&action, 0, sizeof(action));
  976. action.sa_handler = SIG_DFL;
  977. if (!sigaction(SIGBUS, &action, NULL)) {
  978. raise(SIGBUS);
  979. sigemptyset(&set);
  980. sigaddset(&set, SIGBUS);
  981. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  982. }
  983. perror("Failed to re-raise SIGBUS!\n");
  984. abort();
  985. }
  986. static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
  987. {
  988. if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
  989. sigbus_reraise();
  990. }
  991. if (current_cpu) {
  992. /* Called asynchronously in VCPU thread. */
  993. if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
  994. sigbus_reraise();
  995. }
  996. } else {
  997. /* Called synchronously (via signalfd) in main thread. */
  998. if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
  999. sigbus_reraise();
  1000. }
  1001. }
  1002. }
  1003. static void qemu_init_sigbus(void)
  1004. {
  1005. struct sigaction action;
  1006. memset(&action, 0, sizeof(action));
  1007. action.sa_flags = SA_SIGINFO;
  1008. action.sa_sigaction = sigbus_handler;
  1009. sigaction(SIGBUS, &action, NULL);
  1010. prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
  1011. }
  1012. #else /* !CONFIG_LINUX */
  1013. static void qemu_init_sigbus(void)
  1014. {
  1015. }
  1016. #endif /* !CONFIG_LINUX */
  1017. static QemuMutex qemu_global_mutex;
  1018. static QemuThread io_thread;
  1019. /* cpu creation */
  1020. static QemuCond qemu_cpu_cond;
  1021. /* system init */
  1022. static QemuCond qemu_pause_cond;
  1023. void qemu_init_cpu_loop(void)
  1024. {
  1025. qemu_init_sigbus();
  1026. qemu_cond_init(&qemu_cpu_cond);
  1027. qemu_cond_init(&qemu_pause_cond);
  1028. qemu_mutex_init(&qemu_global_mutex);
  1029. qemu_thread_get_self(&io_thread);
  1030. }
  1031. void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
  1032. {
  1033. do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
  1034. }
  1035. static void qemu_kvm_destroy_vcpu(CPUState *cpu)
  1036. {
  1037. if (kvm_destroy_vcpu(cpu) < 0) {
  1038. error_report("kvm_destroy_vcpu failed");
  1039. exit(EXIT_FAILURE);
  1040. }
  1041. }
  1042. static void qemu_tcg_destroy_vcpu(CPUState *cpu)
  1043. {
  1044. }
  1045. static void qemu_cpu_stop(CPUState *cpu, bool exit)
  1046. {
  1047. g_assert(qemu_cpu_is_self(cpu));
  1048. cpu->stop = false;
  1049. cpu->stopped = true;
  1050. if (exit) {
  1051. cpu_exit(cpu);
  1052. }
  1053. qemu_cond_broadcast(&qemu_pause_cond);
  1054. }
  1055. static void qemu_wait_io_event_common(CPUState *cpu)
  1056. {
  1057. atomic_mb_set(&cpu->thread_kicked, false);
  1058. if (cpu->stop) {
  1059. qemu_cpu_stop(cpu, false);
  1060. }
  1061. process_queued_cpu_work(cpu);
  1062. }
  1063. static void qemu_tcg_rr_wait_io_event(void)
  1064. {
  1065. CPUState *cpu;
  1066. while (all_cpu_threads_idle()) {
  1067. stop_tcg_kick_timer();
  1068. qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
  1069. }
  1070. start_tcg_kick_timer();
  1071. CPU_FOREACH(cpu) {
  1072. qemu_wait_io_event_common(cpu);
  1073. }
  1074. }
  1075. static void qemu_wait_io_event(CPUState *cpu)
  1076. {
  1077. while (cpu_thread_is_idle(cpu)) {
  1078. qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
  1079. }
  1080. #ifdef _WIN32
  1081. /* Eat dummy APC queued by qemu_cpu_kick_thread. */
  1082. if (!tcg_enabled()) {
  1083. SleepEx(0, TRUE);
  1084. }
  1085. #endif
  1086. qemu_wait_io_event_common(cpu);
  1087. }
  1088. static void *qemu_kvm_cpu_thread_fn(void *arg)
  1089. {
  1090. CPUState *cpu = arg;
  1091. int r;
  1092. rcu_register_thread();
  1093. qemu_mutex_lock_iothread();
  1094. qemu_thread_get_self(cpu->thread);
  1095. cpu->thread_id = qemu_get_thread_id();
  1096. cpu->can_do_io = 1;
  1097. current_cpu = cpu;
  1098. r = kvm_init_vcpu(cpu);
  1099. if (r < 0) {
  1100. error_report("kvm_init_vcpu failed: %s", strerror(-r));
  1101. exit(1);
  1102. }
  1103. kvm_init_cpu_signals(cpu);
  1104. /* signal CPU creation */
  1105. cpu->created = true;
  1106. qemu_cond_signal(&qemu_cpu_cond);
  1107. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  1108. do {
  1109. if (cpu_can_run(cpu)) {
  1110. r = kvm_cpu_exec(cpu);
  1111. if (r == EXCP_DEBUG) {
  1112. cpu_handle_guest_debug(cpu);
  1113. }
  1114. }
  1115. qemu_wait_io_event(cpu);
  1116. } while (!cpu->unplug || cpu_can_run(cpu));
  1117. qemu_kvm_destroy_vcpu(cpu);
  1118. cpu->created = false;
  1119. qemu_cond_signal(&qemu_cpu_cond);
  1120. qemu_mutex_unlock_iothread();
  1121. rcu_unregister_thread();
  1122. return NULL;
  1123. }
  1124. static void *qemu_dummy_cpu_thread_fn(void *arg)
  1125. {
  1126. #ifdef _WIN32
  1127. error_report("qtest is not supported under Windows");
  1128. exit(1);
  1129. #else
  1130. CPUState *cpu = arg;
  1131. sigset_t waitset;
  1132. int r;
  1133. rcu_register_thread();
  1134. qemu_mutex_lock_iothread();
  1135. qemu_thread_get_self(cpu->thread);
  1136. cpu->thread_id = qemu_get_thread_id();
  1137. cpu->can_do_io = 1;
  1138. current_cpu = cpu;
  1139. sigemptyset(&waitset);
  1140. sigaddset(&waitset, SIG_IPI);
  1141. /* signal CPU creation */
  1142. cpu->created = true;
  1143. qemu_cond_signal(&qemu_cpu_cond);
  1144. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  1145. do {
  1146. qemu_mutex_unlock_iothread();
  1147. do {
  1148. int sig;
  1149. r = sigwait(&waitset, &sig);
  1150. } while (r == -1 && (errno == EAGAIN || errno == EINTR));
  1151. if (r == -1) {
  1152. perror("sigwait");
  1153. exit(1);
  1154. }
  1155. qemu_mutex_lock_iothread();
  1156. qemu_wait_io_event(cpu);
  1157. } while (!cpu->unplug);
  1158. qemu_mutex_unlock_iothread();
  1159. rcu_unregister_thread();
  1160. return NULL;
  1161. #endif
  1162. }
  1163. static int64_t tcg_get_icount_limit(void)
  1164. {
  1165. int64_t deadline;
  1166. if (replay_mode != REPLAY_MODE_PLAY) {
  1167. deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  1168. /* Maintain prior (possibly buggy) behaviour where if no deadline
  1169. * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
  1170. * INT32_MAX nanoseconds ahead, we still use INT32_MAX
  1171. * nanoseconds.
  1172. */
  1173. if ((deadline < 0) || (deadline > INT32_MAX)) {
  1174. deadline = INT32_MAX;
  1175. }
  1176. return qemu_icount_round(deadline);
  1177. } else {
  1178. return replay_get_instructions();
  1179. }
  1180. }
  1181. static void handle_icount_deadline(void)
  1182. {
  1183. assert(qemu_in_vcpu_thread());
  1184. if (use_icount) {
  1185. int64_t deadline =
  1186. qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  1187. if (deadline == 0) {
  1188. /* Wake up other AioContexts. */
  1189. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  1190. qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
  1191. }
  1192. }
  1193. }
  1194. static void prepare_icount_for_run(CPUState *cpu)
  1195. {
  1196. if (use_icount) {
  1197. int insns_left;
  1198. /* These should always be cleared by process_icount_data after
  1199. * each vCPU execution. However u16.high can be raised
  1200. * asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt
  1201. */
  1202. g_assert(cpu_neg(cpu)->icount_decr.u16.low == 0);
  1203. g_assert(cpu->icount_extra == 0);
  1204. cpu->icount_budget = tcg_get_icount_limit();
  1205. insns_left = MIN(0xffff, cpu->icount_budget);
  1206. cpu_neg(cpu)->icount_decr.u16.low = insns_left;
  1207. cpu->icount_extra = cpu->icount_budget - insns_left;
  1208. replay_mutex_lock();
  1209. }
  1210. }
  1211. static void process_icount_data(CPUState *cpu)
  1212. {
  1213. if (use_icount) {
  1214. /* Account for executed instructions */
  1215. cpu_update_icount(cpu);
  1216. /* Reset the counters */
  1217. cpu_neg(cpu)->icount_decr.u16.low = 0;
  1218. cpu->icount_extra = 0;
  1219. cpu->icount_budget = 0;
  1220. replay_account_executed_instructions();
  1221. replay_mutex_unlock();
  1222. }
  1223. }
  1224. static int tcg_cpu_exec(CPUState *cpu)
  1225. {
  1226. int ret;
  1227. #ifdef CONFIG_PROFILER
  1228. int64_t ti;
  1229. #endif
  1230. assert(tcg_enabled());
  1231. #ifdef CONFIG_PROFILER
  1232. ti = profile_getclock();
  1233. #endif
  1234. cpu_exec_start(cpu);
  1235. ret = cpu_exec(cpu);
  1236. cpu_exec_end(cpu);
  1237. #ifdef CONFIG_PROFILER
  1238. atomic_set(&tcg_ctx->prof.cpu_exec_time,
  1239. tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
  1240. #endif
  1241. return ret;
  1242. }
  1243. /* Destroy any remaining vCPUs which have been unplugged and have
  1244. * finished running
  1245. */
  1246. static void deal_with_unplugged_cpus(void)
  1247. {
  1248. CPUState *cpu;
  1249. CPU_FOREACH(cpu) {
  1250. if (cpu->unplug && !cpu_can_run(cpu)) {
  1251. qemu_tcg_destroy_vcpu(cpu);
  1252. cpu->created = false;
  1253. qemu_cond_signal(&qemu_cpu_cond);
  1254. break;
  1255. }
  1256. }
  1257. }
  1258. /* Single-threaded TCG
  1259. *
  1260. * In the single-threaded case each vCPU is simulated in turn. If
  1261. * there is more than a single vCPU we create a simple timer to kick
  1262. * the vCPU and ensure we don't get stuck in a tight loop in one vCPU.
  1263. * This is done explicitly rather than relying on side-effects
  1264. * elsewhere.
  1265. */
  1266. static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
  1267. {
  1268. CPUState *cpu = arg;
  1269. assert(tcg_enabled());
  1270. rcu_register_thread();
  1271. tcg_register_thread();
  1272. qemu_mutex_lock_iothread();
  1273. qemu_thread_get_self(cpu->thread);
  1274. cpu->thread_id = qemu_get_thread_id();
  1275. cpu->created = true;
  1276. cpu->can_do_io = 1;
  1277. qemu_cond_signal(&qemu_cpu_cond);
  1278. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  1279. /* wait for initial kick-off after machine start */
  1280. while (first_cpu->stopped) {
  1281. qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
  1282. /* process any pending work */
  1283. CPU_FOREACH(cpu) {
  1284. current_cpu = cpu;
  1285. qemu_wait_io_event_common(cpu);
  1286. }
  1287. }
  1288. start_tcg_kick_timer();
  1289. cpu = first_cpu;
  1290. /* process any pending work */
  1291. cpu->exit_request = 1;
  1292. while (1) {
  1293. qemu_mutex_unlock_iothread();
  1294. replay_mutex_lock();
  1295. qemu_mutex_lock_iothread();
  1296. /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
  1297. qemu_account_warp_timer();
  1298. /* Run the timers here. This is much more efficient than
  1299. * waking up the I/O thread and waiting for completion.
  1300. */
  1301. handle_icount_deadline();
  1302. replay_mutex_unlock();
  1303. if (!cpu) {
  1304. cpu = first_cpu;
  1305. }
  1306. while (cpu && !cpu->queued_work_first && !cpu->exit_request) {
  1307. atomic_mb_set(&tcg_current_rr_cpu, cpu);
  1308. current_cpu = cpu;
  1309. qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
  1310. (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
  1311. if (cpu_can_run(cpu)) {
  1312. int r;
  1313. qemu_mutex_unlock_iothread();
  1314. prepare_icount_for_run(cpu);
  1315. r = tcg_cpu_exec(cpu);
  1316. process_icount_data(cpu);
  1317. qemu_mutex_lock_iothread();
  1318. if (r == EXCP_DEBUG) {
  1319. cpu_handle_guest_debug(cpu);
  1320. break;
  1321. } else if (r == EXCP_ATOMIC) {
  1322. qemu_mutex_unlock_iothread();
  1323. cpu_exec_step_atomic(cpu);
  1324. qemu_mutex_lock_iothread();
  1325. break;
  1326. }
  1327. } else if (cpu->stop) {
  1328. if (cpu->unplug) {
  1329. cpu = CPU_NEXT(cpu);
  1330. }
  1331. break;
  1332. }
  1333. cpu = CPU_NEXT(cpu);
  1334. } /* while (cpu && !cpu->exit_request).. */
  1335. /* Does not need atomic_mb_set because a spurious wakeup is okay. */
  1336. atomic_set(&tcg_current_rr_cpu, NULL);
  1337. if (cpu && cpu->exit_request) {
  1338. atomic_mb_set(&cpu->exit_request, 0);
  1339. }
  1340. if (use_icount && all_cpu_threads_idle()) {
  1341. /*
  1342. * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
  1343. * in the main_loop, wake it up in order to start the warp timer.
  1344. */
  1345. qemu_notify_event();
  1346. }
  1347. qemu_tcg_rr_wait_io_event();
  1348. deal_with_unplugged_cpus();
  1349. }
  1350. rcu_unregister_thread();
  1351. return NULL;
  1352. }
  1353. static void *qemu_hax_cpu_thread_fn(void *arg)
  1354. {
  1355. CPUState *cpu = arg;
  1356. int r;
  1357. rcu_register_thread();
  1358. qemu_mutex_lock_iothread();
  1359. qemu_thread_get_self(cpu->thread);
  1360. cpu->thread_id = qemu_get_thread_id();
  1361. cpu->created = true;
  1362. cpu->halted = 0;
  1363. current_cpu = cpu;
  1364. hax_init_vcpu(cpu);
  1365. qemu_cond_signal(&qemu_cpu_cond);
  1366. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  1367. do {
  1368. if (cpu_can_run(cpu)) {
  1369. r = hax_smp_cpu_exec(cpu);
  1370. if (r == EXCP_DEBUG) {
  1371. cpu_handle_guest_debug(cpu);
  1372. }
  1373. }
  1374. qemu_wait_io_event(cpu);
  1375. } while (!cpu->unplug || cpu_can_run(cpu));
  1376. rcu_unregister_thread();
  1377. return NULL;
  1378. }
  1379. /* The HVF-specific vCPU thread function. This one should only run when the host
  1380. * CPU supports the VMX "unrestricted guest" feature. */
  1381. static void *qemu_hvf_cpu_thread_fn(void *arg)
  1382. {
  1383. CPUState *cpu = arg;
  1384. int r;
  1385. assert(hvf_enabled());
  1386. rcu_register_thread();
  1387. qemu_mutex_lock_iothread();
  1388. qemu_thread_get_self(cpu->thread);
  1389. cpu->thread_id = qemu_get_thread_id();
  1390. cpu->can_do_io = 1;
  1391. current_cpu = cpu;
  1392. hvf_init_vcpu(cpu);
  1393. /* signal CPU creation */
  1394. cpu->created = true;
  1395. qemu_cond_signal(&qemu_cpu_cond);
  1396. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  1397. do {
  1398. if (cpu_can_run(cpu)) {
  1399. r = hvf_vcpu_exec(cpu);
  1400. if (r == EXCP_DEBUG) {
  1401. cpu_handle_guest_debug(cpu);
  1402. }
  1403. }
  1404. qemu_wait_io_event(cpu);
  1405. } while (!cpu->unplug || cpu_can_run(cpu));
  1406. hvf_vcpu_destroy(cpu);
  1407. cpu->created = false;
  1408. qemu_cond_signal(&qemu_cpu_cond);
  1409. qemu_mutex_unlock_iothread();
  1410. rcu_unregister_thread();
  1411. return NULL;
  1412. }
  1413. static void *qemu_whpx_cpu_thread_fn(void *arg)
  1414. {
  1415. CPUState *cpu = arg;
  1416. int r;
  1417. rcu_register_thread();
  1418. qemu_mutex_lock_iothread();
  1419. qemu_thread_get_self(cpu->thread);
  1420. cpu->thread_id = qemu_get_thread_id();
  1421. current_cpu = cpu;
  1422. r = whpx_init_vcpu(cpu);
  1423. if (r < 0) {
  1424. fprintf(stderr, "whpx_init_vcpu failed: %s\n", strerror(-r));
  1425. exit(1);
  1426. }
  1427. /* signal CPU creation */
  1428. cpu->created = true;
  1429. qemu_cond_signal(&qemu_cpu_cond);
  1430. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  1431. do {
  1432. if (cpu_can_run(cpu)) {
  1433. r = whpx_vcpu_exec(cpu);
  1434. if (r == EXCP_DEBUG) {
  1435. cpu_handle_guest_debug(cpu);
  1436. }
  1437. }
  1438. while (cpu_thread_is_idle(cpu)) {
  1439. qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
  1440. }
  1441. qemu_wait_io_event_common(cpu);
  1442. } while (!cpu->unplug || cpu_can_run(cpu));
  1443. whpx_destroy_vcpu(cpu);
  1444. cpu->created = false;
  1445. qemu_cond_signal(&qemu_cpu_cond);
  1446. qemu_mutex_unlock_iothread();
  1447. rcu_unregister_thread();
  1448. return NULL;
  1449. }
  1450. #ifdef _WIN32
  1451. static void CALLBACK dummy_apc_func(ULONG_PTR unused)
  1452. {
  1453. }
  1454. #endif
  1455. /* Multi-threaded TCG
  1456. *
  1457. * In the multi-threaded case each vCPU has its own thread. The TLS
  1458. * variable current_cpu can be used deep in the code to find the
  1459. * current CPUState for a given thread.
  1460. */
  1461. static void *qemu_tcg_cpu_thread_fn(void *arg)
  1462. {
  1463. CPUState *cpu = arg;
  1464. assert(tcg_enabled());
  1465. g_assert(!use_icount);
  1466. rcu_register_thread();
  1467. tcg_register_thread();
  1468. qemu_mutex_lock_iothread();
  1469. qemu_thread_get_self(cpu->thread);
  1470. cpu->thread_id = qemu_get_thread_id();
  1471. cpu->created = true;
  1472. cpu->can_do_io = 1;
  1473. current_cpu = cpu;
  1474. qemu_cond_signal(&qemu_cpu_cond);
  1475. qemu_guest_random_seed_thread_part2(cpu->random_seed);
  1476. /* process any pending work */
  1477. cpu->exit_request = 1;
  1478. do {
  1479. if (cpu_can_run(cpu)) {
  1480. int r;
  1481. qemu_mutex_unlock_iothread();
  1482. r = tcg_cpu_exec(cpu);
  1483. qemu_mutex_lock_iothread();
  1484. switch (r) {
  1485. case EXCP_DEBUG:
  1486. cpu_handle_guest_debug(cpu);
  1487. break;
  1488. case EXCP_HALTED:
  1489. /* during start-up the vCPU is reset and the thread is
  1490. * kicked several times. If we don't ensure we go back
  1491. * to sleep in the halted state we won't cleanly
  1492. * start-up when the vCPU is enabled.
  1493. *
  1494. * cpu->halted should ensure we sleep in wait_io_event
  1495. */
  1496. g_assert(cpu->halted);
  1497. break;
  1498. case EXCP_ATOMIC:
  1499. qemu_mutex_unlock_iothread();
  1500. cpu_exec_step_atomic(cpu);
  1501. qemu_mutex_lock_iothread();
  1502. default:
  1503. /* Ignore everything else? */
  1504. break;
  1505. }
  1506. }
  1507. atomic_mb_set(&cpu->exit_request, 0);
  1508. qemu_wait_io_event(cpu);
  1509. } while (!cpu->unplug || cpu_can_run(cpu));
  1510. qemu_tcg_destroy_vcpu(cpu);
  1511. cpu->created = false;
  1512. qemu_cond_signal(&qemu_cpu_cond);
  1513. qemu_mutex_unlock_iothread();
  1514. rcu_unregister_thread();
  1515. return NULL;
  1516. }
  1517. static void qemu_cpu_kick_thread(CPUState *cpu)
  1518. {
  1519. #ifndef _WIN32
  1520. int err;
  1521. if (cpu->thread_kicked) {
  1522. return;
  1523. }
  1524. cpu->thread_kicked = true;
  1525. err = pthread_kill(cpu->thread->thread, SIG_IPI);
  1526. if (err && err != ESRCH) {
  1527. fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
  1528. exit(1);
  1529. }
  1530. #else /* _WIN32 */
  1531. if (!qemu_cpu_is_self(cpu)) {
  1532. if (whpx_enabled()) {
  1533. whpx_vcpu_kick(cpu);
  1534. } else if (!QueueUserAPC(dummy_apc_func, cpu->hThread, 0)) {
  1535. fprintf(stderr, "%s: QueueUserAPC failed with error %lu\n",
  1536. __func__, GetLastError());
  1537. exit(1);
  1538. }
  1539. }
  1540. #endif
  1541. }
  1542. void qemu_cpu_kick(CPUState *cpu)
  1543. {
  1544. qemu_cond_broadcast(cpu->halt_cond);
  1545. if (tcg_enabled()) {
  1546. cpu_exit(cpu);
  1547. /* NOP unless doing single-thread RR */
  1548. qemu_cpu_kick_rr_cpu();
  1549. } else {
  1550. if (hax_enabled()) {
  1551. /*
  1552. * FIXME: race condition with the exit_request check in
  1553. * hax_vcpu_hax_exec
  1554. */
  1555. cpu->exit_request = 1;
  1556. }
  1557. qemu_cpu_kick_thread(cpu);
  1558. }
  1559. }
  1560. void qemu_cpu_kick_self(void)
  1561. {
  1562. assert(current_cpu);
  1563. qemu_cpu_kick_thread(current_cpu);
  1564. }
  1565. bool qemu_cpu_is_self(CPUState *cpu)
  1566. {
  1567. return qemu_thread_is_self(cpu->thread);
  1568. }
  1569. bool qemu_in_vcpu_thread(void)
  1570. {
  1571. return current_cpu && qemu_cpu_is_self(current_cpu);
  1572. }
  1573. static __thread bool iothread_locked = false;
  1574. bool qemu_mutex_iothread_locked(void)
  1575. {
  1576. return iothread_locked;
  1577. }
  1578. /*
  1579. * The BQL is taken from so many places that it is worth profiling the
  1580. * callers directly, instead of funneling them all through a single function.
  1581. */
  1582. void qemu_mutex_lock_iothread_impl(const char *file, int line)
  1583. {
  1584. QemuMutexLockFunc bql_lock = atomic_read(&qemu_bql_mutex_lock_func);
  1585. g_assert(!qemu_mutex_iothread_locked());
  1586. bql_lock(&qemu_global_mutex, file, line);
  1587. iothread_locked = true;
  1588. }
  1589. void qemu_mutex_unlock_iothread(void)
  1590. {
  1591. g_assert(qemu_mutex_iothread_locked());
  1592. iothread_locked = false;
  1593. qemu_mutex_unlock(&qemu_global_mutex);
  1594. }
  1595. static bool all_vcpus_paused(void)
  1596. {
  1597. CPUState *cpu;
  1598. CPU_FOREACH(cpu) {
  1599. if (!cpu->stopped) {
  1600. return false;
  1601. }
  1602. }
  1603. return true;
  1604. }
  1605. void pause_all_vcpus(void)
  1606. {
  1607. CPUState *cpu;
  1608. qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
  1609. CPU_FOREACH(cpu) {
  1610. if (qemu_cpu_is_self(cpu)) {
  1611. qemu_cpu_stop(cpu, true);
  1612. } else {
  1613. cpu->stop = true;
  1614. qemu_cpu_kick(cpu);
  1615. }
  1616. }
  1617. /* We need to drop the replay_lock so any vCPU threads woken up
  1618. * can finish their replay tasks
  1619. */
  1620. replay_mutex_unlock();
  1621. while (!all_vcpus_paused()) {
  1622. qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
  1623. CPU_FOREACH(cpu) {
  1624. qemu_cpu_kick(cpu);
  1625. }
  1626. }
  1627. qemu_mutex_unlock_iothread();
  1628. replay_mutex_lock();
  1629. qemu_mutex_lock_iothread();
  1630. }
  1631. void cpu_resume(CPUState *cpu)
  1632. {
  1633. cpu->stop = false;
  1634. cpu->stopped = false;
  1635. qemu_cpu_kick(cpu);
  1636. }
  1637. void resume_all_vcpus(void)
  1638. {
  1639. CPUState *cpu;
  1640. qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
  1641. CPU_FOREACH(cpu) {
  1642. cpu_resume(cpu);
  1643. }
  1644. }
  1645. void cpu_remove_sync(CPUState *cpu)
  1646. {
  1647. cpu->stop = true;
  1648. cpu->unplug = true;
  1649. qemu_cpu_kick(cpu);
  1650. qemu_mutex_unlock_iothread();
  1651. qemu_thread_join(cpu->thread);
  1652. qemu_mutex_lock_iothread();
  1653. }
  1654. /* For temporary buffers for forming a name */
  1655. #define VCPU_THREAD_NAME_SIZE 16
  1656. static void qemu_tcg_init_vcpu(CPUState *cpu)
  1657. {
  1658. char thread_name[VCPU_THREAD_NAME_SIZE];
  1659. static QemuCond *single_tcg_halt_cond;
  1660. static QemuThread *single_tcg_cpu_thread;
  1661. static int tcg_region_inited;
  1662. assert(tcg_enabled());
  1663. /*
  1664. * Initialize TCG regions--once. Now is a good time, because:
  1665. * (1) TCG's init context, prologue and target globals have been set up.
  1666. * (2) qemu_tcg_mttcg_enabled() works now (TCG init code runs before the
  1667. * -accel flag is processed, so the check doesn't work then).
  1668. */
  1669. if (!tcg_region_inited) {
  1670. tcg_region_inited = 1;
  1671. tcg_region_init();
  1672. }
  1673. if (qemu_tcg_mttcg_enabled() || !single_tcg_cpu_thread) {
  1674. cpu->thread = g_malloc0(sizeof(QemuThread));
  1675. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1676. qemu_cond_init(cpu->halt_cond);
  1677. if (qemu_tcg_mttcg_enabled()) {
  1678. /* create a thread per vCPU with TCG (MTTCG) */
  1679. parallel_cpus = true;
  1680. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
  1681. cpu->cpu_index);
  1682. qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
  1683. cpu, QEMU_THREAD_JOINABLE);
  1684. } else {
  1685. /* share a single thread for all cpus with TCG */
  1686. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "ALL CPUs/TCG");
  1687. qemu_thread_create(cpu->thread, thread_name,
  1688. qemu_tcg_rr_cpu_thread_fn,
  1689. cpu, QEMU_THREAD_JOINABLE);
  1690. single_tcg_halt_cond = cpu->halt_cond;
  1691. single_tcg_cpu_thread = cpu->thread;
  1692. }
  1693. #ifdef _WIN32
  1694. cpu->hThread = qemu_thread_get_handle(cpu->thread);
  1695. #endif
  1696. } else {
  1697. /* For non-MTTCG cases we share the thread */
  1698. cpu->thread = single_tcg_cpu_thread;
  1699. cpu->halt_cond = single_tcg_halt_cond;
  1700. cpu->thread_id = first_cpu->thread_id;
  1701. cpu->can_do_io = 1;
  1702. cpu->created = true;
  1703. }
  1704. }
  1705. static void qemu_hax_start_vcpu(CPUState *cpu)
  1706. {
  1707. char thread_name[VCPU_THREAD_NAME_SIZE];
  1708. cpu->thread = g_malloc0(sizeof(QemuThread));
  1709. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1710. qemu_cond_init(cpu->halt_cond);
  1711. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HAX",
  1712. cpu->cpu_index);
  1713. qemu_thread_create(cpu->thread, thread_name, qemu_hax_cpu_thread_fn,
  1714. cpu, QEMU_THREAD_JOINABLE);
  1715. #ifdef _WIN32
  1716. cpu->hThread = qemu_thread_get_handle(cpu->thread);
  1717. #endif
  1718. }
  1719. static void qemu_kvm_start_vcpu(CPUState *cpu)
  1720. {
  1721. char thread_name[VCPU_THREAD_NAME_SIZE];
  1722. cpu->thread = g_malloc0(sizeof(QemuThread));
  1723. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1724. qemu_cond_init(cpu->halt_cond);
  1725. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
  1726. cpu->cpu_index);
  1727. qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
  1728. cpu, QEMU_THREAD_JOINABLE);
  1729. }
  1730. static void qemu_hvf_start_vcpu(CPUState *cpu)
  1731. {
  1732. char thread_name[VCPU_THREAD_NAME_SIZE];
  1733. /* HVF currently does not support TCG, and only runs in
  1734. * unrestricted-guest mode. */
  1735. assert(hvf_enabled());
  1736. cpu->thread = g_malloc0(sizeof(QemuThread));
  1737. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1738. qemu_cond_init(cpu->halt_cond);
  1739. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/HVF",
  1740. cpu->cpu_index);
  1741. qemu_thread_create(cpu->thread, thread_name, qemu_hvf_cpu_thread_fn,
  1742. cpu, QEMU_THREAD_JOINABLE);
  1743. }
  1744. static void qemu_whpx_start_vcpu(CPUState *cpu)
  1745. {
  1746. char thread_name[VCPU_THREAD_NAME_SIZE];
  1747. cpu->thread = g_malloc0(sizeof(QemuThread));
  1748. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1749. qemu_cond_init(cpu->halt_cond);
  1750. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/WHPX",
  1751. cpu->cpu_index);
  1752. qemu_thread_create(cpu->thread, thread_name, qemu_whpx_cpu_thread_fn,
  1753. cpu, QEMU_THREAD_JOINABLE);
  1754. #ifdef _WIN32
  1755. cpu->hThread = qemu_thread_get_handle(cpu->thread);
  1756. #endif
  1757. }
  1758. static void qemu_dummy_start_vcpu(CPUState *cpu)
  1759. {
  1760. char thread_name[VCPU_THREAD_NAME_SIZE];
  1761. cpu->thread = g_malloc0(sizeof(QemuThread));
  1762. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1763. qemu_cond_init(cpu->halt_cond);
  1764. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
  1765. cpu->cpu_index);
  1766. qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
  1767. QEMU_THREAD_JOINABLE);
  1768. }
  1769. void qemu_init_vcpu(CPUState *cpu)
  1770. {
  1771. cpu->nr_cores = smp_cores;
  1772. cpu->nr_threads = smp_threads;
  1773. cpu->stopped = true;
  1774. cpu->random_seed = qemu_guest_random_seed_thread_part1();
  1775. if (!cpu->as) {
  1776. /* If the target cpu hasn't set up any address spaces itself,
  1777. * give it the default one.
  1778. */
  1779. cpu->num_ases = 1;
  1780. cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
  1781. }
  1782. if (kvm_enabled()) {
  1783. qemu_kvm_start_vcpu(cpu);
  1784. } else if (hax_enabled()) {
  1785. qemu_hax_start_vcpu(cpu);
  1786. } else if (hvf_enabled()) {
  1787. qemu_hvf_start_vcpu(cpu);
  1788. } else if (tcg_enabled()) {
  1789. qemu_tcg_init_vcpu(cpu);
  1790. } else if (whpx_enabled()) {
  1791. qemu_whpx_start_vcpu(cpu);
  1792. } else {
  1793. qemu_dummy_start_vcpu(cpu);
  1794. }
  1795. while (!cpu->created) {
  1796. qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
  1797. }
  1798. }
  1799. void cpu_stop_current(void)
  1800. {
  1801. if (current_cpu) {
  1802. current_cpu->stop = true;
  1803. cpu_exit(current_cpu);
  1804. }
  1805. }
  1806. int vm_stop(RunState state)
  1807. {
  1808. if (qemu_in_vcpu_thread()) {
  1809. qemu_system_vmstop_request_prepare();
  1810. qemu_system_vmstop_request(state);
  1811. /*
  1812. * FIXME: should not return to device code in case
  1813. * vm_stop() has been requested.
  1814. */
  1815. cpu_stop_current();
  1816. return 0;
  1817. }
  1818. return do_vm_stop(state, true);
  1819. }
  1820. /**
  1821. * Prepare for (re)starting the VM.
  1822. * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
  1823. * running or in case of an error condition), 0 otherwise.
  1824. */
  1825. int vm_prepare_start(void)
  1826. {
  1827. RunState requested;
  1828. qemu_vmstop_requested(&requested);
  1829. if (runstate_is_running() && requested == RUN_STATE__MAX) {
  1830. return -1;
  1831. }
  1832. /* Ensure that a STOP/RESUME pair of events is emitted if a
  1833. * vmstop request was pending. The BLOCK_IO_ERROR event, for
  1834. * example, according to documentation is always followed by
  1835. * the STOP event.
  1836. */
  1837. if (runstate_is_running()) {
  1838. qapi_event_send_stop();
  1839. qapi_event_send_resume();
  1840. return -1;
  1841. }
  1842. /* We are sending this now, but the CPUs will be resumed shortly later */
  1843. qapi_event_send_resume();
  1844. replay_enable_events();
  1845. cpu_enable_ticks();
  1846. runstate_set(RUN_STATE_RUNNING);
  1847. vm_state_notify(1, RUN_STATE_RUNNING);
  1848. return 0;
  1849. }
  1850. void vm_start(void)
  1851. {
  1852. if (!vm_prepare_start()) {
  1853. resume_all_vcpus();
  1854. }
  1855. }
  1856. /* does a state transition even if the VM is already stopped,
  1857. current state is forgotten forever */
  1858. int vm_stop_force_state(RunState state)
  1859. {
  1860. if (runstate_is_running()) {
  1861. return vm_stop(state);
  1862. } else {
  1863. runstate_set(state);
  1864. bdrv_drain_all();
  1865. /* Make sure to return an error if the flush in a previous vm_stop()
  1866. * failed. */
  1867. return bdrv_flush_all();
  1868. }
  1869. }
  1870. void list_cpus(const char *optarg)
  1871. {
  1872. /* XXX: implement xxx_cpu_list for targets that still miss it */
  1873. #if defined(cpu_list)
  1874. cpu_list();
  1875. #endif
  1876. }
  1877. CpuInfoList *qmp_query_cpus(Error **errp)
  1878. {
  1879. MachineState *ms = MACHINE(qdev_get_machine());
  1880. MachineClass *mc = MACHINE_GET_CLASS(ms);
  1881. CpuInfoList *head = NULL, *cur_item = NULL;
  1882. CPUState *cpu;
  1883. CPU_FOREACH(cpu) {
  1884. CpuInfoList *info;
  1885. #if defined(TARGET_I386)
  1886. X86CPU *x86_cpu = X86_CPU(cpu);
  1887. CPUX86State *env = &x86_cpu->env;
  1888. #elif defined(TARGET_PPC)
  1889. PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
  1890. CPUPPCState *env = &ppc_cpu->env;
  1891. #elif defined(TARGET_SPARC)
  1892. SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
  1893. CPUSPARCState *env = &sparc_cpu->env;
  1894. #elif defined(TARGET_RISCV)
  1895. RISCVCPU *riscv_cpu = RISCV_CPU(cpu);
  1896. CPURISCVState *env = &riscv_cpu->env;
  1897. #elif defined(TARGET_MIPS)
  1898. MIPSCPU *mips_cpu = MIPS_CPU(cpu);
  1899. CPUMIPSState *env = &mips_cpu->env;
  1900. #elif defined(TARGET_TRICORE)
  1901. TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
  1902. CPUTriCoreState *env = &tricore_cpu->env;
  1903. #elif defined(TARGET_S390X)
  1904. S390CPU *s390_cpu = S390_CPU(cpu);
  1905. CPUS390XState *env = &s390_cpu->env;
  1906. #endif
  1907. cpu_synchronize_state(cpu);
  1908. info = g_malloc0(sizeof(*info));
  1909. info->value = g_malloc0(sizeof(*info->value));
  1910. info->value->CPU = cpu->cpu_index;
  1911. info->value->current = (cpu == first_cpu);
  1912. info->value->halted = cpu->halted;
  1913. info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
  1914. info->value->thread_id = cpu->thread_id;
  1915. #if defined(TARGET_I386)
  1916. info->value->arch = CPU_INFO_ARCH_X86;
  1917. info->value->u.x86.pc = env->eip + env->segs[R_CS].base;
  1918. #elif defined(TARGET_PPC)
  1919. info->value->arch = CPU_INFO_ARCH_PPC;
  1920. info->value->u.ppc.nip = env->nip;
  1921. #elif defined(TARGET_SPARC)
  1922. info->value->arch = CPU_INFO_ARCH_SPARC;
  1923. info->value->u.q_sparc.pc = env->pc;
  1924. info->value->u.q_sparc.npc = env->npc;
  1925. #elif defined(TARGET_MIPS)
  1926. info->value->arch = CPU_INFO_ARCH_MIPS;
  1927. info->value->u.q_mips.PC = env->active_tc.PC;
  1928. #elif defined(TARGET_TRICORE)
  1929. info->value->arch = CPU_INFO_ARCH_TRICORE;
  1930. info->value->u.tricore.PC = env->PC;
  1931. #elif defined(TARGET_S390X)
  1932. info->value->arch = CPU_INFO_ARCH_S390;
  1933. info->value->u.s390.cpu_state = env->cpu_state;
  1934. #elif defined(TARGET_RISCV)
  1935. info->value->arch = CPU_INFO_ARCH_RISCV;
  1936. info->value->u.riscv.pc = env->pc;
  1937. #else
  1938. info->value->arch = CPU_INFO_ARCH_OTHER;
  1939. #endif
  1940. info->value->has_props = !!mc->cpu_index_to_instance_props;
  1941. if (info->value->has_props) {
  1942. CpuInstanceProperties *props;
  1943. props = g_malloc0(sizeof(*props));
  1944. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  1945. info->value->props = props;
  1946. }
  1947. /* XXX: waiting for the qapi to support GSList */
  1948. if (!cur_item) {
  1949. head = cur_item = info;
  1950. } else {
  1951. cur_item->next = info;
  1952. cur_item = info;
  1953. }
  1954. }
  1955. return head;
  1956. }
  1957. static CpuInfoArch sysemu_target_to_cpuinfo_arch(SysEmuTarget target)
  1958. {
  1959. /*
  1960. * The @SysEmuTarget -> @CpuInfoArch mapping below is based on the
  1961. * TARGET_ARCH -> TARGET_BASE_ARCH mapping in the "configure" script.
  1962. */
  1963. switch (target) {
  1964. case SYS_EMU_TARGET_I386:
  1965. case SYS_EMU_TARGET_X86_64:
  1966. return CPU_INFO_ARCH_X86;
  1967. case SYS_EMU_TARGET_PPC:
  1968. case SYS_EMU_TARGET_PPC64:
  1969. return CPU_INFO_ARCH_PPC;
  1970. case SYS_EMU_TARGET_SPARC:
  1971. case SYS_EMU_TARGET_SPARC64:
  1972. return CPU_INFO_ARCH_SPARC;
  1973. case SYS_EMU_TARGET_MIPS:
  1974. case SYS_EMU_TARGET_MIPSEL:
  1975. case SYS_EMU_TARGET_MIPS64:
  1976. case SYS_EMU_TARGET_MIPS64EL:
  1977. return CPU_INFO_ARCH_MIPS;
  1978. case SYS_EMU_TARGET_TRICORE:
  1979. return CPU_INFO_ARCH_TRICORE;
  1980. case SYS_EMU_TARGET_S390X:
  1981. return CPU_INFO_ARCH_S390;
  1982. case SYS_EMU_TARGET_RISCV32:
  1983. case SYS_EMU_TARGET_RISCV64:
  1984. return CPU_INFO_ARCH_RISCV;
  1985. default:
  1986. return CPU_INFO_ARCH_OTHER;
  1987. }
  1988. }
  1989. static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
  1990. {
  1991. #ifdef TARGET_S390X
  1992. S390CPU *s390_cpu = S390_CPU(cpu);
  1993. CPUS390XState *env = &s390_cpu->env;
  1994. info->cpu_state = env->cpu_state;
  1995. #else
  1996. abort();
  1997. #endif
  1998. }
  1999. /*
  2000. * fast means: we NEVER interrupt vCPU threads to retrieve
  2001. * information from KVM.
  2002. */
  2003. CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
  2004. {
  2005. MachineState *ms = MACHINE(qdev_get_machine());
  2006. MachineClass *mc = MACHINE_GET_CLASS(ms);
  2007. CpuInfoFastList *head = NULL, *cur_item = NULL;
  2008. SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
  2009. -1, &error_abort);
  2010. CPUState *cpu;
  2011. CPU_FOREACH(cpu) {
  2012. CpuInfoFastList *info = g_malloc0(sizeof(*info));
  2013. info->value = g_malloc0(sizeof(*info->value));
  2014. info->value->cpu_index = cpu->cpu_index;
  2015. info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
  2016. info->value->thread_id = cpu->thread_id;
  2017. info->value->has_props = !!mc->cpu_index_to_instance_props;
  2018. if (info->value->has_props) {
  2019. CpuInstanceProperties *props;
  2020. props = g_malloc0(sizeof(*props));
  2021. *props = mc->cpu_index_to_instance_props(ms, cpu->cpu_index);
  2022. info->value->props = props;
  2023. }
  2024. info->value->arch = sysemu_target_to_cpuinfo_arch(target);
  2025. info->value->target = target;
  2026. if (target == SYS_EMU_TARGET_S390X) {
  2027. cpustate_to_cpuinfo_s390(&info->value->u.s390x, cpu);
  2028. }
  2029. if (!cur_item) {
  2030. head = cur_item = info;
  2031. } else {
  2032. cur_item->next = info;
  2033. cur_item = info;
  2034. }
  2035. }
  2036. return head;
  2037. }
  2038. void qmp_memsave(int64_t addr, int64_t size, const char *filename,
  2039. bool has_cpu, int64_t cpu_index, Error **errp)
  2040. {
  2041. FILE *f;
  2042. uint32_t l;
  2043. CPUState *cpu;
  2044. uint8_t buf[1024];
  2045. int64_t orig_addr = addr, orig_size = size;
  2046. if (!has_cpu) {
  2047. cpu_index = 0;
  2048. }
  2049. cpu = qemu_get_cpu(cpu_index);
  2050. if (cpu == NULL) {
  2051. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
  2052. "a CPU number");
  2053. return;
  2054. }
  2055. f = fopen(filename, "wb");
  2056. if (!f) {
  2057. error_setg_file_open(errp, errno, filename);
  2058. return;
  2059. }
  2060. while (size != 0) {
  2061. l = sizeof(buf);
  2062. if (l > size)
  2063. l = size;
  2064. if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
  2065. error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
  2066. " specified", orig_addr, orig_size);
  2067. goto exit;
  2068. }
  2069. if (fwrite(buf, 1, l, f) != l) {
  2070. error_setg(errp, QERR_IO_ERROR);
  2071. goto exit;
  2072. }
  2073. addr += l;
  2074. size -= l;
  2075. }
  2076. exit:
  2077. fclose(f);
  2078. }
  2079. void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
  2080. Error **errp)
  2081. {
  2082. FILE *f;
  2083. uint32_t l;
  2084. uint8_t buf[1024];
  2085. f = fopen(filename, "wb");
  2086. if (!f) {
  2087. error_setg_file_open(errp, errno, filename);
  2088. return;
  2089. }
  2090. while (size != 0) {
  2091. l = sizeof(buf);
  2092. if (l > size)
  2093. l = size;
  2094. cpu_physical_memory_read(addr, buf, l);
  2095. if (fwrite(buf, 1, l, f) != l) {
  2096. error_setg(errp, QERR_IO_ERROR);
  2097. goto exit;
  2098. }
  2099. addr += l;
  2100. size -= l;
  2101. }
  2102. exit:
  2103. fclose(f);
  2104. }
  2105. void qmp_inject_nmi(Error **errp)
  2106. {
  2107. nmi_monitor_handle(monitor_get_cpu_index(), errp);
  2108. }
  2109. void dump_drift_info(void)
  2110. {
  2111. if (!use_icount) {
  2112. return;
  2113. }
  2114. qemu_printf("Host - Guest clock %"PRIi64" ms\n",
  2115. (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
  2116. if (icount_align_option) {
  2117. qemu_printf("Max guest delay %"PRIi64" ms\n",
  2118. -max_delay / SCALE_MS);
  2119. qemu_printf("Max guest advance %"PRIi64" ms\n",
  2120. max_advance / SCALE_MS);
  2121. } else {
  2122. qemu_printf("Max guest delay NA\n");
  2123. qemu_printf("Max guest advance NA\n");
  2124. }
  2125. }