master
c 1,106 lines 29.8 KB
Raw
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
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "system/qtest.h"
17 #include "system/runstate.h"
18 #include "chardev/char-fe.h"
19 #include "system/ioport.h"
20 #include "system/memory.h"
21 #include "exec/tswap.h"
22 #include "hw/core/qdev.h"
23 #include "hw/core/irq.h"
24 #include "hw/core/cpu.h"
25 #include "qemu/accel.h"
26 #include "system/cpu-timers.h"
27 #include "qemu/config-file.h"
28 #include "qemu/option.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qemu/cutils.h"
32 #include "qemu/target-info.h"
33 #include "qom/object_interfaces.h"
34 #include "qom/qom-qobject.h"
35 #include "qobject/qobject.h"
36 #include "qapi-commands-block.h"
37
38 #define MAX_IRQ 256
39
40 #define TYPE_QTEST "qtest"
41
42 OBJECT_DECLARE_SIMPLE_TYPE(QTest, QTEST)
43
44 struct QTest {
45 Object parent;
46
47 bool has_machine_link;
48 char *chr_name;
49 Chardev *chr;
50 CharFrontend qtest_chr;
51 char *log;
52 };
53
54 bool qtest_allowed;
55
56 static DeviceState *irq_intercept_dev;
57 static FILE *qtest_log_fp;
58 static QTest *qtest;
59 static GString *inbuf;
60 static int irq_levels[MAX_IRQ];
61 static GTimer *timer;
62 static bool qtest_opened;
63 static void (*qtest_server_send)(void*, const char*);
64 static void *qtest_server_send_opaque;
65
66 #define FMT_timeval "%.06f"
67
68 /**
69 * DOC: QTest Protocol
70 *
71 * Line based protocol, request/response based. Server can send async messages
72 * so clients should always handle many async messages before the response
73 * comes in.
74 *
75 * Extra ASCII space characters in command inputs are permitted and ignored.
76 * Lines containing only spaces are permitted and ignored.
77 * Lines that start with a '#' character (comments) are permitted and ignored.
78 *
79 * Valid requests
80 * ^^^^^^^^^^^^^^
81 *
82 * Clock management:
83 * """""""""""""""""
84 *
85 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
86 * let you adjust the value of the clock (monotonically). All the commands
87 * return the current value of the clock in nanoseconds.
88 *
89 * If the commands FAIL then time wasn't advanced which is likely
90 * because the machine was in a paused state or no timer events exist
91 * in the future. This will cause qtest to abort and the test will
92 * need to check its assumptions.
93 *
94 * .. code-block:: none
95 *
96 * > clock_step
97 * < OK VALUE
98 *
99 * Advance the clock to the next deadline. Useful when waiting for
100 * asynchronous events.
101 *
102 * .. code-block:: none
103 *
104 * > clock_step NS
105 * < OK VALUE
106 *
107 * Advance the clock by NS nanoseconds.
108 *
109 * .. code-block:: none
110 *
111 * > clock_set NS
112 * < OK VALUE
113 *
114 * Advance the clock to NS nanoseconds (do nothing if it's already past).
115 *
116 * PIO and memory access:
117 * """"""""""""""""""""""
118 *
119 * .. code-block:: none
120 *
121 * > outb ADDR VALUE
122 * < OK
123 *
124 * .. code-block:: none
125 *
126 * > outw ADDR VALUE
127 * < OK
128 *
129 * .. code-block:: none
130 *
131 * > outl ADDR VALUE
132 * < OK
133 *
134 * .. code-block:: none
135 *
136 * > inb ADDR
137 * < OK VALUE
138 *
139 * .. code-block:: none
140 *
141 * > inw ADDR
142 * < OK VALUE
143 *
144 * .. code-block:: none
145 *
146 * > inl ADDR
147 * < OK VALUE
148 *
149 * .. code-block:: none
150 *
151 * > writeb ADDR VALUE
152 * < OK
153 *
154 * .. code-block:: none
155 *
156 * > writew ADDR VALUE
157 * < OK
158 *
159 * .. code-block:: none
160 *
161 * > writel ADDR VALUE
162 * < OK
163 *
164 * .. code-block:: none
165 *
166 * > writeq ADDR VALUE
167 * < OK
168 *
169 * .. code-block:: none
170 *
171 * > readb ADDR
172 * < OK VALUE
173 *
174 * .. code-block:: none
175 *
176 * > readw ADDR
177 * < OK VALUE
178 *
179 * .. code-block:: none
180 *
181 * > readl ADDR
182 * < OK VALUE
183 *
184 * .. code-block:: none
185 *
186 * > readq ADDR
187 * < OK VALUE
188 *
189 * .. code-block:: none
190 *
191 * > read ADDR SIZE
192 * < OK DATA
193 *
194 * .. code-block:: none
195 *
196 * > write ADDR SIZE DATA
197 * < OK
198 *
199 * .. code-block:: none
200 *
201 * > b64read ADDR SIZE
202 * < OK B64_DATA
203 *
204 * .. code-block:: none
205 *
206 * > b64write ADDR SIZE B64_DATA
207 * < OK
208 *
209 * .. code-block:: none
210 *
211 * > memset ADDR SIZE VALUE
212 * < OK
213 *
214 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
215 * For 'memset' a zero size is permitted and does nothing.
216 *
217 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
218 * than the expected size, the value will be zero filled at the end of the data
219 * sequence.
220 *
221 * B64_DATA is an arbitrarily long base64 encoded string.
222 * If the sizes do not match, the data will be truncated.
223 *
224 * IRQ management:
225 * """""""""""""""
226 *
227 * .. code-block:: none
228 *
229 * > irq_intercept_in QOM-PATH
230 * < OK
231 *
232 * .. code-block:: none
233 *
234 * > irq_intercept_out QOM-PATH
235 * < OK
236 *
237 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
238 * QOM-PATH. When the pin is triggered, one of the following async messages
239 * will be printed to the qtest stream::
240 *
241 * IRQ raise NUM
242 * IRQ lower NUM
243 *
244 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
245 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
246 * NUM=0 even though it is remapped to GSI 2).
247 *
248 * Setting interrupt level:
249 * """"""""""""""""""""""""
250 *
251 * .. code-block:: none
252 *
253 * > set_irq_in QOM-PATH NAME NUM LEVEL
254 * < OK
255 *
256 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
257 * LEVEL is an signed integer IRQ level.
258 *
259 * Forcibly set the given interrupt pin to the given level.
260 *
261 */
262
263 static int hex2nib(char ch)
264 {
265 if (ch >= '0' && ch <= '9') {
266 return ch - '0';
267 } else if (ch >= 'a' && ch <= 'f') {
268 return 10 + (ch - 'a');
269 } else if (ch >= 'A' && ch <= 'F') {
270 return 10 + (ch - 'A');
271 } else {
272 return -1;
273 }
274 }
275
276 static void qtest_log_timestamp(void)
277 {
278 if (!qtest_log_fp || !qtest_opened) {
279 return;
280 }
281
282 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ", g_timer_elapsed(timer, NULL));
283 }
284
285 static void G_GNUC_PRINTF(1, 2) qtest_log_send(const char *fmt, ...)
286 {
287 va_list ap;
288
289 if (!qtest_log_fp || !qtest_opened) {
290 return;
291 }
292
293 qtest_log_timestamp();
294
295 va_start(ap, fmt);
296 vfprintf(qtest_log_fp, fmt, ap);
297 va_end(ap);
298 }
299
300 static void qtest_server_char_be_send(void *opaque, const char *str)
301 {
302 size_t len = strlen(str);
303 CharFrontend* chr = (CharFrontend *)opaque;
304 qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
305 if (qtest_log_fp && qtest_opened) {
306 fprintf(qtest_log_fp, "%s", str);
307 }
308 }
309
310 static void qtest_send(CharFrontend *chr, const char *str)
311 {
312 qtest_log_timestamp();
313 qtest_server_send(qtest_server_send_opaque, str);
314 }
315
316 void qtest_sendf(CharFrontend *chr, const char *fmt, ...)
317 {
318 va_list ap;
319 gchar *buffer;
320
321 va_start(ap, fmt);
322 buffer = g_strdup_vprintf(fmt, ap);
323 qtest_send(chr, buffer);
324 g_free(buffer);
325 va_end(ap);
326 }
327
328 static void qtest_irq_handler(void *opaque, int n, int level)
329 {
330 if (irq_levels[n] != level) {
331 CharFrontend *chr = &qtest->qtest_chr;
332 irq_levels[n] = level;
333 qtest_sendf(chr, "IRQ %s %d\n",
334 level ? "raise" : "lower", n);
335 }
336 }
337
338 static bool (*process_command_cb)(CharFrontend *chr, gchar **words);
339
340 void qtest_set_command_cb(bool (*pc_cb)(CharFrontend *chr, gchar **words))
341 {
342 assert(!process_command_cb); /* Switch to a list if we need more than one */
343
344 process_command_cb = pc_cb;
345 }
346
347 static void qtest_install_gpio_out_intercept(DeviceState *dev, const char *name, int n)
348 {
349 qemu_irq *disconnected = g_new0(qemu_irq, 1);
350 qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
351 disconnected, n);
352
353 *disconnected = qdev_intercept_gpio_out(dev, icpt, name, n);
354 }
355
356 static void qtest_process_command(CharFrontend *chr, gchar **words)
357 {
358 const gchar *command;
359
360 g_assert(words);
361
362 command = words[0];
363
364 if (qtest_log_fp) {
365 int i;
366
367 fprintf(qtest_log_fp, "[R +" FMT_timeval "]", g_timer_elapsed(timer, NULL));
368 for (i = 0; words[i]; i++) {
369 fprintf(qtest_log_fp, " %s", words[i]);
370 }
371 fprintf(qtest_log_fp, "\n");
372 }
373
374 if (!command || command[0] == '#') {
375 /* Input line was blank or a comment: ignore it */
376 return;
377 }
378
379 if (strcmp(words[0], "irq_intercept_out") == 0
380 || strcmp(words[0], "irq_intercept_in") == 0) {
381 DeviceState *dev;
382 NamedGPIOList *ngl;
383 bool is_named;
384 bool is_outbound;
385 bool interception_succeeded = false;
386
387 g_assert(words[1]);
388 is_named = words[2] != NULL;
389 is_outbound = words[0][14] == 'o';
390 dev = DEVICE(object_resolve_path(words[1], NULL));
391 if (!dev) {
392 qtest_send(chr, "FAIL Unknown device\n");
393 return;
394 }
395
396 if (is_named && !is_outbound) {
397 qtest_send(chr, "FAIL Interception of named in-GPIOs not yet supported\n");
398 return;
399 }
400
401 if (irq_intercept_dev) {
402 if (irq_intercept_dev != dev) {
403 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
404 } else {
405 qtest_send(chr, "OK\n");
406 }
407 return;
408 }
409
410 QLIST_FOREACH(ngl, &dev->gpios, node) {
411 /* We don't support inbound interception of named GPIOs yet */
412 if (is_outbound) {
413 /* NULL is valid and matchable, for "unnamed GPIO" */
414 if (g_strcmp0(ngl->name, words[2]) == 0) {
415 int i;
416 for (i = 0; i < ngl->num_out; ++i) {
417 qtest_install_gpio_out_intercept(dev, ngl->name, i);
418 }
419 interception_succeeded = true;
420 }
421 } else {
422 qemu_irq_set_observer(ngl->in, qtest_irq_handler,
423 ngl->num_in);
424 interception_succeeded = true;
425 }
426 }
427
428 if (interception_succeeded) {
429 irq_intercept_dev = dev;
430 qtest_send(chr, "OK\n");
431 } else {
432 qtest_send(chr, "FAIL No intercepts installed\n");
433 }
434 } else if (strcmp(words[0], "set_irq_in") == 0) {
435 DeviceState *dev;
436 qemu_irq irq;
437 char *name;
438 int ret;
439 int num;
440 int level;
441
442 g_assert(words[1] && words[2] && words[3] && words[4]);
443
444 dev = DEVICE(object_resolve_path(words[1], NULL));
445 if (!dev) {
446 qtest_send(chr, "FAIL Unknown device\n");
447 return;
448 }
449
450 if (strcmp(words[2], "unnamed-gpio-in") == 0) {
451 name = NULL;
452 } else {
453 name = words[2];
454 }
455
456 ret = qemu_strtoi(words[3], NULL, 0, &num);
457 g_assert(!ret);
458 ret = qemu_strtoi(words[4], NULL, 0, &level);
459 g_assert(!ret);
460
461 irq = qdev_get_gpio_in_named(dev, name, num);
462
463 qemu_set_irq(irq, level);
464 qtest_send(chr, "OK\n");
465 } else if (strcmp(words[0], "outb") == 0 ||
466 strcmp(words[0], "outw") == 0 ||
467 strcmp(words[0], "outl") == 0) {
468 unsigned long addr;
469 unsigned long value;
470 int ret;
471
472 g_assert(words[1] && words[2]);
473 ret = qemu_strtoul(words[1], NULL, 0, &addr);
474 g_assert(ret == 0);
475 ret = qemu_strtoul(words[2], NULL, 0, &value);
476 g_assert(ret == 0);
477 g_assert(addr <= 0xffff);
478
479 if (words[0][3] == 'b') {
480 cpu_outb(addr, value);
481 } else if (words[0][3] == 'w') {
482 cpu_outw(addr, value);
483 } else if (words[0][3] == 'l') {
484 cpu_outl(addr, value);
485 }
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) {
490 unsigned long addr;
491 uint32_t value = -1U;
492 int ret;
493
494 g_assert(words[1]);
495 ret = qemu_strtoul(words[1], NULL, 0, &addr);
496 g_assert(ret == 0);
497 g_assert(addr <= 0xffff);
498
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_sendf(chr, "OK 0x%04x\n", value);
507 } else if (strcmp(words[0], "writeb") == 0 ||
508 strcmp(words[0], "writew") == 0 ||
509 strcmp(words[0], "writel") == 0 ||
510 strcmp(words[0], "writeq") == 0) {
511 uint64_t addr;
512 uint64_t value;
513 int ret;
514
515 g_assert(words[1] && words[2]);
516 ret = qemu_strtou64(words[1], NULL, 0, &addr);
517 g_assert(ret == 0);
518 ret = qemu_strtou64(words[2], NULL, 0, &value);
519 g_assert(ret == 0);
520
521 if (words[0][5] == 'b') {
522 uint8_t data = value;
523 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
524 &data, 1);
525 } else if (words[0][5] == 'w') {
526 uint16_t data = value;
527 tswap16s(&data);
528 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
529 &data, 2);
530 } else if (words[0][5] == 'l') {
531 uint32_t data = value;
532 tswap32s(&data);
533 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
534 &data, 4);
535 } else if (words[0][5] == 'q') {
536 uint64_t data = value;
537 tswap64s(&data);
538 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
539 &data, 8);
540 }
541 qtest_send(chr, "OK\n");
542 } else if (strcmp(words[0], "readb") == 0 ||
543 strcmp(words[0], "readw") == 0 ||
544 strcmp(words[0], "readl") == 0 ||
545 strcmp(words[0], "readq") == 0) {
546 uint64_t addr;
547 uint64_t value = UINT64_C(-1);
548 int ret;
549
550 g_assert(words[1]);
551 ret = qemu_strtou64(words[1], NULL, 0, &addr);
552 g_assert(ret == 0);
553
554 if (words[0][4] == 'b') {
555 uint8_t data;
556 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
557 &data, 1);
558 value = data;
559 } else if (words[0][4] == 'w') {
560 uint16_t data;
561 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
562 &data, 2);
563 value = tswap16(data);
564 } else if (words[0][4] == 'l') {
565 uint32_t data;
566 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
567 &data, 4);
568 value = tswap32(data);
569 } else if (words[0][4] == 'q') {
570 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
571 &value, 8);
572 tswap64s(&value);
573 }
574 qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
575 } else if (strcmp(words[0], "read") == 0) {
576 g_autoptr(GString) enc = NULL;
577 uint64_t addr, len;
578 uint8_t *data;
579 int ret;
580
581 g_assert(words[1] && words[2]);
582 ret = qemu_strtou64(words[1], NULL, 0, &addr);
583 g_assert(ret == 0);
584 ret = qemu_strtou64(words[2], NULL, 0, &len);
585 g_assert(ret == 0);
586 /* We'd send garbage to libqtest if len is 0 */
587 g_assert(len);
588
589 data = g_malloc(len);
590 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
591 len);
592
593 enc = qemu_hexdump_line(NULL, data, len, 0, 0);
594
595 qtest_sendf(chr, "OK 0x%s\n", enc->str);
596
597 g_free(data);
598 } else if (strcmp(words[0], "b64read") == 0) {
599 uint64_t addr, len;
600 uint8_t *data;
601 gchar *b64_data;
602 int ret;
603
604 g_assert(words[1] && words[2]);
605 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);
609
610 data = g_malloc(len);
611 address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
612 len);
613 b64_data = g_base64_encode(data, len);
614 qtest_sendf(chr, "OK %s\n", b64_data);
615
616 g_free(data);
617 g_free(b64_data);
618 } else if (strcmp(words[0], "write") == 0) {
619 uint64_t addr, len, i;
620 uint8_t *data;
621 size_t data_len;
622 int ret;
623
624 g_assert(words[1] && words[2] && words[3]);
625 ret = qemu_strtou64(words[1], NULL, 0, &addr);
626 g_assert(ret == 0);
627 ret = qemu_strtou64(words[2], NULL, 0, &len);
628 g_assert(ret == 0);
629
630 data_len = strlen(words[3]);
631 if (data_len < 3) {
632 qtest_send(chr, "ERR invalid argument size\n");
633 return;
634 }
635
636 data = g_malloc(len);
637 for (i = 0; i < len; i++) {
638 if ((i * 2 + 4) <= data_len) {
639 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
640 data[i] |= hex2nib(words[3][i * 2 + 3]);
641 } else {
642 data[i] = 0;
643 }
644 }
645 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
646 len);
647 g_free(data);
648
649 qtest_send(chr, "OK\n");
650 } else if (strcmp(words[0], "memset") == 0) {
651 uint64_t addr, len;
652 uint8_t *data;
653 unsigned long pattern;
654 int ret;
655
656 g_assert(words[1] && words[2] && words[3]);
657 ret = qemu_strtou64(words[1], NULL, 0, &addr);
658 g_assert(ret == 0);
659 ret = qemu_strtou64(words[2], NULL, 0, &len);
660 g_assert(ret == 0);
661 ret = qemu_strtoul(words[3], NULL, 0, &pattern);
662 g_assert(ret == 0);
663
664 if (len) {
665 data = g_malloc(len);
666 memset(data, pattern, len);
667 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
668 data, len);
669 g_free(data);
670 }
671
672 qtest_send(chr, "OK\n");
673 } else if (strcmp(words[0], "b64write") == 0) {
674 uint64_t addr, len;
675 uint8_t *data;
676 size_t data_len;
677 gsize out_len;
678 int ret;
679
680 g_assert(words[1] && words[2] && words[3]);
681 ret = qemu_strtou64(words[1], NULL, 0, &addr);
682 g_assert(ret == 0);
683 ret = qemu_strtou64(words[2], NULL, 0, &len);
684 g_assert(ret == 0);
685
686 data_len = strlen(words[3]);
687 if (data_len < 3) {
688 qtest_send(chr, "ERR invalid argument size\n");
689 return;
690 }
691
692 data = g_base64_decode_inplace(words[3], &out_len);
693 if (out_len != len) {
694 qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
695 "found %zu)\n",
696 len, out_len);
697 out_len = MIN(out_len, len);
698 }
699
700 address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
701 out_len);
702
703 qtest_send(chr, "OK\n");
704 } else if (strcmp(words[0], "endianness") == 0) {
705 if (target_big_endian()) {
706 qtest_sendf(chr, "OK big\n");
707 } else {
708 qtest_sendf(chr, "OK little\n");
709 }
710 } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
711 int64_t old_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
712 int64_t ns, new_ns;
713
714 if (words[1]) {
715 int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
716 g_assert(ret == 0);
717 } else {
718 ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
719 QEMU_TIMER_ATTR_ALL);
720 if (ns < 0) {
721 qtest_send(chr, "FAIL "
722 "cannot advance clock to the next deadline "
723 "because there is no pending deadline\n");
724 return;
725 }
726 }
727 new_ns = qemu_clock_advance_virtual_time(old_ns + ns);
728 if (new_ns > old_ns) {
729 qtest_sendf(chr, "OK %"PRIi64"\n", new_ns);
730 } else {
731 qtest_sendf(chr, "FAIL could not advance time\n");
732 }
733 } else if (strcmp(words[0], "module_load") == 0) {
734 Error *local_err = NULL;
735 int rv;
736 g_assert(words[1] && words[2]);
737
738 rv = module_load(words[1], words[2], &local_err);
739 if (rv > 0) {
740 qtest_sendf(chr, "OK\n");
741 } else {
742 if (rv < 0) {
743 error_report_err(local_err);
744 }
745 qtest_sendf(chr, "FAIL\n");
746 }
747 } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
748 int64_t ns, new_ns;
749 int ret;
750
751 g_assert(words[1]);
752 ret = qemu_strtoi64(words[1], NULL, 0, &ns);
753 g_assert(ret == 0);
754 new_ns = qemu_clock_advance_virtual_time(ns);
755 qtest_sendf(chr, "%s %"PRIi64"\n",
756 new_ns == ns ? "OK" : "FAIL", new_ns);
757 } else if (strcmp(words[0], "qom-tests") == 0) {
758 GSList *list, *l;
759
760 list = object_class_get_list(NULL, false);
761 for (l = list; l; l = l->next) {
762 ObjectClass *klass = l->data;
763 const char *type_name = object_class_get_name(klass);
764 Object *obj;
765 ObjectPropertyIterator iter;
766 ObjectProperty *prop;
767
768 obj = object_new_with_class(klass);
769 object_property_iter_init(&iter, obj);
770 while ((prop = object_property_iter_next(&iter))) {
771 QObject *value;
772 Error *local_err = NULL;
773
774 value = object_property_get_qobject(obj, prop->name,
775 &local_err);
776 if (local_err) {
777 error_report("qom-tests: %s.%s: get failed: %s",
778 type_name, prop->name,
779 error_get_pretty(local_err));
780 error_free(local_err);
781 continue;
782 }
783
784 if (prop->set) {
785 if (!object_property_set_qobject(obj, prop->name, value,
786 &local_err)) {
787 error_report("qom-tests: %s.%s: set failed: %s",
788 type_name, prop->name,
789 error_get_pretty(local_err));
790 error_free(local_err);
791 }
792 }
793
794 qobject_unref(value);
795 }
796
797 object_unref(obj);
798 }
799 g_slist_free(list);
800 qtest_send(chr, "OK\n");
801 } else if (strcmp(words[0], "qemu-io") == 0) {
802 const char *io_dev;
803 g_autofree char *io_cmd = NULL;
804 Error *err = NULL;
805
806 g_assert(words[1] && words[2]);
807 io_dev = words[1];
808 io_cmd = g_strjoinv(" ", &words[2]);
809
810 qmp_x_qemu_io(io_dev, NULL, io_cmd, &err);
811 qtest_sendf(chr, err ? "FAIL %s\n" : "OK\n",
812 err ? error_get_pretty(err) : NULL);
813 error_free(err);
814 } else if (process_command_cb && process_command_cb(chr, words)) {
815 /* Command got consumed by the callback handler */
816 } else {
817 qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
818 }
819 }
820
821 /*
822 * Process as much of @inbuf as we can in newline terminated chunks.
823 * Remove the processed commands from @inbuf as we go.
824 */
825 static void qtest_process_inbuf(CharFrontend *chr, GString *inbuf)
826 {
827 char *end;
828
829 while ((end = strchr(inbuf->str, '\n')) != NULL) {
830 size_t len = end - inbuf->str;
831 g_autofree char *cmd = g_strndup(inbuf->str, len);
832 g_auto(GStrv) words = g_strsplit(cmd, " ", 0);
833
834 g_string_erase(inbuf, 0, len + 1);
835 qtest_process_command(chr, words);
836 }
837 }
838
839 static void qtest_read(void *opaque, const uint8_t *buf, int size)
840 {
841 CharFrontend *chr = opaque;
842
843 g_string_append_len(inbuf, (const gchar *)buf, size);
844 qtest_process_inbuf(chr, inbuf);
845 }
846
847 static int qtest_can_read(void *opaque)
848 {
849 return 1024;
850 }
851
852 static void qtest_event(void *opaque, QEMUChrEvent event)
853 {
854 int i;
855
856 switch (event) {
857 case CHR_EVENT_OPENED:
858 /*
859 * We used to call qemu_system_reset() here, hoping we could
860 * use the same process for multiple tests that way. Never
861 * used. Injects an extra reset even when it's not used, and
862 * that can mess up tests, e.g. -boot once.
863 */
864 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
865 irq_levels[i] = 0;
866 }
867
868 g_clear_pointer(&timer, g_timer_destroy);
869 timer = g_timer_new();
870 qtest_opened = true;
871 if (qtest_log_fp) {
872 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n", g_timer_elapsed(timer, NULL));
873 }
874 break;
875 case CHR_EVENT_CLOSED:
876 if (!qtest_opened) {
877 /* Ignore CLOSED events if we have already closed the log */
878 break;
879 }
880 qtest_opened = false;
881 if (qtest_log_fp) {
882 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n", g_timer_elapsed(timer, NULL));
883 }
884 g_clear_pointer(&timer, g_timer_destroy);
885 break;
886 default:
887 break;
888 }
889 }
890
891 void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
892 {
893 ERRP_GUARD();
894 Chardev *chr;
895 Object *qobj;
896
897 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
898 if (chr == NULL) {
899 error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
900 qtest_chrdev);
901 return;
902 }
903
904 qobj = object_new(TYPE_QTEST);
905 object_property_set_str(qobj, "chardev", chr->label, &error_abort);
906 if (qtest_log) {
907 object_property_set_str(qobj, "log", qtest_log, &error_abort);
908 }
909 object_property_add_child(qdev_get_machine(), "qtest", qobj);
910 user_creatable_complete(USER_CREATABLE(qobj), errp);
911 if (*errp) {
912 object_unparent(qobj);
913 }
914 object_unref(OBJECT(chr));
915 object_unref(qobj);
916 }
917
918 static bool qtest_server_start(QTest *q, Error **errp)
919 {
920 Chardev *chr = q->chr;
921 const char *qtest_log = q->log;
922
923 if (qtest_log) {
924 if (strcmp(qtest_log, "none") != 0) {
925 qtest_log_fp = fopen(qtest_log, "w+");
926 }
927 } else {
928 qtest_log_fp = stderr;
929 }
930
931 if (!qemu_chr_fe_init(&q->qtest_chr, chr, errp)) {
932 return false;
933 }
934 qemu_chr_fe_set_handlers(&q->qtest_chr, qtest_can_read, qtest_read,
935 qtest_event, NULL, &q->qtest_chr, NULL, true);
936 qemu_chr_fe_set_echo(&q->qtest_chr, true);
937
938 inbuf = g_string_new("");
939
940 if (!qtest_server_send) {
941 qtest_server_set_send_handler(qtest_server_char_be_send, &q->qtest_chr);
942 }
943 qtest = q;
944 return true;
945 }
946
947 void qtest_server_set_send_handler(void (*send)(void*, const char*),
948 void *opaque)
949 {
950 qtest_server_send = send;
951 qtest_server_send_opaque = opaque;
952 }
953
954 bool qtest_driver(void)
955 {
956 return qtest && qtest->qtest_chr.chr != NULL;
957 }
958
959 void qtest_server_inproc_recv(void *dummy, const char *buf)
960 {
961 static GString *gstr;
962 if (!gstr) {
963 gstr = g_string_new(NULL);
964 }
965 g_string_append(gstr, buf);
966 if (gstr->str[gstr->len - 1] == '\n') {
967 qtest_process_inbuf(NULL, gstr);
968 g_string_truncate(gstr, 0);
969 }
970 }
971
972 static void qtest_complete(UserCreatable *uc, Error **errp)
973 {
974 QTest *q = QTEST(uc);
975 if (qtest) {
976 error_setg(errp, "Only one instance of qtest can be created");
977 return;
978 }
979 if (!q->chr_name) {
980 error_setg(errp, "No backend specified");
981 return;
982 }
983
984 if (OBJECT(uc)->parent != qdev_get_machine()) {
985 q->has_machine_link = true;
986 object_property_add_const_link(qdev_get_machine(), "qtest", OBJECT(uc));
987 } else {
988 /* -qtest was used. */
989 }
990
991 qtest_server_start(q, errp);
992 }
993
994 static void qtest_unparent(Object *obj)
995 {
996 QTest *q = QTEST(obj);
997
998 if (qtest == q) {
999 qemu_chr_fe_disconnect(&q->qtest_chr);
1000 assert(!qtest_opened);
1001 qemu_chr_fe_deinit(&q->qtest_chr, false);
1002 if (qtest_log_fp) {
1003 fclose(qtest_log_fp);
1004 qtest_log_fp = NULL;
1005 }
1006 qtest = NULL;
1007 }
1008
1009 if (q->has_machine_link) {
1010 object_property_del(qdev_get_machine(), "qtest");
1011 q->has_machine_link = false;
1012 }
1013 }
1014
1015 static void qtest_set_log(Object *obj, const char *value, Error **errp)
1016 {
1017 QTest *q = QTEST(obj);
1018
1019 if (qtest == q) {
1020 error_setg(errp, "Property 'log' can not be set now");
1021 } else {
1022 g_free(q->log);
1023 q->log = g_strdup(value);
1024 }
1025 }
1026
1027 static char *qtest_get_log(Object *obj, Error **errp)
1028 {
1029 QTest *q = QTEST(obj);
1030
1031 return g_strdup(q->log);
1032 }
1033
1034 static void qtest_set_chardev(Object *obj, const char *value, Error **errp)
1035 {
1036 QTest *q = QTEST(obj);
1037 Chardev *chr;
1038
1039 if (qtest == q) {
1040 error_setg(errp, "Property 'chardev' can not be set now");
1041 return;
1042 }
1043
1044 chr = qemu_chr_find(value);
1045 if (!chr) {
1046 error_setg(errp, "Cannot find character device '%s'", value);
1047 return;
1048 }
1049
1050 g_free(q->chr_name);
1051 q->chr_name = g_strdup(value);
1052
1053 if (q->chr) {
1054 object_unref(q->chr);
1055 }
1056 q->chr = chr;
1057 object_ref(chr);
1058 }
1059
1060 static char *qtest_get_chardev(Object *obj, Error **errp)
1061 {
1062 QTest *q = QTEST(obj);
1063
1064 return g_strdup(q->chr_name);
1065 }
1066
1067 static void qtest_class_init(ObjectClass *oc, const void *data)
1068 {
1069 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
1070
1071 oc->unparent = qtest_unparent;
1072 ucc->complete = qtest_complete;
1073
1074 object_class_property_add_str(oc, "chardev",
1075 qtest_get_chardev, qtest_set_chardev);
1076 object_class_property_add_str(oc, "log",
1077 qtest_get_log, qtest_set_log);
1078 }
1079
1080 static void qtest_finalize(Object *obj)
1081 {
1082 QTest *q = QTEST(obj);
1083
1084 g_free(q->chr_name);
1085 g_free(q->log);
1086 object_unref(q->chr);
1087 }
1088
1089 static const TypeInfo qtest_info = {
1090 .name = TYPE_QTEST,
1091 .parent = TYPE_OBJECT,
1092 .class_init = qtest_class_init,
1093 .instance_finalize = qtest_finalize,
1094 .instance_size = sizeof(QTest),
1095 .interfaces = (const InterfaceInfo[]) {
1096 { TYPE_USER_CREATABLE },
1097 { }
1098 }
1099 };
1100
1101 static void register_types(void)
1102 {
1103 type_register_static(&qtest_info);
1104 }
1105
1106 type_init(register_types);