2
0

qtest.c 14 KB

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