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