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-streamer.h"
34 #include "qapi/qmp/json-parser.h"
37 #define SOCKET_TIMEOUT 5
39 QTestState
*global_qtest
;
45 bool irq_level
[MAX_IRQ
];
47 pid_t qemu_pid
; /* our child QEMU process */
48 struct sigaction sigact_old
; /* restored on exit */
51 static GList
*qtest_instances
;
52 static struct sigaction sigact_old
;
54 #define g_assert_no_errno(ret) do { \
55 g_assert_cmpint(ret, !=, -1); \
58 static int init_socket(const char *socket_path
)
60 struct sockaddr_un addr
;
64 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
65 g_assert_no_errno(sock
);
67 addr
.sun_family
= AF_UNIX
;
68 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
69 qemu_set_cloexec(sock
);
72 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
73 } while (ret
== -1 && errno
== EINTR
);
74 g_assert_no_errno(ret
);
80 static int socket_accept(int sock
)
82 struct sockaddr_un addr
;
85 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
88 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
91 addrlen
= sizeof(addr
);
93 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
94 } while (ret
== -1 && errno
== EINTR
);
100 static void kill_qemu(QTestState
*s
)
102 if (s
->qemu_pid
!= -1) {
103 kill(s
->qemu_pid
, SIGTERM
);
104 waitpid(s
->qemu_pid
, NULL
, 0);
108 static void sigabrt_handler(int signo
)
111 for (elem
= qtest_instances
; elem
; elem
= elem
->next
) {
112 kill_qemu(elem
->data
);
116 static void setup_sigabrt_handler(void)
118 struct sigaction sigact
;
120 /* Catch SIGABRT to clean up on g_assert() failure */
121 sigact
= (struct sigaction
){
122 .sa_handler
= sigabrt_handler
,
123 .sa_flags
= SA_RESETHAND
,
125 sigemptyset(&sigact
.sa_mask
);
126 sigaction(SIGABRT
, &sigact
, &sigact_old
);
129 static void cleanup_sigabrt_handler(void)
131 sigaction(SIGABRT
, &sigact_old
, NULL
);
134 QTestState
*qtest_init(const char *extra_args
)
137 int sock
, qmpsock
, i
;
139 gchar
*qmp_socket_path
;
141 const char *qemu_binary
;
143 qemu_binary
= getenv("QTEST_QEMU_BINARY");
144 g_assert(qemu_binary
!= NULL
);
146 s
= g_malloc(sizeof(*s
));
148 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
149 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
151 sock
= init_socket(socket_path
);
152 qmpsock
= init_socket(qmp_socket_path
);
154 /* Only install SIGABRT handler once */
155 if (!qtest_instances
) {
156 setup_sigabrt_handler();
159 qtest_instances
= g_list_prepend(qtest_instances
, s
);
161 s
->qemu_pid
= fork();
162 if (s
->qemu_pid
== 0) {
163 command
= g_strdup_printf("exec %s "
164 "-qtest unix:%s,nowait "
165 "-qtest-log /dev/null "
166 "-qmp unix:%s,nowait "
167 "-machine accel=qtest "
169 "%s", qemu_binary
, socket_path
,
172 execlp("/bin/sh", "sh", "-c", command
, NULL
);
176 s
->fd
= socket_accept(sock
);
178 s
->qmp_fd
= socket_accept(qmpsock
);
181 unlink(qmp_socket_path
);
183 g_free(qmp_socket_path
);
185 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
187 s
->rx
= g_string_new("");
188 for (i
= 0; i
< MAX_IRQ
; i
++) {
189 s
->irq_level
[i
] = false;
192 /* Read the QMP greeting and then do the handshake */
193 qtest_qmp_discard_response(s
, "");
194 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
196 if (getenv("QTEST_STOP")) {
197 kill(s
->qemu_pid
, SIGSTOP
);
203 void qtest_quit(QTestState
*s
)
205 /* Uninstall SIGABRT handler on last instance */
206 if (qtest_instances
&& !qtest_instances
->next
) {
207 cleanup_sigabrt_handler();
210 qtest_instances
= g_list_remove(qtest_instances
, s
);
215 g_string_free(s
->rx
, true);
219 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
224 str
= g_strdup_vprintf(fmt
, ap
);
228 while (offset
< size
) {
231 len
= write(fd
, str
+ offset
, size
- offset
);
232 if (len
== -1 && errno
== EINTR
) {
236 g_assert_no_errno(len
);
237 g_assert_cmpint(len
, >, 0);
243 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
248 socket_sendf(s
->fd
, fmt
, ap
);
252 static GString
*qtest_recv_line(QTestState
*s
)
258 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
262 len
= read(s
->fd
, buffer
, sizeof(buffer
));
263 if (len
== -1 && errno
== EINTR
) {
267 if (len
== -1 || len
== 0) {
268 fprintf(stderr
, "Broken pipe\n");
272 g_string_append_len(s
->rx
, buffer
, len
);
275 offset
= eol
- s
->rx
->str
;
276 line
= g_string_new_len(s
->rx
->str
, offset
);
277 g_string_erase(s
->rx
, 0, offset
+ 1);
282 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
289 line
= qtest_recv_line(s
);
290 words
= g_strsplit(line
->str
, " ", 0);
291 g_string_free(line
, TRUE
);
293 if (strcmp(words
[0], "IRQ") == 0) {
296 g_assert(words
[1] != NULL
);
297 g_assert(words
[2] != NULL
);
299 irq
= strtoul(words
[2], NULL
, 0);
300 g_assert_cmpint(irq
, >=, 0);
301 g_assert_cmpint(irq
, <, MAX_IRQ
);
303 if (strcmp(words
[1], "raise") == 0) {
304 s
->irq_level
[irq
] = true;
306 s
->irq_level
[irq
] = false;
313 g_assert(words
[0] != NULL
);
314 g_assert_cmpstr(words
[0], ==, "OK");
317 for (i
= 0; i
< expected_args
; i
++) {
318 g_assert(words
[i
] != NULL
);
328 JSONMessageParser parser
;
332 static void qmp_response(JSONMessageParser
*parser
, QList
*tokens
)
334 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
337 obj
= json_parser_parse(tokens
, NULL
);
339 fprintf(stderr
, "QMP JSON response parsing failed\n");
343 g_assert(qobject_type(obj
) == QTYPE_QDICT
);
344 g_assert(!qmp
->response
);
345 qmp
->response
= (QDict
*)obj
;
348 QDict
*qtest_qmp_receive(QTestState
*s
)
350 QMPResponseParser qmp
;
353 json_message_parser_init(&qmp
.parser
, qmp_response
);
354 while (!qmp
.response
) {
358 len
= read(s
->qmp_fd
, &c
, 1);
359 if (len
== -1 && errno
== EINTR
) {
363 if (len
== -1 || len
== 0) {
364 fprintf(stderr
, "Broken pipe\n");
368 json_message_parser_feed(&qmp
.parser
, &c
, 1);
370 json_message_parser_destroy(&qmp
.parser
);
375 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
377 /* Send QMP request */
378 socket_sendf(s
->qmp_fd
, fmt
, ap
);
381 return qtest_qmp_receive(s
);
384 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
390 response
= qtest_qmpv(s
, fmt
, ap
);
395 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
397 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
401 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
407 response
= qtest_qmpv(s
, fmt
, ap
);
412 const char *qtest_get_arch(void)
414 const char *qemu
= getenv("QTEST_QEMU_BINARY");
415 const char *end
= strrchr(qemu
, '/');
417 return end
+ strlen("/qemu-system-");
420 bool qtest_get_irq(QTestState
*s
, int num
)
422 /* dummy operation in order to make sure irq is up to date */
425 return s
->irq_level
[num
];
428 static int64_t qtest_clock_rsp(QTestState
*s
)
432 words
= qtest_rsp(s
, 2);
433 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
438 int64_t qtest_clock_step_next(QTestState
*s
)
440 qtest_sendf(s
, "clock_step\n");
441 return qtest_clock_rsp(s
);
444 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
446 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
447 return qtest_clock_rsp(s
);
450 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
452 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
453 return qtest_clock_rsp(s
);
456 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
458 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
462 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
464 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
468 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
470 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
474 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
476 qtest_out(s
, "outb", addr
, value
);
479 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
481 qtest_out(s
, "outw", addr
, value
);
484 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
486 qtest_out(s
, "outl", addr
, value
);
489 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
494 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
495 args
= qtest_rsp(s
, 2);
496 value
= strtoul(args
[1], NULL
, 0);
502 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
504 return qtest_in(s
, "inb", addr
);
507 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
509 return qtest_in(s
, "inw", addr
);
512 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
514 return qtest_in(s
, "inl", addr
);
517 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
520 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
524 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
526 qtest_write(s
, "writeb", addr
, value
);
529 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
531 qtest_write(s
, "writew", addr
, value
);
534 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
536 qtest_write(s
, "writel", addr
, value
);
539 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
541 qtest_write(s
, "writeq", addr
, value
);
544 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
549 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
550 args
= qtest_rsp(s
, 2);
551 value
= strtoull(args
[1], NULL
, 0);
557 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
559 return qtest_read(s
, "readb", addr
);
562 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
564 return qtest_read(s
, "readw", addr
);
567 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
569 return qtest_read(s
, "readl", addr
);
572 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
574 return qtest_read(s
, "readq", addr
);
577 static int hex2nib(char ch
)
579 if (ch
>= '0' && ch
<= '9') {
581 } else if (ch
>= 'a' && ch
<= 'f') {
582 return 10 + (ch
- 'a');
583 } else if (ch
>= 'A' && ch
<= 'F') {
584 return 10 + (ch
- 'a');
590 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
596 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
597 args
= qtest_rsp(s
, 2);
599 for (i
= 0; i
< size
; i
++) {
600 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
601 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
607 void qtest_add_func(const char *str
, void (*fn
))
609 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
610 g_test_add_func(path
, fn
);
613 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
615 const uint8_t *ptr
= data
;
618 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x", addr
, size
);
619 for (i
= 0; i
< size
; i
++) {
620 qtest_sendf(s
, "%02x", ptr
[i
]);
622 qtest_sendf(s
, "\n");
626 QDict
*qmp(const char *fmt
, ...)
632 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
637 void qmp_discard_response(const char *fmt
, ...)
642 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);