blob: 5672b75c354b6cdae5df0b0d49e0ba9da0b13faf [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 "cpu.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010017#include "sysemu/qtest.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020018#include "sysemu/runstate.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040019#include "chardev/char-fe.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010020#include "exec/ioport.h"
21#include "exec/memory.h"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020022#include "hw/irq.h"
Eduardo Habkost3a6ce512014-09-26 17:45:26 -030023#include "sysemu/accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010024#include "sysemu/cpus.h"
Sebastian Tanase1ad95802014-07-25 11:56:28 +020025#include "qemu/config-file.h"
26#include "qemu/option.h"
27#include "qemu/error-report.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028#include "qemu/module.h"
Laurent Vivieraa15f492016-09-13 14:52:43 +020029#include "qemu/cutils.h"
Laurent Vivier2c6e9182020-02-06 00:20:15 +010030#include "config-devices.h"
31#ifdef CONFIG_PSERIES
Laurent Viviereeddd592016-09-13 14:52:45 +020032#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;
Alexander Bulekove731d082020-02-19 23:11:01 -050046static void (*qtest_server_send)(void*, const char*);
47static void *qtest_server_send_opaque;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020048
Anthony Liguori6b7cff72012-03-30 12:53:54 -050049#define FMT_timeval "%ld.%06ld"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020050
51/**
52 * QTest Protocol
53 *
54 * Line based protocol, request/response based. Server can send async messages
55 * so clients should always handle many async messages before the response
56 * comes in.
57 *
58 * Valid requests
59 *
Paolo Bonzini8156be52012-03-28 15:42:04 +020060 * Clock management:
61 *
Alex Blighbc72ad62013-08-21 16:03:08 +010062 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
Paolo Bonzini8156be52012-03-28 15:42:04 +020063 * let you adjust the value of the clock (monotonically). All the commands
64 * return the current value of the clock in nanoseconds.
65 *
66 * > clock_step
67 * < OK VALUE
68 *
69 * Advance the clock to the next deadline. Useful when waiting for
70 * asynchronous events.
71 *
72 * > clock_step NS
73 * < OK VALUE
74 *
75 * Advance the clock by NS nanoseconds.
76 *
77 * > clock_set NS
78 * < OK VALUE
79 *
80 * Advance the clock to NS nanoseconds (do nothing if it's already past).
81 *
82 * PIO and memory access:
83 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020084 * > outb ADDR VALUE
85 * < OK
86 *
87 * > outw ADDR VALUE
88 * < OK
89 *
90 * > outl ADDR VALUE
91 * < OK
92 *
93 * > inb ADDR
94 * < OK VALUE
95 *
96 * > inw ADDR
97 * < OK VALUE
98 *
99 * > inl ADDR
100 * < OK VALUE
101 *
Andreas Färber872536b2013-02-16 22:44:03 +0100102 * > writeb ADDR VALUE
103 * < OK
104 *
105 * > writew ADDR VALUE
106 * < OK
107 *
108 * > writel ADDR VALUE
109 * < OK
110 *
111 * > writeq ADDR VALUE
112 * < OK
113 *
114 * > readb ADDR
115 * < OK VALUE
116 *
117 * > readw ADDR
118 * < OK VALUE
119 *
120 * > readl ADDR
121 * < OK VALUE
122 *
123 * > readq ADDR
124 * < OK VALUE
125 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200126 * > read ADDR SIZE
127 * < OK DATA
128 *
129 * > write ADDR SIZE DATA
130 * < OK
131 *
John Snow7a6a7402015-05-22 14:13:44 -0400132 * > b64read ADDR SIZE
133 * < OK B64_DATA
134 *
135 * > b64write ADDR SIZE B64_DATA
136 * < OK
137 *
John Snow4d007962015-05-22 14:13:44 -0400138 * > memset ADDR SIZE VALUE
139 * < OK
140 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200141 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100142 * For 'memset' a zero size is permitted and does nothing.
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200143 *
144 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
145 * than the expected size, the value will be zero filled at the end of the data
146 * sequence.
147 *
John Snow7a6a7402015-05-22 14:13:44 -0400148 * B64_DATA is an arbitrarily long base64 encoded string.
149 * If the sizes do not match, the data will be truncated.
150 *
Paolo Bonzini20288342012-03-28 15:42:03 +0200151 * IRQ management:
152 *
153 * > irq_intercept_in QOM-PATH
154 * < OK
155 *
156 * > irq_intercept_out QOM-PATH
157 * < OK
158 *
159 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
160 * QOM-PATH. When the pin is triggered, one of the following async messages
161 * will be printed to the qtest stream:
162 *
163 * IRQ raise NUM
164 * IRQ lower NUM
165 *
166 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
167 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
168 * NUM=0 even though it is remapped to GSI 2).
Steffen Görtz9813dc62019-01-07 15:23:47 +0000169 *
170 * Setting interrupt level:
171 *
172 * > set_irq_in QOM-PATH NAME NUM LEVEL
173 * < OK
174 *
175 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
176 * LEVEL is an signed integer IRQ level.
177 *
178 * Forcibly set the given interrupt pin to the given level.
179 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200180 */
181
182static int hex2nib(char ch)
183{
184 if (ch >= '0' && ch <= '9') {
185 return ch - '0';
186 } else if (ch >= 'a' && ch <= 'f') {
187 return 10 + (ch - 'a');
188 } else if (ch >= 'A' && ch <= 'F') {
Sergey Fedorov2a802aa2014-05-27 16:15:20 +0400189 return 10 + (ch - 'A');
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200190 } else {
191 return -1;
192 }
193}
194
Anthony Liguori6e924662012-03-30 14:04:04 -0500195static void qtest_get_time(qemu_timeval *tv)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200196{
Anthony Liguori6e924662012-03-30 14:04:04 -0500197 qemu_gettimeofday(tv);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200198 tv->tv_sec -= start_time.tv_sec;
199 tv->tv_usec -= start_time.tv_usec;
200 if (tv->tv_usec < 0) {
201 tv->tv_usec += 1000000;
202 tv->tv_sec -= 1;
203 }
204}
205
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300206static void qtest_send_prefix(CharBackend *chr)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200207{
Anthony Liguori6e924662012-03-30 14:04:04 -0500208 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200209
210 if (!qtest_log_fp || !qtest_opened) {
211 return;
212 }
213
214 qtest_get_time(&tv);
215 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700216 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200217}
218
John Snow7a6a7402015-05-22 14:13:44 -0400219static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
220{
221 va_list ap;
222
223 if (!qtest_log_fp || !qtest_opened) {
224 return;
225 }
226
227 qtest_send_prefix(NULL);
228
229 va_start(ap, fmt);
230 vfprintf(qtest_log_fp, fmt, ap);
231 va_end(ap);
232}
233
Alexander Bulekove731d082020-02-19 23:11:01 -0500234static void qtest_server_char_be_send(void *opaque, const char *str)
John Snow332cc7e2015-05-22 14:13:43 -0400235{
Alexander Bulekove731d082020-02-19 23:11:01 -0500236 size_t len = strlen(str);
237 CharBackend* chr = (CharBackend *)opaque;
John Snow332cc7e2015-05-22 14:13:43 -0400238 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
239 if (qtest_log_fp && qtest_opened) {
240 fprintf(qtest_log_fp, "%s", str);
241 }
242}
243
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300244static void qtest_send(CharBackend *chr, const char *str)
John Snow332cc7e2015-05-22 14:13:43 -0400245{
Alexander Bulekove731d082020-02-19 23:11:01 -0500246 qtest_server_send(qtest_server_send_opaque, str);
John Snow332cc7e2015-05-22 14:13:43 -0400247}
248
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300249static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
John Snow332cc7e2015-05-22 14:13:43 -0400250 const char *fmt, ...)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200251{
252 va_list ap;
John Snow332cc7e2015-05-22 14:13:43 -0400253 gchar *buffer;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200254
255 va_start(ap, fmt);
John Snow332cc7e2015-05-22 14:13:43 -0400256 buffer = g_strdup_vprintf(fmt, ap);
257 qtest_send(chr, buffer);
Marc-André Lureaufc340592016-11-10 12:25:00 +0400258 g_free(buffer);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200259 va_end(ap);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200260}
261
Paolo Bonzini20288342012-03-28 15:42:03 +0200262static void qtest_irq_handler(void *opaque, int n, int level)
263{
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700264 qemu_irq old_irq = *(qemu_irq *)opaque;
265 qemu_set_irq(old_irq, level);
Paolo Bonzini20288342012-03-28 15:42:03 +0200266
267 if (irq_levels[n] != level) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300268 CharBackend *chr = &qtest_chr;
Paolo Bonzini20288342012-03-28 15:42:03 +0200269 irq_levels[n] = level;
270 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400271 qtest_sendf(chr, "IRQ %s %d\n",
272 level ? "raise" : "lower", n);
Paolo Bonzini20288342012-03-28 15:42:03 +0200273 }
274}
275
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300276static void qtest_process_command(CharBackend *chr, gchar **words)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200277{
278 const gchar *command;
279
280 g_assert(words);
281
282 command = words[0];
283
284 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500285 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200286 int i;
287
288 qtest_get_time(&tv);
289 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700290 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200291 for (i = 0; words[i]; i++) {
292 fprintf(qtest_log_fp, " %s", words[i]);
293 }
294 fprintf(qtest_log_fp, "\n");
295 }
296
297 g_assert(command);
Paolo Bonzini20288342012-03-28 15:42:03 +0200298 if (strcmp(words[0], "irq_intercept_out") == 0
299 || strcmp(words[0], "irq_intercept_in") == 0) {
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700300 DeviceState *dev;
301 NamedGPIOList *ngl;
Paolo Bonzini20288342012-03-28 15:42:03 +0200302
303 g_assert(words[1]);
304 dev = DEVICE(object_resolve_path(words[1], NULL));
305 if (!dev) {
306 qtest_send_prefix(chr);
307 qtest_send(chr, "FAIL Unknown device\n");
Paolo Bonzini7d374352018-12-13 23:37:37 +0100308 return;
Paolo Bonzini20288342012-03-28 15:42:03 +0200309 }
310
311 if (irq_intercept_dev) {
312 qtest_send_prefix(chr);
313 if (irq_intercept_dev != dev) {
314 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
315 } else {
316 qtest_send(chr, "OK\n");
317 }
Paolo Bonzini7d374352018-12-13 23:37:37 +0100318 return;
Paolo Bonzini20288342012-03-28 15:42:03 +0200319 }
320
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700321 QLIST_FOREACH(ngl, &dev->gpios, node) {
322 /* We don't support intercept of named GPIOs yet */
323 if (ngl->name) {
324 continue;
325 }
326 if (words[0][14] == 'o') {
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700327 int i;
328 for (i = 0; i < ngl->num_out; ++i) {
329 qemu_irq *disconnected = g_new0(qemu_irq, 1);
330 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
331 disconnected, i);
332
333 *disconnected = qdev_intercept_gpio_out(dev, icpt,
334 ngl->name, i);
335 }
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700336 } else {
337 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
338 ngl->num_in);
339 }
Paolo Bonzini20288342012-03-28 15:42:03 +0200340 }
341 irq_intercept_dev = dev;
342 qtest_send_prefix(chr);
343 qtest_send(chr, "OK\n");
Steffen Görtz9813dc62019-01-07 15:23:47 +0000344 } else if (strcmp(words[0], "set_irq_in") == 0) {
345 DeviceState *dev;
346 qemu_irq irq;
347 char *name;
348 int ret;
349 int num;
350 int level;
Paolo Bonzini20288342012-03-28 15:42:03 +0200351
Steffen Görtz9813dc62019-01-07 15:23:47 +0000352 g_assert(words[1] && words[2] && words[3] && words[4]);
353
354 dev = DEVICE(object_resolve_path(words[1], NULL));
355 if (!dev) {
356 qtest_send_prefix(chr);
357 qtest_send(chr, "FAIL Unknown device\n");
358 return;
359 }
360
361 if (strcmp(words[2], "unnamed-gpio-in") == 0) {
362 name = NULL;
363 } else {
364 name = words[2];
365 }
366
367 ret = qemu_strtoi(words[3], NULL, 0, &num);
368 g_assert(!ret);
369 ret = qemu_strtoi(words[4], NULL, 0, &level);
370 g_assert(!ret);
371
372 irq = qdev_get_gpio_in_named(dev, name, num);
373
374 qemu_set_irq(irq, level);
375 qtest_send_prefix(chr);
376 qtest_send(chr, "OK\n");
Paolo Bonzini20288342012-03-28 15:42:03 +0200377 } else if (strcmp(words[0], "outb") == 0 ||
378 strcmp(words[0], "outw") == 0 ||
379 strcmp(words[0], "outl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200380 unsigned long addr;
381 unsigned long value;
Eric Blake14773122017-09-11 12:19:46 -0500382 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200383
384 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500385 ret = qemu_strtoul(words[1], NULL, 0, &addr);
386 g_assert(ret == 0);
387 ret = qemu_strtoul(words[2], NULL, 0, &value);
388 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200389 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200390
391 if (words[0][3] == 'b') {
392 cpu_outb(addr, value);
393 } else if (words[0][3] == 'w') {
394 cpu_outw(addr, value);
395 } else if (words[0][3] == 'l') {
396 cpu_outl(addr, value);
397 }
398 qtest_send_prefix(chr);
399 qtest_send(chr, "OK\n");
400 } else if (strcmp(words[0], "inb") == 0 ||
401 strcmp(words[0], "inw") == 0 ||
402 strcmp(words[0], "inl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200403 unsigned long addr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200404 uint32_t value = -1U;
Eric Blake14773122017-09-11 12:19:46 -0500405 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200406
407 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500408 ret = qemu_strtoul(words[1], NULL, 0, &addr);
409 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200410 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200411
412 if (words[0][2] == 'b') {
413 value = cpu_inb(addr);
414 } else if (words[0][2] == 'w') {
415 value = cpu_inw(addr);
416 } else if (words[0][2] == 'l') {
417 value = cpu_inl(addr);
418 }
419 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400420 qtest_sendf(chr, "OK 0x%04x\n", value);
Andreas Färber872536b2013-02-16 22:44:03 +0100421 } else if (strcmp(words[0], "writeb") == 0 ||
422 strcmp(words[0], "writew") == 0 ||
423 strcmp(words[0], "writel") == 0 ||
424 strcmp(words[0], "writeq") == 0) {
425 uint64_t addr;
426 uint64_t value;
Eric Blake14773122017-09-11 12:19:46 -0500427 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100428
429 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500430 ret = qemu_strtou64(words[1], NULL, 0, &addr);
431 g_assert(ret == 0);
432 ret = qemu_strtou64(words[2], NULL, 0, &value);
433 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100434
435 if (words[0][5] == 'b') {
436 uint8_t data = value;
Peter Maydell19f70342020-02-18 11:24:57 +0000437 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
438 &data, 1);
Andreas Färber872536b2013-02-16 22:44:03 +0100439 } else if (words[0][5] == 'w') {
440 uint16_t data = value;
441 tswap16s(&data);
Peter Maydell19f70342020-02-18 11:24:57 +0000442 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
443 &data, 2);
Andreas Färber872536b2013-02-16 22:44:03 +0100444 } else if (words[0][5] == 'l') {
445 uint32_t data = value;
446 tswap32s(&data);
Peter Maydell19f70342020-02-18 11:24:57 +0000447 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
448 &data, 4);
Andreas Färber872536b2013-02-16 22:44:03 +0100449 } else if (words[0][5] == 'q') {
450 uint64_t data = value;
451 tswap64s(&data);
Peter Maydell19f70342020-02-18 11:24:57 +0000452 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
453 &data, 8);
Andreas Färber872536b2013-02-16 22:44:03 +0100454 }
455 qtest_send_prefix(chr);
456 qtest_send(chr, "OK\n");
457 } else if (strcmp(words[0], "readb") == 0 ||
458 strcmp(words[0], "readw") == 0 ||
459 strcmp(words[0], "readl") == 0 ||
460 strcmp(words[0], "readq") == 0) {
461 uint64_t addr;
462 uint64_t value = UINT64_C(-1);
Eric Blake14773122017-09-11 12:19:46 -0500463 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100464
465 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500466 ret = qemu_strtou64(words[1], NULL, 0, &addr);
467 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100468
469 if (words[0][4] == 'b') {
470 uint8_t data;
Peter Maydell19f70342020-02-18 11:24:57 +0000471 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
472 &data, 1);
Andreas Färber872536b2013-02-16 22:44:03 +0100473 value = data;
474 } else if (words[0][4] == 'w') {
475 uint16_t data;
Peter Maydell19f70342020-02-18 11:24:57 +0000476 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
477 &data, 2);
Andreas Färber872536b2013-02-16 22:44:03 +0100478 value = tswap16(data);
479 } else if (words[0][4] == 'l') {
480 uint32_t data;
Peter Maydell19f70342020-02-18 11:24:57 +0000481 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
482 &data, 4);
Andreas Färber872536b2013-02-16 22:44:03 +0100483 value = tswap32(data);
484 } else if (words[0][4] == 'q') {
Peter Maydell19f70342020-02-18 11:24:57 +0000485 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
486 &value, 8);
Andreas Färber872536b2013-02-16 22:44:03 +0100487 tswap64s(&value);
488 }
489 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400490 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200491 } else if (strcmp(words[0], "read") == 0) {
492 uint64_t addr, len, i;
493 uint8_t *data;
John Snow5560b852015-05-22 14:13:44 -0400494 char *enc;
Eric Blake14773122017-09-11 12:19:46 -0500495 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200496
497 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500498 ret = qemu_strtou64(words[1], NULL, 0, &addr);
499 g_assert(ret == 0);
500 ret = qemu_strtou64(words[2], NULL, 0, &len);
501 g_assert(ret == 0);
Greg Kurz204febd2017-01-11 09:49:32 +0100502 /* We'd send garbage to libqtest if len is 0 */
503 g_assert(len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200504
505 data = g_malloc(len);
Peter Maydell19f70342020-02-18 11:24:57 +0000506 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
507 len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200508
John Snow5560b852015-05-22 14:13:44 -0400509 enc = g_malloc(2 * len + 1);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200510 for (i = 0; i < len; i++) {
John Snow5560b852015-05-22 14:13:44 -0400511 sprintf(&enc[i * 2], "%02x", data[i]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200512 }
John Snow5560b852015-05-22 14:13:44 -0400513
514 qtest_send_prefix(chr);
515 qtest_sendf(chr, "OK 0x%s\n", enc);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200516
517 g_free(data);
John Snow5560b852015-05-22 14:13:44 -0400518 g_free(enc);
John Snow7a6a7402015-05-22 14:13:44 -0400519 } else if (strcmp(words[0], "b64read") == 0) {
520 uint64_t addr, len;
521 uint8_t *data;
522 gchar *b64_data;
Eric Blake14773122017-09-11 12:19:46 -0500523 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400524
525 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500526 ret = qemu_strtou64(words[1], NULL, 0, &addr);
527 g_assert(ret == 0);
528 ret = qemu_strtou64(words[2], NULL, 0, &len);
529 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400530
531 data = g_malloc(len);
Peter Maydell19f70342020-02-18 11:24:57 +0000532 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
533 len);
John Snow7a6a7402015-05-22 14:13:44 -0400534 b64_data = g_base64_encode(data, len);
535 qtest_send_prefix(chr);
536 qtest_sendf(chr, "OK %s\n", b64_data);
537
538 g_free(data);
539 g_free(b64_data);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200540 } else if (strcmp(words[0], "write") == 0) {
541 uint64_t addr, len, i;
542 uint8_t *data;
543 size_t data_len;
Eric Blake14773122017-09-11 12:19:46 -0500544 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200545
546 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500547 ret = qemu_strtou64(words[1], NULL, 0, &addr);
548 g_assert(ret == 0);
549 ret = qemu_strtou64(words[2], NULL, 0, &len);
550 g_assert(ret == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200551
552 data_len = strlen(words[3]);
553 if (data_len < 3) {
554 qtest_send(chr, "ERR invalid argument size\n");
555 return;
556 }
557
558 data = g_malloc(len);
559 for (i = 0; i < len; i++) {
560 if ((i * 2 + 4) <= data_len) {
561 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
562 data[i] |= hex2nib(words[3][i * 2 + 3]);
563 } else {
564 data[i] = 0;
565 }
566 }
Peter Maydell19f70342020-02-18 11:24:57 +0000567 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
568 len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200569 g_free(data);
570
571 qtest_send_prefix(chr);
572 qtest_send(chr, "OK\n");
John Snow4d007962015-05-22 14:13:44 -0400573 } else if (strcmp(words[0], "memset") == 0) {
574 uint64_t addr, len;
575 uint8_t *data;
Laurent Vivieraa15f492016-09-13 14:52:43 +0200576 unsigned long pattern;
Eric Blake14773122017-09-11 12:19:46 -0500577 int ret;
John Snow4d007962015-05-22 14:13:44 -0400578
579 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500580 ret = qemu_strtou64(words[1], NULL, 0, &addr);
581 g_assert(ret == 0);
582 ret = qemu_strtou64(words[2], NULL, 0, &len);
583 g_assert(ret == 0);
584 ret = qemu_strtoul(words[3], NULL, 0, &pattern);
585 g_assert(ret == 0);
John Snow4d007962015-05-22 14:13:44 -0400586
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100587 if (len) {
588 data = g_malloc(len);
589 memset(data, pattern, len);
Peter Maydell19f70342020-02-18 11:24:57 +0000590 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
591 data, len);
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100592 g_free(data);
593 }
John Snow4d007962015-05-22 14:13:44 -0400594
595 qtest_send_prefix(chr);
596 qtest_send(chr, "OK\n");
John Snow7a6a7402015-05-22 14:13:44 -0400597 } else if (strcmp(words[0], "b64write") == 0) {
598 uint64_t addr, len;
599 uint8_t *data;
600 size_t data_len;
601 gsize out_len;
Eric Blake14773122017-09-11 12:19:46 -0500602 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400603
604 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500605 ret = qemu_strtou64(words[1], NULL, 0, &addr);
606 g_assert(ret == 0);
607 ret = qemu_strtou64(words[2], NULL, 0, &len);
608 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400609
610 data_len = strlen(words[3]);
611 if (data_len < 3) {
612 qtest_send(chr, "ERR invalid argument size\n");
613 return;
614 }
615
616 data = g_base64_decode_inplace(words[3], &out_len);
617 if (out_len != len) {
618 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
619 "found %zu)\n",
620 len, out_len);
621 out_len = MIN(out_len, len);
622 }
623
Peter Maydell19f70342020-02-18 11:24:57 +0000624 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
625 len);
John Snow7a6a7402015-05-22 14:13:44 -0400626
627 qtest_send_prefix(chr);
628 qtest_send(chr, "OK\n");
Laurent Vivier54ce6f22016-10-07 12:14:27 +0200629 } else if (strcmp(words[0], "endianness") == 0) {
630 qtest_send_prefix(chr);
631#if defined(TARGET_WORDS_BIGENDIAN)
632 qtest_sendf(chr, "OK big\n");
633#else
634 qtest_sendf(chr, "OK little\n");
635#endif
Laurent Vivier2c6e9182020-02-06 00:20:15 +0100636#ifdef CONFIG_PSERIES
Laurent Viviereeddd592016-09-13 14:52:45 +0200637 } else if (strcmp(words[0], "rtas") == 0) {
638 uint64_t res, args, ret;
639 unsigned long nargs, nret;
Eric Blake14773122017-09-11 12:19:46 -0500640 int rc;
Laurent Viviereeddd592016-09-13 14:52:45 +0200641
Eric Blake14773122017-09-11 12:19:46 -0500642 rc = qemu_strtoul(words[2], NULL, 0, &nargs);
643 g_assert(rc == 0);
644 rc = qemu_strtou64(words[3], NULL, 0, &args);
645 g_assert(rc == 0);
646 rc = qemu_strtoul(words[4], NULL, 0, &nret);
647 g_assert(rc == 0);
648 rc = qemu_strtou64(words[5], NULL, 0, &ret);
649 g_assert(rc == 0);
Laurent Viviereeddd592016-09-13 14:52:45 +0200650 res = qtest_rtas_call(words[1], nargs, args, nret, ret);
651
652 qtest_send_prefix(chr);
653 qtest_sendf(chr, "OK %"PRIu64"\n", res);
654#endif
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200655 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200656 int64_t ns;
657
658 if (words[1]) {
Eric Blake14773122017-09-11 12:19:46 -0500659 int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
660 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200661 } else {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300662 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
663 QEMU_TIMER_ATTR_ALL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200664 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100665 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200666 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400667 qtest_sendf(chr, "OK %"PRIi64"\n",
668 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Marc-André Lureaueb062cf2019-07-22 22:51:40 +0400669 } else if (strcmp(words[0], "module_load") == 0) {
670 g_assert(words[1] && words[2]);
671
672 qtest_send_prefix(chr);
673 if (module_load_one(words[1], words[2])) {
674 qtest_sendf(chr, "OK\n");
675 } else {
676 qtest_sendf(chr, "FAIL\n");
677 }
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200678 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200679 int64_t ns;
Eric Blake14773122017-09-11 12:19:46 -0500680 int ret;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200681
682 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500683 ret = qemu_strtoi64(words[1], NULL, 0, &ns);
684 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200685 qtest_clock_warp(ns);
686 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400687 qtest_sendf(chr, "OK %"PRIi64"\n",
688 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200689 } else {
690 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400691 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200692 }
693}
694
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300695static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200696{
697 char *end;
698
699 while ((end = strchr(inbuf->str, '\n')) != NULL) {
700 size_t offset;
701 GString *cmd;
702 gchar **words;
703
704 offset = end - inbuf->str;
705
706 cmd = g_string_new_len(inbuf->str, offset);
707 g_string_erase(inbuf, 0, offset + 1);
708
709 words = g_strsplit(cmd->str, " ", 0);
710 qtest_process_command(chr, words);
711 g_strfreev(words);
712
713 g_string_free(cmd, TRUE);
714 }
715}
716
717static void qtest_read(void *opaque, const uint8_t *buf, int size)
718{
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300719 CharBackend *chr = opaque;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200720
721 g_string_append_len(inbuf, (const gchar *)buf, size);
722 qtest_process_inbuf(chr, inbuf);
723}
724
725static int qtest_can_read(void *opaque)
726{
727 return 1024;
728}
729
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +0100730static void qtest_event(void *opaque, QEMUChrEvent event)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200731{
732 int i;
733
734 switch (event) {
735 case CHR_EVENT_OPENED:
Markus Armbrusterba646ff2013-06-26 15:52:12 +0200736 /*
737 * We used to call qemu_system_reset() here, hoping we could
738 * use the same process for multiple tests that way. Never
739 * used. Injects an extra reset even when it's not used, and
740 * that can mess up tests, e.g. -boot once.
741 */
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200742 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
743 irq_levels[i] = 0;
744 }
Anthony Liguori6e924662012-03-30 14:04:04 -0500745 qemu_gettimeofday(&start_time);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200746 qtest_opened = true;
747 if (qtest_log_fp) {
748 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700749 (long) start_time.tv_sec, (long) start_time.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200750 }
751 break;
752 case CHR_EVENT_CLOSED:
753 qtest_opened = false;
754 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500755 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200756 qtest_get_time(&tv);
757 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700758 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200759 }
760 break;
761 default:
762 break;
763 }
764}
Oleinik, Alexander2b8985f2019-08-05 03:13:01 +0000765void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200766{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300767 Chardev *chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200768
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +0100769 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200770
Fam Zheng23802b42014-02-10 09:28:02 +0800771 if (chr == NULL) {
772 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
773 qtest_chrdev);
774 return;
775 }
776
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200777 if (qtest_log) {
778 if (strcmp(qtest_log, "none") != 0) {
779 qtest_log_fp = fopen(qtest_log, "w+");
780 }
781 } else {
782 qtest_log_fp = stderr;
783 }
784
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300785 qemu_chr_fe_init(&qtest_chr, chr, errp);
786 qemu_chr_fe_set_handlers(&qtest_chr, qtest_can_read, qtest_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300787 qtest_event, NULL, &qtest_chr, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300788 qemu_chr_fe_set_echo(&qtest_chr, true);
Li Liu107684c2014-10-22 10:26:47 +0800789
790 inbuf = g_string_new("");
Alexander Bulekove731d082020-02-19 23:11:01 -0500791
792 if (!qtest_server_send) {
793 qtest_server_set_send_handler(qtest_server_char_be_send, &qtest_chr);
794 }
795}
796
Alexander Bulekov3fc92f82020-02-26 22:14:39 -0500797void qtest_server_set_send_handler(void (*send)(void*, const char*),
798 void *opaque)
Alexander Bulekove731d082020-02-19 23:11:01 -0500799{
800 qtest_server_send = send;
801 qtest_server_send_opaque = opaque;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200802}
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200803
804bool qtest_driver(void)
805{
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300806 return qtest_chr.chr != NULL;
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200807}
Alexander Bulekov0bd9aef2020-02-19 23:11:04 -0500808
809void qtest_server_inproc_recv(void *dummy, const char *buf)
810{
811 static GString *gstr;
812 if (!gstr) {
813 gstr = g_string_new(NULL);
814 }
815 g_string_append(gstr, buf);
816 if (gstr->str[gstr->len - 1] == '\n') {
817 qtest_process_inbuf(NULL, gstr);
818 g_string_truncate(gstr, 0);
819 }
820}