2
0

qtest.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 "system/qtest.h"
  16. #include "system/runstate.h"
  17. #include "chardev/char-fe.h"
  18. #include "exec/ioport.h"
  19. #include "exec/memory.h"
  20. #include "exec/tswap.h"
  21. #include "hw/qdev-core.h"
  22. #include "hw/irq.h"
  23. #include "hw/core/cpu.h"
  24. #include "qemu/accel.h"
  25. #include "system/cpu-timers.h"
  26. #include "qemu/config-file.h"
  27. #include "qemu/option.h"
  28. #include "qemu/error-report.h"
  29. #include "qemu/module.h"
  30. #include "qemu/cutils.h"
  31. #include "qom/object_interfaces.h"
  32. #define MAX_IRQ 256
  33. #define TYPE_QTEST "qtest"
  34. OBJECT_DECLARE_SIMPLE_TYPE(QTest, QTEST)
  35. struct QTest {
  36. Object parent;
  37. bool has_machine_link;
  38. char *chr_name;
  39. Chardev *chr;
  40. CharBackend qtest_chr;
  41. char *log;
  42. };
  43. bool qtest_allowed;
  44. static DeviceState *irq_intercept_dev;
  45. static FILE *qtest_log_fp;
  46. static QTest *qtest;
  47. static GString *inbuf;
  48. static int irq_levels[MAX_IRQ];
  49. static GTimer *timer;
  50. static bool qtest_opened;
  51. static void (*qtest_server_send)(void*, const char*);
  52. static void *qtest_server_send_opaque;
  53. #define FMT_timeval "%.06f"
  54. /**
  55. * DOC: QTest Protocol
  56. *
  57. * Line based protocol, request/response based. Server can send async messages
  58. * so clients should always handle many async messages before the response
  59. * comes in.
  60. *
  61. * Valid requests
  62. * ^^^^^^^^^^^^^^
  63. *
  64. * Clock management:
  65. * """""""""""""""""
  66. *
  67. * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
  68. * let you adjust the value of the clock (monotonically). All the commands
  69. * return the current value of the clock in nanoseconds.
  70. *
  71. * If the commands FAIL then time wasn't advanced which is likely
  72. * because the machine was in a paused state or no timer events exist
  73. * in the future. This will cause qtest to abort and the test will
  74. * need to check its assumptions.
  75. *
  76. * .. code-block:: none
  77. *
  78. * > clock_step
  79. * < OK VALUE
  80. *
  81. * Advance the clock to the next deadline. Useful when waiting for
  82. * asynchronous events.
  83. *
  84. * .. code-block:: none
  85. *
  86. * > clock_step NS
  87. * < OK VALUE
  88. *
  89. * Advance the clock by NS nanoseconds.
  90. *
  91. * .. code-block:: none
  92. *
  93. * > clock_set NS
  94. * < OK VALUE
  95. *
  96. * Advance the clock to NS nanoseconds (do nothing if it's already past).
  97. *
  98. * PIO and memory access:
  99. * """"""""""""""""""""""
  100. *
  101. * .. code-block:: none
  102. *
  103. * > outb ADDR VALUE
  104. * < OK
  105. *
  106. * .. code-block:: none
  107. *
  108. * > outw ADDR VALUE
  109. * < OK
  110. *
  111. * .. code-block:: none
  112. *
  113. * > outl ADDR VALUE
  114. * < OK
  115. *
  116. * .. code-block:: none
  117. *
  118. * > inb ADDR
  119. * < OK VALUE
  120. *
  121. * .. code-block:: none
  122. *
  123. * > inw ADDR
  124. * < OK VALUE
  125. *
  126. * .. code-block:: none
  127. *
  128. * > inl ADDR
  129. * < OK VALUE
  130. *
  131. * .. code-block:: none
  132. *
  133. * > writeb ADDR VALUE
  134. * < OK
  135. *
  136. * .. code-block:: none
  137. *
  138. * > writew ADDR VALUE
  139. * < OK
  140. *
  141. * .. code-block:: none
  142. *
  143. * > writel ADDR VALUE
  144. * < OK
  145. *
  146. * .. code-block:: none
  147. *
  148. * > writeq ADDR VALUE
  149. * < OK
  150. *
  151. * .. code-block:: none
  152. *
  153. * > readb ADDR
  154. * < OK VALUE
  155. *
  156. * .. code-block:: none
  157. *
  158. * > readw ADDR
  159. * < OK VALUE
  160. *
  161. * .. code-block:: none
  162. *
  163. * > readl ADDR
  164. * < OK VALUE
  165. *
  166. * .. code-block:: none
  167. *
  168. * > readq ADDR
  169. * < OK VALUE
  170. *
  171. * .. code-block:: none
  172. *
  173. * > read ADDR SIZE
  174. * < OK DATA
  175. *
  176. * .. code-block:: none
  177. *
  178. * > write ADDR SIZE DATA
  179. * < OK
  180. *
  181. * .. code-block:: none
  182. *
  183. * > b64read ADDR SIZE
  184. * < OK B64_DATA
  185. *
  186. * .. code-block:: none
  187. *
  188. * > b64write ADDR SIZE B64_DATA
  189. * < OK
  190. *
  191. * .. code-block:: none
  192. *
  193. * > memset ADDR SIZE VALUE
  194. * < OK
  195. *
  196. * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
  197. * For 'memset' a zero size is permitted and does nothing.
  198. *
  199. * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
  200. * than the expected size, the value will be zero filled at the end of the data
  201. * sequence.
  202. *
  203. * B64_DATA is an arbitrarily long base64 encoded string.
  204. * If the sizes do not match, the data will be truncated.
  205. *
  206. * IRQ management:
  207. * """""""""""""""
  208. *
  209. * .. code-block:: none
  210. *
  211. * > irq_intercept_in QOM-PATH
  212. * < OK
  213. *
  214. * .. code-block:: none
  215. *
  216. * > irq_intercept_out QOM-PATH
  217. * < OK
  218. *
  219. * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
  220. * QOM-PATH. When the pin is triggered, one of the following async messages
  221. * will be printed to the qtest stream::
  222. *
  223. * IRQ raise NUM
  224. * IRQ lower NUM
  225. *
  226. * where NUM is an IRQ number. For the PC, interrupts can be intercepted
  227. * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
  228. * NUM=0 even though it is remapped to GSI 2).
  229. *
  230. * Setting interrupt level:
  231. * """"""""""""""""""""""""
  232. *
  233. * .. code-block:: none
  234. *
  235. * > set_irq_in QOM-PATH NAME NUM LEVEL
  236. * < OK
  237. *
  238. * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
  239. * LEVEL is an signed integer IRQ level.
  240. *
  241. * Forcibly set the given interrupt pin to the given level.
  242. *
  243. */
  244. static int hex2nib(char ch)
  245. {
  246. if (ch >= '0' && ch <= '9') {
  247. return ch - '0';
  248. } else if (ch >= 'a' && ch <= 'f') {
  249. return 10 + (ch - 'a');
  250. } else if (ch >= 'A' && ch <= 'F') {
  251. return 10 + (ch - 'A');
  252. } else {
  253. return -1;
  254. }
  255. }
  256. static void qtest_log_timestamp(void)
  257. {
  258. if (!qtest_log_fp || !qtest_opened) {
  259. return;
  260. }
  261. fprintf(qtest_log_fp, "[S +" FMT_timeval "] ", g_timer_elapsed(timer, NULL));
  262. }
  263. static void G_GNUC_PRINTF(1, 2) qtest_log_send(const char *fmt, ...)
  264. {
  265. va_list ap;
  266. if (!qtest_log_fp || !qtest_opened) {
  267. return;
  268. }
  269. qtest_log_timestamp();
  270. va_start(ap, fmt);
  271. vfprintf(qtest_log_fp, fmt, ap);
  272. va_end(ap);
  273. }
  274. static void qtest_server_char_be_send(void *opaque, const char *str)
  275. {
  276. size_t len = strlen(str);
  277. CharBackend* chr = (CharBackend *)opaque;
  278. qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
  279. if (qtest_log_fp && qtest_opened) {
  280. fprintf(qtest_log_fp, "%s", str);
  281. }
  282. }
  283. static void qtest_send(CharBackend *chr, const char *str)
  284. {
  285. qtest_log_timestamp();
  286. qtest_server_send(qtest_server_send_opaque, str);
  287. }
  288. void qtest_sendf(CharBackend *chr, const char *fmt, ...)
  289. {
  290. va_list ap;
  291. gchar *buffer;
  292. va_start(ap, fmt);
  293. buffer = g_strdup_vprintf(fmt, ap);
  294. qtest_send(chr, buffer);
  295. g_free(buffer);
  296. va_end(ap);
  297. }
  298. static void qtest_irq_handler(void *opaque, int n, int level)
  299. {
  300. qemu_irq old_irq = *(qemu_irq *)opaque;
  301. qemu_set_irq(old_irq, level);
  302. if (irq_levels[n] != level) {
  303. CharBackend *chr = &qtest->qtest_chr;
  304. irq_levels[n] = level;
  305. qtest_sendf(chr, "IRQ %s %d\n",
  306. level ? "raise" : "lower", n);
  307. }
  308. }
  309. static bool (*process_command_cb)(CharBackend *chr, gchar **words);
  310. void qtest_set_command_cb(bool (*pc_cb)(CharBackend *chr, gchar **words))
  311. {
  312. assert(!process_command_cb); /* Switch to a list if we need more than one */
  313. process_command_cb = pc_cb;
  314. }
  315. static void qtest_install_gpio_out_intercept(DeviceState *dev, const char *name, int n)
  316. {
  317. qemu_irq *disconnected = g_new0(qemu_irq, 1);
  318. qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
  319. disconnected, n);
  320. *disconnected = qdev_intercept_gpio_out(dev, icpt, name, n);
  321. }
  322. static void qtest_process_command(CharBackend *chr, gchar **words)
  323. {
  324. const gchar *command;
  325. g_assert(words);
  326. command = words[0];
  327. if (qtest_log_fp) {
  328. int i;
  329. fprintf(qtest_log_fp, "[R +" FMT_timeval "]", g_timer_elapsed(timer, NULL));
  330. for (i = 0; words[i]; i++) {
  331. fprintf(qtest_log_fp, " %s", words[i]);
  332. }
  333. fprintf(qtest_log_fp, "\n");
  334. }
  335. g_assert(command);
  336. if (strcmp(words[0], "irq_intercept_out") == 0
  337. || strcmp(words[0], "irq_intercept_in") == 0) {
  338. DeviceState *dev;
  339. NamedGPIOList *ngl;
  340. bool is_named;
  341. bool is_outbound;
  342. bool interception_succeeded = false;
  343. g_assert(words[1]);
  344. is_named = words[2] != NULL;
  345. is_outbound = words[0][14] == 'o';
  346. dev = DEVICE(object_resolve_path(words[1], NULL));
  347. if (!dev) {
  348. qtest_send(chr, "FAIL Unknown device\n");
  349. return;
  350. }
  351. if (is_named && !is_outbound) {
  352. qtest_send(chr, "FAIL Interception of named in-GPIOs not yet supported\n");
  353. return;
  354. }
  355. if (irq_intercept_dev) {
  356. if (irq_intercept_dev != dev) {
  357. qtest_send(chr, "FAIL IRQ intercept already enabled\n");
  358. } else {
  359. qtest_send(chr, "OK\n");
  360. }
  361. return;
  362. }
  363. QLIST_FOREACH(ngl, &dev->gpios, node) {
  364. /* We don't support inbound interception of named GPIOs yet */
  365. if (is_outbound) {
  366. /* NULL is valid and matchable, for "unnamed GPIO" */
  367. if (g_strcmp0(ngl->name, words[2]) == 0) {
  368. int i;
  369. for (i = 0; i < ngl->num_out; ++i) {
  370. qtest_install_gpio_out_intercept(dev, ngl->name, i);
  371. }
  372. interception_succeeded = true;
  373. }
  374. } else {
  375. qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
  376. ngl->num_in);
  377. interception_succeeded = true;
  378. }
  379. }
  380. if (interception_succeeded) {
  381. irq_intercept_dev = dev;
  382. qtest_send(chr, "OK\n");
  383. } else {
  384. qtest_send(chr, "FAIL No intercepts installed\n");
  385. }
  386. } else if (strcmp(words[0], "set_irq_in") == 0) {
  387. DeviceState *dev;
  388. qemu_irq irq;
  389. char *name;
  390. int ret;
  391. int num;
  392. int level;
  393. g_assert(words[1] && words[2] && words[3] && words[4]);
  394. dev = DEVICE(object_resolve_path(words[1], NULL));
  395. if (!dev) {
  396. qtest_send(chr, "FAIL Unknown device\n");
  397. return;
  398. }
  399. if (strcmp(words[2], "unnamed-gpio-in") == 0) {
  400. name = NULL;
  401. } else {
  402. name = words[2];
  403. }
  404. ret = qemu_strtoi(words[3], NULL, 0, &num);
  405. g_assert(!ret);
  406. ret = qemu_strtoi(words[4], NULL, 0, &level);
  407. g_assert(!ret);
  408. irq = qdev_get_gpio_in_named(dev, name, num);
  409. qemu_set_irq(irq, level);
  410. qtest_send(chr, "OK\n");
  411. } else if (strcmp(words[0], "outb") == 0 ||
  412. strcmp(words[0], "outw") == 0 ||
  413. strcmp(words[0], "outl") == 0) {
  414. unsigned long addr;
  415. unsigned long value;
  416. int ret;
  417. g_assert(words[1] && words[2]);
  418. ret = qemu_strtoul(words[1], NULL, 0, &addr);
  419. g_assert(ret == 0);
  420. ret = qemu_strtoul(words[2], NULL, 0, &value);
  421. g_assert(ret == 0);
  422. g_assert(addr <= 0xffff);
  423. if (words[0][3] == 'b') {
  424. cpu_outb(addr, value);
  425. } else if (words[0][3] == 'w') {
  426. cpu_outw(addr, value);
  427. } else if (words[0][3] == 'l') {
  428. cpu_outl(addr, value);
  429. }
  430. qtest_send(chr, "OK\n");
  431. } else if (strcmp(words[0], "inb") == 0 ||
  432. strcmp(words[0], "inw") == 0 ||
  433. strcmp(words[0], "inl") == 0) {
  434. unsigned long addr;
  435. uint32_t value = -1U;
  436. int ret;
  437. g_assert(words[1]);
  438. ret = qemu_strtoul(words[1], NULL, 0, &addr);
  439. g_assert(ret == 0);
  440. g_assert(addr <= 0xffff);
  441. if (words[0][2] == 'b') {
  442. value = cpu_inb(addr);
  443. } else if (words[0][2] == 'w') {
  444. value = cpu_inw(addr);
  445. } else if (words[0][2] == 'l') {
  446. value = cpu_inl(addr);
  447. }
  448. qtest_sendf(chr, "OK 0x%04x\n", value);
  449. } else if (strcmp(words[0], "writeb") == 0 ||
  450. strcmp(words[0], "writew") == 0 ||
  451. strcmp(words[0], "writel") == 0 ||
  452. strcmp(words[0], "writeq") == 0) {
  453. uint64_t addr;
  454. uint64_t value;
  455. int ret;
  456. g_assert(words[1] && words[2]);
  457. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  458. g_assert(ret == 0);
  459. ret = qemu_strtou64(words[2], NULL, 0, &value);
  460. g_assert(ret == 0);
  461. if (words[0][5] == 'b') {
  462. uint8_t data = value;
  463. address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  464. &data, 1);
  465. } else if (words[0][5] == 'w') {
  466. uint16_t data = value;
  467. tswap16s(&data);
  468. address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  469. &data, 2);
  470. } else if (words[0][5] == 'l') {
  471. uint32_t data = value;
  472. tswap32s(&data);
  473. address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  474. &data, 4);
  475. } else if (words[0][5] == 'q') {
  476. uint64_t data = value;
  477. tswap64s(&data);
  478. address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  479. &data, 8);
  480. }
  481. qtest_send(chr, "OK\n");
  482. } else if (strcmp(words[0], "readb") == 0 ||
  483. strcmp(words[0], "readw") == 0 ||
  484. strcmp(words[0], "readl") == 0 ||
  485. strcmp(words[0], "readq") == 0) {
  486. uint64_t addr;
  487. uint64_t value = UINT64_C(-1);
  488. int ret;
  489. g_assert(words[1]);
  490. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  491. g_assert(ret == 0);
  492. if (words[0][4] == 'b') {
  493. uint8_t data;
  494. address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  495. &data, 1);
  496. value = data;
  497. } else if (words[0][4] == 'w') {
  498. uint16_t data;
  499. address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  500. &data, 2);
  501. value = tswap16(data);
  502. } else if (words[0][4] == 'l') {
  503. uint32_t data;
  504. address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  505. &data, 4);
  506. value = tswap32(data);
  507. } else if (words[0][4] == 'q') {
  508. address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  509. &value, 8);
  510. tswap64s(&value);
  511. }
  512. qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
  513. } else if (strcmp(words[0], "read") == 0) {
  514. g_autoptr(GString) enc = NULL;
  515. uint64_t addr, len;
  516. uint8_t *data;
  517. int ret;
  518. g_assert(words[1] && words[2]);
  519. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  520. g_assert(ret == 0);
  521. ret = qemu_strtou64(words[2], NULL, 0, &len);
  522. g_assert(ret == 0);
  523. /* We'd send garbage to libqtest if len is 0 */
  524. g_assert(len);
  525. data = g_malloc(len);
  526. address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
  527. len);
  528. enc = qemu_hexdump_line(NULL, data, len, 0, 0);
  529. qtest_sendf(chr, "OK 0x%s\n", enc->str);
  530. g_free(data);
  531. } else if (strcmp(words[0], "b64read") == 0) {
  532. uint64_t addr, len;
  533. uint8_t *data;
  534. gchar *b64_data;
  535. int ret;
  536. g_assert(words[1] && words[2]);
  537. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  538. g_assert(ret == 0);
  539. ret = qemu_strtou64(words[2], NULL, 0, &len);
  540. g_assert(ret == 0);
  541. data = g_malloc(len);
  542. address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
  543. len);
  544. b64_data = g_base64_encode(data, len);
  545. qtest_sendf(chr, "OK %s\n", b64_data);
  546. g_free(data);
  547. g_free(b64_data);
  548. } else if (strcmp(words[0], "write") == 0) {
  549. uint64_t addr, len, i;
  550. uint8_t *data;
  551. size_t data_len;
  552. int ret;
  553. g_assert(words[1] && words[2] && words[3]);
  554. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  555. g_assert(ret == 0);
  556. ret = qemu_strtou64(words[2], NULL, 0, &len);
  557. g_assert(ret == 0);
  558. data_len = strlen(words[3]);
  559. if (data_len < 3) {
  560. qtest_send(chr, "ERR invalid argument size\n");
  561. return;
  562. }
  563. data = g_malloc(len);
  564. for (i = 0; i < len; i++) {
  565. if ((i * 2 + 4) <= data_len) {
  566. data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
  567. data[i] |= hex2nib(words[3][i * 2 + 3]);
  568. } else {
  569. data[i] = 0;
  570. }
  571. }
  572. address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
  573. len);
  574. g_free(data);
  575. qtest_send(chr, "OK\n");
  576. } else if (strcmp(words[0], "memset") == 0) {
  577. uint64_t addr, len;
  578. uint8_t *data;
  579. unsigned long pattern;
  580. int ret;
  581. g_assert(words[1] && words[2] && words[3]);
  582. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  583. g_assert(ret == 0);
  584. ret = qemu_strtou64(words[2], NULL, 0, &len);
  585. g_assert(ret == 0);
  586. ret = qemu_strtoul(words[3], NULL, 0, &pattern);
  587. g_assert(ret == 0);
  588. if (len) {
  589. data = g_malloc(len);
  590. memset(data, pattern, len);
  591. address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
  592. data, len);
  593. g_free(data);
  594. }
  595. qtest_send(chr, "OK\n");
  596. } else if (strcmp(words[0], "b64write") == 0) {
  597. uint64_t addr, len;
  598. uint8_t *data;
  599. size_t data_len;
  600. gsize out_len;
  601. int ret;
  602. g_assert(words[1] && words[2] && words[3]);
  603. ret = qemu_strtou64(words[1], NULL, 0, &addr);
  604. g_assert(ret == 0);
  605. ret = qemu_strtou64(words[2], NULL, 0, &len);
  606. g_assert(ret == 0);
  607. data_len = strlen(words[3]);
  608. if (data_len < 3) {
  609. qtest_send(chr, "ERR invalid argument size\n");
  610. return;
  611. }
  612. data = g_base64_decode_inplace(words[3], &out_len);
  613. if (out_len != len) {
  614. qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
  615. "found %zu)\n",
  616. len, out_len);
  617. out_len = MIN(out_len, len);
  618. }
  619. address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
  620. len);
  621. qtest_send(chr, "OK\n");
  622. } else if (strcmp(words[0], "endianness") == 0) {
  623. if (target_words_bigendian()) {
  624. qtest_sendf(chr, "OK big\n");
  625. } else {
  626. qtest_sendf(chr, "OK little\n");
  627. }
  628. } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
  629. int64_t old_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  630. int64_t ns, new_ns;
  631. if (words[1]) {
  632. int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
  633. g_assert(ret == 0);
  634. } else {
  635. ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
  636. QEMU_TIMER_ATTR_ALL);
  637. if (ns < 0) {
  638. qtest_send(chr, "FAIL "
  639. "cannot advance clock to the next deadline "
  640. "because there is no pending deadline\n");
  641. return;
  642. }
  643. }
  644. new_ns = qemu_clock_advance_virtual_time(old_ns + ns);
  645. if (new_ns > old_ns) {
  646. qtest_sendf(chr, "OK %"PRIi64"\n", new_ns);
  647. } else {
  648. qtest_sendf(chr, "FAIL could not advance time\n");
  649. }
  650. } else if (strcmp(words[0], "module_load") == 0) {
  651. Error *local_err = NULL;
  652. int rv;
  653. g_assert(words[1] && words[2]);
  654. rv = module_load(words[1], words[2], &local_err);
  655. if (rv > 0) {
  656. qtest_sendf(chr, "OK\n");
  657. } else {
  658. if (rv < 0) {
  659. error_report_err(local_err);
  660. }
  661. qtest_sendf(chr, "FAIL\n");
  662. }
  663. } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
  664. int64_t ns, new_ns;
  665. int ret;
  666. g_assert(words[1]);
  667. ret = qemu_strtoi64(words[1], NULL, 0, &ns);
  668. g_assert(ret == 0);
  669. new_ns = qemu_clock_advance_virtual_time(ns);
  670. qtest_sendf(chr, "%s %"PRIi64"\n",
  671. new_ns == ns ? "OK" : "FAIL", new_ns);
  672. } else if (process_command_cb && process_command_cb(chr, words)) {
  673. /* Command got consumed by the callback handler */
  674. } else {
  675. qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
  676. }
  677. }
  678. /*
  679. * Process as much of @inbuf as we can in newline terminated chunks.
  680. * Remove the processed commands from @inbuf as we go.
  681. */
  682. static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
  683. {
  684. char *end;
  685. while ((end = strchr(inbuf->str, '\n')) != NULL) {
  686. size_t len = end - inbuf->str;
  687. g_autofree char *cmd = g_strndup(inbuf->str, len);
  688. g_auto(GStrv) words = g_strsplit(cmd, " ", 0);
  689. g_string_erase(inbuf, 0, len + 1);
  690. qtest_process_command(chr, words);
  691. }
  692. }
  693. static void qtest_read(void *opaque, const uint8_t *buf, int size)
  694. {
  695. CharBackend *chr = opaque;
  696. g_string_append_len(inbuf, (const gchar *)buf, size);
  697. qtest_process_inbuf(chr, inbuf);
  698. }
  699. static int qtest_can_read(void *opaque)
  700. {
  701. return 1024;
  702. }
  703. static void qtest_event(void *opaque, QEMUChrEvent event)
  704. {
  705. int i;
  706. switch (event) {
  707. case CHR_EVENT_OPENED:
  708. /*
  709. * We used to call qemu_system_reset() here, hoping we could
  710. * use the same process for multiple tests that way. Never
  711. * used. Injects an extra reset even when it's not used, and
  712. * that can mess up tests, e.g. -boot once.
  713. */
  714. for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
  715. irq_levels[i] = 0;
  716. }
  717. g_clear_pointer(&timer, g_timer_destroy);
  718. timer = g_timer_new();
  719. qtest_opened = true;
  720. if (qtest_log_fp) {
  721. fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n", g_timer_elapsed(timer, NULL));
  722. }
  723. break;
  724. case CHR_EVENT_CLOSED:
  725. qtest_opened = false;
  726. if (qtest_log_fp) {
  727. fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n", g_timer_elapsed(timer, NULL));
  728. }
  729. g_clear_pointer(&timer, g_timer_destroy);
  730. break;
  731. default:
  732. break;
  733. }
  734. }
  735. void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
  736. {
  737. ERRP_GUARD();
  738. Chardev *chr;
  739. Object *qobj;
  740. chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
  741. if (chr == NULL) {
  742. error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
  743. qtest_chrdev);
  744. return;
  745. }
  746. qobj = object_new(TYPE_QTEST);
  747. object_property_set_str(qobj, "chardev", chr->label, &error_abort);
  748. if (qtest_log) {
  749. object_property_set_str(qobj, "log", qtest_log, &error_abort);
  750. }
  751. object_property_add_child(qdev_get_machine(), "qtest", qobj);
  752. user_creatable_complete(USER_CREATABLE(qobj), errp);
  753. if (*errp) {
  754. object_unparent(qobj);
  755. }
  756. object_unref(OBJECT(chr));
  757. object_unref(qobj);
  758. }
  759. static bool qtest_server_start(QTest *q, Error **errp)
  760. {
  761. Chardev *chr = q->chr;
  762. const char *qtest_log = q->log;
  763. if (qtest_log) {
  764. if (strcmp(qtest_log, "none") != 0) {
  765. qtest_log_fp = fopen(qtest_log, "w+");
  766. }
  767. } else {
  768. qtest_log_fp = stderr;
  769. }
  770. if (!qemu_chr_fe_init(&q->qtest_chr, chr, errp)) {
  771. return false;
  772. }
  773. qemu_chr_fe_set_handlers(&q->qtest_chr, qtest_can_read, qtest_read,
  774. qtest_event, NULL, &q->qtest_chr, NULL, true);
  775. qemu_chr_fe_set_echo(&q->qtest_chr, true);
  776. inbuf = g_string_new("");
  777. if (!qtest_server_send) {
  778. qtest_server_set_send_handler(qtest_server_char_be_send, &q->qtest_chr);
  779. }
  780. qtest = q;
  781. return true;
  782. }
  783. void qtest_server_set_send_handler(void (*send)(void*, const char*),
  784. void *opaque)
  785. {
  786. qtest_server_send = send;
  787. qtest_server_send_opaque = opaque;
  788. }
  789. bool qtest_driver(void)
  790. {
  791. return qtest && qtest->qtest_chr.chr != NULL;
  792. }
  793. void qtest_server_inproc_recv(void *dummy, const char *buf)
  794. {
  795. static GString *gstr;
  796. if (!gstr) {
  797. gstr = g_string_new(NULL);
  798. }
  799. g_string_append(gstr, buf);
  800. if (gstr->str[gstr->len - 1] == '\n') {
  801. qtest_process_inbuf(NULL, gstr);
  802. g_string_truncate(gstr, 0);
  803. }
  804. }
  805. static void qtest_complete(UserCreatable *uc, Error **errp)
  806. {
  807. QTest *q = QTEST(uc);
  808. if (qtest) {
  809. error_setg(errp, "Only one instance of qtest can be created");
  810. return;
  811. }
  812. if (!q->chr_name) {
  813. error_setg(errp, "No backend specified");
  814. return;
  815. }
  816. if (OBJECT(uc)->parent != qdev_get_machine()) {
  817. q->has_machine_link = true;
  818. object_property_add_const_link(qdev_get_machine(), "qtest", OBJECT(uc));
  819. } else {
  820. /* -qtest was used. */
  821. }
  822. qtest_server_start(q, errp);
  823. }
  824. static void qtest_unparent(Object *obj)
  825. {
  826. QTest *q = QTEST(obj);
  827. if (qtest == q) {
  828. qemu_chr_fe_disconnect(&q->qtest_chr);
  829. assert(!qtest_opened);
  830. qemu_chr_fe_deinit(&q->qtest_chr, false);
  831. if (qtest_log_fp) {
  832. fclose(qtest_log_fp);
  833. qtest_log_fp = NULL;
  834. }
  835. qtest = NULL;
  836. }
  837. if (q->has_machine_link) {
  838. object_property_del(qdev_get_machine(), "qtest");
  839. q->has_machine_link = false;
  840. }
  841. }
  842. static void qtest_set_log(Object *obj, const char *value, Error **errp)
  843. {
  844. QTest *q = QTEST(obj);
  845. if (qtest == q) {
  846. error_setg(errp, "Property 'log' can not be set now");
  847. } else {
  848. g_free(q->log);
  849. q->log = g_strdup(value);
  850. }
  851. }
  852. static char *qtest_get_log(Object *obj, Error **errp)
  853. {
  854. QTest *q = QTEST(obj);
  855. return g_strdup(q->log);
  856. }
  857. static void qtest_set_chardev(Object *obj, const char *value, Error **errp)
  858. {
  859. QTest *q = QTEST(obj);
  860. Chardev *chr;
  861. if (qtest == q) {
  862. error_setg(errp, "Property 'chardev' can not be set now");
  863. return;
  864. }
  865. chr = qemu_chr_find(value);
  866. if (!chr) {
  867. error_setg(errp, "Cannot find character device '%s'", value);
  868. return;
  869. }
  870. g_free(q->chr_name);
  871. q->chr_name = g_strdup(value);
  872. if (q->chr) {
  873. object_unref(q->chr);
  874. }
  875. q->chr = chr;
  876. object_ref(chr);
  877. }
  878. static char *qtest_get_chardev(Object *obj, Error **errp)
  879. {
  880. QTest *q = QTEST(obj);
  881. return g_strdup(q->chr_name);
  882. }
  883. static void qtest_class_init(ObjectClass *oc, void *data)
  884. {
  885. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  886. oc->unparent = qtest_unparent;
  887. ucc->complete = qtest_complete;
  888. object_class_property_add_str(oc, "chardev",
  889. qtest_get_chardev, qtest_set_chardev);
  890. object_class_property_add_str(oc, "log",
  891. qtest_get_log, qtest_set_log);
  892. }
  893. static const TypeInfo qtest_info = {
  894. .name = TYPE_QTEST,
  895. .parent = TYPE_OBJECT,
  896. .class_init = qtest_class_init,
  897. .instance_size = sizeof(QTest),
  898. .interfaces = (InterfaceInfo[]) {
  899. { TYPE_USER_CREATABLE },
  900. { }
  901. }
  902. };
  903. static void register_types(void)
  904. {
  905. type_register_static(&qtest_info);
  906. }
  907. type_init(register_types);