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.
20 #include <sys/types.h>
21 #include <sys/socket.h>
31 #include "qemu/compiler.h"
32 #include "qemu/osdep.h"
33 #include "qapi/qmp/json-parser.h"
34 #include "qapi/qmp/json-streamer.h"
35 #include "qapi/qmp/qjson.h"
38 #define SOCKET_TIMEOUT 5
40 QTestState
*global_qtest
;
46 bool irq_level
[MAX_IRQ
];
48 pid_t qemu_pid
; /* our child QEMU process */
49 struct sigaction sigact_old
; /* restored on exit */
52 static GList
*qtest_instances
;
53 static struct sigaction sigact_old
;
55 #define g_assert_no_errno(ret) do { \
56 g_assert_cmpint(ret, !=, -1); \
59 static int init_socket(const char *socket_path
)
61 struct sockaddr_un addr
;
65 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
66 g_assert_no_errno(sock
);
68 addr
.sun_family
= AF_UNIX
;
69 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
70 qemu_set_cloexec(sock
);
73 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
74 } while (ret
== -1 && errno
== EINTR
);
75 g_assert_no_errno(ret
);
76 ret
= listen(sock
, 1);
77 g_assert_no_errno(ret
);
82 static int socket_accept(int sock
)
84 struct sockaddr_un addr
;
87 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
90 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
94 addrlen
= sizeof(addr
);
95 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
96 } while (ret
== -1 && errno
== EINTR
);
98 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
105 static void kill_qemu(QTestState
*s
)
107 if (s
->qemu_pid
!= -1) {
108 kill(s
->qemu_pid
, SIGTERM
);
109 waitpid(s
->qemu_pid
, NULL
, 0);
113 static void sigabrt_handler(int signo
)
116 for (elem
= qtest_instances
; elem
; elem
= elem
->next
) {
117 kill_qemu(elem
->data
);
121 static void setup_sigabrt_handler(void)
123 struct sigaction sigact
;
125 /* Catch SIGABRT to clean up on g_assert() failure */
126 sigact
= (struct sigaction
){
127 .sa_handler
= sigabrt_handler
,
128 .sa_flags
= SA_RESETHAND
,
130 sigemptyset(&sigact
.sa_mask
);
131 sigaction(SIGABRT
, &sigact
, &sigact_old
);
134 static void cleanup_sigabrt_handler(void)
136 sigaction(SIGABRT
, &sigact_old
, NULL
);
139 QTestState
*qtest_init(const char *extra_args
)
142 int sock
, qmpsock
, i
;
144 gchar
*qmp_socket_path
;
146 const char *qemu_binary
;
148 qemu_binary
= getenv("QTEST_QEMU_BINARY");
149 g_assert(qemu_binary
!= NULL
);
151 s
= g_malloc(sizeof(*s
));
153 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
154 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
156 sock
= init_socket(socket_path
);
157 qmpsock
= init_socket(qmp_socket_path
);
159 /* Only install SIGABRT handler once */
160 if (!qtest_instances
) {
161 setup_sigabrt_handler();
164 qtest_instances
= g_list_prepend(qtest_instances
, s
);
166 s
->qemu_pid
= fork();
167 if (s
->qemu_pid
== 0) {
168 command
= g_strdup_printf("exec %s "
169 "-qtest unix:%s,nowait "
171 "-qmp unix:%s,nowait "
172 "-machine accel=qtest "
174 "%s", qemu_binary
, socket_path
,
175 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
178 execlp("/bin/sh", "sh", "-c", command
, NULL
);
182 s
->fd
= socket_accept(sock
);
184 s
->qmp_fd
= socket_accept(qmpsock
);
187 unlink(qmp_socket_path
);
189 g_free(qmp_socket_path
);
191 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
193 s
->rx
= g_string_new("");
194 for (i
= 0; i
< MAX_IRQ
; i
++) {
195 s
->irq_level
[i
] = false;
198 /* Read the QMP greeting and then do the handshake */
199 qtest_qmp_discard_response(s
, "");
200 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
202 if (getenv("QTEST_STOP")) {
203 kill(s
->qemu_pid
, SIGSTOP
);
209 void qtest_quit(QTestState
*s
)
211 /* Uninstall SIGABRT handler on last instance */
212 if (qtest_instances
&& !qtest_instances
->next
) {
213 cleanup_sigabrt_handler();
216 qtest_instances
= g_list_remove(qtest_instances
, s
);
221 g_string_free(s
->rx
, true);
225 static void socket_send(int fd
, const char *buf
, size_t size
)
230 while (offset
< size
) {
233 len
= write(fd
, buf
+ offset
, size
- offset
);
234 if (len
== -1 && errno
== EINTR
) {
238 g_assert_no_errno(len
);
239 g_assert_cmpint(len
, >, 0);
245 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
247 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
248 size_t size
= strlen(str
);
250 socket_send(fd
, str
, size
);
254 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
259 socket_sendf(s
->fd
, fmt
, ap
);
263 static GString
*qtest_recv_line(QTestState
*s
)
269 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
273 len
= read(s
->fd
, buffer
, sizeof(buffer
));
274 if (len
== -1 && errno
== EINTR
) {
278 if (len
== -1 || len
== 0) {
279 fprintf(stderr
, "Broken pipe\n");
283 g_string_append_len(s
->rx
, buffer
, len
);
286 offset
= eol
- s
->rx
->str
;
287 line
= g_string_new_len(s
->rx
->str
, offset
);
288 g_string_erase(s
->rx
, 0, offset
+ 1);
293 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
300 line
= qtest_recv_line(s
);
301 words
= g_strsplit(line
->str
, " ", 0);
302 g_string_free(line
, TRUE
);
304 if (strcmp(words
[0], "IRQ") == 0) {
307 g_assert(words
[1] != NULL
);
308 g_assert(words
[2] != NULL
);
310 irq
= strtoul(words
[2], NULL
, 0);
311 g_assert_cmpint(irq
, >=, 0);
312 g_assert_cmpint(irq
, <, MAX_IRQ
);
314 if (strcmp(words
[1], "raise") == 0) {
315 s
->irq_level
[irq
] = true;
317 s
->irq_level
[irq
] = false;
324 g_assert(words
[0] != NULL
);
325 g_assert_cmpstr(words
[0], ==, "OK");
328 for (i
= 0; i
< expected_args
; i
++) {
329 g_assert(words
[i
] != NULL
);
339 JSONMessageParser parser
;
343 static void qmp_response(JSONMessageParser
*parser
, QList
*tokens
)
345 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
348 obj
= json_parser_parse(tokens
, NULL
);
350 fprintf(stderr
, "QMP JSON response parsing failed\n");
354 g_assert(qobject_type(obj
) == QTYPE_QDICT
);
355 g_assert(!qmp
->response
);
356 qmp
->response
= (QDict
*)obj
;
359 QDict
*qtest_qmp_receive(QTestState
*s
)
361 QMPResponseParser qmp
;
362 bool log
= getenv("QTEST_LOG") != NULL
;
365 json_message_parser_init(&qmp
.parser
, qmp_response
);
366 while (!qmp
.response
) {
370 len
= read(s
->qmp_fd
, &c
, 1);
371 if (len
== -1 && errno
== EINTR
) {
375 if (len
== -1 || len
== 0) {
376 fprintf(stderr
, "Broken pipe\n");
381 len
= write(2, &c
, 1);
383 json_message_parser_feed(&qmp
.parser
, &c
, 1);
385 json_message_parser_destroy(&qmp
.parser
);
390 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
395 /* Going through qobject ensures we escape strings properly.
396 * This seemingly unnecessary copy is required in case va_list
399 va_copy(ap_copy
, ap
);
400 qobj
= qobject_from_jsonv(fmt
, &ap_copy
);
403 /* No need to send anything for an empty QObject. */
405 QString
*qstr
= qobject_to_json(qobj
);
406 const char *str
= qstring_get_str(qstr
);
407 size_t size
= qstring_get_length(qstr
);
409 /* Send QMP request */
410 socket_send(s
->qmp_fd
, str
, size
);
413 qobject_decref(qobj
);
417 return qtest_qmp_receive(s
);
420 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
426 response
= qtest_qmpv(s
, fmt
, ap
);
431 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
433 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
437 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
443 response
= qtest_qmpv(s
, fmt
, ap
);
448 const char *qtest_get_arch(void)
450 const char *qemu
= getenv("QTEST_QEMU_BINARY");
451 const char *end
= strrchr(qemu
, '/');
453 return end
+ strlen("/qemu-system-");
456 bool qtest_get_irq(QTestState
*s
, int num
)
458 /* dummy operation in order to make sure irq is up to date */
461 return s
->irq_level
[num
];
464 static int64_t qtest_clock_rsp(QTestState
*s
)
468 words
= qtest_rsp(s
, 2);
469 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
474 int64_t qtest_clock_step_next(QTestState
*s
)
476 qtest_sendf(s
, "clock_step\n");
477 return qtest_clock_rsp(s
);
480 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
482 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
483 return qtest_clock_rsp(s
);
486 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
488 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
489 return qtest_clock_rsp(s
);
492 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
494 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
498 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
500 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
504 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
506 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
510 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
512 qtest_out(s
, "outb", addr
, value
);
515 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
517 qtest_out(s
, "outw", addr
, value
);
520 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
522 qtest_out(s
, "outl", addr
, value
);
525 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
530 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
531 args
= qtest_rsp(s
, 2);
532 value
= strtoul(args
[1], NULL
, 0);
538 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
540 return qtest_in(s
, "inb", addr
);
543 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
545 return qtest_in(s
, "inw", addr
);
548 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
550 return qtest_in(s
, "inl", addr
);
553 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
556 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
560 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
562 qtest_write(s
, "writeb", addr
, value
);
565 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
567 qtest_write(s
, "writew", addr
, value
);
570 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
572 qtest_write(s
, "writel", addr
, value
);
575 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
577 qtest_write(s
, "writeq", addr
, value
);
580 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
585 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
586 args
= qtest_rsp(s
, 2);
587 value
= strtoull(args
[1], NULL
, 0);
593 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
595 return qtest_read(s
, "readb", addr
);
598 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
600 return qtest_read(s
, "readw", addr
);
603 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
605 return qtest_read(s
, "readl", addr
);
608 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
610 return qtest_read(s
, "readq", addr
);
613 static int hex2nib(char ch
)
615 if (ch
>= '0' && ch
<= '9') {
617 } else if (ch
>= 'a' && ch
<= 'f') {
618 return 10 + (ch
- 'a');
619 } else if (ch
>= 'A' && ch
<= 'F') {
620 return 10 + (ch
- 'a');
626 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
632 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
633 args
= qtest_rsp(s
, 2);
635 for (i
= 0; i
< size
; i
++) {
636 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
637 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
643 void qtest_add_func(const char *str
, void (*fn
))
645 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
646 g_test_add_func(path
, fn
);
650 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
652 const uint8_t *ptr
= data
;
655 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x", addr
, size
);
656 for (i
= 0; i
< size
; i
++) {
657 qtest_sendf(s
, "%02x", ptr
[i
]);
659 qtest_sendf(s
, "\n");
663 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
667 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x", addr
, size
);
668 for (i
= 0; i
< size
; i
++) {
669 qtest_sendf(s
, "%02x", pattern
);
671 qtest_sendf(s
, "\n");
675 QDict
*qmp(const char *fmt
, ...)
681 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
686 void qmp_discard_response(const char *fmt
, ...)
691 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);