xref: /openbmc/qemu/monitor/monitor-internal.h (revision 5bce308a)
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-misc.h"
31 #include "qapi/qmp/dispatch.h"
32 #include "qapi/qmp/json-parser.h"
33 #include "qemu/readline.h"
34 
35 /*
36  * Supported types:
37  *
38  * 'F'          filename
39  * 'B'          block device name
40  * 's'          string (accept optional quote)
41  * 'S'          it just appends the rest of the string (accept optional quote)
42  * 'O'          option string of the form NAME=VALUE,...
43  *              parsed according to QemuOptsList given by its name
44  *              Example: 'device:O' uses qemu_device_opts.
45  *              Restriction: only lists with empty desc are supported
46  *              TODO lift the restriction
47  * 'i'          32 bit integer
48  * 'l'          target long (32 or 64 bit)
49  * 'M'          Non-negative target long (32 or 64 bit), in user mode the
50  *              value is multiplied by 2^20 (think Mebibyte)
51  * 'o'          octets (aka bytes)
52  *              user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
53  *              K, k suffix, which multiplies the value by 2^60 for suffixes E
54  *              and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
55  *              2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
56  * 'T'          double
57  *              user mode accepts an optional ms, us, ns suffix,
58  *              which divides the value by 1e3, 1e6, 1e9, respectively
59  * '/'          optional gdb-like print format (like "/10x")
60  *
61  * '?'          optional type (for all types, except '/')
62  * '.'          other form of optional type (for 'i' and 'l')
63  * 'b'          boolean
64  *              user mode accepts "on" or "off"
65  * '-'          optional parameter (eg. '-f')
66  *
67  */
68 
69 typedef struct HMPCommand {
70     const char *name;
71     const char *args_type;
72     const char *params;
73     const char *help;
74     const char *flags; /* p=preconfig */
75     void (*cmd)(Monitor *mon, const QDict *qdict);
76     /*
77      * @sub_table is a list of 2nd level of commands. If it does not exist,
78      * cmd should be used. If it exists, sub_table[?].cmd should be
79      * used, and cmd of 1st level plays the role of help function.
80      */
81     struct HMPCommand *sub_table;
82     void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
83 } HMPCommand;
84 
85 struct Monitor {
86     CharBackend chr;
87     int reset_seen;
88     int flags;
89     int suspend_cnt;            /* Needs to be accessed atomically */
90     bool skip_flush;
91     bool use_io_thread;
92 
93     gchar *mon_cpu_path;
94     QTAILQ_ENTRY(Monitor) entry;
95 
96     /*
97      * The per-monitor lock. We can't access guest memory when holding
98      * the lock.
99      */
100     QemuMutex mon_lock;
101 
102     /*
103      * Members that are protected by the per-monitor lock
104      */
105     QLIST_HEAD(, mon_fd_t) fds;
106     QString *outbuf;
107     guint out_watch;
108     /* Read under either BQL or mon_lock, written with BQL+mon_lock.  */
109     int mux_out;
110 };
111 
112 struct MonitorHMP {
113     Monitor common;
114     /*
115      * State used only in the thread "owning" the monitor.
116      * If @use_io_thread, this is @mon_iothread. (This does not actually happen
117      * in the current state of the code.)
118      * Else, it's the main thread.
119      * These members can be safely accessed without locks.
120      */
121     ReadLineState *rs;
122 };
123 
124 typedef struct {
125     Monitor common;
126     JSONMessageParser parser;
127     /*
128      * When a client connects, we're in capabilities negotiation mode.
129      * @commands is &qmp_cap_negotiation_commands then.  When command
130      * qmp_capabilities succeeds, we go into command mode, and
131      * @command becomes &qmp_commands.
132      */
133     QmpCommandList *commands;
134     bool capab_offered[QMP_CAPABILITY__MAX]; /* capabilities offered */
135     bool capab[QMP_CAPABILITY__MAX];         /* offered and accepted */
136     /*
137      * Protects qmp request/response queue.
138      * Take monitor_lock first when you need both.
139      */
140     QemuMutex qmp_queue_lock;
141     /* Input queue that holds all the parsed QMP requests */
142     GQueue *qmp_requests;
143 } MonitorQMP;
144 
145 #endif
146