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"
39 #define SIG_IPI (SIGRTMIN+4)
41 #define SIG_IPI SIGUSR1
44 static CPUState
*next_cpu
;
46 /***********************************************************/
47 void hw_error(const char *fmt
, ...)
53 fprintf(stderr
, "qemu: hardware error: ");
54 vfprintf(stderr
, fmt
, ap
);
55 fprintf(stderr
, "\n");
56 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
57 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
59 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
61 cpu_dump_state(env
, stderr
, fprintf
, 0);
68 void cpu_synchronize_all_states(void)
72 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
73 cpu_synchronize_state(cpu
);
77 void cpu_synchronize_all_post_reset(void)
81 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
82 cpu_synchronize_post_reset(cpu
);
86 void cpu_synchronize_all_post_init(void)
90 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
91 cpu_synchronize_post_init(cpu
);
95 int cpu_is_stopped(CPUState
*env
)
97 return !vm_running
|| env
->stopped
;
100 static void do_vm_stop(int reason
)
106 vm_state_notify(0, reason
);
107 monitor_protocol_event(QEVENT_STOP
, NULL
);
111 static int cpu_can_run(CPUState
*env
)
115 if (env
->stopped
|| !vm_running
)
120 static int cpu_has_work(CPUState
*env
)
124 if (env
->queued_work_first
)
126 if (env
->stopped
|| !vm_running
)
130 if (qemu_cpu_has_work(env
))
135 static int any_cpu_has_work(void)
139 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
140 if (cpu_has_work(env
))
145 static void cpu_debug_handler(CPUState
*env
)
147 gdb_set_stop_cpu(env
);
148 debug_requested
= EXCP_DEBUG
;
153 static int io_thread_fd
= -1;
155 static void qemu_event_increment(void)
157 /* Write 8 bytes to be compatible with eventfd. */
158 static const uint64_t val
= 1;
161 if (io_thread_fd
== -1)
165 ret
= write(io_thread_fd
, &val
, sizeof(val
));
166 } while (ret
< 0 && errno
== EINTR
);
168 /* EAGAIN is fine, a read must be pending. */
169 if (ret
< 0 && errno
!= EAGAIN
) {
170 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
176 static void qemu_event_read(void *opaque
)
178 int fd
= (unsigned long)opaque
;
182 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
184 len
= read(fd
, buffer
, sizeof(buffer
));
185 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
188 static int qemu_event_init(void)
193 err
= qemu_eventfd(fds
);
197 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
201 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
205 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
206 (void *)(unsigned long)fds
[0]);
208 io_thread_fd
= fds
[1];
217 HANDLE qemu_event_handle
;
219 static void dummy_event_handler(void *opaque
)
223 static int qemu_event_init(void)
225 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
226 if (!qemu_event_handle
) {
227 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
230 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
234 static void qemu_event_increment(void)
236 if (!SetEvent(qemu_event_handle
)) {
237 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
244 #ifndef CONFIG_IOTHREAD
245 int qemu_init_main_loop(void)
247 cpu_set_debug_excp_handler(cpu_debug_handler
);
249 return qemu_event_init();
252 void qemu_main_loop_start(void)
256 void qemu_init_vcpu(void *_env
)
258 CPUState
*env
= _env
;
260 env
->nr_cores
= smp_cores
;
261 env
->nr_threads
= smp_threads
;
267 int qemu_cpu_self(void *env
)
272 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
277 void resume_all_vcpus(void)
281 void pause_all_vcpus(void)
285 void qemu_cpu_kick(void *env
)
290 void qemu_notify_event(void)
292 CPUState
*env
= cpu_single_env
;
294 qemu_event_increment ();
298 if (next_cpu
&& env
!= next_cpu
) {
303 void qemu_mutex_lock_iothread(void) {}
304 void qemu_mutex_unlock_iothread(void) {}
306 void vm_stop(int reason
)
311 #else /* CONFIG_IOTHREAD */
313 #include "qemu-thread.h"
315 QemuMutex qemu_global_mutex
;
316 static QemuMutex qemu_fair_mutex
;
318 static QemuThread io_thread
;
320 static QemuThread
*tcg_cpu_thread
;
321 static QemuCond
*tcg_halt_cond
;
323 static int qemu_system_ready
;
325 static QemuCond qemu_cpu_cond
;
327 static QemuCond qemu_system_cond
;
328 static QemuCond qemu_pause_cond
;
329 static QemuCond qemu_work_cond
;
331 static void tcg_init_ipi(void);
332 static void kvm_init_ipi(CPUState
*env
);
333 static sigset_t
block_io_signals(void);
335 /* If we have signalfd, we mask out the signals we want to handle and then
336 * use signalfd to listen for them. We rely on whatever the current signal
337 * handler is to dispatch the signals when we receive them.
339 static void sigfd_handler(void *opaque
)
341 int fd
= (unsigned long) opaque
;
342 struct qemu_signalfd_siginfo info
;
343 struct sigaction action
;
348 len
= read(fd
, &info
, sizeof(info
));
349 } while (len
== -1 && errno
== EINTR
);
351 if (len
== -1 && errno
== EAGAIN
) {
355 if (len
!= sizeof(info
)) {
356 printf("read from sigfd returned %zd: %m\n", len
);
360 sigaction(info
.ssi_signo
, NULL
, &action
);
361 if ((action
.sa_flags
& SA_SIGINFO
) && action
.sa_sigaction
) {
362 action
.sa_sigaction(info
.ssi_signo
,
363 (siginfo_t
*)&info
, NULL
);
364 } else if (action
.sa_handler
) {
365 action
.sa_handler(info
.ssi_signo
);
370 static int qemu_signalfd_init(sigset_t mask
)
374 sigfd
= qemu_signalfd(&mask
);
376 fprintf(stderr
, "failed to create signalfd\n");
380 fcntl_setfl(sigfd
, O_NONBLOCK
);
382 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
383 (void *)(unsigned long) sigfd
);
388 int qemu_init_main_loop(void)
391 sigset_t blocked_signals
;
393 cpu_set_debug_excp_handler(cpu_debug_handler
);
395 blocked_signals
= block_io_signals();
397 ret
= qemu_signalfd_init(blocked_signals
);
401 /* Note eventfd must be drained before signalfd handlers run */
402 ret
= qemu_event_init();
406 qemu_cond_init(&qemu_pause_cond
);
407 qemu_cond_init(&qemu_system_cond
);
408 qemu_mutex_init(&qemu_fair_mutex
);
409 qemu_mutex_init(&qemu_global_mutex
);
410 qemu_mutex_lock(&qemu_global_mutex
);
412 qemu_thread_self(&io_thread
);
417 void qemu_main_loop_start(void)
419 qemu_system_ready
= 1;
420 qemu_cond_broadcast(&qemu_system_cond
);
423 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
425 struct qemu_work_item wi
;
427 if (qemu_cpu_self(env
)) {
434 if (!env
->queued_work_first
)
435 env
->queued_work_first
= &wi
;
437 env
->queued_work_last
->next
= &wi
;
438 env
->queued_work_last
= &wi
;
444 CPUState
*self_env
= cpu_single_env
;
446 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
447 cpu_single_env
= self_env
;
451 static void flush_queued_work(CPUState
*env
)
453 struct qemu_work_item
*wi
;
455 if (!env
->queued_work_first
)
458 while ((wi
= env
->queued_work_first
)) {
459 env
->queued_work_first
= wi
->next
;
463 env
->queued_work_last
= NULL
;
464 qemu_cond_broadcast(&qemu_work_cond
);
467 static void qemu_wait_io_event_common(CPUState
*env
)
472 qemu_cond_signal(&qemu_pause_cond
);
474 flush_queued_work(env
);
477 static void qemu_tcg_wait_io_event(void)
481 while (!any_cpu_has_work())
482 qemu_cond_timedwait(tcg_halt_cond
, &qemu_global_mutex
, 1000);
484 qemu_mutex_unlock(&qemu_global_mutex
);
487 * Users of qemu_global_mutex can be starved, having no chance
488 * to acquire it since this path will get to it first.
489 * So use another lock to provide fairness.
491 qemu_mutex_lock(&qemu_fair_mutex
);
492 qemu_mutex_unlock(&qemu_fair_mutex
);
494 qemu_mutex_lock(&qemu_global_mutex
);
496 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
497 qemu_wait_io_event_common(env
);
501 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
508 ts
.tv_sec
= timeout
/ 1000;
509 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
511 sigemptyset(&waitset
);
512 sigaddset(&waitset
, SIG_IPI
);
514 qemu_mutex_unlock(&qemu_global_mutex
);
515 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
517 qemu_mutex_lock(&qemu_global_mutex
);
519 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
520 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
525 static void qemu_kvm_wait_io_event(CPUState
*env
)
527 while (!cpu_has_work(env
))
528 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
530 qemu_kvm_eat_signal(env
, 0);
531 qemu_wait_io_event_common(env
);
534 static int qemu_cpu_exec(CPUState
*env
);
536 static void *kvm_cpu_thread_fn(void *arg
)
540 qemu_mutex_lock(&qemu_global_mutex
);
541 qemu_thread_self(env
->thread
);
547 /* signal CPU creation */
549 qemu_cond_signal(&qemu_cpu_cond
);
551 /* and wait for machine initialization */
552 while (!qemu_system_ready
)
553 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
556 if (cpu_can_run(env
))
558 qemu_kvm_wait_io_event(env
);
564 static void *tcg_cpu_thread_fn(void *arg
)
569 qemu_thread_self(env
->thread
);
571 /* signal CPU creation */
572 qemu_mutex_lock(&qemu_global_mutex
);
573 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
575 qemu_cond_signal(&qemu_cpu_cond
);
577 /* and wait for machine initialization */
578 while (!qemu_system_ready
)
579 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
583 qemu_tcg_wait_io_event();
589 void qemu_cpu_kick(void *_env
)
591 CPUState
*env
= _env
;
592 qemu_cond_broadcast(env
->halt_cond
);
593 qemu_thread_signal(env
->thread
, SIG_IPI
);
596 int qemu_cpu_self(void *_env
)
598 CPUState
*env
= _env
;
601 qemu_thread_self(&this);
603 return qemu_thread_equal(&this, env
->thread
);
606 static void cpu_signal(int sig
)
609 cpu_exit(cpu_single_env
);
613 static void tcg_init_ipi(void)
616 struct sigaction sigact
;
618 memset(&sigact
, 0, sizeof(sigact
));
619 sigact
.sa_handler
= cpu_signal
;
620 sigaction(SIG_IPI
, &sigact
, NULL
);
623 sigaddset(&set
, SIG_IPI
);
624 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
627 static void dummy_signal(int sig
)
631 static void kvm_init_ipi(CPUState
*env
)
635 struct sigaction sigact
;
637 memset(&sigact
, 0, sizeof(sigact
));
638 sigact
.sa_handler
= dummy_signal
;
639 sigaction(SIG_IPI
, &sigact
, NULL
);
641 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
642 sigdelset(&set
, SIG_IPI
);
643 r
= kvm_set_signal_mask(env
, &set
);
645 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
650 static sigset_t
block_io_signals(void)
654 /* SIGUSR2 used by posix-aio-compat.c */
656 sigaddset(&set
, SIGUSR2
);
657 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
660 sigaddset(&set
, SIGIO
);
661 sigaddset(&set
, SIGALRM
);
662 sigaddset(&set
, SIG_IPI
);
663 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
668 void qemu_mutex_lock_iothread(void)
671 qemu_mutex_lock(&qemu_fair_mutex
);
672 qemu_mutex_lock(&qemu_global_mutex
);
673 qemu_mutex_unlock(&qemu_fair_mutex
);
675 qemu_mutex_lock(&qemu_fair_mutex
);
676 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
677 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
678 qemu_mutex_lock(&qemu_global_mutex
);
680 qemu_mutex_unlock(&qemu_fair_mutex
);
684 void qemu_mutex_unlock_iothread(void)
686 qemu_mutex_unlock(&qemu_global_mutex
);
689 static int all_vcpus_paused(void)
691 CPUState
*penv
= first_cpu
;
696 penv
= (CPUState
*)penv
->next_cpu
;
702 void pause_all_vcpus(void)
704 CPUState
*penv
= first_cpu
;
709 penv
= (CPUState
*)penv
->next_cpu
;
712 while (!all_vcpus_paused()) {
713 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
717 penv
= (CPUState
*)penv
->next_cpu
;
722 void resume_all_vcpus(void)
724 CPUState
*penv
= first_cpu
;
730 penv
= (CPUState
*)penv
->next_cpu
;
734 static void tcg_init_vcpu(void *_env
)
736 CPUState
*env
= _env
;
737 /* share a single thread for all cpus with TCG */
738 if (!tcg_cpu_thread
) {
739 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
740 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
741 qemu_cond_init(env
->halt_cond
);
742 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
743 while (env
->created
== 0)
744 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
745 tcg_cpu_thread
= env
->thread
;
746 tcg_halt_cond
= env
->halt_cond
;
748 env
->thread
= tcg_cpu_thread
;
749 env
->halt_cond
= tcg_halt_cond
;
753 static void kvm_start_vcpu(CPUState
*env
)
755 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
756 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
757 qemu_cond_init(env
->halt_cond
);
758 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
759 while (env
->created
== 0)
760 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
763 void qemu_init_vcpu(void *_env
)
765 CPUState
*env
= _env
;
767 env
->nr_cores
= smp_cores
;
768 env
->nr_threads
= smp_threads
;
775 void qemu_notify_event(void)
777 qemu_event_increment();
780 static void qemu_system_vmstop_request(int reason
)
782 vmstop_requested
= reason
;
786 void vm_stop(int reason
)
789 qemu_thread_self(&me
);
791 if (!qemu_thread_equal(&me
, &io_thread
)) {
792 qemu_system_vmstop_request(reason
);
794 * FIXME: should not return to device code in case
795 * vm_stop() has been requested.
797 if (cpu_single_env
) {
798 cpu_exit(cpu_single_env
);
799 cpu_single_env
->stop
= 1;
808 static int qemu_cpu_exec(CPUState
*env
)
811 #ifdef CONFIG_PROFILER
815 #ifdef CONFIG_PROFILER
816 ti
= profile_getclock();
821 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
822 env
->icount_decr
.u16
.low
= 0;
823 env
->icount_extra
= 0;
824 count
= qemu_icount_round (qemu_next_deadline());
825 qemu_icount
+= count
;
826 decr
= (count
> 0xffff) ? 0xffff : count
;
828 env
->icount_decr
.u16
.low
= decr
;
829 env
->icount_extra
= count
;
832 #ifdef CONFIG_PROFILER
833 qemu_time
+= profile_getclock() - ti
;
836 /* Fold pending instructions back into the
837 instruction counter, and clear the interrupt flag. */
838 qemu_icount
-= (env
->icount_decr
.u16
.low
839 + env
->icount_extra
);
840 env
->icount_decr
.u32
= 0;
841 env
->icount_extra
= 0;
846 bool cpu_exec_all(void)
848 if (next_cpu
== NULL
)
849 next_cpu
= first_cpu
;
850 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
851 CPUState
*env
= next_cpu
;
853 qemu_clock_enable(vm_clock
,
854 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
856 if (qemu_alarm_pending())
858 if (cpu_can_run(env
)) {
859 if (qemu_cpu_exec(env
) == EXCP_DEBUG
) {
862 } else if (env
->stop
) {
867 return any_cpu_has_work();
870 void set_numa_modes(void)
875 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
876 for (i
= 0; i
< nb_numa_nodes
; i
++) {
877 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
884 void set_cpu_log(const char *optarg
)
887 const CPULogItem
*item
;
889 mask
= cpu_str_to_log_mask(optarg
);
891 printf("Log items (comma separated):\n");
892 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
893 printf("%-10s %s\n", item
->name
, item
->help
);
900 /* Return the virtual CPU time, based on the instruction counter. */
901 int64_t cpu_get_icount(void)
904 CPUState
*env
= cpu_single_env
;;
906 icount
= qemu_icount
;
908 if (!can_do_io(env
)) {
909 fprintf(stderr
, "Bad clock read\n");
911 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
913 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
916 void list_cpus(FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
919 /* XXX: implement xxx_cpu_list for targets that still miss it */
920 #if defined(cpu_list_id)
921 cpu_list_id(f
, cpu_fprintf
, optarg
);
922 #elif defined(cpu_list)
923 cpu_list(f
, cpu_fprintf
); /* deprecated */