Skip to content

Commit 71b047c

Browse files
shiqingglijinxia
authored andcommitted
hv: fix 'Switch case not terminated with break'
MISRA-C requires that every switch case shall be terminated with break to avoid the unintentional fall through. The code will become redundant if we enforce this rule. So, we will keep the current implementation for the following two cases. 1. The fall through is intentional. 2. The function is returned in the switch case. If we decide to eliminate the mutiple returns in one function later, this case would be handled properly at that time. What this patch does: - add the mssing break for the default case - add the pre condition for some functions and remove the corresponding panic which will never happen since the function caller could guarantee the pre condition based on the code implementation v1 -> v2: * remove the redundant cases above default in 'vlapic_get_lvtptr' * add the similar pre condition for 'lvt_off_to_idx' as 'vlapic_get_lvtptr' since all the function callers could guarantee it * remove the assertion in 'lvt_off_to_idx' since the pre condition could guarantee that the assertion will never happen * add the similar pre condition for 'vpic_set_irqstate' as 'vioapic_set_irqstate' since all the function callers could guarantee it * remove the assertion in 'vpic_set_irqstate' since the pre condition could guarantee that the assertion will never happen Tracked-On: #861 Signed-off-by: Shiqing Gao <shiqing.gao@intel.com> Reviewed-by: Junjie Mao <junjie.mao@intel.com>
1 parent f375885 commit 71b047c

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

hypervisor/arch/x86/guest/vlapic.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,20 @@ vlapic_set_intr_ready(struct acrn_vlapic *vlapic, uint32_t vector, bool level)
518518
return 1;
519519
}
520520

521+
/**
522+
* @pre offset value shall be one of the folllowing values:
523+
* APIC_OFFSET_CMCI_LVT
524+
* APIC_OFFSET_TIMER_LVT
525+
* APIC_OFFSET_THERM_LVT
526+
* APIC_OFFSET_PERF_LVT
527+
* APIC_OFFSET_LINT0_LVT
528+
* APIC_OFFSET_LINT1_LVT
529+
* APIC_OFFSET_ERROR_LVT
530+
*/
521531
static inline uint32_t
522532
lvt_off_to_idx(uint32_t offset)
523533
{
524-
uint32_t index = ~0U;
534+
uint32_t index;
525535

526536
switch (offset) {
527537
case APIC_OFFSET_CMCI_LVT:
@@ -543,24 +553,29 @@ lvt_off_to_idx(uint32_t offset)
543553
index = APIC_LVT_LINT1;
544554
break;
545555
case APIC_OFFSET_ERROR_LVT:
546-
index = APIC_LVT_ERROR;
547-
break;
548556
default:
549557
/*
550-
* For the offset that is not handled (an invalid offset of
551-
* Local Vector Table), its index is assigned to a default
552-
* value, which indicates an invalid index.
553-
* The index will be checked later to guarantee the validity.
558+
* The function caller could guarantee the pre condition.
559+
* So, all of the possible 'offset' other than
560+
* APIC_OFFSET_ERROR_LVT has been handled in prior cases.
554561
*/
562+
index = APIC_LVT_ERROR;
555563
break;
556564
}
557-
ASSERT(index <= VLAPIC_MAXLVT_INDEX,
558-
"%s: invalid lvt index %u for offset %#x",
559-
__func__, index, offset);
560565

561566
return index;
562567
}
563568

569+
/**
570+
* @pre offset value shall be one of the folllowing values:
571+
* APIC_OFFSET_CMCI_LVT
572+
* APIC_OFFSET_TIMER_LVT
573+
* APIC_OFFSET_THERM_LVT
574+
* APIC_OFFSET_PERF_LVT
575+
* APIC_OFFSET_LINT0_LVT
576+
* APIC_OFFSET_LINT1_LVT
577+
* APIC_OFFSET_ERROR_LVT
578+
*/
564579
static inline uint32_t *
565580
vlapic_get_lvtptr(struct acrn_vlapic *vlapic, uint32_t offset)
566581
{
@@ -570,16 +585,14 @@ vlapic_get_lvtptr(struct acrn_vlapic *vlapic, uint32_t offset)
570585
switch (offset) {
571586
case APIC_OFFSET_CMCI_LVT:
572587
return &lapic->lvt_cmci.v;
573-
case APIC_OFFSET_TIMER_LVT:
574-
case APIC_OFFSET_THERM_LVT:
575-
case APIC_OFFSET_PERF_LVT:
576-
case APIC_OFFSET_LINT0_LVT:
577-
case APIC_OFFSET_LINT1_LVT:
578-
case APIC_OFFSET_ERROR_LVT:
588+
default:
589+
/*
590+
* The function caller could guarantee the pre condition.
591+
* All the possible 'offset' other than APIC_OFFSET_CMCI_LVT
592+
* could be handled here.
593+
*/
579594
i = lvt_off_to_idx(offset);
580595
return &(lapic->lvt[i].v);
581-
default:
582-
panic("vlapic_get_lvt: invalid LVT\n");
583596
}
584597
}
585598

hypervisor/arch/x86/mtrr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static uint32_t update_ept(struct vm *vm, uint64_t start,
145145
case MTRR_MEM_TYPE_UC:
146146
default:
147147
attr = EPT_UNCACHED;
148+
break;
148149
}
149150

150151
ept_mr_modify(vm, (uint64_t *)vm->arch_vm.nworld_eptp,

hypervisor/dm/vioapic.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ enum irqstate {
128128

129129
/**
130130
* @pre irq < vioapic_pincount(vm)
131+
* @pre irqstate value shall be one of the folllowing values:
132+
* IRQSTATE_ASSERT
133+
* IRQSTATE_DEASSERT
134+
* IRQSTATE_PULSE
131135
*/
132136
static void
133137
vioapic_set_irqstate(struct vm *vm, uint32_t irq, enum irqstate irqstate)
@@ -150,7 +154,11 @@ vioapic_set_irqstate(struct vm *vm, uint32_t irq, enum irqstate irqstate)
150154
vioapic_set_pinstate(vioapic, pin, false);
151155
break;
152156
default:
153-
panic("vioapic_set_irqstate: invalid irqstate %d", irqstate);
157+
/*
158+
* The function caller could guarantee the pre condition.
159+
* All the possible 'irqstate' has been handled in prior cases.
160+
*/
161+
break;
154162
}
155163
spinlock_release(&(vioapic->mtx));
156164
}

hypervisor/dm/vpic.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ static void vpic_set_pinstate(struct acrn_vpic *vpic, uint8_t pin, bool newstate
453453

454454
/**
455455
* @pre irq < NR_VPIC_PINS_TOTAL
456+
* @pre irqstate value shall be one of the folllowing values:
457+
* IRQSTATE_ASSERT
458+
* IRQSTATE_DEASSERT
459+
* IRQSTATE_PULSE
456460
*/
457461
static void vpic_set_irqstate(struct vm *vm, uint32_t irq,
458462
enum irqstate irqstate)
@@ -486,7 +490,11 @@ static void vpic_set_irqstate(struct vm *vm, uint32_t irq,
486490
vpic_set_pinstate(vpic, pin, false);
487491
break;
488492
default:
489-
ASSERT(false, "vpic_set_irqstate: invalid irqstate");
493+
/*
494+
* The function caller could guarantee the pre condition.
495+
* All the possible 'irqstate' has been handled in prior cases.
496+
*/
497+
break;
490498
}
491499
spinlock_release(&(vpic->lock));
492500
}

0 commit comments

Comments
 (0)