2 * Miscellaneous target-dependent HMP commands
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "disas/disas.h"
27 #include "exec/address-spaces.h"
28 #include "monitor/hmp-target.h"
29 #include "monitor/monitor-internal.h"
30 #include "qapi/error.h"
31 #include "qapi/qmp/qdict.h"
32 #include "sysemu/hw_accel.h"
34 /* Set the current CPU defined by the user. Callers must hold BQL. */
35 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
39 cpu
= qemu_get_cpu(cpu_index
);
43 g_free(mon
->mon_cpu_path
);
44 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
48 /* Callers must hold BQL. */
49 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
53 if (mon
->mon_cpu_path
) {
54 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
57 g_free(mon
->mon_cpu_path
);
58 mon
->mon_cpu_path
= NULL
;
61 if (!mon
->mon_cpu_path
) {
65 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
70 cpu_synchronize_state(cpu
);
75 CPUState
*mon_get_cpu(Monitor
*mon
)
77 return mon_get_cpu_sync(mon
, true);
80 CPUArchState
*mon_get_cpu_env(Monitor
*mon
)
82 CPUState
*cs
= mon_get_cpu(mon
);
84 return cs
? cs
->env_ptr
: NULL
;
87 int monitor_get_cpu_index(Monitor
*mon
)
89 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
91 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
94 void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
96 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
97 int vcpu
= qdict_get_try_int(qdict
, "vcpu", -1);
102 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
103 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
106 cs
= vcpu
>= 0 ? qemu_get_cpu(vcpu
) : mon_get_cpu(mon
);
110 monitor_printf(mon
, "CPU#%d not available\n", vcpu
);
112 monitor_printf(mon
, "No CPU available\n");
117 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
118 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
122 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
123 hwaddr addr
, int is_physical
)
125 int l
, line_size
, i
, max_digits
, len
;
128 CPUState
*cs
= mon_get_cpu(mon
);
130 if (!cs
&& (format
== 'i' || !is_physical
)) {
131 monitor_printf(mon
, "Can not dump without CPU\n");
136 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
150 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
154 max_digits
= (wsize
* 8) / 4;
158 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
167 monitor_printf(mon
, HWADDR_FMT_plx
":", addr
);
169 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
175 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
176 MemTxResult r
= address_space_read(as
, addr
,
177 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
179 monitor_printf(mon
, " Cannot access memory\n");
183 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
184 monitor_printf(mon
, " Cannot access memory\n");
199 v
= (uint32_t)ldl_p(buf
+ i
);
205 monitor_printf(mon
, " ");
208 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
211 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
214 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
217 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
220 monitor_printc(mon
, v
);
225 monitor_printf(mon
, "\n");
231 void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
233 int count
= qdict_get_int(qdict
, "count");
234 int format
= qdict_get_int(qdict
, "format");
235 int size
= qdict_get_int(qdict
, "size");
236 target_long addr
= qdict_get_int(qdict
, "addr");
238 memory_dump(mon
, count
, format
, size
, addr
, 0);
241 void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
243 int count
= qdict_get_int(qdict
, "count");
244 int format
= qdict_get_int(qdict
, "format");
245 int size
= qdict_get_int(qdict
, "size");
246 hwaddr addr
= qdict_get_int(qdict
, "addr");
248 memory_dump(mon
, count
, format
, size
, addr
, 1);
251 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
253 Int128 gpa_region_size
;
254 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
258 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
262 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
263 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
264 memory_region_unref(mrs
.mr
);
268 gpa_region_size
= int128_make64(size
);
269 if (int128_lt(mrs
.size
, gpa_region_size
)) {
270 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
272 memory_region_unref(mrs
.mr
);
277 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
280 void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
282 hwaddr addr
= qdict_get_int(qdict
, "addr");
283 Error
*local_err
= NULL
;
284 MemoryRegion
*mr
= NULL
;
287 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
289 error_report_err(local_err
);
293 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
295 addr
, mr
->name
, ptr
);
297 memory_region_unref(mr
);
300 void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
302 target_ulong addr
= qdict_get_int(qdict
, "addr");
304 CPUState
*cs
= mon_get_cpu(mon
);
308 monitor_printf(mon
, "No cpu\n");
312 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
314 monitor_printf(mon
, "Unmapped\n");
316 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
317 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
322 static uint64_t vtop(void *ptr
, Error
**errp
)
326 uintptr_t addr
= (uintptr_t) ptr
;
327 uintptr_t pagesize
= qemu_real_host_page_size();
328 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
331 fd
= open("/proc/self/pagemap", O_RDONLY
);
333 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
337 /* Force copy-on-write if necessary. */
338 qatomic_add((uint8_t *)ptr
, 0);
340 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
341 error_setg_errno(errp
, errno
, "Cannot read pagemap");
344 if ((pinfo
& (1ull << 63)) == 0) {
345 error_setg(errp
, "Page not present");
348 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
355 void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
357 hwaddr addr
= qdict_get_int(qdict
, "addr");
358 Error
*local_err
= NULL
;
359 MemoryRegion
*mr
= NULL
;
363 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
365 error_report_err(local_err
);
369 physaddr
= vtop(ptr
, &local_err
);
371 error_report_err(local_err
);
373 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
374 " (%s) is 0x%" PRIx64
"\n",
375 addr
, mr
->name
, (uint64_t) physaddr
);
378 memory_region_unref(mr
);