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