blob: 3a72d067911fb3c23c5aa9c9e1e3089b697dabb3 [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}
225#else
226HANDLE qemu_event_handle;
227
228static void dummy_event_handler(void *opaque)
229{
230}
231
232static int qemu_event_init(void)
233{
234 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
235 if (!qemu_event_handle) {
236 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
237 return -1;
238 }
239 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
240 return 0;
241}
242
243static void qemu_event_increment(void)
244{
245 if (!SetEvent(qemu_event_handle)) {
246 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
247 GetLastError());
248 exit (1);
249 }
250}
251#endif
252
253#ifndef CONFIG_IOTHREAD
254int qemu_init_main_loop(void)
255{
Jan Kiszka3c638d02010-06-25 16:56:56 +0200256 cpu_set_debug_excp_handler(cpu_debug_handler);
257
Blue Swirl296af7c2010-03-29 19:23:50 +0000258 return qemu_event_init();
259}
260
Blue Swirl7277e022010-04-12 17:19:06 +0000261void qemu_main_loop_start(void)
262{
263}
264
Blue Swirl296af7c2010-03-29 19:23:50 +0000265void qemu_init_vcpu(void *_env)
266{
267 CPUState *env = _env;
Jan Kiszka84b49152011-02-01 22:15:50 +0100268 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000269
270 env->nr_cores = smp_cores;
271 env->nr_threads = smp_threads;
Jan Kiszka84b49152011-02-01 22:15:50 +0100272
273 if (kvm_enabled()) {
274 r = kvm_init_vcpu(env);
275 if (r < 0) {
276 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
277 exit(1);
278 }
279 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000280}
281
282int qemu_cpu_self(void *env)
283{
284 return 1;
285}
286
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300287void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
288{
289 func(data);
290}
291
Blue Swirl296af7c2010-03-29 19:23:50 +0000292void resume_all_vcpus(void)
293{
294}
295
296void pause_all_vcpus(void)
297{
298}
299
300void qemu_cpu_kick(void *env)
301{
302 return;
303}
304
305void qemu_notify_event(void)
306{
307 CPUState *env = cpu_single_env;
308
309 qemu_event_increment ();
310 if (env) {
311 cpu_exit(env);
312 }
313 if (next_cpu && env != next_cpu) {
314 cpu_exit(next_cpu);
315 }
Jan Kiszka38145df2011-02-01 22:15:45 +0100316 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000317}
318
319void qemu_mutex_lock_iothread(void) {}
320void qemu_mutex_unlock_iothread(void) {}
321
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100322void cpu_stop_current(void)
323{
324}
325
Blue Swirl296af7c2010-03-29 19:23:50 +0000326void vm_stop(int reason)
327{
328 do_vm_stop(reason);
329}
330
331#else /* CONFIG_IOTHREAD */
332
333#include "qemu-thread.h"
334
335QemuMutex qemu_global_mutex;
336static QemuMutex qemu_fair_mutex;
337
338static QemuThread io_thread;
339
340static QemuThread *tcg_cpu_thread;
341static QemuCond *tcg_halt_cond;
342
343static int qemu_system_ready;
344/* cpu creation */
345static QemuCond qemu_cpu_cond;
346/* system init */
347static QemuCond qemu_system_cond;
348static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300349static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000350
Paolo Bonzini55541c82010-06-03 15:20:32 +0200351static void tcg_init_ipi(void);
352static void kvm_init_ipi(CPUState *env);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300353static sigset_t block_io_signals(void);
354
355/* If we have signalfd, we mask out the signals we want to handle and then
356 * use signalfd to listen for them. We rely on whatever the current signal
357 * handler is to dispatch the signals when we receive them.
358 */
359static void sigfd_handler(void *opaque)
360{
361 int fd = (unsigned long) opaque;
362 struct qemu_signalfd_siginfo info;
363 struct sigaction action;
364 ssize_t len;
365
366 while (1) {
367 do {
368 len = read(fd, &info, sizeof(info));
369 } while (len == -1 && errno == EINTR);
370
371 if (len == -1 && errno == EAGAIN) {
372 break;
373 }
374
375 if (len != sizeof(info)) {
376 printf("read from sigfd returned %zd: %m\n", len);
377 return;
378 }
379
380 sigaction(info.ssi_signo, NULL, &action);
381 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
382 action.sa_sigaction(info.ssi_signo,
383 (siginfo_t *)&info, NULL);
384 } else if (action.sa_handler) {
385 action.sa_handler(info.ssi_signo);
386 }
387 }
388}
389
390static int qemu_signalfd_init(sigset_t mask)
391{
392 int sigfd;
393
394 sigfd = qemu_signalfd(&mask);
395 if (sigfd == -1) {
396 fprintf(stderr, "failed to create signalfd\n");
397 return -errno;
398 }
399
400 fcntl_setfl(sigfd, O_NONBLOCK);
401
402 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
403 (void *)(unsigned long) sigfd);
404
405 return 0;
406}
Blue Swirl296af7c2010-03-29 19:23:50 +0000407
408int qemu_init_main_loop(void)
409{
410 int ret;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300411 sigset_t blocked_signals;
Blue Swirl296af7c2010-03-29 19:23:50 +0000412
Jan Kiszka3c638d02010-06-25 16:56:56 +0200413 cpu_set_debug_excp_handler(cpu_debug_handler);
414
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300415 blocked_signals = block_io_signals();
416
417 ret = qemu_signalfd_init(blocked_signals);
418 if (ret)
419 return ret;
420
421 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000422 ret = qemu_event_init();
423 if (ret)
424 return ret;
425
426 qemu_cond_init(&qemu_pause_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200427 qemu_cond_init(&qemu_system_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000428 qemu_mutex_init(&qemu_fair_mutex);
429 qemu_mutex_init(&qemu_global_mutex);
430 qemu_mutex_lock(&qemu_global_mutex);
431
Blue Swirl296af7c2010-03-29 19:23:50 +0000432 qemu_thread_self(&io_thread);
433
434 return 0;
435}
436
Blue Swirl7277e022010-04-12 17:19:06 +0000437void qemu_main_loop_start(void)
438{
439 qemu_system_ready = 1;
440 qemu_cond_broadcast(&qemu_system_cond);
441}
442
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300443void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
444{
445 struct qemu_work_item wi;
446
447 if (qemu_cpu_self(env)) {
448 func(data);
449 return;
450 }
451
452 wi.func = func;
453 wi.data = data;
454 if (!env->queued_work_first)
455 env->queued_work_first = &wi;
456 else
457 env->queued_work_last->next = &wi;
458 env->queued_work_last = &wi;
459 wi.next = NULL;
460 wi.done = false;
461
462 qemu_cpu_kick(env);
463 while (!wi.done) {
464 CPUState *self_env = cpu_single_env;
465
466 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
467 cpu_single_env = self_env;
468 }
469}
470
471static void flush_queued_work(CPUState *env)
472{
473 struct qemu_work_item *wi;
474
475 if (!env->queued_work_first)
476 return;
477
478 while ((wi = env->queued_work_first)) {
479 env->queued_work_first = wi->next;
480 wi->func(wi->data);
481 wi->done = true;
482 }
483 env->queued_work_last = NULL;
484 qemu_cond_broadcast(&qemu_work_cond);
485}
486
Blue Swirl296af7c2010-03-29 19:23:50 +0000487static void qemu_wait_io_event_common(CPUState *env)
488{
489 if (env->stop) {
490 env->stop = 0;
491 env->stopped = 1;
492 qemu_cond_signal(&qemu_pause_cond);
493 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300494 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100495 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000496}
497
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200498static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000499{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200500 CPUState *env;
501
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200502 while (!any_cpu_has_work())
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200503 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
Blue Swirl296af7c2010-03-29 19:23:50 +0000504
505 qemu_mutex_unlock(&qemu_global_mutex);
506
507 /*
508 * Users of qemu_global_mutex can be starved, having no chance
509 * to acquire it since this path will get to it first.
510 * So use another lock to provide fairness.
511 */
512 qemu_mutex_lock(&qemu_fair_mutex);
513 qemu_mutex_unlock(&qemu_fair_mutex);
514
515 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200516
517 for (env = first_cpu; env != NULL; env = env->next_cpu) {
518 qemu_wait_io_event_common(env);
519 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000520}
521
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300522static void sigbus_reraise(void)
523{
524 sigset_t set;
525 struct sigaction action;
526
527 memset(&action, 0, sizeof(action));
528 action.sa_handler = SIG_DFL;
529 if (!sigaction(SIGBUS, &action, NULL)) {
530 raise(SIGBUS);
531 sigemptyset(&set);
532 sigaddset(&set, SIGBUS);
533 sigprocmask(SIG_UNBLOCK, &set, NULL);
534 }
535 perror("Failed to re-raise SIGBUS!\n");
536 abort();
537}
538
539static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
540 void *ctx)
541{
542#if defined(TARGET_I386)
543 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr))
544#endif
545 sigbus_reraise();
546}
547
Blue Swirl296af7c2010-03-29 19:23:50 +0000548static void qemu_kvm_eat_signal(CPUState *env, int timeout)
549{
550 struct timespec ts;
551 int r, e;
552 siginfo_t siginfo;
553 sigset_t waitset;
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300554 sigset_t chkset;
Blue Swirl296af7c2010-03-29 19:23:50 +0000555
556 ts.tv_sec = timeout / 1000;
557 ts.tv_nsec = (timeout % 1000) * 1000000;
558
559 sigemptyset(&waitset);
560 sigaddset(&waitset, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300561 sigaddset(&waitset, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000562
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300563 do {
564 qemu_mutex_unlock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000565
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300566 r = sigtimedwait(&waitset, &siginfo, &ts);
567 e = errno;
568
569 qemu_mutex_lock(&qemu_global_mutex);
570
571 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
572 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
573 exit(1);
574 }
575
576 switch (r) {
577 case SIGBUS:
578#ifdef TARGET_I386
579 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr))
580#endif
581 sigbus_reraise();
582 break;
583 default:
584 break;
585 }
586
587 r = sigpending(&chkset);
588 if (r == -1) {
589 fprintf(stderr, "sigpending: %s\n", strerror(e));
590 exit(1);
591 }
592 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Blue Swirl296af7c2010-03-29 19:23:50 +0000593}
594
595static void qemu_kvm_wait_io_event(CPUState *env)
596{
597 while (!cpu_has_work(env))
598 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
599
600 qemu_kvm_eat_signal(env, 0);
601 qemu_wait_io_event_common(env);
602}
603
604static int qemu_cpu_exec(CPUState *env);
605
606static void *kvm_cpu_thread_fn(void *arg)
607{
608 CPUState *env = arg;
Jan Kiszka84b49152011-02-01 22:15:50 +0100609 int r;
Blue Swirl296af7c2010-03-29 19:23:50 +0000610
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300611 qemu_mutex_lock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000612 qemu_thread_self(env->thread);
Jan Kiszkad31ae052011-02-01 22:15:49 +0100613
Jan Kiszka84b49152011-02-01 22:15:50 +0100614 r = kvm_init_vcpu(env);
615 if (r < 0) {
616 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
617 exit(1);
618 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000619
Paolo Bonzini55541c82010-06-03 15:20:32 +0200620 kvm_init_ipi(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000621
622 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000623 env->created = 1;
624 qemu_cond_signal(&qemu_cpu_cond);
625
626 /* and wait for machine initialization */
627 while (!qemu_system_ready)
628 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
629
630 while (1) {
631 if (cpu_can_run(env))
632 qemu_cpu_exec(env);
633 qemu_kvm_wait_io_event(env);
634 }
635
636 return NULL;
637}
638
639static void *tcg_cpu_thread_fn(void *arg)
640{
641 CPUState *env = arg;
642
Paolo Bonzini55541c82010-06-03 15:20:32 +0200643 tcg_init_ipi();
Blue Swirl296af7c2010-03-29 19:23:50 +0000644 qemu_thread_self(env->thread);
645
646 /* signal CPU creation */
647 qemu_mutex_lock(&qemu_global_mutex);
648 for (env = first_cpu; env != NULL; env = env->next_cpu)
649 env->created = 1;
650 qemu_cond_signal(&qemu_cpu_cond);
651
652 /* and wait for machine initialization */
653 while (!qemu_system_ready)
654 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
655
656 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200657 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200658 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000659 }
660
661 return NULL;
662}
663
664void qemu_cpu_kick(void *_env)
665{
666 CPUState *env = _env;
667 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100668 if (!env->thread_kicked) {
669 qemu_thread_signal(env->thread, SIG_IPI);
670 env->thread_kicked = true;
671 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000672}
673
674int qemu_cpu_self(void *_env)
675{
676 CPUState *env = _env;
677 QemuThread this;
678
679 qemu_thread_self(&this);
680
681 return qemu_thread_equal(&this, env->thread);
682}
683
684static void cpu_signal(int sig)
685{
686 if (cpu_single_env)
687 cpu_exit(cpu_single_env);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300688 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000689}
690
Paolo Bonzini55541c82010-06-03 15:20:32 +0200691static void tcg_init_ipi(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000692{
693 sigset_t set;
694 struct sigaction sigact;
695
Paolo Bonzini55541c82010-06-03 15:20:32 +0200696 memset(&sigact, 0, sizeof(sigact));
697 sigact.sa_handler = cpu_signal;
698 sigaction(SIG_IPI, &sigact, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000699
700 sigemptyset(&set);
701 sigaddset(&set, SIG_IPI);
702 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000703}
704
705static void dummy_signal(int sig)
706{
707}
708
Paolo Bonzini55541c82010-06-03 15:20:32 +0200709static void kvm_init_ipi(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000710{
711 int r;
712 sigset_t set;
713 struct sigaction sigact;
714
Blue Swirl296af7c2010-03-29 19:23:50 +0000715 memset(&sigact, 0, sizeof(sigact));
716 sigact.sa_handler = dummy_signal;
717 sigaction(SIG_IPI, &sigact, NULL);
718
Paolo Bonzini55541c82010-06-03 15:20:32 +0200719 pthread_sigmask(SIG_BLOCK, NULL, &set);
720 sigdelset(&set, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300721 sigdelset(&set, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000722 r = kvm_set_signal_mask(env, &set);
723 if (r) {
724 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
725 exit(1);
726 }
727}
728
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300729static sigset_t block_io_signals(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000730{
731 sigset_t set;
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300732 struct sigaction action;
Blue Swirl296af7c2010-03-29 19:23:50 +0000733
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300734 /* SIGUSR2 used by posix-aio-compat.c */
Blue Swirl296af7c2010-03-29 19:23:50 +0000735 sigemptyset(&set);
736 sigaddset(&set, SIGUSR2);
Blue Swirl296af7c2010-03-29 19:23:50 +0000737 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
738
739 sigemptyset(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300740 sigaddset(&set, SIGIO);
741 sigaddset(&set, SIGALRM);
Blue Swirl296af7c2010-03-29 19:23:50 +0000742 sigaddset(&set, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300743 sigaddset(&set, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000744 pthread_sigmask(SIG_BLOCK, &set, NULL);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300745
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300746 memset(&action, 0, sizeof(action));
747 action.sa_flags = SA_SIGINFO;
748 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
749 sigaction(SIGBUS, &action, NULL);
750 prctl(PR_MCE_KILL, 1, 1, 0, 0);
751
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300752 return set;
Blue Swirl296af7c2010-03-29 19:23:50 +0000753}
754
Blue Swirl296af7c2010-03-29 19:23:50 +0000755void qemu_mutex_lock_iothread(void)
756{
757 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000758 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300759 } else {
760 qemu_mutex_lock(&qemu_fair_mutex);
761 if (qemu_mutex_trylock(&qemu_global_mutex)) {
762 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
763 qemu_mutex_lock(&qemu_global_mutex);
764 }
765 qemu_mutex_unlock(&qemu_fair_mutex);
766 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000767}
768
769void qemu_mutex_unlock_iothread(void)
770{
771 qemu_mutex_unlock(&qemu_global_mutex);
772}
773
774static int all_vcpus_paused(void)
775{
776 CPUState *penv = first_cpu;
777
778 while (penv) {
779 if (!penv->stopped)
780 return 0;
781 penv = (CPUState *)penv->next_cpu;
782 }
783
784 return 1;
785}
786
787void pause_all_vcpus(void)
788{
789 CPUState *penv = first_cpu;
790
791 while (penv) {
792 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000793 qemu_cpu_kick(penv);
794 penv = (CPUState *)penv->next_cpu;
795 }
796
797 while (!all_vcpus_paused()) {
798 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
799 penv = first_cpu;
800 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300801 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000802 penv = (CPUState *)penv->next_cpu;
803 }
804 }
805}
806
807void resume_all_vcpus(void)
808{
809 CPUState *penv = first_cpu;
810
811 while (penv) {
812 penv->stop = 0;
813 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000814 qemu_cpu_kick(penv);
815 penv = (CPUState *)penv->next_cpu;
816 }
817}
818
819static void tcg_init_vcpu(void *_env)
820{
821 CPUState *env = _env;
822 /* share a single thread for all cpus with TCG */
823 if (!tcg_cpu_thread) {
824 env->thread = qemu_mallocz(sizeof(QemuThread));
825 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
826 qemu_cond_init(env->halt_cond);
827 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
828 while (env->created == 0)
829 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
830 tcg_cpu_thread = env->thread;
831 tcg_halt_cond = env->halt_cond;
832 } else {
833 env->thread = tcg_cpu_thread;
834 env->halt_cond = tcg_halt_cond;
835 }
836}
837
838static void kvm_start_vcpu(CPUState *env)
839{
840 env->thread = qemu_mallocz(sizeof(QemuThread));
841 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
842 qemu_cond_init(env->halt_cond);
843 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
844 while (env->created == 0)
845 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
846}
847
848void qemu_init_vcpu(void *_env)
849{
850 CPUState *env = _env;
851
852 env->nr_cores = smp_cores;
853 env->nr_threads = smp_threads;
854 if (kvm_enabled())
855 kvm_start_vcpu(env);
856 else
857 tcg_init_vcpu(env);
858}
859
860void qemu_notify_event(void)
861{
862 qemu_event_increment();
863}
864
865static void qemu_system_vmstop_request(int reason)
866{
867 vmstop_requested = reason;
868 qemu_notify_event();
869}
870
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100871void cpu_stop_current(void)
872{
873 if (cpu_single_env) {
874 cpu_single_env->stopped = 1;
875 cpu_exit(cpu_single_env);
876 }
877}
878
Blue Swirl296af7c2010-03-29 19:23:50 +0000879void vm_stop(int reason)
880{
881 QemuThread me;
882 qemu_thread_self(&me);
883
884 if (!qemu_thread_equal(&me, &io_thread)) {
885 qemu_system_vmstop_request(reason);
886 /*
887 * FIXME: should not return to device code in case
888 * vm_stop() has been requested.
889 */
Jan Kiszkab4a3d962011-02-01 22:15:43 +0100890 cpu_stop_current();
Blue Swirl296af7c2010-03-29 19:23:50 +0000891 return;
892 }
893 do_vm_stop(reason);
894}
895
896#endif
897
898static int qemu_cpu_exec(CPUState *env)
899{
900 int ret;
901#ifdef CONFIG_PROFILER
902 int64_t ti;
903#endif
904
905#ifdef CONFIG_PROFILER
906 ti = profile_getclock();
907#endif
908 if (use_icount) {
909 int64_t count;
910 int decr;
911 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
912 env->icount_decr.u16.low = 0;
913 env->icount_extra = 0;
914 count = qemu_icount_round (qemu_next_deadline());
915 qemu_icount += count;
916 decr = (count > 0xffff) ? 0xffff : count;
917 count -= decr;
918 env->icount_decr.u16.low = decr;
919 env->icount_extra = count;
920 }
921 ret = cpu_exec(env);
922#ifdef CONFIG_PROFILER
923 qemu_time += profile_getclock() - ti;
924#endif
925 if (use_icount) {
926 /* Fold pending instructions back into the
927 instruction counter, and clear the interrupt flag. */
928 qemu_icount -= (env->icount_decr.u16.low
929 + env->icount_extra);
930 env->icount_decr.u32 = 0;
931 env->icount_extra = 0;
932 }
933 return ret;
934}
935
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200936bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000937{
Blue Swirl296af7c2010-03-29 19:23:50 +0000938 if (next_cpu == NULL)
939 next_cpu = first_cpu;
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200940 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +0200941 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000942
943 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +0200944 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +0000945
946 if (qemu_alarm_pending())
947 break;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200948 if (cpu_can_run(env)) {
949 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
950 break;
951 }
952 } else if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000953 break;
954 }
955 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200956 exit_request = 0;
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200957 return any_cpu_has_work();
Blue Swirl296af7c2010-03-29 19:23:50 +0000958}
959
960void set_numa_modes(void)
961{
962 CPUState *env;
963 int i;
964
965 for (env = first_cpu; env != NULL; env = env->next_cpu) {
966 for (i = 0; i < nb_numa_nodes; i++) {
967 if (node_cpumask[i] & (1 << env->cpu_index)) {
968 env->numa_node = i;
969 }
970 }
971 }
972}
973
974void set_cpu_log(const char *optarg)
975{
976 int mask;
977 const CPULogItem *item;
978
979 mask = cpu_str_to_log_mask(optarg);
980 if (!mask) {
981 printf("Log items (comma separated):\n");
982 for (item = cpu_log_items; item->mask != 0; item++) {
983 printf("%-10s %s\n", item->name, item->help);
984 }
985 exit(1);
986 }
987 cpu_set_log(mask);
988}
Blue Swirl29e922b2010-03-29 19:24:00 +0000989
990/* Return the virtual CPU time, based on the instruction counter. */
991int64_t cpu_get_icount(void)
992{
993 int64_t icount;
994 CPUState *env = cpu_single_env;;
995
996 icount = qemu_icount;
997 if (env) {
998 if (!can_do_io(env)) {
999 fprintf(stderr, "Bad clock read\n");
1000 }
1001 icount -= (env->icount_decr.u16.low + env->icount_extra);
1002 }
1003 return qemu_icount_bias + (icount << icount_time_shift);
1004}
Blue Swirl262353c2010-05-04 19:55:35 +00001005
Stefan Weil9a78eea2010-10-22 23:03:33 +02001006void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +00001007{
1008 /* XXX: implement xxx_cpu_list for targets that still miss it */
1009#if defined(cpu_list_id)
1010 cpu_list_id(f, cpu_fprintf, optarg);
1011#elif defined(cpu_list)
1012 cpu_list(f, cpu_fprintf); /* deprecated */
1013#endif
1014}