qtest.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Test Server
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qemu-common.h"
  16. #include "cpu.h"
  17. #include "sysemu/qtest.h"
  18. #include "hw/qdev.h"
  19. #include "chardev/char-fe.h"
  20. #include "exec/ioport.h"
  21. #include "exec/memory.h"
  22. #include "hw/irq.h"
  23. #include "sysemu/accel.h"
  24. #include "sysemu/sysemu.h"
  25. #include "sysemu/cpus.h"
  26. #include "qemu/config-file.h"
  27. #include "qemu/option.h"
  28. #include "qemu/error-report.h"
  29. #include "qemu/cutils.h"
  30. #ifdef TARGET_PPC64
  31. #include "hw/ppc/spapr_rtas.h"
  32. #endif
  33. #define MAX_IRQ 256
  34. bool qtest_allowed;
  35. static DeviceState *irq_intercept_dev;
  36. static FILE *qtest_log_fp;
  37. static CharBackend qtest_chr;
  38. static GString *inbuf;
  39. static int irq_levels[MAX_IRQ];
  40. static qemu_timeval start_time;
  41. static bool qtest_opened;
  42. #define FMT_timeval "%ld.%06ld"
  43. /**
  44. * QTest Protocol
  45. *
  46. * Line based protocol, request/response based. Server can send async messages
  47. * so clients should always handle many async messages before the response
  48. * comes in.
  49. *
  50. * Valid requests
  51. *
  52. * Clock management:
  53. *
  54. * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
  55. * let you adjust the value of the clock (monotonically). All the commands
  56. * return the current value of the clock in nanoseconds.
  57. *
  58. * > clock_step
  59. * < OK VALUE
  60. *
  61. * Advance the clock to the next deadline. Useful when waiting for
  62. * asynchronous events.
  63. *
  64. * > clock_step NS
  65. * < OK VALUE
  66. *
  67. * Advance the clock by NS nanoseconds.
  68. *
  69. * > clock_set NS
  70. * < OK VALUE
  71. *
  72. * Advance the clock to NS nanoseconds (do nothing if it's already past).
  73. *
  74. * PIO and memory access:
  75. *
  76. * > outb ADDR VALUE
  77. * < OK
  78. *
  79. * > outw ADDR VALUE
  80. * < OK
  81. *
  82. * > outl ADDR VALUE
  83. * < OK
  84. *
  85. * > inb ADDR
  86. * < OK VALUE
  87. *
  88. * > inw ADDR
  89. * < OK VALUE
  90. *
  91. * > inl ADDR
  92. * < OK VALUE
  93. *
  94. * > writeb ADDR VALUE
  95. * < OK
  96. *
  97. * > writew ADDR VALUE
  98. * < OK
  99. *
  100. * > writel ADDR VALUE
  101. * < OK
  102. *
  103. * > writeq ADDR VALUE
  104. * < OK
  105. *
  106. * > readb ADDR
  107. * < OK VALUE
  108. *
  109. * > readw ADDR
  110. * < OK VALUE
  111. *
  112. * > readl ADDR
  113. * < OK VALUE
  114. *
  115. * > readq ADDR
  116. * < OK VALUE
  117. *
  118. * > read ADDR SIZE
  119. * < OK DATA
  120. *
  121. * > write ADDR SIZE DATA
  122. * < OK
  123. *
  124. * > b64read ADDR SIZE
  125. * < OK B64_DATA
  126. *
  127. * > b64write ADDR SIZE B64_DATA
  128. * < OK
  129. *
  130. * > memset ADDR SIZE VALUE
  131. * < OK
  132. *
  133. * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
  134. * For 'memset' a zero size is permitted and does nothing.
  135. *
  136. * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
  137. * than the expected size, the value will be zero filled at the end of the data
  138. * sequence.
  139. *
  140. * B64_DATA is an arbitrarily long base64 encoded string.
  141. * If the sizes do not match, the data will be truncated.
  142. *
  143. * IRQ management:
  144. *
  145. * > irq_intercept_in QOM-PATH
  146. * < OK
  147. *
  148. * > irq_intercept_out QOM-PATH
  149. * < OK
  150. *
  151. * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
  152. * QOM-PATH. When the pin is triggered, one of the following async messages
  153. * will be printed to the qtest stream:
  154. *
  155. * IRQ raise NUM
  156. * IRQ lower NUM
  157. *
  158. * where NUM is an IRQ number. For the PC, interrupts can be intercepted
  159. * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
  160. * NUM=0 even though it is remapped to GSI 2).
  161. */
  162. static int hex2nib(char ch)
  163. {
  164. if (ch >= '0' && ch <= '9') {
  165. return ch - '0';
  166. } else if (ch >= 'a' && ch <= 'f') {
  167. return 10 + (ch - 'a');
  168. } else if (ch >= 'A' && ch <= 'F') {
  169. return 10 + (ch - 'A');
  170. } else {
  171. return -1;
  172. }
  173. }
  174. static void qtest_get_time(qemu_timeval *tv)
  175. {
  176. qemu_gettimeofday(tv);
  177. tv->tv_sec -= start_time.tv_sec;
  178. tv->tv_usec -= start_time.tv_usec;
  179. if (tv->tv_usec < 0) {
  180. tv->tv_usec += 1000000;
  181. tv->tv_sec -= 1;
  182. }
  183. }
  184. static void qtest_send_prefix(CharBackend *chr)
  185. {
  186. qemu_timeval tv;
  187. if (!qtest_log_fp || !qtest_opened) {
  188. return;
  189. }
  190. qtest_get_time(&tv);
  191. fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
  192. (long) tv.tv_sec, (long) tv.tv_usec);
  193. }
  194. static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
  195. {
  196. va_list ap;
  197. if (!qtest_log_fp || !qtest_opened) {
  198. return;
  199. }
  200. qtest_send_prefix(NULL);
  201. va_start(ap, fmt);
  202. vfprintf(qtest_log_fp, fmt, ap);
  203. va_end(ap);
  204. }
  205. static void do_qtest_send(CharBackend *chr, const char *str, size_t len)
  206. {
  207. qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
  208. if (qtest_log_fp && qtest_opened) {
  209. fprintf(qtest_log_fp, "%s", str);
  210. }
  211. }
  212. static void qtest_send(CharBackend *chr, const char *str)
  213. {
  214. do_qtest_send(chr, str, strlen(str));
  215. }
  216. static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
  217. const char *fmt, ...)
  218. {
  219. va_list ap;
  220. gchar *buffer;
  221. va_start(ap, fmt);
  222. buffer = g_strdup_vprintf(fmt, ap);
  223. qtest_send(chr, buffer);
  224. g_free(buffer);
  225. va_end(ap);
  226. }
  227. static void qtest_irq_handler(void *opaque, int n, int level)
  228. {
  229. qemu_irq old_irq = *(qemu_irq *)opaque;
  230. qemu_set_irq(old_irq, level);
  231. if (irq_levels[n] != level) {
  232. CharBackend *chr = &qtest_chr;
  233. irq_levels[n] = level;
  234. qtest_send_prefix(chr);
  235. qtest_sendf(chr, "IRQ %s %d\n",
  236. level ? "raise" : "lower", n);
  237. }
  238. }
  239. static void qtest_process_command(CharBackend *chr, gchar **words)
  240. {
  241. const gchar *command;
  242. g_assert(words);
  243. command = words[0];
  244. if (qtest_log_fp) {
  245. qemu_timeval tv;
  246. int i;
  247. qtest_get_time(&tv);
  248. fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
  249. (long) tv.tv_sec, (long) tv.tv_usec);
  250. for (i = 0; words[i]; i++) {
  251. fprintf(qtest_log_fp, " %s", words[i]);
  252. }
  253. fprintf(qtest_log_fp, "\n");
  254. }
  255. g_assert(command);
  256. if (strcmp(words[0], "irq_intercept_out") == 0
  257. || strcmp(words[0], "irq_intercept_in") == 0) {
  258. DeviceState *dev;
  259. NamedGPIOList *ngl;
  260. g_assert(words[1]);
  261. dev = DEVICE(object_resolve_path(words[1], NULL));
  262. if (!dev) {
  263. qtest_send_prefix(chr);
  264. qtest_send(chr, "FAIL Unknown device\n");
  265. return;
  266. }
  267. if (irq_intercept_dev) {
  268. qtest_send_prefix(chr);
  269. if (irq_intercept_dev != dev) {
  270. qtest_send(chr, "FAIL IRQ intercept already enabled\n");
  271. } else {
  272. qtest_send(chr, "OK\n");
  273. }
  274. return;
  275. }
  276. QLIST_FOREACH(ngl, &dev->gpios, node) {
  277. /* We don't support intercept of named GPIOs yet */
  278. if (ngl->name) {
  279. continue;
  280. }
  281. if (words[0][14] == 'o') {
  282. int i;
  283. for (i = 0; i < ngl->num_out; ++i) {
  284. qemu_irq *disconnected = g_new0(qemu_irq, 1);
  285. qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
  286. disconnected, i);
  287. *disconnected = qdev_intercept_gpio_out(dev, icpt,
  288. ngl->name, i);
  289. }
  290. } else {
  291. qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
  292. ngl->num_in);
  293. }
  294. }
  295. irq_intercept_dev = dev;
  296. qtest_send_prefix(chr);
  297. qtest_send(chr, "OK\n");
  298. } else if (strcmp(words[0], "outb") == 0 ||
  299. strcmp(words[0], "outw") == 0 ||
  300. strcmp(words[0], "outl") == 0) {
  301. unsigned long addr;
  302. unsigned long value;
  303. int ret;
  304. g_assert(words[1] && words[2]);
  305. ret = qemu_strtoul(words[1], NULL, 0, &addr);
  306. g_assert(ret == 0);
  307. ret = qemu_strtoul(words[2], NULL, 0, &value);
  308. g_assert(ret == 0);
  309. g_assert(addr <= 0xffff);
  310. if (words[0][3] == 'b') {
  311. cpu_outb(addr, value);
  312. } else if (words[0][3] == 'w') {
  313. cpu_outw(addr, value);
  314. } else if (words[0][3] == 'l') {
  315. cpu_outl(addr, value);
  316. }
  317. qtest_send_prefix(chr);
  318. qtest_send(chr, "OK\n");
  319. } else if (strcmp(words[0], "inb") == 0 ||
  320. strcmp(words[0], "inw") == 0 ||
  321. strcmp(words[0], "inl") == 0) {
  322. unsigned long addr;
  323. uint32_t value = -1U;
  324. int ret;
  325. g_assert(words[1]);
  326. ret = qemu_strtoul(words[1], NULL, 0, &addr);
  327. g_assert(ret == 0);
  328. g_assert(addr <= 0xffff);
  329. if (words[0][2] == 'b') {
  330. value = cpu_inb(addr);
  331. } else if (words[0][2] == 'w') {
  332. value = cpu_inw(addr);
  333. } else if (words[0][2] == 'l') {
  334. value = cpu_inl(addr);
  335. }
  336. qtest_send_prefix(chr);
  337. qtest_sendf(chr, "OK 0x%04x\n", value);
  338. } else if (strcmp(words[0], "writeb") == 0 ||
  339. strcmp(words[0], "writew") == 0 ||
  340. strcmp(words[0], "writel") == 0 ||
  341. strcmp(words[0], "writeq") == 0) {
  342. uint64_t addr;
  343. uint64_t value;
  344. int ret;
  345. g_assert(words[1] && words[2]);
  346. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  347. g_assert(ret == 0);
  348. ret = qemu_strtou64(words[2], NULL, 0, &value);
  349. g_assert(ret == 0);
  350. if (words[0][5] == 'b') {
  351. uint8_t data = value;
  352. cpu_physical_memory_write(addr, &data, 1);
  353. } else if (words[0][5] == 'w') {
  354. uint16_t data = value;
  355. tswap16s(&data);
  356. cpu_physical_memory_write(addr, &data, 2);
  357. } else if (words[0][5] == 'l') {
  358. uint32_t data = value;
  359. tswap32s(&data);
  360. cpu_physical_memory_write(addr, &data, 4);
  361. } else if (words[0][5] == 'q') {
  362. uint64_t data = value;
  363. tswap64s(&data);
  364. cpu_physical_memory_write(addr, &data, 8);
  365. }
  366. qtest_send_prefix(chr);
  367. qtest_send(chr, "OK\n");
  368. } else if (strcmp(words[0], "readb") == 0 ||
  369. strcmp(words[0], "readw") == 0 ||
  370. strcmp(words[0], "readl") == 0 ||
  371. strcmp(words[0], "readq") == 0) {
  372. uint64_t addr;
  373. uint64_t value = UINT64_C(-1);
  374. int ret;
  375. g_assert(words[1]);
  376. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  377. g_assert(ret == 0);
  378. if (words[0][4] == 'b') {
  379. uint8_t data;
  380. cpu_physical_memory_read(addr, &data, 1);
  381. value = data;
  382. } else if (words[0][4] == 'w') {
  383. uint16_t data;
  384. cpu_physical_memory_read(addr, &data, 2);
  385. value = tswap16(data);
  386. } else if (words[0][4] == 'l') {
  387. uint32_t data;
  388. cpu_physical_memory_read(addr, &data, 4);
  389. value = tswap32(data);
  390. } else if (words[0][4] == 'q') {
  391. cpu_physical_memory_read(addr, &value, 8);
  392. tswap64s(&value);
  393. }
  394. qtest_send_prefix(chr);
  395. qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
  396. } else if (strcmp(words[0], "read") == 0) {
  397. uint64_t addr, len, i;
  398. uint8_t *data;
  399. char *enc;
  400. int ret;
  401. g_assert(words[1] && words[2]);
  402. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  403. g_assert(ret == 0);
  404. ret = qemu_strtou64(words[2], NULL, 0, &len);
  405. g_assert(ret == 0);
  406. /* We'd send garbage to libqtest if len is 0 */
  407. g_assert(len);
  408. data = g_malloc(len);
  409. cpu_physical_memory_read(addr, data, len);
  410. enc = g_malloc(2 * len + 1);
  411. for (i = 0; i < len; i++) {
  412. sprintf(&enc[i * 2], "%02x", data[i]);
  413. }
  414. qtest_send_prefix(chr);
  415. qtest_sendf(chr, "OK 0x%s\n", enc);
  416. g_free(data);
  417. g_free(enc);
  418. } else if (strcmp(words[0], "b64read") == 0) {
  419. uint64_t addr, len;
  420. uint8_t *data;
  421. gchar *b64_data;
  422. int ret;
  423. g_assert(words[1] && words[2]);
  424. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  425. g_assert(ret == 0);
  426. ret = qemu_strtou64(words[2], NULL, 0, &len);
  427. g_assert(ret == 0);
  428. data = g_malloc(len);
  429. cpu_physical_memory_read(addr, data, len);
  430. b64_data = g_base64_encode(data, len);
  431. qtest_send_prefix(chr);
  432. qtest_sendf(chr, "OK %s\n", b64_data);
  433. g_free(data);
  434. g_free(b64_data);
  435. } else if (strcmp(words[0], "write") == 0) {
  436. uint64_t addr, len, i;
  437. uint8_t *data;
  438. size_t data_len;
  439. int ret;
  440. g_assert(words[1] && words[2] && words[3]);
  441. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  442. g_assert(ret == 0);
  443. ret = qemu_strtou64(words[2], NULL, 0, &len);
  444. g_assert(ret == 0);
  445. data_len = strlen(words[3]);
  446. if (data_len < 3) {
  447. qtest_send(chr, "ERR invalid argument size\n");
  448. return;
  449. }
  450. data = g_malloc(len);
  451. for (i = 0; i < len; i++) {
  452. if ((i * 2 + 4) <= data_len) {
  453. data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
  454. data[i] |= hex2nib(words[3][i * 2 + 3]);
  455. } else {
  456. data[i] = 0;
  457. }
  458. }
  459. cpu_physical_memory_write(addr, data, len);
  460. g_free(data);
  461. qtest_send_prefix(chr);
  462. qtest_send(chr, "OK\n");
  463. } else if (strcmp(words[0], "memset") == 0) {
  464. uint64_t addr, len;
  465. uint8_t *data;
  466. unsigned long pattern;
  467. int ret;
  468. g_assert(words[1] && words[2] && words[3]);
  469. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  470. g_assert(ret == 0);
  471. ret = qemu_strtou64(words[2], NULL, 0, &len);
  472. g_assert(ret == 0);
  473. ret = qemu_strtoul(words[3], NULL, 0, &pattern);
  474. g_assert(ret == 0);
  475. if (len) {
  476. data = g_malloc(len);
  477. memset(data, pattern, len);
  478. cpu_physical_memory_write(addr, data, len);
  479. g_free(data);
  480. }
  481. qtest_send_prefix(chr);
  482. qtest_send(chr, "OK\n");
  483. } else if (strcmp(words[0], "b64write") == 0) {
  484. uint64_t addr, len;
  485. uint8_t *data;
  486. size_t data_len;
  487. gsize out_len;
  488. int ret;
  489. g_assert(words[1] && words[2] && words[3]);
  490. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  491. g_assert(ret == 0);
  492. ret = qemu_strtou64(words[2], NULL, 0, &len);
  493. g_assert(ret == 0);
  494. data_len = strlen(words[3]);
  495. if (data_len < 3) {
  496. qtest_send(chr, "ERR invalid argument size\n");
  497. return;
  498. }
  499. data = g_base64_decode_inplace(words[3], &out_len);
  500. if (out_len != len) {
  501. qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
  502. "found %zu)\n",
  503. len, out_len);
  504. out_len = MIN(out_len, len);
  505. }
  506. cpu_physical_memory_write(addr, data, out_len);
  507. qtest_send_prefix(chr);
  508. qtest_send(chr, "OK\n");
  509. } else if (strcmp(words[0], "endianness") == 0) {
  510. qtest_send_prefix(chr);
  511. #if defined(TARGET_WORDS_BIGENDIAN)
  512. qtest_sendf(chr, "OK big\n");
  513. #else
  514. qtest_sendf(chr, "OK little\n");
  515. #endif
  516. #ifdef TARGET_PPC64
  517. } else if (strcmp(words[0], "rtas") == 0) {
  518. uint64_t res, args, ret;
  519. unsigned long nargs, nret;
  520. int rc;
  521. rc = qemu_strtoul(words[2], NULL, 0, &nargs);
  522. g_assert(rc == 0);
  523. rc = qemu_strtou64(words[3], NULL, 0, &args);
  524. g_assert(rc == 0);
  525. rc = qemu_strtoul(words[4], NULL, 0, &nret);
  526. g_assert(rc == 0);
  527. rc = qemu_strtou64(words[5], NULL, 0, &ret);
  528. g_assert(rc == 0);
  529. res = qtest_rtas_call(words[1], nargs, args, nret, ret);
  530. qtest_send_prefix(chr);
  531. qtest_sendf(chr, "OK %"PRIu64"\n", res);
  532. #endif
  533. } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
  534. int64_t ns;
  535. if (words[1]) {
  536. int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
  537. g_assert(ret == 0);
  538. } else {
  539. ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  540. }
  541. qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
  542. qtest_send_prefix(chr);
  543. qtest_sendf(chr, "OK %"PRIi64"\n",
  544. (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  545. } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
  546. int64_t ns;
  547. int ret;
  548. g_assert(words[1]);
  549. ret = qemu_strtoi64(words[1], NULL, 0, &ns);
  550. g_assert(ret == 0);
  551. qtest_clock_warp(ns);
  552. qtest_send_prefix(chr);
  553. qtest_sendf(chr, "OK %"PRIi64"\n",
  554. (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  555. } else {
  556. qtest_send_prefix(chr);
  557. qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
  558. }
  559. }
  560. static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
  561. {
  562. char *end;
  563. while ((end = strchr(inbuf->str, '\n')) != NULL) {
  564. size_t offset;
  565. GString *cmd;
  566. gchar **words;
  567. offset = end - inbuf->str;
  568. cmd = g_string_new_len(inbuf->str, offset);
  569. g_string_erase(inbuf, 0, offset + 1);
  570. words = g_strsplit(cmd->str, " ", 0);
  571. qtest_process_command(chr, words);
  572. g_strfreev(words);
  573. g_string_free(cmd, TRUE);
  574. }
  575. }
  576. static void qtest_read(void *opaque, const uint8_t *buf, int size)
  577. {
  578. CharBackend *chr = opaque;
  579. g_string_append_len(inbuf, (const gchar *)buf, size);
  580. qtest_process_inbuf(chr, inbuf);
  581. }
  582. static int qtest_can_read(void *opaque)
  583. {
  584. return 1024;
  585. }
  586. static void qtest_event(void *opaque, int event)
  587. {
  588. int i;
  589. switch (event) {
  590. case CHR_EVENT_OPENED:
  591. /*
  592. * We used to call qemu_system_reset() here, hoping we could
  593. * use the same process for multiple tests that way. Never
  594. * used. Injects an extra reset even when it's not used, and
  595. * that can mess up tests, e.g. -boot once.
  596. */
  597. for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
  598. irq_levels[i] = 0;
  599. }
  600. qemu_gettimeofday(&start_time);
  601. qtest_opened = true;
  602. if (qtest_log_fp) {
  603. fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
  604. (long) start_time.tv_sec, (long) start_time.tv_usec);
  605. }
  606. break;
  607. case CHR_EVENT_CLOSED:
  608. qtest_opened = false;
  609. if (qtest_log_fp) {
  610. qemu_timeval tv;
  611. qtest_get_time(&tv);
  612. fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
  613. (long) tv.tv_sec, (long) tv.tv_usec);
  614. }
  615. break;
  616. default:
  617. break;
  618. }
  619. }
  620. static int qtest_init_accel(MachineState *ms)
  621. {
  622. QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
  623. &error_abort);
  624. qemu_opt_set(opts, "shift", "0", &error_abort);
  625. configure_icount(opts, &error_abort);
  626. qemu_opts_del(opts);
  627. return 0;
  628. }
  629. void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
  630. {
  631. Chardev *chr;
  632. chr = qemu_chr_new("qtest", qtest_chrdev);
  633. if (chr == NULL) {
  634. error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
  635. qtest_chrdev);
  636. return;
  637. }
  638. if (qtest_log) {
  639. if (strcmp(qtest_log, "none") != 0) {
  640. qtest_log_fp = fopen(qtest_log, "w+");
  641. }
  642. } else {
  643. qtest_log_fp = stderr;
  644. }
  645. qemu_chr_fe_init(&qtest_chr, chr, errp);
  646. qemu_chr_fe_set_handlers(&qtest_chr, qtest_can_read, qtest_read,
  647. qtest_event, NULL, &qtest_chr, NULL, true);
  648. qemu_chr_fe_set_echo(&qtest_chr, true);
  649. inbuf = g_string_new("");
  650. }
  651. bool qtest_driver(void)
  652. {
  653. return qtest_chr.chr != NULL;
  654. }
  655. static void qtest_accel_class_init(ObjectClass *oc, void *data)
  656. {
  657. AccelClass *ac = ACCEL_CLASS(oc);
  658. ac->name = "QTest";
  659. ac->available = qtest_available;
  660. ac->init_machine = qtest_init_accel;
  661. ac->allowed = &qtest_allowed;
  662. }
  663. #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
  664. static const TypeInfo qtest_accel_type = {
  665. .name = TYPE_QTEST_ACCEL,
  666. .parent = TYPE_ACCEL,
  667. .class_init = qtest_accel_class_init,
  668. };
  669. static void qtest_type_init(void)
  670. {
  671. type_register_static(&qtest_accel_type);
  672. }
  673. type_init(qtest_type_init);