Fix regression caused by qemu_kvm_init_cpu_signals()
[qemu/qemu-dev-zwu.git] / cpus.c
blobb8bfe6a616a428e78c68345d64c10280f46ed53b
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 "cpus.h"
36 #include "compatfd.h"
38 #ifdef SIGRTMIN
39 #define SIG_IPI (SIGRTMIN+4)
40 #else
41 #define SIG_IPI SIGUSR1
42 #endif
44 #ifdef CONFIG_LINUX
46 #include <sys/prctl.h>
48 #ifndef PR_MCE_KILL
49 #define PR_MCE_KILL 33
50 #endif
52 #ifndef PR_MCE_KILL_SET
53 #define PR_MCE_KILL_SET 1
54 #endif
56 #ifndef PR_MCE_KILL_EARLY
57 #define PR_MCE_KILL_EARLY 1
58 #endif
60 #endif /* CONFIG_LINUX */
62 static CPUState *next_cpu;
64 /***********************************************************/
65 void hw_error(const char *fmt, ...)
67 va_list ap;
68 CPUState *env;
70 va_start(ap, fmt);
71 fprintf(stderr, "qemu: hardware error: ");
72 vfprintf(stderr, fmt, ap);
73 fprintf(stderr, "\n");
74 for(env = first_cpu; env != NULL; env = env->next_cpu) {
75 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
76 #ifdef TARGET_I386
77 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
78 #else
79 cpu_dump_state(env, stderr, fprintf, 0);
80 #endif
82 va_end(ap);
83 abort();
86 void cpu_synchronize_all_states(void)
88 CPUState *cpu;
90 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
91 cpu_synchronize_state(cpu);
95 void cpu_synchronize_all_post_reset(void)
97 CPUState *cpu;
99 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
100 cpu_synchronize_post_reset(cpu);
104 void cpu_synchronize_all_post_init(void)
106 CPUState *cpu;
108 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
109 cpu_synchronize_post_init(cpu);
113 int cpu_is_stopped(CPUState *env)
115 return !vm_running || env->stopped;
118 static void do_vm_stop(int reason)
120 if (vm_running) {
121 cpu_disable_ticks();
122 vm_running = 0;
123 pause_all_vcpus();
124 vm_state_notify(0, reason);
125 qemu_aio_flush();
126 bdrv_flush_all();
127 monitor_protocol_event(QEVENT_STOP, NULL);
131 static int cpu_can_run(CPUState *env)
133 if (env->stop) {
134 return 0;
136 if (env->stopped || !vm_running) {
137 return 0;
139 return 1;
142 static bool cpu_thread_is_idle(CPUState *env)
144 if (env->stop || env->queued_work_first) {
145 return false;
147 if (env->stopped || !vm_running) {
148 return true;
150 if (!env->halted || qemu_cpu_has_work(env)) {
151 return false;
153 return true;
156 static bool all_cpu_threads_idle(void)
158 CPUState *env;
160 for (env = first_cpu; env != NULL; env = env->next_cpu) {
161 if (!cpu_thread_is_idle(env)) {
162 return false;
165 return true;
168 static CPUDebugExcpHandler *debug_excp_handler;
170 CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler)
172 CPUDebugExcpHandler *old_handler = debug_excp_handler;
174 debug_excp_handler = handler;
175 return old_handler;
178 static void cpu_handle_debug_exception(CPUState *env)
180 CPUWatchpoint *wp;
182 if (!env->watchpoint_hit) {
183 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
184 wp->flags &= ~BP_WATCHPOINT_HIT;
187 if (debug_excp_handler) {
188 debug_excp_handler(env);
191 gdb_set_stop_cpu(env);
192 qemu_system_debug_request();
193 #ifdef CONFIG_IOTHREAD
194 env->stopped = 1;
195 #endif
198 #ifdef CONFIG_LINUX
199 static void sigbus_reraise(void)
201 sigset_t set;
202 struct sigaction action;
204 memset(&action, 0, sizeof(action));
205 action.sa_handler = SIG_DFL;
206 if (!sigaction(SIGBUS, &action, NULL)) {
207 raise(SIGBUS);
208 sigemptyset(&set);
209 sigaddset(&set, SIGBUS);
210 sigprocmask(SIG_UNBLOCK, &set, NULL);
212 perror("Failed to re-raise SIGBUS!\n");
213 abort();
216 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
217 void *ctx)
219 if (kvm_on_sigbus(siginfo->ssi_code,
220 (void *)(intptr_t)siginfo->ssi_addr)) {
221 sigbus_reraise();
225 static void qemu_init_sigbus(void)
227 struct sigaction action;
229 memset(&action, 0, sizeof(action));
230 action.sa_flags = SA_SIGINFO;
231 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
232 sigaction(SIGBUS, &action, NULL);
234 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
237 #else /* !CONFIG_LINUX */
239 static void qemu_init_sigbus(void)
242 #endif /* !CONFIG_LINUX */
244 #ifndef _WIN32
245 static int io_thread_fd = -1;
247 static void qemu_event_increment(void)
249 /* Write 8 bytes to be compatible with eventfd. */
250 static const uint64_t val = 1;
251 ssize_t ret;
253 if (io_thread_fd == -1) {
254 return;
256 do {
257 ret = write(io_thread_fd, &val, sizeof(val));
258 } while (ret < 0 && errno == EINTR);
260 /* EAGAIN is fine, a read must be pending. */
261 if (ret < 0 && errno != EAGAIN) {
262 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
263 strerror(errno));
264 exit (1);
268 static void qemu_event_read(void *opaque)
270 int fd = (unsigned long)opaque;
271 ssize_t len;
272 char buffer[512];
274 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
275 do {
276 len = read(fd, buffer, sizeof(buffer));
277 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
280 static int qemu_event_init(void)
282 int err;
283 int fds[2];
285 err = qemu_eventfd(fds);
286 if (err == -1) {
287 return -errno;
289 err = fcntl_setfl(fds[0], O_NONBLOCK);
290 if (err < 0) {
291 goto fail;
293 err = fcntl_setfl(fds[1], O_NONBLOCK);
294 if (err < 0) {
295 goto fail;
297 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
298 (void *)(unsigned long)fds[0]);
300 io_thread_fd = fds[1];
301 return 0;
303 fail:
304 close(fds[0]);
305 close(fds[1]);
306 return err;
309 #ifdef OBSOLETE_KVM_IMPL
310 static void dummy_signal(int sig)
313 #endif
315 /* If we have signalfd, we mask out the signals we want to handle and then
316 * use signalfd to listen for them. We rely on whatever the current signal
317 * handler is to dispatch the signals when we receive them.
319 static void sigfd_handler(void *opaque)
321 int fd = (unsigned long) opaque;
322 struct qemu_signalfd_siginfo info;
323 struct sigaction action;
324 ssize_t len;
326 while (1) {
327 do {
328 len = read(fd, &info, sizeof(info));
329 } while (len == -1 && errno == EINTR);
331 if (len == -1 && errno == EAGAIN) {
332 break;
335 if (len != sizeof(info)) {
336 printf("read from sigfd returned %zd: %m\n", len);
337 return;
340 sigaction(info.ssi_signo, NULL, &action);
341 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
342 action.sa_sigaction(info.ssi_signo,
343 (siginfo_t *)&info, NULL);
344 } else if (action.sa_handler) {
345 action.sa_handler(info.ssi_signo);
350 static int qemu_signalfd_init(sigset_t mask)
352 int sigfd;
354 sigfd = qemu_signalfd(&mask);
355 if (sigfd == -1) {
356 fprintf(stderr, "failed to create signalfd\n");
357 return -errno;
360 fcntl_setfl(sigfd, O_NONBLOCK);
362 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
363 (void *)(unsigned long) sigfd);
365 return 0;
368 static void qemu_kvm_eat_signals(CPUState *env)
370 struct timespec ts = { 0, 0 };
371 siginfo_t siginfo;
372 sigset_t waitset;
373 sigset_t chkset;
374 int r;
376 sigemptyset(&waitset);
377 sigaddset(&waitset, SIG_IPI);
378 sigaddset(&waitset, SIGBUS);
380 do {
381 r = sigtimedwait(&waitset, &siginfo, &ts);
382 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
383 perror("sigtimedwait");
384 exit(1);
387 switch (r) {
388 case SIGBUS:
389 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
390 sigbus_reraise();
392 break;
393 default:
394 break;
397 r = sigpending(&chkset);
398 if (r == -1) {
399 perror("sigpending");
400 exit(1);
402 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
404 #ifndef CONFIG_IOTHREAD
405 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
406 qemu_notify_event();
408 #endif
411 #else /* _WIN32 */
413 HANDLE qemu_event_handle;
415 static void dummy_event_handler(void *opaque)
419 static int qemu_event_init(void)
421 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
422 if (!qemu_event_handle) {
423 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
424 return -1;
426 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
427 return 0;
430 static void qemu_event_increment(void)
432 if (!SetEvent(qemu_event_handle)) {
433 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
434 GetLastError());
435 exit (1);
439 static void qemu_kvm_eat_signals(CPUState *env)
442 #endif /* _WIN32 */
444 #ifndef CONFIG_IOTHREAD
445 static void qemu_kvm_init_cpu_signals(CPUState *env)
447 #if 0 /* Causes regressions: autotest WinXP.64 migrate.tcp */
448 #ifndef _WIN32
449 int r;
450 sigset_t set;
451 struct sigaction sigact;
453 memset(&sigact, 0, sizeof(sigact));
454 sigact.sa_handler = dummy_signal;
455 sigaction(SIG_IPI, &sigact, NULL);
457 sigemptyset(&set);
458 sigaddset(&set, SIG_IPI);
459 sigaddset(&set, SIGIO);
460 sigaddset(&set, SIGALRM);
461 pthread_sigmask(SIG_BLOCK, &set, NULL);
463 pthread_sigmask(SIG_BLOCK, NULL, &set);
464 sigdelset(&set, SIG_IPI);
465 sigdelset(&set, SIGBUS);
466 sigdelset(&set, SIGIO);
467 sigdelset(&set, SIGALRM);
468 r = kvm_set_signal_mask(env, &set);
469 if (r) {
470 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
471 exit(1);
473 #endif
474 #endif
477 #ifndef _WIN32
478 static sigset_t block_synchronous_signals(void)
480 sigset_t set;
482 sigemptyset(&set);
483 sigaddset(&set, SIGBUS);
484 if (kvm_enabled()) {
486 * We need to process timer signals synchronously to avoid a race
487 * between exit_request check and KVM vcpu entry.
489 sigaddset(&set, SIGIO);
490 sigaddset(&set, SIGALRM);
493 return set;
495 #endif
497 int qemu_init_main_loop(void)
499 #ifndef _WIN32
500 sigset_t blocked_signals;
501 int ret;
503 blocked_signals = block_synchronous_signals();
505 ret = qemu_signalfd_init(blocked_signals);
506 if (ret) {
507 return ret;
509 #endif
511 qemu_init_sigbus();
513 return qemu_event_init();
516 void qemu_main_loop_start(void)
520 void qemu_init_vcpu(void *_env)
522 CPUState *env = _env;
523 int r;
525 env->nr_cores = smp_cores;
526 env->nr_threads = smp_threads;
528 if (kvm_enabled()) {
529 r = kvm_init_vcpu(env);
530 if (r < 0) {
531 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
532 exit(1);
534 qemu_kvm_init_cpu_signals(env);
538 int qemu_cpu_self(void *env)
540 return 1;
543 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
545 func(data);
548 void resume_all_vcpus(void)
552 void pause_all_vcpus(void)
556 void qemu_cpu_kick(void *env)
560 void qemu_cpu_kick_self(void)
562 #ifndef _WIN32
563 assert(cpu_single_env);
565 raise(SIG_IPI);
566 #else
567 abort();
568 #endif
571 void qemu_notify_event(void)
573 CPUState *env = cpu_single_env;
575 qemu_event_increment ();
576 if (env) {
577 cpu_exit(env);
579 if (next_cpu && env != next_cpu) {
580 cpu_exit(next_cpu);
582 exit_request = 1;
585 #if defined(OBSOLETE_KVM_IMPL) || !defined(CONFIG_KVM)
586 void qemu_mutex_lock_iothread(void) {}
587 void qemu_mutex_unlock_iothread(void) {}
588 #endif
590 void cpu_stop_current(void)
594 void vm_stop(int reason)
596 do_vm_stop(reason);
599 #else /* CONFIG_IOTHREAD */
601 #include "qemu-thread.h"
603 QemuMutex qemu_global_mutex;
604 static QemuMutex qemu_fair_mutex;
606 static QemuThread io_thread;
608 static QemuThread *tcg_cpu_thread;
609 static QemuCond *tcg_halt_cond;
611 static int qemu_system_ready;
612 /* cpu creation */
613 static QemuCond qemu_cpu_cond;
614 /* system init */
615 static QemuCond qemu_system_cond;
616 static QemuCond qemu_pause_cond;
617 static QemuCond qemu_work_cond;
619 static void cpu_signal(int sig)
621 if (cpu_single_env) {
622 cpu_exit(cpu_single_env);
624 exit_request = 1;
627 static void qemu_kvm_init_cpu_signals(CPUState *env)
629 int r;
630 sigset_t set;
631 struct sigaction sigact;
633 memset(&sigact, 0, sizeof(sigact));
634 sigact.sa_handler = dummy_signal;
635 sigaction(SIG_IPI, &sigact, NULL);
637 pthread_sigmask(SIG_BLOCK, NULL, &set);
638 sigdelset(&set, SIG_IPI);
639 sigdelset(&set, SIGBUS);
640 r = kvm_set_signal_mask(env, &set);
641 if (r) {
642 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
643 exit(1);
647 static void qemu_tcg_init_cpu_signals(void)
649 sigset_t set;
650 struct sigaction sigact;
652 memset(&sigact, 0, sizeof(sigact));
653 sigact.sa_handler = cpu_signal;
654 sigaction(SIG_IPI, &sigact, NULL);
656 sigemptyset(&set);
657 sigaddset(&set, SIG_IPI);
658 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
661 static sigset_t block_io_signals(void)
663 sigset_t set;
665 /* SIGUSR2 used by posix-aio-compat.c */
666 sigemptyset(&set);
667 sigaddset(&set, SIGUSR2);
668 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
670 sigemptyset(&set);
671 sigaddset(&set, SIGIO);
672 sigaddset(&set, SIGALRM);
673 sigaddset(&set, SIG_IPI);
674 sigaddset(&set, SIGBUS);
675 pthread_sigmask(SIG_BLOCK, &set, NULL);
677 return set;
680 int qemu_init_main_loop(void)
682 int ret;
683 sigset_t blocked_signals;
685 qemu_init_sigbus();
687 blocked_signals = block_io_signals();
689 ret = qemu_signalfd_init(blocked_signals);
690 if (ret) {
691 return ret;
694 /* Note eventfd must be drained before signalfd handlers run */
695 ret = qemu_event_init();
696 if (ret) {
697 return ret;
700 qemu_cond_init(&qemu_cpu_cond);
701 qemu_cond_init(&qemu_system_cond);
702 qemu_cond_init(&qemu_pause_cond);
703 qemu_cond_init(&qemu_work_cond);
704 qemu_mutex_init(&qemu_fair_mutex);
705 qemu_mutex_init(&qemu_global_mutex);
706 qemu_mutex_lock(&qemu_global_mutex);
708 qemu_thread_self(&io_thread);
710 return 0;
713 void qemu_main_loop_start(void)
715 qemu_system_ready = 1;
716 qemu_cond_broadcast(&qemu_system_cond);
719 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
721 struct qemu_work_item wi;
723 if (qemu_cpu_self(env)) {
724 func(data);
725 return;
728 wi.func = func;
729 wi.data = data;
730 if (!env->queued_work_first) {
731 env->queued_work_first = &wi;
732 } else {
733 env->queued_work_last->next = &wi;
735 env->queued_work_last = &wi;
736 wi.next = NULL;
737 wi.done = false;
739 qemu_cpu_kick(env);
740 while (!wi.done) {
741 CPUState *self_env = cpu_single_env;
743 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
744 cpu_single_env = self_env;
748 static void flush_queued_work(CPUState *env)
750 struct qemu_work_item *wi;
752 if (!env->queued_work_first) {
753 return;
756 while ((wi = env->queued_work_first)) {
757 env->queued_work_first = wi->next;
758 wi->func(wi->data);
759 wi->done = true;
761 env->queued_work_last = NULL;
762 qemu_cond_broadcast(&qemu_work_cond);
765 static void qemu_wait_io_event_common(CPUState *env)
767 if (env->stop) {
768 env->stop = 0;
769 env->stopped = 1;
770 qemu_cond_signal(&qemu_pause_cond);
772 flush_queued_work(env);
773 env->thread_kicked = false;
776 static void qemu_tcg_wait_io_event(void)
778 CPUState *env;
780 while (all_cpu_threads_idle()) {
781 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
784 qemu_mutex_unlock(&qemu_global_mutex);
787 * Users of qemu_global_mutex can be starved, having no chance
788 * to acquire it since this path will get to it first.
789 * So use another lock to provide fairness.
791 qemu_mutex_lock(&qemu_fair_mutex);
792 qemu_mutex_unlock(&qemu_fair_mutex);
794 qemu_mutex_lock(&qemu_global_mutex);
796 for (env = first_cpu; env != NULL; env = env->next_cpu) {
797 qemu_wait_io_event_common(env);
801 static void qemu_kvm_wait_io_event(CPUState *env)
803 while (cpu_thread_is_idle(env)) {
804 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
807 qemu_kvm_eat_signals(env);
808 qemu_wait_io_event_common(env);
811 static void *qemu_kvm_cpu_thread_fn(void *arg)
813 CPUState *env = arg;
814 int r;
816 qemu_mutex_lock(&qemu_global_mutex);
817 qemu_thread_self(env->thread);
819 r = kvm_init_vcpu(env);
820 if (r < 0) {
821 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
822 exit(1);
825 qemu_kvm_init_cpu_signals(env);
827 /* signal CPU creation */
828 env->created = 1;
829 qemu_cond_signal(&qemu_cpu_cond);
831 /* and wait for machine initialization */
832 while (!qemu_system_ready) {
833 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
836 while (1) {
837 if (cpu_can_run(env)) {
838 r = kvm_cpu_exec(env);
839 if (r == EXCP_DEBUG) {
840 cpu_handle_debug_exception(env);
843 qemu_kvm_wait_io_event(env);
846 return NULL;
849 static void *qemu_tcg_cpu_thread_fn(void *arg)
851 CPUState *env = arg;
853 qemu_tcg_init_cpu_signals();
854 qemu_thread_self(env->thread);
856 /* signal CPU creation */
857 qemu_mutex_lock(&qemu_global_mutex);
858 for (env = first_cpu; env != NULL; env = env->next_cpu) {
859 env->created = 1;
861 qemu_cond_signal(&qemu_cpu_cond);
863 /* and wait for machine initialization */
864 while (!qemu_system_ready) {
865 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
868 while (1) {
869 cpu_exec_all();
870 qemu_tcg_wait_io_event();
873 return NULL;
876 void qemu_cpu_kick(void *_env)
878 CPUState *env = _env;
880 qemu_cond_broadcast(env->halt_cond);
881 if (!env->thread_kicked) {
882 qemu_thread_signal(env->thread, SIG_IPI);
883 env->thread_kicked = true;
887 void qemu_cpu_kick_self(void)
889 assert(cpu_single_env);
891 if (!cpu_single_env->thread_kicked) {
892 qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
893 cpu_single_env->thread_kicked = true;
897 int qemu_cpu_self(void *_env)
899 CPUState *env = _env;
900 QemuThread this;
902 qemu_thread_self(&this);
904 return qemu_thread_equal(&this, env->thread);
907 void qemu_mutex_lock_iothread(void)
909 if (kvm_enabled()) {
910 qemu_mutex_lock(&qemu_global_mutex);
911 } else {
912 qemu_mutex_lock(&qemu_fair_mutex);
913 if (qemu_mutex_trylock(&qemu_global_mutex)) {
914 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
915 qemu_mutex_lock(&qemu_global_mutex);
917 qemu_mutex_unlock(&qemu_fair_mutex);
921 void qemu_mutex_unlock_iothread(void)
923 qemu_mutex_unlock(&qemu_global_mutex);
926 static int all_vcpus_paused(void)
928 CPUState *penv = first_cpu;
930 while (penv) {
931 if (!penv->stopped) {
932 return 0;
934 penv = (CPUState *)penv->next_cpu;
937 return 1;
940 void pause_all_vcpus(void)
942 CPUState *penv = first_cpu;
944 while (penv) {
945 penv->stop = 1;
946 qemu_cpu_kick(penv);
947 penv = (CPUState *)penv->next_cpu;
950 while (!all_vcpus_paused()) {
951 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
952 penv = first_cpu;
953 while (penv) {
954 qemu_cpu_kick(penv);
955 penv = (CPUState *)penv->next_cpu;
960 void resume_all_vcpus(void)
962 CPUState *penv = first_cpu;
964 while (penv) {
965 penv->stop = 0;
966 penv->stopped = 0;
967 qemu_cpu_kick(penv);
968 penv = (CPUState *)penv->next_cpu;
972 static void qemu_tcg_init_vcpu(void *_env)
974 CPUState *env = _env;
976 /* share a single thread for all cpus with TCG */
977 if (!tcg_cpu_thread) {
978 env->thread = qemu_mallocz(sizeof(QemuThread));
979 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
980 qemu_cond_init(env->halt_cond);
981 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
982 while (env->created == 0) {
983 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
985 tcg_cpu_thread = env->thread;
986 tcg_halt_cond = env->halt_cond;
987 } else {
988 env->thread = tcg_cpu_thread;
989 env->halt_cond = tcg_halt_cond;
993 static void qemu_kvm_start_vcpu(CPUState *env)
995 env->thread = qemu_mallocz(sizeof(QemuThread));
996 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
997 qemu_cond_init(env->halt_cond);
998 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
999 while (env->created == 0) {
1000 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
1004 void qemu_init_vcpu(void *_env)
1006 CPUState *env = _env;
1008 env->nr_cores = smp_cores;
1009 env->nr_threads = smp_threads;
1010 if (kvm_enabled()) {
1011 qemu_kvm_start_vcpu(env);
1012 } else {
1013 qemu_tcg_init_vcpu(env);
1017 void qemu_notify_event(void)
1019 qemu_event_increment();
1022 void cpu_stop_current(void)
1024 if (cpu_single_env) {
1025 cpu_single_env->stopped = 1;
1026 cpu_exit(cpu_single_env);
1030 void vm_stop(int reason)
1032 QemuThread me;
1033 qemu_thread_self(&me);
1035 if (!qemu_thread_equal(&me, &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_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 if (next_cpu == NULL) {
1092 next_cpu = first_cpu;
1094 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1095 CPUState *env = next_cpu;
1097 qemu_clock_enable(vm_clock,
1098 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1100 if (qemu_alarm_pending()) {
1101 break;
1103 if (cpu_can_run(env)) {
1104 if (kvm_enabled()) {
1105 r = kvm_cpu_exec(env);
1106 qemu_kvm_eat_signals(env);
1107 } else {
1108 r = tcg_cpu_exec(env);
1110 if (r == EXCP_DEBUG) {
1111 cpu_handle_debug_exception(env);
1112 break;
1114 } else if (env->stop) {
1115 break;
1118 exit_request = 0;
1119 return !all_cpu_threads_idle();
1122 void set_numa_modes(void)
1124 CPUState *env;
1125 int i;
1127 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1128 for (i = 0; i < nb_numa_nodes; i++) {
1129 if (node_cpumask[i] & (1 << env->cpu_index)) {
1130 env->numa_node = i;
1136 void set_cpu_log(const char *optarg)
1138 int mask;
1139 const CPULogItem *item;
1141 mask = cpu_str_to_log_mask(optarg);
1142 if (!mask) {
1143 printf("Log items (comma separated):\n");
1144 for (item = cpu_log_items; item->mask != 0; item++) {
1145 printf("%-10s %s\n", item->name, item->help);
1147 exit(1);
1149 cpu_set_log(mask);
1152 /* Return the virtual CPU time, based on the instruction counter. */
1153 int64_t cpu_get_icount(void)
1155 int64_t icount;
1156 CPUState *env = cpu_single_env;;
1158 icount = qemu_icount;
1159 if (env) {
1160 if (!can_do_io(env)) {
1161 fprintf(stderr, "Bad clock read\n");
1163 icount -= (env->icount_decr.u16.low + env->icount_extra);
1165 return qemu_icount_bias + (icount << icount_time_shift);
1168 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1170 /* XXX: implement xxx_cpu_list for targets that still miss it */
1171 #if defined(cpu_list_id)
1172 cpu_list_id(f, cpu_fprintf, optarg);
1173 #elif defined(cpu_list)
1174 cpu_list(f, cpu_fprintf); /* deprecated */
1175 #endif