blob: 649f7b26c63377f65cd8611ffcde9cf5781cc841 [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"
Markus Armbrusterda34e652016-03-14 09:01:28 +010015#include "qapi/error.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010016#include "qemu-common.h"
17#include "cpu.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010018#include "sysemu/qtest.h"
Paolo Bonzini20288342012-03-28 15:42:03 +020019#include "hw/qdev.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020020#include "sysemu/char.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010021#include "exec/ioport.h"
22#include "exec/memory.h"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020023#include "hw/irq.h"
Eduardo Habkost3a6ce512014-09-26 17:45:26 -030024#include "sysemu/accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/sysemu.h"
26#include "sysemu/cpus.h"
Sebastian Tanase1ad95802014-07-25 11:56:28 +020027#include "qemu/config-file.h"
28#include "qemu/option.h"
29#include "qemu/error-report.h"
Laurent Vivieraa15f492016-09-13 14:52:43 +020030#include "qemu/cutils.h"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020031
32#define MAX_IRQ 256
33
liguangd5286af2013-01-24 13:03:27 +080034bool qtest_allowed;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020035
Paolo Bonzini20288342012-03-28 15:42:03 +020036static DeviceState *irq_intercept_dev;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020037static FILE *qtest_log_fp;
38static CharDriverState *qtest_chr;
39static GString *inbuf;
40static int irq_levels[MAX_IRQ];
Anthony Liguori6e924662012-03-30 14:04:04 -050041static qemu_timeval start_time;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020042static bool qtest_opened;
43
Anthony Liguori6b7cff72012-03-30 12:53:54 -050044#define FMT_timeval "%ld.%06ld"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020045
46/**
47 * QTest Protocol
48 *
49 * Line based protocol, request/response based. Server can send async messages
50 * so clients should always handle many async messages before the response
51 * comes in.
52 *
53 * Valid requests
54 *
Paolo Bonzini8156be52012-03-28 15:42:04 +020055 * Clock management:
56 *
Alex Blighbc72ad62013-08-21 16:03:08 +010057 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
Paolo Bonzini8156be52012-03-28 15:42:04 +020058 * let you adjust the value of the clock (monotonically). All the commands
59 * return the current value of the clock in nanoseconds.
60 *
61 * > clock_step
62 * < OK VALUE
63 *
64 * Advance the clock to the next deadline. Useful when waiting for
65 * asynchronous events.
66 *
67 * > clock_step NS
68 * < OK VALUE
69 *
70 * Advance the clock by NS nanoseconds.
71 *
72 * > clock_set NS
73 * < OK VALUE
74 *
75 * Advance the clock to NS nanoseconds (do nothing if it's already past).
76 *
77 * PIO and memory access:
78 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020079 * > outb ADDR VALUE
80 * < OK
81 *
82 * > outw ADDR VALUE
83 * < OK
84 *
85 * > outl ADDR VALUE
86 * < OK
87 *
88 * > inb ADDR
89 * < OK VALUE
90 *
91 * > inw ADDR
92 * < OK VALUE
93 *
94 * > inl ADDR
95 * < OK VALUE
96 *
Andreas Färber872536b2013-02-16 22:44:03 +010097 * > writeb ADDR VALUE
98 * < OK
99 *
100 * > writew ADDR VALUE
101 * < OK
102 *
103 * > writel ADDR VALUE
104 * < OK
105 *
106 * > writeq ADDR VALUE
107 * < OK
108 *
109 * > readb ADDR
110 * < OK VALUE
111 *
112 * > readw ADDR
113 * < OK VALUE
114 *
115 * > readl ADDR
116 * < OK VALUE
117 *
118 * > readq ADDR
119 * < OK VALUE
120 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200121 * > read ADDR SIZE
122 * < OK DATA
123 *
124 * > write ADDR SIZE DATA
125 * < OK
126 *
John Snow7a6a7402015-05-22 14:13:44 -0400127 * > b64read ADDR SIZE
128 * < OK B64_DATA
129 *
130 * > b64write ADDR SIZE B64_DATA
131 * < OK
132 *
John Snow4d007962015-05-22 14:13:44 -0400133 * > memset ADDR SIZE VALUE
134 * < OK
135 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200136 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100137 * For 'memset' a zero size is permitted and does nothing.
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200138 *
139 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
140 * than the expected size, the value will be zero filled at the end of the data
141 * sequence.
142 *
John Snow7a6a7402015-05-22 14:13:44 -0400143 * B64_DATA is an arbitrarily long base64 encoded string.
144 * If the sizes do not match, the data will be truncated.
145 *
Paolo Bonzini20288342012-03-28 15:42:03 +0200146 * IRQ management:
147 *
148 * > irq_intercept_in QOM-PATH
149 * < OK
150 *
151 * > irq_intercept_out QOM-PATH
152 * < OK
153 *
154 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
155 * QOM-PATH. When the pin is triggered, one of the following async messages
156 * will be printed to the qtest stream:
157 *
158 * IRQ raise NUM
159 * IRQ lower NUM
160 *
161 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
162 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
163 * NUM=0 even though it is remapped to GSI 2).
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200164 */
165
166static int hex2nib(char ch)
167{
168 if (ch >= '0' && ch <= '9') {
169 return ch - '0';
170 } else if (ch >= 'a' && ch <= 'f') {
171 return 10 + (ch - 'a');
172 } else if (ch >= 'A' && ch <= 'F') {
Sergey Fedorov2a802aa2014-05-27 16:15:20 +0400173 return 10 + (ch - 'A');
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200174 } else {
175 return -1;
176 }
177}
178
Anthony Liguori6e924662012-03-30 14:04:04 -0500179static void qtest_get_time(qemu_timeval *tv)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200180{
Anthony Liguori6e924662012-03-30 14:04:04 -0500181 qemu_gettimeofday(tv);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200182 tv->tv_sec -= start_time.tv_sec;
183 tv->tv_usec -= start_time.tv_usec;
184 if (tv->tv_usec < 0) {
185 tv->tv_usec += 1000000;
186 tv->tv_sec -= 1;
187 }
188}
189
190static void qtest_send_prefix(CharDriverState *chr)
191{
Anthony Liguori6e924662012-03-30 14:04:04 -0500192 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200193
194 if (!qtest_log_fp || !qtest_opened) {
195 return;
196 }
197
198 qtest_get_time(&tv);
199 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700200 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200201}
202
John Snow7a6a7402015-05-22 14:13:44 -0400203static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
204{
205 va_list ap;
206
207 if (!qtest_log_fp || !qtest_opened) {
208 return;
209 }
210
211 qtest_send_prefix(NULL);
212
213 va_start(ap, fmt);
214 vfprintf(qtest_log_fp, fmt, ap);
215 va_end(ap);
216}
217
John Snow332cc7e2015-05-22 14:13:43 -0400218static void do_qtest_send(CharDriverState *chr, const char *str, size_t len)
219{
220 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
221 if (qtest_log_fp && qtest_opened) {
222 fprintf(qtest_log_fp, "%s", str);
223 }
224}
225
226static void qtest_send(CharDriverState *chr, const char *str)
227{
228 do_qtest_send(chr, str, strlen(str));
229}
230
231static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharDriverState *chr,
232 const char *fmt, ...)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200233{
234 va_list ap;
John Snow332cc7e2015-05-22 14:13:43 -0400235 gchar *buffer;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200236
237 va_start(ap, fmt);
John Snow332cc7e2015-05-22 14:13:43 -0400238 buffer = g_strdup_vprintf(fmt, ap);
239 qtest_send(chr, buffer);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200240 va_end(ap);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200241}
242
Paolo Bonzini20288342012-03-28 15:42:03 +0200243static void qtest_irq_handler(void *opaque, int n, int level)
244{
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700245 qemu_irq old_irq = *(qemu_irq *)opaque;
246 qemu_set_irq(old_irq, level);
Paolo Bonzini20288342012-03-28 15:42:03 +0200247
248 if (irq_levels[n] != level) {
249 CharDriverState *chr = qtest_chr;
250 irq_levels[n] = level;
251 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400252 qtest_sendf(chr, "IRQ %s %d\n",
253 level ? "raise" : "lower", n);
Paolo Bonzini20288342012-03-28 15:42:03 +0200254 }
255}
256
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200257static void qtest_process_command(CharDriverState *chr, gchar **words)
258{
259 const gchar *command;
260
261 g_assert(words);
262
263 command = words[0];
264
265 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500266 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200267 int i;
268
269 qtest_get_time(&tv);
270 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700271 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200272 for (i = 0; words[i]; i++) {
273 fprintf(qtest_log_fp, " %s", words[i]);
274 }
275 fprintf(qtest_log_fp, "\n");
276 }
277
278 g_assert(command);
Paolo Bonzini20288342012-03-28 15:42:03 +0200279 if (strcmp(words[0], "irq_intercept_out") == 0
280 || strcmp(words[0], "irq_intercept_in") == 0) {
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700281 DeviceState *dev;
282 NamedGPIOList *ngl;
Paolo Bonzini20288342012-03-28 15:42:03 +0200283
284 g_assert(words[1]);
285 dev = DEVICE(object_resolve_path(words[1], NULL));
286 if (!dev) {
287 qtest_send_prefix(chr);
288 qtest_send(chr, "FAIL Unknown device\n");
289 return;
290 }
291
292 if (irq_intercept_dev) {
293 qtest_send_prefix(chr);
294 if (irq_intercept_dev != dev) {
295 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
296 } else {
297 qtest_send(chr, "OK\n");
298 }
299 return;
300 }
301
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700302 QLIST_FOREACH(ngl, &dev->gpios, node) {
303 /* We don't support intercept of named GPIOs yet */
304 if (ngl->name) {
305 continue;
306 }
307 if (words[0][14] == 'o') {
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700308 int i;
309 for (i = 0; i < ngl->num_out; ++i) {
310 qemu_irq *disconnected = g_new0(qemu_irq, 1);
311 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
312 disconnected, i);
313
314 *disconnected = qdev_intercept_gpio_out(dev, icpt,
315 ngl->name, i);
316 }
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700317 } else {
318 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
319 ngl->num_in);
320 }
Paolo Bonzini20288342012-03-28 15:42:03 +0200321 }
322 irq_intercept_dev = dev;
323 qtest_send_prefix(chr);
324 qtest_send(chr, "OK\n");
325
326 } else if (strcmp(words[0], "outb") == 0 ||
327 strcmp(words[0], "outw") == 0 ||
328 strcmp(words[0], "outl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200329 unsigned long addr;
330 unsigned long value;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200331
332 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200333 g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
334 g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
335 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200336
337 if (words[0][3] == 'b') {
338 cpu_outb(addr, value);
339 } else if (words[0][3] == 'w') {
340 cpu_outw(addr, value);
341 } else if (words[0][3] == 'l') {
342 cpu_outl(addr, value);
343 }
344 qtest_send_prefix(chr);
345 qtest_send(chr, "OK\n");
346 } else if (strcmp(words[0], "inb") == 0 ||
347 strcmp(words[0], "inw") == 0 ||
348 strcmp(words[0], "inl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200349 unsigned long addr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200350 uint32_t value = -1U;
351
352 g_assert(words[1]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200353 g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
354 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200355
356 if (words[0][2] == 'b') {
357 value = cpu_inb(addr);
358 } else if (words[0][2] == 'w') {
359 value = cpu_inw(addr);
360 } else if (words[0][2] == 'l') {
361 value = cpu_inl(addr);
362 }
363 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400364 qtest_sendf(chr, "OK 0x%04x\n", value);
Andreas Färber872536b2013-02-16 22:44:03 +0100365 } else if (strcmp(words[0], "writeb") == 0 ||
366 strcmp(words[0], "writew") == 0 ||
367 strcmp(words[0], "writel") == 0 ||
368 strcmp(words[0], "writeq") == 0) {
369 uint64_t addr;
370 uint64_t value;
371
372 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200373 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
374 g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100375
376 if (words[0][5] == 'b') {
377 uint8_t data = value;
378 cpu_physical_memory_write(addr, &data, 1);
379 } else if (words[0][5] == 'w') {
380 uint16_t data = value;
381 tswap16s(&data);
382 cpu_physical_memory_write(addr, &data, 2);
383 } else if (words[0][5] == 'l') {
384 uint32_t data = value;
385 tswap32s(&data);
386 cpu_physical_memory_write(addr, &data, 4);
387 } else if (words[0][5] == 'q') {
388 uint64_t data = value;
389 tswap64s(&data);
390 cpu_physical_memory_write(addr, &data, 8);
391 }
392 qtest_send_prefix(chr);
393 qtest_send(chr, "OK\n");
394 } else if (strcmp(words[0], "readb") == 0 ||
395 strcmp(words[0], "readw") == 0 ||
396 strcmp(words[0], "readl") == 0 ||
397 strcmp(words[0], "readq") == 0) {
398 uint64_t addr;
399 uint64_t value = UINT64_C(-1);
400
401 g_assert(words[1]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200402 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100403
404 if (words[0][4] == 'b') {
405 uint8_t data;
406 cpu_physical_memory_read(addr, &data, 1);
407 value = data;
408 } else if (words[0][4] == 'w') {
409 uint16_t data;
410 cpu_physical_memory_read(addr, &data, 2);
411 value = tswap16(data);
412 } else if (words[0][4] == 'l') {
413 uint32_t data;
414 cpu_physical_memory_read(addr, &data, 4);
415 value = tswap32(data);
416 } else if (words[0][4] == 'q') {
417 cpu_physical_memory_read(addr, &value, 8);
418 tswap64s(&value);
419 }
420 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400421 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200422 } else if (strcmp(words[0], "read") == 0) {
423 uint64_t addr, len, i;
424 uint8_t *data;
John Snow5560b852015-05-22 14:13:44 -0400425 char *enc;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200426
427 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200428 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
429 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200430
431 data = g_malloc(len);
432 cpu_physical_memory_read(addr, data, len);
433
John Snow5560b852015-05-22 14:13:44 -0400434 enc = g_malloc(2 * len + 1);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200435 for (i = 0; i < len; i++) {
John Snow5560b852015-05-22 14:13:44 -0400436 sprintf(&enc[i * 2], "%02x", data[i]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200437 }
John Snow5560b852015-05-22 14:13:44 -0400438
439 qtest_send_prefix(chr);
440 qtest_sendf(chr, "OK 0x%s\n", enc);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200441
442 g_free(data);
John Snow5560b852015-05-22 14:13:44 -0400443 g_free(enc);
John Snow7a6a7402015-05-22 14:13:44 -0400444 } else if (strcmp(words[0], "b64read") == 0) {
445 uint64_t addr, len;
446 uint8_t *data;
447 gchar *b64_data;
448
449 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200450 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
451 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400452
453 data = g_malloc(len);
454 cpu_physical_memory_read(addr, data, len);
455 b64_data = g_base64_encode(data, len);
456 qtest_send_prefix(chr);
457 qtest_sendf(chr, "OK %s\n", b64_data);
458
459 g_free(data);
460 g_free(b64_data);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200461 } else if (strcmp(words[0], "write") == 0) {
462 uint64_t addr, len, i;
463 uint8_t *data;
464 size_t data_len;
465
466 g_assert(words[1] && words[2] && words[3]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200467 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
468 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200469
470 data_len = strlen(words[3]);
471 if (data_len < 3) {
472 qtest_send(chr, "ERR invalid argument size\n");
473 return;
474 }
475
476 data = g_malloc(len);
477 for (i = 0; i < len; i++) {
478 if ((i * 2 + 4) <= data_len) {
479 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
480 data[i] |= hex2nib(words[3][i * 2 + 3]);
481 } else {
482 data[i] = 0;
483 }
484 }
485 cpu_physical_memory_write(addr, data, len);
486 g_free(data);
487
488 qtest_send_prefix(chr);
489 qtest_send(chr, "OK\n");
John Snow4d007962015-05-22 14:13:44 -0400490 } else if (strcmp(words[0], "memset") == 0) {
491 uint64_t addr, len;
492 uint8_t *data;
Laurent Vivieraa15f492016-09-13 14:52:43 +0200493 unsigned long pattern;
John Snow4d007962015-05-22 14:13:44 -0400494
495 g_assert(words[1] && words[2] && words[3]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200496 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
497 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
498 g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);
John Snow4d007962015-05-22 14:13:44 -0400499
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100500 if (len) {
501 data = g_malloc(len);
502 memset(data, pattern, len);
503 cpu_physical_memory_write(addr, data, len);
504 g_free(data);
505 }
John Snow4d007962015-05-22 14:13:44 -0400506
507 qtest_send_prefix(chr);
508 qtest_send(chr, "OK\n");
John Snow7a6a7402015-05-22 14:13:44 -0400509 } else if (strcmp(words[0], "b64write") == 0) {
510 uint64_t addr, len;
511 uint8_t *data;
512 size_t data_len;
513 gsize out_len;
514
515 g_assert(words[1] && words[2] && words[3]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200516 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
517 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400518
519 data_len = strlen(words[3]);
520 if (data_len < 3) {
521 qtest_send(chr, "ERR invalid argument size\n");
522 return;
523 }
524
525 data = g_base64_decode_inplace(words[3], &out_len);
526 if (out_len != len) {
527 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
528 "found %zu)\n",
529 len, out_len);
530 out_len = MIN(out_len, len);
531 }
532
533 cpu_physical_memory_write(addr, data, out_len);
534
535 qtest_send_prefix(chr);
536 qtest_send(chr, "OK\n");
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200537 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200538 int64_t ns;
539
540 if (words[1]) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200541 g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200542 } else {
Alex Bligh40daca52013-08-21 16:03:02 +0100543 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200544 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100545 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200546 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));
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200549 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200550 int64_t ns;
551
552 g_assert(words[1]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200553 g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200554 qtest_clock_warp(ns);
555 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400556 qtest_sendf(chr, "OK %"PRIi64"\n",
557 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200558 } else {
559 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400560 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200561 }
562}
563
564static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
565{
566 char *end;
567
568 while ((end = strchr(inbuf->str, '\n')) != NULL) {
569 size_t offset;
570 GString *cmd;
571 gchar **words;
572
573 offset = end - inbuf->str;
574
575 cmd = g_string_new_len(inbuf->str, offset);
576 g_string_erase(inbuf, 0, offset + 1);
577
578 words = g_strsplit(cmd->str, " ", 0);
579 qtest_process_command(chr, words);
580 g_strfreev(words);
581
582 g_string_free(cmd, TRUE);
583 }
584}
585
586static void qtest_read(void *opaque, const uint8_t *buf, int size)
587{
588 CharDriverState *chr = opaque;
589
590 g_string_append_len(inbuf, (const gchar *)buf, size);
591 qtest_process_inbuf(chr, inbuf);
592}
593
594static int qtest_can_read(void *opaque)
595{
596 return 1024;
597}
598
599static void qtest_event(void *opaque, int event)
600{
601 int i;
602
603 switch (event) {
604 case CHR_EVENT_OPENED:
Markus Armbrusterba646ff2013-06-26 15:52:12 +0200605 /*
606 * We used to call qemu_system_reset() here, hoping we could
607 * use the same process for multiple tests that way. Never
608 * used. Injects an extra reset even when it's not used, and
609 * that can mess up tests, e.g. -boot once.
610 */
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200611 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
612 irq_levels[i] = 0;
613 }
Anthony Liguori6e924662012-03-30 14:04:04 -0500614 qemu_gettimeofday(&start_time);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200615 qtest_opened = true;
616 if (qtest_log_fp) {
617 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700618 (long) start_time.tv_sec, (long) start_time.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200619 }
620 break;
621 case CHR_EVENT_CLOSED:
622 qtest_opened = false;
623 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500624 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200625 qtest_get_time(&tv);
626 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700627 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200628 }
629 break;
630 default:
631 break;
632 }
633}
634
Eduardo Habkostf6a1ef62014-09-26 17:45:30 -0300635static int qtest_init_accel(MachineState *ms)
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200636{
Markus Armbrusterb3adf5a2015-02-13 15:48:19 +0100637 QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
638 &error_abort);
639 qemu_opt_set(opts, "shift", "0", &error_abort);
640 configure_icount(opts, &error_abort);
641 qemu_opts_del(opts);
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200642 return 0;
643}
644
Fam Zheng23802b42014-02-10 09:28:02 +0800645void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200646{
647 CharDriverState *chr;
648
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200649 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
650
Fam Zheng23802b42014-02-10 09:28:02 +0800651 if (chr == NULL) {
652 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
653 qtest_chrdev);
654 return;
655 }
656
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200657 if (qtest_log) {
658 if (strcmp(qtest_log, "none") != 0) {
659 qtest_log_fp = fopen(qtest_log, "w+");
660 }
661 } else {
662 qtest_log_fp = stderr;
663 }
664
Li Liu107684c2014-10-22 10:26:47 +0800665 qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
666 qemu_chr_fe_set_echo(chr, true);
667
668 inbuf = g_string_new("");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200669 qtest_chr = chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200670}
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200671
672bool qtest_driver(void)
673{
674 return qtest_chr;
675}
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300676
677static void qtest_accel_class_init(ObjectClass *oc, void *data)
678{
679 AccelClass *ac = ACCEL_CLASS(oc);
680 ac->name = "QTest";
681 ac->available = qtest_available;
Eduardo Habkost0d15da82014-09-26 17:45:29 -0300682 ac->init_machine = qtest_init_accel;
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300683 ac->allowed = &qtest_allowed;
684}
685
686#define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
687
688static const TypeInfo qtest_accel_type = {
689 .name = TYPE_QTEST_ACCEL,
690 .parent = TYPE_ACCEL,
691 .class_init = qtest_accel_class_init,
692};
693
694static void qtest_type_init(void)
695{
696 type_register_static(&qtest_accel_type);
697}
698
699type_init(qtest_type_init);