really fix -icount in the iothread case
[qemu.git] / cpus.c
blobcbeac7a40e5b7bb4461799de954a594aabcc3f76
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 static 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 static void dummy_signal(int sig)
351 /* If we have signalfd, we mask out the signals we want to handle and then
352 * use signalfd to listen for them. We rely on whatever the current signal
353 * handler is to dispatch the signals when we receive them.
355 static void sigfd_handler(void *opaque)
357 int fd = (intptr_t)opaque;
358 struct qemu_signalfd_siginfo info;
359 struct sigaction action;
360 ssize_t len;
362 while (1) {
363 do {
364 len = read(fd, &info, sizeof(info));
365 } while (len == -1 && errno == EINTR);
367 if (len == -1 && errno == EAGAIN) {
368 break;
371 if (len != sizeof(info)) {
372 printf("read from sigfd returned %zd: %m\n", len);
373 return;
376 sigaction(info.ssi_signo, NULL, &action);
377 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
378 action.sa_sigaction(info.ssi_signo,
379 (siginfo_t *)&info, NULL);
380 } else if (action.sa_handler) {
381 action.sa_handler(info.ssi_signo);
386 static int qemu_signal_init(void)
388 int sigfd;
389 sigset_t set;
391 #ifdef CONFIG_IOTHREAD
392 /* SIGUSR2 used by posix-aio-compat.c */
393 sigemptyset(&set);
394 sigaddset(&set, SIGUSR2);
395 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
397 sigemptyset(&set);
398 sigaddset(&set, SIGIO);
399 sigaddset(&set, SIGALRM);
400 sigaddset(&set, SIG_IPI);
401 sigaddset(&set, SIGBUS);
402 pthread_sigmask(SIG_BLOCK, &set, NULL);
403 #else
404 sigemptyset(&set);
405 sigaddset(&set, SIGBUS);
406 if (kvm_enabled()) {
408 * We need to process timer signals synchronously to avoid a race
409 * between exit_request check and KVM vcpu entry.
411 sigaddset(&set, SIGIO);
412 sigaddset(&set, SIGALRM);
414 #endif
416 sigfd = qemu_signalfd(&set);
417 if (sigfd == -1) {
418 fprintf(stderr, "failed to create signalfd\n");
419 return -errno;
422 fcntl_setfl(sigfd, O_NONBLOCK);
424 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
425 (void *)(intptr_t)sigfd);
427 return 0;
430 static void qemu_kvm_init_cpu_signals(CPUState *env)
432 int r;
433 sigset_t set;
434 struct sigaction sigact;
436 memset(&sigact, 0, sizeof(sigact));
437 sigact.sa_handler = dummy_signal;
438 sigaction(SIG_IPI, &sigact, NULL);
440 #ifdef CONFIG_IOTHREAD
441 pthread_sigmask(SIG_BLOCK, NULL, &set);
442 sigdelset(&set, SIG_IPI);
443 sigdelset(&set, SIGBUS);
444 r = kvm_set_signal_mask(env, &set);
445 if (r) {
446 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
447 exit(1);
449 #else
450 sigemptyset(&set);
451 sigaddset(&set, SIG_IPI);
452 sigaddset(&set, SIGIO);
453 sigaddset(&set, SIGALRM);
454 pthread_sigmask(SIG_BLOCK, &set, NULL);
456 pthread_sigmask(SIG_BLOCK, NULL, &set);
457 sigdelset(&set, SIGIO);
458 sigdelset(&set, SIGALRM);
459 #endif
460 sigdelset(&set, SIG_IPI);
461 sigdelset(&set, SIGBUS);
462 r = kvm_set_signal_mask(env, &set);
463 if (r) {
464 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
465 exit(1);
469 static void qemu_tcg_init_cpu_signals(void)
471 #ifdef CONFIG_IOTHREAD
472 sigset_t set;
473 struct sigaction sigact;
475 memset(&sigact, 0, sizeof(sigact));
476 sigact.sa_handler = cpu_signal;
477 sigaction(SIG_IPI, &sigact, NULL);
479 sigemptyset(&set);
480 sigaddset(&set, SIG_IPI);
481 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
482 #endif
485 #else /* _WIN32 */
487 HANDLE qemu_event_handle;
489 static void dummy_event_handler(void *opaque)
493 static int qemu_event_init(void)
495 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
496 if (!qemu_event_handle) {
497 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
498 return -1;
500 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
501 return 0;
504 static void qemu_event_increment(void)
506 if (!SetEvent(qemu_event_handle)) {
507 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
508 GetLastError());
509 exit (1);
513 static int qemu_signal_init(void)
515 return 0;
518 static void qemu_kvm_init_cpu_signals(CPUState *env)
520 abort();
523 static void qemu_tcg_init_cpu_signals(void)
526 #endif /* _WIN32 */
528 #ifndef CONFIG_IOTHREAD
529 int qemu_init_main_loop(void)
531 int ret;
533 ret = qemu_signal_init();
534 if (ret) {
535 return ret;
538 qemu_init_sigbus();
540 return qemu_event_init();
543 void qemu_main_loop_start(void)
547 void qemu_init_vcpu(void *_env)
549 CPUState *env = _env;
550 int r;
552 env->nr_cores = smp_cores;
553 env->nr_threads = smp_threads;
555 if (kvm_enabled()) {
556 r = kvm_init_vcpu(env);
557 if (r < 0) {
558 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
559 exit(1);
561 qemu_kvm_init_cpu_signals(env);
562 } else {
563 qemu_tcg_init_cpu_signals();
567 int qemu_cpu_is_self(void *env)
569 return 1;
572 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
574 func(data);
577 void resume_all_vcpus(void)
581 void pause_all_vcpus(void)
585 void qemu_cpu_kick(void *env)
589 void qemu_cpu_kick_self(void)
591 #ifndef _WIN32
592 assert(cpu_single_env);
594 raise(SIG_IPI);
595 #else
596 abort();
597 #endif
600 void qemu_notify_event(void)
602 CPUState *env = cpu_single_env;
604 qemu_event_increment ();
605 if (env) {
606 cpu_exit(env);
608 if (next_cpu && env != next_cpu) {
609 cpu_exit(next_cpu);
611 exit_request = 1;
614 void qemu_mutex_lock_iothread(void) {}
615 void qemu_mutex_unlock_iothread(void) {}
617 void cpu_stop_current(void)
621 void vm_stop(int reason)
623 do_vm_stop(reason);
626 #else /* CONFIG_IOTHREAD */
628 QemuMutex qemu_global_mutex;
629 static QemuMutex qemu_fair_mutex;
631 static QemuThread io_thread;
633 static QemuThread *tcg_cpu_thread;
634 static QemuCond *tcg_halt_cond;
636 static int qemu_system_ready;
637 /* cpu creation */
638 static QemuCond qemu_cpu_cond;
639 /* system init */
640 static QemuCond qemu_system_cond;
641 static QemuCond qemu_pause_cond;
642 static QemuCond qemu_work_cond;
644 int qemu_init_main_loop(void)
646 int ret;
648 qemu_init_sigbus();
650 ret = qemu_signal_init();
651 if (ret) {
652 return ret;
655 /* Note eventfd must be drained before signalfd handlers run */
656 ret = qemu_event_init();
657 if (ret) {
658 return ret;
661 qemu_cond_init(&qemu_cpu_cond);
662 qemu_cond_init(&qemu_system_cond);
663 qemu_cond_init(&qemu_pause_cond);
664 qemu_cond_init(&qemu_work_cond);
665 qemu_mutex_init(&qemu_fair_mutex);
666 qemu_mutex_init(&qemu_global_mutex);
667 qemu_mutex_lock(&qemu_global_mutex);
669 qemu_thread_get_self(&io_thread);
671 return 0;
674 void qemu_main_loop_start(void)
676 qemu_system_ready = 1;
677 qemu_cond_broadcast(&qemu_system_cond);
680 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
682 struct qemu_work_item wi;
684 if (qemu_cpu_is_self(env)) {
685 func(data);
686 return;
689 wi.func = func;
690 wi.data = data;
691 if (!env->queued_work_first) {
692 env->queued_work_first = &wi;
693 } else {
694 env->queued_work_last->next = &wi;
696 env->queued_work_last = &wi;
697 wi.next = NULL;
698 wi.done = false;
700 qemu_cpu_kick(env);
701 while (!wi.done) {
702 CPUState *self_env = cpu_single_env;
704 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
705 cpu_single_env = self_env;
709 static void flush_queued_work(CPUState *env)
711 struct qemu_work_item *wi;
713 if (!env->queued_work_first) {
714 return;
717 while ((wi = env->queued_work_first)) {
718 env->queued_work_first = wi->next;
719 wi->func(wi->data);
720 wi->done = true;
722 env->queued_work_last = NULL;
723 qemu_cond_broadcast(&qemu_work_cond);
726 static void qemu_wait_io_event_common(CPUState *env)
728 if (env->stop) {
729 env->stop = 0;
730 env->stopped = 1;
731 qemu_cond_signal(&qemu_pause_cond);
733 flush_queued_work(env);
734 env->thread_kicked = false;
737 static void qemu_tcg_wait_io_event(void)
739 CPUState *env;
741 while (all_cpu_threads_idle()) {
742 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
745 qemu_mutex_unlock(&qemu_global_mutex);
748 * Users of qemu_global_mutex can be starved, having no chance
749 * to acquire it since this path will get to it first.
750 * So use another lock to provide fairness.
752 qemu_mutex_lock(&qemu_fair_mutex);
753 qemu_mutex_unlock(&qemu_fair_mutex);
755 qemu_mutex_lock(&qemu_global_mutex);
757 for (env = first_cpu; env != NULL; env = env->next_cpu) {
758 qemu_wait_io_event_common(env);
762 static void qemu_kvm_wait_io_event(CPUState *env)
764 while (cpu_thread_is_idle(env)) {
765 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
768 qemu_kvm_eat_signals(env);
769 qemu_wait_io_event_common(env);
772 static void *qemu_kvm_cpu_thread_fn(void *arg)
774 CPUState *env = arg;
775 int r;
777 qemu_mutex_lock(&qemu_global_mutex);
778 qemu_thread_get_self(env->thread);
779 env->thread_id = qemu_get_thread_id();
781 r = kvm_init_vcpu(env);
782 if (r < 0) {
783 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
784 exit(1);
787 qemu_kvm_init_cpu_signals(env);
789 /* signal CPU creation */
790 env->created = 1;
791 qemu_cond_signal(&qemu_cpu_cond);
793 /* and wait for machine initialization */
794 while (!qemu_system_ready) {
795 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
798 while (1) {
799 if (cpu_can_run(env)) {
800 r = kvm_cpu_exec(env);
801 if (r == EXCP_DEBUG) {
802 cpu_handle_guest_debug(env);
805 qemu_kvm_wait_io_event(env);
808 return NULL;
811 static void *qemu_tcg_cpu_thread_fn(void *arg)
813 CPUState *env = arg;
815 qemu_tcg_init_cpu_signals();
816 qemu_thread_get_self(env->thread);
818 /* signal CPU creation */
819 qemu_mutex_lock(&qemu_global_mutex);
820 for (env = first_cpu; env != NULL; env = env->next_cpu) {
821 env->thread_id = qemu_get_thread_id();
822 env->created = 1;
824 qemu_cond_signal(&qemu_cpu_cond);
826 /* and wait for machine initialization */
827 while (!qemu_system_ready) {
828 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
831 while (1) {
832 cpu_exec_all();
833 if (use_icount && qemu_next_deadline() <= 0) {
834 qemu_notify_event();
836 qemu_tcg_wait_io_event();
839 return NULL;
842 static void qemu_cpu_kick_thread(CPUState *env)
844 #ifndef _WIN32
845 int err;
847 err = pthread_kill(env->thread->thread, SIG_IPI);
848 if (err) {
849 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
850 exit(1);
852 #else /* _WIN32 */
853 if (!qemu_cpu_is_self(env)) {
854 SuspendThread(env->thread->thread);
855 cpu_signal(0);
856 ResumeThread(env->thread->thread);
858 #endif
861 void qemu_cpu_kick(void *_env)
863 CPUState *env = _env;
865 qemu_cond_broadcast(env->halt_cond);
866 if (!env->thread_kicked) {
867 qemu_cpu_kick_thread(env);
868 env->thread_kicked = true;
872 void qemu_cpu_kick_self(void)
874 #ifndef _WIN32
875 assert(cpu_single_env);
877 if (!cpu_single_env->thread_kicked) {
878 qemu_cpu_kick_thread(cpu_single_env);
879 cpu_single_env->thread_kicked = true;
881 #else
882 abort();
883 #endif
886 int qemu_cpu_is_self(void *_env)
888 CPUState *env = _env;
890 return qemu_thread_is_self(env->thread);
893 void qemu_mutex_lock_iothread(void)
895 if (kvm_enabled()) {
896 qemu_mutex_lock(&qemu_global_mutex);
897 } else {
898 qemu_mutex_lock(&qemu_fair_mutex);
899 if (qemu_mutex_trylock(&qemu_global_mutex)) {
900 qemu_cpu_kick_thread(first_cpu);
901 qemu_mutex_lock(&qemu_global_mutex);
903 qemu_mutex_unlock(&qemu_fair_mutex);
907 void qemu_mutex_unlock_iothread(void)
909 qemu_mutex_unlock(&qemu_global_mutex);
912 static int all_vcpus_paused(void)
914 CPUState *penv = first_cpu;
916 while (penv) {
917 if (!penv->stopped) {
918 return 0;
920 penv = (CPUState *)penv->next_cpu;
923 return 1;
926 void pause_all_vcpus(void)
928 CPUState *penv = first_cpu;
930 while (penv) {
931 penv->stop = 1;
932 qemu_cpu_kick(penv);
933 penv = (CPUState *)penv->next_cpu;
936 while (!all_vcpus_paused()) {
937 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
938 penv = first_cpu;
939 while (penv) {
940 qemu_cpu_kick(penv);
941 penv = (CPUState *)penv->next_cpu;
946 void resume_all_vcpus(void)
948 CPUState *penv = first_cpu;
950 while (penv) {
951 penv->stop = 0;
952 penv->stopped = 0;
953 qemu_cpu_kick(penv);
954 penv = (CPUState *)penv->next_cpu;
958 static void qemu_tcg_init_vcpu(void *_env)
960 CPUState *env = _env;
962 /* share a single thread for all cpus with TCG */
963 if (!tcg_cpu_thread) {
964 env->thread = qemu_mallocz(sizeof(QemuThread));
965 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
966 qemu_cond_init(env->halt_cond);
967 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
968 while (env->created == 0) {
969 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
971 tcg_cpu_thread = env->thread;
972 tcg_halt_cond = env->halt_cond;
973 } else {
974 env->thread = tcg_cpu_thread;
975 env->halt_cond = tcg_halt_cond;
979 static void qemu_kvm_start_vcpu(CPUState *env)
981 env->thread = qemu_mallocz(sizeof(QemuThread));
982 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
983 qemu_cond_init(env->halt_cond);
984 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
985 while (env->created == 0) {
986 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
990 void qemu_init_vcpu(void *_env)
992 CPUState *env = _env;
994 env->nr_cores = smp_cores;
995 env->nr_threads = smp_threads;
996 if (kvm_enabled()) {
997 qemu_kvm_start_vcpu(env);
998 } else {
999 qemu_tcg_init_vcpu(env);
1003 void qemu_notify_event(void)
1005 qemu_event_increment();
1008 void cpu_stop_current(void)
1010 if (cpu_single_env) {
1011 cpu_single_env->stop = 0;
1012 cpu_single_env->stopped = 1;
1013 cpu_exit(cpu_single_env);
1014 qemu_cond_signal(&qemu_pause_cond);
1018 void vm_stop(int reason)
1020 if (!qemu_thread_is_self(&io_thread)) {
1021 qemu_system_vmstop_request(reason);
1023 * FIXME: should not return to device code in case
1024 * vm_stop() has been requested.
1026 cpu_stop_current();
1027 return;
1029 do_vm_stop(reason);
1032 #endif
1034 static int tcg_cpu_exec(CPUState *env)
1036 int ret;
1037 #ifdef CONFIG_PROFILER
1038 int64_t ti;
1039 #endif
1041 #ifdef CONFIG_PROFILER
1042 ti = profile_getclock();
1043 #endif
1044 if (use_icount) {
1045 int64_t count;
1046 int decr;
1047 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1048 env->icount_decr.u16.low = 0;
1049 env->icount_extra = 0;
1050 count = qemu_icount_round (qemu_next_deadline());
1051 qemu_icount += count;
1052 decr = (count > 0xffff) ? 0xffff : count;
1053 count -= decr;
1054 env->icount_decr.u16.low = decr;
1055 env->icount_extra = count;
1057 ret = cpu_exec(env);
1058 #ifdef CONFIG_PROFILER
1059 qemu_time += profile_getclock() - ti;
1060 #endif
1061 if (use_icount) {
1062 /* Fold pending instructions back into the
1063 instruction counter, and clear the interrupt flag. */
1064 qemu_icount -= (env->icount_decr.u16.low
1065 + env->icount_extra);
1066 env->icount_decr.u32 = 0;
1067 env->icount_extra = 0;
1069 return ret;
1072 bool cpu_exec_all(void)
1074 int r;
1076 if (next_cpu == NULL) {
1077 next_cpu = first_cpu;
1079 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1080 CPUState *env = next_cpu;
1082 qemu_clock_enable(vm_clock,
1083 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1085 #ifndef CONFIG_IOTHREAD
1086 if (qemu_alarm_pending()) {
1087 break;
1089 #endif
1090 if (cpu_can_run(env)) {
1091 if (kvm_enabled()) {
1092 r = kvm_cpu_exec(env);
1093 qemu_kvm_eat_signals(env);
1094 } else {
1095 r = tcg_cpu_exec(env);
1097 if (r == EXCP_DEBUG) {
1098 cpu_handle_guest_debug(env);
1099 break;
1101 } else if (env->stop || env->stopped) {
1102 break;
1105 exit_request = 0;
1106 return !all_cpu_threads_idle();
1109 void set_numa_modes(void)
1111 CPUState *env;
1112 int i;
1114 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1115 for (i = 0; i < nb_numa_nodes; i++) {
1116 if (node_cpumask[i] & (1 << env->cpu_index)) {
1117 env->numa_node = i;
1123 void set_cpu_log(const char *optarg)
1125 int mask;
1126 const CPULogItem *item;
1128 mask = cpu_str_to_log_mask(optarg);
1129 if (!mask) {
1130 printf("Log items (comma separated):\n");
1131 for (item = cpu_log_items; item->mask != 0; item++) {
1132 printf("%-10s %s\n", item->name, item->help);
1134 exit(1);
1136 cpu_set_log(mask);
1139 /* Return the virtual CPU time, based on the instruction counter. */
1140 int64_t cpu_get_icount(void)
1142 int64_t icount;
1143 CPUState *env = cpu_single_env;;
1145 icount = qemu_icount;
1146 if (env) {
1147 if (!can_do_io(env)) {
1148 fprintf(stderr, "Bad clock read\n");
1150 icount -= (env->icount_decr.u16.low + env->icount_extra);
1152 return qemu_icount_bias + (icount << icount_time_shift);
1155 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1157 /* XXX: implement xxx_cpu_list for targets that still miss it */
1158 #if defined(cpu_list_id)
1159 cpu_list_id(f, cpu_fprintf, optarg);
1160 #elif defined(cpu_list)
1161 cpu_list(f, cpu_fprintf); /* deprecated */
1162 #endif