2
0

cpus.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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 "config-host.h"
  26. #include "monitor/monitor.h"
  27. #include "qapi/qmp/qerror.h"
  28. #include "sysemu/sysemu.h"
  29. #include "exec/gdbstub.h"
  30. #include "sysemu/dma.h"
  31. #include "sysemu/kvm.h"
  32. #include "qmp-commands.h"
  33. #include "qemu/thread.h"
  34. #include "sysemu/cpus.h"
  35. #include "sysemu/qtest.h"
  36. #include "qemu/main-loop.h"
  37. #include "qemu/bitmap.h"
  38. #include "qemu/seqlock.h"
  39. #include "qapi-event.h"
  40. #include "hw/nmi.h"
  41. #ifndef _WIN32
  42. #include "qemu/compatfd.h"
  43. #endif
  44. #ifdef CONFIG_LINUX
  45. #include <sys/prctl.h>
  46. #ifndef PR_MCE_KILL
  47. #define PR_MCE_KILL 33
  48. #endif
  49. #ifndef PR_MCE_KILL_SET
  50. #define PR_MCE_KILL_SET 1
  51. #endif
  52. #ifndef PR_MCE_KILL_EARLY
  53. #define PR_MCE_KILL_EARLY 1
  54. #endif
  55. #endif /* CONFIG_LINUX */
  56. static CPUState *next_cpu;
  57. int64_t max_delay;
  58. int64_t max_advance;
  59. bool cpu_is_stopped(CPUState *cpu)
  60. {
  61. return cpu->stopped || !runstate_is_running();
  62. }
  63. static bool cpu_thread_is_idle(CPUState *cpu)
  64. {
  65. if (cpu->stop || cpu->queued_work_first) {
  66. return false;
  67. }
  68. if (cpu_is_stopped(cpu)) {
  69. return true;
  70. }
  71. if (!cpu->halted || cpu_has_work(cpu) ||
  72. kvm_halt_in_kernel()) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. static bool all_cpu_threads_idle(void)
  78. {
  79. CPUState *cpu;
  80. CPU_FOREACH(cpu) {
  81. if (!cpu_thread_is_idle(cpu)) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. /***********************************************************/
  88. /* guest cycle counter */
  89. /* Protected by TimersState seqlock */
  90. static int64_t vm_clock_warp_start = -1;
  91. /* Conversion factor from emulated instructions to virtual clock ticks. */
  92. static int icount_time_shift;
  93. /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
  94. #define MAX_ICOUNT_SHIFT 10
  95. static QEMUTimer *icount_rt_timer;
  96. static QEMUTimer *icount_vm_timer;
  97. static QEMUTimer *icount_warp_timer;
  98. typedef struct TimersState {
  99. /* Protected by BQL. */
  100. int64_t cpu_ticks_prev;
  101. int64_t cpu_ticks_offset;
  102. /* cpu_clock_offset can be read out of BQL, so protect it with
  103. * this lock.
  104. */
  105. QemuSeqLock vm_clock_seqlock;
  106. int64_t cpu_clock_offset;
  107. int32_t cpu_ticks_enabled;
  108. int64_t dummy;
  109. /* Compensate for varying guest execution speed. */
  110. int64_t qemu_icount_bias;
  111. /* Only written by TCG thread */
  112. int64_t qemu_icount;
  113. } TimersState;
  114. static TimersState timers_state;
  115. /* Return the virtual CPU time, based on the instruction counter. */
  116. static int64_t cpu_get_icount_locked(void)
  117. {
  118. int64_t icount;
  119. CPUState *cpu = current_cpu;
  120. icount = timers_state.qemu_icount;
  121. if (cpu) {
  122. if (!cpu_can_do_io(cpu)) {
  123. fprintf(stderr, "Bad clock read\n");
  124. }
  125. icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
  126. }
  127. return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
  128. }
  129. int64_t cpu_get_icount(void)
  130. {
  131. int64_t icount;
  132. unsigned start;
  133. do {
  134. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  135. icount = cpu_get_icount_locked();
  136. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  137. return icount;
  138. }
  139. int64_t cpu_icount_to_ns(int64_t icount)
  140. {
  141. return icount << icount_time_shift;
  142. }
  143. /* return the host CPU cycle counter and handle stop/restart */
  144. /* Caller must hold the BQL */
  145. int64_t cpu_get_ticks(void)
  146. {
  147. int64_t ticks;
  148. if (use_icount) {
  149. return cpu_get_icount();
  150. }
  151. ticks = timers_state.cpu_ticks_offset;
  152. if (timers_state.cpu_ticks_enabled) {
  153. ticks += cpu_get_real_ticks();
  154. }
  155. if (timers_state.cpu_ticks_prev > ticks) {
  156. /* Note: non increasing ticks may happen if the host uses
  157. software suspend */
  158. timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
  159. ticks = timers_state.cpu_ticks_prev;
  160. }
  161. timers_state.cpu_ticks_prev = ticks;
  162. return ticks;
  163. }
  164. static int64_t cpu_get_clock_locked(void)
  165. {
  166. int64_t ticks;
  167. ticks = timers_state.cpu_clock_offset;
  168. if (timers_state.cpu_ticks_enabled) {
  169. ticks += get_clock();
  170. }
  171. return ticks;
  172. }
  173. /* return the host CPU monotonic timer and handle stop/restart */
  174. int64_t cpu_get_clock(void)
  175. {
  176. int64_t ti;
  177. unsigned start;
  178. do {
  179. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  180. ti = cpu_get_clock_locked();
  181. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  182. return ti;
  183. }
  184. /* return the offset between the host clock and virtual CPU clock */
  185. int64_t cpu_get_clock_offset(void)
  186. {
  187. int64_t ti;
  188. unsigned start;
  189. do {
  190. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  191. ti = timers_state.cpu_clock_offset;
  192. if (!timers_state.cpu_ticks_enabled) {
  193. ti -= get_clock();
  194. }
  195. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  196. return -ti;
  197. }
  198. /* enable cpu_get_ticks()
  199. * Caller must hold BQL which server as mutex for vm_clock_seqlock.
  200. */
  201. void cpu_enable_ticks(void)
  202. {
  203. /* Here, the really thing protected by seqlock is cpu_clock_offset. */
  204. seqlock_write_lock(&timers_state.vm_clock_seqlock);
  205. if (!timers_state.cpu_ticks_enabled) {
  206. timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
  207. timers_state.cpu_clock_offset -= get_clock();
  208. timers_state.cpu_ticks_enabled = 1;
  209. }
  210. seqlock_write_unlock(&timers_state.vm_clock_seqlock);
  211. }
  212. /* disable cpu_get_ticks() : the clock is stopped. You must not call
  213. * cpu_get_ticks() after that.
  214. * Caller must hold BQL which server as mutex for vm_clock_seqlock.
  215. */
  216. void cpu_disable_ticks(void)
  217. {
  218. /* Here, the really thing protected by seqlock is cpu_clock_offset. */
  219. seqlock_write_lock(&timers_state.vm_clock_seqlock);
  220. if (timers_state.cpu_ticks_enabled) {
  221. timers_state.cpu_ticks_offset += cpu_get_real_ticks();
  222. timers_state.cpu_clock_offset = cpu_get_clock_locked();
  223. timers_state.cpu_ticks_enabled = 0;
  224. }
  225. seqlock_write_unlock(&timers_state.vm_clock_seqlock);
  226. }
  227. /* Correlation between real and virtual time is always going to be
  228. fairly approximate, so ignore small variation.
  229. When the guest is idle real and virtual time will be aligned in
  230. the IO wait loop. */
  231. #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
  232. static void icount_adjust(void)
  233. {
  234. int64_t cur_time;
  235. int64_t cur_icount;
  236. int64_t delta;
  237. /* Protected by TimersState mutex. */
  238. static int64_t last_delta;
  239. /* If the VM is not running, then do nothing. */
  240. if (!runstate_is_running()) {
  241. return;
  242. }
  243. seqlock_write_lock(&timers_state.vm_clock_seqlock);
  244. cur_time = cpu_get_clock_locked();
  245. cur_icount = cpu_get_icount_locked();
  246. delta = cur_icount - cur_time;
  247. /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
  248. if (delta > 0
  249. && last_delta + ICOUNT_WOBBLE < delta * 2
  250. && icount_time_shift > 0) {
  251. /* The guest is getting too far ahead. Slow time down. */
  252. icount_time_shift--;
  253. }
  254. if (delta < 0
  255. && last_delta - ICOUNT_WOBBLE > delta * 2
  256. && icount_time_shift < MAX_ICOUNT_SHIFT) {
  257. /* The guest is getting too far behind. Speed time up. */
  258. icount_time_shift++;
  259. }
  260. last_delta = delta;
  261. timers_state.qemu_icount_bias = cur_icount
  262. - (timers_state.qemu_icount << icount_time_shift);
  263. seqlock_write_unlock(&timers_state.vm_clock_seqlock);
  264. }
  265. static void icount_adjust_rt(void *opaque)
  266. {
  267. timer_mod(icount_rt_timer,
  268. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  269. icount_adjust();
  270. }
  271. static void icount_adjust_vm(void *opaque)
  272. {
  273. timer_mod(icount_vm_timer,
  274. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  275. get_ticks_per_sec() / 10);
  276. icount_adjust();
  277. }
  278. static int64_t qemu_icount_round(int64_t count)
  279. {
  280. return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
  281. }
  282. static void icount_warp_rt(void *opaque)
  283. {
  284. /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
  285. * changes from -1 to another value, so the race here is okay.
  286. */
  287. if (atomic_read(&vm_clock_warp_start) == -1) {
  288. return;
  289. }
  290. seqlock_write_lock(&timers_state.vm_clock_seqlock);
  291. if (runstate_is_running()) {
  292. int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  293. int64_t warp_delta;
  294. warp_delta = clock - vm_clock_warp_start;
  295. if (use_icount == 2) {
  296. /*
  297. * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
  298. * far ahead of real time.
  299. */
  300. int64_t cur_time = cpu_get_clock_locked();
  301. int64_t cur_icount = cpu_get_icount_locked();
  302. int64_t delta = cur_time - cur_icount;
  303. warp_delta = MIN(warp_delta, delta);
  304. }
  305. timers_state.qemu_icount_bias += warp_delta;
  306. }
  307. vm_clock_warp_start = -1;
  308. seqlock_write_unlock(&timers_state.vm_clock_seqlock);
  309. if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
  310. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  311. }
  312. }
  313. void qtest_clock_warp(int64_t dest)
  314. {
  315. int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  316. AioContext *aio_context;
  317. assert(qtest_enabled());
  318. aio_context = qemu_get_aio_context();
  319. while (clock < dest) {
  320. int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  321. int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
  322. seqlock_write_lock(&timers_state.vm_clock_seqlock);
  323. timers_state.qemu_icount_bias += warp;
  324. seqlock_write_unlock(&timers_state.vm_clock_seqlock);
  325. qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
  326. timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
  327. clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  328. }
  329. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  330. }
  331. void qemu_clock_warp(QEMUClockType type)
  332. {
  333. int64_t clock;
  334. int64_t deadline;
  335. /*
  336. * There are too many global variables to make the "warp" behavior
  337. * applicable to other clocks. But a clock argument removes the
  338. * need for if statements all over the place.
  339. */
  340. if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
  341. return;
  342. }
  343. /*
  344. * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
  345. * This ensures that the deadline for the timer is computed correctly below.
  346. * This also makes sure that the insn counter is synchronized before the
  347. * CPU starts running, in case the CPU is woken by an event other than
  348. * the earliest QEMU_CLOCK_VIRTUAL timer.
  349. */
  350. icount_warp_rt(NULL);
  351. timer_del(icount_warp_timer);
  352. if (!all_cpu_threads_idle()) {
  353. return;
  354. }
  355. if (qtest_enabled()) {
  356. /* When testing, qtest commands advance icount. */
  357. return;
  358. }
  359. /* We want to use the earliest deadline from ALL vm_clocks */
  360. clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
  361. deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  362. if (deadline < 0) {
  363. return;
  364. }
  365. if (deadline > 0) {
  366. /*
  367. * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
  368. * sleep. Otherwise, the CPU might be waiting for a future timer
  369. * interrupt to wake it up, but the interrupt never comes because
  370. * the vCPU isn't running any insns and thus doesn't advance the
  371. * QEMU_CLOCK_VIRTUAL.
  372. *
  373. * An extreme solution for this problem would be to never let VCPUs
  374. * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL
  375. * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL
  376. * event. Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL
  377. * after some e"real" time, (related to the time left until the next
  378. * event) has passed. The QEMU_CLOCK_REALTIME timer will do this.
  379. * This avoids that the warps are visible externally; for example,
  380. * you will not be sending network packets continuously instead of
  381. * every 100ms.
  382. */
  383. seqlock_write_lock(&timers_state.vm_clock_seqlock);
  384. if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
  385. vm_clock_warp_start = clock;
  386. }
  387. seqlock_write_unlock(&timers_state.vm_clock_seqlock);
  388. timer_mod_anticipate(icount_warp_timer, clock + deadline);
  389. } else if (deadline == 0) {
  390. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  391. }
  392. }
  393. static bool icount_state_needed(void *opaque)
  394. {
  395. return use_icount;
  396. }
  397. /*
  398. * This is a subsection for icount migration.
  399. */
  400. static const VMStateDescription icount_vmstate_timers = {
  401. .name = "timer/icount",
  402. .version_id = 1,
  403. .minimum_version_id = 1,
  404. .fields = (VMStateField[]) {
  405. VMSTATE_INT64(qemu_icount_bias, TimersState),
  406. VMSTATE_INT64(qemu_icount, TimersState),
  407. VMSTATE_END_OF_LIST()
  408. }
  409. };
  410. static const VMStateDescription vmstate_timers = {
  411. .name = "timer",
  412. .version_id = 2,
  413. .minimum_version_id = 1,
  414. .fields = (VMStateField[]) {
  415. VMSTATE_INT64(cpu_ticks_offset, TimersState),
  416. VMSTATE_INT64(dummy, TimersState),
  417. VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
  418. VMSTATE_END_OF_LIST()
  419. },
  420. .subsections = (VMStateSubsection[]) {
  421. {
  422. .vmsd = &icount_vmstate_timers,
  423. .needed = icount_state_needed,
  424. }, {
  425. /* empty */
  426. }
  427. }
  428. };
  429. void cpu_ticks_init(void)
  430. {
  431. seqlock_init(&timers_state.vm_clock_seqlock, NULL);
  432. vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
  433. }
  434. void configure_icount(QemuOpts *opts, Error **errp)
  435. {
  436. const char *option;
  437. char *rem_str = NULL;
  438. option = qemu_opt_get(opts, "shift");
  439. if (!option) {
  440. if (qemu_opt_get(opts, "align") != NULL) {
  441. error_setg(errp, "Please specify shift option when using align");
  442. }
  443. return;
  444. }
  445. icount_align_option = qemu_opt_get_bool(opts, "align", false);
  446. icount_warp_timer = timer_new_ns(QEMU_CLOCK_REALTIME,
  447. icount_warp_rt, NULL);
  448. if (strcmp(option, "auto") != 0) {
  449. errno = 0;
  450. icount_time_shift = strtol(option, &rem_str, 0);
  451. if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
  452. error_setg(errp, "icount: Invalid shift value");
  453. }
  454. use_icount = 1;
  455. return;
  456. } else if (icount_align_option) {
  457. error_setg(errp, "shift=auto and align=on are incompatible");
  458. }
  459. use_icount = 2;
  460. /* 125MIPS seems a reasonable initial guess at the guest speed.
  461. It will be corrected fairly quickly anyway. */
  462. icount_time_shift = 3;
  463. /* Have both realtime and virtual time triggers for speed adjustment.
  464. The realtime trigger catches emulated time passing too slowly,
  465. the virtual time trigger catches emulated time passing too fast.
  466. Realtime triggers occur even when idle, so use them less frequently
  467. than VM triggers. */
  468. icount_rt_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  469. icount_adjust_rt, NULL);
  470. timer_mod(icount_rt_timer,
  471. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  472. icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  473. icount_adjust_vm, NULL);
  474. timer_mod(icount_vm_timer,
  475. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  476. get_ticks_per_sec() / 10);
  477. }
  478. /***********************************************************/
  479. void hw_error(const char *fmt, ...)
  480. {
  481. va_list ap;
  482. CPUState *cpu;
  483. va_start(ap, fmt);
  484. fprintf(stderr, "qemu: hardware error: ");
  485. vfprintf(stderr, fmt, ap);
  486. fprintf(stderr, "\n");
  487. CPU_FOREACH(cpu) {
  488. fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
  489. cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
  490. }
  491. va_end(ap);
  492. abort();
  493. }
  494. void cpu_synchronize_all_states(void)
  495. {
  496. CPUState *cpu;
  497. CPU_FOREACH(cpu) {
  498. cpu_synchronize_state(cpu);
  499. }
  500. }
  501. void cpu_synchronize_all_post_reset(void)
  502. {
  503. CPUState *cpu;
  504. CPU_FOREACH(cpu) {
  505. cpu_synchronize_post_reset(cpu);
  506. }
  507. }
  508. void cpu_synchronize_all_post_init(void)
  509. {
  510. CPUState *cpu;
  511. CPU_FOREACH(cpu) {
  512. cpu_synchronize_post_init(cpu);
  513. }
  514. }
  515. void cpu_clean_all_dirty(void)
  516. {
  517. CPUState *cpu;
  518. CPU_FOREACH(cpu) {
  519. cpu_clean_state(cpu);
  520. }
  521. }
  522. static int do_vm_stop(RunState state)
  523. {
  524. int ret = 0;
  525. if (runstate_is_running()) {
  526. cpu_disable_ticks();
  527. pause_all_vcpus();
  528. runstate_set(state);
  529. vm_state_notify(0, state);
  530. qapi_event_send_stop(&error_abort);
  531. }
  532. bdrv_drain_all();
  533. ret = bdrv_flush_all();
  534. return ret;
  535. }
  536. static bool cpu_can_run(CPUState *cpu)
  537. {
  538. if (cpu->stop) {
  539. return false;
  540. }
  541. if (cpu_is_stopped(cpu)) {
  542. return false;
  543. }
  544. return true;
  545. }
  546. static void cpu_handle_guest_debug(CPUState *cpu)
  547. {
  548. gdb_set_stop_cpu(cpu);
  549. qemu_system_debug_request();
  550. cpu->stopped = true;
  551. }
  552. static void cpu_signal(int sig)
  553. {
  554. if (current_cpu) {
  555. cpu_exit(current_cpu);
  556. }
  557. exit_request = 1;
  558. }
  559. #ifdef CONFIG_LINUX
  560. static void sigbus_reraise(void)
  561. {
  562. sigset_t set;
  563. struct sigaction action;
  564. memset(&action, 0, sizeof(action));
  565. action.sa_handler = SIG_DFL;
  566. if (!sigaction(SIGBUS, &action, NULL)) {
  567. raise(SIGBUS);
  568. sigemptyset(&set);
  569. sigaddset(&set, SIGBUS);
  570. sigprocmask(SIG_UNBLOCK, &set, NULL);
  571. }
  572. perror("Failed to re-raise SIGBUS!\n");
  573. abort();
  574. }
  575. static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
  576. void *ctx)
  577. {
  578. if (kvm_on_sigbus(siginfo->ssi_code,
  579. (void *)(intptr_t)siginfo->ssi_addr)) {
  580. sigbus_reraise();
  581. }
  582. }
  583. static void qemu_init_sigbus(void)
  584. {
  585. struct sigaction action;
  586. memset(&action, 0, sizeof(action));
  587. action.sa_flags = SA_SIGINFO;
  588. action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
  589. sigaction(SIGBUS, &action, NULL);
  590. prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
  591. }
  592. static void qemu_kvm_eat_signals(CPUState *cpu)
  593. {
  594. struct timespec ts = { 0, 0 };
  595. siginfo_t siginfo;
  596. sigset_t waitset;
  597. sigset_t chkset;
  598. int r;
  599. sigemptyset(&waitset);
  600. sigaddset(&waitset, SIG_IPI);
  601. sigaddset(&waitset, SIGBUS);
  602. do {
  603. r = sigtimedwait(&waitset, &siginfo, &ts);
  604. if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
  605. perror("sigtimedwait");
  606. exit(1);
  607. }
  608. switch (r) {
  609. case SIGBUS:
  610. if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
  611. sigbus_reraise();
  612. }
  613. break;
  614. default:
  615. break;
  616. }
  617. r = sigpending(&chkset);
  618. if (r == -1) {
  619. perror("sigpending");
  620. exit(1);
  621. }
  622. } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
  623. }
  624. #else /* !CONFIG_LINUX */
  625. static void qemu_init_sigbus(void)
  626. {
  627. }
  628. static void qemu_kvm_eat_signals(CPUState *cpu)
  629. {
  630. }
  631. #endif /* !CONFIG_LINUX */
  632. #ifndef _WIN32
  633. static void dummy_signal(int sig)
  634. {
  635. }
  636. static void qemu_kvm_init_cpu_signals(CPUState *cpu)
  637. {
  638. int r;
  639. sigset_t set;
  640. struct sigaction sigact;
  641. memset(&sigact, 0, sizeof(sigact));
  642. sigact.sa_handler = dummy_signal;
  643. sigaction(SIG_IPI, &sigact, NULL);
  644. pthread_sigmask(SIG_BLOCK, NULL, &set);
  645. sigdelset(&set, SIG_IPI);
  646. sigdelset(&set, SIGBUS);
  647. r = kvm_set_signal_mask(cpu, &set);
  648. if (r) {
  649. fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
  650. exit(1);
  651. }
  652. }
  653. static void qemu_tcg_init_cpu_signals(void)
  654. {
  655. sigset_t set;
  656. struct sigaction sigact;
  657. memset(&sigact, 0, sizeof(sigact));
  658. sigact.sa_handler = cpu_signal;
  659. sigaction(SIG_IPI, &sigact, NULL);
  660. sigemptyset(&set);
  661. sigaddset(&set, SIG_IPI);
  662. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  663. }
  664. #else /* _WIN32 */
  665. static void qemu_kvm_init_cpu_signals(CPUState *cpu)
  666. {
  667. abort();
  668. }
  669. static void qemu_tcg_init_cpu_signals(void)
  670. {
  671. }
  672. #endif /* _WIN32 */
  673. static QemuMutex qemu_global_mutex;
  674. static QemuCond qemu_io_proceeded_cond;
  675. static bool iothread_requesting_mutex;
  676. static QemuThread io_thread;
  677. static QemuThread *tcg_cpu_thread;
  678. static QemuCond *tcg_halt_cond;
  679. /* cpu creation */
  680. static QemuCond qemu_cpu_cond;
  681. /* system init */
  682. static QemuCond qemu_pause_cond;
  683. static QemuCond qemu_work_cond;
  684. void qemu_init_cpu_loop(void)
  685. {
  686. qemu_init_sigbus();
  687. qemu_cond_init(&qemu_cpu_cond);
  688. qemu_cond_init(&qemu_pause_cond);
  689. qemu_cond_init(&qemu_work_cond);
  690. qemu_cond_init(&qemu_io_proceeded_cond);
  691. qemu_mutex_init(&qemu_global_mutex);
  692. qemu_thread_get_self(&io_thread);
  693. }
  694. void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
  695. {
  696. struct qemu_work_item wi;
  697. if (qemu_cpu_is_self(cpu)) {
  698. func(data);
  699. return;
  700. }
  701. wi.func = func;
  702. wi.data = data;
  703. wi.free = false;
  704. if (cpu->queued_work_first == NULL) {
  705. cpu->queued_work_first = &wi;
  706. } else {
  707. cpu->queued_work_last->next = &wi;
  708. }
  709. cpu->queued_work_last = &wi;
  710. wi.next = NULL;
  711. wi.done = false;
  712. qemu_cpu_kick(cpu);
  713. while (!wi.done) {
  714. CPUState *self_cpu = current_cpu;
  715. qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
  716. current_cpu = self_cpu;
  717. }
  718. }
  719. void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
  720. {
  721. struct qemu_work_item *wi;
  722. if (qemu_cpu_is_self(cpu)) {
  723. func(data);
  724. return;
  725. }
  726. wi = g_malloc0(sizeof(struct qemu_work_item));
  727. wi->func = func;
  728. wi->data = data;
  729. wi->free = true;
  730. if (cpu->queued_work_first == NULL) {
  731. cpu->queued_work_first = wi;
  732. } else {
  733. cpu->queued_work_last->next = wi;
  734. }
  735. cpu->queued_work_last = wi;
  736. wi->next = NULL;
  737. wi->done = false;
  738. qemu_cpu_kick(cpu);
  739. }
  740. static void flush_queued_work(CPUState *cpu)
  741. {
  742. struct qemu_work_item *wi;
  743. if (cpu->queued_work_first == NULL) {
  744. return;
  745. }
  746. while ((wi = cpu->queued_work_first)) {
  747. cpu->queued_work_first = wi->next;
  748. wi->func(wi->data);
  749. wi->done = true;
  750. if (wi->free) {
  751. g_free(wi);
  752. }
  753. }
  754. cpu->queued_work_last = NULL;
  755. qemu_cond_broadcast(&qemu_work_cond);
  756. }
  757. static void qemu_wait_io_event_common(CPUState *cpu)
  758. {
  759. if (cpu->stop) {
  760. cpu->stop = false;
  761. cpu->stopped = true;
  762. qemu_cond_signal(&qemu_pause_cond);
  763. }
  764. flush_queued_work(cpu);
  765. cpu->thread_kicked = false;
  766. }
  767. static void qemu_tcg_wait_io_event(void)
  768. {
  769. CPUState *cpu;
  770. while (all_cpu_threads_idle()) {
  771. /* Start accounting real time to the virtual clock if the CPUs
  772. are idle. */
  773. qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
  774. qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
  775. }
  776. while (iothread_requesting_mutex) {
  777. qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
  778. }
  779. CPU_FOREACH(cpu) {
  780. qemu_wait_io_event_common(cpu);
  781. }
  782. }
  783. static void qemu_kvm_wait_io_event(CPUState *cpu)
  784. {
  785. while (cpu_thread_is_idle(cpu)) {
  786. qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
  787. }
  788. qemu_kvm_eat_signals(cpu);
  789. qemu_wait_io_event_common(cpu);
  790. }
  791. static void *qemu_kvm_cpu_thread_fn(void *arg)
  792. {
  793. CPUState *cpu = arg;
  794. int r;
  795. qemu_mutex_lock(&qemu_global_mutex);
  796. qemu_thread_get_self(cpu->thread);
  797. cpu->thread_id = qemu_get_thread_id();
  798. current_cpu = cpu;
  799. r = kvm_init_vcpu(cpu);
  800. if (r < 0) {
  801. fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
  802. exit(1);
  803. }
  804. qemu_kvm_init_cpu_signals(cpu);
  805. /* signal CPU creation */
  806. cpu->created = true;
  807. qemu_cond_signal(&qemu_cpu_cond);
  808. while (1) {
  809. if (cpu_can_run(cpu)) {
  810. r = kvm_cpu_exec(cpu);
  811. if (r == EXCP_DEBUG) {
  812. cpu_handle_guest_debug(cpu);
  813. }
  814. }
  815. qemu_kvm_wait_io_event(cpu);
  816. }
  817. return NULL;
  818. }
  819. static void *qemu_dummy_cpu_thread_fn(void *arg)
  820. {
  821. #ifdef _WIN32
  822. fprintf(stderr, "qtest is not supported under Windows\n");
  823. exit(1);
  824. #else
  825. CPUState *cpu = arg;
  826. sigset_t waitset;
  827. int r;
  828. qemu_mutex_lock_iothread();
  829. qemu_thread_get_self(cpu->thread);
  830. cpu->thread_id = qemu_get_thread_id();
  831. sigemptyset(&waitset);
  832. sigaddset(&waitset, SIG_IPI);
  833. /* signal CPU creation */
  834. cpu->created = true;
  835. qemu_cond_signal(&qemu_cpu_cond);
  836. current_cpu = cpu;
  837. while (1) {
  838. current_cpu = NULL;
  839. qemu_mutex_unlock_iothread();
  840. do {
  841. int sig;
  842. r = sigwait(&waitset, &sig);
  843. } while (r == -1 && (errno == EAGAIN || errno == EINTR));
  844. if (r == -1) {
  845. perror("sigwait");
  846. exit(1);
  847. }
  848. qemu_mutex_lock_iothread();
  849. current_cpu = cpu;
  850. qemu_wait_io_event_common(cpu);
  851. }
  852. return NULL;
  853. #endif
  854. }
  855. static void tcg_exec_all(void);
  856. static void *qemu_tcg_cpu_thread_fn(void *arg)
  857. {
  858. CPUState *cpu = arg;
  859. qemu_tcg_init_cpu_signals();
  860. qemu_thread_get_self(cpu->thread);
  861. qemu_mutex_lock(&qemu_global_mutex);
  862. CPU_FOREACH(cpu) {
  863. cpu->thread_id = qemu_get_thread_id();
  864. cpu->created = true;
  865. }
  866. qemu_cond_signal(&qemu_cpu_cond);
  867. /* wait for initial kick-off after machine start */
  868. while (QTAILQ_FIRST(&cpus)->stopped) {
  869. qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
  870. /* process any pending work */
  871. CPU_FOREACH(cpu) {
  872. qemu_wait_io_event_common(cpu);
  873. }
  874. }
  875. while (1) {
  876. tcg_exec_all();
  877. if (use_icount) {
  878. int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  879. if (deadline == 0) {
  880. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  881. }
  882. }
  883. qemu_tcg_wait_io_event();
  884. }
  885. return NULL;
  886. }
  887. static void qemu_cpu_kick_thread(CPUState *cpu)
  888. {
  889. #ifndef _WIN32
  890. int err;
  891. err = pthread_kill(cpu->thread->thread, SIG_IPI);
  892. if (err) {
  893. fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
  894. exit(1);
  895. }
  896. #else /* _WIN32 */
  897. if (!qemu_cpu_is_self(cpu)) {
  898. CONTEXT tcgContext;
  899. if (SuspendThread(cpu->hThread) == (DWORD)-1) {
  900. fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
  901. GetLastError());
  902. exit(1);
  903. }
  904. /* On multi-core systems, we are not sure that the thread is actually
  905. * suspended until we can get the context.
  906. */
  907. tcgContext.ContextFlags = CONTEXT_CONTROL;
  908. while (GetThreadContext(cpu->hThread, &tcgContext) != 0) {
  909. continue;
  910. }
  911. cpu_signal(0);
  912. if (ResumeThread(cpu->hThread) == (DWORD)-1) {
  913. fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
  914. GetLastError());
  915. exit(1);
  916. }
  917. }
  918. #endif
  919. }
  920. void qemu_cpu_kick(CPUState *cpu)
  921. {
  922. qemu_cond_broadcast(cpu->halt_cond);
  923. if (!tcg_enabled() && !cpu->thread_kicked) {
  924. qemu_cpu_kick_thread(cpu);
  925. cpu->thread_kicked = true;
  926. }
  927. }
  928. void qemu_cpu_kick_self(void)
  929. {
  930. #ifndef _WIN32
  931. assert(current_cpu);
  932. if (!current_cpu->thread_kicked) {
  933. qemu_cpu_kick_thread(current_cpu);
  934. current_cpu->thread_kicked = true;
  935. }
  936. #else
  937. abort();
  938. #endif
  939. }
  940. bool qemu_cpu_is_self(CPUState *cpu)
  941. {
  942. return qemu_thread_is_self(cpu->thread);
  943. }
  944. static bool qemu_in_vcpu_thread(void)
  945. {
  946. return current_cpu && qemu_cpu_is_self(current_cpu);
  947. }
  948. void qemu_mutex_lock_iothread(void)
  949. {
  950. if (!tcg_enabled()) {
  951. qemu_mutex_lock(&qemu_global_mutex);
  952. } else {
  953. iothread_requesting_mutex = true;
  954. if (qemu_mutex_trylock(&qemu_global_mutex)) {
  955. qemu_cpu_kick_thread(first_cpu);
  956. qemu_mutex_lock(&qemu_global_mutex);
  957. }
  958. iothread_requesting_mutex = false;
  959. qemu_cond_broadcast(&qemu_io_proceeded_cond);
  960. }
  961. }
  962. void qemu_mutex_unlock_iothread(void)
  963. {
  964. qemu_mutex_unlock(&qemu_global_mutex);
  965. }
  966. static int all_vcpus_paused(void)
  967. {
  968. CPUState *cpu;
  969. CPU_FOREACH(cpu) {
  970. if (!cpu->stopped) {
  971. return 0;
  972. }
  973. }
  974. return 1;
  975. }
  976. void pause_all_vcpus(void)
  977. {
  978. CPUState *cpu;
  979. qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
  980. CPU_FOREACH(cpu) {
  981. cpu->stop = true;
  982. qemu_cpu_kick(cpu);
  983. }
  984. if (qemu_in_vcpu_thread()) {
  985. cpu_stop_current();
  986. if (!kvm_enabled()) {
  987. CPU_FOREACH(cpu) {
  988. cpu->stop = false;
  989. cpu->stopped = true;
  990. }
  991. return;
  992. }
  993. }
  994. while (!all_vcpus_paused()) {
  995. qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
  996. CPU_FOREACH(cpu) {
  997. qemu_cpu_kick(cpu);
  998. }
  999. }
  1000. }
  1001. void cpu_resume(CPUState *cpu)
  1002. {
  1003. cpu->stop = false;
  1004. cpu->stopped = false;
  1005. qemu_cpu_kick(cpu);
  1006. }
  1007. void resume_all_vcpus(void)
  1008. {
  1009. CPUState *cpu;
  1010. qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
  1011. CPU_FOREACH(cpu) {
  1012. cpu_resume(cpu);
  1013. }
  1014. }
  1015. /* For temporary buffers for forming a name */
  1016. #define VCPU_THREAD_NAME_SIZE 16
  1017. static void qemu_tcg_init_vcpu(CPUState *cpu)
  1018. {
  1019. char thread_name[VCPU_THREAD_NAME_SIZE];
  1020. tcg_cpu_address_space_init(cpu, cpu->as);
  1021. /* share a single thread for all cpus with TCG */
  1022. if (!tcg_cpu_thread) {
  1023. cpu->thread = g_malloc0(sizeof(QemuThread));
  1024. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1025. qemu_cond_init(cpu->halt_cond);
  1026. tcg_halt_cond = cpu->halt_cond;
  1027. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
  1028. cpu->cpu_index);
  1029. qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
  1030. cpu, QEMU_THREAD_JOINABLE);
  1031. #ifdef _WIN32
  1032. cpu->hThread = qemu_thread_get_handle(cpu->thread);
  1033. #endif
  1034. while (!cpu->created) {
  1035. qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
  1036. }
  1037. tcg_cpu_thread = cpu->thread;
  1038. } else {
  1039. cpu->thread = tcg_cpu_thread;
  1040. cpu->halt_cond = tcg_halt_cond;
  1041. }
  1042. }
  1043. static void qemu_kvm_start_vcpu(CPUState *cpu)
  1044. {
  1045. char thread_name[VCPU_THREAD_NAME_SIZE];
  1046. cpu->thread = g_malloc0(sizeof(QemuThread));
  1047. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1048. qemu_cond_init(cpu->halt_cond);
  1049. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
  1050. cpu->cpu_index);
  1051. qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
  1052. cpu, QEMU_THREAD_JOINABLE);
  1053. while (!cpu->created) {
  1054. qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
  1055. }
  1056. }
  1057. static void qemu_dummy_start_vcpu(CPUState *cpu)
  1058. {
  1059. char thread_name[VCPU_THREAD_NAME_SIZE];
  1060. cpu->thread = g_malloc0(sizeof(QemuThread));
  1061. cpu->halt_cond = g_malloc0(sizeof(QemuCond));
  1062. qemu_cond_init(cpu->halt_cond);
  1063. snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
  1064. cpu->cpu_index);
  1065. qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
  1066. QEMU_THREAD_JOINABLE);
  1067. while (!cpu->created) {
  1068. qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
  1069. }
  1070. }
  1071. void qemu_init_vcpu(CPUState *cpu)
  1072. {
  1073. cpu->nr_cores = smp_cores;
  1074. cpu->nr_threads = smp_threads;
  1075. cpu->stopped = true;
  1076. if (kvm_enabled()) {
  1077. qemu_kvm_start_vcpu(cpu);
  1078. } else if (tcg_enabled()) {
  1079. qemu_tcg_init_vcpu(cpu);
  1080. } else {
  1081. qemu_dummy_start_vcpu(cpu);
  1082. }
  1083. }
  1084. void cpu_stop_current(void)
  1085. {
  1086. if (current_cpu) {
  1087. current_cpu->stop = false;
  1088. current_cpu->stopped = true;
  1089. cpu_exit(current_cpu);
  1090. qemu_cond_signal(&qemu_pause_cond);
  1091. }
  1092. }
  1093. int vm_stop(RunState state)
  1094. {
  1095. if (qemu_in_vcpu_thread()) {
  1096. qemu_system_vmstop_request_prepare();
  1097. qemu_system_vmstop_request(state);
  1098. /*
  1099. * FIXME: should not return to device code in case
  1100. * vm_stop() has been requested.
  1101. */
  1102. cpu_stop_current();
  1103. return 0;
  1104. }
  1105. return do_vm_stop(state);
  1106. }
  1107. /* does a state transition even if the VM is already stopped,
  1108. current state is forgotten forever */
  1109. int vm_stop_force_state(RunState state)
  1110. {
  1111. if (runstate_is_running()) {
  1112. return vm_stop(state);
  1113. } else {
  1114. runstate_set(state);
  1115. /* Make sure to return an error if the flush in a previous vm_stop()
  1116. * failed. */
  1117. return bdrv_flush_all();
  1118. }
  1119. }
  1120. static int tcg_cpu_exec(CPUArchState *env)
  1121. {
  1122. CPUState *cpu = ENV_GET_CPU(env);
  1123. int ret;
  1124. #ifdef CONFIG_PROFILER
  1125. int64_t ti;
  1126. #endif
  1127. #ifdef CONFIG_PROFILER
  1128. ti = profile_getclock();
  1129. #endif
  1130. if (use_icount) {
  1131. int64_t count;
  1132. int64_t deadline;
  1133. int decr;
  1134. timers_state.qemu_icount -= (cpu->icount_decr.u16.low
  1135. + cpu->icount_extra);
  1136. cpu->icount_decr.u16.low = 0;
  1137. cpu->icount_extra = 0;
  1138. deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  1139. /* Maintain prior (possibly buggy) behaviour where if no deadline
  1140. * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
  1141. * INT32_MAX nanoseconds ahead, we still use INT32_MAX
  1142. * nanoseconds.
  1143. */
  1144. if ((deadline < 0) || (deadline > INT32_MAX)) {
  1145. deadline = INT32_MAX;
  1146. }
  1147. count = qemu_icount_round(deadline);
  1148. timers_state.qemu_icount += count;
  1149. decr = (count > 0xffff) ? 0xffff : count;
  1150. count -= decr;
  1151. cpu->icount_decr.u16.low = decr;
  1152. cpu->icount_extra = count;
  1153. }
  1154. ret = cpu_exec(env);
  1155. #ifdef CONFIG_PROFILER
  1156. qemu_time += profile_getclock() - ti;
  1157. #endif
  1158. if (use_icount) {
  1159. /* Fold pending instructions back into the
  1160. instruction counter, and clear the interrupt flag. */
  1161. timers_state.qemu_icount -= (cpu->icount_decr.u16.low
  1162. + cpu->icount_extra);
  1163. cpu->icount_decr.u32 = 0;
  1164. cpu->icount_extra = 0;
  1165. }
  1166. return ret;
  1167. }
  1168. static void tcg_exec_all(void)
  1169. {
  1170. int r;
  1171. /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
  1172. qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
  1173. if (next_cpu == NULL) {
  1174. next_cpu = first_cpu;
  1175. }
  1176. for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
  1177. CPUState *cpu = next_cpu;
  1178. CPUArchState *env = cpu->env_ptr;
  1179. qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
  1180. (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
  1181. if (cpu_can_run(cpu)) {
  1182. r = tcg_cpu_exec(env);
  1183. if (r == EXCP_DEBUG) {
  1184. cpu_handle_guest_debug(cpu);
  1185. break;
  1186. }
  1187. } else if (cpu->stop || cpu->stopped) {
  1188. break;
  1189. }
  1190. }
  1191. exit_request = 0;
  1192. }
  1193. void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
  1194. {
  1195. /* XXX: implement xxx_cpu_list for targets that still miss it */
  1196. #if defined(cpu_list)
  1197. cpu_list(f, cpu_fprintf);
  1198. #endif
  1199. }
  1200. CpuInfoList *qmp_query_cpus(Error **errp)
  1201. {
  1202. CpuInfoList *head = NULL, *cur_item = NULL;
  1203. CPUState *cpu;
  1204. CPU_FOREACH(cpu) {
  1205. CpuInfoList *info;
  1206. #if defined(TARGET_I386)
  1207. X86CPU *x86_cpu = X86_CPU(cpu);
  1208. CPUX86State *env = &x86_cpu->env;
  1209. #elif defined(TARGET_PPC)
  1210. PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
  1211. CPUPPCState *env = &ppc_cpu->env;
  1212. #elif defined(TARGET_SPARC)
  1213. SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
  1214. CPUSPARCState *env = &sparc_cpu->env;
  1215. #elif defined(TARGET_MIPS)
  1216. MIPSCPU *mips_cpu = MIPS_CPU(cpu);
  1217. CPUMIPSState *env = &mips_cpu->env;
  1218. #elif defined(TARGET_TRICORE)
  1219. TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
  1220. CPUTriCoreState *env = &tricore_cpu->env;
  1221. #endif
  1222. cpu_synchronize_state(cpu);
  1223. info = g_malloc0(sizeof(*info));
  1224. info->value = g_malloc0(sizeof(*info->value));
  1225. info->value->CPU = cpu->cpu_index;
  1226. info->value->current = (cpu == first_cpu);
  1227. info->value->halted = cpu->halted;
  1228. info->value->thread_id = cpu->thread_id;
  1229. #if defined(TARGET_I386)
  1230. info->value->has_pc = true;
  1231. info->value->pc = env->eip + env->segs[R_CS].base;
  1232. #elif defined(TARGET_PPC)
  1233. info->value->has_nip = true;
  1234. info->value->nip = env->nip;
  1235. #elif defined(TARGET_SPARC)
  1236. info->value->has_pc = true;
  1237. info->value->pc = env->pc;
  1238. info->value->has_npc = true;
  1239. info->value->npc = env->npc;
  1240. #elif defined(TARGET_MIPS)
  1241. info->value->has_PC = true;
  1242. info->value->PC = env->active_tc.PC;
  1243. #elif defined(TARGET_TRICORE)
  1244. info->value->has_PC = true;
  1245. info->value->PC = env->PC;
  1246. #endif
  1247. /* XXX: waiting for the qapi to support GSList */
  1248. if (!cur_item) {
  1249. head = cur_item = info;
  1250. } else {
  1251. cur_item->next = info;
  1252. cur_item = info;
  1253. }
  1254. }
  1255. return head;
  1256. }
  1257. void qmp_memsave(int64_t addr, int64_t size, const char *filename,
  1258. bool has_cpu, int64_t cpu_index, Error **errp)
  1259. {
  1260. FILE *f;
  1261. uint32_t l;
  1262. CPUState *cpu;
  1263. uint8_t buf[1024];
  1264. if (!has_cpu) {
  1265. cpu_index = 0;
  1266. }
  1267. cpu = qemu_get_cpu(cpu_index);
  1268. if (cpu == NULL) {
  1269. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
  1270. "a CPU number");
  1271. return;
  1272. }
  1273. f = fopen(filename, "wb");
  1274. if (!f) {
  1275. error_setg_file_open(errp, errno, filename);
  1276. return;
  1277. }
  1278. while (size != 0) {
  1279. l = sizeof(buf);
  1280. if (l > size)
  1281. l = size;
  1282. if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
  1283. error_setg(errp, "Invalid addr 0x%016" PRIx64 "specified", addr);
  1284. goto exit;
  1285. }
  1286. if (fwrite(buf, 1, l, f) != l) {
  1287. error_set(errp, QERR_IO_ERROR);
  1288. goto exit;
  1289. }
  1290. addr += l;
  1291. size -= l;
  1292. }
  1293. exit:
  1294. fclose(f);
  1295. }
  1296. void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
  1297. Error **errp)
  1298. {
  1299. FILE *f;
  1300. uint32_t l;
  1301. uint8_t buf[1024];
  1302. f = fopen(filename, "wb");
  1303. if (!f) {
  1304. error_setg_file_open(errp, errno, filename);
  1305. return;
  1306. }
  1307. while (size != 0) {
  1308. l = sizeof(buf);
  1309. if (l > size)
  1310. l = size;
  1311. cpu_physical_memory_read(addr, buf, l);
  1312. if (fwrite(buf, 1, l, f) != l) {
  1313. error_set(errp, QERR_IO_ERROR);
  1314. goto exit;
  1315. }
  1316. addr += l;
  1317. size -= l;
  1318. }
  1319. exit:
  1320. fclose(f);
  1321. }
  1322. void qmp_inject_nmi(Error **errp)
  1323. {
  1324. #if defined(TARGET_I386)
  1325. CPUState *cs;
  1326. CPU_FOREACH(cs) {
  1327. X86CPU *cpu = X86_CPU(cs);
  1328. if (!cpu->apic_state) {
  1329. cpu_interrupt(cs, CPU_INTERRUPT_NMI);
  1330. } else {
  1331. apic_deliver_nmi(cpu->apic_state);
  1332. }
  1333. }
  1334. #else
  1335. nmi_monitor_handle(monitor_get_cpu_index(), errp);
  1336. #endif
  1337. }
  1338. void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
  1339. {
  1340. if (!use_icount) {
  1341. return;
  1342. }
  1343. cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
  1344. (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
  1345. if (icount_align_option) {
  1346. cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
  1347. cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
  1348. } else {
  1349. cpu_fprintf(f, "Max guest delay NA\n");
  1350. cpu_fprintf(f, "Max guest advance NA\n");
  1351. }
  1352. }