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 "sysemu/qtest.h"
16 #include "sysemu/char.h"
17 #include "exec/ioport.h"
18 #include "exec/memory.h"
20 #include "sysemu/accel.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/cpus.h"
23 #include "qemu/config-file.h"
24 #include "qemu/option.h"
25 #include "qemu/error-report.h"
31 static DeviceState
*irq_intercept_dev
;
32 static FILE *qtest_log_fp
;
33 static CharDriverState
*qtest_chr
;
34 static GString
*inbuf
;
35 static int irq_levels
[MAX_IRQ
];
36 static qemu_timeval start_time
;
37 static bool qtest_opened
;
39 #define FMT_timeval "%ld.%06ld"
44 * Line based protocol, request/response based. Server can send async messages
45 * so clients should always handle many async messages before the response
52 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
53 * let you adjust the value of the clock (monotonically). All the commands
54 * return the current value of the clock in nanoseconds.
59 * Advance the clock to the next deadline. Useful when waiting for
60 * asynchronous events.
65 * Advance the clock by NS nanoseconds.
70 * Advance the clock to NS nanoseconds (do nothing if it's already past).
72 * PIO and memory access:
101 * > writeq ADDR VALUE
119 * > write ADDR SIZE DATA
122 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
124 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
125 * than the expected size, the value will be zero filled at the end of the data
130 * > irq_intercept_in QOM-PATH
133 * > irq_intercept_out QOM-PATH
136 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
137 * QOM-PATH. When the pin is triggered, one of the following async messages
138 * will be printed to the qtest stream:
143 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
144 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
145 * NUM=0 even though it is remapped to GSI 2).
148 static int hex2nib(char ch
)
150 if (ch
>= '0' && ch
<= '9') {
152 } else if (ch
>= 'a' && ch
<= 'f') {
153 return 10 + (ch
- 'a');
154 } else if (ch
>= 'A' && ch
<= 'F') {
155 return 10 + (ch
- 'A');
161 static void qtest_get_time(qemu_timeval
*tv
)
163 qemu_gettimeofday(tv
);
164 tv
->tv_sec
-= start_time
.tv_sec
;
165 tv
->tv_usec
-= start_time
.tv_usec
;
166 if (tv
->tv_usec
< 0) {
167 tv
->tv_usec
+= 1000000;
172 static void qtest_send_prefix(CharDriverState
*chr
)
176 if (!qtest_log_fp
|| !qtest_opened
) {
181 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ",
182 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
185 static void GCC_FMT_ATTR(2, 3) qtest_send(CharDriverState
*chr
,
186 const char *fmt
, ...)
193 len
= vsnprintf(buffer
, sizeof(buffer
), fmt
, ap
);
196 qemu_chr_fe_write_all(chr
, (uint8_t *)buffer
, len
);
197 if (qtest_log_fp
&& qtest_opened
) {
198 fprintf(qtest_log_fp
, "%s", buffer
);
202 static void qtest_irq_handler(void *opaque
, int n
, int level
)
204 qemu_irq
*old_irqs
= opaque
;
205 qemu_set_irq(old_irqs
[n
], level
);
207 if (irq_levels
[n
] != level
) {
208 CharDriverState
*chr
= qtest_chr
;
209 irq_levels
[n
] = level
;
210 qtest_send_prefix(chr
);
211 qtest_send(chr
, "IRQ %s %d\n",
212 level
? "raise" : "lower", n
);
216 static void qtest_process_command(CharDriverState
*chr
, gchar
**words
)
218 const gchar
*command
;
229 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]",
230 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
231 for (i
= 0; words
[i
]; i
++) {
232 fprintf(qtest_log_fp
, " %s", words
[i
]);
234 fprintf(qtest_log_fp
, "\n");
238 if (strcmp(words
[0], "irq_intercept_out") == 0
239 || strcmp(words
[0], "irq_intercept_in") == 0) {
244 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
246 qtest_send_prefix(chr
);
247 qtest_send(chr
, "FAIL Unknown device\n");
251 if (irq_intercept_dev
) {
252 qtest_send_prefix(chr
);
253 if (irq_intercept_dev
!= dev
) {
254 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
256 qtest_send(chr
, "OK\n");
261 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
262 /* We don't support intercept of named GPIOs yet */
266 if (words
[0][14] == 'o') {
267 qemu_irq_intercept_out(&ngl
->out
, qtest_irq_handler
,
270 qemu_irq_intercept_in(ngl
->in
, qtest_irq_handler
,
274 irq_intercept_dev
= dev
;
275 qtest_send_prefix(chr
);
276 qtest_send(chr
, "OK\n");
278 } else if (strcmp(words
[0], "outb") == 0 ||
279 strcmp(words
[0], "outw") == 0 ||
280 strcmp(words
[0], "outl") == 0) {
284 g_assert(words
[1] && words
[2]);
285 addr
= strtoul(words
[1], NULL
, 0);
286 value
= strtoul(words
[2], NULL
, 0);
288 if (words
[0][3] == 'b') {
289 cpu_outb(addr
, value
);
290 } else if (words
[0][3] == 'w') {
291 cpu_outw(addr
, value
);
292 } else if (words
[0][3] == 'l') {
293 cpu_outl(addr
, value
);
295 qtest_send_prefix(chr
);
296 qtest_send(chr
, "OK\n");
297 } else if (strcmp(words
[0], "inb") == 0 ||
298 strcmp(words
[0], "inw") == 0 ||
299 strcmp(words
[0], "inl") == 0) {
301 uint32_t value
= -1U;
304 addr
= strtoul(words
[1], NULL
, 0);
306 if (words
[0][2] == 'b') {
307 value
= cpu_inb(addr
);
308 } else if (words
[0][2] == 'w') {
309 value
= cpu_inw(addr
);
310 } else if (words
[0][2] == 'l') {
311 value
= cpu_inl(addr
);
313 qtest_send_prefix(chr
);
314 qtest_send(chr
, "OK 0x%04x\n", value
);
315 } else if (strcmp(words
[0], "writeb") == 0 ||
316 strcmp(words
[0], "writew") == 0 ||
317 strcmp(words
[0], "writel") == 0 ||
318 strcmp(words
[0], "writeq") == 0) {
322 g_assert(words
[1] && words
[2]);
323 addr
= strtoull(words
[1], NULL
, 0);
324 value
= strtoull(words
[2], NULL
, 0);
326 if (words
[0][5] == 'b') {
327 uint8_t data
= value
;
328 cpu_physical_memory_write(addr
, &data
, 1);
329 } else if (words
[0][5] == 'w') {
330 uint16_t data
= value
;
332 cpu_physical_memory_write(addr
, &data
, 2);
333 } else if (words
[0][5] == 'l') {
334 uint32_t data
= value
;
336 cpu_physical_memory_write(addr
, &data
, 4);
337 } else if (words
[0][5] == 'q') {
338 uint64_t data
= value
;
340 cpu_physical_memory_write(addr
, &data
, 8);
342 qtest_send_prefix(chr
);
343 qtest_send(chr
, "OK\n");
344 } else if (strcmp(words
[0], "readb") == 0 ||
345 strcmp(words
[0], "readw") == 0 ||
346 strcmp(words
[0], "readl") == 0 ||
347 strcmp(words
[0], "readq") == 0) {
349 uint64_t value
= UINT64_C(-1);
352 addr
= strtoull(words
[1], NULL
, 0);
354 if (words
[0][4] == 'b') {
356 cpu_physical_memory_read(addr
, &data
, 1);
358 } else if (words
[0][4] == 'w') {
360 cpu_physical_memory_read(addr
, &data
, 2);
361 value
= tswap16(data
);
362 } else if (words
[0][4] == 'l') {
364 cpu_physical_memory_read(addr
, &data
, 4);
365 value
= tswap32(data
);
366 } else if (words
[0][4] == 'q') {
367 cpu_physical_memory_read(addr
, &value
, 8);
370 qtest_send_prefix(chr
);
371 qtest_send(chr
, "OK 0x%016" PRIx64
"\n", value
);
372 } else if (strcmp(words
[0], "read") == 0) {
373 uint64_t addr
, len
, i
;
376 g_assert(words
[1] && words
[2]);
377 addr
= strtoull(words
[1], NULL
, 0);
378 len
= strtoull(words
[2], NULL
, 0);
380 data
= g_malloc(len
);
381 cpu_physical_memory_read(addr
, data
, len
);
383 qtest_send_prefix(chr
);
384 qtest_send(chr
, "OK 0x");
385 for (i
= 0; i
< len
; i
++) {
386 qtest_send(chr
, "%02x", data
[i
]);
388 qtest_send(chr
, "\n");
391 } else if (strcmp(words
[0], "write") == 0) {
392 uint64_t addr
, len
, i
;
396 g_assert(words
[1] && words
[2] && words
[3]);
397 addr
= strtoull(words
[1], NULL
, 0);
398 len
= strtoull(words
[2], NULL
, 0);
400 data_len
= strlen(words
[3]);
402 qtest_send(chr
, "ERR invalid argument size\n");
406 data
= g_malloc(len
);
407 for (i
= 0; i
< len
; i
++) {
408 if ((i
* 2 + 4) <= data_len
) {
409 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
410 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
415 cpu_physical_memory_write(addr
, data
, len
);
418 qtest_send_prefix(chr
);
419 qtest_send(chr
, "OK\n");
420 } else if (qtest_enabled() && strcmp(words
[0], "clock_step") == 0) {
424 ns
= strtoll(words
[1], NULL
, 0);
426 ns
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
);
428 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + ns
);
429 qtest_send_prefix(chr
);
430 qtest_send(chr
, "OK %"PRIi64
"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
431 } else if (qtest_enabled() && strcmp(words
[0], "clock_set") == 0) {
435 ns
= strtoll(words
[1], NULL
, 0);
436 qtest_clock_warp(ns
);
437 qtest_send_prefix(chr
);
438 qtest_send(chr
, "OK %"PRIi64
"\n", (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
440 qtest_send_prefix(chr
);
441 qtest_send(chr
, "FAIL Unknown command `%s'\n", words
[0]);
445 static void qtest_process_inbuf(CharDriverState
*chr
, GString
*inbuf
)
449 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
454 offset
= end
- inbuf
->str
;
456 cmd
= g_string_new_len(inbuf
->str
, offset
);
457 g_string_erase(inbuf
, 0, offset
+ 1);
459 words
= g_strsplit(cmd
->str
, " ", 0);
460 qtest_process_command(chr
, words
);
463 g_string_free(cmd
, TRUE
);
467 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
469 CharDriverState
*chr
= opaque
;
471 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
472 qtest_process_inbuf(chr
, inbuf
);
475 static int qtest_can_read(void *opaque
)
480 static void qtest_event(void *opaque
, int event
)
485 case CHR_EVENT_OPENED
:
487 * We used to call qemu_system_reset() here, hoping we could
488 * use the same process for multiple tests that way. Never
489 * used. Injects an extra reset even when it's not used, and
490 * that can mess up tests, e.g. -boot once.
492 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
495 qemu_gettimeofday(&start_time
);
498 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n",
499 (long) start_time
.tv_sec
, (long) start_time
.tv_usec
);
502 case CHR_EVENT_CLOSED
:
503 qtest_opened
= false;
507 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n",
508 (long) tv
.tv_sec
, (long) tv
.tv_usec
);
516 static void configure_qtest_icount(const char *options
)
518 QemuOpts
*opts
= qemu_opts_parse(qemu_find_opts("icount"), options
, 1);
519 configure_icount(opts
, &error_abort
);
523 static int qtest_init_accel(MachineState
*ms
)
525 configure_qtest_icount("0");
529 void qtest_init(const char *qtest_chrdev
, const char *qtest_log
, Error
**errp
)
531 CharDriverState
*chr
;
533 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
536 error_setg(errp
, "Failed to initialize device for qtest: \"%s\"",
541 qemu_chr_add_handlers(chr
, qtest_can_read
, qtest_read
, qtest_event
, chr
);
542 qemu_chr_fe_set_echo(chr
, true);
544 inbuf
= g_string_new("");
547 if (strcmp(qtest_log
, "none") != 0) {
548 qtest_log_fp
= fopen(qtest_log
, "w+");
551 qtest_log_fp
= stderr
;
557 bool qtest_driver(void)
562 static void qtest_accel_class_init(ObjectClass
*oc
, void *data
)
564 AccelClass
*ac
= ACCEL_CLASS(oc
);
566 ac
->available
= qtest_available
;
567 ac
->init_machine
= qtest_init_accel
;
568 ac
->allowed
= &qtest_allowed
;
571 #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
573 static const TypeInfo qtest_accel_type
= {
574 .name
= TYPE_QTEST_ACCEL
,
575 .parent
= TYPE_ACCEL
,
576 .class_init
= qtest_accel_class_init
,
579 static void qtest_type_init(void)
581 type_register_static(&qtest_accel_type
);
584 type_init(qtest_type_init
);