blob: 69b9e9962b5da5563ed12c870ffad1bf31cf94cc [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"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040020#include "chardev/char-fe.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;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +030041static CharBackend qtest_chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020042static 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
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300193static void qtest_send_prefix(CharBackend *chr)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200194{
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
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300221static void do_qtest_send(CharBackend *chr, const char *str, size_t len)
John Snow332cc7e2015-05-22 14:13:43 -0400222{
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
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300229static void qtest_send(CharBackend *chr, const char *str)
John Snow332cc7e2015-05-22 14:13:43 -0400230{
231 do_qtest_send(chr, str, strlen(str));
232}
233
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300234static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
John Snow332cc7e2015-05-22 14:13:43 -0400235 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);
Marc-André Lureaufc340592016-11-10 12:25:00 +0400243 g_free(buffer);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200244 va_end(ap);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200245}
246
Paolo Bonzini20288342012-03-28 15:42:03 +0200247static void qtest_irq_handler(void *opaque, int n, int level)
248{
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700249 qemu_irq old_irq = *(qemu_irq *)opaque;
250 qemu_set_irq(old_irq, level);
Paolo Bonzini20288342012-03-28 15:42:03 +0200251
252 if (irq_levels[n] != level) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300253 CharBackend *chr = &qtest_chr;
Paolo Bonzini20288342012-03-28 15:42:03 +0200254 irq_levels[n] = level;
255 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400256 qtest_sendf(chr, "IRQ %s %d\n",
257 level ? "raise" : "lower", n);
Paolo Bonzini20288342012-03-28 15:42:03 +0200258 }
259}
260
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300261static void qtest_process_command(CharBackend *chr, gchar **words)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200262{
263 const gchar *command;
264
265 g_assert(words);
266
267 command = words[0];
268
269 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500270 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200271 int i;
272
273 qtest_get_time(&tv);
274 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700275 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200276 for (i = 0; words[i]; i++) {
277 fprintf(qtest_log_fp, " %s", words[i]);
278 }
279 fprintf(qtest_log_fp, "\n");
280 }
281
282 g_assert(command);
Paolo Bonzini20288342012-03-28 15:42:03 +0200283 if (strcmp(words[0], "irq_intercept_out") == 0
284 || strcmp(words[0], "irq_intercept_in") == 0) {
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700285 DeviceState *dev;
286 NamedGPIOList *ngl;
Paolo Bonzini20288342012-03-28 15:42:03 +0200287
288 g_assert(words[1]);
289 dev = DEVICE(object_resolve_path(words[1], NULL));
290 if (!dev) {
291 qtest_send_prefix(chr);
292 qtest_send(chr, "FAIL Unknown device\n");
293 return;
294 }
295
296 if (irq_intercept_dev) {
297 qtest_send_prefix(chr);
298 if (irq_intercept_dev != dev) {
299 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
300 } else {
301 qtest_send(chr, "OK\n");
302 }
303 return;
304 }
305
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700306 QLIST_FOREACH(ngl, &dev->gpios, node) {
307 /* We don't support intercept of named GPIOs yet */
308 if (ngl->name) {
309 continue;
310 }
311 if (words[0][14] == 'o') {
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700312 int i;
313 for (i = 0; i < ngl->num_out; ++i) {
314 qemu_irq *disconnected = g_new0(qemu_irq, 1);
315 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
316 disconnected, i);
317
318 *disconnected = qdev_intercept_gpio_out(dev, icpt,
319 ngl->name, i);
320 }
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700321 } else {
322 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
323 ngl->num_in);
324 }
Paolo Bonzini20288342012-03-28 15:42:03 +0200325 }
326 irq_intercept_dev = dev;
327 qtest_send_prefix(chr);
328 qtest_send(chr, "OK\n");
329
330 } else if (strcmp(words[0], "outb") == 0 ||
331 strcmp(words[0], "outw") == 0 ||
332 strcmp(words[0], "outl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200333 unsigned long addr;
334 unsigned long value;
Eric Blake14773122017-09-11 12:19:46 -0500335 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200336
337 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500338 ret = qemu_strtoul(words[1], NULL, 0, &addr);
339 g_assert(ret == 0);
340 ret = qemu_strtoul(words[2], NULL, 0, &value);
341 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200342 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200343
344 if (words[0][3] == 'b') {
345 cpu_outb(addr, value);
346 } else if (words[0][3] == 'w') {
347 cpu_outw(addr, value);
348 } else if (words[0][3] == 'l') {
349 cpu_outl(addr, value);
350 }
351 qtest_send_prefix(chr);
352 qtest_send(chr, "OK\n");
353 } else if (strcmp(words[0], "inb") == 0 ||
354 strcmp(words[0], "inw") == 0 ||
355 strcmp(words[0], "inl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200356 unsigned long addr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200357 uint32_t value = -1U;
Eric Blake14773122017-09-11 12:19:46 -0500358 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200359
360 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500361 ret = qemu_strtoul(words[1], NULL, 0, &addr);
362 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200363 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200364
365 if (words[0][2] == 'b') {
366 value = cpu_inb(addr);
367 } else if (words[0][2] == 'w') {
368 value = cpu_inw(addr);
369 } else if (words[0][2] == 'l') {
370 value = cpu_inl(addr);
371 }
372 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400373 qtest_sendf(chr, "OK 0x%04x\n", value);
Andreas Färber872536b2013-02-16 22:44:03 +0100374 } else if (strcmp(words[0], "writeb") == 0 ||
375 strcmp(words[0], "writew") == 0 ||
376 strcmp(words[0], "writel") == 0 ||
377 strcmp(words[0], "writeq") == 0) {
378 uint64_t addr;
379 uint64_t value;
Eric Blake14773122017-09-11 12:19:46 -0500380 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100381
382 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500383 ret = qemu_strtou64(words[1], NULL, 0, &addr);
384 g_assert(ret == 0);
385 ret = qemu_strtou64(words[2], NULL, 0, &value);
386 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100387
388 if (words[0][5] == 'b') {
389 uint8_t data = value;
Julia Suvorova70da3042018-07-02 09:52:37 +0300390 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
391 &data, 1, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100392 } else if (words[0][5] == 'w') {
393 uint16_t data = value;
394 tswap16s(&data);
Julia Suvorova70da3042018-07-02 09:52:37 +0300395 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
396 (uint8_t *) &data, 2, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100397 } else if (words[0][5] == 'l') {
398 uint32_t data = value;
399 tswap32s(&data);
Julia Suvorova70da3042018-07-02 09:52:37 +0300400 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
401 (uint8_t *) &data, 4, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100402 } else if (words[0][5] == 'q') {
403 uint64_t data = value;
404 tswap64s(&data);
Julia Suvorova70da3042018-07-02 09:52:37 +0300405 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
406 (uint8_t *) &data, 8, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100407 }
408 qtest_send_prefix(chr);
409 qtest_send(chr, "OK\n");
410 } else if (strcmp(words[0], "readb") == 0 ||
411 strcmp(words[0], "readw") == 0 ||
412 strcmp(words[0], "readl") == 0 ||
413 strcmp(words[0], "readq") == 0) {
414 uint64_t addr;
415 uint64_t value = UINT64_C(-1);
Eric Blake14773122017-09-11 12:19:46 -0500416 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100417
418 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500419 ret = qemu_strtou64(words[1], NULL, 0, &addr);
420 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100421
422 if (words[0][4] == 'b') {
423 uint8_t data;
Julia Suvorova70da3042018-07-02 09:52:37 +0300424 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
425 &data, 1, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100426 value = data;
427 } else if (words[0][4] == 'w') {
428 uint16_t data;
Julia Suvorova70da3042018-07-02 09:52:37 +0300429 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
430 (uint8_t *) &data, 2, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100431 value = tswap16(data);
432 } else if (words[0][4] == 'l') {
433 uint32_t data;
Julia Suvorova70da3042018-07-02 09:52:37 +0300434 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
435 (uint8_t *) &data, 4, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100436 value = tswap32(data);
437 } else if (words[0][4] == 'q') {
Julia Suvorova70da3042018-07-02 09:52:37 +0300438 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
439 (uint8_t *) &value, 8, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100440 tswap64s(&value);
441 }
442 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400443 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200444 } else if (strcmp(words[0], "read") == 0) {
445 uint64_t addr, len, i;
446 uint8_t *data;
John Snow5560b852015-05-22 14:13:44 -0400447 char *enc;
Eric Blake14773122017-09-11 12:19:46 -0500448 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200449
450 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500451 ret = qemu_strtou64(words[1], NULL, 0, &addr);
452 g_assert(ret == 0);
453 ret = qemu_strtou64(words[2], NULL, 0, &len);
454 g_assert(ret == 0);
Greg Kurz204febd2017-01-11 09:49:32 +0100455 /* We'd send garbage to libqtest if len is 0 */
456 g_assert(len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200457
458 data = g_malloc(len);
Julia Suvorova70da3042018-07-02 09:52:37 +0300459 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
460 data, len, false);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200461
John Snow5560b852015-05-22 14:13:44 -0400462 enc = g_malloc(2 * len + 1);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200463 for (i = 0; i < len; i++) {
John Snow5560b852015-05-22 14:13:44 -0400464 sprintf(&enc[i * 2], "%02x", data[i]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200465 }
John Snow5560b852015-05-22 14:13:44 -0400466
467 qtest_send_prefix(chr);
468 qtest_sendf(chr, "OK 0x%s\n", enc);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200469
470 g_free(data);
John Snow5560b852015-05-22 14:13:44 -0400471 g_free(enc);
John Snow7a6a7402015-05-22 14:13:44 -0400472 } else if (strcmp(words[0], "b64read") == 0) {
473 uint64_t addr, len;
474 uint8_t *data;
475 gchar *b64_data;
Eric Blake14773122017-09-11 12:19:46 -0500476 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400477
478 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500479 ret = qemu_strtou64(words[1], NULL, 0, &addr);
480 g_assert(ret == 0);
481 ret = qemu_strtou64(words[2], NULL, 0, &len);
482 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400483
484 data = g_malloc(len);
Julia Suvorova70da3042018-07-02 09:52:37 +0300485 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
486 data, len, false);
John Snow7a6a7402015-05-22 14:13:44 -0400487 b64_data = g_base64_encode(data, len);
488 qtest_send_prefix(chr);
489 qtest_sendf(chr, "OK %s\n", b64_data);
490
491 g_free(data);
492 g_free(b64_data);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200493 } else if (strcmp(words[0], "write") == 0) {
494 uint64_t addr, len, i;
495 uint8_t *data;
496 size_t data_len;
Eric Blake14773122017-09-11 12:19:46 -0500497 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200498
499 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500500 ret = qemu_strtou64(words[1], NULL, 0, &addr);
501 g_assert(ret == 0);
502 ret = qemu_strtou64(words[2], NULL, 0, &len);
503 g_assert(ret == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200504
505 data_len = strlen(words[3]);
506 if (data_len < 3) {
507 qtest_send(chr, "ERR invalid argument size\n");
508 return;
509 }
510
511 data = g_malloc(len);
512 for (i = 0; i < len; i++) {
513 if ((i * 2 + 4) <= data_len) {
514 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
515 data[i] |= hex2nib(words[3][i * 2 + 3]);
516 } else {
517 data[i] = 0;
518 }
519 }
Julia Suvorova70da3042018-07-02 09:52:37 +0300520 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
521 data, len, true);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200522 g_free(data);
523
524 qtest_send_prefix(chr);
525 qtest_send(chr, "OK\n");
John Snow4d007962015-05-22 14:13:44 -0400526 } else if (strcmp(words[0], "memset") == 0) {
527 uint64_t addr, len;
528 uint8_t *data;
Laurent Vivieraa15f492016-09-13 14:52:43 +0200529 unsigned long pattern;
Eric Blake14773122017-09-11 12:19:46 -0500530 int ret;
John Snow4d007962015-05-22 14:13:44 -0400531
532 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500533 ret = qemu_strtou64(words[1], NULL, 0, &addr);
534 g_assert(ret == 0);
535 ret = qemu_strtou64(words[2], NULL, 0, &len);
536 g_assert(ret == 0);
537 ret = qemu_strtoul(words[3], NULL, 0, &pattern);
538 g_assert(ret == 0);
John Snow4d007962015-05-22 14:13:44 -0400539
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100540 if (len) {
541 data = g_malloc(len);
542 memset(data, pattern, len);
Julia Suvorova70da3042018-07-02 09:52:37 +0300543 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
544 data, len, true);
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100545 g_free(data);
546 }
John Snow4d007962015-05-22 14:13:44 -0400547
548 qtest_send_prefix(chr);
549 qtest_send(chr, "OK\n");
John Snow7a6a7402015-05-22 14:13:44 -0400550 } else if (strcmp(words[0], "b64write") == 0) {
551 uint64_t addr, len;
552 uint8_t *data;
553 size_t data_len;
554 gsize out_len;
Eric Blake14773122017-09-11 12:19:46 -0500555 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400556
557 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500558 ret = qemu_strtou64(words[1], NULL, 0, &addr);
559 g_assert(ret == 0);
560 ret = qemu_strtou64(words[2], NULL, 0, &len);
561 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400562
563 data_len = strlen(words[3]);
564 if (data_len < 3) {
565 qtest_send(chr, "ERR invalid argument size\n");
566 return;
567 }
568
569 data = g_base64_decode_inplace(words[3], &out_len);
570 if (out_len != len) {
571 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
572 "found %zu)\n",
573 len, out_len);
574 out_len = MIN(out_len, len);
575 }
576
Julia Suvorova70da3042018-07-02 09:52:37 +0300577 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
578 data, len, true);
John Snow7a6a7402015-05-22 14:13:44 -0400579
580 qtest_send_prefix(chr);
581 qtest_send(chr, "OK\n");
Laurent Vivier54ce6f22016-10-07 12:14:27 +0200582 } else if (strcmp(words[0], "endianness") == 0) {
583 qtest_send_prefix(chr);
584#if defined(TARGET_WORDS_BIGENDIAN)
585 qtest_sendf(chr, "OK big\n");
586#else
587 qtest_sendf(chr, "OK little\n");
588#endif
Laurent Viviereeddd592016-09-13 14:52:45 +0200589#ifdef TARGET_PPC64
590 } else if (strcmp(words[0], "rtas") == 0) {
591 uint64_t res, args, ret;
592 unsigned long nargs, nret;
Eric Blake14773122017-09-11 12:19:46 -0500593 int rc;
Laurent Viviereeddd592016-09-13 14:52:45 +0200594
Eric Blake14773122017-09-11 12:19:46 -0500595 rc = qemu_strtoul(words[2], NULL, 0, &nargs);
596 g_assert(rc == 0);
597 rc = qemu_strtou64(words[3], NULL, 0, &args);
598 g_assert(rc == 0);
599 rc = qemu_strtoul(words[4], NULL, 0, &nret);
600 g_assert(rc == 0);
601 rc = qemu_strtou64(words[5], NULL, 0, &ret);
602 g_assert(rc == 0);
Laurent Viviereeddd592016-09-13 14:52:45 +0200603 res = qtest_rtas_call(words[1], nargs, args, nret, ret);
604
605 qtest_send_prefix(chr);
606 qtest_sendf(chr, "OK %"PRIu64"\n", res);
607#endif
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200608 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200609 int64_t ns;
610
611 if (words[1]) {
Eric Blake14773122017-09-11 12:19:46 -0500612 int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
613 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200614 } else {
Alex Bligh40daca52013-08-21 16:03:02 +0100615 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200616 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100617 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200618 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400619 qtest_sendf(chr, "OK %"PRIi64"\n",
620 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200621 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200622 int64_t ns;
Eric Blake14773122017-09-11 12:19:46 -0500623 int ret;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200624
625 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500626 ret = qemu_strtoi64(words[1], NULL, 0, &ns);
627 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200628 qtest_clock_warp(ns);
629 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400630 qtest_sendf(chr, "OK %"PRIi64"\n",
631 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200632 } else {
633 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400634 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200635 }
636}
637
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300638static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200639{
640 char *end;
641
642 while ((end = strchr(inbuf->str, '\n')) != NULL) {
643 size_t offset;
644 GString *cmd;
645 gchar **words;
646
647 offset = end - inbuf->str;
648
649 cmd = g_string_new_len(inbuf->str, offset);
650 g_string_erase(inbuf, 0, offset + 1);
651
652 words = g_strsplit(cmd->str, " ", 0);
653 qtest_process_command(chr, words);
654 g_strfreev(words);
655
656 g_string_free(cmd, TRUE);
657 }
658}
659
660static void qtest_read(void *opaque, const uint8_t *buf, int size)
661{
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300662 CharBackend *chr = opaque;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200663
664 g_string_append_len(inbuf, (const gchar *)buf, size);
665 qtest_process_inbuf(chr, inbuf);
666}
667
668static int qtest_can_read(void *opaque)
669{
670 return 1024;
671}
672
673static void qtest_event(void *opaque, int event)
674{
675 int i;
676
677 switch (event) {
678 case CHR_EVENT_OPENED:
Markus Armbrusterba646ff2013-06-26 15:52:12 +0200679 /*
680 * We used to call qemu_system_reset() here, hoping we could
681 * use the same process for multiple tests that way. Never
682 * used. Injects an extra reset even when it's not used, and
683 * that can mess up tests, e.g. -boot once.
684 */
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200685 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
686 irq_levels[i] = 0;
687 }
Anthony Liguori6e924662012-03-30 14:04:04 -0500688 qemu_gettimeofday(&start_time);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200689 qtest_opened = true;
690 if (qtest_log_fp) {
691 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700692 (long) start_time.tv_sec, (long) start_time.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200693 }
694 break;
695 case CHR_EVENT_CLOSED:
696 qtest_opened = false;
697 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500698 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200699 qtest_get_time(&tv);
700 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700701 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200702 }
703 break;
704 default:
705 break;
706 }
707}
708
Eduardo Habkostf6a1ef62014-09-26 17:45:30 -0300709static int qtest_init_accel(MachineState *ms)
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200710{
Markus Armbrusterb3adf5a2015-02-13 15:48:19 +0100711 QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0,
712 &error_abort);
713 qemu_opt_set(opts, "shift", "0", &error_abort);
714 configure_icount(opts, &error_abort);
715 qemu_opts_del(opts);
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200716 return 0;
717}
718
Fam Zheng23802b42014-02-10 09:28:02 +0800719void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200720{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300721 Chardev *chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200722
Marc-André Lureaub4948be2016-10-22 12:52:46 +0300723 chr = qemu_chr_new("qtest", qtest_chrdev);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200724
Fam Zheng23802b42014-02-10 09:28:02 +0800725 if (chr == NULL) {
726 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
727 qtest_chrdev);
728 return;
729 }
730
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200731 if (qtest_log) {
732 if (strcmp(qtest_log, "none") != 0) {
733 qtest_log_fp = fopen(qtest_log, "w+");
734 }
735 } else {
736 qtest_log_fp = stderr;
737 }
738
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300739 qemu_chr_fe_init(&qtest_chr, chr, errp);
740 qemu_chr_fe_set_handlers(&qtest_chr, qtest_can_read, qtest_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300741 qtest_event, NULL, &qtest_chr, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300742 qemu_chr_fe_set_echo(&qtest_chr, true);
Li Liu107684c2014-10-22 10:26:47 +0800743
744 inbuf = g_string_new("");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200745}
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200746
747bool qtest_driver(void)
748{
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300749 return qtest_chr.chr != NULL;
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200750}
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300751
752static void qtest_accel_class_init(ObjectClass *oc, void *data)
753{
754 AccelClass *ac = ACCEL_CLASS(oc);
755 ac->name = "QTest";
756 ac->available = qtest_available;
Eduardo Habkost0d15da82014-09-26 17:45:29 -0300757 ac->init_machine = qtest_init_accel;
Eduardo Habkost3a6ce512014-09-26 17:45:26 -0300758 ac->allowed = &qtest_allowed;
759}
760
761#define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest")
762
763static const TypeInfo qtest_accel_type = {
764 .name = TYPE_QTEST_ACCEL,
765 .parent = TYPE_ACCEL,
766 .class_init = qtest_accel_class_init,
767};
768
769static void qtest_type_init(void)
770{
771 type_register_static(&qtest_accel_type);
772}
773
774type_init(qtest_type_init);