From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-il1-f176.google.com (mail-il1-f176.google.com [209.85.166.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E197218EBA for ; Wed, 16 Jul 2025 02:06:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752631605; cv=none; b=f+Bauva1nuHPjSWM1QimuHVhOGSqscsZZJwAD9ttDPm6Y4VS4URP4aGWJK7H9yUrMhOeSfVERMjEVTdbRG7igXMZPJAkoSbfefxWXzBaV3Wc1ZIAuyRAmEYOW6bGNCEb/Myi1e00ryEsVPAAvh3V51l4BQTMDf0gs7U3wc6/1IU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752631605; c=relaxed/simple; bh=TPISs9NBFDN53Y3aUroY0WGCUZDIWplP0R+8uYAH4bo=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=LX+CSyNFBo0DJDpkySmA1cafFrcgFKQiDPS6QcRemndJ/nC0FvRHeBN+7KcFpgfdYaMIY1oPC9MCipt6dn24f/K5DTwwWRe3efZcYiOBrr3KbeZFZY4gDwoPwvgKTdxAb0/fZCWjadldXyTyt1cH2s+LklWRu+A6EbMoxq8v/kQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=DSjdlGhS; arc=none smtp.client-ip=209.85.166.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="DSjdlGhS" Received: by mail-il1-f176.google.com with SMTP id e9e14a558f8ab-3df2ddd39c6so23152385ab.0 for ; Tue, 15 Jul 2025 19:06:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1752631601; x=1753236401; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5j+F9L1888D1/lTB0yjly7TVvzjMYjvETcZUaJXOGrM=; b=DSjdlGhSODTU8fT6Aw9TLtMH3N/iEdSvZKxeVopfidb1K6wf7w5EDQ9Ap3T3hXmmm3 dbqTd+uHD6nWju+4hClirXyxT7tqwI1ClajTD27h2T+M20IeDnCBeUhVFFckars8M4Te 0tyqlNrmqeFfY//U9hCdO1XKgFiZurvto1vcWpZNHJFyoOwzcuGTn3aA3Pg4q+ioYjea QaGfk09z9+T3438UoawbjtztZvyh8QXV+peewcAhO5zZyUQqPmyFm2ar2wVep5OrtEGB GIMv1tx/LjrNBhHkjj1FMJFbD9C7zVsllD0sUJNs4zVrAS8X21N0MXCyaXGODKcnluG2 sv0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752631601; x=1753236401; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5j+F9L1888D1/lTB0yjly7TVvzjMYjvETcZUaJXOGrM=; b=TfPXi6iIXKBll/TceS4WPZ5j8lwUXddAe4D/bIdeRzxhY4SmcIgp7sNyiLyxYtw3kS b/qcLzlGMqKruNC0ePwN++PyUqnr58/k0GkhO075AdNd7bIPT0d3l2Tnplr1X51TQvP0 UWCYqGNYUSOwUKdrfQnuQnp6z30HMV4wURBGC3sAjysZQ4PW+ge+L3MMHXZ0ucuo7S3i N2DoJB9ogJYwiP+BRNyR6QCimEZeYmU3K4iRs0GDh3m4AQkcPaK5w1YrYGyXqcHg+wSn UYXXrH+qDCHG3aGlMuTlAZBiiTJF/JGMnrwIfr+c0H5X9QnYklfd6W3l8iTr44Cj5BTV +1zQ== X-Forwarded-Encrypted: i=1; AJvYcCUTcvZpbeSgRBrsT6QwcxrBOrc6oyztphqabQHk3NtSy1Bdc+2luT1czASILKs+TUUU6Hy/0StqTmCQHBOE0A==@vger.kernel.org X-Gm-Message-State: AOJu0YzMPjKWKLa5V8fkwQ+k2ma8/3Dw6tBz8L4cZaEPTzfk218we3LE CoJMaWDyAVHRhKwZ2ma798bXPYW7OwA/k6A+uqWUG1ZdMrcSwQwWGs2yLnf1fbR+z4ULPR9uD3J d5mQVp/3SKa1zhC6AVDIbdzPytxOfJmjoV5z3UMh7dw== X-Gm-Gg: ASbGncu8FoNCGnOm95RIf9MTHyzpD6E7X647Ezb3nNFayqSoSV2uilHwrTbA+4mdIIC 3dWME/pmjexxfnV3kt8ROSx8KqNiBRjy1JjUT6j7M7m5nGHffghyRktLDjvO4IMdQYdE/US7nAB o0BIWAiVRZPblyG7pxkagrTCcuYrPSgtDFPS3HHR8l+zLvcEe8dkMzw72tR10K3cZS+QF3AM6hV RTPZgyitayDmnAqAWX2 X-Google-Smtp-Source: AGHT+IH9/HXYsLu/DJVcgXHmyW23optDc6uoRoN2rcWn2HHkb7B7VzOcj2uDSKGa9qQvekhhenVwxhU+VuPhiZoGO+E= X-Received: by 2002:a05:6e02:1aa3:b0:3df:3886:ad6e with SMTP id e9e14a558f8ab-3e282e65b61mr10740485ab.12.1752631601151; Tue, 15 Jul 2025 19:06:41 -0700 (PDT) Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20250604-v5_user_cfi_series-v17-0-4565c2cf869f@rivosinc.com> <20250604-v5_user_cfi_series-v17-15-4565c2cf869f@rivosinc.com> In-Reply-To: From: Zong Li Date: Wed, 16 Jul 2025 10:06:28 +0800 X-Gm-Features: Ac12FXxQabhbT3gOiwtrnSWdGCnuEDBYfeHbjfUOFZRTxM5oaqlSklQMtx6WBCk Message-ID: Subject: Re: [PATCH v17 15/27] riscv/traps: Introduce software check exception and uprobe handling To: Deepak Gupta Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, rust-for-linux@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jul 16, 2025 at 5:34=E2=80=AFAM Deepak Gupta w= rote: > > Hi Zong, > > > On Thu, Jun 19, 2025 at 7:16=E2=80=AFPM Zong Li wrot= e: > > > > On Mon, Jun 16, 2025 at 3:31=E2=80=AFPM Zong Li wr= ote: > > > > > > On Thu, Jun 5, 2025 at 1:17=E2=80=AFAM Deepak Gupta wrote: > > > > > > > > zicfiss / zicfilp introduces a new exception to priv isa `software = check > > > > exception` with cause code =3D 18. This patch implements software c= heck > > > > exception. > > > > > ..... > > > > When a user mode CFI violation occurs, the ELP state should be 1, and > > > the system traps into supervisor mode. During this trap, sstatus.SPEL= P > > > is set to 1, and the ELP state is reset to 0. If we don=E2=80=99t cle= ar > > > sstatus.SPELP, the ELP state will become 1 again after executing the > > > sret instruction. As a result, the system might trigger another > > > forward CFI violation upon executing the next instruction in the user > > > program, unless it happens to be a lpad instruction. > > > > > > The previous patch was tested on QEMU, but QEMU does not set the > > > sstatus.SPELP bit to 1 when a forward CFI violation occurs. Therefore= , > > > I suspect that QEMU might also require some fixes. > > > > Hi Deepak, > > The issue with QEMU was that the sw-check exception bit in medeleg > > couldn't be set. This has been fixed in the latest QEMU mainline. I > > have re-tested the latest QEMU version, and it works. > > What was this issue, can you point me to the patch in mainline? Hi Deepak The issue was that my QEMU setup somehow missed the change of `target/riscv/csr.c` in your following patch: https://github.com/qemu/qemu/commit/6031102401ae8a69a87b20fbec2aae666625d96= a After I upgraded to the latest QEMU source, I found the kernel issue if we didn't clear sstatus.SPELP in the handler Thanks > > > > > > > > > Thanks > > > > > > > + > > > > + if (is_fcfi || is_bcfi) { > > > > + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, > > > > + "Oops - control flow violation"); > > > > + return true; > > > > + } > > > > + > > > > + return false; > > > > +} > > > > + > > > > +/* > > > > + * software check exception is defined with risc-v cfi spec. Softw= are check > > > > + * exception is raised when:- > > > > + * a) An indirect branch doesn't land on 4 byte aligned PC or `lpa= d` > > > > + * instruction or `label` value programmed in `lpad` instr does= n't > > > > + * match with value setup in `x7`. reported code in `xtval` is = 2. > > > > + * b) `sspopchk` instruction finds a mismatch between top of shado= w stack (ssp) > > > > + * and x1/x5. reported code in `xtval` is 3. > > > > + */ > > > > +asmlinkage __visible __trap_section void do_trap_software_check(st= ruct pt_regs *regs) > > > > +{ > > > > + if (user_mode(regs)) { > > > > + irqentry_enter_from_user_mode(regs); > > > > + > > > > + /* not a cfi violation, then merge into flow of unk= nown trap handler */ > > > > + if (!handle_user_cfi_violation(regs)) > > > > + do_trap_unknown(regs); > > > > + > > > > + irqentry_exit_to_user_mode(regs); > > > > + } else { > > > > + /* sw check exception coming from kernel is a bug i= n kernel */ > > > > + die(regs, "Kernel BUG"); > > > > + } > > > > +} > > > > + > > > > #ifdef CONFIG_MMU > > > > asmlinkage __visible noinstr void do_page_fault(struct pt_regs *re= gs) > > > > { > > > > > > > > -- > > > > 2.43.0 > > > >