blob: 62120c450628a625ff12ce087a45363e4e25158f [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
226#ifdef CONFIG_IOTHREAD
227static void dummy_signal(int sig)
228{
229}
230#endif
231
232#else /* _WIN32 */
233
Blue Swirl296af7c2010-03-29 19:23:50 +0000234HANDLE qemu_event_handle;
235
236static void dummy_event_handler(void *opaque)
237{
238}
239
240static int qemu_event_init(void)
241{
242 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
243 if (!qemu_event_handle) {
244 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
245 return -1;
246 }
247 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
248 return 0;
249}
250
251static void qemu_event_increment(void)
252{
253 if (!SetEvent(qemu_event_handle)) {
254 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
255 GetLastError());
256 exit (1);
257 }
258}
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100259#endif /* _WIN32 */
Blue Swirl296af7c2010-03-29 19:23:50 +0000260
261#ifndef CONFIG_IOTHREAD
262int qemu_init_main_loop(void)
263{
Jan Kiszka3c638d02010-06-25 16:56:56 +0200264 cpu_set_debug_excp_handler(cpu_debug_handler);
265
Blue Swirl296af7c2010-03-29 19:23:50 +0000266 return qemu_event_init();
267}
268
Blue Swirl7277e022010-04-12 17:19:06 +0000269void qemu_main_loop_start(void)
270{
271}
272
Blue Swirl296af7c2010-03-29 19:23:50 +0000273void qemu_init_vcpu(void *_env)
274{
275 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100276 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000277
278 env->nr_cores = smp_cores;
279 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100280
281 if (kvm_enabled()) {
282 r = kvm_init_vcpu(env);
283 if (r < 0) {
284 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
285 exit(1);
286 }
287 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000288}
289
290int qemu_cpu_self(void *env)
291{
292 return 1;
293}
294
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300295void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
296{
297 func(data);
298}
299
Blue Swirl296af7c2010-03-29 19:23:50 +0000300void resume_all_vcpus(void)
301{
302}
303
304void pause_all_vcpus(void)
305{
306}
307
308void qemu_cpu_kick(void *env)
309{
310 return;
311}
312
313void qemu_notify_event(void)
314{
315 CPUState *env = cpu_single_env;
316
317 qemu_event_increment ();
318 if (env) {
319 cpu_exit(env);
320 }
321 if (next_cpu && env != next_cpu) {
322 cpu_exit(next_cpu);
323 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100324 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000325}
326
327void qemu_mutex_lock_iothread(void) {}
328void qemu_mutex_unlock_iothread(void) {}
329
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100330void cpu_stop_current(void)
331{
332}
333
Blue Swirl296af7c2010-03-29 19:23:50 +0000334void vm_stop(int reason)
335{
336 do_vm_stop(reason);
337}
338
339#else /* CONFIG_IOTHREAD */
340
341#include "qemu-thread.h"
342
343QemuMutex qemu_global_mutex;
344static QemuMutex qemu_fair_mutex;
345
346static QemuThread io_thread;
347
348static QemuThread *tcg_cpu_thread;
349static QemuCond *tcg_halt_cond;
350
351static int qemu_system_ready;
352/* cpu creation */
353static QemuCond qemu_cpu_cond;
354/* system init */
355static QemuCond qemu_system_cond;
356static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300357static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000358
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300359/* If we have signalfd, we mask out the signals we want to handle and then
360 * use signalfd to listen for them. We rely on whatever the current signal
361 * handler is to dispatch the signals when we receive them.
362 */
363static void sigfd_handler(void *opaque)
364{
365 int fd = (unsigned long) opaque;
366 struct qemu_signalfd_siginfo info;
367 struct sigaction action;
368 ssize_t len;
369
370 while (1) {
371 do {
372 len = read(fd, &info, sizeof(info));
373 } while (len == -1 && errno == EINTR);
374
375 if (len == -1 && errno == EAGAIN) {
376 break;
377 }
378
379 if (len != sizeof(info)) {
380 printf("read from sigfd returned %zd: %m\n", len);
381 return;
382 }
383
384 sigaction(info.ssi_signo, NULL, &action);
385 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
386 action.sa_sigaction(info.ssi_signo,
387 (siginfo_t *)&info, NULL);
388 } else if (action.sa_handler) {
389 action.sa_handler(info.ssi_signo);
390 }
391 }
392}
393
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100394static void cpu_signal(int sig)
395{
396 if (cpu_single_env) {
397 cpu_exit(cpu_single_env);
398 }
399 exit_request = 1;
400}
401
402static void qemu_kvm_init_cpu_signals(CPUState *env)
403{
404 int r;
405 sigset_t set;
406 struct sigaction sigact;
407
408 memset(&sigact, 0, sizeof(sigact));
409 sigact.sa_handler = dummy_signal;
410 sigaction(SIG_IPI, &sigact, NULL);
411
412 pthread_sigmask(SIG_BLOCK, NULL, &set);
413 sigdelset(&set, SIG_IPI);
414 sigdelset(&set, SIGBUS);
415 r = kvm_set_signal_mask(env, &set);
416 if (r) {
417 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
418 exit(1);
419 }
420}
421
422static void qemu_tcg_init_cpu_signals(void)
423{
424 sigset_t set;
425 struct sigaction sigact;
426
427 memset(&sigact, 0, sizeof(sigact));
428 sigact.sa_handler = cpu_signal;
429 sigaction(SIG_IPI, &sigact, NULL);
430
431 sigemptyset(&set);
432 sigaddset(&set, SIG_IPI);
433 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
434}
435
436static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
437 void *ctx);
438
439static sigset_t block_io_signals(void)
440{
441 sigset_t set;
442 struct sigaction action;
443
444 /* SIGUSR2 used by posix-aio-compat.c */
445 sigemptyset(&set);
446 sigaddset(&set, SIGUSR2);
447 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
448
449 sigemptyset(&set);
450 sigaddset(&set, SIGIO);
451 sigaddset(&set, SIGALRM);
452 sigaddset(&set, SIG_IPI);
453 sigaddset(&set, SIGBUS);
454 pthread_sigmask(SIG_BLOCK, &set, NULL);
455
456 memset(&action, 0, sizeof(action));
457 action.sa_flags = SA_SIGINFO;
458 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
459 sigaction(SIGBUS, &action, NULL);
460 prctl(PR_MCE_KILL, 1, 1, 0, 0);
461
462 return set;
463}
464
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300465static int qemu_signalfd_init(sigset_t mask)
466{
467 int sigfd;
468
469 sigfd = qemu_signalfd(&mask);
470 if (sigfd == -1) {
471 fprintf(stderr, "failed to create signalfd\n");
472 return -errno;
473 }
474
475 fcntl_setfl(sigfd, O_NONBLOCK);
476
477 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
478 (void *)(unsigned long) sigfd);
479
480 return 0;
481}
Blue Swirl296af7c2010-03-29 19:23:50 +0000482
483int qemu_init_main_loop(void)
484{
485 int ret;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300486 sigset_t blocked_signals;
Blue Swirl296af7c2010-03-29 19:23:50 +0000487
Jan Kiszka3c638d02010-06-25 16:56:56 +0200488 cpu_set_debug_excp_handler(cpu_debug_handler);
489
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300490 blocked_signals = block_io_signals();
491
492 ret = qemu_signalfd_init(blocked_signals);
493 if (ret)
494 return ret;
495
496 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000497 ret = qemu_event_init();
498 if (ret)
499 return ret;
500
501 qemu_cond_init(&qemu_pause_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200502 qemu_cond_init(&qemu_system_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000503 qemu_mutex_init(&qemu_fair_mutex);
504 qemu_mutex_init(&qemu_global_mutex);
505 qemu_mutex_lock(&qemu_global_mutex);
506
Blue Swirl296af7c2010-03-29 19:23:50 +0000507 qemu_thread_self(&io_thread);
508
509 return 0;
510}
511
Blue Swirl7277e022010-04-12 17:19:06 +0000512void qemu_main_loop_start(void)
513{
514 qemu_system_ready = 1;
515 qemu_cond_broadcast(&qemu_system_cond);
516}
517
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300518void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
519{
520 struct qemu_work_item wi;
521
522 if (qemu_cpu_self(env)) {
523 func(data);
524 return;
525 }
526
527 wi.func = func;
528 wi.data = data;
529 if (!env->queued_work_first)
530 env->queued_work_first = &wi;
531 else
532 env->queued_work_last->next = &wi;
533 env->queued_work_last = &wi;
534 wi.next = NULL;
535 wi.done = false;
536
537 qemu_cpu_kick(env);
538 while (!wi.done) {
539 CPUState *self_env = cpu_single_env;
540
541 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
542 cpu_single_env = self_env;
543 }
544}
545
546static void flush_queued_work(CPUState *env)
547{
548 struct qemu_work_item *wi;
549
550 if (!env->queued_work_first)
551 return;
552
553 while ((wi = env->queued_work_first)) {
554 env->queued_work_first = wi->next;
555 wi->func(wi->data);
556 wi->done = true;
557 }
558 env->queued_work_last = NULL;
559 qemu_cond_broadcast(&qemu_work_cond);
560}
561
Blue Swirl296af7c2010-03-29 19:23:50 +0000562static void qemu_wait_io_event_common(CPUState *env)
563{
564 if (env->stop) {
565 env->stop = 0;
566 env->stopped = 1;
567 qemu_cond_signal(&qemu_pause_cond);
568 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300569 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100570 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000571}
572
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200573static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000574{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200575 CPUState *env;
576
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200577 while (!any_cpu_has_work())
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200578 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
Blue Swirl296af7c2010-03-29 19:23:50 +0000579
580 qemu_mutex_unlock(&qemu_global_mutex);
581
582 /*
583 * Users of qemu_global_mutex can be starved, having no chance
584 * to acquire it since this path will get to it first.
585 * So use another lock to provide fairness.
586 */
587 qemu_mutex_lock(&qemu_fair_mutex);
588 qemu_mutex_unlock(&qemu_fair_mutex);
589
590 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200591
592 for (env = first_cpu; env != NULL; env = env->next_cpu) {
593 qemu_wait_io_event_common(env);
594 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000595}
596
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300597static void sigbus_reraise(void)
598{
599 sigset_t set;
600 struct sigaction action;
601
602 memset(&action, 0, sizeof(action));
603 action.sa_handler = SIG_DFL;
604 if (!sigaction(SIGBUS, &action, NULL)) {
605 raise(SIGBUS);
606 sigemptyset(&set);
607 sigaddset(&set, SIGBUS);
608 sigprocmask(SIG_UNBLOCK, &set, NULL);
609 }
610 perror("Failed to re-raise SIGBUS!\n");
611 abort();
612}
613
614static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
615 void *ctx)
616{
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100617 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) {
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300618 sigbus_reraise();
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100619 }
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300620}
621
Blue Swirl296af7c2010-03-29 19:23:50 +0000622static void qemu_kvm_eat_signal(CPUState *env, int timeout)
623{
624 struct timespec ts;
625 int r, e;
626 siginfo_t siginfo;
627 sigset_t waitset;
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300628 sigset_t chkset;
Blue Swirl296af7c2010-03-29 19:23:50 +0000629
630 ts.tv_sec = timeout / 1000;
631 ts.tv_nsec = (timeout % 1000) * 1000000;
632
633 sigemptyset(&waitset);
634 sigaddset(&waitset, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300635 sigaddset(&waitset, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000636
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300637 do {
638 qemu_mutex_unlock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000639
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300640 r = sigtimedwait(&waitset, &siginfo, &ts);
641 e = errno;
642
643 qemu_mutex_lock(&qemu_global_mutex);
644
645 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
646 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
647 exit(1);
648 }
649
650 switch (r) {
651 case SIGBUS:
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100652 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300653 sigbus_reraise();
Jan Kiszkaa1b87fe2011-02-01 22:15:51 +0100654 }
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300655 break;
656 default:
657 break;
658 }
659
660 r = sigpending(&chkset);
661 if (r == -1) {
662 fprintf(stderr, "sigpending: %s\n", strerror(e));
663 exit(1);
664 }
665 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Blue Swirl296af7c2010-03-29 19:23:50 +0000666}
667
668static void qemu_kvm_wait_io_event(CPUState *env)
669{
670 while (!cpu_has_work(env))
671 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
672
673 qemu_kvm_eat_signal(env, 0);
674 qemu_wait_io_event_common(env);
675}
676
677static int qemu_cpu_exec(CPUState *env);
678
679static void *kvm_cpu_thread_fn(void *arg)
680{
681 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100682 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000683
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300684 qemu_mutex_lock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000685 qemu_thread_self(env->thread);
Jan Kiszkad31ae052011-02-01 22:15:49 +0100686
Jan Kiszka84b49152011-02-01 22:15:50 +0100687 r = kvm_init_vcpu(env);
688 if (r < 0) {
689 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
690 exit(1);
691 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000692
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100693 qemu_kvm_init_cpu_signals(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000694
695 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000696 env->created = 1;
697 qemu_cond_signal(&qemu_cpu_cond);
698
699 /* and wait for machine initialization */
700 while (!qemu_system_ready)
701 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
702
703 while (1) {
704 if (cpu_can_run(env))
705 qemu_cpu_exec(env);
706 qemu_kvm_wait_io_event(env);
707 }
708
709 return NULL;
710}
711
712static void *tcg_cpu_thread_fn(void *arg)
713{
714 CPUState *env = arg;
715
Jan Kiszka55f8d6a2011-02-01 22:15:52 +0100716 qemu_tcg_init_cpu_signals();
Blue Swirl296af7c2010-03-29 19:23:50 +0000717 qemu_thread_self(env->thread);
718
719 /* signal CPU creation */
720 qemu_mutex_lock(&qemu_global_mutex);
721 for (env = first_cpu; env != NULL; env = env->next_cpu)
722 env->created = 1;
723 qemu_cond_signal(&qemu_cpu_cond);
724
725 /* and wait for machine initialization */
726 while (!qemu_system_ready)
727 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
728
729 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200730 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200731 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000732 }
733
734 return NULL;
735}
736
737void qemu_cpu_kick(void *_env)
738{
739 CPUState *env = _env;
740 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100741 if (!env->thread_kicked) {
742 qemu_thread_signal(env->thread, SIG_IPI);
743 env->thread_kicked = true;
744 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000745}
746
747int qemu_cpu_self(void *_env)
748{
749 CPUState *env = _env;
750 QemuThread this;
751
752 qemu_thread_self(&this);
753
754 return qemu_thread_equal(&this, env->thread);
755}
756
Blue Swirl296af7c2010-03-29 19:23:50 +0000757void qemu_mutex_lock_iothread(void)
758{
759 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000760 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300761 } else {
762 qemu_mutex_lock(&qemu_fair_mutex);
763 if (qemu_mutex_trylock(&qemu_global_mutex)) {
764 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
765 qemu_mutex_lock(&qemu_global_mutex);
766 }
767 qemu_mutex_unlock(&qemu_fair_mutex);
768 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000769}
770
771void qemu_mutex_unlock_iothread(void)
772{
773 qemu_mutex_unlock(&qemu_global_mutex);
774}
775
776static int all_vcpus_paused(void)
777{
778 CPUState *penv = first_cpu;
779
780 while (penv) {
781 if (!penv->stopped)
782 return 0;
783 penv = (CPUState *)penv->next_cpu;
784 }
785
786 return 1;
787}
788
789void pause_all_vcpus(void)
790{
791 CPUState *penv = first_cpu;
792
793 while (penv) {
794 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000795 qemu_cpu_kick(penv);
796 penv = (CPUState *)penv->next_cpu;
797 }
798
799 while (!all_vcpus_paused()) {
800 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
801 penv = first_cpu;
802 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300803 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000804 penv = (CPUState *)penv->next_cpu;
805 }
806 }
807}
808
809void resume_all_vcpus(void)
810{
811 CPUState *penv = first_cpu;
812
813 while (penv) {
814 penv->stop = 0;
815 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000816 qemu_cpu_kick(penv);
817 penv = (CPUState *)penv->next_cpu;
818 }
819}
820
821static void tcg_init_vcpu(void *_env)
822{
823 CPUState *env = _env;
824 /* share a single thread for all cpus with TCG */
825 if (!tcg_cpu_thread) {
826 env->thread = qemu_mallocz(sizeof(QemuThread));
827 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
828 qemu_cond_init(env->halt_cond);
829 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
830 while (env->created == 0)
831 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
832 tcg_cpu_thread = env->thread;
833 tcg_halt_cond = env->halt_cond;
834 } else {
835 env->thread = tcg_cpu_thread;
836 env->halt_cond = tcg_halt_cond;
837 }
838}
839
840static void kvm_start_vcpu(CPUState *env)
841{
842 env->thread = qemu_mallocz(sizeof(QemuThread));
843 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
844 qemu_cond_init(env->halt_cond);
845 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
846 while (env->created == 0)
847 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
848}
849
850void qemu_init_vcpu(void *_env)
851{
852 CPUState *env = _env;
853
854 env->nr_cores = smp_cores;
855 env->nr_threads = smp_threads;
856 if (kvm_enabled())
857 kvm_start_vcpu(env);
858 else
859 tcg_init_vcpu(env);
860}
861
862void qemu_notify_event(void)
863{
864 qemu_event_increment();
865}
866
867static void qemu_system_vmstop_request(int reason)
868{
869 vmstop_requested = reason;
870 qemu_notify_event();
871}
872
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100873void cpu_stop_current(void)
874{
875 if (cpu_single_env) {
876 cpu_single_env->stopped = 1;
877 cpu_exit(cpu_single_env);
878 }
879}
880
Blue Swirl296af7c2010-03-29 19:23:50 +0000881void vm_stop(int reason)
882{
883 QemuThread me;
884 qemu_thread_self(&me);
885
886 if (!qemu_thread_equal(&me, &io_thread)) {
887 qemu_system_vmstop_request(reason);
888 /*
889 * FIXME: should not return to device code in case
890 * vm_stop() has been requested.
891 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100892 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +0000893 return;
894 }
895 do_vm_stop(reason);
896}
897
898#endif
899
900static int qemu_cpu_exec(CPUState *env)
901{
902 int ret;
903#ifdef CONFIG_PROFILER
904 int64_t ti;
905#endif
906
907#ifdef CONFIG_PROFILER
908 ti = profile_getclock();
909#endif
910 if (use_icount) {
911 int64_t count;
912 int decr;
913 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
914 env->icount_decr.u16.low = 0;
915 env->icount_extra = 0;
916 count = qemu_icount_round (qemu_next_deadline());
917 qemu_icount += count;
918 decr = (count > 0xffff) ? 0xffff : count;
919 count -= decr;
920 env->icount_decr.u16.low = decr;
921 env->icount_extra = count;
922 }
923 ret = cpu_exec(env);
924#ifdef CONFIG_PROFILER
925 qemu_time += profile_getclock() - ti;
926#endif
927 if (use_icount) {
928 /* Fold pending instructions back into the
929 instruction counter, and clear the interrupt flag. */
930 qemu_icount -= (env->icount_decr.u16.low
931 + env->icount_extra);
932 env->icount_decr.u32 = 0;
933 env->icount_extra = 0;
934 }
935 return ret;
936}
937
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200938bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000939{
Blue Swirl296af7c2010-03-29 19:23:50 +0000940 if (next_cpu == NULL)
941 next_cpu = first_cpu;
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200942 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +0200943 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000944
945 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +0200946 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +0000947
948 if (qemu_alarm_pending())
949 break;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200950 if (cpu_can_run(env)) {
951 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
952 break;
953 }
954 } else if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000955 break;
956 }
957 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200958 exit_request = 0;
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200959 return any_cpu_has_work();
Blue Swirl296af7c2010-03-29 19:23:50 +0000960}
961
962void set_numa_modes(void)
963{
964 CPUState *env;
965 int i;
966
967 for (env = first_cpu; env != NULL; env = env->next_cpu) {
968 for (i = 0; i < nb_numa_nodes; i++) {
969 if (node_cpumask[i] & (1 << env->cpu_index)) {
970 env->numa_node = i;
971 }
972 }
973 }
974}
975
976void set_cpu_log(const char *optarg)
977{
978 int mask;
979 const CPULogItem *item;
980
981 mask = cpu_str_to_log_mask(optarg);
982 if (!mask) {
983 printf("Log items (comma separated):\n");
984 for (item = cpu_log_items; item->mask != 0; item++) {
985 printf("%-10s %s\n", item->name, item->help);
986 }
987 exit(1);
988 }
989 cpu_set_log(mask);
990}
Blue Swirl29e922b2010-03-29 19:24:00 +0000991
992/* Return the virtual CPU time, based on the instruction counter. */
993int64_t cpu_get_icount(void)
994{
995 int64_t icount;
996 CPUState *env = cpu_single_env;;
997
998 icount = qemu_icount;
999 if (env) {
1000 if (!can_do_io(env)) {
1001 fprintf(stderr, "Bad clock read\n");
1002 }
1003 icount -= (env->icount_decr.u16.low + env->icount_extra);
1004 }
1005 return qemu_icount_bias + (icount << icount_time_shift);
1006}
Blue Swirl262353c2010-05-04 19:55:35 +00001007
Stefan Weil9a78eea2010-10-22 23:03:33 +02001008void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001009{
1010 /* XXX: implement xxx_cpu_list for targets that still miss it */
1011#if defined(cpu_list_id)
1012 cpu_list_id(f, cpu_fprintf, optarg);
1013#elif defined(cpu_list)
1014 cpu_list(f, cpu_fprintf); /* deprecated */
1015#endif
1016}