Statistics
| Branch: | Revision:

root / cpus.c @ 9a78eead

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