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