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 "sysemu/qtest.h"
18 #include "sysemu/char.h"
19 #include "exec/ioport.h"
20 #include "exec/memory.h"
22 #include "sysemu/accel.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/cpus.h"
25 #include "qemu/config-file.h"
26 #include "qemu/option.h"
27 #include "qemu/error-report.h"
33 static DeviceState
*irq_intercept_dev
;
34 static FILE *qtest_log_fp
;
35 static CharDriverState
*qtest_chr
;
36 static GString
*inbuf
;
37 static int irq_levels
[MAX_IRQ
];
38 static qemu_timeval start_time
;
39 static bool qtest_opened
;
41 #define FMT_timeval "%ld.%06ld"
46 * Line based protocol, request/response based. Server can send async messages
47 * so clients should always handle many async messages before the response
54 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
55 * let you adjust the value of the clock (monotonically). All the commands
56 * return the current value of the clock in nanoseconds.
61 * Advance the clock to the next deadline. Useful when waiting for
62 * asynchronous events.
67 * Advance the clock by NS nanoseconds.
72 * Advance the clock to NS nanoseconds (do nothing if it's already past).
74 * PIO and memory access:
100 * > writel ADDR VALUE
103 * > writeq ADDR VALUE
121 * > write ADDR SIZE DATA
124 * > b64read ADDR SIZE
127 * > b64write ADDR SIZE B64_DATA
130 * > memset ADDR SIZE VALUE
133 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
135 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
136 * than the expected size, the value will be zero filled at the end of the data
139 * B64_DATA is an arbitrarily long base64 encoded string.
140 * If the sizes do not match, the data will be truncated.
144 * > irq_intercept_in QOM-PATH
147 * > irq_intercept_out QOM-PATH
150 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
151 * QOM-PATH. When the pin is triggered, one of the following async messages
152 * will be printed to the qtest stream:
157 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
158 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
159 * NUM=0 even though it is remapped to GSI 2).
162 static int hex2nib(char ch
)
164 if (ch
>= '0' && ch
<= '9') {
166 } else if (ch
>= 'a' && ch
<= 'f') {
167 return 10 + (ch
- 'a');
168 } else if (ch
>= 'A' && ch
<= 'F') {
169 return 10 + (ch
- 'A');
175 static void qtest_get_time(qemu_timeval
*tv
)
177 qemu_gettimeofday(tv
);
178 tv
->tv_sec
-= start_time
.tv_sec
;
179 tv
->tv_usec
-= start_time
.tv_usec
;
180 if (tv
->tv_usec
< 0) {
181 tv
->tv_usec
+= 1000000;
186 static void qtest_send_prefix(CharDriverState
*chr
)
190 if (!qtest_log_fp
|| !qtest_opened
) {
195 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ",
196 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
199 static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt
, ...)
203 if (!qtest_log_fp
|| !qtest_opened
) {
207 qtest_send_prefix(NULL
);
210 vfprintf(qtest_log_fp
, fmt
, ap
);
214 static void do_qtest_send(CharDriverState
*chr
, const char *str
, size_t len
)
216 qemu_chr_fe_write_all(chr
, (uint8_t *)str
, len
);
217 if (qtest_log_fp
&& qtest_opened
) {
218 fprintf(qtest_log_fp
, "%s", str
);
222 static void qtest_send(CharDriverState
*chr
, const char *str
)
224 do_qtest_send(chr
, str
, strlen(str
));
227 static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharDriverState
*chr
,
228 const char *fmt
, ...)
234 buffer
= g_strdup_vprintf(fmt
, ap
);
235 qtest_send(chr
, buffer
);
239 static void qtest_irq_handler(void *opaque
, int n
, int level
)
241 qemu_irq old_irq
= *(qemu_irq
*)opaque
;
242 qemu_set_irq(old_irq
, level
);
244 if (irq_levels
[n
] != level
) {
245 CharDriverState
*chr
= qtest_chr
;
246 irq_levels
[n
] = level
;
247 qtest_send_prefix(chr
);
248 qtest_sendf(chr
, "IRQ %s %d\n",
249 level
? "raise" : "lower", n
);
253 static void qtest_process_command(CharDriverState
*chr
, gchar
**words
)
255 const gchar
*command
;
266 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]",
267 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
268 for (i
= 0; words
[i
]; i
++) {
269 fprintf(qtest_log_fp
, " %s", words
[i
]);
271 fprintf(qtest_log_fp
, "\n");
275 if (strcmp(words
[0], "irq_intercept_out") == 0
276 || strcmp(words
[0], "irq_intercept_in") == 0) {
281 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
283 qtest_send_prefix(chr
);
284 qtest_send(chr
, "FAIL Unknown device\n");
288 if (irq_intercept_dev
) {
289 qtest_send_prefix(chr
);
290 if (irq_intercept_dev
!= dev
) {
291 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
293 qtest_send(chr
, "OK\n");
298 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
299 /* We don't support intercept of named GPIOs yet */
303 if (words
[0][14] == 'o') {
305 for (i
= 0; i
< ngl
->num_out
; ++i
) {
306 qemu_irq
*disconnected
= g_new0(qemu_irq
, 1);
307 qemu_irq icpt
= qemu_allocate_irq(qtest_irq_handler
,
310 *disconnected
= qdev_intercept_gpio_out(dev
, icpt
,
314 qemu_irq_intercept_in(ngl
->in
, qtest_irq_handler
,
318 irq_intercept_dev
= dev
;
319 qtest_send_prefix(chr
);
320 qtest_send(chr
, "OK\n");
322 } else if (strcmp(words
[0], "outb") == 0 ||
323 strcmp(words
[0], "outw") == 0 ||
324 strcmp(words
[0], "outl") == 0) {
328 g_assert(words
[1] && words
[2]);
329 addr
= strtoul(words
[1], NULL
, 0);
330 value
= strtoul(words
[2], NULL
, 0);
332 if (words
[0][3] == 'b') {
333 cpu_outb(addr
, value
);
334 } else if (words
[0][3] == 'w') {
335 cpu_outw(addr
, value
);
336 } else if (words
[0][3] == 'l') {
337 cpu_outl(addr
, value
);
339 qtest_send_prefix(chr
);
340 qtest_send(chr
, "OK\n");
341 } else if (strcmp(words
[0], "inb") == 0 ||
342 strcmp(words
[0], "inw") == 0 ||
343 strcmp(words
[0], "inl") == 0) {
345 uint32_t value
= -1U;
348 addr
= strtoul(words
[1], NULL
, 0);
350 if (words
[0][2] == 'b') {
351 value
= cpu_inb(addr
);
352 } else if (words
[0][2] == 'w') {
353 value
= cpu_inw(addr
);
354 } else if (words
[0][2] == 'l') {
355 value
= cpu_inl(addr
);
357 qtest_send_prefix(chr
);
358 qtest_sendf(chr
, "OK 0x%04x\n", value
);
359 } else if (strcmp(words
[0], "writeb") == 0 ||
360 strcmp(words
[0], "writew") == 0 ||
361 strcmp(words
[0], "writel") == 0 ||
362 strcmp(words
[0], "writeq") == 0) {
366 g_assert(words
[1] && words
[2]);
367 addr
= strtoull(words
[1], NULL
, 0);
368 value
= strtoull(words
[2], NULL
, 0);
370 if (words
[0][5] == 'b') {
371 uint8_t data
= value
;
372 cpu_physical_memory_write(addr
, &data
, 1);
373 } else if (words
[0][5] == 'w') {
374 uint16_t data
= value
;
376 cpu_physical_memory_write(addr
, &data
, 2);
377 } else if (words
[0][5] == 'l') {
378 uint32_t data
= value
;
380 cpu_physical_memory_write(addr
, &data
, 4);
381 } else if (words
[0][5] == 'q') {
382 uint64_t data
= value
;
384 cpu_physical_memory_write(addr
, &data
, 8);
386 qtest_send_prefix(chr
);
387 qtest_send(chr
, "OK\n");
388 } else if (strcmp(words
[0], "readb") == 0 ||
389 strcmp(words
[0], "readw") == 0 ||
390 strcmp(words
[0], "readl") == 0 ||
391 strcmp(words
[0], "readq") == 0) {
393 uint64_t value
= UINT64_C(-1);
396 addr
= strtoull(words
[1], NULL
, 0);
398 if (words
[0][4] == 'b') {
400 cpu_physical_memory_read(addr
, &data
, 1);
402 } else if (words
[0][4] == 'w') {
404 cpu_physical_memory_read(addr
, &data
, 2);
405 value
= tswap16(data
);
406 } else if (words
[0][4] == 'l') {
408 cpu_physical_memory_read(addr
, &data
, 4);
409 value
= tswap32(data
);
410 } else if (words
[0][4] == 'q') {
411 cpu_physical_memory_read(addr
, &value
, 8);
414 qtest_send_prefix(chr
);
415 qtest_sendf(chr
, "OK 0x%016" PRIx64
"\n", value
);
416 } else if (strcmp(words
[0], "read") == 0) {
417 uint64_t addr
, len
, i
;
421 g_assert(words
[1] && words
[2]);
422 addr
= strtoull(words
[1], NULL
, 0);
423 len
= strtoull(words
[2], NULL
, 0);
425 data
= g_malloc(len
);
426 cpu_physical_memory_read(addr
, data
, len
);
428 enc
= g_malloc(2 * len
+ 1);
429 for (i
= 0; i
< len
; i
++) {
430 sprintf(&enc
[i
* 2], "%02x", data
[i
]);
433 qtest_send_prefix(chr
);
434 qtest_sendf(chr
, "OK 0x%s\n", enc
);
438 } else if (strcmp(words
[0], "b64read") == 0) {
443 g_assert(words
[1] && words
[2]);
444 addr
= strtoull(words
[1], NULL
, 0);
445 len
= strtoull(words
[2], NULL
, 0);
447 data
= g_malloc(len
);
448 cpu_physical_memory_read(addr
, data
, len
);
449 b64_data
= g_base64_encode(data
, len
);
450 qtest_send_prefix(chr
);
451 qtest_sendf(chr
, "OK %s\n", b64_data
);
455 } else if (strcmp(words
[0], "write") == 0) {
456 uint64_t addr
, len
, i
;
460 g_assert(words
[1] && words
[2] && words
[3]);
461 addr
= strtoull(words
[1], NULL
, 0);
462 len
= strtoull(words
[2], NULL
, 0);
464 data_len
= strlen(words
[3]);
466 qtest_send(chr
, "ERR invalid argument size\n");
470 data
= g_malloc(len
);
471 for (i
= 0; i
< len
; i
++) {
472 if ((i
* 2 + 4) <= data_len
) {
473 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
474 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
479 cpu_physical_memory_write(addr
, data
, len
);
482 qtest_send_prefix(chr
);
483 qtest_send(chr
, "OK\n");
484 } else if (strcmp(words
[0], "memset") == 0) {
489 g_assert(words
[1] && words
[2] && words
[3]);
490 addr
= strtoull(words
[1], NULL
, 0);
491 len
= strtoull(words
[2], NULL
, 0);
492 pattern
= strtoull(words
[3], NULL
, 0);
494 data
= g_malloc(len
);
495 memset(data
, pattern
, len
);
496 cpu_physical_memory_write(addr
, data
, len
);
499 qtest_send_prefix(chr
);
500 qtest_send(chr
, "OK\n");
501 } else if (strcmp(words
[0], "b64write") == 0) {
507 g_assert(words
[1] && words
[2] && words
[3]);
508 addr
= strtoull(words
[1], NULL
, 0);
509 len
= strtoull(words
[2], NULL
, 0);
511 data_len
= strlen(words
[3]);
513 qtest_send(chr
, "ERR invalid argument size\n");
517 data
= g_base64_decode_inplace(words
[3], &out_len
);
518 if (out_len
!= len
) {
519 qtest_log_send("b64write: data length mismatch (told %"PRIu64
", "
522 out_len
= MIN(out_len
, len
);
525 cpu_physical_memory_write(addr
, data
, out_len
);
527 qtest_send_prefix(chr
);
528 qtest_send(chr
, "OK\n");
529 } else if (qtest_enabled() && strcmp(words
[0], "clock_step") == 0) {
533 ns
= strtoll(words
[1], NULL
, 0);
535 ns
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
537 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + ns
);
538 qtest_send_prefix(chr
);
539 qtest_sendf(chr
, "OK %"PRIi64
"\n",
540 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
541 } else if (qtest_enabled() && strcmp(words
[0], "clock_set") == 0) {
545 ns
= strtoll(words
[1], NULL
, 0);
546 qtest_clock_warp(ns
);
547 qtest_send_prefix(chr
);
548 qtest_sendf(chr
, "OK %"PRIi64
"\n",
549 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
551 qtest_send_prefix(chr
);
552 qtest_sendf(chr
, "FAIL Unknown command '%s'\n", words
[0]);
556 static void qtest_process_inbuf(CharDriverState
*chr
, GString
*inbuf
)
560 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
565 offset
= end
- inbuf
->str
;
567 cmd
= g_string_new_len(inbuf
->str
, offset
);
568 g_string_erase(inbuf
, 0, offset
+ 1);
570 words
= g_strsplit(cmd
->str
, " ", 0);
571 qtest_process_command(chr
, words
);
574 g_string_free(cmd
, TRUE
);
578 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
580 CharDriverState
*chr
= opaque
;
582 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
583 qtest_process_inbuf(chr
, inbuf
);
586 static int qtest_can_read(void *opaque
)
591 static void qtest_event(void *opaque
, int event
)
596 case CHR_EVENT_OPENED
:
598 * We used to call qemu_system_reset() here, hoping we could
599 * use the same process for multiple tests that way. Never
600 * used. Injects an extra reset even when it's not used, and
601 * that can mess up tests, e.g. -boot once.
603 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
606 qemu_gettimeofday(&start_time
);
609 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n",
610 (long) start_time
.tv_sec
, (long) start_time
.tv_usec
);
613 case CHR_EVENT_CLOSED
:
614 qtest_opened
= false;
618 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n",
619 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
627 static int qtest_init_accel(MachineState
*ms
)
629 QemuOpts
*opts
= qemu_opts_create(qemu_find_opts("icount"), NULL
, 0,
631 qemu_opt_set(opts
, "shift", "0", &error_abort
);
632 configure_icount(opts
, &error_abort
);
637 void qtest_init(const char *qtest_chrdev
, const char *qtest_log
, Error
**errp
)
639 CharDriverState
*chr
;
641 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
644 error_setg(errp
, "Failed to initialize device for qtest: \"%s\"",
650 if (strcmp(qtest_log
, "none") != 0) {
651 qtest_log_fp
= fopen(qtest_log
, "w+");
654 qtest_log_fp
= stderr
;
657 qemu_chr_add_handlers(chr
, qtest_can_read
, qtest_read
, qtest_event
, chr
);
658 qemu_chr_fe_set_echo(chr
, true);
660 inbuf
= g_string_new("");
664 bool qtest_driver(void)
669 static void qtest_accel_class_init(ObjectClass
*oc
, void *data
)
671 AccelClass
*ac
= ACCEL_CLASS(oc
);
673 ac
->available
= qtest_available
;
674 ac
->init_machine
= qtest_init_accel
;
675 ac
->allowed
= &qtest_allowed
;
678 #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
680 static const TypeInfo qtest_accel_type
= {
681 .name
= TYPE_QTEST_ACCEL
,
682 .parent
= TYPE_ACCEL
,
683 .class_init
= qtest_accel_class_init
,
686 static void qtest_type_init(void)
688 type_register_static(&qtest_accel_type
);
691 type_init(qtest_type_init
);