Statistics
| Branch: | Revision:

root / cpus.c @ d0f294ce

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