Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1574323rdb; Wed, 24 Jan 2024 22:36:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IH0zwx4Ptt9/uQgAYzl3EUkWHoRiB0oIu0R4JdfQT0D4BBG380b3yia34KkXmmaycBz7m4g X-Received: by 2002:a17:902:fc8d:b0:1d5:f667:10a3 with SMTP id mf13-20020a170902fc8d00b001d5f66710a3mr497000plb.53.1706164575822; Wed, 24 Jan 2024 22:36:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706164575; cv=pass; d=google.com; s=arc-20160816; b=vh/HQKJdHZ4Sd6j0VcL6NAzHYbaAKRIvi634NtomQgN4COZY2EFI5yCgKOHe4QsezV 3sAkruSeMBJHc1R14HXU0rU8vbo1aFPcJ3UeqYXw6Dt40KP2qMxGyE2iOyuZn/M+xDyj eFBzuxorkrpcCnPy6bJ6Y0l6YWEh6d2lRSeg7/kJQBkUWK5qcLNDK+BG2eYdLR6Uiv8n 8wWcfW5HRMkjYwsIe1Kalgoj8I2PkeK13aRe5gJ3JGK1SUJ/KCqcC9zNitW0pWblSxPm 8Jc3t5J6LFhCUqsilFV1SMfL/awi0wva7ObfifN2wsREB8ifJpuWG4e/6lyjeRNUf0w8 XLgg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=CWoZzchBSoMo3jyN4IG6PBlEmlapmOPwf7uW/AAql2Y=; fh=oy7V1wWqiaug32hsWRXm98kKNp5NFPTWbY7PiGr4deM=; b=S17yAiEYEJbhu/tq+J/8irquGMeO7jyrPsX6zo89otnwX6liEcpJRnnRPdnutZvJUo zsQe3ocyH87XmovFJ5WtS3QuYELWyT+DIzmIkz0b2Zg0oZz9MXHYHwTa82bF+7+yCzUy ds3tRSx+9lJrRl4F1ywA75EKbdk69gD0/vZBQL2S4DDCKjdCXuXsQMftrf3wneC271ZB 8TtVs0/Q9hsWMr32aZSmZ1qRydnRq5BUeNSQeriVxEXtXP+ymPnRSFCOfygZk8vksTvs 1IBVxkS062rVOWvbUIQlLKtU3H1O1/kuIzAnB4bAEYmiz7L57XiSKjsZHCebbHoUguVc SB/Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LHuiVm1f; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-38029-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38029-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id k14-20020a170902c40e00b001d720046805si11098533plk.138.2024.01.24.22.36.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:36:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38029-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LHuiVm1f; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-38029-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38029-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 70C462838B9 for ; Thu, 25 Jan 2024 06:36:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 45FC112E58; Thu, 25 Jan 2024 06:30:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="LHuiVm1f" Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (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 3308E1BDE4 for ; Thu, 25 Jan 2024 06:30:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164217; cv=none; b=CQUWQcfuOi9vhoyQjSyuMVax+i1GkEDZRKoxZ40OgE5le7AeviqdVMSSlMinllDDk/ded7svEAML4EAuIBswH162dTLaMBqawrg65dpJxley/YGCGXyS+lzY3dSsFZcqyeShQnQeZGnuJZy+ZhFFFKHDGENofP+juJfyqSANDz8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164217; c=relaxed/simple; bh=Iw9o++lUhsJG5xH47NbdLp+1ZL2m0D0Eopr2tTL9+ds=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sBMKIsEbTQShm/fsUZRexzvNPNsiNnPJU2NB6/SJ4mfszGaC8H0zoTz12aqKupEf3xDh3kvIy1p91hNrc1ArAL6Uf5InNGsn0PT200rntHzLpIwNm3Mb9atrlkPOcwvgIsGATt8tKyiUmTD3t7zhUSBee0+pk4ukG3A9fJJcdJk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=LHuiVm1f; arc=none smtp.client-ip=209.85.210.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-6ddef319fabso4188697a34.1 for ; Wed, 24 Jan 2024 22:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164214; x=1706769014; darn=vger.kernel.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=CWoZzchBSoMo3jyN4IG6PBlEmlapmOPwf7uW/AAql2Y=; b=LHuiVm1fbiqrGfA6QIcn1hbrVZuZ3k08DRMPPnsQ8zadwBWNPiL75mftrtZQCo/ITk kTu50sCwOODwmf9EX38dbdpyTtpM3ha4CQ6nlAR6do/uhOZXdxcIZw3BX4rxZnlq+gAb oAOmTMrXAOkpZaK914Fx/tGMY6UmKayp2EE6NgXoQofxAQuRs8N56IPZ2yL89u9o2AO2 FiU3dSK6FRsC8P/yQ5VZCk0ctM7QpLrbyqfHWuaVIDtJwz/idqc1oGfXrEHckXI5S6Ra 4O9hjjkVLLBUWaUaG1KavRagQY/u2+RJLimEsAoXnv05GfxAjwEF3v7T9LPxDcqwyeMs jjTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164214; x=1706769014; 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=CWoZzchBSoMo3jyN4IG6PBlEmlapmOPwf7uW/AAql2Y=; b=FaiQc+TYjQcZPoRM3jWDWga7DghLUqqg3/b+pnttLi63rHEzlXGWE3qa+QPPW9tAX2 wjZbdzacS+j9HCS2de6FQjEVTwdHv8TJgd0lqSUQfiaa8+8SlcJkoVdjW87Ro6iAcvvk 9wTUYJJHkIbZaAoJ7tR6C41Rqx05m19NFy7EWfogdADTQOEvb8XQP3v/1MgULSsr5+ld u92VcyDpeOSSdh/uAdNnzuGsossIDN7M2I9Y8vgG9Cf23mwDAmoB8Di5kTHXsnWdTbwf rBR9M81cso9s8cYzsxMUYKCHvAw+dK7hryFSye8FwooPdAbk/Fox5Tkh23EUEvwlsRwc ymGA== X-Gm-Message-State: AOJu0YyrkrqHfrYkEXewxXQid+ciocePyGbqtvOIp3pH32O+CeZYh0Lk IIgP+RgLwHAuj+44tcpHV6XHKwiuO64D3MoQuu/1ENTCflUaG1AUaDuAeijn3JE= X-Received: by 2002:a9d:4d9a:0:b0:6dd:bf77:480 with SMTP id u26-20020a9d4d9a000000b006ddbf770480mr380479otk.51.1706164214351; Wed, 24 Jan 2024 22:30:14 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:14 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 21/28] riscv/traps: Introduce software check exception Date: Wed, 24 Jan 2024 22:21:46 -0800 Message-ID: <20240125062739.1339782-22-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Deepak Gupta 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 36b955c762ba..4ba8aea58dd0 100644 --- a/arch/riscv/include/asm/asm-prototypes.h +++ b/arch/riscv/include/asm/asm-prototypes.h @@ -24,6 +24,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 410659e2eadb..56dfe04094c1 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -369,6 +369,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) { -- 2.43.0