]> git.proxmox.com Git - mirror_qemu.git/blob - tests/libqtest.h
ec4203152333378a9e58ae000cc5276847e95185
[mirror_qemu.git] / tests / libqtest.h
1 /*
2 * QTest
3 *
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
7 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
12 *
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.
15 *
16 */
17 #ifndef LIBQTEST_H
18 #define LIBQTEST_H
19
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"
27
28 typedef struct QTestState QTestState;
29
30 extern QTestState *global_qtest;
31
32 /**
33 * qtest_init:
34 * @extra_args: other arguments to pass to QEMU.
35 *
36 * Returns: #QTestState instance.
37 */
38 QTestState *qtest_init(const char *extra_args);
39
40 /**
41 * qtest_quit:
42 * @s: #QTestState instance to operate on.
43 *
44 * Shut down the QEMU process associated to @s.
45 */
46 void qtest_quit(QTestState *s);
47
48 /**
49 * qtest_qmp_discard_response:
50 * @s: #QTestState instance to operate on.
51 * @fmt...: QMP message to send to qemu
52 *
53 * Sends a QMP message to QEMU and consumes the response.
54 */
55 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
56
57 /**
58 * qtest_qmp:
59 * @s: #QTestState instance to operate on.
60 * @fmt...: QMP message to send to qemu
61 *
62 * Sends a QMP message to QEMU and returns the response.
63 */
64 QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
65
66 /**
67 * qtest_async_qmp:
68 * @s: #QTestState instance to operate on.
69 * @fmt...: QMP message to send to qemu
70 *
71 * Sends a QMP message to QEMU and leaves the response in the stream.
72 */
73 void qtest_async_qmp(QTestState *s, const char *fmt, ...);
74
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
80 *
81 * Sends a QMP message to QEMU and consumes the response.
82 */
83 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
84
85 /**
86 * qtest_qmpv:
87 * @s: #QTestState instance to operate on.
88 * @fmt: QMP message to send to QEMU
89 * @ap: QMP message arguments
90 *
91 * Sends a QMP message to QEMU and returns the response.
92 */
93 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
94
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
100 *
101 * Sends a QMP message to QEMU and leaves the response in the stream.
102 */
103 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap);
104
105 /**
106 * qtest_receive:
107 * @s: #QTestState instance to operate on.
108 *
109 * Reads a QMP message from QEMU and returns the response.
110 */
111 QDict *qtest_qmp_receive(QTestState *s);
112
113 /**
114 * qtest_qmp_eventwait:
115 * @s: #QTestState instance to operate on.
116 * @s: #event event to wait for.
117 *
118 * Continuosly polls for QMP responses until it receives the desired event.
119 */
120 void qtest_qmp_eventwait(QTestState *s, const char *event);
121
122 /**
123 * qtest_get_irq:
124 * @s: #QTestState instance to operate on.
125 * @num: Interrupt to observe.
126 *
127 * Returns: The level of the @num interrupt.
128 */
129 bool qtest_get_irq(QTestState *s, int num);
130
131 /**
132 * qtest_irq_intercept_in:
133 * @s: #QTestState instance to operate on.
134 * @string: QOM path of a device.
135 *
136 * Associate qtest irqs with the GPIO-in pins of the device
137 * whose path is specified by @string.
138 */
139 void qtest_irq_intercept_in(QTestState *s, const char *string);
140
141 /**
142 * qtest_irq_intercept_out:
143 * @s: #QTestState instance to operate on.
144 * @string: QOM path of a device.
145 *
146 * Associate qtest irqs with the GPIO-out pins of the device
147 * whose path is specified by @string.
148 */
149 void qtest_irq_intercept_out(QTestState *s, const char *string);
150
151 /**
152 * qtest_outb:
153 * @s: #QTestState instance to operate on.
154 * @addr: I/O port to write to.
155 * @value: Value being written.
156 *
157 * Write an 8-bit value to an I/O port.
158 */
159 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
160
161 /**
162 * qtest_outw:
163 * @s: #QTestState instance to operate on.
164 * @addr: I/O port to write to.
165 * @value: Value being written.
166 *
167 * Write a 16-bit value to an I/O port.
168 */
169 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
170
171 /**
172 * qtest_outl:
173 * @s: #QTestState instance to operate on.
174 * @addr: I/O port to write to.
175 * @value: Value being written.
176 *
177 * Write a 32-bit value to an I/O port.
178 */
179 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
180
181 /**
182 * qtest_inb:
183 * @s: #QTestState instance to operate on.
184 * @addr: I/O port to read from.
185 *
186 * Returns an 8-bit value from an I/O port.
187 */
188 uint8_t qtest_inb(QTestState *s, uint16_t addr);
189
190 /**
191 * qtest_inw:
192 * @s: #QTestState instance to operate on.
193 * @addr: I/O port to read from.
194 *
195 * Returns a 16-bit value from an I/O port.
196 */
197 uint16_t qtest_inw(QTestState *s, uint16_t addr);
198
199 /**
200 * qtest_inl:
201 * @s: #QTestState instance to operate on.
202 * @addr: I/O port to read from.
203 *
204 * Returns a 32-bit value from an I/O port.
205 */
206 uint32_t qtest_inl(QTestState *s, uint16_t addr);
207
208 /**
209 * qtest_writeb:
210 * @s: #QTestState instance to operate on.
211 * @addr: Guest address to write to.
212 * @value: Value being written.
213 *
214 * Writes an 8-bit value to memory.
215 */
216 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
217
218 /**
219 * qtest_writew:
220 * @s: #QTestState instance to operate on.
221 * @addr: Guest address to write to.
222 * @value: Value being written.
223 *
224 * Writes a 16-bit value to memory.
225 */
226 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
227
228 /**
229 * qtest_writel:
230 * @s: #QTestState instance to operate on.
231 * @addr: Guest address to write to.
232 * @value: Value being written.
233 *
234 * Writes a 32-bit value to memory.
235 */
236 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
237
238 /**
239 * qtest_writeq:
240 * @s: #QTestState instance to operate on.
241 * @addr: Guest address to write to.
242 * @value: Value being written.
243 *
244 * Writes a 64-bit value to memory.
245 */
246 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
247
248 /**
249 * qtest_readb:
250 * @s: #QTestState instance to operate on.
251 * @addr: Guest address to read from.
252 *
253 * Reads an 8-bit value from memory.
254 *
255 * Returns: Value read.
256 */
257 uint8_t qtest_readb(QTestState *s, uint64_t addr);
258
259 /**
260 * qtest_readw:
261 * @s: #QTestState instance to operate on.
262 * @addr: Guest address to read from.
263 *
264 * Reads a 16-bit value from memory.
265 *
266 * Returns: Value read.
267 */
268 uint16_t qtest_readw(QTestState *s, uint64_t addr);
269
270 /**
271 * qtest_readl:
272 * @s: #QTestState instance to operate on.
273 * @addr: Guest address to read from.
274 *
275 * Reads a 32-bit value from memory.
276 *
277 * Returns: Value read.
278 */
279 uint32_t qtest_readl(QTestState *s, uint64_t addr);
280
281 /**
282 * qtest_readq:
283 * @s: #QTestState instance to operate on.
284 * @addr: Guest address to read from.
285 *
286 * Reads a 64-bit value from memory.
287 *
288 * Returns: Value read.
289 */
290 uint64_t qtest_readq(QTestState *s, uint64_t addr);
291
292 /**
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.
298 *
299 * Read guest memory into a buffer.
300 */
301 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
302
303 /**
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.
309 *
310 * Read guest memory into a buffer and receive using a base64 encoding.
311 */
312 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
313
314 /**
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.
320 *
321 * Write a buffer to guest memory.
322 */
323 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
324
325 /**
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.
331 *
332 * Write a buffer to guest memory and transmit using a base64 encoding.
333 */
334 void qtest_bufwrite(QTestState *s, uint64_t addr,
335 const void *data, size_t size);
336
337 /**
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.
343 *
344 * Write a pattern to guest memory.
345 */
346 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
347
348 /**
349 * qtest_clock_step_next:
350 * @s: #QTestState instance to operate on.
351 *
352 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
353 *
354 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
355 */
356 int64_t qtest_clock_step_next(QTestState *s);
357
358 /**
359 * qtest_clock_step:
360 * @s: QTestState instance to operate on.
361 * @step: Number of nanoseconds to advance the clock by.
362 *
363 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
364 *
365 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
366 */
367 int64_t qtest_clock_step(QTestState *s, int64_t step);
368
369 /**
370 * qtest_clock_set:
371 * @s: QTestState instance to operate on.
372 * @val: Nanoseconds value to advance the clock to.
373 *
374 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
375 *
376 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
377 */
378 int64_t qtest_clock_set(QTestState *s, int64_t val);
379
380 /**
381 * qtest_get_arch:
382 *
383 * Returns: The architecture for the QEMU executable under test.
384 */
385 const char *qtest_get_arch(void);
386
387 /**
388 * qtest_add_func:
389 * @str: Test case path.
390 * @fn: Test case function
391 *
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().
395 */
396 void qtest_add_func(const char *str, void (*fn));
397
398 /**
399 * qtest_add_data_func:
400 * @str: Test case path.
401 * @data: Test case data
402 * @fn: Test case function
403 *
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().
407 */
408 void qtest_add_data_func(const char *str, const void *data, void (*fn));
409
410 /**
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
418 *
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().
422 */
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)
429
430 /**
431 * qtest_start:
432 * @args: other arguments to pass to QEMU
433 *
434 * Start QEMU and assign the resulting #QTestState to a global variable.
435 * The global variable is used by "shortcut" functions documented below.
436 *
437 * Returns: #QTestState instance.
438 */
439 static inline QTestState *qtest_start(const char *args)
440 {
441 global_qtest = qtest_init(args);
442 return global_qtest;
443 }
444
445 /**
446 * qtest_end:
447 *
448 * Shut down the QEMU process started by qtest_start().
449 */
450 static inline void qtest_end(void)
451 {
452 qtest_quit(global_qtest);
453 global_qtest = NULL;
454 }
455
456 /**
457 * qmp:
458 * @fmt...: QMP message to send to qemu
459 *
460 * Sends a QMP message to QEMU and returns the response.
461 */
462 QDict *qmp(const char *fmt, ...);
463
464 /**
465 * qmp_async:
466 * @fmt...: QMP message to send to qemu
467 *
468 * Sends a QMP message to QEMU and leaves the response in the stream.
469 */
470 void qmp_async(const char *fmt, ...);
471
472 /**
473 * qmp_discard_response:
474 * @fmt...: QMP message to send to qemu
475 *
476 * Sends a QMP message to QEMU and consumes the response.
477 */
478 void qmp_discard_response(const char *fmt, ...);
479
480 /**
481 * qmp_receive:
482 *
483 * Reads a QMP message from QEMU and returns the response.
484 */
485 static inline QDict *qmp_receive(void)
486 {
487 return qtest_qmp_receive(global_qtest);
488 }
489
490 /**
491 * qmp_eventwait:
492 * @s: #event event to wait for.
493 *
494 * Continuosly polls for QMP responses until it receives the desired event.
495 */
496 static inline void qmp_eventwait(const char *event)
497 {
498 return qtest_qmp_eventwait(global_qtest, event);
499 }
500
501 /**
502 * get_irq:
503 * @num: Interrupt to observe.
504 *
505 * Returns: The level of the @num interrupt.
506 */
507 static inline bool get_irq(int num)
508 {
509 return qtest_get_irq(global_qtest, num);
510 }
511
512 /**
513 * irq_intercept_in:
514 * @string: QOM path of a device.
515 *
516 * Associate qtest irqs with the GPIO-in pins of the device
517 * whose path is specified by @string.
518 */
519 static inline void irq_intercept_in(const char *string)
520 {
521 qtest_irq_intercept_in(global_qtest, string);
522 }
523
524 /**
525 * qtest_irq_intercept_out:
526 * @string: QOM path of a device.
527 *
528 * Associate qtest irqs with the GPIO-out pins of the device
529 * whose path is specified by @string.
530 */
531 static inline void irq_intercept_out(const char *string)
532 {
533 qtest_irq_intercept_out(global_qtest, string);
534 }
535
536 /**
537 * outb:
538 * @addr: I/O port to write to.
539 * @value: Value being written.
540 *
541 * Write an 8-bit value to an I/O port.
542 */
543 static inline void outb(uint16_t addr, uint8_t value)
544 {
545 qtest_outb(global_qtest, addr, value);
546 }
547
548 /**
549 * outw:
550 * @addr: I/O port to write to.
551 * @value: Value being written.
552 *
553 * Write a 16-bit value to an I/O port.
554 */
555 static inline void outw(uint16_t addr, uint16_t value)
556 {
557 qtest_outw(global_qtest, addr, value);
558 }
559
560 /**
561 * outl:
562 * @addr: I/O port to write to.
563 * @value: Value being written.
564 *
565 * Write a 32-bit value to an I/O port.
566 */
567 static inline void outl(uint16_t addr, uint32_t value)
568 {
569 qtest_outl(global_qtest, addr, value);
570 }
571
572 /**
573 * inb:
574 * @addr: I/O port to read from.
575 *
576 * Reads an 8-bit value from an I/O port.
577 *
578 * Returns: Value read.
579 */
580 static inline uint8_t inb(uint16_t addr)
581 {
582 return qtest_inb(global_qtest, addr);
583 }
584
585 /**
586 * inw:
587 * @addr: I/O port to read from.
588 *
589 * Reads a 16-bit value from an I/O port.
590 *
591 * Returns: Value read.
592 */
593 static inline uint16_t inw(uint16_t addr)
594 {
595 return qtest_inw(global_qtest, addr);
596 }
597
598 /**
599 * inl:
600 * @addr: I/O port to read from.
601 *
602 * Reads a 32-bit value from an I/O port.
603 *
604 * Returns: Value read.
605 */
606 static inline uint32_t inl(uint16_t addr)
607 {
608 return qtest_inl(global_qtest, addr);
609 }
610
611 /**
612 * writeb:
613 * @addr: Guest address to write to.
614 * @value: Value being written.
615 *
616 * Writes an 8-bit value to guest memory.
617 */
618 static inline void writeb(uint64_t addr, uint8_t value)
619 {
620 qtest_writeb(global_qtest, addr, value);
621 }
622
623 /**
624 * writew:
625 * @addr: Guest address to write to.
626 * @value: Value being written.
627 *
628 * Writes a 16-bit value to guest memory.
629 */
630 static inline void writew(uint64_t addr, uint16_t value)
631 {
632 qtest_writew(global_qtest, addr, value);
633 }
634
635 /**
636 * writel:
637 * @addr: Guest address to write to.
638 * @value: Value being written.
639 *
640 * Writes a 32-bit value to guest memory.
641 */
642 static inline void writel(uint64_t addr, uint32_t value)
643 {
644 qtest_writel(global_qtest, addr, value);
645 }
646
647 /**
648 * writeq:
649 * @addr: Guest address to write to.
650 * @value: Value being written.
651 *
652 * Writes a 64-bit value to guest memory.
653 */
654 static inline void writeq(uint64_t addr, uint64_t value)
655 {
656 qtest_writeq(global_qtest, addr, value);
657 }
658
659 /**
660 * readb:
661 * @addr: Guest address to read from.
662 *
663 * Reads an 8-bit value from guest memory.
664 *
665 * Returns: Value read.
666 */
667 static inline uint8_t readb(uint64_t addr)
668 {
669 return qtest_readb(global_qtest, addr);
670 }
671
672 /**
673 * readw:
674 * @addr: Guest address to read from.
675 *
676 * Reads a 16-bit value from guest memory.
677 *
678 * Returns: Value read.
679 */
680 static inline uint16_t readw(uint64_t addr)
681 {
682 return qtest_readw(global_qtest, addr);
683 }
684
685 /**
686 * readl:
687 * @addr: Guest address to read from.
688 *
689 * Reads a 32-bit value from guest memory.
690 *
691 * Returns: Value read.
692 */
693 static inline uint32_t readl(uint64_t addr)
694 {
695 return qtest_readl(global_qtest, addr);
696 }
697
698 /**
699 * readq:
700 * @addr: Guest address to read from.
701 *
702 * Reads a 64-bit value from guest memory.
703 *
704 * Returns: Value read.
705 */
706 static inline uint64_t readq(uint64_t addr)
707 {
708 return qtest_readq(global_qtest, addr);
709 }
710
711 /**
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.
716 *
717 * Read guest memory into a buffer.
718 */
719 static inline void memread(uint64_t addr, void *data, size_t size)
720 {
721 qtest_memread(global_qtest, addr, data, size);
722 }
723
724 /**
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.
729 *
730 * Read guest memory into a buffer, receive using a base64 encoding.
731 */
732 static inline void bufread(uint64_t addr, void *data, size_t size)
733 {
734 qtest_bufread(global_qtest, addr, data, size);
735 }
736
737 /**
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.
742 *
743 * Write a buffer to guest memory.
744 */
745 static inline void memwrite(uint64_t addr, const void *data, size_t size)
746 {
747 qtest_memwrite(global_qtest, addr, data, size);
748 }
749
750 /**
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.
755 *
756 * Write a buffer to guest memory, transmit using a base64 encoding.
757 */
758 static inline void bufwrite(uint64_t addr, const void *data, size_t size)
759 {
760 qtest_bufwrite(global_qtest, addr, data, size);
761 }
762
763 /**
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.
768 *
769 * Write a pattern to guest memory.
770 */
771 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
772 {
773 qtest_memset(global_qtest, addr, patt, size);
774 }
775
776 /**
777 * clock_step_next:
778 *
779 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
780 *
781 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
782 */
783 static inline int64_t clock_step_next(void)
784 {
785 return qtest_clock_step_next(global_qtest);
786 }
787
788 /**
789 * clock_step:
790 * @step: Number of nanoseconds to advance the clock by.
791 *
792 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
793 *
794 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
795 */
796 static inline int64_t clock_step(int64_t step)
797 {
798 return qtest_clock_step(global_qtest, step);
799 }
800
801 /**
802 * clock_set:
803 * @val: Nanoseconds value to advance the clock to.
804 *
805 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
806 *
807 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
808 */
809 static inline int64_t clock_set(int64_t val)
810 {
811 return qtest_clock_set(global_qtest, val);
812 }
813
814 /**
815 * qtest_big_endian:
816 *
817 * Returns: True if the architecture under test has a big endian configuration.
818 */
819 bool qtest_big_endian(void);
820
821 #endif