migration: don't use uninitialized variables
[qemu.git] / qtest.c
blob74f1842c1e3e16ea388813166f7399a23fa95118
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 "sysemu/qtest.h"
15 #include "hw/qdev.h"
16 #include "sysemu/char.h"
17 #include "exec/ioport.h"
18 #include "exec/memory.h"
19 #include "hw/irq.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/cpus.h"
23 #define MAX_IRQ 256
25 const char *qtest_chrdev;
26 const char *qtest_log;
27 bool qtest_allowed;
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"
39 /**
40 * QTest Protocol
42 * Line based protocol, request/response based. Server can send async messages
43 * so clients should always handle many async messages before the response
44 * comes in.
46 * Valid requests
48 * Clock management:
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.
54 * > clock_step
55 * < OK VALUE
57 * Advance the clock to the next deadline. Useful when waiting for
58 * asynchronous events.
60 * > clock_step NS
61 * < OK VALUE
63 * Advance the clock by NS nanoseconds.
65 * > clock_set NS
66 * < OK VALUE
68 * Advance the clock to NS nanoseconds (do nothing if it's already past).
70 * PIO and memory access:
72 * > outb ADDR VALUE
73 * < OK
75 * > outw ADDR VALUE
76 * < OK
78 * > outl ADDR VALUE
79 * < OK
81 * > inb ADDR
82 * < OK VALUE
84 * > inw ADDR
85 * < OK VALUE
87 * > inl ADDR
88 * < OK VALUE
90 * > writeb ADDR VALUE
91 * < OK
93 * > writew ADDR VALUE
94 * < OK
96 * > writel ADDR VALUE
97 * < OK
99 * > writeq ADDR VALUE
100 * < OK
102 * > readb ADDR
103 * < OK VALUE
105 * > readw ADDR
106 * < OK VALUE
108 * > readl ADDR
109 * < OK VALUE
111 * > readq ADDR
112 * < OK VALUE
114 * > read ADDR SIZE
115 * < OK DATA
117 * > write ADDR SIZE DATA
118 * < OK
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
124 * sequence.
126 * IRQ management:
128 * > irq_intercept_in QOM-PATH
129 * < OK
131 * > irq_intercept_out QOM-PATH
132 * < OK
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:
138 * IRQ raise NUM
139 * IRQ lower NUM
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') {
149 return ch - '0';
150 } else if (ch >= 'a' && ch <= 'f') {
151 return 10 + (ch - 'a');
152 } else if (ch >= 'A' && ch <= 'F') {
153 return 10 + (ch - 'a');
154 } else {
155 return -1;
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;
166 tv->tv_sec -= 1;
170 static void qtest_send_prefix(CharDriverState *chr)
172 qemu_timeval tv;
174 if (!qtest_log_fp || !qtest_opened) {
175 return;
178 qtest_get_time(&tv);
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, ...)
186 va_list ap;
187 char buffer[1024];
188 size_t len;
190 va_start(ap, fmt);
191 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
192 va_end(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;
218 g_assert(words);
220 command = words[0];
222 if (qtest_log_fp) {
223 qemu_timeval tv;
224 int i;
226 qtest_get_time(&tv);
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");
235 g_assert(command);
236 if (strcmp(words[0], "irq_intercept_out") == 0
237 || strcmp(words[0], "irq_intercept_in") == 0) {
238 DeviceState *dev;
240 g_assert(words[1]);
241 dev = DEVICE(object_resolve_path(words[1], NULL));
242 if (!dev) {
243 qtest_send_prefix(chr);
244 qtest_send(chr, "FAIL Unknown device\n");
245 return;
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");
252 } else {
253 qtest_send(chr, "OK\n");
255 return;
258 if (words[0][14] == 'o') {
259 qemu_irq_intercept_out(&dev->gpio_out, qtest_irq_handler, dev->num_gpio_out);
260 } else {
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) {
270 uint16_t addr;
271 uint32_t value;
273 g_assert(words[1] && words[2]);
274 addr = strtoul(words[1], NULL, 0);
275 value = strtoul(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) {
289 uint16_t addr;
290 uint32_t value = -1U;
292 g_assert(words[1]);
293 addr = strtoul(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) {
308 uint64_t addr;
309 uint64_t value;
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;
320 tswap16s(&data);
321 cpu_physical_memory_write(addr, &data, 2);
322 } else if (words[0][5] == 'l') {
323 uint32_t data = value;
324 tswap32s(&data);
325 cpu_physical_memory_write(addr, &data, 4);
326 } else if (words[0][5] == 'q') {
327 uint64_t data = value;
328 tswap64s(&data);
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) {
337 uint64_t addr;
338 uint64_t value = UINT64_C(-1);
340 g_assert(words[1]);
341 addr = strtoull(words[1], NULL, 0);
343 if (words[0][4] == 'b') {
344 uint8_t data;
345 cpu_physical_memory_read(addr, &data, 1);
346 value = data;
347 } else if (words[0][4] == 'w') {
348 uint16_t data;
349 cpu_physical_memory_read(addr, &data, 2);
350 value = tswap16(data);
351 } else if (words[0][4] == 'l') {
352 uint32_t data;
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);
357 tswap64s(&value);
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;
363 uint8_t *data;
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");
379 g_free(data);
380 } else if (strcmp(words[0], "write") == 0) {
381 uint64_t addr, len, i;
382 uint8_t *data;
383 size_t data_len;
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]);
390 if (data_len < 3) {
391 qtest_send(chr, "ERR invalid argument size\n");
392 return;
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]);
400 } else {
401 data[i] = 0;
404 cpu_physical_memory_write(addr, data, len);
405 g_free(data);
407 qtest_send_prefix(chr);
408 qtest_send(chr, "OK\n");
409 } else if (strcmp(words[0], "clock_step") == 0) {
410 int64_t ns;
412 if (words[1]) {
413 ns = strtoll(words[1], NULL, 0);
414 } else {
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) {
421 int64_t ns;
423 g_assert(words[1]);
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));
428 } else {
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)
436 char *end;
438 while ((end = strchr(inbuf->str, '\n')) != NULL) {
439 size_t offset;
440 GString *cmd;
441 gchar **words;
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);
450 g_strfreev(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)
466 return 1024;
469 static void qtest_event(void *opaque, int event)
471 int i;
473 switch (event) {
474 case CHR_EVENT_OPENED:
476 * We used to call qemu_system_reset() here, hoping we could
477 * use the same process for multiple tests that way. Never
478 * used. Injects an extra reset even when it's not used, and
479 * that can mess up tests, e.g. -boot once.
481 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
482 irq_levels[i] = 0;
484 qemu_gettimeofday(&start_time);
485 qtest_opened = true;
486 if (qtest_log_fp) {
487 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
488 start_time.tv_sec, (long) start_time.tv_usec);
490 break;
491 case CHR_EVENT_CLOSED:
492 qtest_opened = false;
493 if (qtest_log_fp) {
494 qemu_timeval tv;
495 qtest_get_time(&tv);
496 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
497 tv.tv_sec, (long) tv.tv_usec);
499 break;
500 default:
501 break;
505 int qtest_init(void)
507 CharDriverState *chr;
509 g_assert(qtest_chrdev != NULL);
511 configure_icount("0");
512 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
514 qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
515 qemu_chr_fe_set_echo(chr, true);
517 inbuf = g_string_new("");
519 if (qtest_log) {
520 if (strcmp(qtest_log, "none") != 0) {
521 qtest_log_fp = fopen(qtest_log, "w+");
523 } else {
524 qtest_log_fp = stderr;
527 qtest_chr = chr;
529 return 0;