arm: clarify the use of muldiv64()
[qemu/ar7.git] / tests / libqtest.c
blobe5188e0327a2b9c7d1d3ebc4ecd930a1f66e5523
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-parser.h"
34 #include "qapi/qmp/json-streamer.h"
35 #include "qapi/qmp/qjson.h"
37 #define MAX_IRQ 256
38 #define SOCKET_TIMEOUT 5
40 QTestState *global_qtest;
42 struct QTestState
44 int fd;
45 int qmp_fd;
46 bool irq_level[MAX_IRQ];
47 GString *rx;
48 pid_t qemu_pid; /* our child QEMU process */
49 struct sigaction sigact_old; /* restored on exit */
52 static GList *qtest_instances;
53 static struct sigaction sigact_old;
55 #define g_assert_no_errno(ret) do { \
56 g_assert_cmpint(ret, !=, -1); \
57 } while (0)
59 static int init_socket(const char *socket_path)
61 struct sockaddr_un addr;
62 int sock;
63 int ret;
65 sock = socket(PF_UNIX, SOCK_STREAM, 0);
66 g_assert_no_errno(sock);
68 addr.sun_family = AF_UNIX;
69 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
70 qemu_set_cloexec(sock);
72 do {
73 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
74 } while (ret == -1 && errno == EINTR);
75 g_assert_no_errno(ret);
76 ret = listen(sock, 1);
77 g_assert_no_errno(ret);
79 return sock;
82 static int socket_accept(int sock)
84 struct sockaddr_un addr;
85 socklen_t addrlen;
86 int ret;
87 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
88 .tv_usec = 0 };
90 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
91 sizeof(timeout));
93 do {
94 addrlen = sizeof(addr);
95 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
96 } while (ret == -1 && errno == EINTR);
97 if (ret == -1) {
98 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
100 close(sock);
102 return ret;
105 static void kill_qemu(QTestState *s)
107 if (s->qemu_pid != -1) {
108 kill(s->qemu_pid, SIGTERM);
109 waitpid(s->qemu_pid, NULL, 0);
113 static void sigabrt_handler(int signo)
115 GList *elem;
116 for (elem = qtest_instances; elem; elem = elem->next) {
117 kill_qemu(elem->data);
121 static void setup_sigabrt_handler(void)
123 struct sigaction sigact;
125 /* Catch SIGABRT to clean up on g_assert() failure */
126 sigact = (struct sigaction){
127 .sa_handler = sigabrt_handler,
128 .sa_flags = SA_RESETHAND,
130 sigemptyset(&sigact.sa_mask);
131 sigaction(SIGABRT, &sigact, &sigact_old);
134 static void cleanup_sigabrt_handler(void)
136 sigaction(SIGABRT, &sigact_old, NULL);
139 QTestState *qtest_init(const char *extra_args)
141 QTestState *s;
142 int sock, qmpsock, i;
143 gchar *socket_path;
144 gchar *qmp_socket_path;
145 gchar *command;
146 const char *qemu_binary;
148 qemu_binary = getenv("QTEST_QEMU_BINARY");
149 g_assert(qemu_binary != NULL);
151 s = g_malloc(sizeof(*s));
153 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
154 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
156 sock = init_socket(socket_path);
157 qmpsock = init_socket(qmp_socket_path);
159 /* Only install SIGABRT handler once */
160 if (!qtest_instances) {
161 setup_sigabrt_handler();
164 qtest_instances = g_list_prepend(qtest_instances, s);
166 s->qemu_pid = fork();
167 if (s->qemu_pid == 0) {
168 setenv("QEMU_AUDIO_DRV", "none", true);
169 command = g_strdup_printf("exec %s "
170 "-qtest unix:%s,nowait "
171 "-qtest-log %s "
172 "-qmp unix:%s,nowait "
173 "-machine accel=qtest "
174 "-display none "
175 "%s", qemu_binary, socket_path,
176 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
177 qmp_socket_path,
178 extra_args ?: "");
179 execlp("/bin/sh", "sh", "-c", command, NULL);
180 exit(1);
183 s->fd = socket_accept(sock);
184 if (s->fd >= 0) {
185 s->qmp_fd = socket_accept(qmpsock);
187 unlink(socket_path);
188 unlink(qmp_socket_path);
189 g_free(socket_path);
190 g_free(qmp_socket_path);
192 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
194 s->rx = g_string_new("");
195 for (i = 0; i < MAX_IRQ; i++) {
196 s->irq_level[i] = false;
199 /* Read the QMP greeting and then do the handshake */
200 qtest_qmp_discard_response(s, "");
201 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
203 if (getenv("QTEST_STOP")) {
204 kill(s->qemu_pid, SIGSTOP);
207 return s;
210 void qtest_quit(QTestState *s)
212 /* Uninstall SIGABRT handler on last instance */
213 if (qtest_instances && !qtest_instances->next) {
214 cleanup_sigabrt_handler();
217 qtest_instances = g_list_remove(qtest_instances, s);
219 kill_qemu(s);
220 close(s->fd);
221 close(s->qmp_fd);
222 g_string_free(s->rx, true);
223 g_free(s);
226 static void socket_send(int fd, const char *buf, size_t size)
228 size_t offset;
230 offset = 0;
231 while (offset < size) {
232 ssize_t len;
234 len = write(fd, buf + offset, size - offset);
235 if (len == -1 && errno == EINTR) {
236 continue;
239 g_assert_no_errno(len);
240 g_assert_cmpint(len, >, 0);
242 offset += len;
246 static void socket_sendf(int fd, const char *fmt, va_list ap)
248 gchar *str = g_strdup_vprintf(fmt, ap);
249 size_t size = strlen(str);
251 socket_send(fd, str, size);
252 g_free(str);
255 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
257 va_list ap;
259 va_start(ap, fmt);
260 socket_sendf(s->fd, fmt, ap);
261 va_end(ap);
264 static GString *qtest_recv_line(QTestState *s)
266 GString *line;
267 size_t offset;
268 char *eol;
270 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
271 ssize_t len;
272 char buffer[1024];
274 len = read(s->fd, buffer, sizeof(buffer));
275 if (len == -1 && errno == EINTR) {
276 continue;
279 if (len == -1 || len == 0) {
280 fprintf(stderr, "Broken pipe\n");
281 exit(1);
284 g_string_append_len(s->rx, buffer, len);
287 offset = eol - s->rx->str;
288 line = g_string_new_len(s->rx->str, offset);
289 g_string_erase(s->rx, 0, offset + 1);
291 return line;
294 static gchar **qtest_rsp(QTestState *s, int expected_args)
296 GString *line;
297 gchar **words;
298 int i;
300 redo:
301 line = qtest_recv_line(s);
302 words = g_strsplit(line->str, " ", 0);
303 g_string_free(line, TRUE);
305 if (strcmp(words[0], "IRQ") == 0) {
306 int irq;
308 g_assert(words[1] != NULL);
309 g_assert(words[2] != NULL);
311 irq = strtoul(words[2], NULL, 0);
312 g_assert_cmpint(irq, >=, 0);
313 g_assert_cmpint(irq, <, MAX_IRQ);
315 if (strcmp(words[1], "raise") == 0) {
316 s->irq_level[irq] = true;
317 } else {
318 s->irq_level[irq] = false;
321 g_strfreev(words);
322 goto redo;
325 g_assert(words[0] != NULL);
326 g_assert_cmpstr(words[0], ==, "OK");
328 if (expected_args) {
329 for (i = 0; i < expected_args; i++) {
330 g_assert(words[i] != NULL);
332 } else {
333 g_strfreev(words);
336 return words;
339 typedef struct {
340 JSONMessageParser parser;
341 QDict *response;
342 } QMPResponseParser;
344 static void qmp_response(JSONMessageParser *parser, QList *tokens)
346 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
347 QObject *obj;
349 obj = json_parser_parse(tokens, NULL);
350 if (!obj) {
351 fprintf(stderr, "QMP JSON response parsing failed\n");
352 exit(1);
355 g_assert(qobject_type(obj) == QTYPE_QDICT);
356 g_assert(!qmp->response);
357 qmp->response = (QDict *)obj;
360 QDict *qtest_qmp_receive(QTestState *s)
362 QMPResponseParser qmp;
363 bool log = getenv("QTEST_LOG") != NULL;
365 qmp.response = NULL;
366 json_message_parser_init(&qmp.parser, qmp_response);
367 while (!qmp.response) {
368 ssize_t len;
369 char c;
371 len = read(s->qmp_fd, &c, 1);
372 if (len == -1 && errno == EINTR) {
373 continue;
376 if (len == -1 || len == 0) {
377 fprintf(stderr, "Broken pipe\n");
378 exit(1);
381 if (log) {
382 len = write(2, &c, 1);
384 json_message_parser_feed(&qmp.parser, &c, 1);
386 json_message_parser_destroy(&qmp.parser);
388 return qmp.response;
392 * Allow users to send a message without waiting for the reply,
393 * in the case that they choose to discard all replies up until
394 * a particular EVENT is received.
396 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
398 va_list ap_copy;
399 QObject *qobj;
401 /* Going through qobject ensures we escape strings properly.
402 * This seemingly unnecessary copy is required in case va_list
403 * is an array type.
405 va_copy(ap_copy, ap);
406 qobj = qobject_from_jsonv(fmt, &ap_copy);
407 va_end(ap_copy);
409 /* No need to send anything for an empty QObject. */
410 if (qobj) {
411 int log = getenv("QTEST_LOG") != NULL;
412 QString *qstr = qobject_to_json(qobj);
413 const char *str = qstring_get_str(qstr);
414 size_t size = qstring_get_length(qstr);
416 if (log) {
417 fprintf(stderr, "%s", str);
419 /* Send QMP request */
420 socket_send(s->qmp_fd, str, size);
422 QDECREF(qstr);
423 qobject_decref(qobj);
427 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
429 qtest_async_qmpv(s, fmt, ap);
431 /* Receive reply */
432 return qtest_qmp_receive(s);
435 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
437 va_list ap;
438 QDict *response;
440 va_start(ap, fmt);
441 response = qtest_qmpv(s, fmt, ap);
442 va_end(ap);
443 return response;
446 void qtest_async_qmp(QTestState *s, const char *fmt, ...)
448 va_list ap;
450 va_start(ap, fmt);
451 qtest_async_qmpv(s, fmt, ap);
452 va_end(ap);
455 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
457 QDict *response = qtest_qmpv(s, fmt, ap);
458 QDECREF(response);
461 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
463 va_list ap;
464 QDict *response;
466 va_start(ap, fmt);
467 response = qtest_qmpv(s, fmt, ap);
468 va_end(ap);
469 QDECREF(response);
472 void qtest_qmp_eventwait(QTestState *s, const char *event)
474 QDict *response;
476 for (;;) {
477 response = qtest_qmp_receive(s);
478 if ((qdict_haskey(response, "event")) &&
479 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
480 QDECREF(response);
481 break;
483 QDECREF(response);
488 const char *qtest_get_arch(void)
490 const char *qemu = getenv("QTEST_QEMU_BINARY");
491 g_assert(qemu != NULL);
492 const char *end = strrchr(qemu, '/');
494 return end + strlen("/qemu-system-");
497 bool qtest_get_irq(QTestState *s, int num)
499 /* dummy operation in order to make sure irq is up to date */
500 qtest_inb(s, 0);
502 return s->irq_level[num];
505 static int64_t qtest_clock_rsp(QTestState *s)
507 gchar **words;
508 int64_t clock;
509 words = qtest_rsp(s, 2);
510 clock = g_ascii_strtoll(words[1], NULL, 0);
511 g_strfreev(words);
512 return clock;
515 int64_t qtest_clock_step_next(QTestState *s)
517 qtest_sendf(s, "clock_step\n");
518 return qtest_clock_rsp(s);
521 int64_t qtest_clock_step(QTestState *s, int64_t step)
523 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
524 return qtest_clock_rsp(s);
527 int64_t qtest_clock_set(QTestState *s, int64_t val)
529 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
530 return qtest_clock_rsp(s);
533 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
535 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
536 qtest_rsp(s, 0);
539 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
541 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
542 qtest_rsp(s, 0);
545 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
547 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
548 qtest_rsp(s, 0);
551 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
553 qtest_out(s, "outb", addr, value);
556 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
558 qtest_out(s, "outw", addr, value);
561 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
563 qtest_out(s, "outl", addr, value);
566 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
568 gchar **args;
569 uint32_t value;
571 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
572 args = qtest_rsp(s, 2);
573 value = strtoul(args[1], NULL, 0);
574 g_strfreev(args);
576 return value;
579 uint8_t qtest_inb(QTestState *s, uint16_t addr)
581 return qtest_in(s, "inb", addr);
584 uint16_t qtest_inw(QTestState *s, uint16_t addr)
586 return qtest_in(s, "inw", addr);
589 uint32_t qtest_inl(QTestState *s, uint16_t addr)
591 return qtest_in(s, "inl", addr);
594 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
595 uint64_t value)
597 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
598 qtest_rsp(s, 0);
601 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
603 qtest_write(s, "writeb", addr, value);
606 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
608 qtest_write(s, "writew", addr, value);
611 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
613 qtest_write(s, "writel", addr, value);
616 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
618 qtest_write(s, "writeq", addr, value);
621 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
623 gchar **args;
624 uint64_t value;
626 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
627 args = qtest_rsp(s, 2);
628 value = strtoull(args[1], NULL, 0);
629 g_strfreev(args);
631 return value;
634 uint8_t qtest_readb(QTestState *s, uint64_t addr)
636 return qtest_read(s, "readb", addr);
639 uint16_t qtest_readw(QTestState *s, uint64_t addr)
641 return qtest_read(s, "readw", addr);
644 uint32_t qtest_readl(QTestState *s, uint64_t addr)
646 return qtest_read(s, "readl", addr);
649 uint64_t qtest_readq(QTestState *s, uint64_t addr)
651 return qtest_read(s, "readq", addr);
654 static int hex2nib(char ch)
656 if (ch >= '0' && ch <= '9') {
657 return ch - '0';
658 } else if (ch >= 'a' && ch <= 'f') {
659 return 10 + (ch - 'a');
660 } else if (ch >= 'A' && ch <= 'F') {
661 return 10 + (ch - 'a');
662 } else {
663 return -1;
667 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
669 uint8_t *ptr = data;
670 gchar **args;
671 size_t i;
673 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
674 args = qtest_rsp(s, 2);
676 for (i = 0; i < size; i++) {
677 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
678 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
681 g_strfreev(args);
684 void qtest_add_func(const char *str, void (*fn))
686 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
687 g_test_add_func(path, fn);
688 g_free(path);
691 void qtest_add_data_func(const char *str, const void *data, void (*fn))
693 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
694 g_test_add_data_func(path, data, fn);
695 g_free(path);
698 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
700 gchar *bdata;
702 bdata = g_base64_encode(data, size);
703 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
704 socket_send(s->fd, bdata, strlen(bdata));
705 socket_send(s->fd, "\n", 1);
706 qtest_rsp(s, 0);
707 g_free(bdata);
710 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
712 gchar **args;
713 size_t len;
715 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
716 args = qtest_rsp(s, 2);
718 g_base64_decode_inplace(args[1], &len);
719 if (size != len) {
720 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
721 size, len);
722 len = MIN(len, size);
725 memcpy(data, args[1], len);
726 g_strfreev(args);
729 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
731 const uint8_t *ptr = data;
732 size_t i;
733 char *enc = g_malloc(2 * size + 1);
735 for (i = 0; i < size; i++) {
736 sprintf(&enc[i * 2], "%02x", ptr[i]);
739 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
740 qtest_rsp(s, 0);
741 g_free(enc);
744 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
746 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
747 qtest_rsp(s, 0);
750 QDict *qmp(const char *fmt, ...)
752 va_list ap;
753 QDict *response;
755 va_start(ap, fmt);
756 response = qtest_qmpv(global_qtest, fmt, ap);
757 va_end(ap);
758 return response;
761 void qmp_async(const char *fmt, ...)
763 va_list ap;
765 va_start(ap, fmt);
766 qtest_async_qmpv(global_qtest, fmt, ap);
767 va_end(ap);
770 void qmp_discard_response(const char *fmt, ...)
772 va_list ap;
774 va_start(ap, fmt);
775 qtest_qmpv_discard_response(global_qtest, fmt, ap);
776 va_end(ap);
779 bool qtest_big_endian(void)
781 const char *arch = qtest_get_arch();
782 int i;
784 static const struct {
785 const char *arch;
786 bool big_endian;
787 } endianness[] = {
788 { "aarch64", false },
789 { "alpha", false },
790 { "arm", false },
791 { "cris", false },
792 { "i386", false },
793 { "lm32", true },
794 { "m68k", true },
795 { "microblaze", true },
796 { "microblazeel", false },
797 { "mips", true },
798 { "mips64", true },
799 { "mips64el", false },
800 { "mipsel", false },
801 { "moxie", true },
802 { "or32", true },
803 { "ppc", true },
804 { "ppc64", true },
805 { "ppcemb", true },
806 { "s390x", true },
807 { "sh4", false },
808 { "sh4eb", true },
809 { "sparc", true },
810 { "sparc64", true },
811 { "unicore32", false },
812 { "x86_64", false },
813 { "xtensa", false },
814 { "xtensaeb", true },
818 for (i = 0; endianness[i].arch; i++) {
819 if (strcmp(endianness[i].arch, arch) == 0) {
820 return endianness[i].big_endian;
824 return false;