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 static void dummy_signal(int sig
)
313 /* If we have signalfd, we mask out the signals we want to handle and then
314 * use signalfd to listen for them. We rely on whatever the current signal
315 * handler is to dispatch the signals when we receive them.
317 static void sigfd_handler(void *opaque
)
319 int fd
= (unsigned long) opaque
;
320 struct qemu_signalfd_siginfo info
;
321 struct sigaction action
;
326 len
= read(fd
, &info
, sizeof(info
));
327 } while (len
== -1 && errno
== EINTR
);
329 if (len
== -1 && errno
== EAGAIN
) {
333 if (len
!= sizeof(info
)) {
334 printf("read from sigfd returned %zd: %m\n", len
);
338 sigaction(info
.ssi_signo
, NULL
, &action
);
339 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
340 action
.sa_sigaction(info
.ssi_signo
,
341 (siginfo_t
*)&info
, NULL
);
342 } else if (action
.sa_handler
) {
343 action
.sa_handler(info
.ssi_signo
);
348 static int qemu_signalfd_init(sigset_t mask
)
352 sigfd
= qemu_signalfd(&mask
);
354 fprintf(stderr
, "failed to create signalfd\n");
358 fcntl_setfl(sigfd
, O_NONBLOCK
);
360 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
361 (void *)(unsigned long) sigfd
);
366 static void qemu_kvm_eat_signals(CPUState
*env
)
368 struct timespec ts
= { 0, 0 };
374 sigemptyset(&waitset
);
375 sigaddset(&waitset
, SIG_IPI
);
376 sigaddset(&waitset
, SIGBUS
);
379 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
380 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
381 perror("sigtimedwait");
387 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
395 r
= sigpending(&chkset
);
397 perror("sigpending");
400 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
402 #ifndef CONFIG_IOTHREAD
403 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
411 HANDLE qemu_event_handle
;
413 static void dummy_event_handler(void *opaque
)
417 static int qemu_event_init(void)
419 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
420 if (!qemu_event_handle
) {
421 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
424 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
428 static void qemu_event_increment(void)
430 if (!SetEvent(qemu_event_handle
)) {
431 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
437 static void qemu_kvm_eat_signals(CPUState
*env
)
442 #ifndef CONFIG_IOTHREAD
443 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
448 struct sigaction sigact
;
450 memset(&sigact
, 0, sizeof(sigact
));
451 sigact
.sa_handler
= dummy_signal
;
452 sigaction(SIG_IPI
, &sigact
, NULL
);
455 sigaddset(&set
, SIG_IPI
);
456 sigaddset(&set
, SIGIO
);
457 sigaddset(&set
, SIGALRM
);
458 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
460 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
461 sigdelset(&set
, SIG_IPI
);
462 sigdelset(&set
, SIGBUS
);
463 sigdelset(&set
, SIGIO
);
464 sigdelset(&set
, SIGALRM
);
465 r
= kvm_set_signal_mask(env
, &set
);
467 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
474 static sigset_t
block_synchronous_signals(void)
479 sigaddset(&set
, SIGBUS
);
482 * We need to process timer signals synchronously to avoid a race
483 * between exit_request check and KVM vcpu entry.
485 sigaddset(&set
, SIGIO
);
486 sigaddset(&set
, SIGALRM
);
493 int qemu_init_main_loop(void)
496 sigset_t blocked_signals
;
499 blocked_signals
= block_synchronous_signals();
501 ret
= qemu_signalfd_init(blocked_signals
);
509 return qemu_event_init();
512 void qemu_main_loop_start(void)
516 void qemu_init_vcpu(void *_env
)
518 CPUState
*env
= _env
;
521 env
->nr_cores
= smp_cores
;
522 env
->nr_threads
= smp_threads
;
525 r
= kvm_init_vcpu(env
);
527 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
530 qemu_kvm_init_cpu_signals(env
);
534 int qemu_cpu_self(void *env
)
539 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
544 void resume_all_vcpus(void)
548 void pause_all_vcpus(void)
552 void qemu_cpu_kick(void *env
)
556 void qemu_cpu_kick_self(void)
559 assert(cpu_single_env
);
567 void qemu_notify_event(void)
569 CPUState
*env
= cpu_single_env
;
571 qemu_event_increment ();
575 if (next_cpu
&& env
!= next_cpu
) {
581 void qemu_mutex_lock_iothread(void) {}
582 void qemu_mutex_unlock_iothread(void) {}
584 void cpu_stop_current(void)
588 void vm_stop(int reason
)
593 #else /* CONFIG_IOTHREAD */
595 #include "qemu-thread.h"
597 QemuMutex qemu_global_mutex
;
598 static QemuMutex qemu_fair_mutex
;
600 static QemuThread io_thread
;
602 static QemuThread
*tcg_cpu_thread
;
603 static QemuCond
*tcg_halt_cond
;
605 static int qemu_system_ready
;
607 static QemuCond qemu_cpu_cond
;
609 static QemuCond qemu_system_cond
;
610 static QemuCond qemu_pause_cond
;
611 static QemuCond qemu_work_cond
;
613 static void cpu_signal(int sig
)
615 if (cpu_single_env
) {
616 cpu_exit(cpu_single_env
);
621 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
625 struct sigaction sigact
;
627 memset(&sigact
, 0, sizeof(sigact
));
628 sigact
.sa_handler
= dummy_signal
;
629 sigaction(SIG_IPI
, &sigact
, NULL
);
631 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
632 sigdelset(&set
, SIG_IPI
);
633 sigdelset(&set
, SIGBUS
);
634 r
= kvm_set_signal_mask(env
, &set
);
636 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
641 static void qemu_tcg_init_cpu_signals(void)
644 struct sigaction sigact
;
646 memset(&sigact
, 0, sizeof(sigact
));
647 sigact
.sa_handler
= cpu_signal
;
648 sigaction(SIG_IPI
, &sigact
, NULL
);
651 sigaddset(&set
, SIG_IPI
);
652 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
655 static sigset_t
block_io_signals(void)
659 /* SIGUSR2 used by posix-aio-compat.c */
661 sigaddset(&set
, SIGUSR2
);
662 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
665 sigaddset(&set
, SIGIO
);
666 sigaddset(&set
, SIGALRM
);
667 sigaddset(&set
, SIG_IPI
);
668 sigaddset(&set
, SIGBUS
);
669 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
674 int qemu_init_main_loop(void)
677 sigset_t blocked_signals
;
681 blocked_signals
= block_io_signals();
683 ret
= qemu_signalfd_init(blocked_signals
);
688 /* Note eventfd must be drained before signalfd handlers run */
689 ret
= qemu_event_init();
694 qemu_cond_init(&qemu_cpu_cond
);
695 qemu_cond_init(&qemu_system_cond
);
696 qemu_cond_init(&qemu_pause_cond
);
697 qemu_cond_init(&qemu_work_cond
);
698 qemu_mutex_init(&qemu_fair_mutex
);
699 qemu_mutex_init(&qemu_global_mutex
);
700 qemu_mutex_lock(&qemu_global_mutex
);
702 qemu_thread_self(&io_thread
);
707 void qemu_main_loop_start(void)
709 qemu_system_ready
= 1;
710 qemu_cond_broadcast(&qemu_system_cond
);
713 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
715 struct qemu_work_item wi
;
717 if (qemu_cpu_self(env
)) {
724 if (!env
->queued_work_first
) {
725 env
->queued_work_first
= &wi
;
727 env
->queued_work_last
->next
= &wi
;
729 env
->queued_work_last
= &wi
;
735 CPUState
*self_env
= cpu_single_env
;
737 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
738 cpu_single_env
= self_env
;
742 static void flush_queued_work(CPUState
*env
)
744 struct qemu_work_item
*wi
;
746 if (!env
->queued_work_first
) {
750 while ((wi
= env
->queued_work_first
)) {
751 env
->queued_work_first
= wi
->next
;
755 env
->queued_work_last
= NULL
;
756 qemu_cond_broadcast(&qemu_work_cond
);
759 static void qemu_wait_io_event_common(CPUState
*env
)
764 qemu_cond_signal(&qemu_pause_cond
);
766 flush_queued_work(env
);
767 env
->thread_kicked
= false;
770 static void qemu_tcg_wait_io_event(void)
774 while (all_cpu_threads_idle()) {
775 qemu_cond_timedwait(tcg_halt_cond
, &qemu_global_mutex
, 1000);
778 qemu_mutex_unlock(&qemu_global_mutex
);
781 * Users of qemu_global_mutex can be starved, having no chance
782 * to acquire it since this path will get to it first.
783 * So use another lock to provide fairness.
785 qemu_mutex_lock(&qemu_fair_mutex
);
786 qemu_mutex_unlock(&qemu_fair_mutex
);
788 qemu_mutex_lock(&qemu_global_mutex
);
790 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
791 qemu_wait_io_event_common(env
);
795 static void qemu_kvm_wait_io_event(CPUState
*env
)
797 while (cpu_thread_is_idle(env
)) {
798 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
801 qemu_kvm_eat_signals(env
);
802 qemu_wait_io_event_common(env
);
805 static void *qemu_kvm_cpu_thread_fn(void *arg
)
810 qemu_mutex_lock(&qemu_global_mutex
);
811 qemu_thread_self(env
->thread
);
813 r
= kvm_init_vcpu(env
);
815 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
819 qemu_kvm_init_cpu_signals(env
);
821 /* signal CPU creation */
823 qemu_cond_signal(&qemu_cpu_cond
);
825 /* and wait for machine initialization */
826 while (!qemu_system_ready
) {
827 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
831 if (cpu_can_run(env
)) {
832 r
= kvm_cpu_exec(env
);
833 if (r
== EXCP_DEBUG
) {
834 cpu_handle_debug_exception(env
);
837 qemu_kvm_wait_io_event(env
);
843 static void *qemu_tcg_cpu_thread_fn(void *arg
)
847 qemu_tcg_init_cpu_signals();
848 qemu_thread_self(env
->thread
);
850 /* signal CPU creation */
851 qemu_mutex_lock(&qemu_global_mutex
);
852 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
855 qemu_cond_signal(&qemu_cpu_cond
);
857 /* and wait for machine initialization */
858 while (!qemu_system_ready
) {
859 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
864 qemu_tcg_wait_io_event();
870 void qemu_cpu_kick(void *_env
)
872 CPUState
*env
= _env
;
874 qemu_cond_broadcast(env
->halt_cond
);
875 if (!env
->thread_kicked
) {
876 qemu_thread_signal(env
->thread
, SIG_IPI
);
877 env
->thread_kicked
= true;
881 void qemu_cpu_kick_self(void)
883 assert(cpu_single_env
);
885 if (!cpu_single_env
->thread_kicked
) {
886 qemu_thread_signal(cpu_single_env
->thread
, SIG_IPI
);
887 cpu_single_env
->thread_kicked
= true;
891 int qemu_cpu_self(void *_env
)
893 CPUState
*env
= _env
;
896 qemu_thread_self(&this);
898 return qemu_thread_equal(&this, env
->thread
);
901 void qemu_mutex_lock_iothread(void)
904 qemu_mutex_lock(&qemu_global_mutex
);
906 qemu_mutex_lock(&qemu_fair_mutex
);
907 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
908 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
909 qemu_mutex_lock(&qemu_global_mutex
);
911 qemu_mutex_unlock(&qemu_fair_mutex
);
915 void qemu_mutex_unlock_iothread(void)
917 qemu_mutex_unlock(&qemu_global_mutex
);
920 static int all_vcpus_paused(void)
922 CPUState
*penv
= first_cpu
;
925 if (!penv
->stopped
) {
928 penv
= (CPUState
*)penv
->next_cpu
;
934 void pause_all_vcpus(void)
936 CPUState
*penv
= first_cpu
;
941 penv
= (CPUState
*)penv
->next_cpu
;
944 while (!all_vcpus_paused()) {
945 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
949 penv
= (CPUState
*)penv
->next_cpu
;
954 void resume_all_vcpus(void)
956 CPUState
*penv
= first_cpu
;
962 penv
= (CPUState
*)penv
->next_cpu
;
966 static void qemu_tcg_init_vcpu(void *_env
)
968 CPUState
*env
= _env
;
970 /* share a single thread for all cpus with TCG */
971 if (!tcg_cpu_thread
) {
972 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
973 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
974 qemu_cond_init(env
->halt_cond
);
975 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
976 while (env
->created
== 0) {
977 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
979 tcg_cpu_thread
= env
->thread
;
980 tcg_halt_cond
= env
->halt_cond
;
982 env
->thread
= tcg_cpu_thread
;
983 env
->halt_cond
= tcg_halt_cond
;
987 static void qemu_kvm_start_vcpu(CPUState
*env
)
989 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
990 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
991 qemu_cond_init(env
->halt_cond
);
992 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
993 while (env
->created
== 0) {
994 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
998 void qemu_init_vcpu(void *_env
)
1000 CPUState
*env
= _env
;
1002 env
->nr_cores
= smp_cores
;
1003 env
->nr_threads
= smp_threads
;
1004 if (kvm_enabled()) {
1005 qemu_kvm_start_vcpu(env
);
1007 qemu_tcg_init_vcpu(env
);
1011 void qemu_notify_event(void)
1013 qemu_event_increment();
1016 void cpu_stop_current(void)
1018 if (cpu_single_env
) {
1019 cpu_single_env
->stopped
= 1;
1020 cpu_exit(cpu_single_env
);
1024 void vm_stop(int reason
)
1027 qemu_thread_self(&me
);
1029 if (!qemu_thread_equal(&me
, &io_thread
)) {
1030 qemu_system_vmstop_request(reason
);
1032 * FIXME: should not return to device code in case
1033 * vm_stop() has been requested.
1043 static int tcg_cpu_exec(CPUState
*env
)
1046 #ifdef CONFIG_PROFILER
1050 #ifdef CONFIG_PROFILER
1051 ti
= profile_getclock();
1056 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1057 env
->icount_decr
.u16
.low
= 0;
1058 env
->icount_extra
= 0;
1059 count
= qemu_icount_round (qemu_next_deadline());
1060 qemu_icount
+= count
;
1061 decr
= (count
> 0xffff) ? 0xffff : count
;
1063 env
->icount_decr
.u16
.low
= decr
;
1064 env
->icount_extra
= count
;
1066 ret
= cpu_exec(env
);
1067 #ifdef CONFIG_PROFILER
1068 qemu_time
+= profile_getclock() - ti
;
1071 /* Fold pending instructions back into the
1072 instruction counter, and clear the interrupt flag. */
1073 qemu_icount
-= (env
->icount_decr
.u16
.low
1074 + env
->icount_extra
);
1075 env
->icount_decr
.u32
= 0;
1076 env
->icount_extra
= 0;
1081 bool cpu_exec_all(void)
1085 if (next_cpu
== NULL
) {
1086 next_cpu
= first_cpu
;
1088 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1089 CPUState
*env
= next_cpu
;
1091 qemu_clock_enable(vm_clock
,
1092 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1094 if (qemu_alarm_pending()) {
1097 if (cpu_can_run(env
)) {
1098 if (kvm_enabled()) {
1099 r
= kvm_cpu_exec(env
);
1100 qemu_kvm_eat_signals(env
);
1102 r
= tcg_cpu_exec(env
);
1104 if (r
== EXCP_DEBUG
) {
1105 cpu_handle_debug_exception(env
);
1108 } else if (env
->stop
) {
1113 return !all_cpu_threads_idle();
1116 void set_numa_modes(void)
1121 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1122 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1123 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1130 void set_cpu_log(const char *optarg
)
1133 const CPULogItem
*item
;
1135 mask
= cpu_str_to_log_mask(optarg
);
1137 printf("Log items (comma separated):\n");
1138 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1139 printf("%-10s %s\n", item
->name
, item
->help
);
1146 /* Return the virtual CPU time, based on the instruction counter. */
1147 int64_t cpu_get_icount(void)
1150 CPUState
*env
= cpu_single_env
;;
1152 icount
= qemu_icount
;
1154 if (!can_do_io(env
)) {
1155 fprintf(stderr
, "Bad clock read\n");
1157 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1159 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1162 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1164 /* XXX: implement xxx_cpu_list for targets that still miss it */
1165 #if defined(cpu_list_id)
1166 cpu_list_id(f
, cpu_fprintf
, optarg
);
1167 #elif defined(cpu_list)
1168 cpu_list(f
, cpu_fprintf
); /* deprecated */