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