blob: 914938c7f68b0a42657e9ff47407b301f29ca2b3 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * 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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
33#include "console.h"
34#include "block.h"
35#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000036#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000037#include "balloon.h"
bellard81d09122004-07-14 17:21:37 +000038#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000039#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000040#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000041#include "kvm.h"
ths6a5bd302007-12-03 17:05:38 +000042
bellard9dc39cb2004-03-14 21:38:27 +000043//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000044//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000045
bellard9307c4c2004-04-04 12:57:25 +000046/*
47 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000048 *
bellard9307c4c2004-04-04 12:57:25 +000049 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000050 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000051 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000052 * 'i' 32 bit integer
53 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000054 * '/' optional gdb-like print format (like "/10x")
55 *
56 * '?' optional type (for 'F', 's' and 'i')
57 *
58 */
59
bellard9dc39cb2004-03-14 21:38:27 +000060typedef struct term_cmd_t {
61 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000062 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000063 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000064 const char *params;
65 const char *help;
66} term_cmd_t;
67
ths20d8a3e2007-02-18 17:04:49 +000068#define MAX_MON 4
69static CharDriverState *monitor_hd[MAX_MON];
ths86e94de2007-01-05 22:01:59 +000070static int hide_banner;
bellard7e2515e2004-08-01 21:52:19 +000071
blueswir18662d652008-10-02 18:32:44 +000072static const term_cmd_t term_cmds[];
73static const term_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000074
ths60fe76f2007-12-16 03:02:09 +000075static uint8_t term_outbuf[1024];
bellard7e2515e2004-08-01 21:52:19 +000076static int term_outbuf_index;
bellard81d09122004-07-14 17:21:37 +000077
aliguori83ab7952008-08-19 14:44:22 +000078static void monitor_start_input(void);
aliguoric0f4ce72009-03-05 23:01:01 +000079static void monitor_readline(const char *prompt, int is_password,
80 char *buf, int buf_size);
aliguori83ab7952008-08-19 14:44:22 +000081
blueswir1b1d8e522008-10-26 13:43:07 +000082static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000083
bellard9dc39cb2004-03-14 21:38:27 +000084void term_flush(void)
85{
ths20d8a3e2007-02-18 17:04:49 +000086 int i;
bellard7e2515e2004-08-01 21:52:19 +000087 if (term_outbuf_index > 0) {
ths20d8a3e2007-02-18 17:04:49 +000088 for (i = 0; i < MAX_MON; i++)
89 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
90 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
bellard7e2515e2004-08-01 21:52:19 +000091 term_outbuf_index = 0;
92 }
93}
94
95/* flush at every end of line or if the buffer is full */
96void term_puts(const char *str)
97{
ths60fe76f2007-12-16 03:02:09 +000098 char c;
bellard7e2515e2004-08-01 21:52:19 +000099 for(;;) {
100 c = *str++;
101 if (c == '\0')
102 break;
bellard7ba12602006-07-14 20:26:42 +0000103 if (c == '\n')
104 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000105 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000106 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000107 c == '\n')
108 term_flush();
109 }
110}
111
112void term_vprintf(const char *fmt, va_list ap)
113{
114 char buf[4096];
115 vsnprintf(buf, sizeof(buf), fmt, ap);
116 term_puts(buf);
117}
118
119void term_printf(const char *fmt, ...)
120{
121 va_list ap;
122 va_start(ap, fmt);
123 term_vprintf(fmt, ap);
124 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000125}
126
thsfef30742006-12-22 14:11:32 +0000127void term_print_filename(const char *filename)
128{
129 int i;
130
131 for (i = 0; filename[i]; i++) {
132 switch (filename[i]) {
133 case ' ':
134 case '"':
135 case '\\':
136 term_printf("\\%c", filename[i]);
137 break;
138 case '\t':
139 term_printf("\\t");
140 break;
141 case '\r':
142 term_printf("\\r");
143 break;
144 case '\n':
145 term_printf("\\n");
146 break;
147 default:
148 term_printf("%c", filename[i]);
149 break;
150 }
151 }
152}
153
bellard7fe48482004-10-09 18:08:01 +0000154static int monitor_fprintf(FILE *stream, const char *fmt, ...)
155{
156 va_list ap;
157 va_start(ap, fmt);
158 term_vprintf(fmt, ap);
159 va_end(ap);
160 return 0;
161}
162
bellard9dc39cb2004-03-14 21:38:27 +0000163static int compare_cmd(const char *name, const char *list)
164{
165 const char *p, *pstart;
166 int len;
167 len = strlen(name);
168 p = list;
169 for(;;) {
170 pstart = p;
171 p = strchr(p, '|');
172 if (!p)
173 p = pstart + strlen(pstart);
174 if ((p - pstart) == len && !memcmp(pstart, name, len))
175 return 1;
176 if (*p == '\0')
177 break;
178 p++;
179 }
180 return 0;
181}
182
blueswir18662d652008-10-02 18:32:44 +0000183static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000184{
blueswir18662d652008-10-02 18:32:44 +0000185 const term_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000186
187 for(cmd = cmds; cmd->name != NULL; cmd++) {
188 if (!name || !strcmp(name, cmd->name))
189 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
190 }
191}
192
193static void help_cmd(const char *name)
194{
195 if (name && !strcmp(name, "info")) {
196 help_cmd1(info_cmds, "info ", NULL);
197 } else {
198 help_cmd1(term_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000199 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000200 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +0000201 term_printf("Log items (comma separated):\n");
202 term_printf("%-10s %s\n", "none", "remove all logs");
203 for(item = cpu_log_items; item->mask != 0; item++) {
204 term_printf("%-10s %s\n", item->name, item->help);
205 }
206 }
bellard9dc39cb2004-03-14 21:38:27 +0000207 }
208}
209
bellard9307c4c2004-04-04 12:57:25 +0000210static void do_help(const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000211{
bellard9307c4c2004-04-04 12:57:25 +0000212 help_cmd(name);
bellard9dc39cb2004-03-14 21:38:27 +0000213}
214
bellard7954c732006-08-01 15:52:40 +0000215static void do_commit(const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000216{
bellard7954c732006-08-01 15:52:40 +0000217 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000218
bellard7954c732006-08-01 15:52:40 +0000219 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000220 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000221 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000222 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
223 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000224 }
225}
226
bellard9307c4c2004-04-04 12:57:25 +0000227static void do_info(const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000228{
blueswir18662d652008-10-02 18:32:44 +0000229 const term_cmd_t *cmd;
blueswir1a5f1b962008-08-17 20:21:51 +0000230 void (*handler)(void);
bellard9dc39cb2004-03-14 21:38:27 +0000231
bellard9307c4c2004-04-04 12:57:25 +0000232 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000233 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000234 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000235 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000236 goto found;
237 }
238 help:
bellard9307c4c2004-04-04 12:57:25 +0000239 help_cmd("info");
bellard9dc39cb2004-03-14 21:38:27 +0000240 return;
241 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000242 handler = cmd->handler;
243 handler();
bellard9dc39cb2004-03-14 21:38:27 +0000244}
245
bellard9bc9d1c2004-10-10 15:15:51 +0000246static void do_info_version(void)
247{
248 term_printf("%s\n", QEMU_VERSION);
249}
250
thsc35734b2007-03-19 15:17:08 +0000251static void do_info_name(void)
252{
253 if (qemu_name)
254 term_printf("%s\n", qemu_name);
255}
256
aurel32bf4f74c2008-12-18 22:42:34 +0000257#if defined(TARGET_I386)
aliguori16b29ae2008-12-17 23:28:44 +0000258static void do_info_hpet(void)
259{
260 term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
261}
aurel32bf4f74c2008-12-18 22:42:34 +0000262#endif
aliguori16b29ae2008-12-17 23:28:44 +0000263
blueswir1f1f23ad2008-09-18 18:30:20 +0000264static void do_info_uuid(void)
265{
266 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
267 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
268 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
269 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
270 qemu_uuid[15]);
271}
272
bellard9307c4c2004-04-04 12:57:25 +0000273static void do_info_block(void)
bellard9dc39cb2004-03-14 21:38:27 +0000274{
275 bdrv_info();
276}
277
thsa36e69d2007-12-02 05:18:19 +0000278static void do_info_blockstats(void)
279{
280 bdrv_info_stats();
281}
282
bellard6a00d602005-11-21 23:25:50 +0000283/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000284static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000285{
286 CPUState *env;
287
288 for(env = first_cpu; env != NULL; env = env->next_cpu) {
289 if (env->cpu_index == cpu_index) {
290 mon_cpu = env;
291 return 0;
292 }
293 }
294 return -1;
295}
296
pbrook9596ebb2007-11-18 01:44:38 +0000297static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000298{
299 if (!mon_cpu) {
300 mon_set_cpu(0);
301 }
302 return mon_cpu;
303}
304
bellard9307c4c2004-04-04 12:57:25 +0000305static void do_info_registers(void)
306{
bellard6a00d602005-11-21 23:25:50 +0000307 CPUState *env;
308 env = mon_get_cpu();
309 if (!env)
310 return;
bellard9307c4c2004-04-04 12:57:25 +0000311#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000312 cpu_dump_state(env, NULL, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000313 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000314#else
ths5fafdf22007-09-16 21:08:06 +0000315 cpu_dump_state(env, NULL, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000316 0);
bellard9307c4c2004-04-04 12:57:25 +0000317#endif
318}
319
bellard6a00d602005-11-21 23:25:50 +0000320static void do_info_cpus(void)
321{
322 CPUState *env;
323
324 /* just to set the default cpu if not already done */
325 mon_get_cpu();
326
327 for(env = first_cpu; env != NULL; env = env->next_cpu) {
ths5fafdf22007-09-16 21:08:06 +0000328 term_printf("%c CPU #%d:",
bellard6a00d602005-11-21 23:25:50 +0000329 (env == mon_cpu) ? '*' : ' ',
330 env->cpu_index);
331#if defined(TARGET_I386)
332 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000333#elif defined(TARGET_PPC)
334 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000335#elif defined(TARGET_SPARC)
336 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000337#elif defined(TARGET_MIPS)
thsb5dc7732008-06-27 10:02:35 +0000338 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000339#endif
thsead93602007-09-06 00:18:15 +0000340 if (env->halted)
341 term_printf(" (halted)");
bellard6a00d602005-11-21 23:25:50 +0000342 term_printf("\n");
343 }
344}
345
346static void do_cpu_set(int index)
347{
348 if (mon_set_cpu(index) < 0)
349 term_printf("Invalid CPU index\n");
350}
351
bellarde3db7222005-01-26 22:00:47 +0000352static void do_info_jit(void)
353{
354 dump_exec_info(NULL, monitor_fprintf);
355}
356
bellardaa455482004-04-04 13:07:25 +0000357static void do_info_history (void)
358{
359 int i;
bellard7e2515e2004-08-01 21:52:19 +0000360 const char *str;
ths3b46e622007-09-17 08:09:54 +0000361
bellard7e2515e2004-08-01 21:52:19 +0000362 i = 0;
363 for(;;) {
364 str = readline_get_history(i);
365 if (!str)
366 break;
367 term_printf("%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000368 i++;
bellardaa455482004-04-04 13:07:25 +0000369 }
370}
371
j_mayer76a66252007-03-07 08:32:30 +0000372#if defined(TARGET_PPC)
373/* XXX: not implemented in other targets */
374static void do_info_cpu_stats (void)
375{
376 CPUState *env;
377
378 env = mon_get_cpu();
379 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
380}
381#endif
382
bellard9307c4c2004-04-04 12:57:25 +0000383static void do_quit(void)
bellard9dc39cb2004-03-14 21:38:27 +0000384{
385 exit(0);
386}
387
388static int eject_device(BlockDriverState *bs, int force)
389{
390 if (bdrv_is_inserted(bs)) {
391 if (!force) {
392 if (!bdrv_is_removable(bs)) {
393 term_printf("device is not removable\n");
394 return -1;
395 }
396 if (bdrv_is_locked(bs)) {
397 term_printf("device is locked\n");
398 return -1;
399 }
400 }
401 bdrv_close(bs);
402 }
403 return 0;
404}
405
bellard9307c4c2004-04-04 12:57:25 +0000406static void do_eject(int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000407{
408 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000409
bellard9307c4c2004-04-04 12:57:25 +0000410 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000411 if (!bs) {
412 term_printf("device not found\n");
413 return;
414 }
415 eject_device(bs, force);
416}
417
aurel322ecea9b2008-06-18 22:10:01 +0000418static void do_change_block(const char *device, const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000419{
420 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000421 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000422
bellard9307c4c2004-04-04 12:57:25 +0000423 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000424 if (!bs) {
425 term_printf("device not found\n");
426 return;
427 }
aurel322ecea9b2008-06-18 22:10:01 +0000428 if (fmt) {
429 drv = bdrv_find_format(fmt);
430 if (!drv) {
431 term_printf("invalid format %s\n", fmt);
432 return;
433 }
434 }
bellard9dc39cb2004-03-14 21:38:27 +0000435 if (eject_device(bs, 0) < 0)
436 return;
aurel322ecea9b2008-06-18 22:10:01 +0000437 bdrv_open2(bs, filename, 0, drv);
aliguoric0f4ce72009-03-05 23:01:01 +0000438 monitor_read_bdrv_key(bs);
bellard9dc39cb2004-03-14 21:38:27 +0000439}
440
aliguori2569da02008-12-10 15:14:13 +0000441static void do_change_vnc(const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000442{
ths70848512007-08-25 01:37:05 +0000443 if (strcmp(target, "passwd") == 0 ||
444 strcmp(target, "password") == 0) {
445 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000446 if (arg) {
447 strncpy(password, arg, sizeof(password));
448 password[sizeof(password) - 1] = '\0';
449 } else
450 monitor_readline("Password: ", 1, password, sizeof(password));
ths70848512007-08-25 01:37:05 +0000451 if (vnc_display_password(NULL, password) < 0)
452 term_printf("could not set VNC server password\n");
453 } else {
454 if (vnc_display_open(NULL, target) < 0)
455 term_printf("could not start VNC server on %s\n", target);
456 }
thse25a5822007-08-25 01:36:20 +0000457}
458
aliguori2569da02008-12-10 15:14:13 +0000459static void do_change(const char *device, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000460{
461 if (strcmp(device, "vnc") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000462 do_change_vnc(target, arg);
thse25a5822007-08-25 01:36:20 +0000463 } else {
aliguori2569da02008-12-10 15:14:13 +0000464 do_change_block(device, target, arg);
thse25a5822007-08-25 01:36:20 +0000465 }
466}
467
bellard9307c4c2004-04-04 12:57:25 +0000468static void do_screen_dump(const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000469{
pbrook95219892006-04-09 01:06:34 +0000470 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000471}
472
pbrooke735b912007-06-30 13:53:24 +0000473static void do_logfile(const char *filename)
474{
475 cpu_set_log_filename(filename);
476}
477
bellard9307c4c2004-04-04 12:57:25 +0000478static void do_log(const char *items)
bellardf193c792004-03-21 17:06:25 +0000479{
480 int mask;
ths3b46e622007-09-17 08:09:54 +0000481
bellard9307c4c2004-04-04 12:57:25 +0000482 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000483 mask = 0;
484 } else {
bellard9307c4c2004-04-04 12:57:25 +0000485 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000486 if (!mask) {
bellard9307c4c2004-04-04 12:57:25 +0000487 help_cmd("log");
bellardf193c792004-03-21 17:06:25 +0000488 return;
489 }
490 }
491 cpu_set_log(mask);
492}
493
bellard9307c4c2004-04-04 12:57:25 +0000494static void do_stop(void)
bellard8a7ddc32004-03-31 19:00:16 +0000495{
496 vm_stop(EXCP_INTERRUPT);
497}
498
aliguoric0f4ce72009-03-05 23:01:01 +0000499static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
500{
501 int *err = opaque;
502
503 if (bdrv_key_required(bs))
504 *err = monitor_read_bdrv_key(bs);
505 else
506 *err = 0;
507}
508
bellard9307c4c2004-04-04 12:57:25 +0000509static void do_cont(void)
bellard8a7ddc32004-03-31 19:00:16 +0000510{
aliguoric0f4ce72009-03-05 23:01:01 +0000511 int err = 0;
512
513 bdrv_iterate(encrypted_bdrv_it, &err);
514 /* only resume the vm if all keys are set and valid */
515 if (!err)
516 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000517}
518
bellard67b915a2004-03-31 23:37:16 +0000519#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +0000520static void do_gdbserver(const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000521{
pbrookcfc34752007-02-22 01:48:01 +0000522 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000523 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000524 if (gdbserver_start(port) < 0) {
525 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000526 } else {
pbrookcfc34752007-02-22 01:48:01 +0000527 qemu_printf("Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000528 }
529}
bellard67b915a2004-03-31 23:37:16 +0000530#endif
bellard8a7ddc32004-03-31 19:00:16 +0000531
bellard9307c4c2004-04-04 12:57:25 +0000532static void term_printc(int c)
533{
534 term_printf("'");
535 switch(c) {
536 case '\'':
537 term_printf("\\'");
538 break;
539 case '\\':
540 term_printf("\\\\");
541 break;
542 case '\n':
543 term_printf("\\n");
544 break;
545 case '\r':
546 term_printf("\\r");
547 break;
548 default:
549 if (c >= 32 && c <= 126) {
550 term_printf("%c", c);
551 } else {
552 term_printf("\\x%02x", c);
553 }
554 break;
555 }
556 term_printf("'");
557}
558
ths5fafdf22007-09-16 21:08:06 +0000559static void memory_dump(int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000560 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000561{
bellard6a00d602005-11-21 23:25:50 +0000562 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000563 int nb_per_line, l, line_size, i, max_digits, len;
564 uint8_t buf[16];
565 uint64_t v;
566
567 if (format == 'i') {
568 int flags;
569 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000570 env = mon_get_cpu();
571 if (!env && !is_physical)
572 return;
bellard9307c4c2004-04-04 12:57:25 +0000573#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000574 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000575 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000576 } else if (wsize == 4) {
577 flags = 0;
578 } else {
bellard6a15fd12006-04-12 21:07:07 +0000579 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000580 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000581 if (env) {
582#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000583 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000584 (env->segs[R_CS].flags & DESC_L_MASK))
585 flags = 2;
586 else
587#endif
588 if (!(env->segs[R_CS].flags & DESC_B_MASK))
589 flags = 1;
590 }
bellard4c27ba22004-04-25 18:05:08 +0000591 }
592#endif
bellard6a00d602005-11-21 23:25:50 +0000593 monitor_disas(env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000594 return;
595 }
596
597 len = wsize * count;
598 if (wsize == 1)
599 line_size = 8;
600 else
601 line_size = 16;
602 nb_per_line = line_size / wsize;
603 max_digits = 0;
604
605 switch(format) {
606 case 'o':
607 max_digits = (wsize * 8 + 2) / 3;
608 break;
609 default:
610 case 'x':
611 max_digits = (wsize * 8) / 4;
612 break;
613 case 'u':
614 case 'd':
615 max_digits = (wsize * 8 * 10 + 32) / 33;
616 break;
617 case 'c':
618 wsize = 1;
619 break;
620 }
621
622 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000623 if (is_physical)
624 term_printf(TARGET_FMT_plx ":", addr);
625 else
626 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000627 l = len;
628 if (l > line_size)
629 l = line_size;
630 if (is_physical) {
631 cpu_physical_memory_rw(addr, buf, l, 0);
632 } else {
bellard6a00d602005-11-21 23:25:50 +0000633 env = mon_get_cpu();
634 if (!env)
635 break;
aliguoric8f79b62008-08-18 14:00:20 +0000636 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
637 term_printf(" Cannot access memory\n");
638 break;
639 }
bellard9307c4c2004-04-04 12:57:25 +0000640 }
ths5fafdf22007-09-16 21:08:06 +0000641 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000642 while (i < l) {
643 switch(wsize) {
644 default:
645 case 1:
646 v = ldub_raw(buf + i);
647 break;
648 case 2:
649 v = lduw_raw(buf + i);
650 break;
651 case 4:
bellard92a31b12005-02-10 22:00:52 +0000652 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000653 break;
654 case 8:
655 v = ldq_raw(buf + i);
656 break;
657 }
658 term_printf(" ");
659 switch(format) {
660 case 'o':
bellard26a76462006-06-25 18:15:32 +0000661 term_printf("%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000662 break;
663 case 'x':
bellard26a76462006-06-25 18:15:32 +0000664 term_printf("0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000665 break;
666 case 'u':
bellard26a76462006-06-25 18:15:32 +0000667 term_printf("%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000668 break;
669 case 'd':
bellard26a76462006-06-25 18:15:32 +0000670 term_printf("%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000671 break;
672 case 'c':
673 term_printc(v);
674 break;
675 }
676 i += wsize;
677 }
678 term_printf("\n");
679 addr += l;
680 len -= l;
681 }
682}
683
bellard92a31b12005-02-10 22:00:52 +0000684#if TARGET_LONG_BITS == 64
685#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
686#else
687#define GET_TLONG(h, l) (l)
688#endif
689
ths5fafdf22007-09-16 21:08:06 +0000690static void do_memory_dump(int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000691 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000692{
bellard92a31b12005-02-10 22:00:52 +0000693 target_long addr = GET_TLONG(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000694 memory_dump(count, format, size, addr, 0);
695}
696
blueswir17743e582007-09-24 18:39:04 +0000697#if TARGET_PHYS_ADDR_BITS > 32
698#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
699#else
700#define GET_TPHYSADDR(h, l) (l)
701#endif
702
bellard92a31b12005-02-10 22:00:52 +0000703static void do_physical_memory_dump(int count, int format, int size,
704 uint32_t addrh, uint32_t addrl)
705
bellard9307c4c2004-04-04 12:57:25 +0000706{
blueswir17743e582007-09-24 18:39:04 +0000707 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000708 memory_dump(count, format, size, addr, 1);
709}
710
bellard92a31b12005-02-10 22:00:52 +0000711static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000712{
blueswir17743e582007-09-24 18:39:04 +0000713 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
714#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000715 switch(format) {
716 case 'o':
717 term_printf("%#o", val);
718 break;
719 case 'x':
720 term_printf("%#x", val);
721 break;
722 case 'u':
723 term_printf("%u", val);
724 break;
725 default:
726 case 'd':
727 term_printf("%d", val);
728 break;
729 case 'c':
730 term_printc(val);
731 break;
732 }
bellard92a31b12005-02-10 22:00:52 +0000733#else
734 switch(format) {
735 case 'o':
bellard26a76462006-06-25 18:15:32 +0000736 term_printf("%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000737 break;
738 case 'x':
bellard26a76462006-06-25 18:15:32 +0000739 term_printf("%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000740 break;
741 case 'u':
bellard26a76462006-06-25 18:15:32 +0000742 term_printf("%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000743 break;
744 default:
745 case 'd':
bellard26a76462006-06-25 18:15:32 +0000746 term_printf("%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000747 break;
748 case 'c':
749 term_printc(val);
750 break;
751 }
752#endif
bellard9307c4c2004-04-04 12:57:25 +0000753 term_printf("\n");
754}
755
ths5fafdf22007-09-16 21:08:06 +0000756static void do_memory_save(unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000757 uint32_t size, const char *filename)
758{
759 FILE *f;
760 target_long addr = GET_TLONG(valh, vall);
761 uint32_t l;
762 CPUState *env;
763 uint8_t buf[1024];
764
765 env = mon_get_cpu();
766 if (!env)
767 return;
768
769 f = fopen(filename, "wb");
770 if (!f) {
771 term_printf("could not open '%s'\n", filename);
772 return;
773 }
774 while (size != 0) {
775 l = sizeof(buf);
776 if (l > size)
777 l = size;
778 cpu_memory_rw_debug(env, addr, buf, l, 0);
779 fwrite(buf, 1, l, f);
780 addr += l;
781 size -= l;
782 }
783 fclose(f);
784}
785
aurel32a8bdf7a2008-04-11 21:36:14 +0000786static void do_physical_memory_save(unsigned int valh, unsigned int vall,
787 uint32_t size, const char *filename)
788{
789 FILE *f;
790 uint32_t l;
791 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000792 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000793
794 f = fopen(filename, "wb");
795 if (!f) {
796 term_printf("could not open '%s'\n", filename);
797 return;
798 }
799 while (size != 0) {
800 l = sizeof(buf);
801 if (l > size)
802 l = size;
803 cpu_physical_memory_rw(addr, buf, l, 0);
804 fwrite(buf, 1, l, f);
805 fflush(f);
806 addr += l;
807 size -= l;
808 }
809 fclose(f);
810}
811
bellarde4cf1ad2005-06-04 20:15:57 +0000812static void do_sum(uint32_t start, uint32_t size)
813{
814 uint32_t addr;
815 uint8_t buf[1];
816 uint16_t sum;
817
818 sum = 0;
819 for(addr = start; addr < (start + size); addr++) {
820 cpu_physical_memory_rw(addr, buf, 1, 0);
821 /* BSD sum algorithm ('sum' Unix command) */
822 sum = (sum >> 1) | (sum << 15);
823 sum += buf[0];
824 }
825 term_printf("%05d\n", sum);
826}
827
bellarda3a91a32004-06-04 11:06:21 +0000828typedef struct {
829 int keycode;
830 const char *name;
831} KeyDef;
832
833static const KeyDef key_defs[] = {
834 { 0x2a, "shift" },
835 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000836
bellarda3a91a32004-06-04 11:06:21 +0000837 { 0x38, "alt" },
838 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000839 { 0x64, "altgr" },
840 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000841 { 0x1d, "ctrl" },
842 { 0x9d, "ctrl_r" },
843
844 { 0xdd, "menu" },
845
846 { 0x01, "esc" },
847
848 { 0x02, "1" },
849 { 0x03, "2" },
850 { 0x04, "3" },
851 { 0x05, "4" },
852 { 0x06, "5" },
853 { 0x07, "6" },
854 { 0x08, "7" },
855 { 0x09, "8" },
856 { 0x0a, "9" },
857 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000858 { 0x0c, "minus" },
859 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000860 { 0x0e, "backspace" },
861
862 { 0x0f, "tab" },
863 { 0x10, "q" },
864 { 0x11, "w" },
865 { 0x12, "e" },
866 { 0x13, "r" },
867 { 0x14, "t" },
868 { 0x15, "y" },
869 { 0x16, "u" },
870 { 0x17, "i" },
871 { 0x18, "o" },
872 { 0x19, "p" },
873
874 { 0x1c, "ret" },
875
876 { 0x1e, "a" },
877 { 0x1f, "s" },
878 { 0x20, "d" },
879 { 0x21, "f" },
880 { 0x22, "g" },
881 { 0x23, "h" },
882 { 0x24, "j" },
883 { 0x25, "k" },
884 { 0x26, "l" },
885
886 { 0x2c, "z" },
887 { 0x2d, "x" },
888 { 0x2e, "c" },
889 { 0x2f, "v" },
890 { 0x30, "b" },
891 { 0x31, "n" },
892 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000893 { 0x33, "comma" },
894 { 0x34, "dot" },
895 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000896
balrog4d3b6f62008-02-10 16:33:14 +0000897 { 0x37, "asterisk" },
898
bellarda3a91a32004-06-04 11:06:21 +0000899 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000900 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000901 { 0x3b, "f1" },
902 { 0x3c, "f2" },
903 { 0x3d, "f3" },
904 { 0x3e, "f4" },
905 { 0x3f, "f5" },
906 { 0x40, "f6" },
907 { 0x41, "f7" },
908 { 0x42, "f8" },
909 { 0x43, "f9" },
910 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000911 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000912 { 0x46, "scroll_lock" },
913
bellard64866c32006-05-07 18:03:31 +0000914 { 0xb5, "kp_divide" },
915 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000916 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000917 { 0x4e, "kp_add" },
918 { 0x9c, "kp_enter" },
919 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000920 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000921
922 { 0x52, "kp_0" },
923 { 0x4f, "kp_1" },
924 { 0x50, "kp_2" },
925 { 0x51, "kp_3" },
926 { 0x4b, "kp_4" },
927 { 0x4c, "kp_5" },
928 { 0x4d, "kp_6" },
929 { 0x47, "kp_7" },
930 { 0x48, "kp_8" },
931 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000932
bellarda3a91a32004-06-04 11:06:21 +0000933 { 0x56, "<" },
934
935 { 0x57, "f11" },
936 { 0x58, "f12" },
937
938 { 0xb7, "print" },
939
940 { 0xc7, "home" },
941 { 0xc9, "pgup" },
942 { 0xd1, "pgdn" },
943 { 0xcf, "end" },
944
945 { 0xcb, "left" },
946 { 0xc8, "up" },
947 { 0xd0, "down" },
948 { 0xcd, "right" },
949
950 { 0xd2, "insert" },
951 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000952#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
953 { 0xf0, "stop" },
954 { 0xf1, "again" },
955 { 0xf2, "props" },
956 { 0xf3, "undo" },
957 { 0xf4, "front" },
958 { 0xf5, "copy" },
959 { 0xf6, "open" },
960 { 0xf7, "paste" },
961 { 0xf8, "find" },
962 { 0xf9, "cut" },
963 { 0xfa, "lf" },
964 { 0xfb, "help" },
965 { 0xfc, "meta_l" },
966 { 0xfd, "meta_r" },
967 { 0xfe, "compose" },
968#endif
bellarda3a91a32004-06-04 11:06:21 +0000969 { 0, NULL },
970};
971
972static int get_keycode(const char *key)
973{
974 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +0000975 char *endp;
976 int ret;
bellarda3a91a32004-06-04 11:06:21 +0000977
978 for(p = key_defs; p->name != NULL; p++) {
979 if (!strcmp(key, p->name))
980 return p->keycode;
981 }
bellard64866c32006-05-07 18:03:31 +0000982 if (strstart(key, "0x", NULL)) {
983 ret = strtoul(key, &endp, 0);
984 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
985 return ret;
986 }
bellarda3a91a32004-06-04 11:06:21 +0000987 return -1;
988}
989
balrogc8256f92008-06-08 22:45:01 +0000990#define MAX_KEYCODES 16
991static uint8_t keycodes[MAX_KEYCODES];
992static int nb_pending_keycodes;
993static QEMUTimer *key_timer;
994
995static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +0000996{
balrogc8256f92008-06-08 22:45:01 +0000997 int keycode;
998
999 while (nb_pending_keycodes > 0) {
1000 nb_pending_keycodes--;
1001 keycode = keycodes[nb_pending_keycodes];
1002 if (keycode & 0x80)
1003 kbd_put_keycode(0xe0);
1004 kbd_put_keycode(keycode | 0x80);
1005 }
1006}
1007
1008static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1009{
balrog3401c0d2008-06-04 10:05:59 +00001010 char keyname_buf[16];
1011 char *separator;
1012 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001013
balrogc8256f92008-06-08 22:45:01 +00001014 if (nb_pending_keycodes > 0) {
1015 qemu_del_timer(key_timer);
1016 release_keys(NULL);
1017 }
1018 if (!has_hold_time)
1019 hold_time = 100;
1020 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001021 while (1) {
1022 separator = strchr(string, '-');
1023 keyname_len = separator ? separator - string : strlen(string);
1024 if (keyname_len > 0) {
1025 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1026 if (keyname_len > sizeof(keyname_buf) - 1) {
1027 term_printf("invalid key: '%s...'\n", keyname_buf);
1028 return;
bellarda3a91a32004-06-04 11:06:21 +00001029 }
balrogc8256f92008-06-08 22:45:01 +00001030 if (i == MAX_KEYCODES) {
balrog3401c0d2008-06-04 10:05:59 +00001031 term_printf("too many keys\n");
1032 return;
1033 }
1034 keyname_buf[keyname_len] = 0;
1035 keycode = get_keycode(keyname_buf);
1036 if (keycode < 0) {
1037 term_printf("unknown key: '%s'\n", keyname_buf);
1038 return;
1039 }
balrogc8256f92008-06-08 22:45:01 +00001040 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001041 }
balrog3401c0d2008-06-04 10:05:59 +00001042 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001043 break;
balrog3401c0d2008-06-04 10:05:59 +00001044 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001045 }
balrogc8256f92008-06-08 22:45:01 +00001046 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001047 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001048 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001049 keycode = keycodes[i];
1050 if (keycode & 0x80)
1051 kbd_put_keycode(0xe0);
1052 kbd_put_keycode(keycode & 0x7f);
1053 }
balrogc8256f92008-06-08 22:45:01 +00001054 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001055 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1056 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001057}
1058
bellard13224a82006-07-14 22:03:35 +00001059static int mouse_button_state;
1060
ths5fafdf22007-09-16 21:08:06 +00001061static void do_mouse_move(const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001062 const char *dz_str)
1063{
1064 int dx, dy, dz;
1065 dx = strtol(dx_str, NULL, 0);
1066 dy = strtol(dy_str, NULL, 0);
1067 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001068 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001069 dz = strtol(dz_str, NULL, 0);
1070 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1071}
1072
1073static void do_mouse_button(int button_state)
1074{
1075 mouse_button_state = button_state;
1076 kbd_mouse_event(0, 0, 0, mouse_button_state);
1077}
1078
bellard34405572004-06-08 00:55:58 +00001079static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1080{
1081 uint32_t val;
1082 int suffix;
1083
1084 if (has_index) {
1085 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1086 addr++;
1087 }
1088 addr &= 0xffff;
1089
1090 switch(size) {
1091 default:
1092 case 1:
1093 val = cpu_inb(NULL, addr);
1094 suffix = 'b';
1095 break;
1096 case 2:
1097 val = cpu_inw(NULL, addr);
1098 suffix = 'w';
1099 break;
1100 case 4:
1101 val = cpu_inl(NULL, addr);
1102 suffix = 'l';
1103 break;
1104 }
1105 term_printf("port%c[0x%04x] = %#0*x\n",
1106 suffix, addr, size * 2, val);
1107}
bellarda3a91a32004-06-04 11:06:21 +00001108
blueswir13b4366d2008-06-20 16:25:06 +00001109/* boot_set handler */
1110static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1111static void *boot_opaque;
1112
1113void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1114{
1115 qemu_boot_set_handler = func;
1116 boot_opaque = opaque;
1117}
1118
aurel320ecdffb2008-05-04 20:11:34 +00001119static void do_boot_set(const char *bootdevice)
1120{
1121 int res;
1122
1123 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001124 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001125 if (res == 0)
1126 term_printf("boot device list now set to %s\n", bootdevice);
1127 else
1128 term_printf("setting boot device list failed with error %i\n", res);
1129 } else {
1130 term_printf("no function defined to set boot device list for this architecture\n");
1131 }
1132}
1133
bellarde4f90822004-06-20 12:35:44 +00001134static void do_system_reset(void)
1135{
1136 qemu_system_reset_request();
1137}
1138
bellard34751872005-07-02 14:31:34 +00001139static void do_system_powerdown(void)
1140{
1141 qemu_system_powerdown_request();
1142}
1143
bellardb86bda52004-09-18 19:32:46 +00001144#if defined(TARGET_I386)
1145static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1146{
ths5fafdf22007-09-16 21:08:06 +00001147 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
bellardb86bda52004-09-18 19:32:46 +00001148 addr,
1149 pte & mask,
1150 pte & PG_GLOBAL_MASK ? 'G' : '-',
1151 pte & PG_PSE_MASK ? 'P' : '-',
1152 pte & PG_DIRTY_MASK ? 'D' : '-',
1153 pte & PG_ACCESSED_MASK ? 'A' : '-',
1154 pte & PG_PCD_MASK ? 'C' : '-',
1155 pte & PG_PWT_MASK ? 'T' : '-',
1156 pte & PG_USER_MASK ? 'U' : '-',
1157 pte & PG_RW_MASK ? 'W' : '-');
1158}
1159
1160static void tlb_info(void)
1161{
bellard6a00d602005-11-21 23:25:50 +00001162 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001163 int l1, l2;
1164 uint32_t pgd, pde, pte;
1165
bellard6a00d602005-11-21 23:25:50 +00001166 env = mon_get_cpu();
1167 if (!env)
1168 return;
1169
bellardb86bda52004-09-18 19:32:46 +00001170 if (!(env->cr[0] & CR0_PG_MASK)) {
1171 term_printf("PG disabled\n");
1172 return;
1173 }
1174 pgd = env->cr[3] & ~0xfff;
1175 for(l1 = 0; l1 < 1024; l1++) {
1176 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1177 pde = le32_to_cpu(pde);
1178 if (pde & PG_PRESENT_MASK) {
1179 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1180 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1181 } else {
1182 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001183 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001184 (uint8_t *)&pte, 4);
1185 pte = le32_to_cpu(pte);
1186 if (pte & PG_PRESENT_MASK) {
ths5fafdf22007-09-16 21:08:06 +00001187 print_pte((l1 << 22) + (l2 << 12),
1188 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001189 ~0xfff);
1190 }
1191 }
1192 }
1193 }
1194 }
1195}
1196
ths5fafdf22007-09-16 21:08:06 +00001197static void mem_print(uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001198 uint32_t end, int prot)
1199{
bellard9746b152004-11-11 18:30:24 +00001200 int prot1;
1201 prot1 = *plast_prot;
1202 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001203 if (*pstart != -1) {
1204 term_printf("%08x-%08x %08x %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001205 *pstart, end, end - *pstart,
bellard9746b152004-11-11 18:30:24 +00001206 prot1 & PG_USER_MASK ? 'u' : '-',
bellardb86bda52004-09-18 19:32:46 +00001207 'r',
bellard9746b152004-11-11 18:30:24 +00001208 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001209 }
1210 if (prot != 0)
1211 *pstart = end;
1212 else
1213 *pstart = -1;
1214 *plast_prot = prot;
1215 }
1216}
1217
1218static void mem_info(void)
1219{
bellard6a00d602005-11-21 23:25:50 +00001220 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001221 int l1, l2, prot, last_prot;
1222 uint32_t pgd, pde, pte, start, end;
1223
bellard6a00d602005-11-21 23:25:50 +00001224 env = mon_get_cpu();
1225 if (!env)
1226 return;
1227
bellardb86bda52004-09-18 19:32:46 +00001228 if (!(env->cr[0] & CR0_PG_MASK)) {
1229 term_printf("PG disabled\n");
1230 return;
1231 }
1232 pgd = env->cr[3] & ~0xfff;
1233 last_prot = 0;
1234 start = -1;
1235 for(l1 = 0; l1 < 1024; l1++) {
1236 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1237 pde = le32_to_cpu(pde);
1238 end = l1 << 22;
1239 if (pde & PG_PRESENT_MASK) {
1240 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1241 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1242 mem_print(&start, &last_prot, end, prot);
1243 } else {
1244 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001245 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001246 (uint8_t *)&pte, 4);
1247 pte = le32_to_cpu(pte);
1248 end = (l1 << 22) + (l2 << 12);
1249 if (pte & PG_PRESENT_MASK) {
1250 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1251 } else {
1252 prot = 0;
1253 }
1254 mem_print(&start, &last_prot, end, prot);
1255 }
1256 }
1257 } else {
1258 prot = 0;
1259 mem_print(&start, &last_prot, end, prot);
1260 }
1261 }
1262}
1263#endif
1264
aurel327c664e22009-03-03 06:12:22 +00001265#if defined(TARGET_SH4)
1266
1267static void print_tlb(int idx, tlb_t *tlb)
1268{
1269 term_printf(" tlb%i:\t"
1270 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1271 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1272 "dirty=%hhu writethrough=%hhu\n",
1273 idx,
1274 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1275 tlb->v, tlb->sh, tlb->c, tlb->pr,
1276 tlb->d, tlb->wt);
1277}
1278
1279static void tlb_info(void)
1280{
1281 CPUState *env = mon_get_cpu();
1282 int i;
1283
1284 term_printf ("ITLB:\n");
1285 for (i = 0 ; i < ITLB_SIZE ; i++)
1286 print_tlb (i, &env->itlb[i]);
1287 term_printf ("UTLB:\n");
1288 for (i = 0 ; i < UTLB_SIZE ; i++)
1289 print_tlb (i, &env->utlb[i]);
1290}
1291
1292#endif
1293
bellard0f4c6412005-07-24 18:10:56 +00001294static void do_info_kqemu(void)
1295{
1296#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001297 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001298 int val;
1299 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001300 env = mon_get_cpu();
1301 if (!env) {
1302 term_printf("No cpu initialized yet");
1303 return;
1304 }
1305 val = env->kqemu_enabled;
bellard5f1ce942006-02-08 22:40:15 +00001306 term_printf("kqemu support: ");
1307 switch(val) {
1308 default:
1309 case 0:
1310 term_printf("disabled\n");
1311 break;
1312 case 1:
1313 term_printf("enabled for user code\n");
1314 break;
1315 case 2:
1316 term_printf("enabled for user and kernel code\n");
1317 break;
1318 }
bellard0f4c6412005-07-24 18:10:56 +00001319#else
bellard5f1ce942006-02-08 22:40:15 +00001320 term_printf("kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001321#endif
ths5fafdf22007-09-16 21:08:06 +00001322}
bellard0f4c6412005-07-24 18:10:56 +00001323
aliguori7ba1e612008-11-05 16:04:33 +00001324static void do_info_kvm(void)
1325{
1326#ifdef CONFIG_KVM
1327 term_printf("kvm support: ");
1328 if (kvm_enabled())
1329 term_printf("enabled\n");
1330 else
1331 term_printf("disabled\n");
1332#else
1333 term_printf("kvm support: not compiled\n");
1334#endif
1335}
1336
bellard5f1ce942006-02-08 22:40:15 +00001337#ifdef CONFIG_PROFILER
1338
1339int64_t kqemu_time;
1340int64_t qemu_time;
1341int64_t kqemu_exec_count;
1342int64_t dev_time;
1343int64_t kqemu_ret_int_count;
1344int64_t kqemu_ret_excp_count;
1345int64_t kqemu_ret_intr_count;
1346
1347static void do_info_profile(void)
1348{
1349 int64_t total;
1350 total = qemu_time;
1351 if (total == 0)
1352 total = 1;
bellard26a76462006-06-25 18:15:32 +00001353 term_printf("async time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001354 dev_time, dev_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001355 term_printf("qemu time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001356 qemu_time, qemu_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001357 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
bellard5f1ce942006-02-08 22:40:15 +00001358 kqemu_time, kqemu_time / (double)ticks_per_sec,
1359 kqemu_time / (double)total * 100.0,
1360 kqemu_exec_count,
1361 kqemu_ret_int_count,
1362 kqemu_ret_excp_count,
1363 kqemu_ret_intr_count);
1364 qemu_time = 0;
1365 kqemu_time = 0;
1366 kqemu_exec_count = 0;
1367 dev_time = 0;
1368 kqemu_ret_int_count = 0;
1369 kqemu_ret_excp_count = 0;
1370 kqemu_ret_intr_count = 0;
1371#ifdef USE_KQEMU
1372 kqemu_record_dump();
1373#endif
1374}
1375#else
1376static void do_info_profile(void)
1377{
1378 term_printf("Internal profiler not compiled\n");
1379}
1380#endif
1381
bellardec36b692006-07-16 18:57:03 +00001382/* Capture support */
1383static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1384
1385static void do_info_capture (void)
1386{
1387 int i;
1388 CaptureState *s;
1389
1390 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1391 term_printf ("[%d]: ", i);
1392 s->ops.info (s->opaque);
1393 }
1394}
1395
1396static void do_stop_capture (int n)
1397{
1398 int i;
1399 CaptureState *s;
1400
1401 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1402 if (i == n) {
1403 s->ops.destroy (s->opaque);
1404 LIST_REMOVE (s, entries);
1405 qemu_free (s);
1406 return;
1407 }
1408 }
1409}
1410
1411#ifdef HAS_AUDIO
bellardec36b692006-07-16 18:57:03 +00001412static void do_wav_capture (const char *path,
1413 int has_freq, int freq,
1414 int has_bits, int bits,
1415 int has_channels, int nchannels)
1416{
1417 CaptureState *s;
1418
1419 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001420
1421 freq = has_freq ? freq : 44100;
1422 bits = has_bits ? bits : 16;
1423 nchannels = has_channels ? nchannels : 2;
1424
1425 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1426 term_printf ("Faied to add wave capture\n");
1427 qemu_free (s);
1428 }
1429 LIST_INSERT_HEAD (&capture_head, s, entries);
1430}
1431#endif
1432
aurel32dc1c0b72008-04-27 23:52:12 +00001433#if defined(TARGET_I386)
1434static void do_inject_nmi(int cpu_index)
1435{
1436 CPUState *env;
1437
1438 for (env = first_cpu; env != NULL; env = env->next_cpu)
1439 if (env->cpu_index == cpu_index) {
1440 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1441 break;
1442 }
1443}
1444#endif
1445
aurel326f9c5ee2008-12-18 22:43:56 +00001446static void do_info_status(void)
1447{
1448 if (vm_running)
1449 term_printf("VM status: running\n");
1450 else
1451 term_printf("VM status: paused\n");
1452}
1453
1454
aliguoridf751fa2008-12-04 20:19:35 +00001455static void do_balloon(int value)
1456{
1457 ram_addr_t target = value;
1458 qemu_balloon(target << 20);
1459}
1460
1461static void do_info_balloon(void)
1462{
1463 ram_addr_t actual;
1464
1465 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001466 if (kvm_enabled() && !kvm_has_sync_mmu())
1467 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1468 else if (actual == 0)
aliguoridf751fa2008-12-04 20:19:35 +00001469 term_printf("Ballooning not activated in VM\n");
1470 else
1471 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1472}
1473
blueswir1d2c639d2009-01-24 18:19:25 +00001474/* Please update qemu-doc.texi when adding or changing commands */
blueswir18662d652008-10-02 18:32:44 +00001475static const term_cmd_t term_cmds[] = {
ths5fafdf22007-09-16 21:08:06 +00001476 { "help|?", "s?", do_help,
bellard9dc39cb2004-03-14 21:38:27 +00001477 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001478 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001479 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001480 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001481 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001482 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001483 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001484 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001485 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001486 { "change", "BFs?", do_change,
1487 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001488 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001489 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001490 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001491 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001492 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001493 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001494 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001495 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001496 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001497 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001498 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001499 "tag|id", "delete a VM snapshot from its tag or id" },
1500 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001501 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001502 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001503 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001504#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001505 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001506 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001507#endif
ths5fafdf22007-09-16 21:08:06 +00001508 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001509 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001510 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001511 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001512 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001513 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001514 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001515 "/fmt addr", "I/O port read" },
1516
balrogc8256f92008-06-08 22:45:01 +00001517 { "sendkey", "si?", do_sendkey,
1518 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
ths5fafdf22007-09-16 21:08:06 +00001519 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001520 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001521 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001522 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001523 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001524 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001525 { "usb_add", "s", do_usb_add,
1526 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1527 { "usb_del", "s", do_usb_del,
1528 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001529 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001530 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001531 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001532 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001533 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001534 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001535 { "mouse_set", "i", do_mouse_set,
1536 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001537#ifdef HAS_AUDIO
1538 { "wavcapture", "si?i?i?", do_wav_capture,
1539 "path [frequency bits channels]",
1540 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1541#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001542 { "stopcapture", "i", do_stop_capture,
1543 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001544 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001545 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001546 { "pmemsave", "lis", do_physical_memory_save,
1547 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001548 { "boot_set", "s", do_boot_set,
1549 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001550#if defined(TARGET_I386)
1551 { "nmi", "i", do_inject_nmi,
1552 "cpu", "inject an NMI on the given CPU", },
1553#endif
aliguori5bb79102008-10-13 03:12:02 +00001554 { "migrate", "-ds", do_migrate,
1555 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1556 { "migrate_cancel", "", do_migrate_cancel,
1557 "", "cancel the current VM migration" },
1558 { "migrate_set_speed", "s", do_migrate_set_speed,
1559 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001560#if defined(TARGET_I386)
1561 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1562 "[file=file][,if=type][,bus=n]\n"
1563 "[,unit=m][,media=d][index=i]\n"
1564 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1565 "[snapshot=on|off][,cache=on|off]",
1566 "add drive to PCI storage controller" },
1567 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1568 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1569 { "host_net_add", "ss", net_host_device_add,
1570 "[tap,user,socket,vde] options", "add host VLAN client" },
1571 { "host_net_remove", "is", net_host_device_remove,
1572 "vlan_id name", "remove host VLAN client" },
1573#endif
aliguoridf751fa2008-12-04 20:19:35 +00001574 { "balloon", "i", do_balloon,
1575 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001576 { "set_link", "ss", do_set_link,
1577 "name [up|down]", "change the link status of a network adapter" },
ths5fafdf22007-09-16 21:08:06 +00001578 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001579};
1580
blueswir1d2c639d2009-01-24 18:19:25 +00001581/* Please update qemu-doc.texi when adding or changing commands */
blueswir18662d652008-10-02 18:32:44 +00001582static const term_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001583 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001584 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001585 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001586 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001587 { "chardev", "", qemu_chr_info,
1588 "", "show the character devices" },
bellard9307c4c2004-04-04 12:57:25 +00001589 { "block", "", do_info_block,
bellard9dc39cb2004-03-14 21:38:27 +00001590 "", "show the block devices" },
thsa36e69d2007-12-02 05:18:19 +00001591 { "blockstats", "", do_info_blockstats,
1592 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001593 { "registers", "", do_info_registers,
1594 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001595 { "cpus", "", do_info_cpus,
1596 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001597 { "history", "", do_info_history,
1598 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001599 { "irq", "", irq_info,
1600 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001601 { "pic", "", pic_info,
1602 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001603 { "pci", "", pci_info,
1604 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001605#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001606 { "tlb", "", tlb_info,
1607 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001608#endif
1609#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001610 { "mem", "", mem_info,
1611 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001612 { "hpet", "", do_info_hpet,
1613 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001614#endif
bellarde3db7222005-01-26 22:00:47 +00001615 { "jit", "", do_info_jit,
1616 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001617 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001618 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001619 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001620 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001621 { "usb", "", usb_info,
1622 "", "show guest USB devices", },
1623 { "usbhost", "", usb_host_info,
1624 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001625 { "profile", "", do_info_profile,
1626 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001627 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001628 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001629 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001630 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001631 { "status", "", do_info_status,
1632 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001633 { "pcmcia", "", pcmcia_info,
1634 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001635 { "mice", "", do_info_mice,
1636 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001637 { "vnc", "", do_info_vnc,
1638 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001639 { "name", "", do_info_name,
1640 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001641 { "uuid", "", do_info_uuid,
1642 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001643#if defined(TARGET_PPC)
1644 { "cpustats", "", do_info_cpu_stats,
1645 "", "show CPU statistics", },
1646#endif
blueswir131a60e22007-10-26 18:42:59 +00001647#if defined(CONFIG_SLIRP)
1648 { "slirp", "", do_info_slirp,
1649 "", "show SLIRP statistics", },
1650#endif
aliguori5bb79102008-10-13 03:12:02 +00001651 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001652 { "balloon", "", do_info_balloon,
1653 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001654 { NULL, NULL, },
1655};
1656
bellard9307c4c2004-04-04 12:57:25 +00001657/*******************************************************************/
1658
1659static const char *pch;
1660static jmp_buf expr_env;
1661
bellard92a31b12005-02-10 22:00:52 +00001662#define MD_TLONG 0
1663#define MD_I32 1
1664
bellard9307c4c2004-04-04 12:57:25 +00001665typedef struct MonitorDef {
1666 const char *name;
1667 int offset;
blueswir18662d652008-10-02 18:32:44 +00001668 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001669 int type;
bellard9307c4c2004-04-04 12:57:25 +00001670} MonitorDef;
1671
bellard57206fd2004-04-25 18:54:52 +00001672#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001673static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001674{
bellard6a00d602005-11-21 23:25:50 +00001675 CPUState *env = mon_get_cpu();
1676 if (!env)
1677 return 0;
1678 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001679}
1680#endif
1681
bellarda541f292004-04-12 20:39:29 +00001682#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001683static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001684{
bellard6a00d602005-11-21 23:25:50 +00001685 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001686 unsigned int u;
1687 int i;
1688
bellard6a00d602005-11-21 23:25:50 +00001689 if (!env)
1690 return 0;
1691
bellarda541f292004-04-12 20:39:29 +00001692 u = 0;
1693 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001694 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001695
1696 return u;
1697}
1698
blueswir18662d652008-10-02 18:32:44 +00001699static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001700{
bellard6a00d602005-11-21 23:25:50 +00001701 CPUState *env = mon_get_cpu();
1702 if (!env)
1703 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001704 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001705}
1706
blueswir18662d652008-10-02 18:32:44 +00001707static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001708{
bellard6a00d602005-11-21 23:25:50 +00001709 CPUState *env = mon_get_cpu();
1710 if (!env)
1711 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001712 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001713}
bellard9fddaa02004-05-21 12:59:32 +00001714
blueswir18662d652008-10-02 18:32:44 +00001715static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001716{
bellard6a00d602005-11-21 23:25:50 +00001717 CPUState *env = mon_get_cpu();
1718 if (!env)
1719 return 0;
1720 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001721}
1722
blueswir18662d652008-10-02 18:32:44 +00001723static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001724{
bellard6a00d602005-11-21 23:25:50 +00001725 CPUState *env = mon_get_cpu();
1726 if (!env)
1727 return 0;
1728 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001729}
1730
blueswir18662d652008-10-02 18:32:44 +00001731static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001732{
bellard6a00d602005-11-21 23:25:50 +00001733 CPUState *env = mon_get_cpu();
1734 if (!env)
1735 return 0;
1736 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001737}
bellarda541f292004-04-12 20:39:29 +00001738#endif
1739
bellarde95c8d52004-09-30 22:22:08 +00001740#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001741#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001742static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001743{
bellard6a00d602005-11-21 23:25:50 +00001744 CPUState *env = mon_get_cpu();
1745 if (!env)
1746 return 0;
1747 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001748}
bellard7b936c02005-10-30 17:05:13 +00001749#endif
bellarde95c8d52004-09-30 22:22:08 +00001750
blueswir18662d652008-10-02 18:32:44 +00001751static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001752{
bellard6a00d602005-11-21 23:25:50 +00001753 CPUState *env = mon_get_cpu();
1754 if (!env)
1755 return 0;
1756 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001757}
1758#endif
1759
blueswir18662d652008-10-02 18:32:44 +00001760static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001761#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001762
1763#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001764 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001765 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001766 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001767
bellard9307c4c2004-04-04 12:57:25 +00001768 { "eax", offsetof(CPUState, regs[0]) },
1769 { "ecx", offsetof(CPUState, regs[1]) },
1770 { "edx", offsetof(CPUState, regs[2]) },
1771 { "ebx", offsetof(CPUState, regs[3]) },
1772 { "esp|sp", offsetof(CPUState, regs[4]) },
1773 { "ebp|fp", offsetof(CPUState, regs[5]) },
1774 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001775 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001776#ifdef TARGET_X86_64
1777 { "r8", offsetof(CPUState, regs[8]) },
1778 { "r9", offsetof(CPUState, regs[9]) },
1779 { "r10", offsetof(CPUState, regs[10]) },
1780 { "r11", offsetof(CPUState, regs[11]) },
1781 { "r12", offsetof(CPUState, regs[12]) },
1782 { "r13", offsetof(CPUState, regs[13]) },
1783 { "r14", offsetof(CPUState, regs[14]) },
1784 { "r15", offsetof(CPUState, regs[15]) },
1785#endif
bellard9307c4c2004-04-04 12:57:25 +00001786 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001787 { "eip", offsetof(CPUState, eip) },
1788 SEG("cs", R_CS)
1789 SEG("ds", R_DS)
1790 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001791 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001792 SEG("fs", R_FS)
1793 SEG("gs", R_GS)
1794 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001795#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001796 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001797 { "r0", offsetof(CPUState, gpr[0]) },
1798 { "r1", offsetof(CPUState, gpr[1]) },
1799 { "r2", offsetof(CPUState, gpr[2]) },
1800 { "r3", offsetof(CPUState, gpr[3]) },
1801 { "r4", offsetof(CPUState, gpr[4]) },
1802 { "r5", offsetof(CPUState, gpr[5]) },
1803 { "r6", offsetof(CPUState, gpr[6]) },
1804 { "r7", offsetof(CPUState, gpr[7]) },
1805 { "r8", offsetof(CPUState, gpr[8]) },
1806 { "r9", offsetof(CPUState, gpr[9]) },
1807 { "r10", offsetof(CPUState, gpr[10]) },
1808 { "r11", offsetof(CPUState, gpr[11]) },
1809 { "r12", offsetof(CPUState, gpr[12]) },
1810 { "r13", offsetof(CPUState, gpr[13]) },
1811 { "r14", offsetof(CPUState, gpr[14]) },
1812 { "r15", offsetof(CPUState, gpr[15]) },
1813 { "r16", offsetof(CPUState, gpr[16]) },
1814 { "r17", offsetof(CPUState, gpr[17]) },
1815 { "r18", offsetof(CPUState, gpr[18]) },
1816 { "r19", offsetof(CPUState, gpr[19]) },
1817 { "r20", offsetof(CPUState, gpr[20]) },
1818 { "r21", offsetof(CPUState, gpr[21]) },
1819 { "r22", offsetof(CPUState, gpr[22]) },
1820 { "r23", offsetof(CPUState, gpr[23]) },
1821 { "r24", offsetof(CPUState, gpr[24]) },
1822 { "r25", offsetof(CPUState, gpr[25]) },
1823 { "r26", offsetof(CPUState, gpr[26]) },
1824 { "r27", offsetof(CPUState, gpr[27]) },
1825 { "r28", offsetof(CPUState, gpr[28]) },
1826 { "r29", offsetof(CPUState, gpr[29]) },
1827 { "r30", offsetof(CPUState, gpr[30]) },
1828 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001829 /* Floating point registers */
1830 { "f0", offsetof(CPUState, fpr[0]) },
1831 { "f1", offsetof(CPUState, fpr[1]) },
1832 { "f2", offsetof(CPUState, fpr[2]) },
1833 { "f3", offsetof(CPUState, fpr[3]) },
1834 { "f4", offsetof(CPUState, fpr[4]) },
1835 { "f5", offsetof(CPUState, fpr[5]) },
1836 { "f6", offsetof(CPUState, fpr[6]) },
1837 { "f7", offsetof(CPUState, fpr[7]) },
1838 { "f8", offsetof(CPUState, fpr[8]) },
1839 { "f9", offsetof(CPUState, fpr[9]) },
1840 { "f10", offsetof(CPUState, fpr[10]) },
1841 { "f11", offsetof(CPUState, fpr[11]) },
1842 { "f12", offsetof(CPUState, fpr[12]) },
1843 { "f13", offsetof(CPUState, fpr[13]) },
1844 { "f14", offsetof(CPUState, fpr[14]) },
1845 { "f15", offsetof(CPUState, fpr[15]) },
1846 { "f16", offsetof(CPUState, fpr[16]) },
1847 { "f17", offsetof(CPUState, fpr[17]) },
1848 { "f18", offsetof(CPUState, fpr[18]) },
1849 { "f19", offsetof(CPUState, fpr[19]) },
1850 { "f20", offsetof(CPUState, fpr[20]) },
1851 { "f21", offsetof(CPUState, fpr[21]) },
1852 { "f22", offsetof(CPUState, fpr[22]) },
1853 { "f23", offsetof(CPUState, fpr[23]) },
1854 { "f24", offsetof(CPUState, fpr[24]) },
1855 { "f25", offsetof(CPUState, fpr[25]) },
1856 { "f26", offsetof(CPUState, fpr[26]) },
1857 { "f27", offsetof(CPUState, fpr[27]) },
1858 { "f28", offsetof(CPUState, fpr[28]) },
1859 { "f29", offsetof(CPUState, fpr[29]) },
1860 { "f30", offsetof(CPUState, fpr[30]) },
1861 { "f31", offsetof(CPUState, fpr[31]) },
1862 { "fpscr", offsetof(CPUState, fpscr) },
1863 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001864 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001865 { "lr", offsetof(CPUState, lr) },
1866 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001867 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001868 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001869 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001870 { "msr", 0, &monitor_get_msr, },
1871 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001872 { "tbu", 0, &monitor_get_tbu, },
1873 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001874#if defined(TARGET_PPC64)
1875 /* Address space register */
1876 { "asr", offsetof(CPUState, asr) },
1877#endif
1878 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001879 { "sdr1", offsetof(CPUState, sdr1) },
1880 { "sr0", offsetof(CPUState, sr[0]) },
1881 { "sr1", offsetof(CPUState, sr[1]) },
1882 { "sr2", offsetof(CPUState, sr[2]) },
1883 { "sr3", offsetof(CPUState, sr[3]) },
1884 { "sr4", offsetof(CPUState, sr[4]) },
1885 { "sr5", offsetof(CPUState, sr[5]) },
1886 { "sr6", offsetof(CPUState, sr[6]) },
1887 { "sr7", offsetof(CPUState, sr[7]) },
1888 { "sr8", offsetof(CPUState, sr[8]) },
1889 { "sr9", offsetof(CPUState, sr[9]) },
1890 { "sr10", offsetof(CPUState, sr[10]) },
1891 { "sr11", offsetof(CPUState, sr[11]) },
1892 { "sr12", offsetof(CPUState, sr[12]) },
1893 { "sr13", offsetof(CPUState, sr[13]) },
1894 { "sr14", offsetof(CPUState, sr[14]) },
1895 { "sr15", offsetof(CPUState, sr[15]) },
1896 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001897#elif defined(TARGET_SPARC)
1898 { "g0", offsetof(CPUState, gregs[0]) },
1899 { "g1", offsetof(CPUState, gregs[1]) },
1900 { "g2", offsetof(CPUState, gregs[2]) },
1901 { "g3", offsetof(CPUState, gregs[3]) },
1902 { "g4", offsetof(CPUState, gregs[4]) },
1903 { "g5", offsetof(CPUState, gregs[5]) },
1904 { "g6", offsetof(CPUState, gregs[6]) },
1905 { "g7", offsetof(CPUState, gregs[7]) },
1906 { "o0", 0, monitor_get_reg },
1907 { "o1", 1, monitor_get_reg },
1908 { "o2", 2, monitor_get_reg },
1909 { "o3", 3, monitor_get_reg },
1910 { "o4", 4, monitor_get_reg },
1911 { "o5", 5, monitor_get_reg },
1912 { "o6", 6, monitor_get_reg },
1913 { "o7", 7, monitor_get_reg },
1914 { "l0", 8, monitor_get_reg },
1915 { "l1", 9, monitor_get_reg },
1916 { "l2", 10, monitor_get_reg },
1917 { "l3", 11, monitor_get_reg },
1918 { "l4", 12, monitor_get_reg },
1919 { "l5", 13, monitor_get_reg },
1920 { "l6", 14, monitor_get_reg },
1921 { "l7", 15, monitor_get_reg },
1922 { "i0", 16, monitor_get_reg },
1923 { "i1", 17, monitor_get_reg },
1924 { "i2", 18, monitor_get_reg },
1925 { "i3", 19, monitor_get_reg },
1926 { "i4", 20, monitor_get_reg },
1927 { "i5", 21, monitor_get_reg },
1928 { "i6", 22, monitor_get_reg },
1929 { "i7", 23, monitor_get_reg },
1930 { "pc", offsetof(CPUState, pc) },
1931 { "npc", offsetof(CPUState, npc) },
1932 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001933#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001934 { "psr", 0, &monitor_get_psr, },
1935 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001936#endif
bellarde95c8d52004-09-30 22:22:08 +00001937 { "tbr", offsetof(CPUState, tbr) },
1938 { "fsr", offsetof(CPUState, fsr) },
1939 { "f0", offsetof(CPUState, fpr[0]) },
1940 { "f1", offsetof(CPUState, fpr[1]) },
1941 { "f2", offsetof(CPUState, fpr[2]) },
1942 { "f3", offsetof(CPUState, fpr[3]) },
1943 { "f4", offsetof(CPUState, fpr[4]) },
1944 { "f5", offsetof(CPUState, fpr[5]) },
1945 { "f6", offsetof(CPUState, fpr[6]) },
1946 { "f7", offsetof(CPUState, fpr[7]) },
1947 { "f8", offsetof(CPUState, fpr[8]) },
1948 { "f9", offsetof(CPUState, fpr[9]) },
1949 { "f10", offsetof(CPUState, fpr[10]) },
1950 { "f11", offsetof(CPUState, fpr[11]) },
1951 { "f12", offsetof(CPUState, fpr[12]) },
1952 { "f13", offsetof(CPUState, fpr[13]) },
1953 { "f14", offsetof(CPUState, fpr[14]) },
1954 { "f15", offsetof(CPUState, fpr[15]) },
1955 { "f16", offsetof(CPUState, fpr[16]) },
1956 { "f17", offsetof(CPUState, fpr[17]) },
1957 { "f18", offsetof(CPUState, fpr[18]) },
1958 { "f19", offsetof(CPUState, fpr[19]) },
1959 { "f20", offsetof(CPUState, fpr[20]) },
1960 { "f21", offsetof(CPUState, fpr[21]) },
1961 { "f22", offsetof(CPUState, fpr[22]) },
1962 { "f23", offsetof(CPUState, fpr[23]) },
1963 { "f24", offsetof(CPUState, fpr[24]) },
1964 { "f25", offsetof(CPUState, fpr[25]) },
1965 { "f26", offsetof(CPUState, fpr[26]) },
1966 { "f27", offsetof(CPUState, fpr[27]) },
1967 { "f28", offsetof(CPUState, fpr[28]) },
1968 { "f29", offsetof(CPUState, fpr[29]) },
1969 { "f30", offsetof(CPUState, fpr[30]) },
1970 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00001971#ifdef TARGET_SPARC64
1972 { "f32", offsetof(CPUState, fpr[32]) },
1973 { "f34", offsetof(CPUState, fpr[34]) },
1974 { "f36", offsetof(CPUState, fpr[36]) },
1975 { "f38", offsetof(CPUState, fpr[38]) },
1976 { "f40", offsetof(CPUState, fpr[40]) },
1977 { "f42", offsetof(CPUState, fpr[42]) },
1978 { "f44", offsetof(CPUState, fpr[44]) },
1979 { "f46", offsetof(CPUState, fpr[46]) },
1980 { "f48", offsetof(CPUState, fpr[48]) },
1981 { "f50", offsetof(CPUState, fpr[50]) },
1982 { "f52", offsetof(CPUState, fpr[52]) },
1983 { "f54", offsetof(CPUState, fpr[54]) },
1984 { "f56", offsetof(CPUState, fpr[56]) },
1985 { "f58", offsetof(CPUState, fpr[58]) },
1986 { "f60", offsetof(CPUState, fpr[60]) },
1987 { "f62", offsetof(CPUState, fpr[62]) },
1988 { "asi", offsetof(CPUState, asi) },
1989 { "pstate", offsetof(CPUState, pstate) },
1990 { "cansave", offsetof(CPUState, cansave) },
1991 { "canrestore", offsetof(CPUState, canrestore) },
1992 { "otherwin", offsetof(CPUState, otherwin) },
1993 { "wstate", offsetof(CPUState, wstate) },
1994 { "cleanwin", offsetof(CPUState, cleanwin) },
1995 { "fprs", offsetof(CPUState, fprs) },
1996#endif
bellard9307c4c2004-04-04 12:57:25 +00001997#endif
1998 { NULL },
1999};
2000
blueswir160cbfb92008-12-28 13:14:48 +00002001static void expr_error(const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002002{
blueswir160cbfb92008-12-28 13:14:48 +00002003 term_printf("%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002004 longjmp(expr_env, 1);
2005}
2006
bellard6a00d602005-11-21 23:25:50 +00002007/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002008static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002009{
blueswir18662d652008-10-02 18:32:44 +00002010 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002011 void *ptr;
2012
bellard9307c4c2004-04-04 12:57:25 +00002013 for(md = monitor_defs; md->name != NULL; md++) {
2014 if (compare_cmd(name, md->name)) {
2015 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002016 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002017 } else {
bellard6a00d602005-11-21 23:25:50 +00002018 CPUState *env = mon_get_cpu();
2019 if (!env)
2020 return -2;
2021 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002022 switch(md->type) {
2023 case MD_I32:
2024 *pval = *(int32_t *)ptr;
2025 break;
2026 case MD_TLONG:
2027 *pval = *(target_long *)ptr;
2028 break;
2029 default:
2030 *pval = 0;
2031 break;
2032 }
bellard9307c4c2004-04-04 12:57:25 +00002033 }
2034 return 0;
2035 }
2036 }
2037 return -1;
2038}
2039
2040static void next(void)
2041{
2042 if (pch != '\0') {
2043 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002044 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002045 pch++;
2046 }
2047}
2048
blueswir1c2efc952007-09-25 17:28:42 +00002049static int64_t expr_sum(void);
bellard9307c4c2004-04-04 12:57:25 +00002050
blueswir1c2efc952007-09-25 17:28:42 +00002051static int64_t expr_unary(void)
bellard9307c4c2004-04-04 12:57:25 +00002052{
blueswir1c2efc952007-09-25 17:28:42 +00002053 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002054 char *p;
bellard6a00d602005-11-21 23:25:50 +00002055 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002056
2057 switch(*pch) {
2058 case '+':
2059 next();
2060 n = expr_unary();
2061 break;
2062 case '-':
2063 next();
2064 n = -expr_unary();
2065 break;
2066 case '~':
2067 next();
2068 n = ~expr_unary();
2069 break;
2070 case '(':
2071 next();
2072 n = expr_sum();
2073 if (*pch != ')') {
2074 expr_error("')' expected");
2075 }
2076 next();
2077 break;
bellard81d09122004-07-14 17:21:37 +00002078 case '\'':
2079 pch++;
2080 if (*pch == '\0')
2081 expr_error("character constant expected");
2082 n = *pch;
2083 pch++;
2084 if (*pch != '\'')
2085 expr_error("missing terminating \' character");
2086 next();
2087 break;
bellard9307c4c2004-04-04 12:57:25 +00002088 case '$':
2089 {
2090 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002091 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002092
bellard9307c4c2004-04-04 12:57:25 +00002093 pch++;
2094 q = buf;
2095 while ((*pch >= 'a' && *pch <= 'z') ||
2096 (*pch >= 'A' && *pch <= 'Z') ||
2097 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002098 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002099 if ((q - buf) < sizeof(buf) - 1)
2100 *q++ = *pch;
2101 pch++;
2102 }
blueswir1cd390082008-11-16 13:53:32 +00002103 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002104 pch++;
2105 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002106 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002107 if (ret == -1)
bellard9307c4c2004-04-04 12:57:25 +00002108 expr_error("unknown register");
ths5fafdf22007-09-16 21:08:06 +00002109 else if (ret == -2)
bellard6a00d602005-11-21 23:25:50 +00002110 expr_error("no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002111 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002112 }
2113 break;
2114 case '\0':
2115 expr_error("unexpected end of expression");
2116 n = 0;
2117 break;
2118 default:
blueswir17743e582007-09-24 18:39:04 +00002119#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002120 n = strtoull(pch, &p, 0);
2121#else
bellard9307c4c2004-04-04 12:57:25 +00002122 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002123#endif
bellard9307c4c2004-04-04 12:57:25 +00002124 if (pch == p) {
2125 expr_error("invalid char in expression");
2126 }
2127 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002128 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002129 pch++;
2130 break;
2131 }
2132 return n;
2133}
2134
2135
blueswir1c2efc952007-09-25 17:28:42 +00002136static int64_t expr_prod(void)
bellard9307c4c2004-04-04 12:57:25 +00002137{
blueswir1c2efc952007-09-25 17:28:42 +00002138 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002139 int op;
ths3b46e622007-09-17 08:09:54 +00002140
bellard9307c4c2004-04-04 12:57:25 +00002141 val = expr_unary();
2142 for(;;) {
2143 op = *pch;
2144 if (op != '*' && op != '/' && op != '%')
2145 break;
2146 next();
2147 val2 = expr_unary();
2148 switch(op) {
2149 default:
2150 case '*':
2151 val *= val2;
2152 break;
2153 case '/':
2154 case '%':
ths5fafdf22007-09-16 21:08:06 +00002155 if (val2 == 0)
bellard5b602122004-05-22 21:41:05 +00002156 expr_error("division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002157 if (op == '/')
2158 val /= val2;
2159 else
2160 val %= val2;
2161 break;
2162 }
2163 }
2164 return val;
2165}
2166
blueswir1c2efc952007-09-25 17:28:42 +00002167static int64_t expr_logic(void)
bellard9307c4c2004-04-04 12:57:25 +00002168{
blueswir1c2efc952007-09-25 17:28:42 +00002169 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002170 int op;
bellard9307c4c2004-04-04 12:57:25 +00002171
2172 val = expr_prod();
2173 for(;;) {
2174 op = *pch;
2175 if (op != '&' && op != '|' && op != '^')
2176 break;
2177 next();
2178 val2 = expr_prod();
2179 switch(op) {
2180 default:
2181 case '&':
2182 val &= val2;
2183 break;
2184 case '|':
2185 val |= val2;
2186 break;
2187 case '^':
2188 val ^= val2;
2189 break;
2190 }
2191 }
2192 return val;
2193}
2194
blueswir1c2efc952007-09-25 17:28:42 +00002195static int64_t expr_sum(void)
bellard9307c4c2004-04-04 12:57:25 +00002196{
blueswir1c2efc952007-09-25 17:28:42 +00002197 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002198 int op;
bellard9307c4c2004-04-04 12:57:25 +00002199
2200 val = expr_logic();
2201 for(;;) {
2202 op = *pch;
2203 if (op != '+' && op != '-')
2204 break;
2205 next();
2206 val2 = expr_logic();
2207 if (op == '+')
2208 val += val2;
2209 else
2210 val -= val2;
2211 }
2212 return val;
2213}
2214
blueswir1c2efc952007-09-25 17:28:42 +00002215static int get_expr(int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002216{
2217 pch = *pp;
2218 if (setjmp(expr_env)) {
2219 *pp = pch;
2220 return -1;
2221 }
blueswir1cd390082008-11-16 13:53:32 +00002222 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002223 pch++;
2224 *pval = expr_sum();
2225 *pp = pch;
2226 return 0;
2227}
2228
2229static int get_str(char *buf, int buf_size, const char **pp)
2230{
2231 const char *p;
2232 char *q;
2233 int c;
2234
bellard81d09122004-07-14 17:21:37 +00002235 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002236 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002237 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002238 p++;
2239 if (*p == '\0') {
2240 fail:
bellard81d09122004-07-14 17:21:37 +00002241 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002242 *pp = p;
2243 return -1;
2244 }
bellard9307c4c2004-04-04 12:57:25 +00002245 if (*p == '\"') {
2246 p++;
2247 while (*p != '\0' && *p != '\"') {
2248 if (*p == '\\') {
2249 p++;
2250 c = *p++;
2251 switch(c) {
2252 case 'n':
2253 c = '\n';
2254 break;
2255 case 'r':
2256 c = '\r';
2257 break;
2258 case '\\':
2259 case '\'':
2260 case '\"':
2261 break;
2262 default:
2263 qemu_printf("unsupported escape code: '\\%c'\n", c);
2264 goto fail;
2265 }
2266 if ((q - buf) < buf_size - 1) {
2267 *q++ = c;
2268 }
2269 } else {
2270 if ((q - buf) < buf_size - 1) {
2271 *q++ = *p;
2272 }
2273 p++;
2274 }
2275 }
2276 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002277 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002278 goto fail;
2279 }
2280 p++;
2281 } else {
blueswir1cd390082008-11-16 13:53:32 +00002282 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002283 if ((q - buf) < buf_size - 1) {
2284 *q++ = *p;
2285 }
2286 p++;
2287 }
bellard9307c4c2004-04-04 12:57:25 +00002288 }
bellard81d09122004-07-14 17:21:37 +00002289 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002290 *pp = p;
2291 return 0;
2292}
2293
2294static int default_fmt_format = 'x';
2295static int default_fmt_size = 4;
2296
2297#define MAX_ARGS 16
2298
bellard7e2515e2004-08-01 21:52:19 +00002299static void monitor_handle_command(const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002300{
2301 const char *p, *pstart, *typestr;
2302 char *q;
2303 int c, nb_args, len, i, has_arg;
blueswir18662d652008-10-02 18:32:44 +00002304 const term_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002305 char cmdname[256];
2306 char buf[1024];
2307 void *str_allocated[MAX_ARGS];
2308 void *args[MAX_ARGS];
blueswir1a5f1b962008-08-17 20:21:51 +00002309 void (*handler_0)(void);
2310 void (*handler_1)(void *arg0);
2311 void (*handler_2)(void *arg0, void *arg1);
2312 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2313 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2314 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2315 void *arg4);
2316 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2317 void *arg4, void *arg5);
2318 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2319 void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002320
2321#ifdef DEBUG
2322 term_printf("command='%s'\n", cmdline);
2323#endif
ths3b46e622007-09-17 08:09:54 +00002324
bellard9307c4c2004-04-04 12:57:25 +00002325 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002326 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002327 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002328 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002329 p++;
2330 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002331 return;
bellard9307c4c2004-04-04 12:57:25 +00002332 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002333 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002334 p++;
2335 len = p - pstart;
2336 if (len > sizeof(cmdname) - 1)
2337 len = sizeof(cmdname) - 1;
2338 memcpy(cmdname, pstart, len);
2339 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002340
bellard9307c4c2004-04-04 12:57:25 +00002341 /* find the command */
bellard9dc39cb2004-03-14 21:38:27 +00002342 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002343 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002344 goto found;
2345 }
bellard9307c4c2004-04-04 12:57:25 +00002346 term_printf("unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002347 return;
2348 found:
bellard9307c4c2004-04-04 12:57:25 +00002349
2350 for(i = 0; i < MAX_ARGS; i++)
2351 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002352
bellard9307c4c2004-04-04 12:57:25 +00002353 /* parse the parameters */
2354 typestr = cmd->args_type;
2355 nb_args = 0;
2356 for(;;) {
2357 c = *typestr;
2358 if (c == '\0')
2359 break;
2360 typestr++;
2361 switch(c) {
2362 case 'F':
bellard81d09122004-07-14 17:21:37 +00002363 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002364 case 's':
2365 {
2366 int ret;
2367 char *str;
ths3b46e622007-09-17 08:09:54 +00002368
blueswir1cd390082008-11-16 13:53:32 +00002369 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002370 p++;
2371 if (*typestr == '?') {
2372 typestr++;
2373 if (*p == '\0') {
2374 /* no optional string: NULL argument */
2375 str = NULL;
2376 goto add_str;
2377 }
2378 }
2379 ret = get_str(buf, sizeof(buf), &p);
2380 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002381 switch(c) {
2382 case 'F':
bellard9307c4c2004-04-04 12:57:25 +00002383 term_printf("%s: filename expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002384 break;
2385 case 'B':
2386 term_printf("%s: block device name expected\n", cmdname);
2387 break;
2388 default:
bellard9307c4c2004-04-04 12:57:25 +00002389 term_printf("%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002390 break;
2391 }
bellard9307c4c2004-04-04 12:57:25 +00002392 goto fail;
2393 }
2394 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002395 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002396 str_allocated[nb_args] = str;
2397 add_str:
2398 if (nb_args >= MAX_ARGS) {
2399 error_args:
2400 term_printf("%s: too many arguments\n", cmdname);
2401 goto fail;
2402 }
2403 args[nb_args++] = str;
2404 }
2405 break;
2406 case '/':
2407 {
2408 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002409
blueswir1cd390082008-11-16 13:53:32 +00002410 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002411 p++;
2412 if (*p == '/') {
2413 /* format found */
2414 p++;
2415 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002416 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002417 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002418 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002419 count = count * 10 + (*p - '0');
2420 p++;
2421 }
2422 }
2423 size = -1;
2424 format = -1;
2425 for(;;) {
2426 switch(*p) {
2427 case 'o':
2428 case 'd':
2429 case 'u':
2430 case 'x':
2431 case 'i':
2432 case 'c':
2433 format = *p++;
2434 break;
2435 case 'b':
2436 size = 1;
2437 p++;
2438 break;
2439 case 'h':
2440 size = 2;
2441 p++;
2442 break;
2443 case 'w':
2444 size = 4;
2445 p++;
2446 break;
2447 case 'g':
2448 case 'L':
2449 size = 8;
2450 p++;
2451 break;
2452 default:
2453 goto next;
2454 }
2455 }
2456 next:
blueswir1cd390082008-11-16 13:53:32 +00002457 if (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002458 term_printf("invalid char in format: '%c'\n", *p);
2459 goto fail;
2460 }
bellard9307c4c2004-04-04 12:57:25 +00002461 if (format < 0)
2462 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002463 if (format != 'i') {
2464 /* for 'i', not specifying a size gives -1 as size */
2465 if (size < 0)
2466 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002467 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002468 }
bellard9307c4c2004-04-04 12:57:25 +00002469 default_fmt_format = format;
2470 } else {
2471 count = 1;
2472 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002473 if (format != 'i') {
2474 size = default_fmt_size;
2475 } else {
2476 size = -1;
2477 }
bellard9307c4c2004-04-04 12:57:25 +00002478 }
2479 if (nb_args + 3 > MAX_ARGS)
2480 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002481 args[nb_args++] = (void*)(long)count;
2482 args[nb_args++] = (void*)(long)format;
2483 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002484 }
2485 break;
2486 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002487 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002488 {
blueswir1c2efc952007-09-25 17:28:42 +00002489 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002490
blueswir1cd390082008-11-16 13:53:32 +00002491 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002492 p++;
bellard34405572004-06-08 00:55:58 +00002493 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002494 if (*typestr == '?') {
2495 if (*p == '\0')
2496 has_arg = 0;
2497 else
2498 has_arg = 1;
2499 } else {
2500 if (*p == '.') {
2501 p++;
blueswir1cd390082008-11-16 13:53:32 +00002502 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002503 p++;
2504 has_arg = 1;
2505 } else {
2506 has_arg = 0;
2507 }
2508 }
bellard13224a82006-07-14 22:03:35 +00002509 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002510 if (nb_args >= MAX_ARGS)
2511 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002512 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002513 if (!has_arg) {
2514 if (nb_args >= MAX_ARGS)
2515 goto error_args;
2516 val = -1;
2517 goto add_num;
2518 }
2519 }
2520 if (get_expr(&val, &p))
2521 goto fail;
2522 add_num:
bellard92a31b12005-02-10 22:00:52 +00002523 if (c == 'i') {
2524 if (nb_args >= MAX_ARGS)
2525 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002526 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002527 } else {
2528 if ((nb_args + 1) >= MAX_ARGS)
2529 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002530#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002531 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002532#else
2533 args[nb_args++] = (void *)0;
2534#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002535 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002536 }
bellard9307c4c2004-04-04 12:57:25 +00002537 }
2538 break;
2539 case '-':
2540 {
2541 int has_option;
2542 /* option */
ths3b46e622007-09-17 08:09:54 +00002543
bellard9307c4c2004-04-04 12:57:25 +00002544 c = *typestr++;
2545 if (c == '\0')
2546 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002547 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002548 p++;
2549 has_option = 0;
2550 if (*p == '-') {
2551 p++;
2552 if (*p != c) {
ths5fafdf22007-09-16 21:08:06 +00002553 term_printf("%s: unsupported option -%c\n",
bellard9307c4c2004-04-04 12:57:25 +00002554 cmdname, *p);
2555 goto fail;
2556 }
2557 p++;
2558 has_option = 1;
2559 }
2560 if (nb_args >= MAX_ARGS)
2561 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002562 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002563 }
2564 break;
2565 default:
2566 bad_type:
2567 term_printf("%s: unknown type '%c'\n", cmdname, c);
2568 goto fail;
2569 }
2570 }
2571 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002572 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002573 p++;
2574 if (*p != '\0') {
ths5fafdf22007-09-16 21:08:06 +00002575 term_printf("%s: extraneous characters at the end of line\n",
bellard9307c4c2004-04-04 12:57:25 +00002576 cmdname);
2577 goto fail;
2578 }
2579
2580 switch(nb_args) {
2581 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002582 handler_0 = cmd->handler;
2583 handler_0();
bellard9307c4c2004-04-04 12:57:25 +00002584 break;
2585 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002586 handler_1 = cmd->handler;
2587 handler_1(args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002588 break;
2589 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002590 handler_2 = cmd->handler;
2591 handler_2(args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002592 break;
2593 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002594 handler_3 = cmd->handler;
2595 handler_3(args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002596 break;
2597 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002598 handler_4 = cmd->handler;
2599 handler_4(args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002600 break;
2601 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002602 handler_5 = cmd->handler;
2603 handler_5(args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002604 break;
bellard34405572004-06-08 00:55:58 +00002605 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002606 handler_6 = cmd->handler;
2607 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002608 break;
bellardec36b692006-07-16 18:57:03 +00002609 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002610 handler_7 = cmd->handler;
2611 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
bellardec36b692006-07-16 18:57:03 +00002612 break;
bellard9307c4c2004-04-04 12:57:25 +00002613 default:
2614 term_printf("unsupported number of arguments: %d\n", nb_args);
2615 goto fail;
2616 }
2617 fail:
2618 for(i = 0; i < MAX_ARGS; i++)
2619 qemu_free(str_allocated[i]);
2620 return;
bellard9dc39cb2004-03-14 21:38:27 +00002621}
2622
bellard81d09122004-07-14 17:21:37 +00002623static void cmd_completion(const char *name, const char *list)
2624{
2625 const char *p, *pstart;
2626 char cmd[128];
2627 int len;
2628
2629 p = list;
2630 for(;;) {
2631 pstart = p;
2632 p = strchr(p, '|');
2633 if (!p)
2634 p = pstart + strlen(pstart);
2635 len = p - pstart;
2636 if (len > sizeof(cmd) - 2)
2637 len = sizeof(cmd) - 2;
2638 memcpy(cmd, pstart, len);
2639 cmd[len] = '\0';
2640 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2641 add_completion(cmd);
2642 }
2643 if (*p == '\0')
2644 break;
2645 p++;
2646 }
2647}
2648
2649static void file_completion(const char *input)
2650{
2651 DIR *ffs;
2652 struct dirent *d;
2653 char path[1024];
2654 char file[1024], file_prefix[1024];
2655 int input_path_len;
2656 const char *p;
2657
ths5fafdf22007-09-16 21:08:06 +00002658 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002659 if (!p) {
2660 input_path_len = 0;
2661 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002662 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002663 } else {
2664 input_path_len = p - input + 1;
2665 memcpy(path, input, input_path_len);
2666 if (input_path_len > sizeof(path) - 1)
2667 input_path_len = sizeof(path) - 1;
2668 path[input_path_len] = '\0';
2669 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2670 }
2671#ifdef DEBUG_COMPLETION
2672 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2673#endif
2674 ffs = opendir(path);
2675 if (!ffs)
2676 return;
2677 for(;;) {
2678 struct stat sb;
2679 d = readdir(ffs);
2680 if (!d)
2681 break;
2682 if (strstart(d->d_name, file_prefix, NULL)) {
2683 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002684 if (input_path_len < sizeof(file))
2685 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2686 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002687 /* stat the file to find out if it's a directory.
2688 * In that case add a slash to speed up typing long paths
2689 */
2690 stat(file, &sb);
2691 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002692 pstrcat(file, sizeof(file), "/");
bellard81d09122004-07-14 17:21:37 +00002693 add_completion(file);
2694 }
2695 }
2696 closedir(ffs);
2697}
2698
aliguori51de9762009-03-05 23:00:43 +00002699static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002700{
aliguori51de9762009-03-05 23:00:43 +00002701 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002702 const char *input = opaque;
2703
2704 if (input[0] == '\0' ||
2705 !strncmp(name, (char *)input, strlen(input))) {
2706 add_completion(name);
2707 }
2708}
2709
2710/* NOTE: this parser is an approximate form of the real command parser */
2711static void parse_cmdline(const char *cmdline,
2712 int *pnb_args, char **args)
2713{
2714 const char *p;
2715 int nb_args, ret;
2716 char buf[1024];
2717
2718 p = cmdline;
2719 nb_args = 0;
2720 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002721 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002722 p++;
2723 if (*p == '\0')
2724 break;
2725 if (nb_args >= MAX_ARGS)
2726 break;
2727 ret = get_str(buf, sizeof(buf), &p);
2728 args[nb_args] = qemu_strdup(buf);
2729 nb_args++;
2730 if (ret < 0)
2731 break;
2732 }
2733 *pnb_args = nb_args;
2734}
2735
bellard7e2515e2004-08-01 21:52:19 +00002736void readline_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002737{
2738 const char *cmdname;
2739 char *args[MAX_ARGS];
2740 int nb_args, i, len;
2741 const char *ptype, *str;
blueswir18662d652008-10-02 18:32:44 +00002742 const term_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002743 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002744
2745 parse_cmdline(cmdline, &nb_args, args);
2746#ifdef DEBUG_COMPLETION
2747 for(i = 0; i < nb_args; i++) {
2748 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2749 }
2750#endif
2751
2752 /* if the line ends with a space, it means we want to complete the
2753 next arg */
2754 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002755 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002756 if (nb_args >= MAX_ARGS)
2757 return;
2758 args[nb_args++] = qemu_strdup("");
2759 }
2760 if (nb_args <= 1) {
2761 /* command completion */
2762 if (nb_args == 0)
2763 cmdname = "";
2764 else
2765 cmdname = args[0];
2766 completion_index = strlen(cmdname);
2767 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2768 cmd_completion(cmdname, cmd->name);
2769 }
2770 } else {
2771 /* find the command */
2772 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2773 if (compare_cmd(args[0], cmd->name))
2774 goto found;
2775 }
2776 return;
2777 found:
2778 ptype = cmd->args_type;
2779 for(i = 0; i < nb_args - 2; i++) {
2780 if (*ptype != '\0') {
2781 ptype++;
2782 while (*ptype == '?')
2783 ptype++;
2784 }
2785 }
2786 str = args[nb_args - 1];
2787 switch(*ptype) {
2788 case 'F':
2789 /* file completion */
2790 completion_index = strlen(str);
2791 file_completion(str);
2792 break;
2793 case 'B':
2794 /* block device name completion */
2795 completion_index = strlen(str);
2796 bdrv_iterate(block_completion_it, (void *)str);
2797 break;
bellard7fe48482004-10-09 18:08:01 +00002798 case 's':
2799 /* XXX: more generic ? */
2800 if (!strcmp(cmd->name, "info")) {
2801 completion_index = strlen(str);
2802 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2803 cmd_completion(str, cmd->name);
2804 }
bellard64866c32006-05-07 18:03:31 +00002805 } else if (!strcmp(cmd->name, "sendkey")) {
2806 completion_index = strlen(str);
2807 for(key = key_defs; key->name != NULL; key++) {
2808 cmd_completion(str, key->name);
2809 }
bellard7fe48482004-10-09 18:08:01 +00002810 }
2811 break;
bellard81d09122004-07-14 17:21:37 +00002812 default:
2813 break;
2814 }
2815 }
2816 for(i = 0; i < nb_args; i++)
2817 qemu_free(args[i]);
2818}
2819
bellard9dc39cb2004-03-14 21:38:27 +00002820static int term_can_read(void *opaque)
2821{
bellard81d09122004-07-14 17:21:37 +00002822 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002823}
2824
2825static void term_read(void *opaque, const uint8_t *buf, int size)
2826{
2827 int i;
2828 for(i = 0; i < size; i++)
bellard7e2515e2004-08-01 21:52:19 +00002829 readline_handle_byte(buf[i]);
2830}
2831
aliguorid8f44602008-10-06 13:52:44 +00002832static int monitor_suspended;
2833
bellard7e2515e2004-08-01 21:52:19 +00002834static void monitor_handle_command1(void *opaque, const char *cmdline)
2835{
2836 monitor_handle_command(cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002837 if (!monitor_suspended)
2838 monitor_start_input();
2839 else
2840 monitor_suspended = 2;
2841}
2842
2843void monitor_suspend(void)
2844{
2845 monitor_suspended = 1;
2846}
2847
2848void monitor_resume(void)
2849{
2850 if (monitor_suspended == 2)
2851 monitor_start_input();
2852 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002853}
2854
aliguori83ab7952008-08-19 14:44:22 +00002855static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002856{
2857 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
bellard9dc39cb2004-03-14 21:38:27 +00002858}
2859
ths86e94de2007-01-05 22:01:59 +00002860static void term_event(void *opaque, int event)
2861{
2862 if (event != CHR_EVENT_RESET)
2863 return;
2864
2865 if (!hide_banner)
2866 term_printf("QEMU %s monitor - type 'help' for more information\n",
2867 QEMU_VERSION);
2868 monitor_start_input();
2869}
2870
ths20d8a3e2007-02-18 17:04:49 +00002871static int is_first_init = 1;
2872
bellard81d09122004-07-14 17:21:37 +00002873void monitor_init(CharDriverState *hd, int show_banner)
bellard9dc39cb2004-03-14 21:38:27 +00002874{
ths20d8a3e2007-02-18 17:04:49 +00002875 int i;
2876
2877 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002878 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2879 if (!key_timer)
2880 return;
ths20d8a3e2007-02-18 17:04:49 +00002881 for (i = 0; i < MAX_MON; i++) {
2882 monitor_hd[i] = NULL;
2883 }
2884 is_first_init = 0;
2885 }
2886 for (i = 0; i < MAX_MON; i++) {
2887 if (monitor_hd[i] == NULL) {
2888 monitor_hd[i] = hd;
2889 break;
2890 }
2891 }
2892
ths86e94de2007-01-05 22:01:59 +00002893 hide_banner = !show_banner;
2894
pbrooke5b0bc42007-01-27 23:46:43 +00002895 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
aliguori83ab7952008-08-19 14:44:22 +00002896
2897 readline_start("", 0, monitor_handle_command1, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002898}
2899
2900/* XXX: use threads ? */
2901/* modal monitor readline */
2902static int monitor_readline_started;
2903static char *monitor_readline_buf;
2904static int monitor_readline_buf_size;
2905
2906static void monitor_readline_cb(void *opaque, const char *input)
2907{
2908 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2909 monitor_readline_started = 0;
2910}
2911
aliguoric0f4ce72009-03-05 23:01:01 +00002912static void monitor_readline(const char *prompt, int is_password,
2913 char *buf, int buf_size)
bellard7e2515e2004-08-01 21:52:19 +00002914{
ths20d8a3e2007-02-18 17:04:49 +00002915 int i;
aliguoribc0129d2008-08-01 15:12:34 +00002916 int old_focus[MAX_MON];
ths20d8a3e2007-02-18 17:04:49 +00002917
bellard7e2515e2004-08-01 21:52:19 +00002918 if (is_password) {
aliguoribc0129d2008-08-01 15:12:34 +00002919 for (i = 0; i < MAX_MON; i++) {
2920 old_focus[i] = 0;
2921 if (monitor_hd[i]) {
2922 old_focus[i] = monitor_hd[i]->focus;
2923 monitor_hd[i]->focus = 0;
ths20d8a3e2007-02-18 17:04:49 +00002924 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
aliguoribc0129d2008-08-01 15:12:34 +00002925 }
2926 }
bellard7e2515e2004-08-01 21:52:19 +00002927 }
aliguoribc0129d2008-08-01 15:12:34 +00002928
bellard7e2515e2004-08-01 21:52:19 +00002929 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2930 monitor_readline_buf = buf;
2931 monitor_readline_buf_size = buf_size;
2932 monitor_readline_started = 1;
2933 while (monitor_readline_started) {
2934 main_loop_wait(10);
2935 }
aliguoribc0129d2008-08-01 15:12:34 +00002936 /* restore original focus */
2937 if (is_password) {
2938 for (i = 0; i < MAX_MON; i++)
2939 if (old_focus[i])
2940 monitor_hd[i]->focus = old_focus[i];
2941 }
bellard9dc39cb2004-03-14 21:38:27 +00002942}
aliguoric0f4ce72009-03-05 23:01:01 +00002943
2944int monitor_read_bdrv_key(BlockDriverState *bs)
2945{
2946 char password[256];
2947 int i;
2948
2949 if (!bdrv_is_encrypted(bs))
2950 return 0;
2951
2952 term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2953 bdrv_get_encrypted_filename(bs));
2954 for(i = 0; i < 3; i++) {
2955 monitor_readline("Password: ", 1, password, sizeof(password));
2956 if (bdrv_set_key(bs, password) == 0)
2957 return 0;
2958 term_printf("invalid password\n");
2959 }
2960 return -EPERM;
2961}