From 6744a179fc998063b2f56a5aac45fceae4cbb32c Mon Sep 17 00:00:00 2001 From: Shiqing Gao Date: Tue, 14 Aug 2018 10:45:44 +0800 Subject: [PATCH] hv: treewide: fix 'Shifting value too far' MISRA-C requires that shift operation cannot exceed the word length. What this patch does: - Add the pre condition for 'init_lapic' regarding to 'pcpu_id' Currently, max 8 physical cpus are supported. Re-design will be required if we would like to support more physical cpus. So, add the pre condition here to avoid the unintentional shift operation mistakes. - Replace the id type with uint8_t in 'vlapic_build_id' - For VM0, it uses 'lapic_id' as its id, which is uint8_t. - For non VM0, it uses 'vcpu_id' as its id, which is uint16_t. Cast this id to uint8_t to make sure there is no loss of data after left shifting 24U. Signed-off-by: Shiqing Gao Acked-by: Eddie Dong --- hypervisor/arch/x86/guest/vlapic.c | 14 ++++++++------ hypervisor/arch/x86/lapic.c | 3 +++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/hypervisor/arch/x86/guest/vlapic.c b/hypervisor/arch/x86/guest/vlapic.c index 567270f78f..6cc16174a9 100644 --- a/hypervisor/arch/x86/guest/vlapic.c +++ b/hypervisor/arch/x86/guest/vlapic.c @@ -169,19 +169,21 @@ static inline uint32_t vlapic_build_id(struct acrn_vlapic *vlapic) { struct vcpu *vcpu = vlapic->vcpu; - uint16_t id; + uint8_t vlapic_id; + uint32_t lapic_regs_id; if (is_vm0(vcpu->vm)) { /* Get APIC ID sequence format from cpu_storage */ - id = per_cpu(lapic_id, vcpu->vcpu_id); + vlapic_id = per_cpu(lapic_id, vcpu->vcpu_id); } else { - id = vcpu->vcpu_id; + vlapic_id = (uint8_t)vcpu->vcpu_id; } - dev_dbg(ACRN_DBG_LAPIC, "vlapic APIC PAGE ID : 0x%08x", - (id << APIC_ID_SHIFT)); + lapic_regs_id = vlapic_id << APIC_ID_SHIFT; - return (id << APIC_ID_SHIFT); + dev_dbg(ACRN_DBG_LAPIC, "vlapic APIC PAGE ID : 0x%08x", lapic_regs_id); + + return lapic_regs_id; } static void diff --git a/hypervisor/arch/x86/lapic.c b/hypervisor/arch/x86/lapic.c index aa35e0ae4b..6c7752c91b 100644 --- a/hypervisor/arch/x86/lapic.c +++ b/hypervisor/arch/x86/lapic.c @@ -209,6 +209,9 @@ void early_init_lapic(void) } } +/** + * @pre pcpu_id < 8U + */ void init_lapic(uint16_t pcpu_id) { /* Set the Logical Destination Register */