blob: 7965dc9a16ba88a479b34d1f4b19a9c71dc24583 [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"
Claudio Fontana740b1752020-08-19 13:17:19 +020024#include "sysemu/cpu-timers.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"
Paolo Bonzini2becc362020-02-03 11:42:03 +010030#include CONFIG_DEVICES
Laurent Vivier2c6e9182020-02-06 00:20:15 +010031#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/**
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040052 * DOC: QTest Protocol
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020053 *
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
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040059 * ^^^^^^^^^^^^^^
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020060 *
Paolo Bonzini8156be52012-03-28 15:42:04 +020061 * Clock management:
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040062 * """""""""""""""""
Paolo Bonzini8156be52012-03-28 15:42:04 +020063 *
Alex Blighbc72ad62013-08-21 16:03:08 +010064 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
Paolo Bonzini8156be52012-03-28 15:42:04 +020065 * let you adjust the value of the clock (monotonically). All the commands
66 * return the current value of the clock in nanoseconds.
67 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040068 * .. code-block:: none
69 *
Paolo Bonzini8156be52012-03-28 15:42:04 +020070 * > clock_step
71 * < OK VALUE
72 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040073 * Advance the clock to the next deadline. Useful when waiting for
74 * asynchronous events.
75 *
76 * .. code-block:: none
Paolo Bonzini8156be52012-03-28 15:42:04 +020077 *
78 * > clock_step NS
79 * < OK VALUE
80 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040081 * Advance the clock by NS nanoseconds.
82 *
83 * .. code-block:: none
Paolo Bonzini8156be52012-03-28 15:42:04 +020084 *
85 * > clock_set NS
86 * < OK VALUE
87 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040088 * Advance the clock to NS nanoseconds (do nothing if it's already past).
Paolo Bonzini8156be52012-03-28 15:42:04 +020089 *
90 * PIO and memory access:
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040091 * """"""""""""""""""""""
92 *
93 * .. code-block:: none
Paolo Bonzini8156be52012-03-28 15:42:04 +020094 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +020095 * > outb ADDR VALUE
96 * < OK
97 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -040098 * .. code-block:: none
99 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200100 * > outw ADDR VALUE
101 * < OK
102 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400103 * .. code-block:: none
104 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200105 * > outl ADDR VALUE
106 * < OK
107 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400108 * .. code-block:: none
109 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200110 * > inb ADDR
111 * < OK VALUE
112 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400113 * .. code-block:: none
114 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200115 * > inw ADDR
116 * < OK VALUE
117 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400118 * .. code-block:: none
119 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200120 * > inl ADDR
121 * < OK VALUE
122 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400123 * .. code-block:: none
124 *
Andreas Färber872536b2013-02-16 22:44:03 +0100125 * > writeb ADDR VALUE
126 * < OK
127 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400128 * .. code-block:: none
129 *
Andreas Färber872536b2013-02-16 22:44:03 +0100130 * > writew ADDR VALUE
131 * < OK
132 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400133 * .. code-block:: none
134 *
Andreas Färber872536b2013-02-16 22:44:03 +0100135 * > writel ADDR VALUE
136 * < OK
137 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400138 * .. code-block:: none
139 *
Andreas Färber872536b2013-02-16 22:44:03 +0100140 * > writeq ADDR VALUE
141 * < OK
142 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400143 * .. code-block:: none
144 *
Andreas Färber872536b2013-02-16 22:44:03 +0100145 * > readb ADDR
146 * < OK VALUE
147 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400148 * .. code-block:: none
149 *
Andreas Färber872536b2013-02-16 22:44:03 +0100150 * > readw ADDR
151 * < OK VALUE
152 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400153 * .. code-block:: none
154 *
Andreas Färber872536b2013-02-16 22:44:03 +0100155 * > readl ADDR
156 * < OK VALUE
157 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400158 * .. code-block:: none
159 *
Andreas Färber872536b2013-02-16 22:44:03 +0100160 * > readq ADDR
161 * < OK VALUE
162 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400163 * .. code-block:: none
164 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200165 * > read ADDR SIZE
166 * < OK DATA
167 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400168 * .. code-block:: none
169 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200170 * > write ADDR SIZE DATA
171 * < OK
172 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400173 * .. code-block:: none
174 *
John Snow7a6a7402015-05-22 14:13:44 -0400175 * > b64read ADDR SIZE
176 * < OK B64_DATA
177 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400178 * .. code-block:: none
179 *
John Snow7a6a7402015-05-22 14:13:44 -0400180 * > b64write ADDR SIZE B64_DATA
181 * < OK
182 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400183 * .. code-block:: none
184 *
John Snow4d007962015-05-22 14:13:44 -0400185 * > memset ADDR SIZE VALUE
186 * < OK
187 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200188 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100189 * For 'memset' a zero size is permitted and does nothing.
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200190 *
191 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
192 * than the expected size, the value will be zero filled at the end of the data
193 * sequence.
194 *
John Snow7a6a7402015-05-22 14:13:44 -0400195 * B64_DATA is an arbitrarily long base64 encoded string.
196 * If the sizes do not match, the data will be truncated.
197 *
Paolo Bonzini20288342012-03-28 15:42:03 +0200198 * IRQ management:
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400199 * """""""""""""""
200 *
201 * .. code-block:: none
Paolo Bonzini20288342012-03-28 15:42:03 +0200202 *
203 * > irq_intercept_in QOM-PATH
204 * < OK
205 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400206 * .. code-block:: none
207 *
Paolo Bonzini20288342012-03-28 15:42:03 +0200208 * > irq_intercept_out QOM-PATH
209 * < OK
210 *
211 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
212 * QOM-PATH. When the pin is triggered, one of the following async messages
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400213 * will be printed to the qtest stream::
Paolo Bonzini20288342012-03-28 15:42:03 +0200214 *
215 * IRQ raise NUM
216 * IRQ lower NUM
217 *
218 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
219 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
220 * NUM=0 even though it is remapped to GSI 2).
Steffen Görtz9813dc62019-01-07 15:23:47 +0000221 *
222 * Setting interrupt level:
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400223 * """"""""""""""""""""""""
224 *
225 * .. code-block:: none
Steffen Görtz9813dc62019-01-07 15:23:47 +0000226 *
227 * > set_irq_in QOM-PATH NAME NUM LEVEL
228 * < OK
229 *
Eduardo Habkostf59c6de2020-10-05 16:52:27 -0400230 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
231 * LEVEL is an signed integer IRQ level.
Steffen Görtz9813dc62019-01-07 15:23:47 +0000232 *
233 * Forcibly set the given interrupt pin to the given level.
234 *
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200235 */
236
237static int hex2nib(char ch)
238{
239 if (ch >= '0' && ch <= '9') {
240 return ch - '0';
241 } else if (ch >= 'a' && ch <= 'f') {
242 return 10 + (ch - 'a');
243 } else if (ch >= 'A' && ch <= 'F') {
Sergey Fedorov2a802aa2014-05-27 16:15:20 +0400244 return 10 + (ch - 'A');
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200245 } else {
246 return -1;
247 }
248}
249
Anthony Liguori6e924662012-03-30 14:04:04 -0500250static void qtest_get_time(qemu_timeval *tv)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200251{
Anthony Liguori6e924662012-03-30 14:04:04 -0500252 qemu_gettimeofday(tv);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200253 tv->tv_sec -= start_time.tv_sec;
254 tv->tv_usec -= start_time.tv_usec;
255 if (tv->tv_usec < 0) {
256 tv->tv_usec += 1000000;
257 tv->tv_sec -= 1;
258 }
259}
260
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300261static void qtest_send_prefix(CharBackend *chr)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200262{
Anthony Liguori6e924662012-03-30 14:04:04 -0500263 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200264
265 if (!qtest_log_fp || !qtest_opened) {
266 return;
267 }
268
269 qtest_get_time(&tv);
270 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700271 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200272}
273
John Snow7a6a7402015-05-22 14:13:44 -0400274static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
275{
276 va_list ap;
277
278 if (!qtest_log_fp || !qtest_opened) {
279 return;
280 }
281
282 qtest_send_prefix(NULL);
283
284 va_start(ap, fmt);
285 vfprintf(qtest_log_fp, fmt, ap);
286 va_end(ap);
287}
288
Alexander Bulekove731d082020-02-19 23:11:01 -0500289static void qtest_server_char_be_send(void *opaque, const char *str)
John Snow332cc7e2015-05-22 14:13:43 -0400290{
Alexander Bulekove731d082020-02-19 23:11:01 -0500291 size_t len = strlen(str);
292 CharBackend* chr = (CharBackend *)opaque;
John Snow332cc7e2015-05-22 14:13:43 -0400293 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
294 if (qtest_log_fp && qtest_opened) {
295 fprintf(qtest_log_fp, "%s", str);
296 }
297}
298
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300299static void qtest_send(CharBackend *chr, const char *str)
John Snow332cc7e2015-05-22 14:13:43 -0400300{
Alexander Bulekove731d082020-02-19 23:11:01 -0500301 qtest_server_send(qtest_server_send_opaque, str);
John Snow332cc7e2015-05-22 14:13:43 -0400302}
303
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300304static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
John Snow332cc7e2015-05-22 14:13:43 -0400305 const char *fmt, ...)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200306{
307 va_list ap;
John Snow332cc7e2015-05-22 14:13:43 -0400308 gchar *buffer;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200309
310 va_start(ap, fmt);
John Snow332cc7e2015-05-22 14:13:43 -0400311 buffer = g_strdup_vprintf(fmt, ap);
312 qtest_send(chr, buffer);
Marc-André Lureaufc340592016-11-10 12:25:00 +0400313 g_free(buffer);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200314 va_end(ap);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200315}
316
Paolo Bonzini20288342012-03-28 15:42:03 +0200317static void qtest_irq_handler(void *opaque, int n, int level)
318{
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700319 qemu_irq old_irq = *(qemu_irq *)opaque;
320 qemu_set_irq(old_irq, level);
Paolo Bonzini20288342012-03-28 15:42:03 +0200321
322 if (irq_levels[n] != level) {
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300323 CharBackend *chr = &qtest_chr;
Paolo Bonzini20288342012-03-28 15:42:03 +0200324 irq_levels[n] = level;
325 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400326 qtest_sendf(chr, "IRQ %s %d\n",
327 level ? "raise" : "lower", n);
Paolo Bonzini20288342012-03-28 15:42:03 +0200328 }
329}
330
Claudio Fontana740b1752020-08-19 13:17:19 +0200331static int64_t qtest_clock_counter;
332
333int64_t qtest_get_virtual_clock(void)
334{
335 return qatomic_read_i64(&qtest_clock_counter);
336}
337
338static void qtest_set_virtual_clock(int64_t count)
339{
340 qatomic_set_i64(&qtest_clock_counter, count);
341}
342
343static void qtest_clock_warp(int64_t dest)
344{
345 int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
346 AioContext *aio_context;
347 assert(qtest_enabled());
348 aio_context = qemu_get_aio_context();
349 while (clock < dest) {
350 int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
351 QEMU_TIMER_ATTR_ALL);
352 int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
353
354 qtest_set_virtual_clock(qtest_get_virtual_clock() + warp);
355
356 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
357 timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
358 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
359 }
360 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
361}
362
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300363static void qtest_process_command(CharBackend *chr, gchar **words)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200364{
365 const gchar *command;
366
367 g_assert(words);
368
369 command = words[0];
370
371 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500372 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200373 int i;
374
375 qtest_get_time(&tv);
376 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700377 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200378 for (i = 0; words[i]; i++) {
379 fprintf(qtest_log_fp, " %s", words[i]);
380 }
381 fprintf(qtest_log_fp, "\n");
382 }
383
384 g_assert(command);
Paolo Bonzini20288342012-03-28 15:42:03 +0200385 if (strcmp(words[0], "irq_intercept_out") == 0
386 || strcmp(words[0], "irq_intercept_in") == 0) {
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700387 DeviceState *dev;
388 NamedGPIOList *ngl;
Paolo Bonzini20288342012-03-28 15:42:03 +0200389
390 g_assert(words[1]);
391 dev = DEVICE(object_resolve_path(words[1], NULL));
392 if (!dev) {
393 qtest_send_prefix(chr);
394 qtest_send(chr, "FAIL Unknown device\n");
Paolo Bonzini7d374352018-12-13 23:37:37 +0100395 return;
Paolo Bonzini20288342012-03-28 15:42:03 +0200396 }
397
398 if (irq_intercept_dev) {
399 qtest_send_prefix(chr);
400 if (irq_intercept_dev != dev) {
401 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
402 } else {
403 qtest_send(chr, "OK\n");
404 }
Paolo Bonzini7d374352018-12-13 23:37:37 +0100405 return;
Paolo Bonzini20288342012-03-28 15:42:03 +0200406 }
407
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700408 QLIST_FOREACH(ngl, &dev->gpios, node) {
409 /* We don't support intercept of named GPIOs yet */
410 if (ngl->name) {
411 continue;
412 }
413 if (words[0][14] == 'o') {
Peter Crosthwaite60a79012014-09-25 22:21:31 -0700414 int i;
415 for (i = 0; i < ngl->num_out; ++i) {
416 qemu_irq *disconnected = g_new0(qemu_irq, 1);
417 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
418 disconnected, i);
419
420 *disconnected = qdev_intercept_gpio_out(dev, icpt,
421 ngl->name, i);
422 }
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700423 } else {
424 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
425 ngl->num_in);
426 }
Paolo Bonzini20288342012-03-28 15:42:03 +0200427 }
428 irq_intercept_dev = dev;
429 qtest_send_prefix(chr);
430 qtest_send(chr, "OK\n");
Steffen Görtz9813dc62019-01-07 15:23:47 +0000431 } else if (strcmp(words[0], "set_irq_in") == 0) {
432 DeviceState *dev;
433 qemu_irq irq;
434 char *name;
435 int ret;
436 int num;
437 int level;
Paolo Bonzini20288342012-03-28 15:42:03 +0200438
Steffen Görtz9813dc62019-01-07 15:23:47 +0000439 g_assert(words[1] && words[2] && words[3] && words[4]);
440
441 dev = DEVICE(object_resolve_path(words[1], NULL));
442 if (!dev) {
443 qtest_send_prefix(chr);
444 qtest_send(chr, "FAIL Unknown device\n");
445 return;
446 }
447
448 if (strcmp(words[2], "unnamed-gpio-in") == 0) {
449 name = NULL;
450 } else {
451 name = words[2];
452 }
453
454 ret = qemu_strtoi(words[3], NULL, 0, &num);
455 g_assert(!ret);
456 ret = qemu_strtoi(words[4], NULL, 0, &level);
457 g_assert(!ret);
458
459 irq = qdev_get_gpio_in_named(dev, name, num);
460
461 qemu_set_irq(irq, level);
462 qtest_send_prefix(chr);
463 qtest_send(chr, "OK\n");
Paolo Bonzini20288342012-03-28 15:42:03 +0200464 } else if (strcmp(words[0], "outb") == 0 ||
465 strcmp(words[0], "outw") == 0 ||
466 strcmp(words[0], "outl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200467 unsigned long addr;
468 unsigned long value;
Eric Blake14773122017-09-11 12:19:46 -0500469 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200470
471 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500472 ret = qemu_strtoul(words[1], NULL, 0, &addr);
473 g_assert(ret == 0);
474 ret = qemu_strtoul(words[2], NULL, 0, &value);
475 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200476 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200477
478 if (words[0][3] == 'b') {
479 cpu_outb(addr, value);
480 } else if (words[0][3] == 'w') {
481 cpu_outw(addr, value);
482 } else if (words[0][3] == 'l') {
483 cpu_outl(addr, value);
484 }
485 qtest_send_prefix(chr);
486 qtest_send(chr, "OK\n");
487 } else if (strcmp(words[0], "inb") == 0 ||
488 strcmp(words[0], "inw") == 0 ||
489 strcmp(words[0], "inl") == 0) {
Laurent Vivieraa15f492016-09-13 14:52:43 +0200490 unsigned long addr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200491 uint32_t value = -1U;
Eric Blake14773122017-09-11 12:19:46 -0500492 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200493
494 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500495 ret = qemu_strtoul(words[1], NULL, 0, &addr);
496 g_assert(ret == 0);
Laurent Vivieraa15f492016-09-13 14:52:43 +0200497 g_assert(addr <= 0xffff);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200498
499 if (words[0][2] == 'b') {
500 value = cpu_inb(addr);
501 } else if (words[0][2] == 'w') {
502 value = cpu_inw(addr);
503 } else if (words[0][2] == 'l') {
504 value = cpu_inl(addr);
505 }
506 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400507 qtest_sendf(chr, "OK 0x%04x\n", value);
Andreas Färber872536b2013-02-16 22:44:03 +0100508 } else if (strcmp(words[0], "writeb") == 0 ||
509 strcmp(words[0], "writew") == 0 ||
510 strcmp(words[0], "writel") == 0 ||
511 strcmp(words[0], "writeq") == 0) {
512 uint64_t addr;
513 uint64_t value;
Eric Blake14773122017-09-11 12:19:46 -0500514 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100515
516 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500517 ret = qemu_strtou64(words[1], NULL, 0, &addr);
518 g_assert(ret == 0);
519 ret = qemu_strtou64(words[2], NULL, 0, &value);
520 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100521
522 if (words[0][5] == 'b') {
523 uint8_t data = value;
Peter Maydell19f70342020-02-18 11:24:57 +0000524 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
525 &data, 1);
Andreas Färber872536b2013-02-16 22:44:03 +0100526 } else if (words[0][5] == 'w') {
527 uint16_t data = value;
528 tswap16s(&data);
Peter Maydell19f70342020-02-18 11:24:57 +0000529 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
530 &data, 2);
Andreas Färber872536b2013-02-16 22:44:03 +0100531 } else if (words[0][5] == 'l') {
532 uint32_t data = value;
533 tswap32s(&data);
Peter Maydell19f70342020-02-18 11:24:57 +0000534 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
535 &data, 4);
Andreas Färber872536b2013-02-16 22:44:03 +0100536 } else if (words[0][5] == 'q') {
537 uint64_t data = value;
538 tswap64s(&data);
Peter Maydell19f70342020-02-18 11:24:57 +0000539 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
540 &data, 8);
Andreas Färber872536b2013-02-16 22:44:03 +0100541 }
542 qtest_send_prefix(chr);
543 qtest_send(chr, "OK\n");
544 } else if (strcmp(words[0], "readb") == 0 ||
545 strcmp(words[0], "readw") == 0 ||
546 strcmp(words[0], "readl") == 0 ||
547 strcmp(words[0], "readq") == 0) {
548 uint64_t addr;
549 uint64_t value = UINT64_C(-1);
Eric Blake14773122017-09-11 12:19:46 -0500550 int ret;
Andreas Färber872536b2013-02-16 22:44:03 +0100551
552 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500553 ret = qemu_strtou64(words[1], NULL, 0, &addr);
554 g_assert(ret == 0);
Andreas Färber872536b2013-02-16 22:44:03 +0100555
556 if (words[0][4] == 'b') {
557 uint8_t data;
Peter Maydell19f70342020-02-18 11:24:57 +0000558 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
559 &data, 1);
Andreas Färber872536b2013-02-16 22:44:03 +0100560 value = data;
561 } else if (words[0][4] == 'w') {
562 uint16_t data;
Peter Maydell19f70342020-02-18 11:24:57 +0000563 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
564 &data, 2);
Andreas Färber872536b2013-02-16 22:44:03 +0100565 value = tswap16(data);
566 } else if (words[0][4] == 'l') {
567 uint32_t data;
Peter Maydell19f70342020-02-18 11:24:57 +0000568 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
569 &data, 4);
Andreas Färber872536b2013-02-16 22:44:03 +0100570 value = tswap32(data);
571 } else if (words[0][4] == 'q') {
Peter Maydell19f70342020-02-18 11:24:57 +0000572 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
573 &value, 8);
Andreas Färber872536b2013-02-16 22:44:03 +0100574 tswap64s(&value);
575 }
576 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400577 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200578 } else if (strcmp(words[0], "read") == 0) {
579 uint64_t addr, len, i;
580 uint8_t *data;
John Snow5560b852015-05-22 14:13:44 -0400581 char *enc;
Eric Blake14773122017-09-11 12:19:46 -0500582 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200583
584 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500585 ret = qemu_strtou64(words[1], NULL, 0, &addr);
586 g_assert(ret == 0);
587 ret = qemu_strtou64(words[2], NULL, 0, &len);
588 g_assert(ret == 0);
Greg Kurz204febd2017-01-11 09:49:32 +0100589 /* We'd send garbage to libqtest if len is 0 */
590 g_assert(len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200591
592 data = g_malloc(len);
Peter Maydell19f70342020-02-18 11:24:57 +0000593 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
594 len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200595
John Snow5560b852015-05-22 14:13:44 -0400596 enc = g_malloc(2 * len + 1);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200597 for (i = 0; i < len; i++) {
John Snow5560b852015-05-22 14:13:44 -0400598 sprintf(&enc[i * 2], "%02x", data[i]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200599 }
John Snow5560b852015-05-22 14:13:44 -0400600
601 qtest_send_prefix(chr);
602 qtest_sendf(chr, "OK 0x%s\n", enc);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200603
604 g_free(data);
John Snow5560b852015-05-22 14:13:44 -0400605 g_free(enc);
John Snow7a6a7402015-05-22 14:13:44 -0400606 } else if (strcmp(words[0], "b64read") == 0) {
607 uint64_t addr, len;
608 uint8_t *data;
609 gchar *b64_data;
Eric Blake14773122017-09-11 12:19:46 -0500610 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400611
612 g_assert(words[1] && words[2]);
Eric Blake14773122017-09-11 12:19:46 -0500613 ret = qemu_strtou64(words[1], NULL, 0, &addr);
614 g_assert(ret == 0);
615 ret = qemu_strtou64(words[2], NULL, 0, &len);
616 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400617
618 data = g_malloc(len);
Peter Maydell19f70342020-02-18 11:24:57 +0000619 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
620 len);
John Snow7a6a7402015-05-22 14:13:44 -0400621 b64_data = g_base64_encode(data, len);
622 qtest_send_prefix(chr);
623 qtest_sendf(chr, "OK %s\n", b64_data);
624
625 g_free(data);
626 g_free(b64_data);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200627 } else if (strcmp(words[0], "write") == 0) {
628 uint64_t addr, len, i;
629 uint8_t *data;
630 size_t data_len;
Eric Blake14773122017-09-11 12:19:46 -0500631 int ret;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200632
633 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500634 ret = qemu_strtou64(words[1], NULL, 0, &addr);
635 g_assert(ret == 0);
636 ret = qemu_strtou64(words[2], NULL, 0, &len);
637 g_assert(ret == 0);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200638
639 data_len = strlen(words[3]);
640 if (data_len < 3) {
641 qtest_send(chr, "ERR invalid argument size\n");
642 return;
643 }
644
645 data = g_malloc(len);
646 for (i = 0; i < len; i++) {
647 if ((i * 2 + 4) <= data_len) {
648 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
649 data[i] |= hex2nib(words[3][i * 2 + 3]);
650 } else {
651 data[i] = 0;
652 }
653 }
Peter Maydell19f70342020-02-18 11:24:57 +0000654 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
655 len);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200656 g_free(data);
657
658 qtest_send_prefix(chr);
659 qtest_send(chr, "OK\n");
John Snow4d007962015-05-22 14:13:44 -0400660 } else if (strcmp(words[0], "memset") == 0) {
661 uint64_t addr, len;
662 uint8_t *data;
Laurent Vivieraa15f492016-09-13 14:52:43 +0200663 unsigned long pattern;
Eric Blake14773122017-09-11 12:19:46 -0500664 int ret;
John Snow4d007962015-05-22 14:13:44 -0400665
666 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500667 ret = qemu_strtou64(words[1], NULL, 0, &addr);
668 g_assert(ret == 0);
669 ret = qemu_strtou64(words[2], NULL, 0, &len);
670 g_assert(ret == 0);
671 ret = qemu_strtoul(words[3], NULL, 0, &pattern);
672 g_assert(ret == 0);
John Snow4d007962015-05-22 14:13:44 -0400673
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100674 if (len) {
675 data = g_malloc(len);
676 memset(data, pattern, len);
Peter Maydell19f70342020-02-18 11:24:57 +0000677 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
678 data, len);
Peter Maydell5f31bbf2016-08-05 11:43:20 +0100679 g_free(data);
680 }
John Snow4d007962015-05-22 14:13:44 -0400681
682 qtest_send_prefix(chr);
683 qtest_send(chr, "OK\n");
John Snow7a6a7402015-05-22 14:13:44 -0400684 } else if (strcmp(words[0], "b64write") == 0) {
685 uint64_t addr, len;
686 uint8_t *data;
687 size_t data_len;
688 gsize out_len;
Eric Blake14773122017-09-11 12:19:46 -0500689 int ret;
John Snow7a6a7402015-05-22 14:13:44 -0400690
691 g_assert(words[1] && words[2] && words[3]);
Eric Blake14773122017-09-11 12:19:46 -0500692 ret = qemu_strtou64(words[1], NULL, 0, &addr);
693 g_assert(ret == 0);
694 ret = qemu_strtou64(words[2], NULL, 0, &len);
695 g_assert(ret == 0);
John Snow7a6a7402015-05-22 14:13:44 -0400696
697 data_len = strlen(words[3]);
698 if (data_len < 3) {
699 qtest_send(chr, "ERR invalid argument size\n");
700 return;
701 }
702
703 data = g_base64_decode_inplace(words[3], &out_len);
704 if (out_len != len) {
705 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
706 "found %zu)\n",
707 len, out_len);
708 out_len = MIN(out_len, len);
709 }
710
Peter Maydell19f70342020-02-18 11:24:57 +0000711 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
712 len);
John Snow7a6a7402015-05-22 14:13:44 -0400713
714 qtest_send_prefix(chr);
715 qtest_send(chr, "OK\n");
Laurent Vivier54ce6f22016-10-07 12:14:27 +0200716 } else if (strcmp(words[0], "endianness") == 0) {
717 qtest_send_prefix(chr);
718#if defined(TARGET_WORDS_BIGENDIAN)
719 qtest_sendf(chr, "OK big\n");
720#else
721 qtest_sendf(chr, "OK little\n");
722#endif
Laurent Vivier2c6e9182020-02-06 00:20:15 +0100723#ifdef CONFIG_PSERIES
Laurent Viviereeddd592016-09-13 14:52:45 +0200724 } else if (strcmp(words[0], "rtas") == 0) {
725 uint64_t res, args, ret;
726 unsigned long nargs, nret;
Eric Blake14773122017-09-11 12:19:46 -0500727 int rc;
Laurent Viviereeddd592016-09-13 14:52:45 +0200728
Eric Blake14773122017-09-11 12:19:46 -0500729 rc = qemu_strtoul(words[2], NULL, 0, &nargs);
730 g_assert(rc == 0);
731 rc = qemu_strtou64(words[3], NULL, 0, &args);
732 g_assert(rc == 0);
733 rc = qemu_strtoul(words[4], NULL, 0, &nret);
734 g_assert(rc == 0);
735 rc = qemu_strtou64(words[5], NULL, 0, &ret);
736 g_assert(rc == 0);
Laurent Viviereeddd592016-09-13 14:52:45 +0200737 res = qtest_rtas_call(words[1], nargs, args, nret, ret);
738
739 qtest_send_prefix(chr);
740 qtest_sendf(chr, "OK %"PRIu64"\n", res);
741#endif
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200742 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200743 int64_t ns;
744
745 if (words[1]) {
Eric Blake14773122017-09-11 12:19:46 -0500746 int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
747 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200748 } else {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300749 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
750 QEMU_TIMER_ATTR_ALL);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200751 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100752 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200753 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400754 qtest_sendf(chr, "OK %"PRIi64"\n",
755 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Marc-André Lureaueb062cf2019-07-22 22:51:40 +0400756 } else if (strcmp(words[0], "module_load") == 0) {
757 g_assert(words[1] && words[2]);
758
759 qtest_send_prefix(chr);
Gerd Hoffmann50109322020-09-23 11:12:17 +0200760 if (module_load_one(words[1], words[2], false)) {
Marc-André Lureaueb062cf2019-07-22 22:51:40 +0400761 qtest_sendf(chr, "OK\n");
762 } else {
763 qtest_sendf(chr, "FAIL\n");
764 }
Paolo Bonzinid4fce242013-10-18 13:51:11 +0200765 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
Paolo Bonzini8156be52012-03-28 15:42:04 +0200766 int64_t ns;
Eric Blake14773122017-09-11 12:19:46 -0500767 int ret;
Paolo Bonzini8156be52012-03-28 15:42:04 +0200768
769 g_assert(words[1]);
Eric Blake14773122017-09-11 12:19:46 -0500770 ret = qemu_strtoi64(words[1], NULL, 0, &ns);
771 g_assert(ret == 0);
Paolo Bonzini8156be52012-03-28 15:42:04 +0200772 qtest_clock_warp(ns);
773 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400774 qtest_sendf(chr, "OK %"PRIi64"\n",
775 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200776 } else {
777 qtest_send_prefix(chr);
John Snow332cc7e2015-05-22 14:13:43 -0400778 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200779 }
780}
781
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300782static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200783{
784 char *end;
785
786 while ((end = strchr(inbuf->str, '\n')) != NULL) {
787 size_t offset;
788 GString *cmd;
789 gchar **words;
790
791 offset = end - inbuf->str;
792
793 cmd = g_string_new_len(inbuf->str, offset);
794 g_string_erase(inbuf, 0, offset + 1);
795
796 words = g_strsplit(cmd->str, " ", 0);
797 qtest_process_command(chr, words);
798 g_strfreev(words);
799
800 g_string_free(cmd, TRUE);
801 }
802}
803
804static void qtest_read(void *opaque, const uint8_t *buf, int size)
805{
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300806 CharBackend *chr = opaque;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200807
808 g_string_append_len(inbuf, (const gchar *)buf, size);
809 qtest_process_inbuf(chr, inbuf);
810}
811
812static int qtest_can_read(void *opaque)
813{
814 return 1024;
815}
816
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +0100817static void qtest_event(void *opaque, QEMUChrEvent event)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200818{
819 int i;
820
821 switch (event) {
822 case CHR_EVENT_OPENED:
Markus Armbrusterba646ff2013-06-26 15:52:12 +0200823 /*
824 * We used to call qemu_system_reset() here, hoping we could
825 * use the same process for multiple tests that way. Never
826 * used. Injects an extra reset even when it's not used, and
827 * that can mess up tests, e.g. -boot once.
828 */
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200829 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
830 irq_levels[i] = 0;
831 }
Anthony Liguori6e924662012-03-30 14:04:04 -0500832 qemu_gettimeofday(&start_time);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200833 qtest_opened = true;
834 if (qtest_log_fp) {
835 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700836 (long) start_time.tv_sec, (long) start_time.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200837 }
838 break;
839 case CHR_EVENT_CLOSED:
840 qtest_opened = false;
841 if (qtest_log_fp) {
Anthony Liguori6e924662012-03-30 14:04:04 -0500842 qemu_timeval tv;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200843 qtest_get_time(&tv);
844 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
Richard Henderson35aa3fb2013-08-20 13:53:25 -0700845 (long) tv.tv_sec, (long) tv.tv_usec);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200846 }
847 break;
848 default:
849 break;
850 }
851}
Oleinik, Alexander2b8985f2019-08-05 03:13:01 +0000852void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200853{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300854 Chardev *chr;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200855
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +0100856 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200857
Fam Zheng23802b42014-02-10 09:28:02 +0800858 if (chr == NULL) {
859 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
860 qtest_chrdev);
861 return;
862 }
863
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200864 if (qtest_log) {
865 if (strcmp(qtest_log, "none") != 0) {
866 qtest_log_fp = fopen(qtest_log, "w+");
867 }
868 } else {
869 qtest_log_fp = stderr;
870 }
871
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300872 qemu_chr_fe_init(&qtest_chr, chr, errp);
873 qemu_chr_fe_set_handlers(&qtest_chr, qtest_can_read, qtest_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300874 qtest_event, NULL, &qtest_chr, NULL, true);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300875 qemu_chr_fe_set_echo(&qtest_chr, true);
Li Liu107684c2014-10-22 10:26:47 +0800876
877 inbuf = g_string_new("");
Alexander Bulekove731d082020-02-19 23:11:01 -0500878
879 if (!qtest_server_send) {
880 qtest_server_set_send_handler(qtest_server_char_be_send, &qtest_chr);
881 }
882}
883
Alexander Bulekov3fc92f82020-02-26 22:14:39 -0500884void qtest_server_set_send_handler(void (*send)(void*, const char*),
885 void *opaque)
Alexander Bulekove731d082020-02-19 23:11:01 -0500886{
887 qtest_server_send = send;
888 qtest_server_send_opaque = opaque;
Anthony Liguoric7f0f3b2012-03-28 15:42:02 +0200889}
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200890
891bool qtest_driver(void)
892{
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300893 return qtest_chr.chr != NULL;
Michael S. Tsirkinb3be57c2014-02-04 20:06:47 +0200894}
Alexander Bulekov0bd9aef2020-02-19 23:11:04 -0500895
896void qtest_server_inproc_recv(void *dummy, const char *buf)
897{
898 static GString *gstr;
899 if (!gstr) {
900 gstr = g_string_new(NULL);
901 }
902 g_string_append(gstr, buf);
903 if (gstr->str[gstr->len - 1] == '\n') {
904 qtest_process_inbuf(NULL, gstr);
905 g_string_truncate(gstr, 0);
906 }
907}