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
199 #ifdef CONFIG_IOTHREAD
200 static void cpu_signal(int sig
)
202 if (cpu_single_env
) {
203 cpu_exit(cpu_single_env
);
210 static void sigbus_reraise(void)
213 struct sigaction action
;
215 memset(&action
, 0, sizeof(action
));
216 action
.sa_handler
= SIG_DFL
;
217 if (!sigaction(SIGBUS
, &action
, NULL
)) {
220 sigaddset(&set
, SIGBUS
);
221 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
223 perror("Failed to re-raise SIGBUS!\n");
227 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
230 if (kvm_on_sigbus(siginfo
->ssi_code
,
231 (void *)(intptr_t)siginfo
->ssi_addr
)) {
236 static void qemu_init_sigbus(void)
238 struct sigaction action
;
240 memset(&action
, 0, sizeof(action
));
241 action
.sa_flags
= SA_SIGINFO
;
242 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
243 sigaction(SIGBUS
, &action
, NULL
);
245 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
248 #else /* !CONFIG_LINUX */
250 static void qemu_init_sigbus(void)
253 #endif /* !CONFIG_LINUX */
256 static int io_thread_fd
= -1;
258 static void qemu_event_increment(void)
260 /* Write 8 bytes to be compatible with eventfd. */
261 static const uint64_t val
= 1;
264 if (io_thread_fd
== -1) {
268 ret
= write(io_thread_fd
, &val
, sizeof(val
));
269 } while (ret
< 0 && errno
== EINTR
);
271 /* EAGAIN is fine, a read must be pending. */
272 if (ret
< 0 && errno
!= EAGAIN
) {
273 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
279 static void qemu_event_read(void *opaque
)
281 int fd
= (intptr_t)opaque
;
285 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
287 len
= read(fd
, buffer
, sizeof(buffer
));
288 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
291 static int qemu_event_init(void)
296 err
= qemu_eventfd(fds
);
300 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
304 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
308 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
309 (void *)(intptr_t)fds
[0]);
311 io_thread_fd
= fds
[1];
320 static void dummy_signal(int sig
)
324 /* If we have signalfd, we mask out the signals we want to handle and then
325 * use signalfd to listen for them. We rely on whatever the current signal
326 * handler is to dispatch the signals when we receive them.
328 static void sigfd_handler(void *opaque
)
330 int fd
= (intptr_t)opaque
;
331 struct qemu_signalfd_siginfo info
;
332 struct sigaction action
;
337 len
= read(fd
, &info
, sizeof(info
));
338 } while (len
== -1 && errno
== EINTR
);
340 if (len
== -1 && errno
== EAGAIN
) {
344 if (len
!= sizeof(info
)) {
345 printf("read from sigfd returned %zd: %m\n", len
);
349 sigaction(info
.ssi_signo
, NULL
, &action
);
350 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
351 action
.sa_sigaction(info
.ssi_signo
,
352 (siginfo_t
*)&info
, NULL
);
353 } else if (action
.sa_handler
) {
354 action
.sa_handler(info
.ssi_signo
);
359 static int qemu_signal_init(void)
364 #ifdef CONFIG_IOTHREAD
365 /* SIGUSR2 used by posix-aio-compat.c */
367 sigaddset(&set
, SIGUSR2
);
368 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
371 sigaddset(&set
, SIGIO
);
372 sigaddset(&set
, SIGALRM
);
373 sigaddset(&set
, SIG_IPI
);
374 sigaddset(&set
, SIGBUS
);
375 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
378 sigaddset(&set
, SIGBUS
);
381 * We need to process timer signals synchronously to avoid a race
382 * between exit_request check and KVM vcpu entry.
384 sigaddset(&set
, SIGIO
);
385 sigaddset(&set
, SIGALRM
);
389 sigfd
= qemu_signalfd(&set
);
391 fprintf(stderr
, "failed to create signalfd\n");
395 fcntl_setfl(sigfd
, O_NONBLOCK
);
397 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
398 (void *)(intptr_t)sigfd
);
403 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
407 struct sigaction sigact
;
409 memset(&sigact
, 0, sizeof(sigact
));
410 sigact
.sa_handler
= dummy_signal
;
411 sigaction(SIG_IPI
, &sigact
, NULL
);
413 #ifdef CONFIG_IOTHREAD
414 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
415 sigdelset(&set
, SIG_IPI
);
416 sigdelset(&set
, SIGBUS
);
417 r
= kvm_set_signal_mask(env
, &set
);
419 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
424 sigaddset(&set
, SIG_IPI
);
425 sigaddset(&set
, SIGIO
);
426 sigaddset(&set
, SIGALRM
);
427 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
429 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
430 sigdelset(&set
, SIGIO
);
431 sigdelset(&set
, SIGALRM
);
433 sigdelset(&set
, SIG_IPI
);
434 sigdelset(&set
, SIGBUS
);
435 r
= kvm_set_signal_mask(env
, &set
);
437 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
442 static void qemu_tcg_init_cpu_signals(void)
444 #ifdef CONFIG_IOTHREAD
446 struct sigaction sigact
;
448 memset(&sigact
, 0, sizeof(sigact
));
449 sigact
.sa_handler
= cpu_signal
;
450 sigaction(SIG_IPI
, &sigact
, NULL
);
453 sigaddset(&set
, SIG_IPI
);
454 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
458 static void qemu_kvm_eat_signals(CPUState
*env
)
460 struct timespec ts
= { 0, 0 };
466 sigemptyset(&waitset
);
467 sigaddset(&waitset
, SIG_IPI
);
468 sigaddset(&waitset
, SIGBUS
);
471 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
472 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
473 perror("sigtimedwait");
479 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
487 r
= sigpending(&chkset
);
489 perror("sigpending");
492 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
494 #ifndef CONFIG_IOTHREAD
495 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
503 HANDLE qemu_event_handle
;
505 static void dummy_event_handler(void *opaque
)
509 static int qemu_event_init(void)
511 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
512 if (!qemu_event_handle
) {
513 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
516 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
520 static void qemu_event_increment(void)
522 if (!SetEvent(qemu_event_handle
)) {
523 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
529 static void qemu_kvm_eat_signals(CPUState
*env
)
533 static int qemu_signal_init(void)
538 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
543 static void qemu_tcg_init_cpu_signals(void)
548 #ifndef CONFIG_IOTHREAD
549 int qemu_init_main_loop(void)
553 ret
= qemu_signal_init();
560 return qemu_event_init();
563 void qemu_main_loop_start(void)
567 void qemu_init_vcpu(void *_env
)
569 CPUState
*env
= _env
;
572 env
->nr_cores
= smp_cores
;
573 env
->nr_threads
= smp_threads
;
576 r
= kvm_init_vcpu(env
);
578 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
581 qemu_kvm_init_cpu_signals(env
);
583 qemu_tcg_init_cpu_signals();
587 int qemu_cpu_is_self(void *env
)
592 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
597 void resume_all_vcpus(void)
601 void pause_all_vcpus(void)
605 void qemu_cpu_kick(void *env
)
609 void qemu_cpu_kick_self(void)
612 assert(cpu_single_env
);
620 void qemu_notify_event(void)
622 CPUState
*env
= cpu_single_env
;
624 qemu_event_increment ();
628 if (next_cpu
&& env
!= next_cpu
) {
634 void qemu_mutex_lock_iothread(void) {}
635 void qemu_mutex_unlock_iothread(void) {}
637 void cpu_stop_current(void)
641 void vm_stop(int reason
)
646 #else /* CONFIG_IOTHREAD */
648 QemuMutex qemu_global_mutex
;
649 static QemuMutex qemu_fair_mutex
;
651 static QemuThread io_thread
;
653 static QemuThread
*tcg_cpu_thread
;
654 static QemuCond
*tcg_halt_cond
;
656 static int qemu_system_ready
;
658 static QemuCond qemu_cpu_cond
;
660 static QemuCond qemu_system_cond
;
661 static QemuCond qemu_pause_cond
;
662 static QemuCond qemu_work_cond
;
664 int qemu_init_main_loop(void)
670 ret
= qemu_signal_init();
675 /* Note eventfd must be drained before signalfd handlers run */
676 ret
= qemu_event_init();
681 qemu_cond_init(&qemu_cpu_cond
);
682 qemu_cond_init(&qemu_system_cond
);
683 qemu_cond_init(&qemu_pause_cond
);
684 qemu_cond_init(&qemu_work_cond
);
685 qemu_mutex_init(&qemu_fair_mutex
);
686 qemu_mutex_init(&qemu_global_mutex
);
687 qemu_mutex_lock(&qemu_global_mutex
);
689 qemu_thread_get_self(&io_thread
);
694 void qemu_main_loop_start(void)
696 qemu_system_ready
= 1;
697 qemu_cond_broadcast(&qemu_system_cond
);
700 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
702 struct qemu_work_item wi
;
704 if (qemu_cpu_is_self(env
)) {
711 if (!env
->queued_work_first
) {
712 env
->queued_work_first
= &wi
;
714 env
->queued_work_last
->next
= &wi
;
716 env
->queued_work_last
= &wi
;
722 CPUState
*self_env
= cpu_single_env
;
724 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
725 cpu_single_env
= self_env
;
729 static void flush_queued_work(CPUState
*env
)
731 struct qemu_work_item
*wi
;
733 if (!env
->queued_work_first
) {
737 while ((wi
= env
->queued_work_first
)) {
738 env
->queued_work_first
= wi
->next
;
742 env
->queued_work_last
= NULL
;
743 qemu_cond_broadcast(&qemu_work_cond
);
746 static void qemu_wait_io_event_common(CPUState
*env
)
751 qemu_cond_signal(&qemu_pause_cond
);
753 flush_queued_work(env
);
754 env
->thread_kicked
= false;
757 static void qemu_tcg_wait_io_event(void)
761 while (all_cpu_threads_idle()) {
762 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
765 qemu_mutex_unlock(&qemu_global_mutex
);
768 * Users of qemu_global_mutex can be starved, having no chance
769 * to acquire it since this path will get to it first.
770 * So use another lock to provide fairness.
772 qemu_mutex_lock(&qemu_fair_mutex
);
773 qemu_mutex_unlock(&qemu_fair_mutex
);
775 qemu_mutex_lock(&qemu_global_mutex
);
777 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
778 qemu_wait_io_event_common(env
);
782 static void qemu_kvm_wait_io_event(CPUState
*env
)
784 while (cpu_thread_is_idle(env
)) {
785 qemu_cond_wait(env
->halt_cond
, &qemu_global_mutex
);
788 qemu_kvm_eat_signals(env
);
789 qemu_wait_io_event_common(env
);
792 static void *qemu_kvm_cpu_thread_fn(void *arg
)
797 qemu_mutex_lock(&qemu_global_mutex
);
798 qemu_thread_get_self(env
->thread
);
800 r
= kvm_init_vcpu(env
);
802 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
806 qemu_kvm_init_cpu_signals(env
);
808 /* signal CPU creation */
810 qemu_cond_signal(&qemu_cpu_cond
);
812 /* and wait for machine initialization */
813 while (!qemu_system_ready
) {
814 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
818 if (cpu_can_run(env
)) {
819 r
= kvm_cpu_exec(env
);
820 if (r
== EXCP_DEBUG
) {
821 cpu_handle_debug_exception(env
);
824 qemu_kvm_wait_io_event(env
);
830 static void *qemu_tcg_cpu_thread_fn(void *arg
)
834 qemu_tcg_init_cpu_signals();
835 qemu_thread_get_self(env
->thread
);
837 /* signal CPU creation */
838 qemu_mutex_lock(&qemu_global_mutex
);
839 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
842 qemu_cond_signal(&qemu_cpu_cond
);
844 /* and wait for machine initialization */
845 while (!qemu_system_ready
) {
846 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
851 qemu_tcg_wait_io_event();
857 static void qemu_cpu_kick_thread(CPUState
*env
)
862 err
= pthread_kill(env
->thread
->thread
, SIG_IPI
);
864 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
868 if (!qemu_cpu_is_self(env
)) {
869 SuspendThread(env
->thread
->thread
);
871 ResumeThread(env
->thread
->thread
);
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_cpu_kick_thread(env
);
883 env
->thread_kicked
= true;
887 void qemu_cpu_kick_self(void)
890 assert(cpu_single_env
);
892 if (!cpu_single_env
->thread_kicked
) {
893 qemu_cpu_kick_thread(cpu_single_env
);
894 cpu_single_env
->thread_kicked
= true;
901 int qemu_cpu_is_self(void *_env
)
903 CPUState
*env
= _env
;
905 return qemu_thread_is_self(env
->thread
);
908 void qemu_mutex_lock_iothread(void)
911 qemu_mutex_lock(&qemu_global_mutex
);
913 qemu_mutex_lock(&qemu_fair_mutex
);
914 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
915 qemu_cpu_kick_thread(first_cpu
);
916 qemu_mutex_lock(&qemu_global_mutex
);
918 qemu_mutex_unlock(&qemu_fair_mutex
);
922 void qemu_mutex_unlock_iothread(void)
924 qemu_mutex_unlock(&qemu_global_mutex
);
927 static int all_vcpus_paused(void)
929 CPUState
*penv
= first_cpu
;
932 if (!penv
->stopped
) {
935 penv
= (CPUState
*)penv
->next_cpu
;
941 void pause_all_vcpus(void)
943 CPUState
*penv
= first_cpu
;
948 penv
= (CPUState
*)penv
->next_cpu
;
951 while (!all_vcpus_paused()) {
952 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
956 penv
= (CPUState
*)penv
->next_cpu
;
961 void resume_all_vcpus(void)
963 CPUState
*penv
= first_cpu
;
969 penv
= (CPUState
*)penv
->next_cpu
;
973 static void qemu_tcg_init_vcpu(void *_env
)
975 CPUState
*env
= _env
;
977 /* share a single thread for all cpus with TCG */
978 if (!tcg_cpu_thread
) {
979 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
980 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
981 qemu_cond_init(env
->halt_cond
);
982 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
983 while (env
->created
== 0) {
984 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
986 tcg_cpu_thread
= env
->thread
;
987 tcg_halt_cond
= env
->halt_cond
;
989 env
->thread
= tcg_cpu_thread
;
990 env
->halt_cond
= tcg_halt_cond
;
994 static void qemu_kvm_start_vcpu(CPUState
*env
)
996 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
997 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
998 qemu_cond_init(env
->halt_cond
);
999 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
1000 while (env
->created
== 0) {
1001 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
1005 void qemu_init_vcpu(void *_env
)
1007 CPUState
*env
= _env
;
1009 env
->nr_cores
= smp_cores
;
1010 env
->nr_threads
= smp_threads
;
1011 if (kvm_enabled()) {
1012 qemu_kvm_start_vcpu(env
);
1014 qemu_tcg_init_vcpu(env
);
1018 void qemu_notify_event(void)
1020 qemu_event_increment();
1023 void cpu_stop_current(void)
1025 if (cpu_single_env
) {
1026 cpu_single_env
->stop
= 0;
1027 cpu_single_env
->stopped
= 1;
1028 cpu_exit(cpu_single_env
);
1029 qemu_cond_signal(&qemu_pause_cond
);
1033 void vm_stop(int reason
)
1035 if (!qemu_thread_is_self(&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 #ifndef CONFIG_IOTHREAD
1101 if (qemu_alarm_pending()) {
1105 if (cpu_can_run(env
)) {
1106 if (kvm_enabled()) {
1107 r
= kvm_cpu_exec(env
);
1108 qemu_kvm_eat_signals(env
);
1110 r
= tcg_cpu_exec(env
);
1112 if (r
== EXCP_DEBUG
) {
1113 cpu_handle_debug_exception(env
);
1116 } else if (env
->stop
|| env
->stopped
) {
1121 return !all_cpu_threads_idle();
1124 void set_numa_modes(void)
1129 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1130 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1131 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1138 void set_cpu_log(const char *optarg
)
1141 const CPULogItem
*item
;
1143 mask
= cpu_str_to_log_mask(optarg
);
1145 printf("Log items (comma separated):\n");
1146 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1147 printf("%-10s %s\n", item
->name
, item
->help
);
1154 /* Return the virtual CPU time, based on the instruction counter. */
1155 int64_t cpu_get_icount(void)
1158 CPUState
*env
= cpu_single_env
;;
1160 icount
= qemu_icount
;
1162 if (!can_do_io(env
)) {
1163 fprintf(stderr
, "Bad clock read\n");
1165 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1167 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1170 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1172 /* XXX: implement xxx_cpu_list for targets that still miss it */
1173 #if defined(cpu_list_id)
1174 cpu_list_id(f
, cpu_fprintf
, optarg
);
1175 #elif defined(cpu_list)
1176 cpu_list(f
, cpu_fprintf
); /* deprecated */