QError: Improve QERR_QMP_BAD_INPUT_OBJECT desc
[qemu/aliguori-queue.git] / cpus.c
blob8450ee4ff4d7b0bbe3db6f057294a32137ca6ecf
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 static void do_vm_stop(int reason)
96 if (vm_running) {
97 cpu_disable_ticks();
98 vm_running = 0;
99 pause_all_vcpus();
100 vm_state_notify(0, reason);
101 monitor_protocol_event(QEVENT_STOP, NULL);
105 static int cpu_can_run(CPUState *env)
107 if (env->stop)
108 return 0;
109 if (env->stopped || !vm_running)
110 return 0;
111 return 1;
114 static int cpu_has_work(CPUState *env)
116 if (env->stop)
117 return 1;
118 if (env->stopped || !vm_running)
119 return 0;
120 if (!env->halted)
121 return 1;
122 if (qemu_cpu_has_work(env))
123 return 1;
124 return 0;
127 static int tcg_has_work(void)
129 CPUState *env;
131 for (env = first_cpu; env != NULL; env = env->next_cpu)
132 if (cpu_has_work(env))
133 return 1;
134 return 0;
137 #ifndef _WIN32
138 static int io_thread_fd = -1;
140 static void qemu_event_increment(void)
142 /* Write 8 bytes to be compatible with eventfd. */
143 static uint64_t val = 1;
144 ssize_t ret;
146 if (io_thread_fd == -1)
147 return;
149 do {
150 ret = write(io_thread_fd, &val, sizeof(val));
151 } while (ret < 0 && errno == EINTR);
153 /* EAGAIN is fine, a read must be pending. */
154 if (ret < 0 && errno != EAGAIN) {
155 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
156 strerror(errno));
157 exit (1);
161 static void qemu_event_read(void *opaque)
163 int fd = (unsigned long)opaque;
164 ssize_t len;
165 char buffer[512];
167 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
168 do {
169 len = read(fd, buffer, sizeof(buffer));
170 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
173 static int qemu_event_init(void)
175 int err;
176 int fds[2];
178 err = qemu_eventfd(fds);
179 if (err == -1)
180 return -errno;
182 err = fcntl_setfl(fds[0], O_NONBLOCK);
183 if (err < 0)
184 goto fail;
186 err = fcntl_setfl(fds[1], O_NONBLOCK);
187 if (err < 0)
188 goto fail;
190 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
191 (void *)(unsigned long)fds[0]);
193 io_thread_fd = fds[1];
194 return 0;
196 fail:
197 close(fds[0]);
198 close(fds[1]);
199 return err;
201 #else
202 HANDLE qemu_event_handle;
204 static void dummy_event_handler(void *opaque)
208 static int qemu_event_init(void)
210 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
211 if (!qemu_event_handle) {
212 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
213 return -1;
215 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
216 return 0;
219 static void qemu_event_increment(void)
221 if (!SetEvent(qemu_event_handle)) {
222 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
223 GetLastError());
224 exit (1);
227 #endif
229 #ifndef CONFIG_IOTHREAD
230 int qemu_init_main_loop(void)
232 return qemu_event_init();
235 void qemu_main_loop_start(void)
239 void qemu_init_vcpu(void *_env)
241 CPUState *env = _env;
243 env->nr_cores = smp_cores;
244 env->nr_threads = smp_threads;
245 if (kvm_enabled())
246 kvm_init_vcpu(env);
247 return;
250 int qemu_cpu_self(void *env)
252 return 1;
255 void resume_all_vcpus(void)
259 void pause_all_vcpus(void)
263 void qemu_cpu_kick(void *env)
265 return;
268 void qemu_notify_event(void)
270 CPUState *env = cpu_single_env;
272 qemu_event_increment ();
273 if (env) {
274 cpu_exit(env);
276 if (next_cpu && env != next_cpu) {
277 cpu_exit(next_cpu);
281 void qemu_mutex_lock_iothread(void) {}
282 void qemu_mutex_unlock_iothread(void) {}
284 void vm_stop(int reason)
286 do_vm_stop(reason);
289 #else /* CONFIG_IOTHREAD */
291 #include "qemu-thread.h"
293 QemuMutex qemu_global_mutex;
294 static QemuMutex qemu_fair_mutex;
296 static QemuThread io_thread;
298 static QemuThread *tcg_cpu_thread;
299 static QemuCond *tcg_halt_cond;
301 static int qemu_system_ready;
302 /* cpu creation */
303 static QemuCond qemu_cpu_cond;
304 /* system init */
305 static QemuCond qemu_system_cond;
306 static QemuCond qemu_pause_cond;
308 static void tcg_block_io_signals(void);
309 static void kvm_block_io_signals(CPUState *env);
310 static void unblock_io_signals(void);
312 int qemu_init_main_loop(void)
314 int ret;
316 ret = qemu_event_init();
317 if (ret)
318 return ret;
320 qemu_cond_init(&qemu_pause_cond);
321 qemu_mutex_init(&qemu_fair_mutex);
322 qemu_mutex_init(&qemu_global_mutex);
323 qemu_mutex_lock(&qemu_global_mutex);
325 unblock_io_signals();
326 qemu_thread_self(&io_thread);
328 return 0;
331 void qemu_main_loop_start(void)
333 qemu_system_ready = 1;
334 qemu_cond_broadcast(&qemu_system_cond);
337 static void qemu_wait_io_event_common(CPUState *env)
339 if (env->stop) {
340 env->stop = 0;
341 env->stopped = 1;
342 qemu_cond_signal(&qemu_pause_cond);
346 static void qemu_wait_io_event(CPUState *env)
348 while (!tcg_has_work())
349 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
351 qemu_mutex_unlock(&qemu_global_mutex);
354 * Users of qemu_global_mutex can be starved, having no chance
355 * to acquire it since this path will get to it first.
356 * So use another lock to provide fairness.
358 qemu_mutex_lock(&qemu_fair_mutex);
359 qemu_mutex_unlock(&qemu_fair_mutex);
361 qemu_mutex_lock(&qemu_global_mutex);
362 qemu_wait_io_event_common(env);
365 static void qemu_kvm_eat_signal(CPUState *env, int timeout)
367 struct timespec ts;
368 int r, e;
369 siginfo_t siginfo;
370 sigset_t waitset;
372 ts.tv_sec = timeout / 1000;
373 ts.tv_nsec = (timeout % 1000) * 1000000;
375 sigemptyset(&waitset);
376 sigaddset(&waitset, SIG_IPI);
378 qemu_mutex_unlock(&qemu_global_mutex);
379 r = sigtimedwait(&waitset, &siginfo, &ts);
380 e = errno;
381 qemu_mutex_lock(&qemu_global_mutex);
383 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
384 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
385 exit(1);
389 static void qemu_kvm_wait_io_event(CPUState *env)
391 while (!cpu_has_work(env))
392 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
394 qemu_kvm_eat_signal(env, 0);
395 qemu_wait_io_event_common(env);
398 static int qemu_cpu_exec(CPUState *env);
400 static void *kvm_cpu_thread_fn(void *arg)
402 CPUState *env = arg;
404 qemu_thread_self(env->thread);
405 if (kvm_enabled())
406 kvm_init_vcpu(env);
408 kvm_block_io_signals(env);
410 /* signal CPU creation */
411 qemu_mutex_lock(&qemu_global_mutex);
412 env->created = 1;
413 qemu_cond_signal(&qemu_cpu_cond);
415 /* and wait for machine initialization */
416 while (!qemu_system_ready)
417 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
419 while (1) {
420 if (cpu_can_run(env))
421 qemu_cpu_exec(env);
422 qemu_kvm_wait_io_event(env);
425 return NULL;
428 static void *tcg_cpu_thread_fn(void *arg)
430 CPUState *env = arg;
432 tcg_block_io_signals();
433 qemu_thread_self(env->thread);
435 /* signal CPU creation */
436 qemu_mutex_lock(&qemu_global_mutex);
437 for (env = first_cpu; env != NULL; env = env->next_cpu)
438 env->created = 1;
439 qemu_cond_signal(&qemu_cpu_cond);
441 /* and wait for machine initialization */
442 while (!qemu_system_ready)
443 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
445 while (1) {
446 tcg_cpu_exec();
447 qemu_wait_io_event(cur_cpu);
450 return NULL;
453 void qemu_cpu_kick(void *_env)
455 CPUState *env = _env;
456 qemu_cond_broadcast(env->halt_cond);
457 if (kvm_enabled())
458 qemu_thread_signal(env->thread, SIG_IPI);
461 int qemu_cpu_self(void *_env)
463 CPUState *env = _env;
464 QemuThread this;
466 qemu_thread_self(&this);
468 return qemu_thread_equal(&this, env->thread);
471 static void cpu_signal(int sig)
473 if (cpu_single_env)
474 cpu_exit(cpu_single_env);
477 static void tcg_block_io_signals(void)
479 sigset_t set;
480 struct sigaction sigact;
482 sigemptyset(&set);
483 sigaddset(&set, SIGUSR2);
484 sigaddset(&set, SIGIO);
485 sigaddset(&set, SIGALRM);
486 sigaddset(&set, SIGCHLD);
487 pthread_sigmask(SIG_BLOCK, &set, NULL);
489 sigemptyset(&set);
490 sigaddset(&set, SIG_IPI);
491 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
493 memset(&sigact, 0, sizeof(sigact));
494 sigact.sa_handler = cpu_signal;
495 sigaction(SIG_IPI, &sigact, NULL);
498 static void dummy_signal(int sig)
502 static void kvm_block_io_signals(CPUState *env)
504 int r;
505 sigset_t set;
506 struct sigaction sigact;
508 sigemptyset(&set);
509 sigaddset(&set, SIGUSR2);
510 sigaddset(&set, SIGIO);
511 sigaddset(&set, SIGALRM);
512 sigaddset(&set, SIGCHLD);
513 sigaddset(&set, SIG_IPI);
514 pthread_sigmask(SIG_BLOCK, &set, NULL);
516 pthread_sigmask(SIG_BLOCK, NULL, &set);
517 sigdelset(&set, SIG_IPI);
519 memset(&sigact, 0, sizeof(sigact));
520 sigact.sa_handler = dummy_signal;
521 sigaction(SIG_IPI, &sigact, NULL);
523 r = kvm_set_signal_mask(env, &set);
524 if (r) {
525 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
526 exit(1);
530 static void unblock_io_signals(void)
532 sigset_t set;
534 sigemptyset(&set);
535 sigaddset(&set, SIGUSR2);
536 sigaddset(&set, SIGIO);
537 sigaddset(&set, SIGALRM);
538 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
540 sigemptyset(&set);
541 sigaddset(&set, SIG_IPI);
542 pthread_sigmask(SIG_BLOCK, &set, NULL);
545 static void qemu_signal_lock(unsigned int msecs)
547 qemu_mutex_lock(&qemu_fair_mutex);
549 while (qemu_mutex_trylock(&qemu_global_mutex)) {
550 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
551 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
552 break;
554 qemu_mutex_unlock(&qemu_fair_mutex);
557 void qemu_mutex_lock_iothread(void)
559 if (kvm_enabled()) {
560 qemu_mutex_lock(&qemu_fair_mutex);
561 qemu_mutex_lock(&qemu_global_mutex);
562 qemu_mutex_unlock(&qemu_fair_mutex);
563 } else
564 qemu_signal_lock(100);
567 void qemu_mutex_unlock_iothread(void)
569 qemu_mutex_unlock(&qemu_global_mutex);
572 static int all_vcpus_paused(void)
574 CPUState *penv = first_cpu;
576 while (penv) {
577 if (!penv->stopped)
578 return 0;
579 penv = (CPUState *)penv->next_cpu;
582 return 1;
585 void pause_all_vcpus(void)
587 CPUState *penv = first_cpu;
589 while (penv) {
590 penv->stop = 1;
591 qemu_thread_signal(penv->thread, SIG_IPI);
592 qemu_cpu_kick(penv);
593 penv = (CPUState *)penv->next_cpu;
596 while (!all_vcpus_paused()) {
597 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
598 penv = first_cpu;
599 while (penv) {
600 qemu_thread_signal(penv->thread, SIG_IPI);
601 penv = (CPUState *)penv->next_cpu;
606 void resume_all_vcpus(void)
608 CPUState *penv = first_cpu;
610 while (penv) {
611 penv->stop = 0;
612 penv->stopped = 0;
613 qemu_thread_signal(penv->thread, SIG_IPI);
614 qemu_cpu_kick(penv);
615 penv = (CPUState *)penv->next_cpu;
619 static void tcg_init_vcpu(void *_env)
621 CPUState *env = _env;
622 /* share a single thread for all cpus with TCG */
623 if (!tcg_cpu_thread) {
624 env->thread = qemu_mallocz(sizeof(QemuThread));
625 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
626 qemu_cond_init(env->halt_cond);
627 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
628 while (env->created == 0)
629 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
630 tcg_cpu_thread = env->thread;
631 tcg_halt_cond = env->halt_cond;
632 } else {
633 env->thread = tcg_cpu_thread;
634 env->halt_cond = tcg_halt_cond;
638 static void kvm_start_vcpu(CPUState *env)
640 env->thread = qemu_mallocz(sizeof(QemuThread));
641 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
642 qemu_cond_init(env->halt_cond);
643 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
644 while (env->created == 0)
645 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
648 void qemu_init_vcpu(void *_env)
650 CPUState *env = _env;
652 env->nr_cores = smp_cores;
653 env->nr_threads = smp_threads;
654 if (kvm_enabled())
655 kvm_start_vcpu(env);
656 else
657 tcg_init_vcpu(env);
660 void qemu_notify_event(void)
662 qemu_event_increment();
665 static void qemu_system_vmstop_request(int reason)
667 vmstop_requested = reason;
668 qemu_notify_event();
671 void vm_stop(int reason)
673 QemuThread me;
674 qemu_thread_self(&me);
676 if (!qemu_thread_equal(&me, &io_thread)) {
677 qemu_system_vmstop_request(reason);
679 * FIXME: should not return to device code in case
680 * vm_stop() has been requested.
682 if (cpu_single_env) {
683 cpu_exit(cpu_single_env);
684 cpu_single_env->stop = 1;
686 return;
688 do_vm_stop(reason);
691 #endif
693 static int qemu_cpu_exec(CPUState *env)
695 int ret;
696 #ifdef CONFIG_PROFILER
697 int64_t ti;
698 #endif
700 #ifdef CONFIG_PROFILER
701 ti = profile_getclock();
702 #endif
703 if (use_icount) {
704 int64_t count;
705 int decr;
706 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
707 env->icount_decr.u16.low = 0;
708 env->icount_extra = 0;
709 count = qemu_icount_round (qemu_next_deadline());
710 qemu_icount += count;
711 decr = (count > 0xffff) ? 0xffff : count;
712 count -= decr;
713 env->icount_decr.u16.low = decr;
714 env->icount_extra = count;
716 ret = cpu_exec(env);
717 #ifdef CONFIG_PROFILER
718 qemu_time += profile_getclock() - ti;
719 #endif
720 if (use_icount) {
721 /* Fold pending instructions back into the
722 instruction counter, and clear the interrupt flag. */
723 qemu_icount -= (env->icount_decr.u16.low
724 + env->icount_extra);
725 env->icount_decr.u32 = 0;
726 env->icount_extra = 0;
728 return ret;
731 bool tcg_cpu_exec(void)
733 int ret = 0;
735 if (next_cpu == NULL)
736 next_cpu = first_cpu;
737 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
738 CPUState *env = cur_cpu = next_cpu;
740 qemu_clock_enable(vm_clock,
741 (cur_cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
743 if (qemu_alarm_pending())
744 break;
745 if (cpu_can_run(env))
746 ret = qemu_cpu_exec(env);
747 else if (env->stop)
748 break;
750 if (ret == EXCP_DEBUG) {
751 gdb_set_stop_cpu(env);
752 debug_requested = EXCP_DEBUG;
753 break;
756 return tcg_has_work();
759 void set_numa_modes(void)
761 CPUState *env;
762 int i;
764 for (env = first_cpu; env != NULL; env = env->next_cpu) {
765 for (i = 0; i < nb_numa_nodes; i++) {
766 if (node_cpumask[i] & (1 << env->cpu_index)) {
767 env->numa_node = i;
773 void set_cpu_log(const char *optarg)
775 int mask;
776 const CPULogItem *item;
778 mask = cpu_str_to_log_mask(optarg);
779 if (!mask) {
780 printf("Log items (comma separated):\n");
781 for (item = cpu_log_items; item->mask != 0; item++) {
782 printf("%-10s %s\n", item->name, item->help);
784 exit(1);
786 cpu_set_log(mask);
789 /* Return the virtual CPU time, based on the instruction counter. */
790 int64_t cpu_get_icount(void)
792 int64_t icount;
793 CPUState *env = cpu_single_env;;
795 icount = qemu_icount;
796 if (env) {
797 if (!can_do_io(env)) {
798 fprintf(stderr, "Bad clock read\n");
800 icount -= (env->icount_decr.u16.low + env->icount_extra);
802 return qemu_icount_bias + (icount << icount_time_shift);