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_signalfd_init(sigset_t mask
)
353 sigfd
= qemu_signalfd(&mask
);
355 fprintf(stderr
, "failed to create signalfd\n");
359 fcntl_setfl(sigfd
, O_NONBLOCK
);
361 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
362 (void *)(unsigned long) sigfd
);
367 static void qemu_kvm_eat_signals(CPUState
*env
)
369 struct timespec ts
= { 0, 0 };
375 sigemptyset(&waitset
);
376 sigaddset(&waitset
, SIG_IPI
);
377 sigaddset(&waitset
, SIGBUS
);
380 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
381 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
382 perror("sigtimedwait");
388 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
396 r
= sigpending(&chkset
);
398 perror("sigpending");
401 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
403 #ifndef CONFIG_IOTHREAD
404 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
412 HANDLE qemu_event_handle
;
414 static void dummy_event_handler(void *opaque
)
418 static int qemu_event_init(void)
420 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
421 if (!qemu_event_handle
) {
422 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
425 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
429 static void qemu_event_increment(void)
431 if (!SetEvent(qemu_event_handle
)) {
432 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
438 static void qemu_kvm_eat_signals(CPUState
*env
)
443 #ifndef CONFIG_IOTHREAD
444 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
449 struct sigaction sigact
;
451 memset(&sigact
, 0, sizeof(sigact
));
452 sigact
.sa_handler
= dummy_signal
;
453 sigaction(SIG_IPI
, &sigact
, NULL
);
456 sigaddset(&set
, SIG_IPI
);
457 sigaddset(&set
, SIGIO
);
458 sigaddset(&set
, SIGALRM
);
459 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
461 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
462 sigdelset(&set
, SIG_IPI
);
463 sigdelset(&set
, SIGBUS
);
464 sigdelset(&set
, SIGIO
);
465 sigdelset(&set
, SIGALRM
);
466 r
= kvm_set_signal_mask(env
, &set
);
468 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
475 static sigset_t
block_synchronous_signals(void)
480 sigaddset(&set
, SIGBUS
);
483 * We need to process timer signals synchronously to avoid a race
484 * between exit_request check and KVM vcpu entry.
486 sigaddset(&set
, SIGIO
);
487 sigaddset(&set
, SIGALRM
);
494 int qemu_init_main_loop(void)
497 sigset_t blocked_signals
;
500 blocked_signals
= block_synchronous_signals();
502 ret
= qemu_signalfd_init(blocked_signals
);
510 return qemu_event_init();
513 void qemu_main_loop_start(void)
517 void qemu_init_vcpu(void *_env
)
519 CPUState
*env
= _env
;
522 env
->nr_cores
= smp_cores
;
523 env
->nr_threads
= smp_threads
;
526 r
= kvm_init_vcpu(env
);
528 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
531 qemu_kvm_init_cpu_signals(env
);
535 int qemu_cpu_is_self(void *env
)
540 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
545 void resume_all_vcpus(void)
549 void pause_all_vcpus(void)
553 void qemu_cpu_kick(void *env
)
557 void qemu_cpu_kick_self(void)
560 assert(cpu_single_env
);
568 void qemu_notify_event(void)
570 CPUState
*env
= cpu_single_env
;
572 qemu_event_increment ();
576 if (next_cpu
&& env
!= next_cpu
) {
582 void qemu_mutex_lock_iothread(void) {}
583 void qemu_mutex_unlock_iothread(void) {}
585 void cpu_stop_current(void)
589 void vm_stop(int reason
)
594 #else /* CONFIG_IOTHREAD */
596 QemuMutex qemu_global_mutex
;
597 static QemuMutex qemu_fair_mutex
;
599 static QemuThread io_thread
;
601 static QemuThread
*tcg_cpu_thread
;
602 static QemuCond
*tcg_halt_cond
;
604 static int qemu_system_ready
;
606 static QemuCond qemu_cpu_cond
;
608 static QemuCond qemu_system_cond
;
609 static QemuCond qemu_pause_cond
;
610 static QemuCond qemu_work_cond
;
612 static void cpu_signal(int sig
)
614 if (cpu_single_env
) {
615 cpu_exit(cpu_single_env
);
620 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
624 struct sigaction sigact
;
626 memset(&sigact
, 0, sizeof(sigact
));
627 sigact
.sa_handler
= dummy_signal
;
628 sigaction(SIG_IPI
, &sigact
, NULL
);
630 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
631 sigdelset(&set
, SIG_IPI
);
632 sigdelset(&set
, SIGBUS
);
633 r
= kvm_set_signal_mask(env
, &set
);
635 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
640 static void qemu_tcg_init_cpu_signals(void)
643 struct sigaction sigact
;
645 memset(&sigact
, 0, sizeof(sigact
));
646 sigact
.sa_handler
= cpu_signal
;
647 sigaction(SIG_IPI
, &sigact
, NULL
);
650 sigaddset(&set
, SIG_IPI
);
651 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
654 static sigset_t
block_io_signals(void)
658 /* SIGUSR2 used by posix-aio-compat.c */
660 sigaddset(&set
, SIGUSR2
);
661 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
664 sigaddset(&set
, SIGIO
);
665 sigaddset(&set
, SIGALRM
);
666 sigaddset(&set
, SIG_IPI
);
667 sigaddset(&set
, SIGBUS
);
668 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
673 int qemu_init_main_loop(void)
676 sigset_t blocked_signals
;
680 blocked_signals
= block_io_signals();
682 ret
= qemu_signalfd_init(blocked_signals
);
687 /* Note eventfd must be drained before signalfd handlers run */
688 ret
= qemu_event_init();
693 qemu_cond_init(&qemu_cpu_cond
);
694 qemu_cond_init(&qemu_system_cond
);
695 qemu_cond_init(&qemu_pause_cond
);
696 qemu_cond_init(&qemu_work_cond
);
697 qemu_mutex_init(&qemu_fair_mutex
);
698 qemu_mutex_init(&qemu_global_mutex
);
699 qemu_mutex_lock(&qemu_global_mutex
);
701 qemu_thread_get_self(&io_thread
);
706 void qemu_main_loop_start(void)
708 qemu_system_ready
= 1;
709 qemu_cond_broadcast(&qemu_system_cond
);
712 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
714 struct qemu_work_item wi
;
716 if (qemu_cpu_is_self(env
)) {
723 if (!env
->queued_work_first
) {
724 env
->queued_work_first
= &wi
;
726 env
->queued_work_last
->next
= &wi
;
728 env
->queued_work_last
= &wi
;
734 CPUState
*self_env
= cpu_single_env
;
736 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
737 cpu_single_env
= self_env
;
741 static void flush_queued_work(CPUState
*env
)
743 struct qemu_work_item
*wi
;
745 if (!env
->queued_work_first
) {
749 while ((wi
= env
->queued_work_first
)) {
750 env
->queued_work_first
= wi
->next
;
754 env
->queued_work_last
= NULL
;
755 qemu_cond_broadcast(&qemu_work_cond
);
758 static void qemu_wait_io_event_common(CPUState
*env
)
763 qemu_cond_signal(&qemu_pause_cond
);
765 flush_queued_work(env
);
766 env
->thread_kicked
= false;
769 static void qemu_tcg_wait_io_event(void)
773 while (all_cpu_threads_idle()) {
774 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
777 qemu_mutex_unlock(&qemu_global_mutex
);
780 * Users of qemu_global_mutex can be starved, having no chance
781 * to acquire it since this path will get to it first.
782 * So use another lock to provide fairness.
784 qemu_mutex_lock(&qemu_fair_mutex
);
785 qemu_mutex_unlock(&qemu_fair_mutex
);
787 qemu_mutex_lock(&qemu_global_mutex
);
789 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
790 qemu_wait_io_event_common(env
);
794 static void qemu_kvm_wait_io_event(CPUState
*env
)
796 while (cpu_thread_is_idle(env
)) {
797 qemu_cond_wait(env
->halt_cond
, &qemu_global_mutex
);
800 qemu_kvm_eat_signals(env
);
801 qemu_wait_io_event_common(env
);
804 static void *qemu_kvm_cpu_thread_fn(void *arg
)
809 qemu_mutex_lock(&qemu_global_mutex
);
810 qemu_thread_get_self(env
->thread
);
812 r
= kvm_init_vcpu(env
);
814 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
818 qemu_kvm_init_cpu_signals(env
);
820 /* signal CPU creation */
822 qemu_cond_signal(&qemu_cpu_cond
);
824 /* and wait for machine initialization */
825 while (!qemu_system_ready
) {
826 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
830 if (cpu_can_run(env
)) {
831 r
= kvm_cpu_exec(env
);
832 if (r
== EXCP_DEBUG
) {
833 cpu_handle_debug_exception(env
);
836 qemu_kvm_wait_io_event(env
);
842 static void *qemu_tcg_cpu_thread_fn(void *arg
)
846 qemu_tcg_init_cpu_signals();
847 qemu_thread_get_self(env
->thread
);
849 /* signal CPU creation */
850 qemu_mutex_lock(&qemu_global_mutex
);
851 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
854 qemu_cond_signal(&qemu_cpu_cond
);
856 /* and wait for machine initialization */
857 while (!qemu_system_ready
) {
858 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
863 qemu_tcg_wait_io_event();
869 void qemu_cpu_kick(void *_env
)
871 CPUState
*env
= _env
;
873 qemu_cond_broadcast(env
->halt_cond
);
874 if (!env
->thread_kicked
) {
875 qemu_thread_signal(env
->thread
, SIG_IPI
);
876 env
->thread_kicked
= true;
880 void qemu_cpu_kick_self(void)
882 assert(cpu_single_env
);
884 if (!cpu_single_env
->thread_kicked
) {
885 qemu_thread_signal(cpu_single_env
->thread
, SIG_IPI
);
886 cpu_single_env
->thread_kicked
= true;
890 int qemu_cpu_is_self(void *_env
)
892 CPUState
*env
= _env
;
894 return qemu_thread_is_self(env
->thread
);
897 void qemu_mutex_lock_iothread(void)
900 qemu_mutex_lock(&qemu_global_mutex
);
902 qemu_mutex_lock(&qemu_fair_mutex
);
903 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
904 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
905 qemu_mutex_lock(&qemu_global_mutex
);
907 qemu_mutex_unlock(&qemu_fair_mutex
);
911 void qemu_mutex_unlock_iothread(void)
913 qemu_mutex_unlock(&qemu_global_mutex
);
916 static int all_vcpus_paused(void)
918 CPUState
*penv
= first_cpu
;
921 if (!penv
->stopped
) {
924 penv
= (CPUState
*)penv
->next_cpu
;
930 void pause_all_vcpus(void)
932 CPUState
*penv
= first_cpu
;
937 penv
= (CPUState
*)penv
->next_cpu
;
940 while (!all_vcpus_paused()) {
941 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
945 penv
= (CPUState
*)penv
->next_cpu
;
950 void resume_all_vcpus(void)
952 CPUState
*penv
= first_cpu
;
958 penv
= (CPUState
*)penv
->next_cpu
;
962 static void qemu_tcg_init_vcpu(void *_env
)
964 CPUState
*env
= _env
;
966 /* share a single thread for all cpus with TCG */
967 if (!tcg_cpu_thread
) {
968 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
969 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
970 qemu_cond_init(env
->halt_cond
);
971 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
972 while (env
->created
== 0) {
973 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
975 tcg_cpu_thread
= env
->thread
;
976 tcg_halt_cond
= env
->halt_cond
;
978 env
->thread
= tcg_cpu_thread
;
979 env
->halt_cond
= tcg_halt_cond
;
983 static void qemu_kvm_start_vcpu(CPUState
*env
)
985 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
986 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
987 qemu_cond_init(env
->halt_cond
);
988 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
989 while (env
->created
== 0) {
990 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
994 void qemu_init_vcpu(void *_env
)
996 CPUState
*env
= _env
;
998 env
->nr_cores
= smp_cores
;
999 env
->nr_threads
= smp_threads
;
1000 if (kvm_enabled()) {
1001 qemu_kvm_start_vcpu(env
);
1003 qemu_tcg_init_vcpu(env
);
1007 void qemu_notify_event(void)
1009 qemu_event_increment();
1012 void cpu_stop_current(void)
1014 if (cpu_single_env
) {
1015 cpu_single_env
->stop
= 0;
1016 cpu_single_env
->stopped
= 1;
1017 cpu_exit(cpu_single_env
);
1018 qemu_cond_signal(&qemu_pause_cond
);
1022 void vm_stop(int reason
)
1024 if (!qemu_thread_is_self(&io_thread
)) {
1025 qemu_system_vmstop_request(reason
);
1027 * FIXME: should not return to device code in case
1028 * vm_stop() has been requested.
1038 static int tcg_cpu_exec(CPUState
*env
)
1041 #ifdef CONFIG_PROFILER
1045 #ifdef CONFIG_PROFILER
1046 ti
= profile_getclock();
1051 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1052 env
->icount_decr
.u16
.low
= 0;
1053 env
->icount_extra
= 0;
1054 count
= qemu_icount_round (qemu_next_deadline());
1055 qemu_icount
+= count
;
1056 decr
= (count
> 0xffff) ? 0xffff : count
;
1058 env
->icount_decr
.u16
.low
= decr
;
1059 env
->icount_extra
= count
;
1061 ret
= cpu_exec(env
);
1062 #ifdef CONFIG_PROFILER
1063 qemu_time
+= profile_getclock() - ti
;
1066 /* Fold pending instructions back into the
1067 instruction counter, and clear the interrupt flag. */
1068 qemu_icount
-= (env
->icount_decr
.u16
.low
1069 + env
->icount_extra
);
1070 env
->icount_decr
.u32
= 0;
1071 env
->icount_extra
= 0;
1076 bool cpu_exec_all(void)
1080 if (next_cpu
== NULL
) {
1081 next_cpu
= first_cpu
;
1083 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1084 CPUState
*env
= next_cpu
;
1086 qemu_clock_enable(vm_clock
,
1087 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1089 if (qemu_alarm_pending()) {
1092 if (cpu_can_run(env
)) {
1093 if (kvm_enabled()) {
1094 r
= kvm_cpu_exec(env
);
1095 qemu_kvm_eat_signals(env
);
1097 r
= tcg_cpu_exec(env
);
1099 if (r
== EXCP_DEBUG
) {
1100 cpu_handle_debug_exception(env
);
1103 } else if (env
->stop
|| env
->stopped
) {
1108 return !all_cpu_threads_idle();
1111 void set_numa_modes(void)
1116 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1117 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1118 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1125 void set_cpu_log(const char *optarg
)
1128 const CPULogItem
*item
;
1130 mask
= cpu_str_to_log_mask(optarg
);
1132 printf("Log items (comma separated):\n");
1133 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1134 printf("%-10s %s\n", item
->name
, item
->help
);
1141 /* Return the virtual CPU time, based on the instruction counter. */
1142 int64_t cpu_get_icount(void)
1145 CPUState
*env
= cpu_single_env
;;
1147 icount
= qemu_icount
;
1149 if (!can_do_io(env
)) {
1150 fprintf(stderr
, "Bad clock read\n");
1152 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1154 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1157 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1159 /* XXX: implement xxx_cpu_list for targets that still miss it */
1160 #if defined(cpu_list_id)
1161 cpu_list_id(f
, cpu_fprintf
, optarg
);
1162 #elif defined(cpu_list)
1163 cpu_list(f
, cpu_fprintf
); /* deprecated */