cutils: Add generic prefetch
[qemu/ar7.git] / qtest.c
blobce4c6dbbf9248a7f5780be20b10ad13489f7ed4f
1 /*
2 * Test Server
4 * Copyright IBM, Corp. 2011
6 * Authors:
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"
17 #include "cpu.h"
18 #include "sysemu/qtest.h"
19 #include "hw/qdev.h"
20 #include "sysemu/char.h"
21 #include "exec/ioport.h"
22 #include "exec/memory.h"
23 #include "hw/irq.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"
31 #define MAX_IRQ 256
33 bool qtest_allowed;
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"
45 /**
46 * QTest Protocol
48 * Line based protocol, request/response based. Server can send async messages
49 * so clients should always handle many async messages before the response
50 * comes in.
52 * Valid requests
54 * Clock management:
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.
60 * > clock_step
61 * < OK VALUE
63 * Advance the clock to the next deadline. Useful when waiting for
64 * asynchronous events.
66 * > clock_step NS
67 * < OK VALUE
69 * Advance the clock by NS nanoseconds.
71 * > clock_set NS
72 * < OK VALUE
74 * Advance the clock to NS nanoseconds (do nothing if it's already past).
76 * PIO and memory access:
78 * > outb ADDR VALUE
79 * < OK
81 * > outw ADDR VALUE
82 * < OK
84 * > outl ADDR VALUE
85 * < OK
87 * > inb ADDR
88 * < OK VALUE
90 * > inw ADDR
91 * < OK VALUE
93 * > inl ADDR
94 * < OK VALUE
96 * > writeb ADDR VALUE
97 * < OK
99 * > writew ADDR VALUE
100 * < OK
102 * > writel ADDR VALUE
103 * < OK
105 * > writeq ADDR VALUE
106 * < OK
108 * > readb ADDR
109 * < OK VALUE
111 * > readw ADDR
112 * < OK VALUE
114 * > readl ADDR
115 * < OK VALUE
117 * > readq ADDR
118 * < OK VALUE
120 * > read ADDR SIZE
121 * < OK DATA
123 * > write ADDR SIZE DATA
124 * < OK
126 * > b64read ADDR SIZE
127 * < OK B64_DATA
129 * > b64write ADDR SIZE B64_DATA
130 * < OK
132 * > memset ADDR SIZE VALUE
133 * < OK
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
140 * sequence.
142 * B64_DATA is an arbitrarily long base64 encoded string.
143 * If the sizes do not match, the data will be truncated.
145 * IRQ management:
147 * > irq_intercept_in QOM-PATH
148 * < OK
150 * > irq_intercept_out QOM-PATH
151 * < OK
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:
157 * IRQ raise NUM
158 * IRQ lower NUM
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') {
168 return ch - '0';
169 } else if (ch >= 'a' && ch <= 'f') {
170 return 10 + (ch - 'a');
171 } else if (ch >= 'A' && ch <= 'F') {
172 return 10 + (ch - 'A');
173 } else {
174 return -1;
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;
185 tv->tv_sec -= 1;
189 static void qtest_send_prefix(CharDriverState *chr)
191 qemu_timeval tv;
193 if (!qtest_log_fp || !qtest_opened) {
194 return;
197 qtest_get_time(&tv);
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, ...)
204 va_list ap;
206 if (!qtest_log_fp || !qtest_opened) {
207 return;
210 qtest_send_prefix(NULL);
212 va_start(ap, fmt);
213 vfprintf(qtest_log_fp, fmt, ap);
214 va_end(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, ...)
233 va_list ap;
234 gchar *buffer;
236 va_start(ap, fmt);
237 buffer = g_strdup_vprintf(fmt, ap);
238 qtest_send(chr, buffer);
239 va_end(ap);
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;
260 g_assert(words);
262 command = words[0];
264 if (qtest_log_fp) {
265 qemu_timeval tv;
266 int i;
268 qtest_get_time(&tv);
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");
277 g_assert(command);
278 if (strcmp(words[0], "irq_intercept_out") == 0
279 || strcmp(words[0], "irq_intercept_in") == 0) {
280 DeviceState *dev;
281 NamedGPIOList *ngl;
283 g_assert(words[1]);
284 dev = DEVICE(object_resolve_path(words[1], NULL));
285 if (!dev) {
286 qtest_send_prefix(chr);
287 qtest_send(chr, "FAIL Unknown device\n");
288 return;
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");
295 } else {
296 qtest_send(chr, "OK\n");
298 return;
301 QLIST_FOREACH(ngl, &dev->gpios, node) {
302 /* We don't support intercept of named GPIOs yet */
303 if (ngl->name) {
304 continue;
306 if (words[0][14] == 'o') {
307 int i;
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,
311 disconnected, i);
313 *disconnected = qdev_intercept_gpio_out(dev, icpt,
314 ngl->name, i);
316 } else {
317 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
318 ngl->num_in);
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) {
328 uint16_t addr;
329 uint32_t value;
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) {
347 uint16_t addr;
348 uint32_t value = -1U;
350 g_assert(words[1]);
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) {
366 uint64_t addr;
367 uint64_t value;
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;
378 tswap16s(&data);
379 cpu_physical_memory_write(addr, &data, 2);
380 } else if (words[0][5] == 'l') {
381 uint32_t data = value;
382 tswap32s(&data);
383 cpu_physical_memory_write(addr, &data, 4);
384 } else if (words[0][5] == 'q') {
385 uint64_t data = value;
386 tswap64s(&data);
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) {
395 uint64_t addr;
396 uint64_t value = UINT64_C(-1);
398 g_assert(words[1]);
399 addr = strtoull(words[1], NULL, 0);
401 if (words[0][4] == 'b') {
402 uint8_t data;
403 cpu_physical_memory_read(addr, &data, 1);
404 value = data;
405 } else if (words[0][4] == 'w') {
406 uint16_t data;
407 cpu_physical_memory_read(addr, &data, 2);
408 value = tswap16(data);
409 } else if (words[0][4] == 'l') {
410 uint32_t data;
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);
415 tswap64s(&value);
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;
421 uint8_t *data;
422 char *enc;
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);
439 g_free(data);
440 g_free(enc);
441 } else if (strcmp(words[0], "b64read") == 0) {
442 uint64_t addr, len;
443 uint8_t *data;
444 gchar *b64_data;
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);
456 g_free(data);
457 g_free(b64_data);
458 } else if (strcmp(words[0], "write") == 0) {
459 uint64_t addr, len, i;
460 uint8_t *data;
461 size_t data_len;
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]);
468 if (data_len < 3) {
469 qtest_send(chr, "ERR invalid argument size\n");
470 return;
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]);
478 } else {
479 data[i] = 0;
482 cpu_physical_memory_write(addr, data, len);
483 g_free(data);
485 qtest_send_prefix(chr);
486 qtest_send(chr, "OK\n");
487 } else if (strcmp(words[0], "memset") == 0) {
488 uint64_t addr, len;
489 uint8_t *data;
490 uint8_t pattern;
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);
497 if (len) {
498 data = g_malloc(len);
499 memset(data, pattern, len);
500 cpu_physical_memory_write(addr, data, len);
501 g_free(data);
504 qtest_send_prefix(chr);
505 qtest_send(chr, "OK\n");
506 } else if (strcmp(words[0], "b64write") == 0) {
507 uint64_t addr, len;
508 uint8_t *data;
509 size_t data_len;
510 gsize out_len;
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]);
517 if (data_len < 3) {
518 qtest_send(chr, "ERR invalid argument size\n");
519 return;
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", "
525 "found %zu)\n",
526 len, out_len);
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) {
535 int64_t ns;
537 if (words[1]) {
538 ns = strtoll(words[1], NULL, 0);
539 } else {
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) {
547 int64_t ns;
549 g_assert(words[1]);
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));
555 } else {
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)
563 char *end;
565 while ((end = strchr(inbuf->str, '\n')) != NULL) {
566 size_t offset;
567 GString *cmd;
568 gchar **words;
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);
577 g_strfreev(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)
593 return 1024;
596 static void qtest_event(void *opaque, int event)
598 int i;
600 switch (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++) {
609 irq_levels[i] = 0;
611 qemu_gettimeofday(&start_time);
612 qtest_opened = true;
613 if (qtest_log_fp) {
614 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
615 (long) start_time.tv_sec, (long) start_time.tv_usec);
617 break;
618 case CHR_EVENT_CLOSED:
619 qtest_opened = false;
620 if (qtest_log_fp) {
621 qemu_timeval tv;
622 qtest_get_time(&tv);
623 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
624 (long) tv.tv_sec, (long) tv.tv_usec);
626 break;
627 default:
628 break;
632 static int qtest_init_accel(MachineState *ms)
634 QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
635 &error_abort);
636 qemu_opt_set(opts, "shift", "0", &error_abort);
637 configure_icount(opts, &error_abort);
638 qemu_opts_del(opts);
639 return 0;
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);
648 if (chr == NULL) {
649 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
650 qtest_chrdev);
651 return;
654 if (qtest_log) {
655 if (strcmp(qtest_log, "none") != 0) {
656 qtest_log_fp = fopen(qtest_log, "w+");
658 } else {
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("");
666 qtest_chr = chr;
669 bool qtest_driver(void)
671 return qtest_chr;
674 static void qtest_accel_class_init(ObjectClass *oc, void *data)
676 AccelClass *ac = ACCEL_CLASS(oc);
677 ac->name = "QTest";
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);