hda-audio: Zap tabs
[qemu/mdroth.git] / cpus.c
blob91a0fb1146aa2c9cfbc0a09066cb7b3a39d7e7cb
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 monitor_protocol_event(QEVENT_STOP, NULL);
118 static int cpu_can_run(CPUState *env)
120 if (env->stop)
121 return 0;
122 if (env->stopped || !vm_running)
123 return 0;
124 return 1;
127 static int cpu_has_work(CPUState *env)
129 if (env->stop)
130 return 1;
131 if (env->queued_work_first)
132 return 1;
133 if (env->stopped || !vm_running)
134 return 0;
135 if (!env->halted)
136 return 1;
137 if (qemu_cpu_has_work(env))
138 return 1;
139 return 0;
142 static int any_cpu_has_work(void)
144 CPUState *env;
146 for (env = first_cpu; env != NULL; env = env->next_cpu)
147 if (cpu_has_work(env))
148 return 1;
149 return 0;
152 static void cpu_debug_handler(CPUState *env)
154 gdb_set_stop_cpu(env);
155 debug_requested = EXCP_DEBUG;
156 vm_stop(EXCP_DEBUG);
159 #ifndef _WIN32
160 static int io_thread_fd = -1;
162 static void qemu_event_increment(void)
164 /* Write 8 bytes to be compatible with eventfd. */
165 static const uint64_t val = 1;
166 ssize_t ret;
168 if (io_thread_fd == -1)
169 return;
171 do {
172 ret = write(io_thread_fd, &val, sizeof(val));
173 } while (ret < 0 && errno == EINTR);
175 /* EAGAIN is fine, a read must be pending. */
176 if (ret < 0 && errno != EAGAIN) {
177 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
178 strerror(errno));
179 exit (1);
183 static void qemu_event_read(void *opaque)
185 int fd = (unsigned long)opaque;
186 ssize_t len;
187 char buffer[512];
189 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
190 do {
191 len = read(fd, buffer, sizeof(buffer));
192 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
195 static int qemu_event_init(void)
197 int err;
198 int fds[2];
200 err = qemu_eventfd(fds);
201 if (err == -1)
202 return -errno;
204 err = fcntl_setfl(fds[0], O_NONBLOCK);
205 if (err < 0)
206 goto fail;
208 err = fcntl_setfl(fds[1], O_NONBLOCK);
209 if (err < 0)
210 goto fail;
212 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
213 (void *)(unsigned long)fds[0]);
215 io_thread_fd = fds[1];
216 return 0;
218 fail:
219 close(fds[0]);
220 close(fds[1]);
221 return err;
223 #else
224 HANDLE qemu_event_handle;
226 static void dummy_event_handler(void *opaque)
230 static int qemu_event_init(void)
232 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
233 if (!qemu_event_handle) {
234 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
235 return -1;
237 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
238 return 0;
241 static void qemu_event_increment(void)
243 if (!SetEvent(qemu_event_handle)) {
244 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
245 GetLastError());
246 exit (1);
249 #endif
251 #ifndef CONFIG_IOTHREAD
252 int qemu_init_main_loop(void)
254 cpu_set_debug_excp_handler(cpu_debug_handler);
256 return qemu_event_init();
259 void qemu_main_loop_start(void)
263 void qemu_init_vcpu(void *_env)
265 CPUState *env = _env;
267 env->nr_cores = smp_cores;
268 env->nr_threads = smp_threads;
269 if (kvm_enabled())
270 kvm_init_vcpu(env);
271 return;
274 int qemu_cpu_self(void *env)
276 return 1;
279 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
281 func(data);
284 void resume_all_vcpus(void)
288 void pause_all_vcpus(void)
292 void qemu_cpu_kick(void *env)
294 return;
297 void qemu_notify_event(void)
299 CPUState *env = cpu_single_env;
301 qemu_event_increment ();
302 if (env) {
303 cpu_exit(env);
305 if (next_cpu && env != next_cpu) {
306 cpu_exit(next_cpu);
310 void qemu_mutex_lock_iothread(void) {}
311 void qemu_mutex_unlock_iothread(void) {}
313 void vm_stop(int reason)
315 do_vm_stop(reason);
318 #else /* CONFIG_IOTHREAD */
320 #include "qemu-thread.h"
322 QemuMutex qemu_global_mutex;
323 static QemuMutex qemu_fair_mutex;
325 static QemuThread io_thread;
327 static QemuThread *tcg_cpu_thread;
328 static QemuCond *tcg_halt_cond;
330 static int qemu_system_ready;
331 /* cpu creation */
332 static QemuCond qemu_cpu_cond;
333 /* system init */
334 static QemuCond qemu_system_cond;
335 static QemuCond qemu_pause_cond;
336 static QemuCond qemu_work_cond;
338 static void tcg_init_ipi(void);
339 static void kvm_init_ipi(CPUState *env);
340 static sigset_t block_io_signals(void);
342 /* If we have signalfd, we mask out the signals we want to handle and then
343 * use signalfd to listen for them. We rely on whatever the current signal
344 * handler is to dispatch the signals when we receive them.
346 static void sigfd_handler(void *opaque)
348 int fd = (unsigned long) opaque;
349 struct qemu_signalfd_siginfo info;
350 struct sigaction action;
351 ssize_t len;
353 while (1) {
354 do {
355 len = read(fd, &info, sizeof(info));
356 } while (len == -1 && errno == EINTR);
358 if (len == -1 && errno == EAGAIN) {
359 break;
362 if (len != sizeof(info)) {
363 printf("read from sigfd returned %zd: %m\n", len);
364 return;
367 sigaction(info.ssi_signo, NULL, &action);
368 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
369 action.sa_sigaction(info.ssi_signo,
370 (siginfo_t *)&info, NULL);
371 } else if (action.sa_handler) {
372 action.sa_handler(info.ssi_signo);
377 static int qemu_signalfd_init(sigset_t mask)
379 int sigfd;
381 sigfd = qemu_signalfd(&mask);
382 if (sigfd == -1) {
383 fprintf(stderr, "failed to create signalfd\n");
384 return -errno;
387 fcntl_setfl(sigfd, O_NONBLOCK);
389 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
390 (void *)(unsigned long) sigfd);
392 return 0;
395 int qemu_init_main_loop(void)
397 int ret;
398 sigset_t blocked_signals;
400 cpu_set_debug_excp_handler(cpu_debug_handler);
402 blocked_signals = block_io_signals();
404 ret = qemu_signalfd_init(blocked_signals);
405 if (ret)
406 return ret;
408 /* Note eventfd must be drained before signalfd handlers run */
409 ret = qemu_event_init();
410 if (ret)
411 return ret;
413 qemu_cond_init(&qemu_pause_cond);
414 qemu_cond_init(&qemu_system_cond);
415 qemu_mutex_init(&qemu_fair_mutex);
416 qemu_mutex_init(&qemu_global_mutex);
417 qemu_mutex_lock(&qemu_global_mutex);
419 qemu_thread_self(&io_thread);
421 return 0;
424 void qemu_main_loop_start(void)
426 qemu_system_ready = 1;
427 qemu_cond_broadcast(&qemu_system_cond);
430 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
432 struct qemu_work_item wi;
434 if (qemu_cpu_self(env)) {
435 func(data);
436 return;
439 wi.func = func;
440 wi.data = data;
441 if (!env->queued_work_first)
442 env->queued_work_first = &wi;
443 else
444 env->queued_work_last->next = &wi;
445 env->queued_work_last = &wi;
446 wi.next = NULL;
447 wi.done = false;
449 qemu_cpu_kick(env);
450 while (!wi.done) {
451 CPUState *self_env = cpu_single_env;
453 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
454 cpu_single_env = self_env;
458 static void flush_queued_work(CPUState *env)
460 struct qemu_work_item *wi;
462 if (!env->queued_work_first)
463 return;
465 while ((wi = env->queued_work_first)) {
466 env->queued_work_first = wi->next;
467 wi->func(wi->data);
468 wi->done = true;
470 env->queued_work_last = NULL;
471 qemu_cond_broadcast(&qemu_work_cond);
474 static void qemu_wait_io_event_common(CPUState *env)
476 if (env->stop) {
477 env->stop = 0;
478 env->stopped = 1;
479 qemu_cond_signal(&qemu_pause_cond);
481 flush_queued_work(env);
484 static void qemu_tcg_wait_io_event(void)
486 CPUState *env;
488 while (!any_cpu_has_work())
489 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
491 qemu_mutex_unlock(&qemu_global_mutex);
494 * Users of qemu_global_mutex can be starved, having no chance
495 * to acquire it since this path will get to it first.
496 * So use another lock to provide fairness.
498 qemu_mutex_lock(&qemu_fair_mutex);
499 qemu_mutex_unlock(&qemu_fair_mutex);
501 qemu_mutex_lock(&qemu_global_mutex);
503 for (env = first_cpu; env != NULL; env = env->next_cpu) {
504 qemu_wait_io_event_common(env);
508 static void sigbus_reraise(void)
510 sigset_t set;
511 struct sigaction action;
513 memset(&action, 0, sizeof(action));
514 action.sa_handler = SIG_DFL;
515 if (!sigaction(SIGBUS, &action, NULL)) {
516 raise(SIGBUS);
517 sigemptyset(&set);
518 sigaddset(&set, SIGBUS);
519 sigprocmask(SIG_UNBLOCK, &set, NULL);
521 perror("Failed to re-raise SIGBUS!\n");
522 abort();
525 static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
526 void *ctx)
528 #if defined(TARGET_I386)
529 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr))
530 #endif
531 sigbus_reraise();
534 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
536 struct timespec ts;
537 int r, e;
538 siginfo_t siginfo;
539 sigset_t waitset;
540 sigset_t chkset;
542 ts.tv_sec = timeout / 1000;
543 ts.tv_nsec = (timeout % 1000) * 1000000;
545 sigemptyset(&waitset);
546 sigaddset(&waitset, SIG_IPI);
547 sigaddset(&waitset, SIGBUS);
549 do {
550 qemu_mutex_unlock(&qemu_global_mutex);
552 r = sigtimedwait(&waitset, &siginfo, &ts);
553 e = errno;
555 qemu_mutex_lock(&qemu_global_mutex);
557 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
558 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
559 exit(1);
562 switch (r) {
563 case SIGBUS:
564 #ifdef TARGET_I386
565 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr))
566 #endif
567 sigbus_reraise();
568 break;
569 default:
570 break;
573 r = sigpending(&chkset);
574 if (r == -1) {
575 fprintf(stderr, "sigpending: %s\n", strerror(e));
576 exit(1);
578 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
581 static void qemu_kvm_wait_io_event(CPUState *env)
583 while (!cpu_has_work(env))
584 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
586 qemu_kvm_eat_signal(env, 0);
587 qemu_wait_io_event_common(env);
590 static int qemu_cpu_exec(CPUState *env);
592 static void *kvm_cpu_thread_fn(void *arg)
594 CPUState *env = arg;
596 qemu_mutex_lock(&qemu_global_mutex);
597 qemu_thread_self(env->thread);
598 if (kvm_enabled())
599 kvm_init_vcpu(env);
601 kvm_init_ipi(env);
603 /* signal CPU creation */
604 env->created = 1;
605 qemu_cond_signal(&qemu_cpu_cond);
607 /* and wait for machine initialization */
608 while (!qemu_system_ready)
609 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
611 while (1) {
612 if (cpu_can_run(env))
613 qemu_cpu_exec(env);
614 qemu_kvm_wait_io_event(env);
617 return NULL;
620 static void *tcg_cpu_thread_fn(void *arg)
622 CPUState *env = arg;
624 tcg_init_ipi();
625 qemu_thread_self(env->thread);
627 /* signal CPU creation */
628 qemu_mutex_lock(&qemu_global_mutex);
629 for (env = first_cpu; env != NULL; env = env->next_cpu)
630 env->created = 1;
631 qemu_cond_signal(&qemu_cpu_cond);
633 /* and wait for machine initialization */
634 while (!qemu_system_ready)
635 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
637 while (1) {
638 cpu_exec_all();
639 qemu_tcg_wait_io_event();
642 return NULL;
645 void qemu_cpu_kick(void *_env)
647 CPUState *env = _env;
648 qemu_cond_broadcast(env->halt_cond);
649 qemu_thread_signal(env->thread, SIG_IPI);
652 int qemu_cpu_self(void *_env)
654 CPUState *env = _env;
655 QemuThread this;
657 qemu_thread_self(&this);
659 return qemu_thread_equal(&this, env->thread);
662 static void cpu_signal(int sig)
664 if (cpu_single_env)
665 cpu_exit(cpu_single_env);
666 exit_request = 1;
669 static void tcg_init_ipi(void)
671 sigset_t set;
672 struct sigaction sigact;
674 memset(&sigact, 0, sizeof(sigact));
675 sigact.sa_handler = cpu_signal;
676 sigaction(SIG_IPI, &sigact, NULL);
678 sigemptyset(&set);
679 sigaddset(&set, SIG_IPI);
680 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
683 static void dummy_signal(int sig)
687 static void kvm_init_ipi(CPUState *env)
689 int r;
690 sigset_t set;
691 struct sigaction sigact;
693 memset(&sigact, 0, sizeof(sigact));
694 sigact.sa_handler = dummy_signal;
695 sigaction(SIG_IPI, &sigact, NULL);
697 pthread_sigmask(SIG_BLOCK, NULL, &set);
698 sigdelset(&set, SIG_IPI);
699 sigdelset(&set, SIGBUS);
700 r = kvm_set_signal_mask(env, &set);
701 if (r) {
702 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
703 exit(1);
707 static sigset_t block_io_signals(void)
709 sigset_t set;
710 struct sigaction action;
712 /* SIGUSR2 used by posix-aio-compat.c */
713 sigemptyset(&set);
714 sigaddset(&set, SIGUSR2);
715 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
717 sigemptyset(&set);
718 sigaddset(&set, SIGIO);
719 sigaddset(&set, SIGALRM);
720 sigaddset(&set, SIG_IPI);
721 sigaddset(&set, SIGBUS);
722 pthread_sigmask(SIG_BLOCK, &set, NULL);
724 memset(&action, 0, sizeof(action));
725 action.sa_flags = SA_SIGINFO;
726 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
727 sigaction(SIGBUS, &action, NULL);
728 prctl(PR_MCE_KILL, 1, 1, 0, 0);
730 return set;
733 void qemu_mutex_lock_iothread(void)
735 if (kvm_enabled()) {
736 qemu_mutex_lock(&qemu_fair_mutex);
737 qemu_mutex_lock(&qemu_global_mutex);
738 qemu_mutex_unlock(&qemu_fair_mutex);
739 } else {
740 qemu_mutex_lock(&qemu_fair_mutex);
741 if (qemu_mutex_trylock(&qemu_global_mutex)) {
742 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
743 qemu_mutex_lock(&qemu_global_mutex);
745 qemu_mutex_unlock(&qemu_fair_mutex);
749 void qemu_mutex_unlock_iothread(void)
751 qemu_mutex_unlock(&qemu_global_mutex);
754 static int all_vcpus_paused(void)
756 CPUState *penv = first_cpu;
758 while (penv) {
759 if (!penv->stopped)
760 return 0;
761 penv = (CPUState *)penv->next_cpu;
764 return 1;
767 void pause_all_vcpus(void)
769 CPUState *penv = first_cpu;
771 while (penv) {
772 penv->stop = 1;
773 qemu_cpu_kick(penv);
774 penv = (CPUState *)penv->next_cpu;
777 while (!all_vcpus_paused()) {
778 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
779 penv = first_cpu;
780 while (penv) {
781 qemu_cpu_kick(penv);
782 penv = (CPUState *)penv->next_cpu;
787 void resume_all_vcpus(void)
789 CPUState *penv = first_cpu;
791 while (penv) {
792 penv->stop = 0;
793 penv->stopped = 0;
794 qemu_cpu_kick(penv);
795 penv = (CPUState *)penv->next_cpu;
799 static void tcg_init_vcpu(void *_env)
801 CPUState *env = _env;
802 /* share a single thread for all cpus with TCG */
803 if (!tcg_cpu_thread) {
804 env->thread = qemu_mallocz(sizeof(QemuThread));
805 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
806 qemu_cond_init(env->halt_cond);
807 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
808 while (env->created == 0)
809 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
810 tcg_cpu_thread = env->thread;
811 tcg_halt_cond = env->halt_cond;
812 } else {
813 env->thread = tcg_cpu_thread;
814 env->halt_cond = tcg_halt_cond;
818 static void kvm_start_vcpu(CPUState *env)
820 env->thread = qemu_mallocz(sizeof(QemuThread));
821 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
822 qemu_cond_init(env->halt_cond);
823 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
824 while (env->created == 0)
825 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
828 void qemu_init_vcpu(void *_env)
830 CPUState *env = _env;
832 env->nr_cores = smp_cores;
833 env->nr_threads = smp_threads;
834 if (kvm_enabled())
835 kvm_start_vcpu(env);
836 else
837 tcg_init_vcpu(env);
840 void qemu_notify_event(void)
842 qemu_event_increment();
845 static void qemu_system_vmstop_request(int reason)
847 vmstop_requested = reason;
848 qemu_notify_event();
851 void vm_stop(int reason)
853 QemuThread me;
854 qemu_thread_self(&me);
856 if (!qemu_thread_equal(&me, &io_thread)) {
857 qemu_system_vmstop_request(reason);
859 * FIXME: should not return to device code in case
860 * vm_stop() has been requested.
862 if (cpu_single_env) {
863 cpu_exit(cpu_single_env);
864 cpu_single_env->stop = 1;
866 return;
868 do_vm_stop(reason);
871 #endif
873 static int qemu_cpu_exec(CPUState *env)
875 int ret;
876 #ifdef CONFIG_PROFILER
877 int64_t ti;
878 #endif
880 #ifdef CONFIG_PROFILER
881 ti = profile_getclock();
882 #endif
883 if (use_icount) {
884 int64_t count;
885 int decr;
886 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
887 env->icount_decr.u16.low = 0;
888 env->icount_extra = 0;
889 count = qemu_icount_round (qemu_next_deadline());
890 qemu_icount += count;
891 decr = (count > 0xffff) ? 0xffff : count;
892 count -= decr;
893 env->icount_decr.u16.low = decr;
894 env->icount_extra = count;
896 ret = cpu_exec(env);
897 #ifdef CONFIG_PROFILER
898 qemu_time += profile_getclock() - ti;
899 #endif
900 if (use_icount) {
901 /* Fold pending instructions back into the
902 instruction counter, and clear the interrupt flag. */
903 qemu_icount -= (env->icount_decr.u16.low
904 + env->icount_extra);
905 env->icount_decr.u32 = 0;
906 env->icount_extra = 0;
908 return ret;
911 bool cpu_exec_all(void)
913 if (next_cpu == NULL)
914 next_cpu = first_cpu;
915 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
916 CPUState *env = next_cpu;
918 qemu_clock_enable(vm_clock,
919 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
921 if (qemu_alarm_pending())
922 break;
923 if (cpu_can_run(env)) {
924 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
925 break;
927 } else if (env->stop) {
928 break;
931 exit_request = 0;
932 return any_cpu_has_work();
935 void set_numa_modes(void)
937 CPUState *env;
938 int i;
940 for (env = first_cpu; env != NULL; env = env->next_cpu) {
941 for (i = 0; i < nb_numa_nodes; i++) {
942 if (node_cpumask[i] & (1 << env->cpu_index)) {
943 env->numa_node = i;
949 void set_cpu_log(const char *optarg)
951 int mask;
952 const CPULogItem *item;
954 mask = cpu_str_to_log_mask(optarg);
955 if (!mask) {
956 printf("Log items (comma separated):\n");
957 for (item = cpu_log_items; item->mask != 0; item++) {
958 printf("%-10s %s\n", item->name, item->help);
960 exit(1);
962 cpu_set_log(mask);
965 /* Return the virtual CPU time, based on the instruction counter. */
966 int64_t cpu_get_icount(void)
968 int64_t icount;
969 CPUState *env = cpu_single_env;;
971 icount = qemu_icount;
972 if (env) {
973 if (!can_do_io(env)) {
974 fprintf(stderr, "Bad clock read\n");
976 icount -= (env->icount_decr.u16.low + env->icount_extra);
978 return qemu_icount_bias + (icount << icount_time_shift);
981 void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
983 /* XXX: implement xxx_cpu_list for targets that still miss it */
984 #if defined(cpu_list_id)
985 cpu_list_id(f, cpu_fprintf, optarg);
986 #elif defined(cpu_list)
987 cpu_list(f, cpu_fprintf); /* deprecated */
988 #endif