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"
43 #define SIG_IPI (SIGRTMIN+4)
45 #define SIG_IPI SIGUSR1
50 #include <sys/prctl.h>
53 #define PR_MCE_KILL 33
56 #ifndef PR_MCE_KILL_SET
57 #define PR_MCE_KILL_SET 1
60 #ifndef PR_MCE_KILL_EARLY
61 #define PR_MCE_KILL_EARLY 1
64 #endif /* CONFIG_LINUX */
66 static CPUState
*next_cpu
;
68 /***********************************************************/
69 void hw_error(const char *fmt
, ...)
75 fprintf(stderr
, "qemu: hardware error: ");
76 vfprintf(stderr
, fmt
, ap
);
77 fprintf(stderr
, "\n");
78 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
79 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
81 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
83 cpu_dump_state(env
, stderr
, fprintf
, 0);
90 void cpu_synchronize_all_states(void)
94 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
95 cpu_synchronize_state(cpu
);
99 void cpu_synchronize_all_post_reset(void)
103 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
104 cpu_synchronize_post_reset(cpu
);
108 void cpu_synchronize_all_post_init(void)
112 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
113 cpu_synchronize_post_init(cpu
);
117 int cpu_is_stopped(CPUState
*env
)
119 return !vm_running
|| env
->stopped
;
122 static void do_vm_stop(int reason
)
128 vm_state_notify(0, reason
);
131 monitor_protocol_event(QEVENT_STOP
, NULL
);
135 static int cpu_can_run(CPUState
*env
)
140 if (env
->stopped
|| !vm_running
) {
146 static bool cpu_thread_is_idle(CPUState
*env
)
148 if (env
->stop
|| env
->queued_work_first
) {
151 if (env
->stopped
|| !vm_running
) {
154 if (!env
->halted
|| qemu_cpu_has_work(env
) ||
155 (kvm_enabled() && kvm_irqchip_in_kernel())) {
161 bool all_cpu_threads_idle(void)
165 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
166 if (!cpu_thread_is_idle(env
)) {
173 static void cpu_handle_guest_debug(CPUState
*env
)
175 gdb_set_stop_cpu(env
);
176 qemu_system_debug_request();
177 #ifdef CONFIG_IOTHREAD
182 #ifdef CONFIG_IOTHREAD
183 static void cpu_signal(int sig
)
185 if (cpu_single_env
) {
186 cpu_exit(cpu_single_env
);
193 static void sigbus_reraise(void)
196 struct sigaction action
;
198 memset(&action
, 0, sizeof(action
));
199 action
.sa_handler
= SIG_DFL
;
200 if (!sigaction(SIGBUS
, &action
, NULL
)) {
203 sigaddset(&set
, SIGBUS
);
204 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
206 perror("Failed to re-raise SIGBUS!\n");
210 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
213 if (kvm_on_sigbus(siginfo
->ssi_code
,
214 (void *)(intptr_t)siginfo
->ssi_addr
)) {
219 static void qemu_init_sigbus(void)
221 struct sigaction action
;
223 memset(&action
, 0, sizeof(action
));
224 action
.sa_flags
= SA_SIGINFO
;
225 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
226 sigaction(SIGBUS
, &action
, NULL
);
228 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
231 static void qemu_kvm_eat_signals(CPUState
*env
)
233 struct timespec ts
= { 0, 0 };
239 sigemptyset(&waitset
);
240 sigaddset(&waitset
, SIG_IPI
);
241 sigaddset(&waitset
, SIGBUS
);
244 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
245 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
246 perror("sigtimedwait");
252 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
260 r
= sigpending(&chkset
);
262 perror("sigpending");
265 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
267 #ifndef CONFIG_IOTHREAD
268 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
274 #else /* !CONFIG_LINUX */
276 static void qemu_init_sigbus(void)
280 static void qemu_kvm_eat_signals(CPUState
*env
)
283 #endif /* !CONFIG_LINUX */
286 static int io_thread_fd
= -1;
288 static void qemu_event_increment(void)
290 /* Write 8 bytes to be compatible with eventfd. */
291 static const uint64_t val
= 1;
294 if (io_thread_fd
== -1) {
298 ret
= write(io_thread_fd
, &val
, sizeof(val
));
299 } while (ret
< 0 && errno
== EINTR
);
301 /* EAGAIN is fine, a read must be pending. */
302 if (ret
< 0 && errno
!= EAGAIN
) {
303 fprintf(stderr
, "qemu_event_increment: write() failed: %s\n",
309 static void qemu_event_read(void *opaque
)
311 int fd
= (intptr_t)opaque
;
315 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
317 len
= read(fd
, buffer
, sizeof(buffer
));
318 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
321 static int qemu_event_init(void)
326 err
= qemu_eventfd(fds
);
330 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
334 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
338 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
339 (void *)(intptr_t)fds
[0]);
341 io_thread_fd
= fds
[1];
350 static void dummy_signal(int sig
)
354 /* If we have signalfd, we mask out the signals we want to handle and then
355 * use signalfd to listen for them. We rely on whatever the current signal
356 * handler is to dispatch the signals when we receive them.
358 static void sigfd_handler(void *opaque
)
360 int fd
= (intptr_t)opaque
;
361 struct qemu_signalfd_siginfo info
;
362 struct sigaction action
;
367 len
= read(fd
, &info
, sizeof(info
));
368 } while (len
== -1 && errno
== EINTR
);
370 if (len
== -1 && errno
== EAGAIN
) {
374 if (len
!= sizeof(info
)) {
375 printf("read from sigfd returned %zd: %m\n", len
);
379 sigaction(info
.ssi_signo
, NULL
, &action
);
380 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
381 action
.sa_sigaction(info
.ssi_signo
,
382 (siginfo_t
*)&info
, NULL
);
383 } else if (action
.sa_handler
) {
384 action
.sa_handler(info
.ssi_signo
);
389 static int qemu_signal_init(void)
394 #ifdef CONFIG_IOTHREAD
395 /* SIGUSR2 used by posix-aio-compat.c */
397 sigaddset(&set
, SIGUSR2
);
398 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
401 sigaddset(&set
, SIGIO
);
402 sigaddset(&set
, SIGALRM
);
403 sigaddset(&set
, SIG_IPI
);
404 sigaddset(&set
, SIGBUS
);
405 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
408 sigaddset(&set
, SIGBUS
);
411 * We need to process timer signals synchronously to avoid a race
412 * between exit_request check and KVM vcpu entry.
414 sigaddset(&set
, SIGIO
);
415 sigaddset(&set
, SIGALRM
);
419 sigfd
= qemu_signalfd(&set
);
421 fprintf(stderr
, "failed to create signalfd\n");
425 fcntl_setfl(sigfd
, O_NONBLOCK
);
427 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
428 (void *)(intptr_t)sigfd
);
433 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
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
));
472 static void qemu_tcg_init_cpu_signals(void)
474 #ifdef CONFIG_IOTHREAD
476 struct sigaction sigact
;
478 memset(&sigact
, 0, sizeof(sigact
));
479 sigact
.sa_handler
= cpu_signal
;
480 sigaction(SIG_IPI
, &sigact
, NULL
);
483 sigaddset(&set
, SIG_IPI
);
484 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
490 HANDLE qemu_event_handle
;
492 static void dummy_event_handler(void *opaque
)
496 static int qemu_event_init(void)
498 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
499 if (!qemu_event_handle
) {
500 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
503 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
507 static void qemu_event_increment(void)
509 if (!SetEvent(qemu_event_handle
)) {
510 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
516 static int qemu_signal_init(void)
521 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
526 static void qemu_tcg_init_cpu_signals(void)
531 #ifndef CONFIG_IOTHREAD
532 int qemu_init_main_loop(void)
536 ret
= qemu_signal_init();
543 return qemu_event_init();
546 void qemu_main_loop_start(void)
550 void qemu_init_vcpu(void *_env
)
552 CPUState
*env
= _env
;
555 env
->nr_cores
= smp_cores
;
556 env
->nr_threads
= smp_threads
;
559 r
= kvm_init_vcpu(env
);
561 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
564 qemu_kvm_init_cpu_signals(env
);
566 qemu_tcg_init_cpu_signals();
570 int qemu_cpu_is_self(void *env
)
575 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
580 void resume_all_vcpus(void)
584 void pause_all_vcpus(void)
588 void qemu_cpu_kick(void *env
)
592 void qemu_cpu_kick_self(void)
595 assert(cpu_single_env
);
603 void qemu_notify_event(void)
605 CPUState
*env
= cpu_single_env
;
607 qemu_event_increment ();
611 if (next_cpu
&& env
!= next_cpu
) {
617 void qemu_mutex_lock_iothread(void) {}
618 void qemu_mutex_unlock_iothread(void) {}
620 void cpu_stop_current(void)
624 void vm_stop(int reason
)
629 #else /* CONFIG_IOTHREAD */
631 QemuMutex qemu_global_mutex
;
632 static QemuMutex qemu_fair_mutex
;
634 static QemuThread io_thread
;
636 static QemuThread
*tcg_cpu_thread
;
637 static QemuCond
*tcg_halt_cond
;
639 static int qemu_system_ready
;
641 static QemuCond qemu_cpu_cond
;
643 static QemuCond qemu_system_cond
;
644 static QemuCond qemu_pause_cond
;
645 static QemuCond qemu_work_cond
;
647 int qemu_init_main_loop(void)
653 ret
= qemu_signal_init();
658 /* Note eventfd must be drained before signalfd handlers run */
659 ret
= qemu_event_init();
664 qemu_cond_init(&qemu_cpu_cond
);
665 qemu_cond_init(&qemu_system_cond
);
666 qemu_cond_init(&qemu_pause_cond
);
667 qemu_cond_init(&qemu_work_cond
);
668 qemu_mutex_init(&qemu_fair_mutex
);
669 qemu_mutex_init(&qemu_global_mutex
);
670 qemu_mutex_lock(&qemu_global_mutex
);
672 qemu_thread_get_self(&io_thread
);
677 void qemu_main_loop_start(void)
679 qemu_system_ready
= 1;
680 qemu_cond_broadcast(&qemu_system_cond
);
683 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
685 struct qemu_work_item wi
;
687 if (qemu_cpu_is_self(env
)) {
694 if (!env
->queued_work_first
) {
695 env
->queued_work_first
= &wi
;
697 env
->queued_work_last
->next
= &wi
;
699 env
->queued_work_last
= &wi
;
705 CPUState
*self_env
= cpu_single_env
;
707 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
708 cpu_single_env
= self_env
;
712 static void flush_queued_work(CPUState
*env
)
714 struct qemu_work_item
*wi
;
716 if (!env
->queued_work_first
) {
720 while ((wi
= env
->queued_work_first
)) {
721 env
->queued_work_first
= wi
->next
;
725 env
->queued_work_last
= NULL
;
726 qemu_cond_broadcast(&qemu_work_cond
);
729 static void qemu_wait_io_event_common(CPUState
*env
)
734 qemu_cond_signal(&qemu_pause_cond
);
736 flush_queued_work(env
);
737 env
->thread_kicked
= false;
740 static void qemu_tcg_wait_io_event(void)
744 while (all_cpu_threads_idle()) {
745 /* Start accounting real time to the virtual clock if the CPUs
747 qemu_clock_warp(vm_clock
);
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 if (use_icount
&& qemu_next_icount_deadline() <= 0) {
842 qemu_tcg_wait_io_event();
848 static void qemu_cpu_kick_thread(CPUState
*env
)
853 err
= pthread_kill(env
->thread
->thread
, SIG_IPI
);
855 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
859 if (!qemu_cpu_is_self(env
)) {
860 SuspendThread(env
->thread
->thread
);
862 ResumeThread(env
->thread
->thread
);
867 void qemu_cpu_kick(void *_env
)
869 CPUState
*env
= _env
;
871 qemu_cond_broadcast(env
->halt_cond
);
872 if (!env
->thread_kicked
) {
873 qemu_cpu_kick_thread(env
);
874 env
->thread_kicked
= true;
878 void qemu_cpu_kick_self(void)
881 assert(cpu_single_env
);
883 if (!cpu_single_env
->thread_kicked
) {
884 qemu_cpu_kick_thread(cpu_single_env
);
885 cpu_single_env
->thread_kicked
= true;
892 int qemu_cpu_is_self(void *_env
)
894 CPUState
*env
= _env
;
896 return qemu_thread_is_self(env
->thread
);
899 void qemu_mutex_lock_iothread(void)
902 qemu_mutex_lock(&qemu_global_mutex
);
904 qemu_mutex_lock(&qemu_fair_mutex
);
905 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
906 qemu_cpu_kick_thread(first_cpu
);
907 qemu_mutex_lock(&qemu_global_mutex
);
909 qemu_mutex_unlock(&qemu_fair_mutex
);
913 void qemu_mutex_unlock_iothread(void)
915 qemu_mutex_unlock(&qemu_global_mutex
);
918 static int all_vcpus_paused(void)
920 CPUState
*penv
= first_cpu
;
923 if (!penv
->stopped
) {
926 penv
= (CPUState
*)penv
->next_cpu
;
932 void pause_all_vcpus(void)
934 CPUState
*penv
= first_cpu
;
939 penv
= (CPUState
*)penv
->next_cpu
;
942 while (!all_vcpus_paused()) {
943 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
947 penv
= (CPUState
*)penv
->next_cpu
;
952 void resume_all_vcpus(void)
954 CPUState
*penv
= first_cpu
;
960 penv
= (CPUState
*)penv
->next_cpu
;
964 static void qemu_tcg_init_vcpu(void *_env
)
966 CPUState
*env
= _env
;
968 /* share a single thread for all cpus with TCG */
969 if (!tcg_cpu_thread
) {
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_tcg_cpu_thread_fn
, env
);
974 while (env
->created
== 0) {
975 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
977 tcg_cpu_thread
= env
->thread
;
978 tcg_halt_cond
= env
->halt_cond
;
980 env
->thread
= tcg_cpu_thread
;
981 env
->halt_cond
= tcg_halt_cond
;
985 static void qemu_kvm_start_vcpu(CPUState
*env
)
987 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
988 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
989 qemu_cond_init(env
->halt_cond
);
990 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
991 while (env
->created
== 0) {
992 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
996 void qemu_init_vcpu(void *_env
)
998 CPUState
*env
= _env
;
1000 env
->nr_cores
= smp_cores
;
1001 env
->nr_threads
= smp_threads
;
1002 if (kvm_enabled()) {
1003 qemu_kvm_start_vcpu(env
);
1005 qemu_tcg_init_vcpu(env
);
1009 void qemu_notify_event(void)
1011 qemu_event_increment();
1014 void cpu_stop_current(void)
1016 if (cpu_single_env
) {
1017 cpu_single_env
->stop
= 0;
1018 cpu_single_env
->stopped
= 1;
1019 cpu_exit(cpu_single_env
);
1020 qemu_cond_signal(&qemu_pause_cond
);
1024 void vm_stop(int reason
)
1026 if (!qemu_thread_is_self(&io_thread
)) {
1027 qemu_system_vmstop_request(reason
);
1029 * FIXME: should not return to device code in case
1030 * vm_stop() has been requested.
1040 static int tcg_cpu_exec(CPUState
*env
)
1043 #ifdef CONFIG_PROFILER
1047 #ifdef CONFIG_PROFILER
1048 ti
= profile_getclock();
1053 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1054 env
->icount_decr
.u16
.low
= 0;
1055 env
->icount_extra
= 0;
1056 count
= qemu_icount_round(qemu_next_icount_deadline());
1057 qemu_icount
+= count
;
1058 decr
= (count
> 0xffff) ? 0xffff : count
;
1060 env
->icount_decr
.u16
.low
= decr
;
1061 env
->icount_extra
= count
;
1063 ret
= cpu_exec(env
);
1064 #ifdef CONFIG_PROFILER
1065 qemu_time
+= profile_getclock() - ti
;
1068 /* Fold pending instructions back into the
1069 instruction counter, and clear the interrupt flag. */
1070 qemu_icount
-= (env
->icount_decr
.u16
.low
1071 + env
->icount_extra
);
1072 env
->icount_decr
.u32
= 0;
1073 env
->icount_extra
= 0;
1078 bool cpu_exec_all(void)
1082 /* Account partial waits to the vm_clock. */
1083 qemu_clock_warp(vm_clock
);
1085 if (next_cpu
== NULL
) {
1086 next_cpu
= first_cpu
;
1088 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1089 CPUState
*env
= next_cpu
;
1091 qemu_clock_enable(vm_clock
,
1092 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1094 #ifndef CONFIG_IOTHREAD
1095 if (qemu_alarm_pending()) {
1099 if (cpu_can_run(env
)) {
1100 if (kvm_enabled()) {
1101 r
= kvm_cpu_exec(env
);
1102 qemu_kvm_eat_signals(env
);
1104 r
= tcg_cpu_exec(env
);
1106 if (r
== EXCP_DEBUG
) {
1107 cpu_handle_guest_debug(env
);
1110 } else if (env
->stop
|| env
->stopped
) {
1115 return !all_cpu_threads_idle();
1118 void set_numa_modes(void)
1123 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1124 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1125 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1132 void set_cpu_log(const char *optarg
)
1135 const CPULogItem
*item
;
1137 mask
= cpu_str_to_log_mask(optarg
);
1139 printf("Log items (comma separated):\n");
1140 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1141 printf("%-10s %s\n", item
->name
, item
->help
);
1148 void set_cpu_log_filename(const char *optarg
)
1150 cpu_set_log_filename(optarg
);
1153 /* Return the virtual CPU time, based on the instruction counter. */
1154 int64_t cpu_get_icount(void)
1157 CPUState
*env
= cpu_single_env
;;
1159 icount
= qemu_icount
;
1161 if (!can_do_io(env
)) {
1162 fprintf(stderr
, "Bad clock read\n");
1164 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1166 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1169 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1171 /* XXX: implement xxx_cpu_list for targets that still miss it */
1172 #if defined(cpu_list_id)
1173 cpu_list_id(f
, cpu_fprintf
, optarg
);
1174 #elif defined(cpu_list)
1175 cpu_list(f
, cpu_fprintf
); /* deprecated */