xref: /qemu/hw/core/qdev-properties-system.c (revision 5551449b)
1 /*
2  * qdev property parsing
3  * (parts specific for qemu-system-*)
4  *
5  * This file is based on code from hw/qdev-properties.c from
6  * commit 074a86fccd185616469dfcdc0e157f438aebba18,
7  * Copyright (c) Gerd Hoffmann <kraxel@redhat.com> and other contributors.
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 #include "qemu/osdep.h"
14 #include "hw/qdev-properties.h"
15 #include "hw/qdev-properties-system.h"
16 #include "qapi/error.h"
17 #include "qapi/visitor.h"
18 #include "qapi/qapi-types-block.h"
19 #include "qapi/qapi-types-machine.h"
20 #include "qapi/qapi-types-migration.h"
21 #include "qapi/qapi-visit-virtio.h"
22 #include "qapi/qmp/qerror.h"
23 #include "qemu/ctype.h"
24 #include "qemu/cutils.h"
25 #include "qemu/units.h"
26 #include "qemu/uuid.h"
27 #include "qemu/error-report.h"
28 #include "qdev-prop-internal.h"
29 
30 #include "audio/audio.h"
31 #include "chardev/char-fe.h"
32 #include "sysemu/block-backend.h"
33 #include "sysemu/blockdev.h"
34 #include "net/net.h"
35 #include "hw/pci/pci.h"
36 #include "hw/pci/pcie.h"
37 #include "hw/i386/x86.h"
38 #include "util/block-helpers.h"
39 
check_prop_still_unset(Object * obj,const char * name,const void * old_val,const char * new_val,bool allow_override,Error ** errp)40 static bool check_prop_still_unset(Object *obj, const char *name,
41                                    const void *old_val, const char *new_val,
42                                    bool allow_override, Error **errp)
43 {
44     const GlobalProperty *prop = qdev_find_global_prop(obj, name);
45 
46     if (!old_val || (!prop && allow_override)) {
47         return true;
48     }
49 
50     if (prop) {
51         error_setg(errp, "-global %s.%s=... conflicts with %s=%s",
52                    prop->driver, prop->property, name, new_val);
53     } else {
54         /* Error message is vague, but a better one would be hard */
55         error_setg(errp, "%s=%s conflicts, and override is not implemented",
56                    name, new_val);
57     }
58     return false;
59 }
60 
61 
62 /* --- drive --- */
63 
get_drive(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)64 static void get_drive(Object *obj, Visitor *v, const char *name, void *opaque,
65                       Error **errp)
66 {
67     Property *prop = opaque;
68     void **ptr = object_field_prop_ptr(obj, prop);
69     const char *value;
70     char *p;
71 
72     if (*ptr) {
73         value = blk_name(*ptr);
74         if (!*value) {
75             BlockDriverState *bs = blk_bs(*ptr);
76             if (bs) {
77                 value = bdrv_get_node_name(bs);
78             }
79         }
80     } else {
81         value = "";
82     }
83 
84     p = g_strdup(value);
85     visit_type_str(v, name, &p, errp);
86     g_free(p);
87 }
88 
set_drive_helper(Object * obj,Visitor * v,const char * name,void * opaque,bool iothread,Error ** errp)89 static void set_drive_helper(Object *obj, Visitor *v, const char *name,
90                              void *opaque, bool iothread, Error **errp)
91 {
92     DeviceState *dev = DEVICE(obj);
93     Property *prop = opaque;
94     void **ptr = object_field_prop_ptr(obj, prop);
95     char *str;
96     BlockBackend *blk;
97     bool blk_created = false;
98     int ret;
99     BlockDriverState *bs;
100     AioContext *ctx;
101 
102     if (!visit_type_str(v, name, &str, errp)) {
103         return;
104     }
105 
106     if (!check_prop_still_unset(obj, name, *ptr, str, true, errp)) {
107         return;
108     }
109 
110     if (*ptr) {
111         /* BlockBackend already exists. So, we want to change attached node */
112         blk = *ptr;
113         ctx = blk_get_aio_context(blk);
114         bs = bdrv_lookup_bs(NULL, str, errp);
115         if (!bs) {
116             return;
117         }
118 
119         if (ctx != bdrv_get_aio_context(bs)) {
120             error_setg(errp, "Different aio context is not supported for new "
121                        "node");
122         }
123 
124         blk_replace_bs(blk, bs, errp);
125         return;
126     }
127 
128     if (!*str) {
129         g_free(str);
130         *ptr = NULL;
131         return;
132     }
133 
134     blk = blk_by_name(str);
135     if (!blk) {
136         bs = bdrv_lookup_bs(NULL, str, NULL);
137         if (bs) {
138             /*
139              * If the device supports iothreads, it will make sure to move the
140              * block node to the right AioContext if necessary (or fail if this
141              * isn't possible because of other users). Devices that are not
142              * aware of iothreads require their BlockBackends to be in the main
143              * AioContext.
144              */
145             ctx = bdrv_get_aio_context(bs);
146             blk = blk_new(iothread ? ctx : qemu_get_aio_context(),
147                           0, BLK_PERM_ALL);
148             blk_created = true;
149 
150             ret = blk_insert_bs(blk, bs, errp);
151             if (ret < 0) {
152                 goto fail;
153             }
154         }
155     }
156     if (!blk) {
157         error_setg(errp, "Property '%s.%s' can't find value '%s'",
158                    object_get_typename(OBJECT(dev)), name, str);
159         goto fail;
160     }
161     if (blk_attach_dev(blk, dev) < 0) {
162         DriveInfo *dinfo = blk_legacy_dinfo(blk);
163 
164         if (dinfo && dinfo->type != IF_NONE) {
165             error_setg(errp, "Drive '%s' is already in use because "
166                        "it has been automatically connected to another "
167                        "device (did you need 'if=none' in the drive options?)",
168                        str);
169         } else {
170             error_setg(errp, "Drive '%s' is already in use by another device",
171                        str);
172         }
173         goto fail;
174     }
175 
176     *ptr = blk;
177 
178 fail:
179     if (blk_created) {
180         /* If we need to keep a reference, blk_attach_dev() took it */
181         blk_unref(blk);
182     }
183 
184     g_free(str);
185 }
186 
set_drive(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)187 static void set_drive(Object *obj, Visitor *v, const char *name, void *opaque,
188                       Error **errp)
189 {
190     set_drive_helper(obj, v, name, opaque, false, errp);
191 }
192 
set_drive_iothread(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)193 static void set_drive_iothread(Object *obj, Visitor *v, const char *name,
194                                void *opaque, Error **errp)
195 {
196     set_drive_helper(obj, v, name, opaque, true, errp);
197 }
198 
release_drive(Object * obj,const char * name,void * opaque)199 static void release_drive(Object *obj, const char *name, void *opaque)
200 {
201     DeviceState *dev = DEVICE(obj);
202     Property *prop = opaque;
203     BlockBackend **ptr = object_field_prop_ptr(obj, prop);
204 
205     if (*ptr) {
206         blockdev_auto_del(*ptr);
207         blk_detach_dev(*ptr, dev);
208     }
209 }
210 
211 const PropertyInfo qdev_prop_drive = {
212     .name  = "str",
213     .description = "Node name or ID of a block device to use as a backend",
214     .realized_set_allowed = true,
215     .get   = get_drive,
216     .set   = set_drive,
217     .release = release_drive,
218 };
219 
220 const PropertyInfo qdev_prop_drive_iothread = {
221     .name  = "str",
222     .description = "Node name or ID of a block device to use as a backend",
223     .realized_set_allowed = true,
224     .get   = get_drive,
225     .set   = set_drive_iothread,
226     .release = release_drive,
227 };
228 
229 /* --- character device --- */
230 
get_chr(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)231 static void get_chr(Object *obj, Visitor *v, const char *name, void *opaque,
232                     Error **errp)
233 {
234     CharBackend *be = object_field_prop_ptr(obj, opaque);
235     char *p;
236 
237     p = g_strdup(be->chr && be->chr->label ? be->chr->label : "");
238     visit_type_str(v, name, &p, errp);
239     g_free(p);
240 }
241 
set_chr(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)242 static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,
243                     Error **errp)
244 {
245     ERRP_GUARD();
246     Property *prop = opaque;
247     CharBackend *be = object_field_prop_ptr(obj, prop);
248     Chardev *s;
249     char *str;
250 
251     if (!visit_type_str(v, name, &str, errp)) {
252         return;
253     }
254 
255     /*
256      * TODO Should this really be an error?  If no, the old value
257      * needs to be released before we store the new one.
258      */
259     if (!check_prop_still_unset(obj, name, be->chr, str, false, errp)) {
260         return;
261     }
262 
263     if (!*str) {
264         g_free(str);
265         be->chr = NULL;
266         return;
267     }
268 
269     s = qemu_chr_find(str);
270     if (s == NULL) {
271         error_setg(errp, "Property '%s.%s' can't find value '%s'",
272                    object_get_typename(obj), name, str);
273     } else if (!qemu_chr_fe_init(be, s, errp)) {
274         error_prepend(errp, "Property '%s.%s' can't take value '%s': ",
275                       object_get_typename(obj), name, str);
276     }
277     g_free(str);
278 }
279 
release_chr(Object * obj,const char * name,void * opaque)280 static void release_chr(Object *obj, const char *name, void *opaque)
281 {
282     Property *prop = opaque;
283     CharBackend *be = object_field_prop_ptr(obj, prop);
284 
285     qemu_chr_fe_deinit(be, false);
286 }
287 
288 const PropertyInfo qdev_prop_chr = {
289     .name  = "str",
290     .description = "ID of a chardev to use as a backend",
291     .get   = get_chr,
292     .set   = set_chr,
293     .release = release_chr,
294 };
295 
296 /* --- mac address --- */
297 
298 /*
299  * accepted syntax versions:
300  *   01:02:03:04:05:06
301  *   01-02-03-04-05-06
302  */
get_mac(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)303 static void get_mac(Object *obj, Visitor *v, const char *name, void *opaque,
304                     Error **errp)
305 {
306     Property *prop = opaque;
307     MACAddr *mac = object_field_prop_ptr(obj, prop);
308     char buffer[2 * 6 + 5 + 1];
309     char *p = buffer;
310 
311     snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
312              mac->a[0], mac->a[1], mac->a[2],
313              mac->a[3], mac->a[4], mac->a[5]);
314 
315     visit_type_str(v, name, &p, errp);
316 }
317 
set_mac(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)318 static void set_mac(Object *obj, Visitor *v, const char *name, void *opaque,
319                     Error **errp)
320 {
321     Property *prop = opaque;
322     MACAddr *mac = object_field_prop_ptr(obj, prop);
323     int i, pos;
324     char *str;
325     const char *p;
326 
327     if (!visit_type_str(v, name, &str, errp)) {
328         return;
329     }
330 
331     for (i = 0, pos = 0; i < 6; i++, pos += 3) {
332         long val;
333 
334         if (!qemu_isxdigit(str[pos])) {
335             goto inval;
336         }
337         if (!qemu_isxdigit(str[pos + 1])) {
338             goto inval;
339         }
340         if (i == 5) {
341             if (str[pos + 2] != '\0') {
342                 goto inval;
343             }
344         } else {
345             if (str[pos + 2] != ':' && str[pos + 2] != '-') {
346                 goto inval;
347             }
348         }
349         if (qemu_strtol(str + pos, &p, 16, &val) < 0 || val > 0xff) {
350             goto inval;
351         }
352         mac->a[i] = val;
353     }
354     g_free(str);
355     return;
356 
357 inval:
358     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
359     g_free(str);
360 }
361 
362 const PropertyInfo qdev_prop_macaddr = {
363     .name  = "str",
364     .description = "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
365     .get   = get_mac,
366     .set   = set_mac,
367 };
368 
qdev_prop_set_macaddr(DeviceState * dev,const char * name,const uint8_t * value)369 void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
370                            const uint8_t *value)
371 {
372     char str[2 * 6 + 5 + 1];
373     snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
374              value[0], value[1], value[2], value[3], value[4], value[5]);
375 
376     object_property_set_str(OBJECT(dev), name, str, &error_abort);
377 }
378 
379 /* --- netdev device --- */
get_netdev(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)380 static void get_netdev(Object *obj, Visitor *v, const char *name,
381                        void *opaque, Error **errp)
382 {
383     Property *prop = opaque;
384     NICPeers *peers_ptr = object_field_prop_ptr(obj, prop);
385     char *p = g_strdup(peers_ptr->ncs[0] ? peers_ptr->ncs[0]->name : "");
386 
387     visit_type_str(v, name, &p, errp);
388     g_free(p);
389 }
390 
set_netdev(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)391 static void set_netdev(Object *obj, Visitor *v, const char *name,
392                        void *opaque, Error **errp)
393 {
394     Property *prop = opaque;
395     NICPeers *peers_ptr = object_field_prop_ptr(obj, prop);
396     NetClientState **ncs = peers_ptr->ncs;
397     NetClientState *peers[MAX_QUEUE_NUM];
398     int queues, err = 0, i = 0;
399     char *str;
400 
401     if (!visit_type_str(v, name, &str, errp)) {
402         return;
403     }
404 
405     queues = qemu_find_net_clients_except(str, peers,
406                                           NET_CLIENT_DRIVER_NIC,
407                                           MAX_QUEUE_NUM);
408     if (queues == 0) {
409         err = -ENOENT;
410         goto out;
411     }
412 
413     if (queues > MAX_QUEUE_NUM) {
414         error_setg(errp, "queues of backend '%s'(%d) exceeds QEMU limitation(%d)",
415                    str, queues, MAX_QUEUE_NUM);
416         goto out;
417     }
418 
419     for (i = 0; i < queues; i++) {
420         if (peers[i]->peer) {
421             err = -EEXIST;
422             goto out;
423         }
424 
425         /*
426          * TODO Should this really be an error?  If no, the old value
427          * needs to be released before we store the new one.
428          */
429         if (!check_prop_still_unset(obj, name, ncs[i], str, false, errp)) {
430             goto out;
431         }
432 
433         if (peers[i]->info->check_peer_type) {
434             if (!peers[i]->info->check_peer_type(peers[i], obj->class, errp)) {
435                 goto out;
436             }
437         }
438 
439         ncs[i] = peers[i];
440         ncs[i]->queue_index = i;
441     }
442 
443     peers_ptr->queues = queues;
444 
445 out:
446     error_set_from_qdev_prop_error(errp, err, obj, prop->name, str);
447     g_free(str);
448 }
449 
450 const PropertyInfo qdev_prop_netdev = {
451     .name  = "str",
452     .description = "ID of a netdev to use as a backend",
453     .get   = get_netdev,
454     .set   = set_netdev,
455 };
456 
457 
458 /* --- audiodev --- */
get_audiodev(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)459 static void get_audiodev(Object *obj, Visitor *v, const char* name,
460                          void *opaque, Error **errp)
461 {
462     Property *prop = opaque;
463     QEMUSoundCard *card = object_field_prop_ptr(obj, prop);
464     char *p = g_strdup(audio_get_id(card));
465 
466     visit_type_str(v, name, &p, errp);
467     g_free(p);
468 }
469 
set_audiodev(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)470 static void set_audiodev(Object *obj, Visitor *v, const char* name,
471                          void *opaque, Error **errp)
472 {
473     Property *prop = opaque;
474     QEMUSoundCard *card = object_field_prop_ptr(obj, prop);
475     AudioState *state;
476     g_autofree char *str = NULL;
477 
478     if (!visit_type_str(v, name, &str, errp)) {
479         return;
480     }
481 
482     state = audio_state_by_name(str, errp);
483     if (state) {
484         card->state = state;
485     }
486 }
487 
488 const PropertyInfo qdev_prop_audiodev = {
489     .name = "str",
490     .description = "ID of an audiodev to use as a backend",
491     /* release done on shutdown */
492     .get = get_audiodev,
493     .set = set_audiodev,
494 };
495 
qdev_prop_set_drive_err(DeviceState * dev,const char * name,BlockBackend * value,Error ** errp)496 bool qdev_prop_set_drive_err(DeviceState *dev, const char *name,
497                              BlockBackend *value, Error **errp)
498 {
499     const char *ref = "";
500 
501     if (value) {
502         ref = blk_name(value);
503         if (!*ref) {
504             const BlockDriverState *bs = blk_bs(value);
505             if (bs) {
506                 ref = bdrv_get_node_name(bs);
507             }
508         }
509     }
510 
511     return object_property_set_str(OBJECT(dev), name, ref, errp);
512 }
513 
qdev_prop_set_drive(DeviceState * dev,const char * name,BlockBackend * value)514 void qdev_prop_set_drive(DeviceState *dev, const char *name,
515                          BlockBackend *value)
516 {
517     qdev_prop_set_drive_err(dev, name, value, &error_abort);
518 }
519 
qdev_prop_set_chr(DeviceState * dev,const char * name,Chardev * value)520 void qdev_prop_set_chr(DeviceState *dev, const char *name,
521                        Chardev *value)
522 {
523     assert(!value || value->label);
524     object_property_set_str(OBJECT(dev), name, value ? value->label : "",
525                             &error_abort);
526 }
527 
qdev_prop_set_netdev(DeviceState * dev,const char * name,NetClientState * value)528 void qdev_prop_set_netdev(DeviceState *dev, const char *name,
529                           NetClientState *value)
530 {
531     assert(!value || value->name);
532     object_property_set_str(OBJECT(dev), name, value ? value->name : "",
533                             &error_abort);
534 }
535 
qdev_set_nic_properties(DeviceState * dev,NICInfo * nd)536 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
537 {
538     qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
539     if (nd->netdev) {
540         qdev_prop_set_netdev(dev, "netdev", nd->netdev);
541     }
542     if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
543         object_property_find(OBJECT(dev), "vectors")) {
544         qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
545     }
546     nd->instantiated = 1;
547 }
548 
549 /* --- lost tick policy --- */
550 
qdev_propinfo_set_losttickpolicy(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)551 static void qdev_propinfo_set_losttickpolicy(Object *obj, Visitor *v,
552                                              const char *name, void *opaque,
553                                              Error **errp)
554 {
555     Property *prop = opaque;
556     int *ptr = object_field_prop_ptr(obj, prop);
557     int value;
558 
559     if (!visit_type_enum(v, name, &value, prop->info->enum_table, errp)) {
560         return;
561     }
562 
563     if (value == LOST_TICK_POLICY_SLEW) {
564         MachineState *ms = MACHINE(qdev_get_machine());
565 
566         if (!object_dynamic_cast(OBJECT(ms), TYPE_X86_MACHINE)) {
567             error_setg(errp,
568                        "the 'slew' policy is only available for x86 machines");
569             return;
570         }
571     }
572 
573     *ptr = value;
574 }
575 
576 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
577 
578 const PropertyInfo qdev_prop_losttickpolicy = {
579     .name  = "LostTickPolicy",
580     .enum_table  = &LostTickPolicy_lookup,
581     .get   = qdev_propinfo_get_enum,
582     .set   = qdev_propinfo_set_losttickpolicy,
583     .set_default_value = qdev_propinfo_set_default_value_enum,
584 };
585 
586 /* --- blocksize --- */
587 
set_blocksize(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)588 static void set_blocksize(Object *obj, Visitor *v, const char *name,
589                           void *opaque, Error **errp)
590 {
591     Property *prop = opaque;
592     uint32_t *ptr = object_field_prop_ptr(obj, prop);
593     uint64_t value;
594 
595     if (!visit_type_size(v, name, &value, errp)) {
596         return;
597     }
598     if (!check_block_size(name, value, errp)) {
599         return;
600     }
601     *ptr = value;
602 }
603 
604 const PropertyInfo qdev_prop_blocksize = {
605     .name  = "size",
606     .description = "A power of two between " MIN_BLOCK_SIZE_STR
607                    " and " MAX_BLOCK_SIZE_STR,
608     .get   = qdev_propinfo_get_size32,
609     .set   = set_blocksize,
610     .set_default_value = qdev_propinfo_set_default_value_uint,
611 };
612 
613 /* --- Block device error handling policy --- */
614 
615 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError) != sizeof(int));
616 
617 const PropertyInfo qdev_prop_blockdev_on_error = {
618     .name = "BlockdevOnError",
619     .description = "Error handling policy, "
620                    "report/ignore/enospc/stop/auto",
621     .enum_table = &BlockdevOnError_lookup,
622     .get = qdev_propinfo_get_enum,
623     .set = qdev_propinfo_set_enum,
624     .set_default_value = qdev_propinfo_set_default_value_enum,
625 };
626 
627 /* --- BIOS CHS translation */
628 
629 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int));
630 
631 const PropertyInfo qdev_prop_bios_chs_trans = {
632     .name = "BiosAtaTranslation",
633     .description = "Logical CHS translation algorithm, "
634                    "auto/none/lba/large/rechs",
635     .enum_table = &BiosAtaTranslation_lookup,
636     .get = qdev_propinfo_get_enum,
637     .set = qdev_propinfo_set_enum,
638     .set_default_value = qdev_propinfo_set_default_value_enum,
639 };
640 
641 /* --- FDC default drive types */
642 
643 const PropertyInfo qdev_prop_fdc_drive_type = {
644     .name = "FdcDriveType",
645     .description = "FDC drive type, "
646                    "144/288/120/none/auto",
647     .enum_table = &FloppyDriveType_lookup,
648     .get = qdev_propinfo_get_enum,
649     .set = qdev_propinfo_set_enum,
650     .set_default_value = qdev_propinfo_set_default_value_enum,
651 };
652 
653 /* --- MultiFDCompression --- */
654 
655 const PropertyInfo qdev_prop_multifd_compression = {
656     .name = "MultiFDCompression",
657     .description = "multifd_compression values, "
658                    "none/zlib/zstd/qpl/uadk/qatzip",
659     .enum_table = &MultiFDCompression_lookup,
660     .get = qdev_propinfo_get_enum,
661     .set = qdev_propinfo_set_enum,
662     .set_default_value = qdev_propinfo_set_default_value_enum,
663 };
664 
665 /* --- MigMode --- */
666 
667 QEMU_BUILD_BUG_ON(sizeof(MigMode) != sizeof(int));
668 
669 const PropertyInfo qdev_prop_mig_mode = {
670     .name = "MigMode",
671     .description = "mig_mode values, "
672                    "normal,cpr-reboot",
673     .enum_table = &MigMode_lookup,
674     .get = qdev_propinfo_get_enum,
675     .set = qdev_propinfo_set_enum,
676     .set_default_value = qdev_propinfo_set_default_value_enum,
677 };
678 
679 /* --- GranuleMode --- */
680 
681 QEMU_BUILD_BUG_ON(sizeof(GranuleMode) != sizeof(int));
682 
683 const PropertyInfo qdev_prop_granule_mode = {
684     .name = "GranuleMode",
685     .description = "granule_mode values, "
686                    "4k, 8k, 16k, 64k, host",
687     .enum_table = &GranuleMode_lookup,
688     .get = qdev_propinfo_get_enum,
689     .set = qdev_propinfo_set_enum,
690     .set_default_value = qdev_propinfo_set_default_value_enum,
691 };
692 
693 const PropertyInfo qdev_prop_zero_page_detection = {
694     .name = "ZeroPageDetection",
695     .description = "zero_page_detection values, "
696                    "none,legacy,multifd",
697     .enum_table = &ZeroPageDetection_lookup,
698     .get = qdev_propinfo_get_enum,
699     .set = qdev_propinfo_set_enum,
700     .set_default_value = qdev_propinfo_set_default_value_enum,
701 };
702 
703 /* --- Reserved Region --- */
704 
705 /*
706  * Accepted syntax:
707  *   <low address>:<high address>:<type>
708  *   where low/high addresses are uint64_t in hexadecimal
709  *   and type is a non-negative decimal integer
710  */
get_reserved_region(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)711 static void get_reserved_region(Object *obj, Visitor *v, const char *name,
712                                 void *opaque, Error **errp)
713 {
714     Property *prop = opaque;
715     ReservedRegion *rr = object_field_prop_ptr(obj, prop);
716     char buffer[64];
717     char *p = buffer;
718     int rc;
719 
720     rc = snprintf(buffer, sizeof(buffer), "0x%"PRIx64":0x%"PRIx64":%u",
721                   range_lob(&rr->range), range_upb(&rr->range), rr->type);
722     assert(rc < sizeof(buffer));
723 
724     visit_type_str(v, name, &p, errp);
725 }
726 
set_reserved_region(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)727 static void set_reserved_region(Object *obj, Visitor *v, const char *name,
728                                 void *opaque, Error **errp)
729 {
730     Property *prop = opaque;
731     ReservedRegion *rr = object_field_prop_ptr(obj, prop);
732     const char *endptr;
733     uint64_t lob, upb;
734     char *str;
735     int ret;
736 
737     if (!visit_type_str(v, name, &str, errp)) {
738         return;
739     }
740 
741     ret = qemu_strtou64(str, &endptr, 16, &lob);
742     if (ret) {
743         error_setg(errp, "start address of '%s'"
744                    " must be a hexadecimal integer", name);
745         goto out;
746     }
747     if (*endptr != ':') {
748         goto separator_error;
749     }
750 
751     ret = qemu_strtou64(endptr + 1, &endptr, 16, &upb);
752     if (ret) {
753         error_setg(errp, "end address of '%s'"
754                    " must be a hexadecimal integer", name);
755         goto out;
756     }
757     if (*endptr != ':') {
758         goto separator_error;
759     }
760 
761     range_set_bounds(&rr->range, lob, upb);
762 
763     ret = qemu_strtoui(endptr + 1, &endptr, 10, &rr->type);
764     if (ret) {
765         error_setg(errp, "type of '%s'"
766                    " must be a non-negative decimal integer", name);
767     }
768     goto out;
769 
770 separator_error:
771     error_setg(errp, "reserved region fields must be separated with ':'");
772 out:
773     g_free(str);
774     return;
775 }
776 
777 const PropertyInfo qdev_prop_reserved_region = {
778     .name  = "reserved_region",
779     .description = "Reserved Region, example: 0xFEE00000:0xFEEFFFFF:0",
780     .get   = get_reserved_region,
781     .set   = set_reserved_region,
782 };
783 
784 /* --- pci address --- */
785 
786 /*
787  * bus-local address, i.e. "$slot" or "$slot.$fn"
788  */
set_pci_devfn(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)789 static void set_pci_devfn(Object *obj, Visitor *v, const char *name,
790                           void *opaque, Error **errp)
791 {
792     Property *prop = opaque;
793     int32_t value, *ptr = object_field_prop_ptr(obj, prop);
794     unsigned int slot, fn, n;
795     char *str;
796 
797     if (!visit_type_str(v, name, &str, NULL)) {
798         if (!visit_type_int32(v, name, &value, errp)) {
799             return;
800         }
801         if (value < -1 || value > 255) {
802             error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
803                        name ? name : "null", "a value between -1 and 255");
804             return;
805         }
806         *ptr = value;
807         return;
808     }
809 
810     if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
811         fn = 0;
812         if (sscanf(str, "%x%n", &slot, &n) != 1) {
813             goto invalid;
814         }
815     }
816     if (str[n] != '\0' || fn > 7 || slot > 31) {
817         goto invalid;
818     }
819     *ptr = slot << 3 | fn;
820     g_free(str);
821     return;
822 
823 invalid:
824     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
825     g_free(str);
826 }
827 
print_pci_devfn(Object * obj,Property * prop,char * dest,size_t len)828 static int print_pci_devfn(Object *obj, Property *prop, char *dest,
829                            size_t len)
830 {
831     int32_t *ptr = object_field_prop_ptr(obj, prop);
832 
833     if (*ptr == -1) {
834         return snprintf(dest, len, "<unset>");
835     } else {
836         return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
837     }
838 }
839 
840 const PropertyInfo qdev_prop_pci_devfn = {
841     .name  = "int32",
842     .description = "Slot and optional function number, example: 06.0 or 06",
843     .print = print_pci_devfn,
844     .get   = qdev_propinfo_get_int32,
845     .set   = set_pci_devfn,
846     .set_default_value = qdev_propinfo_set_default_value_int,
847 };
848 
849 /* --- pci host address --- */
850 
get_pci_host_devaddr(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)851 static void get_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
852                                  void *opaque, Error **errp)
853 {
854     Property *prop = opaque;
855     PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
856     char buffer[] = "ffff:ff:ff.f";
857     char *p = buffer;
858     int rc = 0;
859 
860     /*
861      * Catch "invalid" device reference from vfio-pci and allow the
862      * default buffer representing the non-existent device to be used.
863      */
864     if (~addr->domain || ~addr->bus || ~addr->slot || ~addr->function) {
865         rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%0d",
866                       addr->domain, addr->bus, addr->slot, addr->function);
867         assert(rc == sizeof(buffer) - 1);
868     }
869 
870     visit_type_str(v, name, &p, errp);
871 }
872 
873 /*
874  * Parse [<domain>:]<bus>:<slot>.<func>
875  *   if <domain> is not supplied, it's assumed to be 0.
876  */
set_pci_host_devaddr(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)877 static void set_pci_host_devaddr(Object *obj, Visitor *v, const char *name,
878                                  void *opaque, Error **errp)
879 {
880     Property *prop = opaque;
881     PCIHostDeviceAddress *addr = object_field_prop_ptr(obj, prop);
882     char *str, *p;
883     char *e;
884     unsigned long val;
885     unsigned long dom = 0, bus = 0;
886     unsigned int slot = 0, func = 0;
887 
888     if (!visit_type_str(v, name, &str, errp)) {
889         return;
890     }
891 
892     p = str;
893     val = strtoul(p, &e, 16);
894     if (e == p || *e != ':') {
895         goto inval;
896     }
897     bus = val;
898 
899     p = e + 1;
900     val = strtoul(p, &e, 16);
901     if (e == p) {
902         goto inval;
903     }
904     if (*e == ':') {
905         dom = bus;
906         bus = val;
907         p = e + 1;
908         val = strtoul(p, &e, 16);
909         if (e == p) {
910             goto inval;
911         }
912     }
913     slot = val;
914 
915     if (*e != '.') {
916         goto inval;
917     }
918     p = e + 1;
919     val = strtoul(p, &e, 10);
920     if (e == p) {
921         goto inval;
922     }
923     func = val;
924 
925     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
926         goto inval;
927     }
928 
929     if (*e) {
930         goto inval;
931     }
932 
933     addr->domain = dom;
934     addr->bus = bus;
935     addr->slot = slot;
936     addr->function = func;
937 
938     g_free(str);
939     return;
940 
941 inval:
942     error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
943     g_free(str);
944 }
945 
946 const PropertyInfo qdev_prop_pci_host_devaddr = {
947     .name = "str",
948     .description = "Address (bus/device/function) of "
949                    "the host device, example: 04:10.0",
950     .get = get_pci_host_devaddr,
951     .set = set_pci_host_devaddr,
952 };
953 
954 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
955 
956 const PropertyInfo qdev_prop_off_auto_pcibar = {
957     .name = "OffAutoPCIBAR",
958     .description = "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
959     .enum_table = &OffAutoPCIBAR_lookup,
960     .get = qdev_propinfo_get_enum,
961     .set = qdev_propinfo_set_enum,
962     .set_default_value = qdev_propinfo_set_default_value_enum,
963 };
964 
965 /* --- PCIELinkSpeed 2_5/5/8/16/32/64 -- */
966 
get_prop_pcielinkspeed(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)967 static void get_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
968                                    void *opaque, Error **errp)
969 {
970     Property *prop = opaque;
971     PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
972     int speed;
973 
974     switch (*p) {
975     case QEMU_PCI_EXP_LNK_2_5GT:
976         speed = PCIE_LINK_SPEED_2_5;
977         break;
978     case QEMU_PCI_EXP_LNK_5GT:
979         speed = PCIE_LINK_SPEED_5;
980         break;
981     case QEMU_PCI_EXP_LNK_8GT:
982         speed = PCIE_LINK_SPEED_8;
983         break;
984     case QEMU_PCI_EXP_LNK_16GT:
985         speed = PCIE_LINK_SPEED_16;
986         break;
987     case QEMU_PCI_EXP_LNK_32GT:
988         speed = PCIE_LINK_SPEED_32;
989         break;
990     case QEMU_PCI_EXP_LNK_64GT:
991         speed = PCIE_LINK_SPEED_64;
992         break;
993     default:
994         /* Unreachable */
995         abort();
996     }
997 
998     visit_type_enum(v, name, &speed, prop->info->enum_table, errp);
999 }
1000 
set_prop_pcielinkspeed(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1001 static void set_prop_pcielinkspeed(Object *obj, Visitor *v, const char *name,
1002                                    void *opaque, Error **errp)
1003 {
1004     Property *prop = opaque;
1005     PCIExpLinkSpeed *p = object_field_prop_ptr(obj, prop);
1006     int speed;
1007 
1008     if (!visit_type_enum(v, name, &speed, prop->info->enum_table,
1009                          errp)) {
1010         return;
1011     }
1012 
1013     switch (speed) {
1014     case PCIE_LINK_SPEED_2_5:
1015         *p = QEMU_PCI_EXP_LNK_2_5GT;
1016         break;
1017     case PCIE_LINK_SPEED_5:
1018         *p = QEMU_PCI_EXP_LNK_5GT;
1019         break;
1020     case PCIE_LINK_SPEED_8:
1021         *p = QEMU_PCI_EXP_LNK_8GT;
1022         break;
1023     case PCIE_LINK_SPEED_16:
1024         *p = QEMU_PCI_EXP_LNK_16GT;
1025         break;
1026     case PCIE_LINK_SPEED_32:
1027         *p = QEMU_PCI_EXP_LNK_32GT;
1028         break;
1029     case PCIE_LINK_SPEED_64:
1030         *p = QEMU_PCI_EXP_LNK_64GT;
1031         break;
1032     default:
1033         /* Unreachable */
1034         abort();
1035     }
1036 }
1037 
1038 const PropertyInfo qdev_prop_pcie_link_speed = {
1039     .name = "PCIELinkSpeed",
1040     .description = "2_5/5/8/16/32/64",
1041     .enum_table = &PCIELinkSpeed_lookup,
1042     .get = get_prop_pcielinkspeed,
1043     .set = set_prop_pcielinkspeed,
1044     .set_default_value = qdev_propinfo_set_default_value_enum,
1045 };
1046 
1047 /* --- PCIELinkWidth 1/2/4/8/12/16/32 -- */
1048 
get_prop_pcielinkwidth(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1049 static void get_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1050                                    void *opaque, Error **errp)
1051 {
1052     Property *prop = opaque;
1053     PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
1054     int width;
1055 
1056     switch (*p) {
1057     case QEMU_PCI_EXP_LNK_X1:
1058         width = PCIE_LINK_WIDTH_1;
1059         break;
1060     case QEMU_PCI_EXP_LNK_X2:
1061         width = PCIE_LINK_WIDTH_2;
1062         break;
1063     case QEMU_PCI_EXP_LNK_X4:
1064         width = PCIE_LINK_WIDTH_4;
1065         break;
1066     case QEMU_PCI_EXP_LNK_X8:
1067         width = PCIE_LINK_WIDTH_8;
1068         break;
1069     case QEMU_PCI_EXP_LNK_X12:
1070         width = PCIE_LINK_WIDTH_12;
1071         break;
1072     case QEMU_PCI_EXP_LNK_X16:
1073         width = PCIE_LINK_WIDTH_16;
1074         break;
1075     case QEMU_PCI_EXP_LNK_X32:
1076         width = PCIE_LINK_WIDTH_32;
1077         break;
1078     default:
1079         /* Unreachable */
1080         abort();
1081     }
1082 
1083     visit_type_enum(v, name, &width, prop->info->enum_table, errp);
1084 }
1085 
set_prop_pcielinkwidth(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1086 static void set_prop_pcielinkwidth(Object *obj, Visitor *v, const char *name,
1087                                    void *opaque, Error **errp)
1088 {
1089     Property *prop = opaque;
1090     PCIExpLinkWidth *p = object_field_prop_ptr(obj, prop);
1091     int width;
1092 
1093     if (!visit_type_enum(v, name, &width, prop->info->enum_table,
1094                          errp)) {
1095         return;
1096     }
1097 
1098     switch (width) {
1099     case PCIE_LINK_WIDTH_1:
1100         *p = QEMU_PCI_EXP_LNK_X1;
1101         break;
1102     case PCIE_LINK_WIDTH_2:
1103         *p = QEMU_PCI_EXP_LNK_X2;
1104         break;
1105     case PCIE_LINK_WIDTH_4:
1106         *p = QEMU_PCI_EXP_LNK_X4;
1107         break;
1108     case PCIE_LINK_WIDTH_8:
1109         *p = QEMU_PCI_EXP_LNK_X8;
1110         break;
1111     case PCIE_LINK_WIDTH_12:
1112         *p = QEMU_PCI_EXP_LNK_X12;
1113         break;
1114     case PCIE_LINK_WIDTH_16:
1115         *p = QEMU_PCI_EXP_LNK_X16;
1116         break;
1117     case PCIE_LINK_WIDTH_32:
1118         *p = QEMU_PCI_EXP_LNK_X32;
1119         break;
1120     default:
1121         /* Unreachable */
1122         abort();
1123     }
1124 }
1125 
1126 const PropertyInfo qdev_prop_pcie_link_width = {
1127     .name = "PCIELinkWidth",
1128     .description = "1/2/4/8/12/16/32",
1129     .enum_table = &PCIELinkWidth_lookup,
1130     .get = get_prop_pcielinkwidth,
1131     .set = set_prop_pcielinkwidth,
1132     .set_default_value = qdev_propinfo_set_default_value_enum,
1133 };
1134 
1135 /* --- UUID --- */
1136 
get_uuid(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1137 static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1138                      Error **errp)
1139 {
1140     Property *prop = opaque;
1141     QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1142     char buffer[UUID_STR_LEN];
1143     char *p = buffer;
1144 
1145     qemu_uuid_unparse(uuid, buffer);
1146 
1147     visit_type_str(v, name, &p, errp);
1148 }
1149 
1150 #define UUID_VALUE_AUTO        "auto"
1151 
set_uuid(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1152 static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,
1153                     Error **errp)
1154 {
1155     Property *prop = opaque;
1156     QemuUUID *uuid = object_field_prop_ptr(obj, prop);
1157     char *str;
1158 
1159     if (!visit_type_str(v, name, &str, errp)) {
1160         return;
1161     }
1162 
1163     if (!strcmp(str, UUID_VALUE_AUTO)) {
1164         qemu_uuid_generate(uuid);
1165     } else if (qemu_uuid_parse(str, uuid) < 0) {
1166         error_set_from_qdev_prop_error(errp, EINVAL, obj, name, str);
1167     }
1168     g_free(str);
1169 }
1170 
set_default_uuid_auto(ObjectProperty * op,const Property * prop)1171 static void set_default_uuid_auto(ObjectProperty *op, const Property *prop)
1172 {
1173     object_property_set_default_str(op, UUID_VALUE_AUTO);
1174 }
1175 
1176 const PropertyInfo qdev_prop_uuid = {
1177     .name  = "str",
1178     .description = "UUID (aka GUID) or \"" UUID_VALUE_AUTO
1179         "\" for random value (default)",
1180     .get   = get_uuid,
1181     .set   = set_uuid,
1182     .set_default_value = set_default_uuid_auto,
1183 };
1184 
1185 /* --- s390 cpu entitlement policy --- */
1186 
1187 QEMU_BUILD_BUG_ON(sizeof(S390CpuEntitlement) != sizeof(int));
1188 
1189 const PropertyInfo qdev_prop_cpus390entitlement = {
1190     .name  = "S390CpuEntitlement",
1191     .description = "low/medium (default)/high",
1192     .enum_table  = &S390CpuEntitlement_lookup,
1193     .get   = qdev_propinfo_get_enum,
1194     .set   = qdev_propinfo_set_enum,
1195     .set_default_value = qdev_propinfo_set_default_value_enum,
1196 };
1197 
1198 /* --- IOThreadVirtQueueMappingList --- */
1199 
get_iothread_vq_mapping_list(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1200 static void get_iothread_vq_mapping_list(Object *obj, Visitor *v,
1201         const char *name, void *opaque, Error **errp)
1202 {
1203     IOThreadVirtQueueMappingList **prop_ptr =
1204         object_field_prop_ptr(obj, opaque);
1205 
1206     visit_type_IOThreadVirtQueueMappingList(v, name, prop_ptr, errp);
1207 }
1208 
set_iothread_vq_mapping_list(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)1209 static void set_iothread_vq_mapping_list(Object *obj, Visitor *v,
1210         const char *name, void *opaque, Error **errp)
1211 {
1212     IOThreadVirtQueueMappingList **prop_ptr =
1213         object_field_prop_ptr(obj, opaque);
1214     IOThreadVirtQueueMappingList *list;
1215 
1216     if (!visit_type_IOThreadVirtQueueMappingList(v, name, &list, errp)) {
1217         return;
1218     }
1219 
1220     qapi_free_IOThreadVirtQueueMappingList(*prop_ptr);
1221     *prop_ptr = list;
1222 }
1223 
release_iothread_vq_mapping_list(Object * obj,const char * name,void * opaque)1224 static void release_iothread_vq_mapping_list(Object *obj,
1225         const char *name, void *opaque)
1226 {
1227     IOThreadVirtQueueMappingList **prop_ptr =
1228         object_field_prop_ptr(obj, opaque);
1229 
1230     qapi_free_IOThreadVirtQueueMappingList(*prop_ptr);
1231     *prop_ptr = NULL;
1232 }
1233 
1234 const PropertyInfo qdev_prop_iothread_vq_mapping_list = {
1235     .name = "IOThreadVirtQueueMappingList",
1236     .description = "IOThread virtqueue mapping list [{\"iothread\":\"<id>\", "
1237                    "\"vqs\":[1,2,3,...]},...]",
1238     .get = get_iothread_vq_mapping_list,
1239     .set = set_iothread_vq_mapping_list,
1240     .release = release_iothread_vq_mapping_list,
1241 };
1242