]> git.proxmox.com Git - qemu.git/blob - monitor.c
qapi: Convert query-spice
[qemu.git] / monitor.c
1 /*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <dirent.h>
25 #include "hw/hw.h"
26 #include "hw/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/pcmcia.h"
29 #include "hw/pc.h"
30 #include "hw/pci.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
33 #include "gdbstub.h"
34 #include "net.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "ui/qemu-spice.h"
38 #include "sysemu.h"
39 #include "monitor.h"
40 #include "readline.h"
41 #include "console.h"
42 #include "blockdev.h"
43 #include "audio/audio.h"
44 #include "disas.h"
45 #include "balloon.h"
46 #include "qemu-timer.h"
47 #include "migration.h"
48 #include "kvm.h"
49 #include "acl.h"
50 #include "qint.h"
51 #include "qfloat.h"
52 #include "qlist.h"
53 #include "qbool.h"
54 #include "qstring.h"
55 #include "qjson.h"
56 #include "json-streamer.h"
57 #include "json-parser.h"
58 #include "osdep.h"
59 #include "cpu.h"
60 #include "trace.h"
61 #include "trace/control.h"
62 #ifdef CONFIG_TRACE_SIMPLE
63 #include "trace/simple.h"
64 #endif
65 #include "ui/qemu-spice.h"
66 #include "memory.h"
67 #include "qmp-commands.h"
68 #include "hmp.h"
69
70 /* for pic/irq_info */
71 #if defined(TARGET_SPARC)
72 #include "hw/sun4m.h"
73 #endif
74 #include "hw/lm32_pic.h"
75
76 //#define DEBUG
77 //#define DEBUG_COMPLETION
78
79 /*
80 * Supported types:
81 *
82 * 'F' filename
83 * 'B' block device name
84 * 's' string (accept optional quote)
85 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
90 * 'i' 32 bit integer
91 * 'l' target long (32 or 64 bit)
92 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
94 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
99 * 'T' double
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
102 * '/' optional gdb-like print format (like "/10x")
103 *
104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
106 * 'b' boolean
107 * user mode accepts "on" or "off"
108 * '-' optional parameter (eg. '-f')
109 *
110 */
111
112 typedef struct MonitorCompletionData MonitorCompletionData;
113 struct MonitorCompletionData {
114 Monitor *mon;
115 void (*user_print)(Monitor *mon, const QObject *data);
116 };
117
118 typedef struct mon_cmd_t {
119 const char *name;
120 const char *args_type;
121 const char *params;
122 const char *help;
123 void (*user_print)(Monitor *mon, const QObject *data);
124 union {
125 void (*info)(Monitor *mon);
126 void (*info_new)(Monitor *mon, QObject **ret_data);
127 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
128 void (*cmd)(Monitor *mon, const QDict *qdict);
129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
130 int (*cmd_async)(Monitor *mon, const QDict *params,
131 MonitorCompletion *cb, void *opaque);
132 } mhandler;
133 bool qapi;
134 int flags;
135 } mon_cmd_t;
136
137 /* file descriptors passed via SCM_RIGHTS */
138 typedef struct mon_fd_t mon_fd_t;
139 struct mon_fd_t {
140 char *name;
141 int fd;
142 QLIST_ENTRY(mon_fd_t) next;
143 };
144
145 typedef struct MonitorControl {
146 QObject *id;
147 JSONMessageParser parser;
148 int command_mode;
149 } MonitorControl;
150
151 struct Monitor {
152 CharDriverState *chr;
153 int mux_out;
154 int reset_seen;
155 int flags;
156 int suspend_cnt;
157 uint8_t outbuf[1024];
158 int outbuf_index;
159 ReadLineState *rs;
160 MonitorControl *mc;
161 CPUState *mon_cpu;
162 BlockDriverCompletionFunc *password_completion_cb;
163 void *password_opaque;
164 #ifdef CONFIG_DEBUG_MONITOR
165 int print_calls_nr;
166 #endif
167 QError *error;
168 QLIST_HEAD(,mon_fd_t) fds;
169 QLIST_ENTRY(Monitor) entry;
170 };
171
172 #ifdef CONFIG_DEBUG_MONITOR
173 #define MON_DEBUG(fmt, ...) do { \
174 fprintf(stderr, "Monitor: "); \
175 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
176
177 static inline void mon_print_count_inc(Monitor *mon)
178 {
179 mon->print_calls_nr++;
180 }
181
182 static inline void mon_print_count_init(Monitor *mon)
183 {
184 mon->print_calls_nr = 0;
185 }
186
187 static inline int mon_print_count_get(const Monitor *mon)
188 {
189 return mon->print_calls_nr;
190 }
191
192 #else /* !CONFIG_DEBUG_MONITOR */
193 #define MON_DEBUG(fmt, ...) do { } while (0)
194 static inline void mon_print_count_inc(Monitor *mon) { }
195 static inline void mon_print_count_init(Monitor *mon) { }
196 static inline int mon_print_count_get(const Monitor *mon) { return 0; }
197 #endif /* CONFIG_DEBUG_MONITOR */
198
199 /* QMP checker flags */
200 #define QMP_ACCEPT_UNKNOWNS 1
201
202 static QLIST_HEAD(mon_list, Monitor) mon_list;
203
204 static const mon_cmd_t mon_cmds[];
205 static const mon_cmd_t info_cmds[];
206
207 static const mon_cmd_t qmp_cmds[];
208 static const mon_cmd_t qmp_query_cmds[];
209
210 Monitor *cur_mon;
211 Monitor *default_mon;
212
213 static void monitor_command_cb(Monitor *mon, const char *cmdline,
214 void *opaque);
215
216 static inline int qmp_cmd_mode(const Monitor *mon)
217 {
218 return (mon->mc ? mon->mc->command_mode : 0);
219 }
220
221 /* Return true if in control mode, false otherwise */
222 static inline int monitor_ctrl_mode(const Monitor *mon)
223 {
224 return (mon->flags & MONITOR_USE_CONTROL);
225 }
226
227 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228 int monitor_cur_is_qmp(void)
229 {
230 return cur_mon && monitor_ctrl_mode(cur_mon);
231 }
232
233 static void monitor_read_command(Monitor *mon, int show_prompt)
234 {
235 if (!mon->rs)
236 return;
237
238 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
239 if (show_prompt)
240 readline_show_prompt(mon->rs);
241 }
242
243 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
244 void *opaque)
245 {
246 if (monitor_ctrl_mode(mon)) {
247 qerror_report(QERR_MISSING_PARAMETER, "password");
248 return -EINVAL;
249 } else if (mon->rs) {
250 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
251 /* prompt is printed on return from the command handler */
252 return 0;
253 } else {
254 monitor_printf(mon, "terminal does not support password prompting\n");
255 return -ENOTTY;
256 }
257 }
258
259 void monitor_flush(Monitor *mon)
260 {
261 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
262 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
263 mon->outbuf_index = 0;
264 }
265 }
266
267 /* flush at every end of line or if the buffer is full */
268 static void monitor_puts(Monitor *mon, const char *str)
269 {
270 char c;
271
272 for(;;) {
273 c = *str++;
274 if (c == '\0')
275 break;
276 if (c == '\n')
277 mon->outbuf[mon->outbuf_index++] = '\r';
278 mon->outbuf[mon->outbuf_index++] = c;
279 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
280 || c == '\n')
281 monitor_flush(mon);
282 }
283 }
284
285 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
286 {
287 char buf[4096];
288
289 if (!mon)
290 return;
291
292 mon_print_count_inc(mon);
293
294 if (monitor_ctrl_mode(mon)) {
295 return;
296 }
297
298 vsnprintf(buf, sizeof(buf), fmt, ap);
299 monitor_puts(mon, buf);
300 }
301
302 void monitor_printf(Monitor *mon, const char *fmt, ...)
303 {
304 va_list ap;
305 va_start(ap, fmt);
306 monitor_vprintf(mon, fmt, ap);
307 va_end(ap);
308 }
309
310 void monitor_print_filename(Monitor *mon, const char *filename)
311 {
312 int i;
313
314 for (i = 0; filename[i]; i++) {
315 switch (filename[i]) {
316 case ' ':
317 case '"':
318 case '\\':
319 monitor_printf(mon, "\\%c", filename[i]);
320 break;
321 case '\t':
322 monitor_printf(mon, "\\t");
323 break;
324 case '\r':
325 monitor_printf(mon, "\\r");
326 break;
327 case '\n':
328 monitor_printf(mon, "\\n");
329 break;
330 default:
331 monitor_printf(mon, "%c", filename[i]);
332 break;
333 }
334 }
335 }
336
337 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
338 const char *fmt, ...)
339 {
340 va_list ap;
341 va_start(ap, fmt);
342 monitor_vprintf((Monitor *)stream, fmt, ap);
343 va_end(ap);
344 return 0;
345 }
346
347 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
348
349 static inline int handler_is_qobject(const mon_cmd_t *cmd)
350 {
351 return cmd->user_print != NULL;
352 }
353
354 static inline bool handler_is_async(const mon_cmd_t *cmd)
355 {
356 return cmd->flags & MONITOR_CMD_ASYNC;
357 }
358
359 static inline int monitor_has_error(const Monitor *mon)
360 {
361 return mon->error != NULL;
362 }
363
364 static void monitor_json_emitter(Monitor *mon, const QObject *data)
365 {
366 QString *json;
367
368 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
369 qobject_to_json(data);
370 assert(json != NULL);
371
372 qstring_append_chr(json, '\n');
373 monitor_puts(mon, qstring_get_str(json));
374
375 QDECREF(json);
376 }
377
378 static void monitor_protocol_emitter(Monitor *mon, QObject *data)
379 {
380 QDict *qmp;
381
382 trace_monitor_protocol_emitter(mon);
383
384 qmp = qdict_new();
385
386 if (!monitor_has_error(mon)) {
387 /* success response */
388 if (data) {
389 qobject_incref(data);
390 qdict_put_obj(qmp, "return", data);
391 } else {
392 /* return an empty QDict by default */
393 qdict_put(qmp, "return", qdict_new());
394 }
395 } else {
396 /* error response */
397 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
398 qdict_put(qmp, "error", mon->error->error);
399 QINCREF(mon->error->error);
400 QDECREF(mon->error);
401 mon->error = NULL;
402 }
403
404 if (mon->mc->id) {
405 qdict_put_obj(qmp, "id", mon->mc->id);
406 mon->mc->id = NULL;
407 }
408
409 monitor_json_emitter(mon, QOBJECT(qmp));
410 QDECREF(qmp);
411 }
412
413 static void timestamp_put(QDict *qdict)
414 {
415 int err;
416 QObject *obj;
417 qemu_timeval tv;
418
419 err = qemu_gettimeofday(&tv);
420 if (err < 0)
421 return;
422
423 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
424 "'microseconds': %" PRId64 " }",
425 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
426 qdict_put_obj(qdict, "timestamp", obj);
427 }
428
429 /**
430 * monitor_protocol_event(): Generate a Monitor event
431 *
432 * Event-specific data can be emitted through the (optional) 'data' parameter.
433 */
434 void monitor_protocol_event(MonitorEvent event, QObject *data)
435 {
436 QDict *qmp;
437 const char *event_name;
438 Monitor *mon;
439
440 assert(event < QEVENT_MAX);
441
442 switch (event) {
443 case QEVENT_SHUTDOWN:
444 event_name = "SHUTDOWN";
445 break;
446 case QEVENT_RESET:
447 event_name = "RESET";
448 break;
449 case QEVENT_POWERDOWN:
450 event_name = "POWERDOWN";
451 break;
452 case QEVENT_STOP:
453 event_name = "STOP";
454 break;
455 case QEVENT_RESUME:
456 event_name = "RESUME";
457 break;
458 case QEVENT_VNC_CONNECTED:
459 event_name = "VNC_CONNECTED";
460 break;
461 case QEVENT_VNC_INITIALIZED:
462 event_name = "VNC_INITIALIZED";
463 break;
464 case QEVENT_VNC_DISCONNECTED:
465 event_name = "VNC_DISCONNECTED";
466 break;
467 case QEVENT_BLOCK_IO_ERROR:
468 event_name = "BLOCK_IO_ERROR";
469 break;
470 case QEVENT_RTC_CHANGE:
471 event_name = "RTC_CHANGE";
472 break;
473 case QEVENT_WATCHDOG:
474 event_name = "WATCHDOG";
475 break;
476 case QEVENT_SPICE_CONNECTED:
477 event_name = "SPICE_CONNECTED";
478 break;
479 case QEVENT_SPICE_INITIALIZED:
480 event_name = "SPICE_INITIALIZED";
481 break;
482 case QEVENT_SPICE_DISCONNECTED:
483 event_name = "SPICE_DISCONNECTED";
484 break;
485 default:
486 abort();
487 break;
488 }
489
490 qmp = qdict_new();
491 timestamp_put(qmp);
492 qdict_put(qmp, "event", qstring_from_str(event_name));
493 if (data) {
494 qobject_incref(data);
495 qdict_put_obj(qmp, "data", data);
496 }
497
498 QLIST_FOREACH(mon, &mon_list, entry) {
499 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
500 monitor_json_emitter(mon, QOBJECT(qmp));
501 }
502 }
503 QDECREF(qmp);
504 }
505
506 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
507 QObject **ret_data)
508 {
509 /* Will setup QMP capabilities in the future */
510 if (monitor_ctrl_mode(mon)) {
511 mon->mc->command_mode = 1;
512 }
513
514 return 0;
515 }
516
517 static void handle_user_command(Monitor *mon, const char *cmdline);
518
519 static int do_hmp_passthrough(Monitor *mon, const QDict *params,
520 QObject **ret_data)
521 {
522 int ret = 0;
523 Monitor *old_mon, hmp;
524 CharDriverState mchar;
525
526 memset(&hmp, 0, sizeof(hmp));
527 qemu_chr_init_mem(&mchar);
528 hmp.chr = &mchar;
529
530 old_mon = cur_mon;
531 cur_mon = &hmp;
532
533 if (qdict_haskey(params, "cpu-index")) {
534 ret = monitor_set_cpu(qdict_get_int(params, "cpu-index"));
535 if (ret < 0) {
536 cur_mon = old_mon;
537 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
538 goto out;
539 }
540 }
541
542 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
543 cur_mon = old_mon;
544
545 if (qemu_chr_mem_osize(hmp.chr) > 0) {
546 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
547 }
548
549 out:
550 qemu_chr_close_mem(hmp.chr);
551 return ret;
552 }
553
554 static int compare_cmd(const char *name, const char *list)
555 {
556 const char *p, *pstart;
557 int len;
558 len = strlen(name);
559 p = list;
560 for(;;) {
561 pstart = p;
562 p = strchr(p, '|');
563 if (!p)
564 p = pstart + strlen(pstart);
565 if ((p - pstart) == len && !memcmp(pstart, name, len))
566 return 1;
567 if (*p == '\0')
568 break;
569 p++;
570 }
571 return 0;
572 }
573
574 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
575 const char *prefix, const char *name)
576 {
577 const mon_cmd_t *cmd;
578
579 for(cmd = cmds; cmd->name != NULL; cmd++) {
580 if (!name || !strcmp(name, cmd->name))
581 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
582 cmd->params, cmd->help);
583 }
584 }
585
586 static void help_cmd(Monitor *mon, const char *name)
587 {
588 if (name && !strcmp(name, "info")) {
589 help_cmd_dump(mon, info_cmds, "info ", NULL);
590 } else {
591 help_cmd_dump(mon, mon_cmds, "", name);
592 if (name && !strcmp(name, "log")) {
593 const CPULogItem *item;
594 monitor_printf(mon, "Log items (comma separated):\n");
595 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
596 for(item = cpu_log_items; item->mask != 0; item++) {
597 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
598 }
599 }
600 }
601 }
602
603 static void do_help_cmd(Monitor *mon, const QDict *qdict)
604 {
605 help_cmd(mon, qdict_get_try_str(qdict, "name"));
606 }
607
608 static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
609 {
610 const char *tp_name = qdict_get_str(qdict, "name");
611 bool new_state = qdict_get_bool(qdict, "option");
612 int ret = trace_event_set_state(tp_name, new_state);
613
614 if (!ret) {
615 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
616 }
617 }
618
619 #ifdef CONFIG_TRACE_SIMPLE
620 static void do_trace_file(Monitor *mon, const QDict *qdict)
621 {
622 const char *op = qdict_get_try_str(qdict, "op");
623 const char *arg = qdict_get_try_str(qdict, "arg");
624
625 if (!op) {
626 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
627 } else if (!strcmp(op, "on")) {
628 st_set_trace_file_enabled(true);
629 } else if (!strcmp(op, "off")) {
630 st_set_trace_file_enabled(false);
631 } else if (!strcmp(op, "flush")) {
632 st_flush_trace_buffer();
633 } else if (!strcmp(op, "set")) {
634 if (arg) {
635 st_set_trace_file(arg);
636 }
637 } else {
638 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
639 help_cmd(mon, "trace-file");
640 }
641 }
642 #endif
643
644 static void user_monitor_complete(void *opaque, QObject *ret_data)
645 {
646 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
647
648 if (ret_data) {
649 data->user_print(data->mon, ret_data);
650 }
651 monitor_resume(data->mon);
652 g_free(data);
653 }
654
655 static void qmp_monitor_complete(void *opaque, QObject *ret_data)
656 {
657 monitor_protocol_emitter(opaque, ret_data);
658 }
659
660 static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
661 const QDict *params)
662 {
663 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
664 }
665
666 static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
667 {
668 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
669 }
670
671 static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
672 const QDict *params)
673 {
674 int ret;
675
676 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
677 cb_data->mon = mon;
678 cb_data->user_print = cmd->user_print;
679 monitor_suspend(mon);
680 ret = cmd->mhandler.cmd_async(mon, params,
681 user_monitor_complete, cb_data);
682 if (ret < 0) {
683 monitor_resume(mon);
684 g_free(cb_data);
685 }
686 }
687
688 static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
689 {
690 int ret;
691
692 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
693 cb_data->mon = mon;
694 cb_data->user_print = cmd->user_print;
695 monitor_suspend(mon);
696 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
697 if (ret < 0) {
698 monitor_resume(mon);
699 g_free(cb_data);
700 }
701 }
702
703 static void do_info(Monitor *mon, const QDict *qdict)
704 {
705 const mon_cmd_t *cmd;
706 const char *item = qdict_get_try_str(qdict, "item");
707
708 if (!item) {
709 goto help;
710 }
711
712 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
713 if (compare_cmd(item, cmd->name))
714 break;
715 }
716
717 if (cmd->name == NULL) {
718 goto help;
719 }
720
721 if (handler_is_async(cmd)) {
722 user_async_info_handler(mon, cmd);
723 } else if (handler_is_qobject(cmd)) {
724 QObject *info_data = NULL;
725
726 cmd->mhandler.info_new(mon, &info_data);
727 if (info_data) {
728 cmd->user_print(mon, info_data);
729 qobject_decref(info_data);
730 }
731 } else {
732 cmd->mhandler.info(mon);
733 }
734
735 return;
736
737 help:
738 help_cmd(mon, "info");
739 }
740
741 static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
742 {
743 CommandInfoList *info;
744
745 info = g_malloc0(sizeof(*info));
746 info->value = g_malloc0(sizeof(*info->value));
747 info->value->name = g_strdup(cmd_name);
748
749 return info;
750 }
751
752 CommandInfoList *qmp_query_commands(Error **errp)
753 {
754 CommandInfoList *info, *cmd_list = NULL;
755 const mon_cmd_t *cmd;
756
757 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
758 info = alloc_cmd_entry(cmd->name);
759 info->next = cmd_list;
760 cmd_list = info;
761 }
762
763 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
764 char buf[128];
765 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
766 info = alloc_cmd_entry(buf);
767 info->next = cmd_list;
768 cmd_list = info;
769 }
770
771 return cmd_list;
772 }
773
774 /* set the current CPU defined by the user */
775 int monitor_set_cpu(int cpu_index)
776 {
777 CPUState *env;
778
779 for(env = first_cpu; env != NULL; env = env->next_cpu) {
780 if (env->cpu_index == cpu_index) {
781 cur_mon->mon_cpu = env;
782 return 0;
783 }
784 }
785 return -1;
786 }
787
788 static CPUState *mon_get_cpu(void)
789 {
790 if (!cur_mon->mon_cpu) {
791 monitor_set_cpu(0);
792 }
793 cpu_synchronize_state(cur_mon->mon_cpu);
794 return cur_mon->mon_cpu;
795 }
796
797 int monitor_get_cpu_index(void)
798 {
799 return mon_get_cpu()->cpu_index;
800 }
801
802 static void do_info_registers(Monitor *mon)
803 {
804 CPUState *env;
805 env = mon_get_cpu();
806 #ifdef TARGET_I386
807 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
808 X86_DUMP_FPU);
809 #else
810 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
811 0);
812 #endif
813 }
814
815 static void do_info_jit(Monitor *mon)
816 {
817 dump_exec_info((FILE *)mon, monitor_fprintf);
818 }
819
820 static void do_info_history(Monitor *mon)
821 {
822 int i;
823 const char *str;
824
825 if (!mon->rs)
826 return;
827 i = 0;
828 for(;;) {
829 str = readline_get_history(mon->rs, i);
830 if (!str)
831 break;
832 monitor_printf(mon, "%d: '%s'\n", i, str);
833 i++;
834 }
835 }
836
837 #if defined(TARGET_PPC)
838 /* XXX: not implemented in other targets */
839 static void do_info_cpu_stats(Monitor *mon)
840 {
841 CPUState *env;
842
843 env = mon_get_cpu();
844 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
845 }
846 #endif
847
848 #if defined(CONFIG_TRACE_SIMPLE)
849 static void do_info_trace(Monitor *mon)
850 {
851 st_print_trace((FILE *)mon, &monitor_fprintf);
852 }
853 #endif
854
855 static void do_trace_print_events(Monitor *mon)
856 {
857 trace_print_events((FILE *)mon, &monitor_fprintf);
858 }
859
860 #ifdef CONFIG_VNC
861 static int change_vnc_password(const char *password)
862 {
863 if (!password || !password[0]) {
864 if (vnc_display_disable_login(NULL)) {
865 qerror_report(QERR_SET_PASSWD_FAILED);
866 return -1;
867 }
868 return 0;
869 }
870
871 if (vnc_display_password(NULL, password) < 0) {
872 qerror_report(QERR_SET_PASSWD_FAILED);
873 return -1;
874 }
875
876 return 0;
877 }
878
879 static void change_vnc_password_cb(Monitor *mon, const char *password,
880 void *opaque)
881 {
882 change_vnc_password(password);
883 monitor_read_command(mon, 1);
884 }
885
886 static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
887 {
888 if (strcmp(target, "passwd") == 0 ||
889 strcmp(target, "password") == 0) {
890 if (arg) {
891 char password[9];
892 strncpy(password, arg, sizeof(password));
893 password[sizeof(password) - 1] = '\0';
894 return change_vnc_password(password);
895 } else {
896 return monitor_read_password(mon, change_vnc_password_cb, NULL);
897 }
898 } else {
899 if (vnc_display_open(NULL, target) < 0) {
900 qerror_report(QERR_VNC_SERVER_FAILED, target);
901 return -1;
902 }
903 }
904
905 return 0;
906 }
907 #else
908 static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
909 {
910 qerror_report(QERR_FEATURE_DISABLED, "vnc");
911 return -ENODEV;
912 }
913 #endif
914
915 /**
916 * do_change(): Change a removable medium, or VNC configuration
917 */
918 static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
919 {
920 const char *device = qdict_get_str(qdict, "device");
921 const char *target = qdict_get_str(qdict, "target");
922 const char *arg = qdict_get_try_str(qdict, "arg");
923 int ret;
924
925 if (strcmp(device, "vnc") == 0) {
926 ret = do_change_vnc(mon, target, arg);
927 } else {
928 ret = do_change_block(mon, device, target, arg);
929 }
930
931 return ret;
932 }
933
934 static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
935 {
936 const char *protocol = qdict_get_str(qdict, "protocol");
937 const char *password = qdict_get_str(qdict, "password");
938 const char *connected = qdict_get_try_str(qdict, "connected");
939 int disconnect_if_connected = 0;
940 int fail_if_connected = 0;
941 int rc;
942
943 if (connected) {
944 if (strcmp(connected, "fail") == 0) {
945 fail_if_connected = 1;
946 } else if (strcmp(connected, "disconnect") == 0) {
947 disconnect_if_connected = 1;
948 } else if (strcmp(connected, "keep") == 0) {
949 /* nothing */
950 } else {
951 qerror_report(QERR_INVALID_PARAMETER, "connected");
952 return -1;
953 }
954 }
955
956 if (strcmp(protocol, "spice") == 0) {
957 if (!using_spice) {
958 /* correct one? spice isn't a device ,,, */
959 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
960 return -1;
961 }
962 rc = qemu_spice_set_passwd(password, fail_if_connected,
963 disconnect_if_connected);
964 if (rc != 0) {
965 qerror_report(QERR_SET_PASSWD_FAILED);
966 return -1;
967 }
968 return 0;
969 }
970
971 if (strcmp(protocol, "vnc") == 0) {
972 if (fail_if_connected || disconnect_if_connected) {
973 /* vnc supports "connected=keep" only */
974 qerror_report(QERR_INVALID_PARAMETER, "connected");
975 return -1;
976 }
977 /* Note that setting an empty password will not disable login through
978 * this interface. */
979 return vnc_display_password(NULL, password);
980 }
981
982 qerror_report(QERR_INVALID_PARAMETER, "protocol");
983 return -1;
984 }
985
986 static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
987 {
988 const char *protocol = qdict_get_str(qdict, "protocol");
989 const char *whenstr = qdict_get_str(qdict, "time");
990 time_t when;
991 int rc;
992
993 if (strcmp(whenstr, "now") == 0) {
994 when = 0;
995 } else if (strcmp(whenstr, "never") == 0) {
996 when = TIME_MAX;
997 } else if (whenstr[0] == '+') {
998 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
999 } else {
1000 when = strtoull(whenstr, NULL, 10);
1001 }
1002
1003 if (strcmp(protocol, "spice") == 0) {
1004 if (!using_spice) {
1005 /* correct one? spice isn't a device ,,, */
1006 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1007 return -1;
1008 }
1009 rc = qemu_spice_set_pw_expire(when);
1010 if (rc != 0) {
1011 qerror_report(QERR_SET_PASSWD_FAILED);
1012 return -1;
1013 }
1014 return 0;
1015 }
1016
1017 if (strcmp(protocol, "vnc") == 0) {
1018 return vnc_display_pw_expire(NULL, when);
1019 }
1020
1021 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1022 return -1;
1023 }
1024
1025 static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1026 {
1027 const char *protocol = qdict_get_str(qdict, "protocol");
1028 const char *fdname = qdict_get_str(qdict, "fdname");
1029 CharDriverState *s;
1030
1031 if (strcmp(protocol, "spice") == 0) {
1032 if (!using_spice) {
1033 /* correct one? spice isn't a device ,,, */
1034 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1035 return -1;
1036 }
1037 qerror_report(QERR_ADD_CLIENT_FAILED);
1038 return -1;
1039 #ifdef CONFIG_VNC
1040 } else if (strcmp(protocol, "vnc") == 0) {
1041 int fd = monitor_get_fd(mon, fdname);
1042 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
1043 vnc_display_add_client(NULL, fd, skipauth);
1044 return 0;
1045 #endif
1046 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1047 int fd = monitor_get_fd(mon, fdname);
1048 if (qemu_chr_add_client(s, fd) < 0) {
1049 qerror_report(QERR_ADD_CLIENT_FAILED);
1050 return -1;
1051 }
1052 return 0;
1053 }
1054
1055 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1056 return -1;
1057 }
1058
1059 static int client_migrate_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
1060 {
1061 const char *protocol = qdict_get_str(qdict, "protocol");
1062 const char *hostname = qdict_get_str(qdict, "hostname");
1063 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1064 int port = qdict_get_try_int(qdict, "port", -1);
1065 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1066 int ret;
1067
1068 if (strcmp(protocol, "spice") == 0) {
1069 if (!using_spice) {
1070 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1071 return -1;
1072 }
1073
1074 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject);
1075 if (ret != 0) {
1076 qerror_report(QERR_UNDEFINED_ERROR);
1077 return -1;
1078 }
1079 return 0;
1080 }
1081
1082 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1083 return -1;
1084 }
1085
1086 static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
1087 {
1088 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
1089 return 0;
1090 }
1091
1092 static void do_logfile(Monitor *mon, const QDict *qdict)
1093 {
1094 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
1095 }
1096
1097 static void do_log(Monitor *mon, const QDict *qdict)
1098 {
1099 int mask;
1100 const char *items = qdict_get_str(qdict, "items");
1101
1102 if (!strcmp(items, "none")) {
1103 mask = 0;
1104 } else {
1105 mask = cpu_str_to_log_mask(items);
1106 if (!mask) {
1107 help_cmd(mon, "log");
1108 return;
1109 }
1110 }
1111 cpu_set_log(mask);
1112 }
1113
1114 static void do_singlestep(Monitor *mon, const QDict *qdict)
1115 {
1116 const char *option = qdict_get_try_str(qdict, "option");
1117 if (!option || !strcmp(option, "on")) {
1118 singlestep = 1;
1119 } else if (!strcmp(option, "off")) {
1120 singlestep = 0;
1121 } else {
1122 monitor_printf(mon, "unexpected option %s\n", option);
1123 }
1124 }
1125
1126 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
1127
1128 struct bdrv_iterate_context {
1129 Monitor *mon;
1130 int err;
1131 };
1132
1133 static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1134 {
1135 bdrv_iostatus_reset(bs);
1136 }
1137
1138 /**
1139 * do_cont(): Resume emulation.
1140 */
1141 static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
1142 {
1143 struct bdrv_iterate_context context = { mon, 0 };
1144
1145 if (runstate_check(RUN_STATE_INMIGRATE)) {
1146 qerror_report(QERR_MIGRATION_EXPECTED);
1147 return -1;
1148 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1149 runstate_check(RUN_STATE_SHUTDOWN)) {
1150 qerror_report(QERR_RESET_REQUIRED);
1151 return -1;
1152 }
1153
1154 bdrv_iterate(iostatus_bdrv_it, NULL);
1155 bdrv_iterate(encrypted_bdrv_it, &context);
1156 /* only resume the vm if all keys are set and valid */
1157 if (!context.err) {
1158 vm_start();
1159 return 0;
1160 } else {
1161 return -1;
1162 }
1163 }
1164
1165 static void bdrv_key_cb(void *opaque, int err)
1166 {
1167 Monitor *mon = opaque;
1168
1169 /* another key was set successfully, retry to continue */
1170 if (!err)
1171 do_cont(mon, NULL, NULL);
1172 }
1173
1174 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1175 {
1176 struct bdrv_iterate_context *context = opaque;
1177
1178 if (!context->err && bdrv_key_required(bs)) {
1179 context->err = -EBUSY;
1180 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1181 context->mon);
1182 }
1183 }
1184
1185 static void do_gdbserver(Monitor *mon, const QDict *qdict)
1186 {
1187 const char *device = qdict_get_try_str(qdict, "device");
1188 if (!device)
1189 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1190 if (gdbserver_start(device) < 0) {
1191 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1192 device);
1193 } else if (strcmp(device, "none") == 0) {
1194 monitor_printf(mon, "Disabled gdbserver\n");
1195 } else {
1196 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1197 device);
1198 }
1199 }
1200
1201 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
1202 {
1203 const char *action = qdict_get_str(qdict, "action");
1204 if (select_watchdog_action(action) == -1) {
1205 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1206 }
1207 }
1208
1209 static void monitor_printc(Monitor *mon, int c)
1210 {
1211 monitor_printf(mon, "'");
1212 switch(c) {
1213 case '\'':
1214 monitor_printf(mon, "\\'");
1215 break;
1216 case '\\':
1217 monitor_printf(mon, "\\\\");
1218 break;
1219 case '\n':
1220 monitor_printf(mon, "\\n");
1221 break;
1222 case '\r':
1223 monitor_printf(mon, "\\r");
1224 break;
1225 default:
1226 if (c >= 32 && c <= 126) {
1227 monitor_printf(mon, "%c", c);
1228 } else {
1229 monitor_printf(mon, "\\x%02x", c);
1230 }
1231 break;
1232 }
1233 monitor_printf(mon, "'");
1234 }
1235
1236 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1237 target_phys_addr_t addr, int is_physical)
1238 {
1239 CPUState *env;
1240 int l, line_size, i, max_digits, len;
1241 uint8_t buf[16];
1242 uint64_t v;
1243
1244 if (format == 'i') {
1245 int flags;
1246 flags = 0;
1247 env = mon_get_cpu();
1248 #ifdef TARGET_I386
1249 if (wsize == 2) {
1250 flags = 1;
1251 } else if (wsize == 4) {
1252 flags = 0;
1253 } else {
1254 /* as default we use the current CS size */
1255 flags = 0;
1256 if (env) {
1257 #ifdef TARGET_X86_64
1258 if ((env->efer & MSR_EFER_LMA) &&
1259 (env->segs[R_CS].flags & DESC_L_MASK))
1260 flags = 2;
1261 else
1262 #endif
1263 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1264 flags = 1;
1265 }
1266 }
1267 #endif
1268 monitor_disas(mon, env, addr, count, is_physical, flags);
1269 return;
1270 }
1271
1272 len = wsize * count;
1273 if (wsize == 1)
1274 line_size = 8;
1275 else
1276 line_size = 16;
1277 max_digits = 0;
1278
1279 switch(format) {
1280 case 'o':
1281 max_digits = (wsize * 8 + 2) / 3;
1282 break;
1283 default:
1284 case 'x':
1285 max_digits = (wsize * 8) / 4;
1286 break;
1287 case 'u':
1288 case 'd':
1289 max_digits = (wsize * 8 * 10 + 32) / 33;
1290 break;
1291 case 'c':
1292 wsize = 1;
1293 break;
1294 }
1295
1296 while (len > 0) {
1297 if (is_physical)
1298 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1299 else
1300 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1301 l = len;
1302 if (l > line_size)
1303 l = line_size;
1304 if (is_physical) {
1305 cpu_physical_memory_read(addr, buf, l);
1306 } else {
1307 env = mon_get_cpu();
1308 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
1309 monitor_printf(mon, " Cannot access memory\n");
1310 break;
1311 }
1312 }
1313 i = 0;
1314 while (i < l) {
1315 switch(wsize) {
1316 default:
1317 case 1:
1318 v = ldub_raw(buf + i);
1319 break;
1320 case 2:
1321 v = lduw_raw(buf + i);
1322 break;
1323 case 4:
1324 v = (uint32_t)ldl_raw(buf + i);
1325 break;
1326 case 8:
1327 v = ldq_raw(buf + i);
1328 break;
1329 }
1330 monitor_printf(mon, " ");
1331 switch(format) {
1332 case 'o':
1333 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1334 break;
1335 case 'x':
1336 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1337 break;
1338 case 'u':
1339 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1340 break;
1341 case 'd':
1342 monitor_printf(mon, "%*" PRId64, max_digits, v);
1343 break;
1344 case 'c':
1345 monitor_printc(mon, v);
1346 break;
1347 }
1348 i += wsize;
1349 }
1350 monitor_printf(mon, "\n");
1351 addr += l;
1352 len -= l;
1353 }
1354 }
1355
1356 static void do_memory_dump(Monitor *mon, const QDict *qdict)
1357 {
1358 int count = qdict_get_int(qdict, "count");
1359 int format = qdict_get_int(qdict, "format");
1360 int size = qdict_get_int(qdict, "size");
1361 target_long addr = qdict_get_int(qdict, "addr");
1362
1363 memory_dump(mon, count, format, size, addr, 0);
1364 }
1365
1366 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1367 {
1368 int count = qdict_get_int(qdict, "count");
1369 int format = qdict_get_int(qdict, "format");
1370 int size = qdict_get_int(qdict, "size");
1371 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
1372
1373 memory_dump(mon, count, format, size, addr, 1);
1374 }
1375
1376 static void do_print(Monitor *mon, const QDict *qdict)
1377 {
1378 int format = qdict_get_int(qdict, "format");
1379 target_phys_addr_t val = qdict_get_int(qdict, "val");
1380
1381 #if TARGET_PHYS_ADDR_BITS == 32
1382 switch(format) {
1383 case 'o':
1384 monitor_printf(mon, "%#o", val);
1385 break;
1386 case 'x':
1387 monitor_printf(mon, "%#x", val);
1388 break;
1389 case 'u':
1390 monitor_printf(mon, "%u", val);
1391 break;
1392 default:
1393 case 'd':
1394 monitor_printf(mon, "%d", val);
1395 break;
1396 case 'c':
1397 monitor_printc(mon, val);
1398 break;
1399 }
1400 #else
1401 switch(format) {
1402 case 'o':
1403 monitor_printf(mon, "%#" PRIo64, val);
1404 break;
1405 case 'x':
1406 monitor_printf(mon, "%#" PRIx64, val);
1407 break;
1408 case 'u':
1409 monitor_printf(mon, "%" PRIu64, val);
1410 break;
1411 default:
1412 case 'd':
1413 monitor_printf(mon, "%" PRId64, val);
1414 break;
1415 case 'c':
1416 monitor_printc(mon, val);
1417 break;
1418 }
1419 #endif
1420 monitor_printf(mon, "\n");
1421 }
1422
1423 static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
1424 {
1425 FILE *f;
1426 uint32_t size = qdict_get_int(qdict, "size");
1427 const char *filename = qdict_get_str(qdict, "filename");
1428 target_long addr = qdict_get_int(qdict, "val");
1429 uint32_t l;
1430 CPUState *env;
1431 uint8_t buf[1024];
1432 int ret = -1;
1433
1434 env = mon_get_cpu();
1435
1436 f = fopen(filename, "wb");
1437 if (!f) {
1438 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1439 return -1;
1440 }
1441 while (size != 0) {
1442 l = sizeof(buf);
1443 if (l > size)
1444 l = size;
1445 cpu_memory_rw_debug(env, addr, buf, l, 0);
1446 if (fwrite(buf, 1, l, f) != l) {
1447 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1448 goto exit;
1449 }
1450 addr += l;
1451 size -= l;
1452 }
1453
1454 ret = 0;
1455
1456 exit:
1457 fclose(f);
1458 return ret;
1459 }
1460
1461 static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
1462 QObject **ret_data)
1463 {
1464 FILE *f;
1465 uint32_t l;
1466 uint8_t buf[1024];
1467 uint32_t size = qdict_get_int(qdict, "size");
1468 const char *filename = qdict_get_str(qdict, "filename");
1469 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1470 int ret = -1;
1471
1472 f = fopen(filename, "wb");
1473 if (!f) {
1474 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1475 return -1;
1476 }
1477 while (size != 0) {
1478 l = sizeof(buf);
1479 if (l > size)
1480 l = size;
1481 cpu_physical_memory_read(addr, buf, l);
1482 if (fwrite(buf, 1, l, f) != l) {
1483 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1484 goto exit;
1485 }
1486 fflush(f);
1487 addr += l;
1488 size -= l;
1489 }
1490
1491 ret = 0;
1492
1493 exit:
1494 fclose(f);
1495 return ret;
1496 }
1497
1498 static void do_sum(Monitor *mon, const QDict *qdict)
1499 {
1500 uint32_t addr;
1501 uint16_t sum;
1502 uint32_t start = qdict_get_int(qdict, "start");
1503 uint32_t size = qdict_get_int(qdict, "size");
1504
1505 sum = 0;
1506 for(addr = start; addr < (start + size); addr++) {
1507 uint8_t val = ldub_phys(addr);
1508 /* BSD sum algorithm ('sum' Unix command) */
1509 sum = (sum >> 1) | (sum << 15);
1510 sum += val;
1511 }
1512 monitor_printf(mon, "%05d\n", sum);
1513 }
1514
1515 typedef struct {
1516 int keycode;
1517 const char *name;
1518 } KeyDef;
1519
1520 static const KeyDef key_defs[] = {
1521 { 0x2a, "shift" },
1522 { 0x36, "shift_r" },
1523
1524 { 0x38, "alt" },
1525 { 0xb8, "alt_r" },
1526 { 0x64, "altgr" },
1527 { 0xe4, "altgr_r" },
1528 { 0x1d, "ctrl" },
1529 { 0x9d, "ctrl_r" },
1530
1531 { 0xdd, "menu" },
1532
1533 { 0x01, "esc" },
1534
1535 { 0x02, "1" },
1536 { 0x03, "2" },
1537 { 0x04, "3" },
1538 { 0x05, "4" },
1539 { 0x06, "5" },
1540 { 0x07, "6" },
1541 { 0x08, "7" },
1542 { 0x09, "8" },
1543 { 0x0a, "9" },
1544 { 0x0b, "0" },
1545 { 0x0c, "minus" },
1546 { 0x0d, "equal" },
1547 { 0x0e, "backspace" },
1548
1549 { 0x0f, "tab" },
1550 { 0x10, "q" },
1551 { 0x11, "w" },
1552 { 0x12, "e" },
1553 { 0x13, "r" },
1554 { 0x14, "t" },
1555 { 0x15, "y" },
1556 { 0x16, "u" },
1557 { 0x17, "i" },
1558 { 0x18, "o" },
1559 { 0x19, "p" },
1560 { 0x1a, "bracket_left" },
1561 { 0x1b, "bracket_right" },
1562 { 0x1c, "ret" },
1563
1564 { 0x1e, "a" },
1565 { 0x1f, "s" },
1566 { 0x20, "d" },
1567 { 0x21, "f" },
1568 { 0x22, "g" },
1569 { 0x23, "h" },
1570 { 0x24, "j" },
1571 { 0x25, "k" },
1572 { 0x26, "l" },
1573 { 0x27, "semicolon" },
1574 { 0x28, "apostrophe" },
1575 { 0x29, "grave_accent" },
1576
1577 { 0x2b, "backslash" },
1578 { 0x2c, "z" },
1579 { 0x2d, "x" },
1580 { 0x2e, "c" },
1581 { 0x2f, "v" },
1582 { 0x30, "b" },
1583 { 0x31, "n" },
1584 { 0x32, "m" },
1585 { 0x33, "comma" },
1586 { 0x34, "dot" },
1587 { 0x35, "slash" },
1588
1589 { 0x37, "asterisk" },
1590
1591 { 0x39, "spc" },
1592 { 0x3a, "caps_lock" },
1593 { 0x3b, "f1" },
1594 { 0x3c, "f2" },
1595 { 0x3d, "f3" },
1596 { 0x3e, "f4" },
1597 { 0x3f, "f5" },
1598 { 0x40, "f6" },
1599 { 0x41, "f7" },
1600 { 0x42, "f8" },
1601 { 0x43, "f9" },
1602 { 0x44, "f10" },
1603 { 0x45, "num_lock" },
1604 { 0x46, "scroll_lock" },
1605
1606 { 0xb5, "kp_divide" },
1607 { 0x37, "kp_multiply" },
1608 { 0x4a, "kp_subtract" },
1609 { 0x4e, "kp_add" },
1610 { 0x9c, "kp_enter" },
1611 { 0x53, "kp_decimal" },
1612 { 0x54, "sysrq" },
1613
1614 { 0x52, "kp_0" },
1615 { 0x4f, "kp_1" },
1616 { 0x50, "kp_2" },
1617 { 0x51, "kp_3" },
1618 { 0x4b, "kp_4" },
1619 { 0x4c, "kp_5" },
1620 { 0x4d, "kp_6" },
1621 { 0x47, "kp_7" },
1622 { 0x48, "kp_8" },
1623 { 0x49, "kp_9" },
1624
1625 { 0x56, "<" },
1626
1627 { 0x57, "f11" },
1628 { 0x58, "f12" },
1629
1630 { 0xb7, "print" },
1631
1632 { 0xc7, "home" },
1633 { 0xc9, "pgup" },
1634 { 0xd1, "pgdn" },
1635 { 0xcf, "end" },
1636
1637 { 0xcb, "left" },
1638 { 0xc8, "up" },
1639 { 0xd0, "down" },
1640 { 0xcd, "right" },
1641
1642 { 0xd2, "insert" },
1643 { 0xd3, "delete" },
1644 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1645 { 0xf0, "stop" },
1646 { 0xf1, "again" },
1647 { 0xf2, "props" },
1648 { 0xf3, "undo" },
1649 { 0xf4, "front" },
1650 { 0xf5, "copy" },
1651 { 0xf6, "open" },
1652 { 0xf7, "paste" },
1653 { 0xf8, "find" },
1654 { 0xf9, "cut" },
1655 { 0xfa, "lf" },
1656 { 0xfb, "help" },
1657 { 0xfc, "meta_l" },
1658 { 0xfd, "meta_r" },
1659 { 0xfe, "compose" },
1660 #endif
1661 { 0, NULL },
1662 };
1663
1664 static int get_keycode(const char *key)
1665 {
1666 const KeyDef *p;
1667 char *endp;
1668 int ret;
1669
1670 for(p = key_defs; p->name != NULL; p++) {
1671 if (!strcmp(key, p->name))
1672 return p->keycode;
1673 }
1674 if (strstart(key, "0x", NULL)) {
1675 ret = strtoul(key, &endp, 0);
1676 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1677 return ret;
1678 }
1679 return -1;
1680 }
1681
1682 #define MAX_KEYCODES 16
1683 static uint8_t keycodes[MAX_KEYCODES];
1684 static int nb_pending_keycodes;
1685 static QEMUTimer *key_timer;
1686
1687 static void release_keys(void *opaque)
1688 {
1689 int keycode;
1690
1691 while (nb_pending_keycodes > 0) {
1692 nb_pending_keycodes--;
1693 keycode = keycodes[nb_pending_keycodes];
1694 if (keycode & 0x80)
1695 kbd_put_keycode(0xe0);
1696 kbd_put_keycode(keycode | 0x80);
1697 }
1698 }
1699
1700 static void do_sendkey(Monitor *mon, const QDict *qdict)
1701 {
1702 char keyname_buf[16];
1703 char *separator;
1704 int keyname_len, keycode, i;
1705 const char *string = qdict_get_str(qdict, "string");
1706 int has_hold_time = qdict_haskey(qdict, "hold_time");
1707 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1708
1709 if (nb_pending_keycodes > 0) {
1710 qemu_del_timer(key_timer);
1711 release_keys(NULL);
1712 }
1713 if (!has_hold_time)
1714 hold_time = 100;
1715 i = 0;
1716 while (1) {
1717 separator = strchr(string, '-');
1718 keyname_len = separator ? separator - string : strlen(string);
1719 if (keyname_len > 0) {
1720 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1721 if (keyname_len > sizeof(keyname_buf) - 1) {
1722 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1723 return;
1724 }
1725 if (i == MAX_KEYCODES) {
1726 monitor_printf(mon, "too many keys\n");
1727 return;
1728 }
1729 keyname_buf[keyname_len] = 0;
1730 keycode = get_keycode(keyname_buf);
1731 if (keycode < 0) {
1732 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1733 return;
1734 }
1735 keycodes[i++] = keycode;
1736 }
1737 if (!separator)
1738 break;
1739 string = separator + 1;
1740 }
1741 nb_pending_keycodes = i;
1742 /* key down events */
1743 for (i = 0; i < nb_pending_keycodes; i++) {
1744 keycode = keycodes[i];
1745 if (keycode & 0x80)
1746 kbd_put_keycode(0xe0);
1747 kbd_put_keycode(keycode & 0x7f);
1748 }
1749 /* delayed key up events */
1750 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
1751 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1752 }
1753
1754 static int mouse_button_state;
1755
1756 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1757 {
1758 int dx, dy, dz;
1759 const char *dx_str = qdict_get_str(qdict, "dx_str");
1760 const char *dy_str = qdict_get_str(qdict, "dy_str");
1761 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1762 dx = strtol(dx_str, NULL, 0);
1763 dy = strtol(dy_str, NULL, 0);
1764 dz = 0;
1765 if (dz_str)
1766 dz = strtol(dz_str, NULL, 0);
1767 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1768 }
1769
1770 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1771 {
1772 int button_state = qdict_get_int(qdict, "button_state");
1773 mouse_button_state = button_state;
1774 kbd_mouse_event(0, 0, 0, mouse_button_state);
1775 }
1776
1777 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1778 {
1779 int size = qdict_get_int(qdict, "size");
1780 int addr = qdict_get_int(qdict, "addr");
1781 int has_index = qdict_haskey(qdict, "index");
1782 uint32_t val;
1783 int suffix;
1784
1785 if (has_index) {
1786 int index = qdict_get_int(qdict, "index");
1787 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1788 addr++;
1789 }
1790 addr &= 0xffff;
1791
1792 switch(size) {
1793 default:
1794 case 1:
1795 val = cpu_inb(addr);
1796 suffix = 'b';
1797 break;
1798 case 2:
1799 val = cpu_inw(addr);
1800 suffix = 'w';
1801 break;
1802 case 4:
1803 val = cpu_inl(addr);
1804 suffix = 'l';
1805 break;
1806 }
1807 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1808 suffix, addr, size * 2, val);
1809 }
1810
1811 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1812 {
1813 int size = qdict_get_int(qdict, "size");
1814 int addr = qdict_get_int(qdict, "addr");
1815 int val = qdict_get_int(qdict, "val");
1816
1817 addr &= IOPORTS_MASK;
1818
1819 switch (size) {
1820 default:
1821 case 1:
1822 cpu_outb(addr, val);
1823 break;
1824 case 2:
1825 cpu_outw(addr, val);
1826 break;
1827 case 4:
1828 cpu_outl(addr, val);
1829 break;
1830 }
1831 }
1832
1833 static void do_boot_set(Monitor *mon, const QDict *qdict)
1834 {
1835 int res;
1836 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1837
1838 res = qemu_boot_set(bootdevice);
1839 if (res == 0) {
1840 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1841 } else if (res > 0) {
1842 monitor_printf(mon, "setting boot device list failed\n");
1843 } else {
1844 monitor_printf(mon, "no function defined to set boot device list for "
1845 "this architecture\n");
1846 }
1847 }
1848
1849 /**
1850 * do_system_powerdown(): Issue a machine powerdown
1851 */
1852 static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1853 QObject **ret_data)
1854 {
1855 qemu_system_powerdown_request();
1856 return 0;
1857 }
1858
1859 #if defined(TARGET_I386)
1860 static void print_pte(Monitor *mon, target_phys_addr_t addr,
1861 target_phys_addr_t pte,
1862 target_phys_addr_t mask)
1863 {
1864 #ifdef TARGET_X86_64
1865 if (addr & (1ULL << 47)) {
1866 addr |= -1LL << 48;
1867 }
1868 #endif
1869 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1870 " %c%c%c%c%c%c%c%c%c\n",
1871 addr,
1872 pte & mask,
1873 pte & PG_NX_MASK ? 'X' : '-',
1874 pte & PG_GLOBAL_MASK ? 'G' : '-',
1875 pte & PG_PSE_MASK ? 'P' : '-',
1876 pte & PG_DIRTY_MASK ? 'D' : '-',
1877 pte & PG_ACCESSED_MASK ? 'A' : '-',
1878 pte & PG_PCD_MASK ? 'C' : '-',
1879 pte & PG_PWT_MASK ? 'T' : '-',
1880 pte & PG_USER_MASK ? 'U' : '-',
1881 pte & PG_RW_MASK ? 'W' : '-');
1882 }
1883
1884 static void tlb_info_32(Monitor *mon, CPUState *env)
1885 {
1886 unsigned int l1, l2;
1887 uint32_t pgd, pde, pte;
1888
1889 pgd = env->cr[3] & ~0xfff;
1890 for(l1 = 0; l1 < 1024; l1++) {
1891 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1892 pde = le32_to_cpu(pde);
1893 if (pde & PG_PRESENT_MASK) {
1894 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1895 /* 4M pages */
1896 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1897 } else {
1898 for(l2 = 0; l2 < 1024; l2++) {
1899 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1900 pte = le32_to_cpu(pte);
1901 if (pte & PG_PRESENT_MASK) {
1902 print_pte(mon, (l1 << 22) + (l2 << 12),
1903 pte & ~PG_PSE_MASK,
1904 ~0xfff);
1905 }
1906 }
1907 }
1908 }
1909 }
1910 }
1911
1912 static void tlb_info_pae32(Monitor *mon, CPUState *env)
1913 {
1914 unsigned int l1, l2, l3;
1915 uint64_t pdpe, pde, pte;
1916 uint64_t pdp_addr, pd_addr, pt_addr;
1917
1918 pdp_addr = env->cr[3] & ~0x1f;
1919 for (l1 = 0; l1 < 4; l1++) {
1920 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1921 pdpe = le64_to_cpu(pdpe);
1922 if (pdpe & PG_PRESENT_MASK) {
1923 pd_addr = pdpe & 0x3fffffffff000ULL;
1924 for (l2 = 0; l2 < 512; l2++) {
1925 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1926 pde = le64_to_cpu(pde);
1927 if (pde & PG_PRESENT_MASK) {
1928 if (pde & PG_PSE_MASK) {
1929 /* 2M pages with PAE, CR4.PSE is ignored */
1930 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1931 ~((target_phys_addr_t)(1 << 20) - 1));
1932 } else {
1933 pt_addr = pde & 0x3fffffffff000ULL;
1934 for (l3 = 0; l3 < 512; l3++) {
1935 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1936 pte = le64_to_cpu(pte);
1937 if (pte & PG_PRESENT_MASK) {
1938 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1939 + (l3 << 12),
1940 pte & ~PG_PSE_MASK,
1941 ~(target_phys_addr_t)0xfff);
1942 }
1943 }
1944 }
1945 }
1946 }
1947 }
1948 }
1949 }
1950
1951 #ifdef TARGET_X86_64
1952 static void tlb_info_64(Monitor *mon, CPUState *env)
1953 {
1954 uint64_t l1, l2, l3, l4;
1955 uint64_t pml4e, pdpe, pde, pte;
1956 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1957
1958 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1959 for (l1 = 0; l1 < 512; l1++) {
1960 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1961 pml4e = le64_to_cpu(pml4e);
1962 if (pml4e & PG_PRESENT_MASK) {
1963 pdp_addr = pml4e & 0x3fffffffff000ULL;
1964 for (l2 = 0; l2 < 512; l2++) {
1965 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1966 pdpe = le64_to_cpu(pdpe);
1967 if (pdpe & PG_PRESENT_MASK) {
1968 if (pdpe & PG_PSE_MASK) {
1969 /* 1G pages, CR4.PSE is ignored */
1970 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1971 0x3ffffc0000000ULL);
1972 } else {
1973 pd_addr = pdpe & 0x3fffffffff000ULL;
1974 for (l3 = 0; l3 < 512; l3++) {
1975 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1976 pde = le64_to_cpu(pde);
1977 if (pde & PG_PRESENT_MASK) {
1978 if (pde & PG_PSE_MASK) {
1979 /* 2M pages, CR4.PSE is ignored */
1980 print_pte(mon, (l1 << 39) + (l2 << 30) +
1981 (l3 << 21), pde,
1982 0x3ffffffe00000ULL);
1983 } else {
1984 pt_addr = pde & 0x3fffffffff000ULL;
1985 for (l4 = 0; l4 < 512; l4++) {
1986 cpu_physical_memory_read(pt_addr
1987 + l4 * 8,
1988 &pte, 8);
1989 pte = le64_to_cpu(pte);
1990 if (pte & PG_PRESENT_MASK) {
1991 print_pte(mon, (l1 << 39) +
1992 (l2 << 30) +
1993 (l3 << 21) + (l4 << 12),
1994 pte & ~PG_PSE_MASK,
1995 0x3fffffffff000ULL);
1996 }
1997 }
1998 }
1999 }
2000 }
2001 }
2002 }
2003 }
2004 }
2005 }
2006 }
2007 #endif
2008
2009 static void tlb_info(Monitor *mon)
2010 {
2011 CPUState *env;
2012
2013 env = mon_get_cpu();
2014
2015 if (!(env->cr[0] & CR0_PG_MASK)) {
2016 monitor_printf(mon, "PG disabled\n");
2017 return;
2018 }
2019 if (env->cr[4] & CR4_PAE_MASK) {
2020 #ifdef TARGET_X86_64
2021 if (env->hflags & HF_LMA_MASK) {
2022 tlb_info_64(mon, env);
2023 } else
2024 #endif
2025 {
2026 tlb_info_pae32(mon, env);
2027 }
2028 } else {
2029 tlb_info_32(mon, env);
2030 }
2031 }
2032
2033 static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2034 int *plast_prot,
2035 target_phys_addr_t end, int prot)
2036 {
2037 int prot1;
2038 prot1 = *plast_prot;
2039 if (prot != prot1) {
2040 if (*pstart != -1) {
2041 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2042 TARGET_FMT_plx " %c%c%c\n",
2043 *pstart, end, end - *pstart,
2044 prot1 & PG_USER_MASK ? 'u' : '-',
2045 'r',
2046 prot1 & PG_RW_MASK ? 'w' : '-');
2047 }
2048 if (prot != 0)
2049 *pstart = end;
2050 else
2051 *pstart = -1;
2052 *plast_prot = prot;
2053 }
2054 }
2055
2056 static void mem_info_32(Monitor *mon, CPUState *env)
2057 {
2058 unsigned int l1, l2;
2059 int prot, last_prot;
2060 uint32_t pgd, pde, pte;
2061 target_phys_addr_t start, end;
2062
2063 pgd = env->cr[3] & ~0xfff;
2064 last_prot = 0;
2065 start = -1;
2066 for(l1 = 0; l1 < 1024; l1++) {
2067 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
2068 pde = le32_to_cpu(pde);
2069 end = l1 << 22;
2070 if (pde & PG_PRESENT_MASK) {
2071 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2072 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2073 mem_print(mon, &start, &last_prot, end, prot);
2074 } else {
2075 for(l2 = 0; l2 < 1024; l2++) {
2076 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
2077 pte = le32_to_cpu(pte);
2078 end = (l1 << 22) + (l2 << 12);
2079 if (pte & PG_PRESENT_MASK) {
2080 prot = pte & pde &
2081 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2082 } else {
2083 prot = 0;
2084 }
2085 mem_print(mon, &start, &last_prot, end, prot);
2086 }
2087 }
2088 } else {
2089 prot = 0;
2090 mem_print(mon, &start, &last_prot, end, prot);
2091 }
2092 }
2093 /* Flush last range */
2094 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
2095 }
2096
2097 static void mem_info_pae32(Monitor *mon, CPUState *env)
2098 {
2099 unsigned int l1, l2, l3;
2100 int prot, last_prot;
2101 uint64_t pdpe, pde, pte;
2102 uint64_t pdp_addr, pd_addr, pt_addr;
2103 target_phys_addr_t start, end;
2104
2105 pdp_addr = env->cr[3] & ~0x1f;
2106 last_prot = 0;
2107 start = -1;
2108 for (l1 = 0; l1 < 4; l1++) {
2109 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
2110 pdpe = le64_to_cpu(pdpe);
2111 end = l1 << 30;
2112 if (pdpe & PG_PRESENT_MASK) {
2113 pd_addr = pdpe & 0x3fffffffff000ULL;
2114 for (l2 = 0; l2 < 512; l2++) {
2115 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
2116 pde = le64_to_cpu(pde);
2117 end = (l1 << 30) + (l2 << 21);
2118 if (pde & PG_PRESENT_MASK) {
2119 if (pde & PG_PSE_MASK) {
2120 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2121 PG_PRESENT_MASK);
2122 mem_print(mon, &start, &last_prot, end, prot);
2123 } else {
2124 pt_addr = pde & 0x3fffffffff000ULL;
2125 for (l3 = 0; l3 < 512; l3++) {
2126 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
2127 pte = le64_to_cpu(pte);
2128 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2129 if (pte & PG_PRESENT_MASK) {
2130 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2131 PG_PRESENT_MASK);
2132 } else {
2133 prot = 0;
2134 }
2135 mem_print(mon, &start, &last_prot, end, prot);
2136 }
2137 }
2138 } else {
2139 prot = 0;
2140 mem_print(mon, &start, &last_prot, end, prot);
2141 }
2142 }
2143 } else {
2144 prot = 0;
2145 mem_print(mon, &start, &last_prot, end, prot);
2146 }
2147 }
2148 /* Flush last range */
2149 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
2150 }
2151
2152
2153 #ifdef TARGET_X86_64
2154 static void mem_info_64(Monitor *mon, CPUState *env)
2155 {
2156 int prot, last_prot;
2157 uint64_t l1, l2, l3, l4;
2158 uint64_t pml4e, pdpe, pde, pte;
2159 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2160
2161 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2162 last_prot = 0;
2163 start = -1;
2164 for (l1 = 0; l1 < 512; l1++) {
2165 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
2166 pml4e = le64_to_cpu(pml4e);
2167 end = l1 << 39;
2168 if (pml4e & PG_PRESENT_MASK) {
2169 pdp_addr = pml4e & 0x3fffffffff000ULL;
2170 for (l2 = 0; l2 < 512; l2++) {
2171 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
2172 pdpe = le64_to_cpu(pdpe);
2173 end = (l1 << 39) + (l2 << 30);
2174 if (pdpe & PG_PRESENT_MASK) {
2175 if (pdpe & PG_PSE_MASK) {
2176 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2177 PG_PRESENT_MASK);
2178 prot &= pml4e;
2179 mem_print(mon, &start, &last_prot, end, prot);
2180 } else {
2181 pd_addr = pdpe & 0x3fffffffff000ULL;
2182 for (l3 = 0; l3 < 512; l3++) {
2183 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
2184 pde = le64_to_cpu(pde);
2185 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2186 if (pde & PG_PRESENT_MASK) {
2187 if (pde & PG_PSE_MASK) {
2188 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2189 PG_PRESENT_MASK);
2190 prot &= pml4e & pdpe;
2191 mem_print(mon, &start, &last_prot, end, prot);
2192 } else {
2193 pt_addr = pde & 0x3fffffffff000ULL;
2194 for (l4 = 0; l4 < 512; l4++) {
2195 cpu_physical_memory_read(pt_addr
2196 + l4 * 8,
2197 &pte, 8);
2198 pte = le64_to_cpu(pte);
2199 end = (l1 << 39) + (l2 << 30) +
2200 (l3 << 21) + (l4 << 12);
2201 if (pte & PG_PRESENT_MASK) {
2202 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2203 PG_PRESENT_MASK);
2204 prot &= pml4e & pdpe & pde;
2205 } else {
2206 prot = 0;
2207 }
2208 mem_print(mon, &start, &last_prot, end, prot);
2209 }
2210 }
2211 } else {
2212 prot = 0;
2213 mem_print(mon, &start, &last_prot, end, prot);
2214 }
2215 }
2216 }
2217 } else {
2218 prot = 0;
2219 mem_print(mon, &start, &last_prot, end, prot);
2220 }
2221 }
2222 } else {
2223 prot = 0;
2224 mem_print(mon, &start, &last_prot, end, prot);
2225 }
2226 }
2227 /* Flush last range */
2228 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
2229 }
2230 #endif
2231
2232 static void mem_info(Monitor *mon)
2233 {
2234 CPUState *env;
2235
2236 env = mon_get_cpu();
2237
2238 if (!(env->cr[0] & CR0_PG_MASK)) {
2239 monitor_printf(mon, "PG disabled\n");
2240 return;
2241 }
2242 if (env->cr[4] & CR4_PAE_MASK) {
2243 #ifdef TARGET_X86_64
2244 if (env->hflags & HF_LMA_MASK) {
2245 mem_info_64(mon, env);
2246 } else
2247 #endif
2248 {
2249 mem_info_pae32(mon, env);
2250 }
2251 } else {
2252 mem_info_32(mon, env);
2253 }
2254 }
2255 #endif
2256
2257 #if defined(TARGET_SH4)
2258
2259 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
2260 {
2261 monitor_printf(mon, " tlb%i:\t"
2262 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2263 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2264 "dirty=%hhu writethrough=%hhu\n",
2265 idx,
2266 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2267 tlb->v, tlb->sh, tlb->c, tlb->pr,
2268 tlb->d, tlb->wt);
2269 }
2270
2271 static void tlb_info(Monitor *mon)
2272 {
2273 CPUState *env = mon_get_cpu();
2274 int i;
2275
2276 monitor_printf (mon, "ITLB:\n");
2277 for (i = 0 ; i < ITLB_SIZE ; i++)
2278 print_tlb (mon, i, &env->itlb[i]);
2279 monitor_printf (mon, "UTLB:\n");
2280 for (i = 0 ; i < UTLB_SIZE ; i++)
2281 print_tlb (mon, i, &env->utlb[i]);
2282 }
2283
2284 #endif
2285
2286 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
2287 static void tlb_info(Monitor *mon)
2288 {
2289 CPUState *env1 = mon_get_cpu();
2290
2291 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2292 }
2293 #endif
2294
2295 static void do_info_mtree(Monitor *mon)
2296 {
2297 mtree_info((fprintf_function)monitor_printf, mon);
2298 }
2299
2300 static void do_info_numa(Monitor *mon)
2301 {
2302 int i;
2303 CPUState *env;
2304
2305 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2306 for (i = 0; i < nb_numa_nodes; i++) {
2307 monitor_printf(mon, "node %d cpus:", i);
2308 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2309 if (env->numa_node == i) {
2310 monitor_printf(mon, " %d", env->cpu_index);
2311 }
2312 }
2313 monitor_printf(mon, "\n");
2314 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2315 node_mem[i] >> 20);
2316 }
2317 }
2318
2319 #ifdef CONFIG_PROFILER
2320
2321 int64_t qemu_time;
2322 int64_t dev_time;
2323
2324 static void do_info_profile(Monitor *mon)
2325 {
2326 int64_t total;
2327 total = qemu_time;
2328 if (total == 0)
2329 total = 1;
2330 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
2331 dev_time, dev_time / (double)get_ticks_per_sec());
2332 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
2333 qemu_time, qemu_time / (double)get_ticks_per_sec());
2334 qemu_time = 0;
2335 dev_time = 0;
2336 }
2337 #else
2338 static void do_info_profile(Monitor *mon)
2339 {
2340 monitor_printf(mon, "Internal profiler not compiled\n");
2341 }
2342 #endif
2343
2344 /* Capture support */
2345 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
2346
2347 static void do_info_capture(Monitor *mon)
2348 {
2349 int i;
2350 CaptureState *s;
2351
2352 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2353 monitor_printf(mon, "[%d]: ", i);
2354 s->ops.info (s->opaque);
2355 }
2356 }
2357
2358 #ifdef HAS_AUDIO
2359 static void do_stop_capture(Monitor *mon, const QDict *qdict)
2360 {
2361 int i;
2362 int n = qdict_get_int(qdict, "n");
2363 CaptureState *s;
2364
2365 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2366 if (i == n) {
2367 s->ops.destroy (s->opaque);
2368 QLIST_REMOVE (s, entries);
2369 g_free (s);
2370 return;
2371 }
2372 }
2373 }
2374
2375 static void do_wav_capture(Monitor *mon, const QDict *qdict)
2376 {
2377 const char *path = qdict_get_str(qdict, "path");
2378 int has_freq = qdict_haskey(qdict, "freq");
2379 int freq = qdict_get_try_int(qdict, "freq", -1);
2380 int has_bits = qdict_haskey(qdict, "bits");
2381 int bits = qdict_get_try_int(qdict, "bits", -1);
2382 int has_channels = qdict_haskey(qdict, "nchannels");
2383 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2384 CaptureState *s;
2385
2386 s = g_malloc0 (sizeof (*s));
2387
2388 freq = has_freq ? freq : 44100;
2389 bits = has_bits ? bits : 16;
2390 nchannels = has_channels ? nchannels : 2;
2391
2392 if (wav_start_capture (s, path, freq, bits, nchannels)) {
2393 monitor_printf(mon, "Failed to add wave capture\n");
2394 g_free (s);
2395 return;
2396 }
2397 QLIST_INSERT_HEAD (&capture_head, s, entries);
2398 }
2399 #endif
2400
2401 #if defined(TARGET_I386)
2402 static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
2403 {
2404 CPUState *env;
2405
2406 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2407 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2408 }
2409
2410 return 0;
2411 }
2412 #else
2413 static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
2414 {
2415 qerror_report(QERR_UNSUPPORTED);
2416 return -1;
2417 }
2418 #endif
2419
2420 static qemu_acl *find_acl(Monitor *mon, const char *name)
2421 {
2422 qemu_acl *acl = qemu_acl_find(name);
2423
2424 if (!acl) {
2425 monitor_printf(mon, "acl: unknown list '%s'\n", name);
2426 }
2427 return acl;
2428 }
2429
2430 static void do_acl_show(Monitor *mon, const QDict *qdict)
2431 {
2432 const char *aclname = qdict_get_str(qdict, "aclname");
2433 qemu_acl *acl = find_acl(mon, aclname);
2434 qemu_acl_entry *entry;
2435 int i = 0;
2436
2437 if (acl) {
2438 monitor_printf(mon, "policy: %s\n",
2439 acl->defaultDeny ? "deny" : "allow");
2440 QTAILQ_FOREACH(entry, &acl->entries, next) {
2441 i++;
2442 monitor_printf(mon, "%d: %s %s\n", i,
2443 entry->deny ? "deny" : "allow", entry->match);
2444 }
2445 }
2446 }
2447
2448 static void do_acl_reset(Monitor *mon, const QDict *qdict)
2449 {
2450 const char *aclname = qdict_get_str(qdict, "aclname");
2451 qemu_acl *acl = find_acl(mon, aclname);
2452
2453 if (acl) {
2454 qemu_acl_reset(acl);
2455 monitor_printf(mon, "acl: removed all rules\n");
2456 }
2457 }
2458
2459 static void do_acl_policy(Monitor *mon, const QDict *qdict)
2460 {
2461 const char *aclname = qdict_get_str(qdict, "aclname");
2462 const char *policy = qdict_get_str(qdict, "policy");
2463 qemu_acl *acl = find_acl(mon, aclname);
2464
2465 if (acl) {
2466 if (strcmp(policy, "allow") == 0) {
2467 acl->defaultDeny = 0;
2468 monitor_printf(mon, "acl: policy set to 'allow'\n");
2469 } else if (strcmp(policy, "deny") == 0) {
2470 acl->defaultDeny = 1;
2471 monitor_printf(mon, "acl: policy set to 'deny'\n");
2472 } else {
2473 monitor_printf(mon, "acl: unknown policy '%s', "
2474 "expected 'deny' or 'allow'\n", policy);
2475 }
2476 }
2477 }
2478
2479 static void do_acl_add(Monitor *mon, const QDict *qdict)
2480 {
2481 const char *aclname = qdict_get_str(qdict, "aclname");
2482 const char *match = qdict_get_str(qdict, "match");
2483 const char *policy = qdict_get_str(qdict, "policy");
2484 int has_index = qdict_haskey(qdict, "index");
2485 int index = qdict_get_try_int(qdict, "index", -1);
2486 qemu_acl *acl = find_acl(mon, aclname);
2487 int deny, ret;
2488
2489 if (acl) {
2490 if (strcmp(policy, "allow") == 0) {
2491 deny = 0;
2492 } else if (strcmp(policy, "deny") == 0) {
2493 deny = 1;
2494 } else {
2495 monitor_printf(mon, "acl: unknown policy '%s', "
2496 "expected 'deny' or 'allow'\n", policy);
2497 return;
2498 }
2499 if (has_index)
2500 ret = qemu_acl_insert(acl, deny, match, index);
2501 else
2502 ret = qemu_acl_append(acl, deny, match);
2503 if (ret < 0)
2504 monitor_printf(mon, "acl: unable to add acl entry\n");
2505 else
2506 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2507 }
2508 }
2509
2510 static void do_acl_remove(Monitor *mon, const QDict *qdict)
2511 {
2512 const char *aclname = qdict_get_str(qdict, "aclname");
2513 const char *match = qdict_get_str(qdict, "match");
2514 qemu_acl *acl = find_acl(mon, aclname);
2515 int ret;
2516
2517 if (acl) {
2518 ret = qemu_acl_remove(acl, match);
2519 if (ret < 0)
2520 monitor_printf(mon, "acl: no matching acl entry\n");
2521 else
2522 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2523 }
2524 }
2525
2526 #if defined(TARGET_I386)
2527 static void do_inject_mce(Monitor *mon, const QDict *qdict)
2528 {
2529 CPUState *cenv;
2530 int cpu_index = qdict_get_int(qdict, "cpu_index");
2531 int bank = qdict_get_int(qdict, "bank");
2532 uint64_t status = qdict_get_int(qdict, "status");
2533 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2534 uint64_t addr = qdict_get_int(qdict, "addr");
2535 uint64_t misc = qdict_get_int(qdict, "misc");
2536 int flags = MCE_INJECT_UNCOND_AO;
2537
2538 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2539 flags |= MCE_INJECT_BROADCAST;
2540 }
2541 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
2542 if (cenv->cpu_index == cpu_index) {
2543 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
2544 flags);
2545 break;
2546 }
2547 }
2548 }
2549 #endif
2550
2551 static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2552 {
2553 const char *fdname = qdict_get_str(qdict, "fdname");
2554 mon_fd_t *monfd;
2555 int fd;
2556
2557 fd = qemu_chr_fe_get_msgfd(mon->chr);
2558 if (fd == -1) {
2559 qerror_report(QERR_FD_NOT_SUPPLIED);
2560 return -1;
2561 }
2562
2563 if (qemu_isdigit(fdname[0])) {
2564 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2565 "a name not starting with a digit");
2566 return -1;
2567 }
2568
2569 QLIST_FOREACH(monfd, &mon->fds, next) {
2570 if (strcmp(monfd->name, fdname) != 0) {
2571 continue;
2572 }
2573
2574 close(monfd->fd);
2575 monfd->fd = fd;
2576 return 0;
2577 }
2578
2579 monfd = g_malloc0(sizeof(mon_fd_t));
2580 monfd->name = g_strdup(fdname);
2581 monfd->fd = fd;
2582
2583 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
2584 return 0;
2585 }
2586
2587 static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2588 {
2589 const char *fdname = qdict_get_str(qdict, "fdname");
2590 mon_fd_t *monfd;
2591
2592 QLIST_FOREACH(monfd, &mon->fds, next) {
2593 if (strcmp(monfd->name, fdname) != 0) {
2594 continue;
2595 }
2596
2597 QLIST_REMOVE(monfd, next);
2598 close(monfd->fd);
2599 g_free(monfd->name);
2600 g_free(monfd);
2601 return 0;
2602 }
2603
2604 qerror_report(QERR_FD_NOT_FOUND, fdname);
2605 return -1;
2606 }
2607
2608 static void do_loadvm(Monitor *mon, const QDict *qdict)
2609 {
2610 int saved_vm_running = runstate_is_running();
2611 const char *name = qdict_get_str(qdict, "name");
2612
2613 vm_stop(RUN_STATE_RESTORE_VM);
2614
2615 if (load_vmstate(name) == 0 && saved_vm_running) {
2616 vm_start();
2617 }
2618 }
2619
2620 int monitor_get_fd(Monitor *mon, const char *fdname)
2621 {
2622 mon_fd_t *monfd;
2623
2624 QLIST_FOREACH(monfd, &mon->fds, next) {
2625 int fd;
2626
2627 if (strcmp(monfd->name, fdname) != 0) {
2628 continue;
2629 }
2630
2631 fd = monfd->fd;
2632
2633 /* caller takes ownership of fd */
2634 QLIST_REMOVE(monfd, next);
2635 g_free(monfd->name);
2636 g_free(monfd);
2637
2638 return fd;
2639 }
2640
2641 return -1;
2642 }
2643
2644 static const mon_cmd_t mon_cmds[] = {
2645 #include "hmp-commands.h"
2646 { NULL, NULL, },
2647 };
2648
2649 /* Please update hmp-commands.hx when adding or changing commands */
2650 static const mon_cmd_t info_cmds[] = {
2651 {
2652 .name = "version",
2653 .args_type = "",
2654 .params = "",
2655 .help = "show the version of QEMU",
2656 .mhandler.info = hmp_info_version,
2657 },
2658 {
2659 .name = "network",
2660 .args_type = "",
2661 .params = "",
2662 .help = "show the network state",
2663 .mhandler.info = do_info_network,
2664 },
2665 {
2666 .name = "chardev",
2667 .args_type = "",
2668 .params = "",
2669 .help = "show the character devices",
2670 .mhandler.info = hmp_info_chardev,
2671 },
2672 {
2673 .name = "block",
2674 .args_type = "",
2675 .params = "",
2676 .help = "show the block devices",
2677 .mhandler.info = hmp_info_block,
2678 },
2679 {
2680 .name = "blockstats",
2681 .args_type = "",
2682 .params = "",
2683 .help = "show block device statistics",
2684 .mhandler.info = hmp_info_blockstats,
2685 },
2686 {
2687 .name = "registers",
2688 .args_type = "",
2689 .params = "",
2690 .help = "show the cpu registers",
2691 .mhandler.info = do_info_registers,
2692 },
2693 {
2694 .name = "cpus",
2695 .args_type = "",
2696 .params = "",
2697 .help = "show infos for each CPU",
2698 .mhandler.info = hmp_info_cpus,
2699 },
2700 {
2701 .name = "history",
2702 .args_type = "",
2703 .params = "",
2704 .help = "show the command line history",
2705 .mhandler.info = do_info_history,
2706 },
2707 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2708 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2709 {
2710 .name = "irq",
2711 .args_type = "",
2712 .params = "",
2713 .help = "show the interrupts statistics (if available)",
2714 #ifdef TARGET_SPARC
2715 .mhandler.info = sun4m_irq_info,
2716 #elif defined(TARGET_LM32)
2717 .mhandler.info = lm32_irq_info,
2718 #else
2719 .mhandler.info = irq_info,
2720 #endif
2721 },
2722 {
2723 .name = "pic",
2724 .args_type = "",
2725 .params = "",
2726 .help = "show i8259 (PIC) state",
2727 #ifdef TARGET_SPARC
2728 .mhandler.info = sun4m_pic_info,
2729 #elif defined(TARGET_LM32)
2730 .mhandler.info = lm32_do_pic_info,
2731 #else
2732 .mhandler.info = pic_info,
2733 #endif
2734 },
2735 #endif
2736 {
2737 .name = "pci",
2738 .args_type = "",
2739 .params = "",
2740 .help = "show PCI info",
2741 .user_print = do_pci_info_print,
2742 .mhandler.info_new = do_pci_info,
2743 },
2744 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2745 defined(TARGET_PPC)
2746 {
2747 .name = "tlb",
2748 .args_type = "",
2749 .params = "",
2750 .help = "show virtual to physical memory mappings",
2751 .mhandler.info = tlb_info,
2752 },
2753 #endif
2754 #if defined(TARGET_I386)
2755 {
2756 .name = "mem",
2757 .args_type = "",
2758 .params = "",
2759 .help = "show the active virtual memory mappings",
2760 .mhandler.info = mem_info,
2761 },
2762 #endif
2763 {
2764 .name = "mtree",
2765 .args_type = "",
2766 .params = "",
2767 .help = "show memory tree",
2768 .mhandler.info = do_info_mtree,
2769 },
2770 {
2771 .name = "jit",
2772 .args_type = "",
2773 .params = "",
2774 .help = "show dynamic compiler info",
2775 .mhandler.info = do_info_jit,
2776 },
2777 {
2778 .name = "kvm",
2779 .args_type = "",
2780 .params = "",
2781 .help = "show KVM information",
2782 .mhandler.info = hmp_info_kvm,
2783 },
2784 {
2785 .name = "numa",
2786 .args_type = "",
2787 .params = "",
2788 .help = "show NUMA information",
2789 .mhandler.info = do_info_numa,
2790 },
2791 {
2792 .name = "usb",
2793 .args_type = "",
2794 .params = "",
2795 .help = "show guest USB devices",
2796 .mhandler.info = usb_info,
2797 },
2798 {
2799 .name = "usbhost",
2800 .args_type = "",
2801 .params = "",
2802 .help = "show host USB devices",
2803 .mhandler.info = usb_host_info,
2804 },
2805 {
2806 .name = "profile",
2807 .args_type = "",
2808 .params = "",
2809 .help = "show profiling information",
2810 .mhandler.info = do_info_profile,
2811 },
2812 {
2813 .name = "capture",
2814 .args_type = "",
2815 .params = "",
2816 .help = "show capture information",
2817 .mhandler.info = do_info_capture,
2818 },
2819 {
2820 .name = "snapshots",
2821 .args_type = "",
2822 .params = "",
2823 .help = "show the currently saved VM snapshots",
2824 .mhandler.info = do_info_snapshots,
2825 },
2826 {
2827 .name = "status",
2828 .args_type = "",
2829 .params = "",
2830 .help = "show the current VM status (running|paused)",
2831 .mhandler.info = hmp_info_status,
2832 },
2833 {
2834 .name = "pcmcia",
2835 .args_type = "",
2836 .params = "",
2837 .help = "show guest PCMCIA status",
2838 .mhandler.info = pcmcia_info,
2839 },
2840 {
2841 .name = "mice",
2842 .args_type = "",
2843 .params = "",
2844 .help = "show which guest mouse is receiving events",
2845 .mhandler.info = hmp_info_mice,
2846 },
2847 {
2848 .name = "vnc",
2849 .args_type = "",
2850 .params = "",
2851 .help = "show the vnc server status",
2852 .mhandler.info = hmp_info_vnc,
2853 },
2854 #if defined(CONFIG_SPICE)
2855 {
2856 .name = "spice",
2857 .args_type = "",
2858 .params = "",
2859 .help = "show the spice server status",
2860 .mhandler.info = hmp_info_spice,
2861 },
2862 #endif
2863 {
2864 .name = "name",
2865 .args_type = "",
2866 .params = "",
2867 .help = "show the current VM name",
2868 .mhandler.info = hmp_info_name,
2869 },
2870 {
2871 .name = "uuid",
2872 .args_type = "",
2873 .params = "",
2874 .help = "show the current VM UUID",
2875 .mhandler.info = hmp_info_uuid,
2876 },
2877 #if defined(TARGET_PPC)
2878 {
2879 .name = "cpustats",
2880 .args_type = "",
2881 .params = "",
2882 .help = "show CPU statistics",
2883 .mhandler.info = do_info_cpu_stats,
2884 },
2885 #endif
2886 #if defined(CONFIG_SLIRP)
2887 {
2888 .name = "usernet",
2889 .args_type = "",
2890 .params = "",
2891 .help = "show user network stack connection states",
2892 .mhandler.info = do_info_usernet,
2893 },
2894 #endif
2895 {
2896 .name = "migrate",
2897 .args_type = "",
2898 .params = "",
2899 .help = "show migration status",
2900 .mhandler.info = hmp_info_migrate,
2901 },
2902 {
2903 .name = "balloon",
2904 .args_type = "",
2905 .params = "",
2906 .help = "show balloon information",
2907 .user_print = monitor_print_balloon,
2908 .mhandler.info_async = do_info_balloon,
2909 .flags = MONITOR_CMD_ASYNC,
2910 },
2911 {
2912 .name = "qtree",
2913 .args_type = "",
2914 .params = "",
2915 .help = "show device tree",
2916 .mhandler.info = do_info_qtree,
2917 },
2918 {
2919 .name = "qdm",
2920 .args_type = "",
2921 .params = "",
2922 .help = "show qdev device model list",
2923 .mhandler.info = do_info_qdm,
2924 },
2925 {
2926 .name = "roms",
2927 .args_type = "",
2928 .params = "",
2929 .help = "show roms",
2930 .mhandler.info = do_info_roms,
2931 },
2932 #if defined(CONFIG_TRACE_SIMPLE)
2933 {
2934 .name = "trace",
2935 .args_type = "",
2936 .params = "",
2937 .help = "show current contents of trace buffer",
2938 .mhandler.info = do_info_trace,
2939 },
2940 #endif
2941 {
2942 .name = "trace-events",
2943 .args_type = "",
2944 .params = "",
2945 .help = "show available trace-events & their state",
2946 .mhandler.info = do_trace_print_events,
2947 },
2948 {
2949 .name = NULL,
2950 },
2951 };
2952
2953 static const mon_cmd_t qmp_cmds[] = {
2954 #include "qmp-commands-old.h"
2955 { /* NULL */ },
2956 };
2957
2958 static const mon_cmd_t qmp_query_cmds[] = {
2959 {
2960 .name = "pci",
2961 .args_type = "",
2962 .params = "",
2963 .help = "show PCI info",
2964 .user_print = do_pci_info_print,
2965 .mhandler.info_new = do_pci_info,
2966 },
2967 {
2968 .name = "balloon",
2969 .args_type = "",
2970 .params = "",
2971 .help = "show balloon information",
2972 .user_print = monitor_print_balloon,
2973 .mhandler.info_async = do_info_balloon,
2974 .flags = MONITOR_CMD_ASYNC,
2975 },
2976 { /* NULL */ },
2977 };
2978
2979 /*******************************************************************/
2980
2981 static const char *pch;
2982 static jmp_buf expr_env;
2983
2984 #define MD_TLONG 0
2985 #define MD_I32 1
2986
2987 typedef struct MonitorDef {
2988 const char *name;
2989 int offset;
2990 target_long (*get_value)(const struct MonitorDef *md, int val);
2991 int type;
2992 } MonitorDef;
2993
2994 #if defined(TARGET_I386)
2995 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2996 {
2997 CPUState *env = mon_get_cpu();
2998 return env->eip + env->segs[R_CS].base;
2999 }
3000 #endif
3001
3002 #if defined(TARGET_PPC)
3003 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
3004 {
3005 CPUState *env = mon_get_cpu();
3006 unsigned int u;
3007 int i;
3008
3009 u = 0;
3010 for (i = 0; i < 8; i++)
3011 u |= env->crf[i] << (32 - (4 * i));
3012
3013 return u;
3014 }
3015
3016 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
3017 {
3018 CPUState *env = mon_get_cpu();
3019 return env->msr;
3020 }
3021
3022 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
3023 {
3024 CPUState *env = mon_get_cpu();
3025 return env->xer;
3026 }
3027
3028 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
3029 {
3030 CPUState *env = mon_get_cpu();
3031 return cpu_ppc_load_decr(env);
3032 }
3033
3034 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
3035 {
3036 CPUState *env = mon_get_cpu();
3037 return cpu_ppc_load_tbu(env);
3038 }
3039
3040 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
3041 {
3042 CPUState *env = mon_get_cpu();
3043 return cpu_ppc_load_tbl(env);
3044 }
3045 #endif
3046
3047 #if defined(TARGET_SPARC)
3048 #ifndef TARGET_SPARC64
3049 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
3050 {
3051 CPUState *env = mon_get_cpu();
3052
3053 return cpu_get_psr(env);
3054 }
3055 #endif
3056
3057 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
3058 {
3059 CPUState *env = mon_get_cpu();
3060 return env->regwptr[val];
3061 }
3062 #endif
3063
3064 static const MonitorDef monitor_defs[] = {
3065 #ifdef TARGET_I386
3066
3067 #define SEG(name, seg) \
3068 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
3069 { name ".base", offsetof(CPUState, segs[seg].base) },\
3070 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
3071
3072 { "eax", offsetof(CPUState, regs[0]) },
3073 { "ecx", offsetof(CPUState, regs[1]) },
3074 { "edx", offsetof(CPUState, regs[2]) },
3075 { "ebx", offsetof(CPUState, regs[3]) },
3076 { "esp|sp", offsetof(CPUState, regs[4]) },
3077 { "ebp|fp", offsetof(CPUState, regs[5]) },
3078 { "esi", offsetof(CPUState, regs[6]) },
3079 { "edi", offsetof(CPUState, regs[7]) },
3080 #ifdef TARGET_X86_64
3081 { "r8", offsetof(CPUState, regs[8]) },
3082 { "r9", offsetof(CPUState, regs[9]) },
3083 { "r10", offsetof(CPUState, regs[10]) },
3084 { "r11", offsetof(CPUState, regs[11]) },
3085 { "r12", offsetof(CPUState, regs[12]) },
3086 { "r13", offsetof(CPUState, regs[13]) },
3087 { "r14", offsetof(CPUState, regs[14]) },
3088 { "r15", offsetof(CPUState, regs[15]) },
3089 #endif
3090 { "eflags", offsetof(CPUState, eflags) },
3091 { "eip", offsetof(CPUState, eip) },
3092 SEG("cs", R_CS)
3093 SEG("ds", R_DS)
3094 SEG("es", R_ES)
3095 SEG("ss", R_SS)
3096 SEG("fs", R_FS)
3097 SEG("gs", R_GS)
3098 { "pc", 0, monitor_get_pc, },
3099 #elif defined(TARGET_PPC)
3100 /* General purpose registers */
3101 { "r0", offsetof(CPUState, gpr[0]) },
3102 { "r1", offsetof(CPUState, gpr[1]) },
3103 { "r2", offsetof(CPUState, gpr[2]) },
3104 { "r3", offsetof(CPUState, gpr[3]) },
3105 { "r4", offsetof(CPUState, gpr[4]) },
3106 { "r5", offsetof(CPUState, gpr[5]) },
3107 { "r6", offsetof(CPUState, gpr[6]) },
3108 { "r7", offsetof(CPUState, gpr[7]) },
3109 { "r8", offsetof(CPUState, gpr[8]) },
3110 { "r9", offsetof(CPUState, gpr[9]) },
3111 { "r10", offsetof(CPUState, gpr[10]) },
3112 { "r11", offsetof(CPUState, gpr[11]) },
3113 { "r12", offsetof(CPUState, gpr[12]) },
3114 { "r13", offsetof(CPUState, gpr[13]) },
3115 { "r14", offsetof(CPUState, gpr[14]) },
3116 { "r15", offsetof(CPUState, gpr[15]) },
3117 { "r16", offsetof(CPUState, gpr[16]) },
3118 { "r17", offsetof(CPUState, gpr[17]) },
3119 { "r18", offsetof(CPUState, gpr[18]) },
3120 { "r19", offsetof(CPUState, gpr[19]) },
3121 { "r20", offsetof(CPUState, gpr[20]) },
3122 { "r21", offsetof(CPUState, gpr[21]) },
3123 { "r22", offsetof(CPUState, gpr[22]) },
3124 { "r23", offsetof(CPUState, gpr[23]) },
3125 { "r24", offsetof(CPUState, gpr[24]) },
3126 { "r25", offsetof(CPUState, gpr[25]) },
3127 { "r26", offsetof(CPUState, gpr[26]) },
3128 { "r27", offsetof(CPUState, gpr[27]) },
3129 { "r28", offsetof(CPUState, gpr[28]) },
3130 { "r29", offsetof(CPUState, gpr[29]) },
3131 { "r30", offsetof(CPUState, gpr[30]) },
3132 { "r31", offsetof(CPUState, gpr[31]) },
3133 /* Floating point registers */
3134 { "f0", offsetof(CPUState, fpr[0]) },
3135 { "f1", offsetof(CPUState, fpr[1]) },
3136 { "f2", offsetof(CPUState, fpr[2]) },
3137 { "f3", offsetof(CPUState, fpr[3]) },
3138 { "f4", offsetof(CPUState, fpr[4]) },
3139 { "f5", offsetof(CPUState, fpr[5]) },
3140 { "f6", offsetof(CPUState, fpr[6]) },
3141 { "f7", offsetof(CPUState, fpr[7]) },
3142 { "f8", offsetof(CPUState, fpr[8]) },
3143 { "f9", offsetof(CPUState, fpr[9]) },
3144 { "f10", offsetof(CPUState, fpr[10]) },
3145 { "f11", offsetof(CPUState, fpr[11]) },
3146 { "f12", offsetof(CPUState, fpr[12]) },
3147 { "f13", offsetof(CPUState, fpr[13]) },
3148 { "f14", offsetof(CPUState, fpr[14]) },
3149 { "f15", offsetof(CPUState, fpr[15]) },
3150 { "f16", offsetof(CPUState, fpr[16]) },
3151 { "f17", offsetof(CPUState, fpr[17]) },
3152 { "f18", offsetof(CPUState, fpr[18]) },
3153 { "f19", offsetof(CPUState, fpr[19]) },
3154 { "f20", offsetof(CPUState, fpr[20]) },
3155 { "f21", offsetof(CPUState, fpr[21]) },
3156 { "f22", offsetof(CPUState, fpr[22]) },
3157 { "f23", offsetof(CPUState, fpr[23]) },
3158 { "f24", offsetof(CPUState, fpr[24]) },
3159 { "f25", offsetof(CPUState, fpr[25]) },
3160 { "f26", offsetof(CPUState, fpr[26]) },
3161 { "f27", offsetof(CPUState, fpr[27]) },
3162 { "f28", offsetof(CPUState, fpr[28]) },
3163 { "f29", offsetof(CPUState, fpr[29]) },
3164 { "f30", offsetof(CPUState, fpr[30]) },
3165 { "f31", offsetof(CPUState, fpr[31]) },
3166 { "fpscr", offsetof(CPUState, fpscr) },
3167 /* Next instruction pointer */
3168 { "nip|pc", offsetof(CPUState, nip) },
3169 { "lr", offsetof(CPUState, lr) },
3170 { "ctr", offsetof(CPUState, ctr) },
3171 { "decr", 0, &monitor_get_decr, },
3172 { "ccr", 0, &monitor_get_ccr, },
3173 /* Machine state register */
3174 { "msr", 0, &monitor_get_msr, },
3175 { "xer", 0, &monitor_get_xer, },
3176 { "tbu", 0, &monitor_get_tbu, },
3177 { "tbl", 0, &monitor_get_tbl, },
3178 #if defined(TARGET_PPC64)
3179 /* Address space register */
3180 { "asr", offsetof(CPUState, asr) },
3181 #endif
3182 /* Segment registers */
3183 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
3184 { "sr0", offsetof(CPUState, sr[0]) },
3185 { "sr1", offsetof(CPUState, sr[1]) },
3186 { "sr2", offsetof(CPUState, sr[2]) },
3187 { "sr3", offsetof(CPUState, sr[3]) },
3188 { "sr4", offsetof(CPUState, sr[4]) },
3189 { "sr5", offsetof(CPUState, sr[5]) },
3190 { "sr6", offsetof(CPUState, sr[6]) },
3191 { "sr7", offsetof(CPUState, sr[7]) },
3192 { "sr8", offsetof(CPUState, sr[8]) },
3193 { "sr9", offsetof(CPUState, sr[9]) },
3194 { "sr10", offsetof(CPUState, sr[10]) },
3195 { "sr11", offsetof(CPUState, sr[11]) },
3196 { "sr12", offsetof(CPUState, sr[12]) },
3197 { "sr13", offsetof(CPUState, sr[13]) },
3198 { "sr14", offsetof(CPUState, sr[14]) },
3199 { "sr15", offsetof(CPUState, sr[15]) },
3200 /* Too lazy to put BATs... */
3201 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3202
3203 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3204 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3205 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3206 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3207 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3208 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3209 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3210 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3211 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3212 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3213 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3214 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3215 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3216 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3217 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3218 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3219 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3220 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3221 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3222 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3223 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3224 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3225 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3226 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3227 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3228 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3229 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3230 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3231 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3232 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3233 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3234 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3235 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3236 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3237 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3238 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3239 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3240 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3241 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3242 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3243 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3244 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3245 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3246 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3247 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3248 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3249 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3250 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3251 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3252 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3253 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3254 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3255 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3256 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3257 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3258 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3259 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3260 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3261 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3262 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3263 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3264 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3265 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3266 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3267 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3268 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3269
3270 #elif defined(TARGET_SPARC)
3271 { "g0", offsetof(CPUState, gregs[0]) },
3272 { "g1", offsetof(CPUState, gregs[1]) },
3273 { "g2", offsetof(CPUState, gregs[2]) },
3274 { "g3", offsetof(CPUState, gregs[3]) },
3275 { "g4", offsetof(CPUState, gregs[4]) },
3276 { "g5", offsetof(CPUState, gregs[5]) },
3277 { "g6", offsetof(CPUState, gregs[6]) },
3278 { "g7", offsetof(CPUState, gregs[7]) },
3279 { "o0", 0, monitor_get_reg },
3280 { "o1", 1, monitor_get_reg },
3281 { "o2", 2, monitor_get_reg },
3282 { "o3", 3, monitor_get_reg },
3283 { "o4", 4, monitor_get_reg },
3284 { "o5", 5, monitor_get_reg },
3285 { "o6", 6, monitor_get_reg },
3286 { "o7", 7, monitor_get_reg },
3287 { "l0", 8, monitor_get_reg },
3288 { "l1", 9, monitor_get_reg },
3289 { "l2", 10, monitor_get_reg },
3290 { "l3", 11, monitor_get_reg },
3291 { "l4", 12, monitor_get_reg },
3292 { "l5", 13, monitor_get_reg },
3293 { "l6", 14, monitor_get_reg },
3294 { "l7", 15, monitor_get_reg },
3295 { "i0", 16, monitor_get_reg },
3296 { "i1", 17, monitor_get_reg },
3297 { "i2", 18, monitor_get_reg },
3298 { "i3", 19, monitor_get_reg },
3299 { "i4", 20, monitor_get_reg },
3300 { "i5", 21, monitor_get_reg },
3301 { "i6", 22, monitor_get_reg },
3302 { "i7", 23, monitor_get_reg },
3303 { "pc", offsetof(CPUState, pc) },
3304 { "npc", offsetof(CPUState, npc) },
3305 { "y", offsetof(CPUState, y) },
3306 #ifndef TARGET_SPARC64
3307 { "psr", 0, &monitor_get_psr, },
3308 { "wim", offsetof(CPUState, wim) },
3309 #endif
3310 { "tbr", offsetof(CPUState, tbr) },
3311 { "fsr", offsetof(CPUState, fsr) },
3312 { "f0", offsetof(CPUState, fpr[0]) },
3313 { "f1", offsetof(CPUState, fpr[1]) },
3314 { "f2", offsetof(CPUState, fpr[2]) },
3315 { "f3", offsetof(CPUState, fpr[3]) },
3316 { "f4", offsetof(CPUState, fpr[4]) },
3317 { "f5", offsetof(CPUState, fpr[5]) },
3318 { "f6", offsetof(CPUState, fpr[6]) },
3319 { "f7", offsetof(CPUState, fpr[7]) },
3320 { "f8", offsetof(CPUState, fpr[8]) },
3321 { "f9", offsetof(CPUState, fpr[9]) },
3322 { "f10", offsetof(CPUState, fpr[10]) },
3323 { "f11", offsetof(CPUState, fpr[11]) },
3324 { "f12", offsetof(CPUState, fpr[12]) },
3325 { "f13", offsetof(CPUState, fpr[13]) },
3326 { "f14", offsetof(CPUState, fpr[14]) },
3327 { "f15", offsetof(CPUState, fpr[15]) },
3328 { "f16", offsetof(CPUState, fpr[16]) },
3329 { "f17", offsetof(CPUState, fpr[17]) },
3330 { "f18", offsetof(CPUState, fpr[18]) },
3331 { "f19", offsetof(CPUState, fpr[19]) },
3332 { "f20", offsetof(CPUState, fpr[20]) },
3333 { "f21", offsetof(CPUState, fpr[21]) },
3334 { "f22", offsetof(CPUState, fpr[22]) },
3335 { "f23", offsetof(CPUState, fpr[23]) },
3336 { "f24", offsetof(CPUState, fpr[24]) },
3337 { "f25", offsetof(CPUState, fpr[25]) },
3338 { "f26", offsetof(CPUState, fpr[26]) },
3339 { "f27", offsetof(CPUState, fpr[27]) },
3340 { "f28", offsetof(CPUState, fpr[28]) },
3341 { "f29", offsetof(CPUState, fpr[29]) },
3342 { "f30", offsetof(CPUState, fpr[30]) },
3343 { "f31", offsetof(CPUState, fpr[31]) },
3344 #ifdef TARGET_SPARC64
3345 { "f32", offsetof(CPUState, fpr[32]) },
3346 { "f34", offsetof(CPUState, fpr[34]) },
3347 { "f36", offsetof(CPUState, fpr[36]) },
3348 { "f38", offsetof(CPUState, fpr[38]) },
3349 { "f40", offsetof(CPUState, fpr[40]) },
3350 { "f42", offsetof(CPUState, fpr[42]) },
3351 { "f44", offsetof(CPUState, fpr[44]) },
3352 { "f46", offsetof(CPUState, fpr[46]) },
3353 { "f48", offsetof(CPUState, fpr[48]) },
3354 { "f50", offsetof(CPUState, fpr[50]) },
3355 { "f52", offsetof(CPUState, fpr[52]) },
3356 { "f54", offsetof(CPUState, fpr[54]) },
3357 { "f56", offsetof(CPUState, fpr[56]) },
3358 { "f58", offsetof(CPUState, fpr[58]) },
3359 { "f60", offsetof(CPUState, fpr[60]) },
3360 { "f62", offsetof(CPUState, fpr[62]) },
3361 { "asi", offsetof(CPUState, asi) },
3362 { "pstate", offsetof(CPUState, pstate) },
3363 { "cansave", offsetof(CPUState, cansave) },
3364 { "canrestore", offsetof(CPUState, canrestore) },
3365 { "otherwin", offsetof(CPUState, otherwin) },
3366 { "wstate", offsetof(CPUState, wstate) },
3367 { "cleanwin", offsetof(CPUState, cleanwin) },
3368 { "fprs", offsetof(CPUState, fprs) },
3369 #endif
3370 #endif
3371 { NULL },
3372 };
3373
3374 static void expr_error(Monitor *mon, const char *msg)
3375 {
3376 monitor_printf(mon, "%s\n", msg);
3377 longjmp(expr_env, 1);
3378 }
3379
3380 /* return 0 if OK, -1 if not found */
3381 static int get_monitor_def(target_long *pval, const char *name)
3382 {
3383 const MonitorDef *md;
3384 void *ptr;
3385
3386 for(md = monitor_defs; md->name != NULL; md++) {
3387 if (compare_cmd(name, md->name)) {
3388 if (md->get_value) {
3389 *pval = md->get_value(md, md->offset);
3390 } else {
3391 CPUState *env = mon_get_cpu();
3392 ptr = (uint8_t *)env + md->offset;
3393 switch(md->type) {
3394 case MD_I32:
3395 *pval = *(int32_t *)ptr;
3396 break;
3397 case MD_TLONG:
3398 *pval = *(target_long *)ptr;
3399 break;
3400 default:
3401 *pval = 0;
3402 break;
3403 }
3404 }
3405 return 0;
3406 }
3407 }
3408 return -1;
3409 }
3410
3411 static void next(void)
3412 {
3413 if (*pch != '\0') {
3414 pch++;
3415 while (qemu_isspace(*pch))
3416 pch++;
3417 }
3418 }
3419
3420 static int64_t expr_sum(Monitor *mon);
3421
3422 static int64_t expr_unary(Monitor *mon)
3423 {
3424 int64_t n;
3425 char *p;
3426 int ret;
3427
3428 switch(*pch) {
3429 case '+':
3430 next();
3431 n = expr_unary(mon);
3432 break;
3433 case '-':
3434 next();
3435 n = -expr_unary(mon);
3436 break;
3437 case '~':
3438 next();
3439 n = ~expr_unary(mon);
3440 break;
3441 case '(':
3442 next();
3443 n = expr_sum(mon);
3444 if (*pch != ')') {
3445 expr_error(mon, "')' expected");
3446 }
3447 next();
3448 break;
3449 case '\'':
3450 pch++;
3451 if (*pch == '\0')
3452 expr_error(mon, "character constant expected");
3453 n = *pch;
3454 pch++;
3455 if (*pch != '\'')
3456 expr_error(mon, "missing terminating \' character");
3457 next();
3458 break;
3459 case '$':
3460 {
3461 char buf[128], *q;
3462 target_long reg=0;
3463
3464 pch++;
3465 q = buf;
3466 while ((*pch >= 'a' && *pch <= 'z') ||
3467 (*pch >= 'A' && *pch <= 'Z') ||
3468 (*pch >= '0' && *pch <= '9') ||
3469 *pch == '_' || *pch == '.') {
3470 if ((q - buf) < sizeof(buf) - 1)
3471 *q++ = *pch;
3472 pch++;
3473 }
3474 while (qemu_isspace(*pch))
3475 pch++;
3476 *q = 0;
3477 ret = get_monitor_def(&reg, buf);
3478 if (ret < 0)
3479 expr_error(mon, "unknown register");
3480 n = reg;
3481 }
3482 break;
3483 case '\0':
3484 expr_error(mon, "unexpected end of expression");
3485 n = 0;
3486 break;
3487 default:
3488 #if TARGET_PHYS_ADDR_BITS > 32
3489 n = strtoull(pch, &p, 0);
3490 #else
3491 n = strtoul(pch, &p, 0);
3492 #endif
3493 if (pch == p) {
3494 expr_error(mon, "invalid char in expression");
3495 }
3496 pch = p;
3497 while (qemu_isspace(*pch))
3498 pch++;
3499 break;
3500 }
3501 return n;
3502 }
3503
3504
3505 static int64_t expr_prod(Monitor *mon)
3506 {
3507 int64_t val, val2;
3508 int op;
3509
3510 val = expr_unary(mon);
3511 for(;;) {
3512 op = *pch;
3513 if (op != '*' && op != '/' && op != '%')
3514 break;
3515 next();
3516 val2 = expr_unary(mon);
3517 switch(op) {
3518 default:
3519 case '*':
3520 val *= val2;
3521 break;
3522 case '/':
3523 case '%':
3524 if (val2 == 0)
3525 expr_error(mon, "division by zero");
3526 if (op == '/')
3527 val /= val2;
3528 else
3529 val %= val2;
3530 break;
3531 }
3532 }
3533 return val;
3534 }
3535
3536 static int64_t expr_logic(Monitor *mon)
3537 {
3538 int64_t val, val2;
3539 int op;
3540
3541 val = expr_prod(mon);
3542 for(;;) {
3543 op = *pch;
3544 if (op != '&' && op != '|' && op != '^')
3545 break;
3546 next();
3547 val2 = expr_prod(mon);
3548 switch(op) {
3549 default:
3550 case '&':
3551 val &= val2;
3552 break;
3553 case '|':
3554 val |= val2;
3555 break;
3556 case '^':
3557 val ^= val2;
3558 break;
3559 }
3560 }
3561 return val;
3562 }
3563
3564 static int64_t expr_sum(Monitor *mon)
3565 {
3566 int64_t val, val2;
3567 int op;
3568
3569 val = expr_logic(mon);
3570 for(;;) {
3571 op = *pch;
3572 if (op != '+' && op != '-')
3573 break;
3574 next();
3575 val2 = expr_logic(mon);
3576 if (op == '+')
3577 val += val2;
3578 else
3579 val -= val2;
3580 }
3581 return val;
3582 }
3583
3584 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3585 {
3586 pch = *pp;
3587 if (setjmp(expr_env)) {
3588 *pp = pch;
3589 return -1;
3590 }
3591 while (qemu_isspace(*pch))
3592 pch++;
3593 *pval = expr_sum(mon);
3594 *pp = pch;
3595 return 0;
3596 }
3597
3598 static int get_double(Monitor *mon, double *pval, const char **pp)
3599 {
3600 const char *p = *pp;
3601 char *tailp;
3602 double d;
3603
3604 d = strtod(p, &tailp);
3605 if (tailp == p) {
3606 monitor_printf(mon, "Number expected\n");
3607 return -1;
3608 }
3609 if (d != d || d - d != 0) {
3610 /* NaN or infinity */
3611 monitor_printf(mon, "Bad number\n");
3612 return -1;
3613 }
3614 *pval = d;
3615 *pp = tailp;
3616 return 0;
3617 }
3618
3619 static int get_str(char *buf, int buf_size, const char **pp)
3620 {
3621 const char *p;
3622 char *q;
3623 int c;
3624
3625 q = buf;
3626 p = *pp;
3627 while (qemu_isspace(*p))
3628 p++;
3629 if (*p == '\0') {
3630 fail:
3631 *q = '\0';
3632 *pp = p;
3633 return -1;
3634 }
3635 if (*p == '\"') {
3636 p++;
3637 while (*p != '\0' && *p != '\"') {
3638 if (*p == '\\') {
3639 p++;
3640 c = *p++;
3641 switch(c) {
3642 case 'n':
3643 c = '\n';
3644 break;
3645 case 'r':
3646 c = '\r';
3647 break;
3648 case '\\':
3649 case '\'':
3650 case '\"':
3651 break;
3652 default:
3653 qemu_printf("unsupported escape code: '\\%c'\n", c);
3654 goto fail;
3655 }
3656 if ((q - buf) < buf_size - 1) {
3657 *q++ = c;
3658 }
3659 } else {
3660 if ((q - buf) < buf_size - 1) {
3661 *q++ = *p;
3662 }
3663 p++;
3664 }
3665 }
3666 if (*p != '\"') {
3667 qemu_printf("unterminated string\n");
3668 goto fail;
3669 }
3670 p++;
3671 } else {
3672 while (*p != '\0' && !qemu_isspace(*p)) {
3673 if ((q - buf) < buf_size - 1) {
3674 *q++ = *p;
3675 }
3676 p++;
3677 }
3678 }
3679 *q = '\0';
3680 *pp = p;
3681 return 0;
3682 }
3683
3684 /*
3685 * Store the command-name in cmdname, and return a pointer to
3686 * the remaining of the command string.
3687 */
3688 static const char *get_command_name(const char *cmdline,
3689 char *cmdname, size_t nlen)
3690 {
3691 size_t len;
3692 const char *p, *pstart;
3693
3694 p = cmdline;
3695 while (qemu_isspace(*p))
3696 p++;
3697 if (*p == '\0')
3698 return NULL;
3699 pstart = p;
3700 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3701 p++;
3702 len = p - pstart;
3703 if (len > nlen - 1)
3704 len = nlen - 1;
3705 memcpy(cmdname, pstart, len);
3706 cmdname[len] = '\0';
3707 return p;
3708 }
3709
3710 /**
3711 * Read key of 'type' into 'key' and return the current
3712 * 'type' pointer.
3713 */
3714 static char *key_get_info(const char *type, char **key)
3715 {
3716 size_t len;
3717 char *p, *str;
3718
3719 if (*type == ',')
3720 type++;
3721
3722 p = strchr(type, ':');
3723 if (!p) {
3724 *key = NULL;
3725 return NULL;
3726 }
3727 len = p - type;
3728
3729 str = g_malloc(len + 1);
3730 memcpy(str, type, len);
3731 str[len] = '\0';
3732
3733 *key = str;
3734 return ++p;
3735 }
3736
3737 static int default_fmt_format = 'x';
3738 static int default_fmt_size = 4;
3739
3740 #define MAX_ARGS 16
3741
3742 static int is_valid_option(const char *c, const char *typestr)
3743 {
3744 char option[3];
3745
3746 option[0] = '-';
3747 option[1] = *c;
3748 option[2] = '\0';
3749
3750 typestr = strstr(typestr, option);
3751 return (typestr != NULL);
3752 }
3753
3754 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3755 const char *cmdname)
3756 {
3757 const mon_cmd_t *cmd;
3758
3759 for (cmd = disp_table; cmd->name != NULL; cmd++) {
3760 if (compare_cmd(cmdname, cmd->name)) {
3761 return cmd;
3762 }
3763 }
3764
3765 return NULL;
3766 }
3767
3768 static const mon_cmd_t *monitor_find_command(const char *cmdname)
3769 {
3770 return search_dispatch_table(mon_cmds, cmdname);
3771 }
3772
3773 static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3774 {
3775 return search_dispatch_table(qmp_query_cmds, info_item);
3776 }
3777
3778 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3779 {
3780 return search_dispatch_table(qmp_cmds, cmdname);
3781 }
3782
3783 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3784 const char *cmdline,
3785 QDict *qdict)
3786 {
3787 const char *p, *typestr;
3788 int c;
3789 const mon_cmd_t *cmd;
3790 char cmdname[256];
3791 char buf[1024];
3792 char *key;
3793
3794 #ifdef DEBUG
3795 monitor_printf(mon, "command='%s'\n", cmdline);
3796 #endif
3797
3798 /* extract the command name */
3799 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3800 if (!p)
3801 return NULL;
3802
3803 cmd = monitor_find_command(cmdname);
3804 if (!cmd) {
3805 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
3806 return NULL;
3807 }
3808
3809 /* parse the parameters */
3810 typestr = cmd->args_type;
3811 for(;;) {
3812 typestr = key_get_info(typestr, &key);
3813 if (!typestr)
3814 break;
3815 c = *typestr;
3816 typestr++;
3817 switch(c) {
3818 case 'F':
3819 case 'B':
3820 case 's':
3821 {
3822 int ret;
3823
3824 while (qemu_isspace(*p))
3825 p++;
3826 if (*typestr == '?') {
3827 typestr++;
3828 if (*p == '\0') {
3829 /* no optional string: NULL argument */
3830 break;
3831 }
3832 }
3833 ret = get_str(buf, sizeof(buf), &p);
3834 if (ret < 0) {
3835 switch(c) {
3836 case 'F':
3837 monitor_printf(mon, "%s: filename expected\n",
3838 cmdname);
3839 break;
3840 case 'B':
3841 monitor_printf(mon, "%s: block device name expected\n",
3842 cmdname);
3843 break;
3844 default:
3845 monitor_printf(mon, "%s: string expected\n", cmdname);
3846 break;
3847 }
3848 goto fail;
3849 }
3850 qdict_put(qdict, key, qstring_from_str(buf));
3851 }
3852 break;
3853 case 'O':
3854 {
3855 QemuOptsList *opts_list;
3856 QemuOpts *opts;
3857
3858 opts_list = qemu_find_opts(key);
3859 if (!opts_list || opts_list->desc->name) {
3860 goto bad_type;
3861 }
3862 while (qemu_isspace(*p)) {
3863 p++;
3864 }
3865 if (!*p)
3866 break;
3867 if (get_str(buf, sizeof(buf), &p) < 0) {
3868 goto fail;
3869 }
3870 opts = qemu_opts_parse(opts_list, buf, 1);
3871 if (!opts) {
3872 goto fail;
3873 }
3874 qemu_opts_to_qdict(opts, qdict);
3875 qemu_opts_del(opts);
3876 }
3877 break;
3878 case '/':
3879 {
3880 int count, format, size;
3881
3882 while (qemu_isspace(*p))
3883 p++;
3884 if (*p == '/') {
3885 /* format found */
3886 p++;
3887 count = 1;
3888 if (qemu_isdigit(*p)) {
3889 count = 0;
3890 while (qemu_isdigit(*p)) {
3891 count = count * 10 + (*p - '0');
3892 p++;
3893 }
3894 }
3895 size = -1;
3896 format = -1;
3897 for(;;) {
3898 switch(*p) {
3899 case 'o':
3900 case 'd':
3901 case 'u':
3902 case 'x':
3903 case 'i':
3904 case 'c':
3905 format = *p++;
3906 break;
3907 case 'b':
3908 size = 1;
3909 p++;
3910 break;
3911 case 'h':
3912 size = 2;
3913 p++;
3914 break;
3915 case 'w':
3916 size = 4;
3917 p++;
3918 break;
3919 case 'g':
3920 case 'L':
3921 size = 8;
3922 p++;
3923 break;
3924 default:
3925 goto next;
3926 }
3927 }
3928 next:
3929 if (*p != '\0' && !qemu_isspace(*p)) {
3930 monitor_printf(mon, "invalid char in format: '%c'\n",
3931 *p);
3932 goto fail;
3933 }
3934 if (format < 0)
3935 format = default_fmt_format;
3936 if (format != 'i') {
3937 /* for 'i', not specifying a size gives -1 as size */
3938 if (size < 0)
3939 size = default_fmt_size;
3940 default_fmt_size = size;
3941 }
3942 default_fmt_format = format;
3943 } else {
3944 count = 1;
3945 format = default_fmt_format;
3946 if (format != 'i') {
3947 size = default_fmt_size;
3948 } else {
3949 size = -1;
3950 }
3951 }
3952 qdict_put(qdict, "count", qint_from_int(count));
3953 qdict_put(qdict, "format", qint_from_int(format));
3954 qdict_put(qdict, "size", qint_from_int(size));
3955 }
3956 break;
3957 case 'i':
3958 case 'l':
3959 case 'M':
3960 {
3961 int64_t val;
3962
3963 while (qemu_isspace(*p))
3964 p++;
3965 if (*typestr == '?' || *typestr == '.') {
3966 if (*typestr == '?') {
3967 if (*p == '\0') {
3968 typestr++;
3969 break;
3970 }
3971 } else {
3972 if (*p == '.') {
3973 p++;
3974 while (qemu_isspace(*p))
3975 p++;
3976 } else {
3977 typestr++;
3978 break;
3979 }
3980 }
3981 typestr++;
3982 }
3983 if (get_expr(mon, &val, &p))
3984 goto fail;
3985 /* Check if 'i' is greater than 32-bit */
3986 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3987 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3988 monitor_printf(mon, "integer is for 32-bit values\n");
3989 goto fail;
3990 } else if (c == 'M') {
3991 val <<= 20;
3992 }
3993 qdict_put(qdict, key, qint_from_int(val));
3994 }
3995 break;
3996 case 'o':
3997 {
3998 int64_t val;
3999 char *end;
4000
4001 while (qemu_isspace(*p)) {
4002 p++;
4003 }
4004 if (*typestr == '?') {
4005 typestr++;
4006 if (*p == '\0') {
4007 break;
4008 }
4009 }
4010 val = strtosz(p, &end);
4011 if (val < 0) {
4012 monitor_printf(mon, "invalid size\n");
4013 goto fail;
4014 }
4015 qdict_put(qdict, key, qint_from_int(val));
4016 p = end;
4017 }
4018 break;
4019 case 'T':
4020 {
4021 double val;
4022
4023 while (qemu_isspace(*p))
4024 p++;
4025 if (*typestr == '?') {
4026 typestr++;
4027 if (*p == '\0') {
4028 break;
4029 }
4030 }
4031 if (get_double(mon, &val, &p) < 0) {
4032 goto fail;
4033 }
4034 if (p[0] && p[1] == 's') {
4035 switch (*p) {
4036 case 'm':
4037 val /= 1e3; p += 2; break;
4038 case 'u':
4039 val /= 1e6; p += 2; break;
4040 case 'n':
4041 val /= 1e9; p += 2; break;
4042 }
4043 }
4044 if (*p && !qemu_isspace(*p)) {
4045 monitor_printf(mon, "Unknown unit suffix\n");
4046 goto fail;
4047 }
4048 qdict_put(qdict, key, qfloat_from_double(val));
4049 }
4050 break;
4051 case 'b':
4052 {
4053 const char *beg;
4054 int val;
4055
4056 while (qemu_isspace(*p)) {
4057 p++;
4058 }
4059 beg = p;
4060 while (qemu_isgraph(*p)) {
4061 p++;
4062 }
4063 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4064 val = 1;
4065 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4066 val = 0;
4067 } else {
4068 monitor_printf(mon, "Expected 'on' or 'off'\n");
4069 goto fail;
4070 }
4071 qdict_put(qdict, key, qbool_from_int(val));
4072 }
4073 break;
4074 case '-':
4075 {
4076 const char *tmp = p;
4077 int skip_key = 0;
4078 /* option */
4079
4080 c = *typestr++;
4081 if (c == '\0')
4082 goto bad_type;
4083 while (qemu_isspace(*p))
4084 p++;
4085 if (*p == '-') {
4086 p++;
4087 if(c != *p) {
4088 if(!is_valid_option(p, typestr)) {
4089
4090 monitor_printf(mon, "%s: unsupported option -%c\n",
4091 cmdname, *p);
4092 goto fail;
4093 } else {
4094 skip_key = 1;
4095 }
4096 }
4097 if(skip_key) {
4098 p = tmp;
4099 } else {
4100 /* has option */
4101 p++;
4102 qdict_put(qdict, key, qbool_from_int(1));
4103 }
4104 }
4105 }
4106 break;
4107 default:
4108 bad_type:
4109 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
4110 goto fail;
4111 }
4112 g_free(key);
4113 key = NULL;
4114 }
4115 /* check that all arguments were parsed */
4116 while (qemu_isspace(*p))
4117 p++;
4118 if (*p != '\0') {
4119 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4120 cmdname);
4121 goto fail;
4122 }
4123
4124 return cmd;
4125
4126 fail:
4127 g_free(key);
4128 return NULL;
4129 }
4130
4131 void monitor_set_error(Monitor *mon, QError *qerror)
4132 {
4133 /* report only the first error */
4134 if (!mon->error) {
4135 mon->error = qerror;
4136 } else {
4137 MON_DEBUG("Additional error report at %s:%d\n",
4138 qerror->file, qerror->linenr);
4139 QDECREF(qerror);
4140 }
4141 }
4142
4143 static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4144 {
4145 if (ret && !monitor_has_error(mon)) {
4146 /*
4147 * If it returns failure, it must have passed on error.
4148 *
4149 * Action: Report an internal error to the client if in QMP.
4150 */
4151 qerror_report(QERR_UNDEFINED_ERROR);
4152 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4153 cmd->name);
4154 }
4155
4156 #ifdef CONFIG_DEBUG_MONITOR
4157 if (!ret && monitor_has_error(mon)) {
4158 /*
4159 * If it returns success, it must not have passed an error.
4160 *
4161 * Action: Report the passed error to the client.
4162 */
4163 MON_DEBUG("command '%s' returned success but passed an error\n",
4164 cmd->name);
4165 }
4166
4167 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4168 /*
4169 * Handlers should not call Monitor print functions.
4170 *
4171 * Action: Ignore them in QMP.
4172 *
4173 * (XXX: we don't check any 'info' or 'query' command here
4174 * because the user print function _is_ called by do_info(), hence
4175 * we will trigger this check. This problem will go away when we
4176 * make 'query' commands real and kill do_info())
4177 */
4178 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4179 cmd->name, mon_print_count_get(mon));
4180 }
4181 #endif
4182 }
4183
4184 static void handle_user_command(Monitor *mon, const char *cmdline)
4185 {
4186 QDict *qdict;
4187 const mon_cmd_t *cmd;
4188
4189 qdict = qdict_new();
4190
4191 cmd = monitor_parse_command(mon, cmdline, qdict);
4192 if (!cmd)
4193 goto out;
4194
4195 if (handler_is_async(cmd)) {
4196 user_async_cmd_handler(mon, cmd, qdict);
4197 } else if (handler_is_qobject(cmd)) {
4198 QObject *data = NULL;
4199
4200 /* XXX: ignores the error code */
4201 cmd->mhandler.cmd_new(mon, qdict, &data);
4202 assert(!monitor_has_error(mon));
4203 if (data) {
4204 cmd->user_print(mon, data);
4205 qobject_decref(data);
4206 }
4207 } else {
4208 cmd->mhandler.cmd(mon, qdict);
4209 }
4210
4211 out:
4212 QDECREF(qdict);
4213 }
4214
4215 static void cmd_completion(const char *name, const char *list)
4216 {
4217 const char *p, *pstart;
4218 char cmd[128];
4219 int len;
4220
4221 p = list;
4222 for(;;) {
4223 pstart = p;
4224 p = strchr(p, '|');
4225 if (!p)
4226 p = pstart + strlen(pstart);
4227 len = p - pstart;
4228 if (len > sizeof(cmd) - 2)
4229 len = sizeof(cmd) - 2;
4230 memcpy(cmd, pstart, len);
4231 cmd[len] = '\0';
4232 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
4233 readline_add_completion(cur_mon->rs, cmd);
4234 }
4235 if (*p == '\0')
4236 break;
4237 p++;
4238 }
4239 }
4240
4241 static void file_completion(const char *input)
4242 {
4243 DIR *ffs;
4244 struct dirent *d;
4245 char path[1024];
4246 char file[1024], file_prefix[1024];
4247 int input_path_len;
4248 const char *p;
4249
4250 p = strrchr(input, '/');
4251 if (!p) {
4252 input_path_len = 0;
4253 pstrcpy(file_prefix, sizeof(file_prefix), input);
4254 pstrcpy(path, sizeof(path), ".");
4255 } else {
4256 input_path_len = p - input + 1;
4257 memcpy(path, input, input_path_len);
4258 if (input_path_len > sizeof(path) - 1)
4259 input_path_len = sizeof(path) - 1;
4260 path[input_path_len] = '\0';
4261 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4262 }
4263 #ifdef DEBUG_COMPLETION
4264 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4265 input, path, file_prefix);
4266 #endif
4267 ffs = opendir(path);
4268 if (!ffs)
4269 return;
4270 for(;;) {
4271 struct stat sb;
4272 d = readdir(ffs);
4273 if (!d)
4274 break;
4275
4276 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4277 continue;
4278 }
4279
4280 if (strstart(d->d_name, file_prefix, NULL)) {
4281 memcpy(file, input, input_path_len);
4282 if (input_path_len < sizeof(file))
4283 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4284 d->d_name);
4285 /* stat the file to find out if it's a directory.
4286 * In that case add a slash to speed up typing long paths
4287 */
4288 stat(file, &sb);
4289 if(S_ISDIR(sb.st_mode))
4290 pstrcat(file, sizeof(file), "/");
4291 readline_add_completion(cur_mon->rs, file);
4292 }
4293 }
4294 closedir(ffs);
4295 }
4296
4297 static void block_completion_it(void *opaque, BlockDriverState *bs)
4298 {
4299 const char *name = bdrv_get_device_name(bs);
4300 const char *input = opaque;
4301
4302 if (input[0] == '\0' ||
4303 !strncmp(name, (char *)input, strlen(input))) {
4304 readline_add_completion(cur_mon->rs, name);
4305 }
4306 }
4307
4308 /* NOTE: this parser is an approximate form of the real command parser */
4309 static void parse_cmdline(const char *cmdline,
4310 int *pnb_args, char **args)
4311 {
4312 const char *p;
4313 int nb_args, ret;
4314 char buf[1024];
4315
4316 p = cmdline;
4317 nb_args = 0;
4318 for(;;) {
4319 while (qemu_isspace(*p))
4320 p++;
4321 if (*p == '\0')
4322 break;
4323 if (nb_args >= MAX_ARGS)
4324 break;
4325 ret = get_str(buf, sizeof(buf), &p);
4326 args[nb_args] = g_strdup(buf);
4327 nb_args++;
4328 if (ret < 0)
4329 break;
4330 }
4331 *pnb_args = nb_args;
4332 }
4333
4334 static const char *next_arg_type(const char *typestr)
4335 {
4336 const char *p = strchr(typestr, ':');
4337 return (p != NULL ? ++p : typestr);
4338 }
4339
4340 static void monitor_find_completion(const char *cmdline)
4341 {
4342 const char *cmdname;
4343 char *args[MAX_ARGS];
4344 int nb_args, i, len;
4345 const char *ptype, *str;
4346 const mon_cmd_t *cmd;
4347 const KeyDef *key;
4348
4349 parse_cmdline(cmdline, &nb_args, args);
4350 #ifdef DEBUG_COMPLETION
4351 for(i = 0; i < nb_args; i++) {
4352 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
4353 }
4354 #endif
4355
4356 /* if the line ends with a space, it means we want to complete the
4357 next arg */
4358 len = strlen(cmdline);
4359 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4360 if (nb_args >= MAX_ARGS) {
4361 goto cleanup;
4362 }
4363 args[nb_args++] = g_strdup("");
4364 }
4365 if (nb_args <= 1) {
4366 /* command completion */
4367 if (nb_args == 0)
4368 cmdname = "";
4369 else
4370 cmdname = args[0];
4371 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
4372 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
4373 cmd_completion(cmdname, cmd->name);
4374 }
4375 } else {
4376 /* find the command */
4377 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4378 if (compare_cmd(args[0], cmd->name)) {
4379 break;
4380 }
4381 }
4382 if (!cmd->name) {
4383 goto cleanup;
4384 }
4385
4386 ptype = next_arg_type(cmd->args_type);
4387 for(i = 0; i < nb_args - 2; i++) {
4388 if (*ptype != '\0') {
4389 ptype = next_arg_type(ptype);
4390 while (*ptype == '?')
4391 ptype = next_arg_type(ptype);
4392 }
4393 }
4394 str = args[nb_args - 1];
4395 if (*ptype == '-' && ptype[1] != '\0') {
4396 ptype = next_arg_type(ptype);
4397 }
4398 switch(*ptype) {
4399 case 'F':
4400 /* file completion */
4401 readline_set_completion_index(cur_mon->rs, strlen(str));
4402 file_completion(str);
4403 break;
4404 case 'B':
4405 /* block device name completion */
4406 readline_set_completion_index(cur_mon->rs, strlen(str));
4407 bdrv_iterate(block_completion_it, (void *)str);
4408 break;
4409 case 's':
4410 /* XXX: more generic ? */
4411 if (!strcmp(cmd->name, "info")) {
4412 readline_set_completion_index(cur_mon->rs, strlen(str));
4413 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4414 cmd_completion(str, cmd->name);
4415 }
4416 } else if (!strcmp(cmd->name, "sendkey")) {
4417 char *sep = strrchr(str, '-');
4418 if (sep)
4419 str = sep + 1;
4420 readline_set_completion_index(cur_mon->rs, strlen(str));
4421 for(key = key_defs; key->name != NULL; key++) {
4422 cmd_completion(str, key->name);
4423 }
4424 } else if (!strcmp(cmd->name, "help|?")) {
4425 readline_set_completion_index(cur_mon->rs, strlen(str));
4426 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4427 cmd_completion(str, cmd->name);
4428 }
4429 }
4430 break;
4431 default:
4432 break;
4433 }
4434 }
4435
4436 cleanup:
4437 for (i = 0; i < nb_args; i++) {
4438 g_free(args[i]);
4439 }
4440 }
4441
4442 static int monitor_can_read(void *opaque)
4443 {
4444 Monitor *mon = opaque;
4445
4446 return (mon->suspend_cnt == 0) ? 1 : 0;
4447 }
4448
4449 static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4450 {
4451 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4452 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4453 }
4454
4455 /*
4456 * Argument validation rules:
4457 *
4458 * 1. The argument must exist in cmd_args qdict
4459 * 2. The argument type must be the expected one
4460 *
4461 * Special case: If the argument doesn't exist in cmd_args and
4462 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4463 * checking is skipped for it.
4464 */
4465 static int check_client_args_type(const QDict *client_args,
4466 const QDict *cmd_args, int flags)
4467 {
4468 const QDictEntry *ent;
4469
4470 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4471 QObject *obj;
4472 QString *arg_type;
4473 const QObject *client_arg = qdict_entry_value(ent);
4474 const char *client_arg_name = qdict_entry_key(ent);
4475
4476 obj = qdict_get(cmd_args, client_arg_name);
4477 if (!obj) {
4478 if (flags & QMP_ACCEPT_UNKNOWNS) {
4479 /* handler accepts unknowns */
4480 continue;
4481 }
4482 /* client arg doesn't exist */
4483 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4484 return -1;
4485 }
4486
4487 arg_type = qobject_to_qstring(obj);
4488 assert(arg_type != NULL);
4489
4490 /* check if argument's type is correct */
4491 switch (qstring_get_str(arg_type)[0]) {
4492 case 'F':
4493 case 'B':
4494 case 's':
4495 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4496 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4497 "string");
4498 return -1;
4499 }
4500 break;
4501 case 'i':
4502 case 'l':
4503 case 'M':
4504 case 'o':
4505 if (qobject_type(client_arg) != QTYPE_QINT) {
4506 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4507 "int");
4508 return -1;
4509 }
4510 break;
4511 case 'T':
4512 if (qobject_type(client_arg) != QTYPE_QINT &&
4513 qobject_type(client_arg) != QTYPE_QFLOAT) {
4514 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4515 "number");
4516 return -1;
4517 }
4518 break;
4519 case 'b':
4520 case '-':
4521 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4522 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4523 "bool");
4524 return -1;
4525 }
4526 break;
4527 case 'O':
4528 assert(flags & QMP_ACCEPT_UNKNOWNS);
4529 break;
4530 case '/':
4531 case '.':
4532 /*
4533 * These types are not supported by QMP and thus are not
4534 * handled here. Fall through.
4535 */
4536 default:
4537 abort();
4538 }
4539 }
4540
4541 return 0;
4542 }
4543
4544 /*
4545 * - Check if the client has passed all mandatory args
4546 * - Set special flags for argument validation
4547 */
4548 static int check_mandatory_args(const QDict *cmd_args,
4549 const QDict *client_args, int *flags)
4550 {
4551 const QDictEntry *ent;
4552
4553 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4554 const char *cmd_arg_name = qdict_entry_key(ent);
4555 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4556 assert(type != NULL);
4557
4558 if (qstring_get_str(type)[0] == 'O') {
4559 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4560 *flags |= QMP_ACCEPT_UNKNOWNS;
4561 } else if (qstring_get_str(type)[0] != '-' &&
4562 qstring_get_str(type)[1] != '?' &&
4563 !qdict_haskey(client_args, cmd_arg_name)) {
4564 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4565 return -1;
4566 }
4567 }
4568
4569 return 0;
4570 }
4571
4572 static QDict *qdict_from_args_type(const char *args_type)
4573 {
4574 int i;
4575 QDict *qdict;
4576 QString *key, *type, *cur_qs;
4577
4578 assert(args_type != NULL);
4579
4580 qdict = qdict_new();
4581
4582 if (args_type == NULL || args_type[0] == '\0') {
4583 /* no args, empty qdict */
4584 goto out;
4585 }
4586
4587 key = qstring_new();
4588 type = qstring_new();
4589
4590 cur_qs = key;
4591
4592 for (i = 0;; i++) {
4593 switch (args_type[i]) {
4594 case ',':
4595 case '\0':
4596 qdict_put(qdict, qstring_get_str(key), type);
4597 QDECREF(key);
4598 if (args_type[i] == '\0') {
4599 goto out;
4600 }
4601 type = qstring_new(); /* qdict has ref */
4602 cur_qs = key = qstring_new();
4603 break;
4604 case ':':
4605 cur_qs = type;
4606 break;
4607 default:
4608 qstring_append_chr(cur_qs, args_type[i]);
4609 break;
4610 }
4611 }
4612
4613 out:
4614 return qdict;
4615 }
4616
4617 /*
4618 * Client argument checking rules:
4619 *
4620 * 1. Client must provide all mandatory arguments
4621 * 2. Each argument provided by the client must be expected
4622 * 3. Each argument provided by the client must have the type expected
4623 * by the command
4624 */
4625 static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4626 {
4627 int flags, err;
4628 QDict *cmd_args;
4629
4630 cmd_args = qdict_from_args_type(cmd->args_type);
4631
4632 flags = 0;
4633 err = check_mandatory_args(cmd_args, client_args, &flags);
4634 if (err) {
4635 goto out;
4636 }
4637
4638 err = check_client_args_type(client_args, cmd_args, flags);
4639
4640 out:
4641 QDECREF(cmd_args);
4642 return err;
4643 }
4644
4645 /*
4646 * Input object checking rules
4647 *
4648 * 1. Input object must be a dict
4649 * 2. The "execute" key must exist
4650 * 3. The "execute" key must be a string
4651 * 4. If the "arguments" key exists, it must be a dict
4652 * 5. If the "id" key exists, it can be anything (ie. json-value)
4653 * 6. Any argument not listed above is considered invalid
4654 */
4655 static QDict *qmp_check_input_obj(QObject *input_obj)
4656 {
4657 const QDictEntry *ent;
4658 int has_exec_key = 0;
4659 QDict *input_dict;
4660
4661 if (qobject_type(input_obj) != QTYPE_QDICT) {
4662 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4663 return NULL;
4664 }
4665
4666 input_dict = qobject_to_qdict(input_obj);
4667
4668 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4669 const char *arg_name = qdict_entry_key(ent);
4670 const QObject *arg_obj = qdict_entry_value(ent);
4671
4672 if (!strcmp(arg_name, "execute")) {
4673 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4674 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4675 "string");
4676 return NULL;
4677 }
4678 has_exec_key = 1;
4679 } else if (!strcmp(arg_name, "arguments")) {
4680 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4681 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4682 "object");
4683 return NULL;
4684 }
4685 } else if (!strcmp(arg_name, "id")) {
4686 /* FIXME: check duplicated IDs for async commands */
4687 } else {
4688 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4689 return NULL;
4690 }
4691 }
4692
4693 if (!has_exec_key) {
4694 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4695 return NULL;
4696 }
4697
4698 return input_dict;
4699 }
4700
4701 static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4702 {
4703 QObject *ret_data = NULL;
4704
4705 if (handler_is_async(cmd)) {
4706 qmp_async_info_handler(mon, cmd);
4707 if (monitor_has_error(mon)) {
4708 monitor_protocol_emitter(mon, NULL);
4709 }
4710 } else {
4711 cmd->mhandler.info_new(mon, &ret_data);
4712 monitor_protocol_emitter(mon, ret_data);
4713 qobject_decref(ret_data);
4714 }
4715 }
4716
4717 static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4718 const QDict *params)
4719 {
4720 int ret;
4721 QObject *data = NULL;
4722
4723 mon_print_count_init(mon);
4724
4725 ret = cmd->mhandler.cmd_new(mon, params, &data);
4726 handler_audit(mon, cmd, ret);
4727 monitor_protocol_emitter(mon, data);
4728 qobject_decref(data);
4729 }
4730
4731 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4732 {
4733 int err;
4734 QObject *obj;
4735 QDict *input, *args;
4736 const mon_cmd_t *cmd;
4737 Monitor *mon = cur_mon;
4738 const char *cmd_name, *query_cmd;
4739
4740 query_cmd = NULL;
4741 args = input = NULL;
4742
4743 obj = json_parser_parse(tokens, NULL);
4744 if (!obj) {
4745 // FIXME: should be triggered in json_parser_parse()
4746 qerror_report(QERR_JSON_PARSING);
4747 goto err_out;
4748 }
4749
4750 input = qmp_check_input_obj(obj);
4751 if (!input) {
4752 qobject_decref(obj);
4753 goto err_out;
4754 }
4755
4756 mon->mc->id = qdict_get(input, "id");
4757 qobject_incref(mon->mc->id);
4758
4759 cmd_name = qdict_get_str(input, "execute");
4760 trace_handle_qmp_command(mon, cmd_name);
4761 if (invalid_qmp_mode(mon, cmd_name)) {
4762 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4763 goto err_out;
4764 }
4765
4766 cmd = qmp_find_cmd(cmd_name);
4767 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
4768 cmd = qmp_find_query_cmd(query_cmd);
4769 }
4770 if (!cmd) {
4771 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4772 goto err_out;
4773 }
4774
4775 obj = qdict_get(input, "arguments");
4776 if (!obj) {
4777 args = qdict_new();
4778 } else {
4779 args = qobject_to_qdict(obj);
4780 QINCREF(args);
4781 }
4782
4783 err = qmp_check_client_args(cmd, args);
4784 if (err < 0) {
4785 goto err_out;
4786 }
4787
4788 if (query_cmd) {
4789 qmp_call_query_cmd(mon, cmd);
4790 } else if (handler_is_async(cmd)) {
4791 err = qmp_async_cmd_handler(mon, cmd, args);
4792 if (err) {
4793 /* emit the error response */
4794 goto err_out;
4795 }
4796 } else {
4797 qmp_call_cmd(mon, cmd, args);
4798 }
4799
4800 goto out;
4801
4802 err_out:
4803 monitor_protocol_emitter(mon, NULL);
4804 out:
4805 QDECREF(input);
4806 QDECREF(args);
4807 }
4808
4809 /**
4810 * monitor_control_read(): Read and handle QMP input
4811 */
4812 static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4813 {
4814 Monitor *old_mon = cur_mon;
4815
4816 cur_mon = opaque;
4817
4818 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
4819
4820 cur_mon = old_mon;
4821 }
4822
4823 static void monitor_read(void *opaque, const uint8_t *buf, int size)
4824 {
4825 Monitor *old_mon = cur_mon;
4826 int i;
4827
4828 cur_mon = opaque;
4829
4830 if (cur_mon->rs) {
4831 for (i = 0; i < size; i++)
4832 readline_handle_byte(cur_mon->rs, buf[i]);
4833 } else {
4834 if (size == 0 || buf[size - 1] != 0)
4835 monitor_printf(cur_mon, "corrupted command\n");
4836 else
4837 handle_user_command(cur_mon, (char *)buf);
4838 }
4839
4840 cur_mon = old_mon;
4841 }
4842
4843 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
4844 {
4845 monitor_suspend(mon);
4846 handle_user_command(mon, cmdline);
4847 monitor_resume(mon);
4848 }
4849
4850 int monitor_suspend(Monitor *mon)
4851 {
4852 if (!mon->rs)
4853 return -ENOTTY;
4854 mon->suspend_cnt++;
4855 return 0;
4856 }
4857
4858 void monitor_resume(Monitor *mon)
4859 {
4860 if (!mon->rs)
4861 return;
4862 if (--mon->suspend_cnt == 0)
4863 readline_show_prompt(mon->rs);
4864 }
4865
4866 static QObject *get_qmp_greeting(void)
4867 {
4868 QObject *ver = NULL;
4869
4870 qmp_marshal_input_query_version(NULL, NULL, &ver);
4871 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4872 }
4873
4874 /**
4875 * monitor_control_event(): Print QMP gretting
4876 */
4877 static void monitor_control_event(void *opaque, int event)
4878 {
4879 QObject *data;
4880 Monitor *mon = opaque;
4881
4882 switch (event) {
4883 case CHR_EVENT_OPENED:
4884 mon->mc->command_mode = 0;
4885 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4886 data = get_qmp_greeting();
4887 monitor_json_emitter(mon, data);
4888 qobject_decref(data);
4889 break;
4890 case CHR_EVENT_CLOSED:
4891 json_message_parser_destroy(&mon->mc->parser);
4892 break;
4893 }
4894 }
4895
4896 static void monitor_event(void *opaque, int event)
4897 {
4898 Monitor *mon = opaque;
4899
4900 switch (event) {
4901 case CHR_EVENT_MUX_IN:
4902 mon->mux_out = 0;
4903 if (mon->reset_seen) {
4904 readline_restart(mon->rs);
4905 monitor_resume(mon);
4906 monitor_flush(mon);
4907 } else {
4908 mon->suspend_cnt = 0;
4909 }
4910 break;
4911
4912 case CHR_EVENT_MUX_OUT:
4913 if (mon->reset_seen) {
4914 if (mon->suspend_cnt == 0) {
4915 monitor_printf(mon, "\n");
4916 }
4917 monitor_flush(mon);
4918 monitor_suspend(mon);
4919 } else {
4920 mon->suspend_cnt++;
4921 }
4922 mon->mux_out = 1;
4923 break;
4924
4925 case CHR_EVENT_OPENED:
4926 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4927 "information\n", QEMU_VERSION);
4928 if (!mon->mux_out) {
4929 readline_show_prompt(mon->rs);
4930 }
4931 mon->reset_seen = 1;
4932 break;
4933 }
4934 }
4935
4936
4937 /*
4938 * Local variables:
4939 * c-indent-level: 4
4940 * c-basic-offset: 4
4941 * tab-width: 8
4942 * End:
4943 */
4944
4945 void monitor_init(CharDriverState *chr, int flags)
4946 {
4947 static int is_first_init = 1;
4948 Monitor *mon;
4949
4950 if (is_first_init) {
4951 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
4952 is_first_init = 0;
4953 }
4954
4955 mon = g_malloc0(sizeof(*mon));
4956
4957 mon->chr = chr;
4958 mon->flags = flags;
4959 if (flags & MONITOR_USE_READLINE) {
4960 mon->rs = readline_init(mon, monitor_find_completion);
4961 monitor_read_command(mon, 0);
4962 }
4963
4964 if (monitor_ctrl_mode(mon)) {
4965 mon->mc = g_malloc0(sizeof(MonitorControl));
4966 /* Control mode requires special handlers */
4967 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4968 monitor_control_event, mon);
4969 qemu_chr_fe_set_echo(chr, true);
4970 } else {
4971 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4972 monitor_event, mon);
4973 }
4974
4975 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4976 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4977 default_mon = mon;
4978 }
4979
4980 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
4981 {
4982 BlockDriverState *bs = opaque;
4983 int ret = 0;
4984
4985 if (bdrv_set_key(bs, password) != 0) {
4986 monitor_printf(mon, "invalid password\n");
4987 ret = -EPERM;
4988 }
4989 if (mon->password_completion_cb)
4990 mon->password_completion_cb(mon->password_opaque, ret);
4991
4992 monitor_read_command(mon, 1);
4993 }
4994
4995 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4996 BlockDriverCompletionFunc *completion_cb,
4997 void *opaque)
4998 {
4999 int err;
5000
5001 if (!bdrv_key_required(bs)) {
5002 if (completion_cb)
5003 completion_cb(opaque, 0);
5004 return 0;
5005 }
5006
5007 if (monitor_ctrl_mode(mon)) {
5008 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
5009 return -1;
5010 }
5011
5012 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5013 bdrv_get_encrypted_filename(bs));
5014
5015 mon->password_completion_cb = completion_cb;
5016 mon->password_opaque = opaque;
5017
5018 err = monitor_read_password(mon, bdrv_password_cb, bs);
5019
5020 if (err && completion_cb)
5021 completion_cb(opaque, err);
5022
5023 return err;
5024 }