monitor: protect outbuf and mux_out with mutex
[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 "monitor/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/pcmcia.h"
29 #include "hw/i386/pc.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
34 #include "net/net.h"
35 #include "net/slirp.h"
36 #include "sysemu/char.h"
37 #include "ui/qemu-spice.h"
38 #include "sysemu/sysemu.h"
39 #include "monitor/monitor.h"
40 #include "qemu/readline.h"
41 #include "ui/console.h"
42 #include "ui/input.h"
43 #include "sysemu/blockdev.h"
44 #include "audio/audio.h"
45 #include "disas/disas.h"
46 #include "sysemu/balloon.h"
47 #include "qemu/timer.h"
48 #include "migration/migration.h"
49 #include "sysemu/kvm.h"
50 #include "qemu/acl.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qint.h"
53 #include "qapi/qmp/qfloat.h"
54 #include "qapi/qmp/qlist.h"
55 #include "qapi/qmp/qbool.h"
56 #include "qapi/qmp/qstring.h"
57 #include "qapi/qmp/qjson.h"
58 #include "qapi/qmp/json-streamer.h"
59 #include "qapi/qmp/json-parser.h"
60 #include <qom/object_interfaces.h>
61 #include "qemu/osdep.h"
62 #include "cpu.h"
63 #include "trace.h"
64 #include "trace/control.h"
65 #ifdef CONFIG_TRACE_SIMPLE
66 #include "trace/simple.h"
67 #endif
68 #include "exec/memory.h"
69 #include "exec/cpu_ldst.h"
70 #include "qmp-commands.h"
71 #include "hmp.h"
72 #include "qemu/thread.h"
73 #include "block/qapi.h"
74 #include "qapi/qmp-event.h"
75 #include "qapi-event.h"
76
77 /* for pic/irq_info */
78 #if defined(TARGET_SPARC)
79 #include "hw/sparc/sun4m.h"
80 #endif
81 #include "hw/lm32/lm32_pic.h"
82
83 //#define DEBUG
84 //#define DEBUG_COMPLETION
85
86 /*
87 * Supported types:
88 *
89 * 'F' filename
90 * 'B' block device name
91 * 's' string (accept optional quote)
92 * 'S' it just appends the rest of the string (accept optional quote)
93 * 'O' option string of the form NAME=VALUE,...
94 * parsed according to QemuOptsList given by its name
95 * Example: 'device:O' uses qemu_device_opts.
96 * Restriction: only lists with empty desc are supported
97 * TODO lift the restriction
98 * 'i' 32 bit integer
99 * 'l' target long (32 or 64 bit)
100 * 'M' Non-negative target long (32 or 64 bit), in user mode the
101 * value is multiplied by 2^20 (think Mebibyte)
102 * 'o' octets (aka bytes)
103 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
104 * K, k suffix, which multiplies the value by 2^60 for suffixes E
105 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
106 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
107 * 'T' double
108 * user mode accepts an optional ms, us, ns suffix,
109 * which divides the value by 1e3, 1e6, 1e9, respectively
110 * '/' optional gdb-like print format (like "/10x")
111 *
112 * '?' optional type (for all types, except '/')
113 * '.' other form of optional type (for 'i' and 'l')
114 * 'b' boolean
115 * user mode accepts "on" or "off"
116 * '-' optional parameter (eg. '-f')
117 *
118 */
119
120 typedef struct MonitorCompletionData MonitorCompletionData;
121 struct MonitorCompletionData {
122 Monitor *mon;
123 void (*user_print)(Monitor *mon, const QObject *data);
124 };
125
126 typedef struct mon_cmd_t {
127 const char *name;
128 const char *args_type;
129 const char *params;
130 const char *help;
131 void (*user_print)(Monitor *mon, const QObject *data);
132 union {
133 void (*cmd)(Monitor *mon, const QDict *qdict);
134 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
135 int (*cmd_async)(Monitor *mon, const QDict *params,
136 MonitorCompletion *cb, void *opaque);
137 } mhandler;
138 int flags;
139 /* @sub_table is a list of 2nd level of commands. If it do not exist,
140 * mhandler should be used. If it exist, sub_table[?].mhandler should be
141 * used, and mhandler of 1st level plays the role of help function.
142 */
143 struct mon_cmd_t *sub_table;
144 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
145 } mon_cmd_t;
146
147 /* file descriptors passed via SCM_RIGHTS */
148 typedef struct mon_fd_t mon_fd_t;
149 struct mon_fd_t {
150 char *name;
151 int fd;
152 QLIST_ENTRY(mon_fd_t) next;
153 };
154
155 /* file descriptor associated with a file descriptor set */
156 typedef struct MonFdsetFd MonFdsetFd;
157 struct MonFdsetFd {
158 int fd;
159 bool removed;
160 char *opaque;
161 QLIST_ENTRY(MonFdsetFd) next;
162 };
163
164 /* file descriptor set containing fds passed via SCM_RIGHTS */
165 typedef struct MonFdset MonFdset;
166 struct MonFdset {
167 int64_t id;
168 QLIST_HEAD(, MonFdsetFd) fds;
169 QLIST_HEAD(, MonFdsetFd) dup_fds;
170 QLIST_ENTRY(MonFdset) next;
171 };
172
173 typedef struct MonitorControl {
174 QObject *id;
175 JSONMessageParser parser;
176 int command_mode;
177 } MonitorControl;
178
179 /*
180 * To prevent flooding clients, events can be throttled. The
181 * throttling is calculated globally, rather than per-Monitor
182 * instance.
183 */
184 typedef struct MonitorQAPIEventState {
185 QAPIEvent event; /* Event being tracked */
186 int64_t rate; /* Minimum time (in ns) between two events */
187 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
188 QEMUTimer *timer; /* Timer for handling delayed events */
189 QObject *data; /* Event pending delayed dispatch */
190 } MonitorQAPIEventState;
191
192 struct Monitor {
193 CharDriverState *chr;
194 int reset_seen;
195 int flags;
196 int suspend_cnt;
197 bool skip_flush;
198
199 QemuMutex out_lock;
200 QString *outbuf;
201 guint out_watch;
202
203 /* Read under either BQL or out_lock, written with BQL+out_lock. */
204 int mux_out;
205
206 ReadLineState *rs;
207 MonitorControl *mc;
208 CPUState *mon_cpu;
209 BlockDriverCompletionFunc *password_completion_cb;
210 void *password_opaque;
211 mon_cmd_t *cmd_table;
212 QError *error;
213 QLIST_HEAD(,mon_fd_t) fds;
214 QLIST_ENTRY(Monitor) entry;
215 };
216
217 /* QMP checker flags */
218 #define QMP_ACCEPT_UNKNOWNS 1
219
220 static QLIST_HEAD(mon_list, Monitor) mon_list;
221 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
222 static int mon_refcount;
223
224 static mon_cmd_t mon_cmds[];
225 static mon_cmd_t info_cmds[];
226
227 static const mon_cmd_t qmp_cmds[];
228
229 Monitor *cur_mon;
230 Monitor *default_mon;
231
232 static void monitor_command_cb(void *opaque, const char *cmdline,
233 void *readline_opaque);
234
235 static inline int qmp_cmd_mode(const Monitor *mon)
236 {
237 return (mon->mc ? mon->mc->command_mode : 0);
238 }
239
240 /* Return true if in control mode, false otherwise */
241 static inline int monitor_ctrl_mode(const Monitor *mon)
242 {
243 return (mon->flags & MONITOR_USE_CONTROL);
244 }
245
246 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
247 int monitor_cur_is_qmp(void)
248 {
249 return cur_mon && monitor_ctrl_mode(cur_mon);
250 }
251
252 void monitor_read_command(Monitor *mon, int show_prompt)
253 {
254 if (!mon->rs)
255 return;
256
257 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
258 if (show_prompt)
259 readline_show_prompt(mon->rs);
260 }
261
262 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
263 void *opaque)
264 {
265 if (monitor_ctrl_mode(mon)) {
266 qerror_report(QERR_MISSING_PARAMETER, "password");
267 return -EINVAL;
268 } else if (mon->rs) {
269 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
270 /* prompt is printed on return from the command handler */
271 return 0;
272 } else {
273 monitor_printf(mon, "terminal does not support password prompting\n");
274 return -ENOTTY;
275 }
276 }
277
278 static void monitor_flush_locked(Monitor *mon);
279
280 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
281 void *opaque)
282 {
283 Monitor *mon = opaque;
284
285 qemu_mutex_lock(&mon->out_lock);
286 mon->out_watch = 0;
287 monitor_flush_locked(mon);
288 qemu_mutex_unlock(&mon->out_lock);
289 return FALSE;
290 }
291
292 /* Called with mon->out_lock held. */
293 static void monitor_flush_locked(Monitor *mon)
294 {
295 int rc;
296 size_t len;
297 const char *buf;
298
299 if (mon->skip_flush) {
300 return;
301 }
302
303 buf = qstring_get_str(mon->outbuf);
304 len = qstring_get_length(mon->outbuf);
305
306 if (len && !mon->mux_out) {
307 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
308 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
309 /* all flushed or error */
310 QDECREF(mon->outbuf);
311 mon->outbuf = qstring_new();
312 return;
313 }
314 if (rc > 0) {
315 /* partinal write */
316 QString *tmp = qstring_from_str(buf + rc);
317 QDECREF(mon->outbuf);
318 mon->outbuf = tmp;
319 }
320 if (mon->out_watch == 0) {
321 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
322 monitor_unblocked, mon);
323 }
324 }
325 }
326
327 void monitor_flush(Monitor *mon)
328 {
329 qemu_mutex_lock(&mon->out_lock);
330 monitor_flush_locked(mon);
331 qemu_mutex_unlock(&mon->out_lock);
332 }
333
334 /* flush at every end of line */
335 static void monitor_puts(Monitor *mon, const char *str)
336 {
337 char c;
338
339 qemu_mutex_lock(&mon->out_lock);
340 for(;;) {
341 c = *str++;
342 if (c == '\0')
343 break;
344 if (c == '\n') {
345 qstring_append_chr(mon->outbuf, '\r');
346 }
347 qstring_append_chr(mon->outbuf, c);
348 if (c == '\n') {
349 monitor_flush_locked(mon);
350 }
351 }
352 qemu_mutex_unlock(&mon->out_lock);
353 }
354
355 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
356 {
357 char *buf;
358
359 if (!mon)
360 return;
361
362 if (monitor_ctrl_mode(mon)) {
363 return;
364 }
365
366 buf = g_strdup_vprintf(fmt, ap);
367 monitor_puts(mon, buf);
368 g_free(buf);
369 }
370
371 void monitor_printf(Monitor *mon, const char *fmt, ...)
372 {
373 va_list ap;
374 va_start(ap, fmt);
375 monitor_vprintf(mon, fmt, ap);
376 va_end(ap);
377 }
378
379 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
380 const char *fmt, ...)
381 {
382 va_list ap;
383 va_start(ap, fmt);
384 monitor_vprintf((Monitor *)stream, fmt, ap);
385 va_end(ap);
386 return 0;
387 }
388
389 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
390
391 static inline int handler_is_qobject(const mon_cmd_t *cmd)
392 {
393 return cmd->user_print != NULL;
394 }
395
396 static inline bool handler_is_async(const mon_cmd_t *cmd)
397 {
398 return cmd->flags & MONITOR_CMD_ASYNC;
399 }
400
401 static inline int monitor_has_error(const Monitor *mon)
402 {
403 return mon->error != NULL;
404 }
405
406 static void monitor_json_emitter(Monitor *mon, const QObject *data)
407 {
408 QString *json;
409
410 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
411 qobject_to_json(data);
412 assert(json != NULL);
413
414 qstring_append_chr(json, '\n');
415 monitor_puts(mon, qstring_get_str(json));
416
417 QDECREF(json);
418 }
419
420 static QDict *build_qmp_error_dict(const QError *err)
421 {
422 QObject *obj;
423
424 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
425 ErrorClass_lookup[err->err_class],
426 qerror_human(err));
427
428 return qobject_to_qdict(obj);
429 }
430
431 static void monitor_protocol_emitter(Monitor *mon, QObject *data)
432 {
433 QDict *qmp;
434
435 trace_monitor_protocol_emitter(mon);
436
437 if (!monitor_has_error(mon)) {
438 /* success response */
439 qmp = qdict_new();
440 if (data) {
441 qobject_incref(data);
442 qdict_put_obj(qmp, "return", data);
443 } else {
444 /* return an empty QDict by default */
445 qdict_put(qmp, "return", qdict_new());
446 }
447 } else {
448 /* error response */
449 qmp = build_qmp_error_dict(mon->error);
450 QDECREF(mon->error);
451 mon->error = NULL;
452 }
453
454 if (mon->mc->id) {
455 qdict_put_obj(qmp, "id", mon->mc->id);
456 mon->mc->id = NULL;
457 }
458
459 monitor_json_emitter(mon, QOBJECT(qmp));
460 QDECREF(qmp);
461 }
462
463
464 static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
465
466 /*
467 * Emits the event to every monitor instance, @event is only used for trace
468 */
469 static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
470 {
471 Monitor *mon;
472
473 trace_monitor_protocol_event_emit(event, data);
474 QLIST_FOREACH(mon, &mon_list, entry) {
475 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
476 monitor_json_emitter(mon, data);
477 }
478 }
479 }
480
481 /*
482 * Queue a new event for emission to Monitor instances,
483 * applying any rate limiting if required.
484 */
485 static void
486 monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
487 {
488 MonitorQAPIEventState *evstate;
489 assert(event < QAPI_EVENT_MAX);
490 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
491
492 evstate = &(monitor_qapi_event_state[event]);
493 trace_monitor_protocol_event_queue(event,
494 data,
495 evstate->rate,
496 evstate->last,
497 now);
498
499 /* Rate limit of 0 indicates no throttling */
500 if (!evstate->rate) {
501 monitor_qapi_event_emit(event, QOBJECT(data));
502 evstate->last = now;
503 } else {
504 int64_t delta = now - evstate->last;
505 if (evstate->data ||
506 delta < evstate->rate) {
507 /* If there's an existing event pending, replace
508 * it with the new event, otherwise schedule a
509 * timer for delayed emission
510 */
511 if (evstate->data) {
512 qobject_decref(evstate->data);
513 } else {
514 int64_t then = evstate->last + evstate->rate;
515 timer_mod_ns(evstate->timer, then);
516 }
517 evstate->data = QOBJECT(data);
518 qobject_incref(evstate->data);
519 } else {
520 monitor_qapi_event_emit(event, QOBJECT(data));
521 evstate->last = now;
522 }
523 }
524 }
525
526 /*
527 * The callback invoked by QemuTimer when a delayed
528 * event is ready to be emitted
529 */
530 static void monitor_qapi_event_handler(void *opaque)
531 {
532 MonitorQAPIEventState *evstate = opaque;
533 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
534
535 trace_monitor_protocol_event_handler(evstate->event,
536 evstate->data,
537 evstate->last,
538 now);
539 if (evstate->data) {
540 monitor_qapi_event_emit(evstate->event, evstate->data);
541 qobject_decref(evstate->data);
542 evstate->data = NULL;
543 }
544 evstate->last = now;
545 }
546
547 /*
548 * @event: the event ID to be limited
549 * @rate: the rate limit in milliseconds
550 *
551 * Sets a rate limit on a particular event, so no
552 * more than 1 event will be emitted within @rate
553 * milliseconds
554 */
555 static void
556 monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
557 {
558 MonitorQAPIEventState *evstate;
559 assert(event < QAPI_EVENT_MAX);
560
561 evstate = &(monitor_qapi_event_state[event]);
562
563 trace_monitor_protocol_event_throttle(event, rate);
564 evstate->event = event;
565 evstate->rate = rate * SCALE_MS;
566 evstate->last = 0;
567 evstate->data = NULL;
568 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
569 SCALE_MS,
570 monitor_qapi_event_handler,
571 evstate);
572 }
573
574 static void monitor_qapi_event_init(void)
575 {
576 /* Limit guest-triggerable events to 1 per second */
577 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
578 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
579 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
580 /* limit the rate of quorum events to avoid hammering the management */
581 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
582 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
583
584 qmp_event_set_func_emit(monitor_qapi_event_queue);
585 }
586
587 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
588 QObject **ret_data)
589 {
590 /* Will setup QMP capabilities in the future */
591 if (monitor_ctrl_mode(mon)) {
592 mon->mc->command_mode = 1;
593 }
594
595 return 0;
596 }
597
598 static void handle_user_command(Monitor *mon, const char *cmdline);
599
600 static void monitor_data_init(Monitor *mon)
601 {
602 memset(mon, 0, sizeof(Monitor));
603 qemu_mutex_init(&mon->out_lock);
604 mon->outbuf = qstring_new();
605 /* Use *mon_cmds by default. */
606 mon->cmd_table = mon_cmds;
607 }
608
609 static void monitor_data_destroy(Monitor *mon)
610 {
611 QDECREF(mon->outbuf);
612 qemu_mutex_destroy(&mon->out_lock);
613 }
614
615 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
616 int64_t cpu_index, Error **errp)
617 {
618 char *output = NULL;
619 Monitor *old_mon, hmp;
620
621 monitor_data_init(&hmp);
622 hmp.skip_flush = true;
623
624 old_mon = cur_mon;
625 cur_mon = &hmp;
626
627 if (has_cpu_index) {
628 int ret = monitor_set_cpu(cpu_index);
629 if (ret < 0) {
630 cur_mon = old_mon;
631 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
632 "a CPU number");
633 goto out;
634 }
635 }
636
637 handle_user_command(&hmp, command_line);
638 cur_mon = old_mon;
639
640 qemu_mutex_lock(&hmp.out_lock);
641 if (qstring_get_length(hmp.outbuf) > 0) {
642 output = g_strdup(qstring_get_str(hmp.outbuf));
643 } else {
644 output = g_strdup("");
645 }
646 qemu_mutex_unlock(&hmp.out_lock);
647
648 out:
649 monitor_data_destroy(&hmp);
650 return output;
651 }
652
653 static int compare_cmd(const char *name, const char *list)
654 {
655 const char *p, *pstart;
656 int len;
657 len = strlen(name);
658 p = list;
659 for(;;) {
660 pstart = p;
661 p = strchr(p, '|');
662 if (!p)
663 p = pstart + strlen(pstart);
664 if ((p - pstart) == len && !memcmp(pstart, name, len))
665 return 1;
666 if (*p == '\0')
667 break;
668 p++;
669 }
670 return 0;
671 }
672
673 static int get_str(char *buf, int buf_size, const char **pp)
674 {
675 const char *p;
676 char *q;
677 int c;
678
679 q = buf;
680 p = *pp;
681 while (qemu_isspace(*p)) {
682 p++;
683 }
684 if (*p == '\0') {
685 fail:
686 *q = '\0';
687 *pp = p;
688 return -1;
689 }
690 if (*p == '\"') {
691 p++;
692 while (*p != '\0' && *p != '\"') {
693 if (*p == '\\') {
694 p++;
695 c = *p++;
696 switch (c) {
697 case 'n':
698 c = '\n';
699 break;
700 case 'r':
701 c = '\r';
702 break;
703 case '\\':
704 case '\'':
705 case '\"':
706 break;
707 default:
708 qemu_printf("unsupported escape code: '\\%c'\n", c);
709 goto fail;
710 }
711 if ((q - buf) < buf_size - 1) {
712 *q++ = c;
713 }
714 } else {
715 if ((q - buf) < buf_size - 1) {
716 *q++ = *p;
717 }
718 p++;
719 }
720 }
721 if (*p != '\"') {
722 qemu_printf("unterminated string\n");
723 goto fail;
724 }
725 p++;
726 } else {
727 while (*p != '\0' && !qemu_isspace(*p)) {
728 if ((q - buf) < buf_size - 1) {
729 *q++ = *p;
730 }
731 p++;
732 }
733 }
734 *q = '\0';
735 *pp = p;
736 return 0;
737 }
738
739 #define MAX_ARGS 16
740
741 static void free_cmdline_args(char **args, int nb_args)
742 {
743 int i;
744
745 assert(nb_args <= MAX_ARGS);
746
747 for (i = 0; i < nb_args; i++) {
748 g_free(args[i]);
749 }
750
751 }
752
753 /*
754 * Parse the command line to get valid args.
755 * @cmdline: command line to be parsed.
756 * @pnb_args: location to store the number of args, must NOT be NULL.
757 * @args: location to store the args, which should be freed by caller, must
758 * NOT be NULL.
759 *
760 * Returns 0 on success, negative on failure.
761 *
762 * NOTE: this parser is an approximate form of the real command parser. Number
763 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
764 * return with failure.
765 */
766 static int parse_cmdline(const char *cmdline,
767 int *pnb_args, char **args)
768 {
769 const char *p;
770 int nb_args, ret;
771 char buf[1024];
772
773 p = cmdline;
774 nb_args = 0;
775 for (;;) {
776 while (qemu_isspace(*p)) {
777 p++;
778 }
779 if (*p == '\0') {
780 break;
781 }
782 if (nb_args >= MAX_ARGS) {
783 goto fail;
784 }
785 ret = get_str(buf, sizeof(buf), &p);
786 if (ret < 0) {
787 goto fail;
788 }
789 args[nb_args] = g_strdup(buf);
790 nb_args++;
791 }
792 *pnb_args = nb_args;
793 return 0;
794
795 fail:
796 free_cmdline_args(args, nb_args);
797 return -1;
798 }
799
800 static void help_cmd_dump_one(Monitor *mon,
801 const mon_cmd_t *cmd,
802 char **prefix_args,
803 int prefix_args_nb)
804 {
805 int i;
806
807 for (i = 0; i < prefix_args_nb; i++) {
808 monitor_printf(mon, "%s ", prefix_args[i]);
809 }
810 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
811 }
812
813 /* @args[@arg_index] is the valid command need to find in @cmds */
814 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
815 char **args, int nb_args, int arg_index)
816 {
817 const mon_cmd_t *cmd;
818
819 /* No valid arg need to compare with, dump all in *cmds */
820 if (arg_index >= nb_args) {
821 for (cmd = cmds; cmd->name != NULL; cmd++) {
822 help_cmd_dump_one(mon, cmd, args, arg_index);
823 }
824 return;
825 }
826
827 /* Find one entry to dump */
828 for (cmd = cmds; cmd->name != NULL; cmd++) {
829 if (compare_cmd(args[arg_index], cmd->name)) {
830 if (cmd->sub_table) {
831 /* continue with next arg */
832 help_cmd_dump(mon, cmd->sub_table,
833 args, nb_args, arg_index + 1);
834 } else {
835 help_cmd_dump_one(mon, cmd, args, arg_index);
836 }
837 break;
838 }
839 }
840 }
841
842 static void help_cmd(Monitor *mon, const char *name)
843 {
844 char *args[MAX_ARGS];
845 int nb_args = 0;
846
847 /* 1. parse user input */
848 if (name) {
849 /* special case for log, directly dump and return */
850 if (!strcmp(name, "log")) {
851 const QEMULogItem *item;
852 monitor_printf(mon, "Log items (comma separated):\n");
853 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
854 for (item = qemu_log_items; item->mask != 0; item++) {
855 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
856 }
857 return;
858 }
859
860 if (parse_cmdline(name, &nb_args, args) < 0) {
861 return;
862 }
863 }
864
865 /* 2. dump the contents according to parsed args */
866 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
867
868 free_cmdline_args(args, nb_args);
869 }
870
871 static void do_help_cmd(Monitor *mon, const QDict *qdict)
872 {
873 help_cmd(mon, qdict_get_try_str(qdict, "name"));
874 }
875
876 static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
877 {
878 const char *tp_name = qdict_get_str(qdict, "name");
879 bool new_state = qdict_get_bool(qdict, "option");
880
881 bool found = false;
882 TraceEvent *ev = NULL;
883 while ((ev = trace_event_pattern(tp_name, ev)) != NULL) {
884 found = true;
885 if (!trace_event_get_state_static(ev)) {
886 monitor_printf(mon, "event \"%s\" is not traceable\n", tp_name);
887 } else {
888 trace_event_set_state_dynamic(ev, new_state);
889 }
890 }
891 if (!trace_event_is_pattern(tp_name) && !found) {
892 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
893 }
894 }
895
896 #ifdef CONFIG_TRACE_SIMPLE
897 static void do_trace_file(Monitor *mon, const QDict *qdict)
898 {
899 const char *op = qdict_get_try_str(qdict, "op");
900 const char *arg = qdict_get_try_str(qdict, "arg");
901
902 if (!op) {
903 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
904 } else if (!strcmp(op, "on")) {
905 st_set_trace_file_enabled(true);
906 } else if (!strcmp(op, "off")) {
907 st_set_trace_file_enabled(false);
908 } else if (!strcmp(op, "flush")) {
909 st_flush_trace_buffer();
910 } else if (!strcmp(op, "set")) {
911 if (arg) {
912 st_set_trace_file(arg);
913 }
914 } else {
915 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
916 help_cmd(mon, "trace-file");
917 }
918 }
919 #endif
920
921 static void user_monitor_complete(void *opaque, QObject *ret_data)
922 {
923 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
924
925 if (ret_data) {
926 data->user_print(data->mon, ret_data);
927 }
928 monitor_resume(data->mon);
929 g_free(data);
930 }
931
932 static void qmp_monitor_complete(void *opaque, QObject *ret_data)
933 {
934 monitor_protocol_emitter(opaque, ret_data);
935 }
936
937 static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
938 const QDict *params)
939 {
940 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
941 }
942
943 static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
944 const QDict *params)
945 {
946 int ret;
947
948 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
949 cb_data->mon = mon;
950 cb_data->user_print = cmd->user_print;
951 monitor_suspend(mon);
952 ret = cmd->mhandler.cmd_async(mon, params,
953 user_monitor_complete, cb_data);
954 if (ret < 0) {
955 monitor_resume(mon);
956 g_free(cb_data);
957 }
958 }
959
960 static void do_info_help(Monitor *mon, const QDict *qdict)
961 {
962 help_cmd(mon, "info");
963 }
964
965 CommandInfoList *qmp_query_commands(Error **errp)
966 {
967 CommandInfoList *info, *cmd_list = NULL;
968 const mon_cmd_t *cmd;
969
970 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
971 info = g_malloc0(sizeof(*info));
972 info->value = g_malloc0(sizeof(*info->value));
973 info->value->name = g_strdup(cmd->name);
974
975 info->next = cmd_list;
976 cmd_list = info;
977 }
978
979 return cmd_list;
980 }
981
982 EventInfoList *qmp_query_events(Error **errp)
983 {
984 EventInfoList *info, *ev_list = NULL;
985 QAPIEvent e;
986
987 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
988 const char *event_name = QAPIEvent_lookup[e];
989 assert(event_name != NULL);
990 info = g_malloc0(sizeof(*info));
991 info->value = g_malloc0(sizeof(*info->value));
992 info->value->name = g_strdup(event_name);
993
994 info->next = ev_list;
995 ev_list = info;
996 }
997
998 return ev_list;
999 }
1000
1001 /* set the current CPU defined by the user */
1002 int monitor_set_cpu(int cpu_index)
1003 {
1004 CPUState *cpu;
1005
1006 cpu = qemu_get_cpu(cpu_index);
1007 if (cpu == NULL) {
1008 return -1;
1009 }
1010 cur_mon->mon_cpu = cpu;
1011 return 0;
1012 }
1013
1014 static CPUArchState *mon_get_cpu(void)
1015 {
1016 if (!cur_mon->mon_cpu) {
1017 monitor_set_cpu(0);
1018 }
1019 cpu_synchronize_state(cur_mon->mon_cpu);
1020 return cur_mon->mon_cpu->env_ptr;
1021 }
1022
1023 int monitor_get_cpu_index(void)
1024 {
1025 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
1026 return cpu->cpu_index;
1027 }
1028
1029 static void do_info_registers(Monitor *mon, const QDict *qdict)
1030 {
1031 CPUState *cpu;
1032 CPUArchState *env;
1033 env = mon_get_cpu();
1034 cpu = ENV_GET_CPU(env);
1035 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1036 }
1037
1038 static void do_info_jit(Monitor *mon, const QDict *qdict)
1039 {
1040 dump_exec_info((FILE *)mon, monitor_fprintf);
1041 }
1042
1043 static void do_info_history(Monitor *mon, const QDict *qdict)
1044 {
1045 int i;
1046 const char *str;
1047
1048 if (!mon->rs)
1049 return;
1050 i = 0;
1051 for(;;) {
1052 str = readline_get_history(mon->rs, i);
1053 if (!str)
1054 break;
1055 monitor_printf(mon, "%d: '%s'\n", i, str);
1056 i++;
1057 }
1058 }
1059
1060 static void do_info_cpu_stats(Monitor *mon, const QDict *qdict)
1061 {
1062 CPUState *cpu;
1063 CPUArchState *env;
1064
1065 env = mon_get_cpu();
1066 cpu = ENV_GET_CPU(env);
1067 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
1068 }
1069
1070 static void do_trace_print_events(Monitor *mon, const QDict *qdict)
1071 {
1072 trace_print_events((FILE *)mon, &monitor_fprintf);
1073 }
1074
1075 static int client_migrate_info(Monitor *mon, const QDict *qdict,
1076 MonitorCompletion cb, void *opaque)
1077 {
1078 const char *protocol = qdict_get_str(qdict, "protocol");
1079 const char *hostname = qdict_get_str(qdict, "hostname");
1080 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1081 int port = qdict_get_try_int(qdict, "port", -1);
1082 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1083 int ret;
1084
1085 if (strcmp(protocol, "spice") == 0) {
1086 if (!using_spice) {
1087 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1088 return -1;
1089 }
1090
1091 if (port == -1 && tls_port == -1) {
1092 qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
1093 return -1;
1094 }
1095
1096 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1097 cb, opaque);
1098 if (ret != 0) {
1099 qerror_report(QERR_UNDEFINED_ERROR);
1100 return -1;
1101 }
1102 return 0;
1103 }
1104
1105 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1106 return -1;
1107 }
1108
1109 static void do_logfile(Monitor *mon, const QDict *qdict)
1110 {
1111 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1112 }
1113
1114 static void do_log(Monitor *mon, const QDict *qdict)
1115 {
1116 int mask;
1117 const char *items = qdict_get_str(qdict, "items");
1118
1119 if (!strcmp(items, "none")) {
1120 mask = 0;
1121 } else {
1122 mask = qemu_str_to_log_mask(items);
1123 if (!mask) {
1124 help_cmd(mon, "log");
1125 return;
1126 }
1127 }
1128 qemu_set_log(mask);
1129 }
1130
1131 static void do_singlestep(Monitor *mon, const QDict *qdict)
1132 {
1133 const char *option = qdict_get_try_str(qdict, "option");
1134 if (!option || !strcmp(option, "on")) {
1135 singlestep = 1;
1136 } else if (!strcmp(option, "off")) {
1137 singlestep = 0;
1138 } else {
1139 monitor_printf(mon, "unexpected option %s\n", option);
1140 }
1141 }
1142
1143 static void do_gdbserver(Monitor *mon, const QDict *qdict)
1144 {
1145 const char *device = qdict_get_try_str(qdict, "device");
1146 if (!device)
1147 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1148 if (gdbserver_start(device) < 0) {
1149 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1150 device);
1151 } else if (strcmp(device, "none") == 0) {
1152 monitor_printf(mon, "Disabled gdbserver\n");
1153 } else {
1154 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1155 device);
1156 }
1157 }
1158
1159 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
1160 {
1161 const char *action = qdict_get_str(qdict, "action");
1162 if (select_watchdog_action(action) == -1) {
1163 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1164 }
1165 }
1166
1167 static void monitor_printc(Monitor *mon, int c)
1168 {
1169 monitor_printf(mon, "'");
1170 switch(c) {
1171 case '\'':
1172 monitor_printf(mon, "\\'");
1173 break;
1174 case '\\':
1175 monitor_printf(mon, "\\\\");
1176 break;
1177 case '\n':
1178 monitor_printf(mon, "\\n");
1179 break;
1180 case '\r':
1181 monitor_printf(mon, "\\r");
1182 break;
1183 default:
1184 if (c >= 32 && c <= 126) {
1185 monitor_printf(mon, "%c", c);
1186 } else {
1187 monitor_printf(mon, "\\x%02x", c);
1188 }
1189 break;
1190 }
1191 monitor_printf(mon, "'");
1192 }
1193
1194 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1195 hwaddr addr, int is_physical)
1196 {
1197 CPUArchState *env;
1198 int l, line_size, i, max_digits, len;
1199 uint8_t buf[16];
1200 uint64_t v;
1201
1202 if (format == 'i') {
1203 int flags;
1204 flags = 0;
1205 env = mon_get_cpu();
1206 #ifdef TARGET_I386
1207 if (wsize == 2) {
1208 flags = 1;
1209 } else if (wsize == 4) {
1210 flags = 0;
1211 } else {
1212 /* as default we use the current CS size */
1213 flags = 0;
1214 if (env) {
1215 #ifdef TARGET_X86_64
1216 if ((env->efer & MSR_EFER_LMA) &&
1217 (env->segs[R_CS].flags & DESC_L_MASK))
1218 flags = 2;
1219 else
1220 #endif
1221 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1222 flags = 1;
1223 }
1224 }
1225 #endif
1226 #ifdef TARGET_PPC
1227 flags = msr_le << 16;
1228 flags |= env->bfd_mach;
1229 #endif
1230 monitor_disas(mon, env, addr, count, is_physical, flags);
1231 return;
1232 }
1233
1234 len = wsize * count;
1235 if (wsize == 1)
1236 line_size = 8;
1237 else
1238 line_size = 16;
1239 max_digits = 0;
1240
1241 switch(format) {
1242 case 'o':
1243 max_digits = (wsize * 8 + 2) / 3;
1244 break;
1245 default:
1246 case 'x':
1247 max_digits = (wsize * 8) / 4;
1248 break;
1249 case 'u':
1250 case 'd':
1251 max_digits = (wsize * 8 * 10 + 32) / 33;
1252 break;
1253 case 'c':
1254 wsize = 1;
1255 break;
1256 }
1257
1258 while (len > 0) {
1259 if (is_physical)
1260 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1261 else
1262 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1263 l = len;
1264 if (l > line_size)
1265 l = line_size;
1266 if (is_physical) {
1267 cpu_physical_memory_read(addr, buf, l);
1268 } else {
1269 env = mon_get_cpu();
1270 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
1271 monitor_printf(mon, " Cannot access memory\n");
1272 break;
1273 }
1274 }
1275 i = 0;
1276 while (i < l) {
1277 switch(wsize) {
1278 default:
1279 case 1:
1280 v = ldub_raw(buf + i);
1281 break;
1282 case 2:
1283 v = lduw_raw(buf + i);
1284 break;
1285 case 4:
1286 v = (uint32_t)ldl_raw(buf + i);
1287 break;
1288 case 8:
1289 v = ldq_raw(buf + i);
1290 break;
1291 }
1292 monitor_printf(mon, " ");
1293 switch(format) {
1294 case 'o':
1295 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1296 break;
1297 case 'x':
1298 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1299 break;
1300 case 'u':
1301 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1302 break;
1303 case 'd':
1304 monitor_printf(mon, "%*" PRId64, max_digits, v);
1305 break;
1306 case 'c':
1307 monitor_printc(mon, v);
1308 break;
1309 }
1310 i += wsize;
1311 }
1312 monitor_printf(mon, "\n");
1313 addr += l;
1314 len -= l;
1315 }
1316 }
1317
1318 static void do_memory_dump(Monitor *mon, const QDict *qdict)
1319 {
1320 int count = qdict_get_int(qdict, "count");
1321 int format = qdict_get_int(qdict, "format");
1322 int size = qdict_get_int(qdict, "size");
1323 target_long addr = qdict_get_int(qdict, "addr");
1324
1325 memory_dump(mon, count, format, size, addr, 0);
1326 }
1327
1328 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1329 {
1330 int count = qdict_get_int(qdict, "count");
1331 int format = qdict_get_int(qdict, "format");
1332 int size = qdict_get_int(qdict, "size");
1333 hwaddr addr = qdict_get_int(qdict, "addr");
1334
1335 memory_dump(mon, count, format, size, addr, 1);
1336 }
1337
1338 static void do_print(Monitor *mon, const QDict *qdict)
1339 {
1340 int format = qdict_get_int(qdict, "format");
1341 hwaddr val = qdict_get_int(qdict, "val");
1342
1343 switch(format) {
1344 case 'o':
1345 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1346 break;
1347 case 'x':
1348 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1349 break;
1350 case 'u':
1351 monitor_printf(mon, "%" HWADDR_PRIu, val);
1352 break;
1353 default:
1354 case 'd':
1355 monitor_printf(mon, "%" HWADDR_PRId, val);
1356 break;
1357 case 'c':
1358 monitor_printc(mon, val);
1359 break;
1360 }
1361 monitor_printf(mon, "\n");
1362 }
1363
1364 static void do_sum(Monitor *mon, const QDict *qdict)
1365 {
1366 uint32_t addr;
1367 uint16_t sum;
1368 uint32_t start = qdict_get_int(qdict, "start");
1369 uint32_t size = qdict_get_int(qdict, "size");
1370
1371 sum = 0;
1372 for(addr = start; addr < (start + size); addr++) {
1373 uint8_t val = ldub_phys(&address_space_memory, addr);
1374 /* BSD sum algorithm ('sum' Unix command) */
1375 sum = (sum >> 1) | (sum << 15);
1376 sum += val;
1377 }
1378 monitor_printf(mon, "%05d\n", sum);
1379 }
1380
1381 static int mouse_button_state;
1382
1383 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1384 {
1385 int dx, dy, dz, button;
1386 const char *dx_str = qdict_get_str(qdict, "dx_str");
1387 const char *dy_str = qdict_get_str(qdict, "dy_str");
1388 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1389
1390 dx = strtol(dx_str, NULL, 0);
1391 dy = strtol(dy_str, NULL, 0);
1392 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1393 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1394
1395 if (dz_str) {
1396 dz = strtol(dz_str, NULL, 0);
1397 if (dz != 0) {
1398 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1399 qemu_input_queue_btn(NULL, button, true);
1400 qemu_input_event_sync();
1401 qemu_input_queue_btn(NULL, button, false);
1402 }
1403 }
1404 qemu_input_event_sync();
1405 }
1406
1407 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1408 {
1409 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1410 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1411 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1412 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1413 };
1414 int button_state = qdict_get_int(qdict, "button_state");
1415
1416 if (mouse_button_state == button_state) {
1417 return;
1418 }
1419 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1420 qemu_input_event_sync();
1421 mouse_button_state = button_state;
1422 }
1423
1424 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1425 {
1426 int size = qdict_get_int(qdict, "size");
1427 int addr = qdict_get_int(qdict, "addr");
1428 int has_index = qdict_haskey(qdict, "index");
1429 uint32_t val;
1430 int suffix;
1431
1432 if (has_index) {
1433 int index = qdict_get_int(qdict, "index");
1434 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1435 addr++;
1436 }
1437 addr &= 0xffff;
1438
1439 switch(size) {
1440 default:
1441 case 1:
1442 val = cpu_inb(addr);
1443 suffix = 'b';
1444 break;
1445 case 2:
1446 val = cpu_inw(addr);
1447 suffix = 'w';
1448 break;
1449 case 4:
1450 val = cpu_inl(addr);
1451 suffix = 'l';
1452 break;
1453 }
1454 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1455 suffix, addr, size * 2, val);
1456 }
1457
1458 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1459 {
1460 int size = qdict_get_int(qdict, "size");
1461 int addr = qdict_get_int(qdict, "addr");
1462 int val = qdict_get_int(qdict, "val");
1463
1464 addr &= IOPORTS_MASK;
1465
1466 switch (size) {
1467 default:
1468 case 1:
1469 cpu_outb(addr, val);
1470 break;
1471 case 2:
1472 cpu_outw(addr, val);
1473 break;
1474 case 4:
1475 cpu_outl(addr, val);
1476 break;
1477 }
1478 }
1479
1480 static void do_boot_set(Monitor *mon, const QDict *qdict)
1481 {
1482 int res;
1483 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1484
1485 res = qemu_boot_set(bootdevice);
1486 if (res == 0) {
1487 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1488 } else if (res > 0) {
1489 monitor_printf(mon, "setting boot device list failed\n");
1490 } else {
1491 monitor_printf(mon, "no function defined to set boot device list for "
1492 "this architecture\n");
1493 }
1494 }
1495
1496 #if defined(TARGET_I386)
1497 static void print_pte(Monitor *mon, hwaddr addr,
1498 hwaddr pte,
1499 hwaddr mask)
1500 {
1501 #ifdef TARGET_X86_64
1502 if (addr & (1ULL << 47)) {
1503 addr |= -1LL << 48;
1504 }
1505 #endif
1506 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1507 " %c%c%c%c%c%c%c%c%c\n",
1508 addr,
1509 pte & mask,
1510 pte & PG_NX_MASK ? 'X' : '-',
1511 pte & PG_GLOBAL_MASK ? 'G' : '-',
1512 pte & PG_PSE_MASK ? 'P' : '-',
1513 pte & PG_DIRTY_MASK ? 'D' : '-',
1514 pte & PG_ACCESSED_MASK ? 'A' : '-',
1515 pte & PG_PCD_MASK ? 'C' : '-',
1516 pte & PG_PWT_MASK ? 'T' : '-',
1517 pte & PG_USER_MASK ? 'U' : '-',
1518 pte & PG_RW_MASK ? 'W' : '-');
1519 }
1520
1521 static void tlb_info_32(Monitor *mon, CPUArchState *env)
1522 {
1523 unsigned int l1, l2;
1524 uint32_t pgd, pde, pte;
1525
1526 pgd = env->cr[3] & ~0xfff;
1527 for(l1 = 0; l1 < 1024; l1++) {
1528 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1529 pde = le32_to_cpu(pde);
1530 if (pde & PG_PRESENT_MASK) {
1531 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1532 /* 4M pages */
1533 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1534 } else {
1535 for(l2 = 0; l2 < 1024; l2++) {
1536 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1537 pte = le32_to_cpu(pte);
1538 if (pte & PG_PRESENT_MASK) {
1539 print_pte(mon, (l1 << 22) + (l2 << 12),
1540 pte & ~PG_PSE_MASK,
1541 ~0xfff);
1542 }
1543 }
1544 }
1545 }
1546 }
1547 }
1548
1549 static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
1550 {
1551 unsigned int l1, l2, l3;
1552 uint64_t pdpe, pde, pte;
1553 uint64_t pdp_addr, pd_addr, pt_addr;
1554
1555 pdp_addr = env->cr[3] & ~0x1f;
1556 for (l1 = 0; l1 < 4; l1++) {
1557 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1558 pdpe = le64_to_cpu(pdpe);
1559 if (pdpe & PG_PRESENT_MASK) {
1560 pd_addr = pdpe & 0x3fffffffff000ULL;
1561 for (l2 = 0; l2 < 512; l2++) {
1562 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1563 pde = le64_to_cpu(pde);
1564 if (pde & PG_PRESENT_MASK) {
1565 if (pde & PG_PSE_MASK) {
1566 /* 2M pages with PAE, CR4.PSE is ignored */
1567 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1568 ~((hwaddr)(1 << 20) - 1));
1569 } else {
1570 pt_addr = pde & 0x3fffffffff000ULL;
1571 for (l3 = 0; l3 < 512; l3++) {
1572 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1573 pte = le64_to_cpu(pte);
1574 if (pte & PG_PRESENT_MASK) {
1575 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1576 + (l3 << 12),
1577 pte & ~PG_PSE_MASK,
1578 ~(hwaddr)0xfff);
1579 }
1580 }
1581 }
1582 }
1583 }
1584 }
1585 }
1586 }
1587
1588 #ifdef TARGET_X86_64
1589 static void tlb_info_64(Monitor *mon, CPUArchState *env)
1590 {
1591 uint64_t l1, l2, l3, l4;
1592 uint64_t pml4e, pdpe, pde, pte;
1593 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1594
1595 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1596 for (l1 = 0; l1 < 512; l1++) {
1597 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1598 pml4e = le64_to_cpu(pml4e);
1599 if (pml4e & PG_PRESENT_MASK) {
1600 pdp_addr = pml4e & 0x3fffffffff000ULL;
1601 for (l2 = 0; l2 < 512; l2++) {
1602 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1603 pdpe = le64_to_cpu(pdpe);
1604 if (pdpe & PG_PRESENT_MASK) {
1605 if (pdpe & PG_PSE_MASK) {
1606 /* 1G pages, CR4.PSE is ignored */
1607 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1608 0x3ffffc0000000ULL);
1609 } else {
1610 pd_addr = pdpe & 0x3fffffffff000ULL;
1611 for (l3 = 0; l3 < 512; l3++) {
1612 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1613 pde = le64_to_cpu(pde);
1614 if (pde & PG_PRESENT_MASK) {
1615 if (pde & PG_PSE_MASK) {
1616 /* 2M pages, CR4.PSE is ignored */
1617 print_pte(mon, (l1 << 39) + (l2 << 30) +
1618 (l3 << 21), pde,
1619 0x3ffffffe00000ULL);
1620 } else {
1621 pt_addr = pde & 0x3fffffffff000ULL;
1622 for (l4 = 0; l4 < 512; l4++) {
1623 cpu_physical_memory_read(pt_addr
1624 + l4 * 8,
1625 &pte, 8);
1626 pte = le64_to_cpu(pte);
1627 if (pte & PG_PRESENT_MASK) {
1628 print_pte(mon, (l1 << 39) +
1629 (l2 << 30) +
1630 (l3 << 21) + (l4 << 12),
1631 pte & ~PG_PSE_MASK,
1632 0x3fffffffff000ULL);
1633 }
1634 }
1635 }
1636 }
1637 }
1638 }
1639 }
1640 }
1641 }
1642 }
1643 }
1644 #endif
1645
1646 static void tlb_info(Monitor *mon, const QDict *qdict)
1647 {
1648 CPUArchState *env;
1649
1650 env = mon_get_cpu();
1651
1652 if (!(env->cr[0] & CR0_PG_MASK)) {
1653 monitor_printf(mon, "PG disabled\n");
1654 return;
1655 }
1656 if (env->cr[4] & CR4_PAE_MASK) {
1657 #ifdef TARGET_X86_64
1658 if (env->hflags & HF_LMA_MASK) {
1659 tlb_info_64(mon, env);
1660 } else
1661 #endif
1662 {
1663 tlb_info_pae32(mon, env);
1664 }
1665 } else {
1666 tlb_info_32(mon, env);
1667 }
1668 }
1669
1670 static void mem_print(Monitor *mon, hwaddr *pstart,
1671 int *plast_prot,
1672 hwaddr end, int prot)
1673 {
1674 int prot1;
1675 prot1 = *plast_prot;
1676 if (prot != prot1) {
1677 if (*pstart != -1) {
1678 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1679 TARGET_FMT_plx " %c%c%c\n",
1680 *pstart, end, end - *pstart,
1681 prot1 & PG_USER_MASK ? 'u' : '-',
1682 'r',
1683 prot1 & PG_RW_MASK ? 'w' : '-');
1684 }
1685 if (prot != 0)
1686 *pstart = end;
1687 else
1688 *pstart = -1;
1689 *plast_prot = prot;
1690 }
1691 }
1692
1693 static void mem_info_32(Monitor *mon, CPUArchState *env)
1694 {
1695 unsigned int l1, l2;
1696 int prot, last_prot;
1697 uint32_t pgd, pde, pte;
1698 hwaddr start, end;
1699
1700 pgd = env->cr[3] & ~0xfff;
1701 last_prot = 0;
1702 start = -1;
1703 for(l1 = 0; l1 < 1024; l1++) {
1704 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1705 pde = le32_to_cpu(pde);
1706 end = l1 << 22;
1707 if (pde & PG_PRESENT_MASK) {
1708 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1709 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1710 mem_print(mon, &start, &last_prot, end, prot);
1711 } else {
1712 for(l2 = 0; l2 < 1024; l2++) {
1713 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1714 pte = le32_to_cpu(pte);
1715 end = (l1 << 22) + (l2 << 12);
1716 if (pte & PG_PRESENT_MASK) {
1717 prot = pte & pde &
1718 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1719 } else {
1720 prot = 0;
1721 }
1722 mem_print(mon, &start, &last_prot, end, prot);
1723 }
1724 }
1725 } else {
1726 prot = 0;
1727 mem_print(mon, &start, &last_prot, end, prot);
1728 }
1729 }
1730 /* Flush last range */
1731 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1732 }
1733
1734 static void mem_info_pae32(Monitor *mon, CPUArchState *env)
1735 {
1736 unsigned int l1, l2, l3;
1737 int prot, last_prot;
1738 uint64_t pdpe, pde, pte;
1739 uint64_t pdp_addr, pd_addr, pt_addr;
1740 hwaddr start, end;
1741
1742 pdp_addr = env->cr[3] & ~0x1f;
1743 last_prot = 0;
1744 start = -1;
1745 for (l1 = 0; l1 < 4; l1++) {
1746 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1747 pdpe = le64_to_cpu(pdpe);
1748 end = l1 << 30;
1749 if (pdpe & PG_PRESENT_MASK) {
1750 pd_addr = pdpe & 0x3fffffffff000ULL;
1751 for (l2 = 0; l2 < 512; l2++) {
1752 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1753 pde = le64_to_cpu(pde);
1754 end = (l1 << 30) + (l2 << 21);
1755 if (pde & PG_PRESENT_MASK) {
1756 if (pde & PG_PSE_MASK) {
1757 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1758 PG_PRESENT_MASK);
1759 mem_print(mon, &start, &last_prot, end, prot);
1760 } else {
1761 pt_addr = pde & 0x3fffffffff000ULL;
1762 for (l3 = 0; l3 < 512; l3++) {
1763 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1764 pte = le64_to_cpu(pte);
1765 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1766 if (pte & PG_PRESENT_MASK) {
1767 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1768 PG_PRESENT_MASK);
1769 } else {
1770 prot = 0;
1771 }
1772 mem_print(mon, &start, &last_prot, end, prot);
1773 }
1774 }
1775 } else {
1776 prot = 0;
1777 mem_print(mon, &start, &last_prot, end, prot);
1778 }
1779 }
1780 } else {
1781 prot = 0;
1782 mem_print(mon, &start, &last_prot, end, prot);
1783 }
1784 }
1785 /* Flush last range */
1786 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1787 }
1788
1789
1790 #ifdef TARGET_X86_64
1791 static void mem_info_64(Monitor *mon, CPUArchState *env)
1792 {
1793 int prot, last_prot;
1794 uint64_t l1, l2, l3, l4;
1795 uint64_t pml4e, pdpe, pde, pte;
1796 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1797
1798 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1799 last_prot = 0;
1800 start = -1;
1801 for (l1 = 0; l1 < 512; l1++) {
1802 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1803 pml4e = le64_to_cpu(pml4e);
1804 end = l1 << 39;
1805 if (pml4e & PG_PRESENT_MASK) {
1806 pdp_addr = pml4e & 0x3fffffffff000ULL;
1807 for (l2 = 0; l2 < 512; l2++) {
1808 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1809 pdpe = le64_to_cpu(pdpe);
1810 end = (l1 << 39) + (l2 << 30);
1811 if (pdpe & PG_PRESENT_MASK) {
1812 if (pdpe & PG_PSE_MASK) {
1813 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1814 PG_PRESENT_MASK);
1815 prot &= pml4e;
1816 mem_print(mon, &start, &last_prot, end, prot);
1817 } else {
1818 pd_addr = pdpe & 0x3fffffffff000ULL;
1819 for (l3 = 0; l3 < 512; l3++) {
1820 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1821 pde = le64_to_cpu(pde);
1822 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1823 if (pde & PG_PRESENT_MASK) {
1824 if (pde & PG_PSE_MASK) {
1825 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1826 PG_PRESENT_MASK);
1827 prot &= pml4e & pdpe;
1828 mem_print(mon, &start, &last_prot, end, prot);
1829 } else {
1830 pt_addr = pde & 0x3fffffffff000ULL;
1831 for (l4 = 0; l4 < 512; l4++) {
1832 cpu_physical_memory_read(pt_addr
1833 + l4 * 8,
1834 &pte, 8);
1835 pte = le64_to_cpu(pte);
1836 end = (l1 << 39) + (l2 << 30) +
1837 (l3 << 21) + (l4 << 12);
1838 if (pte & PG_PRESENT_MASK) {
1839 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1840 PG_PRESENT_MASK);
1841 prot &= pml4e & pdpe & pde;
1842 } else {
1843 prot = 0;
1844 }
1845 mem_print(mon, &start, &last_prot, end, prot);
1846 }
1847 }
1848 } else {
1849 prot = 0;
1850 mem_print(mon, &start, &last_prot, end, prot);
1851 }
1852 }
1853 }
1854 } else {
1855 prot = 0;
1856 mem_print(mon, &start, &last_prot, end, prot);
1857 }
1858 }
1859 } else {
1860 prot = 0;
1861 mem_print(mon, &start, &last_prot, end, prot);
1862 }
1863 }
1864 /* Flush last range */
1865 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
1866 }
1867 #endif
1868
1869 static void mem_info(Monitor *mon, const QDict *qdict)
1870 {
1871 CPUArchState *env;
1872
1873 env = mon_get_cpu();
1874
1875 if (!(env->cr[0] & CR0_PG_MASK)) {
1876 monitor_printf(mon, "PG disabled\n");
1877 return;
1878 }
1879 if (env->cr[4] & CR4_PAE_MASK) {
1880 #ifdef TARGET_X86_64
1881 if (env->hflags & HF_LMA_MASK) {
1882 mem_info_64(mon, env);
1883 } else
1884 #endif
1885 {
1886 mem_info_pae32(mon, env);
1887 }
1888 } else {
1889 mem_info_32(mon, env);
1890 }
1891 }
1892 #endif
1893
1894 #if defined(TARGET_SH4)
1895
1896 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1897 {
1898 monitor_printf(mon, " tlb%i:\t"
1899 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1900 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1901 "dirty=%hhu writethrough=%hhu\n",
1902 idx,
1903 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1904 tlb->v, tlb->sh, tlb->c, tlb->pr,
1905 tlb->d, tlb->wt);
1906 }
1907
1908 static void tlb_info(Monitor *mon, const QDict *qdict)
1909 {
1910 CPUArchState *env = mon_get_cpu();
1911 int i;
1912
1913 monitor_printf (mon, "ITLB:\n");
1914 for (i = 0 ; i < ITLB_SIZE ; i++)
1915 print_tlb (mon, i, &env->itlb[i]);
1916 monitor_printf (mon, "UTLB:\n");
1917 for (i = 0 ; i < UTLB_SIZE ; i++)
1918 print_tlb (mon, i, &env->utlb[i]);
1919 }
1920
1921 #endif
1922
1923 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1924 static void tlb_info(Monitor *mon, const QDict *qdict)
1925 {
1926 CPUArchState *env1 = mon_get_cpu();
1927
1928 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1929 }
1930 #endif
1931
1932 static void do_info_mtree(Monitor *mon, const QDict *qdict)
1933 {
1934 mtree_info((fprintf_function)monitor_printf, mon);
1935 }
1936
1937 static void do_info_numa(Monitor *mon, const QDict *qdict)
1938 {
1939 int i;
1940 CPUState *cpu;
1941
1942 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1943 for (i = 0; i < nb_numa_nodes; i++) {
1944 monitor_printf(mon, "node %d cpus:", i);
1945 CPU_FOREACH(cpu) {
1946 if (cpu->numa_node == i) {
1947 monitor_printf(mon, " %d", cpu->cpu_index);
1948 }
1949 }
1950 monitor_printf(mon, "\n");
1951 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1952 numa_info[i].node_mem >> 20);
1953 }
1954 }
1955
1956 #ifdef CONFIG_PROFILER
1957
1958 int64_t qemu_time;
1959 int64_t dev_time;
1960
1961 static void do_info_profile(Monitor *mon, const QDict *qdict)
1962 {
1963 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1964 dev_time, dev_time / (double)get_ticks_per_sec());
1965 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1966 qemu_time, qemu_time / (double)get_ticks_per_sec());
1967 qemu_time = 0;
1968 dev_time = 0;
1969 }
1970 #else
1971 static void do_info_profile(Monitor *mon, const QDict *qdict)
1972 {
1973 monitor_printf(mon, "Internal profiler not compiled\n");
1974 }
1975 #endif
1976
1977 /* Capture support */
1978 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1979
1980 static void do_info_capture(Monitor *mon, const QDict *qdict)
1981 {
1982 int i;
1983 CaptureState *s;
1984
1985 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1986 monitor_printf(mon, "[%d]: ", i);
1987 s->ops.info (s->opaque);
1988 }
1989 }
1990
1991 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1992 {
1993 int i;
1994 int n = qdict_get_int(qdict, "n");
1995 CaptureState *s;
1996
1997 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1998 if (i == n) {
1999 s->ops.destroy (s->opaque);
2000 QLIST_REMOVE (s, entries);
2001 g_free (s);
2002 return;
2003 }
2004 }
2005 }
2006
2007 static void do_wav_capture(Monitor *mon, const QDict *qdict)
2008 {
2009 const char *path = qdict_get_str(qdict, "path");
2010 int has_freq = qdict_haskey(qdict, "freq");
2011 int freq = qdict_get_try_int(qdict, "freq", -1);
2012 int has_bits = qdict_haskey(qdict, "bits");
2013 int bits = qdict_get_try_int(qdict, "bits", -1);
2014 int has_channels = qdict_haskey(qdict, "nchannels");
2015 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2016 CaptureState *s;
2017
2018 s = g_malloc0 (sizeof (*s));
2019
2020 freq = has_freq ? freq : 44100;
2021 bits = has_bits ? bits : 16;
2022 nchannels = has_channels ? nchannels : 2;
2023
2024 if (wav_start_capture (s, path, freq, bits, nchannels)) {
2025 monitor_printf(mon, "Failed to add wave capture\n");
2026 g_free (s);
2027 return;
2028 }
2029 QLIST_INSERT_HEAD (&capture_head, s, entries);
2030 }
2031
2032 static qemu_acl *find_acl(Monitor *mon, const char *name)
2033 {
2034 qemu_acl *acl = qemu_acl_find(name);
2035
2036 if (!acl) {
2037 monitor_printf(mon, "acl: unknown list '%s'\n", name);
2038 }
2039 return acl;
2040 }
2041
2042 static void do_acl_show(Monitor *mon, const QDict *qdict)
2043 {
2044 const char *aclname = qdict_get_str(qdict, "aclname");
2045 qemu_acl *acl = find_acl(mon, aclname);
2046 qemu_acl_entry *entry;
2047 int i = 0;
2048
2049 if (acl) {
2050 monitor_printf(mon, "policy: %s\n",
2051 acl->defaultDeny ? "deny" : "allow");
2052 QTAILQ_FOREACH(entry, &acl->entries, next) {
2053 i++;
2054 monitor_printf(mon, "%d: %s %s\n", i,
2055 entry->deny ? "deny" : "allow", entry->match);
2056 }
2057 }
2058 }
2059
2060 static void do_acl_reset(Monitor *mon, const QDict *qdict)
2061 {
2062 const char *aclname = qdict_get_str(qdict, "aclname");
2063 qemu_acl *acl = find_acl(mon, aclname);
2064
2065 if (acl) {
2066 qemu_acl_reset(acl);
2067 monitor_printf(mon, "acl: removed all rules\n");
2068 }
2069 }
2070
2071 static void do_acl_policy(Monitor *mon, const QDict *qdict)
2072 {
2073 const char *aclname = qdict_get_str(qdict, "aclname");
2074 const char *policy = qdict_get_str(qdict, "policy");
2075 qemu_acl *acl = find_acl(mon, aclname);
2076
2077 if (acl) {
2078 if (strcmp(policy, "allow") == 0) {
2079 acl->defaultDeny = 0;
2080 monitor_printf(mon, "acl: policy set to 'allow'\n");
2081 } else if (strcmp(policy, "deny") == 0) {
2082 acl->defaultDeny = 1;
2083 monitor_printf(mon, "acl: policy set to 'deny'\n");
2084 } else {
2085 monitor_printf(mon, "acl: unknown policy '%s', "
2086 "expected 'deny' or 'allow'\n", policy);
2087 }
2088 }
2089 }
2090
2091 static void do_acl_add(Monitor *mon, const QDict *qdict)
2092 {
2093 const char *aclname = qdict_get_str(qdict, "aclname");
2094 const char *match = qdict_get_str(qdict, "match");
2095 const char *policy = qdict_get_str(qdict, "policy");
2096 int has_index = qdict_haskey(qdict, "index");
2097 int index = qdict_get_try_int(qdict, "index", -1);
2098 qemu_acl *acl = find_acl(mon, aclname);
2099 int deny, ret;
2100
2101 if (acl) {
2102 if (strcmp(policy, "allow") == 0) {
2103 deny = 0;
2104 } else if (strcmp(policy, "deny") == 0) {
2105 deny = 1;
2106 } else {
2107 monitor_printf(mon, "acl: unknown policy '%s', "
2108 "expected 'deny' or 'allow'\n", policy);
2109 return;
2110 }
2111 if (has_index)
2112 ret = qemu_acl_insert(acl, deny, match, index);
2113 else
2114 ret = qemu_acl_append(acl, deny, match);
2115 if (ret < 0)
2116 monitor_printf(mon, "acl: unable to add acl entry\n");
2117 else
2118 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2119 }
2120 }
2121
2122 static void do_acl_remove(Monitor *mon, const QDict *qdict)
2123 {
2124 const char *aclname = qdict_get_str(qdict, "aclname");
2125 const char *match = qdict_get_str(qdict, "match");
2126 qemu_acl *acl = find_acl(mon, aclname);
2127 int ret;
2128
2129 if (acl) {
2130 ret = qemu_acl_remove(acl, match);
2131 if (ret < 0)
2132 monitor_printf(mon, "acl: no matching acl entry\n");
2133 else
2134 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2135 }
2136 }
2137
2138 #if defined(TARGET_I386)
2139 static void do_inject_mce(Monitor *mon, const QDict *qdict)
2140 {
2141 X86CPU *cpu;
2142 CPUState *cs;
2143 int cpu_index = qdict_get_int(qdict, "cpu_index");
2144 int bank = qdict_get_int(qdict, "bank");
2145 uint64_t status = qdict_get_int(qdict, "status");
2146 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2147 uint64_t addr = qdict_get_int(qdict, "addr");
2148 uint64_t misc = qdict_get_int(qdict, "misc");
2149 int flags = MCE_INJECT_UNCOND_AO;
2150
2151 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2152 flags |= MCE_INJECT_BROADCAST;
2153 }
2154 cs = qemu_get_cpu(cpu_index);
2155 if (cs != NULL) {
2156 cpu = X86_CPU(cs);
2157 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2158 flags);
2159 }
2160 }
2161 #endif
2162
2163 void qmp_getfd(const char *fdname, Error **errp)
2164 {
2165 mon_fd_t *monfd;
2166 int fd;
2167
2168 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
2169 if (fd == -1) {
2170 error_set(errp, QERR_FD_NOT_SUPPLIED);
2171 return;
2172 }
2173
2174 if (qemu_isdigit(fdname[0])) {
2175 close(fd);
2176 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2177 "a name not starting with a digit");
2178 return;
2179 }
2180
2181 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2182 if (strcmp(monfd->name, fdname) != 0) {
2183 continue;
2184 }
2185
2186 close(monfd->fd);
2187 monfd->fd = fd;
2188 return;
2189 }
2190
2191 monfd = g_malloc0(sizeof(mon_fd_t));
2192 monfd->name = g_strdup(fdname);
2193 monfd->fd = fd;
2194
2195 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
2196 }
2197
2198 void qmp_closefd(const char *fdname, Error **errp)
2199 {
2200 mon_fd_t *monfd;
2201
2202 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2203 if (strcmp(monfd->name, fdname) != 0) {
2204 continue;
2205 }
2206
2207 QLIST_REMOVE(monfd, next);
2208 close(monfd->fd);
2209 g_free(monfd->name);
2210 g_free(monfd);
2211 return;
2212 }
2213
2214 error_set(errp, QERR_FD_NOT_FOUND, fdname);
2215 }
2216
2217 static void do_loadvm(Monitor *mon, const QDict *qdict)
2218 {
2219 int saved_vm_running = runstate_is_running();
2220 const char *name = qdict_get_str(qdict, "name");
2221
2222 vm_stop(RUN_STATE_RESTORE_VM);
2223
2224 if (load_vmstate(name) == 0 && saved_vm_running) {
2225 vm_start();
2226 }
2227 }
2228
2229 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
2230 {
2231 mon_fd_t *monfd;
2232
2233 QLIST_FOREACH(monfd, &mon->fds, next) {
2234 int fd;
2235
2236 if (strcmp(monfd->name, fdname) != 0) {
2237 continue;
2238 }
2239
2240 fd = monfd->fd;
2241
2242 /* caller takes ownership of fd */
2243 QLIST_REMOVE(monfd, next);
2244 g_free(monfd->name);
2245 g_free(monfd);
2246
2247 return fd;
2248 }
2249
2250 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
2251 return -1;
2252 }
2253
2254 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2255 {
2256 MonFdsetFd *mon_fdset_fd;
2257 MonFdsetFd *mon_fdset_fd_next;
2258
2259 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
2260 if ((mon_fdset_fd->removed ||
2261 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2262 runstate_is_running()) {
2263 close(mon_fdset_fd->fd);
2264 g_free(mon_fdset_fd->opaque);
2265 QLIST_REMOVE(mon_fdset_fd, next);
2266 g_free(mon_fdset_fd);
2267 }
2268 }
2269
2270 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
2271 QLIST_REMOVE(mon_fdset, next);
2272 g_free(mon_fdset);
2273 }
2274 }
2275
2276 static void monitor_fdsets_cleanup(void)
2277 {
2278 MonFdset *mon_fdset;
2279 MonFdset *mon_fdset_next;
2280
2281 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2282 monitor_fdset_cleanup(mon_fdset);
2283 }
2284 }
2285
2286 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2287 const char *opaque, Error **errp)
2288 {
2289 int fd;
2290 Monitor *mon = cur_mon;
2291 AddfdInfo *fdinfo;
2292
2293 fd = qemu_chr_fe_get_msgfd(mon->chr);
2294 if (fd == -1) {
2295 error_set(errp, QERR_FD_NOT_SUPPLIED);
2296 goto error;
2297 }
2298
2299 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2300 has_opaque, opaque, errp);
2301 if (fdinfo) {
2302 return fdinfo;
2303 }
2304
2305 error:
2306 if (fd != -1) {
2307 close(fd);
2308 }
2309 return NULL;
2310 }
2311
2312 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2313 {
2314 MonFdset *mon_fdset;
2315 MonFdsetFd *mon_fdset_fd;
2316 char fd_str[60];
2317
2318 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2319 if (mon_fdset->id != fdset_id) {
2320 continue;
2321 }
2322 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2323 if (has_fd) {
2324 if (mon_fdset_fd->fd != fd) {
2325 continue;
2326 }
2327 mon_fdset_fd->removed = true;
2328 break;
2329 } else {
2330 mon_fdset_fd->removed = true;
2331 }
2332 }
2333 if (has_fd && !mon_fdset_fd) {
2334 goto error;
2335 }
2336 monitor_fdset_cleanup(mon_fdset);
2337 return;
2338 }
2339
2340 error:
2341 if (has_fd) {
2342 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2343 fdset_id, fd);
2344 } else {
2345 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2346 }
2347 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2348 }
2349
2350 FdsetInfoList *qmp_query_fdsets(Error **errp)
2351 {
2352 MonFdset *mon_fdset;
2353 MonFdsetFd *mon_fdset_fd;
2354 FdsetInfoList *fdset_list = NULL;
2355
2356 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2357 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2358 FdsetFdInfoList *fdsetfd_list = NULL;
2359
2360 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2361 fdset_info->value->fdset_id = mon_fdset->id;
2362
2363 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2364 FdsetFdInfoList *fdsetfd_info;
2365
2366 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2367 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2368 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2369 if (mon_fdset_fd->opaque) {
2370 fdsetfd_info->value->has_opaque = true;
2371 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2372 } else {
2373 fdsetfd_info->value->has_opaque = false;
2374 }
2375
2376 fdsetfd_info->next = fdsetfd_list;
2377 fdsetfd_list = fdsetfd_info;
2378 }
2379
2380 fdset_info->value->fds = fdsetfd_list;
2381
2382 fdset_info->next = fdset_list;
2383 fdset_list = fdset_info;
2384 }
2385
2386 return fdset_list;
2387 }
2388
2389 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2390 bool has_opaque, const char *opaque,
2391 Error **errp)
2392 {
2393 MonFdset *mon_fdset = NULL;
2394 MonFdsetFd *mon_fdset_fd;
2395 AddfdInfo *fdinfo;
2396
2397 if (has_fdset_id) {
2398 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2399 /* Break if match found or match impossible due to ordering by ID */
2400 if (fdset_id <= mon_fdset->id) {
2401 if (fdset_id < mon_fdset->id) {
2402 mon_fdset = NULL;
2403 }
2404 break;
2405 }
2406 }
2407 }
2408
2409 if (mon_fdset == NULL) {
2410 int64_t fdset_id_prev = -1;
2411 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2412
2413 if (has_fdset_id) {
2414 if (fdset_id < 0) {
2415 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2416 "a non-negative value");
2417 return NULL;
2418 }
2419 /* Use specified fdset ID */
2420 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2421 mon_fdset_cur = mon_fdset;
2422 if (fdset_id < mon_fdset_cur->id) {
2423 break;
2424 }
2425 }
2426 } else {
2427 /* Use first available fdset ID */
2428 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2429 mon_fdset_cur = mon_fdset;
2430 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2431 fdset_id_prev = mon_fdset_cur->id;
2432 continue;
2433 }
2434 break;
2435 }
2436 }
2437
2438 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2439 if (has_fdset_id) {
2440 mon_fdset->id = fdset_id;
2441 } else {
2442 mon_fdset->id = fdset_id_prev + 1;
2443 }
2444
2445 /* The fdset list is ordered by fdset ID */
2446 if (!mon_fdset_cur) {
2447 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2448 } else if (mon_fdset->id < mon_fdset_cur->id) {
2449 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2450 } else {
2451 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2452 }
2453 }
2454
2455 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2456 mon_fdset_fd->fd = fd;
2457 mon_fdset_fd->removed = false;
2458 if (has_opaque) {
2459 mon_fdset_fd->opaque = g_strdup(opaque);
2460 }
2461 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2462
2463 fdinfo = g_malloc0(sizeof(*fdinfo));
2464 fdinfo->fdset_id = mon_fdset->id;
2465 fdinfo->fd = mon_fdset_fd->fd;
2466
2467 return fdinfo;
2468 }
2469
2470 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2471 {
2472 #ifndef _WIN32
2473 MonFdset *mon_fdset;
2474 MonFdsetFd *mon_fdset_fd;
2475 int mon_fd_flags;
2476
2477 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2478 if (mon_fdset->id != fdset_id) {
2479 continue;
2480 }
2481 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2482 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2483 if (mon_fd_flags == -1) {
2484 return -1;
2485 }
2486
2487 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2488 return mon_fdset_fd->fd;
2489 }
2490 }
2491 errno = EACCES;
2492 return -1;
2493 }
2494 #endif
2495
2496 errno = ENOENT;
2497 return -1;
2498 }
2499
2500 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2501 {
2502 MonFdset *mon_fdset;
2503 MonFdsetFd *mon_fdset_fd_dup;
2504
2505 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2506 if (mon_fdset->id != fdset_id) {
2507 continue;
2508 }
2509 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2510 if (mon_fdset_fd_dup->fd == dup_fd) {
2511 return -1;
2512 }
2513 }
2514 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2515 mon_fdset_fd_dup->fd = dup_fd;
2516 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2517 return 0;
2518 }
2519 return -1;
2520 }
2521
2522 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2523 {
2524 MonFdset *mon_fdset;
2525 MonFdsetFd *mon_fdset_fd_dup;
2526
2527 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2528 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2529 if (mon_fdset_fd_dup->fd == dup_fd) {
2530 if (remove) {
2531 QLIST_REMOVE(mon_fdset_fd_dup, next);
2532 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2533 monitor_fdset_cleanup(mon_fdset);
2534 }
2535 }
2536 return mon_fdset->id;
2537 }
2538 }
2539 }
2540 return -1;
2541 }
2542
2543 int monitor_fdset_dup_fd_find(int dup_fd)
2544 {
2545 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2546 }
2547
2548 int monitor_fdset_dup_fd_remove(int dup_fd)
2549 {
2550 return monitor_fdset_dup_fd_find_remove(dup_fd, true);
2551 }
2552
2553 int monitor_handle_fd_param(Monitor *mon, const char *fdname)
2554 {
2555 int fd;
2556 Error *local_err = NULL;
2557
2558 fd = monitor_handle_fd_param2(mon, fdname, &local_err);
2559 if (local_err) {
2560 qerror_report_err(local_err);
2561 error_free(local_err);
2562 }
2563 return fd;
2564 }
2565
2566 int monitor_handle_fd_param2(Monitor *mon, const char *fdname, Error **errp)
2567 {
2568 int fd;
2569 Error *local_err = NULL;
2570
2571 if (!qemu_isdigit(fdname[0]) && mon) {
2572 fd = monitor_get_fd(mon, fdname, &local_err);
2573 } else {
2574 fd = qemu_parse_fd(fdname);
2575 if (fd == -1) {
2576 error_setg(&local_err, "Invalid file descriptor number '%s'",
2577 fdname);
2578 }
2579 }
2580 if (local_err) {
2581 error_propagate(errp, local_err);
2582 assert(fd == -1);
2583 } else {
2584 assert(fd != -1);
2585 }
2586
2587 return fd;
2588 }
2589
2590 /* Please update hmp-commands.hx when adding or changing commands */
2591 static mon_cmd_t info_cmds[] = {
2592 {
2593 .name = "version",
2594 .args_type = "",
2595 .params = "",
2596 .help = "show the version of QEMU",
2597 .mhandler.cmd = hmp_info_version,
2598 },
2599 {
2600 .name = "network",
2601 .args_type = "",
2602 .params = "",
2603 .help = "show the network state",
2604 .mhandler.cmd = do_info_network,
2605 },
2606 {
2607 .name = "chardev",
2608 .args_type = "",
2609 .params = "",
2610 .help = "show the character devices",
2611 .mhandler.cmd = hmp_info_chardev,
2612 },
2613 {
2614 .name = "block",
2615 .args_type = "verbose:-v,device:B?",
2616 .params = "[-v] [device]",
2617 .help = "show info of one block device or all block devices "
2618 "(and details of images with -v option)",
2619 .mhandler.cmd = hmp_info_block,
2620 },
2621 {
2622 .name = "blockstats",
2623 .args_type = "",
2624 .params = "",
2625 .help = "show block device statistics",
2626 .mhandler.cmd = hmp_info_blockstats,
2627 },
2628 {
2629 .name = "block-jobs",
2630 .args_type = "",
2631 .params = "",
2632 .help = "show progress of ongoing block device operations",
2633 .mhandler.cmd = hmp_info_block_jobs,
2634 },
2635 {
2636 .name = "registers",
2637 .args_type = "",
2638 .params = "",
2639 .help = "show the cpu registers",
2640 .mhandler.cmd = do_info_registers,
2641 },
2642 {
2643 .name = "cpus",
2644 .args_type = "",
2645 .params = "",
2646 .help = "show infos for each CPU",
2647 .mhandler.cmd = hmp_info_cpus,
2648 },
2649 {
2650 .name = "history",
2651 .args_type = "",
2652 .params = "",
2653 .help = "show the command line history",
2654 .mhandler.cmd = do_info_history,
2655 },
2656 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2657 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2658 {
2659 .name = "irq",
2660 .args_type = "",
2661 .params = "",
2662 .help = "show the interrupts statistics (if available)",
2663 #ifdef TARGET_SPARC
2664 .mhandler.cmd = sun4m_irq_info,
2665 #elif defined(TARGET_LM32)
2666 .mhandler.cmd = lm32_irq_info,
2667 #else
2668 .mhandler.cmd = irq_info,
2669 #endif
2670 },
2671 {
2672 .name = "pic",
2673 .args_type = "",
2674 .params = "",
2675 .help = "show i8259 (PIC) state",
2676 #ifdef TARGET_SPARC
2677 .mhandler.cmd = sun4m_pic_info,
2678 #elif defined(TARGET_LM32)
2679 .mhandler.cmd = lm32_do_pic_info,
2680 #else
2681 .mhandler.cmd = pic_info,
2682 #endif
2683 },
2684 #endif
2685 {
2686 .name = "pci",
2687 .args_type = "",
2688 .params = "",
2689 .help = "show PCI info",
2690 .mhandler.cmd = hmp_info_pci,
2691 },
2692 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2693 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2694 {
2695 .name = "tlb",
2696 .args_type = "",
2697 .params = "",
2698 .help = "show virtual to physical memory mappings",
2699 .mhandler.cmd = tlb_info,
2700 },
2701 #endif
2702 #if defined(TARGET_I386)
2703 {
2704 .name = "mem",
2705 .args_type = "",
2706 .params = "",
2707 .help = "show the active virtual memory mappings",
2708 .mhandler.cmd = mem_info,
2709 },
2710 #endif
2711 {
2712 .name = "mtree",
2713 .args_type = "",
2714 .params = "",
2715 .help = "show memory tree",
2716 .mhandler.cmd = do_info_mtree,
2717 },
2718 {
2719 .name = "jit",
2720 .args_type = "",
2721 .params = "",
2722 .help = "show dynamic compiler info",
2723 .mhandler.cmd = do_info_jit,
2724 },
2725 {
2726 .name = "kvm",
2727 .args_type = "",
2728 .params = "",
2729 .help = "show KVM information",
2730 .mhandler.cmd = hmp_info_kvm,
2731 },
2732 {
2733 .name = "numa",
2734 .args_type = "",
2735 .params = "",
2736 .help = "show NUMA information",
2737 .mhandler.cmd = do_info_numa,
2738 },
2739 {
2740 .name = "usb",
2741 .args_type = "",
2742 .params = "",
2743 .help = "show guest USB devices",
2744 .mhandler.cmd = usb_info,
2745 },
2746 {
2747 .name = "usbhost",
2748 .args_type = "",
2749 .params = "",
2750 .help = "show host USB devices",
2751 .mhandler.cmd = usb_host_info,
2752 },
2753 {
2754 .name = "profile",
2755 .args_type = "",
2756 .params = "",
2757 .help = "show profiling information",
2758 .mhandler.cmd = do_info_profile,
2759 },
2760 {
2761 .name = "capture",
2762 .args_type = "",
2763 .params = "",
2764 .help = "show capture information",
2765 .mhandler.cmd = do_info_capture,
2766 },
2767 {
2768 .name = "snapshots",
2769 .args_type = "",
2770 .params = "",
2771 .help = "show the currently saved VM snapshots",
2772 .mhandler.cmd = do_info_snapshots,
2773 },
2774 {
2775 .name = "status",
2776 .args_type = "",
2777 .params = "",
2778 .help = "show the current VM status (running|paused)",
2779 .mhandler.cmd = hmp_info_status,
2780 },
2781 {
2782 .name = "pcmcia",
2783 .args_type = "",
2784 .params = "",
2785 .help = "show guest PCMCIA status",
2786 .mhandler.cmd = pcmcia_info,
2787 },
2788 {
2789 .name = "mice",
2790 .args_type = "",
2791 .params = "",
2792 .help = "show which guest mouse is receiving events",
2793 .mhandler.cmd = hmp_info_mice,
2794 },
2795 {
2796 .name = "vnc",
2797 .args_type = "",
2798 .params = "",
2799 .help = "show the vnc server status",
2800 .mhandler.cmd = hmp_info_vnc,
2801 },
2802 #if defined(CONFIG_SPICE)
2803 {
2804 .name = "spice",
2805 .args_type = "",
2806 .params = "",
2807 .help = "show the spice server status",
2808 .mhandler.cmd = hmp_info_spice,
2809 },
2810 #endif
2811 {
2812 .name = "name",
2813 .args_type = "",
2814 .params = "",
2815 .help = "show the current VM name",
2816 .mhandler.cmd = hmp_info_name,
2817 },
2818 {
2819 .name = "uuid",
2820 .args_type = "",
2821 .params = "",
2822 .help = "show the current VM UUID",
2823 .mhandler.cmd = hmp_info_uuid,
2824 },
2825 {
2826 .name = "cpustats",
2827 .args_type = "",
2828 .params = "",
2829 .help = "show CPU statistics",
2830 .mhandler.cmd = do_info_cpu_stats,
2831 },
2832 #if defined(CONFIG_SLIRP)
2833 {
2834 .name = "usernet",
2835 .args_type = "",
2836 .params = "",
2837 .help = "show user network stack connection states",
2838 .mhandler.cmd = do_info_usernet,
2839 },
2840 #endif
2841 {
2842 .name = "migrate",
2843 .args_type = "",
2844 .params = "",
2845 .help = "show migration status",
2846 .mhandler.cmd = hmp_info_migrate,
2847 },
2848 {
2849 .name = "migrate_capabilities",
2850 .args_type = "",
2851 .params = "",
2852 .help = "show current migration capabilities",
2853 .mhandler.cmd = hmp_info_migrate_capabilities,
2854 },
2855 {
2856 .name = "migrate_cache_size",
2857 .args_type = "",
2858 .params = "",
2859 .help = "show current migration xbzrle cache size",
2860 .mhandler.cmd = hmp_info_migrate_cache_size,
2861 },
2862 {
2863 .name = "balloon",
2864 .args_type = "",
2865 .params = "",
2866 .help = "show balloon information",
2867 .mhandler.cmd = hmp_info_balloon,
2868 },
2869 {
2870 .name = "qtree",
2871 .args_type = "",
2872 .params = "",
2873 .help = "show device tree",
2874 .mhandler.cmd = do_info_qtree,
2875 },
2876 {
2877 .name = "qdm",
2878 .args_type = "",
2879 .params = "",
2880 .help = "show qdev device model list",
2881 .mhandler.cmd = do_info_qdm,
2882 },
2883 {
2884 .name = "roms",
2885 .args_type = "",
2886 .params = "",
2887 .help = "show roms",
2888 .mhandler.cmd = do_info_roms,
2889 },
2890 {
2891 .name = "trace-events",
2892 .args_type = "",
2893 .params = "",
2894 .help = "show available trace-events & their state",
2895 .mhandler.cmd = do_trace_print_events,
2896 },
2897 {
2898 .name = "tpm",
2899 .args_type = "",
2900 .params = "",
2901 .help = "show the TPM device",
2902 .mhandler.cmd = hmp_info_tpm,
2903 },
2904 {
2905 .name = "memdev",
2906 .args_type = "",
2907 .params = "",
2908 .help = "show the memory device",
2909 .mhandler.cmd = hmp_info_memdev,
2910 },
2911 {
2912 .name = NULL,
2913 },
2914 };
2915
2916 /* mon_cmds and info_cmds would be sorted at runtime */
2917 static mon_cmd_t mon_cmds[] = {
2918 #include "hmp-commands.h"
2919 { NULL, NULL, },
2920 };
2921
2922 static const mon_cmd_t qmp_cmds[] = {
2923 #include "qmp-commands-old.h"
2924 { /* NULL */ },
2925 };
2926
2927 /*******************************************************************/
2928
2929 static const char *pch;
2930 static sigjmp_buf expr_env;
2931
2932 #define MD_TLONG 0
2933 #define MD_I32 1
2934
2935 typedef struct MonitorDef {
2936 const char *name;
2937 int offset;
2938 target_long (*get_value)(const struct MonitorDef *md, int val);
2939 int type;
2940 } MonitorDef;
2941
2942 #if defined(TARGET_I386)
2943 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2944 {
2945 CPUArchState *env = mon_get_cpu();
2946 return env->eip + env->segs[R_CS].base;
2947 }
2948 #endif
2949
2950 #if defined(TARGET_PPC)
2951 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2952 {
2953 CPUArchState *env = mon_get_cpu();
2954 unsigned int u;
2955 int i;
2956
2957 u = 0;
2958 for (i = 0; i < 8; i++)
2959 u |= env->crf[i] << (32 - (4 * i));
2960
2961 return u;
2962 }
2963
2964 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2965 {
2966 CPUArchState *env = mon_get_cpu();
2967 return env->msr;
2968 }
2969
2970 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2971 {
2972 CPUArchState *env = mon_get_cpu();
2973 return env->xer;
2974 }
2975
2976 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2977 {
2978 CPUArchState *env = mon_get_cpu();
2979 return cpu_ppc_load_decr(env);
2980 }
2981
2982 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2983 {
2984 CPUArchState *env = mon_get_cpu();
2985 return cpu_ppc_load_tbu(env);
2986 }
2987
2988 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2989 {
2990 CPUArchState *env = mon_get_cpu();
2991 return cpu_ppc_load_tbl(env);
2992 }
2993 #endif
2994
2995 #if defined(TARGET_SPARC)
2996 #ifndef TARGET_SPARC64
2997 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2998 {
2999 CPUArchState *env = mon_get_cpu();
3000
3001 return cpu_get_psr(env);
3002 }
3003 #endif
3004
3005 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
3006 {
3007 CPUArchState *env = mon_get_cpu();
3008 return env->regwptr[val];
3009 }
3010 #endif
3011
3012 static const MonitorDef monitor_defs[] = {
3013 #ifdef TARGET_I386
3014
3015 #define SEG(name, seg) \
3016 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3017 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3018 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
3019
3020 { "eax", offsetof(CPUX86State, regs[0]) },
3021 { "ecx", offsetof(CPUX86State, regs[1]) },
3022 { "edx", offsetof(CPUX86State, regs[2]) },
3023 { "ebx", offsetof(CPUX86State, regs[3]) },
3024 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3025 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3026 { "esi", offsetof(CPUX86State, regs[6]) },
3027 { "edi", offsetof(CPUX86State, regs[7]) },
3028 #ifdef TARGET_X86_64
3029 { "r8", offsetof(CPUX86State, regs[8]) },
3030 { "r9", offsetof(CPUX86State, regs[9]) },
3031 { "r10", offsetof(CPUX86State, regs[10]) },
3032 { "r11", offsetof(CPUX86State, regs[11]) },
3033 { "r12", offsetof(CPUX86State, regs[12]) },
3034 { "r13", offsetof(CPUX86State, regs[13]) },
3035 { "r14", offsetof(CPUX86State, regs[14]) },
3036 { "r15", offsetof(CPUX86State, regs[15]) },
3037 #endif
3038 { "eflags", offsetof(CPUX86State, eflags) },
3039 { "eip", offsetof(CPUX86State, eip) },
3040 SEG("cs", R_CS)
3041 SEG("ds", R_DS)
3042 SEG("es", R_ES)
3043 SEG("ss", R_SS)
3044 SEG("fs", R_FS)
3045 SEG("gs", R_GS)
3046 { "pc", 0, monitor_get_pc, },
3047 #elif defined(TARGET_PPC)
3048 /* General purpose registers */
3049 { "r0", offsetof(CPUPPCState, gpr[0]) },
3050 { "r1", offsetof(CPUPPCState, gpr[1]) },
3051 { "r2", offsetof(CPUPPCState, gpr[2]) },
3052 { "r3", offsetof(CPUPPCState, gpr[3]) },
3053 { "r4", offsetof(CPUPPCState, gpr[4]) },
3054 { "r5", offsetof(CPUPPCState, gpr[5]) },
3055 { "r6", offsetof(CPUPPCState, gpr[6]) },
3056 { "r7", offsetof(CPUPPCState, gpr[7]) },
3057 { "r8", offsetof(CPUPPCState, gpr[8]) },
3058 { "r9", offsetof(CPUPPCState, gpr[9]) },
3059 { "r10", offsetof(CPUPPCState, gpr[10]) },
3060 { "r11", offsetof(CPUPPCState, gpr[11]) },
3061 { "r12", offsetof(CPUPPCState, gpr[12]) },
3062 { "r13", offsetof(CPUPPCState, gpr[13]) },
3063 { "r14", offsetof(CPUPPCState, gpr[14]) },
3064 { "r15", offsetof(CPUPPCState, gpr[15]) },
3065 { "r16", offsetof(CPUPPCState, gpr[16]) },
3066 { "r17", offsetof(CPUPPCState, gpr[17]) },
3067 { "r18", offsetof(CPUPPCState, gpr[18]) },
3068 { "r19", offsetof(CPUPPCState, gpr[19]) },
3069 { "r20", offsetof(CPUPPCState, gpr[20]) },
3070 { "r21", offsetof(CPUPPCState, gpr[21]) },
3071 { "r22", offsetof(CPUPPCState, gpr[22]) },
3072 { "r23", offsetof(CPUPPCState, gpr[23]) },
3073 { "r24", offsetof(CPUPPCState, gpr[24]) },
3074 { "r25", offsetof(CPUPPCState, gpr[25]) },
3075 { "r26", offsetof(CPUPPCState, gpr[26]) },
3076 { "r27", offsetof(CPUPPCState, gpr[27]) },
3077 { "r28", offsetof(CPUPPCState, gpr[28]) },
3078 { "r29", offsetof(CPUPPCState, gpr[29]) },
3079 { "r30", offsetof(CPUPPCState, gpr[30]) },
3080 { "r31", offsetof(CPUPPCState, gpr[31]) },
3081 /* Floating point registers */
3082 { "f0", offsetof(CPUPPCState, fpr[0]) },
3083 { "f1", offsetof(CPUPPCState, fpr[1]) },
3084 { "f2", offsetof(CPUPPCState, fpr[2]) },
3085 { "f3", offsetof(CPUPPCState, fpr[3]) },
3086 { "f4", offsetof(CPUPPCState, fpr[4]) },
3087 { "f5", offsetof(CPUPPCState, fpr[5]) },
3088 { "f6", offsetof(CPUPPCState, fpr[6]) },
3089 { "f7", offsetof(CPUPPCState, fpr[7]) },
3090 { "f8", offsetof(CPUPPCState, fpr[8]) },
3091 { "f9", offsetof(CPUPPCState, fpr[9]) },
3092 { "f10", offsetof(CPUPPCState, fpr[10]) },
3093 { "f11", offsetof(CPUPPCState, fpr[11]) },
3094 { "f12", offsetof(CPUPPCState, fpr[12]) },
3095 { "f13", offsetof(CPUPPCState, fpr[13]) },
3096 { "f14", offsetof(CPUPPCState, fpr[14]) },
3097 { "f15", offsetof(CPUPPCState, fpr[15]) },
3098 { "f16", offsetof(CPUPPCState, fpr[16]) },
3099 { "f17", offsetof(CPUPPCState, fpr[17]) },
3100 { "f18", offsetof(CPUPPCState, fpr[18]) },
3101 { "f19", offsetof(CPUPPCState, fpr[19]) },
3102 { "f20", offsetof(CPUPPCState, fpr[20]) },
3103 { "f21", offsetof(CPUPPCState, fpr[21]) },
3104 { "f22", offsetof(CPUPPCState, fpr[22]) },
3105 { "f23", offsetof(CPUPPCState, fpr[23]) },
3106 { "f24", offsetof(CPUPPCState, fpr[24]) },
3107 { "f25", offsetof(CPUPPCState, fpr[25]) },
3108 { "f26", offsetof(CPUPPCState, fpr[26]) },
3109 { "f27", offsetof(CPUPPCState, fpr[27]) },
3110 { "f28", offsetof(CPUPPCState, fpr[28]) },
3111 { "f29", offsetof(CPUPPCState, fpr[29]) },
3112 { "f30", offsetof(CPUPPCState, fpr[30]) },
3113 { "f31", offsetof(CPUPPCState, fpr[31]) },
3114 { "fpscr", offsetof(CPUPPCState, fpscr) },
3115 /* Next instruction pointer */
3116 { "nip|pc", offsetof(CPUPPCState, nip) },
3117 { "lr", offsetof(CPUPPCState, lr) },
3118 { "ctr", offsetof(CPUPPCState, ctr) },
3119 { "decr", 0, &monitor_get_decr, },
3120 { "ccr", 0, &monitor_get_ccr, },
3121 /* Machine state register */
3122 { "msr", 0, &monitor_get_msr, },
3123 { "xer", 0, &monitor_get_xer, },
3124 { "tbu", 0, &monitor_get_tbu, },
3125 { "tbl", 0, &monitor_get_tbl, },
3126 /* Segment registers */
3127 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3128 { "sr0", offsetof(CPUPPCState, sr[0]) },
3129 { "sr1", offsetof(CPUPPCState, sr[1]) },
3130 { "sr2", offsetof(CPUPPCState, sr[2]) },
3131 { "sr3", offsetof(CPUPPCState, sr[3]) },
3132 { "sr4", offsetof(CPUPPCState, sr[4]) },
3133 { "sr5", offsetof(CPUPPCState, sr[5]) },
3134 { "sr6", offsetof(CPUPPCState, sr[6]) },
3135 { "sr7", offsetof(CPUPPCState, sr[7]) },
3136 { "sr8", offsetof(CPUPPCState, sr[8]) },
3137 { "sr9", offsetof(CPUPPCState, sr[9]) },
3138 { "sr10", offsetof(CPUPPCState, sr[10]) },
3139 { "sr11", offsetof(CPUPPCState, sr[11]) },
3140 { "sr12", offsetof(CPUPPCState, sr[12]) },
3141 { "sr13", offsetof(CPUPPCState, sr[13]) },
3142 { "sr14", offsetof(CPUPPCState, sr[14]) },
3143 { "sr15", offsetof(CPUPPCState, sr[15]) },
3144 /* Too lazy to put BATs... */
3145 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
3146
3147 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3148 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3149 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3150 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3151 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
3152 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3153 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3154 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3155 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3156 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3157 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3158 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3159 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3160 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3161 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3162 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3163 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3164 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3165 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3166 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3167 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3168 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3169 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3170 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3171 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3172 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3173 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3174 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3175 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3176 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3177 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3178 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3179 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3180 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3181 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3182 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3183 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3184 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3185 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3186 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3187 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3188 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3189 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3190 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3191 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3192 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3193 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3194 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3195 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3196 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3197 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3198 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3199 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3200 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3201 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3202 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3203 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3204 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3205 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3206 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3207 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3208 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3209 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3210 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3211 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3212 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3213 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3214 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3215 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
3216
3217 #elif defined(TARGET_SPARC)
3218 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3219 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3220 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3221 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3222 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3223 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3224 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3225 { "g7", offsetof(CPUSPARCState, gregs[7]) },
3226 { "o0", 0, monitor_get_reg },
3227 { "o1", 1, monitor_get_reg },
3228 { "o2", 2, monitor_get_reg },
3229 { "o3", 3, monitor_get_reg },
3230 { "o4", 4, monitor_get_reg },
3231 { "o5", 5, monitor_get_reg },
3232 { "o6", 6, monitor_get_reg },
3233 { "o7", 7, monitor_get_reg },
3234 { "l0", 8, monitor_get_reg },
3235 { "l1", 9, monitor_get_reg },
3236 { "l2", 10, monitor_get_reg },
3237 { "l3", 11, monitor_get_reg },
3238 { "l4", 12, monitor_get_reg },
3239 { "l5", 13, monitor_get_reg },
3240 { "l6", 14, monitor_get_reg },
3241 { "l7", 15, monitor_get_reg },
3242 { "i0", 16, monitor_get_reg },
3243 { "i1", 17, monitor_get_reg },
3244 { "i2", 18, monitor_get_reg },
3245 { "i3", 19, monitor_get_reg },
3246 { "i4", 20, monitor_get_reg },
3247 { "i5", 21, monitor_get_reg },
3248 { "i6", 22, monitor_get_reg },
3249 { "i7", 23, monitor_get_reg },
3250 { "pc", offsetof(CPUSPARCState, pc) },
3251 { "npc", offsetof(CPUSPARCState, npc) },