blob: b9f1573f871ee059ead455d7277fbe13a362685b [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"
Paolo Bonzini44a9b352011-09-12 16:44:30 +020036#include "main-loop.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020037
38#ifndef _WIN32
Marcelo Tosattia8486bc2010-10-11 15:31:16 -030039#include "compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020040#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000041
Jan Kiszka6d9cb732011-02-01 22:15:58 +010042#ifdef CONFIG_LINUX
43
44#include <sys/prctl.h>
45
Marcelo Tosattic0532a72010-10-11 15:31:21 -030046#ifndef PR_MCE_KILL
47#define PR_MCE_KILL 33
48#endif
49
Jan Kiszka6d9cb732011-02-01 22:15:58 +010050#ifndef PR_MCE_KILL_SET
51#define PR_MCE_KILL_SET 1
52#endif
53
54#ifndef PR_MCE_KILL_EARLY
55#define PR_MCE_KILL_EARLY 1
56#endif
57
58#endif /* CONFIG_LINUX */
59
Blue Swirl296af7c2010-03-29 19:23:50 +000060static CPUState *next_cpu;
61
62/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +020063/* guest cycle counter */
64
65/* Conversion factor from emulated instructions to virtual clock ticks. */
66static int icount_time_shift;
67/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
68#define MAX_ICOUNT_SHIFT 10
69/* Compensate for varying guest execution speed. */
70static int64_t qemu_icount_bias;
71static QEMUTimer *icount_rt_timer;
72static QEMUTimer *icount_vm_timer;
73static QEMUTimer *icount_warp_timer;
74static int64_t vm_clock_warp_start;
75static int64_t qemu_icount;
76
77typedef struct TimersState {
78 int64_t cpu_ticks_prev;
79 int64_t cpu_ticks_offset;
80 int64_t cpu_clock_offset;
81 int32_t cpu_ticks_enabled;
82 int64_t dummy;
83} TimersState;
84
85TimersState timers_state;
86
87/* Return the virtual CPU time, based on the instruction counter. */
88int64_t cpu_get_icount(void)
89{
90 int64_t icount;
91 CPUState *env = cpu_single_env;;
92
93 icount = qemu_icount;
94 if (env) {
95 if (!can_do_io(env)) {
96 fprintf(stderr, "Bad clock read\n");
97 }
98 icount -= (env->icount_decr.u16.low + env->icount_extra);
99 }
100 return qemu_icount_bias + (icount << icount_time_shift);
101}
102
103/* return the host CPU cycle counter and handle stop/restart */
104int64_t cpu_get_ticks(void)
105{
106 if (use_icount) {
107 return cpu_get_icount();
108 }
109 if (!timers_state.cpu_ticks_enabled) {
110 return timers_state.cpu_ticks_offset;
111 } else {
112 int64_t ticks;
113 ticks = cpu_get_real_ticks();
114 if (timers_state.cpu_ticks_prev > ticks) {
115 /* Note: non increasing ticks may happen if the host uses
116 software suspend */
117 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
118 }
119 timers_state.cpu_ticks_prev = ticks;
120 return ticks + timers_state.cpu_ticks_offset;
121 }
122}
123
124/* return the host CPU monotonic timer and handle stop/restart */
125int64_t cpu_get_clock(void)
126{
127 int64_t ti;
128 if (!timers_state.cpu_ticks_enabled) {
129 return timers_state.cpu_clock_offset;
130 } else {
131 ti = get_clock();
132 return ti + timers_state.cpu_clock_offset;
133 }
134}
135
136/* enable cpu_get_ticks() */
137void cpu_enable_ticks(void)
138{
139 if (!timers_state.cpu_ticks_enabled) {
140 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
141 timers_state.cpu_clock_offset -= get_clock();
142 timers_state.cpu_ticks_enabled = 1;
143 }
144}
145
146/* disable cpu_get_ticks() : the clock is stopped. You must not call
147 cpu_get_ticks() after that. */
148void cpu_disable_ticks(void)
149{
150 if (timers_state.cpu_ticks_enabled) {
151 timers_state.cpu_ticks_offset = cpu_get_ticks();
152 timers_state.cpu_clock_offset = cpu_get_clock();
153 timers_state.cpu_ticks_enabled = 0;
154 }
155}
156
157/* Correlation between real and virtual time is always going to be
158 fairly approximate, so ignore small variation.
159 When the guest is idle real and virtual time will be aligned in
160 the IO wait loop. */
161#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
162
163static void icount_adjust(void)
164{
165 int64_t cur_time;
166 int64_t cur_icount;
167 int64_t delta;
168 static int64_t last_delta;
169 /* If the VM is not running, then do nothing. */
170 if (!runstate_is_running()) {
171 return;
172 }
173 cur_time = cpu_get_clock();
174 cur_icount = qemu_get_clock_ns(vm_clock);
175 delta = cur_icount - cur_time;
176 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
177 if (delta > 0
178 && last_delta + ICOUNT_WOBBLE < delta * 2
179 && icount_time_shift > 0) {
180 /* The guest is getting too far ahead. Slow time down. */
181 icount_time_shift--;
182 }
183 if (delta < 0
184 && last_delta - ICOUNT_WOBBLE > delta * 2
185 && icount_time_shift < MAX_ICOUNT_SHIFT) {
186 /* The guest is getting too far behind. Speed time up. */
187 icount_time_shift++;
188 }
189 last_delta = delta;
190 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
191}
192
193static void icount_adjust_rt(void *opaque)
194{
195 qemu_mod_timer(icount_rt_timer,
196 qemu_get_clock_ms(rt_clock) + 1000);
197 icount_adjust();
198}
199
200static void icount_adjust_vm(void *opaque)
201{
202 qemu_mod_timer(icount_vm_timer,
203 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
204 icount_adjust();
205}
206
207static int64_t qemu_icount_round(int64_t count)
208{
209 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
210}
211
212static void icount_warp_rt(void *opaque)
213{
214 if (vm_clock_warp_start == -1) {
215 return;
216 }
217
218 if (runstate_is_running()) {
219 int64_t clock = qemu_get_clock_ns(rt_clock);
220 int64_t warp_delta = clock - vm_clock_warp_start;
221 if (use_icount == 1) {
222 qemu_icount_bias += warp_delta;
223 } else {
224 /*
225 * In adaptive mode, do not let the vm_clock run too
226 * far ahead of real time.
227 */
228 int64_t cur_time = cpu_get_clock();
229 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
230 int64_t delta = cur_time - cur_icount;
231 qemu_icount_bias += MIN(warp_delta, delta);
232 }
233 if (qemu_clock_expired(vm_clock)) {
234 qemu_notify_event();
235 }
236 }
237 vm_clock_warp_start = -1;
238}
239
240void qemu_clock_warp(QEMUClock *clock)
241{
242 int64_t deadline;
243
244 /*
245 * There are too many global variables to make the "warp" behavior
246 * applicable to other clocks. But a clock argument removes the
247 * need for if statements all over the place.
248 */
249 if (clock != vm_clock || !use_icount) {
250 return;
251 }
252
253 /*
254 * If the CPUs have been sleeping, advance the vm_clock timer now. This
255 * ensures that the deadline for the timer is computed correctly below.
256 * This also makes sure that the insn counter is synchronized before the
257 * CPU starts running, in case the CPU is woken by an event other than
258 * the earliest vm_clock timer.
259 */
260 icount_warp_rt(NULL);
261 if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock)) {
262 qemu_del_timer(icount_warp_timer);
263 return;
264 }
265
266 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
267 deadline = qemu_clock_deadline(vm_clock);
268 if (deadline > 0) {
269 /*
270 * Ensure the vm_clock proceeds even when the virtual CPU goes to
271 * sleep. Otherwise, the CPU might be waiting for a future timer
272 * interrupt to wake it up, but the interrupt never comes because
273 * the vCPU isn't running any insns and thus doesn't advance the
274 * vm_clock.
275 *
276 * An extreme solution for this problem would be to never let VCPUs
277 * sleep in icount mode if there is a pending vm_clock timer; rather
278 * time could just advance to the next vm_clock event. Instead, we
279 * do stop VCPUs and only advance vm_clock after some "real" time,
280 * (related to the time left until the next event) has passed. This
281 * rt_clock timer will do this. This avoids that the warps are too
282 * visible externally---for example, you will not be sending network
283 * packets continously instead of every 100ms.
284 */
285 qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);
286 } else {
287 qemu_notify_event();
288 }
289}
290
291static const VMStateDescription vmstate_timers = {
292 .name = "timer",
293 .version_id = 2,
294 .minimum_version_id = 1,
295 .minimum_version_id_old = 1,
296 .fields = (VMStateField[]) {
297 VMSTATE_INT64(cpu_ticks_offset, TimersState),
298 VMSTATE_INT64(dummy, TimersState),
299 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
300 VMSTATE_END_OF_LIST()
301 }
302};
303
304void configure_icount(const char *option)
305{
306 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
307 if (!option) {
308 return;
309 }
310
311 icount_warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
312 if (strcmp(option, "auto") != 0) {
313 icount_time_shift = strtol(option, NULL, 0);
314 use_icount = 1;
315 return;
316 }
317
318 use_icount = 2;
319
320 /* 125MIPS seems a reasonable initial guess at the guest speed.
321 It will be corrected fairly quickly anyway. */
322 icount_time_shift = 3;
323
324 /* Have both realtime and virtual time triggers for speed adjustment.
325 The realtime trigger catches emulated time passing too slowly,
326 the virtual time trigger catches emulated time passing too fast.
327 Realtime triggers occur even when idle, so use them less frequently
328 than VM triggers. */
329 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
330 qemu_mod_timer(icount_rt_timer,
331 qemu_get_clock_ms(rt_clock) + 1000);
332 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
333 qemu_mod_timer(icount_vm_timer,
334 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
335}
336
337/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000338void hw_error(const char *fmt, ...)
339{
340 va_list ap;
341 CPUState *env;
342
343 va_start(ap, fmt);
344 fprintf(stderr, "qemu: hardware error: ");
345 vfprintf(stderr, fmt, ap);
346 fprintf(stderr, "\n");
347 for(env = first_cpu; env != NULL; env = env->next_cpu) {
348 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
349#ifdef TARGET_I386
350 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
351#else
352 cpu_dump_state(env, stderr, fprintf, 0);
353#endif
354 }
355 va_end(ap);
356 abort();
357}
358
359void cpu_synchronize_all_states(void)
360{
361 CPUState *cpu;
362
363 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
364 cpu_synchronize_state(cpu);
365 }
366}
367
368void cpu_synchronize_all_post_reset(void)
369{
370 CPUState *cpu;
371
372 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
373 cpu_synchronize_post_reset(cpu);
374 }
375}
376
377void cpu_synchronize_all_post_init(void)
378{
379 CPUState *cpu;
380
381 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
382 cpu_synchronize_post_init(cpu);
383 }
384}
385
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300386int cpu_is_stopped(CPUState *env)
387{
Luiz Capitulino13548692011-07-29 15:36:43 -0300388 return !runstate_is_running() || env->stopped;
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300389}
390
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300391static void do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000392{
Luiz Capitulino13548692011-07-29 15:36:43 -0300393 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000394 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000395 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300396 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300397 vm_state_notify(0, state);
Michael S. Tsirkin55df6f32010-11-22 19:52:22 +0200398 qemu_aio_flush();
399 bdrv_flush_all();
Blue Swirl296af7c2010-03-29 19:23:50 +0000400 monitor_protocol_event(QEVENT_STOP, NULL);
401 }
402}
403
404static int cpu_can_run(CPUState *env)
405{
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100406 if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000407 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100408 }
Luiz Capitulino13548692011-07-29 15:36:43 -0300409 if (env->stopped || !runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000410 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100411 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000412 return 1;
413}
414
Jan Kiszka16400322011-02-09 16:29:37 +0100415static bool cpu_thread_is_idle(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000416{
Jan Kiszka16400322011-02-09 16:29:37 +0100417 if (env->stop || env->queued_work_first) {
418 return false;
419 }
Luiz Capitulino13548692011-07-29 15:36:43 -0300420 if (env->stopped || !runstate_is_running()) {
Jan Kiszka16400322011-02-09 16:29:37 +0100421 return true;
422 }
Jan Kiszkaf2c1cc82011-03-15 12:26:18 +0100423 if (!env->halted || qemu_cpu_has_work(env) ||
424 (kvm_enabled() && kvm_irqchip_in_kernel())) {
Jan Kiszka16400322011-02-09 16:29:37 +0100425 return false;
426 }
427 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000428}
429
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200430bool all_cpu_threads_idle(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000431{
432 CPUState *env;
433
Jan Kiszka16400322011-02-09 16:29:37 +0100434 for (env = first_cpu; env != NULL; env = env->next_cpu) {
435 if (!cpu_thread_is_idle(env)) {
436 return false;
437 }
438 }
439 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000440}
441
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100442static void cpu_handle_guest_debug(CPUState *env)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200443{
444 gdb_set_stop_cpu(env);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100445 qemu_system_debug_request();
Jan Kiszka83f338f2011-02-07 12:19:17 +0100446 env->stopped = 1;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200447}
448
Paolo Bonzini714bd042011-03-12 17:44:06 +0100449static void cpu_signal(int sig)
450{
451 if (cpu_single_env) {
452 cpu_exit(cpu_single_env);
453 }
454 exit_request = 1;
455}
Paolo Bonzini714bd042011-03-12 17:44:06 +0100456
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100457#ifdef CONFIG_LINUX
458static void sigbus_reraise(void)
459{
460 sigset_t set;
461 struct sigaction action;
462
463 memset(&action, 0, sizeof(action));
464 action.sa_handler = SIG_DFL;
465 if (!sigaction(SIGBUS, &action, NULL)) {
466 raise(SIGBUS);
467 sigemptyset(&set);
468 sigaddset(&set, SIGBUS);
469 sigprocmask(SIG_UNBLOCK, &set, NULL);
470 }
471 perror("Failed to re-raise SIGBUS!\n");
472 abort();
473}
474
475static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
476 void *ctx)
477{
478 if (kvm_on_sigbus(siginfo->ssi_code,
479 (void *)(intptr_t)siginfo->ssi_addr)) {
480 sigbus_reraise();
481 }
482}
483
484static void qemu_init_sigbus(void)
485{
486 struct sigaction action;
487
488 memset(&action, 0, sizeof(action));
489 action.sa_flags = SA_SIGINFO;
490 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
491 sigaction(SIGBUS, &action, NULL);
492
493 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
494}
495
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100496static void qemu_kvm_eat_signals(CPUState *env)
497{
498 struct timespec ts = { 0, 0 };
499 siginfo_t siginfo;
500 sigset_t waitset;
501 sigset_t chkset;
502 int r;
503
504 sigemptyset(&waitset);
505 sigaddset(&waitset, SIG_IPI);
506 sigaddset(&waitset, SIGBUS);
507
508 do {
509 r = sigtimedwait(&waitset, &siginfo, &ts);
510 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
511 perror("sigtimedwait");
512 exit(1);
513 }
514
515 switch (r) {
516 case SIGBUS:
517 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
518 sigbus_reraise();
519 }
520 break;
521 default:
522 break;
523 }
524
525 r = sigpending(&chkset);
526 if (r == -1) {
527 perror("sigpending");
528 exit(1);
529 }
530 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100531}
532
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100533#else /* !CONFIG_LINUX */
534
535static void qemu_init_sigbus(void)
536{
537}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100538
539static void qemu_kvm_eat_signals(CPUState *env)
540{
541}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100542#endif /* !CONFIG_LINUX */
543
Blue Swirl296af7c2010-03-29 19:23:50 +0000544#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100545static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000546{
547}
548
Paolo Bonzini714bd042011-03-12 17:44:06 +0100549static void qemu_kvm_init_cpu_signals(CPUState *env)
550{
551 int r;
552 sigset_t set;
553 struct sigaction sigact;
554
555 memset(&sigact, 0, sizeof(sigact));
556 sigact.sa_handler = dummy_signal;
557 sigaction(SIG_IPI, &sigact, NULL);
558
Paolo Bonzini714bd042011-03-12 17:44:06 +0100559 pthread_sigmask(SIG_BLOCK, NULL, &set);
560 sigdelset(&set, SIG_IPI);
561 sigdelset(&set, SIGBUS);
562 r = kvm_set_signal_mask(env, &set);
563 if (r) {
564 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
565 exit(1);
566 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100567
Paolo Bonzini714bd042011-03-12 17:44:06 +0100568 sigdelset(&set, SIG_IPI);
569 sigdelset(&set, SIGBUS);
570 r = kvm_set_signal_mask(env, &set);
571 if (r) {
572 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
573 exit(1);
574 }
575}
576
577static void qemu_tcg_init_cpu_signals(void)
578{
Paolo Bonzini714bd042011-03-12 17:44:06 +0100579 sigset_t set;
580 struct sigaction sigact;
581
582 memset(&sigact, 0, sizeof(sigact));
583 sigact.sa_handler = cpu_signal;
584 sigaction(SIG_IPI, &sigact, NULL);
585
586 sigemptyset(&set);
587 sigaddset(&set, SIG_IPI);
588 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100589}
590
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100591#else /* _WIN32 */
Paolo Bonzini714bd042011-03-12 17:44:06 +0100592static void qemu_kvm_init_cpu_signals(CPUState *env)
593{
594 abort();
595}
596
597static void qemu_tcg_init_cpu_signals(void)
598{
599}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100600#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000601
Blue Swirl296af7c2010-03-29 19:23:50 +0000602QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200603static QemuCond qemu_io_proceeded_cond;
604static bool iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000605
606static QemuThread io_thread;
607
608static QemuThread *tcg_cpu_thread;
609static QemuCond *tcg_halt_cond;
610
Blue Swirl296af7c2010-03-29 19:23:50 +0000611/* cpu creation */
612static QemuCond qemu_cpu_cond;
613/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000614static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300615static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000616
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200617void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000618{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100619 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100620 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100621 qemu_cond_init(&qemu_pause_cond);
622 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200623 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000624 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000625
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100626 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000627}
628
Blue Swirl7277e022010-04-12 17:19:06 +0000629void qemu_main_loop_start(void)
630{
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200631 resume_all_vcpus();
Blue Swirl7277e022010-04-12 17:19:06 +0000632}
633
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300634void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
635{
636 struct qemu_work_item wi;
637
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100638 if (qemu_cpu_is_self(env)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300639 func(data);
640 return;
641 }
642
643 wi.func = func;
644 wi.data = data;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100645 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300646 env->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100647 } else {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300648 env->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100649 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300650 env->queued_work_last = &wi;
651 wi.next = NULL;
652 wi.done = false;
653
654 qemu_cpu_kick(env);
655 while (!wi.done) {
656 CPUState *self_env = cpu_single_env;
657
658 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
659 cpu_single_env = self_env;
660 }
661}
662
663static void flush_queued_work(CPUState *env)
664{
665 struct qemu_work_item *wi;
666
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100667 if (!env->queued_work_first) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300668 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100669 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300670
671 while ((wi = env->queued_work_first)) {
672 env->queued_work_first = wi->next;
673 wi->func(wi->data);
674 wi->done = true;
675 }
676 env->queued_work_last = NULL;
677 qemu_cond_broadcast(&qemu_work_cond);
678}
679
Blue Swirl296af7c2010-03-29 19:23:50 +0000680static void qemu_wait_io_event_common(CPUState *env)
681{
682 if (env->stop) {
683 env->stop = 0;
684 env->stopped = 1;
685 qemu_cond_signal(&qemu_pause_cond);
686 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300687 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100688 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000689}
690
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200691static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000692{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200693 CPUState *env;
694
Jan Kiszka16400322011-02-09 16:29:37 +0100695 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200696 /* Start accounting real time to the virtual clock if the CPUs
697 are idle. */
698 qemu_clock_warp(vm_clock);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100699 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100700 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000701
Paolo Bonzini46daff12011-06-09 13:10:24 +0200702 while (iothread_requesting_mutex) {
703 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
704 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200705
706 for (env = first_cpu; env != NULL; env = env->next_cpu) {
707 qemu_wait_io_event_common(env);
708 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000709}
710
Blue Swirl296af7c2010-03-29 19:23:50 +0000711static void qemu_kvm_wait_io_event(CPUState *env)
712{
Jan Kiszka16400322011-02-09 16:29:37 +0100713 while (cpu_thread_is_idle(env)) {
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100714 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100715 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000716
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100717 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000718 qemu_wait_io_event_common(env);
719}
720
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100721static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000722{
723 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100724 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000725
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300726 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100727 qemu_thread_get_self(env->thread);
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100728 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000729
Jan Kiszka84b49152011-02-01 22:15:50 +0100730 r = kvm_init_vcpu(env);
731 if (r < 0) {
732 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
733 exit(1);
734 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000735
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100736 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000737
738 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000739 env->created = 1;
740 qemu_cond_signal(&qemu_cpu_cond);
741
Blue Swirl296af7c2010-03-29 19:23:50 +0000742 while (1) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100743 if (cpu_can_run(env)) {
Jan Kiszka6792a572011-02-07 12:19:18 +0100744 r = kvm_cpu_exec(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100745 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +0100746 cpu_handle_guest_debug(env);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100747 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100748 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000749 qemu_kvm_wait_io_event(env);
750 }
751
752 return NULL;
753}
754
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100755static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000756{
757 CPUState *env = arg;
758
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100759 qemu_tcg_init_cpu_signals();
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100760 qemu_thread_get_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000761
762 /* signal CPU creation */
763 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100764 for (env = first_cpu; env != NULL; env = env->next_cpu) {
Jan Kiszkadc7a09c2011-03-15 12:26:31 +0100765 env->thread_id = qemu_get_thread_id();
Blue Swirl296af7c2010-03-29 19:23:50 +0000766 env->created = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100767 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000768 qemu_cond_signal(&qemu_cpu_cond);
769
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200770 /* wait for initial kick-off after machine start */
771 while (first_cpu->stopped) {
772 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100773 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000774
775 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200776 cpu_exec_all();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200777 if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
Paolo Bonzini3b2319a2011-04-13 10:03:43 +0200778 qemu_notify_event();
779 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200780 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000781 }
782
783 return NULL;
784}
785
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100786static void qemu_cpu_kick_thread(CPUState *env)
787{
788#ifndef _WIN32
789 int err;
790
791 err = pthread_kill(env->thread->thread, SIG_IPI);
792 if (err) {
793 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
794 exit(1);
795 }
796#else /* _WIN32 */
797 if (!qemu_cpu_is_self(env)) {
798 SuspendThread(env->thread->thread);
799 cpu_signal(0);
800 ResumeThread(env->thread->thread);
801 }
802#endif
803}
804
Blue Swirl296af7c2010-03-29 19:23:50 +0000805void qemu_cpu_kick(void *_env)
806{
807 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100808
Blue Swirl296af7c2010-03-29 19:23:50 +0000809 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaeae74cf2011-08-22 17:46:03 +0200810 if (kvm_enabled() && !env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100811 qemu_cpu_kick_thread(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100812 env->thread_kicked = true;
813 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000814}
815
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100816void qemu_cpu_kick_self(void)
817{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100818#ifndef _WIN32
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100819 assert(cpu_single_env);
820
821 if (!cpu_single_env->thread_kicked) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100822 qemu_cpu_kick_thread(cpu_single_env);
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100823 cpu_single_env->thread_kicked = true;
824 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100825#else
826 abort();
827#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000828}
829
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100830int qemu_cpu_is_self(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000831{
832 CPUState *env = _env;
Blue Swirl296af7c2010-03-29 19:23:50 +0000833
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100834 return qemu_thread_is_self(env->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000835}
836
Blue Swirl296af7c2010-03-29 19:23:50 +0000837void qemu_mutex_lock_iothread(void)
838{
839 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000840 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300841 } else {
Paolo Bonzini46daff12011-06-09 13:10:24 +0200842 iothread_requesting_mutex = true;
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300843 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100844 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300845 qemu_mutex_lock(&qemu_global_mutex);
846 }
Paolo Bonzini46daff12011-06-09 13:10:24 +0200847 iothread_requesting_mutex = false;
848 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300849 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000850}
851
852void qemu_mutex_unlock_iothread(void)
853{
854 qemu_mutex_unlock(&qemu_global_mutex);
855}
856
857static int all_vcpus_paused(void)
858{
859 CPUState *penv = first_cpu;
860
861 while (penv) {
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100862 if (!penv->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000863 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100864 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000865 penv = (CPUState *)penv->next_cpu;
866 }
867
868 return 1;
869}
870
871void pause_all_vcpus(void)
872{
873 CPUState *penv = first_cpu;
874
Paolo Bonzinia5c57d62011-09-12 14:40:36 +0200875 qemu_clock_enable(vm_clock, false);
Blue Swirl296af7c2010-03-29 19:23:50 +0000876 while (penv) {
877 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000878 qemu_cpu_kick(penv);
879 penv = (CPUState *)penv->next_cpu;
880 }
881
882 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +0100883 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000884 penv = first_cpu;
885 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300886 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000887 penv = (CPUState *)penv->next_cpu;
888 }
889 }
890}
891
892void resume_all_vcpus(void)
893{
894 CPUState *penv = first_cpu;
895
896 while (penv) {
897 penv->stop = 0;
898 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000899 qemu_cpu_kick(penv);
900 penv = (CPUState *)penv->next_cpu;
901 }
902}
903
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100904static void qemu_tcg_init_vcpu(void *_env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000905{
906 CPUState *env = _env;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100907
Blue Swirl296af7c2010-03-29 19:23:50 +0000908 /* share a single thread for all cpus with TCG */
909 if (!tcg_cpu_thread) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500910 env->thread = g_malloc0(sizeof(QemuThread));
911 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +0000912 qemu_cond_init(env->halt_cond);
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200913 tcg_halt_cond = env->halt_cond;
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100914 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100915 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100916 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100917 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000918 tcg_cpu_thread = env->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +0000919 } else {
920 env->thread = tcg_cpu_thread;
921 env->halt_cond = tcg_halt_cond;
922 }
923}
924
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100925static void qemu_kvm_start_vcpu(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000926{
Anthony Liguori7267c092011-08-20 22:09:37 -0500927 env->thread = g_malloc0(sizeof(QemuThread));
928 env->halt_cond = g_malloc0(sizeof(QemuCond));
Blue Swirl296af7c2010-03-29 19:23:50 +0000929 qemu_cond_init(env->halt_cond);
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100930 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100931 while (env->created == 0) {
Paolo Bonzini18a85722011-03-12 17:44:03 +0100932 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100933 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000934}
935
936void qemu_init_vcpu(void *_env)
937{
938 CPUState *env = _env;
939
940 env->nr_cores = smp_cores;
941 env->nr_threads = smp_threads;
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200942 env->stopped = 1;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100943 if (kvm_enabled()) {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100944 qemu_kvm_start_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100945 } else {
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100946 qemu_tcg_init_vcpu(env);
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100947 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000948}
949
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100950void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000951{
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100952 if (cpu_single_env) {
Paolo Bonzini67bb1722011-03-12 17:43:59 +0100953 cpu_single_env->stop = 0;
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100954 cpu_single_env->stopped = 1;
955 cpu_exit(cpu_single_env);
Paolo Bonzini67bb1722011-03-12 17:43:59 +0100956 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100957 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000958}
959
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300960void vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000961{
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100962 if (!qemu_thread_is_self(&io_thread)) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300963 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000964 /*
965 * FIXME: should not return to device code in case
966 * vm_stop() has been requested.
967 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100968 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +0000969 return;
970 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300971 do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000972}
973
Luiz Capitulino8a9236f2011-10-14 11:18:09 -0300974/* does a state transition even if the VM is already stopped,
975 current state is forgotten forever */
976void vm_stop_force_state(RunState state)
977{
978 if (runstate_is_running()) {
979 vm_stop(state);
980 } else {
981 runstate_set(state);
982 }
983}
984
Jan Kiszka6792a572011-02-07 12:19:18 +0100985static int tcg_cpu_exec(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000986{
987 int ret;
988#ifdef CONFIG_PROFILER
989 int64_t ti;
990#endif
991
992#ifdef CONFIG_PROFILER
993 ti = profile_getclock();
994#endif
995 if (use_icount) {
996 int64_t count;
997 int decr;
998 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
999 env->icount_decr.u16.low = 0;
1000 env->icount_extra = 0;
Paolo Bonzini946fb272011-09-12 13:57:37 +02001001 count = qemu_icount_round(qemu_clock_deadline(vm_clock));
Blue Swirl296af7c2010-03-29 19:23:50 +00001002 qemu_icount += count;
1003 decr = (count > 0xffff) ? 0xffff : count;
1004 count -= decr;
1005 env->icount_decr.u16.low = decr;
1006 env->icount_extra = count;
1007 }
1008 ret = cpu_exec(env);
1009#ifdef CONFIG_PROFILER
1010 qemu_time += profile_getclock() - ti;
1011#endif
1012 if (use_icount) {
1013 /* Fold pending instructions back into the
1014 instruction counter, and clear the interrupt flag. */
1015 qemu_icount -= (env->icount_decr.u16.low
1016 + env->icount_extra);
1017 env->icount_decr.u32 = 0;
1018 env->icount_extra = 0;
1019 }
1020 return ret;
1021}
1022
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001023bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001024{
Jan Kiszka9a360852011-02-01 22:15:55 +01001025 int r;
1026
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001027 /* Account partial waits to the vm_clock. */
1028 qemu_clock_warp(vm_clock);
1029
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001030 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001031 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001032 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001033 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +02001034 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001035
1036 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +02001037 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001038
Jan Kiszka3c638d02010-06-25 16:56:56 +02001039 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +01001040 if (kvm_enabled()) {
Jan Kiszka6792a572011-02-07 12:19:18 +01001041 r = kvm_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001042 qemu_kvm_eat_signals(env);
Jan Kiszka6792a572011-02-07 12:19:18 +01001043 } else {
1044 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001045 }
1046 if (r == EXCP_DEBUG) {
Jan Kiszka1009d2e2011-03-15 12:26:13 +01001047 cpu_handle_guest_debug(env);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001048 break;
1049 }
Paolo Bonzinidf646df2011-03-12 17:43:58 +01001050 } else if (env->stop || env->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001051 break;
1052 }
1053 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001054 exit_request = 0;
Jan Kiszka16400322011-02-09 16:29:37 +01001055 return !all_cpu_threads_idle();
Blue Swirl296af7c2010-03-29 19:23:50 +00001056}
1057
1058void set_numa_modes(void)
1059{
1060 CPUState *env;
1061 int i;
1062
1063 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1064 for (i = 0; i < nb_numa_nodes; i++) {
1065 if (node_cpumask[i] & (1 << env->cpu_index)) {
1066 env->numa_node = i;
1067 }
1068 }
1069 }
1070}
1071
1072void set_cpu_log(const char *optarg)
1073{
1074 int mask;
1075 const CPULogItem *item;
1076
1077 mask = cpu_str_to_log_mask(optarg);
1078 if (!mask) {
1079 printf("Log items (comma separated):\n");
1080 for (item = cpu_log_items; item->mask != 0; item++) {
1081 printf("%-10s %s\n", item->name, item->help);
1082 }
1083 exit(1);
1084 }
1085 cpu_set_log(mask);
1086}
Blue Swirl29e922b2010-03-29 19:24:00 +00001087
Matthew Fernandezc235d732011-06-07 16:32:40 +00001088void set_cpu_log_filename(const char *optarg)
1089{
1090 cpu_set_log_filename(optarg);
1091}
1092
Stefan Weil9a78eea2010-10-22 23:03:33 +02001093void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001094{
1095 /* XXX: implement xxx_cpu_list for targets that still miss it */
1096#if defined(cpu_list_id)
1097 cpu_list_id(f, cpu_fprintf, optarg);
1098#elif defined(cpu_list)
1099 cpu_list(f, cpu_fprintf); /* deprecated */
1100#endif
1101}