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