test: Add rmap_chain.flat to unittests config file
[qemu/qemu-dev-zwu.git] / cpus.c
blob2e40814c685aee1d8e493e7653ef12d6087d978c
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"
37 #ifdef SIGRTMIN
38 #define SIG_IPI (SIGRTMIN+4)
39 #else
40 #define SIG_IPI SIGUSR1
41 #endif
43 static CPUState *cur_cpu;
44 static CPUState *next_cpu;
46 /***********************************************************/
47 void hw_error(const char *fmt, ...)
49 va_list ap;
50 CPUState *env;
52 va_start(ap, fmt);
53 fprintf(stderr, "qemu: hardware error: ");
54 vfprintf(stderr, fmt, ap);
55 fprintf(stderr, "\n");
56 for(env = first_cpu; env != NULL; env = env->next_cpu) {
57 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
58 #ifdef TARGET_I386
59 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
60 #else
61 cpu_dump_state(env, stderr, fprintf, 0);
62 #endif
64 va_end(ap);
65 abort();
68 void cpu_synchronize_all_states(void)
70 CPUState *cpu;
72 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
73 cpu_synchronize_state(cpu);
77 void cpu_synchronize_all_post_reset(void)
79 CPUState *cpu;
81 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
82 cpu_synchronize_post_reset(cpu);
86 void cpu_synchronize_all_post_init(void)
88 CPUState *cpu;
90 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
91 cpu_synchronize_post_init(cpu);
95 int cpu_is_stopped(CPUState *env)
97 return !vm_running || env->stopped;
100 static void do_vm_stop(int reason)
102 if (vm_running) {
103 cpu_disable_ticks();
104 vm_running = 0;
105 pause_all_vcpus();
106 vm_state_notify(0, reason);
107 monitor_protocol_event(QEVENT_STOP, NULL);
111 static int cpu_can_run(CPUState *env)
113 if (env->stop)
114 return 0;
115 if (env->stopped || !vm_running)
116 return 0;
117 return 1;
120 static int cpu_has_work(CPUState *env)
122 if (env->stop)
123 return 1;
124 if (env->queued_work_first)
125 return 1;
126 if (env->stopped || !vm_running)
127 return 0;
128 if (!env->halted)
129 return 1;
130 if (qemu_cpu_has_work(env))
131 return 1;
132 return 0;
135 static int tcg_has_work(void)
137 CPUState *env;
139 for (env = first_cpu; env != NULL; env = env->next_cpu)
140 if (cpu_has_work(env))
141 return 1;
142 return 0;
145 #ifndef _WIN32
146 static int io_thread_fd = -1;
148 static void qemu_event_increment(void)
150 /* Write 8 bytes to be compatible with eventfd. */
151 static const uint64_t val = 1;
152 ssize_t ret;
154 if (io_thread_fd == -1)
155 return;
157 do {
158 ret = write(io_thread_fd, &val, sizeof(val));
159 } while (ret < 0 && errno == EINTR);
161 /* EAGAIN is fine, a read must be pending. */
162 if (ret < 0 && errno != EAGAIN) {
163 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
164 strerror(errno));
165 exit (1);
169 static void qemu_event_read(void *opaque)
171 int fd = (unsigned long)opaque;
172 ssize_t len;
173 char buffer[512];
175 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
176 do {
177 len = read(fd, buffer, sizeof(buffer));
178 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
181 static int qemu_event_init(void)
183 int err;
184 int fds[2];
186 err = qemu_eventfd(fds);
187 if (err == -1)
188 return -errno;
190 err = fcntl_setfl(fds[0], O_NONBLOCK);
191 if (err < 0)
192 goto fail;
194 err = fcntl_setfl(fds[1], O_NONBLOCK);
195 if (err < 0)
196 goto fail;
198 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
199 (void *)(unsigned long)fds[0]);
201 io_thread_fd = fds[1];
202 return 0;
204 fail:
205 close(fds[0]);
206 close(fds[1]);
207 return err;
209 #else
210 HANDLE qemu_event_handle;
212 static void dummy_event_handler(void *opaque)
216 static int qemu_event_init(void)
218 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
219 if (!qemu_event_handle) {
220 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
221 return -1;
223 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
224 return 0;
227 static void qemu_event_increment(void)
229 if (!SetEvent(qemu_event_handle)) {
230 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
231 GetLastError());
232 exit (1);
235 #endif
237 #ifndef CONFIG_IOTHREAD
238 int qemu_init_main_loop(void)
240 return qemu_event_init();
243 void qemu_main_loop_start(void)
247 void qemu_init_vcpu(void *_env)
249 CPUState *env = _env;
251 env->nr_cores = smp_cores;
252 env->nr_threads = smp_threads;
253 if (kvm_enabled())
254 kvm_init_vcpu(env);
255 return;
258 int qemu_cpu_self(void *env)
260 return 1;
263 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
265 func(data);
268 void resume_all_vcpus(void)
272 void pause_all_vcpus(void)
276 void qemu_cpu_kick(void *env)
278 return;
281 void qemu_notify_event(void)
283 CPUState *env = cpu_single_env;
285 if (kvm_enabled()) {
286 qemu_kvm_notify_work();
287 return;
290 qemu_event_increment ();
291 if (env) {
292 cpu_exit(env);
294 if (next_cpu && env != next_cpu) {
295 cpu_exit(next_cpu);
299 #if defined(KVM_UPSTREAM) || !defined(CONFIG_KVM)
300 void qemu_mutex_lock_iothread(void) {}
301 void qemu_mutex_unlock_iothread(void) {}
302 #endif
304 void vm_stop(int reason)
306 do_vm_stop(reason);
309 #else /* CONFIG_IOTHREAD */
311 #include "qemu-thread.h"
313 QemuMutex qemu_global_mutex;
314 static QemuMutex qemu_fair_mutex;
316 static QemuThread io_thread;
318 static QemuThread *tcg_cpu_thread;
319 static QemuCond *tcg_halt_cond;
321 static int qemu_system_ready;
322 /* cpu creation */
323 static QemuCond qemu_cpu_cond;
324 /* system init */
325 static QemuCond qemu_system_cond;
326 static QemuCond qemu_pause_cond;
327 static QemuCond qemu_work_cond;
329 static void tcg_init_ipi(void);
330 static void kvm_init_ipi(CPUState *env);
331 static void unblock_io_signals(void);
333 int qemu_init_main_loop(void)
335 int ret;
337 ret = qemu_event_init();
338 if (ret)
339 return ret;
341 qemu_cond_init(&qemu_pause_cond);
342 qemu_mutex_init(&qemu_fair_mutex);
343 qemu_mutex_init(&qemu_global_mutex);
344 qemu_mutex_lock(&qemu_global_mutex);
346 unblock_io_signals();
347 qemu_thread_self(&io_thread);
349 return 0;
352 void qemu_main_loop_start(void)
354 qemu_system_ready = 1;
355 qemu_cond_broadcast(&qemu_system_cond);
358 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
360 struct qemu_work_item wi;
362 if (qemu_cpu_self(env)) {
363 func(data);
364 return;
367 wi.func = func;
368 wi.data = data;
369 if (!env->queued_work_first)
370 env->queued_work_first = &wi;
371 else
372 env->queued_work_last->next = &wi;
373 env->queued_work_last = &wi;
374 wi.next = NULL;
375 wi.done = false;
377 qemu_cpu_kick(env);
378 while (!wi.done) {
379 CPUState *self_env = cpu_single_env;
381 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
382 cpu_single_env = self_env;
386 static void flush_queued_work(CPUState *env)
388 struct qemu_work_item *wi;
390 if (!env->queued_work_first)
391 return;
393 while ((wi = env->queued_work_first)) {
394 env->queued_work_first = wi->next;
395 wi->func(wi->data);
396 wi->done = true;
398 env->queued_work_last = NULL;
399 qemu_cond_broadcast(&qemu_work_cond);
402 static void qemu_wait_io_event_common(CPUState *env)
404 if (env->stop) {
405 env->stop = 0;
406 env->stopped = 1;
407 qemu_cond_signal(&qemu_pause_cond);
409 flush_queued_work(env);
412 static void qemu_wait_io_event(CPUState *env)
414 while (!tcg_has_work())
415 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
417 qemu_mutex_unlock(&qemu_global_mutex);
420 * Users of qemu_global_mutex can be starved, having no chance
421 * to acquire it since this path will get to it first.
422 * So use another lock to provide fairness.
424 qemu_mutex_lock(&qemu_fair_mutex);
425 qemu_mutex_unlock(&qemu_fair_mutex);
427 qemu_mutex_lock(&qemu_global_mutex);
428 qemu_wait_io_event_common(env);
431 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
433 struct timespec ts;
434 int r, e;
435 siginfo_t siginfo;
436 sigset_t waitset;
438 ts.tv_sec = timeout / 1000;
439 ts.tv_nsec = (timeout % 1000) * 1000000;
441 sigemptyset(&waitset);
442 sigaddset(&waitset, SIG_IPI);
444 qemu_mutex_unlock(&qemu_global_mutex);
445 r = sigtimedwait(&waitset, &siginfo, &ts);
446 e = errno;
447 qemu_mutex_lock(&qemu_global_mutex);
449 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
450 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
451 exit(1);
455 static void qemu_kvm_wait_io_event(CPUState *env)
457 while (!cpu_has_work(env))
458 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
460 qemu_kvm_eat_signal(env, 0);
461 qemu_wait_io_event_common(env);
464 static int qemu_cpu_exec(CPUState *env);
466 static void *kvm_cpu_thread_fn(void *arg)
468 CPUState *env = arg;
470 qemu_mutex_lock(&qemu_global_mutex);
471 qemu_thread_self(env->thread);
472 if (kvm_enabled())
473 kvm_init_vcpu(env);
475 kvm_init_ipi(env);
477 /* signal CPU creation */
478 env->created = 1;
479 qemu_cond_signal(&qemu_cpu_cond);
481 /* and wait for machine initialization */
482 while (!qemu_system_ready)
483 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
485 while (1) {
486 if (cpu_can_run(env))
487 qemu_cpu_exec(env);
488 qemu_kvm_wait_io_event(env);
491 return NULL;
494 static void *tcg_cpu_thread_fn(void *arg)
496 CPUState *env = arg;
498 tcg_init_ipi();
499 qemu_thread_self(env->thread);
501 /* signal CPU creation */
502 qemu_mutex_lock(&qemu_global_mutex);
503 for (env = first_cpu; env != NULL; env = env->next_cpu)
504 env->created = 1;
505 qemu_cond_signal(&qemu_cpu_cond);
507 /* and wait for machine initialization */
508 while (!qemu_system_ready)
509 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
511 while (1) {
512 tcg_cpu_exec();
513 qemu_wait_io_event(cur_cpu);
516 return NULL;
519 void qemu_cpu_kick(void *_env)
521 CPUState *env = _env;
522 qemu_cond_broadcast(env->halt_cond);
523 qemu_thread_signal(env->thread, SIG_IPI);
526 int qemu_cpu_self(void *_env)
528 CPUState *env = _env;
529 QemuThread this;
531 qemu_thread_self(&this);
533 return qemu_thread_equal(&this, env->thread);
536 static void cpu_signal(int sig)
538 if (cpu_single_env)
539 cpu_exit(cpu_single_env);
540 exit_request = 1;
543 static void tcg_init_ipi(void)
545 sigset_t set;
546 struct sigaction sigact;
548 memset(&sigact, 0, sizeof(sigact));
549 sigact.sa_handler = cpu_signal;
550 sigaction(SIG_IPI, &sigact, NULL);
552 sigemptyset(&set);
553 sigaddset(&set, SIG_IPI);
554 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
557 static void dummy_signal(int sig)
561 static void kvm_init_ipi(CPUState *env)
563 int r;
564 sigset_t set;
565 struct sigaction sigact;
567 memset(&sigact, 0, sizeof(sigact));
568 sigact.sa_handler = dummy_signal;
569 sigaction(SIG_IPI, &sigact, NULL);
571 pthread_sigmask(SIG_BLOCK, NULL, &set);
572 sigdelset(&set, SIG_IPI);
573 r = kvm_set_signal_mask(env, &set);
574 if (r) {
575 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
576 exit(1);
580 static void unblock_io_signals(void)
582 sigset_t set;
584 sigemptyset(&set);
585 sigaddset(&set, SIGUSR2);
586 sigaddset(&set, SIGIO);
587 sigaddset(&set, SIGALRM);
588 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
590 sigemptyset(&set);
591 sigaddset(&set, SIG_IPI);
592 pthread_sigmask(SIG_BLOCK, &set, NULL);
595 void qemu_mutex_lock_iothread(void)
597 if (kvm_enabled()) {
598 qemu_mutex_lock(&qemu_fair_mutex);
599 qemu_mutex_lock(&qemu_global_mutex);
600 qemu_mutex_unlock(&qemu_fair_mutex);
601 } else {
602 qemu_mutex_lock(&qemu_fair_mutex);
603 if (qemu_mutex_trylock(&qemu_global_mutex)) {
604 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
605 qemu_mutex_lock(&qemu_global_mutex);
607 qemu_mutex_unlock(&qemu_fair_mutex);
611 void qemu_mutex_unlock_iothread(void)
613 qemu_mutex_unlock(&qemu_global_mutex);
616 static int all_vcpus_paused(void)
618 CPUState *penv = first_cpu;
620 while (penv) {
621 if (!penv->stopped)
622 return 0;
623 penv = (CPUState *)penv->next_cpu;
626 return 1;
629 void pause_all_vcpus(void)
631 CPUState *penv = first_cpu;
633 while (penv) {
634 penv->stop = 1;
635 qemu_cpu_kick(penv);
636 penv = (CPUState *)penv->next_cpu;
639 while (!all_vcpus_paused()) {
640 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
641 penv = first_cpu;
642 while (penv) {
643 qemu_cpu_kick(penv);
644 penv = (CPUState *)penv->next_cpu;
649 void resume_all_vcpus(void)
651 CPUState *penv = first_cpu;
653 while (penv) {
654 penv->stop = 0;
655 penv->stopped = 0;
656 qemu_cpu_kick(penv);
657 penv = (CPUState *)penv->next_cpu;
661 static void tcg_init_vcpu(void *_env)
663 CPUState *env = _env;
664 /* share a single thread for all cpus with TCG */
665 if (!tcg_cpu_thread) {
666 env->thread = qemu_mallocz(sizeof(QemuThread));
667 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
668 qemu_cond_init(env->halt_cond);
669 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
670 while (env->created == 0)
671 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
672 tcg_cpu_thread = env->thread;
673 tcg_halt_cond = env->halt_cond;
674 } else {
675 env->thread = tcg_cpu_thread;
676 env->halt_cond = tcg_halt_cond;
680 static void kvm_start_vcpu(CPUState *env)
682 env->thread = qemu_mallocz(sizeof(QemuThread));
683 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
684 qemu_cond_init(env->halt_cond);
685 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
686 while (env->created == 0)
687 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
690 void qemu_init_vcpu(void *_env)
692 CPUState *env = _env;
694 env->nr_cores = smp_cores;
695 env->nr_threads = smp_threads;
696 if (kvm_enabled())
697 kvm_start_vcpu(env);
698 else
699 tcg_init_vcpu(env);
702 void qemu_notify_event(void)
704 qemu_event_increment();
707 static void qemu_system_vmstop_request(int reason)
709 vmstop_requested = reason;
710 qemu_notify_event();
713 void vm_stop(int reason)
715 QemuThread me;
716 qemu_thread_self(&me);
718 if (!qemu_thread_equal(&me, &io_thread)) {
719 qemu_system_vmstop_request(reason);
721 * FIXME: should not return to device code in case
722 * vm_stop() has been requested.
724 if (cpu_single_env) {
725 cpu_exit(cpu_single_env);
726 cpu_single_env->stop = 1;
728 return;
730 do_vm_stop(reason);
733 #endif
735 static int qemu_cpu_exec(CPUState *env)
737 int ret;
738 #ifdef CONFIG_PROFILER
739 int64_t ti;
740 #endif
742 #ifdef CONFIG_PROFILER
743 ti = profile_getclock();
744 #endif
745 if (use_icount) {
746 int64_t count;
747 int decr;
748 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
749 env->icount_decr.u16.low = 0;
750 env->icount_extra = 0;
751 count = qemu_icount_round (qemu_next_deadline());
752 qemu_icount += count;
753 decr = (count > 0xffff) ? 0xffff : count;
754 count -= decr;
755 env->icount_decr.u16.low = decr;
756 env->icount_extra = count;
758 ret = cpu_exec(env);
759 #ifdef CONFIG_PROFILER
760 qemu_time += profile_getclock() - ti;
761 #endif
762 if (use_icount) {
763 /* Fold pending instructions back into the
764 instruction counter, and clear the interrupt flag. */
765 qemu_icount -= (env->icount_decr.u16.low
766 + env->icount_extra);
767 env->icount_decr.u32 = 0;
768 env->icount_extra = 0;
770 return ret;
773 bool tcg_cpu_exec(void)
775 int ret = 0;
777 if (next_cpu == NULL)
778 next_cpu = first_cpu;
779 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
780 CPUState *env = cur_cpu = next_cpu;
782 qemu_clock_enable(vm_clock,
783 (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
785 if (qemu_alarm_pending())
786 break;
787 if (cpu_can_run(env))
788 ret = qemu_cpu_exec(env);
789 else if (env->stop)
790 break;
792 if (ret == EXCP_DEBUG) {
793 gdb_set_stop_cpu(env);
794 debug_requested = EXCP_DEBUG;
795 break;
798 return tcg_has_work();
801 void set_numa_modes(void)
803 CPUState *env;
804 int i;
806 for (env = first_cpu; env != NULL; env = env->next_cpu) {
807 for (i = 0; i < nb_numa_nodes; i++) {
808 if (node_cpumask[i] & (1 << env->cpu_index)) {
809 env->numa_node = i;
815 void set_cpu_log(const char *optarg)
817 int mask;
818 const CPULogItem *item;
820 mask = cpu_str_to_log_mask(optarg);
821 if (!mask) {
822 printf("Log items (comma separated):\n");
823 for (item = cpu_log_items; item->mask != 0; item++) {
824 printf("%-10s %s\n", item->name, item->help);
826 exit(1);
828 cpu_set_log(mask);
831 /* Return the virtual CPU time, based on the instruction counter. */
832 int64_t cpu_get_icount(void)
834 int64_t icount;
835 CPUState *env = cpu_single_env;;
837 icount = qemu_icount;
838 if (env) {
839 if (!can_do_io(env)) {
840 fprintf(stderr, "Bad clock read\n");
842 icount -= (env->icount_decr.u16.low + env->icount_extra);
844 return qemu_icount_bias + (icount << icount_time_shift);
847 void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
848 const char *optarg)
850 /* XXX: implement xxx_cpu_list for targets that still miss it */
851 #if defined(cpu_list_id)
852 cpu_list_id(f, cpu_fprintf, optarg);
853 #elif defined(cpu_list)
854 cpu_list(f, cpu_fprintf); /* deprecated */
855 #endif