ia64/xen-unstable

view tools/ioemu/monitor.c @ 10949:4c2fab8f8c34

[qemu] Use xenstore to configure ioemu block devices.
- read ioemu block device config from xenstore
- don't require the ioemu: prefix on block devices any longer
- allow change of media associated with cdrom drives
- replace cdrom= option by :cdrom suffix on regular block device config:
'file:/root/mytest.iso,hdc:cdrom,r'
- don't create default cdrom drive anymore - to create default empty
cdrom drive use: ',hdc:cdrom,r'

Signed-off-by: Christian Limpach <Christian.Limpach@xensource.com>
author chris@kneesaa.uk.xensource.com
date Thu Aug 03 18:28:29 2006 +0100 (2006-08-03)
parents b450f21472a0
children 08a11694b109
line source
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24 #include "vl.h"
25 #include "disas.h"
26 #include <dirent.h>
27 #include "block_int.h"
29 //#define DEBUG
30 //#define DEBUG_COMPLETION
32 #ifndef offsetof
33 #define offsetof(type, field) ((size_t) &((type *)0)->field)
34 #endif
36 /*
37 * Supported types:
38 *
39 * 'F' filename
40 * 'B' block device name
41 * 's' string (accept optional quote)
42 * 'i' 32 bit integer
43 * 'l' target long (32 or 64 bit)
44 * '/' optional gdb-like print format (like "/10x")
45 *
46 * '?' optional type (for 'F', 's' and 'i')
47 *
48 */
50 typedef struct term_cmd_t {
51 const char *name;
52 const char *args_type;
53 void (*handler)();
54 const char *params;
55 const char *help;
56 } term_cmd_t;
58 static CharDriverState *monitor_hd;
60 static term_cmd_t term_cmds[];
61 static term_cmd_t info_cmds[];
63 static char term_outbuf[1024];
64 static int term_outbuf_index;
66 static void monitor_start_input(void);
68 CPUState *mon_cpu = NULL;
70 void term_flush(void)
71 {
72 #ifdef CONFIG_DM
73 if (term_outbuf_index > 0 && !monitor_hd) {
74 fwrite(term_outbuf, term_outbuf_index, 1, stderr);
75 term_outbuf_index = 0;
76 }
77 #endif
78 if (term_outbuf_index > 0) {
79 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
80 term_outbuf_index = 0;
81 }
82 }
84 /* flush at every end of line or if the buffer is full */
85 void term_puts(const char *str)
86 {
87 int c;
88 for(;;) {
89 c = *str++;
90 if (c == '\0')
91 break;
92 term_outbuf[term_outbuf_index++] = c;
93 if (term_outbuf_index >= sizeof(term_outbuf) ||
94 c == '\n')
95 term_flush();
96 }
97 }
99 void term_vprintf(const char *fmt, va_list ap)
100 {
101 char buf[4096];
102 vsnprintf(buf, sizeof(buf), fmt, ap);
103 term_puts(buf);
104 }
106 void term_printf(const char *fmt, ...)
107 {
108 va_list ap;
109 va_start(ap, fmt);
110 term_vprintf(fmt, ap);
111 va_end(ap);
112 }
114 #ifndef CONFIG_DM
115 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
116 {
117 va_list ap;
118 va_start(ap, fmt);
119 term_vprintf(fmt, ap);
120 va_end(ap);
121 return 0;
122 }
123 #endif /* !CONFIG_DM */
125 static int compare_cmd(const char *name, const char *list)
126 {
127 const char *p, *pstart;
128 int len;
129 len = strlen(name);
130 p = list;
131 for(;;) {
132 pstart = p;
133 p = strchr(p, '|');
134 if (!p)
135 p = pstart + strlen(pstart);
136 if ((p - pstart) == len && !memcmp(pstart, name, len))
137 return 1;
138 if (*p == '\0')
139 break;
140 p++;
141 }
142 return 0;
143 }
145 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
146 {
147 term_cmd_t *cmd;
149 for(cmd = cmds; cmd->name != NULL; cmd++) {
150 if (!name || !strcmp(name, cmd->name))
151 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
152 }
153 }
155 static void help_cmd(const char *name)
156 {
157 if (name && !strcmp(name, "info")) {
158 help_cmd1(info_cmds, "info ", NULL);
159 } else {
160 help_cmd1(term_cmds, "", name);
161 if (name && !strcmp(name, "log")) {
162 CPULogItem *item;
163 term_printf("Log items (comma separated):\n");
164 term_printf("%-10s %s\n", "none", "remove all logs");
165 for(item = cpu_log_items; item->mask != 0; item++) {
166 term_printf("%-10s %s\n", item->name, item->help);
167 }
168 }
169 }
170 }
172 static void do_help(const char *name)
173 {
174 help_cmd(name);
175 }
177 static void do_commit(void)
178 {
179 int i;
181 for (i = 0; i < MAX_DISKS; i++) {
182 if (bs_table[i]) {
183 bdrv_commit(bs_table[i]);
184 }
185 }
186 }
188 static void do_info(const char *item)
189 {
190 term_cmd_t *cmd;
192 if (!item)
193 goto help;
194 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
195 if (compare_cmd(item, cmd->name))
196 goto found;
197 }
198 help:
199 help_cmd("info");
200 return;
201 found:
202 cmd->handler();
203 }
205 static void do_info_version(void)
206 {
207 term_printf("%s\n", QEMU_VERSION);
208 }
210 static void do_info_block(void)
211 {
212 bdrv_info();
213 }
215 /* get the current CPU defined by the user */
216 int mon_set_cpu(int cpu_index)
217 {
218 CPUState *env;
220 for(env = first_cpu; env != NULL; env = env->next_cpu) {
221 if (env->cpu_index == cpu_index) {
222 mon_cpu = env;
223 return 0;
224 }
225 }
226 return -1;
227 }
229 CPUState *mon_get_cpu(void)
230 {
231 if (!mon_cpu) {
232 mon_set_cpu(0);
233 }
234 return mon_cpu;
235 }
237 #ifndef CONFIG_DM
238 static void do_info_registers(void)
239 {
240 CPUState *env;
241 env = mon_get_cpu();
242 if (!env)
243 return;
244 #ifdef TARGET_I386
245 cpu_dump_state(env, NULL, monitor_fprintf,
246 X86_DUMP_FPU);
247 #else
248 cpu_dump_state(env, NULL, monitor_fprintf,
249 0);
250 #endif
251 }
253 static void do_info_cpus(void)
254 {
255 CPUState *env;
257 /* just to set the default cpu if not already done */
258 mon_get_cpu();
260 for(env = first_cpu; env != NULL; env = env->next_cpu) {
261 term_printf("%c CPU #%d:",
262 (env == mon_cpu) ? '*' : ' ',
263 env->cpu_index);
264 #if defined(TARGET_I386)
265 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
266 if (env->hflags & HF_HALTED_MASK)
267 term_printf(" (halted)");
268 #elif defined(TARGET_PPC)
269 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
270 if (env->halted)
271 term_printf(" (halted)");
272 #elif defined(TARGET_SPARC)
273 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
274 if (env->halted)
275 term_printf(" (halted)");
276 #endif
277 term_printf("\n");
278 }
279 }
281 static void do_cpu_set(int index)
282 {
283 if (mon_set_cpu(index) < 0)
284 term_printf("Invalid CPU index\n");
285 }
287 static void do_info_jit(void)
288 {
289 dump_exec_info(NULL, monitor_fprintf);
290 }
291 #endif /* !CONFIG_DM */
293 static void do_info_history (void)
294 {
295 int i;
296 const char *str;
298 i = 0;
299 for(;;) {
300 str = readline_get_history(i);
301 if (!str)
302 break;
303 term_printf("%d: '%s'\n", i, str);
304 i++;
305 }
306 }
308 static void do_quit(void)
309 {
310 destroy_hvm_domain();
311 exit(0);
312 }
314 static int eject_device(BlockDriverState *bs, int force)
315 {
316 if (bdrv_is_inserted(bs)) {
317 if (!force) {
318 if (!bdrv_is_removable(bs)) {
319 term_printf("device is not removable\n");
320 return -1;
321 }
322 if (bdrv_is_locked(bs)) {
323 term_printf("device is locked\n");
324 return -1;
325 }
326 }
327 bdrv_close(bs);
328 }
329 return 0;
330 }
332 void do_eject(int force, const char *filename)
333 {
334 BlockDriverState *bs;
336 bs = bdrv_find(filename);
337 if (!bs) {
338 term_printf("device not found\n");
339 return;
340 }
341 eject_device(bs, force);
342 }
344 void do_change(const char *device, const char *filename)
345 {
346 BlockDriverState *bs;
347 int i;
348 char password[256];
350 bs = bdrv_find(device);
351 if (!bs) {
352 term_printf("device not found\n");
353 return;
354 }
355 if (eject_device(bs, 0) < 0)
356 return;
357 bdrv_open(bs, filename, 0);
358 if (bdrv_is_encrypted(bs)) {
359 term_printf("%s is encrypted.\n", device);
360 for(i = 0; i < 3; i++) {
361 monitor_readline("Password: ", 1, password, sizeof(password));
362 if (bdrv_set_key(bs, password) == 0)
363 break;
364 term_printf("invalid password\n");
365 }
366 }
367 }
369 static void do_screen_dump(const char *filename)
370 {
371 vga_hw_screen_dump(filename);
372 }
374 static void do_log(const char *items)
375 {
376 int mask;
378 if (!strcmp(items, "none")) {
379 mask = 0;
380 } else {
381 mask = cpu_str_to_log_mask(items);
382 if (!mask) {
383 help_cmd("log");
384 return;
385 }
386 }
387 cpu_set_log(mask);
388 }
390 #ifndef CONFIG_DM
391 static void do_savevm(const char *filename)
392 {
393 if (qemu_savevm(filename) < 0)
394 term_printf("I/O error when saving VM to '%s'\n", filename);
395 }
397 static void do_loadvm(const char *filename)
398 {
399 if (qemu_loadvm(filename) < 0)
400 term_printf("I/O error when loading VM from '%s'\n", filename);
401 }
403 static void do_stop(void)
404 {
405 vm_stop(EXCP_INTERRUPT);
406 }
408 static void do_cont(void)
409 {
410 vm_start();
411 }
413 #ifdef CONFIG_GDBSTUB
414 static void do_gdbserver(int has_port, int port)
415 {
416 if (!has_port)
417 port = DEFAULT_GDBSTUB_PORT;
418 if (gdbserver_start(port) < 0) {
419 qemu_printf("Could not open gdbserver socket on port %d\n", port);
420 } else {
421 qemu_printf("Waiting gdb connection on port %d\n", port);
422 }
423 }
424 #endif
426 static void term_printc(int c)
427 {
428 term_printf("'");
429 switch(c) {
430 case '\'':
431 term_printf("\\'");
432 break;
433 case '\\':
434 term_printf("\\\\");
435 break;
436 case '\n':
437 term_printf("\\n");
438 break;
439 case '\r':
440 term_printf("\\r");
441 break;
442 default:
443 if (c >= 32 && c <= 126) {
444 term_printf("%c", c);
445 } else {
446 term_printf("\\x%02x", c);
447 }
448 break;
449 }
450 term_printf("'");
451 }
453 static void memory_dump(int count, int format, int wsize,
454 target_ulong addr, int is_physical)
455 {
456 CPUState *env;
457 int nb_per_line, l, line_size, i, max_digits, len;
458 uint8_t buf[16];
459 uint64_t v;
461 if (format == 'i') {
462 int flags;
463 flags = 0;
464 env = mon_get_cpu();
465 if (!env && !is_physical)
466 return;
467 #ifdef TARGET_I386
468 if (wsize == 2) {
469 flags = 1;
470 } else if (wsize == 4) {
471 flags = 0;
472 } else {
473 /* as default we use the current CS size */
474 flags = 0;
475 if (env) {
476 #ifdef TARGET_X86_64
477 if ((env->efer & MSR_EFER_LMA) &&
478 (env->segs[R_CS].flags & DESC_L_MASK))
479 flags = 2;
480 else
481 #endif
482 if (!(env->segs[R_CS].flags & DESC_B_MASK))
483 flags = 1;
484 }
485 }
486 #endif
487 monitor_disas(env, addr, count, is_physical, flags);
488 return;
489 }
491 len = wsize * count;
492 if (wsize == 1)
493 line_size = 8;
494 else
495 line_size = 16;
496 nb_per_line = line_size / wsize;
497 max_digits = 0;
499 switch(format) {
500 case 'o':
501 max_digits = (wsize * 8 + 2) / 3;
502 break;
503 default:
504 case 'x':
505 max_digits = (wsize * 8) / 4;
506 break;
507 case 'u':
508 case 'd':
509 max_digits = (wsize * 8 * 10 + 32) / 33;
510 break;
511 case 'c':
512 wsize = 1;
513 break;
514 }
516 while (len > 0) {
517 term_printf(TARGET_FMT_lx ":", addr);
518 l = len;
519 if (l > line_size)
520 l = line_size;
521 if (is_physical) {
522 cpu_physical_memory_rw(addr, buf, l, 0);
523 } else {
524 env = mon_get_cpu();
525 if (!env)
526 break;
527 cpu_memory_rw_debug(env, addr, buf, l, 0);
528 }
529 i = 0;
530 while (i < l) {
531 switch(wsize) {
532 default:
533 case 1:
534 v = ldub_raw(buf + i);
535 break;
536 case 2:
537 v = lduw_raw(buf + i);
538 break;
539 case 4:
540 v = (uint32_t)ldl_raw(buf + i);
541 break;
542 case 8:
543 v = ldq_raw(buf + i);
544 break;
545 }
546 term_printf(" ");
547 switch(format) {
548 case 'o':
549 term_printf("%#*llo", max_digits, v);
550 break;
551 case 'x':
552 term_printf("0x%0*llx", max_digits, v);
553 break;
554 case 'u':
555 term_printf("%*llu", max_digits, v);
556 break;
557 case 'd':
558 term_printf("%*lld", max_digits, v);
559 break;
560 case 'c':
561 term_printc(v);
562 break;
563 }
564 i += wsize;
565 }
566 term_printf("\n");
567 addr += l;
568 len -= l;
569 }
570 }
572 #if TARGET_LONG_BITS == 64
573 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
574 #else
575 #define GET_TLONG(h, l) (l)
576 #endif
578 static void do_memory_dump(int count, int format, int size,
579 uint32_t addrh, uint32_t addrl)
580 {
581 target_long addr = GET_TLONG(addrh, addrl);
582 memory_dump(count, format, size, addr, 0);
583 }
585 static void do_physical_memory_dump(int count, int format, int size,
586 uint32_t addrh, uint32_t addrl)
588 {
589 target_long addr = GET_TLONG(addrh, addrl);
590 memory_dump(count, format, size, addr, 1);
591 }
593 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
594 {
595 target_long val = GET_TLONG(valh, vall);
596 #if TARGET_LONG_BITS == 32
597 switch(format) {
598 case 'o':
599 term_printf("%#o", val);
600 break;
601 case 'x':
602 term_printf("%#x", val);
603 break;
604 case 'u':
605 term_printf("%u", val);
606 break;
607 default:
608 case 'd':
609 term_printf("%d", val);
610 break;
611 case 'c':
612 term_printc(val);
613 break;
614 }
615 #else
616 switch(format) {
617 case 'o':
618 term_printf("%#llo", val);
619 break;
620 case 'x':
621 term_printf("%#llx", val);
622 break;
623 case 'u':
624 term_printf("%llu", val);
625 break;
626 default:
627 case 'd':
628 term_printf("%lld", val);
629 break;
630 case 'c':
631 term_printc(val);
632 break;
633 }
634 #endif
635 term_printf("\n");
636 }
637 #endif /* !CONFIG_DM */
639 static void do_sum(uint32_t start, uint32_t size)
640 {
641 uint32_t addr;
642 uint8_t buf[1];
643 uint16_t sum;
645 sum = 0;
646 for(addr = start; addr < (start + size); addr++) {
647 cpu_physical_memory_rw(addr, buf, 1, 0);
648 /* BSD sum algorithm ('sum' Unix command) */
649 sum = (sum >> 1) | (sum << 15);
650 sum += buf[0];
651 }
652 term_printf("%05d\n", sum);
653 }
655 typedef struct {
656 int keycode;
657 const char *name;
658 } KeyDef;
660 static const KeyDef key_defs[] = {
661 { 0x2a, "shift" },
662 { 0x36, "shift_r" },
664 { 0x38, "alt" },
665 { 0xb8, "alt_r" },
666 { 0x1d, "ctrl" },
667 { 0x9d, "ctrl_r" },
669 { 0xdd, "menu" },
671 { 0x01, "esc" },
673 { 0x02, "1" },
674 { 0x03, "2" },
675 { 0x04, "3" },
676 { 0x05, "4" },
677 { 0x06, "5" },
678 { 0x07, "6" },
679 { 0x08, "7" },
680 { 0x09, "8" },
681 { 0x0a, "9" },
682 { 0x0b, "0" },
683 { 0x0e, "backspace" },
685 { 0x0f, "tab" },
686 { 0x10, "q" },
687 { 0x11, "w" },
688 { 0x12, "e" },
689 { 0x13, "r" },
690 { 0x14, "t" },
691 { 0x15, "y" },
692 { 0x16, "u" },
693 { 0x17, "i" },
694 { 0x18, "o" },
695 { 0x19, "p" },
697 { 0x1c, "ret" },
699 { 0x1e, "a" },
700 { 0x1f, "s" },
701 { 0x20, "d" },
702 { 0x21, "f" },
703 { 0x22, "g" },
704 { 0x23, "h" },
705 { 0x24, "j" },
706 { 0x25, "k" },
707 { 0x26, "l" },
709 { 0x2c, "z" },
710 { 0x2d, "x" },
711 { 0x2e, "c" },
712 { 0x2f, "v" },
713 { 0x30, "b" },
714 { 0x31, "n" },
715 { 0x32, "m" },
717 { 0x39, "spc" },
718 { 0x3a, "caps_lock" },
719 { 0x3b, "f1" },
720 { 0x3c, "f2" },
721 { 0x3d, "f3" },
722 { 0x3e, "f4" },
723 { 0x3f, "f5" },
724 { 0x40, "f6" },
725 { 0x41, "f7" },
726 { 0x42, "f8" },
727 { 0x43, "f9" },
728 { 0x44, "f10" },
729 { 0x45, "num_lock" },
730 { 0x46, "scroll_lock" },
732 { 0x56, "<" },
734 { 0x57, "f11" },
735 { 0x58, "f12" },
737 { 0xb7, "print" },
739 { 0xc7, "home" },
740 { 0xc9, "pgup" },
741 { 0xd1, "pgdn" },
742 { 0xcf, "end" },
744 { 0xcb, "left" },
745 { 0xc8, "up" },
746 { 0xd0, "down" },
747 { 0xcd, "right" },
749 { 0xd2, "insert" },
750 { 0xd3, "delete" },
751 { 0, NULL },
752 };
754 static int get_keycode(const char *key)
755 {
756 const KeyDef *p;
758 for(p = key_defs; p->name != NULL; p++) {
759 if (!strcmp(key, p->name))
760 return p->keycode;
761 }
762 return -1;
763 }
765 static void do_send_key(const char *string)
766 {
767 char keybuf[16], *q;
768 uint8_t keycodes[16];
769 const char *p;
770 int nb_keycodes, keycode, i;
772 nb_keycodes = 0;
773 p = string;
774 while (*p != '\0') {
775 q = keybuf;
776 while (*p != '\0' && *p != '-') {
777 if ((q - keybuf) < sizeof(keybuf) - 1) {
778 *q++ = *p;
779 }
780 p++;
781 }
782 *q = '\0';
783 keycode = get_keycode(keybuf);
784 if (keycode < 0) {
785 term_printf("unknown key: '%s'\n", keybuf);
786 return;
787 }
788 keycodes[nb_keycodes++] = keycode;
789 if (*p == '\0')
790 break;
791 p++;
792 }
793 /* key down events */
794 for(i = 0; i < nb_keycodes; i++) {
795 keycode = keycodes[i];
796 if (keycode & 0x80)
797 kbd_put_keycode(0xe0);
798 kbd_put_keycode(keycode & 0x7f);
799 }
800 /* key up events */
801 for(i = nb_keycodes - 1; i >= 0; i--) {
802 keycode = keycodes[i];
803 if (keycode & 0x80)
804 kbd_put_keycode(0xe0);
805 kbd_put_keycode(keycode | 0x80);
806 }
807 }
809 #ifndef CONFIG_DM
810 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
811 {
812 uint32_t val;
813 int suffix;
815 if (has_index) {
816 cpu_outb(NULL, addr & 0xffff, index & 0xff);
817 addr++;
818 }
819 addr &= 0xffff;
821 switch(size) {
822 default:
823 case 1:
824 val = cpu_inb(NULL, addr);
825 suffix = 'b';
826 break;
827 case 2:
828 val = cpu_inw(NULL, addr);
829 suffix = 'w';
830 break;
831 case 4:
832 val = cpu_inl(NULL, addr);
833 suffix = 'l';
834 break;
835 }
836 term_printf("port%c[0x%04x] = %#0*x\n",
837 suffix, addr, size * 2, val);
838 }
840 static void do_system_reset(void)
841 {
842 qemu_system_reset_request();
843 }
845 static void do_system_powerdown(void)
846 {
847 qemu_system_powerdown_request();
848 }
850 #if defined(TARGET_I386)
851 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
852 {
853 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
854 addr,
855 pte & mask,
856 pte & PG_GLOBAL_MASK ? 'G' : '-',
857 pte & PG_PSE_MASK ? 'P' : '-',
858 pte & PG_DIRTY_MASK ? 'D' : '-',
859 pte & PG_ACCESSED_MASK ? 'A' : '-',
860 pte & PG_PCD_MASK ? 'C' : '-',
861 pte & PG_PWT_MASK ? 'T' : '-',
862 pte & PG_USER_MASK ? 'U' : '-',
863 pte & PG_RW_MASK ? 'W' : '-');
864 }
866 static void tlb_info(void)
867 {
868 CPUState *env;
869 int l1, l2;
870 uint32_t pgd, pde, pte;
872 env = mon_get_cpu();
873 if (!env)
874 return;
876 if (!(env->cr[0] & CR0_PG_MASK)) {
877 term_printf("PG disabled\n");
878 return;
879 }
880 pgd = env->cr[3] & ~0xfff;
881 for(l1 = 0; l1 < 1024; l1++) {
882 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
883 pde = le32_to_cpu(pde);
884 if (pde & PG_PRESENT_MASK) {
885 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
886 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
887 } else {
888 for(l2 = 0; l2 < 1024; l2++) {
889 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
890 (uint8_t *)&pte, 4);
891 pte = le32_to_cpu(pte);
892 if (pte & PG_PRESENT_MASK) {
893 print_pte((l1 << 22) + (l2 << 12),
894 pte & ~PG_PSE_MASK,
895 ~0xfff);
896 }
897 }
898 }
899 }
900 }
901 }
903 static void mem_print(uint32_t *pstart, int *plast_prot,
904 uint32_t end, int prot)
905 {
906 int prot1;
907 prot1 = *plast_prot;
908 if (prot != prot1) {
909 if (*pstart != -1) {
910 term_printf("%08x-%08x %08x %c%c%c\n",
911 *pstart, end, end - *pstart,
912 prot1 & PG_USER_MASK ? 'u' : '-',
913 'r',
914 prot1 & PG_RW_MASK ? 'w' : '-');
915 }
916 if (prot != 0)
917 *pstart = end;
918 else
919 *pstart = -1;
920 *plast_prot = prot;
921 }
922 }
924 static void mem_info(void)
925 {
926 CPUState *env;
927 int l1, l2, prot, last_prot;
928 uint32_t pgd, pde, pte, start, end;
930 env = mon_get_cpu();
931 if (!env)
932 return;
934 if (!(env->cr[0] & CR0_PG_MASK)) {
935 term_printf("PG disabled\n");
936 return;
937 }
938 pgd = env->cr[3] & ~0xfff;
939 last_prot = 0;
940 start = -1;
941 for(l1 = 0; l1 < 1024; l1++) {
942 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
943 pde = le32_to_cpu(pde);
944 end = l1 << 22;
945 if (pde & PG_PRESENT_MASK) {
946 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
947 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
948 mem_print(&start, &last_prot, end, prot);
949 } else {
950 for(l2 = 0; l2 < 1024; l2++) {
951 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
952 (uint8_t *)&pte, 4);
953 pte = le32_to_cpu(pte);
954 end = (l1 << 22) + (l2 << 12);
955 if (pte & PG_PRESENT_MASK) {
956 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
957 } else {
958 prot = 0;
959 }
960 mem_print(&start, &last_prot, end, prot);
961 }
962 }
963 } else {
964 prot = 0;
965 mem_print(&start, &last_prot, end, prot);
966 }
967 }
968 }
969 #endif
970 #endif /* !CONFIG_DM */
972 static void do_info_kqemu(void)
973 {
974 #ifdef USE_KQEMU
975 CPUState *env;
976 int val;
977 val = 0;
978 env = mon_get_cpu();
979 if (!env) {
980 term_printf("No cpu initialized yet");
981 return;
982 }
983 val = env->kqemu_enabled;
984 term_printf("kqemu support: ");
985 switch(val) {
986 default:
987 case 0:
988 term_printf("disabled\n");
989 break;
990 case 1:
991 term_printf("enabled for user code\n");
992 break;
993 case 2:
994 term_printf("enabled for user and kernel code\n");
995 break;
996 }
997 #else
998 term_printf("kqemu support: not compiled\n");
999 #endif
1002 #ifdef CONFIG_PROFILER
1004 int64_t kqemu_time;
1005 int64_t qemu_time;
1006 int64_t kqemu_exec_count;
1007 int64_t dev_time;
1008 int64_t kqemu_ret_int_count;
1009 int64_t kqemu_ret_excp_count;
1010 int64_t kqemu_ret_intr_count;
1012 static void do_info_profile(void)
1014 int64_t total;
1015 total = qemu_time;
1016 if (total == 0)
1017 total = 1;
1018 term_printf("async time %lld (%0.3f)\n",
1019 dev_time, dev_time / (double)ticks_per_sec);
1020 term_printf("qemu time %lld (%0.3f)\n",
1021 qemu_time, qemu_time / (double)ticks_per_sec);
1022 term_printf("kqemu time %lld (%0.3f %0.1f%%) count=%lld int=%lld excp=%lld intr=%lld\n",
1023 kqemu_time, kqemu_time / (double)ticks_per_sec,
1024 kqemu_time / (double)total * 100.0,
1025 kqemu_exec_count,
1026 kqemu_ret_int_count,
1027 kqemu_ret_excp_count,
1028 kqemu_ret_intr_count);
1029 qemu_time = 0;
1030 kqemu_time = 0;
1031 kqemu_exec_count = 0;
1032 dev_time = 0;
1033 kqemu_ret_int_count = 0;
1034 kqemu_ret_excp_count = 0;
1035 kqemu_ret_intr_count = 0;
1036 #ifdef USE_KQEMU
1037 kqemu_record_dump();
1038 #endif
1040 #else
1041 static void do_info_profile(void)
1043 term_printf("Internal profiler not compiled\n");
1045 #endif
1047 static term_cmd_t term_cmds[] = {
1048 { "help|?", "s?", do_help,
1049 "[cmd]", "show the help" },
1050 { "commit", "", do_commit,
1051 "", "commit changes to the disk images (if -snapshot is used)" },
1052 { "info", "s?", do_info,
1053 "subcommand", "show various information about the system state" },
1054 { "q|quit", "", do_quit,
1055 "", "quit the emulator" },
1056 { "eject", "-fB", do_eject,
1057 "[-f] device", "eject a removable media (use -f to force it)" },
1058 { "change", "BF", do_change,
1059 "device filename", "change a removable media" },
1060 { "screendump", "F", do_screen_dump,
1061 "filename", "save screen into PPM image 'filename'" },
1062 { "log", "s", do_log,
1063 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1064 #ifndef CONFIG_DM
1065 { "savevm", "F", do_savevm,
1066 "filename", "save the whole virtual machine state to 'filename'" },
1067 { "loadvm", "F", do_loadvm,
1068 "filename", "restore the whole virtual machine state from 'filename'" },
1069 { "stop", "", do_stop,
1070 "", "stop emulation", },
1071 { "c|cont", "", do_cont,
1072 "", "resume emulation", },
1073 #ifdef CONFIG_GDBSTUB
1074 { "gdbserver", "i?", do_gdbserver,
1075 "[port]", "start gdbserver session (default port=1234)", },
1076 #endif
1077 { "x", "/l", do_memory_dump,
1078 "/fmt addr", "virtual memory dump starting at 'addr'", },
1079 { "xp", "/l", do_physical_memory_dump,
1080 "/fmt addr", "physical memory dump starting at 'addr'", },
1081 { "p|print", "/l", do_print,
1082 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1083 { "i", "/ii.", do_ioport_read,
1084 "/fmt addr", "I/O port read" },
1085 #endif/* !CONFIG_DM */
1087 { "sendkey", "s", do_send_key,
1088 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1089 #ifndef CONFIG_DM
1090 { "system_reset", "", do_system_reset,
1091 "", "reset the system" },
1092 { "system_powerdown", "", do_system_powerdown,
1093 "", "send system power down event" },
1094 #endif /* !CONFIG_DM */
1095 { "sum", "ii", do_sum,
1096 "addr size", "compute the checksum of a memory region" },
1097 { "usb_add", "s", do_usb_add,
1098 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1099 { "usb_del", "s", do_usb_del,
1100 "device", "remove USB device 'bus.addr'" },
1101 #ifndef CONFIG_DM
1102 { "cpu", "i", do_cpu_set,
1103 "index", "set the default CPU" },
1104 #endif /* !CONFIG_DM */
1105 { NULL, NULL, },
1106 };
1108 static term_cmd_t info_cmds[] = {
1109 { "version", "", do_info_version,
1110 "", "show the version of qemu" },
1111 { "network", "", do_info_network,
1112 "", "show the network state" },
1113 { "block", "", do_info_block,
1114 "", "show the block devices" },
1115 #ifndef CONFIG_DM
1116 { "registers", "", do_info_registers,
1117 "", "show the cpu registers" },
1118 { "cpus", "", do_info_cpus,
1119 "", "show infos for each CPU" },
1120 #endif /* !CONFIG_DM */
1121 { "history", "", do_info_history,
1122 "", "show the command line history", },
1123 { "irq", "", irq_info,
1124 "", "show the interrupts statistics (if available)", },
1125 { "pic", "", pic_info,
1126 "", "show i8259 (PIC) state", },
1127 { "pci", "", pci_info,
1128 "", "show PCI info", },
1129 #ifndef CONFIG_DM
1130 #if defined(TARGET_I386)
1131 { "tlb", "", tlb_info,
1132 "", "show virtual to physical memory mappings", },
1133 { "mem", "", mem_info,
1134 "", "show the active virtual memory mappings", },
1135 #endif
1136 { "jit", "", do_info_jit,
1137 "", "show dynamic compiler info", },
1138 #endif /* !CONFIG_DM */
1139 { "kqemu", "", do_info_kqemu,
1140 "", "show kqemu information", },
1141 { "usb", "", usb_info,
1142 "", "show guest USB devices", },
1143 { "usbhost", "", usb_host_info,
1144 "", "show host USB devices", },
1145 { "profile", "", do_info_profile,
1146 "", "show profiling information", },
1147 #ifdef CONFIG_DM
1148 { "hvmiopage", "", sp_info,
1149 "", "show HVM device model shared page info", },
1150 #endif /* CONFIG_DM */
1151 { NULL, NULL, },
1152 };
1154 /*******************************************************************/
1156 #ifndef CONFIG_DM
1157 static const char *pch;
1158 static jmp_buf expr_env;
1160 #define MD_TLONG 0
1161 #define MD_I32 1
1163 typedef struct MonitorDef {
1164 const char *name;
1165 int offset;
1166 target_long (*get_value)(struct MonitorDef *md, int val);
1167 int type;
1168 } MonitorDef;
1170 #if defined(TARGET_I386)
1171 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1173 CPUState *env = mon_get_cpu();
1174 if (!env)
1175 return 0;
1176 return env->eip + env->segs[R_CS].base;
1178 #endif
1180 #if defined(TARGET_PPC)
1181 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1183 CPUState *env = mon_get_cpu();
1184 unsigned int u;
1185 int i;
1187 if (!env)
1188 return 0;
1190 u = 0;
1191 for (i = 0; i < 8; i++)
1192 u |= env->crf[i] << (32 - (4 * i));
1194 return u;
1197 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1199 CPUState *env = mon_get_cpu();
1200 if (!env)
1201 return 0;
1202 return (env->msr[MSR_POW] << MSR_POW) |
1203 (env->msr[MSR_ILE] << MSR_ILE) |
1204 (env->msr[MSR_EE] << MSR_EE) |
1205 (env->msr[MSR_PR] << MSR_PR) |
1206 (env->msr[MSR_FP] << MSR_FP) |
1207 (env->msr[MSR_ME] << MSR_ME) |
1208 (env->msr[MSR_FE0] << MSR_FE0) |
1209 (env->msr[MSR_SE] << MSR_SE) |
1210 (env->msr[MSR_BE] << MSR_BE) |
1211 (env->msr[MSR_FE1] << MSR_FE1) |
1212 (env->msr[MSR_IP] << MSR_IP) |
1213 (env->msr[MSR_IR] << MSR_IR) |
1214 (env->msr[MSR_DR] << MSR_DR) |
1215 (env->msr[MSR_RI] << MSR_RI) |
1216 (env->msr[MSR_LE] << MSR_LE);
1219 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1221 CPUState *env = mon_get_cpu();
1222 if (!env)
1223 return 0;
1224 return (env->xer[XER_SO] << XER_SO) |
1225 (env->xer[XER_OV] << XER_OV) |
1226 (env->xer[XER_CA] << XER_CA) |
1227 (env->xer[XER_BC] << XER_BC);
1230 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1232 CPUState *env = mon_get_cpu();
1233 if (!env)
1234 return 0;
1235 return cpu_ppc_load_decr(env);
1238 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1240 CPUState *env = mon_get_cpu();
1241 if (!env)
1242 return 0;
1243 return cpu_ppc_load_tbu(env);
1246 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1248 CPUState *env = mon_get_cpu();
1249 if (!env)
1250 return 0;
1251 return cpu_ppc_load_tbl(env);
1253 #endif
1255 #if defined(TARGET_SPARC)
1256 #ifndef TARGET_SPARC64
1257 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1259 CPUState *env = mon_get_cpu();
1260 if (!env)
1261 return 0;
1262 return GET_PSR(env);
1264 #endif
1266 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1268 CPUState *env = mon_get_cpu();
1269 if (!env)
1270 return 0;
1271 return env->regwptr[val];
1273 #endif
1275 static MonitorDef monitor_defs[] = {
1276 #ifdef TARGET_I386
1278 #define SEG(name, seg) \
1279 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1280 { name ".base", offsetof(CPUState, segs[seg].base) },\
1281 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1283 { "eax", offsetof(CPUState, regs[0]) },
1284 { "ecx", offsetof(CPUState, regs[1]) },
1285 { "edx", offsetof(CPUState, regs[2]) },
1286 { "ebx", offsetof(CPUState, regs[3]) },
1287 { "esp|sp", offsetof(CPUState, regs[4]) },
1288 { "ebp|fp", offsetof(CPUState, regs[5]) },
1289 { "esi", offsetof(CPUState, regs[6]) },
1290 { "edi", offsetof(CPUState, regs[7]) },
1291 #ifdef TARGET_X86_64
1292 { "r8", offsetof(CPUState, regs[8]) },
1293 { "r9", offsetof(CPUState, regs[9]) },
1294 { "r10", offsetof(CPUState, regs[10]) },
1295 { "r11", offsetof(CPUState, regs[11]) },
1296 { "r12", offsetof(CPUState, regs[12]) },
1297 { "r13", offsetof(CPUState, regs[13]) },
1298 { "r14", offsetof(CPUState, regs[14]) },
1299 { "r15", offsetof(CPUState, regs[15]) },
1300 #endif
1301 { "eflags", offsetof(CPUState, eflags) },
1302 { "eip", offsetof(CPUState, eip) },
1303 SEG("cs", R_CS)
1304 SEG("ds", R_DS)
1305 SEG("es", R_ES)
1306 SEG("ss", R_SS)
1307 SEG("fs", R_FS)
1308 SEG("gs", R_GS)
1309 { "pc", 0, monitor_get_pc, },
1310 #elif defined(TARGET_PPC)
1311 { "r0", offsetof(CPUState, gpr[0]) },
1312 { "r1", offsetof(CPUState, gpr[1]) },
1313 { "r2", offsetof(CPUState, gpr[2]) },
1314 { "r3", offsetof(CPUState, gpr[3]) },
1315 { "r4", offsetof(CPUState, gpr[4]) },
1316 { "r5", offsetof(CPUState, gpr[5]) },
1317 { "r6", offsetof(CPUState, gpr[6]) },
1318 { "r7", offsetof(CPUState, gpr[7]) },
1319 { "r8", offsetof(CPUState, gpr[8]) },
1320 { "r9", offsetof(CPUState, gpr[9]) },
1321 { "r10", offsetof(CPUState, gpr[10]) },
1322 { "r11", offsetof(CPUState, gpr[11]) },
1323 { "r12", offsetof(CPUState, gpr[12]) },
1324 { "r13", offsetof(CPUState, gpr[13]) },
1325 { "r14", offsetof(CPUState, gpr[14]) },
1326 { "r15", offsetof(CPUState, gpr[15]) },
1327 { "r16", offsetof(CPUState, gpr[16]) },
1328 { "r17", offsetof(CPUState, gpr[17]) },
1329 { "r18", offsetof(CPUState, gpr[18]) },
1330 { "r19", offsetof(CPUState, gpr[19]) },
1331 { "r20", offsetof(CPUState, gpr[20]) },
1332 { "r21", offsetof(CPUState, gpr[21]) },
1333 { "r22", offsetof(CPUState, gpr[22]) },
1334 { "r23", offsetof(CPUState, gpr[23]) },
1335 { "r24", offsetof(CPUState, gpr[24]) },
1336 { "r25", offsetof(CPUState, gpr[25]) },
1337 { "r26", offsetof(CPUState, gpr[26]) },
1338 { "r27", offsetof(CPUState, gpr[27]) },
1339 { "r28", offsetof(CPUState, gpr[28]) },
1340 { "r29", offsetof(CPUState, gpr[29]) },
1341 { "r30", offsetof(CPUState, gpr[30]) },
1342 { "r31", offsetof(CPUState, gpr[31]) },
1343 { "nip|pc", offsetof(CPUState, nip) },
1344 { "lr", offsetof(CPUState, lr) },
1345 { "ctr", offsetof(CPUState, ctr) },
1346 { "decr", 0, &monitor_get_decr, },
1347 { "ccr", 0, &monitor_get_ccr, },
1348 { "msr", 0, &monitor_get_msr, },
1349 { "xer", 0, &monitor_get_xer, },
1350 { "tbu", 0, &monitor_get_tbu, },
1351 { "tbl", 0, &monitor_get_tbl, },
1352 { "sdr1", offsetof(CPUState, sdr1) },
1353 { "sr0", offsetof(CPUState, sr[0]) },
1354 { "sr1", offsetof(CPUState, sr[1]) },
1355 { "sr2", offsetof(CPUState, sr[2]) },
1356 { "sr3", offsetof(CPUState, sr[3]) },
1357 { "sr4", offsetof(CPUState, sr[4]) },
1358 { "sr5", offsetof(CPUState, sr[5]) },
1359 { "sr6", offsetof(CPUState, sr[6]) },
1360 { "sr7", offsetof(CPUState, sr[7]) },
1361 { "sr8", offsetof(CPUState, sr[8]) },
1362 { "sr9", offsetof(CPUState, sr[9]) },
1363 { "sr10", offsetof(CPUState, sr[10]) },
1364 { "sr11", offsetof(CPUState, sr[11]) },
1365 { "sr12", offsetof(CPUState, sr[12]) },
1366 { "sr13", offsetof(CPUState, sr[13]) },
1367 { "sr14", offsetof(CPUState, sr[14]) },
1368 { "sr15", offsetof(CPUState, sr[15]) },
1369 /* Too lazy to put BATs and SPRs ... */
1370 #elif defined(TARGET_SPARC)
1371 { "g0", offsetof(CPUState, gregs[0]) },
1372 { "g1", offsetof(CPUState, gregs[1]) },
1373 { "g2", offsetof(CPUState, gregs[2]) },
1374 { "g3", offsetof(CPUState, gregs[3]) },
1375 { "g4", offsetof(CPUState, gregs[4]) },
1376 { "g5", offsetof(CPUState, gregs[5]) },
1377 { "g6", offsetof(CPUState, gregs[6]) },
1378 { "g7", offsetof(CPUState, gregs[7]) },
1379 { "o0", 0, monitor_get_reg },
1380 { "o1", 1, monitor_get_reg },
1381 { "o2", 2, monitor_get_reg },
1382 { "o3", 3, monitor_get_reg },
1383 { "o4", 4, monitor_get_reg },
1384 { "o5", 5, monitor_get_reg },
1385 { "o6", 6, monitor_get_reg },
1386 { "o7", 7, monitor_get_reg },
1387 { "l0", 8, monitor_get_reg },
1388 { "l1", 9, monitor_get_reg },
1389 { "l2", 10, monitor_get_reg },
1390 { "l3", 11, monitor_get_reg },
1391 { "l4", 12, monitor_get_reg },
1392 { "l5", 13, monitor_get_reg },
1393 { "l6", 14, monitor_get_reg },
1394 { "l7", 15, monitor_get_reg },
1395 { "i0", 16, monitor_get_reg },
1396 { "i1", 17, monitor_get_reg },
1397 { "i2", 18, monitor_get_reg },
1398 { "i3", 19, monitor_get_reg },
1399 { "i4", 20, monitor_get_reg },
1400 { "i5", 21, monitor_get_reg },
1401 { "i6", 22, monitor_get_reg },
1402 { "i7", 23, monitor_get_reg },
1403 { "pc", offsetof(CPUState, pc) },
1404 { "npc", offsetof(CPUState, npc) },
1405 { "y", offsetof(CPUState, y) },
1406 #ifndef TARGET_SPARC64
1407 { "psr", 0, &monitor_get_psr, },
1408 { "wim", offsetof(CPUState, wim) },
1409 #endif
1410 { "tbr", offsetof(CPUState, tbr) },
1411 { "fsr", offsetof(CPUState, fsr) },
1412 { "f0", offsetof(CPUState, fpr[0]) },
1413 { "f1", offsetof(CPUState, fpr[1]) },
1414 { "f2", offsetof(CPUState, fpr[2]) },
1415 { "f3", offsetof(CPUState, fpr[3]) },
1416 { "f4", offsetof(CPUState, fpr[4]) },
1417 { "f5", offsetof(CPUState, fpr[5]) },
1418 { "f6", offsetof(CPUState, fpr[6]) },
1419 { "f7", offsetof(CPUState, fpr[7]) },
1420 { "f8", offsetof(CPUState, fpr[8]) },
1421 { "f9", offsetof(CPUState, fpr[9]) },
1422 { "f10", offsetof(CPUState, fpr[10]) },
1423 { "f11", offsetof(CPUState, fpr[11]) },
1424 { "f12", offsetof(CPUState, fpr[12]) },
1425 { "f13", offsetof(CPUState, fpr[13]) },
1426 { "f14", offsetof(CPUState, fpr[14]) },
1427 { "f15", offsetof(CPUState, fpr[15]) },
1428 { "f16", offsetof(CPUState, fpr[16]) },
1429 { "f17", offsetof(CPUState, fpr[17]) },
1430 { "f18", offsetof(CPUState, fpr[18]) },
1431 { "f19", offsetof(CPUState, fpr[19]) },
1432 { "f20", offsetof(CPUState, fpr[20]) },
1433 { "f21", offsetof(CPUState, fpr[21]) },
1434 { "f22", offsetof(CPUState, fpr[22]) },
1435 { "f23", offsetof(CPUState, fpr[23]) },
1436 { "f24", offsetof(CPUState, fpr[24]) },
1437 { "f25", offsetof(CPUState, fpr[25]) },
1438 { "f26", offsetof(CPUState, fpr[26]) },
1439 { "f27", offsetof(CPUState, fpr[27]) },
1440 { "f28", offsetof(CPUState, fpr[28]) },
1441 { "f29", offsetof(CPUState, fpr[29]) },
1442 { "f30", offsetof(CPUState, fpr[30]) },
1443 { "f31", offsetof(CPUState, fpr[31]) },
1444 #ifdef TARGET_SPARC64
1445 { "f32", offsetof(CPUState, fpr[32]) },
1446 { "f34", offsetof(CPUState, fpr[34]) },
1447 { "f36", offsetof(CPUState, fpr[36]) },
1448 { "f38", offsetof(CPUState, fpr[38]) },
1449 { "f40", offsetof(CPUState, fpr[40]) },
1450 { "f42", offsetof(CPUState, fpr[42]) },
1451 { "f44", offsetof(CPUState, fpr[44]) },
1452 { "f46", offsetof(CPUState, fpr[46]) },
1453 { "f48", offsetof(CPUState, fpr[48]) },
1454 { "f50", offsetof(CPUState, fpr[50]) },
1455 { "f52", offsetof(CPUState, fpr[52]) },
1456 { "f54", offsetof(CPUState, fpr[54]) },
1457 { "f56", offsetof(CPUState, fpr[56]) },
1458 { "f58", offsetof(CPUState, fpr[58]) },
1459 { "f60", offsetof(CPUState, fpr[60]) },
1460 { "f62", offsetof(CPUState, fpr[62]) },
1461 { "asi", offsetof(CPUState, asi) },
1462 { "pstate", offsetof(CPUState, pstate) },
1463 { "cansave", offsetof(CPUState, cansave) },
1464 { "canrestore", offsetof(CPUState, canrestore) },
1465 { "otherwin", offsetof(CPUState, otherwin) },
1466 { "wstate", offsetof(CPUState, wstate) },
1467 { "cleanwin", offsetof(CPUState, cleanwin) },
1468 { "fprs", offsetof(CPUState, fprs) },
1469 #endif
1470 #endif
1471 { NULL },
1472 };
1474 static void expr_error(const char *fmt)
1476 term_printf(fmt);
1477 term_printf("\n");
1478 longjmp(expr_env, 1);
1481 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1482 static int get_monitor_def(target_long *pval, const char *name)
1484 MonitorDef *md;
1485 void *ptr;
1487 for(md = monitor_defs; md->name != NULL; md++) {
1488 if (compare_cmd(name, md->name)) {
1489 if (md->get_value) {
1490 *pval = md->get_value(md, md->offset);
1491 } else {
1492 CPUState *env = mon_get_cpu();
1493 if (!env)
1494 return -2;
1495 ptr = (uint8_t *)env + md->offset;
1496 switch(md->type) {
1497 case MD_I32:
1498 *pval = *(int32_t *)ptr;
1499 break;
1500 case MD_TLONG:
1501 *pval = *(target_long *)ptr;
1502 break;
1503 default:
1504 *pval = 0;
1505 break;
1508 return 0;
1511 return -1;
1514 static void next(void)
1516 if (pch != '\0') {
1517 pch++;
1518 while (isspace(*pch))
1519 pch++;
1523 static target_long expr_sum(void);
1525 static target_long expr_unary(void)
1527 target_long n;
1528 char *p;
1529 int ret;
1531 switch(*pch) {
1532 case '+':
1533 next();
1534 n = expr_unary();
1535 break;
1536 case '-':
1537 next();
1538 n = -expr_unary();
1539 break;
1540 case '~':
1541 next();
1542 n = ~expr_unary();
1543 break;
1544 case '(':
1545 next();
1546 n = expr_sum();
1547 if (*pch != ')') {
1548 expr_error("')' expected");
1550 next();
1551 break;
1552 case '\'':
1553 pch++;
1554 if (*pch == '\0')
1555 expr_error("character constant expected");
1556 n = *pch;
1557 pch++;
1558 if (*pch != '\'')
1559 expr_error("missing terminating \' character");
1560 next();
1561 break;
1562 case '$':
1564 char buf[128], *q;
1566 pch++;
1567 q = buf;
1568 while ((*pch >= 'a' && *pch <= 'z') ||
1569 (*pch >= 'A' && *pch <= 'Z') ||
1570 (*pch >= '0' && *pch <= '9') ||
1571 *pch == '_' || *pch == '.') {
1572 if ((q - buf) < sizeof(buf) - 1)
1573 *q++ = *pch;
1574 pch++;
1576 while (isspace(*pch))
1577 pch++;
1578 *q = 0;
1579 ret = get_monitor_def(&n, buf);
1580 if (ret == -1)
1581 expr_error("unknown register");
1582 else if (ret == -2)
1583 expr_error("no cpu defined");
1585 break;
1586 case '\0':
1587 expr_error("unexpected end of expression");
1588 n = 0;
1589 break;
1590 default:
1591 /* XXX: 64 bit version */
1592 n = strtoul(pch, &p, 0);
1593 if (pch == p) {
1594 expr_error("invalid char in expression");
1596 pch = p;
1597 while (isspace(*pch))
1598 pch++;
1599 break;
1601 return n;
1605 static target_long expr_prod(void)
1607 target_long val, val2;
1608 int op;
1610 val = expr_unary();
1611 for(;;) {
1612 op = *pch;
1613 if (op != '*' && op != '/' && op != '%')
1614 break;
1615 next();
1616 val2 = expr_unary();
1617 switch(op) {
1618 default:
1619 case '*':
1620 val *= val2;
1621 break;
1622 case '/':
1623 case '%':
1624 if (val2 == 0)
1625 expr_error("division by zero");
1626 if (op == '/')
1627 val /= val2;
1628 else
1629 val %= val2;
1630 break;
1633 return val;
1636 static target_long expr_logic(void)
1638 target_long val, val2;
1639 int op;
1641 val = expr_prod();
1642 for(;;) {
1643 op = *pch;
1644 if (op != '&' && op != '|' && op != '^')
1645 break;
1646 next();
1647 val2 = expr_prod();
1648 switch(op) {
1649 default:
1650 case '&':
1651 val &= val2;
1652 break;
1653 case '|':
1654 val |= val2;
1655 break;
1656 case '^':
1657 val ^= val2;
1658 break;
1661 return val;
1664 static target_long expr_sum(void)
1666 target_long val, val2;
1667 int op;
1669 val = expr_logic();
1670 for(;;) {
1671 op = *pch;
1672 if (op != '+' && op != '-')
1673 break;
1674 next();
1675 val2 = expr_logic();
1676 if (op == '+')
1677 val += val2;
1678 else
1679 val -= val2;
1681 return val;
1684 static int get_expr(target_long *pval, const char **pp)
1686 pch = *pp;
1687 if (setjmp(expr_env)) {
1688 *pp = pch;
1689 return -1;
1691 while (isspace(*pch))
1692 pch++;
1693 *pval = expr_sum();
1694 *pp = pch;
1695 return 0;
1697 #endif /* !CONFIG_DM */
1699 static int get_str(char *buf, int buf_size, const char **pp)
1701 const char *p;
1702 char *q;
1703 int c;
1705 q = buf;
1706 p = *pp;
1707 while (isspace(*p))
1708 p++;
1709 if (*p == '\0') {
1710 fail:
1711 *q = '\0';
1712 *pp = p;
1713 return -1;
1715 if (*p == '\"') {
1716 p++;
1717 while (*p != '\0' && *p != '\"') {
1718 if (*p == '\\') {
1719 p++;
1720 c = *p++;
1721 switch(c) {
1722 case 'n':
1723 c = '\n';
1724 break;
1725 case 'r':
1726 c = '\r';
1727 break;
1728 case '\\':
1729 case '\'':
1730 case '\"':
1731 break;
1732 default:
1733 qemu_printf("unsupported escape code: '\\%c'\n", c);
1734 goto fail;
1736 if ((q - buf) < buf_size - 1) {
1737 *q++ = c;
1739 } else {
1740 if ((q - buf) < buf_size - 1) {
1741 *q++ = *p;
1743 p++;
1746 if (*p != '\"') {
1747 qemu_printf("unterminated string\n");
1748 goto fail;
1750 p++;
1751 } else {
1752 while (*p != '\0' && !isspace(*p)) {
1753 if ((q - buf) < buf_size - 1) {
1754 *q++ = *p;
1756 p++;
1759 *q = '\0';
1760 *pp = p;
1761 return 0;
1764 #ifndef CONFIG_DM
1765 static int default_fmt_format = 'x';
1766 static int default_fmt_size = 4;
1767 #endif /* !CONFIG_DM */
1769 #define MAX_ARGS 16
1771 static void monitor_handle_command(const char *cmdline)
1773 const char *p, *pstart, *typestr;
1774 char *q;
1775 int c, nb_args, len, i;
1776 #ifndef CONFIG_DM
1777 int has_arg;
1778 #endif /* !CONFIG_DM */
1779 term_cmd_t *cmd;
1780 char cmdname[256];
1781 char buf[1024];
1782 void *str_allocated[MAX_ARGS];
1783 void *args[MAX_ARGS];
1785 #ifdef DEBUG
1786 term_printf("command='%s'\n", cmdline);
1787 #endif
1789 /* extract the command name */
1790 p = cmdline;
1791 q = cmdname;
1792 while (isspace(*p))
1793 p++;
1794 if (*p == '\0')
1795 return;
1796 pstart = p;
1797 while (*p != '\0' && *p != '/' && !isspace(*p))
1798 p++;
1799 len = p - pstart;
1800 if (len > sizeof(cmdname) - 1)
1801 len = sizeof(cmdname) - 1;
1802 memcpy(cmdname, pstart, len);
1803 cmdname[len] = '\0';
1805 /* find the command */
1806 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1807 if (compare_cmd(cmdname, cmd->name))
1808 goto found;
1810 term_printf("unknown command: '%s'\n", cmdname);
1811 return;
1812 found:
1814 for(i = 0; i < MAX_ARGS; i++)
1815 str_allocated[i] = NULL;
1817 /* parse the parameters */
1818 typestr = cmd->args_type;
1819 nb_args = 0;
1820 for(;;) {
1821 c = *typestr;
1822 if (c == '\0')
1823 break;
1824 typestr++;
1825 switch(c) {
1826 case 'F':
1827 case 'B':
1828 case 's':
1830 int ret;
1831 char *str;
1833 while (isspace(*p))
1834 p++;
1835 if (*typestr == '?') {
1836 typestr++;
1837 if (*p == '\0') {
1838 /* no optional string: NULL argument */
1839 str = NULL;
1840 goto add_str;
1843 ret = get_str(buf, sizeof(buf), &p);
1844 if (ret < 0) {
1845 switch(c) {
1846 case 'F':
1847 term_printf("%s: filename expected\n", cmdname);
1848 break;
1849 case 'B':
1850 term_printf("%s: block device name expected\n", cmdname);
1851 break;
1852 default:
1853 term_printf("%s: string expected\n", cmdname);
1854 break;
1856 goto fail;
1858 str = qemu_malloc(strlen(buf) + 1);
1859 strcpy(str, buf);
1860 str_allocated[nb_args] = str;
1861 add_str:
1862 if (nb_args >= MAX_ARGS) {
1863 error_args:
1864 term_printf("%s: too many arguments\n", cmdname);
1865 goto fail;
1867 args[nb_args++] = str;
1869 break;
1870 #ifndef CONFIG_DM
1871 case '/':
1873 int count, format, size;
1875 while (isspace(*p))
1876 p++;
1877 if (*p == '/') {
1878 /* format found */
1879 p++;
1880 count = 1;
1881 if (isdigit(*p)) {
1882 count = 0;
1883 while (isdigit(*p)) {
1884 count = count * 10 + (*p - '0');
1885 p++;
1888 size = -1;
1889 format = -1;
1890 for(;;) {
1891 switch(*p) {
1892 case 'o':
1893 case 'd':
1894 case 'u':
1895 case 'x':
1896 case 'i':
1897 case 'c':
1898 format = *p++;
1899 break;
1900 case 'b':
1901 size = 1;
1902 p++;
1903 break;
1904 case 'h':
1905 size = 2;
1906 p++;
1907 break;
1908 case 'w':
1909 size = 4;
1910 p++;
1911 break;
1912 case 'g':
1913 case 'L':
1914 size = 8;
1915 p++;
1916 break;
1917 default:
1918 goto next;
1921 next:
1922 if (*p != '\0' && !isspace(*p)) {
1923 term_printf("invalid char in format: '%c'\n", *p);
1924 goto fail;
1926 if (format < 0)
1927 format = default_fmt_format;
1928 if (format != 'i') {
1929 /* for 'i', not specifying a size gives -1 as size */
1930 if (size < 0)
1931 size = default_fmt_size;
1933 default_fmt_size = size;
1934 default_fmt_format = format;
1935 } else {
1936 count = 1;
1937 format = default_fmt_format;
1938 if (format != 'i') {
1939 size = default_fmt_size;
1940 } else {
1941 size = -1;
1944 if (nb_args + 3 > MAX_ARGS)
1945 goto error_args;
1946 args[nb_args++] = (void*)count;
1947 args[nb_args++] = (void*)format;
1948 args[nb_args++] = (void*)size;
1950 break;
1951 case 'i':
1952 case 'l':
1954 target_long val;
1955 while (isspace(*p))
1956 p++;
1957 if (*typestr == '?' || *typestr == '.') {
1958 typestr++;
1959 if (*typestr == '?') {
1960 if (*p == '\0')
1961 has_arg = 0;
1962 else
1963 has_arg = 1;
1964 } else {
1965 if (*p == '.') {
1966 p++;
1967 while (isspace(*p))
1968 p++;
1969 has_arg = 1;
1970 } else {
1971 has_arg = 0;
1974 if (nb_args >= MAX_ARGS)
1975 goto error_args;
1976 args[nb_args++] = (void *)has_arg;
1977 if (!has_arg) {
1978 if (nb_args >= MAX_ARGS)
1979 goto error_args;
1980 val = -1;
1981 goto add_num;
1984 if (get_expr(&val, &p))
1985 goto fail;
1986 add_num:
1987 if (c == 'i') {
1988 if (nb_args >= MAX_ARGS)
1989 goto error_args;
1990 args[nb_args++] = (void *)(int)val;
1991 } else {
1992 if ((nb_args + 1) >= MAX_ARGS)
1993 goto error_args;
1994 #if TARGET_LONG_BITS == 64
1995 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1996 #else
1997 args[nb_args++] = (void *)0;
1998 #endif
1999 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2002 break;
2003 #endif /* !CONFIG_DM */
2004 case '-':
2006 int has_option;
2007 /* option */
2009 c = *typestr++;
2010 if (c == '\0')
2011 goto bad_type;
2012 while (isspace(*p))
2013 p++;
2014 has_option = 0;
2015 if (*p == '-') {
2016 p++;
2017 if (*p != c) {
2018 term_printf("%s: unsupported option -%c\n",
2019 cmdname, *p);
2020 goto fail;
2022 p++;
2023 has_option = 1;
2025 if (nb_args >= MAX_ARGS)
2026 goto error_args;
2027 args[nb_args++] = (void *)has_option;
2029 break;
2030 #ifdef CONFIG_DM
2031 /* TODO: add more commands we need here to support hvm device model */
2032 case '/':
2033 case 'i':
2034 #endif /* CONFIG_DM */
2035 default:
2036 bad_type:
2037 term_printf("%s: unknown type '%c'\n", cmdname, c);
2038 goto fail;
2041 /* check that all arguments were parsed */
2042 while (isspace(*p))
2043 p++;
2044 if (*p != '\0') {
2045 term_printf("%s: extraneous characters at the end of line\n",
2046 cmdname);
2047 goto fail;
2050 switch(nb_args) {
2051 case 0:
2052 cmd->handler();
2053 break;
2054 case 1:
2055 cmd->handler(args[0]);
2056 break;
2057 case 2:
2058 cmd->handler(args[0], args[1]);
2059 break;
2060 case 3:
2061 cmd->handler(args[0], args[1], args[2]);
2062 break;
2063 case 4:
2064 cmd->handler(args[0], args[1], args[2], args[3]);
2065 break;
2066 case 5:
2067 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2068 break;
2069 case 6:
2070 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2071 break;
2072 default:
2073 term_printf("unsupported number of arguments: %d\n", nb_args);
2074 goto fail;
2076 fail:
2077 for(i = 0; i < MAX_ARGS; i++)
2078 qemu_free(str_allocated[i]);
2079 return;
2082 #ifndef CONFIG_DM
2083 static void cmd_completion(const char *name, const char *list)
2085 const char *p, *pstart;
2086 char cmd[128];
2087 int len;
2089 p = list;
2090 for(;;) {
2091 pstart = p;
2092 p = strchr(p, '|');
2093 if (!p)
2094 p = pstart + strlen(pstart);
2095 len = p - pstart;
2096 if (len > sizeof(cmd) - 2)
2097 len = sizeof(cmd) - 2;
2098 memcpy(cmd, pstart, len);
2099 cmd[len] = '\0';
2100 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2101 add_completion(cmd);
2103 if (*p == '\0')
2104 break;
2105 p++;
2109 static void file_completion(const char *input)
2111 DIR *ffs;
2112 struct dirent *d;
2113 char path[1024];
2114 char file[1024], file_prefix[1024];
2115 int input_path_len;
2116 const char *p;
2118 p = strrchr(input, '/');
2119 if (!p) {
2120 input_path_len = 0;
2121 pstrcpy(file_prefix, sizeof(file_prefix), input);
2122 strcpy(path, ".");
2123 } else {
2124 input_path_len = p - input + 1;
2125 memcpy(path, input, input_path_len);
2126 if (input_path_len > sizeof(path) - 1)
2127 input_path_len = sizeof(path) - 1;
2128 path[input_path_len] = '\0';
2129 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2131 #ifdef DEBUG_COMPLETION
2132 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2133 #endif
2134 ffs = opendir(path);
2135 if (!ffs)
2136 return;
2137 for(;;) {
2138 struct stat sb;
2139 d = readdir(ffs);
2140 if (!d)
2141 break;
2142 if (strstart(d->d_name, file_prefix, NULL)) {
2143 memcpy(file, input, input_path_len);
2144 strcpy(file + input_path_len, d->d_name);
2145 /* stat the file to find out if it's a directory.
2146 * In that case add a slash to speed up typing long paths
2147 */
2148 stat(file, &sb);
2149 if(S_ISDIR(sb.st_mode))
2150 strcat(file, "/");
2151 add_completion(file);
2154 closedir(ffs);
2157 static void block_completion_it(void *opaque, const char *name)
2159 const char *input = opaque;
2161 if (input[0] == '\0' ||
2162 !strncmp(name, (char *)input, strlen(input))) {
2163 add_completion(name);
2167 /* NOTE: this parser is an approximate form of the real command parser */
2168 static void parse_cmdline(const char *cmdline,
2169 int *pnb_args, char **args)
2171 const char *p;
2172 int nb_args, ret;
2173 char buf[1024];
2175 p = cmdline;
2176 nb_args = 0;
2177 for(;;) {
2178 while (isspace(*p))
2179 p++;
2180 if (*p == '\0')
2181 break;
2182 if (nb_args >= MAX_ARGS)
2183 break;
2184 ret = get_str(buf, sizeof(buf), &p);
2185 args[nb_args] = qemu_strdup(buf);
2186 nb_args++;
2187 if (ret < 0)
2188 break;
2190 *pnb_args = nb_args;
2193 void readline_find_completion(const char *cmdline)
2195 const char *cmdname;
2196 char *args[MAX_ARGS];
2197 int nb_args, i, len;
2198 const char *ptype, *str;
2199 term_cmd_t *cmd;
2201 parse_cmdline(cmdline, &nb_args, args);
2202 #ifdef DEBUG_COMPLETION
2203 for(i = 0; i < nb_args; i++) {
2204 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2206 #endif
2208 /* if the line ends with a space, it means we want to complete the
2209 next arg */
2210 len = strlen(cmdline);
2211 if (len > 0 && isspace(cmdline[len - 1])) {
2212 if (nb_args >= MAX_ARGS)
2213 return;
2214 args[nb_args++] = qemu_strdup("");
2216 if (nb_args <= 1) {
2217 /* command completion */
2218 if (nb_args == 0)
2219 cmdname = "";
2220 else
2221 cmdname = args[0];
2222 completion_index = strlen(cmdname);
2223 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2224 cmd_completion(cmdname, cmd->name);
2226 } else {
2227 /* find the command */
2228 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2229 if (compare_cmd(args[0], cmd->name))
2230 goto found;
2232 return;
2233 found:
2234 ptype = cmd->args_type;
2235 for(i = 0; i < nb_args - 2; i++) {
2236 if (*ptype != '\0') {
2237 ptype++;
2238 while (*ptype == '?')
2239 ptype++;
2242 str = args[nb_args - 1];
2243 switch(*ptype) {
2244 case 'F':
2245 /* file completion */
2246 completion_index = strlen(str);
2247 file_completion(str);
2248 break;
2249 case 'B':
2250 /* block device name completion */
2251 completion_index = strlen(str);
2252 bdrv_iterate(block_completion_it, (void *)str);
2253 break;
2254 case 's':
2255 /* XXX: more generic ? */
2256 if (!strcmp(cmd->name, "info")) {
2257 completion_index = strlen(str);
2258 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2259 cmd_completion(str, cmd->name);
2262 break;
2263 default:
2264 break;
2267 for(i = 0; i < nb_args; i++)
2268 qemu_free(args[i]);
2270 #else
2271 void readline_find_completion(const char *cmdline)
2274 #endif /* !CONFIG_DM */
2276 static int term_can_read(void *opaque)
2278 return 128;
2281 static void term_read(void *opaque, const uint8_t *buf, int size)
2283 int i;
2284 for(i = 0; i < size; i++)
2285 readline_handle_byte(buf[i]);
2288 static void monitor_start_input(void);
2290 static void monitor_handle_command1(void *opaque, const char *cmdline)
2292 monitor_handle_command(cmdline);
2293 monitor_start_input();
2296 static void monitor_start_input(void)
2298 readline_start("(HVMXen) ", 0, monitor_handle_command1, NULL);
2301 void monitor_init(CharDriverState *hd, int show_banner)
2303 monitor_hd = hd;
2304 if (show_banner) {
2305 term_printf("HVM device model. type 'q' to exit\n");
2307 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2308 monitor_start_input();
2311 /* XXX: use threads ? */
2312 /* modal monitor readline */
2313 static int monitor_readline_started;
2314 static char *monitor_readline_buf;
2315 static int monitor_readline_buf_size;
2317 static void monitor_readline_cb(void *opaque, const char *input)
2319 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2320 monitor_readline_started = 0;
2323 void monitor_readline(const char *prompt, int is_password,
2324 char *buf, int buf_size)
2326 if (is_password) {
2327 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2329 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2330 monitor_readline_buf = buf;
2331 monitor_readline_buf_size = buf_size;
2332 monitor_readline_started = 1;
2333 while (monitor_readline_started) {
2334 main_loop_wait(10);