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.
141 * Returns: the command's output. The caller should g_free() it.
143 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...);
147 * @s: #QTestState instance to operate on.
148 * @fmt: HMP command to send to QEMU
149 * @ap: HMP command arguments
151 * Send HMP command to QEMU via QMP's human-monitor-command.
153 * Returns: the command's output. The caller should g_free() it.
155 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
);
159 * @s: #QTestState instance to operate on.
160 * @num: Interrupt to observe.
162 * Returns: The level of the @num interrupt.
164 bool qtest_get_irq(QTestState
*s
, int num
);
167 * qtest_irq_intercept_in:
168 * @s: #QTestState instance to operate on.
169 * @string: QOM path of a device.
171 * Associate qtest irqs with the GPIO-in pins of the device
172 * whose path is specified by @string.
174 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
177 * qtest_irq_intercept_out:
178 * @s: #QTestState instance to operate on.
179 * @string: QOM path of a device.
181 * Associate qtest irqs with the GPIO-out pins of the device
182 * whose path is specified by @string.
184 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
188 * @s: #QTestState instance to operate on.
189 * @addr: I/O port to write to.
190 * @value: Value being written.
192 * Write an 8-bit value to an I/O port.
194 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
198 * @s: #QTestState instance to operate on.
199 * @addr: I/O port to write to.
200 * @value: Value being written.
202 * Write a 16-bit value to an I/O port.
204 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
208 * @s: #QTestState instance to operate on.
209 * @addr: I/O port to write to.
210 * @value: Value being written.
212 * Write a 32-bit value to an I/O port.
214 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
218 * @s: #QTestState instance to operate on.
219 * @addr: I/O port to read from.
221 * Returns an 8-bit value from an I/O port.
223 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
227 * @s: #QTestState instance to operate on.
228 * @addr: I/O port to read from.
230 * Returns a 16-bit value from an I/O port.
232 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
236 * @s: #QTestState instance to operate on.
237 * @addr: I/O port to read from.
239 * Returns a 32-bit value from an I/O port.
241 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
245 * @s: #QTestState instance to operate on.
246 * @addr: Guest address to write to.
247 * @value: Value being written.
249 * Writes an 8-bit value to memory.
251 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
255 * @s: #QTestState instance to operate on.
256 * @addr: Guest address to write to.
257 * @value: Value being written.
259 * Writes a 16-bit value to memory.
261 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
265 * @s: #QTestState instance to operate on.
266 * @addr: Guest address to write to.
267 * @value: Value being written.
269 * Writes a 32-bit value to memory.
271 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
275 * @s: #QTestState instance to operate on.
276 * @addr: Guest address to write to.
277 * @value: Value being written.
279 * Writes a 64-bit value to memory.
281 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
285 * @s: #QTestState instance to operate on.
286 * @addr: Guest address to read from.
288 * Reads an 8-bit value from memory.
290 * Returns: Value read.
292 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
296 * @s: #QTestState instance to operate on.
297 * @addr: Guest address to read from.
299 * Reads a 16-bit value from memory.
301 * Returns: Value read.
303 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
307 * @s: #QTestState instance to operate on.
308 * @addr: Guest address to read from.
310 * Reads a 32-bit value from memory.
312 * Returns: Value read.
314 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
318 * @s: #QTestState instance to operate on.
319 * @addr: Guest address to read from.
321 * Reads a 64-bit value from memory.
323 * Returns: Value read.
325 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
329 * @s: #QTestState instance to operate on.
330 * @addr: Guest address to read from.
331 * @data: Pointer to where memory contents will be stored.
332 * @size: Number of bytes to read.
334 * Read guest memory into a buffer.
336 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
340 * @s: #QTestState instance to operate on.
341 * @name: name of the command to call.
342 * @nargs: Number of args.
343 * @args: Guest address to read args from.
344 * @nret: Number of return value.
345 * @ret: Guest address to write return values to.
347 * Call an RTAS function
349 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
350 uint32_t nargs
, uint64_t args
,
351 uint32_t nret
, uint64_t ret
);
355 * @s: #QTestState instance to operate on.
356 * @addr: Guest address to read from.
357 * @data: Pointer to where memory contents will be stored.
358 * @size: Number of bytes to read.
360 * Read guest memory into a buffer and receive using a base64 encoding.
362 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
366 * @s: #QTestState instance to operate on.
367 * @addr: Guest address to write to.
368 * @data: Pointer to the bytes that will be written to guest memory.
369 * @size: Number of bytes to write.
371 * Write a buffer to guest memory.
373 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
377 * @s: #QTestState instance to operate on.
378 * @addr: Guest address to write to.
379 * @data: Pointer to the bytes that will be written to guest memory.
380 * @size: Number of bytes to write.
382 * Write a buffer to guest memory and transmit using a base64 encoding.
384 void qtest_bufwrite(QTestState
*s
, uint64_t addr
,
385 const void *data
, size_t size
);
389 * @s: #QTestState instance to operate on.
390 * @addr: Guest address to write to.
391 * @patt: Byte pattern to fill the guest memory region with.
392 * @size: Number of bytes to write.
394 * Write a pattern to guest memory.
396 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
399 * qtest_clock_step_next:
400 * @s: #QTestState instance to operate on.
402 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
404 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
406 int64_t qtest_clock_step_next(QTestState
*s
);
410 * @s: QTestState instance to operate on.
411 * @step: Number of nanoseconds to advance the clock by.
413 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
415 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
417 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
421 * @s: QTestState instance to operate on.
422 * @val: Nanoseconds value to advance the clock to.
424 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
426 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
428 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
432 * @s: QTestState instance to operate on.
434 * Returns: True if the architecture under test has a big endian configuration.
436 bool qtest_big_endian(QTestState
*s
);
441 * Returns: The architecture for the QEMU executable under test.
443 const char *qtest_get_arch(void);
447 * @str: Test case path.
448 * @fn: Test case function
450 * Add a GTester testcase with the given name and function.
451 * The path is prefixed with the architecture under test, as
452 * returned by qtest_get_arch().
454 void qtest_add_func(const char *str
, void (*fn
)(void));
457 * qtest_add_data_func:
458 * @str: Test case path.
459 * @data: Test case data
460 * @fn: Test case function
462 * Add a GTester testcase with the given name, data and function.
463 * The path is prefixed with the architecture under test, as
464 * returned by qtest_get_arch().
466 void qtest_add_data_func(const char *str
, const void *data
,
467 void (*fn
)(const void *));
470 * qtest_add_data_func_full:
471 * @str: Test case path.
472 * @data: Test case data
473 * @fn: Test case function
474 * @data_free_func: GDestroyNotify for data
476 * Add a GTester testcase with the given name, data and function.
477 * The path is prefixed with the architecture under test, as
478 * returned by qtest_get_arch().
480 * @data is passed to @data_free_func() on test completion.
482 void qtest_add_data_func_full(const char *str
, void *data
,
483 void (*fn
)(const void *),
484 GDestroyNotify data_free_func
);
488 * @testpath: Test case path
489 * @Fixture: Fixture type
490 * @tdata: Test case data
491 * @fsetup: Test case setup function
492 * @ftest: Test case function
493 * @fteardown: Test case teardown function
495 * Add a GTester testcase with the given name, data and functions.
496 * The path is prefixed with the architecture under test, as
497 * returned by qtest_get_arch().
499 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
501 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
502 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
506 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
);
510 * @args: other arguments to pass to QEMU
512 * Start QEMU and assign the resulting #QTestState to a global variable.
513 * The global variable is used by "shortcut" functions documented below.
515 * Returns: #QTestState instance.
517 static inline QTestState
*qtest_start(const char *args
)
519 global_qtest
= qtest_init(args
);
526 * Shut down the QEMU process started by qtest_start().
528 static inline void qtest_end(void)
530 qtest_quit(global_qtest
);
536 * @fmt...: QMP message to send to qemu
538 * Sends a QMP message to QEMU and returns the response.
540 QDict
*qmp(const char *fmt
, ...);
544 * @fmt...: QMP message to send to qemu
546 * Sends a QMP message to QEMU and leaves the response in the stream.
548 void qmp_async(const char *fmt
, ...);
551 * qmp_discard_response:
552 * @fmt...: QMP message to send to qemu
554 * Sends a QMP message to QEMU and consumes the response.
556 void qmp_discard_response(const char *fmt
, ...);
561 * Reads a QMP message from QEMU and returns the response.
563 static inline QDict
*qmp_receive(void)
565 return qtest_qmp_receive(global_qtest
);
570 * @s: #event event to wait for.
572 * Continuosly polls for QMP responses until it receives the desired event.
574 static inline void qmp_eventwait(const char *event
)
576 return qtest_qmp_eventwait(global_qtest
, event
);
581 * @s: #event event to wait for.
583 * Continuosly polls for QMP responses until it receives the desired event.
584 * Returns a copy of the event for further investigation.
586 static inline QDict
*qmp_eventwait_ref(const char *event
)
588 return qtest_qmp_eventwait_ref(global_qtest
, event
);
593 * @fmt...: HMP command to send to QEMU
595 * Send HMP command to QEMU via QMP's human-monitor-command.
597 * Returns: the command's output. The caller should g_free() it.
599 char *hmp(const char *fmt
, ...);
603 * @num: Interrupt to observe.
605 * Returns: The level of the @num interrupt.
607 static inline bool get_irq(int num
)
609 return qtest_get_irq(global_qtest
, num
);
614 * @string: QOM path of a device.
616 * Associate qtest irqs with the GPIO-in pins of the device
617 * whose path is specified by @string.
619 static inline void irq_intercept_in(const char *string
)
621 qtest_irq_intercept_in(global_qtest
, string
);
625 * qtest_irq_intercept_out:
626 * @string: QOM path of a device.
628 * Associate qtest irqs with the GPIO-out pins of the device
629 * whose path is specified by @string.
631 static inline void irq_intercept_out(const char *string
)
633 qtest_irq_intercept_out(global_qtest
, string
);
638 * @addr: I/O port to write to.
639 * @value: Value being written.
641 * Write an 8-bit value to an I/O port.
643 static inline void outb(uint16_t addr
, uint8_t value
)
645 qtest_outb(global_qtest
, addr
, value
);
650 * @addr: I/O port to write to.
651 * @value: Value being written.
653 * Write a 16-bit value to an I/O port.
655 static inline void outw(uint16_t addr
, uint16_t value
)
657 qtest_outw(global_qtest
, addr
, value
);
662 * @addr: I/O port to write to.
663 * @value: Value being written.
665 * Write a 32-bit value to an I/O port.
667 static inline void outl(uint16_t addr
, uint32_t value
)
669 qtest_outl(global_qtest
, addr
, value
);
674 * @addr: I/O port to read from.
676 * Reads an 8-bit value from an I/O port.
678 * Returns: Value read.
680 static inline uint8_t inb(uint16_t addr
)
682 return qtest_inb(global_qtest
, addr
);
687 * @addr: I/O port to read from.
689 * Reads a 16-bit value from an I/O port.
691 * Returns: Value read.
693 static inline uint16_t inw(uint16_t addr
)
695 return qtest_inw(global_qtest
, addr
);
700 * @addr: I/O port to read from.
702 * Reads a 32-bit value from an I/O port.
704 * Returns: Value read.
706 static inline uint32_t inl(uint16_t addr
)
708 return qtest_inl(global_qtest
, addr
);
713 * @addr: Guest address to write to.
714 * @value: Value being written.
716 * Writes an 8-bit value to guest memory.
718 static inline void writeb(uint64_t addr
, uint8_t value
)
720 qtest_writeb(global_qtest
, addr
, value
);
725 * @addr: Guest address to write to.
726 * @value: Value being written.
728 * Writes a 16-bit value to guest memory.
730 static inline void writew(uint64_t addr
, uint16_t value
)
732 qtest_writew(global_qtest
, addr
, value
);
737 * @addr: Guest address to write to.
738 * @value: Value being written.
740 * Writes a 32-bit value to guest memory.
742 static inline void writel(uint64_t addr
, uint32_t value
)
744 qtest_writel(global_qtest
, addr
, value
);
749 * @addr: Guest address to write to.
750 * @value: Value being written.
752 * Writes a 64-bit value to guest memory.
754 static inline void writeq(uint64_t addr
, uint64_t value
)
756 qtest_writeq(global_qtest
, addr
, value
);
761 * @addr: Guest address to read from.
763 * Reads an 8-bit value from guest memory.
765 * Returns: Value read.
767 static inline uint8_t readb(uint64_t addr
)
769 return qtest_readb(global_qtest
, addr
);
774 * @addr: Guest address to read from.
776 * Reads a 16-bit value from guest memory.
778 * Returns: Value read.
780 static inline uint16_t readw(uint64_t addr
)
782 return qtest_readw(global_qtest
, addr
);
787 * @addr: Guest address to read from.
789 * Reads a 32-bit value from guest memory.
791 * Returns: Value read.
793 static inline uint32_t readl(uint64_t addr
)
795 return qtest_readl(global_qtest
, addr
);
800 * @addr: Guest address to read from.
802 * Reads a 64-bit value from guest memory.
804 * Returns: Value read.
806 static inline uint64_t readq(uint64_t addr
)
808 return qtest_readq(global_qtest
, addr
);
813 * @addr: Guest address to read from.
814 * @data: Pointer to where memory contents will be stored.
815 * @size: Number of bytes to read.
817 * Read guest memory into a buffer.
819 static inline void memread(uint64_t addr
, void *data
, size_t size
)
821 qtest_memread(global_qtest
, addr
, data
, size
);
826 * @addr: Guest address to read from.
827 * @data: Pointer to where memory contents will be stored.
828 * @size: Number of bytes to read.
830 * Read guest memory into a buffer, receive using a base64 encoding.
832 static inline void bufread(uint64_t addr
, void *data
, size_t size
)
834 qtest_bufread(global_qtest
, addr
, data
, size
);
839 * @addr: Guest address to write to.
840 * @data: Pointer to the bytes that will be written to guest memory.
841 * @size: Number of bytes to write.
843 * Write a buffer to guest memory.
845 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
847 qtest_memwrite(global_qtest
, addr
, data
, size
);
852 * @addr: Guest address to write to.
853 * @data: Pointer to the bytes that will be written to guest memory.
854 * @size: Number of bytes to write.
856 * Write a buffer to guest memory, transmit using a base64 encoding.
858 static inline void bufwrite(uint64_t addr
, const void *data
, size_t size
)
860 qtest_bufwrite(global_qtest
, addr
, data
, size
);
865 * @addr: Guest address to write to.
866 * @patt: Byte pattern to fill the guest memory region with.
867 * @size: Number of bytes to write.
869 * Write a pattern to guest memory.
871 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
873 qtest_memset(global_qtest
, addr
, patt
, size
);
879 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
881 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
883 static inline int64_t clock_step_next(void)
885 return qtest_clock_step_next(global_qtest
);
890 * @step: Number of nanoseconds to advance the clock by.
892 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
894 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
896 static inline int64_t clock_step(int64_t step
)
898 return qtest_clock_step(global_qtest
, step
);
903 * @val: Nanoseconds value to advance the clock to.
905 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
907 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
909 static inline int64_t clock_set(int64_t val
)
911 return qtest_clock_set(global_qtest
, val
);
914 QDict
*qmp_fd_receive(int fd
);
915 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
);
916 void qmp_fd_send(int fd
, const char *fmt
, ...);
917 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
);
918 QDict
*qmp_fd(int fd
, const char *fmt
, ...);