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