virtio: use VRingMemoryRegionCaches for descriptor ring
[qemu/ar7.git] / tests / libqtest.h
blob90f182e1d86efcacd50d9ad8a8bd6d6a17388d02
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
8 * Authors:
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.
17 #ifndef LIBQTEST_H
18 #define LIBQTEST_H
20 #include "qapi/qmp/qdict.h"
22 typedef struct QTestState QTestState;
24 extern QTestState *global_qtest;
26 /**
27 * qtest_init:
28 * @extra_args: other arguments to pass to QEMU.
30 * Returns: #QTestState instance.
32 QTestState *qtest_init(const char *extra_args);
34 /**
35 * qtest_quit:
36 * @s: #QTestState instance to operate on.
38 * Shut down the QEMU process associated to @s.
40 void qtest_quit(QTestState *s);
42 /**
43 * qtest_qmp_discard_response:
44 * @s: #QTestState instance to operate on.
45 * @fmt...: QMP message to send to qemu
47 * Sends a QMP message to QEMU and consumes the response.
49 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
51 /**
52 * qtest_qmp:
53 * @s: #QTestState instance to operate on.
54 * @fmt...: QMP message to send to qemu
56 * Sends a QMP message to QEMU and returns the response.
58 QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
60 /**
61 * qtest_async_qmp:
62 * @s: #QTestState instance to operate on.
63 * @fmt...: QMP message to send to qemu
65 * Sends a QMP message to QEMU and leaves the response in the stream.
67 void qtest_async_qmp(QTestState *s, const char *fmt, ...);
69 /**
70 * qtest_qmpv_discard_response:
71 * @s: #QTestState instance to operate on.
72 * @fmt: QMP message to send to QEMU
73 * @ap: QMP message arguments
75 * Sends a QMP message to QEMU and consumes the response.
77 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
79 /**
80 * qtest_qmpv:
81 * @s: #QTestState instance to operate on.
82 * @fmt: QMP message to send to QEMU
83 * @ap: QMP message arguments
85 * Sends a QMP message to QEMU and returns the response.
87 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
89 /**
90 * qtest_async_qmpv:
91 * @s: #QTestState instance to operate on.
92 * @fmt: QMP message to send to QEMU
93 * @ap: QMP message arguments
95 * Sends a QMP message to QEMU and leaves the response in the stream.
97 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap);
99 /**
100 * qtest_receive:
101 * @s: #QTestState instance to operate on.
103 * Reads a QMP message from QEMU and returns the response.
105 QDict *qtest_qmp_receive(QTestState *s);
108 * qtest_qmp_eventwait:
109 * @s: #QTestState instance to operate on.
110 * @s: #event event to wait for.
112 * Continuosly polls for QMP responses until it receives the desired event.
114 void qtest_qmp_eventwait(QTestState *s, const char *event);
117 * qtest_qmp_eventwait_ref:
118 * @s: #QTestState instance to operate on.
119 * @s: #event event to wait for.
121 * Continuosly polls for QMP responses until it receives the desired event.
122 * Returns a copy of the event for further investigation.
124 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
127 * qtest_hmpv:
128 * @s: #QTestState instance to operate on.
129 * @fmt...: HMP command to send to QEMU
131 * Send HMP command to QEMU via QMP's human-monitor-command.
133 * Returns: the command's output. The caller should g_free() it.
135 char *qtest_hmp(QTestState *s, const char *fmt, ...);
138 * qtest_hmpv:
139 * @s: #QTestState instance to operate on.
140 * @fmt: HMP command to send to QEMU
141 * @ap: HMP command arguments
143 * Send HMP command to QEMU via QMP's human-monitor-command.
145 * Returns: the command's output. The caller should g_free() it.
147 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap);
150 * qtest_get_irq:
151 * @s: #QTestState instance to operate on.
152 * @num: Interrupt to observe.
154 * Returns: The level of the @num interrupt.
156 bool qtest_get_irq(QTestState *s, int num);
159 * qtest_irq_intercept_in:
160 * @s: #QTestState instance to operate on.
161 * @string: QOM path of a device.
163 * Associate qtest irqs with the GPIO-in pins of the device
164 * whose path is specified by @string.
166 void qtest_irq_intercept_in(QTestState *s, const char *string);
169 * qtest_irq_intercept_out:
170 * @s: #QTestState instance to operate on.
171 * @string: QOM path of a device.
173 * Associate qtest irqs with the GPIO-out pins of the device
174 * whose path is specified by @string.
176 void qtest_irq_intercept_out(QTestState *s, const char *string);
179 * qtest_outb:
180 * @s: #QTestState instance to operate on.
181 * @addr: I/O port to write to.
182 * @value: Value being written.
184 * Write an 8-bit value to an I/O port.
186 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
189 * qtest_outw:
190 * @s: #QTestState instance to operate on.
191 * @addr: I/O port to write to.
192 * @value: Value being written.
194 * Write a 16-bit value to an I/O port.
196 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
199 * qtest_outl:
200 * @s: #QTestState instance to operate on.
201 * @addr: I/O port to write to.
202 * @value: Value being written.
204 * Write a 32-bit value to an I/O port.
206 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
209 * qtest_inb:
210 * @s: #QTestState instance to operate on.
211 * @addr: I/O port to read from.
213 * Returns an 8-bit value from an I/O port.
215 uint8_t qtest_inb(QTestState *s, uint16_t addr);
218 * qtest_inw:
219 * @s: #QTestState instance to operate on.
220 * @addr: I/O port to read from.
222 * Returns a 16-bit value from an I/O port.
224 uint16_t qtest_inw(QTestState *s, uint16_t addr);
227 * qtest_inl:
228 * @s: #QTestState instance to operate on.
229 * @addr: I/O port to read from.
231 * Returns a 32-bit value from an I/O port.
233 uint32_t qtest_inl(QTestState *s, uint16_t addr);
236 * qtest_writeb:
237 * @s: #QTestState instance to operate on.
238 * @addr: Guest address to write to.
239 * @value: Value being written.
241 * Writes an 8-bit value to memory.
243 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
246 * qtest_writew:
247 * @s: #QTestState instance to operate on.
248 * @addr: Guest address to write to.
249 * @value: Value being written.
251 * Writes a 16-bit value to memory.
253 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
256 * qtest_writel:
257 * @s: #QTestState instance to operate on.
258 * @addr: Guest address to write to.
259 * @value: Value being written.
261 * Writes a 32-bit value to memory.
263 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
266 * qtest_writeq:
267 * @s: #QTestState instance to operate on.
268 * @addr: Guest address to write to.
269 * @value: Value being written.
271 * Writes a 64-bit value to memory.
273 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
276 * qtest_readb:
277 * @s: #QTestState instance to operate on.
278 * @addr: Guest address to read from.
280 * Reads an 8-bit value from memory.
282 * Returns: Value read.
284 uint8_t qtest_readb(QTestState *s, uint64_t addr);
287 * qtest_readw:
288 * @s: #QTestState instance to operate on.
289 * @addr: Guest address to read from.
291 * Reads a 16-bit value from memory.
293 * Returns: Value read.
295 uint16_t qtest_readw(QTestState *s, uint64_t addr);
298 * qtest_readl:
299 * @s: #QTestState instance to operate on.
300 * @addr: Guest address to read from.
302 * Reads a 32-bit value from memory.
304 * Returns: Value read.
306 uint32_t qtest_readl(QTestState *s, uint64_t addr);
309 * qtest_readq:
310 * @s: #QTestState instance to operate on.
311 * @addr: Guest address to read from.
313 * Reads a 64-bit value from memory.
315 * Returns: Value read.
317 uint64_t qtest_readq(QTestState *s, uint64_t addr);
320 * qtest_memread:
321 * @s: #QTestState instance to operate on.
322 * @addr: Guest address to read from.
323 * @data: Pointer to where memory contents will be stored.
324 * @size: Number of bytes to read.
326 * Read guest memory into a buffer.
328 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
331 * qtest_rtas_call:
332 * @s: #QTestState instance to operate on.
333 * @name: name of the command to call.
334 * @nargs: Number of args.
335 * @args: Guest address to read args from.
336 * @nret: Number of return value.
337 * @ret: Guest address to write return values to.
339 * Call an RTAS function
341 uint64_t qtest_rtas_call(QTestState *s, const char *name,
342 uint32_t nargs, uint64_t args,
343 uint32_t nret, uint64_t ret);
346 * qtest_bufread:
347 * @s: #QTestState instance to operate on.
348 * @addr: Guest address to read from.
349 * @data: Pointer to where memory contents will be stored.
350 * @size: Number of bytes to read.
352 * Read guest memory into a buffer and receive using a base64 encoding.
354 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
357 * qtest_memwrite:
358 * @s: #QTestState instance to operate on.
359 * @addr: Guest address to write to.
360 * @data: Pointer to the bytes that will be written to guest memory.
361 * @size: Number of bytes to write.
363 * Write a buffer to guest memory.
365 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
368 * qtest_bufwrite:
369 * @s: #QTestState instance to operate on.
370 * @addr: Guest address to write to.
371 * @data: Pointer to the bytes that will be written to guest memory.
372 * @size: Number of bytes to write.
374 * Write a buffer to guest memory and transmit using a base64 encoding.
376 void qtest_bufwrite(QTestState *s, uint64_t addr,
377 const void *data, size_t size);
380 * qtest_memset:
381 * @s: #QTestState instance to operate on.
382 * @addr: Guest address to write to.
383 * @patt: Byte pattern to fill the guest memory region with.
384 * @size: Number of bytes to write.
386 * Write a pattern to guest memory.
388 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
391 * qtest_clock_step_next:
392 * @s: #QTestState instance to operate on.
394 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
396 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
398 int64_t qtest_clock_step_next(QTestState *s);
401 * qtest_clock_step:
402 * @s: QTestState instance to operate on.
403 * @step: Number of nanoseconds to advance the clock by.
405 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
407 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
409 int64_t qtest_clock_step(QTestState *s, int64_t step);
412 * qtest_clock_set:
413 * @s: QTestState instance to operate on.
414 * @val: Nanoseconds value to advance the clock to.
416 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
418 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
420 int64_t qtest_clock_set(QTestState *s, int64_t val);
423 * qtest_big_endian:
424 * @s: QTestState instance to operate on.
426 * Returns: True if the architecture under test has a big endian configuration.
428 bool qtest_big_endian(QTestState *s);
431 * qtest_get_arch:
433 * Returns: The architecture for the QEMU executable under test.
435 const char *qtest_get_arch(void);
438 * qtest_add_func:
439 * @str: Test case path.
440 * @fn: Test case function
442 * Add a GTester testcase with the given name and function.
443 * The path is prefixed with the architecture under test, as
444 * returned by qtest_get_arch().
446 void qtest_add_func(const char *str, void (*fn)(void));
449 * qtest_add_data_func:
450 * @str: Test case path.
451 * @data: Test case data
452 * @fn: Test case function
454 * Add a GTester testcase with the given name, data and function.
455 * The path is prefixed with the architecture under test, as
456 * returned by qtest_get_arch().
458 void qtest_add_data_func(const char *str, const void *data,
459 void (*fn)(const void *));
462 * qtest_add_data_func_full:
463 * @str: Test case path.
464 * @data: Test case data
465 * @fn: Test case function
466 * @data_free_func: GDestroyNotify for data
468 * Add a GTester testcase with the given name, data and function.
469 * The path is prefixed with the architecture under test, as
470 * returned by qtest_get_arch().
472 * @data is passed to @data_free_func() on test completion.
474 void qtest_add_data_func_full(const char *str, void *data,
475 void (*fn)(const void *),
476 GDestroyNotify data_free_func);
479 * qtest_add:
480 * @testpath: Test case path
481 * @Fixture: Fixture type
482 * @tdata: Test case data
483 * @fsetup: Test case setup function
484 * @ftest: Test case function
485 * @fteardown: Test case teardown function
487 * Add a GTester testcase with the given name, data and functions.
488 * The path is prefixed with the architecture under test, as
489 * returned by qtest_get_arch().
491 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
492 do { \
493 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
494 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
495 g_free(path); \
496 } while (0)
498 void qtest_add_abrt_handler(GHookFunc fn, const void *data);
501 * qtest_start:
502 * @args: other arguments to pass to QEMU
504 * Start QEMU and assign the resulting #QTestState to a global variable.
505 * The global variable is used by "shortcut" functions documented below.
507 * Returns: #QTestState instance.
509 static inline QTestState *qtest_start(const char *args)
511 global_qtest = qtest_init(args);
512 return global_qtest;
516 * qtest_end:
518 * Shut down the QEMU process started by qtest_start().
520 static inline void qtest_end(void)
522 qtest_quit(global_qtest);
523 global_qtest = NULL;
527 * qmp:
528 * @fmt...: QMP message to send to qemu
530 * Sends a QMP message to QEMU and returns the response.
532 QDict *qmp(const char *fmt, ...);
535 * qmp_async:
536 * @fmt...: QMP message to send to qemu
538 * Sends a QMP message to QEMU and leaves the response in the stream.
540 void qmp_async(const char *fmt, ...);
543 * qmp_discard_response:
544 * @fmt...: QMP message to send to qemu
546 * Sends a QMP message to QEMU and consumes the response.
548 void qmp_discard_response(const char *fmt, ...);
551 * qmp_receive:
553 * Reads a QMP message from QEMU and returns the response.
555 static inline QDict *qmp_receive(void)
557 return qtest_qmp_receive(global_qtest);
561 * qmp_eventwait:
562 * @s: #event event to wait for.
564 * Continuosly polls for QMP responses until it receives the desired event.
566 static inline void qmp_eventwait(const char *event)
568 return qtest_qmp_eventwait(global_qtest, event);
572 * qmp_eventwait_ref:
573 * @s: #event event to wait for.
575 * Continuosly polls for QMP responses until it receives the desired event.
576 * Returns a copy of the event for further investigation.
578 static inline QDict *qmp_eventwait_ref(const char *event)
580 return qtest_qmp_eventwait_ref(global_qtest, event);
584 * hmp:
585 * @fmt...: HMP command to send to QEMU
587 * Send HMP command to QEMU via QMP's human-monitor-command.
589 * Returns: the command's output. The caller should g_free() it.
591 char *hmp(const char *fmt, ...);
594 * get_irq:
595 * @num: Interrupt to observe.
597 * Returns: The level of the @num interrupt.
599 static inline bool get_irq(int num)
601 return qtest_get_irq(global_qtest, num);
605 * irq_intercept_in:
606 * @string: QOM path of a device.
608 * Associate qtest irqs with the GPIO-in pins of the device
609 * whose path is specified by @string.
611 static inline void irq_intercept_in(const char *string)
613 qtest_irq_intercept_in(global_qtest, string);
617 * qtest_irq_intercept_out:
618 * @string: QOM path of a device.
620 * Associate qtest irqs with the GPIO-out pins of the device
621 * whose path is specified by @string.
623 static inline void irq_intercept_out(const char *string)
625 qtest_irq_intercept_out(global_qtest, string);
629 * outb:
630 * @addr: I/O port to write to.
631 * @value: Value being written.
633 * Write an 8-bit value to an I/O port.
635 static inline void outb(uint16_t addr, uint8_t value)
637 qtest_outb(global_qtest, addr, value);
641 * outw:
642 * @addr: I/O port to write to.
643 * @value: Value being written.
645 * Write a 16-bit value to an I/O port.
647 static inline void outw(uint16_t addr, uint16_t value)
649 qtest_outw(global_qtest, addr, value);
653 * outl:
654 * @addr: I/O port to write to.
655 * @value: Value being written.
657 * Write a 32-bit value to an I/O port.
659 static inline void outl(uint16_t addr, uint32_t value)
661 qtest_outl(global_qtest, addr, value);
665 * inb:
666 * @addr: I/O port to read from.
668 * Reads an 8-bit value from an I/O port.
670 * Returns: Value read.
672 static inline uint8_t inb(uint16_t addr)
674 return qtest_inb(global_qtest, addr);
678 * inw:
679 * @addr: I/O port to read from.
681 * Reads a 16-bit value from an I/O port.
683 * Returns: Value read.
685 static inline uint16_t inw(uint16_t addr)
687 return qtest_inw(global_qtest, addr);
691 * inl:
692 * @addr: I/O port to read from.
694 * Reads a 32-bit value from an I/O port.
696 * Returns: Value read.
698 static inline uint32_t inl(uint16_t addr)
700 return qtest_inl(global_qtest, addr);
704 * writeb:
705 * @addr: Guest address to write to.
706 * @value: Value being written.
708 * Writes an 8-bit value to guest memory.
710 static inline void writeb(uint64_t addr, uint8_t value)
712 qtest_writeb(global_qtest, addr, value);
716 * writew:
717 * @addr: Guest address to write to.
718 * @value: Value being written.
720 * Writes a 16-bit value to guest memory.
722 static inline void writew(uint64_t addr, uint16_t value)
724 qtest_writew(global_qtest, addr, value);
728 * writel:
729 * @addr: Guest address to write to.
730 * @value: Value being written.
732 * Writes a 32-bit value to guest memory.
734 static inline void writel(uint64_t addr, uint32_t value)
736 qtest_writel(global_qtest, addr, value);
740 * writeq:
741 * @addr: Guest address to write to.
742 * @value: Value being written.
744 * Writes a 64-bit value to guest memory.
746 static inline void writeq(uint64_t addr, uint64_t value)
748 qtest_writeq(global_qtest, addr, value);
752 * readb:
753 * @addr: Guest address to read from.
755 * Reads an 8-bit value from guest memory.
757 * Returns: Value read.
759 static inline uint8_t readb(uint64_t addr)
761 return qtest_readb(global_qtest, addr);
765 * readw:
766 * @addr: Guest address to read from.
768 * Reads a 16-bit value from guest memory.
770 * Returns: Value read.
772 static inline uint16_t readw(uint64_t addr)
774 return qtest_readw(global_qtest, addr);
778 * readl:
779 * @addr: Guest address to read from.
781 * Reads a 32-bit value from guest memory.
783 * Returns: Value read.
785 static inline uint32_t readl(uint64_t addr)
787 return qtest_readl(global_qtest, addr);
791 * readq:
792 * @addr: Guest address to read from.
794 * Reads a 64-bit value from guest memory.
796 * Returns: Value read.
798 static inline uint64_t readq(uint64_t addr)
800 return qtest_readq(global_qtest, addr);
804 * memread:
805 * @addr: Guest address to read from.
806 * @data: Pointer to where memory contents will be stored.
807 * @size: Number of bytes to read.
809 * Read guest memory into a buffer.
811 static inline void memread(uint64_t addr, void *data, size_t size)
813 qtest_memread(global_qtest, addr, data, size);
817 * bufread:
818 * @addr: Guest address to read from.
819 * @data: Pointer to where memory contents will be stored.
820 * @size: Number of bytes to read.
822 * Read guest memory into a buffer, receive using a base64 encoding.
824 static inline void bufread(uint64_t addr, void *data, size_t size)
826 qtest_bufread(global_qtest, addr, data, size);
830 * memwrite:
831 * @addr: Guest address to write to.
832 * @data: Pointer to the bytes that will be written to guest memory.
833 * @size: Number of bytes to write.
835 * Write a buffer to guest memory.
837 static inline void memwrite(uint64_t addr, const void *data, size_t size)
839 qtest_memwrite(global_qtest, addr, data, size);
843 * bufwrite:
844 * @addr: Guest address to write to.
845 * @data: Pointer to the bytes that will be written to guest memory.
846 * @size: Number of bytes to write.
848 * Write a buffer to guest memory, transmit using a base64 encoding.
850 static inline void bufwrite(uint64_t addr, const void *data, size_t size)
852 qtest_bufwrite(global_qtest, addr, data, size);
856 * qmemset:
857 * @addr: Guest address to write to.
858 * @patt: Byte pattern to fill the guest memory region with.
859 * @size: Number of bytes to write.
861 * Write a pattern to guest memory.
863 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
865 qtest_memset(global_qtest, addr, patt, size);
869 * clock_step_next:
871 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
873 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
875 static inline int64_t clock_step_next(void)
877 return qtest_clock_step_next(global_qtest);
881 * clock_step:
882 * @step: Number of nanoseconds to advance the clock by.
884 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
886 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
888 static inline int64_t clock_step(int64_t step)
890 return qtest_clock_step(global_qtest, step);
894 * clock_set:
895 * @val: Nanoseconds value to advance the clock to.
897 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
899 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
901 static inline int64_t clock_set(int64_t val)
903 return qtest_clock_set(global_qtest, val);
906 QDict *qmp_fd_receive(int fd);
907 void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
908 void qmp_fd_send(int fd, const char *fmt, ...);
909 QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
910 QDict *qmp_fd(int fd, const char *fmt, ...);
912 #endif