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 #define SIG_IPI (SIGRTMIN+4)
40 #define SIG_IPI SIGUSR1
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
;
284 qemu_event_increment ();
288 if (next_cpu
&& env
!= next_cpu
) {
293 void qemu_mutex_lock_iothread(void) {}
294 void qemu_mutex_unlock_iothread(void) {}
296 void vm_stop(int reason
)
301 #else /* CONFIG_IOTHREAD */
303 #include "qemu-thread.h"
305 QemuMutex qemu_global_mutex
;
306 static QemuMutex qemu_fair_mutex
;
308 static QemuThread io_thread
;
310 static QemuThread
*tcg_cpu_thread
;
311 static QemuCond
*tcg_halt_cond
;
313 static int qemu_system_ready
;
315 static QemuCond qemu_cpu_cond
;
317 static QemuCond qemu_system_cond
;
318 static QemuCond qemu_pause_cond
;
319 static QemuCond qemu_work_cond
;
321 static void tcg_init_ipi(void);
322 static void kvm_init_ipi(CPUState
*env
);
323 static void unblock_io_signals(void);
325 int qemu_init_main_loop(void)
329 ret
= qemu_event_init();
333 qemu_cond_init(&qemu_pause_cond
);
334 qemu_cond_init(&qemu_system_cond
);
335 qemu_mutex_init(&qemu_fair_mutex
);
336 qemu_mutex_init(&qemu_global_mutex
);
337 qemu_mutex_lock(&qemu_global_mutex
);
339 unblock_io_signals();
340 qemu_thread_self(&io_thread
);
345 void qemu_main_loop_start(void)
347 qemu_system_ready
= 1;
348 qemu_cond_broadcast(&qemu_system_cond
);
351 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
353 struct qemu_work_item wi
;
355 if (qemu_cpu_self(env
)) {
362 if (!env
->queued_work_first
)
363 env
->queued_work_first
= &wi
;
365 env
->queued_work_last
->next
= &wi
;
366 env
->queued_work_last
= &wi
;
372 CPUState
*self_env
= cpu_single_env
;
374 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
375 cpu_single_env
= self_env
;
379 static void flush_queued_work(CPUState
*env
)
381 struct qemu_work_item
*wi
;
383 if (!env
->queued_work_first
)
386 while ((wi
= env
->queued_work_first
)) {
387 env
->queued_work_first
= wi
->next
;
391 env
->queued_work_last
= NULL
;
392 qemu_cond_broadcast(&qemu_work_cond
);
395 static void qemu_wait_io_event_common(CPUState
*env
)
400 qemu_cond_signal(&qemu_pause_cond
);
402 flush_queued_work(env
);
405 static void qemu_tcg_wait_io_event(void)
409 while (!tcg_has_work())
410 qemu_cond_timedwait(tcg_halt_cond
, &qemu_global_mutex
, 1000);
412 qemu_mutex_unlock(&qemu_global_mutex
);
415 * Users of qemu_global_mutex can be starved, having no chance
416 * to acquire it since this path will get to it first.
417 * So use another lock to provide fairness.
419 qemu_mutex_lock(&qemu_fair_mutex
);
420 qemu_mutex_unlock(&qemu_fair_mutex
);
422 qemu_mutex_lock(&qemu_global_mutex
);
424 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
425 qemu_wait_io_event_common(env
);
429 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
436 ts
.tv_sec
= timeout
/ 1000;
437 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
439 sigemptyset(&waitset
);
440 sigaddset(&waitset
, SIG_IPI
);
442 qemu_mutex_unlock(&qemu_global_mutex
);
443 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
445 qemu_mutex_lock(&qemu_global_mutex
);
447 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
448 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
453 static void qemu_kvm_wait_io_event(CPUState
*env
)
455 while (!cpu_has_work(env
))
456 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
458 qemu_kvm_eat_signal(env
, 0);
459 qemu_wait_io_event_common(env
);
462 static int qemu_cpu_exec(CPUState
*env
);
464 static void *kvm_cpu_thread_fn(void *arg
)
468 qemu_mutex_lock(&qemu_global_mutex
);
469 qemu_thread_self(env
->thread
);
475 /* signal CPU creation */
477 qemu_cond_signal(&qemu_cpu_cond
);
479 /* and wait for machine initialization */
480 while (!qemu_system_ready
)
481 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
484 if (cpu_can_run(env
))
486 qemu_kvm_wait_io_event(env
);
492 static void *tcg_cpu_thread_fn(void *arg
)
497 qemu_thread_self(env
->thread
);
499 /* signal CPU creation */
500 qemu_mutex_lock(&qemu_global_mutex
);
501 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
503 qemu_cond_signal(&qemu_cpu_cond
);
505 /* and wait for machine initialization */
506 while (!qemu_system_ready
)
507 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
511 qemu_tcg_wait_io_event();
517 void qemu_cpu_kick(void *_env
)
519 CPUState
*env
= _env
;
520 qemu_cond_broadcast(env
->halt_cond
);
521 qemu_thread_signal(env
->thread
, SIG_IPI
);
524 int qemu_cpu_self(void *_env
)
526 CPUState
*env
= _env
;
529 qemu_thread_self(&this);
531 return qemu_thread_equal(&this, env
->thread
);
534 static void cpu_signal(int sig
)
537 cpu_exit(cpu_single_env
);
541 static void tcg_init_ipi(void)
544 struct sigaction sigact
;
546 memset(&sigact
, 0, sizeof(sigact
));
547 sigact
.sa_handler
= cpu_signal
;
548 sigaction(SIG_IPI
, &sigact
, NULL
);
551 sigaddset(&set
, SIG_IPI
);
552 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
555 static void dummy_signal(int sig
)
559 static void kvm_init_ipi(CPUState
*env
)
563 struct sigaction sigact
;
565 memset(&sigact
, 0, sizeof(sigact
));
566 sigact
.sa_handler
= dummy_signal
;
567 sigaction(SIG_IPI
, &sigact
, NULL
);
569 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
570 sigdelset(&set
, SIG_IPI
);
571 r
= kvm_set_signal_mask(env
, &set
);
573 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
578 static void unblock_io_signals(void)
583 sigaddset(&set
, SIGUSR2
);
584 sigaddset(&set
, SIGIO
);
585 sigaddset(&set
, SIGALRM
);
586 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
589 sigaddset(&set
, SIG_IPI
);
590 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
593 void qemu_mutex_lock_iothread(void)
596 qemu_mutex_lock(&qemu_fair_mutex
);
597 qemu_mutex_lock(&qemu_global_mutex
);
598 qemu_mutex_unlock(&qemu_fair_mutex
);
600 qemu_mutex_lock(&qemu_fair_mutex
);
601 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
602 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
603 qemu_mutex_lock(&qemu_global_mutex
);
605 qemu_mutex_unlock(&qemu_fair_mutex
);
609 void qemu_mutex_unlock_iothread(void)
611 qemu_mutex_unlock(&qemu_global_mutex
);
614 static int all_vcpus_paused(void)
616 CPUState
*penv
= first_cpu
;
621 penv
= (CPUState
*)penv
->next_cpu
;
627 void pause_all_vcpus(void)
629 CPUState
*penv
= first_cpu
;
634 penv
= (CPUState
*)penv
->next_cpu
;
637 while (!all_vcpus_paused()) {
638 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
642 penv
= (CPUState
*)penv
->next_cpu
;
647 void resume_all_vcpus(void)
649 CPUState
*penv
= first_cpu
;
655 penv
= (CPUState
*)penv
->next_cpu
;
659 static void tcg_init_vcpu(void *_env
)
661 CPUState
*env
= _env
;
662 /* share a single thread for all cpus with TCG */
663 if (!tcg_cpu_thread
) {
664 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
665 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
666 qemu_cond_init(env
->halt_cond
);
667 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
668 while (env
->created
== 0)
669 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
670 tcg_cpu_thread
= env
->thread
;
671 tcg_halt_cond
= env
->halt_cond
;
673 env
->thread
= tcg_cpu_thread
;
674 env
->halt_cond
= tcg_halt_cond
;
678 static void kvm_start_vcpu(CPUState
*env
)
680 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
681 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
682 qemu_cond_init(env
->halt_cond
);
683 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
684 while (env
->created
== 0)
685 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
688 void qemu_init_vcpu(void *_env
)
690 CPUState
*env
= _env
;
692 env
->nr_cores
= smp_cores
;
693 env
->nr_threads
= smp_threads
;
700 void qemu_notify_event(void)
702 qemu_event_increment();
705 static void qemu_system_vmstop_request(int reason
)
707 vmstop_requested
= reason
;
711 void vm_stop(int reason
)
714 qemu_thread_self(&me
);
716 if (!qemu_thread_equal(&me
, &io_thread
)) {
717 qemu_system_vmstop_request(reason
);
719 * FIXME: should not return to device code in case
720 * vm_stop() has been requested.
722 if (cpu_single_env
) {
723 cpu_exit(cpu_single_env
);
724 cpu_single_env
->stop
= 1;
733 static int qemu_cpu_exec(CPUState
*env
)
736 #ifdef CONFIG_PROFILER
740 #ifdef CONFIG_PROFILER
741 ti
= profile_getclock();
746 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
747 env
->icount_decr
.u16
.low
= 0;
748 env
->icount_extra
= 0;
749 count
= qemu_icount_round (qemu_next_deadline());
750 qemu_icount
+= count
;
751 decr
= (count
> 0xffff) ? 0xffff : count
;
753 env
->icount_decr
.u16
.low
= decr
;
754 env
->icount_extra
= count
;
757 #ifdef CONFIG_PROFILER
758 qemu_time
+= profile_getclock() - ti
;
761 /* Fold pending instructions back into the
762 instruction counter, and clear the interrupt flag. */
763 qemu_icount
-= (env
->icount_decr
.u16
.low
764 + env
->icount_extra
);
765 env
->icount_decr
.u32
= 0;
766 env
->icount_extra
= 0;
771 bool tcg_cpu_exec(void)
775 if (next_cpu
== NULL
)
776 next_cpu
= first_cpu
;
777 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
778 CPUState
*env
= next_cpu
;
780 qemu_clock_enable(vm_clock
,
781 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
783 if (qemu_alarm_pending())
785 if (cpu_can_run(env
))
786 ret
= qemu_cpu_exec(env
);
790 if (ret
== EXCP_DEBUG
) {
791 gdb_set_stop_cpu(env
);
792 debug_requested
= EXCP_DEBUG
;
797 return tcg_has_work();
800 void set_numa_modes(void)
805 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
806 for (i
= 0; i
< nb_numa_nodes
; i
++) {
807 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
814 void set_cpu_log(const char *optarg
)
817 const CPULogItem
*item
;
819 mask
= cpu_str_to_log_mask(optarg
);
821 printf("Log items (comma separated):\n");
822 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
823 printf("%-10s %s\n", item
->name
, item
->help
);
830 /* Return the virtual CPU time, based on the instruction counter. */
831 int64_t cpu_get_icount(void)
834 CPUState
*env
= cpu_single_env
;;
836 icount
= qemu_icount
;
838 if (!can_do_io(env
)) {
839 fprintf(stderr
, "Bad clock read\n");
841 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
843 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
846 void list_cpus(FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
849 /* XXX: implement xxx_cpu_list for targets that still miss it */
850 #if defined(cpu_list_id)
851 cpu_list_id(f
, cpu_fprintf
, optarg
);
852 #elif defined(cpu_list)
853 cpu_list(f
, cpu_fprintf
); /* deprecated */