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"
27 #include "monitor/monitor.h"
28 #include "monitor/hmp-target.h"
29 #include "monitor/hmp.h"
30 #include "qapi/qmp/qdict.h"
31 #include "sysemu/kvm.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-misc-target.h"
34 #include "qapi/qapi-commands-misc.h"
35 #include "hw/i386/pc.h"
37 /* Perform linear address sign extension */
38 static hwaddr
addr_canonical(CPUArchState
*env
, hwaddr addr
)
41 if (env
->cr
[4] & CR4_LA57_MASK
) {
42 if (addr
& (1ULL << 56)) {
43 addr
|= (hwaddr
)-(1LL << 57);
46 if (addr
& (1ULL << 47)) {
47 addr
|= (hwaddr
)-(1LL << 48);
54 static void print_pte(Monitor
*mon
, CPUArchState
*env
, hwaddr addr
,
55 hwaddr pte
, hwaddr mask
)
57 addr
= addr_canonical(env
, addr
);
59 monitor_printf(mon
, HWADDR_FMT_plx
": " HWADDR_FMT_plx
60 " %c%c%c%c%c%c%c%c%c\n",
63 pte
& PG_NX_MASK
? 'X' : '-',
64 pte
& PG_GLOBAL_MASK
? 'G' : '-',
65 pte
& PG_PSE_MASK
? 'P' : '-',
66 pte
& PG_DIRTY_MASK
? 'D' : '-',
67 pte
& PG_ACCESSED_MASK
? 'A' : '-',
68 pte
& PG_PCD_MASK
? 'C' : '-',
69 pte
& PG_PWT_MASK
? 'T' : '-',
70 pte
& PG_USER_MASK
? 'U' : '-',
71 pte
& PG_RW_MASK
? 'W' : '-');
74 static void tlb_info_32(Monitor
*mon
, CPUArchState
*env
)
77 uint32_t pgd
, pde
, pte
;
79 pgd
= env
->cr
[3] & ~0xfff;
80 for(l1
= 0; l1
< 1024; l1
++) {
81 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
82 pde
= le32_to_cpu(pde
);
83 if (pde
& PG_PRESENT_MASK
) {
84 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
86 print_pte(mon
, env
, (l1
<< 22), pde
, ~((1 << 21) - 1));
88 for(l2
= 0; l2
< 1024; l2
++) {
89 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
90 pte
= le32_to_cpu(pte
);
91 if (pte
& PG_PRESENT_MASK
) {
92 print_pte(mon
, env
, (l1
<< 22) + (l2
<< 12),
102 static void tlb_info_pae32(Monitor
*mon
, CPUArchState
*env
)
104 unsigned int l1
, l2
, l3
;
105 uint64_t pdpe
, pde
, pte
;
106 uint64_t pdp_addr
, pd_addr
, pt_addr
;
108 pdp_addr
= env
->cr
[3] & ~0x1f;
109 for (l1
= 0; l1
< 4; l1
++) {
110 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
111 pdpe
= le64_to_cpu(pdpe
);
112 if (pdpe
& PG_PRESENT_MASK
) {
113 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
114 for (l2
= 0; l2
< 512; l2
++) {
115 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
116 pde
= le64_to_cpu(pde
);
117 if (pde
& PG_PRESENT_MASK
) {
118 if (pde
& PG_PSE_MASK
) {
119 /* 2M pages with PAE, CR4.PSE is ignored */
120 print_pte(mon
, env
, (l1
<< 30) + (l2
<< 21), pde
,
121 ~((hwaddr
)(1 << 20) - 1));
123 pt_addr
= pde
& 0x3fffffffff000ULL
;
124 for (l3
= 0; l3
< 512; l3
++) {
125 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
126 pte
= le64_to_cpu(pte
);
127 if (pte
& PG_PRESENT_MASK
) {
128 print_pte(mon
, env
, (l1
<< 30) + (l2
<< 21)
142 static void tlb_info_la48(Monitor
*mon
, CPUArchState
*env
,
143 uint64_t l0
, uint64_t pml4_addr
)
145 uint64_t l1
, l2
, l3
, l4
;
146 uint64_t pml4e
, pdpe
, pde
, pte
;
147 uint64_t pdp_addr
, pd_addr
, pt_addr
;
149 for (l1
= 0; l1
< 512; l1
++) {
150 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
151 pml4e
= le64_to_cpu(pml4e
);
152 if (!(pml4e
& PG_PRESENT_MASK
)) {
156 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
157 for (l2
= 0; l2
< 512; l2
++) {
158 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
159 pdpe
= le64_to_cpu(pdpe
);
160 if (!(pdpe
& PG_PRESENT_MASK
)) {
164 if (pdpe
& PG_PSE_MASK
) {
165 /* 1G pages, CR4.PSE is ignored */
166 print_pte(mon
, env
, (l0
<< 48) + (l1
<< 39) + (l2
<< 30),
167 pdpe
, 0x3ffffc0000000ULL
);
171 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
172 for (l3
= 0; l3
< 512; l3
++) {
173 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
174 pde
= le64_to_cpu(pde
);
175 if (!(pde
& PG_PRESENT_MASK
)) {
179 if (pde
& PG_PSE_MASK
) {
180 /* 2M pages, CR4.PSE is ignored */
181 print_pte(mon
, env
, (l0
<< 48) + (l1
<< 39) + (l2
<< 30) +
182 (l3
<< 21), pde
, 0x3ffffffe00000ULL
);
186 pt_addr
= pde
& 0x3fffffffff000ULL
;
187 for (l4
= 0; l4
< 512; l4
++) {
188 cpu_physical_memory_read(pt_addr
191 pte
= le64_to_cpu(pte
);
192 if (pte
& PG_PRESENT_MASK
) {
193 print_pte(mon
, env
, (l0
<< 48) + (l1
<< 39) +
194 (l2
<< 30) + (l3
<< 21) + (l4
<< 12),
195 pte
& ~PG_PSE_MASK
, 0x3fffffffff000ULL
);
203 static void tlb_info_la57(Monitor
*mon
, CPUArchState
*env
)
209 pml5_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
210 for (l0
= 0; l0
< 512; l0
++) {
211 cpu_physical_memory_read(pml5_addr
+ l0
* 8, &pml5e
, 8);
212 pml5e
= le64_to_cpu(pml5e
);
213 if (pml5e
& PG_PRESENT_MASK
) {
214 tlb_info_la48(mon
, env
, l0
, pml5e
& 0x3fffffffff000ULL
);
218 #endif /* TARGET_X86_64 */
220 void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
224 env
= mon_get_cpu_env(mon
);
226 monitor_printf(mon
, "No CPU available\n");
230 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
231 monitor_printf(mon
, "PG disabled\n");
234 if (env
->cr
[4] & CR4_PAE_MASK
) {
236 if (env
->hflags
& HF_LMA_MASK
) {
237 if (env
->cr
[4] & CR4_LA57_MASK
) {
238 tlb_info_la57(mon
, env
);
240 tlb_info_la48(mon
, env
, 0, env
->cr
[3] & 0x3fffffffff000ULL
);
245 tlb_info_pae32(mon
, env
);
248 tlb_info_32(mon
, env
);
252 static void mem_print(Monitor
*mon
, CPUArchState
*env
,
253 hwaddr
*pstart
, int *plast_prot
,
254 hwaddr end
, int prot
)
260 monitor_printf(mon
, HWADDR_FMT_plx
"-" HWADDR_FMT_plx
" "
261 HWADDR_FMT_plx
" %c%c%c\n",
262 addr_canonical(env
, *pstart
),
263 addr_canonical(env
, end
),
264 addr_canonical(env
, end
- *pstart
),
265 prot1
& PG_USER_MASK
? 'u' : '-',
267 prot1
& PG_RW_MASK
? 'w' : '-');
277 static void mem_info_32(Monitor
*mon
, CPUArchState
*env
)
281 uint32_t pgd
, pde
, pte
;
284 pgd
= env
->cr
[3] & ~0xfff;
287 for(l1
= 0; l1
< 1024; l1
++) {
288 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
289 pde
= le32_to_cpu(pde
);
291 if (pde
& PG_PRESENT_MASK
) {
292 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
293 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
294 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
296 for(l2
= 0; l2
< 1024; l2
++) {
297 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
298 pte
= le32_to_cpu(pte
);
299 end
= (l1
<< 22) + (l2
<< 12);
300 if (pte
& PG_PRESENT_MASK
) {
302 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
306 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
311 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
314 /* Flush last range */
315 mem_print(mon
, env
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
318 static void mem_info_pae32(Monitor
*mon
, CPUArchState
*env
)
320 unsigned int l1
, l2
, l3
;
322 uint64_t pdpe
, pde
, pte
;
323 uint64_t pdp_addr
, pd_addr
, pt_addr
;
326 pdp_addr
= env
->cr
[3] & ~0x1f;
329 for (l1
= 0; l1
< 4; l1
++) {
330 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
331 pdpe
= le64_to_cpu(pdpe
);
333 if (pdpe
& PG_PRESENT_MASK
) {
334 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
335 for (l2
= 0; l2
< 512; l2
++) {
336 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
337 pde
= le64_to_cpu(pde
);
338 end
= (l1
<< 30) + (l2
<< 21);
339 if (pde
& PG_PRESENT_MASK
) {
340 if (pde
& PG_PSE_MASK
) {
341 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
343 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
345 pt_addr
= pde
& 0x3fffffffff000ULL
;
346 for (l3
= 0; l3
< 512; l3
++) {
347 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
348 pte
= le64_to_cpu(pte
);
349 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
350 if (pte
& PG_PRESENT_MASK
) {
351 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
356 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
361 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
366 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
369 /* Flush last range */
370 mem_print(mon
, env
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
375 static void mem_info_la48(Monitor
*mon
, CPUArchState
*env
)
378 uint64_t l1
, l2
, l3
, l4
;
379 uint64_t pml4e
, pdpe
, pde
, pte
;
380 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
382 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
385 for (l1
= 0; l1
< 512; l1
++) {
386 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
387 pml4e
= le64_to_cpu(pml4e
);
389 if (pml4e
& PG_PRESENT_MASK
) {
390 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
391 for (l2
= 0; l2
< 512; l2
++) {
392 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
393 pdpe
= le64_to_cpu(pdpe
);
394 end
= (l1
<< 39) + (l2
<< 30);
395 if (pdpe
& PG_PRESENT_MASK
) {
396 if (pdpe
& PG_PSE_MASK
) {
397 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
400 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
402 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
403 for (l3
= 0; l3
< 512; l3
++) {
404 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
405 pde
= le64_to_cpu(pde
);
406 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
407 if (pde
& PG_PRESENT_MASK
) {
408 if (pde
& PG_PSE_MASK
) {
409 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
411 prot
&= pml4e
& pdpe
;
412 mem_print(mon
, env
, &start
,
413 &last_prot
, end
, prot
);
415 pt_addr
= pde
& 0x3fffffffff000ULL
;
416 for (l4
= 0; l4
< 512; l4
++) {
417 cpu_physical_memory_read(pt_addr
420 pte
= le64_to_cpu(pte
);
421 end
= (l1
<< 39) + (l2
<< 30) +
422 (l3
<< 21) + (l4
<< 12);
423 if (pte
& PG_PRESENT_MASK
) {
424 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
426 prot
&= pml4e
& pdpe
& pde
;
430 mem_print(mon
, env
, &start
,
431 &last_prot
, end
, prot
);
436 mem_print(mon
, env
, &start
,
437 &last_prot
, end
, prot
);
443 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
448 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
451 /* Flush last range */
452 mem_print(mon
, env
, &start
, &last_prot
, (hwaddr
)1 << 48, 0);
455 static void mem_info_la57(Monitor
*mon
, CPUArchState
*env
)
458 uint64_t l0
, l1
, l2
, l3
, l4
;
459 uint64_t pml5e
, pml4e
, pdpe
, pde
, pte
;
460 uint64_t pml5_addr
, pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
462 pml5_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
465 for (l0
= 0; l0
< 512; l0
++) {
466 cpu_physical_memory_read(pml5_addr
+ l0
* 8, &pml5e
, 8);
467 pml5e
= le64_to_cpu(pml5e
);
469 if (!(pml5e
& PG_PRESENT_MASK
)) {
471 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
475 pml4_addr
= pml5e
& 0x3fffffffff000ULL
;
476 for (l1
= 0; l1
< 512; l1
++) {
477 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
478 pml4e
= le64_to_cpu(pml4e
);
479 end
= (l0
<< 48) + (l1
<< 39);
480 if (!(pml4e
& PG_PRESENT_MASK
)) {
482 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
486 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
487 for (l2
= 0; l2
< 512; l2
++) {
488 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
489 pdpe
= le64_to_cpu(pdpe
);
490 end
= (l0
<< 48) + (l1
<< 39) + (l2
<< 30);
491 if (pdpe
& PG_PRESENT_MASK
) {
493 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
497 if (pdpe
& PG_PSE_MASK
) {
498 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
500 prot
&= pml5e
& pml4e
;
501 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
505 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
506 for (l3
= 0; l3
< 512; l3
++) {
507 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
508 pde
= le64_to_cpu(pde
);
509 end
= (l0
<< 48) + (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
510 if (pde
& PG_PRESENT_MASK
) {
512 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
516 if (pde
& PG_PSE_MASK
) {
517 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
519 prot
&= pml5e
& pml4e
& pdpe
;
520 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
524 pt_addr
= pde
& 0x3fffffffff000ULL
;
525 for (l4
= 0; l4
< 512; l4
++) {
526 cpu_physical_memory_read(pt_addr
+ l4
* 8, &pte
, 8);
527 pte
= le64_to_cpu(pte
);
528 end
= (l0
<< 48) + (l1
<< 39) + (l2
<< 30) +
529 (l3
<< 21) + (l4
<< 12);
530 if (pte
& PG_PRESENT_MASK
) {
531 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
533 prot
&= pml5e
& pml4e
& pdpe
& pde
;
537 mem_print(mon
, env
, &start
, &last_prot
, end
, prot
);
543 /* Flush last range */
544 mem_print(mon
, env
, &start
, &last_prot
, (hwaddr
)1 << 57, 0);
546 #endif /* TARGET_X86_64 */
548 void hmp_info_mem(Monitor
*mon
, const QDict
*qdict
)
552 env
= mon_get_cpu_env(mon
);
554 monitor_printf(mon
, "No CPU available\n");
558 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
559 monitor_printf(mon
, "PG disabled\n");
562 if (env
->cr
[4] & CR4_PAE_MASK
) {
564 if (env
->hflags
& HF_LMA_MASK
) {
565 if (env
->cr
[4] & CR4_LA57_MASK
) {
566 mem_info_la57(mon
, env
);
568 mem_info_la48(mon
, env
);
573 mem_info_pae32(mon
, env
);
576 mem_info_32(mon
, env
);
580 void hmp_mce(Monitor
*mon
, const QDict
*qdict
)
584 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
585 int bank
= qdict_get_int(qdict
, "bank");
586 uint64_t status
= qdict_get_int(qdict
, "status");
587 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
588 uint64_t addr
= qdict_get_int(qdict
, "addr");
589 uint64_t misc
= qdict_get_int(qdict
, "misc");
590 int flags
= MCE_INJECT_UNCOND_AO
;
592 if (qdict_get_try_bool(qdict
, "broadcast", false)) {
593 flags
|= MCE_INJECT_BROADCAST
;
595 cs
= qemu_get_cpu(cpu_index
);
598 cpu_x86_inject_mce(mon
, cpu
, bank
, status
, mcg_status
, addr
, misc
,
603 static target_long
monitor_get_pc(Monitor
*mon
, const struct MonitorDef
*md
,
606 CPUArchState
*env
= mon_get_cpu_env(mon
);
607 return env
->eip
+ env
->segs
[R_CS
].base
;
610 const MonitorDef monitor_defs
[] = {
611 #define SEG(name, seg) \
612 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
613 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
614 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
616 { "eax", offsetof(CPUX86State
, regs
[0]) },
617 { "ecx", offsetof(CPUX86State
, regs
[1]) },
618 { "edx", offsetof(CPUX86State
, regs
[2]) },
619 { "ebx", offsetof(CPUX86State
, regs
[3]) },
620 { "esp|sp", offsetof(CPUX86State
, regs
[4]) },
621 { "ebp|fp", offsetof(CPUX86State
, regs
[5]) },
622 { "esi", offsetof(CPUX86State
, regs
[6]) },
623 { "edi", offsetof(CPUX86State
, regs
[7]) },
625 { "r8", offsetof(CPUX86State
, regs
[8]) },
626 { "r9", offsetof(CPUX86State
, regs
[9]) },
627 { "r10", offsetof(CPUX86State
, regs
[10]) },
628 { "r11", offsetof(CPUX86State
, regs
[11]) },
629 { "r12", offsetof(CPUX86State
, regs
[12]) },
630 { "r13", offsetof(CPUX86State
, regs
[13]) },
631 { "r14", offsetof(CPUX86State
, regs
[14]) },
632 { "r15", offsetof(CPUX86State
, regs
[15]) },
634 { "eflags", offsetof(CPUX86State
, eflags
) },
635 { "eip", offsetof(CPUX86State
, eip
) },
642 { "pc", 0, monitor_get_pc
, },
646 const MonitorDef
*target_monitor_defs(void)
651 void hmp_info_local_apic(Monitor
*mon
, const QDict
*qdict
)
655 if (qdict_haskey(qdict
, "apic-id")) {
656 int id
= qdict_get_try_int(qdict
, "apic-id", 0);
657 cs
= cpu_by_arch_id(id
);
659 cs
= mon_get_cpu(mon
);
664 monitor_printf(mon
, "No CPU available\n");
667 x86_cpu_dump_local_apic_state(cs
, CPU_DUMP_FPU
);