blob: ab6e40e93d5c886db295601546ac532462e6a45d [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;
268
269 env->nr_cores = smp_cores;
270 env->nr_threads = smp_threads;
271 if (kvm_enabled())
272 kvm_init_vcpu(env);
273 return;
274}
275
276int qemu_cpu_self(void *env)
277{
278 return 1;
279}
280
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300281void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
282{
283 func(data);
284}
285
Blue Swirl296af7c2010-03-29 19:23:50 +0000286void resume_all_vcpus(void)
287{
288}
289
290void pause_all_vcpus(void)
291{
292}
293
294void qemu_cpu_kick(void *env)
295{
296 return;
297}
298
299void qemu_notify_event(void)
300{
301 CPUState *env = cpu_single_env;
302
303 qemu_event_increment ();
304 if (env) {
305 cpu_exit(env);
306 }
307 if (next_cpu && env != next_cpu) {
308 cpu_exit(next_cpu);
309 }
310}
311
312void qemu_mutex_lock_iothread(void) {}
313void qemu_mutex_unlock_iothread(void) {}
314
315void vm_stop(int reason)
316{
317 do_vm_stop(reason);
318}
319
320#else /* CONFIG_IOTHREAD */
321
322#include "qemu-thread.h"
323
324QemuMutex qemu_global_mutex;
325static QemuMutex qemu_fair_mutex;
326
327static QemuThread io_thread;
328
329static QemuThread *tcg_cpu_thread;
330static QemuCond *tcg_halt_cond;
331
332static int qemu_system_ready;
333/* cpu creation */
334static QemuCond qemu_cpu_cond;
335/* system init */
336static QemuCond qemu_system_cond;
337static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300338static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000339
Paolo Bonzini55541c82010-06-03 15:20:32 +0200340static void tcg_init_ipi(void);
341static void kvm_init_ipi(CPUState *env);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300342static sigset_t block_io_signals(void);
343
344/* If we have signalfd, we mask out the signals we want to handle and then
345 * use signalfd to listen for them. We rely on whatever the current signal
346 * handler is to dispatch the signals when we receive them.
347 */
348static void sigfd_handler(void *opaque)
349{
350 int fd = (unsigned long) opaque;
351 struct qemu_signalfd_siginfo info;
352 struct sigaction action;
353 ssize_t len;
354
355 while (1) {
356 do {
357 len = read(fd, &info, sizeof(info));
358 } while (len == -1 && errno == EINTR);
359
360 if (len == -1 && errno == EAGAIN) {
361 break;
362 }
363
364 if (len != sizeof(info)) {
365 printf("read from sigfd returned %zd: %m\n", len);
366 return;
367 }
368
369 sigaction(info.ssi_signo, NULL, &action);
370 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
371 action.sa_sigaction(info.ssi_signo,
372 (siginfo_t *)&info, NULL);
373 } else if (action.sa_handler) {
374 action.sa_handler(info.ssi_signo);
375 }
376 }
377}
378
379static int qemu_signalfd_init(sigset_t mask)
380{
381 int sigfd;
382
383 sigfd = qemu_signalfd(&mask);
384 if (sigfd == -1) {
385 fprintf(stderr, "failed to create signalfd\n");
386 return -errno;
387 }
388
389 fcntl_setfl(sigfd, O_NONBLOCK);
390
391 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
392 (void *)(unsigned long) sigfd);
393
394 return 0;
395}
Blue Swirl296af7c2010-03-29 19:23:50 +0000396
397int qemu_init_main_loop(void)
398{
399 int ret;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300400 sigset_t blocked_signals;
Blue Swirl296af7c2010-03-29 19:23:50 +0000401
Jan Kiszka3c638d02010-06-25 16:56:56 +0200402 cpu_set_debug_excp_handler(cpu_debug_handler);
403
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300404 blocked_signals = block_io_signals();
405
406 ret = qemu_signalfd_init(blocked_signals);
407 if (ret)
408 return ret;
409
410 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000411 ret = qemu_event_init();
412 if (ret)
413 return ret;
414
415 qemu_cond_init(&qemu_pause_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200416 qemu_cond_init(&qemu_system_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000417 qemu_mutex_init(&qemu_fair_mutex);
418 qemu_mutex_init(&qemu_global_mutex);
419 qemu_mutex_lock(&qemu_global_mutex);
420
Blue Swirl296af7c2010-03-29 19:23:50 +0000421 qemu_thread_self(&io_thread);
422
423 return 0;
424}
425
Blue Swirl7277e022010-04-12 17:19:06 +0000426void qemu_main_loop_start(void)
427{
428 qemu_system_ready = 1;
429 qemu_cond_broadcast(&qemu_system_cond);
430}
431
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300432void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
433{
434 struct qemu_work_item wi;
435
436 if (qemu_cpu_self(env)) {
437 func(data);
438 return;
439 }
440
441 wi.func = func;
442 wi.data = data;
443 if (!env->queued_work_first)
444 env->queued_work_first = &wi;
445 else
446 env->queued_work_last->next = &wi;
447 env->queued_work_last = &wi;
448 wi.next = NULL;
449 wi.done = false;
450
451 qemu_cpu_kick(env);
452 while (!wi.done) {
453 CPUState *self_env = cpu_single_env;
454
455 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
456 cpu_single_env = self_env;
457 }
458}
459
460static void flush_queued_work(CPUState *env)
461{
462 struct qemu_work_item *wi;
463
464 if (!env->queued_work_first)
465 return;
466
467 while ((wi = env->queued_work_first)) {
468 env->queued_work_first = wi->next;
469 wi->func(wi->data);
470 wi->done = true;
471 }
472 env->queued_work_last = NULL;
473 qemu_cond_broadcast(&qemu_work_cond);
474}
475
Blue Swirl296af7c2010-03-29 19:23:50 +0000476static void qemu_wait_io_event_common(CPUState *env)
477{
478 if (env->stop) {
479 env->stop = 0;
480 env->stopped = 1;
481 qemu_cond_signal(&qemu_pause_cond);
482 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300483 flush_queued_work(env);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100484 env->thread_kicked = false;
Blue Swirl296af7c2010-03-29 19:23:50 +0000485}
486
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200487static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000488{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200489 CPUState *env;
490
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200491 while (!any_cpu_has_work())
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200492 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
Blue Swirl296af7c2010-03-29 19:23:50 +0000493
494 qemu_mutex_unlock(&qemu_global_mutex);
495
496 /*
497 * Users of qemu_global_mutex can be starved, having no chance
498 * to acquire it since this path will get to it first.
499 * So use another lock to provide fairness.
500 */
501 qemu_mutex_lock(&qemu_fair_mutex);
502 qemu_mutex_unlock(&qemu_fair_mutex);
503
504 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200505
506 for (env = first_cpu; env != NULL; env = env->next_cpu) {
507 qemu_wait_io_event_common(env);
508 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000509}
510
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300511static void sigbus_reraise(void)
512{
513 sigset_t set;
514 struct sigaction action;
515
516 memset(&action, 0, sizeof(action));
517 action.sa_handler = SIG_DFL;
518 if (!sigaction(SIGBUS, &action, NULL)) {
519 raise(SIGBUS);
520 sigemptyset(&set);
521 sigaddset(&set, SIGBUS);
522 sigprocmask(SIG_UNBLOCK, &set, NULL);
523 }
524 perror("Failed to re-raise SIGBUS!\n");
525 abort();
526}
527
528static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
529 void *ctx)
530{
531#if defined(TARGET_I386)
532 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr))
533#endif
534 sigbus_reraise();
535}
536
Blue Swirl296af7c2010-03-29 19:23:50 +0000537static void qemu_kvm_eat_signal(CPUState *env, int timeout)
538{
539 struct timespec ts;
540 int r, e;
541 siginfo_t siginfo;
542 sigset_t waitset;
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300543 sigset_t chkset;
Blue Swirl296af7c2010-03-29 19:23:50 +0000544
545 ts.tv_sec = timeout / 1000;
546 ts.tv_nsec = (timeout % 1000) * 1000000;
547
548 sigemptyset(&waitset);
549 sigaddset(&waitset, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300550 sigaddset(&waitset, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000551
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300552 do {
553 qemu_mutex_unlock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000554
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300555 r = sigtimedwait(&waitset, &siginfo, &ts);
556 e = errno;
557
558 qemu_mutex_lock(&qemu_global_mutex);
559
560 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
561 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
562 exit(1);
563 }
564
565 switch (r) {
566 case SIGBUS:
567#ifdef TARGET_I386
568 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr))
569#endif
570 sigbus_reraise();
571 break;
572 default:
573 break;
574 }
575
576 r = sigpending(&chkset);
577 if (r == -1) {
578 fprintf(stderr, "sigpending: %s\n", strerror(e));
579 exit(1);
580 }
581 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
Blue Swirl296af7c2010-03-29 19:23:50 +0000582}
583
584static void qemu_kvm_wait_io_event(CPUState *env)
585{
586 while (!cpu_has_work(env))
587 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
588
589 qemu_kvm_eat_signal(env, 0);
590 qemu_wait_io_event_common(env);
591}
592
593static int qemu_cpu_exec(CPUState *env);
594
595static void *kvm_cpu_thread_fn(void *arg)
596{
597 CPUState *env = arg;
598
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300599 qemu_mutex_lock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000600 qemu_thread_self(env->thread);
601 if (kvm_enabled())
602 kvm_init_vcpu(env);
603
Paolo Bonzini55541c82010-06-03 15:20:32 +0200604 kvm_init_ipi(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000605
606 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000607 env->created = 1;
608 qemu_cond_signal(&qemu_cpu_cond);
609
610 /* and wait for machine initialization */
611 while (!qemu_system_ready)
612 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
613
614 while (1) {
615 if (cpu_can_run(env))
616 qemu_cpu_exec(env);
617 qemu_kvm_wait_io_event(env);
618 }
619
620 return NULL;
621}
622
623static void *tcg_cpu_thread_fn(void *arg)
624{
625 CPUState *env = arg;
626
Paolo Bonzini55541c82010-06-03 15:20:32 +0200627 tcg_init_ipi();
Blue Swirl296af7c2010-03-29 19:23:50 +0000628 qemu_thread_self(env->thread);
629
630 /* signal CPU creation */
631 qemu_mutex_lock(&qemu_global_mutex);
632 for (env = first_cpu; env != NULL; env = env->next_cpu)
633 env->created = 1;
634 qemu_cond_signal(&qemu_cpu_cond);
635
636 /* and wait for machine initialization */
637 while (!qemu_system_ready)
638 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
639
640 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200641 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200642 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000643 }
644
645 return NULL;
646}
647
648void qemu_cpu_kick(void *_env)
649{
650 CPUState *env = _env;
651 qemu_cond_broadcast(env->halt_cond);
Jan Kiszkaaa2c3642011-02-01 22:15:42 +0100652 if (!env->thread_kicked) {
653 qemu_thread_signal(env->thread, SIG_IPI);
654 env->thread_kicked = true;
655 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000656}
657
658int qemu_cpu_self(void *_env)
659{
660 CPUState *env = _env;
661 QemuThread this;
662
663 qemu_thread_self(&this);
664
665 return qemu_thread_equal(&this, env->thread);
666}
667
668static void cpu_signal(int sig)
669{
670 if (cpu_single_env)
671 cpu_exit(cpu_single_env);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300672 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000673}
674
Paolo Bonzini55541c82010-06-03 15:20:32 +0200675static void tcg_init_ipi(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000676{
677 sigset_t set;
678 struct sigaction sigact;
679
Paolo Bonzini55541c82010-06-03 15:20:32 +0200680 memset(&sigact, 0, sizeof(sigact));
681 sigact.sa_handler = cpu_signal;
682 sigaction(SIG_IPI, &sigact, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000683
684 sigemptyset(&set);
685 sigaddset(&set, SIG_IPI);
686 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000687}
688
689static void dummy_signal(int sig)
690{
691}
692
Paolo Bonzini55541c82010-06-03 15:20:32 +0200693static void kvm_init_ipi(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000694{
695 int r;
696 sigset_t set;
697 struct sigaction sigact;
698
Blue Swirl296af7c2010-03-29 19:23:50 +0000699 memset(&sigact, 0, sizeof(sigact));
700 sigact.sa_handler = dummy_signal;
701 sigaction(SIG_IPI, &sigact, NULL);
702
Paolo Bonzini55541c82010-06-03 15:20:32 +0200703 pthread_sigmask(SIG_BLOCK, NULL, &set);
704 sigdelset(&set, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300705 sigdelset(&set, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000706 r = kvm_set_signal_mask(env, &set);
707 if (r) {
708 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
709 exit(1);
710 }
711}
712
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300713static sigset_t block_io_signals(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000714{
715 sigset_t set;
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300716 struct sigaction action;
Blue Swirl296af7c2010-03-29 19:23:50 +0000717
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300718 /* SIGUSR2 used by posix-aio-compat.c */
Blue Swirl296af7c2010-03-29 19:23:50 +0000719 sigemptyset(&set);
720 sigaddset(&set, SIGUSR2);
Blue Swirl296af7c2010-03-29 19:23:50 +0000721 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
722
723 sigemptyset(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300724 sigaddset(&set, SIGIO);
725 sigaddset(&set, SIGALRM);
Blue Swirl296af7c2010-03-29 19:23:50 +0000726 sigaddset(&set, SIG_IPI);
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300727 sigaddset(&set, SIGBUS);
Blue Swirl296af7c2010-03-29 19:23:50 +0000728 pthread_sigmask(SIG_BLOCK, &set, NULL);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300729
Marcelo Tosattic0532a72010-10-11 15:31:21 -0300730 memset(&action, 0, sizeof(action));
731 action.sa_flags = SA_SIGINFO;
732 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
733 sigaction(SIGBUS, &action, NULL);
734 prctl(PR_MCE_KILL, 1, 1, 0, 0);
735
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300736 return set;
Blue Swirl296af7c2010-03-29 19:23:50 +0000737}
738
Blue Swirl296af7c2010-03-29 19:23:50 +0000739void qemu_mutex_lock_iothread(void)
740{
741 if (kvm_enabled()) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000742 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300743 } else {
744 qemu_mutex_lock(&qemu_fair_mutex);
745 if (qemu_mutex_trylock(&qemu_global_mutex)) {
746 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
747 qemu_mutex_lock(&qemu_global_mutex);
748 }
749 qemu_mutex_unlock(&qemu_fair_mutex);
750 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000751}
752
753void qemu_mutex_unlock_iothread(void)
754{
755 qemu_mutex_unlock(&qemu_global_mutex);
756}
757
758static int all_vcpus_paused(void)
759{
760 CPUState *penv = first_cpu;
761
762 while (penv) {
763 if (!penv->stopped)
764 return 0;
765 penv = (CPUState *)penv->next_cpu;
766 }
767
768 return 1;
769}
770
771void pause_all_vcpus(void)
772{
773 CPUState *penv = first_cpu;
774
775 while (penv) {
776 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000777 qemu_cpu_kick(penv);
778 penv = (CPUState *)penv->next_cpu;
779 }
780
781 while (!all_vcpus_paused()) {
782 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
783 penv = first_cpu;
784 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300785 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000786 penv = (CPUState *)penv->next_cpu;
787 }
788 }
789}
790
791void resume_all_vcpus(void)
792{
793 CPUState *penv = first_cpu;
794
795 while (penv) {
796 penv->stop = 0;
797 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000798 qemu_cpu_kick(penv);
799 penv = (CPUState *)penv->next_cpu;
800 }
801}
802
803static void tcg_init_vcpu(void *_env)
804{
805 CPUState *env = _env;
806 /* share a single thread for all cpus with TCG */
807 if (!tcg_cpu_thread) {
808 env->thread = qemu_mallocz(sizeof(QemuThread));
809 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
810 qemu_cond_init(env->halt_cond);
811 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
812 while (env->created == 0)
813 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
814 tcg_cpu_thread = env->thread;
815 tcg_halt_cond = env->halt_cond;
816 } else {
817 env->thread = tcg_cpu_thread;
818 env->halt_cond = tcg_halt_cond;
819 }
820}
821
822static void kvm_start_vcpu(CPUState *env)
823{
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, kvm_cpu_thread_fn, env);
828 while (env->created == 0)
829 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
830}
831
832void qemu_init_vcpu(void *_env)
833{
834 CPUState *env = _env;
835
836 env->nr_cores = smp_cores;
837 env->nr_threads = smp_threads;
838 if (kvm_enabled())
839 kvm_start_vcpu(env);
840 else
841 tcg_init_vcpu(env);
842}
843
844void qemu_notify_event(void)
845{
846 qemu_event_increment();
847}
848
849static void qemu_system_vmstop_request(int reason)
850{
851 vmstop_requested = reason;
852 qemu_notify_event();
853}
854
855void vm_stop(int reason)
856{
857 QemuThread me;
858 qemu_thread_self(&me);
859
860 if (!qemu_thread_equal(&me, &io_thread)) {
861 qemu_system_vmstop_request(reason);
862 /*
863 * FIXME: should not return to device code in case
864 * vm_stop() has been requested.
865 */
866 if (cpu_single_env) {
867 cpu_exit(cpu_single_env);
868 cpu_single_env->stop = 1;
869 }
870 return;
871 }
872 do_vm_stop(reason);
873}
874
875#endif
876
877static int qemu_cpu_exec(CPUState *env)
878{
879 int ret;
880#ifdef CONFIG_PROFILER
881 int64_t ti;
882#endif
883
884#ifdef CONFIG_PROFILER
885 ti = profile_getclock();
886#endif
887 if (use_icount) {
888 int64_t count;
889 int decr;
890 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
891 env->icount_decr.u16.low = 0;
892 env->icount_extra = 0;
893 count = qemu_icount_round (qemu_next_deadline());
894 qemu_icount += count;
895 decr = (count > 0xffff) ? 0xffff : count;
896 count -= decr;
897 env->icount_decr.u16.low = decr;
898 env->icount_extra = count;
899 }
900 ret = cpu_exec(env);
901#ifdef CONFIG_PROFILER
902 qemu_time += profile_getclock() - ti;
903#endif
904 if (use_icount) {
905 /* Fold pending instructions back into the
906 instruction counter, and clear the interrupt flag. */
907 qemu_icount -= (env->icount_decr.u16.low
908 + env->icount_extra);
909 env->icount_decr.u32 = 0;
910 env->icount_extra = 0;
911 }
912 return ret;
913}
914
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200915bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000916{
Blue Swirl296af7c2010-03-29 19:23:50 +0000917 if (next_cpu == NULL)
918 next_cpu = first_cpu;
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200919 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +0200920 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000921
922 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +0200923 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +0000924
925 if (qemu_alarm_pending())
926 break;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200927 if (cpu_can_run(env)) {
928 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
929 break;
930 }
931 } else if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000932 break;
933 }
934 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200935 exit_request = 0;
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200936 return any_cpu_has_work();
Blue Swirl296af7c2010-03-29 19:23:50 +0000937}
938
939void set_numa_modes(void)
940{
941 CPUState *env;
942 int i;
943
944 for (env = first_cpu; env != NULL; env = env->next_cpu) {
945 for (i = 0; i < nb_numa_nodes; i++) {
946 if (node_cpumask[i] & (1 << env->cpu_index)) {
947 env->numa_node = i;
948 }
949 }
950 }
951}
952
953void set_cpu_log(const char *optarg)
954{
955 int mask;
956 const CPULogItem *item;
957
958 mask = cpu_str_to_log_mask(optarg);
959 if (!mask) {
960 printf("Log items (comma separated):\n");
961 for (item = cpu_log_items; item->mask != 0; item++) {
962 printf("%-10s %s\n", item->name, item->help);
963 }
964 exit(1);
965 }
966 cpu_set_log(mask);
967}
Blue Swirl29e922b2010-03-29 19:24:00 +0000968
969/* Return the virtual CPU time, based on the instruction counter. */
970int64_t cpu_get_icount(void)
971{
972 int64_t icount;
973 CPUState *env = cpu_single_env;;
974
975 icount = qemu_icount;
976 if (env) {
977 if (!can_do_io(env)) {
978 fprintf(stderr, "Bad clock read\n");
979 }
980 icount -= (env->icount_decr.u16.low + env->icount_extra);
981 }
982 return qemu_icount_bias + (icount << icount_time_shift);
983}
Blue Swirl262353c2010-05-04 19:55:35 +0000984
Stefan Weil9a78eea2010-10-22 23:03:33 +0200985void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
Blue Swirl262353c2010-05-04 19:55:35 +0000986{
987 /* XXX: implement xxx_cpu_list for targets that still miss it */
988#if defined(cpu_list_id)
989 cpu_list_id(f, cpu_fprintf, optarg);
990#elif defined(cpu_list)
991 cpu_list(f, cpu_fprintf); /* deprecated */
992#endif
993}