ac97: IOMMU support
[qemu-kvm/amd-iommu.git] / cpus.c
blobfdbaa568f1bf98bb9fd48857b4b0011f47ef1c50
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 #ifdef SIGRTMIN
37 #define SIG_IPI (SIGRTMIN+4)
38 #else
39 #define SIG_IPI SIGUSR1
40 #endif
42 static CPUState *cur_cpu;
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 tcg_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 #ifndef _WIN32
145 static int io_thread_fd = -1;
147 static void qemu_event_increment(void)
149 /* Write 8 bytes to be compatible with eventfd. */
150 static const uint64_t val = 1;
151 ssize_t ret;
153 if (io_thread_fd == -1)
154 return;
156 do {
157 ret = write(io_thread_fd, &val, sizeof(val));
158 } while (ret < 0 && errno == EINTR);
160 /* EAGAIN is fine, a read must be pending. */
161 if (ret < 0 && errno != EAGAIN) {
162 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
163 strerror(errno));
164 exit (1);
168 static void qemu_event_read(void *opaque)
170 int fd = (unsigned long)opaque;
171 ssize_t len;
172 char buffer[512];
174 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
175 do {
176 len = read(fd, buffer, sizeof(buffer));
177 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
180 static int qemu_event_init(void)
182 int err;
183 int fds[2];
185 err = qemu_eventfd(fds);
186 if (err == -1)
187 return -errno;
189 err = fcntl_setfl(fds[0], O_NONBLOCK);
190 if (err < 0)
191 goto fail;
193 err = fcntl_setfl(fds[1], O_NONBLOCK);
194 if (err < 0)
195 goto fail;
197 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
198 (void *)(unsigned long)fds[0]);
200 io_thread_fd = fds[1];
201 return 0;
203 fail:
204 close(fds[0]);
205 close(fds[1]);
206 return err;
208 #else
209 HANDLE qemu_event_handle;
211 static void dummy_event_handler(void *opaque)
215 static int qemu_event_init(void)
217 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
218 if (!qemu_event_handle) {
219 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
220 return -1;
222 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
223 return 0;
226 static void qemu_event_increment(void)
228 if (!SetEvent(qemu_event_handle)) {
229 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
230 GetLastError());
231 exit (1);
234 #endif
236 #ifndef CONFIG_IOTHREAD
237 int qemu_init_main_loop(void)
239 return qemu_event_init();
242 void qemu_main_loop_start(void)
246 void qemu_init_vcpu(void *_env)
248 CPUState *env = _env;
250 env->nr_cores = smp_cores;
251 env->nr_threads = smp_threads;
252 if (kvm_enabled())
253 kvm_init_vcpu(env);
254 return;
257 int qemu_cpu_self(void *env)
259 return 1;
262 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
264 func(data);
267 void resume_all_vcpus(void)
271 void pause_all_vcpus(void)
275 void qemu_cpu_kick(void *env)
277 return;
280 void qemu_notify_event(void)
282 CPUState *env = cpu_single_env;
284 if (kvm_enabled()) {
285 qemu_kvm_notify_work();
286 return;
289 qemu_event_increment ();
290 if (env) {
291 cpu_exit(env);
293 if (next_cpu && env != next_cpu) {
294 cpu_exit(next_cpu);
298 #if defined(KVM_UPSTREAM) || !defined(CONFIG_KVM)
299 void qemu_mutex_lock_iothread(void) {}
300 void qemu_mutex_unlock_iothread(void) {}
301 #endif
303 void vm_stop(int reason)
305 do_vm_stop(reason);
308 #else /* CONFIG_IOTHREAD */
310 #include "qemu-thread.h"
312 QemuMutex qemu_global_mutex;
313 static QemuMutex qemu_fair_mutex;
315 static QemuThread io_thread;
317 static QemuThread *tcg_cpu_thread;
318 static QemuCond *tcg_halt_cond;
320 static int qemu_system_ready;
321 /* cpu creation */
322 static QemuCond qemu_cpu_cond;
323 /* system init */
324 static QemuCond qemu_system_cond;
325 static QemuCond qemu_pause_cond;
326 static QemuCond qemu_work_cond;
328 static void tcg_init_ipi(void);
329 static void kvm_init_ipi(CPUState *env);
330 static void unblock_io_signals(void);
332 int qemu_init_main_loop(void)
334 int ret;
336 ret = qemu_event_init();
337 if (ret)
338 return ret;
340 qemu_cond_init(&qemu_pause_cond);
341 qemu_mutex_init(&qemu_fair_mutex);
342 qemu_mutex_init(&qemu_global_mutex);
343 qemu_mutex_lock(&qemu_global_mutex);
345 unblock_io_signals();
346 qemu_thread_self(&io_thread);
348 return 0;
351 void qemu_main_loop_start(void)
353 qemu_system_ready = 1;
354 qemu_cond_broadcast(&qemu_system_cond);
357 void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
359 struct qemu_work_item wi;
361 if (qemu_cpu_self(env)) {
362 func(data);
363 return;
366 wi.func = func;
367 wi.data = data;
368 if (!env->queued_work_first)
369 env->queued_work_first = &wi;
370 else
371 env->queued_work_last->next = &wi;
372 env->queued_work_last = &wi;
373 wi.next = NULL;
374 wi.done = false;
376 qemu_cpu_kick(env);
377 while (!wi.done) {
378 CPUState *self_env = cpu_single_env;
380 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
381 cpu_single_env = self_env;
385 static void flush_queued_work(CPUState *env)
387 struct qemu_work_item *wi;
389 if (!env->queued_work_first)
390 return;
392 while ((wi = env->queued_work_first)) {
393 env->queued_work_first = wi->next;
394 wi->func(wi->data);
395 wi->done = true;
397 env->queued_work_last = NULL;
398 qemu_cond_broadcast(&qemu_work_cond);
401 static void qemu_wait_io_event_common(CPUState *env)
403 if (env->stop) {
404 env->stop = 0;
405 env->stopped = 1;
406 qemu_cond_signal(&qemu_pause_cond);
408 flush_queued_work(env);
411 static void qemu_wait_io_event(CPUState *env)
413 while (!tcg_has_work())
414 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
416 qemu_mutex_unlock(&qemu_global_mutex);
419 * Users of qemu_global_mutex can be starved, having no chance
420 * to acquire it since this path will get to it first.
421 * So use another lock to provide fairness.
423 qemu_mutex_lock(&qemu_fair_mutex);
424 qemu_mutex_unlock(&qemu_fair_mutex);
426 qemu_mutex_lock(&qemu_global_mutex);
427 qemu_wait_io_event_common(env);
430 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
432 struct timespec ts;
433 int r, e;
434 siginfo_t siginfo;
435 sigset_t waitset;
437 ts.tv_sec = timeout / 1000;
438 ts.tv_nsec = (timeout % 1000) * 1000000;
440 sigemptyset(&waitset);
441 sigaddset(&waitset, SIG_IPI);
443 qemu_mutex_unlock(&qemu_global_mutex);
444 r = sigtimedwait(&waitset, &siginfo, &ts);
445 e = errno;
446 qemu_mutex_lock(&qemu_global_mutex);
448 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
449 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
450 exit(1);
454 static void qemu_kvm_wait_io_event(CPUState *env)
456 while (!cpu_has_work(env))
457 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
459 qemu_kvm_eat_signal(env, 0);
460 qemu_wait_io_event_common(env);
463 static int qemu_cpu_exec(CPUState *env);
465 static void *kvm_cpu_thread_fn(void *arg)
467 CPUState *env = arg;
469 qemu_mutex_lock(&qemu_global_mutex);
470 qemu_thread_self(env->thread);
471 if (kvm_enabled())
472 kvm_init_vcpu(env);
474 kvm_init_ipi(env);
476 /* signal CPU creation */
477 env->created = 1;
478 qemu_cond_signal(&qemu_cpu_cond);
480 /* and wait for machine initialization */
481 while (!qemu_system_ready)
482 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
484 while (1) {
485 if (cpu_can_run(env))
486 qemu_cpu_exec(env);
487 qemu_kvm_wait_io_event(env);
490 return NULL;
493 static void *tcg_cpu_thread_fn(void *arg)
495 CPUState *env = arg;
497 tcg_init_ipi();
498 qemu_thread_self(env->thread);
500 /* signal CPU creation */
501 qemu_mutex_lock(&qemu_global_mutex);
502 for (env = first_cpu; env != NULL; env = env->next_cpu)
503 env->created = 1;
504 qemu_cond_signal(&qemu_cpu_cond);
506 /* and wait for machine initialization */
507 while (!qemu_system_ready)
508 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
510 while (1) {
511 tcg_cpu_exec();
512 qemu_wait_io_event(cur_cpu);
515 return NULL;
518 void qemu_cpu_kick(void *_env)
520 CPUState *env = _env;
521 qemu_cond_broadcast(env->halt_cond);
522 qemu_thread_signal(env->thread, SIG_IPI);
525 int qemu_cpu_self(void *_env)
527 CPUState *env = _env;
528 QemuThread this;
530 qemu_thread_self(&this);
532 return qemu_thread_equal(&this, env->thread);
535 static void cpu_signal(int sig)
537 if (cpu_single_env)
538 cpu_exit(cpu_single_env);
539 exit_request = 1;
542 static void tcg_init_ipi(void)
544 sigset_t set;
545 struct sigaction sigact;
547 memset(&sigact, 0, sizeof(sigact));
548 sigact.sa_handler = cpu_signal;
549 sigaction(SIG_IPI, &sigact, NULL);
551 sigemptyset(&set);
552 sigaddset(&set, SIG_IPI);
553 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
556 static void dummy_signal(int sig)
560 static void kvm_init_ipi(CPUState *env)
562 int r;
563 sigset_t set;
564 struct sigaction sigact;
566 memset(&sigact, 0, sizeof(sigact));
567 sigact.sa_handler = dummy_signal;
568 sigaction(SIG_IPI, &sigact, NULL);
570 pthread_sigmask(SIG_BLOCK, NULL, &set);
571 sigdelset(&set, SIG_IPI);
572 r = kvm_set_signal_mask(env, &set);
573 if (r) {
574 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
575 exit(1);
579 static void unblock_io_signals(void)
581 sigset_t set;
583 sigemptyset(&set);
584 sigaddset(&set, SIGUSR2);
585 sigaddset(&set, SIGIO);
586 sigaddset(&set, SIGALRM);
587 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
589 sigemptyset(&set);
590 sigaddset(&set, SIG_IPI);
591 pthread_sigmask(SIG_BLOCK, &set, NULL);
594 void qemu_mutex_lock_iothread(void)
596 if (kvm_enabled()) {
597 qemu_mutex_lock(&qemu_fair_mutex);
598 qemu_mutex_lock(&qemu_global_mutex);
599 qemu_mutex_unlock(&qemu_fair_mutex);
600 } else {
601 qemu_mutex_lock(&qemu_fair_mutex);
602 if (qemu_mutex_trylock(&qemu_global_mutex)) {
603 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
604 qemu_mutex_lock(&qemu_global_mutex);
606 qemu_mutex_unlock(&qemu_fair_mutex);
610 void qemu_mutex_unlock_iothread(void)
612 qemu_mutex_unlock(&qemu_global_mutex);
615 static int all_vcpus_paused(void)
617 CPUState *penv = first_cpu;
619 while (penv) {
620 if (!penv->stopped)
621 return 0;
622 penv = (CPUState *)penv->next_cpu;
625 return 1;
628 void pause_all_vcpus(void)
630 CPUState *penv = first_cpu;
632 while (penv) {
633 penv->stop = 1;
634 qemu_cpu_kick(penv);
635 penv = (CPUState *)penv->next_cpu;
638 while (!all_vcpus_paused()) {
639 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
640 penv = first_cpu;
641 while (penv) {
642 qemu_cpu_kick(penv);
643 penv = (CPUState *)penv->next_cpu;
648 void resume_all_vcpus(void)
650 CPUState *penv = first_cpu;
652 while (penv) {
653 penv->stop = 0;
654 penv->stopped = 0;
655 qemu_cpu_kick(penv);
656 penv = (CPUState *)penv->next_cpu;
660 static void tcg_init_vcpu(void *_env)
662 CPUState *env = _env;
663 /* share a single thread for all cpus with TCG */
664 if (!tcg_cpu_thread) {
665 env->thread = qemu_mallocz(sizeof(QemuThread));
666 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
667 qemu_cond_init(env->halt_cond);
668 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
669 while (env->created == 0)
670 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
671 tcg_cpu_thread = env->thread;
672 tcg_halt_cond = env->halt_cond;
673 } else {
674 env->thread = tcg_cpu_thread;
675 env->halt_cond = tcg_halt_cond;
679 static void kvm_start_vcpu(CPUState *env)
681 env->thread = qemu_mallocz(sizeof(QemuThread));
682 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
683 qemu_cond_init(env->halt_cond);
684 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
685 while (env->created == 0)
686 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
689 void qemu_init_vcpu(void *_env)
691 CPUState *env = _env;
693 env->nr_cores = smp_cores;
694 env->nr_threads = smp_threads;
695 if (kvm_enabled())
696 kvm_start_vcpu(env);
697 else
698 tcg_init_vcpu(env);
701 void qemu_notify_event(void)
703 qemu_event_increment();
706 static void qemu_system_vmstop_request(int reason)
708 vmstop_requested = reason;
709 qemu_notify_event();
712 void vm_stop(int reason)
714 QemuThread me;
715 qemu_thread_self(&me);
717 if (!qemu_thread_equal(&me, &io_thread)) {
718 qemu_system_vmstop_request(reason);
720 * FIXME: should not return to device code in case
721 * vm_stop() has been requested.
723 if (cpu_single_env) {
724 cpu_exit(cpu_single_env);
725 cpu_single_env->stop = 1;
727 return;
729 do_vm_stop(reason);
732 #endif
734 static int qemu_cpu_exec(CPUState *env)
736 int ret;
737 #ifdef CONFIG_PROFILER
738 int64_t ti;
739 #endif
741 #ifdef CONFIG_PROFILER
742 ti = profile_getclock();
743 #endif
744 if (use_icount) {
745 int64_t count;
746 int decr;
747 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
748 env->icount_decr.u16.low = 0;
749 env->icount_extra = 0;
750 count = qemu_icount_round (qemu_next_deadline());
751 qemu_icount += count;
752 decr = (count > 0xffff) ? 0xffff : count;
753 count -= decr;
754 env->icount_decr.u16.low = decr;
755 env->icount_extra = count;
757 ret = cpu_exec(env);
758 #ifdef CONFIG_PROFILER
759 qemu_time += profile_getclock() - ti;
760 #endif
761 if (use_icount) {
762 /* Fold pending instructions back into the
763 instruction counter, and clear the interrupt flag. */
764 qemu_icount -= (env->icount_decr.u16.low
765 + env->icount_extra);
766 env->icount_decr.u32 = 0;
767 env->icount_extra = 0;
769 return ret;
772 bool tcg_cpu_exec(void)
774 int ret = 0;
776 if (next_cpu == NULL)
777 next_cpu = first_cpu;
778 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
779 CPUState *env = cur_cpu = next_cpu;
781 qemu_clock_enable(vm_clock,
782 (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
784 if (qemu_alarm_pending())
785 break;
786 if (cpu_can_run(env))
787 ret = qemu_cpu_exec(env);
788 else if (env->stop)
789 break;
791 if (ret == EXCP_DEBUG) {
792 gdb_set_stop_cpu(env);
793 debug_requested = EXCP_DEBUG;
794 break;
797 return tcg_has_work();
800 void set_numa_modes(void)
802 CPUState *env;
803 int i;
805 for (env = first_cpu; env != NULL; env = env->next_cpu) {
806 for (i = 0; i < nb_numa_nodes; i++) {
807 if (node_cpumask[i] & (1 << env->cpu_index)) {
808 env->numa_node = i;
814 void set_cpu_log(const char *optarg)
816 int mask;
817 const CPULogItem *item;
819 mask = cpu_str_to_log_mask(optarg);
820 if (!mask) {
821 printf("Log items (comma separated):\n");
822 for (item = cpu_log_items; item->mask != 0; item++) {
823 printf("%-10s %s\n", item->name, item->help);
825 exit(1);
827 cpu_set_log(mask);
830 /* Return the virtual CPU time, based on the instruction counter. */
831 int64_t cpu_get_icount(void)
833 int64_t icount;
834 CPUState *env = cpu_single_env;;
836 icount = qemu_icount;
837 if (env) {
838 if (!can_do_io(env)) {
839 fprintf(stderr, "Bad clock read\n");
841 icount -= (env->icount_decr.u16.low + env->icount_extra);
843 return qemu_icount_bias + (icount << icount_time_shift);
846 void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
847 const char *optarg)
849 /* XXX: implement xxx_cpu_list for targets that still miss it */
850 #if defined(cpu_list_id)
851 cpu_list_id(f, cpu_fprintf, optarg);
852 #elif defined(cpu_list)
853 cpu_list(f, cpu_fprintf); /* deprecated */
854 #endif