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 any_cpu_has_work(void)
138 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
139 if (cpu_has_work(env
))
144 static void cpu_debug_handler(CPUState
*env
)
146 gdb_set_stop_cpu(env
);
147 debug_requested
= EXCP_DEBUG
;
152 static int io_thread_fd
= -1;
154 static void qemu_event_increment(void)
156 /* Write 8 bytes to be compatible with eventfd. */
157 static const uint64_t val
= 1;
160 if (io_thread_fd
== -1)
164 ret
= write(io_thread_fd
, &val
, sizeof(val
));
165 } while (ret
< 0 && errno
== EINTR
);
167 /* EAGAIN is fine, a read must be pending. */
168 if (ret
< 0 && errno
!= EAGAIN
) {
169 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
175 static void qemu_event_read(void *opaque
)
177 int fd
= (unsigned long)opaque
;
181 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
183 len
= read(fd
, buffer
, sizeof(buffer
));
184 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
187 static int qemu_event_init(void)
192 err
= qemu_eventfd(fds
);
196 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
200 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
204 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
205 (void *)(unsigned long)fds
[0]);
207 io_thread_fd
= fds
[1];
216 HANDLE qemu_event_handle
;
218 static void dummy_event_handler(void *opaque
)
222 static int qemu_event_init(void)
224 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
225 if (!qemu_event_handle
) {
226 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
229 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
233 static void qemu_event_increment(void)
235 if (!SetEvent(qemu_event_handle
)) {
236 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
243 #ifndef CONFIG_IOTHREAD
244 int qemu_init_main_loop(void)
246 cpu_set_debug_excp_handler(cpu_debug_handler
);
248 return qemu_event_init();
251 void qemu_main_loop_start(void)
255 void qemu_init_vcpu(void *_env
)
257 CPUState
*env
= _env
;
259 env
->nr_cores
= smp_cores
;
260 env
->nr_threads
= smp_threads
;
266 int qemu_cpu_self(void *env
)
271 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
276 void resume_all_vcpus(void)
280 void pause_all_vcpus(void)
284 void qemu_cpu_kick(void *env
)
289 void qemu_notify_event(void)
291 CPUState
*env
= cpu_single_env
;
294 qemu_kvm_notify_work();
298 qemu_event_increment ();
302 if (next_cpu
&& env
!= next_cpu
) {
307 #if defined(KVM_UPSTREAM) || !defined(CONFIG_KVM)
308 void qemu_mutex_lock_iothread(void) {}
309 void qemu_mutex_unlock_iothread(void) {}
312 void vm_stop(int reason
)
317 #else /* CONFIG_IOTHREAD */
319 #include "qemu-thread.h"
321 QemuMutex qemu_global_mutex
;
322 static QemuMutex qemu_fair_mutex
;
324 static QemuThread io_thread
;
326 static QemuThread
*tcg_cpu_thread
;
327 static QemuCond
*tcg_halt_cond
;
329 static int qemu_system_ready
;
331 static QemuCond qemu_cpu_cond
;
333 static QemuCond qemu_system_cond
;
334 static QemuCond qemu_pause_cond
;
335 static QemuCond qemu_work_cond
;
337 static void tcg_init_ipi(void);
338 static void kvm_init_ipi(CPUState
*env
);
339 static void unblock_io_signals(void);
341 int qemu_init_main_loop(void)
345 cpu_set_debug_excp_handler(cpu_debug_handler
);
347 ret
= qemu_event_init();
351 qemu_cond_init(&qemu_pause_cond
);
352 qemu_cond_init(&qemu_system_cond
);
353 qemu_mutex_init(&qemu_fair_mutex
);
354 qemu_mutex_init(&qemu_global_mutex
);
355 qemu_mutex_lock(&qemu_global_mutex
);
357 unblock_io_signals();
358 qemu_thread_self(&io_thread
);
363 void qemu_main_loop_start(void)
365 qemu_system_ready
= 1;
366 qemu_cond_broadcast(&qemu_system_cond
);
369 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
371 struct qemu_work_item wi
;
373 if (qemu_cpu_self(env
)) {
380 if (!env
->queued_work_first
)
381 env
->queued_work_first
= &wi
;
383 env
->queued_work_last
->next
= &wi
;
384 env
->queued_work_last
= &wi
;
390 CPUState
*self_env
= cpu_single_env
;
392 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
393 cpu_single_env
= self_env
;
397 static void flush_queued_work(CPUState
*env
)
399 struct qemu_work_item
*wi
;
401 if (!env
->queued_work_first
)
404 while ((wi
= env
->queued_work_first
)) {
405 env
->queued_work_first
= wi
->next
;
409 env
->queued_work_last
= NULL
;
410 qemu_cond_broadcast(&qemu_work_cond
);
413 static void qemu_wait_io_event_common(CPUState
*env
)
418 qemu_cond_signal(&qemu_pause_cond
);
420 flush_queued_work(env
);
423 static void qemu_tcg_wait_io_event(void)
427 while (!any_cpu_has_work())
428 qemu_cond_timedwait(tcg_halt_cond
, &qemu_global_mutex
, 1000);
430 qemu_mutex_unlock(&qemu_global_mutex
);
433 * Users of qemu_global_mutex can be starved, having no chance
434 * to acquire it since this path will get to it first.
435 * So use another lock to provide fairness.
437 qemu_mutex_lock(&qemu_fair_mutex
);
438 qemu_mutex_unlock(&qemu_fair_mutex
);
440 qemu_mutex_lock(&qemu_global_mutex
);
442 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
443 qemu_wait_io_event_common(env
);
447 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
454 ts
.tv_sec
= timeout
/ 1000;
455 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
457 sigemptyset(&waitset
);
458 sigaddset(&waitset
, SIG_IPI
);
460 qemu_mutex_unlock(&qemu_global_mutex
);
461 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
463 qemu_mutex_lock(&qemu_global_mutex
);
465 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
466 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
471 static void qemu_kvm_wait_io_event(CPUState
*env
)
473 while (!cpu_has_work(env
))
474 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
476 qemu_kvm_eat_signal(env
, 0);
477 qemu_wait_io_event_common(env
);
480 static int qemu_cpu_exec(CPUState
*env
);
482 static void *kvm_cpu_thread_fn(void *arg
)
486 qemu_mutex_lock(&qemu_global_mutex
);
487 qemu_thread_self(env
->thread
);
493 /* signal CPU creation */
495 qemu_cond_signal(&qemu_cpu_cond
);
497 /* and wait for machine initialization */
498 while (!qemu_system_ready
)
499 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
502 if (cpu_can_run(env
))
504 qemu_kvm_wait_io_event(env
);
510 static void *tcg_cpu_thread_fn(void *arg
)
515 qemu_thread_self(env
->thread
);
517 /* signal CPU creation */
518 qemu_mutex_lock(&qemu_global_mutex
);
519 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
521 qemu_cond_signal(&qemu_cpu_cond
);
523 /* and wait for machine initialization */
524 while (!qemu_system_ready
)
525 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
529 qemu_tcg_wait_io_event();
535 void qemu_cpu_kick(void *_env
)
537 CPUState
*env
= _env
;
538 qemu_cond_broadcast(env
->halt_cond
);
539 qemu_thread_signal(env
->thread
, SIG_IPI
);
542 int qemu_cpu_self(void *_env
)
544 CPUState
*env
= _env
;
547 qemu_thread_self(&this);
549 return qemu_thread_equal(&this, env
->thread
);
552 static void cpu_signal(int sig
)
555 cpu_exit(cpu_single_env
);
559 static void tcg_init_ipi(void)
562 struct sigaction sigact
;
564 memset(&sigact
, 0, sizeof(sigact
));
565 sigact
.sa_handler
= cpu_signal
;
566 sigaction(SIG_IPI
, &sigact
, NULL
);
569 sigaddset(&set
, SIG_IPI
);
570 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
573 static void dummy_signal(int sig
)
577 static void kvm_init_ipi(CPUState
*env
)
581 struct sigaction sigact
;
583 memset(&sigact
, 0, sizeof(sigact
));
584 sigact
.sa_handler
= dummy_signal
;
585 sigaction(SIG_IPI
, &sigact
, NULL
);
587 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
588 sigdelset(&set
, SIG_IPI
);
589 r
= kvm_set_signal_mask(env
, &set
);
591 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
596 static void unblock_io_signals(void)
601 sigaddset(&set
, SIGUSR2
);
602 sigaddset(&set
, SIGIO
);
603 sigaddset(&set
, SIGALRM
);
604 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
607 sigaddset(&set
, SIG_IPI
);
608 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
611 void qemu_mutex_lock_iothread(void)
614 qemu_mutex_lock(&qemu_fair_mutex
);
615 qemu_mutex_lock(&qemu_global_mutex
);
616 qemu_mutex_unlock(&qemu_fair_mutex
);
618 qemu_mutex_lock(&qemu_fair_mutex
);
619 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
620 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
621 qemu_mutex_lock(&qemu_global_mutex
);
623 qemu_mutex_unlock(&qemu_fair_mutex
);
627 void qemu_mutex_unlock_iothread(void)
629 qemu_mutex_unlock(&qemu_global_mutex
);
632 static int all_vcpus_paused(void)
634 CPUState
*penv
= first_cpu
;
639 penv
= (CPUState
*)penv
->next_cpu
;
645 void pause_all_vcpus(void)
647 CPUState
*penv
= first_cpu
;
652 penv
= (CPUState
*)penv
->next_cpu
;
655 while (!all_vcpus_paused()) {
656 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
660 penv
= (CPUState
*)penv
->next_cpu
;
665 void resume_all_vcpus(void)
667 CPUState
*penv
= first_cpu
;
673 penv
= (CPUState
*)penv
->next_cpu
;
677 static void tcg_init_vcpu(void *_env
)
679 CPUState
*env
= _env
;
680 /* share a single thread for all cpus with TCG */
681 if (!tcg_cpu_thread
) {
682 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
683 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
684 qemu_cond_init(env
->halt_cond
);
685 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
686 while (env
->created
== 0)
687 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
688 tcg_cpu_thread
= env
->thread
;
689 tcg_halt_cond
= env
->halt_cond
;
691 env
->thread
= tcg_cpu_thread
;
692 env
->halt_cond
= tcg_halt_cond
;
696 static void kvm_start_vcpu(CPUState
*env
)
698 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
699 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
700 qemu_cond_init(env
->halt_cond
);
701 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
702 while (env
->created
== 0)
703 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
706 void qemu_init_vcpu(void *_env
)
708 CPUState
*env
= _env
;
710 env
->nr_cores
= smp_cores
;
711 env
->nr_threads
= smp_threads
;
718 void qemu_notify_event(void)
720 qemu_event_increment();
723 static void qemu_system_vmstop_request(int reason
)
725 vmstop_requested
= reason
;
729 void vm_stop(int reason
)
732 qemu_thread_self(&me
);
734 if (!qemu_thread_equal(&me
, &io_thread
)) {
735 qemu_system_vmstop_request(reason
);
737 * FIXME: should not return to device code in case
738 * vm_stop() has been requested.
740 if (cpu_single_env
) {
741 cpu_exit(cpu_single_env
);
742 cpu_single_env
->stop
= 1;
751 static int qemu_cpu_exec(CPUState
*env
)
754 #ifdef CONFIG_PROFILER
758 #ifdef CONFIG_PROFILER
759 ti
= profile_getclock();
764 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
765 env
->icount_decr
.u16
.low
= 0;
766 env
->icount_extra
= 0;
767 count
= qemu_icount_round (qemu_next_deadline());
768 qemu_icount
+= count
;
769 decr
= (count
> 0xffff) ? 0xffff : count
;
771 env
->icount_decr
.u16
.low
= decr
;
772 env
->icount_extra
= count
;
775 #ifdef CONFIG_PROFILER
776 qemu_time
+= profile_getclock() - ti
;
779 /* Fold pending instructions back into the
780 instruction counter, and clear the interrupt flag. */
781 qemu_icount
-= (env
->icount_decr
.u16
.low
782 + env
->icount_extra
);
783 env
->icount_decr
.u32
= 0;
784 env
->icount_extra
= 0;
789 bool cpu_exec_all(void)
791 if (next_cpu
== NULL
)
792 next_cpu
= first_cpu
;
793 for (; next_cpu
!= NULL
&& !exit_request
; next_cpu
= next_cpu
->next_cpu
) {
794 CPUState
*env
= next_cpu
;
796 qemu_clock_enable(vm_clock
,
797 (env
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
799 if (qemu_alarm_pending())
801 if (cpu_can_run(env
)) {
802 if (qemu_cpu_exec(env
) == EXCP_DEBUG
) {
805 } else if (env
->stop
) {
810 return any_cpu_has_work();
813 void set_numa_modes(void)
818 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
819 for (i
= 0; i
< nb_numa_nodes
; i
++) {
820 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
827 void set_cpu_log(const char *optarg
)
830 const CPULogItem
*item
;
832 mask
= cpu_str_to_log_mask(optarg
);
834 printf("Log items (comma separated):\n");
835 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
836 printf("%-10s %s\n", item
->name
, item
->help
);
843 /* Return the virtual CPU time, based on the instruction counter. */
844 int64_t cpu_get_icount(void)
847 CPUState
*env
= cpu_single_env
;;
849 icount
= qemu_icount
;
851 if (!can_do_io(env
)) {
852 fprintf(stderr
, "Bad clock read\n");
854 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
856 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
859 void list_cpus(FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
862 /* XXX: implement xxx_cpu_list for targets that still miss it */
863 #if defined(cpu_list_id)
864 cpu_list_id(f
, cpu_fprintf
, optarg
);
865 #elif defined(cpu_list)
866 cpu_list(f
, cpu_fprintf
); /* deprecated */