]> git.proxmox.com Git - mirror_qemu.git/blame - qtest.c
hw/i386/pc: Pass the apic_id_limit value by argument
[mirror_qemu.git] / qtest.c
CommitLineData
c7f0f3b1
AL
1/*
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
d38ea87a 14#include "qemu/osdep.h"
da34e65c 15#include "qapi/error.h"
33c11879 16#include "cpu.h"
9c17d615 17#include "sysemu/qtest.h"
54d31236 18#include "sysemu/runstate.h"
4d43a603 19#include "chardev/char-fe.h"
022c62cb
PB
20#include "exec/ioport.h"
21#include "exec/memory.h"
c7f0f3b1 22#include "hw/irq.h"
3a6ce514 23#include "sysemu/accel.h"
9c17d615 24#include "sysemu/cpus.h"
1ad9580b
ST
25#include "qemu/config-file.h"
26#include "qemu/option.h"
27#include "qemu/error-report.h"
0b8fa32f 28#include "qemu/module.h"
aa15f497 29#include "qemu/cutils.h"
eeddd59f
LV
30#ifdef TARGET_PPC64
31#include "hw/ppc/spapr_rtas.h"
32#endif
c7f0f3b1
AL
33
34#define MAX_IRQ 256
35
d5286af5 36bool qtest_allowed;
c7f0f3b1 37
20288345 38static DeviceState *irq_intercept_dev;
c7f0f3b1 39static FILE *qtest_log_fp;
32a6ebec 40static CharBackend qtest_chr;
c7f0f3b1
AL
41static GString *inbuf;
42static int irq_levels[MAX_IRQ];
6e92466a 43static qemu_timeval start_time;
c7f0f3b1
AL
44static bool qtest_opened;
45
6b7cff76 46#define FMT_timeval "%ld.%06ld"
c7f0f3b1
AL
47
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 *
8156be56
PB
57 * Clock management:
58 *
bc72ad67 59 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
8156be56
PB
60 * 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 *
c7f0f3b1
AL
81 * > 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 *
872536bf
AF
99 * > 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 *
c7f0f3b1
AL
123 * > read ADDR SIZE
124 * < OK DATA
125 *
126 * > write ADDR SIZE DATA
127 * < OK
128 *
7a6a740d
JS
129 * > b64read ADDR SIZE
130 * < OK B64_DATA
131 *
132 * > b64write ADDR SIZE B64_DATA
133 * < OK
134 *
4d007963
JS
135 * > memset ADDR SIZE VALUE
136 * < OK
137 *
c7f0f3b1 138 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
5f31bbf1 139 * For 'memset' a zero size is permitted and does nothing.
c7f0f3b1
AL
140 *
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 *
7a6a740d
JS
145 * B64_DATA is an arbitrarily long base64 encoded string.
146 * If the sizes do not match, the data will be truncated.
147 *
20288345
PB
148 * 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).
9813dc6a
SG
166 *
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 *
c7f0f3b1
AL
177 */
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') {
2a802aaf 186 return 10 + (ch - 'A');
c7f0f3b1
AL
187 } else {
188 return -1;
189 }
190}
191
6e92466a 192static void qtest_get_time(qemu_timeval *tv)
c7f0f3b1 193{
6e92466a 194 qemu_gettimeofday(tv);
c7f0f3b1
AL
195 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
5345fdb4 203static void qtest_send_prefix(CharBackend *chr)
c7f0f3b1 204{
6e92466a 205 qemu_timeval tv;
c7f0f3b1
AL
206
207 if (!qtest_log_fp || !qtest_opened) {
208 return;
209 }
210
211 qtest_get_time(&tv);
212 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
35aa3fb3 213 (long) tv.tv_sec, (long) tv.tv_usec);
c7f0f3b1
AL
214}
215
7a6a740d
JS
216static 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
5345fdb4 231static void do_qtest_send(CharBackend *chr, const char *str, size_t len)
332cc7e9
JS
232{
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
5345fdb4 239static void qtest_send(CharBackend *chr, const char *str)
332cc7e9
JS
240{
241 do_qtest_send(chr, str, strlen(str));
242}
243
5345fdb4 244static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharBackend *chr,
332cc7e9 245 const char *fmt, ...)
c7f0f3b1
AL
246{
247 va_list ap;
332cc7e9 248 gchar *buffer;
c7f0f3b1
AL
249
250 va_start(ap, fmt);
332cc7e9
JS
251 buffer = g_strdup_vprintf(fmt, ap);
252 qtest_send(chr, buffer);
fc34059f 253 g_free(buffer);
c7f0f3b1 254 va_end(ap);
c7f0f3b1
AL
255}
256
20288345
PB
257static void qtest_irq_handler(void *opaque, int n, int level)
258{
60a79016
PC
259 qemu_irq old_irq = *(qemu_irq *)opaque;
260 qemu_set_irq(old_irq, level);
20288345
PB
261
262 if (irq_levels[n] != level) {
5345fdb4 263 CharBackend *chr = &qtest_chr;
20288345
PB
264 irq_levels[n] = level;
265 qtest_send_prefix(chr);
332cc7e9
JS
266 qtest_sendf(chr, "IRQ %s %d\n",
267 level ? "raise" : "lower", n);
20288345
PB
268 }
269}
270
5345fdb4 271static void qtest_process_command(CharBackend *chr, gchar **words)
c7f0f3b1
AL
272{
273 const gchar *command;
274
275 g_assert(words);
276
277 command = words[0];
278
279 if (qtest_log_fp) {
6e92466a 280 qemu_timeval tv;
c7f0f3b1
AL
281 int i;
282
283 qtest_get_time(&tv);
284 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
35aa3fb3 285 (long) tv.tv_sec, (long) tv.tv_usec);
c7f0f3b1
AL
286 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);
20288345
PB
293 if (strcmp(words[0], "irq_intercept_out") == 0
294 || strcmp(words[0], "irq_intercept_in") == 0) {
a5f54290
PC
295 DeviceState *dev;
296 NamedGPIOList *ngl;
20288345
PB
297
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");
7d37435b 303 return;
20288345
PB
304 }
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 }
7d37435b 313 return;
20288345
PB
314 }
315
a5f54290
PC
316 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') {
60a79016
PC
322 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 }
a5f54290
PC
331 } else {
332 qemu_irq_intercept_in(ngl->in, qtest_irq_handler,
333 ngl->num_in);
334 }
20288345
PB
335 }
336 irq_intercept_dev = dev;
337 qtest_send_prefix(chr);
338 qtest_send(chr, "OK\n");
9813dc6a
SG
339 } 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;
346
347 g_assert(words[1] && words[2] && words[3] && words[4]);
20288345 348
9813dc6a
SG
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");
20288345
PB
372 } else if (strcmp(words[0], "outb") == 0 ||
373 strcmp(words[0], "outw") == 0 ||
374 strcmp(words[0], "outl") == 0) {
aa15f497
LV
375 unsigned long addr;
376 unsigned long value;
14773125 377 int ret;
c7f0f3b1
AL
378
379 g_assert(words[1] && words[2]);
14773125
EB
380 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);
aa15f497 384 g_assert(addr <= 0xffff);
c7f0f3b1
AL
385
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) {
aa15f497 398 unsigned long addr;
c7f0f3b1 399 uint32_t value = -1U;
14773125 400 int ret;
c7f0f3b1
AL
401
402 g_assert(words[1]);
14773125
EB
403 ret = qemu_strtoul(words[1], NULL, 0, &addr);
404 g_assert(ret == 0);
aa15f497 405 g_assert(addr <= 0xffff);
c7f0f3b1
AL
406
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);
332cc7e9 415 qtest_sendf(chr, "OK 0x%04x\n", value);
872536bf
AF
416 } 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;
14773125 422 int ret;
872536bf
AF
423
424 g_assert(words[1] && words[2]);
14773125
EB
425 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);
872536bf
AF
429
430 if (words[0][5] == 'b') {
431 uint8_t data = value;
70da3048
JS
432 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
433 &data, 1, true);
872536bf
AF
434 } else if (words[0][5] == 'w') {
435 uint16_t data = value;
436 tswap16s(&data);
70da3048
JS
437 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
438 (uint8_t *) &data, 2, true);
872536bf
AF
439 } else if (words[0][5] == 'l') {
440 uint32_t data = value;
441 tswap32s(&data);
70da3048
JS
442 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
443 (uint8_t *) &data, 4, true);
872536bf
AF
444 } else if (words[0][5] == 'q') {
445 uint64_t data = value;
446 tswap64s(&data);
70da3048
JS
447 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
448 (uint8_t *) &data, 8, true);
872536bf
AF
449 }
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);
14773125 458 int ret;
872536bf
AF
459
460 g_assert(words[1]);
14773125
EB
461 ret = qemu_strtou64(words[1], NULL, 0, &addr);
462 g_assert(ret == 0);
872536bf
AF
463
464 if (words[0][4] == 'b') {
465 uint8_t data;
70da3048
JS
466 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
467 &data, 1, false);
872536bf
AF
468 value = data;
469 } else if (words[0][4] == 'w') {
470 uint16_t data;
70da3048
JS
471 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
472 (uint8_t *) &data, 2, false);
872536bf
AF
473 value = tswap16(data);
474 } else if (words[0][4] == 'l') {
475 uint32_t data;
70da3048
JS
476 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
477 (uint8_t *) &data, 4, false);
872536bf
AF
478 value = tswap32(data);
479 } else if (words[0][4] == 'q') {
70da3048
JS
480 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
481 (uint8_t *) &value, 8, false);
872536bf
AF
482 tswap64s(&value);
483 }
484 qtest_send_prefix(chr);
332cc7e9 485 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
c7f0f3b1
AL
486 } else if (strcmp(words[0], "read") == 0) {
487 uint64_t addr, len, i;
488 uint8_t *data;
5560b85a 489 char *enc;
14773125 490 int ret;
c7f0f3b1
AL
491
492 g_assert(words[1] && words[2]);
14773125
EB
493 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);
204febd1
GK
497 /* We'd send garbage to libqtest if len is 0 */
498 g_assert(len);
c7f0f3b1
AL
499
500 data = g_malloc(len);
70da3048
JS
501 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
502 data, len, false);
c7f0f3b1 503
5560b85a 504 enc = g_malloc(2 * len + 1);
c7f0f3b1 505 for (i = 0; i < len; i++) {
5560b85a 506 sprintf(&enc[i * 2], "%02x", data[i]);
c7f0f3b1 507 }
5560b85a
JS
508
509 qtest_send_prefix(chr);
510 qtest_sendf(chr, "OK 0x%s\n", enc);
c7f0f3b1
AL
511
512 g_free(data);
5560b85a 513 g_free(enc);
7a6a740d
JS
514 } else if (strcmp(words[0], "b64read") == 0) {
515 uint64_t addr, len;
516 uint8_t *data;
517 gchar *b64_data;
14773125 518 int ret;
7a6a740d
JS
519
520 g_assert(words[1] && words[2]);
14773125
EB
521 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);
7a6a740d
JS
525
526 data = g_malloc(len);
70da3048
JS
527 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
528 data, len, false);
7a6a740d
JS
529 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);
c7f0f3b1
AL
535 } else if (strcmp(words[0], "write") == 0) {
536 uint64_t addr, len, i;
537 uint8_t *data;
538 size_t data_len;
14773125 539 int ret;
c7f0f3b1
AL
540
541 g_assert(words[1] && words[2] && words[3]);
14773125
EB
542 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);
c7f0f3b1
AL
546
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 }
70da3048
JS
562 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
563 data, len, true);
c7f0f3b1
AL
564 g_free(data);
565
4d007963
JS
566 qtest_send_prefix(chr);
567 qtest_send(chr, "OK\n");
568 } else if (strcmp(words[0], "memset") == 0) {
569 uint64_t addr, len;
570 uint8_t *data;
aa15f497 571 unsigned long pattern;
14773125 572 int ret;
4d007963
JS
573
574 g_assert(words[1] && words[2] && words[3]);
14773125
EB
575 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);
4d007963 581
5f31bbf1
PM
582 if (len) {
583 data = g_malloc(len);
584 memset(data, pattern, len);
70da3048
JS
585 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
586 data, len, true);
5f31bbf1
PM
587 g_free(data);
588 }
4d007963 589
7a6a740d
JS
590 qtest_send_prefix(chr);
591 qtest_send(chr, "OK\n");
592 } 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;
14773125 597 int ret;
7a6a740d
JS
598
599 g_assert(words[1] && words[2] && words[3]);
14773125
EB
600 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);
7a6a740d
JS
604
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
70da3048
JS
619 address_space_rw(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
620 data, len, true);
7a6a740d 621
c7f0f3b1
AL
622 qtest_send_prefix(chr);
623 qtest_send(chr, "OK\n");
54ce6f22
LV
624 } 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
eeddd59f
LV
631#ifdef TARGET_PPC64
632 } else if (strcmp(words[0], "rtas") == 0) {
633 uint64_t res, args, ret;
634 unsigned long nargs, nret;
14773125
EB
635 int rc;
636
637 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);
eeddd59f
LV
645 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
d4fce24f 650 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
8156be56
PB
651 int64_t ns;
652
653 if (words[1]) {
14773125
EB
654 int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
655 g_assert(ret == 0);
8156be56 656 } else {
dcb15780
PD
657 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
658 QEMU_TIMER_ATTR_ALL);
8156be56 659 }
bc72ad67 660 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns);
8156be56 661 qtest_send_prefix(chr);
332cc7e9
JS
662 qtest_sendf(chr, "OK %"PRIi64"\n",
663 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
eb062cfa
MAL
664 } 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 }
d4fce24f 673 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
8156be56 674 int64_t ns;
14773125 675 int ret;
8156be56
PB
676
677 g_assert(words[1]);
14773125
EB
678 ret = qemu_strtoi64(words[1], NULL, 0, &ns);
679 g_assert(ret == 0);
8156be56
PB
680 qtest_clock_warp(ns);
681 qtest_send_prefix(chr);
332cc7e9
JS
682 qtest_sendf(chr, "OK %"PRIi64"\n",
683 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
c7f0f3b1
AL
684 } else {
685 qtest_send_prefix(chr);
332cc7e9 686 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
c7f0f3b1
AL
687 }
688}
689
5345fdb4 690static void qtest_process_inbuf(CharBackend *chr, GString *inbuf)
c7f0f3b1
AL
691{
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{
5345fdb4 714 CharBackend *chr = opaque;
c7f0f3b1
AL
715
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:
ba646ff6
MA
731 /*
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 */
c7f0f3b1
AL
737 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
738 irq_levels[i] = 0;
739 }
6e92466a 740 qemu_gettimeofday(&start_time);
c7f0f3b1
AL
741 qtest_opened = true;
742 if (qtest_log_fp) {
743 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
35aa3fb3 744 (long) start_time.tv_sec, (long) start_time.tv_usec);
c7f0f3b1
AL
745 }
746 break;
747 case CHR_EVENT_CLOSED:
748 qtest_opened = false;
749 if (qtest_log_fp) {
6e92466a 750 qemu_timeval tv;
c7f0f3b1
AL
751 qtest_get_time(&tv);
752 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
35aa3fb3 753 (long) tv.tv_sec, (long) tv.tv_usec);
c7f0f3b1
AL
754 }
755 break;
756 default:
757 break;
758 }
759}
2b8985f1 760void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
d4fce24f 761{
0ec7b3e7 762 Chardev *chr;
c7f0f3b1 763
4ad6f6cb 764 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
c7f0f3b1 765
23802b4f
FZ
766 if (chr == NULL) {
767 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
768 qtest_chrdev);
769 return;
770 }
771
c7f0f3b1
AL
772 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
5345fdb4
MAL
780 qemu_chr_fe_init(&qtest_chr, chr, errp);
781 qemu_chr_fe_set_handlers(&qtest_chr, qtest_can_read, qtest_read,
81517ba3 782 qtest_event, NULL, &qtest_chr, NULL, true);
5345fdb4 783 qemu_chr_fe_set_echo(&qtest_chr, true);
107684c0
LL
784
785 inbuf = g_string_new("");
c7f0f3b1 786}
b3be57c3
MT
787
788bool qtest_driver(void)
789{
32a6ebec 790 return qtest_chr.chr != NULL;
b3be57c3 791}