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
;
77 static BlockDriverCompletionFunc
*password_completion_cb
;
78 static void *password_opaque
;
80 static void monitor_start_input(void);
82 static CPUState
*mon_cpu
= NULL
;
84 static void monitor_read_password(ReadLineFunc
*readline_func
, void *opaque
)
86 readline_start("Password: ", 1, readline_func
, opaque
);
92 if (term_outbuf_index
> 0) {
93 for (i
= 0; i
< MAX_MON
; i
++)
94 if (monitor_hd
[i
] && monitor_hd
[i
]->focus
== 0)
95 qemu_chr_write(monitor_hd
[i
], term_outbuf
, term_outbuf_index
);
96 term_outbuf_index
= 0;
100 /* flush at every end of line or if the buffer is full */
101 void term_puts(const char *str
)
109 term_outbuf
[term_outbuf_index
++] = '\r';
110 term_outbuf
[term_outbuf_index
++] = c
;
111 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
117 void term_vprintf(const char *fmt
, va_list ap
)
120 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
124 void term_printf(const char *fmt
, ...)
128 term_vprintf(fmt
, ap
);
132 void term_print_filename(const char *filename
)
136 for (i
= 0; filename
[i
]; i
++) {
137 switch (filename
[i
]) {
141 term_printf("\\%c", filename
[i
]);
153 term_printf("%c", filename
[i
]);
159 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
163 term_vprintf(fmt
, ap
);
168 static int compare_cmd(const char *name
, const char *list
)
170 const char *p
, *pstart
;
178 p
= pstart
+ strlen(pstart
);
179 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
188 static void help_cmd1(const term_cmd_t
*cmds
, const char *prefix
, const char *name
)
190 const term_cmd_t
*cmd
;
192 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
193 if (!name
|| !strcmp(name
, cmd
->name
))
194 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
198 static void help_cmd(const char *name
)
200 if (name
&& !strcmp(name
, "info")) {
201 help_cmd1(info_cmds
, "info ", NULL
);
203 help_cmd1(term_cmds
, "", name
);
204 if (name
&& !strcmp(name
, "log")) {
205 const CPULogItem
*item
;
206 term_printf("Log items (comma separated):\n");
207 term_printf("%-10s %s\n", "none", "remove all logs");
208 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
209 term_printf("%-10s %s\n", item
->name
, item
->help
);
215 static void do_help(const char *name
)
220 static void do_commit(const char *device
)
224 all_devices
= !strcmp(device
, "all");
225 for (i
= 0; i
< nb_drives
; i
++) {
227 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
228 bdrv_commit(drives_table
[i
].bdrv
);
232 static void do_info(const char *item
)
234 const term_cmd_t
*cmd
;
235 void (*handler
)(void);
239 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
240 if (compare_cmd(item
, cmd
->name
))
247 handler
= cmd
->handler
;
251 static void do_info_version(void)
253 term_printf("%s\n", QEMU_VERSION
);
256 static void do_info_name(void)
259 term_printf("%s\n", qemu_name
);
262 #if defined(TARGET_I386)
263 static void do_info_hpet(void)
265 term_printf("HPET is %s by QEMU\n", (no_hpet
) ? "disabled" : "enabled");
269 static void do_info_uuid(void)
271 term_printf(UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1], qemu_uuid
[2],
272 qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5], qemu_uuid
[6],
273 qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9], qemu_uuid
[10],
274 qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13], qemu_uuid
[14],
278 static void do_info_block(void)
283 static void do_info_blockstats(void)
288 /* get the current CPU defined by the user */
289 static int mon_set_cpu(int cpu_index
)
293 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
294 if (env
->cpu_index
== cpu_index
) {
302 static CPUState
*mon_get_cpu(void)
310 static void do_info_registers(void)
317 cpu_dump_state(env
, NULL
, monitor_fprintf
,
320 cpu_dump_state(env
, NULL
, monitor_fprintf
,
325 static void do_info_cpus(void)
329 /* just to set the default cpu if not already done */
332 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
333 term_printf("%c CPU #%d:",
334 (env
== mon_cpu
) ? '*' : ' ',
336 #if defined(TARGET_I386)
337 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
338 #elif defined(TARGET_PPC)
339 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
340 #elif defined(TARGET_SPARC)
341 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
342 #elif defined(TARGET_MIPS)
343 term_printf(" PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
346 term_printf(" (halted)");
351 static void do_cpu_set(int index
)
353 if (mon_set_cpu(index
) < 0)
354 term_printf("Invalid CPU index\n");
357 static void do_info_jit(void)
359 dump_exec_info(NULL
, monitor_fprintf
);
362 static void do_info_history (void)
369 str
= readline_get_history(i
);
372 term_printf("%d: '%s'\n", i
, str
);
377 #if defined(TARGET_PPC)
378 /* XXX: not implemented in other targets */
379 static void do_info_cpu_stats (void)
384 cpu_dump_statistics(env
, NULL
, &monitor_fprintf
, 0);
388 static void do_quit(void)
393 static int eject_device(BlockDriverState
*bs
, int force
)
395 if (bdrv_is_inserted(bs
)) {
397 if (!bdrv_is_removable(bs
)) {
398 term_printf("device is not removable\n");
401 if (bdrv_is_locked(bs
)) {
402 term_printf("device is locked\n");
411 static void do_eject(int force
, const char *filename
)
413 BlockDriverState
*bs
;
415 bs
= bdrv_find(filename
);
417 term_printf("device not found\n");
420 eject_device(bs
, force
);
423 static void do_change_block(const char *device
, const char *filename
, const char *fmt
)
425 BlockDriverState
*bs
;
426 BlockDriver
*drv
= NULL
;
428 bs
= bdrv_find(device
);
430 term_printf("device not found\n");
434 drv
= bdrv_find_format(fmt
);
436 term_printf("invalid format %s\n", fmt
);
440 if (eject_device(bs
, 0) < 0)
442 bdrv_open2(bs
, filename
, 0, drv
);
443 monitor_read_bdrv_key_start(bs
, NULL
, NULL
);
446 static void change_vnc_password_cb(void *opaque
, const char *password
)
448 if (vnc_display_password(NULL
, password
) < 0)
449 term_printf("could not set VNC server password\n");
451 monitor_start_input();
454 static void do_change_vnc(const char *target
, const char *arg
)
456 if (strcmp(target
, "passwd") == 0 ||
457 strcmp(target
, "password") == 0) {
460 strncpy(password
, arg
, sizeof(password
));
461 password
[sizeof(password
) - 1] = '\0';
462 change_vnc_password_cb(NULL
, password
);
464 monitor_read_password(change_vnc_password_cb
, NULL
);
467 if (vnc_display_open(NULL
, target
) < 0)
468 term_printf("could not start VNC server on %s\n", target
);
472 static void do_change(const char *device
, const char *target
, const char *arg
)
474 if (strcmp(device
, "vnc") == 0) {
475 do_change_vnc(target
, arg
);
477 do_change_block(device
, target
, arg
);
481 static void do_screen_dump(const char *filename
)
483 vga_hw_screen_dump(filename
);
486 static void do_logfile(const char *filename
)
488 cpu_set_log_filename(filename
);
491 static void do_log(const char *items
)
495 if (!strcmp(items
, "none")) {
498 mask
= cpu_str_to_log_mask(items
);
507 static void do_stop(void)
509 vm_stop(EXCP_INTERRUPT
);
512 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
514 static void do_cont(void)
518 bdrv_iterate(encrypted_bdrv_it
, &err
);
519 /* only resume the vm if all keys are set and valid */
524 static void bdrv_key_cb(void *opaque
, int err
)
526 /* another key was set successfully, retry to continue */
531 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
535 if (!*err
&& bdrv_key_required(bs
)) {
537 monitor_read_bdrv_key_start(bs
, bdrv_key_cb
, NULL
);
541 #ifdef CONFIG_GDBSTUB
542 static void do_gdbserver(const char *port
)
545 port
= DEFAULT_GDBSTUB_PORT
;
546 if (gdbserver_start(port
) < 0) {
547 qemu_printf("Could not open gdbserver socket on port '%s'\n", port
);
549 qemu_printf("Waiting gdb connection on port '%s'\n", port
);
554 static void term_printc(int c
)
571 if (c
>= 32 && c
<= 126) {
572 term_printf("%c", c
);
574 term_printf("\\x%02x", c
);
581 static void memory_dump(int count
, int format
, int wsize
,
582 target_phys_addr_t addr
, int is_physical
)
585 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
593 if (!env
&& !is_physical
)
598 } else if (wsize
== 4) {
601 /* as default we use the current CS size */
605 if ((env
->efer
& MSR_EFER_LMA
) &&
606 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
610 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
615 monitor_disas(env
, addr
, count
, is_physical
, flags
);
624 nb_per_line
= line_size
/ wsize
;
629 max_digits
= (wsize
* 8 + 2) / 3;
633 max_digits
= (wsize
* 8) / 4;
637 max_digits
= (wsize
* 8 * 10 + 32) / 33;
646 term_printf(TARGET_FMT_plx
":", addr
);
648 term_printf(TARGET_FMT_lx
":", (target_ulong
)addr
);
653 cpu_physical_memory_rw(addr
, buf
, l
, 0);
658 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
659 term_printf(" Cannot access memory\n");
668 v
= ldub_raw(buf
+ i
);
671 v
= lduw_raw(buf
+ i
);
674 v
= (uint32_t)ldl_raw(buf
+ i
);
677 v
= ldq_raw(buf
+ i
);
683 term_printf("%#*" PRIo64
, max_digits
, v
);
686 term_printf("0x%0*" PRIx64
, max_digits
, v
);
689 term_printf("%*" PRIu64
, max_digits
, v
);
692 term_printf("%*" PRId64
, max_digits
, v
);
706 #if TARGET_LONG_BITS == 64
707 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
709 #define GET_TLONG(h, l) (l)
712 static void do_memory_dump(int count
, int format
, int size
,
713 uint32_t addrh
, uint32_t addrl
)
715 target_long addr
= GET_TLONG(addrh
, addrl
);
716 memory_dump(count
, format
, size
, addr
, 0);
719 #if TARGET_PHYS_ADDR_BITS > 32
720 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
722 #define GET_TPHYSADDR(h, l) (l)
725 static void do_physical_memory_dump(int count
, int format
, int size
,
726 uint32_t addrh
, uint32_t addrl
)
729 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
730 memory_dump(count
, format
, size
, addr
, 1);
733 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
735 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
736 #if TARGET_PHYS_ADDR_BITS == 32
739 term_printf("%#o", val
);
742 term_printf("%#x", val
);
745 term_printf("%u", val
);
749 term_printf("%d", val
);
758 term_printf("%#" PRIo64
, val
);
761 term_printf("%#" PRIx64
, val
);
764 term_printf("%" PRIu64
, val
);
768 term_printf("%" PRId64
, val
);
778 static void do_memory_save(unsigned int valh
, unsigned int vall
,
779 uint32_t size
, const char *filename
)
782 target_long addr
= GET_TLONG(valh
, vall
);
791 f
= fopen(filename
, "wb");
793 term_printf("could not open '%s'\n", filename
);
800 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
801 fwrite(buf
, 1, l
, f
);
808 static void do_physical_memory_save(unsigned int valh
, unsigned int vall
,
809 uint32_t size
, const char *filename
)
814 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
816 f
= fopen(filename
, "wb");
818 term_printf("could not open '%s'\n", filename
);
825 cpu_physical_memory_rw(addr
, buf
, l
, 0);
826 fwrite(buf
, 1, l
, f
);
834 static void do_sum(uint32_t start
, uint32_t size
)
841 for(addr
= start
; addr
< (start
+ size
); addr
++) {
842 cpu_physical_memory_rw(addr
, buf
, 1, 0);
843 /* BSD sum algorithm ('sum' Unix command) */
844 sum
= (sum
>> 1) | (sum
<< 15);
847 term_printf("%05d\n", sum
);
855 static const KeyDef key_defs
[] = {
882 { 0x0e, "backspace" },
919 { 0x37, "asterisk" },
922 { 0x3a, "caps_lock" },
933 { 0x45, "num_lock" },
934 { 0x46, "scroll_lock" },
936 { 0xb5, "kp_divide" },
937 { 0x37, "kp_multiply" },
938 { 0x4a, "kp_subtract" },
940 { 0x9c, "kp_enter" },
941 { 0x53, "kp_decimal" },
974 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
994 static int get_keycode(const char *key
)
1000 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1001 if (!strcmp(key
, p
->name
))
1004 if (strstart(key
, "0x", NULL
)) {
1005 ret
= strtoul(key
, &endp
, 0);
1006 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1012 #define MAX_KEYCODES 16
1013 static uint8_t keycodes
[MAX_KEYCODES
];
1014 static int nb_pending_keycodes
;
1015 static QEMUTimer
*key_timer
;
1017 static void release_keys(void *opaque
)
1021 while (nb_pending_keycodes
> 0) {
1022 nb_pending_keycodes
--;
1023 keycode
= keycodes
[nb_pending_keycodes
];
1025 kbd_put_keycode(0xe0);
1026 kbd_put_keycode(keycode
| 0x80);
1030 static void do_sendkey(const char *string
, int has_hold_time
, int hold_time
)
1032 char keyname_buf
[16];
1034 int keyname_len
, keycode
, i
;
1036 if (nb_pending_keycodes
> 0) {
1037 qemu_del_timer(key_timer
);
1044 separator
= strchr(string
, '-');
1045 keyname_len
= separator
? separator
- string
: strlen(string
);
1046 if (keyname_len
> 0) {
1047 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1048 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1049 term_printf("invalid key: '%s...'\n", keyname_buf
);
1052 if (i
== MAX_KEYCODES
) {
1053 term_printf("too many keys\n");
1056 keyname_buf
[keyname_len
] = 0;
1057 keycode
= get_keycode(keyname_buf
);
1059 term_printf("unknown key: '%s'\n", keyname_buf
);
1062 keycodes
[i
++] = keycode
;
1066 string
= separator
+ 1;
1068 nb_pending_keycodes
= i
;
1069 /* key down events */
1070 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1071 keycode
= keycodes
[i
];
1073 kbd_put_keycode(0xe0);
1074 kbd_put_keycode(keycode
& 0x7f);
1076 /* delayed key up events */
1077 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1078 muldiv64(ticks_per_sec
, hold_time
, 1000));
1081 static int mouse_button_state
;
1083 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
1087 dx
= strtol(dx_str
, NULL
, 0);
1088 dy
= strtol(dy_str
, NULL
, 0);
1091 dz
= strtol(dz_str
, NULL
, 0);
1092 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1095 static void do_mouse_button(int button_state
)
1097 mouse_button_state
= button_state
;
1098 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1101 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
1107 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1115 val
= cpu_inb(NULL
, addr
);
1119 val
= cpu_inw(NULL
, addr
);
1123 val
= cpu_inl(NULL
, addr
);
1127 term_printf("port%c[0x%04x] = %#0*x\n",
1128 suffix
, addr
, size
* 2, val
);
1131 /* boot_set handler */
1132 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1133 static void *boot_opaque
;
1135 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1137 qemu_boot_set_handler
= func
;
1138 boot_opaque
= opaque
;
1141 static void do_boot_set(const char *bootdevice
)
1145 if (qemu_boot_set_handler
) {
1146 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1148 term_printf("boot device list now set to %s\n", bootdevice
);
1150 term_printf("setting boot device list failed with error %i\n", res
);
1152 term_printf("no function defined to set boot device list for this architecture\n");
1156 static void do_system_reset(void)
1158 qemu_system_reset_request();
1161 static void do_system_powerdown(void)
1163 qemu_system_powerdown_request();
1166 #if defined(TARGET_I386)
1167 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
1169 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1172 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1173 pte
& PG_PSE_MASK
? 'P' : '-',
1174 pte
& PG_DIRTY_MASK
? 'D' : '-',
1175 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1176 pte
& PG_PCD_MASK
? 'C' : '-',
1177 pte
& PG_PWT_MASK
? 'T' : '-',
1178 pte
& PG_USER_MASK
? 'U' : '-',
1179 pte
& PG_RW_MASK
? 'W' : '-');
1182 static void tlb_info(void)
1186 uint32_t pgd
, pde
, pte
;
1188 env
= mon_get_cpu();
1192 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1193 term_printf("PG disabled\n");
1196 pgd
= env
->cr
[3] & ~0xfff;
1197 for(l1
= 0; l1
< 1024; l1
++) {
1198 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1199 pde
= le32_to_cpu(pde
);
1200 if (pde
& PG_PRESENT_MASK
) {
1201 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1202 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1204 for(l2
= 0; l2
< 1024; l2
++) {
1205 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1206 (uint8_t *)&pte
, 4);
1207 pte
= le32_to_cpu(pte
);
1208 if (pte
& PG_PRESENT_MASK
) {
1209 print_pte((l1
<< 22) + (l2
<< 12),
1219 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1220 uint32_t end
, int prot
)
1223 prot1
= *plast_prot
;
1224 if (prot
!= prot1
) {
1225 if (*pstart
!= -1) {
1226 term_printf("%08x-%08x %08x %c%c%c\n",
1227 *pstart
, end
, end
- *pstart
,
1228 prot1
& PG_USER_MASK
? 'u' : '-',
1230 prot1
& PG_RW_MASK
? 'w' : '-');
1240 static void mem_info(void)
1243 int l1
, l2
, prot
, last_prot
;
1244 uint32_t pgd
, pde
, pte
, start
, end
;
1246 env
= mon_get_cpu();
1250 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1251 term_printf("PG disabled\n");
1254 pgd
= env
->cr
[3] & ~0xfff;
1257 for(l1
= 0; l1
< 1024; l1
++) {
1258 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1259 pde
= le32_to_cpu(pde
);
1261 if (pde
& PG_PRESENT_MASK
) {
1262 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1263 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1264 mem_print(&start
, &last_prot
, end
, prot
);
1266 for(l2
= 0; l2
< 1024; l2
++) {
1267 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1268 (uint8_t *)&pte
, 4);
1269 pte
= le32_to_cpu(pte
);
1270 end
= (l1
<< 22) + (l2
<< 12);
1271 if (pte
& PG_PRESENT_MASK
) {
1272 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1276 mem_print(&start
, &last_prot
, end
, prot
);
1281 mem_print(&start
, &last_prot
, end
, prot
);
1287 #if defined(TARGET_SH4)
1289 static void print_tlb(int idx
, tlb_t
*tlb
)
1291 term_printf(" tlb%i:\t"
1292 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1293 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1294 "dirty=%hhu writethrough=%hhu\n",
1296 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1297 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1301 static void tlb_info(void)
1303 CPUState
*env
= mon_get_cpu();
1306 term_printf ("ITLB:\n");
1307 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1308 print_tlb (i
, &env
->itlb
[i
]);
1309 term_printf ("UTLB:\n");
1310 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1311 print_tlb (i
, &env
->utlb
[i
]);
1316 static void do_info_kqemu(void)
1322 env
= mon_get_cpu();
1324 term_printf("No cpu initialized yet");
1327 val
= env
->kqemu_enabled
;
1328 term_printf("kqemu support: ");
1332 term_printf("disabled\n");
1335 term_printf("enabled for user code\n");
1338 term_printf("enabled for user and kernel code\n");
1342 term_printf("kqemu support: not compiled\n");
1346 static void do_info_kvm(void)
1349 term_printf("kvm support: ");
1351 term_printf("enabled\n");
1353 term_printf("disabled\n");
1355 term_printf("kvm support: not compiled\n");
1359 #ifdef CONFIG_PROFILER
1363 int64_t kqemu_exec_count
;
1365 int64_t kqemu_ret_int_count
;
1366 int64_t kqemu_ret_excp_count
;
1367 int64_t kqemu_ret_intr_count
;
1369 static void do_info_profile(void)
1375 term_printf("async time %" PRId64
" (%0.3f)\n",
1376 dev_time
, dev_time
/ (double)ticks_per_sec
);
1377 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1378 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1379 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1380 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1381 kqemu_time
/ (double)total
* 100.0,
1383 kqemu_ret_int_count
,
1384 kqemu_ret_excp_count
,
1385 kqemu_ret_intr_count
);
1388 kqemu_exec_count
= 0;
1390 kqemu_ret_int_count
= 0;
1391 kqemu_ret_excp_count
= 0;
1392 kqemu_ret_intr_count
= 0;
1394 kqemu_record_dump();
1398 static void do_info_profile(void)
1400 term_printf("Internal profiler not compiled\n");
1404 /* Capture support */
1405 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1407 static void do_info_capture (void)
1412 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1413 term_printf ("[%d]: ", i
);
1414 s
->ops
.info (s
->opaque
);
1418 static void do_stop_capture (int n
)
1423 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1425 s
->ops
.destroy (s
->opaque
);
1426 LIST_REMOVE (s
, entries
);
1434 static void do_wav_capture (const char *path
,
1435 int has_freq
, int freq
,
1436 int has_bits
, int bits
,
1437 int has_channels
, int nchannels
)
1441 s
= qemu_mallocz (sizeof (*s
));
1443 freq
= has_freq
? freq
: 44100;
1444 bits
= has_bits
? bits
: 16;
1445 nchannels
= has_channels
? nchannels
: 2;
1447 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1448 term_printf ("Faied to add wave capture\n");
1451 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1455 #if defined(TARGET_I386)
1456 static void do_inject_nmi(int cpu_index
)
1460 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1461 if (env
->cpu_index
== cpu_index
) {
1462 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1468 static void do_info_status(void)
1471 term_printf("VM status: running\n");
1473 term_printf("VM status: paused\n");
1477 static void do_balloon(int value
)
1479 ram_addr_t target
= value
;
1480 qemu_balloon(target
<< 20);
1483 static void do_info_balloon(void)
1487 actual
= qemu_balloon_status();
1488 if (kvm_enabled() && !kvm_has_sync_mmu())
1489 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1490 else if (actual
== 0)
1491 term_printf("Ballooning not activated in VM\n");
1493 term_printf("balloon: actual=%d\n", (int)(actual
>> 20));
1496 /* Please update qemu-doc.texi when adding or changing commands */
1497 static const term_cmd_t term_cmds
[] = {
1498 { "help|?", "s?", do_help
,
1499 "[cmd]", "show the help" },
1500 { "commit", "s", do_commit
,
1501 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1502 { "info", "s?", do_info
,
1503 "subcommand", "show various information about the system state" },
1504 { "q|quit", "", do_quit
,
1505 "", "quit the emulator" },
1506 { "eject", "-fB", do_eject
,
1507 "[-f] device", "eject a removable medium (use -f to force it)" },
1508 { "change", "BFs?", do_change
,
1509 "device filename [format]", "change a removable medium, optional format" },
1510 { "screendump", "F", do_screen_dump
,
1511 "filename", "save screen into PPM image 'filename'" },
1512 { "logfile", "F", do_logfile
,
1513 "filename", "output logs to 'filename'" },
1514 { "log", "s", do_log
,
1515 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1516 { "savevm", "s?", do_savevm
,
1517 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1518 { "loadvm", "s", do_loadvm
,
1519 "tag|id", "restore a VM snapshot from its tag or id" },
1520 { "delvm", "s", do_delvm
,
1521 "tag|id", "delete a VM snapshot from its tag or id" },
1522 { "stop", "", do_stop
,
1523 "", "stop emulation", },
1524 { "c|cont", "", do_cont
,
1525 "", "resume emulation", },
1526 #ifdef CONFIG_GDBSTUB
1527 { "gdbserver", "s?", do_gdbserver
,
1528 "[port]", "start gdbserver session (default port=1234)", },
1530 { "x", "/l", do_memory_dump
,
1531 "/fmt addr", "virtual memory dump starting at 'addr'", },
1532 { "xp", "/l", do_physical_memory_dump
,
1533 "/fmt addr", "physical memory dump starting at 'addr'", },
1534 { "p|print", "/l", do_print
,
1535 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1536 { "i", "/ii.", do_ioport_read
,
1537 "/fmt addr", "I/O port read" },
1539 { "sendkey", "si?", do_sendkey
,
1540 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1541 { "system_reset", "", do_system_reset
,
1542 "", "reset the system" },
1543 { "system_powerdown", "", do_system_powerdown
,
1544 "", "send system power down event" },
1545 { "sum", "ii", do_sum
,
1546 "addr size", "compute the checksum of a memory region" },
1547 { "usb_add", "s", do_usb_add
,
1548 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1549 { "usb_del", "s", do_usb_del
,
1550 "device", "remove USB device 'bus.addr'" },
1551 { "cpu", "i", do_cpu_set
,
1552 "index", "set the default CPU" },
1553 { "mouse_move", "sss?", do_mouse_move
,
1554 "dx dy [dz]", "send mouse move events" },
1555 { "mouse_button", "i", do_mouse_button
,
1556 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1557 { "mouse_set", "i", do_mouse_set
,
1558 "index", "set which mouse device receives events" },
1560 { "wavcapture", "si?i?i?", do_wav_capture
,
1561 "path [frequency bits channels]",
1562 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1564 { "stopcapture", "i", do_stop_capture
,
1565 "capture index", "stop capture" },
1566 { "memsave", "lis", do_memory_save
,
1567 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1568 { "pmemsave", "lis", do_physical_memory_save
,
1569 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1570 { "boot_set", "s", do_boot_set
,
1571 "bootdevice", "define new values for the boot device list" },
1572 #if defined(TARGET_I386)
1573 { "nmi", "i", do_inject_nmi
,
1574 "cpu", "inject an NMI on the given CPU", },
1576 { "migrate", "-ds", do_migrate
,
1577 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1578 { "migrate_cancel", "", do_migrate_cancel
,
1579 "", "cancel the current VM migration" },
1580 { "migrate_set_speed", "s", do_migrate_set_speed
,
1581 "value", "set maximum speed (in bytes) for migrations" },
1582 #if defined(TARGET_I386)
1583 { "drive_add", "ss", drive_hot_add
, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1584 "[file=file][,if=type][,bus=n]\n"
1585 "[,unit=m][,media=d][index=i]\n"
1586 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1587 "[snapshot=on|off][,cache=on|off]",
1588 "add drive to PCI storage controller" },
1589 { "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" },
1590 { "pci_del", "s", pci_device_hot_remove
, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1591 { "host_net_add", "ss", net_host_device_add
,
1592 "[tap,user,socket,vde] options", "add host VLAN client" },
1593 { "host_net_remove", "is", net_host_device_remove
,
1594 "vlan_id name", "remove host VLAN client" },
1596 { "balloon", "i", do_balloon
,
1597 "target", "request VM to change it's memory allocation (in MB)" },
1598 { "set_link", "ss", do_set_link
,
1599 "name [up|down]", "change the link status of a network adapter" },
1603 /* Please update qemu-doc.texi when adding or changing commands */
1604 static const term_cmd_t info_cmds
[] = {
1605 { "version", "", do_info_version
,
1606 "", "show the version of QEMU" },
1607 { "network", "", do_info_network
,
1608 "", "show the network state" },
1609 { "chardev", "", qemu_chr_info
,
1610 "", "show the character devices" },
1611 { "block", "", do_info_block
,
1612 "", "show the block devices" },
1613 { "blockstats", "", do_info_blockstats
,
1614 "", "show block device statistics" },
1615 { "registers", "", do_info_registers
,
1616 "", "show the cpu registers" },
1617 { "cpus", "", do_info_cpus
,
1618 "", "show infos for each CPU" },
1619 { "history", "", do_info_history
,
1620 "", "show the command line history", },
1621 { "irq", "", irq_info
,
1622 "", "show the interrupts statistics (if available)", },
1623 { "pic", "", pic_info
,
1624 "", "show i8259 (PIC) state", },
1625 { "pci", "", pci_info
,
1626 "", "show PCI info", },
1627 #if defined(TARGET_I386) || defined(TARGET_SH4)
1628 { "tlb", "", tlb_info
,
1629 "", "show virtual to physical memory mappings", },
1631 #if defined(TARGET_I386)
1632 { "mem", "", mem_info
,
1633 "", "show the active virtual memory mappings", },
1634 { "hpet", "", do_info_hpet
,
1635 "", "show state of HPET", },
1637 { "jit", "", do_info_jit
,
1638 "", "show dynamic compiler info", },
1639 { "kqemu", "", do_info_kqemu
,
1640 "", "show KQEMU information", },
1641 { "kvm", "", do_info_kvm
,
1642 "", "show KVM information", },
1643 { "usb", "", usb_info
,
1644 "", "show guest USB devices", },
1645 { "usbhost", "", usb_host_info
,
1646 "", "show host USB devices", },
1647 { "profile", "", do_info_profile
,
1648 "", "show profiling information", },
1649 { "capture", "", do_info_capture
,
1650 "", "show capture information" },
1651 { "snapshots", "", do_info_snapshots
,
1652 "", "show the currently saved VM snapshots" },
1653 { "status", "", do_info_status
,
1654 "", "show the current VM status (running|paused)" },
1655 { "pcmcia", "", pcmcia_info
,
1656 "", "show guest PCMCIA status" },
1657 { "mice", "", do_info_mice
,
1658 "", "show which guest mouse is receiving events" },
1659 { "vnc", "", do_info_vnc
,
1660 "", "show the vnc server status"},
1661 { "name", "", do_info_name
,
1662 "", "show the current VM name" },
1663 { "uuid", "", do_info_uuid
,
1664 "", "show the current VM UUID" },
1665 #if defined(TARGET_PPC)
1666 { "cpustats", "", do_info_cpu_stats
,
1667 "", "show CPU statistics", },
1669 #if defined(CONFIG_SLIRP)
1670 { "slirp", "", do_info_slirp
,
1671 "", "show SLIRP statistics", },
1673 { "migrate", "", do_info_migrate
, "", "show migration status" },
1674 { "balloon", "", do_info_balloon
,
1675 "", "show balloon information" },
1679 /*******************************************************************/
1681 static const char *pch
;
1682 static jmp_buf expr_env
;
1687 typedef struct MonitorDef
{
1690 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1694 #if defined(TARGET_I386)
1695 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1697 CPUState
*env
= mon_get_cpu();
1700 return env
->eip
+ env
->segs
[R_CS
].base
;
1704 #if defined(TARGET_PPC)
1705 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1707 CPUState
*env
= mon_get_cpu();
1715 for (i
= 0; i
< 8; i
++)
1716 u
|= env
->crf
[i
] << (32 - (4 * i
));
1721 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1723 CPUState
*env
= mon_get_cpu();
1729 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1731 CPUState
*env
= mon_get_cpu();
1737 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1739 CPUState
*env
= mon_get_cpu();
1742 return cpu_ppc_load_decr(env
);
1745 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1747 CPUState
*env
= mon_get_cpu();
1750 return cpu_ppc_load_tbu(env
);
1753 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1755 CPUState
*env
= mon_get_cpu();
1758 return cpu_ppc_load_tbl(env
);
1762 #if defined(TARGET_SPARC)
1763 #ifndef TARGET_SPARC64
1764 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1766 CPUState
*env
= mon_get_cpu();
1769 return GET_PSR(env
);
1773 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1775 CPUState
*env
= mon_get_cpu();
1778 return env
->regwptr
[val
];
1782 static const MonitorDef monitor_defs
[] = {
1785 #define SEG(name, seg) \
1786 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1787 { name ".base", offsetof(CPUState, segs[seg].base) },\
1788 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1790 { "eax", offsetof(CPUState
, regs
[0]) },
1791 { "ecx", offsetof(CPUState
, regs
[1]) },
1792 { "edx", offsetof(CPUState
, regs
[2]) },
1793 { "ebx", offsetof(CPUState
, regs
[3]) },
1794 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1795 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1796 { "esi", offsetof(CPUState
, regs
[6]) },
1797 { "edi", offsetof(CPUState
, regs
[7]) },
1798 #ifdef TARGET_X86_64
1799 { "r8", offsetof(CPUState
, regs
[8]) },
1800 { "r9", offsetof(CPUState
, regs
[9]) },
1801 { "r10", offsetof(CPUState
, regs
[10]) },
1802 { "r11", offsetof(CPUState
, regs
[11]) },
1803 { "r12", offsetof(CPUState
, regs
[12]) },
1804 { "r13", offsetof(CPUState
, regs
[13]) },
1805 { "r14", offsetof(CPUState
, regs
[14]) },
1806 { "r15", offsetof(CPUState
, regs
[15]) },
1808 { "eflags", offsetof(CPUState
, eflags
) },
1809 { "eip", offsetof(CPUState
, eip
) },
1816 { "pc", 0, monitor_get_pc
, },
1817 #elif defined(TARGET_PPC)
1818 /* General purpose registers */
1819 { "r0", offsetof(CPUState
, gpr
[0]) },
1820 { "r1", offsetof(CPUState
, gpr
[1]) },
1821 { "r2", offsetof(CPUState
, gpr
[2]) },
1822 { "r3", offsetof(CPUState
, gpr
[3]) },
1823 { "r4", offsetof(CPUState
, gpr
[4]) },
1824 { "r5", offsetof(CPUState
, gpr
[5]) },
1825 { "r6", offsetof(CPUState
, gpr
[6]) },
1826 { "r7", offsetof(CPUState
, gpr
[7]) },
1827 { "r8", offsetof(CPUState
, gpr
[8]) },
1828 { "r9", offsetof(CPUState
, gpr
[9]) },
1829 { "r10", offsetof(CPUState
, gpr
[10]) },
1830 { "r11", offsetof(CPUState
, gpr
[11]) },
1831 { "r12", offsetof(CPUState
, gpr
[12]) },
1832 { "r13", offsetof(CPUState
, gpr
[13]) },
1833 { "r14", offsetof(CPUState
, gpr
[14]) },
1834 { "r15", offsetof(CPUState
, gpr
[15]) },
1835 { "r16", offsetof(CPUState
, gpr
[16]) },
1836 { "r17", offsetof(CPUState
, gpr
[17]) },
1837 { "r18", offsetof(CPUState
, gpr
[18]) },
1838 { "r19", offsetof(CPUState
, gpr
[19]) },
1839 { "r20", offsetof(CPUState
, gpr
[20]) },
1840 { "r21", offsetof(CPUState
, gpr
[21]) },
1841 { "r22", offsetof(CPUState
, gpr
[22]) },
1842 { "r23", offsetof(CPUState
, gpr
[23]) },
1843 { "r24", offsetof(CPUState
, gpr
[24]) },
1844 { "r25", offsetof(CPUState
, gpr
[25]) },
1845 { "r26", offsetof(CPUState
, gpr
[26]) },
1846 { "r27", offsetof(CPUState
, gpr
[27]) },
1847 { "r28", offsetof(CPUState
, gpr
[28]) },
1848 { "r29", offsetof(CPUState
, gpr
[29]) },
1849 { "r30", offsetof(CPUState
, gpr
[30]) },
1850 { "r31", offsetof(CPUState
, gpr
[31]) },
1851 /* Floating point registers */
1852 { "f0", offsetof(CPUState
, fpr
[0]) },
1853 { "f1", offsetof(CPUState
, fpr
[1]) },
1854 { "f2", offsetof(CPUState
, fpr
[2]) },
1855 { "f3", offsetof(CPUState
, fpr
[3]) },
1856 { "f4", offsetof(CPUState
, fpr
[4]) },
1857 { "f5", offsetof(CPUState
, fpr
[5]) },
1858 { "f6", offsetof(CPUState
, fpr
[6]) },
1859 { "f7", offsetof(CPUState
, fpr
[7]) },
1860 { "f8", offsetof(CPUState
, fpr
[8]) },
1861 { "f9", offsetof(CPUState
, fpr
[9]) },
1862 { "f10", offsetof(CPUState
, fpr
[10]) },
1863 { "f11", offsetof(CPUState
, fpr
[11]) },
1864 { "f12", offsetof(CPUState
, fpr
[12]) },
1865 { "f13", offsetof(CPUState
, fpr
[13]) },
1866 { "f14", offsetof(CPUState
, fpr
[14]) },
1867 { "f15", offsetof(CPUState
, fpr
[15]) },
1868 { "f16", offsetof(CPUState
, fpr
[16]) },
1869 { "f17", offsetof(CPUState
, fpr
[17]) },
1870 { "f18", offsetof(CPUState
, fpr
[18]) },
1871 { "f19", offsetof(CPUState
, fpr
[19]) },
1872 { "f20", offsetof(CPUState
, fpr
[20]) },
1873 { "f21", offsetof(CPUState
, fpr
[21]) },
1874 { "f22", offsetof(CPUState
, fpr
[22]) },
1875 { "f23", offsetof(CPUState
, fpr
[23]) },
1876 { "f24", offsetof(CPUState
, fpr
[24]) },
1877 { "f25", offsetof(CPUState
, fpr
[25]) },
1878 { "f26", offsetof(CPUState
, fpr
[26]) },
1879 { "f27", offsetof(CPUState
, fpr
[27]) },
1880 { "f28", offsetof(CPUState
, fpr
[28]) },
1881 { "f29", offsetof(CPUState
, fpr
[29]) },
1882 { "f30", offsetof(CPUState
, fpr
[30]) },
1883 { "f31", offsetof(CPUState
, fpr
[31]) },
1884 { "fpscr", offsetof(CPUState
, fpscr
) },
1885 /* Next instruction pointer */
1886 { "nip|pc", offsetof(CPUState
, nip
) },
1887 { "lr", offsetof(CPUState
, lr
) },
1888 { "ctr", offsetof(CPUState
, ctr
) },
1889 { "decr", 0, &monitor_get_decr
, },
1890 { "ccr", 0, &monitor_get_ccr
, },
1891 /* Machine state register */
1892 { "msr", 0, &monitor_get_msr
, },
1893 { "xer", 0, &monitor_get_xer
, },
1894 { "tbu", 0, &monitor_get_tbu
, },
1895 { "tbl", 0, &monitor_get_tbl
, },
1896 #if defined(TARGET_PPC64)
1897 /* Address space register */
1898 { "asr", offsetof(CPUState
, asr
) },
1900 /* Segment registers */
1901 { "sdr1", offsetof(CPUState
, sdr1
) },
1902 { "sr0", offsetof(CPUState
, sr
[0]) },
1903 { "sr1", offsetof(CPUState
, sr
[1]) },
1904 { "sr2", offsetof(CPUState
, sr
[2]) },
1905 { "sr3", offsetof(CPUState
, sr
[3]) },
1906 { "sr4", offsetof(CPUState
, sr
[4]) },
1907 { "sr5", offsetof(CPUState
, sr
[5]) },
1908 { "sr6", offsetof(CPUState
, sr
[6]) },
1909 { "sr7", offsetof(CPUState
, sr
[7]) },
1910 { "sr8", offsetof(CPUState
, sr
[8]) },
1911 { "sr9", offsetof(CPUState
, sr
[9]) },
1912 { "sr10", offsetof(CPUState
, sr
[10]) },
1913 { "sr11", offsetof(CPUState
, sr
[11]) },
1914 { "sr12", offsetof(CPUState
, sr
[12]) },
1915 { "sr13", offsetof(CPUState
, sr
[13]) },
1916 { "sr14", offsetof(CPUState
, sr
[14]) },
1917 { "sr15", offsetof(CPUState
, sr
[15]) },
1918 /* Too lazy to put BATs and SPRs ... */
1919 #elif defined(TARGET_SPARC)
1920 { "g0", offsetof(CPUState
, gregs
[0]) },
1921 { "g1", offsetof(CPUState
, gregs
[1]) },
1922 { "g2", offsetof(CPUState
, gregs
[2]) },
1923 { "g3", offsetof(CPUState
, gregs
[3]) },
1924 { "g4", offsetof(CPUState
, gregs
[4]) },
1925 { "g5", offsetof(CPUState
, gregs
[5]) },
1926 { "g6", offsetof(CPUState
, gregs
[6]) },
1927 { "g7", offsetof(CPUState
, gregs
[7]) },
1928 { "o0", 0, monitor_get_reg
},
1929 { "o1", 1, monitor_get_reg
},
1930 { "o2", 2, monitor_get_reg
},
1931 { "o3", 3, monitor_get_reg
},
1932 { "o4", 4, monitor_get_reg
},
1933 { "o5", 5, monitor_get_reg
},
1934 { "o6", 6, monitor_get_reg
},
1935 { "o7", 7, monitor_get_reg
},
1936 { "l0", 8, monitor_get_reg
},
1937 { "l1", 9, monitor_get_reg
},
1938 { "l2", 10, monitor_get_reg
},
1939 { "l3", 11, monitor_get_reg
},
1940 { "l4", 12, monitor_get_reg
},
1941 { "l5", 13, monitor_get_reg
},
1942 { "l6", 14, monitor_get_reg
},
1943 { "l7", 15, monitor_get_reg
},
1944 { "i0", 16, monitor_get_reg
},
1945 { "i1", 17, monitor_get_reg
},
1946 { "i2", 18, monitor_get_reg
},
1947 { "i3", 19, monitor_get_reg
},
1948 { "i4", 20, monitor_get_reg
},
1949 { "i5", 21, monitor_get_reg
},
1950 { "i6", 22, monitor_get_reg
},
1951 { "i7", 23, monitor_get_reg
},
1952 { "pc", offsetof(CPUState
, pc
) },
1953 { "npc", offsetof(CPUState
, npc
) },
1954 { "y", offsetof(CPUState
, y
) },
1955 #ifndef TARGET_SPARC64
1956 { "psr", 0, &monitor_get_psr
, },
1957 { "wim", offsetof(CPUState
, wim
) },
1959 { "tbr", offsetof(CPUState
, tbr
) },
1960 { "fsr", offsetof(CPUState
, fsr
) },
1961 { "f0", offsetof(CPUState
, fpr
[0]) },
1962 { "f1", offsetof(CPUState
, fpr
[1]) },
1963 { "f2", offsetof(CPUState
, fpr
[2]) },
1964 { "f3", offsetof(CPUState
, fpr
[3]) },
1965 { "f4", offsetof(CPUState
, fpr
[4]) },
1966 { "f5", offsetof(CPUState
, fpr
[5]) },
1967 { "f6", offsetof(CPUState
, fpr
[6]) },
1968 { "f7", offsetof(CPUState
, fpr
[7]) },
1969 { "f8", offsetof(CPUState
, fpr
[8]) },
1970 { "f9", offsetof(CPUState
, fpr
[9]) },
1971 { "f10", offsetof(CPUState
, fpr
[10]) },
1972 { "f11", offsetof(CPUState
, fpr
[11]) },
1973 { "f12", offsetof(CPUState
, fpr
[12]) },
1974 { "f13", offsetof(CPUState
, fpr
[13]) },
1975 { "f14", offsetof(CPUState
, fpr
[14]) },
1976 { "f15", offsetof(CPUState
, fpr
[15]) },
1977 { "f16", offsetof(CPUState
, fpr
[16]) },
1978 { "f17", offsetof(CPUState
, fpr
[17]) },
1979 { "f18", offsetof(CPUState
, fpr
[18]) },
1980 { "f19", offsetof(CPUState
, fpr
[19]) },
1981 { "f20", offsetof(CPUState
, fpr
[20]) },
1982 { "f21", offsetof(CPUState
, fpr
[21]) },
1983 { "f22", offsetof(CPUState
, fpr
[22]) },
1984 { "f23", offsetof(CPUState
, fpr
[23]) },
1985 { "f24", offsetof(CPUState
, fpr
[24]) },
1986 { "f25", offsetof(CPUState
, fpr
[25]) },
1987 { "f26", offsetof(CPUState
, fpr
[26]) },
1988 { "f27", offsetof(CPUState
, fpr
[27]) },
1989 { "f28", offsetof(CPUState
, fpr
[28]) },
1990 { "f29", offsetof(CPUState
, fpr
[29]) },
1991 { "f30", offsetof(CPUState
, fpr
[30]) },
1992 { "f31", offsetof(CPUState
, fpr
[31]) },
1993 #ifdef TARGET_SPARC64
1994 { "f32", offsetof(CPUState
, fpr
[32]) },
1995 { "f34", offsetof(CPUState
, fpr
[34]) },
1996 { "f36", offsetof(CPUState
, fpr
[36]) },
1997 { "f38", offsetof(CPUState
, fpr
[38]) },
1998 { "f40", offsetof(CPUState
, fpr
[40]) },
1999 { "f42", offsetof(CPUState
, fpr
[42]) },
2000 { "f44", offsetof(CPUState
, fpr
[44]) },
2001 { "f46", offsetof(CPUState
, fpr
[46]) },
2002 { "f48", offsetof(CPUState
, fpr
[48]) },
2003 { "f50", offsetof(CPUState
, fpr
[50]) },
2004 { "f52", offsetof(CPUState
, fpr
[52]) },
2005 { "f54", offsetof(CPUState
, fpr
[54]) },
2006 { "f56", offsetof(CPUState
, fpr
[56]) },
2007 { "f58", offsetof(CPUState
, fpr
[58]) },
2008 { "f60", offsetof(CPUState
, fpr
[60]) },
2009 { "f62", offsetof(CPUState
, fpr
[62]) },
2010 { "asi", offsetof(CPUState
, asi
) },
2011 { "pstate", offsetof(CPUState
, pstate
) },
2012 { "cansave", offsetof(CPUState
, cansave
) },
2013 { "canrestore", offsetof(CPUState
, canrestore
) },
2014 { "otherwin", offsetof(CPUState
, otherwin
) },
2015 { "wstate", offsetof(CPUState
, wstate
) },
2016 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2017 { "fprs", offsetof(CPUState
, fprs
) },
2023 static void expr_error(const char *msg
)
2025 term_printf("%s\n", msg
);
2026 longjmp(expr_env
, 1);
2029 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2030 static int get_monitor_def(target_long
*pval
, const char *name
)
2032 const MonitorDef
*md
;
2035 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2036 if (compare_cmd(name
, md
->name
)) {
2037 if (md
->get_value
) {
2038 *pval
= md
->get_value(md
, md
->offset
);
2040 CPUState
*env
= mon_get_cpu();
2043 ptr
= (uint8_t *)env
+ md
->offset
;
2046 *pval
= *(int32_t *)ptr
;
2049 *pval
= *(target_long
*)ptr
;
2062 static void next(void)
2066 while (qemu_isspace(*pch
))
2071 static int64_t expr_sum(void);
2073 static int64_t expr_unary(void)
2096 expr_error("')' expected");
2103 expr_error("character constant expected");
2107 expr_error("missing terminating \' character");
2117 while ((*pch
>= 'a' && *pch
<= 'z') ||
2118 (*pch
>= 'A' && *pch
<= 'Z') ||
2119 (*pch
>= '0' && *pch
<= '9') ||
2120 *pch
== '_' || *pch
== '.') {
2121 if ((q
- buf
) < sizeof(buf
) - 1)
2125 while (qemu_isspace(*pch
))
2128 ret
= get_monitor_def(®
, buf
);
2130 expr_error("unknown register");
2132 expr_error("no cpu defined");
2137 expr_error("unexpected end of expression");
2141 #if TARGET_PHYS_ADDR_BITS > 32
2142 n
= strtoull(pch
, &p
, 0);
2144 n
= strtoul(pch
, &p
, 0);
2147 expr_error("invalid char in expression");
2150 while (qemu_isspace(*pch
))
2158 static int64_t expr_prod(void)
2166 if (op
!= '*' && op
!= '/' && op
!= '%')
2169 val2
= expr_unary();
2178 expr_error("division by zero");
2189 static int64_t expr_logic(void)
2197 if (op
!= '&' && op
!= '|' && op
!= '^')
2217 static int64_t expr_sum(void)
2225 if (op
!= '+' && op
!= '-')
2228 val2
= expr_logic();
2237 static int get_expr(int64_t *pval
, const char **pp
)
2240 if (setjmp(expr_env
)) {
2244 while (qemu_isspace(*pch
))
2251 static int get_str(char *buf
, int buf_size
, const char **pp
)
2259 while (qemu_isspace(*p
))
2269 while (*p
!= '\0' && *p
!= '\"') {
2285 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2288 if ((q
- buf
) < buf_size
- 1) {
2292 if ((q
- buf
) < buf_size
- 1) {
2299 qemu_printf("unterminated string\n");
2304 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2305 if ((q
- buf
) < buf_size
- 1) {
2316 static int default_fmt_format
= 'x';
2317 static int default_fmt_size
= 4;
2321 static void monitor_handle_command(const char *cmdline
)
2323 const char *p
, *pstart
, *typestr
;
2325 int c
, nb_args
, len
, i
, has_arg
;
2326 const term_cmd_t
*cmd
;
2329 void *str_allocated
[MAX_ARGS
];
2330 void *args
[MAX_ARGS
];
2331 void (*handler_0
)(void);
2332 void (*handler_1
)(void *arg0
);
2333 void (*handler_2
)(void *arg0
, void *arg1
);
2334 void (*handler_3
)(void *arg0
, void *arg1
, void *arg2
);
2335 void (*handler_4
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
);
2336 void (*handler_5
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2338 void (*handler_6
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2339 void *arg4
, void *arg5
);
2340 void (*handler_7
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2341 void *arg4
, void *arg5
, void *arg6
);
2344 term_printf("command='%s'\n", cmdline
);
2347 /* extract the command name */
2350 while (qemu_isspace(*p
))
2355 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2358 if (len
> sizeof(cmdname
) - 1)
2359 len
= sizeof(cmdname
) - 1;
2360 memcpy(cmdname
, pstart
, len
);
2361 cmdname
[len
] = '\0';
2363 /* find the command */
2364 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2365 if (compare_cmd(cmdname
, cmd
->name
))
2368 term_printf("unknown command: '%s'\n", cmdname
);
2372 for(i
= 0; i
< MAX_ARGS
; i
++)
2373 str_allocated
[i
] = NULL
;
2375 /* parse the parameters */
2376 typestr
= cmd
->args_type
;
2391 while (qemu_isspace(*p
))
2393 if (*typestr
== '?') {
2396 /* no optional string: NULL argument */
2401 ret
= get_str(buf
, sizeof(buf
), &p
);
2405 term_printf("%s: filename expected\n", cmdname
);
2408 term_printf("%s: block device name expected\n", cmdname
);
2411 term_printf("%s: string expected\n", cmdname
);
2416 str
= qemu_malloc(strlen(buf
) + 1);
2417 pstrcpy(str
, sizeof(buf
), buf
);
2418 str_allocated
[nb_args
] = str
;
2420 if (nb_args
>= MAX_ARGS
) {
2422 term_printf("%s: too many arguments\n", cmdname
);
2425 args
[nb_args
++] = str
;
2430 int count
, format
, size
;
2432 while (qemu_isspace(*p
))
2438 if (qemu_isdigit(*p
)) {
2440 while (qemu_isdigit(*p
)) {
2441 count
= count
* 10 + (*p
- '0');
2479 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2480 term_printf("invalid char in format: '%c'\n", *p
);
2484 format
= default_fmt_format
;
2485 if (format
!= 'i') {
2486 /* for 'i', not specifying a size gives -1 as size */
2488 size
= default_fmt_size
;
2489 default_fmt_size
= size
;
2491 default_fmt_format
= format
;
2494 format
= default_fmt_format
;
2495 if (format
!= 'i') {
2496 size
= default_fmt_size
;
2501 if (nb_args
+ 3 > MAX_ARGS
)
2503 args
[nb_args
++] = (void*)(long)count
;
2504 args
[nb_args
++] = (void*)(long)format
;
2505 args
[nb_args
++] = (void*)(long)size
;
2513 while (qemu_isspace(*p
))
2515 if (*typestr
== '?' || *typestr
== '.') {
2516 if (*typestr
== '?') {
2524 while (qemu_isspace(*p
))
2532 if (nb_args
>= MAX_ARGS
)
2534 args
[nb_args
++] = (void *)(long)has_arg
;
2536 if (nb_args
>= MAX_ARGS
)
2542 if (get_expr(&val
, &p
))
2546 if (nb_args
>= MAX_ARGS
)
2548 args
[nb_args
++] = (void *)(long)val
;
2550 if ((nb_args
+ 1) >= MAX_ARGS
)
2552 #if TARGET_PHYS_ADDR_BITS > 32
2553 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2555 args
[nb_args
++] = (void *)0;
2557 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2569 while (qemu_isspace(*p
))
2575 term_printf("%s: unsupported option -%c\n",
2582 if (nb_args
>= MAX_ARGS
)
2584 args
[nb_args
++] = (void *)(long)has_option
;
2589 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2593 /* check that all arguments were parsed */
2594 while (qemu_isspace(*p
))
2597 term_printf("%s: extraneous characters at the end of line\n",
2604 handler_0
= cmd
->handler
;
2608 handler_1
= cmd
->handler
;
2612 handler_2
= cmd
->handler
;
2613 handler_2(args
[0], args
[1]);
2616 handler_3
= cmd
->handler
;
2617 handler_3(args
[0], args
[1], args
[2]);
2620 handler_4
= cmd
->handler
;
2621 handler_4(args
[0], args
[1], args
[2], args
[3]);
2624 handler_5
= cmd
->handler
;
2625 handler_5(args
[0], args
[1], args
[2], args
[3], args
[4]);
2628 handler_6
= cmd
->handler
;
2629 handler_6(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2632 handler_7
= cmd
->handler
;
2633 handler_7(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2636 term_printf("unsupported number of arguments: %d\n", nb_args
);
2640 for(i
= 0; i
< MAX_ARGS
; i
++)
2641 qemu_free(str_allocated
[i
]);
2645 static void cmd_completion(const char *name
, const char *list
)
2647 const char *p
, *pstart
;
2656 p
= pstart
+ strlen(pstart
);
2658 if (len
> sizeof(cmd
) - 2)
2659 len
= sizeof(cmd
) - 2;
2660 memcpy(cmd
, pstart
, len
);
2662 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2663 add_completion(cmd
);
2671 static void file_completion(const char *input
)
2676 char file
[1024], file_prefix
[1024];
2680 p
= strrchr(input
, '/');
2683 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2684 pstrcpy(path
, sizeof(path
), ".");
2686 input_path_len
= p
- input
+ 1;
2687 memcpy(path
, input
, input_path_len
);
2688 if (input_path_len
> sizeof(path
) - 1)
2689 input_path_len
= sizeof(path
) - 1;
2690 path
[input_path_len
] = '\0';
2691 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2693 #ifdef DEBUG_COMPLETION
2694 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2696 ffs
= opendir(path
);
2704 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2705 memcpy(file
, input
, input_path_len
);
2706 if (input_path_len
< sizeof(file
))
2707 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2709 /* stat the file to find out if it's a directory.
2710 * In that case add a slash to speed up typing long paths
2713 if(S_ISDIR(sb
.st_mode
))
2714 pstrcat(file
, sizeof(file
), "/");
2715 add_completion(file
);
2721 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2723 const char *name
= bdrv_get_device_name(bs
);
2724 const char *input
= opaque
;
2726 if (input
[0] == '\0' ||
2727 !strncmp(name
, (char *)input
, strlen(input
))) {
2728 add_completion(name
);
2732 /* NOTE: this parser is an approximate form of the real command parser */
2733 static void parse_cmdline(const char *cmdline
,
2734 int *pnb_args
, char **args
)
2743 while (qemu_isspace(*p
))
2747 if (nb_args
>= MAX_ARGS
)
2749 ret
= get_str(buf
, sizeof(buf
), &p
);
2750 args
[nb_args
] = qemu_strdup(buf
);
2755 *pnb_args
= nb_args
;
2758 void readline_find_completion(const char *cmdline
)
2760 const char *cmdname
;
2761 char *args
[MAX_ARGS
];
2762 int nb_args
, i
, len
;
2763 const char *ptype
, *str
;
2764 const term_cmd_t
*cmd
;
2767 parse_cmdline(cmdline
, &nb_args
, args
);
2768 #ifdef DEBUG_COMPLETION
2769 for(i
= 0; i
< nb_args
; i
++) {
2770 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2774 /* if the line ends with a space, it means we want to complete the
2776 len
= strlen(cmdline
);
2777 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2778 if (nb_args
>= MAX_ARGS
)
2780 args
[nb_args
++] = qemu_strdup("");
2783 /* command completion */
2788 completion_index
= strlen(cmdname
);
2789 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2790 cmd_completion(cmdname
, cmd
->name
);
2793 /* find the command */
2794 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2795 if (compare_cmd(args
[0], cmd
->name
))
2800 ptype
= cmd
->args_type
;
2801 for(i
= 0; i
< nb_args
- 2; i
++) {
2802 if (*ptype
!= '\0') {
2804 while (*ptype
== '?')
2808 str
= args
[nb_args
- 1];
2811 /* file completion */
2812 completion_index
= strlen(str
);
2813 file_completion(str
);
2816 /* block device name completion */
2817 completion_index
= strlen(str
);
2818 bdrv_iterate(block_completion_it
, (void *)str
);
2821 /* XXX: more generic ? */
2822 if (!strcmp(cmd
->name
, "info")) {
2823 completion_index
= strlen(str
);
2824 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2825 cmd_completion(str
, cmd
->name
);
2827 } else if (!strcmp(cmd
->name
, "sendkey")) {
2828 completion_index
= strlen(str
);
2829 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2830 cmd_completion(str
, key
->name
);
2838 for(i
= 0; i
< nb_args
; i
++)
2842 static int term_can_read(void *opaque
)
2847 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2850 for(i
= 0; i
< size
; i
++)
2851 readline_handle_byte(buf
[i
]);
2854 static int monitor_suspended
;
2856 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2858 monitor_handle_command(cmdline
);
2859 if (!monitor_suspended
)
2860 readline_show_prompt();
2862 monitor_suspended
= 2;
2865 void monitor_suspend(void)
2867 monitor_suspended
= 1;
2870 void monitor_resume(void)
2872 if (monitor_suspended
== 2)
2873 monitor_start_input();
2874 monitor_suspended
= 0;
2877 static void monitor_start_input(void)
2879 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2880 readline_show_prompt();
2883 static void term_event(void *opaque
, int event
)
2885 if (event
!= CHR_EVENT_RESET
)
2889 term_printf("QEMU %s monitor - type 'help' for more information\n",
2891 monitor_start_input();
2894 static int is_first_init
= 1;
2896 void monitor_init(CharDriverState
*hd
, int show_banner
)
2900 if (is_first_init
) {
2901 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
2904 for (i
= 0; i
< MAX_MON
; i
++) {
2905 monitor_hd
[i
] = NULL
;
2909 for (i
= 0; i
< MAX_MON
; i
++) {
2910 if (monitor_hd
[i
] == NULL
) {
2916 hide_banner
= !show_banner
;
2918 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2920 readline_start("", 0, monitor_handle_command1
, NULL
);
2923 static void bdrv_password_cb(void *opaque
, const char *password
)
2925 BlockDriverState
*bs
= opaque
;
2928 if (bdrv_set_key(bs
, password
) != 0) {
2929 term_printf("invalid password\n");
2932 if (password_completion_cb
)
2933 password_completion_cb(password_opaque
, ret
);
2935 monitor_start_input();
2938 void monitor_read_bdrv_key_start(BlockDriverState
*bs
,
2939 BlockDriverCompletionFunc
*completion_cb
,
2942 if (!bdrv_key_required(bs
)) {
2944 completion_cb(opaque
, 0);
2948 term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
2949 bdrv_get_encrypted_filename(bs
));
2951 password_completion_cb
= completion_cb
;
2952 password_opaque
= opaque
;
2954 monitor_read_password(bdrv_password_cb
, bs
);