1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include "qemu/osdep.h"
18
19#include <sys/socket.h>
20#include <sys/wait.h>
21#include <sys/un.h>
22
23#include "libqtest.h"
24#include "qemu-common.h"
25#include "qemu/ctype.h"
26#include "qemu/cutils.h"
27#include "qapi/error.h"
28#include "qapi/qmp/json-parser.h"
29#include "qapi/qmp/qdict.h"
30#include "qapi/qmp/qjson.h"
31#include "qapi/qmp/qlist.h"
32#include "qapi/qmp/qstring.h"
33
34#define MAX_IRQ 256
35#define SOCKET_TIMEOUT 50
36#define SOCKET_MAX_FDS 16
37
38QTestState *global_qtest;
39
40struct QTestState
41{
42 int fd;
43 int qmp_fd;
44 pid_t qemu_pid;
45 int wstatus;
46 bool big_endian;
47 bool irq_level[MAX_IRQ];
48 GString *rx;
49};
50
51static GHookList abrt_hooks;
52static struct sigaction sigact_old;
53
54static int qtest_query_target_endianness(QTestState *s);
55
56static int init_socket(const char *socket_path)
57{
58 struct sockaddr_un addr;
59 int sock;
60 int ret;
61
62 sock = socket(PF_UNIX, SOCK_STREAM, 0);
63 g_assert_cmpint(sock, !=, -1);
64
65 addr.sun_family = AF_UNIX;
66 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
67 qemu_set_cloexec(sock);
68
69 do {
70 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
71 } while (ret == -1 && errno == EINTR);
72 g_assert_cmpint(ret, !=, -1);
73 ret = listen(sock, 1);
74 g_assert_cmpint(ret, !=, -1);
75
76 return sock;
77}
78
79static int socket_accept(int sock)
80{
81 struct sockaddr_un addr;
82 socklen_t addrlen;
83 int ret;
84 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
85 .tv_usec = 0 };
86
87 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
88 sizeof(timeout));
89
90 do {
91 addrlen = sizeof(addr);
92 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
93 } while (ret == -1 && errno == EINTR);
94 if (ret == -1) {
95 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
96 }
97 close(sock);
98
99 return ret;
100}
101
102bool qtest_probe_child(QTestState *s)
103{
104 pid_t pid = s->qemu_pid;
105
106 if (pid != -1) {
107 pid = waitpid(pid, &s->wstatus, WNOHANG);
108 if (pid == 0) {
109 return true;
110 }
111 s->qemu_pid = -1;
112 }
113 return false;
114}
115
116static void kill_qemu(QTestState *s)
117{
118 pid_t pid = s->qemu_pid;
119 int wstatus;
120
121
122 if (pid != -1) {
123 kill(pid, SIGTERM);
124 TFR(pid = waitpid(s->qemu_pid, &s->wstatus, 0));
125 assert(pid == s->qemu_pid);
126 }
127
128
129
130
131
132 wstatus = s->wstatus;
133 if (wstatus) {
134 if (WIFEXITED(wstatus)) {
135 fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
136 "process but encountered exit status %d\n",
137 __FILE__, __LINE__, WEXITSTATUS(wstatus));
138 } else if (WIFSIGNALED(wstatus)) {
139 int sig = WTERMSIG(wstatus);
140 const char *signame = strsignal(sig) ?: "unknown ???";
141 const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : "";
142
143 fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death "
144 "from signal %d (%s)%s\n",
145 __FILE__, __LINE__, sig, signame, dump);
146 }
147 abort();
148 }
149}
150
151static void kill_qemu_hook_func(void *s)
152{
153 kill_qemu(s);
154}
155
156static void sigabrt_handler(int signo)
157{
158 g_hook_list_invoke(&abrt_hooks, FALSE);
159}
160
161static void setup_sigabrt_handler(void)
162{
163 struct sigaction sigact;
164
165
166 sigact = (struct sigaction){
167 .sa_handler = sigabrt_handler,
168 .sa_flags = SA_RESETHAND,
169 };
170 sigemptyset(&sigact.sa_mask);
171 sigaction(SIGABRT, &sigact, &sigact_old);
172}
173
174static void cleanup_sigabrt_handler(void)
175{
176 sigaction(SIGABRT, &sigact_old, NULL);
177}
178
179void qtest_add_abrt_handler(GHookFunc fn, const void *data)
180{
181 GHook *hook;
182
183
184 if (!abrt_hooks.is_setup) {
185 g_hook_list_init(&abrt_hooks, sizeof(GHook));
186 }
187 setup_sigabrt_handler();
188
189 hook = g_hook_alloc(&abrt_hooks);
190 hook->func = fn;
191 hook->data = (void *)data;
192
193 g_hook_prepend(&abrt_hooks, hook);
194}
195
196static const char *qtest_qemu_binary(void)
197{
198 const char *qemu_bin;
199
200 qemu_bin = getenv("QTEST_QEMU_BINARY");
201 if (!qemu_bin) {
202 fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
203 exit(1);
204 }
205
206 return qemu_bin;
207}
208
209QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
210{
211 QTestState *s;
212 int sock, qmpsock, i;
213 gchar *socket_path;
214 gchar *qmp_socket_path;
215 gchar *command;
216 const char *qemu_binary = qtest_qemu_binary();
217
218 s = g_new(QTestState, 1);
219
220 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
221 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
222
223
224
225
226
227
228 unlink(socket_path);
229 unlink(qmp_socket_path);
230
231 sock = init_socket(socket_path);
232 qmpsock = init_socket(qmp_socket_path);
233
234 qtest_add_abrt_handler(kill_qemu_hook_func, s);
235
236 command = g_strdup_printf("exec %s "
237 "-qtest unix:%s "
238 "-qtest-log %s "
239 "-chardev socket,path=%s,id=char0 "
240 "-mon chardev=char0,mode=control "
241 "-machine accel=qtest "
242 "-display none "
243 "%s", qemu_binary, socket_path,
244 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
245 qmp_socket_path,
246 extra_args ?: "");
247
248 g_test_message("starting QEMU: %s", command);
249
250 s->wstatus = 0;
251 s->qemu_pid = fork();
252 if (s->qemu_pid == 0) {
253 setenv("QEMU_AUDIO_DRV", "none", true);
254 execlp("/bin/sh", "sh", "-c", command, NULL);
255 exit(1);
256 }
257
258 g_free(command);
259 s->fd = socket_accept(sock);
260 if (s->fd >= 0) {
261 s->qmp_fd = socket_accept(qmpsock);
262 }
263 unlink(socket_path);
264 unlink(qmp_socket_path);
265 g_free(socket_path);
266 g_free(qmp_socket_path);
267
268 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
269
270 s->rx = g_string_new("");
271 for (i = 0; i < MAX_IRQ; i++) {
272 s->irq_level[i] = false;
273 }
274
275 if (getenv("QTEST_STOP")) {
276 kill(s->qemu_pid, SIGSTOP);
277 }
278
279
280
281 s->big_endian = qtest_query_target_endianness(s);
282
283 return s;
284}
285
286QTestState *qtest_init(const char *extra_args)
287{
288 QTestState *s = qtest_init_without_qmp_handshake(extra_args);
289 QDict *greeting;
290
291
292 greeting = qtest_qmp_receive(s);
293 qobject_unref(greeting);
294 qobject_unref(qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }"));
295
296 return s;
297}
298
299QTestState *qtest_vinitf(const char *fmt, va_list ap)
300{
301 char *args = g_strdup_vprintf(fmt, ap);
302 QTestState *s;
303
304 s = qtest_init(args);
305 g_free(args);
306 return s;
307}
308
309QTestState *qtest_initf(const char *fmt, ...)
310{
311 va_list ap;
312 QTestState *s;
313
314 va_start(ap, fmt);
315 s = qtest_vinitf(fmt, ap);
316 va_end(ap);
317 return s;
318}
319
320QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd)
321{
322 int sock_fd_init;
323 char *sock_path, sock_dir[] = "/tmp/qtest-serial-XXXXXX";
324 QTestState *qts;
325
326 g_assert_true(mkdtemp(sock_dir) != NULL);
327 sock_path = g_strdup_printf("%s/sock", sock_dir);
328
329 sock_fd_init = init_socket(sock_path);
330
331 qts = qtest_initf("-chardev socket,id=s0,path=%s -serial chardev:s0 %s",
332 sock_path, extra_args);
333
334 *sock_fd = socket_accept(sock_fd_init);
335
336 unlink(sock_path);
337 g_free(sock_path);
338 rmdir(sock_dir);
339
340 g_assert_true(*sock_fd >= 0);
341
342 return qts;
343}
344
345void qtest_quit(QTestState *s)
346{
347 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
348
349
350 cleanup_sigabrt_handler();
351
352 kill_qemu(s);
353 close(s->fd);
354 close(s->qmp_fd);
355 g_string_free(s->rx, true);
356 g_free(s);
357}
358
359static void socket_send(int fd, const char *buf, size_t size)
360{
361 size_t offset;
362
363 offset = 0;
364 while (offset < size) {
365 ssize_t len;
366
367 len = write(fd, buf + offset, size - offset);
368 if (len == -1 && errno == EINTR) {
369 continue;
370 }
371
372 g_assert_cmpint(len, >, 0);
373
374 offset += len;
375 }
376}
377
378static void socket_sendf(int fd, const char *fmt, va_list ap)
379{
380 gchar *str = g_strdup_vprintf(fmt, ap);
381 size_t size = strlen(str);
382
383 socket_send(fd, str, size);
384 g_free(str);
385}
386
387static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
388{
389 va_list ap;
390
391 va_start(ap, fmt);
392 socket_sendf(s->fd, fmt, ap);
393 va_end(ap);
394}
395
396
397
398static void socket_send_fds(int socket_fd, int *fds, size_t fds_num,
399 const char *buf, size_t buf_size)
400{
401 ssize_t ret;
402 struct msghdr msg = { 0 };
403 char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)] = { 0 };
404 size_t fdsize = sizeof(int) * fds_num;
405 struct cmsghdr *cmsg;
406 struct iovec iov = { .iov_base = (char *)buf, .iov_len = buf_size };
407
408 msg.msg_iov = &iov;
409 msg.msg_iovlen = 1;
410
411 if (fds && fds_num > 0) {
412 g_assert_cmpuint(fds_num, <, SOCKET_MAX_FDS);
413
414 msg.msg_control = control;
415 msg.msg_controllen = CMSG_SPACE(fdsize);
416
417 cmsg = CMSG_FIRSTHDR(&msg);
418 cmsg->cmsg_len = CMSG_LEN(fdsize);
419 cmsg->cmsg_level = SOL_SOCKET;
420 cmsg->cmsg_type = SCM_RIGHTS;
421 memcpy(CMSG_DATA(cmsg), fds, fdsize);
422 }
423
424 do {
425 ret = sendmsg(socket_fd, &msg, 0);
426 } while (ret < 0 && errno == EINTR);
427 g_assert_cmpint(ret, >, 0);
428}
429
430static GString *qtest_recv_line(QTestState *s)
431{
432 GString *line;
433 size_t offset;
434 char *eol;
435
436 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
437 ssize_t len;
438 char buffer[1024];
439
440 len = read(s->fd, buffer, sizeof(buffer));
441 if (len == -1 && errno == EINTR) {
442 continue;
443 }
444
445 if (len == -1 || len == 0) {
446 fprintf(stderr, "Broken pipe\n");
447 abort();
448 }
449
450 g_string_append_len(s->rx, buffer, len);
451 }
452
453 offset = eol - s->rx->str;
454 line = g_string_new_len(s->rx->str, offset);
455 g_string_erase(s->rx, 0, offset + 1);
456
457 return line;
458}
459
460static gchar **qtest_rsp(QTestState *s, int expected_args)
461{
462 GString *line;
463 gchar **words;
464 int i;
465
466redo:
467 line = qtest_recv_line(s);
468 words = g_strsplit(line->str, " ", 0);
469 g_string_free(line, TRUE);
470
471 if (strcmp(words[0], "IRQ") == 0) {
472 long irq;
473 int ret;
474
475 g_assert(words[1] != NULL);
476 g_assert(words[2] != NULL);
477
478 ret = qemu_strtol(words[2], NULL, 0, &irq);
479 g_assert(!ret);
480 g_assert_cmpint(irq, >=, 0);
481 g_assert_cmpint(irq, <, MAX_IRQ);
482
483 if (strcmp(words[1], "raise") == 0) {
484 s->irq_level[irq] = true;
485 } else {
486 s->irq_level[irq] = false;
487 }
488
489 g_strfreev(words);
490 goto redo;
491 }
492
493 g_assert(words[0] != NULL);
494 g_assert_cmpstr(words[0], ==, "OK");
495
496 if (expected_args) {
497 for (i = 0; i < expected_args; i++) {
498 g_assert(words[i] != NULL);
499 }
500 } else {
501 g_strfreev(words);
502 }
503
504 return words;
505}
506
507static int qtest_query_target_endianness(QTestState *s)
508{
509 gchar **args;
510 int big_endian;
511
512 qtest_sendf(s, "endianness\n");
513 args = qtest_rsp(s, 1);
514 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
515 big_endian = strcmp(args[1], "big") == 0;
516 g_strfreev(args);
517
518 return big_endian;
519}
520
521typedef struct {
522 JSONMessageParser parser;
523 QDict *response;
524} QMPResponseParser;
525
526static void qmp_response(void *opaque, QObject *obj, Error *err)
527{
528 QMPResponseParser *qmp = opaque;
529
530 assert(!obj != !err);
531
532 if (err) {
533 error_prepend(&err, "QMP JSON response parsing failed: ");
534 error_report_err(err);
535 abort();
536 }
537
538 g_assert(!qmp->response);
539 qmp->response = qobject_to(QDict, obj);
540 g_assert(qmp->response);
541}
542
543QDict *qmp_fd_receive(int fd)
544{
545 QMPResponseParser qmp;
546 bool log = getenv("QTEST_LOG") != NULL;
547
548 qmp.response = NULL;
549 json_message_parser_init(&qmp.parser, qmp_response, &qmp, NULL);
550 while (!qmp.response) {
551 ssize_t len;
552 char c;
553
554 len = read(fd, &c, 1);
555 if (len == -1 && errno == EINTR) {
556 continue;
557 }
558
559 if (len == -1 || len == 0) {
560 fprintf(stderr, "Broken pipe\n");
561 abort();
562 }
563
564 if (log) {
565 len = write(2, &c, 1);
566 }
567 json_message_parser_feed(&qmp.parser, &c, 1);
568 }
569 json_message_parser_destroy(&qmp.parser);
570
571 return qmp.response;
572}
573
574QDict *qtest_qmp_receive(QTestState *s)
575{
576 return qmp_fd_receive(s->qmp_fd);
577}
578
579
580
581
582
583
584void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num,
585 const char *fmt, va_list ap)
586{
587 QObject *qobj;
588
589
590 qobj = qobject_from_vjsonf_nofail(fmt, ap);
591
592
593 if (qobj) {
594 int log = getenv("QTEST_LOG") != NULL;
595 QString *qstr = qobject_to_json(qobj);
596 const char *str;
597
598
599
600
601
602 qstring_append_chr(qstr, '\n');
603 str = qstring_get_str(qstr);
604
605 if (log) {
606 fprintf(stderr, "%s", str);
607 }
608
609 if (fds && fds_num > 0) {
610 socket_send_fds(fd, fds, fds_num, str, qstring_get_length(qstr));
611 } else {
612 socket_send(fd, str, qstring_get_length(qstr));
613 }
614
615 qobject_unref(qstr);
616 qobject_unref(qobj);
617 }
618}
619
620void qmp_fd_vsend(int fd, const char *fmt, va_list ap)
621{
622 qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
623}
624
625void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
626 const char *fmt, va_list ap)
627{
628 qmp_fd_vsend_fds(s->qmp_fd, fds, fds_num, fmt, ap);
629}
630
631void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
632{
633 qmp_fd_vsend_fds(s->qmp_fd, NULL, 0, fmt, ap);
634}
635
636QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
637{
638 qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
639
640 return qmp_fd_receive(fd);
641}
642
643QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
644 const char *fmt, va_list ap)
645{
646 qtest_qmp_vsend_fds(s, fds, fds_num, fmt, ap);
647
648
649 return qtest_qmp_receive(s);
650}
651
652QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
653{
654 qtest_qmp_vsend(s, fmt, ap);
655
656
657 return qtest_qmp_receive(s);
658}
659
660QDict *qmp_fd(int fd, const char *fmt, ...)
661{
662 va_list ap;
663 QDict *response;
664
665 va_start(ap, fmt);
666 response = qmp_fdv(fd, fmt, ap);
667 va_end(ap);
668 return response;
669}
670
671void qmp_fd_send(int fd, const char *fmt, ...)
672{
673 va_list ap;
674
675 va_start(ap, fmt);
676 qmp_fd_vsend(fd, fmt, ap);
677 va_end(ap);
678}
679
680QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
681 const char *fmt, ...)
682{
683 va_list ap;
684 QDict *response;
685
686 va_start(ap, fmt);
687 response = qtest_vqmp_fds(s, fds, fds_num, fmt, ap);
688 va_end(ap);
689 return response;
690}
691
692QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
693{
694 va_list ap;
695 QDict *response;
696
697 va_start(ap, fmt);
698 response = qtest_vqmp(s, fmt, ap);
699 va_end(ap);
700 return response;
701}
702
703void qtest_qmp_send(QTestState *s, const char *fmt, ...)
704{
705 va_list ap;
706
707 va_start(ap, fmt);
708 qtest_qmp_vsend(s, fmt, ap);
709 va_end(ap);
710}
711
712void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap)
713{
714 bool log = getenv("QTEST_LOG") != NULL;
715 char *str = g_strdup_vprintf(fmt, ap);
716
717 if (log) {
718 fprintf(stderr, "%s", str);
719 }
720 socket_send(fd, str, strlen(str));
721 g_free(str);
722}
723
724void qmp_fd_send_raw(int fd, const char *fmt, ...)
725{
726 va_list ap;
727
728 va_start(ap, fmt);
729 qmp_fd_vsend_raw(fd, fmt, ap);
730 va_end(ap);
731}
732
733void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
734{
735 va_list ap;
736
737 va_start(ap, fmt);
738 qmp_fd_vsend_raw(s->qmp_fd, fmt, ap);
739 va_end(ap);
740}
741
742QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
743{
744 QDict *response;
745
746 for (;;) {
747 response = qtest_qmp_receive(s);
748 if ((qdict_haskey(response, "event")) &&
749 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
750 return response;
751 }
752 qobject_unref(response);
753 }
754}
755
756void qtest_qmp_eventwait(QTestState *s, const char *event)
757{
758 QDict *response;
759
760 response = qtest_qmp_eventwait_ref(s, event);
761 qobject_unref(response);
762}
763
764char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
765{
766 char *cmd;
767 QDict *resp;
768 char *ret;
769
770 cmd = g_strdup_vprintf(fmt, ap);
771 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
772 " 'arguments': {'command-line': %s}}",
773 cmd);
774 ret = g_strdup(qdict_get_try_str(resp, "return"));
775 while (ret == NULL && qdict_get_try_str(resp, "event")) {
776
777 qobject_unref(resp);
778 resp = qtest_qmp_receive(s);
779 ret = g_strdup(qdict_get_try_str(resp, "return"));
780 }
781 g_assert(ret);
782 qobject_unref(resp);
783 g_free(cmd);
784 return ret;
785}
786
787char *qtest_hmp(QTestState *s, const char *fmt, ...)
788{
789 va_list ap;
790 char *ret;
791
792 va_start(ap, fmt);
793 ret = qtest_vhmp(s, fmt, ap);
794 va_end(ap);
795 return ret;
796}
797
798const char *qtest_get_arch(void)
799{
800 const char *qemu = qtest_qemu_binary();
801 const char *end = strrchr(qemu, '/');
802
803 return end + strlen("/qemu-system-");
804}
805
806bool qtest_get_irq(QTestState *s, int num)
807{
808
809 qtest_inb(s, 0);
810
811 return s->irq_level[num];
812}
813
814static int64_t qtest_clock_rsp(QTestState *s)
815{
816 gchar **words;
817 int64_t clock;
818 words = qtest_rsp(s, 2);
819 clock = g_ascii_strtoll(words[1], NULL, 0);
820 g_strfreev(words);
821 return clock;
822}
823
824int64_t qtest_clock_step_next(QTestState *s)
825{
826 qtest_sendf(s, "clock_step\n");
827 return qtest_clock_rsp(s);
828}
829
830int64_t qtest_clock_step(QTestState *s, int64_t step)
831{
832 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
833 return qtest_clock_rsp(s);
834}
835
836int64_t qtest_clock_set(QTestState *s, int64_t val)
837{
838 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
839 return qtest_clock_rsp(s);
840}
841
842void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
843{
844 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
845 qtest_rsp(s, 0);
846}
847
848void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
849{
850 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
851 qtest_rsp(s, 0);
852}
853
854void qtest_set_irq_in(QTestState *s, const char *qom_path, const char *name,
855 int num, int level)
856{
857 if (!name) {
858 name = "unnamed-gpio-in";
859 }
860 qtest_sendf(s, "set_irq_in %s %s %d %d\n", qom_path, name, num, level);
861 qtest_rsp(s, 0);
862}
863
864static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
865{
866 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
867 qtest_rsp(s, 0);
868}
869
870void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
871{
872 qtest_out(s, "outb", addr, value);
873}
874
875void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
876{
877 qtest_out(s, "outw", addr, value);
878}
879
880void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
881{
882 qtest_out(s, "outl", addr, value);
883}
884
885static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
886{
887 gchar **args;
888 int ret;
889 unsigned long value;
890
891 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
892 args = qtest_rsp(s, 2);
893 ret = qemu_strtoul(args[1], NULL, 0, &value);
894 g_assert(!ret && value <= UINT32_MAX);
895 g_strfreev(args);
896
897 return value;
898}
899
900uint8_t qtest_inb(QTestState *s, uint16_t addr)
901{
902 return qtest_in(s, "inb", addr);
903}
904
905uint16_t qtest_inw(QTestState *s, uint16_t addr)
906{
907 return qtest_in(s, "inw", addr);
908}
909
910uint32_t qtest_inl(QTestState *s, uint16_t addr)
911{
912 return qtest_in(s, "inl", addr);
913}
914
915static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
916 uint64_t value)
917{
918 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
919 qtest_rsp(s, 0);
920}
921
922void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
923{
924 qtest_write(s, "writeb", addr, value);
925}
926
927void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
928{
929 qtest_write(s, "writew", addr, value);
930}
931
932void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
933{
934 qtest_write(s, "writel", addr, value);
935}
936
937void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
938{
939 qtest_write(s, "writeq", addr, value);
940}
941
942static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
943{
944 gchar **args;
945 int ret;
946 uint64_t value;
947
948 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
949 args = qtest_rsp(s, 2);
950 ret = qemu_strtou64(args[1], NULL, 0, &value);
951 g_assert(!ret);
952 g_strfreev(args);
953
954 return value;
955}
956
957uint8_t qtest_readb(QTestState *s, uint64_t addr)
958{
959 return qtest_read(s, "readb", addr);
960}
961
962uint16_t qtest_readw(QTestState *s, uint64_t addr)
963{
964 return qtest_read(s, "readw", addr);
965}
966
967uint32_t qtest_readl(QTestState *s, uint64_t addr)
968{
969 return qtest_read(s, "readl", addr);
970}
971
972uint64_t qtest_readq(QTestState *s, uint64_t addr)
973{
974 return qtest_read(s, "readq", addr);
975}
976
977static int hex2nib(char ch)
978{
979 if (ch >= '0' && ch <= '9') {
980 return ch - '0';
981 } else if (ch >= 'a' && ch <= 'f') {
982 return 10 + (ch - 'a');
983 } else if (ch >= 'A' && ch <= 'F') {
984 return 10 + (ch - 'a');
985 } else {
986 return -1;
987 }
988}
989
990void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
991{
992 uint8_t *ptr = data;
993 gchar **args;
994 size_t i;
995
996 if (!size) {
997 return;
998 }
999
1000 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
1001 args = qtest_rsp(s, 2);
1002
1003 for (i = 0; i < size; i++) {
1004 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
1005 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
1006 }
1007
1008 g_strfreev(args);
1009}
1010
1011uint64_t qtest_rtas_call(QTestState *s, const char *name,
1012 uint32_t nargs, uint64_t args,
1013 uint32_t nret, uint64_t ret)
1014{
1015 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
1016 name, nargs, args, nret, ret);
1017 qtest_rsp(s, 0);
1018 return 0;
1019}
1020
1021void qtest_add_func(const char *str, void (*fn)(void))
1022{
1023 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1024 g_test_add_func(path, fn);
1025 g_free(path);
1026}
1027
1028void qtest_add_data_func_full(const char *str, void *data,
1029 void (*fn)(const void *),
1030 GDestroyNotify data_free_func)
1031{
1032 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1033 g_test_add_data_func_full(path, data, fn, data_free_func);
1034 g_free(path);
1035}
1036
1037void qtest_add_data_func(const char *str, const void *data,
1038 void (*fn)(const void *))
1039{
1040 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1041 g_test_add_data_func(path, data, fn);
1042 g_free(path);
1043}
1044
1045void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
1046{
1047 gchar *bdata;
1048
1049 bdata = g_base64_encode(data, size);
1050 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
1051 socket_send(s->fd, bdata, strlen(bdata));
1052 socket_send(s->fd, "\n", 1);
1053 qtest_rsp(s, 0);
1054 g_free(bdata);
1055}
1056
1057void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
1058{
1059 gchar **args;
1060 size_t len;
1061
1062 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
1063 args = qtest_rsp(s, 2);
1064
1065 g_base64_decode_inplace(args[1], &len);
1066 if (size != len) {
1067 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
1068 size, len);
1069 len = MIN(len, size);
1070 }
1071
1072 memcpy(data, args[1], len);
1073 g_strfreev(args);
1074}
1075
1076void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
1077{
1078 const uint8_t *ptr = data;
1079 size_t i;
1080 char *enc;
1081
1082 if (!size) {
1083 return;
1084 }
1085
1086 enc = g_malloc(2 * size + 1);
1087
1088 for (i = 0; i < size; i++) {
1089 sprintf(&enc[i * 2], "%02x", ptr[i]);
1090 }
1091
1092 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
1093 qtest_rsp(s, 0);
1094 g_free(enc);
1095}
1096
1097void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
1098{
1099 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
1100 qtest_rsp(s, 0);
1101}
1102
1103QDict *qmp(const char *fmt, ...)
1104{
1105 va_list ap;
1106 QDict *response;
1107
1108 va_start(ap, fmt);
1109 response = qtest_vqmp(global_qtest, fmt, ap);
1110 va_end(ap);
1111 return response;
1112}
1113
1114void qmp_assert_success(const char *fmt, ...)
1115{
1116 va_list ap;
1117 QDict *response;
1118
1119 va_start(ap, fmt);
1120 response = qtest_vqmp(global_qtest, fmt, ap);
1121 va_end(ap);
1122
1123 g_assert(response);
1124 if (!qdict_haskey(response, "return")) {
1125 QString *s = qobject_to_json_pretty(QOBJECT(response));
1126 g_test_message("%s", qstring_get_str(s));
1127 qobject_unref(s);
1128 }
1129 g_assert(qdict_haskey(response, "return"));
1130 qobject_unref(response);
1131}
1132
1133char *hmp(const char *fmt, ...)
1134{
1135 va_list ap;
1136 char *ret;
1137
1138 va_start(ap, fmt);
1139 ret = qtest_vhmp(global_qtest, fmt, ap);
1140 va_end(ap);
1141 return ret;
1142}
1143
1144bool qtest_big_endian(QTestState *s)
1145{
1146 return s->big_endian;
1147}
1148
1149static bool qtest_check_machine_version(const char *mname, const char *basename,
1150 int major, int minor)
1151{
1152 char *newname;
1153 bool is_equal;
1154
1155 newname = g_strdup_printf("%s-%i.%i", basename, major, minor);
1156 is_equal = g_str_equal(mname, newname);
1157 g_free(newname);
1158
1159 return is_equal;
1160}
1161
1162static bool qtest_is_old_versioned_machine(const char *mname)
1163{
1164 const char *dash = strrchr(mname, '-');
1165 const char *dot = strrchr(mname, '.');
1166 const char *chr;
1167 char *bname;
1168 const int major = QEMU_VERSION_MAJOR;
1169 const int minor = QEMU_VERSION_MINOR;
1170 bool res = false;
1171
1172 if (dash && dot && dot > dash) {
1173 for (chr = dash + 1; *chr; chr++) {
1174 if (!qemu_isdigit(*chr) && *chr != '.') {
1175 return false;
1176 }
1177 }
1178
1179
1180
1181
1182
1183 bname = g_strdup(mname);
1184 bname[dash - mname] = 0;
1185 res = !qtest_check_machine_version(mname, bname, major + 1, 0) &&
1186 !qtest_check_machine_version(mname, bname, major, minor + 1) &&
1187 !qtest_check_machine_version(mname, bname, major, minor);
1188 g_free(bname);
1189 }
1190
1191 return res;
1192}
1193
1194void qtest_cb_for_every_machine(void (*cb)(const char *machine),
1195 bool skip_old_versioned)
1196{
1197 QDict *response, *minfo;
1198 QList *list;
1199 const QListEntry *p;
1200 QObject *qobj;
1201 QString *qstr;
1202 const char *mname;
1203
1204 qtest_start("-machine none");
1205 response = qmp("{ 'execute': 'query-machines' }");
1206 g_assert(response);
1207 list = qdict_get_qlist(response, "return");
1208 g_assert(list);
1209
1210 for (p = qlist_first(list); p; p = qlist_next(p)) {
1211 minfo = qobject_to(QDict, qlist_entry_obj(p));
1212 g_assert(minfo);
1213 qobj = qdict_get(minfo, "name");
1214 g_assert(qobj);
1215 qstr = qobject_to(QString, qobj);
1216 g_assert(qstr);
1217 mname = qstring_get_str(qstr);
1218 if (!skip_old_versioned || !qtest_is_old_versioned_machine(mname)) {
1219 cb(mname);
1220 }
1221 }
1222
1223 qtest_end();
1224 qobject_unref(response);
1225}
1226
1227QDict *qtest_qmp_receive_success(QTestState *s,
1228 void (*event_cb)(void *opaque,
1229 const char *event,
1230 QDict *data),
1231 void *opaque)
1232{
1233 QDict *response, *ret, *data;
1234 const char *event;
1235
1236 for (;;) {
1237 response = qtest_qmp_receive(s);
1238 g_assert(!qdict_haskey(response, "error"));
1239 ret = qdict_get_qdict(response, "return");
1240 if (ret) {
1241 break;
1242 }
1243 event = qdict_get_str(response, "event");
1244 data = qdict_get_qdict(response, "data");
1245 if (event_cb) {
1246 event_cb(opaque, event, data);
1247 }
1248 qobject_unref(response);
1249 }
1250
1251 qobject_ref(ret);
1252 qobject_unref(response);
1253 return ret;
1254}
1255
1256
1257
1258
1259void qtest_qmp_device_add(const char *driver, const char *id,
1260 const char *fmt, ...)
1261{
1262 QDict *args, *response;
1263 va_list ap;
1264
1265 va_start(ap, fmt);
1266 args = qdict_from_vjsonf_nofail(fmt, ap);
1267 va_end(ap);
1268
1269 g_assert(!qdict_haskey(args, "driver") && !qdict_haskey(args, "id"));
1270 qdict_put_str(args, "driver", driver);
1271 qdict_put_str(args, "id", id);
1272
1273 response = qmp("{'execute': 'device_add', 'arguments': %p}", args);
1274 g_assert(response);
1275 g_assert(!qdict_haskey(response, "event"));
1276 g_assert(!qdict_haskey(response, "error"));
1277 qobject_unref(response);
1278}
1279
1280static void device_deleted_cb(void *opaque, const char *name, QDict *data)
1281{
1282 bool *got_event = opaque;
1283
1284 g_assert_cmpstr(name, ==, "DEVICE_DELETED");
1285 *got_event = true;
1286}
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306void qtest_qmp_device_del(const char *id)
1307{
1308 bool got_event = false;
1309 QDict *rsp;
1310
1311 qtest_qmp_send(global_qtest,
1312 "{'execute': 'device_del', 'arguments': {'id': %s}}",
1313 id);
1314 rsp = qtest_qmp_receive_success(global_qtest, device_deleted_cb,
1315 &got_event);
1316 qobject_unref(rsp);
1317 if (!got_event) {
1318 rsp = qtest_qmp_receive(global_qtest);
1319 g_assert_cmpstr(qdict_get_try_str(rsp, "event"),
1320 ==, "DEVICE_DELETED");
1321 qobject_unref(rsp);
1322 }
1323}
1324
1325bool qmp_rsp_is_err(QDict *rsp)
1326{
1327 QDict *error = qdict_get_qdict(rsp, "error");
1328 qobject_unref(rsp);
1329 return !!error;
1330}
1331
1332void qmp_assert_error_class(QDict *rsp, const char *class)
1333{
1334 QDict *error = qdict_get_qdict(rsp, "error");
1335
1336 g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class);
1337 g_assert_nonnull(qdict_get_try_str(error, "desc"));
1338 g_assert(!qdict_haskey(rsp, "return"));
1339
1340 qobject_unref(rsp);
1341}
1342