~dannf/qemu-linaro/qemu-highbank-ppa

« back to all changes in this revision

Viewing changes to hw/qdev-monitor.c

  • Committer: Steve Langasek
  • Date: 2012-03-15 21:13:19 UTC
  • mfrom: (0.1.15)
  • Revision ID: steve.langasek@canonical.com-20120315211319-f1j3ot1ihx30b2s9
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Dynamic device configuration and creation.
 
3
 *
 
4
 *  Copyright (c) 2009 CodeSourcery
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "qdev.h"
 
21
#include "monitor.h"
 
22
 
 
23
/*
 
24
 * Aliases were a bad idea from the start.  Let's keep them
 
25
 * from spreading further.
 
26
 */
 
27
typedef struct QDevAlias
 
28
{
 
29
    const char *typename;
 
30
    const char *alias;
 
31
} QDevAlias;
 
32
 
 
33
static const QDevAlias qdev_alias_table[] = {
 
34
    { "virtio-blk-pci", "virtio-blk" },
 
35
    { "virtio-net-pci", "virtio-net" },
 
36
    { "virtio-serial-pci", "virtio-serial" },
 
37
    { "virtio-balloon-pci", "virtio-balloon" },
 
38
    { "virtio-blk-s390", "virtio-blk" },
 
39
    { "virtio-net-s390", "virtio-net" },
 
40
    { "virtio-serial-s390", "virtio-serial" },
 
41
    { "lsi53c895a", "lsi" },
 
42
    { "ich9-ahci", "ahci" },
 
43
    { }
 
44
};
 
45
 
 
46
static const char *qdev_class_get_alias(DeviceClass *dc)
 
47
{
 
48
    const char *typename = object_class_get_name(OBJECT_CLASS(dc));
 
49
    int i;
 
50
 
 
51
    for (i = 0; qdev_alias_table[i].typename; i++) {
 
52
        if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
 
53
            return qdev_alias_table[i].alias;
 
54
        }
 
55
    }
 
56
 
 
57
    return NULL;
 
58
}
 
59
 
 
60
static bool qdev_class_has_alias(DeviceClass *dc)
 
61
{
 
62
    return (qdev_class_get_alias(dc) != NULL);
 
63
}
 
64
 
 
65
static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
 
66
{
 
67
    DeviceClass *dc;
 
68
    bool *show_no_user = opaque;
 
69
 
 
70
    dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
 
71
 
 
72
    if (!dc || (show_no_user && !*show_no_user && dc->no_user)) {
 
73
        return;
 
74
    }
 
75
 
 
76
    error_printf("name \"%s\"", object_class_get_name(klass));
 
77
    if (dc->bus_info) {
 
78
        error_printf(", bus %s", dc->bus_info->name);
 
79
    }
 
80
    if (qdev_class_has_alias(dc)) {
 
81
        error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
 
82
    }
 
83
    if (dc->desc) {
 
84
        error_printf(", desc \"%s\"", dc->desc);
 
85
    }
 
86
    if (dc->no_user) {
 
87
        error_printf(", no-user");
 
88
    }
 
89
    error_printf("\n");
 
90
}
 
91
 
 
92
static int set_property(const char *name, const char *value, void *opaque)
 
93
{
 
94
    DeviceState *dev = opaque;
 
95
 
 
96
    if (strcmp(name, "driver") == 0)
 
97
        return 0;
 
98
    if (strcmp(name, "bus") == 0)
 
99
        return 0;
 
100
 
 
101
    if (qdev_prop_parse(dev, name, value) == -1) {
 
102
        return -1;
 
103
    }
 
104
    return 0;
 
105
}
 
106
 
 
107
static const char *find_typename_by_alias(const char *alias)
 
108
{
 
109
    int i;
 
110
 
 
111
    for (i = 0; qdev_alias_table[i].alias; i++) {
 
112
        if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
 
113
            return qdev_alias_table[i].typename;
 
114
        }
 
115
    }
 
116
 
 
117
    return NULL;
 
118
}
 
119
 
 
120
int qdev_device_help(QemuOpts *opts)
 
121
{
 
122
    const char *driver;
 
123
    Property *prop;
 
124
    ObjectClass *klass;
 
125
    DeviceClass *info;
 
126
 
 
127
    driver = qemu_opt_get(opts, "driver");
 
128
    if (driver && !strcmp(driver, "?")) {
 
129
        bool show_no_user = false;
 
130
        object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user);
 
131
        return 1;
 
132
    }
 
133
 
 
134
    if (!driver || !qemu_opt_get(opts, "?")) {
 
135
        return 0;
 
136
    }
 
137
 
 
138
    klass = object_class_by_name(driver);
 
139
    if (!klass) {
 
140
        const char *typename = find_typename_by_alias(driver);
 
141
 
 
142
        if (typename) {
 
143
            driver = typename;
 
144
            klass = object_class_by_name(driver);
 
145
        }
 
146
    }
 
147
 
 
148
    if (!klass) {
 
149
        return 0;
 
150
    }
 
151
    info = DEVICE_CLASS(klass);
 
152
 
 
153
    for (prop = info->props; prop && prop->name; prop++) {
 
154
        /*
 
155
         * TODO Properties without a parser are just for dirty hacks.
 
156
         * qdev_prop_ptr is the only such PropertyInfo.  It's marked
 
157
         * for removal.  This conditional should be removed along with
 
158
         * it.
 
159
         */
 
160
        if (!prop->info->parse) {
 
161
            continue;           /* no way to set it, don't show */
 
162
        }
 
163
        error_printf("%s.%s=%s\n", driver, prop->name,
 
164
                     prop->info->legacy_name ?: prop->info->name);
 
165
    }
 
166
    if (info->bus_info) {
 
167
        for (prop = info->bus_info->props; prop && prop->name; prop++) {
 
168
            if (!prop->info->parse) {
 
169
                continue;           /* no way to set it, don't show */
 
170
            }
 
171
            error_printf("%s.%s=%s\n", driver, prop->name,
 
172
                         prop->info->legacy_name ?: prop->info->name);
 
173
        }
 
174
    }
 
175
    return 1;
 
176
}
 
177
 
 
178
static Object *qdev_get_peripheral(void)
 
179
{
 
180
    static Object *dev;
 
181
 
 
182
    if (dev == NULL) {
 
183
        dev = object_new("container");
 
184
        object_property_add_child(object_get_root(), "peripheral",
 
185
                                  OBJECT(dev), NULL);
 
186
    }
 
187
 
 
188
    return dev;
 
189
}
 
190
 
 
191
static Object *qdev_get_peripheral_anon(void)
 
192
{
 
193
    static Object *dev;
 
194
 
 
195
    if (dev == NULL) {
 
196
        dev = object_new("container");
 
197
        object_property_add_child(object_get_root(), "peripheral-anon",
 
198
                                  OBJECT(dev), NULL);
 
199
    }
 
200
 
 
201
    return dev;
 
202
}
 
203
 
 
204
static void qbus_list_bus(DeviceState *dev)
 
205
{
 
206
    BusState *child;
 
207
    const char *sep = " ";
 
208
 
 
209
    error_printf("child busses at \"%s\":",
 
210
                 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
 
211
    QLIST_FOREACH(child, &dev->child_bus, sibling) {
 
212
        error_printf("%s\"%s\"", sep, child->name);
 
213
        sep = ", ";
 
214
    }
 
215
    error_printf("\n");
 
216
}
 
217
 
 
218
static void qbus_list_dev(BusState *bus)
 
219
{
 
220
    DeviceState *dev;
 
221
    const char *sep = " ";
 
222
 
 
223
    error_printf("devices at \"%s\":", bus->name);
 
224
    QTAILQ_FOREACH(dev, &bus->children, sibling) {
 
225
        error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
 
226
        if (dev->id)
 
227
            error_printf("/\"%s\"", dev->id);
 
228
        sep = ", ";
 
229
    }
 
230
    error_printf("\n");
 
231
}
 
232
 
 
233
static BusState *qbus_find_bus(DeviceState *dev, char *elem)
 
234
{
 
235
    BusState *child;
 
236
 
 
237
    QLIST_FOREACH(child, &dev->child_bus, sibling) {
 
238
        if (strcmp(child->name, elem) == 0) {
 
239
            return child;
 
240
        }
 
241
    }
 
242
    return NULL;
 
243
}
 
244
 
 
245
static DeviceState *qbus_find_dev(BusState *bus, char *elem)
 
246
{
 
247
    DeviceState *dev;
 
248
 
 
249
    /*
 
250
     * try to match in order:
 
251
     *   (1) instance id, if present
 
252
     *   (2) driver name
 
253
     *   (3) driver alias, if present
 
254
     */
 
255
    QTAILQ_FOREACH(dev, &bus->children, sibling) {
 
256
        if (dev->id  &&  strcmp(dev->id, elem) == 0) {
 
257
            return dev;
 
258
        }
 
259
    }
 
260
    QTAILQ_FOREACH(dev, &bus->children, sibling) {
 
261
        if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
 
262
            return dev;
 
263
        }
 
264
    }
 
265
    QTAILQ_FOREACH(dev, &bus->children, sibling) {
 
266
        DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
267
 
 
268
        if (qdev_class_has_alias(dc) &&
 
269
            strcmp(qdev_class_get_alias(dc), elem) == 0) {
 
270
            return dev;
 
271
        }
 
272
    }
 
273
    return NULL;
 
274
}
 
275
 
 
276
static BusState *qbus_find_recursive(BusState *bus, const char *name,
 
277
                                     const BusInfo *info)
 
278
{
 
279
    DeviceState *dev;
 
280
    BusState *child, *ret;
 
281
    int match = 1;
 
282
 
 
283
    if (name && (strcmp(bus->name, name) != 0)) {
 
284
        match = 0;
 
285
    }
 
286
    if (info && (bus->info != info)) {
 
287
        match = 0;
 
288
    }
 
289
    if (match) {
 
290
        return bus;
 
291
    }
 
292
 
 
293
    QTAILQ_FOREACH(dev, &bus->children, sibling) {
 
294
        QLIST_FOREACH(child, &dev->child_bus, sibling) {
 
295
            ret = qbus_find_recursive(child, name, info);
 
296
            if (ret) {
 
297
                return ret;
 
298
            }
 
299
        }
 
300
    }
 
301
    return NULL;
 
302
}
 
303
 
 
304
static BusState *qbus_find(const char *path)
 
305
{
 
306
    DeviceState *dev;
 
307
    BusState *bus;
 
308
    char elem[128];
 
309
    int pos, len;
 
310
 
 
311
    /* find start element */
 
312
    if (path[0] == '/') {
 
313
        bus = sysbus_get_default();
 
314
        pos = 0;
 
315
    } else {
 
316
        if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
 
317
            assert(!path[0]);
 
318
            elem[0] = len = 0;
 
319
        }
 
320
        bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
 
321
        if (!bus) {
 
322
            qerror_report(QERR_BUS_NOT_FOUND, elem);
 
323
            return NULL;
 
324
        }
 
325
        pos = len;
 
326
    }
 
327
 
 
328
    for (;;) {
 
329
        assert(path[pos] == '/' || !path[pos]);
 
330
        while (path[pos] == '/') {
 
331
            pos++;
 
332
        }
 
333
        if (path[pos] == '\0') {
 
334
            return bus;
 
335
        }
 
336
 
 
337
        /* find device */
 
338
        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
 
339
            assert(0);
 
340
            elem[0] = len = 0;
 
341
        }
 
342
        pos += len;
 
343
        dev = qbus_find_dev(bus, elem);
 
344
        if (!dev) {
 
345
            qerror_report(QERR_DEVICE_NOT_FOUND, elem);
 
346
            if (!monitor_cur_is_qmp()) {
 
347
                qbus_list_dev(bus);
 
348
            }
 
349
            return NULL;
 
350
        }
 
351
 
 
352
        assert(path[pos] == '/' || !path[pos]);
 
353
        while (path[pos] == '/') {
 
354
            pos++;
 
355
        }
 
356
        if (path[pos] == '\0') {
 
357
            /* last specified element is a device.  If it has exactly
 
358
             * one child bus accept it nevertheless */
 
359
            switch (dev->num_child_bus) {
 
360
            case 0:
 
361
                qerror_report(QERR_DEVICE_NO_BUS, elem);
 
362
                return NULL;
 
363
            case 1:
 
364
                return QLIST_FIRST(&dev->child_bus);
 
365
            default:
 
366
                qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
 
367
                if (!monitor_cur_is_qmp()) {
 
368
                    qbus_list_bus(dev);
 
369
                }
 
370
                return NULL;
 
371
            }
 
372
        }
 
373
 
 
374
        /* find bus */
 
375
        if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
 
376
            assert(0);
 
377
            elem[0] = len = 0;
 
378
        }
 
379
        pos += len;
 
380
        bus = qbus_find_bus(dev, elem);
 
381
        if (!bus) {
 
382
            qerror_report(QERR_BUS_NOT_FOUND, elem);
 
383
            if (!monitor_cur_is_qmp()) {
 
384
                qbus_list_bus(dev);
 
385
            }
 
386
            return NULL;
 
387
        }
 
388
    }
 
389
}
 
390
 
 
391
DeviceState *qdev_device_add(QemuOpts *opts)
 
392
{
 
393
    ObjectClass *obj;
 
394
    DeviceClass *k;
 
395
    const char *driver, *path, *id;
 
396
    DeviceState *qdev;
 
397
    BusState *bus;
 
398
 
 
399
    driver = qemu_opt_get(opts, "driver");
 
400
    if (!driver) {
 
401
        qerror_report(QERR_MISSING_PARAMETER, "driver");
 
402
        return NULL;
 
403
    }
 
404
 
 
405
    /* find driver */
 
406
    obj = object_class_by_name(driver);
 
407
    if (!obj) {
 
408
        const char *typename = find_typename_by_alias(driver);
 
409
 
 
410
        if (typename) {
 
411
            driver = typename;
 
412
            obj = object_class_by_name(driver);
 
413
        }
 
414
    }
 
415
 
 
416
    if (!obj) {
 
417
        qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
 
418
        return NULL;
 
419
    }
 
420
 
 
421
    k = DEVICE_CLASS(obj);
 
422
 
 
423
    /* find bus */
 
424
    path = qemu_opt_get(opts, "bus");
 
425
    if (path != NULL) {
 
426
        bus = qbus_find(path);
 
427
        if (!bus) {
 
428
            return NULL;
 
429
        }
 
430
        if (bus->info != k->bus_info) {
 
431
            qerror_report(QERR_BAD_BUS_FOR_DEVICE,
 
432
                           driver, bus->info->name);
 
433
            return NULL;
 
434
        }
 
435
    } else {
 
436
        bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_info);
 
437
        if (!bus) {
 
438
            qerror_report(QERR_NO_BUS_FOR_DEVICE,
 
439
                          driver, k->bus_info->name);
 
440
            return NULL;
 
441
        }
 
442
    }
 
443
    if (qdev_hotplug && !bus->allow_hotplug) {
 
444
        qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
 
445
        return NULL;
 
446
    }
 
447
 
 
448
    if (!bus) {
 
449
        bus = sysbus_get_default();
 
450
    }
 
451
 
 
452
    /* create device, set properties */
 
453
    qdev = DEVICE(object_new(driver));
 
454
    qdev_set_parent_bus(qdev, bus);
 
455
    qdev_prop_set_globals(qdev);
 
456
 
 
457
    id = qemu_opts_id(opts);
 
458
    if (id) {
 
459
        qdev->id = id;
 
460
    }
 
461
    if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
 
462
        qdev_free(qdev);
 
463
        return NULL;
 
464
    }
 
465
    if (qdev_init(qdev) < 0) {
 
466
        qerror_report(QERR_DEVICE_INIT_FAILED, driver);
 
467
        return NULL;
 
468
    }
 
469
    if (qdev->id) {
 
470
        object_property_add_child(qdev_get_peripheral(), qdev->id,
 
471
                                  OBJECT(qdev), NULL);
 
472
    } else {
 
473
        static int anon_count;
 
474
        gchar *name = g_strdup_printf("device[%d]", anon_count++);
 
475
        object_property_add_child(qdev_get_peripheral_anon(), name,
 
476
                                  OBJECT(qdev), NULL);
 
477
        g_free(name);
 
478
    }        
 
479
    qdev->opts = opts;
 
480
    return qdev;
 
481
}
 
482
 
 
483
 
 
484
#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
 
485
static void qbus_print(Monitor *mon, BusState *bus, int indent);
 
486
 
 
487
static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
 
488
                             const char *prefix, int indent)
 
489
{
 
490
    if (!props)
 
491
        return;
 
492
    for (; props->name; props++) {
 
493
        Error *err = NULL;
 
494
        char *value;
 
495
        char *legacy_name = g_strdup_printf("legacy-%s", props->name);
 
496
        if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
 
497
            value = object_property_get_str(OBJECT(dev), legacy_name, &err);
 
498
        } else {
 
499
            value = object_property_get_str(OBJECT(dev), props->name, &err);
 
500
        }
 
501
        g_free(legacy_name);
 
502
 
 
503
        if (err) {
 
504
            error_free(err);
 
505
            continue;
 
506
        }
 
507
        qdev_printf("%s-prop: %s = %s\n", prefix, props->name,
 
508
                    value && *value ? value : "<null>");
 
509
        g_free(value);
 
510
    }
 
511
}
 
512
 
 
513
static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
 
514
{
 
515
    BusState *child;
 
516
    qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
 
517
                dev->id ? dev->id : "");
 
518
    indent += 2;
 
519
    if (dev->num_gpio_in) {
 
520
        qdev_printf("gpio-in %d\n", dev->num_gpio_in);
 
521
    }
 
522
    if (dev->num_gpio_out) {
 
523
        qdev_printf("gpio-out %d\n", dev->num_gpio_out);
 
524
    }
 
525
    qdev_print_props(mon, dev, qdev_get_props(dev), "dev", indent);
 
526
    qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
 
527
    if (dev->parent_bus->info->print_dev)
 
528
        dev->parent_bus->info->print_dev(mon, dev, indent);
 
529
    QLIST_FOREACH(child, &dev->child_bus, sibling) {
 
530
        qbus_print(mon, child, indent);
 
531
    }
 
532
}
 
533
 
 
534
static void qbus_print(Monitor *mon, BusState *bus, int indent)
 
535
{
 
536
    struct DeviceState *dev;
 
537
 
 
538
    qdev_printf("bus: %s\n", bus->name);
 
539
    indent += 2;
 
540
    qdev_printf("type %s\n", bus->info->name);
 
541
    QTAILQ_FOREACH(dev, &bus->children, sibling) {
 
542
        qdev_print(mon, dev, indent);
 
543
    }
 
544
}
 
545
#undef qdev_printf
 
546
 
 
547
void do_info_qtree(Monitor *mon)
 
548
{
 
549
    if (sysbus_get_default())
 
550
        qbus_print(mon, sysbus_get_default(), 0);
 
551
}
 
552
 
 
553
void do_info_qdm(Monitor *mon)
 
554
{
 
555
    object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
 
556
}
 
557
 
 
558
int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
 
559
{
 
560
    QemuOpts *opts;
 
561
 
 
562
    opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
 
563
    if (!opts) {
 
564
        return -1;
 
565
    }
 
566
    if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
 
567
        qemu_opts_del(opts);
 
568
        return 0;
 
569
    }
 
570
    if (!qdev_device_add(opts)) {
 
571
        qemu_opts_del(opts);
 
572
        return -1;
 
573
    }
 
574
    return 0;
 
575
}
 
576
 
 
577
int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
 
578
{
 
579
    const char *id = qdict_get_str(qdict, "id");
 
580
    DeviceState *dev;
 
581
 
 
582
    dev = qdev_find_recursive(sysbus_get_default(), id);
 
583
    if (NULL == dev) {
 
584
        qerror_report(QERR_DEVICE_NOT_FOUND, id);
 
585
        return -1;
 
586
    }
 
587
    return qdev_unplug(dev);
 
588
}
 
589
 
 
590
void qdev_machine_init(void)
 
591
{
 
592
    qdev_get_peripheral_anon();
 
593
    qdev_get_peripheral();
 
594
}