@samitouri / QOSamiQemu / commits / 259c2aae8d

system: Extract QMP memsave/pmemsave commands to physmem-qmp-cmds.c

Keep cpus.c related to vCPU scheduling, move the QMP handlers related to dumping physical memory to file to their own unit. Fix a pair of checkpatch.pl errors doing so: ERROR: braces {} are necessary for all arms of this statement #185: FILE: system/physmem-qmp-cmds.c:51: + if (l > size) [...] Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20260812211708.92824-16-philmd@oss.qualcomm.com>

Philippe Mathieu-Daudé committed Aug 12, 2026 at 14:53 UTC 259c2aae8d24b7ef4631e11ed14b7b32795127f0
4 files changed +109 -92
MAINTAINERS
+1
@@ -3436,6 +3436,7 @@ F: system/ioport.c
3436 F: system/memory.c
3437 F: system/memory_mapping.c
3438 F: system/physmem.c
3439 +F: system/physmem-qmp-cmds.c
3440 F: system/memory_ldst*
3441 F: system/memory-internal.h
3442 F: system/ram-block-attributes.c
system/cpus.c
-92
@@ -25,9 +25,7 @@
25 #include "qemu/osdep.h"
26 #include "qemu/coroutine-tls.h"
27 #include "qapi/error.h"
28 -#include "qapi/qapi-commands-machine.h"
28 #include "qapi/qapi-events-run-state.h"
30 -#include "qapi/qmp/qerror.h"
29 #include "exec/gdbstub.h"
30 #include "accel/accel-cpu-ops.h"
31 #include "system/hw_accel.h"
@@ -40,7 +38,6 @@
38 #include "system/physmem.h"
39 #include "system/replay.h"
40 #include "system/runstate.h"
43 -#include "migration/misc.h"
41 #include "system/cpu-timers.h"
42 #include "system/whpx.h"
43 #include "hw/core/boards.h"
@@ -831,92 +828,3 @@ int vm_stop_force_state(RunState state)
828 return ret;
829 }
830 }
834 -
835 -void qmp_memsave(uint64_t addr, uint64_t size, const char *filename,
836 - bool has_cpu, int64_t cpu_index, Error **errp)
837 -{
838 - FILE *f;
839 - uint64_t l;
840 - CPUState *cpu;
841 - uint8_t buf[1024];
842 - uint64_t orig_addr = addr, orig_size = size;
843 -
844 - if (migration_guest_ram_loading()) {
845 - error_setg(errp, "Guest memory access not allowed during migration");
846 - return;
847 - }
848 -
849 - if (!has_cpu) {
850 - cpu_index = 0;
851 - }
852 -
853 - cpu = qemu_get_cpu(cpu_index);
854 - if (cpu == NULL) {
855 - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
856 - "a CPU number");
857 - return;
858 - }
859 -
860 - f = fopen(filename, "wb");
861 - if (!f) {
862 - error_setg_file_open(errp, errno, filename);
863 - return;
864 - }
865 -
866 - while (size != 0) {
867 - l = sizeof(buf);
868 - if (l > size)
869 - l = size;
870 - if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
871 - error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRIu64
872 - " specified", orig_addr, orig_size);
873 - goto exit;
874 - }
875 - if (fwrite(buf, 1, l, f) != l) {
876 - error_setg(errp, "writing memory to '%s' failed",
877 - filename);
878 - goto exit;
879 - }
880 - addr += l;
881 - size -= l;
882 - }
883 -
884 -exit:
885 - fclose(f);
886 -}
887 -
888 -void qmp_pmemsave(uint64_t addr, uint64_t size, const char *filename,
889 - Error **errp)
890 -{
891 - FILE *f;
892 - uint64_t l;
893 - uint8_t buf[1024];
894 -
895 - if (migration_guest_ram_loading()) {
896 - error_setg(errp, "Guest memory access not allowed during migration");
897 - return;
898 - }
899 -
900 - f = fopen(filename, "wb");
901 - if (!f) {
902 - error_setg_file_open(errp, errno, filename);
903 - return;
904 - }
905 -
906 - while (size != 0) {
907 - l = sizeof(buf);
908 - if (l > size)
909 - l = size;
910 - physical_memory_read(addr, buf, l);
911 - if (fwrite(buf, 1, l, f) != l) {
912 - error_setg(errp, "writing memory to '%s' failed",
913 - filename);
914 - goto exit;
915 - }
916 - addr += l;
917 - size -= l;
918 - }
919 -
920 -exit:
921 - fclose(f);
922 -}
system/meson.build
+1
@@ -19,6 +19,7 @@ system_ss.add(files(
19 'memory_mapping.c',
20 'memory.c',
21 'physmem.c',
22 + 'physmem-qmp-cmds.c',
23 'qdev-monitor.c',
24 'qtest.c',
25 'rtc.c',
system/physmem-qmp-cmds.c new
+107
@@ -0,0 +1,107 @@
1 +/*
2 + * QMP commands to dump physical memory
3 + *
4 + * Copyright (c) 2003-2008 Fabrice Bellard
5 + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
6 + *
7 + * SPDX-License-Identifier: GPL-2.0-or-later
8 + */
9 +
10 +#include "qemu/osdep.h"
11 +#include "qapi/error.h"
12 +#include "qapi/qapi-commands-machine.h"
13 +#include "qapi/qmp/qerror.h"
14 +#include "hw/core/cpu.h"
15 +#include "system/physmem.h"
16 +#include "migration/misc.h"
17 +
18 +void qmp_memsave(uint64_t addr, uint64_t size, const char *filename,
19 + bool has_cpu, int64_t cpu_index, Error **errp)
20 +{
21 + FILE *f;
22 + uint64_t l;
23 + CPUState *cpu;
24 + uint8_t buf[1024];
25 + uint64_t orig_addr = addr, orig_size = size;
26 +
27 + if (migration_guest_ram_loading()) {
28 + error_setg(errp, "Guest memory access not allowed during migration");
29 + return;
30 + }
31 +
32 + if (!has_cpu) {
33 + cpu_index = 0;
34 + }
35 +
36 + cpu = qemu_get_cpu(cpu_index);
37 + if (cpu == NULL) {
38 + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
39 + "a CPU number");
40 + return;
41 + }
42 +
43 + f = fopen(filename, "wb");
44 + if (!f) {
45 + error_setg_file_open(errp, errno, filename);
46 + return;
47 + }
48 +
49 + while (size != 0) {
50 + l = sizeof(buf);
51 + if (l > size) {
52 + l = size;
53 + }
54 + if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
55 + error_setg(errp, "Invalid addr 0x%016" PRIx64 "/size %" PRIu64
56 + " specified", orig_addr, orig_size);
57 + goto exit;
58 + }
59 + if (fwrite(buf, 1, l, f) != l) {
60 + error_setg(errp, "writing memory to '%s' failed",
61 + filename);
62 + goto exit;
63 + }
64 + addr += l;
65 + size -= l;
66 + }
67 +
68 +exit:
69 + fclose(f);
70 +}
71 +
72 +void qmp_pmemsave(uint64_t addr, uint64_t size, const char *filename,
73 + Error **errp)
74 +{
75 + FILE *f;
76 + uint64_t l;
77 + uint8_t buf[1024];
78 +
79 + if (migration_guest_ram_loading()) {
80 + error_setg(errp, "Guest memory access not allowed during migration");
81 + return;
82 + }
83 +
84 + f = fopen(filename, "wb");
85 + if (!f) {
86 + error_setg_file_open(errp, errno, filename);
87 + return;
88 + }
89 +
90 + while (size != 0) {
91 + l = sizeof(buf);
92 + if (l > size) {
93 + l = size;
94 + }
95 + physical_memory_read(addr, buf, l);
96 + if (fwrite(buf, 1, l, f) != l) {
97 + error_setg(errp, "writing memory to '%s' failed",
98 + filename);
99 + goto exit;
100 + }
101 + addr += l;
102 + size -= l;
103 + }
104 +
105 +exit:
106 + fclose(f);
107 +}