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"
38 QTestState
*global_qtest
;
44 bool irq_level
[MAX_IRQ
];
46 int child_pid
; /* Child process created to execute QEMU */
47 pid_t qemu_pid
; /* QEMU process spawned by our child */
50 #define g_assert_no_errno(ret) do { \
51 g_assert_cmpint(ret, !=, -1); \
54 static int init_socket(const char *socket_path
)
56 struct sockaddr_un addr
;
60 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
61 g_assert_no_errno(sock
);
63 addr
.sun_family
= AF_UNIX
;
64 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
65 qemu_set_cloexec(sock
);
68 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
69 } while (ret
== -1 && errno
== EINTR
);
70 g_assert_no_errno(ret
);
76 static int socket_accept(int sock
)
78 struct sockaddr_un addr
;
82 addrlen
= sizeof(addr
);
84 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
85 } while (ret
== -1 && errno
== EINTR
);
86 g_assert_no_errno(ret
);
92 static pid_t
read_pid_file(const char *pid_file
)
98 f
= fopen(pid_file
, "r");
100 if (fgets(buffer
, sizeof(buffer
), f
)) {
108 QTestState
*qtest_init(const char *extra_args
)
111 int sock
, qmpsock
, i
;
113 gchar
*qmp_socket_path
;
116 const char *qemu_binary
;
119 qemu_binary
= getenv("QTEST_QEMU_BINARY");
120 g_assert(qemu_binary
!= NULL
);
122 s
= g_malloc(sizeof(*s
));
124 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
125 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
126 pid_file
= g_strdup_printf("/tmp/qtest-%d.pid", getpid());
128 sock
= init_socket(socket_path
);
129 qmpsock
= init_socket(qmp_socket_path
);
133 command
= g_strdup_printf("%s "
134 "-qtest unix:%s,nowait "
135 "-qtest-log /dev/null "
136 "-qmp unix:%s,nowait "
138 "-machine accel=qtest "
140 "%s", qemu_binary
, socket_path
,
141 qmp_socket_path
, pid_file
,
143 execlp("/bin/sh", "sh", "-c", command
, NULL
);
147 s
->fd
= socket_accept(sock
);
148 s
->qmp_fd
= socket_accept(qmpsock
);
150 unlink(qmp_socket_path
);
152 g_free(qmp_socket_path
);
154 s
->rx
= g_string_new("");
156 for (i
= 0; i
< MAX_IRQ
; i
++) {
157 s
->irq_level
[i
] = false;
160 /* Read the QMP greeting and then do the handshake */
161 qtest_qmp_discard_response(s
, "");
162 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
164 s
->qemu_pid
= read_pid_file(pid_file
);
168 if (getenv("QTEST_STOP")) {
169 kill(s
->qemu_pid
, SIGSTOP
);
175 void qtest_quit(QTestState
*s
)
179 if (s
->qemu_pid
!= -1) {
180 kill(s
->qemu_pid
, SIGTERM
);
181 waitpid(s
->qemu_pid
, &status
, 0);
186 g_string_free(s
->rx
, true);
190 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
195 str
= g_strdup_vprintf(fmt
, ap
);
199 while (offset
< size
) {
202 len
= write(fd
, str
+ offset
, size
- offset
);
203 if (len
== -1 && errno
== EINTR
) {
207 g_assert_no_errno(len
);
208 g_assert_cmpint(len
, >, 0);
214 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
219 socket_sendf(s
->fd
, fmt
, ap
);
223 static GString
*qtest_recv_line(QTestState
*s
)
229 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
233 len
= read(s
->fd
, buffer
, sizeof(buffer
));
234 if (len
== -1 && errno
== EINTR
) {
238 if (len
== -1 || len
== 0) {
239 fprintf(stderr
, "Broken pipe\n");
243 g_string_append_len(s
->rx
, buffer
, len
);
246 offset
= eol
- s
->rx
->str
;
247 line
= g_string_new_len(s
->rx
->str
, offset
);
248 g_string_erase(s
->rx
, 0, offset
+ 1);
253 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
260 line
= qtest_recv_line(s
);
261 words
= g_strsplit(line
->str
, " ", 0);
262 g_string_free(line
, TRUE
);
264 if (strcmp(words
[0], "IRQ") == 0) {
267 g_assert(words
[1] != NULL
);
268 g_assert(words
[2] != NULL
);
270 irq
= strtoul(words
[2], NULL
, 0);
271 g_assert_cmpint(irq
, >=, 0);
272 g_assert_cmpint(irq
, <, MAX_IRQ
);
274 if (strcmp(words
[1], "raise") == 0) {
275 s
->irq_level
[irq
] = true;
277 s
->irq_level
[irq
] = false;
284 g_assert(words
[0] != NULL
);
285 g_assert_cmpstr(words
[0], ==, "OK");
288 for (i
= 0; i
< expected_args
; i
++) {
289 g_assert(words
[i
] != NULL
);
299 JSONMessageParser parser
;
303 static void qmp_response(JSONMessageParser
*parser
, QList
*tokens
)
305 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
308 obj
= json_parser_parse(tokens
, NULL
);
310 fprintf(stderr
, "QMP JSON response parsing failed\n");
314 g_assert(qobject_type(obj
) == QTYPE_QDICT
);
315 g_assert(!qmp
->response
);
316 qmp
->response
= (QDict
*)obj
;
319 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
321 QMPResponseParser qmp
;
323 /* Send QMP request */
324 socket_sendf(s
->qmp_fd
, fmt
, ap
);
328 json_message_parser_init(&qmp
.parser
, qmp_response
);
329 while (!qmp
.response
) {
333 len
= read(s
->qmp_fd
, &c
, 1);
334 if (len
== -1 && errno
== EINTR
) {
338 if (len
== -1 || len
== 0) {
339 fprintf(stderr
, "Broken pipe\n");
343 json_message_parser_feed(&qmp
.parser
, &c
, 1);
345 json_message_parser_destroy(&qmp
.parser
);
350 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
356 response
= qtest_qmpv(s
, fmt
, ap
);
361 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
363 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
367 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
373 response
= qtest_qmpv(s
, fmt
, ap
);
378 const char *qtest_get_arch(void)
380 const char *qemu
= getenv("QTEST_QEMU_BINARY");
381 const char *end
= strrchr(qemu
, '/');
383 return end
+ strlen("/qemu-system-");
386 bool qtest_get_irq(QTestState
*s
, int num
)
388 /* dummy operation in order to make sure irq is up to date */
391 return s
->irq_level
[num
];
394 static int64_t qtest_clock_rsp(QTestState
*s
)
398 words
= qtest_rsp(s
, 2);
399 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
404 int64_t qtest_clock_step_next(QTestState
*s
)
406 qtest_sendf(s
, "clock_step\n");
407 return qtest_clock_rsp(s
);
410 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
412 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
413 return qtest_clock_rsp(s
);
416 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
418 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
419 return qtest_clock_rsp(s
);
422 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
424 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
428 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
430 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
434 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
436 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
440 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
442 qtest_out(s
, "outb", addr
, value
);
445 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
447 qtest_out(s
, "outw", addr
, value
);
450 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
452 qtest_out(s
, "outl", addr
, value
);
455 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
460 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
461 args
= qtest_rsp(s
, 2);
462 value
= strtoul(args
[1], NULL
, 0);
468 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
470 return qtest_in(s
, "inb", addr
);
473 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
475 return qtest_in(s
, "inw", addr
);
478 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
480 return qtest_in(s
, "inl", addr
);
483 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
486 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
490 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
492 qtest_write(s
, "writeb", addr
, value
);
495 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
497 qtest_write(s
, "writew", addr
, value
);
500 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
502 qtest_write(s
, "writel", addr
, value
);
505 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
507 qtest_write(s
, "writeq", addr
, value
);
510 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
515 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
516 args
= qtest_rsp(s
, 2);
517 value
= strtoull(args
[1], NULL
, 0);
523 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
525 return qtest_read(s
, "readb", addr
);
528 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
530 return qtest_read(s
, "readw", addr
);
533 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
535 return qtest_read(s
, "readl", addr
);
538 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
540 return qtest_read(s
, "readq", addr
);
543 static int hex2nib(char ch
)
545 if (ch
>= '0' && ch
<= '9') {
547 } else if (ch
>= 'a' && ch
<= 'f') {
548 return 10 + (ch
- 'a');
549 } else if (ch
>= 'A' && ch
<= 'F') {
550 return 10 + (ch
- 'a');
556 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
562 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
563 args
= qtest_rsp(s
, 2);
565 for (i
= 0; i
< size
; i
++) {
566 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
567 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
573 void qtest_add_func(const char *str
, void (*fn
))
575 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
576 g_test_add_func(path
, fn
);
579 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
581 const uint8_t *ptr
= data
;
584 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x", addr
, size
);
585 for (i
= 0; i
< size
; i
++) {
586 qtest_sendf(s
, "%02x", ptr
[i
]);
588 qtest_sendf(s
, "\n");