Merge remote-tracking branch 'remotes/ehabkost/tags/x86-pull-request' into staging
[qemu.git] / tests / libqtest.h
blobec4203152333378a9e58ae000cc5276847e95185
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 <stddef.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <stdarg.h>
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;
32 /**
33 * qtest_init:
34 * @extra_args: other arguments to pass to QEMU.
36 * Returns: #QTestState instance.
38 QTestState *qtest_init(const char *extra_args);
40 /**
41 * qtest_quit:
42 * @s: #QTestState instance to operate on.
44 * Shut down the QEMU process associated to @s.
46 void qtest_quit(QTestState *s);
48 /**
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, ...);
57 /**
58 * qtest_qmp:
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, ...);
66 /**
67 * qtest_async_qmp:
68 * @s: #QTestState instance to operate on.
69 * @fmt...: QMP message to send to qemu
71 * Sends a QMP message to QEMU and leaves the response in the stream.
73 void qtest_async_qmp(QTestState *s, const char *fmt, ...);
75 /**
76 * qtest_qmpv_discard_response:
77 * @s: #QTestState instance to operate on.
78 * @fmt: QMP message to send to QEMU
79 * @ap: QMP message arguments
81 * Sends a QMP message to QEMU and consumes the response.
83 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
85 /**
86 * qtest_qmpv:
87 * @s: #QTestState instance to operate on.
88 * @fmt: QMP message to send to QEMU
89 * @ap: QMP message arguments
91 * Sends a QMP message to QEMU and returns the response.
93 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
95 /**
96 * qtest_async_qmpv:
97 * @s: #QTestState instance to operate on.
98 * @fmt: QMP message to send to QEMU
99 * @ap: QMP message arguments
101 * Sends a QMP message to QEMU and leaves the response in the stream.
103 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap);
106 * qtest_receive:
107 * @s: #QTestState instance to operate on.
109 * Reads a QMP message from QEMU and returns the response.
111 QDict *qtest_qmp_receive(QTestState *s);
114 * qtest_qmp_eventwait:
115 * @s: #QTestState instance to operate on.
116 * @s: #event event to wait for.
118 * Continuosly polls for QMP responses until it receives the desired event.
120 void qtest_qmp_eventwait(QTestState *s, const char *event);
123 * qtest_get_irq:
124 * @s: #QTestState instance to operate on.
125 * @num: Interrupt to observe.
127 * Returns: The level of the @num interrupt.
129 bool qtest_get_irq(QTestState *s, int num);
132 * qtest_irq_intercept_in:
133 * @s: #QTestState instance to operate on.
134 * @string: QOM path of a device.
136 * Associate qtest irqs with the GPIO-in pins of the device
137 * whose path is specified by @string.
139 void qtest_irq_intercept_in(QTestState *s, const char *string);
142 * qtest_irq_intercept_out:
143 * @s: #QTestState instance to operate on.
144 * @string: QOM path of a device.
146 * Associate qtest irqs with the GPIO-out pins of the device
147 * whose path is specified by @string.
149 void qtest_irq_intercept_out(QTestState *s, const char *string);
152 * qtest_outb:
153 * @s: #QTestState instance to operate on.
154 * @addr: I/O port to write to.
155 * @value: Value being written.
157 * Write an 8-bit value to an I/O port.
159 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
162 * qtest_outw:
163 * @s: #QTestState instance to operate on.
164 * @addr: I/O port to write to.
165 * @value: Value being written.
167 * Write a 16-bit value to an I/O port.
169 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
172 * qtest_outl:
173 * @s: #QTestState instance to operate on.
174 * @addr: I/O port to write to.
175 * @value: Value being written.
177 * Write a 32-bit value to an I/O port.
179 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
182 * qtest_inb:
183 * @s: #QTestState instance to operate on.
184 * @addr: I/O port to read from.
186 * Returns an 8-bit value from an I/O port.
188 uint8_t qtest_inb(QTestState *s, uint16_t addr);
191 * qtest_inw:
192 * @s: #QTestState instance to operate on.
193 * @addr: I/O port to read from.
195 * Returns a 16-bit value from an I/O port.
197 uint16_t qtest_inw(QTestState *s, uint16_t addr);
200 * qtest_inl:
201 * @s: #QTestState instance to operate on.
202 * @addr: I/O port to read from.
204 * Returns a 32-bit value from an I/O port.
206 uint32_t qtest_inl(QTestState *s, uint16_t addr);
209 * qtest_writeb:
210 * @s: #QTestState instance to operate on.
211 * @addr: Guest address to write to.
212 * @value: Value being written.
214 * Writes an 8-bit value to memory.
216 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
219 * qtest_writew:
220 * @s: #QTestState instance to operate on.
221 * @addr: Guest address to write to.
222 * @value: Value being written.
224 * Writes a 16-bit value to memory.
226 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
229 * qtest_writel:
230 * @s: #QTestState instance to operate on.
231 * @addr: Guest address to write to.
232 * @value: Value being written.
234 * Writes a 32-bit value to memory.
236 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
239 * qtest_writeq:
240 * @s: #QTestState instance to operate on.
241 * @addr: Guest address to write to.
242 * @value: Value being written.
244 * Writes a 64-bit value to memory.
246 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
249 * qtest_readb:
250 * @s: #QTestState instance to operate on.
251 * @addr: Guest address to read from.
253 * Reads an 8-bit value from memory.
255 * Returns: Value read.
257 uint8_t qtest_readb(QTestState *s, uint64_t addr);
260 * qtest_readw:
261 * @s: #QTestState instance to operate on.
262 * @addr: Guest address to read from.
264 * Reads a 16-bit value from memory.
266 * Returns: Value read.
268 uint16_t qtest_readw(QTestState *s, uint64_t addr);
271 * qtest_readl:
272 * @s: #QTestState instance to operate on.
273 * @addr: Guest address to read from.
275 * Reads a 32-bit value from memory.
277 * Returns: Value read.
279 uint32_t qtest_readl(QTestState *s, uint64_t addr);
282 * qtest_readq:
283 * @s: #QTestState instance to operate on.
284 * @addr: Guest address to read from.
286 * Reads a 64-bit value from memory.
288 * Returns: Value read.
290 uint64_t qtest_readq(QTestState *s, uint64_t addr);
293 * qtest_memread:
294 * @s: #QTestState instance to operate on.
295 * @addr: Guest address to read from.
296 * @data: Pointer to where memory contents will be stored.
297 * @size: Number of bytes to read.
299 * Read guest memory into a buffer.
301 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
304 * qtest_bufread:
305 * @s: #QTestState instance to operate on.
306 * @addr: Guest address to read from.
307 * @data: Pointer to where memory contents will be stored.
308 * @size: Number of bytes to read.
310 * Read guest memory into a buffer and receive using a base64 encoding.
312 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
315 * qtest_memwrite:
316 * @s: #QTestState instance to operate on.
317 * @addr: Guest address to write to.
318 * @data: Pointer to the bytes that will be written to guest memory.
319 * @size: Number of bytes to write.
321 * Write a buffer to guest memory.
323 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
326 * qtest_bufwrite:
327 * @s: #QTestState instance to operate on.
328 * @addr: Guest address to write to.
329 * @data: Pointer to the bytes that will be written to guest memory.
330 * @size: Number of bytes to write.
332 * Write a buffer to guest memory and transmit using a base64 encoding.
334 void qtest_bufwrite(QTestState *s, uint64_t addr,
335 const void *data, size_t size);
338 * qtest_memset:
339 * @s: #QTestState instance to operate on.
340 * @addr: Guest address to write to.
341 * @patt: Byte pattern to fill the guest memory region with.
342 * @size: Number of bytes to write.
344 * Write a pattern to guest memory.
346 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
349 * qtest_clock_step_next:
350 * @s: #QTestState instance to operate on.
352 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
354 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
356 int64_t qtest_clock_step_next(QTestState *s);
359 * qtest_clock_step:
360 * @s: QTestState instance to operate on.
361 * @step: Number of nanoseconds to advance the clock by.
363 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
365 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
367 int64_t qtest_clock_step(QTestState *s, int64_t step);
370 * qtest_clock_set:
371 * @s: QTestState instance to operate on.
372 * @val: Nanoseconds value to advance the clock to.
374 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
376 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
378 int64_t qtest_clock_set(QTestState *s, int64_t val);
381 * qtest_get_arch:
383 * Returns: The architecture for the QEMU executable under test.
385 const char *qtest_get_arch(void);
388 * qtest_add_func:
389 * @str: Test case path.
390 * @fn: Test case function
392 * Add a GTester testcase with the given name and function.
393 * The path is prefixed with the architecture under test, as
394 * returned by qtest_get_arch().
396 void qtest_add_func(const char *str, void (*fn));
399 * qtest_add_data_func:
400 * @str: Test case path.
401 * @data: Test case data
402 * @fn: Test case function
404 * Add a GTester testcase with the given name, data and function.
405 * The path is prefixed with the architecture under test, as
406 * returned by qtest_get_arch().
408 void qtest_add_data_func(const char *str, const void *data, void (*fn));
411 * qtest_add:
412 * @testpath: Test case path
413 * @Fixture: Fixture type
414 * @tdata: Test case data
415 * @fsetup: Test case setup function
416 * @ftest: Test case function
417 * @fteardown: Test case teardown function
419 * Add a GTester testcase with the given name, data and functions.
420 * The path is prefixed with the architecture under test, as
421 * returned by qtest_get_arch().
423 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
424 do { \
425 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
426 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
427 g_free(path); \
428 } while (0)
431 * qtest_start:
432 * @args: other arguments to pass to QEMU
434 * Start QEMU and assign the resulting #QTestState to a global variable.
435 * The global variable is used by "shortcut" functions documented below.
437 * Returns: #QTestState instance.
439 static inline QTestState *qtest_start(const char *args)
441 global_qtest = qtest_init(args);
442 return global_qtest;
446 * qtest_end:
448 * Shut down the QEMU process started by qtest_start().
450 static inline void qtest_end(void)
452 qtest_quit(global_qtest);
453 global_qtest = NULL;
457 * qmp:
458 * @fmt...: QMP message to send to qemu
460 * Sends a QMP message to QEMU and returns the response.
462 QDict *qmp(const char *fmt, ...);
465 * qmp_async:
466 * @fmt...: QMP message to send to qemu
468 * Sends a QMP message to QEMU and leaves the response in the stream.
470 void qmp_async(const char *fmt, ...);
473 * qmp_discard_response:
474 * @fmt...: QMP message to send to qemu
476 * Sends a QMP message to QEMU and consumes the response.
478 void qmp_discard_response(const char *fmt, ...);
481 * qmp_receive:
483 * Reads a QMP message from QEMU and returns the response.
485 static inline QDict *qmp_receive(void)
487 return qtest_qmp_receive(global_qtest);
491 * qmp_eventwait:
492 * @s: #event event to wait for.
494 * Continuosly polls for QMP responses until it receives the desired event.
496 static inline void qmp_eventwait(const char *event)
498 return qtest_qmp_eventwait(global_qtest, event);
502 * get_irq:
503 * @num: Interrupt to observe.
505 * Returns: The level of the @num interrupt.
507 static inline bool get_irq(int num)
509 return qtest_get_irq(global_qtest, num);
513 * irq_intercept_in:
514 * @string: QOM path of a device.
516 * Associate qtest irqs with the GPIO-in pins of the device
517 * whose path is specified by @string.
519 static inline void irq_intercept_in(const char *string)
521 qtest_irq_intercept_in(global_qtest, string);
525 * qtest_irq_intercept_out:
526 * @string: QOM path of a device.
528 * Associate qtest irqs with the GPIO-out pins of the device
529 * whose path is specified by @string.
531 static inline void irq_intercept_out(const char *string)
533 qtest_irq_intercept_out(global_qtest, string);
537 * outb:
538 * @addr: I/O port to write to.
539 * @value: Value being written.
541 * Write an 8-bit value to an I/O port.
543 static inline void outb(uint16_t addr, uint8_t value)
545 qtest_outb(global_qtest, addr, value);
549 * outw:
550 * @addr: I/O port to write to.
551 * @value: Value being written.
553 * Write a 16-bit value to an I/O port.
555 static inline void outw(uint16_t addr, uint16_t value)
557 qtest_outw(global_qtest, addr, value);
561 * outl:
562 * @addr: I/O port to write to.
563 * @value: Value being written.
565 * Write a 32-bit value to an I/O port.
567 static inline void outl(uint16_t addr, uint32_t value)
569 qtest_outl(global_qtest, addr, value);
573 * inb:
574 * @addr: I/O port to read from.
576 * Reads an 8-bit value from an I/O port.
578 * Returns: Value read.
580 static inline uint8_t inb(uint16_t addr)
582 return qtest_inb(global_qtest, addr);
586 * inw:
587 * @addr: I/O port to read from.
589 * Reads a 16-bit value from an I/O port.
591 * Returns: Value read.
593 static inline uint16_t inw(uint16_t addr)
595 return qtest_inw(global_qtest, addr);
599 * inl:
600 * @addr: I/O port to read from.
602 * Reads a 32-bit value from an I/O port.
604 * Returns: Value read.
606 static inline uint32_t inl(uint16_t addr)
608 return qtest_inl(global_qtest, addr);
612 * writeb:
613 * @addr: Guest address to write to.
614 * @value: Value being written.
616 * Writes an 8-bit value to guest memory.
618 static inline void writeb(uint64_t addr, uint8_t value)
620 qtest_writeb(global_qtest, addr, value);
624 * writew:
625 * @addr: Guest address to write to.
626 * @value: Value being written.
628 * Writes a 16-bit value to guest memory.
630 static inline void writew(uint64_t addr, uint16_t value)
632 qtest_writew(global_qtest, addr, value);
636 * writel:
637 * @addr: Guest address to write to.
638 * @value: Value being written.
640 * Writes a 32-bit value to guest memory.
642 static inline void writel(uint64_t addr, uint32_t value)
644 qtest_writel(global_qtest, addr, value);
648 * writeq:
649 * @addr: Guest address to write to.
650 * @value: Value being written.
652 * Writes a 64-bit value to guest memory.
654 static inline void writeq(uint64_t addr, uint64_t value)
656 qtest_writeq(global_qtest, addr, value);
660 * readb:
661 * @addr: Guest address to read from.
663 * Reads an 8-bit value from guest memory.
665 * Returns: Value read.
667 static inline uint8_t readb(uint64_t addr)
669 return qtest_readb(global_qtest, addr);
673 * readw:
674 * @addr: Guest address to read from.
676 * Reads a 16-bit value from guest memory.
678 * Returns: Value read.
680 static inline uint16_t readw(uint64_t addr)
682 return qtest_readw(global_qtest, addr);
686 * readl:
687 * @addr: Guest address to read from.
689 * Reads a 32-bit value from guest memory.
691 * Returns: Value read.
693 static inline uint32_t readl(uint64_t addr)
695 return qtest_readl(global_qtest, addr);
699 * readq:
700 * @addr: Guest address to read from.
702 * Reads a 64-bit value from guest memory.
704 * Returns: Value read.
706 static inline uint64_t readq(uint64_t addr)
708 return qtest_readq(global_qtest, addr);
712 * memread:
713 * @addr: Guest address to read from.
714 * @data: Pointer to where memory contents will be stored.
715 * @size: Number of bytes to read.
717 * Read guest memory into a buffer.
719 static inline void memread(uint64_t addr, void *data, size_t size)
721 qtest_memread(global_qtest, addr, data, size);
725 * bufread:
726 * @addr: Guest address to read from.
727 * @data: Pointer to where memory contents will be stored.
728 * @size: Number of bytes to read.
730 * Read guest memory into a buffer, receive using a base64 encoding.
732 static inline void bufread(uint64_t addr, void *data, size_t size)
734 qtest_bufread(global_qtest, addr, data, size);
738 * memwrite:
739 * @addr: Guest address to write to.
740 * @data: Pointer to the bytes that will be written to guest memory.
741 * @size: Number of bytes to write.
743 * Write a buffer to guest memory.
745 static inline void memwrite(uint64_t addr, const void *data, size_t size)
747 qtest_memwrite(global_qtest, addr, data, size);
751 * bufwrite:
752 * @addr: Guest address to write to.
753 * @data: Pointer to the bytes that will be written to guest memory.
754 * @size: Number of bytes to write.
756 * Write a buffer to guest memory, transmit using a base64 encoding.
758 static inline void bufwrite(uint64_t addr, const void *data, size_t size)
760 qtest_bufwrite(global_qtest, addr, data, size);
764 * qmemset:
765 * @addr: Guest address to write to.
766 * @patt: Byte pattern to fill the guest memory region with.
767 * @size: Number of bytes to write.
769 * Write a pattern to guest memory.
771 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
773 qtest_memset(global_qtest, addr, patt, size);
777 * clock_step_next:
779 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
781 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
783 static inline int64_t clock_step_next(void)
785 return qtest_clock_step_next(global_qtest);
789 * clock_step:
790 * @step: Number of nanoseconds to advance the clock by.
792 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
794 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
796 static inline int64_t clock_step(int64_t step)
798 return qtest_clock_step(global_qtest, step);
802 * clock_set:
803 * @val: Nanoseconds value to advance the clock to.
805 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
807 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
809 static inline int64_t clock_set(int64_t val)
811 return qtest_clock_set(global_qtest, val);
815 * qtest_big_endian:
817 * Returns: True if the architecture under test has a big endian configuration.
819 bool qtest_big_endian(void);
821 #endif