accel: Restrict sysemu stubs to system emulation
[qemu/rayw.git] / softmmu / cpus.c
blob1681844b61d5772b42d83fe7982790126004ce98
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "monitor/monitor.h"
28 #include "qemu/coroutine-tls.h"
29 #include "qapi/error.h"
30 #include "qapi/qapi-commands-machine.h"
31 #include "qapi/qapi-commands-misc.h"
32 #include "qapi/qapi-events-run-state.h"
33 #include "qapi/qmp/qerror.h"
34 #include "exec/gdbstub.h"
35 #include "sysemu/hw_accel.h"
36 #include "exec/exec-all.h"
37 #include "qemu/thread.h"
38 #include "qemu/plugin.h"
39 #include "sysemu/cpus.h"
40 #include "qemu/guest-random.h"
41 #include "hw/nmi.h"
42 #include "sysemu/replay.h"
43 #include "sysemu/runstate.h"
44 #include "sysemu/cpu-timers.h"
45 #include "sysemu/whpx.h"
46 #include "hw/boards.h"
47 #include "hw/hw.h"
48 #include "trace.h"
50 #ifdef CONFIG_LINUX
52 #include <sys/prctl.h>
54 #ifndef PR_MCE_KILL
55 #define PR_MCE_KILL 33
56 #endif
58 #ifndef PR_MCE_KILL_SET
59 #define PR_MCE_KILL_SET 1
60 #endif
62 #ifndef PR_MCE_KILL_EARLY
63 #define PR_MCE_KILL_EARLY 1
64 #endif
66 #endif /* CONFIG_LINUX */
68 static QemuMutex qemu_global_mutex;
70 bool cpu_is_stopped(CPUState *cpu)
72 return cpu->stopped || !runstate_is_running();
75 bool cpu_work_list_empty(CPUState *cpu)
77 return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list);
80 bool cpu_thread_is_idle(CPUState *cpu)
82 if (cpu->stop || !cpu_work_list_empty(cpu)) {
83 return false;
85 if (cpu_is_stopped(cpu)) {
86 return true;
88 if (!cpu->halted || cpu_has_work(cpu) ||
89 kvm_halt_in_kernel() || whpx_apic_in_platform()) {
90 return false;
92 return true;
95 bool all_cpu_threads_idle(void)
97 CPUState *cpu;
99 CPU_FOREACH(cpu) {
100 if (!cpu_thread_is_idle(cpu)) {
101 return false;
104 return true;
107 /***********************************************************/
108 void hw_error(const char *fmt, ...)
110 va_list ap;
111 CPUState *cpu;
113 va_start(ap, fmt);
114 fprintf(stderr, "qemu: hardware error: ");
115 vfprintf(stderr, fmt, ap);
116 fprintf(stderr, "\n");
117 CPU_FOREACH(cpu) {
118 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
119 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
121 va_end(ap);
122 abort();
126 * The chosen accelerator is supposed to register this.
128 static const AccelOpsClass *cpus_accel;
130 void cpu_synchronize_all_states(void)
132 CPUState *cpu;
134 CPU_FOREACH(cpu) {
135 cpu_synchronize_state(cpu);
139 void cpu_synchronize_all_post_reset(void)
141 CPUState *cpu;
143 CPU_FOREACH(cpu) {
144 cpu_synchronize_post_reset(cpu);
148 void cpu_synchronize_all_post_init(void)
150 CPUState *cpu;
152 CPU_FOREACH(cpu) {
153 cpu_synchronize_post_init(cpu);
157 void cpu_synchronize_all_pre_loadvm(void)
159 CPUState *cpu;
161 CPU_FOREACH(cpu) {
162 cpu_synchronize_pre_loadvm(cpu);
166 void cpu_synchronize_state(CPUState *cpu)
168 if (cpus_accel->synchronize_state) {
169 cpus_accel->synchronize_state(cpu);
173 void cpu_synchronize_post_reset(CPUState *cpu)
175 if (cpus_accel->synchronize_post_reset) {
176 cpus_accel->synchronize_post_reset(cpu);
180 void cpu_synchronize_post_init(CPUState *cpu)
182 if (cpus_accel->synchronize_post_init) {
183 cpus_accel->synchronize_post_init(cpu);
187 void cpu_synchronize_pre_loadvm(CPUState *cpu)
189 if (cpus_accel->synchronize_pre_loadvm) {
190 cpus_accel->synchronize_pre_loadvm(cpu);
194 bool cpus_are_resettable(void)
196 return cpu_check_are_resettable();
199 int64_t cpus_get_virtual_clock(void)
202 * XXX
204 * need to check that cpus_accel is not NULL, because qcow2 calls
205 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
206 * with ticks disabled in some io-tests:
207 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
209 * is this expected?
211 * XXX
213 if (cpus_accel && cpus_accel->get_virtual_clock) {
214 return cpus_accel->get_virtual_clock();
216 return cpu_get_clock();
220 * return the time elapsed in VM between vm_start and vm_stop. Unless
221 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
222 * counter.
224 int64_t cpus_get_elapsed_ticks(void)
226 if (cpus_accel->get_elapsed_ticks) {
227 return cpus_accel->get_elapsed_ticks();
229 return cpu_get_ticks();
232 static void generic_handle_interrupt(CPUState *cpu, int mask)
234 cpu->interrupt_request |= mask;
236 if (!qemu_cpu_is_self(cpu)) {
237 qemu_cpu_kick(cpu);
241 void cpu_interrupt(CPUState *cpu, int mask)
243 if (cpus_accel->handle_interrupt) {
244 cpus_accel->handle_interrupt(cpu, mask);
245 } else {
246 generic_handle_interrupt(cpu, mask);
250 static int do_vm_stop(RunState state, bool send_stop)
252 int ret = 0;
254 if (runstate_is_running()) {
255 runstate_set(state);
256 cpu_disable_ticks();
257 pause_all_vcpus();
258 vm_state_notify(0, state);
259 if (send_stop) {
260 qapi_event_send_stop();
264 bdrv_drain_all();
265 ret = bdrv_flush_all();
266 trace_vm_stop_flush_all(ret);
268 return ret;
271 /* Special vm_stop() variant for terminating the process. Historically clients
272 * did not expect a QMP STOP event and so we need to retain compatibility.
274 int vm_shutdown(void)
276 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
279 bool cpu_can_run(CPUState *cpu)
281 if (cpu->stop) {
282 return false;
284 if (cpu_is_stopped(cpu)) {
285 return false;
287 return true;
290 void cpu_handle_guest_debug(CPUState *cpu)
292 if (replay_running_debug()) {
293 if (!cpu->singlestep_enabled) {
295 * Report about the breakpoint and
296 * make a single step to skip it
298 replay_breakpoint();
299 cpu_single_step(cpu, SSTEP_ENABLE);
300 } else {
301 cpu_single_step(cpu, 0);
303 } else {
304 gdb_set_stop_cpu(cpu);
305 qemu_system_debug_request();
306 cpu->stopped = true;
310 #ifdef CONFIG_LINUX
311 static void sigbus_reraise(void)
313 sigset_t set;
314 struct sigaction action;
316 memset(&action, 0, sizeof(action));
317 action.sa_handler = SIG_DFL;
318 if (!sigaction(SIGBUS, &action, NULL)) {
319 raise(SIGBUS);
320 sigemptyset(&set);
321 sigaddset(&set, SIGBUS);
322 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
324 perror("Failed to re-raise SIGBUS!");
325 abort();
328 static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
330 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
331 sigbus_reraise();
334 if (current_cpu) {
335 /* Called asynchronously in VCPU thread. */
336 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
337 sigbus_reraise();
339 } else {
340 /* Called synchronously (via signalfd) in main thread. */
341 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
342 sigbus_reraise();
347 static void qemu_init_sigbus(void)
349 struct sigaction action;
352 * ALERT: when modifying this, take care that SIGBUS forwarding in
353 * os_mem_prealloc() will continue working as expected.
355 memset(&action, 0, sizeof(action));
356 action.sa_flags = SA_SIGINFO;
357 action.sa_sigaction = sigbus_handler;
358 sigaction(SIGBUS, &action, NULL);
360 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
362 #else /* !CONFIG_LINUX */
363 static void qemu_init_sigbus(void)
366 #endif /* !CONFIG_LINUX */
368 static QemuThread io_thread;
370 /* cpu creation */
371 static QemuCond qemu_cpu_cond;
372 /* system init */
373 static QemuCond qemu_pause_cond;
375 void qemu_init_cpu_loop(void)
377 qemu_init_sigbus();
378 qemu_cond_init(&qemu_cpu_cond);
379 qemu_cond_init(&qemu_pause_cond);
380 qemu_mutex_init(&qemu_global_mutex);
382 qemu_thread_get_self(&io_thread);
385 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
387 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
390 static void qemu_cpu_stop(CPUState *cpu, bool exit)
392 g_assert(qemu_cpu_is_self(cpu));
393 cpu->stop = false;
394 cpu->stopped = true;
395 if (exit) {
396 cpu_exit(cpu);
398 qemu_cond_broadcast(&qemu_pause_cond);
401 void qemu_wait_io_event_common(CPUState *cpu)
403 qatomic_mb_set(&cpu->thread_kicked, false);
404 if (cpu->stop) {
405 qemu_cpu_stop(cpu, false);
407 process_queued_cpu_work(cpu);
410 void qemu_wait_io_event(CPUState *cpu)
412 bool slept = false;
414 while (cpu_thread_is_idle(cpu)) {
415 if (!slept) {
416 slept = true;
417 qemu_plugin_vcpu_idle_cb(cpu);
419 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
421 if (slept) {
422 qemu_plugin_vcpu_resume_cb(cpu);
425 #ifdef _WIN32
426 /* Eat dummy APC queued by cpus_kick_thread. */
427 if (hax_enabled()) {
428 SleepEx(0, TRUE);
430 #endif
431 qemu_wait_io_event_common(cpu);
434 void cpus_kick_thread(CPUState *cpu)
436 #ifndef _WIN32
437 int err;
439 if (cpu->thread_kicked) {
440 return;
442 cpu->thread_kicked = true;
443 err = pthread_kill(cpu->thread->thread, SIG_IPI);
444 if (err && err != ESRCH) {
445 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
446 exit(1);
448 #endif
451 void qemu_cpu_kick(CPUState *cpu)
453 qemu_cond_broadcast(cpu->halt_cond);
454 if (cpus_accel->kick_vcpu_thread) {
455 cpus_accel->kick_vcpu_thread(cpu);
456 } else { /* default */
457 cpus_kick_thread(cpu);
461 void qemu_cpu_kick_self(void)
463 assert(current_cpu);
464 cpus_kick_thread(current_cpu);
467 bool qemu_cpu_is_self(CPUState *cpu)
469 return qemu_thread_is_self(cpu->thread);
472 bool qemu_in_vcpu_thread(void)
474 return current_cpu && qemu_cpu_is_self(current_cpu);
477 QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
479 bool qemu_mutex_iothread_locked(void)
481 return get_iothread_locked();
484 bool qemu_in_main_thread(void)
486 return qemu_mutex_iothread_locked();
490 * The BQL is taken from so many places that it is worth profiling the
491 * callers directly, instead of funneling them all through a single function.
493 void qemu_mutex_lock_iothread_impl(const char *file, int line)
495 QemuMutexLockFunc bql_lock = qatomic_read(&qemu_bql_mutex_lock_func);
497 g_assert(!qemu_mutex_iothread_locked());
498 bql_lock(&qemu_global_mutex, file, line);
499 set_iothread_locked(true);
502 void qemu_mutex_unlock_iothread(void)
504 g_assert(qemu_mutex_iothread_locked());
505 set_iothread_locked(false);
506 qemu_mutex_unlock(&qemu_global_mutex);
509 void qemu_cond_wait_iothread(QemuCond *cond)
511 qemu_cond_wait(cond, &qemu_global_mutex);
514 void qemu_cond_timedwait_iothread(QemuCond *cond, int ms)
516 qemu_cond_timedwait(cond, &qemu_global_mutex, ms);
519 /* signal CPU creation */
520 void cpu_thread_signal_created(CPUState *cpu)
522 cpu->created = true;
523 qemu_cond_signal(&qemu_cpu_cond);
526 /* signal CPU destruction */
527 void cpu_thread_signal_destroyed(CPUState *cpu)
529 cpu->created = false;
530 qemu_cond_signal(&qemu_cpu_cond);
534 static bool all_vcpus_paused(void)
536 CPUState *cpu;
538 CPU_FOREACH(cpu) {
539 if (!cpu->stopped) {
540 return false;
544 return true;
547 void pause_all_vcpus(void)
549 CPUState *cpu;
551 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
552 CPU_FOREACH(cpu) {
553 if (qemu_cpu_is_self(cpu)) {
554 qemu_cpu_stop(cpu, true);
555 } else {
556 cpu->stop = true;
557 qemu_cpu_kick(cpu);
561 /* We need to drop the replay_lock so any vCPU threads woken up
562 * can finish their replay tasks
564 replay_mutex_unlock();
566 while (!all_vcpus_paused()) {
567 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
568 CPU_FOREACH(cpu) {
569 qemu_cpu_kick(cpu);
573 qemu_mutex_unlock_iothread();
574 replay_mutex_lock();
575 qemu_mutex_lock_iothread();
578 void cpu_resume(CPUState *cpu)
580 cpu->stop = false;
581 cpu->stopped = false;
582 qemu_cpu_kick(cpu);
585 void resume_all_vcpus(void)
587 CPUState *cpu;
589 if (!runstate_is_running()) {
590 return;
593 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
594 CPU_FOREACH(cpu) {
595 cpu_resume(cpu);
599 void cpu_remove_sync(CPUState *cpu)
601 cpu->stop = true;
602 cpu->unplug = true;
603 qemu_cpu_kick(cpu);
604 qemu_mutex_unlock_iothread();
605 qemu_thread_join(cpu->thread);
606 qemu_mutex_lock_iothread();
609 void cpus_register_accel(const AccelOpsClass *ops)
611 assert(ops != NULL);
612 assert(ops->create_vcpu_thread != NULL); /* mandatory */
613 cpus_accel = ops;
616 void qemu_init_vcpu(CPUState *cpu)
618 MachineState *ms = MACHINE(qdev_get_machine());
620 cpu->nr_cores = ms->smp.cores;
621 cpu->nr_threads = ms->smp.threads;
622 cpu->stopped = true;
623 cpu->random_seed = qemu_guest_random_seed_thread_part1();
625 if (!cpu->as) {
626 /* If the target cpu hasn't set up any address spaces itself,
627 * give it the default one.
629 cpu->num_ases = 1;
630 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
633 /* accelerators all implement the AccelOpsClass */
634 g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL);
635 cpus_accel->create_vcpu_thread(cpu);
637 while (!cpu->created) {
638 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
642 void cpu_stop_current(void)
644 if (current_cpu) {
645 current_cpu->stop = true;
646 cpu_exit(current_cpu);
650 int vm_stop(RunState state)
652 if (qemu_in_vcpu_thread()) {
653 qemu_system_vmstop_request_prepare();
654 qemu_system_vmstop_request(state);
656 * FIXME: should not return to device code in case
657 * vm_stop() has been requested.
659 cpu_stop_current();
660 return 0;
663 return do_vm_stop(state, true);
667 * Prepare for (re)starting the VM.
668 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
669 * running or in case of an error condition), 0 otherwise.
671 int vm_prepare_start(void)
673 RunState requested;
675 qemu_vmstop_requested(&requested);
676 if (runstate_is_running() && requested == RUN_STATE__MAX) {
677 return -1;
680 /* Ensure that a STOP/RESUME pair of events is emitted if a
681 * vmstop request was pending. The BLOCK_IO_ERROR event, for
682 * example, according to documentation is always followed by
683 * the STOP event.
685 if (runstate_is_running()) {
686 qapi_event_send_stop();
687 qapi_event_send_resume();
688 return -1;
691 /* We are sending this now, but the CPUs will be resumed shortly later */
692 qapi_event_send_resume();
694 cpu_enable_ticks();
695 runstate_set(RUN_STATE_RUNNING);
696 vm_state_notify(1, RUN_STATE_RUNNING);
697 return 0;
700 void vm_start(void)
702 if (!vm_prepare_start()) {
703 resume_all_vcpus();
707 /* does a state transition even if the VM is already stopped,
708 current state is forgotten forever */
709 int vm_stop_force_state(RunState state)
711 if (runstate_is_running()) {
712 return vm_stop(state);
713 } else {
714 int ret;
715 runstate_set(state);
717 bdrv_drain_all();
718 /* Make sure to return an error if the flush in a previous vm_stop()
719 * failed. */
720 ret = bdrv_flush_all();
721 trace_vm_stop_flush_all(ret);
722 return ret;
726 void list_cpus(const char *optarg)
728 /* XXX: implement xxx_cpu_list for targets that still miss it */
729 #if defined(cpu_list)
730 cpu_list();
731 #endif
734 void qmp_memsave(int64_t addr, int64_t size, const char *filename,
735 bool has_cpu, int64_t cpu_index, Error **errp)
737 FILE *f;
738 uint32_t l;
739 CPUState *cpu;
740 uint8_t buf[1024];
741 int64_t orig_addr = addr, orig_size = size;
743 if (!has_cpu) {
744 cpu_index = 0;
747 cpu = qemu_get_cpu(cpu_index);
748 if (cpu == NULL) {
749 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
750 "a CPU number");
751 return;
754 f = fopen(filename, "wb");
755 if (!f) {
756 error_setg_file_open(errp, errno, filename);
757 return;
760 while (size != 0) {
761 l = sizeof(buf);
762 if (l > size)
763 l = size;
764 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
765 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
766 " specified", orig_addr, orig_size);
767 goto exit;
769 if (fwrite(buf, 1, l, f) != l) {
770 error_setg(errp, QERR_IO_ERROR);
771 goto exit;
773 addr += l;
774 size -= l;
777 exit:
778 fclose(f);
781 void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
782 Error **errp)
784 FILE *f;
785 uint32_t l;
786 uint8_t buf[1024];
788 f = fopen(filename, "wb");
789 if (!f) {
790 error_setg_file_open(errp, errno, filename);
791 return;
794 while (size != 0) {
795 l = sizeof(buf);
796 if (l > size)
797 l = size;
798 cpu_physical_memory_read(addr, buf, l);
799 if (fwrite(buf, 1, l, f) != l) {
800 error_setg(errp, QERR_IO_ERROR);
801 goto exit;
803 addr += l;
804 size -= l;
807 exit:
808 fclose(f);
811 void qmp_inject_nmi(Error **errp)
813 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp);