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"
36 static CPUState
*cur_cpu
;
37 static CPUState
*next_cpu
;
39 /***********************************************************/
40 void hw_error(const char *fmt
, ...)
46 fprintf(stderr
, "qemu: hardware error: ");
47 vfprintf(stderr
, fmt
, ap
);
48 fprintf(stderr
, "\n");
49 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
50 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
52 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
54 cpu_dump_state(env
, stderr
, fprintf
, 0);
61 void cpu_synchronize_all_states(void)
65 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
66 cpu_synchronize_state(cpu
);
70 void cpu_synchronize_all_post_reset(void)
74 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
75 cpu_synchronize_post_reset(cpu
);
79 void cpu_synchronize_all_post_init(void)
83 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
84 cpu_synchronize_post_init(cpu
);
88 static void do_vm_stop(int reason
)
94 vm_state_notify(0, reason
);
95 monitor_protocol_event(QEVENT_STOP
, NULL
);
99 static int cpu_can_run(CPUState
*env
)
103 if (env
->stopped
|| !vm_running
)
108 static int cpu_has_work(CPUState
*env
)
112 if (env
->stopped
|| !vm_running
)
116 if (qemu_cpu_has_work(env
))
121 static int tcg_has_work(void)
125 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
126 if (cpu_has_work(env
))
132 static int io_thread_fd
= -1;
134 static void qemu_event_increment(void)
136 /* Write 8 bytes to be compatible with eventfd. */
137 static uint64_t val
= 1;
140 if (io_thread_fd
== -1)
144 ret
= write(io_thread_fd
, &val
, sizeof(val
));
145 } while (ret
< 0 && errno
== EINTR
);
147 /* EAGAIN is fine, a read must be pending. */
148 if (ret
< 0 && errno
!= EAGAIN
) {
149 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
155 static void qemu_event_read(void *opaque
)
157 int fd
= (unsigned long)opaque
;
161 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
163 len
= read(fd
, buffer
, sizeof(buffer
));
164 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
167 static int qemu_event_init(void)
172 err
= qemu_eventfd(fds
);
176 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
180 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
184 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
185 (void *)(unsigned long)fds
[0]);
187 io_thread_fd
= fds
[1];
196 HANDLE qemu_event_handle
;
198 static void dummy_event_handler(void *opaque
)
202 static int qemu_event_init(void)
204 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
205 if (!qemu_event_handle
) {
206 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
209 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
213 static void qemu_event_increment(void)
215 if (!SetEvent(qemu_event_handle
)) {
216 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
223 #ifndef CONFIG_IOTHREAD
224 int qemu_init_main_loop(void)
226 return qemu_event_init();
229 void qemu_init_vcpu(void *_env
)
231 CPUState
*env
= _env
;
233 env
->nr_cores
= smp_cores
;
234 env
->nr_threads
= smp_threads
;
240 int qemu_cpu_self(void *env
)
245 void resume_all_vcpus(void)
249 void pause_all_vcpus(void)
253 void qemu_cpu_kick(void *env
)
258 void qemu_notify_event(void)
260 CPUState
*env
= cpu_single_env
;
262 qemu_event_increment ();
266 if (next_cpu
&& env
!= next_cpu
) {
271 void qemu_mutex_lock_iothread(void) {}
272 void qemu_mutex_unlock_iothread(void) {}
274 void vm_stop(int reason
)
279 #else /* CONFIG_IOTHREAD */
281 #include "qemu-thread.h"
283 QemuMutex qemu_global_mutex
;
284 static QemuMutex qemu_fair_mutex
;
286 static QemuThread io_thread
;
288 static QemuThread
*tcg_cpu_thread
;
289 static QemuCond
*tcg_halt_cond
;
291 static int qemu_system_ready
;
293 static QemuCond qemu_cpu_cond
;
295 static QemuCond qemu_system_cond
;
296 static QemuCond qemu_pause_cond
;
298 static void tcg_block_io_signals(void);
299 static void kvm_block_io_signals(CPUState
*env
);
300 static void unblock_io_signals(void);
302 int qemu_init_main_loop(void)
306 ret
= qemu_event_init();
310 qemu_cond_init(&qemu_pause_cond
);
311 qemu_mutex_init(&qemu_fair_mutex
);
312 qemu_mutex_init(&qemu_global_mutex
);
313 qemu_mutex_lock(&qemu_global_mutex
);
315 unblock_io_signals();
316 qemu_thread_self(&io_thread
);
321 static void qemu_wait_io_event_common(CPUState
*env
)
326 qemu_cond_signal(&qemu_pause_cond
);
330 static void qemu_wait_io_event(CPUState
*env
)
332 while (!tcg_has_work())
333 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
335 qemu_mutex_unlock(&qemu_global_mutex
);
338 * Users of qemu_global_mutex can be starved, having no chance
339 * to acquire it since this path will get to it first.
340 * So use another lock to provide fairness.
342 qemu_mutex_lock(&qemu_fair_mutex
);
343 qemu_mutex_unlock(&qemu_fair_mutex
);
345 qemu_mutex_lock(&qemu_global_mutex
);
346 qemu_wait_io_event_common(env
);
349 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
356 ts
.tv_sec
= timeout
/ 1000;
357 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
359 sigemptyset(&waitset
);
360 sigaddset(&waitset
, SIG_IPI
);
362 qemu_mutex_unlock(&qemu_global_mutex
);
363 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
365 qemu_mutex_lock(&qemu_global_mutex
);
367 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
368 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
373 static void qemu_kvm_wait_io_event(CPUState
*env
)
375 while (!cpu_has_work(env
))
376 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
378 qemu_kvm_eat_signal(env
, 0);
379 qemu_wait_io_event_common(env
);
382 static int qemu_cpu_exec(CPUState
*env
);
384 static void *kvm_cpu_thread_fn(void *arg
)
388 qemu_thread_self(env
->thread
);
392 kvm_block_io_signals(env
);
394 /* signal CPU creation */
395 qemu_mutex_lock(&qemu_global_mutex
);
397 qemu_cond_signal(&qemu_cpu_cond
);
399 /* and wait for machine initialization */
400 while (!qemu_system_ready
)
401 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
404 if (cpu_can_run(env
))
406 qemu_kvm_wait_io_event(env
);
412 static void *tcg_cpu_thread_fn(void *arg
)
416 tcg_block_io_signals();
417 qemu_thread_self(env
->thread
);
419 /* signal CPU creation */
420 qemu_mutex_lock(&qemu_global_mutex
);
421 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
423 qemu_cond_signal(&qemu_cpu_cond
);
425 /* and wait for machine initialization */
426 while (!qemu_system_ready
)
427 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
431 qemu_wait_io_event(cur_cpu
);
437 void qemu_cpu_kick(void *_env
)
439 CPUState
*env
= _env
;
440 qemu_cond_broadcast(env
->halt_cond
);
442 qemu_thread_signal(env
->thread
, SIG_IPI
);
445 int qemu_cpu_self(void *_env
)
447 CPUState
*env
= _env
;
450 qemu_thread_self(&this);
452 return qemu_thread_equal(&this, env
->thread
);
455 static void cpu_signal(int sig
)
458 cpu_exit(cpu_single_env
);
461 static void tcg_block_io_signals(void)
464 struct sigaction sigact
;
467 sigaddset(&set
, SIGUSR2
);
468 sigaddset(&set
, SIGIO
);
469 sigaddset(&set
, SIGALRM
);
470 sigaddset(&set
, SIGCHLD
);
471 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
474 sigaddset(&set
, SIG_IPI
);
475 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
477 memset(&sigact
, 0, sizeof(sigact
));
478 sigact
.sa_handler
= cpu_signal
;
479 sigaction(SIG_IPI
, &sigact
, NULL
);
482 static void dummy_signal(int sig
)
486 static void kvm_block_io_signals(CPUState
*env
)
490 struct sigaction sigact
;
493 sigaddset(&set
, SIGUSR2
);
494 sigaddset(&set
, SIGIO
);
495 sigaddset(&set
, SIGALRM
);
496 sigaddset(&set
, SIGCHLD
);
497 sigaddset(&set
, SIG_IPI
);
498 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
500 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
501 sigdelset(&set
, SIG_IPI
);
503 memset(&sigact
, 0, sizeof(sigact
));
504 sigact
.sa_handler
= dummy_signal
;
505 sigaction(SIG_IPI
, &sigact
, NULL
);
507 r
= kvm_set_signal_mask(env
, &set
);
509 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
514 static void unblock_io_signals(void)
519 sigaddset(&set
, SIGUSR2
);
520 sigaddset(&set
, SIGIO
);
521 sigaddset(&set
, SIGALRM
);
522 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
525 sigaddset(&set
, SIG_IPI
);
526 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
529 static void qemu_signal_lock(unsigned int msecs
)
531 qemu_mutex_lock(&qemu_fair_mutex
);
533 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
534 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
535 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
538 qemu_mutex_unlock(&qemu_fair_mutex
);
541 void qemu_mutex_lock_iothread(void)
544 qemu_mutex_lock(&qemu_fair_mutex
);
545 qemu_mutex_lock(&qemu_global_mutex
);
546 qemu_mutex_unlock(&qemu_fair_mutex
);
548 qemu_signal_lock(100);
551 void qemu_mutex_unlock_iothread(void)
553 qemu_mutex_unlock(&qemu_global_mutex
);
556 static int all_vcpus_paused(void)
558 CPUState
*penv
= first_cpu
;
563 penv
= (CPUState
*)penv
->next_cpu
;
569 void pause_all_vcpus(void)
571 CPUState
*penv
= first_cpu
;
575 qemu_thread_signal(penv
->thread
, SIG_IPI
);
577 penv
= (CPUState
*)penv
->next_cpu
;
580 while (!all_vcpus_paused()) {
581 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
584 qemu_thread_signal(penv
->thread
, SIG_IPI
);
585 penv
= (CPUState
*)penv
->next_cpu
;
590 void resume_all_vcpus(void)
592 CPUState
*penv
= first_cpu
;
597 qemu_thread_signal(penv
->thread
, SIG_IPI
);
599 penv
= (CPUState
*)penv
->next_cpu
;
603 static void tcg_init_vcpu(void *_env
)
605 CPUState
*env
= _env
;
606 /* share a single thread for all cpus with TCG */
607 if (!tcg_cpu_thread
) {
608 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
609 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
610 qemu_cond_init(env
->halt_cond
);
611 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
612 while (env
->created
== 0)
613 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
614 tcg_cpu_thread
= env
->thread
;
615 tcg_halt_cond
= env
->halt_cond
;
617 env
->thread
= tcg_cpu_thread
;
618 env
->halt_cond
= tcg_halt_cond
;
622 static void kvm_start_vcpu(CPUState
*env
)
624 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
625 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
626 qemu_cond_init(env
->halt_cond
);
627 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
628 while (env
->created
== 0)
629 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
632 void qemu_init_vcpu(void *_env
)
634 CPUState
*env
= _env
;
636 env
->nr_cores
= smp_cores
;
637 env
->nr_threads
= smp_threads
;
644 void qemu_notify_event(void)
646 qemu_event_increment();
649 static void qemu_system_vmstop_request(int reason
)
651 vmstop_requested
= reason
;
655 void vm_stop(int reason
)
658 qemu_thread_self(&me
);
660 if (!qemu_thread_equal(&me
, &io_thread
)) {
661 qemu_system_vmstop_request(reason
);
663 * FIXME: should not return to device code in case
664 * vm_stop() has been requested.
666 if (cpu_single_env
) {
667 cpu_exit(cpu_single_env
);
668 cpu_single_env
->stop
= 1;
677 static int qemu_cpu_exec(CPUState
*env
)
680 #ifdef CONFIG_PROFILER
684 #ifdef CONFIG_PROFILER
685 ti
= profile_getclock();
690 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
691 env
->icount_decr
.u16
.low
= 0;
692 env
->icount_extra
= 0;
693 count
= qemu_icount_round (qemu_next_deadline());
694 qemu_icount
+= count
;
695 decr
= (count
> 0xffff) ? 0xffff : count
;
697 env
->icount_decr
.u16
.low
= decr
;
698 env
->icount_extra
= count
;
701 #ifdef CONFIG_PROFILER
702 qemu_time
+= profile_getclock() - ti
;
705 /* Fold pending instructions back into the
706 instruction counter, and clear the interrupt flag. */
707 qemu_icount
-= (env
->icount_decr
.u16
.low
708 + env
->icount_extra
);
709 env
->icount_decr
.u32
= 0;
710 env
->icount_extra
= 0;
715 bool tcg_cpu_exec(void)
719 if (next_cpu
== NULL
)
720 next_cpu
= first_cpu
;
721 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
722 CPUState
*env
= cur_cpu
= next_cpu
;
724 qemu_clock_enable(vm_clock
,
725 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
727 if (qemu_alarm_pending())
729 if (cpu_can_run(env
))
730 ret
= qemu_cpu_exec(env
);
734 if (ret
== EXCP_DEBUG
) {
735 gdb_set_stop_cpu(env
);
736 debug_requested
= EXCP_DEBUG
;
740 return tcg_has_work();
743 void set_numa_modes(void)
748 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
749 for (i
= 0; i
< nb_numa_nodes
; i
++) {
750 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
757 void set_cpu_log(const char *optarg
)
760 const CPULogItem
*item
;
762 mask
= cpu_str_to_log_mask(optarg
);
764 printf("Log items (comma separated):\n");
765 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
766 printf("%-10s %s\n", item
->name
, item
->help
);
773 /* Return the virtual CPU time, based on the instruction counter. */
774 int64_t cpu_get_icount(void)
777 CPUState
*env
= cpu_single_env
;;
779 icount
= qemu_icount
;
781 if (!can_do_io(env
)) {
782 fprintf(stderr
, "Bad clock read\n");
784 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
786 return qemu_icount_bias
+ (icount
<< icount_time_shift
);