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