libqtest-single.h 6.1 KB

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