4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
18 #include <sys/types.h>
19 #include <sys/socket.h>
34 QTestState
*global_qtest
;
40 bool irq_level
[MAX_IRQ
];
43 char *socket_path
, *qmp_socket_path
;
46 #define g_assert_no_errno(ret) do { \
47 g_assert_cmpint(ret, !=, -1); \
50 static int init_socket(const char *socket_path
)
52 struct sockaddr_un addr
;
56 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
57 g_assert_no_errno(sock
);
59 addr
.sun_family
= AF_UNIX
;
60 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
61 qemu_set_cloexec(sock
);
64 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
65 } while (ret
== -1 && errno
== EINTR
);
66 g_assert_no_errno(ret
);
72 static int socket_accept(int sock
)
74 struct sockaddr_un addr
;
78 addrlen
= sizeof(addr
);
80 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
81 } while (ret
== -1 && errno
== EINTR
);
82 g_assert_no_errno(ret
);
88 QTestState
*qtest_init(const char *extra_args
)
91 int sock
, qmpsock
, ret
, i
;
94 const char *qemu_binary
;
97 qemu_binary
= getenv("QTEST_QEMU_BINARY");
98 g_assert(qemu_binary
!= NULL
);
100 s
= g_malloc(sizeof(*s
));
102 s
->socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
103 s
->qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
104 pid_file
= g_strdup_printf("/tmp/qtest-%d.pid", getpid());
106 sock
= init_socket(s
->socket_path
);
107 qmpsock
= init_socket(s
->qmp_socket_path
);
111 command
= g_strdup_printf("%s "
112 "-qtest unix:%s,nowait "
113 "-qtest-log /dev/null "
114 "-qmp unix:%s,nowait "
116 "-machine accel=qtest "
117 "%s", qemu_binary
, s
->socket_path
,
118 s
->qmp_socket_path
, pid_file
,
121 ret
= system(command
);
126 s
->fd
= socket_accept(sock
);
127 s
->qmp_fd
= socket_accept(qmpsock
);
129 s
->rx
= g_string_new("");
130 s
->pid_file
= pid_file
;
131 for (i
= 0; i
< MAX_IRQ
; i
++) {
132 s
->irq_level
[i
] = false;
135 /* Read the QMP greeting and then do the handshake */
137 qtest_qmp(s
, "{ 'execute': 'qmp_capabilities' }");
142 void qtest_quit(QTestState
*s
)
147 f
= fopen(s
->pid_file
, "r");
149 if (fgets(buffer
, sizeof(buffer
), f
)) {
150 pid_t pid
= atoi(buffer
);
154 waitpid(pid
, &status
, 0);
161 unlink(s
->socket_path
);
162 unlink(s
->qmp_socket_path
);
164 g_free(s
->socket_path
);
165 g_free(s
->qmp_socket_path
);
168 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
173 str
= g_strdup_vprintf(fmt
, ap
);
177 while (offset
< size
) {
180 len
= write(fd
, str
+ offset
, size
- offset
);
181 if (len
== -1 && errno
== EINTR
) {
185 g_assert_no_errno(len
);
186 g_assert_cmpint(len
, >, 0);
192 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
197 socket_sendf(s
->fd
, fmt
, ap
);
201 static GString
*qtest_recv_line(QTestState
*s
)
207 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
211 len
= read(s
->fd
, buffer
, sizeof(buffer
));
212 if (len
== -1 && errno
== EINTR
) {
216 if (len
== -1 || len
== 0) {
217 fprintf(stderr
, "Broken pipe\n");
221 g_string_append_len(s
->rx
, buffer
, len
);
224 offset
= eol
- s
->rx
->str
;
225 line
= g_string_new_len(s
->rx
->str
, offset
);
226 g_string_erase(s
->rx
, 0, offset
+ 1);
231 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
238 line
= qtest_recv_line(s
);
239 words
= g_strsplit(line
->str
, " ", 0);
240 g_string_free(line
, TRUE
);
242 if (strcmp(words
[0], "IRQ") == 0) {
245 g_assert(words
[1] != NULL
);
246 g_assert(words
[2] != NULL
);
248 irq
= strtoul(words
[2], NULL
, 0);
249 g_assert_cmpint(irq
, >=, 0);
250 g_assert_cmpint(irq
, <, MAX_IRQ
);
252 if (strcmp(words
[1], "raise") == 0) {
253 s
->irq_level
[irq
] = true;
255 s
->irq_level
[irq
] = false;
262 g_assert(words
[0] != NULL
);
263 g_assert_cmpstr(words
[0], ==, "OK");
266 for (i
= 0; i
< expected_args
; i
++) {
267 g_assert(words
[i
] != NULL
);
276 void qtest_qmp(QTestState
*s
, const char *fmt
, ...)
279 bool has_reply
= false;
282 /* Send QMP request */
284 socket_sendf(s
->qmp_fd
, fmt
, ap
);
288 while (!has_reply
|| nesting
> 0) {
292 len
= read(s
->qmp_fd
, &c
, 1);
293 if (len
== -1 && errno
== EINTR
) {
297 if (len
== -1 || len
== 0) {
298 fprintf(stderr
, "Broken pipe\n");
314 const char *qtest_get_arch(void)
316 const char *qemu
= getenv("QTEST_QEMU_BINARY");
317 const char *end
= strrchr(qemu
, '/');
319 return end
+ strlen("/qemu-system-");
322 bool qtest_get_irq(QTestState
*s
, int num
)
324 /* dummy operation in order to make sure irq is up to date */
327 return s
->irq_level
[num
];
330 static int64_t qtest_clock_rsp(QTestState
*s
)
334 words
= qtest_rsp(s
, 2);
335 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
340 int64_t qtest_clock_step_next(QTestState
*s
)
342 qtest_sendf(s
, "clock_step\n");
343 return qtest_clock_rsp(s
);
346 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
348 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
349 return qtest_clock_rsp(s
);
352 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
354 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
355 return qtest_clock_rsp(s
);
358 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
360 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
364 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
366 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
370 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
372 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
376 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
378 qtest_out(s
, "outb", addr
, value
);
381 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
383 qtest_out(s
, "outw", addr
, value
);
386 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
388 qtest_out(s
, "outl", addr
, value
);
391 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
396 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
397 args
= qtest_rsp(s
, 2);
398 value
= strtoul(args
[1], NULL
, 0);
404 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
406 return qtest_in(s
, "inb", addr
);
409 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
411 return qtest_in(s
, "inw", addr
);
414 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
416 return qtest_in(s
, "inl", addr
);
419 static int hex2nib(char ch
)
421 if (ch
>= '0' && ch
<= '9') {
423 } else if (ch
>= 'a' && ch
<= 'f') {
424 return 10 + (ch
- 'a');
425 } else if (ch
>= 'A' && ch
<= 'F') {
426 return 10 + (ch
- 'a');
432 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
438 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
439 args
= qtest_rsp(s
, 2);
441 for (i
= 0; i
< size
; i
++) {
442 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
443 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
449 void qtest_add_func(const char *str
, void (*fn
))
451 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
452 g_test_add_func(path
, fn
);
455 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
457 const uint8_t *ptr
= data
;
460 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x", addr
, size
);
461 for (i
= 0; i
< size
; i
++) {
462 qtest_sendf(s
, "%02x", ptr
[i
]);
464 qtest_sendf(s
, "\n");