virtio-pci: compile per-target
[qemu/aliguori-queue.git] / cpus.c
bloba2e0642a46e6f1455e2e3b9f794390701f82b087
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"
34 #include "cpus.h"
36 static CPUState *cur_cpu;
37 static CPUState *next_cpu;
39 /***********************************************************/
40 void hw_error(const char *fmt, ...)
42 va_list ap;
43 CPUState *env;
45 va_start(ap, fmt);
46 fprintf(stderr, "qemu: hardware error: ");
47 vfprintf(stderr, fmt, ap);
48 fprintf(stderr, "\n");
49 for(env = first_cpu; env != NULL; env = env->next_cpu) {
50 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
51 #ifdef TARGET_I386
52 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
53 #else
54 cpu_dump_state(env, stderr, fprintf, 0);
55 #endif
57 va_end(ap);
58 abort();
61 void cpu_synchronize_all_states(void)
63 CPUState *cpu;
65 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
66 cpu_synchronize_state(cpu);
70 void cpu_synchronize_all_post_reset(void)
72 CPUState *cpu;
74 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
75 cpu_synchronize_post_reset(cpu);
79 void cpu_synchronize_all_post_init(void)
81 CPUState *cpu;
83 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
84 cpu_synchronize_post_init(cpu);
88 static void do_vm_stop(int reason)
90 if (vm_running) {
91 cpu_disable_ticks();
92 vm_running = 0;
93 pause_all_vcpus();
94 vm_state_notify(0, reason);
95 monitor_protocol_event(QEVENT_STOP, NULL);
99 static int cpu_can_run(CPUState *env)
101 if (env->stop)
102 return 0;
103 if (env->stopped)
104 return 0;
105 if (!vm_running)
106 return 0;
107 return 1;
110 static int cpu_has_work(CPUState *env)
112 if (env->stop)
113 return 1;
114 if (env->stopped)
115 return 0;
116 if (!env->halted)
117 return 1;
118 if (qemu_cpu_has_work(env))
119 return 1;
120 return 0;
123 static int tcg_has_work(void)
125 CPUState *env;
127 for (env = first_cpu; env != NULL; env = env->next_cpu)
128 if (cpu_has_work(env))
129 return 1;
130 return 0;
133 #ifndef _WIN32
134 static int io_thread_fd = -1;
136 static void qemu_event_increment(void)
138 /* Write 8 bytes to be compatible with eventfd. */
139 static uint64_t val = 1;
140 ssize_t ret;
142 if (io_thread_fd == -1)
143 return;
145 do {
146 ret = write(io_thread_fd, &val, sizeof(val));
147 } while (ret < 0 && errno == EINTR);
149 /* EAGAIN is fine, a read must be pending. */
150 if (ret < 0 && errno != EAGAIN) {
151 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
152 strerror(errno));
153 exit (1);
157 static void qemu_event_read(void *opaque)
159 int fd = (unsigned long)opaque;
160 ssize_t len;
161 char buffer[512];
163 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
164 do {
165 len = read(fd, buffer, sizeof(buffer));
166 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
169 static int qemu_event_init(void)
171 int err;
172 int fds[2];
174 err = qemu_eventfd(fds);
175 if (err == -1)
176 return -errno;
178 err = fcntl_setfl(fds[0], O_NONBLOCK);
179 if (err < 0)
180 goto fail;
182 err = fcntl_setfl(fds[1], O_NONBLOCK);
183 if (err < 0)
184 goto fail;
186 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
187 (void *)(unsigned long)fds[0]);
189 io_thread_fd = fds[1];
190 return 0;
192 fail:
193 close(fds[0]);
194 close(fds[1]);
195 return err;
197 #else
198 HANDLE qemu_event_handle;
200 static void dummy_event_handler(void *opaque)
204 static int qemu_event_init(void)
206 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
207 if (!qemu_event_handle) {
208 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
209 return -1;
211 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
212 return 0;
215 static void qemu_event_increment(void)
217 if (!SetEvent(qemu_event_handle)) {
218 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
219 GetLastError());
220 exit (1);
223 #endif
225 #ifndef CONFIG_IOTHREAD
226 int qemu_init_main_loop(void)
228 return qemu_event_init();
231 void qemu_init_vcpu(void *_env)
233 CPUState *env = _env;
235 env->nr_cores = smp_cores;
236 env->nr_threads = smp_threads;
237 if (kvm_enabled())
238 kvm_init_vcpu(env);
239 return;
242 int qemu_cpu_self(void *env)
244 return 1;
247 void resume_all_vcpus(void)
251 void pause_all_vcpus(void)
255 void qemu_cpu_kick(void *env)
257 return;
260 void qemu_notify_event(void)
262 CPUState *env = cpu_single_env;
264 qemu_event_increment ();
265 if (env) {
266 cpu_exit(env);
268 if (next_cpu && env != next_cpu) {
269 cpu_exit(next_cpu);
273 void qemu_mutex_lock_iothread(void) {}
274 void qemu_mutex_unlock_iothread(void) {}
276 void vm_stop(int reason)
278 do_vm_stop(reason);
281 #else /* CONFIG_IOTHREAD */
283 #include "qemu-thread.h"
285 QemuMutex qemu_global_mutex;
286 static QemuMutex qemu_fair_mutex;
288 static QemuThread io_thread;
290 static QemuThread *tcg_cpu_thread;
291 static QemuCond *tcg_halt_cond;
293 static int qemu_system_ready;
294 /* cpu creation */
295 static QemuCond qemu_cpu_cond;
296 /* system init */
297 static QemuCond qemu_system_cond;
298 static QemuCond qemu_pause_cond;
300 static void tcg_block_io_signals(void);
301 static void kvm_block_io_signals(CPUState *env);
302 static void unblock_io_signals(void);
304 int qemu_init_main_loop(void)
306 int ret;
308 ret = qemu_event_init();
309 if (ret)
310 return ret;
312 qemu_cond_init(&qemu_pause_cond);
313 qemu_mutex_init(&qemu_fair_mutex);
314 qemu_mutex_init(&qemu_global_mutex);
315 qemu_mutex_lock(&qemu_global_mutex);
317 unblock_io_signals();
318 qemu_thread_self(&io_thread);
320 return 0;
323 static void qemu_wait_io_event_common(CPUState *env)
325 if (env->stop) {
326 env->stop = 0;
327 env->stopped = 1;
328 qemu_cond_signal(&qemu_pause_cond);
332 static void qemu_wait_io_event(CPUState *env)
334 while (!tcg_has_work())
335 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
337 qemu_mutex_unlock(&qemu_global_mutex);
340 * Users of qemu_global_mutex can be starved, having no chance
341 * to acquire it since this path will get to it first.
342 * So use another lock to provide fairness.
344 qemu_mutex_lock(&qemu_fair_mutex);
345 qemu_mutex_unlock(&qemu_fair_mutex);
347 qemu_mutex_lock(&qemu_global_mutex);
348 qemu_wait_io_event_common(env);
351 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
353 struct timespec ts;
354 int r, e;
355 siginfo_t siginfo;
356 sigset_t waitset;
358 ts.tv_sec = timeout / 1000;
359 ts.tv_nsec = (timeout % 1000) * 1000000;
361 sigemptyset(&waitset);
362 sigaddset(&waitset, SIG_IPI);
364 qemu_mutex_unlock(&qemu_global_mutex);
365 r = sigtimedwait(&waitset, &siginfo, &ts);
366 e = errno;
367 qemu_mutex_lock(&qemu_global_mutex);
369 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
370 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
371 exit(1);
375 static void qemu_kvm_wait_io_event(CPUState *env)
377 while (!cpu_has_work(env))
378 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
380 qemu_kvm_eat_signal(env, 0);
381 qemu_wait_io_event_common(env);
384 static int qemu_cpu_exec(CPUState *env);
386 static void *kvm_cpu_thread_fn(void *arg)
388 CPUState *env = arg;
390 qemu_thread_self(env->thread);
391 if (kvm_enabled())
392 kvm_init_vcpu(env);
394 kvm_block_io_signals(env);
396 /* signal CPU creation */
397 qemu_mutex_lock(&qemu_global_mutex);
398 env->created = 1;
399 qemu_cond_signal(&qemu_cpu_cond);
401 /* and wait for machine initialization */
402 while (!qemu_system_ready)
403 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
405 while (1) {
406 if (cpu_can_run(env))
407 qemu_cpu_exec(env);
408 qemu_kvm_wait_io_event(env);
411 return NULL;
414 static void *tcg_cpu_thread_fn(void *arg)
416 CPUState *env = arg;
418 tcg_block_io_signals();
419 qemu_thread_self(env->thread);
421 /* signal CPU creation */
422 qemu_mutex_lock(&qemu_global_mutex);
423 for (env = first_cpu; env != NULL; env = env->next_cpu)
424 env->created = 1;
425 qemu_cond_signal(&qemu_cpu_cond);
427 /* and wait for machine initialization */
428 while (!qemu_system_ready)
429 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
431 while (1) {
432 tcg_cpu_exec();
433 qemu_wait_io_event(cur_cpu);
436 return NULL;
439 void qemu_cpu_kick(void *_env)
441 CPUState *env = _env;
442 qemu_cond_broadcast(env->halt_cond);
443 if (kvm_enabled())
444 qemu_thread_signal(env->thread, SIG_IPI);
447 int qemu_cpu_self(void *_env)
449 CPUState *env = _env;
450 QemuThread this;
452 qemu_thread_self(&this);
454 return qemu_thread_equal(&this, env->thread);
457 static void cpu_signal(int sig)
459 if (cpu_single_env)
460 cpu_exit(cpu_single_env);
463 static void tcg_block_io_signals(void)
465 sigset_t set;
466 struct sigaction sigact;
468 sigemptyset(&set);
469 sigaddset(&set, SIGUSR2);
470 sigaddset(&set, SIGIO);
471 sigaddset(&set, SIGALRM);
472 sigaddset(&set, SIGCHLD);
473 pthread_sigmask(SIG_BLOCK, &set, NULL);
475 sigemptyset(&set);
476 sigaddset(&set, SIG_IPI);
477 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
479 memset(&sigact, 0, sizeof(sigact));
480 sigact.sa_handler = cpu_signal;
481 sigaction(SIG_IPI, &sigact, NULL);
484 static void dummy_signal(int sig)
488 static void kvm_block_io_signals(CPUState *env)
490 int r;
491 sigset_t set;
492 struct sigaction sigact;
494 sigemptyset(&set);
495 sigaddset(&set, SIGUSR2);
496 sigaddset(&set, SIGIO);
497 sigaddset(&set, SIGALRM);
498 sigaddset(&set, SIGCHLD);
499 sigaddset(&set, SIG_IPI);
500 pthread_sigmask(SIG_BLOCK, &set, NULL);
502 pthread_sigmask(SIG_BLOCK, NULL, &set);
503 sigdelset(&set, SIG_IPI);
505 memset(&sigact, 0, sizeof(sigact));
506 sigact.sa_handler = dummy_signal;
507 sigaction(SIG_IPI, &sigact, NULL);
509 r = kvm_set_signal_mask(env, &set);
510 if (r) {
511 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
512 exit(1);
516 static void unblock_io_signals(void)
518 sigset_t set;
520 sigemptyset(&set);
521 sigaddset(&set, SIGUSR2);
522 sigaddset(&set, SIGIO);
523 sigaddset(&set, SIGALRM);
524 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
526 sigemptyset(&set);
527 sigaddset(&set, SIG_IPI);
528 pthread_sigmask(SIG_BLOCK, &set, NULL);
531 static void qemu_signal_lock(unsigned int msecs)
533 qemu_mutex_lock(&qemu_fair_mutex);
535 while (qemu_mutex_trylock(&qemu_global_mutex)) {
536 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
537 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
538 break;
540 qemu_mutex_unlock(&qemu_fair_mutex);
543 void qemu_mutex_lock_iothread(void)
545 if (kvm_enabled()) {
546 qemu_mutex_lock(&qemu_fair_mutex);
547 qemu_mutex_lock(&qemu_global_mutex);
548 qemu_mutex_unlock(&qemu_fair_mutex);
549 } else
550 qemu_signal_lock(100);
553 void qemu_mutex_unlock_iothread(void)
555 qemu_mutex_unlock(&qemu_global_mutex);
558 static int all_vcpus_paused(void)
560 CPUState *penv = first_cpu;
562 while (penv) {
563 if (!penv->stopped)
564 return 0;
565 penv = (CPUState *)penv->next_cpu;
568 return 1;
571 void pause_all_vcpus(void)
573 CPUState *penv = first_cpu;
575 while (penv) {
576 penv->stop = 1;
577 qemu_thread_signal(penv->thread, SIG_IPI);
578 qemu_cpu_kick(penv);
579 penv = (CPUState *)penv->next_cpu;
582 while (!all_vcpus_paused()) {
583 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
584 penv = first_cpu;
585 while (penv) {
586 qemu_thread_signal(penv->thread, SIG_IPI);
587 penv = (CPUState *)penv->next_cpu;
592 void resume_all_vcpus(void)
594 CPUState *penv = first_cpu;
596 while (penv) {
597 penv->stop = 0;
598 penv->stopped = 0;
599 qemu_thread_signal(penv->thread, SIG_IPI);
600 qemu_cpu_kick(penv);
601 penv = (CPUState *)penv->next_cpu;
605 static void tcg_init_vcpu(void *_env)
607 CPUState *env = _env;
608 /* share a single thread for all cpus with TCG */
609 if (!tcg_cpu_thread) {
610 env->thread = qemu_mallocz(sizeof(QemuThread));
611 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
612 qemu_cond_init(env->halt_cond);
613 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
614 while (env->created == 0)
615 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
616 tcg_cpu_thread = env->thread;
617 tcg_halt_cond = env->halt_cond;
618 } else {
619 env->thread = tcg_cpu_thread;
620 env->halt_cond = tcg_halt_cond;
624 static void kvm_start_vcpu(CPUState *env)
626 env->thread = qemu_mallocz(sizeof(QemuThread));
627 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
628 qemu_cond_init(env->halt_cond);
629 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
630 while (env->created == 0)
631 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
634 void qemu_init_vcpu(void *_env)
636 CPUState *env = _env;
638 env->nr_cores = smp_cores;
639 env->nr_threads = smp_threads;
640 if (kvm_enabled())
641 kvm_start_vcpu(env);
642 else
643 tcg_init_vcpu(env);
646 void qemu_notify_event(void)
648 qemu_event_increment();
651 static void qemu_system_vmstop_request(int reason)
653 vmstop_requested = reason;
654 qemu_notify_event();
657 void vm_stop(int reason)
659 QemuThread me;
660 qemu_thread_self(&me);
662 if (!qemu_thread_equal(&me, &io_thread)) {
663 qemu_system_vmstop_request(reason);
665 * FIXME: should not return to device code in case
666 * vm_stop() has been requested.
668 if (cpu_single_env) {
669 cpu_exit(cpu_single_env);
670 cpu_single_env->stop = 1;
672 return;
674 do_vm_stop(reason);
677 #endif
679 static int qemu_cpu_exec(CPUState *env)
681 int ret;
682 #ifdef CONFIG_PROFILER
683 int64_t ti;
684 #endif
686 #ifdef CONFIG_PROFILER
687 ti = profile_getclock();
688 #endif
689 if (use_icount) {
690 int64_t count;
691 int decr;
692 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
693 env->icount_decr.u16.low = 0;
694 env->icount_extra = 0;
695 count = qemu_icount_round (qemu_next_deadline());
696 qemu_icount += count;
697 decr = (count > 0xffff) ? 0xffff : count;
698 count -= decr;
699 env->icount_decr.u16.low = decr;
700 env->icount_extra = count;
702 ret = cpu_exec(env);
703 #ifdef CONFIG_PROFILER
704 qemu_time += profile_getclock() - ti;
705 #endif
706 if (use_icount) {
707 /* Fold pending instructions back into the
708 instruction counter, and clear the interrupt flag. */
709 qemu_icount -= (env->icount_decr.u16.low
710 + env->icount_extra);
711 env->icount_decr.u32 = 0;
712 env->icount_extra = 0;
714 return ret;
717 bool tcg_cpu_exec(void)
719 int ret = 0;
721 if (next_cpu == NULL)
722 next_cpu = first_cpu;
723 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
724 CPUState *env = cur_cpu = next_cpu;
726 qemu_clock_enable(vm_clock,
727 (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
729 if (qemu_alarm_pending())
730 break;
731 if (cpu_can_run(env))
732 ret = qemu_cpu_exec(env);
733 else if (env->stop)
734 break;
736 if (ret == EXCP_DEBUG) {
737 gdb_set_stop_cpu(env);
738 debug_requested = EXCP_DEBUG;
739 break;
742 return tcg_has_work();
745 void set_numa_modes(void)
747 CPUState *env;
748 int i;
750 for (env = first_cpu; env != NULL; env = env->next_cpu) {
751 for (i = 0; i < nb_numa_nodes; i++) {
752 if (node_cpumask[i] & (1 << env->cpu_index)) {
753 env->numa_node = i;
759 void set_cpu_log(const char *optarg)
761 int mask;
762 const CPULogItem *item;
764 mask = cpu_str_to_log_mask(optarg);
765 if (!mask) {
766 printf("Log items (comma separated):\n");
767 for (item = cpu_log_items; item->mask != 0; item++) {
768 printf("%-10s %s\n", item->name, item->help);
770 exit(1);
772 cpu_set_log(mask);
775 /* Return the virtual CPU time, based on the instruction counter. */
776 int64_t cpu_get_icount(void)
778 int64_t icount;
779 CPUState *env = cpu_single_env;;
781 icount = qemu_icount;
782 if (env) {
783 if (!can_do_io(env)) {
784 fprintf(stderr, "Bad clock read\n");
786 icount -= (env->icount_decr.u16.low + env->icount_extra);
788 return qemu_icount_bias + (icount << icount_time_shift);