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/cpus.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"
31 #include "hw/ppc/spapr_rtas.h"
38 static DeviceState
*irq_intercept_dev
;
39 static FILE *qtest_log_fp
;
40 static CharBackend qtest_chr
;
41 static GString
*inbuf
;
42 static int irq_levels
[MAX_IRQ
];
43 static qemu_timeval start_time
;
44 static bool qtest_opened
;
46 #define FMT_timeval "%ld.%06ld"
51 * Line based protocol, request/response based. Server can send async messages
52 * so clients should always handle many async messages before the response
59 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
60 * let you adjust the value of the clock (monotonically). All the commands
61 * return the current value of the clock in nanoseconds.
66 * Advance the clock to the next deadline. Useful when waiting for
67 * asynchronous events.
72 * Advance the clock by NS nanoseconds.
77 * Advance the clock to NS nanoseconds (do nothing if it's already past).
79 * PIO and memory access:
102 * > writew ADDR VALUE
105 * > writel ADDR VALUE
108 * > writeq ADDR VALUE
126 * > write ADDR SIZE DATA
129 * > b64read ADDR SIZE
132 * > b64write ADDR SIZE B64_DATA
135 * > memset ADDR SIZE VALUE
138 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
139 * For 'memset' a zero size is permitted and does nothing.
141 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
142 * than the expected size, the value will be zero filled at the end of the data
145 * B64_DATA is an arbitrarily long base64 encoded string.
146 * If the sizes do not match, the data will be truncated.
150 * > irq_intercept_in QOM-PATH
153 * > irq_intercept_out QOM-PATH
156 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
157 * QOM-PATH. When the pin is triggered, one of the following async messages
158 * will be printed to the qtest stream:
163 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
164 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
165 * NUM=0 even though it is remapped to GSI 2).
167 * Setting interrupt level:
169 * > set_irq_in QOM-PATH NAME NUM LEVEL
172 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
173 * LEVEL is an signed integer IRQ level.
175 * Forcibly set the given interrupt pin to the given level.
179 static int hex2nib(char ch
)
181 if (ch
>= '0' && ch
<= '9') {
183 } else if (ch
>= 'a' && ch
<= 'f') {
184 return 10 + (ch
- 'a');
185 } else if (ch
>= 'A' && ch
<= 'F') {
186 return 10 + (ch
- 'A');
192 static void qtest_get_time(qemu_timeval
*tv
)
194 qemu_gettimeofday(tv
);
195 tv
->tv_sec
-= start_time
.tv_sec
;
196 tv
->tv_usec
-= start_time
.tv_usec
;
197 if (tv
->tv_usec
< 0) {
198 tv
->tv_usec
+= 1000000;
203 static void qtest_send_prefix(CharBackend
*chr
)
207 if (!qtest_log_fp
|| !qtest_opened
) {
212 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ",
213 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
216 static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt
, ...)
220 if (!qtest_log_fp
|| !qtest_opened
) {
224 qtest_send_prefix(NULL
);
227 vfprintf(qtest_log_fp
, fmt
, ap
);
231 static void do_qtest_send(CharBackend
*chr
, const char *str
, size_t len
)
233 qemu_chr_fe_write_all(chr
, (uint8_t *)str
, len
);
234 if (qtest_log_fp
&& qtest_opened
) {
235 fprintf(qtest_log_fp
, "%s", str
);
239 static void qtest_send(CharBackend
*chr
, const char *str
)
241 do_qtest_send(chr
, str
, strlen(str
));
244 static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend
*chr
,
245 const char *fmt
, ...)
251 buffer
= g_strdup_vprintf(fmt
, ap
);
252 qtest_send(chr
, buffer
);
257 static void qtest_irq_handler(void *opaque
, int n
, int level
)
259 qemu_irq old_irq
= *(qemu_irq
*)opaque
;
260 qemu_set_irq(old_irq
, level
);
262 if (irq_levels
[n
] != level
) {
263 CharBackend
*chr
= &qtest_chr
;
264 irq_levels
[n
] = level
;
265 qtest_send_prefix(chr
);
266 qtest_sendf(chr
, "IRQ %s %d\n",
267 level
? "raise" : "lower", n
);
271 static void qtest_process_command(CharBackend
*chr
, gchar
**words
)
273 const gchar
*command
;
284 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]",
285 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
286 for (i
= 0; words
[i
]; i
++) {
287 fprintf(qtest_log_fp
, " %s", words
[i
]);
289 fprintf(qtest_log_fp
, "\n");
293 if (strcmp(words
[0], "irq_intercept_out") == 0
294 || strcmp(words
[0], "irq_intercept_in") == 0) {
299 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
301 qtest_send_prefix(chr
);
302 qtest_send(chr
, "FAIL Unknown device\n");
306 if (irq_intercept_dev
) {
307 qtest_send_prefix(chr
);
308 if (irq_intercept_dev
!= dev
) {
309 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
311 qtest_send(chr
, "OK\n");
316 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
317 /* We don't support intercept of named GPIOs yet */
321 if (words
[0][14] == 'o') {
323 for (i
= 0; i
< ngl
->num_out
; ++i
) {
324 qemu_irq
*disconnected
= g_new0(qemu_irq
, 1);
325 qemu_irq icpt
= qemu_allocate_irq(qtest_irq_handler
,
328 *disconnected
= qdev_intercept_gpio_out(dev
, icpt
,
332 qemu_irq_intercept_in(ngl
->in
, qtest_irq_handler
,
336 irq_intercept_dev
= dev
;
337 qtest_send_prefix(chr
);
338 qtest_send(chr
, "OK\n");
339 } else if (strcmp(words
[0], "set_irq_in") == 0) {
347 g_assert(words
[1] && words
[2] && words
[3] && words
[4]);
349 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
351 qtest_send_prefix(chr
);
352 qtest_send(chr
, "FAIL Unknown device\n");
356 if (strcmp(words
[2], "unnamed-gpio-in") == 0) {
362 ret
= qemu_strtoi(words
[3], NULL
, 0, &num
);
364 ret
= qemu_strtoi(words
[4], NULL
, 0, &level
);
367 irq
= qdev_get_gpio_in_named(dev
, name
, num
);
369 qemu_set_irq(irq
, level
);
370 qtest_send_prefix(chr
);
371 qtest_send(chr
, "OK\n");
372 } else if (strcmp(words
[0], "outb") == 0 ||
373 strcmp(words
[0], "outw") == 0 ||
374 strcmp(words
[0], "outl") == 0) {
379 g_assert(words
[1] && words
[2]);
380 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
382 ret
= qemu_strtoul(words
[2], NULL
, 0, &value
);
384 g_assert(addr
<= 0xffff);
386 if (words
[0][3] == 'b') {
387 cpu_outb(addr
, value
);
388 } else if (words
[0][3] == 'w') {
389 cpu_outw(addr
, value
);
390 } else if (words
[0][3] == 'l') {
391 cpu_outl(addr
, value
);
393 qtest_send_prefix(chr
);
394 qtest_send(chr
, "OK\n");
395 } else if (strcmp(words
[0], "inb") == 0 ||
396 strcmp(words
[0], "inw") == 0 ||
397 strcmp(words
[0], "inl") == 0) {
399 uint32_t value
= -1U;
403 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
405 g_assert(addr
<= 0xffff);
407 if (words
[0][2] == 'b') {
408 value
= cpu_inb(addr
);
409 } else if (words
[0][2] == 'w') {
410 value
= cpu_inw(addr
);
411 } else if (words
[0][2] == 'l') {
412 value
= cpu_inl(addr
);
414 qtest_send_prefix(chr
);
415 qtest_sendf(chr
, "OK 0x%04x\n", value
);
416 } else if (strcmp(words
[0], "writeb") == 0 ||
417 strcmp(words
[0], "writew") == 0 ||
418 strcmp(words
[0], "writel") == 0 ||
419 strcmp(words
[0], "writeq") == 0) {
424 g_assert(words
[1] && words
[2]);
425 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
427 ret
= qemu_strtou64(words
[2], NULL
, 0, &value
);
430 if (words
[0][5] == 'b') {
431 uint8_t data
= value
;
432 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
434 } else if (words
[0][5] == 'w') {
435 uint16_t data
= value
;
437 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
438 (uint8_t *) &data
, 2, true);
439 } else if (words
[0][5] == 'l') {
440 uint32_t data
= value
;
442 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
443 (uint8_t *) &data
, 4, true);
444 } else if (words
[0][5] == 'q') {
445 uint64_t data
= value
;
447 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
448 (uint8_t *) &data
, 8, true);
450 qtest_send_prefix(chr
);
451 qtest_send(chr
, "OK\n");
452 } else if (strcmp(words
[0], "readb") == 0 ||
453 strcmp(words
[0], "readw") == 0 ||
454 strcmp(words
[0], "readl") == 0 ||
455 strcmp(words
[0], "readq") == 0) {
457 uint64_t value
= UINT64_C(-1);
461 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
464 if (words
[0][4] == 'b') {
466 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
469 } else if (words
[0][4] == 'w') {
471 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
472 (uint8_t *) &data
, 2, false);
473 value
= tswap16(data
);
474 } else if (words
[0][4] == 'l') {
476 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
477 (uint8_t *) &data
, 4, false);
478 value
= tswap32(data
);
479 } else if (words
[0][4] == 'q') {
480 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
481 (uint8_t *) &value
, 8, false);
484 qtest_send_prefix(chr
);
485 qtest_sendf(chr
, "OK 0x%016" PRIx64
"\n", value
);
486 } else if (strcmp(words
[0], "read") == 0) {
487 uint64_t addr
, len
, i
;
492 g_assert(words
[1] && words
[2]);
493 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
495 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
497 /* We'd send garbage to libqtest if len is 0 */
500 data
= g_malloc(len
);
501 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
504 enc
= g_malloc(2 * len
+ 1);
505 for (i
= 0; i
< len
; i
++) {
506 sprintf(&enc
[i
* 2], "%02x", data
[i
]);
509 qtest_send_prefix(chr
);
510 qtest_sendf(chr
, "OK 0x%s\n", enc
);
514 } else if (strcmp(words
[0], "b64read") == 0) {
520 g_assert(words
[1] && words
[2]);
521 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
523 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
526 data
= g_malloc(len
);
527 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
529 b64_data
= g_base64_encode(data
, len
);
530 qtest_send_prefix(chr
);
531 qtest_sendf(chr
, "OK %s\n", b64_data
);
535 } else if (strcmp(words
[0], "write") == 0) {
536 uint64_t addr
, len
, i
;
541 g_assert(words
[1] && words
[2] && words
[3]);
542 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
544 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
547 data_len
= strlen(words
[3]);
549 qtest_send(chr
, "ERR invalid argument size\n");
553 data
= g_malloc(len
);
554 for (i
= 0; i
< len
; i
++) {
555 if ((i
* 2 + 4) <= data_len
) {
556 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
557 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
562 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
566 qtest_send_prefix(chr
);
567 qtest_send(chr
, "OK\n");
568 } else if (strcmp(words
[0], "memset") == 0) {
571 unsigned long pattern
;
574 g_assert(words
[1] && words
[2] && words
[3]);
575 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
577 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
579 ret
= qemu_strtoul(words
[3], NULL
, 0, &pattern
);
583 data
= g_malloc(len
);
584 memset(data
, pattern
, len
);
585 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
590 qtest_send_prefix(chr
);
591 qtest_send(chr
, "OK\n");
592 } else if (strcmp(words
[0], "b64write") == 0) {
599 g_assert(words
[1] && words
[2] && words
[3]);
600 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
602 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
605 data_len
= strlen(words
[3]);
607 qtest_send(chr
, "ERR invalid argument size\n");
611 data
= g_base64_decode_inplace(words
[3], &out_len
);
612 if (out_len
!= len
) {
613 qtest_log_send("b64write: data length mismatch (told %"PRIu64
", "
616 out_len
= MIN(out_len
, len
);
619 address_space_rw(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
622 qtest_send_prefix(chr
);
623 qtest_send(chr
, "OK\n");
624 } else if (strcmp(words
[0], "endianness") == 0) {
625 qtest_send_prefix(chr
);
626 #if defined(TARGET_WORDS_BIGENDIAN)
627 qtest_sendf(chr
, "OK big\n");
629 qtest_sendf(chr
, "OK little\n");
632 } else if (strcmp(words
[0], "rtas") == 0) {
633 uint64_t res
, args
, ret
;
634 unsigned long nargs
, nret
;
637 rc
= qemu_strtoul(words
[2], NULL
, 0, &nargs
);
639 rc
= qemu_strtou64(words
[3], NULL
, 0, &args
);
641 rc
= qemu_strtoul(words
[4], NULL
, 0, &nret
);
643 rc
= qemu_strtou64(words
[5], NULL
, 0, &ret
);
645 res
= qtest_rtas_call(words
[1], nargs
, args
, nret
, ret
);
647 qtest_send_prefix(chr
);
648 qtest_sendf(chr
, "OK %"PRIu64
"\n", res
);
650 } else if (qtest_enabled() && strcmp(words
[0], "clock_step") == 0) {
654 int ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
657 ns
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
,
658 QEMU_TIMER_ATTR_ALL
);
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 (strcmp(words
[0], "module_load") == 0) {
665 g_assert(words
[1] && words
[2]);
667 qtest_send_prefix(chr
);
668 if (module_load_one(words
[1], words
[2])) {
669 qtest_sendf(chr
, "OK\n");
671 qtest_sendf(chr
, "FAIL\n");
673 } else if (qtest_enabled() && strcmp(words
[0], "clock_set") == 0) {
678 ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
680 qtest_clock_warp(ns
);
681 qtest_send_prefix(chr
);
682 qtest_sendf(chr
, "OK %"PRIi64
"\n",
683 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
685 qtest_send_prefix(chr
);
686 qtest_sendf(chr
, "FAIL Unknown command '%s'\n", words
[0]);
690 static void qtest_process_inbuf(CharBackend
*chr
, GString
*inbuf
)
694 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
699 offset
= end
- inbuf
->str
;
701 cmd
= g_string_new_len(inbuf
->str
, offset
);
702 g_string_erase(inbuf
, 0, offset
+ 1);
704 words
= g_strsplit(cmd
->str
, " ", 0);
705 qtest_process_command(chr
, words
);
708 g_string_free(cmd
, TRUE
);
712 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
714 CharBackend
*chr
= opaque
;
716 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
717 qtest_process_inbuf(chr
, inbuf
);
720 static int qtest_can_read(void *opaque
)
725 static void qtest_event(void *opaque
, QEMUChrEvent event
)
730 case CHR_EVENT_OPENED
:
732 * We used to call qemu_system_reset() here, hoping we could
733 * use the same process for multiple tests that way. Never
734 * used. Injects an extra reset even when it's not used, and
735 * that can mess up tests, e.g. -boot once.
737 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
740 qemu_gettimeofday(&start_time
);
743 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n",
744 (long) start_time
.tv_sec
, (long) start_time
.tv_usec
);
747 case CHR_EVENT_CLOSED
:
748 qtest_opened
= false;
752 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n",
753 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
760 void qtest_server_init(const char *qtest_chrdev
, const char *qtest_log
, Error
**errp
)
764 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
767 error_setg(errp
, "Failed to initialize device for qtest: \"%s\"",
773 if (strcmp(qtest_log
, "none") != 0) {
774 qtest_log_fp
= fopen(qtest_log
, "w+");
777 qtest_log_fp
= stderr
;
780 qemu_chr_fe_init(&qtest_chr
, chr
, errp
);
781 qemu_chr_fe_set_handlers(&qtest_chr
, qtest_can_read
, qtest_read
,
782 qtest_event
, NULL
, &qtest_chr
, NULL
, true);
783 qemu_chr_fe_set_echo(&qtest_chr
, true);
785 inbuf
= g_string_new("");
788 bool qtest_driver(void)
790 return qtest_chr
.chr
!= NULL
;