Skip to content

Commit 414860f

Browse files
Shawnshhwenlingz
authored andcommitted
hv: dev: 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 ba44417 commit 414860f

File tree

3 files changed

+105
-96
lines changed

3 files changed

+105
-96
lines changed

hypervisor/arch/x86/assign.c

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ ptirq_lookup_entry_by_vpin(struct acrn_vm *vm, uint8_t virt_pin, bool pic_pin)
5555
#ifdef CONFIG_COM_IRQ
5656
static bool ptdev_hv_owned_intx(const struct acrn_vm *vm, const union source_id *virt_sid)
5757
{
58+
bool ret;
59+
5860
/* vm0 vuart pin is owned by hypervisor under debug version */
5961
if (is_vm0(vm) && (virt_sid->intx_id.pin == CONFIG_COM_IRQ)) {
60-
return true;
62+
ret = true;
6163
} else {
62-
return false;
64+
ret = false;
6365
}
66+
67+
return ret;
6468
}
6569
#endif /* CONFIG_COM_IRQ */
6670

@@ -496,37 +500,35 @@ void ptirq_intx_ack(struct acrn_vm *vm, uint8_t virt_pin,
496500
bool pic_pin = (vpin_src == PTDEV_VPIN_PIC);
497501

498502
entry = ptirq_lookup_entry_by_vpin(vm, virt_pin, pic_pin);
499-
if (entry == NULL) {
500-
return;
501-
}
502-
503-
phys_irq = entry->allocated_pirq;
503+
if (entry != NULL) {
504+
phys_irq = entry->allocated_pirq;
504505

505-
/* NOTE: only Level trigger will process EOI/ACK and if we got here
506-
* means we have this vioapic or vpic or both enabled
507-
*/
508-
switch (vpin_src) {
509-
case PTDEV_VPIN_IOAPIC:
510-
if (entry->polarity != 0U) {
511-
vioapic_set_irq(vm, virt_pin, GSI_SET_HIGH);
512-
} else {
513-
vioapic_set_irq(vm, virt_pin, GSI_SET_LOW);
514-
}
515-
break;
516-
case PTDEV_VPIN_PIC:
517-
vpic_set_irq(vm, virt_pin, GSI_SET_LOW);
518-
default:
519-
/*
520-
* In this switch statement, vpin_src shall either be
521-
* PTDEV_VPIN_IOAPIC or PTDEV_VPIN_PIC.
522-
* Gracefully return if prior case clauses have not been met.
506+
/* NOTE: only Level trigger will process EOI/ACK and if we got here
507+
* means we have this vioapic or vpic or both enabled
523508
*/
524-
break;
525-
}
509+
switch (vpin_src) {
510+
case PTDEV_VPIN_IOAPIC:
511+
if (entry->polarity != 0U) {
512+
vioapic_set_irq(vm, virt_pin, GSI_SET_HIGH);
513+
} else {
514+
vioapic_set_irq(vm, virt_pin, GSI_SET_LOW);
515+
}
516+
break;
517+
case PTDEV_VPIN_PIC:
518+
vpic_set_irq(vm, virt_pin, GSI_SET_LOW);
519+
default:
520+
/*
521+
* In this switch statement, vpin_src shall either be
522+
* PTDEV_VPIN_IOAPIC or PTDEV_VPIN_PIC.
523+
* Gracefully return if prior case clauses have not been met.
524+
*/
525+
break;
526+
}
526527

527-
dev_dbg(ACRN_DBG_PTIRQ, "dev-assign: irq=0x%x acked vr: 0x%x",
528-
phys_irq, irq_to_vector(phys_irq));
529-
gsi_unmask_irq(phys_irq);
528+
dev_dbg(ACRN_DBG_PTIRQ, "dev-assign: irq=0x%x acked vr: 0x%x",
529+
phys_irq, irq_to_vector(phys_irq));
530+
gsi_unmask_irq(phys_irq);
531+
}
530532
}
531533

532534
/* Main entry for PCI device assignment with MSI and MSI-X

hypervisor/arch/x86/virq.c

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,29 +266,35 @@ static void vcpu_inject_exception(struct acrn_vcpu *vcpu, uint32_t vector)
266266
vcpu_retain_rip(vcpu);
267267
}
268268

269-
static int vcpu_inject_hi_exception(struct acrn_vcpu *vcpu)
269+
static int32_t vcpu_inject_hi_exception(struct acrn_vcpu *vcpu)
270270
{
271271
uint32_t vector = vcpu->arch.exception_info.exception;
272+
int32_t ret;
272273

273274
if (vector == IDT_MC || vector == IDT_BP || vector == IDT_DB) {
274275
vcpu_inject_exception(vcpu, vector);
275-
return 1;
276+
ret = 1;
277+
} else {
278+
ret = 0;
276279
}
277280

278-
return 0;
281+
return ret;
279282
}
280283

281-
static int vcpu_inject_lo_exception(struct acrn_vcpu *vcpu)
284+
static int32_t vcpu_inject_lo_exception(struct acrn_vcpu *vcpu)
282285
{
283286
uint32_t vector = vcpu->arch.exception_info.exception;
287+
int32_t ret;
284288

285289
/* high priority exception already be injected */
286290
if (vector <= NR_MAX_VECTOR) {
287291
vcpu_inject_exception(vcpu, vector);
288-
return 1;
292+
ret = 1;
293+
} else {
294+
ret = 0;
289295
}
290296

291-
return 0;
297+
return ret;
292298
}
293299

294300
/* Inject external interrupt to guest */
@@ -357,36 +363,38 @@ int interrupt_window_vmexit_handler(struct acrn_vcpu *vcpu)
357363
return 0;
358364
}
359365

360-
int external_interrupt_vmexit_handler(struct acrn_vcpu *vcpu)
366+
int32_t external_interrupt_vmexit_handler(struct acrn_vcpu *vcpu)
361367
{
362368
uint32_t intr_info;
363369
struct intr_excp_ctx ctx;
370+
int32_t ret;
364371

365372
intr_info = exec_vmread32(VMX_EXIT_INT_INFO);
366373
if (((intr_info & VMX_INT_INFO_VALID) == 0U) ||
367374
(((intr_info & VMX_INT_TYPE_MASK) >> 8U)
368375
!= VMX_INT_TYPE_EXT_INT)) {
369376
pr_err("Invalid VM exit interrupt info:%x", intr_info);
370377
vcpu_retain_rip(vcpu);
371-
return -EINVAL;
372-
}
373-
374-
ctx.vector = intr_info & 0xFFU;
375-
ctx.rip = vcpu_get_rip(vcpu);
376-
ctx.rflags = vcpu_get_rflags(vcpu);
377-
ctx.cs = exec_vmread32(VMX_GUEST_CS_SEL);
378+
ret = -EINVAL;
379+
} else {
380+
ctx.vector = intr_info & 0xFFU;
381+
ctx.rip = vcpu_get_rip(vcpu);
382+
ctx.rflags = vcpu_get_rflags(vcpu);
383+
ctx.cs = exec_vmread32(VMX_GUEST_CS_SEL);
378384

379385
#ifdef CONFIG_PARTITION_MODE
380-
partition_mode_dispatch_interrupt(&ctx);
386+
partition_mode_dispatch_interrupt(&ctx);
381387
#else
382-
dispatch_interrupt(&ctx);
388+
dispatch_interrupt(&ctx);
383389
#endif
384390

385-
vcpu_retain_rip(vcpu);
391+
vcpu_retain_rip(vcpu);
386392

387-
TRACE_2L(TRACE_VMEXIT_EXTERNAL_INTERRUPT, ctx.vector, 0UL);
393+
TRACE_2L(TRACE_VMEXIT_EXTERNAL_INTERRUPT, ctx.vector, 0UL);
394+
ret = 0;
395+
}
388396

389-
return 0;
397+
return ret;
390398
}
391399

392400
int acrn_handle_pending_request(struct acrn_vcpu *vcpu)

hypervisor/arch/x86/vtd.c

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,15 @@ static inline uint8_t* get_ctx_table(uint32_t dmar_index, uint8_t bus_no)
144144

145145
bool iommu_snoop_supported(struct acrn_vm *vm)
146146
{
147+
bool ret;
148+
147149
if (vm->iommu == NULL || vm->iommu->iommu_snoop) {
148-
return true;
150+
ret = true;
151+
} else {
152+
ret = false;
149153
}
150154

151-
return false;
155+
return ret;
152156
}
153157

154158
static struct dmar_drhd_rt dmar_drhd_units[CONFIG_MAX_IOMMU_NUM];
@@ -267,12 +271,10 @@ static void iommu_flush_cache(const struct dmar_drhd_rt *dmar_unit,
267271
uint32_t i;
268272

269273
/* if vtd support page-walk coherency, no need to flush cacheline */
270-
if (iommu_ecap_c(dmar_unit->ecap) != 0U) {
271-
return;
272-
}
273-
274-
for (i = 0U; i < size; i += CACHE_LINE_SIZE) {
275-
clflush((char *)p + i);
274+
if (iommu_ecap_c(dmar_unit->ecap) == 0U) {
275+
for (i = 0U; i < size; i += CACHE_LINE_SIZE) {
276+
clflush((char *)p + i);
277+
}
276278
}
277279
}
278280

@@ -478,16 +480,14 @@ static void dmar_write_buffer_flush(struct dmar_drhd_rt *dmar_unit)
478480
{
479481
uint32_t status;
480482

481-
if (iommu_cap_rwbf(dmar_unit->cap) == 0U) {
482-
return;
483-
}
484-
485-
spinlock_obtain(&(dmar_unit->lock));
486-
iommu_write32(dmar_unit, DMAR_GCMD_REG, dmar_unit->gcmd | DMA_GCMD_WBF);
483+
if (iommu_cap_rwbf(dmar_unit->cap) != 0U) {
484+
spinlock_obtain(&(dmar_unit->lock));
485+
iommu_write32(dmar_unit, DMAR_GCMD_REG, dmar_unit->gcmd | DMA_GCMD_WBF);
487486

488-
/* read lower 32 bits to check */
489-
dmar_wait_completion(dmar_unit, DMAR_GSTS_REG, DMA_GSTS_WBFS, true, &status);
490-
spinlock_release(&(dmar_unit->lock));
487+
/* read lower 32 bits to check */
488+
dmar_wait_completion(dmar_unit, DMAR_GSTS_REG, DMA_GSTS_WBFS, true, &status);
489+
spinlock_release(&(dmar_unit->lock));
490+
}
491491
}
492492

493493
/*
@@ -687,19 +687,17 @@ static void fault_status_analysis(uint32_t status)
687687

688688
static void fault_record_analysis(__unused uint64_t low, uint64_t high)
689689
{
690-
if (dma_frcd_up_f(high)) {
691-
return;
692-
}
693-
694-
/* currently skip PASID related parsing */
695-
pr_info("%s, Reason: 0x%x, SID: %x.%x.%x @0x%llx",
696-
(dma_frcd_up_t(high) != 0U) ? "Read/Atomic" : "Write", dma_frcd_up_fr(high),
697-
pci_bus(dma_frcd_up_sid(high)), pci_slot(dma_frcd_up_sid(high)), pci_func(dma_frcd_up_sid(high)), low);
690+
if (!dma_frcd_up_f(high)) {
691+
/* currently skip PASID related parsing */
692+
pr_info("%s, Reason: 0x%x, SID: %x.%x.%x @0x%llx",
693+
(dma_frcd_up_t(high) != 0U) ? "Read/Atomic" : "Write", dma_frcd_up_fr(high),
694+
pci_bus(dma_frcd_up_sid(high)), pci_slot(dma_frcd_up_sid(high)), pci_func(dma_frcd_up_sid(high)), low);
698695
#if DBG_IOMMU
699-
if (iommu_ecap_dt(dmar_unit->ecap) != 0U) {
700-
pr_info("Address Type: 0x%x", dma_frcd_up_at(high));
701-
}
696+
if (iommu_ecap_dt(dmar_unit->ecap) != 0U) {
697+
pr_info("Address Type: 0x%x", dma_frcd_up_at(high));
698+
}
702699
#endif
700+
}
703701
}
704702

705703
static void dmar_fault_handler(uint32_t irq, void *data)
@@ -1011,24 +1009,24 @@ struct iommu_domain *create_iommu_domain(uint16_t vm_id, uint64_t translation_ta
10111009

10121010
if (translation_table == 0UL) {
10131011
pr_err("translation table is NULL");
1014-
return NULL;
1015-
}
1016-
1017-
/*
1018-
* A hypercall is called to create an iommu domain for a valid VM,
1019-
* and hv code limit the VM number to CONFIG_MAX_VM_NUM.
1020-
* So the array iommu_domains will not be accessed out of range.
1021-
*/
1022-
domain = &iommu_domains[vmid_to_domainid(vm_id)];
1012+
domain = NULL;
1013+
} else {
1014+
/*
1015+
* A hypercall is called to create an iommu domain for a valid VM,
1016+
* and hv code limit the VM number to CONFIG_MAX_VM_NUM.
1017+
* So the array iommu_domains will not be accessed out of range.
1018+
*/
1019+
domain = &iommu_domains[vmid_to_domainid(vm_id)];
10231020

1024-
domain->is_host = false;
1025-
domain->vm_id = vm_id;
1026-
domain->trans_table_ptr = translation_table;
1027-
domain->addr_width = addr_width;
1028-
domain->is_tt_ept = true;
1021+
domain->is_host = false;
1022+
domain->vm_id = vm_id;
1023+
domain->trans_table_ptr = translation_table;
1024+
domain->addr_width = addr_width;
1025+
domain->is_tt_ept = true;
10291026

1030-
dev_dbg(ACRN_DBG_IOMMU, "create domain [%d]: vm_id = %hu, ept@0x%x",
1031-
vmid_to_domainid(domain->vm_id), domain->vm_id, domain->trans_table_ptr);
1027+
dev_dbg(ACRN_DBG_IOMMU, "create domain [%d]: vm_id = %hu, ept@0x%x",
1028+
vmid_to_domainid(domain->vm_id), domain->vm_id, domain->trans_table_ptr);
1029+
}
10321030

10331031
return domain;
10341032
}
@@ -1105,15 +1103,16 @@ void resume_iommu(void)
11051103

11061104
int init_iommu(void)
11071105
{
1108-
int ret = 0;
1106+
int ret;
11091107

11101108
ret = register_hrhd_units();
11111109
if (ret != 0) {
11121110
return ret;
1111+
} else {
1112+
do_action_for_iommus(dmar_prepare);
1113+
ret = 0;
11131114
}
11141115

1115-
do_action_for_iommus(dmar_prepare);
1116-
11171116
return ret;
11181117
}
11191118

0 commit comments

Comments
 (0)