cpus.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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.h"
  27. #include "sysemu.h"
  28. #include "gdbstub.h"
  29. #include "dma.h"
  30. #include "kvm.h"
  31. #include "qemu-thread.h"
  32. #include "cpus.h"
  33. #ifndef _WIN32
  34. #include "compatfd.h"
  35. #endif
  36. #ifdef SIGRTMIN
  37. #define SIG_IPI (SIGRTMIN+4)
  38. #else
  39. #define SIG_IPI SIGUSR1
  40. #endif
  41. #ifdef CONFIG_LINUX
  42. #include <sys/prctl.h>
  43. #ifndef PR_MCE_KILL
  44. #define PR_MCE_KILL 33
  45. #endif
  46. #ifndef PR_MCE_KILL_SET
  47. #define PR_MCE_KILL_SET 1
  48. #endif
  49. #ifndef PR_MCE_KILL_EARLY
  50. #define PR_MCE_KILL_EARLY 1
  51. #endif
  52. #endif /* CONFIG_LINUX */
  53. static CPUState *next_cpu;
  54. /***********************************************************/
  55. void hw_error(const char *fmt, ...)
  56. {
  57. va_list ap;
  58. CPUState *env;
  59. va_start(ap, fmt);
  60. fprintf(stderr, "qemu: hardware error: ");
  61. vfprintf(stderr, fmt, ap);
  62. fprintf(stderr, "\n");
  63. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  64. fprintf(stderr, "CPU #%d:\n", env->cpu_index);
  65. #ifdef TARGET_I386
  66. cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
  67. #else
  68. cpu_dump_state(env, stderr, fprintf, 0);
  69. #endif
  70. }
  71. va_end(ap);
  72. abort();
  73. }
  74. void cpu_synchronize_all_states(void)
  75. {
  76. CPUState *cpu;
  77. for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
  78. cpu_synchronize_state(cpu);
  79. }
  80. }
  81. void cpu_synchronize_all_post_reset(void)
  82. {
  83. CPUState *cpu;
  84. for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
  85. cpu_synchronize_post_reset(cpu);
  86. }
  87. }
  88. void cpu_synchronize_all_post_init(void)
  89. {
  90. CPUState *cpu;
  91. for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
  92. cpu_synchronize_post_init(cpu);
  93. }
  94. }
  95. int cpu_is_stopped(CPUState *env)
  96. {
  97. return !vm_running || env->stopped;
  98. }
  99. static void do_vm_stop(int reason)
  100. {
  101. if (vm_running) {
  102. cpu_disable_ticks();
  103. vm_running = 0;
  104. pause_all_vcpus();
  105. vm_state_notify(0, reason);
  106. qemu_aio_flush();
  107. bdrv_flush_all();
  108. monitor_protocol_event(QEVENT_STOP, NULL);
  109. }
  110. }
  111. static int cpu_can_run(CPUState *env)
  112. {
  113. if (env->stop) {
  114. return 0;
  115. }
  116. if (env->stopped || !vm_running) {
  117. return 0;
  118. }
  119. return 1;
  120. }
  121. static bool cpu_thread_is_idle(CPUState *env)
  122. {
  123. if (env->stop || env->queued_work_first) {
  124. return false;
  125. }
  126. if (env->stopped || !vm_running) {
  127. return true;
  128. }
  129. if (!env->halted || qemu_cpu_has_work(env) ||
  130. (kvm_enabled() && kvm_irqchip_in_kernel())) {
  131. return false;
  132. }
  133. return true;
  134. }
  135. bool all_cpu_threads_idle(void)
  136. {
  137. CPUState *env;
  138. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  139. if (!cpu_thread_is_idle(env)) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. static void cpu_handle_guest_debug(CPUState *env)
  146. {
  147. gdb_set_stop_cpu(env);
  148. qemu_system_debug_request();
  149. #ifdef CONFIG_IOTHREAD
  150. env->stopped = 1;
  151. #endif
  152. }
  153. #ifdef CONFIG_IOTHREAD
  154. static void cpu_signal(int sig)
  155. {
  156. if (cpu_single_env) {
  157. cpu_exit(cpu_single_env);
  158. }
  159. exit_request = 1;
  160. }
  161. #endif
  162. #ifdef CONFIG_LINUX
  163. static void sigbus_reraise(void)
  164. {
  165. sigset_t set;
  166. struct sigaction action;
  167. memset(&action, 0, sizeof(action));
  168. action.sa_handler = SIG_DFL;
  169. if (!sigaction(SIGBUS, &action, NULL)) {
  170. raise(SIGBUS);
  171. sigemptyset(&set);
  172. sigaddset(&set, SIGBUS);
  173. sigprocmask(SIG_UNBLOCK, &set, NULL);
  174. }
  175. perror("Failed to re-raise SIGBUS!\n");
  176. abort();
  177. }
  178. static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
  179. void *ctx)
  180. {
  181. if (kvm_on_sigbus(siginfo->ssi_code,
  182. (void *)(intptr_t)siginfo->ssi_addr)) {
  183. sigbus_reraise();
  184. }
  185. }
  186. static void qemu_init_sigbus(void)
  187. {
  188. struct sigaction action;
  189. memset(&action, 0, sizeof(action));
  190. action.sa_flags = SA_SIGINFO;
  191. action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
  192. sigaction(SIGBUS, &action, NULL);
  193. prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
  194. }
  195. static void qemu_kvm_eat_signals(CPUState *env)
  196. {
  197. struct timespec ts = { 0, 0 };
  198. siginfo_t siginfo;
  199. sigset_t waitset;
  200. sigset_t chkset;
  201. int r;
  202. sigemptyset(&waitset);
  203. sigaddset(&waitset, SIG_IPI);
  204. sigaddset(&waitset, SIGBUS);
  205. do {
  206. r = sigtimedwait(&waitset, &siginfo, &ts);
  207. if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
  208. perror("sigtimedwait");
  209. exit(1);
  210. }
  211. switch (r) {
  212. case SIGBUS:
  213. if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
  214. sigbus_reraise();
  215. }
  216. break;
  217. default:
  218. break;
  219. }
  220. r = sigpending(&chkset);
  221. if (r == -1) {
  222. perror("sigpending");
  223. exit(1);
  224. }
  225. } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
  226. #ifndef CONFIG_IOTHREAD
  227. if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
  228. qemu_notify_event();
  229. }
  230. #endif
  231. }
  232. #else /* !CONFIG_LINUX */
  233. static void qemu_init_sigbus(void)
  234. {
  235. }
  236. static void qemu_kvm_eat_signals(CPUState *env)
  237. {
  238. }
  239. #endif /* !CONFIG_LINUX */
  240. #ifndef _WIN32
  241. static int io_thread_fd = -1;
  242. static void qemu_event_increment(void)
  243. {
  244. /* Write 8 bytes to be compatible with eventfd. */
  245. static const uint64_t val = 1;
  246. ssize_t ret;
  247. if (io_thread_fd == -1) {
  248. return;
  249. }
  250. do {
  251. ret = write(io_thread_fd, &val, sizeof(val));
  252. } while (ret < 0 && errno == EINTR);
  253. /* EAGAIN is fine, a read must be pending. */
  254. if (ret < 0 && errno != EAGAIN) {
  255. fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
  256. strerror(errno));
  257. exit (1);
  258. }
  259. }
  260. static void qemu_event_read(void *opaque)
  261. {
  262. int fd = (intptr_t)opaque;
  263. ssize_t len;
  264. char buffer[512];
  265. /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
  266. do {
  267. len = read(fd, buffer, sizeof(buffer));
  268. } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
  269. }
  270. static int qemu_event_init(void)
  271. {
  272. int err;
  273. int fds[2];
  274. err = qemu_eventfd(fds);
  275. if (err == -1) {
  276. return -errno;
  277. }
  278. err = fcntl_setfl(fds[0], O_NONBLOCK);
  279. if (err < 0) {
  280. goto fail;
  281. }
  282. err = fcntl_setfl(fds[1], O_NONBLOCK);
  283. if (err < 0) {
  284. goto fail;
  285. }
  286. qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
  287. (void *)(intptr_t)fds[0]);
  288. io_thread_fd = fds[1];
  289. return 0;
  290. fail:
  291. close(fds[0]);
  292. close(fds[1]);
  293. return err;
  294. }
  295. static void dummy_signal(int sig)
  296. {
  297. }
  298. /* If we have signalfd, we mask out the signals we want to handle and then
  299. * use signalfd to listen for them. We rely on whatever the current signal
  300. * handler is to dispatch the signals when we receive them.
  301. */
  302. static void sigfd_handler(void *opaque)
  303. {
  304. int fd = (intptr_t)opaque;
  305. struct qemu_signalfd_siginfo info;
  306. struct sigaction action;
  307. ssize_t len;
  308. while (1) {
  309. do {
  310. len = read(fd, &info, sizeof(info));
  311. } while (len == -1 && errno == EINTR);
  312. if (len == -1 && errno == EAGAIN) {
  313. break;
  314. }
  315. if (len != sizeof(info)) {
  316. printf("read from sigfd returned %zd: %m\n", len);
  317. return;
  318. }
  319. sigaction(info.ssi_signo, NULL, &action);
  320. if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
  321. action.sa_sigaction(info.ssi_signo,
  322. (siginfo_t *)&info, NULL);
  323. } else if (action.sa_handler) {
  324. action.sa_handler(info.ssi_signo);
  325. }
  326. }
  327. }
  328. static int qemu_signal_init(void)
  329. {
  330. int sigfd;
  331. sigset_t set;
  332. #ifdef CONFIG_IOTHREAD
  333. /* SIGUSR2 used by posix-aio-compat.c */
  334. sigemptyset(&set);
  335. sigaddset(&set, SIGUSR2);
  336. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  337. /*
  338. * SIG_IPI must be blocked in the main thread and must not be caught
  339. * by sigwait() in the signal thread. Otherwise, the cpu thread will
  340. * not catch it reliably.
  341. */
  342. sigemptyset(&set);
  343. sigaddset(&set, SIG_IPI);
  344. pthread_sigmask(SIG_BLOCK, &set, NULL);
  345. sigemptyset(&set);
  346. sigaddset(&set, SIGIO);
  347. sigaddset(&set, SIGALRM);
  348. sigaddset(&set, SIGBUS);
  349. #else
  350. sigemptyset(&set);
  351. sigaddset(&set, SIGBUS);
  352. if (kvm_enabled()) {
  353. /*
  354. * We need to process timer signals synchronously to avoid a race
  355. * between exit_request check and KVM vcpu entry.
  356. */
  357. sigaddset(&set, SIGIO);
  358. sigaddset(&set, SIGALRM);
  359. }
  360. #endif
  361. pthread_sigmask(SIG_BLOCK, &set, NULL);
  362. sigfd = qemu_signalfd(&set);
  363. if (sigfd == -1) {
  364. fprintf(stderr, "failed to create signalfd\n");
  365. return -errno;
  366. }
  367. fcntl_setfl(sigfd, O_NONBLOCK);
  368. qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
  369. (void *)(intptr_t)sigfd);
  370. return 0;
  371. }
  372. static void qemu_kvm_init_cpu_signals(CPUState *env)
  373. {
  374. int r;
  375. sigset_t set;
  376. struct sigaction sigact;
  377. memset(&sigact, 0, sizeof(sigact));
  378. sigact.sa_handler = dummy_signal;
  379. sigaction(SIG_IPI, &sigact, NULL);
  380. #ifdef CONFIG_IOTHREAD
  381. pthread_sigmask(SIG_BLOCK, NULL, &set);
  382. sigdelset(&set, SIG_IPI);
  383. sigdelset(&set, SIGBUS);
  384. r = kvm_set_signal_mask(env, &set);
  385. if (r) {
  386. fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
  387. exit(1);
  388. }
  389. #else
  390. sigemptyset(&set);
  391. sigaddset(&set, SIG_IPI);
  392. sigaddset(&set, SIGIO);
  393. sigaddset(&set, SIGALRM);
  394. pthread_sigmask(SIG_BLOCK, &set, NULL);
  395. pthread_sigmask(SIG_BLOCK, NULL, &set);
  396. sigdelset(&set, SIGIO);
  397. sigdelset(&set, SIGALRM);
  398. #endif
  399. sigdelset(&set, SIG_IPI);
  400. sigdelset(&set, SIGBUS);
  401. r = kvm_set_signal_mask(env, &set);
  402. if (r) {
  403. fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
  404. exit(1);
  405. }
  406. }
  407. static void qemu_tcg_init_cpu_signals(void)
  408. {
  409. #ifdef CONFIG_IOTHREAD
  410. sigset_t set;
  411. struct sigaction sigact;
  412. memset(&sigact, 0, sizeof(sigact));
  413. sigact.sa_handler = cpu_signal;
  414. sigaction(SIG_IPI, &sigact, NULL);
  415. sigemptyset(&set);
  416. sigaddset(&set, SIG_IPI);
  417. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  418. #endif
  419. }
  420. #else /* _WIN32 */
  421. HANDLE qemu_event_handle;
  422. static void dummy_event_handler(void *opaque)
  423. {
  424. }
  425. static int qemu_event_init(void)
  426. {
  427. qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
  428. if (!qemu_event_handle) {
  429. fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
  430. return -1;
  431. }
  432. qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
  433. return 0;
  434. }
  435. static void qemu_event_increment(void)
  436. {
  437. if (!SetEvent(qemu_event_handle)) {
  438. fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
  439. GetLastError());
  440. exit (1);
  441. }
  442. }
  443. static int qemu_signal_init(void)
  444. {
  445. return 0;
  446. }
  447. static void qemu_kvm_init_cpu_signals(CPUState *env)
  448. {
  449. abort();
  450. }
  451. static void qemu_tcg_init_cpu_signals(void)
  452. {
  453. }
  454. #endif /* _WIN32 */
  455. #ifndef CONFIG_IOTHREAD
  456. int qemu_init_main_loop(void)
  457. {
  458. int ret;
  459. ret = qemu_signal_init();
  460. if (ret) {
  461. return ret;
  462. }
  463. qemu_init_sigbus();
  464. return qemu_event_init();
  465. }
  466. void qemu_main_loop_start(void)
  467. {
  468. }
  469. void qemu_init_vcpu(void *_env)
  470. {
  471. CPUState *env = _env;
  472. int r;
  473. env->nr_cores = smp_cores;
  474. env->nr_threads = smp_threads;
  475. if (kvm_enabled()) {
  476. r = kvm_init_vcpu(env);
  477. if (r < 0) {
  478. fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
  479. exit(1);
  480. }
  481. qemu_kvm_init_cpu_signals(env);
  482. } else {
  483. qemu_tcg_init_cpu_signals();
  484. }
  485. }
  486. int qemu_cpu_is_self(void *env)
  487. {
  488. return 1;
  489. }
  490. void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
  491. {
  492. func(data);
  493. }
  494. void resume_all_vcpus(void)
  495. {
  496. }
  497. void pause_all_vcpus(void)
  498. {
  499. }
  500. void qemu_cpu_kick(void *env)
  501. {
  502. }
  503. void qemu_cpu_kick_self(void)
  504. {
  505. #ifndef _WIN32
  506. assert(cpu_single_env);
  507. raise(SIG_IPI);
  508. #else
  509. abort();
  510. #endif
  511. }
  512. void qemu_notify_event(void)
  513. {
  514. CPUState *env = cpu_single_env;
  515. qemu_event_increment ();
  516. if (env) {
  517. cpu_exit(env);
  518. }
  519. if (next_cpu && env != next_cpu) {
  520. cpu_exit(next_cpu);
  521. }
  522. exit_request = 1;
  523. }
  524. void qemu_mutex_lock_iothread(void) {}
  525. void qemu_mutex_unlock_iothread(void) {}
  526. void cpu_stop_current(void)
  527. {
  528. }
  529. void vm_stop(int reason)
  530. {
  531. do_vm_stop(reason);
  532. }
  533. #else /* CONFIG_IOTHREAD */
  534. QemuMutex qemu_global_mutex;
  535. static QemuCond qemu_io_proceeded_cond;
  536. static bool iothread_requesting_mutex;
  537. static QemuThread io_thread;
  538. static QemuThread *tcg_cpu_thread;
  539. static QemuCond *tcg_halt_cond;
  540. static int qemu_system_ready;
  541. /* cpu creation */
  542. static QemuCond qemu_cpu_cond;
  543. /* system init */
  544. static QemuCond qemu_system_cond;
  545. static QemuCond qemu_pause_cond;
  546. static QemuCond qemu_work_cond;
  547. int qemu_init_main_loop(void)
  548. {
  549. int ret;
  550. qemu_init_sigbus();
  551. ret = qemu_signal_init();
  552. if (ret) {
  553. return ret;
  554. }
  555. /* Note eventfd must be drained before signalfd handlers run */
  556. ret = qemu_event_init();
  557. if (ret) {
  558. return ret;
  559. }
  560. qemu_cond_init(&qemu_cpu_cond);
  561. qemu_cond_init(&qemu_system_cond);
  562. qemu_cond_init(&qemu_pause_cond);
  563. qemu_cond_init(&qemu_work_cond);
  564. qemu_cond_init(&qemu_io_proceeded_cond);
  565. qemu_mutex_init(&qemu_global_mutex);
  566. qemu_mutex_lock(&qemu_global_mutex);
  567. qemu_thread_get_self(&io_thread);
  568. return 0;
  569. }
  570. void qemu_main_loop_start(void)
  571. {
  572. qemu_system_ready = 1;
  573. qemu_cond_broadcast(&qemu_system_cond);
  574. }
  575. void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
  576. {
  577. struct qemu_work_item wi;
  578. if (qemu_cpu_is_self(env)) {
  579. func(data);
  580. return;
  581. }
  582. wi.func = func;
  583. wi.data = data;
  584. if (!env->queued_work_first) {
  585. env->queued_work_first = &wi;
  586. } else {
  587. env->queued_work_last->next = &wi;
  588. }
  589. env->queued_work_last = &wi;
  590. wi.next = NULL;
  591. wi.done = false;
  592. qemu_cpu_kick(env);
  593. while (!wi.done) {
  594. CPUState *self_env = cpu_single_env;
  595. qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
  596. cpu_single_env = self_env;
  597. }
  598. }
  599. static void flush_queued_work(CPUState *env)
  600. {
  601. struct qemu_work_item *wi;
  602. if (!env->queued_work_first) {
  603. return;
  604. }
  605. while ((wi = env->queued_work_first)) {
  606. env->queued_work_first = wi->next;
  607. wi->func(wi->data);
  608. wi->done = true;
  609. }
  610. env->queued_work_last = NULL;
  611. qemu_cond_broadcast(&qemu_work_cond);
  612. }
  613. static void qemu_wait_io_event_common(CPUState *env)
  614. {
  615. if (env->stop) {
  616. env->stop = 0;
  617. env->stopped = 1;
  618. qemu_cond_signal(&qemu_pause_cond);
  619. }
  620. flush_queued_work(env);
  621. env->thread_kicked = false;
  622. }
  623. static void qemu_tcg_wait_io_event(void)
  624. {
  625. CPUState *env;
  626. while (all_cpu_threads_idle()) {
  627. /* Start accounting real time to the virtual clock if the CPUs
  628. are idle. */
  629. qemu_clock_warp(vm_clock);
  630. qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
  631. }
  632. while (iothread_requesting_mutex) {
  633. qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
  634. }
  635. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  636. qemu_wait_io_event_common(env);
  637. }
  638. }
  639. static void qemu_kvm_wait_io_event(CPUState *env)
  640. {
  641. while (cpu_thread_is_idle(env)) {
  642. qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
  643. }
  644. qemu_kvm_eat_signals(env);
  645. qemu_wait_io_event_common(env);
  646. }
  647. static void *qemu_kvm_cpu_thread_fn(void *arg)
  648. {
  649. CPUState *env = arg;
  650. int r;
  651. qemu_mutex_lock(&qemu_global_mutex);
  652. qemu_thread_get_self(env->thread);
  653. env->thread_id = qemu_get_thread_id();
  654. r = kvm_init_vcpu(env);
  655. if (r < 0) {
  656. fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
  657. exit(1);
  658. }
  659. qemu_kvm_init_cpu_signals(env);
  660. /* signal CPU creation */
  661. env->created = 1;
  662. qemu_cond_signal(&qemu_cpu_cond);
  663. /* and wait for machine initialization */
  664. while (!qemu_system_ready) {
  665. qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
  666. }
  667. while (1) {
  668. if (cpu_can_run(env)) {
  669. r = kvm_cpu_exec(env);
  670. if (r == EXCP_DEBUG) {
  671. cpu_handle_guest_debug(env);
  672. }
  673. }
  674. qemu_kvm_wait_io_event(env);
  675. }
  676. return NULL;
  677. }
  678. static void *qemu_tcg_cpu_thread_fn(void *arg)
  679. {
  680. CPUState *env = arg;
  681. qemu_tcg_init_cpu_signals();
  682. qemu_thread_get_self(env->thread);
  683. /* signal CPU creation */
  684. qemu_mutex_lock(&qemu_global_mutex);
  685. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  686. env->thread_id = qemu_get_thread_id();
  687. env->created = 1;
  688. }
  689. qemu_cond_signal(&qemu_cpu_cond);
  690. /* and wait for machine initialization */
  691. while (!qemu_system_ready) {
  692. qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
  693. }
  694. while (1) {
  695. cpu_exec_all();
  696. if (use_icount && qemu_next_icount_deadline() <= 0) {
  697. qemu_notify_event();
  698. }
  699. qemu_tcg_wait_io_event();
  700. }
  701. return NULL;
  702. }
  703. static void qemu_cpu_kick_thread(CPUState *env)
  704. {
  705. #ifndef _WIN32
  706. int err;
  707. err = pthread_kill(env->thread->thread, SIG_IPI);
  708. if (err) {
  709. fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
  710. exit(1);
  711. }
  712. #else /* _WIN32 */
  713. if (!qemu_cpu_is_self(env)) {
  714. SuspendThread(env->thread->thread);
  715. cpu_signal(0);
  716. ResumeThread(env->thread->thread);
  717. }
  718. #endif
  719. }
  720. void qemu_cpu_kick(void *_env)
  721. {
  722. CPUState *env = _env;
  723. qemu_cond_broadcast(env->halt_cond);
  724. if (!env->thread_kicked) {
  725. qemu_cpu_kick_thread(env);
  726. env->thread_kicked = true;
  727. }
  728. }
  729. void qemu_cpu_kick_self(void)
  730. {
  731. #ifndef _WIN32
  732. assert(cpu_single_env);
  733. if (!cpu_single_env->thread_kicked) {
  734. qemu_cpu_kick_thread(cpu_single_env);
  735. cpu_single_env->thread_kicked = true;
  736. }
  737. #else
  738. abort();
  739. #endif
  740. }
  741. int qemu_cpu_is_self(void *_env)
  742. {
  743. CPUState *env = _env;
  744. return qemu_thread_is_self(env->thread);
  745. }
  746. void qemu_mutex_lock_iothread(void)
  747. {
  748. if (kvm_enabled()) {
  749. qemu_mutex_lock(&qemu_global_mutex);
  750. } else {
  751. iothread_requesting_mutex = true;
  752. if (qemu_mutex_trylock(&qemu_global_mutex)) {
  753. qemu_cpu_kick_thread(first_cpu);
  754. qemu_mutex_lock(&qemu_global_mutex);
  755. }
  756. iothread_requesting_mutex = false;
  757. qemu_cond_broadcast(&qemu_io_proceeded_cond);
  758. }
  759. }
  760. void qemu_mutex_unlock_iothread(void)
  761. {
  762. qemu_mutex_unlock(&qemu_global_mutex);
  763. }
  764. static int all_vcpus_paused(void)
  765. {
  766. CPUState *penv = first_cpu;
  767. while (penv) {
  768. if (!penv->stopped) {
  769. return 0;
  770. }
  771. penv = (CPUState *)penv->next_cpu;
  772. }
  773. return 1;
  774. }
  775. void pause_all_vcpus(void)
  776. {
  777. CPUState *penv = first_cpu;
  778. while (penv) {
  779. penv->stop = 1;
  780. qemu_cpu_kick(penv);
  781. penv = (CPUState *)penv->next_cpu;
  782. }
  783. while (!all_vcpus_paused()) {
  784. qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
  785. penv = first_cpu;
  786. while (penv) {
  787. qemu_cpu_kick(penv);
  788. penv = (CPUState *)penv->next_cpu;
  789. }
  790. }
  791. }
  792. void resume_all_vcpus(void)
  793. {
  794. CPUState *penv = first_cpu;
  795. while (penv) {
  796. penv->stop = 0;
  797. penv->stopped = 0;
  798. qemu_cpu_kick(penv);
  799. penv = (CPUState *)penv->next_cpu;
  800. }
  801. }
  802. static void qemu_tcg_init_vcpu(void *_env)
  803. {
  804. CPUState *env = _env;
  805. /* share a single thread for all cpus with TCG */
  806. if (!tcg_cpu_thread) {
  807. env->thread = qemu_mallocz(sizeof(QemuThread));
  808. env->halt_cond = qemu_mallocz(sizeof(QemuCond));
  809. qemu_cond_init(env->halt_cond);
  810. qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
  811. while (env->created == 0) {
  812. qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
  813. }
  814. tcg_cpu_thread = env->thread;
  815. tcg_halt_cond = env->halt_cond;
  816. } else {
  817. env->thread = tcg_cpu_thread;
  818. env->halt_cond = tcg_halt_cond;
  819. }
  820. }
  821. static void qemu_kvm_start_vcpu(CPUState *env)
  822. {
  823. env->thread = qemu_mallocz(sizeof(QemuThread));
  824. env->halt_cond = qemu_mallocz(sizeof(QemuCond));
  825. qemu_cond_init(env->halt_cond);
  826. qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
  827. while (env->created == 0) {
  828. qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
  829. }
  830. }
  831. void qemu_init_vcpu(void *_env)
  832. {
  833. CPUState *env = _env;
  834. env->nr_cores = smp_cores;
  835. env->nr_threads = smp_threads;
  836. if (kvm_enabled()) {
  837. qemu_kvm_start_vcpu(env);
  838. } else {
  839. qemu_tcg_init_vcpu(env);
  840. }
  841. }
  842. void qemu_notify_event(void)
  843. {
  844. qemu_event_increment();
  845. }
  846. void cpu_stop_current(void)
  847. {
  848. if (cpu_single_env) {
  849. cpu_single_env->stop = 0;
  850. cpu_single_env->stopped = 1;
  851. cpu_exit(cpu_single_env);
  852. qemu_cond_signal(&qemu_pause_cond);
  853. }
  854. }
  855. void vm_stop(int reason)
  856. {
  857. if (!qemu_thread_is_self(&io_thread)) {
  858. qemu_system_vmstop_request(reason);
  859. /*
  860. * FIXME: should not return to device code in case
  861. * vm_stop() has been requested.
  862. */
  863. cpu_stop_current();
  864. return;
  865. }
  866. do_vm_stop(reason);
  867. }
  868. #endif
  869. static int tcg_cpu_exec(CPUState *env)
  870. {
  871. int ret;
  872. #ifdef CONFIG_PROFILER
  873. int64_t ti;
  874. #endif
  875. #ifdef CONFIG_PROFILER
  876. ti = profile_getclock();
  877. #endif
  878. if (use_icount) {
  879. int64_t count;
  880. int decr;
  881. qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
  882. env->icount_decr.u16.low = 0;
  883. env->icount_extra = 0;
  884. count = qemu_icount_round(qemu_next_icount_deadline());
  885. qemu_icount += count;
  886. decr = (count > 0xffff) ? 0xffff : count;
  887. count -= decr;
  888. env->icount_decr.u16.low = decr;
  889. env->icount_extra = count;
  890. }
  891. ret = cpu_exec(env);
  892. #ifdef CONFIG_PROFILER
  893. qemu_time += profile_getclock() - ti;
  894. #endif
  895. if (use_icount) {
  896. /* Fold pending instructions back into the
  897. instruction counter, and clear the interrupt flag. */
  898. qemu_icount -= (env->icount_decr.u16.low
  899. + env->icount_extra);
  900. env->icount_decr.u32 = 0;
  901. env->icount_extra = 0;
  902. }
  903. return ret;
  904. }
  905. bool cpu_exec_all(void)
  906. {
  907. int r;
  908. /* Account partial waits to the vm_clock. */
  909. qemu_clock_warp(vm_clock);
  910. if (next_cpu == NULL) {
  911. next_cpu = first_cpu;
  912. }
  913. for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
  914. CPUState *env = next_cpu;
  915. qemu_clock_enable(vm_clock,
  916. (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
  917. #ifndef CONFIG_IOTHREAD
  918. if (qemu_alarm_pending()) {
  919. break;
  920. }
  921. #endif
  922. if (cpu_can_run(env)) {
  923. if (kvm_enabled()) {
  924. r = kvm_cpu_exec(env);
  925. qemu_kvm_eat_signals(env);
  926. } else {
  927. r = tcg_cpu_exec(env);
  928. }
  929. if (r == EXCP_DEBUG) {
  930. cpu_handle_guest_debug(env);
  931. break;
  932. }
  933. } else if (env->stop || env->stopped) {
  934. break;
  935. }
  936. }
  937. exit_request = 0;
  938. return !all_cpu_threads_idle();
  939. }
  940. void set_numa_modes(void)
  941. {
  942. CPUState *env;
  943. int i;
  944. for (env = first_cpu; env != NULL; env = env->next_cpu) {
  945. for (i = 0; i < nb_numa_nodes; i++) {
  946. if (node_cpumask[i] & (1 << env->cpu_index)) {
  947. env->numa_node = i;
  948. }
  949. }
  950. }
  951. }
  952. void set_cpu_log(const char *optarg)
  953. {
  954. int mask;
  955. const CPULogItem *item;
  956. mask = cpu_str_to_log_mask(optarg);
  957. if (!mask) {
  958. printf("Log items (comma separated):\n");
  959. for (item = cpu_log_items; item->mask != 0; item++) {
  960. printf("%-10s %s\n", item->name, item->help);
  961. }
  962. exit(1);
  963. }
  964. cpu_set_log(mask);
  965. }
  966. void set_cpu_log_filename(const char *optarg)
  967. {
  968. cpu_set_log_filename(optarg);
  969. }
  970. /* Return the virtual CPU time, based on the instruction counter. */
  971. int64_t cpu_get_icount(void)
  972. {
  973. int64_t icount;
  974. CPUState *env = cpu_single_env;;
  975. icount = qemu_icount;
  976. if (env) {
  977. if (!can_do_io(env)) {
  978. fprintf(stderr, "Bad clock read\n");
  979. }
  980. icount -= (env->icount_decr.u16.low + env->icount_extra);
  981. }
  982. return qemu_icount_bias + (icount << icount_time_shift);
  983. }
  984. void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
  985. {
  986. /* XXX: implement xxx_cpu_list for targets that still miss it */
  987. #if defined(cpu_list_id)
  988. cpu_list_id(f, cpu_fprintf, optarg);
  989. #elif defined(cpu_list)
  990. cpu_list(f, cpu_fprintf); /* deprecated */
  991. #endif
  992. }