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 #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 "accel/accel-cpu-ops.h" 35 #include "system/hw_accel.h" 36 #include "exec/cpu-common.h" 37 #include "qemu/thread.h" 38 #include "qemu/main-loop.h" 39 #include "qemu/plugin.h" 40 #include "system/cpus.h" 41 #include "qemu/guest-random.h" 42 #include "hw/nmi.h" 43 #include "system/replay.h" 44 #include "system/runstate.h" 45 #include "system/cpu-timers.h" 46 #include "system/whpx.h" 47 #include "hw/boards.h" 48 #include "hw/hw.h" 49 #include "trace.h" 50 51 #ifdef CONFIG_LINUX 52 53 #include <sys/prctl.h> 54 55 #ifndef PR_MCE_KILL 56 #define PR_MCE_KILL 33 57 #endif 58 59 #ifndef PR_MCE_KILL_SET 60 #define PR_MCE_KILL_SET 1 61 #endif 62 63 #ifndef PR_MCE_KILL_EARLY 64 #define PR_MCE_KILL_EARLY 1 65 #endif 66 67 #endif /* CONFIG_LINUX */ 68 69 /* The Big QEMU Lock (BQL) */ 70 static QemuMutex bql; 71 72 /* 73 * The chosen accelerator is supposed to register this. 74 */ 75 static const AccelOpsClass *cpus_accel; 76 77 bool cpu_is_stopped(CPUState *cpu) 78 { 79 return cpu->stopped || !runstate_is_running(); 80 } 81 82 bool cpu_work_list_empty(CPUState *cpu) 83 { 84 return QSIMPLEQ_EMPTY_ATOMIC(&cpu->work_list); 85 } 86 87 bool cpu_thread_is_idle(CPUState *cpu) 88 { 89 if (cpu->stop || !cpu_work_list_empty(cpu)) { 90 return false; 91 } 92 if (cpu_is_stopped(cpu)) { 93 return true; 94 } 95 if (!cpu->halted || cpu_has_work(cpu)) { 96 return false; 97 } 98 if (cpus_accel->cpu_thread_is_idle) { 99 return cpus_accel->cpu_thread_is_idle(cpu); 100 } 101 return true; 102 } 103 104 bool all_cpu_threads_idle(void) 105 { 106 CPUState *cpu; 107 108 CPU_FOREACH(cpu) { 109 if (!cpu_thread_is_idle(cpu)) { 110 return false; 111 } 112 } 113 return true; 114 } 115 116 /***********************************************************/ 117 void hw_error(const char *fmt, ...) 118 { 119 va_list ap; 120 CPUState *cpu; 121 122 va_start(ap, fmt); 123 fprintf(stderr, "qemu: hardware error: "); 124 vfprintf(stderr, fmt, ap); 125 fprintf(stderr, "\n"); 126 CPU_FOREACH(cpu) { 127 fprintf(stderr, "CPU #%d:\n", cpu->cpu_index); 128 cpu_dump_state(cpu, stderr, CPU_DUMP_FPU); 129 } 130 va_end(ap); 131 abort(); 132 } 133 134 void cpu_synchronize_all_states(void) 135 { 136 CPUState *cpu; 137 138 CPU_FOREACH(cpu) { 139 cpu_synchronize_state(cpu); 140 } 141 } 142 143 void cpu_synchronize_all_post_reset(void) 144 { 145 CPUState *cpu; 146 147 CPU_FOREACH(cpu) { 148 cpu_synchronize_post_reset(cpu); 149 } 150 } 151 152 void cpu_synchronize_all_post_init(void) 153 { 154 CPUState *cpu; 155 156 CPU_FOREACH(cpu) { 157 cpu_synchronize_post_init(cpu); 158 } 159 } 160 161 void cpu_synchronize_all_pre_loadvm(void) 162 { 163 CPUState *cpu; 164 165 CPU_FOREACH(cpu) { 166 cpu_synchronize_pre_loadvm(cpu); 167 } 168 } 169 170 void cpu_synchronize_state(CPUState *cpu) 171 { 172 if (cpus_accel->synchronize_state) { 173 cpus_accel->synchronize_state(cpu); 174 } 175 } 176 177 void cpu_synchronize_post_reset(CPUState *cpu) 178 { 179 if (cpus_accel->synchronize_post_reset) { 180 cpus_accel->synchronize_post_reset(cpu); 181 } 182 } 183 184 void cpu_synchronize_post_init(CPUState *cpu) 185 { 186 if (cpus_accel->synchronize_post_init) { 187 cpus_accel->synchronize_post_init(cpu); 188 } 189 } 190 191 void cpu_synchronize_pre_loadvm(CPUState *cpu) 192 { 193 if (cpus_accel->synchronize_pre_loadvm) { 194 cpus_accel->synchronize_pre_loadvm(cpu); 195 } 196 } 197 198 bool cpus_are_resettable(void) 199 { 200 if (cpus_accel->cpus_are_resettable) { 201 return cpus_accel->cpus_are_resettable(); 202 } 203 return true; 204 } 205 206 void cpu_exec_reset_hold(CPUState *cpu) 207 { 208 if (cpus_accel->cpu_reset_hold) { 209 cpus_accel->cpu_reset_hold(cpu); 210 } 211 } 212 213 int64_t cpus_get_virtual_clock(void) 214 { 215 /* 216 * XXX 217 * 218 * need to check that cpus_accel is not NULL, because qcow2 calls 219 * qemu_get_clock_ns(CLOCK_VIRTUAL) without any accel initialized and 220 * with ticks disabled in some io-tests: 221 * 030 040 041 060 099 120 127 140 156 161 172 181 191 192 195 203 229 249 256 267 222 * 223 * is this expected? 224 * 225 * XXX 226 */ 227 if (cpus_accel && cpus_accel->get_virtual_clock) { 228 return cpus_accel->get_virtual_clock(); 229 } 230 return cpu_get_clock(); 231 } 232 233 /* 234 * Signal the new virtual time to the accelerator. This is only needed 235 * by accelerators that need to track the changes as we warp time. 236 */ 237 void cpus_set_virtual_clock(int64_t new_time) 238 { 239 if (cpus_accel && cpus_accel->set_virtual_clock) { 240 cpus_accel->set_virtual_clock(new_time); 241 } 242 } 243 244 /* 245 * return the time elapsed in VM between vm_start and vm_stop. Unless 246 * icount is active, cpus_get_elapsed_ticks() uses units of the host CPU cycle 247 * counter. 248 */ 249 int64_t cpus_get_elapsed_ticks(void) 250 { 251 if (cpus_accel->get_elapsed_ticks) { 252 return cpus_accel->get_elapsed_ticks(); 253 } 254 return cpu_get_ticks(); 255 } 256 257 void cpu_set_interrupt(CPUState *cpu, int mask) 258 { 259 /* Pairs with cpu_test_interrupt(). */ 260 qatomic_or(&cpu->interrupt_request, mask); 261 } 262 263 void generic_handle_interrupt(CPUState *cpu, int mask) 264 { 265 cpu_set_interrupt(cpu, mask); 266 267 if (!qemu_cpu_is_self(cpu)) { 268 qemu_cpu_kick(cpu); 269 } 270 } 271 272 void cpu_interrupt(CPUState *cpu, int mask) 273 { 274 g_assert(bql_locked()); 275 276 cpus_accel->handle_interrupt(cpu, mask); 277 } 278 279 /* 280 * True if the vm was previously suspended, and has not been woken or reset. 281 */ 282 static int vm_was_suspended; 283 284 void vm_set_suspended(bool suspended) 285 { 286 vm_was_suspended = suspended; 287 } 288 289 bool vm_get_suspended(void) 290 { 291 return vm_was_suspended; 292 } 293 294 static int do_vm_stop(RunState state, bool send_stop) 295 { 296 int ret = 0; 297 RunState oldstate = runstate_get(); 298 299 if (runstate_is_live(oldstate)) { 300 vm_was_suspended = (oldstate == RUN_STATE_SUSPENDED); 301 runstate_set(state); 302 cpu_disable_ticks(); 303 if (oldstate == RUN_STATE_RUNNING) { 304 pause_all_vcpus(); 305 } 306 ret = vm_state_notify(0, state); 307 if (send_stop) { 308 qapi_event_send_stop(); 309 } 310 } 311 312 bdrv_drain_all(); 313 /* 314 * Even if vm_state_notify() return failure, 315 * it would be better to flush as before. 316 */ 317 ret |= bdrv_flush_all(); 318 trace_vm_stop_flush_all(ret); 319 320 return ret; 321 } 322 323 /* Special vm_stop() variant for terminating the process. Historically clients 324 * did not expect a QMP STOP event and so we need to retain compatibility. 325 */ 326 int vm_shutdown(void) 327 { 328 return do_vm_stop(RUN_STATE_SHUTDOWN, false); 329 } 330 331 bool cpu_can_run(CPUState *cpu) 332 { 333 if (cpu->stop) { 334 return false; 335 } 336 if (cpu_is_stopped(cpu)) { 337 return false; 338 } 339 return true; 340 } 341 342 void cpu_handle_guest_debug(CPUState *cpu) 343 { 344 if (replay_running_debug()) { 345 if (!cpu->singlestep_enabled) { 346 /* 347 * Report about the breakpoint and 348 * make a single step to skip it 349 */ 350 replay_breakpoint(); 351 cpu_single_step(cpu, SSTEP_ENABLE); 352 } else { 353 cpu_single_step(cpu, 0); 354 } 355 } else { 356 gdb_set_stop_cpu(cpu); 357 qemu_system_debug_request(); 358 cpu->stopped = true; 359 } 360 } 361 362 #ifdef CONFIG_LINUX 363 static void sigbus_reraise(void) 364 { 365 sigset_t set; 366 struct sigaction action; 367 368 memset(&action, 0, sizeof(action)); 369 action.sa_handler = SIG_DFL; 370 if (!sigaction(SIGBUS, &action, NULL)) { 371 raise(SIGBUS); 372 sigemptyset(&set); 373 sigaddset(&set, SIGBUS); 374 pthread_sigmask(SIG_UNBLOCK, &set, NULL); 375 } 376 perror("Failed to re-raise SIGBUS!"); 377 abort(); 378 } 379 380 static void sigbus_handler(int n, siginfo_t *siginfo, void *ctx) 381 { 382 if (siginfo->si_code != BUS_MCEERR_AO && siginfo->si_code != BUS_MCEERR_AR) { 383 sigbus_reraise(); 384 } 385 386 if (current_cpu) { 387 /* Called asynchronously in VCPU thread. */ 388 if (kvm_on_sigbus_vcpu(current_cpu, siginfo->si_code, siginfo->si_addr)) { 389 sigbus_reraise(); 390 } 391 } else { 392 /* Called synchronously (via signalfd) in main thread. */ 393 if (kvm_on_sigbus(siginfo->si_code, siginfo->si_addr)) { 394 sigbus_reraise(); 395 } 396 } 397 } 398 399 static void qemu_init_sigbus(void) 400 { 401 struct sigaction action; 402 403 /* 404 * ALERT: when modifying this, take care that SIGBUS forwarding in 405 * qemu_prealloc_mem() will continue working as expected. 406 */ 407 memset(&action, 0, sizeof(action)); 408 action.sa_flags = SA_SIGINFO; 409 action.sa_sigaction = sigbus_handler; 410 sigaction(SIGBUS, &action, NULL); 411 412 prctl(PR_MCE_KILL, PR_MCE_KILL_SET, PR_MCE_KILL_EARLY, 0, 0); 413 } 414 #else /* !CONFIG_LINUX */ 415 static void qemu_init_sigbus(void) 416 { 417 } 418 #endif /* !CONFIG_LINUX */ 419 420 static QemuThread io_thread; 421 422 /* cpu creation */ 423 static QemuCond qemu_cpu_cond; 424 /* system init */ 425 static QemuCond qemu_pause_cond; 426 427 void qemu_init_cpu_loop(void) 428 { 429 qemu_init_sigbus(); 430 qemu_cond_init(&qemu_cpu_cond); 431 qemu_cond_init(&qemu_pause_cond); 432 qemu_mutex_init(&bql); 433 434 qemu_thread_get_self(&io_thread); 435 } 436 437 void run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data) 438 { 439 do_run_on_cpu(cpu, func, data, &bql); 440 } 441 442 static void qemu_cpu_stop(CPUState *cpu, bool exit) 443 { 444 g_assert(qemu_cpu_is_self(cpu)); 445 cpu->stop = false; 446 cpu->stopped = true; 447 if (exit) { 448 cpu_exit(cpu); 449 } 450 qemu_cond_broadcast(&qemu_pause_cond); 451 } 452 453 void qemu_process_cpu_events_common(CPUState *cpu) 454 { 455 qatomic_set_mb(&cpu->thread_kicked, false); 456 if (cpu->stop) { 457 qemu_cpu_stop(cpu, false); 458 } 459 process_queued_cpu_work(cpu); 460 } 461 462 void qemu_process_cpu_events(CPUState *cpu) 463 { 464 bool slept = false; 465 466 qatomic_set(&cpu->exit_request, false); 467 while (cpu_thread_is_idle(cpu)) { 468 if (!slept) { 469 slept = true; 470 qemu_plugin_vcpu_idle_cb(cpu); 471 } 472 qemu_cond_wait(cpu->halt_cond, &bql); 473 } 474 if (slept) { 475 qemu_plugin_vcpu_resume_cb(cpu); 476 } 477 478 qemu_process_cpu_events_common(cpu); 479 } 480 481 void cpus_kick_thread(CPUState *cpu) 482 { 483 if (qatomic_read(&cpu->thread_kicked)) { 484 return; 485 } 486 qatomic_set(&cpu->thread_kicked, true); 487 488 #ifndef _WIN32 489 int err = pthread_kill(cpu->thread->thread, SIG_IPI); 490 if (err && err != ESRCH) { 491 fprintf(stderr, "qemu:%s: %s", __func__, strerror(err)); 492 exit(1); 493 } 494 #else 495 qemu_sem_post(&cpu->sem); 496 #endif 497 } 498 499 void qemu_cpu_kick(CPUState *cpu) 500 { 501 qemu_cond_broadcast(cpu->halt_cond); 502 if (cpus_accel->kick_vcpu_thread) { 503 cpus_accel->kick_vcpu_thread(cpu); 504 } else { /* default */ 505 cpus_kick_thread(cpu); 506 } 507 } 508 509 void qemu_cpu_kick_self(void) 510 { 511 assert(current_cpu); 512 cpus_kick_thread(current_cpu); 513 } 514 515 bool qemu_cpu_is_self(CPUState *cpu) 516 { 517 return qemu_thread_is_self(cpu->thread); 518 } 519 520 bool qemu_in_vcpu_thread(void) 521 { 522 return current_cpu && qemu_cpu_is_self(current_cpu); 523 } 524 525 QEMU_DEFINE_STATIC_CO_TLS(bool, bql_locked) 526 527 bool mutex_is_bql(QemuMutex *mutex) 528 { 529 return mutex == &bql; 530 } 531 532 void bql_update_status(bool locked) 533 { 534 /* This function should only be used when an update happened.. */ 535 assert(bql_locked() != locked); 536 set_bql_locked(locked); 537 } 538 539 static uint32_t bql_unlock_blocked; 540 541 void bql_block_unlock(bool increase) 542 { 543 uint32_t new_value; 544 545 assert(bql_locked()); 546 547 /* check for overflow! */ 548 new_value = bql_unlock_blocked + increase - !increase; 549 assert((new_value > bql_unlock_blocked) == increase); 550 bql_unlock_blocked = new_value; 551 } 552 553 bool bql_locked(void) 554 { 555 return get_bql_locked(); 556 } 557 558 bool qemu_in_main_thread(void) 559 { 560 return bql_locked(); 561 } 562 563 void rust_bql_mock_lock(void) 564 { 565 error_report("This function should be used only from tests"); 566 abort(); 567 } 568 569 /* 570 * The BQL is taken from so many places that it is worth profiling the 571 * callers directly, instead of funneling them all through a single function. 572 */ 573 void bql_lock_impl(const char *file, int line) 574 { 575 QemuMutexLockFunc bql_lock_fn = qatomic_read(&bql_mutex_lock_func); 576 577 g_assert(!bql_locked()); 578 bql_lock_fn(&bql, file, line); 579 } 580 581 void bql_unlock(void) 582 { 583 g_assert(bql_locked()); 584 g_assert(!bql_unlock_blocked); 585 qemu_mutex_unlock(&bql); 586 } 587 588 void qemu_cond_wait_bql(QemuCond *cond) 589 { 590 qemu_cond_wait(cond, &bql); 591 } 592 593 void qemu_cond_timedwait_bql(QemuCond *cond, int ms) 594 { 595 qemu_cond_timedwait(cond, &bql, ms); 596 } 597 598 /* signal CPU creation */ 599 void cpu_thread_signal_created(CPUState *cpu) 600 { 601 cpu->created = true; 602 qemu_cond_signal(&qemu_cpu_cond); 603 } 604 605 /* signal CPU destruction */ 606 void cpu_thread_signal_destroyed(CPUState *cpu) 607 { 608 cpu->created = false; 609 qemu_cond_signal(&qemu_cpu_cond); 610 } 611 612 void cpu_pause(CPUState *cpu) 613 { 614 if (qemu_cpu_is_self(cpu)) { 615 qemu_cpu_stop(cpu, true); 616 } else { 617 cpu->stop = true; 618 cpu_exit(cpu); 619 } 620 } 621 622 void cpu_resume(CPUState *cpu) 623 { 624 cpu->stop = false; 625 cpu->stopped = false; 626 qemu_cpu_kick(cpu); 627 } 628 629 static bool all_vcpus_paused(void) 630 { 631 CPUState *cpu; 632 633 CPU_FOREACH(cpu) { 634 if (!cpu->stopped) { 635 return false; 636 } 637 } 638 639 return true; 640 } 641 642 void pause_all_vcpus(void) 643 { 644 CPUState *cpu; 645 646 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, false); 647 CPU_FOREACH(cpu) { 648 cpu_pause(cpu); 649 } 650 651 /* We need to drop the replay_lock so any vCPU threads woken up 652 * can finish their replay tasks 653 */ 654 replay_mutex_unlock(); 655 656 while (!all_vcpus_paused()) { 657 qemu_cond_wait(&qemu_pause_cond, &bql); 658 /* FIXME: is this needed? */ 659 CPU_FOREACH(cpu) { 660 qemu_cpu_kick(cpu); 661 } 662 } 663 664 bql_unlock(); 665 replay_mutex_lock(); 666 bql_lock(); 667 } 668 669 void resume_all_vcpus(void) 670 { 671 CPUState *cpu; 672 673 if (!runstate_is_running()) { 674 return; 675 } 676 677 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); 678 CPU_FOREACH(cpu) { 679 cpu_resume(cpu); 680 } 681 } 682 683 void cpu_remove_sync(CPUState *cpu) 684 { 685 cpu->stop = true; 686 cpu->unplug = true; 687 cpu_exit(cpu); 688 bql_unlock(); 689 qemu_thread_join(cpu->thread); 690 bql_lock(); 691 } 692 693 void cpus_register_accel(const AccelOpsClass *ops) 694 { 695 assert(ops != NULL); 696 assert(ops->create_vcpu_thread != NULL); /* mandatory */ 697 assert(ops->handle_interrupt); 698 699 cpus_accel = ops; 700 } 701 702 const AccelOpsClass *cpus_get_accel(void) 703 { 704 /* broken if we call this early */ 705 assert(cpus_accel); 706 return cpus_accel; 707 } 708 709 void qemu_init_vcpu(CPUState *cpu) 710 { 711 MachineState *ms = MACHINE(qdev_get_machine()); 712 713 cpu->nr_threads = ms->smp.threads; 714 cpu->stopped = true; 715 cpu->random_seed = qemu_guest_random_seed_thread_part1(); 716 717 if (!cpu->as) { 718 /* If the target cpu hasn't set up any address spaces itself, 719 * give it the default one. 720 */ 721 cpu->num_ases = 1; 722 cpu_address_space_init(cpu, 0, "cpu-memory", cpu->memory); 723 } 724 725 /* accelerators all implement the AccelOpsClass */ 726 g_assert(cpus_accel != NULL && cpus_accel->create_vcpu_thread != NULL); 727 cpus_accel->create_vcpu_thread(cpu); 728 729 while (!cpu->created) { 730 qemu_cond_wait(&qemu_cpu_cond, &bql); 731 } 732 } 733 734 void cpu_stop_current(void) 735 { 736 if (current_cpu) { 737 current_cpu->stop = true; 738 cpu_exit(current_cpu); 739 } 740 } 741 742 int vm_stop(RunState state) 743 { 744 if (qemu_in_vcpu_thread()) { 745 qemu_system_vmstop_request_prepare(); 746 qemu_system_vmstop_request(state); 747 /* 748 * FIXME: should not return to device code in case 749 * vm_stop() has been requested. 750 */ 751 cpu_stop_current(); 752 return 0; 753 } 754 755 return do_vm_stop(state, true); 756 } 757 758 /** 759 * Prepare for (re)starting the VM. 760 * Returns 0 if the vCPUs should be restarted, -1 on an error condition, 761 * and 1 otherwise. 762 */ 763 int vm_prepare_start(bool step_pending) 764 { 765 int ret = vm_was_suspended ? 1 : 0; 766 RunState state = vm_was_suspended ? RUN_STATE_SUSPENDED : RUN_STATE_RUNNING; 767 RunState requested; 768 769 qemu_vmstop_requested(&requested); 770 if (runstate_is_running() && requested == RUN_STATE__MAX) { 771 return -1; 772 } 773 774 /* Ensure that a STOP/RESUME pair of events is emitted if a 775 * vmstop request was pending. The BLOCK_IO_ERROR event, for 776 * example, according to documentation is always followed by 777 * the STOP event. 778 */ 779 if (runstate_is_running()) { 780 qapi_event_send_stop(); 781 qapi_event_send_resume(); 782 return -1; 783 } 784 785 /* 786 * WHPX accelerator needs to know whether we are going to step 787 * any CPUs, before starting the first one. 788 */ 789 accel_pre_resume(MACHINE(qdev_get_machine()), step_pending); 790 791 /* We are sending this now, but the CPUs will be resumed shortly later */ 792 qapi_event_send_resume(); 793 794 cpu_enable_ticks(); 795 runstate_set(state); 796 vm_state_notify(1, state); 797 vm_was_suspended = false; 798 return ret; 799 } 800 801 void vm_start(void) 802 { 803 if (!vm_prepare_start(false)) { 804 resume_all_vcpus(); 805 } 806 } 807 808 void vm_resume(RunState state) 809 { 810 if (runstate_is_live(state)) { 811 vm_start(); 812 } else { 813 runstate_set(state); 814 } 815 } 816 817 /* does a state transition even if the VM is already stopped, 818 current state is forgotten forever */ 819 int vm_stop_force_state(RunState state) 820 { 821 if (runstate_is_live(runstate_get())) { 822 return vm_stop(state); 823 } else { 824 int ret; 825 runstate_set(state); 826 827 bdrv_drain_all(); 828 /* Make sure to return an error if the flush in a previous vm_stop() 829 * failed. */ 830 ret = bdrv_flush_all(); 831 trace_vm_stop_flush_all(ret); 832 return ret; 833 } 834 } 835 836 void qmp_memsave(uint64_t addr, uint64_t size, const char *filename, 837 bool has_cpu, int64_t cpu_index, Error **errp) 838 { 839 FILE *f; 840 uint64_t l; 841 CPUState *cpu; 842 uint8_t buf[1024]; 843 uint64_t orig_addr = addr, orig_size = size; 844 845 if (!has_cpu) { 846 cpu_index = 0; 847 } 848 849 cpu = qemu_get_cpu(cpu_index); 850 if (cpu == NULL) { 851 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", 852 "a CPU number"); 853 return; 854 } 855 856 f = fopen(filename, "wb"); 857 if (!f) { 858 error_setg_file_open(errp, errno, filename); 859 return; 860 } 861 862 while (size != 0) { 863 l = sizeof(buf); 864 if (l > size) 865 l = size; 866 if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) { 867 error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRIu64 868 " specified", orig_addr, orig_size); 869 goto exit; 870 } 871 if (fwrite(buf, 1, l, f) != l) { 872 error_setg(errp, "writing memory to '%s' failed", 873 filename); 874 goto exit; 875 } 876 addr += l; 877 size -= l; 878 } 879 880 exit: 881 fclose(f); 882 } 883 884 void qmp_pmemsave(uint64_t addr, uint64_t size, const char *filename, 885 Error **errp) 886 { 887 FILE *f; 888 uint64_t l; 889 uint8_t buf[1024]; 890 891 f = fopen(filename, "wb"); 892 if (!f) { 893 error_setg_file_open(errp, errno, filename); 894 return; 895 } 896 897 while (size != 0) { 898 l = sizeof(buf); 899 if (l > size) 900 l = size; 901 cpu_physical_memory_read(addr, buf, l); 902 if (fwrite(buf, 1, l, f) != l) { 903 error_setg(errp, "writing memory to '%s' failed", 904 filename); 905 goto exit; 906 } 907 addr += l; 908 size -= l; 909 } 910 911 exit: 912 fclose(f); 913 } 914 915 void qmp_inject_nmi(Error **errp) 916 { 917 nmi_monitor_handle(monitor_get_cpu_index(monitor_cur()), errp); 918 } 919 920