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