]> git.proxmox.com Git - mirror_qemu.git/blame - tests/libqtest.h
qapi: Ignore files created during make check
[mirror_qemu.git] / tests / libqtest.h
CommitLineData
49ee3590
AL
1/*
2 * QTest
3 *
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
872536bf 6 * Copyright SUSE LINUX Products GmbH 2013
49ee3590
AL
7 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
872536bf 11 * Andreas Färber <afaerber@suse.de>
49ee3590
AL
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
1d9358e6 20#include <stddef.h>
49ee3590
AL
21#include <stdint.h>
22#include <stdbool.h>
b73cf9e9 23#include <stdarg.h>
49ee3590 24#include <sys/types.h>
0c460dac 25#include "qapi/qmp/qdict.h"
49ee3590
AL
26
27typedef struct QTestState QTestState;
28
29extern QTestState *global_qtest;
30
31/**
32 * qtest_init:
33 * @extra_args: other arguments to pass to QEMU.
6acf801d
AF
34 *
35 * Returns: #QTestState instance.
49ee3590
AL
36 */
37QTestState *qtest_init(const char *extra_args);
38
39/**
40 * qtest_quit:
6acf801d 41 * @s: #QTestState instance to operate on.
49ee3590
AL
42 *
43 * Shut down the QEMU process associated to @s.
44 */
45void qtest_quit(QTestState *s);
46
a3ca163c 47/**
0d1aa05e 48 * qtest_qmp_discard_response:
6acf801d 49 * @s: #QTestState instance to operate on.
a3ca163c
KW
50 * @fmt...: QMP message to send to qemu
51 *
0d1aa05e 52 * Sends a QMP message to QEMU and consumes the response.
a3ca163c 53 */
0d1aa05e 54void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
a3ca163c 55
0c460dac
SH
56/**
57 * qtest_qmp:
58 * @s: #QTestState instance to operate on.
59 * @fmt...: QMP message to send to qemu
60 *
61 * Sends a QMP message to QEMU and returns the response.
62 */
63QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
64
b73cf9e9 65/**
0d1aa05e 66 * qtest_qmpv_discard_response:
b73cf9e9
AF
67 * @s: #QTestState instance to operate on.
68 * @fmt: QMP message to send to QEMU
69 * @ap: QMP message arguments
70 *
0d1aa05e 71 * Sends a QMP message to QEMU and consumes the response.
b73cf9e9 72 */
0d1aa05e 73void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
b73cf9e9 74
0c460dac
SH
75/**
76 * qtest_qmpv:
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 returns the response.
82 */
83QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
84
66e0c7b1
AF
85/**
86 * qtest_receive:
87 * @s: #QTestState instance to operate on.
88 *
89 * Reads a QMP message from QEMU and returns the response.
90 */
91QDict *qtest_qmp_receive(QTestState *s);
92
49ee3590
AL
93/**
94 * qtest_get_irq:
6acf801d 95 * @s: #QTestState instance to operate on.
49ee3590
AL
96 * @num: Interrupt to observe.
97 *
6acf801d 98 * Returns: The level of the @num interrupt.
49ee3590
AL
99 */
100bool qtest_get_irq(QTestState *s, int num);
101
102/**
103 * qtest_irq_intercept_in:
6acf801d 104 * @s: #QTestState instance to operate on.
49ee3590
AL
105 * @string: QOM path of a device.
106 *
107 * Associate qtest irqs with the GPIO-in pins of the device
108 * whose path is specified by @string.
109 */
110void qtest_irq_intercept_in(QTestState *s, const char *string);
111
112/**
113 * qtest_irq_intercept_out:
6acf801d 114 * @s: #QTestState instance to operate on.
49ee3590
AL
115 * @string: QOM path of a device.
116 *
117 * Associate qtest irqs with the GPIO-out pins of the device
118 * whose path is specified by @string.
119 */
120void qtest_irq_intercept_out(QTestState *s, const char *string);
121
122/**
123 * qtest_outb:
6acf801d 124 * @s: #QTestState instance to operate on.
49ee3590
AL
125 * @addr: I/O port to write to.
126 * @value: Value being written.
127 *
128 * Write an 8-bit value to an I/O port.
129 */
130void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
131
132/**
133 * qtest_outw:
6acf801d 134 * @s: #QTestState instance to operate on.
49ee3590
AL
135 * @addr: I/O port to write to.
136 * @value: Value being written.
137 *
138 * Write a 16-bit value to an I/O port.
139 */
140void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
141
142/**
143 * qtest_outl:
6acf801d 144 * @s: #QTestState instance to operate on.
49ee3590
AL
145 * @addr: I/O port to write to.
146 * @value: Value being written.
147 *
148 * Write a 32-bit value to an I/O port.
149 */
150void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
151
152/**
153 * qtest_inb:
6acf801d 154 * @s: #QTestState instance to operate on.
49ee3590 155 * @addr: I/O port to read from.
49ee3590
AL
156 *
157 * Returns an 8-bit value from an I/O port.
158 */
159uint8_t qtest_inb(QTestState *s, uint16_t addr);
160
161/**
162 * qtest_inw:
6acf801d 163 * @s: #QTestState instance to operate on.
49ee3590 164 * @addr: I/O port to read from.
49ee3590
AL
165 *
166 * Returns a 16-bit value from an I/O port.
167 */
168uint16_t qtest_inw(QTestState *s, uint16_t addr);
169
170/**
171 * qtest_inl:
6acf801d 172 * @s: #QTestState instance to operate on.
49ee3590 173 * @addr: I/O port to read from.
49ee3590
AL
174 *
175 * Returns a 32-bit value from an I/O port.
176 */
177uint32_t qtest_inl(QTestState *s, uint16_t addr);
178
872536bf
AF
179/**
180 * qtest_writeb:
181 * @s: #QTestState instance to operate on.
182 * @addr: Guest address to write to.
183 * @value: Value being written.
184 *
185 * Writes an 8-bit value to memory.
186 */
187void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
188
189/**
190 * qtest_writew:
191 * @s: #QTestState instance to operate on.
192 * @addr: Guest address to write to.
193 * @value: Value being written.
194 *
195 * Writes a 16-bit value to memory.
196 */
197void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
198
199/**
200 * qtest_writel:
201 * @s: #QTestState instance to operate on.
202 * @addr: Guest address to write to.
203 * @value: Value being written.
204 *
205 * Writes a 32-bit value to memory.
206 */
207void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
208
209/**
210 * qtest_writeq:
211 * @s: #QTestState instance to operate on.
212 * @addr: Guest address to write to.
213 * @value: Value being written.
214 *
215 * Writes a 64-bit value to memory.
216 */
217void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
218
219/**
220 * qtest_readb:
221 * @s: #QTestState instance to operate on.
222 * @addr: Guest address to read from.
223 *
224 * Reads an 8-bit value from memory.
225 *
226 * Returns: Value read.
227 */
228uint8_t qtest_readb(QTestState *s, uint64_t addr);
229
230/**
231 * qtest_readw:
232 * @s: #QTestState instance to operate on.
233 * @addr: Guest address to read from.
234 *
235 * Reads a 16-bit value from memory.
236 *
237 * Returns: Value read.
238 */
239uint16_t qtest_readw(QTestState *s, uint64_t addr);
240
241/**
242 * qtest_readl:
243 * @s: #QTestState instance to operate on.
244 * @addr: Guest address to read from.
245 *
246 * Reads a 32-bit value from memory.
247 *
248 * Returns: Value read.
249 */
250uint32_t qtest_readl(QTestState *s, uint64_t addr);
251
252/**
253 * qtest_readq:
254 * @s: #QTestState instance to operate on.
255 * @addr: Guest address to read from.
256 *
257 * Reads a 64-bit value from memory.
258 *
259 * Returns: Value read.
260 */
261uint64_t qtest_readq(QTestState *s, uint64_t addr);
262
49ee3590
AL
263/**
264 * qtest_memread:
6acf801d 265 * @s: #QTestState instance to operate on.
49ee3590
AL
266 * @addr: Guest address to read from.
267 * @data: Pointer to where memory contents will be stored.
268 * @size: Number of bytes to read.
269 *
270 * Read guest memory into a buffer.
271 */
272void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
273
274/**
275 * qtest_memwrite:
6acf801d 276 * @s: #QTestState instance to operate on.
49ee3590
AL
277 * @addr: Guest address to write to.
278 * @data: Pointer to the bytes that will be written to guest memory.
279 * @size: Number of bytes to write.
280 *
281 * Write a buffer to guest memory.
282 */
283void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
284
86298845
JS
285/**
286 * qtest_memset:
287 * @s: #QTestState instance to operate on.
288 * @addr: Guest address to write to.
289 * @patt: Byte pattern to fill the guest memory region with.
290 * @size: Number of bytes to write.
291 *
292 * Write a pattern to guest memory.
293 */
294void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
295
49ee3590
AL
296/**
297 * qtest_clock_step_next:
6acf801d
AF
298 * @s: #QTestState instance to operate on.
299 *
bc72ad67 300 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
49ee3590 301 *
bc72ad67 302 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590
AL
303 */
304int64_t qtest_clock_step_next(QTestState *s);
305
306/**
307 * qtest_clock_step:
308 * @s: QTestState instance to operate on.
309 * @step: Number of nanoseconds to advance the clock by.
310 *
bc72ad67 311 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
6acf801d 312 *
bc72ad67 313 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590
AL
314 */
315int64_t qtest_clock_step(QTestState *s, int64_t step);
316
317/**
318 * qtest_clock_set:
319 * @s: QTestState instance to operate on.
320 * @val: Nanoseconds value to advance the clock to.
321 *
bc72ad67 322 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
6acf801d 323 *
bc72ad67 324 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590
AL
325 */
326int64_t qtest_clock_set(QTestState *s, int64_t val);
327
328/**
329 * qtest_get_arch:
330 *
6acf801d 331 * Returns: The architecture for the QEMU executable under test.
49ee3590
AL
332 */
333const char *qtest_get_arch(void);
334
335/**
336 * qtest_add_func:
337 * @str: Test case path.
338 * @fn: Test case function
339 *
340 * Add a GTester testcase with the given name and function.
341 * The path is prefixed with the architecture under test, as
6acf801d 342 * returned by qtest_get_arch().
49ee3590
AL
343 */
344void qtest_add_func(const char *str, void (*fn));
345
346/**
347 * qtest_start:
348 * @args: other arguments to pass to QEMU
349 *
6acf801d
AF
350 * Start QEMU and assign the resulting #QTestState to a global variable.
351 * The global variable is used by "shortcut" functions documented below.
352 *
353 * Returns: #QTestState instance.
49ee3590 354 */
6acf801d
AF
355static inline QTestState *qtest_start(const char *args)
356{
96b8ca47
SH
357 global_qtest = qtest_init(args);
358 return global_qtest;
6acf801d 359}
49ee3590 360
1d9358e6
MA
361/**
362 * qtest_end:
363 *
364 * Shut down the QEMU process started by qtest_start().
365 */
366static inline void qtest_end(void)
367{
368 qtest_quit(global_qtest);
96b8ca47 369 global_qtest = NULL;
1d9358e6
MA
370}
371
0c460dac
SH
372/**
373 * qmp:
374 * @fmt...: QMP message to send to qemu
375 *
376 * Sends a QMP message to QEMU and returns the response.
377 */
0100f425 378QDict *qmp(const char *fmt, ...);
0c460dac 379
a3ca163c 380/**
0d1aa05e 381 * qmp_discard_response:
a3ca163c
KW
382 * @fmt...: QMP message to send to qemu
383 *
0d1aa05e 384 * Sends a QMP message to QEMU and consumes the response.
a3ca163c 385 */
0100f425 386void qmp_discard_response(const char *fmt, ...);
a3ca163c 387
66e0c7b1
AF
388/**
389 * qmp_receive:
390 *
391 * Reads a QMP message from QEMU and returns the response.
392 */
393static inline QDict *qmp_receive(void)
394{
395 return qtest_qmp_receive(global_qtest);
396}
397
49ee3590
AL
398/**
399 * get_irq:
400 * @num: Interrupt to observe.
401 *
6acf801d 402 * Returns: The level of the @num interrupt.
49ee3590 403 */
6acf801d
AF
404static inline bool get_irq(int num)
405{
406 return qtest_get_irq(global_qtest, num);
407}
49ee3590
AL
408
409/**
410 * irq_intercept_in:
411 * @string: QOM path of a device.
412 *
413 * Associate qtest irqs with the GPIO-in pins of the device
414 * whose path is specified by @string.
415 */
6acf801d
AF
416static inline void irq_intercept_in(const char *string)
417{
418 qtest_irq_intercept_in(global_qtest, string);
419}
49ee3590
AL
420
421/**
422 * qtest_irq_intercept_out:
423 * @string: QOM path of a device.
424 *
425 * Associate qtest irqs with the GPIO-out pins of the device
426 * whose path is specified by @string.
427 */
6acf801d
AF
428static inline void irq_intercept_out(const char *string)
429{
430 qtest_irq_intercept_out(global_qtest, string);
431}
49ee3590
AL
432
433/**
434 * outb:
435 * @addr: I/O port to write to.
436 * @value: Value being written.
437 *
438 * Write an 8-bit value to an I/O port.
439 */
6acf801d
AF
440static inline void outb(uint16_t addr, uint8_t value)
441{
442 qtest_outb(global_qtest, addr, value);
443}
49ee3590
AL
444
445/**
446 * outw:
447 * @addr: I/O port to write to.
448 * @value: Value being written.
449 *
450 * Write a 16-bit value to an I/O port.
451 */
6acf801d
AF
452static inline void outw(uint16_t addr, uint16_t value)
453{
454 qtest_outw(global_qtest, addr, value);
455}
49ee3590
AL
456
457/**
458 * outl:
459 * @addr: I/O port to write to.
460 * @value: Value being written.
461 *
462 * Write a 32-bit value to an I/O port.
463 */
6acf801d
AF
464static inline void outl(uint16_t addr, uint32_t value)
465{
466 qtest_outl(global_qtest, addr, value);
467}
49ee3590
AL
468
469/**
470 * inb:
471 * @addr: I/O port to read from.
49ee3590 472 *
6acf801d
AF
473 * Reads an 8-bit value from an I/O port.
474 *
475 * Returns: Value read.
49ee3590 476 */
6acf801d
AF
477static inline uint8_t inb(uint16_t addr)
478{
479 return qtest_inb(global_qtest, addr);
480}
49ee3590
AL
481
482/**
483 * inw:
484 * @addr: I/O port to read from.
49ee3590 485 *
6acf801d
AF
486 * Reads a 16-bit value from an I/O port.
487 *
488 * Returns: Value read.
49ee3590 489 */
6acf801d
AF
490static inline uint16_t inw(uint16_t addr)
491{
492 return qtest_inw(global_qtest, addr);
493}
49ee3590
AL
494
495/**
496 * inl:
497 * @addr: I/O port to read from.
49ee3590 498 *
6acf801d
AF
499 * Reads a 32-bit value from an I/O port.
500 *
501 * Returns: Value read.
49ee3590 502 */
6acf801d
AF
503static inline uint32_t inl(uint16_t addr)
504{
505 return qtest_inl(global_qtest, addr);
506}
49ee3590 507
872536bf
AF
508/**
509 * writeb:
510 * @addr: Guest address to write to.
511 * @value: Value being written.
512 *
513 * Writes an 8-bit value to guest memory.
514 */
515static inline void writeb(uint64_t addr, uint8_t value)
516{
517 qtest_writeb(global_qtest, addr, value);
518}
519
520/**
521 * writew:
522 * @addr: Guest address to write to.
523 * @value: Value being written.
524 *
525 * Writes a 16-bit value to guest memory.
526 */
527static inline void writew(uint64_t addr, uint16_t value)
528{
529 qtest_writew(global_qtest, addr, value);
530}
531
532/**
533 * writel:
534 * @addr: Guest address to write to.
535 * @value: Value being written.
536 *
537 * Writes a 32-bit value to guest memory.
538 */
539static inline void writel(uint64_t addr, uint32_t value)
540{
541 qtest_writel(global_qtest, addr, value);
542}
543
544/**
545 * writeq:
546 * @addr: Guest address to write to.
547 * @value: Value being written.
548 *
549 * Writes a 64-bit value to guest memory.
550 */
551static inline void writeq(uint64_t addr, uint64_t value)
552{
553 qtest_writeq(global_qtest, addr, value);
554}
555
556/**
557 * readb:
558 * @addr: Guest address to read from.
559 *
560 * Reads an 8-bit value from guest memory.
561 *
562 * Returns: Value read.
563 */
564static inline uint8_t readb(uint64_t addr)
565{
566 return qtest_readb(global_qtest, addr);
567}
568
569/**
570 * readw:
571 * @addr: Guest address to read from.
572 *
573 * Reads a 16-bit value from guest memory.
574 *
575 * Returns: Value read.
576 */
577static inline uint16_t readw(uint64_t addr)
578{
579 return qtest_readw(global_qtest, addr);
580}
581
582/**
583 * readl:
584 * @addr: Guest address to read from.
585 *
586 * Reads a 32-bit value from guest memory.
587 *
588 * Returns: Value read.
589 */
590static inline uint32_t readl(uint64_t addr)
591{
592 return qtest_readl(global_qtest, addr);
593}
594
595/**
596 * readq:
597 * @addr: Guest address to read from.
598 *
599 * Reads a 64-bit value from guest memory.
600 *
601 * Returns: Value read.
602 */
603static inline uint64_t readq(uint64_t addr)
604{
605 return qtest_readq(global_qtest, addr);
606}
607
49ee3590
AL
608/**
609 * memread:
610 * @addr: Guest address to read from.
611 * @data: Pointer to where memory contents will be stored.
612 * @size: Number of bytes to read.
613 *
614 * Read guest memory into a buffer.
615 */
6acf801d
AF
616static inline void memread(uint64_t addr, void *data, size_t size)
617{
618 qtest_memread(global_qtest, addr, data, size);
619}
49ee3590
AL
620
621/**
622 * memwrite:
623 * @addr: Guest address to write to.
624 * @data: Pointer to the bytes that will be written to guest memory.
625 * @size: Number of bytes to write.
626 *
627 * Write a buffer to guest memory.
628 */
6acf801d
AF
629static inline void memwrite(uint64_t addr, const void *data, size_t size)
630{
631 qtest_memwrite(global_qtest, addr, data, size);
632}
49ee3590 633
86298845
JS
634/**
635 * qmemset:
636 * @addr: Guest address to write to.
637 * @patt: Byte pattern to fill the guest memory region with.
638 * @size: Number of bytes to write.
639 *
640 * Write a pattern to guest memory.
641 */
642static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
643{
644 qtest_memset(global_qtest, addr, patt, size);
645}
646
49ee3590
AL
647/**
648 * clock_step_next:
649 *
bc72ad67 650 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
6acf801d 651 *
bc72ad67 652 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590 653 */
6acf801d
AF
654static inline int64_t clock_step_next(void)
655{
656 return qtest_clock_step_next(global_qtest);
657}
49ee3590
AL
658
659/**
660 * clock_step:
661 * @step: Number of nanoseconds to advance the clock by.
662 *
bc72ad67 663 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
6acf801d 664 *
bc72ad67 665 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590 666 */
6acf801d
AF
667static inline int64_t clock_step(int64_t step)
668{
669 return qtest_clock_step(global_qtest, step);
670}
49ee3590
AL
671
672/**
673 * clock_set:
674 * @val: Nanoseconds value to advance the clock to.
675 *
bc72ad67 676 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
6acf801d 677 *
bc72ad67 678 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590 679 */
6acf801d
AF
680static inline int64_t clock_set(int64_t val)
681{
682 return qtest_clock_set(global_qtest, val);
683}
49ee3590 684
46e0cf76
MM
685/**
686 * qtest_big_endian:
687 *
688 * Returns: True if the architecture under test has a big endian configuration.
689 */
690bool qtest_big_endian(void);
691
49ee3590 692#endif