blob: 41bec7cc561df67465b7c22c6eef90e5fd9233f9 [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"
Jan Kiszka262ea182010-07-06 10:49:57 +020033#include "exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000034
Paolo Bonzini96284e82011-03-12 17:43:53 +010035#include "qemu-thread.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000036#include "cpus.h"
Marcelo Tosattia8486bc2010-10-11 15:31:16 -030037#include "compatfd.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000038
Blue Swirl7277e022010-04-12 17:19:06 +000039#ifdef SIGRTMIN
40#define SIG_IPI (SIGRTMIN+4)
41#else
42#define SIG_IPI SIGUSR1
43#endif
44
Jan Kiszka6d9cb732011-02-01 22:15:58 +010045#ifdef CONFIG_LINUX
46
47#include <sys/prctl.h>
48
Marcelo Tosattic0532a72010-10-11 15:31:21 -030049#ifndef PR_MCE_KILL
50#define PR_MCE_KILL 33
51#endif
52
Jan Kiszka6d9cb732011-02-01 22:15:58 +010053#ifndef PR_MCE_KILL_SET
54#define PR_MCE_KILL_SET 1
55#endif
56
57#ifndef PR_MCE_KILL_EARLY
58#define PR_MCE_KILL_EARLY 1
59#endif
60
61#endif /* CONFIG_LINUX */
62
Blue Swirl296af7c2010-03-29 19:23:50 +000063static CPUState *next_cpu;
64
65/***********************************************************/
66void hw_error(const char *fmt, ...)
67{
68 va_list ap;
69 CPUState *env;
70
71 va_start(ap, fmt);
72 fprintf(stderr, "qemu: hardware error: ");
73 vfprintf(stderr, fmt, ap);
74 fprintf(stderr, "\n");
75 for(env = first_cpu; env != NULL; env = env->next_cpu) {
76 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
77#ifdef TARGET_I386
78 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
79#else
80 cpu_dump_state(env, stderr, fprintf, 0);
81#endif
82 }
83 va_end(ap);
84 abort();
85}
86
87void cpu_synchronize_all_states(void)
88{
89 CPUState *cpu;
90
91 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
92 cpu_synchronize_state(cpu);
93 }
94}
95
96void cpu_synchronize_all_post_reset(void)
97{
98 CPUState *cpu;
99
100 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
101 cpu_synchronize_post_reset(cpu);
102 }
103}
104
105void cpu_synchronize_all_post_init(void)
106{
107 CPUState *cpu;
108
109 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
110 cpu_synchronize_post_init(cpu);
111 }
112}
113
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300114int cpu_is_stopped(CPUState *env)
115{
116 return !vm_running || env->stopped;
117}
118
Blue Swirl296af7c2010-03-29 19:23:50 +0000119static void do_vm_stop(int reason)
120{
121 if (vm_running) {
122 cpu_disable_ticks();
123 vm_running = 0;
124 pause_all_vcpus();
125 vm_state_notify(0, reason);
Michael S. Tsirkin55df6f32010-11-22 19:52:22 +0200126 qemu_aio_flush();
127 bdrv_flush_all();
Blue Swirl296af7c2010-03-29 19:23:50 +0000128 monitor_protocol_event(QEVENT_STOP, NULL);
129 }
130}
131
132static int cpu_can_run(CPUState *env)
133{
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100134 if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000135 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100136 }
137 if (env->stopped || !vm_running) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000138 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100139 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000140 return 1;
141}
142
Jan Kiszka16400322011-02-09 16:29:37 +0100143static bool cpu_thread_is_idle(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000144{
Jan Kiszka16400322011-02-09 16:29:37 +0100145 if (env->stop || env->queued_work_first) {
146 return false;
147 }
148 if (env->stopped || !vm_running) {
149 return true;
150 }
Jan Kiszkaf2c1cc82011-03-15 12:26:18 +0100151 if (!env->halted || qemu_cpu_has_work(env) ||
152 (kvm_enabled() && kvm_irqchip_in_kernel())) {
Jan Kiszka16400322011-02-09 16:29:37 +0100153 return false;
154 }
155 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000156}
157
Jan Kiszka16400322011-02-09 16:29:37 +0100158static bool all_cpu_threads_idle(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000159{
160 CPUState *env;
161
Jan Kiszka16400322011-02-09 16:29:37 +0100162 for (env = first_cpu; env != NULL; env = env->next_cpu) {
163 if (!cpu_thread_is_idle(env)) {
164 return false;
165 }
166 }
167 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000168}
169
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100170static void cpu_handle_guest_debug(CPUState *env)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200171{
172 gdb_set_stop_cpu(env);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100173 qemu_system_debug_request();
Jan Kiszka83f338f2011-02-07 12:19:17 +0100174#ifdef CONFIG_IOTHREAD
175 env->stopped = 1;
176#endif
Jan Kiszka3c638d02010-06-25 16:56:56 +0200177}
178
Paolo Bonzini714bd042011-03-12 17:44:06 +0100179#ifdef CONFIG_IOTHREAD
180static void cpu_signal(int sig)
181{
182 if (cpu_single_env) {
183 cpu_exit(cpu_single_env);
184 }
185 exit_request = 1;
186}
187#endif
188
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100189#ifdef CONFIG_LINUX
190static void sigbus_reraise(void)
191{
192 sigset_t set;
193 struct sigaction action;
194
195 memset(&action, 0, sizeof(action));
196 action.sa_handler = SIG_DFL;
197 if (!sigaction(SIGBUS, &action, NULL)) {
198 raise(SIGBUS);
199 sigemptyset(&set);
200 sigaddset(&set, SIGBUS);
201 sigprocmask(SIG_UNBLOCK, &set, NULL);
202 }
203 perror("Failed to re-raise SIGBUS!\n");
204 abort();
205}
206
207static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
208 void *ctx)
209{
210 if (kvm_on_sigbus(siginfo->ssi_code,
211 (void *)(intptr_t)siginfo->ssi_addr)) {
212 sigbus_reraise();
213 }
214}
215
216static void qemu_init_sigbus(void)
217{
218 struct sigaction action;
219
220 memset(&action, 0, sizeof(action));
221 action.sa_flags = SA_SIGINFO;
222 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
223 sigaction(SIGBUS, &action, NULL);
224
225 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
226}
227
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100228static void qemu_kvm_eat_signals(CPUState *env)
229{
230 struct timespec ts = { 0, 0 };
231 siginfo_t siginfo;
232 sigset_t waitset;
233 sigset_t chkset;
234 int r;
235
236 sigemptyset(&waitset);
237 sigaddset(&waitset, SIG_IPI);
238 sigaddset(&waitset, SIGBUS);
239
240 do {
241 r = sigtimedwait(&waitset, &siginfo, &ts);
242 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
243 perror("sigtimedwait");
244 exit(1);
245 }
246
247 switch (r) {
248 case SIGBUS:
249 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
250 sigbus_reraise();
251 }
252 break;
253 default:
254 break;
255 }
256
257 r = sigpending(&chkset);
258 if (r == -1) {
259 perror("sigpending");
260 exit(1);
261 }
262 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
263
264#ifndef CONFIG_IOTHREAD
265 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
266 qemu_notify_event();
267 }
268#endif
269}
270
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100271#else /* !CONFIG_LINUX */
272
273static void qemu_init_sigbus(void)
274{
275}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100276
277static void qemu_kvm_eat_signals(CPUState *env)
278{
279}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100280#endif /* !CONFIG_LINUX */
281
Blue Swirl296af7c2010-03-29 19:23:50 +0000282#ifndef _WIN32
283static int io_thread_fd = -1;
284
285static void qemu_event_increment(void)
286{
287 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000288 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000289 ssize_t ret;
290
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100291 if (io_thread_fd == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000292 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100293 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000294 do {
295 ret = write(io_thread_fd, &val, sizeof(val));
296 } while (ret < 0 && errno == EINTR);
297
298 /* EAGAIN is fine, a read must be pending. */
299 if (ret < 0 && errno != EAGAIN) {
300 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
301 strerror(errno));
302 exit (1);
303 }
304}
305
306static void qemu_event_read(void *opaque)
307{
Stefan Weile0efb992011-02-23 19:09:16 +0100308 int fd = (intptr_t)opaque;
Blue Swirl296af7c2010-03-29 19:23:50 +0000309 ssize_t len;
310 char buffer[512];
311
312 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
313 do {
314 len = read(fd, buffer, sizeof(buffer));
315 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
316}
317
318static int qemu_event_init(void)
319{
320 int err;
321 int fds[2];
322
323 err = qemu_eventfd(fds);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100324 if (err == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000325 return -errno;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100326 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000327 err = fcntl_setfl(fds[0], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100328 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000329 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100330 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000331 err = fcntl_setfl(fds[1], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100332 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000333 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100334 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000335 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100336 (void *)(intptr_t)fds[0]);
Blue Swirl296af7c2010-03-29 19:23:50 +0000337
338 io_thread_fd = fds[1];
339 return 0;
340
341fail:
342 close(fds[0]);
343 close(fds[1]);
344 return err;
345}
Blue Swirl296af7c2010-03-29 19:23:50 +0000346
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100347static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000348{
349}
350
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300351/* If we have signalfd, we mask out the signals we want to handle and then
352 * use signalfd to listen for them. We rely on whatever the current signal
353 * handler is to dispatch the signals when we receive them.
354 */
355static void sigfd_handler(void *opaque)
356{
Stefan Weile0efb992011-02-23 19:09:16 +0100357 int fd = (intptr_t)opaque;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300358 struct qemu_signalfd_siginfo info;
359 struct sigaction action;
360 ssize_t len;
361
362 while (1) {
363 do {
364 len = read(fd, &info, sizeof(info));
365 } while (len == -1 && errno == EINTR);
366
367 if (len == -1 && errno == EAGAIN) {
368 break;
369 }
370
371 if (len != sizeof(info)) {
372 printf("read from sigfd returned %zd: %m\n", len);
373 return;
374 }
375
376 sigaction(info.ssi_signo, NULL, &action);
377 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
378 action.sa_sigaction(info.ssi_signo,
379 (siginfo_t *)&info, NULL);
380 } else if (action.sa_handler) {
381 action.sa_handler(info.ssi_signo);
382 }
383 }
384}
385
Paolo Bonzini712ae482011-03-12 17:44:05 +0100386static int qemu_signal_init(void)
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300387{
388 int sigfd;
Paolo Bonzini712ae482011-03-12 17:44:05 +0100389 sigset_t set;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300390
Paolo Bonzini712ae482011-03-12 17:44:05 +0100391#ifdef CONFIG_IOTHREAD
392 /* SIGUSR2 used by posix-aio-compat.c */
393 sigemptyset(&set);
394 sigaddset(&set, SIGUSR2);
395 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
396
397 sigemptyset(&set);
398 sigaddset(&set, SIGIO);
399 sigaddset(&set, SIGALRM);
400 sigaddset(&set, SIG_IPI);
401 sigaddset(&set, SIGBUS);
402 pthread_sigmask(SIG_BLOCK, &set, NULL);
403#else
404 sigemptyset(&set);
405 sigaddset(&set, SIGBUS);
406 if (kvm_enabled()) {
407 /*
408 * We need to process timer signals synchronously to avoid a race
409 * between exit_request check and KVM vcpu entry.
410 */
411 sigaddset(&set, SIGIO);
412 sigaddset(&set, SIGALRM);
413 }
414#endif
415
416 sigfd = qemu_signalfd(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300417 if (sigfd == -1) {
418 fprintf(stderr, "failed to create signalfd\n");
419 return -errno;
420 }
421
422 fcntl_setfl(sigfd, O_NONBLOCK);
423
424 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100425 (void *)(intptr_t)sigfd);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300426
427 return 0;
428}
Blue Swirl296af7c2010-03-29 19:23:50 +0000429
Paolo Bonzini714bd042011-03-12 17:44:06 +0100430static void qemu_kvm_init_cpu_signals(CPUState *env)
431{
432 int r;
433 sigset_t set;
434 struct sigaction sigact;
435
436 memset(&sigact, 0, sizeof(sigact));
437 sigact.sa_handler = dummy_signal;
438 sigaction(SIG_IPI, &sigact, NULL);
439
440#ifdef CONFIG_IOTHREAD
441 pthread_sigmask(SIG_BLOCK, NULL, &set);
442 sigdelset(&set, SIG_IPI);
443 sigdelset(&set, SIGBUS);
444 r = kvm_set_signal_mask(env, &set);
445 if (r) {
446 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
447 exit(1);
448 }
449#else
450 sigemptyset(&set);
451 sigaddset(&set, SIG_IPI);
452 sigaddset(&set, SIGIO);
453 sigaddset(&set, SIGALRM);
454 pthread_sigmask(SIG_BLOCK, &set, NULL);
455
456 pthread_sigmask(SIG_BLOCK, NULL, &set);
457 sigdelset(&set, SIGIO);
458 sigdelset(&set, SIGALRM);
459#endif
460 sigdelset(&set, SIG_IPI);
461 sigdelset(&set, SIGBUS);
462 r = kvm_set_signal_mask(env, &set);
463 if (r) {
464 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
465 exit(1);
466 }
467}
468
469static void qemu_tcg_init_cpu_signals(void)
470{
471#ifdef CONFIG_IOTHREAD
472 sigset_t set;
473 struct sigaction sigact;
474
475 memset(&sigact, 0, sizeof(sigact));
476 sigact.sa_handler = cpu_signal;
477 sigaction(SIG_IPI, &sigact, NULL);
478
479 sigemptyset(&set);
480 sigaddset(&set, SIG_IPI);
481 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
482#endif
483}
484
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100485#else /* _WIN32 */
486
Blue Swirl296af7c2010-03-29 19:23:50 +0000487HANDLE qemu_event_handle;
488
489static void dummy_event_handler(void *opaque)
490{
491}
492
493static int qemu_event_init(void)
494{
495 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
496 if (!qemu_event_handle) {
497 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
498 return -1;
499 }
500 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
501 return 0;
502}
503
504static void qemu_event_increment(void)
505{
506 if (!SetEvent(qemu_event_handle)) {
507 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
508 GetLastError());
509 exit (1);
510 }
511}
Jan Kiszka9a360852011-02-01 22:15:55 +0100512
Paolo Bonzini712ae482011-03-12 17:44:05 +0100513static int qemu_signal_init(void)
514{
515 return 0;
516}
517
Paolo Bonzini714bd042011-03-12 17:44:06 +0100518static void qemu_kvm_init_cpu_signals(CPUState *env)
519{
520 abort();
521}
522
523static void qemu_tcg_init_cpu_signals(void)
524{
525}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100526#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000527
528#ifndef CONFIG_IOTHREAD
529int qemu_init_main_loop(void)
530{
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100531 int ret;
532
Paolo Bonzini712ae482011-03-12 17:44:05 +0100533 ret = qemu_signal_init();
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100534 if (ret) {
535 return ret;
536 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000537
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100538 qemu_init_sigbus();
539
Blue Swirl296af7c2010-03-29 19:23:50 +0000540 return qemu_event_init();
541}
542
543void qemu_main_loop_start(void)
544{
545}
546
547void qemu_init_vcpu(void *_env)
548{
549 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100550 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000551
552 env->nr_cores = smp_cores;
553 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100554
555 if (kvm_enabled()) {
556 r = kvm_init_vcpu(env);
557 if (r < 0) {
558 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
559 exit(1);
560 }
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100561 qemu_kvm_init_cpu_signals(env);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100562 } else {
563 qemu_tcg_init_cpu_signals();
Jan Kiszka84b49152011-02-01 22:15:50 +0100564 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000565}
566
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100567int qemu_cpu_is_self(void *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000568{
569 return 1;
570}
571
572void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
573{
574 func(data);
575}
576
577void resume_all_vcpus(void)
578{
579}
580
581void pause_all_vcpus(void)
582{
583}
584
585void qemu_cpu_kick(void *env)
586{
Blue Swirl296af7c2010-03-29 19:23:50 +0000587}
588
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100589void qemu_cpu_kick_self(void)
590{
591#ifndef _WIN32
592 assert(cpu_single_env);
593
594 raise(SIG_IPI);
595#else
596 abort();
597#endif
598}
599
Blue Swirl296af7c2010-03-29 19:23:50 +0000600void qemu_notify_event(void)
601{
602 CPUState *env = cpu_single_env;
603
604 qemu_event_increment ();
605 if (env) {
606 cpu_exit(env);
607 }
608 if (next_cpu && env != next_cpu) {
609 cpu_exit(next_cpu);
610 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100611 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000612}
613
614void qemu_mutex_lock_iothread(void) {}
615void qemu_mutex_unlock_iothread(void) {}
616
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100617void cpu_stop_current(void)
618{
619}
620
Blue Swirl296af7c2010-03-29 19:23:50 +0000621void vm_stop(int reason)
622{
623 do_vm_stop(reason);
624}
625
626#else /* CONFIG_IOTHREAD */
627
Blue Swirl296af7c2010-03-29 19:23:50 +0000628QemuMutex qemu_global_mutex;
629static QemuMutex qemu_fair_mutex;
630
631static QemuThread io_thread;
632
633static QemuThread *tcg_cpu_thread;
634static QemuCond *tcg_halt_cond;
635
636static int qemu_system_ready;
637/* cpu creation */
638static QemuCond qemu_cpu_cond;
639/* system init */
640static QemuCond qemu_system_cond;
641static QemuCond qemu_pause_cond;
642static QemuCond qemu_work_cond;
643
Blue Swirl296af7c2010-03-29 19:23:50 +0000644int qemu_init_main_loop(void)
645{
646 int ret;
647
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100648 qemu_init_sigbus();
Jan Kiszka3c638d02010-06-25 16:56:56 +0200649
Paolo Bonzini712ae482011-03-12 17:44:05 +0100650 ret = qemu_signal_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100651 if (ret) {
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300652 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100653 }
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300654
655 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000656 ret = qemu_event_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100657 if (ret) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000658 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100659 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000660
Anthony Liguoried945922011-02-08 18:18:18 +0100661 qemu_cond_init(&qemu_cpu_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200662 qemu_cond_init(&qemu_system_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100663 qemu_cond_init(&qemu_pause_cond);
664 qemu_cond_init(&qemu_work_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000665 qemu_mutex_init(&qemu_fair_mutex);
666 qemu_mutex_init(&qemu_global_mutex);
667 qemu_mutex_lock(&qemu_global_mutex);
668
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100669 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000670
671 return 0;
672}
673
Blue Swirl7277e022010-04-12 17:19:06 +0000674void qemu_main_loop_start(void)
675{
676 qemu_system_ready = 1;
677 qemu_cond_broadcast(&qemu_system_cond);
678}
679
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300680void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
681{
682 struct qemu_work_item wi;
683
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100684 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300685 func(data);
686 return;
687 }
688
689 wi.func = func;
690 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100691 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300692 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100693 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300694 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100695 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300696 env->queued_work_last = &wi;
697 wi.next = NULL;
698 wi.done = false;
699
700 qemu_cpu_kick(env);
701 while (!wi.done) {
702 CPUState *self_env = cpu_single_env;
703
704 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
705 cpu_single_env = self_env;
706 }
707}
708
709static void flush_queued_work(CPUState *env)
710{
711 struct qemu_work_item *wi;
712
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100713 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300714 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100715 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300716
717 while ((wi = env->queued_work_first)) {
718 env->queued_work_first = wi->next;
719 wi->func(wi->data);
720 wi->done = true;
721 }
722 env->queued_work_last = NULL;
723 qemu_cond_broadcast(&qemu_work_cond);
724}
725
Blue Swirl296af7c2010-03-29 19:23:50 +0000726static void qemu_wait_io_event_common(CPUState *env)
727{
728 if (env->stop) {
729 env->stop = 0;
730 env->stopped = 1;
731 qemu_cond_signal(&qemu_pause_cond);
732 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300733 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100734 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000735}
736
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200737static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000738{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200739 CPUState *env;
740
Jan Kiszka16400322011-02-09 16:29:37 +0100741 while (all_cpu_threads_idle()) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100742 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100743 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000744
745 qemu_mutex_unlock(&qemu_global_mutex);
746
747 /*
748 * Users of qemu_global_mutex can be starved, having no chance
749 * to acquire it since this path will get to it first.
750 * So use another lock to provide fairness.
751 */
752 qemu_mutex_lock(&qemu_fair_mutex);
753 qemu_mutex_unlock(&qemu_fair_mutex);
754
755 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200756
757 for (env = first_cpu; env != NULL; env = env->next_cpu) {
758 qemu_wait_io_event_common(env);
759 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000760}
761
Blue Swirl296af7c2010-03-29 19:23:50 +0000762static void qemu_kvm_wait_io_event(CPUState *env)
763{
Jan Kiszka16400322011-02-09 16:29:37 +0100764 while (cpu_thread_is_idle(env)) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100765 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100766 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000767
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100768 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000769 qemu_wait_io_event_common(env);
770}
771
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100772static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000773{
774 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100775 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000776
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300777 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100778 qemu_thread_get_self(env->thread);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100779 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000780
Jan Kiszka84b49152011-02-01 22:15:50 +0100781 r = kvm_init_vcpu(env);
782 if (r < 0) {
783 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
784 exit(1);
785 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000786
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100787 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000788
789 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000790 env->created = 1;
791 qemu_cond_signal(&qemu_cpu_cond);
792
793 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100794 while (!qemu_system_ready) {
Paolo Bonzinie0098942011-03-12 17:44:01 +0100795 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100796 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000797
798 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100799 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100800 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100801 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100802 cpu_handle_guest_debug(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100803 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100804 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000805 qemu_kvm_wait_io_event(env);
806 }
807
808 return NULL;
809}
810
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100811static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000812{
813 CPUState *env = arg;
814
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100815 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100816 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000817
818 /* signal CPU creation */
819 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100820 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100821 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000822 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100823 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000824 qemu_cond_signal(&qemu_cpu_cond);
825
826 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100827 while (!qemu_system_ready) {
Paolo Bonzinie0098942011-03-12 17:44:01 +0100828 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100829 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000830
831 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200832 cpu_exec_all();
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 Kiszkaaa2c3642011-02-01 22:15:42 +0100863 if (!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 {
895 qemu_mutex_lock(&qemu_fair_mutex);
896 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 }
900 qemu_mutex_unlock(&qemu_fair_mutex);
901 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000902}
903
904void qemu_mutex_unlock_iothread(void)
905{
906 qemu_mutex_unlock(&qemu_global_mutex);
907}
908
909static int all_vcpus_paused(void)
910{
911 CPUState *penv = first_cpu;
912
913 while (penv) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100914 if (!penv->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000915 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100916 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000917 penv = (CPUState *)penv->next_cpu;
918 }
919
920 return 1;
921}
922
923void pause_all_vcpus(void)
924{
925 CPUState *penv = first_cpu;
926
927 while (penv) {
928 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000929 qemu_cpu_kick(penv);
930 penv = (CPUState *)penv->next_cpu;
931 }
932
933 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100934 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000935 penv = first_cpu;
936 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300937 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000938 penv = (CPUState *)penv->next_cpu;
939 }
940 }
941}
942
943void resume_all_vcpus(void)
944{
945 CPUState *penv = first_cpu;
946
947 while (penv) {
948 penv->stop = 0;
949 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000950 qemu_cpu_kick(penv);
951 penv = (CPUState *)penv->next_cpu;
952 }
953}
954
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100955static void qemu_tcg_init_vcpu(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000956{
957 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100958
Blue Swirl296af7c2010-03-29 19:23:50 +0000959 /* share a single thread for all cpus with TCG */
960 if (!tcg_cpu_thread) {
961 env->thread = qemu_mallocz(sizeof(QemuThread));
962 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
963 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100964 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100965 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100966 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100967 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000968 tcg_cpu_thread = env->thread;
969 tcg_halt_cond = env->halt_cond;
970 } else {
971 env->thread = tcg_cpu_thread;
972 env->halt_cond = tcg_halt_cond;
973 }
974}
975
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100976static void qemu_kvm_start_vcpu(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000977{
978 env->thread = qemu_mallocz(sizeof(QemuThread));
979 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
980 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100981 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100982 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100983 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100984 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000985}
986
987void qemu_init_vcpu(void *_env)
988{
989 CPUState *env = _env;
990
991 env->nr_cores = smp_cores;
992 env->nr_threads = smp_threads;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100993 if (kvm_enabled()) {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100994 qemu_kvm_start_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100995 } else {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100996 qemu_tcg_init_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100997 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000998}
999
1000void qemu_notify_event(void)
1001{
1002 qemu_event_increment();
1003}
1004
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001005void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001006{
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001007 if (cpu_single_env) {
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001008 cpu_single_env->stop = 0;
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001009 cpu_single_env->stopped = 1;
1010 cpu_exit(cpu_single_env);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001011 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001012 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001013}
1014
1015void vm_stop(int reason)
1016{
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001017 if (!qemu_thread_is_self(&io_thread)) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001018 qemu_system_vmstop_request(reason);
1019 /*
1020 * FIXME: should not return to device code in case
1021 * vm_stop() has been requested.
1022 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001023 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +00001024 return;
1025 }
1026 do_vm_stop(reason);
1027}
1028
1029#endif
1030
Jan Kiszka6792a572011-02-07 12:19:18 +01001031static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001032{
1033 int ret;
1034#ifdef CONFIG_PROFILER
1035 int64_t ti;
1036#endif
1037
1038#ifdef CONFIG_PROFILER
1039 ti = profile_getclock();
1040#endif
1041 if (use_icount) {
1042 int64_t count;
1043 int decr;
1044 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1045 env->icount_decr.u16.low = 0;
1046 env->icount_extra = 0;
1047 count = qemu_icount_round (qemu_next_deadline());
1048 qemu_icount += count;
1049 decr = (count > 0xffff) ? 0xffff : count;
1050 count -= decr;
1051 env->icount_decr.u16.low = decr;
1052 env->icount_extra = count;
1053 }
1054 ret = cpu_exec(env);
1055#ifdef CONFIG_PROFILER
1056 qemu_time += profile_getclock() - ti;
1057#endif
1058 if (use_icount) {
1059 /* Fold pending instructions back into the
1060 instruction counter, and clear the interrupt flag. */
1061 qemu_icount -= (env->icount_decr.u16.low
1062 + env->icount_extra);
1063 env->icount_decr.u32 = 0;
1064 env->icount_extra = 0;
1065 }
1066 return ret;
1067}
1068
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001069bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001070{
Jan Kiszka9a360852011-02-01 22:15:55 +01001071 int r;
1072
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001073 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001074 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001075 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001076 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +02001077 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001078
1079 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +02001080 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001081
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001082#ifndef CONFIG_IOTHREAD
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001083 if (qemu_alarm_pending()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001084 break;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001085 }
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001086#endif
Jan Kiszka3c638d02010-06-25 16:56:56 +02001087 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +01001088 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +01001089 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001090 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +01001091 } else {
1092 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001093 }
1094 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +01001095 cpu_handle_guest_debug(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001096 break;
1097 }
Paolo Bonzinidf646df2011-03-12 17:43:58 +01001098 } else if (env->stop || env->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001099 break;
1100 }
1101 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001102 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +01001103 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +00001104}
1105
1106void set_numa_modes(void)
1107{
1108 CPUState *env;
1109 int i;
1110
1111 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1112 for (i = 0; i < nb_numa_nodes; i++) {
1113 if (node_cpumask[i] & (1 << env->cpu_index)) {
1114 env->numa_node = i;
1115 }
1116 }
1117 }
1118}
1119
1120void set_cpu_log(const char *optarg)
1121{
1122 int mask;
1123 const CPULogItem *item;
1124
1125 mask = cpu_str_to_log_mask(optarg);
1126 if (!mask) {
1127 printf("Log items (comma separated):\n");
1128 for (item = cpu_log_items; item->mask != 0; item++) {
1129 printf("%-10s %s\n", item->name, item->help);
1130 }
1131 exit(1);
1132 }
1133 cpu_set_log(mask);
1134}
Blue Swirl29e922b2010-03-29 19:24:00 +00001135
1136/* Return the virtual CPU time, based on the instruction counter. */
1137int64_t cpu_get_icount(void)
1138{
1139 int64_t icount;
1140 CPUState *env = cpu_single_env;;
1141
1142 icount = qemu_icount;
1143 if (env) {
1144 if (!can_do_io(env)) {
1145 fprintf(stderr, "Bad clock read\n");
1146 }
1147 icount -= (env->icount_decr.u16.low + env->icount_extra);
1148 }
1149 return qemu_icount_bias + (icount << icount_time_shift);
1150}
Blue Swirl262353c2010-05-04 19:55:35 +00001151
Stefan Weil9a78eea2010-10-22 23:03:33 +02001152void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001153{
1154 /* XXX: implement xxx_cpu_list for targets that still miss it */
1155#if defined(cpu_list_id)
1156 cpu_list_id(f, cpu_fprintf, optarg);
1157#elif defined(cpu_list)
1158 cpu_list(f, cpu_fprintf); /* deprecated */
1159#endif
1160}