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/sysemu.h"
21 #include "sysemu/cpus.h"
25 const char *qtest_chrdev
;
26 const char *qtest_log
;
29 static DeviceState
*irq_intercept_dev
;
30 static FILE *qtest_log_fp
;
31 static CharDriverState
*qtest_chr
;
32 static GString
*inbuf
;
33 static int irq_levels
[MAX_IRQ
];
34 static qemu_timeval start_time
;
35 static bool qtest_opened
;
37 #define FMT_timeval "%ld.%06ld"
42 * Line based protocol, request/response based. Server can send async messages
43 * so clients should always handle many async messages before the response
50 * The qtest client is completely in charge of the vm_clock. qtest commands
51 * let you adjust the value of the clock (monotonically). All the commands
52 * return the current value of the clock in nanoseconds.
57 * Advance the clock to the next deadline. Useful when waiting for
58 * asynchronous events.
63 * Advance the clock by NS nanoseconds.
68 * Advance the clock to NS nanoseconds (do nothing if it's already past).
70 * PIO and memory access:
117 * > write ADDR SIZE DATA
120 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
122 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
123 * than the expected size, the value will be zero filled at the end of the data
128 * > irq_intercept_in QOM-PATH
131 * > irq_intercept_out QOM-PATH
134 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
135 * QOM-PATH. When the pin is triggered, one of the following async messages
136 * will be printed to the qtest stream:
141 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
142 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
143 * NUM=0 even though it is remapped to GSI 2).
146 static int hex2nib(char ch
)
148 if (ch
>= '0' && ch
<= '9') {
150 } else if (ch
>= 'a' && ch
<= 'f') {
151 return 10 + (ch
- 'a');
152 } else if (ch
>= 'A' && ch
<= 'F') {
153 return 10 + (ch
- 'a');
159 static void qtest_get_time(qemu_timeval
*tv
)
161 qemu_gettimeofday(tv
);
162 tv
->tv_sec
-= start_time
.tv_sec
;
163 tv
->tv_usec
-= start_time
.tv_usec
;
164 if (tv
->tv_usec
< 0) {
165 tv
->tv_usec
+= 1000000;
170 static void qtest_send_prefix(CharDriverState
*chr
)
174 if (!qtest_log_fp
|| !qtest_opened
) {
179 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ",
180 tv
.tv_sec
, (long) tv
.tv_usec
);
183 static void GCC_FMT_ATTR(2, 3) qtest_send(CharDriverState
*chr
,
184 const char *fmt
, ...)
191 len
= vsnprintf(buffer
, sizeof(buffer
), fmt
, ap
);
194 qemu_chr_fe_write_all(chr
, (uint8_t *)buffer
, len
);
195 if (qtest_log_fp
&& qtest_opened
) {
196 fprintf(qtest_log_fp
, "%s", buffer
);
200 static void qtest_irq_handler(void *opaque
, int n
, int level
)
202 qemu_irq
*old_irqs
= opaque
;
203 qemu_set_irq(old_irqs
[n
], level
);
205 if (irq_levels
[n
] != level
) {
206 CharDriverState
*chr
= qtest_chr
;
207 irq_levels
[n
] = level
;
208 qtest_send_prefix(chr
);
209 qtest_send(chr
, "IRQ %s %d\n",
210 level
? "raise" : "lower", n
);
214 static void qtest_process_command(CharDriverState
*chr
, gchar
**words
)
216 const gchar
*command
;
227 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]",
228 tv
.tv_sec
, (long) tv
.tv_usec
);
229 for (i
= 0; words
[i
]; i
++) {
230 fprintf(qtest_log_fp
, " %s", words
[i
]);
232 fprintf(qtest_log_fp
, "\n");
236 if (strcmp(words
[0], "irq_intercept_out") == 0
237 || strcmp(words
[0], "irq_intercept_in") == 0) {
241 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
243 qtest_send_prefix(chr
);
244 qtest_send(chr
, "FAIL Unknown device\n");
248 if (irq_intercept_dev
) {
249 qtest_send_prefix(chr
);
250 if (irq_intercept_dev
!= dev
) {
251 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
253 qtest_send(chr
, "OK\n");
258 if (words
[0][14] == 'o') {
259 qemu_irq_intercept_out(&dev
->gpio_out
, qtest_irq_handler
, dev
->num_gpio_out
);
261 qemu_irq_intercept_in(dev
->gpio_in
, qtest_irq_handler
, dev
->num_gpio_in
);
263 irq_intercept_dev
= dev
;
264 qtest_send_prefix(chr
);
265 qtest_send(chr
, "OK\n");
267 } else if (strcmp(words
[0], "outb") == 0 ||
268 strcmp(words
[0], "outw") == 0 ||
269 strcmp(words
[0], "outl") == 0) {
273 g_assert(words
[1] && words
[2]);
274 addr
= strtol(words
[1], NULL
, 0);
275 value
= strtol(words
[2], NULL
, 0);
277 if (words
[0][3] == 'b') {
278 cpu_outb(addr
, value
);
279 } else if (words
[0][3] == 'w') {
280 cpu_outw(addr
, value
);
281 } else if (words
[0][3] == 'l') {
282 cpu_outl(addr
, value
);
284 qtest_send_prefix(chr
);
285 qtest_send(chr
, "OK\n");
286 } else if (strcmp(words
[0], "inb") == 0 ||
287 strcmp(words
[0], "inw") == 0 ||
288 strcmp(words
[0], "inl") == 0) {
290 uint32_t value
= -1U;
293 addr
= strtol(words
[1], NULL
, 0);
295 if (words
[0][2] == 'b') {
296 value
= cpu_inb(addr
);
297 } else if (words
[0][2] == 'w') {
298 value
= cpu_inw(addr
);
299 } else if (words
[0][2] == 'l') {
300 value
= cpu_inl(addr
);
302 qtest_send_prefix(chr
);
303 qtest_send(chr
, "OK 0x%04x\n", value
);
304 } else if (strcmp(words
[0], "writeb") == 0 ||
305 strcmp(words
[0], "writew") == 0 ||
306 strcmp(words
[0], "writel") == 0 ||
307 strcmp(words
[0], "writeq") == 0) {
311 g_assert(words
[1] && words
[2]);
312 addr
= strtoull(words
[1], NULL
, 0);
313 value
= strtoull(words
[2], NULL
, 0);
315 if (words
[0][5] == 'b') {
316 uint8_t data
= value
;
317 cpu_physical_memory_write(addr
, &data
, 1);
318 } else if (words
[0][5] == 'w') {
319 uint16_t data
= value
;
321 cpu_physical_memory_write(addr
, &data
, 2);
322 } else if (words
[0][5] == 'l') {
323 uint32_t data
= value
;
325 cpu_physical_memory_write(addr
, &data
, 4);
326 } else if (words
[0][5] == 'q') {
327 uint64_t data
= value
;
329 cpu_physical_memory_write(addr
, &data
, 8);
331 qtest_send_prefix(chr
);
332 qtest_send(chr
, "OK\n");
333 } else if (strcmp(words
[0], "readb") == 0 ||
334 strcmp(words
[0], "readw") == 0 ||
335 strcmp(words
[0], "readl") == 0 ||
336 strcmp(words
[0], "readq") == 0) {
338 uint64_t value
= UINT64_C(-1);
341 addr
= strtoull(words
[1], NULL
, 0);
343 if (words
[0][4] == 'b') {
345 cpu_physical_memory_read(addr
, &data
, 1);
347 } else if (words
[0][4] == 'w') {
349 cpu_physical_memory_read(addr
, &data
, 2);
350 value
= tswap16(data
);
351 } else if (words
[0][4] == 'l') {
353 cpu_physical_memory_read(addr
, &data
, 4);
354 value
= tswap32(data
);
355 } else if (words
[0][4] == 'q') {
356 cpu_physical_memory_read(addr
, &value
, 8);
359 qtest_send_prefix(chr
);
360 qtest_send(chr
, "OK 0x%016" PRIx64
"\n", value
);
361 } else if (strcmp(words
[0], "read") == 0) {
362 uint64_t addr
, len
, i
;
365 g_assert(words
[1] && words
[2]);
366 addr
= strtoull(words
[1], NULL
, 0);
367 len
= strtoull(words
[2], NULL
, 0);
369 data
= g_malloc(len
);
370 cpu_physical_memory_read(addr
, data
, len
);
372 qtest_send_prefix(chr
);
373 qtest_send(chr
, "OK 0x");
374 for (i
= 0; i
< len
; i
++) {
375 qtest_send(chr
, "%02x", data
[i
]);
377 qtest_send(chr
, "\n");
380 } else if (strcmp(words
[0], "write") == 0) {
381 uint64_t addr
, len
, i
;
385 g_assert(words
[1] && words
[2] && words
[3]);
386 addr
= strtoull(words
[1], NULL
, 0);
387 len
= strtoull(words
[2], NULL
, 0);
389 data_len
= strlen(words
[3]);
391 qtest_send(chr
, "ERR invalid argument size\n");
395 data
= g_malloc(len
);
396 for (i
= 0; i
< len
; i
++) {
397 if ((i
* 2 + 4) <= data_len
) {
398 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
399 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
404 cpu_physical_memory_write(addr
, data
, len
);
407 qtest_send_prefix(chr
);
408 qtest_send(chr
, "OK\n");
409 } else if (strcmp(words
[0], "clock_step") == 0) {
413 ns
= strtoll(words
[1], NULL
, 0);
415 ns
= qemu_clock_deadline(vm_clock
);
417 qtest_clock_warp(qemu_get_clock_ns(vm_clock
) + ns
);
418 qtest_send_prefix(chr
);
419 qtest_send(chr
, "OK %"PRIi64
"\n", (int64_t)qemu_get_clock_ns(vm_clock
));
420 } else if (strcmp(words
[0], "clock_set") == 0) {
424 ns
= strtoll(words
[1], NULL
, 0);
425 qtest_clock_warp(ns
);
426 qtest_send_prefix(chr
);
427 qtest_send(chr
, "OK %"PRIi64
"\n", (int64_t)qemu_get_clock_ns(vm_clock
));
429 qtest_send_prefix(chr
);
430 qtest_send(chr
, "FAIL Unknown command `%s'\n", words
[0]);
434 static void qtest_process_inbuf(CharDriverState
*chr
, GString
*inbuf
)
438 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
443 offset
= end
- inbuf
->str
;
445 cmd
= g_string_new_len(inbuf
->str
, offset
);
446 g_string_erase(inbuf
, 0, offset
+ 1);
448 words
= g_strsplit(cmd
->str
, " ", 0);
449 qtest_process_command(chr
, words
);
452 g_string_free(cmd
, TRUE
);
456 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
458 CharDriverState
*chr
= opaque
;
460 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
461 qtest_process_inbuf(chr
, inbuf
);
464 static int qtest_can_read(void *opaque
)
469 static void qtest_event(void *opaque
, int event
)
474 case CHR_EVENT_OPENED
:
475 qemu_system_reset(false);
476 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
479 qemu_gettimeofday(&start_time
);
482 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n",
483 start_time
.tv_sec
, (long) start_time
.tv_usec
);
486 case CHR_EVENT_CLOSED
:
487 qtest_opened
= false;
491 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n",
492 tv
.tv_sec
, (long) tv
.tv_usec
);
502 CharDriverState
*chr
;
504 g_assert(qtest_chrdev
!= NULL
);
506 configure_icount("0");
507 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
509 qemu_chr_add_handlers(chr
, qtest_can_read
, qtest_read
, qtest_event
, chr
);
510 qemu_chr_fe_set_echo(chr
, true);
512 inbuf
= g_string_new("");
515 if (strcmp(qtest_log
, "none") != 0) {
516 qtest_log_fp
= fopen(qtest_log
, "w+");
519 qtest_log_fp
= stderr
;