]>
Commit | Line | Data |
---|---|---|
c7f0f3b1 AL |
1 | /* |
2 | * Test Server | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
d38ea87a | 14 | #include "qemu/osdep.h" |
da34e65c | 15 | #include "qapi/error.h" |
33c11879 PB |
16 | #include "qemu-common.h" |
17 | #include "cpu.h" | |
9c17d615 | 18 | #include "sysemu/qtest.h" |
20288345 | 19 | #include "hw/qdev.h" |
dccfcd0e | 20 | #include "sysemu/char.h" |
022c62cb PB |
21 | #include "exec/ioport.h" |
22 | #include "exec/memory.h" | |
c7f0f3b1 | 23 | #include "hw/irq.h" |
3a6ce514 | 24 | #include "sysemu/accel.h" |
9c17d615 PB |
25 | #include "sysemu/sysemu.h" |
26 | #include "sysemu/cpus.h" | |
1ad9580b ST |
27 | #include "qemu/config-file.h" |
28 | #include "qemu/option.h" | |
29 | #include "qemu/error-report.h" | |
c7f0f3b1 AL |
30 | |
31 | #define MAX_IRQ 256 | |
32 | ||
d5286af5 | 33 | bool qtest_allowed; |
c7f0f3b1 | 34 | |
20288345 | 35 | static DeviceState *irq_intercept_dev; |
c7f0f3b1 AL |
36 | static FILE *qtest_log_fp; |
37 | static CharDriverState *qtest_chr; | |
38 | static GString *inbuf; | |
39 | static int irq_levels[MAX_IRQ]; | |
6e92466a | 40 | static qemu_timeval start_time; |
c7f0f3b1 AL |
41 | static bool qtest_opened; |
42 | ||
6b7cff76 | 43 | #define FMT_timeval "%ld.%06ld" |
c7f0f3b1 AL |
44 | |
45 | /** | |
46 | * QTest Protocol | |
47 | * | |
48 | * Line based protocol, request/response based. Server can send async messages | |
49 | * so clients should always handle many async messages before the response | |
50 | * comes in. | |
51 | * | |
52 | * Valid requests | |
53 | * | |
8156be56 PB |
54 | * Clock management: |
55 | * | |
bc72ad67 | 56 | * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands |
8156be56 PB |
57 | * let you adjust the value of the clock (monotonically). All the commands |
58 | * return the current value of the clock in nanoseconds. | |
59 | * | |
60 | * > clock_step | |
61 | * < OK VALUE | |
62 | * | |
63 | * Advance the clock to the next deadline. Useful when waiting for | |
64 | * asynchronous events. | |
65 | * | |
66 | * > clock_step NS | |
67 | * < OK VALUE | |
68 | * | |
69 | * Advance the clock by NS nanoseconds. | |
70 | * | |
71 | * > clock_set NS | |
72 | * < OK VALUE | |
73 | * | |
74 | * Advance the clock to NS nanoseconds (do nothing if it's already past). | |
75 | * | |
76 | * PIO and memory access: | |
77 | * | |
c7f0f3b1 AL |
78 | * > outb ADDR VALUE |
79 | * < OK | |
80 | * | |
81 | * > outw ADDR VALUE | |
82 | * < OK | |
83 | * | |
84 | * > outl ADDR VALUE | |
85 | * < OK | |
86 | * | |
87 | * > inb ADDR | |
88 | * < OK VALUE | |
89 | * | |
90 | * > inw ADDR | |
91 | * < OK VALUE | |
92 | * | |
93 | * > inl ADDR | |
94 | * < OK VALUE | |
95 | * | |
872536bf AF |
96 | * > writeb ADDR VALUE |
97 | * < OK | |
98 | * | |
99 | * > writew ADDR VALUE | |
100 | * < OK | |
101 | * | |
102 | * > writel ADDR VALUE | |
103 | * < OK | |
104 | * | |
105 | * > writeq ADDR VALUE | |
106 | * < OK | |
107 | * | |
108 | * > readb ADDR | |
109 | * < OK VALUE | |
110 | * | |
111 | * > readw ADDR | |
112 | * < OK VALUE | |
113 | * | |
114 | * > readl ADDR | |
115 | * < OK VALUE | |
116 | * | |
117 | * > readq ADDR | |
118 | * < OK VALUE | |
119 | * | |
c7f0f3b1 AL |
120 | * > read ADDR SIZE |
121 | * < OK DATA | |
122 | * | |
123 | * > write ADDR SIZE DATA | |
124 | * < OK | |
125 | * | |
7a6a740d JS |
126 | * > b64read ADDR SIZE |
127 | * < OK B64_DATA | |
128 | * | |
129 | * > b64write ADDR SIZE B64_DATA | |
130 | * < OK | |
131 | * | |
4d007963 JS |
132 | * > memset ADDR SIZE VALUE |
133 | * < OK | |
134 | * | |
c7f0f3b1 AL |
135 | * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0. |
136 | * | |
137 | * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller | |
138 | * than the expected size, the value will be zero filled at the end of the data | |
139 | * sequence. | |
140 | * | |
7a6a740d JS |
141 | * B64_DATA is an arbitrarily long base64 encoded string. |
142 | * If the sizes do not match, the data will be truncated. | |
143 | * | |
20288345 PB |
144 | * IRQ management: |
145 | * | |
146 | * > irq_intercept_in QOM-PATH | |
147 | * < OK | |
148 | * | |
149 | * > irq_intercept_out QOM-PATH | |
150 | * < OK | |
151 | * | |
152 | * Attach to the gpio-in (resp. gpio-out) pins exported by the device at | |
153 | * QOM-PATH. When the pin is triggered, one of the following async messages | |
154 | * will be printed to the qtest stream: | |
155 | * | |
156 | * IRQ raise NUM | |
157 | * IRQ lower NUM | |
158 | * | |
159 | * where NUM is an IRQ number. For the PC, interrupts can be intercepted | |
160 | * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with | |
161 | * NUM=0 even though it is remapped to GSI 2). | |
c7f0f3b1 AL |
162 | */ |
163 | ||
164 | static int hex2nib(char ch) | |
165 | { | |
166 | if (ch >= '0' && ch <= '9') { | |
167 | return ch - '0'; | |
168 | } else if (ch >= 'a' && ch <= 'f') { | |
169 | return 10 + (ch - 'a'); | |
170 | } else if (ch >= 'A' && ch <= 'F') { | |
2a802aaf | 171 | return 10 + (ch - 'A'); |
c7f0f3b1 AL |
172 | } else { |
173 | return -1; | |
174 | } | |
175 | } | |
176 | ||
6e92466a | 177 | static void qtest_get_time(qemu_timeval *tv) |
c7f0f3b1 | 178 | { |
6e92466a | 179 | qemu_gettimeofday(tv); |
c7f0f3b1 AL |
180 | tv->tv_sec -= start_time.tv_sec; |
181 | tv->tv_usec -= start_time.tv_usec; | |
182 | if (tv->tv_usec < 0) { | |
183 | tv->tv_usec += 1000000; | |
184 | tv->tv_sec -= 1; | |
185 | } | |
186 | } | |
187 | ||
188 | static void qtest_send_prefix(CharDriverState *chr) | |
189 | { | |
6e92466a | 190 | qemu_timeval tv; |
c7f0f3b1 AL |
191 | |
192 | if (!qtest_log_fp || !qtest_opened) { | |
193 | return; | |
194 | } | |
195 | ||
196 | qtest_get_time(&tv); | |
197 | fprintf(qtest_log_fp, "[S +" FMT_timeval "] ", | |
35aa3fb3 | 198 | (long) tv.tv_sec, (long) tv.tv_usec); |
c7f0f3b1 AL |
199 | } |
200 | ||
7a6a740d JS |
201 | static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...) |
202 | { | |
203 | va_list ap; | |
204 | ||
205 | if (!qtest_log_fp || !qtest_opened) { | |
206 | return; | |
207 | } | |
208 | ||
209 | qtest_send_prefix(NULL); | |
210 | ||
211 | va_start(ap, fmt); | |
212 | vfprintf(qtest_log_fp, fmt, ap); | |
213 | va_end(ap); | |
214 | } | |
215 | ||
332cc7e9 JS |
216 | static void do_qtest_send(CharDriverState *chr, const char *str, size_t len) |
217 | { | |
218 | qemu_chr_fe_write_all(chr, (uint8_t *)str, len); | |
219 | if (qtest_log_fp && qtest_opened) { | |
220 | fprintf(qtest_log_fp, "%s", str); | |
221 | } | |
222 | } | |
223 | ||
224 | static void qtest_send(CharDriverState *chr, const char *str) | |
225 | { | |
226 | do_qtest_send(chr, str, strlen(str)); | |
227 | } | |
228 | ||
229 | static void GCC_FMT_ATTR(2, 3) qtest_sendf(CharDriverState *chr, | |
230 | const char *fmt, ...) | |
c7f0f3b1 AL |
231 | { |
232 | va_list ap; | |
332cc7e9 | 233 | gchar *buffer; |
c7f0f3b1 AL |
234 | |
235 | va_start(ap, fmt); | |
332cc7e9 JS |
236 | buffer = g_strdup_vprintf(fmt, ap); |
237 | qtest_send(chr, buffer); | |
c7f0f3b1 | 238 | va_end(ap); |
c7f0f3b1 AL |
239 | } |
240 | ||
20288345 PB |
241 | static void qtest_irq_handler(void *opaque, int n, int level) |
242 | { | |
60a79016 PC |
243 | qemu_irq old_irq = *(qemu_irq *)opaque; |
244 | qemu_set_irq(old_irq, level); | |
20288345 PB |
245 | |
246 | if (irq_levels[n] != level) { | |
247 | CharDriverState *chr = qtest_chr; | |
248 | irq_levels[n] = level; | |
249 | qtest_send_prefix(chr); | |
332cc7e9 JS |
250 | qtest_sendf(chr, "IRQ %s %d\n", |
251 | level ? "raise" : "lower", n); | |
20288345 PB |
252 | } |
253 | } | |
254 | ||
c7f0f3b1 AL |
255 | static void qtest_process_command(CharDriverState *chr, gchar **words) |
256 | { | |
257 | const gchar *command; | |
258 | ||
259 | g_assert(words); | |
260 | ||
261 | command = words[0]; | |
262 | ||
263 | if (qtest_log_fp) { | |
6e92466a | 264 | qemu_timeval tv; |
c7f0f3b1 AL |
265 | int i; |
266 | ||
267 | qtest_get_time(&tv); | |
268 | fprintf(qtest_log_fp, "[R +" FMT_timeval "]", | |
35aa3fb3 | 269 | (long) tv.tv_sec, (long) tv.tv_usec); |
c7f0f3b1 AL |
270 | for (i = 0; words[i]; i++) { |
271 | fprintf(qtest_log_fp, " %s", words[i]); | |
272 | } | |
273 | fprintf(qtest_log_fp, "\n"); | |
274 | } | |
275 | ||
276 | g_assert(command); | |
20288345 PB |
277 | if (strcmp(words[0], "irq_intercept_out") == 0 |
278 | || strcmp(words[0], "irq_intercept_in") == 0) { | |
a5f54290 PC |
279 | DeviceState *dev; |
280 | NamedGPIOList *ngl; | |
20288345 PB |
281 | |
282 | g_assert(words[1]); | |
283 | dev = DEVICE(object_resolve_path(words[1], NULL)); | |
284 | if (!dev) { | |
285 | qtest_send_prefix(chr); | |
286 | qtest_send(chr, "FAIL Unknown device\n"); | |
287 | return; | |
288 | } | |
289 | ||
290 | if (irq_intercept_dev) { | |
291 | qtest_send_prefix(chr); | |
292 | if (irq_intercept_dev != dev) { | |
293 | qtest_send(chr, "FAIL IRQ intercept already enabled\n"); | |
294 | } else { | |
295 | qtest_send(chr, "OK\n"); | |
296 | } | |
297 | return; | |
298 | } | |
299 | ||
a5f54290 PC |
300 | QLIST_FOREACH(ngl, &dev->gpios, node) { |
301 | /* We don't support intercept of named GPIOs yet */ | |
302 | if (ngl->name) { | |
303 | continue; | |
304 | } | |
305 | if (words[0][14] == 'o') { | |
60a79016 PC |
306 | int i; |
307 | for (i = 0; i < ngl->num_out; ++i) { | |
308 | qemu_irq *disconnected = g_new0(qemu_irq, 1); | |
309 | qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler, | |
310 | disconnected, i); | |
311 | ||
312 | *disconnected = qdev_intercept_gpio_out(dev, icpt, | |
313 | ngl->name, i); | |
314 | } | |
a5f54290 PC |
315 | } else { |
316 | qemu_irq_intercept_in(ngl->in, qtest_irq_handler, | |
317 | ngl->num_in); | |
318 | } | |
20288345 PB |
319 | } |
320 | irq_intercept_dev = dev; | |
321 | qtest_send_prefix(chr); | |
322 | qtest_send(chr, "OK\n"); | |
323 | ||
324 | } else if (strcmp(words[0], "outb") == 0 || | |
325 | strcmp(words[0], "outw") == 0 || | |
326 | strcmp(words[0], "outl") == 0) { | |
c7f0f3b1 AL |
327 | uint16_t addr; |
328 | uint32_t value; | |
329 | ||
330 | g_assert(words[1] && words[2]); | |
56863d4f PM |
331 | addr = strtoul(words[1], NULL, 0); |
332 | value = strtoul(words[2], NULL, 0); | |
c7f0f3b1 AL |
333 | |
334 | if (words[0][3] == 'b') { | |
335 | cpu_outb(addr, value); | |
336 | } else if (words[0][3] == 'w') { | |
337 | cpu_outw(addr, value); | |
338 | } else if (words[0][3] == 'l') { | |
339 | cpu_outl(addr, value); | |
340 | } | |
341 | qtest_send_prefix(chr); | |
342 | qtest_send(chr, "OK\n"); | |
343 | } else if (strcmp(words[0], "inb") == 0 || | |
344 | strcmp(words[0], "inw") == 0 || | |
345 | strcmp(words[0], "inl") == 0) { | |
346 | uint16_t addr; | |
347 | uint32_t value = -1U; | |
348 | ||
349 | g_assert(words[1]); | |
56863d4f | 350 | addr = strtoul(words[1], NULL, 0); |
c7f0f3b1 AL |
351 | |
352 | if (words[0][2] == 'b') { | |
353 | value = cpu_inb(addr); | |
354 | } else if (words[0][2] == 'w') { | |
355 | value = cpu_inw(addr); | |
356 | } else if (words[0][2] == 'l') { | |
357 | value = cpu_inl(addr); | |
358 | } | |
359 | qtest_send_prefix(chr); | |
332cc7e9 | 360 | qtest_sendf(chr, "OK 0x%04x\n", value); |
872536bf AF |
361 | } else if (strcmp(words[0], "writeb") == 0 || |
362 | strcmp(words[0], "writew") == 0 || | |
363 | strcmp(words[0], "writel") == 0 || | |
364 | strcmp(words[0], "writeq") == 0) { | |
365 | uint64_t addr; | |
366 | uint64_t value; | |
367 | ||
368 | g_assert(words[1] && words[2]); | |
369 | addr = strtoull(words[1], NULL, 0); | |
370 | value = strtoull(words[2], NULL, 0); | |
371 | ||
372 | if (words[0][5] == 'b') { | |
373 | uint8_t data = value; | |
374 | cpu_physical_memory_write(addr, &data, 1); | |
375 | } else if (words[0][5] == 'w') { | |
376 | uint16_t data = value; | |
377 | tswap16s(&data); | |
378 | cpu_physical_memory_write(addr, &data, 2); | |
379 | } else if (words[0][5] == 'l') { | |
380 | uint32_t data = value; | |
381 | tswap32s(&data); | |
382 | cpu_physical_memory_write(addr, &data, 4); | |
383 | } else if (words[0][5] == 'q') { | |
384 | uint64_t data = value; | |
385 | tswap64s(&data); | |
386 | cpu_physical_memory_write(addr, &data, 8); | |
387 | } | |
388 | qtest_send_prefix(chr); | |
389 | qtest_send(chr, "OK\n"); | |
390 | } else if (strcmp(words[0], "readb") == 0 || | |
391 | strcmp(words[0], "readw") == 0 || | |
392 | strcmp(words[0], "readl") == 0 || | |
393 | strcmp(words[0], "readq") == 0) { | |
394 | uint64_t addr; | |
395 | uint64_t value = UINT64_C(-1); | |
396 | ||
397 | g_assert(words[1]); | |
398 | addr = strtoull(words[1], NULL, 0); | |
399 | ||
400 | if (words[0][4] == 'b') { | |
401 | uint8_t data; | |
402 | cpu_physical_memory_read(addr, &data, 1); | |
403 | value = data; | |
404 | } else if (words[0][4] == 'w') { | |
405 | uint16_t data; | |
406 | cpu_physical_memory_read(addr, &data, 2); | |
407 | value = tswap16(data); | |
408 | } else if (words[0][4] == 'l') { | |
409 | uint32_t data; | |
410 | cpu_physical_memory_read(addr, &data, 4); | |
411 | value = tswap32(data); | |
412 | } else if (words[0][4] == 'q') { | |
413 | cpu_physical_memory_read(addr, &value, 8); | |
414 | tswap64s(&value); | |
415 | } | |
416 | qtest_send_prefix(chr); | |
332cc7e9 | 417 | qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value); |
c7f0f3b1 AL |
418 | } else if (strcmp(words[0], "read") == 0) { |
419 | uint64_t addr, len, i; | |
420 | uint8_t *data; | |
5560b85a | 421 | char *enc; |
c7f0f3b1 AL |
422 | |
423 | g_assert(words[1] && words[2]); | |
5dd6be06 AF |
424 | addr = strtoull(words[1], NULL, 0); |
425 | len = strtoull(words[2], NULL, 0); | |
c7f0f3b1 AL |
426 | |
427 | data = g_malloc(len); | |
428 | cpu_physical_memory_read(addr, data, len); | |
429 | ||
5560b85a | 430 | enc = g_malloc(2 * len + 1); |
c7f0f3b1 | 431 | for (i = 0; i < len; i++) { |
5560b85a | 432 | sprintf(&enc[i * 2], "%02x", data[i]); |
c7f0f3b1 | 433 | } |
5560b85a JS |
434 | |
435 | qtest_send_prefix(chr); | |
436 | qtest_sendf(chr, "OK 0x%s\n", enc); | |
c7f0f3b1 AL |
437 | |
438 | g_free(data); | |
5560b85a | 439 | g_free(enc); |
7a6a740d JS |
440 | } else if (strcmp(words[0], "b64read") == 0) { |
441 | uint64_t addr, len; | |
442 | uint8_t *data; | |
443 | gchar *b64_data; | |
444 | ||
445 | g_assert(words[1] && words[2]); | |
446 | addr = strtoull(words[1], NULL, 0); | |
447 | len = strtoull(words[2], NULL, 0); | |
448 | ||
449 | data = g_malloc(len); | |
450 | cpu_physical_memory_read(addr, data, len); | |
451 | b64_data = g_base64_encode(data, len); | |
452 | qtest_send_prefix(chr); | |
453 | qtest_sendf(chr, "OK %s\n", b64_data); | |
454 | ||
455 | g_free(data); | |
456 | g_free(b64_data); | |
c7f0f3b1 AL |
457 | } else if (strcmp(words[0], "write") == 0) { |
458 | uint64_t addr, len, i; | |
459 | uint8_t *data; | |
460 | size_t data_len; | |
461 | ||
462 | g_assert(words[1] && words[2] && words[3]); | |
5dd6be06 AF |
463 | addr = strtoull(words[1], NULL, 0); |
464 | len = strtoull(words[2], NULL, 0); | |
c7f0f3b1 AL |
465 | |
466 | data_len = strlen(words[3]); | |
467 | if (data_len < 3) { | |
468 | qtest_send(chr, "ERR invalid argument size\n"); | |
469 | return; | |
470 | } | |
471 | ||
472 | data = g_malloc(len); | |
473 | for (i = 0; i < len; i++) { | |
474 | if ((i * 2 + 4) <= data_len) { | |
475 | data[i] = hex2nib(words[3][i * 2 + 2]) << 4; | |
476 | data[i] |= hex2nib(words[3][i * 2 + 3]); | |
477 | } else { | |
478 | data[i] = 0; | |
479 | } | |
480 | } | |
481 | cpu_physical_memory_write(addr, data, len); | |
482 | g_free(data); | |
483 | ||
4d007963 JS |
484 | qtest_send_prefix(chr); |
485 | qtest_send(chr, "OK\n"); | |
486 | } else if (strcmp(words[0], "memset") == 0) { | |
487 | uint64_t addr, len; | |
488 | uint8_t *data; | |
489 | uint8_t pattern; | |
490 | ||
491 | g_assert(words[1] && words[2] && words[3]); | |
492 | addr = strtoull(words[1], NULL, 0); | |
493 | len = strtoull(words[2], NULL, 0); | |
494 | pattern = strtoull(words[3], NULL, 0); | |
495 | ||
496 | data = g_malloc(len); | |
497 | memset(data, pattern, len); | |
498 | cpu_physical_memory_write(addr, data, len); | |
499 | g_free(data); | |
500 | ||
7a6a740d JS |
501 | qtest_send_prefix(chr); |
502 | qtest_send(chr, "OK\n"); | |
503 | } else if (strcmp(words[0], "b64write") == 0) { | |
504 | uint64_t addr, len; | |
505 | uint8_t *data; | |
506 | size_t data_len; | |
507 | gsize out_len; | |
508 | ||
509 | g_assert(words[1] && words[2] && words[3]); | |
510 | addr = strtoull(words[1], NULL, 0); | |
511 | len = strtoull(words[2], NULL, 0); | |
512 | ||
513 | data_len = strlen(words[3]); | |
514 | if (data_len < 3) { | |
515 | qtest_send(chr, "ERR invalid argument size\n"); | |
516 | return; | |
517 | } | |
518 | ||
519 | data = g_base64_decode_inplace(words[3], &out_len); | |
520 | if (out_len != len) { | |
521 | qtest_log_send("b64write: data length mismatch (told %"PRIu64", " | |
522 | "found %zu)\n", | |
523 | len, out_len); | |
524 | out_len = MIN(out_len, len); | |
525 | } | |
526 | ||
527 | cpu_physical_memory_write(addr, data, out_len); | |
528 | ||
c7f0f3b1 AL |
529 | qtest_send_prefix(chr); |
530 | qtest_send(chr, "OK\n"); | |
d4fce24f | 531 | } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) { |
8156be56 PB |
532 | int64_t ns; |
533 | ||
534 | if (words[1]) { | |
535 | ns = strtoll(words[1], NULL, 0); | |
536 | } else { | |
40daca54 | 537 | ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL); |
8156be56 | 538 | } |
bc72ad67 | 539 | qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + ns); |
8156be56 | 540 | qtest_send_prefix(chr); |
332cc7e9 JS |
541 | qtest_sendf(chr, "OK %"PRIi64"\n", |
542 | (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); | |
d4fce24f | 543 | } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) { |
8156be56 PB |
544 | int64_t ns; |
545 | ||
546 | g_assert(words[1]); | |
547 | ns = strtoll(words[1], NULL, 0); | |
548 | qtest_clock_warp(ns); | |
549 | qtest_send_prefix(chr); | |
332cc7e9 JS |
550 | qtest_sendf(chr, "OK %"PRIi64"\n", |
551 | (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)); | |
c7f0f3b1 AL |
552 | } else { |
553 | qtest_send_prefix(chr); | |
332cc7e9 | 554 | qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]); |
c7f0f3b1 AL |
555 | } |
556 | } | |
557 | ||
558 | static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf) | |
559 | { | |
560 | char *end; | |
561 | ||
562 | while ((end = strchr(inbuf->str, '\n')) != NULL) { | |
563 | size_t offset; | |
564 | GString *cmd; | |
565 | gchar **words; | |
566 | ||
567 | offset = end - inbuf->str; | |
568 | ||
569 | cmd = g_string_new_len(inbuf->str, offset); | |
570 | g_string_erase(inbuf, 0, offset + 1); | |
571 | ||
572 | words = g_strsplit(cmd->str, " ", 0); | |
573 | qtest_process_command(chr, words); | |
574 | g_strfreev(words); | |
575 | ||
576 | g_string_free(cmd, TRUE); | |
577 | } | |
578 | } | |
579 | ||
580 | static void qtest_read(void *opaque, const uint8_t *buf, int size) | |
581 | { | |
582 | CharDriverState *chr = opaque; | |
583 | ||
584 | g_string_append_len(inbuf, (const gchar *)buf, size); | |
585 | qtest_process_inbuf(chr, inbuf); | |
586 | } | |
587 | ||
588 | static int qtest_can_read(void *opaque) | |
589 | { | |
590 | return 1024; | |
591 | } | |
592 | ||
593 | static void qtest_event(void *opaque, int event) | |
594 | { | |
595 | int i; | |
596 | ||
597 | switch (event) { | |
598 | case CHR_EVENT_OPENED: | |
ba646ff6 MA |
599 | /* |
600 | * We used to call qemu_system_reset() here, hoping we could | |
601 | * use the same process for multiple tests that way. Never | |
602 | * used. Injects an extra reset even when it's not used, and | |
603 | * that can mess up tests, e.g. -boot once. | |
604 | */ | |
c7f0f3b1 AL |
605 | for (i = 0; i < ARRAY_SIZE(irq_levels); i++) { |
606 | irq_levels[i] = 0; | |
607 | } | |
6e92466a | 608 | qemu_gettimeofday(&start_time); |
c7f0f3b1 AL |
609 | qtest_opened = true; |
610 | if (qtest_log_fp) { | |
611 | fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n", | |
35aa3fb3 | 612 | (long) start_time.tv_sec, (long) start_time.tv_usec); |
c7f0f3b1 AL |
613 | } |
614 | break; | |
615 | case CHR_EVENT_CLOSED: | |
616 | qtest_opened = false; | |
617 | if (qtest_log_fp) { | |
6e92466a | 618 | qemu_timeval tv; |
c7f0f3b1 AL |
619 | qtest_get_time(&tv); |
620 | fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n", | |
35aa3fb3 | 621 | (long) tv.tv_sec, (long) tv.tv_usec); |
c7f0f3b1 AL |
622 | } |
623 | break; | |
624 | default: | |
625 | break; | |
626 | } | |
627 | } | |
628 | ||
b3adf5ac | 629 | static int qtest_init_accel(MachineState *ms) |
c7f0f3b1 | 630 | { |
b3adf5ac MA |
631 | QemuOpts *opts = qemu_opts_create(qemu_find_opts("icount"), NULL, 0, |
632 | &error_abort); | |
633 | qemu_opt_set(opts, "shift", "0", &error_abort); | |
1ad9580b ST |
634 | configure_icount(opts, &error_abort); |
635 | qemu_opts_del(opts); | |
d4fce24f PB |
636 | return 0; |
637 | } | |
638 | ||
23802b4f | 639 | void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp) |
d4fce24f PB |
640 | { |
641 | CharDriverState *chr; | |
c7f0f3b1 AL |
642 | |
643 | chr = qemu_chr_new("qtest", qtest_chrdev, NULL); | |
644 | ||
23802b4f FZ |
645 | if (chr == NULL) { |
646 | error_setg(errp, "Failed to initialize device for qtest: \"%s\"", | |
647 | qtest_chrdev); | |
648 | return; | |
649 | } | |
650 | ||
c7f0f3b1 AL |
651 | if (qtest_log) { |
652 | if (strcmp(qtest_log, "none") != 0) { | |
653 | qtest_log_fp = fopen(qtest_log, "w+"); | |
654 | } | |
655 | } else { | |
656 | qtest_log_fp = stderr; | |
657 | } | |
658 | ||
107684c0 LL |
659 | qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr); |
660 | qemu_chr_fe_set_echo(chr, true); | |
661 | ||
662 | inbuf = g_string_new(""); | |
c7f0f3b1 | 663 | qtest_chr = chr; |
c7f0f3b1 | 664 | } |
b3be57c3 MT |
665 | |
666 | bool qtest_driver(void) | |
667 | { | |
668 | return qtest_chr; | |
669 | } | |
3a6ce514 EH |
670 | |
671 | static void qtest_accel_class_init(ObjectClass *oc, void *data) | |
672 | { | |
673 | AccelClass *ac = ACCEL_CLASS(oc); | |
674 | ac->name = "QTest"; | |
675 | ac->available = qtest_available; | |
0d15da8e | 676 | ac->init_machine = qtest_init_accel; |
3a6ce514 EH |
677 | ac->allowed = &qtest_allowed; |
678 | } | |
679 | ||
680 | #define TYPE_QTEST_ACCEL ACCEL_CLASS_NAME("qtest") | |
681 | ||
682 | static const TypeInfo qtest_accel_type = { | |
683 | .name = TYPE_QTEST_ACCEL, | |
684 | .parent = TYPE_ACCEL, | |
685 | .class_init = qtest_accel_class_init, | |
686 | }; | |
687 | ||
688 | static void qtest_type_init(void) | |
689 | { | |
690 | type_register_static(&qtest_accel_type); | |
691 | } | |
692 | ||
693 | type_init(qtest_type_init); |