xref: /openbmc/qemu/tests/qtest/libqtest-single.h (revision 764a6ee9)
1 /*
2  * QTest - wrappers for test with single QEMU instances
3  *
4  * Copyright IBM, Corp. 2012
5  * Copyright Red Hat, Inc. 2012
6  * Copyright SUSE LINUX Products GmbH 2013
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  */
11 #ifndef LIBQTEST_SINGLE_H
12 #define LIBQTEST_SINGLE_H
13 
14 #include "libqtest.h"
15 
16 #ifndef _WIN32
17 QTestState *global_qtest __attribute__((common, weak));
18 #else
19 __declspec(selectany) QTestState *global_qtest;
20 #endif
21 
22 /**
23  * qtest_start:
24  * @args: other arguments to pass to QEMU
25  *
26  * Start QEMU and assign the resulting #QTestState to a global variable.
27  * The global variable is used by "shortcut" functions documented below.
28  *
29  * Returns: #QTestState instance.
30  */
31 static inline QTestState *qtest_start(const char *args)
32 {
33     global_qtest = qtest_init(args);
34     return global_qtest;
35 }
36 
37 /**
38  * qtest_end:
39  *
40  * Shut down the QEMU process started by qtest_start().
41  */
42 static inline void qtest_end(void)
43 {
44     if (!global_qtest) {
45         return;
46     }
47     qtest_quit(global_qtest);
48     global_qtest = NULL;
49 }
50 
51 /**
52  * qmp:
53  * @fmt...: QMP message to send to qemu, formatted like
54  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
55  * supported after '%'.
56  *
57  * Sends a QMP message to QEMU and returns the response.
58  */
59 G_GNUC_PRINTF(1, 2)
60 static inline QDict *qmp(const char *fmt, ...)
61 {
62     va_list ap;
63     QDict *response;
64 
65     va_start(ap, fmt);
66     response = qtest_vqmp(global_qtest, fmt, ap);
67     va_end(ap);
68     return response;
69 }
70 
71 /**
72  * qmp_eventwait:
73  * @s: #event event to wait for.
74  *
75  * Continuously polls for QMP responses until it receives the desired event.
76  */
77 static inline void qmp_eventwait(const char *event)
78 {
79     return qtest_qmp_eventwait(global_qtest, event);
80 }
81 
82 /**
83  * get_irq:
84  * @num: Interrupt to observe.
85  *
86  * Returns: The level of the @num interrupt.
87  */
88 static inline bool get_irq(int num)
89 {
90     return qtest_get_irq(global_qtest, num);
91 }
92 
93 /**
94  * outb:
95  * @addr: I/O port to write to.
96  * @value: Value being written.
97  *
98  * Write an 8-bit value to an I/O port.
99  */
100 static inline void outb(uint16_t addr, uint8_t value)
101 {
102     qtest_outb(global_qtest, addr, value);
103 }
104 
105 /**
106  * outw:
107  * @addr: I/O port to write to.
108  * @value: Value being written.
109  *
110  * Write a 16-bit value to an I/O port.
111  */
112 static inline void outw(uint16_t addr, uint16_t value)
113 {
114     qtest_outw(global_qtest, addr, value);
115 }
116 
117 /**
118  * outl:
119  * @addr: I/O port to write to.
120  * @value: Value being written.
121  *
122  * Write a 32-bit value to an I/O port.
123  */
124 static inline void outl(uint16_t addr, uint32_t value)
125 {
126     qtest_outl(global_qtest, addr, value);
127 }
128 
129 /**
130  * inb:
131  * @addr: I/O port to read from.
132  *
133  * Reads an 8-bit value from an I/O port.
134  *
135  * Returns: Value read.
136  */
137 static inline uint8_t inb(uint16_t addr)
138 {
139     return qtest_inb(global_qtest, addr);
140 }
141 
142 /**
143  * inw:
144  * @addr: I/O port to read from.
145  *
146  * Reads a 16-bit value from an I/O port.
147  *
148  * Returns: Value read.
149  */
150 static inline uint16_t inw(uint16_t addr)
151 {
152     return qtest_inw(global_qtest, addr);
153 }
154 
155 /**
156  * inl:
157  * @addr: I/O port to read from.
158  *
159  * Reads a 32-bit value from an I/O port.
160  *
161  * Returns: Value read.
162  */
163 static inline uint32_t inl(uint16_t addr)
164 {
165     return qtest_inl(global_qtest, addr);
166 }
167 
168 /**
169  * writeb:
170  * @addr: Guest address to write to.
171  * @value: Value being written.
172  *
173  * Writes an 8-bit value to guest memory.
174  */
175 static inline void writeb(uint64_t addr, uint8_t value)
176 {
177     qtest_writeb(global_qtest, addr, value);
178 }
179 
180 /**
181  * writew:
182  * @addr: Guest address to write to.
183  * @value: Value being written.
184  *
185  * Writes a 16-bit value to guest memory.
186  */
187 static inline void writew(uint64_t addr, uint16_t value)
188 {
189     qtest_writew(global_qtest, addr, value);
190 }
191 
192 /**
193  * writel:
194  * @addr: Guest address to write to.
195  * @value: Value being written.
196  *
197  * Writes a 32-bit value to guest memory.
198  */
199 static inline void writel(uint64_t addr, uint32_t value)
200 {
201     qtest_writel(global_qtest, addr, value);
202 }
203 
204 /**
205  * writeq:
206  * @addr: Guest address to write to.
207  * @value: Value being written.
208  *
209  * Writes a 64-bit value to guest memory.
210  */
211 static inline void writeq(uint64_t addr, uint64_t value)
212 {
213     qtest_writeq(global_qtest, addr, value);
214 }
215 
216 /**
217  * readb:
218  * @addr: Guest address to read from.
219  *
220  * Reads an 8-bit value from guest memory.
221  *
222  * Returns: Value read.
223  */
224 static inline uint8_t readb(uint64_t addr)
225 {
226     return qtest_readb(global_qtest, addr);
227 }
228 
229 /**
230  * readw:
231  * @addr: Guest address to read from.
232  *
233  * Reads a 16-bit value from guest memory.
234  *
235  * Returns: Value read.
236  */
237 static inline uint16_t readw(uint64_t addr)
238 {
239     return qtest_readw(global_qtest, addr);
240 }
241 
242 /**
243  * readl:
244  * @addr: Guest address to read from.
245  *
246  * Reads a 32-bit value from guest memory.
247  *
248  * Returns: Value read.
249  */
250 static inline uint32_t readl(uint64_t addr)
251 {
252     return qtest_readl(global_qtest, addr);
253 }
254 
255 /**
256  * readq:
257  * @addr: Guest address to read from.
258  *
259  * Reads a 64-bit value from guest memory.
260  *
261  * Returns: Value read.
262  */
263 static inline uint64_t readq(uint64_t addr)
264 {
265     return qtest_readq(global_qtest, addr);
266 }
267 
268 /**
269  * memread:
270  * @addr: Guest address to read from.
271  * @data: Pointer to where memory contents will be stored.
272  * @size: Number of bytes to read.
273  *
274  * Read guest memory into a buffer.
275  */
276 static inline void memread(uint64_t addr, void *data, size_t size)
277 {
278     qtest_memread(global_qtest, addr, data, size);
279 }
280 
281 /**
282  * memwrite:
283  * @addr: Guest address to write to.
284  * @data: Pointer to the bytes that will be written to guest memory.
285  * @size: Number of bytes to write.
286  *
287  * Write a buffer to guest memory.
288  */
289 static inline void memwrite(uint64_t addr, const void *data, size_t size)
290 {
291     qtest_memwrite(global_qtest, addr, data, size);
292 }
293 
294 /**
295  * clock_step_next:
296  *
297  * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
298  *
299  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
300  */
301 static inline int64_t clock_step_next(void)
302 {
303     return qtest_clock_step_next(global_qtest);
304 }
305 
306 /**
307  * clock_step:
308  * @step: Number of nanoseconds to advance the clock by.
309  *
310  * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
311  *
312  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
313  */
314 static inline int64_t clock_step(int64_t step)
315 {
316     return qtest_clock_step(global_qtest, step);
317 }
318 
319 #endif
320