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 "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.
136 * For 'memset' a zero size is permitted and does nothing.
138 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
139 * than the expected size, the value will be zero filled at the end of the data
142 * B64_DATA is an arbitrarily long base64 encoded string.
143 * If the sizes do not match, the data will be truncated.
147 * > irq_intercept_in QOM-PATH
150 * > irq_intercept_out QOM-PATH
153 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
154 * QOM-PATH. When the pin is triggered, one of the following async messages
155 * will be printed to the qtest stream:
160 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
161 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
162 * NUM=0 even though it is remapped to GSI 2).
165 static int hex2nib(char ch)
167 if (ch >= '0' && ch <= '9') {
169 } else if (ch >= 'a' && ch <= 'f') {
170 return 10 + (ch - 'a');
171 } else if (ch >= 'A' && ch <= 'F') {
172 return 10 + (ch - 'A');
178 static void qtest_get_time(qemu_timeval *tv)
180 qemu_gettimeofday(tv);
181 tv->tv_sec -= start_time.tv_sec;
182 tv->tv_usec -= start_time.tv_usec;
183 if (tv->tv_usec < 0) {
184 tv->tv_usec += 1000000;
189 static void qtest_send_prefix(CharDriverState *chr)
193 if (!qtest_log_fp || !qtest_opened) {
198 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
199 (long) tv.tv_sec, (long) tv.tv_usec);
202 static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
206 if (!qtest_log_fp || !qtest_opened) {
210 qtest_send_prefix(NULL);
213 vfprintf(qtest_log_fp, fmt, ap);
217 static void do_qtest_send(CharDriverState *chr, const char *str, size_t len)
219 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
220 if (qtest_log_fp && qtest_opened) {
221 fprintf(qtest_log_fp, "%s", str);
225 static void qtest_send(CharDriverState *chr, const char *str)
227 do_qtest_send(chr, str, strlen(str));
230 static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharDriverState *chr,
231 const char *fmt, ...)
237 buffer = g_strdup_vprintf(fmt, ap);
238 qtest_send(chr, buffer);
242 static void qtest_irq_handler(void *opaque, int n, int level)
244 qemu_irq old_irq = *(qemu_irq *)opaque;
245 qemu_set_irq(old_irq, level);
247 if (irq_levels[n] != level) {
248 CharDriverState *chr = qtest_chr;
249 irq_levels[n] = level;
250 qtest_send_prefix(chr);
251 qtest_sendf(chr, "IRQ %s %d\n",
252 level ? "raise" : "lower", n);
256 static void qtest_process_command(CharDriverState *chr, gchar **words)
258 const gchar *command;
269 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
270 (long) tv.tv_sec, (long) tv.tv_usec);
271 for (i = 0; words[i]; i++) {
272 fprintf(qtest_log_fp, " %s", words[i]);
274 fprintf(qtest_log_fp, "\n");
278 if (strcmp(words[0], "irq_intercept_out") == 0
279 || strcmp(words[0], "irq_intercept_in") == 0) {
284 dev = DEVICE(object_resolve_path(words[1], NULL));
286 qtest_send_prefix(chr);
287 qtest_send(chr, "FAIL Unknown device\n");
291 if (irq_intercept_dev) {
292 qtest_send_prefix(chr);
293 if (irq_intercept_dev != dev) {
294 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
296 qtest_send(chr, "OK\n");
301 QLIST_FOREACH(ngl, &dev->gpios, node) {
302 /* We don't support intercept of named GPIOs yet */
306 if (words[0][14] == 'o') {
308 for (i = 0; i < ngl->num_out; ++i) {
309 qemu_irq *disconnected = g_new0(qemu_irq, 1);
310 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
313 *disconnected = qdev_intercept_gpio_out(dev, icpt,
317 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
321 irq_intercept_dev = dev;
322 qtest_send_prefix(chr);
323 qtest_send(chr, "OK\n");
325 } else if (strcmp(words[0], "outb") == 0 ||
326 strcmp(words[0], "outw") == 0 ||
327 strcmp(words[0], "outl") == 0) {
331 g_assert(words[1] && words[2]);
332 addr = strtoul(words[1], NULL, 0);
333 value = strtoul(words[2], NULL, 0);
335 if (words[0][3] == 'b') {
336 cpu_outb(addr, value);
337 } else if (words[0][3] == 'w') {
338 cpu_outw(addr, value);
339 } else if (words[0][3] == 'l') {
340 cpu_outl(addr, value);
342 qtest_send_prefix(chr);
343 qtest_send(chr, "OK\n");
344 } else if (strcmp(words[0], "inb") == 0 ||
345 strcmp(words[0], "inw") == 0 ||
346 strcmp(words[0], "inl") == 0) {
348 uint32_t value = -1U;
351 addr = strtoul(words[1], NULL, 0);
353 if (words[0][2] == 'b') {
354 value = cpu_inb(addr);
355 } else if (words[0][2] == 'w') {
356 value = cpu_inw(addr);
357 } else if (words[0][2] == 'l') {
358 value = cpu_inl(addr);
360 qtest_send_prefix(chr);
361 qtest_sendf(chr, "OK 0x%04x\n", value);
362 } else if (strcmp(words[0], "writeb") == 0 ||
363 strcmp(words[0], "writew") == 0 ||
364 strcmp(words[0], "writel") == 0 ||
365 strcmp(words[0], "writeq") == 0) {
369 g_assert(words[1] && words[2]);
370 addr = strtoull(words[1], NULL, 0);
371 value = strtoull(words[2], NULL, 0);
373 if (words[0][5] == 'b') {
374 uint8_t data = value;
375 cpu_physical_memory_write(addr, &data, 1);
376 } else if (words[0][5] == 'w') {
377 uint16_t data = value;
379 cpu_physical_memory_write(addr, &data, 2);
380 } else if (words[0][5] == 'l') {
381 uint32_t data = value;
383 cpu_physical_memory_write(addr, &data, 4);
384 } else if (words[0][5] == 'q') {
385 uint64_t data = value;
387 cpu_physical_memory_write(addr, &data, 8);
389 qtest_send_prefix(chr);
390 qtest_send(chr, "OK\n");
391 } else if (strcmp(words[0], "readb") == 0 ||
392 strcmp(words[0], "readw") == 0 ||
393 strcmp(words[0], "readl") == 0 ||
394 strcmp(words[0], "readq") == 0) {
396 uint64_t value = UINT64_C(-1);
399 addr = strtoull(words[1], NULL, 0);
401 if (words[0][4] == 'b') {
403 cpu_physical_memory_read(addr, &data, 1);
405 } else if (words[0][4] == 'w') {
407 cpu_physical_memory_read(addr, &data, 2);
408 value = tswap16(data);
409 } else if (words[0][4] == 'l') {
411 cpu_physical_memory_read(addr, &data, 4);
412 value = tswap32(data);
413 } else if (words[0][4] == 'q') {
414 cpu_physical_memory_read(addr, &value, 8);
417 qtest_send_prefix(chr);
418 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
419 } else if (strcmp(words[0], "read") == 0) {
420 uint64_t addr, len, i;
424 g_assert(words[1] && words[2]);
425 addr = strtoull(words[1], NULL, 0);
426 len = strtoull(words[2], NULL, 0);
428 data = g_malloc(len);
429 cpu_physical_memory_read(addr, data, len);
431 enc = g_malloc(2 * len + 1);
432 for (i = 0; i < len; i++) {
433 sprintf(&enc[i * 2], "%02x", data[i]);
436 qtest_send_prefix(chr);
437 qtest_sendf(chr, "OK 0x%s\n", enc);
441 } else if (strcmp(words[0], "b64read") == 0) {
446 g_assert(words[1] && words[2]);
447 addr = strtoull(words[1], NULL, 0);
448 len = strtoull(words[2], NULL, 0);
450 data = g_malloc(len);
451 cpu_physical_memory_read(addr, data, len);
452 b64_data = g_base64_encode(data, len);
453 qtest_send_prefix(chr);
454 qtest_sendf(chr, "OK %s\n", b64_data);
458 } else if (strcmp(words[0], "write") == 0) {
459 uint64_t addr, len, i;
463 g_assert(words[1] && words[2] && words[3]);
464 addr = strtoull(words[1], NULL, 0);
465 len = strtoull(words[2], NULL, 0);
467 data_len = strlen(words[3]);
469 qtest_send(chr, "ERR invalid argument size\n");
473 data = g_malloc(len);
474 for (i = 0; i < len; i++) {
475 if ((i * 2 + 4) <= data_len) {
476 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
477 data[i] |= hex2nib(words[3][i * 2 + 3]);
482 cpu_physical_memory_write(addr, data, len);
485 qtest_send_prefix(chr);
486 qtest_send(chr, "OK\n");
487 } else if (strcmp(words[0], "memset") == 0) {
492 g_assert(words[1] && words[2] && words[3]);
493 addr = strtoull(words[1], NULL, 0);
494 len = strtoull(words[2], NULL, 0);
495 pattern = strtoull(words[3], NULL, 0);
498 data = g_malloc(len);
499 memset(data, pattern, len);
500 cpu_physical_memory_write(addr, data, len);
504 qtest_send_prefix(chr);
505 qtest_send(chr, "OK\n");
506 } else if (strcmp(words[0], "b64write") == 0) {
512 g_assert(words[1] && words[2] && words[3]);
513 addr = strtoull(words[1], NULL, 0);
514 len = strtoull(words[2], NULL, 0);
516 data_len = strlen(words[3]);
518 qtest_send(chr, "ERR invalid argument size\n");
522 data = g_base64_decode_inplace(words[3], &out_len);
523 if (out_len != len) {
524 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
527 out_len = MIN(out_len, len);
530 cpu_physical_memory_write(addr, data, out_len);
532 qtest_send_prefix(chr);
533 qtest_send(chr, "OK\n");
534 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
538 ns = strtoll(words[1], NULL, 0);
540 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
542 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
543 qtest_send_prefix(chr);
544 qtest_sendf(chr, "OK %"PRIi64"\n",
545 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
546 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
550 ns = strtoll(words[1], NULL, 0);
551 qtest_clock_warp(ns);
552 qtest_send_prefix(chr);
553 qtest_sendf(chr, "OK %"PRIi64"\n",
554 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
556 qtest_send_prefix(chr);
557 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
561 static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
565 while ((end = strchr(inbuf->str, '\n')) != NULL) {
570 offset = end - inbuf->str;
572 cmd = g_string_new_len(inbuf->str, offset);
573 g_string_erase(inbuf, 0, offset + 1);
575 words = g_strsplit(cmd->str, " ", 0);
576 qtest_process_command(chr, words);
579 g_string_free(cmd, TRUE);
583 static void qtest_read(void *opaque, const uint8_t *buf, int size)
585 CharDriverState *chr = opaque;
587 g_string_append_len(inbuf, (const gchar *)buf, size);
588 qtest_process_inbuf(chr, inbuf);
591 static int qtest_can_read(void *opaque)
596 static void qtest_event(void *opaque, int event)
601 case CHR_EVENT_OPENED:
603 * We used to call qemu_system_reset() here, hoping we could
604 * use the same process for multiple tests that way. Never
605 * used. Injects an extra reset even when it's not used, and
606 * that can mess up tests, e.g. -boot once.
608 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
611 qemu_gettimeofday(&start_time);
614 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
615 (long) start_time.tv_sec, (long) start_time.tv_usec);
618 case CHR_EVENT_CLOSED:
619 qtest_opened = false;
623 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
624 (long) tv.tv_sec, (long) tv.tv_usec);
632 static int qtest_init_accel(MachineState *ms)
634 QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
636 qemu_opt_set(opts, "shift", "0", &error_abort);
637 configure_icount(opts, &error_abort);
642 void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
644 CharDriverState *chr;
646 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
649 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
655 if (strcmp(qtest_log, "none") != 0) {
656 qtest_log_fp = fopen(qtest_log, "w+");
659 qtest_log_fp = stderr;
662 qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
663 qemu_chr_fe_set_echo(chr, true);
665 inbuf = g_string_new("");
669 bool qtest_driver(void)
674 static void qtest_accel_class_init(ObjectClass *oc, void *data)
676 AccelClass *ac = ACCEL_CLASS(oc);
678 ac->available = qtest_available;
679 ac->init_machine = qtest_init_accel;
680 ac->allowed = &qtest_allowed;
683 #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
685 static const TypeInfo qtest_accel_type = {
686 .name = TYPE_QTEST_ACCEL,
687 .parent = TYPE_ACCEL,
688 .class_init = qtest_accel_class_init,
691 static void qtest_type_init(void)
693 type_register_static(&qtest_accel_type);
696 type_init(qtest_type_init);