1 #include "qemu/osdep.h"
4 #include "qapi/error.h"
5 #include "hw/pci/pci.h"
6 #include "qapi/qmp/qerror.h"
7 #include "qemu/error-report.h"
8 #include "hw/block/block.h"
10 #include "qapi/visitor.h"
11 #include "chardev/char.h"
12 #include "qemu/uuid.h"
14 void qdev_prop_set_after_realize(DeviceState
*dev
, const char *name
,
18 error_setg(errp
, "Attempt to set property '%s' on device '%s' "
19 "(type '%s') after it was realized", name
, dev
->id
,
20 object_get_typename(OBJECT(dev
)));
22 error_setg(errp
, "Attempt to set property '%s' on anonymous device "
23 "(type '%s') after it was realized", name
,
24 object_get_typename(OBJECT(dev
)));
28 void qdev_prop_allow_set_link_before_realize(const Object
*obj
,
30 Object
*val
, Error
**errp
)
32 DeviceState
*dev
= DEVICE(obj
);
35 error_setg(errp
, "Attempt to set link property '%s' on device '%s' "
36 "(type '%s') after it was realized",
37 name
, dev
->id
, object_get_typename(obj
));
41 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
48 static void get_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
51 DeviceState
*dev
= DEVICE(obj
);
52 Property
*prop
= opaque
;
53 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
55 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
58 static void set_enum(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
61 DeviceState
*dev
= DEVICE(obj
);
62 Property
*prop
= opaque
;
63 int *ptr
= qdev_get_prop_ptr(dev
, prop
);
66 qdev_prop_set_after_realize(dev
, name
, errp
);
70 visit_type_enum(v
, prop
->name
, ptr
, prop
->info
->enum_table
, errp
);
73 static void set_default_value_enum(Object
*obj
, const Property
*prop
)
75 object_property_set_str(obj
,
76 qapi_enum_lookup(prop
->info
->enum_table
,
78 prop
->name
, &error_abort
);
83 static uint32_t qdev_get_prop_mask(Property
*prop
)
85 assert(prop
->info
== &qdev_prop_bit
);
86 return 0x1 << prop
->bitnr
;
89 static void bit_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
91 uint32_t *p
= qdev_get_prop_ptr(dev
, props
);
92 uint32_t mask
= qdev_get_prop_mask(props
);
100 static void prop_get_bit(Object
*obj
, Visitor
*v
, const char *name
,
101 void *opaque
, Error
**errp
)
103 DeviceState
*dev
= DEVICE(obj
);
104 Property
*prop
= opaque
;
105 uint32_t *p
= qdev_get_prop_ptr(dev
, prop
);
106 bool value
= (*p
& qdev_get_prop_mask(prop
)) != 0;
108 visit_type_bool(v
, name
, &value
, errp
);
111 static void prop_set_bit(Object
*obj
, Visitor
*v
, const char *name
,
112 void *opaque
, Error
**errp
)
114 DeviceState
*dev
= DEVICE(obj
);
115 Property
*prop
= opaque
;
116 Error
*local_err
= NULL
;
120 qdev_prop_set_after_realize(dev
, name
, errp
);
124 visit_type_bool(v
, name
, &value
, &local_err
);
126 error_propagate(errp
, local_err
);
129 bit_prop_set(dev
, prop
, value
);
132 static void set_default_value_bool(Object
*obj
, const Property
*prop
)
134 object_property_set_bool(obj
, prop
->defval
.u
, prop
->name
, &error_abort
);
137 const PropertyInfo qdev_prop_bit
= {
139 .description
= "on/off",
142 .set_default_value
= set_default_value_bool
,
147 static uint64_t qdev_get_prop_mask64(Property
*prop
)
149 assert(prop
->info
== &qdev_prop_bit64
);
150 return 0x1ull
<< prop
->bitnr
;
153 static void bit64_prop_set(DeviceState
*dev
, Property
*props
, bool val
)
155 uint64_t *p
= qdev_get_prop_ptr(dev
, props
);
156 uint64_t mask
= qdev_get_prop_mask64(props
);
164 static void prop_get_bit64(Object
*obj
, Visitor
*v
, const char *name
,
165 void *opaque
, Error
**errp
)
167 DeviceState
*dev
= DEVICE(obj
);
168 Property
*prop
= opaque
;
169 uint64_t *p
= qdev_get_prop_ptr(dev
, prop
);
170 bool value
= (*p
& qdev_get_prop_mask64(prop
)) != 0;
172 visit_type_bool(v
, name
, &value
, errp
);
175 static void prop_set_bit64(Object
*obj
, Visitor
*v
, const char *name
,
176 void *opaque
, Error
**errp
)
178 DeviceState
*dev
= DEVICE(obj
);
179 Property
*prop
= opaque
;
180 Error
*local_err
= NULL
;
184 qdev_prop_set_after_realize(dev
, name
, errp
);
188 visit_type_bool(v
, name
, &value
, &local_err
);
190 error_propagate(errp
, local_err
);
193 bit64_prop_set(dev
, prop
, value
);
196 const PropertyInfo qdev_prop_bit64
= {
198 .description
= "on/off",
199 .get
= prop_get_bit64
,
200 .set
= prop_set_bit64
,
201 .set_default_value
= set_default_value_bool
,
206 static void get_bool(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
209 DeviceState
*dev
= DEVICE(obj
);
210 Property
*prop
= opaque
;
211 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
213 visit_type_bool(v
, name
, ptr
, errp
);
216 static void set_bool(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
219 DeviceState
*dev
= DEVICE(obj
);
220 Property
*prop
= opaque
;
221 bool *ptr
= qdev_get_prop_ptr(dev
, prop
);
224 qdev_prop_set_after_realize(dev
, name
, errp
);
228 visit_type_bool(v
, name
, ptr
, errp
);
231 const PropertyInfo qdev_prop_bool
= {
235 .set_default_value
= set_default_value_bool
,
238 /* --- 8bit integer --- */
240 static void get_uint8(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
243 DeviceState
*dev
= DEVICE(obj
);
244 Property
*prop
= opaque
;
245 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
247 visit_type_uint8(v
, name
, ptr
, errp
);
250 static void set_uint8(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
253 DeviceState
*dev
= DEVICE(obj
);
254 Property
*prop
= opaque
;
255 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
258 qdev_prop_set_after_realize(dev
, name
, errp
);
262 visit_type_uint8(v
, name
, ptr
, errp
);
265 static void set_default_value_int(Object
*obj
, const Property
*prop
)
267 object_property_set_int(obj
, prop
->defval
.i
, prop
->name
, &error_abort
);
270 static void set_default_value_uint(Object
*obj
, const Property
*prop
)
272 object_property_set_uint(obj
, prop
->defval
.u
, prop
->name
, &error_abort
);
275 const PropertyInfo qdev_prop_uint8
= {
279 .set_default_value
= set_default_value_uint
,
282 /* --- 16bit integer --- */
284 static void get_uint16(Object
*obj
, Visitor
*v
, const char *name
,
285 void *opaque
, Error
**errp
)
287 DeviceState
*dev
= DEVICE(obj
);
288 Property
*prop
= opaque
;
289 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
291 visit_type_uint16(v
, name
, ptr
, errp
);
294 static void set_uint16(Object
*obj
, Visitor
*v
, const char *name
,
295 void *opaque
, Error
**errp
)
297 DeviceState
*dev
= DEVICE(obj
);
298 Property
*prop
= opaque
;
299 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
302 qdev_prop_set_after_realize(dev
, name
, errp
);
306 visit_type_uint16(v
, name
, ptr
, errp
);
309 const PropertyInfo qdev_prop_uint16
= {
313 .set_default_value
= set_default_value_uint
,
316 /* --- 32bit integer --- */
318 static void get_uint32(Object
*obj
, Visitor
*v
, const char *name
,
319 void *opaque
, Error
**errp
)
321 DeviceState
*dev
= DEVICE(obj
);
322 Property
*prop
= opaque
;
323 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
325 visit_type_uint32(v
, name
, ptr
, errp
);
328 static void set_uint32(Object
*obj
, Visitor
*v
, const char *name
,
329 void *opaque
, Error
**errp
)
331 DeviceState
*dev
= DEVICE(obj
);
332 Property
*prop
= opaque
;
333 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
336 qdev_prop_set_after_realize(dev
, name
, errp
);
340 visit_type_uint32(v
, name
, ptr
, errp
);
343 static void get_int32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
346 DeviceState
*dev
= DEVICE(obj
);
347 Property
*prop
= opaque
;
348 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
350 visit_type_int32(v
, name
, ptr
, errp
);
353 static void set_int32(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
356 DeviceState
*dev
= DEVICE(obj
);
357 Property
*prop
= opaque
;
358 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
361 qdev_prop_set_after_realize(dev
, name
, errp
);
365 visit_type_int32(v
, name
, ptr
, errp
);
368 const PropertyInfo qdev_prop_uint32
= {
372 .set_default_value
= set_default_value_uint
,
375 const PropertyInfo qdev_prop_int32
= {
379 .set_default_value
= set_default_value_int
,
382 /* --- 64bit integer --- */
384 static void get_uint64(Object
*obj
, Visitor
*v
, const char *name
,
385 void *opaque
, Error
**errp
)
387 DeviceState
*dev
= DEVICE(obj
);
388 Property
*prop
= opaque
;
389 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
391 visit_type_uint64(v
, name
, ptr
, errp
);
394 static void set_uint64(Object
*obj
, Visitor
*v
, const char *name
,
395 void *opaque
, Error
**errp
)
397 DeviceState
*dev
= DEVICE(obj
);
398 Property
*prop
= opaque
;
399 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
402 qdev_prop_set_after_realize(dev
, name
, errp
);
406 visit_type_uint64(v
, name
, ptr
, errp
);
409 static void get_int64(Object
*obj
, Visitor
*v
, const char *name
,
410 void *opaque
, Error
**errp
)
412 DeviceState
*dev
= DEVICE(obj
);
413 Property
*prop
= opaque
;
414 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
416 visit_type_int64(v
, name
, ptr
, errp
);
419 static void set_int64(Object
*obj
, Visitor
*v
, const char *name
,
420 void *opaque
, Error
**errp
)
422 DeviceState
*dev
= DEVICE(obj
);
423 Property
*prop
= opaque
;
424 int64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
427 qdev_prop_set_after_realize(dev
, name
, errp
);
431 visit_type_int64(v
, name
, ptr
, errp
);
434 const PropertyInfo qdev_prop_uint64
= {
438 .set_default_value
= set_default_value_uint
,
441 const PropertyInfo qdev_prop_int64
= {
445 .set_default_value
= set_default_value_int
,
450 static void release_string(Object
*obj
, const char *name
, void *opaque
)
452 Property
*prop
= opaque
;
453 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj
), prop
));
456 static void get_string(Object
*obj
, Visitor
*v
, const char *name
,
457 void *opaque
, Error
**errp
)
459 DeviceState
*dev
= DEVICE(obj
);
460 Property
*prop
= opaque
;
461 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
464 char *str
= (char *)"";
465 visit_type_str(v
, name
, &str
, errp
);
467 visit_type_str(v
, name
, ptr
, errp
);
471 static void set_string(Object
*obj
, Visitor
*v
, const char *name
,
472 void *opaque
, Error
**errp
)
474 DeviceState
*dev
= DEVICE(obj
);
475 Property
*prop
= opaque
;
476 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
477 Error
*local_err
= NULL
;
481 qdev_prop_set_after_realize(dev
, name
, errp
);
485 visit_type_str(v
, name
, &str
, &local_err
);
487 error_propagate(errp
, local_err
);
494 const PropertyInfo qdev_prop_string
= {
496 .release
= release_string
,
501 /* --- pointer --- */
503 /* Not a proper property, just for dirty hacks. TODO Remove it! */
504 const PropertyInfo qdev_prop_ptr
= {
508 /* --- mac address --- */
511 * accepted syntax versions:
515 static void get_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
518 DeviceState
*dev
= DEVICE(obj
);
519 Property
*prop
= opaque
;
520 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
521 char buffer
[2 * 6 + 5 + 1];
524 snprintf(buffer
, sizeof(buffer
), "%02x:%02x:%02x:%02x:%02x:%02x",
525 mac
->a
[0], mac
->a
[1], mac
->a
[2],
526 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
528 visit_type_str(v
, name
, &p
, errp
);
531 static void set_mac(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
534 DeviceState
*dev
= DEVICE(obj
);
535 Property
*prop
= opaque
;
536 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
537 Error
*local_err
= NULL
;
542 qdev_prop_set_after_realize(dev
, name
, errp
);
546 visit_type_str(v
, name
, &str
, &local_err
);
548 error_propagate(errp
, local_err
);
552 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
553 if (!qemu_isxdigit(str
[pos
])) {
556 if (!qemu_isxdigit(str
[pos
+1])) {
560 if (str
[pos
+2] != '\0') {
564 if (str
[pos
+2] != ':' && str
[pos
+2] != '-') {
568 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
574 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
578 const PropertyInfo qdev_prop_macaddr
= {
580 .description
= "Ethernet 6-byte MAC Address, example: 52:54:00:12:34:56",
585 /* --- on/off/auto --- */
587 const PropertyInfo qdev_prop_on_off_auto
= {
589 .description
= "on/off/auto",
590 .enum_table
= &OnOffAuto_lookup
,
593 .set_default_value
= set_default_value_enum
,
596 /* --- lost tick policy --- */
598 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy
) != sizeof(int));
600 const PropertyInfo qdev_prop_losttickpolicy
= {
601 .name
= "LostTickPolicy",
602 .enum_table
= &LostTickPolicy_lookup
,
605 .set_default_value
= set_default_value_enum
,
608 /* --- Block device error handling policy --- */
610 QEMU_BUILD_BUG_ON(sizeof(BlockdevOnError
) != sizeof(int));
612 const PropertyInfo qdev_prop_blockdev_on_error
= {
613 .name
= "BlockdevOnError",
614 .description
= "Error handling policy, "
615 "report/ignore/enospc/stop/auto",
616 .enum_table
= &BlockdevOnError_lookup
,
619 .set_default_value
= set_default_value_enum
,
622 /* --- BIOS CHS translation */
624 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation
) != sizeof(int));
626 const PropertyInfo qdev_prop_bios_chs_trans
= {
627 .name
= "BiosAtaTranslation",
628 .description
= "Logical CHS translation algorithm, "
629 "auto/none/lba/large/rechs",
630 .enum_table
= &BiosAtaTranslation_lookup
,
633 .set_default_value
= set_default_value_enum
,
636 /* --- FDC default drive types */
638 const PropertyInfo qdev_prop_fdc_drive_type
= {
639 .name
= "FdcDriveType",
640 .description
= "FDC drive type, "
641 "144/288/120/none/auto",
642 .enum_table
= &FloppyDriveType_lookup
,
645 .set_default_value
= set_default_value_enum
,
648 /* --- pci address --- */
651 * bus-local address, i.e. "$slot" or "$slot.$fn"
653 static void set_pci_devfn(Object
*obj
, Visitor
*v
, const char *name
,
654 void *opaque
, Error
**errp
)
656 DeviceState
*dev
= DEVICE(obj
);
657 Property
*prop
= opaque
;
658 int32_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
659 unsigned int slot
, fn
, n
;
660 Error
*local_err
= NULL
;
664 qdev_prop_set_after_realize(dev
, name
, errp
);
668 visit_type_str(v
, name
, &str
, &local_err
);
670 error_free(local_err
);
672 visit_type_int32(v
, name
, &value
, &local_err
);
674 error_propagate(errp
, local_err
);
675 } else if (value
< -1 || value
> 255) {
676 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
677 name
? name
: "null", "pci_devfn");
684 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
686 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
690 if (str
[n
] != '\0' || fn
> 7 || slot
> 31) {
693 *ptr
= slot
<< 3 | fn
;
698 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
702 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
,
705 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
708 return snprintf(dest
, len
, "<unset>");
710 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
714 const PropertyInfo qdev_prop_pci_devfn
= {
716 .description
= "Slot and optional function number, example: 06.0 or 06",
717 .print
= print_pci_devfn
,
719 .set
= set_pci_devfn
,
720 .set_default_value
= set_default_value_int
,
723 /* --- blocksize --- */
725 static void set_blocksize(Object
*obj
, Visitor
*v
, const char *name
,
726 void *opaque
, Error
**errp
)
728 DeviceState
*dev
= DEVICE(obj
);
729 Property
*prop
= opaque
;
730 uint16_t value
, *ptr
= qdev_get_prop_ptr(dev
, prop
);
731 Error
*local_err
= NULL
;
732 const int64_t min
= 512;
733 const int64_t max
= 32768;
736 qdev_prop_set_after_realize(dev
, name
, errp
);
740 visit_type_uint16(v
, name
, &value
, &local_err
);
742 error_propagate(errp
, local_err
);
745 /* value of 0 means "unset" */
746 if (value
&& (value
< min
|| value
> max
)) {
747 error_setg(errp
, QERR_PROPERTY_VALUE_OUT_OF_RANGE
,
748 dev
->id
? : "", name
, (int64_t)value
, min
, max
);
752 /* We rely on power-of-2 blocksizes for bitmasks */
753 if ((value
& (value
- 1)) != 0) {
755 "Property %s.%s doesn't take value '%" PRId64
"', it's not a power of 2",
756 dev
->id
?: "", name
, (int64_t)value
);
763 const PropertyInfo qdev_prop_blocksize
= {
765 .description
= "A power of two between 512 and 32768",
767 .set
= set_blocksize
,
768 .set_default_value
= set_default_value_uint
,
771 /* --- pci host address --- */
773 static void get_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
774 void *opaque
, Error
**errp
)
776 DeviceState
*dev
= DEVICE(obj
);
777 Property
*prop
= opaque
;
778 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
779 char buffer
[] = "ffff:ff:ff.f";
784 * Catch "invalid" device reference from vfio-pci and allow the
785 * default buffer representing the non-existent device to be used.
787 if (~addr
->domain
|| ~addr
->bus
|| ~addr
->slot
|| ~addr
->function
) {
788 rc
= snprintf(buffer
, sizeof(buffer
), "%04x:%02x:%02x.%0d",
789 addr
->domain
, addr
->bus
, addr
->slot
, addr
->function
);
790 assert(rc
== sizeof(buffer
) - 1);
793 visit_type_str(v
, name
, &p
, errp
);
797 * Parse [<domain>:]<bus>:<slot>.<func>
798 * if <domain> is not supplied, it's assumed to be 0.
800 static void set_pci_host_devaddr(Object
*obj
, Visitor
*v
, const char *name
,
801 void *opaque
, Error
**errp
)
803 DeviceState
*dev
= DEVICE(obj
);
804 Property
*prop
= opaque
;
805 PCIHostDeviceAddress
*addr
= qdev_get_prop_ptr(dev
, prop
);
806 Error
*local_err
= NULL
;
810 unsigned long dom
= 0, bus
= 0;
811 unsigned int slot
= 0, func
= 0;
814 qdev_prop_set_after_realize(dev
, name
, errp
);
818 visit_type_str(v
, name
, &str
, &local_err
);
820 error_propagate(errp
, local_err
);
825 val
= strtoul(p
, &e
, 16);
826 if (e
== p
|| *e
!= ':') {
832 val
= strtoul(p
, &e
, 16);
840 val
= strtoul(p
, &e
, 16);
851 val
= strtoul(p
, &e
, 10);
857 if (dom
> 0xffff || bus
> 0xff || slot
> 0x1f || func
> 7) {
868 addr
->function
= func
;
874 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
878 const PropertyInfo qdev_prop_pci_host_devaddr
= {
880 .description
= "Address (bus/device/function) of "
881 "the host device, example: 04:10.0",
882 .get
= get_pci_host_devaddr
,
883 .set
= set_pci_host_devaddr
,
888 static void get_uuid(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
891 DeviceState
*dev
= DEVICE(obj
);
892 Property
*prop
= opaque
;
893 QemuUUID
*uuid
= qdev_get_prop_ptr(dev
, prop
);
894 char buffer
[UUID_FMT_LEN
+ 1];
897 qemu_uuid_unparse(uuid
, buffer
);
899 visit_type_str(v
, name
, &p
, errp
);
902 #define UUID_VALUE_AUTO "auto"
904 static void set_uuid(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
907 DeviceState
*dev
= DEVICE(obj
);
908 Property
*prop
= opaque
;
909 QemuUUID
*uuid
= qdev_get_prop_ptr(dev
, prop
);
910 Error
*local_err
= NULL
;
914 qdev_prop_set_after_realize(dev
, name
, errp
);
918 visit_type_str(v
, name
, &str
, &local_err
);
920 error_propagate(errp
, local_err
);
924 if (!strcmp(str
, UUID_VALUE_AUTO
)) {
925 qemu_uuid_generate(uuid
);
926 } else if (qemu_uuid_parse(str
, uuid
) < 0) {
927 error_set_from_qdev_prop_error(errp
, EINVAL
, dev
, prop
, str
);
932 static void set_default_uuid_auto(Object
*obj
, const Property
*prop
)
934 object_property_set_str(obj
, UUID_VALUE_AUTO
, prop
->name
, &error_abort
);
937 const PropertyInfo qdev_prop_uuid
= {
939 .description
= "UUID (aka GUID) or \"" UUID_VALUE_AUTO
940 "\" for random value (default)",
943 .set_default_value
= set_default_uuid_auto
,
946 /* --- support for array properties --- */
948 /* Used as an opaque for the object properties we add for each
949 * array element. Note that the struct Property must be first
950 * in the struct so that a pointer to this works as the opaque
951 * for the underlying element's property hooks as well as for
952 * our own release callback.
955 struct Property prop
;
957 ObjectPropertyRelease
*release
;
958 } ArrayElementProperty
;
960 /* object property release callback for array element properties:
961 * we call the underlying element's property release hook, and
962 * then free the memory we allocated when we added the property.
964 static void array_element_release(Object
*obj
, const char *name
, void *opaque
)
966 ArrayElementProperty
*p
= opaque
;
968 p
->release(obj
, name
, opaque
);
974 static void set_prop_arraylen(Object
*obj
, Visitor
*v
, const char *name
,
975 void *opaque
, Error
**errp
)
977 /* Setter for the property which defines the length of a
978 * variable-sized property array. As well as actually setting the
979 * array-length field in the device struct, we have to create the
980 * array itself and dynamically add the corresponding properties.
982 DeviceState
*dev
= DEVICE(obj
);
983 Property
*prop
= opaque
;
984 uint32_t *alenptr
= qdev_get_prop_ptr(dev
, prop
);
985 void **arrayptr
= (void *)dev
+ prop
->arrayoffset
;
986 Error
*local_err
= NULL
;
988 const char *arrayname
;
992 qdev_prop_set_after_realize(dev
, name
, errp
);
996 error_setg(errp
, "array size property %s may not be set more than once",
1000 visit_type_uint32(v
, name
, alenptr
, &local_err
);
1002 error_propagate(errp
, local_err
);
1009 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix;
1010 * strip it off so we can get the name of the array itself.
1012 assert(strncmp(name
, PROP_ARRAY_LEN_PREFIX
,
1013 strlen(PROP_ARRAY_LEN_PREFIX
)) == 0);
1014 arrayname
= name
+ strlen(PROP_ARRAY_LEN_PREFIX
);
1016 /* Note that it is the responsibility of the individual device's deinit
1017 * to free the array proper.
1019 *arrayptr
= eltptr
= g_malloc0(*alenptr
* prop
->arrayfieldsize
);
1020 for (i
= 0; i
< *alenptr
; i
++, eltptr
+= prop
->arrayfieldsize
) {
1021 char *propname
= g_strdup_printf("%s[%d]", arrayname
, i
);
1022 ArrayElementProperty
*arrayprop
= g_new0(ArrayElementProperty
, 1);
1023 arrayprop
->release
= prop
->arrayinfo
->release
;
1024 arrayprop
->propname
= propname
;
1025 arrayprop
->prop
.info
= prop
->arrayinfo
;
1026 arrayprop
->prop
.name
= propname
;
1027 /* This ugly piece of pointer arithmetic sets up the offset so
1028 * that when the underlying get/set hooks call qdev_get_prop_ptr
1029 * they get the right answer despite the array element not actually
1030 * being inside the device struct.
1032 arrayprop
->prop
.offset
= eltptr
- (void *)dev
;
1033 assert(qdev_get_prop_ptr(dev
, &arrayprop
->prop
) == eltptr
);
1034 object_property_add(obj
, propname
,
1035 arrayprop
->prop
.info
->name
,
1036 arrayprop
->prop
.info
->get
,
1037 arrayprop
->prop
.info
->set
,
1038 array_element_release
,
1039 arrayprop
, &local_err
);
1041 error_propagate(errp
, local_err
);
1047 const PropertyInfo qdev_prop_arraylen
= {
1050 .set
= set_prop_arraylen
,
1051 .set_default_value
= set_default_value_uint
,
1054 /* --- public helpers --- */
1056 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
1061 while (props
->name
) {
1062 if (strcmp(props
->name
, name
) == 0) {
1070 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
1075 /* device properties */
1076 class = object_get_class(OBJECT(dev
));
1078 prop
= qdev_prop_walk(DEVICE_CLASS(class)->props
, name
);
1082 class = object_class_get_parent(class);
1083 } while (class != object_class_by_name(TYPE_DEVICE
));
1088 void error_set_from_qdev_prop_error(Error
**errp
, int ret
, DeviceState
*dev
,
1089 Property
*prop
, const char *value
)
1093 error_setg(errp
, "Property '%s.%s' can't take value '%s', it's in use",
1094 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1098 error_setg(errp
, QERR_PROPERTY_VALUE_BAD
,
1099 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1102 error_setg(errp
, "Property '%s.%s' can't find value '%s'",
1103 object_get_typename(OBJECT(dev
)), prop
->name
, value
);
1110 void qdev_prop_set_bit(DeviceState
*dev
, const char *name
, bool value
)
1112 object_property_set_bool(OBJECT(dev
), value
, name
, &error_abort
);
1115 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
1117 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1120 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
1122 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1125 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
1127 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1130 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
1132 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1135 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
1137 object_property_set_int(OBJECT(dev
), value
, name
, &error_abort
);
1140 void qdev_prop_set_string(DeviceState
*dev
, const char *name
, const char *value
)
1142 object_property_set_str(OBJECT(dev
), value
, name
, &error_abort
);
1145 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
,
1146 const uint8_t *value
)
1148 char str
[2 * 6 + 5 + 1];
1149 snprintf(str
, sizeof(str
), "%02x:%02x:%02x:%02x:%02x:%02x",
1150 value
[0], value
[1], value
[2], value
[3], value
[4], value
[5]);
1152 object_property_set_str(OBJECT(dev
), str
, name
, &error_abort
);
1155 void qdev_prop_set_enum(DeviceState
*dev
, const char *name
, int value
)
1159 prop
= qdev_prop_find(dev
, name
);
1160 object_property_set_str(OBJECT(dev
),
1161 qapi_enum_lookup(prop
->info
->enum_table
, value
),
1162 name
, &error_abort
);
1165 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
1170 prop
= qdev_prop_find(dev
, name
);
1171 assert(prop
&& prop
->info
== &qdev_prop_ptr
);
1172 ptr
= qdev_get_prop_ptr(dev
, prop
);
1176 static GList
*global_props
;
1178 void qdev_prop_register_global(GlobalProperty
*prop
)
1180 global_props
= g_list_append(global_props
, prop
);
1183 void register_compat_prop(const char *driver
,
1184 const char *property
,
1187 GlobalProperty
*p
= g_new0(GlobalProperty
, 1);
1189 /* Any compat_props must never cause error */
1190 p
->errp
= &error_abort
;
1192 p
->property
= property
;
1194 qdev_prop_register_global(p
);
1197 void register_compat_props_array(GlobalProperty
*prop
)
1199 for (; prop
&& prop
->driver
; prop
++) {
1200 register_compat_prop(prop
->driver
, prop
->property
, prop
->value
);
1204 void qdev_prop_register_global_list(GlobalProperty
*props
)
1208 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
1209 qdev_prop_register_global(props
+i
);
1213 int qdev_prop_check_globals(void)
1218 for (l
= global_props
; l
; l
= l
->next
) {
1219 GlobalProperty
*prop
= l
->data
;
1225 if (!prop
->user_provided
) {
1228 oc
= object_class_by_name(prop
->driver
);
1229 oc
= object_class_dynamic_cast(oc
, TYPE_DEVICE
);
1231 warn_report("global %s.%s has invalid class name",
1232 prop
->driver
, prop
->property
);
1236 dc
= DEVICE_CLASS(oc
);
1237 if (!dc
->hotpluggable
&& !prop
->used
) {
1238 warn_report("global %s.%s=%s not used",
1239 prop
->driver
, prop
->property
, prop
->value
);
1247 void qdev_prop_set_globals(DeviceState
*dev
)
1251 for (l
= global_props
; l
; l
= l
->next
) {
1252 GlobalProperty
*prop
= l
->data
;
1255 if (object_dynamic_cast(OBJECT(dev
), prop
->driver
) == NULL
) {
1259 object_property_parse(OBJECT(dev
), prop
->value
, prop
->property
, &err
);
1261 error_prepend(&err
, "can't apply global %s.%s=%s: ",
1262 prop
->driver
, prop
->property
, prop
->value
);
1263 if (!dev
->hotplugged
&& prop
->errp
) {
1264 error_propagate(prop
->errp
, err
);
1266 assert(prop
->user_provided
);
1267 warn_report_err(err
);
1273 /* --- 64bit unsigned int 'size' type --- */
1275 static void get_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1278 DeviceState
*dev
= DEVICE(obj
);
1279 Property
*prop
= opaque
;
1280 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1282 visit_type_size(v
, name
, ptr
, errp
);
1285 static void set_size(Object
*obj
, Visitor
*v
, const char *name
, void *opaque
,
1288 DeviceState
*dev
= DEVICE(obj
);
1289 Property
*prop
= opaque
;
1290 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1292 visit_type_size(v
, name
, ptr
, errp
);
1295 const PropertyInfo qdev_prop_size
= {
1299 .set_default_value
= set_default_value_uint
,
1302 /* --- object link property --- */
1304 static void create_link_property(Object
*obj
, Property
*prop
, Error
**errp
)
1306 Object
**child
= qdev_get_prop_ptr(DEVICE(obj
), prop
);
1308 object_property_add_link(obj
, prop
->name
, prop
->link_type
,
1310 qdev_prop_allow_set_link_before_realize
,
1311 OBJ_PROP_LINK_STRONG
,
1315 const PropertyInfo qdev_prop_link
= {
1317 .create
= create_link_property
,
1320 /* --- OffAutoPCIBAR off/auto/bar0/bar1/bar2/bar3/bar4/bar5 --- */
1322 const PropertyInfo qdev_prop_off_auto_pcibar
= {
1323 .name
= "OffAutoPCIBAR",
1324 .description
= "off/auto/bar0/bar1/bar2/bar3/bar4/bar5",
1325 .enum_table
= &OffAutoPCIBAR_lookup
,
1328 .set_default_value
= set_default_value_enum
,