blob: d44c0eda89c5a9dd49e224d34339fc8398f93a84 [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"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020045
46#ifndef _WIN32
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/compatfd.h"
Jan Kiszka0ff0fc12011-06-23 10:15:55 +020048#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000049
Jan Kiszka6d9cb732011-02-01 22:15:58 +010050#ifdef CONFIG_LINUX
51
52#include <sys/prctl.h>
53
Marcelo Tosattic0532a72010-10-11 15:31:21 -030054#ifndef PR_MCE_KILL
55#define PR_MCE_KILL 33
56#endif
57
Jan Kiszka6d9cb732011-02-01 22:15:58 +010058#ifndef PR_MCE_KILL_SET
59#define PR_MCE_KILL_SET 1
60#endif
61
62#ifndef PR_MCE_KILL_EARLY
63#define PR_MCE_KILL_EARLY 1
64#endif
65
66#endif /* CONFIG_LINUX */
67
Andreas Färber182735e2013-05-29 22:29:20 +020068static CPUState *next_cpu;
Sebastian Tanase27498be2014-07-25 11:56:33 +020069int64_t max_delay;
70int64_t max_advance;
Blue Swirl296af7c2010-03-29 19:23:50 +000071
Jason J. Herne2adcc852015-09-08 13:12:33 -040072/* vcpu throttling controls */
73static QEMUTimer *throttle_timer;
74static unsigned int throttle_percentage;
75
76#define CPU_THROTTLE_PCT_MIN 1
77#define CPU_THROTTLE_PCT_MAX 99
78#define CPU_THROTTLE_TIMESLICE_NS 10000000
79
Tiejun Chen321bc0b2013-08-02 09:43:09 +080080bool cpu_is_stopped(CPUState *cpu)
81{
82 return cpu->stopped || !runstate_is_running();
83}
84
Andreas Färbera98ae1d2013-05-26 23:21:08 +020085static bool cpu_thread_is_idle(CPUState *cpu)
Peter Maydellac873f12012-07-19 16:52:27 +010086{
Andreas Färberc64ca812012-05-03 02:11:45 +020087 if (cpu->stop || cpu->queued_work_first) {
Peter Maydellac873f12012-07-19 16:52:27 +010088 return false;
89 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +080090 if (cpu_is_stopped(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +010091 return true;
92 }
Andreas Färber8c2e1b02013-08-25 18:53:55 +020093 if (!cpu->halted || cpu_has_work(cpu) ||
Alexander Graf215e79c2013-04-24 22:24:12 +020094 kvm_halt_in_kernel()) {
Peter Maydellac873f12012-07-19 16:52:27 +010095 return false;
96 }
97 return true;
98}
99
100static bool all_cpu_threads_idle(void)
101{
Andreas Färber182735e2013-05-29 22:29:20 +0200102 CPUState *cpu;
Peter Maydellac873f12012-07-19 16:52:27 +0100103
Andreas Färberbdc44642013-06-24 23:50:24 +0200104 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200105 if (!cpu_thread_is_idle(cpu)) {
Peter Maydellac873f12012-07-19 16:52:27 +0100106 return false;
107 }
108 }
109 return true;
110}
111
Blue Swirl296af7c2010-03-29 19:23:50 +0000112/***********************************************************/
Paolo Bonzini946fb272011-09-12 13:57:37 +0200113/* guest cycle counter */
114
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200115/* Protected by TimersState seqlock */
116
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200117static bool icount_sleep = true;
Sebastian Tanase71468392014-07-23 11:47:50 +0200118static int64_t vm_clock_warp_start = -1;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200119/* Conversion factor from emulated instructions to virtual clock ticks. */
120static int icount_time_shift;
121/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
122#define MAX_ICOUNT_SHIFT 10
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200123
Paolo Bonzini946fb272011-09-12 13:57:37 +0200124static QEMUTimer *icount_rt_timer;
125static QEMUTimer *icount_vm_timer;
126static QEMUTimer *icount_warp_timer;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200127
128typedef struct TimersState {
Liu Ping Fancb365642013-09-25 14:20:58 +0800129 /* Protected by BQL. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200130 int64_t cpu_ticks_prev;
131 int64_t cpu_ticks_offset;
Liu Ping Fancb365642013-09-25 14:20:58 +0800132
133 /* cpu_clock_offset can be read out of BQL, so protect it with
134 * this lock.
135 */
136 QemuSeqLock vm_clock_seqlock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200137 int64_t cpu_clock_offset;
138 int32_t cpu_ticks_enabled;
139 int64_t dummy;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200140
141 /* Compensate for varying guest execution speed. */
142 int64_t qemu_icount_bias;
143 /* Only written by TCG thread */
144 int64_t qemu_icount;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200145} TimersState;
146
Liu Ping Fand9cd4002013-07-21 08:43:00 +0000147static TimersState timers_state;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200148
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300149int64_t cpu_get_icount_raw(void)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200150{
151 int64_t icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200152 CPUState *cpu = current_cpu;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200153
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200154 icount = timers_state.qemu_icount;
Andreas Färber4917cf42013-05-27 05:17:50 +0200155 if (cpu) {
Paolo Bonzini414b15c2015-06-24 14:16:26 +0200156 if (!cpu->can_do_io) {
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300157 fprintf(stderr, "Bad icount read\n");
158 exit(1);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200159 }
Andreas Färber28ecfd72013-08-26 05:51:49 +0200160 icount -= (cpu->icount_decr.u16.low + cpu->icount_extra);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200161 }
Pavel Dovgalyuk2a629142014-12-08 10:53:45 +0300162 return icount;
163}
164
165/* Return the virtual CPU time, based on the instruction counter. */
166static int64_t cpu_get_icount_locked(void)
167{
168 int64_t icount = cpu_get_icount_raw();
KONRAD Frederic3f031312014-08-01 01:37:15 +0200169 return timers_state.qemu_icount_bias + cpu_icount_to_ns(icount);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200170}
171
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200172int64_t cpu_get_icount(void)
173{
174 int64_t icount;
175 unsigned start;
176
177 do {
178 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
179 icount = cpu_get_icount_locked();
180 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
181
182 return icount;
183}
184
KONRAD Frederic3f031312014-08-01 01:37:15 +0200185int64_t cpu_icount_to_ns(int64_t icount)
186{
187 return icount << icount_time_shift;
188}
189
Paolo Bonzini946fb272011-09-12 13:57:37 +0200190/* return the host CPU cycle counter and handle stop/restart */
Liu Ping Fancb365642013-09-25 14:20:58 +0800191/* Caller must hold the BQL */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200192int64_t cpu_get_ticks(void)
193{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100194 int64_t ticks;
195
Paolo Bonzini946fb272011-09-12 13:57:37 +0200196 if (use_icount) {
197 return cpu_get_icount();
198 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100199
200 ticks = timers_state.cpu_ticks_offset;
201 if (timers_state.cpu_ticks_enabled) {
202 ticks += cpu_get_real_ticks();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200203 }
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100204
205 if (timers_state.cpu_ticks_prev > ticks) {
206 /* Note: non increasing ticks may happen if the host uses
207 software suspend */
208 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
209 ticks = timers_state.cpu_ticks_prev;
210 }
211
212 timers_state.cpu_ticks_prev = ticks;
213 return ticks;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200214}
215
Liu Ping Fancb365642013-09-25 14:20:58 +0800216static int64_t cpu_get_clock_locked(void)
217{
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100218 int64_t ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800219
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100220 ticks = timers_state.cpu_clock_offset;
221 if (timers_state.cpu_ticks_enabled) {
222 ticks += get_clock();
Liu Ping Fancb365642013-09-25 14:20:58 +0800223 }
224
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100225 return ticks;
Liu Ping Fancb365642013-09-25 14:20:58 +0800226}
227
Paolo Bonzini946fb272011-09-12 13:57:37 +0200228/* return the host CPU monotonic timer and handle stop/restart */
229int64_t cpu_get_clock(void)
230{
231 int64_t ti;
Liu Ping Fancb365642013-09-25 14:20:58 +0800232 unsigned start;
233
234 do {
235 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
236 ti = cpu_get_clock_locked();
237 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
238
239 return ti;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200240}
241
Liu Ping Fancb365642013-09-25 14:20:58 +0800242/* enable cpu_get_ticks()
243 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
244 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200245void cpu_enable_ticks(void)
246{
Liu Ping Fancb365642013-09-25 14:20:58 +0800247 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
248 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200249 if (!timers_state.cpu_ticks_enabled) {
250 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
251 timers_state.cpu_clock_offset -= get_clock();
252 timers_state.cpu_ticks_enabled = 1;
253 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800254 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200255}
256
257/* disable cpu_get_ticks() : the clock is stopped. You must not call
Liu Ping Fancb365642013-09-25 14:20:58 +0800258 * cpu_get_ticks() after that.
259 * Caller must hold BQL which server as mutex for vm_clock_seqlock.
260 */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200261void cpu_disable_ticks(void)
262{
Liu Ping Fancb365642013-09-25 14:20:58 +0800263 /* Here, the really thing protected by seqlock is cpu_clock_offset. */
264 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200265 if (timers_state.cpu_ticks_enabled) {
Paolo Bonzini5f3e3102013-10-28 17:32:18 +0100266 timers_state.cpu_ticks_offset += cpu_get_real_ticks();
Liu Ping Fancb365642013-09-25 14:20:58 +0800267 timers_state.cpu_clock_offset = cpu_get_clock_locked();
Paolo Bonzini946fb272011-09-12 13:57:37 +0200268 timers_state.cpu_ticks_enabled = 0;
269 }
Liu Ping Fancb365642013-09-25 14:20:58 +0800270 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200271}
272
273/* Correlation between real and virtual time is always going to be
274 fairly approximate, so ignore small variation.
275 When the guest is idle real and virtual time will be aligned in
276 the IO wait loop. */
277#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
278
279static void icount_adjust(void)
280{
281 int64_t cur_time;
282 int64_t cur_icount;
283 int64_t delta;
Paolo Bonzinia3270e12013-10-07 17:18:15 +0200284
285 /* Protected by TimersState mutex. */
Paolo Bonzini946fb272011-09-12 13:57:37 +0200286 static int64_t last_delta;
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200287
Paolo Bonzini946fb272011-09-12 13:57:37 +0200288 /* If the VM is not running, then do nothing. */
289 if (!runstate_is_running()) {
290 return;
291 }
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200292
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200293 seqlock_write_lock(&timers_state.vm_clock_seqlock);
294 cur_time = cpu_get_clock_locked();
295 cur_icount = cpu_get_icount_locked();
Paolo Bonzini468cc7c2013-10-07 17:21:51 +0200296
Paolo Bonzini946fb272011-09-12 13:57:37 +0200297 delta = cur_icount - cur_time;
298 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
299 if (delta > 0
300 && last_delta + ICOUNT_WOBBLE < delta * 2
301 && icount_time_shift > 0) {
302 /* The guest is getting too far ahead. Slow time down. */
303 icount_time_shift--;
304 }
305 if (delta < 0
306 && last_delta - ICOUNT_WOBBLE > delta * 2
307 && icount_time_shift < MAX_ICOUNT_SHIFT) {
308 /* The guest is getting too far behind. Speed time up. */
309 icount_time_shift++;
310 }
311 last_delta = delta;
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200312 timers_state.qemu_icount_bias = cur_icount
313 - (timers_state.qemu_icount << icount_time_shift);
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200314 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200315}
316
317static void icount_adjust_rt(void *opaque)
318{
Alex Bligh40daca52013-08-21 16:03:02 +0100319 timer_mod(icount_rt_timer,
Pavel Dovgalyuk1979b902015-01-12 15:00:43 +0300320 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200321 icount_adjust();
322}
323
324static void icount_adjust_vm(void *opaque)
325{
Alex Bligh40daca52013-08-21 16:03:02 +0100326 timer_mod(icount_vm_timer,
327 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
328 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200329 icount_adjust();
330}
331
332static int64_t qemu_icount_round(int64_t count)
333{
334 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
335}
336
337static void icount_warp_rt(void *opaque)
338{
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200339 /* The icount_warp_timer is rescheduled soon after vm_clock_warp_start
340 * changes from -1 to another value, so the race here is okay.
341 */
342 if (atomic_read(&vm_clock_warp_start) == -1) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200343 return;
344 }
345
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200346 seqlock_write_lock(&timers_state.vm_clock_seqlock);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200347 if (runstate_is_running()) {
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300348 int64_t clock = cpu_get_clock_locked();
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200349 int64_t warp_delta;
350
351 warp_delta = clock - vm_clock_warp_start;
352 if (use_icount == 2) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200353 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100354 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too
Paolo Bonzini946fb272011-09-12 13:57:37 +0200355 * far ahead of real time.
356 */
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200357 int64_t cur_icount = cpu_get_icount_locked();
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300358 int64_t delta = clock - cur_icount;
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200359 warp_delta = MIN(warp_delta, delta);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200360 }
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200361 timers_state.qemu_icount_bias += warp_delta;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200362 }
363 vm_clock_warp_start = -1;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200364 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
Paolo Bonzini8ed961d2013-10-07 17:26:07 +0200365
366 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
367 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
368 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200369}
370
Paolo Bonzini8156be52012-03-28 15:42:04 +0200371void qtest_clock_warp(int64_t dest)
372{
Alex Bligh40daca52013-08-21 16:03:02 +0100373 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800374 AioContext *aio_context;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200375 assert(qtest_enabled());
Fam Zhengefef88b2015-01-19 17:51:43 +0800376 aio_context = qemu_get_aio_context();
Paolo Bonzini8156be52012-03-28 15:42:04 +0200377 while (clock < dest) {
Alex Bligh40daca52013-08-21 16:03:02 +0100378 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Sergey Fedorovc9299e22014-06-10 13:10:28 +0400379 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
Fam Zhengefef88b2015-01-19 17:51:43 +0800380
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200381 seqlock_write_lock(&timers_state.vm_clock_seqlock);
KONRAD Fredericc96778b2014-08-01 01:37:09 +0200382 timers_state.qemu_icount_bias += warp;
Paolo Bonzini17a15f12013-10-03 15:17:25 +0200383 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
384
Alex Bligh40daca52013-08-21 16:03:02 +0100385 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
Fam Zhengefef88b2015-01-19 17:51:43 +0800386 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
Alex Bligh40daca52013-08-21 16:03:02 +0100387 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200388 }
Alex Bligh40daca52013-08-21 16:03:02 +0100389 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200390}
391
Alex Bligh40daca52013-08-21 16:03:02 +0100392void qemu_clock_warp(QEMUClockType type)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200393{
Paolo Bonzinice78d182013-10-07 17:30:02 +0200394 int64_t clock;
Paolo Bonzini946fb272011-09-12 13:57:37 +0200395 int64_t deadline;
396
397 /*
398 * There are too many global variables to make the "warp" behavior
399 * applicable to other clocks. But a clock argument removes the
400 * need for if statements all over the place.
401 */
Alex Bligh40daca52013-08-21 16:03:02 +0100402 if (type != QEMU_CLOCK_VIRTUAL || !use_icount) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200403 return;
404 }
405
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200406 if (icount_sleep) {
407 /*
408 * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now.
409 * This ensures that the deadline for the timer is computed correctly
410 * below.
411 * This also makes sure that the insn counter is synchronized before
412 * the CPU starts running, in case the CPU is woken by an event other
413 * than the earliest QEMU_CLOCK_VIRTUAL timer.
414 */
415 icount_warp_rt(NULL);
416 timer_del(icount_warp_timer);
417 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200418 if (!all_cpu_threads_idle()) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200419 return;
420 }
421
Paolo Bonzini8156be52012-03-28 15:42:04 +0200422 if (qtest_enabled()) {
423 /* When testing, qtest commands advance icount. */
424 return;
425 }
426
Alex Blighac70aaf2013-08-21 16:02:57 +0100427 /* We want to use the earliest deadline from ALL vm_clocks */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300428 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
Alex Bligh40daca52013-08-21 16:03:02 +0100429 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200430 if (deadline < 0) {
Victor CLEMENTd7a0f712015-05-29 17:14:06 +0200431 static bool notified;
432 if (!icount_sleep && !notified) {
433 error_report("WARNING: icount sleep disabled and no active timers");
434 notified = true;
435 }
Paolo Bonzinice78d182013-10-07 17:30:02 +0200436 return;
Alex Blighac70aaf2013-08-21 16:02:57 +0100437 }
438
Paolo Bonzini946fb272011-09-12 13:57:37 +0200439 if (deadline > 0) {
440 /*
Alex Bligh40daca52013-08-21 16:03:02 +0100441 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
Paolo Bonzini946fb272011-09-12 13:57:37 +0200442 * sleep. Otherwise, the CPU might be waiting for a future timer
443 * interrupt to wake it up, but the interrupt never comes because
444 * the vCPU isn't running any insns and thus doesn't advance the
Alex Bligh40daca52013-08-21 16:03:02 +0100445 * QEMU_CLOCK_VIRTUAL.
Paolo Bonzini946fb272011-09-12 13:57:37 +0200446 */
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200447 if (!icount_sleep) {
448 /*
449 * We never let VCPUs sleep in no sleep icount mode.
450 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
451 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
452 * It is useful when we want a deterministic execution time,
453 * isolated from host latencies.
454 */
455 seqlock_write_lock(&timers_state.vm_clock_seqlock);
456 timers_state.qemu_icount_bias += deadline;
457 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
458 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
459 } else {
460 /*
461 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
462 * "real" time, (related to the time left until the next event) has
463 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
464 * This avoids that the warps are visible externally; for example,
465 * you will not be sending network packets continuously instead of
466 * every 100ms.
467 */
468 seqlock_write_lock(&timers_state.vm_clock_seqlock);
469 if (vm_clock_warp_start == -1 || vm_clock_warp_start > clock) {
470 vm_clock_warp_start = clock;
471 }
472 seqlock_write_unlock(&timers_state.vm_clock_seqlock);
473 timer_mod_anticipate(icount_warp_timer, clock + deadline);
Paolo Bonzinice78d182013-10-07 17:30:02 +0200474 }
Alex Blighac70aaf2013-08-21 16:02:57 +0100475 } else if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +0100476 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200477 }
478}
479
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200480static bool icount_state_needed(void *opaque)
481{
482 return use_icount;
483}
484
485/*
486 * This is a subsection for icount migration.
487 */
488static const VMStateDescription icount_vmstate_timers = {
489 .name = "timer/icount",
490 .version_id = 1,
491 .minimum_version_id = 1,
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200492 .needed = icount_state_needed,
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200493 .fields = (VMStateField[]) {
494 VMSTATE_INT64(qemu_icount_bias, TimersState),
495 VMSTATE_INT64(qemu_icount, TimersState),
496 VMSTATE_END_OF_LIST()
497 }
498};
499
Paolo Bonzini946fb272011-09-12 13:57:37 +0200500static const VMStateDescription vmstate_timers = {
501 .name = "timer",
502 .version_id = 2,
503 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200504 .fields = (VMStateField[]) {
Paolo Bonzini946fb272011-09-12 13:57:37 +0200505 VMSTATE_INT64(cpu_ticks_offset, TimersState),
506 VMSTATE_INT64(dummy, TimersState),
507 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
508 VMSTATE_END_OF_LIST()
KONRAD Fredericd09eae32014-08-01 01:37:10 +0200509 },
Juan Quintela5cd8cad2014-09-23 14:09:54 +0200510 .subsections = (const VMStateDescription*[]) {
511 &icount_vmstate_timers,
512 NULL
Paolo Bonzini946fb272011-09-12 13:57:37 +0200513 }
514};
515
Jason J. Herne2adcc852015-09-08 13:12:33 -0400516static void cpu_throttle_thread(void *opaque)
517{
518 CPUState *cpu = opaque;
519 double pct;
520 double throttle_ratio;
521 long sleeptime_ns;
522
523 if (!cpu_throttle_get_percentage()) {
524 return;
525 }
526
527 pct = (double)cpu_throttle_get_percentage()/100;
528 throttle_ratio = pct / (1 - pct);
529 sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
530
531 qemu_mutex_unlock_iothread();
532 atomic_set(&cpu->throttle_thread_scheduled, 0);
533 g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
534 qemu_mutex_lock_iothread();
535}
536
537static void cpu_throttle_timer_tick(void *opaque)
538{
539 CPUState *cpu;
540 double pct;
541
542 /* Stop the timer if needed */
543 if (!cpu_throttle_get_percentage()) {
544 return;
545 }
546 CPU_FOREACH(cpu) {
547 if (!atomic_xchg(&cpu->throttle_thread_scheduled, 1)) {
548 async_run_on_cpu(cpu, cpu_throttle_thread, cpu);
549 }
550 }
551
552 pct = (double)cpu_throttle_get_percentage()/100;
553 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
554 CPU_THROTTLE_TIMESLICE_NS / (1-pct));
555}
556
557void cpu_throttle_set(int new_throttle_pct)
558{
559 /* Ensure throttle percentage is within valid range */
560 new_throttle_pct = MIN(new_throttle_pct, CPU_THROTTLE_PCT_MAX);
561 new_throttle_pct = MAX(new_throttle_pct, CPU_THROTTLE_PCT_MIN);
562
563 atomic_set(&throttle_percentage, new_throttle_pct);
564
565 timer_mod(throttle_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT) +
566 CPU_THROTTLE_TIMESLICE_NS);
567}
568
569void cpu_throttle_stop(void)
570{
571 atomic_set(&throttle_percentage, 0);
572}
573
574bool cpu_throttle_active(void)
575{
576 return (cpu_throttle_get_percentage() != 0);
577}
578
579int cpu_throttle_get_percentage(void)
580{
581 return atomic_read(&throttle_percentage);
582}
583
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400584void cpu_ticks_init(void)
585{
586 seqlock_init(&timers_state.vm_clock_seqlock, NULL);
587 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Jason J. Herne2adcc852015-09-08 13:12:33 -0400588 throttle_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
589 cpu_throttle_timer_tick, NULL);
Pavel Dovgalyuk4603ea02014-09-01 09:34:49 +0400590}
591
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200592void configure_icount(QemuOpts *opts, Error **errp)
Paolo Bonzini946fb272011-09-12 13:57:37 +0200593{
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200594 const char *option;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200595 char *rem_str = NULL;
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200596
Sebastian Tanase1ad95802014-07-25 11:56:28 +0200597 option = qemu_opt_get(opts, "shift");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200598 if (!option) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200599 if (qemu_opt_get(opts, "align") != NULL) {
600 error_setg(errp, "Please specify shift option when using align");
601 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200602 return;
603 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200604
605 icount_sleep = qemu_opt_get_bool(opts, "sleep", true);
Victor CLEMENT5045e9d92015-05-29 17:14:04 +0200606 if (icount_sleep) {
607 icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
608 icount_warp_rt, NULL);
609 }
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200610
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200611 icount_align_option = qemu_opt_get_bool(opts, "align", false);
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200612
613 if (icount_align_option && !icount_sleep) {
614 error_setg(errp, "align=on and sleep=no are incompatible");
615 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200616 if (strcmp(option, "auto") != 0) {
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200617 errno = 0;
618 icount_time_shift = strtol(option, &rem_str, 0);
619 if (errno != 0 || *rem_str != '\0' || !strlen(option)) {
620 error_setg(errp, "icount: Invalid shift value");
621 }
Paolo Bonzini946fb272011-09-12 13:57:37 +0200622 use_icount = 1;
623 return;
Sebastian Tanasea8bfac32014-07-25 11:56:29 +0200624 } else if (icount_align_option) {
625 error_setg(errp, "shift=auto and align=on are incompatible");
Victor CLEMENTf1f4b572015-05-29 17:14:05 +0200626 } else if (!icount_sleep) {
627 error_setg(errp, "shift=auto and sleep=no are incompatible");
Paolo Bonzini946fb272011-09-12 13:57:37 +0200628 }
629
630 use_icount = 2;
631
632 /* 125MIPS seems a reasonable initial guess at the guest speed.
633 It will be corrected fairly quickly anyway. */
634 icount_time_shift = 3;
635
636 /* Have both realtime and virtual time triggers for speed adjustment.
637 The realtime trigger catches emulated time passing too slowly,
638 the virtual time trigger catches emulated time passing too fast.
639 Realtime triggers occur even when idle, so use them less frequently
640 than VM triggers. */
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300641 icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
642 icount_adjust_rt, NULL);
Alex Bligh40daca52013-08-21 16:03:02 +0100643 timer_mod(icount_rt_timer,
Pavel Dovgalyukbf2a7dd2014-11-26 13:40:55 +0300644 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
Alex Bligh40daca52013-08-21 16:03:02 +0100645 icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
646 icount_adjust_vm, NULL);
647 timer_mod(icount_vm_timer,
648 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
649 get_ticks_per_sec() / 10);
Paolo Bonzini946fb272011-09-12 13:57:37 +0200650}
651
652/***********************************************************/
Blue Swirl296af7c2010-03-29 19:23:50 +0000653void hw_error(const char *fmt, ...)
654{
655 va_list ap;
Andreas Färber55e5c282012-12-17 06:18:02 +0100656 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000657
658 va_start(ap, fmt);
659 fprintf(stderr, "qemu: hardware error: ");
660 vfprintf(stderr, fmt, ap);
661 fprintf(stderr, "\n");
Andreas Färberbdc44642013-06-24 23:50:24 +0200662 CPU_FOREACH(cpu) {
Andreas Färber55e5c282012-12-17 06:18:02 +0100663 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
Andreas Färber878096e2013-05-27 01:33:50 +0200664 cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU);
Blue Swirl296af7c2010-03-29 19:23:50 +0000665 }
666 va_end(ap);
667 abort();
668}
669
670void cpu_synchronize_all_states(void)
671{
Andreas Färber182735e2013-05-29 22:29:20 +0200672 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000673
Andreas Färberbdc44642013-06-24 23:50:24 +0200674 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200675 cpu_synchronize_state(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000676 }
677}
678
679void cpu_synchronize_all_post_reset(void)
680{
Andreas Färber182735e2013-05-29 22:29:20 +0200681 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000682
Andreas Färberbdc44642013-06-24 23:50:24 +0200683 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200684 cpu_synchronize_post_reset(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000685 }
686}
687
688void cpu_synchronize_all_post_init(void)
689{
Andreas Färber182735e2013-05-29 22:29:20 +0200690 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000691
Andreas Färberbdc44642013-06-24 23:50:24 +0200692 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200693 cpu_synchronize_post_init(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +0000694 }
695}
696
Marcelo Tosattide9d61e2014-09-05 10:52:46 -0300697void cpu_clean_all_dirty(void)
698{
699 CPUState *cpu;
700
701 CPU_FOREACH(cpu) {
702 cpu_clean_state(cpu);
703 }
704}
705
Kevin Wolf56983462013-07-05 13:49:54 +0200706static int do_vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +0000707{
Kevin Wolf56983462013-07-05 13:49:54 +0200708 int ret = 0;
709
Luiz Capitulino13548692011-07-29 15:36:43 -0300710 if (runstate_is_running()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000711 cpu_disable_ticks();
Blue Swirl296af7c2010-03-29 19:23:50 +0000712 pause_all_vcpus();
Luiz Capitulinof5bbfba2011-07-29 15:04:45 -0300713 runstate_set(state);
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300714 vm_state_notify(0, state);
Wenchao Xiaa4e15de2014-06-18 08:43:36 +0200715 qapi_event_send_stop(&error_abort);
Blue Swirl296af7c2010-03-29 19:23:50 +0000716 }
Kevin Wolf56983462013-07-05 13:49:54 +0200717
Kevin Wolf594a45c2013-07-18 14:52:19 +0200718 bdrv_drain_all();
719 ret = bdrv_flush_all();
720
Kevin Wolf56983462013-07-05 13:49:54 +0200721 return ret;
Blue Swirl296af7c2010-03-29 19:23:50 +0000722}
723
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200724static bool cpu_can_run(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000725{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200726 if (cpu->stop) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200727 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100728 }
Tiejun Chen321bc0b2013-08-02 09:43:09 +0800729 if (cpu_is_stopped(cpu)) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200730 return false;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100731 }
Andreas Färbera1fcaa72012-05-02 23:42:26 +0200732 return true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000733}
734
Andreas Färber91325042013-05-27 02:07:49 +0200735static void cpu_handle_guest_debug(CPUState *cpu)
Jan Kiszka3c638d02010-06-25 16:56:56 +0200736{
Andreas Färber64f6b342013-05-27 02:06:09 +0200737 gdb_set_stop_cpu(cpu);
Jan Kiszka8cf71712011-02-07 12:19:16 +0100738 qemu_system_debug_request();
Andreas Färberf324e762012-05-02 23:26:21 +0200739 cpu->stopped = true;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200740}
741
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100742#ifdef CONFIG_LINUX
743static void sigbus_reraise(void)
744{
745 sigset_t set;
746 struct sigaction action;
747
748 memset(&action, 0, sizeof(action));
749 action.sa_handler = SIG_DFL;
750 if (!sigaction(SIGBUS, &action, NULL)) {
751 raise(SIGBUS);
752 sigemptyset(&set);
753 sigaddset(&set, SIGBUS);
754 sigprocmask(SIG_UNBLOCK, &set, NULL);
755 }
756 perror("Failed to re-raise SIGBUS!\n");
757 abort();
758}
759
760static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
761 void *ctx)
762{
763 if (kvm_on_sigbus(siginfo->ssi_code,
764 (void *)(intptr_t)siginfo->ssi_addr)) {
765 sigbus_reraise();
766 }
767}
768
769static void qemu_init_sigbus(void)
770{
771 struct sigaction action;
772
773 memset(&action, 0, sizeof(action));
774 action.sa_flags = SA_SIGINFO;
775 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
776 sigaction(SIGBUS, &action, NULL);
777
778 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
779}
780
Andreas Färber290adf32013-01-17 09:30:27 +0100781static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100782{
783 struct timespec ts = { 0, 0 };
784 siginfo_t siginfo;
785 sigset_t waitset;
786 sigset_t chkset;
787 int r;
788
789 sigemptyset(&waitset);
790 sigaddset(&waitset, SIG_IPI);
791 sigaddset(&waitset, SIGBUS);
792
793 do {
794 r = sigtimedwait(&waitset, &siginfo, &ts);
795 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
796 perror("sigtimedwait");
797 exit(1);
798 }
799
800 switch (r) {
801 case SIGBUS:
Andreas Färber290adf32013-01-17 09:30:27 +0100802 if (kvm_on_sigbus_vcpu(cpu, siginfo.si_code, siginfo.si_addr)) {
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100803 sigbus_reraise();
804 }
805 break;
806 default:
807 break;
808 }
809
810 r = sigpending(&chkset);
811 if (r == -1) {
812 perror("sigpending");
813 exit(1);
814 }
815 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100816}
817
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100818#else /* !CONFIG_LINUX */
819
820static void qemu_init_sigbus(void)
821{
822}
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100823
Andreas Färber290adf32013-01-17 09:30:27 +0100824static void qemu_kvm_eat_signals(CPUState *cpu)
Jan Kiszka1ab3c6c2011-03-15 12:26:12 +0100825{
826}
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100827#endif /* !CONFIG_LINUX */
828
Blue Swirl296af7c2010-03-29 19:23:50 +0000829#ifndef _WIN32
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100830static void dummy_signal(int sig)
Blue Swirl296af7c2010-03-29 19:23:50 +0000831{
832}
833
Andreas Färber13618e02013-05-26 23:41:00 +0200834static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100835{
836 int r;
837 sigset_t set;
838 struct sigaction sigact;
839
840 memset(&sigact, 0, sizeof(sigact));
841 sigact.sa_handler = dummy_signal;
842 sigaction(SIG_IPI, &sigact, NULL);
843
Paolo Bonzini714bd042011-03-12 17:44:06 +0100844 pthread_sigmask(SIG_BLOCK, NULL, &set);
845 sigdelset(&set, SIG_IPI);
846 sigdelset(&set, SIGBUS);
Andreas Färber491d6e82013-05-26 23:38:10 +0200847 r = kvm_set_signal_mask(cpu, &set);
Paolo Bonzini714bd042011-03-12 17:44:06 +0100848 if (r) {
849 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
850 exit(1);
851 }
Paolo Bonzini714bd042011-03-12 17:44:06 +0100852}
853
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100854#else /* _WIN32 */
Andreas Färber13618e02013-05-26 23:41:00 +0200855static void qemu_kvm_init_cpu_signals(CPUState *cpu)
Paolo Bonzini714bd042011-03-12 17:44:06 +0100856{
857 abort();
858}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100859#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000860
Stefan Weilb2532d82012-09-27 07:41:42 +0200861static QemuMutex qemu_global_mutex;
Paolo Bonzini46daff12011-06-09 13:10:24 +0200862static QemuCond qemu_io_proceeded_cond;
Paolo Bonzini6b498092015-02-27 19:58:23 +0100863static unsigned iothread_requesting_mutex;
Blue Swirl296af7c2010-03-29 19:23:50 +0000864
865static QemuThread io_thread;
866
Blue Swirl296af7c2010-03-29 19:23:50 +0000867/* cpu creation */
868static QemuCond qemu_cpu_cond;
869/* system init */
Blue Swirl296af7c2010-03-29 19:23:50 +0000870static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300871static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000872
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200873void qemu_init_cpu_loop(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000874{
Jan Kiszka6d9cb732011-02-01 22:15:58 +0100875 qemu_init_sigbus();
Anthony Liguoried945922011-02-08 18:18:18 +0100876 qemu_cond_init(&qemu_cpu_cond);
Anthony Liguoried945922011-02-08 18:18:18 +0100877 qemu_cond_init(&qemu_pause_cond);
878 qemu_cond_init(&qemu_work_cond);
Paolo Bonzini46daff12011-06-09 13:10:24 +0200879 qemu_cond_init(&qemu_io_proceeded_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000880 qemu_mutex_init(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000881
Jan Kiszkab7680cb2011-03-12 17:43:51 +0100882 qemu_thread_get_self(&io_thread);
Blue Swirl296af7c2010-03-29 19:23:50 +0000883}
884
Andreas Färberf100f0b2012-05-03 14:58:47 +0200885void run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300886{
887 struct qemu_work_item wi;
888
Andreas Färber60e82572012-05-02 22:23:49 +0200889 if (qemu_cpu_is_self(cpu)) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300890 func(data);
891 return;
892 }
893
894 wi.func = func;
895 wi.data = data;
Chegu Vinod3c022702013-06-24 03:49:41 -0600896 wi.free = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200897
898 qemu_mutex_lock(&cpu->work_mutex);
Andreas Färberc64ca812012-05-03 02:11:45 +0200899 if (cpu->queued_work_first == NULL) {
900 cpu->queued_work_first = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100901 } else {
Andreas Färberc64ca812012-05-03 02:11:45 +0200902 cpu->queued_work_last->next = &wi;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100903 }
Andreas Färberc64ca812012-05-03 02:11:45 +0200904 cpu->queued_work_last = &wi;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300905 wi.next = NULL;
906 wi.done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200907 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300908
Andreas Färberc08d7422012-05-03 04:34:15 +0200909 qemu_cpu_kick(cpu);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200910 while (!atomic_mb_read(&wi.done)) {
Andreas Färber4917cf42013-05-27 05:17:50 +0200911 CPUState *self_cpu = current_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300912
913 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
Andreas Färber4917cf42013-05-27 05:17:50 +0200914 current_cpu = self_cpu;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300915 }
916}
917
Chegu Vinod3c022702013-06-24 03:49:41 -0600918void async_run_on_cpu(CPUState *cpu, void (*func)(void *data), void *data)
919{
920 struct qemu_work_item *wi;
921
922 if (qemu_cpu_is_self(cpu)) {
923 func(data);
924 return;
925 }
926
927 wi = g_malloc0(sizeof(struct qemu_work_item));
928 wi->func = func;
929 wi->data = data;
930 wi->free = true;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200931
932 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600933 if (cpu->queued_work_first == NULL) {
934 cpu->queued_work_first = wi;
935 } else {
936 cpu->queued_work_last->next = wi;
937 }
938 cpu->queued_work_last = wi;
939 wi->next = NULL;
940 wi->done = false;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200941 qemu_mutex_unlock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600942
943 qemu_cpu_kick(cpu);
944}
945
Andreas Färber6d45b102012-05-03 02:13:22 +0200946static void flush_queued_work(CPUState *cpu)
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300947{
948 struct qemu_work_item *wi;
949
Andreas Färberc64ca812012-05-03 02:11:45 +0200950 if (cpu->queued_work_first == NULL) {
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300951 return;
Jan Kiszka0ab07c62011-02-07 12:19:14 +0100952 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300953
Paolo Bonzini376692b2015-07-10 12:32:32 +0200954 qemu_mutex_lock(&cpu->work_mutex);
955 while (cpu->queued_work_first != NULL) {
956 wi = cpu->queued_work_first;
Andreas Färberc64ca812012-05-03 02:11:45 +0200957 cpu->queued_work_first = wi->next;
Paolo Bonzini376692b2015-07-10 12:32:32 +0200958 if (!cpu->queued_work_first) {
959 cpu->queued_work_last = NULL;
960 }
961 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300962 wi->func(wi->data);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200963 qemu_mutex_lock(&cpu->work_mutex);
Chegu Vinod3c022702013-06-24 03:49:41 -0600964 if (wi->free) {
965 g_free(wi);
Paolo Bonzini376692b2015-07-10 12:32:32 +0200966 } else {
967 atomic_mb_set(&wi->done, true);
Chegu Vinod3c022702013-06-24 03:49:41 -0600968 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300969 }
Paolo Bonzini376692b2015-07-10 12:32:32 +0200970 qemu_mutex_unlock(&cpu->work_mutex);
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300971 qemu_cond_broadcast(&qemu_work_cond);
972}
973
Andreas Färber509a0d72012-05-03 02:18:09 +0200974static void qemu_wait_io_event_common(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000975{
Andreas Färber4fdeee72012-05-02 23:10:09 +0200976 if (cpu->stop) {
977 cpu->stop = false;
Andreas Färberf324e762012-05-02 23:26:21 +0200978 cpu->stopped = true;
Blue Swirl296af7c2010-03-29 19:23:50 +0000979 qemu_cond_signal(&qemu_pause_cond);
980 }
Andreas Färber6d45b102012-05-03 02:13:22 +0200981 flush_queued_work(cpu);
Andreas Färber216fc9a2012-05-02 17:49:49 +0200982 cpu->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000983}
984
KONRAD Fredericd5f8d612015-08-10 17:27:06 +0200985static void qemu_tcg_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +0000986{
Jan Kiszka16400322011-02-09 16:29:37 +0100987 while (all_cpu_threads_idle()) {
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200988 /* Start accounting real time to the virtual clock if the CPUs
989 are idle. */
Alex Bligh40daca52013-08-21 16:03:02 +0100990 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
KONRAD Fredericd5f8d612015-08-10 17:27:06 +0200991 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +0100992 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000993
Paolo Bonzini46daff12011-06-09 13:10:24 +0200994 while (iothread_requesting_mutex) {
995 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
996 }
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200997
Andreas Färberbdc44642013-06-24 23:50:24 +0200998 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200999 qemu_wait_io_event_common(cpu);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +02001000 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001001}
1002
Andreas Färberfd529e82013-05-26 23:24:55 +02001003static void qemu_kvm_wait_io_event(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001004{
Andreas Färbera98ae1d2013-05-26 23:21:08 +02001005 while (cpu_thread_is_idle(cpu)) {
Andreas Färberf5c121b2012-05-03 01:22:49 +02001006 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka16400322011-02-09 16:29:37 +01001007 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001008
Andreas Färber290adf32013-01-17 09:30:27 +01001009 qemu_kvm_eat_signals(cpu);
Andreas Färber509a0d72012-05-03 02:18:09 +02001010 qemu_wait_io_event_common(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001011}
1012
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001013static void *qemu_kvm_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001014{
Andreas Färber48a106b2013-05-27 02:20:39 +02001015 CPUState *cpu = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +01001016 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +00001017
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001018 rcu_register_thread();
1019
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001020 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001021 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001022 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001023 cpu->can_do_io = 1;
Andreas Färber4917cf42013-05-27 05:17:50 +02001024 current_cpu = cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001025
Andreas Färber504134d2012-12-17 06:38:45 +01001026 r = kvm_init_vcpu(cpu);
Jan Kiszka84b49152011-02-01 22:15:50 +01001027 if (r < 0) {
1028 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
1029 exit(1);
1030 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001031
Andreas Färber13618e02013-05-26 23:41:00 +02001032 qemu_kvm_init_cpu_signals(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001033
1034 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001035 cpu->created = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001036 qemu_cond_signal(&qemu_cpu_cond);
1037
Blue Swirl296af7c2010-03-29 19:23:50 +00001038 while (1) {
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001039 if (cpu_can_run(cpu)) {
Andreas Färber1458c362013-05-26 23:46:55 +02001040 r = kvm_cpu_exec(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001041 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001042 cpu_handle_guest_debug(cpu);
Jan Kiszka83f338f2011-02-07 12:19:17 +01001043 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001044 }
Andreas Färberfd529e82013-05-26 23:24:55 +02001045 qemu_kvm_wait_io_event(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001046 }
1047
1048 return NULL;
1049}
1050
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001051static void *qemu_dummy_cpu_thread_fn(void *arg)
1052{
1053#ifdef _WIN32
1054 fprintf(stderr, "qtest is not supported under Windows\n");
1055 exit(1);
1056#else
Andreas Färber10a90212013-05-27 02:24:35 +02001057 CPUState *cpu = arg;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001058 sigset_t waitset;
1059 int r;
1060
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001061 rcu_register_thread();
1062
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001063 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001064 qemu_thread_get_self(cpu->thread);
Andreas Färber9f09e182012-05-03 06:59:07 +02001065 cpu->thread_id = qemu_get_thread_id();
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001066 cpu->can_do_io = 1;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001067
1068 sigemptyset(&waitset);
1069 sigaddset(&waitset, SIG_IPI);
1070
1071 /* signal CPU creation */
Andreas Färber61a46212012-05-02 22:49:36 +02001072 cpu->created = true;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001073 qemu_cond_signal(&qemu_cpu_cond);
1074
Andreas Färber4917cf42013-05-27 05:17:50 +02001075 current_cpu = cpu;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001076 while (1) {
Andreas Färber4917cf42013-05-27 05:17:50 +02001077 current_cpu = NULL;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001078 qemu_mutex_unlock_iothread();
1079 do {
1080 int sig;
1081 r = sigwait(&waitset, &sig);
1082 } while (r == -1 && (errno == EAGAIN || errno == EINTR));
1083 if (r == -1) {
1084 perror("sigwait");
1085 exit(1);
1086 }
1087 qemu_mutex_lock_iothread();
Andreas Färber4917cf42013-05-27 05:17:50 +02001088 current_cpu = cpu;
Andreas Färber509a0d72012-05-03 02:18:09 +02001089 qemu_wait_io_event_common(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001090 }
1091
1092 return NULL;
1093#endif
1094}
1095
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001096static void tcg_exec_all(void);
1097
Jan Kiszka7e97cd82011-02-07 12:19:12 +01001098static void *qemu_tcg_cpu_thread_fn(void *arg)
Blue Swirl296af7c2010-03-29 19:23:50 +00001099{
Andreas Färberc3586ba2012-05-03 01:41:24 +02001100 CPUState *cpu = arg;
Blue Swirl296af7c2010-03-29 19:23:50 +00001101
Paolo Bonziniab28bd22015-07-09 08:55:38 +02001102 rcu_register_thread();
1103
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001104 qemu_mutex_lock_iothread();
Andreas Färber814e6122012-05-02 17:00:37 +02001105 qemu_thread_get_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001106
Andreas Färber38fcbd32013-07-07 19:50:23 +02001107 CPU_FOREACH(cpu) {
1108 cpu->thread_id = qemu_get_thread_id();
1109 cpu->created = true;
Pavel Dovgalyuk626cf8f2014-12-08 10:53:17 +03001110 cpu->can_do_io = 1;
Andreas Färber38fcbd32013-07-07 19:50:23 +02001111 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001112 qemu_cond_signal(&qemu_cpu_cond);
1113
Jan Kiszkafa7d1862011-08-22 18:35:25 +02001114 /* wait for initial kick-off after machine start */
Emilio G. Cotac28e3992015-04-27 12:45:28 -04001115 while (first_cpu->stopped) {
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001116 qemu_cond_wait(first_cpu->halt_cond, &qemu_global_mutex);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001117
1118 /* process any pending work */
Andreas Färberbdc44642013-06-24 23:50:24 +02001119 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001120 qemu_wait_io_event_common(cpu);
Jan Kiszka8e564b42012-02-17 18:31:15 +01001121 }
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001122 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001123
Paolo Bonzini21618b32015-02-27 20:01:03 +01001124 /* process any pending work */
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001125 atomic_mb_set(&exit_request, 1);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001126
Blue Swirl296af7c2010-03-29 19:23:50 +00001127 while (1) {
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001128 tcg_exec_all();
Alex Blighac70aaf2013-08-21 16:02:57 +01001129
1130 if (use_icount) {
Alex Bligh40daca52013-08-21 16:03:02 +01001131 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001132
1133 if (deadline == 0) {
Alex Bligh40daca52013-08-21 16:03:02 +01001134 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001135 }
Paolo Bonzini3b2319a2011-04-13 10:03:43 +02001136 }
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001137 qemu_tcg_wait_io_event(QTAILQ_FIRST(&cpus));
Blue Swirl296af7c2010-03-29 19:23:50 +00001138 }
1139
1140 return NULL;
1141}
1142
Andreas Färber2ff09a42012-05-03 00:23:30 +02001143static void qemu_cpu_kick_thread(CPUState *cpu)
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001144{
1145#ifndef _WIN32
1146 int err;
1147
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001148 if (cpu->thread_kicked) {
1149 return;
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001150 }
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001151 cpu->thread_kicked = true;
Andreas Färber814e6122012-05-02 17:00:37 +02001152 err = pthread_kill(cpu->thread->thread, SIG_IPI);
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001153 if (err) {
1154 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
1155 exit(1);
1156 }
1157#else /* _WIN32 */
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001158 abort();
Paolo Bonzinicc015e92011-03-12 17:44:08 +01001159#endif
1160}
1161
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001162static void qemu_cpu_kick_no_halt(void)
1163{
1164 CPUState *cpu;
1165 /* Ensure whatever caused the exit has reached the CPU threads before
1166 * writing exit_request.
1167 */
1168 atomic_mb_set(&exit_request, 1);
1169 cpu = atomic_mb_read(&tcg_current_cpu);
1170 if (cpu) {
1171 cpu_exit(cpu);
1172 }
1173}
1174
Andreas Färberc08d7422012-05-03 04:34:15 +02001175void qemu_cpu_kick(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001176{
Andreas Färberf5c121b2012-05-03 01:22:49 +02001177 qemu_cond_broadcast(cpu->halt_cond);
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001178 if (tcg_enabled()) {
1179 qemu_cpu_kick_no_halt();
1180 } else {
1181 qemu_cpu_kick_thread(cpu);
1182 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001183}
1184
Jan Kiszka46d62fa2011-02-01 22:15:59 +01001185void qemu_cpu_kick_self(void)
1186{
Andreas Färber4917cf42013-05-27 05:17:50 +02001187 assert(current_cpu);
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001188 qemu_cpu_kick_thread(current_cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001189}
1190
Andreas Färber60e82572012-05-02 22:23:49 +02001191bool qemu_cpu_is_self(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001192{
Andreas Färber814e6122012-05-02 17:00:37 +02001193 return qemu_thread_is_self(cpu->thread);
Blue Swirl296af7c2010-03-29 19:23:50 +00001194}
1195
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001196bool qemu_in_vcpu_thread(void)
Juan Quintelaaa723c22012-09-18 16:30:11 +02001197{
Andreas Färber4917cf42013-05-27 05:17:50 +02001198 return current_cpu && qemu_cpu_is_self(current_cpu);
Juan Quintelaaa723c22012-09-18 16:30:11 +02001199}
1200
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001201static __thread bool iothread_locked = false;
1202
1203bool qemu_mutex_iothread_locked(void)
1204{
1205 return iothread_locked;
1206}
1207
Blue Swirl296af7c2010-03-29 19:23:50 +00001208void qemu_mutex_lock_iothread(void)
1209{
Paolo Bonzini21618b32015-02-27 20:01:03 +01001210 atomic_inc(&iothread_requesting_mutex);
Paolo Bonzini2e7f7a32015-06-18 18:47:18 +02001211 /* In the simple case there is no need to bump the VCPU thread out of
1212 * TCG code execution.
1213 */
1214 if (!tcg_enabled() || qemu_in_vcpu_thread() ||
Aníbal Limón46036b22015-09-03 15:48:33 -05001215 !first_cpu || !first_cpu->created) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001216 qemu_mutex_lock(&qemu_global_mutex);
Paolo Bonzini21618b32015-02-27 20:01:03 +01001217 atomic_dec(&iothread_requesting_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001218 } else {
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001219 if (qemu_mutex_trylock(&qemu_global_mutex)) {
Paolo Bonzinie0c38212015-08-26 00:19:19 +02001220 qemu_cpu_kick_no_halt();
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001221 qemu_mutex_lock(&qemu_global_mutex);
1222 }
Paolo Bonzini6b498092015-02-27 19:58:23 +01001223 atomic_dec(&iothread_requesting_mutex);
Paolo Bonzini46daff12011-06-09 13:10:24 +02001224 qemu_cond_broadcast(&qemu_io_proceeded_cond);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -03001225 }
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001226 iothread_locked = true;
Blue Swirl296af7c2010-03-29 19:23:50 +00001227}
1228
1229void qemu_mutex_unlock_iothread(void)
1230{
Paolo Bonziniafbe7052015-06-18 18:47:19 +02001231 iothread_locked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +00001232 qemu_mutex_unlock(&qemu_global_mutex);
1233}
1234
1235static int all_vcpus_paused(void)
1236{
Andreas Färberbdc44642013-06-24 23:50:24 +02001237 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001238
Andreas Färberbdc44642013-06-24 23:50:24 +02001239 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001240 if (!cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001241 return 0;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001242 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001243 }
1244
1245 return 1;
1246}
1247
1248void pause_all_vcpus(void)
1249{
Andreas Färberbdc44642013-06-24 23:50:24 +02001250 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001251
Alex Bligh40daca52013-08-21 16:03:02 +01001252 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
Andreas Färberbdc44642013-06-24 23:50:24 +02001253 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001254 cpu->stop = true;
1255 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001256 }
1257
Juan Quintelaaa723c22012-09-18 16:30:11 +02001258 if (qemu_in_vcpu_thread()) {
Jan Kiszkad798e972012-02-17 18:31:16 +01001259 cpu_stop_current();
1260 if (!kvm_enabled()) {
Andreas Färberbdc44642013-06-24 23:50:24 +02001261 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001262 cpu->stop = false;
1263 cpu->stopped = true;
Jan Kiszkad798e972012-02-17 18:31:16 +01001264 }
1265 return;
1266 }
1267 }
1268
Blue Swirl296af7c2010-03-29 19:23:50 +00001269 while (!all_vcpus_paused()) {
Paolo Bonzinibe7d6c52011-03-12 17:44:02 +01001270 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
Andreas Färberbdc44642013-06-24 23:50:24 +02001271 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001272 qemu_cpu_kick(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001273 }
1274 }
1275}
1276
Igor Mammedov29936832013-04-23 10:29:37 +02001277void cpu_resume(CPUState *cpu)
1278{
1279 cpu->stop = false;
1280 cpu->stopped = false;
1281 qemu_cpu_kick(cpu);
1282}
1283
Blue Swirl296af7c2010-03-29 19:23:50 +00001284void resume_all_vcpus(void)
1285{
Andreas Färberbdc44642013-06-24 23:50:24 +02001286 CPUState *cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001287
Alex Bligh40daca52013-08-21 16:03:02 +01001288 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
Andreas Färberbdc44642013-06-24 23:50:24 +02001289 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +02001290 cpu_resume(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001291 }
1292}
1293
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001294/* For temporary buffers for forming a name */
1295#define VCPU_THREAD_NAME_SIZE 16
1296
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001297static void qemu_tcg_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001298{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001299 char thread_name[VCPU_THREAD_NAME_SIZE];
KONRAD Fredericd5f8d612015-08-10 17:27:06 +02001300 static QemuCond *tcg_halt_cond;
1301 static QemuThread *tcg_cpu_thread;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001302
Edgar E. Iglesias09daed82013-12-17 13:06:51 +10001303 tcg_cpu_address_space_init(cpu, cpu->as);
1304
Blue Swirl296af7c2010-03-29 19:23:50 +00001305 /* share a single thread for all cpus with TCG */
1306 if (!tcg_cpu_thread) {
Andreas Färber814e6122012-05-02 17:00:37 +02001307 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001308 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1309 qemu_cond_init(cpu->halt_cond);
1310 tcg_halt_cond = cpu->halt_cond;
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001311 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG",
1312 cpu->cpu_index);
1313 qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn,
1314 cpu, QEMU_THREAD_JOINABLE);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001315#ifdef _WIN32
Andreas Färber814e6122012-05-02 17:00:37 +02001316 cpu->hThread = qemu_thread_get_handle(cpu->thread);
Paolo Bonzini1ecf47b2011-12-13 13:43:52 +01001317#endif
Andreas Färber61a46212012-05-02 22:49:36 +02001318 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001319 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001320 }
Andreas Färber814e6122012-05-02 17:00:37 +02001321 tcg_cpu_thread = cpu->thread;
Blue Swirl296af7c2010-03-29 19:23:50 +00001322 } else {
Andreas Färber814e6122012-05-02 17:00:37 +02001323 cpu->thread = tcg_cpu_thread;
Andreas Färberf5c121b2012-05-03 01:22:49 +02001324 cpu->halt_cond = tcg_halt_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +00001325 }
1326}
1327
Andreas Färber48a106b2013-05-27 02:20:39 +02001328static void qemu_kvm_start_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001329{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001330 char thread_name[VCPU_THREAD_NAME_SIZE];
1331
Andreas Färber814e6122012-05-02 17:00:37 +02001332 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001333 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1334 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001335 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/KVM",
1336 cpu->cpu_index);
1337 qemu_thread_create(cpu->thread, thread_name, qemu_kvm_cpu_thread_fn,
1338 cpu, QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001339 while (!cpu->created) {
Paolo Bonzini18a85722011-03-12 17:44:03 +01001340 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001341 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001342}
1343
Andreas Färber10a90212013-05-27 02:24:35 +02001344static void qemu_dummy_start_vcpu(CPUState *cpu)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001345{
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001346 char thread_name[VCPU_THREAD_NAME_SIZE];
1347
Andreas Färber814e6122012-05-02 17:00:37 +02001348 cpu->thread = g_malloc0(sizeof(QemuThread));
Andreas Färberf5c121b2012-05-03 01:22:49 +02001349 cpu->halt_cond = g_malloc0(sizeof(QemuCond));
1350 qemu_cond_init(cpu->halt_cond);
Dr. David Alan Gilbert49001162014-01-30 10:20:32 +00001351 snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/DUMMY",
1352 cpu->cpu_index);
1353 qemu_thread_create(cpu->thread, thread_name, qemu_dummy_cpu_thread_fn, cpu,
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001354 QEMU_THREAD_JOINABLE);
Andreas Färber61a46212012-05-02 22:49:36 +02001355 while (!cpu->created) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001356 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1357 }
1358}
1359
Andreas Färberc643bed2013-05-27 03:23:24 +02001360void qemu_init_vcpu(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001361{
Andreas Färberce3960e2012-12-17 03:27:07 +01001362 cpu->nr_cores = smp_cores;
1363 cpu->nr_threads = smp_threads;
Andreas Färberf324e762012-05-02 23:26:21 +02001364 cpu->stopped = true;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001365 if (kvm_enabled()) {
Andreas Färber48a106b2013-05-27 02:20:39 +02001366 qemu_kvm_start_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001367 } else if (tcg_enabled()) {
Andreas Färbere5ab30a2012-05-03 01:50:44 +02001368 qemu_tcg_init_vcpu(cpu);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001369 } else {
Andreas Färber10a90212013-05-27 02:24:35 +02001370 qemu_dummy_start_vcpu(cpu);
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001371 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001372}
1373
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001374void cpu_stop_current(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001375{
Andreas Färber4917cf42013-05-27 05:17:50 +02001376 if (current_cpu) {
1377 current_cpu->stop = false;
1378 current_cpu->stopped = true;
1379 cpu_exit(current_cpu);
Paolo Bonzini67bb1722011-03-12 17:43:59 +01001380 qemu_cond_signal(&qemu_pause_cond);
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001381 }
Blue Swirl296af7c2010-03-29 19:23:50 +00001382}
1383
Kevin Wolf56983462013-07-05 13:49:54 +02001384int vm_stop(RunState state)
Blue Swirl296af7c2010-03-29 19:23:50 +00001385{
Juan Quintelaaa723c22012-09-18 16:30:11 +02001386 if (qemu_in_vcpu_thread()) {
Paolo Bonzini74892d22014-06-05 14:53:58 +02001387 qemu_system_vmstop_request_prepare();
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001388 qemu_system_vmstop_request(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001389 /*
1390 * FIXME: should not return to device code in case
1391 * vm_stop() has been requested.
1392 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001393 cpu_stop_current();
Kevin Wolf56983462013-07-05 13:49:54 +02001394 return 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001395 }
Kevin Wolf56983462013-07-05 13:49:54 +02001396
1397 return do_vm_stop(state);
Blue Swirl296af7c2010-03-29 19:23:50 +00001398}
1399
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001400/* does a state transition even if the VM is already stopped,
1401 current state is forgotten forever */
Kevin Wolf56983462013-07-05 13:49:54 +02001402int vm_stop_force_state(RunState state)
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001403{
1404 if (runstate_is_running()) {
Kevin Wolf56983462013-07-05 13:49:54 +02001405 return vm_stop(state);
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001406 } else {
1407 runstate_set(state);
Kevin Wolf594a45c2013-07-18 14:52:19 +02001408 /* Make sure to return an error if the flush in a previous vm_stop()
1409 * failed. */
1410 return bdrv_flush_all();
Luiz Capitulino8a9236f2011-10-14 11:18:09 -03001411 }
1412}
1413
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001414static int tcg_cpu_exec(CPUState *cpu)
Blue Swirl296af7c2010-03-29 19:23:50 +00001415{
1416 int ret;
1417#ifdef CONFIG_PROFILER
1418 int64_t ti;
1419#endif
1420
1421#ifdef CONFIG_PROFILER
1422 ti = profile_getclock();
1423#endif
1424 if (use_icount) {
1425 int64_t count;
Alex Blighac70aaf2013-08-21 16:02:57 +01001426 int64_t deadline;
Blue Swirl296af7c2010-03-29 19:23:50 +00001427 int decr;
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001428 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1429 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001430 cpu->icount_decr.u16.low = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001431 cpu->icount_extra = 0;
Alex Bligh40daca52013-08-21 16:03:02 +01001432 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Alex Blighac70aaf2013-08-21 16:02:57 +01001433
1434 /* Maintain prior (possibly buggy) behaviour where if no deadline
Alex Bligh40daca52013-08-21 16:03:02 +01001435 * was set (as there is no QEMU_CLOCK_VIRTUAL timer) or it is more than
Alex Blighac70aaf2013-08-21 16:02:57 +01001436 * INT32_MAX nanoseconds ahead, we still use INT32_MAX
1437 * nanoseconds.
1438 */
1439 if ((deadline < 0) || (deadline > INT32_MAX)) {
1440 deadline = INT32_MAX;
1441 }
1442
1443 count = qemu_icount_round(deadline);
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001444 timers_state.qemu_icount += count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001445 decr = (count > 0xffff) ? 0xffff : count;
1446 count -= decr;
Andreas Färber28ecfd72013-08-26 05:51:49 +02001447 cpu->icount_decr.u16.low = decr;
Andreas Färberefee7342013-08-26 05:39:29 +02001448 cpu->icount_extra = count;
Blue Swirl296af7c2010-03-29 19:23:50 +00001449 }
Peter Crosthwaiteea3e9842015-06-18 10:24:55 -07001450 ret = cpu_exec(cpu);
Blue Swirl296af7c2010-03-29 19:23:50 +00001451#ifdef CONFIG_PROFILER
Alexey Kardashevskiy89d5cbd2015-03-16 14:57:38 +11001452 tcg_time += profile_getclock() - ti;
Blue Swirl296af7c2010-03-29 19:23:50 +00001453#endif
1454 if (use_icount) {
1455 /* Fold pending instructions back into the
1456 instruction counter, and clear the interrupt flag. */
KONRAD Fredericc96778b2014-08-01 01:37:09 +02001457 timers_state.qemu_icount -= (cpu->icount_decr.u16.low
1458 + cpu->icount_extra);
Andreas Färber28ecfd72013-08-26 05:51:49 +02001459 cpu->icount_decr.u32 = 0;
Andreas Färberefee7342013-08-26 05:39:29 +02001460 cpu->icount_extra = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +00001461 }
1462 return ret;
1463}
1464
Jan Kiszkabdb7ca62011-09-26 09:40:39 +02001465static void tcg_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +00001466{
Jan Kiszka9a360852011-02-01 22:15:55 +01001467 int r;
1468
Alex Bligh40daca52013-08-21 16:03:02 +01001469 /* Account partial waits to QEMU_CLOCK_VIRTUAL. */
1470 qemu_clock_warp(QEMU_CLOCK_VIRTUAL);
Paolo Bonziniab33fcd2011-04-13 10:03:44 +02001471
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001472 if (next_cpu == NULL) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001473 next_cpu = first_cpu;
Jan Kiszka0ab07c62011-02-07 12:19:14 +01001474 }
Andreas Färberbdc44642013-06-24 23:50:24 +02001475 for (; next_cpu != NULL && !exit_request; next_cpu = CPU_NEXT(next_cpu)) {
Andreas Färber182735e2013-05-29 22:29:20 +02001476 CPUState *cpu = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +00001477
Alex Bligh40daca52013-08-21 16:03:02 +01001478 qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
Andreas Färbered2803d2013-06-21 20:20:45 +02001479 (cpu->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001480
Andreas Färbera1fcaa72012-05-02 23:42:26 +02001481 if (cpu_can_run(cpu)) {
Peter Crosthwaite3d57f782015-06-23 19:31:17 -07001482 r = tcg_cpu_exec(cpu);
Jan Kiszka9a360852011-02-01 22:15:55 +01001483 if (r == EXCP_DEBUG) {
Andreas Färber91325042013-05-27 02:07:49 +02001484 cpu_handle_guest_debug(cpu);
Jan Kiszka3c638d02010-06-25 16:56:56 +02001485 break;
1486 }
Andreas Färberf324e762012-05-02 23:26:21 +02001487 } else if (cpu->stop || cpu->stopped) {
Blue Swirl296af7c2010-03-29 19:23:50 +00001488 break;
1489 }
1490 }
Paolo Bonziniaed807c2015-08-18 06:43:15 -07001491
1492 /* Pairs with smp_wmb in qemu_cpu_kick. */
1493 atomic_mb_set(&exit_request, 0);
Blue Swirl296af7c2010-03-29 19:23:50 +00001494}
1495
Stefan Weil9a78eea2010-10-22 23:03:33 +02001496void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001497{
1498 /* XXX: implement xxx_cpu_list for targets that still miss it */
Peter Maydelle916cbf2012-09-05 17:41:08 -03001499#if defined(cpu_list)
1500 cpu_list(f, cpu_fprintf);
Blue Swirl262353c2010-05-04 19:55:35 +00001501#endif
1502}
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001503
1504CpuInfoList *qmp_query_cpus(Error **errp)
1505{
1506 CpuInfoList *head = NULL, *cur_item = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +02001507 CPUState *cpu;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001508
Andreas Färberbdc44642013-06-24 23:50:24 +02001509 CPU_FOREACH(cpu) {
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001510 CpuInfoList *info;
Andreas Färber182735e2013-05-29 22:29:20 +02001511#if defined(TARGET_I386)
1512 X86CPU *x86_cpu = X86_CPU(cpu);
1513 CPUX86State *env = &x86_cpu->env;
1514#elif defined(TARGET_PPC)
1515 PowerPCCPU *ppc_cpu = POWERPC_CPU(cpu);
1516 CPUPPCState *env = &ppc_cpu->env;
1517#elif defined(TARGET_SPARC)
1518 SPARCCPU *sparc_cpu = SPARC_CPU(cpu);
1519 CPUSPARCState *env = &sparc_cpu->env;
1520#elif defined(TARGET_MIPS)
1521 MIPSCPU *mips_cpu = MIPS_CPU(cpu);
1522 CPUMIPSState *env = &mips_cpu->env;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001523#elif defined(TARGET_TRICORE)
1524 TriCoreCPU *tricore_cpu = TRICORE_CPU(cpu);
1525 CPUTriCoreState *env = &tricore_cpu->env;
Andreas Färber182735e2013-05-29 22:29:20 +02001526#endif
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001527
Andreas Färbercb446ec2013-05-01 14:24:52 +02001528 cpu_synchronize_state(cpu);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001529
1530 info = g_malloc0(sizeof(*info));
1531 info->value = g_malloc0(sizeof(*info->value));
Andreas Färber55e5c282012-12-17 06:18:02 +01001532 info->value->CPU = cpu->cpu_index;
Andreas Färber182735e2013-05-29 22:29:20 +02001533 info->value->current = (cpu == first_cpu);
Andreas Färber259186a2013-01-17 18:51:17 +01001534 info->value->halted = cpu->halted;
Eduardo Habkost58f88d42015-05-08 16:04:22 -03001535 info->value->qom_path = object_get_canonical_path(OBJECT(cpu));
Andreas Färber9f09e182012-05-03 06:59:07 +02001536 info->value->thread_id = cpu->thread_id;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001537#if defined(TARGET_I386)
1538 info->value->has_pc = true;
1539 info->value->pc = env->eip + env->segs[R_CS].base;
1540#elif defined(TARGET_PPC)
1541 info->value->has_nip = true;
1542 info->value->nip = env->nip;
1543#elif defined(TARGET_SPARC)
1544 info->value->has_pc = true;
1545 info->value->pc = env->pc;
1546 info->value->has_npc = true;
1547 info->value->npc = env->npc;
1548#elif defined(TARGET_MIPS)
1549 info->value->has_PC = true;
1550 info->value->PC = env->active_tc.PC;
Bastian Koppelmann48e06fe2014-09-01 12:59:46 +01001551#elif defined(TARGET_TRICORE)
1552 info->value->has_PC = true;
1553 info->value->PC = env->PC;
Luiz Capitulinode0b36b2011-09-21 16:38:35 -03001554#endif
1555
1556 /* XXX: waiting for the qapi to support GSList */
1557 if (!cur_item) {
1558 head = cur_item = info;
1559 } else {
1560 cur_item->next = info;
1561 cur_item = info;
1562 }
1563 }
1564
1565 return head;
1566}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001567
1568void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1569 bool has_cpu, int64_t cpu_index, Error **errp)
1570{
1571 FILE *f;
1572 uint32_t l;
Andreas Färber55e5c282012-12-17 06:18:02 +01001573 CPUState *cpu;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001574 uint8_t buf[1024];
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001575 int64_t orig_addr = addr, orig_size = size;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001576
1577 if (!has_cpu) {
1578 cpu_index = 0;
1579 }
1580
Andreas Färber151d1322013-02-15 15:41:49 +01001581 cpu = qemu_get_cpu(cpu_index);
1582 if (cpu == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001583 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1584 "a CPU number");
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001585 return;
1586 }
1587
1588 f = fopen(filename, "wb");
1589 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001590 error_setg_file_open(errp, errno, filename);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001591 return;
1592 }
1593
1594 while (size != 0) {
1595 l = sizeof(buf);
1596 if (l > size)
1597 l = size;
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301598 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
Borislav Petkov0dc9daf2015-02-08 13:14:38 +01001599 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
1600 " specified", orig_addr, orig_size);
Aneesh Kumar K.V2f4d0f52013-10-01 21:49:30 +05301601 goto exit;
1602 }
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001603 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001604 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -02001605 goto exit;
1606 }
1607 addr += l;
1608 size -= l;
1609 }
1610
1611exit:
1612 fclose(f);
1613}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001614
1615void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1616 Error **errp)
1617{
1618 FILE *f;
1619 uint32_t l;
1620 uint8_t buf[1024];
1621
1622 f = fopen(filename, "wb");
1623 if (!f) {
Luiz Capitulino618da852013-06-07 14:35:06 -04001624 error_setg_file_open(errp, errno, filename);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001625 return;
1626 }
1627
1628 while (size != 0) {
1629 l = sizeof(buf);
1630 if (l > size)
1631 l = size;
Stefan Weileb6282f2014-04-07 20:28:23 +02001632 cpu_physical_memory_read(addr, buf, l);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001633 if (fwrite(buf, 1, l, f) != l) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001634 error_setg(errp, QERR_IO_ERROR);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -02001635 goto exit;
1636 }
1637 addr += l;
1638 size -= l;
1639 }
1640
1641exit:
1642 fclose(f);
1643}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001644
1645void qmp_inject_nmi(Error **errp)
1646{
1647#if defined(TARGET_I386)
Andreas Färber182735e2013-05-29 22:29:20 +02001648 CPUState *cs;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001649
Andreas Färberbdc44642013-06-24 23:50:24 +02001650 CPU_FOREACH(cs) {
Andreas Färber182735e2013-05-29 22:29:20 +02001651 X86CPU *cpu = X86_CPU(cs);
Andreas Färber182735e2013-05-29 22:29:20 +02001652
Chen Fan02e51482013-12-23 17:04:02 +08001653 if (!cpu->apic_state) {
Andreas Färber182735e2013-05-29 22:29:20 +02001654 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
Jan Kiszka02c09192011-10-18 00:00:06 +08001655 } else {
Chen Fan02e51482013-12-23 17:04:02 +08001656 apic_deliver_nmi(cpu->apic_state);
Jan Kiszka02c09192011-10-18 00:00:06 +08001657 }
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001658 }
1659#else
Alexey Kardashevskiy9cb805f2014-08-20 22:16:33 +10001660 nmi_monitor_handle(monitor_get_cpu_index(), errp);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -02001661#endif
1662}
Sebastian Tanase27498be2014-07-25 11:56:33 +02001663
1664void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)
1665{
1666 if (!use_icount) {
1667 return;
1668 }
1669
1670 cpu_fprintf(f, "Host - Guest clock %"PRIi64" ms\n",
1671 (cpu_get_clock() - cpu_get_icount())/SCALE_MS);
1672 if (icount_align_option) {
1673 cpu_fprintf(f, "Max guest delay %"PRIi64" ms\n", -max_delay/SCALE_MS);
1674 cpu_fprintf(f, "Max guest advance %"PRIi64" ms\n", max_advance/SCALE_MS);
1675 } else {
1676 cpu_fprintf(f, "Max guest delay NA\n");
1677 cpu_fprintf(f, "Max guest advance NA\n");
1678 }
1679}