Do not include compatfd for WIN32
[qemu/stefanha.git] / cpus.c
blobded21007e7b16e07f2b8a9d83df1da80b66a0890
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
25 /* Needed early for CONFIG_BSD etc. */
26 #include "config-host.h"
28 #include "monitor.h"
29 #include "sysemu.h"
30 #include "gdbstub.h"
31 #include "dma.h"
32 #include "kvm.h"
33 #include "exec-all.h"
35 #include "qemu-thread.h"
36 #include "cpus.h"
38 #ifndef _WIN32
39 #include "compatfd.h"
40 #endif
42 #ifdef SIGRTMIN
43 #define SIG_IPI (SIGRTMIN+4)
44 #else
45 #define SIG_IPI SIGUSR1
46 #endif
48 #ifdef CONFIG_LINUX
50 #include <sys/prctl.h>
52 #ifndef PR_MCE_KILL
53 #define PR_MCE_KILL 33
54 #endif
56 #ifndef PR_MCE_KILL_SET
57 #define PR_MCE_KILL_SET 1
58 #endif
60 #ifndef PR_MCE_KILL_EARLY
61 #define PR_MCE_KILL_EARLY 1
62 #endif
64 #endif /* CONFIG_LINUX */
66 static CPUState *next_cpu;
68 /***********************************************************/
69 void hw_error(const char *fmt, ...)
71 va_list ap;
72 CPUState *env;
74 va_start(ap, fmt);
75 fprintf(stderr, "qemu: hardware error: ");
76 vfprintf(stderr, fmt, ap);
77 fprintf(stderr, "\n");
78 for(env = first_cpu; env != NULL; env = env->next_cpu) {
79 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
80 #ifdef TARGET_I386
81 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
82 #else
83 cpu_dump_state(env, stderr, fprintf, 0);
84 #endif
86 va_end(ap);
87 abort();
90 void cpu_synchronize_all_states(void)
92 CPUState *cpu;
94 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
95 cpu_synchronize_state(cpu);
99 void cpu_synchronize_all_post_reset(void)
101 CPUState *cpu;
103 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
104 cpu_synchronize_post_reset(cpu);
108 void cpu_synchronize_all_post_init(void)
110 CPUState *cpu;
112 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
113 cpu_synchronize_post_init(cpu);
117 int cpu_is_stopped(CPUState *env)
119 return !vm_running || env->stopped;
122 static void do_vm_stop(int reason)
124 if (vm_running) {
125 cpu_disable_ticks();
126 vm_running = 0;
127 pause_all_vcpus();
128 vm_state_notify(0, reason);
129 qemu_aio_flush();
130 bdrv_flush_all();
131 monitor_protocol_event(QEVENT_STOP, NULL);
135 static int cpu_can_run(CPUState *env)
137 if (env->stop) {
138 return 0;
140 if (env->stopped || !vm_running) {
141 return 0;
143 return 1;
146 static bool cpu_thread_is_idle(CPUState *env)
148 if (env->stop || env->queued_work_first) {
149 return false;
151 if (env->stopped || !vm_running) {
152 return true;
154 if (!env->halted || qemu_cpu_has_work(env) ||
155 (kvm_enabled() && kvm_irqchip_in_kernel())) {
156 return false;
158 return true;
161 bool all_cpu_threads_idle(void)
163 CPUState *env;
165 for (env = first_cpu; env != NULL; env = env->next_cpu) {
166 if (!cpu_thread_is_idle(env)) {
167 return false;
170 return true;
173 static void cpu_handle_guest_debug(CPUState *env)
175 gdb_set_stop_cpu(env);
176 qemu_system_debug_request();
177 #ifdef CONFIG_IOTHREAD
178 env->stopped = 1;
179 #endif
182 #ifdef CONFIG_IOTHREAD
183 static void cpu_signal(int sig)
185 if (cpu_single_env) {
186 cpu_exit(cpu_single_env);
188 exit_request = 1;
190 #endif
192 #ifdef CONFIG_LINUX
193 static void sigbus_reraise(void)
195 sigset_t set;
196 struct sigaction action;
198 memset(&action, 0, sizeof(action));
199 action.sa_handler = SIG_DFL;
200 if (!sigaction(SIGBUS, &action, NULL)) {
201 raise(SIGBUS);
202 sigemptyset(&set);
203 sigaddset(&set, SIGBUS);
204 sigprocmask(SIG_UNBLOCK, &set, NULL);
206 perror("Failed to re-raise SIGBUS!\n");
207 abort();
210 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
211 void *ctx)
213 if (kvm_on_sigbus(siginfo->ssi_code,
214 (void *)(intptr_t)siginfo->ssi_addr)) {
215 sigbus_reraise();
219 static void qemu_init_sigbus(void)
221 struct sigaction action;
223 memset(&action, 0, sizeof(action));
224 action.sa_flags = SA_SIGINFO;
225 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
226 sigaction(SIGBUS, &action, NULL);
228 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
231 static void qemu_kvm_eat_signals(CPUState *env)
233 struct timespec ts = { 0, 0 };
234 siginfo_t siginfo;
235 sigset_t waitset;
236 sigset_t chkset;
237 int r;
239 sigemptyset(&waitset);
240 sigaddset(&waitset, SIG_IPI);
241 sigaddset(&waitset, SIGBUS);
243 do {
244 r = sigtimedwait(&waitset, &siginfo, &ts);
245 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
246 perror("sigtimedwait");
247 exit(1);
250 switch (r) {
251 case SIGBUS:
252 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
253 sigbus_reraise();
255 break;
256 default:
257 break;
260 r = sigpending(&chkset);
261 if (r == -1) {
262 perror("sigpending");
263 exit(1);
265 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
267 #ifndef CONFIG_IOTHREAD
268 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
269 qemu_notify_event();
271 #endif
274 #else /* !CONFIG_LINUX */
276 static void qemu_init_sigbus(void)
280 static void qemu_kvm_eat_signals(CPUState *env)
283 #endif /* !CONFIG_LINUX */
285 #ifndef _WIN32
286 static int io_thread_fd = -1;
288 static void qemu_event_increment(void)
290 /* Write 8 bytes to be compatible with eventfd. */
291 static const uint64_t val = 1;
292 ssize_t ret;
294 if (io_thread_fd == -1) {
295 return;
297 do {
298 ret = write(io_thread_fd, &val, sizeof(val));
299 } while (ret < 0 && errno == EINTR);
301 /* EAGAIN is fine, a read must be pending. */
302 if (ret < 0 && errno != EAGAIN) {
303 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
304 strerror(errno));
305 exit (1);
309 static void qemu_event_read(void *opaque)
311 int fd = (intptr_t)opaque;
312 ssize_t len;
313 char buffer[512];
315 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
316 do {
317 len = read(fd, buffer, sizeof(buffer));
318 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
321 static int qemu_event_init(void)
323 int err;
324 int fds[2];
326 err = qemu_eventfd(fds);
327 if (err == -1) {
328 return -errno;
330 err = fcntl_setfl(fds[0], O_NONBLOCK);
331 if (err < 0) {
332 goto fail;
334 err = fcntl_setfl(fds[1], O_NONBLOCK);
335 if (err < 0) {
336 goto fail;
338 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
339 (void *)(intptr_t)fds[0]);
341 io_thread_fd = fds[1];
342 return 0;
344 fail:
345 close(fds[0]);
346 close(fds[1]);
347 return err;
350 static void dummy_signal(int sig)
354 /* If we have signalfd, we mask out the signals we want to handle and then
355 * use signalfd to listen for them. We rely on whatever the current signal
356 * handler is to dispatch the signals when we receive them.
358 static void sigfd_handler(void *opaque)
360 int fd = (intptr_t)opaque;
361 struct qemu_signalfd_siginfo info;
362 struct sigaction action;
363 ssize_t len;
365 while (1) {
366 do {
367 len = read(fd, &info, sizeof(info));
368 } while (len == -1 && errno == EINTR);
370 if (len == -1 && errno == EAGAIN) {
371 break;
374 if (len != sizeof(info)) {
375 printf("read from sigfd returned %zd: %m\n", len);
376 return;
379 sigaction(info.ssi_signo, NULL, &action);
380 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
381 action.sa_sigaction(info.ssi_signo,
382 (siginfo_t *)&info, NULL);
383 } else if (action.sa_handler) {
384 action.sa_handler(info.ssi_signo);
389 static int qemu_signal_init(void)
391 int sigfd;
392 sigset_t set;
394 #ifdef CONFIG_IOTHREAD
395 /* SIGUSR2 used by posix-aio-compat.c */
396 sigemptyset(&set);
397 sigaddset(&set, SIGUSR2);
398 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
400 sigemptyset(&set);
401 sigaddset(&set, SIGIO);
402 sigaddset(&set, SIGALRM);
403 sigaddset(&set, SIG_IPI);
404 sigaddset(&set, SIGBUS);
405 pthread_sigmask(SIG_BLOCK, &set, NULL);
406 #else
407 sigemptyset(&set);
408 sigaddset(&set, SIGBUS);
409 if (kvm_enabled()) {
411 * We need to process timer signals synchronously to avoid a race
412 * between exit_request check and KVM vcpu entry.
414 sigaddset(&set, SIGIO);
415 sigaddset(&set, SIGALRM);
417 #endif
419 sigfd = qemu_signalfd(&set);
420 if (sigfd == -1) {
421 fprintf(stderr, "failed to create signalfd\n");
422 return -errno;
425 fcntl_setfl(sigfd, O_NONBLOCK);
427 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
428 (void *)(intptr_t)sigfd);
430 return 0;
433 static void qemu_kvm_init_cpu_signals(CPUState *env)
435 int r;
436 sigset_t set;
437 struct sigaction sigact;
439 memset(&sigact, 0, sizeof(sigact));
440 sigact.sa_handler = dummy_signal;
441 sigaction(SIG_IPI, &sigact, NULL);
443 #ifdef CONFIG_IOTHREAD
444 pthread_sigmask(SIG_BLOCK, NULL, &set);
445 sigdelset(&set, SIG_IPI);
446 sigdelset(&set, SIGBUS);
447 r = kvm_set_signal_mask(env, &set);
448 if (r) {
449 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
450 exit(1);
452 #else
453 sigemptyset(&set);
454 sigaddset(&set, SIG_IPI);
455 sigaddset(&set, SIGIO);
456 sigaddset(&set, SIGALRM);
457 pthread_sigmask(SIG_BLOCK, &set, NULL);
459 pthread_sigmask(SIG_BLOCK, NULL, &set);
460 sigdelset(&set, SIGIO);
461 sigdelset(&set, SIGALRM);
462 #endif
463 sigdelset(&set, SIG_IPI);
464 sigdelset(&set, SIGBUS);
465 r = kvm_set_signal_mask(env, &set);
466 if (r) {
467 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
468 exit(1);
472 static void qemu_tcg_init_cpu_signals(void)
474 #ifdef CONFIG_IOTHREAD
475 sigset_t set;
476 struct sigaction sigact;
478 memset(&sigact, 0, sizeof(sigact));
479 sigact.sa_handler = cpu_signal;
480 sigaction(SIG_IPI, &sigact, NULL);
482 sigemptyset(&set);
483 sigaddset(&set, SIG_IPI);
484 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
485 #endif
488 #else /* _WIN32 */
490 HANDLE qemu_event_handle;
492 static void dummy_event_handler(void *opaque)
496 static int qemu_event_init(void)
498 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
499 if (!qemu_event_handle) {
500 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
501 return -1;
503 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
504 return 0;
507 static void qemu_event_increment(void)
509 if (!SetEvent(qemu_event_handle)) {
510 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
511 GetLastError());
512 exit (1);
516 static int qemu_signal_init(void)
518 return 0;
521 static void qemu_kvm_init_cpu_signals(CPUState *env)
523 abort();
526 static void qemu_tcg_init_cpu_signals(void)
529 #endif /* _WIN32 */
531 #ifndef CONFIG_IOTHREAD
532 int qemu_init_main_loop(void)
534 int ret;
536 ret = qemu_signal_init();
537 if (ret) {
538 return ret;
541 qemu_init_sigbus();
543 return qemu_event_init();
546 void qemu_main_loop_start(void)
550 void qemu_init_vcpu(void *_env)
552 CPUState *env = _env;
553 int r;
555 env->nr_cores = smp_cores;
556 env->nr_threads = smp_threads;
558 if (kvm_enabled()) {
559 r = kvm_init_vcpu(env);
560 if (r < 0) {
561 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
562 exit(1);
564 qemu_kvm_init_cpu_signals(env);
565 } else {
566 qemu_tcg_init_cpu_signals();
570 int qemu_cpu_is_self(void *env)
572 return 1;
575 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
577 func(data);
580 void resume_all_vcpus(void)
584 void pause_all_vcpus(void)
588 void qemu_cpu_kick(void *env)
592 void qemu_cpu_kick_self(void)
594 #ifndef _WIN32
595 assert(cpu_single_env);
597 raise(SIG_IPI);
598 #else
599 abort();
600 #endif
603 void qemu_notify_event(void)
605 CPUState *env = cpu_single_env;
607 qemu_event_increment ();
608 if (env) {
609 cpu_exit(env);
611 if (next_cpu && env != next_cpu) {
612 cpu_exit(next_cpu);
614 exit_request = 1;
617 void qemu_mutex_lock_iothread(void) {}
618 void qemu_mutex_unlock_iothread(void) {}
620 void cpu_stop_current(void)
624 void vm_stop(int reason)
626 do_vm_stop(reason);
629 #else /* CONFIG_IOTHREAD */
631 QemuMutex qemu_global_mutex;
632 static QemuMutex qemu_fair_mutex;
634 static QemuThread io_thread;
636 static QemuThread *tcg_cpu_thread;
637 static QemuCond *tcg_halt_cond;
639 static int qemu_system_ready;
640 /* cpu creation */
641 static QemuCond qemu_cpu_cond;
642 /* system init */
643 static QemuCond qemu_system_cond;
644 static QemuCond qemu_pause_cond;
645 static QemuCond qemu_work_cond;
647 int qemu_init_main_loop(void)
649 int ret;
651 qemu_init_sigbus();
653 ret = qemu_signal_init();
654 if (ret) {
655 return ret;
658 /* Note eventfd must be drained before signalfd handlers run */
659 ret = qemu_event_init();
660 if (ret) {
661 return ret;
664 qemu_cond_init(&qemu_cpu_cond);
665 qemu_cond_init(&qemu_system_cond);
666 qemu_cond_init(&qemu_pause_cond);
667 qemu_cond_init(&qemu_work_cond);
668 qemu_mutex_init(&qemu_fair_mutex);
669 qemu_mutex_init(&qemu_global_mutex);
670 qemu_mutex_lock(&qemu_global_mutex);
672 qemu_thread_get_self(&io_thread);
674 return 0;
677 void qemu_main_loop_start(void)
679 qemu_system_ready = 1;
680 qemu_cond_broadcast(&qemu_system_cond);
683 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
685 struct qemu_work_item wi;
687 if (qemu_cpu_is_self(env)) {
688 func(data);
689 return;
692 wi.func = func;
693 wi.data = data;
694 if (!env->queued_work_first) {
695 env->queued_work_first = &wi;
696 } else {
697 env->queued_work_last->next = &wi;
699 env->queued_work_last = &wi;
700 wi.next = NULL;
701 wi.done = false;
703 qemu_cpu_kick(env);
704 while (!wi.done) {
705 CPUState *self_env = cpu_single_env;
707 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
708 cpu_single_env = self_env;
712 static void flush_queued_work(CPUState *env)
714 struct qemu_work_item *wi;
716 if (!env->queued_work_first) {
717 return;
720 while ((wi = env->queued_work_first)) {
721 env->queued_work_first = wi->next;
722 wi->func(wi->data);
723 wi->done = true;
725 env->queued_work_last = NULL;
726 qemu_cond_broadcast(&qemu_work_cond);
729 static void qemu_wait_io_event_common(CPUState *env)
731 if (env->stop) {
732 env->stop = 0;
733 env->stopped = 1;
734 qemu_cond_signal(&qemu_pause_cond);
736 flush_queued_work(env);
737 env->thread_kicked = false;
740 static void qemu_tcg_wait_io_event(void)
742 CPUState *env;
744 while (all_cpu_threads_idle()) {
745 /* Start accounting real time to the virtual clock if the CPUs
746 are idle. */
747 qemu_clock_warp(vm_clock);
748 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
751 qemu_mutex_unlock(&qemu_global_mutex);
754 * Users of qemu_global_mutex can be starved, having no chance
755 * to acquire it since this path will get to it first.
756 * So use another lock to provide fairness.
758 qemu_mutex_lock(&qemu_fair_mutex);
759 qemu_mutex_unlock(&qemu_fair_mutex);
761 qemu_mutex_lock(&qemu_global_mutex);
763 for (env = first_cpu; env != NULL; env = env->next_cpu) {
764 qemu_wait_io_event_common(env);
768 static void qemu_kvm_wait_io_event(CPUState *env)
770 while (cpu_thread_is_idle(env)) {
771 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
774 qemu_kvm_eat_signals(env);
775 qemu_wait_io_event_common(env);
778 static void *qemu_kvm_cpu_thread_fn(void *arg)
780 CPUState *env = arg;
781 int r;
783 qemu_mutex_lock(&qemu_global_mutex);
784 qemu_thread_get_self(env->thread);
785 env->thread_id = qemu_get_thread_id();
787 r = kvm_init_vcpu(env);
788 if (r < 0) {
789 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
790 exit(1);
793 qemu_kvm_init_cpu_signals(env);
795 /* signal CPU creation */
796 env->created = 1;
797 qemu_cond_signal(&qemu_cpu_cond);
799 /* and wait for machine initialization */
800 while (!qemu_system_ready) {
801 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
804 while (1) {
805 if (cpu_can_run(env)) {
806 r = kvm_cpu_exec(env);
807 if (r == EXCP_DEBUG) {
808 cpu_handle_guest_debug(env);
811 qemu_kvm_wait_io_event(env);
814 return NULL;
817 static void *qemu_tcg_cpu_thread_fn(void *arg)
819 CPUState *env = arg;
821 qemu_tcg_init_cpu_signals();
822 qemu_thread_get_self(env->thread);
824 /* signal CPU creation */
825 qemu_mutex_lock(&qemu_global_mutex);
826 for (env = first_cpu; env != NULL; env = env->next_cpu) {
827 env->thread_id = qemu_get_thread_id();
828 env->created = 1;
830 qemu_cond_signal(&qemu_cpu_cond);
832 /* and wait for machine initialization */
833 while (!qemu_system_ready) {
834 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
837 while (1) {
838 cpu_exec_all();
839 if (use_icount && qemu_next_icount_deadline() <= 0) {
840 qemu_notify_event();
842 qemu_tcg_wait_io_event();
845 return NULL;
848 static void qemu_cpu_kick_thread(CPUState *env)
850 #ifndef _WIN32
851 int err;
853 err = pthread_kill(env->thread->thread, SIG_IPI);
854 if (err) {
855 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
856 exit(1);
858 #else /* _WIN32 */
859 if (!qemu_cpu_is_self(env)) {
860 SuspendThread(env->thread->thread);
861 cpu_signal(0);
862 ResumeThread(env->thread->thread);
864 #endif
867 void qemu_cpu_kick(void *_env)
869 CPUState *env = _env;
871 qemu_cond_broadcast(env->halt_cond);
872 if (!env->thread_kicked) {
873 qemu_cpu_kick_thread(env);
874 env->thread_kicked = true;
878 void qemu_cpu_kick_self(void)
880 #ifndef _WIN32
881 assert(cpu_single_env);
883 if (!cpu_single_env->thread_kicked) {
884 qemu_cpu_kick_thread(cpu_single_env);
885 cpu_single_env->thread_kicked = true;
887 #else
888 abort();
889 #endif
892 int qemu_cpu_is_self(void *_env)
894 CPUState *env = _env;
896 return qemu_thread_is_self(env->thread);
899 void qemu_mutex_lock_iothread(void)
901 if (kvm_enabled()) {
902 qemu_mutex_lock(&qemu_global_mutex);
903 } else {
904 qemu_mutex_lock(&qemu_fair_mutex);
905 if (qemu_mutex_trylock(&qemu_global_mutex)) {
906 qemu_cpu_kick_thread(first_cpu);
907 qemu_mutex_lock(&qemu_global_mutex);
909 qemu_mutex_unlock(&qemu_fair_mutex);
913 void qemu_mutex_unlock_iothread(void)
915 qemu_mutex_unlock(&qemu_global_mutex);
918 static int all_vcpus_paused(void)
920 CPUState *penv = first_cpu;
922 while (penv) {
923 if (!penv->stopped) {
924 return 0;
926 penv = (CPUState *)penv->next_cpu;
929 return 1;
932 void pause_all_vcpus(void)
934 CPUState *penv = first_cpu;
936 while (penv) {
937 penv->stop = 1;
938 qemu_cpu_kick(penv);
939 penv = (CPUState *)penv->next_cpu;
942 while (!all_vcpus_paused()) {
943 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
944 penv = first_cpu;
945 while (penv) {
946 qemu_cpu_kick(penv);
947 penv = (CPUState *)penv->next_cpu;
952 void resume_all_vcpus(void)
954 CPUState *penv = first_cpu;
956 while (penv) {
957 penv->stop = 0;
958 penv->stopped = 0;
959 qemu_cpu_kick(penv);
960 penv = (CPUState *)penv->next_cpu;
964 static void qemu_tcg_init_vcpu(void *_env)
966 CPUState *env = _env;
968 /* share a single thread for all cpus with TCG */
969 if (!tcg_cpu_thread) {
970 env->thread = qemu_mallocz(sizeof(QemuThread));
971 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
972 qemu_cond_init(env->halt_cond);
973 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
974 while (env->created == 0) {
975 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
977 tcg_cpu_thread = env->thread;
978 tcg_halt_cond = env->halt_cond;
979 } else {
980 env->thread = tcg_cpu_thread;
981 env->halt_cond = tcg_halt_cond;
985 static void qemu_kvm_start_vcpu(CPUState *env)
987 env->thread = qemu_mallocz(sizeof(QemuThread));
988 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
989 qemu_cond_init(env->halt_cond);
990 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
991 while (env->created == 0) {
992 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
996 void qemu_init_vcpu(void *_env)
998 CPUState *env = _env;
1000 env->nr_cores = smp_cores;
1001 env->nr_threads = smp_threads;
1002 if (kvm_enabled()) {
1003 qemu_kvm_start_vcpu(env);
1004 } else {
1005 qemu_tcg_init_vcpu(env);
1009 void qemu_notify_event(void)
1011 qemu_event_increment();
1014 void cpu_stop_current(void)
1016 if (cpu_single_env) {
1017 cpu_single_env->stop = 0;
1018 cpu_single_env->stopped = 1;
1019 cpu_exit(cpu_single_env);
1020 qemu_cond_signal(&qemu_pause_cond);
1024 void vm_stop(int reason)
1026 if (!qemu_thread_is_self(&io_thread)) {
1027 qemu_system_vmstop_request(reason);
1029 * FIXME: should not return to device code in case
1030 * vm_stop() has been requested.
1032 cpu_stop_current();
1033 return;
1035 do_vm_stop(reason);
1038 #endif
1040 static int tcg_cpu_exec(CPUState *env)
1042 int ret;
1043 #ifdef CONFIG_PROFILER
1044 int64_t ti;
1045 #endif
1047 #ifdef CONFIG_PROFILER
1048 ti = profile_getclock();
1049 #endif
1050 if (use_icount) {
1051 int64_t count;
1052 int decr;
1053 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1054 env->icount_decr.u16.low = 0;
1055 env->icount_extra = 0;
1056 count = qemu_icount_round(qemu_next_icount_deadline());
1057 qemu_icount += count;
1058 decr = (count > 0xffff) ? 0xffff : count;
1059 count -= decr;
1060 env->icount_decr.u16.low = decr;
1061 env->icount_extra = count;
1063 ret = cpu_exec(env);
1064 #ifdef CONFIG_PROFILER
1065 qemu_time += profile_getclock() - ti;
1066 #endif
1067 if (use_icount) {
1068 /* Fold pending instructions back into the
1069 instruction counter, and clear the interrupt flag. */
1070 qemu_icount -= (env->icount_decr.u16.low
1071 + env->icount_extra);
1072 env->icount_decr.u32 = 0;
1073 env->icount_extra = 0;
1075 return ret;
1078 bool cpu_exec_all(void)
1080 int r;
1082 /* Account partial waits to the vm_clock. */
1083 qemu_clock_warp(vm_clock);
1085 if (next_cpu == NULL) {
1086 next_cpu = first_cpu;
1088 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1089 CPUState *env = next_cpu;
1091 qemu_clock_enable(vm_clock,
1092 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1094 #ifndef CONFIG_IOTHREAD
1095 if (qemu_alarm_pending()) {
1096 break;
1098 #endif
1099 if (cpu_can_run(env)) {
1100 if (kvm_enabled()) {
1101 r = kvm_cpu_exec(env);
1102 qemu_kvm_eat_signals(env);
1103 } else {
1104 r = tcg_cpu_exec(env);
1106 if (r == EXCP_DEBUG) {
1107 cpu_handle_guest_debug(env);
1108 break;
1110 } else if (env->stop || env->stopped) {
1111 break;
1114 exit_request = 0;
1115 return !all_cpu_threads_idle();
1118 void set_numa_modes(void)
1120 CPUState *env;
1121 int i;
1123 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1124 for (i = 0; i < nb_numa_nodes; i++) {
1125 if (node_cpumask[i] & (1 << env->cpu_index)) {
1126 env->numa_node = i;
1132 void set_cpu_log(const char *optarg)
1134 int mask;
1135 const CPULogItem *item;
1137 mask = cpu_str_to_log_mask(optarg);
1138 if (!mask) {
1139 printf("Log items (comma separated):\n");
1140 for (item = cpu_log_items; item->mask != 0; item++) {
1141 printf("%-10s %s\n", item->name, item->help);
1143 exit(1);
1145 cpu_set_log(mask);
1148 void set_cpu_log_filename(const char *optarg)
1150 cpu_set_log_filename(optarg);
1153 /* Return the virtual CPU time, based on the instruction counter. */
1154 int64_t cpu_get_icount(void)
1156 int64_t icount;
1157 CPUState *env = cpu_single_env;;
1159 icount = qemu_icount;
1160 if (env) {
1161 if (!can_do_io(env)) {
1162 fprintf(stderr, "Bad clock read\n");
1164 icount -= (env->icount_decr.u16.low + env->icount_extra);
1166 return qemu_icount_bias + (icount << icount_time_shift);
1169 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1171 /* XXX: implement xxx_cpu_list for targets that still miss it */
1172 #if defined(cpu_list_id)
1173 cpu_list_id(f, cpu_fprintf, optarg);
1174 #elif defined(cpu_list)
1175 cpu_list(f, cpu_fprintf); /* deprecated */
1176 #endif