Fix SCSI off-by-one device size.
[qemu/mini2440.git] / monitor.c
blobb40eac4c4b58a7ef443e43accf2aa4338e360d0b
1 /*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
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 "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
28 //#define DEBUG
29 //#define DEBUG_COMPLETION
31 #ifndef offsetof
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
33 #endif
36 * Supported types:
38 * 'F' filename
39 * 'B' block device name
40 * 's' string (accept optional quote)
41 * 'i' 32 bit integer
42 * 'l' target long (32 or 64 bit)
43 * '/' optional gdb-like print format (like "/10x")
45 * '?' optional type (for 'F', 's' and 'i')
49 typedef struct term_cmd_t {
50 const char *name;
51 const char *args_type;
52 void (*handler)();
53 const char *params;
54 const char *help;
55 } term_cmd_t;
57 static CharDriverState *monitor_hd;
59 static term_cmd_t term_cmds[];
60 static term_cmd_t info_cmds[];
62 static char term_outbuf[1024];
63 static int term_outbuf_index;
65 static void monitor_start_input(void);
67 CPUState *mon_cpu = NULL;
69 void term_flush(void)
71 if (term_outbuf_index > 0) {
72 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
73 term_outbuf_index = 0;
77 /* flush at every end of line or if the buffer is full */
78 void term_puts(const char *str)
80 int c;
81 for(;;) {
82 c = *str++;
83 if (c == '\0')
84 break;
85 if (c == '\n')
86 term_outbuf[term_outbuf_index++] = '\r';
87 term_outbuf[term_outbuf_index++] = c;
88 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
89 c == '\n')
90 term_flush();
94 void term_vprintf(const char *fmt, va_list ap)
96 char buf[4096];
97 vsnprintf(buf, sizeof(buf), fmt, ap);
98 term_puts(buf);
101 void term_printf(const char *fmt, ...)
103 va_list ap;
104 va_start(ap, fmt);
105 term_vprintf(fmt, ap);
106 va_end(ap);
109 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
111 va_list ap;
112 va_start(ap, fmt);
113 term_vprintf(fmt, ap);
114 va_end(ap);
115 return 0;
118 static int compare_cmd(const char *name, const char *list)
120 const char *p, *pstart;
121 int len;
122 len = strlen(name);
123 p = list;
124 for(;;) {
125 pstart = p;
126 p = strchr(p, '|');
127 if (!p)
128 p = pstart + strlen(pstart);
129 if ((p - pstart) == len && !memcmp(pstart, name, len))
130 return 1;
131 if (*p == '\0')
132 break;
133 p++;
135 return 0;
138 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
140 term_cmd_t *cmd;
142 for(cmd = cmds; cmd->name != NULL; cmd++) {
143 if (!name || !strcmp(name, cmd->name))
144 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
148 static void help_cmd(const char *name)
150 if (name && !strcmp(name, "info")) {
151 help_cmd1(info_cmds, "info ", NULL);
152 } else {
153 help_cmd1(term_cmds, "", name);
154 if (name && !strcmp(name, "log")) {
155 CPULogItem *item;
156 term_printf("Log items (comma separated):\n");
157 term_printf("%-10s %s\n", "none", "remove all logs");
158 for(item = cpu_log_items; item->mask != 0; item++) {
159 term_printf("%-10s %s\n", item->name, item->help);
165 static void do_help(const char *name)
167 help_cmd(name);
170 static void do_commit(const char *device)
172 int i, all_devices;
174 all_devices = !strcmp(device, "all");
175 for (i = 0; i < MAX_DISKS; i++) {
176 if (bs_table[i]) {
177 if (all_devices ||
178 !strcmp(bdrv_get_device_name(bs_table[i]), device))
179 bdrv_commit(bs_table[i]);
184 static void do_info(const char *item)
186 term_cmd_t *cmd;
188 if (!item)
189 goto help;
190 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
191 if (compare_cmd(item, cmd->name))
192 goto found;
194 help:
195 help_cmd("info");
196 return;
197 found:
198 cmd->handler();
201 static void do_info_version(void)
203 term_printf("%s\n", QEMU_VERSION);
206 static void do_info_block(void)
208 bdrv_info();
211 /* get the current CPU defined by the user */
212 int mon_set_cpu(int cpu_index)
214 CPUState *env;
216 for(env = first_cpu; env != NULL; env = env->next_cpu) {
217 if (env->cpu_index == cpu_index) {
218 mon_cpu = env;
219 return 0;
222 return -1;
225 CPUState *mon_get_cpu(void)
227 if (!mon_cpu) {
228 mon_set_cpu(0);
230 return mon_cpu;
233 static void do_info_registers(void)
235 CPUState *env;
236 env = mon_get_cpu();
237 if (!env)
238 return;
239 #ifdef TARGET_I386
240 cpu_dump_state(env, NULL, monitor_fprintf,
241 X86_DUMP_FPU);
242 #else
243 cpu_dump_state(env, NULL, monitor_fprintf,
245 #endif
248 static void do_info_cpus(void)
250 CPUState *env;
252 /* just to set the default cpu if not already done */
253 mon_get_cpu();
255 for(env = first_cpu; env != NULL; env = env->next_cpu) {
256 term_printf("%c CPU #%d:",
257 (env == mon_cpu) ? '*' : ' ',
258 env->cpu_index);
259 #if defined(TARGET_I386)
260 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
261 if (env->hflags & HF_HALTED_MASK)
262 term_printf(" (halted)");
263 #elif defined(TARGET_PPC)
264 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
265 if (env->halted)
266 term_printf(" (halted)");
267 #elif defined(TARGET_SPARC)
268 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
269 if (env->halted)
270 term_printf(" (halted)");
271 #endif
272 term_printf("\n");
276 static void do_cpu_set(int index)
278 if (mon_set_cpu(index) < 0)
279 term_printf("Invalid CPU index\n");
282 static void do_info_jit(void)
284 dump_exec_info(NULL, monitor_fprintf);
287 static void do_info_history (void)
289 int i;
290 const char *str;
292 i = 0;
293 for(;;) {
294 str = readline_get_history(i);
295 if (!str)
296 break;
297 term_printf("%d: '%s'\n", i, str);
298 i++;
302 static void do_quit(void)
304 exit(0);
307 static int eject_device(BlockDriverState *bs, int force)
309 if (bdrv_is_inserted(bs)) {
310 if (!force) {
311 if (!bdrv_is_removable(bs)) {
312 term_printf("device is not removable\n");
313 return -1;
315 if (bdrv_is_locked(bs)) {
316 term_printf("device is locked\n");
317 return -1;
320 bdrv_close(bs);
322 return 0;
325 static void do_eject(int force, const char *filename)
327 BlockDriverState *bs;
329 bs = bdrv_find(filename);
330 if (!bs) {
331 term_printf("device not found\n");
332 return;
334 eject_device(bs, force);
337 static void do_change(const char *device, const char *filename)
339 BlockDriverState *bs;
340 int i;
341 char password[256];
343 bs = bdrv_find(device);
344 if (!bs) {
345 term_printf("device not found\n");
346 return;
348 if (eject_device(bs, 0) < 0)
349 return;
350 bdrv_open(bs, filename, 0);
351 if (bdrv_is_encrypted(bs)) {
352 term_printf("%s is encrypted.\n", device);
353 for(i = 0; i < 3; i++) {
354 monitor_readline("Password: ", 1, password, sizeof(password));
355 if (bdrv_set_key(bs, password) == 0)
356 break;
357 term_printf("invalid password\n");
362 static void do_screen_dump(const char *filename)
364 vga_hw_screen_dump(filename);
367 static void do_log(const char *items)
369 int mask;
371 if (!strcmp(items, "none")) {
372 mask = 0;
373 } else {
374 mask = cpu_str_to_log_mask(items);
375 if (!mask) {
376 help_cmd("log");
377 return;
380 cpu_set_log(mask);
383 static void do_stop(void)
385 vm_stop(EXCP_INTERRUPT);
388 static void do_cont(void)
390 vm_start();
393 #ifdef CONFIG_GDBSTUB
394 static void do_gdbserver(int has_port, int port)
396 if (!has_port)
397 port = DEFAULT_GDBSTUB_PORT;
398 if (gdbserver_start(port) < 0) {
399 qemu_printf("Could not open gdbserver socket on port %d\n", port);
400 } else {
401 qemu_printf("Waiting gdb connection on port %d\n", port);
404 #endif
406 static void term_printc(int c)
408 term_printf("'");
409 switch(c) {
410 case '\'':
411 term_printf("\\'");
412 break;
413 case '\\':
414 term_printf("\\\\");
415 break;
416 case '\n':
417 term_printf("\\n");
418 break;
419 case '\r':
420 term_printf("\\r");
421 break;
422 default:
423 if (c >= 32 && c <= 126) {
424 term_printf("%c", c);
425 } else {
426 term_printf("\\x%02x", c);
428 break;
430 term_printf("'");
433 static void memory_dump(int count, int format, int wsize,
434 target_ulong addr, int is_physical)
436 CPUState *env;
437 int nb_per_line, l, line_size, i, max_digits, len;
438 uint8_t buf[16];
439 uint64_t v;
441 if (format == 'i') {
442 int flags;
443 flags = 0;
444 env = mon_get_cpu();
445 if (!env && !is_physical)
446 return;
447 #ifdef TARGET_I386
448 if (wsize == 2) {
449 flags = 1;
450 } else if (wsize == 4) {
451 flags = 0;
452 } else {
453 /* as default we use the current CS size */
454 flags = 0;
455 if (env) {
456 #ifdef TARGET_X86_64
457 if ((env->efer & MSR_EFER_LMA) &&
458 (env->segs[R_CS].flags & DESC_L_MASK))
459 flags = 2;
460 else
461 #endif
462 if (!(env->segs[R_CS].flags & DESC_B_MASK))
463 flags = 1;
466 #endif
467 monitor_disas(env, addr, count, is_physical, flags);
468 return;
471 len = wsize * count;
472 if (wsize == 1)
473 line_size = 8;
474 else
475 line_size = 16;
476 nb_per_line = line_size / wsize;
477 max_digits = 0;
479 switch(format) {
480 case 'o':
481 max_digits = (wsize * 8 + 2) / 3;
482 break;
483 default:
484 case 'x':
485 max_digits = (wsize * 8) / 4;
486 break;
487 case 'u':
488 case 'd':
489 max_digits = (wsize * 8 * 10 + 32) / 33;
490 break;
491 case 'c':
492 wsize = 1;
493 break;
496 while (len > 0) {
497 term_printf(TARGET_FMT_lx ":", addr);
498 l = len;
499 if (l > line_size)
500 l = line_size;
501 if (is_physical) {
502 cpu_physical_memory_rw(addr, buf, l, 0);
503 } else {
504 env = mon_get_cpu();
505 if (!env)
506 break;
507 cpu_memory_rw_debug(env, addr, buf, l, 0);
509 i = 0;
510 while (i < l) {
511 switch(wsize) {
512 default:
513 case 1:
514 v = ldub_raw(buf + i);
515 break;
516 case 2:
517 v = lduw_raw(buf + i);
518 break;
519 case 4:
520 v = (uint32_t)ldl_raw(buf + i);
521 break;
522 case 8:
523 v = ldq_raw(buf + i);
524 break;
526 term_printf(" ");
527 switch(format) {
528 case 'o':
529 term_printf("%#*" PRIo64, max_digits, v);
530 break;
531 case 'x':
532 term_printf("0x%0*" PRIx64, max_digits, v);
533 break;
534 case 'u':
535 term_printf("%*" PRIu64, max_digits, v);
536 break;
537 case 'd':
538 term_printf("%*" PRId64, max_digits, v);
539 break;
540 case 'c':
541 term_printc(v);
542 break;
544 i += wsize;
546 term_printf("\n");
547 addr += l;
548 len -= l;
552 #if TARGET_LONG_BITS == 64
553 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
554 #else
555 #define GET_TLONG(h, l) (l)
556 #endif
558 static void do_memory_dump(int count, int format, int size,
559 uint32_t addrh, uint32_t addrl)
561 target_long addr = GET_TLONG(addrh, addrl);
562 memory_dump(count, format, size, addr, 0);
565 static void do_physical_memory_dump(int count, int format, int size,
566 uint32_t addrh, uint32_t addrl)
569 target_long addr = GET_TLONG(addrh, addrl);
570 memory_dump(count, format, size, addr, 1);
573 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
575 target_long val = GET_TLONG(valh, vall);
576 #if TARGET_LONG_BITS == 32
577 switch(format) {
578 case 'o':
579 term_printf("%#o", val);
580 break;
581 case 'x':
582 term_printf("%#x", val);
583 break;
584 case 'u':
585 term_printf("%u", val);
586 break;
587 default:
588 case 'd':
589 term_printf("%d", val);
590 break;
591 case 'c':
592 term_printc(val);
593 break;
595 #else
596 switch(format) {
597 case 'o':
598 term_printf("%#" PRIo64, val);
599 break;
600 case 'x':
601 term_printf("%#" PRIx64, val);
602 break;
603 case 'u':
604 term_printf("%" PRIu64, val);
605 break;
606 default:
607 case 'd':
608 term_printf("%" PRId64, val);
609 break;
610 case 'c':
611 term_printc(val);
612 break;
614 #endif
615 term_printf("\n");
618 static void do_sum(uint32_t start, uint32_t size)
620 uint32_t addr;
621 uint8_t buf[1];
622 uint16_t sum;
624 sum = 0;
625 for(addr = start; addr < (start + size); addr++) {
626 cpu_physical_memory_rw(addr, buf, 1, 0);
627 /* BSD sum algorithm ('sum' Unix command) */
628 sum = (sum >> 1) | (sum << 15);
629 sum += buf[0];
631 term_printf("%05d\n", sum);
634 typedef struct {
635 int keycode;
636 const char *name;
637 } KeyDef;
639 static const KeyDef key_defs[] = {
640 { 0x2a, "shift" },
641 { 0x36, "shift_r" },
643 { 0x38, "alt" },
644 { 0xb8, "alt_r" },
645 { 0x1d, "ctrl" },
646 { 0x9d, "ctrl_r" },
648 { 0xdd, "menu" },
650 { 0x01, "esc" },
652 { 0x02, "1" },
653 { 0x03, "2" },
654 { 0x04, "3" },
655 { 0x05, "4" },
656 { 0x06, "5" },
657 { 0x07, "6" },
658 { 0x08, "7" },
659 { 0x09, "8" },
660 { 0x0a, "9" },
661 { 0x0b, "0" },
662 { 0x0c, "minus" },
663 { 0x0d, "equal" },
664 { 0x0e, "backspace" },
666 { 0x0f, "tab" },
667 { 0x10, "q" },
668 { 0x11, "w" },
669 { 0x12, "e" },
670 { 0x13, "r" },
671 { 0x14, "t" },
672 { 0x15, "y" },
673 { 0x16, "u" },
674 { 0x17, "i" },
675 { 0x18, "o" },
676 { 0x19, "p" },
678 { 0x1c, "ret" },
680 { 0x1e, "a" },
681 { 0x1f, "s" },
682 { 0x20, "d" },
683 { 0x21, "f" },
684 { 0x22, "g" },
685 { 0x23, "h" },
686 { 0x24, "j" },
687 { 0x25, "k" },
688 { 0x26, "l" },
690 { 0x2c, "z" },
691 { 0x2d, "x" },
692 { 0x2e, "c" },
693 { 0x2f, "v" },
694 { 0x30, "b" },
695 { 0x31, "n" },
696 { 0x32, "m" },
698 { 0x39, "spc" },
699 { 0x3a, "caps_lock" },
700 { 0x3b, "f1" },
701 { 0x3c, "f2" },
702 { 0x3d, "f3" },
703 { 0x3e, "f4" },
704 { 0x3f, "f5" },
705 { 0x40, "f6" },
706 { 0x41, "f7" },
707 { 0x42, "f8" },
708 { 0x43, "f9" },
709 { 0x44, "f10" },
710 { 0x45, "num_lock" },
711 { 0x46, "scroll_lock" },
713 { 0xb5, "kp_divide" },
714 { 0x37, "kp_multiply" },
715 { 0x4a, "kp_substract" },
716 { 0x4e, "kp_add" },
717 { 0x9c, "kp_enter" },
718 { 0x53, "kp_decimal" },
720 { 0x52, "kp_0" },
721 { 0x4f, "kp_1" },
722 { 0x50, "kp_2" },
723 { 0x51, "kp_3" },
724 { 0x4b, "kp_4" },
725 { 0x4c, "kp_5" },
726 { 0x4d, "kp_6" },
727 { 0x47, "kp_7" },
728 { 0x48, "kp_8" },
729 { 0x49, "kp_9" },
731 { 0x56, "<" },
733 { 0x57, "f11" },
734 { 0x58, "f12" },
736 { 0xb7, "print" },
738 { 0xc7, "home" },
739 { 0xc9, "pgup" },
740 { 0xd1, "pgdn" },
741 { 0xcf, "end" },
743 { 0xcb, "left" },
744 { 0xc8, "up" },
745 { 0xd0, "down" },
746 { 0xcd, "right" },
748 { 0xd2, "insert" },
749 { 0xd3, "delete" },
750 { 0, NULL },
753 static int get_keycode(const char *key)
755 const KeyDef *p;
756 char *endp;
757 int ret;
759 for(p = key_defs; p->name != NULL; p++) {
760 if (!strcmp(key, p->name))
761 return p->keycode;
763 if (strstart(key, "0x", NULL)) {
764 ret = strtoul(key, &endp, 0);
765 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
766 return ret;
768 return -1;
771 static void do_send_key(const char *string)
773 char keybuf[16], *q;
774 uint8_t keycodes[16];
775 const char *p;
776 int nb_keycodes, keycode, i;
778 nb_keycodes = 0;
779 p = string;
780 while (*p != '\0') {
781 q = keybuf;
782 while (*p != '\0' && *p != '-') {
783 if ((q - keybuf) < sizeof(keybuf) - 1) {
784 *q++ = *p;
786 p++;
788 *q = '\0';
789 keycode = get_keycode(keybuf);
790 if (keycode < 0) {
791 term_printf("unknown key: '%s'\n", keybuf);
792 return;
794 keycodes[nb_keycodes++] = keycode;
795 if (*p == '\0')
796 break;
797 p++;
799 /* key down events */
800 for(i = 0; i < nb_keycodes; i++) {
801 keycode = keycodes[i];
802 if (keycode & 0x80)
803 kbd_put_keycode(0xe0);
804 kbd_put_keycode(keycode & 0x7f);
806 /* key up events */
807 for(i = nb_keycodes - 1; i >= 0; i--) {
808 keycode = keycodes[i];
809 if (keycode & 0x80)
810 kbd_put_keycode(0xe0);
811 kbd_put_keycode(keycode | 0x80);
815 static int mouse_button_state;
817 static void do_mouse_move(const char *dx_str, const char *dy_str,
818 const char *dz_str)
820 int dx, dy, dz;
821 dx = strtol(dx_str, NULL, 0);
822 dy = strtol(dy_str, NULL, 0);
823 dz = 0;
824 if (dz_str)
825 dz = strtol(dz_str, NULL, 0);
826 kbd_mouse_event(dx, dy, dz, mouse_button_state);
829 static void do_mouse_button(int button_state)
831 mouse_button_state = button_state;
832 kbd_mouse_event(0, 0, 0, mouse_button_state);
835 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
837 uint32_t val;
838 int suffix;
840 if (has_index) {
841 cpu_outb(NULL, addr & 0xffff, index & 0xff);
842 addr++;
844 addr &= 0xffff;
846 switch(size) {
847 default:
848 case 1:
849 val = cpu_inb(NULL, addr);
850 suffix = 'b';
851 break;
852 case 2:
853 val = cpu_inw(NULL, addr);
854 suffix = 'w';
855 break;
856 case 4:
857 val = cpu_inl(NULL, addr);
858 suffix = 'l';
859 break;
861 term_printf("port%c[0x%04x] = %#0*x\n",
862 suffix, addr, size * 2, val);
865 static void do_system_reset(void)
867 qemu_system_reset_request();
870 static void do_system_powerdown(void)
872 qemu_system_powerdown_request();
875 #if defined(TARGET_I386)
876 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
878 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
879 addr,
880 pte & mask,
881 pte & PG_GLOBAL_MASK ? 'G' : '-',
882 pte & PG_PSE_MASK ? 'P' : '-',
883 pte & PG_DIRTY_MASK ? 'D' : '-',
884 pte & PG_ACCESSED_MASK ? 'A' : '-',
885 pte & PG_PCD_MASK ? 'C' : '-',
886 pte & PG_PWT_MASK ? 'T' : '-',
887 pte & PG_USER_MASK ? 'U' : '-',
888 pte & PG_RW_MASK ? 'W' : '-');
891 static void tlb_info(void)
893 CPUState *env;
894 int l1, l2;
895 uint32_t pgd, pde, pte;
897 env = mon_get_cpu();
898 if (!env)
899 return;
901 if (!(env->cr[0] & CR0_PG_MASK)) {
902 term_printf("PG disabled\n");
903 return;
905 pgd = env->cr[3] & ~0xfff;
906 for(l1 = 0; l1 < 1024; l1++) {
907 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
908 pde = le32_to_cpu(pde);
909 if (pde & PG_PRESENT_MASK) {
910 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
911 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
912 } else {
913 for(l2 = 0; l2 < 1024; l2++) {
914 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
915 (uint8_t *)&pte, 4);
916 pte = le32_to_cpu(pte);
917 if (pte & PG_PRESENT_MASK) {
918 print_pte((l1 << 22) + (l2 << 12),
919 pte & ~PG_PSE_MASK,
920 ~0xfff);
928 static void mem_print(uint32_t *pstart, int *plast_prot,
929 uint32_t end, int prot)
931 int prot1;
932 prot1 = *plast_prot;
933 if (prot != prot1) {
934 if (*pstart != -1) {
935 term_printf("%08x-%08x %08x %c%c%c\n",
936 *pstart, end, end - *pstart,
937 prot1 & PG_USER_MASK ? 'u' : '-',
938 'r',
939 prot1 & PG_RW_MASK ? 'w' : '-');
941 if (prot != 0)
942 *pstart = end;
943 else
944 *pstart = -1;
945 *plast_prot = prot;
949 static void mem_info(void)
951 CPUState *env;
952 int l1, l2, prot, last_prot;
953 uint32_t pgd, pde, pte, start, end;
955 env = mon_get_cpu();
956 if (!env)
957 return;
959 if (!(env->cr[0] & CR0_PG_MASK)) {
960 term_printf("PG disabled\n");
961 return;
963 pgd = env->cr[3] & ~0xfff;
964 last_prot = 0;
965 start = -1;
966 for(l1 = 0; l1 < 1024; l1++) {
967 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
968 pde = le32_to_cpu(pde);
969 end = l1 << 22;
970 if (pde & PG_PRESENT_MASK) {
971 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
972 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
973 mem_print(&start, &last_prot, end, prot);
974 } else {
975 for(l2 = 0; l2 < 1024; l2++) {
976 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
977 (uint8_t *)&pte, 4);
978 pte = le32_to_cpu(pte);
979 end = (l1 << 22) + (l2 << 12);
980 if (pte & PG_PRESENT_MASK) {
981 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
982 } else {
983 prot = 0;
985 mem_print(&start, &last_prot, end, prot);
988 } else {
989 prot = 0;
990 mem_print(&start, &last_prot, end, prot);
994 #endif
996 static void do_info_kqemu(void)
998 #ifdef USE_KQEMU
999 CPUState *env;
1000 int val;
1001 val = 0;
1002 env = mon_get_cpu();
1003 if (!env) {
1004 term_printf("No cpu initialized yet");
1005 return;
1007 val = env->kqemu_enabled;
1008 term_printf("kqemu support: ");
1009 switch(val) {
1010 default:
1011 case 0:
1012 term_printf("disabled\n");
1013 break;
1014 case 1:
1015 term_printf("enabled for user code\n");
1016 break;
1017 case 2:
1018 term_printf("enabled for user and kernel code\n");
1019 break;
1021 #else
1022 term_printf("kqemu support: not compiled\n");
1023 #endif
1026 #ifdef CONFIG_PROFILER
1028 int64_t kqemu_time;
1029 int64_t qemu_time;
1030 int64_t kqemu_exec_count;
1031 int64_t dev_time;
1032 int64_t kqemu_ret_int_count;
1033 int64_t kqemu_ret_excp_count;
1034 int64_t kqemu_ret_intr_count;
1036 static void do_info_profile(void)
1038 int64_t total;
1039 total = qemu_time;
1040 if (total == 0)
1041 total = 1;
1042 term_printf("async time %" PRId64 " (%0.3f)\n",
1043 dev_time, dev_time / (double)ticks_per_sec);
1044 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1045 qemu_time, qemu_time / (double)ticks_per_sec);
1046 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1047 kqemu_time, kqemu_time / (double)ticks_per_sec,
1048 kqemu_time / (double)total * 100.0,
1049 kqemu_exec_count,
1050 kqemu_ret_int_count,
1051 kqemu_ret_excp_count,
1052 kqemu_ret_intr_count);
1053 qemu_time = 0;
1054 kqemu_time = 0;
1055 kqemu_exec_count = 0;
1056 dev_time = 0;
1057 kqemu_ret_int_count = 0;
1058 kqemu_ret_excp_count = 0;
1059 kqemu_ret_intr_count = 0;
1060 #ifdef USE_KQEMU
1061 kqemu_record_dump();
1062 #endif
1064 #else
1065 static void do_info_profile(void)
1067 term_printf("Internal profiler not compiled\n");
1069 #endif
1071 /* Capture support */
1072 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1074 static void do_info_capture (void)
1076 int i;
1077 CaptureState *s;
1079 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1080 term_printf ("[%d]: ", i);
1081 s->ops.info (s->opaque);
1085 static void do_stop_capture (int n)
1087 int i;
1088 CaptureState *s;
1090 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1091 if (i == n) {
1092 s->ops.destroy (s->opaque);
1093 LIST_REMOVE (s, entries);
1094 qemu_free (s);
1095 return;
1100 #ifdef HAS_AUDIO
1101 int wav_start_capture (CaptureState *s, const char *path, int freq,
1102 int bits, int nchannels);
1104 static void do_wav_capture (const char *path,
1105 int has_freq, int freq,
1106 int has_bits, int bits,
1107 int has_channels, int nchannels)
1109 CaptureState *s;
1111 s = qemu_mallocz (sizeof (*s));
1112 if (!s) {
1113 term_printf ("Not enough memory to add wave capture\n");
1114 return;
1117 freq = has_freq ? freq : 44100;
1118 bits = has_bits ? bits : 16;
1119 nchannels = has_channels ? nchannels : 2;
1121 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1122 term_printf ("Faied to add wave capture\n");
1123 qemu_free (s);
1125 LIST_INSERT_HEAD (&capture_head, s, entries);
1127 #endif
1129 static term_cmd_t term_cmds[] = {
1130 { "help|?", "s?", do_help,
1131 "[cmd]", "show the help" },
1132 { "commit", "s", do_commit,
1133 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1134 { "info", "s?", do_info,
1135 "subcommand", "show various information about the system state" },
1136 { "q|quit", "", do_quit,
1137 "", "quit the emulator" },
1138 { "eject", "-fB", do_eject,
1139 "[-f] device", "eject a removable media (use -f to force it)" },
1140 { "change", "BF", do_change,
1141 "device filename", "change a removable media" },
1142 { "screendump", "F", do_screen_dump,
1143 "filename", "save screen into PPM image 'filename'" },
1144 { "log", "s", do_log,
1145 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1146 { "savevm", "s?", do_savevm,
1147 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1148 { "loadvm", "s", do_loadvm,
1149 "tag|id", "restore a VM snapshot from its tag or id" },
1150 { "delvm", "s", do_delvm,
1151 "tag|id", "delete a VM snapshot from its tag or id" },
1152 { "stop", "", do_stop,
1153 "", "stop emulation", },
1154 { "c|cont", "", do_cont,
1155 "", "resume emulation", },
1156 #ifdef CONFIG_GDBSTUB
1157 { "gdbserver", "i?", do_gdbserver,
1158 "[port]", "start gdbserver session (default port=1234)", },
1159 #endif
1160 { "x", "/l", do_memory_dump,
1161 "/fmt addr", "virtual memory dump starting at 'addr'", },
1162 { "xp", "/l", do_physical_memory_dump,
1163 "/fmt addr", "physical memory dump starting at 'addr'", },
1164 { "p|print", "/l", do_print,
1165 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1166 { "i", "/ii.", do_ioport_read,
1167 "/fmt addr", "I/O port read" },
1169 { "sendkey", "s", do_send_key,
1170 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1171 { "system_reset", "", do_system_reset,
1172 "", "reset the system" },
1173 { "system_powerdown", "", do_system_powerdown,
1174 "", "send system power down event" },
1175 { "sum", "ii", do_sum,
1176 "addr size", "compute the checksum of a memory region" },
1177 { "usb_add", "s", do_usb_add,
1178 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1179 { "usb_del", "s", do_usb_del,
1180 "device", "remove USB device 'bus.addr'" },
1181 { "cpu", "i", do_cpu_set,
1182 "index", "set the default CPU" },
1183 { "mouse_move", "sss?", do_mouse_move,
1184 "dx dy [dz]", "send mouse move events" },
1185 { "mouse_button", "i", do_mouse_button,
1186 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1187 #ifdef HAS_AUDIO
1188 { "wavcapture", "si?i?i?", do_wav_capture,
1189 "path [frequency bits channels]",
1190 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1191 #endif
1192 { "stopcapture", "i", do_stop_capture,
1193 "capture index", "stop capture" },
1194 { NULL, NULL, },
1197 static term_cmd_t info_cmds[] = {
1198 { "version", "", do_info_version,
1199 "", "show the version of qemu" },
1200 { "network", "", do_info_network,
1201 "", "show the network state" },
1202 { "block", "", do_info_block,
1203 "", "show the block devices" },
1204 { "registers", "", do_info_registers,
1205 "", "show the cpu registers" },
1206 { "cpus", "", do_info_cpus,
1207 "", "show infos for each CPU" },
1208 { "history", "", do_info_history,
1209 "", "show the command line history", },
1210 { "irq", "", irq_info,
1211 "", "show the interrupts statistics (if available)", },
1212 { "pic", "", pic_info,
1213 "", "show i8259 (PIC) state", },
1214 { "pci", "", pci_info,
1215 "", "show PCI info", },
1216 #if defined(TARGET_I386)
1217 { "tlb", "", tlb_info,
1218 "", "show virtual to physical memory mappings", },
1219 { "mem", "", mem_info,
1220 "", "show the active virtual memory mappings", },
1221 #endif
1222 { "jit", "", do_info_jit,
1223 "", "show dynamic compiler info", },
1224 { "kqemu", "", do_info_kqemu,
1225 "", "show kqemu information", },
1226 { "usb", "", usb_info,
1227 "", "show guest USB devices", },
1228 { "usbhost", "", usb_host_info,
1229 "", "show host USB devices", },
1230 { "profile", "", do_info_profile,
1231 "", "show profiling information", },
1232 { "capture", "", do_info_capture,
1233 "show capture information" },
1234 { "snapshots", "", do_info_snapshots,
1235 "show the currently saved VM snapshots" },
1236 { NULL, NULL, },
1239 /*******************************************************************/
1241 static const char *pch;
1242 static jmp_buf expr_env;
1244 #define MD_TLONG 0
1245 #define MD_I32 1
1247 typedef struct MonitorDef {
1248 const char *name;
1249 int offset;
1250 target_long (*get_value)(struct MonitorDef *md, int val);
1251 int type;
1252 } MonitorDef;
1254 #if defined(TARGET_I386)
1255 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1257 CPUState *env = mon_get_cpu();
1258 if (!env)
1259 return 0;
1260 return env->eip + env->segs[R_CS].base;
1262 #endif
1264 #if defined(TARGET_PPC)
1265 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1267 CPUState *env = mon_get_cpu();
1268 unsigned int u;
1269 int i;
1271 if (!env)
1272 return 0;
1274 u = 0;
1275 for (i = 0; i < 8; i++)
1276 u |= env->crf[i] << (32 - (4 * i));
1278 return u;
1281 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1283 CPUState *env = mon_get_cpu();
1284 if (!env)
1285 return 0;
1286 return (env->msr[MSR_POW] << MSR_POW) |
1287 (env->msr[MSR_ILE] << MSR_ILE) |
1288 (env->msr[MSR_EE] << MSR_EE) |
1289 (env->msr[MSR_PR] << MSR_PR) |
1290 (env->msr[MSR_FP] << MSR_FP) |
1291 (env->msr[MSR_ME] << MSR_ME) |
1292 (env->msr[MSR_FE0] << MSR_FE0) |
1293 (env->msr[MSR_SE] << MSR_SE) |
1294 (env->msr[MSR_BE] << MSR_BE) |
1295 (env->msr[MSR_FE1] << MSR_FE1) |
1296 (env->msr[MSR_IP] << MSR_IP) |
1297 (env->msr[MSR_IR] << MSR_IR) |
1298 (env->msr[MSR_DR] << MSR_DR) |
1299 (env->msr[MSR_RI] << MSR_RI) |
1300 (env->msr[MSR_LE] << MSR_LE);
1303 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1305 CPUState *env = mon_get_cpu();
1306 if (!env)
1307 return 0;
1308 return (env->xer[XER_SO] << XER_SO) |
1309 (env->xer[XER_OV] << XER_OV) |
1310 (env->xer[XER_CA] << XER_CA) |
1311 (env->xer[XER_BC] << XER_BC);
1314 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1316 CPUState *env = mon_get_cpu();
1317 if (!env)
1318 return 0;
1319 return cpu_ppc_load_decr(env);
1322 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1324 CPUState *env = mon_get_cpu();
1325 if (!env)
1326 return 0;
1327 return cpu_ppc_load_tbu(env);
1330 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1332 CPUState *env = mon_get_cpu();
1333 if (!env)
1334 return 0;
1335 return cpu_ppc_load_tbl(env);
1337 #endif
1339 #if defined(TARGET_SPARC)
1340 #ifndef TARGET_SPARC64
1341 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1343 CPUState *env = mon_get_cpu();
1344 if (!env)
1345 return 0;
1346 return GET_PSR(env);
1348 #endif
1350 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1352 CPUState *env = mon_get_cpu();
1353 if (!env)
1354 return 0;
1355 return env->regwptr[val];
1357 #endif
1359 static MonitorDef monitor_defs[] = {
1360 #ifdef TARGET_I386
1362 #define SEG(name, seg) \
1363 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1364 { name ".base", offsetof(CPUState, segs[seg].base) },\
1365 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1367 { "eax", offsetof(CPUState, regs[0]) },
1368 { "ecx", offsetof(CPUState, regs[1]) },
1369 { "edx", offsetof(CPUState, regs[2]) },
1370 { "ebx", offsetof(CPUState, regs[3]) },
1371 { "esp|sp", offsetof(CPUState, regs[4]) },
1372 { "ebp|fp", offsetof(CPUState, regs[5]) },
1373 { "esi", offsetof(CPUState, regs[6]) },
1374 { "edi", offsetof(CPUState, regs[7]) },
1375 #ifdef TARGET_X86_64
1376 { "r8", offsetof(CPUState, regs[8]) },
1377 { "r9", offsetof(CPUState, regs[9]) },
1378 { "r10", offsetof(CPUState, regs[10]) },
1379 { "r11", offsetof(CPUState, regs[11]) },
1380 { "r12", offsetof(CPUState, regs[12]) },
1381 { "r13", offsetof(CPUState, regs[13]) },
1382 { "r14", offsetof(CPUState, regs[14]) },
1383 { "r15", offsetof(CPUState, regs[15]) },
1384 #endif
1385 { "eflags", offsetof(CPUState, eflags) },
1386 { "eip", offsetof(CPUState, eip) },
1387 SEG("cs", R_CS)
1388 SEG("ds", R_DS)
1389 SEG("es", R_ES)
1390 SEG("ss", R_SS)
1391 SEG("fs", R_FS)
1392 SEG("gs", R_GS)
1393 { "pc", 0, monitor_get_pc, },
1394 #elif defined(TARGET_PPC)
1395 { "r0", offsetof(CPUState, gpr[0]) },
1396 { "r1", offsetof(CPUState, gpr[1]) },
1397 { "r2", offsetof(CPUState, gpr[2]) },
1398 { "r3", offsetof(CPUState, gpr[3]) },
1399 { "r4", offsetof(CPUState, gpr[4]) },
1400 { "r5", offsetof(CPUState, gpr[5]) },
1401 { "r6", offsetof(CPUState, gpr[6]) },
1402 { "r7", offsetof(CPUState, gpr[7]) },
1403 { "r8", offsetof(CPUState, gpr[8]) },
1404 { "r9", offsetof(CPUState, gpr[9]) },
1405 { "r10", offsetof(CPUState, gpr[10]) },
1406 { "r11", offsetof(CPUState, gpr[11]) },
1407 { "r12", offsetof(CPUState, gpr[12]) },
1408 { "r13", offsetof(CPUState, gpr[13]) },
1409 { "r14", offsetof(CPUState, gpr[14]) },
1410 { "r15", offsetof(CPUState, gpr[15]) },
1411 { "r16", offsetof(CPUState, gpr[16]) },
1412 { "r17", offsetof(CPUState, gpr[17]) },
1413 { "r18", offsetof(CPUState, gpr[18]) },
1414 { "r19", offsetof(CPUState, gpr[19]) },
1415 { "r20", offsetof(CPUState, gpr[20]) },
1416 { "r21", offsetof(CPUState, gpr[21]) },
1417 { "r22", offsetof(CPUState, gpr[22]) },
1418 { "r23", offsetof(CPUState, gpr[23]) },
1419 { "r24", offsetof(CPUState, gpr[24]) },
1420 { "r25", offsetof(CPUState, gpr[25]) },
1421 { "r26", offsetof(CPUState, gpr[26]) },
1422 { "r27", offsetof(CPUState, gpr[27]) },
1423 { "r28", offsetof(CPUState, gpr[28]) },
1424 { "r29", offsetof(CPUState, gpr[29]) },
1425 { "r30", offsetof(CPUState, gpr[30]) },
1426 { "r31", offsetof(CPUState, gpr[31]) },
1427 { "nip|pc", offsetof(CPUState, nip) },
1428 { "lr", offsetof(CPUState, lr) },
1429 { "ctr", offsetof(CPUState, ctr) },
1430 { "decr", 0, &monitor_get_decr, },
1431 { "ccr", 0, &monitor_get_ccr, },
1432 { "msr", 0, &monitor_get_msr, },
1433 { "xer", 0, &monitor_get_xer, },
1434 { "tbu", 0, &monitor_get_tbu, },
1435 { "tbl", 0, &monitor_get_tbl, },
1436 { "sdr1", offsetof(CPUState, sdr1) },
1437 { "sr0", offsetof(CPUState, sr[0]) },
1438 { "sr1", offsetof(CPUState, sr[1]) },
1439 { "sr2", offsetof(CPUState, sr[2]) },
1440 { "sr3", offsetof(CPUState, sr[3]) },
1441 { "sr4", offsetof(CPUState, sr[4]) },
1442 { "sr5", offsetof(CPUState, sr[5]) },
1443 { "sr6", offsetof(CPUState, sr[6]) },
1444 { "sr7", offsetof(CPUState, sr[7]) },
1445 { "sr8", offsetof(CPUState, sr[8]) },
1446 { "sr9", offsetof(CPUState, sr[9]) },
1447 { "sr10", offsetof(CPUState, sr[10]) },
1448 { "sr11", offsetof(CPUState, sr[11]) },
1449 { "sr12", offsetof(CPUState, sr[12]) },
1450 { "sr13", offsetof(CPUState, sr[13]) },
1451 { "sr14", offsetof(CPUState, sr[14]) },
1452 { "sr15", offsetof(CPUState, sr[15]) },
1453 /* Too lazy to put BATs and SPRs ... */
1454 #elif defined(TARGET_SPARC)
1455 { "g0", offsetof(CPUState, gregs[0]) },
1456 { "g1", offsetof(CPUState, gregs[1]) },
1457 { "g2", offsetof(CPUState, gregs[2]) },
1458 { "g3", offsetof(CPUState, gregs[3]) },
1459 { "g4", offsetof(CPUState, gregs[4]) },
1460 { "g5", offsetof(CPUState, gregs[5]) },
1461 { "g6", offsetof(CPUState, gregs[6]) },
1462 { "g7", offsetof(CPUState, gregs[7]) },
1463 { "o0", 0, monitor_get_reg },
1464 { "o1", 1, monitor_get_reg },
1465 { "o2", 2, monitor_get_reg },
1466 { "o3", 3, monitor_get_reg },
1467 { "o4", 4, monitor_get_reg },
1468 { "o5", 5, monitor_get_reg },
1469 { "o6", 6, monitor_get_reg },
1470 { "o7", 7, monitor_get_reg },
1471 { "l0", 8, monitor_get_reg },
1472 { "l1", 9, monitor_get_reg },
1473 { "l2", 10, monitor_get_reg },
1474 { "l3", 11, monitor_get_reg },
1475 { "l4", 12, monitor_get_reg },
1476 { "l5", 13, monitor_get_reg },
1477 { "l6", 14, monitor_get_reg },
1478 { "l7", 15, monitor_get_reg },
1479 { "i0", 16, monitor_get_reg },
1480 { "i1", 17, monitor_get_reg },
1481 { "i2", 18, monitor_get_reg },
1482 { "i3", 19, monitor_get_reg },
1483 { "i4", 20, monitor_get_reg },
1484 { "i5", 21, monitor_get_reg },
1485 { "i6", 22, monitor_get_reg },
1486 { "i7", 23, monitor_get_reg },
1487 { "pc", offsetof(CPUState, pc) },
1488 { "npc", offsetof(CPUState, npc) },
1489 { "y", offsetof(CPUState, y) },
1490 #ifndef TARGET_SPARC64
1491 { "psr", 0, &monitor_get_psr, },
1492 { "wim", offsetof(CPUState, wim) },
1493 #endif
1494 { "tbr", offsetof(CPUState, tbr) },
1495 { "fsr", offsetof(CPUState, fsr) },
1496 { "f0", offsetof(CPUState, fpr[0]) },
1497 { "f1", offsetof(CPUState, fpr[1]) },
1498 { "f2", offsetof(CPUState, fpr[2]) },
1499 { "f3", offsetof(CPUState, fpr[3]) },
1500 { "f4", offsetof(CPUState, fpr[4]) },
1501 { "f5", offsetof(CPUState, fpr[5]) },
1502 { "f6", offsetof(CPUState, fpr[6]) },
1503 { "f7", offsetof(CPUState, fpr[7]) },
1504 { "f8", offsetof(CPUState, fpr[8]) },
1505 { "f9", offsetof(CPUState, fpr[9]) },
1506 { "f10", offsetof(CPUState, fpr[10]) },
1507 { "f11", offsetof(CPUState, fpr[11]) },
1508 { "f12", offsetof(CPUState, fpr[12]) },
1509 { "f13", offsetof(CPUState, fpr[13]) },
1510 { "f14", offsetof(CPUState, fpr[14]) },
1511 { "f15", offsetof(CPUState, fpr[15]) },
1512 { "f16", offsetof(CPUState, fpr[16]) },
1513 { "f17", offsetof(CPUState, fpr[17]) },
1514 { "f18", offsetof(CPUState, fpr[18]) },
1515 { "f19", offsetof(CPUState, fpr[19]) },
1516 { "f20", offsetof(CPUState, fpr[20]) },
1517 { "f21", offsetof(CPUState, fpr[21]) },
1518 { "f22", offsetof(CPUState, fpr[22]) },
1519 { "f23", offsetof(CPUState, fpr[23]) },
1520 { "f24", offsetof(CPUState, fpr[24]) },
1521 { "f25", offsetof(CPUState, fpr[25]) },
1522 { "f26", offsetof(CPUState, fpr[26]) },
1523 { "f27", offsetof(CPUState, fpr[27]) },
1524 { "f28", offsetof(CPUState, fpr[28]) },
1525 { "f29", offsetof(CPUState, fpr[29]) },
1526 { "f30", offsetof(CPUState, fpr[30]) },
1527 { "f31", offsetof(CPUState, fpr[31]) },
1528 #ifdef TARGET_SPARC64
1529 { "f32", offsetof(CPUState, fpr[32]) },
1530 { "f34", offsetof(CPUState, fpr[34]) },
1531 { "f36", offsetof(CPUState, fpr[36]) },
1532 { "f38", offsetof(CPUState, fpr[38]) },
1533 { "f40", offsetof(CPUState, fpr[40]) },
1534 { "f42", offsetof(CPUState, fpr[42]) },
1535 { "f44", offsetof(CPUState, fpr[44]) },
1536 { "f46", offsetof(CPUState, fpr[46]) },
1537 { "f48", offsetof(CPUState, fpr[48]) },
1538 { "f50", offsetof(CPUState, fpr[50]) },
1539 { "f52", offsetof(CPUState, fpr[52]) },
1540 { "f54", offsetof(CPUState, fpr[54]) },
1541 { "f56", offsetof(CPUState, fpr[56]) },
1542 { "f58", offsetof(CPUState, fpr[58]) },
1543 { "f60", offsetof(CPUState, fpr[60]) },
1544 { "f62", offsetof(CPUState, fpr[62]) },
1545 { "asi", offsetof(CPUState, asi) },
1546 { "pstate", offsetof(CPUState, pstate) },
1547 { "cansave", offsetof(CPUState, cansave) },
1548 { "canrestore", offsetof(CPUState, canrestore) },
1549 { "otherwin", offsetof(CPUState, otherwin) },
1550 { "wstate", offsetof(CPUState, wstate) },
1551 { "cleanwin", offsetof(CPUState, cleanwin) },
1552 { "fprs", offsetof(CPUState, fprs) },
1553 #endif
1554 #endif
1555 { NULL },
1558 static void expr_error(const char *fmt)
1560 term_printf(fmt);
1561 term_printf("\n");
1562 longjmp(expr_env, 1);
1565 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1566 static int get_monitor_def(target_long *pval, const char *name)
1568 MonitorDef *md;
1569 void *ptr;
1571 for(md = monitor_defs; md->name != NULL; md++) {
1572 if (compare_cmd(name, md->name)) {
1573 if (md->get_value) {
1574 *pval = md->get_value(md, md->offset);
1575 } else {
1576 CPUState *env = mon_get_cpu();
1577 if (!env)
1578 return -2;
1579 ptr = (uint8_t *)env + md->offset;
1580 switch(md->type) {
1581 case MD_I32:
1582 *pval = *(int32_t *)ptr;
1583 break;
1584 case MD_TLONG:
1585 *pval = *(target_long *)ptr;
1586 break;
1587 default:
1588 *pval = 0;
1589 break;
1592 return 0;
1595 return -1;
1598 static void next(void)
1600 if (pch != '\0') {
1601 pch++;
1602 while (isspace(*pch))
1603 pch++;
1607 static target_long expr_sum(void);
1609 static target_long expr_unary(void)
1611 target_long n;
1612 char *p;
1613 int ret;
1615 switch(*pch) {
1616 case '+':
1617 next();
1618 n = expr_unary();
1619 break;
1620 case '-':
1621 next();
1622 n = -expr_unary();
1623 break;
1624 case '~':
1625 next();
1626 n = ~expr_unary();
1627 break;
1628 case '(':
1629 next();
1630 n = expr_sum();
1631 if (*pch != ')') {
1632 expr_error("')' expected");
1634 next();
1635 break;
1636 case '\'':
1637 pch++;
1638 if (*pch == '\0')
1639 expr_error("character constant expected");
1640 n = *pch;
1641 pch++;
1642 if (*pch != '\'')
1643 expr_error("missing terminating \' character");
1644 next();
1645 break;
1646 case '$':
1648 char buf[128], *q;
1650 pch++;
1651 q = buf;
1652 while ((*pch >= 'a' && *pch <= 'z') ||
1653 (*pch >= 'A' && *pch <= 'Z') ||
1654 (*pch >= '0' && *pch <= '9') ||
1655 *pch == '_' || *pch == '.') {
1656 if ((q - buf) < sizeof(buf) - 1)
1657 *q++ = *pch;
1658 pch++;
1660 while (isspace(*pch))
1661 pch++;
1662 *q = 0;
1663 ret = get_monitor_def(&n, buf);
1664 if (ret == -1)
1665 expr_error("unknown register");
1666 else if (ret == -2)
1667 expr_error("no cpu defined");
1669 break;
1670 case '\0':
1671 expr_error("unexpected end of expression");
1672 n = 0;
1673 break;
1674 default:
1675 #if TARGET_LONG_BITS == 64
1676 n = strtoull(pch, &p, 0);
1677 #else
1678 n = strtoul(pch, &p, 0);
1679 #endif
1680 if (pch == p) {
1681 expr_error("invalid char in expression");
1683 pch = p;
1684 while (isspace(*pch))
1685 pch++;
1686 break;
1688 return n;
1692 static target_long expr_prod(void)
1694 target_long val, val2;
1695 int op;
1697 val = expr_unary();
1698 for(;;) {
1699 op = *pch;
1700 if (op != '*' && op != '/' && op != '%')
1701 break;
1702 next();
1703 val2 = expr_unary();
1704 switch(op) {
1705 default:
1706 case '*':
1707 val *= val2;
1708 break;
1709 case '/':
1710 case '%':
1711 if (val2 == 0)
1712 expr_error("division by zero");
1713 if (op == '/')
1714 val /= val2;
1715 else
1716 val %= val2;
1717 break;
1720 return val;
1723 static target_long expr_logic(void)
1725 target_long val, val2;
1726 int op;
1728 val = expr_prod();
1729 for(;;) {
1730 op = *pch;
1731 if (op != '&' && op != '|' && op != '^')
1732 break;
1733 next();
1734 val2 = expr_prod();
1735 switch(op) {
1736 default:
1737 case '&':
1738 val &= val2;
1739 break;
1740 case '|':
1741 val |= val2;
1742 break;
1743 case '^':
1744 val ^= val2;
1745 break;
1748 return val;
1751 static target_long expr_sum(void)
1753 target_long val, val2;
1754 int op;
1756 val = expr_logic();
1757 for(;;) {
1758 op = *pch;
1759 if (op != '+' && op != '-')
1760 break;
1761 next();
1762 val2 = expr_logic();
1763 if (op == '+')
1764 val += val2;
1765 else
1766 val -= val2;
1768 return val;
1771 static int get_expr(target_long *pval, const char **pp)
1773 pch = *pp;
1774 if (setjmp(expr_env)) {
1775 *pp = pch;
1776 return -1;
1778 while (isspace(*pch))
1779 pch++;
1780 *pval = expr_sum();
1781 *pp = pch;
1782 return 0;
1785 static int get_str(char *buf, int buf_size, const char **pp)
1787 const char *p;
1788 char *q;
1789 int c;
1791 q = buf;
1792 p = *pp;
1793 while (isspace(*p))
1794 p++;
1795 if (*p == '\0') {
1796 fail:
1797 *q = '\0';
1798 *pp = p;
1799 return -1;
1801 if (*p == '\"') {
1802 p++;
1803 while (*p != '\0' && *p != '\"') {
1804 if (*p == '\\') {
1805 p++;
1806 c = *p++;
1807 switch(c) {
1808 case 'n':
1809 c = '\n';
1810 break;
1811 case 'r':
1812 c = '\r';
1813 break;
1814 case '\\':
1815 case '\'':
1816 case '\"':
1817 break;
1818 default:
1819 qemu_printf("unsupported escape code: '\\%c'\n", c);
1820 goto fail;
1822 if ((q - buf) < buf_size - 1) {
1823 *q++ = c;
1825 } else {
1826 if ((q - buf) < buf_size - 1) {
1827 *q++ = *p;
1829 p++;
1832 if (*p != '\"') {
1833 qemu_printf("unterminated string\n");
1834 goto fail;
1836 p++;
1837 } else {
1838 while (*p != '\0' && !isspace(*p)) {
1839 if ((q - buf) < buf_size - 1) {
1840 *q++ = *p;
1842 p++;
1845 *q = '\0';
1846 *pp = p;
1847 return 0;
1850 static int default_fmt_format = 'x';
1851 static int default_fmt_size = 4;
1853 #define MAX_ARGS 16
1855 static void monitor_handle_command(const char *cmdline)
1857 const char *p, *pstart, *typestr;
1858 char *q;
1859 int c, nb_args, len, i, has_arg;
1860 term_cmd_t *cmd;
1861 char cmdname[256];
1862 char buf[1024];
1863 void *str_allocated[MAX_ARGS];
1864 void *args[MAX_ARGS];
1866 #ifdef DEBUG
1867 term_printf("command='%s'\n", cmdline);
1868 #endif
1870 /* extract the command name */
1871 p = cmdline;
1872 q = cmdname;
1873 while (isspace(*p))
1874 p++;
1875 if (*p == '\0')
1876 return;
1877 pstart = p;
1878 while (*p != '\0' && *p != '/' && !isspace(*p))
1879 p++;
1880 len = p - pstart;
1881 if (len > sizeof(cmdname) - 1)
1882 len = sizeof(cmdname) - 1;
1883 memcpy(cmdname, pstart, len);
1884 cmdname[len] = '\0';
1886 /* find the command */
1887 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1888 if (compare_cmd(cmdname, cmd->name))
1889 goto found;
1891 term_printf("unknown command: '%s'\n", cmdname);
1892 return;
1893 found:
1895 for(i = 0; i < MAX_ARGS; i++)
1896 str_allocated[i] = NULL;
1898 /* parse the parameters */
1899 typestr = cmd->args_type;
1900 nb_args = 0;
1901 for(;;) {
1902 c = *typestr;
1903 if (c == '\0')
1904 break;
1905 typestr++;
1906 switch(c) {
1907 case 'F':
1908 case 'B':
1909 case 's':
1911 int ret;
1912 char *str;
1914 while (isspace(*p))
1915 p++;
1916 if (*typestr == '?') {
1917 typestr++;
1918 if (*p == '\0') {
1919 /* no optional string: NULL argument */
1920 str = NULL;
1921 goto add_str;
1924 ret = get_str(buf, sizeof(buf), &p);
1925 if (ret < 0) {
1926 switch(c) {
1927 case 'F':
1928 term_printf("%s: filename expected\n", cmdname);
1929 break;
1930 case 'B':
1931 term_printf("%s: block device name expected\n", cmdname);
1932 break;
1933 default:
1934 term_printf("%s: string expected\n", cmdname);
1935 break;
1937 goto fail;
1939 str = qemu_malloc(strlen(buf) + 1);
1940 strcpy(str, buf);
1941 str_allocated[nb_args] = str;
1942 add_str:
1943 if (nb_args >= MAX_ARGS) {
1944 error_args:
1945 term_printf("%s: too many arguments\n", cmdname);
1946 goto fail;
1948 args[nb_args++] = str;
1950 break;
1951 case '/':
1953 int count, format, size;
1955 while (isspace(*p))
1956 p++;
1957 if (*p == '/') {
1958 /* format found */
1959 p++;
1960 count = 1;
1961 if (isdigit(*p)) {
1962 count = 0;
1963 while (isdigit(*p)) {
1964 count = count * 10 + (*p - '0');
1965 p++;
1968 size = -1;
1969 format = -1;
1970 for(;;) {
1971 switch(*p) {
1972 case 'o':
1973 case 'd':
1974 case 'u':
1975 case 'x':
1976 case 'i':
1977 case 'c':
1978 format = *p++;
1979 break;
1980 case 'b':
1981 size = 1;
1982 p++;
1983 break;
1984 case 'h':
1985 size = 2;
1986 p++;
1987 break;
1988 case 'w':
1989 size = 4;
1990 p++;
1991 break;
1992 case 'g':
1993 case 'L':
1994 size = 8;
1995 p++;
1996 break;
1997 default:
1998 goto next;
2001 next:
2002 if (*p != '\0' && !isspace(*p)) {
2003 term_printf("invalid char in format: '%c'\n", *p);
2004 goto fail;
2006 if (format < 0)
2007 format = default_fmt_format;
2008 if (format != 'i') {
2009 /* for 'i', not specifying a size gives -1 as size */
2010 if (size < 0)
2011 size = default_fmt_size;
2013 default_fmt_size = size;
2014 default_fmt_format = format;
2015 } else {
2016 count = 1;
2017 format = default_fmt_format;
2018 if (format != 'i') {
2019 size = default_fmt_size;
2020 } else {
2021 size = -1;
2024 if (nb_args + 3 > MAX_ARGS)
2025 goto error_args;
2026 args[nb_args++] = (void*)count;
2027 args[nb_args++] = (void*)format;
2028 args[nb_args++] = (void*)size;
2030 break;
2031 case 'i':
2032 case 'l':
2034 target_long val;
2035 while (isspace(*p))
2036 p++;
2037 if (*typestr == '?' || *typestr == '.') {
2038 if (*typestr == '?') {
2039 if (*p == '\0')
2040 has_arg = 0;
2041 else
2042 has_arg = 1;
2043 } else {
2044 if (*p == '.') {
2045 p++;
2046 while (isspace(*p))
2047 p++;
2048 has_arg = 1;
2049 } else {
2050 has_arg = 0;
2053 typestr++;
2054 if (nb_args >= MAX_ARGS)
2055 goto error_args;
2056 args[nb_args++] = (void *)has_arg;
2057 if (!has_arg) {
2058 if (nb_args >= MAX_ARGS)
2059 goto error_args;
2060 val = -1;
2061 goto add_num;
2064 if (get_expr(&val, &p))
2065 goto fail;
2066 add_num:
2067 if (c == 'i') {
2068 if (nb_args >= MAX_ARGS)
2069 goto error_args;
2070 args[nb_args++] = (void *)(int)val;
2071 } else {
2072 if ((nb_args + 1) >= MAX_ARGS)
2073 goto error_args;
2074 #if TARGET_LONG_BITS == 64
2075 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2076 #else
2077 args[nb_args++] = (void *)0;
2078 #endif
2079 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2082 break;
2083 case '-':
2085 int has_option;
2086 /* option */
2088 c = *typestr++;
2089 if (c == '\0')
2090 goto bad_type;
2091 while (isspace(*p))
2092 p++;
2093 has_option = 0;
2094 if (*p == '-') {
2095 p++;
2096 if (*p != c) {
2097 term_printf("%s: unsupported option -%c\n",
2098 cmdname, *p);
2099 goto fail;
2101 p++;
2102 has_option = 1;
2104 if (nb_args >= MAX_ARGS)
2105 goto error_args;
2106 args[nb_args++] = (void *)has_option;
2108 break;
2109 default:
2110 bad_type:
2111 term_printf("%s: unknown type '%c'\n", cmdname, c);
2112 goto fail;
2115 /* check that all arguments were parsed */
2116 while (isspace(*p))
2117 p++;
2118 if (*p != '\0') {
2119 term_printf("%s: extraneous characters at the end of line\n",
2120 cmdname);
2121 goto fail;
2124 switch(nb_args) {
2125 case 0:
2126 cmd->handler();
2127 break;
2128 case 1:
2129 cmd->handler(args[0]);
2130 break;
2131 case 2:
2132 cmd->handler(args[0], args[1]);
2133 break;
2134 case 3:
2135 cmd->handler(args[0], args[1], args[2]);
2136 break;
2137 case 4:
2138 cmd->handler(args[0], args[1], args[2], args[3]);
2139 break;
2140 case 5:
2141 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2142 break;
2143 case 6:
2144 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2145 break;
2146 case 7:
2147 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2148 break;
2149 default:
2150 term_printf("unsupported number of arguments: %d\n", nb_args);
2151 goto fail;
2153 fail:
2154 for(i = 0; i < MAX_ARGS; i++)
2155 qemu_free(str_allocated[i]);
2156 return;
2159 static void cmd_completion(const char *name, const char *list)
2161 const char *p, *pstart;
2162 char cmd[128];
2163 int len;
2165 p = list;
2166 for(;;) {
2167 pstart = p;
2168 p = strchr(p, '|');
2169 if (!p)
2170 p = pstart + strlen(pstart);
2171 len = p - pstart;
2172 if (len > sizeof(cmd) - 2)
2173 len = sizeof(cmd) - 2;
2174 memcpy(cmd, pstart, len);
2175 cmd[len] = '\0';
2176 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2177 add_completion(cmd);
2179 if (*p == '\0')
2180 break;
2181 p++;
2185 static void file_completion(const char *input)
2187 DIR *ffs;
2188 struct dirent *d;
2189 char path[1024];
2190 char file[1024], file_prefix[1024];
2191 int input_path_len;
2192 const char *p;
2194 p = strrchr(input, '/');
2195 if (!p) {
2196 input_path_len = 0;
2197 pstrcpy(file_prefix, sizeof(file_prefix), input);
2198 strcpy(path, ".");
2199 } else {
2200 input_path_len = p - input + 1;
2201 memcpy(path, input, input_path_len);
2202 if (input_path_len > sizeof(path) - 1)
2203 input_path_len = sizeof(path) - 1;
2204 path[input_path_len] = '\0';
2205 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2207 #ifdef DEBUG_COMPLETION
2208 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2209 #endif
2210 ffs = opendir(path);
2211 if (!ffs)
2212 return;
2213 for(;;) {
2214 struct stat sb;
2215 d = readdir(ffs);
2216 if (!d)
2217 break;
2218 if (strstart(d->d_name, file_prefix, NULL)) {
2219 memcpy(file, input, input_path_len);
2220 strcpy(file + input_path_len, d->d_name);
2221 /* stat the file to find out if it's a directory.
2222 * In that case add a slash to speed up typing long paths
2224 stat(file, &sb);
2225 if(S_ISDIR(sb.st_mode))
2226 strcat(file, "/");
2227 add_completion(file);
2230 closedir(ffs);
2233 static void block_completion_it(void *opaque, const char *name)
2235 const char *input = opaque;
2237 if (input[0] == '\0' ||
2238 !strncmp(name, (char *)input, strlen(input))) {
2239 add_completion(name);
2243 /* NOTE: this parser is an approximate form of the real command parser */
2244 static void parse_cmdline(const char *cmdline,
2245 int *pnb_args, char **args)
2247 const char *p;
2248 int nb_args, ret;
2249 char buf[1024];
2251 p = cmdline;
2252 nb_args = 0;
2253 for(;;) {
2254 while (isspace(*p))
2255 p++;
2256 if (*p == '\0')
2257 break;
2258 if (nb_args >= MAX_ARGS)
2259 break;
2260 ret = get_str(buf, sizeof(buf), &p);
2261 args[nb_args] = qemu_strdup(buf);
2262 nb_args++;
2263 if (ret < 0)
2264 break;
2266 *pnb_args = nb_args;
2269 void readline_find_completion(const char *cmdline)
2271 const char *cmdname;
2272 char *args[MAX_ARGS];
2273 int nb_args, i, len;
2274 const char *ptype, *str;
2275 term_cmd_t *cmd;
2276 const KeyDef *key;
2278 parse_cmdline(cmdline, &nb_args, args);
2279 #ifdef DEBUG_COMPLETION
2280 for(i = 0; i < nb_args; i++) {
2281 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2283 #endif
2285 /* if the line ends with a space, it means we want to complete the
2286 next arg */
2287 len = strlen(cmdline);
2288 if (len > 0 && isspace(cmdline[len - 1])) {
2289 if (nb_args >= MAX_ARGS)
2290 return;
2291 args[nb_args++] = qemu_strdup("");
2293 if (nb_args <= 1) {
2294 /* command completion */
2295 if (nb_args == 0)
2296 cmdname = "";
2297 else
2298 cmdname = args[0];
2299 completion_index = strlen(cmdname);
2300 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2301 cmd_completion(cmdname, cmd->name);
2303 } else {
2304 /* find the command */
2305 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2306 if (compare_cmd(args[0], cmd->name))
2307 goto found;
2309 return;
2310 found:
2311 ptype = cmd->args_type;
2312 for(i = 0; i < nb_args - 2; i++) {
2313 if (*ptype != '\0') {
2314 ptype++;
2315 while (*ptype == '?')
2316 ptype++;
2319 str = args[nb_args - 1];
2320 switch(*ptype) {
2321 case 'F':
2322 /* file completion */
2323 completion_index = strlen(str);
2324 file_completion(str);
2325 break;
2326 case 'B':
2327 /* block device name completion */
2328 completion_index = strlen(str);
2329 bdrv_iterate(block_completion_it, (void *)str);
2330 break;
2331 case 's':
2332 /* XXX: more generic ? */
2333 if (!strcmp(cmd->name, "info")) {
2334 completion_index = strlen(str);
2335 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2336 cmd_completion(str, cmd->name);
2338 } else if (!strcmp(cmd->name, "sendkey")) {
2339 completion_index = strlen(str);
2340 for(key = key_defs; key->name != NULL; key++) {
2341 cmd_completion(str, key->name);
2344 break;
2345 default:
2346 break;
2349 for(i = 0; i < nb_args; i++)
2350 qemu_free(args[i]);
2353 static int term_can_read(void *opaque)
2355 return 128;
2358 static void term_read(void *opaque, const uint8_t *buf, int size)
2360 int i;
2361 for(i = 0; i < size; i++)
2362 readline_handle_byte(buf[i]);
2365 static void monitor_start_input(void);
2367 static void monitor_handle_command1(void *opaque, const char *cmdline)
2369 monitor_handle_command(cmdline);
2370 monitor_start_input();
2373 static void monitor_start_input(void)
2375 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2378 void monitor_init(CharDriverState *hd, int show_banner)
2380 monitor_hd = hd;
2381 if (show_banner) {
2382 term_printf("QEMU %s monitor - type 'help' for more information\n",
2383 QEMU_VERSION);
2385 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2386 monitor_start_input();
2389 /* XXX: use threads ? */
2390 /* modal monitor readline */
2391 static int monitor_readline_started;
2392 static char *monitor_readline_buf;
2393 static int monitor_readline_buf_size;
2395 static void monitor_readline_cb(void *opaque, const char *input)
2397 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2398 monitor_readline_started = 0;
2401 void monitor_readline(const char *prompt, int is_password,
2402 char *buf, int buf_size)
2404 if (is_password) {
2405 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2407 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2408 monitor_readline_buf = buf;
2409 monitor_readline_buf_size = buf_size;
2410 monitor_readline_started = 1;
2411 while (monitor_readline_started) {
2412 main_loop_wait(10);