Skip to content

Commit ddb5483

Browse files
Shawnshhwenlingz
authored andcommitted
hv: cpu: 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>
1 parent 7f08ad8 commit ddb5483

File tree

2 files changed

+108
-108
lines changed

2 files changed

+108
-108
lines changed

hypervisor/arch/x86/cpu.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ bool cpu_has_cap(uint32_t bit)
6767
{
6868
uint32_t feat_idx = bit >> 5U;
6969
uint32_t feat_bit = bit & 0x1fU;
70+
bool ret;
7071

7172
if (feat_idx >= FEATURE_WORDS) {
72-
return false;
73+
ret = false;
74+
} else {
75+
ret = ((boot_cpu_data.cpuid_leaves[feat_idx] & (1U << feat_bit)) != 0U);
7376
}
7477

75-
return ((boot_cpu_data.cpuid_leaves[feat_idx] & (1U << feat_bit)) != 0U);
78+
return ret;
7679
}
7780

7881
static inline bool get_monitor_cap(void)
@@ -710,22 +713,21 @@ void cpu_dead(uint16_t pcpu_id)
710713
*/
711714
int halt = 1;
712715

713-
if (bitmap_test_and_clear_lock(pcpu_id, &pcpu_active_bitmap) == false) {
714-
pr_err("pcpu%hu already dead", pcpu_id);
715-
return;
716-
}
717-
718-
/* clean up native stuff */
719-
vmx_off(pcpu_id);
720-
cache_flush_invalidate_all();
716+
if (bitmap_test_and_clear_lock(pcpu_id, &pcpu_active_bitmap)) {
717+
/* clean up native stuff */
718+
vmx_off(pcpu_id);
719+
cache_flush_invalidate_all();
721720

722-
/* Set state to show CPU is dead */
723-
cpu_set_current_state(pcpu_id, PCPU_STATE_DEAD);
721+
/* Set state to show CPU is dead */
722+
cpu_set_current_state(pcpu_id, PCPU_STATE_DEAD);
724723

725-
/* Halt the CPU */
726-
do {
727-
hlt_cpu();
728-
} while (halt != 0);
724+
/* Halt the CPU */
725+
do {
726+
hlt_cpu();
727+
} while (halt != 0);
728+
} else {
729+
pr_err("pcpu%hu already dead", pcpu_id);
730+
}
729731
}
730732

731733
static void set_current_cpu_id(uint16_t pcpu_id)
@@ -804,15 +806,13 @@ static void ept_cap_detect(void)
804806
msr_val = msr_val >> 32U;
805807

806808
/* Check if secondary processor based VM control is available. */
807-
if ((msr_val & VMX_PROCBASED_CTLS_SECONDARY) == 0UL) {
808-
return;
809-
}
809+
if ((msr_val & VMX_PROCBASED_CTLS_SECONDARY) != 0UL) {
810+
/* Read secondary processor based VM control. */
811+
msr_val = msr_read(MSR_IA32_VMX_PROCBASED_CTLS2);
810812

811-
/* Read secondary processor based VM control. */
812-
msr_val = msr_read(MSR_IA32_VMX_PROCBASED_CTLS2);
813-
814-
if (is_ctrl_setting_allowed(msr_val, VMX_PROCBASED_CTLS2_EPT)) {
815-
cpu_caps.ept_features = 1U;
813+
if (is_ctrl_setting_allowed(msr_val, VMX_PROCBASED_CTLS2_EPT)) {
814+
cpu_caps.ept_features = 1U;
815+
}
816816
}
817817
}
818818

hypervisor/arch/x86/cpuid.c

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,24 @@ static inline struct vcpuid_entry *find_vcpuid_entry(const struct acrn_vcpu *vcp
6363
return entry;
6464
}
6565

66-
static inline int set_vcpuid_entry(struct acrn_vm *vm,
66+
static inline int32_t set_vcpuid_entry(struct acrn_vm *vm,
6767
const struct vcpuid_entry *entry)
6868
{
6969
struct vcpuid_entry *tmp;
7070
size_t entry_size = sizeof(struct vcpuid_entry);
71+
int32_t ret;
7172

7273
if (vm->vcpuid_entry_nr == MAX_VM_VCPUID_ENTRIES) {
7374
pr_err("%s, vcpuid entry over MAX_VM_VCPUID_ENTRIES(%u)\n",
7475
__func__, MAX_VM_VCPUID_ENTRIES);
75-
return -ENOMEM;
76+
ret = -ENOMEM;
77+
} else {
78+
tmp = &vm->vcpuid_entries[vm->vcpuid_entry_nr];
79+
vm->vcpuid_entry_nr++;
80+
(void)memcpy_s(tmp, entry_size, entry, entry_size);
81+
ret = 0;
7682
}
77-
78-
tmp = &vm->vcpuid_entries[vm->vcpuid_entry_nr];
79-
vm->vcpuid_entry_nr++;
80-
(void)memcpy_s(tmp, entry_size, entry, entry_size);
81-
return 0;
83+
return ret;
8284
}
8385

8486
/**
@@ -320,102 +322,100 @@ void guest_cpuid(struct acrn_vcpu *vcpu,
320322
*ecx = 0U;
321323
*edx = 0U;
322324
}
323-
324-
return;
325-
}
326-
327-
/* percpu related */
328-
switch (leaf) {
329-
case 0x01U:
330-
{
331-
cpuid(leaf, eax, ebx, ecx, edx);
332-
uint32_t apicid = vlapic_get_apicid(vcpu_vlapic(vcpu));
333-
/* Patching initial APIC ID */
334-
*ebx &= ~APIC_ID_MASK;
335-
*ebx |= (apicid << APIC_ID_SHIFT);
325+
} else {
326+
/* percpu related */
327+
switch (leaf) {
328+
case 0x01U:
329+
{
330+
cpuid(leaf, eax, ebx, ecx, edx);
331+
uint32_t apicid = vlapic_get_apicid(vcpu_vlapic(vcpu));
332+
/* Patching initial APIC ID */
333+
*ebx &= ~APIC_ID_MASK;
334+
*ebx |= (apicid << APIC_ID_SHIFT);
336335

337336
#ifndef CONFIG_MTRR_ENABLED
338-
/* mask mtrr */
339-
*edx &= ~CPUID_EDX_MTRR;
337+
/* mask mtrr */
338+
*edx &= ~CPUID_EDX_MTRR;
340339
#endif
341340

342-
/* mask pcid */
343-
*ecx &= ~CPUID_ECX_PCID;
341+
/* mask pcid */
342+
*ecx &= ~CPUID_ECX_PCID;
344343

345-
/*mask vmx to guest os */
346-
*ecx &= ~CPUID_ECX_VMX;
344+
/*mask vmx to guest os */
345+
*ecx &= ~CPUID_ECX_VMX;
347346

348-
/*no xsave support for guest if it is not enabled on host*/
349-
if ((*ecx & CPUID_ECX_OSXSAVE) == 0U) {
350-
*ecx &= ~CPUID_ECX_XSAVE;
351-
}
347+
/*no xsave support for guest if it is not enabled on host*/
348+
if ((*ecx & CPUID_ECX_OSXSAVE) == 0U) {
349+
*ecx &= ~CPUID_ECX_XSAVE;
350+
}
352351

353-
*ecx &= ~CPUID_ECX_OSXSAVE;
354-
if ((*ecx & CPUID_ECX_XSAVE) != 0U) {
355-
uint64_t cr4;
356-
/*read guest CR4*/
357-
cr4 = exec_vmread(VMX_GUEST_CR4);
358-
if ((cr4 & CR4_OSXSAVE) != 0UL) {
359-
*ecx |= CPUID_ECX_OSXSAVE;
352+
*ecx &= ~CPUID_ECX_OSXSAVE;
353+
if ((*ecx & CPUID_ECX_XSAVE) != 0U) {
354+
uint64_t cr4;
355+
/*read guest CR4*/
356+
cr4 = exec_vmread(VMX_GUEST_CR4);
357+
if ((cr4 & CR4_OSXSAVE) != 0UL) {
358+
*ecx |= CPUID_ECX_OSXSAVE;
359+
}
360360
}
361+
break;
361362
}
362-
break;
363-
}
364363

365-
case 0x0bU:
366-
/* Patching X2APIC */
364+
case 0x0bU:
365+
/* Patching X2APIC */
367366
#ifdef CONFIG_PARTITION_MODE
368-
cpuid_subleaf(leaf, subleaf, eax, ebx, ecx, edx);
369-
#else
370-
if (is_vm0(vcpu->vm)) {
371367
cpuid_subleaf(leaf, subleaf, eax, ebx, ecx, edx);
372-
} else {
373-
*ecx = subleaf & 0xFFU;
374-
*edx = vlapic_get_apicid(vcpu_vlapic(vcpu));
375-
/* No HT emulation for UOS */
376-
switch (subleaf) {
377-
case 0U:
378-
*eax = 0U;
379-
*ebx = 1U;
380-
*ecx |= (1U << 8U);
381-
break;
382-
case 1U:
383-
if (vcpu->vm->hw.created_vcpus == 1U) {
368+
#else
369+
if (is_vm0(vcpu->vm)) {
370+
cpuid_subleaf(leaf, subleaf, eax, ebx, ecx, edx);
371+
} else {
372+
*ecx = subleaf & 0xFFU;
373+
*edx = vlapic_get_apicid(vcpu_vlapic(vcpu));
374+
/* No HT emulation for UOS */
375+
switch (subleaf) {
376+
case 0U:
384377
*eax = 0U;
385-
} else {
386-
*eax = (uint32_t)fls32(vcpu->vm->hw.created_vcpus - 1U) + 1U;
378+
*ebx = 1U;
379+
*ecx |= (1U << 8U);
380+
break;
381+
case 1U:
382+
if (vcpu->vm->hw.created_vcpus == 1U) {
383+
*eax = 0U;
384+
} else {
385+
*eax = (uint32_t)fls32(vcpu->vm->hw.created_vcpus - 1U) + 1U;
386+
}
387+
*ebx = vcpu->vm->hw.created_vcpus;
388+
*ecx |= (2U << 8U);
389+
break;
390+
default:
391+
*eax = 0U;
392+
*ebx = 0U;
393+
*ecx |= (0U << 8U);
394+
break;
387395
}
388-
*ebx = vcpu->vm->hw.created_vcpus;
389-
*ecx |= (2U << 8U);
396+
}
397+
#endif
390398
break;
391-
default:
399+
400+
case 0x0dU:
401+
if (!cpu_has_cap(X86_FEATURE_OSXSAVE)) {
392402
*eax = 0U;
393403
*ebx = 0U;
394-
*ecx |= (0U << 8U);
395-
break;
404+
*ecx = 0U;
405+
*edx = 0U;
406+
} else {
407+
cpuid_subleaf(leaf, subleaf, eax, ebx, ecx, edx);
396408
}
397-
}
398-
#endif
399-
break;
409+
break;
400410

401-
case 0x0dU:
402-
if (!cpu_has_cap(X86_FEATURE_OSXSAVE)) {
403-
*eax = 0U;
404-
*ebx = 0U;
405-
*ecx = 0U;
406-
*edx = 0U;
407-
} else {
408-
cpuid_subleaf(leaf, subleaf, eax, ebx, ecx, edx);
411+
default:
412+
/*
413+
* In this switch statement, leaf shall either be 0x01U or 0x0bU
414+
* or 0x0dU. All the other cases have been handled properly
415+
* before this switch statement.
416+
* Gracefully return if prior case clauses have not been met.
417+
*/
418+
break;
409419
}
410-
break;
411-
412-
default:
413-
/*
414-
* In this switch statement, leaf shall either be 0x01U or 0x0bU
415-
* or 0x0dU. All the other cases have been handled properly
416-
* before this switch statement.
417-
* Gracefully return if prior case clauses have not been met.
418-
*/
419-
break;
420420
}
421421
}

0 commit comments

Comments
 (0)