xen-hvm: Handle machine opt max-ram-below-4g
[qemu/kevin.git] / tests / libqtest.c
blob71468ac9c70909a73c26268aec803e70a59057ff
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
8 * Authors:
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.
17 #include "libqtest.h"
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>
31 #include "qemu/compiler.h"
32 #include "qemu/osdep.h"
33 #include "qapi/qmp/json-streamer.h"
34 #include "qapi/qmp/json-parser.h"
36 #define MAX_IRQ 256
37 #define SOCKET_TIMEOUT 5
39 QTestState *global_qtest;
41 struct QTestState
43 int fd;
44 int qmp_fd;
45 bool irq_level[MAX_IRQ];
46 GString *rx;
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); \
56 } while (0)
58 static int init_socket(const char *socket_path)
60 struct sockaddr_un addr;
61 int sock;
62 int ret;
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);
71 do {
72 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
73 } while (ret == -1 && errno == EINTR);
74 g_assert_no_errno(ret);
75 ret = listen(sock, 1);
76 g_assert_no_errno(ret);
78 return sock;
81 static int socket_accept(int sock)
83 struct sockaddr_un addr;
84 socklen_t addrlen;
85 int ret;
86 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
87 .tv_usec = 0 };
89 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
90 sizeof(timeout));
92 do {
93 addrlen = sizeof(addr);
94 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
95 } while (ret == -1 && errno == EINTR);
96 if (ret == -1) {
97 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
99 close(sock);
101 return ret;
104 static void kill_qemu(QTestState *s)
106 if (s->qemu_pid != -1) {
107 kill(s->qemu_pid, SIGTERM);
108 waitpid(s->qemu_pid, NULL, 0);
112 static void sigabrt_handler(int signo)
114 GList *elem;
115 for (elem = qtest_instances; elem; elem = elem->next) {
116 kill_qemu(elem->data);
120 static void setup_sigabrt_handler(void)
122 struct sigaction sigact;
124 /* Catch SIGABRT to clean up on g_assert() failure */
125 sigact = (struct sigaction){
126 .sa_handler = sigabrt_handler,
127 .sa_flags = SA_RESETHAND,
129 sigemptyset(&sigact.sa_mask);
130 sigaction(SIGABRT, &sigact, &sigact_old);
133 static void cleanup_sigabrt_handler(void)
135 sigaction(SIGABRT, &sigact_old, NULL);
138 QTestState *qtest_init(const char *extra_args)
140 QTestState *s;
141 int sock, qmpsock, i;
142 gchar *socket_path;
143 gchar *qmp_socket_path;
144 gchar *command;
145 const char *qemu_binary;
147 qemu_binary = getenv("QTEST_QEMU_BINARY");
148 g_assert(qemu_binary != NULL);
150 s = g_malloc(sizeof(*s));
152 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
153 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
155 sock = init_socket(socket_path);
156 qmpsock = init_socket(qmp_socket_path);
158 /* Only install SIGABRT handler once */
159 if (!qtest_instances) {
160 setup_sigabrt_handler();
163 qtest_instances = g_list_prepend(qtest_instances, s);
165 s->qemu_pid = fork();
166 if (s->qemu_pid == 0) {
167 command = g_strdup_printf("exec %s "
168 "-qtest unix:%s,nowait "
169 "-qtest-log /dev/null "
170 "-qmp unix:%s,nowait "
171 "-machine accel=qtest "
172 "-display none "
173 "%s", qemu_binary, socket_path,
174 qmp_socket_path,
175 extra_args ?: "");
176 execlp("/bin/sh", "sh", "-c", command, NULL);
177 exit(1);
180 s->fd = socket_accept(sock);
181 if (s->fd >= 0) {
182 s->qmp_fd = socket_accept(qmpsock);
184 unlink(socket_path);
185 unlink(qmp_socket_path);
186 g_free(socket_path);
187 g_free(qmp_socket_path);
189 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
191 s->rx = g_string_new("");
192 for (i = 0; i < MAX_IRQ; i++) {
193 s->irq_level[i] = false;
196 /* Read the QMP greeting and then do the handshake */
197 qtest_qmp_discard_response(s, "");
198 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
200 if (getenv("QTEST_STOP")) {
201 kill(s->qemu_pid, SIGSTOP);
204 return s;
207 void qtest_quit(QTestState *s)
209 /* Uninstall SIGABRT handler on last instance */
210 if (qtest_instances && !qtest_instances->next) {
211 cleanup_sigabrt_handler();
214 qtest_instances = g_list_remove(qtest_instances, s);
216 kill_qemu(s);
217 close(s->fd);
218 close(s->qmp_fd);
219 g_string_free(s->rx, true);
220 g_free(s);
223 static void socket_sendf(int fd, const char *fmt, va_list ap)
225 gchar *str;
226 size_t size, offset;
228 str = g_strdup_vprintf(fmt, ap);
229 size = strlen(str);
231 offset = 0;
232 while (offset < size) {
233 ssize_t len;
235 len = write(fd, str + offset, size - offset);
236 if (len == -1 && errno == EINTR) {
237 continue;
240 g_assert_no_errno(len);
241 g_assert_cmpint(len, >, 0);
243 offset += len;
247 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
249 va_list ap;
251 va_start(ap, fmt);
252 socket_sendf(s->fd, fmt, ap);
253 va_end(ap);
256 static GString *qtest_recv_line(QTestState *s)
258 GString *line;
259 size_t offset;
260 char *eol;
262 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
263 ssize_t len;
264 char buffer[1024];
266 len = read(s->fd, buffer, sizeof(buffer));
267 if (len == -1 && errno == EINTR) {
268 continue;
271 if (len == -1 || len == 0) {
272 fprintf(stderr, "Broken pipe\n");
273 exit(1);
276 g_string_append_len(s->rx, buffer, len);
279 offset = eol - s->rx->str;
280 line = g_string_new_len(s->rx->str, offset);
281 g_string_erase(s->rx, 0, offset + 1);
283 return line;
286 static gchar **qtest_rsp(QTestState *s, int expected_args)
288 GString *line;
289 gchar **words;
290 int i;
292 redo:
293 line = qtest_recv_line(s);
294 words = g_strsplit(line->str, " ", 0);
295 g_string_free(line, TRUE);
297 if (strcmp(words[0], "IRQ") == 0) {
298 int irq;
300 g_assert(words[1] != NULL);
301 g_assert(words[2] != NULL);
303 irq = strtoul(words[2], NULL, 0);
304 g_assert_cmpint(irq, >=, 0);
305 g_assert_cmpint(irq, <, MAX_IRQ);
307 if (strcmp(words[1], "raise") == 0) {
308 s->irq_level[irq] = true;
309 } else {
310 s->irq_level[irq] = false;
313 g_strfreev(words);
314 goto redo;
317 g_assert(words[0] != NULL);
318 g_assert_cmpstr(words[0], ==, "OK");
320 if (expected_args) {
321 for (i = 0; i < expected_args; i++) {
322 g_assert(words[i] != NULL);
324 } else {
325 g_strfreev(words);
328 return words;
331 typedef struct {
332 JSONMessageParser parser;
333 QDict *response;
334 } QMPResponseParser;
336 static void qmp_response(JSONMessageParser *parser, QList *tokens)
338 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
339 QObject *obj;
341 obj = json_parser_parse(tokens, NULL);
342 if (!obj) {
343 fprintf(stderr, "QMP JSON response parsing failed\n");
344 exit(1);
347 g_assert(qobject_type(obj) == QTYPE_QDICT);
348 g_assert(!qmp->response);
349 qmp->response = (QDict *)obj;
352 QDict *qtest_qmp_receive(QTestState *s)
354 QMPResponseParser qmp;
356 qmp.response = NULL;
357 json_message_parser_init(&qmp.parser, qmp_response);
358 while (!qmp.response) {
359 ssize_t len;
360 char c;
362 len = read(s->qmp_fd, &c, 1);
363 if (len == -1 && errno == EINTR) {
364 continue;
367 if (len == -1 || len == 0) {
368 fprintf(stderr, "Broken pipe\n");
369 exit(1);
372 json_message_parser_feed(&qmp.parser, &c, 1);
374 json_message_parser_destroy(&qmp.parser);
376 return qmp.response;
379 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
381 /* Send QMP request */
382 socket_sendf(s->qmp_fd, fmt, ap);
384 /* Receive reply */
385 return qtest_qmp_receive(s);
388 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
390 va_list ap;
391 QDict *response;
393 va_start(ap, fmt);
394 response = qtest_qmpv(s, fmt, ap);
395 va_end(ap);
396 return response;
399 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
401 QDict *response = qtest_qmpv(s, fmt, ap);
402 QDECREF(response);
405 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
407 va_list ap;
408 QDict *response;
410 va_start(ap, fmt);
411 response = qtest_qmpv(s, fmt, ap);
412 va_end(ap);
413 QDECREF(response);
416 const char *qtest_get_arch(void)
418 const char *qemu = getenv("QTEST_QEMU_BINARY");
419 const char *end = strrchr(qemu, '/');
421 return end + strlen("/qemu-system-");
424 bool qtest_get_irq(QTestState *s, int num)
426 /* dummy operation in order to make sure irq is up to date */
427 qtest_inb(s, 0);
429 return s->irq_level[num];
432 static int64_t qtest_clock_rsp(QTestState *s)
434 gchar **words;
435 int64_t clock;
436 words = qtest_rsp(s, 2);
437 clock = g_ascii_strtoll(words[1], NULL, 0);
438 g_strfreev(words);
439 return clock;
442 int64_t qtest_clock_step_next(QTestState *s)
444 qtest_sendf(s, "clock_step\n");
445 return qtest_clock_rsp(s);
448 int64_t qtest_clock_step(QTestState *s, int64_t step)
450 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
451 return qtest_clock_rsp(s);
454 int64_t qtest_clock_set(QTestState *s, int64_t val)
456 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
457 return qtest_clock_rsp(s);
460 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
462 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
463 qtest_rsp(s, 0);
466 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
468 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
469 qtest_rsp(s, 0);
472 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
474 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
475 qtest_rsp(s, 0);
478 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
480 qtest_out(s, "outb", addr, value);
483 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
485 qtest_out(s, "outw", addr, value);
488 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
490 qtest_out(s, "outl", addr, value);
493 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
495 gchar **args;
496 uint32_t value;
498 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
499 args = qtest_rsp(s, 2);
500 value = strtoul(args[1], NULL, 0);
501 g_strfreev(args);
503 return value;
506 uint8_t qtest_inb(QTestState *s, uint16_t addr)
508 return qtest_in(s, "inb", addr);
511 uint16_t qtest_inw(QTestState *s, uint16_t addr)
513 return qtest_in(s, "inw", addr);
516 uint32_t qtest_inl(QTestState *s, uint16_t addr)
518 return qtest_in(s, "inl", addr);
521 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
522 uint64_t value)
524 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
525 qtest_rsp(s, 0);
528 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
530 qtest_write(s, "writeb", addr, value);
533 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
535 qtest_write(s, "writew", addr, value);
538 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
540 qtest_write(s, "writel", addr, value);
543 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
545 qtest_write(s, "writeq", addr, value);
548 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
550 gchar **args;
551 uint64_t value;
553 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
554 args = qtest_rsp(s, 2);
555 value = strtoull(args[1], NULL, 0);
556 g_strfreev(args);
558 return value;
561 uint8_t qtest_readb(QTestState *s, uint64_t addr)
563 return qtest_read(s, "readb", addr);
566 uint16_t qtest_readw(QTestState *s, uint64_t addr)
568 return qtest_read(s, "readw", addr);
571 uint32_t qtest_readl(QTestState *s, uint64_t addr)
573 return qtest_read(s, "readl", addr);
576 uint64_t qtest_readq(QTestState *s, uint64_t addr)
578 return qtest_read(s, "readq", addr);
581 static int hex2nib(char ch)
583 if (ch >= '0' && ch <= '9') {
584 return ch - '0';
585 } else if (ch >= 'a' && ch <= 'f') {
586 return 10 + (ch - 'a');
587 } else if (ch >= 'A' && ch <= 'F') {
588 return 10 + (ch - 'a');
589 } else {
590 return -1;
594 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
596 uint8_t *ptr = data;
597 gchar **args;
598 size_t i;
600 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
601 args = qtest_rsp(s, 2);
603 for (i = 0; i < size; i++) {
604 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
605 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
608 g_strfreev(args);
611 void qtest_add_func(const char *str, void (*fn))
613 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
614 g_test_add_func(path, fn);
617 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
619 const uint8_t *ptr = data;
620 size_t i;
622 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size);
623 for (i = 0; i < size; i++) {
624 qtest_sendf(s, "%02x", ptr[i]);
626 qtest_sendf(s, "\n");
627 qtest_rsp(s, 0);
630 QDict *qmp(const char *fmt, ...)
632 va_list ap;
633 QDict *response;
635 va_start(ap, fmt);
636 response = qtest_qmpv(global_qtest, fmt, ap);
637 va_end(ap);
638 return response;
641 void qmp_discard_response(const char *fmt, ...)
643 va_list ap;
645 va_start(ap, fmt);
646 qtest_qmpv_discard_response(global_qtest, fmt, ap);
647 va_end(ap);