blob: 58a7732ef3e90c5bf850846054f60dba9e3e4ac2 [file] [log] [blame]
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +02001/*
2 * Test Server
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 *
12 */
13
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010015#include "sysemu/qtest.h"
Paolo Bonzini20288342012-03-28 15:42:03 +020016#include "hw/qdev.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020017#include "sysemu/char.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010018#include "exec/ioport.h"
19#include "exec/memory.h"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020020#include "hw/irq.h"
Eduardo Habkost3a6ce512014-09-26 17:45:26 -030021#include "sysemu/accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/sysemu.h"
23#include "sysemu/cpus.h"
Sebastian Tanase1ad95802014-07-25 11:56:28 +020024#include "qemu/config-file.h"
25#include "qemu/option.h"
26#include "qemu/error-report.h"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020027
28#define MAX_IRQ 256
29
liguangd5286af2013-01-24 13:03:27 +080030bool qtest_allowed;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020031
Paolo Bonzini20288342012-03-28 15:42:03 +020032static DeviceState *irq_intercept_dev;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020033static FILE *qtest_log_fp;
34static CharDriverState *qtest_chr;
35static GString *inbuf;
36static int irq_levels[MAX_IRQ];
Anthony Liguori6e924662012-03-30 14:04:04 -050037static qemu_timeval start_time;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020038static bool qtest_opened;
39
Anthony Liguori6b7cff72012-03-30 12:53:54 -050040#define FMT_timeval "%ld.%06ld"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020041
42/**
43 * QTest Protocol
44 *
45 * Line based protocol, request/response based. Server can send async messages
46 * so clients should always handle many async messages before the response
47 * comes in.
48 *
49 * Valid requests
50 *
Paolo Bonzini8156be52012-03-28 15:42:04 +020051 * Clock management:
52 *
Alex Blighbc72ad62013-08-21 16:03:08 +010053 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
Paolo Bonzini8156be52012-03-28 15:42:04 +020054 * let you adjust the value of the clock (monotonically). All the commands
55 * return the current value of the clock in nanoseconds.
56 *
57 * > clock_step
58 * < OK VALUE
59 *
60 * Advance the clock to the next deadline. Useful when waiting for
61 * asynchronous events.
62 *
63 * > clock_step NS
64 * < OK VALUE
65 *
66 * Advance the clock by NS nanoseconds.
67 *
68 * > clock_set NS
69 * < OK VALUE
70 *
71 * Advance the clock to NS nanoseconds (do nothing if it's already past).
72 *
73 * PIO and memory access:
74 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020075 * > outb ADDR VALUE
76 * < OK
77 *
78 * > outw ADDR VALUE
79 * < OK
80 *
81 * > outl ADDR VALUE
82 * < OK
83 *
84 * > inb ADDR
85 * < OK VALUE
86 *
87 * > inw ADDR
88 * < OK VALUE
89 *
90 * > inl ADDR
91 * < OK VALUE
92 *
Andreas Färber872536b2013-02-16 22:44:03 +010093 * > writeb ADDR VALUE
94 * < OK
95 *
96 * > writew ADDR VALUE
97 * < OK
98 *
99 * > writel ADDR VALUE
100 * < OK
101 *
102 * > writeq ADDR VALUE
103 * < OK
104 *
105 * > readb ADDR
106 * < OK VALUE
107 *
108 * > readw ADDR
109 * < OK VALUE
110 *
111 * > readl ADDR
112 * < OK VALUE
113 *
114 * > readq ADDR
115 * < OK VALUE
116 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200117 * > read ADDR SIZE
118 * < OK DATA
119 *
120 * > write ADDR SIZE DATA
121 * < OK
122 *
John Snow7a6a7402015-05-22 14:13:44 -0400123 * > b64read ADDR SIZE
124 * < OK B64_DATA
125 *
126 * > b64write ADDR SIZE B64_DATA
127 * < OK
128 *
John Snow4d007962015-05-22 14:13:44 -0400129 * > memset ADDR SIZE VALUE
130 * < OK
131 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200132 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
133 *
134 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
135 * than the expected size, the value will be zero filled at the end of the data
136 * sequence.
137 *
John Snow7a6a7402015-05-22 14:13:44 -0400138 * B64_DATA is an arbitrarily long base64 encoded string.
139 * If the sizes do not match, the data will be truncated.
140 *
Paolo Bonzini20288342012-03-28 15:42:03 +0200141 * IRQ management:
142 *
143 * > irq_intercept_in QOM-PATH
144 * < OK
145 *
146 * > irq_intercept_out QOM-PATH
147 * < OK
148 *
149 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
150 * QOM-PATH. When the pin is triggered, one of the following async messages
151 * will be printed to the qtest stream:
152 *
153 * IRQ raise NUM
154 * IRQ lower NUM
155 *
156 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
157 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
158 * NUM=0 even though it is remapped to GSI 2).
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200159 */
160
161static int hex2nib(char ch)
162{
163 if (ch >= '0' && ch <= '9') {
164 return ch - '0';
165 } else if (ch >= 'a' && ch <= 'f') {
166 return 10 + (ch - 'a');
167 } else if (ch >= 'A' && ch <= 'F') {
Sergey Fedorov2a802aa2014-05-27 16:15:20 +0400168 return 10 + (ch - 'A');
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200169 } else {
170 return -1;
171 }
172}
173
Anthony Liguori6e924662012-03-30 14:04:04 -0500174static void qtest_get_time(qemu_timeval *tv)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200175{
Anthony Liguori6e924662012-03-30 14:04:04 -0500176 qemu_gettimeofday(tv);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200177 tv->tv_sec -= start_time.tv_sec;
178 tv->tv_usec -= start_time.tv_usec;
179 if (tv->tv_usec < 0) {
180 tv->tv_usec += 1000000;
181 tv->tv_sec -= 1;
182 }
183}
184
185static void qtest_send_prefix(CharDriverState *chr)
186{
Anthony Liguori6e924662012-03-30 14:04:04 -0500187 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200188
189 if (!qtest_log_fp || !qtest_opened) {
190 return;
191 }
192
193 qtest_get_time(&tv);
194 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700195 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200196}
197
John Snow7a6a7402015-05-22 14:13:44 -0400198static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
199{
200 va_list ap;
201
202 if (!qtest_log_fp || !qtest_opened) {
203 return;
204 }
205
206 qtest_send_prefix(NULL);
207
208 va_start(ap, fmt);
209 vfprintf(qtest_log_fp, fmt, ap);
210 va_end(ap);
211}
212
John Snow332cc7e2015-05-22 14:13:43 -0400213static void do_qtest_send(CharDriverState *chr, const char *str, size_t len)
214{
215 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
216 if (qtest_log_fp && qtest_opened) {
217 fprintf(qtest_log_fp, "%s", str);
218 }
219}
220
221static void qtest_send(CharDriverState *chr, const char *str)
222{
223 do_qtest_send(chr, str, strlen(str));
224}
225
226static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharDriverState *chr,
227 const char *fmt, ...)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200228{
229 va_list ap;
John Snow332cc7e2015-05-22 14:13:43 -0400230 gchar *buffer;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200231
232 va_start(ap, fmt);
John Snow332cc7e2015-05-22 14:13:43 -0400233 buffer = g_strdup_vprintf(fmt, ap);
234 qtest_send(chr, buffer);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200235 va_end(ap);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200236}
237
Paolo Bonzini20288342012-03-28 15:42:03 +0200238static void qtest_irq_handler(void *opaque, int n, int level)
239{
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700240 qemu_irq old_irq = *(qemu_irq *)opaque;
241 qemu_set_irq(old_irq, level);
Paolo Bonzini20288342012-03-28 15:42:03 +0200242
243 if (irq_levels[n] != level) {
244 CharDriverState *chr = qtest_chr;
245 irq_levels[n] = level;
246 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400247 qtest_sendf(chr, "IRQ %s %d\n",
248 level ? "raise" : "lower", n);
Paolo Bonzini20288342012-03-28 15:42:03 +0200249 }
250}
251
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200252static void qtest_process_command(CharDriverState *chr, gchar **words)
253{
254 const gchar *command;
255
256 g_assert(words);
257
258 command = words[0];
259
260 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500261 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200262 int i;
263
264 qtest_get_time(&tv);
265 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700266 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200267 for (i = 0; words[i]; i++) {
268 fprintf(qtest_log_fp, " %s", words[i]);
269 }
270 fprintf(qtest_log_fp, "\n");
271 }
272
273 g_assert(command);
Paolo Bonzini20288342012-03-28 15:42:03 +0200274 if (strcmp(words[0], "irq_intercept_out") == 0
275 || strcmp(words[0], "irq_intercept_in") == 0) {
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700276 DeviceState *dev;
277 NamedGPIOList *ngl;
Paolo Bonzini20288342012-03-28 15:42:03 +0200278
279 g_assert(words[1]);
280 dev = DEVICE(object_resolve_path(words[1], NULL));
281 if (!dev) {
282 qtest_send_prefix(chr);
283 qtest_send(chr, "FAIL Unknown device\n");
284 return;
285 }
286
287 if (irq_intercept_dev) {
288 qtest_send_prefix(chr);
289 if (irq_intercept_dev != dev) {
290 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
291 } else {
292 qtest_send(chr, "OK\n");
293 }
294 return;
295 }
296
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700297 QLIST_FOREACH(ngl, &dev->gpios, node) {
298 /* We don't support intercept of named GPIOs yet */
299 if (ngl->name) {
300 continue;
301 }
302 if (words[0][14] == 'o') {
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700303 int i;
304 for (i = 0; i < ngl->num_out; ++i) {
305 qemu_irq *disconnected = g_new0(qemu_irq, 1);
306 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
307 disconnected, i);
308
309 *disconnected = qdev_intercept_gpio_out(dev, icpt,
310 ngl->name, i);
311 }
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700312 } else {
313 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
314 ngl->num_in);
315 }
Paolo Bonzini20288342012-03-28 15:42:03 +0200316 }
317 irq_intercept_dev = dev;
318 qtest_send_prefix(chr);
319 qtest_send(chr, "OK\n");
320
321 } else if (strcmp(words[0], "outb") == 0 ||
322 strcmp(words[0], "outw") == 0 ||
323 strcmp(words[0], "outl") == 0) {
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200324 uint16_t addr;
325 uint32_t value;
326
327 g_assert(words[1] && words[2]);
Peter Maydell56863d42013-04-21 14:30:03 +0100328 addr = strtoul(words[1], NULL, 0);
329 value = strtoul(words[2], NULL, 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200330
331 if (words[0][3] == 'b') {
332 cpu_outb(addr, value);
333 } else if (words[0][3] == 'w') {
334 cpu_outw(addr, value);
335 } else if (words[0][3] == 'l') {
336 cpu_outl(addr, value);
337 }
338 qtest_send_prefix(chr);
339 qtest_send(chr, "OK\n");
340 } else if (strcmp(words[0], "inb") == 0 ||
341 strcmp(words[0], "inw") == 0 ||
342 strcmp(words[0], "inl") == 0) {
343 uint16_t addr;
344 uint32_t value = -1U;
345
346 g_assert(words[1]);
Peter Maydell56863d42013-04-21 14:30:03 +0100347 addr = strtoul(words[1], NULL, 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200348
349 if (words[0][2] == 'b') {
350 value = cpu_inb(addr);
351 } else if (words[0][2] == 'w') {
352 value = cpu_inw(addr);
353 } else if (words[0][2] == 'l') {
354 value = cpu_inl(addr);
355 }
356 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400357 qtest_sendf(chr, "OK 0x%04x\n", value);
Andreas Färber872536b2013-02-16 22:44:03 +0100358 } else if (strcmp(words[0], "writeb") == 0 ||
359 strcmp(words[0], "writew") == 0 ||
360 strcmp(words[0], "writel") == 0 ||
361 strcmp(words[0], "writeq") == 0) {
362 uint64_t addr;
363 uint64_t value;
364
365 g_assert(words[1] && words[2]);
366 addr = strtoull(words[1], NULL, 0);
367 value = strtoull(words[2], NULL, 0);
368
369 if (words[0][5] == 'b') {
370 uint8_t data = value;
371 cpu_physical_memory_write(addr, &data, 1);
372 } else if (words[0][5] == 'w') {
373 uint16_t data = value;
374 tswap16s(&data);
375 cpu_physical_memory_write(addr, &data, 2);
376 } else if (words[0][5] == 'l') {
377 uint32_t data = value;
378 tswap32s(&data);
379 cpu_physical_memory_write(addr, &data, 4);
380 } else if (words[0][5] == 'q') {
381 uint64_t data = value;
382 tswap64s(&data);
383 cpu_physical_memory_write(addr, &data, 8);
384 }
385 qtest_send_prefix(chr);
386 qtest_send(chr, "OK\n");
387 } else if (strcmp(words[0], "readb") == 0 ||
388 strcmp(words[0], "readw") == 0 ||
389 strcmp(words[0], "readl") == 0 ||
390 strcmp(words[0], "readq") == 0) {
391 uint64_t addr;
392 uint64_t value = UINT64_C(-1);
393
394 g_assert(words[1]);
395 addr = strtoull(words[1], NULL, 0);
396
397 if (words[0][4] == 'b') {
398 uint8_t data;
399 cpu_physical_memory_read(addr, &data, 1);
400 value = data;
401 } else if (words[0][4] == 'w') {
402 uint16_t data;
403 cpu_physical_memory_read(addr, &data, 2);
404 value = tswap16(data);
405 } else if (words[0][4] == 'l') {
406 uint32_t data;
407 cpu_physical_memory_read(addr, &data, 4);
408 value = tswap32(data);
409 } else if (words[0][4] == 'q') {
410 cpu_physical_memory_read(addr, &value, 8);
411 tswap64s(&value);
412 }
413 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400414 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200415 } else if (strcmp(words[0], "read") == 0) {
416 uint64_t addr, len, i;
417 uint8_t *data;
John Snow5560b852015-05-22 14:13:44 -0400418 char *enc;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200419
420 g_assert(words[1] && words[2]);
Andreas Färber5dd6be02013-02-11 17:41:53 +0100421 addr = strtoull(words[1], NULL, 0);
422 len = strtoull(words[2], NULL, 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200423
424 data = g_malloc(len);
425 cpu_physical_memory_read(addr, data, len);
426
John Snow5560b852015-05-22 14:13:44 -0400427 enc = g_malloc(2 * len + 1);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200428 for (i = 0; i < len; i++) {
John Snow5560b852015-05-22 14:13:44 -0400429 sprintf(&enc[i * 2], "%02x", data[i]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200430 }
John Snow5560b852015-05-22 14:13:44 -0400431
432 qtest_send_prefix(chr);
433 qtest_sendf(chr, "OK 0x%s\n", enc);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200434
435 g_free(data);
John Snow5560b852015-05-22 14:13:44 -0400436 g_free(enc);
John Snow7a6a7402015-05-22 14:13:44 -0400437 } else if (strcmp(words[0], "b64read") == 0) {
438 uint64_t addr, len;
439 uint8_t *data;
440 gchar *b64_data;
441
442 g_assert(words[1] && words[2]);
443 addr = strtoull(words[1], NULL, 0);
444 len = strtoull(words[2], NULL, 0);
445
446 data = g_malloc(len);
447 cpu_physical_memory_read(addr, data, len);
448 b64_data = g_base64_encode(data, len);
449 qtest_send_prefix(chr);
450 qtest_sendf(chr, "OK %s\n", b64_data);
451
452 g_free(data);
453 g_free(b64_data);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200454 } else if (strcmp(words[0], "write") == 0) {
455 uint64_t addr, len, i;
456 uint8_t *data;
457 size_t data_len;
458
459 g_assert(words[1] && words[2] && words[3]);
Andreas Färber5dd6be02013-02-11 17:41:53 +0100460 addr = strtoull(words[1], NULL, 0);
461 len = strtoull(words[2], NULL, 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200462
463 data_len = strlen(words[3]);
464 if (data_len < 3) {
465 qtest_send(chr, "ERR invalid argument size\n");
466 return;
467 }
468
469 data = g_malloc(len);
470 for (i = 0; i < len; i++) {
471 if ((i * 2 + 4) <= data_len) {
472 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
473 data[i] |= hex2nib(words[3][i * 2 + 3]);
474 } else {
475 data[i] = 0;
476 }
477 }
478 cpu_physical_memory_write(addr, data, len);
479 g_free(data);
480
481 qtest_send_prefix(chr);
482 qtest_send(chr, "OK\n");
John Snow4d007962015-05-22 14:13:44 -0400483 } else if (strcmp(words[0], "memset") == 0) {
484 uint64_t addr, len;
485 uint8_t *data;
486 uint8_t pattern;
487
488 g_assert(words[1] && words[2] && words[3]);
489 addr = strtoull(words[1], NULL, 0);
490 len = strtoull(words[2], NULL, 0);
491 pattern = strtoull(words[3], NULL, 0);
492
493 data = g_malloc(len);
494 memset(data, pattern, len);
495 cpu_physical_memory_write(addr, data, len);
496 g_free(data);
497
498 qtest_send_prefix(chr);
499 qtest_send(chr, "OK\n");
John Snow7a6a7402015-05-22 14:13:44 -0400500 } else if (strcmp(words[0], "b64write") == 0) {
501 uint64_t addr, len;
502 uint8_t *data;
503 size_t data_len;
504 gsize out_len;
505
506 g_assert(words[1] && words[2] && words[3]);
507 addr = strtoull(words[1], NULL, 0);
508 len = strtoull(words[2], NULL, 0);
509
510 data_len = strlen(words[3]);
511 if (data_len < 3) {
512 qtest_send(chr, "ERR invalid argument size\n");
513 return;
514 }
515
516 data = g_base64_decode_inplace(words[3], &out_len);
517 if (out_len != len) {
518 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
519 "found %zu)\n",
520 len, out_len);
521 out_len = MIN(out_len, len);
522 }
523
524 cpu_physical_memory_write(addr, data, out_len);
525
526 qtest_send_prefix(chr);
527 qtest_send(chr, "OK\n");
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200528 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200529 int64_t ns;
530
531 if (words[1]) {
532 ns = strtoll(words[1], NULL, 0);
533 } else {
Alex Bligh40daca52013-08-21 16:03:02 +0100534 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200535 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100536 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200537 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400538 qtest_sendf(chr, "OK %"PRIi64"\n",
539 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200540 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200541 int64_t ns;
542
543 g_assert(words[1]);
544 ns = strtoll(words[1], NULL, 0);
545 qtest_clock_warp(ns);
546 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400547 qtest_sendf(chr, "OK %"PRIi64"\n",
548 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200549 } else {
550 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400551 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200552 }
553}
554
555static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
556{
557 char *end;
558
559 while ((end = strchr(inbuf->str, '\n')) != NULL) {
560 size_t offset;
561 GString *cmd;
562 gchar **words;
563
564 offset = end - inbuf->str;
565
566 cmd = g_string_new_len(inbuf->str, offset);
567 g_string_erase(inbuf, 0, offset + 1);
568
569 words = g_strsplit(cmd->str, " ", 0);
570 qtest_process_command(chr, words);
571 g_strfreev(words);
572
573 g_string_free(cmd, TRUE);
574 }
575}
576
577static void qtest_read(void *opaque, const uint8_t *buf, int size)
578{
579 CharDriverState *chr = opaque;
580
581 g_string_append_len(inbuf, (const gchar *)buf, size);
582 qtest_process_inbuf(chr, inbuf);
583}
584
585static int qtest_can_read(void *opaque)
586{
587 return 1024;
588}
589
590static void qtest_event(void *opaque, int event)
591{
592 int i;
593
594 switch (event) {
595 case CHR_EVENT_OPENED:
Markus Armbrusterba646ff2013-06-26 15:52:12 +0200596 /*
597 * We used to call qemu_system_reset() here, hoping we could
598 * use the same process for multiple tests that way. Never
599 * used. Injects an extra reset even when it's not used, and
600 * that can mess up tests, e.g. -boot once.
601 */
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200602 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
603 irq_levels[i] = 0;
604 }
Anthony Liguori6e924662012-03-30 14:04:04 -0500605 qemu_gettimeofday(&start_time);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200606 qtest_opened = true;
607 if (qtest_log_fp) {
608 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700609 (long) start_time.tv_sec, (long) start_time.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200610 }
611 break;
612 case CHR_EVENT_CLOSED:
613 qtest_opened = false;
614 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500615 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200616 qtest_get_time(&tv);
617 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700618 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200619 }
620 break;
621 default:
622 break;
623 }
624}
625
Eduardo Habkostf6a1ef62014-09-26 17:45:30 -0300626static int qtest_init_accel(MachineState *ms)
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200627{
Markus Armbrusterb3adf5a2015-02-13 15:48:19 +0100628 QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
629 &error_abort);
630 qemu_opt_set(opts, "shift", "0", &error_abort);
631 configure_icount(opts, &error_abort);
632 qemu_opts_del(opts);
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200633 return 0;
634}
635
Fam Zheng23802b42014-02-10 09:28:02 +0800636void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200637{
638 CharDriverState *chr;
639
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200640 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
641
Fam Zheng23802b42014-02-10 09:28:02 +0800642 if (chr == NULL) {
643 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
644 qtest_chrdev);
645 return;
646 }
647
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200648 if (qtest_log) {
649 if (strcmp(qtest_log, "none") != 0) {
650 qtest_log_fp = fopen(qtest_log, "w+");
651 }
652 } else {
653 qtest_log_fp = stderr;
654 }
655
Li Liu107684c2014-10-22 10:26:47 +0800656 qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
657 qemu_chr_fe_set_echo(chr, true);
658
659 inbuf = g_string_new("");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200660 qtest_chr = chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200661}
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200662
663bool qtest_driver(void)
664{
665 return qtest_chr;
666}
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300667
668static void qtest_accel_class_init(ObjectClass *oc, void *data)
669{
670 AccelClass *ac = ACCEL_CLASS(oc);
671 ac->name = "QTest";
672 ac->available = qtest_available;
Eduardo Habkost0d15da82014-09-26 17:45:29 -0300673 ac->init_machine = qtest_init_accel;
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300674 ac->allowed = &qtest_allowed;
675}
676
677#define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
678
679static const TypeInfo qtest_accel_type = {
680 .name = TYPE_QTEST_ACCEL,
681 .parent = TYPE_ACCEL,
682 .class_init = qtest_accel_class_init,
683};
684
685static void qtest_type_init(void)
686{
687 type_register_static(&qtest_accel_type);
688}
689
690type_init(qtest_type_init);