2
0

qtest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 "sysemu/qtest.h"
  14. #include "hw/qdev.h"
  15. #include "sysemu/char.h"
  16. #include "exec/ioport.h"
  17. #include "exec/memory.h"
  18. #include "hw/irq.h"
  19. #include "sysemu/accel.h"
  20. #include "sysemu/sysemu.h"
  21. #include "sysemu/cpus.h"
  22. #include "qemu/config-file.h"
  23. #include "qemu/option.h"
  24. #include "qemu/error-report.h"
  25. #define MAX_IRQ 256
  26. bool qtest_allowed;
  27. static DeviceState *irq_intercept_dev;
  28. static FILE *qtest_log_fp;
  29. static CharDriverState *qtest_chr;
  30. static GString *inbuf;
  31. static int irq_levels[MAX_IRQ];
  32. static qemu_timeval start_time;
  33. static bool qtest_opened;
  34. #define FMT_timeval "%ld.%06ld"
  35. /**
  36. * QTest Protocol
  37. *
  38. * Line based protocol, request/response based. Server can send async messages
  39. * so clients should always handle many async messages before the response
  40. * comes in.
  41. *
  42. * Valid requests
  43. *
  44. * Clock management:
  45. *
  46. * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
  47. * let you adjust the value of the clock (monotonically). All the commands
  48. * return the current value of the clock in nanoseconds.
  49. *
  50. * > clock_step
  51. * < OK VALUE
  52. *
  53. * Advance the clock to the next deadline. Useful when waiting for
  54. * asynchronous events.
  55. *
  56. * > clock_step NS
  57. * < OK VALUE
  58. *
  59. * Advance the clock by NS nanoseconds.
  60. *
  61. * > clock_set NS
  62. * < OK VALUE
  63. *
  64. * Advance the clock to NS nanoseconds (do nothing if it's already past).
  65. *
  66. * PIO and memory access:
  67. *
  68. * > outb ADDR VALUE
  69. * < OK
  70. *
  71. * > outw ADDR VALUE
  72. * < OK
  73. *
  74. * > outl ADDR VALUE
  75. * < OK
  76. *
  77. * > inb ADDR
  78. * < OK VALUE
  79. *
  80. * > inw ADDR
  81. * < OK VALUE
  82. *
  83. * > inl ADDR
  84. * < OK VALUE
  85. *
  86. * > writeb ADDR VALUE
  87. * < OK
  88. *
  89. * > writew ADDR VALUE
  90. * < OK
  91. *
  92. * > writel ADDR VALUE
  93. * < OK
  94. *
  95. * > writeq ADDR VALUE
  96. * < OK
  97. *
  98. * > readb ADDR
  99. * < OK VALUE
  100. *
  101. * > readw ADDR
  102. * < OK VALUE
  103. *
  104. * > readl ADDR
  105. * < OK VALUE
  106. *
  107. * > readq ADDR
  108. * < OK VALUE
  109. *
  110. * > read ADDR SIZE
  111. * < OK DATA
  112. *
  113. * > write ADDR SIZE DATA
  114. * < OK
  115. *
  116. * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
  117. *
  118. * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
  119. * than the expected size, the value will be zero filled at the end of the data
  120. * sequence.
  121. *
  122. * IRQ management:
  123. *
  124. * > irq_intercept_in QOM-PATH
  125. * < OK
  126. *
  127. * > irq_intercept_out QOM-PATH
  128. * < OK
  129. *
  130. * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
  131. * QOM-PATH. When the pin is triggered, one of the following async messages
  132. * will be printed to the qtest stream:
  133. *
  134. * IRQ raise NUM
  135. * IRQ lower NUM
  136. *
  137. * where NUM is an IRQ number. For the PC, interrupts can be intercepted
  138. * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
  139. * NUM=0 even though it is remapped to GSI 2).
  140. */
  141. static int hex2nib(char ch)
  142. {
  143. if (ch >= '0' && ch <= '9') {
  144. return ch - '0';
  145. } else if (ch >= 'a' && ch <= 'f') {
  146. return 10 + (ch - 'a');
  147. } else if (ch >= 'A' && ch <= 'F') {
  148. return 10 + (ch - 'A');
  149. } else {
  150. return -1;
  151. }
  152. }
  153. static void qtest_get_time(qemu_timeval *tv)
  154. {
  155. qemu_gettimeofday(tv);
  156. tv->tv_sec -= start_time.tv_sec;
  157. tv->tv_usec -= start_time.tv_usec;
  158. if (tv->tv_usec < 0) {
  159. tv->tv_usec += 1000000;
  160. tv->tv_sec -= 1;
  161. }
  162. }
  163. static void qtest_send_prefix(CharDriverState *chr)
  164. {
  165. qemu_timeval tv;
  166. if (!qtest_log_fp || !qtest_opened) {
  167. return;
  168. }
  169. qtest_get_time(&tv);
  170. fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
  171. (long) tv.tv_sec, (long) tv.tv_usec);
  172. }
  173. static void GCC_FMT_ATTR(2, 3) qtest_send(CharDriverState *chr,
  174. const char *fmt, ...)
  175. {
  176. va_list ap;
  177. char buffer[1024];
  178. size_t len;
  179. va_start(ap, fmt);
  180. len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
  181. va_end(ap);
  182. qemu_chr_fe_write_all(chr, (uint8_t *)buffer, len);
  183. if (qtest_log_fp && qtest_opened) {
  184. fprintf(qtest_log_fp, "%s", buffer);
  185. }
  186. }
  187. static void qtest_irq_handler(void *opaque, int n, int level)
  188. {
  189. qemu_irq old_irq = *(qemu_irq *)opaque;
  190. qemu_set_irq(old_irq, level);
  191. if (irq_levels[n] != level) {
  192. CharDriverState *chr = qtest_chr;
  193. irq_levels[n] = level;
  194. qtest_send_prefix(chr);
  195. qtest_send(chr, "IRQ %s %d\n",
  196. level ? "raise" : "lower", n);
  197. }
  198. }
  199. static void qtest_process_command(CharDriverState *chr, gchar **words)
  200. {
  201. const gchar *command;
  202. g_assert(words);
  203. command = words[0];
  204. if (qtest_log_fp) {
  205. qemu_timeval tv;
  206. int i;
  207. qtest_get_time(&tv);
  208. fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
  209. (long) tv.tv_sec, (long) tv.tv_usec);
  210. for (i = 0; words[i]; i++) {
  211. fprintf(qtest_log_fp, " %s", words[i]);
  212. }
  213. fprintf(qtest_log_fp, "\n");
  214. }
  215. g_assert(command);
  216. if (strcmp(words[0], "irq_intercept_out") == 0
  217. || strcmp(words[0], "irq_intercept_in") == 0) {
  218. DeviceState *dev;
  219. NamedGPIOList *ngl;
  220. g_assert(words[1]);
  221. dev = DEVICE(object_resolve_path(words[1], NULL));
  222. if (!dev) {
  223. qtest_send_prefix(chr);
  224. qtest_send(chr, "FAIL Unknown device\n");
  225. return;
  226. }
  227. if (irq_intercept_dev) {
  228. qtest_send_prefix(chr);
  229. if (irq_intercept_dev != dev) {
  230. qtest_send(chr, "FAIL IRQ intercept already enabled\n");
  231. } else {
  232. qtest_send(chr, "OK\n");
  233. }
  234. return;
  235. }
  236. QLIST_FOREACH(ngl, &dev->gpios, node) {
  237. /* We don't support intercept of named GPIOs yet */
  238. if (ngl->name) {
  239. continue;
  240. }
  241. if (words[0][14] == 'o') {
  242. int i;
  243. for (i = 0; i < ngl->num_out; ++i) {
  244. qemu_irq *disconnected = g_new0(qemu_irq, 1);
  245. qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
  246. disconnected, i);
  247. *disconnected = qdev_intercept_gpio_out(dev, icpt,
  248. ngl->name, i);
  249. }
  250. } else {
  251. qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
  252. ngl->num_in);
  253. }
  254. }
  255. irq_intercept_dev = dev;
  256. qtest_send_prefix(chr);
  257. qtest_send(chr, "OK\n");
  258. } else if (strcmp(words[0], "outb") == 0 ||
  259. strcmp(words[0], "outw") == 0 ||
  260. strcmp(words[0], "outl") == 0) {
  261. uint16_t addr;
  262. uint32_t value;
  263. g_assert(words[1] && words[2]);
  264. addr = strtoul(words[1], NULL, 0);
  265. value = strtoul(words[2], NULL, 0);
  266. if (words[0][3] == 'b') {
  267. cpu_outb(addr, value);
  268. } else if (words[0][3] == 'w') {
  269. cpu_outw(addr, value);
  270. } else if (words[0][3] == 'l') {
  271. cpu_outl(addr, value);
  272. }
  273. qtest_send_prefix(chr);
  274. qtest_send(chr, "OK\n");
  275. } else if (strcmp(words[0], "inb") == 0 ||
  276. strcmp(words[0], "inw") == 0 ||
  277. strcmp(words[0], "inl") == 0) {
  278. uint16_t addr;
  279. uint32_t value = -1U;
  280. g_assert(words[1]);
  281. addr = strtoul(words[1], NULL, 0);
  282. if (words[0][2] == 'b') {
  283. value = cpu_inb(addr);
  284. } else if (words[0][2] == 'w') {
  285. value = cpu_inw(addr);
  286. } else if (words[0][2] == 'l') {
  287. value = cpu_inl(addr);
  288. }
  289. qtest_send_prefix(chr);
  290. qtest_send(chr, "OK 0x%04x\n", value);
  291. } else if (strcmp(words[0], "writeb") == 0 ||
  292. strcmp(words[0], "writew") == 0 ||
  293. strcmp(words[0], "writel") == 0 ||
  294. strcmp(words[0], "writeq") == 0) {
  295. uint64_t addr;
  296. uint64_t value;
  297. g_assert(words[1] && words[2]);
  298. addr = strtoull(words[1], NULL, 0);
  299. value = strtoull(words[2], NULL, 0);
  300. if (words[0][5] == 'b') {
  301. uint8_t data = value;
  302. cpu_physical_memory_write(addr, &data, 1);
  303. } else if (words[0][5] == 'w') {
  304. uint16_t data = value;
  305. tswap16s(&data);
  306. cpu_physical_memory_write(addr, &data, 2);
  307. } else if (words[0][5] == 'l') {
  308. uint32_t data = value;
  309. tswap32s(&data);
  310. cpu_physical_memory_write(addr, &data, 4);
  311. } else if (words[0][5] == 'q') {
  312. uint64_t data = value;
  313. tswap64s(&data);
  314. cpu_physical_memory_write(addr, &data, 8);
  315. }
  316. qtest_send_prefix(chr);
  317. qtest_send(chr, "OK\n");
  318. } else if (strcmp(words[0], "readb") == 0 ||
  319. strcmp(words[0], "readw") == 0 ||
  320. strcmp(words[0], "readl") == 0 ||
  321. strcmp(words[0], "readq") == 0) {
  322. uint64_t addr;
  323. uint64_t value = UINT64_C(-1);
  324. g_assert(words[1]);
  325. addr = strtoull(words[1], NULL, 0);
  326. if (words[0][4] == 'b') {
  327. uint8_t data;
  328. cpu_physical_memory_read(addr, &data, 1);
  329. value = data;
  330. } else if (words[0][4] == 'w') {
  331. uint16_t data;
  332. cpu_physical_memory_read(addr, &data, 2);
  333. value = tswap16(data);
  334. } else if (words[0][4] == 'l') {
  335. uint32_t data;
  336. cpu_physical_memory_read(addr, &data, 4);
  337. value = tswap32(data);
  338. } else if (words[0][4] == 'q') {
  339. cpu_physical_memory_read(addr, &value, 8);
  340. tswap64s(&value);
  341. }
  342. qtest_send_prefix(chr);
  343. qtest_send(chr, "OK 0x%016" PRIx64 "\n", value);
  344. } else if (strcmp(words[0], "read") == 0) {
  345. uint64_t addr, len, i;
  346. uint8_t *data;
  347. g_assert(words[1] && words[2]);
  348. addr = strtoull(words[1], NULL, 0);
  349. len = strtoull(words[2], NULL, 0);
  350. data = g_malloc(len);
  351. cpu_physical_memory_read(addr, data, len);
  352. qtest_send_prefix(chr);
  353. qtest_send(chr, "OK 0x");
  354. for (i = 0; i < len; i++) {
  355. qtest_send(chr, "%02x", data[i]);
  356. }
  357. qtest_send(chr, "\n");
  358. g_free(data);
  359. } else if (strcmp(words[0], "write") == 0) {
  360. uint64_t addr, len, i;
  361. uint8_t *data;
  362. size_t data_len;
  363. g_assert(words[1] && words[2] && words[3]);
  364. addr = strtoull(words[1], NULL, 0);
  365. len = strtoull(words[2], NULL, 0);
  366. data_len = strlen(words[3]);
  367. if (data_len < 3) {
  368. qtest_send(chr, "ERR invalid argument size\n");
  369. return;
  370. }
  371. data = g_malloc(len);
  372. for (i = 0; i < len; i++) {
  373. if ((i * 2 + 4) <= data_len) {
  374. data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
  375. data[i] |= hex2nib(words[3][i * 2 + 3]);
  376. } else {
  377. data[i] = 0;
  378. }
  379. }
  380. cpu_physical_memory_write(addr, data, len);
  381. g_free(data);
  382. qtest_send_prefix(chr);
  383. qtest_send(chr, "OK\n");
  384. } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
  385. int64_t ns;
  386. if (words[1]) {
  387. ns = strtoll(words[1], NULL, 0);
  388. } else {
  389. ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
  390. }
  391. qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
  392. qtest_send_prefix(chr);
  393. qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  394. } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
  395. int64_t ns;
  396. g_assert(words[1]);
  397. ns = strtoll(words[1], NULL, 0);
  398. qtest_clock_warp(ns);
  399. qtest_send_prefix(chr);
  400. qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  401. } else {
  402. qtest_send_prefix(chr);
  403. qtest_send(chr, "FAIL Unknown command `%s'\n", words[0]);
  404. }
  405. }
  406. static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
  407. {
  408. char *end;
  409. while ((end = strchr(inbuf->str, '\n')) != NULL) {
  410. size_t offset;
  411. GString *cmd;
  412. gchar **words;
  413. offset = end - inbuf->str;
  414. cmd = g_string_new_len(inbuf->str, offset);
  415. g_string_erase(inbuf, 0, offset + 1);
  416. words = g_strsplit(cmd->str, " ", 0);
  417. qtest_process_command(chr, words);
  418. g_strfreev(words);
  419. g_string_free(cmd, TRUE);
  420. }
  421. }
  422. static void qtest_read(void *opaque, const uint8_t *buf, int size)
  423. {
  424. CharDriverState *chr = opaque;
  425. g_string_append_len(inbuf, (const gchar *)buf, size);
  426. qtest_process_inbuf(chr, inbuf);
  427. }
  428. static int qtest_can_read(void *opaque)
  429. {
  430. return 1024;
  431. }
  432. static void qtest_event(void *opaque, int event)
  433. {
  434. int i;
  435. switch (event) {
  436. case CHR_EVENT_OPENED:
  437. /*
  438. * We used to call qemu_system_reset() here, hoping we could
  439. * use the same process for multiple tests that way. Never
  440. * used. Injects an extra reset even when it's not used, and
  441. * that can mess up tests, e.g. -boot once.
  442. */
  443. for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
  444. irq_levels[i] = 0;
  445. }
  446. qemu_gettimeofday(&start_time);
  447. qtest_opened = true;
  448. if (qtest_log_fp) {
  449. fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
  450. (long) start_time.tv_sec, (long) start_time.tv_usec);
  451. }
  452. break;
  453. case CHR_EVENT_CLOSED:
  454. qtest_opened = false;
  455. if (qtest_log_fp) {
  456. qemu_timeval tv;
  457. qtest_get_time(&tv);
  458. fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
  459. (long) tv.tv_sec, (long) tv.tv_usec);
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. }
  466. static void configure_qtest_icount(const char *options)
  467. {
  468. QemuOpts *opts = qemu_opts_parse(qemu_find_opts("icount"), options, 1);
  469. configure_icount(opts, &error_abort);
  470. qemu_opts_del(opts);
  471. }
  472. static int qtest_init_accel(MachineState *ms)
  473. {
  474. configure_qtest_icount("0");
  475. return 0;
  476. }
  477. void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
  478. {
  479. CharDriverState *chr;
  480. chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
  481. if (chr == NULL) {
  482. error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
  483. qtest_chrdev);
  484. return;
  485. }
  486. if (qtest_log) {
  487. if (strcmp(qtest_log, "none") != 0) {
  488. qtest_log_fp = fopen(qtest_log, "w+");
  489. }
  490. } else {
  491. qtest_log_fp = stderr;
  492. }
  493. qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
  494. qemu_chr_fe_set_echo(chr, true);
  495. inbuf = g_string_new("");
  496. qtest_chr = chr;
  497. }
  498. bool qtest_driver(void)
  499. {
  500. return qtest_chr;
  501. }
  502. static void qtest_accel_class_init(ObjectClass *oc, void *data)
  503. {
  504. AccelClass *ac = ACCEL_CLASS(oc);
  505. ac->name = "QTest";
  506. ac->available = qtest_available;
  507. ac->init_machine = qtest_init_accel;
  508. ac->allowed = &qtest_allowed;
  509. }
  510. #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
  511. static const TypeInfo qtest_accel_type = {
  512. .name = TYPE_QTEST_ACCEL,
  513. .parent = TYPE_ACCEL,
  514. .class_init = qtest_accel_class_init,
  515. };
  516. static void qtest_type_init(void)
  517. {
  518. type_register_static(&qtest_accel_type);
  519. }
  520. type_init(qtest_type_init);