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 * @fmt...: Format for creating other arguments to pass to QEMU, formatted
31 * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
32 * #global_qtest is left at NULL).
34 * Returns: #QTestState instance.
36 QTestState
*qtest_startf(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
40 * @fmt: Format for creating other arguments to pass to QEMU, formatted
42 * @ap: Format arguments.
44 * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
45 * #global_qtest is left at NULL).
47 * Returns: #QTestState instance.
49 QTestState
*qtest_vstartf(const char *fmt
, va_list ap
) GCC_FMT_ATTR(1, 0);
53 * @extra_args: other arguments to pass to QEMU.
55 * Returns: #QTestState instance.
57 QTestState
*qtest_init(const char *extra_args
);
60 * qtest_init_without_qmp_handshake:
61 * @extra_args: other arguments to pass to QEMU.
63 * Returns: #QTestState instance.
65 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
);
69 * @s: #QTestState instance to operate on.
71 * Shut down the QEMU process associated to @s.
73 void qtest_quit(QTestState
*s
);
76 * qtest_qmp_discard_response:
77 * @s: #QTestState instance to operate on.
78 * @fmt...: QMP message to send to qemu
80 * Sends a QMP message to QEMU and consumes the response.
82 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...);
86 * @s: #QTestState instance to operate on.
87 * @fmt...: QMP message to send to qemu
89 * Sends a QMP message to QEMU and returns the response.
91 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...);
95 * @s: #QTestState instance to operate on.
96 * @fmt...: QMP message to send to qemu
98 * Sends a QMP message to QEMU and leaves the response in the stream.
100 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...);
103 * qtest_qmpv_discard_response:
104 * @s: #QTestState instance to operate on.
105 * @fmt: QMP message to send to QEMU
106 * @ap: QMP message arguments
108 * Sends a QMP message to QEMU and consumes the response.
110 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
);
114 * @s: #QTestState instance to operate on.
115 * @fmt: QMP message to send to QEMU
116 * @ap: QMP message arguments
118 * Sends a QMP message to QEMU and returns the response.
120 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
124 * @s: #QTestState instance to operate on.
125 * @fmt: QMP message to send to QEMU
126 * @ap: QMP message arguments
128 * Sends a QMP message to QEMU and leaves the response in the stream.
130 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
134 * @s: #QTestState instance to operate on.
136 * Reads a QMP message from QEMU and returns the response.
138 QDict
*qtest_qmp_receive(QTestState
*s
);
141 * qtest_qmp_eventwait:
142 * @s: #QTestState instance to operate on.
143 * @s: #event event to wait for.
145 * Continuously polls for QMP responses until it receives the desired event.
147 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
150 * qtest_qmp_eventwait_ref:
151 * @s: #QTestState instance to operate on.
152 * @s: #event event to wait for.
154 * Continuously polls for QMP responses until it receives the desired event.
155 * Returns a copy of the event for further investigation.
157 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
);
161 * @s: #QTestState instance to operate on.
162 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
164 * Send HMP command to QEMU via QMP's human-monitor-command.
165 * QMP events are discarded.
167 * Returns: the command's output. The caller should g_free() it.
169 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...) GCC_FMT_ATTR(2, 3);
173 * @s: #QTestState instance to operate on.
174 * @fmt: HMP command to send to QEMU
175 * @ap: HMP command arguments
177 * Send HMP command to QEMU via QMP's human-monitor-command.
178 * QMP events are discarded.
180 * Returns: the command's output. The caller should g_free() it.
182 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
);
186 * @s: #QTestState instance to operate on.
187 * @num: Interrupt to observe.
189 * Returns: The level of the @num interrupt.
191 bool qtest_get_irq(QTestState
*s
, int num
);
194 * qtest_irq_intercept_in:
195 * @s: #QTestState instance to operate on.
196 * @string: QOM path of a device.
198 * Associate qtest irqs with the GPIO-in pins of the device
199 * whose path is specified by @string.
201 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
204 * qtest_irq_intercept_out:
205 * @s: #QTestState instance to operate on.
206 * @string: QOM path of a device.
208 * Associate qtest irqs with the GPIO-out pins of the device
209 * whose path is specified by @string.
211 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
215 * @s: #QTestState instance to operate on.
216 * @addr: I/O port to write to.
217 * @value: Value being written.
219 * Write an 8-bit value to an I/O port.
221 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
225 * @s: #QTestState instance to operate on.
226 * @addr: I/O port to write to.
227 * @value: Value being written.
229 * Write a 16-bit value to an I/O port.
231 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
235 * @s: #QTestState instance to operate on.
236 * @addr: I/O port to write to.
237 * @value: Value being written.
239 * Write a 32-bit value to an I/O port.
241 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
245 * @s: #QTestState instance to operate on.
246 * @addr: I/O port to read from.
248 * Returns an 8-bit value from an I/O port.
250 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
254 * @s: #QTestState instance to operate on.
255 * @addr: I/O port to read from.
257 * Returns a 16-bit value from an I/O port.
259 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
263 * @s: #QTestState instance to operate on.
264 * @addr: I/O port to read from.
266 * Returns a 32-bit value from an I/O port.
268 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
272 * @s: #QTestState instance to operate on.
273 * @addr: Guest address to write to.
274 * @value: Value being written.
276 * Writes an 8-bit value to memory.
278 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
282 * @s: #QTestState instance to operate on.
283 * @addr: Guest address to write to.
284 * @value: Value being written.
286 * Writes a 16-bit value to memory.
288 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
292 * @s: #QTestState instance to operate on.
293 * @addr: Guest address to write to.
294 * @value: Value being written.
296 * Writes a 32-bit value to memory.
298 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
302 * @s: #QTestState instance to operate on.
303 * @addr: Guest address to write to.
304 * @value: Value being written.
306 * Writes a 64-bit value to memory.
308 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
312 * @s: #QTestState instance to operate on.
313 * @addr: Guest address to read from.
315 * Reads an 8-bit value from memory.
317 * Returns: Value read.
319 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
323 * @s: #QTestState instance to operate on.
324 * @addr: Guest address to read from.
326 * Reads a 16-bit value from memory.
328 * Returns: Value read.
330 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
334 * @s: #QTestState instance to operate on.
335 * @addr: Guest address to read from.
337 * Reads a 32-bit value from memory.
339 * Returns: Value read.
341 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
345 * @s: #QTestState instance to operate on.
346 * @addr: Guest address to read from.
348 * Reads a 64-bit value from memory.
350 * Returns: Value read.
352 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
356 * @s: #QTestState instance to operate on.
357 * @addr: Guest address to read from.
358 * @data: Pointer to where memory contents will be stored.
359 * @size: Number of bytes to read.
361 * Read guest memory into a buffer.
363 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
367 * @s: #QTestState instance to operate on.
368 * @name: name of the command to call.
369 * @nargs: Number of args.
370 * @args: Guest address to read args from.
371 * @nret: Number of return value.
372 * @ret: Guest address to write return values to.
374 * Call an RTAS function
376 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
377 uint32_t nargs
, uint64_t args
,
378 uint32_t nret
, uint64_t ret
);
382 * @s: #QTestState instance to operate on.
383 * @addr: Guest address to read from.
384 * @data: Pointer to where memory contents will be stored.
385 * @size: Number of bytes to read.
387 * Read guest memory into a buffer and receive using a base64 encoding.
389 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
393 * @s: #QTestState instance to operate on.
394 * @addr: Guest address to write to.
395 * @data: Pointer to the bytes that will be written to guest memory.
396 * @size: Number of bytes to write.
398 * Write a buffer to guest memory.
400 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
404 * @s: #QTestState instance to operate on.
405 * @addr: Guest address to write to.
406 * @data: Pointer to the bytes that will be written to guest memory.
407 * @size: Number of bytes to write.
409 * Write a buffer to guest memory and transmit using a base64 encoding.
411 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
412 const void *data
, size_t size
);
416 * @s: #QTestState instance to operate on.
417 * @addr: Guest address to write to.
418 * @patt: Byte pattern to fill the guest memory region with.
419 * @size: Number of bytes to write.
421 * Write a pattern to guest memory.
423 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
426 * qtest_clock_step_next:
427 * @s: #QTestState instance to operate on.
429 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
431 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
433 int64_t qtest_clock_step_next(QTestState
*s
);
437 * @s: QTestState instance to operate on.
438 * @step: Number of nanoseconds to advance the clock by.
440 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
442 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
444 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
448 * @s: QTestState instance to operate on.
449 * @val: Nanoseconds value to advance the clock to.
451 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
453 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
455 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
459 * @s: QTestState instance to operate on.
461 * Returns: True if the architecture under test has a big endian configuration.
463 bool qtest_big_endian(QTestState
*s
);
468 * Returns: The architecture for the QEMU executable under test.
470 const char *qtest_get_arch(void);
474 * @str: Test case path.
475 * @fn: Test case function
477 * Add a GTester testcase with the given name and function.
478 * The path is prefixed with the architecture under test, as
479 * returned by qtest_get_arch().
481 void qtest_add_func(const char *str
, void (*fn
)(void));
484 * qtest_add_data_func:
485 * @str: Test case path.
486 * @data: Test case data
487 * @fn: Test case function
489 * Add a GTester testcase with the given name, data and function.
490 * The path is prefixed with the architecture under test, as
491 * returned by qtest_get_arch().
493 void qtest_add_data_func(const char *str
, const void *data
,
494 void (*fn
)(const void *));
497 * qtest_add_data_func_full:
498 * @str: Test case path.
499 * @data: Test case data
500 * @fn: Test case function
501 * @data_free_func: GDestroyNotify for data
503 * Add a GTester testcase with the given name, data and function.
504 * The path is prefixed with the architecture under test, as
505 * returned by qtest_get_arch().
507 * @data is passed to @data_free_func() on test completion.
509 void qtest_add_data_func_full(const char *str
, void *data
,
510 void (*fn
)(const void *),
511 GDestroyNotify data_free_func
);
515 * @testpath: Test case path
516 * @Fixture: Fixture type
517 * @tdata: Test case data
518 * @fsetup: Test case setup function
519 * @ftest: Test case function
520 * @fteardown: Test case teardown function
522 * Add a GTester testcase with the given name, data and functions.
523 * The path is prefixed with the architecture under test, as
524 * returned by qtest_get_arch().
526 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
528 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
529 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
533 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
537 * @args: other arguments to pass to QEMU
539 * Start QEMU and assign the resulting #QTestState to a global variable.
540 * The global variable is used by "shortcut" functions documented below.
542 * Returns: #QTestState instance.
544 static inline QTestState
*qtest_start(const char *args
)
546 global_qtest
= qtest_init(args
);
553 * Shut down the QEMU process started by qtest_start().
555 static inline void qtest_end(void)
557 qtest_quit(global_qtest
);
563 * @fmt...: QMP message to send to qemu
565 * Sends a QMP message to QEMU and returns the response.
567 QDict
*qmp(const char *fmt
, ...);
571 * @fmt...: QMP message to send to qemu
573 * Sends a QMP message to QEMU and leaves the response in the stream.
575 void qmp_async(const char *fmt
, ...);
578 * qmp_discard_response:
579 * @fmt...: QMP message to send to qemu
581 * Sends a QMP message to QEMU and consumes the response.
583 void qmp_discard_response(const char *fmt
, ...);
588 * Reads a QMP message from QEMU and returns the response.
590 static inline QDict
*qmp_receive(void)
592 return qtest_qmp_receive(global_qtest
);
597 * @s: #event event to wait for.
599 * Continuously polls for QMP responses until it receives the desired event.
601 static inline void qmp_eventwait(const char *event
)
603 return qtest_qmp_eventwait(global_qtest
, event
);
608 * @s: #event event to wait for.
610 * Continuously polls for QMP responses until it receives the desired event.
611 * Returns a copy of the event for further investigation.
613 static inline QDict
*qmp_eventwait_ref(const char *event
)
615 return qtest_qmp_eventwait_ref(global_qtest
, event
);
620 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
622 * Send HMP command to QEMU via QMP's human-monitor-command.
624 * Returns: the command's output. The caller should g_free() it.
626 char *hmp(const char *fmt
, ...) GCC_FMT_ATTR(1, 2);
630 * @num: Interrupt to observe.
632 * Returns: The level of the @num interrupt.
634 static inline bool get_irq(int num
)
636 return qtest_get_irq(global_qtest
, num
);
641 * @string: QOM path of a device.
643 * Associate qtest irqs with the GPIO-in pins of the device
644 * whose path is specified by @string.
646 static inline void irq_intercept_in(const char *string
)
648 qtest_irq_intercept_in(global_qtest
, string
);
652 * qtest_irq_intercept_out:
653 * @string: QOM path of a device.
655 * Associate qtest irqs with the GPIO-out pins of the device
656 * whose path is specified by @string.
658 static inline void irq_intercept_out(const char *string
)
660 qtest_irq_intercept_out(global_qtest
, string
);
665 * @addr: I/O port to write to.
666 * @value: Value being written.
668 * Write an 8-bit value to an I/O port.
670 static inline void outb(uint16_t addr
, uint8_t value
)
672 qtest_outb(global_qtest
, addr
, value
);
677 * @addr: I/O port to write to.
678 * @value: Value being written.
680 * Write a 16-bit value to an I/O port.
682 static inline void outw(uint16_t addr
, uint16_t value
)
684 qtest_outw(global_qtest
, addr
, value
);
689 * @addr: I/O port to write to.
690 * @value: Value being written.
692 * Write a 32-bit value to an I/O port.
694 static inline void outl(uint16_t addr
, uint32_t value
)
696 qtest_outl(global_qtest
, addr
, value
);
701 * @addr: I/O port to read from.
703 * Reads an 8-bit value from an I/O port.
705 * Returns: Value read.
707 static inline uint8_t inb(uint16_t addr
)
709 return qtest_inb(global_qtest
, addr
);
714 * @addr: I/O port to read from.
716 * Reads a 16-bit value from an I/O port.
718 * Returns: Value read.
720 static inline uint16_t inw(uint16_t addr
)
722 return qtest_inw(global_qtest
, addr
);
727 * @addr: I/O port to read from.
729 * Reads a 32-bit value from an I/O port.
731 * Returns: Value read.
733 static inline uint32_t inl(uint16_t addr
)
735 return qtest_inl(global_qtest
, addr
);
740 * @addr: Guest address to write to.
741 * @value: Value being written.
743 * Writes an 8-bit value to guest memory.
745 static inline void writeb(uint64_t addr
, uint8_t value
)
747 qtest_writeb(global_qtest
, addr
, value
);
752 * @addr: Guest address to write to.
753 * @value: Value being written.
755 * Writes a 16-bit value to guest memory.
757 static inline void writew(uint64_t addr
, uint16_t value
)
759 qtest_writew(global_qtest
, addr
, value
);
764 * @addr: Guest address to write to.
765 * @value: Value being written.
767 * Writes a 32-bit value to guest memory.
769 static inline void writel(uint64_t addr
, uint32_t value
)
771 qtest_writel(global_qtest
, addr
, value
);
776 * @addr: Guest address to write to.
777 * @value: Value being written.
779 * Writes a 64-bit value to guest memory.
781 static inline void writeq(uint64_t addr
, uint64_t value
)
783 qtest_writeq(global_qtest
, addr
, value
);
788 * @addr: Guest address to read from.
790 * Reads an 8-bit value from guest memory.
792 * Returns: Value read.
794 static inline uint8_t readb(uint64_t addr
)
796 return qtest_readb(global_qtest
, addr
);
801 * @addr: Guest address to read from.
803 * Reads a 16-bit value from guest memory.
805 * Returns: Value read.
807 static inline uint16_t readw(uint64_t addr
)
809 return qtest_readw(global_qtest
, addr
);
814 * @addr: Guest address to read from.
816 * Reads a 32-bit value from guest memory.
818 * Returns: Value read.
820 static inline uint32_t readl(uint64_t addr
)
822 return qtest_readl(global_qtest
, addr
);
827 * @addr: Guest address to read from.
829 * Reads a 64-bit value from guest memory.
831 * Returns: Value read.
833 static inline uint64_t readq(uint64_t addr
)
835 return qtest_readq(global_qtest
, addr
);
840 * @addr: Guest address to read from.
841 * @data: Pointer to where memory contents will be stored.
842 * @size: Number of bytes to read.
844 * Read guest memory into a buffer.
846 static inline void memread(uint64_t addr
, void *data
, size_t size
)
848 qtest_memread(global_qtest
, addr
, data
, size
);
853 * @addr: Guest address to read from.
854 * @data: Pointer to where memory contents will be stored.
855 * @size: Number of bytes to read.
857 * Read guest memory into a buffer, receive using a base64 encoding.
859 static inline void bufread(uint64_t addr
, void *data
, size_t size
)
861 qtest_bufread(global_qtest
, addr
, data
, size
);
866 * @addr: Guest address to write to.
867 * @data: Pointer to the bytes that will be written to guest memory.
868 * @size: Number of bytes to write.
870 * Write a buffer to guest memory.
872 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
874 qtest_memwrite(global_qtest
, addr
, data
, size
);
879 * @addr: Guest address to write to.
880 * @data: Pointer to the bytes that will be written to guest memory.
881 * @size: Number of bytes to write.
883 * Write a buffer to guest memory, transmit using a base64 encoding.
885 static inline void bufwrite(uint64_t addr
, const void *data
, size_t size
)
887 qtest_bufwrite(global_qtest
, addr
, data
, size
);
892 * @addr: Guest address to write to.
893 * @patt: Byte pattern to fill the guest memory region with.
894 * @size: Number of bytes to write.
896 * Write a pattern to guest memory.
898 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
900 qtest_memset(global_qtest
, addr
, patt
, size
);
906 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
908 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
910 static inline int64_t clock_step_next(void)
912 return qtest_clock_step_next(global_qtest
);
917 * @step: Number of nanoseconds to advance the clock by.
919 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
921 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
923 static inline int64_t clock_step(int64_t step
)
925 return qtest_clock_step(global_qtest
, step
);
930 * @val: Nanoseconds value to advance the clock to.
932 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
934 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
936 static inline int64_t clock_set(int64_t val
)
938 return qtest_clock_set(global_qtest
, val
);
941 QDict
*qmp_fd_receive(int fd
);
942 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
);
943 void qmp_fd_send(int fd
, const char *fmt
, ...);
944 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
);
945 QDict
*qmp_fd(int fd
, const char *fmt
, ...);
948 * qtest_cb_for_every_machine:
949 * @cb: Pointer to the callback function
951 * Call a callback function for every name of all available machines.
953 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
));
956 * qtest_qmp_device_add:
957 * @driver: Name of the device that should be added
958 * @id: Identification string
959 * @fmt: printf-like format string for further options to device_add
961 * Generic hot-plugging test via the device_add QMP command.
963 void qtest_qmp_device_add(const char *driver
, const char *id
, const char *fmt
,
964 ...) GCC_FMT_ATTR(3, 4);
967 * qtest_qmp_device_del:
968 * @id: Identification string
970 * Generic hot-unplugging test via the device_del QMP command.
972 void qtest_qmp_device_del(const char *id
);