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"
36 #include "main-loop.h"
44 #include <sys/prctl.h>
47 #define PR_MCE_KILL 33
50 #ifndef PR_MCE_KILL_SET
51 #define PR_MCE_KILL_SET 1
54 #ifndef PR_MCE_KILL_EARLY
55 #define PR_MCE_KILL_EARLY 1
58 #endif /* CONFIG_LINUX */
60 static CPUState
*next_cpu
;
62 /***********************************************************/
63 /* guest cycle counter */
65 /* Conversion factor from emulated instructions to virtual clock ticks. */
66 static int icount_time_shift
;
67 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
68 #define MAX_ICOUNT_SHIFT 10
69 /* Compensate for varying guest execution speed. */
70 static int64_t qemu_icount_bias
;
71 static QEMUTimer
*icount_rt_timer
;
72 static QEMUTimer
*icount_vm_timer
;
73 static QEMUTimer
*icount_warp_timer
;
74 static int64_t vm_clock_warp_start
;
75 static int64_t qemu_icount
;
77 typedef struct TimersState
{
78 int64_t cpu_ticks_prev
;
79 int64_t cpu_ticks_offset
;
80 int64_t cpu_clock_offset
;
81 int32_t cpu_ticks_enabled
;
85 TimersState timers_state
;
87 /* Return the virtual CPU time, based on the instruction counter. */
88 int64_t cpu_get_icount(void)
91 CPUState
*env
= cpu_single_env
;;
95 if (!can_do_io(env
)) {
96 fprintf(stderr
, "Bad clock read\n");
98 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
100 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
103 /* return the host CPU cycle counter and handle stop/restart */
104 int64_t cpu_get_ticks(void)
107 return cpu_get_icount();
109 if (!timers_state
.cpu_ticks_enabled
) {
110 return timers_state
.cpu_ticks_offset
;
113 ticks
= cpu_get_real_ticks();
114 if (timers_state
.cpu_ticks_prev
> ticks
) {
115 /* Note: non increasing ticks may happen if the host uses
117 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
119 timers_state
.cpu_ticks_prev
= ticks
;
120 return ticks
+ timers_state
.cpu_ticks_offset
;
124 /* return the host CPU monotonic timer and handle stop/restart */
125 int64_t cpu_get_clock(void)
128 if (!timers_state
.cpu_ticks_enabled
) {
129 return timers_state
.cpu_clock_offset
;
132 return ti
+ timers_state
.cpu_clock_offset
;
136 /* enable cpu_get_ticks() */
137 void cpu_enable_ticks(void)
139 if (!timers_state
.cpu_ticks_enabled
) {
140 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
141 timers_state
.cpu_clock_offset
-= get_clock();
142 timers_state
.cpu_ticks_enabled
= 1;
146 /* disable cpu_get_ticks() : the clock is stopped. You must not call
147 cpu_get_ticks() after that. */
148 void cpu_disable_ticks(void)
150 if (timers_state
.cpu_ticks_enabled
) {
151 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
152 timers_state
.cpu_clock_offset
= cpu_get_clock();
153 timers_state
.cpu_ticks_enabled
= 0;
157 /* Correlation between real and virtual time is always going to be
158 fairly approximate, so ignore small variation.
159 When the guest is idle real and virtual time will be aligned in
161 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
163 static void icount_adjust(void)
168 static int64_t last_delta
;
169 /* If the VM is not running, then do nothing. */
170 if (!runstate_is_running()) {
173 cur_time
= cpu_get_clock();
174 cur_icount
= qemu_get_clock_ns(vm_clock
);
175 delta
= cur_icount
- cur_time
;
176 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
178 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
179 && icount_time_shift
> 0) {
180 /* The guest is getting too far ahead. Slow time down. */
184 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
185 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
186 /* The guest is getting too far behind. Speed time up. */
190 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
193 static void icount_adjust_rt(void *opaque
)
195 qemu_mod_timer(icount_rt_timer
,
196 qemu_get_clock_ms(rt_clock
) + 1000);
200 static void icount_adjust_vm(void *opaque
)
202 qemu_mod_timer(icount_vm_timer
,
203 qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 10);
207 static int64_t qemu_icount_round(int64_t count
)
209 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
212 static void icount_warp_rt(void *opaque
)
214 if (vm_clock_warp_start
== -1) {
218 if (runstate_is_running()) {
219 int64_t clock
= qemu_get_clock_ns(rt_clock
);
220 int64_t warp_delta
= clock
- vm_clock_warp_start
;
221 if (use_icount
== 1) {
222 qemu_icount_bias
+= warp_delta
;
225 * In adaptive mode, do not let the vm_clock run too
226 * far ahead of real time.
228 int64_t cur_time
= cpu_get_clock();
229 int64_t cur_icount
= qemu_get_clock_ns(vm_clock
);
230 int64_t delta
= cur_time
- cur_icount
;
231 qemu_icount_bias
+= MIN(warp_delta
, delta
);
233 if (qemu_clock_expired(vm_clock
)) {
237 vm_clock_warp_start
= -1;
240 void qemu_clock_warp(QEMUClock
*clock
)
245 * There are too many global variables to make the "warp" behavior
246 * applicable to other clocks. But a clock argument removes the
247 * need for if statements all over the place.
249 if (clock
!= vm_clock
|| !use_icount
) {
254 * If the CPUs have been sleeping, advance the vm_clock timer now. This
255 * ensures that the deadline for the timer is computed correctly below.
256 * This also makes sure that the insn counter is synchronized before the
257 * CPU starts running, in case the CPU is woken by an event other than
258 * the earliest vm_clock timer.
260 icount_warp_rt(NULL
);
261 if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock
)) {
262 qemu_del_timer(icount_warp_timer
);
266 vm_clock_warp_start
= qemu_get_clock_ns(rt_clock
);
267 deadline
= qemu_clock_deadline(vm_clock
);
270 * Ensure the vm_clock proceeds even when the virtual CPU goes to
271 * sleep. Otherwise, the CPU might be waiting for a future timer
272 * interrupt to wake it up, but the interrupt never comes because
273 * the vCPU isn't running any insns and thus doesn't advance the
276 * An extreme solution for this problem would be to never let VCPUs
277 * sleep in icount mode if there is a pending vm_clock timer; rather
278 * time could just advance to the next vm_clock event. Instead, we
279 * do stop VCPUs and only advance vm_clock after some "real" time,
280 * (related to the time left until the next event) has passed. This
281 * rt_clock timer will do this. This avoids that the warps are too
282 * visible externally---for example, you will not be sending network
283 * packets continously instead of every 100ms.
285 qemu_mod_timer(icount_warp_timer
, vm_clock_warp_start
+ deadline
);
291 static const VMStateDescription vmstate_timers
= {
294 .minimum_version_id
= 1,
295 .minimum_version_id_old
= 1,
296 .fields
= (VMStateField
[]) {
297 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
298 VMSTATE_INT64(dummy
, TimersState
),
299 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
300 VMSTATE_END_OF_LIST()
304 void configure_icount(const char *option
)
306 vmstate_register(NULL
, 0, &vmstate_timers
, &timers_state
);
311 icount_warp_timer
= qemu_new_timer_ns(rt_clock
, icount_warp_rt
, NULL
);
312 if (strcmp(option
, "auto") != 0) {
313 icount_time_shift
= strtol(option
, NULL
, 0);
320 /* 125MIPS seems a reasonable initial guess at the guest speed.
321 It will be corrected fairly quickly anyway. */
322 icount_time_shift
= 3;
324 /* Have both realtime and virtual time triggers for speed adjustment.
325 The realtime trigger catches emulated time passing too slowly,
326 the virtual time trigger catches emulated time passing too fast.
327 Realtime triggers occur even when idle, so use them less frequently
329 icount_rt_timer
= qemu_new_timer_ms(rt_clock
, icount_adjust_rt
, NULL
);
330 qemu_mod_timer(icount_rt_timer
,
331 qemu_get_clock_ms(rt_clock
) + 1000);
332 icount_vm_timer
= qemu_new_timer_ns(vm_clock
, icount_adjust_vm
, NULL
);
333 qemu_mod_timer(icount_vm_timer
,
334 qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 10);
337 /***********************************************************/
338 void hw_error(const char *fmt
, ...)
344 fprintf(stderr
, "qemu: hardware error: ");
345 vfprintf(stderr
, fmt
, ap
);
346 fprintf(stderr
, "\n");
347 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
348 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
350 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
352 cpu_dump_state(env
, stderr
, fprintf
, 0);
359 void cpu_synchronize_all_states(void)
363 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
364 cpu_synchronize_state(cpu
);
368 void cpu_synchronize_all_post_reset(void)
372 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
373 cpu_synchronize_post_reset(cpu
);
377 void cpu_synchronize_all_post_init(void)
381 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
382 cpu_synchronize_post_init(cpu
);
386 int cpu_is_stopped(CPUState
*env
)
388 return !runstate_is_running() || env
->stopped
;
391 static void do_vm_stop(RunState state
)
393 if (runstate_is_running()) {
397 vm_state_notify(0, state
);
400 monitor_protocol_event(QEVENT_STOP
, NULL
);
404 static int cpu_can_run(CPUState
*env
)
409 if (env
->stopped
|| !runstate_is_running()) {
415 static bool cpu_thread_is_idle(CPUState
*env
)
417 if (env
->stop
|| env
->queued_work_first
) {
420 if (env
->stopped
|| !runstate_is_running()) {
423 if (!env
->halted
|| qemu_cpu_has_work(env
) ||
424 (kvm_enabled() && kvm_irqchip_in_kernel())) {
430 bool all_cpu_threads_idle(void)
434 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
435 if (!cpu_thread_is_idle(env
)) {
442 static void cpu_handle_guest_debug(CPUState
*env
)
444 gdb_set_stop_cpu(env
);
445 qemu_system_debug_request();
449 static void cpu_signal(int sig
)
451 if (cpu_single_env
) {
452 cpu_exit(cpu_single_env
);
458 static void sigbus_reraise(void)
461 struct sigaction action
;
463 memset(&action
, 0, sizeof(action
));
464 action
.sa_handler
= SIG_DFL
;
465 if (!sigaction(SIGBUS
, &action
, NULL
)) {
468 sigaddset(&set
, SIGBUS
);
469 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
471 perror("Failed to re-raise SIGBUS!\n");
475 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
478 if (kvm_on_sigbus(siginfo
->ssi_code
,
479 (void *)(intptr_t)siginfo
->ssi_addr
)) {
484 static void qemu_init_sigbus(void)
486 struct sigaction action
;
488 memset(&action
, 0, sizeof(action
));
489 action
.sa_flags
= SA_SIGINFO
;
490 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
491 sigaction(SIGBUS
, &action
, NULL
);
493 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
496 static void qemu_kvm_eat_signals(CPUState
*env
)
498 struct timespec ts
= { 0, 0 };
504 sigemptyset(&waitset
);
505 sigaddset(&waitset
, SIG_IPI
);
506 sigaddset(&waitset
, SIGBUS
);
509 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
510 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
511 perror("sigtimedwait");
517 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
525 r
= sigpending(&chkset
);
527 perror("sigpending");
530 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
533 #else /* !CONFIG_LINUX */
535 static void qemu_init_sigbus(void)
539 static void qemu_kvm_eat_signals(CPUState
*env
)
542 #endif /* !CONFIG_LINUX */
545 static int io_thread_fd
= -1;
547 static void qemu_event_increment(void)
549 /* Write 8 bytes to be compatible with eventfd. */
550 static const uint64_t val
= 1;
553 if (io_thread_fd
== -1) {
557 ret
= write(io_thread_fd
, &val
, sizeof(val
));
558 } while (ret
< 0 && errno
== EINTR
);
560 /* EAGAIN is fine, a read must be pending. */
561 if (ret
< 0 && errno
!= EAGAIN
) {
562 fprintf(stderr
, "qemu_event_increment: write() failed: %s\n",
568 static void qemu_event_read(void *opaque
)
570 int fd
= (intptr_t)opaque
;
574 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
576 len
= read(fd
, buffer
, sizeof(buffer
));
577 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
580 static int qemu_event_init(void)
585 err
= qemu_eventfd(fds
);
589 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
593 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
597 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
598 (void *)(intptr_t)fds
[0]);
600 io_thread_fd
= fds
[1];
609 static void dummy_signal(int sig
)
613 /* If we have signalfd, we mask out the signals we want to handle and then
614 * use signalfd to listen for them. We rely on whatever the current signal
615 * handler is to dispatch the signals when we receive them.
617 static void sigfd_handler(void *opaque
)
619 int fd
= (intptr_t)opaque
;
620 struct qemu_signalfd_siginfo info
;
621 struct sigaction action
;
626 len
= read(fd
, &info
, sizeof(info
));
627 } while (len
== -1 && errno
== EINTR
);
629 if (len
== -1 && errno
== EAGAIN
) {
633 if (len
!= sizeof(info
)) {
634 printf("read from sigfd returned %zd: %m\n", len
);
638 sigaction(info
.ssi_signo
, NULL
, &action
);
639 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
640 action
.sa_sigaction(info
.ssi_signo
,
641 (siginfo_t
*)&info
, NULL
);
642 } else if (action
.sa_handler
) {
643 action
.sa_handler(info
.ssi_signo
);
648 static int qemu_signal_init(void)
654 * SIG_IPI must be blocked in the main thread and must not be caught
655 * by sigwait() in the signal thread. Otherwise, the cpu thread will
656 * not catch it reliably.
659 sigaddset(&set
, SIG_IPI
);
660 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
663 sigaddset(&set
, SIGIO
);
664 sigaddset(&set
, SIGALRM
);
665 sigaddset(&set
, SIGBUS
);
666 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
668 sigfd
= qemu_signalfd(&set
);
670 fprintf(stderr
, "failed to create signalfd\n");
674 fcntl_setfl(sigfd
, O_NONBLOCK
);
676 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
677 (void *)(intptr_t)sigfd
);
682 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
686 struct sigaction sigact
;
688 memset(&sigact
, 0, sizeof(sigact
));
689 sigact
.sa_handler
= dummy_signal
;
690 sigaction(SIG_IPI
, &sigact
, NULL
);
692 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
693 sigdelset(&set
, SIG_IPI
);
694 sigdelset(&set
, SIGBUS
);
695 r
= kvm_set_signal_mask(env
, &set
);
697 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
701 sigdelset(&set
, SIG_IPI
);
702 sigdelset(&set
, SIGBUS
);
703 r
= kvm_set_signal_mask(env
, &set
);
705 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
710 static void qemu_tcg_init_cpu_signals(void)
713 struct sigaction sigact
;
715 memset(&sigact
, 0, sizeof(sigact
));
716 sigact
.sa_handler
= cpu_signal
;
717 sigaction(SIG_IPI
, &sigact
, NULL
);
720 sigaddset(&set
, SIG_IPI
);
721 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
726 HANDLE qemu_event_handle
;
728 static void dummy_event_handler(void *opaque
)
732 static int qemu_event_init(void)
734 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
735 if (!qemu_event_handle
) {
736 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
739 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
743 static void qemu_event_increment(void)
745 if (!SetEvent(qemu_event_handle
)) {
746 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
752 static int qemu_signal_init(void)
757 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
762 static void qemu_tcg_init_cpu_signals(void)
767 QemuMutex qemu_global_mutex
;
768 static QemuCond qemu_io_proceeded_cond
;
769 static bool iothread_requesting_mutex
;
771 static QemuThread io_thread
;
773 static QemuThread
*tcg_cpu_thread
;
774 static QemuCond
*tcg_halt_cond
;
777 static QemuCond qemu_cpu_cond
;
779 static QemuCond qemu_pause_cond
;
780 static QemuCond qemu_work_cond
;
782 int qemu_init_main_loop(void)
788 ret
= qemu_signal_init();
793 /* Note eventfd must be drained before signalfd handlers run */
794 ret
= qemu_event_init();
799 qemu_cond_init(&qemu_cpu_cond
);
800 qemu_cond_init(&qemu_pause_cond
);
801 qemu_cond_init(&qemu_work_cond
);
802 qemu_cond_init(&qemu_io_proceeded_cond
);
803 qemu_mutex_init(&qemu_global_mutex
);
804 qemu_mutex_lock(&qemu_global_mutex
);
806 qemu_thread_get_self(&io_thread
);
811 void qemu_main_loop_start(void)
816 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
818 struct qemu_work_item wi
;
820 if (qemu_cpu_is_self(env
)) {
827 if (!env
->queued_work_first
) {
828 env
->queued_work_first
= &wi
;
830 env
->queued_work_last
->next
= &wi
;
832 env
->queued_work_last
= &wi
;
838 CPUState
*self_env
= cpu_single_env
;
840 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
841 cpu_single_env
= self_env
;
845 static void flush_queued_work(CPUState
*env
)
847 struct qemu_work_item
*wi
;
849 if (!env
->queued_work_first
) {
853 while ((wi
= env
->queued_work_first
)) {
854 env
->queued_work_first
= wi
->next
;
858 env
->queued_work_last
= NULL
;
859 qemu_cond_broadcast(&qemu_work_cond
);
862 static void qemu_wait_io_event_common(CPUState
*env
)
867 qemu_cond_signal(&qemu_pause_cond
);
869 flush_queued_work(env
);
870 env
->thread_kicked
= false;
873 static void qemu_tcg_wait_io_event(void)
877 while (all_cpu_threads_idle()) {
878 /* Start accounting real time to the virtual clock if the CPUs
880 qemu_clock_warp(vm_clock
);
881 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
884 while (iothread_requesting_mutex
) {
885 qemu_cond_wait(&qemu_io_proceeded_cond
, &qemu_global_mutex
);
888 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
889 qemu_wait_io_event_common(env
);
893 static void qemu_kvm_wait_io_event(CPUState
*env
)
895 while (cpu_thread_is_idle(env
)) {
896 qemu_cond_wait(env
->halt_cond
, &qemu_global_mutex
);
899 qemu_kvm_eat_signals(env
);
900 qemu_wait_io_event_common(env
);
903 static void *qemu_kvm_cpu_thread_fn(void *arg
)
908 qemu_mutex_lock(&qemu_global_mutex
);
909 qemu_thread_get_self(env
->thread
);
910 env
->thread_id
= qemu_get_thread_id();
912 r
= kvm_init_vcpu(env
);
914 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
918 qemu_kvm_init_cpu_signals(env
);
920 /* signal CPU creation */
922 qemu_cond_signal(&qemu_cpu_cond
);
925 if (cpu_can_run(env
)) {
926 r
= kvm_cpu_exec(env
);
927 if (r
== EXCP_DEBUG
) {
928 cpu_handle_guest_debug(env
);
931 qemu_kvm_wait_io_event(env
);
937 static void *qemu_tcg_cpu_thread_fn(void *arg
)
941 qemu_tcg_init_cpu_signals();
942 qemu_thread_get_self(env
->thread
);
944 /* signal CPU creation */
945 qemu_mutex_lock(&qemu_global_mutex
);
946 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
947 env
->thread_id
= qemu_get_thread_id();
950 qemu_cond_signal(&qemu_cpu_cond
);
952 /* wait for initial kick-off after machine start */
953 while (first_cpu
->stopped
) {
954 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
959 if (use_icount
&& qemu_clock_deadline(vm_clock
) <= 0) {
962 qemu_tcg_wait_io_event();
968 static void qemu_cpu_kick_thread(CPUState
*env
)
973 err
= pthread_kill(env
->thread
->thread
, SIG_IPI
);
975 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
979 if (!qemu_cpu_is_self(env
)) {
980 SuspendThread(env
->thread
->thread
);
982 ResumeThread(env
->thread
->thread
);
987 void qemu_cpu_kick(void *_env
)
989 CPUState
*env
= _env
;
991 qemu_cond_broadcast(env
->halt_cond
);
992 if (kvm_enabled() && !env
->thread_kicked
) {
993 qemu_cpu_kick_thread(env
);
994 env
->thread_kicked
= true;
998 void qemu_cpu_kick_self(void)
1001 assert(cpu_single_env
);
1003 if (!cpu_single_env
->thread_kicked
) {
1004 qemu_cpu_kick_thread(cpu_single_env
);
1005 cpu_single_env
->thread_kicked
= true;
1012 int qemu_cpu_is_self(void *_env
)
1014 CPUState
*env
= _env
;
1016 return qemu_thread_is_self(env
->thread
);
1019 void qemu_mutex_lock_iothread(void)
1021 if (kvm_enabled()) {
1022 qemu_mutex_lock(&qemu_global_mutex
);
1024 iothread_requesting_mutex
= true;
1025 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
1026 qemu_cpu_kick_thread(first_cpu
);
1027 qemu_mutex_lock(&qemu_global_mutex
);
1029 iothread_requesting_mutex
= false;
1030 qemu_cond_broadcast(&qemu_io_proceeded_cond
);
1034 void qemu_mutex_unlock_iothread(void)
1036 qemu_mutex_unlock(&qemu_global_mutex
);
1039 static int all_vcpus_paused(void)
1041 CPUState
*penv
= first_cpu
;
1044 if (!penv
->stopped
) {
1047 penv
= (CPUState
*)penv
->next_cpu
;
1053 void pause_all_vcpus(void)
1055 CPUState
*penv
= first_cpu
;
1057 qemu_clock_enable(vm_clock
, false);
1060 qemu_cpu_kick(penv
);
1061 penv
= (CPUState
*)penv
->next_cpu
;
1064 while (!all_vcpus_paused()) {
1065 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
1068 qemu_cpu_kick(penv
);
1069 penv
= (CPUState
*)penv
->next_cpu
;
1074 void resume_all_vcpus(void)
1076 CPUState
*penv
= first_cpu
;
1081 qemu_cpu_kick(penv
);
1082 penv
= (CPUState
*)penv
->next_cpu
;
1086 static void qemu_tcg_init_vcpu(void *_env
)
1088 CPUState
*env
= _env
;
1090 /* share a single thread for all cpus with TCG */
1091 if (!tcg_cpu_thread
) {
1092 env
->thread
= g_malloc0(sizeof(QemuThread
));
1093 env
->halt_cond
= g_malloc0(sizeof(QemuCond
));
1094 qemu_cond_init(env
->halt_cond
);
1095 tcg_halt_cond
= env
->halt_cond
;
1096 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
1097 while (env
->created
== 0) {
1098 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
1100 tcg_cpu_thread
= env
->thread
;
1102 env
->thread
= tcg_cpu_thread
;
1103 env
->halt_cond
= tcg_halt_cond
;
1107 static void qemu_kvm_start_vcpu(CPUState
*env
)
1109 env
->thread
= g_malloc0(sizeof(QemuThread
));
1110 env
->halt_cond
= g_malloc0(sizeof(QemuCond
));
1111 qemu_cond_init(env
->halt_cond
);
1112 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
1113 while (env
->created
== 0) {
1114 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
1118 void qemu_init_vcpu(void *_env
)
1120 CPUState
*env
= _env
;
1122 env
->nr_cores
= smp_cores
;
1123 env
->nr_threads
= smp_threads
;
1125 if (kvm_enabled()) {
1126 qemu_kvm_start_vcpu(env
);
1128 qemu_tcg_init_vcpu(env
);
1132 void qemu_notify_event(void)
1134 qemu_event_increment();
1137 void cpu_stop_current(void)
1139 if (cpu_single_env
) {
1140 cpu_single_env
->stop
= 0;
1141 cpu_single_env
->stopped
= 1;
1142 cpu_exit(cpu_single_env
);
1143 qemu_cond_signal(&qemu_pause_cond
);
1147 void vm_stop(RunState state
)
1149 if (!qemu_thread_is_self(&io_thread
)) {
1150 qemu_system_vmstop_request(state
);
1152 * FIXME: should not return to device code in case
1153 * vm_stop() has been requested.
1161 /* does a state transition even if the VM is already stopped,
1162 current state is forgotten forever */
1163 void vm_stop_force_state(RunState state
)
1165 if (runstate_is_running()) {
1168 runstate_set(state
);
1172 static int tcg_cpu_exec(CPUState
*env
)
1175 #ifdef CONFIG_PROFILER
1179 #ifdef CONFIG_PROFILER
1180 ti
= profile_getclock();
1185 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1186 env
->icount_decr
.u16
.low
= 0;
1187 env
->icount_extra
= 0;
1188 count
= qemu_icount_round(qemu_clock_deadline(vm_clock
));
1189 qemu_icount
+= count
;
1190 decr
= (count
> 0xffff) ? 0xffff : count
;
1192 env
->icount_decr
.u16
.low
= decr
;
1193 env
->icount_extra
= count
;
1195 ret
= cpu_exec(env
);
1196 #ifdef CONFIG_PROFILER
1197 qemu_time
+= profile_getclock() - ti
;
1200 /* Fold pending instructions back into the
1201 instruction counter, and clear the interrupt flag. */
1202 qemu_icount
-= (env
->icount_decr
.u16
.low
1203 + env
->icount_extra
);
1204 env
->icount_decr
.u32
= 0;
1205 env
->icount_extra
= 0;
1210 bool cpu_exec_all(void)
1214 /* Account partial waits to the vm_clock. */
1215 qemu_clock_warp(vm_clock
);
1217 if (next_cpu
== NULL
) {
1218 next_cpu
= first_cpu
;
1220 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1221 CPUState
*env
= next_cpu
;
1223 qemu_clock_enable(vm_clock
,
1224 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1226 if (cpu_can_run(env
)) {
1227 if (kvm_enabled()) {
1228 r
= kvm_cpu_exec(env
);
1229 qemu_kvm_eat_signals(env
);
1231 r
= tcg_cpu_exec(env
);
1233 if (r
== EXCP_DEBUG
) {
1234 cpu_handle_guest_debug(env
);
1237 } else if (env
->stop
|| env
->stopped
) {
1242 return !all_cpu_threads_idle();
1245 void set_numa_modes(void)
1250 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1251 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1252 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1259 void set_cpu_log(const char *optarg
)
1262 const CPULogItem
*item
;
1264 mask
= cpu_str_to_log_mask(optarg
);
1266 printf("Log items (comma separated):\n");
1267 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1268 printf("%-10s %s\n", item
->name
, item
->help
);
1275 void set_cpu_log_filename(const char *optarg
)
1277 cpu_set_log_filename(optarg
);
1280 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1282 /* XXX: implement xxx_cpu_list for targets that still miss it */
1283 #if defined(cpu_list_id)
1284 cpu_list_id(f
, cpu_fprintf
, optarg
);
1285 #elif defined(cpu_list)
1286 cpu_list(f
, cpu_fprintf
); /* deprecated */