blob: 38756572d993a0700678f35be6ec711e5d37ce48 [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"
Blue Swirl296af7c2010-03-29 19:23:50 +000037
Blue Swirl7277e022010-04-12 17:19:06 +000038#ifdef SIGRTMIN
39#define SIG_IPI (SIGRTMIN+4)
40#else
41#define SIG_IPI SIGUSR1
42#endif
43
Blue Swirl296af7c2010-03-29 19:23:50 +000044static CPUState *next_cpu;
45
46/***********************************************************/
47void hw_error(const char *fmt, ...)
48{
49 va_list ap;
50 CPUState *env;
51
52 va_start(ap, fmt);
53 fprintf(stderr, "qemu: hardware error: ");
54 vfprintf(stderr, fmt, ap);
55 fprintf(stderr, "\n");
56 for(env = first_cpu; env != NULL; env = env->next_cpu) {
57 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
58#ifdef TARGET_I386
59 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
60#else
61 cpu_dump_state(env, stderr, fprintf, 0);
62#endif
63 }
64 va_end(ap);
65 abort();
66}
67
68void cpu_synchronize_all_states(void)
69{
70 CPUState *cpu;
71
72 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
73 cpu_synchronize_state(cpu);
74 }
75}
76
77void cpu_synchronize_all_post_reset(void)
78{
79 CPUState *cpu;
80
81 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
82 cpu_synchronize_post_reset(cpu);
83 }
84}
85
86void cpu_synchronize_all_post_init(void)
87{
88 CPUState *cpu;
89
90 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
91 cpu_synchronize_post_init(cpu);
92 }
93}
94
Marcelo Tosatti3ae95012010-05-04 09:45:24 -030095int cpu_is_stopped(CPUState *env)
96{
97 return !vm_running || env->stopped;
98}
99
Blue Swirl296af7c2010-03-29 19:23:50 +0000100static void do_vm_stop(int reason)
101{
102 if (vm_running) {
103 cpu_disable_ticks();
104 vm_running = 0;
105 pause_all_vcpus();
106 vm_state_notify(0, reason);
107 monitor_protocol_event(QEVENT_STOP, NULL);
108 }
109}
110
111static int cpu_can_run(CPUState *env)
112{
113 if (env->stop)
114 return 0;
Paolo Bonzini55274a32010-04-07 00:11:09 +0200115 if (env->stopped || !vm_running)
Blue Swirl296af7c2010-03-29 19:23:50 +0000116 return 0;
117 return 1;
118}
119
120static int cpu_has_work(CPUState *env)
121{
122 if (env->stop)
123 return 1;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300124 if (env->queued_work_first)
125 return 1;
Paolo Bonzini55274a32010-04-07 00:11:09 +0200126 if (env->stopped || !vm_running)
Blue Swirl296af7c2010-03-29 19:23:50 +0000127 return 0;
128 if (!env->halted)
129 return 1;
130 if (qemu_cpu_has_work(env))
131 return 1;
132 return 0;
133}
134
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200135static int any_cpu_has_work(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000136{
137 CPUState *env;
138
139 for (env = first_cpu; env != NULL; env = env->next_cpu)
140 if (cpu_has_work(env))
141 return 1;
142 return 0;
143}
144
Jan Kiszka3c638d02010-06-25 16:56:56 +0200145static void cpu_debug_handler(CPUState *env)
146{
147 gdb_set_stop_cpu(env);
148 debug_requested = EXCP_DEBUG;
149 vm_stop(EXCP_DEBUG);
150}
151
Blue Swirl296af7c2010-03-29 19:23:50 +0000152#ifndef _WIN32
153static int io_thread_fd = -1;
154
155static void qemu_event_increment(void)
156{
157 /* Write 8 bytes to be compatible with eventfd. */
Blue Swirl26a82332010-05-14 19:32:21 +0000158 static const uint64_t val = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000159 ssize_t ret;
160
161 if (io_thread_fd == -1)
162 return;
163
164 do {
165 ret = write(io_thread_fd, &val, sizeof(val));
166 } while (ret < 0 && errno == EINTR);
167
168 /* EAGAIN is fine, a read must be pending. */
169 if (ret < 0 && errno != EAGAIN) {
170 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
171 strerror(errno));
172 exit (1);
173 }
174}
175
176static void qemu_event_read(void *opaque)
177{
178 int fd = (unsigned long)opaque;
179 ssize_t len;
180 char buffer[512];
181
182 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
183 do {
184 len = read(fd, buffer, sizeof(buffer));
185 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
186}
187
188static int qemu_event_init(void)
189{
190 int err;
191 int fds[2];
192
193 err = qemu_eventfd(fds);
194 if (err == -1)
195 return -errno;
196
197 err = fcntl_setfl(fds[0], O_NONBLOCK);
198 if (err < 0)
199 goto fail;
200
201 err = fcntl_setfl(fds[1], O_NONBLOCK);
202 if (err < 0)
203 goto fail;
204
205 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
206 (void *)(unsigned long)fds[0]);
207
208 io_thread_fd = fds[1];
209 return 0;
210
211fail:
212 close(fds[0]);
213 close(fds[1]);
214 return err;
215}
216#else
217HANDLE qemu_event_handle;
218
219static void dummy_event_handler(void *opaque)
220{
221}
222
223static int qemu_event_init(void)
224{
225 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
226 if (!qemu_event_handle) {
227 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
228 return -1;
229 }
230 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
231 return 0;
232}
233
234static void qemu_event_increment(void)
235{
236 if (!SetEvent(qemu_event_handle)) {
237 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
238 GetLastError());
239 exit (1);
240 }
241}
242#endif
243
244#ifndef CONFIG_IOTHREAD
245int qemu_init_main_loop(void)
246{
Jan Kiszka3c638d02010-06-25 16:56:56 +0200247 cpu_set_debug_excp_handler(cpu_debug_handler);
248
Blue Swirl296af7c2010-03-29 19:23:50 +0000249 return qemu_event_init();
250}
251
Blue Swirl7277e022010-04-12 17:19:06 +0000252void qemu_main_loop_start(void)
253{
254}
255
Blue Swirl296af7c2010-03-29 19:23:50 +0000256void qemu_init_vcpu(void *_env)
257{
258 CPUState *env = _env;
259
260 env->nr_cores = smp_cores;
261 env->nr_threads = smp_threads;
262 if (kvm_enabled())
263 kvm_init_vcpu(env);
264 return;
265}
266
267int qemu_cpu_self(void *env)
268{
269 return 1;
270}
271
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300272void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
273{
274 func(data);
275}
276
Blue Swirl296af7c2010-03-29 19:23:50 +0000277void resume_all_vcpus(void)
278{
279}
280
281void pause_all_vcpus(void)
282{
283}
284
285void qemu_cpu_kick(void *env)
286{
287 return;
288}
289
290void qemu_notify_event(void)
291{
292 CPUState *env = cpu_single_env;
293
294 qemu_event_increment ();
295 if (env) {
296 cpu_exit(env);
297 }
298 if (next_cpu && env != next_cpu) {
299 cpu_exit(next_cpu);
300 }
301}
302
303void qemu_mutex_lock_iothread(void) {}
304void qemu_mutex_unlock_iothread(void) {}
305
306void vm_stop(int reason)
307{
308 do_vm_stop(reason);
309}
310
311#else /* CONFIG_IOTHREAD */
312
313#include "qemu-thread.h"
314
315QemuMutex qemu_global_mutex;
316static QemuMutex qemu_fair_mutex;
317
318static QemuThread io_thread;
319
320static QemuThread *tcg_cpu_thread;
321static QemuCond *tcg_halt_cond;
322
323static int qemu_system_ready;
324/* cpu creation */
325static QemuCond qemu_cpu_cond;
326/* system init */
327static QemuCond qemu_system_cond;
328static QemuCond qemu_pause_cond;
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300329static QemuCond qemu_work_cond;
Blue Swirl296af7c2010-03-29 19:23:50 +0000330
Paolo Bonzini55541c82010-06-03 15:20:32 +0200331static void tcg_init_ipi(void);
332static void kvm_init_ipi(CPUState *env);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300333static sigset_t block_io_signals(void);
334
335/* If we have signalfd, we mask out the signals we want to handle and then
336 * use signalfd to listen for them. We rely on whatever the current signal
337 * handler is to dispatch the signals when we receive them.
338 */
339static void sigfd_handler(void *opaque)
340{
341 int fd = (unsigned long) opaque;
342 struct qemu_signalfd_siginfo info;
343 struct sigaction action;
344 ssize_t len;
345
346 while (1) {
347 do {
348 len = read(fd, &info, sizeof(info));
349 } while (len == -1 && errno == EINTR);
350
351 if (len == -1 && errno == EAGAIN) {
352 break;
353 }
354
355 if (len != sizeof(info)) {
356 printf("read from sigfd returned %zd: %m\n", len);
357 return;
358 }
359
360 sigaction(info.ssi_signo, NULL, &action);
361 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
362 action.sa_sigaction(info.ssi_signo,
363 (siginfo_t *)&info, NULL);
364 } else if (action.sa_handler) {
365 action.sa_handler(info.ssi_signo);
366 }
367 }
368}
369
370static int qemu_signalfd_init(sigset_t mask)
371{
372 int sigfd;
373
374 sigfd = qemu_signalfd(&mask);
375 if (sigfd == -1) {
376 fprintf(stderr, "failed to create signalfd\n");
377 return -errno;
378 }
379
380 fcntl_setfl(sigfd, O_NONBLOCK);
381
382 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
383 (void *)(unsigned long) sigfd);
384
385 return 0;
386}
Blue Swirl296af7c2010-03-29 19:23:50 +0000387
388int qemu_init_main_loop(void)
389{
390 int ret;
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300391 sigset_t blocked_signals;
Blue Swirl296af7c2010-03-29 19:23:50 +0000392
Jan Kiszka3c638d02010-06-25 16:56:56 +0200393 cpu_set_debug_excp_handler(cpu_debug_handler);
394
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300395 blocked_signals = block_io_signals();
396
397 ret = qemu_signalfd_init(blocked_signals);
398 if (ret)
399 return ret;
400
401 /* Note eventfd must be drained before signalfd handlers run */
Blue Swirl296af7c2010-03-29 19:23:50 +0000402 ret = qemu_event_init();
403 if (ret)
404 return ret;
405
406 qemu_cond_init(&qemu_pause_cond);
Jan Kiszkaf8ca7b42010-06-25 16:56:51 +0200407 qemu_cond_init(&qemu_system_cond);
Blue Swirl296af7c2010-03-29 19:23:50 +0000408 qemu_mutex_init(&qemu_fair_mutex);
409 qemu_mutex_init(&qemu_global_mutex);
410 qemu_mutex_lock(&qemu_global_mutex);
411
Blue Swirl296af7c2010-03-29 19:23:50 +0000412 qemu_thread_self(&io_thread);
413
414 return 0;
415}
416
Blue Swirl7277e022010-04-12 17:19:06 +0000417void qemu_main_loop_start(void)
418{
419 qemu_system_ready = 1;
420 qemu_cond_broadcast(&qemu_system_cond);
421}
422
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300423void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
424{
425 struct qemu_work_item wi;
426
427 if (qemu_cpu_self(env)) {
428 func(data);
429 return;
430 }
431
432 wi.func = func;
433 wi.data = data;
434 if (!env->queued_work_first)
435 env->queued_work_first = &wi;
436 else
437 env->queued_work_last->next = &wi;
438 env->queued_work_last = &wi;
439 wi.next = NULL;
440 wi.done = false;
441
442 qemu_cpu_kick(env);
443 while (!wi.done) {
444 CPUState *self_env = cpu_single_env;
445
446 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
447 cpu_single_env = self_env;
448 }
449}
450
451static void flush_queued_work(CPUState *env)
452{
453 struct qemu_work_item *wi;
454
455 if (!env->queued_work_first)
456 return;
457
458 while ((wi = env->queued_work_first)) {
459 env->queued_work_first = wi->next;
460 wi->func(wi->data);
461 wi->done = true;
462 }
463 env->queued_work_last = NULL;
464 qemu_cond_broadcast(&qemu_work_cond);
465}
466
Blue Swirl296af7c2010-03-29 19:23:50 +0000467static void qemu_wait_io_event_common(CPUState *env)
468{
469 if (env->stop) {
470 env->stop = 0;
471 env->stopped = 1;
472 qemu_cond_signal(&qemu_pause_cond);
473 }
Marcelo Tosattie82bcec2010-05-04 09:45:22 -0300474 flush_queued_work(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000475}
476
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200477static void qemu_tcg_wait_io_event(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000478{
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200479 CPUState *env;
480
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200481 while (!any_cpu_has_work())
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200482 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
Blue Swirl296af7c2010-03-29 19:23:50 +0000483
484 qemu_mutex_unlock(&qemu_global_mutex);
485
486 /*
487 * Users of qemu_global_mutex can be starved, having no chance
488 * to acquire it since this path will get to it first.
489 * So use another lock to provide fairness.
490 */
491 qemu_mutex_lock(&qemu_fair_mutex);
492 qemu_mutex_unlock(&qemu_fair_mutex);
493
494 qemu_mutex_lock(&qemu_global_mutex);
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200495
496 for (env = first_cpu; env != NULL; env = env->next_cpu) {
497 qemu_wait_io_event_common(env);
498 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000499}
500
501static void qemu_kvm_eat_signal(CPUState *env, int timeout)
502{
503 struct timespec ts;
504 int r, e;
505 siginfo_t siginfo;
506 sigset_t waitset;
507
508 ts.tv_sec = timeout / 1000;
509 ts.tv_nsec = (timeout % 1000) * 1000000;
510
511 sigemptyset(&waitset);
512 sigaddset(&waitset, SIG_IPI);
513
514 qemu_mutex_unlock(&qemu_global_mutex);
515 r = sigtimedwait(&waitset, &siginfo, &ts);
516 e = errno;
517 qemu_mutex_lock(&qemu_global_mutex);
518
519 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
520 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
521 exit(1);
522 }
523}
524
525static void qemu_kvm_wait_io_event(CPUState *env)
526{
527 while (!cpu_has_work(env))
528 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
529
530 qemu_kvm_eat_signal(env, 0);
531 qemu_wait_io_event_common(env);
532}
533
534static int qemu_cpu_exec(CPUState *env);
535
536static void *kvm_cpu_thread_fn(void *arg)
537{
538 CPUState *env = arg;
539
Marcelo Tosatti6164e6d2010-03-23 13:37:13 -0300540 qemu_mutex_lock(&qemu_global_mutex);
Blue Swirl296af7c2010-03-29 19:23:50 +0000541 qemu_thread_self(env->thread);
542 if (kvm_enabled())
543 kvm_init_vcpu(env);
544
Paolo Bonzini55541c82010-06-03 15:20:32 +0200545 kvm_init_ipi(env);
Blue Swirl296af7c2010-03-29 19:23:50 +0000546
547 /* signal CPU creation */
Blue Swirl296af7c2010-03-29 19:23:50 +0000548 env->created = 1;
549 qemu_cond_signal(&qemu_cpu_cond);
550
551 /* and wait for machine initialization */
552 while (!qemu_system_ready)
553 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
554
555 while (1) {
556 if (cpu_can_run(env))
557 qemu_cpu_exec(env);
558 qemu_kvm_wait_io_event(env);
559 }
560
561 return NULL;
562}
563
564static void *tcg_cpu_thread_fn(void *arg)
565{
566 CPUState *env = arg;
567
Paolo Bonzini55541c82010-06-03 15:20:32 +0200568 tcg_init_ipi();
Blue Swirl296af7c2010-03-29 19:23:50 +0000569 qemu_thread_self(env->thread);
570
571 /* signal CPU creation */
572 qemu_mutex_lock(&qemu_global_mutex);
573 for (env = first_cpu; env != NULL; env = env->next_cpu)
574 env->created = 1;
575 qemu_cond_signal(&qemu_cpu_cond);
576
577 /* and wait for machine initialization */
578 while (!qemu_system_ready)
579 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
580
581 while (1) {
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200582 cpu_exec_all();
Jan Kiszka6cabe1f2010-06-25 16:56:53 +0200583 qemu_tcg_wait_io_event();
Blue Swirl296af7c2010-03-29 19:23:50 +0000584 }
585
586 return NULL;
587}
588
589void qemu_cpu_kick(void *_env)
590{
591 CPUState *env = _env;
592 qemu_cond_broadcast(env->halt_cond);
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300593 qemu_thread_signal(env->thread, SIG_IPI);
Blue Swirl296af7c2010-03-29 19:23:50 +0000594}
595
596int qemu_cpu_self(void *_env)
597{
598 CPUState *env = _env;
599 QemuThread this;
600
601 qemu_thread_self(&this);
602
603 return qemu_thread_equal(&this, env->thread);
604}
605
606static void cpu_signal(int sig)
607{
608 if (cpu_single_env)
609 cpu_exit(cpu_single_env);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300610 exit_request = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000611}
612
Paolo Bonzini55541c82010-06-03 15:20:32 +0200613static void tcg_init_ipi(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000614{
615 sigset_t set;
616 struct sigaction sigact;
617
Paolo Bonzini55541c82010-06-03 15:20:32 +0200618 memset(&sigact, 0, sizeof(sigact));
619 sigact.sa_handler = cpu_signal;
620 sigaction(SIG_IPI, &sigact, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000621
622 sigemptyset(&set);
623 sigaddset(&set, SIG_IPI);
624 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Blue Swirl296af7c2010-03-29 19:23:50 +0000625}
626
627static void dummy_signal(int sig)
628{
629}
630
Paolo Bonzini55541c82010-06-03 15:20:32 +0200631static void kvm_init_ipi(CPUState *env)
Blue Swirl296af7c2010-03-29 19:23:50 +0000632{
633 int r;
634 sigset_t set;
635 struct sigaction sigact;
636
Blue Swirl296af7c2010-03-29 19:23:50 +0000637 memset(&sigact, 0, sizeof(sigact));
638 sigact.sa_handler = dummy_signal;
639 sigaction(SIG_IPI, &sigact, NULL);
640
Paolo Bonzini55541c82010-06-03 15:20:32 +0200641 pthread_sigmask(SIG_BLOCK, NULL, &set);
642 sigdelset(&set, SIG_IPI);
Blue Swirl296af7c2010-03-29 19:23:50 +0000643 r = kvm_set_signal_mask(env, &set);
644 if (r) {
645 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
646 exit(1);
647 }
648}
649
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300650static sigset_t block_io_signals(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000651{
652 sigset_t set;
653
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300654 /* SIGUSR2 used by posix-aio-compat.c */
Blue Swirl296af7c2010-03-29 19:23:50 +0000655 sigemptyset(&set);
656 sigaddset(&set, SIGUSR2);
Blue Swirl296af7c2010-03-29 19:23:50 +0000657 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
658
659 sigemptyset(&set);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300660 sigaddset(&set, SIGIO);
661 sigaddset(&set, SIGALRM);
Blue Swirl296af7c2010-03-29 19:23:50 +0000662 sigaddset(&set, SIG_IPI);
663 pthread_sigmask(SIG_BLOCK, &set, NULL);
Marcelo Tosattia8486bc2010-10-11 15:31:16 -0300664
665 return set;
Blue Swirl296af7c2010-03-29 19:23:50 +0000666}
667
Blue Swirl296af7c2010-03-29 19:23:50 +0000668void qemu_mutex_lock_iothread(void)
669{
670 if (kvm_enabled()) {
671 qemu_mutex_lock(&qemu_fair_mutex);
672 qemu_mutex_lock(&qemu_global_mutex);
673 qemu_mutex_unlock(&qemu_fair_mutex);
Marcelo Tosatti1a28cac2010-05-04 09:45:20 -0300674 } else {
675 qemu_mutex_lock(&qemu_fair_mutex);
676 if (qemu_mutex_trylock(&qemu_global_mutex)) {
677 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
678 qemu_mutex_lock(&qemu_global_mutex);
679 }
680 qemu_mutex_unlock(&qemu_fair_mutex);
681 }
Blue Swirl296af7c2010-03-29 19:23:50 +0000682}
683
684void qemu_mutex_unlock_iothread(void)
685{
686 qemu_mutex_unlock(&qemu_global_mutex);
687}
688
689static int all_vcpus_paused(void)
690{
691 CPUState *penv = first_cpu;
692
693 while (penv) {
694 if (!penv->stopped)
695 return 0;
696 penv = (CPUState *)penv->next_cpu;
697 }
698
699 return 1;
700}
701
702void pause_all_vcpus(void)
703{
704 CPUState *penv = first_cpu;
705
706 while (penv) {
707 penv->stop = 1;
Blue Swirl296af7c2010-03-29 19:23:50 +0000708 qemu_cpu_kick(penv);
709 penv = (CPUState *)penv->next_cpu;
710 }
711
712 while (!all_vcpus_paused()) {
713 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
714 penv = first_cpu;
715 while (penv) {
Marcelo Tosatti1fbb22e2010-05-04 09:45:21 -0300716 qemu_cpu_kick(penv);
Blue Swirl296af7c2010-03-29 19:23:50 +0000717 penv = (CPUState *)penv->next_cpu;
718 }
719 }
720}
721
722void resume_all_vcpus(void)
723{
724 CPUState *penv = first_cpu;
725
726 while (penv) {
727 penv->stop = 0;
728 penv->stopped = 0;
Blue Swirl296af7c2010-03-29 19:23:50 +0000729 qemu_cpu_kick(penv);
730 penv = (CPUState *)penv->next_cpu;
731 }
732}
733
734static void tcg_init_vcpu(void *_env)
735{
736 CPUState *env = _env;
737 /* share a single thread for all cpus with TCG */
738 if (!tcg_cpu_thread) {
739 env->thread = qemu_mallocz(sizeof(QemuThread));
740 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
741 qemu_cond_init(env->halt_cond);
742 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
743 while (env->created == 0)
744 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
745 tcg_cpu_thread = env->thread;
746 tcg_halt_cond = env->halt_cond;
747 } else {
748 env->thread = tcg_cpu_thread;
749 env->halt_cond = tcg_halt_cond;
750 }
751}
752
753static void kvm_start_vcpu(CPUState *env)
754{
755 env->thread = qemu_mallocz(sizeof(QemuThread));
756 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
757 qemu_cond_init(env->halt_cond);
758 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
759 while (env->created == 0)
760 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
761}
762
763void qemu_init_vcpu(void *_env)
764{
765 CPUState *env = _env;
766
767 env->nr_cores = smp_cores;
768 env->nr_threads = smp_threads;
769 if (kvm_enabled())
770 kvm_start_vcpu(env);
771 else
772 tcg_init_vcpu(env);
773}
774
775void qemu_notify_event(void)
776{
777 qemu_event_increment();
778}
779
780static void qemu_system_vmstop_request(int reason)
781{
782 vmstop_requested = reason;
783 qemu_notify_event();
784}
785
786void vm_stop(int reason)
787{
788 QemuThread me;
789 qemu_thread_self(&me);
790
791 if (!qemu_thread_equal(&me, &io_thread)) {
792 qemu_system_vmstop_request(reason);
793 /*
794 * FIXME: should not return to device code in case
795 * vm_stop() has been requested.
796 */
797 if (cpu_single_env) {
798 cpu_exit(cpu_single_env);
799 cpu_single_env->stop = 1;
800 }
801 return;
802 }
803 do_vm_stop(reason);
804}
805
806#endif
807
808static int qemu_cpu_exec(CPUState *env)
809{
810 int ret;
811#ifdef CONFIG_PROFILER
812 int64_t ti;
813#endif
814
815#ifdef CONFIG_PROFILER
816 ti = profile_getclock();
817#endif
818 if (use_icount) {
819 int64_t count;
820 int decr;
821 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
822 env->icount_decr.u16.low = 0;
823 env->icount_extra = 0;
824 count = qemu_icount_round (qemu_next_deadline());
825 qemu_icount += count;
826 decr = (count > 0xffff) ? 0xffff : count;
827 count -= decr;
828 env->icount_decr.u16.low = decr;
829 env->icount_extra = count;
830 }
831 ret = cpu_exec(env);
832#ifdef CONFIG_PROFILER
833 qemu_time += profile_getclock() - ti;
834#endif
835 if (use_icount) {
836 /* Fold pending instructions back into the
837 instruction counter, and clear the interrupt flag. */
838 qemu_icount -= (env->icount_decr.u16.low
839 + env->icount_extra);
840 env->icount_decr.u32 = 0;
841 env->icount_extra = 0;
842 }
843 return ret;
844}
845
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200846bool cpu_exec_all(void)
Blue Swirl296af7c2010-03-29 19:23:50 +0000847{
Blue Swirl296af7c2010-03-29 19:23:50 +0000848 if (next_cpu == NULL)
849 next_cpu = first_cpu;
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200850 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
Jan Kiszka345f4422010-06-25 16:56:54 +0200851 CPUState *env = next_cpu;
Blue Swirl296af7c2010-03-29 19:23:50 +0000852
853 qemu_clock_enable(vm_clock,
Jan Kiszka345f4422010-06-25 16:56:54 +0200854 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
Blue Swirl296af7c2010-03-29 19:23:50 +0000855
856 if (qemu_alarm_pending())
857 break;
Jan Kiszka3c638d02010-06-25 16:56:56 +0200858 if (cpu_can_run(env)) {
859 if (qemu_cpu_exec(env) == EXCP_DEBUG) {
860 break;
861 }
862 } else if (env->stop) {
Blue Swirl296af7c2010-03-29 19:23:50 +0000863 break;
864 }
865 }
Jan Kiszkac629a4b2010-06-25 16:56:52 +0200866 exit_request = 0;
Jan Kiszka472fb0c2010-06-25 16:56:55 +0200867 return any_cpu_has_work();
Blue Swirl296af7c2010-03-29 19:23:50 +0000868}
869
870void set_numa_modes(void)
871{
872 CPUState *env;
873 int i;
874
875 for (env = first_cpu; env != NULL; env = env->next_cpu) {
876 for (i = 0; i < nb_numa_nodes; i++) {
877 if (node_cpumask[i] & (1 << env->cpu_index)) {
878 env->numa_node = i;
879 }
880 }
881 }
882}
883
884void set_cpu_log(const char *optarg)
885{
886 int mask;
887 const CPULogItem *item;
888
889 mask = cpu_str_to_log_mask(optarg);
890 if (!mask) {
891 printf("Log items (comma separated):\n");
892 for (item = cpu_log_items; item->mask != 0; item++) {
893 printf("%-10s %s\n", item->name, item->help);
894 }
895 exit(1);
896 }
897 cpu_set_log(mask);
898}
Blue Swirl29e922b2010-03-29 19:24:00 +0000899
900/* Return the virtual CPU time, based on the instruction counter. */
901int64_t cpu_get_icount(void)
902{
903 int64_t icount;
904 CPUState *env = cpu_single_env;;
905
906 icount = qemu_icount;
907 if (env) {
908 if (!can_do_io(env)) {
909 fprintf(stderr, "Bad clock read\n");
910 }
911 icount -= (env->icount_decr.u16.low + env->icount_extra);
912 }
913 return qemu_icount_bias + (icount << icount_time_shift);
914}
Blue Swirl262353c2010-05-04 19:55:35 +0000915
916void list_cpus(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
917 const char *optarg)
918{
919 /* XXX: implement xxx_cpu_list for targets that still miss it */
920#if defined(cpu_list_id)
921 cpu_list_id(f, cpu_fprintf, optarg);
922#elif defined(cpu_list)
923 cpu_list(f, cpu_fprintf); /* deprecated */
924#endif
925}