blob: a111c9cddbb5307cdf616bcec3522c84a10bd950 [file] [log] [blame]
Anthony Liguori49ee3592012-03-28 15:42:05 +02001/*
2 * QTest
3 *
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 *
14 */
15#ifndef LIBQTEST_H
16#define LIBQTEST_H
17
18#include <stdint.h>
19#include <stdbool.h>
20#include <sys/types.h>
21
22typedef struct QTestState QTestState;
23
24extern QTestState *global_qtest;
25
26/**
27 * qtest_init:
28 * @extra_args: other arguments to pass to QEMU.
Andreas Färber6acf8012013-02-16 22:44:01 +010029 *
30 * Returns: #QTestState instance.
Anthony Liguori49ee3592012-03-28 15:42:05 +020031 */
32QTestState *qtest_init(const char *extra_args);
33
34/**
35 * qtest_quit:
Andreas Färber6acf8012013-02-16 22:44:01 +010036 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +020037 *
38 * Shut down the QEMU process associated to @s.
39 */
40void qtest_quit(QTestState *s);
41
42/**
Kevin Wolfa3ca1632012-04-26 19:07:55 +020043 * qtest_qmp:
Andreas Färber6acf8012013-02-16 22:44:01 +010044 * @s: #QTestState instance to operate on.
Kevin Wolfa3ca1632012-04-26 19:07:55 +020045 * @fmt...: QMP message to send to qemu
46 *
47 * Sends a QMP message to QEMU
48 */
49void qtest_qmp(QTestState *s, const char *fmt, ...);
50
51/**
Anthony Liguori49ee3592012-03-28 15:42:05 +020052 * qtest_get_irq:
Andreas Färber6acf8012013-02-16 22:44:01 +010053 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +020054 * @num: Interrupt to observe.
55 *
Andreas Färber6acf8012013-02-16 22:44:01 +010056 * Returns: The level of the @num interrupt.
Anthony Liguori49ee3592012-03-28 15:42:05 +020057 */
58bool qtest_get_irq(QTestState *s, int num);
59
60/**
61 * qtest_irq_intercept_in:
Andreas Färber6acf8012013-02-16 22:44:01 +010062 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +020063 * @string: QOM path of a device.
64 *
65 * Associate qtest irqs with the GPIO-in pins of the device
66 * whose path is specified by @string.
67 */
68void qtest_irq_intercept_in(QTestState *s, const char *string);
69
70/**
71 * qtest_irq_intercept_out:
Andreas Färber6acf8012013-02-16 22:44:01 +010072 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +020073 * @string: QOM path of a device.
74 *
75 * Associate qtest irqs with the GPIO-out pins of the device
76 * whose path is specified by @string.
77 */
78void qtest_irq_intercept_out(QTestState *s, const char *string);
79
80/**
81 * qtest_outb:
Andreas Färber6acf8012013-02-16 22:44:01 +010082 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +020083 * @addr: I/O port to write to.
84 * @value: Value being written.
85 *
86 * Write an 8-bit value to an I/O port.
87 */
88void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
89
90/**
91 * qtest_outw:
Andreas Färber6acf8012013-02-16 22:44:01 +010092 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +020093 * @addr: I/O port to write to.
94 * @value: Value being written.
95 *
96 * Write a 16-bit value to an I/O port.
97 */
98void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
99
100/**
101 * qtest_outl:
Andreas Färber6acf8012013-02-16 22:44:01 +0100102 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200103 * @addr: I/O port to write to.
104 * @value: Value being written.
105 *
106 * Write a 32-bit value to an I/O port.
107 */
108void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
109
110/**
111 * qtest_inb:
Andreas Färber6acf8012013-02-16 22:44:01 +0100112 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200113 * @addr: I/O port to read from.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200114 *
115 * Returns an 8-bit value from an I/O port.
116 */
117uint8_t qtest_inb(QTestState *s, uint16_t addr);
118
119/**
120 * qtest_inw:
Andreas Färber6acf8012013-02-16 22:44:01 +0100121 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200122 * @addr: I/O port to read from.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200123 *
124 * Returns a 16-bit value from an I/O port.
125 */
126uint16_t qtest_inw(QTestState *s, uint16_t addr);
127
128/**
129 * qtest_inl:
Andreas Färber6acf8012013-02-16 22:44:01 +0100130 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200131 * @addr: I/O port to read from.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200132 *
133 * Returns a 32-bit value from an I/O port.
134 */
135uint32_t qtest_inl(QTestState *s, uint16_t addr);
136
137/**
138 * qtest_memread:
Andreas Färber6acf8012013-02-16 22:44:01 +0100139 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200140 * @addr: Guest address to read from.
141 * @data: Pointer to where memory contents will be stored.
142 * @size: Number of bytes to read.
143 *
144 * Read guest memory into a buffer.
145 */
146void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
147
148/**
149 * qtest_memwrite:
Andreas Färber6acf8012013-02-16 22:44:01 +0100150 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200151 * @addr: Guest address to write to.
152 * @data: Pointer to the bytes that will be written to guest memory.
153 * @size: Number of bytes to write.
154 *
155 * Write a buffer to guest memory.
156 */
157void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
158
159/**
160 * qtest_clock_step_next:
Andreas Färber6acf8012013-02-16 22:44:01 +0100161 * @s: #QTestState instance to operate on.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200162 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100163 * Advance the vm_clock to the next deadline.
164 *
165 * Returns: The current value of the vm_clock in nanoseconds.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200166 */
167int64_t qtest_clock_step_next(QTestState *s);
168
169/**
170 * qtest_clock_step:
171 * @s: QTestState instance to operate on.
172 * @step: Number of nanoseconds to advance the clock by.
173 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100174 * Advance the vm_clock by @step nanoseconds.
175 *
176 * Returns: The current value of the vm_clock in nanoseconds.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200177 */
178int64_t qtest_clock_step(QTestState *s, int64_t step);
179
180/**
181 * qtest_clock_set:
182 * @s: QTestState instance to operate on.
183 * @val: Nanoseconds value to advance the clock to.
184 *
185 * Advance the vm_clock to @val nanoseconds since the VM was launched.
Andreas Färber6acf8012013-02-16 22:44:01 +0100186 *
187 * Returns: The current value of the vm_clock in nanoseconds.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200188 */
189int64_t qtest_clock_set(QTestState *s, int64_t val);
190
191/**
192 * qtest_get_arch:
193 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100194 * Returns: The architecture for the QEMU executable under test.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200195 */
196const char *qtest_get_arch(void);
197
198/**
199 * qtest_add_func:
200 * @str: Test case path.
201 * @fn: Test case function
202 *
203 * Add a GTester testcase with the given name and function.
204 * The path is prefixed with the architecture under test, as
Andreas Färber6acf8012013-02-16 22:44:01 +0100205 * returned by qtest_get_arch().
Anthony Liguori49ee3592012-03-28 15:42:05 +0200206 */
207void qtest_add_func(const char *str, void (*fn));
208
209/**
210 * qtest_start:
211 * @args: other arguments to pass to QEMU
212 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100213 * Start QEMU and assign the resulting #QTestState to a global variable.
214 * The global variable is used by "shortcut" functions documented below.
215 *
216 * Returns: #QTestState instance.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200217 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100218static inline QTestState *qtest_start(const char *args)
219{
220 global_qtest = qtest_init(args);
221 return global_qtest;
222}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200223
224/**
Kevin Wolfa3ca1632012-04-26 19:07:55 +0200225 * qmp:
226 * @fmt...: QMP message to send to qemu
227 *
228 * Sends a QMP message to QEMU
229 */
230#define qmp(fmt, ...) qtest_qmp(global_qtest, fmt, ## __VA_ARGS__)
231
232/**
Anthony Liguori49ee3592012-03-28 15:42:05 +0200233 * get_irq:
234 * @num: Interrupt to observe.
235 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100236 * Returns: The level of the @num interrupt.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200237 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100238static inline bool get_irq(int num)
239{
240 return qtest_get_irq(global_qtest, num);
241}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200242
243/**
244 * irq_intercept_in:
245 * @string: QOM path of a device.
246 *
247 * Associate qtest irqs with the GPIO-in pins of the device
248 * whose path is specified by @string.
249 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100250static inline void irq_intercept_in(const char *string)
251{
252 qtest_irq_intercept_in(global_qtest, string);
253}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200254
255/**
256 * qtest_irq_intercept_out:
257 * @string: QOM path of a device.
258 *
259 * Associate qtest irqs with the GPIO-out pins of the device
260 * whose path is specified by @string.
261 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100262static inline void irq_intercept_out(const char *string)
263{
264 qtest_irq_intercept_out(global_qtest, string);
265}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200266
267/**
268 * outb:
269 * @addr: I/O port to write to.
270 * @value: Value being written.
271 *
272 * Write an 8-bit value to an I/O port.
273 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100274static inline void outb(uint16_t addr, uint8_t value)
275{
276 qtest_outb(global_qtest, addr, value);
277}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200278
279/**
280 * outw:
281 * @addr: I/O port to write to.
282 * @value: Value being written.
283 *
284 * Write a 16-bit value to an I/O port.
285 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100286static inline void outw(uint16_t addr, uint16_t value)
287{
288 qtest_outw(global_qtest, addr, value);
289}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200290
291/**
292 * outl:
293 * @addr: I/O port to write to.
294 * @value: Value being written.
295 *
296 * Write a 32-bit value to an I/O port.
297 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100298static inline void outl(uint16_t addr, uint32_t value)
299{
300 qtest_outl(global_qtest, addr, value);
301}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200302
303/**
304 * inb:
305 * @addr: I/O port to read from.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200306 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100307 * Reads an 8-bit value from an I/O port.
308 *
309 * Returns: Value read.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200310 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100311static inline uint8_t inb(uint16_t addr)
312{
313 return qtest_inb(global_qtest, addr);
314}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200315
316/**
317 * inw:
318 * @addr: I/O port to read from.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200319 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100320 * Reads a 16-bit value from an I/O port.
321 *
322 * Returns: Value read.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200323 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100324static inline uint16_t inw(uint16_t addr)
325{
326 return qtest_inw(global_qtest, addr);
327}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200328
329/**
330 * inl:
331 * @addr: I/O port to read from.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200332 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100333 * Reads a 32-bit value from an I/O port.
334 *
335 * Returns: Value read.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200336 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100337static inline uint32_t inl(uint16_t addr)
338{
339 return qtest_inl(global_qtest, addr);
340}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200341
342/**
343 * memread:
344 * @addr: Guest address to read from.
345 * @data: Pointer to where memory contents will be stored.
346 * @size: Number of bytes to read.
347 *
348 * Read guest memory into a buffer.
349 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100350static inline void memread(uint64_t addr, void *data, size_t size)
351{
352 qtest_memread(global_qtest, addr, data, size);
353}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200354
355/**
356 * memwrite:
357 * @addr: Guest address to write to.
358 * @data: Pointer to the bytes that will be written to guest memory.
359 * @size: Number of bytes to write.
360 *
361 * Write a buffer to guest memory.
362 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100363static inline void memwrite(uint64_t addr, const void *data, size_t size)
364{
365 qtest_memwrite(global_qtest, addr, data, size);
366}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200367
368/**
369 * clock_step_next:
370 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100371 * Advance the vm_clock to the next deadline.
372 *
373 * Returns: The current value of the vm_clock in nanoseconds.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200374 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100375static inline int64_t clock_step_next(void)
376{
377 return qtest_clock_step_next(global_qtest);
378}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200379
380/**
381 * clock_step:
382 * @step: Number of nanoseconds to advance the clock by.
383 *
Andreas Färber6acf8012013-02-16 22:44:01 +0100384 * Advance the vm_clock by @step nanoseconds.
385 *
386 * Returns: The current value of the vm_clock in nanoseconds.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200387 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100388static inline int64_t clock_step(int64_t step)
389{
390 return qtest_clock_step(global_qtest, step);
391}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200392
393/**
394 * clock_set:
395 * @val: Nanoseconds value to advance the clock to.
396 *
397 * Advance the vm_clock to @val nanoseconds since the VM was launched.
Andreas Färber6acf8012013-02-16 22:44:01 +0100398 *
399 * Returns: The current value of the vm_clock in nanoseconds.
Anthony Liguori49ee3592012-03-28 15:42:05 +0200400 */
Andreas Färber6acf8012013-02-16 22:44:01 +0100401static inline int64_t clock_set(int64_t val)
402{
403 return qtest_clock_set(global_qtest, val);
404}
Anthony Liguori49ee3592012-03-28 15:42:05 +0200405
406#endif