Statistics
| Branch: | Revision:

root / cpus.c @ 38145df2

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