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.
24 #include <sys/types.h>
25 #include "qapi/qmp/qdict.h"
26 #include "glib-compat.h"
28 typedef struct QTestState QTestState
;
30 extern QTestState
*global_qtest
;
34 * @extra_args: other arguments to pass to QEMU.
36 * Returns: #QTestState instance.
38 QTestState
*qtest_init(const char *extra_args
);
42 * @s: #QTestState instance to operate on.
44 * Shut down the QEMU process associated to @s.
46 void qtest_quit(QTestState
*s
);
49 * qtest_qmp_discard_response:
50 * @s: #QTestState instance to operate on.
51 * @fmt...: QMP message to send to qemu
53 * Sends a QMP message to QEMU and consumes the response.
55 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...);
59 * @s: #QTestState instance to operate on.
60 * @fmt...: QMP message to send to qemu
62 * Sends a QMP message to QEMU and returns the response.
64 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...);
67 * qtest_qmpv_discard_response:
68 * @s: #QTestState instance to operate on.
69 * @fmt: QMP message to send to QEMU
70 * @ap: QMP message arguments
72 * Sends a QMP message to QEMU and consumes the response.
74 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
);
78 * @s: #QTestState instance to operate on.
79 * @fmt: QMP message to send to QEMU
80 * @ap: QMP message arguments
82 * Sends a QMP message to QEMU and returns the response.
84 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
);
88 * @s: #QTestState instance to operate on.
90 * Reads a QMP message from QEMU and returns the response.
92 QDict
*qtest_qmp_receive(QTestState
*s
);
95 * qtest_qmp_eventwait:
96 * @s: #QTestState instance to operate on.
97 * @s: #event event to wait for.
99 * Continuosly polls for QMP responses until it receives the desired event.
101 void qtest_qmp_eventwait(QTestState
*s
, const char *event
);
105 * @s: #QTestState instance to operate on.
106 * @num: Interrupt to observe.
108 * Returns: The level of the @num interrupt.
110 bool qtest_get_irq(QTestState
*s
, int num
);
113 * qtest_irq_intercept_in:
114 * @s: #QTestState instance to operate on.
115 * @string: QOM path of a device.
117 * Associate qtest irqs with the GPIO-in pins of the device
118 * whose path is specified by @string.
120 void qtest_irq_intercept_in(QTestState
*s
, const char *string
);
123 * qtest_irq_intercept_out:
124 * @s: #QTestState instance to operate on.
125 * @string: QOM path of a device.
127 * Associate qtest irqs with the GPIO-out pins of the device
128 * whose path is specified by @string.
130 void qtest_irq_intercept_out(QTestState
*s
, const char *string
);
134 * @s: #QTestState instance to operate on.
135 * @addr: I/O port to write to.
136 * @value: Value being written.
138 * Write an 8-bit value to an I/O port.
140 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
);
144 * @s: #QTestState instance to operate on.
145 * @addr: I/O port to write to.
146 * @value: Value being written.
148 * Write a 16-bit value to an I/O port.
150 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
);
154 * @s: #QTestState instance to operate on.
155 * @addr: I/O port to write to.
156 * @value: Value being written.
158 * Write a 32-bit value to an I/O port.
160 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
);
164 * @s: #QTestState instance to operate on.
165 * @addr: I/O port to read from.
167 * Returns an 8-bit value from an I/O port.
169 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
);
173 * @s: #QTestState instance to operate on.
174 * @addr: I/O port to read from.
176 * Returns a 16-bit value from an I/O port.
178 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
);
182 * @s: #QTestState instance to operate on.
183 * @addr: I/O port to read from.
185 * Returns a 32-bit value from an I/O port.
187 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
);
191 * @s: #QTestState instance to operate on.
192 * @addr: Guest address to write to.
193 * @value: Value being written.
195 * Writes an 8-bit value to memory.
197 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
);
201 * @s: #QTestState instance to operate on.
202 * @addr: Guest address to write to.
203 * @value: Value being written.
205 * Writes a 16-bit value to memory.
207 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
);
211 * @s: #QTestState instance to operate on.
212 * @addr: Guest address to write to.
213 * @value: Value being written.
215 * Writes a 32-bit value to memory.
217 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
);
221 * @s: #QTestState instance to operate on.
222 * @addr: Guest address to write to.
223 * @value: Value being written.
225 * Writes a 64-bit value to memory.
227 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
);
231 * @s: #QTestState instance to operate on.
232 * @addr: Guest address to read from.
234 * Reads an 8-bit value from memory.
236 * Returns: Value read.
238 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
);
242 * @s: #QTestState instance to operate on.
243 * @addr: Guest address to read from.
245 * Reads a 16-bit value from memory.
247 * Returns: Value read.
249 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
);
253 * @s: #QTestState instance to operate on.
254 * @addr: Guest address to read from.
256 * Reads a 32-bit value from memory.
258 * Returns: Value read.
260 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
);
264 * @s: #QTestState instance to operate on.
265 * @addr: Guest address to read from.
267 * Reads a 64-bit value from memory.
269 * Returns: Value read.
271 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
);
275 * @s: #QTestState instance to operate on.
276 * @addr: Guest address to read from.
277 * @data: Pointer to where memory contents will be stored.
278 * @size: Number of bytes to read.
280 * Read guest memory into a buffer.
282 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
);
286 * @s: #QTestState instance to operate on.
287 * @addr: Guest address to write to.
288 * @data: Pointer to the bytes that will be written to guest memory.
289 * @size: Number of bytes to write.
291 * Write a buffer to guest memory.
293 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
);
297 * @s: #QTestState instance to operate on.
298 * @addr: Guest address to write to.
299 * @patt: Byte pattern to fill the guest memory region with.
300 * @size: Number of bytes to write.
302 * Write a pattern to guest memory.
304 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t patt
, size_t size
);
307 * qtest_clock_step_next:
308 * @s: #QTestState instance to operate on.
310 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
312 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
314 int64_t qtest_clock_step_next(QTestState
*s
);
318 * @s: QTestState instance to operate on.
319 * @step: Number of nanoseconds to advance the clock by.
321 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
323 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
325 int64_t qtest_clock_step(QTestState
*s
, int64_t step
);
329 * @s: QTestState instance to operate on.
330 * @val: Nanoseconds value to advance the clock to.
332 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
334 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
336 int64_t qtest_clock_set(QTestState
*s
, int64_t val
);
341 * Returns: The architecture for the QEMU executable under test.
343 const char *qtest_get_arch(void);
347 * @str: Test case path.
348 * @fn: Test case function
350 * Add a GTester testcase with the given name and function.
351 * The path is prefixed with the architecture under test, as
352 * returned by qtest_get_arch().
354 void qtest_add_func(const char *str
, void (*fn
));
357 * qtest_add_data_func:
358 * @str: Test case path.
359 * @data: Test case data
360 * @fn: Test case function
362 * Add a GTester testcase with the given name, data and function.
363 * The path is prefixed with the architecture under test, as
364 * returned by qtest_get_arch().
366 void qtest_add_data_func(const char *str
, const void *data
, void (*fn
));
370 * @testpath: Test case path
371 * @Fixture: Fixture type
372 * @tdata: Test case data
373 * @fsetup: Test case setup function
374 * @ftest: Test case function
375 * @fteardown: Test case teardown function
377 * Add a GTester testcase with the given name, data and functions.
378 * The path is prefixed with the architecture under test, as
379 * returned by qtest_get_arch().
381 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
383 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
384 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
390 * @args: other arguments to pass to QEMU
392 * Start QEMU and assign the resulting #QTestState to a global variable.
393 * The global variable is used by "shortcut" functions documented below.
395 * Returns: #QTestState instance.
397 static inline QTestState
*qtest_start(const char *args
)
399 global_qtest
= qtest_init(args
);
406 * Shut down the QEMU process started by qtest_start().
408 static inline void qtest_end(void)
410 qtest_quit(global_qtest
);
416 * @fmt...: QMP message to send to qemu
418 * Sends a QMP message to QEMU and returns the response.
420 QDict
*qmp(const char *fmt
, ...);
423 * qmp_discard_response:
424 * @fmt...: QMP message to send to qemu
426 * Sends a QMP message to QEMU and consumes the response.
428 void qmp_discard_response(const char *fmt
, ...);
433 * Reads a QMP message from QEMU and returns the response.
435 static inline QDict
*qmp_receive(void)
437 return qtest_qmp_receive(global_qtest
);
442 * @s: #event event to wait for.
444 * Continuosly polls for QMP responses until it receives the desired event.
446 static inline void qmp_eventwait(const char *event
)
448 return qtest_qmp_eventwait(global_qtest
, event
);
453 * @num: Interrupt to observe.
455 * Returns: The level of the @num interrupt.
457 static inline bool get_irq(int num
)
459 return qtest_get_irq(global_qtest
, num
);
464 * @string: QOM path of a device.
466 * Associate qtest irqs with the GPIO-in pins of the device
467 * whose path is specified by @string.
469 static inline void irq_intercept_in(const char *string
)
471 qtest_irq_intercept_in(global_qtest
, string
);
475 * qtest_irq_intercept_out:
476 * @string: QOM path of a device.
478 * Associate qtest irqs with the GPIO-out pins of the device
479 * whose path is specified by @string.
481 static inline void irq_intercept_out(const char *string
)
483 qtest_irq_intercept_out(global_qtest
, string
);
488 * @addr: I/O port to write to.
489 * @value: Value being written.
491 * Write an 8-bit value to an I/O port.
493 static inline void outb(uint16_t addr
, uint8_t value
)
495 qtest_outb(global_qtest
, addr
, value
);
500 * @addr: I/O port to write to.
501 * @value: Value being written.
503 * Write a 16-bit value to an I/O port.
505 static inline void outw(uint16_t addr
, uint16_t value
)
507 qtest_outw(global_qtest
, addr
, value
);
512 * @addr: I/O port to write to.
513 * @value: Value being written.
515 * Write a 32-bit value to an I/O port.
517 static inline void outl(uint16_t addr
, uint32_t value
)
519 qtest_outl(global_qtest
, addr
, value
);
524 * @addr: I/O port to read from.
526 * Reads an 8-bit value from an I/O port.
528 * Returns: Value read.
530 static inline uint8_t inb(uint16_t addr
)
532 return qtest_inb(global_qtest
, addr
);
537 * @addr: I/O port to read from.
539 * Reads a 16-bit value from an I/O port.
541 * Returns: Value read.
543 static inline uint16_t inw(uint16_t addr
)
545 return qtest_inw(global_qtest
, addr
);
550 * @addr: I/O port to read from.
552 * Reads a 32-bit value from an I/O port.
554 * Returns: Value read.
556 static inline uint32_t inl(uint16_t addr
)
558 return qtest_inl(global_qtest
, addr
);
563 * @addr: Guest address to write to.
564 * @value: Value being written.
566 * Writes an 8-bit value to guest memory.
568 static inline void writeb(uint64_t addr
, uint8_t value
)
570 qtest_writeb(global_qtest
, addr
, value
);
575 * @addr: Guest address to write to.
576 * @value: Value being written.
578 * Writes a 16-bit value to guest memory.
580 static inline void writew(uint64_t addr
, uint16_t value
)
582 qtest_writew(global_qtest
, addr
, value
);
587 * @addr: Guest address to write to.
588 * @value: Value being written.
590 * Writes a 32-bit value to guest memory.
592 static inline void writel(uint64_t addr
, uint32_t value
)
594 qtest_writel(global_qtest
, addr
, value
);
599 * @addr: Guest address to write to.
600 * @value: Value being written.
602 * Writes a 64-bit value to guest memory.
604 static inline void writeq(uint64_t addr
, uint64_t value
)
606 qtest_writeq(global_qtest
, addr
, value
);
611 * @addr: Guest address to read from.
613 * Reads an 8-bit value from guest memory.
615 * Returns: Value read.
617 static inline uint8_t readb(uint64_t addr
)
619 return qtest_readb(global_qtest
, addr
);
624 * @addr: Guest address to read from.
626 * Reads a 16-bit value from guest memory.
628 * Returns: Value read.
630 static inline uint16_t readw(uint64_t addr
)
632 return qtest_readw(global_qtest
, addr
);
637 * @addr: Guest address to read from.
639 * Reads a 32-bit value from guest memory.
641 * Returns: Value read.
643 static inline uint32_t readl(uint64_t addr
)
645 return qtest_readl(global_qtest
, addr
);
650 * @addr: Guest address to read from.
652 * Reads a 64-bit value from guest memory.
654 * Returns: Value read.
656 static inline uint64_t readq(uint64_t addr
)
658 return qtest_readq(global_qtest
, addr
);
663 * @addr: Guest address to read from.
664 * @data: Pointer to where memory contents will be stored.
665 * @size: Number of bytes to read.
667 * Read guest memory into a buffer.
669 static inline void memread(uint64_t addr
, void *data
, size_t size
)
671 qtest_memread(global_qtest
, addr
, data
, size
);
676 * @addr: Guest address to write to.
677 * @data: Pointer to the bytes that will be written to guest memory.
678 * @size: Number of bytes to write.
680 * Write a buffer to guest memory.
682 static inline void memwrite(uint64_t addr
, const void *data
, size_t size
)
684 qtest_memwrite(global_qtest
, addr
, data
, size
);
689 * @addr: Guest address to write to.
690 * @patt: Byte pattern to fill the guest memory region with.
691 * @size: Number of bytes to write.
693 * Write a pattern to guest memory.
695 static inline void qmemset(uint64_t addr
, uint8_t patt
, size_t size
)
697 qtest_memset(global_qtest
, addr
, patt
, size
);
703 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
705 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
707 static inline int64_t clock_step_next(void)
709 return qtest_clock_step_next(global_qtest
);
714 * @step: Number of nanoseconds to advance the clock by.
716 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
718 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
720 static inline int64_t clock_step(int64_t step
)
722 return qtest_clock_step(global_qtest
, step
);
727 * @val: Nanoseconds value to advance the clock to.
729 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
731 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
733 static inline int64_t clock_set(int64_t val
)
735 return qtest_clock_set(global_qtest
, val
);
741 * Returns: True if the architecture under test has a big endian configuration.
743 bool qtest_big_endian(void);