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.
16 #include "qemu-char.h"
25 const char *qtest_chrdev
;
26 const char *qtest_log
;
27 int qtest_allowed
= 0;
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:
93 * > write ADDR SIZE DATA
96 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
98 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
99 * than the expected size, the value will be zero filled at the end of the data
104 * > irq_intercept_in QOM-PATH
107 * > irq_intercept_out QOM-PATH
110 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
111 * QOM-PATH. When the pin is triggered, one of the following async messages
112 * will be printed to the qtest stream:
117 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
118 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
119 * NUM=0 even though it is remapped to GSI 2).
122 static int hex2nib(char ch
)
124 if (ch
>= '0' && ch
<= '9') {
126 } else if (ch
>= 'a' && ch
<= 'f') {
127 return 10 + (ch
- 'a');
128 } else if (ch
>= 'A' && ch
<= 'F') {
129 return 10 + (ch
- 'a');
135 static void qtest_get_time(qemu_timeval
*tv
)
137 qemu_gettimeofday(tv
);
138 tv
->tv_sec
-= start_time
.tv_sec
;
139 tv
->tv_usec
-= start_time
.tv_usec
;
140 if (tv
->tv_usec
< 0) {
141 tv
->tv_usec
+= 1000000;
146 static void qtest_send_prefix(CharDriverState
*chr
)
150 if (!qtest_log_fp
|| !qtest_opened
) {
155 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ",
156 tv
.tv_sec
, (long) tv
.tv_usec
);
159 static void GCC_FMT_ATTR(2, 3) qtest_send(CharDriverState
*chr
,
160 const char *fmt
, ...)
167 len
= vsnprintf(buffer
, sizeof(buffer
), fmt
, ap
);
170 qemu_chr_fe_write(chr
, (uint8_t *)buffer
, len
);
171 if (qtest_log_fp
&& qtest_opened
) {
172 fprintf(qtest_log_fp
, "%s", buffer
);
176 static void qtest_irq_handler(void *opaque
, int n
, int level
)
178 qemu_irq
*old_irqs
= opaque
;
179 qemu_set_irq(old_irqs
[n
], level
);
181 if (irq_levels
[n
] != level
) {
182 CharDriverState
*chr
= qtest_chr
;
183 irq_levels
[n
] = level
;
184 qtest_send_prefix(chr
);
185 qtest_send(chr
, "IRQ %s %d\n",
186 level
? "raise" : "lower", n
);
190 static void qtest_process_command(CharDriverState
*chr
, gchar
**words
)
192 const gchar
*command
;
203 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]",
204 tv
.tv_sec
, (long) tv
.tv_usec
);
205 for (i
= 0; words
[i
]; i
++) {
206 fprintf(qtest_log_fp
, " %s", words
[i
]);
208 fprintf(qtest_log_fp
, "\n");
212 if (strcmp(words
[0], "irq_intercept_out") == 0
213 || strcmp(words
[0], "irq_intercept_in") == 0) {
217 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
219 qtest_send_prefix(chr
);
220 qtest_send(chr
, "FAIL Unknown device\n");
224 if (irq_intercept_dev
) {
225 qtest_send_prefix(chr
);
226 if (irq_intercept_dev
!= dev
) {
227 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
229 qtest_send(chr
, "OK\n");
234 if (words
[0][14] == 'o') {
235 qemu_irq_intercept_out(&dev
->gpio_out
, qtest_irq_handler
, dev
->num_gpio_out
);
237 qemu_irq_intercept_in(dev
->gpio_in
, qtest_irq_handler
, dev
->num_gpio_in
);
239 irq_intercept_dev
= dev
;
240 qtest_send_prefix(chr
);
241 qtest_send(chr
, "OK\n");
243 } else if (strcmp(words
[0], "outb") == 0 ||
244 strcmp(words
[0], "outw") == 0 ||
245 strcmp(words
[0], "outl") == 0) {
249 g_assert(words
[1] && words
[2]);
250 addr
= strtol(words
[1], NULL
, 0);
251 value
= strtol(words
[2], NULL
, 0);
253 if (words
[0][3] == 'b') {
254 cpu_outb(addr
, value
);
255 } else if (words
[0][3] == 'w') {
256 cpu_outw(addr
, value
);
257 } else if (words
[0][3] == 'l') {
258 cpu_outl(addr
, value
);
260 qtest_send_prefix(chr
);
261 qtest_send(chr
, "OK\n");
262 } else if (strcmp(words
[0], "inb") == 0 ||
263 strcmp(words
[0], "inw") == 0 ||
264 strcmp(words
[0], "inl") == 0) {
266 uint32_t value
= -1U;
269 addr
= strtol(words
[1], NULL
, 0);
271 if (words
[0][2] == 'b') {
272 value
= cpu_inb(addr
);
273 } else if (words
[0][2] == 'w') {
274 value
= cpu_inw(addr
);
275 } else if (words
[0][2] == 'l') {
276 value
= cpu_inl(addr
);
278 qtest_send_prefix(chr
);
279 qtest_send(chr
, "OK 0x%04x\n", value
);
280 } else if (strcmp(words
[0], "read") == 0) {
281 uint64_t addr
, len
, i
;
284 g_assert(words
[1] && words
[2]);
285 addr
= strtoul(words
[1], NULL
, 0);
286 len
= strtoul(words
[2], NULL
, 0);
288 data
= g_malloc(len
);
289 cpu_physical_memory_read(addr
, data
, len
);
291 qtest_send_prefix(chr
);
292 qtest_send(chr
, "OK 0x");
293 for (i
= 0; i
< len
; i
++) {
294 qtest_send(chr
, "%02x", data
[i
]);
296 qtest_send(chr
, "\n");
299 } else if (strcmp(words
[0], "write") == 0) {
300 uint64_t addr
, len
, i
;
304 g_assert(words
[1] && words
[2] && words
[3]);
305 addr
= strtoul(words
[1], NULL
, 0);
306 len
= strtoul(words
[2], NULL
, 0);
308 data_len
= strlen(words
[3]);
310 qtest_send(chr
, "ERR invalid argument size\n");
314 data
= g_malloc(len
);
315 for (i
= 0; i
< len
; i
++) {
316 if ((i
* 2 + 4) <= data_len
) {
317 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
318 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
323 cpu_physical_memory_write(addr
, data
, len
);
326 qtest_send_prefix(chr
);
327 qtest_send(chr
, "OK\n");
328 } else if (strcmp(words
[0], "clock_step") == 0) {
332 ns
= strtoll(words
[1], NULL
, 0);
334 ns
= qemu_clock_deadline(vm_clock
);
336 qtest_clock_warp(qemu_get_clock_ns(vm_clock
) + ns
);
337 qtest_send_prefix(chr
);
338 qtest_send(chr
, "OK %"PRIi64
"\n", (int64_t)qemu_get_clock_ns(vm_clock
));
339 } else if (strcmp(words
[0], "clock_set") == 0) {
343 ns
= strtoll(words
[1], NULL
, 0);
344 qtest_clock_warp(ns
);
345 qtest_send_prefix(chr
);
346 qtest_send(chr
, "OK %"PRIi64
"\n", (int64_t)qemu_get_clock_ns(vm_clock
));
348 qtest_send_prefix(chr
);
349 qtest_send(chr
, "FAIL Unknown command `%s'\n", words
[0]);
353 static void qtest_process_inbuf(CharDriverState
*chr
, GString
*inbuf
)
357 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
362 offset
= end
- inbuf
->str
;
364 cmd
= g_string_new_len(inbuf
->str
, offset
);
365 g_string_erase(inbuf
, 0, offset
+ 1);
367 words
= g_strsplit(cmd
->str
, " ", 0);
368 qtest_process_command(chr
, words
);
371 g_string_free(cmd
, TRUE
);
375 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
377 CharDriverState
*chr
= opaque
;
379 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
380 qtest_process_inbuf(chr
, inbuf
);
383 static int qtest_can_read(void *opaque
)
388 static void qtest_event(void *opaque
, int event
)
393 case CHR_EVENT_OPENED
:
394 qemu_system_reset(false);
395 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
398 qemu_gettimeofday(&start_time
);
401 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n",
402 start_time
.tv_sec
, (long) start_time
.tv_usec
);
405 case CHR_EVENT_CLOSED
:
406 qtest_opened
= false;
410 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n",
411 tv
.tv_sec
, (long) tv
.tv_usec
);
421 CharDriverState
*chr
;
423 g_assert(qtest_chrdev
!= NULL
);
425 configure_icount("0");
426 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
428 qemu_chr_add_handlers(chr
, qtest_can_read
, qtest_read
, qtest_event
, chr
);
429 qemu_chr_fe_set_echo(chr
, true);
431 inbuf
= g_string_new("");
434 if (strcmp(qtest_log
, "none") != 0) {
435 qtest_log_fp
= fopen(qtest_log
, "w+");
438 qtest_log_fp
= stderr
;