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