Merge commit 'b4a3d965dee06d52281496bb5fd0a5cb5534b545' into upstream-merge
[qemu-kvm.git] / cpus.c
blob553af89878b167dbc0fd993f60d08c3587536c14
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"
37 #ifdef CONFIG_LINUX
38 #include <sys/prctl.h>
39 #endif
41 #ifdef SIGRTMIN
42 #define SIG_IPI (SIGRTMIN+4)
43 #else
44 #define SIG_IPI SIGUSR1
45 #endif
47 #ifndef PR_MCE_KILL
48 #define PR_MCE_KILL 33
49 #endif
51 static CPUState *next_cpu;
53 /***********************************************************/
54 void hw_error(const char *fmt, ...)
56 va_list ap;
57 CPUState *env;
59 va_start(ap, fmt);
60 fprintf(stderr, "qemu: hardware error: ");
61 vfprintf(stderr, fmt, ap);
62 fprintf(stderr, "\n");
63 for(env = first_cpu; env != NULL; env = env->next_cpu) {
64 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
65 #ifdef TARGET_I386
66 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
67 #else
68 cpu_dump_state(env, stderr, fprintf, 0);
69 #endif
71 va_end(ap);
72 abort();
75 void cpu_synchronize_all_states(void)
77 CPUState *cpu;
79 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
80 cpu_synchronize_state(cpu);
84 void cpu_synchronize_all_post_reset(void)
86 CPUState *cpu;
88 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
89 cpu_synchronize_post_reset(cpu);
93 void cpu_synchronize_all_post_init(void)
95 CPUState *cpu;
97 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
98 cpu_synchronize_post_init(cpu);
102 int cpu_is_stopped(CPUState *env)
104 return !vm_running || env->stopped;
107 static void do_vm_stop(int reason)
109 if (vm_running) {
110 cpu_disable_ticks();
111 vm_running = 0;
112 pause_all_vcpus();
113 vm_state_notify(0, reason);
114 qemu_aio_flush();
115 bdrv_flush_all();
116 monitor_protocol_event(QEVENT_STOP, NULL);
120 static int cpu_can_run(CPUState *env)
122 if (env->stop)
123 return 0;
124 if (env->stopped || !vm_running)
125 return 0;
126 return 1;
129 static int cpu_has_work(CPUState *env)
131 if (env->stop)
132 return 1;
133 if (env->queued_work_first)
134 return 1;
135 if (env->stopped || !vm_running)
136 return 0;
137 if (!env->halted)
138 return 1;
139 if (qemu_cpu_has_work(env))
140 return 1;
141 return 0;
144 static int any_cpu_has_work(void)
146 CPUState *env;
148 for (env = first_cpu; env != NULL; env = env->next_cpu)
149 if (cpu_has_work(env))
150 return 1;
151 return 0;
154 static void cpu_debug_handler(CPUState *env)
156 gdb_set_stop_cpu(env);
157 debug_requested = EXCP_DEBUG;
158 vm_stop(EXCP_DEBUG);
161 #ifndef _WIN32
162 static int io_thread_fd = -1;
164 static void qemu_event_increment(void)
166 /* Write 8 bytes to be compatible with eventfd. */
167 static const uint64_t val = 1;
168 ssize_t ret;
170 if (io_thread_fd == -1)
171 return;
173 do {
174 ret = write(io_thread_fd, &val, sizeof(val));
175 } while (ret < 0 && errno == EINTR);
177 /* EAGAIN is fine, a read must be pending. */
178 if (ret < 0 && errno != EAGAIN) {
179 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
180 strerror(errno));
181 exit (1);
185 static void qemu_event_read(void *opaque)
187 int fd = (unsigned long)opaque;
188 ssize_t len;
189 char buffer[512];
191 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
192 do {
193 len = read(fd, buffer, sizeof(buffer));
194 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
197 static int qemu_event_init(void)
199 int err;
200 int fds[2];
202 err = qemu_eventfd(fds);
203 if (err == -1)
204 return -errno;
206 err = fcntl_setfl(fds[0], O_NONBLOCK);
207 if (err < 0)
208 goto fail;
210 err = fcntl_setfl(fds[1], O_NONBLOCK);
211 if (err < 0)
212 goto fail;
214 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
215 (void *)(unsigned long)fds[0]);
217 io_thread_fd = fds[1];
218 return 0;
220 fail:
221 close(fds[0]);
222 close(fds[1]);
223 return err;
225 #else
226 HANDLE qemu_event_handle;
228 static void dummy_event_handler(void *opaque)
232 static int qemu_event_init(void)
234 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
235 if (!qemu_event_handle) {
236 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
237 return -1;
239 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
240 return 0;
243 static void qemu_event_increment(void)
245 if (!SetEvent(qemu_event_handle)) {
246 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
247 GetLastError());
248 exit (1);
251 #endif
253 #ifndef CONFIG_IOTHREAD
254 int qemu_init_main_loop(void)
256 cpu_set_debug_excp_handler(cpu_debug_handler);
258 return qemu_event_init();
261 void qemu_main_loop_start(void)
265 void qemu_init_vcpu(void *_env)
267 CPUState *env = _env;
269 env->nr_cores = smp_cores;
270 env->nr_threads = smp_threads;
271 if (kvm_enabled())
272 kvm_init_vcpu(env);
273 return;
276 int qemu_cpu_self(void *env)
278 return 1;
281 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
283 func(data);
286 void resume_all_vcpus(void)
290 void pause_all_vcpus(void)
294 void qemu_cpu_kick(void *env)
296 return;
299 void qemu_notify_event(void)
301 CPUState *env = cpu_single_env;
303 qemu_event_increment ();
304 if (env) {
305 cpu_exit(env);
307 if (next_cpu && env != next_cpu) {
308 cpu_exit(next_cpu);
312 #if defined(OBSOLETE_KVM_IMPL) || !defined(CONFIG_KVM)
313 void qemu_mutex_lock_iothread(void) {}
314 void qemu_mutex_unlock_iothread(void) {}
315 #endif
317 void cpu_stop_current(void)
321 void vm_stop(int reason)
323 do_vm_stop(reason);
326 #else /* CONFIG_IOTHREAD */
328 #include "qemu-thread.h"
330 QemuMutex qemu_global_mutex;
331 static QemuMutex qemu_fair_mutex;
333 static QemuThread io_thread;
335 static QemuThread *tcg_cpu_thread;
336 static QemuCond *tcg_halt_cond;
338 static int qemu_system_ready;
339 /* cpu creation */
340 static QemuCond qemu_cpu_cond;
341 /* system init */
342 static QemuCond qemu_system_cond;
343 static QemuCond qemu_pause_cond;
344 static QemuCond qemu_work_cond;
346 static void tcg_init_ipi(void);
347 static void kvm_init_ipi(CPUState *env);
348 static sigset_t block_io_signals(void);
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 = (unsigned long) 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_signalfd_init(sigset_t mask)
387 int sigfd;
389 sigfd = qemu_signalfd(&mask);
390 if (sigfd == -1) {
391 fprintf(stderr, "failed to create signalfd\n");
392 return -errno;
395 fcntl_setfl(sigfd, O_NONBLOCK);
397 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
398 (void *)(unsigned long) sigfd);
400 return 0;
403 int qemu_init_main_loop(void)
405 int ret;
406 sigset_t blocked_signals;
408 cpu_set_debug_excp_handler(cpu_debug_handler);
410 blocked_signals = block_io_signals();
412 ret = qemu_signalfd_init(blocked_signals);
413 if (ret)
414 return ret;
416 /* Note eventfd must be drained before signalfd handlers run */
417 ret = qemu_event_init();
418 if (ret)
419 return ret;
421 qemu_cond_init(&qemu_pause_cond);
422 qemu_cond_init(&qemu_system_cond);
423 qemu_mutex_init(&qemu_fair_mutex);
424 qemu_mutex_init(&qemu_global_mutex);
425 qemu_mutex_lock(&qemu_global_mutex);
427 qemu_thread_self(&io_thread);
429 return 0;
432 void qemu_main_loop_start(void)
434 qemu_system_ready = 1;
435 qemu_cond_broadcast(&qemu_system_cond);
438 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
440 struct qemu_work_item wi;
442 if (qemu_cpu_self(env)) {
443 func(data);
444 return;
447 wi.func = func;
448 wi.data = data;
449 if (!env->queued_work_first)
450 env->queued_work_first = &wi;
451 else
452 env->queued_work_last->next = &wi;
453 env->queued_work_last = &wi;
454 wi.next = NULL;
455 wi.done = false;
457 qemu_cpu_kick(env);
458 while (!wi.done) {
459 CPUState *self_env = cpu_single_env;
461 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
462 cpu_single_env = self_env;
466 static void flush_queued_work(CPUState *env)
468 struct qemu_work_item *wi;
470 if (!env->queued_work_first)
471 return;
473 while ((wi = env->queued_work_first)) {
474 env->queued_work_first = wi->next;
475 wi->func(wi->data);
476 wi->done = true;
478 env->queued_work_last = NULL;
479 qemu_cond_broadcast(&qemu_work_cond);
482 static void qemu_wait_io_event_common(CPUState *env)
484 if (env->stop) {
485 env->stop = 0;
486 env->stopped = 1;
487 qemu_cond_signal(&qemu_pause_cond);
489 flush_queued_work(env);
490 env->thread_kicked = false;
493 static void qemu_tcg_wait_io_event(void)
495 CPUState *env;
497 while (!any_cpu_has_work())
498 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
500 qemu_mutex_unlock(&qemu_global_mutex);
503 * Users of qemu_global_mutex can be starved, having no chance
504 * to acquire it since this path will get to it first.
505 * So use another lock to provide fairness.
507 qemu_mutex_lock(&qemu_fair_mutex);
508 qemu_mutex_unlock(&qemu_fair_mutex);
510 qemu_mutex_lock(&qemu_global_mutex);
512 for (env = first_cpu; env != NULL; env = env->next_cpu) {
513 qemu_wait_io_event_common(env);
517 static void sigbus_reraise(void)
519 sigset_t set;
520 struct sigaction action;
522 memset(&action, 0, sizeof(action));
523 action.sa_handler = SIG_DFL;
524 if (!sigaction(SIGBUS, &action, NULL)) {
525 raise(SIGBUS);
526 sigemptyset(&set);
527 sigaddset(&set, SIGBUS);
528 sigprocmask(SIG_UNBLOCK, &set, NULL);
530 perror("Failed to re-raise SIGBUS!\n");
531 abort();
534 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
535 void *ctx)
537 #if defined(TARGET_I386)
538 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr))
539 #endif
540 sigbus_reraise();
543 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
545 struct timespec ts;
546 int r, e;
547 siginfo_t siginfo;
548 sigset_t waitset;
549 sigset_t chkset;
551 ts.tv_sec = timeout / 1000;
552 ts.tv_nsec = (timeout % 1000) * 1000000;
554 sigemptyset(&waitset);
555 sigaddset(&waitset, SIG_IPI);
556 sigaddset(&waitset, SIGBUS);
558 do {
559 qemu_mutex_unlock(&qemu_global_mutex);
561 r = sigtimedwait(&waitset, &siginfo, &ts);
562 e = errno;
564 qemu_mutex_lock(&qemu_global_mutex);
566 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
567 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
568 exit(1);
571 switch (r) {
572 case SIGBUS:
573 #ifdef TARGET_I386
574 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr))
575 #endif
576 sigbus_reraise();
577 break;
578 default:
579 break;
582 r = sigpending(&chkset);
583 if (r == -1) {
584 fprintf(stderr, "sigpending: %s\n", strerror(e));
585 exit(1);
587 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
590 static void qemu_kvm_wait_io_event(CPUState *env)
592 while (!cpu_has_work(env))
593 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
595 qemu_kvm_eat_signal(env, 0);
596 qemu_wait_io_event_common(env);
599 static int qemu_cpu_exec(CPUState *env);
601 static void *kvm_cpu_thread_fn(void *arg)
603 CPUState *env = arg;
605 qemu_mutex_lock(&qemu_global_mutex);
606 qemu_thread_self(env->thread);
607 if (kvm_enabled())
608 kvm_init_vcpu(env);
610 kvm_init_ipi(env);
612 /* signal CPU creation */
613 env->created = 1;
614 qemu_cond_signal(&qemu_cpu_cond);
616 /* and wait for machine initialization */
617 while (!qemu_system_ready)
618 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
620 while (1) {
621 if (cpu_can_run(env))
622 qemu_cpu_exec(env);
623 qemu_kvm_wait_io_event(env);
626 return NULL;
629 static void *tcg_cpu_thread_fn(void *arg)
631 CPUState *env = arg;
633 tcg_init_ipi();
634 qemu_thread_self(env->thread);
636 /* signal CPU creation */
637 qemu_mutex_lock(&qemu_global_mutex);
638 for (env = first_cpu; env != NULL; env = env->next_cpu)
639 env->created = 1;
640 qemu_cond_signal(&qemu_cpu_cond);
642 /* and wait for machine initialization */
643 while (!qemu_system_ready)
644 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
646 while (1) {
647 cpu_exec_all();
648 qemu_tcg_wait_io_event();
651 return NULL;
654 void qemu_cpu_kick(void *_env)
656 CPUState *env = _env;
657 qemu_cond_broadcast(env->halt_cond);
658 if (!env->thread_kicked) {
659 qemu_thread_signal(env->thread, SIG_IPI);
660 env->thread_kicked = true;
664 int qemu_cpu_self(void *_env)
666 CPUState *env = _env;
667 QemuThread this;
669 qemu_thread_self(&this);
671 return qemu_thread_equal(&this, env->thread);
674 static void cpu_signal(int sig)
676 if (cpu_single_env)
677 cpu_exit(cpu_single_env);
678 exit_request = 1;
681 static void tcg_init_ipi(void)
683 sigset_t set;
684 struct sigaction sigact;
686 memset(&sigact, 0, sizeof(sigact));
687 sigact.sa_handler = cpu_signal;
688 sigaction(SIG_IPI, &sigact, NULL);
690 sigemptyset(&set);
691 sigaddset(&set, SIG_IPI);
692 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
695 static void dummy_signal(int sig)
699 static void kvm_init_ipi(CPUState *env)
701 int r;
702 sigset_t set;
703 struct sigaction sigact;
705 memset(&sigact, 0, sizeof(sigact));
706 sigact.sa_handler = dummy_signal;
707 sigaction(SIG_IPI, &sigact, NULL);
709 pthread_sigmask(SIG_BLOCK, NULL, &set);
710 sigdelset(&set, SIG_IPI);
711 sigdelset(&set, SIGBUS);
712 r = kvm_set_signal_mask(env, &set);
713 if (r) {
714 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
715 exit(1);
719 static sigset_t block_io_signals(void)
721 sigset_t set;
722 struct sigaction action;
724 /* SIGUSR2 used by posix-aio-compat.c */
725 sigemptyset(&set);
726 sigaddset(&set, SIGUSR2);
727 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
729 sigemptyset(&set);
730 sigaddset(&set, SIGIO);
731 sigaddset(&set, SIGALRM);
732 sigaddset(&set, SIG_IPI);
733 sigaddset(&set, SIGBUS);
734 pthread_sigmask(SIG_BLOCK, &set, NULL);
736 memset(&action, 0, sizeof(action));
737 action.sa_flags = SA_SIGINFO;
738 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
739 sigaction(SIGBUS, &action, NULL);
740 prctl(PR_MCE_KILL, 1, 1, 0, 0);
742 return set;
745 void qemu_mutex_lock_iothread(void)
747 if (kvm_enabled()) {
748 qemu_mutex_lock(&qemu_global_mutex);
749 } else {
750 qemu_mutex_lock(&qemu_fair_mutex);
751 if (qemu_mutex_trylock(&qemu_global_mutex)) {
752 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
753 qemu_mutex_lock(&qemu_global_mutex);
755 qemu_mutex_unlock(&qemu_fair_mutex);
759 void qemu_mutex_unlock_iothread(void)
761 qemu_mutex_unlock(&qemu_global_mutex);
764 static int all_vcpus_paused(void)
766 CPUState *penv = first_cpu;
768 while (penv) {
769 if (!penv->stopped)
770 return 0;
771 penv = (CPUState *)penv->next_cpu;
774 return 1;
777 void pause_all_vcpus(void)
779 CPUState *penv = first_cpu;
781 while (penv) {
782 penv->stop = 1;
783 qemu_cpu_kick(penv);
784 penv = (CPUState *)penv->next_cpu;
787 while (!all_vcpus_paused()) {
788 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
789 penv = first_cpu;
790 while (penv) {
791 qemu_cpu_kick(penv);
792 penv = (CPUState *)penv->next_cpu;
797 void resume_all_vcpus(void)
799 CPUState *penv = first_cpu;
801 while (penv) {
802 penv->stop = 0;
803 penv->stopped = 0;
804 qemu_cpu_kick(penv);
805 penv = (CPUState *)penv->next_cpu;
809 static void tcg_init_vcpu(void *_env)
811 CPUState *env = _env;
812 /* share a single thread for all cpus with TCG */
813 if (!tcg_cpu_thread) {
814 env->thread = qemu_mallocz(sizeof(QemuThread));
815 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
816 qemu_cond_init(env->halt_cond);
817 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
818 while (env->created == 0)
819 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
820 tcg_cpu_thread = env->thread;
821 tcg_halt_cond = env->halt_cond;
822 } else {
823 env->thread = tcg_cpu_thread;
824 env->halt_cond = tcg_halt_cond;
828 static void kvm_start_vcpu(CPUState *env)
830 env->thread = qemu_mallocz(sizeof(QemuThread));
831 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
832 qemu_cond_init(env->halt_cond);
833 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
834 while (env->created == 0)
835 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
838 void qemu_init_vcpu(void *_env)
840 CPUState *env = _env;
842 env->nr_cores = smp_cores;
843 env->nr_threads = smp_threads;
844 if (kvm_enabled())
845 kvm_start_vcpu(env);
846 else
847 tcg_init_vcpu(env);
850 void qemu_notify_event(void)
852 qemu_event_increment();
855 static void qemu_system_vmstop_request(int reason)
857 vmstop_requested = reason;
858 qemu_notify_event();
861 void cpu_stop_current(void)
863 if (cpu_single_env) {
864 cpu_single_env->stopped = 1;
865 cpu_exit(cpu_single_env);
869 void vm_stop(int reason)
871 QemuThread me;
872 qemu_thread_self(&me);
874 if (!qemu_thread_equal(&me, &io_thread)) {
875 qemu_system_vmstop_request(reason);
877 * FIXME: should not return to device code in case
878 * vm_stop() has been requested.
880 cpu_stop_current();
881 return;
883 do_vm_stop(reason);
886 #endif
888 static int qemu_cpu_exec(CPUState *env)
890 int ret;
891 #ifdef CONFIG_PROFILER
892 int64_t ti;
893 #endif
895 #ifdef CONFIG_PROFILER
896 ti = profile_getclock();
897 #endif
898 if (use_icount) {
899 int64_t count;
900 int decr;
901 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
902 env->icount_decr.u16.low = 0;
903 env->icount_extra = 0;
904 count = qemu_icount_round (qemu_next_deadline());
905 qemu_icount += count;
906 decr = (count > 0xffff) ? 0xffff : count;
907 count -= decr;
908 env->icount_decr.u16.low = decr;
909 env->icount_extra = count;
911 ret = cpu_exec(env);
912 #ifdef CONFIG_PROFILER
913 qemu_time += profile_getclock() - ti;
914 #endif
915 if (use_icount) {
916 /* Fold pending instructions back into the
917 instruction counter, and clear the interrupt flag. */
918 qemu_icount -= (env->icount_decr.u16.low
919 + env->icount_extra);
920 env->icount_decr.u32 = 0;
921 env->icount_extra = 0;
923 return ret;
926 bool cpu_exec_all(void)
928 if (next_cpu == NULL)
929 next_cpu = first_cpu;
930 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
931 CPUState *env = next_cpu;
933 qemu_clock_enable(vm_clock,
934 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
936 if (qemu_alarm_pending())
937 break;
938 if (cpu_can_run(env)) {
939 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
940 break;
942 } else if (env->stop) {
943 break;
946 exit_request = 0;
947 return any_cpu_has_work();
950 void set_numa_modes(void)
952 CPUState *env;
953 int i;
955 for (env = first_cpu; env != NULL; env = env->next_cpu) {
956 for (i = 0; i < nb_numa_nodes; i++) {
957 if (node_cpumask[i] & (1 << env->cpu_index)) {
958 env->numa_node = i;
964 void set_cpu_log(const char *optarg)
966 int mask;
967 const CPULogItem *item;
969 mask = cpu_str_to_log_mask(optarg);
970 if (!mask) {
971 printf("Log items (comma separated):\n");
972 for (item = cpu_log_items; item->mask != 0; item++) {
973 printf("%-10s %s\n", item->name, item->help);
975 exit(1);
977 cpu_set_log(mask);
980 /* Return the virtual CPU time, based on the instruction counter. */
981 int64_t cpu_get_icount(void)
983 int64_t icount;
984 CPUState *env = cpu_single_env;;
986 icount = qemu_icount;
987 if (env) {
988 if (!can_do_io(env)) {
989 fprintf(stderr, "Bad clock read\n");
991 icount -= (env->icount_decr.u16.low + env->icount_extra);
993 return qemu_icount_bias + (icount << icount_time_shift);
996 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
998 /* XXX: implement xxx_cpu_list for targets that still miss it */
999 #if defined(cpu_list_id)
1000 cpu_list_id(f, cpu_fprintf, optarg);
1001 #elif defined(cpu_list)
1002 cpu_list(f, cpu_fprintf); /* deprecated */
1003 #endif