qemu-kvm: Refactor exit_request processing in kvm_run
[qemu/qemu-dev-zwu.git] / cpus.c
blobb64c29f2f3d3a64ee832f825700ee3c1da4add5e
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 /* Needed early for CONFIG_BSD etc. */
26 #include "config-host.h"
28 #include "monitor.h"
29 #include "sysemu.h"
30 #include "gdbstub.h"
31 #include "dma.h"
32 #include "kvm.h"
33 #include "exec-all.h"
35 #include "qemu-thread.h"
36 #include "cpus.h"
37 #include "compatfd.h"
39 #ifdef SIGRTMIN
40 #define SIG_IPI (SIGRTMIN+4)
41 #else
42 #define SIG_IPI SIGUSR1
43 #endif
45 #ifdef CONFIG_LINUX
47 #include <sys/prctl.h>
49 #ifndef PR_MCE_KILL
50 #define PR_MCE_KILL 33
51 #endif
53 #ifndef PR_MCE_KILL_SET
54 #define PR_MCE_KILL_SET 1
55 #endif
57 #ifndef PR_MCE_KILL_EARLY
58 #define PR_MCE_KILL_EARLY 1
59 #endif
61 #endif /* CONFIG_LINUX */
63 static CPUState *next_cpu;
65 /***********************************************************/
66 void hw_error(const char *fmt, ...)
68 va_list ap;
69 CPUState *env;
71 va_start(ap, fmt);
72 fprintf(stderr, "qemu: hardware error: ");
73 vfprintf(stderr, fmt, ap);
74 fprintf(stderr, "\n");
75 for(env = first_cpu; env != NULL; env = env->next_cpu) {
76 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
77 #ifdef TARGET_I386
78 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
79 #else
80 cpu_dump_state(env, stderr, fprintf, 0);
81 #endif
83 va_end(ap);
84 abort();
87 void cpu_synchronize_all_states(void)
89 CPUState *cpu;
91 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
92 cpu_synchronize_state(cpu);
96 void cpu_synchronize_all_post_reset(void)
98 CPUState *cpu;
100 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
101 cpu_synchronize_post_reset(cpu);
105 void cpu_synchronize_all_post_init(void)
107 CPUState *cpu;
109 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
110 cpu_synchronize_post_init(cpu);
114 int cpu_is_stopped(CPUState *env)
116 return !vm_running || env->stopped;
119 static void do_vm_stop(int reason)
121 if (vm_running) {
122 cpu_disable_ticks();
123 vm_running = 0;
124 pause_all_vcpus();
125 vm_state_notify(0, reason);
126 qemu_aio_flush();
127 bdrv_flush_all();
128 monitor_protocol_event(QEVENT_STOP, NULL);
132 static int cpu_can_run(CPUState *env)
134 if (env->stop) {
135 return 0;
137 if (env->stopped || !vm_running) {
138 return 0;
140 return 1;
143 static bool cpu_thread_is_idle(CPUState *env)
145 if (env->stop || env->queued_work_first) {
146 return false;
148 if (env->stopped || !vm_running) {
149 return true;
151 if (!env->halted || qemu_cpu_has_work(env) ||
152 (kvm_enabled() && kvm_irqchip_in_kernel())) {
153 return false;
155 return true;
158 bool all_cpu_threads_idle(void)
160 CPUState *env;
162 for (env = first_cpu; env != NULL; env = env->next_cpu) {
163 if (!cpu_thread_is_idle(env)) {
164 return false;
167 return true;
170 static void cpu_handle_guest_debug(CPUState *env)
172 gdb_set_stop_cpu(env);
173 qemu_system_debug_request();
174 #ifdef CONFIG_IOTHREAD
175 env->stopped = 1;
176 #endif
179 #ifdef CONFIG_IOTHREAD
180 static void cpu_signal(int sig)
182 if (cpu_single_env) {
183 cpu_exit(cpu_single_env);
185 exit_request = 1;
187 #endif
189 #ifdef CONFIG_LINUX
190 static void sigbus_reraise(void)
192 sigset_t set;
193 struct sigaction action;
195 memset(&action, 0, sizeof(action));
196 action.sa_handler = SIG_DFL;
197 if (!sigaction(SIGBUS, &action, NULL)) {
198 raise(SIGBUS);
199 sigemptyset(&set);
200 sigaddset(&set, SIGBUS);
201 sigprocmask(SIG_UNBLOCK, &set, NULL);
203 perror("Failed to re-raise SIGBUS!\n");
204 abort();
207 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
208 void *ctx)
210 if (kvm_on_sigbus(siginfo->ssi_code,
211 (void *)(intptr_t)siginfo->ssi_addr)) {
212 sigbus_reraise();
216 static void qemu_init_sigbus(void)
218 struct sigaction action;
220 memset(&action, 0, sizeof(action));
221 action.sa_flags = SA_SIGINFO;
222 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
223 sigaction(SIGBUS, &action, NULL);
225 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
228 static void qemu_kvm_eat_signals(CPUState *env)
230 struct timespec ts = { 0, 0 };
231 siginfo_t siginfo;
232 sigset_t waitset;
233 sigset_t chkset;
234 int r;
236 sigemptyset(&waitset);
237 sigaddset(&waitset, SIG_IPI);
238 sigaddset(&waitset, SIGBUS);
240 do {
241 r = sigtimedwait(&waitset, &siginfo, &ts);
242 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
243 perror("sigtimedwait");
244 exit(1);
247 switch (r) {
248 case SIGBUS:
249 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
250 sigbus_reraise();
252 break;
253 default:
254 break;
257 r = sigpending(&chkset);
258 if (r == -1) {
259 perror("sigpending");
260 exit(1);
262 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
264 #ifndef CONFIG_IOTHREAD
265 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
266 qemu_notify_event();
268 #endif
271 #else /* !CONFIG_LINUX */
273 static void qemu_init_sigbus(void)
277 static void qemu_kvm_eat_signals(CPUState *env)
280 #endif /* !CONFIG_LINUX */
282 #ifndef _WIN32
283 static int io_thread_fd = -1;
285 static void qemu_event_increment(void)
287 /* Write 8 bytes to be compatible with eventfd. */
288 static const uint64_t val = 1;
289 ssize_t ret;
291 if (io_thread_fd == -1) {
292 return;
294 do {
295 ret = write(io_thread_fd, &val, sizeof(val));
296 } while (ret < 0 && errno == EINTR);
298 /* EAGAIN is fine, a read must be pending. */
299 if (ret < 0 && errno != EAGAIN) {
300 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
301 strerror(errno));
302 exit (1);
306 static void qemu_event_read(void *opaque)
308 int fd = (intptr_t)opaque;
309 ssize_t len;
310 char buffer[512];
312 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
313 do {
314 len = read(fd, buffer, sizeof(buffer));
315 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
318 static int qemu_event_init(void)
320 int err;
321 int fds[2];
323 err = qemu_eventfd(fds);
324 if (err == -1) {
325 return -errno;
327 err = fcntl_setfl(fds[0], O_NONBLOCK);
328 if (err < 0) {
329 goto fail;
331 err = fcntl_setfl(fds[1], O_NONBLOCK);
332 if (err < 0) {
333 goto fail;
335 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
336 (void *)(intptr_t)fds[0]);
338 io_thread_fd = fds[1];
339 return 0;
341 fail:
342 close(fds[0]);
343 close(fds[1]);
344 return err;
347 #ifdef OBSOLETE_KVM_IMPL
348 static void dummy_signal(int sig)
351 #endif
353 /* If we have signalfd, we mask out the signals we want to handle and then
354 * use signalfd to listen for them. We rely on whatever the current signal
355 * handler is to dispatch the signals when we receive them.
357 static void sigfd_handler(void *opaque)
359 int fd = (intptr_t)opaque;
360 struct qemu_signalfd_siginfo info;
361 struct sigaction action;
362 ssize_t len;
364 while (1) {
365 do {
366 len = read(fd, &info, sizeof(info));
367 } while (len == -1 && errno == EINTR);
369 if (len == -1 && errno == EAGAIN) {
370 break;
373 if (len != sizeof(info)) {
374 printf("read from sigfd returned %zd: %m\n", len);
375 return;
378 sigaction(info.ssi_signo, NULL, &action);
379 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
380 action.sa_sigaction(info.ssi_signo,
381 (siginfo_t *)&info, NULL);
382 } else if (action.sa_handler) {
383 action.sa_handler(info.ssi_signo);
388 static int qemu_signal_init(void)
390 int sigfd;
391 sigset_t set;
393 #ifdef CONFIG_IOTHREAD
394 /* SIGUSR2 used by posix-aio-compat.c */
395 sigemptyset(&set);
396 sigaddset(&set, SIGUSR2);
397 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
399 sigemptyset(&set);
400 sigaddset(&set, SIGIO);
401 sigaddset(&set, SIGALRM);
402 sigaddset(&set, SIG_IPI);
403 sigaddset(&set, SIGBUS);
404 pthread_sigmask(SIG_BLOCK, &set, NULL);
405 #else
406 sigemptyset(&set);
407 sigaddset(&set, SIGBUS);
408 if (kvm_enabled()) {
410 * We need to process timer signals synchronously to avoid a race
411 * between exit_request check and KVM vcpu entry.
413 sigaddset(&set, SIGIO);
414 sigaddset(&set, SIGALRM);
416 #endif
418 sigfd = qemu_signalfd(&set);
419 if (sigfd == -1) {
420 fprintf(stderr, "failed to create signalfd\n");
421 return -errno;
424 fcntl_setfl(sigfd, O_NONBLOCK);
426 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
427 (void *)(intptr_t)sigfd);
429 return 0;
432 static void qemu_kvm_init_cpu_signals(CPUState *env)
434 #if 0 /* Causes regressions: autotest WinXP.64 migrate.tcp */
435 int r;
436 sigset_t set;
437 struct sigaction sigact;
439 memset(&sigact, 0, sizeof(sigact));
440 sigact.sa_handler = dummy_signal;
441 sigaction(SIG_IPI, &sigact, NULL);
443 #ifdef CONFIG_IOTHREAD
444 pthread_sigmask(SIG_BLOCK, NULL, &set);
445 sigdelset(&set, SIG_IPI);
446 sigdelset(&set, SIGBUS);
447 r = kvm_set_signal_mask(env, &set);
448 if (r) {
449 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
450 exit(1);
452 #else
453 sigemptyset(&set);
454 sigaddset(&set, SIG_IPI);
455 sigaddset(&set, SIGIO);
456 sigaddset(&set, SIGALRM);
457 pthread_sigmask(SIG_BLOCK, &set, NULL);
459 pthread_sigmask(SIG_BLOCK, NULL, &set);
460 sigdelset(&set, SIGIO);
461 sigdelset(&set, SIGALRM);
462 #endif
463 sigdelset(&set, SIG_IPI);
464 sigdelset(&set, SIGBUS);
465 r = kvm_set_signal_mask(env, &set);
466 if (r) {
467 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
468 exit(1);
470 #endif
473 static void qemu_tcg_init_cpu_signals(void)
475 #ifdef CONFIG_IOTHREAD
476 sigset_t set;
477 struct sigaction sigact;
479 memset(&sigact, 0, sizeof(sigact));
480 sigact.sa_handler = cpu_signal;
481 sigaction(SIG_IPI, &sigact, NULL);
483 sigemptyset(&set);
484 sigaddset(&set, SIG_IPI);
485 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
486 #endif
489 #else /* _WIN32 */
491 HANDLE qemu_event_handle;
493 static void dummy_event_handler(void *opaque)
497 static int qemu_event_init(void)
499 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
500 if (!qemu_event_handle) {
501 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
502 return -1;
504 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
505 return 0;
508 static void qemu_event_increment(void)
510 if (!SetEvent(qemu_event_handle)) {
511 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
512 GetLastError());
513 exit (1);
517 static int qemu_signal_init(void)
519 return 0;
522 static void qemu_kvm_init_cpu_signals(CPUState *env)
524 abort();
527 static void qemu_tcg_init_cpu_signals(void)
530 #endif /* _WIN32 */
532 #ifndef CONFIG_IOTHREAD
533 int qemu_init_main_loop(void)
535 int ret;
537 ret = qemu_signal_init();
538 if (ret) {
539 return ret;
542 qemu_init_sigbus();
544 return qemu_event_init();
547 void qemu_main_loop_start(void)
551 void qemu_init_vcpu(void *_env)
553 CPUState *env = _env;
554 int r;
556 env->nr_cores = smp_cores;
557 env->nr_threads = smp_threads;
559 if (kvm_enabled()) {
560 r = kvm_init_vcpu(env);
561 if (r < 0) {
562 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
563 exit(1);
565 qemu_kvm_init_cpu_signals(env);
566 } else {
567 qemu_tcg_init_cpu_signals();
571 int qemu_cpu_is_self(void *env)
573 return 1;
576 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
578 func(data);
581 void resume_all_vcpus(void)
585 void pause_all_vcpus(void)
589 void qemu_cpu_kick(void *env)
593 #ifdef UNUSED_IMPL
594 void qemu_cpu_kick_self(void)
596 #ifndef _WIN32
597 assert(cpu_single_env);
599 raise(SIG_IPI);
600 #else
601 abort();
602 #endif
604 #endif
606 void qemu_notify_event(void)
608 CPUState *env = cpu_single_env;
610 qemu_event_increment ();
611 if (env) {
612 cpu_exit(env);
614 if (next_cpu && env != next_cpu) {
615 cpu_exit(next_cpu);
617 exit_request = 1;
620 #if defined(OBSOLETE_KVM_IMPL) || !defined(CONFIG_KVM)
621 void qemu_mutex_lock_iothread(void) {}
622 void qemu_mutex_unlock_iothread(void) {}
623 #endif
625 void cpu_stop_current(void)
629 void vm_stop(int reason)
631 do_vm_stop(reason);
634 #else /* CONFIG_IOTHREAD */
636 QemuMutex qemu_global_mutex;
637 static QemuMutex qemu_fair_mutex;
639 static QemuThread io_thread;
641 static QemuThread *tcg_cpu_thread;
642 static QemuCond *tcg_halt_cond;
644 static int qemu_system_ready;
645 /* cpu creation */
646 static QemuCond qemu_cpu_cond;
647 /* system init */
648 static QemuCond qemu_system_cond;
649 static QemuCond qemu_pause_cond;
650 static QemuCond qemu_work_cond;
652 int qemu_init_main_loop(void)
654 int ret;
656 qemu_init_sigbus();
658 ret = qemu_signal_init();
659 if (ret) {
660 return ret;
663 /* Note eventfd must be drained before signalfd handlers run */
664 ret = qemu_event_init();
665 if (ret) {
666 return ret;
669 qemu_cond_init(&qemu_cpu_cond);
670 qemu_cond_init(&qemu_system_cond);
671 qemu_cond_init(&qemu_pause_cond);
672 qemu_cond_init(&qemu_work_cond);
673 qemu_mutex_init(&qemu_fair_mutex);
674 qemu_mutex_init(&qemu_global_mutex);
675 qemu_mutex_lock(&qemu_global_mutex);
677 qemu_thread_get_self(&io_thread);
679 return 0;
682 void qemu_main_loop_start(void)
684 qemu_system_ready = 1;
685 qemu_cond_broadcast(&qemu_system_cond);
688 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
690 struct qemu_work_item wi;
692 if (qemu_cpu_is_self(env)) {
693 func(data);
694 return;
697 wi.func = func;
698 wi.data = data;
699 if (!env->queued_work_first) {
700 env->queued_work_first = &wi;
701 } else {
702 env->queued_work_last->next = &wi;
704 env->queued_work_last = &wi;
705 wi.next = NULL;
706 wi.done = false;
708 qemu_cpu_kick(env);
709 while (!wi.done) {
710 CPUState *self_env = cpu_single_env;
712 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
713 cpu_single_env = self_env;
717 static void flush_queued_work(CPUState *env)
719 struct qemu_work_item *wi;
721 if (!env->queued_work_first) {
722 return;
725 while ((wi = env->queued_work_first)) {
726 env->queued_work_first = wi->next;
727 wi->func(wi->data);
728 wi->done = true;
730 env->queued_work_last = NULL;
731 qemu_cond_broadcast(&qemu_work_cond);
734 static void qemu_wait_io_event_common(CPUState *env)
736 if (env->stop) {
737 env->stop = 0;
738 env->stopped = 1;
739 qemu_cond_signal(&qemu_pause_cond);
741 flush_queued_work(env);
742 env->thread_kicked = false;
745 static void qemu_tcg_wait_io_event(void)
747 CPUState *env;
749 while (all_cpu_threads_idle()) {
750 /* Start accounting real time to the virtual clock if the CPUs
751 are idle. */
752 qemu_clock_warp(vm_clock);
753 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
756 qemu_mutex_unlock(&qemu_global_mutex);
759 * Users of qemu_global_mutex can be starved, having no chance
760 * to acquire it since this path will get to it first.
761 * So use another lock to provide fairness.
763 qemu_mutex_lock(&qemu_fair_mutex);
764 qemu_mutex_unlock(&qemu_fair_mutex);
766 qemu_mutex_lock(&qemu_global_mutex);
768 for (env = first_cpu; env != NULL; env = env->next_cpu) {
769 qemu_wait_io_event_common(env);
773 static void qemu_kvm_wait_io_event(CPUState *env)
775 while (cpu_thread_is_idle(env)) {
776 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
779 qemu_kvm_eat_signals(env);
780 qemu_wait_io_event_common(env);
783 static void *qemu_kvm_cpu_thread_fn(void *arg)
785 CPUState *env = arg;
786 int r;
788 qemu_mutex_lock(&qemu_global_mutex);
789 qemu_thread_get_self(env->thread);
790 env->thread_id = qemu_get_thread_id();
792 r = kvm_init_vcpu(env);
793 if (r < 0) {
794 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
795 exit(1);
798 qemu_kvm_init_cpu_signals(env);
800 /* signal CPU creation */
801 env->created = 1;
802 qemu_cond_signal(&qemu_cpu_cond);
804 /* and wait for machine initialization */
805 while (!qemu_system_ready) {
806 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
809 while (1) {
810 if (cpu_can_run(env)) {
811 r = kvm_cpu_exec(env);
812 if (r == EXCP_DEBUG) {
813 cpu_handle_guest_debug(env);
816 qemu_kvm_wait_io_event(env);
819 return NULL;
822 static void *qemu_tcg_cpu_thread_fn(void *arg)
824 CPUState *env = arg;
826 qemu_tcg_init_cpu_signals();
827 qemu_thread_get_self(env->thread);
829 /* signal CPU creation */
830 qemu_mutex_lock(&qemu_global_mutex);
831 for (env = first_cpu; env != NULL; env = env->next_cpu) {
832 env->thread_id = qemu_get_thread_id();
833 env->created = 1;
835 qemu_cond_signal(&qemu_cpu_cond);
837 /* and wait for machine initialization */
838 while (!qemu_system_ready) {
839 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
842 while (1) {
843 cpu_exec_all();
844 if (use_icount && qemu_next_icount_deadline() <= 0) {
845 qemu_notify_event();
847 qemu_tcg_wait_io_event();
850 return NULL;
853 #endif
854 static void qemu_cpu_kick_thread(CPUState *env)
856 #ifndef _WIN32
857 int err;
859 err = pthread_kill(env->thread->thread, SIG_IPI);
860 if (err) {
861 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
862 exit(1);
864 #else /* _WIN32 */
865 if (!qemu_cpu_is_self(env)) {
866 SuspendThread(env->thread->thread);
867 cpu_signal(0);
868 ResumeThread(env->thread->thread);
870 #endif
872 #ifdef CONFIG_IOTHREAD
874 void qemu_cpu_kick(void *_env)
876 CPUState *env = _env;
878 qemu_cond_broadcast(env->halt_cond);
879 if (!env->thread_kicked) {
880 qemu_cpu_kick_thread(env);
881 env->thread_kicked = true;
885 #endif
886 void qemu_cpu_kick_self(void)
888 #ifndef _WIN32
889 assert(cpu_single_env);
891 if (!cpu_single_env->thread_kicked) {
892 qemu_cpu_kick_thread(cpu_single_env);
893 cpu_single_env->thread_kicked = true;
895 #else
896 abort();
897 #endif
899 #ifdef CONFIG_IOTHREAD
901 int qemu_cpu_is_self(void *_env)
903 CPUState *env = _env;
905 return qemu_thread_is_self(env->thread);
908 void qemu_mutex_lock_iothread(void)
910 if (kvm_enabled()) {
911 qemu_mutex_lock(&qemu_global_mutex);
912 } else {
913 qemu_mutex_lock(&qemu_fair_mutex);
914 if (qemu_mutex_trylock(&qemu_global_mutex)) {
915 qemu_cpu_kick_thread(first_cpu);
916 qemu_mutex_lock(&qemu_global_mutex);
918 qemu_mutex_unlock(&qemu_fair_mutex);
922 void qemu_mutex_unlock_iothread(void)
924 qemu_mutex_unlock(&qemu_global_mutex);
927 static int all_vcpus_paused(void)
929 CPUState *penv = first_cpu;
931 while (penv) {
932 if (!penv->stopped) {
933 return 0;
935 penv = (CPUState *)penv->next_cpu;
938 return 1;
941 void pause_all_vcpus(void)
943 CPUState *penv = first_cpu;
945 while (penv) {
946 penv->stop = 1;
947 qemu_cpu_kick(penv);
948 penv = (CPUState *)penv->next_cpu;
951 while (!all_vcpus_paused()) {
952 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
953 penv = first_cpu;
954 while (penv) {
955 qemu_cpu_kick(penv);
956 penv = (CPUState *)penv->next_cpu;
961 void resume_all_vcpus(void)
963 CPUState *penv = first_cpu;
965 while (penv) {
966 penv->stop = 0;
967 penv->stopped = 0;
968 qemu_cpu_kick(penv);
969 penv = (CPUState *)penv->next_cpu;
973 static void qemu_tcg_init_vcpu(void *_env)
975 CPUState *env = _env;
977 /* share a single thread for all cpus with TCG */
978 if (!tcg_cpu_thread) {
979 env->thread = qemu_mallocz(sizeof(QemuThread));
980 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
981 qemu_cond_init(env->halt_cond);
982 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
983 while (env->created == 0) {
984 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
986 tcg_cpu_thread = env->thread;
987 tcg_halt_cond = env->halt_cond;
988 } else {
989 env->thread = tcg_cpu_thread;
990 env->halt_cond = tcg_halt_cond;
994 static void qemu_kvm_start_vcpu(CPUState *env)
996 env->thread = qemu_mallocz(sizeof(QemuThread));
997 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
998 qemu_cond_init(env->halt_cond);
999 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
1000 while (env->created == 0) {
1001 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1005 void qemu_init_vcpu(void *_env)
1007 CPUState *env = _env;
1009 env->nr_cores = smp_cores;
1010 env->nr_threads = smp_threads;
1011 if (kvm_enabled()) {
1012 qemu_kvm_start_vcpu(env);
1013 } else {
1014 qemu_tcg_init_vcpu(env);
1018 void qemu_notify_event(void)
1020 qemu_event_increment();
1023 void cpu_stop_current(void)
1025 if (cpu_single_env) {
1026 cpu_single_env->stop = 0;
1027 cpu_single_env->stopped = 1;
1028 cpu_exit(cpu_single_env);
1029 qemu_cond_signal(&qemu_pause_cond);
1033 void vm_stop(int reason)
1035 if (!qemu_thread_is_self(&io_thread)) {
1036 qemu_system_vmstop_request(reason);
1038 * FIXME: should not return to device code in case
1039 * vm_stop() has been requested.
1041 cpu_stop_current();
1042 return;
1044 do_vm_stop(reason);
1047 #endif
1049 static int tcg_cpu_exec(CPUState *env)
1051 int ret;
1052 #ifdef CONFIG_PROFILER
1053 int64_t ti;
1054 #endif
1056 #ifdef CONFIG_PROFILER
1057 ti = profile_getclock();
1058 #endif
1059 if (use_icount) {
1060 int64_t count;
1061 int decr;
1062 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1063 env->icount_decr.u16.low = 0;
1064 env->icount_extra = 0;
1065 count = qemu_icount_round(qemu_next_icount_deadline());
1066 qemu_icount += count;
1067 decr = (count > 0xffff) ? 0xffff : count;
1068 count -= decr;
1069 env->icount_decr.u16.low = decr;
1070 env->icount_extra = count;
1072 ret = cpu_exec(env);
1073 #ifdef CONFIG_PROFILER
1074 qemu_time += profile_getclock() - ti;
1075 #endif
1076 if (use_icount) {
1077 /* Fold pending instructions back into the
1078 instruction counter, and clear the interrupt flag. */
1079 qemu_icount -= (env->icount_decr.u16.low
1080 + env->icount_extra);
1081 env->icount_decr.u32 = 0;
1082 env->icount_extra = 0;
1084 return ret;
1087 bool cpu_exec_all(void)
1089 int r;
1091 /* Account partial waits to the vm_clock. */
1092 qemu_clock_warp(vm_clock);
1094 if (next_cpu == NULL) {
1095 next_cpu = first_cpu;
1097 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1098 CPUState *env = next_cpu;
1100 qemu_clock_enable(vm_clock,
1101 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1103 #ifndef CONFIG_IOTHREAD
1104 if (qemu_alarm_pending()) {
1105 break;
1107 #endif
1108 if (cpu_can_run(env)) {
1109 if (kvm_enabled()) {
1110 r = kvm_cpu_exec(env);
1111 qemu_kvm_eat_signals(env);
1112 } else {
1113 r = tcg_cpu_exec(env);
1115 if (r == EXCP_DEBUG) {
1116 cpu_handle_guest_debug(env);
1117 break;
1119 } else if (env->stop || env->stopped) {
1120 break;
1123 exit_request = 0;
1124 return !all_cpu_threads_idle();
1127 void set_numa_modes(void)
1129 CPUState *env;
1130 int i;
1132 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1133 for (i = 0; i < nb_numa_nodes; i++) {
1134 if (node_cpumask[i] & (1 << env->cpu_index)) {
1135 env->numa_node = i;
1141 void set_cpu_log(const char *optarg)
1143 int mask;
1144 const CPULogItem *item;
1146 mask = cpu_str_to_log_mask(optarg);
1147 if (!mask) {
1148 printf("Log items (comma separated):\n");
1149 for (item = cpu_log_items; item->mask != 0; item++) {
1150 printf("%-10s %s\n", item->name, item->help);
1152 exit(1);
1154 cpu_set_log(mask);
1157 /* Return the virtual CPU time, based on the instruction counter. */
1158 int64_t cpu_get_icount(void)
1160 int64_t icount;
1161 CPUState *env = cpu_single_env;;
1163 icount = qemu_icount;
1164 if (env) {
1165 if (!can_do_io(env)) {
1166 fprintf(stderr, "Bad clock read\n");
1168 icount -= (env->icount_decr.u16.low + env->icount_extra);
1170 return qemu_icount_bias + (icount << icount_time_shift);
1173 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1175 /* XXX: implement xxx_cpu_list for targets that still miss it */
1176 #if defined(cpu_list_id)
1177 cpu_list_id(f, cpu_fprintf, optarg);
1178 #elif defined(cpu_list)
1179 cpu_list(f, cpu_fprintf); /* deprecated */
1180 #endif