From nobody Fri Jul 24 23:35:55 2026 Received: from smtpbgsg1.qq.com (smtpbgsg1.qq.com [54.254.200.92]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20BC138238F; Wed, 22 Jul 2026 06:28:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=54.254.200.92 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784701706; cv=none; b=s6/6qjFxxGFh0PnM7rfbnQC1NJHzsiDg8oqA/ELa7QFlyoOiP9K5nSCvLS7o9edxJELty+u85l3dnaYWu+J8CequWP56L29HjAobZ8ju2AHyqP1TwZ7DapmgzBZ2puIY9gfpv7z/5MY7IMme74X1xwHt56x1CroEkSvG81JvyEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784701706; c=relaxed/simple; bh=K7wCGZLHd3z6e9GFAEUNb9Va+fDlOdsKY9UIqye4oTk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vA7jiiYGB7Nyvwc9Y3qWK7LANFsQZ3EclRrQDOX5Fwi/+qb4D4bnQ4cMSagUqUrfw3fHclojtsV6HGOQ+7qPpmK9eKZPT9j2KYFeqF+Amydpss1ygdZ2spF3Gu0ULGr5TsIaU4V8NMVA5DgK+hvN21ikg8a+vG9f/TT+0v75aXY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=haiwei.tech; spf=pass smtp.mailfrom=haiwei.tech; arc=none smtp.client-ip=54.254.200.92 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=haiwei.tech Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=haiwei.tech X-QQ-mid: esmtpgz12t1784701691t24bb549c X-QQ-Originating-IP: 9oYuK7QuFV4ueBJwUK0W0oO7gIQzndCeg/X3NL+l4rc= Received: from rsl ( [183.242.33.186]) by bizesmtp.qq.com (ESMTP) with id ; Wed, 22 Jul 2026 14:28:08 +0800 (CST) X-QQ-SSF: 0000000000000000000000000000000 X-QQ-GoodBg: 0 X-BIZMAIL-ID: 14402031847002852684 EX-QQ-RecipientCnt: 14 From: JinRui To: Anup Patel , Paolo Bonzini , Shuah Khan , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Atish Patra , Alexandre Ghiti , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, jinrui Subject: [PATCH v6] KVM: selftests: riscv: Add lazy V extension enablement for guests Date: Wed, 22 Jul 2026 06:28:04 +0000 Message-ID: X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260721122442.351865-1-jinrui@haiwei.tech> References: <20260721122442.351865-1-jinrui@haiwei.tech> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: esmtpgz:haiwei.tech:qybglogicsvrgz:qybglogicsvrgz6b-0 X-QQ-XMAILINFO: M83iEva+1QDBSbzosKlIiFlGB1rkcB2ZXI1IHQtxPuFhxbfG2fyGbEo9 8dof3RhtjK/T4od47pF5ODzqFQVzTe+PHPNRetIZUprWLeHyBbHN8xxSEEJk0VF+SebTLWi PdLkMXnSijUbEDByjEkTuePXV1SodCIVAzvtZLNJjENLHGTs+NVTBYMExVVPYKUCMhNffj4 CbD+7UHTICTQLEgnUwBewdh0hUh7mguaeRDfDeqjx6d8TX/WjgYEl0x8GM+x66B+p+h4IEr hQhDpNC3lRXCDHkRvjZff31e8ge9i9ugjZzqoSaE286IwglA9uJ6BFPzTPlth57F7dVFXSn 3hE63CmxvRL5wQRCdrghyVTTl94j06cyYpo9tLDwPzzoPgcPk26rgujSOIET+V98+jpL85I IbFB3HuFUGMlJx2yROSDXUfBHyyx+Gq1kwB/AK8zv5vs3rwg3IsckU8OvXEQMHB3qSQMOzy k0XBxDKkcJOTmajU66GB1wGC33eN06knMAlx5f1dwRJZ6M4ey/VeWwXa/iOx6iyo0PDgamt QKy8IHp0RxNWLPNURSV4U4A+ZxuzXDFWohpqvapeCHgifDr8cnROZwTwwP2OYAU7+Hrvkjh K0I6MbtmN3HtvSGkK5E2b2FqyQ5aSN5AprxSrfmWLmTbGoFOWoQHoissWncuom7DBdzHNsu 69Z+TqCjgjyur1PZbyHsOsC/NaT2I0n2DnSyRu96cGhyaXrDjcnuJpuEPCVz0RG3ol6yIOw Azd2CTwMagzDETu5m25Bqa5ePriX7eVfbjXHsVCUuEMIv9HvCo48oRP2qDETV4GuOhP5lJc sckfEBdM+GnmBsUhTSk19doWKJSGLQa5s5Dw7Y85hzUZfgSn5q6w4f2xOaxHs+5PfkszX+T Oy/EV/+87cM9VdIWRDPHjoU8RMYkl/RJuhTqK2D6cEiNTEbJsQlhd4aaIivEz+mWnX7CUTW WASY0k80PBh4yLP+vot1ZPbwVZ3uAQJdbGwRamH4UmQ4cOfTGJh3ds0XwJNUPkr57mhPZ4x qTxUG2BZRiQsP+IzYL2icTrdEnNKs= X-QQ-XMRINFO: NyFYKkN4Ny6FuXrnB5Ye7Aabb3ujjtK+gg== X-QQ-RECHKSPAM: 0 Content-Type: text/plain; charset="utf-8" From: jinrui When the cross-compiler defaults to an -march that includes the V (vector) extension, -O2 auto-vectorization generates vector instructions (e.g. vsetvli, vadd.vv) in Guest binary code. If the Guest executes a vector instruction while sstatus.VS is Off, an EXC_INST_ILLEGAL (scause=3D2) is raised. KVM's hedeleg delegates this exception to the Guest, but the selftest has no way to handle it as a bare-metal program, causing all Guest tests to fail. In contrast, a real OS kernel handles this via riscv_v_first_use_handler(), which detects the vector instruction, sets sstatus.VS to Initial, and srets to re-execute. Fix this with four changes in processor.c: 1. Delete the now-unused guest_unexp_trap() handler, which is replaced by the full exception vector table. 2. In vm_arch_vcpu_add(), notify KVM that the Guest is allowed to use the V extension via __vcpu_set_reg(V, 1) (best-effort, silently ignores errors on hardware without V). Also replace the raw stvec handler with the full exception vector table, which provides save_context/restore_context for safe lazy enablement. 3. In route_exception(), add a lazy V enablement check that runs before any test-registered handler. When the cause is a non-IRQ EXC_INST_ILLEGAL and sstatus.VS is Off, set VS to Initial and return so the faulting instruction is re-executed via sret. Track the epc per-vCPU via a flexible array member to avoid infinite loops on hardware without V and eliminate races between concurrent vCPUs. 4. Make vm_init_vector_tables() idempotent by checking vm->handlers before allocation, so tests that manually call it (ebreak_test, arch_timer, sbi_pmu_test) do not leak memory. The check runs before test-registered EXC_INST_ILLEGAL handlers (e.g. sbi_pmu_test) to ensure V enablement takes priority. Tested on a riscv64 host with KVM enabled: all 20 KVM selftest binaries pass (arch_timer, ebreak_test, steal_time, get-reg-list, sbi_pmu_test, etc.). Signed-off-by: jinrui --- .../selftests/kvm/lib/riscv/processor.c | 108 +++++++++++++++--- 1 file changed, 93 insertions(+), 15 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/test= ing/selftests/kvm/lib/riscv/processor.c index ded5429f3448..b7449dffe660 100644 --- a/tools/testing/selftests/kvm/lib/riscv/processor.c +++ b/tools/testing/selftests/kvm/lib/riscv/processor.c @@ -17,6 +17,13 @@ =20 static gva_t exception_handlers; =20 +struct handlers { + exception_handler_fn exception_handlers[NR_VECTORS][NR_EXCEPTIONS]; + bool v_available; + int v_epc_capacity; + unsigned long v_epc[]; +}; + bool __vcpu_has_ext(struct kvm_vcpu *vcpu, u64 ext) { unsigned long value =3D 0; @@ -297,14 +304,6 @@ void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcp= u, u8 indent) " T3: 0x%016lx T4: 0x%016lx T5: 0x%016lx T6: 0x%016lx\n", core.regs.t3, core.regs.t4, core.regs.t5, core.regs.t6); } - -static void __aligned(16) guest_unexp_trap(void) -{ - sbi_ecall(KVM_RISCV_SELFTESTS_SBI_EXT, - KVM_RISCV_SELFTESTS_SBI_UNEXP, - 0, 0, 0, 0, 0, 0); -} - void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code) { vcpu_set_reg(vcpu, RISCV_CORE_REG(regs.pc), (unsigned long)guest_code); @@ -348,8 +347,33 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, u= 32 vcpu_id) /* Setup sscratch for guest_get_vcpuid() */ vcpu_set_reg(vcpu, RISCV_GENERAL_CSR_REG(sscratch), vcpu_id); =20 - /* Setup default exception vector of guest */ - vcpu_set_reg(vcpu, RISCV_GENERAL_CSR_REG(stvec), (unsigned long)guest_une= xp_trap); + /* + * Enable the V (vector) extension in KVM so that the compiler can + * safely generate vector instructions (e.g. via -O2 auto- + * vectorization). Silently ignore errors; the test will still work + * without V. + */ + __vcpu_set_reg(vcpu, RISCV_ISA_EXT_REG(KVM_RISCV_ISA_EXT_V), 1); + + /* + * Use the full exception vector table (which provides lazy V + * extension enablement for EXC_INST_ILLEGAL in route_exception) + * as the default exception handler. vm_init_vector_tables() is + * idempotent; tests that call it again will get a no-op. + */ + vm_init_vector_tables(vm); + vcpu_init_vector_tables(vcpu); + + /* + * Record V extension availability in the handlers struct so that + * route_exception() (called from Guest context) can check it + * without relying on a host-side global variable. + */ + { + struct handlers *h =3D addr_gva2hva(vm, vm->handlers); + + h->v_available =3D __vcpu_has_isa_ext(vcpu, KVM_RISCV_ISA_EXT_V); + } =20 return vcpu; } @@ -408,15 +432,12 @@ void assert_on_unhandled_exception(struct kvm_vcpu *v= cpu) struct ucall uc; =20 if (get_ucall(vcpu, &uc) =3D=3D UCALL_UNHANDLED) { + vcpu_dump(stderr, vcpu, 2); TEST_FAIL("Unexpected exception (vector:0x%lx, ec:0x%lx)", uc.args[0], uc.args[1]); } } =20 -struct handlers { - exception_handler_fn exception_handlers[NR_VECTORS][NR_EXCEPTIONS]; -}; - void route_exception(struct pt_regs *regs) { struct handlers *handlers =3D (struct handlers *)exception_handlers; @@ -432,6 +453,46 @@ void route_exception(struct pt_regs *regs) ec =3D 0; } =20 + /* + * Handle V (vector) extension lazy enablement before any + * registered handler. The compiler's default march may include + * V, and auto-vectorization generates vector instructions that + * trigger EXC_INST_ILLEGAL when VS (Vector Status) in sstatus + * is Off. Enable VS to Initial and re-execute the faulting + * instruction, mimicking what a real OS kernel does. + * + * This check runs before any test-registered handler, so tests + * that install their own EXC_INST_ILLEGAL handler (e.g. + * sbi_pmu_test) are not affected. + */ + if (!(regs->cause & CAUSE_IRQ_FLAG) && ec =3D=3D EXC_INST_ILLEGAL) { + /* + * If KVM supports the V extension for this Guest and VS + * (Vector Status) is Off in the saved sstatus, set it to + * Initial and re-execute the faulting instruction. + * + * Use regs->status (saved at exception entry) rather than + * reading the live CSR to avoid a TOCTOU race. + * + * Track the epc per-vCPU to avoid an infinite loop when + * V is disabled or the hardware rejects the VS change. + * Using a per-vCPU array avoids races between concurrent + * vCPUs that would occur with a single shared (epc, vcpu) + * pair. + */ + if (handlers && handlers->v_available && !(regs->status & SR_VS)) { + int vcpu_id; + + asm volatile("csrr %0, sscratch" : "=3Dr" (vcpu_id)); + if (vcpu_id < handlers->v_epc_capacity && + handlers->v_epc[vcpu_id] !=3D regs->epc) { + handlers->v_epc[vcpu_id] =3D regs->epc; + regs->status |=3D SR_VS_INITIAL; + return; + } + } + } + if (handlers && handlers->exception_handlers[vector][ec]) return handlers->exception_handlers[vector][ec](regs); =20 @@ -448,9 +509,26 @@ void vcpu_init_vector_tables(struct kvm_vcpu *vcpu) =20 void vm_init_vector_tables(struct kvm_vm *vm) { - vm->handlers =3D __vm_alloc(vm, sizeof(struct handlers), vm->page_size, + int max_vcpus; + size_t size; + + if (vm->handlers) + return; + + max_vcpus =3D kvm_check_cap(KVM_CAP_MAX_VCPUS); + if (max_vcpus < 1) + max_vcpus =3D 512; + + size =3D sizeof(struct handlers) + max_vcpus * sizeof(unsigned long); + vm->handlers =3D __vm_alloc(vm, size, vm->page_size, MEM_REGION_DATA); =20 + { + struct handlers *h =3D addr_gva2hva(vm, vm->handlers); + + h->v_epc_capacity =3D max_vcpus; + } + *(gva_t *)addr_gva2hva(vm, (gva_t)(&exception_handlers)) =3D vm->handlers; } =20 --=20 2.43.0