From patchwork Thu Jan 25 06:21:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13530077 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 61F72C47258 for ; Thu, 25 Jan 2024 06:29:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E72566B0092; Thu, 25 Jan 2024 01:29:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D895F6B0093; Thu, 25 Jan 2024 01:29:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3FCD6B0095; Thu, 25 Jan 2024 01:29:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9A2886B0092 for ; Thu, 25 Jan 2024 01:29:05 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6DA3812048E for ; Thu, 25 Jan 2024 06:29:05 +0000 (UTC) X-FDA: 81716855850.29.BE1D3AD Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) by imf09.hostedemail.com (Postfix) with ESMTP id 962C414001F for ; Thu, 25 Jan 2024 06:29:03 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wf9FbnXG; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706164143; 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=rs1o7FymY4gTUwXZVJx54t4Zjv5gF4CojhNG+aQW4Ms=; b=y+l2VbNpfuhCYJnLeDjFVgwICqrQb2eW3YcjSHxc1NvGmbgp3q+XerPH7M2XmHFUlR/OaF r9Dj6cdAbqUMOCO2qvmWwpJkZ+HILHvQ7/bVHDNg8BbZrzektC/ub9koPCFW8oPL0O0zKQ 57oAg6d9wExwmfcxQXAIjFhBe/B6sTo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706164143; a=rsa-sha256; cv=none; b=HydbyfRxyK/JDljEW5n0UBHIgnWOExRaE8A9ywUAIFJDYgG0oLBsC1HKGlx8crz9s+mrBQ RIOaoPX7ym8qSheN0t9E7psxJ84lktGvo2ibjthqYWZN6P5wMwLPbnULiZEtZ9Ky5PtSNI jzpVJSoy7mUlG9YU8YeBHt5iMn0wabU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=Wf9FbnXG; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.161.43 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-59927972125so3173935eaf.3 for ; Wed, 24 Jan 2024 22:29:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164142; x=1706768942; 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=rs1o7FymY4gTUwXZVJx54t4Zjv5gF4CojhNG+aQW4Ms=; b=Wf9FbnXGX8ehBVRWbvliEzD5Nhc4hBG2jkrNF864+hCXukySPxquNLi/BTs3jBxWkV 0n0lm8WRLZUk9J4STgRrFscB9l6zPEseRXoOv8kVusy6cJPk7NjxnxOGZxdDCCe+QMZh lHTphb7KyRIrCjSsDyaaCxoWoOvd3i12+351Xc/S1tf735BEG0m49GCtT1/GqbNSujlf owcQRgSPOYDz5OczBGVevjHG/JHU0pknUZ1z9Ryu/RDrPZO+cKJniz0773UUrjoy/CXJ sYP98YInIma9yaGJj8qGSaEAPH7UxmY15JEmjADRLdMJgNUvtaT8lG0nezGt/gr63WXY EUsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164142; x=1706768942; 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=rs1o7FymY4gTUwXZVJx54t4Zjv5gF4CojhNG+aQW4Ms=; b=vEv39g8wLeZcS0m/vPLPx8OaGbdEe5dsXgi2RW7fDaI6mEdhERNBR1GOQuN+w3xSfm 4cZ6/KfPTKq1vu6uFOyl/5bic7v6CZ8jmTDArOqxN/IGL6epfPu+RTajAc92JuQYNbMk Mm+c01PAJT/pJf3ZOStInq99GMGAEk6XVbBOASGPFd3GOjDYCmiAN3ws2RMBULTmMJsq aPVeHnv6ReAW4qjjZcg8mADimCqC+/rggF7ko47rulxIPC9jjmsWpdx9+txfsHbjmz/k F3cps7N21x/v2ef5IXGrYjIleUGCcxHrryeGMuJMgOLqfGAVHb772BfJ/a/VLVa3gHQK NpBQ== X-Gm-Message-State: AOJu0YypSW2wMrA0ksnt89cJSZvB+VsT9MKjei3KORP35lPP8ya0pYet YAAUGhqwYSN01b79uIHs7sKFHFIc/wMU6ly7OJU6ERgtGcKq62cfmTf8HlxjrqM= X-Google-Smtp-Source: AGHT+IGQZnueATOgDHrwaXrd5AWfHpxvbbHwGR+jLnJrGj0m/f+oWOE49v8FM+hbtn4VeA1L1U6wQQ== X-Received: by 2002:a05:6358:d388:b0:176:5cad:5144 with SMTP id mp8-20020a056358d38800b001765cad5144mr620289rwb.45.1706164142630; Wed, 24 Jan 2024 22:29:02 -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.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:29:02 -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 07/28] riscv: kernel handling on trap entry/exit for user cfi Date: Wed, 24 Jan 2024 22:21:32 -0800 Message-ID: <20240125062739.1339782-8-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> MIME-Version: 1.0 X-Stat-Signature: skw57858zggf9ct3ew3sxidp5q7a95w3 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 962C414001F X-Rspam-User: X-HE-Tag: 1706164143-783453 X-HE-Meta: U2FsdGVkX1/CeIrFpmkRyaf1zNZockb2sg1qq84YEvE4GF57LFNDFtD4LrB7z+QYm5hYjn8gZMnC2uEWno6WHHVE6EXb+bSBEeBvuoOxGiQ4KNrAFwK+zAWiPrPPRhZR9bUUUYbu5BaGCVGcxz4VLsC/MU7/2vPQq9a4C2gIIt7+kgvyVT6QBuMKN8f6HYKEJE1i94J0Fmu29oqHMnc0hgFF1+HrtVgZN9uBm5ATlVDGG5e7wghBwPqX58MGjg4fcXxQ2V3kZzKJndbapIoXpgCfMHxl8kthZsdY82xDeXuS7l4ZrOPMS5zlTZGXZvyG4kZlnooqRTTro2MI5eSUQ4ATEU6gr0UVSAmYk3IiF4flCf4lRPLRhHYSHeLDhdPpBPcDkyuZfZW1dlGtbwirgouSj5tzgFZEtOChAzM4nKt7ebLh1tsyAHggnzdRQ1f2yTzIyiSf8T64Vmxyy/PvojRJb+AvJW6KK7AQsAE598GnUifi/qIjcPG//D/dCprHFJHfUvVgxHo8h0YrYI0zC82vzomWslWT+c8takbWbOj++zUNBbrqYwC4oXi8u+LokhmKiA5d/H6EQ5PlI4jLtdyK15zdGRETsWVy9U9arEUJld1P1zb8AzvGvp7j6vWXPJEpXarC6eF3HIBMVq+JF91pbvOiY+GhW3UFmUgww5gbCIeKoYZDHcITAIuUdUXVesNogyBUnw1fFPdgEYMxQd5312sy9tvfnh11v5FYeyGMUvq93mulBh/gm3AcOqtxcy4jNXvPuHVSWYvxCNEYjftzyiYISuER3PIyAaw9qAlyc+vvDwcKyD0laVKfWmJwU1mSuoXJzB4J4+25gNA/kq3eP74ZYgoEkdtjyJjQnNWo0x4xDSknUnboDWWWoX5/sMf95+r6ASR+4z58xSFe/Zj0Hg0o2Yug/nveJiojxSAdTXt8DgjxKvGIFP8YJkSCiDZXhyrrurzjFpWotGk j4j89sUm pXqIzDakZctO8V3g3sDh3aoiDGHMPTLKR8s9eKPPbHeYrL1nZfRAmm3DmascoEfRzPw1tapYVbk4SyGRUltu+bgfvh17dd6+Ug9l1jth2hu35khUTKcBurCkf0FeQHqIvYT7Y83IiYqciZ+9949LkuOT2jwy7okfeO4k6pct+v2GraMBYGDPFkGkZ2PWuXxkxqW+MTDtesq64Bfr0+fWnhbBuRlQHc0QYiV0uIRr2p8NyQ5fSyAJOBsxa6QRkGQObAGlrN6m4H46GAXqpErgNViPxm1SXWE8xBw9reiiepSq8CBYe34e6JgQAOrTbCjbvW9Lf0ECip5Tv5eS62qJXrDAM6MYX42lfjXwLyBD9fCZ7tg0tDicAA0Lfetu2+jFmVLQKUQXhWAMhY2AmKW/zP/XJM0obKEehpf3b0pzBsUJGaFIjCi+nsZt6BnRKSX98kqf+ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Deepak Gupta Carves out space in arch specific thread struct for cfi status and shadow stack in usermode on riscv. This patch does following - defines a new structure cfi_status with status bit for cfi feature - defines shadow stack pointer, base and size in cfi_status structure - defines offsets to new member fields in thread in asm-offsets.c - Saves and restore shadow stack pointer on trap entry (U --> S) and exit (S --> U) Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/processor.h | 1 + arch/riscv/include/asm/thread_info.h | 3 +++ arch/riscv/include/asm/usercfi.h | 24 ++++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 5 ++++- arch/riscv/kernel/entry.S | 25 +++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/include/asm/usercfi.h diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index ee2f51787ff8..d4dc298880fc 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,6 +14,7 @@ #include #include +#include #ifdef CONFIG_64BIT #define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 320bc899a63b..6a2acecec546 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -58,6 +58,9 @@ struct thread_info { int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ unsigned long envcfg; +#ifdef CONFIG_RISCV_USER_CFI + struct cfi_status user_cfi_state; +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h new file mode 100644 index 000000000000..080d7077d12c --- /dev/null +++ b/arch/riscv/include/asm/usercfi.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 + * Copyright (C) 2023 Rivos, Inc. + * Deepak Gupta + */ +#ifndef _ASM_RISCV_USERCFI_H +#define _ASM_RISCV_USERCFI_H + +#ifndef __ASSEMBLY__ +#include + +#ifdef CONFIG_RISCV_USER_CFI +struct cfi_status { + unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ + unsigned long rsvd : ((sizeof(unsigned long)*8) - 1); + unsigned long user_shdw_stk; /* Current user shadow stack pointer */ + unsigned long shdw_stk_base; /* Base address of shadow stack */ + unsigned long shdw_stk_size; /* size of shadow stack */ +}; + +#endif /* CONFIG_RISCV_USER_CFI */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_RISCV_USERCFI_H */ diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index cdd8f095c30c..5e1f412e96ba 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -43,8 +43,11 @@ void asm_offsets(void) #ifdef CONFIG_SHADOW_CALL_STACK OFFSET(TASK_TI_SCS_SP, task_struct, thread_info.scs_sp); #endif - OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu); +#ifdef CONFIG_RISCV_USER_CFI + OFFSET(TASK_TI_CFI_STATUS, task_struct, thread_info.user_cfi_state); + OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk); +#endif OFFSET(TASK_THREAD_F0, task_struct, thread.fstate.f[0]); OFFSET(TASK_THREAD_F1, task_struct, thread.fstate.f[1]); OFFSET(TASK_THREAD_F2, task_struct, thread.fstate.f[2]); diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 63c3855ba80d..410659e2eadb 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -49,6 +49,21 @@ SYM_CODE_START(handle_exception) REG_S x5, PT_T0(sp) save_from_x6_to_x31 +#ifdef CONFIG_RISCV_USER_CFI + /* + * we need to save cfi status only when previous mode was U + */ + csrr s2, CSR_STATUS + andi s2, s2, SR_SPP + bnez s2, skip_bcfi_save + /* load cfi status word */ + lw s3, TASK_TI_CFI_STATUS(tp) + andi s3, s3, 1 + beqz s3, skip_bcfi_save + csrr s3, CSR_SSP + REG_S s3, TASK_TI_USER_SSP(tp) /* save user ssp in thread_info */ +skip_bcfi_save: +#endif /* * Disable user-mode memory access as it should only be set in the * actual user copy routines. @@ -141,6 +156,16 @@ SYM_CODE_START_NOALIGN(ret_from_exception) * structures again. */ csrw CSR_SCRATCH, tp + +#ifdef CONFIG_RISCV_USER_CFI + lw s3, TASK_TI_CFI_STATUS(tp) + andi s3, s3, 1 + beqz s3, skip_bcfi_resume + REG_L s3, TASK_TI_USER_SSP(tp) /* restore user ssp from thread struct */ + csrw CSR_SSP, s3 +skip_bcfi_resume: +#endif + 1: REG_L a0, PT_STATUS(sp) /*