git: 89d52f02bf37 - main - Revert "bhyve/riscv: correctly handle empty masks and broadcast in SBI IPI/RFNC"

From: Ruslan Bukin <br_at_FreeBSD.org>
Date: Mon, 02 Nov 2026 21:39:27 UTC
The branch main has been updated by br:

URL: https://cgit.FreeBSD.org/src/commit/?id=89d52f02bf37ed3d9fdb464f38f0714aa979eb41

commit 89d52f02bf37ed3d9fdb464f38f0714aa979eb41
Author:     Ruslan Bukin <br@FreeBSD.org>
AuthorDate: 2026-11-02 21:33:57 +0000
Commit:     Ruslan Bukin <br@FreeBSD.org>
CommitDate: 2026-11-02 21:38:04 +0000

    Revert "bhyve/riscv: correctly handle empty masks and broadcast in SBI IPI/RFNC"
    
    This reverts commit ada1f577cc291336cbb69782075dbbf9fe746872.
---
 sys/riscv/riscv/sbi_ipi.c |  2 --
 sys/riscv/vmm/vmm_riscv.c |  2 --
 sys/riscv/vmm/vmm_sbi.c   | 71 +++++++++++++++++++----------------------------
 3 files changed, 28 insertions(+), 47 deletions(-)

diff --git a/sys/riscv/riscv/sbi_ipi.c b/sys/riscv/riscv/sbi_ipi.c
index af1df948880b..fac5c0c39b99 100644
--- a/sys/riscv/riscv/sbi_ipi.c
+++ b/sys/riscv/riscv/sbi_ipi.c
@@ -74,8 +74,6 @@ sbi_ipi_pic_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
 			mask |= (1ul << pc->pc_hart);
 		}
 	}
-	if (mask == 0)
-		return;
 	sbi_send_ipi(&mask);
 }
 
diff --git a/sys/riscv/vmm/vmm_riscv.c b/sys/riscv/vmm/vmm_riscv.c
index 3798ab8b0ec8..fe2ca5c07789 100644
--- a/sys/riscv/vmm/vmm_riscv.c
+++ b/sys/riscv/vmm/vmm_riscv.c
@@ -548,8 +548,6 @@ riscv_send_ipi(struct hyp *hyp, cpuset_t *cpus)
 		if (!CPU_ISSET(i, cpus))
 			continue;
 		hypctx = hyp->ctx[i];
-		if (hypctx == NULL)
-			continue;
 		atomic_set_32(&hypctx->ipi_pending, 1);
 		vcpu_notify_event(vm_vcpu(vm, i));
 	}
diff --git a/sys/riscv/vmm/vmm_sbi.c b/sys/riscv/vmm/vmm_sbi.c
index 85e336847916..c1e6022097e3 100644
--- a/sys/riscv/vmm/vmm_sbi.c
+++ b/sys/riscv/vmm/vmm_sbi.c
@@ -52,7 +52,7 @@ vmm_sbi_handle_rfnc(struct vcpu *vcpu, struct hypctx *hypctx)
 	struct hyp *hyp;
 	uint16_t maxcpus;
 	cpuset_t cpus;
-	u_int i;
+	int i;
 
 	func_id = hypctx->guest_regs.hyp_a[6];
 	hart_mask = hypctx->guest_regs.hyp_a[0];
@@ -83,29 +83,27 @@ vmm_sbi_handle_rfnc(struct vcpu *vcpu, struct hypctx *hypctx)
 	hyp = hypctx->hyp;
 	active_cpus = vm_active_cpus(hyp->vm);
 	maxcpus = vm_get_maxcpus(hyp->vm);
-
-	if (hart_mask_base == (uint64_t)-1) {
-		CPU_COPY(&active_cpus, &cpus);
-	} else {
-		if (hart_mask == 0)
-			return (SBI_SUCCESS);
-		if (hart_mask_base >= maxcpus)
-			return (SBI_ERR_INVALID_PARAM);
-
-		while (hart_mask != 0) {
-			i = ffsl(hart_mask) - 1;
-			hart_mask &= ~(1UL << i);
-
-			if (hart_mask_base + i >= maxcpus)
-				return (SBI_ERR_INVALID_PARAM);
-			if (!CPU_ISSET(hart_mask_base + i, &active_cpus))
-				return (SBI_ERR_INVALID_PARAM);
-			CPU_SET(hart_mask_base + i, &cpus);
+	for (i = 0; i < maxcpus; i++) {
+		vcpu = vm_vcpu(hyp->vm, i);
+		if (vcpu == NULL)
+			continue;
+		if (hart_mask_base != -1UL) {
+			if (i < hart_mask_base)
+				continue;
+			if (!(hart_mask & (1UL << (i - hart_mask_base))))
+				continue;
 		}
+		/*
+		 * If either hart_mask_base or at least one hartid from
+		 * hart_mask is not valid, then return error.
+		 */
+		if (!CPU_ISSET(i, &active_cpus))
+			return (SBI_ERR_INVALID_PARAM);
+		CPU_SET(i, &cpus);
 	}
 
 	if (CPU_EMPTY(&cpus))
-		return (SBI_SUCCESS);
+		return (SBI_ERR_INVALID_PARAM);
 
 	vmm_fence_add(hyp->vm, &cpus, &fence);
 
@@ -140,9 +138,9 @@ vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx)
 	uint64_t hart_mask;
 	uint64_t hart_mask_base;
 	uint64_t func_id;
-	uint16_t maxcpus;
 	cpuset_t cpus;
-	u_int i;
+	int hart_id;
+	int bit;
 
 	func_id = hypctx->guest_regs.hyp_a[6];
 	hart_mask = hypctx->guest_regs.hyp_a[0];
@@ -153,30 +151,18 @@ vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx)
 	hyp = hypctx->hyp;
 
 	active_cpus = vm_active_cpus(hyp->vm);
-	maxcpus = vm_get_maxcpus(hyp->vm);
 
 	CPU_ZERO(&cpus);
 	switch (func_id) {
 	case SBI_IPI_SEND_IPI:
-		if (hart_mask_base == (uint64_t)-1) {
-			CPU_COPY(&active_cpus, &cpus);
-		} else {
-			if (hart_mask == 0)
-				return (SBI_SUCCESS);
-			if (hart_mask_base >= maxcpus)
+		while ((bit = ffs(hart_mask))) {
+			hart_id = (bit - 1);
+			hart_mask &= ~(1u << hart_id);
+			if (hart_mask_base != -1)
+				hart_id += hart_mask_base;
+			if (!CPU_ISSET(hart_id, &active_cpus))
 				return (SBI_ERR_INVALID_PARAM);
-
-			while (hart_mask != 0) {
-				i = ffsl(hart_mask) - 1;
-				hart_mask &= ~(1UL << i);
-
-				if (hart_mask_base + i >= maxcpus)
-					return (SBI_ERR_INVALID_PARAM);
-				if (!CPU_ISSET(hart_mask_base + i,
-				    &active_cpus))
-					return (SBI_ERR_INVALID_PARAM);
-				CPU_SET(hart_mask_base + i, &cpus);
-			}
+			CPU_SET(hart_id, &cpus);
 		}
 		break;
 	default:
@@ -185,7 +171,7 @@ vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx)
 	}
 
 	if (CPU_EMPTY(&cpus))
-		return (SBI_SUCCESS);
+		return (SBI_ERR_INVALID_PARAM);
 
 	riscv_send_ipi(hyp, &cpus);
 
@@ -228,7 +214,6 @@ vmm_sbi_ecall(struct vcpu *vcpu)
 	}
 
 	hypctx->guest_regs.hyp_a[0] = error;
-	hypctx->guest_regs.hyp_a[1] = 0;
 
 	/* Request is handled in kernel mode. */
 	return (true);