1 #include "net/net.h" 2 #include "hw/qdev.h" 3 #include "qapi/qmp/qerror.h" 4 #include "sysemu/blockdev.h" 5 #include "hw/block/block.h" 6 #include "net/hub.h" 7 #include "qapi/visitor.h" 8 #include "sysemu/char.h" 9 10 void qdev_prop_set_after_realize(DeviceState *dev, const char *name, 11 Error **errp) 12 { 13 if (dev->id) { 14 error_setg(errp, "Attempt to set property '%s' on device '%s' " 15 "(type '%s') after it was realized", name, dev->id, 16 object_get_typename(OBJECT(dev))); 17 } else { 18 error_setg(errp, "Attempt to set property '%s' on anonymous device " 19 "(type '%s') after it was realized", name, 20 object_get_typename(OBJECT(dev))); 21 } 22 } 23 24 void qdev_prop_allow_set_link_before_realize(Object *obj, const char *name, 25 Object *val, Error **errp) 26 { 27 DeviceState *dev = DEVICE(obj); 28 29 if (dev->realized) { 30 error_setg(errp, "Attempt to set link property '%s' on device '%s' " 31 "(type '%s') after it was realized", 32 name, dev->id, object_get_typename(obj)); 33 } 34 } 35 36 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop) 37 { 38 void *ptr = dev; 39 ptr += prop->offset; 40 return ptr; 41 } 42 43 static void get_enum(Object *obj, Visitor *v, void *opaque, 44 const char *name, Error **errp) 45 { 46 DeviceState *dev = DEVICE(obj); 47 Property *prop = opaque; 48 int *ptr = qdev_get_prop_ptr(dev, prop); 49 50 visit_type_enum(v, ptr, prop->info->enum_table, 51 prop->info->name, prop->name, errp); 52 } 53 54 static void set_enum(Object *obj, Visitor *v, void *opaque, 55 const char *name, Error **errp) 56 { 57 DeviceState *dev = DEVICE(obj); 58 Property *prop = opaque; 59 int *ptr = qdev_get_prop_ptr(dev, prop); 60 61 if (dev->realized) { 62 qdev_prop_set_after_realize(dev, name, errp); 63 return; 64 } 65 66 visit_type_enum(v, ptr, prop->info->enum_table, 67 prop->info->name, prop->name, errp); 68 } 69 70 /* Bit */ 71 72 static uint32_t qdev_get_prop_mask(Property *prop) 73 { 74 assert(prop->info == &qdev_prop_bit); 75 return 0x1 << prop->bitnr; 76 } 77 78 static void bit_prop_set(DeviceState *dev, Property *props, bool val) 79 { 80 uint32_t *p = qdev_get_prop_ptr(dev, props); 81 uint32_t mask = qdev_get_prop_mask(props); 82 if (val) { 83 *p |= mask; 84 } else { 85 *p &= ~mask; 86 } 87 } 88 89 static void prop_get_bit(Object *obj, Visitor *v, void *opaque, 90 const char *name, Error **errp) 91 { 92 DeviceState *dev = DEVICE(obj); 93 Property *prop = opaque; 94 uint32_t *p = qdev_get_prop_ptr(dev, prop); 95 bool value = (*p & qdev_get_prop_mask(prop)) != 0; 96 97 visit_type_bool(v, &value, name, errp); 98 } 99 100 static void prop_set_bit(Object *obj, Visitor *v, void *opaque, 101 const char *name, Error **errp) 102 { 103 DeviceState *dev = DEVICE(obj); 104 Property *prop = opaque; 105 Error *local_err = NULL; 106 bool value; 107 108 if (dev->realized) { 109 qdev_prop_set_after_realize(dev, name, errp); 110 return; 111 } 112 113 visit_type_bool(v, &value, name, &local_err); 114 if (local_err) { 115 error_propagate(errp, local_err); 116 return; 117 } 118 bit_prop_set(dev, prop, value); 119 } 120 121 PropertyInfo qdev_prop_bit = { 122 .name = "bool", 123 .legacy_name = "on/off", 124 .get = prop_get_bit, 125 .set = prop_set_bit, 126 }; 127 128 /* --- bool --- */ 129 130 static void get_bool(Object *obj, Visitor *v, void *opaque, 131 const char *name, Error **errp) 132 { 133 DeviceState *dev = DEVICE(obj); 134 Property *prop = opaque; 135 bool *ptr = qdev_get_prop_ptr(dev, prop); 136 137 visit_type_bool(v, ptr, name, errp); 138 } 139 140 static void set_bool(Object *obj, Visitor *v, void *opaque, 141 const char *name, Error **errp) 142 { 143 DeviceState *dev = DEVICE(obj); 144 Property *prop = opaque; 145 bool *ptr = qdev_get_prop_ptr(dev, prop); 146 147 if (dev->realized) { 148 qdev_prop_set_after_realize(dev, name, errp); 149 return; 150 } 151 152 visit_type_bool(v, ptr, name, errp); 153 } 154 155 PropertyInfo qdev_prop_bool = { 156 .name = "bool", 157 .get = get_bool, 158 .set = set_bool, 159 }; 160 161 /* --- 8bit integer --- */ 162 163 static void get_uint8(Object *obj, Visitor *v, void *opaque, 164 const char *name, Error **errp) 165 { 166 DeviceState *dev = DEVICE(obj); 167 Property *prop = opaque; 168 uint8_t *ptr = qdev_get_prop_ptr(dev, prop); 169 170 visit_type_uint8(v, ptr, name, errp); 171 } 172 173 static void set_uint8(Object *obj, Visitor *v, void *opaque, 174 const char *name, Error **errp) 175 { 176 DeviceState *dev = DEVICE(obj); 177 Property *prop = opaque; 178 uint8_t *ptr = qdev_get_prop_ptr(dev, prop); 179 180 if (dev->realized) { 181 qdev_prop_set_after_realize(dev, name, errp); 182 return; 183 } 184 185 visit_type_uint8(v, ptr, name, errp); 186 } 187 188 PropertyInfo qdev_prop_uint8 = { 189 .name = "uint8", 190 .get = get_uint8, 191 .set = set_uint8, 192 }; 193 194 /* --- 16bit integer --- */ 195 196 static void get_uint16(Object *obj, Visitor *v, void *opaque, 197 const char *name, Error **errp) 198 { 199 DeviceState *dev = DEVICE(obj); 200 Property *prop = opaque; 201 uint16_t *ptr = qdev_get_prop_ptr(dev, prop); 202 203 visit_type_uint16(v, ptr, name, errp); 204 } 205 206 static void set_uint16(Object *obj, Visitor *v, void *opaque, 207 const char *name, Error **errp) 208 { 209 DeviceState *dev = DEVICE(obj); 210 Property *prop = opaque; 211 uint16_t *ptr = qdev_get_prop_ptr(dev, prop); 212 213 if (dev->realized) { 214 qdev_prop_set_after_realize(dev, name, errp); 215 return; 216 } 217 218 visit_type_uint16(v, ptr, name, errp); 219 } 220 221 PropertyInfo qdev_prop_uint16 = { 222 .name = "uint16", 223 .get = get_uint16, 224 .set = set_uint16, 225 }; 226 227 /* --- 32bit integer --- */ 228 229 static void get_uint32(Object *obj, Visitor *v, void *opaque, 230 const char *name, Error **errp) 231 { 232 DeviceState *dev = DEVICE(obj); 233 Property *prop = opaque; 234 uint32_t *ptr = qdev_get_prop_ptr(dev, prop); 235 236 visit_type_uint32(v, ptr, name, errp); 237 } 238 239 static void set_uint32(Object *obj, Visitor *v, void *opaque, 240 const char *name, Error **errp) 241 { 242 DeviceState *dev = DEVICE(obj); 243 Property *prop = opaque; 244 uint32_t *ptr = qdev_get_prop_ptr(dev, prop); 245 246 if (dev->realized) { 247 qdev_prop_set_after_realize(dev, name, errp); 248 return; 249 } 250 251 visit_type_uint32(v, ptr, name, errp); 252 } 253 254 static void get_int32(Object *obj, Visitor *v, void *opaque, 255 const char *name, Error **errp) 256 { 257 DeviceState *dev = DEVICE(obj); 258 Property *prop = opaque; 259 int32_t *ptr = qdev_get_prop_ptr(dev, prop); 260 261 visit_type_int32(v, ptr, name, errp); 262 } 263 264 static void set_int32(Object *obj, Visitor *v, void *opaque, 265 const char *name, Error **errp) 266 { 267 DeviceState *dev = DEVICE(obj); 268 Property *prop = opaque; 269 int32_t *ptr = qdev_get_prop_ptr(dev, prop); 270 271 if (dev->realized) { 272 qdev_prop_set_after_realize(dev, name, errp); 273 return; 274 } 275 276 visit_type_int32(v, ptr, name, errp); 277 } 278 279 PropertyInfo qdev_prop_uint32 = { 280 .name = "uint32", 281 .get = get_uint32, 282 .set = set_uint32, 283 }; 284 285 PropertyInfo qdev_prop_int32 = { 286 .name = "int32", 287 .get = get_int32, 288 .set = set_int32, 289 }; 290 291 /* --- 64bit integer --- */ 292 293 static void get_uint64(Object *obj, Visitor *v, void *opaque, 294 const char *name, Error **errp) 295 { 296 DeviceState *dev = DEVICE(obj); 297 Property *prop = opaque; 298 uint64_t *ptr = qdev_get_prop_ptr(dev, prop); 299 300 visit_type_uint64(v, ptr, name, errp); 301 } 302 303 static void set_uint64(Object *obj, Visitor *v, void *opaque, 304 const char *name, Error **errp) 305 { 306 DeviceState *dev = DEVICE(obj); 307 Property *prop = opaque; 308 uint64_t *ptr = qdev_get_prop_ptr(dev, prop); 309 310 if (dev->realized) { 311 qdev_prop_set_after_realize(dev, name, errp); 312 return; 313 } 314 315 visit_type_uint64(v, ptr, name, errp); 316 } 317 318 PropertyInfo qdev_prop_uint64 = { 319 .name = "uint64", 320 .get = get_uint64, 321 .set = set_uint64, 322 }; 323 324 /* --- string --- */ 325 326 static void release_string(Object *obj, const char *name, void *opaque) 327 { 328 Property *prop = opaque; 329 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj), prop)); 330 } 331 332 static void get_string(Object *obj, Visitor *v, void *opaque, 333 const char *name, Error **errp) 334 { 335 DeviceState *dev = DEVICE(obj); 336 Property *prop = opaque; 337 char **ptr = qdev_get_prop_ptr(dev, prop); 338 339 if (!*ptr) { 340 char *str = (char *)""; 341 visit_type_str(v, &str, name, errp); 342 } else { 343 visit_type_str(v, ptr, name, errp); 344 } 345 } 346 347 static void set_string(Object *obj, Visitor *v, void *opaque, 348 const char *name, Error **errp) 349 { 350 DeviceState *dev = DEVICE(obj); 351 Property *prop = opaque; 352 char **ptr = qdev_get_prop_ptr(dev, prop); 353 Error *local_err = NULL; 354 char *str; 355 356 if (dev->realized) { 357 qdev_prop_set_after_realize(dev, name, errp); 358 return; 359 } 360 361 visit_type_str(v, &str, name, &local_err); 362 if (local_err) { 363 error_propagate(errp, local_err); 364 return; 365 } 366 if (*ptr) { 367 g_free(*ptr); 368 } 369 *ptr = str; 370 } 371 372 PropertyInfo qdev_prop_string = { 373 .name = "str", 374 .release = release_string, 375 .get = get_string, 376 .set = set_string, 377 }; 378 379 /* --- pointer --- */ 380 381 /* Not a proper property, just for dirty hacks. TODO Remove it! */ 382 PropertyInfo qdev_prop_ptr = { 383 .name = "ptr", 384 }; 385 386 /* --- mac address --- */ 387 388 /* 389 * accepted syntax versions: 390 * 01:02:03:04:05:06 391 * 01-02-03-04-05-06 392 */ 393 static void get_mac(Object *obj, Visitor *v, void *opaque, 394 const char *name, Error **errp) 395 { 396 DeviceState *dev = DEVICE(obj); 397 Property *prop = opaque; 398 MACAddr *mac = qdev_get_prop_ptr(dev, prop); 399 char buffer[2 * 6 + 5 + 1]; 400 char *p = buffer; 401 402 snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x", 403 mac->a[0], mac->a[1], mac->a[2], 404 mac->a[3], mac->a[4], mac->a[5]); 405 406 visit_type_str(v, &p, name, errp); 407 } 408 409 static void set_mac(Object *obj, Visitor *v, void *opaque, 410 const char *name, Error **errp) 411 { 412 DeviceState *dev = DEVICE(obj); 413 Property *prop = opaque; 414 MACAddr *mac = qdev_get_prop_ptr(dev, prop); 415 Error *local_err = NULL; 416 int i, pos; 417 char *str, *p; 418 419 if (dev->realized) { 420 qdev_prop_set_after_realize(dev, name, errp); 421 return; 422 } 423 424 visit_type_str(v, &str, name, &local_err); 425 if (local_err) { 426 error_propagate(errp, local_err); 427 return; 428 } 429 430 for (i = 0, pos = 0; i < 6; i++, pos += 3) { 431 if (!qemu_isxdigit(str[pos])) { 432 goto inval; 433 } 434 if (!qemu_isxdigit(str[pos+1])) { 435 goto inval; 436 } 437 if (i == 5) { 438 if (str[pos+2] != '\0') { 439 goto inval; 440 } 441 } else { 442 if (str[pos+2] != ':' && str[pos+2] != '-') { 443 goto inval; 444 } 445 } 446 mac->a[i] = strtol(str+pos, &p, 16); 447 } 448 g_free(str); 449 return; 450 451 inval: 452 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str); 453 g_free(str); 454 } 455 456 PropertyInfo qdev_prop_macaddr = { 457 .name = "str", 458 .legacy_name = "macaddr", 459 .get = get_mac, 460 .set = set_mac, 461 }; 462 463 /* --- lost tick policy --- */ 464 465 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int)); 466 467 PropertyInfo qdev_prop_losttickpolicy = { 468 .name = "LostTickPolicy", 469 .enum_table = LostTickPolicy_lookup, 470 .get = get_enum, 471 .set = set_enum, 472 }; 473 474 /* --- BIOS CHS translation */ 475 476 QEMU_BUILD_BUG_ON(sizeof(BiosAtaTranslation) != sizeof(int)); 477 478 PropertyInfo qdev_prop_bios_chs_trans = { 479 .name = "BiosAtaTranslation", 480 .legacy_name = "bios-chs-trans", 481 .enum_table = BiosAtaTranslation_lookup, 482 .get = get_enum, 483 .set = set_enum, 484 }; 485 486 /* --- pci address --- */ 487 488 /* 489 * bus-local address, i.e. "$slot" or "$slot.$fn" 490 */ 491 static void set_pci_devfn(Object *obj, Visitor *v, void *opaque, 492 const char *name, Error **errp) 493 { 494 DeviceState *dev = DEVICE(obj); 495 Property *prop = opaque; 496 int32_t value, *ptr = qdev_get_prop_ptr(dev, prop); 497 unsigned int slot, fn, n; 498 Error *local_err = NULL; 499 char *str; 500 501 if (dev->realized) { 502 qdev_prop_set_after_realize(dev, name, errp); 503 return; 504 } 505 506 visit_type_str(v, &str, name, &local_err); 507 if (local_err) { 508 error_free(local_err); 509 local_err = NULL; 510 visit_type_int32(v, &value, name, &local_err); 511 if (local_err) { 512 error_propagate(errp, local_err); 513 } else if (value < -1 || value > 255) { 514 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null", 515 "pci_devfn"); 516 } else { 517 *ptr = value; 518 } 519 return; 520 } 521 522 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) { 523 fn = 0; 524 if (sscanf(str, "%x%n", &slot, &n) != 1) { 525 goto invalid; 526 } 527 } 528 if (str[n] != '\0' || fn > 7 || slot > 31) { 529 goto invalid; 530 } 531 *ptr = slot << 3 | fn; 532 g_free(str); 533 return; 534 535 invalid: 536 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str); 537 g_free(str); 538 } 539 540 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, 541 size_t len) 542 { 543 int32_t *ptr = qdev_get_prop_ptr(dev, prop); 544 545 if (*ptr == -1) { 546 return snprintf(dest, len, "<unset>"); 547 } else { 548 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7); 549 } 550 } 551 552 PropertyInfo qdev_prop_pci_devfn = { 553 .name = "int32", 554 .legacy_name = "pci-devfn", 555 .print = print_pci_devfn, 556 .get = get_int32, 557 .set = set_pci_devfn, 558 }; 559 560 /* --- blocksize --- */ 561 562 static void set_blocksize(Object *obj, Visitor *v, void *opaque, 563 const char *name, Error **errp) 564 { 565 DeviceState *dev = DEVICE(obj); 566 Property *prop = opaque; 567 uint16_t value, *ptr = qdev_get_prop_ptr(dev, prop); 568 Error *local_err = NULL; 569 const int64_t min = 512; 570 const int64_t max = 32768; 571 572 if (dev->realized) { 573 qdev_prop_set_after_realize(dev, name, errp); 574 return; 575 } 576 577 visit_type_uint16(v, &value, name, &local_err); 578 if (local_err) { 579 error_propagate(errp, local_err); 580 return; 581 } 582 if (value < min || value > max) { 583 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE, 584 dev->id?:"", name, (int64_t)value, min, max); 585 return; 586 } 587 588 /* We rely on power-of-2 blocksizes for bitmasks */ 589 if ((value & (value - 1)) != 0) { 590 error_setg(errp, 591 "Property %s.%s doesn't take value '%" PRId64 "', it's not a power of 2", 592 dev->id ?: "", name, (int64_t)value); 593 return; 594 } 595 596 *ptr = value; 597 } 598 599 PropertyInfo qdev_prop_blocksize = { 600 .name = "uint16", 601 .legacy_name = "blocksize", 602 .get = get_uint16, 603 .set = set_blocksize, 604 }; 605 606 /* --- pci host address --- */ 607 608 static void get_pci_host_devaddr(Object *obj, Visitor *v, void *opaque, 609 const char *name, Error **errp) 610 { 611 DeviceState *dev = DEVICE(obj); 612 Property *prop = opaque; 613 PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop); 614 char buffer[] = "xxxx:xx:xx.x"; 615 char *p = buffer; 616 int rc = 0; 617 618 rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%d", 619 addr->domain, addr->bus, addr->slot, addr->function); 620 assert(rc == sizeof(buffer) - 1); 621 622 visit_type_str(v, &p, name, errp); 623 } 624 625 /* 626 * Parse [<domain>:]<bus>:<slot>.<func> 627 * if <domain> is not supplied, it's assumed to be 0. 628 */ 629 static void set_pci_host_devaddr(Object *obj, Visitor *v, void *opaque, 630 const char *name, Error **errp) 631 { 632 DeviceState *dev = DEVICE(obj); 633 Property *prop = opaque; 634 PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop); 635 Error *local_err = NULL; 636 char *str, *p; 637 char *e; 638 unsigned long val; 639 unsigned long dom = 0, bus = 0; 640 unsigned int slot = 0, func = 0; 641 642 if (dev->realized) { 643 qdev_prop_set_after_realize(dev, name, errp); 644 return; 645 } 646 647 visit_type_str(v, &str, name, &local_err); 648 if (local_err) { 649 error_propagate(errp, local_err); 650 return; 651 } 652 653 p = str; 654 val = strtoul(p, &e, 16); 655 if (e == p || *e != ':') { 656 goto inval; 657 } 658 bus = val; 659 660 p = e + 1; 661 val = strtoul(p, &e, 16); 662 if (e == p) { 663 goto inval; 664 } 665 if (*e == ':') { 666 dom = bus; 667 bus = val; 668 p = e + 1; 669 val = strtoul(p, &e, 16); 670 if (e == p) { 671 goto inval; 672 } 673 } 674 slot = val; 675 676 if (*e != '.') { 677 goto inval; 678 } 679 p = e + 1; 680 val = strtoul(p, &e, 10); 681 if (e == p) { 682 goto inval; 683 } 684 func = val; 685 686 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) { 687 goto inval; 688 } 689 690 if (*e) { 691 goto inval; 692 } 693 694 addr->domain = dom; 695 addr->bus = bus; 696 addr->slot = slot; 697 addr->function = func; 698 699 g_free(str); 700 return; 701 702 inval: 703 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str); 704 g_free(str); 705 } 706 707 PropertyInfo qdev_prop_pci_host_devaddr = { 708 .name = "str", 709 .legacy_name = "pci-host-devaddr", 710 .get = get_pci_host_devaddr, 711 .set = set_pci_host_devaddr, 712 }; 713 714 /* --- support for array properties --- */ 715 716 /* Used as an opaque for the object properties we add for each 717 * array element. Note that the struct Property must be first 718 * in the struct so that a pointer to this works as the opaque 719 * for the underlying element's property hooks as well as for 720 * our own release callback. 721 */ 722 typedef struct { 723 struct Property prop; 724 char *propname; 725 ObjectPropertyRelease *release; 726 } ArrayElementProperty; 727 728 /* object property release callback for array element properties: 729 * we call the underlying element's property release hook, and 730 * then free the memory we allocated when we added the property. 731 */ 732 static void array_element_release(Object *obj, const char *name, void *opaque) 733 { 734 ArrayElementProperty *p = opaque; 735 if (p->release) { 736 p->release(obj, name, opaque); 737 } 738 g_free(p->propname); 739 g_free(p); 740 } 741 742 static void set_prop_arraylen(Object *obj, Visitor *v, void *opaque, 743 const char *name, Error **errp) 744 { 745 /* Setter for the property which defines the length of a 746 * variable-sized property array. As well as actually setting the 747 * array-length field in the device struct, we have to create the 748 * array itself and dynamically add the corresponding properties. 749 */ 750 DeviceState *dev = DEVICE(obj); 751 Property *prop = opaque; 752 uint32_t *alenptr = qdev_get_prop_ptr(dev, prop); 753 void **arrayptr = (void *)dev + prop->arrayoffset; 754 Error *local_err = NULL; 755 void *eltptr; 756 const char *arrayname; 757 int i; 758 759 if (dev->realized) { 760 qdev_prop_set_after_realize(dev, name, errp); 761 return; 762 } 763 if (*alenptr) { 764 error_setg(errp, "array size property %s may not be set more than once", 765 name); 766 return; 767 } 768 visit_type_uint32(v, alenptr, name, &local_err); 769 if (local_err) { 770 error_propagate(errp, local_err); 771 return; 772 } 773 if (!*alenptr) { 774 return; 775 } 776 777 /* DEFINE_PROP_ARRAY guarantees that name should start with this prefix; 778 * strip it off so we can get the name of the array itself. 779 */ 780 assert(strncmp(name, PROP_ARRAY_LEN_PREFIX, 781 strlen(PROP_ARRAY_LEN_PREFIX)) == 0); 782 arrayname = name + strlen(PROP_ARRAY_LEN_PREFIX); 783 784 /* Note that it is the responsibility of the individual device's deinit 785 * to free the array proper. 786 */ 787 *arrayptr = eltptr = g_malloc0(*alenptr * prop->arrayfieldsize); 788 for (i = 0; i < *alenptr; i++, eltptr += prop->arrayfieldsize) { 789 char *propname = g_strdup_printf("%s[%d]", arrayname, i); 790 ArrayElementProperty *arrayprop = g_new0(ArrayElementProperty, 1); 791 arrayprop->release = prop->arrayinfo->release; 792 arrayprop->propname = propname; 793 arrayprop->prop.info = prop->arrayinfo; 794 arrayprop->prop.name = propname; 795 /* This ugly piece of pointer arithmetic sets up the offset so 796 * that when the underlying get/set hooks call qdev_get_prop_ptr 797 * they get the right answer despite the array element not actually 798 * being inside the device struct. 799 */ 800 arrayprop->prop.offset = eltptr - (void *)dev; 801 assert(qdev_get_prop_ptr(dev, &arrayprop->prop) == eltptr); 802 object_property_add(obj, propname, 803 arrayprop->prop.info->name, 804 arrayprop->prop.info->get, 805 arrayprop->prop.info->set, 806 array_element_release, 807 arrayprop, &local_err); 808 if (local_err) { 809 error_propagate(errp, local_err); 810 return; 811 } 812 } 813 } 814 815 PropertyInfo qdev_prop_arraylen = { 816 .name = "uint32", 817 .get = get_uint32, 818 .set = set_prop_arraylen, 819 }; 820 821 /* --- public helpers --- */ 822 823 static Property *qdev_prop_walk(Property *props, const char *name) 824 { 825 if (!props) { 826 return NULL; 827 } 828 while (props->name) { 829 if (strcmp(props->name, name) == 0) { 830 return props; 831 } 832 props++; 833 } 834 return NULL; 835 } 836 837 static Property *qdev_prop_find(DeviceState *dev, const char *name) 838 { 839 ObjectClass *class; 840 Property *prop; 841 842 /* device properties */ 843 class = object_get_class(OBJECT(dev)); 844 do { 845 prop = qdev_prop_walk(DEVICE_CLASS(class)->props, name); 846 if (prop) { 847 return prop; 848 } 849 class = object_class_get_parent(class); 850 } while (class != object_class_by_name(TYPE_DEVICE)); 851 852 return NULL; 853 } 854 855 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev, 856 Property *prop, const char *value) 857 { 858 switch (ret) { 859 case -EEXIST: 860 error_setg(errp, "Property '%s.%s' can't take value '%s', it's in use", 861 object_get_typename(OBJECT(dev)), prop->name, value); 862 break; 863 default: 864 case -EINVAL: 865 error_set(errp, QERR_PROPERTY_VALUE_BAD, 866 object_get_typename(OBJECT(dev)), prop->name, value); 867 break; 868 case -ENOENT: 869 error_setg(errp, "Property '%s.%s' can't find value '%s'", 870 object_get_typename(OBJECT(dev)), prop->name, value); 871 break; 872 case 0: 873 break; 874 } 875 } 876 877 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value) 878 { 879 object_property_set_bool(OBJECT(dev), value, name, &error_abort); 880 } 881 882 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value) 883 { 884 object_property_set_int(OBJECT(dev), value, name, &error_abort); 885 } 886 887 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value) 888 { 889 object_property_set_int(OBJECT(dev), value, name, &error_abort); 890 } 891 892 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value) 893 { 894 object_property_set_int(OBJECT(dev), value, name, &error_abort); 895 } 896 897 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value) 898 { 899 object_property_set_int(OBJECT(dev), value, name, &error_abort); 900 } 901 902 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value) 903 { 904 object_property_set_int(OBJECT(dev), value, name, &error_abort); 905 } 906 907 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value) 908 { 909 object_property_set_str(OBJECT(dev), value, name, &error_abort); 910 } 911 912 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value) 913 { 914 char str[2 * 6 + 5 + 1]; 915 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x", 916 value[0], value[1], value[2], value[3], value[4], value[5]); 917 918 object_property_set_str(OBJECT(dev), str, name, &error_abort); 919 } 920 921 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value) 922 { 923 Property *prop; 924 925 prop = qdev_prop_find(dev, name); 926 object_property_set_str(OBJECT(dev), prop->info->enum_table[value], 927 name, &error_abort); 928 } 929 930 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value) 931 { 932 Property *prop; 933 void **ptr; 934 935 prop = qdev_prop_find(dev, name); 936 assert(prop && prop->info == &qdev_prop_ptr); 937 ptr = qdev_get_prop_ptr(dev, prop); 938 *ptr = value; 939 } 940 941 static QTAILQ_HEAD(, GlobalProperty) global_props = 942 QTAILQ_HEAD_INITIALIZER(global_props); 943 944 void qdev_prop_register_global(GlobalProperty *prop) 945 { 946 QTAILQ_INSERT_TAIL(&global_props, prop, next); 947 } 948 949 void qdev_prop_register_global_list(GlobalProperty *props) 950 { 951 int i; 952 953 for (i = 0; props[i].driver != NULL; i++) { 954 qdev_prop_register_global(props+i); 955 } 956 } 957 958 int qdev_prop_check_global(void) 959 { 960 GlobalProperty *prop; 961 int ret = 0; 962 963 QTAILQ_FOREACH(prop, &global_props, next) { 964 if (!prop->not_used) { 965 continue; 966 } 967 ret = 1; 968 error_report("Warning: \"-global %s.%s=%s\" not used", 969 prop->driver, prop->property, prop->value); 970 971 } 972 return ret; 973 } 974 975 void qdev_prop_set_globals_for_type(DeviceState *dev, const char *typename, 976 Error **errp) 977 { 978 GlobalProperty *prop; 979 980 QTAILQ_FOREACH(prop, &global_props, next) { 981 Error *err = NULL; 982 983 if (strcmp(typename, prop->driver) != 0) { 984 continue; 985 } 986 prop->not_used = false; 987 object_property_parse(OBJECT(dev), prop->value, prop->property, &err); 988 if (err != NULL) { 989 error_propagate(errp, err); 990 return; 991 } 992 } 993 } 994 995 void qdev_prop_set_globals(DeviceState *dev, Error **errp) 996 { 997 ObjectClass *class = object_get_class(OBJECT(dev)); 998 999 do { 1000 Error *err = NULL; 1001 1002 qdev_prop_set_globals_for_type(dev, object_class_get_name(class), 1003 &err); 1004 if (err != NULL) { 1005 error_propagate(errp, err); 1006 return; 1007 } 1008 class = object_class_get_parent(class); 1009 } while (class); 1010 } 1011 1012 /* --- 64bit unsigned int 'size' type --- */ 1013 1014 static void get_size(Object *obj, Visitor *v, void *opaque, 1015 const char *name, Error **errp) 1016 { 1017 DeviceState *dev = DEVICE(obj); 1018 Property *prop = opaque; 1019 uint64_t *ptr = qdev_get_prop_ptr(dev, prop); 1020 1021 visit_type_size(v, ptr, name, errp); 1022 } 1023 1024 static void set_size(Object *obj, Visitor *v, void *opaque, 1025 const char *name, Error **errp) 1026 { 1027 DeviceState *dev = DEVICE(obj); 1028 Property *prop = opaque; 1029 uint64_t *ptr = qdev_get_prop_ptr(dev, prop); 1030 1031 visit_type_size(v, ptr, name, errp); 1032 } 1033 1034 PropertyInfo qdev_prop_size = { 1035 .name = "size", 1036 .get = get_size, 1037 .set = set_size, 1038 }; 1039