Statistics
| Branch: | Revision:

root / cpus.c @ 67bb172f

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