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"
38 #include <sys/prctl.h>
42 #define SIG_IPI (SIGRTMIN+4)
44 #define SIG_IPI SIGUSR1
48 #define PR_MCE_KILL 33
51 static CPUState
*next_cpu
;
53 /***********************************************************/
54 void hw_error(const char *fmt
, ...)
60 fprintf(stderr
, "qemu: hardware error: ");
61 vfprintf(stderr
, fmt
, ap
);
62 fprintf(stderr
, "\n");
63 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
64 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
66 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
68 cpu_dump_state(env
, stderr
, fprintf
, 0);
75 void cpu_synchronize_all_states(void)
79 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
80 cpu_synchronize_state(cpu
);
84 void cpu_synchronize_all_post_reset(void)
88 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
89 cpu_synchronize_post_reset(cpu
);
93 void cpu_synchronize_all_post_init(void)
97 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
98 cpu_synchronize_post_init(cpu
);
102 int cpu_is_stopped(CPUState
*env
)
104 return !vm_running
|| env
->stopped
;
107 static void do_vm_stop(int reason
)
113 vm_state_notify(0, reason
);
116 monitor_protocol_event(QEVENT_STOP
, NULL
);
120 static int cpu_can_run(CPUState
*env
)
124 if (env
->stopped
|| !vm_running
)
129 static int cpu_has_work(CPUState
*env
)
133 if (env
->queued_work_first
)
135 if (env
->stopped
|| !vm_running
)
139 if (qemu_cpu_has_work(env
))
144 static int any_cpu_has_work(void)
148 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
149 if (cpu_has_work(env
))
154 static void cpu_debug_handler(CPUState
*env
)
156 gdb_set_stop_cpu(env
);
157 debug_requested
= EXCP_DEBUG
;
162 static int io_thread_fd
= -1;
164 static void qemu_event_increment(void)
166 /* Write 8 bytes to be compatible with eventfd. */
167 static const uint64_t val
= 1;
170 if (io_thread_fd
== -1)
174 ret
= write(io_thread_fd
, &val
, sizeof(val
));
175 } while (ret
< 0 && errno
== EINTR
);
177 /* EAGAIN is fine, a read must be pending. */
178 if (ret
< 0 && errno
!= EAGAIN
) {
179 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
185 static void qemu_event_read(void *opaque
)
187 int fd
= (unsigned long)opaque
;
191 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
193 len
= read(fd
, buffer
, sizeof(buffer
));
194 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
197 static int qemu_event_init(void)
202 err
= qemu_eventfd(fds
);
206 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
210 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
214 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
215 (void *)(unsigned long)fds
[0]);
217 io_thread_fd
= fds
[1];
226 HANDLE qemu_event_handle
;
228 static void dummy_event_handler(void *opaque
)
232 static int qemu_event_init(void)
234 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
235 if (!qemu_event_handle
) {
236 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
239 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
243 static void qemu_event_increment(void)
245 if (!SetEvent(qemu_event_handle
)) {
246 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
253 #ifndef CONFIG_IOTHREAD
254 int qemu_init_main_loop(void)
256 cpu_set_debug_excp_handler(cpu_debug_handler
);
258 return qemu_event_init();
261 void qemu_main_loop_start(void)
265 void qemu_init_vcpu(void *_env
)
267 CPUState
*env
= _env
;
270 env
->nr_cores
= smp_cores
;
271 env
->nr_threads
= smp_threads
;
274 r
= kvm_init_vcpu(env
);
276 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
282 int qemu_cpu_self(void *env
)
287 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
292 void resume_all_vcpus(void)
296 void pause_all_vcpus(void)
300 void qemu_cpu_kick(void *env
)
305 void qemu_notify_event(void)
307 CPUState
*env
= cpu_single_env
;
309 qemu_event_increment ();
313 if (next_cpu
&& env
!= next_cpu
) {
319 void qemu_mutex_lock_iothread(void) {}
320 void qemu_mutex_unlock_iothread(void) {}
322 void cpu_stop_current(void)
326 void vm_stop(int reason
)
331 #else /* CONFIG_IOTHREAD */
333 #include "qemu-thread.h"
335 QemuMutex qemu_global_mutex
;
336 static QemuMutex qemu_fair_mutex
;
338 static QemuThread io_thread
;
340 static QemuThread
*tcg_cpu_thread
;
341 static QemuCond
*tcg_halt_cond
;
343 static int qemu_system_ready
;
345 static QemuCond qemu_cpu_cond
;
347 static QemuCond qemu_system_cond
;
348 static QemuCond qemu_pause_cond
;
349 static QemuCond qemu_work_cond
;
351 static void tcg_init_ipi(void);
352 static void kvm_init_ipi(CPUState
*env
);
353 static sigset_t
block_io_signals(void);
355 /* If we have signalfd, we mask out the signals we want to handle and then
356 * use signalfd to listen for them. We rely on whatever the current signal
357 * handler is to dispatch the signals when we receive them.
359 static void sigfd_handler(void *opaque
)
361 int fd
= (unsigned long) opaque
;
362 struct qemu_signalfd_siginfo info
;
363 struct sigaction action
;
368 len
= read(fd
, &info
, sizeof(info
));
369 } while (len
== -1 && errno
== EINTR
);
371 if (len
== -1 && errno
== EAGAIN
) {
375 if (len
!= sizeof(info
)) {
376 printf("read from sigfd returned %zd: %m\n", len
);
380 sigaction(info
.ssi_signo
, NULL
, &action
);
381 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
382 action
.sa_sigaction(info
.ssi_signo
,
383 (siginfo_t
*)&info
, NULL
);
384 } else if (action
.sa_handler
) {
385 action
.sa_handler(info
.ssi_signo
);
390 static int qemu_signalfd_init(sigset_t mask
)
394 sigfd
= qemu_signalfd(&mask
);
396 fprintf(stderr
, "failed to create signalfd\n");
400 fcntl_setfl(sigfd
, O_NONBLOCK
);
402 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
403 (void *)(unsigned long) sigfd
);
408 int qemu_init_main_loop(void)
411 sigset_t blocked_signals
;
413 cpu_set_debug_excp_handler(cpu_debug_handler
);
415 blocked_signals
= block_io_signals();
417 ret
= qemu_signalfd_init(blocked_signals
);
421 /* Note eventfd must be drained before signalfd handlers run */
422 ret
= qemu_event_init();
426 qemu_cond_init(&qemu_pause_cond
);
427 qemu_cond_init(&qemu_system_cond
);
428 qemu_mutex_init(&qemu_fair_mutex
);
429 qemu_mutex_init(&qemu_global_mutex
);
430 qemu_mutex_lock(&qemu_global_mutex
);
432 qemu_thread_self(&io_thread
);
437 void qemu_main_loop_start(void)
439 qemu_system_ready
= 1;
440 qemu_cond_broadcast(&qemu_system_cond
);
443 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
445 struct qemu_work_item wi
;
447 if (qemu_cpu_self(env
)) {
454 if (!env
->queued_work_first
)
455 env
->queued_work_first
= &wi
;
457 env
->queued_work_last
->next
= &wi
;
458 env
->queued_work_last
= &wi
;
464 CPUState
*self_env
= cpu_single_env
;
466 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
467 cpu_single_env
= self_env
;
471 static void flush_queued_work(CPUState
*env
)
473 struct qemu_work_item
*wi
;
475 if (!env
->queued_work_first
)
478 while ((wi
= env
->queued_work_first
)) {
479 env
->queued_work_first
= wi
->next
;
483 env
->queued_work_last
= NULL
;
484 qemu_cond_broadcast(&qemu_work_cond
);
487 static void qemu_wait_io_event_common(CPUState
*env
)
492 qemu_cond_signal(&qemu_pause_cond
);
494 flush_queued_work(env
);
495 env
->thread_kicked
= false;
498 static void qemu_tcg_wait_io_event(void)
502 while (!any_cpu_has_work())
503 qemu_cond_timedwait(tcg_halt_cond
, &qemu_global_mutex
, 1000);
505 qemu_mutex_unlock(&qemu_global_mutex
);
508 * Users of qemu_global_mutex can be starved, having no chance
509 * to acquire it since this path will get to it first.
510 * So use another lock to provide fairness.
512 qemu_mutex_lock(&qemu_fair_mutex
);
513 qemu_mutex_unlock(&qemu_fair_mutex
);
515 qemu_mutex_lock(&qemu_global_mutex
);
517 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
518 qemu_wait_io_event_common(env
);
522 static void sigbus_reraise(void)
525 struct sigaction action
;
527 memset(&action
, 0, sizeof(action
));
528 action
.sa_handler
= SIG_DFL
;
529 if (!sigaction(SIGBUS
, &action
, NULL
)) {
532 sigaddset(&set
, SIGBUS
);
533 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
535 perror("Failed to re-raise SIGBUS!\n");
539 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
542 #if defined(TARGET_I386)
543 if (kvm_on_sigbus(siginfo
->ssi_code
, (void *)(intptr_t)siginfo
->ssi_addr
))
548 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
556 ts
.tv_sec
= timeout
/ 1000;
557 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
559 sigemptyset(&waitset
);
560 sigaddset(&waitset
, SIG_IPI
);
561 sigaddset(&waitset
, SIGBUS
);
564 qemu_mutex_unlock(&qemu_global_mutex
);
566 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
569 qemu_mutex_lock(&qemu_global_mutex
);
571 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
572 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
579 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
))
587 r
= sigpending(&chkset
);
589 fprintf(stderr
, "sigpending: %s\n", strerror(e
));
592 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
595 static void qemu_kvm_wait_io_event(CPUState
*env
)
597 while (!cpu_has_work(env
))
598 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
600 qemu_kvm_eat_signal(env
, 0);
601 qemu_wait_io_event_common(env
);
604 static int qemu_cpu_exec(CPUState
*env
);
606 static void *kvm_cpu_thread_fn(void *arg
)
611 qemu_mutex_lock(&qemu_global_mutex
);
612 qemu_thread_self(env
->thread
);
614 r
= kvm_init_vcpu(env
);
616 fprintf(stderr
, "kvm_init_vcpu failed: %s\n", strerror(-r
));
622 /* signal CPU creation */
624 qemu_cond_signal(&qemu_cpu_cond
);
626 /* and wait for machine initialization */
627 while (!qemu_system_ready
)
628 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
631 if (cpu_can_run(env
))
633 qemu_kvm_wait_io_event(env
);
639 static void *tcg_cpu_thread_fn(void *arg
)
644 qemu_thread_self(env
->thread
);
646 /* signal CPU creation */
647 qemu_mutex_lock(&qemu_global_mutex
);
648 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
650 qemu_cond_signal(&qemu_cpu_cond
);
652 /* and wait for machine initialization */
653 while (!qemu_system_ready
)
654 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
658 qemu_tcg_wait_io_event();
664 void qemu_cpu_kick(void *_env
)
666 CPUState
*env
= _env
;
667 qemu_cond_broadcast(env
->halt_cond
);
668 if (!env
->thread_kicked
) {
669 qemu_thread_signal(env
->thread
, SIG_IPI
);
670 env
->thread_kicked
= true;
674 int qemu_cpu_self(void *_env
)
676 CPUState
*env
= _env
;
679 qemu_thread_self(&this);
681 return qemu_thread_equal(&this, env
->thread
);
684 static void cpu_signal(int sig
)
687 cpu_exit(cpu_single_env
);
691 static void tcg_init_ipi(void)
694 struct sigaction sigact
;
696 memset(&sigact
, 0, sizeof(sigact
));
697 sigact
.sa_handler
= cpu_signal
;
698 sigaction(SIG_IPI
, &sigact
, NULL
);
701 sigaddset(&set
, SIG_IPI
);
702 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
705 static void dummy_signal(int sig
)
709 static void kvm_init_ipi(CPUState
*env
)
713 struct sigaction sigact
;
715 memset(&sigact
, 0, sizeof(sigact
));
716 sigact
.sa_handler
= dummy_signal
;
717 sigaction(SIG_IPI
, &sigact
, NULL
);
719 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
720 sigdelset(&set
, SIG_IPI
);
721 sigdelset(&set
, SIGBUS
);
722 r
= kvm_set_signal_mask(env
, &set
);
724 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
729 static sigset_t
block_io_signals(void)
732 struct sigaction action
;
734 /* SIGUSR2 used by posix-aio-compat.c */
736 sigaddset(&set
, SIGUSR2
);
737 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
740 sigaddset(&set
, SIGIO
);
741 sigaddset(&set
, SIGALRM
);
742 sigaddset(&set
, SIG_IPI
);
743 sigaddset(&set
, SIGBUS
);
744 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
746 memset(&action
, 0, sizeof(action
));
747 action
.sa_flags
= SA_SIGINFO
;
748 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
749 sigaction(SIGBUS
, &action
, NULL
);
750 prctl(PR_MCE_KILL
, 1, 1, 0, 0);
755 void qemu_mutex_lock_iothread(void)
758 qemu_mutex_lock(&qemu_global_mutex
);
760 qemu_mutex_lock(&qemu_fair_mutex
);
761 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
762 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
763 qemu_mutex_lock(&qemu_global_mutex
);
765 qemu_mutex_unlock(&qemu_fair_mutex
);
769 void qemu_mutex_unlock_iothread(void)
771 qemu_mutex_unlock(&qemu_global_mutex
);
774 static int all_vcpus_paused(void)
776 CPUState
*penv
= first_cpu
;
781 penv
= (CPUState
*)penv
->next_cpu
;
787 void pause_all_vcpus(void)
789 CPUState
*penv
= first_cpu
;
794 penv
= (CPUState
*)penv
->next_cpu
;
797 while (!all_vcpus_paused()) {
798 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
802 penv
= (CPUState
*)penv
->next_cpu
;
807 void resume_all_vcpus(void)
809 CPUState
*penv
= first_cpu
;
815 penv
= (CPUState
*)penv
->next_cpu
;
819 static void tcg_init_vcpu(void *_env
)
821 CPUState
*env
= _env
;
822 /* share a single thread for all cpus with TCG */
823 if (!tcg_cpu_thread
) {
824 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
825 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
826 qemu_cond_init(env
->halt_cond
);
827 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
828 while (env
->created
== 0)
829 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
830 tcg_cpu_thread
= env
->thread
;
831 tcg_halt_cond
= env
->halt_cond
;
833 env
->thread
= tcg_cpu_thread
;
834 env
->halt_cond
= tcg_halt_cond
;
838 static void kvm_start_vcpu(CPUState
*env
)
840 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
841 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
842 qemu_cond_init(env
->halt_cond
);
843 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
844 while (env
->created
== 0)
845 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
848 void qemu_init_vcpu(void *_env
)
850 CPUState
*env
= _env
;
852 env
->nr_cores
= smp_cores
;
853 env
->nr_threads
= smp_threads
;
860 void qemu_notify_event(void)
862 qemu_event_increment();
865 static void qemu_system_vmstop_request(int reason
)
867 vmstop_requested
= reason
;
871 void cpu_stop_current(void)
873 if (cpu_single_env
) {
874 cpu_single_env
->stopped
= 1;
875 cpu_exit(cpu_single_env
);
879 void vm_stop(int reason
)
882 qemu_thread_self(&me
);
884 if (!qemu_thread_equal(&me
, &io_thread
)) {
885 qemu_system_vmstop_request(reason
);
887 * FIXME: should not return to device code in case
888 * vm_stop() has been requested.
898 static int qemu_cpu_exec(CPUState
*env
)
901 #ifdef CONFIG_PROFILER
905 #ifdef CONFIG_PROFILER
906 ti
= profile_getclock();
911 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
912 env
->icount_decr
.u16
.low
= 0;
913 env
->icount_extra
= 0;
914 count
= qemu_icount_round (qemu_next_deadline());
915 qemu_icount
+= count
;
916 decr
= (count
> 0xffff) ? 0xffff : count
;
918 env
->icount_decr
.u16
.low
= decr
;
919 env
->icount_extra
= count
;
922 #ifdef CONFIG_PROFILER
923 qemu_time
+= profile_getclock() - ti
;
926 /* Fold pending instructions back into the
927 instruction counter, and clear the interrupt flag. */
928 qemu_icount
-= (env
->icount_decr
.u16
.low
929 + env
->icount_extra
);
930 env
->icount_decr
.u32
= 0;
931 env
->icount_extra
= 0;
936 bool cpu_exec_all(void)
938 if (next_cpu
== NULL
)
939 next_cpu
= first_cpu
;
940 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
941 CPUState
*env
= next_cpu
;
943 qemu_clock_enable(vm_clock
,
944 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
946 if (qemu_alarm_pending())
948 if (cpu_can_run(env
)) {
949 if (qemu_cpu_exec(env
) == EXCP_DEBUG
) {
952 } else if (env
->stop
) {
957 return any_cpu_has_work();
960 void set_numa_modes(void)
965 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
966 for (i
= 0; i
< nb_numa_nodes
; i
++) {
967 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
974 void set_cpu_log(const char *optarg
)
977 const CPULogItem
*item
;
979 mask
= cpu_str_to_log_mask(optarg
);
981 printf("Log items (comma separated):\n");
982 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
983 printf("%-10s %s\n", item
->name
, item
->help
);
990 /* Return the virtual CPU time, based on the instruction counter. */
991 int64_t cpu_get_icount(void)
994 CPUState
*env
= cpu_single_env
;;
996 icount
= qemu_icount
;
998 if (!can_do_io(env
)) {
999 fprintf(stderr
, "Bad clock read\n");
1001 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
1003 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
1006 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
1008 /* XXX: implement xxx_cpu_list for targets that still miss it */
1009 #if defined(cpu_list_id)
1010 cpu_list_id(f
, cpu_fprintf
, optarg
);
1011 #elif defined(cpu_list)
1012 cpu_list(f
, cpu_fprintf
); /* deprecated */