pseries: Fix stalls on hypervisor virtual console
[qemu/ar7.git] / tests / libqtest.h
bloba6e99bd023f16b35363018451aeb0b0cc02151ca
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
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.
17 #ifndef LIBQTEST_H
18 #define LIBQTEST_H
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <stdarg.h>
24 #include <sys/types.h>
26 typedef struct QTestState QTestState;
28 extern QTestState *global_qtest;
30 /**
31 * qtest_init:
32 * @extra_args: other arguments to pass to QEMU.
34 * Returns: #QTestState instance.
36 QTestState *qtest_init(const char *extra_args);
38 /**
39 * qtest_quit:
40 * @s: #QTestState instance to operate on.
42 * Shut down the QEMU process associated to @s.
44 void qtest_quit(QTestState *s);
46 /**
47 * qtest_qmp:
48 * @s: #QTestState instance to operate on.
49 * @fmt...: QMP message to send to qemu
51 * Sends a QMP message to QEMU
53 void qtest_qmp(QTestState *s, const char *fmt, ...);
55 /**
56 * qtest_qmpv:
57 * @s: #QTestState instance to operate on.
58 * @fmt: QMP message to send to QEMU
59 * @ap: QMP message arguments
61 * Sends a QMP message to QEMU.
63 void qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
65 /**
66 * qtest_get_irq:
67 * @s: #QTestState instance to operate on.
68 * @num: Interrupt to observe.
70 * Returns: The level of the @num interrupt.
72 bool qtest_get_irq(QTestState *s, int num);
74 /**
75 * qtest_irq_intercept_in:
76 * @s: #QTestState instance to operate on.
77 * @string: QOM path of a device.
79 * Associate qtest irqs with the GPIO-in pins of the device
80 * whose path is specified by @string.
82 void qtest_irq_intercept_in(QTestState *s, const char *string);
84 /**
85 * qtest_irq_intercept_out:
86 * @s: #QTestState instance to operate on.
87 * @string: QOM path of a device.
89 * Associate qtest irqs with the GPIO-out pins of the device
90 * whose path is specified by @string.
92 void qtest_irq_intercept_out(QTestState *s, const char *string);
94 /**
95 * qtest_outb:
96 * @s: #QTestState instance to operate on.
97 * @addr: I/O port to write to.
98 * @value: Value being written.
100 * Write an 8-bit value to an I/O port.
102 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
105 * qtest_outw:
106 * @s: #QTestState instance to operate on.
107 * @addr: I/O port to write to.
108 * @value: Value being written.
110 * Write a 16-bit value to an I/O port.
112 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
115 * qtest_outl:
116 * @s: #QTestState instance to operate on.
117 * @addr: I/O port to write to.
118 * @value: Value being written.
120 * Write a 32-bit value to an I/O port.
122 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
125 * qtest_inb:
126 * @s: #QTestState instance to operate on.
127 * @addr: I/O port to read from.
129 * Returns an 8-bit value from an I/O port.
131 uint8_t qtest_inb(QTestState *s, uint16_t addr);
134 * qtest_inw:
135 * @s: #QTestState instance to operate on.
136 * @addr: I/O port to read from.
138 * Returns a 16-bit value from an I/O port.
140 uint16_t qtest_inw(QTestState *s, uint16_t addr);
143 * qtest_inl:
144 * @s: #QTestState instance to operate on.
145 * @addr: I/O port to read from.
147 * Returns a 32-bit value from an I/O port.
149 uint32_t qtest_inl(QTestState *s, uint16_t addr);
152 * qtest_writeb:
153 * @s: #QTestState instance to operate on.
154 * @addr: Guest address to write to.
155 * @value: Value being written.
157 * Writes an 8-bit value to memory.
159 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
162 * qtest_writew:
163 * @s: #QTestState instance to operate on.
164 * @addr: Guest address to write to.
165 * @value: Value being written.
167 * Writes a 16-bit value to memory.
169 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
172 * qtest_writel:
173 * @s: #QTestState instance to operate on.
174 * @addr: Guest address to write to.
175 * @value: Value being written.
177 * Writes a 32-bit value to memory.
179 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
182 * qtest_writeq:
183 * @s: #QTestState instance to operate on.
184 * @addr: Guest address to write to.
185 * @value: Value being written.
187 * Writes a 64-bit value to memory.
189 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
192 * qtest_readb:
193 * @s: #QTestState instance to operate on.
194 * @addr: Guest address to read from.
196 * Reads an 8-bit value from memory.
198 * Returns: Value read.
200 uint8_t qtest_readb(QTestState *s, uint64_t addr);
203 * qtest_readw:
204 * @s: #QTestState instance to operate on.
205 * @addr: Guest address to read from.
207 * Reads a 16-bit value from memory.
209 * Returns: Value read.
211 uint16_t qtest_readw(QTestState *s, uint64_t addr);
214 * qtest_readl:
215 * @s: #QTestState instance to operate on.
216 * @addr: Guest address to read from.
218 * Reads a 32-bit value from memory.
220 * Returns: Value read.
222 uint32_t qtest_readl(QTestState *s, uint64_t addr);
225 * qtest_readq:
226 * @s: #QTestState instance to operate on.
227 * @addr: Guest address to read from.
229 * Reads a 64-bit value from memory.
231 * Returns: Value read.
233 uint64_t qtest_readq(QTestState *s, uint64_t addr);
236 * qtest_memread:
237 * @s: #QTestState instance to operate on.
238 * @addr: Guest address to read from.
239 * @data: Pointer to where memory contents will be stored.
240 * @size: Number of bytes to read.
242 * Read guest memory into a buffer.
244 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
247 * qtest_memwrite:
248 * @s: #QTestState instance to operate on.
249 * @addr: Guest address to write to.
250 * @data: Pointer to the bytes that will be written to guest memory.
251 * @size: Number of bytes to write.
253 * Write a buffer to guest memory.
255 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
258 * qtest_clock_step_next:
259 * @s: #QTestState instance to operate on.
261 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
263 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
265 int64_t qtest_clock_step_next(QTestState *s);
268 * qtest_clock_step:
269 * @s: QTestState instance to operate on.
270 * @step: Number of nanoseconds to advance the clock by.
272 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
274 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
276 int64_t qtest_clock_step(QTestState *s, int64_t step);
279 * qtest_clock_set:
280 * @s: QTestState instance to operate on.
281 * @val: Nanoseconds value to advance the clock to.
283 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
285 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
287 int64_t qtest_clock_set(QTestState *s, int64_t val);
290 * qtest_get_arch:
292 * Returns: The architecture for the QEMU executable under test.
294 const char *qtest_get_arch(void);
297 * qtest_add_func:
298 * @str: Test case path.
299 * @fn: Test case function
301 * Add a GTester testcase with the given name and function.
302 * The path is prefixed with the architecture under test, as
303 * returned by qtest_get_arch().
305 void qtest_add_func(const char *str, void (*fn));
308 * qtest_start:
309 * @args: other arguments to pass to QEMU
311 * Start QEMU and assign the resulting #QTestState to a global variable.
312 * The global variable is used by "shortcut" functions documented below.
314 * Returns: #QTestState instance.
316 static inline QTestState *qtest_start(const char *args)
318 global_qtest = qtest_init(args);
319 return global_qtest;
323 * qtest_end:
325 * Shut down the QEMU process started by qtest_start().
327 static inline void qtest_end(void)
329 qtest_quit(global_qtest);
330 global_qtest = NULL;
334 * qmp:
335 * @fmt...: QMP message to send to qemu
337 * Sends a QMP message to QEMU
339 static inline void qmp(const char *fmt, ...)
341 va_list ap;
343 va_start(ap, fmt);
344 qtest_qmpv(global_qtest, fmt, ap);
345 va_end(ap);
349 * get_irq:
350 * @num: Interrupt to observe.
352 * Returns: The level of the @num interrupt.
354 static inline bool get_irq(int num)
356 return qtest_get_irq(global_qtest, num);
360 * irq_intercept_in:
361 * @string: QOM path of a device.
363 * Associate qtest irqs with the GPIO-in pins of the device
364 * whose path is specified by @string.
366 static inline void irq_intercept_in(const char *string)
368 qtest_irq_intercept_in(global_qtest, string);
372 * qtest_irq_intercept_out:
373 * @string: QOM path of a device.
375 * Associate qtest irqs with the GPIO-out pins of the device
376 * whose path is specified by @string.
378 static inline void irq_intercept_out(const char *string)
380 qtest_irq_intercept_out(global_qtest, string);
384 * outb:
385 * @addr: I/O port to write to.
386 * @value: Value being written.
388 * Write an 8-bit value to an I/O port.
390 static inline void outb(uint16_t addr, uint8_t value)
392 qtest_outb(global_qtest, addr, value);
396 * outw:
397 * @addr: I/O port to write to.
398 * @value: Value being written.
400 * Write a 16-bit value to an I/O port.
402 static inline void outw(uint16_t addr, uint16_t value)
404 qtest_outw(global_qtest, addr, value);
408 * outl:
409 * @addr: I/O port to write to.
410 * @value: Value being written.
412 * Write a 32-bit value to an I/O port.
414 static inline void outl(uint16_t addr, uint32_t value)
416 qtest_outl(global_qtest, addr, value);
420 * inb:
421 * @addr: I/O port to read from.
423 * Reads an 8-bit value from an I/O port.
425 * Returns: Value read.
427 static inline uint8_t inb(uint16_t addr)
429 return qtest_inb(global_qtest, addr);
433 * inw:
434 * @addr: I/O port to read from.
436 * Reads a 16-bit value from an I/O port.
438 * Returns: Value read.
440 static inline uint16_t inw(uint16_t addr)
442 return qtest_inw(global_qtest, addr);
446 * inl:
447 * @addr: I/O port to read from.
449 * Reads a 32-bit value from an I/O port.
451 * Returns: Value read.
453 static inline uint32_t inl(uint16_t addr)
455 return qtest_inl(global_qtest, addr);
459 * writeb:
460 * @addr: Guest address to write to.
461 * @value: Value being written.
463 * Writes an 8-bit value to guest memory.
465 static inline void writeb(uint64_t addr, uint8_t value)
467 qtest_writeb(global_qtest, addr, value);
471 * writew:
472 * @addr: Guest address to write to.
473 * @value: Value being written.
475 * Writes a 16-bit value to guest memory.
477 static inline void writew(uint64_t addr, uint16_t value)
479 qtest_writew(global_qtest, addr, value);
483 * writel:
484 * @addr: Guest address to write to.
485 * @value: Value being written.
487 * Writes a 32-bit value to guest memory.
489 static inline void writel(uint64_t addr, uint32_t value)
491 qtest_writel(global_qtest, addr, value);
495 * writeq:
496 * @addr: Guest address to write to.
497 * @value: Value being written.
499 * Writes a 64-bit value to guest memory.
501 static inline void writeq(uint64_t addr, uint64_t value)
503 qtest_writeq(global_qtest, addr, value);
507 * readb:
508 * @addr: Guest address to read from.
510 * Reads an 8-bit value from guest memory.
512 * Returns: Value read.
514 static inline uint8_t readb(uint64_t addr)
516 return qtest_readb(global_qtest, addr);
520 * readw:
521 * @addr: Guest address to read from.
523 * Reads a 16-bit value from guest memory.
525 * Returns: Value read.
527 static inline uint16_t readw(uint64_t addr)
529 return qtest_readw(global_qtest, addr);
533 * readl:
534 * @addr: Guest address to read from.
536 * Reads a 32-bit value from guest memory.
538 * Returns: Value read.
540 static inline uint32_t readl(uint64_t addr)
542 return qtest_readl(global_qtest, addr);
546 * readq:
547 * @addr: Guest address to read from.
549 * Reads a 64-bit value from guest memory.
551 * Returns: Value read.
553 static inline uint64_t readq(uint64_t addr)
555 return qtest_readq(global_qtest, addr);
559 * memread:
560 * @addr: Guest address to read from.
561 * @data: Pointer to where memory contents will be stored.
562 * @size: Number of bytes to read.
564 * Read guest memory into a buffer.
566 static inline void memread(uint64_t addr, void *data, size_t size)
568 qtest_memread(global_qtest, addr, data, size);
572 * memwrite:
573 * @addr: Guest address to write to.
574 * @data: Pointer to the bytes that will be written to guest memory.
575 * @size: Number of bytes to write.
577 * Write a buffer to guest memory.
579 static inline void memwrite(uint64_t addr, const void *data, size_t size)
581 qtest_memwrite(global_qtest, addr, data, size);
585 * clock_step_next:
587 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
589 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
591 static inline int64_t clock_step_next(void)
593 return qtest_clock_step_next(global_qtest);
597 * clock_step:
598 * @step: Number of nanoseconds to advance the clock by.
600 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
602 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
604 static inline int64_t clock_step(int64_t step)
606 return qtest_clock_step(global_qtest, step);
610 * clock_set:
611 * @val: Nanoseconds value to advance the clock to.
613 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
615 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
617 static inline int64_t clock_set(int64_t val)
619 return qtest_clock_set(global_qtest, val);
622 #endif