Skip to content

Commit

Permalink
hv: treewide: fix 'Shifting value too far'
Browse files Browse the repository at this point in the history
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 <shiqing.gao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
  • Loading branch information
shiqingg authored and lijinxia committed Aug 14, 2018
1 parent a9151ff commit 6744a17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 8 additions & 6 deletions hypervisor/arch/x86/guest/vlapic.c
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions hypervisor/arch/x86/lapic.c
Expand Up @@ -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 */
Expand Down

0 comments on commit 6744a17

Please sign in to comment.