2
0

cpus.c 53 KB

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