blob: b163efe4a88f6eb56fc9aeb4511a272449475e6b [file] [log] [blame]
Blue Swirl296af7c2010-03-29 19:23:50 +00001/*
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
Paolo Bonzini96284e82011-03-12 17:43:53 +010034#include "qemu-thread.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000035#include "cpus.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020036
37#ifndef _WIN32
Marcelo Tosattia8486bc2010-10-11 15:31:16 -030038#include "compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020039#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000040
Blue Swirl7277e022010-04-12 17:19:06 +000041#ifdef SIGRTMIN
42#define SIG_IPI (SIGRTMIN+4)
43#else
44#define SIG_IPI SIGUSR1
45#endif
46
Jan Kiszka6d9cb732011-02-01 22:15:58 +010047#ifdef CONFIG_LINUX
48
49#include <sys/prctl.h>
50
Marcelo Tosattic0532a72010-10-11 15:31:21 -030051#ifndef PR_MCE_KILL
52#define PR_MCE_KILL 33
53#endif
54
Jan Kiszka6d9cb732011-02-01 22:15:58 +010055#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
Blue Swirl296af7c2010-03-29 19:23:50 +000065static 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
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300116int cpu_is_stopped(CPUState *env)
117{
118 return !vm_running || env->stopped;
119}
120
Blue Swirl296af7c2010-03-29 19:23:50 +0000121static 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);
Michael S. Tsirkin55df6f32010-11-22 19:52:22 +0200128 qemu_aio_flush();
129 bdrv_flush_all();
Blue Swirl296af7c2010-03-29 19:23:50 +0000130 monitor_protocol_event(QEVENT_STOP, NULL);
131 }
132}
133
134static int cpu_can_run(CPUState *env)
135{
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100136 if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000137 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100138 }
139 if (env->stopped || !vm_running) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000140 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100141 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000142 return 1;
143}
144
Jan Kiszka16400322011-02-09 16:29:37 +0100145static bool cpu_thread_is_idle(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000146{
Jan Kiszka16400322011-02-09 16:29:37 +0100147 if (env->stop || env->queued_work_first) {
148 return false;
149 }
150 if (env->stopped || !vm_running) {
151 return true;
152 }
Jan Kiszkaf2c1cc82011-03-15 12:26:18 +0100153 if (!env->halted || qemu_cpu_has_work(env) ||
154 (kvm_enabled() && kvm_irqchip_in_kernel())) {
Jan Kiszka16400322011-02-09 16:29:37 +0100155 return false;
156 }
157 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000158}
159
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200160bool all_cpu_threads_idle(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000161{
162 CPUState *env;
163
Jan Kiszka16400322011-02-09 16:29:37 +0100164 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;
Blue Swirl296af7c2010-03-29 19:23:50 +0000170}
171
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100172static void cpu_handle_guest_debug(CPUState *env)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200173{
174 gdb_set_stop_cpu(env);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100175 qemu_system_debug_request();
Jan Kiszka83f338f2011-02-07 12:19:17 +0100176#ifdef CONFIG_IOTHREAD
177 env->stopped = 1;
178#endif
Jan Kiszka3c638d02010-06-25 16:56:56 +0200179}
180
Paolo Bonzini714bd042011-03-12 17:44:06 +0100181#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
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100191#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
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100230static 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
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100273#else /* !CONFIG_LINUX */
274
275static void qemu_init_sigbus(void)
276{
277}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100278
279static void qemu_kvm_eat_signals(CPUState *env)
280{
281}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100282#endif /* !CONFIG_LINUX */
283
Blue Swirl296af7c2010-03-29 19:23:50 +0000284#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. */
Blue Swirl26a82332010-05-14 19:32:21 +0000290 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000291 ssize_t ret;
292
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100293 if (io_thread_fd == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000294 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100295 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000296 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) {
Alexandre Raymond77bec682011-06-13 22:16:36 -0400302 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
Blue Swirl296af7c2010-03-29 19:23:50 +0000303 strerror(errno));
304 exit (1);
305 }
306}
307
308static void qemu_event_read(void *opaque)
309{
Stefan Weile0efb992011-02-23 19:09:16 +0100310 int fd = (intptr_t)opaque;
Blue Swirl296af7c2010-03-29 19:23:50 +0000311 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);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100326 if (err == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000327 return -errno;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100328 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000329 err = fcntl_setfl(fds[0], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100330 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000331 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100332 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000333 err = fcntl_setfl(fds[1], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100334 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000335 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100336 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000337 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100338 (void *)(intptr_t)fds[0]);
Blue Swirl296af7c2010-03-29 19:23:50 +0000339
340 io_thread_fd = fds[1];
341 return 0;
342
343fail:
344 close(fds[0]);
345 close(fds[1]);
346 return err;
347}
Blue Swirl296af7c2010-03-29 19:23:50 +0000348
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100349static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000350{
351}
352
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300353/* 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{
Stefan Weile0efb992011-02-23 19:09:16 +0100359 int fd = (intptr_t)opaque;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300360 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
Paolo Bonzini712ae482011-03-12 17:44:05 +0100388static int qemu_signal_init(void)
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300389{
390 int sigfd;
Paolo Bonzini712ae482011-03-12 17:44:05 +0100391 sigset_t set;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300392
Paolo Bonzini712ae482011-03-12 17:44:05 +0100393#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
Alexandre Raymond89b9ba62011-06-15 01:20:31 -0400399 /*
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
Paolo Bonzini712ae482011-03-12 17:44:05 +0100408 sigemptyset(&set);
409 sigaddset(&set, SIGIO);
410 sigaddset(&set, SIGALRM);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100411 sigaddset(&set, SIGBUS);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100412#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
Alexandre Raymond5664aed2011-06-14 10:05:36 -0400424 pthread_sigmask(SIG_BLOCK, &set, NULL);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100425
426 sigfd = qemu_signalfd(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300427 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,
Stefan Weile0efb992011-02-23 19:09:16 +0100435 (void *)(intptr_t)sigfd);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300436
437 return 0;
438}
Blue Swirl296af7c2010-03-29 19:23:50 +0000439
Paolo Bonzini714bd042011-03-12 17:44:06 +0100440static 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
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100495#else /* _WIN32 */
496
Blue Swirl296af7c2010-03-29 19:23:50 +0000497HANDLE 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}
Jan Kiszka9a360852011-02-01 22:15:55 +0100522
Paolo Bonzini712ae482011-03-12 17:44:05 +0100523static int qemu_signal_init(void)
524{
525 return 0;
526}
527
Paolo Bonzini714bd042011-03-12 17:44:06 +0100528static void qemu_kvm_init_cpu_signals(CPUState *env)
529{
530 abort();
531}
532
533static void qemu_tcg_init_cpu_signals(void)
534{
535}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100536#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000537
538#ifndef CONFIG_IOTHREAD
539int qemu_init_main_loop(void)
540{
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100541 int ret;
542
Paolo Bonzini712ae482011-03-12 17:44:05 +0100543 ret = qemu_signal_init();
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100544 if (ret) {
545 return ret;
546 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000547
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100548 qemu_init_sigbus();
549
Blue Swirl296af7c2010-03-29 19:23:50 +0000550 return qemu_event_init();
551}
552
553void qemu_main_loop_start(void)
554{
555}
556
557void qemu_init_vcpu(void *_env)
558{
559 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100560 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000561
562 env->nr_cores = smp_cores;
563 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100564
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 }
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100571 qemu_kvm_init_cpu_signals(env);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100572 } else {
573 qemu_tcg_init_cpu_signals();
Jan Kiszka84b49152011-02-01 22:15:50 +0100574 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000575}
576
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100577int qemu_cpu_is_self(void *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000578{
579 return 1;
580}
581
582void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
583{
584 func(data);
585}
586
587void resume_all_vcpus(void)
588{
589}
590
591void pause_all_vcpus(void)
592{
593}
594
595void qemu_cpu_kick(void *env)
596{
Blue Swirl296af7c2010-03-29 19:23:50 +0000597}
598
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100599void 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
608}
609
Blue Swirl296af7c2010-03-29 19:23:50 +0000610void 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 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100621 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000622}
623
624void qemu_mutex_lock_iothread(void) {}
625void qemu_mutex_unlock_iothread(void) {}
626
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100627void cpu_stop_current(void)
628{
629}
630
Blue Swirl296af7c2010-03-29 19:23:50 +0000631void vm_stop(int reason)
632{
633 do_vm_stop(reason);
634}
635
636#else /* CONFIG_IOTHREAD */
637
Blue Swirl296af7c2010-03-29 19:23:50 +0000638QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200639static QemuCond qemu_io_proceeded_cond;
640static bool iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000641
642static QemuThread io_thread;
643
644static QemuThread *tcg_cpu_thread;
645static QemuCond *tcg_halt_cond;
646
Blue Swirl296af7c2010-03-29 19:23:50 +0000647/* cpu creation */
648static QemuCond qemu_cpu_cond;
649/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000650static QemuCond qemu_pause_cond;
651static QemuCond qemu_work_cond;
652
Blue Swirl296af7c2010-03-29 19:23:50 +0000653int qemu_init_main_loop(void)
654{
655 int ret;
656
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100657 qemu_init_sigbus();
Jan Kiszka3c638d02010-06-25 16:56:56 +0200658
Paolo Bonzini712ae482011-03-12 17:44:05 +0100659 ret = qemu_signal_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100660 if (ret) {
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300661 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100662 }
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300663
664 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000665 ret = qemu_event_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100666 if (ret) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000667 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100668 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000669
Anthony Liguoried945922011-02-08 18:18:18 +0100670 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100671 qemu_cond_init(&qemu_pause_cond);
672 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200673 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000674 qemu_mutex_init(&qemu_global_mutex);
675 qemu_mutex_lock(&qemu_global_mutex);
676
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100677 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000678
679 return 0;
680}
681
Blue Swirl7277e022010-04-12 17:19:06 +0000682void qemu_main_loop_start(void)
683{
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200684 resume_all_vcpus();
Blue Swirl7277e022010-04-12 17:19:06 +0000685}
686
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300687void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
688{
689 struct qemu_work_item wi;
690
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100691 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300692 func(data);
693 return;
694 }
695
696 wi.func = func;
697 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100698 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300699 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100700 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300701 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100702 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300703 env->queued_work_last = &wi;
704 wi.next = NULL;
705 wi.done = false;
706
707 qemu_cpu_kick(env);
708 while (!wi.done) {
709 CPUState *self_env = cpu_single_env;
710
711 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
712 cpu_single_env = self_env;
713 }
714}
715
716static void flush_queued_work(CPUState *env)
717{
718 struct qemu_work_item *wi;
719
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100720 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300721 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100722 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300723
724 while ((wi = env->queued_work_first)) {
725 env->queued_work_first = wi->next;
726 wi->func(wi->data);
727 wi->done = true;
728 }
729 env->queued_work_last = NULL;
730 qemu_cond_broadcast(&qemu_work_cond);
731}
732
Blue Swirl296af7c2010-03-29 19:23:50 +0000733static void qemu_wait_io_event_common(CPUState *env)
734{
735 if (env->stop) {
736 env->stop = 0;
737 env->stopped = 1;
738 qemu_cond_signal(&qemu_pause_cond);
739 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300740 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100741 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000742}
743
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200744static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000745{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200746 CPUState *env;
747
Jan Kiszka16400322011-02-09 16:29:37 +0100748 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200749 /* Start accounting real time to the virtual clock if the CPUs
750 are idle. */
751 qemu_clock_warp(vm_clock);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100752 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100753 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000754
Paolo Bonzini46daff12011-06-09 13:10:24 +0200755 while (iothread_requesting_mutex) {
756 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
757 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200758
759 for (env = first_cpu; env != NULL; env = env->next_cpu) {
760 qemu_wait_io_event_common(env);
761 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000762}
763
Blue Swirl296af7c2010-03-29 19:23:50 +0000764static void qemu_kvm_wait_io_event(CPUState *env)
765{
Jan Kiszka16400322011-02-09 16:29:37 +0100766 while (cpu_thread_is_idle(env)) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100767 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100768 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000769
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100770 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000771 qemu_wait_io_event_common(env);
772}
773
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100774static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000775{
776 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100777 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000778
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300779 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100780 qemu_thread_get_self(env->thread);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100781 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000782
Jan Kiszka84b49152011-02-01 22:15:50 +0100783 r = kvm_init_vcpu(env);
784 if (r < 0) {
785 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
786 exit(1);
787 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000788
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100789 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000790
791 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000792 env->created = 1;
793 qemu_cond_signal(&qemu_cpu_cond);
794
Blue Swirl296af7c2010-03-29 19:23:50 +0000795 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100796 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100797 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100798 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100799 cpu_handle_guest_debug(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100800 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100801 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000802 qemu_kvm_wait_io_event(env);
803 }
804
805 return NULL;
806}
807
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100808static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000809{
810 CPUState *env = arg;
811
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100812 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100813 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000814
815 /* signal CPU creation */
816 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100817 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100818 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000819 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100820 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000821 qemu_cond_signal(&qemu_cpu_cond);
822
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200823 /* wait for initial kick-off after machine start */
824 while (first_cpu->stopped) {
825 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100826 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000827
828 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200829 cpu_exec_all();
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200830 if (use_icount && qemu_next_icount_deadline() <= 0) {
Paolo Bonzini3b2319a2011-04-13 10:03:43 +0200831 qemu_notify_event();
832 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200833 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000834 }
835
836 return NULL;
837}
838
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100839static void qemu_cpu_kick_thread(CPUState *env)
840{
841#ifndef _WIN32
842 int err;
843
844 err = pthread_kill(env->thread->thread, SIG_IPI);
845 if (err) {
846 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
847 exit(1);
848 }
849#else /* _WIN32 */
850 if (!qemu_cpu_is_self(env)) {
851 SuspendThread(env->thread->thread);
852 cpu_signal(0);
853 ResumeThread(env->thread->thread);
854 }
855#endif
856}
857
Blue Swirl296af7c2010-03-29 19:23:50 +0000858void qemu_cpu_kick(void *_env)
859{
860 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100861
Blue Swirl296af7c2010-03-29 19:23:50 +0000862 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaeae74cf2011-08-22 17:46:03 +0200863 if (kvm_enabled() && !env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100864 qemu_cpu_kick_thread(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100865 env->thread_kicked = true;
866 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000867}
868
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100869void qemu_cpu_kick_self(void)
870{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100871#ifndef _WIN32
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100872 assert(cpu_single_env);
873
874 if (!cpu_single_env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100875 qemu_cpu_kick_thread(cpu_single_env);
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100876 cpu_single_env->thread_kicked = true;
877 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100878#else
879 abort();
880#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000881}
882
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100883int qemu_cpu_is_self(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000884{
885 CPUState *env = _env;
Blue Swirl296af7c2010-03-29 19:23:50 +0000886
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100887 return qemu_thread_is_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000888}
889
Blue Swirl296af7c2010-03-29 19:23:50 +0000890void qemu_mutex_lock_iothread(void)
891{
892 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000893 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300894 } else {
Paolo Bonzini46daff12011-06-09 13:10:24 +0200895 iothread_requesting_mutex = true;
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300896 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100897 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300898 qemu_mutex_lock(&qemu_global_mutex);
899 }
Paolo Bonzini46daff12011-06-09 13:10:24 +0200900 iothread_requesting_mutex = false;
901 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300902 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000903}
904
905void qemu_mutex_unlock_iothread(void)
906{
907 qemu_mutex_unlock(&qemu_global_mutex);
908}
909
910static int all_vcpus_paused(void)
911{
912 CPUState *penv = first_cpu;
913
914 while (penv) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100915 if (!penv->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000916 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100917 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000918 penv = (CPUState *)penv->next_cpu;
919 }
920
921 return 1;
922}
923
924void pause_all_vcpus(void)
925{
926 CPUState *penv = first_cpu;
927
928 while (penv) {
929 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000930 qemu_cpu_kick(penv);
931 penv = (CPUState *)penv->next_cpu;
932 }
933
934 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100935 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000936 penv = first_cpu;
937 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300938 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000939 penv = (CPUState *)penv->next_cpu;
940 }
941 }
942}
943
944void resume_all_vcpus(void)
945{
946 CPUState *penv = first_cpu;
947
948 while (penv) {
949 penv->stop = 0;
950 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000951 qemu_cpu_kick(penv);
952 penv = (CPUState *)penv->next_cpu;
953 }
954}
955
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100956static void qemu_tcg_init_vcpu(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000957{
958 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100959
Blue Swirl296af7c2010-03-29 19:23:50 +0000960 /* share a single thread for all cpus with TCG */
961 if (!tcg_cpu_thread) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500962 env->thread = g_malloc0(sizeof(QemuThread));
963 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +0000964 qemu_cond_init(env->halt_cond);
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200965 tcg_halt_cond = env->halt_cond;
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100966 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100967 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100968 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100969 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000970 tcg_cpu_thread = env->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +0000971 } else {
972 env->thread = tcg_cpu_thread;
973 env->halt_cond = tcg_halt_cond;
974 }
975}
976
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100977static void qemu_kvm_start_vcpu(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000978{
Anthony Liguori7267c092011-08-20 22:09:37 -0500979 env->thread = g_malloc0(sizeof(QemuThread));
980 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +0000981 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100982 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100983 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100984 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100985 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000986}
987
988void qemu_init_vcpu(void *_env)
989{
990 CPUState *env = _env;
991
992 env->nr_cores = smp_cores;
993 env->nr_threads = smp_threads;
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200994 env->stopped = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100995 if (kvm_enabled()) {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100996 qemu_kvm_start_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100997 } else {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100998 qemu_tcg_init_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100999 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001000}
1001
1002void qemu_notify_event(void)
1003{
1004 qemu_event_increment();
1005}
1006
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001007void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001008{
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001009 if (cpu_single_env) {
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001010 cpu_single_env->stop = 0;
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001011 cpu_single_env->stopped = 1;
1012 cpu_exit(cpu_single_env);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001013 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001014 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001015}
1016
1017void vm_stop(int reason)
1018{
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001019 if (!qemu_thread_is_self(&io_thread)) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001020 qemu_system_vmstop_request(reason);
1021 /*
1022 * FIXME: should not return to device code in case
1023 * vm_stop() has been requested.
1024 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001025 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +00001026 return;
1027 }
1028 do_vm_stop(reason);
1029}
1030
1031#endif
1032
Jan Kiszka6792a572011-02-07 12:19:18 +01001033static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001034{
1035 int ret;
1036#ifdef CONFIG_PROFILER
1037 int64_t ti;
1038#endif
1039
1040#ifdef CONFIG_PROFILER
1041 ti = profile_getclock();
1042#endif
1043 if (use_icount) {
1044 int64_t count;
1045 int decr;
1046 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1047 env->icount_decr.u16.low = 0;
1048 env->icount_extra = 0;
Paolo Bonzinicb842c92011-04-13 10:03:46 +02001049 count = qemu_icount_round(qemu_next_icount_deadline());
Blue Swirl296af7c2010-03-29 19:23:50 +00001050 qemu_icount += count;
1051 decr = (count > 0xffff) ? 0xffff : count;
1052 count -= decr;
1053 env->icount_decr.u16.low = decr;
1054 env->icount_extra = count;
1055 }
1056 ret = cpu_exec(env);
1057#ifdef CONFIG_PROFILER
1058 qemu_time += profile_getclock() - ti;
1059#endif
1060 if (use_icount) {
1061 /* Fold pending instructions back into the
1062 instruction counter, and clear the interrupt flag. */
1063 qemu_icount -= (env->icount_decr.u16.low
1064 + env->icount_extra);
1065 env->icount_decr.u32 = 0;
1066 env->icount_extra = 0;
1067 }
1068 return ret;
1069}
1070
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001071bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001072{
Jan Kiszka9a360852011-02-01 22:15:55 +01001073 int r;
1074
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001075 /* Account partial waits to the vm_clock. */
1076 qemu_clock_warp(vm_clock);
1077
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001078 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001079 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001080 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001081 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +02001082 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001083
1084 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +02001085 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001086
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001087#ifndef CONFIG_IOTHREAD
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001088 if (qemu_alarm_pending()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001089 break;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001090 }
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001091#endif
Jan Kiszka3c638d02010-06-25 16:56:56 +02001092 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +01001093 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +01001094 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001095 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +01001096 } else {
1097 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001098 }
1099 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +01001100 cpu_handle_guest_debug(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001101 break;
1102 }
Paolo Bonzinidf646df2011-03-12 17:43:58 +01001103 } else if (env->stop || env->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001104 break;
1105 }
1106 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001107 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +01001108 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +00001109}
1110
1111void set_numa_modes(void)
1112{
1113 CPUState *env;
1114 int i;
1115
1116 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1117 for (i = 0; i < nb_numa_nodes; i++) {
1118 if (node_cpumask[i] & (1 << env->cpu_index)) {
1119 env->numa_node = i;
1120 }
1121 }
1122 }
1123}
1124
1125void set_cpu_log(const char *optarg)
1126{
1127 int mask;
1128 const CPULogItem *item;
1129
1130 mask = cpu_str_to_log_mask(optarg);
1131 if (!mask) {
1132 printf("Log items (comma separated):\n");
1133 for (item = cpu_log_items; item->mask != 0; item++) {
1134 printf("%-10s %s\n", item->name, item->help);
1135 }
1136 exit(1);
1137 }
1138 cpu_set_log(mask);
1139}
Blue Swirl29e922b2010-03-29 19:24:00 +00001140
Matthew Fernandezc235d732011-06-07 16:32:40 +00001141void set_cpu_log_filename(const char *optarg)
1142{
1143 cpu_set_log_filename(optarg);
1144}
1145
Blue Swirl29e922b2010-03-29 19:24:00 +00001146/* Return the virtual CPU time, based on the instruction counter. */
1147int64_t cpu_get_icount(void)
1148{
1149 int64_t icount;
1150 CPUState *env = cpu_single_env;;
1151
1152 icount = qemu_icount;
1153 if (env) {
1154 if (!can_do_io(env)) {
1155 fprintf(stderr, "Bad clock read\n");
1156 }
1157 icount -= (env->icount_decr.u16.low + env->icount_extra);
1158 }
1159 return qemu_icount_bias + (icount << icount_time_shift);
1160}
Blue Swirl262353c2010-05-04 19:55:35 +00001161
Stefan Weil9a78eea2010-10-22 23:03:33 +02001162void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001163{
1164 /* XXX: implement xxx_cpu_list for targets that still miss it */
1165#if defined(cpu_list_id)
1166 cpu_list_id(f, cpu_fprintf, optarg);
1167#elif defined(cpu_list)
1168 cpu_list(f, cpu_fprintf); /* deprecated */
1169#endif
1170}