4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
17 #include "sysemu/qtest.h"
18 #include "sysemu/runstate.h"
19 #include "chardev/char-fe.h"
20 #include "exec/ioport.h"
21 #include "exec/memory.h"
23 #include "sysemu/accel.h"
24 #include "sysemu/cpu-timers.h"
25 #include "qemu/config-file.h"
26 #include "qemu/option.h"
27 #include "qemu/error-report.h"
28 #include "qemu/module.h"
29 #include "qemu/cutils.h"
30 #include CONFIG_DEVICES
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
;
46 static void (*qtest_server_send
)(void*, const char*);
47 static void *qtest_server_send_opaque
;
49 #define FMT_timeval "%ld.%06ld"
54 * Line based protocol, request/response based. Server can send async messages
55 * so clients should always handle many async messages before the response
64 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
65 * let you adjust the value of the clock (monotonically). All the commands
66 * return the current value of the clock in nanoseconds.
68 * .. code-block:: none
73 * Advance the clock to the next deadline. Useful when waiting for
74 * asynchronous events.
76 * .. code-block:: none
81 * Advance the clock by NS nanoseconds.
83 * .. code-block:: none
88 * Advance the clock to NS nanoseconds (do nothing if it's already past).
90 * PIO and memory access:
91 * """"""""""""""""""""""
93 * .. code-block:: none
98 * .. code-block:: none
103 * .. code-block:: none
108 * .. code-block:: none
113 * .. code-block:: none
118 * .. code-block:: none
123 * .. code-block:: none
125 * > writeb ADDR VALUE
128 * .. code-block:: none
130 * > writew ADDR VALUE
133 * .. code-block:: none
135 * > writel ADDR VALUE
138 * .. code-block:: none
140 * > writeq ADDR VALUE
143 * .. code-block:: none
148 * .. code-block:: none
153 * .. code-block:: none
158 * .. code-block:: none
163 * .. code-block:: none
168 * .. code-block:: none
170 * > write ADDR SIZE DATA
173 * .. code-block:: none
175 * > b64read ADDR SIZE
178 * .. code-block:: none
180 * > b64write ADDR SIZE B64_DATA
183 * .. code-block:: none
185 * > memset ADDR SIZE VALUE
188 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
189 * For 'memset' a zero size is permitted and does nothing.
191 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
192 * than the expected size, the value will be zero filled at the end of the data
195 * B64_DATA is an arbitrarily long base64 encoded string.
196 * If the sizes do not match, the data will be truncated.
201 * .. code-block:: none
203 * > irq_intercept_in QOM-PATH
206 * .. code-block:: none
208 * > irq_intercept_out QOM-PATH
211 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
212 * QOM-PATH. When the pin is triggered, one of the following async messages
213 * will be printed to the qtest stream::
218 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
219 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
220 * NUM=0 even though it is remapped to GSI 2).
222 * Setting interrupt level:
223 * """"""""""""""""""""""""
225 * .. code-block:: none
227 * > set_irq_in QOM-PATH NAME NUM LEVEL
230 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
231 * LEVEL is an signed integer IRQ level.
233 * Forcibly set the given interrupt pin to the given level.
237 static int hex2nib(char ch
)
239 if (ch
>= '0' && ch
<= '9') {
241 } else if (ch
>= 'a' && ch
<= 'f') {
242 return 10 + (ch
- 'a');
243 } else if (ch
>= 'A' && ch
<= 'F') {
244 return 10 + (ch
- 'A');
250 static void qtest_get_time(qemu_timeval
*tv
)
252 qemu_gettimeofday(tv
);
253 tv
->tv_sec
-= start_time
.tv_sec
;
254 tv
->tv_usec
-= start_time
.tv_usec
;
255 if (tv
->tv_usec
< 0) {
256 tv
->tv_usec
+= 1000000;
261 static void qtest_send_prefix(CharBackend
*chr
)
265 if (!qtest_log_fp
|| !qtest_opened
) {
270 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ",
271 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
274 static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt
, ...)
278 if (!qtest_log_fp
|| !qtest_opened
) {
282 qtest_send_prefix(NULL
);
285 vfprintf(qtest_log_fp
, fmt
, ap
);
289 static void qtest_server_char_be_send(void *opaque
, const char *str
)
291 size_t len
= strlen(str
);
292 CharBackend
* chr
= (CharBackend
*)opaque
;
293 qemu_chr_fe_write_all(chr
, (uint8_t *)str
, len
);
294 if (qtest_log_fp
&& qtest_opened
) {
295 fprintf(qtest_log_fp
, "%s", str
);
299 static void qtest_send(CharBackend
*chr
, const char *str
)
301 qtest_server_send(qtest_server_send_opaque
, str
);
304 static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend
*chr
,
305 const char *fmt
, ...)
311 buffer
= g_strdup_vprintf(fmt
, ap
);
312 qtest_send(chr
, buffer
);
317 static void qtest_irq_handler(void *opaque
, int n
, int level
)
319 qemu_irq old_irq
= *(qemu_irq
*)opaque
;
320 qemu_set_irq(old_irq
, level
);
322 if (irq_levels
[n
] != level
) {
323 CharBackend
*chr
= &qtest_chr
;
324 irq_levels
[n
] = level
;
325 qtest_send_prefix(chr
);
326 qtest_sendf(chr
, "IRQ %s %d\n",
327 level
? "raise" : "lower", n
);
331 static int64_t qtest_clock_counter
;
333 int64_t qtest_get_virtual_clock(void)
335 return qatomic_read_i64(&qtest_clock_counter
);
338 static void qtest_set_virtual_clock(int64_t count
)
340 qatomic_set_i64(&qtest_clock_counter
, count
);
343 static void qtest_clock_warp(int64_t dest
)
345 int64_t clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
346 AioContext
*aio_context
;
347 assert(qtest_enabled());
348 aio_context
= qemu_get_aio_context();
349 while (clock
< dest
) {
350 int64_t deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
,
351 QEMU_TIMER_ATTR_ALL
);
352 int64_t warp
= qemu_soonest_timeout(dest
- clock
, deadline
);
354 qtest_set_virtual_clock(qtest_get_virtual_clock() + warp
);
356 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL
);
357 timerlist_run_timers(aio_context
->tlg
.tl
[QEMU_CLOCK_VIRTUAL
]);
358 clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
360 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
363 static void qtest_process_command(CharBackend
*chr
, gchar
**words
)
365 const gchar
*command
;
376 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]",
377 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
378 for (i
= 0; words
[i
]; i
++) {
379 fprintf(qtest_log_fp
, " %s", words
[i
]);
381 fprintf(qtest_log_fp
, "\n");
385 if (strcmp(words
[0], "irq_intercept_out") == 0
386 || strcmp(words
[0], "irq_intercept_in") == 0) {
391 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
393 qtest_send_prefix(chr
);
394 qtest_send(chr
, "FAIL Unknown device\n");
398 if (irq_intercept_dev
) {
399 qtest_send_prefix(chr
);
400 if (irq_intercept_dev
!= dev
) {
401 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
403 qtest_send(chr
, "OK\n");
408 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
409 /* We don't support intercept of named GPIOs yet */
413 if (words
[0][14] == 'o') {
415 for (i
= 0; i
< ngl
->num_out
; ++i
) {
416 qemu_irq
*disconnected
= g_new0(qemu_irq
, 1);
417 qemu_irq icpt
= qemu_allocate_irq(qtest_irq_handler
,
420 *disconnected
= qdev_intercept_gpio_out(dev
, icpt
,
424 qemu_irq_intercept_in(ngl
->in
, qtest_irq_handler
,
428 irq_intercept_dev
= dev
;
429 qtest_send_prefix(chr
);
430 qtest_send(chr
, "OK\n");
431 } else if (strcmp(words
[0], "set_irq_in") == 0) {
439 g_assert(words
[1] && words
[2] && words
[3] && words
[4]);
441 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
443 qtest_send_prefix(chr
);
444 qtest_send(chr
, "FAIL Unknown device\n");
448 if (strcmp(words
[2], "unnamed-gpio-in") == 0) {
454 ret
= qemu_strtoi(words
[3], NULL
, 0, &num
);
456 ret
= qemu_strtoi(words
[4], NULL
, 0, &level
);
459 irq
= qdev_get_gpio_in_named(dev
, name
, num
);
461 qemu_set_irq(irq
, level
);
462 qtest_send_prefix(chr
);
463 qtest_send(chr
, "OK\n");
464 } else if (strcmp(words
[0], "outb") == 0 ||
465 strcmp(words
[0], "outw") == 0 ||
466 strcmp(words
[0], "outl") == 0) {
471 g_assert(words
[1] && words
[2]);
472 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
474 ret
= qemu_strtoul(words
[2], NULL
, 0, &value
);
476 g_assert(addr
<= 0xffff);
478 if (words
[0][3] == 'b') {
479 cpu_outb(addr
, value
);
480 } else if (words
[0][3] == 'w') {
481 cpu_outw(addr
, value
);
482 } else if (words
[0][3] == 'l') {
483 cpu_outl(addr
, value
);
485 qtest_send_prefix(chr
);
486 qtest_send(chr
, "OK\n");
487 } else if (strcmp(words
[0], "inb") == 0 ||
488 strcmp(words
[0], "inw") == 0 ||
489 strcmp(words
[0], "inl") == 0) {
491 uint32_t value
= -1U;
495 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
497 g_assert(addr
<= 0xffff);
499 if (words
[0][2] == 'b') {
500 value
= cpu_inb(addr
);
501 } else if (words
[0][2] == 'w') {
502 value
= cpu_inw(addr
);
503 } else if (words
[0][2] == 'l') {
504 value
= cpu_inl(addr
);
506 qtest_send_prefix(chr
);
507 qtest_sendf(chr
, "OK 0x%04x\n", value
);
508 } else if (strcmp(words
[0], "writeb") == 0 ||
509 strcmp(words
[0], "writew") == 0 ||
510 strcmp(words
[0], "writel") == 0 ||
511 strcmp(words
[0], "writeq") == 0) {
516 g_assert(words
[1] && words
[2]);
517 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
519 ret
= qemu_strtou64(words
[2], NULL
, 0, &value
);
522 if (words
[0][5] == 'b') {
523 uint8_t data
= value
;
524 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
526 } else if (words
[0][5] == 'w') {
527 uint16_t data
= value
;
529 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
531 } else if (words
[0][5] == 'l') {
532 uint32_t data
= value
;
534 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
536 } else if (words
[0][5] == 'q') {
537 uint64_t data
= value
;
539 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
542 qtest_send_prefix(chr
);
543 qtest_send(chr
, "OK\n");
544 } else if (strcmp(words
[0], "readb") == 0 ||
545 strcmp(words
[0], "readw") == 0 ||
546 strcmp(words
[0], "readl") == 0 ||
547 strcmp(words
[0], "readq") == 0) {
549 uint64_t value
= UINT64_C(-1);
553 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
556 if (words
[0][4] == 'b') {
558 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
561 } else if (words
[0][4] == 'w') {
563 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
565 value
= tswap16(data
);
566 } else if (words
[0][4] == 'l') {
568 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
570 value
= tswap32(data
);
571 } else if (words
[0][4] == 'q') {
572 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
576 qtest_send_prefix(chr
);
577 qtest_sendf(chr
, "OK 0x%016" PRIx64
"\n", value
);
578 } else if (strcmp(words
[0], "read") == 0) {
579 uint64_t addr
, len
, i
;
584 g_assert(words
[1] && words
[2]);
585 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
587 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
589 /* We'd send garbage to libqtest if len is 0 */
592 data
= g_malloc(len
);
593 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
596 enc
= g_malloc(2 * len
+ 1);
597 for (i
= 0; i
< len
; i
++) {
598 sprintf(&enc
[i
* 2], "%02x", data
[i
]);
601 qtest_send_prefix(chr
);
602 qtest_sendf(chr
, "OK 0x%s\n", enc
);
606 } else if (strcmp(words
[0], "b64read") == 0) {
612 g_assert(words
[1] && words
[2]);
613 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
615 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
618 data
= g_malloc(len
);
619 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
621 b64_data
= g_base64_encode(data
, len
);
622 qtest_send_prefix(chr
);
623 qtest_sendf(chr
, "OK %s\n", b64_data
);
627 } else if (strcmp(words
[0], "write") == 0) {
628 uint64_t addr
, len
, i
;
633 g_assert(words
[1] && words
[2] && words
[3]);
634 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
636 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
639 data_len
= strlen(words
[3]);
641 qtest_send(chr
, "ERR invalid argument size\n");
645 data
= g_malloc(len
);
646 for (i
= 0; i
< len
; i
++) {
647 if ((i
* 2 + 4) <= data_len
) {
648 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
649 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
654 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
658 qtest_send_prefix(chr
);
659 qtest_send(chr
, "OK\n");
660 } else if (strcmp(words
[0], "memset") == 0) {
663 unsigned long pattern
;
666 g_assert(words
[1] && words
[2] && words
[3]);
667 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
669 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
671 ret
= qemu_strtoul(words
[3], NULL
, 0, &pattern
);
675 data
= g_malloc(len
);
676 memset(data
, pattern
, len
);
677 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
682 qtest_send_prefix(chr
);
683 qtest_send(chr
, "OK\n");
684 } else if (strcmp(words
[0], "b64write") == 0) {
691 g_assert(words
[1] && words
[2] && words
[3]);
692 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
694 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
697 data_len
= strlen(words
[3]);
699 qtest_send(chr
, "ERR invalid argument size\n");
703 data
= g_base64_decode_inplace(words
[3], &out_len
);
704 if (out_len
!= len
) {
705 qtest_log_send("b64write: data length mismatch (told %"PRIu64
", "
708 out_len
= MIN(out_len
, len
);
711 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
714 qtest_send_prefix(chr
);
715 qtest_send(chr
, "OK\n");
716 } else if (strcmp(words
[0], "endianness") == 0) {
717 qtest_send_prefix(chr
);
718 #if defined(TARGET_WORDS_BIGENDIAN)
719 qtest_sendf(chr
, "OK big\n");
721 qtest_sendf(chr
, "OK little\n");
723 #ifdef CONFIG_PSERIES
724 } else if (strcmp(words
[0], "rtas") == 0) {
725 uint64_t res
, args
, ret
;
726 unsigned long nargs
, nret
;
729 rc
= qemu_strtoul(words
[2], NULL
, 0, &nargs
);
731 rc
= qemu_strtou64(words
[3], NULL
, 0, &args
);
733 rc
= qemu_strtoul(words
[4], NULL
, 0, &nret
);
735 rc
= qemu_strtou64(words
[5], NULL
, 0, &ret
);
737 res
= qtest_rtas_call(words
[1], nargs
, args
, nret
, ret
);
739 qtest_send_prefix(chr
);
740 qtest_sendf(chr
, "OK %"PRIu64
"\n", res
);
742 } else if (qtest_enabled() && strcmp(words
[0], "clock_step") == 0) {
746 int ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
749 ns
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
,
750 QEMU_TIMER_ATTR_ALL
);
752 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + ns
);
753 qtest_send_prefix(chr
);
754 qtest_sendf(chr
, "OK %"PRIi64
"\n",
755 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
756 } else if (strcmp(words
[0], "module_load") == 0) {
757 g_assert(words
[1] && words
[2]);
759 qtest_send_prefix(chr
);
760 if (module_load_one(words
[1], words
[2], false)) {
761 qtest_sendf(chr
, "OK\n");
763 qtest_sendf(chr
, "FAIL\n");
765 } else if (qtest_enabled() && strcmp(words
[0], "clock_set") == 0) {
770 ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
772 qtest_clock_warp(ns
);
773 qtest_send_prefix(chr
);
774 qtest_sendf(chr
, "OK %"PRIi64
"\n",
775 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
777 qtest_send_prefix(chr
);
778 qtest_sendf(chr
, "FAIL Unknown command '%s'\n", words
[0]);
782 static void qtest_process_inbuf(CharBackend
*chr
, GString
*inbuf
)
786 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
791 offset
= end
- inbuf
->str
;
793 cmd
= g_string_new_len(inbuf
->str
, offset
);
794 g_string_erase(inbuf
, 0, offset
+ 1);
796 words
= g_strsplit(cmd
->str
, " ", 0);
797 qtest_process_command(chr
, words
);
800 g_string_free(cmd
, TRUE
);
804 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
806 CharBackend
*chr
= opaque
;
808 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
809 qtest_process_inbuf(chr
, inbuf
);
812 static int qtest_can_read(void *opaque
)
817 static void qtest_event(void *opaque
, QEMUChrEvent event
)
822 case CHR_EVENT_OPENED
:
824 * We used to call qemu_system_reset() here, hoping we could
825 * use the same process for multiple tests that way. Never
826 * used. Injects an extra reset even when it's not used, and
827 * that can mess up tests, e.g. -boot once.
829 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
832 qemu_gettimeofday(&start_time
);
835 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n",
836 (long) start_time
.tv_sec
, (long) start_time
.tv_usec
);
839 case CHR_EVENT_CLOSED
:
840 qtest_opened
= false;
844 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n",
845 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
852 void qtest_server_init(const char *qtest_chrdev
, const char *qtest_log
, Error
**errp
)
856 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
859 error_setg(errp
, "Failed to initialize device for qtest: \"%s\"",
865 if (strcmp(qtest_log
, "none") != 0) {
866 qtest_log_fp
= fopen(qtest_log
, "w+");
869 qtest_log_fp
= stderr
;
872 qemu_chr_fe_init(&qtest_chr
, chr
, errp
);
873 qemu_chr_fe_set_handlers(&qtest_chr
, qtest_can_read
, qtest_read
,
874 qtest_event
, NULL
, &qtest_chr
, NULL
, true);
875 qemu_chr_fe_set_echo(&qtest_chr
, true);
877 inbuf
= g_string_new("");
879 if (!qtest_server_send
) {
880 qtest_server_set_send_handler(qtest_server_char_be_send
, &qtest_chr
);
884 void qtest_server_set_send_handler(void (*send
)(void*, const char*),
887 qtest_server_send
= send
;
888 qtest_server_send_opaque
= opaque
;
891 bool qtest_driver(void)
893 return qtest_chr
.chr
!= NULL
;
896 void qtest_server_inproc_recv(void *dummy
, const char *buf
)
898 static GString
*gstr
;
900 gstr
= g_string_new(NULL
);
902 g_string_append(gstr
, buf
);
903 if (gstr
->str
[gstr
->len
- 1] == '\n') {
904 qtest_process_inbuf(NULL
, gstr
);
905 g_string_truncate(gstr
, 0);