| 522 | } |
| 523 | |
| 524 | static void |
| 525 | vpid_alloc(uint16_t *vpid, int num) |
| 526 | { |
| 527 | int i, x; |
| 528 | |
| 529 | if (num <= 0 || num > VM_MAXCPU) |
| 530 | panic("invalid number of vpids requested: %d", num); |
| 531 | |
| 532 | /* |
| 533 | * If the "enable vpid" execution control is not enabled then the |
| 534 | * VPID is required to be 0 for all vcpus. |
| 535 | */ |
| 536 | if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) == 0) { |
| 537 | for (i = 0; i < num; i++) |
| 538 | vpid[i] = 0; |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Allocate a unique VPID for each vcpu from the unit number allocator. |
| 544 | */ |
| 545 | for (i = 0; i < num; i++) { |
| 546 | x = alloc_unr(vpid_unr); |
| 547 | if (x == -1) |
| 548 | break; |
| 549 | else |
| 550 | vpid[i] = x; |
| 551 | } |
| 552 | |
| 553 | if (i < num) { |
| 554 | atomic_add_int(&vpid_alloc_failed, 1); |
| 555 | |
| 556 | /* |
| 557 | * If the unit number allocator does not have enough unique |
| 558 | * VPIDs then we need to allocate from the [1,VM_MAXCPU] range. |
| 559 | * |
| 560 | * These VPIDs are not be unique across VMs but this does not |
| 561 | * affect correctness because the combined mappings are also |
| 562 | * tagged with the EP4TA which is unique for each VM. |
| 563 | * |
| 564 | * It is still sub-optimal because the invvpid will invalidate |
| 565 | * combined mappings for a particular VPID across all EP4TAs. |
| 566 | */ |
| 567 | while (i-- > 0) |
| 568 | vpid_free(vpid[i]); |
| 569 | |
| 570 | for (i = 0; i < num; i++) |
| 571 | vpid[i] = i + 1; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | static void |
| 576 | vpid_init(void) |