4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
13 * This work is licensed under the terms of the GNU GPL, version 2 or later.
14 * See the COPYING file in the top-level directory.
17 #include "qemu/osdep.h"
21 #include <sys/socket.h>
25 #include "qapi/qmp/json-parser.h"
26 #include "qapi/qmp/json-streamer.h"
27 #include "qapi/qmp/qjson.h"
30 #define SOCKET_TIMEOUT 5
32 QTestState
*global_qtest
;
38 bool irq_level
[MAX_IRQ
];
40 pid_t qemu_pid
; /* our child QEMU process */
43 static GHookList abrt_hooks
;
44 static GList
*qtest_instances
;
45 static struct sigaction sigact_old
;
47 #define g_assert_no_errno(ret) do { \
48 g_assert_cmpint(ret, !=, -1); \
51 static int init_socket(const char *socket_path
)
53 struct sockaddr_un addr
;
57 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
58 g_assert_no_errno(sock
);
60 addr
.sun_family
= AF_UNIX
;
61 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
62 qemu_set_cloexec(sock
);
65 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
66 } while (ret
== -1 && errno
== EINTR
);
67 g_assert_no_errno(ret
);
68 ret
= listen(sock
, 1);
69 g_assert_no_errno(ret
);
74 static int socket_accept(int sock
)
76 struct sockaddr_un addr
;
79 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
82 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
86 addrlen
= sizeof(addr
);
87 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
88 } while (ret
== -1 && errno
== EINTR
);
90 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
97 static void kill_qemu(QTestState
*s
)
99 if (s
->qemu_pid
!= -1) {
100 kill(s
->qemu_pid
, SIGTERM
);
101 waitpid(s
->qemu_pid
, NULL
, 0);
105 static void kill_qemu_hook_func(void *s
)
110 static void sigabrt_handler(int signo
)
112 g_hook_list_invoke(&abrt_hooks
, FALSE
);
115 static void setup_sigabrt_handler(void)
117 struct sigaction sigact
;
119 /* Catch SIGABRT to clean up on g_assert() failure */
120 sigact
= (struct sigaction
){
121 .sa_handler
= sigabrt_handler
,
122 .sa_flags
= SA_RESETHAND
,
124 sigemptyset(&sigact
.sa_mask
);
125 sigaction(SIGABRT
, &sigact
, &sigact_old
);
128 static void cleanup_sigabrt_handler(void)
130 sigaction(SIGABRT
, &sigact_old
, NULL
);
133 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
137 /* Only install SIGABRT handler once */
138 if (!abrt_hooks
.is_setup
) {
139 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
140 setup_sigabrt_handler();
143 hook
= g_hook_alloc(&abrt_hooks
);
145 hook
->data
= (void *)data
;
147 g_hook_prepend(&abrt_hooks
, hook
);
150 QTestState
*qtest_init(const char *extra_args
)
153 int sock
, qmpsock
, i
;
155 gchar
*qmp_socket_path
;
157 const char *qemu_binary
;
159 qemu_binary
= getenv("QTEST_QEMU_BINARY");
160 g_assert(qemu_binary
!= NULL
);
162 s
= g_malloc(sizeof(*s
));
164 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
165 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
167 sock
= init_socket(socket_path
);
168 qmpsock
= init_socket(qmp_socket_path
);
170 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
172 s
->qemu_pid
= fork();
173 if (s
->qemu_pid
== 0) {
174 setenv("QEMU_AUDIO_DRV", "none", true);
175 command
= g_strdup_printf("exec %s "
176 "-qtest unix:%s,nowait "
178 "-qmp unix:%s,nowait "
179 "-machine accel=qtest "
181 "%s", qemu_binary
, socket_path
,
182 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
185 execlp("/bin/sh", "sh", "-c", command
, NULL
);
189 s
->fd
= socket_accept(sock
);
191 s
->qmp_fd
= socket_accept(qmpsock
);
194 unlink(qmp_socket_path
);
196 g_free(qmp_socket_path
);
198 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
200 s
->rx
= g_string_new("");
201 for (i
= 0; i
< MAX_IRQ
; i
++) {
202 s
->irq_level
[i
] = false;
205 /* Read the QMP greeting and then do the handshake */
206 qtest_qmp_discard_response(s
, "");
207 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
209 if (getenv("QTEST_STOP")) {
210 kill(s
->qemu_pid
, SIGSTOP
);
216 void qtest_quit(QTestState
*s
)
218 qtest_instances
= g_list_remove(qtest_instances
, s
);
219 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
221 /* Uninstall SIGABRT handler on last instance */
222 if (!qtest_instances
) {
223 cleanup_sigabrt_handler();
229 g_string_free(s
->rx
, true);
233 static void socket_send(int fd
, const char *buf
, size_t size
)
238 while (offset
< size
) {
241 len
= write(fd
, buf
+ offset
, size
- offset
);
242 if (len
== -1 && errno
== EINTR
) {
246 g_assert_no_errno(len
);
247 g_assert_cmpint(len
, >, 0);
253 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
255 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
256 size_t size
= strlen(str
);
258 socket_send(fd
, str
, size
);
262 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
267 socket_sendf(s
->fd
, fmt
, ap
);
271 static GString
*qtest_recv_line(QTestState
*s
)
277 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
281 len
= read(s
->fd
, buffer
, sizeof(buffer
));
282 if (len
== -1 && errno
== EINTR
) {
286 if (len
== -1 || len
== 0) {
287 fprintf(stderr
, "Broken pipe\n");
291 g_string_append_len(s
->rx
, buffer
, len
);
294 offset
= eol
- s
->rx
->str
;
295 line
= g_string_new_len(s
->rx
->str
, offset
);
296 g_string_erase(s
->rx
, 0, offset
+ 1);
301 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
308 line
= qtest_recv_line(s
);
309 words
= g_strsplit(line
->str
, " ", 0);
310 g_string_free(line
, TRUE
);
312 if (strcmp(words
[0], "IRQ") == 0) {
315 g_assert(words
[1] != NULL
);
316 g_assert(words
[2] != NULL
);
318 irq
= strtoul(words
[2], NULL
, 0);
319 g_assert_cmpint(irq
, >=, 0);
320 g_assert_cmpint(irq
, <, MAX_IRQ
);
322 if (strcmp(words
[1], "raise") == 0) {
323 s
->irq_level
[irq
] = true;
325 s
->irq_level
[irq
] = false;
332 g_assert(words
[0] != NULL
);
333 g_assert_cmpstr(words
[0], ==, "OK");
336 for (i
= 0; i
< expected_args
; i
++) {
337 g_assert(words
[i
] != NULL
);
347 JSONMessageParser parser
;
351 static void qmp_response(JSONMessageParser
*parser
, GQueue
*tokens
)
353 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
356 obj
= json_parser_parse(tokens
, NULL
);
358 fprintf(stderr
, "QMP JSON response parsing failed\n");
362 g_assert(qobject_type(obj
) == QTYPE_QDICT
);
363 g_assert(!qmp
->response
);
364 qmp
->response
= (QDict
*)obj
;
367 QDict
*qmp_fd_receive(int fd
)
369 QMPResponseParser qmp
;
370 bool log
= getenv("QTEST_LOG") != NULL
;
373 json_message_parser_init(&qmp
.parser
, qmp_response
);
374 while (!qmp
.response
) {
378 len
= read(fd
, &c
, 1);
379 if (len
== -1 && errno
== EINTR
) {
383 if (len
== -1 || len
== 0) {
384 fprintf(stderr
, "Broken pipe\n");
389 len
= write(2, &c
, 1);
391 json_message_parser_feed(&qmp
.parser
, &c
, 1);
393 json_message_parser_destroy(&qmp
.parser
);
398 QDict
*qtest_qmp_receive(QTestState
*s
)
400 return qmp_fd_receive(s
->qmp_fd
);
404 * Allow users to send a message without waiting for the reply,
405 * in the case that they choose to discard all replies up until
406 * a particular EVENT is received.
408 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
)
413 /* Going through qobject ensures we escape strings properly.
414 * This seemingly unnecessary copy is required in case va_list
417 va_copy(ap_copy
, ap
);
418 qobj
= qobject_from_jsonv(fmt
, &ap_copy
);
421 /* No need to send anything for an empty QObject. */
423 int log
= getenv("QTEST_LOG") != NULL
;
424 QString
*qstr
= qobject_to_json(qobj
);
425 const char *str
= qstring_get_str(qstr
);
426 size_t size
= qstring_get_length(qstr
);
429 fprintf(stderr
, "%s", str
);
431 /* Send QMP request */
432 socket_send(fd
, str
, size
);
435 qobject_decref(qobj
);
439 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
441 qmp_fd_sendv(s
->qmp_fd
, fmt
, ap
);
444 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
446 qmp_fd_sendv(fd
, fmt
, ap
);
448 return qmp_fd_receive(fd
);
451 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
453 qtest_async_qmpv(s
, fmt
, ap
);
456 return qtest_qmp_receive(s
);
459 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
465 response
= qmp_fdv(fd
, fmt
, ap
);
470 void qmp_fd_send(int fd
, const char *fmt
, ...)
475 qmp_fd_sendv(fd
, fmt
, ap
);
479 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
485 response
= qtest_qmpv(s
, fmt
, ap
);
490 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...)
495 qtest_async_qmpv(s
, fmt
, ap
);
499 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
501 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
505 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
511 response
= qtest_qmpv(s
, fmt
, ap
);
516 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
521 response
= qtest_qmp_receive(s
);
522 if ((qdict_haskey(response
, "event")) &&
523 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
531 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
)
537 cmd
= g_strdup_vprintf(fmt
, ap
);
538 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
539 " 'arguments': {'command-line': %s}}",
541 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
548 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
554 ret
= qtest_hmpv(s
, fmt
, ap
);
559 const char *qtest_get_arch(void)
561 const char *qemu
= getenv("QTEST_QEMU_BINARY");
562 g_assert(qemu
!= NULL
);
563 const char *end
= strrchr(qemu
, '/');
565 return end
+ strlen("/qemu-system-");
568 bool qtest_get_irq(QTestState
*s
, int num
)
570 /* dummy operation in order to make sure irq is up to date */
573 return s
->irq_level
[num
];
576 static int64_t qtest_clock_rsp(QTestState
*s
)
580 words
= qtest_rsp(s
, 2);
581 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
586 int64_t qtest_clock_step_next(QTestState
*s
)
588 qtest_sendf(s
, "clock_step\n");
589 return qtest_clock_rsp(s
);
592 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
594 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
595 return qtest_clock_rsp(s
);
598 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
600 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
601 return qtest_clock_rsp(s
);
604 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
606 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
610 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
612 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
616 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
618 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
622 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
624 qtest_out(s
, "outb", addr
, value
);
627 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
629 qtest_out(s
, "outw", addr
, value
);
632 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
634 qtest_out(s
, "outl", addr
, value
);
637 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
642 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
643 args
= qtest_rsp(s
, 2);
644 value
= strtoul(args
[1], NULL
, 0);
650 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
652 return qtest_in(s
, "inb", addr
);
655 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
657 return qtest_in(s
, "inw", addr
);
660 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
662 return qtest_in(s
, "inl", addr
);
665 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
668 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
672 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
674 qtest_write(s
, "writeb", addr
, value
);
677 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
679 qtest_write(s
, "writew", addr
, value
);
682 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
684 qtest_write(s
, "writel", addr
, value
);
687 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
689 qtest_write(s
, "writeq", addr
, value
);
692 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
697 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
698 args
= qtest_rsp(s
, 2);
699 value
= strtoull(args
[1], NULL
, 0);
705 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
707 return qtest_read(s
, "readb", addr
);
710 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
712 return qtest_read(s
, "readw", addr
);
715 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
717 return qtest_read(s
, "readl", addr
);
720 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
722 return qtest_read(s
, "readq", addr
);
725 static int hex2nib(char ch
)
727 if (ch
>= '0' && ch
<= '9') {
729 } else if (ch
>= 'a' && ch
<= 'f') {
730 return 10 + (ch
- 'a');
731 } else if (ch
>= 'A' && ch
<= 'F') {
732 return 10 + (ch
- 'a');
738 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
744 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
745 args
= qtest_rsp(s
, 2);
747 for (i
= 0; i
< size
; i
++) {
748 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
749 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
755 void qtest_add_func(const char *str
, void (*fn
)(void))
757 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
758 g_test_add_func(path
, fn
);
762 void qtest_add_data_func(const char *str
, const void *data
,
763 void (*fn
)(const void *))
765 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
766 g_test_add_data_func(path
, data
, fn
);
770 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
774 bdata
= g_base64_encode(data
, size
);
775 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
776 socket_send(s
->fd
, bdata
, strlen(bdata
));
777 socket_send(s
->fd
, "\n", 1);
782 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
787 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
788 args
= qtest_rsp(s
, 2);
790 g_base64_decode_inplace(args
[1], &len
);
792 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
794 len
= MIN(len
, size
);
797 memcpy(data
, args
[1], len
);
801 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
803 const uint8_t *ptr
= data
;
805 char *enc
= g_malloc(2 * size
+ 1);
807 for (i
= 0; i
< size
; i
++) {
808 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
811 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
816 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
818 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
822 QDict
*qmp(const char *fmt
, ...)
828 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
833 void qmp_async(const char *fmt
, ...)
838 qtest_async_qmpv(global_qtest
, fmt
, ap
);
842 void qmp_discard_response(const char *fmt
, ...)
847 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);
850 char *hmp(const char *fmt
, ...)
856 ret
= qtest_hmpv(global_qtest
, fmt
, ap
);
861 bool qtest_big_endian(void)
863 const char *arch
= qtest_get_arch();
866 static const struct {
870 { "aarch64", false },
877 { "microblaze", true },
878 { "microblazeel", false },
881 { "mips64el", false },
893 { "unicore32", false },
896 { "xtensaeb", true },
900 for (i
= 0; endianness
[i
].arch
; i
++) {
901 if (strcmp(endianness
[i
].arch
, arch
) == 0) {
902 return endianness
[i
].big_endian
;