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
25 /* Needed early for CONFIG_BSD etc. */
26 #include "config-host.h"
34 #include "qemu-thread.h"
42 #define SIG_IPI (SIGRTMIN+4)
44 #define SIG_IPI SIGUSR1
49 #include <sys/prctl.h>
52 #define PR_MCE_KILL 33
55 #ifndef PR_MCE_KILL_SET
56 #define PR_MCE_KILL_SET 1
59 #ifndef PR_MCE_KILL_EARLY
60 #define PR_MCE_KILL_EARLY 1
63 #endif /* CONFIG_LINUX */
65 static CPUState *next_cpu;
67 /***********************************************************/
68 /* guest cycle counter */
70 /* Conversion factor from emulated instructions to virtual clock ticks. */
71 static int icount_time_shift;
72 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
73 #define MAX_ICOUNT_SHIFT 10
74 /* Compensate for varying guest execution speed. */
75 static int64_t qemu_icount_bias;
76 static QEMUTimer *icount_rt_timer;
77 static QEMUTimer *icount_vm_timer;
78 static QEMUTimer *icount_warp_timer;
79 static int64_t vm_clock_warp_start;
80 static int64_t qemu_icount;
82 typedef struct TimersState {
83 int64_t cpu_ticks_prev;
84 int64_t cpu_ticks_offset;
85 int64_t cpu_clock_offset;
86 int32_t cpu_ticks_enabled;
90 TimersState timers_state;
92 /* Return the virtual CPU time, based on the instruction counter. */
93 int64_t cpu_get_icount(void)
96 CPUState *env = cpu_single_env;;
100 if (!can_do_io(env)) {
101 fprintf(stderr, "Bad clock read\n");
103 icount -= (env->icount_decr.u16.low + env->icount_extra);
105 return qemu_icount_bias + (icount << icount_time_shift);
108 /* return the host CPU cycle counter and handle stop/restart */
109 int64_t cpu_get_ticks(void)
112 return cpu_get_icount();
114 if (!timers_state.cpu_ticks_enabled) {
115 return timers_state.cpu_ticks_offset;
118 ticks = cpu_get_real_ticks();
119 if (timers_state.cpu_ticks_prev > ticks) {
120 /* Note: non increasing ticks may happen if the host uses
122 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
124 timers_state.cpu_ticks_prev = ticks;
125 return ticks + timers_state.cpu_ticks_offset;
129 /* return the host CPU monotonic timer and handle stop/restart */
130 int64_t cpu_get_clock(void)
133 if (!timers_state.cpu_ticks_enabled) {
134 return timers_state.cpu_clock_offset;
137 return ti + timers_state.cpu_clock_offset;
141 /* enable cpu_get_ticks() */
142 void cpu_enable_ticks(void)
144 if (!timers_state.cpu_ticks_enabled) {
145 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
146 timers_state.cpu_clock_offset -= get_clock();
147 timers_state.cpu_ticks_enabled = 1;
151 /* disable cpu_get_ticks() : the clock is stopped. You must not call
152 cpu_get_ticks() after that. */
153 void cpu_disable_ticks(void)
155 if (timers_state.cpu_ticks_enabled) {
156 timers_state.cpu_ticks_offset = cpu_get_ticks();
157 timers_state.cpu_clock_offset = cpu_get_clock();
158 timers_state.cpu_ticks_enabled = 0;
162 /* Correlation between real and virtual time is always going to be
163 fairly approximate, so ignore small variation.
164 When the guest is idle real and virtual time will be aligned in
166 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
168 static void icount_adjust(void)
173 static int64_t last_delta;
174 /* If the VM is not running, then do nothing. */
175 if (!runstate_is_running()) {
178 cur_time = cpu_get_clock();
179 cur_icount = qemu_get_clock_ns(vm_clock);
180 delta = cur_icount - cur_time;
181 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
183 && last_delta + ICOUNT_WOBBLE < delta * 2
184 && icount_time_shift > 0) {
185 /* The guest is getting too far ahead. Slow time down. */
189 && last_delta - ICOUNT_WOBBLE > delta * 2
190 && icount_time_shift < MAX_ICOUNT_SHIFT) {
191 /* The guest is getting too far behind. Speed time up. */
195 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
198 static void icount_adjust_rt(void *opaque)
200 qemu_mod_timer(icount_rt_timer,
201 qemu_get_clock_ms(rt_clock) + 1000);
205 static void icount_adjust_vm(void *opaque)
207 qemu_mod_timer(icount_vm_timer,
208 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
212 static int64_t qemu_icount_round(int64_t count)
214 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
217 static void icount_warp_rt(void *opaque)
219 if (vm_clock_warp_start == -1) {
223 if (runstate_is_running()) {
224 int64_t clock = qemu_get_clock_ns(rt_clock);
225 int64_t warp_delta = clock - vm_clock_warp_start;
226 if (use_icount == 1) {
227 qemu_icount_bias += warp_delta;
230 * In adaptive mode, do not let the vm_clock run too
231 * far ahead of real time.
233 int64_t cur_time = cpu_get_clock();
234 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
235 int64_t delta = cur_time - cur_icount;
236 qemu_icount_bias += MIN(warp_delta, delta);
238 if (qemu_clock_expired(vm_clock)) {
242 vm_clock_warp_start = -1;
245 void qemu_clock_warp(QEMUClock *clock)
250 * There are too many global variables to make the "warp" behavior
251 * applicable to other clocks. But a clock argument removes the
252 * need for if statements all over the place.
254 if (clock != vm_clock || !use_icount) {
259 * If the CPUs have been sleeping, advance the vm_clock timer now. This
260 * ensures that the deadline for the timer is computed correctly below.
261 * This also makes sure that the insn counter is synchronized before the
262 * CPU starts running, in case the CPU is woken by an event other than
263 * the earliest vm_clock timer.
265 icount_warp_rt(NULL);
266 if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock)) {
267 qemu_del_timer(icount_warp_timer);
271 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
272 deadline = qemu_clock_deadline(vm_clock);
275 * Ensure the vm_clock proceeds even when the virtual CPU goes to
276 * sleep. Otherwise, the CPU might be waiting for a future timer
277 * interrupt to wake it up, but the interrupt never comes because
278 * the vCPU isn't running any insns and thus doesn't advance the
281 * An extreme solution for this problem would be to never let VCPUs
282 * sleep in icount mode if there is a pending vm_clock timer; rather
283 * time could just advance to the next vm_clock event. Instead, we
284 * do stop VCPUs and only advance vm_clock after some "real" time,
285 * (related to the time left until the next event) has passed. This
286 * rt_clock timer will do this. This avoids that the warps are too
287 * visible externally---for example, you will not be sending network
288 * packets continously instead of every 100ms.
290 qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);
296 static const VMStateDescription vmstate_timers = {
299 .minimum_version_id = 1,
300 .minimum_version_id_old = 1,
301 .fields = (VMStateField[]) {
302 VMSTATE_INT64(cpu_ticks_offset, TimersState),
303 VMSTATE_INT64(dummy, TimersState),
304 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
305 VMSTATE_END_OF_LIST()
309 void configure_icount(const char *option)
311 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
316 icount_warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
317 if (strcmp(option, "auto") != 0) {
318 icount_time_shift = strtol(option, NULL, 0);
325 /* 125MIPS seems a reasonable initial guess at the guest speed.
326 It will be corrected fairly quickly anyway. */
327 icount_time_shift = 3;
329 /* Have both realtime and virtual time triggers for speed adjustment.
330 The realtime trigger catches emulated time passing too slowly,
331 the virtual time trigger catches emulated time passing too fast.
332 Realtime triggers occur even when idle, so use them less frequently
334 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
335 qemu_mod_timer(icount_rt_timer,
336 qemu_get_clock_ms(rt_clock) + 1000);
337 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
338 qemu_mod_timer(icount_vm_timer,
339 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
342 /***********************************************************/
343 void hw_error(const char *fmt, ...)
349 fprintf(stderr, "qemu: hardware error: ");
350 vfprintf(stderr, fmt, ap);
351 fprintf(stderr, "\n");
352 for(env = first_cpu; env != NULL; env = env->next_cpu) {
353 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
355 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
357 cpu_dump_state(env, stderr, fprintf, 0);
364 void cpu_synchronize_all_states(void)
368 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
369 cpu_synchronize_state(cpu);
373 void cpu_synchronize_all_post_reset(void)
377 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
378 cpu_synchronize_post_reset(cpu);
382 void cpu_synchronize_all_post_init(void)
386 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
387 cpu_synchronize_post_init(cpu);
391 int cpu_is_stopped(CPUState *env)
393 return !runstate_is_running() || env->stopped;
396 static void do_vm_stop(RunState state)
398 if (runstate_is_running()) {
402 vm_state_notify(0, state);
405 monitor_protocol_event(QEVENT_STOP, NULL);
409 static int cpu_can_run(CPUState *env)
414 if (env->stopped || !runstate_is_running()) {
420 static bool cpu_thread_is_idle(CPUState *env)
422 if (env->stop || env->queued_work_first) {
425 if (env->stopped || !runstate_is_running()) {
428 if (!env->halted || qemu_cpu_has_work(env) ||
429 (kvm_enabled() && kvm_irqchip_in_kernel())) {
435 bool all_cpu_threads_idle(void)
439 for (env = first_cpu; env != NULL; env = env->next_cpu) {
440 if (!cpu_thread_is_idle(env)) {
447 static void cpu_handle_guest_debug(CPUState *env)
449 gdb_set_stop_cpu(env);
450 qemu_system_debug_request();
454 static void cpu_signal(int sig)
456 if (cpu_single_env) {
457 cpu_exit(cpu_single_env);
463 static void sigbus_reraise(void)
466 struct sigaction action;
468 memset(&action, 0, sizeof(action));
469 action.sa_handler = SIG_DFL;
470 if (!sigaction(SIGBUS, &action, NULL)) {
473 sigaddset(&set, SIGBUS);
474 sigprocmask(SIG_UNBLOCK, &set, NULL);
476 perror("Failed to re-raise SIGBUS!\n");
480 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
483 if (kvm_on_sigbus(siginfo->ssi_code,
484 (void *)(intptr_t)siginfo->ssi_addr)) {
489 static void qemu_init_sigbus(void)
491 struct sigaction action;
493 memset(&action, 0, sizeof(action));
494 action.sa_flags = SA_SIGINFO;
495 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
496 sigaction(SIGBUS, &action, NULL);
498 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
501 static void qemu_kvm_eat_signals(CPUState *env)
503 struct timespec ts = { 0, 0 };
509 sigemptyset(&waitset);
510 sigaddset(&waitset, SIG_IPI);
511 sigaddset(&waitset, SIGBUS);
514 r = sigtimedwait(&waitset, &siginfo, &ts);
515 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
516 perror("sigtimedwait");
522 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
530 r = sigpending(&chkset);
532 perror("sigpending");
535 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
538 #else /* !CONFIG_LINUX */
540 static void qemu_init_sigbus(void)
544 static void qemu_kvm_eat_signals(CPUState *env)
547 #endif /* !CONFIG_LINUX */
550 static int io_thread_fd = -1;
552 static void qemu_event_increment(void)
554 /* Write 8 bytes to be compatible with eventfd. */
555 static const uint64_t val = 1;
558 if (io_thread_fd == -1) {
562 ret = write(io_thread_fd, &val, sizeof(val));
563 } while (ret < 0 && errno == EINTR);
565 /* EAGAIN is fine, a read must be pending. */
566 if (ret < 0 && errno != EAGAIN) {
567 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
573 static void qemu_event_read(void *opaque)
575 int fd = (intptr_t)opaque;
579 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
581 len = read(fd, buffer, sizeof(buffer));
582 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
585 static int qemu_event_init(void)
590 err = qemu_eventfd(fds);
594 err = fcntl_setfl(fds[0], O_NONBLOCK);
598 err = fcntl_setfl(fds[1], O_NONBLOCK);
602 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
603 (void *)(intptr_t)fds[0]);
605 io_thread_fd = fds[1];
614 static void dummy_signal(int sig)
618 /* If we have signalfd, we mask out the signals we want to handle and then
619 * use signalfd to listen for them. We rely on whatever the current signal
620 * handler is to dispatch the signals when we receive them.
622 static void sigfd_handler(void *opaque)
624 int fd = (intptr_t)opaque;
625 struct qemu_signalfd_siginfo info;
626 struct sigaction action;
631 len = read(fd, &info, sizeof(info));
632 } while (len == -1 && errno == EINTR);
634 if (len == -1 && errno == EAGAIN) {
638 if (len != sizeof(info)) {
639 printf("read from sigfd returned %zd: %m\n", len);
643 sigaction(info.ssi_signo, NULL, &action);
644 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
645 action.sa_sigaction(info.ssi_signo,
646 (siginfo_t *)&info, NULL);
647 } else if (action.sa_handler) {
648 action.sa_handler(info.ssi_signo);
653 static int qemu_signal_init(void)
659 * SIG_IPI must be blocked in the main thread and must not be caught
660 * by sigwait() in the signal thread. Otherwise, the cpu thread will
661 * not catch it reliably.
664 sigaddset(&set, SIG_IPI);
665 pthread_sigmask(SIG_BLOCK, &set, NULL);
668 sigaddset(&set, SIGIO);
669 sigaddset(&set, SIGALRM);
670 sigaddset(&set, SIGBUS);
671 pthread_sigmask(SIG_BLOCK, &set, NULL);
673 sigfd = qemu_signalfd(&set);
675 fprintf(stderr, "failed to create signalfd\n");
679 fcntl_setfl(sigfd, O_NONBLOCK);
681 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
682 (void *)(intptr_t)sigfd);
687 static void qemu_kvm_init_cpu_signals(CPUState *env)
691 struct sigaction sigact;
693 memset(&sigact, 0, sizeof(sigact));
694 sigact.sa_handler = dummy_signal;
695 sigaction(SIG_IPI, &sigact, NULL);
697 pthread_sigmask(SIG_BLOCK, NULL, &set);
698 sigdelset(&set, SIG_IPI);
699 sigdelset(&set, SIGBUS);
700 r = kvm_set_signal_mask(env, &set);
702 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
706 sigdelset(&set, SIG_IPI);
707 sigdelset(&set, SIGBUS);
708 r = kvm_set_signal_mask(env, &set);
710 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
715 static void qemu_tcg_init_cpu_signals(void)
718 struct sigaction sigact;
720 memset(&sigact, 0, sizeof(sigact));
721 sigact.sa_handler = cpu_signal;
722 sigaction(SIG_IPI, &sigact, NULL);
725 sigaddset(&set, SIG_IPI);
726 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
731 HANDLE qemu_event_handle;
733 static void dummy_event_handler(void *opaque)
737 static int qemu_event_init(void)
739 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
740 if (!qemu_event_handle) {
741 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
744 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
748 static void qemu_event_increment(void)
750 if (!SetEvent(qemu_event_handle)) {
751 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
757 static int qemu_signal_init(void)
762 static void qemu_kvm_init_cpu_signals(CPUState *env)
767 static void qemu_tcg_init_cpu_signals(void)
772 QemuMutex qemu_global_mutex;
773 static QemuCond qemu_io_proceeded_cond;
774 static bool iothread_requesting_mutex;
776 static QemuThread io_thread;
778 static QemuThread *tcg_cpu_thread;
779 static QemuCond *tcg_halt_cond;
782 static QemuCond qemu_cpu_cond;
784 static QemuCond qemu_pause_cond;
785 static QemuCond qemu_work_cond;
787 int qemu_init_main_loop(void)
793 ret = qemu_signal_init();
798 /* Note eventfd must be drained before signalfd handlers run */
799 ret = qemu_event_init();
804 qemu_cond_init(&qemu_cpu_cond);
805 qemu_cond_init(&qemu_pause_cond);
806 qemu_cond_init(&qemu_work_cond);
807 qemu_cond_init(&qemu_io_proceeded_cond);
808 qemu_mutex_init(&qemu_global_mutex);
809 qemu_mutex_lock(&qemu_global_mutex);
811 qemu_thread_get_self(&io_thread);
816 void qemu_main_loop_start(void)
821 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
823 struct qemu_work_item wi;
825 if (qemu_cpu_is_self(env)) {
832 if (!env->queued_work_first) {
833 env->queued_work_first = &wi;
835 env->queued_work_last->next = &wi;
837 env->queued_work_last = &wi;
843 CPUState *self_env = cpu_single_env;
845 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
846 cpu_single_env = self_env;
850 static void flush_queued_work(CPUState *env)
852 struct qemu_work_item *wi;
854 if (!env->queued_work_first) {
858 while ((wi = env->queued_work_first)) {
859 env->queued_work_first = wi->next;
863 env->queued_work_last = NULL;
864 qemu_cond_broadcast(&qemu_work_cond);
867 static void qemu_wait_io_event_common(CPUState *env)
872 qemu_cond_signal(&qemu_pause_cond);
874 flush_queued_work(env);
875 env->thread_kicked = false;
878 static void qemu_tcg_wait_io_event(void)
882 while (all_cpu_threads_idle()) {
883 /* Start accounting real time to the virtual clock if the CPUs
885 qemu_clock_warp(vm_clock);
886 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
889 while (iothread_requesting_mutex) {
890 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
893 for (env = first_cpu; env != NULL; env = env->next_cpu) {
894 qemu_wait_io_event_common(env);
898 static void qemu_kvm_wait_io_event(CPUState *env)
900 while (cpu_thread_is_idle(env)) {
901 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
904 qemu_kvm_eat_signals(env);
905 qemu_wait_io_event_common(env);
908 static void *qemu_kvm_cpu_thread_fn(void *arg)
913 qemu_mutex_lock(&qemu_global_mutex);
914 qemu_thread_get_self(env->thread);
915 env->thread_id = qemu_get_thread_id();
917 r = kvm_init_vcpu(env);
919 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
923 qemu_kvm_init_cpu_signals(env);
925 /* signal CPU creation */
927 qemu_cond_signal(&qemu_cpu_cond);
930 if (cpu_can_run(env)) {
931 r = kvm_cpu_exec(env);
932 if (r == EXCP_DEBUG) {
933 cpu_handle_guest_debug(env);
936 qemu_kvm_wait_io_event(env);
942 static void *qemu_tcg_cpu_thread_fn(void *arg)
946 qemu_tcg_init_cpu_signals();
947 qemu_thread_get_self(env->thread);
949 /* signal CPU creation */
950 qemu_mutex_lock(&qemu_global_mutex);
951 for (env = first_cpu; env != NULL; env = env->next_cpu) {
952 env->thread_id = qemu_get_thread_id();
955 qemu_cond_signal(&qemu_cpu_cond);
957 /* wait for initial kick-off after machine start */
958 while (first_cpu->stopped) {
959 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
964 if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
967 qemu_tcg_wait_io_event();
973 static void qemu_cpu_kick_thread(CPUState *env)
978 err = pthread_kill(env->thread->thread, SIG_IPI);
980 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
984 if (!qemu_cpu_is_self(env)) {
985 SuspendThread(env->thread->thread);
987 ResumeThread(env->thread->thread);
992 void qemu_cpu_kick(void *_env)
994 CPUState *env = _env;
996 qemu_cond_broadcast(env->halt_cond);
997 if (kvm_enabled() && !env->thread_kicked) {
998 qemu_cpu_kick_thread(env);
999 env->thread_kicked = true;
1003 void qemu_cpu_kick_self(void)
1006 assert(cpu_single_env);
1008 if (!cpu_single_env->thread_kicked) {
1009 qemu_cpu_kick_thread(cpu_single_env);
1010 cpu_single_env->thread_kicked = true;
1017 int qemu_cpu_is_self(void *_env)
1019 CPUState *env = _env;
1021 return qemu_thread_is_self(env->thread);
1024 void qemu_mutex_lock_iothread(void)
1026 if (kvm_enabled()) {
1027 qemu_mutex_lock(&qemu_global_mutex);
1029 iothread_requesting_mutex = true;
1030 if (qemu_mutex_trylock(&qemu_global_mutex)) {
1031 qemu_cpu_kick_thread(first_cpu);
1032 qemu_mutex_lock(&qemu_global_mutex);
1034 iothread_requesting_mutex = false;
1035 qemu_cond_broadcast(&qemu_io_proceeded_cond);
1039 void qemu_mutex_unlock_iothread(void)
1041 qemu_mutex_unlock(&qemu_global_mutex);
1044 static int all_vcpus_paused(void)
1046 CPUState *penv = first_cpu;
1049 if (!penv->stopped) {
1052 penv = (CPUState *)penv->next_cpu;
1058 void pause_all_vcpus(void)
1060 CPUState *penv = first_cpu;
1062 qemu_clock_enable(vm_clock, false);
1065 qemu_cpu_kick(penv);
1066 penv = (CPUState *)penv->next_cpu;
1069 while (!all_vcpus_paused()) {
1070 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
1073 qemu_cpu_kick(penv);
1074 penv = (CPUState *)penv->next_cpu;
1079 void resume_all_vcpus(void)
1081 CPUState *penv = first_cpu;
1086 qemu_cpu_kick(penv);
1087 penv = (CPUState *)penv->next_cpu;
1091 static void qemu_tcg_init_vcpu(void *_env)
1093 CPUState *env = _env;
1095 /* share a single thread for all cpus with TCG */
1096 if (!tcg_cpu_thread) {
1097 env->thread = g_malloc0(sizeof(QemuThread));
1098 env->halt_cond = g_malloc0(sizeof(QemuCond));
1099 qemu_cond_init(env->halt_cond);
1100 tcg_halt_cond = env->halt_cond;
1101 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
1102 while (env->created == 0) {
1103 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1105 tcg_cpu_thread = env->thread;
1107 env->thread = tcg_cpu_thread;
1108 env->halt_cond = tcg_halt_cond;
1112 static void qemu_kvm_start_vcpu(CPUState *env)
1114 env->thread = g_malloc0(sizeof(QemuThread));
1115 env->halt_cond = g_malloc0(sizeof(QemuCond));
1116 qemu_cond_init(env->halt_cond);
1117 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
1118 while (env->created == 0) {
1119 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1123 void qemu_init_vcpu(void *_env)
1125 CPUState *env = _env;
1127 env->nr_cores = smp_cores;
1128 env->nr_threads = smp_threads;
1130 if (kvm_enabled()) {
1131 qemu_kvm_start_vcpu(env);
1133 qemu_tcg_init_vcpu(env);
1137 void qemu_notify_event(void)
1139 qemu_event_increment();
1142 void cpu_stop_current(void)
1144 if (cpu_single_env) {
1145 cpu_single_env->stop = 0;
1146 cpu_single_env->stopped = 1;
1147 cpu_exit(cpu_single_env);
1148 qemu_cond_signal(&qemu_pause_cond);
1152 void vm_stop(RunState state)
1154 if (!qemu_thread_is_self(&io_thread)) {
1155 qemu_system_vmstop_request(state);
1157 * FIXME: should not return to device code in case
1158 * vm_stop() has been requested.
1166 /* does a state transition even if the VM is already stopped,
1167 current state is forgotten forever */
1168 void vm_stop_force_state(RunState state)
1170 if (runstate_is_running()) {
1173 runstate_set(state);
1177 static int tcg_cpu_exec(CPUState *env)
1180 #ifdef CONFIG_PROFILER
1184 #ifdef CONFIG_PROFILER
1185 ti = profile_getclock();
1190 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1191 env->icount_decr.u16.low = 0;
1192 env->icount_extra = 0;
1193 count = qemu_icount_round(qemu_clock_deadline(vm_clock));
1194 qemu_icount += count;
1195 decr = (count > 0xffff) ? 0xffff : count;
1197 env->icount_decr.u16.low = decr;
1198 env->icount_extra = count;
1200 ret = cpu_exec(env);
1201 #ifdef CONFIG_PROFILER
1202 qemu_time += profile_getclock() - ti;
1205 /* Fold pending instructions back into the
1206 instruction counter, and clear the interrupt flag. */
1207 qemu_icount -= (env->icount_decr.u16.low
1208 + env->icount_extra);
1209 env->icount_decr.u32 = 0;
1210 env->icount_extra = 0;
1215 bool cpu_exec_all(void)
1219 /* Account partial waits to the vm_clock. */
1220 qemu_clock_warp(vm_clock);
1222 if (next_cpu == NULL) {
1223 next_cpu = first_cpu;
1225 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1226 CPUState *env = next_cpu;
1228 qemu_clock_enable(vm_clock,
1229 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1231 if (cpu_can_run(env)) {
1232 if (kvm_enabled()) {
1233 r = kvm_cpu_exec(env);
1234 qemu_kvm_eat_signals(env);
1236 r = tcg_cpu_exec(env);
1238 if (r == EXCP_DEBUG) {
1239 cpu_handle_guest_debug(env);
1242 } else if (env->stop || env->stopped) {
1247 return !all_cpu_threads_idle();
1250 void set_numa_modes(void)
1255 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1256 for (i = 0; i < nb_numa_nodes; i++) {
1257 if (node_cpumask[i] & (1 << env->cpu_index)) {
1264 void set_cpu_log(const char *optarg)
1267 const CPULogItem *item;
1269 mask = cpu_str_to_log_mask(optarg);
1271 printf("Log items (comma separated):\n");
1272 for (item = cpu_log_items; item->mask != 0; item++) {
1273 printf("%-10s %s\n", item->name, item->help);
1280 void set_cpu_log_filename(const char *optarg)
1282 cpu_set_log_filename(optarg);
1285 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1287 /* XXX: implement xxx_cpu_list for targets that still miss it */
1288 #if defined(cpu_list_id)
1289 cpu_list_id(f, cpu_fprintf, optarg);
1290 #elif defined(cpu_list)
1291 cpu_list(f, cpu_fprintf); /* deprecated */