blob: 7559a02016b0cd6284bc7b7f603205725fa79476 [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 }
151 if (!env->halted || qemu_cpu_has_work(env)) {
152 return false;
153 }
154 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000155}
156
Jan Kiszka16400322011-02-09 16:29:37 +0100157static bool all_cpu_threads_idle(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000158{
159 CPUState *env;
160
Jan Kiszka16400322011-02-09 16:29:37 +0100161 for (env = first_cpu; env != NULL; env = env->next_cpu) {
162 if (!cpu_thread_is_idle(env)) {
163 return false;
164 }
165 }
166 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000167}
168
Jan Kiszka83f338f2011-02-07 12:19:17 +0100169static CPUDebugExcpHandler *debug_excp_handler;
170
171CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200172{
Jan Kiszka83f338f2011-02-07 12:19:17 +0100173 CPUDebugExcpHandler *old_handler = debug_excp_handler;
174
175 debug_excp_handler = handler;
176 return old_handler;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200177}
178
Jan Kiszka83f338f2011-02-07 12:19:17 +0100179static void cpu_handle_debug_exception(CPUState *env)
180{
181 CPUWatchpoint *wp;
182
183 if (!env->watchpoint_hit) {
184 QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
185 wp->flags &= ~BP_WATCHPOINT_HIT;
186 }
187 }
188 if (debug_excp_handler) {
189 debug_excp_handler(env);
190 }
191
Blue Swirl296af7c2010-03-29 19:23:50 +0000192 gdb_set_stop_cpu(env);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100193 qemu_system_debug_request();
Jan Kiszka83f338f2011-02-07 12:19:17 +0100194#ifdef CONFIG_IOTHREAD
195 env->stopped = 1;
196#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000197}
198
Paolo Bonzini714bd042011-03-12 17:44:06 +0100199#ifdef CONFIG_IOTHREAD
200static void cpu_signal(int sig)
201{
202 if (cpu_single_env) {
203 cpu_exit(cpu_single_env);
204 }
205 exit_request = 1;
206}
207#endif
208
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100209#ifdef CONFIG_LINUX
210static void sigbus_reraise(void)
211{
212 sigset_t set;
213 struct sigaction action;
214
215 memset(&action, 0, sizeof(action));
216 action.sa_handler = SIG_DFL;
217 if (!sigaction(SIGBUS, &action, NULL)) {
218 raise(SIGBUS);
219 sigemptyset(&set);
220 sigaddset(&set, SIGBUS);
221 sigprocmask(SIG_UNBLOCK, &set, NULL);
222 }
223 perror("Failed to re-raise SIGBUS!\n");
224 abort();
225}
226
227static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
228 void *ctx)
229{
230 if (kvm_on_sigbus(siginfo->ssi_code,
231 (void *)(intptr_t)siginfo->ssi_addr)) {
232 sigbus_reraise();
233 }
234}
235
236static void qemu_init_sigbus(void)
237{
238 struct sigaction action;
239
240 memset(&action, 0, sizeof(action));
241 action.sa_flags = SA_SIGINFO;
242 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
243 sigaction(SIGBUS, &action, NULL);
244
245 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
246}
247
248#else /* !CONFIG_LINUX */
249
250static void qemu_init_sigbus(void)
251{
252}
253#endif /* !CONFIG_LINUX */
254
Blue Swirl296af7c2010-03-29 19:23:50 +0000255#ifndef _WIN32
256static int io_thread_fd = -1;
257
258static void qemu_event_increment(void)
259{
260 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000261 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000262 ssize_t ret;
263
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100264 if (io_thread_fd == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000265 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100266 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000267 do {
268 ret = write(io_thread_fd, &val, sizeof(val));
269 } while (ret < 0 && errno == EINTR);
270
271 /* EAGAIN is fine, a read must be pending. */
272 if (ret < 0 && errno != EAGAIN) {
273 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
274 strerror(errno));
275 exit (1);
276 }
277}
278
279static void qemu_event_read(void *opaque)
280{
281 int fd = (unsigned long)opaque;
282 ssize_t len;
283 char buffer[512];
284
285 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
286 do {
287 len = read(fd, buffer, sizeof(buffer));
288 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
289}
290
291static int qemu_event_init(void)
292{
293 int err;
294 int fds[2];
295
296 err = qemu_eventfd(fds);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100297 if (err == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000298 return -errno;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100299 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000300 err = fcntl_setfl(fds[0], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100301 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000302 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100303 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000304 err = fcntl_setfl(fds[1], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100305 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000306 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100307 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000308 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
309 (void *)(unsigned long)fds[0]);
310
311 io_thread_fd = fds[1];
312 return 0;
313
314fail:
315 close(fds[0]);
316 close(fds[1]);
317 return err;
318}
Blue Swirl296af7c2010-03-29 19:23:50 +0000319
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100320static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000321{
322}
323
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300324/* If we have signalfd, we mask out the signals we want to handle and then
325 * use signalfd to listen for them. We rely on whatever the current signal
326 * handler is to dispatch the signals when we receive them.
327 */
328static void sigfd_handler(void *opaque)
329{
330 int fd = (unsigned long) opaque;
331 struct qemu_signalfd_siginfo info;
332 struct sigaction action;
333 ssize_t len;
334
335 while (1) {
336 do {
337 len = read(fd, &info, sizeof(info));
338 } while (len == -1 && errno == EINTR);
339
340 if (len == -1 && errno == EAGAIN) {
341 break;
342 }
343
344 if (len != sizeof(info)) {
345 printf("read from sigfd returned %zd: %m\n", len);
346 return;
347 }
348
349 sigaction(info.ssi_signo, NULL, &action);
350 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
351 action.sa_sigaction(info.ssi_signo,
352 (siginfo_t *)&info, NULL);
353 } else if (action.sa_handler) {
354 action.sa_handler(info.ssi_signo);
355 }
356 }
357}
358
Paolo Bonzini712ae482011-03-12 17:44:05 +0100359static int qemu_signal_init(void)
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300360{
361 int sigfd;
Paolo Bonzini712ae482011-03-12 17:44:05 +0100362 sigset_t set;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300363
Paolo Bonzini712ae482011-03-12 17:44:05 +0100364#ifdef CONFIG_IOTHREAD
365 /* SIGUSR2 used by posix-aio-compat.c */
366 sigemptyset(&set);
367 sigaddset(&set, SIGUSR2);
368 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
369
370 sigemptyset(&set);
371 sigaddset(&set, SIGIO);
372 sigaddset(&set, SIGALRM);
373 sigaddset(&set, SIG_IPI);
374 sigaddset(&set, SIGBUS);
375 pthread_sigmask(SIG_BLOCK, &set, NULL);
376#else
377 sigemptyset(&set);
378 sigaddset(&set, SIGBUS);
379 if (kvm_enabled()) {
380 /*
381 * We need to process timer signals synchronously to avoid a race
382 * between exit_request check and KVM vcpu entry.
383 */
384 sigaddset(&set, SIGIO);
385 sigaddset(&set, SIGALRM);
386 }
387#endif
388
389 sigfd = qemu_signalfd(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300390 if (sigfd == -1) {
391 fprintf(stderr, "failed to create signalfd\n");
392 return -errno;
393 }
394
395 fcntl_setfl(sigfd, O_NONBLOCK);
396
397 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
398 (void *)(unsigned long) sigfd);
399
400 return 0;
401}
Blue Swirl296af7c2010-03-29 19:23:50 +0000402
Paolo Bonzini714bd042011-03-12 17:44:06 +0100403static void qemu_kvm_init_cpu_signals(CPUState *env)
404{
405 int r;
406 sigset_t set;
407 struct sigaction sigact;
408
409 memset(&sigact, 0, sizeof(sigact));
410 sigact.sa_handler = dummy_signal;
411 sigaction(SIG_IPI, &sigact, NULL);
412
413#ifdef CONFIG_IOTHREAD
414 pthread_sigmask(SIG_BLOCK, NULL, &set);
415 sigdelset(&set, SIG_IPI);
416 sigdelset(&set, SIGBUS);
417 r = kvm_set_signal_mask(env, &set);
418 if (r) {
419 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
420 exit(1);
421 }
422#else
423 sigemptyset(&set);
424 sigaddset(&set, SIG_IPI);
425 sigaddset(&set, SIGIO);
426 sigaddset(&set, SIGALRM);
427 pthread_sigmask(SIG_BLOCK, &set, NULL);
428
429 pthread_sigmask(SIG_BLOCK, NULL, &set);
430 sigdelset(&set, SIGIO);
431 sigdelset(&set, SIGALRM);
432#endif
433 sigdelset(&set, SIG_IPI);
434 sigdelset(&set, SIGBUS);
435 r = kvm_set_signal_mask(env, &set);
436 if (r) {
437 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
438 exit(1);
439 }
440}
441
442static void qemu_tcg_init_cpu_signals(void)
443{
444#ifdef CONFIG_IOTHREAD
445 sigset_t set;
446 struct sigaction sigact;
447
448 memset(&sigact, 0, sizeof(sigact));
449 sigact.sa_handler = cpu_signal;
450 sigaction(SIG_IPI, &sigact, NULL);
451
452 sigemptyset(&set);
453 sigaddset(&set, SIG_IPI);
454 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
455#endif
456}
457
Jan Kiszka9a360852011-02-01 22:15:55 +0100458static void qemu_kvm_eat_signals(CPUState *env)
459{
460 struct timespec ts = { 0, 0 };
461 siginfo_t siginfo;
462 sigset_t waitset;
463 sigset_t chkset;
464 int r;
465
466 sigemptyset(&waitset);
467 sigaddset(&waitset, SIG_IPI);
468 sigaddset(&waitset, SIGBUS);
469
470 do {
471 r = sigtimedwait(&waitset, &siginfo, &ts);
472 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
473 perror("sigtimedwait");
474 exit(1);
475 }
476
477 switch (r) {
Jan Kiszka9a360852011-02-01 22:15:55 +0100478 case SIGBUS:
479 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
480 sigbus_reraise();
481 }
482 break;
Jan Kiszka9a360852011-02-01 22:15:55 +0100483 default:
484 break;
485 }
486
487 r = sigpending(&chkset);
488 if (r == -1) {
489 perror("sigpending");
490 exit(1);
491 }
492 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszkade758972011-02-01 22:15:57 +0100493
494#ifndef CONFIG_IOTHREAD
495 if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
496 qemu_notify_event();
497 }
498#endif
Jan Kiszka9a360852011-02-01 22:15:55 +0100499}
500
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100501#else /* _WIN32 */
502
Blue Swirl296af7c2010-03-29 19:23:50 +0000503HANDLE qemu_event_handle;
504
505static void dummy_event_handler(void *opaque)
506{
507}
508
509static int qemu_event_init(void)
510{
511 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
512 if (!qemu_event_handle) {
513 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
514 return -1;
515 }
516 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
517 return 0;
518}
519
520static void qemu_event_increment(void)
521{
522 if (!SetEvent(qemu_event_handle)) {
523 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
524 GetLastError());
525 exit (1);
526 }
527}
Jan Kiszka9a360852011-02-01 22:15:55 +0100528
529static void qemu_kvm_eat_signals(CPUState *env)
530{
531}
Paolo Bonzini712ae482011-03-12 17:44:05 +0100532
533static int qemu_signal_init(void)
534{
535 return 0;
536}
537
Paolo Bonzini714bd042011-03-12 17:44:06 +0100538static void qemu_kvm_init_cpu_signals(CPUState *env)
539{
540 abort();
541}
542
543static void qemu_tcg_init_cpu_signals(void)
544{
545}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100546#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000547
548#ifndef CONFIG_IOTHREAD
549int qemu_init_main_loop(void)
550{
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100551 int ret;
552
Paolo Bonzini712ae482011-03-12 17:44:05 +0100553 ret = qemu_signal_init();
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100554 if (ret) {
555 return ret;
556 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000557
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100558 qemu_init_sigbus();
559
Blue Swirl296af7c2010-03-29 19:23:50 +0000560 return qemu_event_init();
561}
562
563void qemu_main_loop_start(void)
564{
565}
566
567void qemu_init_vcpu(void *_env)
568{
569 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100570 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000571
572 env->nr_cores = smp_cores;
573 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100574
575 if (kvm_enabled()) {
576 r = kvm_init_vcpu(env);
577 if (r < 0) {
578 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
579 exit(1);
580 }
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100581 qemu_kvm_init_cpu_signals(env);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100582 } else {
583 qemu_tcg_init_cpu_signals();
Jan Kiszka84b49152011-02-01 22:15:50 +0100584 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000585}
586
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100587int qemu_cpu_is_self(void *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000588{
589 return 1;
590}
591
592void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
593{
594 func(data);
595}
596
597void resume_all_vcpus(void)
598{
599}
600
601void pause_all_vcpus(void)
602{
603}
604
605void qemu_cpu_kick(void *env)
606{
Blue Swirl296af7c2010-03-29 19:23:50 +0000607}
608
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100609void qemu_cpu_kick_self(void)
610{
611#ifndef _WIN32
612 assert(cpu_single_env);
613
614 raise(SIG_IPI);
615#else
616 abort();
617#endif
618}
619
Blue Swirl296af7c2010-03-29 19:23:50 +0000620void qemu_notify_event(void)
621{
622 CPUState *env = cpu_single_env;
623
624 qemu_event_increment ();
625 if (env) {
626 cpu_exit(env);
627 }
628 if (next_cpu && env != next_cpu) {
629 cpu_exit(next_cpu);
630 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100631 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000632}
633
634void qemu_mutex_lock_iothread(void) {}
635void qemu_mutex_unlock_iothread(void) {}
636
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100637void cpu_stop_current(void)
638{
639}
640
Blue Swirl296af7c2010-03-29 19:23:50 +0000641void vm_stop(int reason)
642{
643 do_vm_stop(reason);
644}
645
646#else /* CONFIG_IOTHREAD */
647
Blue Swirl296af7c2010-03-29 19:23:50 +0000648QemuMutex qemu_global_mutex;
649static QemuMutex qemu_fair_mutex;
650
651static QemuThread io_thread;
652
653static QemuThread *tcg_cpu_thread;
654static QemuCond *tcg_halt_cond;
655
656static int qemu_system_ready;
657/* cpu creation */
658static QemuCond qemu_cpu_cond;
659/* system init */
660static QemuCond qemu_system_cond;
661static QemuCond qemu_pause_cond;
662static QemuCond qemu_work_cond;
663
Blue Swirl296af7c2010-03-29 19:23:50 +0000664int qemu_init_main_loop(void)
665{
666 int ret;
667
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100668 qemu_init_sigbus();
Jan Kiszka3c638d02010-06-25 16:56:56 +0200669
Paolo Bonzini712ae482011-03-12 17:44:05 +0100670 ret = qemu_signal_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100671 if (ret) {
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300672 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100673 }
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300674
675 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000676 ret = qemu_event_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100677 if (ret) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000678 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100679 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000680
Anthony Liguoried945922011-02-08 18:18:18 +0100681 qemu_cond_init(&qemu_cpu_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200682 qemu_cond_init(&qemu_system_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100683 qemu_cond_init(&qemu_pause_cond);
684 qemu_cond_init(&qemu_work_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000685 qemu_mutex_init(&qemu_fair_mutex);
686 qemu_mutex_init(&qemu_global_mutex);
687 qemu_mutex_lock(&qemu_global_mutex);
688
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100689 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000690
691 return 0;
692}
693
Blue Swirl7277e022010-04-12 17:19:06 +0000694void qemu_main_loop_start(void)
695{
696 qemu_system_ready = 1;
697 qemu_cond_broadcast(&qemu_system_cond);
698}
699
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300700void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
701{
702 struct qemu_work_item wi;
703
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100704 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300705 func(data);
706 return;
707 }
708
709 wi.func = func;
710 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100711 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300712 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100713 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300714 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100715 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300716 env->queued_work_last = &wi;
717 wi.next = NULL;
718 wi.done = false;
719
720 qemu_cpu_kick(env);
721 while (!wi.done) {
722 CPUState *self_env = cpu_single_env;
723
724 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
725 cpu_single_env = self_env;
726 }
727}
728
729static void flush_queued_work(CPUState *env)
730{
731 struct qemu_work_item *wi;
732
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100733 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300734 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100735 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300736
737 while ((wi = env->queued_work_first)) {
738 env->queued_work_first = wi->next;
739 wi->func(wi->data);
740 wi->done = true;
741 }
742 env->queued_work_last = NULL;
743 qemu_cond_broadcast(&qemu_work_cond);
744}
745
Blue Swirl296af7c2010-03-29 19:23:50 +0000746static void qemu_wait_io_event_common(CPUState *env)
747{
748 if (env->stop) {
749 env->stop = 0;
750 env->stopped = 1;
751 qemu_cond_signal(&qemu_pause_cond);
752 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300753 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100754 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000755}
756
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200757static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000758{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200759 CPUState *env;
760
Jan Kiszka16400322011-02-09 16:29:37 +0100761 while (all_cpu_threads_idle()) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100762 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100763 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000764
765 qemu_mutex_unlock(&qemu_global_mutex);
766
767 /*
768 * Users of qemu_global_mutex can be starved, having no chance
769 * to acquire it since this path will get to it first.
770 * So use another lock to provide fairness.
771 */
772 qemu_mutex_lock(&qemu_fair_mutex);
773 qemu_mutex_unlock(&qemu_fair_mutex);
774
775 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200776
777 for (env = first_cpu; env != NULL; env = env->next_cpu) {
778 qemu_wait_io_event_common(env);
779 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000780}
781
Blue Swirl296af7c2010-03-29 19:23:50 +0000782static void qemu_kvm_wait_io_event(CPUState *env)
783{
Jan Kiszka16400322011-02-09 16:29:37 +0100784 while (cpu_thread_is_idle(env)) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100785 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100786 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000787
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100788 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000789 qemu_wait_io_event_common(env);
790}
791
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100792static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000793{
794 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100795 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000796
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300797 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100798 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000799
Jan Kiszka84b49152011-02-01 22:15:50 +0100800 r = kvm_init_vcpu(env);
801 if (r < 0) {
802 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
803 exit(1);
804 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000805
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100806 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000807
808 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000809 env->created = 1;
810 qemu_cond_signal(&qemu_cpu_cond);
811
812 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100813 while (!qemu_system_ready) {
Paolo Bonzinie0098942011-03-12 17:44:01 +0100814 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100815 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000816
817 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100818 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100819 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100820 if (r == EXCP_DEBUG) {
821 cpu_handle_debug_exception(env);
822 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100823 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000824 qemu_kvm_wait_io_event(env);
825 }
826
827 return NULL;
828}
829
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100830static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000831{
832 CPUState *env = arg;
833
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100834 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100835 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000836
837 /* signal CPU creation */
838 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100839 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000840 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100841 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000842 qemu_cond_signal(&qemu_cpu_cond);
843
844 /* and wait for machine initialization */
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100845 while (!qemu_system_ready) {
Paolo Bonzinie0098942011-03-12 17:44:01 +0100846 qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100847 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000848
849 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200850 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200851 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000852 }
853
854 return NULL;
855}
856
857void qemu_cpu_kick(void *_env)
858{
859 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100860
Blue Swirl296af7c2010-03-29 19:23:50 +0000861 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100862 if (!env->thread_kicked) {
863 qemu_thread_signal(env->thread, SIG_IPI);
864 env->thread_kicked = true;
865 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000866}
867
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100868void qemu_cpu_kick_self(void)
869{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100870#ifndef _WIN32
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100871 assert(cpu_single_env);
872
873 if (!cpu_single_env->thread_kicked) {
874 qemu_thread_signal(cpu_single_env->thread, SIG_IPI);
875 cpu_single_env->thread_kicked = true;
876 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100877#else
878 abort();
879#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000880}
881
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100882int qemu_cpu_is_self(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000883{
884 CPUState *env = _env;
Blue Swirl296af7c2010-03-29 19:23:50 +0000885
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100886 return qemu_thread_is_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000887}
888
Blue Swirl296af7c2010-03-29 19:23:50 +0000889void qemu_mutex_lock_iothread(void)
890{
891 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000892 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300893 } else {
894 qemu_mutex_lock(&qemu_fair_mutex);
895 if (qemu_mutex_trylock(&qemu_global_mutex)) {
896 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
897 qemu_mutex_lock(&qemu_global_mutex);
898 }
899 qemu_mutex_unlock(&qemu_fair_mutex);
900 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000901}
902
903void qemu_mutex_unlock_iothread(void)
904{
905 qemu_mutex_unlock(&qemu_global_mutex);
906}
907
908static int all_vcpus_paused(void)
909{
910 CPUState *penv = first_cpu;
911
912 while (penv) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100913 if (!penv->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000914 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100915 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000916 penv = (CPUState *)penv->next_cpu;
917 }
918
919 return 1;
920}
921
922void pause_all_vcpus(void)
923{
924 CPUState *penv = first_cpu;
925
926 while (penv) {
927 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000928 qemu_cpu_kick(penv);
929 penv = (CPUState *)penv->next_cpu;
930 }
931
932 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100933 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000934 penv = first_cpu;
935 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300936 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000937 penv = (CPUState *)penv->next_cpu;
938 }
939 }
940}
941
942void resume_all_vcpus(void)
943{
944 CPUState *penv = first_cpu;
945
946 while (penv) {
947 penv->stop = 0;
948 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000949 qemu_cpu_kick(penv);
950 penv = (CPUState *)penv->next_cpu;
951 }
952}
953
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100954static void qemu_tcg_init_vcpu(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000955{
956 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100957
Blue Swirl296af7c2010-03-29 19:23:50 +0000958 /* share a single thread for all cpus with TCG */
959 if (!tcg_cpu_thread) {
960 env->thread = qemu_mallocz(sizeof(QemuThread));
961 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
962 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100963 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100964 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100965 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100966 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000967 tcg_cpu_thread = env->thread;
968 tcg_halt_cond = env->halt_cond;
969 } else {
970 env->thread = tcg_cpu_thread;
971 env->halt_cond = tcg_halt_cond;
972 }
973}
974
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100975static void qemu_kvm_start_vcpu(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000976{
977 env->thread = qemu_mallocz(sizeof(QemuThread));
978 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
979 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100980 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100981 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100982 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100983 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000984}
985
986void qemu_init_vcpu(void *_env)
987{
988 CPUState *env = _env;
989
990 env->nr_cores = smp_cores;
991 env->nr_threads = smp_threads;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100992 if (kvm_enabled()) {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100993 qemu_kvm_start_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100994 } else {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100995 qemu_tcg_init_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100996 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000997}
998
999void qemu_notify_event(void)
1000{
1001 qemu_event_increment();
1002}
1003
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001004void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001005{
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001006 if (cpu_single_env) {
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001007 cpu_single_env->stop = 0;
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001008 cpu_single_env->stopped = 1;
1009 cpu_exit(cpu_single_env);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001010 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001011 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001012}
1013
1014void vm_stop(int reason)
1015{
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001016 if (!qemu_thread_is_self(&io_thread)) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001017 qemu_system_vmstop_request(reason);
1018 /*
1019 * FIXME: should not return to device code in case
1020 * vm_stop() has been requested.
1021 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001022 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +00001023 return;
1024 }
1025 do_vm_stop(reason);
1026}
1027
1028#endif
1029
Jan Kiszka6792a572011-02-07 12:19:18 +01001030static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001031{
1032 int ret;
1033#ifdef CONFIG_PROFILER
1034 int64_t ti;
1035#endif
1036
1037#ifdef CONFIG_PROFILER
1038 ti = profile_getclock();
1039#endif
1040 if (use_icount) {
1041 int64_t count;
1042 int decr;
1043 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1044 env->icount_decr.u16.low = 0;
1045 env->icount_extra = 0;
1046 count = qemu_icount_round (qemu_next_deadline());
1047 qemu_icount += count;
1048 decr = (count > 0xffff) ? 0xffff : count;
1049 count -= decr;
1050 env->icount_decr.u16.low = decr;
1051 env->icount_extra = count;
1052 }
1053 ret = cpu_exec(env);
1054#ifdef CONFIG_PROFILER
1055 qemu_time += profile_getclock() - ti;
1056#endif
1057 if (use_icount) {
1058 /* Fold pending instructions back into the
1059 instruction counter, and clear the interrupt flag. */
1060 qemu_icount -= (env->icount_decr.u16.low
1061 + env->icount_extra);
1062 env->icount_decr.u32 = 0;
1063 env->icount_extra = 0;
1064 }
1065 return ret;
1066}
1067
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001068bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001069{
Jan Kiszka9a360852011-02-01 22:15:55 +01001070 int r;
1071
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001072 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001073 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001074 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001075 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +02001076 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001077
1078 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +02001079 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001080
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001081#ifndef CONFIG_IOTHREAD
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001082 if (qemu_alarm_pending()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001083 break;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001084 }
Paolo Bonzini8cf3f222011-03-12 17:44:04 +01001085#endif
Jan Kiszka3c638d02010-06-25 16:56:56 +02001086 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +01001087 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +01001088 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001089 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +01001090 } else {
1091 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001092 }
1093 if (r == EXCP_DEBUG) {
Jan Kiszka83f338f2011-02-07 12:19:17 +01001094 cpu_handle_debug_exception(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001095 break;
1096 }
Paolo Bonzinidf646df2011-03-12 17:43:58 +01001097 } else if (env->stop || env->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001098 break;
1099 }
1100 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001101 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +01001102 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +00001103}
1104
1105void set_numa_modes(void)
1106{
1107 CPUState *env;
1108 int i;
1109
1110 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1111 for (i = 0; i < nb_numa_nodes; i++) {
1112 if (node_cpumask[i] & (1 << env->cpu_index)) {
1113 env->numa_node = i;
1114 }
1115 }
1116 }
1117}
1118
1119void set_cpu_log(const char *optarg)
1120{
1121 int mask;
1122 const CPULogItem *item;
1123
1124 mask = cpu_str_to_log_mask(optarg);
1125 if (!mask) {
1126 printf("Log items (comma separated):\n");
1127 for (item = cpu_log_items; item->mask != 0; item++) {
1128 printf("%-10s %s\n", item->name, item->help);
1129 }
1130 exit(1);
1131 }
1132 cpu_set_log(mask);
1133}
Blue Swirl29e922b2010-03-29 19:24:00 +00001134
1135/* Return the virtual CPU time, based on the instruction counter. */
1136int64_t cpu_get_icount(void)
1137{
1138 int64_t icount;
1139 CPUState *env = cpu_single_env;;
1140
1141 icount = qemu_icount;
1142 if (env) {
1143 if (!can_do_io(env)) {
1144 fprintf(stderr, "Bad clock read\n");
1145 }
1146 icount -= (env->icount_decr.u16.low + env->icount_extra);
1147 }
1148 return qemu_icount_bias + (icount << icount_time_shift);
1149}
Blue Swirl262353c2010-05-04 19:55:35 +00001150
Stefan Weil9a78eea2010-10-22 23:03:33 +02001151void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001152{
1153 /* XXX: implement xxx_cpu_list for targets that still miss it */
1154#if defined(cpu_list_id)
1155 cpu_list_id(f, cpu_fprintf, optarg);
1156#elif defined(cpu_list)
1157 cpu_list(f, cpu_fprintf); /* deprecated */
1158#endif
1159}