s390x/ipl: clean up qom definitions and turn into TYPE_DEVICE
[qemu.git] / tests / libqtest.h
blobdf087452ee2119e50dbde6103c518afb2eab4a8f
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_hmpv:
124 * @s: #QTestState instance to operate on.
125 * @fmt...: HMP command to send to QEMU
127 * Send HMP command to QEMU via QMP's human-monitor-command.
129 * Returns: the command's output. The caller should g_free() it.
131 char *qtest_hmp(QTestState *s, const char *fmt, ...);
134 * qtest_hmpv:
135 * @s: #QTestState instance to operate on.
136 * @fmt: HMP command to send to QEMU
137 * @ap: HMP command arguments
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_hmpv(QTestState *s, const char *fmt, va_list ap);
146 * qtest_get_irq:
147 * @s: #QTestState instance to operate on.
148 * @num: Interrupt to observe.
150 * Returns: The level of the @num interrupt.
152 bool qtest_get_irq(QTestState *s, int num);
155 * qtest_irq_intercept_in:
156 * @s: #QTestState instance to operate on.
157 * @string: QOM path of a device.
159 * Associate qtest irqs with the GPIO-in pins of the device
160 * whose path is specified by @string.
162 void qtest_irq_intercept_in(QTestState *s, const char *string);
165 * qtest_irq_intercept_out:
166 * @s: #QTestState instance to operate on.
167 * @string: QOM path of a device.
169 * Associate qtest irqs with the GPIO-out pins of the device
170 * whose path is specified by @string.
172 void qtest_irq_intercept_out(QTestState *s, const char *string);
175 * qtest_outb:
176 * @s: #QTestState instance to operate on.
177 * @addr: I/O port to write to.
178 * @value: Value being written.
180 * Write an 8-bit value to an I/O port.
182 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
185 * qtest_outw:
186 * @s: #QTestState instance to operate on.
187 * @addr: I/O port to write to.
188 * @value: Value being written.
190 * Write a 16-bit value to an I/O port.
192 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
195 * qtest_outl:
196 * @s: #QTestState instance to operate on.
197 * @addr: I/O port to write to.
198 * @value: Value being written.
200 * Write a 32-bit value to an I/O port.
202 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
205 * qtest_inb:
206 * @s: #QTestState instance to operate on.
207 * @addr: I/O port to read from.
209 * Returns an 8-bit value from an I/O port.
211 uint8_t qtest_inb(QTestState *s, uint16_t addr);
214 * qtest_inw:
215 * @s: #QTestState instance to operate on.
216 * @addr: I/O port to read from.
218 * Returns a 16-bit value from an I/O port.
220 uint16_t qtest_inw(QTestState *s, uint16_t addr);
223 * qtest_inl:
224 * @s: #QTestState instance to operate on.
225 * @addr: I/O port to read from.
227 * Returns a 32-bit value from an I/O port.
229 uint32_t qtest_inl(QTestState *s, uint16_t addr);
232 * qtest_writeb:
233 * @s: #QTestState instance to operate on.
234 * @addr: Guest address to write to.
235 * @value: Value being written.
237 * Writes an 8-bit value to memory.
239 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
242 * qtest_writew:
243 * @s: #QTestState instance to operate on.
244 * @addr: Guest address to write to.
245 * @value: Value being written.
247 * Writes a 16-bit value to memory.
249 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
252 * qtest_writel:
253 * @s: #QTestState instance to operate on.
254 * @addr: Guest address to write to.
255 * @value: Value being written.
257 * Writes a 32-bit value to memory.
259 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
262 * qtest_writeq:
263 * @s: #QTestState instance to operate on.
264 * @addr: Guest address to write to.
265 * @value: Value being written.
267 * Writes a 64-bit value to memory.
269 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
272 * qtest_readb:
273 * @s: #QTestState instance to operate on.
274 * @addr: Guest address to read from.
276 * Reads an 8-bit value from memory.
278 * Returns: Value read.
280 uint8_t qtest_readb(QTestState *s, uint64_t addr);
283 * qtest_readw:
284 * @s: #QTestState instance to operate on.
285 * @addr: Guest address to read from.
287 * Reads a 16-bit value from memory.
289 * Returns: Value read.
291 uint16_t qtest_readw(QTestState *s, uint64_t addr);
294 * qtest_readl:
295 * @s: #QTestState instance to operate on.
296 * @addr: Guest address to read from.
298 * Reads a 32-bit value from memory.
300 * Returns: Value read.
302 uint32_t qtest_readl(QTestState *s, uint64_t addr);
305 * qtest_readq:
306 * @s: #QTestState instance to operate on.
307 * @addr: Guest address to read from.
309 * Reads a 64-bit value from memory.
311 * Returns: Value read.
313 uint64_t qtest_readq(QTestState *s, uint64_t addr);
316 * qtest_memread:
317 * @s: #QTestState instance to operate on.
318 * @addr: Guest address to read from.
319 * @data: Pointer to where memory contents will be stored.
320 * @size: Number of bytes to read.
322 * Read guest memory into a buffer.
324 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
327 * qtest_bufread:
328 * @s: #QTestState instance to operate on.
329 * @addr: Guest address to read from.
330 * @data: Pointer to where memory contents will be stored.
331 * @size: Number of bytes to read.
333 * Read guest memory into a buffer and receive using a base64 encoding.
335 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
338 * qtest_memwrite:
339 * @s: #QTestState instance to operate on.
340 * @addr: Guest address to write to.
341 * @data: Pointer to the bytes that will be written to guest memory.
342 * @size: Number of bytes to write.
344 * Write a buffer to guest memory.
346 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
349 * qtest_bufwrite:
350 * @s: #QTestState instance to operate on.
351 * @addr: Guest address to write to.
352 * @data: Pointer to the bytes that will be written to guest memory.
353 * @size: Number of bytes to write.
355 * Write a buffer to guest memory and transmit using a base64 encoding.
357 void qtest_bufwrite(QTestState *s, uint64_t addr,
358 const void *data, size_t size);
361 * qtest_memset:
362 * @s: #QTestState instance to operate on.
363 * @addr: Guest address to write to.
364 * @patt: Byte pattern to fill the guest memory region with.
365 * @size: Number of bytes to write.
367 * Write a pattern to guest memory.
369 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
372 * qtest_clock_step_next:
373 * @s: #QTestState instance to operate on.
375 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
377 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
379 int64_t qtest_clock_step_next(QTestState *s);
382 * qtest_clock_step:
383 * @s: QTestState instance to operate on.
384 * @step: Number of nanoseconds to advance the clock by.
386 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
388 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
390 int64_t qtest_clock_step(QTestState *s, int64_t step);
393 * qtest_clock_set:
394 * @s: QTestState instance to operate on.
395 * @val: Nanoseconds value to advance the clock to.
397 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
399 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
401 int64_t qtest_clock_set(QTestState *s, int64_t val);
404 * qtest_get_arch:
406 * Returns: The architecture for the QEMU executable under test.
408 const char *qtest_get_arch(void);
411 * qtest_add_func:
412 * @str: Test case path.
413 * @fn: Test case function
415 * Add a GTester testcase with the given name and function.
416 * The path is prefixed with the architecture under test, as
417 * returned by qtest_get_arch().
419 void qtest_add_func(const char *str, void (*fn));
422 * qtest_add_data_func:
423 * @str: Test case path.
424 * @data: Test case data
425 * @fn: Test case function
427 * Add a GTester testcase with the given name, data and function.
428 * The path is prefixed with the architecture under test, as
429 * returned by qtest_get_arch().
431 void qtest_add_data_func(const char *str, const void *data, void (*fn));
434 * qtest_add:
435 * @testpath: Test case path
436 * @Fixture: Fixture type
437 * @tdata: Test case data
438 * @fsetup: Test case setup function
439 * @ftest: Test case function
440 * @fteardown: Test case teardown function
442 * Add a GTester testcase with the given name, data and functions.
443 * The path is prefixed with the architecture under test, as
444 * returned by qtest_get_arch().
446 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
447 do { \
448 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
449 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
450 g_free(path); \
451 } while (0)
453 void qtest_add_abrt_handler(void (*fn), const void *data);
456 * qtest_start:
457 * @args: other arguments to pass to QEMU
459 * Start QEMU and assign the resulting #QTestState to a global variable.
460 * The global variable is used by "shortcut" functions documented below.
462 * Returns: #QTestState instance.
464 static inline QTestState *qtest_start(const char *args)
466 global_qtest = qtest_init(args);
467 return global_qtest;
471 * qtest_end:
473 * Shut down the QEMU process started by qtest_start().
475 static inline void qtest_end(void)
477 qtest_quit(global_qtest);
478 global_qtest = NULL;
482 * qmp:
483 * @fmt...: QMP message to send to qemu
485 * Sends a QMP message to QEMU and returns the response.
487 QDict *qmp(const char *fmt, ...);
490 * qmp_async:
491 * @fmt...: QMP message to send to qemu
493 * Sends a QMP message to QEMU and leaves the response in the stream.
495 void qmp_async(const char *fmt, ...);
498 * qmp_discard_response:
499 * @fmt...: QMP message to send to qemu
501 * Sends a QMP message to QEMU and consumes the response.
503 void qmp_discard_response(const char *fmt, ...);
506 * qmp_receive:
508 * Reads a QMP message from QEMU and returns the response.
510 static inline QDict *qmp_receive(void)
512 return qtest_qmp_receive(global_qtest);
516 * qmp_eventwait:
517 * @s: #event event to wait for.
519 * Continuosly polls for QMP responses until it receives the desired event.
521 static inline void qmp_eventwait(const char *event)
523 return qtest_qmp_eventwait(global_qtest, event);
527 * hmp:
528 * @fmt...: HMP command to send to QEMU
530 * Send HMP command to QEMU via QMP's human-monitor-command.
532 * Returns: the command's output. The caller should g_free() it.
534 char *hmp(const char *fmt, ...);
537 * get_irq:
538 * @num: Interrupt to observe.
540 * Returns: The level of the @num interrupt.
542 static inline bool get_irq(int num)
544 return qtest_get_irq(global_qtest, num);
548 * irq_intercept_in:
549 * @string: QOM path of a device.
551 * Associate qtest irqs with the GPIO-in pins of the device
552 * whose path is specified by @string.
554 static inline void irq_intercept_in(const char *string)
556 qtest_irq_intercept_in(global_qtest, string);
560 * qtest_irq_intercept_out:
561 * @string: QOM path of a device.
563 * Associate qtest irqs with the GPIO-out pins of the device
564 * whose path is specified by @string.
566 static inline void irq_intercept_out(const char *string)
568 qtest_irq_intercept_out(global_qtest, string);
572 * outb:
573 * @addr: I/O port to write to.
574 * @value: Value being written.
576 * Write an 8-bit value to an I/O port.
578 static inline void outb(uint16_t addr, uint8_t value)
580 qtest_outb(global_qtest, addr, value);
584 * outw:
585 * @addr: I/O port to write to.
586 * @value: Value being written.
588 * Write a 16-bit value to an I/O port.
590 static inline void outw(uint16_t addr, uint16_t value)
592 qtest_outw(global_qtest, addr, value);
596 * outl:
597 * @addr: I/O port to write to.
598 * @value: Value being written.
600 * Write a 32-bit value to an I/O port.
602 static inline void outl(uint16_t addr, uint32_t value)
604 qtest_outl(global_qtest, addr, value);
608 * inb:
609 * @addr: I/O port to read from.
611 * Reads an 8-bit value from an I/O port.
613 * Returns: Value read.
615 static inline uint8_t inb(uint16_t addr)
617 return qtest_inb(global_qtest, addr);
621 * inw:
622 * @addr: I/O port to read from.
624 * Reads a 16-bit value from an I/O port.
626 * Returns: Value read.
628 static inline uint16_t inw(uint16_t addr)
630 return qtest_inw(global_qtest, addr);
634 * inl:
635 * @addr: I/O port to read from.
637 * Reads a 32-bit value from an I/O port.
639 * Returns: Value read.
641 static inline uint32_t inl(uint16_t addr)
643 return qtest_inl(global_qtest, addr);
647 * writeb:
648 * @addr: Guest address to write to.
649 * @value: Value being written.
651 * Writes an 8-bit value to guest memory.
653 static inline void writeb(uint64_t addr, uint8_t value)
655 qtest_writeb(global_qtest, addr, value);
659 * writew:
660 * @addr: Guest address to write to.
661 * @value: Value being written.
663 * Writes a 16-bit value to guest memory.
665 static inline void writew(uint64_t addr, uint16_t value)
667 qtest_writew(global_qtest, addr, value);
671 * writel:
672 * @addr: Guest address to write to.
673 * @value: Value being written.
675 * Writes a 32-bit value to guest memory.
677 static inline void writel(uint64_t addr, uint32_t value)
679 qtest_writel(global_qtest, addr, value);
683 * writeq:
684 * @addr: Guest address to write to.
685 * @value: Value being written.
687 * Writes a 64-bit value to guest memory.
689 static inline void writeq(uint64_t addr, uint64_t value)
691 qtest_writeq(global_qtest, addr, value);
695 * readb:
696 * @addr: Guest address to read from.
698 * Reads an 8-bit value from guest memory.
700 * Returns: Value read.
702 static inline uint8_t readb(uint64_t addr)
704 return qtest_readb(global_qtest, addr);
708 * readw:
709 * @addr: Guest address to read from.
711 * Reads a 16-bit value from guest memory.
713 * Returns: Value read.
715 static inline uint16_t readw(uint64_t addr)
717 return qtest_readw(global_qtest, addr);
721 * readl:
722 * @addr: Guest address to read from.
724 * Reads a 32-bit value from guest memory.
726 * Returns: Value read.
728 static inline uint32_t readl(uint64_t addr)
730 return qtest_readl(global_qtest, addr);
734 * readq:
735 * @addr: Guest address to read from.
737 * Reads a 64-bit value from guest memory.
739 * Returns: Value read.
741 static inline uint64_t readq(uint64_t addr)
743 return qtest_readq(global_qtest, addr);
747 * memread:
748 * @addr: Guest address to read from.
749 * @data: Pointer to where memory contents will be stored.
750 * @size: Number of bytes to read.
752 * Read guest memory into a buffer.
754 static inline void memread(uint64_t addr, void *data, size_t size)
756 qtest_memread(global_qtest, addr, data, size);
760 * bufread:
761 * @addr: Guest address to read from.
762 * @data: Pointer to where memory contents will be stored.
763 * @size: Number of bytes to read.
765 * Read guest memory into a buffer, receive using a base64 encoding.
767 static inline void bufread(uint64_t addr, void *data, size_t size)
769 qtest_bufread(global_qtest, addr, data, size);
773 * memwrite:
774 * @addr: Guest address to write to.
775 * @data: Pointer to the bytes that will be written to guest memory.
776 * @size: Number of bytes to write.
778 * Write a buffer to guest memory.
780 static inline void memwrite(uint64_t addr, const void *data, size_t size)
782 qtest_memwrite(global_qtest, addr, data, size);
786 * bufwrite:
787 * @addr: Guest address to write to.
788 * @data: Pointer to the bytes that will be written to guest memory.
789 * @size: Number of bytes to write.
791 * Write a buffer to guest memory, transmit using a base64 encoding.
793 static inline void bufwrite(uint64_t addr, const void *data, size_t size)
795 qtest_bufwrite(global_qtest, addr, data, size);
799 * qmemset:
800 * @addr: Guest address to write to.
801 * @patt: Byte pattern to fill the guest memory region with.
802 * @size: Number of bytes to write.
804 * Write a pattern to guest memory.
806 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
808 qtest_memset(global_qtest, addr, patt, size);
812 * clock_step_next:
814 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
816 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
818 static inline int64_t clock_step_next(void)
820 return qtest_clock_step_next(global_qtest);
824 * clock_step:
825 * @step: Number of nanoseconds to advance the clock by.
827 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
829 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
831 static inline int64_t clock_step(int64_t step)
833 return qtest_clock_step(global_qtest, step);
837 * clock_set:
838 * @val: Nanoseconds value to advance the clock to.
840 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
842 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
844 static inline int64_t clock_set(int64_t val)
846 return qtest_clock_set(global_qtest, val);
850 * qtest_big_endian:
852 * Returns: True if the architecture under test has a big endian configuration.
854 bool qtest_big_endian(void);
857 QDict *qmp_fd_receive(int fd);
858 void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
859 void qmp_fd_send(int fd, const char *fmt, ...);
860 QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
861 QDict *qmp_fd(int fd, const char *fmt, ...);
863 #endif