1 /* 2 * QTest - wrappers for test with single QEMU instances 3 * 4 * Copyright IBM, Corp. 2012 5 * Copyright Red Hat, Inc. 2012 6 * Copyright SUSE LINUX Products GmbH 2013 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or later. 9 * See the COPYING file in the top-level directory. 10 */ 11 #ifndef LIBQTEST_SINGLE_H 12 #define LIBQTEST_SINGLE_H 13 14 #include "libqos/libqtest.h" 15 16 QTestState *global_qtest __attribute__((common, weak)); 17 18 /** 19 * qtest_start: 20 * @args: other arguments to pass to QEMU 21 * 22 * Start QEMU and assign the resulting #QTestState to a global variable. 23 * The global variable is used by "shortcut" functions documented below. 24 * 25 * Returns: #QTestState instance. 26 */ 27 static inline QTestState *qtest_start(const char *args) 28 { 29 global_qtest = qtest_init(args); 30 return global_qtest; 31 } 32 33 /** 34 * qtest_end: 35 * 36 * Shut down the QEMU process started by qtest_start(). 37 */ 38 static inline void qtest_end(void) 39 { 40 if (!global_qtest) { 41 return; 42 } 43 qtest_quit(global_qtest); 44 global_qtest = NULL; 45 } 46 47 /** 48 * qmp: 49 * @fmt...: QMP message to send to qemu, formatted like 50 * qobject_from_jsonf_nofail(). See parse_escape() for what's 51 * supported after '%'. 52 * 53 * Sends a QMP message to QEMU and returns the response. 54 */ 55 GCC_FMT_ATTR(1, 2) 56 static inline QDict *qmp(const char *fmt, ...) 57 { 58 va_list ap; 59 QDict *response; 60 61 va_start(ap, fmt); 62 response = qtest_vqmp(global_qtest, fmt, ap); 63 va_end(ap); 64 return response; 65 } 66 67 /** 68 * qmp_eventwait: 69 * @s: #event event to wait for. 70 * 71 * Continuously polls for QMP responses until it receives the desired event. 72 */ 73 static inline void qmp_eventwait(const char *event) 74 { 75 return qtest_qmp_eventwait(global_qtest, event); 76 } 77 78 /** 79 * get_irq: 80 * @num: Interrupt to observe. 81 * 82 * Returns: The level of the @num interrupt. 83 */ 84 static inline bool get_irq(int num) 85 { 86 return qtest_get_irq(global_qtest, num); 87 } 88 89 /** 90 * outb: 91 * @addr: I/O port to write to. 92 * @value: Value being written. 93 * 94 * Write an 8-bit value to an I/O port. 95 */ 96 static inline void outb(uint16_t addr, uint8_t value) 97 { 98 qtest_outb(global_qtest, addr, value); 99 } 100 101 /** 102 * outw: 103 * @addr: I/O port to write to. 104 * @value: Value being written. 105 * 106 * Write a 16-bit value to an I/O port. 107 */ 108 static inline void outw(uint16_t addr, uint16_t value) 109 { 110 qtest_outw(global_qtest, addr, value); 111 } 112 113 /** 114 * outl: 115 * @addr: I/O port to write to. 116 * @value: Value being written. 117 * 118 * Write a 32-bit value to an I/O port. 119 */ 120 static inline void outl(uint16_t addr, uint32_t value) 121 { 122 qtest_outl(global_qtest, addr, value); 123 } 124 125 /** 126 * inb: 127 * @addr: I/O port to read from. 128 * 129 * Reads an 8-bit value from an I/O port. 130 * 131 * Returns: Value read. 132 */ 133 static inline uint8_t inb(uint16_t addr) 134 { 135 return qtest_inb(global_qtest, addr); 136 } 137 138 /** 139 * inw: 140 * @addr: I/O port to read from. 141 * 142 * Reads a 16-bit value from an I/O port. 143 * 144 * Returns: Value read. 145 */ 146 static inline uint16_t inw(uint16_t addr) 147 { 148 return qtest_inw(global_qtest, addr); 149 } 150 151 /** 152 * inl: 153 * @addr: I/O port to read from. 154 * 155 * Reads a 32-bit value from an I/O port. 156 * 157 * Returns: Value read. 158 */ 159 static inline uint32_t inl(uint16_t addr) 160 { 161 return qtest_inl(global_qtest, addr); 162 } 163 164 /** 165 * writeb: 166 * @addr: Guest address to write to. 167 * @value: Value being written. 168 * 169 * Writes an 8-bit value to guest memory. 170 */ 171 static inline void writeb(uint64_t addr, uint8_t value) 172 { 173 qtest_writeb(global_qtest, addr, value); 174 } 175 176 /** 177 * writew: 178 * @addr: Guest address to write to. 179 * @value: Value being written. 180 * 181 * Writes a 16-bit value to guest memory. 182 */ 183 static inline void writew(uint64_t addr, uint16_t value) 184 { 185 qtest_writew(global_qtest, addr, value); 186 } 187 188 /** 189 * writel: 190 * @addr: Guest address to write to. 191 * @value: Value being written. 192 * 193 * Writes a 32-bit value to guest memory. 194 */ 195 static inline void writel(uint64_t addr, uint32_t value) 196 { 197 qtest_writel(global_qtest, addr, value); 198 } 199 200 /** 201 * writeq: 202 * @addr: Guest address to write to. 203 * @value: Value being written. 204 * 205 * Writes a 64-bit value to guest memory. 206 */ 207 static inline void writeq(uint64_t addr, uint64_t value) 208 { 209 qtest_writeq(global_qtest, addr, value); 210 } 211 212 /** 213 * readb: 214 * @addr: Guest address to read from. 215 * 216 * Reads an 8-bit value from guest memory. 217 * 218 * Returns: Value read. 219 */ 220 static inline uint8_t readb(uint64_t addr) 221 { 222 return qtest_readb(global_qtest, addr); 223 } 224 225 /** 226 * readw: 227 * @addr: Guest address to read from. 228 * 229 * Reads a 16-bit value from guest memory. 230 * 231 * Returns: Value read. 232 */ 233 static inline uint16_t readw(uint64_t addr) 234 { 235 return qtest_readw(global_qtest, addr); 236 } 237 238 /** 239 * readl: 240 * @addr: Guest address to read from. 241 * 242 * Reads a 32-bit value from guest memory. 243 * 244 * Returns: Value read. 245 */ 246 static inline uint32_t readl(uint64_t addr) 247 { 248 return qtest_readl(global_qtest, addr); 249 } 250 251 /** 252 * readq: 253 * @addr: Guest address to read from. 254 * 255 * Reads a 64-bit value from guest memory. 256 * 257 * Returns: Value read. 258 */ 259 static inline uint64_t readq(uint64_t addr) 260 { 261 return qtest_readq(global_qtest, addr); 262 } 263 264 /** 265 * memread: 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 */ 272 static inline void memread(uint64_t addr, void *data, size_t size) 273 { 274 qtest_memread(global_qtest, addr, data, size); 275 } 276 277 /** 278 * memwrite: 279 * @addr: Guest address to write to. 280 * @data: Pointer to the bytes that will be written to guest memory. 281 * @size: Number of bytes to write. 282 * 283 * Write a buffer to guest memory. 284 */ 285 static inline void memwrite(uint64_t addr, const void *data, size_t size) 286 { 287 qtest_memwrite(global_qtest, addr, data, size); 288 } 289 290 /** 291 * clock_step_next: 292 * 293 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline. 294 * 295 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds. 296 */ 297 static inline int64_t clock_step_next(void) 298 { 299 return qtest_clock_step_next(global_qtest); 300 } 301 302 /** 303 * clock_step: 304 * @step: Number of nanoseconds to advance the clock by. 305 * 306 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds. 307 * 308 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds. 309 */ 310 static inline int64_t clock_step(int64_t step) 311 { 312 return qtest_clock_step(global_qtest, step); 313 } 314 315 #endif 316