hv:fix MISRA-C violations in create_vm

-- fix "more than one exit point" and "goto detected" violations
-- change prepare_vm0_memmap to void type
-- Add free_vm_id when create vm failed

Tracked-On: #861
Signed-off-by: Mingqiang Chi <mingqiang.chi@intel.com>
Acked-by: Anthony Xu <anthony.xu@intel.com>
This commit is contained in:
Mingqiang Chi 2018-12-19 16:39:31 +08:00 committed by wenlingz
parent bb47184f3c
commit 81a9de6067
3 changed files with 103 additions and 103 deletions

View File

@ -463,7 +463,7 @@ int32_t copy_to_gva(struct acrn_vcpu *vcpu, void *h_ptr, uint64_t gva,
* @pre vm != NULL * @pre vm != NULL
* @pre is_vm0(vm) == true * @pre is_vm0(vm) == true
*/ */
int32_t prepare_vm0_memmap(struct acrn_vm *vm) void prepare_vm0_memmap(struct acrn_vm *vm)
{ {
uint32_t i; uint32_t i;
uint64_t attr_uc = (EPT_RWX | EPT_UNCACHED); uint64_t attr_uc = (EPT_RWX | EPT_UNCACHED);
@ -507,5 +507,4 @@ int32_t prepare_vm0_memmap(struct acrn_vm *vm)
*/ */
hv_hpa = get_hv_image_base(); hv_hpa = get_hv_image_base();
ept_mr_del(vm, pml4_page, hv_hpa, CONFIG_HV_RAM_SIZE); ept_mr_del(vm, pml4_page, hv_hpa, CONFIG_HV_RAM_SIZE);
return 0;
} }

View File

@ -26,9 +26,7 @@ static inline uint16_t alloc_vm_id(void)
} }
id = ffz64(vmid_bitmap); id = ffz64(vmid_bitmap);
} }
return (id < CONFIG_MAX_VM_NUM) ? id : INVALID_VM_ID;
id = (id >= CONFIG_MAX_VM_NUM) ? INVALID_VM_ID : id;
return id;
} }
static inline void free_vm_id(const struct acrn_vm *vm) static inline void free_vm_id(const struct acrn_vm *vm)
@ -62,9 +60,10 @@ struct acrn_vm *get_vm_from_vmid(uint16_t vm_id)
*/ */
int32_t create_vm(struct vm_description *vm_desc, struct acrn_vm **rtn_vm) int32_t create_vm(struct vm_description *vm_desc, struct acrn_vm **rtn_vm)
{ {
struct acrn_vm *vm; struct acrn_vm *vm = NULL;
int32_t status; int32_t status = 0;
uint16_t vm_id; uint16_t vm_id;
bool need_cleanup = false;
#ifdef CONFIG_PARTITION_MODE #ifdef CONFIG_PARTITION_MODE
vm_id = vm_desc->vm_id; vm_id = vm_desc->vm_id;
@ -72,11 +71,8 @@ int32_t create_vm(struct vm_description *vm_desc, struct acrn_vm **rtn_vm)
#else #else
vm_id = alloc_vm_id(); vm_id = alloc_vm_id();
#endif #endif
if (vm_id >= CONFIG_MAX_VM_NUM) {
pr_err("%s, vm id is invalid!\n", __func__);
return -ENODEV;
}
if (vm_id < CONFIG_MAX_VM_NUM) {
/* Allocate memory for virtual machine */ /* Allocate memory for virtual machine */
vm = &vm_array[vm_id]; vm = &vm_array[vm_id];
(void)memset((void *)vm, 0U, sizeof(struct acrn_vm)); (void)memset((void *)vm, 0U, sizeof(struct acrn_vm));
@ -101,22 +97,23 @@ int32_t create_vm(struct vm_description *vm_desc, struct acrn_vm **rtn_vm)
if (is_vm0(vm)) { if (is_vm0(vm)) {
vm->snoopy_mem = false; vm->snoopy_mem = false;
rebuild_vm0_e820(); rebuild_vm0_e820();
status = prepare_vm0_memmap(vm); prepare_vm0_memmap(vm);
if (status != 0) {
goto err;
}
#ifndef CONFIG_EFI_STUB #ifndef CONFIG_EFI_STUB
status = init_vm_boot_info(vm); status = init_vm_boot_info(vm);
if (status != 0) {
goto err;
}
#endif #endif
if (status == 0) {
init_iommu_vm0_domain(vm); init_iommu_vm0_domain(vm);
} else {
need_cleanup = true;
}
} else { } else {
/* populate UOS vm fields according to vm_desc */ /* populate UOS vm fields according to vm_desc */
vm->sworld_control.flag.supported = vm_desc->sworld_supported; vm->sworld_control.flag.supported = vm_desc->sworld_supported;
if (vm->sworld_control.flag.supported != 0UL) { if (vm->sworld_control.flag.supported != 0UL) {
struct memory_ops *ept_mem_ops = &vm->arch_vm.ept_mem_ops; struct memory_ops *ept_mem_ops = &vm->arch_vm.ept_mem_ops;
ept_mr_add(vm, (uint64_t *)vm->arch_vm.nworld_eptp, ept_mr_add(vm, (uint64_t *)vm->arch_vm.nworld_eptp,
hva2hpa(ept_mem_ops->get_sworld_memory_base(ept_mem_ops->info)), hva2hpa(ept_mem_ops->get_sworld_memory_base(ept_mem_ops->info)),
TRUSTY_EPT_REBASE_GPA, TRUSTY_RAM_SIZE, EPT_WB | EPT_RWX); TRUSTY_EPT_REBASE_GPA, TRUSTY_RAM_SIZE, EPT_WB | EPT_RWX);
@ -132,6 +129,7 @@ int32_t create_vm(struct vm_description *vm_desc, struct acrn_vm **rtn_vm)
#endif #endif
} }
if (status == 0) {
enable_iommu(); enable_iommu();
INIT_LIST_HEAD(&vm->softirq_dev_entry_list); INIT_LIST_HEAD(&vm->softirq_dev_entry_list);
@ -179,22 +177,25 @@ int32_t create_vm(struct vm_description *vm_desc, struct acrn_vm **rtn_vm)
/* Now, enable IO completion polling mode for all VMs with CONFIG_IOREQ_POLLING. */ /* Now, enable IO completion polling mode for all VMs with CONFIG_IOREQ_POLLING. */
vm->sw.is_completion_polling = true; vm->sw.is_completion_polling = true;
#endif #endif
status = set_vcpuid_entries(vm); status = set_vcpuid_entries(vm);
if (status != 0) { if (status == 0) {
goto err; vm->state = VM_CREATED;
} else {
need_cleanup = true;
}
} }
vm->state = VM_CREATED; } else {
pr_err("%s, vm id is invalid!\n", __func__);
return 0; status = -ENODEV;
}
err:
if (need_cleanup && (vm != NULL)) {
if (vm->arch_vm.nworld_eptp != NULL) { if (vm->arch_vm.nworld_eptp != NULL) {
(void)memset(vm->arch_vm.nworld_eptp, 0U, PAGE_SIZE); (void)memset(vm->arch_vm.nworld_eptp, 0U, PAGE_SIZE);
} }
free_vm_id(vm);
}
return status; return status;
} }

View File

@ -70,7 +70,7 @@
#define LDTR_AR (0x0082U) /* LDT, type must be 2, refer to SDM Vol3 26.3.1.2 */ #define LDTR_AR (0x0082U) /* LDT, type must be 2, refer to SDM Vol3 26.3.1.2 */
#define TR_AR (0x008bU) /* TSS (busy), refer to SDM Vol3 26.3.1.2 */ #define TR_AR (0x008bU) /* TSS (busy), refer to SDM Vol3 26.3.1.2 */
int32_t prepare_vm0_memmap(struct acrn_vm *vm); void prepare_vm0_memmap(struct acrn_vm *vm);
/* Definition for a mem map lookup */ /* Definition for a mem map lookup */
struct vm_lu_mem_map { struct vm_lu_mem_map {