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"
39 #define SIG_IPI (SIGRTMIN+4)
41 #define SIG_IPI SIGUSR1
46 #include <sys/prctl.h>
49 #define PR_MCE_KILL 33
52 #ifndef PR_MCE_KILL_SET
53 #define PR_MCE_KILL_SET 1
56 #ifndef PR_MCE_KILL_EARLY
57 #define PR_MCE_KILL_EARLY 1
60 #endif /* CONFIG_LINUX */
62 static CPUState
*next_cpu
;
64 /***********************************************************/
65 void hw_error(const char *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
);
77 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
79 cpu_dump_state(env
, stderr
, fprintf
, 0);
86 void cpu_synchronize_all_states(void)
90 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
91 cpu_synchronize_state(cpu
);
95 void cpu_synchronize_all_post_reset(void)
99 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
100 cpu_synchronize_post_reset(cpu
);
104 void cpu_synchronize_all_post_init(void)
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
)
124 vm_state_notify(0, reason
);
127 monitor_protocol_event(QEVENT_STOP
, NULL
);
131 static int cpu_can_run(CPUState
*env
)
136 if (env
->stopped
|| !vm_running
) {
142 static bool cpu_thread_is_idle(CPUState
*env
)
144 if (env
->stop
|| env
->queued_work_first
) {
147 if (env
->stopped
|| !vm_running
) {
150 if (!env
->halted
|| qemu_cpu_has_work(env
)) {
156 static bool all_cpu_threads_idle(void)
160 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
161 if (!cpu_thread_is_idle(env
)) {
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
;
178 static void cpu_handle_debug_exception(CPUState
*env
)
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
199 static void sigbus_reraise(void)
202 struct sigaction action
;
204 memset(&action
, 0, sizeof(action
));
205 action
.sa_handler
= SIG_DFL
;
206 if (!sigaction(SIGBUS
, &action
, NULL
)) {
209 sigaddset(&set
, SIGBUS
);
210 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
212 perror("Failed to re-raise SIGBUS!\n");
216 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
219 if (kvm_on_sigbus(siginfo
->ssi_code
,
220 (void *)(intptr_t)siginfo
->ssi_addr
)) {
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 */
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;
253 if (io_thread_fd
== -1) {
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",
268 static void qemu_event_read(void *opaque
)
270 int fd
= (unsigned long)opaque
;
274 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
276 len
= read(fd
, buffer
, sizeof(buffer
));
277 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
280 static int qemu_event_init(void)
285 err
= qemu_eventfd(fds
);
289 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
293 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
297 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
298 (void *)(unsigned long)fds
[0]);
300 io_thread_fd
= fds
[1];
309 #ifdef OBSOLETE_KVM_IMPL
310 static void dummy_signal(int sig
)
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
;
328 len
= read(fd
, &info
, sizeof(info
));
329 } while (len
== -1 && errno
== EINTR
);
331 if (len
== -1 && errno
== EAGAIN
) {
335 if (len
!= sizeof(info
)) {
336 printf("read from sigfd returned %zd: %m\n", len
);
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
)
354 sigfd
= qemu_signalfd(&mask
);
356 fprintf(stderr
, "failed to create signalfd\n");
360 fcntl_setfl(sigfd
, O_NONBLOCK
);
362 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
363 (void *)(unsigned long) sigfd
);
368 static void qemu_kvm_eat_signals(CPUState
*env
)
370 struct timespec ts
= { 0, 0 };
376 sigemptyset(&waitset
);
377 sigaddset(&waitset
, SIG_IPI
);
378 sigaddset(&waitset
, SIGBUS
);
381 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
382 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
383 perror("sigtimedwait");
389 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
397 r
= sigpending(&chkset
);
399 perror("sigpending");
402 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
404 #ifndef CONFIG_IOTHREAD
405 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
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());
426 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
430 static void qemu_event_increment(void)
432 if (!SetEvent(qemu_event_handle
)) {
433 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
439 static void qemu_kvm_eat_signals(CPUState
*env
)
444 #ifndef CONFIG_IOTHREAD
445 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
447 #if 0 /* Causes regressions: autotest WinXP.64 migrate.tcp */
451 struct sigaction sigact
;
453 memset(&sigact
, 0, sizeof(sigact
));
454 sigact
.sa_handler
= dummy_signal
;
455 sigaction(SIG_IPI
, &sigact
, NULL
);
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
);
470 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
478 static sigset_t
block_synchronous_signals(void)
483 sigaddset(&set
, SIGBUS
);
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
);
497 int qemu_init_main_loop(void)
500 sigset_t blocked_signals
;
503 blocked_signals
= block_synchronous_signals();
505 ret
= qemu_signalfd_init(blocked_signals
);
513 return qemu_event_init();
516 void qemu_main_loop_start(void)
520 void qemu_init_vcpu(void *_env
)
522 CPUState
*env
= _env
;
525 env
->nr_cores
= smp_cores
;
526 env
->nr_threads
= smp_threads
;
529 r
= kvm_init_vcpu(env
);
531 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
534 qemu_kvm_init_cpu_signals(env
);
538 int qemu_cpu_self(void *env
)
543 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *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)
563 assert(cpu_single_env
);
571 void qemu_notify_event(void)
573 CPUState
*env
= cpu_single_env
;
575 qemu_event_increment ();
579 if (next_cpu
&& env
!= next_cpu
) {
585 #if defined(OBSOLETE_KVM_IMPL) || !defined(CONFIG_KVM)
586 void qemu_mutex_lock_iothread(void) {}
587 void qemu_mutex_unlock_iothread(void) {}
590 void cpu_stop_current(void)
594 void vm_stop(int 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
;
613 static QemuCond qemu_cpu_cond
;
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
);
627 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
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
);
642 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
647 static void qemu_tcg_init_cpu_signals(void)
650 struct sigaction sigact
;
652 memset(&sigact
, 0, sizeof(sigact
));
653 sigact
.sa_handler
= cpu_signal
;
654 sigaction(SIG_IPI
, &sigact
, NULL
);
657 sigaddset(&set
, SIG_IPI
);
658 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
661 static sigset_t
block_io_signals(void)
665 /* SIGUSR2 used by posix-aio-compat.c */
667 sigaddset(&set
, SIGUSR2
);
668 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
671 sigaddset(&set
, SIGIO
);
672 sigaddset(&set
, SIGALRM
);
673 sigaddset(&set
, SIG_IPI
);
674 sigaddset(&set
, SIGBUS
);
675 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
680 int qemu_init_main_loop(void)
683 sigset_t blocked_signals
;
687 blocked_signals
= block_io_signals();
689 ret
= qemu_signalfd_init(blocked_signals
);
694 /* Note eventfd must be drained before signalfd handlers run */
695 ret
= qemu_event_init();
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
);
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
)) {
730 if (!env
->queued_work_first
) {
731 env
->queued_work_first
= &wi
;
733 env
->queued_work_last
->next
= &wi
;
735 env
->queued_work_last
= &wi
;
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
) {
756 while ((wi
= env
->queued_work_first
)) {
757 env
->queued_work_first
= wi
->next
;
761 env
->queued_work_last
= NULL
;
762 qemu_cond_broadcast(&qemu_work_cond
);
765 static void qemu_wait_io_event_common(CPUState
*env
)
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)
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
)
816 qemu_mutex_lock(&qemu_global_mutex
);
817 qemu_thread_self(env
->thread
);
819 r
= kvm_init_vcpu(env
);
821 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
825 qemu_kvm_init_cpu_signals(env
);
827 /* signal CPU creation */
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);
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
);
849 static void *qemu_tcg_cpu_thread_fn(void *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
) {
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);
870 qemu_tcg_wait_io_event();
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
;
902 qemu_thread_self(&this);
904 return qemu_thread_equal(&this, env
->thread
);
907 void qemu_mutex_lock_iothread(void)
910 qemu_mutex_lock(&qemu_global_mutex
);
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
;
931 if (!penv
->stopped
) {
934 penv
= (CPUState
*)penv
->next_cpu
;
940 void pause_all_vcpus(void)
942 CPUState
*penv
= first_cpu
;
947 penv
= (CPUState
*)penv
->next_cpu
;
950 while (!all_vcpus_paused()) {
951 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
955 penv
= (CPUState
*)penv
->next_cpu
;
960 void resume_all_vcpus(void)
962 CPUState
*penv
= first_cpu
;
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
;
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
);
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
)
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.
1049 static int tcg_cpu_exec(CPUState
*env
)
1052 #ifdef CONFIG_PROFILER
1056 #ifdef CONFIG_PROFILER
1057 ti
= profile_getclock();
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
;
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
;
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;
1087 bool cpu_exec_all(void)
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()) {
1103 if (cpu_can_run(env
)) {
1104 if (kvm_enabled()) {
1105 r
= kvm_cpu_exec(env
);
1106 qemu_kvm_eat_signals(env
);
1108 r
= tcg_cpu_exec(env
);
1110 if (r
== EXCP_DEBUG
) {
1111 cpu_handle_debug_exception(env
);
1114 } else if (env
->stop
) {
1119 return !all_cpu_threads_idle();
1122 void set_numa_modes(void)
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
)) {
1136 void set_cpu_log(const char *optarg
)
1139 const CPULogItem
*item
;
1141 mask
= cpu_str_to_log_mask(optarg
);
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
);
1152 /* Return the virtual CPU time, based on the instruction counter. */
1153 int64_t cpu_get_icount(void)
1156 CPUState
*env
= cpu_single_env
;;
1158 icount
= qemu_icount
;
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 */