blob: 22482cc35922b296e611a08955713688d99b28b4 [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"
Laurent Viviereeddd592016-09-13 14:52:45 +020031#ifdef TARGET_PPC64
32#include "hw/ppc/spapr_rtas.h"
33#endif
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020034
35#define MAX_IRQ 256
36
liguangd5286af2013-01-24 13:03:27 +080037bool qtest_allowed;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020038
Paolo Bonzini20288342012-03-28 15:42:03 +020039static DeviceState *irq_intercept_dev;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020040static FILE *qtest_log_fp;
41static CharDriverState *qtest_chr;
42static GString *inbuf;
43static int irq_levels[MAX_IRQ];
Anthony Liguori6e924662012-03-30 14:04:04 -050044static qemu_timeval start_time;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020045static bool qtest_opened;
46
Anthony Liguori6b7cff72012-03-30 12:53:54 -050047#define FMT_timeval "%ld.%06ld"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020048
49/**
50 * QTest Protocol
51 *
52 * Line based protocol, request/response based. Server can send async messages
53 * so clients should always handle many async messages before the response
54 * comes in.
55 *
56 * Valid requests
57 *
Paolo Bonzini8156be52012-03-28 15:42:04 +020058 * Clock management:
59 *
Alex Blighbc72ad62013-08-21 16:03:08 +010060 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
Paolo Bonzini8156be52012-03-28 15:42:04 +020061 * let you adjust the value of the clock (monotonically). All the commands
62 * return the current value of the clock in nanoseconds.
63 *
64 * > clock_step
65 * < OK VALUE
66 *
67 * Advance the clock to the next deadline. Useful when waiting for
68 * asynchronous events.
69 *
70 * > clock_step NS
71 * < OK VALUE
72 *
73 * Advance the clock by NS nanoseconds.
74 *
75 * > clock_set NS
76 * < OK VALUE
77 *
78 * Advance the clock to NS nanoseconds (do nothing if it's already past).
79 *
80 * PIO and memory access:
81 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020082 * > outb ADDR VALUE
83 * < OK
84 *
85 * > outw ADDR VALUE
86 * < OK
87 *
88 * > outl ADDR VALUE
89 * < OK
90 *
91 * > inb ADDR
92 * < OK VALUE
93 *
94 * > inw ADDR
95 * < OK VALUE
96 *
97 * > inl ADDR
98 * < OK VALUE
99 *
Andreas Färber872536b2013-02-16 22:44:03 +0100100 * > writeb ADDR VALUE
101 * < OK
102 *
103 * > writew ADDR VALUE
104 * < OK
105 *
106 * > writel ADDR VALUE
107 * < OK
108 *
109 * > writeq ADDR VALUE
110 * < OK
111 *
112 * > readb ADDR
113 * < OK VALUE
114 *
115 * > readw ADDR
116 * < OK VALUE
117 *
118 * > readl ADDR
119 * < OK VALUE
120 *
121 * > readq ADDR
122 * < OK VALUE
123 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200124 * > read ADDR SIZE
125 * < OK DATA
126 *
127 * > write ADDR SIZE DATA
128 * < OK
129 *
John Snow7a6a7402015-05-22 14:13:44 -0400130 * > b64read ADDR SIZE
131 * < OK B64_DATA
132 *
133 * > b64write ADDR SIZE B64_DATA
134 * < OK
135 *
John Snow4d007962015-05-22 14:13:44 -0400136 * > memset ADDR SIZE VALUE
137 * < OK
138 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200139 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100140 * For 'memset' a zero size is permitted and does nothing.
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200141 *
142 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
143 * than the expected size, the value will be zero filled at the end of the data
144 * sequence.
145 *
John Snow7a6a7402015-05-22 14:13:44 -0400146 * B64_DATA is an arbitrarily long base64 encoded string.
147 * If the sizes do not match, the data will be truncated.
148 *
Paolo Bonzini20288342012-03-28 15:42:03 +0200149 * IRQ management:
150 *
151 * > irq_intercept_in QOM-PATH
152 * < OK
153 *
154 * > irq_intercept_out QOM-PATH
155 * < OK
156 *
157 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
158 * QOM-PATH. When the pin is triggered, one of the following async messages
159 * will be printed to the qtest stream:
160 *
161 * IRQ raise NUM
162 * IRQ lower NUM
163 *
164 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
165 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
166 * NUM=0 even though it is remapped to GSI 2).
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200167 */
168
169static int hex2nib(char ch)
170{
171 if (ch >= '0' && ch <= '9') {
172 return ch - '0';
173 } else if (ch >= 'a' && ch <= 'f') {
174 return 10 + (ch - 'a');
175 } else if (ch >= 'A' && ch <= 'F') {
Sergey Fedorov2a802aa2014-05-27 16:15:20 +0400176 return 10 + (ch - 'A');
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200177 } else {
178 return -1;
179 }
180}
181
Anthony Liguori6e924662012-03-30 14:04:04 -0500182static void qtest_get_time(qemu_timeval *tv)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200183{
Anthony Liguori6e924662012-03-30 14:04:04 -0500184 qemu_gettimeofday(tv);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200185 tv->tv_sec -= start_time.tv_sec;
186 tv->tv_usec -= start_time.tv_usec;
187 if (tv->tv_usec < 0) {
188 tv->tv_usec += 1000000;
189 tv->tv_sec -= 1;
190 }
191}
192
193static void qtest_send_prefix(CharDriverState *chr)
194{
Anthony Liguori6e924662012-03-30 14:04:04 -0500195 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200196
197 if (!qtest_log_fp || !qtest_opened) {
198 return;
199 }
200
201 qtest_get_time(&tv);
202 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700203 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200204}
205
John Snow7a6a7402015-05-22 14:13:44 -0400206static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
207{
208 va_list ap;
209
210 if (!qtest_log_fp || !qtest_opened) {
211 return;
212 }
213
214 qtest_send_prefix(NULL);
215
216 va_start(ap, fmt);
217 vfprintf(qtest_log_fp, fmt, ap);
218 va_end(ap);
219}
220
John Snow332cc7e2015-05-22 14:13:43 -0400221static void do_qtest_send(CharDriverState *chr, const char *str, size_t len)
222{
223 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
224 if (qtest_log_fp && qtest_opened) {
225 fprintf(qtest_log_fp, "%s", str);
226 }
227}
228
229static void qtest_send(CharDriverState *chr, const char *str)
230{
231 do_qtest_send(chr, str, strlen(str));
232}
233
234static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharDriverState *chr,
235 const char *fmt, ...)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200236{
237 va_list ap;
John Snow332cc7e2015-05-22 14:13:43 -0400238 gchar *buffer;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200239
240 va_start(ap, fmt);
John Snow332cc7e2015-05-22 14:13:43 -0400241 buffer = g_strdup_vprintf(fmt, ap);
242 qtest_send(chr, buffer);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200243 va_end(ap);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200244}
245
Paolo Bonzini20288342012-03-28 15:42:03 +0200246static void qtest_irq_handler(void *opaque, int n, int level)
247{
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700248 qemu_irq old_irq = *(qemu_irq *)opaque;
249 qemu_set_irq(old_irq, level);
Paolo Bonzini20288342012-03-28 15:42:03 +0200250
251 if (irq_levels[n] != level) {
252 CharDriverState *chr = qtest_chr;
253 irq_levels[n] = level;
254 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400255 qtest_sendf(chr, "IRQ %s %d\n",
256 level ? "raise" : "lower", n);
Paolo Bonzini20288342012-03-28 15:42:03 +0200257 }
258}
259
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200260static void qtest_process_command(CharDriverState *chr, gchar **words)
261{
262 const gchar *command;
263
264 g_assert(words);
265
266 command = words[0];
267
268 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500269 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200270 int i;
271
272 qtest_get_time(&tv);
273 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700274 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200275 for (i = 0; words[i]; i++) {
276 fprintf(qtest_log_fp, " %s", words[i]);
277 }
278 fprintf(qtest_log_fp, "\n");
279 }
280
281 g_assert(command);
Paolo Bonzini20288342012-03-28 15:42:03 +0200282 if (strcmp(words[0], "irq_intercept_out") == 0
283 || strcmp(words[0], "irq_intercept_in") == 0) {
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700284 DeviceState *dev;
285 NamedGPIOList *ngl;
Paolo Bonzini20288342012-03-28 15:42:03 +0200286
287 g_assert(words[1]);
288 dev = DEVICE(object_resolve_path(words[1], NULL));
289 if (!dev) {
290 qtest_send_prefix(chr);
291 qtest_send(chr, "FAIL Unknown device\n");
292 return;
293 }
294
295 if (irq_intercept_dev) {
296 qtest_send_prefix(chr);
297 if (irq_intercept_dev != dev) {
298 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
299 } else {
300 qtest_send(chr, "OK\n");
301 }
302 return;
303 }
304
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700305 QLIST_FOREACH(ngl, &dev->gpios, node) {
306 /* We don't support intercept of named GPIOs yet */
307 if (ngl->name) {
308 continue;
309 }
310 if (words[0][14] == 'o') {
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700311 int i;
312 for (i = 0; i < ngl->num_out; ++i) {
313 qemu_irq *disconnected = g_new0(qemu_irq, 1);
314 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
315 disconnected, i);
316
317 *disconnected = qdev_intercept_gpio_out(dev, icpt,
318 ngl->name, i);
319 }
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700320 } else {
321 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
322 ngl->num_in);
323 }
Paolo Bonzini20288342012-03-28 15:42:03 +0200324 }
325 irq_intercept_dev = dev;
326 qtest_send_prefix(chr);
327 qtest_send(chr, "OK\n");
328
329 } else if (strcmp(words[0], "outb") == 0 ||
330 strcmp(words[0], "outw") == 0 ||
331 strcmp(words[0], "outl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200332 unsigned long addr;
333 unsigned long value;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200334
335 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200336 g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
337 g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
338 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200339
340 if (words[0][3] == 'b') {
341 cpu_outb(addr, value);
342 } else if (words[0][3] == 'w') {
343 cpu_outw(addr, value);
344 } else if (words[0][3] == 'l') {
345 cpu_outl(addr, value);
346 }
347 qtest_send_prefix(chr);
348 qtest_send(chr, "OK\n");
349 } else if (strcmp(words[0], "inb") == 0 ||
350 strcmp(words[0], "inw") == 0 ||
351 strcmp(words[0], "inl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200352 unsigned long addr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200353 uint32_t value = -1U;
354
355 g_assert(words[1]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200356 g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
357 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200358
359 if (words[0][2] == 'b') {
360 value = cpu_inb(addr);
361 } else if (words[0][2] == 'w') {
362 value = cpu_inw(addr);
363 } else if (words[0][2] == 'l') {
364 value = cpu_inl(addr);
365 }
366 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400367 qtest_sendf(chr, "OK 0x%04x\n", value);
Andreas Färber872536b2013-02-16 22:44:03 +0100368 } else if (strcmp(words[0], "writeb") == 0 ||
369 strcmp(words[0], "writew") == 0 ||
370 strcmp(words[0], "writel") == 0 ||
371 strcmp(words[0], "writeq") == 0) {
372 uint64_t addr;
373 uint64_t value;
374
375 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200376 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
377 g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100378
379 if (words[0][5] == 'b') {
380 uint8_t data = value;
381 cpu_physical_memory_write(addr, &data, 1);
382 } else if (words[0][5] == 'w') {
383 uint16_t data = value;
384 tswap16s(&data);
385 cpu_physical_memory_write(addr, &data, 2);
386 } else if (words[0][5] == 'l') {
387 uint32_t data = value;
388 tswap32s(&data);
389 cpu_physical_memory_write(addr, &data, 4);
390 } else if (words[0][5] == 'q') {
391 uint64_t data = value;
392 tswap64s(&data);
393 cpu_physical_memory_write(addr, &data, 8);
394 }
395 qtest_send_prefix(chr);
396 qtest_send(chr, "OK\n");
397 } else if (strcmp(words[0], "readb") == 0 ||
398 strcmp(words[0], "readw") == 0 ||
399 strcmp(words[0], "readl") == 0 ||
400 strcmp(words[0], "readq") == 0) {
401 uint64_t addr;
402 uint64_t value = UINT64_C(-1);
403
404 g_assert(words[1]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200405 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100406
407 if (words[0][4] == 'b') {
408 uint8_t data;
409 cpu_physical_memory_read(addr, &data, 1);
410 value = data;
411 } else if (words[0][4] == 'w') {
412 uint16_t data;
413 cpu_physical_memory_read(addr, &data, 2);
414 value = tswap16(data);
415 } else if (words[0][4] == 'l') {
416 uint32_t data;
417 cpu_physical_memory_read(addr, &data, 4);
418 value = tswap32(data);
419 } else if (words[0][4] == 'q') {
420 cpu_physical_memory_read(addr, &value, 8);
421 tswap64s(&value);
422 }
423 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400424 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200425 } else if (strcmp(words[0], "read") == 0) {
426 uint64_t addr, len, i;
427 uint8_t *data;
John Snow5560b852015-05-22 14:13:44 -0400428 char *enc;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200429
430 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200431 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
432 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200433
434 data = g_malloc(len);
435 cpu_physical_memory_read(addr, data, len);
436
John Snow5560b852015-05-22 14:13:44 -0400437 enc = g_malloc(2 * len + 1);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200438 for (i = 0; i < len; i++) {
John Snow5560b852015-05-22 14:13:44 -0400439 sprintf(&enc[i * 2], "%02x", data[i]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200440 }
John Snow5560b852015-05-22 14:13:44 -0400441
442 qtest_send_prefix(chr);
443 qtest_sendf(chr, "OK 0x%s\n", enc);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200444
445 g_free(data);
John Snow5560b852015-05-22 14:13:44 -0400446 g_free(enc);
John Snow7a6a7402015-05-22 14:13:44 -0400447 } else if (strcmp(words[0], "b64read") == 0) {
448 uint64_t addr, len;
449 uint8_t *data;
450 gchar *b64_data;
451
452 g_assert(words[1] && words[2]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200453 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
454 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400455
456 data = g_malloc(len);
457 cpu_physical_memory_read(addr, data, len);
458 b64_data = g_base64_encode(data, len);
459 qtest_send_prefix(chr);
460 qtest_sendf(chr, "OK %s\n", b64_data);
461
462 g_free(data);
463 g_free(b64_data);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200464 } else if (strcmp(words[0], "write") == 0) {
465 uint64_t addr, len, i;
466 uint8_t *data;
467 size_t data_len;
468
469 g_assert(words[1] && words[2] && words[3]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200470 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
471 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200472
473 data_len = strlen(words[3]);
474 if (data_len < 3) {
475 qtest_send(chr, "ERR invalid argument size\n");
476 return;
477 }
478
479 data = g_malloc(len);
480 for (i = 0; i < len; i++) {
481 if ((i * 2 + 4) <= data_len) {
482 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
483 data[i] |= hex2nib(words[3][i * 2 + 3]);
484 } else {
485 data[i] = 0;
486 }
487 }
488 cpu_physical_memory_write(addr, data, len);
489 g_free(data);
490
491 qtest_send_prefix(chr);
492 qtest_send(chr, "OK\n");
John Snow4d007962015-05-22 14:13:44 -0400493 } else if (strcmp(words[0], "memset") == 0) {
494 uint64_t addr, len;
495 uint8_t *data;
Laurent Vivieraa15f492016-09-13 14:52:43 +0200496 unsigned long pattern;
John Snow4d007962015-05-22 14:13:44 -0400497
498 g_assert(words[1] && words[2] && words[3]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200499 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
500 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
501 g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);
John Snow4d007962015-05-22 14:13:44 -0400502
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100503 if (len) {
504 data = g_malloc(len);
505 memset(data, pattern, len);
506 cpu_physical_memory_write(addr, data, len);
507 g_free(data);
508 }
John Snow4d007962015-05-22 14:13:44 -0400509
510 qtest_send_prefix(chr);
511 qtest_send(chr, "OK\n");
John Snow7a6a7402015-05-22 14:13:44 -0400512 } else if (strcmp(words[0], "b64write") == 0) {
513 uint64_t addr, len;
514 uint8_t *data;
515 size_t data_len;
516 gsize out_len;
517
518 g_assert(words[1] && words[2] && words[3]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200519 g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
520 g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400521
522 data_len = strlen(words[3]);
523 if (data_len < 3) {
524 qtest_send(chr, "ERR invalid argument size\n");
525 return;
526 }
527
528 data = g_base64_decode_inplace(words[3], &out_len);
529 if (out_len != len) {
530 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
531 "found %zu)\n",
532 len, out_len);
533 out_len = MIN(out_len, len);
534 }
535
536 cpu_physical_memory_write(addr, data, out_len);
537
538 qtest_send_prefix(chr);
539 qtest_send(chr, "OK\n");
Laurent Viviereeddd592016-09-13 14:52:45 +0200540#ifdef TARGET_PPC64
541 } else if (strcmp(words[0], "rtas") == 0) {
542 uint64_t res, args, ret;
543 unsigned long nargs, nret;
544
545 g_assert(qemu_strtoul(words[2], NULL, 0, &nargs) == 0);
546 g_assert(qemu_strtoull(words[3], NULL, 0, &args) == 0);
547 g_assert(qemu_strtoul(words[4], NULL, 0, &nret) == 0);
548 g_assert(qemu_strtoull(words[5], NULL, 0, &ret) == 0);
549 res = qtest_rtas_call(words[1], nargs, args, nret, ret);
550
551 qtest_send_prefix(chr);
552 qtest_sendf(chr, "OK %"PRIu64"\n", res);
553#endif
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200554 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200555 int64_t ns;
556
557 if (words[1]) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200558 g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200559 } else {
Alex Bligh40daca52013-08-21 16:03:02 +0100560 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200561 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100562 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200563 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400564 qtest_sendf(chr, "OK %"PRIi64"\n",
565 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200566 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200567 int64_t ns;
568
569 g_assert(words[1]);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200570 g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200571 qtest_clock_warp(ns);
572 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400573 qtest_sendf(chr, "OK %"PRIi64"\n",
574 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200575 } else {
576 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400577 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200578 }
579}
580
581static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
582{
583 char *end;
584
585 while ((end = strchr(inbuf->str, '\n')) != NULL) {
586 size_t offset;
587 GString *cmd;
588 gchar **words;
589
590 offset = end - inbuf->str;
591
592 cmd = g_string_new_len(inbuf->str, offset);
593 g_string_erase(inbuf, 0, offset + 1);
594
595 words = g_strsplit(cmd->str, " ", 0);
596 qtest_process_command(chr, words);
597 g_strfreev(words);
598
599 g_string_free(cmd, TRUE);
600 }
601}
602
603static void qtest_read(void *opaque, const uint8_t *buf, int size)
604{
605 CharDriverState *chr = opaque;
606
607 g_string_append_len(inbuf, (const gchar *)buf, size);
608 qtest_process_inbuf(chr, inbuf);
609}
610
611static int qtest_can_read(void *opaque)
612{
613 return 1024;
614}
615
616static void qtest_event(void *opaque, int event)
617{
618 int i;
619
620 switch (event) {
621 case CHR_EVENT_OPENED:
Markus Armbrusterba646ff2013-06-26 15:52:12 +0200622 /*
623 * We used to call qemu_system_reset() here, hoping we could
624 * use the same process for multiple tests that way. Never
625 * used. Injects an extra reset even when it's not used, and
626 * that can mess up tests, e.g. -boot once.
627 */
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200628 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
629 irq_levels[i] = 0;
630 }
Anthony Liguori6e924662012-03-30 14:04:04 -0500631 qemu_gettimeofday(&start_time);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200632 qtest_opened = true;
633 if (qtest_log_fp) {
634 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700635 (long) start_time.tv_sec, (long) start_time.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200636 }
637 break;
638 case CHR_EVENT_CLOSED:
639 qtest_opened = false;
640 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500641 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200642 qtest_get_time(&tv);
643 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700644 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200645 }
646 break;
647 default:
648 break;
649 }
650}
651
Eduardo Habkostf6a1ef62014-09-26 17:45:30 -0300652static int qtest_init_accel(MachineState *ms)
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200653{
Markus Armbrusterb3adf5a2015-02-13 15:48:19 +0100654 QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
655 &error_abort);
656 qemu_opt_set(opts, "shift", "0", &error_abort);
657 configure_icount(opts, &error_abort);
658 qemu_opts_del(opts);
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200659 return 0;
660}
661
Fam Zheng23802b42014-02-10 09:28:02 +0800662void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200663{
664 CharDriverState *chr;
665
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200666 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
667
Fam Zheng23802b42014-02-10 09:28:02 +0800668 if (chr == NULL) {
669 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
670 qtest_chrdev);
671 return;
672 }
673
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200674 if (qtest_log) {
675 if (strcmp(qtest_log, "none") != 0) {
676 qtest_log_fp = fopen(qtest_log, "w+");
677 }
678 } else {
679 qtest_log_fp = stderr;
680 }
681
Li Liu107684c2014-10-22 10:26:47 +0800682 qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
683 qemu_chr_fe_set_echo(chr, true);
684
685 inbuf = g_string_new("");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200686 qtest_chr = chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200687}
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200688
689bool qtest_driver(void)
690{
691 return qtest_chr;
692}
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300693
694static void qtest_accel_class_init(ObjectClass *oc, void *data)
695{
696 AccelClass *ac = ACCEL_CLASS(oc);
697 ac->name = "QTest";
698 ac->available = qtest_available;
Eduardo Habkost0d15da82014-09-26 17:45:29 -0300699 ac->init_machine = qtest_init_accel;
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300700 ac->allowed = &qtest_allowed;
701}
702
703#define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
704
705static const TypeInfo qtest_accel_type = {
706 .name = TYPE_QTEST_ACCEL,
707 .parent = TYPE_ACCEL,
708 .class_init = qtest_accel_class_init,
709};
710
711static void qtest_type_init(void)
712{
713 type_register_static(&qtest_accel_type);
714}
715
716type_init(qtest_type_init);