xref: /openbmc/qemu/monitor/monitor-internal.h (revision 68a5b02a)
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 
25 #ifndef MONITOR_INTERNAL_H
26 #define MONITOR_INTERNAL_H
27 
28 #include "chardev/char-fe.h"
29 #include "monitor/monitor.h"
30 #include "qapi/qapi-types-control.h"
31 #include "qapi/qmp/dispatch.h"
32 #include "qapi/qmp/json-parser.h"
33 #include "qemu/readline.h"
34 #include "sysemu/iothread.h"
35 
36 /*
37  * Supported types:
38  *
39  * 'F'          filename
40  * 'B'          block device name
41  * 's'          string (accept optional quote)
42  * 'S'          it just appends the rest of the string (accept optional quote)
43  * 'O'          option string of the form NAME=VALUE,...
44  *              parsed according to QemuOptsList given by its name
45  *              Example: 'device:O' uses qemu_device_opts.
46  *              Restriction: only lists with empty desc are supported
47  *              TODO lift the restriction
48  * 'i'          32 bit integer
49  * 'l'          target long (32 or 64 bit)
50  * 'M'          Non-negative target long (32 or 64 bit), in user mode the
51  *              value is multiplied by 2^20 (think Mebibyte)
52  * 'o'          octets (aka bytes)
53  *              user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
54  *              K, k suffix, which multiplies the value by 2^60 for suffixes E
55  *              and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
56  *              2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
57  * 'T'          double
58  *              user mode accepts an optional ms, us, ns suffix,
59  *              which divides the value by 1e3, 1e6, 1e9, respectively
60  * '/'          optional gdb-like print format (like "/10x")
61  *
62  * '?'          optional type (for all types, except '/')
63  * '.'          other form of optional type (for 'i' and 'l')
64  * 'b'          boolean
65  *              user mode accepts "on" or "off"
66  * '-'          optional parameter (eg. '-f')
67  *
68  */
69 
70 typedef struct HMPCommand {
71     const char *name;
72     const char *args_type;
73     const char *params;
74     const char *help;
75     const char *flags; /* p=preconfig */
76     void (*cmd)(Monitor *mon, const QDict *qdict);
77     bool coroutine;
78     /*
79      * @sub_table is a list of 2nd level of commands. If it does not exist,
80      * cmd should be used. If it exists, sub_table[?].cmd should be
81      * used, and cmd of 1st level plays the role of help function.
82      */
83     struct HMPCommand *sub_table;
84     void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
85 } HMPCommand;
86 
87 struct Monitor {
88     CharBackend chr;
89     int reset_seen;
90     int suspend_cnt;            /* Needs to be accessed atomically */
91     bool is_qmp;
92     bool skip_flush;
93     bool use_io_thread;
94 
95     char *mon_cpu_path;
96     QTAILQ_ENTRY(Monitor) entry;
97 
98     /*
99      * The per-monitor lock. We can't access guest memory when holding
100      * the lock.
101      */
102     QemuMutex mon_lock;
103 
104     /*
105      * Members that are protected by the per-monitor lock
106      */
107     QLIST_HEAD(, mon_fd_t) fds;
108     GString *outbuf;
109     guint out_watch;
110     /* Read under either BQL or mon_lock, written with BQL+mon_lock.  */
111     int mux_out;
112 };
113 
114 struct MonitorHMP {
115     Monitor common;
116     bool use_readline;
117     /*
118      * State used only in the thread "owning" the monitor.
119      * If @use_io_thread, this is @mon_iothread. (This does not actually happen
120      * in the current state of the code.)
121      * Else, it's the main thread.
122      * These members can be safely accessed without locks.
123      */
124     ReadLineState *rs;
125 };
126 
127 typedef struct {
128     Monitor common;
129     JSONMessageParser parser;
130     bool pretty;
131     /*
132      * When a client connects, we're in capabilities negotiation mode.
133      * @commands is &qmp_cap_negotiation_commands then.  When command
134      * qmp_capabilities succeeds, we go into command mode, and
135      * @command becomes &qmp_commands.
136      */
137     const QmpCommandList *commands;
138     bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
139     bool capab[QMP_CAPABILITY__MAX];         /* offered and accepted */
140     /*
141      * Protects qmp request/response queue.
142      * Take monitor_lock first when you need both.
143      */
144     QemuMutex qmp_queue_lock;
145     /* Input queue that holds all the parsed QMP requests */
146     GQueue *qmp_requests;
147 } MonitorQMP;
148 
149 /**
150  * Is @mon a QMP monitor?
151  */
152 static inline bool monitor_is_qmp(const Monitor *mon)
153 {
154     return mon->is_qmp;
155 }
156 
157 typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList;
158 extern IOThread *mon_iothread;
159 extern Coroutine *qmp_dispatcher_co;
160 extern bool qmp_dispatcher_co_shutdown;
161 extern bool qmp_dispatcher_co_busy;
162 extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
163 extern QemuMutex monitor_lock;
164 extern MonitorList mon_list;
165 extern int mon_refcount;
166 
167 extern HMPCommand hmp_cmds[];
168 
169 int monitor_puts(Monitor *mon, const char *str);
170 void monitor_data_init(Monitor *mon, bool is_qmp, bool skip_flush,
171                        bool use_io_thread);
172 void monitor_data_destroy(Monitor *mon);
173 int monitor_can_read(void *opaque);
174 void monitor_list_append(Monitor *mon);
175 void monitor_fdsets_cleanup(void);
176 
177 void qmp_send_response(MonitorQMP *mon, const QDict *rsp);
178 void monitor_data_destroy_qmp(MonitorQMP *mon);
179 void coroutine_fn monitor_qmp_dispatcher_co(void *data);
180 
181 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name);
182 void help_cmd(Monitor *mon, const char *name);
183 void handle_hmp_command(MonitorHMP *mon, const char *cmdline);
184 int hmp_compare_cmd(const char *name, const char *list);
185 
186 void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
187                                  Error **errp);
188 
189 #endif
190