Statistics
| Branch: | Revision:

root / hw / qdev-monitor.c @ c06aaf01

History | View | Annotate | Download (16.1 kB)

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