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 void cpu_handle_guest_debug(CPUState
*env
)
171 gdb_set_stop_cpu(env
);
172 qemu_system_debug_request();
173 #ifdef CONFIG_IOTHREAD
178 #ifdef CONFIG_IOTHREAD
179 static void cpu_signal(int sig
)
181 if (cpu_single_env
) {
182 cpu_exit(cpu_single_env
);
189 static void sigbus_reraise(void)
192 struct sigaction action
;
194 memset(&action
, 0, sizeof(action
));
195 action
.sa_handler
= SIG_DFL
;
196 if (!sigaction(SIGBUS
, &action
, NULL
)) {
199 sigaddset(&set
, SIGBUS
);
200 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
202 perror("Failed to re-raise SIGBUS!\n");
206 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
209 if (kvm_on_sigbus(siginfo
->ssi_code
,
210 (void *)(intptr_t)siginfo
->ssi_addr
)) {
215 static void qemu_init_sigbus(void)
217 struct sigaction action
;
219 memset(&action
, 0, sizeof(action
));
220 action
.sa_flags
= SA_SIGINFO
;
221 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
222 sigaction(SIGBUS
, &action
, NULL
);
224 prctl(PR_MCE_KILL
, PR_MCE_KILL_SET
, PR_MCE_KILL_EARLY
, 0, 0);
227 static void qemu_kvm_eat_signals(CPUState
*env
)
229 struct timespec ts
= { 0, 0 };
235 sigemptyset(&waitset
);
236 sigaddset(&waitset
, SIG_IPI
);
237 sigaddset(&waitset
, SIGBUS
);
240 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
241 if (r
== -1 && !(errno
== EAGAIN
|| errno
== EINTR
)) {
242 perror("sigtimedwait");
248 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
)) {
256 r
= sigpending(&chkset
);
258 perror("sigpending");
261 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
263 #ifndef CONFIG_IOTHREAD
264 if (sigismember(&chkset
, SIGIO
) || sigismember(&chkset
, SIGALRM
)) {
270 #else /* !CONFIG_LINUX */
272 static void qemu_init_sigbus(void)
276 static void qemu_kvm_eat_signals(CPUState
*env
)
279 #endif /* !CONFIG_LINUX */
282 static int io_thread_fd
= -1;
284 static void qemu_event_increment(void)
286 /* Write 8 bytes to be compatible with eventfd. */
287 static const uint64_t val
= 1;
290 if (io_thread_fd
== -1) {
294 ret
= write(io_thread_fd
, &val
, sizeof(val
));
295 } while (ret
< 0 && errno
== EINTR
);
297 /* EAGAIN is fine, a read must be pending. */
298 if (ret
< 0 && errno
!= EAGAIN
) {
299 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
305 static void qemu_event_read(void *opaque
)
307 int fd
= (unsigned long)opaque
;
311 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
313 len
= read(fd
, buffer
, sizeof(buffer
));
314 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
317 static int qemu_event_init(void)
322 err
= qemu_eventfd(fds
);
326 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
330 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
334 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
335 (void *)(unsigned long)fds
[0]);
337 io_thread_fd
= fds
[1];
346 static void dummy_signal(int sig
)
350 /* If we have signalfd, we mask out the signals we want to handle and then
351 * use signalfd to listen for them. We rely on whatever the current signal
352 * handler is to dispatch the signals when we receive them.
354 static void sigfd_handler(void *opaque
)
356 int fd
= (unsigned long) opaque
;
357 struct qemu_signalfd_siginfo info
;
358 struct sigaction action
;
363 len
= read(fd
, &info
, sizeof(info
));
364 } while (len
== -1 && errno
== EINTR
);
366 if (len
== -1 && errno
== EAGAIN
) {
370 if (len
!= sizeof(info
)) {
371 printf("read from sigfd returned %zd: %m\n", len
);
375 sigaction(info
.ssi_signo
, NULL
, &action
);
376 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
377 action
.sa_sigaction(info
.ssi_signo
,
378 (siginfo_t
*)&info
, NULL
);
379 } else if (action
.sa_handler
) {
380 action
.sa_handler(info
.ssi_signo
);
385 static int qemu_signal_init(void)
390 #ifdef CONFIG_IOTHREAD
391 /* SIGUSR2 used by posix-aio-compat.c */
393 sigaddset(&set
, SIGUSR2
);
394 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
397 sigaddset(&set
, SIGIO
);
398 sigaddset(&set
, SIGALRM
);
399 sigaddset(&set
, SIG_IPI
);
400 sigaddset(&set
, SIGBUS
);
401 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
404 sigaddset(&set
, SIGBUS
);
407 * We need to process timer signals synchronously to avoid a race
408 * between exit_request check and KVM vcpu entry.
410 sigaddset(&set
, SIGIO
);
411 sigaddset(&set
, SIGALRM
);
415 sigfd
= qemu_signalfd(&set
);
417 fprintf(stderr
, "failed to create signalfd\n");
421 fcntl_setfl(sigfd
, O_NONBLOCK
);
423 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
424 (void *)(unsigned long) sigfd
);
429 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
433 struct sigaction sigact
;
435 memset(&sigact
, 0, sizeof(sigact
));
436 sigact
.sa_handler
= dummy_signal
;
437 sigaction(SIG_IPI
, &sigact
, NULL
);
439 #ifdef CONFIG_IOTHREAD
440 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
441 sigdelset(&set
, SIG_IPI
);
442 sigdelset(&set
, SIGBUS
);
443 r
= kvm_set_signal_mask(env
, &set
);
445 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
450 sigaddset(&set
, SIG_IPI
);
451 sigaddset(&set
, SIGIO
);
452 sigaddset(&set
, SIGALRM
);
453 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
455 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
456 sigdelset(&set
, SIGIO
);
457 sigdelset(&set
, SIGALRM
);
459 sigdelset(&set
, SIG_IPI
);
460 sigdelset(&set
, SIGBUS
);
461 r
= kvm_set_signal_mask(env
, &set
);
463 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(-r
));
468 static void qemu_tcg_init_cpu_signals(void)
470 #ifdef CONFIG_IOTHREAD
472 struct sigaction sigact
;
474 memset(&sigact
, 0, sizeof(sigact
));
475 sigact
.sa_handler
= cpu_signal
;
476 sigaction(SIG_IPI
, &sigact
, NULL
);
479 sigaddset(&set
, SIG_IPI
);
480 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
486 HANDLE qemu_event_handle
;
488 static void dummy_event_handler(void *opaque
)
492 static int qemu_event_init(void)
494 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
495 if (!qemu_event_handle
) {
496 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
499 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
503 static void qemu_event_increment(void)
505 if (!SetEvent(qemu_event_handle
)) {
506 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
512 static int qemu_signal_init(void)
517 static void qemu_kvm_init_cpu_signals(CPUState
*env
)
522 static void qemu_tcg_init_cpu_signals(void)
527 #ifndef CONFIG_IOTHREAD
528 int qemu_init_main_loop(void)
532 ret
= qemu_signal_init();
539 return qemu_event_init();
542 void qemu_main_loop_start(void)
546 void qemu_init_vcpu(void *_env
)
548 CPUState
*env
= _env
;
551 env
->nr_cores
= smp_cores
;
552 env
->nr_threads
= smp_threads
;
555 r
= kvm_init_vcpu(env
);
557 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
560 qemu_kvm_init_cpu_signals(env
);
562 qemu_tcg_init_cpu_signals();
566 int qemu_cpu_is_self(void *env
)
571 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
576 void resume_all_vcpus(void)
580 void pause_all_vcpus(void)
584 void qemu_cpu_kick(void *env
)
588 void qemu_cpu_kick_self(void)
591 assert(cpu_single_env
);
599 void qemu_notify_event(void)
601 CPUState
*env
= cpu_single_env
;
603 qemu_event_increment ();
607 if (next_cpu
&& env
!= next_cpu
) {
613 void qemu_mutex_lock_iothread(void) {}
614 void qemu_mutex_unlock_iothread(void) {}
616 void cpu_stop_current(void)
620 void vm_stop(int reason
)
625 #else /* CONFIG_IOTHREAD */
627 QemuMutex qemu_global_mutex
;
628 static QemuMutex qemu_fair_mutex
;
630 static QemuThread io_thread
;
632 static QemuThread
*tcg_cpu_thread
;
633 static QemuCond
*tcg_halt_cond
;
635 static int qemu_system_ready
;
637 static QemuCond qemu_cpu_cond
;
639 static QemuCond qemu_system_cond
;
640 static QemuCond qemu_pause_cond
;
641 static QemuCond qemu_work_cond
;
643 int qemu_init_main_loop(void)
649 ret
= qemu_signal_init();
654 /* Note eventfd must be drained before signalfd handlers run */
655 ret
= qemu_event_init();
660 qemu_cond_init(&qemu_cpu_cond
);
661 qemu_cond_init(&qemu_system_cond
);
662 qemu_cond_init(&qemu_pause_cond
);
663 qemu_cond_init(&qemu_work_cond
);
664 qemu_mutex_init(&qemu_fair_mutex
);
665 qemu_mutex_init(&qemu_global_mutex
);
666 qemu_mutex_lock(&qemu_global_mutex
);
668 qemu_thread_get_self(&io_thread
);
673 void qemu_main_loop_start(void)
675 qemu_system_ready
= 1;
676 qemu_cond_broadcast(&qemu_system_cond
);
679 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
681 struct qemu_work_item wi
;
683 if (qemu_cpu_is_self(env
)) {
690 if (!env
->queued_work_first
) {
691 env
->queued_work_first
= &wi
;
693 env
->queued_work_last
->next
= &wi
;
695 env
->queued_work_last
= &wi
;
701 CPUState
*self_env
= cpu_single_env
;
703 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
704 cpu_single_env
= self_env
;
708 static void flush_queued_work(CPUState
*env
)
710 struct qemu_work_item
*wi
;
712 if (!env
->queued_work_first
) {
716 while ((wi
= env
->queued_work_first
)) {
717 env
->queued_work_first
= wi
->next
;
721 env
->queued_work_last
= NULL
;
722 qemu_cond_broadcast(&qemu_work_cond
);
725 static void qemu_wait_io_event_common(CPUState
*env
)
730 qemu_cond_signal(&qemu_pause_cond
);
732 flush_queued_work(env
);
733 env
->thread_kicked
= false;
736 static void qemu_tcg_wait_io_event(void)
740 while (all_cpu_threads_idle()) {
741 qemu_cond_wait(tcg_halt_cond
, &qemu_global_mutex
);
744 qemu_mutex_unlock(&qemu_global_mutex
);
747 * Users of qemu_global_mutex can be starved, having no chance
748 * to acquire it since this path will get to it first.
749 * So use another lock to provide fairness.
751 qemu_mutex_lock(&qemu_fair_mutex
);
752 qemu_mutex_unlock(&qemu_fair_mutex
);
754 qemu_mutex_lock(&qemu_global_mutex
);
756 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
757 qemu_wait_io_event_common(env
);
761 static void qemu_kvm_wait_io_event(CPUState
*env
)
763 while (cpu_thread_is_idle(env
)) {
764 qemu_cond_wait(env
->halt_cond
, &qemu_global_mutex
);
767 qemu_kvm_eat_signals(env
);
768 qemu_wait_io_event_common(env
);
771 static void *qemu_kvm_cpu_thread_fn(void *arg
)
776 qemu_mutex_lock(&qemu_global_mutex
);
777 qemu_thread_get_self(env
->thread
);
779 r
= kvm_init_vcpu(env
);
781 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
785 qemu_kvm_init_cpu_signals(env
);
787 /* signal CPU creation */
789 qemu_cond_signal(&qemu_cpu_cond
);
791 /* and wait for machine initialization */
792 while (!qemu_system_ready
) {
793 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
797 if (cpu_can_run(env
)) {
798 r
= kvm_cpu_exec(env
);
799 if (r
== EXCP_DEBUG
) {
800 cpu_handle_guest_debug(env
);
803 qemu_kvm_wait_io_event(env
);
809 static void *qemu_tcg_cpu_thread_fn(void *arg
)
813 qemu_tcg_init_cpu_signals();
814 qemu_thread_get_self(env
->thread
);
816 /* signal CPU creation */
817 qemu_mutex_lock(&qemu_global_mutex
);
818 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
821 qemu_cond_signal(&qemu_cpu_cond
);
823 /* and wait for machine initialization */
824 while (!qemu_system_ready
) {
825 qemu_cond_wait(&qemu_system_cond
, &qemu_global_mutex
);
830 qemu_tcg_wait_io_event();
836 static void qemu_cpu_kick_thread(CPUState
*env
)
841 err
= pthread_kill(env
->thread
->thread
, SIG_IPI
);
843 fprintf(stderr
, "qemu:%s: %s", __func__
, strerror(err
));
847 if (!qemu_cpu_is_self(env
)) {
848 SuspendThread(env
->thread
->thread
);
850 ResumeThread(env
->thread
->thread
);
855 void qemu_cpu_kick(void *_env
)
857 CPUState
*env
= _env
;
859 qemu_cond_broadcast(env
->halt_cond
);
860 if (!env
->thread_kicked
) {
861 qemu_cpu_kick_thread(env
);
862 env
->thread_kicked
= true;
866 void qemu_cpu_kick_self(void)
869 assert(cpu_single_env
);
871 if (!cpu_single_env
->thread_kicked
) {
872 qemu_cpu_kick_thread(cpu_single_env
);
873 cpu_single_env
->thread_kicked
= true;
880 int qemu_cpu_is_self(void *_env
)
882 CPUState
*env
= _env
;
884 return qemu_thread_is_self(env
->thread
);
887 void qemu_mutex_lock_iothread(void)
890 qemu_mutex_lock(&qemu_global_mutex
);
892 qemu_mutex_lock(&qemu_fair_mutex
);
893 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
894 qemu_cpu_kick_thread(first_cpu
);
895 qemu_mutex_lock(&qemu_global_mutex
);
897 qemu_mutex_unlock(&qemu_fair_mutex
);
901 void qemu_mutex_unlock_iothread(void)
903 qemu_mutex_unlock(&qemu_global_mutex
);
906 static int all_vcpus_paused(void)
908 CPUState
*penv
= first_cpu
;
911 if (!penv
->stopped
) {
914 penv
= (CPUState
*)penv
->next_cpu
;
920 void pause_all_vcpus(void)
922 CPUState
*penv
= first_cpu
;
927 penv
= (CPUState
*)penv
->next_cpu
;
930 while (!all_vcpus_paused()) {
931 qemu_cond_wait(&qemu_pause_cond
, &qemu_global_mutex
);
935 penv
= (CPUState
*)penv
->next_cpu
;
940 void resume_all_vcpus(void)
942 CPUState
*penv
= first_cpu
;
948 penv
= (CPUState
*)penv
->next_cpu
;
952 static void qemu_tcg_init_vcpu(void *_env
)
954 CPUState
*env
= _env
;
956 /* share a single thread for all cpus with TCG */
957 if (!tcg_cpu_thread
) {
958 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
959 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
960 qemu_cond_init(env
->halt_cond
);
961 qemu_thread_create(env
->thread
, qemu_tcg_cpu_thread_fn
, env
);
962 while (env
->created
== 0) {
963 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
965 tcg_cpu_thread
= env
->thread
;
966 tcg_halt_cond
= env
->halt_cond
;
968 env
->thread
= tcg_cpu_thread
;
969 env
->halt_cond
= tcg_halt_cond
;
973 static void qemu_kvm_start_vcpu(CPUState
*env
)
975 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
976 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
977 qemu_cond_init(env
->halt_cond
);
978 qemu_thread_create(env
->thread
, qemu_kvm_cpu_thread_fn
, env
);
979 while (env
->created
== 0) {
980 qemu_cond_wait(&qemu_cpu_cond
, &qemu_global_mutex
);
984 void qemu_init_vcpu(void *_env
)
986 CPUState
*env
= _env
;
988 env
->nr_cores
= smp_cores
;
989 env
->nr_threads
= smp_threads
;
991 qemu_kvm_start_vcpu(env
);
993 qemu_tcg_init_vcpu(env
);
997 void qemu_notify_event(void)
999 qemu_event_increment();
1002 void cpu_stop_current(void)
1004 if (cpu_single_env
) {
1005 cpu_single_env
->stop
= 0;
1006 cpu_single_env
->stopped
= 1;
1007 cpu_exit(cpu_single_env
);
1008 qemu_cond_signal(&qemu_pause_cond
);
1012 void vm_stop(int reason
)
1014 if (!qemu_thread_is_self(&io_thread
)) {
1015 qemu_system_vmstop_request(reason
);
1017 * FIXME: should not return to device code in case
1018 * vm_stop() has been requested.
1028 static int tcg_cpu_exec(CPUState
*env
)
1031 #ifdef CONFIG_PROFILER
1035 #ifdef CONFIG_PROFILER
1036 ti
= profile_getclock();
1041 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1042 env
->icount_decr
.u16
.low
= 0;
1043 env
->icount_extra
= 0;
1044 count
= qemu_icount_round (qemu_next_deadline());
1045 qemu_icount
+= count
;
1046 decr
= (count
> 0xffff) ? 0xffff : count
;
1048 env
->icount_decr
.u16
.low
= decr
;
1049 env
->icount_extra
= count
;
1051 ret
= cpu_exec(env
);
1052 #ifdef CONFIG_PROFILER
1053 qemu_time
+= profile_getclock() - ti
;
1056 /* Fold pending instructions back into the
1057 instruction counter, and clear the interrupt flag. */
1058 qemu_icount
-= (env
->icount_decr
.u16
.low
1059 + env
->icount_extra
);
1060 env
->icount_decr
.u32
= 0;
1061 env
->icount_extra
= 0;
1066 bool cpu_exec_all(void)
1070 if (next_cpu
== NULL
) {
1071 next_cpu
= first_cpu
;
1073 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
1074 CPUState
*env
= next_cpu
;
1076 qemu_clock_enable(vm_clock
,
1077 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
1079 #ifndef CONFIG_IOTHREAD
1080 if (qemu_alarm_pending()) {
1084 if (cpu_can_run(env
)) {
1085 if (kvm_enabled()) {
1086 r
= kvm_cpu_exec(env
);
1087 qemu_kvm_eat_signals(env
);
1089 r
= tcg_cpu_exec(env
);
1091 if (r
== EXCP_DEBUG
) {
1092 cpu_handle_guest_debug(env
);
1095 } else if (env
->stop
|| env
->stopped
) {
1100 return !all_cpu_threads_idle();
1103 void set_numa_modes(void)
1108 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1109 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1110 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
1117 void set_cpu_log(const char *optarg
)
1120 const CPULogItem
*item
;
1122 mask
= cpu_str_to_log_mask(optarg
);
1124 printf("Log items (comma separated):\n");
1125 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1126 printf("%-10s %s\n", item
->name
, item
->help
);
1133 /* Return the virtual CPU time, based on the instruction counter. */
1134 int64_t cpu_get_icount(void)
1137 CPUState
*env
= cpu_single_env
;;
1139 icount
= qemu_icount
;
1141 if (!can_do_io(env
)) {
1142 fprintf(stderr
, "Bad clock read\n");
1144 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1146 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1149 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1151 /* XXX: implement xxx_cpu_list for targets that still miss it */
1152 #if defined(cpu_list_id)
1153 cpu_list_id(f
, cpu_fprintf
, optarg
);
1154 #elif defined(cpu_list)
1155 cpu_list(f
, cpu_fprintf
); /* deprecated */