Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1948057rdh; Tue, 26 Sep 2023 08:05:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHvzYcWAHu2p2f9CQFn8bNzdF5uVXQNe0SSzExA0P/0Sy4Z4z6lxOwy1hqcvEQCUo1ZjoH2 X-Received: by 2002:a05:6a21:a586:b0:159:fe1d:2f09 with SMTP id gd6-20020a056a21a58600b00159fe1d2f09mr13450438pzc.6.1695740702896; Tue, 26 Sep 2023 08:05:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695740702; cv=none; d=google.com; s=arc-20160816; b=LICWA4g69UffEAujpIHyGiLLQHaIPph/GHqcOSI9kF74N+yAHQZ8FtRIszoqBIz/5m KDFEKpPdAbtD1CI7SsOf9ct0CT7CyqEDjC961fKZqBD+ZriKor4oEGq02jFU6yXhKgpA cKoopYPWjO7pV4RLhMMZ7aDRzBHNzxwcRMYh2EBUGHjZRGukEzJ7LcRnoUr8oxVmCgMn qd9jF9wyu7vNtBBwMHtSrAQnBh8yfRL4OKusdngv2c12PnYQQsMZuYdSAWlm2RlEuUqX k4NrSEnOmM0nrTMe2+mLGNgc+609OY7TW50WI/+BBj2OaViuDEBw0T5kwHKMCZijW1Mj GxWg== 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=dFmLYHjhKHshwcnMAhKMVILwqWg7P2jKMKJHqTBO9sw=; fh=eI1w0vL8wMQYq+89KHAVQG8zXClfMmqUQ4ir9T7VotM=; b=vvmzXWLqohBRws37Yvzk+TPxI5JQZbhL44/8vc6wclKM4wuz9iAfpgTRu2dLkCAOZr ZZ9drhuqf7L82VnmQkld0AcorW41u0NK8sI1uLhr25YfRo8aJDF1VApg5tOGUcrSkTVD LuXC2ztx3M9uNLaI4W60zo3slOstYs3ZMKhcDx7p9nqX67O7ODCUEPx0CFofr/CJk8tN LamAVngT6ugYcPY7tY7Kq/73AF14q30TW+yBLvcdqxmbacRbzNlIejIWksyIMiHry3or meEFUVgdtnteQj0hof+Fs2RKbVVtlgZjJ3xfkfd0h3ZJYpPAS22+V9wy33FQjeSz2rst XYBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OCOs+Toc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id f24-20020a63e318000000b00578a2da998asi12880630pgh.304.2023.09.26.08.05.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:05:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OCOs+Toc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 5A5B281C4A86; Tue, 26 Sep 2023 08:04:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235054AbjIZPEU (ORCPT + 99 others); Tue, 26 Sep 2023 11:04:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235023AbjIZPEH (ORCPT ); Tue, 26 Sep 2023 11:04:07 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C16C11D for ; Tue, 26 Sep 2023 08:04:00 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-3232bb059d3so776037f8f.1 for ; Tue, 26 Sep 2023 08:04:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695740638; x=1696345438; 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=dFmLYHjhKHshwcnMAhKMVILwqWg7P2jKMKJHqTBO9sw=; b=OCOs+Toc6Zcr89I8P6fgdkouO225He7DaJ2RFuV8kM+kd1GIFL14/6GQmf3+q8Dqdk /fLHP9FxG1M9MkErU2RthU1buWKjdjghpM77TOqSAznrQ3APkKT/wqFygw5jnv9j5Kyn 83yg8scHJzBUfoFdh8OmSbRoLBmJw9nVM58CHlTrYD/NXTJVvzSoH20iojrWvqM/DnDR ZW+g8AtxjFTsXrEq2Ljn1aU9JMLQCpFE/SsszwEk8BqGTClbi+pQoy/HK2r59OXP1dl0 npTVpDEDh8y/Ek2tj2I7JBjPqfP4A/qvWD1MhUXWo2IBnUHCj04Ux+qfyuyuv1JL4gmg 1wrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695740638; x=1696345438; 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=dFmLYHjhKHshwcnMAhKMVILwqWg7P2jKMKJHqTBO9sw=; b=jJfkZdnaIRP1omniPvurm3AO8zKXGG6FGOVyV9xk1n/QQ6vWctbazdMAalbK5jn060 LW7mrugPbQ7Hz64Km+NwEYaaIB0NwWXYQxAJH/I+WCePD58ckrWHftN6w3nss96SC3sU Vmtl5+PYtFbO0Lf6GOABUkbGK4ZC/Mooxky3+AwvJbLsQ3fZkmBvfdyrp8urEGuIg5bF 2WOXr0wmt4U2zgh5tmhyz9HgcnJkCCp2a2iunk7jQYTQ4NUAPu/nSID7YttG0qQPJxhm aaaD+IbH1S8Bf+xkD4l4JMvOTC4HH1RJiI0JKU5LrpHWD45+2mplr0tqLntfm2qwowLr UzIg== X-Gm-Message-State: AOJu0YzHVLpuVIYGMYz7wksLlJzZSjoKEpVGIhO602qfy0iHVXOL3y2Z WBvWWvJF8N2JK/X/a85tfI3MpQ== X-Received: by 2002:a5d:6a8a:0:b0:31a:ea18:c516 with SMTP id s10-20020a5d6a8a000000b0031aea18c516mr7713743wru.3.1695740638606; Tue, 26 Sep 2023 08:03:58 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:999:a3a0:2b3d:6c70:9dbf:5ede]) by smtp.gmail.com with ESMTPSA id x11-20020a5d650b000000b00318147fd2d3sm14926060wru.41.2023.09.26.08.03.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:03:58 -0700 (PDT) From: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , Atish Patra , Andrew Jones , Evan Green , =?UTF-8?q?Bj=C3=B6rn=20Topel?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Ron Minnich , Daniel Maslowski Subject: [PATCH 7/7] riscv: add support for PR_SET_UNALIGN and PR_GET_UNALIGN Date: Tue, 26 Sep 2023 17:03:16 +0200 Message-Id: <20230926150316.1129648-8-cleger@rivosinc.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230926150316.1129648-1-cleger@rivosinc.com> References: <20230926150316.1129648-1-cleger@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 26 Sep 2023 08:04:51 -0700 (PDT) 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 --- arch/riscv/include/asm/processor.h | 9 +++++++++ arch/riscv/kernel/process.c | 18 ++++++++++++++++++ 2 files changed, 27 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); -- 2.40.1