blob: 84792f0a0e91f57ae9226441a72a7c267b64a98f [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);
Michael S. Tsirkin55df6f32010-11-22 19:52:22 +0200114 qemu_aio_flush();
115 bdrv_flush_all();
Blue Swirl296af7c2010-03-29 19:23:50 +0000116 monitor_protocol_event(QEVENT_STOP, NULL);
117 }
118}
119
120static int cpu_can_run(CPUState *env)
121{
122 if (env->stop)
123 return 0;
Paolo Bonzini55274a32010-04-07 00:11:09 +0200124 if (env->stopped || !vm_running)
Blue Swirl296af7c2010-03-29 19:23:50 +0000125 return 0;
126 return 1;
127}
128
129static int cpu_has_work(CPUState *env)
130{
131 if (env->stop)
132 return 1;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300133 if (env->queued_work_first)
134 return 1;
Paolo Bonzini55274a32010-04-07 00:11:09 +0200135 if (env->stopped || !vm_running)
Blue Swirl296af7c2010-03-29 19:23:50 +0000136 return 0;
137 if (!env->halted)
138 return 1;
139 if (qemu_cpu_has_work(env))
140 return 1;
141 return 0;
142}
143
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200144static int any_cpu_has_work(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000145{
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
Jan Kiszka3c638d02010-06-25 16:56:56 +0200154static 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
Blue Swirl296af7c2010-03-29 19:23:50 +0000161#ifndef _WIN32
162static int io_thread_fd = -1;
163
164static void qemu_event_increment(void)
165{
166 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000167 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000168 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
185static 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
197static 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
220fail:
221 close(fds[0]);
222 close(fds[1]);
223 return err;
224}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100225
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100226static void dummy_signal(int sig)
227{
228}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100229
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100230/* If we have signalfd, we mask out the signals we want to handle and then
231 * use signalfd to listen for them. We rely on whatever the current signal
232 * handler is to dispatch the signals when we receive them.
233 */
234static void sigfd_handler(void *opaque)
235{
236 int fd = (unsigned long) opaque;
237 struct qemu_signalfd_siginfo info;
238 struct sigaction action;
239 ssize_t len;
240
241 while (1) {
242 do {
243 len = read(fd, &info, sizeof(info));
244 } while (len == -1 && errno == EINTR);
245
246 if (len == -1 && errno == EAGAIN) {
247 break;
248 }
249
250 if (len != sizeof(info)) {
251 printf("read from sigfd returned %zd: %m\n", len);
252 return;
253 }
254
255 sigaction(info.ssi_signo, NULL, &action);
256 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
257 action.sa_sigaction(info.ssi_signo,
258 (siginfo_t *)&info, NULL);
259 } else if (action.sa_handler) {
260 action.sa_handler(info.ssi_signo);
261 }
262 }
263}
264
265static int qemu_signalfd_init(sigset_t mask)
266{
267 int sigfd;
268
269 sigfd = qemu_signalfd(&mask);
270 if (sigfd == -1) {
271 fprintf(stderr, "failed to create signalfd\n");
272 return -errno;
273 }
274
275 fcntl_setfl(sigfd, O_NONBLOCK);
276
277 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
278 (void *)(unsigned long) sigfd);
279
280 return 0;
281}
282
Jan Kiszka9a360852011-02-01 22:15:55 +0100283static void sigbus_reraise(void);
284
285static void qemu_kvm_eat_signals(CPUState *env)
286{
287 struct timespec ts = { 0, 0 };
288 siginfo_t siginfo;
289 sigset_t waitset;
290 sigset_t chkset;
291 int r;
292
293 sigemptyset(&waitset);
294 sigaddset(&waitset, SIG_IPI);
295 sigaddset(&waitset, SIGBUS);
296
297 do {
298 r = sigtimedwait(&waitset, &siginfo, &ts);
299 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
300 perror("sigtimedwait");
301 exit(1);
302 }
303
304 switch (r) {
305#ifdef CONFIG_IOTHREAD
306 case SIGBUS:
307 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
308 sigbus_reraise();
309 }
310 break;
311#endif
312 default:
313 break;
314 }
315
316 r = sigpending(&chkset);
317 if (r == -1) {
318 perror("sigpending");
319 exit(1);
320 }
321 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
322}
323
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100324#else /* _WIN32 */
325
Blue Swirl296af7c2010-03-29 19:23:50 +0000326HANDLE qemu_event_handle;
327
328static void dummy_event_handler(void *opaque)
329{
330}
331
332static int qemu_event_init(void)
333{
334 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
335 if (!qemu_event_handle) {
336 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
337 return -1;
338 }
339 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
340 return 0;
341}
342
343static void qemu_event_increment(void)
344{
345 if (!SetEvent(qemu_event_handle)) {
346 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
347 GetLastError());
348 exit (1);
349 }
350}
Jan Kiszka9a360852011-02-01 22:15:55 +0100351
352static void qemu_kvm_eat_signals(CPUState *env)
353{
354}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100355#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000356
357#ifndef CONFIG_IOTHREAD
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100358static void qemu_kvm_init_cpu_signals(CPUState *env)
359{
360#ifndef _WIN32
361 int r;
362 sigset_t set;
363 struct sigaction sigact;
364
365 memset(&sigact, 0, sizeof(sigact));
366 sigact.sa_handler = dummy_signal;
367 sigaction(SIG_IPI, &sigact, NULL);
368
369 sigemptyset(&set);
370 sigaddset(&set, SIG_IPI);
371 pthread_sigmask(SIG_BLOCK, &set, NULL);
372
373 pthread_sigmask(SIG_BLOCK, NULL, &set);
374 sigdelset(&set, SIG_IPI);
375 sigdelset(&set, SIGBUS);
376 r = kvm_set_signal_mask(env, &set);
377 if (r) {
378 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
379 exit(1);
380 }
381#endif
382}
383
Blue Swirl296af7c2010-03-29 19:23:50 +0000384int qemu_init_main_loop(void)
385{
Jan Kiszkad0f294c2011-02-01 22:15:56 +0100386#ifndef _WIN32
387 sigset_t blocked_signals;
388 int ret;
389
390 sigemptyset(&blocked_signals);
391
392 ret = qemu_signalfd_init(blocked_signals);
393 if (ret) {
394 return ret;
395 }
396#endif
Jan Kiszka3c638d02010-06-25 16:56:56 +0200397 cpu_set_debug_excp_handler(cpu_debug_handler);
398
Blue Swirl296af7c2010-03-29 19:23:50 +0000399 return qemu_event_init();
400}
401
Blue Swirl7277e022010-04-12 17:19:06 +0000402void qemu_main_loop_start(void)
403{
404}
405
Blue Swirl296af7c2010-03-29 19:23:50 +0000406void qemu_init_vcpu(void *_env)
407{
408 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100409 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000410
411 env->nr_cores = smp_cores;
412 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100413
414 if (kvm_enabled()) {
415 r = kvm_init_vcpu(env);
416 if (r < 0) {
417 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
418 exit(1);
419 }
Jan Kiszkaff48eb52011-02-01 22:15:53 +0100420 qemu_kvm_init_cpu_signals(env);
Jan Kiszka84b49152011-02-01 22:15:50 +0100421 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000422}
423
424int qemu_cpu_self(void *env)
425{
426 return 1;
427}
428
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300429void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
430{
431 func(data);
432}
433
Blue Swirl296af7c2010-03-29 19:23:50 +0000434void resume_all_vcpus(void)
435{
436}
437
438void pause_all_vcpus(void)
439{
440}
441
442void qemu_cpu_kick(void *env)
443{
444 return;
445}
446
447void qemu_notify_event(void)
448{
449 CPUState *env = cpu_single_env;
450
451 qemu_event_increment ();
452 if (env) {
453 cpu_exit(env);
454 }
455 if (next_cpu && env != next_cpu) {
456 cpu_exit(next_cpu);
457 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100458 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000459}
460
461void qemu_mutex_lock_iothread(void) {}
462void qemu_mutex_unlock_iothread(void) {}
463
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100464void cpu_stop_current(void)
465{
466}
467
Blue Swirl296af7c2010-03-29 19:23:50 +0000468void vm_stop(int reason)
469{
470 do_vm_stop(reason);
471}
472
473#else /* CONFIG_IOTHREAD */
474
475#include "qemu-thread.h"
476
477QemuMutex qemu_global_mutex;
478static QemuMutex qemu_fair_mutex;
479
480static QemuThread io_thread;
481
482static QemuThread *tcg_cpu_thread;
483static QemuCond *tcg_halt_cond;
484
485static int qemu_system_ready;
486/* cpu creation */
487static QemuCond qemu_cpu_cond;
488/* system init */
489static QemuCond qemu_system_cond;
490static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300491static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000492
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100493static void cpu_signal(int sig)
494{
495 if (cpu_single_env) {
496 cpu_exit(cpu_single_env);
497 }
498 exit_request = 1;
499}
500
501static void qemu_kvm_init_cpu_signals(CPUState *env)
502{
503 int r;
504 sigset_t set;
505 struct sigaction sigact;
506
507 memset(&sigact, 0, sizeof(sigact));
508 sigact.sa_handler = dummy_signal;
509 sigaction(SIG_IPI, &sigact, NULL);
510
511 pthread_sigmask(SIG_BLOCK, NULL, &set);
512 sigdelset(&set, SIG_IPI);
513 sigdelset(&set, SIGBUS);
514 r = kvm_set_signal_mask(env, &set);
515 if (r) {
516 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
517 exit(1);
518 }
519}
520
521static void qemu_tcg_init_cpu_signals(void)
522{
523 sigset_t set;
524 struct sigaction sigact;
525
526 memset(&sigact, 0, sizeof(sigact));
527 sigact.sa_handler = cpu_signal;
528 sigaction(SIG_IPI, &sigact, NULL);
529
530 sigemptyset(&set);
531 sigaddset(&set, SIG_IPI);
532 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
533}
534
535static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
536 void *ctx);
537
538static sigset_t block_io_signals(void)
539{
540 sigset_t set;
541 struct sigaction action;
542
543 /* SIGUSR2 used by posix-aio-compat.c */
544 sigemptyset(&set);
545 sigaddset(&set, SIGUSR2);
546 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
547
548 sigemptyset(&set);
549 sigaddset(&set, SIGIO);
550 sigaddset(&set, SIGALRM);
551 sigaddset(&set, SIG_IPI);
552 sigaddset(&set, SIGBUS);
553 pthread_sigmask(SIG_BLOCK, &set, NULL);
554
555 memset(&action, 0, sizeof(action));
556 action.sa_flags = SA_SIGINFO;
557 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
558 sigaction(SIGBUS, &action, NULL);
559 prctl(PR_MCE_KILL, 1, 1, 0, 0);
560
561 return set;
562}
563
Blue Swirl296af7c2010-03-29 19:23:50 +0000564int qemu_init_main_loop(void)
565{
566 int ret;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300567 sigset_t blocked_signals;
Blue Swirl296af7c2010-03-29 19:23:50 +0000568
Jan Kiszka3c638d02010-06-25 16:56:56 +0200569 cpu_set_debug_excp_handler(cpu_debug_handler);
570
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300571 blocked_signals = block_io_signals();
572
573 ret = qemu_signalfd_init(blocked_signals);
574 if (ret)
575 return ret;
576
577 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000578 ret = qemu_event_init();
579 if (ret)
580 return ret;
581
582 qemu_cond_init(&qemu_pause_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200583 qemu_cond_init(&qemu_system_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000584 qemu_mutex_init(&qemu_fair_mutex);
585 qemu_mutex_init(&qemu_global_mutex);
586 qemu_mutex_lock(&qemu_global_mutex);
587
Blue Swirl296af7c2010-03-29 19:23:50 +0000588 qemu_thread_self(&io_thread);
589
590 return 0;
591}
592
Blue Swirl7277e022010-04-12 17:19:06 +0000593void qemu_main_loop_start(void)
594{
595 qemu_system_ready = 1;
596 qemu_cond_broadcast(&qemu_system_cond);
597}
598
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300599void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
600{
601 struct qemu_work_item wi;
602
603 if (qemu_cpu_self(env)) {
604 func(data);
605 return;
606 }
607
608 wi.func = func;
609 wi.data = data;
610 if (!env->queued_work_first)
611 env->queued_work_first = &wi;
612 else
613 env->queued_work_last->next = &wi;
614 env->queued_work_last = &wi;
615 wi.next = NULL;
616 wi.done = false;
617
618 qemu_cpu_kick(env);
619 while (!wi.done) {
620 CPUState *self_env = cpu_single_env;
621
622 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
623 cpu_single_env = self_env;
624 }
625}
626
627static void flush_queued_work(CPUState *env)
628{
629 struct qemu_work_item *wi;
630
631 if (!env->queued_work_first)
632 return;
633
634 while ((wi = env->queued_work_first)) {
635 env->queued_work_first = wi->next;
636 wi->func(wi->data);
637 wi->done = true;
638 }
639 env->queued_work_last = NULL;
640 qemu_cond_broadcast(&qemu_work_cond);
641}
642
Blue Swirl296af7c2010-03-29 19:23:50 +0000643static void qemu_wait_io_event_common(CPUState *env)
644{
645 if (env->stop) {
646 env->stop = 0;
647 env->stopped = 1;
648 qemu_cond_signal(&qemu_pause_cond);
649 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300650 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100651 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000652}
653
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200654static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000655{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200656 CPUState *env;
657
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200658 while (!any_cpu_has_work())
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200659 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
Blue Swirl296af7c2010-03-29 19:23:50 +0000660
661 qemu_mutex_unlock(&qemu_global_mutex);
662
663 /*
664 * Users of qemu_global_mutex can be starved, having no chance
665 * to acquire it since this path will get to it first.
666 * So use another lock to provide fairness.
667 */
668 qemu_mutex_lock(&qemu_fair_mutex);
669 qemu_mutex_unlock(&qemu_fair_mutex);
670
671 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200672
673 for (env = first_cpu; env != NULL; env = env->next_cpu) {
674 qemu_wait_io_event_common(env);
675 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000676}
677
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300678static void sigbus_reraise(void)
679{
680 sigset_t set;
681 struct sigaction action;
682
683 memset(&action, 0, sizeof(action));
684 action.sa_handler = SIG_DFL;
685 if (!sigaction(SIGBUS, &action, NULL)) {
686 raise(SIGBUS);
687 sigemptyset(&set);
688 sigaddset(&set, SIGBUS);
689 sigprocmask(SIG_UNBLOCK, &set, NULL);
690 }
691 perror("Failed to re-raise SIGBUS!\n");
692 abort();
693}
694
695static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
696 void *ctx)
697{
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100698 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) {
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300699 sigbus_reraise();
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100700 }
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300701}
702
Blue Swirl296af7c2010-03-29 19:23:50 +0000703static void qemu_kvm_wait_io_event(CPUState *env)
704{
705 while (!cpu_has_work(env))
706 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
707
Jan Kiszka5db5bda2011-02-01 22:15:54 +0100708 qemu_kvm_eat_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000709 qemu_wait_io_event_common(env);
710}
711
712static int qemu_cpu_exec(CPUState *env);
713
714static void *kvm_cpu_thread_fn(void *arg)
715{
716 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100717 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000718
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300719 qemu_mutex_lock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000720 qemu_thread_self(env->thread);
Jan Kiszkad31ae052011-02-01 22:15:49 +0100721
Jan Kiszka84b49152011-02-01 22:15:50 +0100722 r = kvm_init_vcpu(env);
723 if (r < 0) {
724 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
725 exit(1);
726 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000727
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100728 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000729
730 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000731 env->created = 1;
732 qemu_cond_signal(&qemu_cpu_cond);
733
734 /* and wait for machine initialization */
735 while (!qemu_system_ready)
736 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
737
738 while (1) {
739 if (cpu_can_run(env))
740 qemu_cpu_exec(env);
741 qemu_kvm_wait_io_event(env);
742 }
743
744 return NULL;
745}
746
747static void *tcg_cpu_thread_fn(void *arg)
748{
749 CPUState *env = arg;
750
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100751 qemu_tcg_init_cpu_signals();
Blue Swirl296af7c2010-03-29 19:23:50 +0000752 qemu_thread_self(env->thread);
753
754 /* signal CPU creation */
755 qemu_mutex_lock(&qemu_global_mutex);
756 for (env = first_cpu; env != NULL; env = env->next_cpu)
757 env->created = 1;
758 qemu_cond_signal(&qemu_cpu_cond);
759
760 /* and wait for machine initialization */
761 while (!qemu_system_ready)
762 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
763
764 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200765 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200766 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000767 }
768
769 return NULL;
770}
771
772void qemu_cpu_kick(void *_env)
773{
774 CPUState *env = _env;
775 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100776 if (!env->thread_kicked) {
777 qemu_thread_signal(env->thread, SIG_IPI);
778 env->thread_kicked = true;
779 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000780}
781
782int qemu_cpu_self(void *_env)
783{
784 CPUState *env = _env;
785 QemuThread this;
786
787 qemu_thread_self(&this);
788
789 return qemu_thread_equal(&this, env->thread);
790}
791
Blue Swirl296af7c2010-03-29 19:23:50 +0000792void qemu_mutex_lock_iothread(void)
793{
794 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000795 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300796 } else {
797 qemu_mutex_lock(&qemu_fair_mutex);
798 if (qemu_mutex_trylock(&qemu_global_mutex)) {
799 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
800 qemu_mutex_lock(&qemu_global_mutex);
801 }
802 qemu_mutex_unlock(&qemu_fair_mutex);
803 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000804}
805
806void qemu_mutex_unlock_iothread(void)
807{
808 qemu_mutex_unlock(&qemu_global_mutex);
809}
810
811static int all_vcpus_paused(void)
812{
813 CPUState *penv = first_cpu;
814
815 while (penv) {
816 if (!penv->stopped)
817 return 0;
818 penv = (CPUState *)penv->next_cpu;
819 }
820
821 return 1;
822}
823
824void pause_all_vcpus(void)
825{
826 CPUState *penv = first_cpu;
827
828 while (penv) {
829 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000830 qemu_cpu_kick(penv);
831 penv = (CPUState *)penv->next_cpu;
832 }
833
834 while (!all_vcpus_paused()) {
835 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
836 penv = first_cpu;
837 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300838 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000839 penv = (CPUState *)penv->next_cpu;
840 }
841 }
842}
843
844void resume_all_vcpus(void)
845{
846 CPUState *penv = first_cpu;
847
848 while (penv) {
849 penv->stop = 0;
850 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000851 qemu_cpu_kick(penv);
852 penv = (CPUState *)penv->next_cpu;
853 }
854}
855
856static void tcg_init_vcpu(void *_env)
857{
858 CPUState *env = _env;
859 /* share a single thread for all cpus with TCG */
860 if (!tcg_cpu_thread) {
861 env->thread = qemu_mallocz(sizeof(QemuThread));
862 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
863 qemu_cond_init(env->halt_cond);
864 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
865 while (env->created == 0)
866 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
867 tcg_cpu_thread = env->thread;
868 tcg_halt_cond = env->halt_cond;
869 } else {
870 env->thread = tcg_cpu_thread;
871 env->halt_cond = tcg_halt_cond;
872 }
873}
874
875static void kvm_start_vcpu(CPUState *env)
876{
877 env->thread = qemu_mallocz(sizeof(QemuThread));
878 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
879 qemu_cond_init(env->halt_cond);
880 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
881 while (env->created == 0)
882 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
883}
884
885void qemu_init_vcpu(void *_env)
886{
887 CPUState *env = _env;
888
889 env->nr_cores = smp_cores;
890 env->nr_threads = smp_threads;
891 if (kvm_enabled())
892 kvm_start_vcpu(env);
893 else
894 tcg_init_vcpu(env);
895}
896
897void qemu_notify_event(void)
898{
899 qemu_event_increment();
900}
901
902static void qemu_system_vmstop_request(int reason)
903{
904 vmstop_requested = reason;
905 qemu_notify_event();
906}
907
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100908void cpu_stop_current(void)
909{
910 if (cpu_single_env) {
911 cpu_single_env->stopped = 1;
912 cpu_exit(cpu_single_env);
913 }
914}
915
Blue Swirl296af7c2010-03-29 19:23:50 +0000916void vm_stop(int reason)
917{
918 QemuThread me;
919 qemu_thread_self(&me);
920
921 if (!qemu_thread_equal(&me, &io_thread)) {
922 qemu_system_vmstop_request(reason);
923 /*
924 * FIXME: should not return to device code in case
925 * vm_stop() has been requested.
926 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100927 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +0000928 return;
929 }
930 do_vm_stop(reason);
931}
932
933#endif
934
935static int qemu_cpu_exec(CPUState *env)
936{
937 int ret;
938#ifdef CONFIG_PROFILER
939 int64_t ti;
940#endif
941
942#ifdef CONFIG_PROFILER
943 ti = profile_getclock();
944#endif
945 if (use_icount) {
946 int64_t count;
947 int decr;
948 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
949 env->icount_decr.u16.low = 0;
950 env->icount_extra = 0;
951 count = qemu_icount_round (qemu_next_deadline());
952 qemu_icount += count;
953 decr = (count > 0xffff) ? 0xffff : count;
954 count -= decr;
955 env->icount_decr.u16.low = decr;
956 env->icount_extra = count;
957 }
958 ret = cpu_exec(env);
959#ifdef CONFIG_PROFILER
960 qemu_time += profile_getclock() - ti;
961#endif
962 if (use_icount) {
963 /* Fold pending instructions back into the
964 instruction counter, and clear the interrupt flag. */
965 qemu_icount -= (env->icount_decr.u16.low
966 + env->icount_extra);
967 env->icount_decr.u32 = 0;
968 env->icount_extra = 0;
969 }
970 return ret;
971}
972
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200973bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000974{
Jan Kiszka9a360852011-02-01 22:15:55 +0100975 int r;
976
Blue Swirl296af7c2010-03-29 19:23:50 +0000977 if (next_cpu == NULL)
978 next_cpu = first_cpu;
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200979 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +0200980 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000981
982 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +0200983 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +0000984
985 if (qemu_alarm_pending())
986 break;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200987 if (cpu_can_run(env)) {
Jan Kiszka9a360852011-02-01 22:15:55 +0100988 r = qemu_cpu_exec(env);
989 if (kvm_enabled()) {
990 qemu_kvm_eat_signals(env);
991 }
992 if (r == EXCP_DEBUG) {
Jan Kiszka3c638d02010-06-25 16:56:56 +0200993 break;
994 }
995 } else if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000996 break;
997 }
998 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200999 exit_request = 0;
Jan Kiszka472fb0c2010-06-25 16:56:55 +02001000 return any_cpu_has_work();
Blue Swirl296af7c2010-03-29 19:23:50 +00001001}
1002
1003void set_numa_modes(void)
1004{
1005 CPUState *env;
1006 int i;
1007
1008 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1009 for (i = 0; i < nb_numa_nodes; i++) {
1010 if (node_cpumask[i] & (1 << env->cpu_index)) {
1011 env->numa_node = i;
1012 }
1013 }
1014 }
1015}
1016
1017void set_cpu_log(const char *optarg)
1018{
1019 int mask;
1020 const CPULogItem *item;
1021
1022 mask = cpu_str_to_log_mask(optarg);
1023 if (!mask) {
1024 printf("Log items (comma separated):\n");
1025 for (item = cpu_log_items; item->mask != 0; item++) {
1026 printf("%-10s %s\n", item->name, item->help);
1027 }
1028 exit(1);
1029 }
1030 cpu_set_log(mask);
1031}
Blue Swirl29e922b2010-03-29 19:24:00 +00001032
1033/* Return the virtual CPU time, based on the instruction counter. */
1034int64_t cpu_get_icount(void)
1035{
1036 int64_t icount;
1037 CPUState *env = cpu_single_env;;
1038
1039 icount = qemu_icount;
1040 if (env) {
1041 if (!can_do_io(env)) {
1042 fprintf(stderr, "Bad clock read\n");
1043 }
1044 icount -= (env->icount_decr.u16.low + env->icount_extra);
1045 }
1046 return qemu_icount_bias + (icount << icount_time_shift);
1047}
Blue Swirl262353c2010-05-04 19:55:35 +00001048
Stefan Weil9a78eea2010-10-22 23:03:33 +02001049void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001050{
1051 /* XXX: implement xxx_cpu_list for targets that still miss it */
1052#if defined(cpu_list_id)
1053 cpu_list_id(f, cpu_fprintf, optarg);
1054#elif defined(cpu_list)
1055 cpu_list(f, cpu_fprintf); /* deprecated */
1056#endif
1057}