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
26 #include "hw/pcmcia.h"
31 #include "qemu-char.h"
35 #include "audio/audio.h"
39 #include "qemu-timer.h"
40 #include "migration.h"
44 //#define DEBUG_COMPLETION
50 * 'B' block device name
51 * 's' string (accept optional quote)
53 * 'l' target long (32 or 64 bit)
54 * '/' optional gdb-like print format (like "/10x")
56 * '?' optional type (for 'F', 's' and 'i')
60 typedef struct term_cmd_t
{
62 const char *args_type
;
69 static CharDriverState
*monitor_hd
[MAX_MON
];
70 static int hide_banner
;
72 static const term_cmd_t term_cmds
[];
73 static const term_cmd_t info_cmds
[];
75 static uint8_t term_outbuf
[1024];
76 static int term_outbuf_index
;
78 static void monitor_start_input(void);
79 static void monitor_readline(const char *prompt
, int is_password
,
80 char *buf
, int buf_size
);
82 static CPUState
*mon_cpu
= NULL
;
87 if (term_outbuf_index
> 0) {
88 for (i
= 0; i
< MAX_MON
; i
++)
89 if (monitor_hd
[i
] && monitor_hd
[i
]->focus
== 0)
90 qemu_chr_write(monitor_hd
[i
], term_outbuf
, term_outbuf_index
);
91 term_outbuf_index
= 0;
95 /* flush at every end of line or if the buffer is full */
96 void term_puts(const char *str
)
104 term_outbuf
[term_outbuf_index
++] = '\r';
105 term_outbuf
[term_outbuf_index
++] = c
;
106 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
112 void term_vprintf(const char *fmt
, va_list ap
)
115 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
119 void term_printf(const char *fmt
, ...)
123 term_vprintf(fmt
, ap
);
127 void term_print_filename(const char *filename
)
131 for (i
= 0; filename
[i
]; i
++) {
132 switch (filename
[i
]) {
136 term_printf("\\%c", filename
[i
]);
148 term_printf("%c", filename
[i
]);
154 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
158 term_vprintf(fmt
, ap
);
163 static int compare_cmd(const char *name
, const char *list
)
165 const char *p
, *pstart
;
173 p
= pstart
+ strlen(pstart
);
174 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
183 static void help_cmd1(const term_cmd_t
*cmds
, const char *prefix
, const char *name
)
185 const term_cmd_t
*cmd
;
187 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
188 if (!name
|| !strcmp(name
, cmd
->name
))
189 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
193 static void help_cmd(const char *name
)
195 if (name
&& !strcmp(name
, "info")) {
196 help_cmd1(info_cmds
, "info ", NULL
);
198 help_cmd1(term_cmds
, "", name
);
199 if (name
&& !strcmp(name
, "log")) {
200 const CPULogItem
*item
;
201 term_printf("Log items (comma separated):\n");
202 term_printf("%-10s %s\n", "none", "remove all logs");
203 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
204 term_printf("%-10s %s\n", item
->name
, item
->help
);
210 static void do_help(const char *name
)
215 static void do_commit(const char *device
)
219 all_devices
= !strcmp(device
, "all");
220 for (i
= 0; i
< nb_drives
; i
++) {
222 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
223 bdrv_commit(drives_table
[i
].bdrv
);
227 static void do_info(const char *item
)
229 const term_cmd_t
*cmd
;
230 void (*handler
)(void);
234 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
235 if (compare_cmd(item
, cmd
->name
))
242 handler
= cmd
->handler
;
246 static void do_info_version(void)
248 term_printf("%s\n", QEMU_VERSION
);
251 static void do_info_name(void)
254 term_printf("%s\n", qemu_name
);
257 #if defined(TARGET_I386)
258 static void do_info_hpet(void)
260 term_printf("HPET is %s by QEMU\n", (no_hpet
) ? "disabled" : "enabled");
264 static void do_info_uuid(void)
266 term_printf(UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1], qemu_uuid
[2],
267 qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5], qemu_uuid
[6],
268 qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9], qemu_uuid
[10],
269 qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13], qemu_uuid
[14],
273 static void do_info_block(void)
278 static void do_info_blockstats(void)
283 /* get the current CPU defined by the user */
284 static int mon_set_cpu(int cpu_index
)
288 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
289 if (env
->cpu_index
== cpu_index
) {
297 static CPUState
*mon_get_cpu(void)
305 static void do_info_registers(void)
312 cpu_dump_state(env
, NULL
, monitor_fprintf
,
315 cpu_dump_state(env
, NULL
, monitor_fprintf
,
320 static void do_info_cpus(void)
324 /* just to set the default cpu if not already done */
327 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
328 term_printf("%c CPU #%d:",
329 (env
== mon_cpu
) ? '*' : ' ',
331 #if defined(TARGET_I386)
332 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
333 #elif defined(TARGET_PPC)
334 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
335 #elif defined(TARGET_SPARC)
336 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
337 #elif defined(TARGET_MIPS)
338 term_printf(" PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
341 term_printf(" (halted)");
346 static void do_cpu_set(int index
)
348 if (mon_set_cpu(index
) < 0)
349 term_printf("Invalid CPU index\n");
352 static void do_info_jit(void)
354 dump_exec_info(NULL
, monitor_fprintf
);
357 static void do_info_history (void)
364 str
= readline_get_history(i
);
367 term_printf("%d: '%s'\n", i
, str
);
372 #if defined(TARGET_PPC)
373 /* XXX: not implemented in other targets */
374 static void do_info_cpu_stats (void)
379 cpu_dump_statistics(env
, NULL
, &monitor_fprintf
, 0);
383 static void do_quit(void)
388 static int eject_device(BlockDriverState
*bs
, int force
)
390 if (bdrv_is_inserted(bs
)) {
392 if (!bdrv_is_removable(bs
)) {
393 term_printf("device is not removable\n");
396 if (bdrv_is_locked(bs
)) {
397 term_printf("device is locked\n");
406 static void do_eject(int force
, const char *filename
)
408 BlockDriverState
*bs
;
410 bs
= bdrv_find(filename
);
412 term_printf("device not found\n");
415 eject_device(bs
, force
);
418 static void do_change_block(const char *device
, const char *filename
, const char *fmt
)
420 BlockDriverState
*bs
;
421 BlockDriver
*drv
= NULL
;
423 bs
= bdrv_find(device
);
425 term_printf("device not found\n");
429 drv
= bdrv_find_format(fmt
);
431 term_printf("invalid format %s\n", fmt
);
435 if (eject_device(bs
, 0) < 0)
437 bdrv_open2(bs
, filename
, 0, drv
);
438 monitor_read_bdrv_key(bs
);
441 static void do_change_vnc(const char *target
, const char *arg
)
443 if (strcmp(target
, "passwd") == 0 ||
444 strcmp(target
, "password") == 0) {
447 strncpy(password
, arg
, sizeof(password
));
448 password
[sizeof(password
) - 1] = '\0';
450 monitor_readline("Password: ", 1, password
, sizeof(password
));
451 if (vnc_display_password(NULL
, password
) < 0)
452 term_printf("could not set VNC server password\n");
454 if (vnc_display_open(NULL
, target
) < 0)
455 term_printf("could not start VNC server on %s\n", target
);
459 static void do_change(const char *device
, const char *target
, const char *arg
)
461 if (strcmp(device
, "vnc") == 0) {
462 do_change_vnc(target
, arg
);
464 do_change_block(device
, target
, arg
);
468 static void do_screen_dump(const char *filename
)
470 vga_hw_screen_dump(filename
);
473 static void do_logfile(const char *filename
)
475 cpu_set_log_filename(filename
);
478 static void do_log(const char *items
)
482 if (!strcmp(items
, "none")) {
485 mask
= cpu_str_to_log_mask(items
);
494 static void do_stop(void)
496 vm_stop(EXCP_INTERRUPT
);
499 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
503 if (bdrv_key_required(bs
))
504 *err
= monitor_read_bdrv_key(bs
);
509 static void do_cont(void)
513 bdrv_iterate(encrypted_bdrv_it
, &err
);
514 /* only resume the vm if all keys are set and valid */
519 #ifdef CONFIG_GDBSTUB
520 static void do_gdbserver(const char *port
)
523 port
= DEFAULT_GDBSTUB_PORT
;
524 if (gdbserver_start(port
) < 0) {
525 qemu_printf("Could not open gdbserver socket on port '%s'\n", port
);
527 qemu_printf("Waiting gdb connection on port '%s'\n", port
);
532 static void term_printc(int c
)
549 if (c
>= 32 && c
<= 126) {
550 term_printf("%c", c
);
552 term_printf("\\x%02x", c
);
559 static void memory_dump(int count
, int format
, int wsize
,
560 target_phys_addr_t addr
, int is_physical
)
563 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
571 if (!env
&& !is_physical
)
576 } else if (wsize
== 4) {
579 /* as default we use the current CS size */
583 if ((env
->efer
& MSR_EFER_LMA
) &&
584 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
588 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
593 monitor_disas(env
, addr
, count
, is_physical
, flags
);
602 nb_per_line
= line_size
/ wsize
;
607 max_digits
= (wsize
* 8 + 2) / 3;
611 max_digits
= (wsize
* 8) / 4;
615 max_digits
= (wsize
* 8 * 10 + 32) / 33;
624 term_printf(TARGET_FMT_plx
":", addr
);
626 term_printf(TARGET_FMT_lx
":", (target_ulong
)addr
);
631 cpu_physical_memory_rw(addr
, buf
, l
, 0);
636 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
637 term_printf(" Cannot access memory\n");
646 v
= ldub_raw(buf
+ i
);
649 v
= lduw_raw(buf
+ i
);
652 v
= (uint32_t)ldl_raw(buf
+ i
);
655 v
= ldq_raw(buf
+ i
);
661 term_printf("%#*" PRIo64
, max_digits
, v
);
664 term_printf("0x%0*" PRIx64
, max_digits
, v
);
667 term_printf("%*" PRIu64
, max_digits
, v
);
670 term_printf("%*" PRId64
, max_digits
, v
);
684 #if TARGET_LONG_BITS == 64
685 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
687 #define GET_TLONG(h, l) (l)
690 static void do_memory_dump(int count
, int format
, int size
,
691 uint32_t addrh
, uint32_t addrl
)
693 target_long addr
= GET_TLONG(addrh
, addrl
);
694 memory_dump(count
, format
, size
, addr
, 0);
697 #if TARGET_PHYS_ADDR_BITS > 32
698 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
700 #define GET_TPHYSADDR(h, l) (l)
703 static void do_physical_memory_dump(int count
, int format
, int size
,
704 uint32_t addrh
, uint32_t addrl
)
707 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
708 memory_dump(count
, format
, size
, addr
, 1);
711 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
713 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
714 #if TARGET_PHYS_ADDR_BITS == 32
717 term_printf("%#o", val
);
720 term_printf("%#x", val
);
723 term_printf("%u", val
);
727 term_printf("%d", val
);
736 term_printf("%#" PRIo64
, val
);
739 term_printf("%#" PRIx64
, val
);
742 term_printf("%" PRIu64
, val
);
746 term_printf("%" PRId64
, val
);
756 static void do_memory_save(unsigned int valh
, unsigned int vall
,
757 uint32_t size
, const char *filename
)
760 target_long addr
= GET_TLONG(valh
, vall
);
769 f
= fopen(filename
, "wb");
771 term_printf("could not open '%s'\n", filename
);
778 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
779 fwrite(buf
, 1, l
, f
);
786 static void do_physical_memory_save(unsigned int valh
, unsigned int vall
,
787 uint32_t size
, const char *filename
)
792 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
794 f
= fopen(filename
, "wb");
796 term_printf("could not open '%s'\n", filename
);
803 cpu_physical_memory_rw(addr
, buf
, l
, 0);
804 fwrite(buf
, 1, l
, f
);
812 static void do_sum(uint32_t start
, uint32_t size
)
819 for(addr
= start
; addr
< (start
+ size
); addr
++) {
820 cpu_physical_memory_rw(addr
, buf
, 1, 0);
821 /* BSD sum algorithm ('sum' Unix command) */
822 sum
= (sum
>> 1) | (sum
<< 15);
825 term_printf("%05d\n", sum
);
833 static const KeyDef key_defs
[] = {
860 { 0x0e, "backspace" },
897 { 0x37, "asterisk" },
900 { 0x3a, "caps_lock" },
911 { 0x45, "num_lock" },
912 { 0x46, "scroll_lock" },
914 { 0xb5, "kp_divide" },
915 { 0x37, "kp_multiply" },
916 { 0x4a, "kp_subtract" },
918 { 0x9c, "kp_enter" },
919 { 0x53, "kp_decimal" },
952 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
972 static int get_keycode(const char *key
)
978 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
979 if (!strcmp(key
, p
->name
))
982 if (strstart(key
, "0x", NULL
)) {
983 ret
= strtoul(key
, &endp
, 0);
984 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
990 #define MAX_KEYCODES 16
991 static uint8_t keycodes
[MAX_KEYCODES
];
992 static int nb_pending_keycodes
;
993 static QEMUTimer
*key_timer
;
995 static void release_keys(void *opaque
)
999 while (nb_pending_keycodes
> 0) {
1000 nb_pending_keycodes
--;
1001 keycode
= keycodes
[nb_pending_keycodes
];
1003 kbd_put_keycode(0xe0);
1004 kbd_put_keycode(keycode
| 0x80);
1008 static void do_sendkey(const char *string
, int has_hold_time
, int hold_time
)
1010 char keyname_buf
[16];
1012 int keyname_len
, keycode
, i
;
1014 if (nb_pending_keycodes
> 0) {
1015 qemu_del_timer(key_timer
);
1022 separator
= strchr(string
, '-');
1023 keyname_len
= separator
? separator
- string
: strlen(string
);
1024 if (keyname_len
> 0) {
1025 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1026 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1027 term_printf("invalid key: '%s...'\n", keyname_buf
);
1030 if (i
== MAX_KEYCODES
) {
1031 term_printf("too many keys\n");
1034 keyname_buf
[keyname_len
] = 0;
1035 keycode
= get_keycode(keyname_buf
);
1037 term_printf("unknown key: '%s'\n", keyname_buf
);
1040 keycodes
[i
++] = keycode
;
1044 string
= separator
+ 1;
1046 nb_pending_keycodes
= i
;
1047 /* key down events */
1048 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1049 keycode
= keycodes
[i
];
1051 kbd_put_keycode(0xe0);
1052 kbd_put_keycode(keycode
& 0x7f);
1054 /* delayed key up events */
1055 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1056 muldiv64(ticks_per_sec
, hold_time
, 1000));
1059 static int mouse_button_state
;
1061 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
1065 dx
= strtol(dx_str
, NULL
, 0);
1066 dy
= strtol(dy_str
, NULL
, 0);
1069 dz
= strtol(dz_str
, NULL
, 0);
1070 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1073 static void do_mouse_button(int button_state
)
1075 mouse_button_state
= button_state
;
1076 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1079 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
1085 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1093 val
= cpu_inb(NULL
, addr
);
1097 val
= cpu_inw(NULL
, addr
);
1101 val
= cpu_inl(NULL
, addr
);
1105 term_printf("port%c[0x%04x] = %#0*x\n",
1106 suffix
, addr
, size
* 2, val
);
1109 /* boot_set handler */
1110 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1111 static void *boot_opaque
;
1113 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1115 qemu_boot_set_handler
= func
;
1116 boot_opaque
= opaque
;
1119 static void do_boot_set(const char *bootdevice
)
1123 if (qemu_boot_set_handler
) {
1124 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1126 term_printf("boot device list now set to %s\n", bootdevice
);
1128 term_printf("setting boot device list failed with error %i\n", res
);
1130 term_printf("no function defined to set boot device list for this architecture\n");
1134 static void do_system_reset(void)
1136 qemu_system_reset_request();
1139 static void do_system_powerdown(void)
1141 qemu_system_powerdown_request();
1144 #if defined(TARGET_I386)
1145 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
1147 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1150 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1151 pte
& PG_PSE_MASK
? 'P' : '-',
1152 pte
& PG_DIRTY_MASK
? 'D' : '-',
1153 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1154 pte
& PG_PCD_MASK
? 'C' : '-',
1155 pte
& PG_PWT_MASK
? 'T' : '-',
1156 pte
& PG_USER_MASK
? 'U' : '-',
1157 pte
& PG_RW_MASK
? 'W' : '-');
1160 static void tlb_info(void)
1164 uint32_t pgd
, pde
, pte
;
1166 env
= mon_get_cpu();
1170 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1171 term_printf("PG disabled\n");
1174 pgd
= env
->cr
[3] & ~0xfff;
1175 for(l1
= 0; l1
< 1024; l1
++) {
1176 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1177 pde
= le32_to_cpu(pde
);
1178 if (pde
& PG_PRESENT_MASK
) {
1179 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1180 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1182 for(l2
= 0; l2
< 1024; l2
++) {
1183 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1184 (uint8_t *)&pte
, 4);
1185 pte
= le32_to_cpu(pte
);
1186 if (pte
& PG_PRESENT_MASK
) {
1187 print_pte((l1
<< 22) + (l2
<< 12),
1197 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1198 uint32_t end
, int prot
)
1201 prot1
= *plast_prot
;
1202 if (prot
!= prot1
) {
1203 if (*pstart
!= -1) {
1204 term_printf("%08x-%08x %08x %c%c%c\n",
1205 *pstart
, end
, end
- *pstart
,
1206 prot1
& PG_USER_MASK
? 'u' : '-',
1208 prot1
& PG_RW_MASK
? 'w' : '-');
1218 static void mem_info(void)
1221 int l1
, l2
, prot
, last_prot
;
1222 uint32_t pgd
, pde
, pte
, start
, end
;
1224 env
= mon_get_cpu();
1228 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1229 term_printf("PG disabled\n");
1232 pgd
= env
->cr
[3] & ~0xfff;
1235 for(l1
= 0; l1
< 1024; l1
++) {
1236 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1237 pde
= le32_to_cpu(pde
);
1239 if (pde
& PG_PRESENT_MASK
) {
1240 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1241 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1242 mem_print(&start
, &last_prot
, end
, prot
);
1244 for(l2
= 0; l2
< 1024; l2
++) {
1245 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1246 (uint8_t *)&pte
, 4);
1247 pte
= le32_to_cpu(pte
);
1248 end
= (l1
<< 22) + (l2
<< 12);
1249 if (pte
& PG_PRESENT_MASK
) {
1250 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1254 mem_print(&start
, &last_prot
, end
, prot
);
1259 mem_print(&start
, &last_prot
, end
, prot
);
1265 #if defined(TARGET_SH4)
1267 static void print_tlb(int idx
, tlb_t
*tlb
)
1269 term_printf(" tlb%i:\t"
1270 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1271 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1272 "dirty=%hhu writethrough=%hhu\n",
1274 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1275 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1279 static void tlb_info(void)
1281 CPUState
*env
= mon_get_cpu();
1284 term_printf ("ITLB:\n");
1285 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1286 print_tlb (i
, &env
->itlb
[i
]);
1287 term_printf ("UTLB:\n");
1288 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1289 print_tlb (i
, &env
->utlb
[i
]);
1294 static void do_info_kqemu(void)
1300 env
= mon_get_cpu();
1302 term_printf("No cpu initialized yet");
1305 val
= env
->kqemu_enabled
;
1306 term_printf("kqemu support: ");
1310 term_printf("disabled\n");
1313 term_printf("enabled for user code\n");
1316 term_printf("enabled for user and kernel code\n");
1320 term_printf("kqemu support: not compiled\n");
1324 static void do_info_kvm(void)
1327 term_printf("kvm support: ");
1329 term_printf("enabled\n");
1331 term_printf("disabled\n");
1333 term_printf("kvm support: not compiled\n");
1337 #ifdef CONFIG_PROFILER
1341 int64_t kqemu_exec_count
;
1343 int64_t kqemu_ret_int_count
;
1344 int64_t kqemu_ret_excp_count
;
1345 int64_t kqemu_ret_intr_count
;
1347 static void do_info_profile(void)
1353 term_printf("async time %" PRId64
" (%0.3f)\n",
1354 dev_time
, dev_time
/ (double)ticks_per_sec
);
1355 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1356 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1357 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1358 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1359 kqemu_time
/ (double)total
* 100.0,
1361 kqemu_ret_int_count
,
1362 kqemu_ret_excp_count
,
1363 kqemu_ret_intr_count
);
1366 kqemu_exec_count
= 0;
1368 kqemu_ret_int_count
= 0;
1369 kqemu_ret_excp_count
= 0;
1370 kqemu_ret_intr_count
= 0;
1372 kqemu_record_dump();
1376 static void do_info_profile(void)
1378 term_printf("Internal profiler not compiled\n");
1382 /* Capture support */
1383 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1385 static void do_info_capture (void)
1390 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1391 term_printf ("[%d]: ", i
);
1392 s
->ops
.info (s
->opaque
);
1396 static void do_stop_capture (int n
)
1401 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1403 s
->ops
.destroy (s
->opaque
);
1404 LIST_REMOVE (s
, entries
);
1412 static void do_wav_capture (const char *path
,
1413 int has_freq
, int freq
,
1414 int has_bits
, int bits
,
1415 int has_channels
, int nchannels
)
1419 s
= qemu_mallocz (sizeof (*s
));
1421 freq
= has_freq
? freq
: 44100;
1422 bits
= has_bits
? bits
: 16;
1423 nchannels
= has_channels
? nchannels
: 2;
1425 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1426 term_printf ("Faied to add wave capture\n");
1429 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1433 #if defined(TARGET_I386)
1434 static void do_inject_nmi(int cpu_index
)
1438 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1439 if (env
->cpu_index
== cpu_index
) {
1440 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1446 static void do_info_status(void)
1449 term_printf("VM status: running\n");
1451 term_printf("VM status: paused\n");
1455 static void do_balloon(int value
)
1457 ram_addr_t target
= value
;
1458 qemu_balloon(target
<< 20);
1461 static void do_info_balloon(void)
1465 actual
= qemu_balloon_status();
1466 if (kvm_enabled() && !kvm_has_sync_mmu())
1467 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1468 else if (actual
== 0)
1469 term_printf("Ballooning not activated in VM\n");
1471 term_printf("balloon: actual=%d\n", (int)(actual
>> 20));
1474 /* Please update qemu-doc.texi when adding or changing commands */
1475 static const term_cmd_t term_cmds
[] = {
1476 { "help|?", "s?", do_help
,
1477 "[cmd]", "show the help" },
1478 { "commit", "s", do_commit
,
1479 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1480 { "info", "s?", do_info
,
1481 "subcommand", "show various information about the system state" },
1482 { "q|quit", "", do_quit
,
1483 "", "quit the emulator" },
1484 { "eject", "-fB", do_eject
,
1485 "[-f] device", "eject a removable medium (use -f to force it)" },
1486 { "change", "BFs?", do_change
,
1487 "device filename [format]", "change a removable medium, optional format" },
1488 { "screendump", "F", do_screen_dump
,
1489 "filename", "save screen into PPM image 'filename'" },
1490 { "logfile", "F", do_logfile
,
1491 "filename", "output logs to 'filename'" },
1492 { "log", "s", do_log
,
1493 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1494 { "savevm", "s?", do_savevm
,
1495 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1496 { "loadvm", "s", do_loadvm
,
1497 "tag|id", "restore a VM snapshot from its tag or id" },
1498 { "delvm", "s", do_delvm
,
1499 "tag|id", "delete a VM snapshot from its tag or id" },
1500 { "stop", "", do_stop
,
1501 "", "stop emulation", },
1502 { "c|cont", "", do_cont
,
1503 "", "resume emulation", },
1504 #ifdef CONFIG_GDBSTUB
1505 { "gdbserver", "s?", do_gdbserver
,
1506 "[port]", "start gdbserver session (default port=1234)", },
1508 { "x", "/l", do_memory_dump
,
1509 "/fmt addr", "virtual memory dump starting at 'addr'", },
1510 { "xp", "/l", do_physical_memory_dump
,
1511 "/fmt addr", "physical memory dump starting at 'addr'", },
1512 { "p|print", "/l", do_print
,
1513 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1514 { "i", "/ii.", do_ioport_read
,
1515 "/fmt addr", "I/O port read" },
1517 { "sendkey", "si?", do_sendkey
,
1518 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1519 { "system_reset", "", do_system_reset
,
1520 "", "reset the system" },
1521 { "system_powerdown", "", do_system_powerdown
,
1522 "", "send system power down event" },
1523 { "sum", "ii", do_sum
,
1524 "addr size", "compute the checksum of a memory region" },
1525 { "usb_add", "s", do_usb_add
,
1526 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1527 { "usb_del", "s", do_usb_del
,
1528 "device", "remove USB device 'bus.addr'" },
1529 { "cpu", "i", do_cpu_set
,
1530 "index", "set the default CPU" },
1531 { "mouse_move", "sss?", do_mouse_move
,
1532 "dx dy [dz]", "send mouse move events" },
1533 { "mouse_button", "i", do_mouse_button
,
1534 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1535 { "mouse_set", "i", do_mouse_set
,
1536 "index", "set which mouse device receives events" },
1538 { "wavcapture", "si?i?i?", do_wav_capture
,
1539 "path [frequency bits channels]",
1540 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1542 { "stopcapture", "i", do_stop_capture
,
1543 "capture index", "stop capture" },
1544 { "memsave", "lis", do_memory_save
,
1545 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1546 { "pmemsave", "lis", do_physical_memory_save
,
1547 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1548 { "boot_set", "s", do_boot_set
,
1549 "bootdevice", "define new values for the boot device list" },
1550 #if defined(TARGET_I386)
1551 { "nmi", "i", do_inject_nmi
,
1552 "cpu", "inject an NMI on the given CPU", },
1554 { "migrate", "-ds", do_migrate
,
1555 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1556 { "migrate_cancel", "", do_migrate_cancel
,
1557 "", "cancel the current VM migration" },
1558 { "migrate_set_speed", "s", do_migrate_set_speed
,
1559 "value", "set maximum speed (in bytes) for migrations" },
1560 #if defined(TARGET_I386)
1561 { "drive_add", "ss", drive_hot_add
, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1562 "[file=file][,if=type][,bus=n]\n"
1563 "[,unit=m][,media=d][index=i]\n"
1564 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1565 "[snapshot=on|off][,cache=on|off]",
1566 "add drive to PCI storage controller" },
1567 { "pci_add", "sss", pci_device_hot_add
, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1568 { "pci_del", "s", pci_device_hot_remove
, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1569 { "host_net_add", "ss", net_host_device_add
,
1570 "[tap,user,socket,vde] options", "add host VLAN client" },
1571 { "host_net_remove", "is", net_host_device_remove
,
1572 "vlan_id name", "remove host VLAN client" },
1574 { "balloon", "i", do_balloon
,
1575 "target", "request VM to change it's memory allocation (in MB)" },
1576 { "set_link", "ss", do_set_link
,
1577 "name [up|down]", "change the link status of a network adapter" },
1581 /* Please update qemu-doc.texi when adding or changing commands */
1582 static const term_cmd_t info_cmds
[] = {
1583 { "version", "", do_info_version
,
1584 "", "show the version of QEMU" },
1585 { "network", "", do_info_network
,
1586 "", "show the network state" },
1587 { "chardev", "", qemu_chr_info
,
1588 "", "show the character devices" },
1589 { "block", "", do_info_block
,
1590 "", "show the block devices" },
1591 { "blockstats", "", do_info_blockstats
,
1592 "", "show block device statistics" },
1593 { "registers", "", do_info_registers
,
1594 "", "show the cpu registers" },
1595 { "cpus", "", do_info_cpus
,
1596 "", "show infos for each CPU" },
1597 { "history", "", do_info_history
,
1598 "", "show the command line history", },
1599 { "irq", "", irq_info
,
1600 "", "show the interrupts statistics (if available)", },
1601 { "pic", "", pic_info
,
1602 "", "show i8259 (PIC) state", },
1603 { "pci", "", pci_info
,
1604 "", "show PCI info", },
1605 #if defined(TARGET_I386) || defined(TARGET_SH4)
1606 { "tlb", "", tlb_info
,
1607 "", "show virtual to physical memory mappings", },
1609 #if defined(TARGET_I386)
1610 { "mem", "", mem_info
,
1611 "", "show the active virtual memory mappings", },
1612 { "hpet", "", do_info_hpet
,
1613 "", "show state of HPET", },
1615 { "jit", "", do_info_jit
,
1616 "", "show dynamic compiler info", },
1617 { "kqemu", "", do_info_kqemu
,
1618 "", "show KQEMU information", },
1619 { "kvm", "", do_info_kvm
,
1620 "", "show KVM information", },
1621 { "usb", "", usb_info
,
1622 "", "show guest USB devices", },
1623 { "usbhost", "", usb_host_info
,
1624 "", "show host USB devices", },
1625 { "profile", "", do_info_profile
,
1626 "", "show profiling information", },
1627 { "capture", "", do_info_capture
,
1628 "", "show capture information" },
1629 { "snapshots", "", do_info_snapshots
,
1630 "", "show the currently saved VM snapshots" },
1631 { "status", "", do_info_status
,
1632 "", "show the current VM status (running|paused)" },
1633 { "pcmcia", "", pcmcia_info
,
1634 "", "show guest PCMCIA status" },
1635 { "mice", "", do_info_mice
,
1636 "", "show which guest mouse is receiving events" },
1637 { "vnc", "", do_info_vnc
,
1638 "", "show the vnc server status"},
1639 { "name", "", do_info_name
,
1640 "", "show the current VM name" },
1641 { "uuid", "", do_info_uuid
,
1642 "", "show the current VM UUID" },
1643 #if defined(TARGET_PPC)
1644 { "cpustats", "", do_info_cpu_stats
,
1645 "", "show CPU statistics", },
1647 #if defined(CONFIG_SLIRP)
1648 { "slirp", "", do_info_slirp
,
1649 "", "show SLIRP statistics", },
1651 { "migrate", "", do_info_migrate
, "", "show migration status" },
1652 { "balloon", "", do_info_balloon
,
1653 "", "show balloon information" },
1657 /*******************************************************************/
1659 static const char *pch
;
1660 static jmp_buf expr_env
;
1665 typedef struct MonitorDef
{
1668 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1672 #if defined(TARGET_I386)
1673 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1675 CPUState
*env
= mon_get_cpu();
1678 return env
->eip
+ env
->segs
[R_CS
].base
;
1682 #if defined(TARGET_PPC)
1683 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1685 CPUState
*env
= mon_get_cpu();
1693 for (i
= 0; i
< 8; i
++)
1694 u
|= env
->crf
[i
] << (32 - (4 * i
));
1699 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1701 CPUState
*env
= mon_get_cpu();
1707 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1709 CPUState
*env
= mon_get_cpu();
1715 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1717 CPUState
*env
= mon_get_cpu();
1720 return cpu_ppc_load_decr(env
);
1723 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1725 CPUState
*env
= mon_get_cpu();
1728 return cpu_ppc_load_tbu(env
);
1731 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1733 CPUState
*env
= mon_get_cpu();
1736 return cpu_ppc_load_tbl(env
);
1740 #if defined(TARGET_SPARC)
1741 #ifndef TARGET_SPARC64
1742 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1744 CPUState
*env
= mon_get_cpu();
1747 return GET_PSR(env
);
1751 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1753 CPUState
*env
= mon_get_cpu();
1756 return env
->regwptr
[val
];
1760 static const MonitorDef monitor_defs
[] = {
1763 #define SEG(name, seg) \
1764 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1765 { name ".base", offsetof(CPUState, segs[seg].base) },\
1766 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1768 { "eax", offsetof(CPUState
, regs
[0]) },
1769 { "ecx", offsetof(CPUState
, regs
[1]) },
1770 { "edx", offsetof(CPUState
, regs
[2]) },
1771 { "ebx", offsetof(CPUState
, regs
[3]) },
1772 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1773 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1774 { "esi", offsetof(CPUState
, regs
[6]) },
1775 { "edi", offsetof(CPUState
, regs
[7]) },
1776 #ifdef TARGET_X86_64
1777 { "r8", offsetof(CPUState
, regs
[8]) },
1778 { "r9", offsetof(CPUState
, regs
[9]) },
1779 { "r10", offsetof(CPUState
, regs
[10]) },
1780 { "r11", offsetof(CPUState
, regs
[11]) },
1781 { "r12", offsetof(CPUState
, regs
[12]) },
1782 { "r13", offsetof(CPUState
, regs
[13]) },
1783 { "r14", offsetof(CPUState
, regs
[14]) },
1784 { "r15", offsetof(CPUState
, regs
[15]) },
1786 { "eflags", offsetof(CPUState
, eflags
) },
1787 { "eip", offsetof(CPUState
, eip
) },
1794 { "pc", 0, monitor_get_pc
, },
1795 #elif defined(TARGET_PPC)
1796 /* General purpose registers */
1797 { "r0", offsetof(CPUState
, gpr
[0]) },
1798 { "r1", offsetof(CPUState
, gpr
[1]) },
1799 { "r2", offsetof(CPUState
, gpr
[2]) },
1800 { "r3", offsetof(CPUState
, gpr
[3]) },
1801 { "r4", offsetof(CPUState
, gpr
[4]) },
1802 { "r5", offsetof(CPUState
, gpr
[5]) },
1803 { "r6", offsetof(CPUState
, gpr
[6]) },
1804 { "r7", offsetof(CPUState
, gpr
[7]) },
1805 { "r8", offsetof(CPUState
, gpr
[8]) },
1806 { "r9", offsetof(CPUState
, gpr
[9]) },
1807 { "r10", offsetof(CPUState
, gpr
[10]) },
1808 { "r11", offsetof(CPUState
, gpr
[11]) },
1809 { "r12", offsetof(CPUState
, gpr
[12]) },
1810 { "r13", offsetof(CPUState
, gpr
[13]) },
1811 { "r14", offsetof(CPUState
, gpr
[14]) },
1812 { "r15", offsetof(CPUState
, gpr
[15]) },
1813 { "r16", offsetof(CPUState
, gpr
[16]) },
1814 { "r17", offsetof(CPUState
, gpr
[17]) },
1815 { "r18", offsetof(CPUState
, gpr
[18]) },
1816 { "r19", offsetof(CPUState
, gpr
[19]) },
1817 { "r20", offsetof(CPUState
, gpr
[20]) },
1818 { "r21", offsetof(CPUState
, gpr
[21]) },
1819 { "r22", offsetof(CPUState
, gpr
[22]) },
1820 { "r23", offsetof(CPUState
, gpr
[23]) },
1821 { "r24", offsetof(CPUState
, gpr
[24]) },
1822 { "r25", offsetof(CPUState
, gpr
[25]) },
1823 { "r26", offsetof(CPUState
, gpr
[26]) },
1824 { "r27", offsetof(CPUState
, gpr
[27]) },
1825 { "r28", offsetof(CPUState
, gpr
[28]) },
1826 { "r29", offsetof(CPUState
, gpr
[29]) },
1827 { "r30", offsetof(CPUState
, gpr
[30]) },
1828 { "r31", offsetof(CPUState
, gpr
[31]) },
1829 /* Floating point registers */
1830 { "f0", offsetof(CPUState
, fpr
[0]) },
1831 { "f1", offsetof(CPUState
, fpr
[1]) },
1832 { "f2", offsetof(CPUState
, fpr
[2]) },
1833 { "f3", offsetof(CPUState
, fpr
[3]) },
1834 { "f4", offsetof(CPUState
, fpr
[4]) },
1835 { "f5", offsetof(CPUState
, fpr
[5]) },
1836 { "f6", offsetof(CPUState
, fpr
[6]) },
1837 { "f7", offsetof(CPUState
, fpr
[7]) },
1838 { "f8", offsetof(CPUState
, fpr
[8]) },
1839 { "f9", offsetof(CPUState
, fpr
[9]) },
1840 { "f10", offsetof(CPUState
, fpr
[10]) },
1841 { "f11", offsetof(CPUState
, fpr
[11]) },
1842 { "f12", offsetof(CPUState
, fpr
[12]) },
1843 { "f13", offsetof(CPUState
, fpr
[13]) },
1844 { "f14", offsetof(CPUState
, fpr
[14]) },
1845 { "f15", offsetof(CPUState
, fpr
[15]) },
1846 { "f16", offsetof(CPUState
, fpr
[16]) },
1847 { "f17", offsetof(CPUState
, fpr
[17]) },
1848 { "f18", offsetof(CPUState
, fpr
[18]) },
1849 { "f19", offsetof(CPUState
, fpr
[19]) },
1850 { "f20", offsetof(CPUState
, fpr
[20]) },
1851 { "f21", offsetof(CPUState
, fpr
[21]) },
1852 { "f22", offsetof(CPUState
, fpr
[22]) },
1853 { "f23", offsetof(CPUState
, fpr
[23]) },
1854 { "f24", offsetof(CPUState
, fpr
[24]) },
1855 { "f25", offsetof(CPUState
, fpr
[25]) },
1856 { "f26", offsetof(CPUState
, fpr
[26]) },
1857 { "f27", offsetof(CPUState
, fpr
[27]) },
1858 { "f28", offsetof(CPUState
, fpr
[28]) },
1859 { "f29", offsetof(CPUState
, fpr
[29]) },
1860 { "f30", offsetof(CPUState
, fpr
[30]) },
1861 { "f31", offsetof(CPUState
, fpr
[31]) },
1862 { "fpscr", offsetof(CPUState
, fpscr
) },
1863 /* Next instruction pointer */
1864 { "nip|pc", offsetof(CPUState
, nip
) },
1865 { "lr", offsetof(CPUState
, lr
) },
1866 { "ctr", offsetof(CPUState
, ctr
) },
1867 { "decr", 0, &monitor_get_decr
, },
1868 { "ccr", 0, &monitor_get_ccr
, },
1869 /* Machine state register */
1870 { "msr", 0, &monitor_get_msr
, },
1871 { "xer", 0, &monitor_get_xer
, },
1872 { "tbu", 0, &monitor_get_tbu
, },
1873 { "tbl", 0, &monitor_get_tbl
, },
1874 #if defined(TARGET_PPC64)
1875 /* Address space register */
1876 { "asr", offsetof(CPUState
, asr
) },
1878 /* Segment registers */
1879 { "sdr1", offsetof(CPUState
, sdr1
) },
1880 { "sr0", offsetof(CPUState
, sr
[0]) },
1881 { "sr1", offsetof(CPUState
, sr
[1]) },
1882 { "sr2", offsetof(CPUState
, sr
[2]) },
1883 { "sr3", offsetof(CPUState
, sr
[3]) },
1884 { "sr4", offsetof(CPUState
, sr
[4]) },
1885 { "sr5", offsetof(CPUState
, sr
[5]) },
1886 { "sr6", offsetof(CPUState
, sr
[6]) },
1887 { "sr7", offsetof(CPUState
, sr
[7]) },
1888 { "sr8", offsetof(CPUState
, sr
[8]) },
1889 { "sr9", offsetof(CPUState
, sr
[9]) },
1890 { "sr10", offsetof(CPUState
, sr
[10]) },
1891 { "sr11", offsetof(CPUState
, sr
[11]) },
1892 { "sr12", offsetof(CPUState
, sr
[12]) },
1893 { "sr13", offsetof(CPUState
, sr
[13]) },
1894 { "sr14", offsetof(CPUState
, sr
[14]) },
1895 { "sr15", offsetof(CPUState
, sr
[15]) },
1896 /* Too lazy to put BATs and SPRs ... */
1897 #elif defined(TARGET_SPARC)
1898 { "g0", offsetof(CPUState
, gregs
[0]) },
1899 { "g1", offsetof(CPUState
, gregs
[1]) },
1900 { "g2", offsetof(CPUState
, gregs
[2]) },
1901 { "g3", offsetof(CPUState
, gregs
[3]) },
1902 { "g4", offsetof(CPUState
, gregs
[4]) },
1903 { "g5", offsetof(CPUState
, gregs
[5]) },
1904 { "g6", offsetof(CPUState
, gregs
[6]) },
1905 { "g7", offsetof(CPUState
, gregs
[7]) },
1906 { "o0", 0, monitor_get_reg
},
1907 { "o1", 1, monitor_get_reg
},
1908 { "o2", 2, monitor_get_reg
},
1909 { "o3", 3, monitor_get_reg
},
1910 { "o4", 4, monitor_get_reg
},
1911 { "o5", 5, monitor_get_reg
},
1912 { "o6", 6, monitor_get_reg
},
1913 { "o7", 7, monitor_get_reg
},
1914 { "l0", 8, monitor_get_reg
},
1915 { "l1", 9, monitor_get_reg
},
1916 { "l2", 10, monitor_get_reg
},
1917 { "l3", 11, monitor_get_reg
},
1918 { "l4", 12, monitor_get_reg
},
1919 { "l5", 13, monitor_get_reg
},
1920 { "l6", 14, monitor_get_reg
},
1921 { "l7", 15, monitor_get_reg
},
1922 { "i0", 16, monitor_get_reg
},
1923 { "i1", 17, monitor_get_reg
},
1924 { "i2", 18, monitor_get_reg
},
1925 { "i3", 19, monitor_get_reg
},
1926 { "i4", 20, monitor_get_reg
},
1927 { "i5", 21, monitor_get_reg
},
1928 { "i6", 22, monitor_get_reg
},
1929 { "i7", 23, monitor_get_reg
},
1930 { "pc", offsetof(CPUState
, pc
) },
1931 { "npc", offsetof(CPUState
, npc
) },
1932 { "y", offsetof(CPUState
, y
) },
1933 #ifndef TARGET_SPARC64
1934 { "psr", 0, &monitor_get_psr
, },
1935 { "wim", offsetof(CPUState
, wim
) },
1937 { "tbr", offsetof(CPUState
, tbr
) },
1938 { "fsr", offsetof(CPUState
, fsr
) },
1939 { "f0", offsetof(CPUState
, fpr
[0]) },
1940 { "f1", offsetof(CPUState
, fpr
[1]) },
1941 { "f2", offsetof(CPUState
, fpr
[2]) },
1942 { "f3", offsetof(CPUState
, fpr
[3]) },
1943 { "f4", offsetof(CPUState
, fpr
[4]) },
1944 { "f5", offsetof(CPUState
, fpr
[5]) },
1945 { "f6", offsetof(CPUState
, fpr
[6]) },
1946 { "f7", offsetof(CPUState
, fpr
[7]) },
1947 { "f8", offsetof(CPUState
, fpr
[8]) },
1948 { "f9", offsetof(CPUState
, fpr
[9]) },
1949 { "f10", offsetof(CPUState
, fpr
[10]) },
1950 { "f11", offsetof(CPUState
, fpr
[11]) },
1951 { "f12", offsetof(CPUState
, fpr
[12]) },
1952 { "f13", offsetof(CPUState
, fpr
[13]) },
1953 { "f14", offsetof(CPUState
, fpr
[14]) },
1954 { "f15", offsetof(CPUState
, fpr
[15]) },
1955 { "f16", offsetof(CPUState
, fpr
[16]) },
1956 { "f17", offsetof(CPUState
, fpr
[17]) },
1957 { "f18", offsetof(CPUState
, fpr
[18]) },
1958 { "f19", offsetof(CPUState
, fpr
[19]) },
1959 { "f20", offsetof(CPUState
, fpr
[20]) },
1960 { "f21", offsetof(CPUState
, fpr
[21]) },
1961 { "f22", offsetof(CPUState
, fpr
[22]) },
1962 { "f23", offsetof(CPUState
, fpr
[23]) },
1963 { "f24", offsetof(CPUState
, fpr
[24]) },
1964 { "f25", offsetof(CPUState
, fpr
[25]) },
1965 { "f26", offsetof(CPUState
, fpr
[26]) },
1966 { "f27", offsetof(CPUState
, fpr
[27]) },
1967 { "f28", offsetof(CPUState
, fpr
[28]) },
1968 { "f29", offsetof(CPUState
, fpr
[29]) },
1969 { "f30", offsetof(CPUState
, fpr
[30]) },
1970 { "f31", offsetof(CPUState
, fpr
[31]) },
1971 #ifdef TARGET_SPARC64
1972 { "f32", offsetof(CPUState
, fpr
[32]) },
1973 { "f34", offsetof(CPUState
, fpr
[34]) },
1974 { "f36", offsetof(CPUState
, fpr
[36]) },
1975 { "f38", offsetof(CPUState
, fpr
[38]) },
1976 { "f40", offsetof(CPUState
, fpr
[40]) },
1977 { "f42", offsetof(CPUState
, fpr
[42]) },
1978 { "f44", offsetof(CPUState
, fpr
[44]) },
1979 { "f46", offsetof(CPUState
, fpr
[46]) },
1980 { "f48", offsetof(CPUState
, fpr
[48]) },
1981 { "f50", offsetof(CPUState
, fpr
[50]) },
1982 { "f52", offsetof(CPUState
, fpr
[52]) },
1983 { "f54", offsetof(CPUState
, fpr
[54]) },
1984 { "f56", offsetof(CPUState
, fpr
[56]) },
1985 { "f58", offsetof(CPUState
, fpr
[58]) },
1986 { "f60", offsetof(CPUState
, fpr
[60]) },
1987 { "f62", offsetof(CPUState
, fpr
[62]) },
1988 { "asi", offsetof(CPUState
, asi
) },
1989 { "pstate", offsetof(CPUState
, pstate
) },
1990 { "cansave", offsetof(CPUState
, cansave
) },
1991 { "canrestore", offsetof(CPUState
, canrestore
) },
1992 { "otherwin", offsetof(CPUState
, otherwin
) },
1993 { "wstate", offsetof(CPUState
, wstate
) },
1994 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1995 { "fprs", offsetof(CPUState
, fprs
) },
2001 static void expr_error(const char *msg
)
2003 term_printf("%s\n", msg
);
2004 longjmp(expr_env
, 1);
2007 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2008 static int get_monitor_def(target_long
*pval
, const char *name
)
2010 const MonitorDef
*md
;
2013 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2014 if (compare_cmd(name
, md
->name
)) {
2015 if (md
->get_value
) {
2016 *pval
= md
->get_value(md
, md
->offset
);
2018 CPUState
*env
= mon_get_cpu();
2021 ptr
= (uint8_t *)env
+ md
->offset
;
2024 *pval
= *(int32_t *)ptr
;
2027 *pval
= *(target_long
*)ptr
;
2040 static void next(void)
2044 while (qemu_isspace(*pch
))
2049 static int64_t expr_sum(void);
2051 static int64_t expr_unary(void)
2074 expr_error("')' expected");
2081 expr_error("character constant expected");
2085 expr_error("missing terminating \' character");
2095 while ((*pch
>= 'a' && *pch
<= 'z') ||
2096 (*pch
>= 'A' && *pch
<= 'Z') ||
2097 (*pch
>= '0' && *pch
<= '9') ||
2098 *pch
== '_' || *pch
== '.') {
2099 if ((q
- buf
) < sizeof(buf
) - 1)
2103 while (qemu_isspace(*pch
))
2106 ret
= get_monitor_def(®
, buf
);
2108 expr_error("unknown register");
2110 expr_error("no cpu defined");
2115 expr_error("unexpected end of expression");
2119 #if TARGET_PHYS_ADDR_BITS > 32
2120 n
= strtoull(pch
, &p
, 0);
2122 n
= strtoul(pch
, &p
, 0);
2125 expr_error("invalid char in expression");
2128 while (qemu_isspace(*pch
))
2136 static int64_t expr_prod(void)
2144 if (op
!= '*' && op
!= '/' && op
!= '%')
2147 val2
= expr_unary();
2156 expr_error("division by zero");
2167 static int64_t expr_logic(void)
2175 if (op
!= '&' && op
!= '|' && op
!= '^')
2195 static int64_t expr_sum(void)
2203 if (op
!= '+' && op
!= '-')
2206 val2
= expr_logic();
2215 static int get_expr(int64_t *pval
, const char **pp
)
2218 if (setjmp(expr_env
)) {
2222 while (qemu_isspace(*pch
))
2229 static int get_str(char *buf
, int buf_size
, const char **pp
)
2237 while (qemu_isspace(*p
))
2247 while (*p
!= '\0' && *p
!= '\"') {
2263 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2266 if ((q
- buf
) < buf_size
- 1) {
2270 if ((q
- buf
) < buf_size
- 1) {
2277 qemu_printf("unterminated string\n");
2282 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2283 if ((q
- buf
) < buf_size
- 1) {
2294 static int default_fmt_format
= 'x';
2295 static int default_fmt_size
= 4;
2299 static void monitor_handle_command(const char *cmdline
)
2301 const char *p
, *pstart
, *typestr
;
2303 int c
, nb_args
, len
, i
, has_arg
;
2304 const term_cmd_t
*cmd
;
2307 void *str_allocated
[MAX_ARGS
];
2308 void *args
[MAX_ARGS
];
2309 void (*handler_0
)(void);
2310 void (*handler_1
)(void *arg0
);
2311 void (*handler_2
)(void *arg0
, void *arg1
);
2312 void (*handler_3
)(void *arg0
, void *arg1
, void *arg2
);
2313 void (*handler_4
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
);
2314 void (*handler_5
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2316 void (*handler_6
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2317 void *arg4
, void *arg5
);
2318 void (*handler_7
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2319 void *arg4
, void *arg5
, void *arg6
);
2322 term_printf("command='%s'\n", cmdline
);
2325 /* extract the command name */
2328 while (qemu_isspace(*p
))
2333 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2336 if (len
> sizeof(cmdname
) - 1)
2337 len
= sizeof(cmdname
) - 1;
2338 memcpy(cmdname
, pstart
, len
);
2339 cmdname
[len
] = '\0';
2341 /* find the command */
2342 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2343 if (compare_cmd(cmdname
, cmd
->name
))
2346 term_printf("unknown command: '%s'\n", cmdname
);
2350 for(i
= 0; i
< MAX_ARGS
; i
++)
2351 str_allocated
[i
] = NULL
;
2353 /* parse the parameters */
2354 typestr
= cmd
->args_type
;
2369 while (qemu_isspace(*p
))
2371 if (*typestr
== '?') {
2374 /* no optional string: NULL argument */
2379 ret
= get_str(buf
, sizeof(buf
), &p
);
2383 term_printf("%s: filename expected\n", cmdname
);
2386 term_printf("%s: block device name expected\n", cmdname
);
2389 term_printf("%s: string expected\n", cmdname
);
2394 str
= qemu_malloc(strlen(buf
) + 1);
2395 pstrcpy(str
, sizeof(buf
), buf
);
2396 str_allocated
[nb_args
] = str
;
2398 if (nb_args
>= MAX_ARGS
) {
2400 term_printf("%s: too many arguments\n", cmdname
);
2403 args
[nb_args
++] = str
;
2408 int count
, format
, size
;
2410 while (qemu_isspace(*p
))
2416 if (qemu_isdigit(*p
)) {
2418 while (qemu_isdigit(*p
)) {
2419 count
= count
* 10 + (*p
- '0');
2457 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2458 term_printf("invalid char in format: '%c'\n", *p
);
2462 format
= default_fmt_format
;
2463 if (format
!= 'i') {
2464 /* for 'i', not specifying a size gives -1 as size */
2466 size
= default_fmt_size
;
2467 default_fmt_size
= size
;
2469 default_fmt_format
= format
;
2472 format
= default_fmt_format
;
2473 if (format
!= 'i') {
2474 size
= default_fmt_size
;
2479 if (nb_args
+ 3 > MAX_ARGS
)
2481 args
[nb_args
++] = (void*)(long)count
;
2482 args
[nb_args
++] = (void*)(long)format
;
2483 args
[nb_args
++] = (void*)(long)size
;
2491 while (qemu_isspace(*p
))
2493 if (*typestr
== '?' || *typestr
== '.') {
2494 if (*typestr
== '?') {
2502 while (qemu_isspace(*p
))
2510 if (nb_args
>= MAX_ARGS
)
2512 args
[nb_args
++] = (void *)(long)has_arg
;
2514 if (nb_args
>= MAX_ARGS
)
2520 if (get_expr(&val
, &p
))
2524 if (nb_args
>= MAX_ARGS
)
2526 args
[nb_args
++] = (void *)(long)val
;
2528 if ((nb_args
+ 1) >= MAX_ARGS
)
2530 #if TARGET_PHYS_ADDR_BITS > 32
2531 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2533 args
[nb_args
++] = (void *)0;
2535 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2547 while (qemu_isspace(*p
))
2553 term_printf("%s: unsupported option -%c\n",
2560 if (nb_args
>= MAX_ARGS
)
2562 args
[nb_args
++] = (void *)(long)has_option
;
2567 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2571 /* check that all arguments were parsed */
2572 while (qemu_isspace(*p
))
2575 term_printf("%s: extraneous characters at the end of line\n",
2582 handler_0
= cmd
->handler
;
2586 handler_1
= cmd
->handler
;
2590 handler_2
= cmd
->handler
;
2591 handler_2(args
[0], args
[1]);
2594 handler_3
= cmd
->handler
;
2595 handler_3(args
[0], args
[1], args
[2]);
2598 handler_4
= cmd
->handler
;
2599 handler_4(args
[0], args
[1], args
[2], args
[3]);
2602 handler_5
= cmd
->handler
;
2603 handler_5(args
[0], args
[1], args
[2], args
[3], args
[4]);
2606 handler_6
= cmd
->handler
;
2607 handler_6(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2610 handler_7
= cmd
->handler
;
2611 handler_7(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2614 term_printf("unsupported number of arguments: %d\n", nb_args
);
2618 for(i
= 0; i
< MAX_ARGS
; i
++)
2619 qemu_free(str_allocated
[i
]);
2623 static void cmd_completion(const char *name
, const char *list
)
2625 const char *p
, *pstart
;
2634 p
= pstart
+ strlen(pstart
);
2636 if (len
> sizeof(cmd
) - 2)
2637 len
= sizeof(cmd
) - 2;
2638 memcpy(cmd
, pstart
, len
);
2640 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2641 add_completion(cmd
);
2649 static void file_completion(const char *input
)
2654 char file
[1024], file_prefix
[1024];
2658 p
= strrchr(input
, '/');
2661 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2662 pstrcpy(path
, sizeof(path
), ".");
2664 input_path_len
= p
- input
+ 1;
2665 memcpy(path
, input
, input_path_len
);
2666 if (input_path_len
> sizeof(path
) - 1)
2667 input_path_len
= sizeof(path
) - 1;
2668 path
[input_path_len
] = '\0';
2669 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2671 #ifdef DEBUG_COMPLETION
2672 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2674 ffs
= opendir(path
);
2682 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2683 memcpy(file
, input
, input_path_len
);
2684 if (input_path_len
< sizeof(file
))
2685 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2687 /* stat the file to find out if it's a directory.
2688 * In that case add a slash to speed up typing long paths
2691 if(S_ISDIR(sb
.st_mode
))
2692 pstrcat(file
, sizeof(file
), "/");
2693 add_completion(file
);
2699 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2701 const char *name
= bdrv_get_device_name(bs
);
2702 const char *input
= opaque
;
2704 if (input
[0] == '\0' ||
2705 !strncmp(name
, (char *)input
, strlen(input
))) {
2706 add_completion(name
);
2710 /* NOTE: this parser is an approximate form of the real command parser */
2711 static void parse_cmdline(const char *cmdline
,
2712 int *pnb_args
, char **args
)
2721 while (qemu_isspace(*p
))
2725 if (nb_args
>= MAX_ARGS
)
2727 ret
= get_str(buf
, sizeof(buf
), &p
);
2728 args
[nb_args
] = qemu_strdup(buf
);
2733 *pnb_args
= nb_args
;
2736 void readline_find_completion(const char *cmdline
)
2738 const char *cmdname
;
2739 char *args
[MAX_ARGS
];
2740 int nb_args
, i
, len
;
2741 const char *ptype
, *str
;
2742 const term_cmd_t
*cmd
;
2745 parse_cmdline(cmdline
, &nb_args
, args
);
2746 #ifdef DEBUG_COMPLETION
2747 for(i
= 0; i
< nb_args
; i
++) {
2748 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2752 /* if the line ends with a space, it means we want to complete the
2754 len
= strlen(cmdline
);
2755 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2756 if (nb_args
>= MAX_ARGS
)
2758 args
[nb_args
++] = qemu_strdup("");
2761 /* command completion */
2766 completion_index
= strlen(cmdname
);
2767 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2768 cmd_completion(cmdname
, cmd
->name
);
2771 /* find the command */
2772 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2773 if (compare_cmd(args
[0], cmd
->name
))
2778 ptype
= cmd
->args_type
;
2779 for(i
= 0; i
< nb_args
- 2; i
++) {
2780 if (*ptype
!= '\0') {
2782 while (*ptype
== '?')
2786 str
= args
[nb_args
- 1];
2789 /* file completion */
2790 completion_index
= strlen(str
);
2791 file_completion(str
);
2794 /* block device name completion */
2795 completion_index
= strlen(str
);
2796 bdrv_iterate(block_completion_it
, (void *)str
);
2799 /* XXX: more generic ? */
2800 if (!strcmp(cmd
->name
, "info")) {
2801 completion_index
= strlen(str
);
2802 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2803 cmd_completion(str
, cmd
->name
);
2805 } else if (!strcmp(cmd
->name
, "sendkey")) {
2806 completion_index
= strlen(str
);
2807 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2808 cmd_completion(str
, key
->name
);
2816 for(i
= 0; i
< nb_args
; i
++)
2820 static int term_can_read(void *opaque
)
2825 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2828 for(i
= 0; i
< size
; i
++)
2829 readline_handle_byte(buf
[i
]);
2832 static int monitor_suspended
;
2834 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2836 monitor_handle_command(cmdline
);
2837 if (!monitor_suspended
)
2838 monitor_start_input();
2840 monitor_suspended
= 2;
2843 void monitor_suspend(void)
2845 monitor_suspended
= 1;
2848 void monitor_resume(void)
2850 if (monitor_suspended
== 2)
2851 monitor_start_input();
2852 monitor_suspended
= 0;
2855 static void monitor_start_input(void)
2857 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2860 static void term_event(void *opaque
, int event
)
2862 if (event
!= CHR_EVENT_RESET
)
2866 term_printf("QEMU %s monitor - type 'help' for more information\n",
2868 monitor_start_input();
2871 static int is_first_init
= 1;
2873 void monitor_init(CharDriverState
*hd
, int show_banner
)
2877 if (is_first_init
) {
2878 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
2881 for (i
= 0; i
< MAX_MON
; i
++) {
2882 monitor_hd
[i
] = NULL
;
2886 for (i
= 0; i
< MAX_MON
; i
++) {
2887 if (monitor_hd
[i
] == NULL
) {
2893 hide_banner
= !show_banner
;
2895 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2897 readline_start("", 0, monitor_handle_command1
, NULL
);
2900 /* XXX: use threads ? */
2901 /* modal monitor readline */
2902 static int monitor_readline_started
;
2903 static char *monitor_readline_buf
;
2904 static int monitor_readline_buf_size
;
2906 static void monitor_readline_cb(void *opaque
, const char *input
)
2908 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2909 monitor_readline_started
= 0;
2912 static void monitor_readline(const char *prompt
, int is_password
,
2913 char *buf
, int buf_size
)
2915 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2916 monitor_readline_buf
= buf
;
2917 monitor_readline_buf_size
= buf_size
;
2918 monitor_readline_started
= 1;
2919 while (monitor_readline_started
) {
2924 int monitor_read_bdrv_key(BlockDriverState
*bs
)
2929 if (!bdrv_is_encrypted(bs
))
2932 term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
2933 bdrv_get_encrypted_filename(bs
));
2934 for(i
= 0; i
< 3; i
++) {
2935 monitor_readline("Password: ", 1, password
, sizeof(password
));
2936 if (bdrv_set_key(bs
, password
) == 0)
2938 term_printf("invalid password\n");