]> git.proxmox.com Git - mirror_qemu.git/blame - cpus.c
qapi: Convert the cpu command
[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"
33
96284e89 34#include "qemu-thread.h"
296af7c9 35#include "cpus.h"
44a9b356 36#include "main-loop.h"
0ff0fc19
JK
37
38#ifndef _WIN32
a8486bc9 39#include "compatfd.h"
0ff0fc19 40#endif
296af7c9 41
6d9cb73c
JK
42#ifdef CONFIG_LINUX
43
44#include <sys/prctl.h>
45
c0532a76
MT
46#ifndef PR_MCE_KILL
47#define PR_MCE_KILL 33
48#endif
49
6d9cb73c
JK
50#ifndef PR_MCE_KILL_SET
51#define PR_MCE_KILL_SET 1
52#endif
53
54#ifndef PR_MCE_KILL_EARLY
55#define PR_MCE_KILL_EARLY 1
56#endif
57
58#endif /* CONFIG_LINUX */
59
296af7c9
BS
60static CPUState *next_cpu;
61
946fb27c
PB
62/***********************************************************/
63/* guest cycle counter */
64
65/* Conversion factor from emulated instructions to virtual clock ticks. */
66static int icount_time_shift;
67/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
68#define MAX_ICOUNT_SHIFT 10
69/* Compensate for varying guest execution speed. */
70static int64_t qemu_icount_bias;
71static QEMUTimer *icount_rt_timer;
72static QEMUTimer *icount_vm_timer;
73static QEMUTimer *icount_warp_timer;
74static int64_t vm_clock_warp_start;
75static int64_t qemu_icount;
76
77typedef struct TimersState {
78 int64_t cpu_ticks_prev;
79 int64_t cpu_ticks_offset;
80 int64_t cpu_clock_offset;
81 int32_t cpu_ticks_enabled;
82 int64_t dummy;
83} TimersState;
84
85TimersState timers_state;
86
87/* Return the virtual CPU time, based on the instruction counter. */
88int64_t cpu_get_icount(void)
89{
90 int64_t icount;
91 CPUState *env = cpu_single_env;;
92
93 icount = qemu_icount;
94 if (env) {
95 if (!can_do_io(env)) {
96 fprintf(stderr, "Bad clock read\n");
97 }
98 icount -= (env->icount_decr.u16.low + env->icount_extra);
99 }
100 return qemu_icount_bias + (icount << icount_time_shift);
101}
102
103/* return the host CPU cycle counter and handle stop/restart */
104int64_t cpu_get_ticks(void)
105{
106 if (use_icount) {
107 return cpu_get_icount();
108 }
109 if (!timers_state.cpu_ticks_enabled) {
110 return timers_state.cpu_ticks_offset;
111 } else {
112 int64_t ticks;
113 ticks = cpu_get_real_ticks();
114 if (timers_state.cpu_ticks_prev > ticks) {
115 /* Note: non increasing ticks may happen if the host uses
116 software suspend */
117 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
118 }
119 timers_state.cpu_ticks_prev = ticks;
120 return ticks + timers_state.cpu_ticks_offset;
121 }
122}
123
124/* return the host CPU monotonic timer and handle stop/restart */
125int64_t cpu_get_clock(void)
126{
127 int64_t ti;
128 if (!timers_state.cpu_ticks_enabled) {
129 return timers_state.cpu_clock_offset;
130 } else {
131 ti = get_clock();
132 return ti + timers_state.cpu_clock_offset;
133 }
134}
135
136/* enable cpu_get_ticks() */
137void cpu_enable_ticks(void)
138{
139 if (!timers_state.cpu_ticks_enabled) {
140 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
141 timers_state.cpu_clock_offset -= get_clock();
142 timers_state.cpu_ticks_enabled = 1;
143 }
144}
145
146/* disable cpu_get_ticks() : the clock is stopped. You must not call
147 cpu_get_ticks() after that. */
148void cpu_disable_ticks(void)
149{
150 if (timers_state.cpu_ticks_enabled) {
151 timers_state.cpu_ticks_offset = cpu_get_ticks();
152 timers_state.cpu_clock_offset = cpu_get_clock();
153 timers_state.cpu_ticks_enabled = 0;
154 }
155}
156
157/* Correlation between real and virtual time is always going to be
158 fairly approximate, so ignore small variation.
159 When the guest is idle real and virtual time will be aligned in
160 the IO wait loop. */
161#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
162
163static void icount_adjust(void)
164{
165 int64_t cur_time;
166 int64_t cur_icount;
167 int64_t delta;
168 static int64_t last_delta;
169 /* If the VM is not running, then do nothing. */
170 if (!runstate_is_running()) {
171 return;
172 }
173 cur_time = cpu_get_clock();
174 cur_icount = qemu_get_clock_ns(vm_clock);
175 delta = cur_icount - cur_time;
176 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
177 if (delta > 0
178 && last_delta + ICOUNT_WOBBLE < delta * 2
179 && icount_time_shift > 0) {
180 /* The guest is getting too far ahead. Slow time down. */
181 icount_time_shift--;
182 }
183 if (delta < 0
184 && last_delta - ICOUNT_WOBBLE > delta * 2
185 && icount_time_shift < MAX_ICOUNT_SHIFT) {
186 /* The guest is getting too far behind. Speed time up. */
187 icount_time_shift++;
188 }
189 last_delta = delta;
190 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
191}
192
193static void icount_adjust_rt(void *opaque)
194{
195 qemu_mod_timer(icount_rt_timer,
196 qemu_get_clock_ms(rt_clock) + 1000);
197 icount_adjust();
198}
199
200static void icount_adjust_vm(void *opaque)
201{
202 qemu_mod_timer(icount_vm_timer,
203 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
204 icount_adjust();
205}
206
207static int64_t qemu_icount_round(int64_t count)
208{
209 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
210}
211
212static void icount_warp_rt(void *opaque)
213{
214 if (vm_clock_warp_start == -1) {
215 return;
216 }
217
218 if (runstate_is_running()) {
219 int64_t clock = qemu_get_clock_ns(rt_clock);
220 int64_t warp_delta = clock - vm_clock_warp_start;
221 if (use_icount == 1) {
222 qemu_icount_bias += warp_delta;
223 } else {
224 /*
225 * In adaptive mode, do not let the vm_clock run too
226 * far ahead of real time.
227 */
228 int64_t cur_time = cpu_get_clock();
229 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
230 int64_t delta = cur_time - cur_icount;
231 qemu_icount_bias += MIN(warp_delta, delta);
232 }
233 if (qemu_clock_expired(vm_clock)) {
234 qemu_notify_event();
235 }
236 }
237 vm_clock_warp_start = -1;
238}
239
240void qemu_clock_warp(QEMUClock *clock)
241{
242 int64_t deadline;
243
244 /*
245 * There are too many global variables to make the "warp" behavior
246 * applicable to other clocks. But a clock argument removes the
247 * need for if statements all over the place.
248 */
249 if (clock != vm_clock || !use_icount) {
250 return;
251 }
252
253 /*
254 * If the CPUs have been sleeping, advance the vm_clock timer now. This
255 * ensures that the deadline for the timer is computed correctly below.
256 * This also makes sure that the insn counter is synchronized before the
257 * CPU starts running, in case the CPU is woken by an event other than
258 * the earliest vm_clock timer.
259 */
260 icount_warp_rt(NULL);
261 if (!all_cpu_threads_idle() || !qemu_clock_has_timers(vm_clock)) {
262 qemu_del_timer(icount_warp_timer);
263 return;
264 }
265
266 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
267 deadline = qemu_clock_deadline(vm_clock);
268 if (deadline > 0) {
269 /*
270 * Ensure the vm_clock proceeds even when the virtual CPU goes to
271 * sleep. Otherwise, the CPU might be waiting for a future timer
272 * interrupt to wake it up, but the interrupt never comes because
273 * the vCPU isn't running any insns and thus doesn't advance the
274 * vm_clock.
275 *
276 * An extreme solution for this problem would be to never let VCPUs
277 * sleep in icount mode if there is a pending vm_clock timer; rather
278 * time could just advance to the next vm_clock event. Instead, we
279 * do stop VCPUs and only advance vm_clock after some "real" time,
280 * (related to the time left until the next event) has passed. This
281 * rt_clock timer will do this. This avoids that the warps are too
282 * visible externally---for example, you will not be sending network
283 * packets continously instead of every 100ms.
284 */
285 qemu_mod_timer(icount_warp_timer, vm_clock_warp_start + deadline);
286 } else {
287 qemu_notify_event();
288 }
289}
290
291static const VMStateDescription vmstate_timers = {
292 .name = "timer",
293 .version_id = 2,
294 .minimum_version_id = 1,
295 .minimum_version_id_old = 1,
296 .fields = (VMStateField[]) {
297 VMSTATE_INT64(cpu_ticks_offset, TimersState),
298 VMSTATE_INT64(dummy, TimersState),
299 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
300 VMSTATE_END_OF_LIST()
301 }
302};
303
304void configure_icount(const char *option)
305{
306 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
307 if (!option) {
308 return;
309 }
310
311 icount_warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
312 if (strcmp(option, "auto") != 0) {
313 icount_time_shift = strtol(option, NULL, 0);
314 use_icount = 1;
315 return;
316 }
317
318 use_icount = 2;
319
320 /* 125MIPS seems a reasonable initial guess at the guest speed.
321 It will be corrected fairly quickly anyway. */
322 icount_time_shift = 3;
323
324 /* Have both realtime and virtual time triggers for speed adjustment.
325 The realtime trigger catches emulated time passing too slowly,
326 the virtual time trigger catches emulated time passing too fast.
327 Realtime triggers occur even when idle, so use them less frequently
328 than VM triggers. */
329 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
330 qemu_mod_timer(icount_rt_timer,
331 qemu_get_clock_ms(rt_clock) + 1000);
332 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
333 qemu_mod_timer(icount_vm_timer,
334 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
335}
336
296af7c9
BS
337/***********************************************************/
338void hw_error(const char *fmt, ...)
339{
340 va_list ap;
341 CPUState *env;
342
343 va_start(ap, fmt);
344 fprintf(stderr, "qemu: hardware error: ");
345 vfprintf(stderr, fmt, ap);
346 fprintf(stderr, "\n");
347 for(env = first_cpu; env != NULL; env = env->next_cpu) {
348 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
349#ifdef TARGET_I386
350 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
351#else
352 cpu_dump_state(env, stderr, fprintf, 0);
353#endif
354 }
355 va_end(ap);
356 abort();
357}
358
359void cpu_synchronize_all_states(void)
360{
361 CPUState *cpu;
362
363 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
364 cpu_synchronize_state(cpu);
365 }
366}
367
368void cpu_synchronize_all_post_reset(void)
369{
370 CPUState *cpu;
371
372 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
373 cpu_synchronize_post_reset(cpu);
374 }
375}
376
377void cpu_synchronize_all_post_init(void)
378{
379 CPUState *cpu;
380
381 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
382 cpu_synchronize_post_init(cpu);
383 }
384}
385
3ae9501c
MT
386int cpu_is_stopped(CPUState *env)
387{
1354869c 388 return !runstate_is_running() || env->stopped;
3ae9501c
MT
389}
390
1dfb4dd9 391static void do_vm_stop(RunState state)
296af7c9 392{
1354869c 393 if (runstate_is_running()) {
296af7c9 394 cpu_disable_ticks();
296af7c9 395 pause_all_vcpus();
f5bbfba1 396 runstate_set(state);
1dfb4dd9 397 vm_state_notify(0, state);
55df6f33
MT
398 qemu_aio_flush();
399 bdrv_flush_all();
296af7c9
BS
400 monitor_protocol_event(QEVENT_STOP, NULL);
401 }
402}
403
404static int cpu_can_run(CPUState *env)
405{
0ab07c62 406 if (env->stop) {
296af7c9 407 return 0;
0ab07c62 408 }
1354869c 409 if (env->stopped || !runstate_is_running()) {
296af7c9 410 return 0;
0ab07c62 411 }
296af7c9
BS
412 return 1;
413}
414
16400322 415static bool cpu_thread_is_idle(CPUState *env)
296af7c9 416{
16400322
JK
417 if (env->stop || env->queued_work_first) {
418 return false;
419 }
1354869c 420 if (env->stopped || !runstate_is_running()) {
16400322
JK
421 return true;
422 }
f2c1cc81
JK
423 if (!env->halted || qemu_cpu_has_work(env) ||
424 (kvm_enabled() && kvm_irqchip_in_kernel())) {
16400322
JK
425 return false;
426 }
427 return true;
296af7c9
BS
428}
429
ab33fcda 430bool all_cpu_threads_idle(void)
296af7c9
BS
431{
432 CPUState *env;
433
16400322
JK
434 for (env = first_cpu; env != NULL; env = env->next_cpu) {
435 if (!cpu_thread_is_idle(env)) {
436 return false;
437 }
438 }
439 return true;
296af7c9
BS
440}
441
1009d2ed 442static void cpu_handle_guest_debug(CPUState *env)
83f338f7 443{
3c638d06 444 gdb_set_stop_cpu(env);
8cf71710 445 qemu_system_debug_request();
83f338f7 446 env->stopped = 1;
3c638d06
JK
447}
448
714bd040
PB
449static void cpu_signal(int sig)
450{
451 if (cpu_single_env) {
452 cpu_exit(cpu_single_env);
453 }
454 exit_request = 1;
455}
714bd040 456
6d9cb73c
JK
457#ifdef CONFIG_LINUX
458static void sigbus_reraise(void)
459{
460 sigset_t set;
461 struct sigaction action;
462
463 memset(&action, 0, sizeof(action));
464 action.sa_handler = SIG_DFL;
465 if (!sigaction(SIGBUS, &action, NULL)) {
466 raise(SIGBUS);
467 sigemptyset(&set);
468 sigaddset(&set, SIGBUS);
469 sigprocmask(SIG_UNBLOCK, &set, NULL);
470 }
471 perror("Failed to re-raise SIGBUS!\n");
472 abort();
473}
474
475static void sigbus_handler(int n, struct qemu_signalfd_siginfo *siginfo,
476 void *ctx)
477{
478 if (kvm_on_sigbus(siginfo->ssi_code,
479 (void *)(intptr_t)siginfo->ssi_addr)) {
480 sigbus_reraise();
481 }
482}
483
484static void qemu_init_sigbus(void)
485{
486 struct sigaction action;
487
488 memset(&action, 0, sizeof(action));
489 action.sa_flags = SA_SIGINFO;
490 action.sa_sigaction = (void (*)(int, siginfo_t*, void*))sigbus_handler;
491 sigaction(SIGBUS, &action, NULL);
492
493 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
494}
495
1ab3c6c0
JK
496static void qemu_kvm_eat_signals(CPUState *env)
497{
498 struct timespec ts = { 0, 0 };
499 siginfo_t siginfo;
500 sigset_t waitset;
501 sigset_t chkset;
502 int r;
503
504 sigemptyset(&waitset);
505 sigaddset(&waitset, SIG_IPI);
506 sigaddset(&waitset, SIGBUS);
507
508 do {
509 r = sigtimedwait(&waitset, &siginfo, &ts);
510 if (r == -1 && !(errno == EAGAIN || errno == EINTR)) {
511 perror("sigtimedwait");
512 exit(1);
513 }
514
515 switch (r) {
516 case SIGBUS:
517 if (kvm_on_sigbus_vcpu(env, siginfo.si_code, siginfo.si_addr)) {
518 sigbus_reraise();
519 }
520 break;
521 default:
522 break;
523 }
524
525 r = sigpending(&chkset);
526 if (r == -1) {
527 perror("sigpending");
528 exit(1);
529 }
530 } while (sigismember(&chkset, SIG_IPI) || sigismember(&chkset, SIGBUS));
1ab3c6c0
JK
531}
532
6d9cb73c
JK
533#else /* !CONFIG_LINUX */
534
535static void qemu_init_sigbus(void)
536{
537}
1ab3c6c0
JK
538
539static void qemu_kvm_eat_signals(CPUState *env)
540{
541}
6d9cb73c
JK
542#endif /* !CONFIG_LINUX */
543
296af7c9 544#ifndef _WIN32
55f8d6ac
JK
545static void dummy_signal(int sig)
546{
547}
55f8d6ac 548
714bd040
PB
549static void qemu_kvm_init_cpu_signals(CPUState *env)
550{
551 int r;
552 sigset_t set;
553 struct sigaction sigact;
554
555 memset(&sigact, 0, sizeof(sigact));
556 sigact.sa_handler = dummy_signal;
557 sigaction(SIG_IPI, &sigact, NULL);
558
714bd040
PB
559 pthread_sigmask(SIG_BLOCK, NULL, &set);
560 sigdelset(&set, SIG_IPI);
561 sigdelset(&set, SIGBUS);
562 r = kvm_set_signal_mask(env, &set);
563 if (r) {
564 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
565 exit(1);
566 }
714bd040 567
714bd040
PB
568 sigdelset(&set, SIG_IPI);
569 sigdelset(&set, SIGBUS);
570 r = kvm_set_signal_mask(env, &set);
571 if (r) {
572 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(-r));
573 exit(1);
574 }
575}
576
577static void qemu_tcg_init_cpu_signals(void)
578{
714bd040
PB
579 sigset_t set;
580 struct sigaction sigact;
581
582 memset(&sigact, 0, sizeof(sigact));
583 sigact.sa_handler = cpu_signal;
584 sigaction(SIG_IPI, &sigact, NULL);
585
586 sigemptyset(&set);
587 sigaddset(&set, SIG_IPI);
588 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
714bd040
PB
589}
590
55f8d6ac 591#else /* _WIN32 */
ff48eb5f
JK
592static void qemu_kvm_init_cpu_signals(CPUState *env)
593{
714bd040
PB
594 abort();
595}
ff48eb5f 596
714bd040
PB
597static void qemu_tcg_init_cpu_signals(void)
598{
ff48eb5f 599}
714bd040 600#endif /* _WIN32 */
ff48eb5f 601
296af7c9 602QemuMutex qemu_global_mutex;
46daff13
PB
603static QemuCond qemu_io_proceeded_cond;
604static bool iothread_requesting_mutex;
296af7c9
BS
605
606static QemuThread io_thread;
607
608static QemuThread *tcg_cpu_thread;
609static QemuCond *tcg_halt_cond;
610
296af7c9
BS
611/* cpu creation */
612static QemuCond qemu_cpu_cond;
613/* system init */
296af7c9 614static QemuCond qemu_pause_cond;
e82bcec2 615static QemuCond qemu_work_cond;
296af7c9 616
d3b12f5d 617void qemu_init_cpu_loop(void)
296af7c9 618{
6d9cb73c 619 qemu_init_sigbus();
ed94592b 620 qemu_cond_init(&qemu_cpu_cond);
ed94592b
AL
621 qemu_cond_init(&qemu_pause_cond);
622 qemu_cond_init(&qemu_work_cond);
46daff13 623 qemu_cond_init(&qemu_io_proceeded_cond);
296af7c9 624 qemu_mutex_init(&qemu_global_mutex);
296af7c9 625
b7680cb6 626 qemu_thread_get_self(&io_thread);
296af7c9
BS
627}
628
e82bcec2
MT
629void run_on_cpu(CPUState *env, void (*func)(void *data), void *data)
630{
631 struct qemu_work_item wi;
632
b7680cb6 633 if (qemu_cpu_is_self(env)) {
e82bcec2
MT
634 func(data);
635 return;
636 }
637
638 wi.func = func;
639 wi.data = data;
0ab07c62 640 if (!env->queued_work_first) {
e82bcec2 641 env->queued_work_first = &wi;
0ab07c62 642 } else {
e82bcec2 643 env->queued_work_last->next = &wi;
0ab07c62 644 }
e82bcec2
MT
645 env->queued_work_last = &wi;
646 wi.next = NULL;
647 wi.done = false;
648
649 qemu_cpu_kick(env);
650 while (!wi.done) {
651 CPUState *self_env = cpu_single_env;
652
653 qemu_cond_wait(&qemu_work_cond, &qemu_global_mutex);
654 cpu_single_env = self_env;
655 }
656}
657
658static void flush_queued_work(CPUState *env)
659{
660 struct qemu_work_item *wi;
661
0ab07c62 662 if (!env->queued_work_first) {
e82bcec2 663 return;
0ab07c62 664 }
e82bcec2
MT
665
666 while ((wi = env->queued_work_first)) {
667 env->queued_work_first = wi->next;
668 wi->func(wi->data);
669 wi->done = true;
670 }
671 env->queued_work_last = NULL;
672 qemu_cond_broadcast(&qemu_work_cond);
673}
674
296af7c9
BS
675static void qemu_wait_io_event_common(CPUState *env)
676{
677 if (env->stop) {
678 env->stop = 0;
679 env->stopped = 1;
680 qemu_cond_signal(&qemu_pause_cond);
681 }
e82bcec2 682 flush_queued_work(env);
aa2c364b 683 env->thread_kicked = false;
296af7c9
BS
684}
685
6cabe1f3 686static void qemu_tcg_wait_io_event(void)
296af7c9 687{
6cabe1f3
JK
688 CPUState *env;
689
16400322 690 while (all_cpu_threads_idle()) {
ab33fcda
PB
691 /* Start accounting real time to the virtual clock if the CPUs
692 are idle. */
693 qemu_clock_warp(vm_clock);
9705fbb5 694 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
16400322 695 }
296af7c9 696
46daff13
PB
697 while (iothread_requesting_mutex) {
698 qemu_cond_wait(&qemu_io_proceeded_cond, &qemu_global_mutex);
699 }
6cabe1f3
JK
700
701 for (env = first_cpu; env != NULL; env = env->next_cpu) {
702 qemu_wait_io_event_common(env);
703 }
296af7c9
BS
704}
705
296af7c9
BS
706static void qemu_kvm_wait_io_event(CPUState *env)
707{
16400322 708 while (cpu_thread_is_idle(env)) {
9705fbb5 709 qemu_cond_wait(env->halt_cond, &qemu_global_mutex);
16400322 710 }
296af7c9 711
5db5bdac 712 qemu_kvm_eat_signals(env);
296af7c9
BS
713 qemu_wait_io_event_common(env);
714}
715
7e97cd88 716static void *qemu_kvm_cpu_thread_fn(void *arg)
296af7c9
BS
717{
718 CPUState *env = arg;
84b4915d 719 int r;
296af7c9 720
6164e6d6 721 qemu_mutex_lock(&qemu_global_mutex);
b7680cb6 722 qemu_thread_get_self(env->thread);
dc7a09cf 723 env->thread_id = qemu_get_thread_id();
296af7c9 724
84b4915d
JK
725 r = kvm_init_vcpu(env);
726 if (r < 0) {
727 fprintf(stderr, "kvm_init_vcpu failed: %s\n", strerror(-r));
728 exit(1);
729 }
296af7c9 730
55f8d6ac 731 qemu_kvm_init_cpu_signals(env);
296af7c9
BS
732
733 /* signal CPU creation */
296af7c9
BS
734 env->created = 1;
735 qemu_cond_signal(&qemu_cpu_cond);
736
296af7c9 737 while (1) {
0ab07c62 738 if (cpu_can_run(env)) {
6792a57b 739 r = kvm_cpu_exec(env);
83f338f7 740 if (r == EXCP_DEBUG) {
1009d2ed 741 cpu_handle_guest_debug(env);
83f338f7 742 }
0ab07c62 743 }
296af7c9
BS
744 qemu_kvm_wait_io_event(env);
745 }
746
747 return NULL;
748}
749
7e97cd88 750static void *qemu_tcg_cpu_thread_fn(void *arg)
296af7c9
BS
751{
752 CPUState *env = arg;
753
55f8d6ac 754 qemu_tcg_init_cpu_signals();
b7680cb6 755 qemu_thread_get_self(env->thread);
296af7c9
BS
756
757 /* signal CPU creation */
758 qemu_mutex_lock(&qemu_global_mutex);
0ab07c62 759 for (env = first_cpu; env != NULL; env = env->next_cpu) {
dc7a09cf 760 env->thread_id = qemu_get_thread_id();
296af7c9 761 env->created = 1;
0ab07c62 762 }
296af7c9
BS
763 qemu_cond_signal(&qemu_cpu_cond);
764
fa7d1867
JK
765 /* wait for initial kick-off after machine start */
766 while (first_cpu->stopped) {
767 qemu_cond_wait(tcg_halt_cond, &qemu_global_mutex);
0ab07c62 768 }
296af7c9
BS
769
770 while (1) {
472fb0c4 771 cpu_exec_all();
946fb27c 772 if (use_icount && qemu_clock_deadline(vm_clock) <= 0) {
3b2319a3
PB
773 qemu_notify_event();
774 }
6cabe1f3 775 qemu_tcg_wait_io_event();
296af7c9
BS
776 }
777
778 return NULL;
779}
780
cc015e9a
PB
781static void qemu_cpu_kick_thread(CPUState *env)
782{
783#ifndef _WIN32
784 int err;
785
786 err = pthread_kill(env->thread->thread, SIG_IPI);
787 if (err) {
788 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
789 exit(1);
790 }
791#else /* _WIN32 */
792 if (!qemu_cpu_is_self(env)) {
793 SuspendThread(env->thread->thread);
794 cpu_signal(0);
795 ResumeThread(env->thread->thread);
796 }
797#endif
798}
799
296af7c9
BS
800void qemu_cpu_kick(void *_env)
801{
802 CPUState *env = _env;
296af7c9 803
296af7c9 804 qemu_cond_broadcast(env->halt_cond);
eae74cf9 805 if (kvm_enabled() && !env->thread_kicked) {
cc015e9a 806 qemu_cpu_kick_thread(env);
aa2c364b
JK
807 env->thread_kicked = true;
808 }
296af7c9
BS
809}
810
46d62fac 811void qemu_cpu_kick_self(void)
296af7c9 812{
b55c22c6 813#ifndef _WIN32
46d62fac 814 assert(cpu_single_env);
296af7c9 815
46d62fac 816 if (!cpu_single_env->thread_kicked) {
cc015e9a 817 qemu_cpu_kick_thread(cpu_single_env);
46d62fac 818 cpu_single_env->thread_kicked = true;
296af7c9 819 }
b55c22c6
PB
820#else
821 abort();
822#endif
296af7c9
BS
823}
824
b7680cb6 825int qemu_cpu_is_self(void *_env)
296af7c9 826{
296af7c9 827 CPUState *env = _env;
a8486bc9 828
b7680cb6 829 return qemu_thread_is_self(env->thread);
296af7c9
BS
830}
831
296af7c9
BS
832void qemu_mutex_lock_iothread(void)
833{
834 if (kvm_enabled()) {
296af7c9 835 qemu_mutex_lock(&qemu_global_mutex);
1a28cac3 836 } else {
46daff13 837 iothread_requesting_mutex = true;
1a28cac3 838 if (qemu_mutex_trylock(&qemu_global_mutex)) {
cc015e9a 839 qemu_cpu_kick_thread(first_cpu);
1a28cac3
MT
840 qemu_mutex_lock(&qemu_global_mutex);
841 }
46daff13
PB
842 iothread_requesting_mutex = false;
843 qemu_cond_broadcast(&qemu_io_proceeded_cond);
1a28cac3 844 }
296af7c9
BS
845}
846
847void qemu_mutex_unlock_iothread(void)
848{
849 qemu_mutex_unlock(&qemu_global_mutex);
850}
851
852static int all_vcpus_paused(void)
853{
854 CPUState *penv = first_cpu;
855
856 while (penv) {
0ab07c62 857 if (!penv->stopped) {
296af7c9 858 return 0;
0ab07c62 859 }
296af7c9
BS
860 penv = (CPUState *)penv->next_cpu;
861 }
862
863 return 1;
864}
865
866void pause_all_vcpus(void)
867{
868 CPUState *penv = first_cpu;
869
a5c57d64 870 qemu_clock_enable(vm_clock, false);
296af7c9
BS
871 while (penv) {
872 penv->stop = 1;
296af7c9
BS
873 qemu_cpu_kick(penv);
874 penv = (CPUState *)penv->next_cpu;
875 }
876
877 while (!all_vcpus_paused()) {
be7d6c57 878 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
296af7c9
BS
879 penv = first_cpu;
880 while (penv) {
1fbb22e5 881 qemu_cpu_kick(penv);
296af7c9
BS
882 penv = (CPUState *)penv->next_cpu;
883 }
884 }
885}
886
887void resume_all_vcpus(void)
888{
889 CPUState *penv = first_cpu;
890
891 while (penv) {
892 penv->stop = 0;
893 penv->stopped = 0;
296af7c9
BS
894 qemu_cpu_kick(penv);
895 penv = (CPUState *)penv->next_cpu;
896 }
897}
898
7e97cd88 899static void qemu_tcg_init_vcpu(void *_env)
296af7c9
BS
900{
901 CPUState *env = _env;
0ab07c62 902
296af7c9
BS
903 /* share a single thread for all cpus with TCG */
904 if (!tcg_cpu_thread) {
7267c094
AL
905 env->thread = g_malloc0(sizeof(QemuThread));
906 env->halt_cond = g_malloc0(sizeof(QemuCond));
296af7c9 907 qemu_cond_init(env->halt_cond);
fa7d1867 908 tcg_halt_cond = env->halt_cond;
7e97cd88 909 qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env);
0ab07c62 910 while (env->created == 0) {
18a85728 911 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
0ab07c62 912 }
296af7c9 913 tcg_cpu_thread = env->thread;
296af7c9
BS
914 } else {
915 env->thread = tcg_cpu_thread;
916 env->halt_cond = tcg_halt_cond;
917 }
918}
919
7e97cd88 920static void qemu_kvm_start_vcpu(CPUState *env)
296af7c9 921{
7267c094
AL
922 env->thread = g_malloc0(sizeof(QemuThread));
923 env->halt_cond = g_malloc0(sizeof(QemuCond));
296af7c9 924 qemu_cond_init(env->halt_cond);
7e97cd88 925 qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env);
0ab07c62 926 while (env->created == 0) {
18a85728 927 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
0ab07c62 928 }
296af7c9
BS
929}
930
931void qemu_init_vcpu(void *_env)
932{
933 CPUState *env = _env;
934
935 env->nr_cores = smp_cores;
936 env->nr_threads = smp_threads;
fa7d1867 937 env->stopped = 1;
0ab07c62 938 if (kvm_enabled()) {
7e97cd88 939 qemu_kvm_start_vcpu(env);
0ab07c62 940 } else {
7e97cd88 941 qemu_tcg_init_vcpu(env);
0ab07c62 942 }
296af7c9
BS
943}
944
b4a3d965 945void cpu_stop_current(void)
296af7c9 946{
b4a3d965 947 if (cpu_single_env) {
67bb172f 948 cpu_single_env->stop = 0;
b4a3d965
JK
949 cpu_single_env->stopped = 1;
950 cpu_exit(cpu_single_env);
67bb172f 951 qemu_cond_signal(&qemu_pause_cond);
b4a3d965 952 }
296af7c9
BS
953}
954
1dfb4dd9 955void vm_stop(RunState state)
296af7c9 956{
b7680cb6 957 if (!qemu_thread_is_self(&io_thread)) {
1dfb4dd9 958 qemu_system_vmstop_request(state);
296af7c9
BS
959 /*
960 * FIXME: should not return to device code in case
961 * vm_stop() has been requested.
962 */
b4a3d965 963 cpu_stop_current();
296af7c9
BS
964 return;
965 }
1dfb4dd9 966 do_vm_stop(state);
296af7c9
BS
967}
968
8a9236f1
LC
969/* does a state transition even if the VM is already stopped,
970 current state is forgotten forever */
971void vm_stop_force_state(RunState state)
972{
973 if (runstate_is_running()) {
974 vm_stop(state);
975 } else {
976 runstate_set(state);
977 }
978}
979
6792a57b 980static int tcg_cpu_exec(CPUState *env)
296af7c9
BS
981{
982 int ret;
983#ifdef CONFIG_PROFILER
984 int64_t ti;
985#endif
986
987#ifdef CONFIG_PROFILER
988 ti = profile_getclock();
989#endif
990 if (use_icount) {
991 int64_t count;
992 int decr;
993 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
994 env->icount_decr.u16.low = 0;
995 env->icount_extra = 0;
946fb27c 996 count = qemu_icount_round(qemu_clock_deadline(vm_clock));
296af7c9
BS
997 qemu_icount += count;
998 decr = (count > 0xffff) ? 0xffff : count;
999 count -= decr;
1000 env->icount_decr.u16.low = decr;
1001 env->icount_extra = count;
1002 }
1003 ret = cpu_exec(env);
1004#ifdef CONFIG_PROFILER
1005 qemu_time += profile_getclock() - ti;
1006#endif
1007 if (use_icount) {
1008 /* Fold pending instructions back into the
1009 instruction counter, and clear the interrupt flag. */
1010 qemu_icount -= (env->icount_decr.u16.low
1011 + env->icount_extra);
1012 env->icount_decr.u32 = 0;
1013 env->icount_extra = 0;
1014 }
1015 return ret;
1016}
1017
472fb0c4 1018bool cpu_exec_all(void)
296af7c9 1019{
9a36085b
JK
1020 int r;
1021
ab33fcda
PB
1022 /* Account partial waits to the vm_clock. */
1023 qemu_clock_warp(vm_clock);
1024
0ab07c62 1025 if (next_cpu == NULL) {
296af7c9 1026 next_cpu = first_cpu;
0ab07c62 1027 }
c629a4bc 1028 for (; next_cpu != NULL && !exit_request; next_cpu = next_cpu->next_cpu) {
345f4426 1029 CPUState *env = next_cpu;
296af7c9
BS
1030
1031 qemu_clock_enable(vm_clock,
345f4426 1032 (env->singlestep_enabled & SSTEP_NOTIMER) == 0);
296af7c9 1033
3c638d06 1034 if (cpu_can_run(env)) {
9a36085b 1035 if (kvm_enabled()) {
6792a57b 1036 r = kvm_cpu_exec(env);
9a36085b 1037 qemu_kvm_eat_signals(env);
6792a57b
JK
1038 } else {
1039 r = tcg_cpu_exec(env);
9a36085b
JK
1040 }
1041 if (r == EXCP_DEBUG) {
1009d2ed 1042 cpu_handle_guest_debug(env);
3c638d06
JK
1043 break;
1044 }
df646dfd 1045 } else if (env->stop || env->stopped) {
296af7c9
BS
1046 break;
1047 }
1048 }
c629a4bc 1049 exit_request = 0;
16400322 1050 return !all_cpu_threads_idle();
296af7c9
BS
1051}
1052
1053void set_numa_modes(void)
1054{
1055 CPUState *env;
1056 int i;
1057
1058 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1059 for (i = 0; i < nb_numa_nodes; i++) {
1060 if (node_cpumask[i] & (1 << env->cpu_index)) {
1061 env->numa_node = i;
1062 }
1063 }
1064 }
1065}
1066
1067void set_cpu_log(const char *optarg)
1068{
1069 int mask;
1070 const CPULogItem *item;
1071
1072 mask = cpu_str_to_log_mask(optarg);
1073 if (!mask) {
1074 printf("Log items (comma separated):\n");
1075 for (item = cpu_log_items; item->mask != 0; item++) {
1076 printf("%-10s %s\n", item->name, item->help);
1077 }
1078 exit(1);
1079 }
1080 cpu_set_log(mask);
1081}
29e922b6 1082
c235d738
MF
1083void set_cpu_log_filename(const char *optarg)
1084{
1085 cpu_set_log_filename(optarg);
1086}
1087
9a78eead 1088void list_cpus(FILE *f, fprintf_function cpu_fprintf, const char *optarg)
262353cb
BS
1089{
1090 /* XXX: implement xxx_cpu_list for targets that still miss it */
1091#if defined(cpu_list_id)
1092 cpu_list_id(f, cpu_fprintf, optarg);
1093#elif defined(cpu_list)
1094 cpu_list(f, cpu_fprintf); /* deprecated */
1095#endif
1096}