Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.139 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796097255; b=Rl7pgChEOZbLKnEra982NH/EfxSbzLY27HNHV8DX/a+wngoHcMO/BOeAOsPUVefhbU4o 0LXhLFfU8jfW2K72iy0+JMo2ctWaligKAgeG5KACPRGa3C1SDLb1Ov1rXGCjcY9lR+/lo eDy93OoVfOghV5O1Yp3pEb/SrAlp4fNca7CEqxmGlBP4sMOQ1mWb+gFWT2ATQHALfDxQA ZddVwg97AVuTfStdUBVXw5chBPFE+8DlhrAkn13e0A0JG5jKEkc1V3YdoX9XCc+1TZaRW SAV9t6DZocqRAGZLjmh7kEhqEjZrOAn1NqGAjXmh8KraEsVu6MIit6lJlxqQld/BkdQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796097255; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=B2to2gzsgMp+5O+O/vMja/CzuCJh4wTuDlRkSGkeras=; b=YySLkxe35OJKJC81zdQ63X0+Qs4c+1UAAfIPgFXOsHv71Zhj9cETIMO3f/cvv8R/aXhA AIDlpP1py2QPRmvZ12h6gY8tdpHGa/5dnFsZpowOjbMZKX5CnCx9IgL+G6g1w07UCtpHx UrJ9d/YGSBIEuwR2aELeuxo9SUmLtg8GR0I4dVmS16K6LxvI+EtV3M6XTpzt1WbLVffi+ Hp7bGyjEqY7KFqmvfTwsXNKUfZtnPm0lzBJXbUOvxrHtW2km0l8h2mtUw9gpu7WlJDsuf V1K25sMD2u+Gs4nRyAk6IQNramyVq9ZGZSCLAg2xU92u2u0VbO+2qrtiTAR4Cj5GcIA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=c1HFBcFe; arc=none smtp.remote-ip=74.125.227.139 Received: from mail-pj2-f11.google.com (mail-pj2-f11.google.com [74.125.227.139]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12C18606A31A for ; Tue, 1 Dec 2026 03:54:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 12C18606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=c1HFBcFe Received: by mail-pj2-f11.google.com with SMTP id d9443c01a7336-2f138cc0a18so8091415ad.1 for ; Mon, 30 Nov 2026 19:54:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796097254; x=1796702054; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=B2to2gzsgMp+5O+O/vMja/CzuCJh4wTuDlRkSGkeras=; b=c1HFBcFelG/KZi5J5EHWw7OpwdeV9HwQTZ0oFda1OUHAxC/fyJyiqqHQgCnjhfdSzA 8fWMAMe/w4Yd5XdkLDJ2WJK1m/kqKim/fiqBNxdA9x7aCjkHw/SB0zWkyuKyy4XZIELQ kXxZzcEPHX9zad1YwAoDMFSvBx5PM6dllcvIskjUNyWvSY46C1NKy9Iz14YDrtSTowzD FFes7mWiLbNxPY1AfKNDMu9i3GZvpVdIlhGtgX/B1fmTB/9sIqx85K1OJJ/Lz8UraSRr ZXsWsiBh/oSKA1zxfesc4NhWRhTIHN/TrmT7DW+zpIXrSYeGZndpWa/zJ28MVZ7cKV/i GPYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796097254; x=1796702054; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=B2to2gzsgMp+5O+O/vMja/CzuCJh4wTuDlRkSGkeras=; b=fSvfa3WdfexYABGSKYxpP9aF9ZYMIIjMiISC1+LTldh13QCR9vZnMjkHs44UskexfU Q5M13cc/HoS4YH/W3pzT7hoHXGBFcv91P1JdMbTTaeFYUmM8/4dE1t1Sm8SNQBqBYyMu qTsyWvGnm6zPh1zrEwFTLHA5tpUQvMd3JP2BJ/YNBTv+qd1C0vgnFaobAE1cBKwqztgg 3cWZqmUDNdn2L79mxYxJIh9WHcR2z6o7NgdiuM8H3EdAq8mWq5ymcIgfMjw9dJz3fmke MUpyybvt1rNkdJ6XoreO6uV5kYIKaanN6COOxQnM7fzXuvsHQYpMwhiG/HUSdPtCFD9p 5ozQ== X-Forwarded-Encrypted: i=1; AL+hErHcnxhEQUIg+oy9vtH5eqWq1N4aJsL+aJKeqa6P9wMt+ruh3yuBTiW9w4rGYkw4dstXz14=@vger.kernel.org X-Gm-Message-State: APuU4Z+pcN7R4X4cWkAhs75KnUORQiwpphrTz/i+/ooi+Z1D997IHvKv ae84aqBE2Hn6WHytALXCtypGxxbMzWKRsbmj8ehl50PIOLq//ED/K+Vd X-Gm-Gg: AZFdTZcbEbzA3VhS3cWwXUpuBW1GaoFX8i9eY7gOZ2sfxyxJFSsxmoQQuU50k0zpZct CWdaSPsXXn2KLEhrD4GzitURxnqeGOo+pnwbB9I5MXBi37BEF96azKx7eAvl2ZXVd3TIiLf/ct/ j5ghsXtPaUlxKovTLD+seuLy0hAbNlH7bVxQ8rrvnlVM6EZJsJ/2GnO3aW6tYZXoq5Uj+o644yw 3O2W5QLTepQGGR8erZDZspjF9cuyRrDCSxdL7zMkAFb2pBhOqN77lRRKY8D2+9awmuHXnA0ybpH CluA5yCvtbqlFwpkwiS6S8tmuqjRako0kva71vjI1jSYDE94pN7VmVpmTfUXeBksXi+ivtUrjQy 5D6ao46hqrD3agi4BCe0BIKglF7iq7F/0MYOtEfO6WmTVZ8jIYjUb/Y/0fFD1/FbqWyDttMzmWw 6/tz7fqpzuvLQY1bfYyDc540oGcD8/ymlOvQhOs43+3B4YmR+kd+Qja+57d7rlb96y8HqDJ1Dav +w4fhng7mERNI7nuS/hAmewrOCAmVRN7H2Z6ICFjWaCJ8bfKzqLsJC4roOhPPK9qvsgDSW7e/KW 9yaWP9ECHQfcvIB0tN92/zkP7sOW3B0RktrCmq+EwsZg2x9tO/aircxqRiHxpXV22clWWK0QRqx YoOOZkZU= X-Received: by 2002:a17:902:ea01:b0:2ef:da23:7aa1 with SMTP id d9443c01a7336-2f03f1c02eamr353564565ad.40.1796097254294; Mon, 30 Nov 2026 19:54:14 -0800 (PST) Received: from zihui.lxd ([137.132.218.138]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f03f1207f1sm175203935ad.64.2026.11.30.19.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 19:54:13 -0800 (PST) From: Zihui Guo To: Anup Patel Cc: Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Jones , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org Subject: [PATCH] RISC-V: KVM: Do not emulate CSR accesses from VU-mode as VS-mode Date: Tue, 1 Dec 2026 03:54:08 +0000 Message-ID: <20261201035408.3734383-1-youzih27@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A virtual-instruction trap taken from VU-mode is emulated as if it had come from VS-mode. kvm_riscv_vcpu_exit() only checks hstatus.SPV, and the CSR emulation never looks at hstatus.SPVP. Two groups of emulated CSRs are reachable from a guest process this way: - sireg and stopei. "csrrw x0, stopei, x0" in guest user mode claims and drops the top pending guest external interrupt, and sireg reads or writes whichever IMSIC or iprio register the guest kernel last selected in siselect. - cycle, instret and hpmcounter*. hcounteren only has TM set, so these always trap, and a guest process gets a counter value even when the guest kernel has cleared the bit in scounteren. Both accesses are illegal in U-mode, and the privileged spec says that a hypervisor not emulating the trapping instruction should convert the virtual-instruction trap into an illegal-instruction exception for the guest. Do that for accesses from VU-mode to CSRs that are not user-level, and to counters that the guest's scounteren does not enable. Fixes: a9ac6c37521f ("RISC-V: KVM: Implement trap & emulate for hpmcounters") Fixes: 2f4d58f7635a ("RISC-V: KVM: Virtualize per-HART AIA CSRs") Signed-off-by: Zihui Guo --- Tested in a nested setup: QEMU 11.1.91 (virt, aia=aplic-imsic, cpu max) as the machine, this tree as the L1 host with KVM, kvmtool, and the same kernel as the guest. A static program in the guest, run as a normal process with kernel.perf_user_access=0: before after csrr cycle (cycles event open) counter value SIGILL csrr instret (instructions event) counter value SIGILL csrr sireg 0x0, no trap SIGILL csrr stopei 0x0, no trap SIGILL Without an open perf event, cycle and instret already gave SIGILL before the patch because KVM had no counter to read. stopei was only read, so no interrupt was claimed by the test. Not tested on hardware; the test program is available on request. WFI and WRS from VU-mode have the same problem. "RISC-V: KVM: Redirect nested WFI and WRS traps" in the nested series already handles them, so they are left out here. Assisted-by: Claude:claude-opus-5-5 arch/riscv/kvm/vcpu_insn.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c index af752275c210..12c450143858 100644 --- a/arch/riscv/kvm/vcpu_insn.c +++ b/arch/riscv/kvm/vcpu_insn.c @@ -145,6 +145,24 @@ int kvm_riscv_vcpu_csr_return(struct kvm_vcpu *vcpu, struct kvm_run *run) return 0; } +/* + * A virtual-instruction trap from VU-mode must not be emulated with the + * privilege of VS-mode. VU-mode can only access user-level CSRs, and the + * counters only when the guest has enabled them in scounteren. + */ +static bool csr_allowed_from_vu(struct kvm_vcpu *vcpu, unsigned int csr_num) +{ + /* Bits [9:8] of the CSR number hold the lowest privilege level */ + if (csr_num & 0x300) + return false; + + if ((csr_num >= CSR_CYCLE && csr_num <= CSR_HPMCOUNTER31) || + (csr_num >= CSR_CYCLEH && csr_num <= CSR_HPMCOUNTER31H)) + return vcpu->arch.guest_csr.scounteren & BIT(csr_num & 0x1f); + + return true; +} + static int csr_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn) { int i, rc = KVM_INSN_ILLEGAL_TRAP; @@ -184,6 +202,11 @@ static int csr_insn(struct kvm_vcpu *vcpu, struct kvm_run *run, ulong insn) return rc; } + /* Accesses from VU-mode get the privilege of VU-mode */ + if (!(vcpu->arch.guest_context.hstatus & HSTATUS_SPVP) && + !csr_allowed_from_vu(vcpu, csr_num)) + return KVM_INSN_ILLEGAL_TRAP; + /* Save instruction decode info */ vcpu->arch.csr_decode.insn = insn; vcpu->arch.csr_decode.return_handled = 0; base-commit: bd68956997dddcc3b1a7dd2fadadf584529027a1 -- 2.43.0