Statistics
| Branch: | Revision:

root / cpus.c @ 3c85e74f

History | View | Annotate | Download (25.5 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 262ea18e Jan Kiszka
#include "exec-all.h"
34 296af7c9 Blue Swirl
35 96284e89 Paolo Bonzini
#include "qemu-thread.h"
36 296af7c9 Blue Swirl
#include "cpus.h"
37 a8486bc9 Marcelo Tosatti
#include "compatfd.h"
38 296af7c9 Blue Swirl
39 7277e027 Blue Swirl
#ifdef SIGRTMIN
40 7277e027 Blue Swirl
#define SIG_IPI (SIGRTMIN+4)
41 7277e027 Blue Swirl
#else
42 7277e027 Blue Swirl
#define SIG_IPI SIGUSR1
43 7277e027 Blue Swirl
#endif
44 7277e027 Blue Swirl
45 6d9cb73c Jan Kiszka
#ifdef CONFIG_LINUX
46 6d9cb73c Jan Kiszka
47 6d9cb73c Jan Kiszka
#include <sys/prctl.h>
48 6d9cb73c Jan Kiszka
49 c0532a76 Marcelo Tosatti
#ifndef PR_MCE_KILL
50 c0532a76 Marcelo Tosatti
#define PR_MCE_KILL 33
51 c0532a76 Marcelo Tosatti
#endif
52 c0532a76 Marcelo Tosatti
53 6d9cb73c Jan Kiszka
#ifndef PR_MCE_KILL_SET
54 6d9cb73c Jan Kiszka
#define PR_MCE_KILL_SET 1
55 6d9cb73c Jan Kiszka
#endif
56 6d9cb73c Jan Kiszka
57 6d9cb73c Jan Kiszka
#ifndef PR_MCE_KILL_EARLY
58 6d9cb73c Jan Kiszka
#define PR_MCE_KILL_EARLY 1
59 6d9cb73c Jan Kiszka
#endif
60 6d9cb73c Jan Kiszka
61 6d9cb73c Jan Kiszka
#endif /* CONFIG_LINUX */
62 6d9cb73c Jan Kiszka
63 296af7c9 Blue Swirl
static CPUState *next_cpu;
64 296af7c9 Blue Swirl
65 296af7c9 Blue Swirl
/***********************************************************/
66 296af7c9 Blue Swirl
void hw_error(const char *fmt, ...)
67 296af7c9 Blue Swirl
{
68 296af7c9 Blue Swirl
    va_list ap;
69 296af7c9 Blue Swirl
    CPUState *env;
70 296af7c9 Blue Swirl
71 296af7c9 Blue Swirl
    va_start(ap, fmt);
72 296af7c9 Blue Swirl
    fprintf(stderr, "qemu: hardware error: ");
73 296af7c9 Blue Swirl
    vfprintf(stderr, fmt, ap);
74 296af7c9 Blue Swirl
    fprintf(stderr, "\n");
75 296af7c9 Blue Swirl
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
76 296af7c9 Blue Swirl
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
77 296af7c9 Blue Swirl
#ifdef TARGET_I386
78 296af7c9 Blue Swirl
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
79 296af7c9 Blue Swirl
#else
80 296af7c9 Blue Swirl
        cpu_dump_state(env, stderr, fprintf, 0);
81 296af7c9 Blue Swirl
#endif
82 296af7c9 Blue Swirl
    }
83 296af7c9 Blue Swirl
    va_end(ap);
84 296af7c9 Blue Swirl
    abort();
85 296af7c9 Blue Swirl
}
86 296af7c9 Blue Swirl
87 296af7c9 Blue Swirl
void cpu_synchronize_all_states(void)
88 296af7c9 Blue Swirl
{
89 296af7c9 Blue Swirl
    CPUState *cpu;
90 296af7c9 Blue Swirl
91 296af7c9 Blue Swirl
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
92 296af7c9 Blue Swirl
        cpu_synchronize_state(cpu);
93 296af7c9 Blue Swirl
    }
94 296af7c9 Blue Swirl
}
95 296af7c9 Blue Swirl
96 296af7c9 Blue Swirl
void cpu_synchronize_all_post_reset(void)
97 296af7c9 Blue Swirl
{
98 296af7c9 Blue Swirl
    CPUState *cpu;
99 296af7c9 Blue Swirl
100 296af7c9 Blue Swirl
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
101 296af7c9 Blue Swirl
        cpu_synchronize_post_reset(cpu);
102 296af7c9 Blue Swirl
    }
103 296af7c9 Blue Swirl
}
104 296af7c9 Blue Swirl
105 296af7c9 Blue Swirl
void cpu_synchronize_all_post_init(void)
106 296af7c9 Blue Swirl
{
107 296af7c9 Blue Swirl
    CPUState *cpu;
108 296af7c9 Blue Swirl
109 296af7c9 Blue Swirl
    for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
110 296af7c9 Blue Swirl
        cpu_synchronize_post_init(cpu);
111 296af7c9 Blue Swirl
    }
112 296af7c9 Blue Swirl
}
113 296af7c9 Blue Swirl
114 3ae9501c Marcelo Tosatti
int cpu_is_stopped(CPUState *env)
115 3ae9501c Marcelo Tosatti
{
116 3ae9501c Marcelo Tosatti
    return !vm_running || env->stopped;
117 3ae9501c Marcelo Tosatti
}
118 3ae9501c Marcelo Tosatti
119 296af7c9 Blue Swirl
static void do_vm_stop(int reason)
120 296af7c9 Blue Swirl
{
121 296af7c9 Blue Swirl
    if (vm_running) {
122 296af7c9 Blue Swirl
        cpu_disable_ticks();
123 296af7c9 Blue Swirl
        vm_running = 0;
124 296af7c9 Blue Swirl
        pause_all_vcpus();
125 296af7c9 Blue Swirl
        vm_state_notify(0, reason);
126 55df6f33 Michael S. Tsirkin
        qemu_aio_flush();
127 55df6f33 Michael S. Tsirkin
        bdrv_flush_all();
128 296af7c9 Blue Swirl
        monitor_protocol_event(QEVENT_STOP, NULL);
129 296af7c9 Blue Swirl
    }
130 296af7c9 Blue Swirl
}
131 296af7c9 Blue Swirl
132 296af7c9 Blue Swirl
static int cpu_can_run(CPUState *env)
133 296af7c9 Blue Swirl
{
134 0ab07c62 Jan Kiszka
    if (env->stop) {
135 296af7c9 Blue Swirl
        return 0;
136 0ab07c62 Jan Kiszka
    }
137 0ab07c62 Jan Kiszka
    if (env->stopped || !vm_running) {
138 296af7c9 Blue Swirl
        return 0;
139 0ab07c62 Jan Kiszka
    }
140 296af7c9 Blue Swirl
    return 1;
141 296af7c9 Blue Swirl
}
142 296af7c9 Blue Swirl
143 16400322 Jan Kiszka
static bool cpu_thread_is_idle(CPUState *env)
144 296af7c9 Blue Swirl
{
145 16400322 Jan Kiszka
    if (env->stop || env->queued_work_first) {
146 16400322 Jan Kiszka
        return false;
147 16400322 Jan Kiszka
    }
148 16400322 Jan Kiszka
    if (env->stopped || !vm_running) {
149 16400322 Jan Kiszka
        return true;
150 16400322 Jan Kiszka
    }
151 16400322 Jan Kiszka
    if (!env->halted || qemu_cpu_has_work(env)) {
152 16400322 Jan Kiszka
        return false;
153 16400322 Jan Kiszka
    }
154 16400322 Jan Kiszka
    return true;
155 296af7c9 Blue Swirl
}
156 296af7c9 Blue Swirl
157 16400322 Jan Kiszka
static bool all_cpu_threads_idle(void)
158 296af7c9 Blue Swirl
{
159 296af7c9 Blue Swirl
    CPUState *env;
160 296af7c9 Blue Swirl
161 16400322 Jan Kiszka
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
162 16400322 Jan Kiszka
        if (!cpu_thread_is_idle(env)) {
163 16400322 Jan Kiszka
            return false;
164 16400322 Jan Kiszka
        }
165 16400322 Jan Kiszka
    }
166 16400322 Jan Kiszka
    return true;
167 296af7c9 Blue Swirl
}
168 296af7c9 Blue Swirl
169 83f338f7 Jan Kiszka
static CPUDebugExcpHandler *debug_excp_handler;
170 83f338f7 Jan Kiszka
171 83f338f7 Jan Kiszka
CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler)
172 83f338f7 Jan Kiszka
{
173 83f338f7 Jan Kiszka
    CPUDebugExcpHandler *old_handler = debug_excp_handler;
174 83f338f7 Jan Kiszka
175 83f338f7 Jan Kiszka
    debug_excp_handler = handler;
176 83f338f7 Jan Kiszka
    return old_handler;
177 83f338f7 Jan Kiszka
}
178 83f338f7 Jan Kiszka
179 83f338f7 Jan Kiszka
static void cpu_handle_debug_exception(CPUState *env)
180 3c638d06 Jan Kiszka
{
181 83f338f7 Jan Kiszka
    CPUWatchpoint *wp;
182 83f338f7 Jan Kiszka
183 83f338f7 Jan Kiszka
    if (!env->watchpoint_hit) {
184 83f338f7 Jan Kiszka
        QTAILQ_FOREACH(wp, &env->watchpoints, entry) {
185 83f338f7 Jan Kiszka
            wp->flags &= ~BP_WATCHPOINT_HIT;
186 83f338f7 Jan Kiszka
        }
187 83f338f7 Jan Kiszka
    }
188 83f338f7 Jan Kiszka
    if (debug_excp_handler) {
189 83f338f7 Jan Kiszka
        debug_excp_handler(env);
190 83f338f7 Jan Kiszka
    }
191 83f338f7 Jan Kiszka
192 3c638d06 Jan Kiszka
    gdb_set_stop_cpu(env);
193 8cf71710 Jan Kiszka
    qemu_system_debug_request();
194 83f338f7 Jan Kiszka
#ifdef CONFIG_IOTHREAD
195 83f338f7 Jan Kiszka
    env->stopped = 1;
196 83f338f7 Jan Kiszka
#endif
197 3c638d06 Jan Kiszka
}
198 3c638d06 Jan Kiszka
199 714bd040 Paolo Bonzini
#ifdef CONFIG_IOTHREAD
200 714bd040 Paolo Bonzini
static void cpu_signal(int sig)
201 714bd040 Paolo Bonzini
{
202 714bd040 Paolo Bonzini
    if (cpu_single_env) {
203 714bd040 Paolo Bonzini
        cpu_exit(cpu_single_env);
204 714bd040 Paolo Bonzini
    }
205 714bd040 Paolo Bonzini
    exit_request = 1;
206 714bd040 Paolo Bonzini
}
207 714bd040 Paolo Bonzini
#endif
208 714bd040 Paolo Bonzini
209 6d9cb73c Jan Kiszka
#ifdef CONFIG_LINUX
210 6d9cb73c Jan Kiszka
static void sigbus_reraise(void)
211 6d9cb73c Jan Kiszka
{
212 6d9cb73c Jan Kiszka
    sigset_t set;
213 6d9cb73c Jan Kiszka
    struct sigaction action;
214 6d9cb73c Jan Kiszka
215 6d9cb73c Jan Kiszka
    memset(&action, 0, sizeof(action));
216 6d9cb73c Jan Kiszka
    action.sa_handler = SIG_DFL;
217 6d9cb73c Jan Kiszka
    if (!sigaction(SIGBUS, &action, NULL)) {
218 6d9cb73c Jan Kiszka
        raise(SIGBUS);
219 6d9cb73c Jan Kiszka
        sigemptyset(&set);
220 6d9cb73c Jan Kiszka
        sigaddset(&set, SIGBUS);
221 6d9cb73c Jan Kiszka
        sigprocmask(SIG_UNBLOCK, &set, NULL);
222 6d9cb73c Jan Kiszka
    }
223 6d9cb73c Jan Kiszka
    perror("Failed to re-raise SIGBUS!\n");
224 6d9cb73c Jan Kiszka
    abort();
225 6d9cb73c Jan Kiszka
}
226 6d9cb73c Jan Kiszka
227 6d9cb73c Jan Kiszka
static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
228 6d9cb73c Jan Kiszka
                           void *ctx)
229 6d9cb73c Jan Kiszka
{
230 6d9cb73c Jan Kiszka
    if (kvm_on_sigbus(siginfo->ssi_code,
231 6d9cb73c Jan Kiszka
                      (void *)(intptr_t)siginfo->ssi_addr)) {
232 6d9cb73c Jan Kiszka
        sigbus_reraise();
233 6d9cb73c Jan Kiszka
    }
234 6d9cb73c Jan Kiszka
}
235 6d9cb73c Jan Kiszka
236 6d9cb73c Jan Kiszka
static void qemu_init_sigbus(void)
237 6d9cb73c Jan Kiszka
{
238 6d9cb73c Jan Kiszka
    struct sigaction action;
239 6d9cb73c Jan Kiszka
240 6d9cb73c Jan Kiszka
    memset(&action, 0, sizeof(action));
241 6d9cb73c Jan Kiszka
    action.sa_flags = SA_SIGINFO;
242 6d9cb73c Jan Kiszka
    action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
243 6d9cb73c Jan Kiszka
    sigaction(SIGBUS, &action, NULL);
244 6d9cb73c Jan Kiszka
245 6d9cb73c Jan Kiszka
    prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
246 6d9cb73c Jan Kiszka
}
247 6d9cb73c Jan Kiszka
248 6d9cb73c Jan Kiszka
#else /* !CONFIG_LINUX */
249 6d9cb73c Jan Kiszka
250 6d9cb73c Jan Kiszka
static void qemu_init_sigbus(void)
251 6d9cb73c Jan Kiszka
{
252 6d9cb73c Jan Kiszka
}
253 6d9cb73c Jan Kiszka
#endif /* !CONFIG_LINUX */
254 6d9cb73c Jan Kiszka
255 296af7c9 Blue Swirl
#ifndef _WIN32
256 296af7c9 Blue Swirl
static int io_thread_fd = -1;
257 296af7c9 Blue Swirl
258 296af7c9 Blue Swirl
static void qemu_event_increment(void)
259 296af7c9 Blue Swirl
{
260 296af7c9 Blue Swirl
    /* Write 8 bytes to be compatible with eventfd.  */
261 26a82330 Blue Swirl
    static const uint64_t val = 1;
262 296af7c9 Blue Swirl
    ssize_t ret;
263 296af7c9 Blue Swirl
264 0ab07c62 Jan Kiszka
    if (io_thread_fd == -1) {
265 296af7c9 Blue Swirl
        return;
266 0ab07c62 Jan Kiszka
    }
267 296af7c9 Blue Swirl
    do {
268 296af7c9 Blue Swirl
        ret = write(io_thread_fd, &val, sizeof(val));
269 296af7c9 Blue Swirl
    } while (ret < 0 && errno == EINTR);
270 296af7c9 Blue Swirl
271 296af7c9 Blue Swirl
    /* EAGAIN is fine, a read must be pending.  */
272 296af7c9 Blue Swirl
    if (ret < 0 && errno != EAGAIN) {
273 296af7c9 Blue Swirl
        fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
274 296af7c9 Blue Swirl
                strerror(errno));
275 296af7c9 Blue Swirl
        exit (1);
276 296af7c9 Blue Swirl
    }
277 296af7c9 Blue Swirl
}
278 296af7c9 Blue Swirl
279 296af7c9 Blue Swirl
static void qemu_event_read(void *opaque)
280 296af7c9 Blue Swirl
{
281 296af7c9 Blue Swirl
    int fd = (unsigned long)opaque;
282 296af7c9 Blue Swirl
    ssize_t len;
283 296af7c9 Blue Swirl
    char buffer[512];
284 296af7c9 Blue Swirl
285 296af7c9 Blue Swirl
    /* Drain the notify pipe.  For eventfd, only 8 bytes will be read.  */
286 296af7c9 Blue Swirl
    do {
287 296af7c9 Blue Swirl
        len = read(fd, buffer, sizeof(buffer));
288 296af7c9 Blue Swirl
    } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
289 296af7c9 Blue Swirl
}
290 296af7c9 Blue Swirl
291 296af7c9 Blue Swirl
static int qemu_event_init(void)
292 296af7c9 Blue Swirl
{
293 296af7c9 Blue Swirl
    int err;
294 296af7c9 Blue Swirl
    int fds[2];
295 296af7c9 Blue Swirl
296 296af7c9 Blue Swirl
    err = qemu_eventfd(fds);
297 0ab07c62 Jan Kiszka
    if (err == -1) {
298 296af7c9 Blue Swirl
        return -errno;
299 0ab07c62 Jan Kiszka
    }
300 296af7c9 Blue Swirl
    err = fcntl_setfl(fds[0], O_NONBLOCK);
301 0ab07c62 Jan Kiszka
    if (err < 0) {
302 296af7c9 Blue Swirl
        goto fail;
303 0ab07c62 Jan Kiszka
    }
304 296af7c9 Blue Swirl
    err = fcntl_setfl(fds[1], O_NONBLOCK);
305 0ab07c62 Jan Kiszka
    if (err < 0) {
306 296af7c9 Blue Swirl
        goto fail;
307 0ab07c62 Jan Kiszka
    }
308 296af7c9 Blue Swirl
    qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
309 296af7c9 Blue Swirl
                         (void *)(unsigned long)fds[0]);
310 296af7c9 Blue Swirl
311 296af7c9 Blue Swirl
    io_thread_fd = fds[1];
312 296af7c9 Blue Swirl
    return 0;
313 296af7c9 Blue Swirl
314 296af7c9 Blue Swirl
fail:
315 296af7c9 Blue Swirl
    close(fds[0]);
316 296af7c9 Blue Swirl
    close(fds[1]);
317 296af7c9 Blue Swirl
    return err;
318 296af7c9 Blue Swirl
}
319 55f8d6ac Jan Kiszka
320 55f8d6ac Jan Kiszka
static void dummy_signal(int sig)
321 55f8d6ac Jan Kiszka
{
322 55f8d6ac Jan Kiszka
}
323 55f8d6ac Jan Kiszka
324 d0f294ce Jan Kiszka
/* If we have signalfd, we mask out the signals we want to handle and then
325 d0f294ce Jan Kiszka
 * use signalfd to listen for them.  We rely on whatever the current signal
326 d0f294ce Jan Kiszka
 * handler is to dispatch the signals when we receive them.
327 d0f294ce Jan Kiszka
 */
328 d0f294ce Jan Kiszka
static void sigfd_handler(void *opaque)
329 d0f294ce Jan Kiszka
{
330 d0f294ce Jan Kiszka
    int fd = (unsigned long) opaque;
331 d0f294ce Jan Kiszka
    struct qemu_signalfd_siginfo info;
332 d0f294ce Jan Kiszka
    struct sigaction action;
333 d0f294ce Jan Kiszka
    ssize_t len;
334 d0f294ce Jan Kiszka
335 d0f294ce Jan Kiszka
    while (1) {
336 d0f294ce Jan Kiszka
        do {
337 d0f294ce Jan Kiszka
            len = read(fd, &info, sizeof(info));
338 d0f294ce Jan Kiszka
        } while (len == -1 && errno == EINTR);
339 d0f294ce Jan Kiszka
340 d0f294ce Jan Kiszka
        if (len == -1 && errno == EAGAIN) {
341 d0f294ce Jan Kiszka
            break;
342 d0f294ce Jan Kiszka
        }
343 d0f294ce Jan Kiszka
344 d0f294ce Jan Kiszka
        if (len != sizeof(info)) {
345 d0f294ce Jan Kiszka
            printf("read from sigfd returned %zd: %m\n", len);
346 d0f294ce Jan Kiszka
            return;
347 d0f294ce Jan Kiszka
        }
348 d0f294ce Jan Kiszka
349 d0f294ce Jan Kiszka
        sigaction(info.ssi_signo, NULL, &action);
350 d0f294ce Jan Kiszka
        if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
351 d0f294ce Jan Kiszka
            action.sa_sigaction(info.ssi_signo,
352 d0f294ce Jan Kiszka
                                (siginfo_t *)&info, NULL);
353 d0f294ce Jan Kiszka
        } else if (action.sa_handler) {
354 d0f294ce Jan Kiszka
            action.sa_handler(info.ssi_signo);
355 d0f294ce Jan Kiszka
        }
356 d0f294ce Jan Kiszka
    }
357 d0f294ce Jan Kiszka
}
358 d0f294ce Jan Kiszka
359 712ae480 Paolo Bonzini
static int qemu_signal_init(void)
360 d0f294ce Jan Kiszka
{
361 d0f294ce Jan Kiszka
    int sigfd;
362 712ae480 Paolo Bonzini
    sigset_t set;
363 d0f294ce Jan Kiszka
364 712ae480 Paolo Bonzini
#ifdef CONFIG_IOTHREAD
365 712ae480 Paolo Bonzini
    /* SIGUSR2 used by posix-aio-compat.c */
366 712ae480 Paolo Bonzini
    sigemptyset(&set);
367 712ae480 Paolo Bonzini
    sigaddset(&set, SIGUSR2);
368 712ae480 Paolo Bonzini
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
369 712ae480 Paolo Bonzini
370 712ae480 Paolo Bonzini
    sigemptyset(&set);
371 712ae480 Paolo Bonzini
    sigaddset(&set, SIGIO);
372 712ae480 Paolo Bonzini
    sigaddset(&set, SIGALRM);
373 712ae480 Paolo Bonzini
    sigaddset(&set, SIG_IPI);
374 712ae480 Paolo Bonzini
    sigaddset(&set, SIGBUS);
375 712ae480 Paolo Bonzini
    pthread_sigmask(SIG_BLOCK, &set, NULL);
376 712ae480 Paolo Bonzini
#else
377 712ae480 Paolo Bonzini
    sigemptyset(&set);
378 712ae480 Paolo Bonzini
    sigaddset(&set, SIGBUS);
379 712ae480 Paolo Bonzini
    if (kvm_enabled()) {
380 712ae480 Paolo Bonzini
        /*
381 712ae480 Paolo Bonzini
         * We need to process timer signals synchronously to avoid a race
382 712ae480 Paolo Bonzini
         * between exit_request check and KVM vcpu entry.
383 712ae480 Paolo Bonzini
         */
384 712ae480 Paolo Bonzini
        sigaddset(&set, SIGIO);
385 712ae480 Paolo Bonzini
        sigaddset(&set, SIGALRM);
386 712ae480 Paolo Bonzini
    }
387 712ae480 Paolo Bonzini
#endif
388 712ae480 Paolo Bonzini
389 712ae480 Paolo Bonzini
    sigfd = qemu_signalfd(&set);
390 d0f294ce Jan Kiszka
    if (sigfd == -1) {
391 d0f294ce Jan Kiszka
        fprintf(stderr, "failed to create signalfd\n");
392 d0f294ce Jan Kiszka
        return -errno;
393 d0f294ce Jan Kiszka
    }
394 d0f294ce Jan Kiszka
395 d0f294ce Jan Kiszka
    fcntl_setfl(sigfd, O_NONBLOCK);
396 d0f294ce Jan Kiszka
397 d0f294ce Jan Kiszka
    qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
398 d0f294ce Jan Kiszka
                         (void *)(unsigned long) sigfd);
399 d0f294ce Jan Kiszka
400 d0f294ce Jan Kiszka
    return 0;
401 d0f294ce Jan Kiszka
}
402 d0f294ce Jan Kiszka
403 714bd040 Paolo Bonzini
static void qemu_kvm_init_cpu_signals(CPUState *env)
404 714bd040 Paolo Bonzini
{
405 714bd040 Paolo Bonzini
    int r;
406 714bd040 Paolo Bonzini
    sigset_t set;
407 714bd040 Paolo Bonzini
    struct sigaction sigact;
408 714bd040 Paolo Bonzini
409 714bd040 Paolo Bonzini
    memset(&sigact, 0, sizeof(sigact));
410 714bd040 Paolo Bonzini
    sigact.sa_handler = dummy_signal;
411 714bd040 Paolo Bonzini
    sigaction(SIG_IPI, &sigact, NULL);
412 714bd040 Paolo Bonzini
413 714bd040 Paolo Bonzini
#ifdef CONFIG_IOTHREAD
414 714bd040 Paolo Bonzini
    pthread_sigmask(SIG_BLOCK, NULL, &set);
415 714bd040 Paolo Bonzini
    sigdelset(&set, SIG_IPI);
416 714bd040 Paolo Bonzini
    sigdelset(&set, SIGBUS);
417 714bd040 Paolo Bonzini
    r = kvm_set_signal_mask(env, &set);
418 714bd040 Paolo Bonzini
    if (r) {
419 714bd040 Paolo Bonzini
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
420 714bd040 Paolo Bonzini
        exit(1);
421 714bd040 Paolo Bonzini
    }
422 714bd040 Paolo Bonzini
#else
423 714bd040 Paolo Bonzini
    sigemptyset(&set);
424 714bd040 Paolo Bonzini
    sigaddset(&set, SIG_IPI);
425 714bd040 Paolo Bonzini
    sigaddset(&set, SIGIO);
426 714bd040 Paolo Bonzini
    sigaddset(&set, SIGALRM);
427 714bd040 Paolo Bonzini
    pthread_sigmask(SIG_BLOCK, &set, NULL);
428 714bd040 Paolo Bonzini
429 714bd040 Paolo Bonzini
    pthread_sigmask(SIG_BLOCK, NULL, &set);
430 714bd040 Paolo Bonzini
    sigdelset(&set, SIGIO);
431 714bd040 Paolo Bonzini
    sigdelset(&set, SIGALRM);
432 714bd040 Paolo Bonzini
#endif
433 714bd040 Paolo Bonzini
    sigdelset(&set, SIG_IPI);
434 714bd040 Paolo Bonzini
    sigdelset(&set, SIGBUS);
435 714bd040 Paolo Bonzini
    r = kvm_set_signal_mask(env, &set);
436 714bd040 Paolo Bonzini
    if (r) {
437 714bd040 Paolo Bonzini
        fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
438 714bd040 Paolo Bonzini
        exit(1);
439 714bd040 Paolo Bonzini
    }
440 714bd040 Paolo Bonzini
}
441 714bd040 Paolo Bonzini
442 714bd040 Paolo Bonzini
static void qemu_tcg_init_cpu_signals(void)
443 714bd040 Paolo Bonzini
{
444 714bd040 Paolo Bonzini
#ifdef CONFIG_IOTHREAD
445 714bd040 Paolo Bonzini
    sigset_t set;
446 714bd040 Paolo Bonzini
    struct sigaction sigact;
447 714bd040 Paolo Bonzini
448 714bd040 Paolo Bonzini
    memset(&sigact, 0, sizeof(sigact));
449 714bd040 Paolo Bonzini
    sigact.sa_handler = cpu_signal;
450 714bd040 Paolo Bonzini
    sigaction(SIG_IPI, &sigact, NULL);
451 714bd040 Paolo Bonzini
452 714bd040 Paolo Bonzini
    sigemptyset(&set);
453 714bd040 Paolo Bonzini
    sigaddset(&set, SIG_IPI);
454 714bd040 Paolo Bonzini
    pthread_sigmask(SIG_UNBLOCK, &set, NULL);
455 714bd040 Paolo Bonzini
#endif
456 714bd040 Paolo Bonzini
}
457 714bd040 Paolo Bonzini
458 9a36085b Jan Kiszka
static void qemu_kvm_eat_signals(CPUState *env)
459 9a36085b Jan Kiszka
{
460 9a36085b Jan Kiszka
    struct timespec ts = { 0, 0 };
461 9a36085b Jan Kiszka
    siginfo_t siginfo;
462 9a36085b Jan Kiszka
    sigset_t waitset;
463 9a36085b Jan Kiszka
    sigset_t chkset;
464 9a36085b Jan Kiszka
    int r;
465 9a36085b Jan Kiszka
466 9a36085b Jan Kiszka
    sigemptyset(&waitset);
467 9a36085b Jan Kiszka
    sigaddset(&waitset, SIG_IPI);
468 9a36085b Jan Kiszka
    sigaddset(&waitset, SIGBUS);
469 9a36085b Jan Kiszka
470 9a36085b Jan Kiszka
    do {
471 9a36085b Jan Kiszka
        r = sigtimedwait(&waitset, &siginfo, &ts);
472 9a36085b Jan Kiszka
        if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
473 9a36085b Jan Kiszka
            perror("sigtimedwait");
474 9a36085b Jan Kiszka
            exit(1);
475 9a36085b Jan Kiszka
        }
476 9a36085b Jan Kiszka
477 9a36085b Jan Kiszka
        switch (r) {
478 9a36085b Jan Kiszka
        case SIGBUS:
479 9a36085b Jan Kiszka
            if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
480 9a36085b Jan Kiszka
                sigbus_reraise();
481 9a36085b Jan Kiszka
            }
482 9a36085b Jan Kiszka
            break;
483 9a36085b Jan Kiszka
        default:
484 9a36085b Jan Kiszka
            break;
485 9a36085b Jan Kiszka
        }
486 9a36085b Jan Kiszka
487 9a36085b Jan Kiszka
        r = sigpending(&chkset);
488 9a36085b Jan Kiszka
        if (r == -1) {
489 9a36085b Jan Kiszka
            perror("sigpending");
490 9a36085b Jan Kiszka
            exit(1);
491 9a36085b Jan Kiszka
        }
492 9a36085b Jan Kiszka
    } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
493 de758970 Jan Kiszka
494 de758970 Jan Kiszka
#ifndef CONFIG_IOTHREAD
495 de758970 Jan Kiszka
    if (sigismember(&chkset, SIGIO) || sigismember(&chkset, SIGALRM)) {
496 de758970 Jan Kiszka
        qemu_notify_event();
497 de758970 Jan Kiszka
    }
498 de758970 Jan Kiszka
#endif
499 9a36085b Jan Kiszka
}
500 9a36085b Jan Kiszka
501 55f8d6ac Jan Kiszka
#else /* _WIN32 */
502 55f8d6ac Jan Kiszka
503 296af7c9 Blue Swirl
HANDLE qemu_event_handle;
504 296af7c9 Blue Swirl
505 296af7c9 Blue Swirl
static void dummy_event_handler(void *opaque)
506 296af7c9 Blue Swirl
{
507 296af7c9 Blue Swirl
}
508 296af7c9 Blue Swirl
509 296af7c9 Blue Swirl
static int qemu_event_init(void)
510 296af7c9 Blue Swirl
{
511 296af7c9 Blue Swirl
    qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
512 296af7c9 Blue Swirl
    if (!qemu_event_handle) {
513 296af7c9 Blue Swirl
        fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
514 296af7c9 Blue Swirl
        return -1;
515 296af7c9 Blue Swirl
    }
516 296af7c9 Blue Swirl
    qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
517 296af7c9 Blue Swirl
    return 0;
518 296af7c9 Blue Swirl
}
519 296af7c9 Blue Swirl
520 296af7c9 Blue Swirl
static void qemu_event_increment(void)
521 296af7c9 Blue Swirl
{
522 296af7c9 Blue Swirl
    if (!SetEvent(qemu_event_handle)) {
523 296af7c9 Blue Swirl
        fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
524 296af7c9 Blue Swirl
                GetLastError());
525 296af7c9 Blue Swirl
        exit (1);
526 296af7c9 Blue Swirl
    }
527 296af7c9 Blue Swirl
}
528 9a36085b Jan Kiszka
529 9a36085b Jan Kiszka
static void qemu_kvm_eat_signals(CPUState *env)
530 9a36085b Jan Kiszka
{
531 9a36085b Jan Kiszka
}
532 712ae480 Paolo Bonzini
533 712ae480 Paolo Bonzini
static int qemu_signal_init(void)
534 712ae480 Paolo Bonzini
{
535 712ae480 Paolo Bonzini
    return 0;
536 712ae480 Paolo Bonzini
}
537 712ae480 Paolo Bonzini
538 ff48eb5f Jan Kiszka
static void qemu_kvm_init_cpu_signals(CPUState *env)
539 ff48eb5f Jan Kiszka
{
540 714bd040 Paolo Bonzini
    abort();
541 714bd040 Paolo Bonzini
}
542 ff48eb5f Jan Kiszka
543 714bd040 Paolo Bonzini
static void qemu_tcg_init_cpu_signals(void)
544 714bd040 Paolo Bonzini
{
545 ff48eb5f Jan Kiszka
}
546 714bd040 Paolo Bonzini
#endif /* _WIN32 */
547 ff48eb5f Jan Kiszka
548 714bd040 Paolo Bonzini
#ifndef CONFIG_IOTHREAD
549 296af7c9 Blue Swirl
int qemu_init_main_loop(void)
550 296af7c9 Blue Swirl
{
551 d0f294ce Jan Kiszka
    int ret;
552 d0f294ce Jan Kiszka
553 712ae480 Paolo Bonzini
    ret = qemu_signal_init();
554 d0f294ce Jan Kiszka
    if (ret) {
555 d0f294ce Jan Kiszka
        return ret;
556 d0f294ce Jan Kiszka
    }
557 3c638d06 Jan Kiszka
558 6d9cb73c Jan Kiszka
    qemu_init_sigbus();
559 3c638d06 Jan Kiszka
560 296af7c9 Blue Swirl
    return qemu_event_init();
561 296af7c9 Blue Swirl
}
562 296af7c9 Blue Swirl
563 7277e027 Blue Swirl
void qemu_main_loop_start(void)
564 7277e027 Blue Swirl
{
565 7277e027 Blue Swirl
}
566 7277e027 Blue Swirl
567 296af7c9 Blue Swirl
void qemu_init_vcpu(void *_env)
568 296af7c9 Blue Swirl
{
569 296af7c9 Blue Swirl
    CPUState *env = _env;
570 84b4915d Jan Kiszka
    int r;
571 296af7c9 Blue Swirl
572 296af7c9 Blue Swirl
    env->nr_cores = smp_cores;
573 296af7c9 Blue Swirl
    env->nr_threads = smp_threads;
574 84b4915d Jan Kiszka
575 84b4915d Jan Kiszka
    if (kvm_enabled()) {
576 84b4915d Jan Kiszka
        r = kvm_init_vcpu(env);
577 84b4915d Jan Kiszka
        if (r < 0) {
578 84b4915d Jan Kiszka
            fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
579 84b4915d Jan Kiszka
            exit(1);
580 84b4915d Jan Kiszka
        }
581 ff48eb5f Jan Kiszka
        qemu_kvm_init_cpu_signals(env);
582 714bd040 Paolo Bonzini
    } else {
583 714bd040 Paolo Bonzini
        qemu_tcg_init_cpu_signals();
584 84b4915d Jan Kiszka
    }
585 296af7c9 Blue Swirl
}
586 296af7c9 Blue Swirl
587 b7680cb6 Jan Kiszka
int qemu_cpu_is_self(void *env)
588 296af7c9 Blue Swirl
{
589 296af7c9 Blue Swirl
    return 1;
590 296af7c9 Blue Swirl
}
591 296af7c9 Blue Swirl
592 e82bcec2 Marcelo Tosatti
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
593 e82bcec2 Marcelo Tosatti
{
594 e82bcec2 Marcelo Tosatti
    func(data);
595 e82bcec2 Marcelo Tosatti
}
596 e82bcec2 Marcelo Tosatti
597 296af7c9 Blue Swirl
void resume_all_vcpus(void)
598 296af7c9 Blue Swirl
{
599 296af7c9 Blue Swirl
}
600 296af7c9 Blue Swirl
601 296af7c9 Blue Swirl
void pause_all_vcpus(void)
602 296af7c9 Blue Swirl
{
603 296af7c9 Blue Swirl
}
604 296af7c9 Blue Swirl
605 296af7c9 Blue Swirl
void qemu_cpu_kick(void *env)
606 296af7c9 Blue Swirl
{
607 296af7c9 Blue Swirl
}
608 296af7c9 Blue Swirl
609 46d62fac Jan Kiszka
void qemu_cpu_kick_self(void)
610 46d62fac Jan Kiszka
{
611 46d62fac Jan Kiszka
#ifndef _WIN32
612 46d62fac Jan Kiszka
    assert(cpu_single_env);
613 46d62fac Jan Kiszka
614 46d62fac Jan Kiszka
    raise(SIG_IPI);
615 46d62fac Jan Kiszka
#else
616 46d62fac Jan Kiszka
    abort();
617 46d62fac Jan Kiszka
#endif
618 296af7c9 Blue Swirl
}
619 296af7c9 Blue Swirl
620 296af7c9 Blue Swirl
void qemu_notify_event(void)
621 296af7c9 Blue Swirl
{
622 296af7c9 Blue Swirl
    CPUState *env = cpu_single_env;
623 296af7c9 Blue Swirl
624 296af7c9 Blue Swirl
    qemu_event_increment ();
625 296af7c9 Blue Swirl
    if (env) {
626 296af7c9 Blue Swirl
        cpu_exit(env);
627 296af7c9 Blue Swirl
    }
628 296af7c9 Blue Swirl
    if (next_cpu && env != next_cpu) {
629 296af7c9 Blue Swirl
        cpu_exit(next_cpu);
630 296af7c9 Blue Swirl
    }
631 38145df2 Jan Kiszka
    exit_request = 1;
632 296af7c9 Blue Swirl
}
633 296af7c9 Blue Swirl
634 296af7c9 Blue Swirl
void qemu_mutex_lock_iothread(void) {}
635 296af7c9 Blue Swirl
void qemu_mutex_unlock_iothread(void) {}
636 296af7c9 Blue Swirl
637 b4a3d965 Jan Kiszka
void cpu_stop_current(void)
638 b4a3d965 Jan Kiszka
{
639 b4a3d965 Jan Kiszka
}
640 b4a3d965 Jan Kiszka
641 296af7c9 Blue Swirl
void vm_stop(int reason)
642 296af7c9 Blue Swirl
{
643 296af7c9 Blue Swirl
    do_vm_stop(reason);
644 296af7c9 Blue Swirl
}
645 296af7c9 Blue Swirl
646 296af7c9 Blue Swirl
#else /* CONFIG_IOTHREAD */
647 296af7c9 Blue Swirl
648 296af7c9 Blue Swirl
QemuMutex qemu_global_mutex;
649 296af7c9 Blue Swirl
static QemuMutex qemu_fair_mutex;
650 296af7c9 Blue Swirl
651 296af7c9 Blue Swirl
static QemuThread io_thread;
652 296af7c9 Blue Swirl
653 296af7c9 Blue Swirl
static QemuThread *tcg_cpu_thread;
654 296af7c9 Blue Swirl
static QemuCond *tcg_halt_cond;
655 296af7c9 Blue Swirl
656 296af7c9 Blue Swirl
static int qemu_system_ready;
657 296af7c9 Blue Swirl
/* cpu creation */
658 296af7c9 Blue Swirl
static QemuCond qemu_cpu_cond;
659 296af7c9 Blue Swirl
/* system init */
660 296af7c9 Blue Swirl
static QemuCond qemu_system_cond;
661 296af7c9 Blue Swirl
static QemuCond qemu_pause_cond;
662 e82bcec2 Marcelo Tosatti
static QemuCond qemu_work_cond;
663 296af7c9 Blue Swirl
664 296af7c9 Blue Swirl
int qemu_init_main_loop(void)
665 296af7c9 Blue Swirl
{
666 296af7c9 Blue Swirl
    int ret;
667 296af7c9 Blue Swirl
668 6d9cb73c Jan Kiszka
    qemu_init_sigbus();
669 3c638d06 Jan Kiszka
670 712ae480 Paolo Bonzini
    ret = qemu_signal_init();
671 0ab07c62 Jan Kiszka
    if (ret) {
672 a8486bc9 Marcelo Tosatti
        return ret;
673 0ab07c62 Jan Kiszka
    }
674 a8486bc9 Marcelo Tosatti
675 a8486bc9 Marcelo Tosatti
    /* Note eventfd must be drained before signalfd handlers run */
676 296af7c9 Blue Swirl
    ret = qemu_event_init();
677 0ab07c62 Jan Kiszka
    if (ret) {
678 296af7c9 Blue Swirl
        return ret;
679 0ab07c62 Jan Kiszka
    }
680 296af7c9 Blue Swirl
681 ed94592b Anthony Liguori
    qemu_cond_init(&qemu_cpu_cond);
682 f8ca7b43 Jan Kiszka
    qemu_cond_init(&qemu_system_cond);
683 ed94592b Anthony Liguori
    qemu_cond_init(&qemu_pause_cond);
684 ed94592b Anthony Liguori
    qemu_cond_init(&qemu_work_cond);
685 296af7c9 Blue Swirl
    qemu_mutex_init(&qemu_fair_mutex);
686 296af7c9 Blue Swirl
    qemu_mutex_init(&qemu_global_mutex);
687 296af7c9 Blue Swirl
    qemu_mutex_lock(&qemu_global_mutex);
688 296af7c9 Blue Swirl
689 b7680cb6 Jan Kiszka
    qemu_thread_get_self(&io_thread);
690 296af7c9 Blue Swirl
691 296af7c9 Blue Swirl
    return 0;
692 296af7c9 Blue Swirl
}
693 296af7c9 Blue Swirl
694 7277e027 Blue Swirl
void qemu_main_loop_start(void)
695 7277e027 Blue Swirl
{
696 7277e027 Blue Swirl
    qemu_system_ready = 1;
697 7277e027 Blue Swirl
    qemu_cond_broadcast(&qemu_system_cond);
698 7277e027 Blue Swirl
}
699 7277e027 Blue Swirl
700 e82bcec2 Marcelo Tosatti
void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
701 e82bcec2 Marcelo Tosatti
{
702 e82bcec2 Marcelo Tosatti
    struct qemu_work_item wi;
703 e82bcec2 Marcelo Tosatti
704 b7680cb6 Jan Kiszka
    if (qemu_cpu_is_self(env)) {
705 e82bcec2 Marcelo Tosatti
        func(data);
706 e82bcec2 Marcelo Tosatti
        return;
707 e82bcec2 Marcelo Tosatti
    }
708 e82bcec2 Marcelo Tosatti
709 e82bcec2 Marcelo Tosatti
    wi.func = func;
710 e82bcec2 Marcelo Tosatti
    wi.data = data;
711 0ab07c62 Jan Kiszka
    if (!env->queued_work_first) {
712 e82bcec2 Marcelo Tosatti
        env->queued_work_first = &wi;
713 0ab07c62 Jan Kiszka
    } else {
714 e82bcec2 Marcelo Tosatti
        env->queued_work_last->next = &wi;
715 0ab07c62 Jan Kiszka
    }
716 e82bcec2 Marcelo Tosatti
    env->queued_work_last = &wi;
717 e82bcec2 Marcelo Tosatti
    wi.next = NULL;
718 e82bcec2 Marcelo Tosatti
    wi.done = false;
719 e82bcec2 Marcelo Tosatti
720 e82bcec2 Marcelo Tosatti
    qemu_cpu_kick(env);
721 e82bcec2 Marcelo Tosatti
    while (!wi.done) {
722 e82bcec2 Marcelo Tosatti
        CPUState *self_env = cpu_single_env;
723 e82bcec2 Marcelo Tosatti
724 e82bcec2 Marcelo Tosatti
        qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
725 e82bcec2 Marcelo Tosatti
        cpu_single_env = self_env;
726 e82bcec2 Marcelo Tosatti
    }
727 e82bcec2 Marcelo Tosatti
}
728 e82bcec2 Marcelo Tosatti
729 e82bcec2 Marcelo Tosatti
static void flush_queued_work(CPUState *env)
730 e82bcec2 Marcelo Tosatti
{
731 e82bcec2 Marcelo Tosatti
    struct qemu_work_item *wi;
732 e82bcec2 Marcelo Tosatti
733 0ab07c62 Jan Kiszka
    if (!env->queued_work_first) {
734 e82bcec2 Marcelo Tosatti
        return;
735 0ab07c62 Jan Kiszka
    }
736 e82bcec2 Marcelo Tosatti
737 e82bcec2 Marcelo Tosatti
    while ((wi = env->queued_work_first)) {
738 e82bcec2 Marcelo Tosatti
        env->queued_work_first = wi->next;
739 e82bcec2 Marcelo Tosatti
        wi->func(wi->data);
740 e82bcec2 Marcelo Tosatti
        wi->done = true;
741 e82bcec2 Marcelo Tosatti
    }
742 e82bcec2 Marcelo Tosatti
    env->queued_work_last = NULL;
743 e82bcec2 Marcelo Tosatti
    qemu_cond_broadcast(&qemu_work_cond);
744 e82bcec2 Marcelo Tosatti
}
745 e82bcec2 Marcelo Tosatti
746 296af7c9 Blue Swirl
static void qemu_wait_io_event_common(CPUState *env)
747 296af7c9 Blue Swirl
{
748 296af7c9 Blue Swirl
    if (env->stop) {
749 296af7c9 Blue Swirl
        env->stop = 0;
750 296af7c9 Blue Swirl
        env->stopped = 1;
751 296af7c9 Blue Swirl
        qemu_cond_signal(&qemu_pause_cond);
752 296af7c9 Blue Swirl
    }
753 e82bcec2 Marcelo Tosatti
    flush_queued_work(env);
754 aa2c364b Jan Kiszka
    env->thread_kicked = false;
755 296af7c9 Blue Swirl
}
756 296af7c9 Blue Swirl
757 6cabe1f3 Jan Kiszka
static void qemu_tcg_wait_io_event(void)
758 296af7c9 Blue Swirl
{
759 6cabe1f3 Jan Kiszka
    CPUState *env;
760 6cabe1f3 Jan Kiszka
761 16400322 Jan Kiszka
    while (all_cpu_threads_idle()) {
762 9705fbb5 Paolo Bonzini
        qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
763 16400322 Jan Kiszka
    }
764 296af7c9 Blue Swirl
765 296af7c9 Blue Swirl
    qemu_mutex_unlock(&qemu_global_mutex);
766 296af7c9 Blue Swirl
767 296af7c9 Blue Swirl
    /*
768 296af7c9 Blue Swirl
     * Users of qemu_global_mutex can be starved, having no chance
769 296af7c9 Blue Swirl
     * to acquire it since this path will get to it first.
770 296af7c9 Blue Swirl
     * So use another lock to provide fairness.
771 296af7c9 Blue Swirl
     */
772 296af7c9 Blue Swirl
    qemu_mutex_lock(&qemu_fair_mutex);
773 296af7c9 Blue Swirl
    qemu_mutex_unlock(&qemu_fair_mutex);
774 296af7c9 Blue Swirl
775 296af7c9 Blue Swirl
    qemu_mutex_lock(&qemu_global_mutex);
776 6cabe1f3 Jan Kiszka
777 6cabe1f3 Jan Kiszka
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
778 6cabe1f3 Jan Kiszka
        qemu_wait_io_event_common(env);
779 6cabe1f3 Jan Kiszka
    }
780 296af7c9 Blue Swirl
}
781 296af7c9 Blue Swirl
782 296af7c9 Blue Swirl
static void qemu_kvm_wait_io_event(CPUState *env)
783 296af7c9 Blue Swirl
{
784 16400322 Jan Kiszka
    while (cpu_thread_is_idle(env)) {
785 9705fbb5 Paolo Bonzini
        qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
786 16400322 Jan Kiszka
    }
787 296af7c9 Blue Swirl
788 5db5bdac Jan Kiszka
    qemu_kvm_eat_signals(env);
789 296af7c9 Blue Swirl
    qemu_wait_io_event_common(env);
790 296af7c9 Blue Swirl
}
791 296af7c9 Blue Swirl
792 7e97cd88 Jan Kiszka
static void *qemu_kvm_cpu_thread_fn(void *arg)
793 296af7c9 Blue Swirl
{
794 296af7c9 Blue Swirl
    CPUState *env = arg;
795 84b4915d Jan Kiszka
    int r;
796 296af7c9 Blue Swirl
797 6164e6d6 Marcelo Tosatti
    qemu_mutex_lock(&qemu_global_mutex);
798 b7680cb6 Jan Kiszka
    qemu_thread_get_self(env->thread);
799 296af7c9 Blue Swirl
800 84b4915d Jan Kiszka
    r = kvm_init_vcpu(env);
801 84b4915d Jan Kiszka
    if (r < 0) {
802 84b4915d Jan Kiszka
        fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
803 84b4915d Jan Kiszka
        exit(1);
804 84b4915d Jan Kiszka
    }
805 296af7c9 Blue Swirl
806 55f8d6ac Jan Kiszka
    qemu_kvm_init_cpu_signals(env);
807 296af7c9 Blue Swirl
808 296af7c9 Blue Swirl
    /* signal CPU creation */
809 296af7c9 Blue Swirl
    env->created = 1;
810 296af7c9 Blue Swirl
    qemu_cond_signal(&qemu_cpu_cond);
811 296af7c9 Blue Swirl
812 296af7c9 Blue Swirl
    /* and wait for machine initialization */
813 0ab07c62 Jan Kiszka
    while (!qemu_system_ready) {
814 e009894f Paolo Bonzini
        qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
815 0ab07c62 Jan Kiszka
    }
816 296af7c9 Blue Swirl
817 296af7c9 Blue Swirl
    while (1) {
818 0ab07c62 Jan Kiszka
        if (cpu_can_run(env)) {
819 6792a57b Jan Kiszka
            r = kvm_cpu_exec(env);
820 83f338f7 Jan Kiszka
            if (r == EXCP_DEBUG) {
821 83f338f7 Jan Kiszka
                cpu_handle_debug_exception(env);
822 83f338f7 Jan Kiszka
            }
823 0ab07c62 Jan Kiszka
        }
824 296af7c9 Blue Swirl
        qemu_kvm_wait_io_event(env);
825 296af7c9 Blue Swirl
    }
826 296af7c9 Blue Swirl
827 296af7c9 Blue Swirl
    return NULL;
828 296af7c9 Blue Swirl
}
829 296af7c9 Blue Swirl
830 7e97cd88 Jan Kiszka
static void *qemu_tcg_cpu_thread_fn(void *arg)
831 296af7c9 Blue Swirl
{
832 296af7c9 Blue Swirl
    CPUState *env = arg;
833 296af7c9 Blue Swirl
834 55f8d6ac Jan Kiszka
    qemu_tcg_init_cpu_signals();
835 b7680cb6 Jan Kiszka
    qemu_thread_get_self(env->thread);
836 296af7c9 Blue Swirl
837 296af7c9 Blue Swirl
    /* signal CPU creation */
838 296af7c9 Blue Swirl
    qemu_mutex_lock(&qemu_global_mutex);
839 0ab07c62 Jan Kiszka
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
840 296af7c9 Blue Swirl
        env->created = 1;
841 0ab07c62 Jan Kiszka
    }
842 296af7c9 Blue Swirl
    qemu_cond_signal(&qemu_cpu_cond);
843 296af7c9 Blue Swirl
844 296af7c9 Blue Swirl
    /* and wait for machine initialization */
845 0ab07c62 Jan Kiszka
    while (!qemu_system_ready) {
846 e009894f Paolo Bonzini
        qemu_cond_wait(&qemu_system_cond, &qemu_global_mutex);
847 0ab07c62 Jan Kiszka
    }
848 296af7c9 Blue Swirl
849 296af7c9 Blue Swirl
    while (1) {
850 472fb0c4 Jan Kiszka
        cpu_exec_all();
851 6cabe1f3 Jan Kiszka
        qemu_tcg_wait_io_event();
852 296af7c9 Blue Swirl
    }
853 296af7c9 Blue Swirl
854 296af7c9 Blue Swirl
    return NULL;
855 296af7c9 Blue Swirl
}
856 296af7c9 Blue Swirl
857 cc015e9a Paolo Bonzini
static void qemu_cpu_kick_thread(CPUState *env)
858 cc015e9a Paolo Bonzini
{
859 cc015e9a Paolo Bonzini
#ifndef _WIN32
860 cc015e9a Paolo Bonzini
    int err;
861 cc015e9a Paolo Bonzini
862 cc015e9a Paolo Bonzini
    err = pthread_kill(env->thread->thread, SIG_IPI);
863 cc015e9a Paolo Bonzini
    if (err) {
864 cc015e9a Paolo Bonzini
        fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
865 cc015e9a Paolo Bonzini
        exit(1);
866 cc015e9a Paolo Bonzini
    }
867 cc015e9a Paolo Bonzini
#else /* _WIN32 */
868 cc015e9a Paolo Bonzini
    if (!qemu_cpu_is_self(env)) {
869 cc015e9a Paolo Bonzini
        SuspendThread(env->thread->thread);
870 cc015e9a Paolo Bonzini
        cpu_signal(0);
871 cc015e9a Paolo Bonzini
        ResumeThread(env->thread->thread);
872 cc015e9a Paolo Bonzini
    }
873 cc015e9a Paolo Bonzini
#endif
874 cc015e9a Paolo Bonzini
}
875 cc015e9a Paolo Bonzini
876 296af7c9 Blue Swirl
void qemu_cpu_kick(void *_env)
877 296af7c9 Blue Swirl
{
878 296af7c9 Blue Swirl
    CPUState *env = _env;
879 296af7c9 Blue Swirl
880 296af7c9 Blue Swirl
    qemu_cond_broadcast(env->halt_cond);
881 aa2c364b Jan Kiszka
    if (!env->thread_kicked) {
882 cc015e9a Paolo Bonzini
        qemu_cpu_kick_thread(env);
883 aa2c364b Jan Kiszka
        env->thread_kicked = true;
884 aa2c364b Jan Kiszka
    }
885 296af7c9 Blue Swirl
}
886 296af7c9 Blue Swirl
887 46d62fac Jan Kiszka
void qemu_cpu_kick_self(void)
888 296af7c9 Blue Swirl
{
889 b55c22c6 Paolo Bonzini
#ifndef _WIN32
890 46d62fac Jan Kiszka
    assert(cpu_single_env);
891 296af7c9 Blue Swirl
892 46d62fac Jan Kiszka
    if (!cpu_single_env->thread_kicked) {
893 cc015e9a Paolo Bonzini
        qemu_cpu_kick_thread(cpu_single_env);
894 46d62fac Jan Kiszka
        cpu_single_env->thread_kicked = true;
895 296af7c9 Blue Swirl
    }
896 b55c22c6 Paolo Bonzini
#else
897 b55c22c6 Paolo Bonzini
    abort();
898 b55c22c6 Paolo Bonzini
#endif
899 296af7c9 Blue Swirl
}
900 296af7c9 Blue Swirl
901 b7680cb6 Jan Kiszka
int qemu_cpu_is_self(void *_env)
902 296af7c9 Blue Swirl
{
903 296af7c9 Blue Swirl
    CPUState *env = _env;
904 a8486bc9 Marcelo Tosatti
905 b7680cb6 Jan Kiszka
    return qemu_thread_is_self(env->thread);
906 296af7c9 Blue Swirl
}
907 296af7c9 Blue Swirl
908 296af7c9 Blue Swirl
void qemu_mutex_lock_iothread(void)
909 296af7c9 Blue Swirl
{
910 296af7c9 Blue Swirl
    if (kvm_enabled()) {
911 296af7c9 Blue Swirl
        qemu_mutex_lock(&qemu_global_mutex);
912 1a28cac3 Marcelo Tosatti
    } else {
913 1a28cac3 Marcelo Tosatti
        qemu_mutex_lock(&qemu_fair_mutex);
914 1a28cac3 Marcelo Tosatti
        if (qemu_mutex_trylock(&qemu_global_mutex)) {
915 cc015e9a Paolo Bonzini
            qemu_cpu_kick_thread(first_cpu);
916 1a28cac3 Marcelo Tosatti
            qemu_mutex_lock(&qemu_global_mutex);
917 1a28cac3 Marcelo Tosatti
        }
918 1a28cac3 Marcelo Tosatti
        qemu_mutex_unlock(&qemu_fair_mutex);
919 1a28cac3 Marcelo Tosatti
    }
920 296af7c9 Blue Swirl
}
921 296af7c9 Blue Swirl
922 296af7c9 Blue Swirl
void qemu_mutex_unlock_iothread(void)
923 296af7c9 Blue Swirl
{
924 296af7c9 Blue Swirl
    qemu_mutex_unlock(&qemu_global_mutex);
925 296af7c9 Blue Swirl
}
926 296af7c9 Blue Swirl
927 296af7c9 Blue Swirl
static int all_vcpus_paused(void)
928 296af7c9 Blue Swirl
{
929 296af7c9 Blue Swirl
    CPUState *penv = first_cpu;
930 296af7c9 Blue Swirl
931 296af7c9 Blue Swirl
    while (penv) {
932 0ab07c62 Jan Kiszka
        if (!penv->stopped) {
933 296af7c9 Blue Swirl
            return 0;
934 0ab07c62 Jan Kiszka
        }
935 296af7c9 Blue Swirl
        penv = (CPUState *)penv->next_cpu;
936 296af7c9 Blue Swirl
    }
937 296af7c9 Blue Swirl
938 296af7c9 Blue Swirl
    return 1;
939 296af7c9 Blue Swirl
}
940 296af7c9 Blue Swirl
941 296af7c9 Blue Swirl
void pause_all_vcpus(void)
942 296af7c9 Blue Swirl
{
943 296af7c9 Blue Swirl
    CPUState *penv = first_cpu;
944 296af7c9 Blue Swirl
945 296af7c9 Blue Swirl
    while (penv) {
946 296af7c9 Blue Swirl
        penv->stop = 1;
947 296af7c9 Blue Swirl
        qemu_cpu_kick(penv);
948 296af7c9 Blue Swirl
        penv = (CPUState *)penv->next_cpu;
949 296af7c9 Blue Swirl
    }
950 296af7c9 Blue Swirl
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 296af7c9 Blue Swirl
            penv = (CPUState *)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 296af7c9 Blue Swirl
    CPUState *penv = first_cpu;
964 296af7c9 Blue Swirl
965 296af7c9 Blue Swirl
    while (penv) {
966 296af7c9 Blue Swirl
        penv->stop = 0;
967 296af7c9 Blue Swirl
        penv->stopped = 0;
968 296af7c9 Blue Swirl
        qemu_cpu_kick(penv);
969 296af7c9 Blue Swirl
        penv = (CPUState *)penv->next_cpu;
970 296af7c9 Blue Swirl
    }
971 296af7c9 Blue Swirl
}
972 296af7c9 Blue Swirl
973 7e97cd88 Jan Kiszka
static void qemu_tcg_init_vcpu(void *_env)
974 296af7c9 Blue Swirl
{
975 296af7c9 Blue Swirl
    CPUState *env = _env;
976 0ab07c62 Jan Kiszka
977 296af7c9 Blue Swirl
    /* share a single thread for all cpus with TCG */
978 296af7c9 Blue Swirl
    if (!tcg_cpu_thread) {
979 296af7c9 Blue Swirl
        env->thread = qemu_mallocz(sizeof(QemuThread));
980 296af7c9 Blue Swirl
        env->halt_cond = qemu_mallocz(sizeof(QemuCond));
981 296af7c9 Blue Swirl
        qemu_cond_init(env->halt_cond);
982 7e97cd88 Jan Kiszka
        qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
983 0ab07c62 Jan Kiszka
        while (env->created == 0) {
984 18a85728 Paolo Bonzini
            qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
985 0ab07c62 Jan Kiszka
        }
986 296af7c9 Blue Swirl
        tcg_cpu_thread = env->thread;
987 296af7c9 Blue Swirl
        tcg_halt_cond = env->halt_cond;
988 296af7c9 Blue Swirl
    } else {
989 296af7c9 Blue Swirl
        env->thread = tcg_cpu_thread;
990 296af7c9 Blue Swirl
        env->halt_cond = tcg_halt_cond;
991 296af7c9 Blue Swirl
    }
992 296af7c9 Blue Swirl
}
993 296af7c9 Blue Swirl
994 7e97cd88 Jan Kiszka
static void qemu_kvm_start_vcpu(CPUState *env)
995 296af7c9 Blue Swirl
{
996 296af7c9 Blue Swirl
    env->thread = qemu_mallocz(sizeof(QemuThread));
997 296af7c9 Blue Swirl
    env->halt_cond = qemu_mallocz(sizeof(QemuCond));
998 296af7c9 Blue Swirl
    qemu_cond_init(env->halt_cond);
999 7e97cd88 Jan Kiszka
    qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
1000 0ab07c62 Jan Kiszka
    while (env->created == 0) {
1001 18a85728 Paolo Bonzini
        qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
1002 0ab07c62 Jan Kiszka
    }
1003 296af7c9 Blue Swirl
}
1004 296af7c9 Blue Swirl
1005 296af7c9 Blue Swirl
void qemu_init_vcpu(void *_env)
1006 296af7c9 Blue Swirl
{
1007 296af7c9 Blue Swirl
    CPUState *env = _env;
1008 296af7c9 Blue Swirl
1009 296af7c9 Blue Swirl
    env->nr_cores = smp_cores;
1010 296af7c9 Blue Swirl
    env->nr_threads = smp_threads;
1011 0ab07c62 Jan Kiszka
    if (kvm_enabled()) {
1012 7e97cd88 Jan Kiszka
        qemu_kvm_start_vcpu(env);
1013 0ab07c62 Jan Kiszka
    } else {
1014 7e97cd88 Jan Kiszka
        qemu_tcg_init_vcpu(env);
1015 0ab07c62 Jan Kiszka
    }
1016 296af7c9 Blue Swirl
}
1017 296af7c9 Blue Swirl
1018 296af7c9 Blue Swirl
void qemu_notify_event(void)
1019 296af7c9 Blue Swirl
{
1020 296af7c9 Blue Swirl
    qemu_event_increment();
1021 296af7c9 Blue Swirl
}
1022 296af7c9 Blue Swirl
1023 b4a3d965 Jan Kiszka
void cpu_stop_current(void)
1024 296af7c9 Blue Swirl
{
1025 b4a3d965 Jan Kiszka
    if (cpu_single_env) {
1026 67bb172f Paolo Bonzini
        cpu_single_env->stop = 0;
1027 b4a3d965 Jan Kiszka
        cpu_single_env->stopped = 1;
1028 b4a3d965 Jan Kiszka
        cpu_exit(cpu_single_env);
1029 67bb172f Paolo Bonzini
        qemu_cond_signal(&qemu_pause_cond);
1030 b4a3d965 Jan Kiszka
    }
1031 296af7c9 Blue Swirl
}
1032 296af7c9 Blue Swirl
1033 296af7c9 Blue Swirl
void vm_stop(int reason)
1034 296af7c9 Blue Swirl
{
1035 b7680cb6 Jan Kiszka
    if (!qemu_thread_is_self(&io_thread)) {
1036 296af7c9 Blue Swirl
        qemu_system_vmstop_request(reason);
1037 296af7c9 Blue Swirl
        /*
1038 296af7c9 Blue Swirl
         * FIXME: should not return to device code in case
1039 296af7c9 Blue Swirl
         * vm_stop() has been requested.
1040 296af7c9 Blue Swirl
         */
1041 b4a3d965 Jan Kiszka
        cpu_stop_current();
1042 296af7c9 Blue Swirl
        return;
1043 296af7c9 Blue Swirl
    }
1044 296af7c9 Blue Swirl
    do_vm_stop(reason);
1045 296af7c9 Blue Swirl
}
1046 296af7c9 Blue Swirl
1047 296af7c9 Blue Swirl
#endif
1048 296af7c9 Blue Swirl
1049 6792a57b Jan Kiszka
static int tcg_cpu_exec(CPUState *env)
1050 296af7c9 Blue Swirl
{
1051 296af7c9 Blue Swirl
    int ret;
1052 296af7c9 Blue Swirl
#ifdef CONFIG_PROFILER
1053 296af7c9 Blue Swirl
    int64_t ti;
1054 296af7c9 Blue Swirl
#endif
1055 296af7c9 Blue Swirl
1056 296af7c9 Blue Swirl
#ifdef CONFIG_PROFILER
1057 296af7c9 Blue Swirl
    ti = profile_getclock();
1058 296af7c9 Blue Swirl
#endif
1059 296af7c9 Blue Swirl
    if (use_icount) {
1060 296af7c9 Blue Swirl
        int64_t count;
1061 296af7c9 Blue Swirl
        int decr;
1062 296af7c9 Blue Swirl
        qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
1063 296af7c9 Blue Swirl
        env->icount_decr.u16.low = 0;
1064 296af7c9 Blue Swirl
        env->icount_extra = 0;
1065 296af7c9 Blue Swirl
        count = qemu_icount_round (qemu_next_deadline());
1066 296af7c9 Blue Swirl
        qemu_icount += count;
1067 296af7c9 Blue Swirl
        decr = (count > 0xffff) ? 0xffff : count;
1068 296af7c9 Blue Swirl
        count -= decr;
1069 296af7c9 Blue Swirl
        env->icount_decr.u16.low = decr;
1070 296af7c9 Blue Swirl
        env->icount_extra = count;
1071 296af7c9 Blue Swirl
    }
1072 296af7c9 Blue Swirl
    ret = cpu_exec(env);
1073 296af7c9 Blue Swirl
#ifdef CONFIG_PROFILER
1074 296af7c9 Blue Swirl
    qemu_time += profile_getclock() - ti;
1075 296af7c9 Blue Swirl
#endif
1076 296af7c9 Blue Swirl
    if (use_icount) {
1077 296af7c9 Blue Swirl
        /* Fold pending instructions back into the
1078 296af7c9 Blue Swirl
           instruction counter, and clear the interrupt flag.  */
1079 296af7c9 Blue Swirl
        qemu_icount -= (env->icount_decr.u16.low
1080 296af7c9 Blue Swirl
                        + env->icount_extra);
1081 296af7c9 Blue Swirl
        env->icount_decr.u32 = 0;
1082 296af7c9 Blue Swirl
        env->icount_extra = 0;
1083 296af7c9 Blue Swirl
    }
1084 296af7c9 Blue Swirl
    return ret;
1085 296af7c9 Blue Swirl
}
1086 296af7c9 Blue Swirl
1087 472fb0c4 Jan Kiszka
bool cpu_exec_all(void)
1088 296af7c9 Blue Swirl
{
1089 9a36085b Jan Kiszka
    int r;
1090 9a36085b Jan Kiszka
1091 0ab07c62 Jan Kiszka
    if (next_cpu == NULL) {
1092 296af7c9 Blue Swirl
        next_cpu = first_cpu;
1093 0ab07c62 Jan Kiszka
    }
1094 c629a4bc Jan Kiszka
    for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
1095 345f4426 Jan Kiszka
        CPUState *env = next_cpu;
1096 296af7c9 Blue Swirl
1097 296af7c9 Blue Swirl
        qemu_clock_enable(vm_clock,
1098 345f4426 Jan Kiszka
                          (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
1099 296af7c9 Blue Swirl
1100 8cf3f22b Paolo Bonzini
#ifndef CONFIG_IOTHREAD
1101 0ab07c62 Jan Kiszka
        if (qemu_alarm_pending()) {
1102 296af7c9 Blue Swirl
            break;
1103 0ab07c62 Jan Kiszka
        }
1104 8cf3f22b Paolo Bonzini
#endif
1105 3c638d06 Jan Kiszka
        if (cpu_can_run(env)) {
1106 9a36085b Jan Kiszka
            if (kvm_enabled()) {
1107 6792a57b Jan Kiszka
                r = kvm_cpu_exec(env);
1108 9a36085b Jan Kiszka
                qemu_kvm_eat_signals(env);
1109 6792a57b Jan Kiszka
            } else {
1110 6792a57b Jan Kiszka
                r = tcg_cpu_exec(env);
1111 9a36085b Jan Kiszka
            }
1112 9a36085b Jan Kiszka
            if (r == EXCP_DEBUG) {
1113 83f338f7 Jan Kiszka
                cpu_handle_debug_exception(env);
1114 3c638d06 Jan Kiszka
                break;
1115 3c638d06 Jan Kiszka
            }
1116 df646dfd Paolo Bonzini
        } else if (env->stop || env->stopped) {
1117 296af7c9 Blue Swirl
            break;
1118 296af7c9 Blue Swirl
        }
1119 296af7c9 Blue Swirl
    }
1120 c629a4bc Jan Kiszka
    exit_request = 0;
1121 16400322 Jan Kiszka
    return !all_cpu_threads_idle();
1122 296af7c9 Blue Swirl
}
1123 296af7c9 Blue Swirl
1124 296af7c9 Blue Swirl
void set_numa_modes(void)
1125 296af7c9 Blue Swirl
{
1126 296af7c9 Blue Swirl
    CPUState *env;
1127 296af7c9 Blue Swirl
    int i;
1128 296af7c9 Blue Swirl
1129 296af7c9 Blue Swirl
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1130 296af7c9 Blue Swirl
        for (i = 0; i < nb_numa_nodes; i++) {
1131 296af7c9 Blue Swirl
            if (node_cpumask[i] & (1 << env->cpu_index)) {
1132 296af7c9 Blue Swirl
                env->numa_node = i;
1133 296af7c9 Blue Swirl
            }
1134 296af7c9 Blue Swirl
        }
1135 296af7c9 Blue Swirl
    }
1136 296af7c9 Blue Swirl
}
1137 296af7c9 Blue Swirl
1138 296af7c9 Blue Swirl
void set_cpu_log(const char *optarg)
1139 296af7c9 Blue Swirl
{
1140 296af7c9 Blue Swirl
    int mask;
1141 296af7c9 Blue Swirl
    const CPULogItem *item;
1142 296af7c9 Blue Swirl
1143 296af7c9 Blue Swirl
    mask = cpu_str_to_log_mask(optarg);
1144 296af7c9 Blue Swirl
    if (!mask) {
1145 296af7c9 Blue Swirl
        printf("Log items (comma separated):\n");
1146 296af7c9 Blue Swirl
        for (item = cpu_log_items; item->mask != 0; item++) {
1147 296af7c9 Blue Swirl
            printf("%-10s %s\n", item->name, item->help);
1148 296af7c9 Blue Swirl
        }
1149 296af7c9 Blue Swirl
        exit(1);
1150 296af7c9 Blue Swirl
    }
1151 296af7c9 Blue Swirl
    cpu_set_log(mask);
1152 296af7c9 Blue Swirl
}
1153 29e922b6 Blue Swirl
1154 29e922b6 Blue Swirl
/* Return the virtual CPU time, based on the instruction counter.  */
1155 29e922b6 Blue Swirl
int64_t cpu_get_icount(void)
1156 29e922b6 Blue Swirl
{
1157 29e922b6 Blue Swirl
    int64_t icount;
1158 29e922b6 Blue Swirl
    CPUState *env = cpu_single_env;;
1159 29e922b6 Blue Swirl
1160 29e922b6 Blue Swirl
    icount = qemu_icount;
1161 29e922b6 Blue Swirl
    if (env) {
1162 29e922b6 Blue Swirl
        if (!can_do_io(env)) {
1163 29e922b6 Blue Swirl
            fprintf(stderr, "Bad clock read\n");
1164 29e922b6 Blue Swirl
        }
1165 29e922b6 Blue Swirl
        icount -= (env->icount_decr.u16.low + env->icount_extra);
1166 29e922b6 Blue Swirl
    }
1167 29e922b6 Blue Swirl
    return qemu_icount_bias + (icount << icount_time_shift);
1168 29e922b6 Blue Swirl
}
1169 262353cb Blue Swirl
1170 9a78eead Stefan Weil
void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
1171 262353cb Blue Swirl
{
1172 262353cb Blue Swirl
    /* XXX: implement xxx_cpu_list for targets that still miss it */
1173 262353cb Blue Swirl
#if defined(cpu_list_id)
1174 262353cb Blue Swirl
    cpu_list_id(f, cpu_fprintf, optarg);
1175 262353cb Blue Swirl
#elif defined(cpu_list)
1176 262353cb Blue Swirl
    cpu_list(f, cpu_fprintf); /* deprecated */
1177 262353cb Blue Swirl
#endif
1178 262353cb Blue Swirl
}