diff --git a/cpus.c b/cpus.c index 0f2ce60a3..e916137db 100644 --- a/cpus.c +++ b/cpus.c @@ -1183,3 +1183,33 @@ void qmp_memsave(int64_t addr, int64_t size, const char *filename, exit: fclose(f); } + +void qmp_pmemsave(int64_t addr, int64_t size, const char *filename, + Error **errp) +{ + FILE *f; + uint32_t l; + uint8_t buf[1024]; + + f = fopen(filename, "wb"); + if (!f) { + error_set(errp, QERR_OPEN_FILE_FAILED, filename); + return; + } + + while (size != 0) { + l = sizeof(buf); + if (l > size) + l = size; + cpu_physical_memory_rw(addr, buf, l, 0); + if (fwrite(buf, 1, l, f) != l) { + error_set(errp, QERR_IO_ERROR); + goto exit; + } + addr += l; + size -= l; + } + +exit: + fclose(f); +} diff --git a/hmp-commands.hx b/hmp-commands.hx index dac0b47b8..0a721ccd9 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -703,8 +703,7 @@ ETEXI .args_type = "val:l,size:i,filename:s", .params = "addr size file", .help = "save to disk physical memory dump starting at 'addr' of size 'size'", - .user_print = monitor_user_noop, - .mhandler.cmd_new = do_physical_memory_save, + .mhandler.cmd = hmp_pmemsave, }, STEXI diff --git a/hmp.c b/hmp.c index 67b3eb394..96e3ce18c 100644 --- a/hmp.c +++ b/hmp.c @@ -550,3 +550,14 @@ void hmp_memsave(Monitor *mon, const QDict *qdict) qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp); hmp_handle_error(mon, &errp); } + +void hmp_pmemsave(Monitor *mon, const QDict *qdict) +{ + uint32_t size = qdict_get_int(qdict, "size"); + const char *filename = qdict_get_str(qdict, "filename"); + uint64_t addr = qdict_get_int(qdict, "val"); + Error *errp = NULL; + + qmp_pmemsave(addr, size, filename, &errp); + hmp_handle_error(mon, &errp); +} diff --git a/hmp.h b/hmp.h index dd8ad0cf4..4882bea1f 100644 --- a/hmp.h +++ b/hmp.h @@ -38,5 +38,6 @@ void hmp_system_reset(Monitor *mon, const QDict *qdict); void hmp_system_powerdown(Monitor *mon, const QDict *qdict); void hmp_cpu(Monitor *mon, const QDict *qdict); void hmp_memsave(Monitor *mon, const QDict *qdict); +void hmp_pmemsave(Monitor *mon, const QDict *qdict); #endif diff --git a/monitor.c b/monitor.c index 727201441..a1b46b3c6 100644 --- a/monitor.c +++ b/monitor.c @@ -1370,43 +1370,6 @@ static void do_print(Monitor *mon, const QDict *qdict) monitor_printf(mon, "\n"); } -static int do_physical_memory_save(Monitor *mon, const QDict *qdict, - QObject **ret_data) -{ - FILE *f; - uint32_t l; - uint8_t buf[1024]; - uint32_t size = qdict_get_int(qdict, "size"); - const char *filename = qdict_get_str(qdict, "filename"); - target_phys_addr_t addr = qdict_get_int(qdict, "val"); - int ret = -1; - - f = fopen(filename, "wb"); - if (!f) { - qerror_report(QERR_OPEN_FILE_FAILED, filename); - return -1; - } - while (size != 0) { - l = sizeof(buf); - if (l > size) - l = size; - cpu_physical_memory_read(addr, buf, l); - if (fwrite(buf, 1, l, f) != l) { - monitor_printf(mon, "fwrite() error in do_physical_memory_save\n"); - goto exit; - } - fflush(f); - addr += l; - size -= l; - } - - ret = 0; - -exit: - fclose(f); - return ret; -} - static void do_sum(Monitor *mon, const QDict *qdict) { uint32_t addr; diff --git a/qapi-schema.json b/qapi-schema.json index dbf617001..7f9aa94f1 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -927,3 +927,25 @@ ## { 'command': 'memsave', 'data': {'val': 'int', 'size': 'int', 'filename': 'str', '*cpu-index': 'int'} } + +## +# @pmemsave: +# +# Save a portion of guest physical memory to a file. +# +# @val: the physical address of the guest to start from +# +# @size: the size of memory region to save +# +# @filename: the file to save the memory to as binary data +# +# Returns: Nothing on success +# If @filename cannot be opened, OpenFileFailed +# If an I/O error occurs while writing the file, IOError +# +# Since: 0.14.0 +# +# Notes: Errors were not reliably returned until 1.1 +## +{ 'command': 'pmemsave', + 'data': {'val': 'int', 'size': 'int', 'filename': 'str'} } diff --git a/qmp-commands.hx b/qmp-commands.hx index 0e2f3926e..5093ac900 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -382,10 +382,7 @@ EQMP { .name = "pmemsave", .args_type = "val:l,size:i,filename:s", - .params = "addr size file", - .help = "save to disk physical memory dump starting at 'addr' of size 'size'", - .user_print = monitor_user_noop, - .mhandler.cmd_new = do_physical_memory_save, + .mhandler.cmd_new = qmp_marshal_input_pmemsave, }, SQMP