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
->queued_work_first
)
120 if (env
->stopped
|| !vm_running
)
124 if (qemu_cpu_has_work(env
))
129 static int tcg_has_work(void)
133 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
134 if (cpu_has_work(env
))
140 static int io_thread_fd
= -1;
142 static void qemu_event_increment(void)
144 /* Write 8 bytes to be compatible with eventfd. */
145 static uint64_t val
= 1;
148 if (io_thread_fd
== -1)
152 ret
= write(io_thread_fd
, &val
, sizeof(val
));
153 } while (ret
< 0 && errno
== EINTR
);
155 /* EAGAIN is fine, a read must be pending. */
156 if (ret
< 0 && errno
!= EAGAIN
) {
157 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
163 static void qemu_event_read(void *opaque
)
165 int fd
= (unsigned long)opaque
;
169 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
171 len
= read(fd
, buffer
, sizeof(buffer
));
172 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
175 static int qemu_event_init(void)
180 err
= qemu_eventfd(fds
);
184 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
188 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
192 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
193 (void *)(unsigned long)fds
[0]);
195 io_thread_fd
= fds
[1];
204 HANDLE qemu_event_handle
;
206 static void dummy_event_handler(void *opaque
)
210 static int qemu_event_init(void)
212 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
213 if (!qemu_event_handle
) {
214 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
217 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
221 static void qemu_event_increment(void)
223 if (!SetEvent(qemu_event_handle
)) {
224 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
231 #ifndef CONFIG_IOTHREAD
232 int qemu_init_main_loop(void)
234 return qemu_event_init();
237 void qemu_main_loop_start(void)
241 void qemu_init_vcpu(void *_env
)
243 CPUState
*env
= _env
;
245 env
->nr_cores
= smp_cores
;
246 env
->nr_threads
= smp_threads
;
252 int qemu_cpu_self(void *env
)
257 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
262 void resume_all_vcpus(void)
266 void pause_all_vcpus(void)
270 void qemu_cpu_kick(void *env
)
275 void qemu_notify_event(void)
277 CPUState
*env
= cpu_single_env
;
279 qemu_event_increment ();
283 if (next_cpu
&& env
!= next_cpu
) {
288 void qemu_mutex_lock_iothread(void) {}
289 void qemu_mutex_unlock_iothread(void) {}
291 void vm_stop(int reason
)
296 #else /* CONFIG_IOTHREAD */
298 #include "qemu-thread.h"
300 QemuMutex qemu_global_mutex
;
301 static QemuMutex qemu_fair_mutex
;
303 static QemuThread io_thread
;
305 static QemuThread
*tcg_cpu_thread
;
306 static QemuCond
*tcg_halt_cond
;
308 static int qemu_system_ready
;
310 static QemuCond qemu_cpu_cond
;
312 static QemuCond qemu_system_cond
;
313 static QemuCond qemu_pause_cond
;
314 static QemuCond qemu_work_cond
;
316 static void tcg_block_io_signals(void);
317 static void kvm_block_io_signals(CPUState
*env
);
318 static void unblock_io_signals(void);
320 int qemu_init_main_loop(void)
324 ret
= qemu_event_init();
328 qemu_cond_init(&qemu_pause_cond
);
329 qemu_mutex_init(&qemu_fair_mutex
);
330 qemu_mutex_init(&qemu_global_mutex
);
331 qemu_mutex_lock(&qemu_global_mutex
);
333 unblock_io_signals();
334 qemu_thread_self(&io_thread
);
339 void qemu_main_loop_start(void)
341 qemu_system_ready
= 1;
342 qemu_cond_broadcast(&qemu_system_cond
);
345 void run_on_cpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
347 struct qemu_work_item wi
;
349 if (qemu_cpu_self(env
)) {
356 if (!env
->queued_work_first
)
357 env
->queued_work_first
= &wi
;
359 env
->queued_work_last
->next
= &wi
;
360 env
->queued_work_last
= &wi
;
366 CPUState
*self_env
= cpu_single_env
;
368 qemu_cond_wait(&qemu_work_cond
, &qemu_global_mutex
);
369 cpu_single_env
= self_env
;
373 static void flush_queued_work(CPUState
*env
)
375 struct qemu_work_item
*wi
;
377 if (!env
->queued_work_first
)
380 while ((wi
= env
->queued_work_first
)) {
381 env
->queued_work_first
= wi
->next
;
385 env
->queued_work_last
= NULL
;
386 qemu_cond_broadcast(&qemu_work_cond
);
389 static void qemu_wait_io_event_common(CPUState
*env
)
394 qemu_cond_signal(&qemu_pause_cond
);
396 flush_queued_work(env
);
399 static void qemu_wait_io_event(CPUState
*env
)
401 while (!tcg_has_work())
402 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
404 qemu_mutex_unlock(&qemu_global_mutex
);
407 * Users of qemu_global_mutex can be starved, having no chance
408 * to acquire it since this path will get to it first.
409 * So use another lock to provide fairness.
411 qemu_mutex_lock(&qemu_fair_mutex
);
412 qemu_mutex_unlock(&qemu_fair_mutex
);
414 qemu_mutex_lock(&qemu_global_mutex
);
415 qemu_wait_io_event_common(env
);
418 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
425 ts
.tv_sec
= timeout
/ 1000;
426 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
428 sigemptyset(&waitset
);
429 sigaddset(&waitset
, SIG_IPI
);
431 qemu_mutex_unlock(&qemu_global_mutex
);
432 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
434 qemu_mutex_lock(&qemu_global_mutex
);
436 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
437 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
442 static void qemu_kvm_wait_io_event(CPUState
*env
)
444 while (!cpu_has_work(env
))
445 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
447 qemu_kvm_eat_signal(env
, 0);
448 qemu_wait_io_event_common(env
);
451 static int qemu_cpu_exec(CPUState
*env
);
453 static void *kvm_cpu_thread_fn(void *arg
)
457 qemu_mutex_lock(&qemu_global_mutex
);
458 qemu_thread_self(env
->thread
);
462 kvm_block_io_signals(env
);
464 /* signal CPU creation */
466 qemu_cond_signal(&qemu_cpu_cond
);
468 /* and wait for machine initialization */
469 while (!qemu_system_ready
)
470 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
473 if (cpu_can_run(env
))
475 qemu_kvm_wait_io_event(env
);
481 static void *tcg_cpu_thread_fn(void *arg
)
485 tcg_block_io_signals();
486 qemu_thread_self(env
->thread
);
488 /* signal CPU creation */
489 qemu_mutex_lock(&qemu_global_mutex
);
490 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
492 qemu_cond_signal(&qemu_cpu_cond
);
494 /* and wait for machine initialization */
495 while (!qemu_system_ready
)
496 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
500 qemu_wait_io_event(cur_cpu
);
506 void qemu_cpu_kick(void *_env
)
508 CPUState
*env
= _env
;
509 qemu_cond_broadcast(env
->halt_cond
);
510 qemu_thread_signal(env
->thread
, SIG_IPI
);
513 int qemu_cpu_self(void *_env
)
515 CPUState
*env
= _env
;
518 qemu_thread_self(&this);
520 return qemu_thread_equal(&this, env
->thread
);
523 static void cpu_signal(int sig
)
526 cpu_exit(cpu_single_env
);
530 static void tcg_block_io_signals(void)
533 struct sigaction sigact
;
536 sigaddset(&set
, SIGUSR2
);
537 sigaddset(&set
, SIGIO
);
538 sigaddset(&set
, SIGALRM
);
539 sigaddset(&set
, SIGCHLD
);
540 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
543 sigaddset(&set
, SIG_IPI
);
544 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
546 memset(&sigact
, 0, sizeof(sigact
));
547 sigact
.sa_handler
= cpu_signal
;
548 sigaction(SIG_IPI
, &sigact
, NULL
);
551 static void dummy_signal(int sig
)
555 static void kvm_block_io_signals(CPUState
*env
)
559 struct sigaction sigact
;
562 sigaddset(&set
, SIGUSR2
);
563 sigaddset(&set
, SIGIO
);
564 sigaddset(&set
, SIGALRM
);
565 sigaddset(&set
, SIGCHLD
);
566 sigaddset(&set
, SIG_IPI
);
567 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
569 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
570 sigdelset(&set
, SIG_IPI
);
572 memset(&sigact
, 0, sizeof(sigact
));
573 sigact
.sa_handler
= dummy_signal
;
574 sigaction(SIG_IPI
, &sigact
, NULL
);
576 r
= kvm_set_signal_mask(env
, &set
);
578 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
583 static void unblock_io_signals(void)
588 sigaddset(&set
, SIGUSR2
);
589 sigaddset(&set
, SIGIO
);
590 sigaddset(&set
, SIGALRM
);
591 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
594 sigaddset(&set
, SIG_IPI
);
595 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
598 void qemu_mutex_lock_iothread(void)
601 qemu_mutex_lock(&qemu_fair_mutex
);
602 qemu_mutex_lock(&qemu_global_mutex
);
603 qemu_mutex_unlock(&qemu_fair_mutex
);
605 qemu_mutex_lock(&qemu_fair_mutex
);
606 if (qemu_mutex_trylock(&qemu_global_mutex
)) {
607 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
608 qemu_mutex_lock(&qemu_global_mutex
);
610 qemu_mutex_unlock(&qemu_fair_mutex
);
614 void qemu_mutex_unlock_iothread(void)
616 qemu_mutex_unlock(&qemu_global_mutex
);
619 static int all_vcpus_paused(void)
621 CPUState
*penv
= first_cpu
;
626 penv
= (CPUState
*)penv
->next_cpu
;
632 void pause_all_vcpus(void)
634 CPUState
*penv
= first_cpu
;
639 penv
= (CPUState
*)penv
->next_cpu
;
642 while (!all_vcpus_paused()) {
643 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
647 penv
= (CPUState
*)penv
->next_cpu
;
652 void resume_all_vcpus(void)
654 CPUState
*penv
= first_cpu
;
660 penv
= (CPUState
*)penv
->next_cpu
;
664 static void tcg_init_vcpu(void *_env
)
666 CPUState
*env
= _env
;
667 /* share a single thread for all cpus with TCG */
668 if (!tcg_cpu_thread
) {
669 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
670 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
671 qemu_cond_init(env
->halt_cond
);
672 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
673 while (env
->created
== 0)
674 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
675 tcg_cpu_thread
= env
->thread
;
676 tcg_halt_cond
= env
->halt_cond
;
678 env
->thread
= tcg_cpu_thread
;
679 env
->halt_cond
= tcg_halt_cond
;
683 static void kvm_start_vcpu(CPUState
*env
)
685 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
686 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
687 qemu_cond_init(env
->halt_cond
);
688 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
689 while (env
->created
== 0)
690 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
693 void qemu_init_vcpu(void *_env
)
695 CPUState
*env
= _env
;
697 env
->nr_cores
= smp_cores
;
698 env
->nr_threads
= smp_threads
;
705 void qemu_notify_event(void)
707 qemu_event_increment();
710 static void qemu_system_vmstop_request(int reason
)
712 vmstop_requested
= reason
;
716 void vm_stop(int reason
)
719 qemu_thread_self(&me
);
721 if (!qemu_thread_equal(&me
, &io_thread
)) {
722 qemu_system_vmstop_request(reason
);
724 * FIXME: should not return to device code in case
725 * vm_stop() has been requested.
727 if (cpu_single_env
) {
728 cpu_exit(cpu_single_env
);
729 cpu_single_env
->stop
= 1;
738 static int qemu_cpu_exec(CPUState
*env
)
741 #ifdef CONFIG_PROFILER
745 #ifdef CONFIG_PROFILER
746 ti
= profile_getclock();
751 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
752 env
->icount_decr
.u16
.low
= 0;
753 env
->icount_extra
= 0;
754 count
= qemu_icount_round (qemu_next_deadline());
755 qemu_icount
+= count
;
756 decr
= (count
> 0xffff) ? 0xffff : count
;
758 env
->icount_decr
.u16
.low
= decr
;
759 env
->icount_extra
= count
;
762 #ifdef CONFIG_PROFILER
763 qemu_time
+= profile_getclock() - ti
;
766 /* Fold pending instructions back into the
767 instruction counter, and clear the interrupt flag. */
768 qemu_icount
-= (env
->icount_decr
.u16
.low
769 + env
->icount_extra
);
770 env
->icount_decr
.u32
= 0;
771 env
->icount_extra
= 0;
776 bool tcg_cpu_exec(void)
780 if (next_cpu
== NULL
)
781 next_cpu
= first_cpu
;
782 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
783 CPUState
*env
= cur_cpu
= next_cpu
;
785 qemu_clock_enable(vm_clock
,
786 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
788 if (qemu_alarm_pending())
790 if (cpu_can_run(env
))
791 ret
= qemu_cpu_exec(env
);
795 if (ret
== EXCP_DEBUG
) {
796 gdb_set_stop_cpu(env
);
797 debug_requested
= EXCP_DEBUG
;
801 return tcg_has_work();
804 void set_numa_modes(void)
809 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
810 for (i
= 0; i
< nb_numa_nodes
; i
++) {
811 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
818 void set_cpu_log(const char *optarg
)
821 const CPULogItem
*item
;
823 mask
= cpu_str_to_log_mask(optarg
);
825 printf("Log items (comma separated):\n");
826 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
827 printf("%-10s %s\n", item
->name
, item
->help
);
834 /* Return the virtual CPU time, based on the instruction counter. */
835 int64_t cpu_get_icount(void)
838 CPUState
*env
= cpu_single_env
;;
840 icount
= qemu_icount
;
842 if (!can_do_io(env
)) {
843 fprintf(stderr
, "Bad clock read\n");
845 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
847 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
850 void list_cpus(FILE *f
, int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
853 /* XXX: implement xxx_cpu_list for targets that still miss it */
854 #if defined(cpu_list_id)
855 cpu_list_id(f
, cpu_fprintf
, optarg
);
856 #elif defined(cpu_list)
857 cpu_list(f
, cpu_fprintf
); /* deprecated */