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