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"
37 #define SIG_IPI (SIGRTMIN+4)
39 #define SIG_IPI SIGUSR1
42 static CPUState
*cur_cpu
;
43 static CPUState
*next_cpu
;
45 /***********************************************************/
46 void hw_error(const char *fmt
, ...)
52 fprintf(stderr
, "qemu: hardware error: ");
53 vfprintf(stderr
, fmt
, ap
);
54 fprintf(stderr
, "\n");
55 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
56 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
58 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
60 cpu_dump_state(env
, stderr
, fprintf
, 0);
67 void cpu_synchronize_all_states(void)
71 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
72 cpu_synchronize_state(cpu
);
76 void cpu_synchronize_all_post_reset(void)
80 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
81 cpu_synchronize_post_reset(cpu
);
85 void cpu_synchronize_all_post_init(void)
89 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
90 cpu_synchronize_post_init(cpu
);
94 int cpu_is_stopped(CPUState
*env
)
96 return !vm_running
|| env
->stopped
;
99 static void do_vm_stop(int reason
)
105 vm_state_notify(0, reason
);
106 monitor_protocol_event(QEVENT_STOP
, NULL
);
110 static int cpu_can_run(CPUState
*env
)
114 if (env
->stopped
|| !vm_running
)
119 static int cpu_has_work(CPUState
*env
)
123 if (env
->queued_work_first
)
125 if (env
->stopped
|| !vm_running
)
129 if (qemu_cpu_has_work(env
))
134 static int tcg_has_work(void)
138 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
139 if (cpu_has_work(env
))
145 static int io_thread_fd
= -1;
147 static void qemu_event_increment(void)
149 /* Write 8 bytes to be compatible with eventfd. */
150 static const uint64_t val
= 1;
153 if (io_thread_fd
== -1)
157 ret
= write(io_thread_fd
, &val
, sizeof(val
));
158 } while (ret
< 0 && errno
== EINTR
);
160 /* EAGAIN is fine, a read must be pending. */
161 if (ret
< 0 && errno
!= EAGAIN
) {
162 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
168 static void qemu_event_read(void *opaque
)
170 int fd
= (unsigned long)opaque
;
174 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
176 len
= read(fd
, buffer
, sizeof(buffer
));
177 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
180 static int qemu_event_init(void)
185 err
= qemu_eventfd(fds
);
189 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
193 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
197 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
198 (void *)(unsigned long)fds
[0]);
200 io_thread_fd
= fds
[1];
209 HANDLE qemu_event_handle
;
211 static void dummy_event_handler(void *opaque
)
215 static int qemu_event_init(void)
217 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
218 if (!qemu_event_handle
) {
219 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
222 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
226 static void qemu_event_increment(void)
228 if (!SetEvent(qemu_event_handle
)) {
229 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
236 #ifndef CONFIG_IOTHREAD
237 int qemu_init_main_loop(void)
239 return qemu_event_init();
242 void qemu_main_loop_start(void)
246 void qemu_init_vcpu(void *_env
)
248 CPUState
*env
= _env
;
250 env
->nr_cores
= smp_cores
;
251 env
->nr_threads
= smp_threads
;
257 int qemu_cpu_self(void *env
)
262 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
267 void resume_all_vcpus(void)
271 void pause_all_vcpus(void)
275 void qemu_cpu_kick(void *env
)
280 void qemu_notify_event(void)
282 CPUState
*env
= cpu_single_env
;
285 qemu_kvm_notify_work();
289 qemu_event_increment ();
293 if (next_cpu
&& env
!= next_cpu
) {
298 #if defined(KVM_UPSTREAM) || !defined(CONFIG_KVM)
299 void qemu_mutex_lock_iothread(void) {}
300 void qemu_mutex_unlock_iothread(void) {}
303 void vm_stop(int reason
)
308 #else /* CONFIG_IOTHREAD */
310 #include "qemu-thread.h"
312 QemuMutex qemu_global_mutex
;
313 static QemuMutex qemu_fair_mutex
;
315 static QemuThread io_thread
;
317 static QemuThread
*tcg_cpu_thread
;
318 static QemuCond
*tcg_halt_cond
;
320 static int qemu_system_ready
;
322 static QemuCond qemu_cpu_cond
;
324 static QemuCond qemu_system_cond
;
325 static QemuCond qemu_pause_cond
;
326 static QemuCond qemu_work_cond
;
328 static void tcg_block_io_signals(void);
329 static void kvm_block_io_signals(CPUState
*env
);
330 static void unblock_io_signals(void);
332 int qemu_init_main_loop(void)
336 ret
= qemu_event_init();
340 qemu_cond_init(&qemu_pause_cond
);
341 qemu_mutex_init(&qemu_fair_mutex
);
342 qemu_mutex_init(&qemu_global_mutex
);
343 qemu_mutex_lock(&qemu_global_mutex
);
345 unblock_io_signals();
346 qemu_thread_self(&io_thread
);
351 void qemu_main_loop_start(void)
353 qemu_system_ready
= 1;
354 qemu_cond_broadcast(&qemu_system_cond
);
357 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
359 struct qemu_work_item wi
;
361 if (qemu_cpu_self(env
)) {
368 if (!env
->queued_work_first
)
369 env
->queued_work_first
= &wi
;
371 env
->queued_work_last
->next
= &wi
;
372 env
->queued_work_last
= &wi
;
378 CPUState
*self_env
= cpu_single_env
;
380 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
381 cpu_single_env
= self_env
;
385 static void flush_queued_work(CPUState
*env
)
387 struct qemu_work_item
*wi
;
389 if (!env
->queued_work_first
)
392 while ((wi
= env
->queued_work_first
)) {
393 env
->queued_work_first
= wi
->next
;
397 env
->queued_work_last
= NULL
;
398 qemu_cond_broadcast(&qemu_work_cond
);
401 static void qemu_wait_io_event_common(CPUState
*env
)
406 qemu_cond_signal(&qemu_pause_cond
);
408 flush_queued_work(env
);
411 static void qemu_wait_io_event(CPUState
*env
)
413 while (!tcg_has_work())
414 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
416 qemu_mutex_unlock(&qemu_global_mutex
);
419 * Users of qemu_global_mutex can be starved, having no chance
420 * to acquire it since this path will get to it first.
421 * So use another lock to provide fairness.
423 qemu_mutex_lock(&qemu_fair_mutex
);
424 qemu_mutex_unlock(&qemu_fair_mutex
);
426 qemu_mutex_lock(&qemu_global_mutex
);
427 qemu_wait_io_event_common(env
);
430 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
437 ts
.tv_sec
= timeout
/ 1000;
438 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
440 sigemptyset(&waitset
);
441 sigaddset(&waitset
, SIG_IPI
);
443 qemu_mutex_unlock(&qemu_global_mutex
);
444 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
446 qemu_mutex_lock(&qemu_global_mutex
);
448 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
449 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
454 static void qemu_kvm_wait_io_event(CPUState
*env
)
456 while (!cpu_has_work(env
))
457 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
459 qemu_kvm_eat_signal(env
, 0);
460 qemu_wait_io_event_common(env
);
463 static int qemu_cpu_exec(CPUState
*env
);
465 static void *kvm_cpu_thread_fn(void *arg
)
469 qemu_mutex_lock(&qemu_global_mutex
);
470 qemu_thread_self(env
->thread
);
474 kvm_block_io_signals(env
);
476 /* signal CPU creation */
478 qemu_cond_signal(&qemu_cpu_cond
);
480 /* and wait for machine initialization */
481 while (!qemu_system_ready
)
482 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
485 if (cpu_can_run(env
))
487 qemu_kvm_wait_io_event(env
);
493 static void *tcg_cpu_thread_fn(void *arg
)
497 tcg_block_io_signals();
498 qemu_thread_self(env
->thread
);
500 /* signal CPU creation */
501 qemu_mutex_lock(&qemu_global_mutex
);
502 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
504 qemu_cond_signal(&qemu_cpu_cond
);
506 /* and wait for machine initialization */
507 while (!qemu_system_ready
)
508 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
512 qemu_wait_io_event(cur_cpu
);
518 void qemu_cpu_kick(void *_env
)
520 CPUState
*env
= _env
;
521 qemu_cond_broadcast(env
->halt_cond
);
522 qemu_thread_signal(env
->thread
, SIG_IPI
);
525 int qemu_cpu_self(void *_env
)
527 CPUState
*env
= _env
;
530 qemu_thread_self(&this);
532 return qemu_thread_equal(&this, env
->thread
);
535 static void cpu_signal(int sig
)
538 cpu_exit(cpu_single_env
);
542 static void tcg_block_io_signals(void)
545 struct sigaction sigact
;
548 sigaddset(&set
, SIGUSR2
);
549 sigaddset(&set
, SIGIO
);
550 sigaddset(&set
, SIGALRM
);
551 sigaddset(&set
, SIGCHLD
);
552 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
555 sigaddset(&set
, SIG_IPI
);
556 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
558 memset(&sigact
, 0, sizeof(sigact
));
559 sigact
.sa_handler
= cpu_signal
;
560 sigaction(SIG_IPI
, &sigact
, NULL
);
563 static void dummy_signal(int sig
)
567 static void kvm_block_io_signals(CPUState
*env
)
571 struct sigaction sigact
;
574 sigaddset(&set
, SIGUSR2
);
575 sigaddset(&set
, SIGIO
);
576 sigaddset(&set
, SIGALRM
);
577 sigaddset(&set
, SIGCHLD
);
578 sigaddset(&set
, SIG_IPI
);
579 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
581 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
582 sigdelset(&set
, SIG_IPI
);
584 memset(&sigact
, 0, sizeof(sigact
));
585 sigact
.sa_handler
= dummy_signal
;
586 sigaction(SIG_IPI
, &sigact
, NULL
);
588 r
= kvm_set_signal_mask(env
, &set
);
590 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
595 static void unblock_io_signals(void)
600 sigaddset(&set
, SIGUSR2
);
601 sigaddset(&set
, SIGIO
);
602 sigaddset(&set
, SIGALRM
);
603 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
606 sigaddset(&set
, SIG_IPI
);
607 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
610 void qemu_mutex_lock_iothread(void)
613 qemu_mutex_lock(&qemu_fair_mutex
);
614 qemu_mutex_lock(&qemu_global_mutex
);
615 qemu_mutex_unlock(&qemu_fair_mutex
);
617 qemu_mutex_lock(&qemu_fair_mutex
);
618 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
619 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
620 qemu_mutex_lock(&qemu_global_mutex
);
622 qemu_mutex_unlock(&qemu_fair_mutex
);
626 void qemu_mutex_unlock_iothread(void)
628 qemu_mutex_unlock(&qemu_global_mutex
);
631 static int all_vcpus_paused(void)
633 CPUState
*penv
= first_cpu
;
638 penv
= (CPUState
*)penv
->next_cpu
;
644 void pause_all_vcpus(void)
646 CPUState
*penv
= first_cpu
;
651 penv
= (CPUState
*)penv
->next_cpu
;
654 while (!all_vcpus_paused()) {
655 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
659 penv
= (CPUState
*)penv
->next_cpu
;
664 void resume_all_vcpus(void)
666 CPUState
*penv
= first_cpu
;
672 penv
= (CPUState
*)penv
->next_cpu
;
676 static void tcg_init_vcpu(void *_env
)
678 CPUState
*env
= _env
;
679 /* share a single thread for all cpus with TCG */
680 if (!tcg_cpu_thread
) {
681 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
682 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
683 qemu_cond_init(env
->halt_cond
);
684 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
685 while (env
->created
== 0)
686 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
687 tcg_cpu_thread
= env
->thread
;
688 tcg_halt_cond
= env
->halt_cond
;
690 env
->thread
= tcg_cpu_thread
;
691 env
->halt_cond
= tcg_halt_cond
;
695 static void kvm_start_vcpu(CPUState
*env
)
697 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
698 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
699 qemu_cond_init(env
->halt_cond
);
700 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
701 while (env
->created
== 0)
702 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
705 void qemu_init_vcpu(void *_env
)
707 CPUState
*env
= _env
;
709 env
->nr_cores
= smp_cores
;
710 env
->nr_threads
= smp_threads
;
717 void qemu_notify_event(void)
719 qemu_event_increment();
722 static void qemu_system_vmstop_request(int reason
)
724 vmstop_requested
= reason
;
728 void vm_stop(int reason
)
731 qemu_thread_self(&me
);
733 if (!qemu_thread_equal(&me
, &io_thread
)) {
734 qemu_system_vmstop_request(reason
);
736 * FIXME: should not return to device code in case
737 * vm_stop() has been requested.
739 if (cpu_single_env
) {
740 cpu_exit(cpu_single_env
);
741 cpu_single_env
->stop
= 1;
750 static int qemu_cpu_exec(CPUState
*env
)
753 #ifdef CONFIG_PROFILER
757 #ifdef CONFIG_PROFILER
758 ti
= profile_getclock();
763 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
764 env
->icount_decr
.u16
.low
= 0;
765 env
->icount_extra
= 0;
766 count
= qemu_icount_round (qemu_next_deadline());
767 qemu_icount
+= count
;
768 decr
= (count
> 0xffff) ? 0xffff : count
;
770 env
->icount_decr
.u16
.low
= decr
;
771 env
->icount_extra
= count
;
774 #ifdef CONFIG_PROFILER
775 qemu_time
+= profile_getclock() - ti
;
778 /* Fold pending instructions back into the
779 instruction counter, and clear the interrupt flag. */
780 qemu_icount
-= (env
->icount_decr
.u16
.low
781 + env
->icount_extra
);
782 env
->icount_decr
.u32
= 0;
783 env
->icount_extra
= 0;
788 bool tcg_cpu_exec(void)
792 if (next_cpu
== NULL
)
793 next_cpu
= first_cpu
;
794 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
795 CPUState
*env
= cur_cpu
= next_cpu
;
797 qemu_clock_enable(vm_clock
,
798 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
800 if (qemu_alarm_pending())
802 if (cpu_can_run(env
))
803 ret
= qemu_cpu_exec(env
);
807 if (ret
== EXCP_DEBUG
) {
808 gdb_set_stop_cpu(env
);
809 debug_requested
= EXCP_DEBUG
;
813 return tcg_has_work();
816 void set_numa_modes(void)
821 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
822 for (i
= 0; i
< nb_numa_nodes
; i
++) {
823 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
830 void set_cpu_log(const char *optarg
)
833 const CPULogItem
*item
;
835 mask
= cpu_str_to_log_mask(optarg
);
837 printf("Log items (comma separated):\n");
838 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
839 printf("%-10s %s\n", item
->name
, item
->help
);
846 /* Return the virtual CPU time, based on the instruction counter. */
847 int64_t cpu_get_icount(void)
850 CPUState
*env
= cpu_single_env
;;
852 icount
= qemu_icount
;
854 if (!can_do_io(env
)) {
855 fprintf(stderr
, "Bad clock read\n");
857 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
859 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
862 void list_cpus(FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
865 /* XXX: implement xxx_cpu_list for targets that still miss it */
866 #if defined(cpu_list_id)
867 cpu_list_id(f
, cpu_fprintf
, optarg
);
868 #elif defined(cpu_list)
869 cpu_list(f
, cpu_fprintf
); /* deprecated */