4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
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);
78 * @s: #QTestState instance to operate on.
79 * @fmt...: QMP message to send to qemu, formatted like
80 * qobject_from_jsonf_nofail(). See parse_escape() for what's
81 * supported after '%'.
83 * Sends a QMP message to QEMU and returns the response.
85 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
90 * @s: #QTestState instance to operate on.
91 * @fmt...: QMP message to send to qemu, formatted like
92 * qobject_from_jsonf_nofail(). See parse_escape() for what's
93 * supported after '%'.
95 * Sends a QMP message to QEMU and leaves the response in the stream.
97 void qtest_qmp_send(QTestState *s, const char *fmt, ...)
102 * @s: #QTestState instance to operate on.
103 * @fmt: QMP message to send to QEMU, formatted like
104 * qobject_from_jsonf_nofail(). See parse_escape() for what's
105 * supported after '%'.
106 * @ap: QMP message arguments
108 * Sends a QMP message to QEMU and returns the response.
110 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
115 * @s: #QTestState instance to operate on.
116 * @fmt: QMP message to send to QEMU, formatted like
117 * qobject_from_jsonf_nofail(). See parse_escape() for what's
118 * supported after '%'.
119 * @ap: QMP message arguments
121 * Sends a QMP message to QEMU and leaves the response in the stream.
123 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
128 * @s: #QTestState instance to operate on.
130 * Reads a QMP message from QEMU and returns the response.
132 QDict *qtest_qmp_receive(QTestState *s);
135 * qtest_qmp_eventwait:
136 * @s: #QTestState instance to operate on.
137 * @s: #event event to wait for.
139 * Continuously polls for QMP responses until it receives the desired event.
141 void qtest_qmp_eventwait(QTestState *s, const char *event);
144 * qtest_qmp_eventwait_ref:
145 * @s: #QTestState instance to operate on.
146 * @s: #event event to wait for.
148 * Continuously polls for QMP responses until it receives the desired event.
149 * Returns a copy of the event for further investigation.
151 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
154 * qtest_qmp_receive_success:
155 * @s: #QTestState instance to operate on
156 * @event_cb: Event callback
157 * @opaque: Argument for @event_cb
159 * Poll QMP messages until a command success response is received.
160 * If @event_cb, call it for each event received, passing @opaque,
161 * the event's name and data.
162 * Return the success response's "return" member.
164 QDict *qtest_qmp_receive_success(QTestState *s,
165 void (*event_cb)(void *opaque,
172 * @s: #QTestState instance to operate on.
173 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
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_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
184 * @s: #QTestState instance to operate on.
185 * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
186 * @ap: HMP command arguments
188 * Send HMP command to QEMU via QMP's human-monitor-command.
189 * QMP events are discarded.
191 * Returns: the command's output. The caller should g_free() it.
193 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
198 * @s: #QTestState instance to operate on.
199 * @num: Interrupt to observe.
201 * Returns: The level of the @num interrupt.
203 bool qtest_get_irq(QTestState *s, int num);
206 * qtest_irq_intercept_in:
207 * @s: #QTestState instance to operate on.
208 * @string: QOM path of a device.
210 * Associate qtest irqs with the GPIO-in pins of the device
211 * whose path is specified by @string.
213 void qtest_irq_intercept_in(QTestState *s, const char *string);
216 * qtest_irq_intercept_out:
217 * @s: #QTestState instance to operate on.
218 * @string: QOM path of a device.
220 * Associate qtest irqs with the GPIO-out pins of the device
221 * whose path is specified by @string.
223 void qtest_irq_intercept_out(QTestState *s, const char *string);
227 * @s: #QTestState instance to operate on.
228 * @addr: I/O port to write to.
229 * @value: Value being written.
231 * Write an 8-bit value to an I/O port.
233 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
237 * @s: #QTestState instance to operate on.
238 * @addr: I/O port to write to.
239 * @value: Value being written.
241 * Write a 16-bit value to an I/O port.
243 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
247 * @s: #QTestState instance to operate on.
248 * @addr: I/O port to write to.
249 * @value: Value being written.
251 * Write a 32-bit value to an I/O port.
253 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
257 * @s: #QTestState instance to operate on.
258 * @addr: I/O port to read from.
260 * Returns an 8-bit value from an I/O port.
262 uint8_t qtest_inb(QTestState *s, uint16_t addr);
266 * @s: #QTestState instance to operate on.
267 * @addr: I/O port to read from.
269 * Returns a 16-bit value from an I/O port.
271 uint16_t qtest_inw(QTestState *s, uint16_t addr);
275 * @s: #QTestState instance to operate on.
276 * @addr: I/O port to read from.
278 * Returns a 32-bit value from an I/O port.
280 uint32_t qtest_inl(QTestState *s, uint16_t addr);
284 * @s: #QTestState instance to operate on.
285 * @addr: Guest address to write to.
286 * @value: Value being written.
288 * Writes an 8-bit value to memory.
290 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
294 * @s: #QTestState instance to operate on.
295 * @addr: Guest address to write to.
296 * @value: Value being written.
298 * Writes a 16-bit value to memory.
300 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
304 * @s: #QTestState instance to operate on.
305 * @addr: Guest address to write to.
306 * @value: Value being written.
308 * Writes a 32-bit value to memory.
310 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
314 * @s: #QTestState instance to operate on.
315 * @addr: Guest address to write to.
316 * @value: Value being written.
318 * Writes a 64-bit value to memory.
320 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
324 * @s: #QTestState instance to operate on.
325 * @addr: Guest address to read from.
327 * Reads an 8-bit value from memory.
329 * Returns: Value read.
331 uint8_t qtest_readb(QTestState *s, uint64_t addr);
335 * @s: #QTestState instance to operate on.
336 * @addr: Guest address to read from.
338 * Reads a 16-bit value from memory.
340 * Returns: Value read.
342 uint16_t qtest_readw(QTestState *s, uint64_t addr);
346 * @s: #QTestState instance to operate on.
347 * @addr: Guest address to read from.
349 * Reads a 32-bit value from memory.
351 * Returns: Value read.
353 uint32_t qtest_readl(QTestState *s, uint64_t addr);
357 * @s: #QTestState instance to operate on.
358 * @addr: Guest address to read from.
360 * Reads a 64-bit value from memory.
362 * Returns: Value read.
364 uint64_t qtest_readq(QTestState *s, uint64_t addr);
368 * @s: #QTestState instance to operate on.
369 * @addr: Guest address to read from.
370 * @data: Pointer to where memory contents will be stored.
371 * @size: Number of bytes to read.
373 * Read guest memory into a buffer.
375 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
379 * @s: #QTestState instance to operate on.
380 * @name: name of the command to call.
381 * @nargs: Number of args.
382 * @args: Guest address to read args from.
383 * @nret: Number of return value.
384 * @ret: Guest address to write return values to.
386 * Call an RTAS function
388 uint64_t qtest_rtas_call(QTestState *s, const char *name,
389 uint32_t nargs, uint64_t args,
390 uint32_t nret, uint64_t ret);
394 * @s: #QTestState instance to operate on.
395 * @addr: Guest address to read from.
396 * @data: Pointer to where memory contents will be stored.
397 * @size: Number of bytes to read.
399 * Read guest memory into a buffer and receive using a base64 encoding.
401 void qtest_bufread(QTestState *s, uint64_t addr, 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.
412 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
416 * @s: #QTestState instance to operate on.
417 * @addr: Guest address to write to.
418 * @data: Pointer to the bytes that will be written to guest memory.
419 * @size: Number of bytes to write.
421 * Write a buffer to guest memory and transmit using a base64 encoding.
423 void qtest_bufwrite(QTestState *s, uint64_t addr,
424 const void *data, size_t size);
428 * @s: #QTestState instance to operate on.
429 * @addr: Guest address to write to.
430 * @patt: Byte pattern to fill the guest memory region with.
431 * @size: Number of bytes to write.
433 * Write a pattern to guest memory.
435 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
438 * qtest_clock_step_next:
439 * @s: #QTestState instance to operate on.
441 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
443 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
445 int64_t qtest_clock_step_next(QTestState *s);
449 * @s: QTestState instance to operate on.
450 * @step: Number of nanoseconds to advance the clock by.
452 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
454 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
456 int64_t qtest_clock_step(QTestState *s, int64_t step);
460 * @s: QTestState instance to operate on.
461 * @val: Nanoseconds value to advance the clock to.
463 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
465 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
467 int64_t qtest_clock_set(QTestState *s, int64_t val);
471 * @s: QTestState instance to operate on.
473 * Returns: True if the architecture under test has a big endian configuration.
475 bool qtest_big_endian(QTestState *s);
480 * Returns: The architecture for the QEMU executable under test.
482 const char *qtest_get_arch(void);
486 * @str: Test case path.
487 * @fn: Test case function
489 * Add a GTester testcase with the given name and function.
490 * The path is prefixed with the architecture under test, as
491 * returned by qtest_get_arch().
493 void qtest_add_func(const char *str, void (*fn)(void));
496 * qtest_add_data_func:
497 * @str: Test case path.
498 * @data: Test case data
499 * @fn: Test case function
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 void qtest_add_data_func(const char *str, const void *data,
506 void (*fn)(const void *));
509 * qtest_add_data_func_full:
510 * @str: Test case path.
511 * @data: Test case data
512 * @fn: Test case function
513 * @data_free_func: GDestroyNotify for data
515 * Add a GTester testcase with the given name, data and function.
516 * The path is prefixed with the architecture under test, as
517 * returned by qtest_get_arch().
519 * @data is passed to @data_free_func() on test completion.
521 void qtest_add_data_func_full(const char *str, void *data,
522 void (*fn)(const void *),
523 GDestroyNotify data_free_func);
527 * @testpath: Test case path
528 * @Fixture: Fixture type
529 * @tdata: Test case data
530 * @fsetup: Test case setup function
531 * @ftest: Test case function
532 * @fteardown: Test case teardown function
534 * Add a GTester testcase with the given name, data and functions.
535 * The path is prefixed with the architecture under test, as
536 * returned by qtest_get_arch().
538 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
540 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
541 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
545 void qtest_add_abrt_handler(GHookFunc fn, const void *data);
549 * @args: other arguments to pass to QEMU
551 * Start QEMU and assign the resulting #QTestState to a global variable.
552 * The global variable is used by "shortcut" functions documented below.
554 * Returns: #QTestState instance.
556 static inline QTestState *qtest_start(const char *args)
558 global_qtest = qtest_init(args);
565 * Shut down the QEMU process started by qtest_start().
567 static inline void qtest_end(void)
569 qtest_quit(global_qtest);
575 * @fmt...: QMP message to send to qemu, formatted like
576 * qobject_from_jsonf_nofail(). See parse_escape() for what's
577 * supported after '%'.
579 * Sends a QMP message to QEMU and returns the response.
581 QDict *qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
585 * @fmt...: QMP message to send to qemu, formatted like
586 * qobject_from_jsonf_nofail(). See parse_escape() for what's
587 * supported after '%'.
589 * Sends a QMP message to QEMU and leaves the response in the stream.
591 void qmp_send(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
596 * Reads a QMP message from QEMU and returns the response.
598 static inline QDict *qmp_receive(void)
600 return qtest_qmp_receive(global_qtest);
605 * @s: #event event to wait for.
607 * Continuously polls for QMP responses until it receives the desired event.
609 static inline void qmp_eventwait(const char *event)
611 return qtest_qmp_eventwait(global_qtest, event);
616 * @s: #event event to wait for.
618 * Continuously polls for QMP responses until it receives the desired event.
619 * Returns a copy of the event for further investigation.
621 static inline QDict *qmp_eventwait_ref(const char *event)
623 return qtest_qmp_eventwait_ref(global_qtest, event);
628 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
630 * Send HMP command to QEMU via QMP's human-monitor-command.
632 * Returns: the command's output. The caller should g_free() it.
634 char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
638 * @num: Interrupt to observe.
640 * Returns: The level of the @num interrupt.
642 static inline bool get_irq(int num)
644 return qtest_get_irq(global_qtest, num);
649 * @string: QOM path of a device.
651 * Associate qtest irqs with the GPIO-in pins of the device
652 * whose path is specified by @string.
654 static inline void irq_intercept_in(const char *string)
656 qtest_irq_intercept_in(global_qtest, string);
660 * qtest_irq_intercept_out:
661 * @string: QOM path of a device.
663 * Associate qtest irqs with the GPIO-out pins of the device
664 * whose path is specified by @string.
666 static inline void irq_intercept_out(const char *string)
668 qtest_irq_intercept_out(global_qtest, string);
673 * @addr: I/O port to write to.
674 * @value: Value being written.
676 * Write an 8-bit value to an I/O port.
678 static inline void outb(uint16_t addr, uint8_t value)
680 qtest_outb(global_qtest, addr, value);
685 * @addr: I/O port to write to.
686 * @value: Value being written.
688 * Write a 16-bit value to an I/O port.
690 static inline void outw(uint16_t addr, uint16_t value)
692 qtest_outw(global_qtest, addr, value);
697 * @addr: I/O port to write to.
698 * @value: Value being written.
700 * Write a 32-bit value to an I/O port.
702 static inline void outl(uint16_t addr, uint32_t value)
704 qtest_outl(global_qtest, addr, value);
709 * @addr: I/O port to read from.
711 * Reads an 8-bit value from an I/O port.
713 * Returns: Value read.
715 static inline uint8_t inb(uint16_t addr)
717 return qtest_inb(global_qtest, addr);
722 * @addr: I/O port to read from.
724 * Reads a 16-bit value from an I/O port.
726 * Returns: Value read.
728 static inline uint16_t inw(uint16_t addr)
730 return qtest_inw(global_qtest, addr);
735 * @addr: I/O port to read from.
737 * Reads a 32-bit value from an I/O port.
739 * Returns: Value read.
741 static inline uint32_t inl(uint16_t addr)
743 return qtest_inl(global_qtest, addr);
748 * @addr: Guest address to write to.
749 * @value: Value being written.
751 * Writes an 8-bit value to guest memory.
753 static inline void writeb(uint64_t addr, uint8_t value)
755 qtest_writeb(global_qtest, addr, value);
760 * @addr: Guest address to write to.
761 * @value: Value being written.
763 * Writes a 16-bit value to guest memory.
765 static inline void writew(uint64_t addr, uint16_t value)
767 qtest_writew(global_qtest, addr, value);
772 * @addr: Guest address to write to.
773 * @value: Value being written.
775 * Writes a 32-bit value to guest memory.
777 static inline void writel(uint64_t addr, uint32_t value)
779 qtest_writel(global_qtest, addr, value);
784 * @addr: Guest address to write to.
785 * @value: Value being written.
787 * Writes a 64-bit value to guest memory.
789 static inline void writeq(uint64_t addr, uint64_t value)
791 qtest_writeq(global_qtest, addr, value);
796 * @addr: Guest address to read from.
798 * Reads an 8-bit value from guest memory.
800 * Returns: Value read.
802 static inline uint8_t readb(uint64_t addr)
804 return qtest_readb(global_qtest, addr);
809 * @addr: Guest address to read from.
811 * Reads a 16-bit value from guest memory.
813 * Returns: Value read.
815 static inline uint16_t readw(uint64_t addr)
817 return qtest_readw(global_qtest, addr);
822 * @addr: Guest address to read from.
824 * Reads a 32-bit value from guest memory.
826 * Returns: Value read.
828 static inline uint32_t readl(uint64_t addr)
830 return qtest_readl(global_qtest, addr);
835 * @addr: Guest address to read from.
837 * Reads a 64-bit value from guest memory.
839 * Returns: Value read.
841 static inline uint64_t readq(uint64_t addr)
843 return qtest_readq(global_qtest, addr);
848 * @addr: Guest address to read from.
849 * @data: Pointer to where memory contents will be stored.
850 * @size: Number of bytes to read.
852 * Read guest memory into a buffer.
854 static inline void memread(uint64_t addr, void *data, size_t size)
856 qtest_memread(global_qtest, addr, data, size);
861 * @addr: Guest address to read from.
862 * @data: Pointer to where memory contents will be stored.
863 * @size: Number of bytes to read.
865 * Read guest memory into a buffer, receive using a base64 encoding.
867 static inline void bufread(uint64_t addr, void *data, size_t size)
869 qtest_bufread(global_qtest, addr, data, size);
874 * @addr: Guest address to write to.
875 * @data: Pointer to the bytes that will be written to guest memory.
876 * @size: Number of bytes to write.
878 * Write a buffer to guest memory.
880 static inline void memwrite(uint64_t addr, const void *data, size_t size)
882 qtest_memwrite(global_qtest, addr, data, size);
887 * @addr: Guest address to write to.
888 * @data: Pointer to the bytes that will be written to guest memory.
889 * @size: Number of bytes to write.
891 * Write a buffer to guest memory, transmit using a base64 encoding.
893 static inline void bufwrite(uint64_t addr, const void *data, size_t size)
895 qtest_bufwrite(global_qtest, addr, data, size);
900 * @addr: Guest address to write to.
901 * @patt: Byte pattern to fill the guest memory region with.
902 * @size: Number of bytes to write.
904 * Write a pattern to guest memory.
906 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
908 qtest_memset(global_qtest, addr, patt, size);
914 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
916 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
918 static inline int64_t clock_step_next(void)
920 return qtest_clock_step_next(global_qtest);
925 * @step: Number of nanoseconds to advance the clock by.
927 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
929 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
931 static inline int64_t clock_step(int64_t step)
933 return qtest_clock_step(global_qtest, step);
938 * @val: Nanoseconds value to advance the clock to.
940 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
942 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
944 static inline int64_t clock_set(int64_t val)
946 return qtest_clock_set(global_qtest, val);
949 QDict *qmp_fd_receive(int fd);
950 void qmp_fd_vsend(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
951 void qmp_fd_send(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
952 QDict *qmp_fdv(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
953 QDict *qmp_fd(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
956 * qtest_cb_for_every_machine:
957 * @cb: Pointer to the callback function
959 * Call a callback function for every name of all available machines.
961 void qtest_cb_for_every_machine(void (*cb)(const char *machine));
964 * qtest_qmp_device_add:
965 * @driver: Name of the device that should be added
966 * @id: Identification string
967 * @fmt...: QMP message to send to qemu, formatted like
968 * qobject_from_jsonf_nofail(). See parse_escape() for what's
969 * supported after '%'.
971 * Generic hot-plugging test via the device_add QMP command.
973 void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
974 ...) GCC_FMT_ATTR(3, 4);
977 * qtest_qmp_device_del:
978 * @id: Identification string
980 * Generic hot-unplugging test via the device_del QMP command.
982 void qtest_qmp_device_del(const char *id);
986 * @rsp: QMP response to check for error
988 * Test @rsp for error and discard @rsp.
989 * Returns 'true' if there is error in @rsp and 'false' otherwise.
991 bool qmp_rsp_is_err(QDict *rsp);