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 * @extra_args: other arguments to pass to QEMU.
61 * Returns: #QTestState instance.
63 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
);
67 * @s: #QTestState instance to operate on.
69 * Shut down the QEMU process associated to @s.
71 void qtest_quit(QTestState
*s
);
74 * qtest_qmp_discard_response:
75 * @s: #QTestState instance to operate on.
76 * @fmt...: QMP message to send to qemu
78 * Sends a QMP message to QEMU and consumes the response.
80 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...);
84 * @s: #QTestState instance to operate on.
85 * @fmt...: QMP message to send to qemu
87 * Sends a QMP message to QEMU and returns the response.
89 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...);
93 * @s: #QTestState instance to operate on.
94 * @fmt...: QMP message to send to qemu
96 * Sends a QMP message to QEMU and leaves the response in the stream.
98 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...);
101 * qtest_qmpv_discard_response:
102 * @s: #QTestState instance to operate on.
103 * @fmt: QMP message to send to QEMU
104 * @ap: QMP message arguments
106 * Sends a QMP message to QEMU and consumes the response.
108 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
);
112 * @s: #QTestState instance to operate on.
113 * @fmt: QMP message to send to QEMU
114 * @ap: QMP message arguments
116 * Sends a QMP message to QEMU and returns the response.
118 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
122 * @s: #QTestState instance to operate on.
123 * @fmt: QMP message to send to QEMU
124 * @ap: QMP message arguments
126 * Sends a QMP message to QEMU and leaves the response in the stream.
128 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
132 * @s: #QTestState instance to operate on.
134 * Reads a QMP message from QEMU and returns the response.
136 QDict
*qtest_qmp_receive(QTestState
*s
);
139 * qtest_qmp_eventwait:
140 * @s: #QTestState instance to operate on.
141 * @s: #event event to wait for.
143 * Continuously polls for QMP responses until it receives the desired event.
145 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
148 * qtest_qmp_eventwait_ref:
149 * @s: #QTestState instance to operate on.
150 * @s: #event event to wait for.
152 * Continuously polls for QMP responses until it receives the desired event.
153 * Returns a copy of the event for further investigation.
155 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
);
159 * @s: #QTestState instance to operate on.
160 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
162 * Send HMP command to QEMU via QMP's human-monitor-command.
163 * QMP events are discarded.
165 * Returns: the command's output. The caller should g_free() it.
167 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
171 * @s: #QTestState instance to operate on.
172 * @fmt: HMP command to send to QEMU
173 * @ap: HMP command arguments
175 * Send HMP command to QEMU via QMP's human-monitor-command.
176 * QMP events are discarded.
178 * Returns: the command's output. The caller should g_free() it.
180 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
);
184 * @s: #QTestState instance to operate on.
185 * @num: Interrupt to observe.
187 * Returns: The level of the @num interrupt.
189 bool qtest_get_irq(QTestState
*s
, int num
);
192 * qtest_irq_intercept_in:
193 * @s: #QTestState instance to operate on.
194 * @string: QOM path of a device.
196 * Associate qtest irqs with the GPIO-in pins of the device
197 * whose path is specified by @string.
199 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
202 * qtest_irq_intercept_out:
203 * @s: #QTestState instance to operate on.
204 * @string: QOM path of a device.
206 * Associate qtest irqs with the GPIO-out pins of the device
207 * whose path is specified by @string.
209 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
213 * @s: #QTestState instance to operate on.
214 * @addr: I/O port to write to.
215 * @value: Value being written.
217 * Write an 8-bit value to an I/O port.
219 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
223 * @s: #QTestState instance to operate on.
224 * @addr: I/O port to write to.
225 * @value: Value being written.
227 * Write a 16-bit value to an I/O port.
229 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
233 * @s: #QTestState instance to operate on.
234 * @addr: I/O port to write to.
235 * @value: Value being written.
237 * Write a 32-bit value to an I/O port.
239 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
243 * @s: #QTestState instance to operate on.
244 * @addr: I/O port to read from.
246 * Returns an 8-bit value from an I/O port.
248 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
252 * @s: #QTestState instance to operate on.
253 * @addr: I/O port to read from.
255 * Returns a 16-bit value from an I/O port.
257 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
261 * @s: #QTestState instance to operate on.
262 * @addr: I/O port to read from.
264 * Returns a 32-bit value from an I/O port.
266 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
270 * @s: #QTestState instance to operate on.
271 * @addr: Guest address to write to.
272 * @value: Value being written.
274 * Writes an 8-bit value to memory.
276 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
280 * @s: #QTestState instance to operate on.
281 * @addr: Guest address to write to.
282 * @value: Value being written.
284 * Writes a 16-bit value to memory.
286 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
290 * @s: #QTestState instance to operate on.
291 * @addr: Guest address to write to.
292 * @value: Value being written.
294 * Writes a 32-bit value to memory.
296 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
300 * @s: #QTestState instance to operate on.
301 * @addr: Guest address to write to.
302 * @value: Value being written.
304 * Writes a 64-bit value to memory.
306 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
310 * @s: #QTestState instance to operate on.
311 * @addr: Guest address to read from.
313 * Reads an 8-bit value from memory.
315 * Returns: Value read.
317 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
321 * @s: #QTestState instance to operate on.
322 * @addr: Guest address to read from.
324 * Reads a 16-bit value from memory.
326 * Returns: Value read.
328 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
332 * @s: #QTestState instance to operate on.
333 * @addr: Guest address to read from.
335 * Reads a 32-bit value from memory.
337 * Returns: Value read.
339 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
343 * @s: #QTestState instance to operate on.
344 * @addr: Guest address to read from.
346 * Reads a 64-bit value from memory.
348 * Returns: Value read.
350 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
354 * @s: #QTestState instance to operate on.
355 * @addr: Guest address to read from.
356 * @data: Pointer to where memory contents will be stored.
357 * @size: Number of bytes to read.
359 * Read guest memory into a buffer.
361 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
365 * @s: #QTestState instance to operate on.
366 * @name: name of the command to call.
367 * @nargs: Number of args.
368 * @args: Guest address to read args from.
369 * @nret: Number of return value.
370 * @ret: Guest address to write return values to.
372 * Call an RTAS function
374 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
375 uint32_t nargs
, uint64_t args
,
376 uint32_t nret
, uint64_t ret
);
380 * @s: #QTestState instance to operate on.
381 * @addr: Guest address to read from.
382 * @data: Pointer to where memory contents will be stored.
383 * @size: Number of bytes to read.
385 * Read guest memory into a buffer and receive using a base64 encoding.
387 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
391 * @s: #QTestState instance to operate on.
392 * @addr: Guest address to write to.
393 * @data: Pointer to the bytes that will be written to guest memory.
394 * @size: Number of bytes to write.
396 * Write a buffer to guest memory.
398 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
402 * @s: #QTestState instance to operate on.
403 * @addr: Guest address to write to.
404 * @data: Pointer to the bytes that will be written to guest memory.
405 * @size: Number of bytes to write.
407 * Write a buffer to guest memory and transmit using a base64 encoding.
409 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
410 const void *data
, size_t size
);
414 * @s: #QTestState instance to operate on.
415 * @addr: Guest address to write to.
416 * @patt: Byte pattern to fill the guest memory region with.
417 * @size: Number of bytes to write.
419 * Write a pattern to guest memory.
421 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
424 * qtest_clock_step_next:
425 * @s: #QTestState instance to operate on.
427 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
429 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
431 int64_t qtest_clock_step_next(QTestState
*s
);
435 * @s: QTestState instance to operate on.
436 * @step: Number of nanoseconds to advance the clock by.
438 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
440 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
442 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
446 * @s: QTestState instance to operate on.
447 * @val: Nanoseconds value to advance the clock to.
449 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
451 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
453 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
457 * @s: QTestState instance to operate on.
459 * Returns: True if the architecture under test has a big endian configuration.
461 bool qtest_big_endian(QTestState
*s
);
466 * Returns: The architecture for the QEMU executable under test.
468 const char *qtest_get_arch(void);
472 * @str: Test case path.
473 * @fn: Test case function
475 * Add a GTester testcase with the given name and function.
476 * The path is prefixed with the architecture under test, as
477 * returned by qtest_get_arch().
479 void qtest_add_func(const char *str
, void (*fn
)(void));
482 * qtest_add_data_func:
483 * @str: Test case path.
484 * @data: Test case data
485 * @fn: Test case function
487 * Add a GTester testcase with the given name, data and function.
488 * The path is prefixed with the architecture under test, as
489 * returned by qtest_get_arch().
491 void qtest_add_data_func(const char *str
, const void *data
,
492 void (*fn
)(const void *));
495 * qtest_add_data_func_full:
496 * @str: Test case path.
497 * @data: Test case data
498 * @fn: Test case function
499 * @data_free_func: GDestroyNotify for data
501 * Add a GTester testcase with the given name, data and function.
502 * The path is prefixed with the architecture under test, as
503 * returned by qtest_get_arch().
505 * @data is passed to @data_free_func() on test completion.
507 void qtest_add_data_func_full(const char *str
, void *data
,
508 void (*fn
)(const void *),
509 GDestroyNotify data_free_func
);
513 * @testpath: Test case path
514 * @Fixture: Fixture type
515 * @tdata: Test case data
516 * @fsetup: Test case setup function
517 * @ftest: Test case function
518 * @fteardown: Test case teardown function
520 * Add a GTester testcase with the given name, data and functions.
521 * The path is prefixed with the architecture under test, as
522 * returned by qtest_get_arch().
524 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
526 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
527 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
531 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
535 * @args: other arguments to pass to QEMU
537 * Start QEMU and assign the resulting #QTestState to a global variable.
538 * The global variable is used by "shortcut" functions documented below.
540 * Returns: #QTestState instance.
542 static inline QTestState
*qtest_start(const char *args
)
544 global_qtest
= qtest_init(args
);
551 * Shut down the QEMU process started by qtest_start().
553 static inline void qtest_end(void)
555 qtest_quit(global_qtest
);
561 * @fmt...: QMP message to send to qemu
563 * Sends a QMP message to QEMU and returns the response.
565 QDict
*qmp(const char *fmt
, ...);
569 * @fmt...: QMP message to send to qemu
571 * Sends a QMP message to QEMU and leaves the response in the stream.
573 void qmp_async(const char *fmt
, ...);
576 * qmp_discard_response:
577 * @fmt...: QMP message to send to qemu
579 * Sends a QMP message to QEMU and consumes the response.
581 void qmp_discard_response(const char *fmt
, ...);
586 * Reads a QMP message from QEMU and returns the response.
588 static inline QDict
*qmp_receive(void)
590 return qtest_qmp_receive(global_qtest
);
595 * @s: #event event to wait for.
597 * Continuously polls for QMP responses until it receives the desired event.
599 static inline void qmp_eventwait(const char *event
)
601 return qtest_qmp_eventwait(global_qtest
, event
);
606 * @s: #event event to wait for.
608 * Continuously polls for QMP responses until it receives the desired event.
609 * Returns a copy of the event for further investigation.
611 static inline QDict
*qmp_eventwait_ref(const char *event
)
613 return qtest_qmp_eventwait_ref(global_qtest
, event
);
618 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
620 * Send HMP command to QEMU via QMP's human-monitor-command.
622 * Returns: the command's output. The caller should g_free() it.
624 char *hmp(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
628 * @num: Interrupt to observe.
630 * Returns: The level of the @num interrupt.
632 static inline bool get_irq(int num
)
634 return qtest_get_irq(global_qtest
, num
);
639 * @string: QOM path of a device.
641 * Associate qtest irqs with the GPIO-in pins of the device
642 * whose path is specified by @string.
644 static inline void irq_intercept_in(const char *string
)
646 qtest_irq_intercept_in(global_qtest
, string
);
650 * qtest_irq_intercept_out:
651 * @string: QOM path of a device.
653 * Associate qtest irqs with the GPIO-out pins of the device
654 * whose path is specified by @string.
656 static inline void irq_intercept_out(const char *string
)
658 qtest_irq_intercept_out(global_qtest
, string
);
663 * @addr: I/O port to write to.
664 * @value: Value being written.
666 * Write an 8-bit value to an I/O port.
668 static inline void outb(uint16_t addr
, uint8_t value
)
670 qtest_outb(global_qtest
, addr
, value
);
675 * @addr: I/O port to write to.
676 * @value: Value being written.
678 * Write a 16-bit value to an I/O port.
680 static inline void outw(uint16_t addr
, uint16_t value
)
682 qtest_outw(global_qtest
, addr
, value
);
687 * @addr: I/O port to write to.
688 * @value: Value being written.
690 * Write a 32-bit value to an I/O port.
692 static inline void outl(uint16_t addr
, uint32_t value
)
694 qtest_outl(global_qtest
, addr
, value
);
699 * @addr: I/O port to read from.
701 * Reads an 8-bit value from an I/O port.
703 * Returns: Value read.
705 static inline uint8_t inb(uint16_t addr
)
707 return qtest_inb(global_qtest
, addr
);
712 * @addr: I/O port to read from.
714 * Reads a 16-bit value from an I/O port.
716 * Returns: Value read.
718 static inline uint16_t inw(uint16_t addr
)
720 return qtest_inw(global_qtest
, addr
);
725 * @addr: I/O port to read from.
727 * Reads a 32-bit value from an I/O port.
729 * Returns: Value read.
731 static inline uint32_t inl(uint16_t addr
)
733 return qtest_inl(global_qtest
, addr
);
738 * @addr: Guest address to write to.
739 * @value: Value being written.
741 * Writes an 8-bit value to guest memory.
743 static inline void writeb(uint64_t addr
, uint8_t value
)
745 qtest_writeb(global_qtest
, addr
, value
);
750 * @addr: Guest address to write to.
751 * @value: Value being written.
753 * Writes a 16-bit value to guest memory.
755 static inline void writew(uint64_t addr
, uint16_t value
)
757 qtest_writew(global_qtest
, addr
, value
);
762 * @addr: Guest address to write to.
763 * @value: Value being written.
765 * Writes a 32-bit value to guest memory.
767 static inline void writel(uint64_t addr
, uint32_t value
)
769 qtest_writel(global_qtest
, addr
, value
);
774 * @addr: Guest address to write to.
775 * @value: Value being written.
777 * Writes a 64-bit value to guest memory.
779 static inline void writeq(uint64_t addr
, uint64_t value
)
781 qtest_writeq(global_qtest
, addr
, value
);
786 * @addr: Guest address to read from.
788 * Reads an 8-bit value from guest memory.
790 * Returns: Value read.
792 static inline uint8_t readb(uint64_t addr
)
794 return qtest_readb(global_qtest
, addr
);
799 * @addr: Guest address to read from.
801 * Reads a 16-bit value from guest memory.
803 * Returns: Value read.
805 static inline uint16_t readw(uint64_t addr
)
807 return qtest_readw(global_qtest
, addr
);
812 * @addr: Guest address to read from.
814 * Reads a 32-bit value from guest memory.
816 * Returns: Value read.
818 static inline uint32_t readl(uint64_t addr
)
820 return qtest_readl(global_qtest
, addr
);
825 * @addr: Guest address to read from.
827 * Reads a 64-bit value from guest memory.
829 * Returns: Value read.
831 static inline uint64_t readq(uint64_t addr
)
833 return qtest_readq(global_qtest
, addr
);
838 * @addr: Guest address to read from.
839 * @data: Pointer to where memory contents will be stored.
840 * @size: Number of bytes to read.
842 * Read guest memory into a buffer.
844 static inline void memread(uint64_t addr
, void *data
, size_t size
)
846 qtest_memread(global_qtest
, addr
, data
, size
);
851 * @addr: Guest address to read from.
852 * @data: Pointer to where memory contents will be stored.
853 * @size: Number of bytes to read.
855 * Read guest memory into a buffer, receive using a base64 encoding.
857 static inline void bufread(uint64_t addr
, void *data
, size_t size
)
859 qtest_bufread(global_qtest
, addr
, data
, size
);
864 * @addr: Guest address to write to.
865 * @data: Pointer to the bytes that will be written to guest memory.
866 * @size: Number of bytes to write.
868 * Write a buffer to guest memory.
870 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
872 qtest_memwrite(global_qtest
, addr
, data
, size
);
877 * @addr: Guest address to write to.
878 * @data: Pointer to the bytes that will be written to guest memory.
879 * @size: Number of bytes to write.
881 * Write a buffer to guest memory, transmit using a base64 encoding.
883 static inline void bufwrite(uint64_t addr
, const void *data
, size_t size
)
885 qtest_bufwrite(global_qtest
, addr
, data
, size
);
890 * @addr: Guest address to write to.
891 * @patt: Byte pattern to fill the guest memory region with.
892 * @size: Number of bytes to write.
894 * Write a pattern to guest memory.
896 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
898 qtest_memset(global_qtest
, addr
, patt
, size
);
904 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
906 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
908 static inline int64_t clock_step_next(void)
910 return qtest_clock_step_next(global_qtest
);
915 * @step: Number of nanoseconds to advance the clock by.
917 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
919 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
921 static inline int64_t clock_step(int64_t step
)
923 return qtest_clock_step(global_qtest
, step
);
928 * @val: Nanoseconds value to advance the clock to.
930 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
932 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
934 static inline int64_t clock_set(int64_t val
)
936 return qtest_clock_set(global_qtest
, val
);
939 QDict
*qmp_fd_receive(int fd
);
940 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
);
941 void qmp_fd_send(int fd
, const char *fmt
, ...);
942 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
);
943 QDict
*qmp_fd(int fd
, const char *fmt
, ...);
946 * qtest_cb_for_every_machine:
947 * @cb: Pointer to the callback function
949 * Call a callback function for every name of all available machines.
951 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
));
954 * qtest_qmp_device_add:
955 * @driver: Name of the device that should be added
956 * @id: Identification string
957 * @fmt: printf-like format string for further options to device_add
959 * Generic hot-plugging test via the device_add QMP command.
961 void qtest_qmp_device_add(const char *driver
, const char *id
, const char *fmt
,
962 ...) GCC_FMT_ATTR(3, 4);
965 * qtest_qmp_device_del:
966 * @id: Identification string
968 * Generic hot-unplugging test via the device_del QMP command.
970 void qtest_qmp_device_del(const char *id
);