4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
13 * This work is licensed under the terms of the GNU GPL, version 2 or later.
14 * See the COPYING file in the top-level directory.
24 #include <sys/types.h>
25 #include "qapi/qmp/qdict.h"
26 #include "glib-compat.h"
28 typedef struct QTestState QTestState
;
30 extern QTestState
*global_qtest
;
34 * @extra_args: other arguments to pass to QEMU.
36 * Returns: #QTestState instance.
38 QTestState
*qtest_init(const char *extra_args
);
42 * @s: #QTestState instance to operate on.
44 * Shut down the QEMU process associated to @s.
46 void qtest_quit(QTestState
*s
);
49 * qtest_qmp_discard_response:
50 * @s: #QTestState instance to operate on.
51 * @fmt...: QMP message to send to qemu
53 * Sends a QMP message to QEMU and consumes the response.
55 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...);
59 * @s: #QTestState instance to operate on.
60 * @fmt...: QMP message to send to qemu
62 * Sends a QMP message to QEMU and returns the response.
64 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...);
68 * @s: #QTestState instance to operate on.
69 * @fmt...: QMP message to send to qemu
71 * Sends a QMP message to QEMU and leaves the response in the stream.
73 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...);
76 * qtest_qmpv_discard_response:
77 * @s: #QTestState instance to operate on.
78 * @fmt: QMP message to send to QEMU
79 * @ap: QMP message arguments
81 * Sends a QMP message to QEMU and consumes the response.
83 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
);
87 * @s: #QTestState instance to operate on.
88 * @fmt: QMP message to send to QEMU
89 * @ap: QMP message arguments
91 * Sends a QMP message to QEMU and returns the response.
93 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
97 * @s: #QTestState instance to operate on.
98 * @fmt: QMP message to send to QEMU
99 * @ap: QMP message arguments
101 * Sends a QMP message to QEMU and leaves the response in the stream.
103 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
107 * @s: #QTestState instance to operate on.
109 * Reads a QMP message from QEMU and returns the response.
111 QDict
*qtest_qmp_receive(QTestState
*s
);
114 * qtest_qmp_eventwait:
115 * @s: #QTestState instance to operate on.
116 * @s: #event event to wait for.
118 * Continuosly polls for QMP responses until it receives the desired event.
120 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
124 * @s: #QTestState instance to operate on.
125 * @num: Interrupt to observe.
127 * Returns: The level of the @num interrupt.
129 bool qtest_get_irq(QTestState
*s
, int num
);
132 * qtest_irq_intercept_in:
133 * @s: #QTestState instance to operate on.
134 * @string: QOM path of a device.
136 * Associate qtest irqs with the GPIO-in pins of the device
137 * whose path is specified by @string.
139 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
142 * qtest_irq_intercept_out:
143 * @s: #QTestState instance to operate on.
144 * @string: QOM path of a device.
146 * Associate qtest irqs with the GPIO-out pins of the device
147 * whose path is specified by @string.
149 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
153 * @s: #QTestState instance to operate on.
154 * @addr: I/O port to write to.
155 * @value: Value being written.
157 * Write an 8-bit value to an I/O port.
159 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
163 * @s: #QTestState instance to operate on.
164 * @addr: I/O port to write to.
165 * @value: Value being written.
167 * Write a 16-bit value to an I/O port.
169 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
173 * @s: #QTestState instance to operate on.
174 * @addr: I/O port to write to.
175 * @value: Value being written.
177 * Write a 32-bit value to an I/O port.
179 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
183 * @s: #QTestState instance to operate on.
184 * @addr: I/O port to read from.
186 * Returns an 8-bit value from an I/O port.
188 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
192 * @s: #QTestState instance to operate on.
193 * @addr: I/O port to read from.
195 * Returns a 16-bit value from an I/O port.
197 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
201 * @s: #QTestState instance to operate on.
202 * @addr: I/O port to read from.
204 * Returns a 32-bit value from an I/O port.
206 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
210 * @s: #QTestState instance to operate on.
211 * @addr: Guest address to write to.
212 * @value: Value being written.
214 * Writes an 8-bit value to memory.
216 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
220 * @s: #QTestState instance to operate on.
221 * @addr: Guest address to write to.
222 * @value: Value being written.
224 * Writes a 16-bit value to memory.
226 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
230 * @s: #QTestState instance to operate on.
231 * @addr: Guest address to write to.
232 * @value: Value being written.
234 * Writes a 32-bit value to memory.
236 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
240 * @s: #QTestState instance to operate on.
241 * @addr: Guest address to write to.
242 * @value: Value being written.
244 * Writes a 64-bit value to memory.
246 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
250 * @s: #QTestState instance to operate on.
251 * @addr: Guest address to read from.
253 * Reads an 8-bit value from memory.
255 * Returns: Value read.
257 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
261 * @s: #QTestState instance to operate on.
262 * @addr: Guest address to read from.
264 * Reads a 16-bit value from memory.
266 * Returns: Value read.
268 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
272 * @s: #QTestState instance to operate on.
273 * @addr: Guest address to read from.
275 * Reads a 32-bit value from memory.
277 * Returns: Value read.
279 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
283 * @s: #QTestState instance to operate on.
284 * @addr: Guest address to read from.
286 * Reads a 64-bit value from memory.
288 * Returns: Value read.
290 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
294 * @s: #QTestState instance to operate on.
295 * @addr: Guest address to read from.
296 * @data: Pointer to where memory contents will be stored.
297 * @size: Number of bytes to read.
299 * Read guest memory into a buffer.
301 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
305 * @s: #QTestState instance to operate on.
306 * @addr: Guest address to write to.
307 * @data: Pointer to the bytes that will be written to guest memory.
308 * @size: Number of bytes to write.
310 * Write a buffer to guest memory.
312 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
316 * @s: #QTestState instance to operate on.
317 * @addr: Guest address to write to.
318 * @patt: Byte pattern to fill the guest memory region with.
319 * @size: Number of bytes to write.
321 * Write a pattern to guest memory.
323 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
326 * qtest_clock_step_next:
327 * @s: #QTestState instance to operate on.
329 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
331 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
333 int64_t qtest_clock_step_next(QTestState
*s
);
337 * @s: QTestState instance to operate on.
338 * @step: Number of nanoseconds to advance the clock by.
340 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
342 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
344 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
348 * @s: QTestState instance to operate on.
349 * @val: Nanoseconds value to advance the clock to.
351 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
353 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
355 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
360 * Returns: The architecture for the QEMU executable under test.
362 const char *qtest_get_arch(void);
366 * @str: Test case path.
367 * @fn: Test case function
369 * Add a GTester testcase with the given name and function.
370 * The path is prefixed with the architecture under test, as
371 * returned by qtest_get_arch().
373 void qtest_add_func(const char *str
, void (*fn
));
376 * qtest_add_data_func:
377 * @str: Test case path.
378 * @data: Test case data
379 * @fn: Test case function
381 * Add a GTester testcase with the given name, data and function.
382 * The path is prefixed with the architecture under test, as
383 * returned by qtest_get_arch().
385 void qtest_add_data_func(const char *str
, const void *data
, void (*fn
));
389 * @testpath: Test case path
390 * @Fixture: Fixture type
391 * @tdata: Test case data
392 * @fsetup: Test case setup function
393 * @ftest: Test case function
394 * @fteardown: Test case teardown function
396 * Add a GTester testcase with the given name, data and functions.
397 * The path is prefixed with the architecture under test, as
398 * returned by qtest_get_arch().
400 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
402 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
403 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
409 * @args: other arguments to pass to QEMU
411 * Start QEMU and assign the resulting #QTestState to a global variable.
412 * The global variable is used by "shortcut" functions documented below.
414 * Returns: #QTestState instance.
416 static inline QTestState
*qtest_start(const char *args
)
418 global_qtest
= qtest_init(args
);
425 * Shut down the QEMU process started by qtest_start().
427 static inline void qtest_end(void)
429 qtest_quit(global_qtest
);
435 * @fmt...: QMP message to send to qemu
437 * Sends a QMP message to QEMU and returns the response.
439 QDict
*qmp(const char *fmt
, ...);
443 * @fmt...: QMP message to send to qemu
445 * Sends a QMP message to QEMU and leaves the response in the stream.
447 void qmp_async(const char *fmt
, ...);
450 * qmp_discard_response:
451 * @fmt...: QMP message to send to qemu
453 * Sends a QMP message to QEMU and consumes the response.
455 void qmp_discard_response(const char *fmt
, ...);
460 * Reads a QMP message from QEMU and returns the response.
462 static inline QDict
*qmp_receive(void)
464 return qtest_qmp_receive(global_qtest
);
469 * @s: #event event to wait for.
471 * Continuosly polls for QMP responses until it receives the desired event.
473 static inline void qmp_eventwait(const char *event
)
475 return qtest_qmp_eventwait(global_qtest
, event
);
480 * @num: Interrupt to observe.
482 * Returns: The level of the @num interrupt.
484 static inline bool get_irq(int num
)
486 return qtest_get_irq(global_qtest
, num
);
491 * @string: QOM path of a device.
493 * Associate qtest irqs with the GPIO-in pins of the device
494 * whose path is specified by @string.
496 static inline void irq_intercept_in(const char *string
)
498 qtest_irq_intercept_in(global_qtest
, string
);
502 * qtest_irq_intercept_out:
503 * @string: QOM path of a device.
505 * Associate qtest irqs with the GPIO-out pins of the device
506 * whose path is specified by @string.
508 static inline void irq_intercept_out(const char *string
)
510 qtest_irq_intercept_out(global_qtest
, string
);
515 * @addr: I/O port to write to.
516 * @value: Value being written.
518 * Write an 8-bit value to an I/O port.
520 static inline void outb(uint16_t addr
, uint8_t value
)
522 qtest_outb(global_qtest
, addr
, value
);
527 * @addr: I/O port to write to.
528 * @value: Value being written.
530 * Write a 16-bit value to an I/O port.
532 static inline void outw(uint16_t addr
, uint16_t value
)
534 qtest_outw(global_qtest
, addr
, value
);
539 * @addr: I/O port to write to.
540 * @value: Value being written.
542 * Write a 32-bit value to an I/O port.
544 static inline void outl(uint16_t addr
, uint32_t value
)
546 qtest_outl(global_qtest
, addr
, value
);
551 * @addr: I/O port to read from.
553 * Reads an 8-bit value from an I/O port.
555 * Returns: Value read.
557 static inline uint8_t inb(uint16_t addr
)
559 return qtest_inb(global_qtest
, addr
);
564 * @addr: I/O port to read from.
566 * Reads a 16-bit value from an I/O port.
568 * Returns: Value read.
570 static inline uint16_t inw(uint16_t addr
)
572 return qtest_inw(global_qtest
, addr
);
577 * @addr: I/O port to read from.
579 * Reads a 32-bit value from an I/O port.
581 * Returns: Value read.
583 static inline uint32_t inl(uint16_t addr
)
585 return qtest_inl(global_qtest
, addr
);
590 * @addr: Guest address to write to.
591 * @value: Value being written.
593 * Writes an 8-bit value to guest memory.
595 static inline void writeb(uint64_t addr
, uint8_t value
)
597 qtest_writeb(global_qtest
, addr
, value
);
602 * @addr: Guest address to write to.
603 * @value: Value being written.
605 * Writes a 16-bit value to guest memory.
607 static inline void writew(uint64_t addr
, uint16_t value
)
609 qtest_writew(global_qtest
, addr
, value
);
614 * @addr: Guest address to write to.
615 * @value: Value being written.
617 * Writes a 32-bit value to guest memory.
619 static inline void writel(uint64_t addr
, uint32_t value
)
621 qtest_writel(global_qtest
, addr
, value
);
626 * @addr: Guest address to write to.
627 * @value: Value being written.
629 * Writes a 64-bit value to guest memory.
631 static inline void writeq(uint64_t addr
, uint64_t value
)
633 qtest_writeq(global_qtest
, addr
, value
);
638 * @addr: Guest address to read from.
640 * Reads an 8-bit value from guest memory.
642 * Returns: Value read.
644 static inline uint8_t readb(uint64_t addr
)
646 return qtest_readb(global_qtest
, addr
);
651 * @addr: Guest address to read from.
653 * Reads a 16-bit value from guest memory.
655 * Returns: Value read.
657 static inline uint16_t readw(uint64_t addr
)
659 return qtest_readw(global_qtest
, addr
);
664 * @addr: Guest address to read from.
666 * Reads a 32-bit value from guest memory.
668 * Returns: Value read.
670 static inline uint32_t readl(uint64_t addr
)
672 return qtest_readl(global_qtest
, addr
);
677 * @addr: Guest address to read from.
679 * Reads a 64-bit value from guest memory.
681 * Returns: Value read.
683 static inline uint64_t readq(uint64_t addr
)
685 return qtest_readq(global_qtest
, addr
);
690 * @addr: Guest address to read from.
691 * @data: Pointer to where memory contents will be stored.
692 * @size: Number of bytes to read.
694 * Read guest memory into a buffer.
696 static inline void memread(uint64_t addr
, void *data
, size_t size
)
698 qtest_memread(global_qtest
, addr
, data
, size
);
703 * @addr: Guest address to write to.
704 * @data: Pointer to the bytes that will be written to guest memory.
705 * @size: Number of bytes to write.
707 * Write a buffer to guest memory.
709 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
711 qtest_memwrite(global_qtest
, addr
, data
, size
);
716 * @addr: Guest address to write to.
717 * @patt: Byte pattern to fill the guest memory region with.
718 * @size: Number of bytes to write.
720 * Write a pattern to guest memory.
722 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
724 qtest_memset(global_qtest
, addr
, patt
, size
);
730 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
732 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
734 static inline int64_t clock_step_next(void)
736 return qtest_clock_step_next(global_qtest
);
741 * @step: Number of nanoseconds to advance the clock by.
743 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
745 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
747 static inline int64_t clock_step(int64_t step
)
749 return qtest_clock_step(global_qtest
, step
);
754 * @val: Nanoseconds value to advance the clock to.
756 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
758 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
760 static inline int64_t clock_set(int64_t val
)
762 return qtest_clock_set(global_qtest
, val
);
768 * Returns: True if the architecture under test has a big endian configuration.
770 bool qtest_big_endian(void);