hv: guest: fix "Procedure has more than one exit point"

IEC 61508,ISO 26262 standards highly recommend single-exit rule.

Reduce the count of the "return entries".
Fix the violations which is comply with the cases list below:
1.Function has 2 return entries.
2.The first return entry is used to return the error code of
checking variable whether is valid.

Fix the violations in "if else" format.

Tracked-On: #861
Signed-off-by: Huihuang Shi <huihuang.shi@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Huihuang Shi 2018-11-28 10:24:26 +08:00 committed by lijinxia
parent c32d41a0be
commit 17a6d9446e
4 changed files with 174 additions and 170 deletions

View File

@ -1462,12 +1462,10 @@ static int emulate_sub(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
break; break;
} }
if (error != 0) { if (error == 0) {
return error;
}
rflags2 = getcc(size, val1, val2); rflags2 = getcc(size, val1, val2);
vie_update_rflags(vcpu, rflags2, RFLAGS_STATUS_BITS); vie_update_rflags(vcpu, rflags2, RFLAGS_STATUS_BITS);
}
return error; return error;
} }
@ -1494,11 +1492,12 @@ static int emulate_group1(struct acrn_vcpu *vcpu, const struct instr_emul_vie *v
return error; return error;
} }
static int emulate_bittest(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie) static int32_t emulate_bittest(struct acrn_vcpu *vcpu, const struct instr_emul_vie *vie)
{ {
uint64_t val, rflags, bitmask; uint64_t val, rflags, bitmask;
uint64_t bitoff; uint64_t bitoff;
uint8_t size; uint8_t size;
int32_t ret;
/* /*
* 0F BA is a Group 8 extended opcode. * 0F BA is a Group 8 extended opcode.
@ -1506,10 +1505,7 @@ static int emulate_bittest(struct acrn_vcpu *vcpu, const struct instr_emul_vie *
* Currently we only emulate the 'Bit Test' instruction which is * Currently we only emulate the 'Bit Test' instruction which is
* identified by a ModR/M:reg encoding of 100b. * identified by a ModR/M:reg encoding of 100b.
*/ */
if ((vie->reg & 7U) != 4U) { if ((vie->reg & 7U) == 4U) {
return -EINVAL;
}
rflags = vm_get_register(vcpu, CPU_REG_RFLAGS); rflags = vm_get_register(vcpu, CPU_REG_RFLAGS);
vie_mmio_read(vcpu, &val); vie_mmio_read(vcpu, &val);
@ -1529,8 +1525,12 @@ static int emulate_bittest(struct acrn_vcpu *vcpu, const struct instr_emul_vie *
} }
size = 8U; size = 8U;
vie_update_register(vcpu, CPU_REG_RFLAGS, rflags, size); vie_update_register(vcpu, CPU_REG_RFLAGS, rflags, size);
ret = 0;
} else {
ret = -EINVAL;
}
return 0; return ret;
} }
static int vmm_emulate_instruction(struct instr_emul_ctxt *ctxt) static int vmm_emulate_instruction(struct instr_emul_ctxt *ctxt)
@ -1539,9 +1539,7 @@ static int vmm_emulate_instruction(struct instr_emul_ctxt *ctxt)
struct acrn_vcpu *vcpu = ctxt->vcpu; struct acrn_vcpu *vcpu = ctxt->vcpu;
int error; int error;
if (vie->decoded == 0U) { if (vie->decoded != 0U) {
return -EINVAL;
}
switch (vie->op.op_type) { switch (vie->op.op_type) {
case VIE_OP_TYPE_GROUP1: case VIE_OP_TYPE_GROUP1:
error = emulate_group1(vcpu, vie); error = emulate_group1(vcpu, vie);
@ -1581,6 +1579,10 @@ static int vmm_emulate_instruction(struct instr_emul_ctxt *ctxt)
error = -EINVAL; error = -EINVAL;
break; break;
} }
} else {
error = -EINVAL;
}
return error; return error;
} }
@ -2141,19 +2143,21 @@ static int local_decode_instruction(enum vm_cpu_mode cpu_mode,
} }
/* for instruction MOVS/STO, check the gva gotten from DI/SI. */ /* for instruction MOVS/STO, check the gva gotten from DI/SI. */
static int instr_check_di(struct acrn_vcpu *vcpu, struct instr_emul_ctxt *emul_ctxt) static int32_t instr_check_di(struct acrn_vcpu *vcpu, struct instr_emul_ctxt *emul_ctxt)
{ {
int ret; int32_t ret;
struct instr_emul_vie *vie = &emul_ctxt->vie; struct instr_emul_vie *vie = &emul_ctxt->vie;
uint64_t gva; uint64_t gva;
ret = get_gva_di_check(vcpu, vie, vie->addrsize, &gva); ret = get_gva_di_check(vcpu, vie, vie->addrsize, &gva);
if (ret < 0) { if (ret < 0) {
return -EFAULT; ret = -EFAULT;
} else {
ret = 0;
} }
return 0; return ret;
} }
static int instr_check_gva(struct acrn_vcpu *vcpu, struct instr_emul_ctxt *emul_ctxt, static int instr_check_gva(struct acrn_vcpu *vcpu, struct instr_emul_ctxt *emul_ctxt,
@ -2305,14 +2309,17 @@ int decode_instruction(struct acrn_vcpu *vcpu)
return (int)(emul_ctxt->vie.opsize); return (int)(emul_ctxt->vie.opsize);
} }
int emulate_instruction(const struct acrn_vcpu *vcpu) int32_t emulate_instruction(const struct acrn_vcpu *vcpu)
{ {
struct instr_emul_ctxt *ctxt = &per_cpu(g_inst_ctxt, vcpu->pcpu_id); struct instr_emul_ctxt *ctxt = &per_cpu(g_inst_ctxt, vcpu->pcpu_id);
int32_t ret;
if (ctxt == NULL) { if (ctxt == NULL) {
pr_err("%s: Failed to get instr_emul_ctxt", __func__); pr_err("%s: Failed to get instr_emul_ctxt", __func__);
return -1; ret = -1;
} else {
ret = vmm_emulate_instruction(ctxt);
} }
return vmm_emulate_instruction(ctxt); return ret;
} }

View File

@ -193,7 +193,7 @@ struct instr_emul_ctxt {
struct acrn_vcpu *vcpu; struct acrn_vcpu *vcpu;
}; };
int emulate_instruction(const struct acrn_vcpu *vcpu); int32_t emulate_instruction(const struct acrn_vcpu *vcpu);
int decode_instruction(struct acrn_vcpu *vcpu); int decode_instruction(struct acrn_vcpu *vcpu);
#endif #endif

View File

@ -39,11 +39,8 @@ static void vm_setup_cpu_px(struct acrn_vm *vm)
(void)memset(vm->pm.px_data, 0U, (void)memset(vm->pm.px_data, 0U,
MAX_PSTATE * sizeof(struct cpu_px_data)); MAX_PSTATE * sizeof(struct cpu_px_data));
if ((boot_cpu_data.state_info.px_cnt == 0U) if ((boot_cpu_data.state_info.px_cnt != 0U)
|| (boot_cpu_data.state_info.px_data == NULL)) { && (boot_cpu_data.state_info.px_data != NULL)) {
return;
}
ASSERT ((boot_cpu_data.state_info.px_cnt <= MAX_PSTATE), ASSERT ((boot_cpu_data.state_info.px_cnt <= MAX_PSTATE),
"failed to setup cpu px"); "failed to setup cpu px");
@ -53,7 +50,7 @@ static void vm_setup_cpu_px(struct acrn_vm *vm)
(void)memcpy_s(vm->pm.px_data, px_data_size, (void)memcpy_s(vm->pm.px_data, px_data_size,
boot_cpu_data.state_info.px_data, px_data_size); boot_cpu_data.state_info.px_data, px_data_size);
}
} }
static void vm_setup_cpu_cx(struct acrn_vm *vm) static void vm_setup_cpu_cx(struct acrn_vm *vm)
@ -64,11 +61,8 @@ static void vm_setup_cpu_cx(struct acrn_vm *vm)
(void)memset(vm->pm.cx_data, 0U, (void)memset(vm->pm.cx_data, 0U,
MAX_CSTATE * sizeof(struct cpu_cx_data)); MAX_CSTATE * sizeof(struct cpu_cx_data));
if ((boot_cpu_data.state_info.cx_cnt == 0U) if ((boot_cpu_data.state_info.cx_cnt != 0U)
|| (boot_cpu_data.state_info.cx_data == NULL)) { && (boot_cpu_data.state_info.cx_data != NULL)) {
return;
}
ASSERT ((boot_cpu_data.state_info.cx_cnt <= MAX_CX_ENTRY), ASSERT ((boot_cpu_data.state_info.cx_cnt <= MAX_CX_ENTRY),
"failed to setup cpu cx"); "failed to setup cpu cx");
@ -81,7 +75,7 @@ static void vm_setup_cpu_cx(struct acrn_vm *vm)
*/ */
(void)memcpy_s(vm->pm.cx_data + 1, cx_data_size, (void)memcpy_s(vm->pm.cx_data + 1, cx_data_size,
boot_cpu_data.state_info.cx_data, cx_data_size); boot_cpu_data.state_info.cx_data, cx_data_size);
}
} }
static inline void init_cx_port(struct acrn_vm *vm) static inline void init_cx_port(struct acrn_vm *vm)
@ -192,13 +186,10 @@ register_gas_io_handler(struct acrn_vm *vm, uint32_t pio_idx, const struct acpi_
uint8_t io_len[5] = {0, 1, 2, 4, 8}; uint8_t io_len[5] = {0, 1, 2, 4, 8};
struct vm_io_range gas_io; struct vm_io_range gas_io;
if ((gas->address == 0UL) if ((gas->address != 0UL)
|| (gas->space_id != SPACE_SYSTEM_IO) && (gas->space_id == SPACE_SYSTEM_IO)
|| (gas->access_size == 0U) && (gas->access_size != 0U)
|| (gas->access_size > 4U)) { && (gas->access_size <= 4U)) {
return;
}
gas_io.flags = IO_ATTR_RW; gas_io.flags = IO_ATTR_RW;
gas_io.base = (uint16_t)gas->address; gas_io.base = (uint16_t)gas->address;
gas_io.len = io_len[gas->access_size]; gas_io.len = io_len[gas->access_size];
@ -208,6 +199,7 @@ register_gas_io_handler(struct acrn_vm *vm, uint32_t pio_idx, const struct acpi_
pr_dbg("Enable PM1A trap for VM %d, port 0x%x, size %d\n", pr_dbg("Enable PM1A trap for VM %d, port 0x%x, size %d\n",
vm->vm_id, gas_io.base, gas_io.len); vm->vm_id, gas_io.base, gas_io.len);
}
} }
void register_pm1ab_handler(struct acrn_vm *vm) void register_pm1ab_handler(struct acrn_vm *vm)

View File

@ -45,11 +45,15 @@ static inline bool is_vm_valid(uint16_t vm_id)
*/ */
struct acrn_vm *get_vm_from_vmid(uint16_t vm_id) struct acrn_vm *get_vm_from_vmid(uint16_t vm_id)
{ {
struct acrn_vm *ret;
if (is_vm_valid(vm_id)) { if (is_vm_valid(vm_id)) {
return &vm_array[vm_id]; ret = &vm_array[vm_id];
} else { } else {
return NULL; ret = NULL;
} }
return ret;
} }
/** /**
@ -188,19 +192,16 @@ err:
/* /*
* @pre vm != NULL * @pre vm != NULL
*/ */
int shutdown_vm(struct acrn_vm *vm) int32_t shutdown_vm(struct acrn_vm *vm)
{ {
int status = 0;
uint16_t i; uint16_t i;
struct acrn_vcpu *vcpu = NULL; struct acrn_vcpu *vcpu = NULL;
int32_t ret;
pause_vm(vm); pause_vm(vm);
/* Only allow shutdown paused vm */ /* Only allow shutdown paused vm */
if (vm->state != VM_PAUSED) { if (vm->state == VM_PAUSED) {
return -EINVAL;
}
foreach_vcpu(i, vm, vcpu) { foreach_vcpu(i, vm, vcpu) {
reset_vcpu(vcpu); reset_vcpu(vcpu);
offline_vcpu(vcpu); offline_vcpu(vcpu);
@ -220,9 +221,13 @@ int shutdown_vm(struct acrn_vm *vm)
free_vm_id(vm); free_vm_id(vm);
vpci_cleanup(vm); vpci_cleanup(vm);
ret = 0;
} else {
ret = -EINVAL;
}
/* Return status to caller */ /* Return status to caller */
return status; return ret;
} }
/** /**
@ -244,15 +249,13 @@ int start_vm(struct acrn_vm *vm)
/** /**
* * @pre vm != NULL * * @pre vm != NULL
*/ */
int reset_vm(struct acrn_vm *vm) int32_t reset_vm(struct acrn_vm *vm)
{ {
uint16_t i; uint16_t i;
struct acrn_vcpu *vcpu = NULL; struct acrn_vcpu *vcpu = NULL;
int32_t ret;
if (vm->state != VM_PAUSED) { if (vm->state == VM_PAUSED) {
return -1;
}
foreach_vcpu(i, vm, vcpu) { foreach_vcpu(i, vm, vcpu) {
reset_vcpu(vcpu); reset_vcpu(vcpu);
} }
@ -265,8 +268,12 @@ int reset_vm(struct acrn_vm *vm)
vioapic_reset(vm_ioapic(vm)); vioapic_reset(vm_ioapic(vm));
destroy_secure_world(vm, false); destroy_secure_world(vm, false);
vm->sworld_control.flag.active = 0UL; vm->sworld_control.flag.active = 0UL;
ret = 0;
} else {
ret = -1;
}
return 0; return ret;
} }
/** /**
@ -277,15 +284,13 @@ void pause_vm(struct acrn_vm *vm)
uint16_t i; uint16_t i;
struct acrn_vcpu *vcpu = NULL; struct acrn_vcpu *vcpu = NULL;
if (vm->state == VM_PAUSED) { if (vm->state != VM_PAUSED) {
return;
}
vm->state = VM_PAUSED; vm->state = VM_PAUSED;
foreach_vcpu(i, vm, vcpu) { foreach_vcpu(i, vm, vcpu) {
pause_vcpu(vcpu, VCPU_ZOMBIE); pause_vcpu(vcpu, VCPU_ZOMBIE);
} }
}
} }
/** /**