blob: f07533530c0453e0506ff65edf081dbf527da4d0 [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
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010029#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010030#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/dma.h"
32#include "sysemu/kvm.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030033#include "qmp-commands.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000034
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010036#include "sysemu/cpus.h"
37#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/main-loop.h"
39#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080040#include "qemu/seqlock.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020041
42#ifndef _WIN32
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020044#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000045
Jan Kiszka6d9cb732011-02-01 22:15:58 +010046#ifdef CONFIG_LINUX
47
48#include <sys/prctl.h>
49
Marcelo Tosattic0532a72010-10-11 15:31:21 -030050#ifndef PR_MCE_KILL
51#define PR_MCE_KILL 33
52#endif
53
Jan Kiszka6d9cb732011-02-01 22:15:58 +010054#ifndef PR_MCE_KILL_SET
55#define PR_MCE_KILL_SET 1
56#endif
57
58#ifndef PR_MCE_KILL_EARLY
59#define PR_MCE_KILL_EARLY 1
60#endif
61
62#endif /* CONFIG_LINUX */
63
Andreas Färber182735e2013-05-29 22:29:20 +020064static CPUState *next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +000065
Tiejun Chen321bc0b2013-08-02 09:43:09 +080066bool cpu_is_stopped(CPUState *cpu)
67{
68 return cpu->stopped || !runstate_is_running();
69}
70
Andreas Färbera98ae1d2013-05-26 23:21:08 +020071static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010072{
Andreas Färberc64ca812012-05-03 02:11:45 +020073 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010074 return false;
75 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080076 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010077 return true;
78 }
Andreas Färber259186a2013-01-17 18:51:17 +010079 if (!cpu->halted || qemu_cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020080 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010081 return false;
82 }
83 return true;
84}
85
86static bool all_cpu_threads_idle(void)
87{
Andreas Färber182735e2013-05-29 22:29:20 +020088 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +010089
Andreas Färberbdc44642013-06-24 23:50:24 +020090 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +020091 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010092 return false;
93 }
94 }
95 return true;
96}
97
Blue Swirl296af7c2010-03-29 19:23:50 +000098/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +020099/* guest cycle counter */
100
101/* Conversion factor from emulated instructions to virtual clock ticks. */
102static int icount_time_shift;
103/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
104#define MAX_ICOUNT_SHIFT 10
105/* Compensate for varying guest execution speed. */
106static int64_t qemu_icount_bias;
107static QEMUTimer *icount_rt_timer;
108static QEMUTimer *icount_vm_timer;
109static QEMUTimer *icount_warp_timer;
110static int64_t vm_clock_warp_start;
111static int64_t qemu_icount;
112
113typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800114 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200115 int64_t cpu_ticks_prev;
116 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800117
118 /* cpu_clock_offset can be read out of BQL, so protect it with
119 * this lock.
120 */
121 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200122 int64_t cpu_clock_offset;
123 int32_t cpu_ticks_enabled;
124 int64_t dummy;
125} TimersState;
126
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000127static TimersState timers_state;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200128
129/* Return the virtual CPU time, based on the instruction counter. */
130int64_t cpu_get_icount(void)
131{
132 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200133 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200134
135 icount = qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200136 if (cpu) {
137 CPUArchState *env = cpu->env_ptr;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200138 if (!can_do_io(env)) {
139 fprintf(stderr, "Bad clock read\n");
140 }
141 icount -= (env->icount_decr.u16.low + env->icount_extra);
142 }
143 return qemu_icount_bias + (icount << icount_time_shift);
144}
145
146/* return the host CPU cycle counter and handle stop/restart */
Liu Ping Fancb365642013-09-25 14:20:58 +0800147/* Caller must hold the BQL */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200148int64_t cpu_get_ticks(void)
149{
150 if (use_icount) {
151 return cpu_get_icount();
152 }
153 if (!timers_state.cpu_ticks_enabled) {
154 return timers_state.cpu_ticks_offset;
155 } else {
156 int64_t ticks;
157 ticks = cpu_get_real_ticks();
158 if (timers_state.cpu_ticks_prev > ticks) {
159 /* Note: non increasing ticks may happen if the host uses
160 software suspend */
161 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
162 }
163 timers_state.cpu_ticks_prev = ticks;
164 return ticks + timers_state.cpu_ticks_offset;
165 }
166}
167
Liu Ping Fancb365642013-09-25 14:20:58 +0800168static int64_t cpu_get_clock_locked(void)
169{
170 int64_t ti;
171
172 if (!timers_state.cpu_ticks_enabled) {
173 ti = timers_state.cpu_clock_offset;
174 } else {
175 ti = get_clock();
176 ti += timers_state.cpu_clock_offset;
177 }
178
179 return ti;
180}
181
Paolo Bonzini946fb272011-09-12 13:57:37 +0200182/* return the host CPU monotonic timer and handle stop/restart */
183int64_t cpu_get_clock(void)
184{
185 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800186 unsigned start;
187
188 do {
189 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
190 ti = cpu_get_clock_locked();
191 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
192
193 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200194}
195
Liu Ping Fancb365642013-09-25 14:20:58 +0800196/* enable cpu_get_ticks()
197 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
198 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200199void cpu_enable_ticks(void)
200{
Liu Ping Fancb365642013-09-25 14:20:58 +0800201 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
202 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200203 if (!timers_state.cpu_ticks_enabled) {
204 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
205 timers_state.cpu_clock_offset -= get_clock();
206 timers_state.cpu_ticks_enabled = 1;
207 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800208 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200209}
210
211/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800212 * cpu_get_ticks() after that.
213 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
214 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200215void cpu_disable_ticks(void)
216{
Liu Ping Fancb365642013-09-25 14:20:58 +0800217 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
218 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200219 if (timers_state.cpu_ticks_enabled) {
220 timers_state.cpu_ticks_offset = cpu_get_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800221 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200222 timers_state.cpu_ticks_enabled = 0;
223 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800224 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200225}
226
227/* Correlation between real and virtual time is always going to be
228 fairly approximate, so ignore small variation.
229 When the guest is idle real and virtual time will be aligned in
230 the IO wait loop. */
231#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
232
233static void icount_adjust(void)
234{
235 int64_t cur_time;
236 int64_t cur_icount;
237 int64_t delta;
238 static int64_t last_delta;
239 /* If the VM is not running, then do nothing. */
240 if (!runstate_is_running()) {
241 return;
242 }
243 cur_time = cpu_get_clock();
Alex Bligh40daca52013-08-21 16:03:02 +0100244 cur_icount = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200245 delta = cur_icount - cur_time;
246 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
247 if (delta > 0
248 && last_delta + ICOUNT_WOBBLE < delta * 2
249 && icount_time_shift > 0) {
250 /* The guest is getting too far ahead. Slow time down. */
251 icount_time_shift--;
252 }
253 if (delta < 0
254 && last_delta - ICOUNT_WOBBLE > delta * 2
255 && icount_time_shift < MAX_ICOUNT_SHIFT) {
256 /* The guest is getting too far behind. Speed time up. */
257 icount_time_shift++;
258 }
259 last_delta = delta;
260 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
261}
262
263static void icount_adjust_rt(void *opaque)
264{
Alex Bligh40daca52013-08-21 16:03:02 +0100265 timer_mod(icount_rt_timer,
266 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200267 icount_adjust();
268}
269
270static void icount_adjust_vm(void *opaque)
271{
Alex Bligh40daca52013-08-21 16:03:02 +0100272 timer_mod(icount_vm_timer,
273 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
274 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200275 icount_adjust();
276}
277
278static int64_t qemu_icount_round(int64_t count)
279{
280 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
281}
282
283static void icount_warp_rt(void *opaque)
284{
285 if (vm_clock_warp_start == -1) {
286 return;
287 }
288
289 if (runstate_is_running()) {
Alex Bligh40daca52013-08-21 16:03:02 +0100290 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200291 int64_t warp_delta = clock - vm_clock_warp_start;
292 if (use_icount == 1) {
293 qemu_icount_bias += warp_delta;
294 } else {
295 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100296 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200297 * far ahead of real time.
298 */
299 int64_t cur_time = cpu_get_clock();
Alex Bligh40daca52013-08-21 16:03:02 +0100300 int64_t cur_icount = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200301 int64_t delta = cur_time - cur_icount;
302 qemu_icount_bias += MIN(warp_delta, delta);
303 }
Alex Bligh40daca52013-08-21 16:03:02 +0100304 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
305 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200306 }
307 }
308 vm_clock_warp_start = -1;
309}
310
Paolo Bonzini8156be52012-03-28 15:42:04 +0200311void qtest_clock_warp(int64_t dest)
312{
Alex Bligh40daca52013-08-21 16:03:02 +0100313 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200314 assert(qtest_enabled());
315 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100316 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200317 int64_t warp = MIN(dest - clock, deadline);
318 qemu_icount_bias += warp;
Alex Bligh40daca52013-08-21 16:03:02 +0100319 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
320 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200321 }
Alex Bligh40daca52013-08-21 16:03:02 +0100322 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200323}
324
Alex Bligh40daca52013-08-21 16:03:02 +0100325void qemu_clock_warp(QEMUClockType type)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200326{
327 int64_t deadline;
328
329 /*
330 * There are too many global variables to make the "warp" behavior
331 * applicable to other clocks. But a clock argument removes the
332 * need for if statements all over the place.
333 */
Alex Bligh40daca52013-08-21 16:03:02 +0100334 if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200335 return;
336 }
337
338 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100339 * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
340 * This ensures that the deadline for the timer is computed correctly below.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200341 * This also makes sure that the insn counter is synchronized before the
342 * CPU starts running, in case the CPU is woken by an event other than
Alex Bligh40daca52013-08-21 16:03:02 +0100343 * the earliest QEMU_CLOCK_VIRTUAL timer.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200344 */
345 icount_warp_rt(NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100346 if (!all_cpu_threads_idle() || !qemu_clock_has_timers(QEMU_CLOCK_VIRTUAL)) {
347 timer_del(icount_warp_timer);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200348 return;
349 }
350
Paolo Bonzini8156be52012-03-28 15:42:04 +0200351 if (qtest_enabled()) {
352 /* When testing, qtest commands advance icount. */
353 return;
354 }
355
Alex Bligh40daca52013-08-21 16:03:02 +0100356 vm_clock_warp_start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
Alex Blighac70aaf2013-08-21 16:02:57 +0100357 /* We want to use the earliest deadline from ALL vm_clocks */
Alex Bligh40daca52013-08-21 16:03:02 +0100358 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +0100359
360 /* Maintain prior (possibly buggy) behaviour where if no deadline
Alex Bligh40daca52013-08-21 16:03:02 +0100361 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
Alex Blighac70aaf2013-08-21 16:02:57 +0100362 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
363 * nanoseconds.
364 */
365 if ((deadline < 0) || (deadline > INT32_MAX)) {
366 deadline = INT32_MAX;
367 }
368
Paolo Bonzini946fb272011-09-12 13:57:37 +0200369 if (deadline > 0) {
370 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100371 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200372 * sleep. Otherwise, the CPU might be waiting for a future timer
373 * interrupt to wake it up, but the interrupt never comes because
374 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100375 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200376 *
377 * An extreme solution for this problem would be to never let VCPUs
Alex Bligh40daca52013-08-21 16:03:02 +0100378 * sleep in icount mode if there is a pending QEMU_CLOCK_VIRTUAL
379 * timer; rather time could just advance to the next QEMU_CLOCK_VIRTUAL
380 * event. Instead, we do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL
381 * after some e"real" time, (related to the time left until the next
382 * event) has passed. The QEMU_CLOCK_REALTIME timer will do this.
383 * This avoids that the warps are visible externally; for example,
384 * you will not be sending network packets continuously instead of
385 * every 100ms.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200386 */
Alex Bligh40daca52013-08-21 16:03:02 +0100387 timer_mod(icount_warp_timer, vm_clock_warp_start + deadline);
Alex Blighac70aaf2013-08-21 16:02:57 +0100388 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100389 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200390 }
391}
392
393static const VMStateDescription vmstate_timers = {
394 .name = "timer",
395 .version_id = 2,
396 .minimum_version_id = 1,
397 .minimum_version_id_old = 1,
398 .fields = (VMStateField[]) {
399 VMSTATE_INT64(cpu_ticks_offset, TimersState),
400 VMSTATE_INT64(dummy, TimersState),
401 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
402 VMSTATE_END_OF_LIST()
403 }
404};
405
406void configure_icount(const char *option)
407{
Liu Ping Fancb365642013-09-25 14:20:58 +0800408 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200409 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
410 if (!option) {
411 return;
412 }
413
Alex Bligh40daca52013-08-21 16:03:02 +0100414 icount_warp_timer = timer_new_ns(QEMU_CLOCK_REALTIME,
415 icount_warp_rt, NULL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200416 if (strcmp(option, "auto") != 0) {
417 icount_time_shift = strtol(option, NULL, 0);
418 use_icount = 1;
419 return;
420 }
421
422 use_icount = 2;
423
424 /* 125MIPS seems a reasonable initial guess at the guest speed.
425 It will be corrected fairly quickly anyway. */
426 icount_time_shift = 3;
427
428 /* Have both realtime and virtual time triggers for speed adjustment.
429 The realtime trigger catches emulated time passing too slowly,
430 the virtual time trigger catches emulated time passing too fast.
431 Realtime triggers occur even when idle, so use them less frequently
432 than VM triggers. */
Alex Bligh40daca52013-08-21 16:03:02 +0100433 icount_rt_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
434 icount_adjust_rt, NULL);
435 timer_mod(icount_rt_timer,
436 qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
437 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
438 icount_adjust_vm, NULL);
439 timer_mod(icount_vm_timer,
440 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
441 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200442}
443
444/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000445void hw_error(const char *fmt, ...)
446{
447 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100448 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000449
450 va_start(ap, fmt);
451 fprintf(stderr, "qemu: hardware error: ");
452 vfprintf(stderr, fmt, ap);
453 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200454 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100455 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200456 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000457 }
458 va_end(ap);
459 abort();
460}
461
462void cpu_synchronize_all_states(void)
463{
Andreas Färber182735e2013-05-29 22:29:20 +0200464 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000465
Andreas Färberbdc44642013-06-24 23:50:24 +0200466 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200467 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000468 }
469}
470
471void cpu_synchronize_all_post_reset(void)
472{
Andreas Färber182735e2013-05-29 22:29:20 +0200473 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000474
Andreas Färberbdc44642013-06-24 23:50:24 +0200475 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200476 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000477 }
478}
479
480void cpu_synchronize_all_post_init(void)
481{
Andreas Färber182735e2013-05-29 22:29:20 +0200482 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000483
Andreas Färberbdc44642013-06-24 23:50:24 +0200484 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200485 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000486 }
487}
488
Kevin Wolf56983462013-07-05 13:49:54 +0200489static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000490{
Kevin Wolf56983462013-07-05 13:49:54 +0200491 int ret = 0;
492
Luiz Capitulino13548692011-07-29 15:36:43 -0300493 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000494 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000495 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300496 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300497 vm_state_notify(0, state);
Blue Swirl296af7c2010-03-29 19:23:50 +0000498 monitor_protocol_event(QEVENT_STOP, NULL);
499 }
Kevin Wolf56983462013-07-05 13:49:54 +0200500
Kevin Wolf594a45c2013-07-18 14:52:19 +0200501 bdrv_drain_all();
502 ret = bdrv_flush_all();
503
Kevin Wolf56983462013-07-05 13:49:54 +0200504 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000505}
506
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200507static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000508{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200509 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200510 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100511 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800512 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200513 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100514 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200515 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000516}
517
Andreas Färber91325042013-05-27 02:07:49 +0200518static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200519{
Andreas Färber64f6b342013-05-27 02:06:09 +0200520 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100521 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200522 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200523}
524
Paolo Bonzini714bd042011-03-12 17:44:06 +0100525static void cpu_signal(int sig)
526{
Andreas Färber4917cf42013-05-27 05:17:50 +0200527 if (current_cpu) {
528 cpu_exit(current_cpu);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100529 }
530 exit_request = 1;
531}
Paolo Bonzini714bd042011-03-12 17:44:06 +0100532
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100533#ifdef CONFIG_LINUX
534static void sigbus_reraise(void)
535{
536 sigset_t set;
537 struct sigaction action;
538
539 memset(&action, 0, sizeof(action));
540 action.sa_handler = SIG_DFL;
541 if (!sigaction(SIGBUS, &action, NULL)) {
542 raise(SIGBUS);
543 sigemptyset(&set);
544 sigaddset(&set, SIGBUS);
545 sigprocmask(SIG_UNBLOCK, &set, NULL);
546 }
547 perror("Failed to re-raise SIGBUS!\n");
548 abort();
549}
550
551static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
552 void *ctx)
553{
554 if (kvm_on_sigbus(siginfo->ssi_code,
555 (void *)(intptr_t)siginfo->ssi_addr)) {
556 sigbus_reraise();
557 }
558}
559
560static void qemu_init_sigbus(void)
561{
562 struct sigaction action;
563
564 memset(&action, 0, sizeof(action));
565 action.sa_flags = SA_SIGINFO;
566 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
567 sigaction(SIGBUS, &action, NULL);
568
569 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
570}
571
Andreas Färber290adf32013-01-17 09:30:27 +0100572static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100573{
574 struct timespec ts = { 0, 0 };
575 siginfo_t siginfo;
576 sigset_t waitset;
577 sigset_t chkset;
578 int r;
579
580 sigemptyset(&waitset);
581 sigaddset(&waitset, SIG_IPI);
582 sigaddset(&waitset, SIGBUS);
583
584 do {
585 r = sigtimedwait(&waitset, &siginfo, &ts);
586 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
587 perror("sigtimedwait");
588 exit(1);
589 }
590
591 switch (r) {
592 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100593 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100594 sigbus_reraise();
595 }
596 break;
597 default:
598 break;
599 }
600
601 r = sigpending(&chkset);
602 if (r == -1) {
603 perror("sigpending");
604 exit(1);
605 }
606 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100607}
608
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100609#else /* !CONFIG_LINUX */
610
611static void qemu_init_sigbus(void)
612{
613}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100614
Andreas Färber290adf32013-01-17 09:30:27 +0100615static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100616{
617}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100618#endif /* !CONFIG_LINUX */
619
Blue Swirl296af7c2010-03-29 19:23:50 +0000620#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100621static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000622{
623}
624
Andreas Färber13618e02013-05-26 23:41:00 +0200625static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100626{
627 int r;
628 sigset_t set;
629 struct sigaction sigact;
630
631 memset(&sigact, 0, sizeof(sigact));
632 sigact.sa_handler = dummy_signal;
633 sigaction(SIG_IPI, &sigact, NULL);
634
Paolo Bonzini714bd042011-03-12 17:44:06 +0100635 pthread_sigmask(SIG_BLOCK, NULL, &set);
636 sigdelset(&set, SIG_IPI);
637 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200638 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100639 if (r) {
640 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
641 exit(1);
642 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100643}
644
645static void qemu_tcg_init_cpu_signals(void)
646{
Paolo Bonzini714bd042011-03-12 17:44:06 +0100647 sigset_t set;
648 struct sigaction sigact;
649
650 memset(&sigact, 0, sizeof(sigact));
651 sigact.sa_handler = cpu_signal;
652 sigaction(SIG_IPI, &sigact, NULL);
653
654 sigemptyset(&set);
655 sigaddset(&set, SIG_IPI);
656 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100657}
658
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100659#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200660static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100661{
662 abort();
663}
664
665static void qemu_tcg_init_cpu_signals(void)
666{
667}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100668#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000669
Stefan Weilb2532d82012-09-27 07:41:42 +0200670static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200671static QemuCond qemu_io_proceeded_cond;
672static bool iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000673
674static QemuThread io_thread;
675
676static QemuThread *tcg_cpu_thread;
677static QemuCond *tcg_halt_cond;
678
Blue Swirl296af7c2010-03-29 19:23:50 +0000679/* cpu creation */
680static QemuCond qemu_cpu_cond;
681/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000682static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300683static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000684
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200685void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000686{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100687 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100688 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100689 qemu_cond_init(&qemu_pause_cond);
690 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200691 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000692 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000693
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100694 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000695}
696
Andreas Färberf100f0b2012-05-03 14:58:47 +0200697void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300698{
699 struct qemu_work_item wi;
700
Andreas Färber60e82572012-05-02 22:23:49 +0200701 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300702 func(data);
703 return;
704 }
705
706 wi.func = func;
707 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600708 wi.free = false;
Andreas Färberc64ca812012-05-03 02:11:45 +0200709 if (cpu->queued_work_first == NULL) {
710 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100711 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200712 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100713 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200714 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300715 wi.next = NULL;
716 wi.done = false;
717
Andreas Färberc08d7422012-05-03 04:34:15 +0200718 qemu_cpu_kick(cpu);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300719 while (!wi.done) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200720 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300721
722 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200723 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300724 }
725}
726
Chegu Vinod3c022702013-06-24 03:49:41 -0600727void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
728{
729 struct qemu_work_item *wi;
730
731 if (qemu_cpu_is_self(cpu)) {
732 func(data);
733 return;
734 }
735
736 wi = g_malloc0(sizeof(struct qemu_work_item));
737 wi->func = func;
738 wi->data = data;
739 wi->free = true;
740 if (cpu->queued_work_first == NULL) {
741 cpu->queued_work_first = wi;
742 } else {
743 cpu->queued_work_last->next = wi;
744 }
745 cpu->queued_work_last = wi;
746 wi->next = NULL;
747 wi->done = false;
748
749 qemu_cpu_kick(cpu);
750}
751
Andreas Färber6d45b102012-05-03 02:13:22 +0200752static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300753{
754 struct qemu_work_item *wi;
755
Andreas Färberc64ca812012-05-03 02:11:45 +0200756 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300757 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100758 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300759
Andreas Färberc64ca812012-05-03 02:11:45 +0200760 while ((wi = cpu->queued_work_first)) {
761 cpu->queued_work_first = wi->next;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300762 wi->func(wi->data);
763 wi->done = true;
Chegu Vinod3c022702013-06-24 03:49:41 -0600764 if (wi->free) {
765 g_free(wi);
766 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300767 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200768 cpu->queued_work_last = NULL;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300769 qemu_cond_broadcast(&qemu_work_cond);
770}
771
Andreas Färber509a0d72012-05-03 02:18:09 +0200772static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000773{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200774 if (cpu->stop) {
775 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +0200776 cpu->stopped = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000777 qemu_cond_signal(&qemu_pause_cond);
778 }
Andreas Färber6d45b102012-05-03 02:13:22 +0200779 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200780 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000781}
782
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200783static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000784{
Andreas Färber182735e2013-05-29 22:29:20 +0200785 CPUState *cpu;
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200786
Jan Kiszka16400322011-02-09 16:29:37 +0100787 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200788 /* Start accounting real time to the virtual clock if the CPUs
789 are idle. */
Alex Bligh40daca52013-08-21 16:03:02 +0100790 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini9705fbb2011-03-12 17:44:00 +0100791 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100792 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000793
Paolo Bonzini46daff12011-06-09 13:10:24 +0200794 while (iothread_requesting_mutex) {
795 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
796 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200797
Andreas Färberbdc44642013-06-24 23:50:24 +0200798 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200799 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200800 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000801}
802
Andreas Färberfd529e82013-05-26 23:24:55 +0200803static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000804{
Andreas Färbera98ae1d2013-05-26 23:21:08 +0200805 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +0200806 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100807 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000808
Andreas Färber290adf32013-01-17 09:30:27 +0100809 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +0200810 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000811}
812
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100813static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000814{
Andreas Färber48a106b2013-05-27 02:20:39 +0200815 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100816 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000817
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300818 qemu_mutex_lock(&qemu_global_mutex);
Andreas Färber814e6122012-05-02 17:00:37 +0200819 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +0200820 cpu->thread_id = qemu_get_thread_id();
Andreas Färber4917cf42013-05-27 05:17:50 +0200821 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000822
Andreas Färber504134d2012-12-17 06:38:45 +0100823 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +0100824 if (r < 0) {
825 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
826 exit(1);
827 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000828
Andreas Färber13618e02013-05-26 23:41:00 +0200829 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000830
831 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +0200832 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000833 qemu_cond_signal(&qemu_cpu_cond);
834
Blue Swirl296af7c2010-03-29 19:23:50 +0000835 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200836 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +0200837 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100838 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +0200839 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +0100840 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100841 }
Andreas Färberfd529e82013-05-26 23:24:55 +0200842 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000843 }
844
845 return NULL;
846}
847
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200848static void *qemu_dummy_cpu_thread_fn(void *arg)
849{
850#ifdef _WIN32
851 fprintf(stderr, "qtest is not supported under Windows\n");
852 exit(1);
853#else
Andreas Färber10a90212013-05-27 02:24:35 +0200854 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200855 sigset_t waitset;
856 int r;
857
858 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +0200859 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +0200860 cpu->thread_id = qemu_get_thread_id();
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200861
862 sigemptyset(&waitset);
863 sigaddset(&waitset, SIG_IPI);
864
865 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +0200866 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200867 qemu_cond_signal(&qemu_cpu_cond);
868
Andreas Färber4917cf42013-05-27 05:17:50 +0200869 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200870 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200871 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200872 qemu_mutex_unlock_iothread();
873 do {
874 int sig;
875 r = sigwait(&waitset, &sig);
876 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
877 if (r == -1) {
878 perror("sigwait");
879 exit(1);
880 }
881 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +0200882 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +0200883 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200884 }
885
886 return NULL;
887#endif
888}
889
Jan Kiszkabdb7ca62011-09-26 09:40:39 +0200890static void tcg_exec_all(void);
891
Jan Kiszka7e97cd82011-02-07 12:19:12 +0100892static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +0000893{
Andreas Färberc3586ba2012-05-03 01:41:24 +0200894 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +0000895
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100896 qemu_tcg_init_cpu_signals();
Andreas Färber814e6122012-05-02 17:00:37 +0200897 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000898
Blue Swirl296af7c2010-03-29 19:23:50 +0000899 qemu_mutex_lock(&qemu_global_mutex);
Andreas Färber38fcbd32013-07-07 19:50:23 +0200900 CPU_FOREACH(cpu) {
901 cpu->thread_id = qemu_get_thread_id();
902 cpu->created = true;
903 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000904 qemu_cond_signal(&qemu_cpu_cond);
905
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200906 /* wait for initial kick-off after machine start */
Andreas Färberbdc44642013-06-24 23:50:24 +0200907 while (QTAILQ_FIRST(&cpus)->stopped) {
Jan Kiszkafa7d1862011-08-22 18:35:25 +0200908 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +0100909
910 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +0200911 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200912 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +0100913 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100914 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000915
916 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +0200917 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +0100918
919 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +0100920 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +0100921
922 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100923 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +0100924 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +0200925 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200926 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000927 }
928
929 return NULL;
930}
931
Andreas Färber2ff09a42012-05-03 00:23:30 +0200932static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100933{
934#ifndef _WIN32
935 int err;
936
Andreas Färber814e6122012-05-02 17:00:37 +0200937 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100938 if (err) {
939 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
940 exit(1);
941 }
942#else /* _WIN32 */
Andreas Färber60e82572012-05-02 22:23:49 +0200943 if (!qemu_cpu_is_self(cpu)) {
Olivier Hainqueed9164a2013-04-09 18:06:53 +0200944 CONTEXT tcgContext;
945
946 if (SuspendThread(cpu->hThread) == (DWORD)-1) {
Stefan Weil7f1721d2013-04-13 22:45:50 +0200947 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
Olivier Hainqueed9164a2013-04-09 18:06:53 +0200948 GetLastError());
949 exit(1);
950 }
951
952 /* On multi-core systems, we are not sure that the thread is actually
953 * suspended until we can get the context.
954 */
955 tcgContext.ContextFlags = CONTEXT_CONTROL;
956 while (GetThreadContext(cpu->hThread, &tcgContext) != 0) {
957 continue;
958 }
959
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100960 cpu_signal(0);
Olivier Hainqueed9164a2013-04-09 18:06:53 +0200961
962 if (ResumeThread(cpu->hThread) == (DWORD)-1) {
Stefan Weil7f1721d2013-04-13 22:45:50 +0200963 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
Olivier Hainqueed9164a2013-04-09 18:06:53 +0200964 GetLastError());
965 exit(1);
966 }
Paolo Bonzinicc015e92011-03-12 17:44:08 +0100967 }
968#endif
969}
970
Andreas Färberc08d7422012-05-03 04:34:15 +0200971void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000972{
Andreas Färberf5c121b2012-05-03 01:22:49 +0200973 qemu_cond_broadcast(cpu->halt_cond);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200974 if (!tcg_enabled() && !cpu->thread_kicked) {
Andreas Färber2ff09a42012-05-03 00:23:30 +0200975 qemu_cpu_kick_thread(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200976 cpu->thread_kicked = true;
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100977 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000978}
979
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100980void qemu_cpu_kick_self(void)
981{
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100982#ifndef _WIN32
Andreas Färber4917cf42013-05-27 05:17:50 +0200983 assert(current_cpu);
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100984
Andreas Färber4917cf42013-05-27 05:17:50 +0200985 if (!current_cpu->thread_kicked) {
986 qemu_cpu_kick_thread(current_cpu);
987 current_cpu->thread_kicked = true;
Jan Kiszka46d62fa2011-02-01 22:15:59 +0100988 }
Paolo Bonzinib55c22c2011-03-12 17:44:07 +0100989#else
990 abort();
991#endif
Blue Swirl296af7c2010-03-29 19:23:50 +0000992}
993
Andreas Färber60e82572012-05-02 22:23:49 +0200994bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000995{
Andreas Färber814e6122012-05-02 17:00:37 +0200996 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000997}
998
Juan Quintelaaa723c22012-09-18 16:30:11 +0200999static bool qemu_in_vcpu_thread(void)
1000{
Andreas Färber4917cf42013-05-27 05:17:50 +02001001 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001002}
1003
Blue Swirl296af7c2010-03-29 19:23:50 +00001004void qemu_mutex_lock_iothread(void)
1005{
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001006 if (!tcg_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001007 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001008 } else {
Paolo Bonzini46daff12011-06-09 13:10:24 +02001009 iothread_requesting_mutex = true;
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001010 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001011 qemu_cpu_kick_thread(first_cpu);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001012 qemu_mutex_lock(&qemu_global_mutex);
1013 }
Paolo Bonzini46daff12011-06-09 13:10:24 +02001014 iothread_requesting_mutex = false;
1015 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001016 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001017}
1018
1019void qemu_mutex_unlock_iothread(void)
1020{
1021 qemu_mutex_unlock(&qemu_global_mutex);
1022}
1023
1024static int all_vcpus_paused(void)
1025{
Andreas Färberbdc44642013-06-24 23:50:24 +02001026 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001027
Andreas Färberbdc44642013-06-24 23:50:24 +02001028 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001029 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001030 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001031 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001032 }
1033
1034 return 1;
1035}
1036
1037void pause_all_vcpus(void)
1038{
Andreas Färberbdc44642013-06-24 23:50:24 +02001039 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001040
Alex Bligh40daca52013-08-21 16:03:02 +01001041 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001042 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001043 cpu->stop = true;
1044 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001045 }
1046
Juan Quintelaaa723c22012-09-18 16:30:11 +02001047 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001048 cpu_stop_current();
1049 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001050 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001051 cpu->stop = false;
1052 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001053 }
1054 return;
1055 }
1056 }
1057
Blue Swirl296af7c2010-03-29 19:23:50 +00001058 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001059 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001060 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001061 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001062 }
1063 }
1064}
1065
Igor Mammedov29936832013-04-23 10:29:37 +02001066void cpu_resume(CPUState *cpu)
1067{
1068 cpu->stop = false;
1069 cpu->stopped = false;
1070 qemu_cpu_kick(cpu);
1071}
1072
Blue Swirl296af7c2010-03-29 19:23:50 +00001073void resume_all_vcpus(void)
1074{
Andreas Färberbdc44642013-06-24 23:50:24 +02001075 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001076
Alex Bligh40daca52013-08-21 16:03:02 +01001077 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001078 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001079 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001080 }
1081}
1082
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001083static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001084{
Blue Swirl296af7c2010-03-29 19:23:50 +00001085 /* share a single thread for all cpus with TCG */
1086 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001087 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001088 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1089 qemu_cond_init(cpu->halt_cond);
1090 tcg_halt_cond = cpu->halt_cond;
Andreas Färberc3586ba2012-05-03 01:41:24 +02001091 qemu_thread_create(cpu->thread, qemu_tcg_cpu_thread_fn, cpu,
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001092 QEMU_THREAD_JOINABLE);
1093#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001094 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001095#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001096 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001097 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001098 }
Andreas Färber814e6122012-05-02 17:00:37 +02001099 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001100 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001101 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001102 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001103 }
1104}
1105
Andreas Färber48a106b2013-05-27 02:20:39 +02001106static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001107{
Andreas Färber814e6122012-05-02 17:00:37 +02001108 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001109 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1110 qemu_cond_init(cpu->halt_cond);
Andreas Färber48a106b2013-05-27 02:20:39 +02001111 qemu_thread_create(cpu->thread, qemu_kvm_cpu_thread_fn, cpu,
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001112 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001113 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001114 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001115 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001116}
1117
Andreas Färber10a90212013-05-27 02:24:35 +02001118static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001119{
Andreas Färber814e6122012-05-02 17:00:37 +02001120 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001121 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1122 qemu_cond_init(cpu->halt_cond);
Andreas Färber10a90212013-05-27 02:24:35 +02001123 qemu_thread_create(cpu->thread, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001124 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001125 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001126 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1127 }
1128}
1129
Andreas Färberc643bed2013-05-27 03:23:24 +02001130void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001131{
Andreas Färberce3960e2012-12-17 03:27:07 +01001132 cpu->nr_cores = smp_cores;
1133 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001134 cpu->stopped = true;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001135 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001136 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001137 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001138 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001139 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001140 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001141 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001142}
1143
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001144void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001145{
Andreas Färber4917cf42013-05-27 05:17:50 +02001146 if (current_cpu) {
1147 current_cpu->stop = false;
1148 current_cpu->stopped = true;
1149 cpu_exit(current_cpu);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001150 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001151 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001152}
1153
Kevin Wolf56983462013-07-05 13:49:54 +02001154int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001155{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001156 if (qemu_in_vcpu_thread()) {
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001157 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001158 /*
1159 * FIXME: should not return to device code in case
1160 * vm_stop() has been requested.
1161 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001162 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001163 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001164 }
Kevin Wolf56983462013-07-05 13:49:54 +02001165
1166 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001167}
1168
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001169/* does a state transition even if the VM is already stopped,
1170 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001171int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001172{
1173 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001174 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001175 } else {
1176 runstate_set(state);
Kevin Wolf594a45c2013-07-18 14:52:19 +02001177 /* Make sure to return an error if the flush in a previous vm_stop()
1178 * failed. */
1179 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001180 }
1181}
1182
Andreas Färber9349b4f2012-03-14 01:38:32 +01001183static int tcg_cpu_exec(CPUArchState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +00001184{
1185 int ret;
1186#ifdef CONFIG_PROFILER
1187 int64_t ti;
1188#endif
1189
1190#ifdef CONFIG_PROFILER
1191 ti = profile_getclock();
1192#endif
1193 if (use_icount) {
1194 int64_t count;
Alex Blighac70aaf2013-08-21 16:02:57 +01001195 int64_t deadline;
Blue Swirl296af7c2010-03-29 19:23:50 +00001196 int decr;
1197 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1198 env->icount_decr.u16.low = 0;
1199 env->icount_extra = 0;
Alex Bligh40daca52013-08-21 16:03:02 +01001200 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001201
1202 /* Maintain prior (possibly buggy) behaviour where if no deadline
Alex Bligh40daca52013-08-21 16:03:02 +01001203 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
Alex Blighac70aaf2013-08-21 16:02:57 +01001204 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1205 * nanoseconds.
1206 */
1207 if ((deadline < 0) || (deadline > INT32_MAX)) {
1208 deadline = INT32_MAX;
1209 }
1210
1211 count = qemu_icount_round(deadline);
Blue Swirl296af7c2010-03-29 19:23:50 +00001212 qemu_icount += count;
1213 decr = (count > 0xffff) ? 0xffff : count;
1214 count -= decr;
1215 env->icount_decr.u16.low = decr;
1216 env->icount_extra = count;
1217 }
1218 ret = cpu_exec(env);
1219#ifdef CONFIG_PROFILER
1220 qemu_time += profile_getclock() - ti;
1221#endif
1222 if (use_icount) {
1223 /* Fold pending instructions back into the
1224 instruction counter, and clear the interrupt flag. */
1225 qemu_icount -= (env->icount_decr.u16.low
1226 + env->icount_extra);
1227 env->icount_decr.u32 = 0;
1228 env->icount_extra = 0;
1229 }
1230 return ret;
1231}
1232
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001233static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001234{
Jan Kiszka9a360852011-02-01 22:15:55 +01001235 int r;
1236
Alex Bligh40daca52013-08-21 16:03:02 +01001237 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1238 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001239
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001240 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001241 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001242 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001243 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001244 CPUState *cpu = next_cpu;
1245 CPUArchState *env = cpu->env_ptr;
Blue Swirl296af7c2010-03-29 19:23:50 +00001246
Alex Bligh40daca52013-08-21 16:03:02 +01001247 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001248 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001249
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001250 if (cpu_can_run(cpu)) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001251 r = tcg_cpu_exec(env);
Jan Kiszka9a360852011-02-01 22:15:55 +01001252 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001253 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001254 break;
1255 }
Andreas Färberf324e762012-05-02 23:26:21 +02001256 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001257 break;
1258 }
1259 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +02001260 exit_request = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001261}
1262
1263void set_numa_modes(void)
1264{
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001265 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001266 int i;
1267
Andreas Färberbdc44642013-06-24 23:50:24 +02001268 CPU_FOREACH(cpu) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001269 for (i = 0; i < nb_numa_nodes; i++) {
Andreas Färber55e5c282012-12-17 06:18:02 +01001270 if (test_bit(cpu->cpu_index, node_cpumask[i])) {
Andreas Färber1b1ed8d2012-12-17 04:22:03 +01001271 cpu->numa_node = i;
Blue Swirl296af7c2010-03-29 19:23:50 +00001272 }
1273 }
1274 }
1275}
1276
Stefan Weil9a78eea2010-10-22 23:03:33 +02001277void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001278{
1279 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001280#if defined(cpu_list)
1281 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001282#endif
1283}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001284
1285CpuInfoList *qmp_query_cpus(Error **errp)
1286{
1287 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001288 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001289
Andreas Färberbdc44642013-06-24 23:50:24 +02001290 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001291 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001292#if defined(TARGET_I386)
1293 X86CPU *x86_cpu = X86_CPU(cpu);
1294 CPUX86State *env = &x86_cpu->env;
1295#elif defined(TARGET_PPC)
1296 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1297 CPUPPCState *env = &ppc_cpu->env;
1298#elif defined(TARGET_SPARC)
1299 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1300 CPUSPARCState *env = &sparc_cpu->env;
1301#elif defined(TARGET_MIPS)
1302 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1303 CPUMIPSState *env = &mips_cpu->env;
1304#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001305
Andreas Färbercb446ec2013-05-01 14:24:52 +02001306 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001307
1308 info = g_malloc0(sizeof(*info));
1309 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001310 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001311 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001312 info->value->halted = cpu->halted;
Andreas Färber9f09e182012-05-03 06:59:07 +02001313 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001314#if defined(TARGET_I386)
1315 info->value->has_pc = true;
1316 info->value->pc = env->eip + env->segs[R_CS].base;
1317#elif defined(TARGET_PPC)
1318 info->value->has_nip = true;
1319 info->value->nip = env->nip;
1320#elif defined(TARGET_SPARC)
1321 info->value->has_pc = true;
1322 info->value->pc = env->pc;
1323 info->value->has_npc = true;
1324 info->value->npc = env->npc;
1325#elif defined(TARGET_MIPS)
1326 info->value->has_PC = true;
1327 info->value->PC = env->active_tc.PC;
1328#endif
1329
1330 /* XXX: waiting for the qapi to support GSList */
1331 if (!cur_item) {
1332 head = cur_item = info;
1333 } else {
1334 cur_item->next = info;
1335 cur_item = info;
1336 }
1337 }
1338
1339 return head;
1340}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001341
1342void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1343 bool has_cpu, int64_t cpu_index, Error **errp)
1344{
1345 FILE *f;
1346 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001347 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001348 uint8_t buf[1024];
1349
1350 if (!has_cpu) {
1351 cpu_index = 0;
1352 }
1353
Andreas Färber151d1322013-02-15 15:41:49 +01001354 cpu = qemu_get_cpu(cpu_index);
1355 if (cpu == NULL) {
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001356 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1357 "a CPU number");
1358 return;
1359 }
1360
1361 f = fopen(filename, "wb");
1362 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001363 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001364 return;
1365 }
1366
1367 while (size != 0) {
1368 l = sizeof(buf);
1369 if (l > size)
1370 l = size;
Andreas Färberf17ec442013-06-29 19:40:58 +02001371 cpu_memory_rw_debug(cpu, addr, buf, l, 0);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001372 if (fwrite(buf, 1, l, f) != l) {
1373 error_set(errp, QERR_IO_ERROR);
1374 goto exit;
1375 }
1376 addr += l;
1377 size -= l;
1378 }
1379
1380exit:
1381 fclose(f);
1382}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001383
1384void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1385 Error **errp)
1386{
1387 FILE *f;
1388 uint32_t l;
1389 uint8_t buf[1024];
1390
1391 f = fopen(filename, "wb");
1392 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001393 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001394 return;
1395 }
1396
1397 while (size != 0) {
1398 l = sizeof(buf);
1399 if (l > size)
1400 l = size;
1401 cpu_physical_memory_rw(addr, buf, l, 0);
1402 if (fwrite(buf, 1, l, f) != l) {
1403 error_set(errp, QERR_IO_ERROR);
1404 goto exit;
1405 }
1406 addr += l;
1407 size -= l;
1408 }
1409
1410exit:
1411 fclose(f);
1412}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001413
1414void qmp_inject_nmi(Error **errp)
1415{
1416#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001417 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001418
Andreas Färberbdc44642013-06-24 23:50:24 +02001419 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001420 X86CPU *cpu = X86_CPU(cs);
1421 CPUX86State *env = &cpu->env;
1422
Jan Kiszka02c09192011-10-18 00:00:06 +08001423 if (!env->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001424 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001425 } else {
1426 apic_deliver_nmi(env->apic_state);
1427 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001428 }
Eugene (jno) Dvurechenski7f7f9752012-12-05 15:50:07 +01001429#elif defined(TARGET_S390X)
1430 CPUState *cs;
1431 S390CPU *cpu;
1432
Andreas Färberbdc44642013-06-24 23:50:24 +02001433 CPU_FOREACH(cs) {
Eugene (jno) Dvurechenski7f7f9752012-12-05 15:50:07 +01001434 cpu = S390_CPU(cs);
1435 if (cpu->env.cpu_num == monitor_get_cpu_index()) {
1436 if (s390_cpu_restart(S390_CPU(cs)) == -1) {
1437 error_set(errp, QERR_UNSUPPORTED);
1438 return;
1439 }
1440 break;
1441 }
1442 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001443#else
1444 error_set(errp, QERR_UNSUPPORTED);
1445#endif
1446}