blob: 3444bc06e7d38de4b964f0aab773e377b59936fd [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
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300121static void do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000122{
123 if (vm_running) {
124 cpu_disable_ticks();
125 vm_running = 0;
126 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300127 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300128 vm_state_notify(0, state);
Michael S. Tsirkin55df6f32010-11-22 19:52:22 +0200129 qemu_aio_flush();
130 bdrv_flush_all();
Blue Swirl296af7c2010-03-29 19:23:50 +0000131 monitor_protocol_event(QEVENT_STOP, NULL);
132 }
133}
134
135static int cpu_can_run(CPUState *env)
136{
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100137 if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000138 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100139 }
140 if (env->stopped || !vm_running) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000141 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100142 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000143 return 1;
144}
145
Jan Kiszka16400322011-02-09 16:29:37 +0100146static bool cpu_thread_is_idle(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000147{
Jan Kiszka16400322011-02-09 16:29:37 +0100148 if (env->stop || env->queued_work_first) {
149 return false;
150 }
151 if (env->stopped || !vm_running) {
152 return true;
153 }
Jan Kiszkaf2c1cc82011-03-15 12:26:18 +0100154 if (!env->halted || qemu_cpu_has_work(env) ||
155 (kvm_enabled() && kvm_irqchip_in_kernel())) {
Jan Kiszka16400322011-02-09 16:29:37 +0100156 return false;
157 }
158 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000159}
160
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200161bool all_cpu_threads_idle(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000162{
163 CPUState *env;
164
Jan Kiszka16400322011-02-09 16:29:37 +0100165 for (env = first_cpu; env != NULL; env = env->next_cpu) {
166 if (!cpu_thread_is_idle(env)) {
167 return false;
168 }
169 }
170 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000171}
172
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100173static void cpu_handle_guest_debug(CPUState *env)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200174{
175 gdb_set_stop_cpu(env);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100176 qemu_system_debug_request();
Jan Kiszka83f338f2011-02-07 12:19:17 +0100177 env->stopped = 1;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200178}
179
Paolo Bonzini714bd042011-03-12 17:44:06 +0100180static void cpu_signal(int sig)
181{
182 if (cpu_single_env) {
183 cpu_exit(cpu_single_env);
184 }
185 exit_request = 1;
186}
Paolo Bonzini714bd042011-03-12 17:44:06 +0100187
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100188#ifdef CONFIG_LINUX
189static void sigbus_reraise(void)
190{
191 sigset_t set;
192 struct sigaction action;
193
194 memset(&action, 0, sizeof(action));
195 action.sa_handler = SIG_DFL;
196 if (!sigaction(SIGBUS, &action, NULL)) {
197 raise(SIGBUS);
198 sigemptyset(&set);
199 sigaddset(&set, SIGBUS);
200 sigprocmask(SIG_UNBLOCK, &set, NULL);
201 }
202 perror("Failed to re-raise SIGBUS!\n");
203 abort();
204}
205
206static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
207 void *ctx)
208{
209 if (kvm_on_sigbus(siginfo->ssi_code,
210 (void *)(intptr_t)siginfo->ssi_addr)) {
211 sigbus_reraise();
212 }
213}
214
215static void qemu_init_sigbus(void)
216{
217 struct sigaction action;
218
219 memset(&action, 0, sizeof(action));
220 action.sa_flags = SA_SIGINFO;
221 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
222 sigaction(SIGBUS, &action, NULL);
223
224 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
225}
226
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100227static void qemu_kvm_eat_signals(CPUState *env)
228{
229 struct timespec ts = { 0, 0 };
230 siginfo_t siginfo;
231 sigset_t waitset;
232 sigset_t chkset;
233 int r;
234
235 sigemptyset(&waitset);
236 sigaddset(&waitset, SIG_IPI);
237 sigaddset(&waitset, SIGBUS);
238
239 do {
240 r = sigtimedwait(&waitset, &siginfo, &ts);
241 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
242 perror("sigtimedwait");
243 exit(1);
244 }
245
246 switch (r) {
247 case SIGBUS:
248 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
249 sigbus_reraise();
250 }
251 break;
252 default:
253 break;
254 }
255
256 r = sigpending(&chkset);
257 if (r == -1) {
258 perror("sigpending");
259 exit(1);
260 }
261 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100262}
263
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100264#else /* !CONFIG_LINUX */
265
266static void qemu_init_sigbus(void)
267{
268}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100269
270static void qemu_kvm_eat_signals(CPUState *env)
271{
272}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100273#endif /* !CONFIG_LINUX */
274
Blue Swirl296af7c2010-03-29 19:23:50 +0000275#ifndef _WIN32
276static int io_thread_fd = -1;
277
278static void qemu_event_increment(void)
279{
280 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000281 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000282 ssize_t ret;
283
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100284 if (io_thread_fd == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000285 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100286 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000287 do {
288 ret = write(io_thread_fd, &val, sizeof(val));
289 } while (ret < 0 && errno == EINTR);
290
291 /* EAGAIN is fine, a read must be pending. */
292 if (ret < 0 && errno != EAGAIN) {
Alexandre Raymond77bec682011-06-13 22:16:36 -0400293 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
Blue Swirl296af7c2010-03-29 19:23:50 +0000294 strerror(errno));
295 exit (1);
296 }
297}
298
299static void qemu_event_read(void *opaque)
300{
Stefan Weile0efb992011-02-23 19:09:16 +0100301 int fd = (intptr_t)opaque;
Blue Swirl296af7c2010-03-29 19:23:50 +0000302 ssize_t len;
303 char buffer[512];
304
305 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
306 do {
307 len = read(fd, buffer, sizeof(buffer));
308 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
309}
310
311static int qemu_event_init(void)
312{
313 int err;
314 int fds[2];
315
316 err = qemu_eventfd(fds);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100317 if (err == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000318 return -errno;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100319 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000320 err = fcntl_setfl(fds[0], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100321 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000322 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100323 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000324 err = fcntl_setfl(fds[1], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100325 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000326 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100327 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000328 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100329 (void *)(intptr_t)fds[0]);
Blue Swirl296af7c2010-03-29 19:23:50 +0000330
331 io_thread_fd = fds[1];
332 return 0;
333
334fail:
335 close(fds[0]);
336 close(fds[1]);
337 return err;
338}
Blue Swirl296af7c2010-03-29 19:23:50 +0000339
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100340static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000341{
342}
343
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300344/* If we have signalfd, we mask out the signals we want to handle and then
345 * use signalfd to listen for them. We rely on whatever the current signal
346 * handler is to dispatch the signals when we receive them.
347 */
348static void sigfd_handler(void *opaque)
349{
Stefan Weile0efb992011-02-23 19:09:16 +0100350 int fd = (intptr_t)opaque;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300351 struct qemu_signalfd_siginfo info;
352 struct sigaction action;
353 ssize_t len;
354
355 while (1) {
356 do {
357 len = read(fd, &info, sizeof(info));
358 } while (len == -1 && errno == EINTR);
359
360 if (len == -1 && errno == EAGAIN) {
361 break;
362 }
363
364 if (len != sizeof(info)) {
365 printf("read from sigfd returned %zd: %m\n", len);
366 return;
367 }
368
369 sigaction(info.ssi_signo, NULL, &action);
370 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
371 action.sa_sigaction(info.ssi_signo,
372 (siginfo_t *)&info, NULL);
373 } else if (action.sa_handler) {
374 action.sa_handler(info.ssi_signo);
375 }
376 }
377}
378
Paolo Bonzini712ae482011-03-12 17:44:05 +0100379static int qemu_signal_init(void)
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300380{
381 int sigfd;
Paolo Bonzini712ae482011-03-12 17:44:05 +0100382 sigset_t set;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300383
Paolo Bonzini712ae482011-03-12 17:44:05 +0100384 /* SIGUSR2 used by posix-aio-compat.c */
385 sigemptyset(&set);
386 sigaddset(&set, SIGUSR2);
387 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
388
Alexandre Raymond89b9ba62011-06-15 01:20:31 -0400389 /*
390 * SIG_IPI must be blocked in the main thread and must not be caught
391 * by sigwait() in the signal thread. Otherwise, the cpu thread will
392 * not catch it reliably.
393 */
394 sigemptyset(&set);
395 sigaddset(&set, SIG_IPI);
396 pthread_sigmask(SIG_BLOCK, &set, NULL);
397
Paolo Bonzini712ae482011-03-12 17:44:05 +0100398 sigemptyset(&set);
399 sigaddset(&set, SIGIO);
400 sigaddset(&set, SIGALRM);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100401 sigaddset(&set, SIGBUS);
Alexandre Raymond5664aed2011-06-14 10:05:36 -0400402 pthread_sigmask(SIG_BLOCK, &set, NULL);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100403
404 sigfd = qemu_signalfd(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300405 if (sigfd == -1) {
406 fprintf(stderr, "failed to create signalfd\n");
407 return -errno;
408 }
409
410 fcntl_setfl(sigfd, O_NONBLOCK);
411
412 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100413 (void *)(intptr_t)sigfd);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300414
415 return 0;
416}
Blue Swirl296af7c2010-03-29 19:23:50 +0000417
Paolo Bonzini714bd042011-03-12 17:44:06 +0100418static void qemu_kvm_init_cpu_signals(CPUState *env)
419{
420 int r;
421 sigset_t set;
422 struct sigaction sigact;
423
424 memset(&sigact, 0, sizeof(sigact));
425 sigact.sa_handler = dummy_signal;
426 sigaction(SIG_IPI, &sigact, NULL);
427
Paolo Bonzini714bd042011-03-12 17:44:06 +0100428 pthread_sigmask(SIG_BLOCK, NULL, &set);
429 sigdelset(&set, SIG_IPI);
430 sigdelset(&set, SIGBUS);
431 r = kvm_set_signal_mask(env, &set);
432 if (r) {
433 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
434 exit(1);
435 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100436
Paolo Bonzini714bd042011-03-12 17:44:06 +0100437 sigdelset(&set, SIG_IPI);
438 sigdelset(&set, SIGBUS);
439 r = kvm_set_signal_mask(env, &set);
440 if (r) {
441 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
442 exit(1);
443 }
444}
445
446static void qemu_tcg_init_cpu_signals(void)
447{
Paolo Bonzini714bd042011-03-12 17:44:06 +0100448 sigset_t set;
449 struct sigaction sigact;
450
451 memset(&sigact, 0, sizeof(sigact));
452 sigact.sa_handler = cpu_signal;
453 sigaction(SIG_IPI, &sigact, NULL);
454
455 sigemptyset(&set);
456 sigaddset(&set, SIG_IPI);
457 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100458}
459
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100460#else /* _WIN32 */
461
Blue Swirl296af7c2010-03-29 19:23:50 +0000462HANDLE qemu_event_handle;
463
464static void dummy_event_handler(void *opaque)
465{
466}
467
468static int qemu_event_init(void)
469{
470 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
471 if (!qemu_event_handle) {
472 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
473 return -1;
474 }
475 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
476 return 0;
477}
478
479static void qemu_event_increment(void)
480{
481 if (!SetEvent(qemu_event_handle)) {
482 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
483 GetLastError());
484 exit (1);
485 }
486}
Jan Kiszka9a360852011-02-01 22:15:55 +0100487
Paolo Bonzini712ae482011-03-12 17:44:05 +0100488static int qemu_signal_init(void)
489{
490 return 0;
491}
492
Paolo Bonzini714bd042011-03-12 17:44:06 +0100493static void qemu_kvm_init_cpu_signals(CPUState *env)
494{
495 abort();
496}
497
498static void qemu_tcg_init_cpu_signals(void)
499{
500}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100501#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000502
Blue Swirl296af7c2010-03-29 19:23:50 +0000503QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200504static QemuCond qemu_io_proceeded_cond;
505static bool iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000506
507static QemuThread io_thread;
508
509static QemuThread *tcg_cpu_thread;
510static QemuCond *tcg_halt_cond;
511
Blue Swirl296af7c2010-03-29 19:23:50 +0000512/* cpu creation */
513static QemuCond qemu_cpu_cond;
514/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000515static QemuCond qemu_pause_cond;
516static QemuCond qemu_work_cond;
517
Blue Swirl296af7c2010-03-29 19:23:50 +0000518int qemu_init_main_loop(void)
519{
520 int ret;
521
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100522 qemu_init_sigbus();
Jan Kiszka3c638d02010-06-25 16:56:56 +0200523
Paolo Bonzini712ae482011-03-12 17:44:05 +0100524 ret = qemu_signal_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100525 if (ret) {
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300526 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100527 }
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300528
529 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000530 ret = qemu_event_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100531 if (ret) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000532 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100533 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000534
Anthony Liguoried945922011-02-08 18:18:18 +0100535 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100536 qemu_cond_init(&qemu_pause_cond);
537 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200538 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000539 qemu_mutex_init(&qemu_global_mutex);
540 qemu_mutex_lock(&qemu_global_mutex);
541
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100542 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000543
544 return 0;
545}
546
Blue Swirl7277e022010-04-12 17:19:06 +0000547void qemu_main_loop_start(void)
548{
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200549 resume_all_vcpus();
Blue Swirl7277e022010-04-12 17:19:06 +0000550}
551
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300552void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
553{
554 struct qemu_work_item wi;
555
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100556 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300557 func(data);
558 return;
559 }
560
561 wi.func = func;
562 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100563 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300564 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100565 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300566 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100567 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300568 env->queued_work_last = &wi;
569 wi.next = NULL;
570 wi.done = false;
571
572 qemu_cpu_kick(env);
573 while (!wi.done) {
574 CPUState *self_env = cpu_single_env;
575
576 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
577 cpu_single_env = self_env;
578 }
579}
580
581static void flush_queued_work(CPUState *env)
582{
583 struct qemu_work_item *wi;
584
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100585 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300586 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100587 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300588
589 while ((wi = env->queued_work_first)) {
590 env->queued_work_first = wi->next;
591 wi->func(wi->data);
592 wi->done = true;
593 }
594 env->queued_work_last = NULL;
595 qemu_cond_broadcast(&qemu_work_cond);
596}
597
Blue Swirl296af7c2010-03-29 19:23:50 +0000598static void qemu_wait_io_event_common(CPUState *env)
599{
600 if (env->stop) {
601 env->stop = 0;
602 env->stopped = 1;
603 qemu_cond_signal(&qemu_pause_cond);
604 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300605 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100606 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000607}
608
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200609static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000610{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200611 CPUState *env;
612
Jan Kiszka16400322011-02-09 16:29:37 +0100613 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200614 /* Start accounting real time to the virtual clock if the CPUs
615 are idle. */
616 qemu_clock_warp(vm_clock);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100617 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100618 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000619
Paolo Bonzini46daff12011-06-09 13:10:24 +0200620 while (iothread_requesting_mutex) {
621 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
622 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200623
624 for (env = first_cpu; env != NULL; env = env->next_cpu) {
625 qemu_wait_io_event_common(env);
626 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000627}
628
Blue Swirl296af7c2010-03-29 19:23:50 +0000629static void qemu_kvm_wait_io_event(CPUState *env)
630{
Jan Kiszka16400322011-02-09 16:29:37 +0100631 while (cpu_thread_is_idle(env)) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100632 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100633 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000634
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100635 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000636 qemu_wait_io_event_common(env);
637}
638
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100639static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000640{
641 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100642 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000643
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300644 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100645 qemu_thread_get_self(env->thread);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100646 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000647
Jan Kiszka84b49152011-02-01 22:15:50 +0100648 r = kvm_init_vcpu(env);
649 if (r < 0) {
650 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
651 exit(1);
652 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000653
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100654 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000655
656 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000657 env->created = 1;
658 qemu_cond_signal(&qemu_cpu_cond);
659
Blue Swirl296af7c2010-03-29 19:23:50 +0000660 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100661 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100662 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100663 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100664 cpu_handle_guest_debug(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100665 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100666 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000667 qemu_kvm_wait_io_event(env);
668 }
669
670 return NULL;
671}
672
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100673static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000674{
675 CPUState *env = arg;
676
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100677 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100678 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000679
680 /* signal CPU creation */
681 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100682 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100683 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000684 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100685 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000686 qemu_cond_signal(&qemu_cpu_cond);
687
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200688 /* wait for initial kick-off after machine start */
689 while (first_cpu->stopped) {
690 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100691 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000692
693 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200694 cpu_exec_all();
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200695 if (use_icount && qemu_next_icount_deadline() <= 0) {
Paolo Bonzini3b2319a2011-04-13 10:03:43 +0200696 qemu_notify_event();
697 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200698 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000699 }
700
701 return NULL;
702}
703
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100704static void qemu_cpu_kick_thread(CPUState *env)
705{
706#ifndef _WIN32
707 int err;
708
709 err = pthread_kill(env->thread->thread, SIG_IPI);
710 if (err) {
711 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
712 exit(1);
713 }
714#else /* _WIN32 */
715 if (!qemu_cpu_is_self(env)) {
716 SuspendThread(env->thread->thread);
717 cpu_signal(0);
718 ResumeThread(env->thread->thread);
719 }
720#endif
721}
722
Blue Swirl296af7c2010-03-29 19:23:50 +0000723void qemu_cpu_kick(void *_env)
724{
725 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100726
Blue Swirl296af7c2010-03-29 19:23:50 +0000727 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaeae74cf2011-08-22 17:46:03 +0200728 if (kvm_enabled() && !env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100729 qemu_cpu_kick_thread(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100730 env->thread_kicked = true;
731 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000732}
733
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100734void qemu_cpu_kick_self(void)
735{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100736#ifndef _WIN32
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100737 assert(cpu_single_env);
738
739 if (!cpu_single_env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100740 qemu_cpu_kick_thread(cpu_single_env);
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100741 cpu_single_env->thread_kicked = true;
742 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100743#else
744 abort();
745#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000746}
747
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100748int qemu_cpu_is_self(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000749{
750 CPUState *env = _env;
Blue Swirl296af7c2010-03-29 19:23:50 +0000751
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100752 return qemu_thread_is_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000753}
754
Blue Swirl296af7c2010-03-29 19:23:50 +0000755void qemu_mutex_lock_iothread(void)
756{
757 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000758 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300759 } else {
Paolo Bonzini46daff12011-06-09 13:10:24 +0200760 iothread_requesting_mutex = true;
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300761 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100762 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300763 qemu_mutex_lock(&qemu_global_mutex);
764 }
Paolo Bonzini46daff12011-06-09 13:10:24 +0200765 iothread_requesting_mutex = false;
766 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300767 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000768}
769
770void qemu_mutex_unlock_iothread(void)
771{
772 qemu_mutex_unlock(&qemu_global_mutex);
773}
774
775static int all_vcpus_paused(void)
776{
777 CPUState *penv = first_cpu;
778
779 while (penv) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100780 if (!penv->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000781 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100782 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000783 penv = (CPUState *)penv->next_cpu;
784 }
785
786 return 1;
787}
788
789void pause_all_vcpus(void)
790{
791 CPUState *penv = first_cpu;
792
793 while (penv) {
794 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000795 qemu_cpu_kick(penv);
796 penv = (CPUState *)penv->next_cpu;
797 }
798
799 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100800 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000801 penv = first_cpu;
802 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300803 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000804 penv = (CPUState *)penv->next_cpu;
805 }
806 }
807}
808
809void resume_all_vcpus(void)
810{
811 CPUState *penv = first_cpu;
812
813 while (penv) {
814 penv->stop = 0;
815 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000816 qemu_cpu_kick(penv);
817 penv = (CPUState *)penv->next_cpu;
818 }
819}
820
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100821static void qemu_tcg_init_vcpu(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000822{
823 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100824
Blue Swirl296af7c2010-03-29 19:23:50 +0000825 /* share a single thread for all cpus with TCG */
826 if (!tcg_cpu_thread) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500827 env->thread = g_malloc0(sizeof(QemuThread));
828 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +0000829 qemu_cond_init(env->halt_cond);
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200830 tcg_halt_cond = env->halt_cond;
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100831 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100832 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100833 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100834 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000835 tcg_cpu_thread = env->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +0000836 } else {
837 env->thread = tcg_cpu_thread;
838 env->halt_cond = tcg_halt_cond;
839 }
840}
841
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100842static void qemu_kvm_start_vcpu(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000843{
Anthony Liguori7267c092011-08-20 22:09:37 -0500844 env->thread = g_malloc0(sizeof(QemuThread));
845 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +0000846 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100847 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100848 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100849 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100850 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000851}
852
853void qemu_init_vcpu(void *_env)
854{
855 CPUState *env = _env;
856
857 env->nr_cores = smp_cores;
858 env->nr_threads = smp_threads;
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200859 env->stopped = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100860 if (kvm_enabled()) {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100861 qemu_kvm_start_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100862 } else {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100863 qemu_tcg_init_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100864 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000865}
866
867void qemu_notify_event(void)
868{
869 qemu_event_increment();
870}
871
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100872void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000873{
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100874 if (cpu_single_env) {
Paolo Bonzini67bb1722011-03-12 17:43:59 +0100875 cpu_single_env->stop = 0;
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100876 cpu_single_env->stopped = 1;
877 cpu_exit(cpu_single_env);
Paolo Bonzini67bb1722011-03-12 17:43:59 +0100878 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100879 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000880}
881
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300882void vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000883{
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100884 if (!qemu_thread_is_self(&io_thread)) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300885 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000886 /*
887 * FIXME: should not return to device code in case
888 * vm_stop() has been requested.
889 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100890 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +0000891 return;
892 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300893 do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000894}
895
Jan Kiszka6792a572011-02-07 12:19:18 +0100896static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000897{
898 int ret;
899#ifdef CONFIG_PROFILER
900 int64_t ti;
901#endif
902
903#ifdef CONFIG_PROFILER
904 ti = profile_getclock();
905#endif
906 if (use_icount) {
907 int64_t count;
908 int decr;
909 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
910 env->icount_decr.u16.low = 0;
911 env->icount_extra = 0;
Paolo Bonzinicb842c92011-04-13 10:03:46 +0200912 count = qemu_icount_round(qemu_next_icount_deadline());
Blue Swirl296af7c2010-03-29 19:23:50 +0000913 qemu_icount += count;
914 decr = (count > 0xffff) ? 0xffff : count;
915 count -= decr;
916 env->icount_decr.u16.low = decr;
917 env->icount_extra = count;
918 }
919 ret = cpu_exec(env);
920#ifdef CONFIG_PROFILER
921 qemu_time += profile_getclock() - ti;
922#endif
923 if (use_icount) {
924 /* Fold pending instructions back into the
925 instruction counter, and clear the interrupt flag. */
926 qemu_icount -= (env->icount_decr.u16.low
927 + env->icount_extra);
928 env->icount_decr.u32 = 0;
929 env->icount_extra = 0;
930 }
931 return ret;
932}
933
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200934bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000935{
Jan Kiszka9a360852011-02-01 22:15:55 +0100936 int r;
937
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200938 /* Account partial waits to the vm_clock. */
939 qemu_clock_warp(vm_clock);
940
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100941 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000942 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100943 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200944 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +0200945 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000946
947 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +0200948 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +0000949
Jan Kiszka3c638d02010-06-25 16:56:56 +0200950 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +0100951 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100952 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +0100953 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +0100954 } else {
955 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +0100956 }
957 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100958 cpu_handle_guest_debug(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +0200959 break;
960 }
Paolo Bonzinidf646df2011-03-12 17:43:58 +0100961 } else if (env->stop || env->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000962 break;
963 }
964 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200965 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +0100966 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +0000967}
968
969void set_numa_modes(void)
970{
971 CPUState *env;
972 int i;
973
974 for (env = first_cpu; env != NULL; env = env->next_cpu) {
975 for (i = 0; i < nb_numa_nodes; i++) {
976 if (node_cpumask[i] & (1 << env->cpu_index)) {
977 env->numa_node = i;
978 }
979 }
980 }
981}
982
983void set_cpu_log(const char *optarg)
984{
985 int mask;
986 const CPULogItem *item;
987
988 mask = cpu_str_to_log_mask(optarg);
989 if (!mask) {
990 printf("Log items (comma separated):\n");
991 for (item = cpu_log_items; item->mask != 0; item++) {
992 printf("%-10s %s\n", item->name, item->help);
993 }
994 exit(1);
995 }
996 cpu_set_log(mask);
997}
Blue Swirl29e922b2010-03-29 19:24:00 +0000998
Matthew Fernandezc235d732011-06-07 16:32:40 +0000999void set_cpu_log_filename(const char *optarg)
1000{
1001 cpu_set_log_filename(optarg);
1002}
1003
Blue Swirl29e922b2010-03-29 19:24:00 +00001004/* Return the virtual CPU time, based on the instruction counter. */
1005int64_t cpu_get_icount(void)
1006{
1007 int64_t icount;
1008 CPUState *env = cpu_single_env;;
1009
1010 icount = qemu_icount;
1011 if (env) {
1012 if (!can_do_io(env)) {
1013 fprintf(stderr, "Bad clock read\n");
1014 }
1015 icount -= (env->icount_decr.u16.low + env->icount_extra);
1016 }
1017 return qemu_icount_bias + (icount << icount_time_shift);
1018}
Blue Swirl262353c2010-05-04 19:55:35 +00001019
Stefan Weil9a78eea2010-10-22 23:03:33 +02001020void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001021{
1022 /* XXX: implement xxx_cpu_list for targets that still miss it */
1023#if defined(cpu_list_id)
1024 cpu_list_id(f, cpu_fprintf, optarg);
1025#elif defined(cpu_list)
1026 cpu_list(f, cpu_fprintf); /* deprecated */
1027#endif
1028}