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 "sysemu/qtest.h"
16 #include "sysemu/char.h"
17 #include "exec/ioport.h"
18 #include "exec/memory.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/cpus.h"
22 #include "qemu/config-file.h"
23 #include "qemu/option.h"
24 #include "qemu/error-report.h"
30 static DeviceState *irq_intercept_dev;
31 static FILE *qtest_log_fp;
32 static CharDriverState *qtest_chr;
33 static GString *inbuf;
34 static int irq_levels[MAX_IRQ];
35 static qemu_timeval start_time;
36 static bool qtest_opened;
38 #define FMT_timeval "%ld.%06ld"
43 * Line based protocol, request/response based. Server can send async messages
44 * so clients should always handle many async messages before the response
51 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
52 * let you adjust the value of the clock (monotonically). All the commands
53 * return the current value of the clock in nanoseconds.
58 * Advance the clock to the next deadline. Useful when waiting for
59 * asynchronous events.
64 * Advance the clock by NS nanoseconds.
69 * Advance the clock to NS nanoseconds (do nothing if it's already past).
71 * PIO and memory access:
100 * > writeq ADDR VALUE
118 * > write ADDR SIZE DATA
121 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
123 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
124 * than the expected size, the value will be zero filled at the end of the data
129 * > irq_intercept_in QOM-PATH
132 * > irq_intercept_out QOM-PATH
135 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
136 * QOM-PATH. When the pin is triggered, one of the following async messages
137 * will be printed to the qtest stream:
142 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
143 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
144 * NUM=0 even though it is remapped to GSI 2).
147 static int hex2nib(char ch)
149 if (ch >= '0' && ch <= '9') {
151 } else if (ch >= 'a' && ch <= 'f') {
152 return 10 + (ch - 'a');
153 } else if (ch >= 'A' && ch <= 'F') {
154 return 10 + (ch - 'A');
160 static void qtest_get_time(qemu_timeval *tv)
162 qemu_gettimeofday(tv);
163 tv->tv_sec -= start_time.tv_sec;
164 tv->tv_usec -= start_time.tv_usec;
165 if (tv->tv_usec < 0) {
166 tv->tv_usec += 1000000;
171 static void qtest_send_prefix(CharDriverState *chr)
175 if (!qtest_log_fp || !qtest_opened) {
180 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
181 (long) tv.tv_sec, (long) tv.tv_usec);
184 static void GCC_FMT_ATTR(2, 3) qtest_send(CharDriverState *chr,
185 const char *fmt, ...)
192 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
195 qemu_chr_fe_write_all(chr, (uint8_t *)buffer, len);
196 if (qtest_log_fp && qtest_opened) {
197 fprintf(qtest_log_fp, "%s", buffer);
201 static void qtest_irq_handler(void *opaque, int n, int level)
203 qemu_irq *old_irqs = opaque;
204 qemu_set_irq(old_irqs[n], level);
206 if (irq_levels[n] != level) {
207 CharDriverState *chr = qtest_chr;
208 irq_levels[n] = level;
209 qtest_send_prefix(chr);
210 qtest_send(chr, "IRQ %s %d\n",
211 level ? "raise" : "lower", n);
215 static void qtest_process_command(CharDriverState *chr, gchar **words)
217 const gchar *command;
228 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
229 (long) tv.tv_sec, (long) tv.tv_usec);
230 for (i = 0; words[i]; i++) {
231 fprintf(qtest_log_fp, " %s", words[i]);
233 fprintf(qtest_log_fp, "\n");
237 if (strcmp(words[0], "irq_intercept_out") == 0
238 || strcmp(words[0], "irq_intercept_in") == 0) {
243 dev = DEVICE(object_resolve_path(words[1], NULL));
245 qtest_send_prefix(chr);
246 qtest_send(chr, "FAIL Unknown device\n");
250 if (irq_intercept_dev) {
251 qtest_send_prefix(chr);
252 if (irq_intercept_dev != dev) {
253 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
255 qtest_send(chr, "OK\n");
260 QLIST_FOREACH(ngl, &dev->gpios, node) {
261 /* We don't support intercept of named GPIOs yet */
265 if (words[0][14] == 'o') {
266 qemu_irq_intercept_out(&ngl->out, qtest_irq_handler,
269 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
273 irq_intercept_dev = dev;
274 qtest_send_prefix(chr);
275 qtest_send(chr, "OK\n");
277 } else if (strcmp(words[0], "outb") == 0 ||
278 strcmp(words[0], "outw") == 0 ||
279 strcmp(words[0], "outl") == 0) {
283 g_assert(words[1] && words[2]);
284 addr = strtoul(words[1], NULL, 0);
285 value = strtoul(words[2], NULL, 0);
287 if (words[0][3] == 'b') {
288 cpu_outb(addr, value);
289 } else if (words[0][3] == 'w') {
290 cpu_outw(addr, value);
291 } else if (words[0][3] == 'l') {
292 cpu_outl(addr, value);
294 qtest_send_prefix(chr);
295 qtest_send(chr, "OK\n");
296 } else if (strcmp(words[0], "inb") == 0 ||
297 strcmp(words[0], "inw") == 0 ||
298 strcmp(words[0], "inl") == 0) {
300 uint32_t value = -1U;
303 addr = strtoul(words[1], NULL, 0);
305 if (words[0][2] == 'b') {
306 value = cpu_inb(addr);
307 } else if (words[0][2] == 'w') {
308 value = cpu_inw(addr);
309 } else if (words[0][2] == 'l') {
310 value = cpu_inl(addr);
312 qtest_send_prefix(chr);
313 qtest_send(chr, "OK 0x%04x\n", value);
314 } else if (strcmp(words[0], "writeb") == 0 ||
315 strcmp(words[0], "writew") == 0 ||
316 strcmp(words[0], "writel") == 0 ||
317 strcmp(words[0], "writeq") == 0) {
321 g_assert(words[1] && words[2]);
322 addr = strtoull(words[1], NULL, 0);
323 value = strtoull(words[2], NULL, 0);
325 if (words[0][5] == 'b') {
326 uint8_t data = value;
327 cpu_physical_memory_write(addr, &data, 1);
328 } else if (words[0][5] == 'w') {
329 uint16_t data = value;
331 cpu_physical_memory_write(addr, &data, 2);
332 } else if (words[0][5] == 'l') {
333 uint32_t data = value;
335 cpu_physical_memory_write(addr, &data, 4);
336 } else if (words[0][5] == 'q') {
337 uint64_t data = value;
339 cpu_physical_memory_write(addr, &data, 8);
341 qtest_send_prefix(chr);
342 qtest_send(chr, "OK\n");
343 } else if (strcmp(words[0], "readb") == 0 ||
344 strcmp(words[0], "readw") == 0 ||
345 strcmp(words[0], "readl") == 0 ||
346 strcmp(words[0], "readq") == 0) {
348 uint64_t value = UINT64_C(-1);
351 addr = strtoull(words[1], NULL, 0);
353 if (words[0][4] == 'b') {
355 cpu_physical_memory_read(addr, &data, 1);
357 } else if (words[0][4] == 'w') {
359 cpu_physical_memory_read(addr, &data, 2);
360 value = tswap16(data);
361 } else if (words[0][4] == 'l') {
363 cpu_physical_memory_read(addr, &data, 4);
364 value = tswap32(data);
365 } else if (words[0][4] == 'q') {
366 cpu_physical_memory_read(addr, &value, 8);
369 qtest_send_prefix(chr);
370 qtest_send(chr, "OK 0x%016" PRIx64 "\n", value);
371 } else if (strcmp(words[0], "read") == 0) {
372 uint64_t addr, len, i;
375 g_assert(words[1] && words[2]);
376 addr = strtoull(words[1], NULL, 0);
377 len = strtoull(words[2], NULL, 0);
379 data = g_malloc(len);
380 cpu_physical_memory_read(addr, data, len);
382 qtest_send_prefix(chr);
383 qtest_send(chr, "OK 0x");
384 for (i = 0; i < len; i++) {
385 qtest_send(chr, "%02x", data[i]);
387 qtest_send(chr, "\n");
390 } else if (strcmp(words[0], "write") == 0) {
391 uint64_t addr, len, i;
395 g_assert(words[1] && words[2] && words[3]);
396 addr = strtoull(words[1], NULL, 0);
397 len = strtoull(words[2], NULL, 0);
399 data_len = strlen(words[3]);
401 qtest_send(chr, "ERR invalid argument size\n");
405 data = g_malloc(len);
406 for (i = 0; i < len; i++) {
407 if ((i * 2 + 4) <= data_len) {
408 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
409 data[i] |= hex2nib(words[3][i * 2 + 3]);
414 cpu_physical_memory_write(addr, data, len);
417 qtest_send_prefix(chr);
418 qtest_send(chr, "OK\n");
419 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
423 ns = strtoll(words[1], NULL, 0);
425 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
427 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
428 qtest_send_prefix(chr);
429 qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
430 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
434 ns = strtoll(words[1], NULL, 0);
435 qtest_clock_warp(ns);
436 qtest_send_prefix(chr);
437 qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
439 qtest_send_prefix(chr);
440 qtest_send(chr, "FAIL Unknown command `%s'\n", words[0]);
444 static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
448 while ((end = strchr(inbuf->str, '\n')) != NULL) {
453 offset = end - inbuf->str;
455 cmd = g_string_new_len(inbuf->str, offset);
456 g_string_erase(inbuf, 0, offset + 1);
458 words = g_strsplit(cmd->str, " ", 0);
459 qtest_process_command(chr, words);
462 g_string_free(cmd, TRUE);
466 static void qtest_read(void *opaque, const uint8_t *buf, int size)
468 CharDriverState *chr = opaque;
470 g_string_append_len(inbuf, (const gchar *)buf, size);
471 qtest_process_inbuf(chr, inbuf);
474 static int qtest_can_read(void *opaque)
479 static void qtest_event(void *opaque, int event)
484 case CHR_EVENT_OPENED:
486 * We used to call qemu_system_reset() here, hoping we could
487 * use the same process for multiple tests that way. Never
488 * used. Injects an extra reset even when it's not used, and
489 * that can mess up tests, e.g. -boot once.
491 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
494 qemu_gettimeofday(&start_time);
497 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
498 (long) start_time.tv_sec, (long) start_time.tv_usec);
501 case CHR_EVENT_CLOSED:
502 qtest_opened = false;
506 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
507 (long) tv.tv_sec, (long) tv.tv_usec);
515 static void configure_qtest_icount(const char *options)
517 QemuOpts *opts = qemu_opts_parse(qemu_find_opts("icount"), options, 1);
518 configure_icount(opts, &error_abort);
522 int qtest_init_accel(MachineClass *mc)
524 configure_qtest_icount("0");
528 void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
530 CharDriverState *chr;
532 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
535 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
540 qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
541 qemu_chr_fe_set_echo(chr, true);
543 inbuf = g_string_new("");
546 if (strcmp(qtest_log, "none") != 0) {
547 qtest_log_fp = fopen(qtest_log, "w+");
550 qtest_log_fp = stderr;
556 bool qtest_driver(void)