blob: 1a7318f513f2f2c998244e56e297426988b2467a [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"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020029#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010030#include "qemu/error-report.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010032#include "exec/gdbstub.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010033#include "sysemu/dma.h"
34#include "sysemu/kvm.h"
Luiz Capitulinode0b36b2011-09-21 16:38:35 -030035#include "qmp-commands.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000036
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/thread.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/cpus.h"
39#include "sysemu/qtest.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010040#include "qemu/main-loop.h"
41#include "qemu/bitmap.h"
Liu Ping Fancb365642013-09-25 14:20:58 +080042#include "qemu/seqlock.h"
Wenchao Xiaa4e15de2014-06-18 08:43:36 +020043#include "qapi-event.h"
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +100044#include "hw/nmi.h"
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +030045#include "sysemu/replay.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020046
47#ifndef _WIN32
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020049#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000050
Jan Kiszka6d9cb732011-02-01 22:15:58 +010051#ifdef CONFIG_LINUX
52
53#include <sys/prctl.h>
54
Marcelo Tosattic0532a72010-10-11 15:31:21 -030055#ifndef PR_MCE_KILL
56#define PR_MCE_KILL 33
57#endif
58
Jan Kiszka6d9cb732011-02-01 22:15:58 +010059#ifndef PR_MCE_KILL_SET
60#define PR_MCE_KILL_SET 1
61#endif
62
63#ifndef PR_MCE_KILL_EARLY
64#define PR_MCE_KILL_EARLY 1
65#endif
66
67#endif /* CONFIG_LINUX */
68
Andreas Färber182735e2013-05-29 22:29:20 +020069static CPUState *next_cpu;
Sebastian Tanase27498be2014-07-25 11:56:33 +020070int64_t max_delay;
71int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000072
Jason J. Herne2adcc852015-09-08 13:12:33 -040073/* vcpu throttling controls */
74static QEMUTimer *throttle_timer;
75static unsigned int throttle_percentage;
76
77#define CPU_THROTTLE_PCT_MIN 1
78#define CPU_THROTTLE_PCT_MAX 99
79#define CPU_THROTTLE_TIMESLICE_NS 10000000
80
Tiejun Chen321bc0b2013-08-02 09:43:09 +080081bool cpu_is_stopped(CPUState *cpu)
82{
83 return cpu->stopped || !runstate_is_running();
84}
85
Andreas Färbera98ae1d2013-05-26 23:21:08 +020086static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010087{
Andreas Färberc64ca812012-05-03 02:11:45 +020088 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010089 return false;
90 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080091 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010092 return true;
93 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020094 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020095 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010096 return false;
97 }
98 return true;
99}
100
101static bool all_cpu_threads_idle(void)
102{
Andreas Färber182735e2013-05-29 22:29:20 +0200103 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100104
Andreas Färberbdc44642013-06-24 23:50:24 +0200105 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200106 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100107 return false;
108 }
109 }
110 return true;
111}
112
Blue Swirl296af7c2010-03-29 19:23:50 +0000113/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200114/* guest cycle counter */
115
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200116/* Protected by TimersState seqlock */
117
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200118static bool icount_sleep = true;
Sebastian Tanase71468392014-07-23 11:47:50 +0200119static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200120/* Conversion factor from emulated instructions to virtual clock ticks. */
121static int icount_time_shift;
122/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
123#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200124
Paolo Bonzini946fb272011-09-12 13:57:37 +0200125static QEMUTimer *icount_rt_timer;
126static QEMUTimer *icount_vm_timer;
127static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200128
129typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800130 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200131 int64_t cpu_ticks_prev;
132 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800133
134 /* cpu_clock_offset can be read out of BQL, so protect it with
135 * this lock.
136 */
137 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200138 int64_t cpu_clock_offset;
139 int32_t cpu_ticks_enabled;
140 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200141
142 /* Compensate for varying guest execution speed. */
143 int64_t qemu_icount_bias;
144 /* Only written by TCG thread */
145 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200146} TimersState;
147
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000148static TimersState timers_state;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200149
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300150int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200151{
152 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200153 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200154
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200155 icount = timers_state.qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200156 if (cpu) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200157 if (!cpu->can_do_io) {
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300158 fprintf(stderr, "Bad icount read\n");
159 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200160 }
Andreas Färber28ecfd72013-08-26 05:51:49 +0200161 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200162 }
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300163 return icount;
164}
165
166/* Return the virtual CPU time, based on the instruction counter. */
167static int64_t cpu_get_icount_locked(void)
168{
169 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200170 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200171}
172
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200173int64_t cpu_get_icount(void)
174{
175 int64_t icount;
176 unsigned start;
177
178 do {
179 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
180 icount = cpu_get_icount_locked();
181 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
182
183 return icount;
184}
185
KONRAD Frederic3f031312014-08-01 01:37:15 +0200186int64_t cpu_icount_to_ns(int64_t icount)
187{
188 return icount << icount_time_shift;
189}
190
Paolo Bonzini946fb272011-09-12 13:57:37 +0200191/* return the host CPU cycle counter and handle stop/restart */
Liu Ping Fancb365642013-09-25 14:20:58 +0800192/* Caller must hold the BQL */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200193int64_t cpu_get_ticks(void)
194{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100195 int64_t ticks;
196
Paolo Bonzini946fb272011-09-12 13:57:37 +0200197 if (use_icount) {
198 return cpu_get_icount();
199 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100200
201 ticks = timers_state.cpu_ticks_offset;
202 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400203 ticks += cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200204 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100205
206 if (timers_state.cpu_ticks_prev > ticks) {
207 /* Note: non increasing ticks may happen if the host uses
208 software suspend */
209 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
210 ticks = timers_state.cpu_ticks_prev;
211 }
212
213 timers_state.cpu_ticks_prev = ticks;
214 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200215}
216
Liu Ping Fancb365642013-09-25 14:20:58 +0800217static int64_t cpu_get_clock_locked(void)
218{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100219 int64_t ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800220
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100221 ticks = timers_state.cpu_clock_offset;
222 if (timers_state.cpu_ticks_enabled) {
223 ticks += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800224 }
225
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100226 return ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800227}
228
Paolo Bonzini946fb272011-09-12 13:57:37 +0200229/* return the host CPU monotonic timer and handle stop/restart */
230int64_t cpu_get_clock(void)
231{
232 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800233 unsigned start;
234
235 do {
236 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
237 ti = cpu_get_clock_locked();
238 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
239
240 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200241}
242
Liu Ping Fancb365642013-09-25 14:20:58 +0800243/* enable cpu_get_ticks()
244 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
245 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200246void cpu_enable_ticks(void)
247{
Liu Ping Fancb365642013-09-25 14:20:58 +0800248 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
249 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200250 if (!timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400251 timers_state.cpu_ticks_offset -= cpu_get_host_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200252 timers_state.cpu_clock_offset -= get_clock();
253 timers_state.cpu_ticks_enabled = 1;
254 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800255 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200256}
257
258/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800259 * cpu_get_ticks() after that.
260 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
261 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200262void cpu_disable_ticks(void)
263{
Liu Ping Fancb365642013-09-25 14:20:58 +0800264 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
265 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200266 if (timers_state.cpu_ticks_enabled) {
Christopher Covington4a7428c2015-09-25 10:42:21 -0400267 timers_state.cpu_ticks_offset += cpu_get_host_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800268 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200269 timers_state.cpu_ticks_enabled = 0;
270 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800271 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200272}
273
274/* Correlation between real and virtual time is always going to be
275 fairly approximate, so ignore small variation.
276 When the guest is idle real and virtual time will be aligned in
277 the IO wait loop. */
278#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
279
280static void icount_adjust(void)
281{
282 int64_t cur_time;
283 int64_t cur_icount;
284 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200285
286 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200287 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200288
Paolo Bonzini946fb272011-09-12 13:57:37 +0200289 /* If the VM is not running, then do nothing. */
290 if (!runstate_is_running()) {
291 return;
292 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200293
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200294 seqlock_write_lock(&timers_state.vm_clock_seqlock);
295 cur_time = cpu_get_clock_locked();
296 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200297
Paolo Bonzini946fb272011-09-12 13:57:37 +0200298 delta = cur_icount - cur_time;
299 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
300 if (delta > 0
301 && last_delta + ICOUNT_WOBBLE < delta * 2
302 && icount_time_shift > 0) {
303 /* The guest is getting too far ahead. Slow time down. */
304 icount_time_shift--;
305 }
306 if (delta < 0
307 && last_delta - ICOUNT_WOBBLE > delta * 2
308 && icount_time_shift < MAX_ICOUNT_SHIFT) {
309 /* The guest is getting too far behind. Speed time up. */
310 icount_time_shift++;
311 }
312 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200313 timers_state.qemu_icount_bias = cur_icount
314 - (timers_state.qemu_icount << icount_time_shift);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200315 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200316}
317
318static void icount_adjust_rt(void *opaque)
319{
Alex Bligh40daca52013-08-21 16:03:02 +0100320 timer_mod(icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300321 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200322 icount_adjust();
323}
324
325static void icount_adjust_vm(void *opaque)
326{
Alex Bligh40daca52013-08-21 16:03:02 +0100327 timer_mod(icount_vm_timer,
328 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
329 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200330 icount_adjust();
331}
332
333static int64_t qemu_icount_round(int64_t count)
334{
335 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
336}
337
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300338static void icount_warp_rt(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200339{
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200340 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
341 * changes from -1 to another value, so the race here is okay.
342 */
343 if (atomic_read(&vm_clock_warp_start) == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200344 return;
345 }
346
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200347 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200348 if (runstate_is_running()) {
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300349 int64_t clock = REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT,
350 cpu_get_clock_locked());
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200351 int64_t warp_delta;
352
353 warp_delta = clock - vm_clock_warp_start;
354 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200355 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100356 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200357 * far ahead of real time.
358 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200359 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300360 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200361 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200362 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200363 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200364 }
365 vm_clock_warp_start = -1;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200366 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200367
368 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
369 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
370 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200371}
372
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300373static void icount_dummy_timer(void *opaque)
374{
375 (void)opaque;
376}
377
Paolo Bonzini8156be52012-03-28 15:42:04 +0200378void qtest_clock_warp(int64_t dest)
379{
Alex Bligh40daca52013-08-21 16:03:02 +0100380 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800381 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200382 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800383 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200384 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100385 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400386 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800387
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200388 seqlock_write_lock(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200389 timers_state.qemu_icount_bias += warp;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200390 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
391
Alex Bligh40daca52013-08-21 16:03:02 +0100392 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800393 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100394 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200395 }
Alex Bligh40daca52013-08-21 16:03:02 +0100396 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200397}
398
Alex Bligh40daca52013-08-21 16:03:02 +0100399void qemu_clock_warp(QEMUClockType type)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200400{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200401 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200402 int64_t deadline;
403
404 /*
405 * There are too many global variables to make the "warp" behavior
406 * applicable to other clocks. But a clock argument removes the
407 * need for if statements all over the place.
408 */
Alex Bligh40daca52013-08-21 16:03:02 +0100409 if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200410 return;
411 }
412
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200413 if (icount_sleep) {
414 /*
415 * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
416 * This ensures that the deadline for the timer is computed correctly
417 * below.
418 * This also makes sure that the insn counter is synchronized before
419 * the CPU starts running, in case the CPU is woken by an event other
420 * than the earliest QEMU_CLOCK_VIRTUAL timer.
421 */
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300422 icount_warp_rt();
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200423 timer_del(icount_warp_timer);
424 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200425 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200426 return;
427 }
428
Paolo Bonzini8156be52012-03-28 15:42:04 +0200429 if (qtest_enabled()) {
430 /* When testing, qtest commands advance icount. */
431 return;
432 }
433
Alex Blighac70aaf2013-08-21 16:02:57 +0100434 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300435 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100436 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200437 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200438 static bool notified;
439 if (!icount_sleep && !notified) {
440 error_report("WARNING: icount sleep disabled and no active timers");
441 notified = true;
442 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200443 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100444 }
445
Paolo Bonzini946fb272011-09-12 13:57:37 +0200446 if (deadline > 0) {
447 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100448 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200449 * sleep. Otherwise, the CPU might be waiting for a future timer
450 * interrupt to wake it up, but the interrupt never comes because
451 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100452 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200453 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200454 if (!icount_sleep) {
455 /*
456 * We never let VCPUs sleep in no sleep icount mode.
457 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
458 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
459 * It is useful when we want a deterministic execution time,
460 * isolated from host latencies.
461 */
462 seqlock_write_lock(&timers_state.vm_clock_seqlock);
463 timers_state.qemu_icount_bias += deadline;
464 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
465 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
466 } else {
467 /*
468 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
469 * "real" time, (related to the time left until the next event) has
470 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
471 * This avoids that the warps are visible externally; for example,
472 * you will not be sending network packets continuously instead of
473 * every 100ms.
474 */
475 seqlock_write_lock(&timers_state.vm_clock_seqlock);
476 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
477 vm_clock_warp_start = clock;
478 }
479 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
480 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200481 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100482 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100483 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200484 }
485}
486
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200487static bool icount_state_needed(void *opaque)
488{
489 return use_icount;
490}
491
492/*
493 * This is a subsection for icount migration.
494 */
495static const VMStateDescription icount_vmstate_timers = {
496 .name = "timer/icount",
497 .version_id = 1,
498 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200499 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200500 .fields = (VMStateField[]) {
501 VMSTATE_INT64(qemu_icount_bias, TimersState),
502 VMSTATE_INT64(qemu_icount, TimersState),
503 VMSTATE_END_OF_LIST()
504 }
505};
506
Paolo Bonzini946fb272011-09-12 13:57:37 +0200507static const VMStateDescription vmstate_timers = {
508 .name = "timer",
509 .version_id = 2,
510 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200511 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200512 VMSTATE_INT64(cpu_ticks_offset, TimersState),
513 VMSTATE_INT64(dummy, TimersState),
514 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
515 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200516 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200517 .subsections = (const VMStateDescription*[]) {
518 &icount_vmstate_timers,
519 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200520 }
521};
522
Jason J. Herne2adcc852015-09-08 13:12:33 -0400523static void cpu_throttle_thread(void *opaque)
524{
525 CPUState *cpu = opaque;
526 double pct;
527 double throttle_ratio;
528 long sleeptime_ns;
529
530 if (!cpu_throttle_get_percentage()) {
531 return;
532 }
533
534 pct = (double)cpu_throttle_get_percentage()/100;
535 throttle_ratio = pct / (1 - pct);
536 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
537
538 qemu_mutex_unlock_iothread();
539 atomic_set(&cpu->throttle_thread_scheduled, 0);
540 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
541 qemu_mutex_lock_iothread();
542}
543
544static void cpu_throttle_timer_tick(void *opaque)
545{
546 CPUState *cpu;
547 double pct;
548
549 /* Stop the timer if needed */
550 if (!cpu_throttle_get_percentage()) {
551 return;
552 }
553 CPU_FOREACH(cpu) {
554 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
555 async_run_on_cpu(cpu, cpu_throttle_thread, cpu);
556 }
557 }
558
559 pct = (double)cpu_throttle_get_percentage()/100;
560 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
561 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
562}
563
564void cpu_throttle_set(int new_throttle_pct)
565{
566 /* Ensure throttle percentage is within valid range */
567 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
568 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
569
570 atomic_set(&throttle_percentage, new_throttle_pct);
571
572 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
573 CPU_THROTTLE_TIMESLICE_NS);
574}
575
576void cpu_throttle_stop(void)
577{
578 atomic_set(&throttle_percentage, 0);
579}
580
581bool cpu_throttle_active(void)
582{
583 return (cpu_throttle_get_percentage() != 0);
584}
585
586int cpu_throttle_get_percentage(void)
587{
588 return atomic_read(&throttle_percentage);
589}
590
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400591void cpu_ticks_init(void)
592{
593 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
594 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400595 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
596 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400597}
598
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200599void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200600{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200601 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200602 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200603
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200604 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200605 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200606 if (qemu_opt_get(opts, "align") != NULL) {
607 error_setg(errp, "Please specify shift option when using align");
608 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200609 return;
610 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200611
612 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200613 if (icount_sleep) {
614 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
Pavel Dovgalyukefab87c2015-09-17 19:24:39 +0300615 icount_dummy_timer, NULL);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200616 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200617
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200618 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200619
620 if (icount_align_option && !icount_sleep) {
621 error_setg(errp, "align=on and sleep=no are incompatible");
622 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200623 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200624 errno = 0;
625 icount_time_shift = strtol(option, &rem_str, 0);
626 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
627 error_setg(errp, "icount: Invalid shift value");
628 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200629 use_icount = 1;
630 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200631 } else if (icount_align_option) {
632 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200633 } else if (!icount_sleep) {
634 error_setg(errp, "shift=auto and sleep=no are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200635 }
636
637 use_icount = 2;
638
639 /* 125MIPS seems a reasonable initial guess at the guest speed.
640 It will be corrected fairly quickly anyway. */
641 icount_time_shift = 3;
642
643 /* Have both realtime and virtual time triggers for speed adjustment.
644 The realtime trigger catches emulated time passing too slowly,
645 the virtual time trigger catches emulated time passing too fast.
646 Realtime triggers occur even when idle, so use them less frequently
647 than VM triggers. */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300648 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
649 icount_adjust_rt, NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100650 timer_mod(icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300651 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Alex Bligh40daca52013-08-21 16:03:02 +0100652 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
653 icount_adjust_vm, NULL);
654 timer_mod(icount_vm_timer,
655 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
656 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200657}
658
659/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000660void hw_error(const char *fmt, ...)
661{
662 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100663 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000664
665 va_start(ap, fmt);
666 fprintf(stderr, "qemu: hardware error: ");
667 vfprintf(stderr, fmt, ap);
668 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200669 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100670 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200671 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000672 }
673 va_end(ap);
674 abort();
675}
676
677void cpu_synchronize_all_states(void)
678{
Andreas Färber182735e2013-05-29 22:29:20 +0200679 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000680
Andreas Färberbdc44642013-06-24 23:50:24 +0200681 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200682 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000683 }
684}
685
686void cpu_synchronize_all_post_reset(void)
687{
Andreas Färber182735e2013-05-29 22:29:20 +0200688 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000689
Andreas Färberbdc44642013-06-24 23:50:24 +0200690 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200691 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000692 }
693}
694
695void cpu_synchronize_all_post_init(void)
696{
Andreas Färber182735e2013-05-29 22:29:20 +0200697 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000698
Andreas Färberbdc44642013-06-24 23:50:24 +0200699 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200700 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000701 }
702}
703
Marcelo Tosattide9d61e2014-09-05 10:52:46 -0300704void cpu_clean_all_dirty(void)
705{
706 CPUState *cpu;
707
708 CPU_FOREACH(cpu) {
709 cpu_clean_state(cpu);
710 }
711}
712
Kevin Wolf56983462013-07-05 13:49:54 +0200713static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000714{
Kevin Wolf56983462013-07-05 13:49:54 +0200715 int ret = 0;
716
Luiz Capitulino13548692011-07-29 15:36:43 -0300717 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000718 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000719 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300720 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300721 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200722 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000723 }
Kevin Wolf56983462013-07-05 13:49:54 +0200724
Kevin Wolf594a45c2013-07-18 14:52:19 +0200725 bdrv_drain_all();
726 ret = bdrv_flush_all();
727
Kevin Wolf56983462013-07-05 13:49:54 +0200728 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000729}
730
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200731static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000732{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200733 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200734 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100735 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800736 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200737 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100738 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200739 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000740}
741
Andreas Färber91325042013-05-27 02:07:49 +0200742static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200743{
Andreas Färber64f6b342013-05-27 02:06:09 +0200744 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100745 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200746 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200747}
748
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100749#ifdef CONFIG_LINUX
750static void sigbus_reraise(void)
751{
752 sigset_t set;
753 struct sigaction action;
754
755 memset(&action, 0, sizeof(action));
756 action.sa_handler = SIG_DFL;
757 if (!sigaction(SIGBUS, &action, NULL)) {
758 raise(SIGBUS);
759 sigemptyset(&set);
760 sigaddset(&set, SIGBUS);
761 sigprocmask(SIG_UNBLOCK, &set, NULL);
762 }
763 perror("Failed to re-raise SIGBUS!\n");
764 abort();
765}
766
767static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
768 void *ctx)
769{
770 if (kvm_on_sigbus(siginfo->ssi_code,
771 (void *)(intptr_t)siginfo->ssi_addr)) {
772 sigbus_reraise();
773 }
774}
775
776static void qemu_init_sigbus(void)
777{
778 struct sigaction action;
779
780 memset(&action, 0, sizeof(action));
781 action.sa_flags = SA_SIGINFO;
782 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
783 sigaction(SIGBUS, &action, NULL);
784
785 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
786}
787
Andreas Färber290adf32013-01-17 09:30:27 +0100788static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100789{
790 struct timespec ts = { 0, 0 };
791 siginfo_t siginfo;
792 sigset_t waitset;
793 sigset_t chkset;
794 int r;
795
796 sigemptyset(&waitset);
797 sigaddset(&waitset, SIG_IPI);
798 sigaddset(&waitset, SIGBUS);
799
800 do {
801 r = sigtimedwait(&waitset, &siginfo, &ts);
802 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
803 perror("sigtimedwait");
804 exit(1);
805 }
806
807 switch (r) {
808 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100809 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100810 sigbus_reraise();
811 }
812 break;
813 default:
814 break;
815 }
816
817 r = sigpending(&chkset);
818 if (r == -1) {
819 perror("sigpending");
820 exit(1);
821 }
822 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100823}
824
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100825#else /* !CONFIG_LINUX */
826
827static void qemu_init_sigbus(void)
828{
829}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100830
Andreas Färber290adf32013-01-17 09:30:27 +0100831static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100832{
833}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100834#endif /* !CONFIG_LINUX */
835
Blue Swirl296af7c2010-03-29 19:23:50 +0000836#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100837static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000838{
839}
840
Andreas Färber13618e02013-05-26 23:41:00 +0200841static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100842{
843 int r;
844 sigset_t set;
845 struct sigaction sigact;
846
847 memset(&sigact, 0, sizeof(sigact));
848 sigact.sa_handler = dummy_signal;
849 sigaction(SIG_IPI, &sigact, NULL);
850
Paolo Bonzini714bd042011-03-12 17:44:06 +0100851 pthread_sigmask(SIG_BLOCK, NULL, &set);
852 sigdelset(&set, SIG_IPI);
853 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200854 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100855 if (r) {
856 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
857 exit(1);
858 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100859}
860
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100861#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200862static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100863{
864 abort();
865}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100866#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000867
Stefan Weilb2532d82012-09-27 07:41:42 +0200868static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200869static QemuCond qemu_io_proceeded_cond;
Paolo Bonzini6b498092015-02-27 19:58:23 +0100870static unsigned iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000871
872static QemuThread io_thread;
873
Blue Swirl296af7c2010-03-29 19:23:50 +0000874/* cpu creation */
875static QemuCond qemu_cpu_cond;
876/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000877static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300878static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000879
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200880void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000881{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100882 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100883 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100884 qemu_cond_init(&qemu_pause_cond);
885 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200886 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000887 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000888
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100889 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000890}
891
Andreas Färberf100f0b2012-05-03 14:58:47 +0200892void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300893{
894 struct qemu_work_item wi;
895
Andreas Färber60e82572012-05-02 22:23:49 +0200896 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300897 func(data);
898 return;
899 }
900
901 wi.func = func;
902 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600903 wi.free = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200904
905 qemu_mutex_lock(&cpu->work_mutex);
Andreas Färberc64ca812012-05-03 02:11:45 +0200906 if (cpu->queued_work_first == NULL) {
907 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100908 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200909 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100910 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200911 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300912 wi.next = NULL;
913 wi.done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200914 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300915
Andreas Färberc08d7422012-05-03 04:34:15 +0200916 qemu_cpu_kick(cpu);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200917 while (!atomic_mb_read(&wi.done)) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200918 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300919
920 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200921 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300922 }
923}
924
Chegu Vinod3c022702013-06-24 03:49:41 -0600925void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
926{
927 struct qemu_work_item *wi;
928
929 if (qemu_cpu_is_self(cpu)) {
930 func(data);
931 return;
932 }
933
934 wi = g_malloc0(sizeof(struct qemu_work_item));
935 wi->func = func;
936 wi->data = data;
937 wi->free = true;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200938
939 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600940 if (cpu->queued_work_first == NULL) {
941 cpu->queued_work_first = wi;
942 } else {
943 cpu->queued_work_last->next = wi;
944 }
945 cpu->queued_work_last = wi;
946 wi->next = NULL;
947 wi->done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200948 qemu_mutex_unlock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600949
950 qemu_cpu_kick(cpu);
951}
952
Andreas Färber6d45b102012-05-03 02:13:22 +0200953static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300954{
955 struct qemu_work_item *wi;
956
Andreas Färberc64ca812012-05-03 02:11:45 +0200957 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300958 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100959 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300960
Paolo Bonzini376692b2015-07-10 12:32:32 +0200961 qemu_mutex_lock(&cpu->work_mutex);
962 while (cpu->queued_work_first != NULL) {
963 wi = cpu->queued_work_first;
Andreas Färberc64ca812012-05-03 02:11:45 +0200964 cpu->queued_work_first = wi->next;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200965 if (!cpu->queued_work_first) {
966 cpu->queued_work_last = NULL;
967 }
968 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300969 wi->func(wi->data);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200970 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600971 if (wi->free) {
972 g_free(wi);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200973 } else {
974 atomic_mb_set(&wi->done, true);
Chegu Vinod3c022702013-06-24 03:49:41 -0600975 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300976 }
Paolo Bonzini376692b2015-07-10 12:32:32 +0200977 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300978 qemu_cond_broadcast(&qemu_work_cond);
979}
980
Andreas Färber509a0d72012-05-03 02:18:09 +0200981static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000982{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200983 if (cpu->stop) {
984 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +0200985 cpu->stopped = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000986 qemu_cond_signal(&qemu_pause_cond);
987 }
Andreas Färber6d45b102012-05-03 02:13:22 +0200988 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200989 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000990}
991
KONRAD Fredericd5f8d612015-08-10 17:27:06 +0200992static void qemu_tcg_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000993{
Jan Kiszka16400322011-02-09 16:29:37 +0100994 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200995 /* Start accounting real time to the virtual clock if the CPUs
996 are idle. */
Alex Bligh40daca52013-08-21 16:03:02 +0100997 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
KONRAD Fredericd5f8d612015-08-10 17:27:06 +0200998 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100999 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001000
Paolo Bonzini46daff12011-06-09 13:10:24 +02001001 while (iothread_requesting_mutex) {
1002 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
1003 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001004
Andreas Färberbdc44642013-06-24 23:50:24 +02001005 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001006 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001007 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001008}
1009
Andreas Färberfd529e82013-05-26 23:24:55 +02001010static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001011{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001012 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001013 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001014 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001015
Andreas Färber290adf32013-01-17 09:30:27 +01001016 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +02001017 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001018}
1019
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001020static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001021{
Andreas Färber48a106b2013-05-27 02:20:39 +02001022 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001023 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001024
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001025 rcu_register_thread();
1026
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001027 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001028 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001029 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001030 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001031 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001032
Andreas Färber504134d2012-12-17 06:38:45 +01001033 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001034 if (r < 0) {
1035 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1036 exit(1);
1037 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001038
Andreas Färber13618e02013-05-26 23:41:00 +02001039 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001040
1041 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001042 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001043 qemu_cond_signal(&qemu_cpu_cond);
1044
Blue Swirl296af7c2010-03-29 19:23:50 +00001045 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001046 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001047 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001048 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001049 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001050 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001051 }
Andreas Färberfd529e82013-05-26 23:24:55 +02001052 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001053 }
1054
1055 return NULL;
1056}
1057
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001058static void *qemu_dummy_cpu_thread_fn(void *arg)
1059{
1060#ifdef _WIN32
1061 fprintf(stderr, "qtest is not supported under Windows\n");
1062 exit(1);
1063#else
Andreas Färber10a90212013-05-27 02:24:35 +02001064 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001065 sigset_t waitset;
1066 int r;
1067
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001068 rcu_register_thread();
1069
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001070 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001071 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001072 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001073 cpu->can_do_io = 1;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001074
1075 sigemptyset(&waitset);
1076 sigaddset(&waitset, SIG_IPI);
1077
1078 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001079 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001080 qemu_cond_signal(&qemu_cpu_cond);
1081
Andreas Färber4917cf42013-05-27 05:17:50 +02001082 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001083 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +02001084 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001085 qemu_mutex_unlock_iothread();
1086 do {
1087 int sig;
1088 r = sigwait(&waitset, &sig);
1089 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1090 if (r == -1) {
1091 perror("sigwait");
1092 exit(1);
1093 }
1094 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +02001095 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +02001096 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001097 }
1098
1099 return NULL;
1100#endif
1101}
1102
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001103static void tcg_exec_all(void);
1104
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001105static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001106{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001107 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001108
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001109 rcu_register_thread();
1110
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001111 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001112 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001113
Andreas Färber38fcbd32013-07-07 19:50:23 +02001114 CPU_FOREACH(cpu) {
1115 cpu->thread_id = qemu_get_thread_id();
1116 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001117 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001118 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001119 qemu_cond_signal(&qemu_cpu_cond);
1120
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001121 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001122 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001123 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001124
1125 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001126 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001127 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001128 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001129 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001130
Paolo Bonzini21618b32015-02-27 20:01:03 +01001131 /* process any pending work */
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001132 atomic_mb_set(&exit_request, 1);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001133
Blue Swirl296af7c2010-03-29 19:23:50 +00001134 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001135 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +01001136
1137 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +01001138 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001139
1140 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +01001141 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001142 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +02001143 }
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001144 qemu_tcg_wait_io_event(QTAILQ_FIRST(&cpus));
Blue Swirl296af7c2010-03-29 19:23:50 +00001145 }
1146
1147 return NULL;
1148}
1149
Andreas Färber2ff09a42012-05-03 00:23:30 +02001150static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001151{
1152#ifndef _WIN32
1153 int err;
1154
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001155 if (cpu->thread_kicked) {
1156 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001157 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001158 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001159 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001160 if (err) {
1161 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1162 exit(1);
1163 }
1164#else /* _WIN32 */
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001165 abort();
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001166#endif
1167}
1168
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001169static void qemu_cpu_kick_no_halt(void)
1170{
1171 CPUState *cpu;
1172 /* Ensure whatever caused the exit has reached the CPU threads before
1173 * writing exit_request.
1174 */
1175 atomic_mb_set(&exit_request, 1);
1176 cpu = atomic_mb_read(&tcg_current_cpu);
1177 if (cpu) {
1178 cpu_exit(cpu);
1179 }
1180}
1181
Andreas Färberc08d7422012-05-03 04:34:15 +02001182void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001183{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001184 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001185 if (tcg_enabled()) {
1186 qemu_cpu_kick_no_halt();
1187 } else {
1188 qemu_cpu_kick_thread(cpu);
1189 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001190}
1191
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001192void qemu_cpu_kick_self(void)
1193{
Andreas Färber4917cf42013-05-27 05:17:50 +02001194 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001195 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001196}
1197
Andreas Färber60e82572012-05-02 22:23:49 +02001198bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001199{
Andreas Färber814e6122012-05-02 17:00:37 +02001200 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001201}
1202
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001203bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001204{
Andreas Färber4917cf42013-05-27 05:17:50 +02001205 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001206}
1207
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001208static __thread bool iothread_locked = false;
1209
1210bool qemu_mutex_iothread_locked(void)
1211{
1212 return iothread_locked;
1213}
1214
Blue Swirl296af7c2010-03-29 19:23:50 +00001215void qemu_mutex_lock_iothread(void)
1216{
Paolo Bonzini21618b32015-02-27 20:01:03 +01001217 atomic_inc(&iothread_requesting_mutex);
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001218 /* In the simple case there is no need to bump the VCPU thread out of
1219 * TCG code execution.
1220 */
1221 if (!tcg_enabled() || qemu_in_vcpu_thread() ||
Aníbal Limón46036b22015-09-03 15:48:33 -05001222 !first_cpu || !first_cpu->created) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001223 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001224 atomic_dec(&iothread_requesting_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001225 } else {
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001226 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001227 qemu_cpu_kick_no_halt();
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001228 qemu_mutex_lock(&qemu_global_mutex);
1229 }
Paolo Bonzini6b498092015-02-27 19:58:23 +01001230 atomic_dec(&iothread_requesting_mutex);
Paolo Bonzini46daff12011-06-09 13:10:24 +02001231 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001232 }
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001233 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001234}
1235
1236void qemu_mutex_unlock_iothread(void)
1237{
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001238 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001239 qemu_mutex_unlock(&qemu_global_mutex);
1240}
1241
1242static int all_vcpus_paused(void)
1243{
Andreas Färberbdc44642013-06-24 23:50:24 +02001244 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001245
Andreas Färberbdc44642013-06-24 23:50:24 +02001246 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001247 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001248 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001249 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001250 }
1251
1252 return 1;
1253}
1254
1255void pause_all_vcpus(void)
1256{
Andreas Färberbdc44642013-06-24 23:50:24 +02001257 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001258
Alex Bligh40daca52013-08-21 16:03:02 +01001259 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001260 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001261 cpu->stop = true;
1262 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001263 }
1264
Juan Quintelaaa723c22012-09-18 16:30:11 +02001265 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001266 cpu_stop_current();
1267 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001268 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001269 cpu->stop = false;
1270 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001271 }
1272 return;
1273 }
1274 }
1275
Blue Swirl296af7c2010-03-29 19:23:50 +00001276 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001277 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001278 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001279 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001280 }
1281 }
1282}
1283
Igor Mammedov29936832013-04-23 10:29:37 +02001284void cpu_resume(CPUState *cpu)
1285{
1286 cpu->stop = false;
1287 cpu->stopped = false;
1288 qemu_cpu_kick(cpu);
1289}
1290
Blue Swirl296af7c2010-03-29 19:23:50 +00001291void resume_all_vcpus(void)
1292{
Andreas Färberbdc44642013-06-24 23:50:24 +02001293 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001294
Alex Bligh40daca52013-08-21 16:03:02 +01001295 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001296 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001297 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001298 }
1299}
1300
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001301/* For temporary buffers for forming a name */
1302#define VCPU_THREAD_NAME_SIZE 16
1303
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001304static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001305{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001306 char thread_name[VCPU_THREAD_NAME_SIZE];
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001307 static QemuCond *tcg_halt_cond;
1308 static QemuThread *tcg_cpu_thread;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001309
Edgar E. Iglesias09daed82013-12-17 13:06:51 +10001310 tcg_cpu_address_space_init(cpu, cpu->as);
1311
Blue Swirl296af7c2010-03-29 19:23:50 +00001312 /* share a single thread for all cpus with TCG */
1313 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001314 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001315 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1316 qemu_cond_init(cpu->halt_cond);
1317 tcg_halt_cond = cpu->halt_cond;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001318 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1319 cpu->cpu_index);
1320 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1321 cpu, QEMU_THREAD_JOINABLE);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001322#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001323 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001324#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001325 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001326 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001327 }
Andreas Färber814e6122012-05-02 17:00:37 +02001328 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001329 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001330 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001331 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001332 }
1333}
1334
Andreas Färber48a106b2013-05-27 02:20:39 +02001335static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001336{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001337 char thread_name[VCPU_THREAD_NAME_SIZE];
1338
Andreas Färber814e6122012-05-02 17:00:37 +02001339 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001340 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1341 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001342 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1343 cpu->cpu_index);
1344 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1345 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001346 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001347 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001348 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001349}
1350
Andreas Färber10a90212013-05-27 02:24:35 +02001351static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001352{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001353 char thread_name[VCPU_THREAD_NAME_SIZE];
1354
Andreas Färber814e6122012-05-02 17:00:37 +02001355 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001356 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1357 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001358 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1359 cpu->cpu_index);
1360 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001361 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001362 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001363 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1364 }
1365}
1366
Andreas Färberc643bed2013-05-27 03:23:24 +02001367void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001368{
Andreas Färberce3960e2012-12-17 03:27:07 +01001369 cpu->nr_cores = smp_cores;
1370 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001371 cpu->stopped = true;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001372 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001373 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001374 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001375 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001376 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001377 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001378 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001379}
1380
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001381void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001382{
Andreas Färber4917cf42013-05-27 05:17:50 +02001383 if (current_cpu) {
1384 current_cpu->stop = false;
1385 current_cpu->stopped = true;
1386 cpu_exit(current_cpu);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001387 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001388 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001389}
1390
Kevin Wolf56983462013-07-05 13:49:54 +02001391int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001392{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001393 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001394 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001395 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001396 /*
1397 * FIXME: should not return to device code in case
1398 * vm_stop() has been requested.
1399 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001400 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001401 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001402 }
Kevin Wolf56983462013-07-05 13:49:54 +02001403
1404 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001405}
1406
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001407/* does a state transition even if the VM is already stopped,
1408 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001409int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001410{
1411 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001412 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001413 } else {
1414 runstate_set(state);
Kevin Wolf594a45c2013-07-18 14:52:19 +02001415 /* Make sure to return an error if the flush in a previous vm_stop()
1416 * failed. */
1417 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001418 }
1419}
1420
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001421static int64_t tcg_get_icount_limit(void)
1422{
1423 int64_t deadline;
1424
1425 if (replay_mode != REPLAY_MODE_PLAY) {
1426 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
1427
1428 /* Maintain prior (possibly buggy) behaviour where if no deadline
1429 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
1430 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1431 * nanoseconds.
1432 */
1433 if ((deadline < 0) || (deadline > INT32_MAX)) {
1434 deadline = INT32_MAX;
1435 }
1436
1437 return qemu_icount_round(deadline);
1438 } else {
1439 return replay_get_instructions();
1440 }
1441}
1442
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001443static int tcg_cpu_exec(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001444{
1445 int ret;
1446#ifdef CONFIG_PROFILER
1447 int64_t ti;
1448#endif
1449
1450#ifdef CONFIG_PROFILER
1451 ti = profile_getclock();
1452#endif
1453 if (use_icount) {
1454 int64_t count;
1455 int decr;
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001456 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1457 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001458 cpu->icount_decr.u16.low = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001459 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001460 count = tcg_get_icount_limit();
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001461 timers_state.qemu_icount += count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001462 decr = (count > 0xffff) ? 0xffff : count;
1463 count -= decr;
Andreas Färber28ecfd72013-08-26 05:51:49 +02001464 cpu->icount_decr.u16.low = decr;
Andreas Färberefee7342013-08-26 05:39:29 +02001465 cpu->icount_extra = count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001466 }
Peter Crosthwaiteea3e9842015-06-18 10:24:55 -07001467 ret = cpu_exec(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001468#ifdef CONFIG_PROFILER
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001469 tcg_time += profile_getclock() - ti;
Blue Swirl296af7c2010-03-29 19:23:50 +00001470#endif
1471 if (use_icount) {
1472 /* Fold pending instructions back into the
1473 instruction counter, and clear the interrupt flag. */
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001474 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1475 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001476 cpu->icount_decr.u32 = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001477 cpu->icount_extra = 0;
Pavel Dovgalyuk8b427042015-09-17 19:24:05 +03001478 replay_account_executed_instructions();
Blue Swirl296af7c2010-03-29 19:23:50 +00001479 }
1480 return ret;
1481}
1482
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001483static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001484{
Jan Kiszka9a360852011-02-01 22:15:55 +01001485 int r;
1486
Alex Bligh40daca52013-08-21 16:03:02 +01001487 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1488 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001489
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001490 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001491 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001492 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001493 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001494 CPUState *cpu = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001495
Alex Bligh40daca52013-08-21 16:03:02 +01001496 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001497 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001498
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001499 if (cpu_can_run(cpu)) {
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001500 r = tcg_cpu_exec(cpu);
Jan Kiszka9a360852011-02-01 22:15:55 +01001501 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001502 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001503 break;
1504 }
Andreas Färberf324e762012-05-02 23:26:21 +02001505 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001506 break;
1507 }
1508 }
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001509
1510 /* Pairs with smp_wmb in qemu_cpu_kick. */
1511 atomic_mb_set(&exit_request, 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001512}
1513
Stefan Weil9a78eea2010-10-22 23:03:33 +02001514void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001515{
1516 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001517#if defined(cpu_list)
1518 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001519#endif
1520}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001521
1522CpuInfoList *qmp_query_cpus(Error **errp)
1523{
1524 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001525 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001526
Andreas Färberbdc44642013-06-24 23:50:24 +02001527 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001528 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001529#if defined(TARGET_I386)
1530 X86CPU *x86_cpu = X86_CPU(cpu);
1531 CPUX86State *env = &x86_cpu->env;
1532#elif defined(TARGET_PPC)
1533 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1534 CPUPPCState *env = &ppc_cpu->env;
1535#elif defined(TARGET_SPARC)
1536 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1537 CPUSPARCState *env = &sparc_cpu->env;
1538#elif defined(TARGET_MIPS)
1539 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1540 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001541#elif defined(TARGET_TRICORE)
1542 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1543 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02001544#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001545
Andreas Färbercb446ec2013-05-01 14:24:52 +02001546 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001547
1548 info = g_malloc0(sizeof(*info));
1549 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001550 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001551 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001552 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03001553 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02001554 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001555#if defined(TARGET_I386)
1556 info->value->has_pc = true;
1557 info->value->pc = env->eip + env->segs[R_CS].base;
1558#elif defined(TARGET_PPC)
1559 info->value->has_nip = true;
1560 info->value->nip = env->nip;
1561#elif defined(TARGET_SPARC)
1562 info->value->has_pc = true;
1563 info->value->pc = env->pc;
1564 info->value->has_npc = true;
1565 info->value->npc = env->npc;
1566#elif defined(TARGET_MIPS)
1567 info->value->has_PC = true;
1568 info->value->PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001569#elif defined(TARGET_TRICORE)
1570 info->value->has_PC = true;
1571 info->value->PC = env->PC;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001572#endif
1573
1574 /* XXX: waiting for the qapi to support GSList */
1575 if (!cur_item) {
1576 head = cur_item = info;
1577 } else {
1578 cur_item->next = info;
1579 cur_item = info;
1580 }
1581 }
1582
1583 return head;
1584}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001585
1586void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1587 bool has_cpu, int64_t cpu_index, Error **errp)
1588{
1589 FILE *f;
1590 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001591 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001592 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001593 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001594
1595 if (!has_cpu) {
1596 cpu_index = 0;
1597 }
1598
Andreas Färber151d1322013-02-15 15:41:49 +01001599 cpu = qemu_get_cpu(cpu_index);
1600 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001601 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1602 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001603 return;
1604 }
1605
1606 f = fopen(filename, "wb");
1607 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001608 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001609 return;
1610 }
1611
1612 while (size != 0) {
1613 l = sizeof(buf);
1614 if (l > size)
1615 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301616 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001617 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1618 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301619 goto exit;
1620 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001621 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001622 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001623 goto exit;
1624 }
1625 addr += l;
1626 size -= l;
1627 }
1628
1629exit:
1630 fclose(f);
1631}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001632
1633void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1634 Error **errp)
1635{
1636 FILE *f;
1637 uint32_t l;
1638 uint8_t buf[1024];
1639
1640 f = fopen(filename, "wb");
1641 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001642 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001643 return;
1644 }
1645
1646 while (size != 0) {
1647 l = sizeof(buf);
1648 if (l > size)
1649 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001650 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001651 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001652 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001653 goto exit;
1654 }
1655 addr += l;
1656 size -= l;
1657 }
1658
1659exit:
1660 fclose(f);
1661}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001662
1663void qmp_inject_nmi(Error **errp)
1664{
1665#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001666 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001667
Andreas Färberbdc44642013-06-24 23:50:24 +02001668 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001669 X86CPU *cpu = X86_CPU(cs);
Andreas Färber182735e2013-05-29 22:29:20 +02001670
Chen Fan02e51482013-12-23 17:04:02 +08001671 if (!cpu->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001672 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001673 } else {
Chen Fan02e51482013-12-23 17:04:02 +08001674 apic_deliver_nmi(cpu->apic_state);
Jan Kiszka02c09192011-10-18 00:00:06 +08001675 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001676 }
1677#else
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10001678 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001679#endif
1680}
Sebastian Tanase27498be2014-07-25 11:56:33 +02001681
1682void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1683{
1684 if (!use_icount) {
1685 return;
1686 }
1687
1688 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1689 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1690 if (icount_align_option) {
1691 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1692 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1693 } else {
1694 cpu_fprintf(f, "Max guest delay NA\n");
1695 cpu_fprintf(f, "Max guest advance NA\n");
1696 }
1697}