Skip to content

Commit 7345677

Browse files
mingqiangchilijinxia
authored andcommitted
hv:cleanup vmid related code
Remove structure vm_attr Wrap two APIs alloc_vm_id and free_vm_id Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 2299926 commit 7345677

File tree

17 files changed

+73
-65
lines changed

17 files changed

+73
-65
lines changed

hypervisor/arch/x86/assign.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ add_msix_remapping(struct vm *vm, uint16_t virt_bdf, uint16_t phys_bdf,
329329
" not able to add into vm%d with vbdf%x",
330330
entry->phys_bdf,
331331
entry->ptdev_intr_info.msi.msix_entry_index,
332-
entry->vm->attr.id,
333-
entry->virt_bdf, vm->attr.id, virt_bdf);
332+
entry->vm->vm_id,
333+
entry->virt_bdf, vm->vm_id, virt_bdf);
334334
ASSERT(false, "msix entry pbdf%x idx%d already in vm%d",
335-
phys_bdf, msix_entry_index, entry->vm->attr.id);
335+
phys_bdf, msix_entry_index, entry->vm->vm_id);
336336

337337
spinlock_release(&ptdev_lock);
338338
return &invalid_entry;
@@ -345,7 +345,7 @@ add_msix_remapping(struct vm *vm, uint16_t virt_bdf, uint16_t phys_bdf,
345345

346346
dev_dbg(ACRN_DBG_IRQ,
347347
"VM%d MSIX add vector mapping vbdf%x:pbdf%x idx=%d",
348-
entry->vm->attr.id, virt_bdf, phys_bdf, msix_entry_index);
348+
entry->vm->vm_id, virt_bdf, phys_bdf, msix_entry_index);
349349

350350
return entry;
351351
}
@@ -369,7 +369,7 @@ remove_msix_remapping(struct vm *vm, uint16_t virt_bdf, uint32_t msix_entry_inde
369369

370370
dev_dbg(ACRN_DBG_IRQ,
371371
"VM%d MSIX remove vector mapping vbdf-pbdf:0x%x-0x%x idx=%d",
372-
entry->vm->attr.id,
372+
entry->vm->vm_id,
373373
entry->virt_bdf, entry->phys_bdf, msix_entry_index);
374374

375375
release_entry(entry);
@@ -413,11 +413,11 @@ add_intx_remapping(struct vm *vm, uint8_t virt_pin,
413413
pr_err("INTX pin%d already in vm%d with vpin%d,"
414414
" not able to add into vm%d with vpin%d",
415415
entry->ptdev_intr_info.intx.phys_pin,
416-
entry->vm->attr.id,
416+
entry->vm->vm_id,
417417
entry->ptdev_intr_info.intx.virt_pin,
418-
vm->attr.id, virt_pin);
418+
vm->vm_id, virt_pin);
419419
ASSERT(false, "intx entry pin%d already vm%d",
420-
phys_pin, entry->vm->attr.id);
420+
phys_pin, entry->vm->vm_id);
421421

422422
spinlock_release(&ptdev_lock);
423423
return &invalid_entry;
@@ -431,7 +431,7 @@ add_intx_remapping(struct vm *vm, uint8_t virt_pin,
431431

432432
dev_dbg(ACRN_DBG_IRQ,
433433
"VM%d INTX add pin mapping vpin%d:ppin%d",
434-
entry->vm->attr.id, virt_pin, phys_pin);
434+
entry->vm->vm_id, virt_pin, phys_pin);
435435

436436
return entry;
437437
}
@@ -466,7 +466,7 @@ static void remove_intx_remapping(struct vm *vm, uint8_t virt_pin, bool pic_pin)
466466
"vPIC" : "vIOAPIC",
467467
entry->ptdev_intr_info.intx.phys_pin, phys_irq);
468468
dev_dbg(ACRN_DBG_IRQ, "from vm%d vpin=%d\n",
469-
entry->vm->attr.id,
469+
entry->vm->vm_id,
470470
entry->ptdev_intr_info.intx.virt_pin);
471471
}
472472

@@ -677,7 +677,7 @@ int ptdev_msix_remap(struct vm *vm, uint16_t virt_bdf,
677677
entry->ptdev_intr_info.msi.msix_entry_index,
678678
entry->ptdev_intr_info.msi.virt_vector,
679679
entry->ptdev_intr_info.msi.phys_vector,
680-
entry->vm->attr.id);
680+
entry->vm->vm_id);
681681
END:
682682
return 0;
683683
}
@@ -831,7 +831,7 @@ int ptdev_intx_pin_remap(struct vm *vm, struct ptdev_intx_info *info)
831831
(entry->ptdev_intr_info.intx.vpin_src != 0)?
832832
"vIOPIC" : "vPIC",
833833
info->virt_pin,
834-
entry->vm->attr.id);
834+
entry->vm->vm_id);
835835
intx->vpin_src = info->vpin_src;
836836
intx->virt_pin = info->virt_pin;
837837
}
@@ -848,7 +848,7 @@ int ptdev_intx_pin_remap(struct vm *vm, struct ptdev_intx_info *info)
848848
"IOAPIC pin=%hhu pirq=%u deassigned ",
849849
phys_pin, phys_irq);
850850
dev_dbg(ACRN_DBG_IRQ, "from vm%d vIOAPIC vpin=%d",
851-
entry->vm->attr.id,
851+
entry->vm->vm_id,
852852
intx->virt_pin);
853853
goto END;
854854
} else {
@@ -869,7 +869,7 @@ int ptdev_intx_pin_remap(struct vm *vm, struct ptdev_intx_info *info)
869869

870870
dev_dbg(ACRN_DBG_IRQ,
871871
"IOAPIC pin=%hhu pirq=%u assigned to vm%d %s vpin=%d",
872-
phys_pin, phys_irq, entry->vm->attr.id,
872+
phys_pin, phys_irq, entry->vm->vm_id,
873873
intx->vpin_src == PTDEV_VPIN_PIC ?
874874
"vPIC" : "vIOAPIC",
875875
intx->virt_pin);
@@ -1037,7 +1037,7 @@ void get_ptdev_info(char *str_arg, int str_max)
10371037
&bdf, &vbdf);
10381038
len = snprintf(str, size,
10391039
"\r\n%d\t%s\t%d\t0x%X\t0x%X",
1040-
entry->vm->attr.id, type,
1040+
entry->vm->vm_id, type,
10411041
irq, vector, dest);
10421042
size -= len;
10431043
str += len;

hypervisor/arch/x86/ept.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ uint64_t local_gpa2hpa(struct vm *vm, uint64_t gpa, uint32_t *size)
129129
pr_dbg("GPA2HPA: 0x%llx->0x%llx", gpa, hpa);
130130
} else {
131131
pr_err("VM %d GPA2HPA: failed for gpa 0x%llx",
132-
vm->attr.boot_idx, gpa);
132+
vm->vm_id, gpa);
133133
}
134134

135135
if (size != NULL) {
@@ -153,7 +153,7 @@ uint64_t hpa2gpa(struct vm *vm, uint64_t hpa)
153153
hpa, &pg_size, PTT_EPT);
154154
if (pgentry == NULL) {
155155
pr_err("VM %d hpa2gpa: failed for hpa 0x%llx",
156-
vm->attr.boot_idx, hpa);
156+
vm->vm_id, hpa);
157157
ASSERT(false, "hpa2gpa not found");
158158
}
159159
return ((*pgentry & (~(pg_size - 1UL)))

hypervisor/arch/x86/guest/guest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct page_walk_info {
3232
inline bool
3333
is_vm0(struct vm *vm)
3434
{
35-
return (vm->attr.boot_idx & 0x7FU) == 0U;
35+
return (vm->vm_id) == 0U;
3636
}
3737

3838
inline struct vcpu *vcpu_from_vid(struct vm *vm, uint16_t vcpu_id)

hypervisor/arch/x86/guest/pm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ register_gas_io_handler(struct vm *vm, struct acpi_generic_address *gas)
210210
&pm1ab_io_read, &pm1ab_io_write);
211211

212212
pr_dbg("Enable PM1A trap for VM %d, port 0x%x, size %d\n",
213-
vm->attr.id, gas_io.base, gas_io.len);
213+
vm->vm_id, gas_io.base, gas_io.len);
214214
}
215215

216216
void register_pm1ab_handler(struct vm *vm)

hypervisor/arch/x86/guest/vcpu.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
7575
per_cpu(vcpu, pcpu_id) = vcpu;
7676

7777
pr_info("PCPU%d is working as VM%d VCPU%d, Role: %s",
78-
vcpu->pcpu_id, vcpu->vm->attr.id, vcpu->vcpu_id,
78+
vcpu->pcpu_id, vcpu->vm->vm_id, vcpu->vcpu_id,
7979
is_vcpu_bsp(vcpu) ? "PRIMARY" : "SECONDARY");
8080

8181
#ifdef CONFIG_START_VM0_BSP_64BIT
@@ -84,7 +84,7 @@ int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
8484
/* Set up temporary guest page tables */
8585
vm->arch_vm.guest_init_pml4 = create_guest_initial_paging(vm);
8686
pr_info("VM %d VCPU %hu CR3: 0x%016llx ",
87-
vm->attr.id, vcpu->vcpu_id,
87+
vm->vm_id, vcpu->vcpu_id,
8888
vm->arch_vm.guest_init_pml4);
8989
}
9090
#endif
@@ -156,7 +156,7 @@ int start_vcpu(struct vcpu *vcpu)
156156
/* If this VCPU is not already launched, launch it */
157157
if (!vcpu->launched) {
158158
pr_info("VM %d Starting VCPU %hu",
159-
vcpu->vm->attr.id, vcpu->vcpu_id);
159+
vcpu->vm->vm_id, vcpu->vcpu_id);
160160

161161
if (vcpu->arch_vcpu.vpid)
162162
exec_vmwrite16(VMX_VPID, vcpu->arch_vcpu.vpid);
@@ -185,7 +185,7 @@ int start_vcpu(struct vcpu *vcpu)
185185
if (status == 0) {
186186
if (is_vcpu_bsp(vcpu)) {
187187
pr_info("VM %d VCPU %hu successfully launched",
188-
vcpu->vm->attr.id, vcpu->vcpu_id);
188+
vcpu->vm->vm_id, vcpu->vcpu_id);
189189
}
190190
}
191191
} else {

hypervisor/arch/x86/guest/vlapic.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ vm_lapic_from_vcpu_id(struct vm *vm, uint16_t vcpu_id)
105105
struct vcpu *vcpu;
106106

107107
vcpu = vcpu_from_vid(vm, vcpu_id);
108-
ASSERT(vcpu != NULL, "vm%d, vcpu%hu", vm->attr.id, vcpu_id);
108+
ASSERT(vcpu != NULL, "vm%d, vcpu%hu", vm->vm_id, vcpu_id);
109109

110110
return vcpu->arch_vcpu.vlapic;
111111
}
@@ -116,7 +116,7 @@ vm_lapic_from_pcpuid(struct vm *vm, uint16_t pcpu_id)
116116
struct vcpu *vcpu;
117117

118118
vcpu = vcpu_from_pid(vm, pcpu_id);
119-
ASSERT(vcpu != NULL, "vm%d, pcpu%hu", vm->attr.id, pcpu_id);
119+
ASSERT(vcpu != NULL, "vm%d, pcpu%hu", vm->vm_id, pcpu_id);
120120

121121
return vcpu->arch_vcpu.vlapic;
122122
}
@@ -1173,7 +1173,7 @@ vlapic_icrlo_write_handler(struct acrn_vlapic *vlapic)
11731173
target_vcpu->arch_vcpu.sipi_vector = vec;
11741174
pr_err("Start Secondary VCPU%hu for VM[%d]...",
11751175
target_vcpu->vcpu_id,
1176-
target_vcpu->vm->attr.id);
1176+
target_vcpu->vm->vm_id);
11771177
schedule_vcpu(target_vcpu);
11781178
} else {
11791179
pr_err("Unhandled icrlo write with mode %u\n", mode);

hypervisor/arch/x86/guest/vm.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ spinlock_t vm_list_lock = {
2525
/* used for vmid allocation. And this means the max vm number is 64 */
2626
static uint64_t vmid_bitmap;
2727

28+
static inline uint16_t alloc_vm_id(void)
29+
{
30+
uint16_t id = ffz64(vmid_bitmap);
31+
32+
while (id < (size_t)(sizeof(vmid_bitmap) * 8U)) {
33+
if (!bitmap_test_and_set_lock(id, &vmid_bitmap)) {
34+
return id;
35+
}
36+
id = ffz64(vmid_bitmap);
37+
}
38+
39+
return INVALID_VM_ID;
40+
}
41+
42+
static inline void free_vm_id(struct vm *vm)
43+
{
44+
bitmap_clear_lock(vm->vm_id, &vmid_bitmap);
45+
}
46+
2847
static void init_vm(struct vm_description *vm_desc,
2948
struct vm *vm_handle)
3049
{
@@ -54,7 +73,7 @@ struct vm *get_vm_from_vmid(uint16_t vm_id)
5473
spinlock_obtain(&vm_list_lock);
5574
list_for_each(pos, &vm_list) {
5675
vm = list_entry(pos, struct vm, list);
57-
if (vm->attr.id == vm_id) {
76+
if (vm->vm_id == vm_id) {
5877
spinlock_release(&vm_list_lock);
5978
return vm;
6079
}
@@ -66,7 +85,6 @@ struct vm *get_vm_from_vmid(uint16_t vm_id)
6685

6786
int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
6887
{
69-
uint16_t id;
7088
struct vm *vm;
7189
int status;
7290

@@ -102,21 +120,13 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
102120
goto err;
103121
}
104122

105-
for (id = 0U; id < (size_t)(sizeof(vmid_bitmap) * 8U); id++) {
106-
if (!bitmap_test_and_set_lock(id, &vmid_bitmap)) {
107-
break;
108-
}
109-
}
110-
111-
if (id >= (size_t)(sizeof(vmid_bitmap) * 8U)) {
123+
vm->vm_id = alloc_vm_id();
124+
if (vm->vm_id == INVALID_VM_ID) {
112125
pr_err("%s, no more VMs can be supported\n", __func__);
113-
status = -EINVAL;
126+
status = -ENODEV;
114127
goto err;
115128
}
116129

117-
vm->attr.id = id;
118-
vm->attr.boot_idx = id;
119-
120130
atomic_store16(&vm->hw.created_vcpus, 0U);
121131

122132
/* gpa_lowtop are used for system start up */
@@ -273,7 +283,8 @@ int shutdown_vm(struct vm *vm)
273283
destroy_iommu_domain(vm->iommu);
274284
}
275285

276-
bitmap_clear_lock(vm->attr.id, &vmid_bitmap);
286+
/* Free vm id */
287+
free_vm_id(vm);
277288

278289
if (vm->vpic != NULL) {
279290
vpic_cleanup(vm);
@@ -300,7 +311,7 @@ int start_vm(struct vm *vm)
300311

301312
/* Only start BSP (vid = 0) and let BSP start other APs */
302313
vcpu = vcpu_from_vid(vm, 0U);
303-
ASSERT(vcpu != NULL, "vm%d, vcpu0", vm->attr.id);
314+
ASSERT(vcpu != NULL, "vm%d, vcpu0", vm->vm_id);
304315
schedule_vcpu(vcpu);
305316

306317
return 0;

hypervisor/arch/x86/guest/vmcall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ int vmcall_vmexit_handler(struct vcpu *vcpu)
181181
out:
182182
cur_context->guest_cpu_regs.regs.rax = (uint64_t)ret;
183183

184-
TRACE_2L(TRACE_VMEXIT_VMCALL, vm->attr.id, hypcall_id);
184+
TRACE_2L(TRACE_VMEXIT_VMCALL, vm->vm_id, hypcall_id);
185185

186186
return 0;
187187
}

hypervisor/arch/x86/guest/vpic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static void vpic_notify_intr(struct acrn_vpic *vpic)
234234
if (vpic->vm->wire_mode == VPIC_WIRE_INTR) {
235235
struct vcpu *vcpu = vcpu_from_vid(vpic->vm, 0U);
236236

237-
ASSERT(vcpu != NULL, "vm%d, vcpu0", vpic->vm->attr.id);
237+
ASSERT(vcpu != NULL, "vm%d, vcpu0", vpic->vm->vm_id);
238238
vcpu_inject_extint(vcpu);
239239
} else {
240240
vlapic_set_local_intr(vpic->vm, BROADCAST_CPU_ID, APIC_LVT_LINT0);

hypervisor/arch/x86/mtrr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void init_mtrr(struct vcpu *vcpu)
117117
}
118118

119119
pr_dbg("vm%d vcpu%hu fixed-range MTRR[%u]: %16llx",
120-
vcpu->vm->attr.id, vcpu->vcpu_id, i,
120+
vcpu->vm->vm_id, vcpu->vcpu_id, i,
121121
vcpu->mtrr.fixed_range[i].value);
122122
}
123123
}

0 commit comments

Comments
 (0)