From patchwork Fri Mar 29 04:44:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610064 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5FAC6C6FD1F for ; Fri, 29 Mar 2024 04:46:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E32FB6B00B8; Fri, 29 Mar 2024 00:46:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0B056B00B9; Fri, 29 Mar 2024 00:46:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5E136B00BA; Fri, 29 Mar 2024 00:46:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9EA756B00B8 for ; Fri, 29 Mar 2024 00:46:53 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 71E35A0504 for ; Fri, 29 Mar 2024 04:46:53 +0000 (UTC) X-FDA: 81948841506.12.BAE06CC Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) by imf26.hostedemail.com (Postfix) with ESMTP id A1217140009 for ; Fri, 29 Mar 2024 04:46:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VE3Aqg3f; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687611; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YO0iTqSJGMZjgI8FFL6Efs68zyEh3di0+Ee8Co7Xl3I=; b=L7AWEtHYfG/2ujEvK3nLFEYmtxqdkGhiK4nBiB1DUjCNYZ6MDMAQyi8sv/DvAjkJZYZQ9+ hs0rxGUjNO3Xlft3VNS0U3WMoBi/g4U9POV3c5w9P8kNoycnVUqvczDXlTvsSpuKFiFiBM Jpd1rKNm6IrtH5DCZXUNcbpjCCB+3QI= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=VE3Aqg3f; dmarc=none; spf=pass (imf26.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687611; a=rsa-sha256; cv=none; b=JWpSALa6HU+XwJCLYSlAJpzMuEmtkw34Ukak/GfUIJMntPERNDZh+pamZNCjtJcAWXWy6g 8EU5bFHhWz/9KmmODgyl+oqBn7vABGVDAQQoNqlcR6MKHq4suvq9iOj2LQZgyAcsFIJPdf HjNCj8J8sS1j3h9LfgClE3dymYPnwns= Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3c36ecdb8cdso778611b6e.1 for ; Thu, 28 Mar 2024 21:46:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687610; x=1712292410; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YO0iTqSJGMZjgI8FFL6Efs68zyEh3di0+Ee8Co7Xl3I=; b=VE3Aqg3fTugZ1wWcTpmhlww+mfLiOGBHuY66eDsG+jfzpVl5pAAQCwYg9iFKgdjmVZ uYegAB8dLCMcI1qG+DTwFp5/0wPlS+0/P7lNPddwOeIFPKiyu/Z47/Csd36/gCo/1cUO Ve4k9mvhQ9YClhSofa8NNlbUaCK3UJCGdasBVm7nCihbgD3mLNwdKfliHUgHxhB9zk35 w8n9LgIMWPhxmv+R2iIFsEDivIiswedyRzo77Dlh7sWhcnLybbxW9JmaXArKb15wD99H V/L/I/pfVEUav6e2yY0j8Yjpr8Ea4uhMq30yNWiteuuPkumu1f0iKwO6euqdKdiyBVSH hyog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687610; x=1712292410; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YO0iTqSJGMZjgI8FFL6Efs68zyEh3di0+Ee8Co7Xl3I=; b=VHfsCIZffs/vgpJ5AruuJP20lKpauH48/LoEsKnZe9ER6js3Y/WPS+btjheI666mPY /L+JwvUbIU6AQgqkXYW4yY0DstkBaIKAJaXrYPMhCTtya9zq97Q6RXfX6n4+fmVF/+dS TZK3zeUC/LBGBBP6fWwkPN7ExkRioNPldThIls9opH7JBDjFkP8cVdHvMl9y+KT+QYD3 PGZhxbyZ/7JUsTvI3r7rJEqqFhU4mEf247yJhtyIwha2HLeGAbhk99kpI0gMNF2RO6+N v0kS2s1Xpb2VEYPCZ+Dkpr64YYw374iHFzS3Mv6RziXewM3puJy6K39J/nw7TlNOscBY Kgpg== X-Forwarded-Encrypted: i=1; AJvYcCUJGYDNojRvkU5T55/7RRE0/z1+AlGBVCS/l7oRKhxtTGMpbOafPr0JYqxjuk+cLc5s11CAfCU14k6wi4E6DQZZE1o= X-Gm-Message-State: AOJu0YzpOFohLC4GJiUIEBG+4zCiNFmURoVoMF+mW6HcD726Qf5oXGZN /TkZe95puoKsU8x71XuPaM9Nt/PdvUs2alNpiCa4ziWXOxIQxf9kjIBxdOI29+k= X-Google-Smtp-Source: AGHT+IEEkqqoh9ZCSipkYJyi1sYiOeAupA2Zd/Tw6FmqvNWOmZwwVKoooYj2QwBiFKxvGUt1R6r40A== X-Received: by 2002:a05:6808:1885:b0:3c3:e436:881f with SMTP id bi5-20020a056808188500b003c3e436881fmr1602543oib.36.1711687610580; Thu, 28 Mar 2024 21:46:50 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:49 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 20/27] riscv/traps: Introduce software check exception Date: Thu, 28 Mar 2024 21:44:52 -0700 Message-Id: <20240329044459.3990638-21-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A1217140009 X-Stat-Signature: 3yzs3ythz945tdfnink7j4x3yftfgf9o X-Rspam-User: X-HE-Tag: 1711687611-695807 X-HE-Meta: U2FsdGVkX1/3eyjW+z9DJ38giiim5f6ZV0nR5FGY2S4sM2CwoeM90n2C+5ZoIwU4bmwVFG7jmHxwko1jo/qmuQv/FSRKD9yOg+QNHSmNW5ty/Vfl6VGOiBt+Js8C0RIqLqkkMZHACmgqJztYeM+1ZIjQ6IFhEGuubgi7bqgeIIZvkdUgiHBG9lHRxDlMaUXd8mjHT9FnZqSnTylkx+j1W+fUKOina+0APwZ74fw/CBFIY2f6FHRpr8q0QVrzzp4or+u/cW1PPJk8LnEO60JNAdSYDY7WmE+ji8+R5a19fhledQ4W7u6EuD+KG79tEl128MCXuXyt39/H/6CFaojz1GwoQ47OLtxg8r6sjdgS4wKmdLpo438wUV+aHuAPqylQ/bba34BXWG8Rn3XG7P1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gQs0mgM0QTm8Lk+UVI8aJvKJfT3cinrIe00= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: zicfiss / zicfilp introduces a new exception to priv isa `software check exception` with cause code = 18. This patch implements software check exception. Additionally it implements a cfi violation handler which checks for code in xtval If xtval=2, it means that sw check exception happened because of an indirect branch not landing on 4 byte aligned PC or not landing on `lpad` instruction or label value embedded in `lpad` not matching label value setup in `x7`. If xtval=3, it means that sw check exception happened because of mismatch between link register (x1 or x5) and top of shadow stack (on execution of `sspopchk`) In case of cfi violation, SIGSEGV is raised with code=SEGV_CPERR. SEGV_CPERR was introduced by x86 shadow stack patches. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/asm-prototypes.h | 1 + arch/riscv/kernel/entry.S | 3 ++ arch/riscv/kernel/traps.c | 38 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/asm/asm-prototypes.h index cd627ec289f1..5a27cefd7805 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -51,6 +51,7 @@ DECLARE_DO_ERROR_INFO(do_trap_ecall_u); DECLARE_DO_ERROR_INFO(do_trap_ecall_s); DECLARE_DO_ERROR_INFO(do_trap_ecall_m); DECLARE_DO_ERROR_INFO(do_trap_break); +DECLARE_DO_ERROR_INFO(do_trap_software_check); asmlinkage void handle_bad_stack(struct pt_regs *regs); asmlinkage void do_page_fault(struct pt_regs *regs); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 7245a0ea25c1..f97af4ff5237 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -374,6 +374,9 @@ SYM_DATA_START_LOCAL(excp_vect_table) RISCV_PTR do_page_fault /* load page fault */ RISCV_PTR do_trap_unknown RISCV_PTR do_page_fault /* store page fault */ + RISCV_PTR do_trap_unknown /* cause=16 */ + RISCV_PTR do_trap_unknown /* cause=17 */ + RISCV_PTR do_trap_software_check /* cause=18 is sw check exception */ SYM_DATA_END_LABEL(excp_vect_table, SYM_L_LOCAL, excp_vect_table_end) #ifndef CONFIG_MMU diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index a1b9be3c4332..9fba263428a1 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -339,6 +339,44 @@ asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs) } +#define CFI_TVAL_FCFI_CODE 2 +#define CFI_TVAL_BCFI_CODE 3 +/* handle cfi violations */ +bool handle_user_cfi_violation(struct pt_regs *regs) +{ + bool ret = false; + unsigned long tval = csr_read(CSR_TVAL); + + if (((tval == CFI_TVAL_FCFI_CODE) && cpu_supports_indirect_br_lp_instr()) || + ((tval == CFI_TVAL_BCFI_CODE) && cpu_supports_shadow_stack())) { + do_trap_error(regs, SIGSEGV, SEGV_CPERR, regs->epc, + "Oops - control flow violation"); + ret = true; + } + + return ret; +} +/* + * software check exception is defined with risc-v cfi spec. Software check + * exception is raised when:- + * a) An indirect branch doesn't land on 4 byte aligned PC or `lpad` + * instruction or `label` value programmed in `lpad` instr doesn't + * match with value setup in `x7`. reported code in `xtval` is 2. + * b) `sspopchk` instruction finds a mismatch between top of shadow stack (ssp) + * and x1/x5. reported code in `xtval` is 3. + */ +asmlinkage __visible __trap_section void do_trap_software_check(struct pt_regs *regs) +{ + if (user_mode(regs)) { + /* not a cfi violation, then merge into flow of unknown trap handler */ + if (!handle_user_cfi_violation(regs)) + do_trap_unknown(regs); + } else { + /* sw check exception coming from kernel is a bug in kernel */ + die(regs, "Kernel BUG"); + } +} + #ifdef CONFIG_MMU asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs) {