4 * Copyright IBM, Corp. 2011
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.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qemu-common.h"
18 #include "sysemu/qtest.h"
20 #include "chardev/char-fe.h"
21 #include "exec/ioport.h"
22 #include "exec/memory.h"
24 #include "sysemu/accel.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/cpus.h"
27 #include "qemu/config-file.h"
28 #include "qemu/option.h"
29 #include "qemu/error-report.h"
30 #include "qemu/cutils.h"
32 #include "hw/ppc/spapr_rtas.h"
39 static DeviceState *irq_intercept_dev;
40 static FILE *qtest_log_fp;
41 static CharBackend qtest_chr;
42 static GString *inbuf;
43 static int irq_levels[MAX_IRQ];
44 static qemu_timeval start_time;
45 static bool qtest_opened;
47 #define FMT_timeval "%ld.%06ld"
52 * Line based protocol, request/response based. Server can send async messages
53 * so clients should always handle many async messages before the response
60 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
61 * let you adjust the value of the clock (monotonically). All the commands
62 * return the current value of the clock in nanoseconds.
67 * Advance the clock to the next deadline. Useful when waiting for
68 * asynchronous events.
73 * Advance the clock by NS nanoseconds.
78 * Advance the clock to NS nanoseconds (do nothing if it's already past).
80 * PIO and memory access:
100 * > writeb ADDR VALUE
103 * > writew ADDR VALUE
106 * > writel ADDR VALUE
109 * > writeq ADDR VALUE
127 * > write ADDR SIZE DATA
130 * > b64read ADDR SIZE
133 * > b64write ADDR SIZE B64_DATA
136 * > memset ADDR SIZE VALUE
139 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
140 * For 'memset' a zero size is permitted and does nothing.
142 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
143 * than the expected size, the value will be zero filled at the end of the data
146 * B64_DATA is an arbitrarily long base64 encoded string.
147 * If the sizes do not match, the data will be truncated.
151 * > irq_intercept_in QOM-PATH
154 * > irq_intercept_out QOM-PATH
157 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
158 * QOM-PATH. When the pin is triggered, one of the following async messages
159 * will be printed to the qtest stream:
164 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
165 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
166 * NUM=0 even though it is remapped to GSI 2).
168 * Setting interrupt level:
170 * > set_irq_in QOM-PATH NAME NUM LEVEL
173 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
174 * LEVEL is an signed integer IRQ level.
176 * Forcibly set the given interrupt pin to the given level.
180 static int hex2nib(char ch)
182 if (ch >= '0' && ch <= '9') {
184 } else if (ch >= 'a' && ch <= 'f') {
185 return 10 + (ch - 'a');
186 } else if (ch >= 'A' && ch <= 'F') {
187 return 10 + (ch - 'A');
193 static void qtest_get_time(qemu_timeval *tv)
195 qemu_gettimeofday(tv);
196 tv->tv_sec -= start_time.tv_sec;
197 tv->tv_usec -= start_time.tv_usec;
198 if (tv->tv_usec < 0) {
199 tv->tv_usec += 1000000;
204 static void qtest_send_prefix(CharBackend *chr)
208 if (!qtest_log_fp || !qtest_opened) {
213 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
214 (long) tv.tv_sec, (long) tv.tv_usec);
217 static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
221 if (!qtest_log_fp || !qtest_opened) {
225 qtest_send_prefix(NULL);
228 vfprintf(qtest_log_fp, fmt, ap);
232 static void do_qtest_send(CharBackend *chr, const char *str, size_t len)
234 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
235 if (qtest_log_fp && qtest_opened) {
236 fprintf(qtest_log_fp, "%s", str);
240 static void qtest_send(CharBackend *chr, const char *str)
242 do_qtest_send(chr, str, strlen(str));
245 static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
246 const char *fmt, ...)
252 buffer = g_strdup_vprintf(fmt, ap);
253 qtest_send(chr, buffer);
258 static void qtest_irq_handler(void *opaque, int n, int level)
260 qemu_irq old_irq = *(qemu_irq *)opaque;
261 qemu_set_irq(old_irq, level);
263 if (irq_levels[n] != level) {
264 CharBackend *chr = &qtest_chr;
265 irq_levels[n] = level;
266 qtest_send_prefix(chr);
267 qtest_sendf(chr, "IRQ %s %d\n",
268 level ? "raise" : "lower", n);
272 static void qtest_process_command(CharBackend *chr, gchar **words)
274 const gchar *command;
285 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
286 (long) tv.tv_sec, (long) tv.tv_usec);
287 for (i = 0; words[i]; i++) {
288 fprintf(qtest_log_fp, " %s", words[i]);
290 fprintf(qtest_log_fp, "\n");
294 if (strcmp(words[0], "irq_intercept_out") == 0
295 || strcmp(words[0], "irq_intercept_in") == 0) {
300 dev = DEVICE(object_resolve_path(words[1], NULL));
302 qtest_send_prefix(chr);
303 qtest_send(chr, "FAIL Unknown device\n");
307 if (irq_intercept_dev) {
308 qtest_send_prefix(chr);
309 if (irq_intercept_dev != dev) {
310 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
312 qtest_send(chr, "OK\n");
317 QLIST_FOREACH(ngl, &dev->gpios, node) {
318 /* We don't support intercept of named GPIOs yet */
322 if (words[0][14] == 'o') {
324 for (i = 0; i < ngl->num_out; ++i) {
325 qemu_irq *disconnected = g_new0(qemu_irq, 1);
326 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
329 *disconnected = qdev_intercept_gpio_out(dev, icpt,
333 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
337 irq_intercept_dev = dev;
338 qtest_send_prefix(chr);
339 qtest_send(chr, "OK\n");
340 } else if (strcmp(words[0], "set_irq_in") == 0) {
348 g_assert(words[1] && words[2] && words[3] && words[4]);
350 dev = DEVICE(object_resolve_path(words[1], NULL));
352 qtest_send_prefix(chr);
353 qtest_send(chr, "FAIL Unknown device\n");
357 if (strcmp(words[2], "unnamed-gpio-in") == 0) {
363 ret = qemu_strtoi(words[3], NULL, 0, &num);
365 ret = qemu_strtoi(words[4], NULL, 0, &level);
368 irq = qdev_get_gpio_in_named(dev, name, num);
370 qemu_set_irq(irq, level);
371 qtest_send_prefix(chr);
372 qtest_send(chr, "OK\n");
373 } else if (strcmp(words[0], "outb") == 0 ||
374 strcmp(words[0], "outw") == 0 ||
375 strcmp(words[0], "outl") == 0) {
380 g_assert(words[1] && words[2]);
381 ret = qemu_strtoul(words[1], NULL, 0, &addr);
383 ret = qemu_strtoul(words[2], NULL, 0, &value);
385 g_assert(addr <= 0xffff);
387 if (words[0][3] == 'b') {
388 cpu_outb(addr, value);
389 } else if (words[0][3] == 'w') {
390 cpu_outw(addr, value);
391 } else if (words[0][3] == 'l') {
392 cpu_outl(addr, value);
394 qtest_send_prefix(chr);
395 qtest_send(chr, "OK\n");
396 } else if (strcmp(words[0], "inb") == 0 ||
397 strcmp(words[0], "inw") == 0 ||
398 strcmp(words[0], "inl") == 0) {
400 uint32_t value = -1U;
404 ret = qemu_strtoul(words[1], NULL, 0, &addr);
406 g_assert(addr <= 0xffff);
408 if (words[0][2] == 'b') {
409 value = cpu_inb(addr);
410 } else if (words[0][2] == 'w') {
411 value = cpu_inw(addr);
412 } else if (words[0][2] == 'l') {
413 value = cpu_inl(addr);
415 qtest_send_prefix(chr);
416 qtest_sendf(chr, "OK 0x%04x\n", value);
417 } else if (strcmp(words[0], "writeb") == 0 ||
418 strcmp(words[0], "writew") == 0 ||
419 strcmp(words[0], "writel") == 0 ||
420 strcmp(words[0], "writeq") == 0) {
425 g_assert(words[1] && words[2]);
426 ret = qemu_strtou64(words[1], NULL, 0, &addr);
428 ret = qemu_strtou64(words[2], NULL, 0, &value);
431 if (words[0][5] == 'b') {
432 uint8_t data = value;
433 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
435 } else if (words[0][5] == 'w') {
436 uint16_t data = value;
438 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
439 (uint8_t *) &data, 2, true);
440 } else if (words[0][5] == 'l') {
441 uint32_t data = value;
443 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
444 (uint8_t *) &data, 4, true);
445 } else if (words[0][5] == 'q') {
446 uint64_t data = value;
448 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
449 (uint8_t *) &data, 8, true);
451 qtest_send_prefix(chr);
452 qtest_send(chr, "OK\n");
453 } else if (strcmp(words[0], "readb") == 0 ||
454 strcmp(words[0], "readw") == 0 ||
455 strcmp(words[0], "readl") == 0 ||
456 strcmp(words[0], "readq") == 0) {
458 uint64_t value = UINT64_C(-1);
462 ret = qemu_strtou64(words[1], NULL, 0, &addr);
465 if (words[0][4] == 'b') {
467 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
470 } else if (words[0][4] == 'w') {
472 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
473 (uint8_t *) &data, 2, false);
474 value = tswap16(data);
475 } else if (words[0][4] == 'l') {
477 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
478 (uint8_t *) &data, 4, false);
479 value = tswap32(data);
480 } else if (words[0][4] == 'q') {
481 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
482 (uint8_t *) &value, 8, false);
485 qtest_send_prefix(chr);
486 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
487 } else if (strcmp(words[0], "read") == 0) {
488 uint64_t addr, len, i;
493 g_assert(words[1] && words[2]);
494 ret = qemu_strtou64(words[1], NULL, 0, &addr);
496 ret = qemu_strtou64(words[2], NULL, 0, &len);
498 /* We'd send garbage to libqtest if len is 0 */
501 data = g_malloc(len);
502 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
505 enc = g_malloc(2 * len + 1);
506 for (i = 0; i < len; i++) {
507 sprintf(&enc[i * 2], "%02x", data[i]);
510 qtest_send_prefix(chr);
511 qtest_sendf(chr, "OK 0x%s\n", enc);
515 } else if (strcmp(words[0], "b64read") == 0) {
521 g_assert(words[1] && words[2]);
522 ret = qemu_strtou64(words[1], NULL, 0, &addr);
524 ret = qemu_strtou64(words[2], NULL, 0, &len);
527 data = g_malloc(len);
528 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
530 b64_data = g_base64_encode(data, len);
531 qtest_send_prefix(chr);
532 qtest_sendf(chr, "OK %s\n", b64_data);
536 } else if (strcmp(words[0], "write") == 0) {
537 uint64_t addr, len, i;
542 g_assert(words[1] && words[2] && words[3]);
543 ret = qemu_strtou64(words[1], NULL, 0, &addr);
545 ret = qemu_strtou64(words[2], NULL, 0, &len);
548 data_len = strlen(words[3]);
550 qtest_send(chr, "ERR invalid argument size\n");
554 data = g_malloc(len);
555 for (i = 0; i < len; i++) {
556 if ((i * 2 + 4) <= data_len) {
557 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
558 data[i] |= hex2nib(words[3][i * 2 + 3]);
563 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
567 qtest_send_prefix(chr);
568 qtest_send(chr, "OK\n");
569 } else if (strcmp(words[0], "memset") == 0) {
572 unsigned long pattern;
575 g_assert(words[1] && words[2] && words[3]);
576 ret = qemu_strtou64(words[1], NULL, 0, &addr);
578 ret = qemu_strtou64(words[2], NULL, 0, &len);
580 ret = qemu_strtoul(words[3], NULL, 0, &pattern);
584 data = g_malloc(len);
585 memset(data, pattern, len);
586 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
591 qtest_send_prefix(chr);
592 qtest_send(chr, "OK\n");
593 } else if (strcmp(words[0], "b64write") == 0) {
600 g_assert(words[1] && words[2] && words[3]);
601 ret = qemu_strtou64(words[1], NULL, 0, &addr);
603 ret = qemu_strtou64(words[2], NULL, 0, &len);
606 data_len = strlen(words[3]);
608 qtest_send(chr, "ERR invalid argument size\n");
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", "
617 out_len = MIN(out_len, len);
620 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
623 qtest_send_prefix(chr);
624 qtest_send(chr, "OK\n");
625 } else if (strcmp(words[0], "endianness") == 0) {
626 qtest_send_prefix(chr);
627 #if defined(TARGET_WORDS_BIGENDIAN)
628 qtest_sendf(chr, "OK big\n");
630 qtest_sendf(chr, "OK little\n");
633 } else if (strcmp(words[0], "rtas") == 0) {
634 uint64_t res, args, ret;
635 unsigned long nargs, nret;
638 rc = qemu_strtoul(words[2], NULL, 0, &nargs);
640 rc = qemu_strtou64(words[3], NULL, 0, &args);
642 rc = qemu_strtoul(words[4], NULL, 0, &nret);
644 rc = qemu_strtou64(words[5], NULL, 0, &ret);
646 res = qtest_rtas_call(words[1], nargs, args, nret, ret);
648 qtest_send_prefix(chr);
649 qtest_sendf(chr, "OK %"PRIu64"\n", res);
651 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
655 int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
658 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
660 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
661 qtest_send_prefix(chr);
662 qtest_sendf(chr, "OK %"PRIi64"\n",
663 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
664 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
669 ret = qemu_strtoi64(words[1], NULL, 0, &ns);
671 qtest_clock_warp(ns);
672 qtest_send_prefix(chr);
673 qtest_sendf(chr, "OK %"PRIi64"\n",
674 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
676 qtest_send_prefix(chr);
677 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
681 static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
685 while ((end = strchr(inbuf->str, '\n')) != NULL) {
690 offset = end - inbuf->str;
692 cmd = g_string_new_len(inbuf->str, offset);
693 g_string_erase(inbuf, 0, offset + 1);
695 words = g_strsplit(cmd->str, " ", 0);
696 qtest_process_command(chr, words);
699 g_string_free(cmd, TRUE);
703 static void qtest_read(void *opaque, const uint8_t *buf, int size)
705 CharBackend *chr = opaque;
707 g_string_append_len(inbuf, (const gchar *)buf, size);
708 qtest_process_inbuf(chr, inbuf);
711 static int qtest_can_read(void *opaque)
716 static void qtest_event(void *opaque, int event)
721 case CHR_EVENT_OPENED:
723 * We used to call qemu_system_reset() here, hoping we could
724 * use the same process for multiple tests that way. Never
725 * used. Injects an extra reset even when it's not used, and
726 * that can mess up tests, e.g. -boot once.
728 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
731 qemu_gettimeofday(&start_time);
734 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
735 (long) start_time.tv_sec, (long) start_time.tv_usec);
738 case CHR_EVENT_CLOSED:
739 qtest_opened = false;
743 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
744 (long) tv.tv_sec, (long) tv.tv_usec);
752 static int qtest_init_accel(MachineState *ms)
754 QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
756 qemu_opt_set(opts, "shift", "0", &error_abort);
757 configure_icount(opts, &error_abort);
762 void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
766 chr = qemu_chr_new("qtest", qtest_chrdev);
769 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
775 if (strcmp(qtest_log, "none") != 0) {
776 qtest_log_fp = fopen(qtest_log, "w+");
779 qtest_log_fp = stderr;
782 qemu_chr_fe_init(&qtest_chr, chr, errp);
783 qemu_chr_fe_set_handlers(&qtest_chr, qtest_can_read, qtest_read,
784 qtest_event, NULL, &qtest_chr, NULL, true);
785 qemu_chr_fe_set_echo(&qtest_chr, true);
787 inbuf = g_string_new("");
790 bool qtest_driver(void)
792 return qtest_chr.chr != NULL;
795 static void qtest_accel_class_init(ObjectClass *oc, void *data)
797 AccelClass *ac = ACCEL_CLASS(oc);
799 ac->available = qtest_available;
800 ac->init_machine = qtest_init_accel;
801 ac->allowed = &qtest_allowed;
804 #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
806 static const TypeInfo qtest_accel_type = {
807 .name = TYPE_QTEST_ACCEL,
808 .parent = TYPE_ACCEL,
809 .class_init = qtest_accel_class_init,
812 static void qtest_type_init(void)
814 type_register_static(&qtest_accel_type);
817 type_init(qtest_type_init);