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