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"
16 #include "qemu-common.h"
18 #include "sysemu/qtest.h"
20 #include "sysemu/char.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"
35 static DeviceState
*irq_intercept_dev
;
36 static FILE *qtest_log_fp
;
37 static CharDriverState
*qtest_chr
;
38 static GString
*inbuf
;
39 static int irq_levels
[MAX_IRQ
];
40 static qemu_timeval start_time
;
41 static bool qtest_opened
;
43 #define FMT_timeval "%ld.%06ld"
48 * Line based protocol, request/response based. Server can send async messages
49 * so clients should always handle many async messages before the response
56 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
57 * let you adjust the value of the clock (monotonically). All the commands
58 * return the current value of the clock in nanoseconds.
63 * Advance the clock to the next deadline. Useful when waiting for
64 * asynchronous events.
69 * Advance the clock by NS nanoseconds.
74 * Advance the clock to NS nanoseconds (do nothing if it's already past).
76 * PIO and memory access:
102 * > writel ADDR VALUE
105 * > writeq ADDR VALUE
123 * > write ADDR SIZE DATA
126 * > b64read ADDR SIZE
129 * > b64write ADDR SIZE B64_DATA
132 * > memset ADDR SIZE VALUE
135 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
137 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
138 * than the expected size, the value will be zero filled at the end of the data
141 * B64_DATA is an arbitrarily long base64 encoded string.
142 * If the sizes do not match, the data will be truncated.
146 * > irq_intercept_in QOM-PATH
149 * > irq_intercept_out QOM-PATH
152 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
153 * QOM-PATH. When the pin is triggered, one of the following async messages
154 * will be printed to the qtest stream:
159 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
160 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
161 * NUM=0 even though it is remapped to GSI 2).
164 static int hex2nib(char ch
)
166 if (ch
>= '0' && ch
<= '9') {
168 } else if (ch
>= 'a' && ch
<= 'f') {
169 return 10 + (ch
- 'a');
170 } else if (ch
>= 'A' && ch
<= 'F') {
171 return 10 + (ch
- 'A');
177 static void qtest_get_time(qemu_timeval
*tv
)
179 qemu_gettimeofday(tv
);
180 tv
->tv_sec
-= start_time
.tv_sec
;
181 tv
->tv_usec
-= start_time
.tv_usec
;
182 if (tv
->tv_usec
< 0) {
183 tv
->tv_usec
+= 1000000;
188 static void qtest_send_prefix(CharDriverState
*chr
)
192 if (!qtest_log_fp
|| !qtest_opened
) {
197 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ",
198 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
201 static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt
, ...)
205 if (!qtest_log_fp
|| !qtest_opened
) {
209 qtest_send_prefix(NULL
);
212 vfprintf(qtest_log_fp
, fmt
, ap
);
216 static void do_qtest_send(CharDriverState
*chr
, const char *str
, size_t len
)
218 qemu_chr_fe_write_all(chr
, (uint8_t *)str
, len
);
219 if (qtest_log_fp
&& qtest_opened
) {
220 fprintf(qtest_log_fp
, "%s", str
);
224 static void qtest_send(CharDriverState
*chr
, const char *str
)
226 do_qtest_send(chr
, str
, strlen(str
));
229 static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharDriverState
*chr
,
230 const char *fmt
, ...)
236 buffer
= g_strdup_vprintf(fmt
, ap
);
237 qtest_send(chr
, buffer
);
241 static void qtest_irq_handler(void *opaque
, int n
, int level
)
243 qemu_irq old_irq
= *(qemu_irq
*)opaque
;
244 qemu_set_irq(old_irq
, level
);
246 if (irq_levels
[n
] != level
) {
247 CharDriverState
*chr
= qtest_chr
;
248 irq_levels
[n
] = level
;
249 qtest_send_prefix(chr
);
250 qtest_sendf(chr
, "IRQ %s %d\n",
251 level
? "raise" : "lower", n
);
255 static void qtest_process_command(CharDriverState
*chr
, gchar
**words
)
257 const gchar
*command
;
268 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]",
269 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
270 for (i
= 0; words
[i
]; i
++) {
271 fprintf(qtest_log_fp
, " %s", words
[i
]);
273 fprintf(qtest_log_fp
, "\n");
277 if (strcmp(words
[0], "irq_intercept_out") == 0
278 || strcmp(words
[0], "irq_intercept_in") == 0) {
283 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
285 qtest_send_prefix(chr
);
286 qtest_send(chr
, "FAIL Unknown device\n");
290 if (irq_intercept_dev
) {
291 qtest_send_prefix(chr
);
292 if (irq_intercept_dev
!= dev
) {
293 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
295 qtest_send(chr
, "OK\n");
300 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
301 /* We don't support intercept of named GPIOs yet */
305 if (words
[0][14] == 'o') {
307 for (i
= 0; i
< ngl
->num_out
; ++i
) {
308 qemu_irq
*disconnected
= g_new0(qemu_irq
, 1);
309 qemu_irq icpt
= qemu_allocate_irq(qtest_irq_handler
,
312 *disconnected
= qdev_intercept_gpio_out(dev
, icpt
,
316 qemu_irq_intercept_in(ngl
->in
, qtest_irq_handler
,
320 irq_intercept_dev
= dev
;
321 qtest_send_prefix(chr
);
322 qtest_send(chr
, "OK\n");
324 } else if (strcmp(words
[0], "outb") == 0 ||
325 strcmp(words
[0], "outw") == 0 ||
326 strcmp(words
[0], "outl") == 0) {
330 g_assert(words
[1] && words
[2]);
331 addr
= strtoul(words
[1], NULL
, 0);
332 value
= strtoul(words
[2], NULL
, 0);
334 if (words
[0][3] == 'b') {
335 cpu_outb(addr
, value
);
336 } else if (words
[0][3] == 'w') {
337 cpu_outw(addr
, value
);
338 } else if (words
[0][3] == 'l') {
339 cpu_outl(addr
, value
);
341 qtest_send_prefix(chr
);
342 qtest_send(chr
, "OK\n");
343 } else if (strcmp(words
[0], "inb") == 0 ||
344 strcmp(words
[0], "inw") == 0 ||
345 strcmp(words
[0], "inl") == 0) {
347 uint32_t value
= -1U;
350 addr
= strtoul(words
[1], NULL
, 0);
352 if (words
[0][2] == 'b') {
353 value
= cpu_inb(addr
);
354 } else if (words
[0][2] == 'w') {
355 value
= cpu_inw(addr
);
356 } else if (words
[0][2] == 'l') {
357 value
= cpu_inl(addr
);
359 qtest_send_prefix(chr
);
360 qtest_sendf(chr
, "OK 0x%04x\n", value
);
361 } else if (strcmp(words
[0], "writeb") == 0 ||
362 strcmp(words
[0], "writew") == 0 ||
363 strcmp(words
[0], "writel") == 0 ||
364 strcmp(words
[0], "writeq") == 0) {
368 g_assert(words
[1] && words
[2]);
369 addr
= strtoull(words
[1], NULL
, 0);
370 value
= strtoull(words
[2], NULL
, 0);
372 if (words
[0][5] == 'b') {
373 uint8_t data
= value
;
374 cpu_physical_memory_write(addr
, &data
, 1);
375 } else if (words
[0][5] == 'w') {
376 uint16_t data
= value
;
378 cpu_physical_memory_write(addr
, &data
, 2);
379 } else if (words
[0][5] == 'l') {
380 uint32_t data
= value
;
382 cpu_physical_memory_write(addr
, &data
, 4);
383 } else if (words
[0][5] == 'q') {
384 uint64_t data
= value
;
386 cpu_physical_memory_write(addr
, &data
, 8);
388 qtest_send_prefix(chr
);
389 qtest_send(chr
, "OK\n");
390 } else if (strcmp(words
[0], "readb") == 0 ||
391 strcmp(words
[0], "readw") == 0 ||
392 strcmp(words
[0], "readl") == 0 ||
393 strcmp(words
[0], "readq") == 0) {
395 uint64_t value
= UINT64_C(-1);
398 addr
= strtoull(words
[1], NULL
, 0);
400 if (words
[0][4] == 'b') {
402 cpu_physical_memory_read(addr
, &data
, 1);
404 } else if (words
[0][4] == 'w') {
406 cpu_physical_memory_read(addr
, &data
, 2);
407 value
= tswap16(data
);
408 } else if (words
[0][4] == 'l') {
410 cpu_physical_memory_read(addr
, &data
, 4);
411 value
= tswap32(data
);
412 } else if (words
[0][4] == 'q') {
413 cpu_physical_memory_read(addr
, &value
, 8);
416 qtest_send_prefix(chr
);
417 qtest_sendf(chr
, "OK 0x%016" PRIx64
"\n", value
);
418 } else if (strcmp(words
[0], "read") == 0) {
419 uint64_t addr
, len
, i
;
423 g_assert(words
[1] && words
[2]);
424 addr
= strtoull(words
[1], NULL
, 0);
425 len
= strtoull(words
[2], NULL
, 0);
427 data
= g_malloc(len
);
428 cpu_physical_memory_read(addr
, data
, len
);
430 enc
= g_malloc(2 * len
+ 1);
431 for (i
= 0; i
< len
; i
++) {
432 sprintf(&enc
[i
* 2], "%02x", data
[i
]);
435 qtest_send_prefix(chr
);
436 qtest_sendf(chr
, "OK 0x%s\n", enc
);
440 } else if (strcmp(words
[0], "b64read") == 0) {
445 g_assert(words
[1] && words
[2]);
446 addr
= strtoull(words
[1], NULL
, 0);
447 len
= strtoull(words
[2], NULL
, 0);
449 data
= g_malloc(len
);
450 cpu_physical_memory_read(addr
, data
, len
);
451 b64_data
= g_base64_encode(data
, len
);
452 qtest_send_prefix(chr
);
453 qtest_sendf(chr
, "OK %s\n", b64_data
);
457 } else if (strcmp(words
[0], "write") == 0) {
458 uint64_t addr
, len
, i
;
462 g_assert(words
[1] && words
[2] && words
[3]);
463 addr
= strtoull(words
[1], NULL
, 0);
464 len
= strtoull(words
[2], NULL
, 0);
466 data_len
= strlen(words
[3]);
468 qtest_send(chr
, "ERR invalid argument size\n");
472 data
= g_malloc(len
);
473 for (i
= 0; i
< len
; i
++) {
474 if ((i
* 2 + 4) <= data_len
) {
475 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
476 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
481 cpu_physical_memory_write(addr
, data
, len
);
484 qtest_send_prefix(chr
);
485 qtest_send(chr
, "OK\n");
486 } else if (strcmp(words
[0], "memset") == 0) {
491 g_assert(words
[1] && words
[2] && words
[3]);
492 addr
= strtoull(words
[1], NULL
, 0);
493 len
= strtoull(words
[2], NULL
, 0);
494 pattern
= strtoull(words
[3], NULL
, 0);
496 data
= g_malloc(len
);
497 memset(data
, pattern
, len
);
498 cpu_physical_memory_write(addr
, data
, len
);
501 qtest_send_prefix(chr
);
502 qtest_send(chr
, "OK\n");
503 } else if (strcmp(words
[0], "b64write") == 0) {
509 g_assert(words
[1] && words
[2] && words
[3]);
510 addr
= strtoull(words
[1], NULL
, 0);
511 len
= strtoull(words
[2], NULL
, 0);
513 data_len
= strlen(words
[3]);
515 qtest_send(chr
, "ERR invalid argument size\n");
519 data
= g_base64_decode_inplace(words
[3], &out_len
);
520 if (out_len
!= len
) {
521 qtest_log_send("b64write: data length mismatch (told %"PRIu64
", "
524 out_len
= MIN(out_len
, len
);
527 cpu_physical_memory_write(addr
, data
, out_len
);
529 qtest_send_prefix(chr
);
530 qtest_send(chr
, "OK\n");
531 } else if (qtest_enabled() && strcmp(words
[0], "clock_step") == 0) {
535 ns
= strtoll(words
[1], NULL
, 0);
537 ns
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
539 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + ns
);
540 qtest_send_prefix(chr
);
541 qtest_sendf(chr
, "OK %"PRIi64
"\n",
542 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
543 } else if (qtest_enabled() && strcmp(words
[0], "clock_set") == 0) {
547 ns
= strtoll(words
[1], NULL
, 0);
548 qtest_clock_warp(ns
);
549 qtest_send_prefix(chr
);
550 qtest_sendf(chr
, "OK %"PRIi64
"\n",
551 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
553 qtest_send_prefix(chr
);
554 qtest_sendf(chr
, "FAIL Unknown command '%s'\n", words
[0]);
558 static void qtest_process_inbuf(CharDriverState
*chr
, GString
*inbuf
)
562 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
567 offset
= end
- inbuf
->str
;
569 cmd
= g_string_new_len(inbuf
->str
, offset
);
570 g_string_erase(inbuf
, 0, offset
+ 1);
572 words
= g_strsplit(cmd
->str
, " ", 0);
573 qtest_process_command(chr
, words
);
576 g_string_free(cmd
, TRUE
);
580 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
582 CharDriverState
*chr
= opaque
;
584 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
585 qtest_process_inbuf(chr
, inbuf
);
588 static int qtest_can_read(void *opaque
)
593 static void qtest_event(void *opaque
, int event
)
598 case CHR_EVENT_OPENED
:
600 * We used to call qemu_system_reset() here, hoping we could
601 * use the same process for multiple tests that way. Never
602 * used. Injects an extra reset even when it's not used, and
603 * that can mess up tests, e.g. -boot once.
605 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
608 qemu_gettimeofday(&start_time
);
611 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n",
612 (long) start_time
.tv_sec
, (long) start_time
.tv_usec
);
615 case CHR_EVENT_CLOSED
:
616 qtest_opened
= false;
620 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n",
621 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
629 static int qtest_init_accel(MachineState
*ms
)
631 QemuOpts
*opts
= qemu_opts_create(qemu_find_opts("icount"), NULL
, 0,
633 qemu_opt_set(opts
, "shift", "0", &error_abort
);
634 configure_icount(opts
, &error_abort
);
639 void qtest_init(const char *qtest_chrdev
, const char *qtest_log
, Error
**errp
)
641 CharDriverState
*chr
;
643 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
646 error_setg(errp
, "Failed to initialize device for qtest: \"%s\"",
652 if (strcmp(qtest_log
, "none") != 0) {
653 qtest_log_fp
= fopen(qtest_log
, "w+");
656 qtest_log_fp
= stderr
;
659 qemu_chr_add_handlers(chr
, qtest_can_read
, qtest_read
, qtest_event
, chr
);
660 qemu_chr_fe_set_echo(chr
, true);
662 inbuf
= g_string_new("");
666 bool qtest_driver(void)
671 static void qtest_accel_class_init(ObjectClass
*oc
, void *data
)
673 AccelClass
*ac
= ACCEL_CLASS(oc
);
675 ac
->available
= qtest_available
;
676 ac
->init_machine
= qtest_init_accel
;
677 ac
->allowed
= &qtest_allowed
;
680 #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
682 static const TypeInfo qtest_accel_type
= {
683 .name
= TYPE_QTEST_ACCEL
,
684 .parent
= TYPE_ACCEL
,
685 .class_init
= qtest_accel_class_init
,
688 static void qtest_type_init(void)
690 type_register_static(&qtest_accel_type
);
693 type_init(qtest_type_init
);