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"
35 #include "qemu-thread.h"
40 #define SIG_IPI (SIGRTMIN+4)
42 #define SIG_IPI SIGUSR1
47 #include <sys/prctl.h>
50 #define PR_MCE_KILL 33
53 #ifndef PR_MCE_KILL_SET
54 #define PR_MCE_KILL_SET 1
57 #ifndef PR_MCE_KILL_EARLY
58 #define PR_MCE_KILL_EARLY 1
61 #endif /* CONFIG_LINUX */
63 static CPUState
*next_cpu
;
65 /***********************************************************/
66 void hw_error(const char *fmt
, ...)
72 fprintf(stderr
, "qemu: hardware error: ");
73 vfprintf(stderr
, fmt
, ap
);
74 fprintf(stderr
, "\n");
75 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
76 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
78 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
80 cpu_dump_state(env
, stderr
, fprintf
, 0);
87 void cpu_synchronize_all_states(void)
91 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
92 cpu_synchronize_state(cpu
);
96 void cpu_synchronize_all_post_reset(void)
100 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
101 cpu_synchronize_post_reset(cpu
);
105 void cpu_synchronize_all_post_init(void)
109 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
110 cpu_synchronize_post_init(cpu
);
114 int cpu_is_stopped(CPUState
*env
)
116 return !vm_running
|| env
->stopped
;
119 static void do_vm_stop(int reason
)
125 vm_state_notify(0, reason
);
128 monitor_protocol_event(QEVENT_STOP
, NULL
);
132 static int cpu_can_run(CPUState
*env
)
137 if (env
->stopped
|| !vm_running
) {
143 static bool cpu_thread_is_idle(CPUState
*env
)
145 if (env
->stop
|| env
->queued_work_first
) {
148 if (env
->stopped
|| !vm_running
) {
151 if (!env
->halted
|| qemu_cpu_has_work(env
)) {
157 static bool all_cpu_threads_idle(void)
161 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
162 if (!cpu_thread_is_idle(env
)) {
169 static CPUDebugExcpHandler
*debug_excp_handler
;
171 CPUDebugExcpHandler
*cpu_set_debug_excp_handler(CPUDebugExcpHandler
*handler
)
173 CPUDebugExcpHandler
*old_handler
= debug_excp_handler
;
175 debug_excp_handler
= handler
;
179 static void cpu_handle_debug_exception(CPUState
*env
)
183 if (!env
->watchpoint_hit
) {
184 QTAILQ_FOREACH(wp
, &env
->watchpoints
, entry
) {
185 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
188 if (debug_excp_handler
) {
189 debug_excp_handler(env
);
192 gdb_set_stop_cpu(env
);
193 qemu_system_debug_request();
194 #ifdef CONFIG_IOTHREAD
200 static void sigbus_reraise(void)
203 struct sigaction action
;
205 memset(&action
, 0, sizeof(action
));
206 action
.sa_handler
= SIG_DFL
;
207 if (!sigaction(SIGBUS
, &action
, NULL
)) {
210 sigaddset(&set
, SIGBUS
);
211 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
213 perror("Failed to re-raise SIGBUS!\n");
217 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
220 if (kvm_on_sigbus(siginfo
->ssi_code
,
221 (void *)(intptr_t)siginfo
->ssi_addr
)) {
226 static void qemu_init_sigbus(void)
228 struct sigaction action
;
230 memset(&action
, 0, sizeof(action
));
231 action
.sa_flags
= SA_SIGINFO
;
232 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
233 sigaction(SIGBUS
, &action
, NULL
);
235 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
238 #else /* !CONFIG_LINUX */
240 static void qemu_init_sigbus(void)
243 #endif /* !CONFIG_LINUX */
246 static int io_thread_fd
= -1;
248 static void qemu_event_increment(void)
250 /* Write 8 bytes to be compatible with eventfd. */
251 static const uint64_t val
= 1;
254 if (io_thread_fd
== -1) {
258 ret
= write(io_thread_fd
, &val
, sizeof(val
));
259 } while (ret
< 0 && errno
== EINTR
);
261 /* EAGAIN is fine, a read must be pending. */
262 if (ret
< 0 && errno
!= EAGAIN
) {
263 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
269 static void qemu_event_read(void *opaque
)
271 int fd
= (unsigned long)opaque
;
275 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
277 len
= read(fd
, buffer
, sizeof(buffer
));
278 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
281 static int qemu_event_init(void)
286 err
= qemu_eventfd(fds
);
290 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
294 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
298 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
299 (void *)(unsigned long)fds
[0]);
301 io_thread_fd
= fds
[1];
310 static void dummy_signal(int sig
)
314 /* If we have signalfd, we mask out the signals we want to handle and then
315 * use signalfd to listen for them. We rely on whatever the current signal
316 * handler is to dispatch the signals when we receive them.
318 static void sigfd_handler(void *opaque
)
320 int fd
= (unsigned long) opaque
;
321 struct qemu_signalfd_siginfo info
;
322 struct sigaction action
;
327 len
= read(fd
, &info
, sizeof(info
));
328 } while (len
== -1 && errno
== EINTR
);
330 if (len
== -1 && errno
== EAGAIN
) {
334 if (len
!= sizeof(info
)) {
335 printf("read from sigfd returned %zd: %m\n", len
);
339 sigaction(info
.ssi_signo
, NULL
, &action
);
340 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
341 action
.sa_sigaction(info
.ssi_signo
,
342 (siginfo_t
*)&info
, NULL
);
343 } else if (action
.sa_handler
) {
344 action
.sa_handler(info
.ssi_signo
);
349 static int qemu_signal_init(void)
354 #ifdef CONFIG_IOTHREAD
355 /* SIGUSR2 used by posix-aio-compat.c */
357 sigaddset(&set
, SIGUSR2
);
358 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
361 sigaddset(&set
, SIGIO
);
362 sigaddset(&set
, SIGALRM
);
363 sigaddset(&set
, SIG_IPI
);
364 sigaddset(&set
, SIGBUS
);
365 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
368 sigaddset(&set
, SIGBUS
);
371 * We need to process timer signals synchronously to avoid a race
372 * between exit_request check and KVM vcpu entry.
374 sigaddset(&set
, SIGIO
);
375 sigaddset(&set
, SIGALRM
);
379 sigfd
= qemu_signalfd(&set
);
381 fprintf(stderr
, "failed to create signalfd\n");
385 fcntl_setfl(sigfd
, O_NONBLOCK
);
387 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
388 (void *)(unsigned long) sigfd
);
393 static void qemu_kvm_eat_signals(CPUState
*env
)
395 struct timespec ts
= { 0, 0 };
401 sigemptyset(&waitset
);
402 sigaddset(&waitset
, SIG_IPI
);
403 sigaddset(&waitset
, SIGBUS
);
406 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
407 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
408 perror("sigtimedwait");
414 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
422 r
= sigpending(&chkset
);
424 perror("sigpending");
427 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
429 #ifndef CONFIG_IOTHREAD
430 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
438 HANDLE qemu_event_handle
;
440 static void dummy_event_handler(void *opaque
)
444 static int qemu_event_init(void)
446 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
447 if (!qemu_event_handle
) {
448 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
451 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
455 static void qemu_event_increment(void)
457 if (!SetEvent(qemu_event_handle
)) {
458 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
464 static void qemu_kvm_eat_signals(CPUState
*env
)
468 static int qemu_signal_init(void)
475 #ifndef CONFIG_IOTHREAD
476 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
481 struct sigaction sigact
;
483 memset(&sigact
, 0, sizeof(sigact
));
484 sigact
.sa_handler
= dummy_signal
;
485 sigaction(SIG_IPI
, &sigact
, NULL
);
488 sigaddset(&set
, SIG_IPI
);
489 sigaddset(&set
, SIGIO
);
490 sigaddset(&set
, SIGALRM
);
491 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
493 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
494 sigdelset(&set
, SIG_IPI
);
495 sigdelset(&set
, SIGBUS
);
496 sigdelset(&set
, SIGIO
);
497 sigdelset(&set
, SIGALRM
);
498 r
= kvm_set_signal_mask(env
, &set
);
500 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
506 int qemu_init_main_loop(void)
510 ret
= qemu_signal_init();
517 return qemu_event_init();
520 void qemu_main_loop_start(void)
524 void qemu_init_vcpu(void *_env
)
526 CPUState
*env
= _env
;
529 env
->nr_cores
= smp_cores
;
530 env
->nr_threads
= smp_threads
;
533 r
= kvm_init_vcpu(env
);
535 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
538 qemu_kvm_init_cpu_signals(env
);
542 int qemu_cpu_is_self(void *env
)
547 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
552 void resume_all_vcpus(void)
556 void pause_all_vcpus(void)
560 void qemu_cpu_kick(void *env
)
564 void qemu_cpu_kick_self(void)
567 assert(cpu_single_env
);
575 void qemu_notify_event(void)
577 CPUState
*env
= cpu_single_env
;
579 qemu_event_increment ();
583 if (next_cpu
&& env
!= next_cpu
) {
589 void qemu_mutex_lock_iothread(void) {}
590 void qemu_mutex_unlock_iothread(void) {}
592 void cpu_stop_current(void)
596 void vm_stop(int reason
)
601 #else /* CONFIG_IOTHREAD */
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 int qemu_init_main_loop(void)
667 ret
= qemu_signal_init();
672 /* Note eventfd must be drained before signalfd handlers run */
673 ret
= qemu_event_init();
678 qemu_cond_init(&qemu_cpu_cond
);
679 qemu_cond_init(&qemu_system_cond
);
680 qemu_cond_init(&qemu_pause_cond
);
681 qemu_cond_init(&qemu_work_cond
);
682 qemu_mutex_init(&qemu_fair_mutex
);
683 qemu_mutex_init(&qemu_global_mutex
);
684 qemu_mutex_lock(&qemu_global_mutex
);
686 qemu_thread_get_self(&io_thread
);
691 void qemu_main_loop_start(void)
693 qemu_system_ready
= 1;
694 qemu_cond_broadcast(&qemu_system_cond
);
697 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
699 struct qemu_work_item wi
;
701 if (qemu_cpu_is_self(env
)) {
708 if (!env
->queued_work_first
) {
709 env
->queued_work_first
= &wi
;
711 env
->queued_work_last
->next
= &wi
;
713 env
->queued_work_last
= &wi
;
719 CPUState
*self_env
= cpu_single_env
;
721 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
722 cpu_single_env
= self_env
;
726 static void flush_queued_work(CPUState
*env
)
728 struct qemu_work_item
*wi
;
730 if (!env
->queued_work_first
) {
734 while ((wi
= env
->queued_work_first
)) {
735 env
->queued_work_first
= wi
->next
;
739 env
->queued_work_last
= NULL
;
740 qemu_cond_broadcast(&qemu_work_cond
);
743 static void qemu_wait_io_event_common(CPUState
*env
)
748 qemu_cond_signal(&qemu_pause_cond
);
750 flush_queued_work(env
);
751 env
->thread_kicked
= false;
754 static void qemu_tcg_wait_io_event(void)
758 while (all_cpu_threads_idle()) {
759 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
762 qemu_mutex_unlock(&qemu_global_mutex
);
765 * Users of qemu_global_mutex can be starved, having no chance
766 * to acquire it since this path will get to it first.
767 * So use another lock to provide fairness.
769 qemu_mutex_lock(&qemu_fair_mutex
);
770 qemu_mutex_unlock(&qemu_fair_mutex
);
772 qemu_mutex_lock(&qemu_global_mutex
);
774 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
775 qemu_wait_io_event_common(env
);
779 static void qemu_kvm_wait_io_event(CPUState
*env
)
781 while (cpu_thread_is_idle(env
)) {
782 qemu_cond_wait(env
->halt_cond
, &qemu_global_mutex
);
785 qemu_kvm_eat_signals(env
);
786 qemu_wait_io_event_common(env
);
789 static void *qemu_kvm_cpu_thread_fn(void *arg
)
794 qemu_mutex_lock(&qemu_global_mutex
);
795 qemu_thread_get_self(env
->thread
);
797 r
= kvm_init_vcpu(env
);
799 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
803 qemu_kvm_init_cpu_signals(env
);
805 /* signal CPU creation */
807 qemu_cond_signal(&qemu_cpu_cond
);
809 /* and wait for machine initialization */
810 while (!qemu_system_ready
) {
811 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
815 if (cpu_can_run(env
)) {
816 r
= kvm_cpu_exec(env
);
817 if (r
== EXCP_DEBUG
) {
818 cpu_handle_debug_exception(env
);
821 qemu_kvm_wait_io_event(env
);
827 static void *qemu_tcg_cpu_thread_fn(void *arg
)
831 qemu_tcg_init_cpu_signals();
832 qemu_thread_get_self(env
->thread
);
834 /* signal CPU creation */
835 qemu_mutex_lock(&qemu_global_mutex
);
836 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
839 qemu_cond_signal(&qemu_cpu_cond
);
841 /* and wait for machine initialization */
842 while (!qemu_system_ready
) {
843 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
848 qemu_tcg_wait_io_event();
854 void qemu_cpu_kick(void *_env
)
856 CPUState
*env
= _env
;
858 qemu_cond_broadcast(env
->halt_cond
);
859 if (!env
->thread_kicked
) {
860 qemu_thread_signal(env
->thread
, SIG_IPI
);
861 env
->thread_kicked
= true;
865 void qemu_cpu_kick_self(void)
867 assert(cpu_single_env
);
869 if (!cpu_single_env
->thread_kicked
) {
870 qemu_thread_signal(cpu_single_env
->thread
, SIG_IPI
);
871 cpu_single_env
->thread_kicked
= true;
875 int qemu_cpu_is_self(void *_env
)
877 CPUState
*env
= _env
;
879 return qemu_thread_is_self(env
->thread
);
882 void qemu_mutex_lock_iothread(void)
885 qemu_mutex_lock(&qemu_global_mutex
);
887 qemu_mutex_lock(&qemu_fair_mutex
);
888 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
889 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
890 qemu_mutex_lock(&qemu_global_mutex
);
892 qemu_mutex_unlock(&qemu_fair_mutex
);
896 void qemu_mutex_unlock_iothread(void)
898 qemu_mutex_unlock(&qemu_global_mutex
);
901 static int all_vcpus_paused(void)
903 CPUState
*penv
= first_cpu
;
906 if (!penv
->stopped
) {
909 penv
= (CPUState
*)penv
->next_cpu
;
915 void pause_all_vcpus(void)
917 CPUState
*penv
= first_cpu
;
922 penv
= (CPUState
*)penv
->next_cpu
;
925 while (!all_vcpus_paused()) {
926 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
930 penv
= (CPUState
*)penv
->next_cpu
;
935 void resume_all_vcpus(void)
937 CPUState
*penv
= first_cpu
;
943 penv
= (CPUState
*)penv
->next_cpu
;
947 static void qemu_tcg_init_vcpu(void *_env
)
949 CPUState
*env
= _env
;
951 /* share a single thread for all cpus with TCG */
952 if (!tcg_cpu_thread
) {
953 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
954 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
955 qemu_cond_init(env
->halt_cond
);
956 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
957 while (env
->created
== 0) {
958 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
960 tcg_cpu_thread
= env
->thread
;
961 tcg_halt_cond
= env
->halt_cond
;
963 env
->thread
= tcg_cpu_thread
;
964 env
->halt_cond
= tcg_halt_cond
;
968 static void qemu_kvm_start_vcpu(CPUState
*env
)
970 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
971 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
972 qemu_cond_init(env
->halt_cond
);
973 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
974 while (env
->created
== 0) {
975 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
979 void qemu_init_vcpu(void *_env
)
981 CPUState
*env
= _env
;
983 env
->nr_cores
= smp_cores
;
984 env
->nr_threads
= smp_threads
;
986 qemu_kvm_start_vcpu(env
);
988 qemu_tcg_init_vcpu(env
);
992 void qemu_notify_event(void)
994 qemu_event_increment();
997 void cpu_stop_current(void)
999 if (cpu_single_env
) {
1000 cpu_single_env
->stop
= 0;
1001 cpu_single_env
->stopped
= 1;
1002 cpu_exit(cpu_single_env
);
1003 qemu_cond_signal(&qemu_pause_cond
);
1007 void vm_stop(int reason
)
1009 if (!qemu_thread_is_self(&io_thread
)) {
1010 qemu_system_vmstop_request(reason
);
1012 * FIXME: should not return to device code in case
1013 * vm_stop() has been requested.
1023 static int tcg_cpu_exec(CPUState
*env
)
1026 #ifdef CONFIG_PROFILER
1030 #ifdef CONFIG_PROFILER
1031 ti
= profile_getclock();
1036 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1037 env
->icount_decr
.u16
.low
= 0;
1038 env
->icount_extra
= 0;
1039 count
= qemu_icount_round (qemu_next_deadline());
1040 qemu_icount
+= count
;
1041 decr
= (count
> 0xffff) ? 0xffff : count
;
1043 env
->icount_decr
.u16
.low
= decr
;
1044 env
->icount_extra
= count
;
1046 ret
= cpu_exec(env
);
1047 #ifdef CONFIG_PROFILER
1048 qemu_time
+= profile_getclock() - ti
;
1051 /* Fold pending instructions back into the
1052 instruction counter, and clear the interrupt flag. */
1053 qemu_icount
-= (env
->icount_decr
.u16
.low
1054 + env
->icount_extra
);
1055 env
->icount_decr
.u32
= 0;
1056 env
->icount_extra
= 0;
1061 bool cpu_exec_all(void)
1065 if (next_cpu
== NULL
) {
1066 next_cpu
= first_cpu
;
1068 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1069 CPUState
*env
= next_cpu
;
1071 qemu_clock_enable(vm_clock
,
1072 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1074 #ifndef CONFIG_IOTHREAD
1075 if (qemu_alarm_pending()) {
1079 if (cpu_can_run(env
)) {
1080 if (kvm_enabled()) {
1081 r
= kvm_cpu_exec(env
);
1082 qemu_kvm_eat_signals(env
);
1084 r
= tcg_cpu_exec(env
);
1086 if (r
== EXCP_DEBUG
) {
1087 cpu_handle_debug_exception(env
);
1090 } else if (env
->stop
|| env
->stopped
) {
1095 return !all_cpu_threads_idle();
1098 void set_numa_modes(void)
1103 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1104 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1105 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1112 void set_cpu_log(const char *optarg
)
1115 const CPULogItem
*item
;
1117 mask
= cpu_str_to_log_mask(optarg
);
1119 printf("Log items (comma separated):\n");
1120 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1121 printf("%-10s %s\n", item
->name
, item
->help
);
1128 /* Return the virtual CPU time, based on the instruction counter. */
1129 int64_t cpu_get_icount(void)
1132 CPUState
*env
= cpu_single_env
;;
1134 icount
= qemu_icount
;
1136 if (!can_do_io(env
)) {
1137 fprintf(stderr
, "Bad clock read\n");
1139 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1141 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1144 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1146 /* XXX: implement xxx_cpu_list for targets that still miss it */
1147 #if defined(cpu_list_id)
1148 cpu_list_id(f
, cpu_fprintf
, optarg
);
1149 #elif defined(cpu_list)
1150 cpu_list(f
, cpu_fprintf
); /* deprecated */