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 typedef struct QTestState QTestState
;
22 extern QTestState
*global_qtest
;
26 * @fmt...: Format for creating other arguments to pass to QEMU, formatted
29 * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
30 * #global_qtest is left at NULL).
32 * Returns: #QTestState instance.
34 QTestState
*qtest_startf(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
38 * @fmt: Format for creating other arguments to pass to QEMU, formatted
40 * @ap: Format arguments.
42 * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
43 * #global_qtest is left at NULL).
45 * Returns: #QTestState instance.
47 QTestState
*qtest_vstartf(const char *fmt
, va_list ap
) GCC_FMT_ATTR(1, 0);
51 * @extra_args: other arguments to pass to QEMU.
53 * Returns: #QTestState instance.
55 QTestState
*qtest_init(const char *extra_args
);
58 * qtest_init_without_qmp_handshake:
59 * @use_oob: true to have the server advertise OOB support
60 * @extra_args: other arguments to pass to QEMU. CAUTION: these
61 * arguments are subject to word splitting and shell evaluation.
63 * Returns: #QTestState instance.
65 QTestState
*qtest_init_without_qmp_handshake(bool use_oob
,
66 const char *extra_args
);
70 * @s: #QTestState instance to operate on.
72 * Shut down the QEMU process associated to @s.
74 void qtest_quit(QTestState
*s
);
77 * qtest_qmp_discard_response:
78 * @s: #QTestState instance to operate on.
79 * @fmt...: QMP message to send to qemu
81 * Sends a QMP message to QEMU and consumes the response.
83 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...);
87 * @s: #QTestState instance to operate on.
88 * @fmt...: QMP message to send to qemu
90 * Sends a QMP message to QEMU and returns the response.
92 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...);
96 * @s: #QTestState instance to operate on.
97 * @fmt...: QMP message to send to qemu
99 * Sends a QMP message to QEMU and leaves the response in the stream.
101 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...);
104 * qtest_qmpv_discard_response:
105 * @s: #QTestState instance to operate on.
106 * @fmt: QMP message to send to QEMU
107 * @ap: QMP message arguments
109 * Sends a QMP message to QEMU and consumes the response.
111 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
);
115 * @s: #QTestState instance to operate on.
116 * @fmt: QMP message to send to QEMU
117 * @ap: QMP message arguments
119 * Sends a QMP message to QEMU and returns the response.
121 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
125 * @s: #QTestState instance to operate on.
126 * @fmt: QMP message to send to QEMU
127 * @ap: QMP message arguments
129 * Sends a QMP message to QEMU and leaves the response in the stream.
131 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
135 * @s: #QTestState instance to operate on.
137 * Reads a QMP message from QEMU and returns the response.
139 QDict
*qtest_qmp_receive(QTestState
*s
);
142 * qtest_qmp_eventwait:
143 * @s: #QTestState instance to operate on.
144 * @s: #event event to wait for.
146 * Continuously polls for QMP responses until it receives the desired event.
148 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
151 * qtest_qmp_eventwait_ref:
152 * @s: #QTestState instance to operate on.
153 * @s: #event event to wait for.
155 * Continuously polls for QMP responses until it receives the desired event.
156 * Returns a copy of the event for further investigation.
158 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
);
162 * @s: #QTestState instance to operate on.
163 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
165 * Send HMP command to QEMU via QMP's human-monitor-command.
166 * QMP events are discarded.
168 * Returns: the command's output. The caller should g_free() it.
170 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
174 * @s: #QTestState instance to operate on.
175 * @fmt: HMP command to send to QEMU
176 * @ap: HMP command arguments
178 * Send HMP command to QEMU via QMP's human-monitor-command.
179 * QMP events are discarded.
181 * Returns: the command's output. The caller should g_free() it.
183 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
);
187 * @s: #QTestState instance to operate on.
188 * @num: Interrupt to observe.
190 * Returns: The level of the @num interrupt.
192 bool qtest_get_irq(QTestState
*s
, int num
);
195 * qtest_irq_intercept_in:
196 * @s: #QTestState instance to operate on.
197 * @string: QOM path of a device.
199 * Associate qtest irqs with the GPIO-in pins of the device
200 * whose path is specified by @string.
202 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
205 * qtest_irq_intercept_out:
206 * @s: #QTestState instance to operate on.
207 * @string: QOM path of a device.
209 * Associate qtest irqs with the GPIO-out pins of the device
210 * whose path is specified by @string.
212 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
216 * @s: #QTestState instance to operate on.
217 * @addr: I/O port to write to.
218 * @value: Value being written.
220 * Write an 8-bit value to an I/O port.
222 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
226 * @s: #QTestState instance to operate on.
227 * @addr: I/O port to write to.
228 * @value: Value being written.
230 * Write a 16-bit value to an I/O port.
232 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
236 * @s: #QTestState instance to operate on.
237 * @addr: I/O port to write to.
238 * @value: Value being written.
240 * Write a 32-bit value to an I/O port.
242 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
246 * @s: #QTestState instance to operate on.
247 * @addr: I/O port to read from.
249 * Returns an 8-bit value from an I/O port.
251 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
255 * @s: #QTestState instance to operate on.
256 * @addr: I/O port to read from.
258 * Returns a 16-bit value from an I/O port.
260 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
264 * @s: #QTestState instance to operate on.
265 * @addr: I/O port to read from.
267 * Returns a 32-bit value from an I/O port.
269 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
273 * @s: #QTestState instance to operate on.
274 * @addr: Guest address to write to.
275 * @value: Value being written.
277 * Writes an 8-bit value to memory.
279 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
283 * @s: #QTestState instance to operate on.
284 * @addr: Guest address to write to.
285 * @value: Value being written.
287 * Writes a 16-bit value to memory.
289 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
293 * @s: #QTestState instance to operate on.
294 * @addr: Guest address to write to.
295 * @value: Value being written.
297 * Writes a 32-bit value to memory.
299 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
303 * @s: #QTestState instance to operate on.
304 * @addr: Guest address to write to.
305 * @value: Value being written.
307 * Writes a 64-bit value to memory.
309 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
313 * @s: #QTestState instance to operate on.
314 * @addr: Guest address to read from.
316 * Reads an 8-bit value from memory.
318 * Returns: Value read.
320 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
324 * @s: #QTestState instance to operate on.
325 * @addr: Guest address to read from.
327 * Reads a 16-bit value from memory.
329 * Returns: Value read.
331 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
335 * @s: #QTestState instance to operate on.
336 * @addr: Guest address to read from.
338 * Reads a 32-bit value from memory.
340 * Returns: Value read.
342 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
346 * @s: #QTestState instance to operate on.
347 * @addr: Guest address to read from.
349 * Reads a 64-bit value from memory.
351 * Returns: Value read.
353 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
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.
364 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
368 * @s: #QTestState instance to operate on.
369 * @name: name of the command to call.
370 * @nargs: Number of args.
371 * @args: Guest address to read args from.
372 * @nret: Number of return value.
373 * @ret: Guest address to write return values to.
375 * Call an RTAS function
377 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
378 uint32_t nargs
, uint64_t args
,
379 uint32_t nret
, uint64_t ret
);
383 * @s: #QTestState instance to operate on.
384 * @addr: Guest address to read from.
385 * @data: Pointer to where memory contents will be stored.
386 * @size: Number of bytes to read.
388 * Read guest memory into a buffer and receive using a base64 encoding.
390 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
394 * @s: #QTestState instance to operate on.
395 * @addr: Guest address to write to.
396 * @data: Pointer to the bytes that will be written to guest memory.
397 * @size: Number of bytes to write.
399 * Write a buffer to guest memory.
401 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
405 * @s: #QTestState instance to operate on.
406 * @addr: Guest address to write to.
407 * @data: Pointer to the bytes that will be written to guest memory.
408 * @size: Number of bytes to write.
410 * Write a buffer to guest memory and transmit using a base64 encoding.
412 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
413 const void *data
, size_t size
);
417 * @s: #QTestState instance to operate on.
418 * @addr: Guest address to write to.
419 * @patt: Byte pattern to fill the guest memory region with.
420 * @size: Number of bytes to write.
422 * Write a pattern to guest memory.
424 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
427 * qtest_clock_step_next:
428 * @s: #QTestState instance to operate on.
430 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
432 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
434 int64_t qtest_clock_step_next(QTestState
*s
);
438 * @s: QTestState instance to operate on.
439 * @step: Number of nanoseconds to advance the clock by.
441 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
443 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
445 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
449 * @s: QTestState instance to operate on.
450 * @val: Nanoseconds value to advance the clock to.
452 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
454 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
456 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
460 * @s: QTestState instance to operate on.
462 * Returns: True if the architecture under test has a big endian configuration.
464 bool qtest_big_endian(QTestState
*s
);
469 * Returns: The architecture for the QEMU executable under test.
471 const char *qtest_get_arch(void);
475 * @str: Test case path.
476 * @fn: Test case function
478 * Add a GTester testcase with the given name and function.
479 * The path is prefixed with the architecture under test, as
480 * returned by qtest_get_arch().
482 void qtest_add_func(const char *str
, void (*fn
)(void));
485 * qtest_add_data_func:
486 * @str: Test case path.
487 * @data: Test case data
488 * @fn: Test case function
490 * Add a GTester testcase with the given name, data and function.
491 * The path is prefixed with the architecture under test, as
492 * returned by qtest_get_arch().
494 void qtest_add_data_func(const char *str
, const void *data
,
495 void (*fn
)(const void *));
498 * qtest_add_data_func_full:
499 * @str: Test case path.
500 * @data: Test case data
501 * @fn: Test case function
502 * @data_free_func: GDestroyNotify for data
504 * Add a GTester testcase with the given name, data and function.
505 * The path is prefixed with the architecture under test, as
506 * returned by qtest_get_arch().
508 * @data is passed to @data_free_func() on test completion.
510 void qtest_add_data_func_full(const char *str
, void *data
,
511 void (*fn
)(const void *),
512 GDestroyNotify data_free_func
);
516 * @testpath: Test case path
517 * @Fixture: Fixture type
518 * @tdata: Test case data
519 * @fsetup: Test case setup function
520 * @ftest: Test case function
521 * @fteardown: Test case teardown function
523 * Add a GTester testcase with the given name, data and functions.
524 * The path is prefixed with the architecture under test, as
525 * returned by qtest_get_arch().
527 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
529 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
530 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
534 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
538 * @args: other arguments to pass to QEMU
540 * Start QEMU and assign the resulting #QTestState to a global variable.
541 * The global variable is used by "shortcut" functions documented below.
543 * Returns: #QTestState instance.
545 static inline QTestState
*qtest_start(const char *args
)
547 global_qtest
= qtest_init(args
);
554 * Shut down the QEMU process started by qtest_start().
556 static inline void qtest_end(void)
558 qtest_quit(global_qtest
);
564 * @fmt...: QMP message to send to qemu
566 * Sends a QMP message to QEMU and returns the response.
568 QDict
*qmp(const char *fmt
, ...);
572 * @fmt...: QMP message to send to qemu
574 * Sends a QMP message to QEMU and leaves the response in the stream.
576 void qmp_async(const char *fmt
, ...);
579 * qmp_discard_response:
580 * @fmt...: QMP message to send to qemu
582 * Sends a QMP message to QEMU and consumes the response.
584 void qmp_discard_response(const char *fmt
, ...);
589 * Reads a QMP message from QEMU and returns the response.
591 static inline QDict
*qmp_receive(void)
593 return qtest_qmp_receive(global_qtest
);
598 * @s: #event event to wait for.
600 * Continuously polls for QMP responses until it receives the desired event.
602 static inline void qmp_eventwait(const char *event
)
604 return qtest_qmp_eventwait(global_qtest
, event
);
609 * @s: #event event to wait for.
611 * Continuously polls for QMP responses until it receives the desired event.
612 * Returns a copy of the event for further investigation.
614 static inline QDict
*qmp_eventwait_ref(const char *event
)
616 return qtest_qmp_eventwait_ref(global_qtest
, event
);
621 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
623 * Send HMP command to QEMU via QMP's human-monitor-command.
625 * Returns: the command's output. The caller should g_free() it.
627 char *hmp(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
631 * @num: Interrupt to observe.
633 * Returns: The level of the @num interrupt.
635 static inline bool get_irq(int num
)
637 return qtest_get_irq(global_qtest
, num
);
642 * @string: QOM path of a device.
644 * Associate qtest irqs with the GPIO-in pins of the device
645 * whose path is specified by @string.
647 static inline void irq_intercept_in(const char *string
)
649 qtest_irq_intercept_in(global_qtest
, string
);
653 * qtest_irq_intercept_out:
654 * @string: QOM path of a device.
656 * Associate qtest irqs with the GPIO-out pins of the device
657 * whose path is specified by @string.
659 static inline void irq_intercept_out(const char *string
)
661 qtest_irq_intercept_out(global_qtest
, string
);
666 * @addr: I/O port to write to.
667 * @value: Value being written.
669 * Write an 8-bit value to an I/O port.
671 static inline void outb(uint16_t addr
, uint8_t value
)
673 qtest_outb(global_qtest
, addr
, value
);
678 * @addr: I/O port to write to.
679 * @value: Value being written.
681 * Write a 16-bit value to an I/O port.
683 static inline void outw(uint16_t addr
, uint16_t value
)
685 qtest_outw(global_qtest
, addr
, value
);
690 * @addr: I/O port to write to.
691 * @value: Value being written.
693 * Write a 32-bit value to an I/O port.
695 static inline void outl(uint16_t addr
, uint32_t value
)
697 qtest_outl(global_qtest
, addr
, value
);
702 * @addr: I/O port to read from.
704 * Reads an 8-bit value from an I/O port.
706 * Returns: Value read.
708 static inline uint8_t inb(uint16_t addr
)
710 return qtest_inb(global_qtest
, addr
);
715 * @addr: I/O port to read from.
717 * Reads a 16-bit value from an I/O port.
719 * Returns: Value read.
721 static inline uint16_t inw(uint16_t addr
)
723 return qtest_inw(global_qtest
, addr
);
728 * @addr: I/O port to read from.
730 * Reads a 32-bit value from an I/O port.
732 * Returns: Value read.
734 static inline uint32_t inl(uint16_t addr
)
736 return qtest_inl(global_qtest
, addr
);
741 * @addr: Guest address to write to.
742 * @value: Value being written.
744 * Writes an 8-bit value to guest memory.
746 static inline void writeb(uint64_t addr
, uint8_t value
)
748 qtest_writeb(global_qtest
, addr
, value
);
753 * @addr: Guest address to write to.
754 * @value: Value being written.
756 * Writes a 16-bit value to guest memory.
758 static inline void writew(uint64_t addr
, uint16_t value
)
760 qtest_writew(global_qtest
, addr
, value
);
765 * @addr: Guest address to write to.
766 * @value: Value being written.
768 * Writes a 32-bit value to guest memory.
770 static inline void writel(uint64_t addr
, uint32_t value
)
772 qtest_writel(global_qtest
, addr
, value
);
777 * @addr: Guest address to write to.
778 * @value: Value being written.
780 * Writes a 64-bit value to guest memory.
782 static inline void writeq(uint64_t addr
, uint64_t value
)
784 qtest_writeq(global_qtest
, addr
, value
);
789 * @addr: Guest address to read from.
791 * Reads an 8-bit value from guest memory.
793 * Returns: Value read.
795 static inline uint8_t readb(uint64_t addr
)
797 return qtest_readb(global_qtest
, addr
);
802 * @addr: Guest address to read from.
804 * Reads a 16-bit value from guest memory.
806 * Returns: Value read.
808 static inline uint16_t readw(uint64_t addr
)
810 return qtest_readw(global_qtest
, addr
);
815 * @addr: Guest address to read from.
817 * Reads a 32-bit value from guest memory.
819 * Returns: Value read.
821 static inline uint32_t readl(uint64_t addr
)
823 return qtest_readl(global_qtest
, addr
);
828 * @addr: Guest address to read from.
830 * Reads a 64-bit value from guest memory.
832 * Returns: Value read.
834 static inline uint64_t readq(uint64_t addr
)
836 return qtest_readq(global_qtest
, addr
);
841 * @addr: Guest address to read from.
842 * @data: Pointer to where memory contents will be stored.
843 * @size: Number of bytes to read.
845 * Read guest memory into a buffer.
847 static inline void memread(uint64_t addr
, void *data
, size_t size
)
849 qtest_memread(global_qtest
, addr
, data
, size
);
854 * @addr: Guest address to read from.
855 * @data: Pointer to where memory contents will be stored.
856 * @size: Number of bytes to read.
858 * Read guest memory into a buffer, receive using a base64 encoding.
860 static inline void bufread(uint64_t addr
, void *data
, size_t size
)
862 qtest_bufread(global_qtest
, addr
, data
, size
);
867 * @addr: Guest address to write to.
868 * @data: Pointer to the bytes that will be written to guest memory.
869 * @size: Number of bytes to write.
871 * Write a buffer to guest memory.
873 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
875 qtest_memwrite(global_qtest
, addr
, data
, size
);
880 * @addr: Guest address to write to.
881 * @data: Pointer to the bytes that will be written to guest memory.
882 * @size: Number of bytes to write.
884 * Write a buffer to guest memory, transmit using a base64 encoding.
886 static inline void bufwrite(uint64_t addr
, const void *data
, size_t size
)
888 qtest_bufwrite(global_qtest
, addr
, data
, size
);
893 * @addr: Guest address to write to.
894 * @patt: Byte pattern to fill the guest memory region with.
895 * @size: Number of bytes to write.
897 * Write a pattern to guest memory.
899 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
901 qtest_memset(global_qtest
, addr
, patt
, size
);
907 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
909 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
911 static inline int64_t clock_step_next(void)
913 return qtest_clock_step_next(global_qtest
);
918 * @step: Number of nanoseconds to advance the clock by.
920 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
922 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
924 static inline int64_t clock_step(int64_t step
)
926 return qtest_clock_step(global_qtest
, step
);
931 * @val: Nanoseconds value to advance the clock to.
933 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
935 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
937 static inline int64_t clock_set(int64_t val
)
939 return qtest_clock_set(global_qtest
, val
);
942 QDict
*qmp_fd_receive(int fd
);
943 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
);
944 void qmp_fd_send(int fd
, const char *fmt
, ...);
945 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
);
946 QDict
*qmp_fd(int fd
, const char *fmt
, ...);
949 * qtest_cb_for_every_machine:
950 * @cb: Pointer to the callback function
952 * Call a callback function for every name of all available machines.
954 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
));
957 * qtest_qmp_device_add:
958 * @driver: Name of the device that should be added
959 * @id: Identification string
960 * @fmt: printf-like format string for further options to device_add
962 * Generic hot-plugging test via the device_add QMP command.
964 void qtest_qmp_device_add(const char *driver
, const char *id
, const char *fmt
,
965 ...) GCC_FMT_ATTR(3, 4);
968 * qtest_qmp_device_del:
969 * @id: Identification string
971 * Generic hot-unplugging test via the device_del QMP command.
973 void qtest_qmp_device_del(const char *id
);
977 * @rsp: QMP response to check for error
979 * Test @rsp for error and discard @rsp.
980 * Returns 'true' if there is error in @rsp and 'false' otherwise.
982 bool qmp_rsp_is_err(QDict
*rsp
);