blob: 36a6d1f350385c172ef37ca9e5fc0ba79520d4b5 [file] [log] [blame]
Blue Swirl296af7c2010-03-29 19:23:50 +00001/*
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"
Jan Kiszka262ea182010-07-06 10:49:57 +020033#include "exec-all.h"
Blue Swirl296af7c2010-03-29 19:23:50 +000034
35#include "cpus.h"
Marcelo Tosattia8486bc2010-10-11 15:31:16 -030036#include "compatfd.h"
Marcelo Tosattic0532a72010-10-11 15:31:21 -030037#ifdef CONFIG_LINUX
38#include <sys/prctl.h>
39#endif
Blue Swirl296af7c2010-03-29 19:23:50 +000040
Blue Swirl7277e022010-04-12 17:19:06 +000041#ifdef SIGRTMIN
42#define SIG_IPI (SIGRTMIN+4)
43#else
44#define SIG_IPI SIGUSR1
45#endif
46
Marcelo Tosattic0532a72010-10-11 15:31:21 -030047#ifndef PR_MCE_KILL
48#define PR_MCE_KILL 33
49#endif
50
Blue Swirl296af7c2010-03-29 19:23:50 +000051static CPUState *next_cpu;
52
53/***********************************************************/
54void 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
75void 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
84void 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
93void 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
Marcelo Tosatti3ae95012010-05-04 09:45:24 -0300102int cpu_is_stopped(CPUState *env)
103{
104 return !vm_running || env->stopped;
105}
106
Blue Swirl296af7c2010-03-29 19:23:50 +0000107static 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 monitor_protocol_event(QEVENT_STOP, NULL);
115 }
116}
117
118static int cpu_can_run(CPUState *env)
119{
120 if (env->stop)
121 return 0;
Paolo Bonzini55274a32010-04-07 00:11:09 +0200122 if (env->stopped || !vm_running)
Blue Swirl296af7c2010-03-29 19:23:50 +0000123 return 0;
124 return 1;
125}
126
127static int cpu_has_work(CPUState *env)
128{
129 if (env->stop)
130 return 1;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300131 if (env->queued_work_first)
132 return 1;
Paolo Bonzini55274a32010-04-07 00:11:09 +0200133 if (env->stopped || !vm_running)
Blue Swirl296af7c2010-03-29 19:23:50 +0000134 return 0;
135 if (!env->halted)
136 return 1;
137 if (qemu_cpu_has_work(env))
138 return 1;
139 return 0;
140}
141
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200142static int any_cpu_has_work(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000143{
144 CPUState *env;
145
146 for (env = first_cpu; env != NULL; env = env->next_cpu)
147 if (cpu_has_work(env))
148 return 1;
149 return 0;
150}
151
Jan Kiszka3c638d02010-06-25 16:56:56 +0200152static void cpu_debug_handler(CPUState *env)
153{
154 gdb_set_stop_cpu(env);
155 debug_requested = EXCP_DEBUG;
156 vm_stop(EXCP_DEBUG);
157}
158
Blue Swirl296af7c2010-03-29 19:23:50 +0000159#ifndef _WIN32
160static int io_thread_fd = -1;
161
162static void qemu_event_increment(void)
163{
164 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000165 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000166 ssize_t ret;
167
168 if (io_thread_fd == -1)
169 return;
170
171 do {
172 ret = write(io_thread_fd, &val, sizeof(val));
173 } while (ret < 0 && errno == EINTR);
174
175 /* EAGAIN is fine, a read must be pending. */
176 if (ret < 0 && errno != EAGAIN) {
177 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
178 strerror(errno));
179 exit (1);
180 }
181}
182
183static void qemu_event_read(void *opaque)
184{
185 int fd = (unsigned long)opaque;
186 ssize_t len;
187 char buffer[512];
188
189 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
190 do {
191 len = read(fd, buffer, sizeof(buffer));
192 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
193}
194
195static int qemu_event_init(void)
196{
197 int err;
198 int fds[2];
199
200 err = qemu_eventfd(fds);
201 if (err == -1)
202 return -errno;
203
204 err = fcntl_setfl(fds[0], O_NONBLOCK);
205 if (err < 0)
206 goto fail;
207
208 err = fcntl_setfl(fds[1], O_NONBLOCK);
209 if (err < 0)
210 goto fail;
211
212 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
213 (void *)(unsigned long)fds[0]);
214
215 io_thread_fd = fds[1];
216 return 0;
217
218fail:
219 close(fds[0]);
220 close(fds[1]);
221 return err;
222}
223#else
224HANDLE qemu_event_handle;
225
226static void dummy_event_handler(void *opaque)
227{
228}
229
230static int qemu_event_init(void)
231{
232 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
233 if (!qemu_event_handle) {
234 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
235 return -1;
236 }
237 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
238 return 0;
239}
240
241static void qemu_event_increment(void)
242{
243 if (!SetEvent(qemu_event_handle)) {
244 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
245 GetLastError());
246 exit (1);
247 }
248}
249#endif
250
251#ifndef CONFIG_IOTHREAD
252int qemu_init_main_loop(void)
253{
Jan Kiszka3c638d02010-06-25 16:56:56 +0200254 cpu_set_debug_excp_handler(cpu_debug_handler);
255
Blue Swirl296af7c2010-03-29 19:23:50 +0000256 return qemu_event_init();
257}
258
Blue Swirl7277e022010-04-12 17:19:06 +0000259void qemu_main_loop_start(void)
260{
261}
262
Blue Swirl296af7c2010-03-29 19:23:50 +0000263void qemu_init_vcpu(void *_env)
264{
265 CPUState *env = _env;
266
267 env->nr_cores = smp_cores;
268 env->nr_threads = smp_threads;
269 if (kvm_enabled())
270 kvm_init_vcpu(env);
271 return;
272}
273
274int qemu_cpu_self(void *env)
275{
276 return 1;
277}
278
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300279void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
280{
281 func(data);
282}
283
Blue Swirl296af7c2010-03-29 19:23:50 +0000284void resume_all_vcpus(void)
285{
286}
287
288void pause_all_vcpus(void)
289{
290}
291
292void qemu_cpu_kick(void *env)
293{
294 return;
295}
296
297void qemu_notify_event(void)
298{
299 CPUState *env = cpu_single_env;
300
301 qemu_event_increment ();
302 if (env) {
303 cpu_exit(env);
304 }
305 if (next_cpu && env != next_cpu) {
306 cpu_exit(next_cpu);
307 }
308}
309
310void qemu_mutex_lock_iothread(void) {}
311void qemu_mutex_unlock_iothread(void) {}
312
313void vm_stop(int reason)
314{
315 do_vm_stop(reason);
316}
317
318#else /* CONFIG_IOTHREAD */
319
320#include "qemu-thread.h"
321
322QemuMutex qemu_global_mutex;
323static QemuMutex qemu_fair_mutex;
324
325static QemuThread io_thread;
326
327static QemuThread *tcg_cpu_thread;
328static QemuCond *tcg_halt_cond;
329
330static int qemu_system_ready;
331/* cpu creation */
332static QemuCond qemu_cpu_cond;
333/* system init */
334static QemuCond qemu_system_cond;
335static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300336static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000337
Paolo Bonzini55541c82010-06-03 15:20:32 +0200338static void tcg_init_ipi(void);
339static void kvm_init_ipi(CPUState *env);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300340static sigset_t block_io_signals(void);
341
342/* If we have signalfd, we mask out the signals we want to handle and then
343 * use signalfd to listen for them. We rely on whatever the current signal
344 * handler is to dispatch the signals when we receive them.
345 */
346static void sigfd_handler(void *opaque)
347{
348 int fd = (unsigned long) opaque;
349 struct qemu_signalfd_siginfo info;
350 struct sigaction action;
351 ssize_t len;
352
353 while (1) {
354 do {
355 len = read(fd, &info, sizeof(info));
356 } while (len == -1 && errno == EINTR);
357
358 if (len == -1 && errno == EAGAIN) {
359 break;
360 }
361
362 if (len != sizeof(info)) {
363 printf("read from sigfd returned %zd: %m\n", len);
364 return;
365 }
366
367 sigaction(info.ssi_signo, NULL, &action);
368 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
369 action.sa_sigaction(info.ssi_signo,
370 (siginfo_t *)&info, NULL);
371 } else if (action.sa_handler) {
372 action.sa_handler(info.ssi_signo);
373 }
374 }
375}
376
377static int qemu_signalfd_init(sigset_t mask)
378{
379 int sigfd;
380
381 sigfd = qemu_signalfd(&mask);
382 if (sigfd == -1) {
383 fprintf(stderr, "failed to create signalfd\n");
384 return -errno;
385 }
386
387 fcntl_setfl(sigfd, O_NONBLOCK);
388
389 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
390 (void *)(unsigned long) sigfd);
391
392 return 0;
393}
Blue Swirl296af7c2010-03-29 19:23:50 +0000394
395int qemu_init_main_loop(void)
396{
397 int ret;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300398 sigset_t blocked_signals;
Blue Swirl296af7c2010-03-29 19:23:50 +0000399
Jan Kiszka3c638d02010-06-25 16:56:56 +0200400 cpu_set_debug_excp_handler(cpu_debug_handler);
401
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300402 blocked_signals = block_io_signals();
403
404 ret = qemu_signalfd_init(blocked_signals);
405 if (ret)
406 return ret;
407
408 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000409 ret = qemu_event_init();
410 if (ret)
411 return ret;
412
413 qemu_cond_init(&qemu_pause_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200414 qemu_cond_init(&qemu_system_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000415 qemu_mutex_init(&qemu_fair_mutex);
416 qemu_mutex_init(&qemu_global_mutex);
417 qemu_mutex_lock(&qemu_global_mutex);
418
Blue Swirl296af7c2010-03-29 19:23:50 +0000419 qemu_thread_self(&io_thread);
420
421 return 0;
422}
423
Blue Swirl7277e022010-04-12 17:19:06 +0000424void qemu_main_loop_start(void)
425{
426 qemu_system_ready = 1;
427 qemu_cond_broadcast(&qemu_system_cond);
428}
429
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300430void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
431{
432 struct qemu_work_item wi;
433
434 if (qemu_cpu_self(env)) {
435 func(data);
436 return;
437 }
438
439 wi.func = func;
440 wi.data = data;
441 if (!env->queued_work_first)
442 env->queued_work_first = &wi;
443 else
444 env->queued_work_last->next = &wi;
445 env->queued_work_last = &wi;
446 wi.next = NULL;
447 wi.done = false;
448
449 qemu_cpu_kick(env);
450 while (!wi.done) {
451 CPUState *self_env = cpu_single_env;
452
453 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
454 cpu_single_env = self_env;
455 }
456}
457
458static void flush_queued_work(CPUState *env)
459{
460 struct qemu_work_item *wi;
461
462 if (!env->queued_work_first)
463 return;
464
465 while ((wi = env->queued_work_first)) {
466 env->queued_work_first = wi->next;
467 wi->func(wi->data);
468 wi->done = true;
469 }
470 env->queued_work_last = NULL;
471 qemu_cond_broadcast(&qemu_work_cond);
472}
473
Blue Swirl296af7c2010-03-29 19:23:50 +0000474static void qemu_wait_io_event_common(CPUState *env)
475{
476 if (env->stop) {
477 env->stop = 0;
478 env->stopped = 1;
479 qemu_cond_signal(&qemu_pause_cond);
480 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300481 flush_queued_work(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000482}
483
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200484static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000485{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200486 CPUState *env;
487
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200488 while (!any_cpu_has_work())
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200489 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
Blue Swirl296af7c2010-03-29 19:23:50 +0000490
491 qemu_mutex_unlock(&qemu_global_mutex);
492
493 /*
494 * Users of qemu_global_mutex can be starved, having no chance
495 * to acquire it since this path will get to it first.
496 * So use another lock to provide fairness.
497 */
498 qemu_mutex_lock(&qemu_fair_mutex);
499 qemu_mutex_unlock(&qemu_fair_mutex);
500
501 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200502
503 for (env = first_cpu; env != NULL; env = env->next_cpu) {
504 qemu_wait_io_event_common(env);
505 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000506}
507
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300508static void sigbus_reraise(void)
509{
510 sigset_t set;
511 struct sigaction action;
512
513 memset(&action, 0, sizeof(action));
514 action.sa_handler = SIG_DFL;
515 if (!sigaction(SIGBUS, &action, NULL)) {
516 raise(SIGBUS);
517 sigemptyset(&set);
518 sigaddset(&set, SIGBUS);
519 sigprocmask(SIG_UNBLOCK, &set, NULL);
520 }
521 perror("Failed to re-raise SIGBUS!\n");
522 abort();
523}
524
525static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
526 void *ctx)
527{
528#if defined(TARGET_I386)
529 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr))
530#endif
531 sigbus_reraise();
532}
533
Blue Swirl296af7c2010-03-29 19:23:50 +0000534static void qemu_kvm_eat_signal(CPUState *env, int timeout)
535{
536 struct timespec ts;
537 int r, e;
538 siginfo_t siginfo;
539 sigset_t waitset;
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300540 sigset_t chkset;
Blue Swirl296af7c2010-03-29 19:23:50 +0000541
542 ts.tv_sec = timeout / 1000;
543 ts.tv_nsec = (timeout % 1000) * 1000000;
544
545 sigemptyset(&waitset);
546 sigaddset(&waitset, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300547 sigaddset(&waitset, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000548
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300549 do {
550 qemu_mutex_unlock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000551
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300552 r = sigtimedwait(&waitset, &siginfo, &ts);
553 e = errno;
554
555 qemu_mutex_lock(&qemu_global_mutex);
556
557 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
558 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
559 exit(1);
560 }
561
562 switch (r) {
563 case SIGBUS:
564#ifdef TARGET_I386
565 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr))
566#endif
567 sigbus_reraise();
568 break;
569 default:
570 break;
571 }
572
573 r = sigpending(&chkset);
574 if (r == -1) {
575 fprintf(stderr, "sigpending: %s\n", strerror(e));
576 exit(1);
577 }
578 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Blue Swirl296af7c2010-03-29 19:23:50 +0000579}
580
581static void qemu_kvm_wait_io_event(CPUState *env)
582{
583 while (!cpu_has_work(env))
584 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
585
586 qemu_kvm_eat_signal(env, 0);
587 qemu_wait_io_event_common(env);
588}
589
590static int qemu_cpu_exec(CPUState *env);
591
592static void *kvm_cpu_thread_fn(void *arg)
593{
594 CPUState *env = arg;
595
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300596 qemu_mutex_lock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000597 qemu_thread_self(env->thread);
598 if (kvm_enabled())
599 kvm_init_vcpu(env);
600
Paolo Bonzini55541c82010-06-03 15:20:32 +0200601 kvm_init_ipi(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000602
603 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000604 env->created = 1;
605 qemu_cond_signal(&qemu_cpu_cond);
606
607 /* and wait for machine initialization */
608 while (!qemu_system_ready)
609 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
610
611 while (1) {
612 if (cpu_can_run(env))
613 qemu_cpu_exec(env);
614 qemu_kvm_wait_io_event(env);
615 }
616
617 return NULL;
618}
619
620static void *tcg_cpu_thread_fn(void *arg)
621{
622 CPUState *env = arg;
623
Paolo Bonzini55541c82010-06-03 15:20:32 +0200624 tcg_init_ipi();
Blue Swirl296af7c2010-03-29 19:23:50 +0000625 qemu_thread_self(env->thread);
626
627 /* signal CPU creation */
628 qemu_mutex_lock(&qemu_global_mutex);
629 for (env = first_cpu; env != NULL; env = env->next_cpu)
630 env->created = 1;
631 qemu_cond_signal(&qemu_cpu_cond);
632
633 /* and wait for machine initialization */
634 while (!qemu_system_ready)
635 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
636
637 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200638 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200639 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000640 }
641
642 return NULL;
643}
644
645void qemu_cpu_kick(void *_env)
646{
647 CPUState *env = _env;
648 qemu_cond_broadcast(env->halt_cond);
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300649 qemu_thread_signal(env->thread, SIG_IPI);
Blue Swirl296af7c2010-03-29 19:23:50 +0000650}
651
652int qemu_cpu_self(void *_env)
653{
654 CPUState *env = _env;
655 QemuThread this;
656
657 qemu_thread_self(&this);
658
659 return qemu_thread_equal(&this, env->thread);
660}
661
662static void cpu_signal(int sig)
663{
664 if (cpu_single_env)
665 cpu_exit(cpu_single_env);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300666 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000667}
668
Paolo Bonzini55541c82010-06-03 15:20:32 +0200669static void tcg_init_ipi(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000670{
671 sigset_t set;
672 struct sigaction sigact;
673
Paolo Bonzini55541c82010-06-03 15:20:32 +0200674 memset(&sigact, 0, sizeof(sigact));
675 sigact.sa_handler = cpu_signal;
676 sigaction(SIG_IPI, &sigact, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000677
678 sigemptyset(&set);
679 sigaddset(&set, SIG_IPI);
680 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000681}
682
683static void dummy_signal(int sig)
684{
685}
686
Paolo Bonzini55541c82010-06-03 15:20:32 +0200687static void kvm_init_ipi(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000688{
689 int r;
690 sigset_t set;
691 struct sigaction sigact;
692
Blue Swirl296af7c2010-03-29 19:23:50 +0000693 memset(&sigact, 0, sizeof(sigact));
694 sigact.sa_handler = dummy_signal;
695 sigaction(SIG_IPI, &sigact, NULL);
696
Paolo Bonzini55541c82010-06-03 15:20:32 +0200697 pthread_sigmask(SIG_BLOCK, NULL, &set);
698 sigdelset(&set, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300699 sigdelset(&set, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000700 r = kvm_set_signal_mask(env, &set);
701 if (r) {
702 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
703 exit(1);
704 }
705}
706
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300707static sigset_t block_io_signals(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000708{
709 sigset_t set;
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300710 struct sigaction action;
Blue Swirl296af7c2010-03-29 19:23:50 +0000711
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300712 /* SIGUSR2 used by posix-aio-compat.c */
Blue Swirl296af7c2010-03-29 19:23:50 +0000713 sigemptyset(&set);
714 sigaddset(&set, SIGUSR2);
Blue Swirl296af7c2010-03-29 19:23:50 +0000715 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
716
717 sigemptyset(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300718 sigaddset(&set, SIGIO);
719 sigaddset(&set, SIGALRM);
Blue Swirl296af7c2010-03-29 19:23:50 +0000720 sigaddset(&set, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300721 sigaddset(&set, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000722 pthread_sigmask(SIG_BLOCK, &set, NULL);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300723
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300724 memset(&action, 0, sizeof(action));
725 action.sa_flags = SA_SIGINFO;
726 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
727 sigaction(SIGBUS, &action, NULL);
728 prctl(PR_MCE_KILL, 1, 1, 0, 0);
729
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300730 return set;
Blue Swirl296af7c2010-03-29 19:23:50 +0000731}
732
Blue Swirl296af7c2010-03-29 19:23:50 +0000733void qemu_mutex_lock_iothread(void)
734{
735 if (kvm_enabled()) {
736 qemu_mutex_lock(&qemu_fair_mutex);
737 qemu_mutex_lock(&qemu_global_mutex);
738 qemu_mutex_unlock(&qemu_fair_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300739 } else {
740 qemu_mutex_lock(&qemu_fair_mutex);
741 if (qemu_mutex_trylock(&qemu_global_mutex)) {
742 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
743 qemu_mutex_lock(&qemu_global_mutex);
744 }
745 qemu_mutex_unlock(&qemu_fair_mutex);
746 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000747}
748
749void qemu_mutex_unlock_iothread(void)
750{
751 qemu_mutex_unlock(&qemu_global_mutex);
752}
753
754static int all_vcpus_paused(void)
755{
756 CPUState *penv = first_cpu;
757
758 while (penv) {
759 if (!penv->stopped)
760 return 0;
761 penv = (CPUState *)penv->next_cpu;
762 }
763
764 return 1;
765}
766
767void pause_all_vcpus(void)
768{
769 CPUState *penv = first_cpu;
770
771 while (penv) {
772 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000773 qemu_cpu_kick(penv);
774 penv = (CPUState *)penv->next_cpu;
775 }
776
777 while (!all_vcpus_paused()) {
778 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
779 penv = first_cpu;
780 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300781 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000782 penv = (CPUState *)penv->next_cpu;
783 }
784 }
785}
786
787void resume_all_vcpus(void)
788{
789 CPUState *penv = first_cpu;
790
791 while (penv) {
792 penv->stop = 0;
793 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000794 qemu_cpu_kick(penv);
795 penv = (CPUState *)penv->next_cpu;
796 }
797}
798
799static void tcg_init_vcpu(void *_env)
800{
801 CPUState *env = _env;
802 /* share a single thread for all cpus with TCG */
803 if (!tcg_cpu_thread) {
804 env->thread = qemu_mallocz(sizeof(QemuThread));
805 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
806 qemu_cond_init(env->halt_cond);
807 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
808 while (env->created == 0)
809 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
810 tcg_cpu_thread = env->thread;
811 tcg_halt_cond = env->halt_cond;
812 } else {
813 env->thread = tcg_cpu_thread;
814 env->halt_cond = tcg_halt_cond;
815 }
816}
817
818static void kvm_start_vcpu(CPUState *env)
819{
820 env->thread = qemu_mallocz(sizeof(QemuThread));
821 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
822 qemu_cond_init(env->halt_cond);
823 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
824 while (env->created == 0)
825 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
826}
827
828void qemu_init_vcpu(void *_env)
829{
830 CPUState *env = _env;
831
832 env->nr_cores = smp_cores;
833 env->nr_threads = smp_threads;
834 if (kvm_enabled())
835 kvm_start_vcpu(env);
836 else
837 tcg_init_vcpu(env);
838}
839
840void qemu_notify_event(void)
841{
842 qemu_event_increment();
843}
844
845static void qemu_system_vmstop_request(int reason)
846{
847 vmstop_requested = reason;
848 qemu_notify_event();
849}
850
851void vm_stop(int reason)
852{
853 QemuThread me;
854 qemu_thread_self(&me);
855
856 if (!qemu_thread_equal(&me, &io_thread)) {
857 qemu_system_vmstop_request(reason);
858 /*
859 * FIXME: should not return to device code in case
860 * vm_stop() has been requested.
861 */
862 if (cpu_single_env) {
863 cpu_exit(cpu_single_env);
864 cpu_single_env->stop = 1;
865 }
866 return;
867 }
868 do_vm_stop(reason);
869}
870
871#endif
872
873static int qemu_cpu_exec(CPUState *env)
874{
875 int ret;
876#ifdef CONFIG_PROFILER
877 int64_t ti;
878#endif
879
880#ifdef CONFIG_PROFILER
881 ti = profile_getclock();
882#endif
883 if (use_icount) {
884 int64_t count;
885 int decr;
886 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
887 env->icount_decr.u16.low = 0;
888 env->icount_extra = 0;
889 count = qemu_icount_round (qemu_next_deadline());
890 qemu_icount += count;
891 decr = (count > 0xffff) ? 0xffff : count;
892 count -= decr;
893 env->icount_decr.u16.low = decr;
894 env->icount_extra = count;
895 }
896 ret = cpu_exec(env);
897#ifdef CONFIG_PROFILER
898 qemu_time += profile_getclock() - ti;
899#endif
900 if (use_icount) {
901 /* Fold pending instructions back into the
902 instruction counter, and clear the interrupt flag. */
903 qemu_icount -= (env->icount_decr.u16.low
904 + env->icount_extra);
905 env->icount_decr.u32 = 0;
906 env->icount_extra = 0;
907 }
908 return ret;
909}
910
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200911bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000912{
Blue Swirl296af7c2010-03-29 19:23:50 +0000913 if (next_cpu == NULL)
914 next_cpu = first_cpu;
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200915 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +0200916 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000917
918 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +0200919 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +0000920
921 if (qemu_alarm_pending())
922 break;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200923 if (cpu_can_run(env)) {
924 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
925 break;
926 }
927 } else if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000928 break;
929 }
930 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200931 exit_request = 0;
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200932 return any_cpu_has_work();
Blue Swirl296af7c2010-03-29 19:23:50 +0000933}
934
935void set_numa_modes(void)
936{
937 CPUState *env;
938 int i;
939
940 for (env = first_cpu; env != NULL; env = env->next_cpu) {
941 for (i = 0; i < nb_numa_nodes; i++) {
942 if (node_cpumask[i] & (1 << env->cpu_index)) {
943 env->numa_node = i;
944 }
945 }
946 }
947}
948
949void set_cpu_log(const char *optarg)
950{
951 int mask;
952 const CPULogItem *item;
953
954 mask = cpu_str_to_log_mask(optarg);
955 if (!mask) {
956 printf("Log items (comma separated):\n");
957 for (item = cpu_log_items; item->mask != 0; item++) {
958 printf("%-10s %s\n", item->name, item->help);
959 }
960 exit(1);
961 }
962 cpu_set_log(mask);
963}
Blue Swirl29e922b2010-03-29 19:24:00 +0000964
965/* Return the virtual CPU time, based on the instruction counter. */
966int64_t cpu_get_icount(void)
967{
968 int64_t icount;
969 CPUState *env = cpu_single_env;;
970
971 icount = qemu_icount;
972 if (env) {
973 if (!can_do_io(env)) {
974 fprintf(stderr, "Bad clock read\n");
975 }
976 icount -= (env->icount_decr.u16.low + env->icount_extra);
977 }
978 return qemu_icount_bias + (icount << icount_time_shift);
979}
Blue Swirl262353c2010-05-04 19:55:35 +0000980
981void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
982 const char *optarg)
983{
984 /* XXX: implement xxx_cpu_list for targets that still miss it */
985#if defined(cpu_list_id)
986 cpu_list_id(f, cpu_fprintf, optarg);
987#elif defined(cpu_list)
988 cpu_list(f, cpu_fprintf); /* deprecated */
989#endif
990}