Temporary workaround for ppc on ppc
[qemu-kvm/fedora.git] / monitor.c
blob022697cf080689d70b73048b9aaaccaf305266f2
1 /*
2 * QEMU monitor
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
22 * THE SOFTWARE.
24 #include "hw/hw.h"
25 #include "hw/usb.h"
26 #include "hw/pcmcia.h"
27 #include "hw/pc.h"
28 #include "hw/pci.h"
29 #include "gdbstub.h"
30 #include "net.h"
31 #include "qemu-char.h"
32 #include "sysemu.h"
33 #include "console.h"
34 #include "block.h"
35 #include "audio/audio.h"
36 #include "disas.h"
37 #include "balloon.h"
38 #include <dirent.h>
39 #include "qemu-timer.h"
40 #include "migration.h"
41 #include "kvm.h"
42 #include "acl.h"
44 #include "qemu-kvm.h"
46 //#define DEBUG
47 //#define DEBUG_COMPLETION
50 * Supported types:
52 * 'F' filename
53 * 'B' block device name
54 * 's' string (accept optional quote)
55 * 'i' 32 bit integer
56 * 'l' target long (32 or 64 bit)
57 * '/' optional gdb-like print format (like "/10x")
59 * '?' optional type (for 'F', 's' and 'i')
63 typedef struct term_cmd_t {
64 const char *name;
65 const char *args_type;
66 void *handler;
67 const char *params;
68 const char *help;
69 } term_cmd_t;
71 #define MAX_MON 4
72 static CharDriverState *monitor_hd[MAX_MON];
73 static int hide_banner;
75 static const term_cmd_t term_cmds[];
76 static const term_cmd_t info_cmds[];
78 static uint8_t term_outbuf[1024];
79 static int term_outbuf_index;
81 static void monitor_start_input(void);
82 static void monitor_readline(const char *prompt, int is_password,
83 char *buf, int buf_size);
85 static CPUState *mon_cpu = NULL;
87 void term_flush(void)
89 int i;
90 if (term_outbuf_index > 0) {
91 for (i = 0; i < MAX_MON; i++)
92 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
93 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
94 term_outbuf_index = 0;
98 /* flush at every end of line or if the buffer is full */
99 void term_puts(const char *str)
101 char c;
102 for(;;) {
103 c = *str++;
104 if (c == '\0')
105 break;
106 if (c == '\n')
107 term_outbuf[term_outbuf_index++] = '\r';
108 term_outbuf[term_outbuf_index++] = c;
109 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
110 c == '\n')
111 term_flush();
115 void term_vprintf(const char *fmt, va_list ap)
117 char buf[4096];
118 vsnprintf(buf, sizeof(buf), fmt, ap);
119 term_puts(buf);
122 void term_printf(const char *fmt, ...)
124 va_list ap;
125 va_start(ap, fmt);
126 term_vprintf(fmt, ap);
127 va_end(ap);
130 void term_print_filename(const char *filename)
132 int i;
134 for (i = 0; filename[i]; i++) {
135 switch (filename[i]) {
136 case ' ':
137 case '"':
138 case '\\':
139 term_printf("\\%c", filename[i]);
140 break;
141 case '\t':
142 term_printf("\\t");
143 break;
144 case '\r':
145 term_printf("\\r");
146 break;
147 case '\n':
148 term_printf("\\n");
149 break;
150 default:
151 term_printf("%c", filename[i]);
152 break;
157 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
159 va_list ap;
160 va_start(ap, fmt);
161 term_vprintf(fmt, ap);
162 va_end(ap);
163 return 0;
166 static int compare_cmd(const char *name, const char *list)
168 const char *p, *pstart;
169 int len;
170 len = strlen(name);
171 p = list;
172 for(;;) {
173 pstart = p;
174 p = strchr(p, '|');
175 if (!p)
176 p = pstart + strlen(pstart);
177 if ((p - pstart) == len && !memcmp(pstart, name, len))
178 return 1;
179 if (*p == '\0')
180 break;
181 p++;
183 return 0;
186 static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
188 const term_cmd_t *cmd;
190 for(cmd = cmds; cmd->name != NULL; cmd++) {
191 if (!name || !strcmp(name, cmd->name))
192 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
196 static void help_cmd(const char *name)
198 if (name && !strcmp(name, "info")) {
199 help_cmd1(info_cmds, "info ", NULL);
200 } else {
201 help_cmd1(term_cmds, "", name);
202 if (name && !strcmp(name, "log")) {
203 const CPULogItem *item;
204 term_printf("Log items (comma separated):\n");
205 term_printf("%-10s %s\n", "none", "remove all logs");
206 for(item = cpu_log_items; item->mask != 0; item++) {
207 term_printf("%-10s %s\n", item->name, item->help);
213 static void do_help(const char *name)
215 help_cmd(name);
218 static void do_commit(const char *device)
220 int i, all_devices;
222 all_devices = !strcmp(device, "all");
223 for (i = 0; i < nb_drives; i++) {
224 if (all_devices ||
225 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
226 bdrv_commit(drives_table[i].bdrv);
230 static void do_info(const char *item)
232 const term_cmd_t *cmd;
233 void (*handler)(void);
235 if (!item)
236 goto help;
237 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
238 if (compare_cmd(item, cmd->name))
239 goto found;
241 help:
242 help_cmd("info");
243 return;
244 found:
245 handler = cmd->handler;
246 handler();
249 static void do_info_version(void)
251 term_printf("%s\n", QEMU_VERSION);
254 static void do_info_name(void)
256 if (qemu_name)
257 term_printf("%s\n", qemu_name);
260 #if defined(TARGET_I386)
261 static void do_info_hpet(void)
263 term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
265 #endif
267 static void do_info_uuid(void)
269 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
270 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
271 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
272 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
273 qemu_uuid[15]);
276 static void do_info_block(void)
278 bdrv_info();
281 static void do_info_blockstats(void)
283 bdrv_info_stats();
286 /* get the current CPU defined by the user */
287 static int mon_set_cpu(int cpu_index)
289 CPUState *env;
291 for(env = first_cpu; env != NULL; env = env->next_cpu) {
292 if (env->cpu_index == cpu_index) {
293 mon_cpu = env;
294 return 0;
297 return -1;
300 static CPUState *mon_get_cpu(void)
302 if (!mon_cpu) {
303 mon_set_cpu(0);
306 kvm_save_registers(mon_cpu);
308 return mon_cpu;
311 static void do_info_registers(void)
313 CPUState *env;
314 env = mon_get_cpu();
315 if (!env)
316 return;
317 #ifdef TARGET_I386
318 cpu_dump_state(env, NULL, monitor_fprintf,
319 X86_DUMP_FPU);
320 #else
321 cpu_dump_state(env, NULL, monitor_fprintf,
323 #endif
326 static void do_info_cpus(void)
328 CPUState *env;
330 /* just to set the default cpu if not already done */
331 mon_get_cpu();
333 for(env = first_cpu; env != NULL; env = env->next_cpu) {
334 kvm_save_registers(env);
335 term_printf("%c CPU #%d:",
336 (env == mon_cpu) ? '*' : ' ',
337 env->cpu_index);
338 #if defined(TARGET_I386)
339 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
340 #elif defined(TARGET_PPC)
341 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
342 #elif defined(TARGET_SPARC)
343 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
344 #elif defined(TARGET_MIPS)
345 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
346 #endif
347 if (env->halted)
348 term_printf(" (halted)");
349 term_printf(" thread_id=%d", env->thread_id);
350 term_printf("\n");
354 static void do_cpu_set(int index)
356 if (mon_set_cpu(index) < 0)
357 term_printf("Invalid CPU index\n");
360 static void do_cpu_set_nr(int value, const char *status)
362 int state;
364 if (!strcmp(status, "online"))
365 state = 1;
366 else if (!strcmp(status, "offline"))
367 state = 0;
368 else {
369 term_printf("invalid status: %s\n", status);
370 return;
372 #if defined(TARGET_I386) || defined(TARGET_X86_64)
373 qemu_system_cpu_hot_add(value, state);
374 #endif
377 static void do_info_jit(void)
379 dump_exec_info(NULL, monitor_fprintf);
382 static void do_info_history (void)
384 int i;
385 const char *str;
387 i = 0;
388 for(;;) {
389 str = readline_get_history(i);
390 if (!str)
391 break;
392 term_printf("%d: '%s'\n", i, str);
393 i++;
397 #if defined(TARGET_PPC)
398 /* XXX: not implemented in other targets */
399 static void do_info_cpu_stats (void)
401 CPUState *env;
403 env = mon_get_cpu();
404 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
406 #endif
408 static void do_quit(void)
410 exit(0);
413 static int eject_device(BlockDriverState *bs, int force)
415 if (bdrv_is_inserted(bs)) {
416 if (!force) {
417 if (!bdrv_is_removable(bs)) {
418 term_printf("device is not removable\n");
419 return -1;
421 if (bdrv_is_locked(bs)) {
422 term_printf("device is locked\n");
423 return -1;
426 bdrv_close(bs);
428 return 0;
431 static void do_eject(int force, const char *filename)
433 BlockDriverState *bs;
435 bs = bdrv_find(filename);
436 if (!bs) {
437 term_printf("device not found\n");
438 return;
440 eject_device(bs, force);
443 static void do_change_block(const char *device, const char *filename, const char *fmt)
445 BlockDriverState *bs;
446 BlockDriver *drv = NULL;
448 bs = bdrv_find(device);
449 if (!bs) {
450 term_printf("device not found\n");
451 return;
453 if (fmt) {
454 drv = bdrv_find_format(fmt);
455 if (!drv) {
456 term_printf("invalid format %s\n", fmt);
457 return;
460 if (eject_device(bs, 0) < 0)
461 return;
462 bdrv_open2(bs, filename, 0, drv);
463 monitor_read_bdrv_key(bs);
466 static void do_change_vnc(const char *target, const char *arg)
468 if (strcmp(target, "passwd") == 0 ||
469 strcmp(target, "password") == 0) {
470 char password[9];
471 if (arg) {
472 strncpy(password, arg, sizeof(password));
473 password[sizeof(password) - 1] = '\0';
474 } else
475 monitor_readline("Password: ", 1, password, sizeof(password));
476 if (vnc_display_password(NULL, password) < 0)
477 term_printf("could not set VNC server password\n");
478 } else {
479 if (vnc_display_open(NULL, target) < 0)
480 term_printf("could not start VNC server on %s\n", target);
484 static void do_change(const char *device, const char *target, const char *arg)
486 if (strcmp(device, "vnc") == 0) {
487 do_change_vnc(target, arg);
488 } else {
489 do_change_block(device, target, arg);
493 static void do_screen_dump(const char *filename)
495 vga_hw_screen_dump(filename);
498 static void do_logfile(const char *filename)
500 cpu_set_log_filename(filename);
503 static void do_log(const char *items)
505 int mask;
507 if (!strcmp(items, "none")) {
508 mask = 0;
509 } else {
510 mask = cpu_str_to_log_mask(items);
511 if (!mask) {
512 help_cmd("log");
513 return;
516 cpu_set_log(mask);
519 static void do_stop(void)
521 vm_stop(EXCP_INTERRUPT);
524 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
526 int *err = opaque;
528 if (bdrv_key_required(bs))
529 *err = monitor_read_bdrv_key(bs);
530 else
531 *err = 0;
534 static void do_cont(void)
536 int err = 0;
538 bdrv_iterate(encrypted_bdrv_it, &err);
539 /* only resume the vm if all keys are set and valid */
540 if (!err)
541 vm_start();
544 #ifdef CONFIG_GDBSTUB
545 static void do_gdbserver(const char *port)
547 if (!port)
548 port = DEFAULT_GDBSTUB_PORT;
549 if (gdbserver_start(port) < 0) {
550 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
551 } else {
552 qemu_printf("Waiting gdb connection on port '%s'\n", port);
555 #endif
557 static void term_printc(int c)
559 term_printf("'");
560 switch(c) {
561 case '\'':
562 term_printf("\\'");
563 break;
564 case '\\':
565 term_printf("\\\\");
566 break;
567 case '\n':
568 term_printf("\\n");
569 break;
570 case '\r':
571 term_printf("\\r");
572 break;
573 default:
574 if (c >= 32 && c <= 126) {
575 term_printf("%c", c);
576 } else {
577 term_printf("\\x%02x", c);
579 break;
581 term_printf("'");
584 static void memory_dump(int count, int format, int wsize,
585 target_phys_addr_t addr, int is_physical)
587 CPUState *env;
588 int nb_per_line, l, line_size, i, max_digits, len;
589 uint8_t buf[16];
590 uint64_t v;
592 if (format == 'i') {
593 int flags;
594 flags = 0;
595 env = mon_get_cpu();
596 if (!env && !is_physical)
597 return;
598 #ifdef TARGET_I386
599 if (wsize == 2) {
600 flags = 1;
601 } else if (wsize == 4) {
602 flags = 0;
603 } else {
604 /* as default we use the current CS size */
605 flags = 0;
606 if (env) {
607 #ifdef TARGET_X86_64
608 if ((env->efer & MSR_EFER_LMA) &&
609 (env->segs[R_CS].flags & DESC_L_MASK))
610 flags = 2;
611 else
612 #endif
613 if (!(env->segs[R_CS].flags & DESC_B_MASK))
614 flags = 1;
617 #endif
618 monitor_disas(env, addr, count, is_physical, flags);
619 return;
622 len = wsize * count;
623 if (wsize == 1)
624 line_size = 8;
625 else
626 line_size = 16;
627 nb_per_line = line_size / wsize;
628 max_digits = 0;
630 switch(format) {
631 case 'o':
632 max_digits = (wsize * 8 + 2) / 3;
633 break;
634 default:
635 case 'x':
636 max_digits = (wsize * 8) / 4;
637 break;
638 case 'u':
639 case 'd':
640 max_digits = (wsize * 8 * 10 + 32) / 33;
641 break;
642 case 'c':
643 wsize = 1;
644 break;
647 while (len > 0) {
648 if (is_physical)
649 term_printf(TARGET_FMT_plx ":", addr);
650 else
651 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
652 l = len;
653 if (l > line_size)
654 l = line_size;
655 if (is_physical) {
656 cpu_physical_memory_rw(addr, buf, l, 0);
657 } else {
658 env = mon_get_cpu();
659 if (!env)
660 break;
661 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
662 term_printf(" Cannot access memory\n");
663 break;
666 i = 0;
667 while (i < l) {
668 switch(wsize) {
669 default:
670 case 1:
671 v = ldub_raw(buf + i);
672 break;
673 case 2:
674 v = lduw_raw(buf + i);
675 break;
676 case 4:
677 v = (uint32_t)ldl_raw(buf + i);
678 break;
679 case 8:
680 v = ldq_raw(buf + i);
681 break;
683 term_printf(" ");
684 switch(format) {
685 case 'o':
686 term_printf("%#*" PRIo64, max_digits, v);
687 break;
688 case 'x':
689 term_printf("0x%0*" PRIx64, max_digits, v);
690 break;
691 case 'u':
692 term_printf("%*" PRIu64, max_digits, v);
693 break;
694 case 'd':
695 term_printf("%*" PRId64, max_digits, v);
696 break;
697 case 'c':
698 term_printc(v);
699 break;
701 i += wsize;
703 term_printf("\n");
704 addr += l;
705 len -= l;
709 #if TARGET_LONG_BITS == 64
710 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
711 #else
712 #define GET_TLONG(h, l) (l)
713 #endif
715 static void do_memory_dump(int count, int format, int size,
716 uint32_t addrh, uint32_t addrl)
718 target_long addr = GET_TLONG(addrh, addrl);
719 memory_dump(count, format, size, addr, 0);
722 #if TARGET_PHYS_ADDR_BITS > 32
723 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
724 #else
725 #define GET_TPHYSADDR(h, l) (l)
726 #endif
728 static void do_physical_memory_dump(int count, int format, int size,
729 uint32_t addrh, uint32_t addrl)
732 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
733 memory_dump(count, format, size, addr, 1);
736 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
738 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
739 #if TARGET_PHYS_ADDR_BITS == 32
740 switch(format) {
741 case 'o':
742 term_printf("%#o", val);
743 break;
744 case 'x':
745 term_printf("%#x", val);
746 break;
747 case 'u':
748 term_printf("%u", val);
749 break;
750 default:
751 case 'd':
752 term_printf("%d", val);
753 break;
754 case 'c':
755 term_printc(val);
756 break;
758 #else
759 switch(format) {
760 case 'o':
761 term_printf("%#" PRIo64, val);
762 break;
763 case 'x':
764 term_printf("%#" PRIx64, val);
765 break;
766 case 'u':
767 term_printf("%" PRIu64, val);
768 break;
769 default:
770 case 'd':
771 term_printf("%" PRId64, val);
772 break;
773 case 'c':
774 term_printc(val);
775 break;
777 #endif
778 term_printf("\n");
781 static void do_memory_save(unsigned int valh, unsigned int vall,
782 uint32_t size, const char *filename)
784 FILE *f;
785 target_long addr = GET_TLONG(valh, vall);
786 uint32_t l;
787 CPUState *env;
788 uint8_t buf[1024];
790 env = mon_get_cpu();
791 if (!env)
792 return;
794 f = fopen(filename, "wb");
795 if (!f) {
796 term_printf("could not open '%s'\n", filename);
797 return;
799 while (size != 0) {
800 l = sizeof(buf);
801 if (l > size)
802 l = size;
803 cpu_memory_rw_debug(env, addr, buf, l, 0);
804 fwrite(buf, 1, l, f);
805 addr += l;
806 size -= l;
808 fclose(f);
811 static void do_physical_memory_save(unsigned int valh, unsigned int vall,
812 uint32_t size, const char *filename)
814 FILE *f;
815 uint32_t l;
816 uint8_t buf[1024];
817 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
819 f = fopen(filename, "wb");
820 if (!f) {
821 term_printf("could not open '%s'\n", filename);
822 return;
824 while (size != 0) {
825 l = sizeof(buf);
826 if (l > size)
827 l = size;
828 cpu_physical_memory_rw(addr, buf, l, 0);
829 fwrite(buf, 1, l, f);
830 fflush(f);
831 addr += l;
832 size -= l;
834 fclose(f);
837 static void do_sum(uint32_t start, uint32_t size)
839 uint32_t addr;
840 uint8_t buf[1];
841 uint16_t sum;
843 sum = 0;
844 for(addr = start; addr < (start + size); addr++) {
845 cpu_physical_memory_rw(addr, buf, 1, 0);
846 /* BSD sum algorithm ('sum' Unix command) */
847 sum = (sum >> 1) | (sum << 15);
848 sum += buf[0];
850 term_printf("%05d\n", sum);
853 typedef struct {
854 int keycode;
855 const char *name;
856 } KeyDef;
858 static const KeyDef key_defs[] = {
859 { 0x2a, "shift" },
860 { 0x36, "shift_r" },
862 { 0x38, "alt" },
863 { 0xb8, "alt_r" },
864 { 0x64, "altgr" },
865 { 0xe4, "altgr_r" },
866 { 0x1d, "ctrl" },
867 { 0x9d, "ctrl_r" },
869 { 0xdd, "menu" },
871 { 0x01, "esc" },
873 { 0x02, "1" },
874 { 0x03, "2" },
875 { 0x04, "3" },
876 { 0x05, "4" },
877 { 0x06, "5" },
878 { 0x07, "6" },
879 { 0x08, "7" },
880 { 0x09, "8" },
881 { 0x0a, "9" },
882 { 0x0b, "0" },
883 { 0x0c, "minus" },
884 { 0x0d, "equal" },
885 { 0x0e, "backspace" },
887 { 0x0f, "tab" },
888 { 0x10, "q" },
889 { 0x11, "w" },
890 { 0x12, "e" },
891 { 0x13, "r" },
892 { 0x14, "t" },
893 { 0x15, "y" },
894 { 0x16, "u" },
895 { 0x17, "i" },
896 { 0x18, "o" },
897 { 0x19, "p" },
899 { 0x1c, "ret" },
901 { 0x1e, "a" },
902 { 0x1f, "s" },
903 { 0x20, "d" },
904 { 0x21, "f" },
905 { 0x22, "g" },
906 { 0x23, "h" },
907 { 0x24, "j" },
908 { 0x25, "k" },
909 { 0x26, "l" },
911 { 0x2c, "z" },
912 { 0x2d, "x" },
913 { 0x2e, "c" },
914 { 0x2f, "v" },
915 { 0x30, "b" },
916 { 0x31, "n" },
917 { 0x32, "m" },
918 { 0x33, "comma" },
919 { 0x34, "dot" },
920 { 0x35, "slash" },
922 { 0x37, "asterisk" },
924 { 0x39, "spc" },
925 { 0x3a, "caps_lock" },
926 { 0x3b, "f1" },
927 { 0x3c, "f2" },
928 { 0x3d, "f3" },
929 { 0x3e, "f4" },
930 { 0x3f, "f5" },
931 { 0x40, "f6" },
932 { 0x41, "f7" },
933 { 0x42, "f8" },
934 { 0x43, "f9" },
935 { 0x44, "f10" },
936 { 0x45, "num_lock" },
937 { 0x46, "scroll_lock" },
939 { 0xb5, "kp_divide" },
940 { 0x37, "kp_multiply" },
941 { 0x4a, "kp_subtract" },
942 { 0x4e, "kp_add" },
943 { 0x9c, "kp_enter" },
944 { 0x53, "kp_decimal" },
945 { 0x54, "sysrq" },
947 { 0x52, "kp_0" },
948 { 0x4f, "kp_1" },
949 { 0x50, "kp_2" },
950 { 0x51, "kp_3" },
951 { 0x4b, "kp_4" },
952 { 0x4c, "kp_5" },
953 { 0x4d, "kp_6" },
954 { 0x47, "kp_7" },
955 { 0x48, "kp_8" },
956 { 0x49, "kp_9" },
958 { 0x56, "<" },
960 { 0x57, "f11" },
961 { 0x58, "f12" },
963 { 0xb7, "print" },
965 { 0xc7, "home" },
966 { 0xc9, "pgup" },
967 { 0xd1, "pgdn" },
968 { 0xcf, "end" },
970 { 0xcb, "left" },
971 { 0xc8, "up" },
972 { 0xd0, "down" },
973 { 0xcd, "right" },
975 { 0xd2, "insert" },
976 { 0xd3, "delete" },
977 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
978 { 0xf0, "stop" },
979 { 0xf1, "again" },
980 { 0xf2, "props" },
981 { 0xf3, "undo" },
982 { 0xf4, "front" },
983 { 0xf5, "copy" },
984 { 0xf6, "open" },
985 { 0xf7, "paste" },
986 { 0xf8, "find" },
987 { 0xf9, "cut" },
988 { 0xfa, "lf" },
989 { 0xfb, "help" },
990 { 0xfc, "meta_l" },
991 { 0xfd, "meta_r" },
992 { 0xfe, "compose" },
993 #endif
994 { 0, NULL },
997 static int get_keycode(const char *key)
999 const KeyDef *p;
1000 char *endp;
1001 int ret;
1003 for(p = key_defs; p->name != NULL; p++) {
1004 if (!strcmp(key, p->name))
1005 return p->keycode;
1007 if (strstart(key, "0x", NULL)) {
1008 ret = strtoul(key, &endp, 0);
1009 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1010 return ret;
1012 return -1;
1015 #define MAX_KEYCODES 16
1016 static uint8_t keycodes[MAX_KEYCODES];
1017 static int nb_pending_keycodes;
1018 static QEMUTimer *key_timer;
1020 static void release_keys(void *opaque)
1022 int keycode;
1024 while (nb_pending_keycodes > 0) {
1025 nb_pending_keycodes--;
1026 keycode = keycodes[nb_pending_keycodes];
1027 if (keycode & 0x80)
1028 kbd_put_keycode(0xe0);
1029 kbd_put_keycode(keycode | 0x80);
1033 static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1035 char keyname_buf[16];
1036 char *separator;
1037 int keyname_len, keycode, i;
1039 if (nb_pending_keycodes > 0) {
1040 qemu_del_timer(key_timer);
1041 release_keys(NULL);
1043 if (!has_hold_time)
1044 hold_time = 100;
1045 i = 0;
1046 while (1) {
1047 separator = strchr(string, '-');
1048 keyname_len = separator ? separator - string : strlen(string);
1049 if (keyname_len > 0) {
1050 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1051 if (keyname_len > sizeof(keyname_buf) - 1) {
1052 term_printf("invalid key: '%s...'\n", keyname_buf);
1053 return;
1055 if (i == MAX_KEYCODES) {
1056 term_printf("too many keys\n");
1057 return;
1059 keyname_buf[keyname_len] = 0;
1060 keycode = get_keycode(keyname_buf);
1061 if (keycode < 0) {
1062 term_printf("unknown key: '%s'\n", keyname_buf);
1063 return;
1065 keycodes[i++] = keycode;
1067 if (!separator)
1068 break;
1069 string = separator + 1;
1071 nb_pending_keycodes = i;
1072 /* key down events */
1073 for (i = 0; i < nb_pending_keycodes; i++) {
1074 keycode = keycodes[i];
1075 if (keycode & 0x80)
1076 kbd_put_keycode(0xe0);
1077 kbd_put_keycode(keycode & 0x7f);
1079 /* delayed key up events */
1080 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1081 muldiv64(ticks_per_sec, hold_time, 1000));
1084 static int mouse_button_state;
1086 static void do_mouse_move(const char *dx_str, const char *dy_str,
1087 const char *dz_str)
1089 int dx, dy, dz;
1090 dx = strtol(dx_str, NULL, 0);
1091 dy = strtol(dy_str, NULL, 0);
1092 dz = 0;
1093 if (dz_str)
1094 dz = strtol(dz_str, NULL, 0);
1095 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1098 static void do_mouse_button(int button_state)
1100 mouse_button_state = button_state;
1101 kbd_mouse_event(0, 0, 0, mouse_button_state);
1104 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1106 uint32_t val;
1107 int suffix;
1109 if (has_index) {
1110 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1111 addr++;
1113 addr &= 0xffff;
1115 switch(size) {
1116 default:
1117 case 1:
1118 val = cpu_inb(NULL, addr);
1119 suffix = 'b';
1120 break;
1121 case 2:
1122 val = cpu_inw(NULL, addr);
1123 suffix = 'w';
1124 break;
1125 case 4:
1126 val = cpu_inl(NULL, addr);
1127 suffix = 'l';
1128 break;
1130 term_printf("port%c[0x%04x] = %#0*x\n",
1131 suffix, addr, size * 2, val);
1134 /* boot_set handler */
1135 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1136 static void *boot_opaque;
1138 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1140 qemu_boot_set_handler = func;
1141 boot_opaque = opaque;
1144 static void do_boot_set(const char *bootdevice)
1146 int res;
1148 if (qemu_boot_set_handler) {
1149 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1150 if (res == 0)
1151 term_printf("boot device list now set to %s\n", bootdevice);
1152 else
1153 term_printf("setting boot device list failed with error %i\n", res);
1154 } else {
1155 term_printf("no function defined to set boot device list for this architecture\n");
1159 static void do_system_reset(void)
1161 qemu_system_reset_request();
1164 static void do_system_powerdown(void)
1166 qemu_system_powerdown_request();
1169 #if defined(TARGET_I386)
1170 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1172 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1173 addr,
1174 pte & mask,
1175 pte & PG_GLOBAL_MASK ? 'G' : '-',
1176 pte & PG_PSE_MASK ? 'P' : '-',
1177 pte & PG_DIRTY_MASK ? 'D' : '-',
1178 pte & PG_ACCESSED_MASK ? 'A' : '-',
1179 pte & PG_PCD_MASK ? 'C' : '-',
1180 pte & PG_PWT_MASK ? 'T' : '-',
1181 pte & PG_USER_MASK ? 'U' : '-',
1182 pte & PG_RW_MASK ? 'W' : '-');
1185 static void tlb_info(void)
1187 CPUState *env;
1188 int l1, l2;
1189 uint32_t pgd, pde, pte;
1191 env = mon_get_cpu();
1192 if (!env)
1193 return;
1195 if (!(env->cr[0] & CR0_PG_MASK)) {
1196 term_printf("PG disabled\n");
1197 return;
1199 pgd = env->cr[3] & ~0xfff;
1200 for(l1 = 0; l1 < 1024; l1++) {
1201 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1202 pde = le32_to_cpu(pde);
1203 if (pde & PG_PRESENT_MASK) {
1204 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1205 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1206 } else {
1207 for(l2 = 0; l2 < 1024; l2++) {
1208 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1209 (uint8_t *)&pte, 4);
1210 pte = le32_to_cpu(pte);
1211 if (pte & PG_PRESENT_MASK) {
1212 print_pte((l1 << 22) + (l2 << 12),
1213 pte & ~PG_PSE_MASK,
1214 ~0xfff);
1222 static void mem_print(uint32_t *pstart, int *plast_prot,
1223 uint32_t end, int prot)
1225 int prot1;
1226 prot1 = *plast_prot;
1227 if (prot != prot1) {
1228 if (*pstart != -1) {
1229 term_printf("%08x-%08x %08x %c%c%c\n",
1230 *pstart, end, end - *pstart,
1231 prot1 & PG_USER_MASK ? 'u' : '-',
1232 'r',
1233 prot1 & PG_RW_MASK ? 'w' : '-');
1235 if (prot != 0)
1236 *pstart = end;
1237 else
1238 *pstart = -1;
1239 *plast_prot = prot;
1243 static void mem_info(void)
1245 CPUState *env;
1246 int l1, l2, prot, last_prot;
1247 uint32_t pgd, pde, pte, start, end;
1249 env = mon_get_cpu();
1250 if (!env)
1251 return;
1253 if (!(env->cr[0] & CR0_PG_MASK)) {
1254 term_printf("PG disabled\n");
1255 return;
1257 pgd = env->cr[3] & ~0xfff;
1258 last_prot = 0;
1259 start = -1;
1260 for(l1 = 0; l1 < 1024; l1++) {
1261 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1262 pde = le32_to_cpu(pde);
1263 end = l1 << 22;
1264 if (pde & PG_PRESENT_MASK) {
1265 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1266 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1267 mem_print(&start, &last_prot, end, prot);
1268 } else {
1269 for(l2 = 0; l2 < 1024; l2++) {
1270 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1271 (uint8_t *)&pte, 4);
1272 pte = le32_to_cpu(pte);
1273 end = (l1 << 22) + (l2 << 12);
1274 if (pte & PG_PRESENT_MASK) {
1275 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1276 } else {
1277 prot = 0;
1279 mem_print(&start, &last_prot, end, prot);
1282 } else {
1283 prot = 0;
1284 mem_print(&start, &last_prot, end, prot);
1288 #endif
1290 #if defined(TARGET_SH4)
1292 static void print_tlb(int idx, tlb_t *tlb)
1294 term_printf(" tlb%i:\t"
1295 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1296 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1297 "dirty=%hhu writethrough=%hhu\n",
1298 idx,
1299 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1300 tlb->v, tlb->sh, tlb->c, tlb->pr,
1301 tlb->d, tlb->wt);
1304 static void tlb_info(void)
1306 CPUState *env = mon_get_cpu();
1307 int i;
1309 term_printf ("ITLB:\n");
1310 for (i = 0 ; i < ITLB_SIZE ; i++)
1311 print_tlb (i, &env->itlb[i]);
1312 term_printf ("UTLB:\n");
1313 for (i = 0 ; i < UTLB_SIZE ; i++)
1314 print_tlb (i, &env->utlb[i]);
1317 #endif
1319 static void do_info_kqemu(void)
1321 #ifdef USE_KQEMU
1322 CPUState *env;
1323 int val;
1324 val = 0;
1325 env = mon_get_cpu();
1326 if (!env) {
1327 term_printf("No cpu initialized yet");
1328 return;
1330 val = env->kqemu_enabled;
1331 term_printf("kqemu support: ");
1332 switch(val) {
1333 default:
1334 case 0:
1335 term_printf("disabled\n");
1336 break;
1337 case 1:
1338 term_printf("enabled for user code\n");
1339 break;
1340 case 2:
1341 term_printf("enabled for user and kernel code\n");
1342 break;
1344 #else
1345 term_printf("kqemu support: not compiled\n");
1346 #endif
1349 static void do_info_kvm(void)
1351 #if defined(USE_KVM) || defined(CONFIG_KVM)
1352 term_printf("kvm support: ");
1353 if (kvm_enabled())
1354 term_printf("enabled\n");
1355 else
1356 term_printf("disabled\n");
1357 #else
1358 term_printf("kvm support: not compiled\n");
1359 #endif
1362 #ifdef CONFIG_PROFILER
1364 int64_t kqemu_time;
1365 int64_t qemu_time;
1366 int64_t kqemu_exec_count;
1367 int64_t dev_time;
1368 int64_t kqemu_ret_int_count;
1369 int64_t kqemu_ret_excp_count;
1370 int64_t kqemu_ret_intr_count;
1372 static void do_info_profile(void)
1374 int64_t total;
1375 total = qemu_time;
1376 if (total == 0)
1377 total = 1;
1378 term_printf("async time %" PRId64 " (%0.3f)\n",
1379 dev_time, dev_time / (double)ticks_per_sec);
1380 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1381 qemu_time, qemu_time / (double)ticks_per_sec);
1382 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1383 kqemu_time, kqemu_time / (double)ticks_per_sec,
1384 kqemu_time / (double)total * 100.0,
1385 kqemu_exec_count,
1386 kqemu_ret_int_count,
1387 kqemu_ret_excp_count,
1388 kqemu_ret_intr_count);
1389 qemu_time = 0;
1390 kqemu_time = 0;
1391 kqemu_exec_count = 0;
1392 dev_time = 0;
1393 kqemu_ret_int_count = 0;
1394 kqemu_ret_excp_count = 0;
1395 kqemu_ret_intr_count = 0;
1396 #ifdef USE_KQEMU
1397 kqemu_record_dump();
1398 #endif
1400 #else
1401 static void do_info_profile(void)
1403 term_printf("Internal profiler not compiled\n");
1405 #endif
1407 /* Capture support */
1408 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1410 static void do_info_capture (void)
1412 int i;
1413 CaptureState *s;
1415 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1416 term_printf ("[%d]: ", i);
1417 s->ops.info (s->opaque);
1421 static void do_stop_capture (int n)
1423 int i;
1424 CaptureState *s;
1426 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1427 if (i == n) {
1428 s->ops.destroy (s->opaque);
1429 LIST_REMOVE (s, entries);
1430 qemu_free (s);
1431 return;
1436 #ifdef HAS_AUDIO
1437 static void do_wav_capture (const char *path,
1438 int has_freq, int freq,
1439 int has_bits, int bits,
1440 int has_channels, int nchannels)
1442 CaptureState *s;
1444 s = qemu_mallocz (sizeof (*s));
1446 freq = has_freq ? freq : 44100;
1447 bits = has_bits ? bits : 16;
1448 nchannels = has_channels ? nchannels : 2;
1450 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1451 term_printf ("Faied to add wave capture\n");
1452 qemu_free (s);
1454 LIST_INSERT_HEAD (&capture_head, s, entries);
1456 #endif
1458 #if defined(TARGET_I386)
1459 static void do_inject_nmi(int cpu_index)
1461 CPUState *env;
1463 for (env = first_cpu; env != NULL; env = env->next_cpu)
1464 if (env->cpu_index == cpu_index) {
1465 if (kvm_enabled())
1466 kvm_inject_interrupt(env, CPU_INTERRUPT_NMI);
1467 else
1468 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1469 break;
1472 #endif
1474 static void do_info_status(void)
1476 if (vm_running)
1477 term_printf("VM status: running\n");
1478 else
1479 term_printf("VM status: paused\n");
1483 static void do_balloon(int value)
1485 ram_addr_t target = value;
1486 qemu_balloon(target << 20);
1489 static void do_info_balloon(void)
1491 ram_addr_t actual;
1493 actual = qemu_balloon_status();
1494 if (kvm_enabled() && !kvm_has_sync_mmu())
1495 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1496 else if (actual == 0)
1497 term_printf("Ballooning not activated in VM\n");
1498 else
1499 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1502 static void do_acl(const char *command,
1503 const char *aclname,
1504 const char *match,
1505 int has_index,
1506 int index)
1508 qemu_acl *acl;
1510 acl = qemu_acl_find(aclname);
1511 if (!acl) {
1512 term_printf("acl: unknown list '%s'\n", aclname);
1513 return;
1516 if (strcmp(command, "show") == 0) {
1517 int i = 0;
1518 qemu_acl_entry *entry;
1519 term_printf("policy: %s\n",
1520 acl->defaultDeny ? "deny" : "allow");
1521 TAILQ_FOREACH(entry, &acl->entries, next) {
1522 i++;
1523 term_printf("%d: %s %s\n", i,
1524 entry->deny ? "deny" : "allow",
1525 entry->match);
1527 } else if (strcmp(command, "reset") == 0) {
1528 qemu_acl_reset(acl);
1529 term_printf("acl: removed all rules\n");
1530 } else if (strcmp(command, "policy") == 0) {
1531 if (!match) {
1532 term_printf("acl: missing policy parameter\n");
1533 return;
1536 if (strcmp(match, "allow") == 0) {
1537 acl->defaultDeny = 0;
1538 term_printf("acl: policy set to 'allow'\n");
1539 } else if (strcmp(match, "deny") == 0) {
1540 acl->defaultDeny = 1;
1541 term_printf("acl: policy set to 'deny'\n");
1542 } else {
1543 term_printf("acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1545 } else if ((strcmp(command, "allow") == 0) ||
1546 (strcmp(command, "deny") == 0)) {
1547 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1548 int ret;
1550 if (!match) {
1551 term_printf("acl: missing match parameter\n");
1552 return;
1555 if (has_index)
1556 ret = qemu_acl_insert(acl, deny, match, index);
1557 else
1558 ret = qemu_acl_append(acl, deny, match);
1559 if (ret < 0)
1560 term_printf("acl: unable to add acl entry\n");
1561 else
1562 term_printf("acl: added rule at position %d\n", ret);
1563 } else if (strcmp(command, "remove") == 0) {
1564 int ret;
1566 if (!match) {
1567 term_printf("acl: missing match parameter\n");
1568 return;
1571 ret = qemu_acl_remove(acl, match);
1572 if (ret < 0)
1573 term_printf("acl: no matching acl entry\n");
1574 else
1575 term_printf("acl: removed rule at position %d\n", ret);
1576 } else {
1577 term_printf("acl: unknown command '%s'\n", command);
1581 /* Please update qemu-doc.texi when adding or changing commands */
1582 static const term_cmd_t term_cmds[] = {
1583 { "help|?", "s?", do_help,
1584 "[cmd]", "show the help" },
1585 { "commit", "s", do_commit,
1586 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1587 { "info", "s?", do_info,
1588 "subcommand", "show various information about the system state" },
1589 { "q|quit", "", do_quit,
1590 "", "quit the emulator" },
1591 { "eject", "-fB", do_eject,
1592 "[-f] device", "eject a removable medium (use -f to force it)" },
1593 { "change", "BFs?", do_change,
1594 "device filename [format]", "change a removable medium, optional format" },
1595 { "screendump", "F", do_screen_dump,
1596 "filename", "save screen into PPM image 'filename'" },
1597 { "logfile", "F", do_logfile,
1598 "filename", "output logs to 'filename'" },
1599 { "log", "s", do_log,
1600 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1601 { "savevm", "s?", do_savevm,
1602 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1603 { "loadvm", "s", do_loadvm,
1604 "tag|id", "restore a VM snapshot from its tag or id" },
1605 { "delvm", "s", do_delvm,
1606 "tag|id", "delete a VM snapshot from its tag or id" },
1607 { "stop", "", do_stop,
1608 "", "stop emulation", },
1609 { "c|cont", "", do_cont,
1610 "", "resume emulation", },
1611 #ifdef CONFIG_GDBSTUB
1612 { "gdbserver", "s?", do_gdbserver,
1613 "[port]", "start gdbserver session (default port=1234)", },
1614 #endif
1615 { "x", "/l", do_memory_dump,
1616 "/fmt addr", "virtual memory dump starting at 'addr'", },
1617 { "xp", "/l", do_physical_memory_dump,
1618 "/fmt addr", "physical memory dump starting at 'addr'", },
1619 { "p|print", "/l", do_print,
1620 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1621 { "i", "/ii.", do_ioport_read,
1622 "/fmt addr", "I/O port read" },
1624 { "sendkey", "si?", do_sendkey,
1625 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1626 { "system_reset", "", do_system_reset,
1627 "", "reset the system" },
1628 { "system_powerdown", "", do_system_powerdown,
1629 "", "send system power down event" },
1630 { "sum", "ii", do_sum,
1631 "addr size", "compute the checksum of a memory region" },
1632 { "usb_add", "s", do_usb_add,
1633 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1634 { "usb_del", "s", do_usb_del,
1635 "device", "remove USB device 'bus.addr'" },
1636 { "cpu", "i", do_cpu_set,
1637 "index", "set the default CPU" },
1638 { "mouse_move", "sss?", do_mouse_move,
1639 "dx dy [dz]", "send mouse move events" },
1640 { "mouse_button", "i", do_mouse_button,
1641 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1642 { "mouse_set", "i", do_mouse_set,
1643 "index", "set which mouse device receives events" },
1644 #ifdef HAS_AUDIO
1645 { "wavcapture", "si?i?i?", do_wav_capture,
1646 "path [frequency bits channels]",
1647 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1648 #endif
1649 { "stopcapture", "i", do_stop_capture,
1650 "capture index", "stop capture" },
1651 { "memsave", "lis", do_memory_save,
1652 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1653 { "pmemsave", "lis", do_physical_memory_save,
1654 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1655 { "boot_set", "s", do_boot_set,
1656 "bootdevice", "define new values for the boot device list" },
1657 #if defined(TARGET_I386)
1658 { "nmi", "i", do_inject_nmi,
1659 "cpu", "inject an NMI on the given CPU", },
1660 #endif
1661 { "migrate", "-ds", do_migrate,
1662 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1663 { "migrate_cancel", "", do_migrate_cancel,
1664 "", "cancel the current VM migration" },
1665 { "migrate_set_speed", "s", do_migrate_set_speed,
1666 "value", "set maximum speed (in bytes) for migrations" },
1667 #if defined(TARGET_I386)
1668 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1669 "[file=file][,if=type][,bus=n]\n"
1670 "[,unit=m][,media=d][index=i]\n"
1671 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1672 "[snapshot=on|off][,cache=on|off]",
1673 "add drive to PCI storage controller" },
1674 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]", "hot-add PCI device" },
1675 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1676 { "host_net_add", "ss", net_host_device_add,
1677 "[tap,user,socket,vde] options", "add host VLAN client" },
1678 { "host_net_remove", "is", net_host_device_remove,
1679 "vlan_id name", "remove host VLAN client" },
1680 #endif
1681 { "balloon", "i", do_balloon,
1682 "target", "request VM to change it's memory allocation (in MB)" },
1683 { "set_link", "ss", do_set_link,
1684 "name [up|down]", "change the link status of a network adapter" },
1685 { "set_link", "ss", do_set_link, "name [up|down]" },
1686 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1687 "acl show vnc.username\n"
1688 "acl policy vnc.username deny\n"
1689 "acl allow vnc.username fred\n"
1690 "acl deny vnc.username bob\n"
1691 "acl reset vnc.username\n" },
1692 { "cpu_set", "is", do_cpu_set_nr, "cpu [online|offline]", "change cpu state" },
1693 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1694 { "drive_add", "iss", drive_hot_add, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1695 "[,unit=m][,media=d][index=i]\n"
1696 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1697 "[snapshot=on|off][,cache=on|off]",
1698 "add drive to PCI storage controller" },
1699 #endif
1701 { NULL, NULL, },
1704 /* Please update qemu-doc.texi when adding or changing commands */
1705 static const term_cmd_t info_cmds[] = {
1706 { "version", "", do_info_version,
1707 "", "show the version of QEMU" },
1708 { "network", "", do_info_network,
1709 "", "show the network state" },
1710 { "chardev", "", qemu_chr_info,
1711 "", "show the character devices" },
1712 { "block", "", do_info_block,
1713 "", "show the block devices" },
1714 { "blockstats", "", do_info_blockstats,
1715 "", "show block device statistics" },
1716 { "registers", "", do_info_registers,
1717 "", "show the cpu registers" },
1718 { "cpus", "", do_info_cpus,
1719 "", "show infos for each CPU" },
1720 { "history", "", do_info_history,
1721 "", "show the command line history", },
1722 { "irq", "", irq_info,
1723 "", "show the interrupts statistics (if available)", },
1724 { "pic", "", pic_info,
1725 "", "show i8259 (PIC) state", },
1726 { "pci", "", pci_info,
1727 "", "show PCI info", },
1728 #if defined(TARGET_I386) || defined(TARGET_SH4)
1729 { "tlb", "", tlb_info,
1730 "", "show virtual to physical memory mappings", },
1731 #endif
1732 #if defined(TARGET_I386)
1733 { "mem", "", mem_info,
1734 "", "show the active virtual memory mappings", },
1735 { "hpet", "", do_info_hpet,
1736 "", "show state of HPET", },
1737 #endif
1738 { "jit", "", do_info_jit,
1739 "", "show dynamic compiler info", },
1740 { "kqemu", "", do_info_kqemu,
1741 "", "show KQEMU information", },
1742 { "kvm", "", do_info_kvm,
1743 "", "show KVM information", },
1744 { "usb", "", usb_info,
1745 "", "show guest USB devices", },
1746 { "usbhost", "", usb_host_info,
1747 "", "show host USB devices", },
1748 { "profile", "", do_info_profile,
1749 "", "show profiling information", },
1750 { "capture", "", do_info_capture,
1751 "", "show capture information" },
1752 { "snapshots", "", do_info_snapshots,
1753 "", "show the currently saved VM snapshots" },
1754 { "status", "", do_info_status,
1755 "", "show the current VM status (running|paused)" },
1756 { "pcmcia", "", pcmcia_info,
1757 "", "show guest PCMCIA status" },
1758 { "mice", "", do_info_mice,
1759 "", "show which guest mouse is receiving events" },
1760 { "vnc", "", do_info_vnc,
1761 "", "show the vnc server status"},
1762 { "name", "", do_info_name,
1763 "", "show the current VM name" },
1764 { "uuid", "", do_info_uuid,
1765 "", "show the current VM UUID" },
1766 #if defined(TARGET_PPC)
1767 { "cpustats", "", do_info_cpu_stats,
1768 "", "show CPU statistics", },
1769 #endif
1770 #if defined(CONFIG_SLIRP)
1771 { "slirp", "", do_info_slirp,
1772 "", "show SLIRP statistics", },
1773 #endif
1774 { "migrate", "", do_info_migrate, "", "show migration status" },
1775 { "balloon", "", do_info_balloon,
1776 "", "show balloon information" },
1777 { NULL, NULL, },
1780 /*******************************************************************/
1782 static const char *pch;
1783 static jmp_buf expr_env;
1785 #define MD_TLONG 0
1786 #define MD_I32 1
1788 typedef struct MonitorDef {
1789 const char *name;
1790 int offset;
1791 target_long (*get_value)(const struct MonitorDef *md, int val);
1792 int type;
1793 } MonitorDef;
1795 #if defined(TARGET_I386)
1796 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1798 CPUState *env = mon_get_cpu();
1799 if (!env)
1800 return 0;
1801 return env->eip + env->segs[R_CS].base;
1803 #endif
1805 #if defined(TARGET_PPC)
1806 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1808 CPUState *env = mon_get_cpu();
1809 unsigned int u;
1810 int i;
1812 if (!env)
1813 return 0;
1815 u = 0;
1816 for (i = 0; i < 8; i++)
1817 u |= env->crf[i] << (32 - (4 * i));
1819 return u;
1822 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1824 CPUState *env = mon_get_cpu();
1825 if (!env)
1826 return 0;
1827 return env->msr;
1830 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1832 CPUState *env = mon_get_cpu();
1833 if (!env)
1834 return 0;
1835 return env->xer;
1838 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1840 CPUState *env = mon_get_cpu();
1841 if (!env)
1842 return 0;
1843 return cpu_ppc_load_decr(env);
1846 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1848 CPUState *env = mon_get_cpu();
1849 if (!env)
1850 return 0;
1851 return cpu_ppc_load_tbu(env);
1854 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1856 CPUState *env = mon_get_cpu();
1857 if (!env)
1858 return 0;
1859 return cpu_ppc_load_tbl(env);
1861 #endif
1863 #if defined(TARGET_SPARC)
1864 #ifndef TARGET_SPARC64
1865 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1867 CPUState *env = mon_get_cpu();
1868 if (!env)
1869 return 0;
1870 return GET_PSR(env);
1872 #endif
1874 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1876 CPUState *env = mon_get_cpu();
1877 if (!env)
1878 return 0;
1879 return env->regwptr[val];
1881 #endif
1883 static const MonitorDef monitor_defs[] = {
1884 #ifdef TARGET_I386
1886 #define SEG(name, seg) \
1887 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1888 { name ".base", offsetof(CPUState, segs[seg].base) },\
1889 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1891 { "eax", offsetof(CPUState, regs[0]) },
1892 { "ecx", offsetof(CPUState, regs[1]) },
1893 { "edx", offsetof(CPUState, regs[2]) },
1894 { "ebx", offsetof(CPUState, regs[3]) },
1895 { "esp|sp", offsetof(CPUState, regs[4]) },
1896 { "ebp|fp", offsetof(CPUState, regs[5]) },
1897 { "esi", offsetof(CPUState, regs[6]) },
1898 { "edi", offsetof(CPUState, regs[7]) },
1899 #ifdef TARGET_X86_64
1900 { "r8", offsetof(CPUState, regs[8]) },
1901 { "r9", offsetof(CPUState, regs[9]) },
1902 { "r10", offsetof(CPUState, regs[10]) },
1903 { "r11", offsetof(CPUState, regs[11]) },
1904 { "r12", offsetof(CPUState, regs[12]) },
1905 { "r13", offsetof(CPUState, regs[13]) },
1906 { "r14", offsetof(CPUState, regs[14]) },
1907 { "r15", offsetof(CPUState, regs[15]) },
1908 #endif
1909 { "eflags", offsetof(CPUState, eflags) },
1910 { "eip", offsetof(CPUState, eip) },
1911 SEG("cs", R_CS)
1912 SEG("ds", R_DS)
1913 SEG("es", R_ES)
1914 SEG("ss", R_SS)
1915 SEG("fs", R_FS)
1916 SEG("gs", R_GS)
1917 { "pc", 0, monitor_get_pc, },
1918 #elif defined(TARGET_PPC)
1919 /* General purpose registers */
1920 { "r0", offsetof(CPUState, gpr[0]) },
1921 { "r1", offsetof(CPUState, gpr[1]) },
1922 { "r2", offsetof(CPUState, gpr[2]) },
1923 { "r3", offsetof(CPUState, gpr[3]) },
1924 { "r4", offsetof(CPUState, gpr[4]) },
1925 { "r5", offsetof(CPUState, gpr[5]) },
1926 { "r6", offsetof(CPUState, gpr[6]) },
1927 { "r7", offsetof(CPUState, gpr[7]) },
1928 { "r8", offsetof(CPUState, gpr[8]) },
1929 { "r9", offsetof(CPUState, gpr[9]) },
1930 { "r10", offsetof(CPUState, gpr[10]) },
1931 { "r11", offsetof(CPUState, gpr[11]) },
1932 { "r12", offsetof(CPUState, gpr[12]) },
1933 { "r13", offsetof(CPUState, gpr[13]) },
1934 { "r14", offsetof(CPUState, gpr[14]) },
1935 { "r15", offsetof(CPUState, gpr[15]) },
1936 { "r16", offsetof(CPUState, gpr[16]) },
1937 { "r17", offsetof(CPUState, gpr[17]) },
1938 { "r18", offsetof(CPUState, gpr[18]) },
1939 { "r19", offsetof(CPUState, gpr[19]) },
1940 { "r20", offsetof(CPUState, gpr[20]) },
1941 { "r21", offsetof(CPUState, gpr[21]) },
1942 { "r22", offsetof(CPUState, gpr[22]) },
1943 { "r23", offsetof(CPUState, gpr[23]) },
1944 { "r24", offsetof(CPUState, gpr[24]) },
1945 { "r25", offsetof(CPUState, gpr[25]) },
1946 { "r26", offsetof(CPUState, gpr[26]) },
1947 { "r27", offsetof(CPUState, gpr[27]) },
1948 { "r28", offsetof(CPUState, gpr[28]) },
1949 { "r29", offsetof(CPUState, gpr[29]) },
1950 { "r30", offsetof(CPUState, gpr[30]) },
1951 { "r31", offsetof(CPUState, gpr[31]) },
1952 /* Floating point registers */
1953 { "f0", offsetof(CPUState, fpr[0]) },
1954 { "f1", offsetof(CPUState, fpr[1]) },
1955 { "f2", offsetof(CPUState, fpr[2]) },
1956 { "f3", offsetof(CPUState, fpr[3]) },
1957 { "f4", offsetof(CPUState, fpr[4]) },
1958 { "f5", offsetof(CPUState, fpr[5]) },
1959 { "f6", offsetof(CPUState, fpr[6]) },
1960 { "f7", offsetof(CPUState, fpr[7]) },
1961 { "f8", offsetof(CPUState, fpr[8]) },
1962 { "f9", offsetof(CPUState, fpr[9]) },
1963 { "f10", offsetof(CPUState, fpr[10]) },
1964 { "f11", offsetof(CPUState, fpr[11]) },
1965 { "f12", offsetof(CPUState, fpr[12]) },
1966 { "f13", offsetof(CPUState, fpr[13]) },
1967 { "f14", offsetof(CPUState, fpr[14]) },
1968 { "f15", offsetof(CPUState, fpr[15]) },
1969 { "f16", offsetof(CPUState, fpr[16]) },
1970 { "f17", offsetof(CPUState, fpr[17]) },
1971 { "f18", offsetof(CPUState, fpr[18]) },
1972 { "f19", offsetof(CPUState, fpr[19]) },
1973 { "f20", offsetof(CPUState, fpr[20]) },
1974 { "f21", offsetof(CPUState, fpr[21]) },
1975 { "f22", offsetof(CPUState, fpr[22]) },
1976 { "f23", offsetof(CPUState, fpr[23]) },
1977 { "f24", offsetof(CPUState, fpr[24]) },
1978 { "f25", offsetof(CPUState, fpr[25]) },
1979 { "f26", offsetof(CPUState, fpr[26]) },
1980 { "f27", offsetof(CPUState, fpr[27]) },
1981 { "f28", offsetof(CPUState, fpr[28]) },
1982 { "f29", offsetof(CPUState, fpr[29]) },
1983 { "f30", offsetof(CPUState, fpr[30]) },
1984 { "f31", offsetof(CPUState, fpr[31]) },
1985 { "fpscr", offsetof(CPUState, fpscr) },
1986 /* Next instruction pointer */
1987 { "nip|pc", offsetof(CPUState, nip) },
1988 { "lr", offsetof(CPUState, lr) },
1989 { "ctr", offsetof(CPUState, ctr) },
1990 { "decr", 0, &monitor_get_decr, },
1991 { "ccr", 0, &monitor_get_ccr, },
1992 /* Machine state register */
1993 { "msr", 0, &monitor_get_msr, },
1994 { "xer", 0, &monitor_get_xer, },
1995 { "tbu", 0, &monitor_get_tbu, },
1996 { "tbl", 0, &monitor_get_tbl, },
1997 #if defined(TARGET_PPC64)
1998 /* Address space register */
1999 { "asr", offsetof(CPUState, asr) },
2000 #endif
2001 /* Segment registers */
2002 { "sdr1", offsetof(CPUState, sdr1) },
2003 { "sr0", offsetof(CPUState, sr[0]) },
2004 { "sr1", offsetof(CPUState, sr[1]) },
2005 { "sr2", offsetof(CPUState, sr[2]) },
2006 { "sr3", offsetof(CPUState, sr[3]) },
2007 { "sr4", offsetof(CPUState, sr[4]) },
2008 { "sr5", offsetof(CPUState, sr[5]) },
2009 { "sr6", offsetof(CPUState, sr[6]) },
2010 { "sr7", offsetof(CPUState, sr[7]) },
2011 { "sr8", offsetof(CPUState, sr[8]) },
2012 { "sr9", offsetof(CPUState, sr[9]) },
2013 { "sr10", offsetof(CPUState, sr[10]) },
2014 { "sr11", offsetof(CPUState, sr[11]) },
2015 { "sr12", offsetof(CPUState, sr[12]) },
2016 { "sr13", offsetof(CPUState, sr[13]) },
2017 { "sr14", offsetof(CPUState, sr[14]) },
2018 { "sr15", offsetof(CPUState, sr[15]) },
2019 /* Too lazy to put BATs and SPRs ... */
2020 #elif defined(TARGET_SPARC)
2021 { "g0", offsetof(CPUState, gregs[0]) },
2022 { "g1", offsetof(CPUState, gregs[1]) },
2023 { "g2", offsetof(CPUState, gregs[2]) },
2024 { "g3", offsetof(CPUState, gregs[3]) },
2025 { "g4", offsetof(CPUState, gregs[4]) },
2026 { "g5", offsetof(CPUState, gregs[5]) },
2027 { "g6", offsetof(CPUState, gregs[6]) },
2028 { "g7", offsetof(CPUState, gregs[7]) },
2029 { "o0", 0, monitor_get_reg },
2030 { "o1", 1, monitor_get_reg },
2031 { "o2", 2, monitor_get_reg },
2032 { "o3", 3, monitor_get_reg },
2033 { "o4", 4, monitor_get_reg },
2034 { "o5", 5, monitor_get_reg },
2035 { "o6", 6, monitor_get_reg },
2036 { "o7", 7, monitor_get_reg },
2037 { "l0", 8, monitor_get_reg },
2038 { "l1", 9, monitor_get_reg },
2039 { "l2", 10, monitor_get_reg },
2040 { "l3", 11, monitor_get_reg },
2041 { "l4", 12, monitor_get_reg },
2042 { "l5", 13, monitor_get_reg },
2043 { "l6", 14, monitor_get_reg },
2044 { "l7", 15, monitor_get_reg },
2045 { "i0", 16, monitor_get_reg },
2046 { "i1", 17, monitor_get_reg },
2047 { "i2", 18, monitor_get_reg },
2048 { "i3", 19, monitor_get_reg },
2049 { "i4", 20, monitor_get_reg },
2050 { "i5", 21, monitor_get_reg },
2051 { "i6", 22, monitor_get_reg },
2052 { "i7", 23, monitor_get_reg },
2053 { "pc", offsetof(CPUState, pc) },
2054 { "npc", offsetof(CPUState, npc) },
2055 { "y", offsetof(CPUState, y) },
2056 #ifndef TARGET_SPARC64
2057 { "psr", 0, &monitor_get_psr, },
2058 { "wim", offsetof(CPUState, wim) },
2059 #endif
2060 { "tbr", offsetof(CPUState, tbr) },
2061 { "fsr", offsetof(CPUState, fsr) },
2062 { "f0", offsetof(CPUState, fpr[0]) },
2063 { "f1", offsetof(CPUState, fpr[1]) },
2064 { "f2", offsetof(CPUState, fpr[2]) },
2065 { "f3", offsetof(CPUState, fpr[3]) },
2066 { "f4", offsetof(CPUState, fpr[4]) },
2067 { "f5", offsetof(CPUState, fpr[5]) },
2068 { "f6", offsetof(CPUState, fpr[6]) },
2069 { "f7", offsetof(CPUState, fpr[7]) },
2070 { "f8", offsetof(CPUState, fpr[8]) },
2071 { "f9", offsetof(CPUState, fpr[9]) },
2072 { "f10", offsetof(CPUState, fpr[10]) },
2073 { "f11", offsetof(CPUState, fpr[11]) },
2074 { "f12", offsetof(CPUState, fpr[12]) },
2075 { "f13", offsetof(CPUState, fpr[13]) },
2076 { "f14", offsetof(CPUState, fpr[14]) },
2077 { "f15", offsetof(CPUState, fpr[15]) },
2078 { "f16", offsetof(CPUState, fpr[16]) },
2079 { "f17", offsetof(CPUState, fpr[17]) },
2080 { "f18", offsetof(CPUState, fpr[18]) },
2081 { "f19", offsetof(CPUState, fpr[19]) },
2082 { "f20", offsetof(CPUState, fpr[20]) },
2083 { "f21", offsetof(CPUState, fpr[21]) },
2084 { "f22", offsetof(CPUState, fpr[22]) },
2085 { "f23", offsetof(CPUState, fpr[23]) },
2086 { "f24", offsetof(CPUState, fpr[24]) },
2087 { "f25", offsetof(CPUState, fpr[25]) },
2088 { "f26", offsetof(CPUState, fpr[26]) },
2089 { "f27", offsetof(CPUState, fpr[27]) },
2090 { "f28", offsetof(CPUState, fpr[28]) },
2091 { "f29", offsetof(CPUState, fpr[29]) },
2092 { "f30", offsetof(CPUState, fpr[30]) },
2093 { "f31", offsetof(CPUState, fpr[31]) },
2094 #ifdef TARGET_SPARC64
2095 { "f32", offsetof(CPUState, fpr[32]) },
2096 { "f34", offsetof(CPUState, fpr[34]) },
2097 { "f36", offsetof(CPUState, fpr[36]) },
2098 { "f38", offsetof(CPUState, fpr[38]) },
2099 { "f40", offsetof(CPUState, fpr[40]) },
2100 { "f42", offsetof(CPUState, fpr[42]) },
2101 { "f44", offsetof(CPUState, fpr[44]) },
2102 { "f46", offsetof(CPUState, fpr[46]) },
2103 { "f48", offsetof(CPUState, fpr[48]) },
2104 { "f50", offsetof(CPUState, fpr[50]) },
2105 { "f52", offsetof(CPUState, fpr[52]) },
2106 { "f54", offsetof(CPUState, fpr[54]) },
2107 { "f56", offsetof(CPUState, fpr[56]) },
2108 { "f58", offsetof(CPUState, fpr[58]) },
2109 { "f60", offsetof(CPUState, fpr[60]) },
2110 { "f62", offsetof(CPUState, fpr[62]) },
2111 { "asi", offsetof(CPUState, asi) },
2112 { "pstate", offsetof(CPUState, pstate) },
2113 { "cansave", offsetof(CPUState, cansave) },
2114 { "canrestore", offsetof(CPUState, canrestore) },
2115 { "otherwin", offsetof(CPUState, otherwin) },
2116 { "wstate", offsetof(CPUState, wstate) },
2117 { "cleanwin", offsetof(CPUState, cleanwin) },
2118 { "fprs", offsetof(CPUState, fprs) },
2119 #endif
2120 #endif
2121 { NULL },
2124 static void expr_error(const char *msg)
2126 term_printf("%s\n", msg);
2127 longjmp(expr_env, 1);
2130 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2131 static int get_monitor_def(target_long *pval, const char *name)
2133 const MonitorDef *md;
2134 void *ptr;
2136 for(md = monitor_defs; md->name != NULL; md++) {
2137 if (compare_cmd(name, md->name)) {
2138 if (md->get_value) {
2139 *pval = md->get_value(md, md->offset);
2140 } else {
2141 CPUState *env = mon_get_cpu();
2142 if (!env)
2143 return -2;
2144 ptr = (uint8_t *)env + md->offset;
2145 switch(md->type) {
2146 case MD_I32:
2147 *pval = *(int32_t *)ptr;
2148 break;
2149 case MD_TLONG:
2150 *pval = *(target_long *)ptr;
2151 break;
2152 default:
2153 *pval = 0;
2154 break;
2157 return 0;
2160 return -1;
2163 static void next(void)
2165 if (pch != '\0') {
2166 pch++;
2167 while (qemu_isspace(*pch))
2168 pch++;
2172 static int64_t expr_sum(void);
2174 static int64_t expr_unary(void)
2176 int64_t n;
2177 char *p;
2178 int ret;
2180 switch(*pch) {
2181 case '+':
2182 next();
2183 n = expr_unary();
2184 break;
2185 case '-':
2186 next();
2187 n = -expr_unary();
2188 break;
2189 case '~':
2190 next();
2191 n = ~expr_unary();
2192 break;
2193 case '(':
2194 next();
2195 n = expr_sum();
2196 if (*pch != ')') {
2197 expr_error("')' expected");
2199 next();
2200 break;
2201 case '\'':
2202 pch++;
2203 if (*pch == '\0')
2204 expr_error("character constant expected");
2205 n = *pch;
2206 pch++;
2207 if (*pch != '\'')
2208 expr_error("missing terminating \' character");
2209 next();
2210 break;
2211 case '$':
2213 char buf[128], *q;
2214 target_long reg=0;
2216 pch++;
2217 q = buf;
2218 while ((*pch >= 'a' && *pch <= 'z') ||
2219 (*pch >= 'A' && *pch <= 'Z') ||
2220 (*pch >= '0' && *pch <= '9') ||
2221 *pch == '_' || *pch == '.') {
2222 if ((q - buf) < sizeof(buf) - 1)
2223 *q++ = *pch;
2224 pch++;
2226 while (qemu_isspace(*pch))
2227 pch++;
2228 *q = 0;
2229 ret = get_monitor_def(&reg, buf);
2230 if (ret == -1)
2231 expr_error("unknown register");
2232 else if (ret == -2)
2233 expr_error("no cpu defined");
2234 n = reg;
2236 break;
2237 case '\0':
2238 expr_error("unexpected end of expression");
2239 n = 0;
2240 break;
2241 default:
2242 #if TARGET_PHYS_ADDR_BITS > 32
2243 n = strtoull(pch, &p, 0);
2244 #else
2245 n = strtoul(pch, &p, 0);
2246 #endif
2247 if (pch == p) {
2248 expr_error("invalid char in expression");
2250 pch = p;
2251 while (qemu_isspace(*pch))
2252 pch++;
2253 break;
2255 return n;
2259 static int64_t expr_prod(void)
2261 int64_t val, val2;
2262 int op;
2264 val = expr_unary();
2265 for(;;) {
2266 op = *pch;
2267 if (op != '*' && op != '/' && op != '%')
2268 break;
2269 next();
2270 val2 = expr_unary();
2271 switch(op) {
2272 default:
2273 case '*':
2274 val *= val2;
2275 break;
2276 case '/':
2277 case '%':
2278 if (val2 == 0)
2279 expr_error("division by zero");
2280 if (op == '/')
2281 val /= val2;
2282 else
2283 val %= val2;
2284 break;
2287 return val;
2290 static int64_t expr_logic(void)
2292 int64_t val, val2;
2293 int op;
2295 val = expr_prod();
2296 for(;;) {
2297 op = *pch;
2298 if (op != '&' && op != '|' && op != '^')
2299 break;
2300 next();
2301 val2 = expr_prod();
2302 switch(op) {
2303 default:
2304 case '&':
2305 val &= val2;
2306 break;
2307 case '|':
2308 val |= val2;
2309 break;
2310 case '^':
2311 val ^= val2;
2312 break;
2315 return val;
2318 static int64_t expr_sum(void)
2320 int64_t val, val2;
2321 int op;
2323 val = expr_logic();
2324 for(;;) {
2325 op = *pch;
2326 if (op != '+' && op != '-')
2327 break;
2328 next();
2329 val2 = expr_logic();
2330 if (op == '+')
2331 val += val2;
2332 else
2333 val -= val2;
2335 return val;
2338 static int get_expr(int64_t *pval, const char **pp)
2340 pch = *pp;
2341 if (setjmp(expr_env)) {
2342 *pp = pch;
2343 return -1;
2345 while (qemu_isspace(*pch))
2346 pch++;
2347 *pval = expr_sum();
2348 *pp = pch;
2349 return 0;
2352 static int get_str(char *buf, int buf_size, const char **pp)
2354 const char *p;
2355 char *q;
2356 int c;
2358 q = buf;
2359 p = *pp;
2360 while (qemu_isspace(*p))
2361 p++;
2362 if (*p == '\0') {
2363 fail:
2364 *q = '\0';
2365 *pp = p;
2366 return -1;
2368 if (*p == '\"') {
2369 p++;
2370 while (*p != '\0' && *p != '\"') {
2371 if (*p == '\\') {
2372 p++;
2373 c = *p++;
2374 switch(c) {
2375 case 'n':
2376 c = '\n';
2377 break;
2378 case 'r':
2379 c = '\r';
2380 break;
2381 case '\\':
2382 case '\'':
2383 case '\"':
2384 break;
2385 default:
2386 qemu_printf("unsupported escape code: '\\%c'\n", c);
2387 goto fail;
2389 if ((q - buf) < buf_size - 1) {
2390 *q++ = c;
2392 } else {
2393 if ((q - buf) < buf_size - 1) {
2394 *q++ = *p;
2396 p++;
2399 if (*p != '\"') {
2400 qemu_printf("unterminated string\n");
2401 goto fail;
2403 p++;
2404 } else {
2405 while (*p != '\0' && !qemu_isspace(*p)) {
2406 if ((q - buf) < buf_size - 1) {
2407 *q++ = *p;
2409 p++;
2412 *q = '\0';
2413 *pp = p;
2414 return 0;
2417 static int default_fmt_format = 'x';
2418 static int default_fmt_size = 4;
2420 #define MAX_ARGS 16
2422 static void monitor_handle_command(const char *cmdline)
2424 const char *p, *pstart, *typestr;
2425 char *q;
2426 int c, nb_args, len, i, has_arg;
2427 const term_cmd_t *cmd;
2428 char cmdname[256];
2429 char buf[1024];
2430 void *str_allocated[MAX_ARGS];
2431 void *args[MAX_ARGS];
2432 void (*handler_0)(void);
2433 void (*handler_1)(void *arg0);
2434 void (*handler_2)(void *arg0, void *arg1);
2435 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2436 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2437 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2438 void *arg4);
2439 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2440 void *arg4, void *arg5);
2441 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2442 void *arg4, void *arg5, void *arg6);
2444 #ifdef DEBUG
2445 term_printf("command='%s'\n", cmdline);
2446 #endif
2448 /* extract the command name */
2449 p = cmdline;
2450 q = cmdname;
2451 while (qemu_isspace(*p))
2452 p++;
2453 if (*p == '\0')
2454 return;
2455 pstart = p;
2456 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2457 p++;
2458 len = p - pstart;
2459 if (len > sizeof(cmdname) - 1)
2460 len = sizeof(cmdname) - 1;
2461 memcpy(cmdname, pstart, len);
2462 cmdname[len] = '\0';
2464 /* find the command */
2465 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2466 if (compare_cmd(cmdname, cmd->name))
2467 goto found;
2469 term_printf("unknown command: '%s'\n", cmdname);
2470 return;
2471 found:
2473 for(i = 0; i < MAX_ARGS; i++)
2474 str_allocated[i] = NULL;
2476 /* parse the parameters */
2477 typestr = cmd->args_type;
2478 nb_args = 0;
2479 for(;;) {
2480 c = *typestr;
2481 if (c == '\0')
2482 break;
2483 typestr++;
2484 switch(c) {
2485 case 'F':
2486 case 'B':
2487 case 's':
2489 int ret;
2490 char *str;
2492 while (qemu_isspace(*p))
2493 p++;
2494 if (*typestr == '?') {
2495 typestr++;
2496 if (*p == '\0') {
2497 /* no optional string: NULL argument */
2498 str = NULL;
2499 goto add_str;
2502 ret = get_str(buf, sizeof(buf), &p);
2503 if (ret < 0) {
2504 switch(c) {
2505 case 'F':
2506 term_printf("%s: filename expected\n", cmdname);
2507 break;
2508 case 'B':
2509 term_printf("%s: block device name expected\n", cmdname);
2510 break;
2511 default:
2512 term_printf("%s: string expected\n", cmdname);
2513 break;
2515 goto fail;
2517 str = qemu_malloc(strlen(buf) + 1);
2518 pstrcpy(str, sizeof(buf), buf);
2519 str_allocated[nb_args] = str;
2520 add_str:
2521 if (nb_args >= MAX_ARGS) {
2522 error_args:
2523 term_printf("%s: too many arguments\n", cmdname);
2524 goto fail;
2526 args[nb_args++] = str;
2528 break;
2529 case '/':
2531 int count, format, size;
2533 while (qemu_isspace(*p))
2534 p++;
2535 if (*p == '/') {
2536 /* format found */
2537 p++;
2538 count = 1;
2539 if (qemu_isdigit(*p)) {
2540 count = 0;
2541 while (qemu_isdigit(*p)) {
2542 count = count * 10 + (*p - '0');
2543 p++;
2546 size = -1;
2547 format = -1;
2548 for(;;) {
2549 switch(*p) {
2550 case 'o':
2551 case 'd':
2552 case 'u':
2553 case 'x':
2554 case 'i':
2555 case 'c':
2556 format = *p++;
2557 break;
2558 case 'b':
2559 size = 1;
2560 p++;
2561 break;
2562 case 'h':
2563 size = 2;
2564 p++;
2565 break;
2566 case 'w':
2567 size = 4;
2568 p++;
2569 break;
2570 case 'g':
2571 case 'L':
2572 size = 8;
2573 p++;
2574 break;
2575 default:
2576 goto next;
2579 next:
2580 if (*p != '\0' && !qemu_isspace(*p)) {
2581 term_printf("invalid char in format: '%c'\n", *p);
2582 goto fail;
2584 if (format < 0)
2585 format = default_fmt_format;
2586 if (format != 'i') {
2587 /* for 'i', not specifying a size gives -1 as size */
2588 if (size < 0)
2589 size = default_fmt_size;
2590 default_fmt_size = size;
2592 default_fmt_format = format;
2593 } else {
2594 count = 1;
2595 format = default_fmt_format;
2596 if (format != 'i') {
2597 size = default_fmt_size;
2598 } else {
2599 size = -1;
2602 if (nb_args + 3 > MAX_ARGS)
2603 goto error_args;
2604 args[nb_args++] = (void*)(long)count;
2605 args[nb_args++] = (void*)(long)format;
2606 args[nb_args++] = (void*)(long)size;
2608 break;
2609 case 'i':
2610 case 'l':
2612 int64_t val;
2614 while (qemu_isspace(*p))
2615 p++;
2616 if (*typestr == '?' || *typestr == '.') {
2617 if (*typestr == '?') {
2618 if (*p == '\0')
2619 has_arg = 0;
2620 else
2621 has_arg = 1;
2622 } else {
2623 if (*p == '.') {
2624 p++;
2625 while (qemu_isspace(*p))
2626 p++;
2627 has_arg = 1;
2628 } else {
2629 has_arg = 0;
2632 typestr++;
2633 if (nb_args >= MAX_ARGS)
2634 goto error_args;
2635 args[nb_args++] = (void *)(long)has_arg;
2636 if (!has_arg) {
2637 if (nb_args >= MAX_ARGS)
2638 goto error_args;
2639 val = -1;
2640 goto add_num;
2643 if (get_expr(&val, &p))
2644 goto fail;
2645 add_num:
2646 if (c == 'i') {
2647 if (nb_args >= MAX_ARGS)
2648 goto error_args;
2649 args[nb_args++] = (void *)(long)val;
2650 } else {
2651 if ((nb_args + 1) >= MAX_ARGS)
2652 goto error_args;
2653 #if TARGET_PHYS_ADDR_BITS > 32
2654 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2655 #else
2656 args[nb_args++] = (void *)0;
2657 #endif
2658 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2661 break;
2662 case '-':
2664 int has_option;
2665 /* option */
2667 c = *typestr++;
2668 if (c == '\0')
2669 goto bad_type;
2670 while (qemu_isspace(*p))
2671 p++;
2672 has_option = 0;
2673 if (*p == '-') {
2674 p++;
2675 if (*p != c) {
2676 term_printf("%s: unsupported option -%c\n",
2677 cmdname, *p);
2678 goto fail;
2680 p++;
2681 has_option = 1;
2683 if (nb_args >= MAX_ARGS)
2684 goto error_args;
2685 args[nb_args++] = (void *)(long)has_option;
2687 break;
2688 default:
2689 bad_type:
2690 term_printf("%s: unknown type '%c'\n", cmdname, c);
2691 goto fail;
2694 /* check that all arguments were parsed */
2695 while (qemu_isspace(*p))
2696 p++;
2697 if (*p != '\0') {
2698 term_printf("%s: extraneous characters at the end of line\n",
2699 cmdname);
2700 goto fail;
2703 switch(nb_args) {
2704 case 0:
2705 handler_0 = cmd->handler;
2706 handler_0();
2707 break;
2708 case 1:
2709 handler_1 = cmd->handler;
2710 handler_1(args[0]);
2711 break;
2712 case 2:
2713 handler_2 = cmd->handler;
2714 handler_2(args[0], args[1]);
2715 break;
2716 case 3:
2717 handler_3 = cmd->handler;
2718 handler_3(args[0], args[1], args[2]);
2719 break;
2720 case 4:
2721 handler_4 = cmd->handler;
2722 handler_4(args[0], args[1], args[2], args[3]);
2723 break;
2724 case 5:
2725 handler_5 = cmd->handler;
2726 handler_5(args[0], args[1], args[2], args[3], args[4]);
2727 break;
2728 case 6:
2729 handler_6 = cmd->handler;
2730 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2731 break;
2732 case 7:
2733 handler_7 = cmd->handler;
2734 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2735 break;
2736 default:
2737 term_printf("unsupported number of arguments: %d\n", nb_args);
2738 goto fail;
2740 fail:
2741 for(i = 0; i < MAX_ARGS; i++)
2742 qemu_free(str_allocated[i]);
2743 return;
2746 static void cmd_completion(const char *name, const char *list)
2748 const char *p, *pstart;
2749 char cmd[128];
2750 int len;
2752 p = list;
2753 for(;;) {
2754 pstart = p;
2755 p = strchr(p, '|');
2756 if (!p)
2757 p = pstart + strlen(pstart);
2758 len = p - pstart;
2759 if (len > sizeof(cmd) - 2)
2760 len = sizeof(cmd) - 2;
2761 memcpy(cmd, pstart, len);
2762 cmd[len] = '\0';
2763 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2764 add_completion(cmd);
2766 if (*p == '\0')
2767 break;
2768 p++;
2772 static void file_completion(const char *input)
2774 DIR *ffs;
2775 struct dirent *d;
2776 char path[1024];
2777 char file[1024], file_prefix[1024];
2778 int input_path_len;
2779 const char *p;
2781 p = strrchr(input, '/');
2782 if (!p) {
2783 input_path_len = 0;
2784 pstrcpy(file_prefix, sizeof(file_prefix), input);
2785 pstrcpy(path, sizeof(path), ".");
2786 } else {
2787 input_path_len = p - input + 1;
2788 memcpy(path, input, input_path_len);
2789 if (input_path_len > sizeof(path) - 1)
2790 input_path_len = sizeof(path) - 1;
2791 path[input_path_len] = '\0';
2792 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2794 #ifdef DEBUG_COMPLETION
2795 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2796 #endif
2797 ffs = opendir(path);
2798 if (!ffs)
2799 return;
2800 for(;;) {
2801 struct stat sb;
2802 d = readdir(ffs);
2803 if (!d)
2804 break;
2805 if (strstart(d->d_name, file_prefix, NULL)) {
2806 memcpy(file, input, input_path_len);
2807 if (input_path_len < sizeof(file))
2808 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2809 d->d_name);
2810 /* stat the file to find out if it's a directory.
2811 * In that case add a slash to speed up typing long paths
2813 stat(file, &sb);
2814 if(S_ISDIR(sb.st_mode))
2815 pstrcat(file, sizeof(file), "/");
2816 add_completion(file);
2819 closedir(ffs);
2822 static void block_completion_it(void *opaque, BlockDriverState *bs)
2824 const char *name = bdrv_get_device_name(bs);
2825 const char *input = opaque;
2827 if (input[0] == '\0' ||
2828 !strncmp(name, (char *)input, strlen(input))) {
2829 add_completion(name);
2833 /* NOTE: this parser is an approximate form of the real command parser */
2834 static void parse_cmdline(const char *cmdline,
2835 int *pnb_args, char **args)
2837 const char *p;
2838 int nb_args, ret;
2839 char buf[1024];
2841 p = cmdline;
2842 nb_args = 0;
2843 for(;;) {
2844 while (qemu_isspace(*p))
2845 p++;
2846 if (*p == '\0')
2847 break;
2848 if (nb_args >= MAX_ARGS)
2849 break;
2850 ret = get_str(buf, sizeof(buf), &p);
2851 args[nb_args] = qemu_strdup(buf);
2852 nb_args++;
2853 if (ret < 0)
2854 break;
2856 *pnb_args = nb_args;
2859 void readline_find_completion(const char *cmdline)
2861 const char *cmdname;
2862 char *args[MAX_ARGS];
2863 int nb_args, i, len;
2864 const char *ptype, *str;
2865 const term_cmd_t *cmd;
2866 const KeyDef *key;
2868 parse_cmdline(cmdline, &nb_args, args);
2869 #ifdef DEBUG_COMPLETION
2870 for(i = 0; i < nb_args; i++) {
2871 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2873 #endif
2875 /* if the line ends with a space, it means we want to complete the
2876 next arg */
2877 len = strlen(cmdline);
2878 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2879 if (nb_args >= MAX_ARGS)
2880 return;
2881 args[nb_args++] = qemu_strdup("");
2883 if (nb_args <= 1) {
2884 /* command completion */
2885 if (nb_args == 0)
2886 cmdname = "";
2887 else
2888 cmdname = args[0];
2889 completion_index = strlen(cmdname);
2890 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2891 cmd_completion(cmdname, cmd->name);
2893 } else {
2894 /* find the command */
2895 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2896 if (compare_cmd(args[0], cmd->name))
2897 goto found;
2899 return;
2900 found:
2901 ptype = cmd->args_type;
2902 for(i = 0; i < nb_args - 2; i++) {
2903 if (*ptype != '\0') {
2904 ptype++;
2905 while (*ptype == '?')
2906 ptype++;
2909 str = args[nb_args - 1];
2910 switch(*ptype) {
2911 case 'F':
2912 /* file completion */
2913 completion_index = strlen(str);
2914 file_completion(str);
2915 break;
2916 case 'B':
2917 /* block device name completion */
2918 completion_index = strlen(str);
2919 bdrv_iterate(block_completion_it, (void *)str);
2920 break;
2921 case 's':
2922 /* XXX: more generic ? */
2923 if (!strcmp(cmd->name, "info")) {
2924 completion_index = strlen(str);
2925 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2926 cmd_completion(str, cmd->name);
2928 } else if (!strcmp(cmd->name, "sendkey")) {
2929 completion_index = strlen(str);
2930 for(key = key_defs; key->name != NULL; key++) {
2931 cmd_completion(str, key->name);
2934 break;
2935 default:
2936 break;
2939 for(i = 0; i < nb_args; i++)
2940 qemu_free(args[i]);
2943 static int term_can_read(void *opaque)
2945 return 128;
2948 static void term_read(void *opaque, const uint8_t *buf, int size)
2950 int i;
2951 for(i = 0; i < size; i++)
2952 readline_handle_byte(buf[i]);
2955 static int monitor_suspended;
2957 static void monitor_handle_command1(void *opaque, const char *cmdline)
2959 monitor_handle_command(cmdline);
2960 if (!monitor_suspended)
2961 monitor_start_input();
2962 else
2963 monitor_suspended = 2;
2966 void monitor_suspend(void)
2968 monitor_suspended = 1;
2971 void monitor_resume(void)
2973 if (monitor_suspended == 2)
2974 monitor_start_input();
2975 monitor_suspended = 0;
2978 static void monitor_start_input(void)
2980 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2983 static void term_event(void *opaque, int event)
2985 if (event != CHR_EVENT_RESET)
2986 return;
2988 if (!hide_banner)
2989 term_printf("QEMU %s monitor - type 'help' for more information\n",
2990 QEMU_VERSION);
2991 monitor_start_input();
2994 static int is_first_init = 1;
2996 void monitor_init(CharDriverState *hd, int show_banner)
2998 int i;
3000 if (is_first_init) {
3001 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3002 if (!key_timer)
3003 return;
3004 for (i = 0; i < MAX_MON; i++) {
3005 monitor_hd[i] = NULL;
3007 is_first_init = 0;
3009 for (i = 0; i < MAX_MON; i++) {
3010 if (monitor_hd[i] == NULL) {
3011 monitor_hd[i] = hd;
3012 break;
3016 hide_banner = !show_banner;
3018 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
3020 readline_start("", 0, monitor_handle_command1, NULL);
3023 /* XXX: use threads ? */
3024 /* modal monitor readline */
3025 static int monitor_readline_started;
3026 static char *monitor_readline_buf;
3027 static int monitor_readline_buf_size;
3029 static void monitor_readline_cb(void *opaque, const char *input)
3031 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
3032 monitor_readline_started = 0;
3035 static void monitor_readline(const char *prompt, int is_password,
3036 char *buf, int buf_size)
3038 int i;
3039 int old_focus[MAX_MON];
3041 if (is_password) {
3042 for (i = 0; i < MAX_MON; i++) {
3043 old_focus[i] = 0;
3044 if (monitor_hd[i]) {
3045 old_focus[i] = monitor_hd[i]->focus;
3046 monitor_hd[i]->focus = 0;
3047 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
3052 readline_start(prompt, is_password, monitor_readline_cb, NULL);
3053 monitor_readline_buf = buf;
3054 monitor_readline_buf_size = buf_size;
3055 monitor_readline_started = 1;
3056 while (monitor_readline_started) {
3057 main_loop_wait(10);
3059 /* restore original focus */
3060 if (is_password) {
3061 for (i = 0; i < MAX_MON; i++)
3062 if (old_focus[i])
3063 monitor_hd[i]->focus = old_focus[i];
3067 int monitor_read_bdrv_key(BlockDriverState *bs)
3069 char password[256];
3070 int i;
3072 if (!bdrv_is_encrypted(bs))
3073 return 0;
3075 term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3076 bdrv_get_encrypted_filename(bs));
3077 for(i = 0; i < 3; i++) {
3078 monitor_readline("Password: ", 1, password, sizeof(password));
3079 if (bdrv_set_key(bs, password) == 0)
3080 return 0;
3081 term_printf("invalid password\n");
3083 return -EPERM;
3088 * Local variables:
3089 * c-indent-level: 4
3090 * c-basic-offset: 4
3091 * tab-width: 8
3092 * End: