Skip to content

Commit 0611f78

Browse files
jones-drewavpatel
authored andcommitted
riscv: KVM: Fix SBI IPI error generation
When an invalid function ID of an SBI extension is used we should return not-supported, not invalid-param. Also, when we see that at least one hartid constructed from the base and mask parameters is invalid, then we should return invalid-param. Finally, rather than relying on overflowing a left shift to result in zero and then using that zero in a condition which [correctly] skips sending an IPI (but loops unnecessarily), explicitly check for overflow and exit the loop immediately. Fixes: 5f862df ("RISC-V: KVM: Add v0.1 replacement SBI extensions defined in v0.2") Signed-off-by: Andrew Jones <ajones@ventanamicro.com> Reviewed-by: Anup Patel <anup@brainfault.org> Link: https://lore.kernel.org/r/20250217084506.18763-10-ajones@ventanamicro.com Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent e3219b0 commit 0611f78

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

arch/riscv/kvm/vcpu_sbi_replace.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ static int kvm_sbi_ext_ipi_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
5151
struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
5252
unsigned long hmask = cp->a0;
5353
unsigned long hbase = cp->a1;
54+
unsigned long hart_bit = 0, sentmask = 0;
5455

5556
if (cp->a6 != SBI_EXT_IPI_SEND_IPI) {
56-
retdata->err_val = SBI_ERR_INVALID_PARAM;
57+
retdata->err_val = SBI_ERR_NOT_SUPPORTED;
5758
return 0;
5859
}
5960

@@ -62,15 +63,23 @@ static int kvm_sbi_ext_ipi_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
6263
if (hbase != -1UL) {
6364
if (tmp->vcpu_id < hbase)
6465
continue;
65-
if (!(hmask & (1UL << (tmp->vcpu_id - hbase))))
66+
hart_bit = tmp->vcpu_id - hbase;
67+
if (hart_bit >= __riscv_xlen)
68+
goto done;
69+
if (!(hmask & (1UL << hart_bit)))
6670
continue;
6771
}
6872
ret = kvm_riscv_vcpu_set_interrupt(tmp, IRQ_VS_SOFT);
6973
if (ret < 0)
7074
break;
75+
sentmask |= 1UL << hart_bit;
7176
kvm_riscv_vcpu_pmu_incr_fw(tmp, SBI_PMU_FW_IPI_RCVD);
7277
}
7378

79+
done:
80+
if (hbase != -1UL && (hmask ^ sentmask))
81+
retdata->err_val = SBI_ERR_INVALID_PARAM;
82+
7483
return ret;
7584
}
7685

0 commit comments

Comments
 (0)