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