1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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/cpu-common.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"
49
50#ifdef CONFIG_LINUX
51
52#include <sys/prctl.h>
53
54#ifndef PR_MCE_KILL
55#define PR_MCE_KILL 33
56#endif
57
58#ifndef PR_MCE_KILL_SET
59#define PR_MCE_KILL_SET 1
60#endif
61
62#ifndef PR_MCE_KILL_EARLY
63#define PR_MCE_KILL_EARLY 1
64#endif
65
66#endif
67
68static QemuMutex qemu_global_mutex;
69
70
71
72
73static const AccelOpsClass *cpus_accel;
74
75bool cpu_is_stopped(CPUState *cpu)
76{
77 return cpu->stopped || !runstate_is_running();
78}
79
80bool cpu_work_list_empty(CPUState *cpu)
81{
82 return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list);
83}
84
85bool cpu_thread_is_idle(CPUState *cpu)
86{
87 if (cpu->stop || !cpu_work_list_empty(cpu)) {
88 return false;
89 }
90 if (cpu_is_stopped(cpu)) {
91 return true;
92 }
93 if (!cpu->halted || cpu_has_work(cpu)) {
94 return false;
95 }
96 if (cpus_accel->cpu_thread_is_idle) {
97 return cpus_accel->cpu_thread_is_idle(cpu);
98 }
99 return true;
100}
101
102bool all_cpu_threads_idle(void)
103{
104 CPUState *cpu;
105
106 CPU_FOREACH(cpu) {
107 if (!cpu_thread_is_idle(cpu)) {
108 return false;
109 }
110 }
111 return true;
112}
113
114
115void hw_error(const char *fmt, ...)
116{
117 va_list ap;
118 CPUState *cpu;
119
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);
127 }
128 va_end(ap);
129 abort();
130}
131
132void cpu_synchronize_all_states(void)
133{
134 CPUState *cpu;
135
136 CPU_FOREACH(cpu) {
137 cpu_synchronize_state(cpu);
138 }
139}
140
141void cpu_synchronize_all_post_reset(void)
142{
143 CPUState *cpu;
144
145 CPU_FOREACH(cpu) {
146 cpu_synchronize_post_reset(cpu);
147 }
148}
149
150void cpu_synchronize_all_post_init(void)
151{
152 CPUState *cpu;
153
154 CPU_FOREACH(cpu) {
155 cpu_synchronize_post_init(cpu);
156 }
157}
158
159void cpu_synchronize_all_pre_loadvm(void)
160{
161 CPUState *cpu;
162
163 CPU_FOREACH(cpu) {
164 cpu_synchronize_pre_loadvm(cpu);
165 }
166}
167
168void cpu_synchronize_state(CPUState *cpu)
169{
170 if (cpus_accel->synchronize_state) {
171 cpus_accel->synchronize_state(cpu);
172 }
173}
174
175void cpu_synchronize_post_reset(CPUState *cpu)
176{
177 if (cpus_accel->synchronize_post_reset) {
178 cpus_accel->synchronize_post_reset(cpu);
179 }
180}
181
182void cpu_synchronize_post_init(CPUState *cpu)
183{
184 if (cpus_accel->synchronize_post_init) {
185 cpus_accel->synchronize_post_init(cpu);
186 }
187}
188
189void cpu_synchronize_pre_loadvm(CPUState *cpu)
190{
191 if (cpus_accel->synchronize_pre_loadvm) {
192 cpus_accel->synchronize_pre_loadvm(cpu);
193 }
194}
195
196bool cpus_are_resettable(void)
197{
198 if (cpus_accel->cpus_are_resettable) {
199 return cpus_accel->cpus_are_resettable();
200 }
201 return true;
202}
203
204int64_t cpus_get_virtual_clock(void)
205{
206
207
208
209
210
211
212
213
214
215
216
217
218 if (cpus_accel && cpus_accel->get_virtual_clock) {
219 return cpus_accel->get_virtual_clock();
220 }
221 return cpu_get_clock();
222}
223
224
225
226
227
228
229int64_t cpus_get_elapsed_ticks(void)
230{
231 if (cpus_accel->get_elapsed_ticks) {
232 return cpus_accel->get_elapsed_ticks();
233 }
234 return cpu_get_ticks();
235}
236
237static void generic_handle_interrupt(CPUState *cpu, int mask)
238{
239 cpu->interrupt_request |= mask;
240
241 if (!qemu_cpu_is_self(cpu)) {
242 qemu_cpu_kick(cpu);
243 }
244}
245
246void cpu_interrupt(CPUState *cpu, int mask)
247{
248 if (cpus_accel->handle_interrupt) {
249 cpus_accel->handle_interrupt(cpu, mask);
250 } else {
251 generic_handle_interrupt(cpu, mask);
252 }
253}
254
255static int do_vm_stop(RunState state, bool send_stop)
256{
257 int ret = 0;
258
259 if (runstate_is_running()) {
260 runstate_set(state);
261 cpu_disable_ticks();
262 pause_all_vcpus();
263 vm_state_notify(0, state);
264 if (send_stop) {
265 qapi_event_send_stop();
266 }
267 }
268
269 bdrv_drain_all();
270 ret = bdrv_flush_all();
271 trace_vm_stop_flush_all(ret);
272
273 return ret;
274}
275
276
277
278
279int vm_shutdown(void)
280{
281 return do_vm_stop(RUN_STATE_SHUTDOWN, false);
282}
283
284bool cpu_can_run(CPUState *cpu)
285{
286 if (cpu->stop) {
287 return false;
288 }
289 if (cpu_is_stopped(cpu)) {
290 return false;
291 }
292 return true;
293}
294
295void cpu_handle_guest_debug(CPUState *cpu)
296{
297 if (replay_running_debug()) {
298 if (!cpu->singlestep_enabled) {
299
300
301
302
303 replay_breakpoint();
304 cpu_single_step(cpu, SSTEP_ENABLE);
305 } else {
306 cpu_single_step(cpu, 0);
307 }
308 } else {
309 gdb_set_stop_cpu(cpu);
310 qemu_system_debug_request();
311 cpu->stopped = true;
312 }
313}
314
315#ifdef CONFIG_LINUX
316static void sigbus_reraise(void)
317{
318 sigset_t set;
319 struct sigaction action;
320
321 memset(&action, 0, sizeof(action));
322 action.sa_handler = SIG_DFL;
323 if (!sigaction(SIGBUS, &action, NULL)) {
324 raise(SIGBUS);
325 sigemptyset(&set);
326 sigaddset(&set, SIGBUS);
327 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
328 }
329 perror("Failed to re-raise SIGBUS!");
330 abort();
331}
332
333static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx)
334{
335 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) {
336 sigbus_reraise();
337 }
338
339 if (current_cpu) {
340
341 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) {
342 sigbus_reraise();
343 }
344 } else {
345
346 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) {
347 sigbus_reraise();
348 }
349 }
350}
351
352static void qemu_init_sigbus(void)
353{
354 struct sigaction action;
355
356
357
358
359
360 memset(&action, 0, sizeof(action));
361 action.sa_flags = SA_SIGINFO;
362 action.sa_sigaction = sigbus_handler;
363 sigaction(SIGBUS, &action, NULL);
364
365 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0);
366}
367#else
368static void qemu_init_sigbus(void)
369{
370}
371#endif
372
373static QemuThread io_thread;
374
375
376static QemuCond qemu_cpu_cond;
377
378static QemuCond qemu_pause_cond;
379
380void qemu_init_cpu_loop(void)
381{
382 qemu_init_sigbus();
383 qemu_cond_init(&qemu_cpu_cond);
384 qemu_cond_init(&qemu_pause_cond);
385 qemu_mutex_init(&qemu_global_mutex);
386
387 qemu_thread_get_self(&io_thread);
388}
389
390void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data)
391{
392 do_run_on_cpu(cpu, func, data, &qemu_global_mutex);
393}
394
395static void qemu_cpu_stop(CPUState *cpu, bool exit)
396{
397 g_assert(qemu_cpu_is_self(cpu));
398 cpu->stop = false;
399 cpu->stopped = true;
400 if (exit) {
401 cpu_exit(cpu);
402 }
403 qemu_cond_broadcast(&qemu_pause_cond);
404}
405
406void qemu_wait_io_event_common(CPUState *cpu)
407{
408 qatomic_mb_set(&cpu->thread_kicked, false);
409 if (cpu->stop) {
410 qemu_cpu_stop(cpu, false);
411 }
412 process_queued_cpu_work(cpu);
413}
414
415void qemu_wait_io_event(CPUState *cpu)
416{
417 bool slept = false;
418
419 while (cpu_thread_is_idle(cpu)) {
420 if (!slept) {
421 slept = true;
422 qemu_plugin_vcpu_idle_cb(cpu);
423 }
424 qemu_cond_wait(cpu->halt_cond, &qemu_global_mutex);
425 }
426 if (slept) {
427 qemu_plugin_vcpu_resume_cb(cpu);
428 }
429
430#ifdef _WIN32
431
432 if (hax_enabled()) {
433 SleepEx(0, TRUE);
434 }
435#endif
436 qemu_wait_io_event_common(cpu);
437}
438
439void cpus_kick_thread(CPUState *cpu)
440{
441#ifndef _WIN32
442 int err;
443
444 if (cpu->thread_kicked) {
445 return;
446 }
447 cpu->thread_kicked = true;
448 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);
452 }
453#endif
454}
455
456void qemu_cpu_kick(CPUState *cpu)
457{
458 qemu_cond_broadcast(cpu->halt_cond);
459 if (cpus_accel->kick_vcpu_thread) {
460 cpus_accel->kick_vcpu_thread(cpu);
461 } else {
462 cpus_kick_thread(cpu);
463 }
464}
465
466void qemu_cpu_kick_self(void)
467{
468 assert(current_cpu);
469 cpus_kick_thread(current_cpu);
470}
471
472bool qemu_cpu_is_self(CPUState *cpu)
473{
474 return qemu_thread_is_self(cpu->thread);
475}
476
477bool qemu_in_vcpu_thread(void)
478{
479 return current_cpu && qemu_cpu_is_self(current_cpu);
480}
481
482QEMU_DEFINE_STATIC_CO_TLS(bool, iothread_locked)
483
484bool qemu_mutex_iothread_locked(void)
485{
486 return get_iothread_locked();
487}
488
489bool qemu_in_main_thread(void)
490{
491 return qemu_mutex_iothread_locked();
492}
493
494
495
496
497
498void qemu_mutex_lock_iothread_impl(const char *file, int line)
499{
500 QemuMutexLockFunc bql_lock = qatomic_read(&qemu_bql_mutex_lock_func);
501
502 g_assert(!qemu_mutex_iothread_locked());
503 bql_lock(&qemu_global_mutex, file, line);
504 set_iothread_locked(true);
505}
506
507void qemu_mutex_unlock_iothread(void)
508{
509 g_assert(qemu_mutex_iothread_locked());
510 set_iothread_locked(false);
511 qemu_mutex_unlock(&qemu_global_mutex);
512}
513
514void qemu_cond_wait_iothread(QemuCond *cond)
515{
516 qemu_cond_wait(cond, &qemu_global_mutex);
517}
518
519void qemu_cond_timedwait_iothread(QemuCond *cond, int ms)
520{
521 qemu_cond_timedwait(cond, &qemu_global_mutex, ms);
522}
523
524
525void cpu_thread_signal_created(CPUState *cpu)
526{
527 cpu->created = true;
528 qemu_cond_signal(&qemu_cpu_cond);
529}
530
531
532void cpu_thread_signal_destroyed(CPUState *cpu)
533{
534 cpu->created = false;
535 qemu_cond_signal(&qemu_cpu_cond);
536}
537
538
539static bool all_vcpus_paused(void)
540{
541 CPUState *cpu;
542
543 CPU_FOREACH(cpu) {
544 if (!cpu->stopped) {
545 return false;
546 }
547 }
548
549 return true;
550}
551
552void pause_all_vcpus(void)
553{
554 CPUState *cpu;
555
556 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false);
557 CPU_FOREACH(cpu) {
558 if (qemu_cpu_is_self(cpu)) {
559 qemu_cpu_stop(cpu, true);
560 } else {
561 cpu->stop = true;
562 qemu_cpu_kick(cpu);
563 }
564 }
565
566
567
568
569 replay_mutex_unlock();
570
571 while (!all_vcpus_paused()) {
572 qemu_cond_wait(&qemu_pause_cond, &qemu_global_mutex);
573 CPU_FOREACH(cpu) {
574 qemu_cpu_kick(cpu);
575 }
576 }
577
578 qemu_mutex_unlock_iothread();
579 replay_mutex_lock();
580 qemu_mutex_lock_iothread();
581}
582
583void cpu_resume(CPUState *cpu)
584{
585 cpu->stop = false;
586 cpu->stopped = false;
587 qemu_cpu_kick(cpu);
588}
589
590void resume_all_vcpus(void)
591{
592 CPUState *cpu;
593
594 if (!runstate_is_running()) {
595 return;
596 }
597
598 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
599 CPU_FOREACH(cpu) {
600 cpu_resume(cpu);
601 }
602}
603
604void cpu_remove_sync(CPUState *cpu)
605{
606 cpu->stop = true;
607 cpu->unplug = true;
608 qemu_cpu_kick(cpu);
609 qemu_mutex_unlock_iothread();
610 qemu_thread_join(cpu->thread);
611 qemu_mutex_lock_iothread();
612}
613
614void cpus_register_accel(const AccelOpsClass *ops)
615{
616 assert(ops != NULL);
617 assert(ops->create_vcpu_thread != NULL);
618 cpus_accel = ops;
619}
620
621void qemu_init_vcpu(CPUState *cpu)
622{
623 MachineState *ms = MACHINE(qdev_get_machine());
624
625 cpu->nr_cores = ms->smp.cores;
626 cpu->nr_threads = ms->smp.threads;
627 cpu->stopped = true;
628 cpu->random_seed = qemu_guest_random_seed_thread_part1();
629
630 if (!cpu->as) {
631
632
633
634 cpu->num_ases = 1;
635 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory);
636 }
637
638
639 g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL);
640 cpus_accel->create_vcpu_thread(cpu);
641
642 while (!cpu->created) {
643 qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
644 }
645}
646
647void cpu_stop_current(void)
648{
649 if (current_cpu) {
650 current_cpu->stop = true;
651 cpu_exit(current_cpu);
652 }
653}
654
655int vm_stop(RunState state)
656{
657 if (qemu_in_vcpu_thread()) {
658 qemu_system_vmstop_request_prepare();
659 qemu_system_vmstop_request(state);
660
661
662
663
664 cpu_stop_current();
665 return 0;
666 }
667
668 return do_vm_stop(state, true);
669}
670
671
672
673
674
675
676int vm_prepare_start(void)
677{
678 RunState requested;
679
680 qemu_vmstop_requested(&requested);
681 if (runstate_is_running() && requested == RUN_STATE__MAX) {
682 return -1;
683 }
684
685
686
687
688
689
690 if (runstate_is_running()) {
691 qapi_event_send_stop();
692 qapi_event_send_resume();
693 return -1;
694 }
695
696
697 qapi_event_send_resume();
698
699 cpu_enable_ticks();
700 runstate_set(RUN_STATE_RUNNING);
701 vm_state_notify(1, RUN_STATE_RUNNING);
702 return 0;
703}
704
705void vm_start(void)
706{
707 if (!vm_prepare_start()) {
708 resume_all_vcpus();
709 }
710}
711
712
713
714int vm_stop_force_state(RunState state)
715{
716 if (runstate_is_running()) {
717 return vm_stop(state);
718 } else {
719 int ret;
720 runstate_set(state);
721
722 bdrv_drain_all();
723
724
725 ret = bdrv_flush_all();
726 trace_vm_stop_flush_all(ret);
727 return ret;
728 }
729}
730
731void qmp_memsave(int64_t addr, int64_t size, const char *filename,
732 bool has_cpu, int64_t cpu_index, Error **errp)
733{
734 FILE *f;
735 uint32_t l;
736 CPUState *cpu;
737 uint8_t buf[1024];
738 int64_t orig_addr = addr, orig_size = size;
739
740 if (!has_cpu) {
741 cpu_index = 0;
742 }
743
744 cpu = qemu_get_cpu(cpu_index);
745 if (cpu == NULL) {
746 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
747 "a CPU number");
748 return;
749 }
750
751 f = fopen(filename, "wb");
752 if (!f) {
753 error_setg_file_open(errp, errno, filename);
754 return;
755 }
756
757 while (size != 0) {
758 l = sizeof(buf);
759 if (l > size)
760 l = size;
761 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
762 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRId64
763 " specified", orig_addr, orig_size);
764 goto exit;
765 }
766 if (fwrite(buf, 1, l, f) != l) {
767 error_setg(errp, QERR_IO_ERROR);
768 goto exit;
769 }
770 addr += l;
771 size -= l;
772 }
773
774exit:
775 fclose(f);
776}
777
778void qmp_pmemsave(int64_t addr, int64_t size, const char *filename,
779 Error **errp)
780{
781 FILE *f;
782 uint32_t l;
783 uint8_t buf[1024];
784
785 f = fopen(filename, "wb");
786 if (!f) {
787 error_setg_file_open(errp, errno, filename);
788 return;
789 }
790
791 while (size != 0) {
792 l = sizeof(buf);
793 if (l > size)
794 l = size;
795 cpu_physical_memory_read(addr, buf, l);
796 if (fwrite(buf, 1, l, f) != l) {
797 error_setg(errp, QERR_IO_ERROR);
798 goto exit;
799 }
800 addr += l;
801 size -= l;
802 }
803
804exit:
805 fclose(f);
806}
807
808void qmp_inject_nmi(Error **errp)
809{
810 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp);
811}
812
813