Statistics
| Branch: | Revision:

root / cpus.c @ ff48eb5f

History | View | Annotate | Download (23.2 kB)

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