]> Git Repo - qemu.git/blame - cpus.c
icount: add sleep parameter to the icount option to set icount_sleep mode
[qemu.git] / cpus.c
CommitLineData
296af7c9
BS
1/*
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
83c9089e 28#include "monitor/monitor.h"
a4e15de9 29#include "qapi/qmp/qerror.h"
9c17d615 30#include "sysemu/sysemu.h"
022c62cb 31#include "exec/gdbstub.h"
9c17d615
PB
32#include "sysemu/dma.h"
33#include "sysemu/kvm.h"
de0b36b6 34#include "qmp-commands.h"
296af7c9 35
1de7afc9 36#include "qemu/thread.h"
9c17d615
PB
37#include "sysemu/cpus.h"
38#include "sysemu/qtest.h"
1de7afc9
PB
39#include "qemu/main-loop.h"
40#include "qemu/bitmap.h"
cb365646 41#include "qemu/seqlock.h"
a4e15de9 42#include "qapi-event.h"
9cb805fd 43#include "hw/nmi.h"
0ff0fc19
JK
44
45#ifndef _WIN32
1de7afc9 46#include "qemu/compatfd.h"
0ff0fc19 47#endif
296af7c9 48
6d9cb73c
JK
49#ifdef CONFIG_LINUX
50
51#include <sys/prctl.h>
52
c0532a76
MT
53#ifndef PR_MCE_KILL
54#define PR_MCE_KILL 33
55#endif
56
6d9cb73c
JK
57#ifndef PR_MCE_KILL_SET
58#define PR_MCE_KILL_SET 1
59#endif
60
61#ifndef PR_MCE_KILL_EARLY
62#define PR_MCE_KILL_EARLY 1
63#endif
64
65#endif /* CONFIG_LINUX */
66
182735ef 67static CPUState *next_cpu;
27498bef
ST
68int64_t max_delay;
69int64_t max_advance;
296af7c9 70
321bc0b2
TC
71bool cpu_is_stopped(CPUState *cpu)
72{
73 return cpu->stopped || !runstate_is_running();
74}
75
a98ae1d8 76static bool cpu_thread_is_idle(CPUState *cpu)
ac873f1e 77{
c64ca814 78 if (cpu->stop || cpu->queued_work_first) {
ac873f1e
PM
79 return false;
80 }
321bc0b2 81 if (cpu_is_stopped(cpu)) {
ac873f1e
PM
82 return true;
83 }
8c2e1b00 84 if (!cpu->halted || cpu_has_work(cpu) ||
215e79c0 85 kvm_halt_in_kernel()) {
ac873f1e
PM
86 return false;
87 }
88 return true;
89}
90
91static bool all_cpu_threads_idle(void)
92{
182735ef 93 CPUState *cpu;
ac873f1e 94
bdc44640 95 CPU_FOREACH(cpu) {
182735ef 96 if (!cpu_thread_is_idle(cpu)) {
ac873f1e
PM
97 return false;
98 }
99 }
100 return true;
101}
102
946fb27c
PB
103/***********************************************************/
104/* guest cycle counter */
105
a3270e19
PB
106/* Protected by TimersState seqlock */
107
5045e9d9 108static bool icount_sleep = true;
71468395 109static int64_t vm_clock_warp_start = -1;
946fb27c
PB
110/* Conversion factor from emulated instructions to virtual clock ticks. */
111static int icount_time_shift;
112/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
113#define MAX_ICOUNT_SHIFT 10
a3270e19 114
946fb27c
PB
115static QEMUTimer *icount_rt_timer;
116static QEMUTimer *icount_vm_timer;
117static QEMUTimer *icount_warp_timer;
946fb27c
PB
118
119typedef struct TimersState {
cb365646 120 /* Protected by BQL. */
946fb27c
PB
121 int64_t cpu_ticks_prev;
122 int64_t cpu_ticks_offset;
cb365646
LPF
123
124 /* cpu_clock_offset can be read out of BQL, so protect it with
125 * this lock.
126 */
127 QemuSeqLock vm_clock_seqlock;
946fb27c
PB
128 int64_t cpu_clock_offset;
129 int32_t cpu_ticks_enabled;
130 int64_t dummy;
c96778bb
FK
131
132 /* Compensate for varying guest execution speed. */
133 int64_t qemu_icount_bias;
134 /* Only written by TCG thread */
135 int64_t qemu_icount;
946fb27c
PB
136} TimersState;
137
d9cd4007 138static TimersState timers_state;
946fb27c 139
2a62914b 140int64_t cpu_get_icount_raw(void)
946fb27c
PB
141{
142 int64_t icount;
4917cf44 143 CPUState *cpu = current_cpu;
946fb27c 144
c96778bb 145 icount = timers_state.qemu_icount;
4917cf44 146 if (cpu) {
99df7dce 147 if (!cpu_can_do_io(cpu)) {
2a62914b
PD
148 fprintf(stderr, "Bad icount read\n");
149 exit(1);
946fb27c 150 }
28ecfd7a 151 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
946fb27c 152 }
2a62914b
PD
153 return icount;
154}
155
156/* Return the virtual CPU time, based on the instruction counter. */
157static int64_t cpu_get_icount_locked(void)
158{
159 int64_t icount = cpu_get_icount_raw();
3f031313 160 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
946fb27c
PB
161}
162
17a15f1b
PB
163int64_t cpu_get_icount(void)
164{
165 int64_t icount;
166 unsigned start;
167
168 do {
169 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
170 icount = cpu_get_icount_locked();
171 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
172
173 return icount;
174}
175
3f031313
FK
176int64_t cpu_icount_to_ns(int64_t icount)
177{
178 return icount << icount_time_shift;
179}
180
946fb27c 181/* return the host CPU cycle counter and handle stop/restart */
cb365646 182/* Caller must hold the BQL */
946fb27c
PB
183int64_t cpu_get_ticks(void)
184{
5f3e3101
PB
185 int64_t ticks;
186
946fb27c
PB
187 if (use_icount) {
188 return cpu_get_icount();
189 }
5f3e3101
PB
190
191 ticks = timers_state.cpu_ticks_offset;
192 if (timers_state.cpu_ticks_enabled) {
193 ticks += cpu_get_real_ticks();
194 }
195
196 if (timers_state.cpu_ticks_prev > ticks) {
197 /* Note: non increasing ticks may happen if the host uses
198 software suspend */
199 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
200 ticks = timers_state.cpu_ticks_prev;
946fb27c 201 }
5f3e3101
PB
202
203 timers_state.cpu_ticks_prev = ticks;
204 return ticks;
946fb27c
PB
205}
206
cb365646 207static int64_t cpu_get_clock_locked(void)
946fb27c 208{
5f3e3101 209 int64_t ticks;
cb365646 210
5f3e3101
PB
211 ticks = timers_state.cpu_clock_offset;
212 if (timers_state.cpu_ticks_enabled) {
213 ticks += get_clock();
946fb27c 214 }
cb365646 215
5f3e3101 216 return ticks;
cb365646
LPF
217}
218
219/* return the host CPU monotonic timer and handle stop/restart */
220int64_t cpu_get_clock(void)
221{
222 int64_t ti;
223 unsigned start;
224
225 do {
226 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
227 ti = cpu_get_clock_locked();
228 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
229
230 return ti;
946fb27c
PB
231}
232
cb365646
LPF
233/* enable cpu_get_ticks()
234 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
235 */
946fb27c
PB
236void cpu_enable_ticks(void)
237{
cb365646
LPF
238 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
239 seqlock_write_lock(&timers_state.vm_clock_seqlock);
946fb27c
PB
240 if (!timers_state.cpu_ticks_enabled) {
241 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
242 timers_state.cpu_clock_offset -= get_clock();
243 timers_state.cpu_ticks_enabled = 1;
244 }
cb365646 245 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
946fb27c
PB
246}
247
248/* disable cpu_get_ticks() : the clock is stopped. You must not call
cb365646
LPF
249 * cpu_get_ticks() after that.
250 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
251 */
946fb27c
PB
252void cpu_disable_ticks(void)
253{
cb365646
LPF
254 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
255 seqlock_write_lock(&timers_state.vm_clock_seqlock);
946fb27c 256 if (timers_state.cpu_ticks_enabled) {
5f3e3101 257 timers_state.cpu_ticks_offset += cpu_get_real_ticks();
cb365646 258 timers_state.cpu_clock_offset = cpu_get_clock_locked();
946fb27c
PB
259 timers_state.cpu_ticks_enabled = 0;
260 }
cb365646 261 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
946fb27c
PB
262}
263
264/* Correlation between real and virtual time is always going to be
265 fairly approximate, so ignore small variation.
266 When the guest is idle real and virtual time will be aligned in
267 the IO wait loop. */
268#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
269
270static void icount_adjust(void)
271{
272 int64_t cur_time;
273 int64_t cur_icount;
274 int64_t delta;
a3270e19
PB
275
276 /* Protected by TimersState mutex. */
946fb27c 277 static int64_t last_delta;
468cc7cf 278
946fb27c
PB
279 /* If the VM is not running, then do nothing. */
280 if (!runstate_is_running()) {
281 return;
282 }
468cc7cf 283
17a15f1b
PB
284 seqlock_write_lock(&timers_state.vm_clock_seqlock);
285 cur_time = cpu_get_clock_locked();
286 cur_icount = cpu_get_icount_locked();
468cc7cf 287
946fb27c
PB
288 delta = cur_icount - cur_time;
289 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
290 if (delta > 0
291 && last_delta + ICOUNT_WOBBLE < delta * 2
292 && icount_time_shift > 0) {
293 /* The guest is getting too far ahead. Slow time down. */
294 icount_time_shift--;
295 }
296 if (delta < 0
297 && last_delta - ICOUNT_WOBBLE > delta * 2
298 && icount_time_shift < MAX_ICOUNT_SHIFT) {
299 /* The guest is getting too far behind. Speed time up. */
300 icount_time_shift++;
301 }
302 last_delta = delta;
c96778bb
FK
303 timers_state.qemu_icount_bias = cur_icount
304 - (timers_state.qemu_icount << icount_time_shift);
17a15f1b 305 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
946fb27c
PB
306}
307
308static void icount_adjust_rt(void *opaque)
309{
40daca54 310 timer_mod(icount_rt_timer,
1979b908 311 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
946fb27c
PB
312 icount_adjust();
313}
314
315static void icount_adjust_vm(void *opaque)
316{
40daca54
AB
317 timer_mod(icount_vm_timer,
318 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
319 get_ticks_per_sec() / 10);
946fb27c
PB
320 icount_adjust();
321}
322
323static int64_t qemu_icount_round(int64_t count)
324{
325 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
326}
327
328static void icount_warp_rt(void *opaque)
329{
17a15f1b
PB
330 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
331 * changes from -1 to another value, so the race here is okay.
332 */
333 if (atomic_read(&vm_clock_warp_start) == -1) {
946fb27c
PB
334 return;
335 }
336
17a15f1b 337 seqlock_write_lock(&timers_state.vm_clock_seqlock);
946fb27c 338 if (runstate_is_running()) {
bf2a7ddb 339 int64_t clock = cpu_get_clock_locked();
8ed961d9
PB
340 int64_t warp_delta;
341
342 warp_delta = clock - vm_clock_warp_start;
343 if (use_icount == 2) {
946fb27c 344 /*
40daca54 345 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
946fb27c
PB
346 * far ahead of real time.
347 */
17a15f1b 348 int64_t cur_icount = cpu_get_icount_locked();
bf2a7ddb 349 int64_t delta = clock - cur_icount;
8ed961d9 350 warp_delta = MIN(warp_delta, delta);
946fb27c 351 }
c96778bb 352 timers_state.qemu_icount_bias += warp_delta;
946fb27c
PB
353 }
354 vm_clock_warp_start = -1;
17a15f1b 355 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
8ed961d9
PB
356
357 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
358 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
359 }
946fb27c
PB
360}
361
8156be56
PB
362void qtest_clock_warp(int64_t dest)
363{
40daca54 364 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
efef88b3 365 AioContext *aio_context;
8156be56 366 assert(qtest_enabled());
efef88b3 367 aio_context = qemu_get_aio_context();
8156be56 368 while (clock < dest) {
40daca54 369 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
c9299e2f 370 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
efef88b3 371
17a15f1b 372 seqlock_write_lock(&timers_state.vm_clock_seqlock);
c96778bb 373 timers_state.qemu_icount_bias += warp;
17a15f1b
PB
374 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
375
40daca54 376 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
efef88b3 377 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
40daca54 378 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
8156be56 379 }
40daca54 380 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
8156be56
PB
381}
382
40daca54 383void qemu_clock_warp(QEMUClockType type)
946fb27c 384{
ce78d18c 385 int64_t clock;
946fb27c
PB
386 int64_t deadline;
387
388 /*
389 * There are too many global variables to make the "warp" behavior
390 * applicable to other clocks. But a clock argument removes the
391 * need for if statements all over the place.
392 */
40daca54 393 if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
946fb27c
PB
394 return;
395 }
396
5045e9d9
VC
397 if (icount_sleep) {
398 /*
399 * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
400 * This ensures that the deadline for the timer is computed correctly
401 * below.
402 * This also makes sure that the insn counter is synchronized before
403 * the CPU starts running, in case the CPU is woken by an event other
404 * than the earliest QEMU_CLOCK_VIRTUAL timer.
405 */
406 icount_warp_rt(NULL);
407 timer_del(icount_warp_timer);
408 }
ce78d18c 409 if (!all_cpu_threads_idle()) {
946fb27c
PB
410 return;
411 }
412
8156be56
PB
413 if (qtest_enabled()) {
414 /* When testing, qtest commands advance icount. */
415 return;
416 }
417
ac70aafc 418 /* We want to use the earliest deadline from ALL vm_clocks */
bf2a7ddb 419 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
40daca54 420 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
ce78d18c
PB
421 if (deadline < 0) {
422 return;
ac70aafc
AB
423 }
424
946fb27c
PB
425 if (deadline > 0) {
426 /*
40daca54 427 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
946fb27c
PB
428 * sleep. Otherwise, the CPU might be waiting for a future timer
429 * interrupt to wake it up, but the interrupt never comes because
430 * the vCPU isn't running any insns and thus doesn't advance the
40daca54 431 * QEMU_CLOCK_VIRTUAL.
946fb27c 432 */
5045e9d9
VC
433 if (!icount_sleep) {
434 /*
435 * We never let VCPUs sleep in no sleep icount mode.
436 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
437 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
438 * It is useful when we want a deterministic execution time,
439 * isolated from host latencies.
440 */
441 seqlock_write_lock(&timers_state.vm_clock_seqlock);
442 timers_state.qemu_icount_bias += deadline;
443 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
444 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
445 } else {
446 /*
447 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
448 * "real" time, (related to the time left until the next event) has
449 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
450 * This avoids that the warps are visible externally; for example,
451 * you will not be sending network packets continuously instead of
452 * every 100ms.
453 */
454 seqlock_write_lock(&timers_state.vm_clock_seqlock);
455 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
456 vm_clock_warp_start = clock;
457 }
458 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
459 timer_mod_anticipate(icount_warp_timer, clock + deadline);
ce78d18c 460 }
ac70aafc 461 } else if (deadline == 0) {
40daca54 462 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
946fb27c
PB
463 }
464}
465
d09eae37
FK
466static bool icount_state_needed(void *opaque)
467{
468 return use_icount;
469}
470
471/*
472 * This is a subsection for icount migration.
473 */
474static const VMStateDescription icount_vmstate_timers = {
475 .name = "timer/icount",
476 .version_id = 1,
477 .minimum_version_id = 1,
478 .fields = (VMStateField[]) {
479 VMSTATE_INT64(qemu_icount_bias, TimersState),
480 VMSTATE_INT64(qemu_icount, TimersState),
481 VMSTATE_END_OF_LIST()
482 }
483};
484
946fb27c
PB
485static const VMStateDescription vmstate_timers = {
486 .name = "timer",
487 .version_id = 2,
488 .minimum_version_id = 1,
35d08458 489 .fields = (VMStateField[]) {
946fb27c
PB
490 VMSTATE_INT64(cpu_ticks_offset, TimersState),
491 VMSTATE_INT64(dummy, TimersState),
492 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
493 VMSTATE_END_OF_LIST()
d09eae37
FK
494 },
495 .subsections = (VMStateSubsection[]) {
496 {
497 .vmsd = &icount_vmstate_timers,
498 .needed = icount_state_needed,
499 }, {
500 /* empty */
501 }
946fb27c
PB
502 }
503};
504
4603ea01
PD
505void cpu_ticks_init(void)
506{
507 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
508 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
509}
510
1ad9580b 511void configure_icount(QemuOpts *opts, Error **errp)
946fb27c 512{
1ad9580b 513 const char *option;
a8bfac37 514 char *rem_str = NULL;
1ad9580b 515
1ad9580b 516 option = qemu_opt_get(opts, "shift");
946fb27c 517 if (!option) {
a8bfac37
ST
518 if (qemu_opt_get(opts, "align") != NULL) {
519 error_setg(errp, "Please specify shift option when using align");
520 }
946fb27c
PB
521 return;
522 }
f1f4b57e
VC
523
524 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
5045e9d9
VC
525 if (icount_sleep) {
526 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
527 icount_warp_rt, NULL);
528 }
f1f4b57e 529
a8bfac37 530 icount_align_option = qemu_opt_get_bool(opts, "align", false);
f1f4b57e
VC
531
532 if (icount_align_option && !icount_sleep) {
533 error_setg(errp, "align=on and sleep=no are incompatible");
534 }
946fb27c 535 if (strcmp(option, "auto") != 0) {
a8bfac37
ST
536 errno = 0;
537 icount_time_shift = strtol(option, &rem_str, 0);
538 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
539 error_setg(errp, "icount: Invalid shift value");
540 }
946fb27c
PB
541 use_icount = 1;
542 return;
a8bfac37
ST
543 } else if (icount_align_option) {
544 error_setg(errp, "shift=auto and align=on are incompatible");
f1f4b57e
VC
545 } else if (!icount_sleep) {
546 error_setg(errp, "shift=auto and sleep=no are incompatible");
946fb27c
PB
547 }
548
549 use_icount = 2;
550
551 /* 125MIPS seems a reasonable initial guess at the guest speed.
552 It will be corrected fairly quickly anyway. */
553 icount_time_shift = 3;
554
555 /* Have both realtime and virtual time triggers for speed adjustment.
556 The realtime trigger catches emulated time passing too slowly,
557 the virtual time trigger catches emulated time passing too fast.
558 Realtime triggers occur even when idle, so use them less frequently
559 than VM triggers. */
bf2a7ddb
PD
560 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
561 icount_adjust_rt, NULL);
40daca54 562 timer_mod(icount_rt_timer,
bf2a7ddb 563 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
40daca54
AB
564 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
565 icount_adjust_vm, NULL);
566 timer_mod(icount_vm_timer,
567 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
568 get_ticks_per_sec() / 10);
946fb27c
PB
569}
570
296af7c9
BS
571/***********************************************************/
572void hw_error(const char *fmt, ...)
573{
574 va_list ap;
55e5c285 575 CPUState *cpu;
296af7c9
BS
576
577 va_start(ap, fmt);
578 fprintf(stderr, "qemu: hardware error: ");
579 vfprintf(stderr, fmt, ap);
580 fprintf(stderr, "\n");
bdc44640 581 CPU_FOREACH(cpu) {
55e5c285 582 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
878096ee 583 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
296af7c9
BS
584 }
585 va_end(ap);
586 abort();
587}
588
589void cpu_synchronize_all_states(void)
590{
182735ef 591 CPUState *cpu;
296af7c9 592
bdc44640 593 CPU_FOREACH(cpu) {
182735ef 594 cpu_synchronize_state(cpu);
296af7c9
BS
595 }
596}
597
598void cpu_synchronize_all_post_reset(void)
599{
182735ef 600 CPUState *cpu;
296af7c9 601
bdc44640 602 CPU_FOREACH(cpu) {
182735ef 603 cpu_synchronize_post_reset(cpu);
296af7c9
BS
604 }
605}
606
607void cpu_synchronize_all_post_init(void)
608{
182735ef 609 CPUState *cpu;
296af7c9 610
bdc44640 611 CPU_FOREACH(cpu) {
182735ef 612 cpu_synchronize_post_init(cpu);
296af7c9
BS
613 }
614}
615
de9d61e8
MT
616void cpu_clean_all_dirty(void)
617{
618 CPUState *cpu;
619
620 CPU_FOREACH(cpu) {
621 cpu_clean_state(cpu);
622 }
623}
624
56983463 625static int do_vm_stop(RunState state)
296af7c9 626{
56983463
KW
627 int ret = 0;
628
1354869c 629 if (runstate_is_running()) {
296af7c9 630 cpu_disable_ticks();
296af7c9 631 pause_all_vcpus();
f5bbfba1 632 runstate_set(state);
1dfb4dd9 633 vm_state_notify(0, state);
a4e15de9 634 qapi_event_send_stop(&error_abort);
296af7c9 635 }
56983463 636
594a45ce
KW
637 bdrv_drain_all();
638 ret = bdrv_flush_all();
639
56983463 640 return ret;
296af7c9
BS
641}
642
a1fcaa73 643static bool cpu_can_run(CPUState *cpu)
296af7c9 644{
4fdeee7c 645 if (cpu->stop) {
a1fcaa73 646 return false;
0ab07c62 647 }
321bc0b2 648 if (cpu_is_stopped(cpu)) {
a1fcaa73 649 return false;
0ab07c62 650 }
a1fcaa73 651 return true;
296af7c9
BS
652}
653
91325046 654static void cpu_handle_guest_debug(CPUState *cpu)
83f338f7 655{
64f6b346 656 gdb_set_stop_cpu(cpu);
8cf71710 657 qemu_system_debug_request();
f324e766 658 cpu->stopped = true;
3c638d06
JK
659}
660
714bd040
PB
661static void cpu_signal(int sig)
662{
4917cf44
AF
663 if (current_cpu) {
664 cpu_exit(current_cpu);
714bd040
PB
665 }
666 exit_request = 1;
667}
714bd040 668
6d9cb73c
JK
669#ifdef CONFIG_LINUX
670static void sigbus_reraise(void)
671{
672 sigset_t set;
673 struct sigaction action;
674
675 memset(&action, 0, sizeof(action));
676 action.sa_handler = SIG_DFL;
677 if (!sigaction(SIGBUS, &action, NULL)) {
678 raise(SIGBUS);
679 sigemptyset(&set);
680 sigaddset(&set, SIGBUS);
681 sigprocmask(SIG_UNBLOCK, &set, NULL);
682 }
683 perror("Failed to re-raise SIGBUS!\n");
684 abort();
685}
686
687static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
688 void *ctx)
689{
690 if (kvm_on_sigbus(siginfo->ssi_code,
691 (void *)(intptr_t)siginfo->ssi_addr)) {
692 sigbus_reraise();
693 }
694}
695
696static void qemu_init_sigbus(void)
697{
698 struct sigaction action;
699
700 memset(&action, 0, sizeof(action));
701 action.sa_flags = SA_SIGINFO;
702 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
703 sigaction(SIGBUS, &action, NULL);
704
705 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
706}
707
290adf38 708static void qemu_kvm_eat_signals(CPUState *cpu)
1ab3c6c0
JK
709{
710 struct timespec ts = { 0, 0 };
711 siginfo_t siginfo;
712 sigset_t waitset;
713 sigset_t chkset;
714 int r;
715
716 sigemptyset(&waitset);
717 sigaddset(&waitset, SIG_IPI);
718 sigaddset(&waitset, SIGBUS);
719
720 do {
721 r = sigtimedwait(&waitset, &siginfo, &ts);
722 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
723 perror("sigtimedwait");
724 exit(1);
725 }
726
727 switch (r) {
728 case SIGBUS:
290adf38 729 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
1ab3c6c0
JK
730 sigbus_reraise();
731 }
732 break;
733 default:
734 break;
735 }
736
737 r = sigpending(&chkset);
738 if (r == -1) {
739 perror("sigpending");
740 exit(1);
741 }
742 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
1ab3c6c0
JK
743}
744
6d9cb73c
JK
745#else /* !CONFIG_LINUX */
746
747static void qemu_init_sigbus(void)
748{
749}
1ab3c6c0 750
290adf38 751static void qemu_kvm_eat_signals(CPUState *cpu)
1ab3c6c0
JK
752{
753}
6d9cb73c
JK
754#endif /* !CONFIG_LINUX */
755
296af7c9 756#ifndef _WIN32
55f8d6ac
JK
757static void dummy_signal(int sig)
758{
759}
55f8d6ac 760
13618e05 761static void qemu_kvm_init_cpu_signals(CPUState *cpu)
714bd040
PB
762{
763 int r;
764 sigset_t set;
765 struct sigaction sigact;
766
767 memset(&sigact, 0, sizeof(sigact));
768 sigact.sa_handler = dummy_signal;
769 sigaction(SIG_IPI, &sigact, NULL);
770
714bd040
PB
771 pthread_sigmask(SIG_BLOCK, NULL, &set);
772 sigdelset(&set, SIG_IPI);
714bd040 773 sigdelset(&set, SIGBUS);
491d6e80 774 r = kvm_set_signal_mask(cpu, &set);
714bd040
PB
775 if (r) {
776 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
777 exit(1);
778 }
779}
780
781static void qemu_tcg_init_cpu_signals(void)
782{
714bd040
PB
783 sigset_t set;
784 struct sigaction sigact;
785
786 memset(&sigact, 0, sizeof(sigact));
787 sigact.sa_handler = cpu_signal;
788 sigaction(SIG_IPI, &sigact, NULL);
789
790 sigemptyset(&set);
791 sigaddset(&set, SIG_IPI);
792 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
714bd040
PB
793}
794
55f8d6ac 795#else /* _WIN32 */
13618e05 796static void qemu_kvm_init_cpu_signals(CPUState *cpu)
ff48eb5f 797{
714bd040
PB
798 abort();
799}
ff48eb5f 800
714bd040
PB
801static void qemu_tcg_init_cpu_signals(void)
802{
ff48eb5f 803}
714bd040 804#endif /* _WIN32 */
ff48eb5f 805
b2532d88 806static QemuMutex qemu_global_mutex;
46daff13 807static QemuCond qemu_io_proceeded_cond;
6b49809c 808static unsigned iothread_requesting_mutex;
296af7c9
BS
809
810static QemuThread io_thread;
811
812static QemuThread *tcg_cpu_thread;
813static QemuCond *tcg_halt_cond;
814
296af7c9
BS
815/* cpu creation */
816static QemuCond qemu_cpu_cond;
817/* system init */
296af7c9 818static QemuCond qemu_pause_cond;
e82bcec2 819static QemuCond qemu_work_cond;
296af7c9 820
d3b12f5d 821void qemu_init_cpu_loop(void)
296af7c9 822{
6d9cb73c 823 qemu_init_sigbus();
ed94592b 824 qemu_cond_init(&qemu_cpu_cond);
ed94592b
AL
825 qemu_cond_init(&qemu_pause_cond);
826 qemu_cond_init(&qemu_work_cond);
46daff13 827 qemu_cond_init(&qemu_io_proceeded_cond);
296af7c9 828 qemu_mutex_init(&qemu_global_mutex);
296af7c9 829
b7680cb6 830 qemu_thread_get_self(&io_thread);
296af7c9
BS
831}
832
f100f0b3 833void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
e82bcec2
MT
834{
835 struct qemu_work_item wi;
836
60e82579 837 if (qemu_cpu_is_self(cpu)) {
e82bcec2
MT
838 func(data);
839 return;
840 }
841
842 wi.func = func;
843 wi.data = data;
3c02270d 844 wi.free = false;
c64ca814
AF
845 if (cpu->queued_work_first == NULL) {
846 cpu->queued_work_first = &wi;
0ab07c62 847 } else {
c64ca814 848 cpu->queued_work_last->next = &wi;
0ab07c62 849 }
c64ca814 850 cpu->queued_work_last = &wi;
e82bcec2
MT
851 wi.next = NULL;
852 wi.done = false;
853
c08d7424 854 qemu_cpu_kick(cpu);
e82bcec2 855 while (!wi.done) {
4917cf44 856 CPUState *self_cpu = current_cpu;
e82bcec2
MT
857
858 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
4917cf44 859 current_cpu = self_cpu;
e82bcec2
MT
860 }
861}
862
3c02270d
CV
863void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
864{
865 struct qemu_work_item *wi;
866
867 if (qemu_cpu_is_self(cpu)) {
868 func(data);
869 return;
870 }
871
872 wi = g_malloc0(sizeof(struct qemu_work_item));
873 wi->func = func;
874 wi->data = data;
875 wi->free = true;
876 if (cpu->queued_work_first == NULL) {
877 cpu->queued_work_first = wi;
878 } else {
879 cpu->queued_work_last->next = wi;
880 }
881 cpu->queued_work_last = wi;
882 wi->next = NULL;
883 wi->done = false;
884
885 qemu_cpu_kick(cpu);
886}
887
6d45b109 888static void flush_queued_work(CPUState *cpu)
e82bcec2
MT
889{
890 struct qemu_work_item *wi;
891
c64ca814 892 if (cpu->queued_work_first == NULL) {
e82bcec2 893 return;
0ab07c62 894 }
e82bcec2 895
c64ca814
AF
896 while ((wi = cpu->queued_work_first)) {
897 cpu->queued_work_first = wi->next;
e82bcec2
MT
898 wi->func(wi->data);
899 wi->done = true;
3c02270d
CV
900 if (wi->free) {
901 g_free(wi);
902 }
e82bcec2 903 }
c64ca814 904 cpu->queued_work_last = NULL;
e82bcec2
MT
905 qemu_cond_broadcast(&qemu_work_cond);
906}
907
509a0d78 908static void qemu_wait_io_event_common(CPUState *cpu)
296af7c9 909{
4fdeee7c
AF
910 if (cpu->stop) {
911 cpu->stop = false;
f324e766 912 cpu->stopped = true;
296af7c9
BS
913 qemu_cond_signal(&qemu_pause_cond);
914 }
6d45b109 915 flush_queued_work(cpu);
216fc9a4 916 cpu->thread_kicked = false;
296af7c9
BS
917}
918
6cabe1f3 919static void qemu_tcg_wait_io_event(void)
296af7c9 920{
182735ef 921 CPUState *cpu;
6cabe1f3 922
16400322 923 while (all_cpu_threads_idle()) {
ab33fcda
PB
924 /* Start accounting real time to the virtual clock if the CPUs
925 are idle. */
40daca54 926 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
9705fbb5 927 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
16400322 928 }
296af7c9 929
46daff13
PB
930 while (iothread_requesting_mutex) {
931 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
932 }
6cabe1f3 933
bdc44640 934 CPU_FOREACH(cpu) {
182735ef 935 qemu_wait_io_event_common(cpu);
6cabe1f3 936 }
296af7c9
BS
937}
938
fd529e8f 939static void qemu_kvm_wait_io_event(CPUState *cpu)
296af7c9 940{
a98ae1d8 941 while (cpu_thread_is_idle(cpu)) {
f5c121b8 942 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
16400322 943 }
296af7c9 944
290adf38 945 qemu_kvm_eat_signals(cpu);
509a0d78 946 qemu_wait_io_event_common(cpu);
296af7c9
BS
947}
948
7e97cd88 949static void *qemu_kvm_cpu_thread_fn(void *arg)
296af7c9 950{
48a106bd 951 CPUState *cpu = arg;
84b4915d 952 int r;
296af7c9 953
6164e6d6 954 qemu_mutex_lock(&qemu_global_mutex);
814e612e 955 qemu_thread_get_self(cpu->thread);
9f09e18a 956 cpu->thread_id = qemu_get_thread_id();
626cf8f4 957 cpu->can_do_io = 1;
4917cf44 958 current_cpu = cpu;
296af7c9 959
504134d2 960 r = kvm_init_vcpu(cpu);
84b4915d
JK
961 if (r < 0) {
962 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
963 exit(1);
964 }
296af7c9 965
13618e05 966 qemu_kvm_init_cpu_signals(cpu);
296af7c9
BS
967
968 /* signal CPU creation */
61a46217 969 cpu->created = true;
296af7c9
BS
970 qemu_cond_signal(&qemu_cpu_cond);
971
296af7c9 972 while (1) {
a1fcaa73 973 if (cpu_can_run(cpu)) {
1458c363 974 r = kvm_cpu_exec(cpu);
83f338f7 975 if (r == EXCP_DEBUG) {
91325046 976 cpu_handle_guest_debug(cpu);
83f338f7 977 }
0ab07c62 978 }
fd529e8f 979 qemu_kvm_wait_io_event(cpu);
296af7c9
BS
980 }
981
982 return NULL;
983}
984
c7f0f3b1
AL
985static void *qemu_dummy_cpu_thread_fn(void *arg)
986{
987#ifdef _WIN32
988 fprintf(stderr, "qtest is not supported under Windows\n");
989 exit(1);
990#else
10a9021d 991 CPUState *cpu = arg;
c7f0f3b1
AL
992 sigset_t waitset;
993 int r;
994
995 qemu_mutex_lock_iothread();
814e612e 996 qemu_thread_get_self(cpu->thread);
9f09e18a 997 cpu->thread_id = qemu_get_thread_id();
626cf8f4 998 cpu->can_do_io = 1;
c7f0f3b1
AL
999
1000 sigemptyset(&waitset);
1001 sigaddset(&waitset, SIG_IPI);
1002
1003 /* signal CPU creation */
61a46217 1004 cpu->created = true;
c7f0f3b1
AL
1005 qemu_cond_signal(&qemu_cpu_cond);
1006
4917cf44 1007 current_cpu = cpu;
c7f0f3b1 1008 while (1) {
4917cf44 1009 current_cpu = NULL;
c7f0f3b1
AL
1010 qemu_mutex_unlock_iothread();
1011 do {
1012 int sig;
1013 r = sigwait(&waitset, &sig);
1014 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1015 if (r == -1) {
1016 perror("sigwait");
1017 exit(1);
1018 }
1019 qemu_mutex_lock_iothread();
4917cf44 1020 current_cpu = cpu;
509a0d78 1021 qemu_wait_io_event_common(cpu);
c7f0f3b1
AL
1022 }
1023
1024 return NULL;
1025#endif
1026}
1027
bdb7ca67
JK
1028static void tcg_exec_all(void);
1029
7e97cd88 1030static void *qemu_tcg_cpu_thread_fn(void *arg)
296af7c9 1031{
c3586ba7 1032 CPUState *cpu = arg;
296af7c9 1033
55f8d6ac 1034 qemu_tcg_init_cpu_signals();
814e612e 1035 qemu_thread_get_self(cpu->thread);
296af7c9 1036
296af7c9 1037 qemu_mutex_lock(&qemu_global_mutex);
38fcbd3f
AF
1038 CPU_FOREACH(cpu) {
1039 cpu->thread_id = qemu_get_thread_id();
1040 cpu->created = true;
626cf8f4 1041 cpu->can_do_io = 1;
38fcbd3f 1042 }
296af7c9
BS
1043 qemu_cond_signal(&qemu_cpu_cond);
1044
fa7d1867 1045 /* wait for initial kick-off after machine start */
c28e399c 1046 while (first_cpu->stopped) {
fa7d1867 1047 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
8e564b4e
JK
1048
1049 /* process any pending work */
bdc44640 1050 CPU_FOREACH(cpu) {
182735ef 1051 qemu_wait_io_event_common(cpu);
8e564b4e 1052 }
0ab07c62 1053 }
296af7c9 1054
21618b3e
PB
1055 /* process any pending work */
1056 exit_request = 1;
1057
296af7c9 1058 while (1) {
bdb7ca67 1059 tcg_exec_all();
ac70aafc
AB
1060
1061 if (use_icount) {
40daca54 1062 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
ac70aafc
AB
1063
1064 if (deadline == 0) {
40daca54 1065 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
ac70aafc 1066 }
3b2319a3 1067 }
6cabe1f3 1068 qemu_tcg_wait_io_event();
296af7c9
BS
1069 }
1070
1071 return NULL;
1072}
1073
2ff09a40 1074static void qemu_cpu_kick_thread(CPUState *cpu)
cc015e9a
PB
1075{
1076#ifndef _WIN32
1077 int err;
1078
814e612e 1079 err = pthread_kill(cpu->thread->thread, SIG_IPI);
cc015e9a
PB
1080 if (err) {
1081 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1082 exit(1);
1083 }
1084#else /* _WIN32 */
60e82579 1085 if (!qemu_cpu_is_self(cpu)) {
ed9164a3
OH
1086 CONTEXT tcgContext;
1087
1088 if (SuspendThread(cpu->hThread) == (DWORD)-1) {
7f1721df 1089 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
ed9164a3
OH
1090 GetLastError());
1091 exit(1);
1092 }
1093
1094 /* On multi-core systems, we are not sure that the thread is actually
1095 * suspended until we can get the context.
1096 */
1097 tcgContext.ContextFlags = CONTEXT_CONTROL;
1098 while (GetThreadContext(cpu->hThread, &tcgContext) != 0) {
1099 continue;
1100 }
1101
cc015e9a 1102 cpu_signal(0);
ed9164a3
OH
1103
1104 if (ResumeThread(cpu->hThread) == (DWORD)-1) {
7f1721df 1105 fprintf(stderr, "qemu:%s: GetLastError:%lu\n", __func__,
ed9164a3
OH
1106 GetLastError());
1107 exit(1);
1108 }
cc015e9a
PB
1109 }
1110#endif
1111}
1112
c08d7424 1113void qemu_cpu_kick(CPUState *cpu)
296af7c9 1114{
f5c121b8 1115 qemu_cond_broadcast(cpu->halt_cond);
216fc9a4 1116 if (!tcg_enabled() && !cpu->thread_kicked) {
2ff09a40 1117 qemu_cpu_kick_thread(cpu);
216fc9a4 1118 cpu->thread_kicked = true;
aa2c364b 1119 }
296af7c9
BS
1120}
1121
46d62fac 1122void qemu_cpu_kick_self(void)
296af7c9 1123{
b55c22c6 1124#ifndef _WIN32
4917cf44 1125 assert(current_cpu);
296af7c9 1126
4917cf44
AF
1127 if (!current_cpu->thread_kicked) {
1128 qemu_cpu_kick_thread(current_cpu);
1129 current_cpu->thread_kicked = true;
296af7c9 1130 }
b55c22c6
PB
1131#else
1132 abort();
1133#endif
296af7c9
BS
1134}
1135
60e82579 1136bool qemu_cpu_is_self(CPUState *cpu)
296af7c9 1137{
814e612e 1138 return qemu_thread_is_self(cpu->thread);
296af7c9
BS
1139}
1140
79e2b9ae 1141bool qemu_in_vcpu_thread(void)
aa723c23 1142{
4917cf44 1143 return current_cpu && qemu_cpu_is_self(current_cpu);
aa723c23
JQ
1144}
1145
296af7c9
BS
1146void qemu_mutex_lock_iothread(void)
1147{
21618b3e 1148 atomic_inc(&iothread_requesting_mutex);
bdd459a0 1149 if (!tcg_enabled() || !first_cpu || !first_cpu->thread) {
296af7c9 1150 qemu_mutex_lock(&qemu_global_mutex);
21618b3e 1151 atomic_dec(&iothread_requesting_mutex);
1a28cac3 1152 } else {
1a28cac3 1153 if (qemu_mutex_trylock(&qemu_global_mutex)) {
182735ef 1154 qemu_cpu_kick_thread(first_cpu);
1a28cac3
MT
1155 qemu_mutex_lock(&qemu_global_mutex);
1156 }
6b49809c 1157 atomic_dec(&iothread_requesting_mutex);
46daff13 1158 qemu_cond_broadcast(&qemu_io_proceeded_cond);
1a28cac3 1159 }
296af7c9
BS
1160}
1161
1162void qemu_mutex_unlock_iothread(void)
1163{
1164 qemu_mutex_unlock(&qemu_global_mutex);
1165}
1166
1167static int all_vcpus_paused(void)
1168{
bdc44640 1169 CPUState *cpu;
296af7c9 1170
bdc44640 1171 CPU_FOREACH(cpu) {
182735ef 1172 if (!cpu->stopped) {
296af7c9 1173 return 0;
0ab07c62 1174 }
296af7c9
BS
1175 }
1176
1177 return 1;
1178}
1179
1180void pause_all_vcpus(void)
1181{
bdc44640 1182 CPUState *cpu;
296af7c9 1183
40daca54 1184 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
bdc44640 1185 CPU_FOREACH(cpu) {
182735ef
AF
1186 cpu->stop = true;
1187 qemu_cpu_kick(cpu);
296af7c9
BS
1188 }
1189
aa723c23 1190 if (qemu_in_vcpu_thread()) {
d798e974
JK
1191 cpu_stop_current();
1192 if (!kvm_enabled()) {
bdc44640 1193 CPU_FOREACH(cpu) {
182735ef
AF
1194 cpu->stop = false;
1195 cpu->stopped = true;
d798e974
JK
1196 }
1197 return;
1198 }
1199 }
1200
296af7c9 1201 while (!all_vcpus_paused()) {
be7d6c57 1202 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
bdc44640 1203 CPU_FOREACH(cpu) {
182735ef 1204 qemu_cpu_kick(cpu);
296af7c9
BS
1205 }
1206 }
1207}
1208
2993683b
IM
1209void cpu_resume(CPUState *cpu)
1210{
1211 cpu->stop = false;
1212 cpu->stopped = false;
1213 qemu_cpu_kick(cpu);
1214}
1215
296af7c9
BS
1216void resume_all_vcpus(void)
1217{
bdc44640 1218 CPUState *cpu;
296af7c9 1219
40daca54 1220 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
bdc44640 1221 CPU_FOREACH(cpu) {
182735ef 1222 cpu_resume(cpu);
296af7c9
BS
1223 }
1224}
1225
4900116e
DDAG
1226/* For temporary buffers for forming a name */
1227#define VCPU_THREAD_NAME_SIZE 16
1228
e5ab30a2 1229static void qemu_tcg_init_vcpu(CPUState *cpu)
296af7c9 1230{
4900116e
DDAG
1231 char thread_name[VCPU_THREAD_NAME_SIZE];
1232
09daed84
EI
1233 tcg_cpu_address_space_init(cpu, cpu->as);
1234
296af7c9
BS
1235 /* share a single thread for all cpus with TCG */
1236 if (!tcg_cpu_thread) {
814e612e 1237 cpu->thread = g_malloc0(sizeof(QemuThread));
f5c121b8
AF
1238 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1239 qemu_cond_init(cpu->halt_cond);
1240 tcg_halt_cond = cpu->halt_cond;
4900116e
DDAG
1241 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1242 cpu->cpu_index);
1243 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1244 cpu, QEMU_THREAD_JOINABLE);
1ecf47bf 1245#ifdef _WIN32
814e612e 1246 cpu->hThread = qemu_thread_get_handle(cpu->thread);
1ecf47bf 1247#endif
61a46217 1248 while (!cpu->created) {
18a85728 1249 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
0ab07c62 1250 }
814e612e 1251 tcg_cpu_thread = cpu->thread;
296af7c9 1252 } else {
814e612e 1253 cpu->thread = tcg_cpu_thread;
f5c121b8 1254 cpu->halt_cond = tcg_halt_cond;
296af7c9
BS
1255 }
1256}
1257
48a106bd 1258static void qemu_kvm_start_vcpu(CPUState *cpu)
296af7c9 1259{
4900116e
DDAG
1260 char thread_name[VCPU_THREAD_NAME_SIZE];
1261
814e612e 1262 cpu->thread = g_malloc0(sizeof(QemuThread));
f5c121b8
AF
1263 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1264 qemu_cond_init(cpu->halt_cond);
4900116e
DDAG
1265 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1266 cpu->cpu_index);
1267 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1268 cpu, QEMU_THREAD_JOINABLE);
61a46217 1269 while (!cpu->created) {
18a85728 1270 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
0ab07c62 1271 }
296af7c9
BS
1272}
1273
10a9021d 1274static void qemu_dummy_start_vcpu(CPUState *cpu)
c7f0f3b1 1275{
4900116e
DDAG
1276 char thread_name[VCPU_THREAD_NAME_SIZE];
1277
814e612e 1278 cpu->thread = g_malloc0(sizeof(QemuThread));
f5c121b8
AF
1279 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1280 qemu_cond_init(cpu->halt_cond);
4900116e
DDAG
1281 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1282 cpu->cpu_index);
1283 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
c7f0f3b1 1284 QEMU_THREAD_JOINABLE);
61a46217 1285 while (!cpu->created) {
c7f0f3b1
AL
1286 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1287 }
1288}
1289
c643bed9 1290void qemu_init_vcpu(CPUState *cpu)
296af7c9 1291{
ce3960eb
AF
1292 cpu->nr_cores = smp_cores;
1293 cpu->nr_threads = smp_threads;
f324e766 1294 cpu->stopped = true;
0ab07c62 1295 if (kvm_enabled()) {
48a106bd 1296 qemu_kvm_start_vcpu(cpu);
c7f0f3b1 1297 } else if (tcg_enabled()) {
e5ab30a2 1298 qemu_tcg_init_vcpu(cpu);
c7f0f3b1 1299 } else {
10a9021d 1300 qemu_dummy_start_vcpu(cpu);
0ab07c62 1301 }
296af7c9
BS
1302}
1303
b4a3d965 1304void cpu_stop_current(void)
296af7c9 1305{
4917cf44
AF
1306 if (current_cpu) {
1307 current_cpu->stop = false;
1308 current_cpu->stopped = true;
1309 cpu_exit(current_cpu);
67bb172f 1310 qemu_cond_signal(&qemu_pause_cond);
b4a3d965 1311 }
296af7c9
BS
1312}
1313
56983463 1314int vm_stop(RunState state)
296af7c9 1315{
aa723c23 1316 if (qemu_in_vcpu_thread()) {
74892d24 1317 qemu_system_vmstop_request_prepare();
1dfb4dd9 1318 qemu_system_vmstop_request(state);
296af7c9
BS
1319 /*
1320 * FIXME: should not return to device code in case
1321 * vm_stop() has been requested.
1322 */
b4a3d965 1323 cpu_stop_current();
56983463 1324 return 0;
296af7c9 1325 }
56983463
KW
1326
1327 return do_vm_stop(state);
296af7c9
BS
1328}
1329
8a9236f1
LC
1330/* does a state transition even if the VM is already stopped,
1331 current state is forgotten forever */
56983463 1332int vm_stop_force_state(RunState state)
8a9236f1
LC
1333{
1334 if (runstate_is_running()) {
56983463 1335 return vm_stop(state);
8a9236f1
LC
1336 } else {
1337 runstate_set(state);
594a45ce
KW
1338 /* Make sure to return an error if the flush in a previous vm_stop()
1339 * failed. */
1340 return bdrv_flush_all();
8a9236f1
LC
1341 }
1342}
1343
9349b4f9 1344static int tcg_cpu_exec(CPUArchState *env)
296af7c9 1345{
efee7340 1346 CPUState *cpu = ENV_GET_CPU(env);
296af7c9
BS
1347 int ret;
1348#ifdef CONFIG_PROFILER
1349 int64_t ti;
1350#endif
1351
1352#ifdef CONFIG_PROFILER
1353 ti = profile_getclock();
1354#endif
1355 if (use_icount) {
1356 int64_t count;
ac70aafc 1357 int64_t deadline;
296af7c9 1358 int decr;
c96778bb
FK
1359 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1360 + cpu->icount_extra);
28ecfd7a 1361 cpu->icount_decr.u16.low = 0;
efee7340 1362 cpu->icount_extra = 0;
40daca54 1363 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
ac70aafc
AB
1364
1365 /* Maintain prior (possibly buggy) behaviour where if no deadline
40daca54 1366 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
ac70aafc
AB
1367 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1368 * nanoseconds.
1369 */
1370 if ((deadline < 0) || (deadline > INT32_MAX)) {
1371 deadline = INT32_MAX;
1372 }
1373
1374 count = qemu_icount_round(deadline);
c96778bb 1375 timers_state.qemu_icount += count;
296af7c9
BS
1376 decr = (count > 0xffff) ? 0xffff : count;
1377 count -= decr;
28ecfd7a 1378 cpu->icount_decr.u16.low = decr;
efee7340 1379 cpu->icount_extra = count;
296af7c9
BS
1380 }
1381 ret = cpu_exec(env);
1382#ifdef CONFIG_PROFILER
89d5cbdd 1383 tcg_time += profile_getclock() - ti;
296af7c9
BS
1384#endif
1385 if (use_icount) {
1386 /* Fold pending instructions back into the
1387 instruction counter, and clear the interrupt flag. */
c96778bb
FK
1388 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1389 + cpu->icount_extra);
28ecfd7a 1390 cpu->icount_decr.u32 = 0;
efee7340 1391 cpu->icount_extra = 0;
296af7c9
BS
1392 }
1393 return ret;
1394}
1395
bdb7ca67 1396static void tcg_exec_all(void)
296af7c9 1397{
9a36085b
JK
1398 int r;
1399
40daca54
AB
1400 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1401 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
ab33fcda 1402
0ab07c62 1403 if (next_cpu == NULL) {
296af7c9 1404 next_cpu = first_cpu;
0ab07c62 1405 }
bdc44640 1406 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
182735ef
AF
1407 CPUState *cpu = next_cpu;
1408 CPUArchState *env = cpu->env_ptr;
296af7c9 1409
40daca54 1410 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
ed2803da 1411 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
296af7c9 1412
a1fcaa73 1413 if (cpu_can_run(cpu)) {
bdb7ca67 1414 r = tcg_cpu_exec(env);
9a36085b 1415 if (r == EXCP_DEBUG) {
91325046 1416 cpu_handle_guest_debug(cpu);
3c638d06
JK
1417 break;
1418 }
f324e766 1419 } else if (cpu->stop || cpu->stopped) {
296af7c9
BS
1420 break;
1421 }
1422 }
c629a4bc 1423 exit_request = 0;
296af7c9
BS
1424}
1425
9a78eead 1426void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
262353cb
BS
1427{
1428 /* XXX: implement xxx_cpu_list for targets that still miss it */
e916cbf8
PM
1429#if defined(cpu_list)
1430 cpu_list(f, cpu_fprintf);
262353cb
BS
1431#endif
1432}
de0b36b6
LC
1433
1434CpuInfoList *qmp_query_cpus(Error **errp)
1435{
1436 CpuInfoList *head = NULL, *cur_item = NULL;
182735ef 1437 CPUState *cpu;
de0b36b6 1438
bdc44640 1439 CPU_FOREACH(cpu) {
de0b36b6 1440 CpuInfoList *info;
182735ef
AF
1441#if defined(TARGET_I386)
1442 X86CPU *x86_cpu = X86_CPU(cpu);
1443 CPUX86State *env = &x86_cpu->env;
1444#elif defined(TARGET_PPC)
1445 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1446 CPUPPCState *env = &ppc_cpu->env;
1447#elif defined(TARGET_SPARC)
1448 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1449 CPUSPARCState *env = &sparc_cpu->env;
1450#elif defined(TARGET_MIPS)
1451 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1452 CPUMIPSState *env = &mips_cpu->env;
48e06fe0
BK
1453#elif defined(TARGET_TRICORE)
1454 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1455 CPUTriCoreState *env = &tricore_cpu->env;
182735ef 1456#endif
de0b36b6 1457
cb446eca 1458 cpu_synchronize_state(cpu);
de0b36b6
LC
1459
1460 info = g_malloc0(sizeof(*info));
1461 info->value = g_malloc0(sizeof(*info->value));
55e5c285 1462 info->value->CPU = cpu->cpu_index;
182735ef 1463 info->value->current = (cpu == first_cpu);
259186a7 1464 info->value->halted = cpu->halted;
58f88d4b 1465 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
9f09e18a 1466 info->value->thread_id = cpu->thread_id;
de0b36b6
LC
1467#if defined(TARGET_I386)
1468 info->value->has_pc = true;
1469 info->value->pc = env->eip + env->segs[R_CS].base;
1470#elif defined(TARGET_PPC)
1471 info->value->has_nip = true;
1472 info->value->nip = env->nip;
1473#elif defined(TARGET_SPARC)
1474 info->value->has_pc = true;
1475 info->value->pc = env->pc;
1476 info->value->has_npc = true;
1477 info->value->npc = env->npc;
1478#elif defined(TARGET_MIPS)
1479 info->value->has_PC = true;
1480 info->value->PC = env->active_tc.PC;
48e06fe0
BK
1481#elif defined(TARGET_TRICORE)
1482 info->value->has_PC = true;
1483 info->value->PC = env->PC;
de0b36b6
LC
1484#endif
1485
1486 /* XXX: waiting for the qapi to support GSList */
1487 if (!cur_item) {
1488 head = cur_item = info;
1489 } else {
1490 cur_item->next = info;
1491 cur_item = info;
1492 }
1493 }
1494
1495 return head;
1496}
0cfd6a9a
LC
1497
1498void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1499 bool has_cpu, int64_t cpu_index, Error **errp)
1500{
1501 FILE *f;
1502 uint32_t l;
55e5c285 1503 CPUState *cpu;
0cfd6a9a 1504 uint8_t buf[1024];
0dc9daf0 1505 int64_t orig_addr = addr, orig_size = size;
0cfd6a9a
LC
1506
1507 if (!has_cpu) {
1508 cpu_index = 0;
1509 }
1510
151d1322
AF
1511 cpu = qemu_get_cpu(cpu_index);
1512 if (cpu == NULL) {
0cfd6a9a
LC
1513 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1514 "a CPU number");
1515 return;
1516 }
1517
1518 f = fopen(filename, "wb");
1519 if (!f) {
618da851 1520 error_setg_file_open(errp, errno, filename);
0cfd6a9a
LC
1521 return;
1522 }
1523
1524 while (size != 0) {
1525 l = sizeof(buf);
1526 if (l > size)
1527 l = size;
2f4d0f59 1528 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
0dc9daf0
BP
1529 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1530 " specified", orig_addr, orig_size);
2f4d0f59
AK
1531 goto exit;
1532 }
0cfd6a9a
LC
1533 if (fwrite(buf, 1, l, f) != l) {
1534 error_set(errp, QERR_IO_ERROR);
1535 goto exit;
1536 }
1537 addr += l;
1538 size -= l;
1539 }
1540
1541exit:
1542 fclose(f);
1543}
6d3962bf
LC
1544
1545void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1546 Error **errp)
1547{
1548 FILE *f;
1549 uint32_t l;
1550 uint8_t buf[1024];
1551
1552 f = fopen(filename, "wb");
1553 if (!f) {
618da851 1554 error_setg_file_open(errp, errno, filename);
6d3962bf
LC
1555 return;
1556 }
1557
1558 while (size != 0) {
1559 l = sizeof(buf);
1560 if (l > size)
1561 l = size;
eb6282f2 1562 cpu_physical_memory_read(addr, buf, l);
6d3962bf
LC
1563 if (fwrite(buf, 1, l, f) != l) {
1564 error_set(errp, QERR_IO_ERROR);
1565 goto exit;
1566 }
1567 addr += l;
1568 size -= l;
1569 }
1570
1571exit:
1572 fclose(f);
1573}
ab49ab5c
LC
1574
1575void qmp_inject_nmi(Error **errp)
1576{
1577#if defined(TARGET_I386)
182735ef
AF
1578 CPUState *cs;
1579
bdc44640 1580 CPU_FOREACH(cs) {
182735ef 1581 X86CPU *cpu = X86_CPU(cs);
ab49ab5c 1582
02e51483 1583 if (!cpu->apic_state) {
182735ef 1584 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
02c09195 1585 } else {
02e51483 1586 apic_deliver_nmi(cpu->apic_state);
02c09195 1587 }
ab49ab5c
LC
1588 }
1589#else
9cb805fd 1590 nmi_monitor_handle(monitor_get_cpu_index(), errp);
ab49ab5c
LC
1591#endif
1592}
27498bef
ST
1593
1594void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1595{
1596 if (!use_icount) {
1597 return;
1598 }
1599
1600 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1601 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1602 if (icount_align_option) {
1603 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1604 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1605 } else {
1606 cpu_fprintf(f, "Max guest delay NA\n");
1607 cpu_fprintf(f, "Max guest advance NA\n");
1608 }
1609}
This page took 0.629076 seconds and 4 git commands to generate.