Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/system/qtest.c
Line
Count
Source
1
/*
2
 * Test Server
3
 *
4
 * Copyright IBM, Corp. 2011
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
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
14
#include "qemu/osdep.h"
15
#include "qapi/error.h"
16
#include "system/qtest.h"
17
#include "system/runstate.h"
18
#include "chardev/char-fe.h"
19
#include "system/ioport.h"
20
#include "system/memory.h"
21
#include "exec/tswap.h"
22
#include "hw/core/qdev.h"
23
#include "hw/core/irq.h"
24
#include "hw/core/cpu.h"
25
#include "qemu/accel.h"
26
#include "system/cpu-timers.h"
27
#include "qemu/config-file.h"
28
#include "qemu/option.h"
29
#include "qemu/error-report.h"
30
#include "qemu/module.h"
31
#include "qemu/cutils.h"
32
#include "qemu/target-info.h"
33
#include "qom/object_interfaces.h"
34
#include "qom/qom-qobject.h"
35
#include "qobject/qobject.h"
36
#include "qapi-commands-block.h"
37
38
#define MAX_IRQ 256
39
40
63
#define TYPE_QTEST "qtest"
41
42
OBJECT_DECLARE_SIMPLE_TYPE(QTest, QTEST)
43
44
struct QTest {
45
    Object parent;
46
47
    bool has_machine_link;
48
    char *chr_name;
49
    Chardev *chr;
50
    CharFrontend qtest_chr;
51
    char *log;
52
};
53
54
bool qtest_allowed;
55
56
static DeviceState *irq_intercept_dev;
57
static FILE *qtest_log_fp;
58
static QTest *qtest;
59
static GString *inbuf;
60
static GTimer *timer;
61
static bool qtest_opened;
62
static void (*qtest_server_send)(void*, const char*);
63
static void *qtest_server_send_opaque;
64
65
typedef struct IrqInterceptState {
66
    int *irq_levels;
67
    int num_irqs;
68
} IrqInterceptState;
69
70
IrqInterceptState irq_intercept_state;
71
72
#define FMT_timeval "%.06f"
73
74
/**
75
 * DOC: QTest Protocol
76
 *
77
 * Line based protocol, request/response based.  Server can send async messages
78
 * so clients should always handle many async messages before the response
79
 * comes in.
80
 *
81
 * Extra ASCII space characters in command inputs are permitted and ignored.
82
 * Lines containing only spaces are permitted and ignored.
83
 * Lines that start with a '#' character (comments) are permitted and ignored.
84
 *
85
 * Valid requests
86
 * ^^^^^^^^^^^^^^
87
 *
88
 * Clock management:
89
 * """""""""""""""""
90
 *
91
 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL.  qtest commands
92
 * let you adjust the value of the clock (monotonically).  All the commands
93
 * return the current value of the clock in nanoseconds.
94
 *
95
 * If the commands FAIL then time wasn't advanced which is likely
96
 * because the machine was in a paused state or no timer events exist
97
 * in the future. This will cause qtest to abort and the test will
98
 * need to check its assumptions.
99
 *
100
 * .. code-block:: none
101
 *
102
 *  > clock_step
103
 *  < OK VALUE
104
 *
105
 * Advance the clock to the next deadline.  Useful when waiting for
106
 * asynchronous events.
107
 *
108
 * .. code-block:: none
109
 *
110
 *  > clock_step NS
111
 *  < OK VALUE
112
 *
113
 * Advance the clock by NS nanoseconds.
114
 *
115
 * .. code-block:: none
116
 *
117
 *  > clock_set NS
118
 *  < OK VALUE
119
 *
120
 * Advance the clock to NS nanoseconds (do nothing if it's already past).
121
 *
122
 * PIO and memory access:
123
 * """"""""""""""""""""""
124
 *
125
 * .. code-block:: none
126
 *
127
 *  > outb ADDR VALUE
128
 *  < OK
129
 *
130
 * .. code-block:: none
131
 *
132
 *  > outw ADDR VALUE
133
 *  < OK
134
 *
135
 * .. code-block:: none
136
 *
137
 *  > outl ADDR VALUE
138
 *  < OK
139
 *
140
 * .. code-block:: none
141
 *
142
 *  > inb ADDR
143
 *  < OK VALUE
144
 *
145
 * .. code-block:: none
146
 *
147
 *  > inw ADDR
148
 *  < OK VALUE
149
 *
150
 * .. code-block:: none
151
 *
152
 *  > inl ADDR
153
 *  < OK VALUE
154
 *
155
 * .. code-block:: none
156
 *
157
 *  > writeb ADDR VALUE
158
 *  < OK
159
 *
160
 * .. code-block:: none
161
 *
162
 *  > writew ADDR VALUE
163
 *  < OK
164
 *
165
 * .. code-block:: none
166
 *
167
 *  > writel ADDR VALUE
168
 *  < OK
169
 *
170
 * .. code-block:: none
171
 *
172
 *  > writeq ADDR VALUE
173
 *  < OK
174
 *
175
 * .. code-block:: none
176
 *
177
 *  > readb ADDR
178
 *  < OK VALUE
179
 *
180
 * .. code-block:: none
181
 *
182
 *  > readw ADDR
183
 *  < OK VALUE
184
 *
185
 * .. code-block:: none
186
 *
187
 *  > readl ADDR
188
 *  < OK VALUE
189
 *
190
 * .. code-block:: none
191
 *
192
 *  > readq ADDR
193
 *  < OK VALUE
194
 *
195
 * .. code-block:: none
196
 *
197
 *  > read ADDR SIZE
198
 *  < OK DATA
199
 *
200
 * .. code-block:: none
201
 *
202
 *  > write ADDR SIZE DATA
203
 *  < OK
204
 *
205
 * .. code-block:: none
206
 *
207
 *  > b64read ADDR SIZE
208
 *  < OK B64_DATA
209
 *
210
 * .. code-block:: none
211
 *
212
 *  > b64write ADDR SIZE B64_DATA
213
 *  < OK
214
 *
215
 * .. code-block:: none
216
 *
217
 *  > memset ADDR SIZE VALUE
218
 *  < OK
219
 *
220
 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
221
 * For 'memset' a zero size is permitted and does nothing.
222
 *
223
 * DATA is an arbitrarily long hex number prefixed with '0x'.  If it's smaller
224
 * than the expected size, the value will be zero filled at the end of the data
225
 * sequence.
226
 *
227
 * B64_DATA is an arbitrarily long base64 encoded string.
228
 * If the sizes do not match, the data will be truncated.
229
 *
230
 * IRQ management:
231
 * """""""""""""""
232
 *
233
 * .. code-block:: none
234
 *
235
 *  > irq_intercept_in QOM-PATH
236
 *  < OK
237
 *
238
 * .. code-block:: none
239
 *
240
 *  > irq_intercept_out QOM-PATH
241
 *  < OK
242
 *
243
 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
244
 * QOM-PATH.  When the pin is triggered, one of the following async messages
245
 * will be printed to the qtest stream::
246
 *
247
 *  IRQ raise NUM
248
 *  IRQ lower NUM
249
 *
250
 * where NUM is an IRQ number.  For the PC, interrupts can be intercepted
251
 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
252
 * NUM=0 even though it is remapped to GSI 2).
253
 *
254
 * Setting interrupt level:
255
 * """"""""""""""""""""""""
256
 *
257
 * .. code-block:: none
258
 *
259
 *  > set_irq_in QOM-PATH NAME NUM LEVEL
260
 *  < OK
261
 *
262
 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
263
 * LEVEL is an signed integer IRQ level.
264
 *
265
 * Forcibly set the given interrupt pin to the given level.
266
 *
267
 */
268
269
static int hex2nib(char ch)
270
0
{
271
0
    if (ch >= '0' && ch <= '9') {
272
0
        return ch - '0';
273
0
    } else if (ch >= 'a' && ch <= 'f') {
274
0
        return 10 + (ch - 'a');
275
0
    } else if (ch >= 'A' && ch <= 'F') {
276
0
        return 10 + (ch - 'A');
277
0
    } else {
278
0
        return -1;
279
0
    }
280
0
}
281
282
static void qtest_log_timestamp(void)
283
27.3M
{
284
27.3M
    if (!qtest_log_fp || !qtest_opened) {
285
27.3M
        return;
286
27.3M
    }
287
288
0
    fprintf(qtest_log_fp, "[S +" FMT_timeval "] ", g_timer_elapsed(timer, NULL));
289
0
}
290
291
static void G_GNUC_PRINTF(1, 2) qtest_log_send(const char *fmt, ...)
292
0
{
293
0
    va_list ap;
294
295
0
    if (!qtest_log_fp || !qtest_opened) {
296
0
        return;
297
0
    }
298
299
0
    qtest_log_timestamp();
300
301
0
    va_start(ap, fmt);
302
0
    vfprintf(qtest_log_fp, fmt, ap);
303
0
    va_end(ap);
304
0
}
305
306
static void qtest_server_char_be_send(void *opaque, const char *str)
307
0
{
308
0
    size_t len = strlen(str);
309
0
    CharFrontend* chr = (CharFrontend *)opaque;
310
0
    qemu_chr_fe_write_all(chr, (uint8_t *)str, len);
311
0
    if (qtest_log_fp && qtest_opened) {
312
0
        fprintf(qtest_log_fp, "%s", str);
313
0
    }
314
0
}
315
316
static void qtest_send(CharFrontend *chr, const char *str)
317
27.3M
{
318
27.3M
    qtest_log_timestamp();
319
27.3M
    qtest_server_send(qtest_server_send_opaque, str);
320
27.3M
}
321
322
void qtest_sendf(CharFrontend *chr, const char *fmt, ...)
323
27.3M
{
324
27.3M
    va_list ap;
325
27.3M
    gchar *buffer;
326
327
27.3M
    va_start(ap, fmt);
328
27.3M
    buffer = g_strdup_vprintf(fmt, ap);
329
27.3M
    qtest_send(chr, buffer);
330
27.3M
    g_free(buffer);
331
27.3M
    va_end(ap);
332
27.3M
}
333
334
static void qtest_irq_handler(void *opaque, int n, int level)
335
0
{
336
0
    assert(n >= 0 && n < irq_intercept_state.num_irqs);
337
0
    if (irq_intercept_state.irq_levels[n] != level) {
338
0
        CharFrontend *chr = &qtest->qtest_chr;
339
0
        irq_intercept_state.irq_levels[n] = level;
340
0
        qtest_sendf(chr, "IRQ %s %d\n",
341
0
                    level ? "raise" : "lower", n);
342
0
    }
343
0
}
344
345
static bool (*process_command_cb)(CharFrontend *chr, gchar **words);
346
347
void qtest_set_command_cb(bool (*pc_cb)(CharFrontend *chr, gchar **words))
348
0
{
349
0
    assert(!process_command_cb);  /* Switch to a list if we need more than one */
350
351
0
    process_command_cb = pc_cb;
352
0
}
353
354
static void qtest_install_gpio_out_intercept(DeviceState *dev, const char *name, int n)
355
0
{
356
0
    qemu_irq *disconnected = g_new0(qemu_irq, 1);
357
0
    qemu_irq icpt = qemu_allocate_irq(qtest_irq_handler,
358
0
                                      disconnected, n);
359
360
0
    *disconnected = qdev_intercept_gpio_out(dev, icpt, name, n);
361
0
}
362
363
static void qtest_init_irq_intercept(int num_irqs)
364
0
{
365
0
    g_free(irq_intercept_state.irq_levels);
366
0
    irq_intercept_state.irq_levels = g_new0(int, num_irqs);
367
0
    irq_intercept_state.num_irqs = num_irqs;
368
0
}
369
370
static void qtest_process_command(CharFrontend *chr, gchar **words)
371
27.3M
{
372
27.3M
    const gchar *command;
373
374
27.3M
    g_assert(words);
375
376
27.3M
    command = words[0];
377
378
27.3M
    if (qtest_log_fp) {
379
0
        int i;
380
381
0
        fprintf(qtest_log_fp, "[R +" FMT_timeval "]", g_timer_elapsed(timer, NULL));
382
0
        for (i = 0; words[i]; i++) {
383
0
            fprintf(qtest_log_fp, " %s", words[i]);
384
0
        }
385
0
        fprintf(qtest_log_fp, "\n");
386
0
    }
387
388
27.3M
    if (!command || command[0] == '#') {
389
        /* Input line was blank or a comment: ignore it */
390
0
        return;
391
0
    }
392
393
27.3M
    if (strcmp(words[0], "irq_intercept_out") == 0
394
27.3M
        || strcmp(words[0], "irq_intercept_in") == 0) {
395
0
        DeviceState *dev;
396
0
        NamedGPIOList *ngl;
397
0
        bool is_named;
398
0
        bool is_outbound;
399
0
        bool interception_succeeded = false;
400
401
0
        g_assert(words[1]);
402
0
        is_named = words[2] != NULL;
403
0
        is_outbound = words[0][14] == 'o';
404
0
        dev = DEVICE(object_resolve_path(words[1], NULL));
405
0
        if (!dev) {
406
0
            qtest_send(chr, "FAIL Unknown device\n");
407
0
            return;
408
0
        }
409
410
0
        if (is_named && !is_outbound) {
411
0
            qtest_send(chr, "FAIL Interception of named in-GPIOs not yet supported\n");
412
0
            return;
413
0
        }
414
415
0
        if (irq_intercept_dev) {
416
0
            if (irq_intercept_dev != dev) {
417
0
                qtest_send(chr, "FAIL IRQ intercept already enabled\n");
418
0
            } else {
419
0
                qtest_send(chr, "OK\n");
420
0
            }
421
0
            return;
422
0
        }
423
424
0
        QLIST_FOREACH(ngl, &dev->gpios, node) {
425
            /* NULL is valid and matchable, for "unnamed GPIO" */
426
0
            if (g_strcmp0(ngl->name, words[2]) != 0) {
427
0
                continue;
428
0
            }
429
430
0
            if (is_outbound) {
431
0
                int i;
432
0
                qtest_init_irq_intercept(ngl->num_out);
433
0
                for (i = 0; i < ngl->num_out; ++i) {
434
0
                    qtest_install_gpio_out_intercept(dev, ngl->name, i);
435
0
                }
436
0
            } else {
437
0
                qtest_init_irq_intercept(ngl->num_in);
438
0
                qemu_irq_set_observer(ngl->in, qtest_irq_handler,
439
0
                                      ngl->num_in);
440
0
            }
441
0
            interception_succeeded = true;
442
0
            break;
443
0
        }
444
445
0
        if (interception_succeeded) {
446
0
            irq_intercept_dev = dev;
447
0
            qtest_send(chr, "OK\n");
448
0
        } else {
449
0
            qtest_send(chr, "FAIL No intercepts installed\n");
450
0
        }
451
27.3M
    } else if (strcmp(words[0], "set_irq_in") == 0) {
452
0
        DeviceState *dev;
453
0
        qemu_irq irq;
454
0
        char *name;
455
0
        int ret;
456
0
        int num;
457
0
        int level;
458
459
0
        g_assert(words[1] && words[2] && words[3] && words[4]);
460
461
0
        dev = DEVICE(object_resolve_path(words[1], NULL));
462
0
        if (!dev) {
463
0
            qtest_send(chr, "FAIL Unknown device\n");
464
0
            return;
465
0
        }
466
467
0
        if (strcmp(words[2], "unnamed-gpio-in") == 0) {
468
0
            name = NULL;
469
0
        } else {
470
0
            name = words[2];
471
0
        }
472
473
0
        ret = qemu_strtoi(words[3], NULL, 0, &num);
474
0
        g_assert(!ret);
475
0
        ret = qemu_strtoi(words[4], NULL, 0, &level);
476
0
        g_assert(!ret);
477
478
0
        irq = qdev_get_gpio_in_named(dev, name, num);
479
480
0
        qemu_set_irq(irq, level);
481
0
        qtest_send(chr, "OK\n");
482
27.3M
    } else if (strcmp(words[0], "outb") == 0 ||
483
27.3M
               strcmp(words[0], "outw") == 0 ||
484
27.3M
               strcmp(words[0], "outl") == 0) {
485
0
        unsigned long addr;
486
0
        unsigned long value;
487
0
        int ret;
488
489
0
        g_assert(words[1] && words[2]);
490
0
        ret = qemu_strtoul(words[1], NULL, 0, &addr);
491
0
        g_assert(ret == 0);
492
0
        ret = qemu_strtoul(words[2], NULL, 0, &value);
493
0
        g_assert(ret == 0);
494
0
        g_assert(addr <= 0xffff);
495
496
0
        if (words[0][3] == 'b') {
497
0
            cpu_outb(addr, value);
498
0
        } else if (words[0][3] == 'w') {
499
0
            cpu_outw(addr, value);
500
0
        } else if (words[0][3] == 'l') {
501
0
            cpu_outl(addr, value);
502
0
        }
503
0
        qtest_send(chr, "OK\n");
504
27.3M
    } else if (strcmp(words[0], "inb") == 0 ||
505
27.3M
        strcmp(words[0], "inw") == 0 ||
506
27.3M
        strcmp(words[0], "inl") == 0) {
507
0
        unsigned long addr;
508
0
        uint32_t value = -1U;
509
0
        int ret;
510
511
0
        g_assert(words[1]);
512
0
        ret = qemu_strtoul(words[1], NULL, 0, &addr);
513
0
        g_assert(ret == 0);
514
0
        g_assert(addr <= 0xffff);
515
516
0
        if (words[0][2] == 'b') {
517
0
            value = cpu_inb(addr);
518
0
        } else if (words[0][2] == 'w') {
519
0
            value = cpu_inw(addr);
520
0
        } else if (words[0][2] == 'l') {
521
0
            value = cpu_inl(addr);
522
0
        }
523
0
        qtest_sendf(chr, "OK 0x%04x\n", value);
524
27.3M
    } else if (strcmp(words[0], "writeb") == 0 ||
525
27.3M
               strcmp(words[0], "writew") == 0 ||
526
27.3M
               strcmp(words[0], "writel") == 0 ||
527
27.3M
               strcmp(words[0], "writeq") == 0) {
528
0
        uint64_t addr;
529
0
        uint64_t value;
530
0
        int ret;
531
532
0
        g_assert(words[1] && words[2]);
533
0
        ret = qemu_strtou64(words[1], NULL, 0, &addr);
534
0
        g_assert(ret == 0);
535
0
        ret = qemu_strtou64(words[2], NULL, 0, &value);
536
0
        g_assert(ret == 0);
537
538
0
        if (words[0][5] == 'b') {
539
0
            uint8_t data = value;
540
0
            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
541
0
                                &data, 1);
542
0
        } else if (words[0][5] == 'w') {
543
0
            uint16_t data = value;
544
0
            tswap16s(&data);
545
0
            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
546
0
                                &data, 2);
547
0
        } else if (words[0][5] == 'l') {
548
0
            uint32_t data = value;
549
0
            tswap32s(&data);
550
0
            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
551
0
                                &data, 4);
552
0
        } else if (words[0][5] == 'q') {
553
0
            uint64_t data = value;
554
0
            tswap64s(&data);
555
0
            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
556
0
                                &data, 8);
557
0
        }
558
0
        qtest_send(chr, "OK\n");
559
27.3M
    } else if (strcmp(words[0], "readb") == 0 ||
560
27.3M
               strcmp(words[0], "readw") == 0 ||
561
27.3M
               strcmp(words[0], "readl") == 0 ||
562
27.3M
               strcmp(words[0], "readq") == 0) {
563
0
        uint64_t addr;
564
0
        uint64_t value = UINT64_C(-1);
565
0
        int ret;
566
567
0
        g_assert(words[1]);
568
0
        ret = qemu_strtou64(words[1], NULL, 0, &addr);
569
0
        g_assert(ret == 0);
570
571
0
        if (words[0][4] == 'b') {
572
0
            uint8_t data;
573
0
            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
574
0
                               &data, 1);
575
0
            value = data;
576
0
        } else if (words[0][4] == 'w') {
577
0
            uint16_t data;
578
0
            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
579
0
                               &data, 2);
580
0
            value = tswap16(data);
581
0
        } else if (words[0][4] == 'l') {
582
0
            uint32_t data;
583
0
            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
584
0
                               &data, 4);
585
0
            value = tswap32(data);
586
0
        } else if (words[0][4] == 'q') {
587
0
            address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
588
0
                               &value, 8);
589
0
            tswap64s(&value);
590
0
        }
591
0
        qtest_sendf(chr, "OK 0x%016" PRIx64 "\n", value);
592
27.3M
    } else if (strcmp(words[0], "read") == 0) {
593
0
        g_autoptr(GString) enc = NULL;
594
0
        uint64_t addr, len;
595
0
        uint8_t *data;
596
0
        int ret;
597
598
0
        g_assert(words[1] && words[2]);
599
0
        ret = qemu_strtou64(words[1], NULL, 0, &addr);
600
0
        g_assert(ret == 0);
601
0
        ret = qemu_strtou64(words[2], NULL, 0, &len);
602
0
        g_assert(ret == 0);
603
        /* We'd send garbage to libqtest if len is 0 */
604
0
        g_assert(len);
605
606
0
        data = g_malloc(len);
607
0
        address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
608
0
                           len);
609
610
0
        enc = qemu_hexdump_line(NULL, data, len, 0, 0);
611
612
0
        qtest_sendf(chr, "OK 0x%s\n", enc->str);
613
614
0
        g_free(data);
615
27.3M
    } else if (strcmp(words[0], "b64read") == 0) {
616
0
        uint64_t addr, len;
617
0
        uint8_t *data;
618
0
        gchar *b64_data;
619
0
        int ret;
620
621
0
        g_assert(words[1] && words[2]);
622
0
        ret = qemu_strtou64(words[1], NULL, 0, &addr);
623
0
        g_assert(ret == 0);
624
0
        ret = qemu_strtou64(words[2], NULL, 0, &len);
625
0
        g_assert(ret == 0);
626
627
0
        data = g_malloc(len);
628
0
        address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
629
0
                           len);
630
0
        b64_data = g_base64_encode(data, len);
631
0
        qtest_sendf(chr, "OK %s\n", b64_data);
632
633
0
        g_free(data);
634
0
        g_free(b64_data);
635
27.3M
    } else if (strcmp(words[0], "write") == 0) {
636
0
        uint64_t addr, len, i;
637
0
        uint8_t *data;
638
0
        size_t data_len;
639
0
        int ret;
640
641
0
        g_assert(words[1] && words[2] && words[3]);
642
0
        ret = qemu_strtou64(words[1], NULL, 0, &addr);
643
0
        g_assert(ret == 0);
644
0
        ret = qemu_strtou64(words[2], NULL, 0, &len);
645
0
        g_assert(ret == 0);
646
647
0
        data_len = strlen(words[3]);
648
0
        if (data_len < 3) {
649
0
            qtest_send(chr, "ERR invalid argument size\n");
650
0
            return;
651
0
        }
652
653
0
        data = g_malloc(len);
654
0
        for (i = 0; i < len; i++) {
655
0
            if ((i * 2 + 4) <= data_len) {
656
0
                data[i] = hex2nib(words[3][i * 2 + 2]) << 4;
657
0
                data[i] |= hex2nib(words[3][i * 2 + 3]);
658
0
            } else {
659
0
                data[i] = 0;
660
0
            }
661
0
        }
662
0
        address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
663
0
                            len);
664
0
        g_free(data);
665
666
0
        qtest_send(chr, "OK\n");
667
27.3M
    } else if (strcmp(words[0], "memset") == 0) {
668
0
        uint64_t addr, len;
669
0
        uint8_t *data;
670
0
        unsigned long pattern;
671
0
        int ret;
672
673
0
        g_assert(words[1] && words[2] && words[3]);
674
0
        ret = qemu_strtou64(words[1], NULL, 0, &addr);
675
0
        g_assert(ret == 0);
676
0
        ret = qemu_strtou64(words[2], NULL, 0, &len);
677
0
        g_assert(ret == 0);
678
0
        ret = qemu_strtoul(words[3], NULL, 0, &pattern);
679
0
        g_assert(ret == 0);
680
681
0
        if (len) {
682
0
            data = g_malloc(len);
683
0
            memset(data, pattern, len);
684
0
            address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
685
0
                                data, len);
686
0
            g_free(data);
687
0
        }
688
689
0
        qtest_send(chr, "OK\n");
690
27.3M
    }  else if (strcmp(words[0], "b64write") == 0) {
691
0
        uint64_t addr, len;
692
0
        uint8_t *data;
693
0
        size_t data_len;
694
0
        gsize out_len;
695
0
        int ret;
696
697
0
        g_assert(words[1] && words[2] && words[3]);
698
0
        ret = qemu_strtou64(words[1], NULL, 0, &addr);
699
0
        g_assert(ret == 0);
700
0
        ret = qemu_strtou64(words[2], NULL, 0, &len);
701
0
        g_assert(ret == 0);
702
703
0
        data_len = strlen(words[3]);
704
0
        if (data_len < 3) {
705
0
            qtest_send(chr, "ERR invalid argument size\n");
706
0
            return;
707
0
        }
708
709
0
        data = g_base64_decode_inplace(words[3], &out_len);
710
0
        if (out_len != len) {
711
0
            qtest_log_send("b64write: data length mismatch (told %"PRIu64", "
712
0
                           "found %zu)\n",
713
0
                           len, out_len);
714
0
            out_len = MIN(out_len, len);
715
0
        }
716
717
0
        address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
718
0
                            out_len);
719
720
0
        qtest_send(chr, "OK\n");
721
27.3M
    } else if (strcmp(words[0], "endianness") == 0) {
722
63
        if (target_big_endian()) {
723
0
            qtest_sendf(chr, "OK big\n");
724
63
        } else {
725
63
            qtest_sendf(chr, "OK little\n");
726
63
        }
727
27.3M
    } else if (qtest_enabled() && strcmp(words[0], "clock_step") == 0) {
728
27.3M
        int64_t old_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
729
27.3M
        int64_t ns, new_ns;
730
731
27.3M
        if (words[1]) {
732
0
            int ret = qemu_strtoi64(words[1], NULL, 0, &ns);
733
0
            g_assert(ret == 0);
734
27.3M
        } else {
735
27.3M
            ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
736
27.3M
                                            QEMU_TIMER_ATTR_ALL);
737
27.3M
            if (ns < 0) {
738
0
                qtest_send(chr, "FAIL "
739
0
                           "cannot advance clock to the next deadline "
740
0
                           "because there is no pending deadline\n");
741
0
                return;
742
0
            }
743
27.3M
        }
744
27.3M
        new_ns = qemu_clock_advance_virtual_time(old_ns + ns);
745
27.3M
        if (new_ns > old_ns) {
746
27.3M
            qtest_sendf(chr, "OK %"PRIi64"\n", new_ns);
747
27.3M
        } else {
748
0
            qtest_sendf(chr, "FAIL could not advance time\n");
749
0
        }
750
27.3M
    } else if (strcmp(words[0], "module_load") == 0) {
751
0
        Error *local_err = NULL;
752
0
        int rv;
753
0
        g_assert(words[1] && words[2]);
754
755
0
        rv = module_load(words[1], words[2], &local_err);
756
0
        if (rv > 0) {
757
0
            qtest_sendf(chr, "OK\n");
758
0
        } else {
759
0
            if (rv < 0) {
760
0
                error_report_err(local_err);
761
0
            }
762
0
            qtest_sendf(chr, "FAIL\n");
763
0
        }
764
0
    } else if (qtest_enabled() && strcmp(words[0], "clock_set") == 0) {
765
0
        int64_t ns, new_ns;
766
0
        int ret;
767
768
0
        g_assert(words[1]);
769
0
        ret = qemu_strtoi64(words[1], NULL, 0, &ns);
770
0
        g_assert(ret == 0);
771
0
        new_ns = qemu_clock_advance_virtual_time(ns);
772
0
        qtest_sendf(chr, "%s %"PRIi64"\n",
773
0
                    new_ns == ns ? "OK" : "FAIL", new_ns);
774
0
    } else if (strcmp(words[0], "qom-tests") == 0) {
775
0
        GSList *list, *l;
776
777
0
        list = object_class_get_list(NULL, false);
778
0
        for (l = list; l; l = l->next) {
779
0
            ObjectClass *klass = l->data;
780
0
            const char *type_name = object_class_get_name(klass);
781
0
            Object *obj;
782
0
            ObjectPropertyIterator iter;
783
0
            ObjectProperty *prop;
784
785
0
            obj = object_new_with_class(klass);
786
0
            object_property_iter_init(&iter, obj);
787
0
            while ((prop = object_property_iter_next(&iter))) {
788
0
                QObject *value;
789
0
                Error *local_err = NULL;
790
791
0
                value = object_property_get_qobject(obj, prop->name,
792
0
                                                    &local_err);
793
0
                if (local_err) {
794
0
                    error_report("qom-tests: %s.%s: get failed: %s",
795
0
                                 type_name, prop->name,
796
0
                                 error_get_pretty(local_err));
797
0
                    error_free(local_err);
798
0
                    continue;
799
0
                }
800
801
0
                if (prop->set) {
802
0
                    if (!object_property_set_qobject(obj, prop->name, value,
803
0
                                                     &local_err)) {
804
0
                        error_report("qom-tests: %s.%s: set failed: %s",
805
0
                                     type_name, prop->name,
806
0
                                     error_get_pretty(local_err));
807
0
                        error_free(local_err);
808
0
                    }
809
0
                }
810
811
0
                qobject_unref(value);
812
0
            }
813
814
0
            object_unref(obj);
815
0
        }
816
0
        g_slist_free(list);
817
0
        qtest_send(chr, "OK\n");
818
0
    } else if (strcmp(words[0], "qemu-io") == 0) {
819
0
        const char *io_dev;
820
0
        g_autofree char *io_cmd = NULL;
821
0
        Error *err = NULL;
822
823
0
        g_assert(words[1] && words[2]);
824
0
        io_dev = words[1];
825
0
        io_cmd = g_strjoinv(" ", &words[2]);
826
827
0
        qmp_x_qemu_io(io_dev, NULL, io_cmd, &err);
828
0
        qtest_sendf(chr, err ? "FAIL %s\n" : "OK\n",
829
0
                    err ? error_get_pretty(err) : NULL);
830
0
        error_free(err);
831
0
    } else if (process_command_cb && process_command_cb(chr, words)) {
832
        /* Command got consumed by the callback handler */
833
0
    } else {
834
0
        qtest_sendf(chr, "FAIL Unknown command '%s'\n", words[0]);
835
0
    }
836
27.3M
}
837
838
/*
839
 * Process as much of @inbuf as we can in newline terminated chunks.
840
 * Remove the processed commands from @inbuf as we go.
841
 */
842
static void qtest_process_inbuf(CharFrontend *chr, GString *inbuf)
843
27.3M
{
844
27.3M
    char *end;
845
846
54.6M
    while ((end = strchr(inbuf->str, '\n')) != NULL) {
847
27.3M
        size_t len = end - inbuf->str;
848
27.3M
        g_autofree char *cmd = g_strndup(inbuf->str, len);
849
27.3M
        g_auto(GStrv) words = g_strsplit(cmd, " ", 0);
850
851
27.3M
        g_string_erase(inbuf, 0, len + 1);
852
27.3M
        qtest_process_command(chr, words);
853
27.3M
    }
854
27.3M
}
855
856
static void qtest_read(void *opaque, const uint8_t *buf, int size)
857
0
{
858
0
    CharFrontend *chr = opaque;
859
860
0
    g_string_append_len(inbuf, (const gchar *)buf, size);
861
0
    qtest_process_inbuf(chr, inbuf);
862
0
}
863
864
static int qtest_can_read(void *opaque)
865
48
{
866
48
    return 1024;
867
48
}
868
869
static void qtest_event(void *opaque, QEMUChrEvent event)
870
87
{
871
87
    int i;
872
873
87
    switch (event) {
874
63
    case CHR_EVENT_OPENED:
875
        /*
876
         * We used to call qemu_system_reset() here, hoping we could
877
         * use the same process for multiple tests that way.  Never
878
         * used.  Injects an extra reset even when it's not used, and
879
         * that can mess up tests, e.g. -boot once.
880
         */
881
63
        if (irq_intercept_state.irq_levels) {
882
0
            for (i = 0; i < irq_intercept_state.num_irqs; i++) {
883
0
                irq_intercept_state.irq_levels[i] = 0;
884
0
            }
885
0
        }
886
887
63
        g_clear_pointer(&timer, g_timer_destroy);
888
63
        timer = g_timer_new();
889
63
        qtest_opened = true;
890
63
        if (qtest_log_fp) {
891
0
            fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n", g_timer_elapsed(timer, NULL));
892
0
        }
893
63
        break;
894
24
    case CHR_EVENT_CLOSED:
895
24
        if (!qtest_opened) {
896
            /* Ignore CLOSED events if we have already closed the log */
897
0
            break;
898
0
        }
899
24
        qtest_opened = false;
900
24
        if (qtest_log_fp) {
901
0
            fprintf(qtest_log_fp, "[I +" FMT_timeval "] CLOSED\n", g_timer_elapsed(timer, NULL));
902
0
        }
903
24
        g_clear_pointer(&timer, g_timer_destroy);
904
24
        break;
905
0
    default:
906
0
        break;
907
87
    }
908
87
}
909
910
void qtest_server_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
911
63
{
912
63
    ERRP_GUARD();
913
63
    Chardev *chr;
914
63
    Object *qobj;
915
916
63
    chr = qemu_chr_new("qtest", qtest_chrdev, NULL);
917
63
    if (chr == NULL) {
918
0
        error_setg(errp, "Failed to initialize device for qtest: \"%s\"",
919
0
                   qtest_chrdev);
920
0
        return;
921
0
    }
922
923
63
    qobj = object_new(TYPE_QTEST);
924
63
    object_property_set_str(qobj, "chardev", chr->label, &error_abort);
925
63
    if (qtest_log) {
926
63
        object_property_set_str(qobj, "log", qtest_log, &error_abort);
927
63
    }
928
63
    object_property_add_child(qdev_get_machine(), "qtest", qobj);
929
63
    user_creatable_complete(USER_CREATABLE(qobj), errp);
930
63
    if (*errp) {
931
0
        object_unparent(qobj);
932
0
    }
933
63
    object_unref(OBJECT(chr));
934
63
    object_unref(qobj);
935
63
}
936
937
static bool qtest_server_start(QTest *q, Error **errp)
938
63
{
939
63
    Chardev *chr = q->chr;
940
63
    const char *qtest_log = q->log;
941
942
63
    if (qtest_log) {
943
63
        if (strcmp(qtest_log, "none") != 0) {
944
0
            qtest_log_fp = fopen(qtest_log, "w+");
945
0
        }
946
63
    } else {
947
0
        qtest_log_fp = stderr;
948
0
    }
949
950
63
    if (!qemu_chr_fe_init(&q->qtest_chr, chr, errp)) {
951
0
        return false;
952
0
    }
953
63
    qemu_chr_fe_set_handlers(&q->qtest_chr, qtest_can_read, qtest_read,
954
63
                             qtest_event, NULL, &q->qtest_chr, NULL, true);
955
63
    qemu_chr_fe_set_echo(&q->qtest_chr, true);
956
957
63
    inbuf = g_string_new("");
958
959
63
    if (!qtest_server_send) {
960
0
        qtest_server_set_send_handler(qtest_server_char_be_send, &q->qtest_chr);
961
0
    }
962
63
    qtest = q;
963
63
    return true;
964
63
}
965
966
void qtest_server_set_send_handler(void (*send)(void*, const char*),
967
                                   void *opaque)
968
63
{
969
63
    qtest_server_send = send;
970
63
    qtest_server_send_opaque = opaque;
971
63
}
972
973
bool qtest_driver(void)
974
0
{
975
0
    return qtest && qtest->qtest_chr.chr != NULL;
976
0
}
977
978
void qtest_server_inproc_recv(void *dummy, const char *buf)
979
27.3M
{
980
27.3M
    static GString *gstr;
981
27.3M
    if (!gstr) {
982
63
        gstr = g_string_new(NULL);
983
63
    }
984
27.3M
    g_string_append(gstr, buf);
985
27.3M
    if (gstr->str[gstr->len - 1] == '\n') {
986
27.3M
        qtest_process_inbuf(NULL, gstr);
987
27.3M
        g_string_truncate(gstr, 0);
988
27.3M
    }
989
27.3M
}
990
991
static void qtest_complete(UserCreatable *uc, Error **errp)
992
63
{
993
63
    QTest *q = QTEST(uc);
994
63
    if (qtest) {
995
0
        error_setg(errp, "Only one instance of qtest can be created");
996
0
        return;
997
0
    }
998
63
    if (!q->chr_name) {
999
0
        error_setg(errp, "No backend specified");
1000
0
        return;
1001
0
    }
1002
1003
63
    if (OBJECT(uc)->parent != qdev_get_machine()) {
1004
0
        q->has_machine_link = true;
1005
0
        object_property_add_const_link(qdev_get_machine(), "qtest", OBJECT(uc));
1006
63
    } else {
1007
        /* -qtest was used.  */
1008
63
    }
1009
1010
63
    qtest_server_start(q, errp);
1011
63
}
1012
1013
static void qtest_unparent(Object *obj)
1014
0
{
1015
0
    QTest *q = QTEST(obj);
1016
1017
0
    if (qtest == q) {
1018
0
        qemu_chr_fe_disconnect(&q->qtest_chr);
1019
0
        assert(!qtest_opened);
1020
0
        qemu_chr_fe_deinit(&q->qtest_chr, false);
1021
0
        if (qtest_log_fp) {
1022
0
            fclose(qtest_log_fp);
1023
0
            qtest_log_fp = NULL;
1024
0
        }
1025
0
        qtest = NULL;
1026
0
    }
1027
1028
0
    if (q->has_machine_link) {
1029
0
        object_property_del(qdev_get_machine(), "qtest");
1030
0
        q->has_machine_link = false;
1031
0
    }
1032
0
}
1033
1034
static void qtest_set_log(Object *obj, const char *value, Error **errp)
1035
63
{
1036
63
    QTest *q = QTEST(obj);
1037
1038
63
    if (qtest == q) {
1039
0
        error_setg(errp, "Property 'log' can not be set now");
1040
63
    } else {
1041
63
        g_free(q->log);
1042
63
        q->log = g_strdup(value);
1043
63
    }
1044
63
}
1045
1046
static char *qtest_get_log(Object *obj, Error **errp)
1047
0
{
1048
0
    QTest *q = QTEST(obj);
1049
1050
0
    return g_strdup(q->log);
1051
0
}
1052
1053
static void qtest_set_chardev(Object *obj, const char *value, Error **errp)
1054
63
{
1055
63
    QTest *q = QTEST(obj);
1056
63
    Chardev *chr;
1057
1058
63
    if (qtest == q) {
1059
0
        error_setg(errp, "Property 'chardev' can not be set now");
1060
0
        return;
1061
0
    }
1062
1063
63
    chr = qemu_chr_find(value);
1064
63
    if (!chr) {
1065
0
        error_setg(errp, "Cannot find character device '%s'", value);
1066
0
        return;
1067
0
    }
1068
1069
63
    g_free(q->chr_name);
1070
63
    q->chr_name = g_strdup(value);
1071
1072
63
    if (q->chr) {
1073
0
        object_unref(q->chr);
1074
0
    }
1075
63
    q->chr = chr;
1076
63
    object_ref(chr);
1077
63
}
1078
1079
static char *qtest_get_chardev(Object *obj, Error **errp)
1080
0
{
1081
0
    QTest *q = QTEST(obj);
1082
1083
0
    return g_strdup(q->chr_name);
1084
0
}
1085
1086
static void qtest_class_init(ObjectClass *oc, const void *data)
1087
63
{
1088
63
    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
1089
1090
63
    oc->unparent = qtest_unparent;
1091
63
    ucc->complete = qtest_complete;
1092
1093
63
    object_class_property_add_str(oc, "chardev",
1094
63
                                  qtest_get_chardev, qtest_set_chardev);
1095
63
    object_class_property_add_str(oc, "log",
1096
63
                                  qtest_get_log, qtest_set_log);
1097
63
}
1098
1099
static void qtest_finalize(Object *obj)
1100
0
{
1101
0
    QTest *q = QTEST(obj);
1102
1103
0
    g_free(q->chr_name);
1104
0
    g_free(q->log);
1105
0
    object_unref(q->chr);
1106
0
}
1107
1108
static const TypeInfo qtest_info = {
1109
    .name = TYPE_QTEST,
1110
    .parent = TYPE_OBJECT,
1111
    .class_init = qtest_class_init,
1112
    .instance_finalize = qtest_finalize,
1113
    .instance_size = sizeof(QTest),
1114
    .interfaces = (const InterfaceInfo[]) {
1115
        { TYPE_USER_CREATABLE },
1116
        { }
1117
    }
1118
};
1119
1120
static void register_types(void)
1121
63
{
1122
63
    type_register_static(&qtest_info);
1123
63
}
1124
1125
type_init(register_types);