]> git.proxmox.com Git - qemu.git/blame - cpus.c
Improve vm_stop reason declarations
[qemu.git] / cpus.c
CommitLineData
296af7c9
BS
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25/* Needed early for CONFIG_BSD etc. */
26#include "config-host.h"
27
28#include "monitor.h"
29#include "sysemu.h"
30#include "gdbstub.h"
31#include "dma.h"
32#include "kvm.h"
262ea18e 33#include "exec-all.h"
296af7c9
BS
34
35#include "cpus.h"
a8486bc9 36#include "compatfd.h"
296af7c9 37
7277e027
BS
38#ifdef SIGRTMIN
39#define SIG_IPI (SIGRTMIN+4)
40#else
41#define SIG_IPI SIGUSR1
42#endif
43
6d9cb73c
JK
44#ifdef CONFIG_LINUX
45
46#include <sys/prctl.h>
47
c0532a76
MT
48#ifndef PR_MCE_KILL
49#define PR_MCE_KILL 33
50#endif
51
6d9cb73c
JK
52#ifndef PR_MCE_KILL_SET
53#define PR_MCE_KILL_SET 1
54#endif
55
56#ifndef PR_MCE_KILL_EARLY
57#define PR_MCE_KILL_EARLY 1
58#endif
59
60#endif /* CONFIG_LINUX */
61
296af7c9
BS
62static CPUState *next_cpu;
63
64/***********************************************************/
65void hw_error(const char *fmt, ...)
66{
67 va_list ap;
68 CPUState *env;
69
70 va_start(ap, fmt);
71 fprintf(stderr, "qemu: hardware error: ");
72 vfprintf(stderr, fmt, ap);
73 fprintf(stderr, "\n");
74 for(env = first_cpu; env != NULL; env = env->next_cpu) {
75 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
76#ifdef TARGET_I386
77 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
78#else
79 cpu_dump_state(env, stderr, fprintf, 0);
80#endif
81 }
82 va_end(ap);
83 abort();
84}
85
86void cpu_synchronize_all_states(void)
87{
88 CPUState *cpu;
89
90 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
91 cpu_synchronize_state(cpu);
92 }
93}
94
95void cpu_synchronize_all_post_reset(void)
96{
97 CPUState *cpu;
98
99 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
100 cpu_synchronize_post_reset(cpu);
101 }
102}
103
104void cpu_synchronize_all_post_init(void)
105{
106 CPUState *cpu;
107
108 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
109 cpu_synchronize_post_init(cpu);
110 }
111}
112
3ae9501c
MT
113int cpu_is_stopped(CPUState *env)
114{
115 return !vm_running || env->stopped;
116}
117
296af7c9
BS
118static void do_vm_stop(int reason)
119{
120 if (vm_running) {
121 cpu_disable_ticks();
122 vm_running = 0;
123 pause_all_vcpus();
124 vm_state_notify(0, reason);
55df6f33
MT
125 qemu_aio_flush();
126 bdrv_flush_all();
296af7c9
BS
127 monitor_protocol_event(QEVENT_STOP, NULL);
128 }
129}
130
131static int cpu_can_run(CPUState *env)
132{
0ab07c62 133 if (env->stop) {
296af7c9 134 return 0;
0ab07c62
JK
135 }
136 if (env->stopped || !vm_running) {
296af7c9 137 return 0;
0ab07c62 138 }
296af7c9
BS
139 return 1;
140}
141
16400322 142static bool cpu_thread_is_idle(CPUState *env)
296af7c9 143{
16400322
JK
144 if (env->stop || env->queued_work_first) {
145 return false;
146 }
147 if (env->stopped || !vm_running) {
148 return true;
149 }
150 if (!env->halted || qemu_cpu_has_work(env)) {
151 return false;
152 }
153 return true;
296af7c9
BS
154}
155
16400322 156static bool all_cpu_threads_idle(void)
296af7c9
BS
157{
158 CPUState *env;
159
16400322
JK
160 for (env = first_cpu; env != NULL; env = env->next_cpu) {
161 if (!cpu_thread_is_idle(env)) {
162 return false;
163 }
164 }
165 return true;
296af7c9
BS
166}
167
3c638d06
JK
168static void cpu_debug_handler(CPUState *env)
169{
170 gdb_set_stop_cpu(env);
e07bbac5
JK
171 debug_requested = VMSTOP_DEBUG;
172 vm_stop(VMSTOP_DEBUG);
3c638d06
JK
173}
174
6d9cb73c
JK
175#ifdef CONFIG_LINUX
176static void sigbus_reraise(void)
177{
178 sigset_t set;
179 struct sigaction action;
180
181 memset(&action, 0, sizeof(action));
182 action.sa_handler = SIG_DFL;
183 if (!sigaction(SIGBUS, &action, NULL)) {
184 raise(SIGBUS);
185 sigemptyset(&set);
186 sigaddset(&set, SIGBUS);
187 sigprocmask(SIG_UNBLOCK, &set, NULL);
188 }
189 perror("Failed to re-raise SIGBUS!\n");
190 abort();
191}
192
193static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
194 void *ctx)
195{
196 if (kvm_on_sigbus(siginfo->ssi_code,
197 (void *)(intptr_t)siginfo->ssi_addr)) {
198 sigbus_reraise();
199 }
200}
201
202static void qemu_init_sigbus(void)
203{
204 struct sigaction action;
205
206 memset(&action, 0, sizeof(action));
207 action.sa_flags = SA_SIGINFO;
208 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
209 sigaction(SIGBUS, &action, NULL);
210
211 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
212}
213
214#else /* !CONFIG_LINUX */
215
216static void qemu_init_sigbus(void)
217{
218}
219#endif /* !CONFIG_LINUX */
220
296af7c9
BS
221#ifndef _WIN32
222static int io_thread_fd = -1;
223
224static void qemu_event_increment(void)
225{
226 /* Write 8 bytes to be compatible with eventfd. */
26a82330 227 static const uint64_t val = 1;
296af7c9
BS
228 ssize_t ret;
229
0ab07c62 230 if (io_thread_fd == -1) {
296af7c9 231 return;
0ab07c62 232 }
296af7c9
BS
233 do {
234 ret = write(io_thread_fd, &val, sizeof(val));
235 } while (ret < 0 && errno == EINTR);
236
237 /* EAGAIN is fine, a read must be pending. */
238 if (ret < 0 && errno != EAGAIN) {
239 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
240 strerror(errno));
241 exit (1);
242 }
243}
244
245static void qemu_event_read(void *opaque)
246{
247 int fd = (unsigned long)opaque;
248 ssize_t len;
249 char buffer[512];
250
251 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
252 do {
253 len = read(fd, buffer, sizeof(buffer));
254 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
255}
256
257static int qemu_event_init(void)
258{
259 int err;
260 int fds[2];
261
262 err = qemu_eventfd(fds);
0ab07c62 263 if (err == -1) {
296af7c9 264 return -errno;
0ab07c62 265 }
296af7c9 266 err = fcntl_setfl(fds[0], O_NONBLOCK);
0ab07c62 267 if (err < 0) {
296af7c9 268 goto fail;
0ab07c62 269 }
296af7c9 270 err = fcntl_setfl(fds[1], O_NONBLOCK);
0ab07c62 271 if (err < 0) {
296af7c9 272 goto fail;
0ab07c62 273 }
296af7c9
BS
274 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
275 (void *)(unsigned long)fds[0]);
276
277 io_thread_fd = fds[1];
278 return 0;
279
280fail:
281 close(fds[0]);
282 close(fds[1]);
283 return err;
284}
55f8d6ac 285
55f8d6ac
JK
286static void dummy_signal(int sig)
287{
288}
55f8d6ac 289
d0f294ce
JK
290/* If we have signalfd, we mask out the signals we want to handle and then
291 * use signalfd to listen for them. We rely on whatever the current signal
292 * handler is to dispatch the signals when we receive them.
293 */
294static void sigfd_handler(void *opaque)
295{
296 int fd = (unsigned long) opaque;
297 struct qemu_signalfd_siginfo info;
298 struct sigaction action;
299 ssize_t len;
300
301 while (1) {
302 do {
303 len = read(fd, &info, sizeof(info));
304 } while (len == -1 && errno == EINTR);
305
306 if (len == -1 && errno == EAGAIN) {
307 break;
308 }
309
310 if (len != sizeof(info)) {
311 printf("read from sigfd returned %zd: %m\n", len);
312 return;
313 }
314
315 sigaction(info.ssi_signo, NULL, &action);
316 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
317 action.sa_sigaction(info.ssi_signo,
318 (siginfo_t *)&info, NULL);
319 } else if (action.sa_handler) {
320 action.sa_handler(info.ssi_signo);
321 }
322 }
323}
324
325static int qemu_signalfd_init(sigset_t mask)
326{
327 int sigfd;
328
329 sigfd = qemu_signalfd(&mask);
330 if (sigfd == -1) {
331 fprintf(stderr, "failed to create signalfd\n");
332 return -errno;
333 }
334
335 fcntl_setfl(sigfd, O_NONBLOCK);
336
337 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
338 (void *)(unsigned long) sigfd);
339
340 return 0;
341}
342
9a36085b
JK
343static void qemu_kvm_eat_signals(CPUState *env)
344{
345 struct timespec ts = { 0, 0 };
346 siginfo_t siginfo;
347 sigset_t waitset;
348 sigset_t chkset;
349 int r;
350
351 sigemptyset(&waitset);
352 sigaddset(&waitset, SIG_IPI);
353 sigaddset(&waitset, SIGBUS);
354
355 do {
356 r = sigtimedwait(&waitset, &siginfo, &ts);
357 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
358 perror("sigtimedwait");
359 exit(1);
360 }
361
362 switch (r) {
9a36085b
JK
363 case SIGBUS:
364 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
365 sigbus_reraise();
366 }
367 break;
9a36085b
JK
368 default:
369 break;
370 }
371
372 r = sigpending(&chkset);
373 if (r == -1) {
374 perror("sigpending");
375 exit(1);
376 }
377 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
de758970
JK
378
379#ifndef CONFIG_IOTHREAD
380 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
381 qemu_notify_event();
382 }
383#endif
9a36085b
JK
384}
385
55f8d6ac
JK
386#else /* _WIN32 */
387
296af7c9
BS
388HANDLE qemu_event_handle;
389
390static void dummy_event_handler(void *opaque)
391{
392}
393
394static int qemu_event_init(void)
395{
396 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
397 if (!qemu_event_handle) {
398 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
399 return -1;
400 }
401 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
402 return 0;
403}
404
405static void qemu_event_increment(void)
406{
407 if (!SetEvent(qemu_event_handle)) {
408 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
409 GetLastError());
410 exit (1);
411 }
412}
9a36085b
JK
413
414static void qemu_kvm_eat_signals(CPUState *env)
415{
416}
55f8d6ac 417#endif /* _WIN32 */
296af7c9
BS
418
419#ifndef CONFIG_IOTHREAD
ff48eb5f
JK
420static void qemu_kvm_init_cpu_signals(CPUState *env)
421{
422#ifndef _WIN32
423 int r;
424 sigset_t set;
425 struct sigaction sigact;
426
427 memset(&sigact, 0, sizeof(sigact));
428 sigact.sa_handler = dummy_signal;
429 sigaction(SIG_IPI, &sigact, NULL);
430
431 sigemptyset(&set);
432 sigaddset(&set, SIG_IPI);
de758970
JK
433 sigaddset(&set, SIGIO);
434 sigaddset(&set, SIGALRM);
ff48eb5f
JK
435 pthread_sigmask(SIG_BLOCK, &set, NULL);
436
437 pthread_sigmask(SIG_BLOCK, NULL, &set);
438 sigdelset(&set, SIG_IPI);
439 sigdelset(&set, SIGBUS);
de758970
JK
440 sigdelset(&set, SIGIO);
441 sigdelset(&set, SIGALRM);
ff48eb5f
JK
442 r = kvm_set_signal_mask(env, &set);
443 if (r) {
444 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
445 exit(1);
446 }
447#endif
448}
449
de758970
JK
450#ifndef _WIN32
451static sigset_t block_synchronous_signals(void)
452{
453 sigset_t set;
454
455 sigemptyset(&set);
6d9cb73c 456 sigaddset(&set, SIGBUS);
de758970
JK
457 if (kvm_enabled()) {
458 /*
459 * We need to process timer signals synchronously to avoid a race
460 * between exit_request check and KVM vcpu entry.
461 */
462 sigaddset(&set, SIGIO);
463 sigaddset(&set, SIGALRM);
464 }
465
466 return set;
467}
468#endif
469
296af7c9
BS
470int qemu_init_main_loop(void)
471{
d0f294ce
JK
472#ifndef _WIN32
473 sigset_t blocked_signals;
474 int ret;
475
de758970 476 blocked_signals = block_synchronous_signals();
d0f294ce
JK
477
478 ret = qemu_signalfd_init(blocked_signals);
479 if (ret) {
480 return ret;
481 }
482#endif
3c638d06
JK
483 cpu_set_debug_excp_handler(cpu_debug_handler);
484
6d9cb73c
JK
485 qemu_init_sigbus();
486
296af7c9
BS
487 return qemu_event_init();
488}
489
7277e027
BS
490void qemu_main_loop_start(void)
491{
492}
493
296af7c9
BS
494void qemu_init_vcpu(void *_env)
495{
496 CPUState *env = _env;
84b4915d 497 int r;
296af7c9
BS
498
499 env->nr_cores = smp_cores;
500 env->nr_threads = smp_threads;
84b4915d
JK
501
502 if (kvm_enabled()) {
503 r = kvm_init_vcpu(env);
504 if (r < 0) {
505 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
506 exit(1);
507 }
ff48eb5f 508 qemu_kvm_init_cpu_signals(env);
84b4915d 509 }
296af7c9
BS
510}
511
512int qemu_cpu_self(void *env)
513{
514 return 1;
515}
516
e82bcec2
MT
517void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
518{
519 func(data);
520}
521
296af7c9
BS
522void resume_all_vcpus(void)
523{
524}
525
526void pause_all_vcpus(void)
527{
528}
529
530void qemu_cpu_kick(void *env)
531{
296af7c9
BS
532}
533
46d62fac
JK
534void qemu_cpu_kick_self(void)
535{
536#ifndef _WIN32
537 assert(cpu_single_env);
538
539 raise(SIG_IPI);
540#else
541 abort();
542#endif
543}
544
296af7c9
BS
545void qemu_notify_event(void)
546{
547 CPUState *env = cpu_single_env;
548
549 qemu_event_increment ();
550 if (env) {
551 cpu_exit(env);
552 }
553 if (next_cpu && env != next_cpu) {
554 cpu_exit(next_cpu);
555 }
38145df2 556 exit_request = 1;
296af7c9
BS
557}
558
559void qemu_mutex_lock_iothread(void) {}
560void qemu_mutex_unlock_iothread(void) {}
561
b4a3d965
JK
562void cpu_stop_current(void)
563{
564}
565
296af7c9
BS
566void vm_stop(int reason)
567{
568 do_vm_stop(reason);
569}
570
571#else /* CONFIG_IOTHREAD */
572
573#include "qemu-thread.h"
574
575QemuMutex qemu_global_mutex;
576static QemuMutex qemu_fair_mutex;
577
578static QemuThread io_thread;
579
580static QemuThread *tcg_cpu_thread;
581static QemuCond *tcg_halt_cond;
582
583static int qemu_system_ready;
584/* cpu creation */
585static QemuCond qemu_cpu_cond;
586/* system init */
587static QemuCond qemu_system_cond;
588static QemuCond qemu_pause_cond;
e82bcec2 589static QemuCond qemu_work_cond;
296af7c9 590
55f8d6ac
JK
591static void cpu_signal(int sig)
592{
593 if (cpu_single_env) {
594 cpu_exit(cpu_single_env);
595 }
596 exit_request = 1;
597}
598
599static void qemu_kvm_init_cpu_signals(CPUState *env)
600{
601 int r;
602 sigset_t set;
603 struct sigaction sigact;
604
605 memset(&sigact, 0, sizeof(sigact));
606 sigact.sa_handler = dummy_signal;
607 sigaction(SIG_IPI, &sigact, NULL);
608
609 pthread_sigmask(SIG_BLOCK, NULL, &set);
610 sigdelset(&set, SIG_IPI);
611 sigdelset(&set, SIGBUS);
612 r = kvm_set_signal_mask(env, &set);
613 if (r) {
614 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
615 exit(1);
616 }
617}
618
619static void qemu_tcg_init_cpu_signals(void)
620{
621 sigset_t set;
622 struct sigaction sigact;
623
624 memset(&sigact, 0, sizeof(sigact));
625 sigact.sa_handler = cpu_signal;
626 sigaction(SIG_IPI, &sigact, NULL);
627
628 sigemptyset(&set);
629 sigaddset(&set, SIG_IPI);
630 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
631}
632
55f8d6ac
JK
633static sigset_t block_io_signals(void)
634{
635 sigset_t set;
55f8d6ac
JK
636
637 /* SIGUSR2 used by posix-aio-compat.c */
638 sigemptyset(&set);
639 sigaddset(&set, SIGUSR2);
640 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
641
642 sigemptyset(&set);
643 sigaddset(&set, SIGIO);
644 sigaddset(&set, SIGALRM);
645 sigaddset(&set, SIG_IPI);
646 sigaddset(&set, SIGBUS);
647 pthread_sigmask(SIG_BLOCK, &set, NULL);
648
55f8d6ac
JK
649 return set;
650}
651
296af7c9
BS
652int qemu_init_main_loop(void)
653{
654 int ret;
a8486bc9 655 sigset_t blocked_signals;
296af7c9 656
3c638d06
JK
657 cpu_set_debug_excp_handler(cpu_debug_handler);
658
6d9cb73c
JK
659 qemu_init_sigbus();
660
a8486bc9
MT
661 blocked_signals = block_io_signals();
662
663 ret = qemu_signalfd_init(blocked_signals);
0ab07c62 664 if (ret) {
a8486bc9 665 return ret;
0ab07c62 666 }
a8486bc9
MT
667
668 /* Note eventfd must be drained before signalfd handlers run */
296af7c9 669 ret = qemu_event_init();
0ab07c62 670 if (ret) {
296af7c9 671 return ret;
0ab07c62 672 }
296af7c9
BS
673
674 qemu_cond_init(&qemu_pause_cond);
f8ca7b43 675 qemu_cond_init(&qemu_system_cond);
296af7c9
BS
676 qemu_mutex_init(&qemu_fair_mutex);
677 qemu_mutex_init(&qemu_global_mutex);
678 qemu_mutex_lock(&qemu_global_mutex);
679
296af7c9
BS
680 qemu_thread_self(&io_thread);
681
682 return 0;
683}
684
7277e027
BS
685void qemu_main_loop_start(void)
686{
687 qemu_system_ready = 1;
688 qemu_cond_broadcast(&qemu_system_cond);
689}
690
e82bcec2
MT
691void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
692{
693 struct qemu_work_item wi;
694
695 if (qemu_cpu_self(env)) {
696 func(data);
697 return;
698 }
699
700 wi.func = func;
701 wi.data = data;
0ab07c62 702 if (!env->queued_work_first) {
e82bcec2 703 env->queued_work_first = &wi;
0ab07c62 704 } else {
e82bcec2 705 env->queued_work_last->next = &wi;
0ab07c62 706 }
e82bcec2
MT
707 env->queued_work_last = &wi;
708 wi.next = NULL;
709 wi.done = false;
710
711 qemu_cpu_kick(env);
712 while (!wi.done) {
713 CPUState *self_env = cpu_single_env;
714
715 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
716 cpu_single_env = self_env;
717 }
718}
719
720static void flush_queued_work(CPUState *env)
721{
722 struct qemu_work_item *wi;
723
0ab07c62 724 if (!env->queued_work_first) {
e82bcec2 725 return;
0ab07c62 726 }
e82bcec2
MT
727
728 while ((wi = env->queued_work_first)) {
729 env->queued_work_first = wi->next;
730 wi->func(wi->data);
731 wi->done = true;
732 }
733 env->queued_work_last = NULL;
734 qemu_cond_broadcast(&qemu_work_cond);
735}
736
296af7c9
BS
737static void qemu_wait_io_event_common(CPUState *env)
738{
739 if (env->stop) {
740 env->stop = 0;
741 env->stopped = 1;
742 qemu_cond_signal(&qemu_pause_cond);
743 }
e82bcec2 744 flush_queued_work(env);
aa2c364b 745 env->thread_kicked = false;
296af7c9
BS
746}
747
6cabe1f3 748static void qemu_tcg_wait_io_event(void)
296af7c9 749{
6cabe1f3
JK
750 CPUState *env;
751
16400322 752 while (all_cpu_threads_idle()) {
6cabe1f3 753 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
16400322 754 }
296af7c9
BS
755
756 qemu_mutex_unlock(&qemu_global_mutex);
757
758 /*
759 * Users of qemu_global_mutex can be starved, having no chance
760 * to acquire it since this path will get to it first.
761 * So use another lock to provide fairness.
762 */
763 qemu_mutex_lock(&qemu_fair_mutex);
764 qemu_mutex_unlock(&qemu_fair_mutex);
765
766 qemu_mutex_lock(&qemu_global_mutex);
6cabe1f3
JK
767
768 for (env = first_cpu; env != NULL; env = env->next_cpu) {
769 qemu_wait_io_event_common(env);
770 }
296af7c9
BS
771}
772
296af7c9
BS
773static void qemu_kvm_wait_io_event(CPUState *env)
774{
16400322 775 while (cpu_thread_is_idle(env)) {
296af7c9 776 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
16400322 777 }
296af7c9 778
5db5bdac 779 qemu_kvm_eat_signals(env);
296af7c9
BS
780 qemu_wait_io_event_common(env);
781}
782
783static int qemu_cpu_exec(CPUState *env);
784
7e97cd88 785static void *qemu_kvm_cpu_thread_fn(void *arg)
296af7c9
BS
786{
787 CPUState *env = arg;
84b4915d 788 int r;
296af7c9 789
6164e6d6 790 qemu_mutex_lock(&qemu_global_mutex);
296af7c9 791 qemu_thread_self(env->thread);
d31ae052 792
84b4915d
JK
793 r = kvm_init_vcpu(env);
794 if (r < 0) {
795 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
796 exit(1);
797 }
296af7c9 798
55f8d6ac 799 qemu_kvm_init_cpu_signals(env);
296af7c9
BS
800
801 /* signal CPU creation */
296af7c9
BS
802 env->created = 1;
803 qemu_cond_signal(&qemu_cpu_cond);
804
805 /* and wait for machine initialization */
0ab07c62 806 while (!qemu_system_ready) {
296af7c9 807 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
0ab07c62 808 }
296af7c9
BS
809
810 while (1) {
0ab07c62 811 if (cpu_can_run(env)) {
296af7c9 812 qemu_cpu_exec(env);
0ab07c62 813 }
296af7c9
BS
814 qemu_kvm_wait_io_event(env);
815 }
816
817 return NULL;
818}
819
7e97cd88 820static void *qemu_tcg_cpu_thread_fn(void *arg)
296af7c9
BS
821{
822 CPUState *env = arg;
823
55f8d6ac 824 qemu_tcg_init_cpu_signals();
296af7c9
BS
825 qemu_thread_self(env->thread);
826
827 /* signal CPU creation */
828 qemu_mutex_lock(&qemu_global_mutex);
0ab07c62 829 for (env = first_cpu; env != NULL; env = env->next_cpu) {
296af7c9 830 env->created = 1;
0ab07c62 831 }
296af7c9
BS
832 qemu_cond_signal(&qemu_cpu_cond);
833
834 /* and wait for machine initialization */
0ab07c62 835 while (!qemu_system_ready) {
296af7c9 836 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
0ab07c62 837 }
296af7c9
BS
838
839 while (1) {
472fb0c4 840 cpu_exec_all();
6cabe1f3 841 qemu_tcg_wait_io_event();
296af7c9
BS
842 }
843
844 return NULL;
845}
846
847void qemu_cpu_kick(void *_env)
848{
849 CPUState *env = _env;
0ab07c62 850
296af7c9 851 qemu_cond_broadcast(env->halt_cond);
aa2c364b
JK
852 if (!env->thread_kicked) {
853 qemu_thread_signal(env->thread, SIG_IPI);
854 env->thread_kicked = true;
855 }
296af7c9
BS
856}
857
46d62fac
JK
858void qemu_cpu_kick_self(void)
859{
860 assert(cpu_single_env);
861
862 if (!cpu_single_env->thread_kicked) {
863 qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
864 cpu_single_env->thread_kicked = true;
865 }
866}
867
296af7c9
BS
868int qemu_cpu_self(void *_env)
869{
870 CPUState *env = _env;
871 QemuThread this;
872
873 qemu_thread_self(&this);
874
875 return qemu_thread_equal(&this, env->thread);
876}
877
296af7c9
BS
878void qemu_mutex_lock_iothread(void)
879{
880 if (kvm_enabled()) {
296af7c9 881 qemu_mutex_lock(&qemu_global_mutex);
1a28cac3
MT
882 } else {
883 qemu_mutex_lock(&qemu_fair_mutex);
884 if (qemu_mutex_trylock(&qemu_global_mutex)) {
885 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
886 qemu_mutex_lock(&qemu_global_mutex);
887 }
888 qemu_mutex_unlock(&qemu_fair_mutex);
889 }
296af7c9
BS
890}
891
892void qemu_mutex_unlock_iothread(void)
893{
894 qemu_mutex_unlock(&qemu_global_mutex);
895}
896
897static int all_vcpus_paused(void)
898{
899 CPUState *penv = first_cpu;
900
901 while (penv) {
0ab07c62 902 if (!penv->stopped) {
296af7c9 903 return 0;
0ab07c62 904 }
296af7c9
BS
905 penv = (CPUState *)penv->next_cpu;
906 }
907
908 return 1;
909}
910
911void pause_all_vcpus(void)
912{
913 CPUState *penv = first_cpu;
914
915 while (penv) {
916 penv->stop = 1;
296af7c9
BS
917 qemu_cpu_kick(penv);
918 penv = (CPUState *)penv->next_cpu;
919 }
920
921 while (!all_vcpus_paused()) {
922 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
923 penv = first_cpu;
924 while (penv) {
1fbb22e5 925 qemu_cpu_kick(penv);
296af7c9
BS
926 penv = (CPUState *)penv->next_cpu;
927 }
928 }
929}
930
931void resume_all_vcpus(void)
932{
933 CPUState *penv = first_cpu;
934
935 while (penv) {
936 penv->stop = 0;
937 penv->stopped = 0;
296af7c9
BS
938 qemu_cpu_kick(penv);
939 penv = (CPUState *)penv->next_cpu;
940 }
941}
942
7e97cd88 943static void qemu_tcg_init_vcpu(void *_env)
296af7c9
BS
944{
945 CPUState *env = _env;
0ab07c62 946
296af7c9
BS
947 /* share a single thread for all cpus with TCG */
948 if (!tcg_cpu_thread) {
949 env->thread = qemu_mallocz(sizeof(QemuThread));
950 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
951 qemu_cond_init(env->halt_cond);
7e97cd88 952 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
0ab07c62 953 while (env->created == 0) {
296af7c9 954 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
0ab07c62 955 }
296af7c9
BS
956 tcg_cpu_thread = env->thread;
957 tcg_halt_cond = env->halt_cond;
958 } else {
959 env->thread = tcg_cpu_thread;
960 env->halt_cond = tcg_halt_cond;
961 }
962}
963
7e97cd88 964static void qemu_kvm_start_vcpu(CPUState *env)
296af7c9
BS
965{
966 env->thread = qemu_mallocz(sizeof(QemuThread));
967 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
968 qemu_cond_init(env->halt_cond);
7e97cd88 969 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
0ab07c62 970 while (env->created == 0) {
296af7c9 971 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
0ab07c62 972 }
296af7c9
BS
973}
974
975void qemu_init_vcpu(void *_env)
976{
977 CPUState *env = _env;
978
979 env->nr_cores = smp_cores;
980 env->nr_threads = smp_threads;
0ab07c62 981 if (kvm_enabled()) {
7e97cd88 982 qemu_kvm_start_vcpu(env);
0ab07c62 983 } else {
7e97cd88 984 qemu_tcg_init_vcpu(env);
0ab07c62 985 }
296af7c9
BS
986}
987
988void qemu_notify_event(void)
989{
990 qemu_event_increment();
991}
992
993static void qemu_system_vmstop_request(int reason)
994{
995 vmstop_requested = reason;
996 qemu_notify_event();
997}
998
b4a3d965
JK
999void cpu_stop_current(void)
1000{
1001 if (cpu_single_env) {
1002 cpu_single_env->stopped = 1;
1003 cpu_exit(cpu_single_env);
1004 }
1005}
1006
296af7c9
BS
1007void vm_stop(int reason)
1008{
1009 QemuThread me;
1010 qemu_thread_self(&me);
1011
1012 if (!qemu_thread_equal(&me, &io_thread)) {
1013 qemu_system_vmstop_request(reason);
1014 /*
1015 * FIXME: should not return to device code in case
1016 * vm_stop() has been requested.
1017 */
b4a3d965 1018 cpu_stop_current();
296af7c9
BS
1019 return;
1020 }
1021 do_vm_stop(reason);
1022}
1023
1024#endif
1025
1026static int qemu_cpu_exec(CPUState *env)
1027{
1028 int ret;
1029#ifdef CONFIG_PROFILER
1030 int64_t ti;
1031#endif
1032
1033#ifdef CONFIG_PROFILER
1034 ti = profile_getclock();
1035#endif
1036 if (use_icount) {
1037 int64_t count;
1038 int decr;
1039 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1040 env->icount_decr.u16.low = 0;
1041 env->icount_extra = 0;
1042 count = qemu_icount_round (qemu_next_deadline());
1043 qemu_icount += count;
1044 decr = (count > 0xffff) ? 0xffff : count;
1045 count -= decr;
1046 env->icount_decr.u16.low = decr;
1047 env->icount_extra = count;
1048 }
1049 ret = cpu_exec(env);
1050#ifdef CONFIG_PROFILER
1051 qemu_time += profile_getclock() - ti;
1052#endif
1053 if (use_icount) {
1054 /* Fold pending instructions back into the
1055 instruction counter, and clear the interrupt flag. */
1056 qemu_icount -= (env->icount_decr.u16.low
1057 + env->icount_extra);
1058 env->icount_decr.u32 = 0;
1059 env->icount_extra = 0;
1060 }
1061 return ret;
1062}
1063
472fb0c4 1064bool cpu_exec_all(void)
296af7c9 1065{
9a36085b
JK
1066 int r;
1067
0ab07c62 1068 if (next_cpu == NULL) {
296af7c9 1069 next_cpu = first_cpu;
0ab07c62 1070 }
c629a4bc 1071 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
345f4426 1072 CPUState *env = next_cpu;
296af7c9
BS
1073
1074 qemu_clock_enable(vm_clock,
345f4426 1075 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
296af7c9 1076
0ab07c62 1077 if (qemu_alarm_pending()) {
296af7c9 1078 break;
0ab07c62 1079 }
3c638d06 1080 if (cpu_can_run(env)) {
9a36085b
JK
1081 r = qemu_cpu_exec(env);
1082 if (kvm_enabled()) {
1083 qemu_kvm_eat_signals(env);
1084 }
1085 if (r == EXCP_DEBUG) {
3c638d06
JK
1086 break;
1087 }
1088 } else if (env->stop) {
296af7c9
BS
1089 break;
1090 }
1091 }
c629a4bc 1092 exit_request = 0;
16400322 1093 return !all_cpu_threads_idle();
296af7c9
BS
1094}
1095
1096void set_numa_modes(void)
1097{
1098 CPUState *env;
1099 int i;
1100
1101 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1102 for (i = 0; i < nb_numa_nodes; i++) {
1103 if (node_cpumask[i] & (1 << env->cpu_index)) {
1104 env->numa_node = i;
1105 }
1106 }
1107 }
1108}
1109
1110void set_cpu_log(const char *optarg)
1111{
1112 int mask;
1113 const CPULogItem *item;
1114
1115 mask = cpu_str_to_log_mask(optarg);
1116 if (!mask) {
1117 printf("Log items (comma separated):\n");
1118 for (item = cpu_log_items; item->mask != 0; item++) {
1119 printf("%-10s %s\n", item->name, item->help);
1120 }
1121 exit(1);
1122 }
1123 cpu_set_log(mask);
1124}
29e922b6
BS
1125
1126/* Return the virtual CPU time, based on the instruction counter. */
1127int64_t cpu_get_icount(void)
1128{
1129 int64_t icount;
1130 CPUState *env = cpu_single_env;;
1131
1132 icount = qemu_icount;
1133 if (env) {
1134 if (!can_do_io(env)) {
1135 fprintf(stderr, "Bad clock read\n");
1136 }
1137 icount -= (env->icount_decr.u16.low + env->icount_extra);
1138 }
1139 return qemu_icount_bias + (icount << icount_time_shift);
1140}
262353cb 1141
9a78eead 1142void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
262353cb
BS
1143{
1144 /* XXX: implement xxx_cpu_list for targets that still miss it */
1145#if defined(cpu_list_id)
1146 cpu_list_id(f, cpu_fprintf, optarg);
1147#elif defined(cpu_list)
1148 cpu_list(f, cpu_fprintf); /* deprecated */
1149#endif
1150}