From patchwork Wed Oct 4 15:14:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13408896 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BA7D8E7C4D4 for ; Wed, 4 Oct 2023 15:14:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=V+04qVmmI07cK3Fna6WMPuulFnzZU7F5MvjN1RerLdg=; b=vkDbnOqshklSXI 9rzuRhTGK1UZEtAmsBwFsWzXxLSrN1ElNgx3G6xpSX8/VmTzXERYzRFEXbhCwnUx55ITsyZwxSNDX A2+YWLSfr1XI/Pu79QLvkpVFYcpnQ7W373GsA+gGX91ivRCB+3ylOSdtq6tmAxnT8tsSKR5uE8IDb u2MmD1gJgm7+hndsCxFxui+4xrnsTmtZKxVf3ipjlhSXnOf4ZDZQgxANh2Q55bPiyMUNCS6YI1WdM pZ8iCESMrTwn8Qi2lkNUktx1dRtPumQ99mv9pDBl+TCs8fNuwxiTqGVASxIFrqV0fn2PmilmKM9X+ GGYIGNHvULqmdQhVnJMA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qo3aA-000Jgr-0K; Wed, 04 Oct 2023 15:14:38 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qo3a4-000JcN-2u for linux-riscv@lists.infradead.org; Wed, 04 Oct 2023 15:14:34 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-4064e3c7c07so3944725e9.1 for ; Wed, 04 Oct 2023 08:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1696432471; x=1697037271; darn=lists.infradead.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=mgvk3biMyb3dWXWlkLQojD9wXEb7N0NOjcs65taIzUI=; b=mIYNHuyg4HaaFYxDuAMNhfkyGj5s+G3hPMnWJneSk7hn+YjBRZymR/t3oe4reTzGpR kMjbZ5dNjGEy4dYslZnkt3CuBykmcyiDAbsFRoGqK/puIfZCZjOv5p9SJC14jcvt2Zgd ZzQhMorkziFWoiheMFF/AUB5Y6qhz7oVqvff4+dzup5Q/LZ68FStB+i9nTzaKaaIRq1F 6AsMh6eFBJSqpk3zcHu+EJDSyzPFSCOmQ1NYTkAxcSqAZbgpKB5t7Jd8fg8JV9pNOvJq UM1/SPRUjy6b1WNqewQJSo5lWr/fYPw1sECwvHckTZF6L4nogytAV3fVSUyiJCDjCDLj eBgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696432471; x=1697037271; 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=mgvk3biMyb3dWXWlkLQojD9wXEb7N0NOjcs65taIzUI=; b=AJycp1YuDBwbpqJf/YltV5mmPQ84ccgLOqXkeqQRPbPhKvLHwUZYCjwprtDXG7GeAT bSjLzPyGj5mk842qgVeF6GTA4eFtKmQ5Oe6dkgritbJBaYIzGQ32ATC9yD5dqCT6Y1r/ Us6Bia+chrzOTexFEz+QXrQ9YmU+Gh8ZOpE903ZPUFDnCGgeG6LK+uomsPLYrOsX2BT8 478hp5G8IZOkiLYfm/ZwNHeF5XQZKPRQrexyWMk3ag8g3OTWp52OIJ5SNVEi3CszxIBC K2202mbHmeJW9KaVo6nD0AIJd+DsgWqQh+UZvPRNJJj8z7+IGE+/rMmTIgrNI6aUPnad 6NEA== X-Gm-Message-State: AOJu0YyC1bQKLUdRxNltMKp09jPWtqHG5tQxfz/7+angWnKLkr7xnRsd 8l5QYrppE9E1MMDMu6cknExBNA== X-Google-Smtp-Source: AGHT+IFWFyxvkpKoO/6/23fHvmhwPdqiEfb/7rp6Yc0TRcJio42GVyA9Nn9VHADu7IcrpwduTsF9nw== X-Received: by 2002:a05:600c:5192:b0:405:1ba2:4fcf with SMTP id fa18-20020a05600c519200b004051ba24fcfmr2488360wmb.4.1696432471665; Wed, 04 Oct 2023 08:14:31 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:999:a3a0:9474:8d75:5115:42cb]) by smtp.gmail.com with ESMTPSA id i2-20020a05600c290200b00402f7b50517sm1768764wmd.40.2023.10.04.08.14.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 08:14:31 -0700 (PDT) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Atish Patra , Andrew Jones , Evan Green , =?utf-8?q?Bj=C3=B6rn_Topel?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Ron Minnich , Daniel Maslowski , Conor Dooley Subject: [PATCH v2 8/8] riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN Date: Wed, 4 Oct 2023 17:14:05 +0200 Message-ID: <20231004151405.521596-9-cleger@rivosinc.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231004151405.521596-1-cleger@rivosinc.com> References: <20231004151405.521596-1-cleger@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231004_081432_945149_EA391D2F X-CRM114-Status: GOOD ( 14.98 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Now that trap support is ready to handle misalignment errors in S-mode, allow the user to control the behavior of misaligned accesses using prctl(PR_SET_UNALIGN). Add an align_ctl flag in thread_struct which will be used to determine if we should SIGBUS the process or not on such fault. Signed-off-by: Clément Léger Reviewed-by: Björn Töpel --- arch/riscv/include/asm/processor.h | 9 +++++++++ arch/riscv/kernel/process.c | 18 ++++++++++++++++++ arch/riscv/kernel/traps_misaligned.c | 6 ++++++ 3 files changed, 33 insertions(+) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 3e23e1786d05..adbe520d07c5 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -8,6 +8,7 @@ #include #include +#include #include @@ -82,6 +83,7 @@ struct thread_struct { unsigned long bad_cause; unsigned long vstate_ctrl; struct __riscv_v_ext_state vstate; + unsigned long align_ctl; }; /* Whitelist the fstate from the task_struct for hardened usercopy */ @@ -94,6 +96,7 @@ static inline void arch_thread_struct_whitelist(unsigned long *offset, #define INIT_THREAD { \ .sp = sizeof(init_stack) + (long)&init_stack, \ + .align_ctl = PR_UNALIGN_NOPRINT, \ } #define task_pt_regs(tsk) \ @@ -134,6 +137,12 @@ extern long riscv_v_vstate_ctrl_set_current(unsigned long arg); extern long riscv_v_vstate_ctrl_get_current(void); #endif /* CONFIG_RISCV_ISA_V */ +extern int get_unalign_ctl(struct task_struct *tsk, unsigned long addr); +extern int set_unalign_ctl(struct task_struct *tsk, unsigned int val); + +#define GET_UNALIGN_CTL(tsk, addr) get_unalign_ctl((tsk), (addr)) +#define SET_UNALIGN_CTL(tsk, val) set_unalign_ctl((tsk), (val)) + #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e32d737e039f..4f21d970a129 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -25,6 +25,7 @@ #include #include #include +#include register unsigned long gp_in_global __asm__("gp"); @@ -41,6 +42,23 @@ void arch_cpu_idle(void) cpu_do_idle(); } +int set_unalign_ctl(struct task_struct *tsk, unsigned int val) +{ + if (!unaligned_ctl_available()) + return -EINVAL; + + tsk->thread.align_ctl = val; + return 0; +} + +int get_unalign_ctl(struct task_struct *tsk, unsigned long adr) +{ + if (!unaligned_ctl_available()) + return -EINVAL; + + return put_user(tsk->thread.align_ctl, (unsigned long __user *)adr); +} + void __show_regs(struct pt_regs *regs) { show_regs_print_info(KERN_DEFAULT); diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index d99b95084b6c..bba301b5194d 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -418,6 +418,9 @@ int handle_misaligned_load(struct pt_regs *regs) if (!unaligned_enabled) return -1; + if (user_mode(regs) && (current->thread.align_ctl & PR_UNALIGN_SIGBUS)) + return -1; + if (get_insn(regs, epc, &insn)) return -1; @@ -517,6 +520,9 @@ int handle_misaligned_store(struct pt_regs *regs) if (!unaligned_enabled) return -1; + if (user_mode(regs) && (current->thread.align_ctl & PR_UNALIGN_SIGBUS)) + return -1; + if (get_insn(regs, epc, &insn)) return -1;