]> git.proxmox.com Git - mirror_qemu.git/blame - cpus.c
kvm: Call qemu_kvm_eat_signals also under !CONFIG_IOTHREAD
[mirror_qemu.git] / cpus.c
CommitLineData
296af7c9
BS
1/*
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"
262ea18e 33#include "exec-all.h"
296af7c9
BS
34
35#include "cpus.h"
a8486bc9 36#include "compatfd.h"
c0532a76
MT
37#ifdef CONFIG_LINUX
38#include <sys/prctl.h>
39#endif
296af7c9 40
7277e027
BS
41#ifdef SIGRTMIN
42#define SIG_IPI (SIGRTMIN+4)
43#else
44#define SIG_IPI SIGUSR1
45#endif
46
c0532a76
MT
47#ifndef PR_MCE_KILL
48#define PR_MCE_KILL 33
49#endif
50
296af7c9
BS
51static 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
3ae9501c
MT
102int cpu_is_stopped(CPUState *env)
103{
104 return !vm_running || env->stopped;
105}
106
296af7c9
BS
107static 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);
55df6f33
MT
114 qemu_aio_flush();
115 bdrv_flush_all();
296af7c9
BS
116 monitor_protocol_event(QEVENT_STOP, NULL);
117 }
118}
119
120static int cpu_can_run(CPUState *env)
121{
122 if (env->stop)
123 return 0;
55274a30 124 if (env->stopped || !vm_running)
296af7c9
BS
125 return 0;
126 return 1;
127}
128
129static int cpu_has_work(CPUState *env)
130{
131 if (env->stop)
132 return 1;
e82bcec2
MT
133 if (env->queued_work_first)
134 return 1;
55274a30 135 if (env->stopped || !vm_running)
296af7c9
BS
136 return 0;
137 if (!env->halted)
138 return 1;
139 if (qemu_cpu_has_work(env))
140 return 1;
141 return 0;
142}
143
472fb0c4 144static int any_cpu_has_work(void)
296af7c9
BS
145{
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
3c638d06
JK
154static 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
296af7c9
BS
161#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. */
26a82330 167 static const uint64_t val = 1;
296af7c9
BS
168 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}
55f8d6ac 225
55f8d6ac
JK
226static void dummy_signal(int sig)
227{
228}
55f8d6ac 229
9a36085b
JK
230static void sigbus_reraise(void);
231
232static void qemu_kvm_eat_signals(CPUState *env)
233{
234 struct timespec ts = { 0, 0 };
235 siginfo_t siginfo;
236 sigset_t waitset;
237 sigset_t chkset;
238 int r;
239
240 sigemptyset(&waitset);
241 sigaddset(&waitset, SIG_IPI);
242 sigaddset(&waitset, SIGBUS);
243
244 do {
245 r = sigtimedwait(&waitset, &siginfo, &ts);
246 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
247 perror("sigtimedwait");
248 exit(1);
249 }
250
251 switch (r) {
252#ifdef CONFIG_IOTHREAD
253 case SIGBUS:
254 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
255 sigbus_reraise();
256 }
257 break;
258#endif
259 default:
260 break;
261 }
262
263 r = sigpending(&chkset);
264 if (r == -1) {
265 perror("sigpending");
266 exit(1);
267 }
268 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
269}
270
55f8d6ac
JK
271#else /* _WIN32 */
272
296af7c9
BS
273HANDLE qemu_event_handle;
274
275static void dummy_event_handler(void *opaque)
276{
277}
278
279static int qemu_event_init(void)
280{
281 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
282 if (!qemu_event_handle) {
283 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
284 return -1;
285 }
286 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
287 return 0;
288}
289
290static void qemu_event_increment(void)
291{
292 if (!SetEvent(qemu_event_handle)) {
293 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
294 GetLastError());
295 exit (1);
296 }
297}
9a36085b
JK
298
299static void qemu_kvm_eat_signals(CPUState *env)
300{
301}
55f8d6ac 302#endif /* _WIN32 */
296af7c9
BS
303
304#ifndef CONFIG_IOTHREAD
ff48eb5f
JK
305static void qemu_kvm_init_cpu_signals(CPUState *env)
306{
307#ifndef _WIN32
308 int r;
309 sigset_t set;
310 struct sigaction sigact;
311
312 memset(&sigact, 0, sizeof(sigact));
313 sigact.sa_handler = dummy_signal;
314 sigaction(SIG_IPI, &sigact, NULL);
315
316 sigemptyset(&set);
317 sigaddset(&set, SIG_IPI);
318 pthread_sigmask(SIG_BLOCK, &set, NULL);
319
320 pthread_sigmask(SIG_BLOCK, NULL, &set);
321 sigdelset(&set, SIG_IPI);
322 sigdelset(&set, SIGBUS);
323 r = kvm_set_signal_mask(env, &set);
324 if (r) {
325 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
326 exit(1);
327 }
328#endif
329}
330
296af7c9
BS
331int qemu_init_main_loop(void)
332{
3c638d06
JK
333 cpu_set_debug_excp_handler(cpu_debug_handler);
334
296af7c9
BS
335 return qemu_event_init();
336}
337
7277e027
BS
338void qemu_main_loop_start(void)
339{
340}
341
296af7c9
BS
342void qemu_init_vcpu(void *_env)
343{
344 CPUState *env = _env;
84b4915d 345 int r;
296af7c9
BS
346
347 env->nr_cores = smp_cores;
348 env->nr_threads = smp_threads;
84b4915d
JK
349
350 if (kvm_enabled()) {
351 r = kvm_init_vcpu(env);
352 if (r < 0) {
353 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
354 exit(1);
355 }
ff48eb5f 356 qemu_kvm_init_cpu_signals(env);
84b4915d 357 }
296af7c9
BS
358}
359
360int qemu_cpu_self(void *env)
361{
362 return 1;
363}
364
e82bcec2
MT
365void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
366{
367 func(data);
368}
369
296af7c9
BS
370void resume_all_vcpus(void)
371{
372}
373
374void pause_all_vcpus(void)
375{
376}
377
378void qemu_cpu_kick(void *env)
379{
380 return;
381}
382
383void qemu_notify_event(void)
384{
385 CPUState *env = cpu_single_env;
386
387 qemu_event_increment ();
388 if (env) {
389 cpu_exit(env);
390 }
391 if (next_cpu && env != next_cpu) {
392 cpu_exit(next_cpu);
393 }
38145df2 394 exit_request = 1;
296af7c9
BS
395}
396
397void qemu_mutex_lock_iothread(void) {}
398void qemu_mutex_unlock_iothread(void) {}
399
b4a3d965
JK
400void cpu_stop_current(void)
401{
402}
403
296af7c9
BS
404void vm_stop(int reason)
405{
406 do_vm_stop(reason);
407}
408
409#else /* CONFIG_IOTHREAD */
410
411#include "qemu-thread.h"
412
413QemuMutex qemu_global_mutex;
414static QemuMutex qemu_fair_mutex;
415
416static QemuThread io_thread;
417
418static QemuThread *tcg_cpu_thread;
419static QemuCond *tcg_halt_cond;
420
421static int qemu_system_ready;
422/* cpu creation */
423static QemuCond qemu_cpu_cond;
424/* system init */
425static QemuCond qemu_system_cond;
426static QemuCond qemu_pause_cond;
e82bcec2 427static QemuCond qemu_work_cond;
296af7c9 428
a8486bc9
MT
429/* If we have signalfd, we mask out the signals we want to handle and then
430 * use signalfd to listen for them. We rely on whatever the current signal
431 * handler is to dispatch the signals when we receive them.
432 */
433static void sigfd_handler(void *opaque)
434{
435 int fd = (unsigned long) opaque;
436 struct qemu_signalfd_siginfo info;
437 struct sigaction action;
438 ssize_t len;
439
440 while (1) {
441 do {
442 len = read(fd, &info, sizeof(info));
443 } while (len == -1 && errno == EINTR);
444
445 if (len == -1 && errno == EAGAIN) {
446 break;
447 }
448
449 if (len != sizeof(info)) {
450 printf("read from sigfd returned %zd: %m\n", len);
451 return;
452 }
453
454 sigaction(info.ssi_signo, NULL, &action);
455 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
456 action.sa_sigaction(info.ssi_signo,
457 (siginfo_t *)&info, NULL);
458 } else if (action.sa_handler) {
459 action.sa_handler(info.ssi_signo);
460 }
461 }
462}
463
55f8d6ac
JK
464static void cpu_signal(int sig)
465{
466 if (cpu_single_env) {
467 cpu_exit(cpu_single_env);
468 }
469 exit_request = 1;
470}
471
472static void qemu_kvm_init_cpu_signals(CPUState *env)
473{
474 int r;
475 sigset_t set;
476 struct sigaction sigact;
477
478 memset(&sigact, 0, sizeof(sigact));
479 sigact.sa_handler = dummy_signal;
480 sigaction(SIG_IPI, &sigact, NULL);
481
482 pthread_sigmask(SIG_BLOCK, NULL, &set);
483 sigdelset(&set, SIG_IPI);
484 sigdelset(&set, SIGBUS);
485 r = kvm_set_signal_mask(env, &set);
486 if (r) {
487 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
488 exit(1);
489 }
490}
491
492static void qemu_tcg_init_cpu_signals(void)
493{
494 sigset_t set;
495 struct sigaction sigact;
496
497 memset(&sigact, 0, sizeof(sigact));
498 sigact.sa_handler = cpu_signal;
499 sigaction(SIG_IPI, &sigact, NULL);
500
501 sigemptyset(&set);
502 sigaddset(&set, SIG_IPI);
503 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
504}
505
506static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
507 void *ctx);
508
509static sigset_t block_io_signals(void)
510{
511 sigset_t set;
512 struct sigaction action;
513
514 /* SIGUSR2 used by posix-aio-compat.c */
515 sigemptyset(&set);
516 sigaddset(&set, SIGUSR2);
517 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
518
519 sigemptyset(&set);
520 sigaddset(&set, SIGIO);
521 sigaddset(&set, SIGALRM);
522 sigaddset(&set, SIG_IPI);
523 sigaddset(&set, SIGBUS);
524 pthread_sigmask(SIG_BLOCK, &set, NULL);
525
526 memset(&action, 0, sizeof(action));
527 action.sa_flags = SA_SIGINFO;
528 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
529 sigaction(SIGBUS, &action, NULL);
530 prctl(PR_MCE_KILL, 1, 1, 0, 0);
531
532 return set;
533}
534
a8486bc9
MT
535static int qemu_signalfd_init(sigset_t mask)
536{
537 int sigfd;
538
539 sigfd = qemu_signalfd(&mask);
540 if (sigfd == -1) {
541 fprintf(stderr, "failed to create signalfd\n");
542 return -errno;
543 }
544
545 fcntl_setfl(sigfd, O_NONBLOCK);
546
547 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
548 (void *)(unsigned long) sigfd);
549
550 return 0;
551}
296af7c9
BS
552
553int qemu_init_main_loop(void)
554{
555 int ret;
a8486bc9 556 sigset_t blocked_signals;
296af7c9 557
3c638d06
JK
558 cpu_set_debug_excp_handler(cpu_debug_handler);
559
a8486bc9
MT
560 blocked_signals = block_io_signals();
561
562 ret = qemu_signalfd_init(blocked_signals);
563 if (ret)
564 return ret;
565
566 /* Note eventfd must be drained before signalfd handlers run */
296af7c9
BS
567 ret = qemu_event_init();
568 if (ret)
569 return ret;
570
571 qemu_cond_init(&qemu_pause_cond);
f8ca7b43 572 qemu_cond_init(&qemu_system_cond);
296af7c9
BS
573 qemu_mutex_init(&qemu_fair_mutex);
574 qemu_mutex_init(&qemu_global_mutex);
575 qemu_mutex_lock(&qemu_global_mutex);
576
296af7c9
BS
577 qemu_thread_self(&io_thread);
578
579 return 0;
580}
581
7277e027
BS
582void qemu_main_loop_start(void)
583{
584 qemu_system_ready = 1;
585 qemu_cond_broadcast(&qemu_system_cond);
586}
587
e82bcec2
MT
588void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
589{
590 struct qemu_work_item wi;
591
592 if (qemu_cpu_self(env)) {
593 func(data);
594 return;
595 }
596
597 wi.func = func;
598 wi.data = data;
599 if (!env->queued_work_first)
600 env->queued_work_first = &wi;
601 else
602 env->queued_work_last->next = &wi;
603 env->queued_work_last = &wi;
604 wi.next = NULL;
605 wi.done = false;
606
607 qemu_cpu_kick(env);
608 while (!wi.done) {
609 CPUState *self_env = cpu_single_env;
610
611 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
612 cpu_single_env = self_env;
613 }
614}
615
616static void flush_queued_work(CPUState *env)
617{
618 struct qemu_work_item *wi;
619
620 if (!env->queued_work_first)
621 return;
622
623 while ((wi = env->queued_work_first)) {
624 env->queued_work_first = wi->next;
625 wi->func(wi->data);
626 wi->done = true;
627 }
628 env->queued_work_last = NULL;
629 qemu_cond_broadcast(&qemu_work_cond);
630}
631
296af7c9
BS
632static void qemu_wait_io_event_common(CPUState *env)
633{
634 if (env->stop) {
635 env->stop = 0;
636 env->stopped = 1;
637 qemu_cond_signal(&qemu_pause_cond);
638 }
e82bcec2 639 flush_queued_work(env);
aa2c364b 640 env->thread_kicked = false;
296af7c9
BS
641}
642
6cabe1f3 643static void qemu_tcg_wait_io_event(void)
296af7c9 644{
6cabe1f3
JK
645 CPUState *env;
646
472fb0c4 647 while (!any_cpu_has_work())
6cabe1f3 648 qemu_cond_timedwait(tcg_halt_cond, &qemu_global_mutex, 1000);
296af7c9
BS
649
650 qemu_mutex_unlock(&qemu_global_mutex);
651
652 /*
653 * Users of qemu_global_mutex can be starved, having no chance
654 * to acquire it since this path will get to it first.
655 * So use another lock to provide fairness.
656 */
657 qemu_mutex_lock(&qemu_fair_mutex);
658 qemu_mutex_unlock(&qemu_fair_mutex);
659
660 qemu_mutex_lock(&qemu_global_mutex);
6cabe1f3
JK
661
662 for (env = first_cpu; env != NULL; env = env->next_cpu) {
663 qemu_wait_io_event_common(env);
664 }
296af7c9
BS
665}
666
c0532a76
MT
667static void sigbus_reraise(void)
668{
669 sigset_t set;
670 struct sigaction action;
671
672 memset(&action, 0, sizeof(action));
673 action.sa_handler = SIG_DFL;
674 if (!sigaction(SIGBUS, &action, NULL)) {
675 raise(SIGBUS);
676 sigemptyset(&set);
677 sigaddset(&set, SIGBUS);
678 sigprocmask(SIG_UNBLOCK, &set, NULL);
679 }
680 perror("Failed to re-raise SIGBUS!\n");
681 abort();
682}
683
684static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
685 void *ctx)
686{
a1b87fe0 687 if (kvm_on_sigbus(siginfo->ssi_code, (void *)(intptr_t)siginfo->ssi_addr)) {
c0532a76 688 sigbus_reraise();
a1b87fe0 689 }
c0532a76
MT
690}
691
296af7c9
BS
692static void qemu_kvm_wait_io_event(CPUState *env)
693{
694 while (!cpu_has_work(env))
695 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
696
5db5bdac 697 qemu_kvm_eat_signals(env);
296af7c9
BS
698 qemu_wait_io_event_common(env);
699}
700
701static int qemu_cpu_exec(CPUState *env);
702
703static void *kvm_cpu_thread_fn(void *arg)
704{
705 CPUState *env = arg;
84b4915d 706 int r;
296af7c9 707
6164e6d6 708 qemu_mutex_lock(&qemu_global_mutex);
296af7c9 709 qemu_thread_self(env->thread);
d31ae052 710
84b4915d
JK
711 r = kvm_init_vcpu(env);
712 if (r < 0) {
713 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
714 exit(1);
715 }
296af7c9 716
55f8d6ac 717 qemu_kvm_init_cpu_signals(env);
296af7c9
BS
718
719 /* signal CPU creation */
296af7c9
BS
720 env->created = 1;
721 qemu_cond_signal(&qemu_cpu_cond);
722
723 /* and wait for machine initialization */
724 while (!qemu_system_ready)
725 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
726
727 while (1) {
728 if (cpu_can_run(env))
729 qemu_cpu_exec(env);
730 qemu_kvm_wait_io_event(env);
731 }
732
733 return NULL;
734}
735
736static void *tcg_cpu_thread_fn(void *arg)
737{
738 CPUState *env = arg;
739
55f8d6ac 740 qemu_tcg_init_cpu_signals();
296af7c9
BS
741 qemu_thread_self(env->thread);
742
743 /* signal CPU creation */
744 qemu_mutex_lock(&qemu_global_mutex);
745 for (env = first_cpu; env != NULL; env = env->next_cpu)
746 env->created = 1;
747 qemu_cond_signal(&qemu_cpu_cond);
748
749 /* and wait for machine initialization */
750 while (!qemu_system_ready)
751 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
752
753 while (1) {
472fb0c4 754 cpu_exec_all();
6cabe1f3 755 qemu_tcg_wait_io_event();
296af7c9
BS
756 }
757
758 return NULL;
759}
760
761void qemu_cpu_kick(void *_env)
762{
763 CPUState *env = _env;
764 qemu_cond_broadcast(env->halt_cond);
aa2c364b
JK
765 if (!env->thread_kicked) {
766 qemu_thread_signal(env->thread, SIG_IPI);
767 env->thread_kicked = true;
768 }
296af7c9
BS
769}
770
771int qemu_cpu_self(void *_env)
772{
773 CPUState *env = _env;
774 QemuThread this;
775
776 qemu_thread_self(&this);
777
778 return qemu_thread_equal(&this, env->thread);
779}
780
296af7c9
BS
781void qemu_mutex_lock_iothread(void)
782{
783 if (kvm_enabled()) {
296af7c9 784 qemu_mutex_lock(&qemu_global_mutex);
1a28cac3
MT
785 } else {
786 qemu_mutex_lock(&qemu_fair_mutex);
787 if (qemu_mutex_trylock(&qemu_global_mutex)) {
788 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
789 qemu_mutex_lock(&qemu_global_mutex);
790 }
791 qemu_mutex_unlock(&qemu_fair_mutex);
792 }
296af7c9
BS
793}
794
795void qemu_mutex_unlock_iothread(void)
796{
797 qemu_mutex_unlock(&qemu_global_mutex);
798}
799
800static int all_vcpus_paused(void)
801{
802 CPUState *penv = first_cpu;
803
804 while (penv) {
805 if (!penv->stopped)
806 return 0;
807 penv = (CPUState *)penv->next_cpu;
808 }
809
810 return 1;
811}
812
813void pause_all_vcpus(void)
814{
815 CPUState *penv = first_cpu;
816
817 while (penv) {
818 penv->stop = 1;
296af7c9
BS
819 qemu_cpu_kick(penv);
820 penv = (CPUState *)penv->next_cpu;
821 }
822
823 while (!all_vcpus_paused()) {
824 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
825 penv = first_cpu;
826 while (penv) {
1fbb22e5 827 qemu_cpu_kick(penv);
296af7c9
BS
828 penv = (CPUState *)penv->next_cpu;
829 }
830 }
831}
832
833void resume_all_vcpus(void)
834{
835 CPUState *penv = first_cpu;
836
837 while (penv) {
838 penv->stop = 0;
839 penv->stopped = 0;
296af7c9
BS
840 qemu_cpu_kick(penv);
841 penv = (CPUState *)penv->next_cpu;
842 }
843}
844
845static void tcg_init_vcpu(void *_env)
846{
847 CPUState *env = _env;
848 /* share a single thread for all cpus with TCG */
849 if (!tcg_cpu_thread) {
850 env->thread = qemu_mallocz(sizeof(QemuThread));
851 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
852 qemu_cond_init(env->halt_cond);
853 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
854 while (env->created == 0)
855 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
856 tcg_cpu_thread = env->thread;
857 tcg_halt_cond = env->halt_cond;
858 } else {
859 env->thread = tcg_cpu_thread;
860 env->halt_cond = tcg_halt_cond;
861 }
862}
863
864static void kvm_start_vcpu(CPUState *env)
865{
866 env->thread = qemu_mallocz(sizeof(QemuThread));
867 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
868 qemu_cond_init(env->halt_cond);
869 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
870 while (env->created == 0)
871 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
872}
873
874void qemu_init_vcpu(void *_env)
875{
876 CPUState *env = _env;
877
878 env->nr_cores = smp_cores;
879 env->nr_threads = smp_threads;
880 if (kvm_enabled())
881 kvm_start_vcpu(env);
882 else
883 tcg_init_vcpu(env);
884}
885
886void qemu_notify_event(void)
887{
888 qemu_event_increment();
889}
890
891static void qemu_system_vmstop_request(int reason)
892{
893 vmstop_requested = reason;
894 qemu_notify_event();
895}
896
b4a3d965
JK
897void cpu_stop_current(void)
898{
899 if (cpu_single_env) {
900 cpu_single_env->stopped = 1;
901 cpu_exit(cpu_single_env);
902 }
903}
904
296af7c9
BS
905void vm_stop(int reason)
906{
907 QemuThread me;
908 qemu_thread_self(&me);
909
910 if (!qemu_thread_equal(&me, &io_thread)) {
911 qemu_system_vmstop_request(reason);
912 /*
913 * FIXME: should not return to device code in case
914 * vm_stop() has been requested.
915 */
b4a3d965 916 cpu_stop_current();
296af7c9
BS
917 return;
918 }
919 do_vm_stop(reason);
920}
921
922#endif
923
924static int qemu_cpu_exec(CPUState *env)
925{
926 int ret;
927#ifdef CONFIG_PROFILER
928 int64_t ti;
929#endif
930
931#ifdef CONFIG_PROFILER
932 ti = profile_getclock();
933#endif
934 if (use_icount) {
935 int64_t count;
936 int decr;
937 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
938 env->icount_decr.u16.low = 0;
939 env->icount_extra = 0;
940 count = qemu_icount_round (qemu_next_deadline());
941 qemu_icount += count;
942 decr = (count > 0xffff) ? 0xffff : count;
943 count -= decr;
944 env->icount_decr.u16.low = decr;
945 env->icount_extra = count;
946 }
947 ret = cpu_exec(env);
948#ifdef CONFIG_PROFILER
949 qemu_time += profile_getclock() - ti;
950#endif
951 if (use_icount) {
952 /* Fold pending instructions back into the
953 instruction counter, and clear the interrupt flag. */
954 qemu_icount -= (env->icount_decr.u16.low
955 + env->icount_extra);
956 env->icount_decr.u32 = 0;
957 env->icount_extra = 0;
958 }
959 return ret;
960}
961
472fb0c4 962bool cpu_exec_all(void)
296af7c9 963{
9a36085b
JK
964 int r;
965
296af7c9
BS
966 if (next_cpu == NULL)
967 next_cpu = first_cpu;
c629a4bc 968 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
345f4426 969 CPUState *env = next_cpu;
296af7c9
BS
970
971 qemu_clock_enable(vm_clock,
345f4426 972 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
296af7c9
BS
973
974 if (qemu_alarm_pending())
975 break;
3c638d06 976 if (cpu_can_run(env)) {
9a36085b
JK
977 r = qemu_cpu_exec(env);
978 if (kvm_enabled()) {
979 qemu_kvm_eat_signals(env);
980 }
981 if (r == EXCP_DEBUG) {
3c638d06
JK
982 break;
983 }
984 } else if (env->stop) {
296af7c9
BS
985 break;
986 }
987 }
c629a4bc 988 exit_request = 0;
472fb0c4 989 return any_cpu_has_work();
296af7c9
BS
990}
991
992void set_numa_modes(void)
993{
994 CPUState *env;
995 int i;
996
997 for (env = first_cpu; env != NULL; env = env->next_cpu) {
998 for (i = 0; i < nb_numa_nodes; i++) {
999 if (node_cpumask[i] & (1 << env->cpu_index)) {
1000 env->numa_node = i;
1001 }
1002 }
1003 }
1004}
1005
1006void set_cpu_log(const char *optarg)
1007{
1008 int mask;
1009 const CPULogItem *item;
1010
1011 mask = cpu_str_to_log_mask(optarg);
1012 if (!mask) {
1013 printf("Log items (comma separated):\n");
1014 for (item = cpu_log_items; item->mask != 0; item++) {
1015 printf("%-10s %s\n", item->name, item->help);
1016 }
1017 exit(1);
1018 }
1019 cpu_set_log(mask);
1020}
29e922b6
BS
1021
1022/* Return the virtual CPU time, based on the instruction counter. */
1023int64_t cpu_get_icount(void)
1024{
1025 int64_t icount;
1026 CPUState *env = cpu_single_env;;
1027
1028 icount = qemu_icount;
1029 if (env) {
1030 if (!can_do_io(env)) {
1031 fprintf(stderr, "Bad clock read\n");
1032 }
1033 icount -= (env->icount_decr.u16.low + env->icount_extra);
1034 }
1035 return qemu_icount_bias + (icount << icount_time_shift);
1036}
262353cb 1037
9a78eead 1038void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
262353cb
BS
1039{
1040 /* XXX: implement xxx_cpu_list for targets that still miss it */
1041#if defined(cpu_list_id)
1042 cpu_list_id(f, cpu_fprintf, optarg);
1043#elif defined(cpu_list)
1044 cpu_list(f, cpu_fprintf); /* deprecated */
1045#endif
1046}