Statistics
| Branch: | Revision:

root / cpus.c @ 46d62fac

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