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