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>
33 QTestState
*global_qtest
;
38 bool irq_level
[MAX_IRQ
];
43 #define g_assert_no_errno(ret) do { \
44 g_assert_cmpint(ret, !=, -1); \
47 QTestState
*qtest_init(const char *extra_args
)
50 struct sockaddr_un addr
;
55 const char *qemu_binary
;
59 qemu_binary
= getenv("QTEST_QEMU_BINARY");
60 g_assert(qemu_binary
!= NULL
);
62 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
63 pid_file
= g_strdup_printf("/tmp/qtest-%d.pid", getpid());
65 s
= g_malloc(sizeof(*s
));
67 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
68 g_assert_no_errno(sock
);
70 addr
.sun_family
= AF_UNIX
;
71 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
72 qemu_set_cloexec(sock
);
75 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
76 } while (ret
== -1 && errno
== EINTR
);
77 g_assert_no_errno(ret
);
82 command
= g_strdup_printf("%s "
83 "-qtest unix:%s,nowait "
84 "-qtest-log /dev/null "
86 "-machine accel=qtest "
87 "%s", qemu_binary
, socket_path
,
91 ret
= system(command
);
97 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
98 } while (ret
== -1 && errno
== EINTR
);
99 g_assert_no_errno(ret
);
103 s
->rx
= g_string_new("");
104 s
->pid_file
= pid_file
;
105 for (i
= 0; i
< MAX_IRQ
; i
++) {
106 s
->irq_level
[i
] = false;
114 void qtest_quit(QTestState
*s
)
119 f
= fopen(s
->pid_file
, "r");
121 if (fgets(buffer
, sizeof(buffer
), f
)) {
122 pid_t pid
= atoi(buffer
);
126 waitpid(pid
, &status
, 0);
133 static void qtest_sendf(QTestState
*s
, const char *fmt
, ...)
140 str
= g_strdup_vprintf(fmt
, ap
);
145 while (offset
< size
) {
148 len
= write(s
->fd
, str
+ offset
, size
- offset
);
149 if (len
== -1 && errno
== EINTR
) {
153 g_assert_no_errno(len
);
154 g_assert_cmpint(len
, >, 0);
160 static GString
*qtest_recv_line(QTestState
*s
)
166 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
170 len
= read(s
->fd
, buffer
, sizeof(buffer
));
171 if (len
== -1 && errno
== EINTR
) {
175 if (len
== -1 || len
== 0) {
176 fprintf(stderr
, "Broken pipe\n");
180 g_string_append_len(s
->rx
, buffer
, len
);
183 offset
= eol
- s
->rx
->str
;
184 line
= g_string_new_len(s
->rx
->str
, offset
);
185 g_string_erase(s
->rx
, 0, offset
+ 1);
190 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
197 line
= qtest_recv_line(s
);
198 words
= g_strsplit(line
->str
, " ", 0);
199 g_string_free(line
, TRUE
);
201 if (strcmp(words
[0], "IRQ") == 0) {
204 g_assert(words
[1] != NULL
);
205 g_assert(words
[2] != NULL
);
207 irq
= strtoul(words
[2], NULL
, 0);
208 g_assert_cmpint(irq
, >=, 0);
209 g_assert_cmpint(irq
, <, MAX_IRQ
);
211 if (strcmp(words
[1], "raise") == 0) {
212 s
->irq_level
[irq
] = true;
214 s
->irq_level
[irq
] = false;
221 g_assert(words
[0] != NULL
);
222 g_assert_cmpstr(words
[0], ==, "OK");
225 for (i
= 0; i
< expected_args
; i
++) {
226 g_assert(words
[i
] != NULL
);
235 const char *qtest_get_arch(void)
237 const char *qemu
= getenv("QTEST_QEMU_BINARY");
238 const char *end
= strrchr(qemu
, '/');
240 return end
+ strlen("/qemu-system-");
243 bool qtest_get_irq(QTestState
*s
, int num
)
245 /* dummy operation in order to make sure irq is up to date */
248 return s
->irq_level
[num
];
251 static int64_t qtest_clock_rsp(QTestState
*s
)
255 words
= qtest_rsp(s
, 2);
256 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
261 int64_t qtest_clock_step_next(QTestState
*s
)
263 qtest_sendf(s
, "clock_step\n");
264 return qtest_clock_rsp(s
);
267 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
269 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
270 return qtest_clock_rsp(s
);
273 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
275 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
276 return qtest_clock_rsp(s
);
279 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
281 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
285 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
287 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
291 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
293 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
297 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
299 qtest_out(s
, "outb", addr
, value
);
302 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
304 qtest_out(s
, "outw", addr
, value
);
307 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
309 qtest_out(s
, "outl", addr
, value
);
312 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
317 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
318 args
= qtest_rsp(s
, 2);
319 value
= strtoul(args
[1], NULL
, 0);
325 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
327 return qtest_in(s
, "inb", addr
);
330 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
332 return qtest_in(s
, "inw", addr
);
335 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
337 return qtest_in(s
, "inl", addr
);
340 static int hex2nib(char ch
)
342 if (ch
>= '0' && ch
<= '9') {
344 } else if (ch
>= 'a' && ch
<= 'f') {
345 return 10 + (ch
- 'a');
346 } else if (ch
>= 'A' && ch
<= 'F') {
347 return 10 + (ch
- 'a');
353 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
359 qtest_sendf(s
, "read 0x%x 0x%x\n", addr
, size
);
360 args
= qtest_rsp(s
, 2);
362 for (i
= 0; i
< size
; i
++) {
363 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
364 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
370 void qtest_add_func(const char *str
, void (*fn
))
372 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
373 g_test_add_func(path
, fn
);
376 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
378 const uint8_t *ptr
= data
;
381 qtest_sendf(s
, "write 0x%x 0x%x 0x", addr
, size
);
382 for (i
= 0; i
< size
; i
++) {
383 qtest_sendf(s
, "%02x", ptr
[i
]);
385 qtest_sendf(s
, "\n");