blob: e02576cb66f667eb445c097ee5162fca5fb3cef3 [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
399 sigemptyset(&set);
400 sigaddset(&set, SIGIO);
401 sigaddset(&set, SIGALRM);
402 sigaddset(&set, SIG_IPI);
403 sigaddset(&set, SIGBUS);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100404#else
405 sigemptyset(&set);
406 sigaddset(&set, SIGBUS);
407 if (kvm_enabled()) {
408 /*
409 * We need to process timer signals synchronously to avoid a race
410 * between exit_request check and KVM vcpu entry.
411 */
412 sigaddset(&set, SIGIO);
413 sigaddset(&set, SIGALRM);
414 }
415#endif
Alexandre Raymond5664aed2011-06-14 10:05:36 -0400416 pthread_sigmask(SIG_BLOCK, &set, NULL);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100417
418 sigfd = qemu_signalfd(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300419 if (sigfd == -1) {
420 fprintf(stderr, "failed to create signalfd\n");
421 return -errno;
422 }
423
424 fcntl_setfl(sigfd, O_NONBLOCK);
425
426 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100427 (void *)(intptr_t)sigfd);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300428
429 return 0;
430}
Blue Swirl296af7c2010-03-29 19:23:50 +0000431
Paolo Bonzini714bd042011-03-12 17:44:06 +0100432static void qemu_kvm_init_cpu_signals(CPUState *env)
433{
434 int r;
435 sigset_t set;
436 struct sigaction sigact;
437
438 memset(&sigact, 0, sizeof(sigact));
439 sigact.sa_handler = dummy_signal;
440 sigaction(SIG_IPI, &sigact, NULL);
441
442#ifdef CONFIG_IOTHREAD
443 pthread_sigmask(SIG_BLOCK, NULL, &set);
444 sigdelset(&set, SIG_IPI);
445 sigdelset(&set, SIGBUS);
446 r = kvm_set_signal_mask(env, &set);
447 if (r) {
448 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
449 exit(1);
450 }
451#else
452 sigemptyset(&set);
453 sigaddset(&set, SIG_IPI);
454 sigaddset(&set, SIGIO);
455 sigaddset(&set, SIGALRM);
456 pthread_sigmask(SIG_BLOCK, &set, NULL);
457
458 pthread_sigmask(SIG_BLOCK, NULL, &set);
459 sigdelset(&set, SIGIO);
460 sigdelset(&set, SIGALRM);
461#endif
462 sigdelset(&set, SIG_IPI);
463 sigdelset(&set, SIGBUS);
464 r = kvm_set_signal_mask(env, &set);
465 if (r) {
466 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
467 exit(1);
468 }
469}
470
471static void qemu_tcg_init_cpu_signals(void)
472{
473#ifdef CONFIG_IOTHREAD
474 sigset_t set;
475 struct sigaction sigact;
476
477 memset(&sigact, 0, sizeof(sigact));
478 sigact.sa_handler = cpu_signal;
479 sigaction(SIG_IPI, &sigact, NULL);
480
481 sigemptyset(&set);
482 sigaddset(&set, SIG_IPI);
483 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
484#endif
485}
486
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100487#else /* _WIN32 */
488
Blue Swirl296af7c2010-03-29 19:23:50 +0000489HANDLE qemu_event_handle;
490
491static void dummy_event_handler(void *opaque)
492{
493}
494
495static int qemu_event_init(void)
496{
497 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
498 if (!qemu_event_handle) {
499 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
500 return -1;
501 }
502 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
503 return 0;
504}
505
506static void qemu_event_increment(void)
507{
508 if (!SetEvent(qemu_event_handle)) {
509 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
510 GetLastError());
511 exit (1);
512 }
513}
Jan Kiszka9a360852011-02-01 22:15:55 +0100514
Paolo Bonzini712ae482011-03-12 17:44:05 +0100515static int qemu_signal_init(void)
516{
517 return 0;
518}
519
Paolo Bonzini714bd042011-03-12 17:44:06 +0100520static void qemu_kvm_init_cpu_signals(CPUState *env)
521{
522 abort();
523}
524
525static void qemu_tcg_init_cpu_signals(void)
526{
527}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100528#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000529
530#ifndef CONFIG_IOTHREAD
531int qemu_init_main_loop(void)
532{
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100533 int ret;
534
Paolo Bonzini712ae482011-03-12 17:44:05 +0100535 ret = qemu_signal_init();
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100536 if (ret) {
537 return ret;
538 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000539
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100540 qemu_init_sigbus();
541
Blue Swirl296af7c2010-03-29 19:23:50 +0000542 return qemu_event_init();
543}
544
545void qemu_main_loop_start(void)
546{
547}
548
549void qemu_init_vcpu(void *_env)
550{
551 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100552 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000553
554 env->nr_cores = smp_cores;
555 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100556
557 if (kvm_enabled()) {
558 r = kvm_init_vcpu(env);
559 if (r < 0) {
560 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
561 exit(1);
562 }
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100563 qemu_kvm_init_cpu_signals(env);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100564 } else {
565 qemu_tcg_init_cpu_signals();
Jan Kiszka84b49152011-02-01 22:15:50 +0100566 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000567}
568
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100569int qemu_cpu_is_self(void *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000570{
571 return 1;
572}
573
574void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
575{
576 func(data);
577}
578
579void resume_all_vcpus(void)
580{
581}
582
583void pause_all_vcpus(void)
584{
585}
586
587void qemu_cpu_kick(void *env)
588{
Blue Swirl296af7c2010-03-29 19:23:50 +0000589}
590
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100591void qemu_cpu_kick_self(void)
592{
593#ifndef _WIN32
594 assert(cpu_single_env);
595
596 raise(SIG_IPI);
597#else
598 abort();
599#endif
600}
601
Blue Swirl296af7c2010-03-29 19:23:50 +0000602void qemu_notify_event(void)
603{
604 CPUState *env = cpu_single_env;
605
606 qemu_event_increment ();
607 if (env) {
608 cpu_exit(env);
609 }
610 if (next_cpu && env != next_cpu) {
611 cpu_exit(next_cpu);
612 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100613 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000614}
615
616void qemu_mutex_lock_iothread(void) {}
617void qemu_mutex_unlock_iothread(void) {}
618
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100619void cpu_stop_current(void)
620{
621}
622
Blue Swirl296af7c2010-03-29 19:23:50 +0000623void vm_stop(int reason)
624{
625 do_vm_stop(reason);
626}
627
628#else /* CONFIG_IOTHREAD */
629
Blue Swirl296af7c2010-03-29 19:23:50 +0000630QemuMutex qemu_global_mutex;
631static QemuMutex qemu_fair_mutex;
632
633static QemuThread io_thread;
634
635static QemuThread *tcg_cpu_thread;
636static QemuCond *tcg_halt_cond;
637
638static int qemu_system_ready;
639/* cpu creation */
640static QemuCond qemu_cpu_cond;
641/* system init */
642static QemuCond qemu_system_cond;
643static QemuCond qemu_pause_cond;
644static QemuCond qemu_work_cond;
645
Blue Swirl296af7c2010-03-29 19:23:50 +0000646int qemu_init_main_loop(void)
647{
648 int ret;
649
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100650 qemu_init_sigbus();
Jan Kiszka3c638d02010-06-25 16:56:56 +0200651
Paolo Bonzini712ae482011-03-12 17:44:05 +0100652 ret = qemu_signal_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100653 if (ret) {
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300654 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100655 }
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300656
657 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000658 ret = qemu_event_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100659 if (ret) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000660 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100661 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000662
Anthony Liguoried945922011-02-08 18:18:18 +0100663 qemu_cond_init(&qemu_cpu_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200664 qemu_cond_init(&qemu_system_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100665 qemu_cond_init(&qemu_pause_cond);
666 qemu_cond_init(&qemu_work_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000667 qemu_mutex_init(&qemu_fair_mutex);
668 qemu_mutex_init(&qemu_global_mutex);
669 qemu_mutex_lock(&qemu_global_mutex);
670
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100671 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000672
673 return 0;
674}
675
Blue Swirl7277e022010-04-12 17:19:06 +0000676void qemu_main_loop_start(void)
677{
678 qemu_system_ready = 1;
679 qemu_cond_broadcast(&qemu_system_cond);
680}
681
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300682void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
683{
684 struct qemu_work_item wi;
685
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100686 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300687 func(data);
688 return;
689 }
690
691 wi.func = func;
692 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100693 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300694 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100695 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300696 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100697 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300698 env->queued_work_last = &wi;
699 wi.next = NULL;
700 wi.done = false;
701
702 qemu_cpu_kick(env);
703 while (!wi.done) {
704 CPUState *self_env = cpu_single_env;
705
706 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
707 cpu_single_env = self_env;
708 }
709}
710
711static void flush_queued_work(CPUState *env)
712{
713 struct qemu_work_item *wi;
714
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100715 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300716 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100717 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300718
719 while ((wi = env->queued_work_first)) {
720 env->queued_work_first = wi->next;
721 wi->func(wi->data);
722 wi->done = true;
723 }
724 env->queued_work_last = NULL;
725 qemu_cond_broadcast(&qemu_work_cond);
726}
727
Blue Swirl296af7c2010-03-29 19:23:50 +0000728static void qemu_wait_io_event_common(CPUState *env)
729{
730 if (env->stop) {
731 env->stop = 0;
732 env->stopped = 1;
733 qemu_cond_signal(&qemu_pause_cond);
734 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300735 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100736 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000737}
738
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200739static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000740{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200741 CPUState *env;
742
Jan Kiszka16400322011-02-09 16:29:37 +0100743 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200744 /* Start accounting real time to the virtual clock if the CPUs
745 are idle. */
746 qemu_clock_warp(vm_clock);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100747 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100748 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000749
750 qemu_mutex_unlock(&qemu_global_mutex);
751
752 /*
753 * Users of qemu_global_mutex can be starved, having no chance
754 * to acquire it since this path will get to it first.
755 * So use another lock to provide fairness.
756 */
757 qemu_mutex_lock(&qemu_fair_mutex);
758 qemu_mutex_unlock(&qemu_fair_mutex);
759
760 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200761
762 for (env = first_cpu; env != NULL; env = env->next_cpu) {
763 qemu_wait_io_event_common(env);
764 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000765}
766
Blue Swirl296af7c2010-03-29 19:23:50 +0000767static void qemu_kvm_wait_io_event(CPUState *env)
768{
Jan Kiszka16400322011-02-09 16:29:37 +0100769 while (cpu_thread_is_idle(env)) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100770 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100771 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000772
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100773 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000774 qemu_wait_io_event_common(env);
775}
776
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100777static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000778{
779 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100780 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000781
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300782 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100783 qemu_thread_get_self(env->thread);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100784 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000785
Jan Kiszka84b49152011-02-01 22:15:50 +0100786 r = kvm_init_vcpu(env);
787 if (r < 0) {
788 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
789 exit(1);
790 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000791
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100792 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000793
794 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000795 env->created = 1;
796 qemu_cond_signal(&qemu_cpu_cond);
797
798 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100799 while (!qemu_system_ready) {
Paolo Bonzinie0098942011-03-12 17:44:01 +0100800 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100801 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000802
803 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100804 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100805 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100806 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100807 cpu_handle_guest_debug(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100808 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100809 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000810 qemu_kvm_wait_io_event(env);
811 }
812
813 return NULL;
814}
815
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100816static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000817{
818 CPUState *env = arg;
819
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100820 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100821 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000822
823 /* signal CPU creation */
824 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100825 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100826 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000827 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100828 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000829 qemu_cond_signal(&qemu_cpu_cond);
830
831 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100832 while (!qemu_system_ready) {
Paolo Bonzinie0098942011-03-12 17:44:01 +0100833 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100834 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000835
836 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200837 cpu_exec_all();
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200838 if (use_icount && qemu_next_icount_deadline() <= 0) {
Paolo Bonzini3b2319a2011-04-13 10:03:43 +0200839 qemu_notify_event();
840 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200841 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000842 }
843
844 return NULL;
845}
846
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100847static void qemu_cpu_kick_thread(CPUState *env)
848{
849#ifndef _WIN32
850 int err;
851
852 err = pthread_kill(env->thread->thread, SIG_IPI);
853 if (err) {
854 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
855 exit(1);
856 }
857#else /* _WIN32 */
858 if (!qemu_cpu_is_self(env)) {
859 SuspendThread(env->thread->thread);
860 cpu_signal(0);
861 ResumeThread(env->thread->thread);
862 }
863#endif
864}
865
Blue Swirl296af7c2010-03-29 19:23:50 +0000866void qemu_cpu_kick(void *_env)
867{
868 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100869
Blue Swirl296af7c2010-03-29 19:23:50 +0000870 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100871 if (!env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100872 qemu_cpu_kick_thread(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100873 env->thread_kicked = true;
874 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000875}
876
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100877void qemu_cpu_kick_self(void)
878{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100879#ifndef _WIN32
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100880 assert(cpu_single_env);
881
882 if (!cpu_single_env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100883 qemu_cpu_kick_thread(cpu_single_env);
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100884 cpu_single_env->thread_kicked = true;
885 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100886#else
887 abort();
888#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000889}
890
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100891int qemu_cpu_is_self(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000892{
893 CPUState *env = _env;
Blue Swirl296af7c2010-03-29 19:23:50 +0000894
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100895 return qemu_thread_is_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000896}
897
Blue Swirl296af7c2010-03-29 19:23:50 +0000898void qemu_mutex_lock_iothread(void)
899{
900 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000901 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300902 } else {
903 qemu_mutex_lock(&qemu_fair_mutex);
904 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100905 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300906 qemu_mutex_lock(&qemu_global_mutex);
907 }
908 qemu_mutex_unlock(&qemu_fair_mutex);
909 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000910}
911
912void qemu_mutex_unlock_iothread(void)
913{
914 qemu_mutex_unlock(&qemu_global_mutex);
915}
916
917static int all_vcpus_paused(void)
918{
919 CPUState *penv = first_cpu;
920
921 while (penv) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100922 if (!penv->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000923 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100924 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000925 penv = (CPUState *)penv->next_cpu;
926 }
927
928 return 1;
929}
930
931void pause_all_vcpus(void)
932{
933 CPUState *penv = first_cpu;
934
935 while (penv) {
936 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000937 qemu_cpu_kick(penv);
938 penv = (CPUState *)penv->next_cpu;
939 }
940
941 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100942 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000943 penv = first_cpu;
944 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300945 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000946 penv = (CPUState *)penv->next_cpu;
947 }
948 }
949}
950
951void resume_all_vcpus(void)
952{
953 CPUState *penv = first_cpu;
954
955 while (penv) {
956 penv->stop = 0;
957 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000958 qemu_cpu_kick(penv);
959 penv = (CPUState *)penv->next_cpu;
960 }
961}
962
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100963static void qemu_tcg_init_vcpu(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000964{
965 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100966
Blue Swirl296af7c2010-03-29 19:23:50 +0000967 /* share a single thread for all cpus with TCG */
968 if (!tcg_cpu_thread) {
969 env->thread = qemu_mallocz(sizeof(QemuThread));
970 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
971 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100972 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100973 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100974 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100975 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000976 tcg_cpu_thread = env->thread;
977 tcg_halt_cond = env->halt_cond;
978 } else {
979 env->thread = tcg_cpu_thread;
980 env->halt_cond = tcg_halt_cond;
981 }
982}
983
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100984static void qemu_kvm_start_vcpu(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000985{
986 env->thread = qemu_mallocz(sizeof(QemuThread));
987 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
988 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100989 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100990 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100991 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100992 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000993}
994
995void qemu_init_vcpu(void *_env)
996{
997 CPUState *env = _env;
998
999 env->nr_cores = smp_cores;
1000 env->nr_threads = smp_threads;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001001 if (kvm_enabled()) {
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001002 qemu_kvm_start_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001003 } else {
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001004 qemu_tcg_init_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001005 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001006}
1007
1008void qemu_notify_event(void)
1009{
1010 qemu_event_increment();
1011}
1012
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001013void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001014{
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001015 if (cpu_single_env) {
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001016 cpu_single_env->stop = 0;
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001017 cpu_single_env->stopped = 1;
1018 cpu_exit(cpu_single_env);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001019 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001020 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001021}
1022
1023void vm_stop(int reason)
1024{
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001025 if (!qemu_thread_is_self(&io_thread)) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001026 qemu_system_vmstop_request(reason);
1027 /*
1028 * FIXME: should not return to device code in case
1029 * vm_stop() has been requested.
1030 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001031 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +00001032 return;
1033 }
1034 do_vm_stop(reason);
1035}
1036
1037#endif
1038
Jan Kiszka6792a572011-02-07 12:19:18 +01001039static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001040{
1041 int ret;
1042#ifdef CONFIG_PROFILER
1043 int64_t ti;
1044#endif
1045
1046#ifdef CONFIG_PROFILER
1047 ti = profile_getclock();
1048#endif
1049 if (use_icount) {
1050 int64_t count;
1051 int decr;
1052 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1053 env->icount_decr.u16.low = 0;
1054 env->icount_extra = 0;
Paolo Bonzinicb842c92011-04-13 10:03:46 +02001055 count = qemu_icount_round(qemu_next_icount_deadline());
Blue Swirl296af7c2010-03-29 19:23:50 +00001056 qemu_icount += count;
1057 decr = (count > 0xffff) ? 0xffff : count;
1058 count -= decr;
1059 env->icount_decr.u16.low = decr;
1060 env->icount_extra = count;
1061 }
1062 ret = cpu_exec(env);
1063#ifdef CONFIG_PROFILER
1064 qemu_time += profile_getclock() - ti;
1065#endif
1066 if (use_icount) {
1067 /* Fold pending instructions back into the
1068 instruction counter, and clear the interrupt flag. */
1069 qemu_icount -= (env->icount_decr.u16.low
1070 + env->icount_extra);
1071 env->icount_decr.u32 = 0;
1072 env->icount_extra = 0;
1073 }
1074 return ret;
1075}
1076
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001077bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001078{
Jan Kiszka9a360852011-02-01 22:15:55 +01001079 int r;
1080
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001081 /* Account partial waits to the vm_clock. */
1082 qemu_clock_warp(vm_clock);
1083
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001084 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001085 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001086 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001087 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +02001088 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001089
1090 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +02001091 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001092
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001093#ifndef CONFIG_IOTHREAD
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001094 if (qemu_alarm_pending()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001095 break;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001096 }
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001097#endif
Jan Kiszka3c638d02010-06-25 16:56:56 +02001098 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +01001099 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +01001100 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001101 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +01001102 } else {
1103 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001104 }
1105 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +01001106 cpu_handle_guest_debug(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001107 break;
1108 }
Paolo Bonzinidf646df2011-03-12 17:43:58 +01001109 } else if (env->stop || env->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001110 break;
1111 }
1112 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001113 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +01001114 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +00001115}
1116
1117void set_numa_modes(void)
1118{
1119 CPUState *env;
1120 int i;
1121
1122 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1123 for (i = 0; i < nb_numa_nodes; i++) {
1124 if (node_cpumask[i] & (1 << env->cpu_index)) {
1125 env->numa_node = i;
1126 }
1127 }
1128 }
1129}
1130
1131void set_cpu_log(const char *optarg)
1132{
1133 int mask;
1134 const CPULogItem *item;
1135
1136 mask = cpu_str_to_log_mask(optarg);
1137 if (!mask) {
1138 printf("Log items (comma separated):\n");
1139 for (item = cpu_log_items; item->mask != 0; item++) {
1140 printf("%-10s %s\n", item->name, item->help);
1141 }
1142 exit(1);
1143 }
1144 cpu_set_log(mask);
1145}
Blue Swirl29e922b2010-03-29 19:24:00 +00001146
Matthew Fernandezc235d732011-06-07 16:32:40 +00001147void set_cpu_log_filename(const char *optarg)
1148{
1149 cpu_set_log_filename(optarg);
1150}
1151
Blue Swirl29e922b2010-03-29 19:24:00 +00001152/* Return the virtual CPU time, based on the instruction counter. */
1153int64_t cpu_get_icount(void)
1154{
1155 int64_t icount;
1156 CPUState *env = cpu_single_env;;
1157
1158 icount = qemu_icount;
1159 if (env) {
1160 if (!can_do_io(env)) {
1161 fprintf(stderr, "Bad clock read\n");
1162 }
1163 icount -= (env->icount_decr.u16.low + env->icount_extra);
1164 }
1165 return qemu_icount_bias + (icount << icount_time_shift);
1166}
Blue Swirl262353c2010-05-04 19:55:35 +00001167
Stefan Weil9a78eea2010-10-22 23:03:33 +02001168void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001169{
1170 /* XXX: implement xxx_cpu_list for targets that still miss it */
1171#if defined(cpu_list_id)
1172 cpu_list_id(f, cpu_fprintf, optarg);
1173#elif defined(cpu_list)
1174 cpu_list(f, cpu_fprintf); /* deprecated */
1175#endif
1176}