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