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.
20 #include "qapi/qmp/qdict.h"
22 typedef struct QTestState QTestState
;
24 extern QTestState
*global_qtest
;
28 * @extra_args: other arguments to pass to QEMU.
30 * Returns: #QTestState instance.
32 QTestState
*qtest_init(const char *extra_args
);
36 * @s: #QTestState instance to operate on.
38 * Shut down the QEMU process associated to @s.
40 void qtest_quit(QTestState
*s
);
43 * qtest_qmp_discard_response:
44 * @s: #QTestState instance to operate on.
45 * @fmt...: QMP message to send to qemu
47 * Sends a QMP message to QEMU and consumes the response.
49 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...);
53 * @s: #QTestState instance to operate on.
54 * @fmt...: QMP message to send to qemu
56 * Sends a QMP message to QEMU and returns the response.
58 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...);
62 * @s: #QTestState instance to operate on.
63 * @fmt...: QMP message to send to qemu
65 * Sends a QMP message to QEMU and leaves the response in the stream.
67 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...);
70 * qtest_qmpv_discard_response:
71 * @s: #QTestState instance to operate on.
72 * @fmt: QMP message to send to QEMU
73 * @ap: QMP message arguments
75 * Sends a QMP message to QEMU and consumes the response.
77 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
);
81 * @s: #QTestState instance to operate on.
82 * @fmt: QMP message to send to QEMU
83 * @ap: QMP message arguments
85 * Sends a QMP message to QEMU and returns the response.
87 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
91 * @s: #QTestState instance to operate on.
92 * @fmt: QMP message to send to QEMU
93 * @ap: QMP message arguments
95 * Sends a QMP message to QEMU and leaves the response in the stream.
97 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
101 * @s: #QTestState instance to operate on.
103 * Reads a QMP message from QEMU and returns the response.
105 QDict
*qtest_qmp_receive(QTestState
*s
);
108 * qtest_qmp_eventwait:
109 * @s: #QTestState instance to operate on.
110 * @s: #event event to wait for.
112 * Continuosly polls for QMP responses until it receives the desired event.
114 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
118 * @s: #QTestState instance to operate on.
119 * @fmt...: HMP command to send to QEMU
121 * Send HMP command to QEMU via QMP's human-monitor-command.
123 * Returns: the command's output. The caller should g_free() it.
125 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...);
129 * @s: #QTestState instance to operate on.
130 * @fmt: HMP command to send to QEMU
131 * @ap: HMP command arguments
133 * Send HMP command to QEMU via QMP's human-monitor-command.
135 * Returns: the command's output. The caller should g_free() it.
137 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
);
141 * @s: #QTestState instance to operate on.
142 * @num: Interrupt to observe.
144 * Returns: The level of the @num interrupt.
146 bool qtest_get_irq(QTestState
*s
, int num
);
149 * qtest_irq_intercept_in:
150 * @s: #QTestState instance to operate on.
151 * @string: QOM path of a device.
153 * Associate qtest irqs with the GPIO-in pins of the device
154 * whose path is specified by @string.
156 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
159 * qtest_irq_intercept_out:
160 * @s: #QTestState instance to operate on.
161 * @string: QOM path of a device.
163 * Associate qtest irqs with the GPIO-out pins of the device
164 * whose path is specified by @string.
166 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
170 * @s: #QTestState instance to operate on.
171 * @addr: I/O port to write to.
172 * @value: Value being written.
174 * Write an 8-bit value to an I/O port.
176 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
180 * @s: #QTestState instance to operate on.
181 * @addr: I/O port to write to.
182 * @value: Value being written.
184 * Write a 16-bit value to an I/O port.
186 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
190 * @s: #QTestState instance to operate on.
191 * @addr: I/O port to write to.
192 * @value: Value being written.
194 * Write a 32-bit value to an I/O port.
196 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
200 * @s: #QTestState instance to operate on.
201 * @addr: I/O port to read from.
203 * Returns an 8-bit value from an I/O port.
205 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
209 * @s: #QTestState instance to operate on.
210 * @addr: I/O port to read from.
212 * Returns a 16-bit value from an I/O port.
214 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
218 * @s: #QTestState instance to operate on.
219 * @addr: I/O port to read from.
221 * Returns a 32-bit value from an I/O port.
223 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
227 * @s: #QTestState instance to operate on.
228 * @addr: Guest address to write to.
229 * @value: Value being written.
231 * Writes an 8-bit value to memory.
233 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
237 * @s: #QTestState instance to operate on.
238 * @addr: Guest address to write to.
239 * @value: Value being written.
241 * Writes a 16-bit value to memory.
243 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
247 * @s: #QTestState instance to operate on.
248 * @addr: Guest address to write to.
249 * @value: Value being written.
251 * Writes a 32-bit value to memory.
253 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
257 * @s: #QTestState instance to operate on.
258 * @addr: Guest address to write to.
259 * @value: Value being written.
261 * Writes a 64-bit value to memory.
263 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
267 * @s: #QTestState instance to operate on.
268 * @addr: Guest address to read from.
270 * Reads an 8-bit value from memory.
272 * Returns: Value read.
274 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
278 * @s: #QTestState instance to operate on.
279 * @addr: Guest address to read from.
281 * Reads a 16-bit value from memory.
283 * Returns: Value read.
285 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
289 * @s: #QTestState instance to operate on.
290 * @addr: Guest address to read from.
292 * Reads a 32-bit value from memory.
294 * Returns: Value read.
296 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
300 * @s: #QTestState instance to operate on.
301 * @addr: Guest address to read from.
303 * Reads a 64-bit value from memory.
305 * Returns: Value read.
307 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
311 * @s: #QTestState instance to operate on.
312 * @addr: Guest address to read from.
313 * @data: Pointer to where memory contents will be stored.
314 * @size: Number of bytes to read.
316 * Read guest memory into a buffer.
318 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
322 * @s: #QTestState instance to operate on.
323 * @name: name of the command to call.
324 * @nargs: Number of args.
325 * @args: Guest address to read args from.
326 * @nret: Number of return value.
327 * @ret: Guest address to write return values to.
329 * Call an RTAS function
331 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
332 uint32_t nargs
, uint64_t args
,
333 uint32_t nret
, uint64_t ret
);
337 * @s: #QTestState instance to operate on.
338 * @addr: Guest address to read from.
339 * @data: Pointer to where memory contents will be stored.
340 * @size: Number of bytes to read.
342 * Read guest memory into a buffer and receive using a base64 encoding.
344 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
348 * @s: #QTestState instance to operate on.
349 * @addr: Guest address to write to.
350 * @data: Pointer to the bytes that will be written to guest memory.
351 * @size: Number of bytes to write.
353 * Write a buffer to guest memory.
355 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
359 * @s: #QTestState instance to operate on.
360 * @addr: Guest address to write to.
361 * @data: Pointer to the bytes that will be written to guest memory.
362 * @size: Number of bytes to write.
364 * Write a buffer to guest memory and transmit using a base64 encoding.
366 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
367 const void *data
, size_t size
);
371 * @s: #QTestState instance to operate on.
372 * @addr: Guest address to write to.
373 * @patt: Byte pattern to fill the guest memory region with.
374 * @size: Number of bytes to write.
376 * Write a pattern to guest memory.
378 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
381 * qtest_clock_step_next:
382 * @s: #QTestState instance to operate on.
384 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
386 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
388 int64_t qtest_clock_step_next(QTestState
*s
);
392 * @s: QTestState instance to operate on.
393 * @step: Number of nanoseconds to advance the clock by.
395 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
397 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
399 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
403 * @s: QTestState instance to operate on.
404 * @val: Nanoseconds value to advance the clock to.
406 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
408 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
410 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
414 * @s: QTestState instance to operate on.
416 * Returns: True if the architecture under test has a big endian configuration.
418 bool qtest_big_endian(QTestState
*s
);
423 * Returns: The architecture for the QEMU executable under test.
425 const char *qtest_get_arch(void);
429 * @str: Test case path.
430 * @fn: Test case function
432 * Add a GTester testcase with the given name and function.
433 * The path is prefixed with the architecture under test, as
434 * returned by qtest_get_arch().
436 void qtest_add_func(const char *str
, void (*fn
)(void));
439 * qtest_add_data_func:
440 * @str: Test case path.
441 * @data: Test case data
442 * @fn: Test case function
444 * Add a GTester testcase with the given name, data and function.
445 * The path is prefixed with the architecture under test, as
446 * returned by qtest_get_arch().
448 void qtest_add_data_func(const char *str
, const void *data
,
449 void (*fn
)(const void *));
452 * qtest_add_data_func_full:
453 * @str: Test case path.
454 * @data: Test case data
455 * @fn: Test case function
456 * @data_free_func: GDestroyNotify for data
458 * Add a GTester testcase with the given name, data and function.
459 * The path is prefixed with the architecture under test, as
460 * returned by qtest_get_arch().
462 * @data is passed to @data_free_func() on test completion.
464 void qtest_add_data_func_full(const char *str
, void *data
,
465 void (*fn
)(const void *),
466 GDestroyNotify data_free_func
);
470 * @testpath: Test case path
471 * @Fixture: Fixture type
472 * @tdata: Test case data
473 * @fsetup: Test case setup function
474 * @ftest: Test case function
475 * @fteardown: Test case teardown function
477 * Add a GTester testcase with the given name, data and functions.
478 * The path is prefixed with the architecture under test, as
479 * returned by qtest_get_arch().
481 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
483 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
484 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
488 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
492 * @args: other arguments to pass to QEMU
494 * Start QEMU and assign the resulting #QTestState to a global variable.
495 * The global variable is used by "shortcut" functions documented below.
497 * Returns: #QTestState instance.
499 static inline QTestState
*qtest_start(const char *args
)
501 global_qtest
= qtest_init(args
);
508 * Shut down the QEMU process started by qtest_start().
510 static inline void qtest_end(void)
512 qtest_quit(global_qtest
);
518 * @fmt...: QMP message to send to qemu
520 * Sends a QMP message to QEMU and returns the response.
522 QDict
*qmp(const char *fmt
, ...);
526 * @fmt...: QMP message to send to qemu
528 * Sends a QMP message to QEMU and leaves the response in the stream.
530 void qmp_async(const char *fmt
, ...);
533 * qmp_discard_response:
534 * @fmt...: QMP message to send to qemu
536 * Sends a QMP message to QEMU and consumes the response.
538 void qmp_discard_response(const char *fmt
, ...);
543 * Reads a QMP message from QEMU and returns the response.
545 static inline QDict
*qmp_receive(void)
547 return qtest_qmp_receive(global_qtest
);
552 * @s: #event event to wait for.
554 * Continuosly polls for QMP responses until it receives the desired event.
556 static inline void qmp_eventwait(const char *event
)
558 return qtest_qmp_eventwait(global_qtest
, event
);
563 * @fmt...: HMP command to send to QEMU
565 * Send HMP command to QEMU via QMP's human-monitor-command.
567 * Returns: the command's output. The caller should g_free() it.
569 char *hmp(const char *fmt
, ...);
573 * @num: Interrupt to observe.
575 * Returns: The level of the @num interrupt.
577 static inline bool get_irq(int num
)
579 return qtest_get_irq(global_qtest
, num
);
584 * @string: QOM path of a device.
586 * Associate qtest irqs with the GPIO-in pins of the device
587 * whose path is specified by @string.
589 static inline void irq_intercept_in(const char *string
)
591 qtest_irq_intercept_in(global_qtest
, string
);
595 * qtest_irq_intercept_out:
596 * @string: QOM path of a device.
598 * Associate qtest irqs with the GPIO-out pins of the device
599 * whose path is specified by @string.
601 static inline void irq_intercept_out(const char *string
)
603 qtest_irq_intercept_out(global_qtest
, string
);
608 * @addr: I/O port to write to.
609 * @value: Value being written.
611 * Write an 8-bit value to an I/O port.
613 static inline void outb(uint16_t addr
, uint8_t value
)
615 qtest_outb(global_qtest
, addr
, value
);
620 * @addr: I/O port to write to.
621 * @value: Value being written.
623 * Write a 16-bit value to an I/O port.
625 static inline void outw(uint16_t addr
, uint16_t value
)
627 qtest_outw(global_qtest
, addr
, value
);
632 * @addr: I/O port to write to.
633 * @value: Value being written.
635 * Write a 32-bit value to an I/O port.
637 static inline void outl(uint16_t addr
, uint32_t value
)
639 qtest_outl(global_qtest
, addr
, value
);
644 * @addr: I/O port to read from.
646 * Reads an 8-bit value from an I/O port.
648 * Returns: Value read.
650 static inline uint8_t inb(uint16_t addr
)
652 return qtest_inb(global_qtest
, addr
);
657 * @addr: I/O port to read from.
659 * Reads a 16-bit value from an I/O port.
661 * Returns: Value read.
663 static inline uint16_t inw(uint16_t addr
)
665 return qtest_inw(global_qtest
, addr
);
670 * @addr: I/O port to read from.
672 * Reads a 32-bit value from an I/O port.
674 * Returns: Value read.
676 static inline uint32_t inl(uint16_t addr
)
678 return qtest_inl(global_qtest
, addr
);
683 * @addr: Guest address to write to.
684 * @value: Value being written.
686 * Writes an 8-bit value to guest memory.
688 static inline void writeb(uint64_t addr
, uint8_t value
)
690 qtest_writeb(global_qtest
, addr
, value
);
695 * @addr: Guest address to write to.
696 * @value: Value being written.
698 * Writes a 16-bit value to guest memory.
700 static inline void writew(uint64_t addr
, uint16_t value
)
702 qtest_writew(global_qtest
, addr
, value
);
707 * @addr: Guest address to write to.
708 * @value: Value being written.
710 * Writes a 32-bit value to guest memory.
712 static inline void writel(uint64_t addr
, uint32_t value
)
714 qtest_writel(global_qtest
, addr
, value
);
719 * @addr: Guest address to write to.
720 * @value: Value being written.
722 * Writes a 64-bit value to guest memory.
724 static inline void writeq(uint64_t addr
, uint64_t value
)
726 qtest_writeq(global_qtest
, addr
, value
);
731 * @addr: Guest address to read from.
733 * Reads an 8-bit value from guest memory.
735 * Returns: Value read.
737 static inline uint8_t readb(uint64_t addr
)
739 return qtest_readb(global_qtest
, addr
);
744 * @addr: Guest address to read from.
746 * Reads a 16-bit value from guest memory.
748 * Returns: Value read.
750 static inline uint16_t readw(uint64_t addr
)
752 return qtest_readw(global_qtest
, addr
);
757 * @addr: Guest address to read from.
759 * Reads a 32-bit value from guest memory.
761 * Returns: Value read.
763 static inline uint32_t readl(uint64_t addr
)
765 return qtest_readl(global_qtest
, addr
);
770 * @addr: Guest address to read from.
772 * Reads a 64-bit value from guest memory.
774 * Returns: Value read.
776 static inline uint64_t readq(uint64_t addr
)
778 return qtest_readq(global_qtest
, addr
);
783 * @addr: Guest address to read from.
784 * @data: Pointer to where memory contents will be stored.
785 * @size: Number of bytes to read.
787 * Read guest memory into a buffer.
789 static inline void memread(uint64_t addr
, void *data
, size_t size
)
791 qtest_memread(global_qtest
, addr
, data
, size
);
796 * @addr: Guest address to read from.
797 * @data: Pointer to where memory contents will be stored.
798 * @size: Number of bytes to read.
800 * Read guest memory into a buffer, receive using a base64 encoding.
802 static inline void bufread(uint64_t addr
, void *data
, size_t size
)
804 qtest_bufread(global_qtest
, addr
, data
, size
);
809 * @addr: Guest address to write to.
810 * @data: Pointer to the bytes that will be written to guest memory.
811 * @size: Number of bytes to write.
813 * Write a buffer to guest memory.
815 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
817 qtest_memwrite(global_qtest
, addr
, data
, size
);
822 * @addr: Guest address to write to.
823 * @data: Pointer to the bytes that will be written to guest memory.
824 * @size: Number of bytes to write.
826 * Write a buffer to guest memory, transmit using a base64 encoding.
828 static inline void bufwrite(uint64_t addr
, const void *data
, size_t size
)
830 qtest_bufwrite(global_qtest
, addr
, data
, size
);
835 * @addr: Guest address to write to.
836 * @patt: Byte pattern to fill the guest memory region with.
837 * @size: Number of bytes to write.
839 * Write a pattern to guest memory.
841 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
843 qtest_memset(global_qtest
, addr
, patt
, size
);
849 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
851 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
853 static inline int64_t clock_step_next(void)
855 return qtest_clock_step_next(global_qtest
);
860 * @step: Number of nanoseconds to advance the clock by.
862 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
864 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
866 static inline int64_t clock_step(int64_t step
)
868 return qtest_clock_step(global_qtest
, step
);
873 * @val: Nanoseconds value to advance the clock to.
875 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
877 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
879 static inline int64_t clock_set(int64_t val
)
881 return qtest_clock_set(global_qtest
, val
);
884 QDict
*qmp_fd_receive(int fd
);
885 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
);
886 void qmp_fd_send(int fd
, const char *fmt
, ...);
887 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
);
888 QDict
*qmp_fd(int fd
, const char *fmt
, ...);