Merge remote-tracking branch 'stefanha/trivial-patches' into staging
[qemu.git] / cpus.c
blobabd24ab31cf043c3b23a127b6383009f79ecac30
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"
37 #ifndef _WIN32
38 #include "compatfd.h"
39 #endif
41 #ifdef SIGRTMIN
42 #define SIG_IPI (SIGRTMIN+4)
43 #else
44 #define SIG_IPI SIGUSR1
45 #endif
47 #ifdef CONFIG_LINUX
49 #include <sys/prctl.h>
51 #ifndef PR_MCE_KILL
52 #define PR_MCE_KILL 33
53 #endif
55 #ifndef PR_MCE_KILL_SET
56 #define PR_MCE_KILL_SET 1
57 #endif
59 #ifndef PR_MCE_KILL_EARLY
60 #define PR_MCE_KILL_EARLY 1
61 #endif
63 #endif /* CONFIG_LINUX */
65 static CPUState *next_cpu;
67 /***********************************************************/
68 void hw_error(const char *fmt, ...)
70 va_list ap;
71 CPUState *env;
73 va_start(ap, fmt);
74 fprintf(stderr, "qemu: hardware error: ");
75 vfprintf(stderr, fmt, ap);
76 fprintf(stderr, "\n");
77 for(env = first_cpu; env != NULL; env = env->next_cpu) {
78 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
79 #ifdef TARGET_I386
80 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
81 #else
82 cpu_dump_state(env, stderr, fprintf, 0);
83 #endif
85 va_end(ap);
86 abort();
89 void cpu_synchronize_all_states(void)
91 CPUState *cpu;
93 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
94 cpu_synchronize_state(cpu);
98 void cpu_synchronize_all_post_reset(void)
100 CPUState *cpu;
102 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
103 cpu_synchronize_post_reset(cpu);
107 void cpu_synchronize_all_post_init(void)
109 CPUState *cpu;
111 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
112 cpu_synchronize_post_init(cpu);
116 int cpu_is_stopped(CPUState *env)
118 return !vm_running || env->stopped;
121 static void do_vm_stop(int reason)
123 if (vm_running) {
124 cpu_disable_ticks();
125 vm_running = 0;
126 pause_all_vcpus();
127 vm_state_notify(0, reason);
128 qemu_aio_flush();
129 bdrv_flush_all();
130 monitor_protocol_event(QEVENT_STOP, NULL);
134 static int cpu_can_run(CPUState *env)
136 if (env->stop) {
137 return 0;
139 if (env->stopped || !vm_running) {
140 return 0;
142 return 1;
145 static bool cpu_thread_is_idle(CPUState *env)
147 if (env->stop || env->queued_work_first) {
148 return false;
150 if (env->stopped || !vm_running) {
151 return true;
153 if (!env->halted || qemu_cpu_has_work(env) ||
154 (kvm_enabled() && kvm_irqchip_in_kernel())) {
155 return false;
157 return true;
160 bool all_cpu_threads_idle(void)
162 CPUState *env;
164 for (env = first_cpu; env != NULL; env = env->next_cpu) {
165 if (!cpu_thread_is_idle(env)) {
166 return false;
169 return true;
172 static void cpu_handle_guest_debug(CPUState *env)
174 gdb_set_stop_cpu(env);
175 qemu_system_debug_request();
176 #ifdef CONFIG_IOTHREAD
177 env->stopped = 1;
178 #endif
181 #ifdef CONFIG_IOTHREAD
182 static void cpu_signal(int sig)
184 if (cpu_single_env) {
185 cpu_exit(cpu_single_env);
187 exit_request = 1;
189 #endif
191 #ifdef CONFIG_LINUX
192 static void sigbus_reraise(void)
194 sigset_t set;
195 struct sigaction action;
197 memset(&action, 0, sizeof(action));
198 action.sa_handler = SIG_DFL;
199 if (!sigaction(SIGBUS, &action, NULL)) {
200 raise(SIGBUS);
201 sigemptyset(&set);
202 sigaddset(&set, SIGBUS);
203 sigprocmask(SIG_UNBLOCK, &set, NULL);
205 perror("Failed to re-raise SIGBUS!\n");
206 abort();
209 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
210 void *ctx)
212 if (kvm_on_sigbus(siginfo->ssi_code,
213 (void *)(intptr_t)siginfo->ssi_addr)) {
214 sigbus_reraise();
218 static void qemu_init_sigbus(void)
220 struct sigaction action;
222 memset(&action, 0, sizeof(action));
223 action.sa_flags = SA_SIGINFO;
224 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
225 sigaction(SIGBUS, &action, NULL);
227 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
230 static void qemu_kvm_eat_signals(CPUState *env)
232 struct timespec ts = { 0, 0 };
233 siginfo_t siginfo;
234 sigset_t waitset;
235 sigset_t chkset;
236 int r;
238 sigemptyset(&waitset);
239 sigaddset(&waitset, SIG_IPI);
240 sigaddset(&waitset, SIGBUS);
242 do {
243 r = sigtimedwait(&waitset, &siginfo, &ts);
244 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
245 perror("sigtimedwait");
246 exit(1);
249 switch (r) {
250 case SIGBUS:
251 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
252 sigbus_reraise();
254 break;
255 default:
256 break;
259 r = sigpending(&chkset);
260 if (r == -1) {
261 perror("sigpending");
262 exit(1);
264 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
266 #ifndef CONFIG_IOTHREAD
267 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
268 qemu_notify_event();
270 #endif
273 #else /* !CONFIG_LINUX */
275 static void qemu_init_sigbus(void)
279 static void qemu_kvm_eat_signals(CPUState *env)
282 #endif /* !CONFIG_LINUX */
284 #ifndef _WIN32
285 static int io_thread_fd = -1;
287 static void qemu_event_increment(void)
289 /* Write 8 bytes to be compatible with eventfd. */
290 static const uint64_t val = 1;
291 ssize_t ret;
293 if (io_thread_fd == -1) {
294 return;
296 do {
297 ret = write(io_thread_fd, &val, sizeof(val));
298 } while (ret < 0 && errno == EINTR);
300 /* EAGAIN is fine, a read must be pending. */
301 if (ret < 0 && errno != EAGAIN) {
302 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
303 strerror(errno));
304 exit (1);
308 static void qemu_event_read(void *opaque)
310 int fd = (intptr_t)opaque;
311 ssize_t len;
312 char buffer[512];
314 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
315 do {
316 len = read(fd, buffer, sizeof(buffer));
317 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
320 static int qemu_event_init(void)
322 int err;
323 int fds[2];
325 err = qemu_eventfd(fds);
326 if (err == -1) {
327 return -errno;
329 err = fcntl_setfl(fds[0], O_NONBLOCK);
330 if (err < 0) {
331 goto fail;
333 err = fcntl_setfl(fds[1], O_NONBLOCK);
334 if (err < 0) {
335 goto fail;
337 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
338 (void *)(intptr_t)fds[0]);
340 io_thread_fd = fds[1];
341 return 0;
343 fail:
344 close(fds[0]);
345 close(fds[1]);
346 return err;
349 static void dummy_signal(int sig)
353 /* If we have signalfd, we mask out the signals we want to handle and then
354 * use signalfd to listen for them. We rely on whatever the current signal
355 * handler is to dispatch the signals when we receive them.
357 static void sigfd_handler(void *opaque)
359 int fd = (intptr_t)opaque;
360 struct qemu_signalfd_siginfo info;
361 struct sigaction action;
362 ssize_t len;
364 while (1) {
365 do {
366 len = read(fd, &info, sizeof(info));
367 } while (len == -1 && errno == EINTR);
369 if (len == -1 && errno == EAGAIN) {
370 break;
373 if (len != sizeof(info)) {
374 printf("read from sigfd returned %zd: %m\n", len);
375 return;
378 sigaction(info.ssi_signo, NULL, &action);
379 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
380 action.sa_sigaction(info.ssi_signo,
381 (siginfo_t *)&info, NULL);
382 } else if (action.sa_handler) {
383 action.sa_handler(info.ssi_signo);
388 static int qemu_signal_init(void)
390 int sigfd;
391 sigset_t set;
393 #ifdef CONFIG_IOTHREAD
394 /* SIGUSR2 used by posix-aio-compat.c */
395 sigemptyset(&set);
396 sigaddset(&set, SIGUSR2);
397 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
399 sigemptyset(&set);
400 sigaddset(&set, SIGIO);
401 sigaddset(&set, SIGALRM);
402 sigaddset(&set, SIG_IPI);
403 sigaddset(&set, SIGBUS);
404 pthread_sigmask(SIG_BLOCK, &set, NULL);
405 #else
406 sigemptyset(&set);
407 sigaddset(&set, SIGBUS);
408 if (kvm_enabled()) {
410 * We need to process timer signals synchronously to avoid a race
411 * between exit_request check and KVM vcpu entry.
413 sigaddset(&set, SIGIO);
414 sigaddset(&set, SIGALRM);
416 #endif
418 sigfd = qemu_signalfd(&set);
419 if (sigfd == -1) {
420 fprintf(stderr, "failed to create signalfd\n");
421 return -errno;
424 fcntl_setfl(sigfd, O_NONBLOCK);
426 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
427 (void *)(intptr_t)sigfd);
429 return 0;
432 static void qemu_kvm_init_cpu_signals(CPUState *env)
434 int r;
435 sigset_t set;
436 struct sigaction sigact;
438 memset(&sigact, 0, sizeof(sigact));
439 sigact.sa_handler = dummy_signal;
440 sigaction(SIG_IPI, &sigact, NULL);
442 #ifdef CONFIG_IOTHREAD
443 pthread_sigmask(SIG_BLOCK, NULL, &set);
444 sigdelset(&set, SIG_IPI);
445 sigdelset(&set, SIGBUS);
446 r = kvm_set_signal_mask(env, &set);
447 if (r) {
448 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
449 exit(1);
451 #else
452 sigemptyset(&set);
453 sigaddset(&set, SIG_IPI);
454 sigaddset(&set, SIGIO);
455 sigaddset(&set, SIGALRM);
456 pthread_sigmask(SIG_BLOCK, &set, NULL);
458 pthread_sigmask(SIG_BLOCK, NULL, &set);
459 sigdelset(&set, SIGIO);
460 sigdelset(&set, SIGALRM);
461 #endif
462 sigdelset(&set, SIG_IPI);
463 sigdelset(&set, SIGBUS);
464 r = kvm_set_signal_mask(env, &set);
465 if (r) {
466 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
467 exit(1);
471 static void qemu_tcg_init_cpu_signals(void)
473 #ifdef CONFIG_IOTHREAD
474 sigset_t set;
475 struct sigaction sigact;
477 memset(&sigact, 0, sizeof(sigact));
478 sigact.sa_handler = cpu_signal;
479 sigaction(SIG_IPI, &sigact, NULL);
481 sigemptyset(&set);
482 sigaddset(&set, SIG_IPI);
483 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
484 #endif
487 #else /* _WIN32 */
489 HANDLE qemu_event_handle;
491 static void dummy_event_handler(void *opaque)
495 static int qemu_event_init(void)
497 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
498 if (!qemu_event_handle) {
499 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
500 return -1;
502 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
503 return 0;
506 static void qemu_event_increment(void)
508 if (!SetEvent(qemu_event_handle)) {
509 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
510 GetLastError());
511 exit (1);
515 static int qemu_signal_init(void)
517 return 0;
520 static void qemu_kvm_init_cpu_signals(CPUState *env)
522 abort();
525 static void qemu_tcg_init_cpu_signals(void)
528 #endif /* _WIN32 */
530 #ifndef CONFIG_IOTHREAD
531 int qemu_init_main_loop(void)
533 int ret;
535 ret = qemu_signal_init();
536 if (ret) {
537 return ret;
540 qemu_init_sigbus();
542 return qemu_event_init();
545 void qemu_main_loop_start(void)
549 void qemu_init_vcpu(void *_env)
551 CPUState *env = _env;
552 int r;
554 env->nr_cores = smp_cores;
555 env->nr_threads = smp_threads;
557 if (kvm_enabled()) {
558 r = kvm_init_vcpu(env);
559 if (r < 0) {
560 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
561 exit(1);
563 qemu_kvm_init_cpu_signals(env);
564 } else {
565 qemu_tcg_init_cpu_signals();
569 int qemu_cpu_is_self(void *env)
571 return 1;
574 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
576 func(data);
579 void resume_all_vcpus(void)
583 void pause_all_vcpus(void)
587 void qemu_cpu_kick(void *env)
591 void qemu_cpu_kick_self(void)
593 #ifndef _WIN32
594 assert(cpu_single_env);
596 raise(SIG_IPI);
597 #else
598 abort();
599 #endif
602 void qemu_notify_event(void)
604 CPUState *env = cpu_single_env;
606 qemu_event_increment ();
607 if (env) {
608 cpu_exit(env);
610 if (next_cpu && env != next_cpu) {
611 cpu_exit(next_cpu);
613 exit_request = 1;
616 void qemu_mutex_lock_iothread(void) {}
617 void qemu_mutex_unlock_iothread(void) {}
619 void cpu_stop_current(void)
623 void vm_stop(int reason)
625 do_vm_stop(reason);
628 #else /* CONFIG_IOTHREAD */
630 QemuMutex qemu_global_mutex;
631 static QemuMutex qemu_fair_mutex;
633 static QemuThread io_thread;
635 static QemuThread *tcg_cpu_thread;
636 static QemuCond *tcg_halt_cond;
638 static int qemu_system_ready;
639 /* cpu creation */
640 static QemuCond qemu_cpu_cond;
641 /* system init */
642 static QemuCond qemu_system_cond;
643 static QemuCond qemu_pause_cond;
644 static QemuCond qemu_work_cond;
646 int qemu_init_main_loop(void)
648 int ret;
650 qemu_init_sigbus();
652 ret = qemu_signal_init();
653 if (ret) {
654 return ret;
657 /* Note eventfd must be drained before signalfd handlers run */
658 ret = qemu_event_init();
659 if (ret) {
660 return ret;
663 qemu_cond_init(&qemu_cpu_cond);
664 qemu_cond_init(&qemu_system_cond);
665 qemu_cond_init(&qemu_pause_cond);
666 qemu_cond_init(&qemu_work_cond);
667 qemu_mutex_init(&qemu_fair_mutex);
668 qemu_mutex_init(&qemu_global_mutex);
669 qemu_mutex_lock(&qemu_global_mutex);
671 qemu_thread_get_self(&io_thread);
673 return 0;
676 void qemu_main_loop_start(void)
678 qemu_system_ready = 1;
679 qemu_cond_broadcast(&qemu_system_cond);
682 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
684 struct qemu_work_item wi;
686 if (qemu_cpu_is_self(env)) {
687 func(data);
688 return;
691 wi.func = func;
692 wi.data = data;
693 if (!env->queued_work_first) {
694 env->queued_work_first = &wi;
695 } else {
696 env->queued_work_last->next = &wi;
698 env->queued_work_last = &wi;
699 wi.next = NULL;
700 wi.done = false;
702 qemu_cpu_kick(env);
703 while (!wi.done) {
704 CPUState *self_env = cpu_single_env;
706 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
707 cpu_single_env = self_env;
711 static void flush_queued_work(CPUState *env)
713 struct qemu_work_item *wi;
715 if (!env->queued_work_first) {
716 return;
719 while ((wi = env->queued_work_first)) {
720 env->queued_work_first = wi->next;
721 wi->func(wi->data);
722 wi->done = true;
724 env->queued_work_last = NULL;
725 qemu_cond_broadcast(&qemu_work_cond);
728 static void qemu_wait_io_event_common(CPUState *env)
730 if (env->stop) {
731 env->stop = 0;
732 env->stopped = 1;
733 qemu_cond_signal(&qemu_pause_cond);
735 flush_queued_work(env);
736 env->thread_kicked = false;
739 static void qemu_tcg_wait_io_event(void)
741 CPUState *env;
743 while (all_cpu_threads_idle()) {
744 /* Start accounting real time to the virtual clock if the CPUs
745 are idle. */
746 qemu_clock_warp(vm_clock);
747 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
750 qemu_mutex_unlock(&qemu_global_mutex);
753 * Users of qemu_global_mutex can be starved, having no chance
754 * to acquire it since this path will get to it first.
755 * So use another lock to provide fairness.
757 qemu_mutex_lock(&qemu_fair_mutex);
758 qemu_mutex_unlock(&qemu_fair_mutex);
760 qemu_mutex_lock(&qemu_global_mutex);
762 for (env = first_cpu; env != NULL; env = env->next_cpu) {
763 qemu_wait_io_event_common(env);
767 static void qemu_kvm_wait_io_event(CPUState *env)
769 while (cpu_thread_is_idle(env)) {
770 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
773 qemu_kvm_eat_signals(env);
774 qemu_wait_io_event_common(env);
777 static void *qemu_kvm_cpu_thread_fn(void *arg)
779 CPUState *env = arg;
780 int r;
782 qemu_mutex_lock(&qemu_global_mutex);
783 qemu_thread_get_self(env->thread);
784 env->thread_id = qemu_get_thread_id();
786 r = kvm_init_vcpu(env);
787 if (r < 0) {
788 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
789 exit(1);
792 qemu_kvm_init_cpu_signals(env);
794 /* signal CPU creation */
795 env->created = 1;
796 qemu_cond_signal(&qemu_cpu_cond);
798 /* and wait for machine initialization */
799 while (!qemu_system_ready) {
800 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
803 while (1) {
804 if (cpu_can_run(env)) {
805 r = kvm_cpu_exec(env);
806 if (r == EXCP_DEBUG) {
807 cpu_handle_guest_debug(env);
810 qemu_kvm_wait_io_event(env);
813 return NULL;
816 static void *qemu_tcg_cpu_thread_fn(void *arg)
818 CPUState *env = arg;
820 qemu_tcg_init_cpu_signals();
821 qemu_thread_get_self(env->thread);
823 /* signal CPU creation */
824 qemu_mutex_lock(&qemu_global_mutex);
825 for (env = first_cpu; env != NULL; env = env->next_cpu) {
826 env->thread_id = qemu_get_thread_id();
827 env->created = 1;
829 qemu_cond_signal(&qemu_cpu_cond);
831 /* and wait for machine initialization */
832 while (!qemu_system_ready) {
833 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
836 while (1) {
837 cpu_exec_all();
838 if (use_icount && qemu_next_icount_deadline() <= 0) {
839 qemu_notify_event();
841 qemu_tcg_wait_io_event();
844 return NULL;
847 static void qemu_cpu_kick_thread(CPUState *env)
849 #ifndef _WIN32
850 int err;
852 err = pthread_kill(env->thread->thread, SIG_IPI);
853 if (err) {
854 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
855 exit(1);
857 #else /* _WIN32 */
858 if (!qemu_cpu_is_self(env)) {
859 SuspendThread(env->thread->thread);
860 cpu_signal(0);
861 ResumeThread(env->thread->thread);
863 #endif
866 void qemu_cpu_kick(void *_env)
868 CPUState *env = _env;
870 qemu_cond_broadcast(env->halt_cond);
871 if (!env->thread_kicked) {
872 qemu_cpu_kick_thread(env);
873 env->thread_kicked = true;
877 void qemu_cpu_kick_self(void)
879 #ifndef _WIN32
880 assert(cpu_single_env);
882 if (!cpu_single_env->thread_kicked) {
883 qemu_cpu_kick_thread(cpu_single_env);
884 cpu_single_env->thread_kicked = true;
886 #else
887 abort();
888 #endif
891 int qemu_cpu_is_self(void *_env)
893 CPUState *env = _env;
895 return qemu_thread_is_self(env->thread);
898 void qemu_mutex_lock_iothread(void)
900 if (kvm_enabled()) {
901 qemu_mutex_lock(&qemu_global_mutex);
902 } else {
903 qemu_mutex_lock(&qemu_fair_mutex);
904 if (qemu_mutex_trylock(&qemu_global_mutex)) {
905 qemu_cpu_kick_thread(first_cpu);
906 qemu_mutex_lock(&qemu_global_mutex);
908 qemu_mutex_unlock(&qemu_fair_mutex);
912 void qemu_mutex_unlock_iothread(void)
914 qemu_mutex_unlock(&qemu_global_mutex);
917 static int all_vcpus_paused(void)
919 CPUState *penv = first_cpu;
921 while (penv) {
922 if (!penv->stopped) {
923 return 0;
925 penv = (CPUState *)penv->next_cpu;
928 return 1;
931 void pause_all_vcpus(void)
933 CPUState *penv = first_cpu;
935 while (penv) {
936 penv->stop = 1;
937 qemu_cpu_kick(penv);
938 penv = (CPUState *)penv->next_cpu;
941 while (!all_vcpus_paused()) {
942 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
943 penv = first_cpu;
944 while (penv) {
945 qemu_cpu_kick(penv);
946 penv = (CPUState *)penv->next_cpu;
951 void resume_all_vcpus(void)
953 CPUState *penv = first_cpu;
955 while (penv) {
956 penv->stop = 0;
957 penv->stopped = 0;
958 qemu_cpu_kick(penv);
959 penv = (CPUState *)penv->next_cpu;
963 static void qemu_tcg_init_vcpu(void *_env)
965 CPUState *env = _env;
967 /* share a single thread for all cpus with TCG */
968 if (!tcg_cpu_thread) {
969 env->thread = qemu_mallocz(sizeof(QemuThread));
970 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
971 qemu_cond_init(env->halt_cond);
972 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
973 while (env->created == 0) {
974 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
976 tcg_cpu_thread = env->thread;
977 tcg_halt_cond = env->halt_cond;
978 } else {
979 env->thread = tcg_cpu_thread;
980 env->halt_cond = tcg_halt_cond;
984 static void qemu_kvm_start_vcpu(CPUState *env)
986 env->thread = qemu_mallocz(sizeof(QemuThread));
987 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
988 qemu_cond_init(env->halt_cond);
989 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
990 while (env->created == 0) {
991 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
995 void qemu_init_vcpu(void *_env)
997 CPUState *env = _env;
999 env->nr_cores = smp_cores;
1000 env->nr_threads = smp_threads;
1001 if (kvm_enabled()) {
1002 qemu_kvm_start_vcpu(env);
1003 } else {
1004 qemu_tcg_init_vcpu(env);
1008 void qemu_notify_event(void)
1010 qemu_event_increment();
1013 void cpu_stop_current(void)
1015 if (cpu_single_env) {
1016 cpu_single_env->stop = 0;
1017 cpu_single_env->stopped = 1;
1018 cpu_exit(cpu_single_env);
1019 qemu_cond_signal(&qemu_pause_cond);
1023 void vm_stop(int reason)
1025 if (!qemu_thread_is_self(&io_thread)) {
1026 qemu_system_vmstop_request(reason);
1028 * FIXME: should not return to device code in case
1029 * vm_stop() has been requested.
1031 cpu_stop_current();
1032 return;
1034 do_vm_stop(reason);
1037 #endif
1039 static int tcg_cpu_exec(CPUState *env)
1041 int ret;
1042 #ifdef CONFIG_PROFILER
1043 int64_t ti;
1044 #endif
1046 #ifdef CONFIG_PROFILER
1047 ti = profile_getclock();
1048 #endif
1049 if (use_icount) {
1050 int64_t count;
1051 int decr;
1052 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1053 env->icount_decr.u16.low = 0;
1054 env->icount_extra = 0;
1055 count = qemu_icount_round(qemu_next_icount_deadline());
1056 qemu_icount += count;
1057 decr = (count > 0xffff) ? 0xffff : count;
1058 count -= decr;
1059 env->icount_decr.u16.low = decr;
1060 env->icount_extra = count;
1062 ret = cpu_exec(env);
1063 #ifdef CONFIG_PROFILER
1064 qemu_time += profile_getclock() - ti;
1065 #endif
1066 if (use_icount) {
1067 /* Fold pending instructions back into the
1068 instruction counter, and clear the interrupt flag. */
1069 qemu_icount -= (env->icount_decr.u16.low
1070 + env->icount_extra);
1071 env->icount_decr.u32 = 0;
1072 env->icount_extra = 0;
1074 return ret;
1077 bool cpu_exec_all(void)
1079 int r;
1081 /* Account partial waits to the vm_clock. */
1082 qemu_clock_warp(vm_clock);
1084 if (next_cpu == NULL) {
1085 next_cpu = first_cpu;
1087 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1088 CPUState *env = next_cpu;
1090 qemu_clock_enable(vm_clock,
1091 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1093 #ifndef CONFIG_IOTHREAD
1094 if (qemu_alarm_pending()) {
1095 break;
1097 #endif
1098 if (cpu_can_run(env)) {
1099 if (kvm_enabled()) {
1100 r = kvm_cpu_exec(env);
1101 qemu_kvm_eat_signals(env);
1102 } else {
1103 r = tcg_cpu_exec(env);
1105 if (r == EXCP_DEBUG) {
1106 cpu_handle_guest_debug(env);
1107 break;
1109 } else if (env->stop || env->stopped) {
1110 break;
1113 exit_request = 0;
1114 return !all_cpu_threads_idle();
1117 void set_numa_modes(void)
1119 CPUState *env;
1120 int i;
1122 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1123 for (i = 0; i < nb_numa_nodes; i++) {
1124 if (node_cpumask[i] & (1 << env->cpu_index)) {
1125 env->numa_node = i;
1131 void set_cpu_log(const char *optarg)
1133 int mask;
1134 const CPULogItem *item;
1136 mask = cpu_str_to_log_mask(optarg);
1137 if (!mask) {
1138 printf("Log items (comma separated):\n");
1139 for (item = cpu_log_items; item->mask != 0; item++) {
1140 printf("%-10s %s\n", item->name, item->help);
1142 exit(1);
1144 cpu_set_log(mask);
1147 void set_cpu_log_filename(const char *optarg)
1149 cpu_set_log_filename(optarg);
1152 /* Return the virtual CPU time, based on the instruction counter. */
1153 int64_t cpu_get_icount(void)
1155 int64_t icount;
1156 CPUState *env = cpu_single_env;;
1158 icount = qemu_icount;
1159 if (env) {
1160 if (!can_do_io(env)) {
1161 fprintf(stderr, "Bad clock read\n");
1163 icount -= (env->icount_decr.u16.low + env->icount_extra);
1165 return qemu_icount_bias + (icount << icount_time_shift);
1168 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1170 /* XXX: implement xxx_cpu_list for targets that still miss it */
1171 #if defined(cpu_list_id)
1172 cpu_list_id(f, cpu_fprintf, optarg);
1173 #elif defined(cpu_list)
1174 cpu_list(f, cpu_fprintf); /* deprecated */
1175 #endif