Delegate setup of TCG temporaries to targets
[qemu/mdroth.git] / cpus.c
blobe2997941fa3b0c4586ca9603d94dad82c876a8b8
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"
34 #include "qemu-thread.h"
35 #include "cpus.h"
36 #include "compatfd.h"
38 #ifdef SIGRTMIN
39 #define SIG_IPI (SIGRTMIN+4)
40 #else
41 #define SIG_IPI SIGUSR1
42 #endif
44 #ifdef CONFIG_LINUX
46 #include <sys/prctl.h>
48 #ifndef PR_MCE_KILL
49 #define PR_MCE_KILL 33
50 #endif
52 #ifndef PR_MCE_KILL_SET
53 #define PR_MCE_KILL_SET 1
54 #endif
56 #ifndef PR_MCE_KILL_EARLY
57 #define PR_MCE_KILL_EARLY 1
58 #endif
60 #endif /* CONFIG_LINUX */
62 static CPUState *next_cpu;
64 /***********************************************************/
65 void hw_error(const char *fmt, ...)
67 va_list ap;
68 CPUState *env;
70 va_start(ap, fmt);
71 fprintf(stderr, "qemu: hardware error: ");
72 vfprintf(stderr, fmt, ap);
73 fprintf(stderr, "\n");
74 for(env = first_cpu; env != NULL; env = env->next_cpu) {
75 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
76 #ifdef TARGET_I386
77 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
78 #else
79 cpu_dump_state(env, stderr, fprintf, 0);
80 #endif
82 va_end(ap);
83 abort();
86 void cpu_synchronize_all_states(void)
88 CPUState *cpu;
90 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
91 cpu_synchronize_state(cpu);
95 void cpu_synchronize_all_post_reset(void)
97 CPUState *cpu;
99 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
100 cpu_synchronize_post_reset(cpu);
104 void cpu_synchronize_all_post_init(void)
106 CPUState *cpu;
108 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
109 cpu_synchronize_post_init(cpu);
113 int cpu_is_stopped(CPUState *env)
115 return !vm_running || env->stopped;
118 static void do_vm_stop(int reason)
120 if (vm_running) {
121 cpu_disable_ticks();
122 vm_running = 0;
123 pause_all_vcpus();
124 vm_state_notify(0, reason);
125 qemu_aio_flush();
126 bdrv_flush_all();
127 monitor_protocol_event(QEVENT_STOP, NULL);
131 static int cpu_can_run(CPUState *env)
133 if (env->stop) {
134 return 0;
136 if (env->stopped || !vm_running) {
137 return 0;
139 return 1;
142 static bool cpu_thread_is_idle(CPUState *env)
144 if (env->stop || env->queued_work_first) {
145 return false;
147 if (env->stopped || !vm_running) {
148 return true;
150 if (!env->halted || qemu_cpu_has_work(env) ||
151 (kvm_enabled() && kvm_irqchip_in_kernel())) {
152 return false;
154 return true;
157 bool all_cpu_threads_idle(void)
159 CPUState *env;
161 for (env = first_cpu; env != NULL; env = env->next_cpu) {
162 if (!cpu_thread_is_idle(env)) {
163 return false;
166 return true;
169 static void cpu_handle_guest_debug(CPUState *env)
171 gdb_set_stop_cpu(env);
172 qemu_system_debug_request();
173 #ifdef CONFIG_IOTHREAD
174 env->stopped = 1;
175 #endif
178 #ifdef CONFIG_IOTHREAD
179 static void cpu_signal(int sig)
181 if (cpu_single_env) {
182 cpu_exit(cpu_single_env);
184 exit_request = 1;
186 #endif
188 #ifdef CONFIG_LINUX
189 static void sigbus_reraise(void)
191 sigset_t set;
192 struct sigaction action;
194 memset(&action, 0, sizeof(action));
195 action.sa_handler = SIG_DFL;
196 if (!sigaction(SIGBUS, &action, NULL)) {
197 raise(SIGBUS);
198 sigemptyset(&set);
199 sigaddset(&set, SIGBUS);
200 sigprocmask(SIG_UNBLOCK, &set, NULL);
202 perror("Failed to re-raise SIGBUS!\n");
203 abort();
206 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
207 void *ctx)
209 if (kvm_on_sigbus(siginfo->ssi_code,
210 (void *)(intptr_t)siginfo->ssi_addr)) {
211 sigbus_reraise();
215 static void qemu_init_sigbus(void)
217 struct sigaction action;
219 memset(&action, 0, sizeof(action));
220 action.sa_flags = SA_SIGINFO;
221 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
222 sigaction(SIGBUS, &action, NULL);
224 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
227 static void qemu_kvm_eat_signals(CPUState *env)
229 struct timespec ts = { 0, 0 };
230 siginfo_t siginfo;
231 sigset_t waitset;
232 sigset_t chkset;
233 int r;
235 sigemptyset(&waitset);
236 sigaddset(&waitset, SIG_IPI);
237 sigaddset(&waitset, SIGBUS);
239 do {
240 r = sigtimedwait(&waitset, &siginfo, &ts);
241 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
242 perror("sigtimedwait");
243 exit(1);
246 switch (r) {
247 case SIGBUS:
248 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
249 sigbus_reraise();
251 break;
252 default:
253 break;
256 r = sigpending(&chkset);
257 if (r == -1) {
258 perror("sigpending");
259 exit(1);
261 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
263 #ifndef CONFIG_IOTHREAD
264 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
265 qemu_notify_event();
267 #endif
270 #else /* !CONFIG_LINUX */
272 static void qemu_init_sigbus(void)
276 static void qemu_kvm_eat_signals(CPUState *env)
279 #endif /* !CONFIG_LINUX */
281 #ifndef _WIN32
282 static int io_thread_fd = -1;
284 static void qemu_event_increment(void)
286 /* Write 8 bytes to be compatible with eventfd. */
287 static const uint64_t val = 1;
288 ssize_t ret;
290 if (io_thread_fd == -1) {
291 return;
293 do {
294 ret = write(io_thread_fd, &val, sizeof(val));
295 } while (ret < 0 && errno == EINTR);
297 /* EAGAIN is fine, a read must be pending. */
298 if (ret < 0 && errno != EAGAIN) {
299 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
300 strerror(errno));
301 exit (1);
305 static void qemu_event_read(void *opaque)
307 int fd = (intptr_t)opaque;
308 ssize_t len;
309 char buffer[512];
311 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
312 do {
313 len = read(fd, buffer, sizeof(buffer));
314 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
317 static int qemu_event_init(void)
319 int err;
320 int fds[2];
322 err = qemu_eventfd(fds);
323 if (err == -1) {
324 return -errno;
326 err = fcntl_setfl(fds[0], O_NONBLOCK);
327 if (err < 0) {
328 goto fail;
330 err = fcntl_setfl(fds[1], O_NONBLOCK);
331 if (err < 0) {
332 goto fail;
334 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
335 (void *)(intptr_t)fds[0]);
337 io_thread_fd = fds[1];
338 return 0;
340 fail:
341 close(fds[0]);
342 close(fds[1]);
343 return err;
346 static void dummy_signal(int sig)
350 /* If we have signalfd, we mask out the signals we want to handle and then
351 * use signalfd to listen for them. We rely on whatever the current signal
352 * handler is to dispatch the signals when we receive them.
354 static void sigfd_handler(void *opaque)
356 int fd = (intptr_t)opaque;
357 struct qemu_signalfd_siginfo info;
358 struct sigaction action;
359 ssize_t len;
361 while (1) {
362 do {
363 len = read(fd, &info, sizeof(info));
364 } while (len == -1 && errno == EINTR);
366 if (len == -1 && errno == EAGAIN) {
367 break;
370 if (len != sizeof(info)) {
371 printf("read from sigfd returned %zd: %m\n", len);
372 return;
375 sigaction(info.ssi_signo, NULL, &action);
376 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
377 action.sa_sigaction(info.ssi_signo,
378 (siginfo_t *)&info, NULL);
379 } else if (action.sa_handler) {
380 action.sa_handler(info.ssi_signo);
385 static int qemu_signal_init(void)
387 int sigfd;
388 sigset_t set;
390 #ifdef CONFIG_IOTHREAD
391 /* SIGUSR2 used by posix-aio-compat.c */
392 sigemptyset(&set);
393 sigaddset(&set, SIGUSR2);
394 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
396 sigemptyset(&set);
397 sigaddset(&set, SIGIO);
398 sigaddset(&set, SIGALRM);
399 sigaddset(&set, SIG_IPI);
400 sigaddset(&set, SIGBUS);
401 pthread_sigmask(SIG_BLOCK, &set, NULL);
402 #else
403 sigemptyset(&set);
404 sigaddset(&set, SIGBUS);
405 if (kvm_enabled()) {
407 * We need to process timer signals synchronously to avoid a race
408 * between exit_request check and KVM vcpu entry.
410 sigaddset(&set, SIGIO);
411 sigaddset(&set, SIGALRM);
413 #endif
415 sigfd = qemu_signalfd(&set);
416 if (sigfd == -1) {
417 fprintf(stderr, "failed to create signalfd\n");
418 return -errno;
421 fcntl_setfl(sigfd, O_NONBLOCK);
423 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
424 (void *)(intptr_t)sigfd);
426 return 0;
429 static void qemu_kvm_init_cpu_signals(CPUState *env)
431 int r;
432 sigset_t set;
433 struct sigaction sigact;
435 memset(&sigact, 0, sizeof(sigact));
436 sigact.sa_handler = dummy_signal;
437 sigaction(SIG_IPI, &sigact, NULL);
439 #ifdef CONFIG_IOTHREAD
440 pthread_sigmask(SIG_BLOCK, NULL, &set);
441 sigdelset(&set, SIG_IPI);
442 sigdelset(&set, SIGBUS);
443 r = kvm_set_signal_mask(env, &set);
444 if (r) {
445 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
446 exit(1);
448 #else
449 sigemptyset(&set);
450 sigaddset(&set, SIG_IPI);
451 sigaddset(&set, SIGIO);
452 sigaddset(&set, SIGALRM);
453 pthread_sigmask(SIG_BLOCK, &set, NULL);
455 pthread_sigmask(SIG_BLOCK, NULL, &set);
456 sigdelset(&set, SIGIO);
457 sigdelset(&set, SIGALRM);
458 #endif
459 sigdelset(&set, SIG_IPI);
460 sigdelset(&set, SIGBUS);
461 r = kvm_set_signal_mask(env, &set);
462 if (r) {
463 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
464 exit(1);
468 static void qemu_tcg_init_cpu_signals(void)
470 #ifdef CONFIG_IOTHREAD
471 sigset_t set;
472 struct sigaction sigact;
474 memset(&sigact, 0, sizeof(sigact));
475 sigact.sa_handler = cpu_signal;
476 sigaction(SIG_IPI, &sigact, NULL);
478 sigemptyset(&set);
479 sigaddset(&set, SIG_IPI);
480 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
481 #endif
484 #else /* _WIN32 */
486 HANDLE qemu_event_handle;
488 static void dummy_event_handler(void *opaque)
492 static int qemu_event_init(void)
494 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
495 if (!qemu_event_handle) {
496 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
497 return -1;
499 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
500 return 0;
503 static void qemu_event_increment(void)
505 if (!SetEvent(qemu_event_handle)) {
506 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
507 GetLastError());
508 exit (1);
512 static int qemu_signal_init(void)
514 return 0;
517 static void qemu_kvm_init_cpu_signals(CPUState *env)
519 abort();
522 static void qemu_tcg_init_cpu_signals(void)
525 #endif /* _WIN32 */
527 #ifndef CONFIG_IOTHREAD
528 int qemu_init_main_loop(void)
530 int ret;
532 ret = qemu_signal_init();
533 if (ret) {
534 return ret;
537 qemu_init_sigbus();
539 return qemu_event_init();
542 void qemu_main_loop_start(void)
546 void qemu_init_vcpu(void *_env)
548 CPUState *env = _env;
549 int r;
551 env->nr_cores = smp_cores;
552 env->nr_threads = smp_threads;
554 if (kvm_enabled()) {
555 r = kvm_init_vcpu(env);
556 if (r < 0) {
557 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
558 exit(1);
560 qemu_kvm_init_cpu_signals(env);
561 } else {
562 qemu_tcg_init_cpu_signals();
566 int qemu_cpu_is_self(void *env)
568 return 1;
571 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
573 func(data);
576 void resume_all_vcpus(void)
580 void pause_all_vcpus(void)
584 void qemu_cpu_kick(void *env)
588 void qemu_cpu_kick_self(void)
590 #ifndef _WIN32
591 assert(cpu_single_env);
593 raise(SIG_IPI);
594 #else
595 abort();
596 #endif
599 void qemu_notify_event(void)
601 CPUState *env = cpu_single_env;
603 qemu_event_increment ();
604 if (env) {
605 cpu_exit(env);
607 if (next_cpu && env != next_cpu) {
608 cpu_exit(next_cpu);
610 exit_request = 1;
613 void qemu_mutex_lock_iothread(void) {}
614 void qemu_mutex_unlock_iothread(void) {}
616 void cpu_stop_current(void)
620 void vm_stop(int reason)
622 do_vm_stop(reason);
625 #else /* CONFIG_IOTHREAD */
627 QemuMutex qemu_global_mutex;
628 static QemuMutex qemu_fair_mutex;
630 static QemuThread io_thread;
632 static QemuThread *tcg_cpu_thread;
633 static QemuCond *tcg_halt_cond;
635 static int qemu_system_ready;
636 /* cpu creation */
637 static QemuCond qemu_cpu_cond;
638 /* system init */
639 static QemuCond qemu_system_cond;
640 static QemuCond qemu_pause_cond;
641 static QemuCond qemu_work_cond;
643 int qemu_init_main_loop(void)
645 int ret;
647 qemu_init_sigbus();
649 ret = qemu_signal_init();
650 if (ret) {
651 return ret;
654 /* Note eventfd must be drained before signalfd handlers run */
655 ret = qemu_event_init();
656 if (ret) {
657 return ret;
660 qemu_cond_init(&qemu_cpu_cond);
661 qemu_cond_init(&qemu_system_cond);
662 qemu_cond_init(&qemu_pause_cond);
663 qemu_cond_init(&qemu_work_cond);
664 qemu_mutex_init(&qemu_fair_mutex);
665 qemu_mutex_init(&qemu_global_mutex);
666 qemu_mutex_lock(&qemu_global_mutex);
668 qemu_thread_get_self(&io_thread);
670 return 0;
673 void qemu_main_loop_start(void)
675 qemu_system_ready = 1;
676 qemu_cond_broadcast(&qemu_system_cond);
679 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
681 struct qemu_work_item wi;
683 if (qemu_cpu_is_self(env)) {
684 func(data);
685 return;
688 wi.func = func;
689 wi.data = data;
690 if (!env->queued_work_first) {
691 env->queued_work_first = &wi;
692 } else {
693 env->queued_work_last->next = &wi;
695 env->queued_work_last = &wi;
696 wi.next = NULL;
697 wi.done = false;
699 qemu_cpu_kick(env);
700 while (!wi.done) {
701 CPUState *self_env = cpu_single_env;
703 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
704 cpu_single_env = self_env;
708 static void flush_queued_work(CPUState *env)
710 struct qemu_work_item *wi;
712 if (!env->queued_work_first) {
713 return;
716 while ((wi = env->queued_work_first)) {
717 env->queued_work_first = wi->next;
718 wi->func(wi->data);
719 wi->done = true;
721 env->queued_work_last = NULL;
722 qemu_cond_broadcast(&qemu_work_cond);
725 static void qemu_wait_io_event_common(CPUState *env)
727 if (env->stop) {
728 env->stop = 0;
729 env->stopped = 1;
730 qemu_cond_signal(&qemu_pause_cond);
732 flush_queued_work(env);
733 env->thread_kicked = false;
736 static void qemu_tcg_wait_io_event(void)
738 CPUState *env;
740 while (all_cpu_threads_idle()) {
741 /* Start accounting real time to the virtual clock if the CPUs
742 are idle. */
743 qemu_clock_warp(vm_clock);
744 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
747 qemu_mutex_unlock(&qemu_global_mutex);
750 * Users of qemu_global_mutex can be starved, having no chance
751 * to acquire it since this path will get to it first.
752 * So use another lock to provide fairness.
754 qemu_mutex_lock(&qemu_fair_mutex);
755 qemu_mutex_unlock(&qemu_fair_mutex);
757 qemu_mutex_lock(&qemu_global_mutex);
759 for (env = first_cpu; env != NULL; env = env->next_cpu) {
760 qemu_wait_io_event_common(env);
764 static void qemu_kvm_wait_io_event(CPUState *env)
766 while (cpu_thread_is_idle(env)) {
767 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
770 qemu_kvm_eat_signals(env);
771 qemu_wait_io_event_common(env);
774 static void *qemu_kvm_cpu_thread_fn(void *arg)
776 CPUState *env = arg;
777 int r;
779 qemu_mutex_lock(&qemu_global_mutex);
780 qemu_thread_get_self(env->thread);
781 env->thread_id = qemu_get_thread_id();
783 r = kvm_init_vcpu(env);
784 if (r < 0) {
785 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
786 exit(1);
789 qemu_kvm_init_cpu_signals(env);
791 /* signal CPU creation */
792 env->created = 1;
793 qemu_cond_signal(&qemu_cpu_cond);
795 /* and wait for machine initialization */
796 while (!qemu_system_ready) {
797 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
800 while (1) {
801 if (cpu_can_run(env)) {
802 r = kvm_cpu_exec(env);
803 if (r == EXCP_DEBUG) {
804 cpu_handle_guest_debug(env);
807 qemu_kvm_wait_io_event(env);
810 return NULL;
813 static void *qemu_tcg_cpu_thread_fn(void *arg)
815 CPUState *env = arg;
817 qemu_tcg_init_cpu_signals();
818 qemu_thread_get_self(env->thread);
820 /* signal CPU creation */
821 qemu_mutex_lock(&qemu_global_mutex);
822 for (env = first_cpu; env != NULL; env = env->next_cpu) {
823 env->thread_id = qemu_get_thread_id();
824 env->created = 1;
826 qemu_cond_signal(&qemu_cpu_cond);
828 /* and wait for machine initialization */
829 while (!qemu_system_ready) {
830 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
833 while (1) {
834 cpu_exec_all();
835 if (use_icount && qemu_next_icount_deadline() <= 0) {
836 qemu_notify_event();
838 qemu_tcg_wait_io_event();
841 return NULL;
844 static void qemu_cpu_kick_thread(CPUState *env)
846 #ifndef _WIN32
847 int err;
849 err = pthread_kill(env->thread->thread, SIG_IPI);
850 if (err) {
851 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
852 exit(1);
854 #else /* _WIN32 */
855 if (!qemu_cpu_is_self(env)) {
856 SuspendThread(env->thread->thread);
857 cpu_signal(0);
858 ResumeThread(env->thread->thread);
860 #endif
863 void qemu_cpu_kick(void *_env)
865 CPUState *env = _env;
867 qemu_cond_broadcast(env->halt_cond);
868 if (!env->thread_kicked) {
869 qemu_cpu_kick_thread(env);
870 env->thread_kicked = true;
874 void qemu_cpu_kick_self(void)
876 #ifndef _WIN32
877 assert(cpu_single_env);
879 if (!cpu_single_env->thread_kicked) {
880 qemu_cpu_kick_thread(cpu_single_env);
881 cpu_single_env->thread_kicked = true;
883 #else
884 abort();
885 #endif
888 int qemu_cpu_is_self(void *_env)
890 CPUState *env = _env;
892 return qemu_thread_is_self(env->thread);
895 void qemu_mutex_lock_iothread(void)
897 if (kvm_enabled()) {
898 qemu_mutex_lock(&qemu_global_mutex);
899 } else {
900 qemu_mutex_lock(&qemu_fair_mutex);
901 if (qemu_mutex_trylock(&qemu_global_mutex)) {
902 qemu_cpu_kick_thread(first_cpu);
903 qemu_mutex_lock(&qemu_global_mutex);
905 qemu_mutex_unlock(&qemu_fair_mutex);
909 void qemu_mutex_unlock_iothread(void)
911 qemu_mutex_unlock(&qemu_global_mutex);
914 static int all_vcpus_paused(void)
916 CPUState *penv = first_cpu;
918 while (penv) {
919 if (!penv->stopped) {
920 return 0;
922 penv = (CPUState *)penv->next_cpu;
925 return 1;
928 void pause_all_vcpus(void)
930 CPUState *penv = first_cpu;
932 while (penv) {
933 penv->stop = 1;
934 qemu_cpu_kick(penv);
935 penv = (CPUState *)penv->next_cpu;
938 while (!all_vcpus_paused()) {
939 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
940 penv = first_cpu;
941 while (penv) {
942 qemu_cpu_kick(penv);
943 penv = (CPUState *)penv->next_cpu;
948 void resume_all_vcpus(void)
950 CPUState *penv = first_cpu;
952 while (penv) {
953 penv->stop = 0;
954 penv->stopped = 0;
955 qemu_cpu_kick(penv);
956 penv = (CPUState *)penv->next_cpu;
960 static void qemu_tcg_init_vcpu(void *_env)
962 CPUState *env = _env;
964 /* share a single thread for all cpus with TCG */
965 if (!tcg_cpu_thread) {
966 env->thread = qemu_mallocz(sizeof(QemuThread));
967 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
968 qemu_cond_init(env->halt_cond);
969 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
970 while (env->created == 0) {
971 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
973 tcg_cpu_thread = env->thread;
974 tcg_halt_cond = env->halt_cond;
975 } else {
976 env->thread = tcg_cpu_thread;
977 env->halt_cond = tcg_halt_cond;
981 static void qemu_kvm_start_vcpu(CPUState *env)
983 env->thread = qemu_mallocz(sizeof(QemuThread));
984 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
985 qemu_cond_init(env->halt_cond);
986 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
987 while (env->created == 0) {
988 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
992 void qemu_init_vcpu(void *_env)
994 CPUState *env = _env;
996 env->nr_cores = smp_cores;
997 env->nr_threads = smp_threads;
998 if (kvm_enabled()) {
999 qemu_kvm_start_vcpu(env);
1000 } else {
1001 qemu_tcg_init_vcpu(env);
1005 void qemu_notify_event(void)
1007 qemu_event_increment();
1010 void cpu_stop_current(void)
1012 if (cpu_single_env) {
1013 cpu_single_env->stop = 0;
1014 cpu_single_env->stopped = 1;
1015 cpu_exit(cpu_single_env);
1016 qemu_cond_signal(&qemu_pause_cond);
1020 void vm_stop(int reason)
1022 if (!qemu_thread_is_self(&io_thread)) {
1023 qemu_system_vmstop_request(reason);
1025 * FIXME: should not return to device code in case
1026 * vm_stop() has been requested.
1028 cpu_stop_current();
1029 return;
1031 do_vm_stop(reason);
1034 #endif
1036 static int tcg_cpu_exec(CPUState *env)
1038 int ret;
1039 #ifdef CONFIG_PROFILER
1040 int64_t ti;
1041 #endif
1043 #ifdef CONFIG_PROFILER
1044 ti = profile_getclock();
1045 #endif
1046 if (use_icount) {
1047 int64_t count;
1048 int decr;
1049 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1050 env->icount_decr.u16.low = 0;
1051 env->icount_extra = 0;
1052 count = qemu_icount_round(qemu_next_icount_deadline());
1053 qemu_icount += count;
1054 decr = (count > 0xffff) ? 0xffff : count;
1055 count -= decr;
1056 env->icount_decr.u16.low = decr;
1057 env->icount_extra = count;
1059 ret = cpu_exec(env);
1060 #ifdef CONFIG_PROFILER
1061 qemu_time += profile_getclock() - ti;
1062 #endif
1063 if (use_icount) {
1064 /* Fold pending instructions back into the
1065 instruction counter, and clear the interrupt flag. */
1066 qemu_icount -= (env->icount_decr.u16.low
1067 + env->icount_extra);
1068 env->icount_decr.u32 = 0;
1069 env->icount_extra = 0;
1071 return ret;
1074 bool cpu_exec_all(void)
1076 int r;
1078 /* Account partial waits to the vm_clock. */
1079 qemu_clock_warp(vm_clock);
1081 if (next_cpu == NULL) {
1082 next_cpu = first_cpu;
1084 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1085 CPUState *env = next_cpu;
1087 qemu_clock_enable(vm_clock,
1088 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1090 #ifndef CONFIG_IOTHREAD
1091 if (qemu_alarm_pending()) {
1092 break;
1094 #endif
1095 if (cpu_can_run(env)) {
1096 if (kvm_enabled()) {
1097 r = kvm_cpu_exec(env);
1098 qemu_kvm_eat_signals(env);
1099 } else {
1100 r = tcg_cpu_exec(env);
1102 if (r == EXCP_DEBUG) {
1103 cpu_handle_guest_debug(env);
1104 break;
1106 } else if (env->stop || env->stopped) {
1107 break;
1110 exit_request = 0;
1111 return !all_cpu_threads_idle();
1114 void set_numa_modes(void)
1116 CPUState *env;
1117 int i;
1119 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1120 for (i = 0; i < nb_numa_nodes; i++) {
1121 if (node_cpumask[i] & (1 << env->cpu_index)) {
1122 env->numa_node = i;
1128 void set_cpu_log(const char *optarg)
1130 int mask;
1131 const CPULogItem *item;
1133 mask = cpu_str_to_log_mask(optarg);
1134 if (!mask) {
1135 printf("Log items (comma separated):\n");
1136 for (item = cpu_log_items; item->mask != 0; item++) {
1137 printf("%-10s %s\n", item->name, item->help);
1139 exit(1);
1141 cpu_set_log(mask);
1144 void set_cpu_log_filename(const char *optarg)
1146 cpu_set_log_filename(optarg);
1149 /* Return the virtual CPU time, based on the instruction counter. */
1150 int64_t cpu_get_icount(void)
1152 int64_t icount;
1153 CPUState *env = cpu_single_env;;
1155 icount = qemu_icount;
1156 if (env) {
1157 if (!can_do_io(env)) {
1158 fprintf(stderr, "Bad clock read\n");
1160 icount -= (env->icount_decr.u16.low + env->icount_extra);
1162 return qemu_icount_bias + (icount << icount_time_shift);
1165 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1167 /* XXX: implement xxx_cpu_list for targets that still miss it */
1168 #if defined(cpu_list_id)
1169 cpu_list_id(f, cpu_fprintf, optarg);
1170 #elif defined(cpu_list)
1171 cpu_list(f, cpu_fprintf); /* deprecated */
1172 #endif