blob: 8b50e2783ee0dde871ecd944340b6a5115902d60 [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 Viviereeddd592016-09-13 14:52:45 +020030#ifdef TARGET_PPC64
31#include "hw/ppc/spapr_rtas.h"
32#endif
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020033
34#define MAX_IRQ 256
35
liguangd5286af2013-01-24 13:03:27 +080036bool qtest_allowed;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020037
Paolo Bonzini20288342012-03-28 15:42:03 +020038static DeviceState *irq_intercept_dev;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020039static FILE *qtest_log_fp;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +030040static CharBackend qtest_chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020041static GString *inbuf;
42static int irq_levels[MAX_IRQ];
Anthony Liguori6e924662012-03-30 14:04:04 -050043static qemu_timeval start_time;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020044static bool qtest_opened;
45
Anthony Liguori6b7cff72012-03-30 12:53:54 -050046#define FMT_timeval "%ld.%06ld"
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020047
48/**
49 * QTest Protocol
50 *
51 * Line based protocol, request/response based. Server can send async messages
52 * so clients should always handle many async messages before the response
53 * comes in.
54 *
55 * Valid requests
56 *
Paolo Bonzini8156be52012-03-28 15:42:04 +020057 * Clock management:
58 *
Alex Blighbc72ad62013-08-21 16:03:08 +010059 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
Paolo Bonzini8156be52012-03-28 15:42:04 +020060 * let you adjust the value of the clock (monotonically). All the commands
61 * return the current value of the clock in nanoseconds.
62 *
63 * > clock_step
64 * < OK VALUE
65 *
66 * Advance the clock to the next deadline. Useful when waiting for
67 * asynchronous events.
68 *
69 * > clock_step NS
70 * < OK VALUE
71 *
72 * Advance the clock by NS nanoseconds.
73 *
74 * > clock_set NS
75 * < OK VALUE
76 *
77 * Advance the clock to NS nanoseconds (do nothing if it's already past).
78 *
79 * PIO and memory access:
80 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020081 * > outb ADDR VALUE
82 * < OK
83 *
84 * > outw ADDR VALUE
85 * < OK
86 *
87 * > outl ADDR VALUE
88 * < OK
89 *
90 * > inb ADDR
91 * < OK VALUE
92 *
93 * > inw ADDR
94 * < OK VALUE
95 *
96 * > inl ADDR
97 * < OK VALUE
98 *
Andreas Färber872536b2013-02-16 22:44:03 +010099 * > writeb ADDR VALUE
100 * < OK
101 *
102 * > writew ADDR VALUE
103 * < OK
104 *
105 * > writel ADDR VALUE
106 * < OK
107 *
108 * > writeq ADDR VALUE
109 * < OK
110 *
111 * > readb ADDR
112 * < OK VALUE
113 *
114 * > readw ADDR
115 * < OK VALUE
116 *
117 * > readl ADDR
118 * < OK VALUE
119 *
120 * > readq ADDR
121 * < OK VALUE
122 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200123 * > read ADDR SIZE
124 * < OK DATA
125 *
126 * > write ADDR SIZE DATA
127 * < OK
128 *
John Snow7a6a7402015-05-22 14:13:44 -0400129 * > b64read ADDR SIZE
130 * < OK B64_DATA
131 *
132 * > b64write ADDR SIZE B64_DATA
133 * < OK
134 *
John Snow4d007962015-05-22 14:13:44 -0400135 * > memset ADDR SIZE VALUE
136 * < OK
137 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200138 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100139 * For 'memset' a zero size is permitted and does nothing.
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200140 *
141 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
142 * than the expected size, the value will be zero filled at the end of the data
143 * sequence.
144 *
John Snow7a6a7402015-05-22 14:13:44 -0400145 * B64_DATA is an arbitrarily long base64 encoded string.
146 * If the sizes do not match, the data will be truncated.
147 *
Paolo Bonzini20288342012-03-28 15:42:03 +0200148 * IRQ management:
149 *
150 * > irq_intercept_in QOM-PATH
151 * < OK
152 *
153 * > irq_intercept_out QOM-PATH
154 * < OK
155 *
156 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
157 * QOM-PATH. When the pin is triggered, one of the following async messages
158 * will be printed to the qtest stream:
159 *
160 * IRQ raise NUM
161 * IRQ lower NUM
162 *
163 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
164 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
165 * NUM=0 even though it is remapped to GSI 2).
Steffen Görtz9813dc62019-01-07 15:23:47 +0000166 *
167 * Setting interrupt level:
168 *
169 * > set_irq_in QOM-PATH NAME NUM LEVEL
170 * < OK
171 *
172 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
173 * LEVEL is an signed integer IRQ level.
174 *
175 * Forcibly set the given interrupt pin to the given level.
176 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200177 */
178
179static int hex2nib(char ch)
180{
181 if (ch >= '0' && ch <= '9') {
182 return ch - '0';
183 } else if (ch >= 'a' && ch <= 'f') {
184 return 10 + (ch - 'a');
185 } else if (ch >= 'A' && ch <= 'F') {
Sergey Fedorov2a802aa2014-05-27 16:15:20 +0400186 return 10 + (ch - 'A');
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200187 } else {
188 return -1;
189 }
190}
191
Anthony Liguori6e924662012-03-30 14:04:04 -0500192static void qtest_get_time(qemu_timeval *tv)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200193{
Anthony Liguori6e924662012-03-30 14:04:04 -0500194 qemu_gettimeofday(tv);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200195 tv->tv_sec -= start_time.tv_sec;
196 tv->tv_usec -= start_time.tv_usec;
197 if (tv->tv_usec < 0) {
198 tv->tv_usec += 1000000;
199 tv->tv_sec -= 1;
200 }
201}
202
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300203static void qtest_send_prefix(CharBackend *chr)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200204{
Anthony Liguori6e924662012-03-30 14:04:04 -0500205 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200206
207 if (!qtest_log_fp || !qtest_opened) {
208 return;
209 }
210
211 qtest_get_time(&tv);
212 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700213 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200214}
215
John Snow7a6a7402015-05-22 14:13:44 -0400216static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
217{
218 va_list ap;
219
220 if (!qtest_log_fp || !qtest_opened) {
221 return;
222 }
223
224 qtest_send_prefix(NULL);
225
226 va_start(ap, fmt);
227 vfprintf(qtest_log_fp, fmt, ap);
228 va_end(ap);
229}
230
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300231static void do_qtest_send(CharBackend *chr, const char *str, size_t len)
John Snow332cc7e2015-05-22 14:13:43 -0400232{
233 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
234 if (qtest_log_fp && qtest_opened) {
235 fprintf(qtest_log_fp, "%s", str);
236 }
237}
238
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300239static void qtest_send(CharBackend *chr, const char *str)
John Snow332cc7e2015-05-22 14:13:43 -0400240{
241 do_qtest_send(chr, str, strlen(str));
242}
243
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300244static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
John Snow332cc7e2015-05-22 14:13:43 -0400245 const char *fmt, ...)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200246{
247 va_list ap;
John Snow332cc7e2015-05-22 14:13:43 -0400248 gchar *buffer;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200249
250 va_start(ap, fmt);
John Snow332cc7e2015-05-22 14:13:43 -0400251 buffer = g_strdup_vprintf(fmt, ap);
252 qtest_send(chr, buffer);
Marc-André Lureaufc340592016-11-10 12:25:00 +0400253 g_free(buffer);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200254 va_end(ap);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200255}
256
Paolo Bonzini20288342012-03-28 15:42:03 +0200257static void qtest_irq_handler(void *opaque, int n, int level)
258{
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700259 qemu_irq old_irq = *(qemu_irq *)opaque;
260 qemu_set_irq(old_irq, level);
Paolo Bonzini20288342012-03-28 15:42:03 +0200261
262 if (irq_levels[n] != level) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300263 CharBackend *chr = &qtest_chr;
Paolo Bonzini20288342012-03-28 15:42:03 +0200264 irq_levels[n] = level;
265 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400266 qtest_sendf(chr, "IRQ %s %d\n",
267 level ? "raise" : "lower", n);
Paolo Bonzini20288342012-03-28 15:42:03 +0200268 }
269}
270
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300271static void qtest_process_command(CharBackend *chr, gchar **words)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200272{
273 const gchar *command;
274
275 g_assert(words);
276
277 command = words[0];
278
279 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500280 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200281 int i;
282
283 qtest_get_time(&tv);
284 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700285 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200286 for (i = 0; words[i]; i++) {
287 fprintf(qtest_log_fp, " %s", words[i]);
288 }
289 fprintf(qtest_log_fp, "\n");
290 }
291
292 g_assert(command);
Paolo Bonzini20288342012-03-28 15:42:03 +0200293 if (strcmp(words[0], "irq_intercept_out") == 0
294 || strcmp(words[0], "irq_intercept_in") == 0) {
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700295 DeviceState *dev;
296 NamedGPIOList *ngl;
Paolo Bonzini20288342012-03-28 15:42:03 +0200297
298 g_assert(words[1]);
299 dev = DEVICE(object_resolve_path(words[1], NULL));
300 if (!dev) {
301 qtest_send_prefix(chr);
302 qtest_send(chr, "FAIL Unknown device\n");
Paolo Bonzini7d374352018-12-13 23:37:37 +0100303 return;
Paolo Bonzini20288342012-03-28 15:42:03 +0200304 }
305
306 if (irq_intercept_dev) {
307 qtest_send_prefix(chr);
308 if (irq_intercept_dev != dev) {
309 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
310 } else {
311 qtest_send(chr, "OK\n");
312 }
Paolo Bonzini7d374352018-12-13 23:37:37 +0100313 return;
Paolo Bonzini20288342012-03-28 15:42:03 +0200314 }
315
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700316 QLIST_FOREACH(ngl, &dev->gpios, node) {
317 /* We don't support intercept of named GPIOs yet */
318 if (ngl->name) {
319 continue;
320 }
321 if (words[0][14] == 'o') {
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700322 int i;
323 for (i = 0; i < ngl->num_out; ++i) {
324 qemu_irq *disconnected = g_new0(qemu_irq, 1);
325 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
326 disconnected, i);
327
328 *disconnected = qdev_intercept_gpio_out(dev, icpt,
329 ngl->name, i);
330 }
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700331 } else {
332 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
333 ngl->num_in);
334 }
Paolo Bonzini20288342012-03-28 15:42:03 +0200335 }
336 irq_intercept_dev = dev;
337 qtest_send_prefix(chr);
338 qtest_send(chr, "OK\n");
Steffen Görtz9813dc62019-01-07 15:23:47 +0000339 } else if (strcmp(words[0], "set_irq_in") == 0) {
340 DeviceState *dev;
341 qemu_irq irq;
342 char *name;
343 int ret;
344 int num;
345 int level;
Paolo Bonzini20288342012-03-28 15:42:03 +0200346
Steffen Görtz9813dc62019-01-07 15:23:47 +0000347 g_assert(words[1] && words[2] && words[3] && words[4]);
348
349 dev = DEVICE(object_resolve_path(words[1], NULL));
350 if (!dev) {
351 qtest_send_prefix(chr);
352 qtest_send(chr, "FAIL Unknown device\n");
353 return;
354 }
355
356 if (strcmp(words[2], "unnamed-gpio-in") == 0) {
357 name = NULL;
358 } else {
359 name = words[2];
360 }
361
362 ret = qemu_strtoi(words[3], NULL, 0, &num);
363 g_assert(!ret);
364 ret = qemu_strtoi(words[4], NULL, 0, &level);
365 g_assert(!ret);
366
367 irq = qdev_get_gpio_in_named(dev, name, num);
368
369 qemu_set_irq(irq, level);
370 qtest_send_prefix(chr);
371 qtest_send(chr, "OK\n");
Paolo Bonzini20288342012-03-28 15:42:03 +0200372 } else if (strcmp(words[0], "outb") == 0 ||
373 strcmp(words[0], "outw") == 0 ||
374 strcmp(words[0], "outl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200375 unsigned long addr;
376 unsigned long value;
Eric Blake14773122017-09-11 12:19:46 -0500377 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200378
379 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500380 ret = qemu_strtoul(words[1], NULL, 0, &addr);
381 g_assert(ret == 0);
382 ret = qemu_strtoul(words[2], NULL, 0, &value);
383 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200384 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200385
386 if (words[0][3] == 'b') {
387 cpu_outb(addr, value);
388 } else if (words[0][3] == 'w') {
389 cpu_outw(addr, value);
390 } else if (words[0][3] == 'l') {
391 cpu_outl(addr, value);
392 }
393 qtest_send_prefix(chr);
394 qtest_send(chr, "OK\n");
395 } else if (strcmp(words[0], "inb") == 0 ||
396 strcmp(words[0], "inw") == 0 ||
397 strcmp(words[0], "inl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200398 unsigned long addr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200399 uint32_t value = -1U;
Eric Blake14773122017-09-11 12:19:46 -0500400 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200401
402 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500403 ret = qemu_strtoul(words[1], NULL, 0, &addr);
404 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200405 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200406
407 if (words[0][2] == 'b') {
408 value = cpu_inb(addr);
409 } else if (words[0][2] == 'w') {
410 value = cpu_inw(addr);
411 } else if (words[0][2] == 'l') {
412 value = cpu_inl(addr);
413 }
414 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400415 qtest_sendf(chr, "OK 0x%04x\n", value);
Andreas Färber872536b2013-02-16 22:44:03 +0100416 } else if (strcmp(words[0], "writeb") == 0 ||
417 strcmp(words[0], "writew") == 0 ||
418 strcmp(words[0], "writel") == 0 ||
419 strcmp(words[0], "writeq") == 0) {
420 uint64_t addr;
421 uint64_t value;
Eric Blake14773122017-09-11 12:19:46 -0500422 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100423
424 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500425 ret = qemu_strtou64(words[1], NULL, 0, &addr);
426 g_assert(ret == 0);
427 ret = qemu_strtou64(words[2], NULL, 0, &value);
428 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100429
430 if (words[0][5] == 'b') {
431 uint8_t data = value;
Julia Suvorova70da3042018-07-02 09:52:37 +0300432 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
433 &data, 1, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100434 } else if (words[0][5] == 'w') {
435 uint16_t data = value;
436 tswap16s(&data);
Julia Suvorova70da3042018-07-02 09:52:37 +0300437 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
438 (uint8_t *) &data, 2, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100439 } else if (words[0][5] == 'l') {
440 uint32_t data = value;
441 tswap32s(&data);
Julia Suvorova70da3042018-07-02 09:52:37 +0300442 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
443 (uint8_t *) &data, 4, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100444 } else if (words[0][5] == 'q') {
445 uint64_t data = value;
446 tswap64s(&data);
Julia Suvorova70da3042018-07-02 09:52:37 +0300447 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
448 (uint8_t *) &data, 8, true);
Andreas Färber872536b2013-02-16 22:44:03 +0100449 }
450 qtest_send_prefix(chr);
451 qtest_send(chr, "OK\n");
452 } else if (strcmp(words[0], "readb") == 0 ||
453 strcmp(words[0], "readw") == 0 ||
454 strcmp(words[0], "readl") == 0 ||
455 strcmp(words[0], "readq") == 0) {
456 uint64_t addr;
457 uint64_t value = UINT64_C(-1);
Eric Blake14773122017-09-11 12:19:46 -0500458 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100459
460 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500461 ret = qemu_strtou64(words[1], NULL, 0, &addr);
462 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100463
464 if (words[0][4] == 'b') {
465 uint8_t data;
Julia Suvorova70da3042018-07-02 09:52:37 +0300466 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
467 &data, 1, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100468 value = data;
469 } else if (words[0][4] == 'w') {
470 uint16_t data;
Julia Suvorova70da3042018-07-02 09:52:37 +0300471 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
472 (uint8_t *) &data, 2, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100473 value = tswap16(data);
474 } else if (words[0][4] == 'l') {
475 uint32_t data;
Julia Suvorova70da3042018-07-02 09:52:37 +0300476 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
477 (uint8_t *) &data, 4, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100478 value = tswap32(data);
479 } else if (words[0][4] == 'q') {
Julia Suvorova70da3042018-07-02 09:52:37 +0300480 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
481 (uint8_t *) &value, 8, false);
Andreas Färber872536b2013-02-16 22:44:03 +0100482 tswap64s(&value);
483 }
484 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400485 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200486 } else if (strcmp(words[0], "read") == 0) {
487 uint64_t addr, len, i;
488 uint8_t *data;
John Snow5560b852015-05-22 14:13:44 -0400489 char *enc;
Eric Blake14773122017-09-11 12:19:46 -0500490 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200491
492 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500493 ret = qemu_strtou64(words[1], NULL, 0, &addr);
494 g_assert(ret == 0);
495 ret = qemu_strtou64(words[2], NULL, 0, &len);
496 g_assert(ret == 0);
Greg Kurz204febd2017-01-11 09:49:32 +0100497 /* We'd send garbage to libqtest if len is 0 */
498 g_assert(len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200499
500 data = g_malloc(len);
Julia Suvorova70da3042018-07-02 09:52:37 +0300501 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
502 data, len, false);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200503
John Snow5560b852015-05-22 14:13:44 -0400504 enc = g_malloc(2 * len + 1);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200505 for (i = 0; i < len; i++) {
John Snow5560b852015-05-22 14:13:44 -0400506 sprintf(&enc[i * 2], "%02x", data[i]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200507 }
John Snow5560b852015-05-22 14:13:44 -0400508
509 qtest_send_prefix(chr);
510 qtest_sendf(chr, "OK 0x%s\n", enc);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200511
512 g_free(data);
John Snow5560b852015-05-22 14:13:44 -0400513 g_free(enc);
John Snow7a6a7402015-05-22 14:13:44 -0400514 } else if (strcmp(words[0], "b64read") == 0) {
515 uint64_t addr, len;
516 uint8_t *data;
517 gchar *b64_data;
Eric Blake14773122017-09-11 12:19:46 -0500518 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400519
520 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500521 ret = qemu_strtou64(words[1], NULL, 0, &addr);
522 g_assert(ret == 0);
523 ret = qemu_strtou64(words[2], NULL, 0, &len);
524 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400525
526 data = g_malloc(len);
Julia Suvorova70da3042018-07-02 09:52:37 +0300527 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
528 data, len, false);
John Snow7a6a7402015-05-22 14:13:44 -0400529 b64_data = g_base64_encode(data, len);
530 qtest_send_prefix(chr);
531 qtest_sendf(chr, "OK %s\n", b64_data);
532
533 g_free(data);
534 g_free(b64_data);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200535 } else if (strcmp(words[0], "write") == 0) {
536 uint64_t addr, len, i;
537 uint8_t *data;
538 size_t data_len;
Eric Blake14773122017-09-11 12:19:46 -0500539 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200540
541 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500542 ret = qemu_strtou64(words[1], NULL, 0, &addr);
543 g_assert(ret == 0);
544 ret = qemu_strtou64(words[2], NULL, 0, &len);
545 g_assert(ret == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200546
547 data_len = strlen(words[3]);
548 if (data_len < 3) {
549 qtest_send(chr, "ERR invalid argument size\n");
550 return;
551 }
552
553 data = g_malloc(len);
554 for (i = 0; i < len; i++) {
555 if ((i * 2 + 4) <= data_len) {
556 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
557 data[i] |= hex2nib(words[3][i * 2 + 3]);
558 } else {
559 data[i] = 0;
560 }
561 }
Julia Suvorova70da3042018-07-02 09:52:37 +0300562 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
563 data, len, true);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200564 g_free(data);
565
566 qtest_send_prefix(chr);
567 qtest_send(chr, "OK\n");
John Snow4d007962015-05-22 14:13:44 -0400568 } else if (strcmp(words[0], "memset") == 0) {
569 uint64_t addr, len;
570 uint8_t *data;
Laurent Vivieraa15f492016-09-13 14:52:43 +0200571 unsigned long pattern;
Eric Blake14773122017-09-11 12:19:46 -0500572 int ret;
John Snow4d007962015-05-22 14:13:44 -0400573
574 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500575 ret = qemu_strtou64(words[1], NULL, 0, &addr);
576 g_assert(ret == 0);
577 ret = qemu_strtou64(words[2], NULL, 0, &len);
578 g_assert(ret == 0);
579 ret = qemu_strtoul(words[3], NULL, 0, &pattern);
580 g_assert(ret == 0);
John Snow4d007962015-05-22 14:13:44 -0400581
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100582 if (len) {
583 data = g_malloc(len);
584 memset(data, pattern, len);
Julia Suvorova70da3042018-07-02 09:52:37 +0300585 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
586 data, len, true);
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100587 g_free(data);
588 }
John Snow4d007962015-05-22 14:13:44 -0400589
590 qtest_send_prefix(chr);
591 qtest_send(chr, "OK\n");
John Snow7a6a7402015-05-22 14:13:44 -0400592 } else if (strcmp(words[0], "b64write") == 0) {
593 uint64_t addr, len;
594 uint8_t *data;
595 size_t data_len;
596 gsize out_len;
Eric Blake14773122017-09-11 12:19:46 -0500597 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400598
599 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500600 ret = qemu_strtou64(words[1], NULL, 0, &addr);
601 g_assert(ret == 0);
602 ret = qemu_strtou64(words[2], NULL, 0, &len);
603 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400604
605 data_len = strlen(words[3]);
606 if (data_len < 3) {
607 qtest_send(chr, "ERR invalid argument size\n");
608 return;
609 }
610
611 data = g_base64_decode_inplace(words[3], &out_len);
612 if (out_len != len) {
613 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
614 "found %zu)\n",
615 len, out_len);
616 out_len = MIN(out_len, len);
617 }
618
Julia Suvorova70da3042018-07-02 09:52:37 +0300619 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
620 data, len, true);
John Snow7a6a7402015-05-22 14:13:44 -0400621
622 qtest_send_prefix(chr);
623 qtest_send(chr, "OK\n");
Laurent Vivier54ce6f22016-10-07 12:14:27 +0200624 } else if (strcmp(words[0], "endianness") == 0) {
625 qtest_send_prefix(chr);
626#if defined(TARGET_WORDS_BIGENDIAN)
627 qtest_sendf(chr, "OK big\n");
628#else
629 qtest_sendf(chr, "OK little\n");
630#endif
Laurent Viviereeddd592016-09-13 14:52:45 +0200631#ifdef TARGET_PPC64
632 } else if (strcmp(words[0], "rtas") == 0) {
633 uint64_t res, args, ret;
634 unsigned long nargs, nret;
Eric Blake14773122017-09-11 12:19:46 -0500635 int rc;
Laurent Viviereeddd592016-09-13 14:52:45 +0200636
Eric Blake14773122017-09-11 12:19:46 -0500637 rc = qemu_strtoul(words[2], NULL, 0, &nargs);
638 g_assert(rc == 0);
639 rc = qemu_strtou64(words[3], NULL, 0, &args);
640 g_assert(rc == 0);
641 rc = qemu_strtoul(words[4], NULL, 0, &nret);
642 g_assert(rc == 0);
643 rc = qemu_strtou64(words[5], NULL, 0, &ret);
644 g_assert(rc == 0);
Laurent Viviereeddd592016-09-13 14:52:45 +0200645 res = qtest_rtas_call(words[1], nargs, args, nret, ret);
646
647 qtest_send_prefix(chr);
648 qtest_sendf(chr, "OK %"PRIu64"\n", res);
649#endif
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200650 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200651 int64_t ns;
652
653 if (words[1]) {
Eric Blake14773122017-09-11 12:19:46 -0500654 int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
655 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200656 } else {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300657 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
658 QEMU_TIMER_ATTR_ALL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200659 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100660 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200661 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400662 qtest_sendf(chr, "OK %"PRIi64"\n",
663 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Marc-André Lureaueb062cf2019-07-22 22:51:40 +0400664 } else if (strcmp(words[0], "module_load") == 0) {
665 g_assert(words[1] && words[2]);
666
667 qtest_send_prefix(chr);
668 if (module_load_one(words[1], words[2])) {
669 qtest_sendf(chr, "OK\n");
670 } else {
671 qtest_sendf(chr, "FAIL\n");
672 }
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200673 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200674 int64_t ns;
Eric Blake14773122017-09-11 12:19:46 -0500675 int ret;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200676
677 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500678 ret = qemu_strtoi64(words[1], NULL, 0, &ns);
679 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200680 qtest_clock_warp(ns);
681 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400682 qtest_sendf(chr, "OK %"PRIi64"\n",
683 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200684 } else {
685 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400686 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200687 }
688}
689
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300690static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200691{
692 char *end;
693
694 while ((end = strchr(inbuf->str, '\n')) != NULL) {
695 size_t offset;
696 GString *cmd;
697 gchar **words;
698
699 offset = end - inbuf->str;
700
701 cmd = g_string_new_len(inbuf->str, offset);
702 g_string_erase(inbuf, 0, offset + 1);
703
704 words = g_strsplit(cmd->str, " ", 0);
705 qtest_process_command(chr, words);
706 g_strfreev(words);
707
708 g_string_free(cmd, TRUE);
709 }
710}
711
712static void qtest_read(void *opaque, const uint8_t *buf, int size)
713{
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300714 CharBackend *chr = opaque;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200715
716 g_string_append_len(inbuf, (const gchar *)buf, size);
717 qtest_process_inbuf(chr, inbuf);
718}
719
720static int qtest_can_read(void *opaque)
721{
722 return 1024;
723}
724
725static void qtest_event(void *opaque, int event)
726{
727 int i;
728
729 switch (event) {
730 case CHR_EVENT_OPENED:
Markus Armbrusterba646ff2013-06-26 15:52:12 +0200731 /*
732 * We used to call qemu_system_reset() here, hoping we could
733 * use the same process for multiple tests that way. Never
734 * used. Injects an extra reset even when it's not used, and
735 * that can mess up tests, e.g. -boot once.
736 */
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200737 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
738 irq_levels[i] = 0;
739 }
Anthony Liguori6e924662012-03-30 14:04:04 -0500740 qemu_gettimeofday(&start_time);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200741 qtest_opened = true;
742 if (qtest_log_fp) {
743 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700744 (long) start_time.tv_sec, (long) start_time.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200745 }
746 break;
747 case CHR_EVENT_CLOSED:
748 qtest_opened = false;
749 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500750 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200751 qtest_get_time(&tv);
752 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700753 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200754 }
755 break;
756 default:
757 break;
758 }
759}
Oleinik, Alexander2b8985f2019-08-05 03:13:01 +0000760void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200761{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300762 Chardev *chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200763
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +0100764 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200765
Fam Zheng23802b42014-02-10 09:28:02 +0800766 if (chr == NULL) {
767 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
768 qtest_chrdev);
769 return;
770 }
771
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200772 if (qtest_log) {
773 if (strcmp(qtest_log, "none") != 0) {
774 qtest_log_fp = fopen(qtest_log, "w+");
775 }
776 } else {
777 qtest_log_fp = stderr;
778 }
779
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300780 qemu_chr_fe_init(&qtest_chr, chr, errp);
781 qemu_chr_fe_set_handlers(&qtest_chr, qtest_can_read, qtest_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300782 qtest_event, NULL, &qtest_chr, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300783 qemu_chr_fe_set_echo(&qtest_chr, true);
Li Liu107684c2014-10-22 10:26:47 +0800784
785 inbuf = g_string_new("");
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200786}
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200787
788bool qtest_driver(void)
789{
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300790 return qtest_chr.chr != NULL;
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200791}