Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2019-09-05-v2' into staging
[qemu.git] / tests / libqtest-single.h
blob6f1bb1331c5217763d5d49e5d707103ea2fc3b39
1 /*
2 * QTest - wrappers for test with single QEMU instances
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
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.
11 #ifndef LIBQTEST_SINGLE_H
12 #define LIBQTEST_SINGLE_H
14 #include "libqtest.h"
16 QTestState *global_qtest __attribute__((common, weak));
18 /**
19 * qtest_start:
20 * @args: other arguments to pass to QEMU
22 * Start QEMU and assign the resulting #QTestState to a global variable.
23 * The global variable is used by "shortcut" functions documented below.
25 * Returns: #QTestState instance.
27 static inline QTestState *qtest_start(const char *args)
29 global_qtest = qtest_init(args);
30 return global_qtest;
33 /**
34 * qtest_end:
36 * Shut down the QEMU process started by qtest_start().
38 static inline void qtest_end(void)
40 if (!global_qtest) {
41 return;
43 qtest_quit(global_qtest);
44 global_qtest = NULL;
47 /**
48 * qmp:
49 * @fmt...: QMP message to send to qemu, formatted like
50 * qobject_from_jsonf_nofail(). See parse_escape() for what's
51 * supported after '%'.
53 * Sends a QMP message to QEMU and returns the response.
55 GCC_FMT_ATTR(1, 2)
56 static inline QDict *qmp(const char *fmt, ...)
58 va_list ap;
59 QDict *response;
61 va_start(ap, fmt);
62 response = qtest_vqmp(global_qtest, fmt, ap);
63 va_end(ap);
64 return response;
67 /**
68 * qmp_eventwait:
69 * @s: #event event to wait for.
71 * Continuously polls for QMP responses until it receives the desired event.
73 static inline void qmp_eventwait(const char *event)
75 return qtest_qmp_eventwait(global_qtest, event);
78 /**
79 * get_irq:
80 * @num: Interrupt to observe.
82 * Returns: The level of the @num interrupt.
84 static inline bool get_irq(int num)
86 return qtest_get_irq(global_qtest, num);
89 /**
90 * outb:
91 * @addr: I/O port to write to.
92 * @value: Value being written.
94 * Write an 8-bit value to an I/O port.
96 static inline void outb(uint16_t addr, uint8_t value)
98 qtest_outb(global_qtest, addr, value);
102 * outw:
103 * @addr: I/O port to write to.
104 * @value: Value being written.
106 * Write a 16-bit value to an I/O port.
108 static inline void outw(uint16_t addr, uint16_t value)
110 qtest_outw(global_qtest, addr, value);
114 * outl:
115 * @addr: I/O port to write to.
116 * @value: Value being written.
118 * Write a 32-bit value to an I/O port.
120 static inline void outl(uint16_t addr, uint32_t value)
122 qtest_outl(global_qtest, addr, value);
126 * inb:
127 * @addr: I/O port to read from.
129 * Reads an 8-bit value from an I/O port.
131 * Returns: Value read.
133 static inline uint8_t inb(uint16_t addr)
135 return qtest_inb(global_qtest, addr);
139 * inw:
140 * @addr: I/O port to read from.
142 * Reads a 16-bit value from an I/O port.
144 * Returns: Value read.
146 static inline uint16_t inw(uint16_t addr)
148 return qtest_inw(global_qtest, addr);
152 * inl:
153 * @addr: I/O port to read from.
155 * Reads a 32-bit value from an I/O port.
157 * Returns: Value read.
159 static inline uint32_t inl(uint16_t addr)
161 return qtest_inl(global_qtest, addr);
165 * writeb:
166 * @addr: Guest address to write to.
167 * @value: Value being written.
169 * Writes an 8-bit value to guest memory.
171 static inline void writeb(uint64_t addr, uint8_t value)
173 qtest_writeb(global_qtest, addr, value);
177 * writew:
178 * @addr: Guest address to write to.
179 * @value: Value being written.
181 * Writes a 16-bit value to guest memory.
183 static inline void writew(uint64_t addr, uint16_t value)
185 qtest_writew(global_qtest, addr, value);
189 * writel:
190 * @addr: Guest address to write to.
191 * @value: Value being written.
193 * Writes a 32-bit value to guest memory.
195 static inline void writel(uint64_t addr, uint32_t value)
197 qtest_writel(global_qtest, addr, value);
201 * writeq:
202 * @addr: Guest address to write to.
203 * @value: Value being written.
205 * Writes a 64-bit value to guest memory.
207 static inline void writeq(uint64_t addr, uint64_t value)
209 qtest_writeq(global_qtest, addr, value);
213 * readb:
214 * @addr: Guest address to read from.
216 * Reads an 8-bit value from guest memory.
218 * Returns: Value read.
220 static inline uint8_t readb(uint64_t addr)
222 return qtest_readb(global_qtest, addr);
226 * readw:
227 * @addr: Guest address to read from.
229 * Reads a 16-bit value from guest memory.
231 * Returns: Value read.
233 static inline uint16_t readw(uint64_t addr)
235 return qtest_readw(global_qtest, addr);
239 * readl:
240 * @addr: Guest address to read from.
242 * Reads a 32-bit value from guest memory.
244 * Returns: Value read.
246 static inline uint32_t readl(uint64_t addr)
248 return qtest_readl(global_qtest, addr);
252 * readq:
253 * @addr: Guest address to read from.
255 * Reads a 64-bit value from guest memory.
257 * Returns: Value read.
259 static inline uint64_t readq(uint64_t addr)
261 return qtest_readq(global_qtest, addr);
265 * memread:
266 * @addr: Guest address to read from.
267 * @data: Pointer to where memory contents will be stored.
268 * @size: Number of bytes to read.
270 * Read guest memory into a buffer.
272 static inline void memread(uint64_t addr, void *data, size_t size)
274 qtest_memread(global_qtest, addr, data, size);
278 * memwrite:
279 * @addr: Guest address to write to.
280 * @data: Pointer to the bytes that will be written to guest memory.
281 * @size: Number of bytes to write.
283 * Write a buffer to guest memory.
285 static inline void memwrite(uint64_t addr, const void *data, size_t size)
287 qtest_memwrite(global_qtest, addr, data, size);
291 * clock_step_next:
293 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
295 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
297 static inline int64_t clock_step_next(void)
299 return qtest_clock_step_next(global_qtest);
303 * clock_step:
304 * @step: Number of nanoseconds to advance the clock by.
306 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
308 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
310 static inline int64_t clock_step(int64_t step)
312 return qtest_clock_step(global_qtest, step);
315 #endif