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