Improve vm_stop reason declarations
[qemu/stefanha.git] / cpus.c
blobca1f01d9dab4251eb88819384720dcbc83a19874
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 "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 return false;
153 return true;
156 static bool all_cpu_threads_idle(void)
158 CPUState *env;
160 for (env = first_cpu; env != NULL; env = env->next_cpu) {
161 if (!cpu_thread_is_idle(env)) {
162 return false;
165 return true;
168 static void cpu_debug_handler(CPUState *env)
170 gdb_set_stop_cpu(env);
171 debug_requested = VMSTOP_DEBUG;
172 vm_stop(VMSTOP_DEBUG);
175 #ifdef CONFIG_LINUX
176 static void sigbus_reraise(void)
178 sigset_t set;
179 struct sigaction action;
181 memset(&action, 0, sizeof(action));
182 action.sa_handler = SIG_DFL;
183 if (!sigaction(SIGBUS, &action, NULL)) {
184 raise(SIGBUS);
185 sigemptyset(&set);
186 sigaddset(&set, SIGBUS);
187 sigprocmask(SIG_UNBLOCK, &set, NULL);
189 perror("Failed to re-raise SIGBUS!\n");
190 abort();
193 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
194 void *ctx)
196 if (kvm_on_sigbus(siginfo->ssi_code,
197 (void *)(intptr_t)siginfo->ssi_addr)) {
198 sigbus_reraise();
202 static void qemu_init_sigbus(void)
204 struct sigaction action;
206 memset(&action, 0, sizeof(action));
207 action.sa_flags = SA_SIGINFO;
208 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
209 sigaction(SIGBUS, &action, NULL);
211 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
214 #else /* !CONFIG_LINUX */
216 static void qemu_init_sigbus(void)
219 #endif /* !CONFIG_LINUX */
221 #ifndef _WIN32
222 static int io_thread_fd = -1;
224 static void qemu_event_increment(void)
226 /* Write 8 bytes to be compatible with eventfd. */
227 static const uint64_t val = 1;
228 ssize_t ret;
230 if (io_thread_fd == -1) {
231 return;
233 do {
234 ret = write(io_thread_fd, &val, sizeof(val));
235 } while (ret < 0 && errno == EINTR);
237 /* EAGAIN is fine, a read must be pending. */
238 if (ret < 0 && errno != EAGAIN) {
239 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
240 strerror(errno));
241 exit (1);
245 static void qemu_event_read(void *opaque)
247 int fd = (unsigned long)opaque;
248 ssize_t len;
249 char buffer[512];
251 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
252 do {
253 len = read(fd, buffer, sizeof(buffer));
254 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
257 static int qemu_event_init(void)
259 int err;
260 int fds[2];
262 err = qemu_eventfd(fds);
263 if (err == -1) {
264 return -errno;
266 err = fcntl_setfl(fds[0], O_NONBLOCK);
267 if (err < 0) {
268 goto fail;
270 err = fcntl_setfl(fds[1], O_NONBLOCK);
271 if (err < 0) {
272 goto fail;
274 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
275 (void *)(unsigned long)fds[0]);
277 io_thread_fd = fds[1];
278 return 0;
280 fail:
281 close(fds[0]);
282 close(fds[1]);
283 return err;
286 static void dummy_signal(int sig)
290 /* If we have signalfd, we mask out the signals we want to handle and then
291 * use signalfd to listen for them. We rely on whatever the current signal
292 * handler is to dispatch the signals when we receive them.
294 static void sigfd_handler(void *opaque)
296 int fd = (unsigned long) opaque;
297 struct qemu_signalfd_siginfo info;
298 struct sigaction action;
299 ssize_t len;
301 while (1) {
302 do {
303 len = read(fd, &info, sizeof(info));
304 } while (len == -1 && errno == EINTR);
306 if (len == -1 && errno == EAGAIN) {
307 break;
310 if (len != sizeof(info)) {
311 printf("read from sigfd returned %zd: %m\n", len);
312 return;
315 sigaction(info.ssi_signo, NULL, &action);
316 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
317 action.sa_sigaction(info.ssi_signo,
318 (siginfo_t *)&info, NULL);
319 } else if (action.sa_handler) {
320 action.sa_handler(info.ssi_signo);
325 static int qemu_signalfd_init(sigset_t mask)
327 int sigfd;
329 sigfd = qemu_signalfd(&mask);
330 if (sigfd == -1) {
331 fprintf(stderr, "failed to create signalfd\n");
332 return -errno;
335 fcntl_setfl(sigfd, O_NONBLOCK);
337 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
338 (void *)(unsigned long) sigfd);
340 return 0;
343 static void qemu_kvm_eat_signals(CPUState *env)
345 struct timespec ts = { 0, 0 };
346 siginfo_t siginfo;
347 sigset_t waitset;
348 sigset_t chkset;
349 int r;
351 sigemptyset(&waitset);
352 sigaddset(&waitset, SIG_IPI);
353 sigaddset(&waitset, SIGBUS);
355 do {
356 r = sigtimedwait(&waitset, &siginfo, &ts);
357 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
358 perror("sigtimedwait");
359 exit(1);
362 switch (r) {
363 case SIGBUS:
364 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
365 sigbus_reraise();
367 break;
368 default:
369 break;
372 r = sigpending(&chkset);
373 if (r == -1) {
374 perror("sigpending");
375 exit(1);
377 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
379 #ifndef CONFIG_IOTHREAD
380 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
381 qemu_notify_event();
383 #endif
386 #else /* _WIN32 */
388 HANDLE qemu_event_handle;
390 static void dummy_event_handler(void *opaque)
394 static int qemu_event_init(void)
396 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
397 if (!qemu_event_handle) {
398 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
399 return -1;
401 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
402 return 0;
405 static void qemu_event_increment(void)
407 if (!SetEvent(qemu_event_handle)) {
408 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
409 GetLastError());
410 exit (1);
414 static void qemu_kvm_eat_signals(CPUState *env)
417 #endif /* _WIN32 */
419 #ifndef CONFIG_IOTHREAD
420 static void qemu_kvm_init_cpu_signals(CPUState *env)
422 #ifndef _WIN32
423 int r;
424 sigset_t set;
425 struct sigaction sigact;
427 memset(&sigact, 0, sizeof(sigact));
428 sigact.sa_handler = dummy_signal;
429 sigaction(SIG_IPI, &sigact, NULL);
431 sigemptyset(&set);
432 sigaddset(&set, SIG_IPI);
433 sigaddset(&set, SIGIO);
434 sigaddset(&set, SIGALRM);
435 pthread_sigmask(SIG_BLOCK, &set, NULL);
437 pthread_sigmask(SIG_BLOCK, NULL, &set);
438 sigdelset(&set, SIG_IPI);
439 sigdelset(&set, SIGBUS);
440 sigdelset(&set, SIGIO);
441 sigdelset(&set, SIGALRM);
442 r = kvm_set_signal_mask(env, &set);
443 if (r) {
444 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
445 exit(1);
447 #endif
450 #ifndef _WIN32
451 static sigset_t block_synchronous_signals(void)
453 sigset_t set;
455 sigemptyset(&set);
456 sigaddset(&set, SIGBUS);
457 if (kvm_enabled()) {
459 * We need to process timer signals synchronously to avoid a race
460 * between exit_request check and KVM vcpu entry.
462 sigaddset(&set, SIGIO);
463 sigaddset(&set, SIGALRM);
466 return set;
468 #endif
470 int qemu_init_main_loop(void)
472 #ifndef _WIN32
473 sigset_t blocked_signals;
474 int ret;
476 blocked_signals = block_synchronous_signals();
478 ret = qemu_signalfd_init(blocked_signals);
479 if (ret) {
480 return ret;
482 #endif
483 cpu_set_debug_excp_handler(cpu_debug_handler);
485 qemu_init_sigbus();
487 return qemu_event_init();
490 void qemu_main_loop_start(void)
494 void qemu_init_vcpu(void *_env)
496 CPUState *env = _env;
497 int r;
499 env->nr_cores = smp_cores;
500 env->nr_threads = smp_threads;
502 if (kvm_enabled()) {
503 r = kvm_init_vcpu(env);
504 if (r < 0) {
505 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
506 exit(1);
508 qemu_kvm_init_cpu_signals(env);
512 int qemu_cpu_self(void *env)
514 return 1;
517 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
519 func(data);
522 void resume_all_vcpus(void)
526 void pause_all_vcpus(void)
530 void qemu_cpu_kick(void *env)
534 void qemu_cpu_kick_self(void)
536 #ifndef _WIN32
537 assert(cpu_single_env);
539 raise(SIG_IPI);
540 #else
541 abort();
542 #endif
545 void qemu_notify_event(void)
547 CPUState *env = cpu_single_env;
549 qemu_event_increment ();
550 if (env) {
551 cpu_exit(env);
553 if (next_cpu && env != next_cpu) {
554 cpu_exit(next_cpu);
556 exit_request = 1;
559 void qemu_mutex_lock_iothread(void) {}
560 void qemu_mutex_unlock_iothread(void) {}
562 void cpu_stop_current(void)
566 void vm_stop(int reason)
568 do_vm_stop(reason);
571 #else /* CONFIG_IOTHREAD */
573 #include "qemu-thread.h"
575 QemuMutex qemu_global_mutex;
576 static QemuMutex qemu_fair_mutex;
578 static QemuThread io_thread;
580 static QemuThread *tcg_cpu_thread;
581 static QemuCond *tcg_halt_cond;
583 static int qemu_system_ready;
584 /* cpu creation */
585 static QemuCond qemu_cpu_cond;
586 /* system init */
587 static QemuCond qemu_system_cond;
588 static QemuCond qemu_pause_cond;
589 static QemuCond qemu_work_cond;
591 static void cpu_signal(int sig)
593 if (cpu_single_env) {
594 cpu_exit(cpu_single_env);
596 exit_request = 1;
599 static void qemu_kvm_init_cpu_signals(CPUState *env)
601 int r;
602 sigset_t set;
603 struct sigaction sigact;
605 memset(&sigact, 0, sizeof(sigact));
606 sigact.sa_handler = dummy_signal;
607 sigaction(SIG_IPI, &sigact, NULL);
609 pthread_sigmask(SIG_BLOCK, NULL, &set);
610 sigdelset(&set, SIG_IPI);
611 sigdelset(&set, SIGBUS);
612 r = kvm_set_signal_mask(env, &set);
613 if (r) {
614 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
615 exit(1);
619 static void qemu_tcg_init_cpu_signals(void)
621 sigset_t set;
622 struct sigaction sigact;
624 memset(&sigact, 0, sizeof(sigact));
625 sigact.sa_handler = cpu_signal;
626 sigaction(SIG_IPI, &sigact, NULL);
628 sigemptyset(&set);
629 sigaddset(&set, SIG_IPI);
630 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
633 static sigset_t block_io_signals(void)
635 sigset_t set;
637 /* SIGUSR2 used by posix-aio-compat.c */
638 sigemptyset(&set);
639 sigaddset(&set, SIGUSR2);
640 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
642 sigemptyset(&set);
643 sigaddset(&set, SIGIO);
644 sigaddset(&set, SIGALRM);
645 sigaddset(&set, SIG_IPI);
646 sigaddset(&set, SIGBUS);
647 pthread_sigmask(SIG_BLOCK, &set, NULL);
649 return set;
652 int qemu_init_main_loop(void)
654 int ret;
655 sigset_t blocked_signals;
657 cpu_set_debug_excp_handler(cpu_debug_handler);
659 qemu_init_sigbus();
661 blocked_signals = block_io_signals();
663 ret = qemu_signalfd_init(blocked_signals);
664 if (ret) {
665 return ret;
668 /* Note eventfd must be drained before signalfd handlers run */
669 ret = qemu_event_init();
670 if (ret) {
671 return ret;
674 qemu_cond_init(&qemu_pause_cond);
675 qemu_cond_init(&qemu_system_cond);
676 qemu_mutex_init(&qemu_fair_mutex);
677 qemu_mutex_init(&qemu_global_mutex);
678 qemu_mutex_lock(&qemu_global_mutex);
680 qemu_thread_self(&io_thread);
682 return 0;
685 void qemu_main_loop_start(void)
687 qemu_system_ready = 1;
688 qemu_cond_broadcast(&qemu_system_cond);
691 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
693 struct qemu_work_item wi;
695 if (qemu_cpu_self(env)) {
696 func(data);
697 return;
700 wi.func = func;
701 wi.data = data;
702 if (!env->queued_work_first) {
703 env->queued_work_first = &wi;
704 } else {
705 env->queued_work_last->next = &wi;
707 env->queued_work_last = &wi;
708 wi.next = NULL;
709 wi.done = false;
711 qemu_cpu_kick(env);
712 while (!wi.done) {
713 CPUState *self_env = cpu_single_env;
715 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
716 cpu_single_env = self_env;
720 static void flush_queued_work(CPUState *env)
722 struct qemu_work_item *wi;
724 if (!env->queued_work_first) {
725 return;
728 while ((wi = env->queued_work_first)) {
729 env->queued_work_first = wi->next;
730 wi->func(wi->data);
731 wi->done = true;
733 env->queued_work_last = NULL;
734 qemu_cond_broadcast(&qemu_work_cond);
737 static void qemu_wait_io_event_common(CPUState *env)
739 if (env->stop) {
740 env->stop = 0;
741 env->stopped = 1;
742 qemu_cond_signal(&qemu_pause_cond);
744 flush_queued_work(env);
745 env->thread_kicked = false;
748 static void qemu_tcg_wait_io_event(void)
750 CPUState *env;
752 while (all_cpu_threads_idle()) {
753 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
756 qemu_mutex_unlock(&qemu_global_mutex);
759 * Users of qemu_global_mutex can be starved, having no chance
760 * to acquire it since this path will get to it first.
761 * So use another lock to provide fairness.
763 qemu_mutex_lock(&qemu_fair_mutex);
764 qemu_mutex_unlock(&qemu_fair_mutex);
766 qemu_mutex_lock(&qemu_global_mutex);
768 for (env = first_cpu; env != NULL; env = env->next_cpu) {
769 qemu_wait_io_event_common(env);
773 static void qemu_kvm_wait_io_event(CPUState *env)
775 while (cpu_thread_is_idle(env)) {
776 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
779 qemu_kvm_eat_signals(env);
780 qemu_wait_io_event_common(env);
783 static int qemu_cpu_exec(CPUState *env);
785 static void *qemu_kvm_cpu_thread_fn(void *arg)
787 CPUState *env = arg;
788 int r;
790 qemu_mutex_lock(&qemu_global_mutex);
791 qemu_thread_self(env->thread);
793 r = kvm_init_vcpu(env);
794 if (r < 0) {
795 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
796 exit(1);
799 qemu_kvm_init_cpu_signals(env);
801 /* signal CPU creation */
802 env->created = 1;
803 qemu_cond_signal(&qemu_cpu_cond);
805 /* and wait for machine initialization */
806 while (!qemu_system_ready) {
807 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
810 while (1) {
811 if (cpu_can_run(env)) {
812 qemu_cpu_exec(env);
814 qemu_kvm_wait_io_event(env);
817 return NULL;
820 static void *qemu_tcg_cpu_thread_fn(void *arg)
822 CPUState *env = arg;
824 qemu_tcg_init_cpu_signals();
825 qemu_thread_self(env->thread);
827 /* signal CPU creation */
828 qemu_mutex_lock(&qemu_global_mutex);
829 for (env = first_cpu; env != NULL; env = env->next_cpu) {
830 env->created = 1;
832 qemu_cond_signal(&qemu_cpu_cond);
834 /* and wait for machine initialization */
835 while (!qemu_system_ready) {
836 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
839 while (1) {
840 cpu_exec_all();
841 qemu_tcg_wait_io_event();
844 return NULL;
847 void qemu_cpu_kick(void *_env)
849 CPUState *env = _env;
851 qemu_cond_broadcast(env->halt_cond);
852 if (!env->thread_kicked) {
853 qemu_thread_signal(env->thread, SIG_IPI);
854 env->thread_kicked = true;
858 void qemu_cpu_kick_self(void)
860 assert(cpu_single_env);
862 if (!cpu_single_env->thread_kicked) {
863 qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
864 cpu_single_env->thread_kicked = true;
868 int qemu_cpu_self(void *_env)
870 CPUState *env = _env;
871 QemuThread this;
873 qemu_thread_self(&this);
875 return qemu_thread_equal(&this, env->thread);
878 void qemu_mutex_lock_iothread(void)
880 if (kvm_enabled()) {
881 qemu_mutex_lock(&qemu_global_mutex);
882 } else {
883 qemu_mutex_lock(&qemu_fair_mutex);
884 if (qemu_mutex_trylock(&qemu_global_mutex)) {
885 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
886 qemu_mutex_lock(&qemu_global_mutex);
888 qemu_mutex_unlock(&qemu_fair_mutex);
892 void qemu_mutex_unlock_iothread(void)
894 qemu_mutex_unlock(&qemu_global_mutex);
897 static int all_vcpus_paused(void)
899 CPUState *penv = first_cpu;
901 while (penv) {
902 if (!penv->stopped) {
903 return 0;
905 penv = (CPUState *)penv->next_cpu;
908 return 1;
911 void pause_all_vcpus(void)
913 CPUState *penv = first_cpu;
915 while (penv) {
916 penv->stop = 1;
917 qemu_cpu_kick(penv);
918 penv = (CPUState *)penv->next_cpu;
921 while (!all_vcpus_paused()) {
922 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
923 penv = first_cpu;
924 while (penv) {
925 qemu_cpu_kick(penv);
926 penv = (CPUState *)penv->next_cpu;
931 void resume_all_vcpus(void)
933 CPUState *penv = first_cpu;
935 while (penv) {
936 penv->stop = 0;
937 penv->stopped = 0;
938 qemu_cpu_kick(penv);
939 penv = (CPUState *)penv->next_cpu;
943 static void qemu_tcg_init_vcpu(void *_env)
945 CPUState *env = _env;
947 /* share a single thread for all cpus with TCG */
948 if (!tcg_cpu_thread) {
949 env->thread = qemu_mallocz(sizeof(QemuThread));
950 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
951 qemu_cond_init(env->halt_cond);
952 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
953 while (env->created == 0) {
954 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
956 tcg_cpu_thread = env->thread;
957 tcg_halt_cond = env->halt_cond;
958 } else {
959 env->thread = tcg_cpu_thread;
960 env->halt_cond = tcg_halt_cond;
964 static void qemu_kvm_start_vcpu(CPUState *env)
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_kvm_cpu_thread_fn, env);
970 while (env->created == 0) {
971 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
975 void qemu_init_vcpu(void *_env)
977 CPUState *env = _env;
979 env->nr_cores = smp_cores;
980 env->nr_threads = smp_threads;
981 if (kvm_enabled()) {
982 qemu_kvm_start_vcpu(env);
983 } else {
984 qemu_tcg_init_vcpu(env);
988 void qemu_notify_event(void)
990 qemu_event_increment();
993 static void qemu_system_vmstop_request(int reason)
995 vmstop_requested = reason;
996 qemu_notify_event();
999 void cpu_stop_current(void)
1001 if (cpu_single_env) {
1002 cpu_single_env->stopped = 1;
1003 cpu_exit(cpu_single_env);
1007 void vm_stop(int reason)
1009 QemuThread me;
1010 qemu_thread_self(&me);
1012 if (!qemu_thread_equal(&me, &io_thread)) {
1013 qemu_system_vmstop_request(reason);
1015 * FIXME: should not return to device code in case
1016 * vm_stop() has been requested.
1018 cpu_stop_current();
1019 return;
1021 do_vm_stop(reason);
1024 #endif
1026 static int qemu_cpu_exec(CPUState *env)
1028 int ret;
1029 #ifdef CONFIG_PROFILER
1030 int64_t ti;
1031 #endif
1033 #ifdef CONFIG_PROFILER
1034 ti = profile_getclock();
1035 #endif
1036 if (use_icount) {
1037 int64_t count;
1038 int decr;
1039 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1040 env->icount_decr.u16.low = 0;
1041 env->icount_extra = 0;
1042 count = qemu_icount_round (qemu_next_deadline());
1043 qemu_icount += count;
1044 decr = (count > 0xffff) ? 0xffff : count;
1045 count -= decr;
1046 env->icount_decr.u16.low = decr;
1047 env->icount_extra = count;
1049 ret = cpu_exec(env);
1050 #ifdef CONFIG_PROFILER
1051 qemu_time += profile_getclock() - ti;
1052 #endif
1053 if (use_icount) {
1054 /* Fold pending instructions back into the
1055 instruction counter, and clear the interrupt flag. */
1056 qemu_icount -= (env->icount_decr.u16.low
1057 + env->icount_extra);
1058 env->icount_decr.u32 = 0;
1059 env->icount_extra = 0;
1061 return ret;
1064 bool cpu_exec_all(void)
1066 int r;
1068 if (next_cpu == NULL) {
1069 next_cpu = first_cpu;
1071 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1072 CPUState *env = next_cpu;
1074 qemu_clock_enable(vm_clock,
1075 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1077 if (qemu_alarm_pending()) {
1078 break;
1080 if (cpu_can_run(env)) {
1081 r = qemu_cpu_exec(env);
1082 if (kvm_enabled()) {
1083 qemu_kvm_eat_signals(env);
1085 if (r == EXCP_DEBUG) {
1086 break;
1088 } else if (env->stop) {
1089 break;
1092 exit_request = 0;
1093 return !all_cpu_threads_idle();
1096 void set_numa_modes(void)
1098 CPUState *env;
1099 int i;
1101 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1102 for (i = 0; i < nb_numa_nodes; i++) {
1103 if (node_cpumask[i] & (1 << env->cpu_index)) {
1104 env->numa_node = i;
1110 void set_cpu_log(const char *optarg)
1112 int mask;
1113 const CPULogItem *item;
1115 mask = cpu_str_to_log_mask(optarg);
1116 if (!mask) {
1117 printf("Log items (comma separated):\n");
1118 for (item = cpu_log_items; item->mask != 0; item++) {
1119 printf("%-10s %s\n", item->name, item->help);
1121 exit(1);
1123 cpu_set_log(mask);
1126 /* Return the virtual CPU time, based on the instruction counter. */
1127 int64_t cpu_get_icount(void)
1129 int64_t icount;
1130 CPUState *env = cpu_single_env;;
1132 icount = qemu_icount;
1133 if (env) {
1134 if (!can_do_io(env)) {
1135 fprintf(stderr, "Bad clock read\n");
1137 icount -= (env->icount_decr.u16.low + env->icount_extra);
1139 return qemu_icount_bias + (icount << icount_time_shift);
1142 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1144 /* XXX: implement xxx_cpu_list for targets that still miss it */
1145 #if defined(cpu_list_id)
1146 cpu_list_id(f, cpu_fprintf, optarg);
1147 #elif defined(cpu_list)
1148 cpu_list(f, cpu_fprintf); /* deprecated */
1149 #endif