4 * Copyright IBM, Corp. 2011
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.
16 #include "qemu-char.h"
25 const char *qtest_chrdev;
26 const char *qtest_log;
27 int qtest_allowed = 0;
29 static DeviceState *irq_intercept_dev;
30 static FILE *qtest_log_fp;
31 static CharDriverState *qtest_chr;
32 static GString *inbuf;
33 static int irq_levels[MAX_IRQ];
34 static struct timeval start_time;
35 static bool qtest_opened;
37 #define FMT_timeval "%ld.%06ld"
42 * Line based protocol, request/response based. Server can send async messages
43 * so clients should always handle many async messages before the response
50 * The qtest client is completely in charge of the vm_clock. qtest commands
51 * let you adjust the value of the clock (monotonically). All the commands
52 * return the current value of the clock in nanoseconds.
57 * Advance the clock to the next deadline. Useful when waiting for
58 * asynchronous events.
63 * Advance the clock by NS nanoseconds.
68 * Advance the clock to NS nanoseconds (do nothing if it's already past).
70 * PIO and memory access:
93 * > write ADDR SIZE DATA
96 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
98 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
99 * than the expected size, the value will be zero filled at the end of the data
104 * > irq_intercept_in QOM-PATH
107 * > irq_intercept_out QOM-PATH
110 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
111 * QOM-PATH. When the pin is triggered, one of the following async messages
112 * will be printed to the qtest stream:
117 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
118 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
119 * NUM=0 even though it is remapped to GSI 2).
122 static int hex2nib(char ch)
124 if (ch >= '0' && ch <= '9') {
126 } else if (ch >= 'a' && ch <= 'f') {
127 return 10 + (ch - 'a');
128 } else if (ch >= 'A' && ch <= 'F') {
129 return 10 + (ch - 'a');
135 static void qtest_get_time(struct timeval *tv)
137 gettimeofday(tv, NULL);
138 tv->tv_sec -= start_time.tv_sec;
139 tv->tv_usec -= start_time.tv_usec;
140 if (tv->tv_usec < 0) {
141 tv->tv_usec += 1000000;
146 static void qtest_send_prefix(CharDriverState *chr)
150 if (!qtest_log_fp || !qtest_opened) {
155 fprintf(qtest_log_fp, "[S +" FMT_timeval "] ",
156 tv.tv_sec, tv.tv_usec);
159 static void qtest_send(CharDriverState *chr, const char *fmt, ...)
166 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
169 qemu_chr_fe_write(chr, (uint8_t *)buffer, len);
170 if (qtest_log_fp && qtest_opened) {
171 fprintf(qtest_log_fp, "%s", buffer);
175 static void qtest_irq_handler(void *opaque, int n, int level)
177 qemu_irq *old_irqs = opaque;
178 qemu_set_irq(old_irqs[n], level);
180 if (irq_levels[n] != level) {
181 CharDriverState *chr = qtest_chr;
182 irq_levels[n] = level;
183 qtest_send_prefix(chr);
184 qtest_send(chr, "IRQ %s %d\n",
185 level ? "raise" : "lower", n);
189 static void qtest_process_command(CharDriverState *chr, gchar **words)
191 const gchar *command;
202 fprintf(qtest_log_fp, "[R +" FMT_timeval "]",
203 tv.tv_sec, tv.tv_usec);
204 for (i = 0; words[i]; i++) {
205 fprintf(qtest_log_fp, " %s", words[i]);
207 fprintf(qtest_log_fp, "\n");
211 if (strcmp(words[0], "irq_intercept_out") == 0
212 || strcmp(words[0], "irq_intercept_in") == 0) {
216 dev = DEVICE(object_resolve_path(words[1], NULL));
218 qtest_send_prefix(chr);
219 qtest_send(chr, "FAIL Unknown device\n");
223 if (irq_intercept_dev) {
224 qtest_send_prefix(chr);
225 if (irq_intercept_dev != dev) {
226 qtest_send(chr, "FAIL IRQ intercept already enabled\n");
228 qtest_send(chr, "OK\n");
233 if (words[0][14] == 'o') {
234 qemu_irq_intercept_out(&dev->gpio_out, qtest_irq_handler, dev->num_gpio_out);
236 qemu_irq_intercept_in(dev->gpio_in, qtest_irq_handler, dev->num_gpio_in);
238 irq_intercept_dev = dev;
239 qtest_send_prefix(chr);
240 qtest_send(chr, "OK\n");
242 } else if (strcmp(words[0], "outb") == 0 ||
243 strcmp(words[0], "outw") == 0 ||
244 strcmp(words[0], "outl") == 0) {
248 g_assert(words[1] && words[2]);
249 addr = strtol(words[1], NULL, 0);
250 value = strtol(words[2], NULL, 0);
252 if (words[0][3] == 'b') {
253 cpu_outb(addr, value);
254 } else if (words[0][3] == 'w') {
255 cpu_outw(addr, value);
256 } else if (words[0][3] == 'l') {
257 cpu_outl(addr, value);
259 qtest_send_prefix(chr);
260 qtest_send(chr, "OK\n");
261 } else if (strcmp(words[0], "inb") == 0 ||
262 strcmp(words[0], "inw") == 0 ||
263 strcmp(words[0], "inl") == 0) {
265 uint32_t value = -1U;
268 addr = strtol(words[1], NULL, 0);
270 if (words[0][2] == 'b') {
271 value = cpu_inb(addr);
272 } else if (words[0][2] == 'w') {
273 value = cpu_inw(addr);
274 } else if (words[0][2] == 'l') {
275 value = cpu_inl(addr);
277 qtest_send_prefix(chr);
278 qtest_send(chr, "OK 0x%04x\n", value);
279 } else if (strcmp(words[0], "read") == 0) {
280 uint64_t addr, len, i;
283 g_assert(words[1] && words[2]);
284 addr = strtoul(words[1], NULL, 0);
285 len = strtoul(words[2], NULL, 0);
287 data = g_malloc(len);
288 cpu_physical_memory_read(addr, data, len);
290 qtest_send_prefix(chr);
291 qtest_send(chr, "OK 0x");
292 for (i = 0; i < len; i++) {
293 qtest_send(chr, "%02x", data[i]);
295 qtest_send(chr, "\n");
298 } else if (strcmp(words[0], "write") == 0) {
299 uint64_t addr, len, i;
303 g_assert(words[1] && words[2] && words[3]);
304 addr = strtoul(words[1], NULL, 0);
305 len = strtoul(words[2], NULL, 0);
307 data_len = strlen(words[3]);
309 qtest_send(chr, "ERR invalid argument size\n");
313 data = g_malloc(len);
314 for (i = 0; i < len; i++) {
315 if ((i * 2 + 4) <= data_len) {
316 data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
317 data[i] |= hex2nib(words[3][i * 2 + 3]);
322 cpu_physical_memory_write(addr, data, len);
325 qtest_send_prefix(chr);
326 qtest_send(chr, "OK\n");
327 } else if (strcmp(words[0], "clock_step") == 0) {
331 ns = strtoll(words[1], NULL, 0);
333 ns = qemu_clock_deadline(vm_clock);
335 qtest_clock_warp(qemu_get_clock_ns(vm_clock) + ns);
336 qtest_send_prefix(chr);
337 qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_get_clock_ns(vm_clock));
338 } else if (strcmp(words[0], "clock_set") == 0) {
342 ns = strtoll(words[1], NULL, 0);
343 qtest_clock_warp(ns);
344 qtest_send_prefix(chr);
345 qtest_send(chr, "OK %"PRIi64"\n", (int64_t)qemu_get_clock_ns(vm_clock));
347 qtest_send_prefix(chr);
348 qtest_send(chr, "FAIL Unknown command `%s'\n", words[0]);
352 static void qtest_process_inbuf(CharDriverState *chr, GString *inbuf)
356 while ((end = strchr(inbuf->str, '\n')) != NULL) {
361 offset = end - inbuf->str;
363 cmd = g_string_new_len(inbuf->str, offset);
364 g_string_erase(inbuf, 0, offset + 1);
366 words = g_strsplit(cmd->str, " ", 0);
367 qtest_process_command(chr, words);
370 g_string_free(cmd, TRUE);
374 static void qtest_read(void *opaque, const uint8_t *buf, int size)
376 CharDriverState *chr = opaque;
378 g_string_append_len(inbuf, (const gchar *)buf, size);
379 qtest_process_inbuf(chr, inbuf);
382 static int qtest_can_read(void *opaque)
387 static void qtest_event(void *opaque, int event)
392 case CHR_EVENT_OPENED:
393 qemu_system_reset(false);
394 for (i = 0; i < ARRAY_SIZE(irq_levels); i++) {
397 gettimeofday(&start_time, NULL);
400 fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
401 start_time.tv_sec, start_time.tv_usec);
404 case CHR_EVENT_CLOSED:
405 qtest_opened = false;
409 fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n",
410 tv.tv_sec, tv.tv_usec);
420 CharDriverState *chr;
422 g_assert(qtest_chrdev != NULL);
424 configure_icount("0");
425 chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
427 qemu_chr_add_handlers(chr, qtest_can_read, qtest_read, qtest_event, chr);
428 qemu_chr_fe_set_echo(chr, true);
430 inbuf = g_string_new("");
433 if (strcmp(qtest_log, "none") != 0) {
434 qtest_log_fp = fopen(qtest_log, "w+");
437 qtest_log_fp = stderr;