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