From patchwork Tue Jul 4 14:09:23 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: 115818 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp1257038vqx; Tue, 4 Jul 2023 07:20:22 -0700 (PDT) X-Google-Smtp-Source: APBJJlE5u16IKnByEHhORdYIiBIhdU17l5/qM7VWrZdFBt8Vy44bkVT4mE3+zYR1fmknciMJFJpQ X-Received: by 2002:a05:687c:12:b0:1ad:3199:cead with SMTP id yf18-20020a05687c001200b001ad3199ceadmr12478941oab.15.1688480422406; Tue, 04 Jul 2023 07:20:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688480422; cv=none; d=google.com; s=arc-20160816; b=aRYqVWd+jqQvt2YmX7qxA9chVgsPQVWyKxHBFliN8hAEazXfAKvHq2X6gqjQCnlTj2 OwrV04CwoBH1Rehs0356NTQ60gsK/B8Lt3y04JZNTggZR3BCsEHym2t83J19ssztHxkB jgW0DcOExGutz6LdXzJRv2D0t0tEdc8Du4yiqltg4tKVBzelYKRzrDMHul0iPmdgbjQR xopHwYCAbRxGKYRk1kqZA5exfg9cU7GM3raBeC+DVrHPKHzm/3+NE8BXiE/xSbtQWWt1 Sg232MxPC+4sufFYAI1pOTI25ZFnw+28+ztJWQj9NnUotXZyYotn0iMg54+K8OrUg491 NIwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AzBMq7R5xdVR7lBpakM9B1ewv3x8Aa9s+j6b6UY73u8=; fh=9wrou+MQEPZGr1Z6SaqMAXmpjnaJyNXl4Ao+MUOh3Is=; b=Osp/Rw0XrFGCQUaZODSJu6yumomJFCMa1cuPS2O7G0/wjvPKn6V8Wk7Kk/izIK7Qpw kAghdU7qepMIAjKB5p1r3Lqwma4nzkcFm+Ydbj2LK2AmfKtcB52oPC/Z81YyKQu80vEz FZ6I3Y35OQ21udyn/cabAhnEatMkGiM6cweMgtV3tkMKYAr+TqkR6CJ2xzxU02JXgMmu AmtkTZT+ZbyhWbkZ3X2lZhZPMP1PokuOmtym5yci7q4i0AwCN2dwHTr4gcBAJ8XiJyHb bY7Cj/0pSu0K0YU61n9iiMtp3Meutw4F61UVyXF/ufSxSiR1OnCjJdfIjAYgAY2QsG5U lmvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=hvWRzizV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nn18-20020a17090b38d200b0026389f6c799si9573739pjb.146.2023.07.04.07.20.06; Tue, 04 Jul 2023 07:20:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=hvWRzizV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231478AbjGDOMO (ORCPT + 99 others); Tue, 4 Jul 2023 10:12:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231616AbjGDOMH (ORCPT ); Tue, 4 Jul 2023 10:12:07 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7599010DA for ; Tue, 4 Jul 2023 07:12:04 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1b7e0904a3aso6750085ad.0 for ; Tue, 04 Jul 2023 07:12:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1688479924; x=1691071924; 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=AzBMq7R5xdVR7lBpakM9B1ewv3x8Aa9s+j6b6UY73u8=; b=hvWRzizVS0S/x2p6tjb3F48jVU/dzlutZSlw8XrL7FD9+thr9gbw2aaqzN6tsZKoSd 2Bjqk1qks5xIwsaJ5NMkcL3bSJO3KnFhsi8534y5jjbxdzdwq4hYpuTmR0c/k45hkPI2 SSdqBN/57ipAVGH1Gmk3Z0hN+ryPyEr6Ch9u/lhiHjq9W0Pgu99Ycd3dgy18oVMArTzR FDcNwx83BA3zeK//pwth3Bbji/waAPqqHxWVDJ1Vp+S5bLt98xaY0nfYjzC4wA1iq9J4 3Q8qbgzRw4CdhSpw1qzUM16Xpz1Oqy8g8bQnaf8MFIkxMvSrkE9rIuIugOSVUf1/Mp3k YBDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688479924; x=1691071924; 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=AzBMq7R5xdVR7lBpakM9B1ewv3x8Aa9s+j6b6UY73u8=; b=ULOz/atDkVU7tYqPTi1QCQUAkmT7Zjo1TO2Qe700oIG0frLTQvlrbPsm3Hu4uN7Ttc OetnjhDurgNMbCbgyYlWc9JftKg+Z2Qy1INv8QNvcn98p5AB1Ug43Q/MdN6WyIIRNEAi X/Ty2mQcNWHl1ZU4vr04tn0/b/N52QsnFCkN22YbwFlWPugxPYVCVvs1/BYMgQY+iWl9 0vlTuSJLsF7JyZsSgxeoVwkHQxEgNVvxYyACW/Dx6RYfm3v8VymNS5VOXmIaEEWBFnPb tmp6teV9dtsUDoA7vZQWW0TqcQkesJ+lxpoODJrlmChGfbIEFU2pQrlNjQ8tVspJO9YX FmeA== X-Gm-Message-State: ABy/qLa+JO7pVulNeIThuseGGFC8eLcNq3Vr+/JHTET3o6nB/b7rMvt8 n0EzkJpqtEb7m9Zwy7D+qyxQdw== X-Received: by 2002:a17:903:786:b0:1b3:ec39:f42c with SMTP id kn6-20020a170903078600b001b3ec39f42cmr13861775plb.5.1688479923889; Tue, 04 Jul 2023 07:12:03 -0700 (PDT) Received: from carbon-x1.home ([2a01:cb15:81c2:f100:ef7b:e0f7:d376:e859]) by smtp.gmail.com with ESMTPSA id o1-20020a170902bcc100b001b042c0939fsm17183735pls.99.2023.07.04.07.11.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jul 2023 07:12:03 -0700 (PDT) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Stafford Horne , Brian Cain , Kefeng Wang , "Russell King (Oracle)" , Michael Ellerman , Sunil V L , Anup Patel , Atish Patra , Andrew Jones , Conor Dooley , Heiko Stuebner , Guo Ren , Alexandre Ghiti , Masahiro Yamada , Xianting Tian , Sia Jee Heng , Li Zhengyu , Jisheng Zhang , "Gautham R. Shenoy" , Mark Rutland , Peter Zijlstra , Marc Zyngier , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Krzysztof Kozlowski , Evan Green , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC V2 PATCH 8/9] riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN Date: Tue, 4 Jul 2023 16:09:23 +0200 Message-Id: <20230704140924.315594-9-cleger@rivosinc.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230704140924.315594-1-cleger@rivosinc.com> References: <20230704140924.315594-1-cleger@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770500047278090846?= X-GMAIL-MSGID: =?utf-8?q?1770500047278090846?= Now that trap support is ready to handle misalignment errors in S-mode, allow the user to control the behavior of misalignment accesses using prctl(). 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 --- arch/riscv/include/asm/cpufeature.h | 8 ++++++++ arch/riscv/include/asm/processor.h | 9 +++++++++ arch/riscv/kernel/process.c | 18 ++++++++++++++++++ arch/riscv/kernel/traps_misaligned.c | 7 +++++++ 4 files changed, 42 insertions(+) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index 7e968499db49..e2fd6fc7157f 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -6,6 +6,8 @@ #ifndef _ASM_CPUFEATURE_H #define _ASM_CPUFEATURE_H +#include + /* * These are probed via a device_initcall(), via either the SBI or directly * from the corresponding CSRs. @@ -20,6 +22,12 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo); DECLARE_PER_CPU(long, misaligned_access_speed); +static inline bool misaligned_access_emulated(void) +{ + return per_cpu(misaligned_access_speed, 0) == + RISCV_HWPROBE_MISALIGNED_EMULATED; +} + void __init misaligned_emulation_init(void); #endif diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index 94a0590c6971..4e6667d5ca68 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -7,6 +7,7 @@ #define _ASM_RISCV_PROCESSOR_H #include +#include #include @@ -39,6 +40,7 @@ struct thread_struct { unsigned long s[12]; /* s[0]: frame pointer */ struct __riscv_d_ext_state fstate; unsigned long bad_cause; + unsigned long align_ctl; }; /* Whitelist the fstate from the task_struct for hardened usercopy */ @@ -51,6 +53,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) \ @@ -80,6 +83,12 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid); extern void riscv_fill_hwcap(void); extern int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src); +extern int get_unalign_ctl(struct task_struct *, unsigned long addr); +extern int set_unalign_ctl(struct task_struct *, 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 e2a060066730..88a71359396b 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -24,6 +24,7 @@ #include #include #include +#include register unsigned long gp_in_global __asm__("gp"); @@ -40,6 +41,23 @@ void arch_cpu_idle(void) cpu_do_idle(); } +int set_unalign_ctl(struct task_struct *tsk, unsigned int val) +{ + if (!misaligned_access_emulated()) + return -EINVAL; + + tsk->thread.align_ctl = val; + return 0; +} + +int get_unalign_ctl(struct task_struct *tsk, unsigned long adr) +{ + if (!misaligned_access_emulated()) + 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 243ef9314734..5fb6758b0bf9 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -305,6 +306,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; @@ -398,6 +402,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;