]>
Commit | Line | Data |
---|---|---|
49ee3590 AL |
1 | /* |
2 | * QTest | |
3 | * | |
4 | * Copyright IBM, Corp. 2012 | |
5 | * Copyright Red Hat, Inc. 2012 | |
6 | * | |
7 | * Authors: | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
9 | * Paolo Bonzini <pbonzini@redhat.com> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
12 | * See the COPYING file in the top-level directory. | |
13 | * | |
14 | */ | |
15 | #include "libqtest.h" | |
16 | ||
17 | #include <glib.h> | |
18 | #include <sys/types.h> | |
19 | #include <sys/socket.h> | |
20 | #include <sys/wait.h> | |
21 | #include <sys/un.h> | |
22 | #include <inttypes.h> | |
23 | #include <errno.h> | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include <string.h> | |
28 | ||
ea7697fc | 29 | #include "compiler.h" |
49ee3590 AL |
30 | #include "osdep.h" |
31 | ||
32 | #define MAX_IRQ 256 | |
33 | ||
34 | QTestState *global_qtest; | |
35 | ||
36 | struct QTestState | |
37 | { | |
38 | int fd; | |
39 | bool irq_level[MAX_IRQ]; | |
40 | GString *rx; | |
41 | gchar *pid_file; | |
42 | }; | |
43 | ||
44 | #define g_assert_no_errno(ret) do { \ | |
45 | g_assert_cmpint(ret, !=, -1); \ | |
46 | } while (0) | |
47 | ||
48 | QTestState *qtest_init(const char *extra_args) | |
49 | { | |
50 | QTestState *s; | |
51 | struct sockaddr_un addr; | |
52 | int sock, ret, i; | |
53 | gchar *socket_path; | |
54 | gchar *pid_file; | |
55 | gchar *command; | |
56 | const char *qemu_binary; | |
57 | pid_t pid; | |
58 | socklen_t addrlen; | |
59 | ||
60 | qemu_binary = getenv("QTEST_QEMU_BINARY"); | |
61 | g_assert(qemu_binary != NULL); | |
62 | ||
63 | socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid()); | |
64 | pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid()); | |
65 | ||
66 | s = g_malloc(sizeof(*s)); | |
67 | ||
68 | sock = socket(PF_UNIX, SOCK_STREAM, 0); | |
69 | g_assert_no_errno(sock); | |
70 | ||
71 | addr.sun_family = AF_UNIX; | |
72 | snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path); | |
73 | qemu_set_cloexec(sock); | |
74 | ||
75 | do { | |
76 | ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr)); | |
77 | } while (ret == -1 && errno == EINTR); | |
78 | g_assert_no_errno(ret); | |
79 | listen(sock, 1); | |
80 | ||
81 | pid = fork(); | |
82 | if (pid == 0) { | |
83 | command = g_strdup_printf("%s " | |
84 | "-qtest unix:%s,nowait " | |
85 | "-qtest-log /dev/null " | |
86 | "-pidfile %s " | |
87 | "-machine accel=qtest " | |
88 | "%s", qemu_binary, socket_path, | |
89 | pid_file, | |
90 | extra_args ?: ""); | |
91 | ||
92 | ret = system(command); | |
93 | exit(ret); | |
94 | g_free(command); | |
95 | } | |
96 | ||
97 | do { | |
98 | ret = accept(sock, (struct sockaddr *)&addr, &addrlen); | |
99 | } while (ret == -1 && errno == EINTR); | |
100 | g_assert_no_errno(ret); | |
101 | close(sock); | |
102 | ||
103 | s->fd = ret; | |
104 | s->rx = g_string_new(""); | |
105 | s->pid_file = pid_file; | |
106 | for (i = 0; i < MAX_IRQ; i++) { | |
107 | s->irq_level[i] = false; | |
108 | } | |
109 | ||
110 | g_free(socket_path); | |
111 | ||
112 | return s; | |
113 | } | |
114 | ||
115 | void qtest_quit(QTestState *s) | |
116 | { | |
117 | FILE *f; | |
118 | char buffer[1024]; | |
119 | ||
120 | f = fopen(s->pid_file, "r"); | |
121 | if (f) { | |
122 | if (fgets(buffer, sizeof(buffer), f)) { | |
123 | pid_t pid = atoi(buffer); | |
124 | int status = 0; | |
125 | ||
126 | kill(pid, SIGTERM); | |
127 | waitpid(pid, &status, 0); | |
128 | } | |
129 | ||
130 | fclose(f); | |
131 | } | |
132 | } | |
133 | ||
ea7697fc | 134 | static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...) |
49ee3590 AL |
135 | { |
136 | va_list ap; | |
137 | gchar *str; | |
138 | size_t size, offset; | |
139 | ||
140 | va_start(ap, fmt); | |
141 | str = g_strdup_vprintf(fmt, ap); | |
142 | va_end(ap); | |
143 | size = strlen(str); | |
144 | ||
145 | offset = 0; | |
146 | while (offset < size) { | |
147 | ssize_t len; | |
148 | ||
149 | len = write(s->fd, str + offset, size - offset); | |
150 | if (len == -1 && errno == EINTR) { | |
151 | continue; | |
152 | } | |
153 | ||
154 | g_assert_no_errno(len); | |
155 | g_assert_cmpint(len, >, 0); | |
156 | ||
157 | offset += len; | |
158 | } | |
159 | } | |
160 | ||
161 | static GString *qtest_recv_line(QTestState *s) | |
162 | { | |
163 | GString *line; | |
164 | size_t offset; | |
165 | char *eol; | |
166 | ||
167 | while ((eol = strchr(s->rx->str, '\n')) == NULL) { | |
168 | ssize_t len; | |
169 | char buffer[1024]; | |
170 | ||
171 | len = read(s->fd, buffer, sizeof(buffer)); | |
172 | if (len == -1 && errno == EINTR) { | |
173 | continue; | |
174 | } | |
175 | ||
176 | if (len == -1 || len == 0) { | |
177 | fprintf(stderr, "Broken pipe\n"); | |
178 | exit(1); | |
179 | } | |
180 | ||
181 | g_string_append_len(s->rx, buffer, len); | |
182 | } | |
183 | ||
184 | offset = eol - s->rx->str; | |
185 | line = g_string_new_len(s->rx->str, offset); | |
186 | g_string_erase(s->rx, 0, offset + 1); | |
187 | ||
188 | return line; | |
189 | } | |
190 | ||
191 | static gchar **qtest_rsp(QTestState *s, int expected_args) | |
192 | { | |
193 | GString *line; | |
194 | gchar **words; | |
195 | int i; | |
196 | ||
197 | redo: | |
198 | line = qtest_recv_line(s); | |
199 | words = g_strsplit(line->str, " ", 0); | |
200 | g_string_free(line, TRUE); | |
201 | ||
202 | if (strcmp(words[0], "IRQ") == 0) { | |
203 | int irq; | |
204 | ||
205 | g_assert(words[1] != NULL); | |
206 | g_assert(words[2] != NULL); | |
207 | ||
208 | irq = strtoul(words[2], NULL, 0); | |
209 | g_assert_cmpint(irq, >=, 0); | |
210 | g_assert_cmpint(irq, <, MAX_IRQ); | |
211 | ||
212 | if (strcmp(words[1], "raise") == 0) { | |
213 | s->irq_level[irq] = true; | |
214 | } else { | |
215 | s->irq_level[irq] = false; | |
216 | } | |
217 | ||
218 | g_strfreev(words); | |
219 | goto redo; | |
220 | } | |
221 | ||
222 | g_assert(words[0] != NULL); | |
223 | g_assert_cmpstr(words[0], ==, "OK"); | |
224 | ||
225 | if (expected_args) { | |
226 | for (i = 0; i < expected_args; i++) { | |
227 | g_assert(words[i] != NULL); | |
228 | } | |
229 | } else { | |
230 | g_strfreev(words); | |
231 | } | |
232 | ||
233 | return words; | |
234 | } | |
235 | ||
236 | const char *qtest_get_arch(void) | |
237 | { | |
238 | const char *qemu = getenv("QTEST_QEMU_BINARY"); | |
239 | const char *end = strrchr(qemu, '/'); | |
240 | ||
241 | return end + strlen("/qemu-system-"); | |
242 | } | |
243 | ||
244 | bool qtest_get_irq(QTestState *s, int num) | |
245 | { | |
246 | /* dummy operation in order to make sure irq is up to date */ | |
247 | qtest_inb(s, 0); | |
248 | ||
249 | return s->irq_level[num]; | |
250 | } | |
251 | ||
252 | static int64_t qtest_clock_rsp(QTestState *s) | |
253 | { | |
254 | gchar **words; | |
255 | int64_t clock; | |
256 | words = qtest_rsp(s, 2); | |
257 | clock = g_ascii_strtoll(words[1], NULL, 0); | |
258 | g_strfreev(words); | |
259 | return clock; | |
260 | } | |
261 | ||
262 | int64_t qtest_clock_step_next(QTestState *s) | |
263 | { | |
264 | qtest_sendf(s, "clock_step\n"); | |
265 | return qtest_clock_rsp(s); | |
266 | } | |
267 | ||
268 | int64_t qtest_clock_step(QTestState *s, int64_t step) | |
269 | { | |
270 | qtest_sendf(s, "clock_step %"PRIi64"\n", step); | |
271 | return qtest_clock_rsp(s); | |
272 | } | |
273 | ||
274 | int64_t qtest_clock_set(QTestState *s, int64_t val) | |
275 | { | |
276 | qtest_sendf(s, "clock_set %"PRIi64"\n", val); | |
277 | return qtest_clock_rsp(s); | |
278 | } | |
279 | ||
280 | void qtest_irq_intercept_out(QTestState *s, const char *qom_path) | |
281 | { | |
282 | qtest_sendf(s, "irq_intercept_out %s\n", qom_path); | |
283 | qtest_rsp(s, 0); | |
284 | } | |
285 | ||
286 | void qtest_irq_intercept_in(QTestState *s, const char *qom_path) | |
287 | { | |
288 | qtest_sendf(s, "irq_intercept_in %s\n", qom_path); | |
289 | qtest_rsp(s, 0); | |
290 | } | |
291 | ||
292 | static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value) | |
293 | { | |
294 | qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value); | |
295 | qtest_rsp(s, 0); | |
296 | } | |
297 | ||
298 | void qtest_outb(QTestState *s, uint16_t addr, uint8_t value) | |
299 | { | |
300 | qtest_out(s, "outb", addr, value); | |
301 | } | |
302 | ||
303 | void qtest_outw(QTestState *s, uint16_t addr, uint16_t value) | |
304 | { | |
305 | qtest_out(s, "outw", addr, value); | |
306 | } | |
307 | ||
308 | void qtest_outl(QTestState *s, uint16_t addr, uint32_t value) | |
309 | { | |
310 | qtest_out(s, "outl", addr, value); | |
311 | } | |
312 | ||
313 | static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr) | |
314 | { | |
315 | gchar **args; | |
316 | uint32_t value; | |
317 | ||
318 | qtest_sendf(s, "%s 0x%x\n", cmd, addr); | |
319 | args = qtest_rsp(s, 2); | |
320 | value = strtoul(args[1], NULL, 0); | |
321 | g_strfreev(args); | |
322 | ||
323 | return value; | |
324 | } | |
325 | ||
326 | uint8_t qtest_inb(QTestState *s, uint16_t addr) | |
327 | { | |
328 | return qtest_in(s, "inb", addr); | |
329 | } | |
330 | ||
331 | uint16_t qtest_inw(QTestState *s, uint16_t addr) | |
332 | { | |
333 | return qtest_in(s, "inw", addr); | |
334 | } | |
335 | ||
336 | uint32_t qtest_inl(QTestState *s, uint16_t addr) | |
337 | { | |
338 | return qtest_in(s, "inl", addr); | |
339 | } | |
340 | ||
341 | static int hex2nib(char ch) | |
342 | { | |
343 | if (ch >= '0' && ch <= '9') { | |
344 | return ch - '0'; | |
345 | } else if (ch >= 'a' && ch <= 'f') { | |
346 | return 10 + (ch - 'a'); | |
347 | } else if (ch >= 'A' && ch <= 'F') { | |
348 | return 10 + (ch - 'a'); | |
349 | } else { | |
350 | return -1; | |
351 | } | |
352 | } | |
353 | ||
354 | void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size) | |
355 | { | |
356 | uint8_t *ptr = data; | |
357 | gchar **args; | |
358 | size_t i; | |
359 | ||
ea7697fc | 360 | qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size); |
49ee3590 AL |
361 | args = qtest_rsp(s, 2); |
362 | ||
363 | for (i = 0; i < size; i++) { | |
364 | ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4; | |
365 | ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]); | |
366 | } | |
367 | ||
368 | g_strfreev(args); | |
369 | } | |
370 | ||
371 | void qtest_add_func(const char *str, void (*fn)) | |
372 | { | |
373 | gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str); | |
374 | g_test_add_func(path, fn); | |
375 | } | |
376 | ||
377 | void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size) | |
378 | { | |
379 | const uint8_t *ptr = data; | |
380 | size_t i; | |
381 | ||
ea7697fc | 382 | qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size); |
49ee3590 AL |
383 | for (i = 0; i < size; i++) { |
384 | qtest_sendf(s, "%02x", ptr[i]); | |
385 | } | |
386 | qtest_sendf(s, "\n"); | |
387 | qtest_rsp(s, 0); | |
388 | } |