xref: /qemu/tests/qtest/libqtest-single.h (revision 39f0e991e10706447cad782e0de48ad9724aadf7)
10ba67974SThomas Huth /*
20ba67974SThomas Huth  * QTest - wrappers for test with single QEMU instances
30ba67974SThomas Huth  *
40ba67974SThomas Huth  * Copyright IBM, Corp. 2012
50ba67974SThomas Huth  * Copyright Red Hat, Inc. 2012
60ba67974SThomas Huth  * Copyright SUSE LINUX Products GmbH 2013
70ba67974SThomas Huth  *
80ba67974SThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
90ba67974SThomas Huth  * See the COPYING file in the top-level directory.
100ba67974SThomas Huth  */
110ba67974SThomas Huth #ifndef LIBQTEST_SINGLE_H
120ba67974SThomas Huth #define LIBQTEST_SINGLE_H
130ba67974SThomas Huth 
14907b5105SMarc-André Lureau #include "libqtest.h"
15dd210749SThomas Huth 
16*39f0e991SXuzhou Cheng #ifndef _WIN32
17dd210749SThomas Huth QTestState *global_qtest __attribute__((common, weak));
18*39f0e991SXuzhou Cheng #else
19*39f0e991SXuzhou Cheng __declspec(selectany) QTestState *global_qtest;
20*39f0e991SXuzhou Cheng #endif
21dd210749SThomas Huth 
220ba67974SThomas Huth /**
230ba67974SThomas Huth  * qtest_start:
240ba67974SThomas Huth  * @args: other arguments to pass to QEMU
250ba67974SThomas Huth  *
260ba67974SThomas Huth  * Start QEMU and assign the resulting #QTestState to a global variable.
270ba67974SThomas Huth  * The global variable is used by "shortcut" functions documented below.
280ba67974SThomas Huth  *
290ba67974SThomas Huth  * Returns: #QTestState instance.
300ba67974SThomas Huth  */
qtest_start(const char * args)310ba67974SThomas Huth static inline QTestState *qtest_start(const char *args)
320ba67974SThomas Huth {
330ba67974SThomas Huth     global_qtest = qtest_init(args);
340ba67974SThomas Huth     return global_qtest;
350ba67974SThomas Huth }
360ba67974SThomas Huth 
370ba67974SThomas Huth /**
380ba67974SThomas Huth  * qtest_end:
390ba67974SThomas Huth  *
400ba67974SThomas Huth  * Shut down the QEMU process started by qtest_start().
410ba67974SThomas Huth  */
qtest_end(void)420ba67974SThomas Huth static inline void qtest_end(void)
430ba67974SThomas Huth {
440ba67974SThomas Huth     if (!global_qtest) {
450ba67974SThomas Huth         return;
460ba67974SThomas Huth     }
470ba67974SThomas Huth     qtest_quit(global_qtest);
480ba67974SThomas Huth     global_qtest = NULL;
490ba67974SThomas Huth }
500ba67974SThomas Huth 
510ba67974SThomas Huth /**
520ba67974SThomas Huth  * qmp:
530ba67974SThomas Huth  * @fmt...: QMP message to send to qemu, formatted like
54ad57e2b1SPeter Maydell  * qobject_from_jsonf_nofail().  See parse_interpolation() for what's
550ba67974SThomas Huth  * supported after '%'.
560ba67974SThomas Huth  *
570ba67974SThomas Huth  * Sends a QMP message to QEMU and returns the response.
580ba67974SThomas Huth  */
599edc6313SMarc-André Lureau G_GNUC_PRINTF(1, 2)
qmp(const char * fmt,...)600ba67974SThomas Huth static inline QDict *qmp(const char *fmt, ...)
610ba67974SThomas Huth {
620ba67974SThomas Huth     va_list ap;
630ba67974SThomas Huth     QDict *response;
640ba67974SThomas Huth 
650ba67974SThomas Huth     va_start(ap, fmt);
660ba67974SThomas Huth     response = qtest_vqmp(global_qtest, fmt, ap);
670ba67974SThomas Huth     va_end(ap);
680ba67974SThomas Huth     return response;
690ba67974SThomas Huth }
700ba67974SThomas Huth 
710ba67974SThomas Huth /**
720ba67974SThomas Huth  * qmp_eventwait:
730ba67974SThomas Huth  * @s: #event event to wait for.
740ba67974SThomas Huth  *
750ba67974SThomas Huth  * Continuously polls for QMP responses until it receives the desired event.
760ba67974SThomas Huth  */
qmp_eventwait(const char * event)770ba67974SThomas Huth static inline void qmp_eventwait(const char *event)
780ba67974SThomas Huth {
790ba67974SThomas Huth     return qtest_qmp_eventwait(global_qtest, event);
800ba67974SThomas Huth }
810ba67974SThomas Huth 
820ba67974SThomas Huth /**
830ba67974SThomas Huth  * get_irq:
840ba67974SThomas Huth  * @num: Interrupt to observe.
850ba67974SThomas Huth  *
860ba67974SThomas Huth  * Returns: The level of the @num interrupt.
870ba67974SThomas Huth  */
get_irq(int num)880ba67974SThomas Huth static inline bool get_irq(int num)
890ba67974SThomas Huth {
900ba67974SThomas Huth     return qtest_get_irq(global_qtest, num);
910ba67974SThomas Huth }
920ba67974SThomas Huth 
930ba67974SThomas Huth /**
940ba67974SThomas Huth  * outb:
950ba67974SThomas Huth  * @addr: I/O port to write to.
960ba67974SThomas Huth  * @value: Value being written.
970ba67974SThomas Huth  *
980ba67974SThomas Huth  * Write an 8-bit value to an I/O port.
990ba67974SThomas Huth  */
outb(uint16_t addr,uint8_t value)1000ba67974SThomas Huth static inline void outb(uint16_t addr, uint8_t value)
1010ba67974SThomas Huth {
1020ba67974SThomas Huth     qtest_outb(global_qtest, addr, value);
1030ba67974SThomas Huth }
1040ba67974SThomas Huth 
1050ba67974SThomas Huth /**
1060ba67974SThomas Huth  * outw:
1070ba67974SThomas Huth  * @addr: I/O port to write to.
1080ba67974SThomas Huth  * @value: Value being written.
1090ba67974SThomas Huth  *
1100ba67974SThomas Huth  * Write a 16-bit value to an I/O port.
1110ba67974SThomas Huth  */
outw(uint16_t addr,uint16_t value)1120ba67974SThomas Huth static inline void outw(uint16_t addr, uint16_t value)
1130ba67974SThomas Huth {
1140ba67974SThomas Huth     qtest_outw(global_qtest, addr, value);
1150ba67974SThomas Huth }
1160ba67974SThomas Huth 
1170ba67974SThomas Huth /**
1180ba67974SThomas Huth  * outl:
1190ba67974SThomas Huth  * @addr: I/O port to write to.
1200ba67974SThomas Huth  * @value: Value being written.
1210ba67974SThomas Huth  *
1220ba67974SThomas Huth  * Write a 32-bit value to an I/O port.
1230ba67974SThomas Huth  */
outl(uint16_t addr,uint32_t value)1240ba67974SThomas Huth static inline void outl(uint16_t addr, uint32_t value)
1250ba67974SThomas Huth {
1260ba67974SThomas Huth     qtest_outl(global_qtest, addr, value);
1270ba67974SThomas Huth }
1280ba67974SThomas Huth 
1290ba67974SThomas Huth /**
1300ba67974SThomas Huth  * inb:
1310ba67974SThomas Huth  * @addr: I/O port to read from.
1320ba67974SThomas Huth  *
1330ba67974SThomas Huth  * Reads an 8-bit value from an I/O port.
1340ba67974SThomas Huth  *
1350ba67974SThomas Huth  * Returns: Value read.
1360ba67974SThomas Huth  */
inb(uint16_t addr)1370ba67974SThomas Huth static inline uint8_t inb(uint16_t addr)
1380ba67974SThomas Huth {
1390ba67974SThomas Huth     return qtest_inb(global_qtest, addr);
1400ba67974SThomas Huth }
1410ba67974SThomas Huth 
1420ba67974SThomas Huth /**
1430ba67974SThomas Huth  * inw:
1440ba67974SThomas Huth  * @addr: I/O port to read from.
1450ba67974SThomas Huth  *
1460ba67974SThomas Huth  * Reads a 16-bit value from an I/O port.
1470ba67974SThomas Huth  *
1480ba67974SThomas Huth  * Returns: Value read.
1490ba67974SThomas Huth  */
inw(uint16_t addr)1500ba67974SThomas Huth static inline uint16_t inw(uint16_t addr)
1510ba67974SThomas Huth {
1520ba67974SThomas Huth     return qtest_inw(global_qtest, addr);
1530ba67974SThomas Huth }
1540ba67974SThomas Huth 
1550ba67974SThomas Huth /**
1560ba67974SThomas Huth  * inl:
1570ba67974SThomas Huth  * @addr: I/O port to read from.
1580ba67974SThomas Huth  *
1590ba67974SThomas Huth  * Reads a 32-bit value from an I/O port.
1600ba67974SThomas Huth  *
1610ba67974SThomas Huth  * Returns: Value read.
1620ba67974SThomas Huth  */
inl(uint16_t addr)1630ba67974SThomas Huth static inline uint32_t inl(uint16_t addr)
1640ba67974SThomas Huth {
1650ba67974SThomas Huth     return qtest_inl(global_qtest, addr);
1660ba67974SThomas Huth }
1670ba67974SThomas Huth 
1680ba67974SThomas Huth /**
1690ba67974SThomas Huth  * writeb:
1700ba67974SThomas Huth  * @addr: Guest address to write to.
1710ba67974SThomas Huth  * @value: Value being written.
1720ba67974SThomas Huth  *
1730ba67974SThomas Huth  * Writes an 8-bit value to guest memory.
1740ba67974SThomas Huth  */
writeb(uint64_t addr,uint8_t value)1750ba67974SThomas Huth static inline void writeb(uint64_t addr, uint8_t value)
1760ba67974SThomas Huth {
1770ba67974SThomas Huth     qtest_writeb(global_qtest, addr, value);
1780ba67974SThomas Huth }
1790ba67974SThomas Huth 
1800ba67974SThomas Huth /**
1810ba67974SThomas Huth  * writew:
1820ba67974SThomas Huth  * @addr: Guest address to write to.
1830ba67974SThomas Huth  * @value: Value being written.
1840ba67974SThomas Huth  *
1850ba67974SThomas Huth  * Writes a 16-bit value to guest memory.
1860ba67974SThomas Huth  */
writew(uint64_t addr,uint16_t value)1870ba67974SThomas Huth static inline void writew(uint64_t addr, uint16_t value)
1880ba67974SThomas Huth {
1890ba67974SThomas Huth     qtest_writew(global_qtest, addr, value);
1900ba67974SThomas Huth }
1910ba67974SThomas Huth 
1920ba67974SThomas Huth /**
1930ba67974SThomas Huth  * writel:
1940ba67974SThomas Huth  * @addr: Guest address to write to.
1950ba67974SThomas Huth  * @value: Value being written.
1960ba67974SThomas Huth  *
1970ba67974SThomas Huth  * Writes a 32-bit value to guest memory.
1980ba67974SThomas Huth  */
writel(uint64_t addr,uint32_t value)1990ba67974SThomas Huth static inline void writel(uint64_t addr, uint32_t value)
2000ba67974SThomas Huth {
2010ba67974SThomas Huth     qtest_writel(global_qtest, addr, value);
2020ba67974SThomas Huth }
2030ba67974SThomas Huth 
2040ba67974SThomas Huth /**
2050ba67974SThomas Huth  * writeq:
2060ba67974SThomas Huth  * @addr: Guest address to write to.
2070ba67974SThomas Huth  * @value: Value being written.
2080ba67974SThomas Huth  *
2090ba67974SThomas Huth  * Writes a 64-bit value to guest memory.
2100ba67974SThomas Huth  */
writeq(uint64_t addr,uint64_t value)2110ba67974SThomas Huth static inline void writeq(uint64_t addr, uint64_t value)
2120ba67974SThomas Huth {
2130ba67974SThomas Huth     qtest_writeq(global_qtest, addr, value);
2140ba67974SThomas Huth }
2150ba67974SThomas Huth 
2160ba67974SThomas Huth /**
2170ba67974SThomas Huth  * readb:
2180ba67974SThomas Huth  * @addr: Guest address to read from.
2190ba67974SThomas Huth  *
2200ba67974SThomas Huth  * Reads an 8-bit value from guest memory.
2210ba67974SThomas Huth  *
2220ba67974SThomas Huth  * Returns: Value read.
2230ba67974SThomas Huth  */
readb(uint64_t addr)2240ba67974SThomas Huth static inline uint8_t readb(uint64_t addr)
2250ba67974SThomas Huth {
2260ba67974SThomas Huth     return qtest_readb(global_qtest, addr);
2270ba67974SThomas Huth }
2280ba67974SThomas Huth 
2290ba67974SThomas Huth /**
2300ba67974SThomas Huth  * readw:
2310ba67974SThomas Huth  * @addr: Guest address to read from.
2320ba67974SThomas Huth  *
2330ba67974SThomas Huth  * Reads a 16-bit value from guest memory.
2340ba67974SThomas Huth  *
2350ba67974SThomas Huth  * Returns: Value read.
2360ba67974SThomas Huth  */
readw(uint64_t addr)2370ba67974SThomas Huth static inline uint16_t readw(uint64_t addr)
2380ba67974SThomas Huth {
2390ba67974SThomas Huth     return qtest_readw(global_qtest, addr);
2400ba67974SThomas Huth }
2410ba67974SThomas Huth 
2420ba67974SThomas Huth /**
2430ba67974SThomas Huth  * readl:
2440ba67974SThomas Huth  * @addr: Guest address to read from.
2450ba67974SThomas Huth  *
2460ba67974SThomas Huth  * Reads a 32-bit value from guest memory.
2470ba67974SThomas Huth  *
2480ba67974SThomas Huth  * Returns: Value read.
2490ba67974SThomas Huth  */
readl(uint64_t addr)2500ba67974SThomas Huth static inline uint32_t readl(uint64_t addr)
2510ba67974SThomas Huth {
2520ba67974SThomas Huth     return qtest_readl(global_qtest, addr);
2530ba67974SThomas Huth }
2540ba67974SThomas Huth 
2550ba67974SThomas Huth /**
2560ba67974SThomas Huth  * readq:
2570ba67974SThomas Huth  * @addr: Guest address to read from.
2580ba67974SThomas Huth  *
2590ba67974SThomas Huth  * Reads a 64-bit value from guest memory.
2600ba67974SThomas Huth  *
2610ba67974SThomas Huth  * Returns: Value read.
2620ba67974SThomas Huth  */
readq(uint64_t addr)2630ba67974SThomas Huth static inline uint64_t readq(uint64_t addr)
2640ba67974SThomas Huth {
2650ba67974SThomas Huth     return qtest_readq(global_qtest, addr);
2660ba67974SThomas Huth }
2670ba67974SThomas Huth 
2680ba67974SThomas Huth /**
2690ba67974SThomas Huth  * memread:
2700ba67974SThomas Huth  * @addr: Guest address to read from.
2710ba67974SThomas Huth  * @data: Pointer to where memory contents will be stored.
2720ba67974SThomas Huth  * @size: Number of bytes to read.
2730ba67974SThomas Huth  *
2740ba67974SThomas Huth  * Read guest memory into a buffer.
2750ba67974SThomas Huth  */
memread(uint64_t addr,void * data,size_t size)2760ba67974SThomas Huth static inline void memread(uint64_t addr, void *data, size_t size)
2770ba67974SThomas Huth {
2780ba67974SThomas Huth     qtest_memread(global_qtest, addr, data, size);
2790ba67974SThomas Huth }
2800ba67974SThomas Huth 
2810ba67974SThomas Huth /**
2820ba67974SThomas Huth  * memwrite:
2830ba67974SThomas Huth  * @addr: Guest address to write to.
2840ba67974SThomas Huth  * @data: Pointer to the bytes that will be written to guest memory.
2850ba67974SThomas Huth  * @size: Number of bytes to write.
2860ba67974SThomas Huth  *
2870ba67974SThomas Huth  * Write a buffer to guest memory.
2880ba67974SThomas Huth  */
memwrite(uint64_t addr,const void * data,size_t size)2890ba67974SThomas Huth static inline void memwrite(uint64_t addr, const void *data, size_t size)
2900ba67974SThomas Huth {
2910ba67974SThomas Huth     qtest_memwrite(global_qtest, addr, data, size);
2920ba67974SThomas Huth }
2930ba67974SThomas Huth 
2940ba67974SThomas Huth /**
2950ba67974SThomas Huth  * clock_step_next:
2960ba67974SThomas Huth  *
2970ba67974SThomas Huth  * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
2980ba67974SThomas Huth  *
2990ba67974SThomas Huth  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
3000ba67974SThomas Huth  */
clock_step_next(void)3010ba67974SThomas Huth static inline int64_t clock_step_next(void)
3020ba67974SThomas Huth {
3030ba67974SThomas Huth     return qtest_clock_step_next(global_qtest);
3040ba67974SThomas Huth }
3050ba67974SThomas Huth 
3060ba67974SThomas Huth /**
3070ba67974SThomas Huth  * clock_step:
3080ba67974SThomas Huth  * @step: Number of nanoseconds to advance the clock by.
3090ba67974SThomas Huth  *
3100ba67974SThomas Huth  * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
3110ba67974SThomas Huth  *
3120ba67974SThomas Huth  * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
3130ba67974SThomas Huth  */
clock_step(int64_t step)3140ba67974SThomas Huth static inline int64_t clock_step(int64_t step)
3150ba67974SThomas Huth {
3160ba67974SThomas Huth     return qtest_clock_step(global_qtest, step);
3170ba67974SThomas Huth }
3180ba67974SThomas Huth 
3190ba67974SThomas Huth #endif
320