Statistics
| Branch: | Revision:

root / cpus.c @ 09f1bbcd

History | View | Annotate | Download (32.7 kB)

1 296af7c9 Blue Swirl
/*
2 296af7c9 Blue Swirl
 * QEMU System Emulator
3 296af7c9 Blue Swirl
 *
4 296af7c9 Blue Swirl
 * Copyright (c) 2003-2008 Fabrice Bellard
5 296af7c9 Blue Swirl
 *
6 296af7c9 Blue Swirl
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 296af7c9 Blue Swirl
 * of this software and associated documentation files (the "Software"), to deal
8 296af7c9 Blue Swirl
 * in the Software without restriction, including without limitation the rights
9 296af7c9 Blue Swirl
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 296af7c9 Blue Swirl
 * copies of the Software, and to permit persons to whom the Software is
11 296af7c9 Blue Swirl
 * furnished to do so, subject to the following conditions:
12 296af7c9 Blue Swirl
 *
13 296af7c9 Blue Swirl
 * The above copyright notice and this permission notice shall be included in
14 296af7c9 Blue Swirl
 * all copies or substantial portions of the Software.
15 296af7c9 Blue Swirl
 *
16 296af7c9 Blue Swirl
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 296af7c9 Blue Swirl
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 296af7c9 Blue Swirl
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 296af7c9 Blue Swirl
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 296af7c9 Blue Swirl
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 296af7c9 Blue Swirl
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 296af7c9 Blue Swirl
 * THE SOFTWARE.
23 296af7c9 Blue Swirl
 */
24 296af7c9 Blue Swirl
25 296af7c9 Blue Swirl
/* Needed early for CONFIG_BSD etc. */
26 296af7c9 Blue Swirl
#include "config-host.h"
27 296af7c9 Blue Swirl
28 296af7c9 Blue Swirl
#include "monitor.h"
29 296af7c9 Blue Swirl
#include "sysemu.h"
30 296af7c9 Blue Swirl
#include "gdbstub.h"
31 296af7c9 Blue Swirl
#include "dma.h"
32 296af7c9 Blue Swirl
#include "kvm.h"
33 de0b36b6 Luiz Capitulino
#include "qmp-commands.h"
34 296af7c9 Blue Swirl
35 96284e89 Paolo Bonzini
#include "qemu-thread.h"
36 296af7c9 Blue Swirl
#include "cpus.h"
37 8156be56 Paolo Bonzini
#include "qtest.h"
38 44a9b356 Paolo Bonzini
#include "main-loop.h"
39 0ff0fc19 Jan Kiszka
40 0ff0fc19 Jan Kiszka
#ifndef _WIN32
41 a8486bc9 Marcelo Tosatti
#include "compatfd.h"
42 0ff0fc19 Jan Kiszka
#endif
43 296af7c9 Blue Swirl
44 6d9cb73c Jan Kiszka
#ifdef CONFIG_LINUX
45 6d9cb73c Jan Kiszka
46 6d9cb73c Jan Kiszka
#include <sys/prctl.h>
47 6d9cb73c Jan Kiszka
48 c0532a76 Marcelo Tosatti
#ifndef PR_MCE_KILL
49 c0532a76 Marcelo Tosatti
#define PR_MCE_KILL 33
50 c0532a76 Marcelo Tosatti
#endif
51 c0532a76 Marcelo Tosatti
52 6d9cb73c Jan Kiszka
#ifndef PR_MCE_KILL_SET
53 6d9cb73c Jan Kiszka
#define PR_MCE_KILL_SET 1
54 6d9cb73c Jan Kiszka
#endif
55 6d9cb73c Jan Kiszka
56 6d9cb73c Jan Kiszka
#ifndef PR_MCE_KILL_EARLY
57 6d9cb73c Jan Kiszka
#define PR_MCE_KILL_EARLY 1
58 6d9cb73c Jan Kiszka
#endif
59 6d9cb73c Jan Kiszka
60 6d9cb73c Jan Kiszka
#endif /* CONFIG_LINUX */
61 6d9cb73c Jan Kiszka
62 9349b4f9 Andreas Färber
static CPUArchState *next_cpu;
63 296af7c9 Blue Swirl
64 296af7c9 Blue Swirl
/***********************************************************/
65 946fb27c Paolo Bonzini
/* guest cycle counter */
66 946fb27c Paolo Bonzini
67 946fb27c Paolo Bonzini
/* Conversion factor from emulated instructions to virtual clock ticks.  */
68 946fb27c Paolo Bonzini
static int icount_time_shift;
69 946fb27c Paolo Bonzini
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
70 946fb27c Paolo Bonzini
#define MAX_ICOUNT_SHIFT 10
71 946fb27c Paolo Bonzini
/* Compensate for varying guest execution speed.  */
72 946fb27c Paolo Bonzini
static int64_t qemu_icount_bias;
73 946fb27c Paolo Bonzini
static QEMUTimer *icount_rt_timer;
74 946fb27c Paolo Bonzini
static QEMUTimer *icount_vm_timer;
75 946fb27c Paolo Bonzini
static QEMUTimer *icount_warp_timer;
76 946fb27c Paolo Bonzini
static int64_t vm_clock_warp_start;
77 946fb27c Paolo Bonzini
static int64_t qemu_icount;
78 946fb27c Paolo Bonzini
79 946fb27c Paolo Bonzini
typedef struct TimersState {
80 946fb27c Paolo Bonzini
    int64_t cpu_ticks_prev;
81 946fb27c Paolo Bonzini
    int64_t cpu_ticks_offset;
82 946fb27c Paolo Bonzini
    int64_t cpu_clock_offset;
83 946fb27c Paolo Bonzini
    int32_t cpu_ticks_enabled;
84 946fb27c Paolo Bonzini
    int64_t dummy;
85 946fb27c Paolo Bonzini
} TimersState;
86 946fb27c Paolo Bonzini
87 946fb27c Paolo Bonzini
TimersState timers_state;
88 946fb27c Paolo Bonzini
89 946fb27c Paolo Bonzini
/* Return the virtual CPU time, based on the instruction counter.  */
90 946fb27c Paolo Bonzini
int64_t cpu_get_icount(void)
91 946fb27c Paolo Bonzini
{
92 946fb27c Paolo Bonzini
    int64_t icount;
93 9349b4f9 Andreas Färber
    CPUArchState *env = cpu_single_env;
94 946fb27c Paolo Bonzini
95 946fb27c Paolo Bonzini
    icount = qemu_icount;
96 946fb27c Paolo Bonzini
    if (env) {
97 946fb27c Paolo Bonzini
        if (!can_do_io(env)) {
98 946fb27c Paolo Bonzini
            fprintf(stderr, "Bad clock read\n");
99 946fb27c Paolo Bonzini
        }
100 946fb27c Paolo Bonzini
        icount -= (env->icount_decr.u16.low + env->icount_extra);
101 946fb27c Paolo Bonzini
    }
102 946fb27c Paolo Bonzini
    return qemu_icount_bias + (icount << icount_time_shift);
103 946fb27c Paolo Bonzini
}
104 946fb27c Paolo Bonzini
105 946fb27c Paolo Bonzini
/* return the host CPU cycle counter and handle stop/restart */
106 946fb27c Paolo Bonzini
int64_t cpu_get_ticks(void)
107 946fb27c Paolo Bonzini
{
108 946fb27c Paolo Bonzini
    if (use_icount) {
109 946fb27c Paolo Bonzini
        return cpu_get_icount();
110 946fb27c Paolo Bonzini
    }
111 946fb27c Paolo Bonzini
    if (!timers_state.cpu_ticks_enabled) {
112 946fb27c Paolo Bonzini
        return timers_state.cpu_ticks_offset;
113 946fb27c Paolo Bonzini
    } else {
114 946fb27c Paolo Bonzini
        int64_t ticks;
115 946fb27c Paolo Bonzini
        ticks = cpu_get_real_ticks();
116 946fb27c Paolo Bonzini
        if (timers_state.cpu_ticks_prev > ticks) {
117 946fb27c Paolo Bonzini
            /* Note: non increasing ticks may happen if the host uses
118 946fb27c Paolo Bonzini
               software suspend */
119 946fb27c Paolo Bonzini
            timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
120 946fb27c Paolo Bonzini
        }
121 946fb27c Paolo Bonzini
        timers_state.cpu_ticks_prev = ticks;
122 946fb27c Paolo Bonzini
        return ticks + timers_state.cpu_ticks_offset;
123 946fb27c Paolo Bonzini
    }
124 946fb27c Paolo Bonzini
}
125 946fb27c Paolo Bonzini
126 946fb27c Paolo Bonzini
/* return the host CPU monotonic timer and handle stop/restart */
127 946fb27c Paolo Bonzini
int64_t cpu_get_clock(void)
128 946fb27c Paolo Bonzini
{
129 946fb27c Paolo Bonzini
    int64_t ti;
130 946fb27c Paolo Bonzini
    if (!timers_state.cpu_ticks_enabled) {
131 946fb27c Paolo Bonzini
        return timers_state.cpu_clock_offset;
132 946fb27c Paolo Bonzini
    } else {
133 946fb27c Paolo Bonzini
        ti = get_clock();
134 946fb27c Paolo Bonzini
        return ti + timers_state.cpu_clock_offset;
135 946fb27c Paolo Bonzini
    }
136 946fb27c Paolo Bonzini
}
137 946fb27c Paolo Bonzini
138 946fb27c Paolo Bonzini
/* enable cpu_get_ticks() */
139 946fb27c Paolo Bonzini
void cpu_enable_ticks(void)
140 946fb27c Paolo Bonzini
{
141 946fb27c Paolo Bonzini
    if (!timers_state.cpu_ticks_enabled) {
142 946fb27c Paolo Bonzini
        timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
143 946fb27c Paolo Bonzini
        timers_state.cpu_clock_offset -= get_clock();
144 946fb27c Paolo Bonzini
        timers_state.cpu_ticks_enabled = 1;
145 946fb27c Paolo Bonzini
    }
146 946fb27c Paolo Bonzini
}
147 946fb27c Paolo Bonzini
148 946fb27c Paolo Bonzini
/* disable cpu_get_ticks() : the clock is stopped. You must not call
149 946fb27c Paolo Bonzini
   cpu_get_ticks() after that.  */
150 946fb27c Paolo Bonzini
void cpu_disable_ticks(void)
151 946fb27c Paolo Bonzini
{
152 946fb27c Paolo Bonzini
    if (timers_state.cpu_ticks_enabled) {
153 946fb27c Paolo Bonzini
        timers_state.cpu_ticks_offset = cpu_get_ticks();
154 946fb27c Paolo Bonzini
        timers_state.cpu_clock_offset = cpu_get_clock();
155 946fb27c Paolo Bonzini
        timers_state.cpu_ticks_enabled = 0;
156 946fb27c Paolo Bonzini
    }
157 946fb27c Paolo Bonzini
}
158 946fb27c Paolo Bonzini
159 946fb27c Paolo Bonzini
/* Correlation between real and virtual time is always going to be
160 946fb27c Paolo Bonzini
   fairly approximate, so ignore small variation.
161 946fb27c Paolo Bonzini
   When the guest is idle real and virtual time will be aligned in
162 946fb27c Paolo Bonzini
   the IO wait loop.  */
163 946fb27c Paolo Bonzini
#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
164 946fb27c Paolo Bonzini
165 946fb27c Paolo Bonzini
static void icount_adjust(void)
166 946fb27c Paolo Bonzini
{
167 946fb27c Paolo Bonzini
    int64_t cur_time;
168 946fb27c Paolo Bonzini
    int64_t cur_icount;
169 946fb27c Paolo Bonzini
    int64_t delta;
170 946fb27c Paolo Bonzini
    static int64_t last_delta;
171 946fb27c Paolo Bonzini
    /* If the VM is not running, then do nothing.  */
172 946fb27c Paolo Bonzini
    if (!runstate_is_running()) {
173 946fb27c Paolo Bonzini
        return;
174 946fb27c Paolo Bonzini
    }
175 946fb27c Paolo Bonzini
    cur_time = cpu_get_clock();
176 946fb27c Paolo Bonzini
    cur_icount = qemu_get_clock_ns(vm_clock);
177 946fb27c Paolo Bonzini
    delta = cur_icount - cur_time;
178 946fb27c Paolo Bonzini
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
179 946fb27c Paolo Bonzini
    if (delta > 0
180 946fb27c Paolo Bonzini
        && last_delta + ICOUNT_WOBBLE < delta * 2
181 946fb27c Paolo Bonzini
        && icount_time_shift > 0) {
182 946fb27c Paolo Bonzini
        /* The guest is getting too far ahead.  Slow time down.  */
183 946fb27c Paolo Bonzini
        icount_time_shift--;
184 946fb27c Paolo Bonzini
    }
185 946fb27c Paolo Bonzini
    if (delta < 0
186 946fb27c Paolo Bonzini
        && last_delta - ICOUNT_WOBBLE > delta * 2
187 946fb27c Paolo Bonzini
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
188 946fb27c Paolo Bonzini
        /* The guest is getting too far behind.  Speed time up.  */
189 946fb27c Paolo Bonzini
        icount_time_shift++;
190 946fb27c Paolo Bonzini
    }
191 946fb27c Paolo Bonzini
    last_delta = delta;
192 946fb27c Paolo Bonzini
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
193 946fb27c Paolo Bonzini
}
194 946fb27c Paolo Bonzini
195 946fb27c Paolo Bonzini
static void icount_adjust_rt(void *opaque)
196 946fb27c Paolo Bonzini
{
197 946fb27c Paolo Bonzini
    qemu_mod_timer(icount_rt_timer,
198 946fb27c Paolo Bonzini
                   qemu_get_clock_ms(rt_clock) + 1000);
199 946fb27c Paolo Bonzini
    icount_adjust();
200 946fb27c Paolo Bonzini
}
201 946fb27c Paolo Bonzini
202 946fb27c Paolo Bonzini
static void icount_adjust_vm(void *opaque)
203 946fb27c Paolo Bonzini
{
204 946fb27c Paolo Bonzini
    qemu_mod_timer(icount_vm_timer,
205 946fb27c Paolo Bonzini
                   qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
206 946fb27c Paolo Bonzini
    icount_adjust();
207 946fb27c Paolo Bonzini
}
208 946fb27c Paolo Bonzini
209 946fb27c Paolo Bonzini
static int64_t qemu_icount_round(int64_t count)
210 946fb27c Paolo Bonzini
{
211 946fb27c Paolo Bonzini
    return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
212 946fb27c Paolo Bonzini
}
213 946fb27c Paolo Bonzini
214 946fb27c Paolo Bonzini
static void icount_warp_rt(void *opaque)
215 946fb27c Paolo Bonzini
{
216 946fb27c Paolo Bonzini
    if (vm_clock_warp_start == -1) {
217 946fb27c Paolo Bonzini
        return;
218 946fb27c Paolo Bonzini
    }
219 946fb27c Paolo Bonzini
220 946fb27c Paolo Bonzini
    if (runstate_is_running()) {
221 946fb27c Paolo Bonzini
        int64_t clock = qemu_get_clock_ns(rt_clock);
222 946fb27c Paolo Bonzini
        int64_t warp_delta = clock - vm_clock_warp_start;
223 946fb27c Paolo Bonzini
        if (use_icount == 1) {
224 946fb27c Paolo Bonzini
            qemu_icount_bias += warp_delta;
225 946fb27c Paolo Bonzini
        } else {
226 946fb27c Paolo Bonzini
            /*
227 946fb27c Paolo Bonzini
             * In adaptive mode, do not let the vm_clock run too
228 946fb27c Paolo Bonzini
             * far ahead of real time.
229 946fb27c Paolo Bonzini
             */
230 946fb27c Paolo Bonzini
            int64_t cur_time = cpu_get_clock();
231 946fb27c Paolo Bonzini
            int64_t cur_icount = qemu_get_clock_ns(vm_clock);
232 946fb27c Paolo Bonzini
            int64_t delta = cur_time - cur_icount;
233 946fb27c Paolo Bonzini
            qemu_icount_bias += MIN(warp_delta, delta);
234 946fb27c Paolo Bonzini
        }
235 946fb27c Paolo Bonzini
        if (qemu_clock_expired(vm_clock)) {
236 946fb27c Paolo Bonzini
            qemu_notify_event();
237 946fb27c Paolo Bonzini
        }
238 946fb27c Paolo Bonzini
    }
239 946fb27c Paolo Bonzini
    vm_clock_warp_start = -1;
240 946fb27c Paolo Bonzini
}
241 946fb27c Paolo Bonzini
242 8156be56 Paolo Bonzini
void qtest_clock_warp(int64_t dest)
243 8156be56 Paolo Bonzini
{
244 8156be56 Paolo Bonzini
    int64_t clock = qemu_get_clock_ns(vm_clock);
245 8156be56 Paolo Bonzini
    assert(qtest_enabled());
246 8156be56 Paolo Bonzini
    while (clock < dest) {
247 8156be56 Paolo Bonzini
        int64_t deadline = qemu_clock_deadline(vm_clock);
248 8156be56 Paolo Bonzini
        int64_t warp = MIN(dest - clock, deadline);
249 8156be56 Paolo Bonzini
        qemu_icount_bias += warp;
250 8156be56 Paolo Bonzini
        qemu_run_timers(vm_clock);
251 8156be56 Paolo Bonzini
        clock = qemu_get_clock_ns(vm_clock);
252 8156be56 Paolo Bonzini
    }
253 8156be56 Paolo Bonzini
    qemu_notify_event();
254 8156be56 Paolo Bonzini
}
255 8156be56 Paolo Bonzini
256 946fb27c Paolo Bonzini
void qemu_clock_warp(QEMUClock *clock)
257 946fb27c Paolo Bonzini
{
258 946fb27c Paolo Bonzini
    int64_t deadline;
259 946fb27c Paolo Bonzini
260 946fb27c Paolo Bonzini
    /*
261 946fb27c Paolo Bonzini
     * There are too many global variables to make the "warp" behavior
262 946fb27c Paolo Bonzini
     * applicable to other clocks.  But a clock argument removes the
263 946fb27c Paolo Bonzini
     * need for if statements all over the place.
264 946fb27c Paolo Bonzini
     */
265 946fb27c Paolo Bonzini
    if (clock != vm_clock || !use_icount) {
266 946fb27c Paolo Bonzini
        return;
267 946fb27c Paolo Bonzini
    }
268 946fb27c Paolo Bonzini
269 946fb27c Paolo Bonzini
    /*
270 946fb27c Paolo Bonzini
     * If the CPUs have been sleeping, advance the vm_clock timer now.  This
271 946fb27c Paolo Bonzini
     * ensures that the deadline for the timer is computed correctly below.
272 946fb27c Paolo Bonzini
     * This also makes sure that the insn counter is synchronized before the
273 946fb27c Paolo Bonzini
     * CPU starts running, in case the CPU is woken by an event other than
274 946fb27c Paolo Bonzini
     * the earliest vm_clock timer.
275 946fb27c Paolo Bonzini
     */
276 946fb27c Paolo Bonzini
    icount_warp_rt(NULL);
277 946fb27c Paolo Bonzini
    if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock)) {
278 946fb27c Paolo Bonzini
        qemu_del_timer(icount_warp_timer);
279 946fb27c Paolo Bonzini
        return;
280 946fb27c Paolo Bonzini
    }
281 946fb27c Paolo Bonzini
282 8156be56 Paolo Bonzini
    if (qtest_enabled()) {
283 8156be56 Paolo Bonzini
        /* When testing, qtest commands advance icount.  */
284 8156be56 Paolo Bonzini
        return;
285 8156be56 Paolo Bonzini
    }
286 8156be56 Paolo Bonzini
287 946fb27c Paolo Bonzini
    vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
288 946fb27c Paolo Bonzini
    deadline = qemu_clock_deadline(vm_clock);
289 946fb27c Paolo Bonzini
    if (deadline > 0) {
290 946fb27c Paolo Bonzini
        /*
291 946fb27c Paolo Bonzini
         * Ensure the vm_clock proceeds even when the virtual CPU goes to
292 946fb27c Paolo Bonzini
         * sleep.  Otherwise, the CPU might be waiting for a future timer
293 946fb27c Paolo Bonzini
         * interrupt to wake it up, but the interrupt never comes because
294 946fb27c Paolo Bonzini
         * the vCPU isn't running any insns and thus doesn't advance the
295 946fb27c Paolo Bonzini
         * vm_clock.
296 946fb27c Paolo Bonzini
         *
297 946fb27c Paolo Bonzini
         * An extreme solution for this problem would be to never let VCPUs
298 946fb27c Paolo Bonzini
         * sleep in icount mode if there is a pending vm_clock timer; rather
299 946fb27c Paolo Bonzini
         * time could just advance to the next vm_clock event.  Instead, we
300 946fb27c Paolo Bonzini
         * do stop VCPUs and only advance vm_clock after some "real" time,
301 946fb27c Paolo Bonzini
         * (related to the time left until the next event) has passed.  This
302 946fb27c Paolo Bonzini
         * rt_clock timer will do this.  This avoids that the warps are too
303 946fb27c Paolo Bonzini
         * visible externally---for example, you will not be sending network
304 07f35073 Dong Xu Wang
         * packets continuously instead of every 100ms.
305 946fb27c Paolo Bonzini
         */
306 946fb27c Paolo Bonzini
        qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);
307 946fb27c Paolo Bonzini
    } else {
308 946fb27c Paolo Bonzini
        qemu_notify_event();
309 946fb27c Paolo Bonzini
    }
310 946fb27c Paolo Bonzini
}
311 946fb27c Paolo Bonzini
312 946fb27c Paolo Bonzini
static const VMStateDescription vmstate_timers = {
313 946fb27c Paolo Bonzini
    .name = "timer",
314 946fb27c Paolo Bonzini
    .version_id = 2,
315 946fb27c Paolo Bonzini
    .minimum_version_id = 1,
316 946fb27c Paolo Bonzini
    .minimum_version_id_old = 1,
317 946fb27c Paolo Bonzini
    .fields      = (VMStateField[]) {
318 946fb27c Paolo Bonzini
        VMSTATE_INT64(cpu_ticks_offset, TimersState),
319 946fb27c Paolo Bonzini
        VMSTATE_INT64(dummy, TimersState),
320 946fb27c Paolo Bonzini
        VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
321 946fb27c Paolo Bonzini
        VMSTATE_END_OF_LIST()
322 946fb27c Paolo Bonzini
    }
323 946fb27c Paolo Bonzini
};
324 946fb27c Paolo Bonzini
325 946fb27c Paolo Bonzini
void configure_icount(const char *option)
326 946fb27c Paolo Bonzini
{
327 946fb27c Paolo Bonzini
    vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
328 946fb27c Paolo Bonzini
    if (!option) {
329 946fb27c Paolo Bonzini
        return;
330 946fb27c Paolo Bonzini
    }
331 946fb27c Paolo Bonzini
332 946fb27c Paolo Bonzini
    icount_warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
333 946fb27c Paolo Bonzini
    if (strcmp(option, "auto") != 0) {
334 946fb27c Paolo Bonzini
        icount_time_shift = strtol(option, NULL, 0);
335 946fb27c Paolo Bonzini
        use_icount = 1;
336 946fb27c Paolo Bonzini
        return;
337 946fb27c Paolo Bonzini
    }
338 946fb27c Paolo Bonzini
339 946fb27c Paolo Bonzini
    use_icount = 2;
340 946fb27c Paolo Bonzini
341 946fb27c Paolo Bonzini
    /* 125MIPS seems a reasonable initial guess at the guest speed.
342 946fb27c Paolo Bonzini
       It will be corrected fairly quickly anyway.  */
343 946fb27c Paolo Bonzini
    icount_time_shift = 3;
344 946fb27c Paolo Bonzini
345 946fb27c Paolo Bonzini
    /* Have both realtime and virtual time triggers for speed adjustment.
346 946fb27c Paolo Bonzini
       The realtime trigger catches emulated time passing too slowly,
347 946fb27c Paolo Bonzini
       the virtual time trigger catches emulated time passing too fast.
348 946fb27c Paolo Bonzini
       Realtime triggers occur even when idle, so use them less frequently
349 946fb27c Paolo Bonzini
       than VM triggers.  */
350 946fb27c Paolo Bonzini
    icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
351 946fb27c Paolo Bonzini
    qemu_mod_timer(icount_rt_timer,
352 946fb27c Paolo Bonzini
                   qemu_get_clock_ms(rt_clock) + 1000);
353 946fb27c Paolo Bonzini
    icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
354 946fb27c Paolo Bonzini
    qemu_mod_timer(icount_vm_timer,
355 946fb27c Paolo Bonzini
                   qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
356 946fb27c Paolo Bonzini
}
357 946fb27c Paolo Bonzini
358 946fb27c Paolo Bonzini
/***********************************************************/
359 296af7c9 Blue Swirl
void hw_error(const char *fmt, ...)
360 296af7c9 Blue Swirl
{
361 296af7c9 Blue Swirl
    va_list ap;
362 9349b4f9 Andreas Färber
    CPUArchState *env;
363 296af7c9 Blue Swirl
364 296af7c9 Blue Swirl
    va_start(ap, fmt);
365 296af7c9 Blue Swirl
    fprintf(stderr, "qemu: hardware error: ");
366 296af7c9 Blue Swirl
    vfprintf(stderr, fmt, ap);
367 296af7c9 Blue Swirl
    fprintf(stderr, "\n");
368 296af7c9 Blue Swirl
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
369 296af7c9 Blue Swirl
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
370 296af7c9 Blue Swirl
#ifdef TARGET_I386
371 296af7c9 Blue Swirl
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
372 296af7c9 Blue Swirl
#else
373 296af7c9 Blue Swirl
        cpu_dump_state(env, stderr, fprintf, 0);
374 296af7c9 Blue Swirl
#endif
375 296af7c9 Blue Swirl
    }
376 296af7c9 Blue Swirl
    va_end(ap);
377 296af7c9 Blue Swirl
    abort();
378 296af7c9 Blue Swirl
}
379 296af7c9 Blue Swirl
380 296af7c9 Blue Swirl
void cpu_synchronize_all_states(void)
381 296af7c9 Blue Swirl
{
382 9349b4f9 Andreas Färber
    CPUArchState *cpu;
383 296af7c9 Blue Swirl
384 296af7c9 Blue Swirl
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
385 296af7c9 Blue Swirl
        cpu_synchronize_state(cpu);
386 296af7c9 Blue Swirl
    }
387 296af7c9 Blue Swirl
}
388 296af7c9 Blue Swirl
389 296af7c9 Blue Swirl
void cpu_synchronize_all_post_reset(void)
390 296af7c9 Blue Swirl
{
391 9349b4f9 Andreas Färber
    CPUArchState *cpu;
392 296af7c9 Blue Swirl
393 296af7c9 Blue Swirl
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
394 296af7c9 Blue Swirl
        cpu_synchronize_post_reset(cpu);
395 296af7c9 Blue Swirl
    }
396 296af7c9 Blue Swirl
}
397 296af7c9 Blue Swirl
398 296af7c9 Blue Swirl
void cpu_synchronize_all_post_init(void)
399 296af7c9 Blue Swirl
{
400 9349b4f9 Andreas Färber
    CPUArchState *cpu;
401 296af7c9 Blue Swirl
402 296af7c9 Blue Swirl
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
403 296af7c9 Blue Swirl
        cpu_synchronize_post_init(cpu);
404 296af7c9 Blue Swirl
    }
405 296af7c9 Blue Swirl
}
406 296af7c9 Blue Swirl
407 9349b4f9 Andreas Färber
int cpu_is_stopped(CPUArchState *env)
408 3ae9501c Marcelo Tosatti
{
409 1354869c Luiz Capitulino
    return !runstate_is_running() || env->stopped;
410 3ae9501c Marcelo Tosatti
}
411 3ae9501c Marcelo Tosatti
412 1dfb4dd9 Luiz Capitulino
static void do_vm_stop(RunState state)
413 296af7c9 Blue Swirl
{
414 1354869c Luiz Capitulino
    if (runstate_is_running()) {
415 296af7c9 Blue Swirl
        cpu_disable_ticks();
416 296af7c9 Blue Swirl
        pause_all_vcpus();
417 f5bbfba1 Luiz Capitulino
        runstate_set(state);
418 1dfb4dd9 Luiz Capitulino
        vm_state_notify(0, state);
419 922453bc Stefan Hajnoczi
        bdrv_drain_all();
420 55df6f33 Michael S. Tsirkin
        bdrv_flush_all();
421 296af7c9 Blue Swirl
        monitor_protocol_event(QEVENT_STOP, NULL);
422 296af7c9 Blue Swirl
    }
423 296af7c9 Blue Swirl
}
424 296af7c9 Blue Swirl
425 9349b4f9 Andreas Färber
static int cpu_can_run(CPUArchState *env)
426 296af7c9 Blue Swirl
{
427 0ab07c62 Jan Kiszka
    if (env->stop) {
428 296af7c9 Blue Swirl
        return 0;
429 0ab07c62 Jan Kiszka
    }
430 1354869c Luiz Capitulino
    if (env->stopped || !runstate_is_running()) {
431 296af7c9 Blue Swirl
        return 0;
432 0ab07c62 Jan Kiszka
    }
433 296af7c9 Blue Swirl
    return 1;
434 296af7c9 Blue Swirl
}
435 296af7c9 Blue Swirl
436 9349b4f9 Andreas Färber
static bool cpu_thread_is_idle(CPUArchState *env)
437 296af7c9 Blue Swirl
{
438 16400322 Jan Kiszka
    if (env->stop || env->queued_work_first) {
439 16400322 Jan Kiszka
        return false;
440 16400322 Jan Kiszka
    }
441 1354869c Luiz Capitulino
    if (env->stopped || !runstate_is_running()) {
442 16400322 Jan Kiszka
        return true;
443 16400322 Jan Kiszka
    }
444 56b9ead2 Jan Kiszka
    if (!env->halted || qemu_cpu_has_work(env) || kvm_irqchip_in_kernel()) {
445 16400322 Jan Kiszka
        return false;
446 16400322 Jan Kiszka
    }
447 16400322 Jan Kiszka
    return true;
448 296af7c9 Blue Swirl
}
449 296af7c9 Blue Swirl
450 ab33fcda Paolo Bonzini
bool all_cpu_threads_idle(void)
451 296af7c9 Blue Swirl
{
452 9349b4f9 Andreas Färber
    CPUArchState *env;
453 296af7c9 Blue Swirl
454 16400322 Jan Kiszka
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
455 16400322 Jan Kiszka
        if (!cpu_thread_is_idle(env)) {
456 16400322 Jan Kiszka
            return false;
457 16400322 Jan Kiszka
        }
458 16400322 Jan Kiszka
    }
459 16400322 Jan Kiszka
    return true;
460 296af7c9 Blue Swirl
}
461 296af7c9 Blue Swirl
462 9349b4f9 Andreas Färber
static void cpu_handle_guest_debug(CPUArchState *env)
463 83f338f7 Jan Kiszka
{
464 3c638d06 Jan Kiszka
    gdb_set_stop_cpu(env);
465 8cf71710 Jan Kiszka
    qemu_system_debug_request();
466 83f338f7 Jan Kiszka
    env->stopped = 1;
467 3c638d06 Jan Kiszka
}
468 3c638d06 Jan Kiszka
469 714bd040 Paolo Bonzini
static void cpu_signal(int sig)
470 714bd040 Paolo Bonzini
{
471 714bd040 Paolo Bonzini
    if (cpu_single_env) {
472 714bd040 Paolo Bonzini
        cpu_exit(cpu_single_env);
473 714bd040 Paolo Bonzini
    }
474 714bd040 Paolo Bonzini
    exit_request = 1;
475 714bd040 Paolo Bonzini
}
476 714bd040 Paolo Bonzini
477 6d9cb73c Jan Kiszka
#ifdef CONFIG_LINUX
478 6d9cb73c Jan Kiszka
static void sigbus_reraise(void)
479 6d9cb73c Jan Kiszka
{
480 6d9cb73c Jan Kiszka
    sigset_t set;
481 6d9cb73c Jan Kiszka
    struct sigaction action;
482 6d9cb73c Jan Kiszka
483 6d9cb73c Jan Kiszka
    memset(&action, 0, sizeof(action));
484 6d9cb73c Jan Kiszka
    action.sa_handler = SIG_DFL;
485 6d9cb73c Jan Kiszka
    if (!sigaction(SIGBUS, &action, NULL)) {
486 6d9cb73c Jan Kiszka
        raise(SIGBUS);
487 6d9cb73c Jan Kiszka
        sigemptyset(&set);
488 6d9cb73c Jan Kiszka
        sigaddset(&set, SIGBUS);
489 6d9cb73c Jan Kiszka
        sigprocmask(SIG_UNBLOCK, &set, NULL);
490 6d9cb73c Jan Kiszka
    }
491 6d9cb73c Jan Kiszka
    perror("Failed to re-raise SIGBUS!\n");
492 6d9cb73c Jan Kiszka
    abort();
493 6d9cb73c Jan Kiszka
}
494 6d9cb73c Jan Kiszka
495 6d9cb73c Jan Kiszka
static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
496 6d9cb73c Jan Kiszka
                           void *ctx)
497 6d9cb73c Jan Kiszka
{
498 6d9cb73c Jan Kiszka
    if (kvm_on_sigbus(siginfo->ssi_code,
499 6d9cb73c Jan Kiszka
                      (void *)(intptr_t)siginfo->ssi_addr)) {
500 6d9cb73c Jan Kiszka
        sigbus_reraise();
501 6d9cb73c Jan Kiszka
    }
502 6d9cb73c Jan Kiszka
}
503 6d9cb73c Jan Kiszka
504 6d9cb73c Jan Kiszka
static void qemu_init_sigbus(void)
505 6d9cb73c Jan Kiszka
{
506 6d9cb73c Jan Kiszka
    struct sigaction action;
507 6d9cb73c Jan Kiszka
508 6d9cb73c Jan Kiszka
    memset(&action, 0, sizeof(action));
509 6d9cb73c Jan Kiszka
    action.sa_flags = SA_SIGINFO;
510 6d9cb73c Jan Kiszka
    action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
511 6d9cb73c Jan Kiszka
    sigaction(SIGBUS, &action, NULL);
512 6d9cb73c Jan Kiszka
513 6d9cb73c Jan Kiszka
    prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
514 6d9cb73c Jan Kiszka
}
515 6d9cb73c Jan Kiszka
516 9349b4f9 Andreas Färber
static void qemu_kvm_eat_signals(CPUArchState *env)
517 1ab3c6c0 Jan Kiszka
{
518 1ab3c6c0 Jan Kiszka
    struct timespec ts = { 0, 0 };
519 1ab3c6c0 Jan Kiszka
    siginfo_t siginfo;
520 1ab3c6c0 Jan Kiszka
    sigset_t waitset;
521 1ab3c6c0 Jan Kiszka
    sigset_t chkset;
522 1ab3c6c0 Jan Kiszka
    int r;
523 1ab3c6c0 Jan Kiszka
524 1ab3c6c0 Jan Kiszka
    sigemptyset(&waitset);
525 1ab3c6c0 Jan Kiszka
    sigaddset(&waitset, SIG_IPI);
526 1ab3c6c0 Jan Kiszka
    sigaddset(&waitset, SIGBUS);
527 1ab3c6c0 Jan Kiszka
528 1ab3c6c0 Jan Kiszka
    do {
529 1ab3c6c0 Jan Kiszka
        r = sigtimedwait(&waitset, &siginfo, &ts);
530 1ab3c6c0 Jan Kiszka
        if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
531 1ab3c6c0 Jan Kiszka
            perror("sigtimedwait");
532 1ab3c6c0 Jan Kiszka
            exit(1);
533 1ab3c6c0 Jan Kiszka
        }
534 1ab3c6c0 Jan Kiszka
535 1ab3c6c0 Jan Kiszka
        switch (r) {
536 1ab3c6c0 Jan Kiszka
        case SIGBUS:
537 1ab3c6c0 Jan Kiszka
            if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
538 1ab3c6c0 Jan Kiszka
                sigbus_reraise();
539 1ab3c6c0 Jan Kiszka
            }
540 1ab3c6c0 Jan Kiszka
            break;
541 1ab3c6c0 Jan Kiszka
        default:
542 1ab3c6c0 Jan Kiszka
            break;
543 1ab3c6c0 Jan Kiszka
        }
544 1ab3c6c0 Jan Kiszka
545 1ab3c6c0 Jan Kiszka
        r = sigpending(&chkset);
546 1ab3c6c0 Jan Kiszka
        if (r == -1) {
547 1ab3c6c0 Jan Kiszka
            perror("sigpending");
548 1ab3c6c0 Jan Kiszka
            exit(1);
549 1ab3c6c0 Jan Kiszka
        }
550 1ab3c6c0 Jan Kiszka
    } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
551 1ab3c6c0 Jan Kiszka
}
552 1ab3c6c0 Jan Kiszka
553 6d9cb73c Jan Kiszka
#else /* !CONFIG_LINUX */
554 6d9cb73c Jan Kiszka
555 6d9cb73c Jan Kiszka
static void qemu_init_sigbus(void)
556 6d9cb73c Jan Kiszka
{
557 6d9cb73c Jan Kiszka
}
558 1ab3c6c0 Jan Kiszka
559 9349b4f9 Andreas Färber
static void qemu_kvm_eat_signals(CPUArchState *env)
560 1ab3c6c0 Jan Kiszka
{
561 1ab3c6c0 Jan Kiszka
}
562 6d9cb73c Jan Kiszka
#endif /* !CONFIG_LINUX */
563 6d9cb73c Jan Kiszka
564 296af7c9 Blue Swirl
#ifndef _WIN32
565 55f8d6ac Jan Kiszka
static void dummy_signal(int sig)
566 55f8d6ac Jan Kiszka
{
567 55f8d6ac Jan Kiszka
}
568 55f8d6ac Jan Kiszka
569 9349b4f9 Andreas Färber
static void qemu_kvm_init_cpu_signals(CPUArchState *env)
570 714bd040 Paolo Bonzini
{
571 714bd040 Paolo Bonzini
    int r;
572 714bd040 Paolo Bonzini
    sigset_t set;
573 714bd040 Paolo Bonzini
    struct sigaction sigact;
574 714bd040 Paolo Bonzini
575 714bd040 Paolo Bonzini
    memset(&sigact, 0, sizeof(sigact));
576 714bd040 Paolo Bonzini
    sigact.sa_handler = dummy_signal;
577 714bd040 Paolo Bonzini
    sigaction(SIG_IPI, &sigact, NULL);
578 714bd040 Paolo Bonzini
579 714bd040 Paolo Bonzini
    pthread_sigmask(SIG_BLOCK, NULL, &set);
580 714bd040 Paolo Bonzini
    sigdelset(&set, SIG_IPI);
581 714bd040 Paolo Bonzini
    sigdelset(&set, SIGBUS);
582 714bd040 Paolo Bonzini
    r = kvm_set_signal_mask(env, &set);
583 714bd040 Paolo Bonzini
    if (r) {
584 714bd040 Paolo Bonzini
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
585 714bd040 Paolo Bonzini
        exit(1);
586 714bd040 Paolo Bonzini
    }
587 714bd040 Paolo Bonzini
}
588 714bd040 Paolo Bonzini
589 714bd040 Paolo Bonzini
static void qemu_tcg_init_cpu_signals(void)
590 714bd040 Paolo Bonzini
{
591 714bd040 Paolo Bonzini
    sigset_t set;
592 714bd040 Paolo Bonzini
    struct sigaction sigact;
593 714bd040 Paolo Bonzini
594 714bd040 Paolo Bonzini
    memset(&sigact, 0, sizeof(sigact));
595 714bd040 Paolo Bonzini
    sigact.sa_handler = cpu_signal;
596 714bd040 Paolo Bonzini
    sigaction(SIG_IPI, &sigact, NULL);
597 714bd040 Paolo Bonzini
598 714bd040 Paolo Bonzini
    sigemptyset(&set);
599 714bd040 Paolo Bonzini
    sigaddset(&set, SIG_IPI);
600 714bd040 Paolo Bonzini
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
601 714bd040 Paolo Bonzini
}
602 714bd040 Paolo Bonzini
603 55f8d6ac Jan Kiszka
#else /* _WIN32 */
604 9349b4f9 Andreas Färber
static void qemu_kvm_init_cpu_signals(CPUArchState *env)
605 ff48eb5f Jan Kiszka
{
606 714bd040 Paolo Bonzini
    abort();
607 714bd040 Paolo Bonzini
}
608 ff48eb5f Jan Kiszka
609 714bd040 Paolo Bonzini
static void qemu_tcg_init_cpu_signals(void)
610 714bd040 Paolo Bonzini
{
611 ff48eb5f Jan Kiszka
}
612 714bd040 Paolo Bonzini
#endif /* _WIN32 */
613 ff48eb5f Jan Kiszka
614 296af7c9 Blue Swirl
QemuMutex qemu_global_mutex;
615 46daff13 Paolo Bonzini
static QemuCond qemu_io_proceeded_cond;
616 46daff13 Paolo Bonzini
static bool iothread_requesting_mutex;
617 296af7c9 Blue Swirl
618 296af7c9 Blue Swirl
static QemuThread io_thread;
619 296af7c9 Blue Swirl
620 296af7c9 Blue Swirl
static QemuThread *tcg_cpu_thread;
621 296af7c9 Blue Swirl
static QemuCond *tcg_halt_cond;
622 296af7c9 Blue Swirl
623 296af7c9 Blue Swirl
/* cpu creation */
624 296af7c9 Blue Swirl
static QemuCond qemu_cpu_cond;
625 296af7c9 Blue Swirl
/* system init */
626 296af7c9 Blue Swirl
static QemuCond qemu_pause_cond;
627 e82bcec2 Marcelo Tosatti
static QemuCond qemu_work_cond;
628 296af7c9 Blue Swirl
629 d3b12f5d Paolo Bonzini
void qemu_init_cpu_loop(void)
630 296af7c9 Blue Swirl
{
631 6d9cb73c Jan Kiszka
    qemu_init_sigbus();
632 ed94592b Anthony Liguori
    qemu_cond_init(&qemu_cpu_cond);
633 ed94592b Anthony Liguori
    qemu_cond_init(&qemu_pause_cond);
634 ed94592b Anthony Liguori
    qemu_cond_init(&qemu_work_cond);
635 46daff13 Paolo Bonzini
    qemu_cond_init(&qemu_io_proceeded_cond);
636 296af7c9 Blue Swirl
    qemu_mutex_init(&qemu_global_mutex);
637 296af7c9 Blue Swirl
638 b7680cb6 Jan Kiszka
    qemu_thread_get_self(&io_thread);
639 296af7c9 Blue Swirl
}
640 296af7c9 Blue Swirl
641 9349b4f9 Andreas Färber
void run_on_cpu(CPUArchState *env, void (*func)(void *data), void *data)
642 e82bcec2 Marcelo Tosatti
{
643 e82bcec2 Marcelo Tosatti
    struct qemu_work_item wi;
644 e82bcec2 Marcelo Tosatti
645 b7680cb6 Jan Kiszka
    if (qemu_cpu_is_self(env)) {
646 e82bcec2 Marcelo Tosatti
        func(data);
647 e82bcec2 Marcelo Tosatti
        return;
648 e82bcec2 Marcelo Tosatti
    }
649 e82bcec2 Marcelo Tosatti
650 e82bcec2 Marcelo Tosatti
    wi.func = func;
651 e82bcec2 Marcelo Tosatti
    wi.data = data;
652 0ab07c62 Jan Kiszka
    if (!env->queued_work_first) {
653 e82bcec2 Marcelo Tosatti
        env->queued_work_first = &wi;
654 0ab07c62 Jan Kiszka
    } else {
655 e82bcec2 Marcelo Tosatti
        env->queued_work_last->next = &wi;
656 0ab07c62 Jan Kiszka
    }
657 e82bcec2 Marcelo Tosatti
    env->queued_work_last = &wi;
658 e82bcec2 Marcelo Tosatti
    wi.next = NULL;
659 e82bcec2 Marcelo Tosatti
    wi.done = false;
660 e82bcec2 Marcelo Tosatti
661 e82bcec2 Marcelo Tosatti
    qemu_cpu_kick(env);
662 e82bcec2 Marcelo Tosatti
    while (!wi.done) {
663 9349b4f9 Andreas Färber
        CPUArchState *self_env = cpu_single_env;
664 e82bcec2 Marcelo Tosatti
665 e82bcec2 Marcelo Tosatti
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
666 e82bcec2 Marcelo Tosatti
        cpu_single_env = self_env;
667 e82bcec2 Marcelo Tosatti
    }
668 e82bcec2 Marcelo Tosatti
}
669 e82bcec2 Marcelo Tosatti
670 9349b4f9 Andreas Färber
static void flush_queued_work(CPUArchState *env)
671 e82bcec2 Marcelo Tosatti
{
672 e82bcec2 Marcelo Tosatti
    struct qemu_work_item *wi;
673 e82bcec2 Marcelo Tosatti
674 0ab07c62 Jan Kiszka
    if (!env->queued_work_first) {
675 e82bcec2 Marcelo Tosatti
        return;
676 0ab07c62 Jan Kiszka
    }
677 e82bcec2 Marcelo Tosatti
678 e82bcec2 Marcelo Tosatti
    while ((wi = env->queued_work_first)) {
679 e82bcec2 Marcelo Tosatti
        env->queued_work_first = wi->next;
680 e82bcec2 Marcelo Tosatti
        wi->func(wi->data);
681 e82bcec2 Marcelo Tosatti
        wi->done = true;
682 e82bcec2 Marcelo Tosatti
    }
683 e82bcec2 Marcelo Tosatti
    env->queued_work_last = NULL;
684 e82bcec2 Marcelo Tosatti
    qemu_cond_broadcast(&qemu_work_cond);
685 e82bcec2 Marcelo Tosatti
}
686 e82bcec2 Marcelo Tosatti
687 9349b4f9 Andreas Färber
static void qemu_wait_io_event_common(CPUArchState *env)
688 296af7c9 Blue Swirl
{
689 296af7c9 Blue Swirl
    if (env->stop) {
690 296af7c9 Blue Swirl
        env->stop = 0;
691 296af7c9 Blue Swirl
        env->stopped = 1;
692 296af7c9 Blue Swirl
        qemu_cond_signal(&qemu_pause_cond);
693 296af7c9 Blue Swirl
    }
694 e82bcec2 Marcelo Tosatti
    flush_queued_work(env);
695 aa2c364b Jan Kiszka
    env->thread_kicked = false;
696 296af7c9 Blue Swirl
}
697 296af7c9 Blue Swirl
698 6cabe1f3 Jan Kiszka
static void qemu_tcg_wait_io_event(void)
699 296af7c9 Blue Swirl
{
700 9349b4f9 Andreas Färber
    CPUArchState *env;
701 6cabe1f3 Jan Kiszka
702 16400322 Jan Kiszka
    while (all_cpu_threads_idle()) {
703 ab33fcda Paolo Bonzini
       /* Start accounting real time to the virtual clock if the CPUs
704 ab33fcda Paolo Bonzini
          are idle.  */
705 ab33fcda Paolo Bonzini
        qemu_clock_warp(vm_clock);
706 9705fbb5 Paolo Bonzini
        qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
707 16400322 Jan Kiszka
    }
708 296af7c9 Blue Swirl
709 46daff13 Paolo Bonzini
    while (iothread_requesting_mutex) {
710 46daff13 Paolo Bonzini
        qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
711 46daff13 Paolo Bonzini
    }
712 6cabe1f3 Jan Kiszka
713 6cabe1f3 Jan Kiszka
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
714 6cabe1f3 Jan Kiszka
        qemu_wait_io_event_common(env);
715 6cabe1f3 Jan Kiszka
    }
716 296af7c9 Blue Swirl
}
717 296af7c9 Blue Swirl
718 9349b4f9 Andreas Färber
static void qemu_kvm_wait_io_event(CPUArchState *env)
719 296af7c9 Blue Swirl
{
720 16400322 Jan Kiszka
    while (cpu_thread_is_idle(env)) {
721 9705fbb5 Paolo Bonzini
        qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
722 16400322 Jan Kiszka
    }
723 296af7c9 Blue Swirl
724 5db5bdac Jan Kiszka
    qemu_kvm_eat_signals(env);
725 296af7c9 Blue Swirl
    qemu_wait_io_event_common(env);
726 296af7c9 Blue Swirl
}
727 296af7c9 Blue Swirl
728 7e97cd88 Jan Kiszka
static void *qemu_kvm_cpu_thread_fn(void *arg)
729 296af7c9 Blue Swirl
{
730 9349b4f9 Andreas Färber
    CPUArchState *env = arg;
731 84b4915d Jan Kiszka
    int r;
732 296af7c9 Blue Swirl
733 6164e6d6 Marcelo Tosatti
    qemu_mutex_lock(&qemu_global_mutex);
734 b7680cb6 Jan Kiszka
    qemu_thread_get_self(env->thread);
735 dc7a09cf Jan Kiszka
    env->thread_id = qemu_get_thread_id();
736 e479c207 Jan Kiszka
    cpu_single_env = env;
737 296af7c9 Blue Swirl
738 84b4915d Jan Kiszka
    r = kvm_init_vcpu(env);
739 84b4915d Jan Kiszka
    if (r < 0) {
740 84b4915d Jan Kiszka
        fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
741 84b4915d Jan Kiszka
        exit(1);
742 84b4915d Jan Kiszka
    }
743 296af7c9 Blue Swirl
744 55f8d6ac Jan Kiszka
    qemu_kvm_init_cpu_signals(env);
745 296af7c9 Blue Swirl
746 296af7c9 Blue Swirl
    /* signal CPU creation */
747 296af7c9 Blue Swirl
    env->created = 1;
748 296af7c9 Blue Swirl
    qemu_cond_signal(&qemu_cpu_cond);
749 296af7c9 Blue Swirl
750 296af7c9 Blue Swirl
    while (1) {
751 0ab07c62 Jan Kiszka
        if (cpu_can_run(env)) {
752 6792a57b Jan Kiszka
            r = kvm_cpu_exec(env);
753 83f338f7 Jan Kiszka
            if (r == EXCP_DEBUG) {
754 1009d2ed Jan Kiszka
                cpu_handle_guest_debug(env);
755 83f338f7 Jan Kiszka
            }
756 0ab07c62 Jan Kiszka
        }
757 296af7c9 Blue Swirl
        qemu_kvm_wait_io_event(env);
758 296af7c9 Blue Swirl
    }
759 296af7c9 Blue Swirl
760 296af7c9 Blue Swirl
    return NULL;
761 296af7c9 Blue Swirl
}
762 296af7c9 Blue Swirl
763 c7f0f3b1 Anthony Liguori
static void *qemu_dummy_cpu_thread_fn(void *arg)
764 c7f0f3b1 Anthony Liguori
{
765 c7f0f3b1 Anthony Liguori
#ifdef _WIN32
766 c7f0f3b1 Anthony Liguori
    fprintf(stderr, "qtest is not supported under Windows\n");
767 c7f0f3b1 Anthony Liguori
    exit(1);
768 c7f0f3b1 Anthony Liguori
#else
769 c7f0f3b1 Anthony Liguori
    CPUArchState *env = arg;
770 c7f0f3b1 Anthony Liguori
    sigset_t waitset;
771 c7f0f3b1 Anthony Liguori
    int r;
772 c7f0f3b1 Anthony Liguori
773 c7f0f3b1 Anthony Liguori
    qemu_mutex_lock_iothread();
774 c7f0f3b1 Anthony Liguori
    qemu_thread_get_self(env->thread);
775 c7f0f3b1 Anthony Liguori
    env->thread_id = qemu_get_thread_id();
776 c7f0f3b1 Anthony Liguori
777 c7f0f3b1 Anthony Liguori
    sigemptyset(&waitset);
778 c7f0f3b1 Anthony Liguori
    sigaddset(&waitset, SIG_IPI);
779 c7f0f3b1 Anthony Liguori
780 c7f0f3b1 Anthony Liguori
    /* signal CPU creation */
781 c7f0f3b1 Anthony Liguori
    env->created = 1;
782 c7f0f3b1 Anthony Liguori
    qemu_cond_signal(&qemu_cpu_cond);
783 c7f0f3b1 Anthony Liguori
784 c7f0f3b1 Anthony Liguori
    cpu_single_env = env;
785 c7f0f3b1 Anthony Liguori
    while (1) {
786 c7f0f3b1 Anthony Liguori
        cpu_single_env = NULL;
787 c7f0f3b1 Anthony Liguori
        qemu_mutex_unlock_iothread();
788 c7f0f3b1 Anthony Liguori
        do {
789 c7f0f3b1 Anthony Liguori
            int sig;
790 c7f0f3b1 Anthony Liguori
            r = sigwait(&waitset, &sig);
791 c7f0f3b1 Anthony Liguori
        } while (r == -1 && (errno == EAGAIN || errno == EINTR));
792 c7f0f3b1 Anthony Liguori
        if (r == -1) {
793 c7f0f3b1 Anthony Liguori
            perror("sigwait");
794 c7f0f3b1 Anthony Liguori
            exit(1);
795 c7f0f3b1 Anthony Liguori
        }
796 c7f0f3b1 Anthony Liguori
        qemu_mutex_lock_iothread();
797 c7f0f3b1 Anthony Liguori
        cpu_single_env = env;
798 c7f0f3b1 Anthony Liguori
        qemu_wait_io_event_common(env);
799 c7f0f3b1 Anthony Liguori
    }
800 c7f0f3b1 Anthony Liguori
801 c7f0f3b1 Anthony Liguori
    return NULL;
802 c7f0f3b1 Anthony Liguori
#endif
803 c7f0f3b1 Anthony Liguori
}
804 c7f0f3b1 Anthony Liguori
805 bdb7ca67 Jan Kiszka
static void tcg_exec_all(void);
806 bdb7ca67 Jan Kiszka
807 7e97cd88 Jan Kiszka
static void *qemu_tcg_cpu_thread_fn(void *arg)
808 296af7c9 Blue Swirl
{
809 9349b4f9 Andreas Färber
    CPUArchState *env = arg;
810 296af7c9 Blue Swirl
811 55f8d6ac Jan Kiszka
    qemu_tcg_init_cpu_signals();
812 b7680cb6 Jan Kiszka
    qemu_thread_get_self(env->thread);
813 296af7c9 Blue Swirl
814 296af7c9 Blue Swirl
    /* signal CPU creation */
815 296af7c9 Blue Swirl
    qemu_mutex_lock(&qemu_global_mutex);
816 0ab07c62 Jan Kiszka
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
817 dc7a09cf Jan Kiszka
        env->thread_id = qemu_get_thread_id();
818 296af7c9 Blue Swirl
        env->created = 1;
819 0ab07c62 Jan Kiszka
    }
820 296af7c9 Blue Swirl
    qemu_cond_signal(&qemu_cpu_cond);
821 296af7c9 Blue Swirl
822 fa7d1867 Jan Kiszka
    /* wait for initial kick-off after machine start */
823 fa7d1867 Jan Kiszka
    while (first_cpu->stopped) {
824 fa7d1867 Jan Kiszka
        qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
825 8e564b4e Jan Kiszka
826 8e564b4e Jan Kiszka
        /* process any pending work */
827 8e564b4e Jan Kiszka
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
828 8e564b4e Jan Kiszka
            qemu_wait_io_event_common(env);
829 8e564b4e Jan Kiszka
        }
830 0ab07c62 Jan Kiszka
    }
831 296af7c9 Blue Swirl
832 296af7c9 Blue Swirl
    while (1) {
833 bdb7ca67 Jan Kiszka
        tcg_exec_all();
834 946fb27c Paolo Bonzini
        if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
835 3b2319a3 Paolo Bonzini
            qemu_notify_event();
836 3b2319a3 Paolo Bonzini
        }
837 6cabe1f3 Jan Kiszka
        qemu_tcg_wait_io_event();
838 296af7c9 Blue Swirl
    }
839 296af7c9 Blue Swirl
840 296af7c9 Blue Swirl
    return NULL;
841 296af7c9 Blue Swirl
}
842 296af7c9 Blue Swirl
843 9349b4f9 Andreas Färber
static void qemu_cpu_kick_thread(CPUArchState *env)
844 cc015e9a Paolo Bonzini
{
845 cc015e9a Paolo Bonzini
#ifndef _WIN32
846 cc015e9a Paolo Bonzini
    int err;
847 cc015e9a Paolo Bonzini
848 cc015e9a Paolo Bonzini
    err = pthread_kill(env->thread->thread, SIG_IPI);
849 cc015e9a Paolo Bonzini
    if (err) {
850 cc015e9a Paolo Bonzini
        fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
851 cc015e9a Paolo Bonzini
        exit(1);
852 cc015e9a Paolo Bonzini
    }
853 cc015e9a Paolo Bonzini
#else /* _WIN32 */
854 cc015e9a Paolo Bonzini
    if (!qemu_cpu_is_self(env)) {
855 1ecf47bf Paolo Bonzini
        SuspendThread(env->hThread);
856 cc015e9a Paolo Bonzini
        cpu_signal(0);
857 1ecf47bf Paolo Bonzini
        ResumeThread(env->hThread);
858 cc015e9a Paolo Bonzini
    }
859 cc015e9a Paolo Bonzini
#endif
860 cc015e9a Paolo Bonzini
}
861 cc015e9a Paolo Bonzini
862 296af7c9 Blue Swirl
void qemu_cpu_kick(void *_env)
863 296af7c9 Blue Swirl
{
864 9349b4f9 Andreas Färber
    CPUArchState *env = _env;
865 296af7c9 Blue Swirl
866 296af7c9 Blue Swirl
    qemu_cond_broadcast(env->halt_cond);
867 c7f0f3b1 Anthony Liguori
    if (!tcg_enabled() && !env->thread_kicked) {
868 cc015e9a Paolo Bonzini
        qemu_cpu_kick_thread(env);
869 aa2c364b Jan Kiszka
        env->thread_kicked = true;
870 aa2c364b Jan Kiszka
    }
871 296af7c9 Blue Swirl
}
872 296af7c9 Blue Swirl
873 46d62fac Jan Kiszka
void qemu_cpu_kick_self(void)
874 296af7c9 Blue Swirl
{
875 b55c22c6 Paolo Bonzini
#ifndef _WIN32
876 46d62fac Jan Kiszka
    assert(cpu_single_env);
877 296af7c9 Blue Swirl
878 46d62fac Jan Kiszka
    if (!cpu_single_env->thread_kicked) {
879 cc015e9a Paolo Bonzini
        qemu_cpu_kick_thread(cpu_single_env);
880 46d62fac Jan Kiszka
        cpu_single_env->thread_kicked = true;
881 296af7c9 Blue Swirl
    }
882 b55c22c6 Paolo Bonzini
#else
883 b55c22c6 Paolo Bonzini
    abort();
884 b55c22c6 Paolo Bonzini
#endif
885 296af7c9 Blue Swirl
}
886 296af7c9 Blue Swirl
887 b7680cb6 Jan Kiszka
int qemu_cpu_is_self(void *_env)
888 296af7c9 Blue Swirl
{
889 9349b4f9 Andreas Färber
    CPUArchState *env = _env;
890 a8486bc9 Marcelo Tosatti
891 b7680cb6 Jan Kiszka
    return qemu_thread_is_self(env->thread);
892 296af7c9 Blue Swirl
}
893 296af7c9 Blue Swirl
894 296af7c9 Blue Swirl
void qemu_mutex_lock_iothread(void)
895 296af7c9 Blue Swirl
{
896 c7f0f3b1 Anthony Liguori
    if (!tcg_enabled()) {
897 296af7c9 Blue Swirl
        qemu_mutex_lock(&qemu_global_mutex);
898 1a28cac3 Marcelo Tosatti
    } else {
899 46daff13 Paolo Bonzini
        iothread_requesting_mutex = true;
900 1a28cac3 Marcelo Tosatti
        if (qemu_mutex_trylock(&qemu_global_mutex)) {
901 cc015e9a Paolo Bonzini
            qemu_cpu_kick_thread(first_cpu);
902 1a28cac3 Marcelo Tosatti
            qemu_mutex_lock(&qemu_global_mutex);
903 1a28cac3 Marcelo Tosatti
        }
904 46daff13 Paolo Bonzini
        iothread_requesting_mutex = false;
905 46daff13 Paolo Bonzini
        qemu_cond_broadcast(&qemu_io_proceeded_cond);
906 1a28cac3 Marcelo Tosatti
    }
907 296af7c9 Blue Swirl
}
908 296af7c9 Blue Swirl
909 296af7c9 Blue Swirl
void qemu_mutex_unlock_iothread(void)
910 296af7c9 Blue Swirl
{
911 296af7c9 Blue Swirl
    qemu_mutex_unlock(&qemu_global_mutex);
912 296af7c9 Blue Swirl
}
913 296af7c9 Blue Swirl
914 296af7c9 Blue Swirl
static int all_vcpus_paused(void)
915 296af7c9 Blue Swirl
{
916 9349b4f9 Andreas Färber
    CPUArchState *penv = first_cpu;
917 296af7c9 Blue Swirl
918 296af7c9 Blue Swirl
    while (penv) {
919 0ab07c62 Jan Kiszka
        if (!penv->stopped) {
920 296af7c9 Blue Swirl
            return 0;
921 0ab07c62 Jan Kiszka
        }
922 5207a5e0 Jan Kiszka
        penv = penv->next_cpu;
923 296af7c9 Blue Swirl
    }
924 296af7c9 Blue Swirl
925 296af7c9 Blue Swirl
    return 1;
926 296af7c9 Blue Swirl
}
927 296af7c9 Blue Swirl
928 296af7c9 Blue Swirl
void pause_all_vcpus(void)
929 296af7c9 Blue Swirl
{
930 9349b4f9 Andreas Färber
    CPUArchState *penv = first_cpu;
931 296af7c9 Blue Swirl
932 a5c57d64 Paolo Bonzini
    qemu_clock_enable(vm_clock, false);
933 296af7c9 Blue Swirl
    while (penv) {
934 296af7c9 Blue Swirl
        penv->stop = 1;
935 296af7c9 Blue Swirl
        qemu_cpu_kick(penv);
936 5207a5e0 Jan Kiszka
        penv = penv->next_cpu;
937 296af7c9 Blue Swirl
    }
938 296af7c9 Blue Swirl
939 d798e974 Jan Kiszka
    if (!qemu_thread_is_self(&io_thread)) {
940 d798e974 Jan Kiszka
        cpu_stop_current();
941 d798e974 Jan Kiszka
        if (!kvm_enabled()) {
942 d798e974 Jan Kiszka
            while (penv) {
943 d798e974 Jan Kiszka
                penv->stop = 0;
944 d798e974 Jan Kiszka
                penv->stopped = 1;
945 d798e974 Jan Kiszka
                penv = penv->next_cpu;
946 d798e974 Jan Kiszka
            }
947 d798e974 Jan Kiszka
            return;
948 d798e974 Jan Kiszka
        }
949 d798e974 Jan Kiszka
    }
950 d798e974 Jan Kiszka
951 296af7c9 Blue Swirl
    while (!all_vcpus_paused()) {
952 be7d6c57 Paolo Bonzini
        qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
953 296af7c9 Blue Swirl
        penv = first_cpu;
954 296af7c9 Blue Swirl
        while (penv) {
955 1fbb22e5 Marcelo Tosatti
            qemu_cpu_kick(penv);
956 5207a5e0 Jan Kiszka
            penv = penv->next_cpu;
957 296af7c9 Blue Swirl
        }
958 296af7c9 Blue Swirl
    }
959 296af7c9 Blue Swirl
}
960 296af7c9 Blue Swirl
961 296af7c9 Blue Swirl
void resume_all_vcpus(void)
962 296af7c9 Blue Swirl
{
963 9349b4f9 Andreas Färber
    CPUArchState *penv = first_cpu;
964 296af7c9 Blue Swirl
965 47113ab6 Wen Congyang
    qemu_clock_enable(vm_clock, true);
966 296af7c9 Blue Swirl
    while (penv) {
967 296af7c9 Blue Swirl
        penv->stop = 0;
968 296af7c9 Blue Swirl
        penv->stopped = 0;
969 296af7c9 Blue Swirl
        qemu_cpu_kick(penv);
970 5207a5e0 Jan Kiszka
        penv = penv->next_cpu;
971 296af7c9 Blue Swirl
    }
972 296af7c9 Blue Swirl
}
973 296af7c9 Blue Swirl
974 7e97cd88 Jan Kiszka
static void qemu_tcg_init_vcpu(void *_env)
975 296af7c9 Blue Swirl
{
976 9349b4f9 Andreas Färber
    CPUArchState *env = _env;
977 0ab07c62 Jan Kiszka
978 296af7c9 Blue Swirl
    /* share a single thread for all cpus with TCG */
979 296af7c9 Blue Swirl
    if (!tcg_cpu_thread) {
980 7267c094 Anthony Liguori
        env->thread = g_malloc0(sizeof(QemuThread));
981 7267c094 Anthony Liguori
        env->halt_cond = g_malloc0(sizeof(QemuCond));
982 296af7c9 Blue Swirl
        qemu_cond_init(env->halt_cond);
983 fa7d1867 Jan Kiszka
        tcg_halt_cond = env->halt_cond;
984 cf218714 Jan Kiszka
        qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env,
985 1ecf47bf Paolo Bonzini
                           QEMU_THREAD_JOINABLE);
986 1ecf47bf Paolo Bonzini
#ifdef _WIN32
987 1ecf47bf Paolo Bonzini
        env->hThread = qemu_thread_get_handle(env->thread);
988 1ecf47bf Paolo Bonzini
#endif
989 0ab07c62 Jan Kiszka
        while (env->created == 0) {
990 18a85728 Paolo Bonzini
            qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
991 0ab07c62 Jan Kiszka
        }
992 296af7c9 Blue Swirl
        tcg_cpu_thread = env->thread;
993 296af7c9 Blue Swirl
    } else {
994 296af7c9 Blue Swirl
        env->thread = tcg_cpu_thread;
995 296af7c9 Blue Swirl
        env->halt_cond = tcg_halt_cond;
996 296af7c9 Blue Swirl
    }
997 296af7c9 Blue Swirl
}
998 296af7c9 Blue Swirl
999 9349b4f9 Andreas Färber
static void qemu_kvm_start_vcpu(CPUArchState *env)
1000 296af7c9 Blue Swirl
{
1001 7267c094 Anthony Liguori
    env->thread = g_malloc0(sizeof(QemuThread));
1002 7267c094 Anthony Liguori
    env->halt_cond = g_malloc0(sizeof(QemuCond));
1003 296af7c9 Blue Swirl
    qemu_cond_init(env->halt_cond);
1004 cf218714 Jan Kiszka
    qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env,
1005 1ecf47bf Paolo Bonzini
                       QEMU_THREAD_JOINABLE);
1006 0ab07c62 Jan Kiszka
    while (env->created == 0) {
1007 18a85728 Paolo Bonzini
        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1008 0ab07c62 Jan Kiszka
    }
1009 296af7c9 Blue Swirl
}
1010 296af7c9 Blue Swirl
1011 c7f0f3b1 Anthony Liguori
static void qemu_dummy_start_vcpu(CPUArchState *env)
1012 c7f0f3b1 Anthony Liguori
{
1013 c7f0f3b1 Anthony Liguori
    env->thread = g_malloc0(sizeof(QemuThread));
1014 c7f0f3b1 Anthony Liguori
    env->halt_cond = g_malloc0(sizeof(QemuCond));
1015 c7f0f3b1 Anthony Liguori
    qemu_cond_init(env->halt_cond);
1016 c7f0f3b1 Anthony Liguori
    qemu_thread_create(env->thread, qemu_dummy_cpu_thread_fn, env,
1017 c7f0f3b1 Anthony Liguori
                       QEMU_THREAD_JOINABLE);
1018 c7f0f3b1 Anthony Liguori
    while (env->created == 0) {
1019 c7f0f3b1 Anthony Liguori
        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1020 c7f0f3b1 Anthony Liguori
    }
1021 c7f0f3b1 Anthony Liguori
}
1022 c7f0f3b1 Anthony Liguori
1023 296af7c9 Blue Swirl
void qemu_init_vcpu(void *_env)
1024 296af7c9 Blue Swirl
{
1025 9349b4f9 Andreas Färber
    CPUArchState *env = _env;
1026 296af7c9 Blue Swirl
1027 296af7c9 Blue Swirl
    env->nr_cores = smp_cores;
1028 296af7c9 Blue Swirl
    env->nr_threads = smp_threads;
1029 fa7d1867 Jan Kiszka
    env->stopped = 1;
1030 0ab07c62 Jan Kiszka
    if (kvm_enabled()) {
1031 7e97cd88 Jan Kiszka
        qemu_kvm_start_vcpu(env);
1032 c7f0f3b1 Anthony Liguori
    } else if (tcg_enabled()) {
1033 7e97cd88 Jan Kiszka
        qemu_tcg_init_vcpu(env);
1034 c7f0f3b1 Anthony Liguori
    } else {
1035 c7f0f3b1 Anthony Liguori
        qemu_dummy_start_vcpu(env);
1036 0ab07c62 Jan Kiszka
    }
1037 296af7c9 Blue Swirl
}
1038 296af7c9 Blue Swirl
1039 b4a3d965 Jan Kiszka
void cpu_stop_current(void)
1040 296af7c9 Blue Swirl
{
1041 b4a3d965 Jan Kiszka
    if (cpu_single_env) {
1042 67bb172f Paolo Bonzini
        cpu_single_env->stop = 0;
1043 b4a3d965 Jan Kiszka
        cpu_single_env->stopped = 1;
1044 b4a3d965 Jan Kiszka
        cpu_exit(cpu_single_env);
1045 67bb172f Paolo Bonzini
        qemu_cond_signal(&qemu_pause_cond);
1046 b4a3d965 Jan Kiszka
    }
1047 296af7c9 Blue Swirl
}
1048 296af7c9 Blue Swirl
1049 1dfb4dd9 Luiz Capitulino
void vm_stop(RunState state)
1050 296af7c9 Blue Swirl
{
1051 b7680cb6 Jan Kiszka
    if (!qemu_thread_is_self(&io_thread)) {
1052 1dfb4dd9 Luiz Capitulino
        qemu_system_vmstop_request(state);
1053 296af7c9 Blue Swirl
        /*
1054 296af7c9 Blue Swirl
         * FIXME: should not return to device code in case
1055 296af7c9 Blue Swirl
         * vm_stop() has been requested.
1056 296af7c9 Blue Swirl
         */
1057 b4a3d965 Jan Kiszka
        cpu_stop_current();
1058 296af7c9 Blue Swirl
        return;
1059 296af7c9 Blue Swirl
    }
1060 1dfb4dd9 Luiz Capitulino
    do_vm_stop(state);
1061 296af7c9 Blue Swirl
}
1062 296af7c9 Blue Swirl
1063 8a9236f1 Luiz Capitulino
/* does a state transition even if the VM is already stopped,
1064 8a9236f1 Luiz Capitulino
   current state is forgotten forever */
1065 8a9236f1 Luiz Capitulino
void vm_stop_force_state(RunState state)
1066 8a9236f1 Luiz Capitulino
{
1067 8a9236f1 Luiz Capitulino
    if (runstate_is_running()) {
1068 8a9236f1 Luiz Capitulino
        vm_stop(state);
1069 8a9236f1 Luiz Capitulino
    } else {
1070 8a9236f1 Luiz Capitulino
        runstate_set(state);
1071 8a9236f1 Luiz Capitulino
    }
1072 8a9236f1 Luiz Capitulino
}
1073 8a9236f1 Luiz Capitulino
1074 9349b4f9 Andreas Färber
static int tcg_cpu_exec(CPUArchState *env)
1075 296af7c9 Blue Swirl
{
1076 296af7c9 Blue Swirl
    int ret;
1077 296af7c9 Blue Swirl
#ifdef CONFIG_PROFILER
1078 296af7c9 Blue Swirl
    int64_t ti;
1079 296af7c9 Blue Swirl
#endif
1080 296af7c9 Blue Swirl
1081 296af7c9 Blue Swirl
#ifdef CONFIG_PROFILER
1082 296af7c9 Blue Swirl
    ti = profile_getclock();
1083 296af7c9 Blue Swirl
#endif
1084 296af7c9 Blue Swirl
    if (use_icount) {
1085 296af7c9 Blue Swirl
        int64_t count;
1086 296af7c9 Blue Swirl
        int decr;
1087 296af7c9 Blue Swirl
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1088 296af7c9 Blue Swirl
        env->icount_decr.u16.low = 0;
1089 296af7c9 Blue Swirl
        env->icount_extra = 0;
1090 946fb27c Paolo Bonzini
        count = qemu_icount_round(qemu_clock_deadline(vm_clock));
1091 296af7c9 Blue Swirl
        qemu_icount += count;
1092 296af7c9 Blue Swirl
        decr = (count > 0xffff) ? 0xffff : count;
1093 296af7c9 Blue Swirl
        count -= decr;
1094 296af7c9 Blue Swirl
        env->icount_decr.u16.low = decr;
1095 296af7c9 Blue Swirl
        env->icount_extra = count;
1096 296af7c9 Blue Swirl
    }
1097 296af7c9 Blue Swirl
    ret = cpu_exec(env);
1098 296af7c9 Blue Swirl
#ifdef CONFIG_PROFILER
1099 296af7c9 Blue Swirl
    qemu_time += profile_getclock() - ti;
1100 296af7c9 Blue Swirl
#endif
1101 296af7c9 Blue Swirl
    if (use_icount) {
1102 296af7c9 Blue Swirl
        /* Fold pending instructions back into the
1103 296af7c9 Blue Swirl
           instruction counter, and clear the interrupt flag.  */
1104 296af7c9 Blue Swirl
        qemu_icount -= (env->icount_decr.u16.low
1105 296af7c9 Blue Swirl
                        + env->icount_extra);
1106 296af7c9 Blue Swirl
        env->icount_decr.u32 = 0;
1107 296af7c9 Blue Swirl
        env->icount_extra = 0;
1108 296af7c9 Blue Swirl
    }
1109 296af7c9 Blue Swirl
    return ret;
1110 296af7c9 Blue Swirl
}
1111 296af7c9 Blue Swirl
1112 bdb7ca67 Jan Kiszka
static void tcg_exec_all(void)
1113 296af7c9 Blue Swirl
{
1114 9a36085b Jan Kiszka
    int r;
1115 9a36085b Jan Kiszka
1116 ab33fcda Paolo Bonzini
    /* Account partial waits to the vm_clock.  */
1117 ab33fcda Paolo Bonzini
    qemu_clock_warp(vm_clock);
1118 ab33fcda Paolo Bonzini
1119 0ab07c62 Jan Kiszka
    if (next_cpu == NULL) {
1120 296af7c9 Blue Swirl
        next_cpu = first_cpu;
1121 0ab07c62 Jan Kiszka
    }
1122 c629a4bc Jan Kiszka
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1123 9349b4f9 Andreas Färber
        CPUArchState *env = next_cpu;
1124 296af7c9 Blue Swirl
1125 296af7c9 Blue Swirl
        qemu_clock_enable(vm_clock,
1126 345f4426 Jan Kiszka
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1127 296af7c9 Blue Swirl
1128 3c638d06 Jan Kiszka
        if (cpu_can_run(env)) {
1129 bdb7ca67 Jan Kiszka
            r = tcg_cpu_exec(env);
1130 9a36085b Jan Kiszka
            if (r == EXCP_DEBUG) {
1131 1009d2ed Jan Kiszka
                cpu_handle_guest_debug(env);
1132 3c638d06 Jan Kiszka
                break;
1133 3c638d06 Jan Kiszka
            }
1134 df646dfd Paolo Bonzini
        } else if (env->stop || env->stopped) {
1135 296af7c9 Blue Swirl
            break;
1136 296af7c9 Blue Swirl
        }
1137 296af7c9 Blue Swirl
    }
1138 c629a4bc Jan Kiszka
    exit_request = 0;
1139 296af7c9 Blue Swirl
}
1140 296af7c9 Blue Swirl
1141 296af7c9 Blue Swirl
void set_numa_modes(void)
1142 296af7c9 Blue Swirl
{
1143 9349b4f9 Andreas Färber
    CPUArchState *env;
1144 296af7c9 Blue Swirl
    int i;
1145 296af7c9 Blue Swirl
1146 296af7c9 Blue Swirl
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1147 296af7c9 Blue Swirl
        for (i = 0; i < nb_numa_nodes; i++) {
1148 296af7c9 Blue Swirl
            if (node_cpumask[i] & (1 << env->cpu_index)) {
1149 296af7c9 Blue Swirl
                env->numa_node = i;
1150 296af7c9 Blue Swirl
            }
1151 296af7c9 Blue Swirl
        }
1152 296af7c9 Blue Swirl
    }
1153 296af7c9 Blue Swirl
}
1154 296af7c9 Blue Swirl
1155 296af7c9 Blue Swirl
void set_cpu_log(const char *optarg)
1156 296af7c9 Blue Swirl
{
1157 296af7c9 Blue Swirl
    int mask;
1158 296af7c9 Blue Swirl
    const CPULogItem *item;
1159 296af7c9 Blue Swirl
1160 296af7c9 Blue Swirl
    mask = cpu_str_to_log_mask(optarg);
1161 296af7c9 Blue Swirl
    if (!mask) {
1162 296af7c9 Blue Swirl
        printf("Log items (comma separated):\n");
1163 296af7c9 Blue Swirl
        for (item = cpu_log_items; item->mask != 0; item++) {
1164 296af7c9 Blue Swirl
            printf("%-10s %s\n", item->name, item->help);
1165 296af7c9 Blue Swirl
        }
1166 296af7c9 Blue Swirl
        exit(1);
1167 296af7c9 Blue Swirl
    }
1168 296af7c9 Blue Swirl
    cpu_set_log(mask);
1169 296af7c9 Blue Swirl
}
1170 29e922b6 Blue Swirl
1171 c235d738 Matthew Fernandez
void set_cpu_log_filename(const char *optarg)
1172 c235d738 Matthew Fernandez
{
1173 c235d738 Matthew Fernandez
    cpu_set_log_filename(optarg);
1174 c235d738 Matthew Fernandez
}
1175 c235d738 Matthew Fernandez
1176 9a78eead Stefan Weil
void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1177 262353cb Blue Swirl
{
1178 262353cb Blue Swirl
    /* XXX: implement xxx_cpu_list for targets that still miss it */
1179 262353cb Blue Swirl
#if defined(cpu_list_id)
1180 262353cb Blue Swirl
    cpu_list_id(f, cpu_fprintf, optarg);
1181 262353cb Blue Swirl
#elif defined(cpu_list)
1182 262353cb Blue Swirl
    cpu_list(f, cpu_fprintf); /* deprecated */
1183 262353cb Blue Swirl
#endif
1184 262353cb Blue Swirl
}
1185 de0b36b6 Luiz Capitulino
1186 de0b36b6 Luiz Capitulino
CpuInfoList *qmp_query_cpus(Error **errp)
1187 de0b36b6 Luiz Capitulino
{
1188 de0b36b6 Luiz Capitulino
    CpuInfoList *head = NULL, *cur_item = NULL;
1189 9349b4f9 Andreas Färber
    CPUArchState *env;
1190 de0b36b6 Luiz Capitulino
1191 de0b36b6 Luiz Capitulino
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
1192 de0b36b6 Luiz Capitulino
        CpuInfoList *info;
1193 de0b36b6 Luiz Capitulino
1194 de0b36b6 Luiz Capitulino
        cpu_synchronize_state(env);
1195 de0b36b6 Luiz Capitulino
1196 de0b36b6 Luiz Capitulino
        info = g_malloc0(sizeof(*info));
1197 de0b36b6 Luiz Capitulino
        info->value = g_malloc0(sizeof(*info->value));
1198 de0b36b6 Luiz Capitulino
        info->value->CPU = env->cpu_index;
1199 de0b36b6 Luiz Capitulino
        info->value->current = (env == first_cpu);
1200 de0b36b6 Luiz Capitulino
        info->value->halted = env->halted;
1201 de0b36b6 Luiz Capitulino
        info->value->thread_id = env->thread_id;
1202 de0b36b6 Luiz Capitulino
#if defined(TARGET_I386)
1203 de0b36b6 Luiz Capitulino
        info->value->has_pc = true;
1204 de0b36b6 Luiz Capitulino
        info->value->pc = env->eip + env->segs[R_CS].base;
1205 de0b36b6 Luiz Capitulino
#elif defined(TARGET_PPC)
1206 de0b36b6 Luiz Capitulino
        info->value->has_nip = true;
1207 de0b36b6 Luiz Capitulino
        info->value->nip = env->nip;
1208 de0b36b6 Luiz Capitulino
#elif defined(TARGET_SPARC)
1209 de0b36b6 Luiz Capitulino
        info->value->has_pc = true;
1210 de0b36b6 Luiz Capitulino
        info->value->pc = env->pc;
1211 de0b36b6 Luiz Capitulino
        info->value->has_npc = true;
1212 de0b36b6 Luiz Capitulino
        info->value->npc = env->npc;
1213 de0b36b6 Luiz Capitulino
#elif defined(TARGET_MIPS)
1214 de0b36b6 Luiz Capitulino
        info->value->has_PC = true;
1215 de0b36b6 Luiz Capitulino
        info->value->PC = env->active_tc.PC;
1216 de0b36b6 Luiz Capitulino
#endif
1217 de0b36b6 Luiz Capitulino
1218 de0b36b6 Luiz Capitulino
        /* XXX: waiting for the qapi to support GSList */
1219 de0b36b6 Luiz Capitulino
        if (!cur_item) {
1220 de0b36b6 Luiz Capitulino
            head = cur_item = info;
1221 de0b36b6 Luiz Capitulino
        } else {
1222 de0b36b6 Luiz Capitulino
            cur_item->next = info;
1223 de0b36b6 Luiz Capitulino
            cur_item = info;
1224 de0b36b6 Luiz Capitulino
        }
1225 de0b36b6 Luiz Capitulino
    }
1226 de0b36b6 Luiz Capitulino
1227 de0b36b6 Luiz Capitulino
    return head;
1228 de0b36b6 Luiz Capitulino
}
1229 0cfd6a9a Luiz Capitulino
1230 0cfd6a9a Luiz Capitulino
void qmp_memsave(int64_t addr, int64_t size, const char *filename,
1231 0cfd6a9a Luiz Capitulino
                 bool has_cpu, int64_t cpu_index, Error **errp)
1232 0cfd6a9a Luiz Capitulino
{
1233 0cfd6a9a Luiz Capitulino
    FILE *f;
1234 0cfd6a9a Luiz Capitulino
    uint32_t l;
1235 9349b4f9 Andreas Färber
    CPUArchState *env;
1236 0cfd6a9a Luiz Capitulino
    uint8_t buf[1024];
1237 0cfd6a9a Luiz Capitulino
1238 0cfd6a9a Luiz Capitulino
    if (!has_cpu) {
1239 0cfd6a9a Luiz Capitulino
        cpu_index = 0;
1240 0cfd6a9a Luiz Capitulino
    }
1241 0cfd6a9a Luiz Capitulino
1242 0cfd6a9a Luiz Capitulino
    for (env = first_cpu; env; env = env->next_cpu) {
1243 0cfd6a9a Luiz Capitulino
        if (cpu_index == env->cpu_index) {
1244 0cfd6a9a Luiz Capitulino
            break;
1245 0cfd6a9a Luiz Capitulino
        }
1246 0cfd6a9a Luiz Capitulino
    }
1247 0cfd6a9a Luiz Capitulino
1248 0cfd6a9a Luiz Capitulino
    if (env == NULL) {
1249 0cfd6a9a Luiz Capitulino
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
1250 0cfd6a9a Luiz Capitulino
                  "a CPU number");
1251 0cfd6a9a Luiz Capitulino
        return;
1252 0cfd6a9a Luiz Capitulino
    }
1253 0cfd6a9a Luiz Capitulino
1254 0cfd6a9a Luiz Capitulino
    f = fopen(filename, "wb");
1255 0cfd6a9a Luiz Capitulino
    if (!f) {
1256 0cfd6a9a Luiz Capitulino
        error_set(errp, QERR_OPEN_FILE_FAILED, filename);
1257 0cfd6a9a Luiz Capitulino
        return;
1258 0cfd6a9a Luiz Capitulino
    }
1259 0cfd6a9a Luiz Capitulino
1260 0cfd6a9a Luiz Capitulino
    while (size != 0) {
1261 0cfd6a9a Luiz Capitulino
        l = sizeof(buf);
1262 0cfd6a9a Luiz Capitulino
        if (l > size)
1263 0cfd6a9a Luiz Capitulino
            l = size;
1264 0cfd6a9a Luiz Capitulino
        cpu_memory_rw_debug(env, addr, buf, l, 0);
1265 0cfd6a9a Luiz Capitulino
        if (fwrite(buf, 1, l, f) != l) {
1266 0cfd6a9a Luiz Capitulino
            error_set(errp, QERR_IO_ERROR);
1267 0cfd6a9a Luiz Capitulino
            goto exit;
1268 0cfd6a9a Luiz Capitulino
        }
1269 0cfd6a9a Luiz Capitulino
        addr += l;
1270 0cfd6a9a Luiz Capitulino
        size -= l;
1271 0cfd6a9a Luiz Capitulino
    }
1272 0cfd6a9a Luiz Capitulino
1273 0cfd6a9a Luiz Capitulino
exit:
1274 0cfd6a9a Luiz Capitulino
    fclose(f);
1275 0cfd6a9a Luiz Capitulino
}
1276 6d3962bf Luiz Capitulino
1277 6d3962bf Luiz Capitulino
void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
1278 6d3962bf Luiz Capitulino
                  Error **errp)
1279 6d3962bf Luiz Capitulino
{
1280 6d3962bf Luiz Capitulino
    FILE *f;
1281 6d3962bf Luiz Capitulino
    uint32_t l;
1282 6d3962bf Luiz Capitulino
    uint8_t buf[1024];
1283 6d3962bf Luiz Capitulino
1284 6d3962bf Luiz Capitulino
    f = fopen(filename, "wb");
1285 6d3962bf Luiz Capitulino
    if (!f) {
1286 6d3962bf Luiz Capitulino
        error_set(errp, QERR_OPEN_FILE_FAILED, filename);
1287 6d3962bf Luiz Capitulino
        return;
1288 6d3962bf Luiz Capitulino
    }
1289 6d3962bf Luiz Capitulino
1290 6d3962bf Luiz Capitulino
    while (size != 0) {
1291 6d3962bf Luiz Capitulino
        l = sizeof(buf);
1292 6d3962bf Luiz Capitulino
        if (l > size)
1293 6d3962bf Luiz Capitulino
            l = size;
1294 6d3962bf Luiz Capitulino
        cpu_physical_memory_rw(addr, buf, l, 0);
1295 6d3962bf Luiz Capitulino
        if (fwrite(buf, 1, l, f) != l) {
1296 6d3962bf Luiz Capitulino
            error_set(errp, QERR_IO_ERROR);
1297 6d3962bf Luiz Capitulino
            goto exit;
1298 6d3962bf Luiz Capitulino
        }
1299 6d3962bf Luiz Capitulino
        addr += l;
1300 6d3962bf Luiz Capitulino
        size -= l;
1301 6d3962bf Luiz Capitulino
    }
1302 6d3962bf Luiz Capitulino
1303 6d3962bf Luiz Capitulino
exit:
1304 6d3962bf Luiz Capitulino
    fclose(f);
1305 6d3962bf Luiz Capitulino
}
1306 ab49ab5c Luiz Capitulino
1307 ab49ab5c Luiz Capitulino
void qmp_inject_nmi(Error **errp)
1308 ab49ab5c Luiz Capitulino
{
1309 ab49ab5c Luiz Capitulino
#if defined(TARGET_I386)
1310 9349b4f9 Andreas Färber
    CPUArchState *env;
1311 ab49ab5c Luiz Capitulino
1312 ab49ab5c Luiz Capitulino
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1313 02c09195 Jan Kiszka
        if (!env->apic_state) {
1314 02c09195 Jan Kiszka
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1315 02c09195 Jan Kiszka
        } else {
1316 02c09195 Jan Kiszka
            apic_deliver_nmi(env->apic_state);
1317 02c09195 Jan Kiszka
        }
1318 ab49ab5c Luiz Capitulino
    }
1319 ab49ab5c Luiz Capitulino
#else
1320 ab49ab5c Luiz Capitulino
    error_set(errp, QERR_UNSUPPORTED);
1321 ab49ab5c Luiz Capitulino
#endif
1322 ab49ab5c Luiz Capitulino
}