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"
34 #include "qemu-thread.h"
42 #define SIG_IPI (SIGRTMIN+4)
44 #define SIG_IPI SIGUSR1
49 #include <sys/prctl.h>
52 #define PR_MCE_KILL 33
55 #ifndef PR_MCE_KILL_SET
56 #define PR_MCE_KILL_SET 1
59 #ifndef PR_MCE_KILL_EARLY
60 #define PR_MCE_KILL_EARLY 1
63 #endif /* CONFIG_LINUX */
65 static CPUState
*next_cpu
;
67 /***********************************************************/
68 void hw_error(const char *fmt
, ...)
74 fprintf(stderr
, "qemu: hardware error: ");
75 vfprintf(stderr
, fmt
, ap
);
76 fprintf(stderr
, "\n");
77 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
78 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
80 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
82 cpu_dump_state(env
, stderr
, fprintf
, 0);
89 void cpu_synchronize_all_states(void)
93 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
94 cpu_synchronize_state(cpu
);
98 void cpu_synchronize_all_post_reset(void)
102 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
103 cpu_synchronize_post_reset(cpu
);
107 void cpu_synchronize_all_post_init(void)
111 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
112 cpu_synchronize_post_init(cpu
);
116 int cpu_is_stopped(CPUState
*env
)
118 return !vm_running
|| env
->stopped
;
121 static void do_vm_stop(int reason
)
127 vm_state_notify(0, reason
);
130 monitor_protocol_event(QEVENT_STOP
, NULL
);
134 static int cpu_can_run(CPUState
*env
)
139 if (env
->stopped
|| !vm_running
) {
145 static bool cpu_thread_is_idle(CPUState
*env
)
147 if (env
->stop
|| env
->queued_work_first
) {
150 if (env
->stopped
|| !vm_running
) {
153 if (!env
->halted
|| qemu_cpu_has_work(env
) ||
154 (kvm_enabled() && kvm_irqchip_in_kernel())) {
160 bool all_cpu_threads_idle(void)
164 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
165 if (!cpu_thread_is_idle(env
)) {
172 static void cpu_handle_guest_debug(CPUState
*env
)
174 gdb_set_stop_cpu(env
);
175 qemu_system_debug_request();
176 #ifdef CONFIG_IOTHREAD
181 #ifdef CONFIG_IOTHREAD
182 static void cpu_signal(int sig
)
184 if (cpu_single_env
) {
185 cpu_exit(cpu_single_env
);
192 static void sigbus_reraise(void)
195 struct sigaction action
;
197 memset(&action
, 0, sizeof(action
));
198 action
.sa_handler
= SIG_DFL
;
199 if (!sigaction(SIGBUS
, &action
, NULL
)) {
202 sigaddset(&set
, SIGBUS
);
203 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
205 perror("Failed to re-raise SIGBUS!\n");
209 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
212 if (kvm_on_sigbus(siginfo
->ssi_code
,
213 (void *)(intptr_t)siginfo
->ssi_addr
)) {
218 static void qemu_init_sigbus(void)
220 struct sigaction action
;
222 memset(&action
, 0, sizeof(action
));
223 action
.sa_flags
= SA_SIGINFO
;
224 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
225 sigaction(SIGBUS
, &action
, NULL
);
227 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
230 static void qemu_kvm_eat_signals(CPUState
*env
)
232 struct timespec ts
= { 0, 0 };
238 sigemptyset(&waitset
);
239 sigaddset(&waitset
, SIG_IPI
);
240 sigaddset(&waitset
, SIGBUS
);
243 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
244 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
245 perror("sigtimedwait");
251 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
259 r
= sigpending(&chkset
);
261 perror("sigpending");
264 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
266 #ifndef CONFIG_IOTHREAD
267 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
273 #else /* !CONFIG_LINUX */
275 static void qemu_init_sigbus(void)
279 static void qemu_kvm_eat_signals(CPUState
*env
)
282 #endif /* !CONFIG_LINUX */
285 static int io_thread_fd
= -1;
287 static void qemu_event_increment(void)
289 /* Write 8 bytes to be compatible with eventfd. */
290 static const uint64_t val
= 1;
293 if (io_thread_fd
== -1) {
297 ret
= write(io_thread_fd
, &val
, sizeof(val
));
298 } while (ret
< 0 && errno
== EINTR
);
300 /* EAGAIN is fine, a read must be pending. */
301 if (ret
< 0 && errno
!= EAGAIN
) {
302 fprintf(stderr
, "qemu_event_increment: write() failed: %s\n",
308 static void qemu_event_read(void *opaque
)
310 int fd
= (intptr_t)opaque
;
314 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
316 len
= read(fd
, buffer
, sizeof(buffer
));
317 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
320 static int qemu_event_init(void)
325 err
= qemu_eventfd(fds
);
329 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
333 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
337 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
338 (void *)(intptr_t)fds
[0]);
340 io_thread_fd
= fds
[1];
349 static void dummy_signal(int sig
)
353 /* If we have signalfd, we mask out the signals we want to handle and then
354 * use signalfd to listen for them. We rely on whatever the current signal
355 * handler is to dispatch the signals when we receive them.
357 static void sigfd_handler(void *opaque
)
359 int fd
= (intptr_t)opaque
;
360 struct qemu_signalfd_siginfo info
;
361 struct sigaction action
;
366 len
= read(fd
, &info
, sizeof(info
));
367 } while (len
== -1 && errno
== EINTR
);
369 if (len
== -1 && errno
== EAGAIN
) {
373 if (len
!= sizeof(info
)) {
374 printf("read from sigfd returned %zd: %m\n", len
);
378 sigaction(info
.ssi_signo
, NULL
, &action
);
379 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
380 action
.sa_sigaction(info
.ssi_signo
,
381 (siginfo_t
*)&info
, NULL
);
382 } else if (action
.sa_handler
) {
383 action
.sa_handler(info
.ssi_signo
);
388 static int qemu_signal_init(void)
393 #ifdef CONFIG_IOTHREAD
394 /* SIGUSR2 used by posix-aio-compat.c */
396 sigaddset(&set
, SIGUSR2
);
397 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
400 sigaddset(&set
, SIGIO
);
401 sigaddset(&set
, SIGALRM
);
402 sigaddset(&set
, SIG_IPI
);
403 sigaddset(&set
, SIGBUS
);
404 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
407 sigaddset(&set
, SIGBUS
);
410 * We need to process timer signals synchronously to avoid a race
411 * between exit_request check and KVM vcpu entry.
413 sigaddset(&set
, SIGIO
);
414 sigaddset(&set
, SIGALRM
);
418 sigfd
= qemu_signalfd(&set
);
420 fprintf(stderr
, "failed to create signalfd\n");
424 fcntl_setfl(sigfd
, O_NONBLOCK
);
426 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
427 (void *)(intptr_t)sigfd
);
432 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
436 struct sigaction sigact
;
438 memset(&sigact
, 0, sizeof(sigact
));
439 sigact
.sa_handler
= dummy_signal
;
440 sigaction(SIG_IPI
, &sigact
, NULL
);
442 #ifdef CONFIG_IOTHREAD
443 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
444 sigdelset(&set
, SIG_IPI
);
445 sigdelset(&set
, SIGBUS
);
446 r
= kvm_set_signal_mask(env
, &set
);
448 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
453 sigaddset(&set
, SIG_IPI
);
454 sigaddset(&set
, SIGIO
);
455 sigaddset(&set
, SIGALRM
);
456 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
458 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
459 sigdelset(&set
, SIGIO
);
460 sigdelset(&set
, SIGALRM
);
462 sigdelset(&set
, SIG_IPI
);
463 sigdelset(&set
, SIGBUS
);
464 r
= kvm_set_signal_mask(env
, &set
);
466 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
471 static void qemu_tcg_init_cpu_signals(void)
473 #ifdef CONFIG_IOTHREAD
475 struct sigaction sigact
;
477 memset(&sigact
, 0, sizeof(sigact
));
478 sigact
.sa_handler
= cpu_signal
;
479 sigaction(SIG_IPI
, &sigact
, NULL
);
482 sigaddset(&set
, SIG_IPI
);
483 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
489 HANDLE qemu_event_handle
;
491 static void dummy_event_handler(void *opaque
)
495 static int qemu_event_init(void)
497 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
498 if (!qemu_event_handle
) {
499 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
502 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
506 static void qemu_event_increment(void)
508 if (!SetEvent(qemu_event_handle
)) {
509 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
515 static int qemu_signal_init(void)
520 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
525 static void qemu_tcg_init_cpu_signals(void)
530 #ifndef CONFIG_IOTHREAD
531 int qemu_init_main_loop(void)
535 ret
= qemu_signal_init();
542 return qemu_event_init();
545 void qemu_main_loop_start(void)
549 void qemu_init_vcpu(void *_env
)
551 CPUState
*env
= _env
;
554 env
->nr_cores
= smp_cores
;
555 env
->nr_threads
= smp_threads
;
558 r
= kvm_init_vcpu(env
);
560 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
563 qemu_kvm_init_cpu_signals(env
);
565 qemu_tcg_init_cpu_signals();
569 int qemu_cpu_is_self(void *env
)
574 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
579 void resume_all_vcpus(void)
583 void pause_all_vcpus(void)
587 void qemu_cpu_kick(void *env
)
591 void qemu_cpu_kick_self(void)
594 assert(cpu_single_env
);
602 void qemu_notify_event(void)
604 CPUState
*env
= cpu_single_env
;
606 qemu_event_increment ();
610 if (next_cpu
&& env
!= next_cpu
) {
616 void qemu_mutex_lock_iothread(void) {}
617 void qemu_mutex_unlock_iothread(void) {}
619 void cpu_stop_current(void)
623 void vm_stop(int reason
)
628 #else /* CONFIG_IOTHREAD */
630 QemuMutex qemu_global_mutex
;
631 static QemuMutex qemu_fair_mutex
;
633 static QemuThread io_thread
;
635 static QemuThread
*tcg_cpu_thread
;
636 static QemuCond
*tcg_halt_cond
;
638 static int qemu_system_ready
;
640 static QemuCond qemu_cpu_cond
;
642 static QemuCond qemu_system_cond
;
643 static QemuCond qemu_pause_cond
;
644 static QemuCond qemu_work_cond
;
646 int qemu_init_main_loop(void)
652 ret
= qemu_signal_init();
657 /* Note eventfd must be drained before signalfd handlers run */
658 ret
= qemu_event_init();
663 qemu_cond_init(&qemu_cpu_cond
);
664 qemu_cond_init(&qemu_system_cond
);
665 qemu_cond_init(&qemu_pause_cond
);
666 qemu_cond_init(&qemu_work_cond
);
667 qemu_mutex_init(&qemu_fair_mutex
);
668 qemu_mutex_init(&qemu_global_mutex
);
669 qemu_mutex_lock(&qemu_global_mutex
);
671 qemu_thread_get_self(&io_thread
);
676 void qemu_main_loop_start(void)
678 qemu_system_ready
= 1;
679 qemu_cond_broadcast(&qemu_system_cond
);
682 bool qemu_system_is_ready(void)
684 return qemu_system_ready
;
687 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
689 struct qemu_work_item wi
;
691 if (qemu_cpu_is_self(env
)) {
698 if (!env
->queued_work_first
) {
699 env
->queued_work_first
= &wi
;
701 env
->queued_work_last
->next
= &wi
;
703 env
->queued_work_last
= &wi
;
709 CPUState
*self_env
= cpu_single_env
;
711 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
712 cpu_single_env
= self_env
;
716 static void flush_queued_work(CPUState
*env
)
718 struct qemu_work_item
*wi
;
720 if (!env
->queued_work_first
) {
724 while ((wi
= env
->queued_work_first
)) {
725 env
->queued_work_first
= wi
->next
;
729 env
->queued_work_last
= NULL
;
730 qemu_cond_broadcast(&qemu_work_cond
);
733 static void qemu_wait_io_event_common(CPUState
*env
)
738 qemu_cond_signal(&qemu_pause_cond
);
740 flush_queued_work(env
);
741 env
->thread_kicked
= false;
744 static void qemu_tcg_wait_io_event(void)
748 while (all_cpu_threads_idle()) {
749 /* Start accounting real time to the virtual clock if the CPUs
751 qemu_clock_warp(vm_clock
);
752 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
755 qemu_mutex_unlock(&qemu_global_mutex
);
758 * Users of qemu_global_mutex can be starved, having no chance
759 * to acquire it since this path will get to it first.
760 * So use another lock to provide fairness.
762 qemu_mutex_lock(&qemu_fair_mutex
);
763 qemu_mutex_unlock(&qemu_fair_mutex
);
765 qemu_mutex_lock(&qemu_global_mutex
);
767 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
768 qemu_wait_io_event_common(env
);
772 static void qemu_kvm_wait_io_event(CPUState
*env
)
774 while (cpu_thread_is_idle(env
)) {
775 qemu_cond_wait(env
->halt_cond
, &qemu_global_mutex
);
778 qemu_kvm_eat_signals(env
);
779 qemu_wait_io_event_common(env
);
782 static void *qemu_kvm_cpu_thread_fn(void *arg
)
787 qemu_mutex_lock(&qemu_global_mutex
);
788 qemu_thread_get_self(env
->thread
);
789 env
->thread_id
= qemu_get_thread_id();
791 r
= kvm_init_vcpu(env
);
793 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
797 qemu_kvm_init_cpu_signals(env
);
799 /* signal CPU creation */
801 qemu_cond_signal(&qemu_cpu_cond
);
803 /* and wait for machine initialization */
804 while (!qemu_system_ready
) {
805 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
809 if (cpu_can_run(env
)) {
810 r
= kvm_cpu_exec(env
);
811 if (r
== EXCP_DEBUG
) {
812 cpu_handle_guest_debug(env
);
815 qemu_kvm_wait_io_event(env
);
821 static void *qemu_tcg_cpu_thread_fn(void *arg
)
825 qemu_tcg_init_cpu_signals();
826 qemu_thread_get_self(env
->thread
);
828 /* signal CPU creation */
829 qemu_mutex_lock(&qemu_global_mutex
);
830 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
831 env
->thread_id
= qemu_get_thread_id();
834 qemu_cond_signal(&qemu_cpu_cond
);
836 /* and wait for machine initialization */
837 while (!qemu_system_ready
) {
838 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
843 if (use_icount
&& qemu_next_icount_deadline() <= 0) {
846 qemu_tcg_wait_io_event();
852 static void qemu_cpu_kick_thread(CPUState
*env
)
857 err
= pthread_kill(env
->thread
->thread
, SIG_IPI
);
859 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
863 if (!qemu_cpu_is_self(env
)) {
864 SuspendThread(env
->thread
->thread
);
866 ResumeThread(env
->thread
->thread
);
871 void qemu_cpu_kick(void *_env
)
873 CPUState
*env
= _env
;
875 qemu_cond_broadcast(env
->halt_cond
);
876 if (!env
->thread_kicked
) {
877 qemu_cpu_kick_thread(env
);
878 env
->thread_kicked
= true;
882 void qemu_cpu_kick_self(void)
885 assert(cpu_single_env
);
887 if (!cpu_single_env
->thread_kicked
) {
888 qemu_cpu_kick_thread(cpu_single_env
);
889 cpu_single_env
->thread_kicked
= true;
896 int qemu_cpu_is_self(void *_env
)
898 CPUState
*env
= _env
;
900 return qemu_thread_is_self(env
->thread
);
903 void qemu_mutex_lock_iothread(void)
906 qemu_mutex_lock(&qemu_global_mutex
);
908 qemu_mutex_lock(&qemu_fair_mutex
);
909 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
910 qemu_cpu_kick_thread(first_cpu
);
911 qemu_mutex_lock(&qemu_global_mutex
);
913 qemu_mutex_unlock(&qemu_fair_mutex
);
917 void qemu_mutex_unlock_iothread(void)
919 qemu_mutex_unlock(&qemu_global_mutex
);
922 static int all_vcpus_paused(void)
924 CPUState
*penv
= first_cpu
;
927 if (!penv
->stopped
) {
930 penv
= (CPUState
*)penv
->next_cpu
;
936 void pause_all_vcpus(void)
938 CPUState
*penv
= first_cpu
;
943 penv
= (CPUState
*)penv
->next_cpu
;
946 while (!all_vcpus_paused()) {
947 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
951 penv
= (CPUState
*)penv
->next_cpu
;
956 void resume_all_vcpus(void)
958 CPUState
*penv
= first_cpu
;
964 penv
= (CPUState
*)penv
->next_cpu
;
968 static void qemu_tcg_init_vcpu(void *_env
)
970 CPUState
*env
= _env
;
972 /* share a single thread for all cpus with TCG */
973 if (!tcg_cpu_thread
) {
974 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
975 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
976 qemu_cond_init(env
->halt_cond
);
977 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
978 while (env
->created
== 0) {
979 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
981 tcg_cpu_thread
= env
->thread
;
982 tcg_halt_cond
= env
->halt_cond
;
984 env
->thread
= tcg_cpu_thread
;
985 env
->halt_cond
= tcg_halt_cond
;
989 static void qemu_kvm_start_vcpu(CPUState
*env
)
991 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
992 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
993 qemu_cond_init(env
->halt_cond
);
994 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
995 while (env
->created
== 0) {
996 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
1000 void qemu_init_vcpu(void *_env
)
1002 CPUState
*env
= _env
;
1004 env
->nr_cores
= smp_cores
;
1005 env
->nr_threads
= smp_threads
;
1006 if (kvm_enabled()) {
1007 qemu_kvm_start_vcpu(env
);
1009 qemu_tcg_init_vcpu(env
);
1013 void qemu_notify_event(void)
1015 qemu_event_increment();
1018 void cpu_stop_current(void)
1020 if (cpu_single_env
) {
1021 cpu_single_env
->stop
= 0;
1022 cpu_single_env
->stopped
= 1;
1023 cpu_exit(cpu_single_env
);
1024 qemu_cond_signal(&qemu_pause_cond
);
1028 void vm_stop(int reason
)
1030 if (!qemu_thread_is_self(&io_thread
)) {
1031 qemu_system_vmstop_request(reason
);
1033 * FIXME: should not return to device code in case
1034 * vm_stop() has been requested.
1044 static int tcg_cpu_exec(CPUState
*env
)
1047 #ifdef CONFIG_PROFILER
1051 #ifdef CONFIG_PROFILER
1052 ti
= profile_getclock();
1057 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1058 env
->icount_decr
.u16
.low
= 0;
1059 env
->icount_extra
= 0;
1060 count
= qemu_icount_round(qemu_next_icount_deadline());
1061 qemu_icount
+= count
;
1062 decr
= (count
> 0xffff) ? 0xffff : count
;
1064 env
->icount_decr
.u16
.low
= decr
;
1065 env
->icount_extra
= count
;
1067 ret
= cpu_exec(env
);
1068 #ifdef CONFIG_PROFILER
1069 qemu_time
+= profile_getclock() - ti
;
1072 /* Fold pending instructions back into the
1073 instruction counter, and clear the interrupt flag. */
1074 qemu_icount
-= (env
->icount_decr
.u16
.low
1075 + env
->icount_extra
);
1076 env
->icount_decr
.u32
= 0;
1077 env
->icount_extra
= 0;
1082 bool cpu_exec_all(void)
1086 /* Account partial waits to the vm_clock. */
1087 qemu_clock_warp(vm_clock
);
1089 if (next_cpu
== NULL
) {
1090 next_cpu
= first_cpu
;
1092 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1093 CPUState
*env
= next_cpu
;
1095 qemu_clock_enable(vm_clock
,
1096 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1098 #ifndef CONFIG_IOTHREAD
1099 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_guest_debug(env
);
1114 } else if (env
->stop
|| env
->stopped
) {
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 void set_cpu_log_filename(const char *optarg
)
1154 cpu_set_log_filename(optarg
);
1157 /* Return the virtual CPU time, based on the instruction counter. */
1158 int64_t cpu_get_icount(void)
1161 CPUState
*env
= cpu_single_env
;;
1163 icount
= qemu_icount
;
1165 if (!can_do_io(env
)) {
1166 fprintf(stderr
, "Bad clock read\n");
1168 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1170 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1173 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1175 /* XXX: implement xxx_cpu_list for targets that still miss it */
1176 #if defined(cpu_list_id)
1177 cpu_list_id(f
, cpu_fprintf
, optarg
);
1178 #elif defined(cpu_list)
1179 cpu_list(f
, cpu_fprintf
); /* deprecated */