cpus.c 55 KB

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