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