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