Statistics
| Branch: | Revision:

root / cpus.c @ 8cf71710

History | View | Annotate | Download (24.7 kB)

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