blob: 1328baac70f61b78e064e60148db6d21849c98c8 [file] [log] [blame]
Blue Swirl296af7c2010-03-29 19:23:50 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25/* Needed early for CONFIG_BSD etc. */
26#include "config-host.h"
27
28#include "monitor.h"
29#include "sysemu.h"
30#include "gdbstub.h"
31#include "dma.h"
32#include "kvm.h"
33
Paolo Bonzini96284e82011-03-12 17:43:53 +010034#include "qemu-thread.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000035#include "cpus.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020036
37#ifndef _WIN32
Marcelo Tosattia8486bc2010-10-11 15:31:16 -030038#include "compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020039#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000040
Blue Swirl7277e022010-04-12 17:19:06 +000041#ifdef SIGRTMIN
42#define SIG_IPI (SIGRTMIN+4)
43#else
44#define SIG_IPI SIGUSR1
45#endif
46
Jan Kiszka6d9cb732011-02-01 22:15:58 +010047#ifdef CONFIG_LINUX
48
49#include <sys/prctl.h>
50
Marcelo Tosattic0532a72010-10-11 15:31:21 -030051#ifndef PR_MCE_KILL
52#define PR_MCE_KILL 33
53#endif
54
Jan Kiszka6d9cb732011-02-01 22:15:58 +010055#ifndef PR_MCE_KILL_SET
56#define PR_MCE_KILL_SET 1
57#endif
58
59#ifndef PR_MCE_KILL_EARLY
60#define PR_MCE_KILL_EARLY 1
61#endif
62
63#endif /* CONFIG_LINUX */
64
Blue Swirl296af7c2010-03-29 19:23:50 +000065static CPUState *next_cpu;
66
67/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +020068/* guest cycle counter */
69
70/* Conversion factor from emulated instructions to virtual clock ticks. */
71static int icount_time_shift;
72/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
73#define MAX_ICOUNT_SHIFT 10
74/* Compensate for varying guest execution speed. */
75static int64_t qemu_icount_bias;
76static QEMUTimer *icount_rt_timer;
77static QEMUTimer *icount_vm_timer;
78static QEMUTimer *icount_warp_timer;
79static int64_t vm_clock_warp_start;
80static int64_t qemu_icount;
81
82typedef struct TimersState {
83 int64_t cpu_ticks_prev;
84 int64_t cpu_ticks_offset;
85 int64_t cpu_clock_offset;
86 int32_t cpu_ticks_enabled;
87 int64_t dummy;
88} TimersState;
89
90TimersState timers_state;
91
92/* Return the virtual CPU time, based on the instruction counter. */
93int64_t cpu_get_icount(void)
94{
95 int64_t icount;
96 CPUState *env = cpu_single_env;;
97
98 icount = qemu_icount;
99 if (env) {
100 if (!can_do_io(env)) {
101 fprintf(stderr, "Bad clock read\n");
102 }
103 icount -= (env->icount_decr.u16.low + env->icount_extra);
104 }
105 return qemu_icount_bias + (icount << icount_time_shift);
106}
107
108/* return the host CPU cycle counter and handle stop/restart */
109int64_t cpu_get_ticks(void)
110{
111 if (use_icount) {
112 return cpu_get_icount();
113 }
114 if (!timers_state.cpu_ticks_enabled) {
115 return timers_state.cpu_ticks_offset;
116 } else {
117 int64_t ticks;
118 ticks = cpu_get_real_ticks();
119 if (timers_state.cpu_ticks_prev > ticks) {
120 /* Note: non increasing ticks may happen if the host uses
121 software suspend */
122 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
123 }
124 timers_state.cpu_ticks_prev = ticks;
125 return ticks + timers_state.cpu_ticks_offset;
126 }
127}
128
129/* return the host CPU monotonic timer and handle stop/restart */
130int64_t cpu_get_clock(void)
131{
132 int64_t ti;
133 if (!timers_state.cpu_ticks_enabled) {
134 return timers_state.cpu_clock_offset;
135 } else {
136 ti = get_clock();
137 return ti + timers_state.cpu_clock_offset;
138 }
139}
140
141/* enable cpu_get_ticks() */
142void cpu_enable_ticks(void)
143{
144 if (!timers_state.cpu_ticks_enabled) {
145 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
146 timers_state.cpu_clock_offset -= get_clock();
147 timers_state.cpu_ticks_enabled = 1;
148 }
149}
150
151/* disable cpu_get_ticks() : the clock is stopped. You must not call
152 cpu_get_ticks() after that. */
153void cpu_disable_ticks(void)
154{
155 if (timers_state.cpu_ticks_enabled) {
156 timers_state.cpu_ticks_offset = cpu_get_ticks();
157 timers_state.cpu_clock_offset = cpu_get_clock();
158 timers_state.cpu_ticks_enabled = 0;
159 }
160}
161
162/* Correlation between real and virtual time is always going to be
163 fairly approximate, so ignore small variation.
164 When the guest is idle real and virtual time will be aligned in
165 the IO wait loop. */
166#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
167
168static void icount_adjust(void)
169{
170 int64_t cur_time;
171 int64_t cur_icount;
172 int64_t delta;
173 static int64_t last_delta;
174 /* If the VM is not running, then do nothing. */
175 if (!runstate_is_running()) {
176 return;
177 }
178 cur_time = cpu_get_clock();
179 cur_icount = qemu_get_clock_ns(vm_clock);
180 delta = cur_icount - cur_time;
181 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
182 if (delta > 0
183 && last_delta + ICOUNT_WOBBLE < delta * 2
184 && icount_time_shift > 0) {
185 /* The guest is getting too far ahead. Slow time down. */
186 icount_time_shift--;
187 }
188 if (delta < 0
189 && last_delta - ICOUNT_WOBBLE > delta * 2
190 && icount_time_shift < MAX_ICOUNT_SHIFT) {
191 /* The guest is getting too far behind. Speed time up. */
192 icount_time_shift++;
193 }
194 last_delta = delta;
195 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
196}
197
198static void icount_adjust_rt(void *opaque)
199{
200 qemu_mod_timer(icount_rt_timer,
201 qemu_get_clock_ms(rt_clock) + 1000);
202 icount_adjust();
203}
204
205static void icount_adjust_vm(void *opaque)
206{
207 qemu_mod_timer(icount_vm_timer,
208 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
209 icount_adjust();
210}
211
212static int64_t qemu_icount_round(int64_t count)
213{
214 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
215}
216
217static void icount_warp_rt(void *opaque)
218{
219 if (vm_clock_warp_start == -1) {
220 return;
221 }
222
223 if (runstate_is_running()) {
224 int64_t clock = qemu_get_clock_ns(rt_clock);
225 int64_t warp_delta = clock - vm_clock_warp_start;
226 if (use_icount == 1) {
227 qemu_icount_bias += warp_delta;
228 } else {
229 /*
230 * In adaptive mode, do not let the vm_clock run too
231 * far ahead of real time.
232 */
233 int64_t cur_time = cpu_get_clock();
234 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
235 int64_t delta = cur_time - cur_icount;
236 qemu_icount_bias += MIN(warp_delta, delta);
237 }
238 if (qemu_clock_expired(vm_clock)) {
239 qemu_notify_event();
240 }
241 }
242 vm_clock_warp_start = -1;
243}
244
245void qemu_clock_warp(QEMUClock *clock)
246{
247 int64_t deadline;
248
249 /*
250 * There are too many global variables to make the "warp" behavior
251 * applicable to other clocks. But a clock argument removes the
252 * need for if statements all over the place.
253 */
254 if (clock != vm_clock || !use_icount) {
255 return;
256 }
257
258 /*
259 * If the CPUs have been sleeping, advance the vm_clock timer now. This
260 * ensures that the deadline for the timer is computed correctly below.
261 * This also makes sure that the insn counter is synchronized before the
262 * CPU starts running, in case the CPU is woken by an event other than
263 * the earliest vm_clock timer.
264 */
265 icount_warp_rt(NULL);
266 if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock)) {
267 qemu_del_timer(icount_warp_timer);
268 return;
269 }
270
271 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
272 deadline = qemu_clock_deadline(vm_clock);
273 if (deadline > 0) {
274 /*
275 * Ensure the vm_clock proceeds even when the virtual CPU goes to
276 * sleep. Otherwise, the CPU might be waiting for a future timer
277 * interrupt to wake it up, but the interrupt never comes because
278 * the vCPU isn't running any insns and thus doesn't advance the
279 * vm_clock.
280 *
281 * An extreme solution for this problem would be to never let VCPUs
282 * sleep in icount mode if there is a pending vm_clock timer; rather
283 * time could just advance to the next vm_clock event. Instead, we
284 * do stop VCPUs and only advance vm_clock after some "real" time,
285 * (related to the time left until the next event) has passed. This
286 * rt_clock timer will do this. This avoids that the warps are too
287 * visible externally---for example, you will not be sending network
288 * packets continously instead of every 100ms.
289 */
290 qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);
291 } else {
292 qemu_notify_event();
293 }
294}
295
296static const VMStateDescription vmstate_timers = {
297 .name = "timer",
298 .version_id = 2,
299 .minimum_version_id = 1,
300 .minimum_version_id_old = 1,
301 .fields = (VMStateField[]) {
302 VMSTATE_INT64(cpu_ticks_offset, TimersState),
303 VMSTATE_INT64(dummy, TimersState),
304 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
305 VMSTATE_END_OF_LIST()
306 }
307};
308
309void configure_icount(const char *option)
310{
311 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
312 if (!option) {
313 return;
314 }
315
316 icount_warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
317 if (strcmp(option, "auto") != 0) {
318 icount_time_shift = strtol(option, NULL, 0);
319 use_icount = 1;
320 return;
321 }
322
323 use_icount = 2;
324
325 /* 125MIPS seems a reasonable initial guess at the guest speed.
326 It will be corrected fairly quickly anyway. */
327 icount_time_shift = 3;
328
329 /* Have both realtime and virtual time triggers for speed adjustment.
330 The realtime trigger catches emulated time passing too slowly,
331 the virtual time trigger catches emulated time passing too fast.
332 Realtime triggers occur even when idle, so use them less frequently
333 than VM triggers. */
334 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
335 qemu_mod_timer(icount_rt_timer,
336 qemu_get_clock_ms(rt_clock) + 1000);
337 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
338 qemu_mod_timer(icount_vm_timer,
339 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
340}
341
342/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000343void hw_error(const char *fmt, ...)
344{
345 va_list ap;
346 CPUState *env;
347
348 va_start(ap, fmt);
349 fprintf(stderr, "qemu: hardware error: ");
350 vfprintf(stderr, fmt, ap);
351 fprintf(stderr, "\n");
352 for(env = first_cpu; env != NULL; env = env->next_cpu) {
353 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
354#ifdef TARGET_I386
355 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
356#else
357 cpu_dump_state(env, stderr, fprintf, 0);
358#endif
359 }
360 va_end(ap);
361 abort();
362}
363
364void cpu_synchronize_all_states(void)
365{
366 CPUState *cpu;
367
368 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
369 cpu_synchronize_state(cpu);
370 }
371}
372
373void cpu_synchronize_all_post_reset(void)
374{
375 CPUState *cpu;
376
377 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
378 cpu_synchronize_post_reset(cpu);
379 }
380}
381
382void cpu_synchronize_all_post_init(void)
383{
384 CPUState *cpu;
385
386 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
387 cpu_synchronize_post_init(cpu);
388 }
389}
390
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300391int cpu_is_stopped(CPUState *env)
392{
Luiz Capitulino13548692011-07-29 15:36:43 -0300393 return !runstate_is_running() || env->stopped;
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300394}
395
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300396static void do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000397{
Luiz Capitulino13548692011-07-29 15:36:43 -0300398 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000399 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000400 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300401 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300402 vm_state_notify(0, state);
Michael S. Tsirkin55df6f32010-11-22 19:52:22 +0200403 qemu_aio_flush();
404 bdrv_flush_all();
Blue Swirl296af7c2010-03-29 19:23:50 +0000405 monitor_protocol_event(QEVENT_STOP, NULL);
406 }
407}
408
409static int cpu_can_run(CPUState *env)
410{
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100411 if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000412 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100413 }
Luiz Capitulino13548692011-07-29 15:36:43 -0300414 if (env->stopped || !runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000415 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100416 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000417 return 1;
418}
419
Jan Kiszka16400322011-02-09 16:29:37 +0100420static bool cpu_thread_is_idle(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000421{
Jan Kiszka16400322011-02-09 16:29:37 +0100422 if (env->stop || env->queued_work_first) {
423 return false;
424 }
Luiz Capitulino13548692011-07-29 15:36:43 -0300425 if (env->stopped || !runstate_is_running()) {
Jan Kiszka16400322011-02-09 16:29:37 +0100426 return true;
427 }
Jan Kiszkaf2c1cc82011-03-15 12:26:18 +0100428 if (!env->halted || qemu_cpu_has_work(env) ||
429 (kvm_enabled() && kvm_irqchip_in_kernel())) {
Jan Kiszka16400322011-02-09 16:29:37 +0100430 return false;
431 }
432 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000433}
434
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200435bool all_cpu_threads_idle(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000436{
437 CPUState *env;
438
Jan Kiszka16400322011-02-09 16:29:37 +0100439 for (env = first_cpu; env != NULL; env = env->next_cpu) {
440 if (!cpu_thread_is_idle(env)) {
441 return false;
442 }
443 }
444 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000445}
446
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100447static void cpu_handle_guest_debug(CPUState *env)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200448{
449 gdb_set_stop_cpu(env);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100450 qemu_system_debug_request();
Jan Kiszka83f338f2011-02-07 12:19:17 +0100451 env->stopped = 1;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200452}
453
Paolo Bonzini714bd042011-03-12 17:44:06 +0100454static void cpu_signal(int sig)
455{
456 if (cpu_single_env) {
457 cpu_exit(cpu_single_env);
458 }
459 exit_request = 1;
460}
Paolo Bonzini714bd042011-03-12 17:44:06 +0100461
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100462#ifdef CONFIG_LINUX
463static void sigbus_reraise(void)
464{
465 sigset_t set;
466 struct sigaction action;
467
468 memset(&action, 0, sizeof(action));
469 action.sa_handler = SIG_DFL;
470 if (!sigaction(SIGBUS, &action, NULL)) {
471 raise(SIGBUS);
472 sigemptyset(&set);
473 sigaddset(&set, SIGBUS);
474 sigprocmask(SIG_UNBLOCK, &set, NULL);
475 }
476 perror("Failed to re-raise SIGBUS!\n");
477 abort();
478}
479
480static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
481 void *ctx)
482{
483 if (kvm_on_sigbus(siginfo->ssi_code,
484 (void *)(intptr_t)siginfo->ssi_addr)) {
485 sigbus_reraise();
486 }
487}
488
489static void qemu_init_sigbus(void)
490{
491 struct sigaction action;
492
493 memset(&action, 0, sizeof(action));
494 action.sa_flags = SA_SIGINFO;
495 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
496 sigaction(SIGBUS, &action, NULL);
497
498 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
499}
500
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100501static void qemu_kvm_eat_signals(CPUState *env)
502{
503 struct timespec ts = { 0, 0 };
504 siginfo_t siginfo;
505 sigset_t waitset;
506 sigset_t chkset;
507 int r;
508
509 sigemptyset(&waitset);
510 sigaddset(&waitset, SIG_IPI);
511 sigaddset(&waitset, SIGBUS);
512
513 do {
514 r = sigtimedwait(&waitset, &siginfo, &ts);
515 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
516 perror("sigtimedwait");
517 exit(1);
518 }
519
520 switch (r) {
521 case SIGBUS:
522 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
523 sigbus_reraise();
524 }
525 break;
526 default:
527 break;
528 }
529
530 r = sigpending(&chkset);
531 if (r == -1) {
532 perror("sigpending");
533 exit(1);
534 }
535 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100536}
537
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100538#else /* !CONFIG_LINUX */
539
540static void qemu_init_sigbus(void)
541{
542}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100543
544static void qemu_kvm_eat_signals(CPUState *env)
545{
546}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100547#endif /* !CONFIG_LINUX */
548
Blue Swirl296af7c2010-03-29 19:23:50 +0000549#ifndef _WIN32
550static int io_thread_fd = -1;
551
552static void qemu_event_increment(void)
553{
554 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000555 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000556 ssize_t ret;
557
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100558 if (io_thread_fd == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000559 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100560 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000561 do {
562 ret = write(io_thread_fd, &val, sizeof(val));
563 } while (ret < 0 && errno == EINTR);
564
565 /* EAGAIN is fine, a read must be pending. */
566 if (ret < 0 && errno != EAGAIN) {
Alexandre Raymond77bec682011-06-13 22:16:36 -0400567 fprintf(stderr, "qemu_event_increment: write() failed: %s\n",
Blue Swirl296af7c2010-03-29 19:23:50 +0000568 strerror(errno));
569 exit (1);
570 }
571}
572
573static void qemu_event_read(void *opaque)
574{
Stefan Weile0efb992011-02-23 19:09:16 +0100575 int fd = (intptr_t)opaque;
Blue Swirl296af7c2010-03-29 19:23:50 +0000576 ssize_t len;
577 char buffer[512];
578
579 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
580 do {
581 len = read(fd, buffer, sizeof(buffer));
582 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
583}
584
585static int qemu_event_init(void)
586{
587 int err;
588 int fds[2];
589
590 err = qemu_eventfd(fds);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100591 if (err == -1) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000592 return -errno;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100593 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000594 err = fcntl_setfl(fds[0], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100595 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000596 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100597 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000598 err = fcntl_setfl(fds[1], O_NONBLOCK);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100599 if (err < 0) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000600 goto fail;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100601 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000602 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100603 (void *)(intptr_t)fds[0]);
Blue Swirl296af7c2010-03-29 19:23:50 +0000604
605 io_thread_fd = fds[1];
606 return 0;
607
608fail:
609 close(fds[0]);
610 close(fds[1]);
611 return err;
612}
Blue Swirl296af7c2010-03-29 19:23:50 +0000613
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100614static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000615{
616}
617
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300618/* If we have signalfd, we mask out the signals we want to handle and then
619 * use signalfd to listen for them. We rely on whatever the current signal
620 * handler is to dispatch the signals when we receive them.
621 */
622static void sigfd_handler(void *opaque)
623{
Stefan Weile0efb992011-02-23 19:09:16 +0100624 int fd = (intptr_t)opaque;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300625 struct qemu_signalfd_siginfo info;
626 struct sigaction action;
627 ssize_t len;
628
629 while (1) {
630 do {
631 len = read(fd, &info, sizeof(info));
632 } while (len == -1 && errno == EINTR);
633
634 if (len == -1 && errno == EAGAIN) {
635 break;
636 }
637
638 if (len != sizeof(info)) {
639 printf("read from sigfd returned %zd: %m\n", len);
640 return;
641 }
642
643 sigaction(info.ssi_signo, NULL, &action);
644 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
645 action.sa_sigaction(info.ssi_signo,
646 (siginfo_t *)&info, NULL);
647 } else if (action.sa_handler) {
648 action.sa_handler(info.ssi_signo);
649 }
650 }
651}
652
Paolo Bonzini712ae482011-03-12 17:44:05 +0100653static int qemu_signal_init(void)
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300654{
655 int sigfd;
Paolo Bonzini712ae482011-03-12 17:44:05 +0100656 sigset_t set;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300657
Alexandre Raymond89b9ba62011-06-15 01:20:31 -0400658 /*
659 * SIG_IPI must be blocked in the main thread and must not be caught
660 * by sigwait() in the signal thread. Otherwise, the cpu thread will
661 * not catch it reliably.
662 */
663 sigemptyset(&set);
664 sigaddset(&set, SIG_IPI);
665 pthread_sigmask(SIG_BLOCK, &set, NULL);
666
Paolo Bonzini712ae482011-03-12 17:44:05 +0100667 sigemptyset(&set);
668 sigaddset(&set, SIGIO);
669 sigaddset(&set, SIGALRM);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100670 sigaddset(&set, SIGBUS);
Alexandre Raymond5664aed2011-06-14 10:05:36 -0400671 pthread_sigmask(SIG_BLOCK, &set, NULL);
Paolo Bonzini712ae482011-03-12 17:44:05 +0100672
673 sigfd = qemu_signalfd(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300674 if (sigfd == -1) {
675 fprintf(stderr, "failed to create signalfd\n");
676 return -errno;
677 }
678
679 fcntl_setfl(sigfd, O_NONBLOCK);
680
681 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
Stefan Weile0efb992011-02-23 19:09:16 +0100682 (void *)(intptr_t)sigfd);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300683
684 return 0;
685}
Blue Swirl296af7c2010-03-29 19:23:50 +0000686
Paolo Bonzini714bd042011-03-12 17:44:06 +0100687static void qemu_kvm_init_cpu_signals(CPUState *env)
688{
689 int r;
690 sigset_t set;
691 struct sigaction sigact;
692
693 memset(&sigact, 0, sizeof(sigact));
694 sigact.sa_handler = dummy_signal;
695 sigaction(SIG_IPI, &sigact, NULL);
696
Paolo Bonzini714bd042011-03-12 17:44:06 +0100697 pthread_sigmask(SIG_BLOCK, NULL, &set);
698 sigdelset(&set, SIG_IPI);
699 sigdelset(&set, SIGBUS);
700 r = kvm_set_signal_mask(env, &set);
701 if (r) {
702 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
703 exit(1);
704 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100705
Paolo Bonzini714bd042011-03-12 17:44:06 +0100706 sigdelset(&set, SIG_IPI);
707 sigdelset(&set, SIGBUS);
708 r = kvm_set_signal_mask(env, &set);
709 if (r) {
710 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
711 exit(1);
712 }
713}
714
715static void qemu_tcg_init_cpu_signals(void)
716{
Paolo Bonzini714bd042011-03-12 17:44:06 +0100717 sigset_t set;
718 struct sigaction sigact;
719
720 memset(&sigact, 0, sizeof(sigact));
721 sigact.sa_handler = cpu_signal;
722 sigaction(SIG_IPI, &sigact, NULL);
723
724 sigemptyset(&set);
725 sigaddset(&set, SIG_IPI);
726 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100727}
728
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100729#else /* _WIN32 */
730
Blue Swirl296af7c2010-03-29 19:23:50 +0000731HANDLE qemu_event_handle;
732
733static void dummy_event_handler(void *opaque)
734{
735}
736
737static int qemu_event_init(void)
738{
739 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
740 if (!qemu_event_handle) {
741 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
742 return -1;
743 }
744 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
745 return 0;
746}
747
748static void qemu_event_increment(void)
749{
750 if (!SetEvent(qemu_event_handle)) {
751 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
752 GetLastError());
753 exit (1);
754 }
755}
Jan Kiszka9a360852011-02-01 22:15:55 +0100756
Paolo Bonzini712ae482011-03-12 17:44:05 +0100757static int qemu_signal_init(void)
758{
759 return 0;
760}
761
Paolo Bonzini714bd042011-03-12 17:44:06 +0100762static void qemu_kvm_init_cpu_signals(CPUState *env)
763{
764 abort();
765}
766
767static void qemu_tcg_init_cpu_signals(void)
768{
769}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100770#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000771
Blue Swirl296af7c2010-03-29 19:23:50 +0000772QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200773static QemuCond qemu_io_proceeded_cond;
774static bool iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000775
776static QemuThread io_thread;
777
778static QemuThread *tcg_cpu_thread;
779static QemuCond *tcg_halt_cond;
780
Blue Swirl296af7c2010-03-29 19:23:50 +0000781/* cpu creation */
782static QemuCond qemu_cpu_cond;
783/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000784static QemuCond qemu_pause_cond;
785static QemuCond qemu_work_cond;
786
Blue Swirl296af7c2010-03-29 19:23:50 +0000787int qemu_init_main_loop(void)
788{
789 int ret;
790
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100791 qemu_init_sigbus();
Jan Kiszka3c638d02010-06-25 16:56:56 +0200792
Paolo Bonzini712ae482011-03-12 17:44:05 +0100793 ret = qemu_signal_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100794 if (ret) {
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300795 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100796 }
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300797
798 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000799 ret = qemu_event_init();
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100800 if (ret) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000801 return ret;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100802 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000803
Anthony Liguoried945922011-02-08 18:18:18 +0100804 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100805 qemu_cond_init(&qemu_pause_cond);
806 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200807 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000808 qemu_mutex_init(&qemu_global_mutex);
809 qemu_mutex_lock(&qemu_global_mutex);
810
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100811 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000812
813 return 0;
814}
815
Blue Swirl7277e022010-04-12 17:19:06 +0000816void qemu_main_loop_start(void)
817{
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200818 resume_all_vcpus();
Blue Swirl7277e022010-04-12 17:19:06 +0000819}
820
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300821void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
822{
823 struct qemu_work_item wi;
824
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100825 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300826 func(data);
827 return;
828 }
829
830 wi.func = func;
831 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100832 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300833 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100834 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300835 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100836 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300837 env->queued_work_last = &wi;
838 wi.next = NULL;
839 wi.done = false;
840
841 qemu_cpu_kick(env);
842 while (!wi.done) {
843 CPUState *self_env = cpu_single_env;
844
845 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
846 cpu_single_env = self_env;
847 }
848}
849
850static void flush_queued_work(CPUState *env)
851{
852 struct qemu_work_item *wi;
853
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100854 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300855 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100856 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300857
858 while ((wi = env->queued_work_first)) {
859 env->queued_work_first = wi->next;
860 wi->func(wi->data);
861 wi->done = true;
862 }
863 env->queued_work_last = NULL;
864 qemu_cond_broadcast(&qemu_work_cond);
865}
866
Blue Swirl296af7c2010-03-29 19:23:50 +0000867static void qemu_wait_io_event_common(CPUState *env)
868{
869 if (env->stop) {
870 env->stop = 0;
871 env->stopped = 1;
872 qemu_cond_signal(&qemu_pause_cond);
873 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300874 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100875 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000876}
877
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200878static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000879{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200880 CPUState *env;
881
Jan Kiszka16400322011-02-09 16:29:37 +0100882 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200883 /* Start accounting real time to the virtual clock if the CPUs
884 are idle. */
885 qemu_clock_warp(vm_clock);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100886 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100887 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000888
Paolo Bonzini46daff12011-06-09 13:10:24 +0200889 while (iothread_requesting_mutex) {
890 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
891 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200892
893 for (env = first_cpu; env != NULL; env = env->next_cpu) {
894 qemu_wait_io_event_common(env);
895 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000896}
897
Blue Swirl296af7c2010-03-29 19:23:50 +0000898static void qemu_kvm_wait_io_event(CPUState *env)
899{
Jan Kiszka16400322011-02-09 16:29:37 +0100900 while (cpu_thread_is_idle(env)) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100901 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100902 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000903
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100904 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000905 qemu_wait_io_event_common(env);
906}
907
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100908static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000909{
910 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100911 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000912
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300913 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100914 qemu_thread_get_self(env->thread);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100915 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000916
Jan Kiszka84b49152011-02-01 22:15:50 +0100917 r = kvm_init_vcpu(env);
918 if (r < 0) {
919 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
920 exit(1);
921 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000922
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100923 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000924
925 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000926 env->created = 1;
927 qemu_cond_signal(&qemu_cpu_cond);
928
Blue Swirl296af7c2010-03-29 19:23:50 +0000929 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100930 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100931 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100932 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100933 cpu_handle_guest_debug(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100934 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100935 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000936 qemu_kvm_wait_io_event(env);
937 }
938
939 return NULL;
940}
941
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100942static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000943{
944 CPUState *env = arg;
945
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100946 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100947 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000948
949 /* signal CPU creation */
950 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100951 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100952 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000953 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100954 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000955 qemu_cond_signal(&qemu_cpu_cond);
956
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200957 /* wait for initial kick-off after machine start */
958 while (first_cpu->stopped) {
959 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100960 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000961
962 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200963 cpu_exec_all();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200964 if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
Paolo Bonzini3b2319a2011-04-13 10:03:43 +0200965 qemu_notify_event();
966 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200967 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000968 }
969
970 return NULL;
971}
972
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100973static void qemu_cpu_kick_thread(CPUState *env)
974{
975#ifndef _WIN32
976 int err;
977
978 err = pthread_kill(env->thread->thread, SIG_IPI);
979 if (err) {
980 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
981 exit(1);
982 }
983#else /* _WIN32 */
984 if (!qemu_cpu_is_self(env)) {
985 SuspendThread(env->thread->thread);
986 cpu_signal(0);
987 ResumeThread(env->thread->thread);
988 }
989#endif
990}
991
Blue Swirl296af7c2010-03-29 19:23:50 +0000992void qemu_cpu_kick(void *_env)
993{
994 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100995
Blue Swirl296af7c2010-03-29 19:23:50 +0000996 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaeae74cf2011-08-22 17:46:03 +0200997 if (kvm_enabled() && !env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100998 qemu_cpu_kick_thread(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100999 env->thread_kicked = true;
1000 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001001}
1002
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001003void qemu_cpu_kick_self(void)
1004{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +01001005#ifndef _WIN32
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001006 assert(cpu_single_env);
1007
1008 if (!cpu_single_env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001009 qemu_cpu_kick_thread(cpu_single_env);
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001010 cpu_single_env->thread_kicked = true;
1011 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +01001012#else
1013 abort();
1014#endif
Blue Swirl296af7c2010-03-29 19:23:50 +00001015}
1016
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001017int qemu_cpu_is_self(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001018{
1019 CPUState *env = _env;
Blue Swirl296af7c2010-03-29 19:23:50 +00001020
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001021 return qemu_thread_is_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001022}
1023
Blue Swirl296af7c2010-03-29 19:23:50 +00001024void qemu_mutex_lock_iothread(void)
1025{
1026 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001027 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001028 } else {
Paolo Bonzini46daff12011-06-09 13:10:24 +02001029 iothread_requesting_mutex = true;
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001030 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001031 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001032 qemu_mutex_lock(&qemu_global_mutex);
1033 }
Paolo Bonzini46daff12011-06-09 13:10:24 +02001034 iothread_requesting_mutex = false;
1035 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001036 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001037}
1038
1039void qemu_mutex_unlock_iothread(void)
1040{
1041 qemu_mutex_unlock(&qemu_global_mutex);
1042}
1043
1044static int all_vcpus_paused(void)
1045{
1046 CPUState *penv = first_cpu;
1047
1048 while (penv) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001049 if (!penv->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001050 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001051 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001052 penv = (CPUState *)penv->next_cpu;
1053 }
1054
1055 return 1;
1056}
1057
1058void pause_all_vcpus(void)
1059{
1060 CPUState *penv = first_cpu;
1061
Paolo Bonzinia5c57d62011-09-12 14:40:36 +02001062 qemu_clock_enable(vm_clock, false);
Blue Swirl296af7c2010-03-29 19:23:50 +00001063 while (penv) {
1064 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +00001065 qemu_cpu_kick(penv);
1066 penv = (CPUState *)penv->next_cpu;
1067 }
1068
1069 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001070 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +00001071 penv = first_cpu;
1072 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -03001073 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +00001074 penv = (CPUState *)penv->next_cpu;
1075 }
1076 }
1077}
1078
1079void resume_all_vcpus(void)
1080{
1081 CPUState *penv = first_cpu;
1082
1083 while (penv) {
1084 penv->stop = 0;
1085 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001086 qemu_cpu_kick(penv);
1087 penv = (CPUState *)penv->next_cpu;
1088 }
1089}
1090
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001091static void qemu_tcg_init_vcpu(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001092{
1093 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001094
Blue Swirl296af7c2010-03-29 19:23:50 +00001095 /* share a single thread for all cpus with TCG */
1096 if (!tcg_cpu_thread) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001097 env->thread = g_malloc0(sizeof(QemuThread));
1098 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +00001099 qemu_cond_init(env->halt_cond);
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001100 tcg_halt_cond = env->halt_cond;
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001101 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001102 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001103 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001104 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001105 tcg_cpu_thread = env->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001106 } else {
1107 env->thread = tcg_cpu_thread;
1108 env->halt_cond = tcg_halt_cond;
1109 }
1110}
1111
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001112static void qemu_kvm_start_vcpu(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001113{
Anthony Liguori7267c092011-08-20 22:09:37 -05001114 env->thread = g_malloc0(sizeof(QemuThread));
1115 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +00001116 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001117 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001118 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001119 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001120 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001121}
1122
1123void qemu_init_vcpu(void *_env)
1124{
1125 CPUState *env = _env;
1126
1127 env->nr_cores = smp_cores;
1128 env->nr_threads = smp_threads;
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001129 env->stopped = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001130 if (kvm_enabled()) {
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001131 qemu_kvm_start_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001132 } else {
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001133 qemu_tcg_init_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001134 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001135}
1136
1137void qemu_notify_event(void)
1138{
1139 qemu_event_increment();
1140}
1141
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001142void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001143{
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001144 if (cpu_single_env) {
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001145 cpu_single_env->stop = 0;
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001146 cpu_single_env->stopped = 1;
1147 cpu_exit(cpu_single_env);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001148 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001149 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001150}
1151
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001152void vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001153{
Jan Kiszkab7680cb2011-03-12 17:43:51 +01001154 if (!qemu_thread_is_self(&io_thread)) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001155 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001156 /*
1157 * FIXME: should not return to device code in case
1158 * vm_stop() has been requested.
1159 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001160 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +00001161 return;
1162 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001163 do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001164}
1165
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001166/* does a state transition even if the VM is already stopped,
1167 current state is forgotten forever */
1168void vm_stop_force_state(RunState state)
1169{
1170 if (runstate_is_running()) {
1171 vm_stop(state);
1172 } else {
1173 runstate_set(state);
1174 }
1175}
1176
Jan Kiszka6792a572011-02-07 12:19:18 +01001177static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001178{
1179 int ret;
1180#ifdef CONFIG_PROFILER
1181 int64_t ti;
1182#endif
1183
1184#ifdef CONFIG_PROFILER
1185 ti = profile_getclock();
1186#endif
1187 if (use_icount) {
1188 int64_t count;
1189 int decr;
1190 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1191 env->icount_decr.u16.low = 0;
1192 env->icount_extra = 0;
Paolo Bonzini946fb272011-09-12 13:57:37 +02001193 count = qemu_icount_round(qemu_clock_deadline(vm_clock));
Blue Swirl296af7c2010-03-29 19:23:50 +00001194 qemu_icount += count;
1195 decr = (count > 0xffff) ? 0xffff : count;
1196 count -= decr;
1197 env->icount_decr.u16.low = decr;
1198 env->icount_extra = count;
1199 }
1200 ret = cpu_exec(env);
1201#ifdef CONFIG_PROFILER
1202 qemu_time += profile_getclock() - ti;
1203#endif
1204 if (use_icount) {
1205 /* Fold pending instructions back into the
1206 instruction counter, and clear the interrupt flag. */
1207 qemu_icount -= (env->icount_decr.u16.low
1208 + env->icount_extra);
1209 env->icount_decr.u32 = 0;
1210 env->icount_extra = 0;
1211 }
1212 return ret;
1213}
1214
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001215bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001216{
Jan Kiszka9a360852011-02-01 22:15:55 +01001217 int r;
1218
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001219 /* Account partial waits to the vm_clock. */
1220 qemu_clock_warp(vm_clock);
1221
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001222 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001223 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001224 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001225 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +02001226 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001227
1228 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +02001229 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001230
Jan Kiszka3c638d02010-06-25 16:56:56 +02001231 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +01001232 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +01001233 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001234 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +01001235 } else {
1236 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001237 }
1238 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +01001239 cpu_handle_guest_debug(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001240 break;
1241 }
Paolo Bonzinidf646df2011-03-12 17:43:58 +01001242 } else if (env->stop || env->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001243 break;
1244 }
1245 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001246 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +01001247 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +00001248}
1249
1250void set_numa_modes(void)
1251{
1252 CPUState *env;
1253 int i;
1254
1255 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1256 for (i = 0; i < nb_numa_nodes; i++) {
1257 if (node_cpumask[i] & (1 << env->cpu_index)) {
1258 env->numa_node = i;
1259 }
1260 }
1261 }
1262}
1263
1264void set_cpu_log(const char *optarg)
1265{
1266 int mask;
1267 const CPULogItem *item;
1268
1269 mask = cpu_str_to_log_mask(optarg);
1270 if (!mask) {
1271 printf("Log items (comma separated):\n");
1272 for (item = cpu_log_items; item->mask != 0; item++) {
1273 printf("%-10s %s\n", item->name, item->help);
1274 }
1275 exit(1);
1276 }
1277 cpu_set_log(mask);
1278}
Blue Swirl29e922b2010-03-29 19:24:00 +00001279
Matthew Fernandezc235d732011-06-07 16:32:40 +00001280void set_cpu_log_filename(const char *optarg)
1281{
1282 cpu_set_log_filename(optarg);
1283}
1284
Stefan Weil9a78eea2010-10-22 23:03:33 +02001285void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001286{
1287 /* XXX: implement xxx_cpu_list for targets that still miss it */
1288#if defined(cpu_list_id)
1289 cpu_list_id(f, cpu_fprintf, optarg);
1290#elif defined(cpu_list)
1291 cpu_list(f, cpu_fprintf); /* deprecated */
1292#endif
1293}