Statistics
| Branch: | Revision:

root / cpus.c @ f8bf8606

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