Merge commit 'cd4ec0b4d169faba8cc03a16b361700e32a83bd6' into upstream-merge
[qemu-kvm/stefanha.git] / cpus.c
blob99c04d1046b60b5b0612a89be37b752efb71eb0b
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 *next_cpu;
45 /***********************************************************/
46 void hw_error(const char *fmt, ...)
48 va_list ap;
49 CPUState *env;
51 va_start(ap, fmt);
52 fprintf(stderr, "qemu: hardware error: ");
53 vfprintf(stderr, fmt, ap);
54 fprintf(stderr, "\n");
55 for(env = first_cpu; env != NULL; env = env->next_cpu) {
56 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
57 #ifdef TARGET_I386
58 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
59 #else
60 cpu_dump_state(env, stderr, fprintf, 0);
61 #endif
63 va_end(ap);
64 abort();
67 void cpu_synchronize_all_states(void)
69 CPUState *cpu;
71 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
72 cpu_synchronize_state(cpu);
76 void cpu_synchronize_all_post_reset(void)
78 CPUState *cpu;
80 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
81 cpu_synchronize_post_reset(cpu);
85 void cpu_synchronize_all_post_init(void)
87 CPUState *cpu;
89 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
90 cpu_synchronize_post_init(cpu);
94 int cpu_is_stopped(CPUState *env)
96 return !vm_running || env->stopped;
99 static void do_vm_stop(int reason)
101 if (vm_running) {
102 cpu_disable_ticks();
103 vm_running = 0;
104 pause_all_vcpus();
105 vm_state_notify(0, reason);
106 monitor_protocol_event(QEVENT_STOP, NULL);
110 static int cpu_can_run(CPUState *env)
112 if (env->stop)
113 return 0;
114 if (env->stopped || !vm_running)
115 return 0;
116 return 1;
119 static int cpu_has_work(CPUState *env)
121 if (env->stop)
122 return 1;
123 if (env->queued_work_first)
124 return 1;
125 if (env->stopped || !vm_running)
126 return 0;
127 if (!env->halted)
128 return 1;
129 if (qemu_cpu_has_work(env))
130 return 1;
131 return 0;
134 static int any_cpu_has_work(void)
136 CPUState *env;
138 for (env = first_cpu; env != NULL; env = env->next_cpu)
139 if (cpu_has_work(env))
140 return 1;
141 return 0;
144 static void cpu_debug_handler(CPUState *env)
146 gdb_set_stop_cpu(env);
147 debug_requested = EXCP_DEBUG;
148 vm_stop(EXCP_DEBUG);
151 #ifndef _WIN32
152 static int io_thread_fd = -1;
154 static void qemu_event_increment(void)
156 /* Write 8 bytes to be compatible with eventfd. */
157 static const uint64_t val = 1;
158 ssize_t ret;
160 if (io_thread_fd == -1)
161 return;
163 do {
164 ret = write(io_thread_fd, &val, sizeof(val));
165 } while (ret < 0 && errno == EINTR);
167 /* EAGAIN is fine, a read must be pending. */
168 if (ret < 0 && errno != EAGAIN) {
169 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
170 strerror(errno));
171 exit (1);
175 static void qemu_event_read(void *opaque)
177 int fd = (unsigned long)opaque;
178 ssize_t len;
179 char buffer[512];
181 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
182 do {
183 len = read(fd, buffer, sizeof(buffer));
184 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
187 static int qemu_event_init(void)
189 int err;
190 int fds[2];
192 err = qemu_eventfd(fds);
193 if (err == -1)
194 return -errno;
196 err = fcntl_setfl(fds[0], O_NONBLOCK);
197 if (err < 0)
198 goto fail;
200 err = fcntl_setfl(fds[1], O_NONBLOCK);
201 if (err < 0)
202 goto fail;
204 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
205 (void *)(unsigned long)fds[0]);
207 io_thread_fd = fds[1];
208 return 0;
210 fail:
211 close(fds[0]);
212 close(fds[1]);
213 return err;
215 #else
216 HANDLE qemu_event_handle;
218 static void dummy_event_handler(void *opaque)
222 static int qemu_event_init(void)
224 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
225 if (!qemu_event_handle) {
226 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
227 return -1;
229 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
230 return 0;
233 static void qemu_event_increment(void)
235 if (!SetEvent(qemu_event_handle)) {
236 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
237 GetLastError());
238 exit (1);
241 #endif
243 #ifndef CONFIG_IOTHREAD
244 int qemu_init_main_loop(void)
246 cpu_set_debug_excp_handler(cpu_debug_handler);
248 return qemu_event_init();
251 void qemu_main_loop_start(void)
255 void qemu_init_vcpu(void *_env)
257 CPUState *env = _env;
259 env->nr_cores = smp_cores;
260 env->nr_threads = smp_threads;
261 if (kvm_enabled())
262 kvm_init_vcpu(env);
263 return;
266 int qemu_cpu_self(void *env)
268 return 1;
271 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
273 func(data);
276 void resume_all_vcpus(void)
280 void pause_all_vcpus(void)
284 void qemu_cpu_kick(void *env)
286 return;
289 void qemu_notify_event(void)
291 CPUState *env = cpu_single_env;
293 qemu_event_increment ();
294 if (env) {
295 cpu_exit(env);
297 if (next_cpu && env != next_cpu) {
298 cpu_exit(next_cpu);
302 #if defined(OBSOLETE_KVM_IMPL) || !defined(CONFIG_KVM)
303 void qemu_mutex_lock_iothread(void) {}
304 void qemu_mutex_unlock_iothread(void) {}
305 #endif
307 void vm_stop(int reason)
309 do_vm_stop(reason);
312 #else /* CONFIG_IOTHREAD */
314 #include "qemu-thread.h"
316 QemuMutex qemu_global_mutex;
317 static QemuMutex qemu_fair_mutex;
319 static QemuThread io_thread;
321 static QemuThread *tcg_cpu_thread;
322 static QemuCond *tcg_halt_cond;
324 static int qemu_system_ready;
325 /* cpu creation */
326 static QemuCond qemu_cpu_cond;
327 /* system init */
328 static QemuCond qemu_system_cond;
329 static QemuCond qemu_pause_cond;
330 static QemuCond qemu_work_cond;
332 static void tcg_init_ipi(void);
333 static void kvm_init_ipi(CPUState *env);
334 static void unblock_io_signals(void);
336 int qemu_init_main_loop(void)
338 int ret;
340 cpu_set_debug_excp_handler(cpu_debug_handler);
342 ret = qemu_event_init();
343 if (ret)
344 return ret;
346 qemu_cond_init(&qemu_pause_cond);
347 qemu_cond_init(&qemu_system_cond);
348 qemu_mutex_init(&qemu_fair_mutex);
349 qemu_mutex_init(&qemu_global_mutex);
350 qemu_mutex_lock(&qemu_global_mutex);
352 unblock_io_signals();
353 qemu_thread_self(&io_thread);
355 return 0;
358 void qemu_main_loop_start(void)
360 qemu_system_ready = 1;
361 qemu_cond_broadcast(&qemu_system_cond);
364 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
366 struct qemu_work_item wi;
368 if (qemu_cpu_self(env)) {
369 func(data);
370 return;
373 wi.func = func;
374 wi.data = data;
375 if (!env->queued_work_first)
376 env->queued_work_first = &wi;
377 else
378 env->queued_work_last->next = &wi;
379 env->queued_work_last = &wi;
380 wi.next = NULL;
381 wi.done = false;
383 qemu_cpu_kick(env);
384 while (!wi.done) {
385 CPUState *self_env = cpu_single_env;
387 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
388 cpu_single_env = self_env;
392 static void flush_queued_work(CPUState *env)
394 struct qemu_work_item *wi;
396 if (!env->queued_work_first)
397 return;
399 while ((wi = env->queued_work_first)) {
400 env->queued_work_first = wi->next;
401 wi->func(wi->data);
402 wi->done = true;
404 env->queued_work_last = NULL;
405 qemu_cond_broadcast(&qemu_work_cond);
408 static void qemu_wait_io_event_common(CPUState *env)
410 if (env->stop) {
411 env->stop = 0;
412 env->stopped = 1;
413 qemu_cond_signal(&qemu_pause_cond);
415 flush_queued_work(env);
418 static void qemu_tcg_wait_io_event(void)
420 CPUState *env;
422 while (!any_cpu_has_work())
423 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
425 qemu_mutex_unlock(&qemu_global_mutex);
428 * Users of qemu_global_mutex can be starved, having no chance
429 * to acquire it since this path will get to it first.
430 * So use another lock to provide fairness.
432 qemu_mutex_lock(&qemu_fair_mutex);
433 qemu_mutex_unlock(&qemu_fair_mutex);
435 qemu_mutex_lock(&qemu_global_mutex);
437 for (env = first_cpu; env != NULL; env = env->next_cpu) {
438 qemu_wait_io_event_common(env);
442 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
444 struct timespec ts;
445 int r, e;
446 siginfo_t siginfo;
447 sigset_t waitset;
449 ts.tv_sec = timeout / 1000;
450 ts.tv_nsec = (timeout % 1000) * 1000000;
452 sigemptyset(&waitset);
453 sigaddset(&waitset, SIG_IPI);
455 qemu_mutex_unlock(&qemu_global_mutex);
456 r = sigtimedwait(&waitset, &siginfo, &ts);
457 e = errno;
458 qemu_mutex_lock(&qemu_global_mutex);
460 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
461 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
462 exit(1);
466 static void qemu_kvm_wait_io_event(CPUState *env)
468 while (!cpu_has_work(env))
469 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
471 qemu_kvm_eat_signal(env, 0);
472 qemu_wait_io_event_common(env);
475 static int qemu_cpu_exec(CPUState *env);
477 static void *kvm_cpu_thread_fn(void *arg)
479 CPUState *env = arg;
481 qemu_mutex_lock(&qemu_global_mutex);
482 qemu_thread_self(env->thread);
483 if (kvm_enabled())
484 kvm_init_vcpu(env);
486 kvm_init_ipi(env);
488 /* signal CPU creation */
489 env->created = 1;
490 qemu_cond_signal(&qemu_cpu_cond);
492 /* and wait for machine initialization */
493 while (!qemu_system_ready)
494 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
496 while (1) {
497 if (cpu_can_run(env))
498 qemu_cpu_exec(env);
499 qemu_kvm_wait_io_event(env);
502 return NULL;
505 static void *tcg_cpu_thread_fn(void *arg)
507 CPUState *env = arg;
509 tcg_init_ipi();
510 qemu_thread_self(env->thread);
512 /* signal CPU creation */
513 qemu_mutex_lock(&qemu_global_mutex);
514 for (env = first_cpu; env != NULL; env = env->next_cpu)
515 env->created = 1;
516 qemu_cond_signal(&qemu_cpu_cond);
518 /* and wait for machine initialization */
519 while (!qemu_system_ready)
520 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
522 while (1) {
523 cpu_exec_all();
524 qemu_tcg_wait_io_event();
527 return NULL;
530 void qemu_cpu_kick(void *_env)
532 CPUState *env = _env;
533 qemu_cond_broadcast(env->halt_cond);
534 qemu_thread_signal(env->thread, SIG_IPI);
537 int qemu_cpu_self(void *_env)
539 CPUState *env = _env;
540 QemuThread this;
542 qemu_thread_self(&this);
544 return qemu_thread_equal(&this, env->thread);
547 static void cpu_signal(int sig)
549 if (cpu_single_env)
550 cpu_exit(cpu_single_env);
551 exit_request = 1;
554 static void tcg_init_ipi(void)
556 sigset_t set;
557 struct sigaction sigact;
559 memset(&sigact, 0, sizeof(sigact));
560 sigact.sa_handler = cpu_signal;
561 sigaction(SIG_IPI, &sigact, NULL);
563 sigemptyset(&set);
564 sigaddset(&set, SIG_IPI);
565 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
568 static void dummy_signal(int sig)
572 static void kvm_init_ipi(CPUState *env)
574 int r;
575 sigset_t set;
576 struct sigaction sigact;
578 memset(&sigact, 0, sizeof(sigact));
579 sigact.sa_handler = dummy_signal;
580 sigaction(SIG_IPI, &sigact, NULL);
582 pthread_sigmask(SIG_BLOCK, NULL, &set);
583 sigdelset(&set, SIG_IPI);
584 r = kvm_set_signal_mask(env, &set);
585 if (r) {
586 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
587 exit(1);
591 static void unblock_io_signals(void)
593 sigset_t set;
595 sigemptyset(&set);
596 sigaddset(&set, SIGUSR2);
597 sigaddset(&set, SIGIO);
598 sigaddset(&set, SIGALRM);
599 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
601 sigemptyset(&set);
602 sigaddset(&set, SIG_IPI);
603 pthread_sigmask(SIG_BLOCK, &set, NULL);
606 void qemu_mutex_lock_iothread(void)
608 if (kvm_enabled()) {
609 qemu_mutex_lock(&qemu_fair_mutex);
610 qemu_mutex_lock(&qemu_global_mutex);
611 qemu_mutex_unlock(&qemu_fair_mutex);
612 } else {
613 qemu_mutex_lock(&qemu_fair_mutex);
614 if (qemu_mutex_trylock(&qemu_global_mutex)) {
615 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
616 qemu_mutex_lock(&qemu_global_mutex);
618 qemu_mutex_unlock(&qemu_fair_mutex);
622 void qemu_mutex_unlock_iothread(void)
624 qemu_mutex_unlock(&qemu_global_mutex);
627 static int all_vcpus_paused(void)
629 CPUState *penv = first_cpu;
631 while (penv) {
632 if (!penv->stopped)
633 return 0;
634 penv = (CPUState *)penv->next_cpu;
637 return 1;
640 void pause_all_vcpus(void)
642 CPUState *penv = first_cpu;
644 while (penv) {
645 penv->stop = 1;
646 qemu_cpu_kick(penv);
647 penv = (CPUState *)penv->next_cpu;
650 while (!all_vcpus_paused()) {
651 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
652 penv = first_cpu;
653 while (penv) {
654 qemu_cpu_kick(penv);
655 penv = (CPUState *)penv->next_cpu;
660 void resume_all_vcpus(void)
662 CPUState *penv = first_cpu;
664 while (penv) {
665 penv->stop = 0;
666 penv->stopped = 0;
667 qemu_cpu_kick(penv);
668 penv = (CPUState *)penv->next_cpu;
672 static void tcg_init_vcpu(void *_env)
674 CPUState *env = _env;
675 /* share a single thread for all cpus with TCG */
676 if (!tcg_cpu_thread) {
677 env->thread = qemu_mallocz(sizeof(QemuThread));
678 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
679 qemu_cond_init(env->halt_cond);
680 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
681 while (env->created == 0)
682 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
683 tcg_cpu_thread = env->thread;
684 tcg_halt_cond = env->halt_cond;
685 } else {
686 env->thread = tcg_cpu_thread;
687 env->halt_cond = tcg_halt_cond;
691 static void kvm_start_vcpu(CPUState *env)
693 env->thread = qemu_mallocz(sizeof(QemuThread));
694 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
695 qemu_cond_init(env->halt_cond);
696 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
697 while (env->created == 0)
698 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
701 void qemu_init_vcpu(void *_env)
703 CPUState *env = _env;
705 env->nr_cores = smp_cores;
706 env->nr_threads = smp_threads;
707 if (kvm_enabled())
708 kvm_start_vcpu(env);
709 else
710 tcg_init_vcpu(env);
713 void qemu_notify_event(void)
715 qemu_event_increment();
718 static void qemu_system_vmstop_request(int reason)
720 vmstop_requested = reason;
721 qemu_notify_event();
724 void vm_stop(int reason)
726 QemuThread me;
727 qemu_thread_self(&me);
729 if (!qemu_thread_equal(&me, &io_thread)) {
730 qemu_system_vmstop_request(reason);
732 * FIXME: should not return to device code in case
733 * vm_stop() has been requested.
735 if (cpu_single_env) {
736 cpu_exit(cpu_single_env);
737 cpu_single_env->stop = 1;
739 return;
741 do_vm_stop(reason);
744 #endif
746 static int qemu_cpu_exec(CPUState *env)
748 int ret;
749 #ifdef CONFIG_PROFILER
750 int64_t ti;
751 #endif
753 #ifdef CONFIG_PROFILER
754 ti = profile_getclock();
755 #endif
756 if (use_icount) {
757 int64_t count;
758 int decr;
759 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
760 env->icount_decr.u16.low = 0;
761 env->icount_extra = 0;
762 count = qemu_icount_round (qemu_next_deadline());
763 qemu_icount += count;
764 decr = (count > 0xffff) ? 0xffff : count;
765 count -= decr;
766 env->icount_decr.u16.low = decr;
767 env->icount_extra = count;
769 ret = cpu_exec(env);
770 #ifdef CONFIG_PROFILER
771 qemu_time += profile_getclock() - ti;
772 #endif
773 if (use_icount) {
774 /* Fold pending instructions back into the
775 instruction counter, and clear the interrupt flag. */
776 qemu_icount -= (env->icount_decr.u16.low
777 + env->icount_extra);
778 env->icount_decr.u32 = 0;
779 env->icount_extra = 0;
781 return ret;
784 bool cpu_exec_all(void)
786 if (next_cpu == NULL)
787 next_cpu = first_cpu;
788 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
789 CPUState *env = next_cpu;
791 qemu_clock_enable(vm_clock,
792 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
794 if (qemu_alarm_pending())
795 break;
796 if (cpu_can_run(env)) {
797 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
798 break;
800 } else if (env->stop) {
801 break;
804 exit_request = 0;
805 return any_cpu_has_work();
808 void set_numa_modes(void)
810 CPUState *env;
811 int i;
813 for (env = first_cpu; env != NULL; env = env->next_cpu) {
814 for (i = 0; i < nb_numa_nodes; i++) {
815 if (node_cpumask[i] & (1 << env->cpu_index)) {
816 env->numa_node = i;
822 void set_cpu_log(const char *optarg)
824 int mask;
825 const CPULogItem *item;
827 mask = cpu_str_to_log_mask(optarg);
828 if (!mask) {
829 printf("Log items (comma separated):\n");
830 for (item = cpu_log_items; item->mask != 0; item++) {
831 printf("%-10s %s\n", item->name, item->help);
833 exit(1);
835 cpu_set_log(mask);
838 /* Return the virtual CPU time, based on the instruction counter. */
839 int64_t cpu_get_icount(void)
841 int64_t icount;
842 CPUState *env = cpu_single_env;;
844 icount = qemu_icount;
845 if (env) {
846 if (!can_do_io(env)) {
847 fprintf(stderr, "Bad clock read\n");
849 icount -= (env->icount_decr.u16.low + env->icount_extra);
851 return qemu_icount_bias + (icount << icount_time_shift);
854 void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
855 const char *optarg)
857 /* XXX: implement xxx_cpu_list for targets that still miss it */
858 #if defined(cpu_list_id)
859 cpu_list_id(f, cpu_fprintf, optarg);
860 #elif defined(cpu_list)
861 cpu_list(f, cpu_fprintf); /* deprecated */
862 #endif