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 is_vm0(vm) == true
*/
int32_t prepare_vm0_memmap(struct acrn_vm *vm)
void prepare_vm0_memmap(struct acrn_vm *vm)
{
uint32_t i;
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();
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 = (id >= CONFIG_MAX_VM_NUM) ? INVALID_VM_ID : id;
return id;
return (id < CONFIG_MAX_VM_NUM) ? id : INVALID_VM_ID;
}
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)
{
struct acrn_vm *vm;
int32_t status;
struct acrn_vm *vm = NULL;
int32_t status = 0;
uint16_t vm_id;
bool need_cleanup = false;
#ifdef CONFIG_PARTITION_MODE
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
vm_id = alloc_vm_id();
#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 */
vm = &vm_array[vm_id];
(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)) {
vm->snoopy_mem = false;
rebuild_vm0_e820();
status = prepare_vm0_memmap(vm);
if (status != 0) {
goto err;
}
prepare_vm0_memmap(vm);
#ifndef CONFIG_EFI_STUB
status = init_vm_boot_info(vm);
if (status != 0) {
goto err;
}
#endif
if (status == 0) {
init_iommu_vm0_domain(vm);
} else {
need_cleanup = true;
}
} else {
/* populate UOS vm fields according to vm_desc */
vm->sworld_control.flag.supported = vm_desc->sworld_supported;
if (vm->sworld_control.flag.supported != 0UL) {
struct memory_ops *ept_mem_ops = &vm->arch_vm.ept_mem_ops;
ept_mr_add(vm, (uint64_t *)vm->arch_vm.nworld_eptp,
hva2hpa(ept_mem_ops->get_sworld_memory_base(ept_mem_ops->info)),
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
}
if (status == 0) {
enable_iommu();
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. */
vm->sw.is_completion_polling = true;
#endif
status = set_vcpuid_entries(vm);
if (status != 0) {
goto err;
if (status == 0) {
vm->state = VM_CREATED;
} else {
need_cleanup = true;
}
}
vm->state = VM_CREATED;
return 0;
err:
} else {
pr_err("%s, vm id is invalid!\n", __func__);
status = -ENODEV;
}
if (need_cleanup && (vm != NULL)) {
if (vm->arch_vm.nworld_eptp != NULL) {
(void)memset(vm->arch_vm.nworld_eptp, 0U, PAGE_SIZE);
}
free_vm_id(vm);
}
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 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 */
struct vm_lu_mem_map {