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 static void do_vm_stop(int reason
)
100 vm_state_notify(0, reason
);
101 monitor_protocol_event(QEVENT_STOP
, NULL
);
105 static int cpu_can_run(CPUState
*env
)
109 if (env
->stopped
|| !vm_running
)
114 static int cpu_has_work(CPUState
*env
)
118 if (env
->stopped
|| !vm_running
)
122 if (qemu_cpu_has_work(env
))
127 static int tcg_has_work(void)
131 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
132 if (cpu_has_work(env
))
138 static int io_thread_fd
= -1;
140 static void qemu_event_increment(void)
142 /* Write 8 bytes to be compatible with eventfd. */
143 static uint64_t val
= 1;
146 if (io_thread_fd
== -1)
150 ret
= write(io_thread_fd
, &val
, sizeof(val
));
151 } while (ret
< 0 && errno
== EINTR
);
153 /* EAGAIN is fine, a read must be pending. */
154 if (ret
< 0 && errno
!= EAGAIN
) {
155 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
161 static void qemu_event_read(void *opaque
)
163 int fd
= (unsigned long)opaque
;
167 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
169 len
= read(fd
, buffer
, sizeof(buffer
));
170 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
173 static int qemu_event_init(void)
178 err
= qemu_eventfd(fds
);
182 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
186 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
190 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
191 (void *)(unsigned long)fds
[0]);
193 io_thread_fd
= fds
[1];
202 HANDLE qemu_event_handle
;
204 static void dummy_event_handler(void *opaque
)
208 static int qemu_event_init(void)
210 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
211 if (!qemu_event_handle
) {
212 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
215 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
219 static void qemu_event_increment(void)
221 if (!SetEvent(qemu_event_handle
)) {
222 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
229 #ifndef CONFIG_IOTHREAD
230 int qemu_init_main_loop(void)
232 return qemu_event_init();
235 void qemu_main_loop_start(void)
239 void qemu_init_vcpu(void *_env
)
241 CPUState
*env
= _env
;
243 env
->nr_cores
= smp_cores
;
244 env
->nr_threads
= smp_threads
;
250 int qemu_cpu_self(void *env
)
255 void resume_all_vcpus(void)
259 void pause_all_vcpus(void)
263 void qemu_cpu_kick(void *env
)
268 void qemu_notify_event(void)
270 CPUState
*env
= cpu_single_env
;
272 qemu_event_increment ();
276 if (next_cpu
&& env
!= next_cpu
) {
281 void qemu_mutex_lock_iothread(void) {}
282 void qemu_mutex_unlock_iothread(void) {}
284 void vm_stop(int reason
)
289 #else /* CONFIG_IOTHREAD */
291 #include "qemu-thread.h"
293 QemuMutex qemu_global_mutex
;
294 static QemuMutex qemu_fair_mutex
;
296 static QemuThread io_thread
;
298 static QemuThread
*tcg_cpu_thread
;
299 static QemuCond
*tcg_halt_cond
;
301 static int qemu_system_ready
;
303 static QemuCond qemu_cpu_cond
;
305 static QemuCond qemu_system_cond
;
306 static QemuCond qemu_pause_cond
;
308 static void tcg_block_io_signals(void);
309 static void kvm_block_io_signals(CPUState
*env
);
310 static void unblock_io_signals(void);
312 int qemu_init_main_loop(void)
316 ret
= qemu_event_init();
320 qemu_cond_init(&qemu_pause_cond
);
321 qemu_mutex_init(&qemu_fair_mutex
);
322 qemu_mutex_init(&qemu_global_mutex
);
323 qemu_mutex_lock(&qemu_global_mutex
);
325 unblock_io_signals();
326 qemu_thread_self(&io_thread
);
331 void qemu_main_loop_start(void)
333 qemu_system_ready
= 1;
334 qemu_cond_broadcast(&qemu_system_cond
);
337 static void qemu_wait_io_event_common(CPUState
*env
)
342 qemu_cond_signal(&qemu_pause_cond
);
346 static void qemu_wait_io_event(CPUState
*env
)
348 while (!tcg_has_work())
349 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
351 qemu_mutex_unlock(&qemu_global_mutex
);
354 * Users of qemu_global_mutex can be starved, having no chance
355 * to acquire it since this path will get to it first.
356 * So use another lock to provide fairness.
358 qemu_mutex_lock(&qemu_fair_mutex
);
359 qemu_mutex_unlock(&qemu_fair_mutex
);
361 qemu_mutex_lock(&qemu_global_mutex
);
362 qemu_wait_io_event_common(env
);
365 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
372 ts
.tv_sec
= timeout
/ 1000;
373 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
375 sigemptyset(&waitset
);
376 sigaddset(&waitset
, SIG_IPI
);
378 qemu_mutex_unlock(&qemu_global_mutex
);
379 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
381 qemu_mutex_lock(&qemu_global_mutex
);
383 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
384 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
389 static void qemu_kvm_wait_io_event(CPUState
*env
)
391 while (!cpu_has_work(env
))
392 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
394 qemu_kvm_eat_signal(env
, 0);
395 qemu_wait_io_event_common(env
);
398 static int qemu_cpu_exec(CPUState
*env
);
400 static void *kvm_cpu_thread_fn(void *arg
)
404 qemu_mutex_lock(&qemu_global_mutex
);
405 qemu_thread_self(env
->thread
);
409 kvm_block_io_signals(env
);
411 /* signal CPU creation */
413 qemu_cond_signal(&qemu_cpu_cond
);
415 /* and wait for machine initialization */
416 while (!qemu_system_ready
)
417 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
420 if (cpu_can_run(env
))
422 qemu_kvm_wait_io_event(env
);
428 static void *tcg_cpu_thread_fn(void *arg
)
432 tcg_block_io_signals();
433 qemu_thread_self(env
->thread
);
435 /* signal CPU creation */
436 qemu_mutex_lock(&qemu_global_mutex
);
437 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
439 qemu_cond_signal(&qemu_cpu_cond
);
441 /* and wait for machine initialization */
442 while (!qemu_system_ready
)
443 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
447 qemu_wait_io_event(cur_cpu
);
453 void qemu_cpu_kick(void *_env
)
455 CPUState
*env
= _env
;
456 qemu_cond_broadcast(env
->halt_cond
);
457 qemu_thread_signal(env
->thread
, SIG_IPI
);
460 int qemu_cpu_self(void *_env
)
462 CPUState
*env
= _env
;
465 qemu_thread_self(&this);
467 return qemu_thread_equal(&this, env
->thread
);
470 static void cpu_signal(int sig
)
473 cpu_exit(cpu_single_env
);
477 static void tcg_block_io_signals(void)
480 struct sigaction sigact
;
483 sigaddset(&set
, SIGUSR2
);
484 sigaddset(&set
, SIGIO
);
485 sigaddset(&set
, SIGALRM
);
486 sigaddset(&set
, SIGCHLD
);
487 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
490 sigaddset(&set
, SIG_IPI
);
491 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
493 memset(&sigact
, 0, sizeof(sigact
));
494 sigact
.sa_handler
= cpu_signal
;
495 sigaction(SIG_IPI
, &sigact
, NULL
);
498 static void dummy_signal(int sig
)
502 static void kvm_block_io_signals(CPUState
*env
)
506 struct sigaction sigact
;
509 sigaddset(&set
, SIGUSR2
);
510 sigaddset(&set
, SIGIO
);
511 sigaddset(&set
, SIGALRM
);
512 sigaddset(&set
, SIGCHLD
);
513 sigaddset(&set
, SIG_IPI
);
514 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
516 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
517 sigdelset(&set
, SIG_IPI
);
519 memset(&sigact
, 0, sizeof(sigact
));
520 sigact
.sa_handler
= dummy_signal
;
521 sigaction(SIG_IPI
, &sigact
, NULL
);
523 r
= kvm_set_signal_mask(env
, &set
);
525 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
530 static void unblock_io_signals(void)
535 sigaddset(&set
, SIGUSR2
);
536 sigaddset(&set
, SIGIO
);
537 sigaddset(&set
, SIGALRM
);
538 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
541 sigaddset(&set
, SIG_IPI
);
542 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
545 void qemu_mutex_lock_iothread(void)
548 qemu_mutex_lock(&qemu_fair_mutex
);
549 qemu_mutex_lock(&qemu_global_mutex
);
550 qemu_mutex_unlock(&qemu_fair_mutex
);
552 qemu_mutex_lock(&qemu_fair_mutex
);
553 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
554 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
555 qemu_mutex_lock(&qemu_global_mutex
);
557 qemu_mutex_unlock(&qemu_fair_mutex
);
561 void qemu_mutex_unlock_iothread(void)
563 qemu_mutex_unlock(&qemu_global_mutex
);
566 static int all_vcpus_paused(void)
568 CPUState
*penv
= first_cpu
;
573 penv
= (CPUState
*)penv
->next_cpu
;
579 void pause_all_vcpus(void)
581 CPUState
*penv
= first_cpu
;
586 penv
= (CPUState
*)penv
->next_cpu
;
589 while (!all_vcpus_paused()) {
590 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
594 penv
= (CPUState
*)penv
->next_cpu
;
599 void resume_all_vcpus(void)
601 CPUState
*penv
= first_cpu
;
607 penv
= (CPUState
*)penv
->next_cpu
;
611 static void tcg_init_vcpu(void *_env
)
613 CPUState
*env
= _env
;
614 /* share a single thread for all cpus with TCG */
615 if (!tcg_cpu_thread
) {
616 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
617 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
618 qemu_cond_init(env
->halt_cond
);
619 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
620 while (env
->created
== 0)
621 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
622 tcg_cpu_thread
= env
->thread
;
623 tcg_halt_cond
= env
->halt_cond
;
625 env
->thread
= tcg_cpu_thread
;
626 env
->halt_cond
= tcg_halt_cond
;
630 static void kvm_start_vcpu(CPUState
*env
)
632 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
633 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
634 qemu_cond_init(env
->halt_cond
);
635 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
636 while (env
->created
== 0)
637 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
640 void qemu_init_vcpu(void *_env
)
642 CPUState
*env
= _env
;
644 env
->nr_cores
= smp_cores
;
645 env
->nr_threads
= smp_threads
;
652 void qemu_notify_event(void)
654 qemu_event_increment();
657 static void qemu_system_vmstop_request(int reason
)
659 vmstop_requested
= reason
;
663 void vm_stop(int reason
)
666 qemu_thread_self(&me
);
668 if (!qemu_thread_equal(&me
, &io_thread
)) {
669 qemu_system_vmstop_request(reason
);
671 * FIXME: should not return to device code in case
672 * vm_stop() has been requested.
674 if (cpu_single_env
) {
675 cpu_exit(cpu_single_env
);
676 cpu_single_env
->stop
= 1;
685 static int qemu_cpu_exec(CPUState
*env
)
688 #ifdef CONFIG_PROFILER
692 #ifdef CONFIG_PROFILER
693 ti
= profile_getclock();
698 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
699 env
->icount_decr
.u16
.low
= 0;
700 env
->icount_extra
= 0;
701 count
= qemu_icount_round (qemu_next_deadline());
702 qemu_icount
+= count
;
703 decr
= (count
> 0xffff) ? 0xffff : count
;
705 env
->icount_decr
.u16
.low
= decr
;
706 env
->icount_extra
= count
;
709 #ifdef CONFIG_PROFILER
710 qemu_time
+= profile_getclock() - ti
;
713 /* Fold pending instructions back into the
714 instruction counter, and clear the interrupt flag. */
715 qemu_icount
-= (env
->icount_decr
.u16
.low
716 + env
->icount_extra
);
717 env
->icount_decr
.u32
= 0;
718 env
->icount_extra
= 0;
723 bool tcg_cpu_exec(void)
727 if (next_cpu
== NULL
)
728 next_cpu
= first_cpu
;
729 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
730 CPUState
*env
= cur_cpu
= next_cpu
;
732 qemu_clock_enable(vm_clock
,
733 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
735 if (qemu_alarm_pending())
737 if (cpu_can_run(env
))
738 ret
= qemu_cpu_exec(env
);
742 if (ret
== EXCP_DEBUG
) {
743 gdb_set_stop_cpu(env
);
744 debug_requested
= EXCP_DEBUG
;
748 return tcg_has_work();
751 void set_numa_modes(void)
756 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
757 for (i
= 0; i
< nb_numa_nodes
; i
++) {
758 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
765 void set_cpu_log(const char *optarg
)
768 const CPULogItem
*item
;
770 mask
= cpu_str_to_log_mask(optarg
);
772 printf("Log items (comma separated):\n");
773 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
774 printf("%-10s %s\n", item
->name
, item
->help
);
781 /* Return the virtual CPU time, based on the instruction counter. */
782 int64_t cpu_get_icount(void)
785 CPUState
*env
= cpu_single_env
;;
787 icount
= qemu_icount
;
789 if (!can_do_io(env
)) {
790 fprintf(stderr
, "Bad clock read\n");
792 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
794 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
797 void list_cpus(FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
800 /* XXX: implement xxx_cpu_list for targets that still miss it */
801 #if defined(cpu_list_id)
802 cpu_list_id(f
, cpu_fprintf
, optarg
);
803 #elif defined(cpu_list)
804 cpu_list(f
, cpu_fprintf
); /* deprecated */