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
);
35 * qtest_init_without_qmp_handshake:
36 * @extra_args: other arguments to pass to QEMU.
38 * Returns: #QTestState instance.
40 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
);
44 * @s: #QTestState instance to operate on.
46 * Shut down the QEMU process associated to @s.
48 void qtest_quit(QTestState
*s
);
51 * qtest_qmp_discard_response:
52 * @s: #QTestState instance to operate on.
53 * @fmt...: QMP message to send to qemu
55 * Sends a QMP message to QEMU and consumes the response.
57 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...);
61 * @s: #QTestState instance to operate on.
62 * @fmt...: QMP message to send to qemu
64 * Sends a QMP message to QEMU and returns the response.
66 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...);
70 * @s: #QTestState instance to operate on.
71 * @fmt...: QMP message to send to qemu
73 * Sends a QMP message to QEMU and leaves the response in the stream.
75 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...);
78 * qtest_qmpv_discard_response:
79 * @s: #QTestState instance to operate on.
80 * @fmt: QMP message to send to QEMU
81 * @ap: QMP message arguments
83 * Sends a QMP message to QEMU and consumes the response.
85 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
);
89 * @s: #QTestState instance to operate on.
90 * @fmt: QMP message to send to QEMU
91 * @ap: QMP message arguments
93 * Sends a QMP message to QEMU and returns the response.
95 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
99 * @s: #QTestState instance to operate on.
100 * @fmt: QMP message to send to QEMU
101 * @ap: QMP message arguments
103 * Sends a QMP message to QEMU and leaves the response in the stream.
105 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
109 * @s: #QTestState instance to operate on.
111 * Reads a QMP message from QEMU and returns the response.
113 QDict
*qtest_qmp_receive(QTestState
*s
);
116 * qtest_qmp_eventwait:
117 * @s: #QTestState instance to operate on.
118 * @s: #event event to wait for.
120 * Continuosly polls for QMP responses until it receives the desired event.
122 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
125 * qtest_qmp_eventwait_ref:
126 * @s: #QTestState instance to operate on.
127 * @s: #event event to wait for.
129 * Continuosly polls for QMP responses until it receives the desired event.
130 * Returns a copy of the event for further investigation.
132 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
);
136 * @s: #QTestState instance to operate on.
137 * @fmt...: HMP command to send to QEMU
139 * Send HMP command to QEMU via QMP's human-monitor-command.
140 * QMP events are discarded.
142 * Returns: the command's output. The caller should g_free() it.
144 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...);
148 * @s: #QTestState instance to operate on.
149 * @fmt: HMP command to send to QEMU
150 * @ap: HMP command arguments
152 * Send HMP command to QEMU via QMP's human-monitor-command.
153 * QMP events are discarded.
155 * Returns: the command's output. The caller should g_free() it.
157 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
);
161 * @s: #QTestState instance to operate on.
162 * @num: Interrupt to observe.
164 * Returns: The level of the @num interrupt.
166 bool qtest_get_irq(QTestState
*s
, int num
);
169 * qtest_irq_intercept_in:
170 * @s: #QTestState instance to operate on.
171 * @string: QOM path of a device.
173 * Associate qtest irqs with the GPIO-in pins of the device
174 * whose path is specified by @string.
176 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
179 * qtest_irq_intercept_out:
180 * @s: #QTestState instance to operate on.
181 * @string: QOM path of a device.
183 * Associate qtest irqs with the GPIO-out pins of the device
184 * whose path is specified by @string.
186 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
190 * @s: #QTestState instance to operate on.
191 * @addr: I/O port to write to.
192 * @value: Value being written.
194 * Write an 8-bit value to an I/O port.
196 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
200 * @s: #QTestState instance to operate on.
201 * @addr: I/O port to write to.
202 * @value: Value being written.
204 * Write a 16-bit value to an I/O port.
206 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
210 * @s: #QTestState instance to operate on.
211 * @addr: I/O port to write to.
212 * @value: Value being written.
214 * Write a 32-bit value to an I/O port.
216 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
220 * @s: #QTestState instance to operate on.
221 * @addr: I/O port to read from.
223 * Returns an 8-bit value from an I/O port.
225 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
229 * @s: #QTestState instance to operate on.
230 * @addr: I/O port to read from.
232 * Returns a 16-bit value from an I/O port.
234 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
238 * @s: #QTestState instance to operate on.
239 * @addr: I/O port to read from.
241 * Returns a 32-bit value from an I/O port.
243 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
247 * @s: #QTestState instance to operate on.
248 * @addr: Guest address to write to.
249 * @value: Value being written.
251 * Writes an 8-bit value to memory.
253 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
257 * @s: #QTestState instance to operate on.
258 * @addr: Guest address to write to.
259 * @value: Value being written.
261 * Writes a 16-bit value to memory.
263 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
267 * @s: #QTestState instance to operate on.
268 * @addr: Guest address to write to.
269 * @value: Value being written.
271 * Writes a 32-bit value to memory.
273 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
277 * @s: #QTestState instance to operate on.
278 * @addr: Guest address to write to.
279 * @value: Value being written.
281 * Writes a 64-bit value to memory.
283 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
287 * @s: #QTestState instance to operate on.
288 * @addr: Guest address to read from.
290 * Reads an 8-bit value from memory.
292 * Returns: Value read.
294 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
298 * @s: #QTestState instance to operate on.
299 * @addr: Guest address to read from.
301 * Reads a 16-bit value from memory.
303 * Returns: Value read.
305 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
309 * @s: #QTestState instance to operate on.
310 * @addr: Guest address to read from.
312 * Reads a 32-bit value from memory.
314 * Returns: Value read.
316 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
320 * @s: #QTestState instance to operate on.
321 * @addr: Guest address to read from.
323 * Reads a 64-bit value from memory.
325 * Returns: Value read.
327 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
331 * @s: #QTestState instance to operate on.
332 * @addr: Guest address to read from.
333 * @data: Pointer to where memory contents will be stored.
334 * @size: Number of bytes to read.
336 * Read guest memory into a buffer.
338 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
342 * @s: #QTestState instance to operate on.
343 * @name: name of the command to call.
344 * @nargs: Number of args.
345 * @args: Guest address to read args from.
346 * @nret: Number of return value.
347 * @ret: Guest address to write return values to.
349 * Call an RTAS function
351 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
352 uint32_t nargs
, uint64_t args
,
353 uint32_t nret
, uint64_t ret
);
357 * @s: #QTestState instance to operate on.
358 * @addr: Guest address to read from.
359 * @data: Pointer to where memory contents will be stored.
360 * @size: Number of bytes to read.
362 * Read guest memory into a buffer and receive using a base64 encoding.
364 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
368 * @s: #QTestState instance to operate on.
369 * @addr: Guest address to write to.
370 * @data: Pointer to the bytes that will be written to guest memory.
371 * @size: Number of bytes to write.
373 * Write a buffer to guest memory.
375 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
379 * @s: #QTestState instance to operate on.
380 * @addr: Guest address to write to.
381 * @data: Pointer to the bytes that will be written to guest memory.
382 * @size: Number of bytes to write.
384 * Write a buffer to guest memory and transmit using a base64 encoding.
386 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
387 const void *data
, size_t size
);
391 * @s: #QTestState instance to operate on.
392 * @addr: Guest address to write to.
393 * @patt: Byte pattern to fill the guest memory region with.
394 * @size: Number of bytes to write.
396 * Write a pattern to guest memory.
398 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
401 * qtest_clock_step_next:
402 * @s: #QTestState instance to operate on.
404 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
406 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
408 int64_t qtest_clock_step_next(QTestState
*s
);
412 * @s: QTestState instance to operate on.
413 * @step: Number of nanoseconds to advance the clock by.
415 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
417 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
419 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
423 * @s: QTestState instance to operate on.
424 * @val: Nanoseconds value to advance the clock to.
426 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
428 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
430 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
434 * @s: QTestState instance to operate on.
436 * Returns: True if the architecture under test has a big endian configuration.
438 bool qtest_big_endian(QTestState
*s
);
443 * Returns: The architecture for the QEMU executable under test.
445 const char *qtest_get_arch(void);
449 * @str: Test case path.
450 * @fn: Test case function
452 * Add a GTester testcase with the given name and function.
453 * The path is prefixed with the architecture under test, as
454 * returned by qtest_get_arch().
456 void qtest_add_func(const char *str
, void (*fn
)(void));
459 * qtest_add_data_func:
460 * @str: Test case path.
461 * @data: Test case data
462 * @fn: Test case function
464 * Add a GTester testcase with the given name, data and function.
465 * The path is prefixed with the architecture under test, as
466 * returned by qtest_get_arch().
468 void qtest_add_data_func(const char *str
, const void *data
,
469 void (*fn
)(const void *));
472 * qtest_add_data_func_full:
473 * @str: Test case path.
474 * @data: Test case data
475 * @fn: Test case function
476 * @data_free_func: GDestroyNotify for data
478 * Add a GTester testcase with the given name, data and function.
479 * The path is prefixed with the architecture under test, as
480 * returned by qtest_get_arch().
482 * @data is passed to @data_free_func() on test completion.
484 void qtest_add_data_func_full(const char *str
, void *data
,
485 void (*fn
)(const void *),
486 GDestroyNotify data_free_func
);
490 * @testpath: Test case path
491 * @Fixture: Fixture type
492 * @tdata: Test case data
493 * @fsetup: Test case setup function
494 * @ftest: Test case function
495 * @fteardown: Test case teardown function
497 * Add a GTester testcase with the given name, data and functions.
498 * The path is prefixed with the architecture under test, as
499 * returned by qtest_get_arch().
501 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
503 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
504 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
508 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
512 * @args: other arguments to pass to QEMU
514 * Start QEMU and assign the resulting #QTestState to a global variable.
515 * The global variable is used by "shortcut" functions documented below.
517 * Returns: #QTestState instance.
519 static inline QTestState
*qtest_start(const char *args
)
521 global_qtest
= qtest_init(args
);
528 * Shut down the QEMU process started by qtest_start().
530 static inline void qtest_end(void)
532 qtest_quit(global_qtest
);
538 * @fmt...: QMP message to send to qemu
540 * Sends a QMP message to QEMU and returns the response.
542 QDict
*qmp(const char *fmt
, ...);
546 * @fmt...: QMP message to send to qemu
548 * Sends a QMP message to QEMU and leaves the response in the stream.
550 void qmp_async(const char *fmt
, ...);
553 * qmp_discard_response:
554 * @fmt...: QMP message to send to qemu
556 * Sends a QMP message to QEMU and consumes the response.
558 void qmp_discard_response(const char *fmt
, ...);
563 * Reads a QMP message from QEMU and returns the response.
565 static inline QDict
*qmp_receive(void)
567 return qtest_qmp_receive(global_qtest
);
572 * @s: #event event to wait for.
574 * Continuosly polls for QMP responses until it receives the desired event.
576 static inline void qmp_eventwait(const char *event
)
578 return qtest_qmp_eventwait(global_qtest
, event
);
583 * @s: #event event to wait for.
585 * Continuosly polls for QMP responses until it receives the desired event.
586 * Returns a copy of the event for further investigation.
588 static inline QDict
*qmp_eventwait_ref(const char *event
)
590 return qtest_qmp_eventwait_ref(global_qtest
, event
);
595 * @fmt...: HMP command to send to QEMU
597 * Send HMP command to QEMU via QMP's human-monitor-command.
599 * Returns: the command's output. The caller should g_free() it.
601 char *hmp(const char *fmt
, ...);
605 * @num: Interrupt to observe.
607 * Returns: The level of the @num interrupt.
609 static inline bool get_irq(int num
)
611 return qtest_get_irq(global_qtest
, num
);
616 * @string: QOM path of a device.
618 * Associate qtest irqs with the GPIO-in pins of the device
619 * whose path is specified by @string.
621 static inline void irq_intercept_in(const char *string
)
623 qtest_irq_intercept_in(global_qtest
, string
);
627 * qtest_irq_intercept_out:
628 * @string: QOM path of a device.
630 * Associate qtest irqs with the GPIO-out pins of the device
631 * whose path is specified by @string.
633 static inline void irq_intercept_out(const char *string
)
635 qtest_irq_intercept_out(global_qtest
, string
);
640 * @addr: I/O port to write to.
641 * @value: Value being written.
643 * Write an 8-bit value to an I/O port.
645 static inline void outb(uint16_t addr
, uint8_t value
)
647 qtest_outb(global_qtest
, addr
, value
);
652 * @addr: I/O port to write to.
653 * @value: Value being written.
655 * Write a 16-bit value to an I/O port.
657 static inline void outw(uint16_t addr
, uint16_t value
)
659 qtest_outw(global_qtest
, addr
, value
);
664 * @addr: I/O port to write to.
665 * @value: Value being written.
667 * Write a 32-bit value to an I/O port.
669 static inline void outl(uint16_t addr
, uint32_t value
)
671 qtest_outl(global_qtest
, addr
, value
);
676 * @addr: I/O port to read from.
678 * Reads an 8-bit value from an I/O port.
680 * Returns: Value read.
682 static inline uint8_t inb(uint16_t addr
)
684 return qtest_inb(global_qtest
, addr
);
689 * @addr: I/O port to read from.
691 * Reads a 16-bit value from an I/O port.
693 * Returns: Value read.
695 static inline uint16_t inw(uint16_t addr
)
697 return qtest_inw(global_qtest
, addr
);
702 * @addr: I/O port to read from.
704 * Reads a 32-bit value from an I/O port.
706 * Returns: Value read.
708 static inline uint32_t inl(uint16_t addr
)
710 return qtest_inl(global_qtest
, addr
);
715 * @addr: Guest address to write to.
716 * @value: Value being written.
718 * Writes an 8-bit value to guest memory.
720 static inline void writeb(uint64_t addr
, uint8_t value
)
722 qtest_writeb(global_qtest
, addr
, value
);
727 * @addr: Guest address to write to.
728 * @value: Value being written.
730 * Writes a 16-bit value to guest memory.
732 static inline void writew(uint64_t addr
, uint16_t value
)
734 qtest_writew(global_qtest
, addr
, value
);
739 * @addr: Guest address to write to.
740 * @value: Value being written.
742 * Writes a 32-bit value to guest memory.
744 static inline void writel(uint64_t addr
, uint32_t value
)
746 qtest_writel(global_qtest
, addr
, value
);
751 * @addr: Guest address to write to.
752 * @value: Value being written.
754 * Writes a 64-bit value to guest memory.
756 static inline void writeq(uint64_t addr
, uint64_t value
)
758 qtest_writeq(global_qtest
, addr
, value
);
763 * @addr: Guest address to read from.
765 * Reads an 8-bit value from guest memory.
767 * Returns: Value read.
769 static inline uint8_t readb(uint64_t addr
)
771 return qtest_readb(global_qtest
, addr
);
776 * @addr: Guest address to read from.
778 * Reads a 16-bit value from guest memory.
780 * Returns: Value read.
782 static inline uint16_t readw(uint64_t addr
)
784 return qtest_readw(global_qtest
, addr
);
789 * @addr: Guest address to read from.
791 * Reads a 32-bit value from guest memory.
793 * Returns: Value read.
795 static inline uint32_t readl(uint64_t addr
)
797 return qtest_readl(global_qtest
, addr
);
802 * @addr: Guest address to read from.
804 * Reads a 64-bit value from guest memory.
806 * Returns: Value read.
808 static inline uint64_t readq(uint64_t addr
)
810 return qtest_readq(global_qtest
, addr
);
815 * @addr: Guest address to read from.
816 * @data: Pointer to where memory contents will be stored.
817 * @size: Number of bytes to read.
819 * Read guest memory into a buffer.
821 static inline void memread(uint64_t addr
, void *data
, size_t size
)
823 qtest_memread(global_qtest
, addr
, data
, size
);
828 * @addr: Guest address to read from.
829 * @data: Pointer to where memory contents will be stored.
830 * @size: Number of bytes to read.
832 * Read guest memory into a buffer, receive using a base64 encoding.
834 static inline void bufread(uint64_t addr
, void *data
, size_t size
)
836 qtest_bufread(global_qtest
, addr
, data
, size
);
841 * @addr: Guest address to write to.
842 * @data: Pointer to the bytes that will be written to guest memory.
843 * @size: Number of bytes to write.
845 * Write a buffer to guest memory.
847 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
849 qtest_memwrite(global_qtest
, addr
, data
, size
);
854 * @addr: Guest address to write to.
855 * @data: Pointer to the bytes that will be written to guest memory.
856 * @size: Number of bytes to write.
858 * Write a buffer to guest memory, transmit using a base64 encoding.
860 static inline void bufwrite(uint64_t addr
, const void *data
, size_t size
)
862 qtest_bufwrite(global_qtest
, addr
, data
, size
);
867 * @addr: Guest address to write to.
868 * @patt: Byte pattern to fill the guest memory region with.
869 * @size: Number of bytes to write.
871 * Write a pattern to guest memory.
873 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
875 qtest_memset(global_qtest
, addr
, patt
, size
);
881 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
883 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
885 static inline int64_t clock_step_next(void)
887 return qtest_clock_step_next(global_qtest
);
892 * @step: Number of nanoseconds to advance the clock by.
894 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
896 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
898 static inline int64_t clock_step(int64_t step
)
900 return qtest_clock_step(global_qtest
, step
);
905 * @val: Nanoseconds value to advance the clock to.
907 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
909 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
911 static inline int64_t clock_set(int64_t val
)
913 return qtest_clock_set(global_qtest
, val
);
916 QDict
*qmp_fd_receive(int fd
);
917 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
);
918 void qmp_fd_send(int fd
, const char *fmt
, ...);
919 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
);
920 QDict
*qmp_fd(int fd
, const char *fmt
, ...);
923 * qtest_cb_for_every_machine:
924 * @cb: Pointer to the callback function
926 * Call a callback function for every name of all available machines.
928 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
));