Statistics
| Branch: | Revision:

root / monitor.c @ a594cfbf

History | View | Annotate | Download (55.3 kB)

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
void term_flush(void)
68
{
69
    if (term_outbuf_index > 0) {
70
        qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
71
        term_outbuf_index = 0;
72
    }
73
}
74

    
75
/* flush at every end of line or if the buffer is full */
76
void term_puts(const char *str)
77
{
78
    int c;
79
    for(;;) {
80
        c = *str++;
81
        if (c == '\0')
82
            break;
83
        term_outbuf[term_outbuf_index++] = c;
84
        if (term_outbuf_index >= sizeof(term_outbuf) ||
85
            c == '\n')
86
            term_flush();
87
    }
88
}
89

    
90
void term_vprintf(const char *fmt, va_list ap)
91
{
92
    char buf[4096];
93
    vsnprintf(buf, sizeof(buf), fmt, ap);
94
    term_puts(buf);
95
}
96

    
97
void term_printf(const char *fmt, ...)
98
{
99
    va_list ap;
100
    va_start(ap, fmt);
101
    term_vprintf(fmt, ap);
102
    va_end(ap);
103
}
104

    
105
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
106
{
107
    va_list ap;
108
    va_start(ap, fmt);
109
    term_vprintf(fmt, ap);
110
    va_end(ap);
111
    return 0;
112
}
113

    
114
static int compare_cmd(const char *name, const char *list)
115
{
116
    const char *p, *pstart;
117
    int len;
118
    len = strlen(name);
119
    p = list;
120
    for(;;) {
121
        pstart = p;
122
        p = strchr(p, '|');
123
        if (!p)
124
            p = pstart + strlen(pstart);
125
        if ((p - pstart) == len && !memcmp(pstart, name, len))
126
            return 1;
127
        if (*p == '\0')
128
            break;
129
        p++;
130
    }
131
    return 0;
132
}
133

    
134
static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
135
{
136
    term_cmd_t *cmd;
137

    
138
    for(cmd = cmds; cmd->name != NULL; cmd++) {
139
        if (!name || !strcmp(name, cmd->name))
140
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
141
    }
142
}
143

    
144
static void help_cmd(const char *name)
145
{
146
    if (name && !strcmp(name, "info")) {
147
        help_cmd1(info_cmds, "info ", NULL);
148
    } else {
149
        help_cmd1(term_cmds, "", name);
150
        if (name && !strcmp(name, "log")) {
151
            CPULogItem *item;
152
            term_printf("Log items (comma separated):\n");
153
            term_printf("%-10s %s\n", "none", "remove all logs");
154
            for(item = cpu_log_items; item->mask != 0; item++) {
155
                term_printf("%-10s %s\n", item->name, item->help);
156
            }
157
        }
158
    }
159
}
160

    
161
static void do_help(const char *name)
162
{
163
    help_cmd(name);
164
}
165

    
166
static void do_commit(void)
167
{
168
    int i;
169

    
170
    for (i = 0; i < MAX_DISKS; i++) {
171
        if (bs_table[i]) {
172
            bdrv_commit(bs_table[i]);
173
        }
174
    }
175
}
176

    
177
static void do_info(const char *item)
178
{
179
    term_cmd_t *cmd;
180

    
181
    if (!item)
182
        goto help;
183
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
184
        if (compare_cmd(item, cmd->name)) 
185
            goto found;
186
    }
187
 help:
188
    help_cmd("info");
189
    return;
190
 found:
191
    cmd->handler();
192
}
193

    
194
static void do_info_version(void)
195
{
196
  term_printf("%s\n", QEMU_VERSION);
197
}
198

    
199
static void do_info_network(void)
200
{
201
    int i, j;
202
    NetDriverState *nd;
203
    
204
    for(i = 0; i < nb_nics; i++) {
205
        nd = &nd_table[i];
206
        term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
207
        for(j = 0; j < 6; j++) {
208
            if (j > 0)
209
                term_printf(":");
210
            term_printf("%02x", nd->macaddr[j]);
211
        }
212
        term_printf("\n");
213
    }
214
}
215
 
216
static void do_info_block(void)
217
{
218
    bdrv_info();
219
}
220

    
221
static void do_info_registers(void)
222
{
223
#ifdef TARGET_I386
224
    cpu_dump_state(cpu_single_env, NULL, monitor_fprintf,
225
                   X86_DUMP_FPU);
226
#else
227
    cpu_dump_state(cpu_single_env, NULL, monitor_fprintf, 
228
                   0);
229
#endif
230
}
231

    
232
static void do_info_jit(void)
233
{
234
    dump_exec_info(NULL, monitor_fprintf);
235
}
236

    
237
static void do_info_history (void)
238
{
239
    int i;
240
    const char *str;
241
    
242
    i = 0;
243
    for(;;) {
244
        str = readline_get_history(i);
245
        if (!str)
246
            break;
247
        term_printf("%d: '%s'\n", i, str);
248
        i++;
249
    }
250
}
251

    
252
static void do_quit(void)
253
{
254
#ifdef USE_KQEMU
255
    kqemu_record_dump();
256
#endif
257
    exit(0);
258
}
259

    
260
static int eject_device(BlockDriverState *bs, int force)
261
{
262
    if (bdrv_is_inserted(bs)) {
263
        if (!force) {
264
            if (!bdrv_is_removable(bs)) {
265
                term_printf("device is not removable\n");
266
                return -1;
267
            }
268
            if (bdrv_is_locked(bs)) {
269
                term_printf("device is locked\n");
270
                return -1;
271
            }
272
        }
273
        bdrv_close(bs);
274
    }
275
    return 0;
276
}
277

    
278
static void do_eject(int force, const char *filename)
279
{
280
    BlockDriverState *bs;
281

    
282
    bs = bdrv_find(filename);
283
    if (!bs) {
284
        term_printf("device not found\n");
285
        return;
286
    }
287
    eject_device(bs, force);
288
}
289

    
290
static void do_change(const char *device, const char *filename)
291
{
292
    BlockDriverState *bs;
293
    int i;
294
    char password[256];
295

    
296
    bs = bdrv_find(device);
297
    if (!bs) {
298
        term_printf("device not found\n");
299
        return;
300
    }
301
    if (eject_device(bs, 0) < 0)
302
        return;
303
    bdrv_open(bs, filename, 0);
304
    if (bdrv_is_encrypted(bs)) {
305
        term_printf("%s is encrypted.\n", device);
306
        for(i = 0; i < 3; i++) {
307
            monitor_readline("Password: ", 1, password, sizeof(password));
308
            if (bdrv_set_key(bs, password) == 0)
309
                break;
310
            term_printf("invalid password\n");
311
        }
312
    }
313
}
314

    
315
static void do_screen_dump(const char *filename)
316
{
317
    vga_screen_dump(filename);
318
}
319

    
320
static void do_log(const char *items)
321
{
322
    int mask;
323
    
324
    if (!strcmp(items, "none")) {
325
        mask = 0;
326
    } else {
327
        mask = cpu_str_to_log_mask(items);
328
        if (!mask) {
329
            help_cmd("log");
330
            return;
331
        }
332
    }
333
    cpu_set_log(mask);
334
}
335

    
336
static void do_savevm(const char *filename)
337
{
338
    if (qemu_savevm(filename) < 0)
339
        term_printf("I/O error when saving VM to '%s'\n", filename);
340
}
341

    
342
static void do_loadvm(const char *filename)
343
{
344
    if (qemu_loadvm(filename) < 0) 
345
        term_printf("I/O error when loading VM from '%s'\n", filename);
346
}
347

    
348
static void do_stop(void)
349
{
350
    vm_stop(EXCP_INTERRUPT);
351
}
352

    
353
static void do_cont(void)
354
{
355
    vm_start();
356
}
357

    
358
#ifdef CONFIG_GDBSTUB
359
static void do_gdbserver(int has_port, int port)
360
{
361
    if (!has_port)
362
        port = DEFAULT_GDBSTUB_PORT;
363
    if (gdbserver_start(port) < 0) {
364
        qemu_printf("Could not open gdbserver socket on port %d\n", port);
365
    } else {
366
        qemu_printf("Waiting gdb connection on port %d\n", port);
367
    }
368
}
369
#endif
370

    
371
static void term_printc(int c)
372
{
373
    term_printf("'");
374
    switch(c) {
375
    case '\'':
376
        term_printf("\\'");
377
        break;
378
    case '\\':
379
        term_printf("\\\\");
380
        break;
381
    case '\n':
382
        term_printf("\\n");
383
        break;
384
    case '\r':
385
        term_printf("\\r");
386
        break;
387
    default:
388
        if (c >= 32 && c <= 126) {
389
            term_printf("%c", c);
390
        } else {
391
            term_printf("\\x%02x", c);
392
        }
393
        break;
394
    }
395
    term_printf("'");
396
}
397

    
398
static void memory_dump(int count, int format, int wsize, 
399
                        target_ulong addr, int is_physical)
400
{
401
    int nb_per_line, l, line_size, i, max_digits, len;
402
    uint8_t buf[16];
403
    uint64_t v;
404

    
405
    if (format == 'i') {
406
        int flags;
407
        flags = 0;
408
#ifdef TARGET_I386
409
        if (wsize == 2) {
410
            flags = 1;
411
        } else if (wsize == 4) {
412
            flags = 0;
413
        } else {
414
            /* as default we use the current CS size */
415
            flags = 0;
416
            if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
417
                flags = 1;
418
        }
419
#endif
420
        monitor_disas(addr, count, is_physical, flags);
421
        return;
422
    }
423

    
424
    len = wsize * count;
425
    if (wsize == 1)
426
        line_size = 8;
427
    else
428
        line_size = 16;
429
    nb_per_line = line_size / wsize;
430
    max_digits = 0;
431

    
432
    switch(format) {
433
    case 'o':
434
        max_digits = (wsize * 8 + 2) / 3;
435
        break;
436
    default:
437
    case 'x':
438
        max_digits = (wsize * 8) / 4;
439
        break;
440
    case 'u':
441
    case 'd':
442
        max_digits = (wsize * 8 * 10 + 32) / 33;
443
        break;
444
    case 'c':
445
        wsize = 1;
446
        break;
447
    }
448

    
449
    while (len > 0) {
450
        term_printf(TARGET_FMT_lx ":", addr);
451
        l = len;
452
        if (l > line_size)
453
            l = line_size;
454
        if (is_physical) {
455
            cpu_physical_memory_rw(addr, buf, l, 0);
456
        } else {
457
            cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
458
        }
459
        i = 0; 
460
        while (i < l) {
461
            switch(wsize) {
462
            default:
463
            case 1:
464
                v = ldub_raw(buf + i);
465
                break;
466
            case 2:
467
                v = lduw_raw(buf + i);
468
                break;
469
            case 4:
470
                v = (uint32_t)ldl_raw(buf + i);
471
                break;
472
            case 8:
473
                v = ldq_raw(buf + i);
474
                break;
475
            }
476
            term_printf(" ");
477
            switch(format) {
478
            case 'o':
479
                term_printf("%#*llo", max_digits, v);
480
                break;
481
            case 'x':
482
                term_printf("0x%0*llx", max_digits, v);
483
                break;
484
            case 'u':
485
                term_printf("%*llu", max_digits, v);
486
                break;
487
            case 'd':
488
                term_printf("%*lld", max_digits, v);
489
                break;
490
            case 'c':
491
                term_printc(v);
492
                break;
493
            }
494
            i += wsize;
495
        }
496
        term_printf("\n");
497
        addr += l;
498
        len -= l;
499
    }
500
}
501

    
502
#if TARGET_LONG_BITS == 64
503
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
504
#else
505
#define GET_TLONG(h, l) (l)
506
#endif
507

    
508
static void do_memory_dump(int count, int format, int size, 
509
                           uint32_t addrh, uint32_t addrl)
510
{
511
    target_long addr = GET_TLONG(addrh, addrl);
512
    memory_dump(count, format, size, addr, 0);
513
}
514

    
515
static void do_physical_memory_dump(int count, int format, int size,
516
                                    uint32_t addrh, uint32_t addrl)
517

    
518
{
519
    target_long addr = GET_TLONG(addrh, addrl);
520
    memory_dump(count, format, size, addr, 1);
521
}
522

    
523
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
524
{
525
    target_long val = GET_TLONG(valh, vall);
526
#if TARGET_LONG_BITS == 32
527
    switch(format) {
528
    case 'o':
529
        term_printf("%#o", val);
530
        break;
531
    case 'x':
532
        term_printf("%#x", val);
533
        break;
534
    case 'u':
535
        term_printf("%u", val);
536
        break;
537
    default:
538
    case 'd':
539
        term_printf("%d", val);
540
        break;
541
    case 'c':
542
        term_printc(val);
543
        break;
544
    }
545
#else
546
    switch(format) {
547
    case 'o':
548
        term_printf("%#llo", val);
549
        break;
550
    case 'x':
551
        term_printf("%#llx", val);
552
        break;
553
    case 'u':
554
        term_printf("%llu", val);
555
        break;
556
    default:
557
    case 'd':
558
        term_printf("%lld", val);
559
        break;
560
    case 'c':
561
        term_printc(val);
562
        break;
563
    }
564
#endif
565
    term_printf("\n");
566
}
567

    
568
static void do_sum(uint32_t start, uint32_t size)
569
{
570
    uint32_t addr;
571
    uint8_t buf[1];
572
    uint16_t sum;
573

    
574
    sum = 0;
575
    for(addr = start; addr < (start + size); addr++) {
576
        cpu_physical_memory_rw(addr, buf, 1, 0);
577
        /* BSD sum algorithm ('sum' Unix command) */
578
        sum = (sum >> 1) | (sum << 15);
579
        sum += buf[0];
580
    }
581
    term_printf("%05d\n", sum);
582
}
583

    
584
typedef struct {
585
    int keycode;
586
    const char *name;
587
} KeyDef;
588

    
589
static const KeyDef key_defs[] = {
590
    { 0x2a, "shift" },
591
    { 0x36, "shift_r" },
592
    
593
    { 0x38, "alt" },
594
    { 0xb8, "alt_r" },
595
    { 0x1d, "ctrl" },
596
    { 0x9d, "ctrl_r" },
597

    
598
    { 0xdd, "menu" },
599

    
600
    { 0x01, "esc" },
601

    
602
    { 0x02, "1" },
603
    { 0x03, "2" },
604
    { 0x04, "3" },
605
    { 0x05, "4" },
606
    { 0x06, "5" },
607
    { 0x07, "6" },
608
    { 0x08, "7" },
609
    { 0x09, "8" },
610
    { 0x0a, "9" },
611
    { 0x0b, "0" },
612
    { 0x0e, "backspace" },
613

    
614
    { 0x0f, "tab" },
615
    { 0x10, "q" },
616
    { 0x11, "w" },
617
    { 0x12, "e" },
618
    { 0x13, "r" },
619
    { 0x14, "t" },
620
    { 0x15, "y" },
621
    { 0x16, "u" },
622
    { 0x17, "i" },
623
    { 0x18, "o" },
624
    { 0x19, "p" },
625

    
626
    { 0x1c, "ret" },
627

    
628
    { 0x1e, "a" },
629
    { 0x1f, "s" },
630
    { 0x20, "d" },
631
    { 0x21, "f" },
632
    { 0x22, "g" },
633
    { 0x23, "h" },
634
    { 0x24, "j" },
635
    { 0x25, "k" },
636
    { 0x26, "l" },
637

    
638
    { 0x2c, "z" },
639
    { 0x2d, "x" },
640
    { 0x2e, "c" },
641
    { 0x2f, "v" },
642
    { 0x30, "b" },
643
    { 0x31, "n" },
644
    { 0x32, "m" },
645
    
646
    { 0x39, "spc" },
647
    { 0x3a, "caps_lock" },
648
    { 0x3b, "f1" },
649
    { 0x3c, "f2" },
650
    { 0x3d, "f3" },
651
    { 0x3e, "f4" },
652
    { 0x3f, "f5" },
653
    { 0x40, "f6" },
654
    { 0x41, "f7" },
655
    { 0x42, "f8" },
656
    { 0x43, "f9" },
657
    { 0x44, "f10" },
658
    { 0x45, "num_lock" },
659
    { 0x46, "scroll_lock" },
660

    
661
    { 0x56, "<" },
662

    
663
    { 0x57, "f11" },
664
    { 0x58, "f12" },
665

    
666
    { 0xb7, "print" },
667

    
668
    { 0xc7, "home" },
669
    { 0xc9, "pgup" },
670
    { 0xd1, "pgdn" },
671
    { 0xcf, "end" },
672

    
673
    { 0xcb, "left" },
674
    { 0xc8, "up" },
675
    { 0xd0, "down" },
676
    { 0xcd, "right" },
677

    
678
    { 0xd2, "insert" },
679
    { 0xd3, "delete" },
680
    { 0, NULL },
681
};
682

    
683
static int get_keycode(const char *key)
684
{
685
    const KeyDef *p;
686

    
687
    for(p = key_defs; p->name != NULL; p++) {
688
        if (!strcmp(key, p->name))
689
            return p->keycode;
690
    }
691
    return -1;
692
}
693

    
694
static void do_send_key(const char *string)
695
{
696
    char keybuf[16], *q;
697
    uint8_t keycodes[16];
698
    const char *p;
699
    int nb_keycodes, keycode, i;
700
    
701
    nb_keycodes = 0;
702
    p = string;
703
    while (*p != '\0') {
704
        q = keybuf;
705
        while (*p != '\0' && *p != '-') {
706
            if ((q - keybuf) < sizeof(keybuf) - 1) {
707
                *q++ = *p;
708
            }
709
            p++;
710
        }
711
        *q = '\0';
712
        keycode = get_keycode(keybuf);
713
        if (keycode < 0) {
714
            term_printf("unknown key: '%s'\n", keybuf);
715
            return;
716
        }
717
        keycodes[nb_keycodes++] = keycode;
718
        if (*p == '\0')
719
            break;
720
        p++;
721
    }
722
    /* key down events */
723
    for(i = 0; i < nb_keycodes; i++) {
724
        keycode = keycodes[i];
725
        if (keycode & 0x80)
726
            kbd_put_keycode(0xe0);
727
        kbd_put_keycode(keycode & 0x7f);
728
    }
729
    /* key up events */
730
    for(i = nb_keycodes - 1; i >= 0; i--) {
731
        keycode = keycodes[i];
732
        if (keycode & 0x80)
733
            kbd_put_keycode(0xe0);
734
        kbd_put_keycode(keycode | 0x80);
735
    }
736
}
737

    
738
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
739
{
740
    uint32_t val;
741
    int suffix;
742

    
743
    if (has_index) {
744
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
745
        addr++;
746
    }
747
    addr &= 0xffff;
748

    
749
    switch(size) {
750
    default:
751
    case 1:
752
        val = cpu_inb(NULL, addr);
753
        suffix = 'b';
754
        break;
755
    case 2:
756
        val = cpu_inw(NULL, addr);
757
        suffix = 'w';
758
        break;
759
    case 4:
760
        val = cpu_inl(NULL, addr);
761
        suffix = 'l';
762
        break;
763
    }
764
    term_printf("port%c[0x%04x] = %#0*x\n",
765
                suffix, addr, size * 2, val);
766
}
767

    
768
static void do_system_reset(void)
769
{
770
    qemu_system_reset_request();
771
}
772

    
773
static void do_system_powerdown(void)
774
{
775
    qemu_system_powerdown_request();
776
}
777

    
778
#if defined(TARGET_I386)
779
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
780
{
781
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
782
                addr,
783
                pte & mask,
784
                pte & PG_GLOBAL_MASK ? 'G' : '-',
785
                pte & PG_PSE_MASK ? 'P' : '-',
786
                pte & PG_DIRTY_MASK ? 'D' : '-',
787
                pte & PG_ACCESSED_MASK ? 'A' : '-',
788
                pte & PG_PCD_MASK ? 'C' : '-',
789
                pte & PG_PWT_MASK ? 'T' : '-',
790
                pte & PG_USER_MASK ? 'U' : '-',
791
                pte & PG_RW_MASK ? 'W' : '-');
792
}
793

    
794
static void tlb_info(void)
795
{
796
    CPUState *env = cpu_single_env;
797
    int l1, l2;
798
    uint32_t pgd, pde, pte;
799

    
800
    if (!(env->cr[0] & CR0_PG_MASK)) {
801
        term_printf("PG disabled\n");
802
        return;
803
    }
804
    pgd = env->cr[3] & ~0xfff;
805
    for(l1 = 0; l1 < 1024; l1++) {
806
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
807
        pde = le32_to_cpu(pde);
808
        if (pde & PG_PRESENT_MASK) {
809
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
810
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
811
            } else {
812
                for(l2 = 0; l2 < 1024; l2++) {
813
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
814
                                             (uint8_t *)&pte, 4);
815
                    pte = le32_to_cpu(pte);
816
                    if (pte & PG_PRESENT_MASK) {
817
                        print_pte((l1 << 22) + (l2 << 12), 
818
                                  pte & ~PG_PSE_MASK, 
819
                                  ~0xfff);
820
                    }
821
                }
822
            }
823
        }
824
    }
825
}
826

    
827
static void mem_print(uint32_t *pstart, int *plast_prot, 
828
                      uint32_t end, int prot)
829
{
830
    int prot1;
831
    prot1 = *plast_prot;
832
    if (prot != prot1) {
833
        if (*pstart != -1) {
834
            term_printf("%08x-%08x %08x %c%c%c\n",
835
                        *pstart, end, end - *pstart, 
836
                        prot1 & PG_USER_MASK ? 'u' : '-',
837
                        'r',
838
                        prot1 & PG_RW_MASK ? 'w' : '-');
839
        }
840
        if (prot != 0)
841
            *pstart = end;
842
        else
843
            *pstart = -1;
844
        *plast_prot = prot;
845
    }
846
}
847

    
848
static void mem_info(void)
849
{
850
    CPUState *env = cpu_single_env;
851
    int l1, l2, prot, last_prot;
852
    uint32_t pgd, pde, pte, start, end;
853

    
854
    if (!(env->cr[0] & CR0_PG_MASK)) {
855
        term_printf("PG disabled\n");
856
        return;
857
    }
858
    pgd = env->cr[3] & ~0xfff;
859
    last_prot = 0;
860
    start = -1;
861
    for(l1 = 0; l1 < 1024; l1++) {
862
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
863
        pde = le32_to_cpu(pde);
864
        end = l1 << 22;
865
        if (pde & PG_PRESENT_MASK) {
866
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
867
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
868
                mem_print(&start, &last_prot, end, prot);
869
            } else {
870
                for(l2 = 0; l2 < 1024; l2++) {
871
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
872
                                             (uint8_t *)&pte, 4);
873
                    pte = le32_to_cpu(pte);
874
                    end = (l1 << 22) + (l2 << 12);
875
                    if (pte & PG_PRESENT_MASK) {
876
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
877
                    } else {
878
                        prot = 0;
879
                    }
880
                    mem_print(&start, &last_prot, end, prot);
881
                }
882
            }
883
        } else {
884
            prot = 0;
885
            mem_print(&start, &last_prot, end, prot);
886
        }
887
    }
888
}
889
#endif
890

    
891
static void do_info_kqemu(void)
892
{
893
#ifdef USE_KQEMU
894
    int val;
895
    val = 0;
896
    if (cpu_single_env)
897
        val = cpu_single_env->kqemu_enabled;
898
    term_printf("kqemu is %s\n", val ? "enabled" : "disabled");
899
#else
900
    term_printf("kqemu support is not compiled\n");
901
#endif
902
} 
903

    
904
static term_cmd_t term_cmds[] = {
905
    { "help|?", "s?", do_help, 
906
      "[cmd]", "show the help" },
907
    { "commit", "", do_commit, 
908
      "", "commit changes to the disk images (if -snapshot is used)" },
909
    { "info", "s?", do_info,
910
      "subcommand", "show various information about the system state" },
911
    { "q|quit", "", do_quit,
912
      "", "quit the emulator" },
913
    { "eject", "-fB", do_eject,
914
      "[-f] device", "eject a removable media (use -f to force it)" },
915
    { "change", "BF", do_change,
916
      "device filename", "change a removable media" },
917
    { "screendump", "F", do_screen_dump, 
918
      "filename", "save screen into PPM image 'filename'" },
919
    { "log", "s", do_log,
920
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
921
    { "savevm", "F", do_savevm,
922
      "filename", "save the whole virtual machine state to 'filename'" }, 
923
    { "loadvm", "F", do_loadvm,
924
      "filename", "restore the whole virtual machine state from 'filename'" }, 
925
    { "stop", "", do_stop, 
926
      "", "stop emulation", },
927
    { "c|cont", "", do_cont, 
928
      "", "resume emulation", },
929
#ifdef CONFIG_GDBSTUB
930
    { "gdbserver", "i?", do_gdbserver, 
931
      "[port]", "start gdbserver session (default port=1234)", },
932
#endif
933
    { "x", "/l", do_memory_dump, 
934
      "/fmt addr", "virtual memory dump starting at 'addr'", },
935
    { "xp", "/l", do_physical_memory_dump, 
936
      "/fmt addr", "physical memory dump starting at 'addr'", },
937
    { "p|print", "/l", do_print, 
938
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
939
    { "i", "/ii.", do_ioport_read, 
940
      "/fmt addr", "I/O port read" },
941

    
942
    { "sendkey", "s", do_send_key, 
943
      "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
944
    { "system_reset", "", do_system_reset, 
945
      "", "reset the system" },
946
    { "system_powerdown", "", do_system_powerdown, 
947
      "", "send system power down event" },
948
    { "sum", "ii", do_sum, 
949
      "addr size", "compute the checksum of a memory region" },
950
    { "usb_add", "s", do_usb_add,
951
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
952
    { "usb_del", "s", do_usb_del,
953
      "device", "remove USB device 'bus.addr'" },
954
    { NULL, NULL, }, 
955
};
956

    
957
static term_cmd_t info_cmds[] = {
958
    { "version", "", do_info_version,
959
      "", "show the version of qemu" },
960
    { "network", "", do_info_network,
961
      "", "show the network state" },
962
    { "block", "", do_info_block,
963
      "", "show the block devices" },
964
    { "registers", "", do_info_registers,
965
      "", "show the cpu registers" },
966
    { "history", "", do_info_history,
967
      "", "show the command line history", },
968
    { "irq", "", irq_info,
969
      "", "show the interrupts statistics (if available)", },
970
    { "pic", "", pic_info,
971
      "", "show i8259 (PIC) state", },
972
    { "pci", "", pci_info,
973
      "", "show PCI info", },
974
#if defined(TARGET_I386)
975
    { "tlb", "", tlb_info,
976
      "", "show virtual to physical memory mappings", },
977
    { "mem", "", mem_info,
978
      "", "show the active virtual memory mappings", },
979
#endif
980
    { "jit", "", do_info_jit,
981
      "", "show dynamic compiler info", },
982
    { "kqemu", "", do_info_kqemu,
983
      "", "show kqemu information", },
984
    { "usb", "", usb_info,
985
      "", "show guest USB devices", },
986
    { "usbhost", "", usb_host_info,
987
      "", "show host USB devices", },
988
    { NULL, NULL, },
989
};
990

    
991
/*******************************************************************/
992

    
993
static const char *pch;
994
static jmp_buf expr_env;
995

    
996
#define MD_TLONG 0
997
#define MD_I32   1
998

    
999
typedef struct MonitorDef {
1000
    const char *name;
1001
    int offset;
1002
    target_long (*get_value)(struct MonitorDef *md, int val);
1003
    int type;
1004
} MonitorDef;
1005

    
1006
#if defined(TARGET_I386)
1007
static target_long monitor_get_pc (struct MonitorDef *md, int val)
1008
{
1009
    return cpu_single_env->eip + cpu_single_env->segs[R_CS].base;
1010
}
1011
#endif
1012

    
1013
#if defined(TARGET_PPC)
1014
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1015
{
1016
    unsigned int u;
1017
    int i;
1018

    
1019
    u = 0;
1020
    for (i = 0; i < 8; i++)
1021
        u |= cpu_single_env->crf[i] << (32 - (4 * i));
1022

    
1023
    return u;
1024
}
1025

    
1026
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1027
{
1028
    return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
1029
        (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
1030
        (cpu_single_env->msr[MSR_EE] << MSR_EE) |
1031
        (cpu_single_env->msr[MSR_PR] << MSR_PR) |
1032
        (cpu_single_env->msr[MSR_FP] << MSR_FP) |
1033
        (cpu_single_env->msr[MSR_ME] << MSR_ME) |
1034
        (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
1035
        (cpu_single_env->msr[MSR_SE] << MSR_SE) |
1036
        (cpu_single_env->msr[MSR_BE] << MSR_BE) |
1037
        (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
1038
        (cpu_single_env->msr[MSR_IP] << MSR_IP) |
1039
        (cpu_single_env->msr[MSR_IR] << MSR_IR) |
1040
        (cpu_single_env->msr[MSR_DR] << MSR_DR) |
1041
        (cpu_single_env->msr[MSR_RI] << MSR_RI) |
1042
        (cpu_single_env->msr[MSR_LE] << MSR_LE);
1043
}
1044

    
1045
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1046
{
1047
    return (cpu_single_env->xer[XER_SO] << XER_SO) |
1048
        (cpu_single_env->xer[XER_OV] << XER_OV) |
1049
        (cpu_single_env->xer[XER_CA] << XER_CA) |
1050
        (cpu_single_env->xer[XER_BC] << XER_BC);
1051
}
1052

    
1053
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1054
{
1055
    return cpu_ppc_load_decr(cpu_single_env);
1056
}
1057

    
1058
static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1059
{
1060
    return cpu_ppc_load_tbu(cpu_single_env);
1061
}
1062

    
1063
static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1064
{
1065
    return cpu_ppc_load_tbl(cpu_single_env);
1066
}
1067
#endif
1068

    
1069
#if defined(TARGET_SPARC)
1070
#ifndef TARGET_SPARC64
1071
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1072
{
1073
    return GET_PSR(cpu_single_env);
1074
}
1075
#endif
1076

    
1077
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1078
{
1079
    return cpu_single_env->regwptr[val];
1080
}
1081
#endif
1082

    
1083
static MonitorDef monitor_defs[] = {
1084
#ifdef TARGET_I386
1085

    
1086
#define SEG(name, seg) \
1087
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1088
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1089
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1090

    
1091
    { "eax", offsetof(CPUState, regs[0]) },
1092
    { "ecx", offsetof(CPUState, regs[1]) },
1093
    { "edx", offsetof(CPUState, regs[2]) },
1094
    { "ebx", offsetof(CPUState, regs[3]) },
1095
    { "esp|sp", offsetof(CPUState, regs[4]) },
1096
    { "ebp|fp", offsetof(CPUState, regs[5]) },
1097
    { "esi", offsetof(CPUState, regs[6]) },
1098
    { "edi", offsetof(CPUState, regs[7]) },
1099
#ifdef TARGET_X86_64
1100
    { "r8", offsetof(CPUState, regs[8]) },
1101
    { "r9", offsetof(CPUState, regs[9]) },
1102
    { "r10", offsetof(CPUState, regs[10]) },
1103
    { "r11", offsetof(CPUState, regs[11]) },
1104
    { "r12", offsetof(CPUState, regs[12]) },
1105
    { "r13", offsetof(CPUState, regs[13]) },
1106
    { "r14", offsetof(CPUState, regs[14]) },
1107
    { "r15", offsetof(CPUState, regs[15]) },
1108
#endif
1109
    { "eflags", offsetof(CPUState, eflags) },
1110
    { "eip", offsetof(CPUState, eip) },
1111
    SEG("cs", R_CS)
1112
    SEG("ds", R_DS)
1113
    SEG("es", R_ES)
1114
    SEG("ss", R_SS)
1115
    SEG("fs", R_FS)
1116
    SEG("gs", R_GS)
1117
    { "pc", 0, monitor_get_pc, },
1118
#elif defined(TARGET_PPC)
1119
    { "r0", offsetof(CPUState, gpr[0]) },
1120
    { "r1", offsetof(CPUState, gpr[1]) },
1121
    { "r2", offsetof(CPUState, gpr[2]) },
1122
    { "r3", offsetof(CPUState, gpr[3]) },
1123
    { "r4", offsetof(CPUState, gpr[4]) },
1124
    { "r5", offsetof(CPUState, gpr[5]) },
1125
    { "r6", offsetof(CPUState, gpr[6]) },
1126
    { "r7", offsetof(CPUState, gpr[7]) },
1127
    { "r8", offsetof(CPUState, gpr[8]) },
1128
    { "r9", offsetof(CPUState, gpr[9]) },
1129
    { "r10", offsetof(CPUState, gpr[10]) },
1130
    { "r11", offsetof(CPUState, gpr[11]) },
1131
    { "r12", offsetof(CPUState, gpr[12]) },
1132
    { "r13", offsetof(CPUState, gpr[13]) },
1133
    { "r14", offsetof(CPUState, gpr[14]) },
1134
    { "r15", offsetof(CPUState, gpr[15]) },
1135
    { "r16", offsetof(CPUState, gpr[16]) },
1136
    { "r17", offsetof(CPUState, gpr[17]) },
1137
    { "r18", offsetof(CPUState, gpr[18]) },
1138
    { "r19", offsetof(CPUState, gpr[19]) },
1139
    { "r20", offsetof(CPUState, gpr[20]) },
1140
    { "r21", offsetof(CPUState, gpr[21]) },
1141
    { "r22", offsetof(CPUState, gpr[22]) },
1142
    { "r23", offsetof(CPUState, gpr[23]) },
1143
    { "r24", offsetof(CPUState, gpr[24]) },
1144
    { "r25", offsetof(CPUState, gpr[25]) },
1145
    { "r26", offsetof(CPUState, gpr[26]) },
1146
    { "r27", offsetof(CPUState, gpr[27]) },
1147
    { "r28", offsetof(CPUState, gpr[28]) },
1148
    { "r29", offsetof(CPUState, gpr[29]) },
1149
    { "r30", offsetof(CPUState, gpr[30]) },
1150
    { "r31", offsetof(CPUState, gpr[31]) },
1151
    { "nip|pc", offsetof(CPUState, nip) },
1152
    { "lr", offsetof(CPUState, lr) },
1153
    { "ctr", offsetof(CPUState, ctr) },
1154
    { "decr", 0, &monitor_get_decr, },
1155
    { "ccr", 0, &monitor_get_ccr, },
1156
    { "msr", 0, &monitor_get_msr, },
1157
    { "xer", 0, &monitor_get_xer, },
1158
    { "tbu", 0, &monitor_get_tbu, },
1159
    { "tbl", 0, &monitor_get_tbl, },
1160
    { "sdr1", offsetof(CPUState, sdr1) },
1161
    { "sr0", offsetof(CPUState, sr[0]) },
1162
    { "sr1", offsetof(CPUState, sr[1]) },
1163
    { "sr2", offsetof(CPUState, sr[2]) },
1164
    { "sr3", offsetof(CPUState, sr[3]) },
1165
    { "sr4", offsetof(CPUState, sr[4]) },
1166
    { "sr5", offsetof(CPUState, sr[5]) },
1167
    { "sr6", offsetof(CPUState, sr[6]) },
1168
    { "sr7", offsetof(CPUState, sr[7]) },
1169
    { "sr8", offsetof(CPUState, sr[8]) },
1170
    { "sr9", offsetof(CPUState, sr[9]) },
1171
    { "sr10", offsetof(CPUState, sr[10]) },
1172
    { "sr11", offsetof(CPUState, sr[11]) },
1173
    { "sr12", offsetof(CPUState, sr[12]) },
1174
    { "sr13", offsetof(CPUState, sr[13]) },
1175
    { "sr14", offsetof(CPUState, sr[14]) },
1176
    { "sr15", offsetof(CPUState, sr[15]) },
1177
    /* Too lazy to put BATs and SPRs ... */
1178
#elif defined(TARGET_SPARC)
1179
    { "g0", offsetof(CPUState, gregs[0]) },
1180
    { "g1", offsetof(CPUState, gregs[1]) },
1181
    { "g2", offsetof(CPUState, gregs[2]) },
1182
    { "g3", offsetof(CPUState, gregs[3]) },
1183
    { "g4", offsetof(CPUState, gregs[4]) },
1184
    { "g5", offsetof(CPUState, gregs[5]) },
1185
    { "g6", offsetof(CPUState, gregs[6]) },
1186
    { "g7", offsetof(CPUState, gregs[7]) },
1187
    { "o0", 0, monitor_get_reg },
1188
    { "o1", 1, monitor_get_reg },
1189
    { "o2", 2, monitor_get_reg },
1190
    { "o3", 3, monitor_get_reg },
1191
    { "o4", 4, monitor_get_reg },
1192
    { "o5", 5, monitor_get_reg },
1193
    { "o6", 6, monitor_get_reg },
1194
    { "o7", 7, monitor_get_reg },
1195
    { "l0", 8, monitor_get_reg },
1196
    { "l1", 9, monitor_get_reg },
1197
    { "l2", 10, monitor_get_reg },
1198
    { "l3", 11, monitor_get_reg },
1199
    { "l4", 12, monitor_get_reg },
1200
    { "l5", 13, monitor_get_reg },
1201
    { "l6", 14, monitor_get_reg },
1202
    { "l7", 15, monitor_get_reg },
1203
    { "i0", 16, monitor_get_reg },
1204
    { "i1", 17, monitor_get_reg },
1205
    { "i2", 18, monitor_get_reg },
1206
    { "i3", 19, monitor_get_reg },
1207
    { "i4", 20, monitor_get_reg },
1208
    { "i5", 21, monitor_get_reg },
1209
    { "i6", 22, monitor_get_reg },
1210
    { "i7", 23, monitor_get_reg },
1211
    { "pc", offsetof(CPUState, pc) },
1212
    { "npc", offsetof(CPUState, npc) },
1213
    { "y", offsetof(CPUState, y) },
1214
#ifndef TARGET_SPARC64
1215
    { "psr", 0, &monitor_get_psr, },
1216
    { "wim", offsetof(CPUState, wim) },
1217
#endif
1218
    { "tbr", offsetof(CPUState, tbr) },
1219
    { "fsr", offsetof(CPUState, fsr) },
1220
    { "f0", offsetof(CPUState, fpr[0]) },
1221
    { "f1", offsetof(CPUState, fpr[1]) },
1222
    { "f2", offsetof(CPUState, fpr[2]) },
1223
    { "f3", offsetof(CPUState, fpr[3]) },
1224
    { "f4", offsetof(CPUState, fpr[4]) },
1225
    { "f5", offsetof(CPUState, fpr[5]) },
1226
    { "f6", offsetof(CPUState, fpr[6]) },
1227
    { "f7", offsetof(CPUState, fpr[7]) },
1228
    { "f8", offsetof(CPUState, fpr[8]) },
1229
    { "f9", offsetof(CPUState, fpr[9]) },
1230
    { "f10", offsetof(CPUState, fpr[10]) },
1231
    { "f11", offsetof(CPUState, fpr[11]) },
1232
    { "f12", offsetof(CPUState, fpr[12]) },
1233
    { "f13", offsetof(CPUState, fpr[13]) },
1234
    { "f14", offsetof(CPUState, fpr[14]) },
1235
    { "f15", offsetof(CPUState, fpr[15]) },
1236
    { "f16", offsetof(CPUState, fpr[16]) },
1237
    { "f17", offsetof(CPUState, fpr[17]) },
1238
    { "f18", offsetof(CPUState, fpr[18]) },
1239
    { "f19", offsetof(CPUState, fpr[19]) },
1240
    { "f20", offsetof(CPUState, fpr[20]) },
1241
    { "f21", offsetof(CPUState, fpr[21]) },
1242
    { "f22", offsetof(CPUState, fpr[22]) },
1243
    { "f23", offsetof(CPUState, fpr[23]) },
1244
    { "f24", offsetof(CPUState, fpr[24]) },
1245
    { "f25", offsetof(CPUState, fpr[25]) },
1246
    { "f26", offsetof(CPUState, fpr[26]) },
1247
    { "f27", offsetof(CPUState, fpr[27]) },
1248
    { "f28", offsetof(CPUState, fpr[28]) },
1249
    { "f29", offsetof(CPUState, fpr[29]) },
1250
    { "f30", offsetof(CPUState, fpr[30]) },
1251
    { "f31", offsetof(CPUState, fpr[31]) },
1252
#ifdef TARGET_SPARC64
1253
    { "f32", offsetof(CPUState, fpr[32]) },
1254
    { "f34", offsetof(CPUState, fpr[34]) },
1255
    { "f36", offsetof(CPUState, fpr[36]) },
1256
    { "f38", offsetof(CPUState, fpr[38]) },
1257
    { "f40", offsetof(CPUState, fpr[40]) },
1258
    { "f42", offsetof(CPUState, fpr[42]) },
1259
    { "f44", offsetof(CPUState, fpr[44]) },
1260
    { "f46", offsetof(CPUState, fpr[46]) },
1261
    { "f48", offsetof(CPUState, fpr[48]) },
1262
    { "f50", offsetof(CPUState, fpr[50]) },
1263
    { "f52", offsetof(CPUState, fpr[52]) },
1264
    { "f54", offsetof(CPUState, fpr[54]) },
1265
    { "f56", offsetof(CPUState, fpr[56]) },
1266
    { "f58", offsetof(CPUState, fpr[58]) },
1267
    { "f60", offsetof(CPUState, fpr[60]) },
1268
    { "f62", offsetof(CPUState, fpr[62]) },
1269
    { "asi", offsetof(CPUState, asi) },
1270
    { "pstate", offsetof(CPUState, pstate) },
1271
    { "cansave", offsetof(CPUState, cansave) },
1272
    { "canrestore", offsetof(CPUState, canrestore) },
1273
    { "otherwin", offsetof(CPUState, otherwin) },
1274
    { "wstate", offsetof(CPUState, wstate) },
1275
    { "cleanwin", offsetof(CPUState, cleanwin) },
1276
    { "fprs", offsetof(CPUState, fprs) },
1277
#endif
1278
#endif
1279
    { NULL },
1280
};
1281

    
1282
static void expr_error(const char *fmt) 
1283
{
1284
    term_printf(fmt);
1285
    term_printf("\n");
1286
    longjmp(expr_env, 1);
1287
}
1288

    
1289
static int get_monitor_def(target_long *pval, const char *name)
1290
{
1291
    MonitorDef *md;
1292
    void *ptr;
1293

    
1294
    for(md = monitor_defs; md->name != NULL; md++) {
1295
        if (compare_cmd(name, md->name)) {
1296
            if (md->get_value) {
1297
                *pval = md->get_value(md, md->offset);
1298
            } else {
1299
                ptr = (uint8_t *)cpu_single_env + md->offset;
1300
                switch(md->type) {
1301
                case MD_I32:
1302
                    *pval = *(int32_t *)ptr;
1303
                    break;
1304
                case MD_TLONG:
1305
                    *pval = *(target_long *)ptr;
1306
                    break;
1307
                default:
1308
                    *pval = 0;
1309
                    break;
1310
                }
1311
            }
1312
            return 0;
1313
        }
1314
    }
1315
    return -1;
1316
}
1317

    
1318
static void next(void)
1319
{
1320
    if (pch != '\0') {
1321
        pch++;
1322
        while (isspace(*pch))
1323
            pch++;
1324
    }
1325
}
1326

    
1327
static target_long expr_sum(void);
1328

    
1329
static target_long expr_unary(void)
1330
{
1331
    target_long n;
1332
    char *p;
1333

    
1334
    switch(*pch) {
1335
    case '+':
1336
        next();
1337
        n = expr_unary();
1338
        break;
1339
    case '-':
1340
        next();
1341
        n = -expr_unary();
1342
        break;
1343
    case '~':
1344
        next();
1345
        n = ~expr_unary();
1346
        break;
1347
    case '(':
1348
        next();
1349
        n = expr_sum();
1350
        if (*pch != ')') {
1351
            expr_error("')' expected");
1352
        }
1353
        next();
1354
        break;
1355
    case '\'':
1356
        pch++;
1357
        if (*pch == '\0')
1358
            expr_error("character constant expected");
1359
        n = *pch;
1360
        pch++;
1361
        if (*pch != '\'')
1362
            expr_error("missing terminating \' character");
1363
        next();
1364
        break;
1365
    case '$':
1366
        {
1367
            char buf[128], *q;
1368
            
1369
            pch++;
1370
            q = buf;
1371
            while ((*pch >= 'a' && *pch <= 'z') ||
1372
                   (*pch >= 'A' && *pch <= 'Z') ||
1373
                   (*pch >= '0' && *pch <= '9') ||
1374
                   *pch == '_' || *pch == '.') {
1375
                if ((q - buf) < sizeof(buf) - 1)
1376
                    *q++ = *pch;
1377
                pch++;
1378
            }
1379
            while (isspace(*pch))
1380
                pch++;
1381
            *q = 0;
1382
            if (get_monitor_def(&n, buf))
1383
                expr_error("unknown register");
1384
        }
1385
        break;
1386
    case '\0':
1387
        expr_error("unexpected end of expression");
1388
        n = 0;
1389
        break;
1390
    default:
1391
        n = strtoul(pch, &p, 0);
1392
        if (pch == p) {
1393
            expr_error("invalid char in expression");
1394
        }
1395
        pch = p;
1396
        while (isspace(*pch))
1397
            pch++;
1398
        break;
1399
    }
1400
    return n;
1401
}
1402

    
1403

    
1404
static target_long expr_prod(void)
1405
{
1406
    target_long val, val2;
1407
    int op;
1408
    
1409
    val = expr_unary();
1410
    for(;;) {
1411
        op = *pch;
1412
        if (op != '*' && op != '/' && op != '%')
1413
            break;
1414
        next();
1415
        val2 = expr_unary();
1416
        switch(op) {
1417
        default:
1418
        case '*':
1419
            val *= val2;
1420
            break;
1421
        case '/':
1422
        case '%':
1423
            if (val2 == 0) 
1424
                expr_error("division by zero");
1425
            if (op == '/')
1426
                val /= val2;
1427
            else
1428
                val %= val2;
1429
            break;
1430
        }
1431
    }
1432
    return val;
1433
}
1434

    
1435
static target_long expr_logic(void)
1436
{
1437
    target_long val, val2;
1438
    int op;
1439

    
1440
    val = expr_prod();
1441
    for(;;) {
1442
        op = *pch;
1443
        if (op != '&' && op != '|' && op != '^')
1444
            break;
1445
        next();
1446
        val2 = expr_prod();
1447
        switch(op) {
1448
        default:
1449
        case '&':
1450
            val &= val2;
1451
            break;
1452
        case '|':
1453
            val |= val2;
1454
            break;
1455
        case '^':
1456
            val ^= val2;
1457
            break;
1458
        }
1459
    }
1460
    return val;
1461
}
1462

    
1463
static target_long expr_sum(void)
1464
{
1465
    target_long val, val2;
1466
    int op;
1467

    
1468
    val = expr_logic();
1469
    for(;;) {
1470
        op = *pch;
1471
        if (op != '+' && op != '-')
1472
            break;
1473
        next();
1474
        val2 = expr_logic();
1475
        if (op == '+')
1476
            val += val2;
1477
        else
1478
            val -= val2;
1479
    }
1480
    return val;
1481
}
1482

    
1483
static int get_expr(target_long *pval, const char **pp)
1484
{
1485
    pch = *pp;
1486
    if (setjmp(expr_env)) {
1487
        *pp = pch;
1488
        return -1;
1489
    }
1490
    while (isspace(*pch))
1491
        pch++;
1492
    *pval = expr_sum();
1493
    *pp = pch;
1494
    return 0;
1495
}
1496

    
1497
static int get_str(char *buf, int buf_size, const char **pp)
1498
{
1499
    const char *p;
1500
    char *q;
1501
    int c;
1502

    
1503
    q = buf;
1504
    p = *pp;
1505
    while (isspace(*p))
1506
        p++;
1507
    if (*p == '\0') {
1508
    fail:
1509
        *q = '\0';
1510
        *pp = p;
1511
        return -1;
1512
    }
1513
    if (*p == '\"') {
1514
        p++;
1515
        while (*p != '\0' && *p != '\"') {
1516
            if (*p == '\\') {
1517
                p++;
1518
                c = *p++;
1519
                switch(c) {
1520
                case 'n':
1521
                    c = '\n';
1522
                    break;
1523
                case 'r':
1524
                    c = '\r';
1525
                    break;
1526
                case '\\':
1527
                case '\'':
1528
                case '\"':
1529
                    break;
1530
                default:
1531
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
1532
                    goto fail;
1533
                }
1534
                if ((q - buf) < buf_size - 1) {
1535
                    *q++ = c;
1536
                }
1537
            } else {
1538
                if ((q - buf) < buf_size - 1) {
1539
                    *q++ = *p;
1540
                }
1541
                p++;
1542
            }
1543
        }
1544
        if (*p != '\"') {
1545
            qemu_printf("unterminated string\n");
1546
            goto fail;
1547
        }
1548
        p++;
1549
    } else {
1550
        while (*p != '\0' && !isspace(*p)) {
1551
            if ((q - buf) < buf_size - 1) {
1552
                *q++ = *p;
1553
            }
1554
            p++;
1555
        }
1556
    }
1557
    *q = '\0';
1558
    *pp = p;
1559
    return 0;
1560
}
1561

    
1562
static int default_fmt_format = 'x';
1563
static int default_fmt_size = 4;
1564

    
1565
#define MAX_ARGS 16
1566

    
1567
static void monitor_handle_command(const char *cmdline)
1568
{
1569
    const char *p, *pstart, *typestr;
1570
    char *q;
1571
    int c, nb_args, len, i, has_arg;
1572
    term_cmd_t *cmd;
1573
    char cmdname[256];
1574
    char buf[1024];
1575
    void *str_allocated[MAX_ARGS];
1576
    void *args[MAX_ARGS];
1577

    
1578
#ifdef DEBUG
1579
    term_printf("command='%s'\n", cmdline);
1580
#endif
1581
    
1582
    /* extract the command name */
1583
    p = cmdline;
1584
    q = cmdname;
1585
    while (isspace(*p))
1586
        p++;
1587
    if (*p == '\0')
1588
        return;
1589
    pstart = p;
1590
    while (*p != '\0' && *p != '/' && !isspace(*p))
1591
        p++;
1592
    len = p - pstart;
1593
    if (len > sizeof(cmdname) - 1)
1594
        len = sizeof(cmdname) - 1;
1595
    memcpy(cmdname, pstart, len);
1596
    cmdname[len] = '\0';
1597
    
1598
    /* find the command */
1599
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1600
        if (compare_cmd(cmdname, cmd->name)) 
1601
            goto found;
1602
    }
1603
    term_printf("unknown command: '%s'\n", cmdname);
1604
    return;
1605
 found:
1606

    
1607
    for(i = 0; i < MAX_ARGS; i++)
1608
        str_allocated[i] = NULL;
1609
    
1610
    /* parse the parameters */
1611
    typestr = cmd->args_type;
1612
    nb_args = 0;
1613
    for(;;) {
1614
        c = *typestr;
1615
        if (c == '\0')
1616
            break;
1617
        typestr++;
1618
        switch(c) {
1619
        case 'F':
1620
        case 'B':
1621
        case 's':
1622
            {
1623
                int ret;
1624
                char *str;
1625
                
1626
                while (isspace(*p)) 
1627
                    p++;
1628
                if (*typestr == '?') {
1629
                    typestr++;
1630
                    if (*p == '\0') {
1631
                        /* no optional string: NULL argument */
1632
                        str = NULL;
1633
                        goto add_str;
1634
                    }
1635
                }
1636
                ret = get_str(buf, sizeof(buf), &p);
1637
                if (ret < 0) {
1638
                    switch(c) {
1639
                    case 'F':
1640
                        term_printf("%s: filename expected\n", cmdname);
1641
                        break;
1642
                    case 'B':
1643
                        term_printf("%s: block device name expected\n", cmdname);
1644
                        break;
1645
                    default:
1646
                        term_printf("%s: string expected\n", cmdname);
1647
                        break;
1648
                    }
1649
                    goto fail;
1650
                }
1651
                str = qemu_malloc(strlen(buf) + 1);
1652
                strcpy(str, buf);
1653
                str_allocated[nb_args] = str;
1654
            add_str:
1655
                if (nb_args >= MAX_ARGS) {
1656
                error_args:
1657
                    term_printf("%s: too many arguments\n", cmdname);
1658
                    goto fail;
1659
                }
1660
                args[nb_args++] = str;
1661
            }
1662
            break;
1663
        case '/':
1664
            {
1665
                int count, format, size;
1666
                
1667
                while (isspace(*p))
1668
                    p++;
1669
                if (*p == '/') {
1670
                    /* format found */
1671
                    p++;
1672
                    count = 1;
1673
                    if (isdigit(*p)) {
1674
                        count = 0;
1675
                        while (isdigit(*p)) {
1676
                            count = count * 10 + (*p - '0');
1677
                            p++;
1678
                        }
1679
                    }
1680
                    size = -1;
1681
                    format = -1;
1682
                    for(;;) {
1683
                        switch(*p) {
1684
                        case 'o':
1685
                        case 'd':
1686
                        case 'u':
1687
                        case 'x':
1688
                        case 'i':
1689
                        case 'c':
1690
                            format = *p++;
1691
                            break;
1692
                        case 'b':
1693
                            size = 1;
1694
                            p++;
1695
                            break;
1696
                        case 'h':
1697
                            size = 2;
1698
                            p++;
1699
                            break;
1700
                        case 'w':
1701
                            size = 4;
1702
                            p++;
1703
                            break;
1704
                        case 'g':
1705
                        case 'L':
1706
                            size = 8;
1707
                            p++;
1708
                            break;
1709
                        default:
1710
                            goto next;
1711
                        }
1712
                    }
1713
                next:
1714
                    if (*p != '\0' && !isspace(*p)) {
1715
                        term_printf("invalid char in format: '%c'\n", *p);
1716
                        goto fail;
1717
                    }
1718
                    if (format < 0)
1719
                        format = default_fmt_format;
1720
                    if (format != 'i') {
1721
                        /* for 'i', not specifying a size gives -1 as size */
1722
                        if (size < 0)
1723
                            size = default_fmt_size;
1724
                    }
1725
                    default_fmt_size = size;
1726
                    default_fmt_format = format;
1727
                } else {
1728
                    count = 1;
1729
                    format = default_fmt_format;
1730
                    if (format != 'i') {
1731
                        size = default_fmt_size;
1732
                    } else {
1733
                        size = -1;
1734
                    }
1735
                }
1736
                if (nb_args + 3 > MAX_ARGS)
1737
                    goto error_args;
1738
                args[nb_args++] = (void*)count;
1739
                args[nb_args++] = (void*)format;
1740
                args[nb_args++] = (void*)size;
1741
            }
1742
            break;
1743
        case 'i':
1744
        case 'l':
1745
            {
1746
                target_long val;
1747
                while (isspace(*p)) 
1748
                    p++;
1749
                if (*typestr == '?' || *typestr == '.') {
1750
                    typestr++;
1751
                    if (*typestr == '?') {
1752
                        if (*p == '\0')
1753
                            has_arg = 0;
1754
                        else
1755
                            has_arg = 1;
1756
                    } else {
1757
                        if (*p == '.') {
1758
                            p++;
1759
                            while (isspace(*p)) 
1760
                                p++;
1761
                            has_arg = 1;
1762
                        } else {
1763
                            has_arg = 0;
1764
                        }
1765
                    }
1766
                    if (nb_args >= MAX_ARGS)
1767
                        goto error_args;
1768
                    args[nb_args++] = (void *)has_arg;
1769
                    if (!has_arg) {
1770
                        if (nb_args >= MAX_ARGS)
1771
                            goto error_args;
1772
                        val = -1;
1773
                        goto add_num;
1774
                    }
1775
                }
1776
                if (get_expr(&val, &p))
1777
                    goto fail;
1778
            add_num:
1779
                if (c == 'i') {
1780
                    if (nb_args >= MAX_ARGS)
1781
                        goto error_args;
1782
                    args[nb_args++] = (void *)(int)val;
1783
                } else {
1784
                    if ((nb_args + 1) >= MAX_ARGS)
1785
                        goto error_args;
1786
#if TARGET_LONG_BITS == 64
1787
                    args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1788
#else
1789
                    args[nb_args++] = (void *)0;
1790
#endif
1791
                    args[nb_args++] = (void *)(int)(val & 0xffffffff);
1792
                }
1793
            }
1794
            break;
1795
        case '-':
1796
            {
1797
                int has_option;
1798
                /* option */
1799
                
1800
                c = *typestr++;
1801
                if (c == '\0')
1802
                    goto bad_type;
1803
                while (isspace(*p)) 
1804
                    p++;
1805
                has_option = 0;
1806
                if (*p == '-') {
1807
                    p++;
1808
                    if (*p != c) {
1809
                        term_printf("%s: unsupported option -%c\n", 
1810
                                    cmdname, *p);
1811
                        goto fail;
1812
                    }
1813
                    p++;
1814
                    has_option = 1;
1815
                }
1816
                if (nb_args >= MAX_ARGS)
1817
                    goto error_args;
1818
                args[nb_args++] = (void *)has_option;
1819
            }
1820
            break;
1821
        default:
1822
        bad_type:
1823
            term_printf("%s: unknown type '%c'\n", cmdname, c);
1824
            goto fail;
1825
        }
1826
    }
1827
    /* check that all arguments were parsed */
1828
    while (isspace(*p))
1829
        p++;
1830
    if (*p != '\0') {
1831
        term_printf("%s: extraneous characters at the end of line\n", 
1832
                    cmdname);
1833
        goto fail;
1834
    }
1835

    
1836
    switch(nb_args) {
1837
    case 0:
1838
        cmd->handler();
1839
        break;
1840
    case 1:
1841
        cmd->handler(args[0]);
1842
        break;
1843
    case 2:
1844
        cmd->handler(args[0], args[1]);
1845
        break;
1846
    case 3:
1847
        cmd->handler(args[0], args[1], args[2]);
1848
        break;
1849
    case 4:
1850
        cmd->handler(args[0], args[1], args[2], args[3]);
1851
        break;
1852
    case 5:
1853
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1854
        break;
1855
    case 6:
1856
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1857
        break;
1858
    default:
1859
        term_printf("unsupported number of arguments: %d\n", nb_args);
1860
        goto fail;
1861
    }
1862
 fail:
1863
    for(i = 0; i < MAX_ARGS; i++)
1864
        qemu_free(str_allocated[i]);
1865
    return;
1866
}
1867

    
1868
static void cmd_completion(const char *name, const char *list)
1869
{
1870
    const char *p, *pstart;
1871
    char cmd[128];
1872
    int len;
1873

    
1874
    p = list;
1875
    for(;;) {
1876
        pstart = p;
1877
        p = strchr(p, '|');
1878
        if (!p)
1879
            p = pstart + strlen(pstart);
1880
        len = p - pstart;
1881
        if (len > sizeof(cmd) - 2)
1882
            len = sizeof(cmd) - 2;
1883
        memcpy(cmd, pstart, len);
1884
        cmd[len] = '\0';
1885
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1886
            add_completion(cmd);
1887
        }
1888
        if (*p == '\0')
1889
            break;
1890
        p++;
1891
    }
1892
}
1893

    
1894
static void file_completion(const char *input)
1895
{
1896
    DIR *ffs;
1897
    struct dirent *d;
1898
    char path[1024];
1899
    char file[1024], file_prefix[1024];
1900
    int input_path_len;
1901
    const char *p;
1902

    
1903
    p = strrchr(input, '/'); 
1904
    if (!p) {
1905
        input_path_len = 0;
1906
        pstrcpy(file_prefix, sizeof(file_prefix), input);
1907
        strcpy(path, ".");
1908
    } else {
1909
        input_path_len = p - input + 1;
1910
        memcpy(path, input, input_path_len);
1911
        if (input_path_len > sizeof(path) - 1)
1912
            input_path_len = sizeof(path) - 1;
1913
        path[input_path_len] = '\0';
1914
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1915
    }
1916
#ifdef DEBUG_COMPLETION
1917
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1918
#endif
1919
    ffs = opendir(path);
1920
    if (!ffs)
1921
        return;
1922
    for(;;) {
1923
        struct stat sb;
1924
        d = readdir(ffs);
1925
        if (!d)
1926
            break;
1927
        if (strstart(d->d_name, file_prefix, NULL)) {
1928
            memcpy(file, input, input_path_len);
1929
            strcpy(file + input_path_len, d->d_name);
1930
            /* stat the file to find out if it's a directory.
1931
             * In that case add a slash to speed up typing long paths
1932
             */
1933
            stat(file, &sb);
1934
            if(S_ISDIR(sb.st_mode))
1935
                strcat(file, "/");
1936
            add_completion(file);
1937
        }
1938
    }
1939
    closedir(ffs);
1940
}
1941

    
1942
static void block_completion_it(void *opaque, const char *name)
1943
{
1944
    const char *input = opaque;
1945

    
1946
    if (input[0] == '\0' ||
1947
        !strncmp(name, (char *)input, strlen(input))) {
1948
        add_completion(name);
1949
    }
1950
}
1951

    
1952
/* NOTE: this parser is an approximate form of the real command parser */
1953
static void parse_cmdline(const char *cmdline,
1954
                         int *pnb_args, char **args)
1955
{
1956
    const char *p;
1957
    int nb_args, ret;
1958
    char buf[1024];
1959

    
1960
    p = cmdline;
1961
    nb_args = 0;
1962
    for(;;) {
1963
        while (isspace(*p))
1964
            p++;
1965
        if (*p == '\0')
1966
            break;
1967
        if (nb_args >= MAX_ARGS)
1968
            break;
1969
        ret = get_str(buf, sizeof(buf), &p);
1970
        args[nb_args] = qemu_strdup(buf);
1971
        nb_args++;
1972
        if (ret < 0)
1973
            break;
1974
    }
1975
    *pnb_args = nb_args;
1976
}
1977

    
1978
void readline_find_completion(const char *cmdline)
1979
{
1980
    const char *cmdname;
1981
    char *args[MAX_ARGS];
1982
    int nb_args, i, len;
1983
    const char *ptype, *str;
1984
    term_cmd_t *cmd;
1985

    
1986
    parse_cmdline(cmdline, &nb_args, args);
1987
#ifdef DEBUG_COMPLETION
1988
    for(i = 0; i < nb_args; i++) {
1989
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1990
    }
1991
#endif
1992

    
1993
    /* if the line ends with a space, it means we want to complete the
1994
       next arg */
1995
    len = strlen(cmdline);
1996
    if (len > 0 && isspace(cmdline[len - 1])) {
1997
        if (nb_args >= MAX_ARGS)
1998
            return;
1999
        args[nb_args++] = qemu_strdup("");
2000
    }
2001
    if (nb_args <= 1) {
2002
        /* command completion */
2003
        if (nb_args == 0)
2004
            cmdname = "";
2005
        else
2006
            cmdname = args[0];
2007
        completion_index = strlen(cmdname);
2008
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2009
            cmd_completion(cmdname, cmd->name);
2010
        }
2011
    } else {
2012
        /* find the command */
2013
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2014
            if (compare_cmd(args[0], cmd->name))
2015
                goto found;
2016
        }
2017
        return;
2018
    found:
2019
        ptype = cmd->args_type;
2020
        for(i = 0; i < nb_args - 2; i++) {
2021
            if (*ptype != '\0') {
2022
                ptype++;
2023
                while (*ptype == '?')
2024
                    ptype++;
2025
            }
2026
        }
2027
        str = args[nb_args - 1];
2028
        switch(*ptype) {
2029
        case 'F':
2030
            /* file completion */
2031
            completion_index = strlen(str);
2032
            file_completion(str);
2033
            break;
2034
        case 'B':
2035
            /* block device name completion */
2036
            completion_index = strlen(str);
2037
            bdrv_iterate(block_completion_it, (void *)str);
2038
            break;
2039
        case 's':
2040
            /* XXX: more generic ? */
2041
            if (!strcmp(cmd->name, "info")) {
2042
                completion_index = strlen(str);
2043
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2044
                    cmd_completion(str, cmd->name);
2045
                }
2046
            }
2047
            break;
2048
        default:
2049
            break;
2050
        }
2051
    }
2052
    for(i = 0; i < nb_args; i++)
2053
        qemu_free(args[i]);
2054
}
2055

    
2056
static int term_can_read(void *opaque)
2057
{
2058
    return 128;
2059
}
2060

    
2061
static void term_read(void *opaque, const uint8_t *buf, int size)
2062
{
2063
    int i;
2064
    for(i = 0; i < size; i++)
2065
        readline_handle_byte(buf[i]);
2066
}
2067

    
2068
static void monitor_start_input(void);
2069

    
2070
static void monitor_handle_command1(void *opaque, const char *cmdline)
2071
{
2072
    monitor_handle_command(cmdline);
2073
    monitor_start_input();
2074
}
2075

    
2076
static void monitor_start_input(void)
2077
{
2078
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2079
}
2080

    
2081
void monitor_init(CharDriverState *hd, int show_banner)
2082
{
2083
    monitor_hd = hd;
2084
    if (show_banner) {
2085
        term_printf("QEMU %s monitor - type 'help' for more information\n",
2086
                    QEMU_VERSION);
2087
    }
2088
    qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2089
    monitor_start_input();
2090
}
2091

    
2092
/* XXX: use threads ? */
2093
/* modal monitor readline */
2094
static int monitor_readline_started;
2095
static char *monitor_readline_buf;
2096
static int monitor_readline_buf_size;
2097

    
2098
static void monitor_readline_cb(void *opaque, const char *input)
2099
{
2100
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2101
    monitor_readline_started = 0;
2102
}
2103

    
2104
void monitor_readline(const char *prompt, int is_password,
2105
                      char *buf, int buf_size)
2106
{
2107
    if (is_password) {
2108
        qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2109
    }
2110
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2111
    monitor_readline_buf = buf;
2112
    monitor_readline_buf_size = buf_size;
2113
    monitor_readline_started = 1;
2114
    while (monitor_readline_started) {
2115
        main_loop_wait(10);
2116
    }
2117
}