MAINTAINERS: Add the CAN documentation file to the CAN section
[qemu/kevin.git] / system / cpus.c
bloba444a747f0164ee9a998ef4b25b49a2efc4607a8
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 "monitor/monitor.h"
27 #include "qemu/coroutine-tls.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-machine.h"
30 #include "qapi/qapi-commands-misc.h"
31 #include "qapi/qapi-events-run-state.h"
32 #include "qapi/qmp/qerror.h"
33 #include "exec/gdbstub.h"
34 #include "sysemu/hw_accel.h"
35 #include "exec/cpu-common.h"
36 #include "qemu/thread.h"
37 #include "qemu/main-loop.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;
71 * The chosen accelerator is supposed to register this.
73 static const AccelOpsClass *cpus_accel;
75 bool cpu_is_stopped(CPUState *cpu)
77 return cpu->stopped || !runstate_is_running();
80 bool cpu_work_list_empty(CPUState *cpu)
82 return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list);
85 bool cpu_thread_is_idle(CPUState *cpu)
87 if (cpu->stop || !cpu_work_list_empty(cpu)) {
88 return false;
90 if (cpu_is_stopped(cpu)) {
91 return true;
93 if (!cpu->halted || cpu_has_work(cpu)) {
94 return false;
96 if (cpus_accel->cpu_thread_is_idle) {
97 return cpus_accel->cpu_thread_is_idle(cpu);
99 return true;
102 bool all_cpu_threads_idle(void)
104 CPUState *cpu;
106 CPU_FOREACH(cpu) {
107 if (!cpu_thread_is_idle(cpu)) {
108 return false;
111 return true;
114 /***********************************************************/
115 void hw_error(const char *fmt, ...)
117 va_list ap;
118 CPUState *cpu;
120 va_start(ap, fmt);
121 fprintf(stderr, "qemu: hardware error: ");
122 vfprintf(stderr, fmt, ap);
123 fprintf(stderr, "\n");
124 CPU_FOREACH(cpu) {
125 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index);
126 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU);
128 va_end(ap);
129 abort();
132 void cpu_synchronize_all_states(void)
134 CPUState *cpu;
136 CPU_FOREACH(cpu) {
137 cpu_synchronize_state(cpu);
141 void cpu_synchronize_all_post_reset(void)
143 CPUState *cpu;
145 CPU_FOREACH(cpu) {
146 cpu_synchronize_post_reset(cpu);
150 void cpu_synchronize_all_post_init(void)
152 CPUState *cpu;
154 CPU_FOREACH(cpu) {
155 cpu_synchronize_post_init(cpu);
159 void cpu_synchronize_all_pre_loadvm(void)
161 CPUState *cpu;
163 CPU_FOREACH(cpu) {
164 cpu_synchronize_pre_loadvm(cpu);
168 void cpu_synchronize_state(CPUState *cpu)
170 if (cpus_accel->synchronize_state) {
171 cpus_accel->synchronize_state(cpu);
175 void cpu_synchronize_post_reset(CPUState *cpu)
177 if (cpus_accel->synchronize_post_reset) {
178 cpus_accel->synchronize_post_reset(cpu);
182 void cpu_synchronize_post_init(CPUState *cpu)
184 if (cpus_accel->synchronize_post_init) {
185 cpus_accel->synchronize_post_init(cpu);
189 void cpu_synchronize_pre_loadvm(CPUState *cpu)
191 if (cpus_accel->synchronize_pre_loadvm) {
192 cpus_accel->synchronize_pre_loadvm(cpu);
196 bool cpus_are_resettable(void)
198 if (cpus_accel->cpus_are_resettable) {
199 return cpus_accel->cpus_are_resettable();
201 return true;
204 void cpu_exec_reset_hold(CPUState *cpu)
206 if (cpus_accel->cpu_reset_hold) {
207 cpus_accel->cpu_reset_hold(cpu);
211 int64_t cpus_get_virtual_clock(void)
214 * XXX
216 * need to check that cpus_accel is not NULL, because qcow2 calls
217 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and
218 * with ticks disabled in some io-tests:
219 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267
221 * is this expected?
223 * XXX
225 if (cpus_accel && cpus_accel->get_virtual_clock) {
226 return cpus_accel->get_virtual_clock();
228 return cpu_get_clock();
232 * return the time elapsed in VM between vm_start and vm_stop. Unless
233 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle
234 * counter.
236 int64_t cpus_get_elapsed_ticks(void)
238 if (cpus_accel->get_elapsed_ticks) {
239 return cpus_accel->get_elapsed_ticks();
241 return cpu_get_ticks();
244 static void generic_handle_interrupt(CPUState *cpu, int mask)
246 cpu->interrupt_request |= mask;
248 if (!qemu_cpu_is_self(cpu)) {
249 qemu_cpu_kick(cpu);
253 void cpu_interrupt(CPUState *cpu, int mask)
255 if (cpus_accel->handle_interrupt) {
256 cpus_accel->handle_interrupt(cpu, mask);
257 } else {
258 generic_handle_interrupt(cpu, mask);
262 static int do_vm_stop(RunState state, bool send_stop)
264 int ret = 0;
266 if (runstate_is_running()) {
267 runstate_set(state);
268 cpu_disable_ticks();
269 pause_all_vcpus();
270 vm_state_notify(0, state);
271 if (send_stop) {
272 qapi_event_send_stop();
276 bdrv_drain_all();
277 ret = bdrv_flush_all();
278 trace_vm_stop_flush_all(ret);
280 return ret;
283 /* Special vm_stop() variant for terminating the process. Historically clients
284 * did not expect a QMP STOP event and so we need to retain compatibility.
286 int vm_shutdown(void)
288 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
291 bool cpu_can_run(CPUState *cpu)
293 if (cpu->stop) {
294 return false;
296 if (cpu_is_stopped(cpu)) {
297 return false;
299 return true;
302 void cpu_handle_guest_debug(CPUState *cpu)
304 if (replay_running_debug()) {
305 if (!cpu->singlestep_enabled) {
307 * Report about the breakpoint and
308 * make a single step to skip it
310 replay_breakpoint();
311 cpu_single_step(cpu, SSTEP_ENABLE);
312 } else {
313 cpu_single_step(cpu, 0);
315 } else {
316 gdb_set_stop_cpu(cpu);
317 qemu_system_debug_request();
318 cpu->stopped = true;
322 #ifdef CONFIG_LINUX
323 static void sigbus_reraise(void)
325 sigset_t set;
326 struct sigaction action;
328 memset(&action, 0, sizeof(action));
329 action.sa_handler = SIG_DFL;
330 if (!sigaction(SIGBUS, &action, NULL)) {
331 raise(SIGBUS);
332 sigemptyset(&set);
333 sigaddset(&set, SIGBUS);
334 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
336 perror("Failed to re-raise SIGBUS!");
337 abort();
340 static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
342 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
343 sigbus_reraise();
346 if (current_cpu) {
347 /* Called asynchronously in VCPU thread. */
348 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
349 sigbus_reraise();
351 } else {
352 /* Called synchronously (via signalfd) in main thread. */
353 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
354 sigbus_reraise();
359 static void qemu_init_sigbus(void)
361 struct sigaction action;
364 * ALERT: when modifying this, take care that SIGBUS forwarding in
365 * qemu_prealloc_mem() will continue working as expected.
367 memset(&action, 0, sizeof(action));
368 action.sa_flags = SA_SIGINFO;
369 action.sa_sigaction = sigbus_handler;
370 sigaction(SIGBUS, &action, NULL);
372 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
374 #else /* !CONFIG_LINUX */
375 static void qemu_init_sigbus(void)
378 #endif /* !CONFIG_LINUX */
380 static QemuThread io_thread;
382 /* cpu creation */
383 static QemuCond qemu_cpu_cond;
384 /* system init */
385 static QemuCond qemu_pause_cond;
387 void qemu_init_cpu_loop(void)
389 qemu_init_sigbus();
390 qemu_cond_init(&qemu_cpu_cond);
391 qemu_cond_init(&qemu_pause_cond);
392 qemu_mutex_init(&qemu_global_mutex);
394 qemu_thread_get_self(&io_thread);
397 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
399 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
402 static void qemu_cpu_stop(CPUState *cpu, bool exit)
404 g_assert(qemu_cpu_is_self(cpu));
405 cpu->stop = false;
406 cpu->stopped = true;
407 if (exit) {
408 cpu_exit(cpu);
410 qemu_cond_broadcast(&qemu_pause_cond);
413 void qemu_wait_io_event_common(CPUState *cpu)
415 qatomic_set_mb(&cpu->thread_kicked, false);
416 if (cpu->stop) {
417 qemu_cpu_stop(cpu, false);
419 process_queued_cpu_work(cpu);
422 void qemu_wait_io_event(CPUState *cpu)
424 bool slept = false;
426 while (cpu_thread_is_idle(cpu)) {
427 if (!slept) {
428 slept = true;
429 qemu_plugin_vcpu_idle_cb(cpu);
431 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
433 if (slept) {
434 qemu_plugin_vcpu_resume_cb(cpu);
437 qemu_wait_io_event_common(cpu);
440 void cpus_kick_thread(CPUState *cpu)
442 if (cpu->thread_kicked) {
443 return;
445 cpu->thread_kicked = true;
447 #ifndef _WIN32
448 int err = pthread_kill(cpu->thread->thread, SIG_IPI);
449 if (err && err != ESRCH) {
450 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err));
451 exit(1);
453 #else
454 qemu_sem_post(&cpu->sem);
455 #endif
458 void qemu_cpu_kick(CPUState *cpu)
460 qemu_cond_broadcast(cpu->halt_cond);
461 if (cpus_accel->kick_vcpu_thread) {
462 cpus_accel->kick_vcpu_thread(cpu);
463 } else { /* default */
464 cpus_kick_thread(cpu);
468 void qemu_cpu_kick_self(void)
470 assert(current_cpu);
471 cpus_kick_thread(current_cpu);
474 bool qemu_cpu_is_self(CPUState *cpu)
476 return qemu_thread_is_self(cpu->thread);
479 bool qemu_in_vcpu_thread(void)
481 return current_cpu && qemu_cpu_is_self(current_cpu);
484 QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
486 bool qemu_mutex_iothread_locked(void)
488 return get_iothread_locked();
491 bool qemu_in_main_thread(void)
493 return qemu_mutex_iothread_locked();
497 * The BQL is taken from so many places that it is worth profiling the
498 * callers directly, instead of funneling them all through a single function.
500 void qemu_mutex_lock_iothread_impl(const char *file, int line)
502 QemuMutexLockFunc bql_lock = qatomic_read(&qemu_bql_mutex_lock_func);
504 g_assert(!qemu_mutex_iothread_locked());
505 bql_lock(&qemu_global_mutex, file, line);
506 set_iothread_locked(true);
509 void qemu_mutex_unlock_iothread(void)
511 g_assert(qemu_mutex_iothread_locked());
512 set_iothread_locked(false);
513 qemu_mutex_unlock(&qemu_global_mutex);
516 void qemu_cond_wait_iothread(QemuCond *cond)
518 qemu_cond_wait(cond, &qemu_global_mutex);
521 void qemu_cond_timedwait_iothread(QemuCond *cond, int ms)
523 qemu_cond_timedwait(cond, &qemu_global_mutex, ms);
526 /* signal CPU creation */
527 void cpu_thread_signal_created(CPUState *cpu)
529 cpu->created = true;
530 qemu_cond_signal(&qemu_cpu_cond);
533 /* signal CPU destruction */
534 void cpu_thread_signal_destroyed(CPUState *cpu)
536 cpu->created = false;
537 qemu_cond_signal(&qemu_cpu_cond);
541 static bool all_vcpus_paused(void)
543 CPUState *cpu;
545 CPU_FOREACH(cpu) {
546 if (!cpu->stopped) {
547 return false;
551 return true;
554 void pause_all_vcpus(void)
556 CPUState *cpu;
558 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
559 CPU_FOREACH(cpu) {
560 if (qemu_cpu_is_self(cpu)) {
561 qemu_cpu_stop(cpu, true);
562 } else {
563 cpu->stop = true;
564 qemu_cpu_kick(cpu);
568 /* We need to drop the replay_lock so any vCPU threads woken up
569 * can finish their replay tasks
571 replay_mutex_unlock();
573 while (!all_vcpus_paused()) {
574 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
575 CPU_FOREACH(cpu) {
576 qemu_cpu_kick(cpu);
580 qemu_mutex_unlock_iothread();
581 replay_mutex_lock();
582 qemu_mutex_lock_iothread();
585 void cpu_resume(CPUState *cpu)
587 cpu->stop = false;
588 cpu->stopped = false;
589 qemu_cpu_kick(cpu);
592 void resume_all_vcpus(void)
594 CPUState *cpu;
596 if (!runstate_is_running()) {
597 return;
600 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
601 CPU_FOREACH(cpu) {
602 cpu_resume(cpu);
606 void cpu_remove_sync(CPUState *cpu)
608 cpu->stop = true;
609 cpu->unplug = true;
610 qemu_cpu_kick(cpu);
611 qemu_mutex_unlock_iothread();
612 qemu_thread_join(cpu->thread);
613 qemu_mutex_lock_iothread();
616 void cpus_register_accel(const AccelOpsClass *ops)
618 assert(ops != NULL);
619 assert(ops->create_vcpu_thread != NULL); /* mandatory */
620 cpus_accel = ops;
623 const AccelOpsClass *cpus_get_accel(void)
625 /* broken if we call this early */
626 assert(cpus_accel);
627 return cpus_accel;
630 void qemu_init_vcpu(CPUState *cpu)
632 MachineState *ms = MACHINE(qdev_get_machine());
634 cpu->nr_cores = machine_topo_get_cores_per_socket(ms);
635 cpu->nr_threads = ms->smp.threads;
636 cpu->stopped = true;
637 cpu->random_seed = qemu_guest_random_seed_thread_part1();
639 if (!cpu->as) {
640 /* If the target cpu hasn't set up any address spaces itself,
641 * give it the default one.
643 cpu->num_ases = 1;
644 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
647 /* accelerators all implement the AccelOpsClass */
648 g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL);
649 cpus_accel->create_vcpu_thread(cpu);
651 while (!cpu->created) {
652 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
656 void cpu_stop_current(void)
658 if (current_cpu) {
659 current_cpu->stop = true;
660 cpu_exit(current_cpu);
664 int vm_stop(RunState state)
666 if (qemu_in_vcpu_thread()) {
667 qemu_system_vmstop_request_prepare();
668 qemu_system_vmstop_request(state);
670 * FIXME: should not return to device code in case
671 * vm_stop() has been requested.
673 cpu_stop_current();
674 return 0;
677 return do_vm_stop(state, true);
681 * Prepare for (re)starting the VM.
682 * Returns -1 if the vCPUs are not to be restarted (e.g. if they are already
683 * running or in case of an error condition), 0 otherwise.
685 int vm_prepare_start(bool step_pending)
687 RunState requested;
689 qemu_vmstop_requested(&requested);
690 if (runstate_is_running() && requested == RUN_STATE__MAX) {
691 return -1;
694 /* Ensure that a STOP/RESUME pair of events is emitted if a
695 * vmstop request was pending. The BLOCK_IO_ERROR event, for
696 * example, according to documentation is always followed by
697 * the STOP event.
699 if (runstate_is_running()) {
700 qapi_event_send_stop();
701 qapi_event_send_resume();
702 return -1;
706 * WHPX accelerator needs to know whether we are going to step
707 * any CPUs, before starting the first one.
709 if (cpus_accel->synchronize_pre_resume) {
710 cpus_accel->synchronize_pre_resume(step_pending);
713 /* We are sending this now, but the CPUs will be resumed shortly later */
714 qapi_event_send_resume();
716 cpu_enable_ticks();
717 runstate_set(RUN_STATE_RUNNING);
718 vm_state_notify(1, RUN_STATE_RUNNING);
719 return 0;
722 void vm_start(void)
724 if (!vm_prepare_start(false)) {
725 resume_all_vcpus();
729 /* does a state transition even if the VM is already stopped,
730 current state is forgotten forever */
731 int vm_stop_force_state(RunState state)
733 if (runstate_is_running()) {
734 return vm_stop(state);
735 } else {
736 int ret;
737 runstate_set(state);
739 bdrv_drain_all();
740 /* Make sure to return an error if the flush in a previous vm_stop()
741 * failed. */
742 ret = bdrv_flush_all();
743 trace_vm_stop_flush_all(ret);
744 return ret;
748 void qmp_memsave(int64_t addr, int64_t size, const char *filename,
749 bool has_cpu, int64_t cpu_index, Error **errp)
751 FILE *f;
752 uint32_t l;
753 CPUState *cpu;
754 uint8_t buf[1024];
755 int64_t orig_addr = addr, orig_size = size;
757 if (!has_cpu) {
758 cpu_index = 0;
761 cpu = qemu_get_cpu(cpu_index);
762 if (cpu == NULL) {
763 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
764 "a CPU number");
765 return;
768 f = fopen(filename, "wb");
769 if (!f) {
770 error_setg_file_open(errp, errno, filename);
771 return;
774 while (size != 0) {
775 l = sizeof(buf);
776 if (l > size)
777 l = size;
778 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
779 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
780 " specified", orig_addr, orig_size);
781 goto exit;
783 if (fwrite(buf, 1, l, f) != l) {
784 error_setg(errp, QERR_IO_ERROR);
785 goto exit;
787 addr += l;
788 size -= l;
791 exit:
792 fclose(f);
795 void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
796 Error **errp)
798 FILE *f;
799 uint32_t l;
800 uint8_t buf[1024];
802 f = fopen(filename, "wb");
803 if (!f) {
804 error_setg_file_open(errp, errno, filename);
805 return;
808 while (size != 0) {
809 l = sizeof(buf);
810 if (l > size)
811 l = size;
812 cpu_physical_memory_read(addr, buf, l);
813 if (fwrite(buf, 1, l, f) != l) {
814 error_setg(errp, QERR_IO_ERROR);
815 goto exit;
817 addr += l;
818 size -= l;
821 exit:
822 fclose(f);
825 void qmp_inject_nmi(Error **errp)
827 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp);