blob: 09ce6fe9d36d7c362920cb882a99ec835b9c01da [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
35#include "cpus.h"
Marcelo Tosattia8486bc2010-10-11 15:31:16 -030036#include "compatfd.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000037
Blue Swirl7277e022010-04-12 17:19:06 +000038#ifdef SIGRTMIN
39#define SIG_IPI (SIGRTMIN+4)
40#else
41#define SIG_IPI SIGUSR1
42#endif
43
Jan Kiszka6d9cb732011-02-01 22:15:58 +010044#ifdef CONFIG_LINUX
45
46#include <sys/prctl.h>
47
Marcelo Tosattic0532a72010-10-11 15:31:21 -030048#ifndef PR_MCE_KILL
49#define PR_MCE_KILL 33
50#endif
51
Jan Kiszka6d9cb732011-02-01 22:15:58 +010052#ifndef PR_MCE_KILL_SET
53#define PR_MCE_KILL_SET 1
54#endif
55
56#ifndef PR_MCE_KILL_EARLY
57#define PR_MCE_KILL_EARLY 1
58#endif
59
60#endif /* CONFIG_LINUX */
61
Blue Swirl296af7c2010-03-29 19:23:50 +000062static CPUState *next_cpu;
63
64/***********************************************************/
65void hw_error(const char *fmt, ...)
66{
67 va_list ap;
68 CPUState *env;
69
70 va_start(ap, fmt);
71 fprintf(stderr, "qemu: hardware error: ");
72 vfprintf(stderr, fmt, ap);
73 fprintf(stderr, "\n");
74 for(env = first_cpu; env != NULL; env = env->next_cpu) {
75 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
76#ifdef TARGET_I386
77 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
78#else
79 cpu_dump_state(env, stderr, fprintf, 0);
80#endif
81 }
82 va_end(ap);
83 abort();
84}
85
86void cpu_synchronize_all_states(void)
87{
88 CPUState *cpu;
89
90 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
91 cpu_synchronize_state(cpu);
92 }
93}
94
95void cpu_synchronize_all_post_reset(void)
96{
97 CPUState *cpu;
98
99 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
100 cpu_synchronize_post_reset(cpu);
101 }
102}
103
104void cpu_synchronize_all_post_init(void)
105{
106 CPUState *cpu;
107
108 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
109 cpu_synchronize_post_init(cpu);
110 }
111}
112
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300113int cpu_is_stopped(CPUState *env)
114{
115 return !vm_running || env->stopped;
116}
117
Blue Swirl296af7c2010-03-29 19:23:50 +0000118static void do_vm_stop(int reason)
119{
120 if (vm_running) {
121 cpu_disable_ticks();
122 vm_running = 0;
123 pause_all_vcpus();
124 vm_state_notify(0, reason);
Michael S. Tsirkin55df6f32010-11-22 19:52:22 +0200125 qemu_aio_flush();
126 bdrv_flush_all();
Blue Swirl296af7c2010-03-29 19:23:50 +0000127 monitor_protocol_event(QEVENT_STOP, NULL);
128 }
129}
130
131static int cpu_can_run(CPUState *env)
132{
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100133 if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000134 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100135 }
136 if (env->stopped || !vm_running) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000137 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100138 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000139 return 1;
140}
141
Jan Kiszka16400322011-02-09 16:29:37 +0100142static bool cpu_thread_is_idle(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000143{
Jan Kiszka16400322011-02-09 16:29:37 +0100144 if (env->stop || env->queued_work_first) {
145 return false;
146 }
147 if (env->stopped || !vm_running) {
148 return true;
149 }
150 if (!env->halted || qemu_cpu_has_work(env)) {
151 return false;
152 }
153 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000154}
155
Jan Kiszka16400322011-02-09 16:29:37 +0100156static bool all_cpu_threads_idle(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000157{
158 CPUState *env;
159
Jan Kiszka16400322011-02-09 16:29:37 +0100160 for (env = first_cpu; env != NULL; env = env->next_cpu) {
161 if (!cpu_thread_is_idle(env)) {
162 return false;
163 }
164 }
165 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000166}
167
Jan Kiszka83f338f2011-02-07 12:19:17 +0100168static CPUDebugExcpHandler *debug_excp_handler;
169
170CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200171{
Jan Kiszka83f338f2011-02-07 12:19:17 +0100172 CPUDebugExcpHandler *old_handler = debug_excp_handler;
173
174 debug_excp_handler = handler;
175 return old_handler;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200176}
177
Jan Kiszka83f338f2011-02-07 12:19:17 +0100178static void cpu_handle_debug_exception(CPUState *env)
179{
180 CPUWatchpoint *wp;
181
182 if (!env->watchpoint_hit) {
183 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
184 wp->flags &= ~BP_WATCHPOINT_HIT;
185 }
186 }
187 if (debug_excp_handler) {
188 debug_excp_handler(env);
189 }
190
Blue Swirl296af7c2010-03-29 19:23:50 +0000191 gdb_set_stop_cpu(env);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100192 qemu_system_debug_request();
Jan Kiszka83f338f2011-02-07 12:19:17 +0100193#ifdef CONFIG_IOTHREAD
194 env->stopped = 1;
195#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000196}
197
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100198#ifdef CONFIG_LINUX
199static void sigbus_reraise(void)
200{
201 sigset_t set;
202 struct sigaction action;
203
204 memset(&action, 0, sizeof(action));
205 action.sa_handler = SIG_DFL;
206 if (!sigaction(SIGBUS, &action, NULL)) {
207 raise(SIGBUS);
208 sigemptyset(&set);
209 sigaddset(&set, SIGBUS);
210 sigprocmask(SIG_UNBLOCK, &set, NULL);
211 }
212 perror("Failed to re-raise SIGBUS!\n");
213 abort();
214}
215
216static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
217 void *ctx)
218{
219 if (kvm_on_sigbus(siginfo->ssi_code,
220 (void *)(intptr_t)siginfo->ssi_addr)) {
221 sigbus_reraise();
222 }
223}
224
225static void qemu_init_sigbus(void)
226{
227 struct sigaction action;
228
229 memset(&action, 0, sizeof(action));
230 action.sa_flags = SA_SIGINFO;
231 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
232 sigaction(SIGBUS, &action, NULL);
233
234 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
235}
236
237#else /* !CONFIG_LINUX */
238
239static void qemu_init_sigbus(void)
240{
241}
242#endif /* !CONFIG_LINUX */
243
Blue Swirl296af7c2010-03-29 19:23:50 +0000244#ifndef _WIN32
245static int io_thread_fd = -1;
246
247static void qemu_event_increment(void)
248{
249 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000250 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000251 ssize_t ret;
252
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100253 if (io_thread_fd == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000254 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100255 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000256 do {
257 ret = write(io_thread_fd, &val, sizeof(val));
258 } while (ret < 0 && errno == EINTR);
259
260 /* EAGAIN is fine, a read must be pending. */
261 if (ret < 0 && errno != EAGAIN) {
262 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
263 strerror(errno));
264 exit (1);
265 }
266}
267
268static void qemu_event_read(void *opaque)
269{
270 int fd = (unsigned long)opaque;
271 ssize_t len;
272 char buffer[512];
273
274 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
275 do {
276 len = read(fd, buffer, sizeof(buffer));
277 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
278}
279
280static int qemu_event_init(void)
281{
282 int err;
283 int fds[2];
284
285 err = qemu_eventfd(fds);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100286 if (err == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000287 return -errno;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100288 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000289 err = fcntl_setfl(fds[0], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100290 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000291 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100292 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000293 err = fcntl_setfl(fds[1], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100294 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000295 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100296 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000297 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
298 (void *)(unsigned long)fds[0]);
299
300 io_thread_fd = fds[1];
301 return 0;
302
303fail:
304 close(fds[0]);
305 close(fds[1]);
306 return err;
307}
Blue Swirl296af7c2010-03-29 19:23:50 +0000308
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100309static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000310{
311}
312
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300313/* If we have signalfd, we mask out the signals we want to handle and then
314 * use signalfd to listen for them. We rely on whatever the current signal
315 * handler is to dispatch the signals when we receive them.
316 */
317static void sigfd_handler(void *opaque)
318{
319 int fd = (unsigned long) opaque;
320 struct qemu_signalfd_siginfo info;
321 struct sigaction action;
322 ssize_t len;
323
324 while (1) {
325 do {
326 len = read(fd, &info, sizeof(info));
327 } while (len == -1 && errno == EINTR);
328
329 if (len == -1 && errno == EAGAIN) {
330 break;
331 }
332
333 if (len != sizeof(info)) {
334 printf("read from sigfd returned %zd: %m\n", len);
335 return;
336 }
337
338 sigaction(info.ssi_signo, NULL, &action);
339 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
340 action.sa_sigaction(info.ssi_signo,
341 (siginfo_t *)&info, NULL);
342 } else if (action.sa_handler) {
343 action.sa_handler(info.ssi_signo);
344 }
345 }
346}
347
348static int qemu_signalfd_init(sigset_t mask)
349{
350 int sigfd;
351
352 sigfd = qemu_signalfd(&mask);
353 if (sigfd == -1) {
354 fprintf(stderr, "failed to create signalfd\n");
355 return -errno;
356 }
357
358 fcntl_setfl(sigfd, O_NONBLOCK);
359
360 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
361 (void *)(unsigned long) sigfd);
362
363 return 0;
364}
Blue Swirl296af7c2010-03-29 19:23:50 +0000365
Jan Kiszka9a360852011-02-01 22:15:55 +0100366static void qemu_kvm_eat_signals(CPUState *env)
367{
368 struct timespec ts = { 0, 0 };
369 siginfo_t siginfo;
370 sigset_t waitset;
371 sigset_t chkset;
372 int r;
373
374 sigemptyset(&waitset);
375 sigaddset(&waitset, SIG_IPI);
376 sigaddset(&waitset, SIGBUS);
377
378 do {
379 r = sigtimedwait(&waitset, &siginfo, &ts);
380 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
381 perror("sigtimedwait");
382 exit(1);
383 }
384
385 switch (r) {
Jan Kiszka9a360852011-02-01 22:15:55 +0100386 case SIGBUS:
387 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
388 sigbus_reraise();
389 }
390 break;
Jan Kiszka9a360852011-02-01 22:15:55 +0100391 default:
392 break;
393 }
394
395 r = sigpending(&chkset);
396 if (r == -1) {
397 perror("sigpending");
398 exit(1);
399 }
400 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszkade758972011-02-01 22:15:57 +0100401
402#ifndef CONFIG_IOTHREAD
403 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
404 qemu_notify_event();
405 }
406#endif
Jan Kiszka9a360852011-02-01 22:15:55 +0100407}
408
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100409#else /* _WIN32 */
410
Blue Swirl296af7c2010-03-29 19:23:50 +0000411HANDLE qemu_event_handle;
412
413static void dummy_event_handler(void *opaque)
414{
415}
416
417static int qemu_event_init(void)
418{
419 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
420 if (!qemu_event_handle) {
421 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
422 return -1;
423 }
424 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
425 return 0;
426}
427
428static void qemu_event_increment(void)
429{
430 if (!SetEvent(qemu_event_handle)) {
431 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
432 GetLastError());
433 exit (1);
434 }
435}
Jan Kiszka9a360852011-02-01 22:15:55 +0100436
437static void qemu_kvm_eat_signals(CPUState *env)
438{
439}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100440#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000441
442#ifndef CONFIG_IOTHREAD
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100443static void qemu_kvm_init_cpu_signals(CPUState *env)
444{
445#ifndef _WIN32
446 int r;
447 sigset_t set;
448 struct sigaction sigact;
449
450 memset(&sigact, 0, sizeof(sigact));
451 sigact.sa_handler = dummy_signal;
452 sigaction(SIG_IPI, &sigact, NULL);
453
454 sigemptyset(&set);
455 sigaddset(&set, SIG_IPI);
Jan Kiszkade758972011-02-01 22:15:57 +0100456 sigaddset(&set, SIGIO);
457 sigaddset(&set, SIGALRM);
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100458 pthread_sigmask(SIG_BLOCK, &set, NULL);
459
460 pthread_sigmask(SIG_BLOCK, NULL, &set);
461 sigdelset(&set, SIG_IPI);
462 sigdelset(&set, SIGBUS);
Jan Kiszkade758972011-02-01 22:15:57 +0100463 sigdelset(&set, SIGIO);
464 sigdelset(&set, SIGALRM);
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100465 r = kvm_set_signal_mask(env, &set);
466 if (r) {
467 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
468 exit(1);
469 }
470#endif
471}
472
Jan Kiszkade758972011-02-01 22:15:57 +0100473#ifndef _WIN32
474static sigset_t block_synchronous_signals(void)
475{
476 sigset_t set;
477
478 sigemptyset(&set);
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100479 sigaddset(&set, SIGBUS);
Jan Kiszkade758972011-02-01 22:15:57 +0100480 if (kvm_enabled()) {
481 /*
482 * We need to process timer signals synchronously to avoid a race
483 * between exit_request check and KVM vcpu entry.
484 */
485 sigaddset(&set, SIGIO);
486 sigaddset(&set, SIGALRM);
487 }
488
489 return set;
490}
491#endif
492
Blue Swirl296af7c2010-03-29 19:23:50 +0000493int qemu_init_main_loop(void)
494{
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100495#ifndef _WIN32
496 sigset_t blocked_signals;
497 int ret;
498
Jan Kiszkade758972011-02-01 22:15:57 +0100499 blocked_signals = block_synchronous_signals();
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100500
501 ret = qemu_signalfd_init(blocked_signals);
502 if (ret) {
503 return ret;
504 }
505#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000506
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100507 qemu_init_sigbus();
508
Blue Swirl296af7c2010-03-29 19:23:50 +0000509 return qemu_event_init();
510}
511
512void qemu_main_loop_start(void)
513{
514}
515
516void qemu_init_vcpu(void *_env)
517{
518 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100519 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000520
521 env->nr_cores = smp_cores;
522 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100523
524 if (kvm_enabled()) {
525 r = kvm_init_vcpu(env);
526 if (r < 0) {
527 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
528 exit(1);
529 }
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100530 qemu_kvm_init_cpu_signals(env);
Jan Kiszka84b49152011-02-01 22:15:50 +0100531 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000532}
533
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100534int qemu_cpu_is_self(void *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000535{
536 return 1;
537}
538
539void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
540{
541 func(data);
542}
543
544void resume_all_vcpus(void)
545{
546}
547
548void pause_all_vcpus(void)
549{
550}
551
552void qemu_cpu_kick(void *env)
553{
Blue Swirl296af7c2010-03-29 19:23:50 +0000554}
555
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100556void qemu_cpu_kick_self(void)
557{
558#ifndef _WIN32
559 assert(cpu_single_env);
560
561 raise(SIG_IPI);
562#else
563 abort();
564#endif
565}
566
Blue Swirl296af7c2010-03-29 19:23:50 +0000567void qemu_notify_event(void)
568{
569 CPUState *env = cpu_single_env;
570
571 qemu_event_increment ();
572 if (env) {
573 cpu_exit(env);
574 }
575 if (next_cpu && env != next_cpu) {
576 cpu_exit(next_cpu);
577 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100578 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000579}
580
581void qemu_mutex_lock_iothread(void) {}
582void qemu_mutex_unlock_iothread(void) {}
583
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100584void cpu_stop_current(void)
585{
586}
587
Blue Swirl296af7c2010-03-29 19:23:50 +0000588void vm_stop(int reason)
589{
590 do_vm_stop(reason);
591}
592
593#else /* CONFIG_IOTHREAD */
594
595#include "qemu-thread.h"
596
597QemuMutex qemu_global_mutex;
598static QemuMutex qemu_fair_mutex;
599
600static QemuThread io_thread;
601
602static QemuThread *tcg_cpu_thread;
603static QemuCond *tcg_halt_cond;
604
605static int qemu_system_ready;
606/* cpu creation */
607static QemuCond qemu_cpu_cond;
608/* system init */
609static QemuCond qemu_system_cond;
610static QemuCond qemu_pause_cond;
611static QemuCond qemu_work_cond;
612
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100613static void cpu_signal(int sig)
614{
615 if (cpu_single_env) {
616 cpu_exit(cpu_single_env);
617 }
618 exit_request = 1;
619}
620
621static void qemu_kvm_init_cpu_signals(CPUState *env)
622{
623 int r;
624 sigset_t set;
625 struct sigaction sigact;
626
627 memset(&sigact, 0, sizeof(sigact));
628 sigact.sa_handler = dummy_signal;
629 sigaction(SIG_IPI, &sigact, NULL);
630
631 pthread_sigmask(SIG_BLOCK, NULL, &set);
632 sigdelset(&set, SIG_IPI);
633 sigdelset(&set, SIGBUS);
634 r = kvm_set_signal_mask(env, &set);
635 if (r) {
636 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
637 exit(1);
638 }
639}
640
641static void qemu_tcg_init_cpu_signals(void)
642{
643 sigset_t set;
644 struct sigaction sigact;
645
646 memset(&sigact, 0, sizeof(sigact));
647 sigact.sa_handler = cpu_signal;
648 sigaction(SIG_IPI, &sigact, NULL);
649
650 sigemptyset(&set);
651 sigaddset(&set, SIG_IPI);
652 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
653}
654
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100655static sigset_t block_io_signals(void)
656{
657 sigset_t set;
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100658
659 /* SIGUSR2 used by posix-aio-compat.c */
660 sigemptyset(&set);
661 sigaddset(&set, SIGUSR2);
662 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
663
664 sigemptyset(&set);
665 sigaddset(&set, SIGIO);
666 sigaddset(&set, SIGALRM);
667 sigaddset(&set, SIG_IPI);
668 sigaddset(&set, SIGBUS);
669 pthread_sigmask(SIG_BLOCK, &set, NULL);
670
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100671 return set;
672}
673
Blue Swirl296af7c2010-03-29 19:23:50 +0000674int qemu_init_main_loop(void)
675{
676 int ret;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300677 sigset_t blocked_signals;
Blue Swirl296af7c2010-03-29 19:23:50 +0000678
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100679 qemu_init_sigbus();
Jan Kiszka3c638d02010-06-25 16:56:56 +0200680
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300681 blocked_signals = block_io_signals();
682
683 ret = qemu_signalfd_init(blocked_signals);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100684 if (ret) {
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300685 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100686 }
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300687
688 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000689 ret = qemu_event_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100690 if (ret) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000691 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100692 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000693
Anthony Liguoried945922011-02-08 18:18:18 +0100694 qemu_cond_init(&qemu_cpu_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200695 qemu_cond_init(&qemu_system_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100696 qemu_cond_init(&qemu_pause_cond);
697 qemu_cond_init(&qemu_work_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000698 qemu_mutex_init(&qemu_fair_mutex);
699 qemu_mutex_init(&qemu_global_mutex);
700 qemu_mutex_lock(&qemu_global_mutex);
701
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100702 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000703
704 return 0;
705}
706
Blue Swirl7277e022010-04-12 17:19:06 +0000707void qemu_main_loop_start(void)
708{
709 qemu_system_ready = 1;
710 qemu_cond_broadcast(&qemu_system_cond);
711}
712
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300713void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
714{
715 struct qemu_work_item wi;
716
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100717 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300718 func(data);
719 return;
720 }
721
722 wi.func = func;
723 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100724 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300725 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100726 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300727 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100728 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300729 env->queued_work_last = &wi;
730 wi.next = NULL;
731 wi.done = false;
732
733 qemu_cpu_kick(env);
734 while (!wi.done) {
735 CPUState *self_env = cpu_single_env;
736
737 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
738 cpu_single_env = self_env;
739 }
740}
741
742static void flush_queued_work(CPUState *env)
743{
744 struct qemu_work_item *wi;
745
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100746 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300747 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100748 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300749
750 while ((wi = env->queued_work_first)) {
751 env->queued_work_first = wi->next;
752 wi->func(wi->data);
753 wi->done = true;
754 }
755 env->queued_work_last = NULL;
756 qemu_cond_broadcast(&qemu_work_cond);
757}
758
Blue Swirl296af7c2010-03-29 19:23:50 +0000759static void qemu_wait_io_event_common(CPUState *env)
760{
761 if (env->stop) {
762 env->stop = 0;
763 env->stopped = 1;
764 qemu_cond_signal(&qemu_pause_cond);
765 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300766 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100767 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000768}
769
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200770static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000771{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200772 CPUState *env;
773
Jan Kiszka16400322011-02-09 16:29:37 +0100774 while (all_cpu_threads_idle()) {
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200775 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
Jan Kiszka16400322011-02-09 16:29:37 +0100776 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000777
778 qemu_mutex_unlock(&qemu_global_mutex);
779
780 /*
781 * Users of qemu_global_mutex can be starved, having no chance
782 * to acquire it since this path will get to it first.
783 * So use another lock to provide fairness.
784 */
785 qemu_mutex_lock(&qemu_fair_mutex);
786 qemu_mutex_unlock(&qemu_fair_mutex);
787
788 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200789
790 for (env = first_cpu; env != NULL; env = env->next_cpu) {
791 qemu_wait_io_event_common(env);
792 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000793}
794
Blue Swirl296af7c2010-03-29 19:23:50 +0000795static void qemu_kvm_wait_io_event(CPUState *env)
796{
Jan Kiszka16400322011-02-09 16:29:37 +0100797 while (cpu_thread_is_idle(env)) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000798 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
Jan Kiszka16400322011-02-09 16:29:37 +0100799 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000800
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100801 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000802 qemu_wait_io_event_common(env);
803}
804
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100805static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000806{
807 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100808 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000809
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300810 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100811 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000812
Jan Kiszka84b49152011-02-01 22:15:50 +0100813 r = kvm_init_vcpu(env);
814 if (r < 0) {
815 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
816 exit(1);
817 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000818
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100819 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000820
821 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000822 env->created = 1;
823 qemu_cond_signal(&qemu_cpu_cond);
824
825 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100826 while (!qemu_system_ready) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000827 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100828 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000829
830 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100831 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100832 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100833 if (r == EXCP_DEBUG) {
834 cpu_handle_debug_exception(env);
835 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100836 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000837 qemu_kvm_wait_io_event(env);
838 }
839
840 return NULL;
841}
842
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100843static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000844{
845 CPUState *env = arg;
846
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100847 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100848 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000849
850 /* signal CPU creation */
851 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100852 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000853 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100854 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000855 qemu_cond_signal(&qemu_cpu_cond);
856
857 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100858 while (!qemu_system_ready) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000859 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100860 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000861
862 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200863 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200864 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000865 }
866
867 return NULL;
868}
869
870void qemu_cpu_kick(void *_env)
871{
872 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100873
Blue Swirl296af7c2010-03-29 19:23:50 +0000874 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100875 if (!env->thread_kicked) {
876 qemu_thread_signal(env->thread, SIG_IPI);
877 env->thread_kicked = true;
878 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000879}
880
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100881void qemu_cpu_kick_self(void)
882{
883 assert(cpu_single_env);
884
885 if (!cpu_single_env->thread_kicked) {
886 qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
887 cpu_single_env->thread_kicked = true;
888 }
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)) {
905 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
906 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()) {
942 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
943 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) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000974 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
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) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000991 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
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) {
1016 cpu_single_env->stopped = 1;
1017 cpu_exit(cpu_single_env);
1018 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001019}
1020
1021void vm_stop(int reason)
1022{
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001023 if (!qemu_thread_is_self(&io_thread)) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001024 qemu_system_vmstop_request(reason);
1025 /*
1026 * FIXME: should not return to device code in case
1027 * vm_stop() has been requested.
1028 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001029 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +00001030 return;
1031 }
1032 do_vm_stop(reason);
1033}
1034
1035#endif
1036
Jan Kiszka6792a572011-02-07 12:19:18 +01001037static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001038{
1039 int ret;
1040#ifdef CONFIG_PROFILER
1041 int64_t ti;
1042#endif
1043
1044#ifdef CONFIG_PROFILER
1045 ti = profile_getclock();
1046#endif
1047 if (use_icount) {
1048 int64_t count;
1049 int decr;
1050 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1051 env->icount_decr.u16.low = 0;
1052 env->icount_extra = 0;
1053 count = qemu_icount_round (qemu_next_deadline());
1054 qemu_icount += count;
1055 decr = (count > 0xffff) ? 0xffff : count;
1056 count -= decr;
1057 env->icount_decr.u16.low = decr;
1058 env->icount_extra = count;
1059 }
1060 ret = cpu_exec(env);
1061#ifdef CONFIG_PROFILER
1062 qemu_time += profile_getclock() - ti;
1063#endif
1064 if (use_icount) {
1065 /* Fold pending instructions back into the
1066 instruction counter, and clear the interrupt flag. */
1067 qemu_icount -= (env->icount_decr.u16.low
1068 + env->icount_extra);
1069 env->icount_decr.u32 = 0;
1070 env->icount_extra = 0;
1071 }
1072 return ret;
1073}
1074
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001075bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001076{
Jan Kiszka9a360852011-02-01 22:15:55 +01001077 int r;
1078
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001079 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001080 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001081 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001082 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +02001083 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001084
1085 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +02001086 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001087
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001088 if (qemu_alarm_pending()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001089 break;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001090 }
Jan Kiszka3c638d02010-06-25 16:56:56 +02001091 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +01001092 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +01001093 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001094 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +01001095 } else {
1096 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001097 }
1098 if (r == EXCP_DEBUG) {
Jan Kiszka83f338f2011-02-07 12:19:17 +01001099 cpu_handle_debug_exception(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001100 break;
1101 }
1102 } else if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001103 break;
1104 }
1105 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001106 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +01001107 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +00001108}
1109
1110void set_numa_modes(void)
1111{
1112 CPUState *env;
1113 int i;
1114
1115 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1116 for (i = 0; i < nb_numa_nodes; i++) {
1117 if (node_cpumask[i] & (1 << env->cpu_index)) {
1118 env->numa_node = i;
1119 }
1120 }
1121 }
1122}
1123
1124void set_cpu_log(const char *optarg)
1125{
1126 int mask;
1127 const CPULogItem *item;
1128
1129 mask = cpu_str_to_log_mask(optarg);
1130 if (!mask) {
1131 printf("Log items (comma separated):\n");
1132 for (item = cpu_log_items; item->mask != 0; item++) {
1133 printf("%-10s %s\n", item->name, item->help);
1134 }
1135 exit(1);
1136 }
1137 cpu_set_log(mask);
1138}
Blue Swirl29e922b2010-03-29 19:24:00 +00001139
1140/* Return the virtual CPU time, based on the instruction counter. */
1141int64_t cpu_get_icount(void)
1142{
1143 int64_t icount;
1144 CPUState *env = cpu_single_env;;
1145
1146 icount = qemu_icount;
1147 if (env) {
1148 if (!can_do_io(env)) {
1149 fprintf(stderr, "Bad clock read\n");
1150 }
1151 icount -= (env->icount_decr.u16.low + env->icount_extra);
1152 }
1153 return qemu_icount_bias + (icount << icount_time_shift);
1154}
Blue Swirl262353c2010-05-04 19:55:35 +00001155
Stefan Weil9a78eea2010-10-22 23:03:33 +02001156void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001157{
1158 /* XXX: implement xxx_cpu_list for targets that still miss it */
1159#if defined(cpu_list_id)
1160 cpu_list_id(f, cpu_fprintf, optarg);
1161#elif defined(cpu_list)
1162 cpu_list(f, cpu_fprintf); /* deprecated */
1163#endif
1164}