Skip to content

Commit f83ddd3

Browse files
jsun26intelacrnsi
authored andcommitted
HV: introduce relative vm id for hcall api
On SDC scenario, SOS VM id is fixed to 0 so some hypercalls from guest are using hardcoded "0" to represent SOS VM, this would bring issues for HYBRID scenario which SOS VM id is non-zero. Now introducing a new VM id concept for DM/VHM hypercall APIs, that return a relative VM id which is from SOS view when create VM for post- launched VMs. DM/VHM could always treat their own vm id is "0". When they make hypercalls, hypervisor will convert the VM id to the absolute id when dispatch the hypercalls. Tracked-On: #3214 Signed-off-by: Victor Sun <victor.sun@intel.com> Acked-by: Eddie Dong <eddie.dong@Intel.com>
1 parent 3d3de6b commit f83ddd3

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

hypervisor/arch/x86/guest/vmcall.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
2828
uint64_t param1 = vcpu_get_gpreg(vcpu, CPU_REG_RDI);
2929
/* hypercall param2 from guest*/
3030
uint64_t param2 = vcpu_get_gpreg(vcpu, CPU_REG_RSI);
31-
/* in case hypercall param1 is a vm id */
32-
uint16_t vm_id = (uint16_t)param1;
31+
/* hypercall param1 is a relative vm id from SOS view */
32+
uint16_t relative_vm_id = (uint16_t)param1;
33+
uint16_t vm_id = rel_vmid_2_vmid(sos_vm->vm_id, relative_vm_id);
3334
bool vmid_is_valid = (vm_id < CONFIG_MAX_VM_NUM) ? true : false;
3435
int32_t ret = -1;
3536

@@ -59,7 +60,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
5960
break;
6061

6162
case HC_DESTROY_VM:
62-
/* param1: vmid */
63+
/* param1: relative vmid to sos, vm_id: absolute vmid */
6364
if (vmid_is_valid) {
6465
spinlock_obtain(&vmm_hypercall_lock);
6566
ret = hcall_destroy_vm(vm_id);
@@ -68,7 +69,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
6869
break;
6970

7071
case HC_START_VM:
71-
/* param1: vmid */
72+
/* param1: relative vmid to sos, vm_id: absolute vmid */
7273
if (vmid_is_valid) {
7374
spinlock_obtain(&vmm_hypercall_lock);
7475
ret = hcall_start_vm(vm_id);
@@ -77,7 +78,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
7778
break;
7879

7980
case HC_RESET_VM:
80-
/* param1: vmid */
81+
/* param1: relative vmid to sos, vm_id: absolute vmid */
8182
if (vmid_is_valid) {
8283
spinlock_obtain(&vmm_hypercall_lock);
8384
ret = hcall_reset_vm(vm_id);
@@ -86,7 +87,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
8687
break;
8788

8889
case HC_PAUSE_VM:
89-
/* param1: vmid */
90+
/* param1: relative vmid to sos, vm_id: absolute vmid */
9091
if (vmid_is_valid) {
9192
spinlock_obtain(&vmm_hypercall_lock);
9293
ret = hcall_pause_vm(vm_id);
@@ -95,7 +96,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
9596
break;
9697

9798
case HC_CREATE_VCPU:
98-
/* param1: vmid */
99+
/* param1: relative vmid to sos, vm_id: absolute vmid */
99100
if (vmid_is_valid) {
100101
spinlock_obtain(&vmm_hypercall_lock);
101102
ret = hcall_create_vcpu(sos_vm, vm_id, param2);
@@ -104,7 +105,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
104105
break;
105106

106107
case HC_SET_VCPU_REGS:
107-
/* param1: vmid */
108+
/* param1: relative vmid to sos, vm_id: absolute vmid */
108109
if (vmid_is_valid) {
109110
spinlock_obtain(&vmm_hypercall_lock);
110111
ret = hcall_set_vcpu_regs(sos_vm, vm_id, param2);
@@ -113,22 +114,22 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
113114
break;
114115

115116
case HC_SET_IRQLINE:
116-
/* param1: vmid */
117+
/* param1: relative vmid to sos, vm_id: absolute vmid */
117118
if (vmid_is_valid) {
118119
ret = hcall_set_irqline(sos_vm, vm_id,
119120
(struct acrn_irqline_ops *)&param2);
120121
}
121122
break;
122123

123124
case HC_INJECT_MSI:
124-
/* param1: vmid */
125+
/* param1: relative vmid to sos, vm_id: absolute vmid */
125126
if (vmid_is_valid) {
126127
ret = hcall_inject_msi(sos_vm, vm_id, param2);
127128
}
128129
break;
129130

130131
case HC_SET_IOREQ_BUFFER:
131-
/* param1: vmid */
132+
/* param1: relative vmid to sos, vm_id: absolute vmid */
132133
if (vmid_is_valid) {
133134
spinlock_obtain(&vmm_hypercall_lock);
134135
ret = hcall_set_ioreq_buffer(sos_vm, vm_id, param2);
@@ -137,7 +138,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
137138
break;
138139

139140
case HC_NOTIFY_REQUEST_FINISH:
140-
/* param1: vmid
141+
/* param1: relative vmid to sos, vm_id: absolute vmid
141142
* param2: vcpu_id */
142143
if (vmid_is_valid) {
143144
ret = hcall_notify_ioreq_finish(vm_id,
@@ -150,7 +151,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
150151
break;
151152

152153
case HC_VM_WRITE_PROTECT_PAGE:
153-
/* param1: vmid */
154+
/* param1: relative vmid to sos, vm_id: absolute vmid */
154155
if (vmid_is_valid) {
155156
ret = hcall_write_protect_page(sos_vm, vm_id, param2);
156157
}
@@ -165,35 +166,35 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
165166
break;
166167

167168
case HC_VM_GPA2HPA:
168-
/* param1: vmid */
169+
/* param1: relative vmid to sos, vm_id: absolute vmid */
169170
if (vmid_is_valid) {
170171
ret = hcall_gpa_to_hpa(sos_vm, vm_id, param2);
171172
}
172173
break;
173174

174175
case HC_ASSIGN_PTDEV:
175-
/* param1: vmid */
176+
/* param1: relative vmid to sos, vm_id: absolute vmid */
176177
if (vmid_is_valid) {
177178
ret = hcall_assign_ptdev(sos_vm, vm_id, param2);
178179
}
179180
break;
180181

181182
case HC_DEASSIGN_PTDEV:
182-
/* param1: vmid */
183+
/* param1: relative vmid to sos, vm_id: absolute vmid */
183184
if (vmid_is_valid) {
184185
ret = hcall_deassign_ptdev(sos_vm, vm_id, param2);
185186
}
186187
break;
187188

188189
case HC_SET_PTDEV_INTR_INFO:
189-
/* param1: vmid */
190+
/* param1: relative vmid to sos, vm_id: absolute vmid */
190191
if (vmid_is_valid) {
191192
ret = hcall_set_ptdev_intr_info(sos_vm, vm_id, param2);
192193
}
193194
break;
194195

195196
case HC_RESET_PTDEV_INTR_INFO:
196-
/* param1: vmid */
197+
/* param1: relative vmid to sos, vm_id: absolute vmid */
197198
if (vmid_is_valid) {
198199
ret = hcall_reset_ptdev_intr_info(sos_vm, vm_id, param2);
199200
}
@@ -204,7 +205,7 @@ static int32_t dispatch_sos_hypercall(const struct acrn_vcpu *vcpu)
204205
break;
205206

206207
case HC_VM_INTR_MONITOR:
207-
/* param1: vmid */
208+
/* param1: relative vmid to sos, vm_id: absolute vmid */
208209
if (vmid_is_valid) {
209210
ret = hcall_vm_intr_monitor(sos_vm, vm_id, param2);
210211
}

hypervisor/common/hypercall.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ int32_t hcall_create_vm(struct acrn_vm *vm, uint64_t param)
153153
(void)memset((void *)&cv, 0U, sizeof(cv));
154154
if (copy_from_gpa(vm, &cv, param, sizeof(cv)) == 0) {
155155
vm_id = get_vmid_by_uuid(&cv.uuid[0]);
156-
if ((vm_id < CONFIG_MAX_VM_NUM)
156+
if ((vm_id > vm->vm_id) && (vm_id < CONFIG_MAX_VM_NUM)
157157
&& (is_poweroff_vm(get_vm_from_vmid(vm_id)))) {
158158
vm_config = get_vm_config(vm_id);
159159

@@ -172,7 +172,8 @@ int32_t hcall_create_vm(struct acrn_vm *vm, uint64_t param)
172172
cv.vmid = ACRN_INVALID_VMID;
173173
ret = -1;
174174
} else {
175-
cv.vmid = target_vm->vm_id;
175+
/* return a relative vm_id from SOS view */
176+
cv.vmid = vmid_2_rel_vmid(vm->vm_id, vm_id);
176177
ret = 0;
177178
}
178179

@@ -699,8 +700,11 @@ int32_t hcall_set_vm_memory_regions(struct acrn_vm *vm, uint64_t param)
699700
(void)memset((void *)&regions, 0U, sizeof(regions));
700701

701702
if (copy_from_gpa(vm, &regions, param, sizeof(regions)) == 0) {
702-
if (regions.vmid < CONFIG_MAX_VM_NUM) {
703-
target_vm = get_vm_from_vmid(regions.vmid);
703+
/* the vmid in regions is a relative vm id, need to convert to absolute vm id */
704+
uint16_t target_vmid = rel_vmid_2_vmid(vm->vm_id, regions.vmid);
705+
706+
if (target_vmid < CONFIG_MAX_VM_NUM) {
707+
target_vm = get_vm_from_vmid(target_vmid);
704708
}
705709
if ((target_vm != NULL) && !is_poweroff_vm(target_vm) && is_postlaunched_vm(target_vm)) {
706710
idx = 0U;
@@ -1040,7 +1044,8 @@ int32_t hcall_get_cpu_pm_state(struct acrn_vm *vm, uint64_t cmd, uint64_t param)
10401044
struct acrn_vm *target_vm = NULL;
10411045
int32_t ret = -1;
10421046

1043-
target_vm_id = (uint16_t)((cmd & PMCMD_VMID_MASK) >> PMCMD_VMID_SHIFT);
1047+
/* the vmid in cmd is a relative vm id, need to convert to absolute vm id */
1048+
target_vm_id = rel_vmid_2_vmid(vm->vm_id, (uint16_t)((cmd & PMCMD_VMID_MASK) >> PMCMD_VMID_SHIFT));
10441049
if (target_vm_id < CONFIG_MAX_VM_NUM) {
10451050
target_vm = get_vm_from_vmid(target_vm_id);
10461051
}

hypervisor/include/arch/x86/guest/vm.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ static inline struct acrn_vcpu *vcpu_from_pid(struct acrn_vm *vm, uint16_t pcpu_
180180
return target_vcpu;
181181
}
182182

183+
/* Convert relative vm id to absolute vm id */
184+
static inline uint16_t rel_vmid_2_vmid(uint16_t sos_vmid, uint16_t rel_vmid) {
185+
return (sos_vmid + rel_vmid);
186+
}
187+
188+
/* Convert absolute vm id to relative vm id */
189+
static inline uint16_t vmid_2_rel_vmid(uint16_t sos_vmid, uint16_t vmid) {
190+
return (vmid - sos_vmid);
191+
}
192+
183193
int32_t shutdown_vm(struct acrn_vm *vm);
184194
void pause_vm(struct acrn_vm *vm);
185195
void resume_vm_from_s3(struct acrn_vm *vm, uint32_t wakeup_vec);

0 commit comments

Comments
 (0)