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
) ||
152 (kvm_enabled() && kvm_irqchip_in_kernel())) {
158 static bool all_cpu_threads_idle(void)
162 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
163 if (!cpu_thread_is_idle(env
)) {
170 static void cpu_handle_guest_debug(CPUState
*env
)
172 gdb_set_stop_cpu(env
);
173 qemu_system_debug_request();
174 #ifdef CONFIG_IOTHREAD
179 #ifdef CONFIG_IOTHREAD
180 static void cpu_signal(int sig
)
182 if (cpu_single_env
) {
183 cpu_exit(cpu_single_env
);
190 static void sigbus_reraise(void)
193 struct sigaction action
;
195 memset(&action
, 0, sizeof(action
));
196 action
.sa_handler
= SIG_DFL
;
197 if (!sigaction(SIGBUS
, &action
, NULL
)) {
200 sigaddset(&set
, SIGBUS
);
201 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
203 perror("Failed to re-raise SIGBUS!\n");
207 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
210 if (kvm_on_sigbus(siginfo
->ssi_code
,
211 (void *)(intptr_t)siginfo
->ssi_addr
)) {
216 static void qemu_init_sigbus(void)
218 struct sigaction action
;
220 memset(&action
, 0, sizeof(action
));
221 action
.sa_flags
= SA_SIGINFO
;
222 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
223 sigaction(SIGBUS
, &action
, NULL
);
225 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
228 static void qemu_kvm_eat_signals(CPUState
*env
)
230 struct timespec ts
= { 0, 0 };
236 sigemptyset(&waitset
);
237 sigaddset(&waitset
, SIG_IPI
);
238 sigaddset(&waitset
, SIGBUS
);
241 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
242 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
243 perror("sigtimedwait");
249 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
257 r
= sigpending(&chkset
);
259 perror("sigpending");
262 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
264 #ifndef CONFIG_IOTHREAD
265 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
271 #else /* !CONFIG_LINUX */
273 static void qemu_init_sigbus(void)
277 static void qemu_kvm_eat_signals(CPUState
*env
)
280 #endif /* !CONFIG_LINUX */
283 static int io_thread_fd
= -1;
285 static void qemu_event_increment(void)
287 /* Write 8 bytes to be compatible with eventfd. */
288 static const uint64_t val
= 1;
291 if (io_thread_fd
== -1) {
295 ret
= write(io_thread_fd
, &val
, sizeof(val
));
296 } while (ret
< 0 && errno
== EINTR
);
298 /* EAGAIN is fine, a read must be pending. */
299 if (ret
< 0 && errno
!= EAGAIN
) {
300 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
306 static void qemu_event_read(void *opaque
)
308 int fd
= (intptr_t)opaque
;
312 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
314 len
= read(fd
, buffer
, sizeof(buffer
));
315 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
318 static int qemu_event_init(void)
323 err
= qemu_eventfd(fds
);
327 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
331 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
335 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
336 (void *)(intptr_t)fds
[0]);
338 io_thread_fd
= fds
[1];
347 #ifdef OBSOLETE_KVM_IMPL
348 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
)
434 #if 0 /* Causes regressions: autotest WinXP.64 migrate.tcp */
437 struct sigaction sigact
;
439 memset(&sigact
, 0, sizeof(sigact
));
440 sigact
.sa_handler
= dummy_signal
;
441 sigaction(SIG_IPI
, &sigact
, NULL
);
443 #ifdef CONFIG_IOTHREAD
444 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
445 sigdelset(&set
, SIG_IPI
);
446 sigdelset(&set
, SIGBUS
);
447 r
= kvm_set_signal_mask(env
, &set
);
449 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
454 sigaddset(&set
, SIG_IPI
);
455 sigaddset(&set
, SIGIO
);
456 sigaddset(&set
, SIGALRM
);
457 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
459 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
460 sigdelset(&set
, SIGIO
);
461 sigdelset(&set
, SIGALRM
);
463 sigdelset(&set
, SIG_IPI
);
464 sigdelset(&set
, SIGBUS
);
465 r
= kvm_set_signal_mask(env
, &set
);
467 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
473 static void qemu_tcg_init_cpu_signals(void)
475 #ifdef CONFIG_IOTHREAD
477 struct sigaction sigact
;
479 memset(&sigact
, 0, sizeof(sigact
));
480 sigact
.sa_handler
= cpu_signal
;
481 sigaction(SIG_IPI
, &sigact
, NULL
);
484 sigaddset(&set
, SIG_IPI
);
485 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
491 HANDLE qemu_event_handle
;
493 static void dummy_event_handler(void *opaque
)
497 static int qemu_event_init(void)
499 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
500 if (!qemu_event_handle
) {
501 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
504 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
508 static void qemu_event_increment(void)
510 if (!SetEvent(qemu_event_handle
)) {
511 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
517 static int qemu_signal_init(void)
522 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
527 static void qemu_tcg_init_cpu_signals(void)
532 #ifndef CONFIG_IOTHREAD
533 int qemu_init_main_loop(void)
537 ret
= qemu_signal_init();
544 return qemu_event_init();
547 void qemu_main_loop_start(void)
551 void qemu_init_vcpu(void *_env
)
553 CPUState
*env
= _env
;
556 env
->nr_cores
= smp_cores
;
557 env
->nr_threads
= smp_threads
;
560 r
= kvm_init_vcpu(env
);
562 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
565 qemu_kvm_init_cpu_signals(env
);
567 qemu_tcg_init_cpu_signals();
571 int qemu_cpu_is_self(void *env
)
576 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
581 void resume_all_vcpus(void)
585 void pause_all_vcpus(void)
589 void qemu_cpu_kick(void *env
)
593 void qemu_cpu_kick_self(void)
596 assert(cpu_single_env
);
604 void qemu_notify_event(void)
606 CPUState
*env
= cpu_single_env
;
608 qemu_event_increment ();
612 if (next_cpu
&& env
!= next_cpu
) {
618 #if defined(OBSOLETE_KVM_IMPL) || !defined(CONFIG_KVM)
619 void qemu_mutex_lock_iothread(void) {}
620 void qemu_mutex_unlock_iothread(void) {}
623 void cpu_stop_current(void)
627 void vm_stop(int reason
)
632 #else /* CONFIG_IOTHREAD */
634 QemuMutex qemu_global_mutex
;
635 static QemuMutex qemu_fair_mutex
;
637 static QemuThread io_thread
;
639 static QemuThread
*tcg_cpu_thread
;
640 static QemuCond
*tcg_halt_cond
;
642 static int qemu_system_ready
;
644 static QemuCond qemu_cpu_cond
;
646 static QemuCond qemu_system_cond
;
647 static QemuCond qemu_pause_cond
;
648 static QemuCond qemu_work_cond
;
650 int qemu_init_main_loop(void)
656 ret
= qemu_signal_init();
661 /* Note eventfd must be drained before signalfd handlers run */
662 ret
= qemu_event_init();
667 qemu_cond_init(&qemu_cpu_cond
);
668 qemu_cond_init(&qemu_system_cond
);
669 qemu_cond_init(&qemu_pause_cond
);
670 qemu_cond_init(&qemu_work_cond
);
671 qemu_mutex_init(&qemu_fair_mutex
);
672 qemu_mutex_init(&qemu_global_mutex
);
673 qemu_mutex_lock(&qemu_global_mutex
);
675 qemu_thread_get_self(&io_thread
);
680 void qemu_main_loop_start(void)
682 qemu_system_ready
= 1;
683 qemu_cond_broadcast(&qemu_system_cond
);
686 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
688 struct qemu_work_item wi
;
690 if (qemu_cpu_is_self(env
)) {
697 if (!env
->queued_work_first
) {
698 env
->queued_work_first
= &wi
;
700 env
->queued_work_last
->next
= &wi
;
702 env
->queued_work_last
= &wi
;
708 CPUState
*self_env
= cpu_single_env
;
710 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
711 cpu_single_env
= self_env
;
715 static void flush_queued_work(CPUState
*env
)
717 struct qemu_work_item
*wi
;
719 if (!env
->queued_work_first
) {
723 while ((wi
= env
->queued_work_first
)) {
724 env
->queued_work_first
= wi
->next
;
728 env
->queued_work_last
= NULL
;
729 qemu_cond_broadcast(&qemu_work_cond
);
732 static void qemu_wait_io_event_common(CPUState
*env
)
737 qemu_cond_signal(&qemu_pause_cond
);
739 flush_queued_work(env
);
740 env
->thread_kicked
= false;
743 static void qemu_tcg_wait_io_event(void)
747 while (all_cpu_threads_idle()) {
748 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
751 qemu_mutex_unlock(&qemu_global_mutex
);
754 * Users of qemu_global_mutex can be starved, having no chance
755 * to acquire it since this path will get to it first.
756 * So use another lock to provide fairness.
758 qemu_mutex_lock(&qemu_fair_mutex
);
759 qemu_mutex_unlock(&qemu_fair_mutex
);
761 qemu_mutex_lock(&qemu_global_mutex
);
763 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
764 qemu_wait_io_event_common(env
);
768 static void qemu_kvm_wait_io_event(CPUState
*env
)
770 while (cpu_thread_is_idle(env
)) {
771 qemu_cond_wait(env
->halt_cond
, &qemu_global_mutex
);
774 qemu_kvm_eat_signals(env
);
775 qemu_wait_io_event_common(env
);
778 static void *qemu_kvm_cpu_thread_fn(void *arg
)
783 qemu_mutex_lock(&qemu_global_mutex
);
784 qemu_thread_get_self(env
->thread
);
785 env
->thread_id
= qemu_get_thread_id();
787 r
= kvm_init_vcpu(env
);
789 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
793 qemu_kvm_init_cpu_signals(env
);
795 /* signal CPU creation */
797 qemu_cond_signal(&qemu_cpu_cond
);
799 /* and wait for machine initialization */
800 while (!qemu_system_ready
) {
801 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
805 if (cpu_can_run(env
)) {
806 r
= kvm_cpu_exec(env
);
807 if (r
== EXCP_DEBUG
) {
808 cpu_handle_guest_debug(env
);
811 qemu_kvm_wait_io_event(env
);
817 static void *qemu_tcg_cpu_thread_fn(void *arg
)
821 qemu_tcg_init_cpu_signals();
822 qemu_thread_get_self(env
->thread
);
824 /* signal CPU creation */
825 qemu_mutex_lock(&qemu_global_mutex
);
826 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
827 env
->thread_id
= qemu_get_thread_id();
830 qemu_cond_signal(&qemu_cpu_cond
);
832 /* and wait for machine initialization */
833 while (!qemu_system_ready
) {
834 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
839 qemu_tcg_wait_io_event();
845 static void qemu_cpu_kick_thread(CPUState
*env
)
850 err
= pthread_kill(env
->thread
->thread
, SIG_IPI
);
852 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
856 if (!qemu_cpu_is_self(env
)) {
857 SuspendThread(env
->thread
->thread
);
859 ResumeThread(env
->thread
->thread
);
864 void qemu_cpu_kick(void *_env
)
866 CPUState
*env
= _env
;
868 qemu_cond_broadcast(env
->halt_cond
);
869 if (!env
->thread_kicked
) {
870 qemu_cpu_kick_thread(env
);
871 env
->thread_kicked
= true;
875 void qemu_cpu_kick_self(void)
878 assert(cpu_single_env
);
880 if (!cpu_single_env
->thread_kicked
) {
881 qemu_cpu_kick_thread(cpu_single_env
);
882 cpu_single_env
->thread_kicked
= true;
889 int qemu_cpu_is_self(void *_env
)
891 CPUState
*env
= _env
;
893 return qemu_thread_is_self(env
->thread
);
896 void qemu_mutex_lock_iothread(void)
899 qemu_mutex_lock(&qemu_global_mutex
);
901 qemu_mutex_lock(&qemu_fair_mutex
);
902 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
903 qemu_cpu_kick_thread(first_cpu
);
904 qemu_mutex_lock(&qemu_global_mutex
);
906 qemu_mutex_unlock(&qemu_fair_mutex
);
910 void qemu_mutex_unlock_iothread(void)
912 qemu_mutex_unlock(&qemu_global_mutex
);
915 static int all_vcpus_paused(void)
917 CPUState
*penv
= first_cpu
;
920 if (!penv
->stopped
) {
923 penv
= (CPUState
*)penv
->next_cpu
;
929 void pause_all_vcpus(void)
931 CPUState
*penv
= first_cpu
;
936 penv
= (CPUState
*)penv
->next_cpu
;
939 while (!all_vcpus_paused()) {
940 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
944 penv
= (CPUState
*)penv
->next_cpu
;
949 void resume_all_vcpus(void)
951 CPUState
*penv
= first_cpu
;
957 penv
= (CPUState
*)penv
->next_cpu
;
961 static void qemu_tcg_init_vcpu(void *_env
)
963 CPUState
*env
= _env
;
965 /* share a single thread for all cpus with TCG */
966 if (!tcg_cpu_thread
) {
967 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
968 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
969 qemu_cond_init(env
->halt_cond
);
970 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
971 while (env
->created
== 0) {
972 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
974 tcg_cpu_thread
= env
->thread
;
975 tcg_halt_cond
= env
->halt_cond
;
977 env
->thread
= tcg_cpu_thread
;
978 env
->halt_cond
= tcg_halt_cond
;
982 static void qemu_kvm_start_vcpu(CPUState
*env
)
984 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
985 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
986 qemu_cond_init(env
->halt_cond
);
987 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
988 while (env
->created
== 0) {
989 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
993 void qemu_init_vcpu(void *_env
)
995 CPUState
*env
= _env
;
997 env
->nr_cores
= smp_cores
;
998 env
->nr_threads
= smp_threads
;
1000 qemu_kvm_start_vcpu(env
);
1002 qemu_tcg_init_vcpu(env
);
1006 void qemu_notify_event(void)
1008 qemu_event_increment();
1011 void cpu_stop_current(void)
1013 if (cpu_single_env
) {
1014 cpu_single_env
->stop
= 0;
1015 cpu_single_env
->stopped
= 1;
1016 cpu_exit(cpu_single_env
);
1017 qemu_cond_signal(&qemu_pause_cond
);
1021 void vm_stop(int reason
)
1023 if (!qemu_thread_is_self(&io_thread
)) {
1024 qemu_system_vmstop_request(reason
);
1026 * FIXME: should not return to device code in case
1027 * vm_stop() has been requested.
1037 static int tcg_cpu_exec(CPUState
*env
)
1040 #ifdef CONFIG_PROFILER
1044 #ifdef CONFIG_PROFILER
1045 ti
= profile_getclock();
1050 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1051 env
->icount_decr
.u16
.low
= 0;
1052 env
->icount_extra
= 0;
1053 count
= qemu_icount_round (qemu_next_deadline());
1054 qemu_icount
+= count
;
1055 decr
= (count
> 0xffff) ? 0xffff : count
;
1057 env
->icount_decr
.u16
.low
= decr
;
1058 env
->icount_extra
= count
;
1060 ret
= cpu_exec(env
);
1061 #ifdef CONFIG_PROFILER
1062 qemu_time
+= profile_getclock() - ti
;
1065 /* Fold pending instructions back into the
1066 instruction counter, and clear the interrupt flag. */
1067 qemu_icount
-= (env
->icount_decr
.u16
.low
1068 + env
->icount_extra
);
1069 env
->icount_decr
.u32
= 0;
1070 env
->icount_extra
= 0;
1075 bool cpu_exec_all(void)
1079 if (next_cpu
== NULL
) {
1080 next_cpu
= first_cpu
;
1082 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1083 CPUState
*env
= next_cpu
;
1085 qemu_clock_enable(vm_clock
,
1086 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1088 #ifndef CONFIG_IOTHREAD
1089 if (qemu_alarm_pending()) {
1093 if (cpu_can_run(env
)) {
1094 if (kvm_enabled()) {
1095 r
= kvm_cpu_exec(env
);
1096 qemu_kvm_eat_signals(env
);
1098 r
= tcg_cpu_exec(env
);
1100 if (r
== EXCP_DEBUG
) {
1101 cpu_handle_guest_debug(env
);
1104 } else if (env
->stop
|| env
->stopped
) {
1109 return !all_cpu_threads_idle();
1112 void set_numa_modes(void)
1117 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1118 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1119 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1126 void set_cpu_log(const char *optarg
)
1129 const CPULogItem
*item
;
1131 mask
= cpu_str_to_log_mask(optarg
);
1133 printf("Log items (comma separated):\n");
1134 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1135 printf("%-10s %s\n", item
->name
, item
->help
);
1142 /* Return the virtual CPU time, based on the instruction counter. */
1143 int64_t cpu_get_icount(void)
1146 CPUState
*env
= cpu_single_env
;;
1148 icount
= qemu_icount
;
1150 if (!can_do_io(env
)) {
1151 fprintf(stderr
, "Bad clock read\n");
1153 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1155 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1158 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1160 /* XXX: implement xxx_cpu_list for targets that still miss it */
1161 #if defined(cpu_list_id)
1162 cpu_list_id(f
, cpu_fprintf
, optarg
);
1163 #elif defined(cpu_list)
1164 cpu_list(f
, cpu_fprintf
); /* deprecated */