From nobody Mon Jun 8 07:22:03 2026 Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) (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 96C6943DA2E for ; Wed, 3 Jun 2026 09:33:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780479202; cv=none; b=cfVrAZwJGFWM89evflM6paxSyA4nFzf4kBh/D4lHqhFJzE6IemWsEeFfHpoF/WQjGIOxSuJrV1y5o+ssaKvBNRdEkVz+jXUCdlWXQP6fDLQphJBfElpw53xRvrHj1uH8Ol4a+vXYrlEY3vOMAhL1IKwgm/EX5p0Sn4+3KNHdb2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780479202; c=relaxed/simple; bh=vgRDbhY5lWWXLNOZFDjQHQ1Z3EPIMgzx6hICZSBrBNs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=n6gtW0raB+gsxp8gWISeCuNHHbf30ztRd6W7XmqMf8VJ383YBHZFp0Ud9est0dmpUtRIpm24Wb0bXeObsKB6dkQfftilNJ25to5FQNqxABPZ7EmGHlWpC9WXz/Za2dU/VzaWKXP2daZTtid93L8i3P0uCGdG7VS1o/8G740RJWI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=T+850De/; arc=none smtp.client-ip=115.124.30.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="T+850De/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1780479195; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=nXWOtMPLwLRV+GvbbdhZX/wwWE7Pl1XYnW0R8qtnR1U=; b=T+850De/gY36tnz+ADHIRK/Phe4shOr5AuqX+tfhkmjX0sw6yJN4HiiZjTcPT0/022+6m9I49jE70eLaOIeR4ACCLIZe9Wi28+vX6XJgS9s/INllVcS80AYUbMNkz170RkIruLburbx08V0imrW6//5VCRfxttqygOLxAsIfzcM= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037033178;MF=cp0613@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0X46xjpy_1780479189; Received: from DESKTOP-S9E58SO.localdomain(mailfrom:cp0613@linux.alibaba.com fp:SMTPD_---0X46xjpy_1780479189 cluster:ay36) by smtp.aliyun-inc.com; Wed, 03 Jun 2026 17:33:14 +0800 From: Chen Pei To: pjw@kernel.org, alexghiti@rivosinc.com, cleger@rivosinc.com, conor.dooley@microchip.com, anup@brainfault.org, guoren@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Chen Pei Subject: [PATCH] riscv: cpufeature: Validate H extension via CSR_HGATP probe Date: Wed, 3 Jun 2026 17:33:01 +0800 Message-ID: <20260603093301.89430-1-cp0613@linux.alibaba.com> X-Mailer: git-send-email 2.43.0 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 Content-Type: text/plain; charset="utf-8" The H extension is advertised to S-mode through three channels -- the DT "riscv,isa" string, the DT "riscv,isa-extensions" list, and the ACPI RHCT ISA string -- all of which converge in riscv_resolve_isa(). The H entry has no validate callback today, so the kernel trusts the description unconditionally. When firmware advertises H but the hardware does not actually implement it (common during early bring-up, e.g. QEMU), kvm.ko reaches kvm_riscv_gstage_mode_detect() and oopses on the first CSR_HGATP access with an illegal instruction. Add a validate callback for H that probes CSR_HGATP under an exception-table fixup. If the read traps, the H bit is cleared during ISA resolution and the rest of the kernel sees H as unavailable. Because the probe runs in riscv_resolve_isa(), it covers all three advertisement sources. Signed-off-by: Chen Pei --- arch/riscv/kernel/cpufeature.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index f46aa5602d74..b34dd789e94e 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -17,9 +17,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -163,6 +165,32 @@ static int riscv_ext_d_validate(const struct riscv_isa= _ext_data *data, return 0; } =20 +static int riscv_ext_h_validate(const struct riscv_isa_ext_data *data, + const unsigned long *isa_bitmap) +{ + unsigned long val, ret =3D 1; + + /* + * The H extension may be advertised by firmware (DT/ACPI) even + * when the underlying hardware does not implement it, or when + * M-mode firmware has not delegated hypervisor CSR access to + * S-mode. Probe CSR_HGATP under an exception-table fixup: if + * the read traps with an illegal instruction, ret stays 1. + */ + asm volatile( + "1: csrr %0, " __stringify(CSR_HGATP) "\n" + " li %1, 0\n" + "2:\n" + _ASM_EXTABLE(1b, 2b) + : "=3Dr" (val), "+r" (ret)); + + if (ret) { + pr_err("H detected in ISA string, disabling as CSR_HGATP is not accessib= le\n"); + return -EINVAL; + } + return 0; +} + static int riscv_ext_vector_x_validate(const struct riscv_isa_ext_data *da= ta, const unsigned long *isa_bitmap) { @@ -498,7 +526,7 @@ const struct riscv_isa_ext_data riscv_isa_ext[] =3D { __RISCV_ISA_EXT_DATA(q, RISCV_ISA_EXT_q), __RISCV_ISA_EXT_SUPERSET(c, RISCV_ISA_EXT_c, riscv_c_exts), __RISCV_ISA_EXT_SUPERSET_VALIDATE(v, RISCV_ISA_EXT_v, riscv_v_exts, riscv= _ext_vector_float_validate), - __RISCV_ISA_EXT_DATA(h, RISCV_ISA_EXT_h), + __RISCV_ISA_EXT_DATA_VALIDATE(h, RISCV_ISA_EXT_h, riscv_ext_h_validate), __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicbom, RISCV_ISA_EXT_ZICBOM, riscv_xli= nuxenvcfg_exts, riscv_ext_zicbom_validate), __RISCV_ISA_EXT_DATA_VALIDATE(zicbop, RISCV_ISA_EXT_ZICBOP, riscv_ext_zic= bop_validate), __RISCV_ISA_EXT_SUPERSET_VALIDATE(zicboz, RISCV_ISA_EXT_ZICBOZ, riscv_xli= nuxenvcfg_exts, riscv_ext_zicboz_validate), --=20 2.50.1