Skip to content

Commit 1d725c8

Browse files
mingqiangchiwenlingz
authored andcommitted
hv:Replace dynamic memory with static for vcpu
-- Replace dynamic memory allocation with static memory -- Remove parameter check if vcpu is NULL Tracked-On: #861 Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com> Reviewed-by: Li, Fei1 <fei1.li@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 7dd35cb commit 1d725c8

File tree

17 files changed

+81
-117
lines changed

17 files changed

+81
-117
lines changed

hypervisor/arch/x86/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ config RELEASE
3434

3535
config MAX_VM_NUM
3636
int "Maximum number of VM"
37+
range 1 8
38+
default 4
39+
40+
config MAX_VCPUS_PER_VM
41+
int "Maximum number of VCPUS per VM"
42+
range 1 8
3743
default 4
3844

3945
config NR_IOAPICS

hypervisor/arch/x86/guest/guest.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ uint64_t vcpumask2pcpumask(struct vm *vm, uint64_t vdmask)
4242
vcpu_id = ffs64(vdmask)) {
4343
bitmap_clear_lock(vcpu_id, &vdmask);
4444
vcpu = vcpu_from_vid(vm, vcpu_id);
45-
ASSERT(vcpu != NULL, "vcpu_from_vid failed");
4645
bitmap_set_lock(vcpu->pcpu_id, &dmask);
4746
}
4847

@@ -383,6 +382,9 @@ static inline int copy_gpa(struct vm *vm, void *h_ptr_arg, uint64_t gpa_arg,
383382
return 0;
384383
}
385384

385+
/*
386+
* @pre vcpu != NULL && err_code != NULL
387+
*/
386388
static inline int copy_gva(struct vcpu *vcpu, void *h_ptr_arg, uint64_t gva_arg,
387389
uint32_t size_arg, uint32_t *err_code, uint64_t *fault_addr,
388390
bool cp_from_vm)
@@ -394,15 +396,6 @@ static inline int copy_gva(struct vcpu *vcpu, void *h_ptr_arg, uint64_t gva_arg,
394396
uint64_t gva = gva_arg;
395397
uint32_t size = size_arg;
396398

397-
if (vcpu == NULL) {
398-
pr_err("guest virt addr copy need vcpu param");
399-
return -EINVAL;
400-
}
401-
if (err_code == NULL) {
402-
pr_err("guest virt addr copy need err_code param");
403-
return -EINVAL;
404-
}
405-
406399
while (size > 0U) {
407400
ret = gva2gpa(vcpu, gva, &gpa, err_code);
408401
if (ret < 0) {

hypervisor/arch/x86/guest/vcpu.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,25 @@ void set_ap_entry(struct vcpu *vcpu, uint64_t entry)
311311
int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
312312
{
313313
struct vcpu *vcpu;
314-
314+
uint16_t vcpu_id;
315315

316316
pr_info("Creating VCPU working on PCPU%hu", pcpu_id);
317317

318+
/*
319+
* vcpu->vcpu_id = vm->hw.created_vcpus;
320+
* vm->hw.created_vcpus++;
321+
*/
322+
vcpu_id = atomic_xadd16(&vm->hw.created_vcpus, 1U);
323+
if (vcpu_id >= CONFIG_MAX_VCPUS_PER_VM) {
324+
pr_err("%s, vcpu id is invalid!\n", __func__);
325+
return -EINVAL;
326+
}
318327
/* Allocate memory for VCPU */
319-
vcpu = calloc(1U, sizeof(struct vcpu));
320-
ASSERT(vcpu != NULL, "");
328+
vcpu = &(vm->hw.vcpu_array[vcpu_id]);
329+
(void)memset((void *)vcpu, 0U, sizeof(struct vcpu));
321330

322-
/* Initialize the physical CPU ID for this VCPU */
331+
/* Initialize CPU ID for this VCPU */
332+
vcpu->vcpu_id = vcpu_id;
323333
vcpu->pcpu_id = pcpu_id;
324334
per_cpu(ever_run_vcpu, pcpu_id) = vcpu;
325335

@@ -334,19 +344,6 @@ int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
334344
* needs revise.
335345
*/
336346

337-
/*
338-
* vcpu->vcpu_id = vm->hw.created_vcpus;
339-
* vm->hw.created_vcpus++;
340-
*/
341-
vcpu->vcpu_id = atomic_xadd16(&vm->hw.created_vcpus, 1U);
342-
/* vm->hw.vcpu_array[vcpu->vcpu_id] = vcpu; */
343-
atomic_store64(
344-
(uint64_t *)&vm->hw.vcpu_array[vcpu->vcpu_id],
345-
(uint64_t)vcpu);
346-
347-
ASSERT(vcpu->vcpu_id < vm->hw.num_vcpus,
348-
"Allocated vcpu_id is out of range!");
349-
350347
per_cpu(vcpu, pcpu_id) = vcpu;
351348

352349
pr_info("PCPU%d is working as VM%d VCPU%d, Role: %s",
@@ -384,6 +381,9 @@ int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle)
384381
return 0;
385382
}
386383

384+
/*
385+
* @pre vcpu != NULL
386+
*/
387387
int run_vcpu(struct vcpu *vcpu)
388388
{
389389
uint32_t instlen, cs_attr;
@@ -392,8 +392,6 @@ int run_vcpu(struct vcpu *vcpu)
392392
&vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context].run_ctx;
393393
int64_t status = 0;
394394

395-
ASSERT(vcpu != NULL, "Incorrect arguments");
396-
397395
if (bitmap_test_and_clear_lock(CPU_REG_RIP, &vcpu->reg_updated))
398396
exec_vmwrite(VMX_GUEST_RIP, ctx->rip);
399397
if (bitmap_test_and_clear_lock(CPU_REG_RSP, &vcpu->reg_updated))
@@ -486,21 +484,15 @@ int shutdown_vcpu(__unused struct vcpu *vcpu)
486484
return 0;
487485
}
488486

489-
void destroy_vcpu(struct vcpu *vcpu)
487+
/*
488+
* @pre vcpu != NULL
489+
*/
490+
void offline_vcpu(struct vcpu *vcpu)
490491
{
491-
ASSERT(vcpu != NULL, "Incorrect arguments");
492-
493-
/* vcpu->vm->hw.vcpu_array[vcpu->vcpu_id] = NULL; */
494-
atomic_store64(
495-
(uint64_t *)&vcpu->vm->hw.vcpu_array[vcpu->vcpu_id],
496-
(uint64_t)NULL);
497-
498-
atomic_dec16(&vcpu->vm->hw.created_vcpus);
499-
500492
vlapic_free(vcpu);
501493
per_cpu(ever_run_vcpu, vcpu->pcpu_id) = NULL;
502494
free_pcpu(vcpu->pcpu_id);
503-
free(vcpu);
495+
vcpu->state = VCPU_OFFLINE;
504496
}
505497

506498
/* NOTE:
@@ -602,7 +594,9 @@ int prepare_vcpu(struct vm *vm, uint16_t pcpu_id)
602594
struct vcpu *vcpu = NULL;
603595

604596
ret = create_vcpu(pcpu_id, vm, &vcpu);
605-
ASSERT(ret == 0, "vcpu create failed");
597+
if (ret != 0) {
598+
return ret;
599+
}
606600

607601
if (!vm_sw_loader) {
608602
vm_sw_loader = general_sw_loader;

hypervisor/arch/x86/guest/vlapic.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ vm_lapic_from_vcpu_id(struct vm *vm, uint16_t vcpu_id)
108108
struct vcpu *vcpu;
109109

110110
vcpu = vcpu_from_vid(vm, vcpu_id);
111-
ASSERT(vcpu != NULL, "vm%d, vcpu%hu", vm->vm_id, vcpu_id);
112111

113112
return vcpu_vlapic(vcpu);
114113
}
@@ -119,7 +118,6 @@ vm_lapic_from_pcpuid(struct vm *vm, uint16_t pcpu_id)
119118
struct vcpu *vcpu;
120119

121120
vcpu = vcpu_from_pid(vm, pcpu_id);
122-
ASSERT(vcpu != NULL, "vm%d, pcpu%hu", vm->vm_id, pcpu_id);
123121

124122
return vcpu_vlapic(vcpu);
125123
}
@@ -1996,14 +1994,13 @@ int vlapic_create(struct vcpu *vcpu)
19961994
return 0;
19971995
}
19981996

1997+
/*
1998+
* @pre vcpu != NULL
1999+
*/
19992000
void vlapic_free(struct vcpu *vcpu)
20002001
{
20012002
struct acrn_vlapic *vlapic = NULL;
20022003

2003-
if (vcpu == NULL) {
2004-
return;
2005-
}
2006-
20072004
vlapic = vcpu_vlapic(vcpu);
20082005

20092006
del_timer(&vlapic->vtimer.timer);

hypervisor/arch/x86/guest/vm.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ static inline bool is_vm_valid(uint16_t vm_id)
4040
return bitmap_test(vm_id, &vmid_bitmap);
4141
}
4242

43-
static void init_vm(struct vm_description *vm_desc,
44-
struct vm *vm_handle)
45-
{
46-
/* Populate VM attributes from VM description */
47-
vm_handle->hw.num_vcpus = vm_desc->vm_hw_num_cores;
48-
49-
#ifdef CONFIG_PARTITION_MODE
50-
vm_handle->vm_desc = vm_desc;
51-
#endif
52-
}
53-
5443
/* return a pointer to the virtual machine structure associated with
5544
* this VM ID
5645
*/
@@ -87,25 +76,12 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
8776
vm = &vm_array[vm_id];
8877
(void)memset((void *)vm, 0U, sizeof(struct vm));
8978
vm->vm_id = vm_id;
90-
/*
91-
* Map Virtual Machine to its VM Description
92-
*/
93-
init_vm(vm_desc, vm);
94-
79+
#ifdef CONFIG_PARTITION_MODE
80+
/* Map Virtual Machine to its VM Description */
81+
vm->vm_desc = vm_desc;
82+
#endif
9583
/* Init mmio list */
9684
INIT_LIST_HEAD(&vm->mmio_list);
97-
98-
if (vm->hw.num_vcpus == 0U) {
99-
vm->hw.num_vcpus = phys_cpu_num;
100-
}
101-
vm->hw.vcpu_array =
102-
calloc(1U, sizeof(struct vcpu *) * vm->hw.num_vcpus);
103-
if (vm->hw.vcpu_array == NULL) {
104-
pr_err("%s, vcpu_array allocation failed\n", __func__);
105-
status = -ENOMEM;
106-
goto err;
107-
}
108-
10985
atomic_store16(&vm->hw.created_vcpus, 0U);
11086

11187
/* gpa_lowtop are used for system start up */
@@ -211,9 +187,6 @@ int create_vm(struct vm_description *vm_desc, struct vm **rtn_vm)
211187
free(vm->arch_vm.nworld_eptp);
212188
}
213189

214-
if (vm->hw.vcpu_array != NULL) {
215-
free(vm->hw.vcpu_array);
216-
}
217190
return status;
218191
}
219192

@@ -235,7 +208,7 @@ int shutdown_vm(struct vm *vm)
235208

236209
foreach_vcpu(i, vm, vcpu) {
237210
reset_vcpu(vcpu);
238-
destroy_vcpu(vcpu);
211+
offline_vcpu(vcpu);
239212
}
240213

241214
ptdev_release_all_entries(vm);
@@ -267,7 +240,6 @@ int shutdown_vm(struct vm *vm)
267240
#ifdef CONFIG_PARTITION_MODE
268241
vpci_cleanup(vm);
269242
#endif
270-
free(vm->hw.vcpu_array);
271243

272244
/* Return status to caller */
273245
return status;
@@ -284,7 +256,6 @@ int start_vm(struct vm *vm)
284256

285257
/* Only start BSP (vid = 0) and let BSP start other APs */
286258
vcpu = vcpu_from_vid(vm, 0U);
287-
ASSERT(vcpu != NULL, "vm%d, vcpu0", vm->vm_id);
288259
schedule_vcpu(vcpu);
289260

290261
return 0;

hypervisor/arch/x86/io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ int register_mmio_emulation_handler(struct vm *vm,
479479
int status = -EINVAL;
480480
struct mem_io_node *mmio_node;
481481

482-
if ((vm->hw.created_vcpus > 0U) && vm->hw.vcpu_array[0]->launched) {
482+
if ((vm->hw.created_vcpus > 0U) && vm->hw.vcpu_array[0].launched) {
483483
ASSERT(false, "register mmio handler after vm launched");
484484
return status;
485485
}

hypervisor/arch/x86/virq.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -563,22 +563,16 @@ void cancel_event_injection(struct vcpu *vcpu)
563563
}
564564
}
565565

566+
/*
567+
* @pre vcpu != NULL
568+
*/
566569
int exception_vmexit_handler(struct vcpu *vcpu)
567570
{
568571
uint32_t intinfo, int_err_code = 0U;
569572
uint32_t exception_vector = VECTOR_INVALID;
570573
uint32_t cpl;
571574
int status = 0;
572575

573-
if (vcpu == NULL) {
574-
TRACE_4I(TRACE_VMEXIT_EXCEPTION_OR_NMI, 0U, 0U, 0U, 0U);
575-
status = -EINVAL;
576-
}
577-
578-
if (status != 0) {
579-
return status;
580-
}
581-
582576
pr_dbg(" Handling guest exception");
583577

584578
/* Obtain VM-Exit information field pg 2912 */

hypervisor/arch/x86/vmx.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ void exec_vmxon_instr(uint16_t pcpu_id)
9999
vmxon_region_pa = hva2hpa(vmxon_region_va);
100100
exec_vmxon(&vmxon_region_pa);
101101

102-
if (vcpu != NULL) {
103-
vmcs_pa = hva2hpa(vcpu->arch_vcpu.vmcs);
104-
exec_vmptrld(&vmcs_pa);
105-
}
102+
vmcs_pa = hva2hpa(vcpu->arch_vcpu.vmcs);
103+
exec_vmptrld(&vmcs_pa);
106104
}
107105

108106
void vmx_off(uint16_t pcpu_id)
@@ -111,10 +109,8 @@ void vmx_off(uint16_t pcpu_id)
111109
struct vcpu *vcpu = get_ever_run_vcpu(pcpu_id);
112110
uint64_t vmcs_pa;
113111

114-
if (vcpu != NULL) {
115-
vmcs_pa = hva2hpa(vcpu->arch_vcpu.vmcs);
116-
exec_vmclear((void *)&vmcs_pa);
117-
}
112+
vmcs_pa = hva2hpa(vcpu->arch_vcpu.vmcs);
113+
exec_vmclear((void *)&vmcs_pa);
118114

119115
asm volatile ("vmxoff" : : : "memory");
120116
}

hypervisor/common/hypercall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int32_t hcall_sos_offline_cpu(struct vm *vm, uint64_t lapicid)
4343
}
4444
pause_vcpu(vcpu, VCPU_ZOMBIE);
4545
reset_vcpu(vcpu);
46-
destroy_vcpu(vcpu);
46+
offline_vcpu(vcpu);
4747
}
4848
}
4949

hypervisor/common/io_request.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ static void fire_vhm_interrupt(void)
2121
vm0 = get_vm_from_vmid(0U);
2222

2323
vcpu = vcpu_from_vid(vm0, 0U);
24-
ASSERT(vcpu != NULL, "vcpu_from_vid failed");
2524

2625
vlapic_intr_edge(vcpu, acrn_vhm_vector);
2726
}
@@ -56,15 +55,17 @@ static void acrn_print_request(uint16_t vcpu_id, struct vhm_request *req)
5655
}
5756
}
5857

58+
/*
59+
* @pre vcpu != NULL && io_req != NULL
60+
*/
5961
int32_t
6062
acrn_insert_request_wait(struct vcpu *vcpu, struct io_request *io_req)
6163
{
6264
union vhm_request_buffer *req_buf = NULL;
6365
struct vhm_request *vhm_req;
6466
uint16_t cur;
6567

66-
if ((vcpu == NULL) || (io_req == NULL) ||
67-
(vcpu->vm->sw.io_shared_page == NULL)) {
68+
if (vcpu->vm->sw.io_shared_page == NULL) {
6869
return -EINVAL;
6970
}
7071

0 commit comments

Comments
 (0)