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
;
269 env
->nr_cores
= smp_cores
;
270 env
->nr_threads
= smp_threads
;
276 int qemu_cpu_self(void *env
)
281 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
286 void resume_all_vcpus(void)
290 void pause_all_vcpus(void)
294 void qemu_cpu_kick(void *env
)
299 void qemu_notify_event(void)
301 CPUState
*env
= cpu_single_env
;
303 qemu_event_increment ();
307 if (next_cpu
&& env
!= next_cpu
) {
312 void qemu_mutex_lock_iothread(void) {}
313 void qemu_mutex_unlock_iothread(void) {}
315 void cpu_stop_current(void)
319 void vm_stop(int reason
)
324 #else /* CONFIG_IOTHREAD */
326 #include "qemu-thread.h"
328 QemuMutex qemu_global_mutex
;
329 static QemuMutex qemu_fair_mutex
;
331 static QemuThread io_thread
;
333 static QemuThread
*tcg_cpu_thread
;
334 static QemuCond
*tcg_halt_cond
;
336 static int qemu_system_ready
;
338 static QemuCond qemu_cpu_cond
;
340 static QemuCond qemu_system_cond
;
341 static QemuCond qemu_pause_cond
;
342 static QemuCond qemu_work_cond
;
344 static void tcg_init_ipi(void);
345 static void kvm_init_ipi(CPUState
*env
);
346 static sigset_t
block_io_signals(void);
348 /* If we have signalfd, we mask out the signals we want to handle and then
349 * use signalfd to listen for them. We rely on whatever the current signal
350 * handler is to dispatch the signals when we receive them.
352 static void sigfd_handler(void *opaque
)
354 int fd
= (unsigned long) opaque
;
355 struct qemu_signalfd_siginfo info
;
356 struct sigaction action
;
361 len
= read(fd
, &info
, sizeof(info
));
362 } while (len
== -1 && errno
== EINTR
);
364 if (len
== -1 && errno
== EAGAIN
) {
368 if (len
!= sizeof(info
)) {
369 printf("read from sigfd returned %zd: %m\n", len
);
373 sigaction(info
.ssi_signo
, NULL
, &action
);
374 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
375 action
.sa_sigaction(info
.ssi_signo
,
376 (siginfo_t
*)&info
, NULL
);
377 } else if (action
.sa_handler
) {
378 action
.sa_handler(info
.ssi_signo
);
383 static int qemu_signalfd_init(sigset_t mask
)
387 sigfd
= qemu_signalfd(&mask
);
389 fprintf(stderr
, "failed to create signalfd\n");
393 fcntl_setfl(sigfd
, O_NONBLOCK
);
395 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
396 (void *)(unsigned long) sigfd
);
401 int qemu_init_main_loop(void)
404 sigset_t blocked_signals
;
406 cpu_set_debug_excp_handler(cpu_debug_handler
);
408 blocked_signals
= block_io_signals();
410 ret
= qemu_signalfd_init(blocked_signals
);
414 /* Note eventfd must be drained before signalfd handlers run */
415 ret
= qemu_event_init();
419 qemu_cond_init(&qemu_pause_cond
);
420 qemu_cond_init(&qemu_system_cond
);
421 qemu_mutex_init(&qemu_fair_mutex
);
422 qemu_mutex_init(&qemu_global_mutex
);
423 qemu_mutex_lock(&qemu_global_mutex
);
425 qemu_thread_self(&io_thread
);
430 void qemu_main_loop_start(void)
432 qemu_system_ready
= 1;
433 qemu_cond_broadcast(&qemu_system_cond
);
436 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
438 struct qemu_work_item wi
;
440 if (qemu_cpu_self(env
)) {
447 if (!env
->queued_work_first
)
448 env
->queued_work_first
= &wi
;
450 env
->queued_work_last
->next
= &wi
;
451 env
->queued_work_last
= &wi
;
457 CPUState
*self_env
= cpu_single_env
;
459 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
460 cpu_single_env
= self_env
;
464 static void flush_queued_work(CPUState
*env
)
466 struct qemu_work_item
*wi
;
468 if (!env
->queued_work_first
)
471 while ((wi
= env
->queued_work_first
)) {
472 env
->queued_work_first
= wi
->next
;
476 env
->queued_work_last
= NULL
;
477 qemu_cond_broadcast(&qemu_work_cond
);
480 static void qemu_wait_io_event_common(CPUState
*env
)
485 qemu_cond_signal(&qemu_pause_cond
);
487 flush_queued_work(env
);
488 env
->thread_kicked
= false;
491 static void qemu_tcg_wait_io_event(void)
495 while (!any_cpu_has_work())
496 qemu_cond_timedwait(tcg_halt_cond
, &qemu_global_mutex
, 1000);
498 qemu_mutex_unlock(&qemu_global_mutex
);
501 * Users of qemu_global_mutex can be starved, having no chance
502 * to acquire it since this path will get to it first.
503 * So use another lock to provide fairness.
505 qemu_mutex_lock(&qemu_fair_mutex
);
506 qemu_mutex_unlock(&qemu_fair_mutex
);
508 qemu_mutex_lock(&qemu_global_mutex
);
510 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
511 qemu_wait_io_event_common(env
);
515 static void sigbus_reraise(void)
518 struct sigaction action
;
520 memset(&action
, 0, sizeof(action
));
521 action
.sa_handler
= SIG_DFL
;
522 if (!sigaction(SIGBUS
, &action
, NULL
)) {
525 sigaddset(&set
, SIGBUS
);
526 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
528 perror("Failed to re-raise SIGBUS!\n");
532 static void sigbus_handler(int n
, struct qemu_signalfd_siginfo
*siginfo
,
535 #if defined(TARGET_I386)
536 if (kvm_on_sigbus(siginfo
->ssi_code
, (void *)(intptr_t)siginfo
->ssi_addr
))
541 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
549 ts
.tv_sec
= timeout
/ 1000;
550 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
552 sigemptyset(&waitset
);
553 sigaddset(&waitset
, SIG_IPI
);
554 sigaddset(&waitset
, SIGBUS
);
557 qemu_mutex_unlock(&qemu_global_mutex
);
559 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
562 qemu_mutex_lock(&qemu_global_mutex
);
564 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
565 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
572 if (kvm_on_sigbus_vcpu(env
, siginfo
.si_code
, siginfo
.si_addr
))
580 r
= sigpending(&chkset
);
582 fprintf(stderr
, "sigpending: %s\n", strerror(e
));
585 } while (sigismember(&chkset
, SIG_IPI
) || sigismember(&chkset
, SIGBUS
));
588 static void qemu_kvm_wait_io_event(CPUState
*env
)
590 while (!cpu_has_work(env
))
591 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
593 qemu_kvm_eat_signal(env
, 0);
594 qemu_wait_io_event_common(env
);
597 static int qemu_cpu_exec(CPUState
*env
);
599 static void *kvm_cpu_thread_fn(void *arg
)
603 qemu_mutex_lock(&qemu_global_mutex
);
604 qemu_thread_self(env
->thread
);
610 /* signal CPU creation */
612 qemu_cond_signal(&qemu_cpu_cond
);
614 /* and wait for machine initialization */
615 while (!qemu_system_ready
)
616 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
619 if (cpu_can_run(env
))
621 qemu_kvm_wait_io_event(env
);
627 static void *tcg_cpu_thread_fn(void *arg
)
632 qemu_thread_self(env
->thread
);
634 /* signal CPU creation */
635 qemu_mutex_lock(&qemu_global_mutex
);
636 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
638 qemu_cond_signal(&qemu_cpu_cond
);
640 /* and wait for machine initialization */
641 while (!qemu_system_ready
)
642 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
646 qemu_tcg_wait_io_event();
652 void qemu_cpu_kick(void *_env
)
654 CPUState
*env
= _env
;
655 qemu_cond_broadcast(env
->halt_cond
);
656 if (!env
->thread_kicked
) {
657 qemu_thread_signal(env
->thread
, SIG_IPI
);
658 env
->thread_kicked
= true;
662 int qemu_cpu_self(void *_env
)
664 CPUState
*env
= _env
;
667 qemu_thread_self(&this);
669 return qemu_thread_equal(&this, env
->thread
);
672 static void cpu_signal(int sig
)
675 cpu_exit(cpu_single_env
);
679 static void tcg_init_ipi(void)
682 struct sigaction sigact
;
684 memset(&sigact
, 0, sizeof(sigact
));
685 sigact
.sa_handler
= cpu_signal
;
686 sigaction(SIG_IPI
, &sigact
, NULL
);
689 sigaddset(&set
, SIG_IPI
);
690 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
693 static void dummy_signal(int sig
)
697 static void kvm_init_ipi(CPUState
*env
)
701 struct sigaction sigact
;
703 memset(&sigact
, 0, sizeof(sigact
));
704 sigact
.sa_handler
= dummy_signal
;
705 sigaction(SIG_IPI
, &sigact
, NULL
);
707 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
708 sigdelset(&set
, SIG_IPI
);
709 sigdelset(&set
, SIGBUS
);
710 r
= kvm_set_signal_mask(env
, &set
);
712 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
717 static sigset_t
block_io_signals(void)
720 struct sigaction action
;
722 /* SIGUSR2 used by posix-aio-compat.c */
724 sigaddset(&set
, SIGUSR2
);
725 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
728 sigaddset(&set
, SIGIO
);
729 sigaddset(&set
, SIGALRM
);
730 sigaddset(&set
, SIG_IPI
);
731 sigaddset(&set
, SIGBUS
);
732 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
734 memset(&action
, 0, sizeof(action
));
735 action
.sa_flags
= SA_SIGINFO
;
736 action
.sa_sigaction
= (void (*)(int, siginfo_t
*, void*))sigbus_handler
;
737 sigaction(SIGBUS
, &action
, NULL
);
738 prctl(PR_MCE_KILL
, 1, 1, 0, 0);
743 void qemu_mutex_lock_iothread(void)
746 qemu_mutex_lock(&qemu_global_mutex
);
748 qemu_mutex_lock(&qemu_fair_mutex
);
749 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
750 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
751 qemu_mutex_lock(&qemu_global_mutex
);
753 qemu_mutex_unlock(&qemu_fair_mutex
);
757 void qemu_mutex_unlock_iothread(void)
759 qemu_mutex_unlock(&qemu_global_mutex
);
762 static int all_vcpus_paused(void)
764 CPUState
*penv
= first_cpu
;
769 penv
= (CPUState
*)penv
->next_cpu
;
775 void pause_all_vcpus(void)
777 CPUState
*penv
= first_cpu
;
782 penv
= (CPUState
*)penv
->next_cpu
;
785 while (!all_vcpus_paused()) {
786 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
790 penv
= (CPUState
*)penv
->next_cpu
;
795 void resume_all_vcpus(void)
797 CPUState
*penv
= first_cpu
;
803 penv
= (CPUState
*)penv
->next_cpu
;
807 static void tcg_init_vcpu(void *_env
)
809 CPUState
*env
= _env
;
810 /* share a single thread for all cpus with TCG */
811 if (!tcg_cpu_thread
) {
812 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
813 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
814 qemu_cond_init(env
->halt_cond
);
815 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
816 while (env
->created
== 0)
817 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
818 tcg_cpu_thread
= env
->thread
;
819 tcg_halt_cond
= env
->halt_cond
;
821 env
->thread
= tcg_cpu_thread
;
822 env
->halt_cond
= tcg_halt_cond
;
826 static void kvm_start_vcpu(CPUState
*env
)
828 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
829 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
830 qemu_cond_init(env
->halt_cond
);
831 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
832 while (env
->created
== 0)
833 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
836 void qemu_init_vcpu(void *_env
)
838 CPUState
*env
= _env
;
840 env
->nr_cores
= smp_cores
;
841 env
->nr_threads
= smp_threads
;
848 void qemu_notify_event(void)
850 qemu_event_increment();
853 static void qemu_system_vmstop_request(int reason
)
855 vmstop_requested
= reason
;
859 void cpu_stop_current(void)
861 if (cpu_single_env
) {
862 cpu_single_env
->stopped
= 1;
863 cpu_exit(cpu_single_env
);
867 void vm_stop(int reason
)
870 qemu_thread_self(&me
);
872 if (!qemu_thread_equal(&me
, &io_thread
)) {
873 qemu_system_vmstop_request(reason
);
875 * FIXME: should not return to device code in case
876 * vm_stop() has been requested.
886 static int qemu_cpu_exec(CPUState
*env
)
889 #ifdef CONFIG_PROFILER
893 #ifdef CONFIG_PROFILER
894 ti
= profile_getclock();
899 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
900 env
->icount_decr
.u16
.low
= 0;
901 env
->icount_extra
= 0;
902 count
= qemu_icount_round (qemu_next_deadline());
903 qemu_icount
+= count
;
904 decr
= (count
> 0xffff) ? 0xffff : count
;
906 env
->icount_decr
.u16
.low
= decr
;
907 env
->icount_extra
= count
;
910 #ifdef CONFIG_PROFILER
911 qemu_time
+= profile_getclock() - ti
;
914 /* Fold pending instructions back into the
915 instruction counter, and clear the interrupt flag. */
916 qemu_icount
-= (env
->icount_decr
.u16
.low
917 + env
->icount_extra
);
918 env
->icount_decr
.u32
= 0;
919 env
->icount_extra
= 0;
924 bool cpu_exec_all(void)
926 if (next_cpu
== NULL
)
927 next_cpu
= first_cpu
;
928 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
929 CPUState
*env
= next_cpu
;
931 qemu_clock_enable(vm_clock
,
932 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
934 if (qemu_alarm_pending())
936 if (cpu_can_run(env
)) {
937 if (qemu_cpu_exec(env
) == EXCP_DEBUG
) {
940 } else if (env
->stop
) {
945 return any_cpu_has_work();
948 void set_numa_modes(void)
953 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
954 for (i
= 0; i
< nb_numa_nodes
; i
++) {
955 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
962 void set_cpu_log(const char *optarg
)
965 const CPULogItem
*item
;
967 mask
= cpu_str_to_log_mask(optarg
);
969 printf("Log items (comma separated):\n");
970 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
971 printf("%-10s %s\n", item
->name
, item
->help
);
978 /* Return the virtual CPU time, based on the instruction counter. */
979 int64_t cpu_get_icount(void)
982 CPUState
*env
= cpu_single_env
;;
984 icount
= qemu_icount
;
986 if (!can_do_io(env
)) {
987 fprintf(stderr
, "Bad clock read\n");
989 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
991 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
994 void list_cpus(FILE *f
, fprintf_function cpu_fprintf
, const char *optarg
)
996 /* XXX: implement xxx_cpu_list for targets that still miss it */
997 #if defined(cpu_list_id)
998 cpu_list_id(f
, cpu_fprintf
, optarg
);
999 #elif defined(cpu_list)
1000 cpu_list(f
, cpu_fprintf
); /* deprecated */