]>
Commit | Line | Data |
---|---|---|
49ee3590 AL |
1 | /* |
2 | * QTest | |
3 | * | |
4 | * Copyright IBM, Corp. 2012 | |
5 | * Copyright Red Hat, Inc. 2012 | |
872536bf | 6 | * Copyright SUSE LINUX Products GmbH 2013 |
49ee3590 AL |
7 | * |
8 | * Authors: | |
9 | * Anthony Liguori <aliguori@us.ibm.com> | |
10 | * Paolo Bonzini <pbonzini@redhat.com> | |
872536bf | 11 | * Andreas Färber <afaerber@suse.de> |
49ee3590 AL |
12 | * |
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. | |
15 | * | |
16 | */ | |
17 | #include "libqtest.h" | |
18 | ||
19 | #include <glib.h> | |
20 | #include <sys/types.h> | |
21 | #include <sys/socket.h> | |
22 | #include <sys/wait.h> | |
23 | #include <sys/un.h> | |
24 | #include <inttypes.h> | |
25 | #include <errno.h> | |
26 | #include <stdio.h> | |
27 | #include <stdlib.h> | |
28 | #include <unistd.h> | |
29 | #include <string.h> | |
30 | ||
1de7afc9 PB |
31 | #include "qemu/compiler.h" |
32 | #include "qemu/osdep.h" | |
0c460dac SH |
33 | #include "qapi/qmp/json-streamer.h" |
34 | #include "qapi/qmp/json-parser.h" | |
49ee3590 AL |
35 | |
36 | #define MAX_IRQ 256 | |
37 | ||
38 | QTestState *global_qtest; | |
39 | ||
40 | struct QTestState | |
41 | { | |
42 | int fd; | |
a3ca163c | 43 | int qmp_fd; |
49ee3590 AL |
44 | bool irq_level[MAX_IRQ]; |
45 | GString *rx; | |
6bf00766 EH |
46 | gchar *pid_file; /* QEMU PID file */ |
47 | int child_pid; /* Child process created to execute QEMU */ | |
bb494a50 | 48 | char *socket_path, *qmp_socket_path; |
49ee3590 AL |
49 | }; |
50 | ||
51 | #define g_assert_no_errno(ret) do { \ | |
52 | g_assert_cmpint(ret, !=, -1); \ | |
53 | } while (0) | |
54 | ||
a3ca163c KW |
55 | static int init_socket(const char *socket_path) |
56 | { | |
57 | struct sockaddr_un addr; | |
58 | int sock; | |
59 | int ret; | |
60 | ||
61 | sock = socket(PF_UNIX, SOCK_STREAM, 0); | |
62 | g_assert_no_errno(sock); | |
63 | ||
64 | addr.sun_family = AF_UNIX; | |
65 | snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path); | |
66 | qemu_set_cloexec(sock); | |
67 | ||
68 | do { | |
69 | ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr)); | |
70 | } while (ret == -1 && errno == EINTR); | |
71 | g_assert_no_errno(ret); | |
72 | listen(sock, 1); | |
73 | ||
74 | return sock; | |
75 | } | |
76 | ||
77 | static int socket_accept(int sock) | |
78 | { | |
79 | struct sockaddr_un addr; | |
80 | socklen_t addrlen; | |
81 | int ret; | |
82 | ||
b34d2595 | 83 | addrlen = sizeof(addr); |
a3ca163c KW |
84 | do { |
85 | ret = accept(sock, (struct sockaddr *)&addr, &addrlen); | |
86 | } while (ret == -1 && errno == EINTR); | |
87 | g_assert_no_errno(ret); | |
88 | close(sock); | |
89 | ||
90 | return ret; | |
91 | } | |
92 | ||
e0fea6b1 PB |
93 | static pid_t qtest_qemu_pid(QTestState *s) |
94 | { | |
95 | FILE *f; | |
96 | char buffer[1024]; | |
97 | pid_t pid = -1; | |
98 | ||
99 | f = fopen(s->pid_file, "r"); | |
100 | if (f) { | |
101 | if (fgets(buffer, sizeof(buffer), f)) { | |
102 | pid = atoi(buffer); | |
103 | } | |
beb54a87 | 104 | fclose(f); |
e0fea6b1 | 105 | } |
e0fea6b1 PB |
106 | return pid; |
107 | } | |
108 | ||
49ee3590 AL |
109 | QTestState *qtest_init(const char *extra_args) |
110 | { | |
111 | QTestState *s; | |
8a8fd637 | 112 | int sock, qmpsock, i; |
49ee3590 AL |
113 | gchar *pid_file; |
114 | gchar *command; | |
115 | const char *qemu_binary; | |
116 | pid_t pid; | |
49ee3590 AL |
117 | |
118 | qemu_binary = getenv("QTEST_QEMU_BINARY"); | |
119 | g_assert(qemu_binary != NULL); | |
120 | ||
49ee3590 AL |
121 | s = g_malloc(sizeof(*s)); |
122 | ||
bb494a50 MA |
123 | s->socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid()); |
124 | s->qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid()); | |
125 | pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid()); | |
126 | ||
127 | sock = init_socket(s->socket_path); | |
128 | qmpsock = init_socket(s->qmp_socket_path); | |
49ee3590 AL |
129 | |
130 | pid = fork(); | |
131 | if (pid == 0) { | |
132 | command = g_strdup_printf("%s " | |
133 | "-qtest unix:%s,nowait " | |
134 | "-qtest-log /dev/null " | |
a3ca163c | 135 | "-qmp unix:%s,nowait " |
49ee3590 AL |
136 | "-pidfile %s " |
137 | "-machine accel=qtest " | |
2ad645d2 | 138 | "-display none " |
bb494a50 MA |
139 | "%s", qemu_binary, s->socket_path, |
140 | s->qmp_socket_path, pid_file, | |
49ee3590 | 141 | extra_args ?: ""); |
8a8fd637 AL |
142 | execlp("/bin/sh", "sh", "-c", command, NULL); |
143 | exit(1); | |
49ee3590 AL |
144 | } |
145 | ||
a3ca163c KW |
146 | s->fd = socket_accept(sock); |
147 | s->qmp_fd = socket_accept(qmpsock); | |
49ee3590 | 148 | |
49ee3590 AL |
149 | s->rx = g_string_new(""); |
150 | s->pid_file = pid_file; | |
6bf00766 | 151 | s->child_pid = pid; |
49ee3590 AL |
152 | for (i = 0; i < MAX_IRQ; i++) { |
153 | s->irq_level[i] = false; | |
154 | } | |
155 | ||
a3ca163c | 156 | /* Read the QMP greeting and then do the handshake */ |
0d1aa05e SH |
157 | qtest_qmp_discard_response(s, ""); |
158 | qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }"); | |
49ee3590 | 159 | |
e0fea6b1 PB |
160 | if (getenv("QTEST_STOP")) { |
161 | kill(qtest_qemu_pid(s), SIGSTOP); | |
162 | } | |
163 | ||
49ee3590 AL |
164 | return s; |
165 | } | |
166 | ||
167 | void qtest_quit(QTestState *s) | |
168 | { | |
e0fea6b1 | 169 | int status; |
49ee3590 | 170 | |
e0fea6b1 PB |
171 | pid_t pid = qtest_qemu_pid(s); |
172 | if (pid != -1) { | |
173 | kill(pid, SIGTERM); | |
8a8fd637 | 174 | waitpid(pid, &status, 0); |
49ee3590 | 175 | } |
bb494a50 | 176 | |
fdd26fca MA |
177 | close(s->fd); |
178 | close(s->qmp_fd); | |
179 | g_string_free(s->rx, true); | |
bb494a50 MA |
180 | unlink(s->pid_file); |
181 | unlink(s->socket_path); | |
182 | unlink(s->qmp_socket_path); | |
183 | g_free(s->pid_file); | |
184 | g_free(s->socket_path); | |
185 | g_free(s->qmp_socket_path); | |
fdd26fca | 186 | g_free(s); |
49ee3590 AL |
187 | } |
188 | ||
a3ca163c | 189 | static void socket_sendf(int fd, const char *fmt, va_list ap) |
49ee3590 | 190 | { |
49ee3590 AL |
191 | gchar *str; |
192 | size_t size, offset; | |
193 | ||
49ee3590 | 194 | str = g_strdup_vprintf(fmt, ap); |
49ee3590 AL |
195 | size = strlen(str); |
196 | ||
197 | offset = 0; | |
198 | while (offset < size) { | |
199 | ssize_t len; | |
200 | ||
a3ca163c | 201 | len = write(fd, str + offset, size - offset); |
49ee3590 AL |
202 | if (len == -1 && errno == EINTR) { |
203 | continue; | |
204 | } | |
205 | ||
206 | g_assert_no_errno(len); | |
207 | g_assert_cmpint(len, >, 0); | |
208 | ||
209 | offset += len; | |
210 | } | |
211 | } | |
212 | ||
a3ca163c KW |
213 | static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...) |
214 | { | |
215 | va_list ap; | |
216 | ||
217 | va_start(ap, fmt); | |
218 | socket_sendf(s->fd, fmt, ap); | |
219 | va_end(ap); | |
220 | } | |
221 | ||
49ee3590 AL |
222 | static GString *qtest_recv_line(QTestState *s) |
223 | { | |
224 | GString *line; | |
225 | size_t offset; | |
226 | char *eol; | |
227 | ||
228 | while ((eol = strchr(s->rx->str, '\n')) == NULL) { | |
229 | ssize_t len; | |
230 | char buffer[1024]; | |
231 | ||
232 | len = read(s->fd, buffer, sizeof(buffer)); | |
233 | if (len == -1 && errno == EINTR) { | |
234 | continue; | |
235 | } | |
236 | ||
237 | if (len == -1 || len == 0) { | |
238 | fprintf(stderr, "Broken pipe\n"); | |
239 | exit(1); | |
240 | } | |
241 | ||
242 | g_string_append_len(s->rx, buffer, len); | |
243 | } | |
244 | ||
245 | offset = eol - s->rx->str; | |
246 | line = g_string_new_len(s->rx->str, offset); | |
247 | g_string_erase(s->rx, 0, offset + 1); | |
248 | ||
249 | return line; | |
250 | } | |
251 | ||
252 | static gchar **qtest_rsp(QTestState *s, int expected_args) | |
253 | { | |
254 | GString *line; | |
255 | gchar **words; | |
256 | int i; | |
257 | ||
258 | redo: | |
259 | line = qtest_recv_line(s); | |
260 | words = g_strsplit(line->str, " ", 0); | |
261 | g_string_free(line, TRUE); | |
262 | ||
263 | if (strcmp(words[0], "IRQ") == 0) { | |
264 | int irq; | |
265 | ||
266 | g_assert(words[1] != NULL); | |
267 | g_assert(words[2] != NULL); | |
268 | ||
269 | irq = strtoul(words[2], NULL, 0); | |
270 | g_assert_cmpint(irq, >=, 0); | |
271 | g_assert_cmpint(irq, <, MAX_IRQ); | |
272 | ||
273 | if (strcmp(words[1], "raise") == 0) { | |
274 | s->irq_level[irq] = true; | |
275 | } else { | |
276 | s->irq_level[irq] = false; | |
277 | } | |
278 | ||
279 | g_strfreev(words); | |
280 | goto redo; | |
281 | } | |
282 | ||
283 | g_assert(words[0] != NULL); | |
284 | g_assert_cmpstr(words[0], ==, "OK"); | |
285 | ||
286 | if (expected_args) { | |
287 | for (i = 0; i < expected_args; i++) { | |
288 | g_assert(words[i] != NULL); | |
289 | } | |
290 | } else { | |
291 | g_strfreev(words); | |
292 | } | |
293 | ||
294 | return words; | |
295 | } | |
296 | ||
0c460dac SH |
297 | typedef struct { |
298 | JSONMessageParser parser; | |
299 | QDict *response; | |
300 | } QMPResponseParser; | |
301 | ||
302 | static void qmp_response(JSONMessageParser *parser, QList *tokens) | |
a3ca163c | 303 | { |
0c460dac SH |
304 | QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser); |
305 | QObject *obj; | |
306 | ||
307 | obj = json_parser_parse(tokens, NULL); | |
308 | if (!obj) { | |
309 | fprintf(stderr, "QMP JSON response parsing failed\n"); | |
310 | exit(1); | |
311 | } | |
312 | ||
313 | g_assert(qobject_type(obj) == QTYPE_QDICT); | |
314 | g_assert(!qmp->response); | |
315 | qmp->response = (QDict *)obj; | |
316 | } | |
317 | ||
318 | QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap) | |
319 | { | |
320 | QMPResponseParser qmp; | |
a3ca163c KW |
321 | |
322 | /* Send QMP request */ | |
a3ca163c | 323 | socket_sendf(s->qmp_fd, fmt, ap); |
a3ca163c KW |
324 | |
325 | /* Receive reply */ | |
0c460dac SH |
326 | qmp.response = NULL; |
327 | json_message_parser_init(&qmp.parser, qmp_response); | |
328 | while (!qmp.response) { | |
a3ca163c KW |
329 | ssize_t len; |
330 | char c; | |
331 | ||
332 | len = read(s->qmp_fd, &c, 1); | |
333 | if (len == -1 && errno == EINTR) { | |
334 | continue; | |
335 | } | |
336 | ||
039380a8 AL |
337 | if (len == -1 || len == 0) { |
338 | fprintf(stderr, "Broken pipe\n"); | |
339 | exit(1); | |
340 | } | |
341 | ||
0c460dac | 342 | json_message_parser_feed(&qmp.parser, &c, 1); |
a3ca163c | 343 | } |
0c460dac SH |
344 | json_message_parser_destroy(&qmp.parser); |
345 | ||
346 | return qmp.response; | |
347 | } | |
348 | ||
349 | QDict *qtest_qmp(QTestState *s, const char *fmt, ...) | |
350 | { | |
351 | va_list ap; | |
352 | QDict *response; | |
353 | ||
354 | va_start(ap, fmt); | |
355 | response = qtest_qmpv(s, fmt, ap); | |
356 | va_end(ap); | |
357 | return response; | |
358 | } | |
359 | ||
360 | void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap) | |
361 | { | |
362 | QDict *response = qtest_qmpv(s, fmt, ap); | |
363 | QDECREF(response); | |
a3ca163c KW |
364 | } |
365 | ||
0d1aa05e | 366 | void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...) |
b73cf9e9 AF |
367 | { |
368 | va_list ap; | |
0c460dac | 369 | QDict *response; |
b73cf9e9 AF |
370 | |
371 | va_start(ap, fmt); | |
0c460dac | 372 | response = qtest_qmpv(s, fmt, ap); |
b73cf9e9 | 373 | va_end(ap); |
0c460dac | 374 | QDECREF(response); |
b73cf9e9 AF |
375 | } |
376 | ||
49ee3590 AL |
377 | const char *qtest_get_arch(void) |
378 | { | |
379 | const char *qemu = getenv("QTEST_QEMU_BINARY"); | |
380 | const char *end = strrchr(qemu, '/'); | |
381 | ||
382 | return end + strlen("/qemu-system-"); | |
383 | } | |
384 | ||
385 | bool qtest_get_irq(QTestState *s, int num) | |
386 | { | |
387 | /* dummy operation in order to make sure irq is up to date */ | |
388 | qtest_inb(s, 0); | |
389 | ||
390 | return s->irq_level[num]; | |
391 | } | |
392 | ||
393 | static int64_t qtest_clock_rsp(QTestState *s) | |
394 | { | |
395 | gchar **words; | |
396 | int64_t clock; | |
397 | words = qtest_rsp(s, 2); | |
398 | clock = g_ascii_strtoll(words[1], NULL, 0); | |
399 | g_strfreev(words); | |
400 | return clock; | |
401 | } | |
402 | ||
403 | int64_t qtest_clock_step_next(QTestState *s) | |
404 | { | |
405 | qtest_sendf(s, "clock_step\n"); | |
406 | return qtest_clock_rsp(s); | |
407 | } | |
408 | ||
409 | int64_t qtest_clock_step(QTestState *s, int64_t step) | |
410 | { | |
411 | qtest_sendf(s, "clock_step %"PRIi64"\n", step); | |
412 | return qtest_clock_rsp(s); | |
413 | } | |
414 | ||
415 | int64_t qtest_clock_set(QTestState *s, int64_t val) | |
416 | { | |
417 | qtest_sendf(s, "clock_set %"PRIi64"\n", val); | |
418 | return qtest_clock_rsp(s); | |
419 | } | |
420 | ||
421 | void qtest_irq_intercept_out(QTestState *s, const char *qom_path) | |
422 | { | |
423 | qtest_sendf(s, "irq_intercept_out %s\n", qom_path); | |
424 | qtest_rsp(s, 0); | |
425 | } | |
426 | ||
427 | void qtest_irq_intercept_in(QTestState *s, const char *qom_path) | |
428 | { | |
429 | qtest_sendf(s, "irq_intercept_in %s\n", qom_path); | |
430 | qtest_rsp(s, 0); | |
431 | } | |
432 | ||
433 | static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value) | |
434 | { | |
435 | qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value); | |
436 | qtest_rsp(s, 0); | |
437 | } | |
438 | ||
439 | void qtest_outb(QTestState *s, uint16_t addr, uint8_t value) | |
440 | { | |
441 | qtest_out(s, "outb", addr, value); | |
442 | } | |
443 | ||
444 | void qtest_outw(QTestState *s, uint16_t addr, uint16_t value) | |
445 | { | |
446 | qtest_out(s, "outw", addr, value); | |
447 | } | |
448 | ||
449 | void qtest_outl(QTestState *s, uint16_t addr, uint32_t value) | |
450 | { | |
451 | qtest_out(s, "outl", addr, value); | |
452 | } | |
453 | ||
454 | static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr) | |
455 | { | |
456 | gchar **args; | |
457 | uint32_t value; | |
458 | ||
459 | qtest_sendf(s, "%s 0x%x\n", cmd, addr); | |
460 | args = qtest_rsp(s, 2); | |
461 | value = strtoul(args[1], NULL, 0); | |
462 | g_strfreev(args); | |
463 | ||
464 | return value; | |
465 | } | |
466 | ||
467 | uint8_t qtest_inb(QTestState *s, uint16_t addr) | |
468 | { | |
469 | return qtest_in(s, "inb", addr); | |
470 | } | |
471 | ||
472 | uint16_t qtest_inw(QTestState *s, uint16_t addr) | |
473 | { | |
474 | return qtest_in(s, "inw", addr); | |
475 | } | |
476 | ||
477 | uint32_t qtest_inl(QTestState *s, uint16_t addr) | |
478 | { | |
479 | return qtest_in(s, "inl", addr); | |
480 | } | |
481 | ||
872536bf AF |
482 | static void qtest_write(QTestState *s, const char *cmd, uint64_t addr, |
483 | uint64_t value) | |
484 | { | |
485 | qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value); | |
486 | qtest_rsp(s, 0); | |
487 | } | |
488 | ||
489 | void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value) | |
490 | { | |
491 | qtest_write(s, "writeb", addr, value); | |
492 | } | |
493 | ||
494 | void qtest_writew(QTestState *s, uint64_t addr, uint16_t value) | |
495 | { | |
496 | qtest_write(s, "writew", addr, value); | |
497 | } | |
498 | ||
499 | void qtest_writel(QTestState *s, uint64_t addr, uint32_t value) | |
500 | { | |
501 | qtest_write(s, "writel", addr, value); | |
502 | } | |
503 | ||
504 | void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value) | |
505 | { | |
506 | qtest_write(s, "writeq", addr, value); | |
507 | } | |
508 | ||
509 | static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr) | |
510 | { | |
511 | gchar **args; | |
512 | uint64_t value; | |
513 | ||
514 | qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr); | |
515 | args = qtest_rsp(s, 2); | |
516 | value = strtoull(args[1], NULL, 0); | |
517 | g_strfreev(args); | |
518 | ||
519 | return value; | |
520 | } | |
521 | ||
522 | uint8_t qtest_readb(QTestState *s, uint64_t addr) | |
523 | { | |
524 | return qtest_read(s, "readb", addr); | |
525 | } | |
526 | ||
527 | uint16_t qtest_readw(QTestState *s, uint64_t addr) | |
528 | { | |
529 | return qtest_read(s, "readw", addr); | |
530 | } | |
531 | ||
532 | uint32_t qtest_readl(QTestState *s, uint64_t addr) | |
533 | { | |
534 | return qtest_read(s, "readl", addr); | |
535 | } | |
536 | ||
537 | uint64_t qtest_readq(QTestState *s, uint64_t addr) | |
538 | { | |
539 | return qtest_read(s, "readq", addr); | |
540 | } | |
541 | ||
49ee3590 AL |
542 | static int hex2nib(char ch) |
543 | { | |
544 | if (ch >= '0' && ch <= '9') { | |
545 | return ch - '0'; | |
546 | } else if (ch >= 'a' && ch <= 'f') { | |
547 | return 10 + (ch - 'a'); | |
548 | } else if (ch >= 'A' && ch <= 'F') { | |
549 | return 10 + (ch - 'a'); | |
550 | } else { | |
551 | return -1; | |
552 | } | |
553 | } | |
554 | ||
555 | void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size) | |
556 | { | |
557 | uint8_t *ptr = data; | |
558 | gchar **args; | |
559 | size_t i; | |
560 | ||
ea7697fc | 561 | qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size); |
49ee3590 AL |
562 | args = qtest_rsp(s, 2); |
563 | ||
564 | for (i = 0; i < size; i++) { | |
565 | ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4; | |
566 | ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]); | |
567 | } | |
568 | ||
569 | g_strfreev(args); | |
570 | } | |
571 | ||
572 | void qtest_add_func(const char *str, void (*fn)) | |
573 | { | |
574 | gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str); | |
575 | g_test_add_func(path, fn); | |
576 | } | |
577 | ||
578 | void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size) | |
579 | { | |
580 | const uint8_t *ptr = data; | |
581 | size_t i; | |
582 | ||
ea7697fc | 583 | qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size); |
49ee3590 AL |
584 | for (i = 0; i < size; i++) { |
585 | qtest_sendf(s, "%02x", ptr[i]); | |
586 | } | |
587 | qtest_sendf(s, "\n"); | |
588 | qtest_rsp(s, 0); | |
589 | } |