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 #include <sys/prctl.h>
42 #define SIG_IPI (SIGRTMIN+4)
44 #define SIG_IPI SIGUSR1
48 #define PR_MCE_KILL 33
51 static CPUState *next_cpu;
53 /***********************************************************/
54 void hw_error(const char *fmt, ...)
60 fprintf(stderr, "qemu: hardware error: ");
61 vfprintf(stderr, fmt, ap);
62 fprintf(stderr, "\n");
63 for(env = first_cpu; env != NULL; env = env->next_cpu) {
64 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
66 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
68 cpu_dump_state(env, stderr, fprintf, 0);
75 void cpu_synchronize_all_states(void)
79 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
80 cpu_synchronize_state(cpu);
84 void cpu_synchronize_all_post_reset(void)
88 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
89 cpu_synchronize_post_reset(cpu);
93 void cpu_synchronize_all_post_init(void)
97 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
98 cpu_synchronize_post_init(cpu);
102 int cpu_is_stopped(CPUState *env)
104 return !vm_running || env->stopped;
107 static void do_vm_stop(int reason)
113 vm_state_notify(0, reason);
114 monitor_protocol_event(QEVENT_STOP, NULL);
118 static int cpu_can_run(CPUState *env)
122 if (env->stopped || !vm_running)
127 static int cpu_has_work(CPUState *env)
131 if (env->queued_work_first)
133 if (env->stopped || !vm_running)
137 if (qemu_cpu_has_work(env))
142 static int any_cpu_has_work(void)
146 for (env = first_cpu; env != NULL; env = env->next_cpu)
147 if (cpu_has_work(env))
152 static void cpu_debug_handler(CPUState *env)
154 gdb_set_stop_cpu(env);
155 debug_requested = EXCP_DEBUG;
160 static int io_thread_fd = -1;
162 static void qemu_event_increment(void)
164 /* Write 8 bytes to be compatible with eventfd. */
165 static const uint64_t val = 1;
168 if (io_thread_fd == -1)
172 ret = write(io_thread_fd, &val, sizeof(val));
173 } while (ret < 0 && errno == EINTR);
175 /* EAGAIN is fine, a read must be pending. */
176 if (ret < 0 && errno != EAGAIN) {
177 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
183 static void qemu_event_read(void *opaque)
185 int fd = (unsigned long)opaque;
189 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
191 len = read(fd, buffer, sizeof(buffer));
192 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
195 static int qemu_event_init(void)
200 err = qemu_eventfd(fds);
204 err = fcntl_setfl(fds[0], O_NONBLOCK);
208 err = fcntl_setfl(fds[1], O_NONBLOCK);
212 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
213 (void *)(unsigned long)fds[0]);
215 io_thread_fd = fds[1];
224 HANDLE qemu_event_handle;
226 static void dummy_event_handler(void *opaque)
230 static int qemu_event_init(void)
232 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
233 if (!qemu_event_handle) {
234 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
237 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
241 static void qemu_event_increment(void)
243 if (!SetEvent(qemu_event_handle)) {
244 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
251 #ifndef CONFIG_IOTHREAD
252 int qemu_init_main_loop(void)
254 cpu_set_debug_excp_handler(cpu_debug_handler);
256 return qemu_event_init();
259 void qemu_main_loop_start(void)
263 void qemu_init_vcpu(void *_env)
265 CPUState *env = _env;
267 env->nr_cores = smp_cores;
268 env->nr_threads = smp_threads;
274 int qemu_cpu_self(void *env)
279 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
284 void resume_all_vcpus(void)
288 void pause_all_vcpus(void)
292 void qemu_cpu_kick(void *env)
297 void qemu_notify_event(void)
299 CPUState *env = cpu_single_env;
301 qemu_event_increment ();
305 if (next_cpu && env != next_cpu) {
310 void qemu_mutex_lock_iothread(void) {}
311 void qemu_mutex_unlock_iothread(void) {}
313 void vm_stop(int reason)
318 #else /* CONFIG_IOTHREAD */
320 #include "qemu-thread.h"
322 QemuMutex qemu_global_mutex;
323 static QemuMutex qemu_fair_mutex;
325 static QemuThread io_thread;
327 static QemuThread *tcg_cpu_thread;
328 static QemuCond *tcg_halt_cond;
330 static int qemu_system_ready;
332 static QemuCond qemu_cpu_cond;
334 static QemuCond qemu_system_cond;
335 static QemuCond qemu_pause_cond;
336 static QemuCond qemu_work_cond;
338 static void tcg_init_ipi(void);
339 static void kvm_init_ipi(CPUState *env);
340 static sigset_t block_io_signals(void);
342 /* If we have signalfd, we mask out the signals we want to handle and then
343 * use signalfd to listen for them. We rely on whatever the current signal
344 * handler is to dispatch the signals when we receive them.
346 static void sigfd_handler(void *opaque)
348 int fd = (unsigned long) opaque;
349 struct qemu_signalfd_siginfo info;
350 struct sigaction action;
355 len = read(fd, &info, sizeof(info));
356 } while (len == -1 && errno == EINTR);
358 if (len == -1 && errno == EAGAIN) {
362 if (len != sizeof(info)) {
363 printf("read from sigfd returned %zd: %m\n", len);
367 sigaction(info.ssi_signo, NULL, &action);
368 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
369 action.sa_sigaction(info.ssi_signo,
370 (siginfo_t *)&info, NULL);
371 } else if (action.sa_handler) {
372 action.sa_handler(info.ssi_signo);
377 static int qemu_signalfd_init(sigset_t mask)
381 sigfd = qemu_signalfd(&mask);
383 fprintf(stderr, "failed to create signalfd\n");
387 fcntl_setfl(sigfd, O_NONBLOCK);
389 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
390 (void *)(unsigned long) sigfd);
395 int qemu_init_main_loop(void)
398 sigset_t blocked_signals;
400 cpu_set_debug_excp_handler(cpu_debug_handler);
402 blocked_signals = block_io_signals();
404 ret = qemu_signalfd_init(blocked_signals);
408 /* Note eventfd must be drained before signalfd handlers run */
409 ret = qemu_event_init();
413 qemu_cond_init(&qemu_pause_cond);
414 qemu_cond_init(&qemu_system_cond);
415 qemu_mutex_init(&qemu_fair_mutex);
416 qemu_mutex_init(&qemu_global_mutex);
417 qemu_mutex_lock(&qemu_global_mutex);
419 qemu_thread_self(&io_thread);
424 void qemu_main_loop_start(void)
426 qemu_system_ready = 1;
427 qemu_cond_broadcast(&qemu_system_cond);
430 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
432 struct qemu_work_item wi;
434 if (qemu_cpu_self(env)) {
441 if (!env->queued_work_first)
442 env->queued_work_first = &wi;
444 env->queued_work_last->next = &wi;
445 env->queued_work_last = &wi;
451 CPUState *self_env = cpu_single_env;
453 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
454 cpu_single_env = self_env;
458 static void flush_queued_work(CPUState *env)
460 struct qemu_work_item *wi;
462 if (!env->queued_work_first)
465 while ((wi = env->queued_work_first)) {
466 env->queued_work_first = wi->next;
470 env->queued_work_last = NULL;
471 qemu_cond_broadcast(&qemu_work_cond);
474 static void qemu_wait_io_event_common(CPUState *env)
479 qemu_cond_signal(&qemu_pause_cond);
481 flush_queued_work(env);
484 static void qemu_tcg_wait_io_event(void)
488 while (!any_cpu_has_work())
489 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
491 qemu_mutex_unlock(&qemu_global_mutex);
494 * Users of qemu_global_mutex can be starved, having no chance
495 * to acquire it since this path will get to it first.
496 * So use another lock to provide fairness.
498 qemu_mutex_lock(&qemu_fair_mutex);
499 qemu_mutex_unlock(&qemu_fair_mutex);
501 qemu_mutex_lock(&qemu_global_mutex);
503 for (env = first_cpu; env != NULL; env = env->next_cpu) {
504 qemu_wait_io_event_common(env);
508 static void sigbus_reraise(void)
511 struct sigaction action;
513 memset(&action, 0, sizeof(action));
514 action.sa_handler = SIG_DFL;
515 if (!sigaction(SIGBUS, &action, NULL)) {
518 sigaddset(&set, SIGBUS);
519 sigprocmask(SIG_UNBLOCK, &set, NULL);
521 perror("Failed to re-raise SIGBUS!\n");
525 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
528 #if defined(TARGET_I386)
529 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr))
534 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
542 ts.tv_sec = timeout / 1000;
543 ts.tv_nsec = (timeout % 1000) * 1000000;
545 sigemptyset(&waitset);
546 sigaddset(&waitset, SIG_IPI);
547 sigaddset(&waitset, SIGBUS);
550 qemu_mutex_unlock(&qemu_global_mutex);
552 r = sigtimedwait(&waitset, &siginfo, &ts);
555 qemu_mutex_lock(&qemu_global_mutex);
557 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
558 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
565 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr))
573 r = sigpending(&chkset);
575 fprintf(stderr, "sigpending: %s\n", strerror(e));
578 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
581 static void qemu_kvm_wait_io_event(CPUState *env)
583 while (!cpu_has_work(env))
584 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
586 qemu_kvm_eat_signal(env, 0);
587 qemu_wait_io_event_common(env);
590 static int qemu_cpu_exec(CPUState *env);
592 static void *kvm_cpu_thread_fn(void *arg)
596 qemu_mutex_lock(&qemu_global_mutex);
597 qemu_thread_self(env->thread);
603 /* signal CPU creation */
605 qemu_cond_signal(&qemu_cpu_cond);
607 /* and wait for machine initialization */
608 while (!qemu_system_ready)
609 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
612 if (cpu_can_run(env))
614 qemu_kvm_wait_io_event(env);
620 static void *tcg_cpu_thread_fn(void *arg)
625 qemu_thread_self(env->thread);
627 /* signal CPU creation */
628 qemu_mutex_lock(&qemu_global_mutex);
629 for (env = first_cpu; env != NULL; env = env->next_cpu)
631 qemu_cond_signal(&qemu_cpu_cond);
633 /* and wait for machine initialization */
634 while (!qemu_system_ready)
635 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
639 qemu_tcg_wait_io_event();
645 void qemu_cpu_kick(void *_env)
647 CPUState *env = _env;
648 qemu_cond_broadcast(env->halt_cond);
649 qemu_thread_signal(env->thread, SIG_IPI);
652 int qemu_cpu_self(void *_env)
654 CPUState *env = _env;
657 qemu_thread_self(&this);
659 return qemu_thread_equal(&this, env->thread);
662 static void cpu_signal(int sig)
665 cpu_exit(cpu_single_env);
669 static void tcg_init_ipi(void)
672 struct sigaction sigact;
674 memset(&sigact, 0, sizeof(sigact));
675 sigact.sa_handler = cpu_signal;
676 sigaction(SIG_IPI, &sigact, NULL);
679 sigaddset(&set, SIG_IPI);
680 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
683 static void dummy_signal(int sig)
687 static void kvm_init_ipi(CPUState *env)
691 struct sigaction sigact;
693 memset(&sigact, 0, sizeof(sigact));
694 sigact.sa_handler = dummy_signal;
695 sigaction(SIG_IPI, &sigact, NULL);
697 pthread_sigmask(SIG_BLOCK, NULL, &set);
698 sigdelset(&set, SIG_IPI);
699 sigdelset(&set, SIGBUS);
700 r = kvm_set_signal_mask(env, &set);
702 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
707 static sigset_t block_io_signals(void)
710 struct sigaction action;
712 /* SIGUSR2 used by posix-aio-compat.c */
714 sigaddset(&set, SIGUSR2);
715 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
718 sigaddset(&set, SIGIO);
719 sigaddset(&set, SIGALRM);
720 sigaddset(&set, SIG_IPI);
721 sigaddset(&set, SIGBUS);
722 pthread_sigmask(SIG_BLOCK, &set, NULL);
724 memset(&action, 0, sizeof(action));
725 action.sa_flags = SA_SIGINFO;
726 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
727 sigaction(SIGBUS, &action, NULL);
728 prctl(PR_MCE_KILL, 1, 1, 0, 0);
733 void qemu_mutex_lock_iothread(void)
736 qemu_mutex_lock(&qemu_fair_mutex);
737 qemu_mutex_lock(&qemu_global_mutex);
738 qemu_mutex_unlock(&qemu_fair_mutex);
740 qemu_mutex_lock(&qemu_fair_mutex);
741 if (qemu_mutex_trylock(&qemu_global_mutex)) {
742 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
743 qemu_mutex_lock(&qemu_global_mutex);
745 qemu_mutex_unlock(&qemu_fair_mutex);
749 void qemu_mutex_unlock_iothread(void)
751 qemu_mutex_unlock(&qemu_global_mutex);
754 static int all_vcpus_paused(void)
756 CPUState *penv = first_cpu;
761 penv = (CPUState *)penv->next_cpu;
767 void pause_all_vcpus(void)
769 CPUState *penv = first_cpu;
774 penv = (CPUState *)penv->next_cpu;
777 while (!all_vcpus_paused()) {
778 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
782 penv = (CPUState *)penv->next_cpu;
787 void resume_all_vcpus(void)
789 CPUState *penv = first_cpu;
795 penv = (CPUState *)penv->next_cpu;
799 static void tcg_init_vcpu(void *_env)
801 CPUState *env = _env;
802 /* share a single thread for all cpus with TCG */
803 if (!tcg_cpu_thread) {
804 env->thread = qemu_mallocz(sizeof(QemuThread));
805 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
806 qemu_cond_init(env->halt_cond);
807 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
808 while (env->created == 0)
809 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
810 tcg_cpu_thread = env->thread;
811 tcg_halt_cond = env->halt_cond;
813 env->thread = tcg_cpu_thread;
814 env->halt_cond = tcg_halt_cond;
818 static void kvm_start_vcpu(CPUState *env)
820 env->thread = qemu_mallocz(sizeof(QemuThread));
821 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
822 qemu_cond_init(env->halt_cond);
823 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
824 while (env->created == 0)
825 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
828 void qemu_init_vcpu(void *_env)
830 CPUState *env = _env;
832 env->nr_cores = smp_cores;
833 env->nr_threads = smp_threads;
840 void qemu_notify_event(void)
842 qemu_event_increment();
845 static void qemu_system_vmstop_request(int reason)
847 vmstop_requested = reason;
851 void vm_stop(int reason)
854 qemu_thread_self(&me);
856 if (!qemu_thread_equal(&me, &io_thread)) {
857 qemu_system_vmstop_request(reason);
859 * FIXME: should not return to device code in case
860 * vm_stop() has been requested.
862 if (cpu_single_env) {
863 cpu_exit(cpu_single_env);
864 cpu_single_env->stop = 1;
873 static int qemu_cpu_exec(CPUState *env)
876 #ifdef CONFIG_PROFILER
880 #ifdef CONFIG_PROFILER
881 ti = profile_getclock();
886 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
887 env->icount_decr.u16.low = 0;
888 env->icount_extra = 0;
889 count = qemu_icount_round (qemu_next_deadline());
890 qemu_icount += count;
891 decr = (count > 0xffff) ? 0xffff : count;
893 env->icount_decr.u16.low = decr;
894 env->icount_extra = count;
897 #ifdef CONFIG_PROFILER
898 qemu_time += profile_getclock() - ti;
901 /* Fold pending instructions back into the
902 instruction counter, and clear the interrupt flag. */
903 qemu_icount -= (env->icount_decr.u16.low
904 + env->icount_extra);
905 env->icount_decr.u32 = 0;
906 env->icount_extra = 0;
911 bool cpu_exec_all(void)
913 if (next_cpu == NULL)
914 next_cpu = first_cpu;
915 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
916 CPUState *env = next_cpu;
918 qemu_clock_enable(vm_clock,
919 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
921 if (qemu_alarm_pending())
923 if (cpu_can_run(env)) {
924 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
927 } else if (env->stop) {
932 return any_cpu_has_work();
935 void set_numa_modes(void)
940 for (env = first_cpu; env != NULL; env = env->next_cpu) {
941 for (i = 0; i < nb_numa_nodes; i++) {
942 if (node_cpumask[i] & (1 << env->cpu_index)) {
949 void set_cpu_log(const char *optarg)
952 const CPULogItem *item;
954 mask = cpu_str_to_log_mask(optarg);
956 printf("Log items (comma separated):\n");
957 for (item = cpu_log_items; item->mask != 0; item++) {
958 printf("%-10s %s\n", item->name, item->help);
965 /* Return the virtual CPU time, based on the instruction counter. */
966 int64_t cpu_get_icount(void)
969 CPUState *env = cpu_single_env;;
971 icount = qemu_icount;
973 if (!can_do_io(env)) {
974 fprintf(stderr, "Bad clock read\n");
976 icount -= (env->icount_decr.u16.low + env->icount_extra);
978 return qemu_icount_bias + (icount << icount_time_shift);
981 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
983 /* XXX: implement xxx_cpu_list for targets that still miss it */
984 #if defined(cpu_list_id)
985 cpu_list_id(f, cpu_fprintf, optarg);
986 #elif defined(cpu_list)
987 cpu_list(f, cpu_fprintf); /* deprecated */