From patchwork Tue Sep 26 15:03:10 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: 144998 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2109071vqu; Tue, 26 Sep 2023 11:20:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3SjB/m425CtNkHP8amg/EUIO4iOqi0YVkNN2TpVNpnuod30XDnHCNEddw7AiJtef+A3DW X-Received: by 2002:a05:6a00:180c:b0:68e:2c3a:8775 with SMTP id y12-20020a056a00180c00b0068e2c3a8775mr9243556pfa.33.1695752421793; Tue, 26 Sep 2023 11:20:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695752421; cv=none; d=google.com; s=arc-20160816; b=yX7kqbfCwcxByKsE6uf6k6GsEEISBfZnhUg9b8y3AkPlEe51E4yvtkvMNNGo5Sr2EB /RhUAZw8K2xzo1lXvkddA18UAbLQ6TLRI4YESJ0hInx6Capk4RNyaP5LHjDXiV9RHxej xcYaDC8ik7P48jCULxRuQOGirUJHSXEKkVSNtao+d0qRtSaLukow1hpyyopr4lYM3m6u hSlB3afYHPBO9NHYHB2jpEUKgHJG0DCEA/JlF4FmF9EX/Th4TlTStEKvWfGfJs5UQ6SI UIwYlLo/vaHSMLN/AuDjpypz4O6wuuEXyrIOOLEOILxHXNZSTnPQiZaYN6R15QntysuA AX9w== 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=XQDAxN5g1vMcAJWLoIEgGtbs3vH3NsC3JS3eiLLd1tc=; fh=eI1w0vL8wMQYq+89KHAVQG8zXClfMmqUQ4ir9T7VotM=; b=z9dZ07NCbFmlv35UDU/INyiU4qil2coiIyURbsMYNd3V8BNbBow/HJa5rSFm1OgQcR N4w0fyaLAY649IglvrjCIFB8DT9MKKWCK9YiAizSa5T62cTunxMyGt26dnrF7awUtBwF jDHE7028l316DeQuecAXs03ik7g7cT3cRpuAzXmM7DXupSy8foZapzeAPwlKWjPioSr2 ypmEfL3IKyDzKVq7YNs5GO14/i/0s+QoYVECIdLW7RIjk3zZqfu/tw4yIBZD9DCFUG0r 1wnJEBjyPCA1smOi3e+pi7gampO0EDA/1Y8nfE/SfK0C5nEiEeSVZ8Q16omEBOGzZoOR 3ZBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RSu2dlzS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id cd12-20020a056a00420c00b00690a7aefa50si13033107pfb.325.2023.09.26.11.20.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 11:20:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=RSu2dlzS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 929C383B009B; Tue, 26 Sep 2023 08:04:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235003AbjIZPEF (ORCPT + 28 others); Tue, 26 Sep 2023 11:04:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232070AbjIZPEB (ORCPT ); Tue, 26 Sep 2023 11:04:01 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38B7C10E for ; Tue, 26 Sep 2023 08:03:54 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-3200b181b67so1192065f8f.0 for ; Tue, 26 Sep 2023 08:03:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695740632; x=1696345432; 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=XQDAxN5g1vMcAJWLoIEgGtbs3vH3NsC3JS3eiLLd1tc=; b=RSu2dlzSLD/MQbHRXs/I7tNPqUsZIF4qAWabMji12OIWw1ls8Th9dnbWmSBGZrj5Bm /IB+CXnhMk6tBmfylXPjM5MDd77T1fnYghsehbVWjN/iioPqMcIcup7OYXr/l4WXpAM0 I7A9P3ZR1Fe1bsv33FnFXHXIcBCfIIo6s4sr2CIm+mg3pOeaKSoVf51gPsRNk7GFlDC3 IN47s+g5kNiZMvK0qJkAzTFHty/i2HfBtsZlJbezzHqV5tFHjN7dqvAEue5ti5dggiuW MKzVB20JM+aE54eP4bHOeuWwKvHfmQPQ15+OilLLABdvS6ioqC7G5d/5pxYUaOx928No ha0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695740632; x=1696345432; 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=XQDAxN5g1vMcAJWLoIEgGtbs3vH3NsC3JS3eiLLd1tc=; b=W+1+9vGbHgCO7Na/mf/sdA90dvDkE4Y/uciqMvcy6ig1Ss7dz4mmJ/As6KdNDIawe9 XaamRdlpluFqezBCOATZyENw2tm33eLqMrq3krahrbuuQhKbI617KmOVvBcTjZdvV5r9 5GclXlE/VaIlKfJCWUf1PB6CzoVfdgevlqM509j59FK5VYlEnr93+bhiahDqBStCcDZu jZ/oN2zFkt+VtoNToroxmWhgj4IDgki4dtVZjqUHYMT38lffktyjREexobFRp+C1yG2p dCaK0LwzSSFwNk+CxiJeLld8Ma/qszv2/GBzZDUPnIeUqUSckWtBbAVE3gBUosXJhNHZ QZlQ== X-Gm-Message-State: AOJu0Yy1IFYOq/hZpJcMK9VNCAlO6HcnbGThhj7g0EcD05lbACSWub2e T/XCenNMObDZlkL4vpk4yEf+ew== X-Received: by 2002:adf:a456:0:b0:323:2df9:618f with SMTP id e22-20020adfa456000000b003232df9618fmr3634053wra.0.1695740632703; Tue, 26 Sep 2023 08:03:52 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:03:52 -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 Subject: [PATCH 1/7] riscv: remove unused functions in traps_misaligned.c Date: Tue, 26 Sep 2023 17:03:10 +0200 Message-Id: <20230926150316.1129648-2-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 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 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 26 Sep 2023 08:04:09 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778125291093968010 X-GMAIL-MSGID: 1778125291093968010 Replace macros by the only two function calls that are done from this file, store_u8() and load_u8(). Signed-off-by: Clément Léger --- arch/riscv/kernel/traps_misaligned.c | 46 +++++----------------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index 378f5b151443..e7bfb33089c1 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -151,51 +151,19 @@ #define PRECISION_S 0 #define PRECISION_D 1 -#define DECLARE_UNPRIVILEGED_LOAD_FUNCTION(type, insn) \ -static inline type load_##type(const type *addr) \ -{ \ - type val; \ - asm (#insn " %0, %1" \ - : "=&r" (val) : "m" (*addr)); \ - return val; \ -} +static inline u8 load_u8(const u8 *addr) +{ + u8 val; -#define DECLARE_UNPRIVILEGED_STORE_FUNCTION(type, insn) \ -static inline void store_##type(type *addr, type val) \ -{ \ - asm volatile (#insn " %0, %1\n" \ - : : "r" (val), "m" (*addr)); \ -} + asm volatile("lbu %0, %1" : "=&r" (val) : "m" (*addr)); -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u8, lbu) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u16, lhu) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s8, lb) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s16, lh) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(s32, lw) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u8, sb) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u16, sh) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u32, sw) -#if defined(CONFIG_64BIT) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u32, lwu) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u64, ld) -DECLARE_UNPRIVILEGED_STORE_FUNCTION(u64, sd) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(ulong, ld) -#else -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(u32, lw) -DECLARE_UNPRIVILEGED_LOAD_FUNCTION(ulong, lw) - -static inline u64 load_u64(const u64 *addr) -{ - return load_u32((u32 *)addr) - + ((u64)load_u32((u32 *)addr + 1) << 32); + return val; } -static inline void store_u64(u64 *addr, u64 val) +static inline void store_u8(u8 *addr, u8 val) { - store_u32((u32 *)addr, val); - store_u32((u32 *)addr + 1, val >> 32); + asm volatile ("sb %0, %1\n" : : "r" (val), "m" (*addr)); } -#endif static inline ulong get_insn(ulong mepc) { From patchwork Tue Sep 26 15:03:11 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: 144936 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp1992951vqu; Tue, 26 Sep 2023 08:17:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFxjLGDDAboi3i+e/wW7CFyd/2lrOKF/hwPFghTl/M2/GPSsqLEnEKY91YeKeRt46X66sNZ X-Received: by 2002:a05:6358:591a:b0:143:9bc0:a975 with SMTP id g26-20020a056358591a00b001439bc0a975mr12863632rwf.7.1695741443510; Tue, 26 Sep 2023 08:17:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695741443; cv=none; d=google.com; s=arc-20160816; b=izSqFqtXDTGz8RHUYQiMgqZcXQ2SsxeJHnPU5rxIRlvwv+selsY/JQzlDqesGMGPVA 1OkmU/SpNW7iBae0sMP0aOi2T2Hk2vbnAPkybAWh3x/NjXiWcSplE98bboI0F6n30WUh 3OJscE9LVB9ZNG3RDnKkEXJ5erE/6SP5BiZc4nKWeTHKEVNvKXfw0/puZ1Ffjh3/bliN TjYtYSivgl3dxnOCqQdajojLZxpkYXougzwfrKfi1B7hZ8+Oaui43qA/yZFUlq2F0svO R0v/8NxHcLNN90IFACYfeJoLLE5SMxDzk44vq1aYd2AdMy2mXOEUFL9sxSAFi0/w6WB1 wr8g== 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=BMCaPvwZIrQsZFzVwV8aZpdb+qmaO/RzFW3CFL/diT8=; fh=eI1w0vL8wMQYq+89KHAVQG8zXClfMmqUQ4ir9T7VotM=; b=iXsoHnTzrs7JnrfSNTmg9A2+9vu6Dje1Gdks4vpAsXjLHNiAf0S9Djh9TEbgQ7D39m FUb+cosC4cNnItLQnH2Ut9AWel1IxixOlpu3KX5VezPLrwndOLcTiWyGduhx90hhZbKl Q2Gac2aTmRBkGN2D9P1p1TbU2obYNKN/mDj7OWZrDWc6n9TAyJKRYV9SgnatvTHhduYC l/ub6MeBCkiLxxDKAzWKUN6oQ3MNu+S55lApp0K2Vp2g/+ZBj0w+QzLkOj4i5CdPGph4 B3rj0IR0GB2WHqghMk9/F/xu/UjFMQ0vXT9BWa+bYJmOrCJDH0Ns9MLyJbTnC9hv4+w4 mKhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=KHJP7mQ8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id e4-20020a635444000000b00573f7d09445si12789454pgm.330.2023.09.26.08.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:17:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=KHJP7mQ8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 48E5A83B6BD0; Tue, 26 Sep 2023 08:04:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235029AbjIZPEJ (ORCPT + 28 others); Tue, 26 Sep 2023 11:04:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234878AbjIZPED (ORCPT ); Tue, 26 Sep 2023 11:04:03 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 730BC11F for ; Tue, 26 Sep 2023 08:03:55 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4047ef37f55so25470145e9.1 for ; Tue, 26 Sep 2023 08:03:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695740634; x=1696345434; 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=BMCaPvwZIrQsZFzVwV8aZpdb+qmaO/RzFW3CFL/diT8=; b=KHJP7mQ8XrDCX9sn41SFqFoOTAIO3VMIUUF2mdaTtrstH26iw3Zv5Mi5/ryaL/MMUi t8TurkiR9IlnAcXAwOPfPer8KJnq07fwp71EFkAq+sOsfK8ySPjqB/VdAY7wQc/SaVOo lBKnFgfySxMzNItCOCqAhIO1yuV9d1JP1FriOC5CL5SxorVkIi648xspjqSqOTzLZKPn 1vrjTpVct8L6YjVOPOgOyxl8Dn1OZt6ZKqFHo22DV7lNwcNJTccSLIgbSHJ5aU39/ZaD iV1mWK5hD4a6G0PFs6cU2br4Y3hkpy0Hr63rZUfL/p4nLpDF/P8W7ZqR5CD3PFaxtNOk cXng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695740634; x=1696345434; 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=BMCaPvwZIrQsZFzVwV8aZpdb+qmaO/RzFW3CFL/diT8=; b=aYHEUZNEXU1ixkn0yVTr4AhtFildCTZvx89+VOQhnJfQEAqe50ylOdsM3d+0BtHxip 4prvMtcWpxnIe628ZyzSWLJzDEGjG1XHtKtf5QR69qsQUQ7/J+G1osgJ9RxXzONGDJhO kppwBMIQ+OVvxcfoTOhNzliaSHzfZMJ+7nRI7Jvazg5KPoCD60lBo/TdJL2zKCW34zs5 70MrzjdntLuCfszWt1oQ73AytiAezY7Tn5q6RvQA+qmag/+rLd1/ft4t2yKgt6TV532i XTmGp2WNqgOKJYsGgETp6i8FU6lOQ16/mYv160m1P07iU1Qndtw46CNwBpO8nFPs3OjK EWxw== X-Gm-Message-State: AOJu0YxhBXivupayO0dnmmCebnV9tpSzj5wl9EJ9pti+4obTSBscWFDC UE4/pN/lfsORV2Q4PEXihb1LGQ== X-Received: by 2002:a5d:584a:0:b0:323:2b68:e0e0 with SMTP id i10-20020a5d584a000000b003232b68e0e0mr5172766wrf.0.1695740633754; Tue, 26 Sep 2023 08:03:53 -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.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:03:53 -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 Subject: [PATCH 2/7] riscv: add support for misaligned handling in S-mode Date: Tue, 26 Sep 2023 17:03:11 +0200 Message-Id: <20230926150316.1129648-3-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 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 26 Sep 2023 08:04:26 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778113779656412599 X-GMAIL-MSGID: 1778113779656412599 Misalignment handling is only supported for M-mode and uses direct accesses to user memory. In S-mode, when handlnig usermode fault, this requires to use the get_user()/put_user() accessors. Implement load_u8(), store_u8() and get_insn() using these accessors. Signed-off-by: Clément Léger --- arch/riscv/include/asm/entry-common.h | 3 + arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/traps.c | 9 -- arch/riscv/kernel/traps_misaligned.c | 119 +++++++++++++++++++++++--- 4 files changed, 110 insertions(+), 23 deletions(-) diff --git a/arch/riscv/include/asm/entry-common.h b/arch/riscv/include/asm/entry-common.h index 6e4dee49d84b..58e9e2976e1b 100644 --- a/arch/riscv/include/asm/entry-common.h +++ b/arch/riscv/include/asm/entry-common.h @@ -8,4 +8,7 @@ void handle_page_fault(struct pt_regs *regs); void handle_break(struct pt_regs *regs); +int handle_misaligned_load(struct pt_regs *regs); +int handle_misaligned_store(struct pt_regs *regs); + #endif /* _ASM_RISCV_ENTRY_COMMON_H */ diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 95cf25d48405..ccdfd029c511 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -56,10 +56,10 @@ obj-y += riscv_ksyms.o obj-y += stacktrace.o obj-y += cacheinfo.o obj-y += patch.o +obj-y += traps_misaligned.o obj-y += probes/ obj-$(CONFIG_MMU) += vdso.o vdso/ -obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o obj-$(CONFIG_SMP) += smpboot.o diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 19807c4d3805..d69779e4b967 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -179,14 +179,6 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re DO_ERROR_INFO(do_trap_load_fault, SIGSEGV, SEGV_ACCERR, "load access fault"); -#ifndef CONFIG_RISCV_M_MODE -DO_ERROR_INFO(do_trap_load_misaligned, - SIGBUS, BUS_ADRALN, "Oops - load address misaligned"); -DO_ERROR_INFO(do_trap_store_misaligned, - SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned"); -#else -int handle_misaligned_load(struct pt_regs *regs); -int handle_misaligned_store(struct pt_regs *regs); asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs) { @@ -229,7 +221,6 @@ asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs irqentry_nmi_exit(regs, state); } } -#endif DO_ERROR_INFO(do_trap_store_fault, SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault"); DO_ERROR_INFO(do_trap_ecall_s, diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index e7bfb33089c1..9daed7d756ae 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -12,6 +12,7 @@ #include #include #include +#include #define INSN_MATCH_LB 0x3 #define INSN_MASK_LB 0x707f @@ -151,21 +152,25 @@ #define PRECISION_S 0 #define PRECISION_D 1 -static inline u8 load_u8(const u8 *addr) +#ifdef CONFIG_RISCV_M_MODE +static inline int load_u8(struct pt_regs *regs, const u8 *addr, u8 *r_val) { u8 val; asm volatile("lbu %0, %1" : "=&r" (val) : "m" (*addr)); + *r_val = val; - return val; + return 0; } -static inline void store_u8(u8 *addr, u8 val) +static inline int store_u8(struct pt_regs *regs, u8 *addr, u8 val) { asm volatile ("sb %0, %1\n" : : "r" (val), "m" (*addr)); + + return 0; } -static inline ulong get_insn(ulong mepc) +static inline int get_insn(struct pt_regs *regs, ulong mepc, ulong *r_insn) { register ulong __mepc asm ("a2") = mepc; ulong val, rvc_mask = 3, tmp; @@ -194,9 +199,87 @@ static inline ulong get_insn(ulong mepc) : [addr] "r" (__mepc), [rvc_mask] "r" (rvc_mask), [xlen_minus_16] "i" (XLEN_MINUS_16)); - return val; + *r_insn = val; + + return 0; +} +#else +static inline int load_u8(struct pt_regs *regs, const u8 *addr, u8 *r_val) +{ + if (user_mode(regs)) { + return __get_user(*r_val, addr); + } else { + *r_val = *addr; + return 0; + } } +static inline int store_u8(struct pt_regs *regs, u8 *addr, u8 val) +{ + if (user_mode(regs)) { + return __put_user(val, addr); + } else { + *addr = val; + return 0; + } +} + +#define __read_insn(regs, insn, insn_addr) \ +({ \ + int __ret; \ + \ + if (user_mode(regs)) { \ + __ret = __get_user(insn, insn_addr); \ + } else { \ + insn = *insn_addr; \ + __ret = 0; \ + } \ + \ + __ret; \ +}) + +static inline int get_insn(struct pt_regs *regs, ulong epc, ulong *r_insn) +{ + ulong insn = 0; + + if (epc & 0x2) { + ulong tmp = 0; + u16 __user *insn_addr = (u16 __user *)epc; + + if (__read_insn(regs, insn, insn_addr)) + return -EFAULT; + /* __get_user() uses regular "lw" which sign extend the loaded + * value make sure to clear higher order bits in case we "or" it + * below with the upper 16 bits half. + */ + insn &= GENMASK(15, 0); + if ((insn & __INSN_LENGTH_MASK) != __INSN_LENGTH_32) { + *r_insn = insn; + return 0; + } + insn_addr++; + if (__read_insn(regs, tmp, insn_addr)) + return -EFAULT; + *r_insn = (tmp << 16) | insn; + + return 0; + } else { + u32 __user *insn_addr = (u32 __user *)epc; + + if (__read_insn(regs, insn, insn_addr)) + return -EFAULT; + if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32) { + *r_insn = insn; + return 0; + } + insn &= GENMASK(15, 0); + *r_insn = insn; + + return 0; + } +} +#endif + union reg_data { u8 data_bytes[8]; ulong data_ulong; @@ -207,10 +290,13 @@ int handle_misaligned_load(struct pt_regs *regs) { union reg_data val; unsigned long epc = regs->epc; - unsigned long insn = get_insn(epc); - unsigned long addr = csr_read(mtval); + unsigned long insn; + unsigned long addr = regs->badaddr; int i, fp = 0, shift = 0, len = 0; + if (get_insn(regs, epc, &insn)) + return -1; + regs->epc = 0; if ((insn & INSN_MASK_LW) == INSN_MATCH_LW) { @@ -274,8 +360,10 @@ int handle_misaligned_load(struct pt_regs *regs) } val.data_u64 = 0; - for (i = 0; i < len; i++) - val.data_bytes[i] = load_u8((void *)(addr + i)); + for (i = 0; i < len; i++) { + if (load_u8(regs, (void *)(addr + i), &val.data_bytes[i])) + return -1; + } if (fp) return -1; @@ -290,10 +378,13 @@ int handle_misaligned_store(struct pt_regs *regs) { union reg_data val; unsigned long epc = regs->epc; - unsigned long insn = get_insn(epc); - unsigned long addr = csr_read(mtval); + unsigned long insn; + unsigned long addr = regs->badaddr; int i, len = 0; + if (get_insn(regs, epc, &insn)) + return -1; + regs->epc = 0; val.data_ulong = GET_RS2(insn, regs); @@ -327,8 +418,10 @@ int handle_misaligned_store(struct pt_regs *regs) return -1; } - for (i = 0; i < len; i++) - store_u8((void *)(addr + i), val.data_bytes[i]); + for (i = 0; i < len; i++) { + if (store_u8(regs, (void *)(addr + i), val.data_bytes[i])) + return -1; + } regs->epc = epc + INSN_LEN(insn); From patchwork Tue Sep 26 15:03:12 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: 145066 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2263279vqu; Tue, 26 Sep 2023 16:42:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGsjLfTK1E2AtYTEmhcsSnVAhWTZ++Bmc5JVEmTJvrCUeyIyr9Uvp9qKR02lrnIp8gb6cX2 X-Received: by 2002:a05:6a00:984:b0:690:d413:ee0f with SMTP id u4-20020a056a00098400b00690d413ee0fmr542546pfg.2.1695771759099; Tue, 26 Sep 2023 16:42:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695771759; cv=none; d=google.com; s=arc-20160816; b=C5Nbh/dJesxDS2MWKUHB2vegyAhw4jsev0XGNwn2q0+HZGLqwPHoh8IIWbhbfM4Cti JQeo5R/nI4C+MQ6sSAe7K6PYocx7JDuxlVtCBp5adsFJxVAOW5wpgAEeAfwsKqT/P+0X ZCB7G0LPRWi1cTY7jK1PpG0L1k1XN/I9a4t8Rr9uBavA83Hk3hmrbhw5uY6Wu5j8bcYW VbYEO8Gkrty0yIl0mJwj9i0PtuGzTxpO8idLGB64tnXle6LS7Stf9HiAWJ1znSf/kU9D 2763ugAKujQKPo8Zx92F3SznJ3xZ3IrYX9eP7XdYcMZ7blSNR3v2Oicu6JWNFYI8oreN +wpg== 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=0nyDpPdQazxwk2pvs7+quB1sTF8r3wWKIala/GUDnoY=; fh=eI1w0vL8wMQYq+89KHAVQG8zXClfMmqUQ4ir9T7VotM=; b=hRdWuI6OYGp4NO3+sHSIcdxtNFSFAfkJMT3ScERIC+lqJbe0Jmp/HaR9rRsANYmzzy 3xd0+CJNGp/cI5RqShrwdWyN7J9D1sKChGr7i7luhW3eEexknehkEj5Wo4CzlDaVeed3 WKCkAMSgljq+zAZ3+BKXsmCn+a2ZwkC176jLZ/yTFDmFPqvRAOfYFWkQ0p0/gVbApd9Z HzL2vuI6oF97lXmYvMpOqb0LJ4sLn+m3mzyRAD7RXI+KMHnqvvvq1e7Etg4uitGm1rba JjmseyCfksS5zpG+XrHpNzrwoT/scb6KeUw/OePmW+Ssu8Mo5mKAoAZLm3hApBUuMTtX CVaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iQLtiJWi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id o7-20020a056a001bc700b00690bc199287si13388904pfw.393.2023.09.26.16.42.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 16:42:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=iQLtiJWi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id A049A83B00AB; Tue, 26 Sep 2023 08:04:14 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235024AbjIZPEH (ORCPT + 28 others); Tue, 26 Sep 2023 11:04:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234710AbjIZPEC (ORCPT ); Tue, 26 Sep 2023 11:04:02 -0400 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 400C3116 for ; Tue, 26 Sep 2023 08:03:56 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40471c054f9so26829005e9.0 for ; Tue, 26 Sep 2023 08:03:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695740635; x=1696345435; 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=0nyDpPdQazxwk2pvs7+quB1sTF8r3wWKIala/GUDnoY=; b=iQLtiJWikzzMfUnX3aBxlqJxZO34Oq4/0eY/4zy67P0ae538nhd3lqfHSMLJchldeK 6n3PW+U4oOlKeEViXFYxh5ZUEYM2usI5M0eNEKFEZDinKouPCi5RYHGmDwO90vHLqXiU wJqfOI8WZSQ204TPtgtXqGRtN/AsPYi1KbzDLeHGOZODi0UnwPyrjcjrEoLRwRw01nID ImKqw1BsTT4/zSIqlQZuEpbqBrv3ppcWmv1EBsv6eVHAT89LCMU/JYfiyTx72i2TIeeA r3R1T3aio4/TENIvKRyTmPjEHUntHoHtPy8K/3JyWjEU6HadoF9bqonMDkp2WYssA77j yG1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695740635; x=1696345435; 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=0nyDpPdQazxwk2pvs7+quB1sTF8r3wWKIala/GUDnoY=; b=EXRs2uNRqTy2MniqwjHv85MGCm9VtXF3u7jU/DboljUF24kZE7KvVn4O6mtI8Pt5Q0 /mANJUkCaAuo+yqb8WZCNwIzehlhr4O8LubsVxJejX+hpFu97QB9zZo7c1VdVDRMs8XS ySOauW7Z9Gdqv83FmfcVGtGMpW0uGqzg8l5LfV+ke/Q72JGnIhLfbzZ+5R+r4SfprHjl WfSNRI0nMw3VO1hGtcOwribRRB7VCh36rSa1fmcF16mTK4YY14mVonEjgaIhMgmaNiyn iwjXY6y4Yjlwsj8pV94Y9akRo6Z9E3tNXk6jGQ84hsXXgCwHgJUdxx0cJ12QENW4uiUq ncjg== X-Gm-Message-State: AOJu0YyTsiLFMIGcvMLAutj8EWxvY2/MXLKDXA38Q4j4rrOnjiUrtbXZ b+NG6lxFt2ZURovys7QHcxnfOw== X-Received: by 2002:a5d:595e:0:b0:31f:899b:a47 with SMTP id e30-20020a5d595e000000b0031f899b0a47mr8497274wri.4.1695740634745; Tue, 26 Sep 2023 08:03:54 -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.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:03:54 -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 Subject: [PATCH 3/7] riscv: report perf event for misaligned fault Date: Tue, 26 Sep 2023 17:03:12 +0200 Message-Id: <20230926150316.1129648-4-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 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 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 26 Sep 2023 08:04:14 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778145567947863961 X-GMAIL-MSGID: 1778145567947863961 Add missing calls to account for misaligned fault event using perf_sw_event(). Signed-off-by: Clément Léger --- arch/riscv/kernel/traps_misaligned.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index 9daed7d756ae..804f6c5e0e44 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -294,6 +295,8 @@ int handle_misaligned_load(struct pt_regs *regs) unsigned long addr = regs->badaddr; int i, fp = 0, shift = 0, len = 0; + perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr); + if (get_insn(regs, epc, &insn)) return -1; @@ -382,6 +385,8 @@ int handle_misaligned_store(struct pt_regs *regs) unsigned long addr = regs->badaddr; int i, len = 0; + perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr); + if (get_insn(regs, epc, &insn)) return -1; From patchwork Tue Sep 26 15:03:13 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: 145020 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2157772vqu; Tue, 26 Sep 2023 12:56:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJOSnfQTd8fX43psYGGcYGygKbZdxIgt2vIv3lXZLJfUJQQS0OZ6dulXjXl/CpJbjAzVjs X-Received: by 2002:a05:6808:1493:b0:3ad:fc05:cfb8 with SMTP id e19-20020a056808149300b003adfc05cfb8mr1724oiw.28.1695758178960; Tue, 26 Sep 2023 12:56:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695758178; cv=none; d=google.com; s=arc-20160816; b=NdoxRzsFK0tF9yKRjZrXrjZdJDN0Cr/DsQOyl/b6qvyBjieWMDsNy/7RWLqqjaVKwu FLaELhIGftcKXOcQF8/IWJVcsuBPSVgk69MWzl2jdMlZX5wHhvUB9Ue/CWtX3MwWUY3O TpGfshZBMiLRoVxjw01Oicj8YnCHG/mh/dJmhB6UczRqudyVmr3jUmPYTmtuNGko1jzg nC1U12ALB4vvOoB435ew7Fk/yOHW2O57rMyGlaC4GKySVd5fng10FMnxZWlWm1izRTaD B0JdgLdRu6g4WTx9ps9aw65AudiGzGrDCz7KTtKlWq2FIZ9PHZ1CdyKWap1h3cpVPrcF RJ9A== 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=/uuvfJ5AANk2b1H7qXwiqMjtZ5fx5LwQZGgrS2bH6zQ=; fh=eI1w0vL8wMQYq+89KHAVQG8zXClfMmqUQ4ir9T7VotM=; b=e5UBFJgD9gdEKdVgbtORzBkviHF/HQAWETXF9BrF5BBkMKr/tvSEyearftitbl1VwX mfmYn6WolV00buc5IlCgO3d5G7lA6k8n09wHxnxNsP1chgziZ7kdn2HDUqPON51xmrm1 lJzF6w4FNSNZeyPMms55W1ob+vAQ0wYKwZeTEs9VObXmVm4KcENC+TmNQJ4nfYArY9Kr J0oZIcw8HdELDtDT9SIZZk1xm1bykB0XkSV3whaETYif+IGFjd0aDIvJaWOtC9DwVszN UOC3FB+cX5+KMinIL3aF4st6GmYDn0bjyUwf3rtat1rwCJcIyFIsgxm396QALAb2wbgW gNOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=IvpG+fJb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id h123-20020a636c81000000b005780a1eff9csi12880044pgc.444.2023.09.26.12.56.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 12:56:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=IvpG+fJb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 745BB81D3AFC; Tue, 26 Sep 2023 08:04:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235055AbjIZPEO (ORCPT + 28 others); Tue, 26 Sep 2023 11:04:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234991AbjIZPEE (ORCPT ); Tue, 26 Sep 2023 11:04:04 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6022210E for ; Tue, 26 Sep 2023 08:03:57 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-32163c3ece5so1120017f8f.1 for ; Tue, 26 Sep 2023 08:03:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695740636; x=1696345436; 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=/uuvfJ5AANk2b1H7qXwiqMjtZ5fx5LwQZGgrS2bH6zQ=; b=IvpG+fJbcpybBzYIchFjWpENMuIq3Vx6KOoTvLMPxnr5iL1A7Zn6JQI47IWz0KtEjF rpow7Mz0NFNh95y3GcAWUoaseXtO6RoF2KGcMxppbx40JcOwrdWCI4DKoC7BEP3ETuPp rtatnnjn4BNBN5xiematBqXKl3f6NXzaSX1FfTQYaTIoRshvB5pIxfNxSNQm0r+T0OVs 6RrALLyHrQgHGLkXE0qRsPsahTIHvrnu1pubMDl/LLgs73EANRrj6rAv+eW2N94hbu6a 4DBr2RppX45l02lOoDfzkmU/g7FMbTAm6oPtEg7zziACdVR9OGIl+Gg8gM47E4DCj/YF f5VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695740636; x=1696345436; 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=/uuvfJ5AANk2b1H7qXwiqMjtZ5fx5LwQZGgrS2bH6zQ=; b=AkREh6fLpIJJhG8sh6iY2AgaN1f9GDi/MisL9iIjuQL/NPjs7dQIlJREWJfftJCoNk llEexLa+B4+eZsVKuLneZ47K6y/aHeMegNeMEqA65q2lB/MA4j9tukNWKR0cuk4pvucO 3V+CwidZW1w8xMuwydc0F2FTb++dRJhPLuAnLSlqtyb14tjOlTCWIVnCUBdU7QmoFp4W +VSTnSwYsY5L3OT6jlQ/W2RxpwaE/P+ukwowDBtumDdLizIDrtiaEA3R8Tln7a0kMSlU 0Na3gNXj64KbhEoYXPL/rwhXeQlOWs64LfKlUobVX7Xg5TTamU1A8SVAdoq8BeaZnFfG kEcQ== X-Gm-Message-State: AOJu0YyA1yKO1mX5e4aT1JvLF8xwojZhNxtRnag/80fCCNtV3wKLBPhM EQCdk8yyF0e8e5ww6cpHwvw4Fg== X-Received: by 2002:adf:ed84:0:b0:322:c494:d481 with SMTP id c4-20020adfed84000000b00322c494d481mr9917893wro.0.1695740635757; Tue, 26 Sep 2023 08:03:55 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:03:55 -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 Subject: [PATCH 4/7] riscv: add floating point insn support to misaligned access emulation Date: Tue, 26 Sep 2023 17:03:13 +0200 Message-Id: <20230926150316.1129648-5-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 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 agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 26 Sep 2023 08:04:58 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778131328536894367 X-GMAIL-MSGID: 1778131328536894367 This support is partially based of openSBI misaligned emulation floating point instruction support. It provides support for the existing floating point instructions (both for 32/64 bits as well as compressed ones). Since floating point registers are not part of the pt_regs struct, we need to modify them directly using some assembly. We also dirty the pt_regs status in case we modify them to be sure context switch will save FP state. With this support, Linux is on par with openSBI support. Signed-off-by: Clément Léger --- arch/riscv/kernel/fpu.S | 117 +++++++++++++++++++++ arch/riscv/kernel/traps_misaligned.c | 152 ++++++++++++++++++++++++++- 2 files changed, 265 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kernel/fpu.S b/arch/riscv/kernel/fpu.S index dd2205473de7..2785badb247c 100644 --- a/arch/riscv/kernel/fpu.S +++ b/arch/riscv/kernel/fpu.S @@ -104,3 +104,120 @@ ENTRY(__fstate_restore) csrc CSR_STATUS, t1 ret ENDPROC(__fstate_restore) + +#define get_f32(which) fmv.x.s a0, which; j 2f +#define put_f32(which) fmv.s.x which, a1; j 2f +#if __riscv_xlen == 64 +# define get_f64(which) fmv.x.d a0, which; j 2f +# define put_f64(which) fmv.d.x which, a1; j 2f +#else +# define get_f64(which) fsd which, 0(a1); j 2f +# define put_f64(which) fld which, 0(a1); j 2f +#endif + +.macro fp_access_prologue + /* + * Compute jump offset to store the correct FP register since we don't + * have indirect FP register access + */ + sll t0, a0, 3 + la t2, 1f + add t0, t0, t2 + li t1, SR_FS + csrs CSR_STATUS, t1 + jr t0 +1: +.endm + +.macro fp_access_epilogue +2: + csrc CSR_STATUS, t1 + ret +.endm + +#define fp_access_body(__access_func) \ + __access_func(f0); \ + __access_func(f1); \ + __access_func(f2); \ + __access_func(f3); \ + __access_func(f4); \ + __access_func(f5); \ + __access_func(f6); \ + __access_func(f7); \ + __access_func(f8); \ + __access_func(f9); \ + __access_func(f10); \ + __access_func(f11); \ + __access_func(f12); \ + __access_func(f13); \ + __access_func(f14); \ + __access_func(f15); \ + __access_func(f16); \ + __access_func(f17); \ + __access_func(f18); \ + __access_func(f19); \ + __access_func(f20); \ + __access_func(f21); \ + __access_func(f22); \ + __access_func(f23); \ + __access_func(f24); \ + __access_func(f25); \ + __access_func(f26); \ + __access_func(f27); \ + __access_func(f28); \ + __access_func(f29); \ + __access_func(f30); \ + __access_func(f31) + + +/* + * Disable compressed instructions set to keep a constant offset between FP + * load/store/move instructions + */ +.option norvc +/* + * put_f32_reg - Set a FP register from a register containing the value + * a0 = FP register index to be set + * a1 = value to be loaded in the FP register + */ +SYM_FUNC_START(put_f32_reg) + fp_access_prologue + fp_access_body(put_f32) + fp_access_epilogue +SYM_FUNC_END(put_f32_reg) + +/* + * get_f32_reg - Get a FP register value and return it + * a0 = FP register index to be retrieved + */ +SYM_FUNC_START(get_f32_reg) + fp_access_prologue + fp_access_body(get_f32) + fp_access_epilogue +SYM_FUNC_END(put_f32_reg) + +/* + * put_f64_reg - Set a 64 bits FP register from a value or a pointer. + * a0 = FP register index to be set + * a1 = value/pointer to be loaded in the FP register (when xlen == 32 bits, we + * load the value to a pointer). + */ +SYM_FUNC_START(put_f64_reg) + fp_access_prologue + fp_access_body(put_f64) + fp_access_epilogue +SYM_FUNC_END(put_f64_reg) + +/* + * put_f64_reg - Get a 64 bits FP register value and returned it or store it to + * a pointer. + * a0 = FP register index to be retrieved + * a1 = If xlen == 32, pointer which should be loaded with the FP register value + * or unused if xlen == 64. In which case the FP register value is returned + * through a0 + */ +SYM_FUNC_START(get_f64_reg) + fp_access_prologue + fp_access_body(get_f64) + fp_access_epilogue +SYM_FUNC_END(get_f64_reg) diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index 804f6c5e0e44..041fd2dbd955 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -153,6 +153,115 @@ #define PRECISION_S 0 #define PRECISION_D 1 +#ifdef CONFIG_FPU + +#define FP_GET_RD(insn) (insn >> 7 & 0x1F) + +extern void put_f32_reg(unsigned long fp_reg, unsigned long value); + +static int set_f32_rd(unsigned long insn, struct pt_regs *regs, + unsigned long val) +{ + unsigned long fp_reg = FP_GET_RD(insn); + + put_f32_reg(fp_reg, val); + regs->status |= SR_FS_DIRTY; + + return 0; +} + +extern void put_f64_reg(unsigned long fp_reg, unsigned long value); + +static int set_f64_rd(unsigned long insn, struct pt_regs *regs, u64 val) +{ + unsigned long fp_reg = FP_GET_RD(insn); + unsigned long value; + +#if __riscv_xlen == 32 + value = (unsigned long) &val; +#else + value = val; +#endif + put_f64_reg(fp_reg, value); + regs->status |= SR_FS_DIRTY; + + return 0; +} + +#if __riscv_xlen == 32 +extern void get_f64_reg(unsigned long fp_reg, u64 *value); + +static u64 get_f64_rs(unsigned long insn, u8 fp_reg_offset, + struct pt_regs *regs) +{ + unsigned long fp_reg = (insn >> fp_reg_offset) & 0x1F; + u64 val; + + get_f64_reg(fp_reg, &val); + regs->status |= SR_FS_DIRTY; + + return val; +} +#else + +extern unsigned long get_f64_reg(unsigned long fp_reg); + +static unsigned long get_f64_rs(unsigned long insn, u8 fp_reg_offset, + struct pt_regs *regs) +{ + unsigned long fp_reg = (insn >> fp_reg_offset) & 0x1F; + unsigned long val; + + val = get_f64_reg(fp_reg); + regs->status |= SR_FS_DIRTY; + + return val; +} + +#endif + +extern unsigned long get_f32_reg(unsigned long fp_reg); + +static unsigned long get_f32_rs(unsigned long insn, u8 fp_reg_offset, + struct pt_regs *regs) +{ + unsigned long fp_reg = (insn >> fp_reg_offset) & 0x1F; + unsigned long val; + + val = get_f32_reg(fp_reg); + regs->status |= SR_FS_DIRTY; + + return val; +} + +#else /* CONFIG_FPU */ +static void set_f32_rd(unsigned long insn, struct pt_regs *regs, + unsigned long val) {} + +static void set_f64_rd(unsigned long insn, struct pt_regs *regs, u64 val) {} + +static unsigned long get_f64_rs(unsigned long insn, u8 fp_reg_offset, + struct pt_regs *regs) +{ + return 0; +} + +static unsigned long get_f32_rs(unsigned long insn, u8 fp_reg_offset, + struct pt_regs *regs) +{ + return 0; +} + +#endif + +#define GET_F64_RS2(insn, regs) (get_f64_rs(insn, 20, regs)) +#define GET_F64_RS2C(insn, regs) (get_f64_rs(insn, 2, regs)) +#define GET_F64_RS2S(insn, regs) (get_f64_rs(RVC_RS2S(insn), 0, regs)) + +#define GET_F32_RS2(insn, regs) (get_f32_rs(insn, 20, regs)) +#define GET_F32_RS2C(insn, regs) (get_f32_rs(insn, 2, regs)) +#define GET_F32_RS2S(insn, regs) (get_f32_rs(RVC_RS2S(insn), 0, regs)) + #ifdef CONFIG_RISCV_M_MODE static inline int load_u8(struct pt_regs *regs, const u8 *addr, u8 *r_val) { @@ -362,15 +471,21 @@ int handle_misaligned_load(struct pt_regs *regs) return -1; } + if (!IS_ENABLED(CONFIG_FPU) && fp) + return -EOPNOTSUPP; + val.data_u64 = 0; for (i = 0; i < len; i++) { if (load_u8(regs, (void *)(addr + i), &val.data_bytes[i])) return -1; } - if (fp) - return -1; - SET_RD(insn, regs, val.data_ulong << shift >> shift); + if (!fp) + SET_RD(insn, regs, val.data_ulong << shift >> shift); + else if (len == 8) + set_f64_rd(insn, regs, val.data_u64); + else + set_f32_rd(insn, regs, val.data_ulong); regs->epc = epc + INSN_LEN(insn); @@ -383,7 +498,7 @@ int handle_misaligned_store(struct pt_regs *regs) unsigned long epc = regs->epc; unsigned long insn; unsigned long addr = regs->badaddr; - int i, len = 0; + int i, len = 0, fp = 0; perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr); @@ -400,6 +515,14 @@ int handle_misaligned_store(struct pt_regs *regs) } else if ((insn & INSN_MASK_SD) == INSN_MATCH_SD) { len = 8; #endif + } else if ((insn & INSN_MASK_FSD) == INSN_MATCH_FSD) { + fp = 1; + len = 8; + val.data_u64 = GET_F64_RS2(insn, regs); + } else if ((insn & INSN_MASK_FSW) == INSN_MATCH_FSW) { + fp = 1; + len = 4; + val.data_ulong = GET_F32_RS2(insn, regs); } else if ((insn & INSN_MASK_SH) == INSN_MATCH_SH) { len = 2; #if defined(CONFIG_64BIT) @@ -418,11 +541,32 @@ int handle_misaligned_store(struct pt_regs *regs) ((insn >> SH_RD) & 0x1f)) { len = 4; val.data_ulong = GET_RS2C(insn, regs); + } else if ((insn & INSN_MASK_C_FSD) == INSN_MATCH_C_FSD) { + fp = 1; + len = 8; + val.data_u64 = GET_F64_RS2S(insn, regs); + } else if ((insn & INSN_MASK_C_FSDSP) == INSN_MATCH_C_FSDSP) { + fp = 1; + len = 8; + val.data_u64 = GET_F64_RS2C(insn, regs); +#if !defined(CONFIG_64BIT) + } else if ((insn & INSN_MASK_C_FSW) == INSN_MATCH_C_FSW) { + fp = 1; + len = 4; + val.data_ulong = GET_F32_RS2S(insn, regs); + } else if ((insn & INSN_MASK_C_FSWSP) == INSN_MATCH_C_FSWSP) { + fp = 1; + len = 4; + val.data_ulong = GET_F32_RS2C(insn, regs); +#endif } else { regs->epc = epc; return -1; } + if (!IS_ENABLED(CONFIG_FPU) && fp) + return -EOPNOTSUPP; + for (i = 0; i < len; i++) { if (store_u8(regs, (void *)(addr + i), val.data_bytes[i])) return -1; From patchwork Tue Sep 26 15:03:14 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: 145042 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2201406vqu; Tue, 26 Sep 2023 14:24:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHFwVFoVG0zoPFjF3FtkW8j7W4qltYpzgNXljr9sD9aLwYIHli20C56JTFyeJpNbDtx3aJq X-Received: by 2002:a05:6808:f91:b0:3ae:2877:9b31 with SMTP id o17-20020a0568080f9100b003ae28779b31mr227134oiw.20.1695763493968; Tue, 26 Sep 2023 14:24:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695763493; cv=none; d=google.com; s=arc-20160816; b=hWda/xpuS1DUDkL4cQOw/M/lEJ6RpMkvrnHG8Dv2s+FFjMDZiCiR5hgT4Di2+wYnpN S3ynMuLfSmVUFSKO2K4C2vlsKMQDjl60ol+ygAS6UWhdlC7buJwl5s4NXueK3f5zNR2s HpY4LNZddkYttd2b75zfvUShnBi3cO/ttKC1GSDqRmGXc7QdISO3CBKyg1kf0Wn4VSYv HSWZUHQuv8s7aewP7u3UUDJCT1Kn4qKdLtOcaoNCsrqDWv3IiLIN0pLw2w0RwUstJO/H 4ZD8KTe4e490IgwIBPiEBjBB3vXWueJJpRp79MiTiUiw91f0qYL044oFXA3OWP8is3KY pgJw== 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=Plk1Hu+6o5eXVj2RFLi33sYNLLeCeOc6UN77fu78+Xg=; fh=eI1w0vL8wMQYq+89KHAVQG8zXClfMmqUQ4ir9T7VotM=; b=OTPuM9oyzfHOF/vIT+vgu90To+K2P0f83QWJK+ojeapb8gZ4Vz70JVpPH8stjEolFz cGuYb6CkU9SwlX5F3b73JEeg3NUMooHq6psuYt3ifNlBkD8GAuNzorkFK/07pJ++hS1l kmsx32pAvYV8DJi0KinjLJPHti7Cv0OSlO1jCbsPHlbikaV9kTW9wy0YkECPC6Si5zwK K6PjO8HR55HhiBK3KJhPFS8SNWtAnGQcQrtdeuSTLJu3WY2RE1cKapuid8i9FNETn2de cnqSJ6qtuHlaOv6w+L/wrDeNkXgAip6qyIaRyqKgwY/43kGmbfYvwWpf6kRG39A5Gpbd EQBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XyGFm7CD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id g24-20020a633758000000b00573fbbf187dsi13786692pgn.216.2023.09.26.14.24.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 14:24:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=XyGFm7CD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id BAE69826A927; Tue, 26 Sep 2023 08:04:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235074AbjIZPER (ORCPT + 28 others); Tue, 26 Sep 2023 11:04:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234996AbjIZPEE (ORCPT ); Tue, 26 Sep 2023 11:04:04 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B600120 for ; Tue, 26 Sep 2023 08:03:58 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3200b181b67so1192110f8f.0 for ; Tue, 26 Sep 2023 08:03:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695740636; x=1696345436; 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=Plk1Hu+6o5eXVj2RFLi33sYNLLeCeOc6UN77fu78+Xg=; b=XyGFm7CDlljALS2QDAY5KtQ4V8TiYkjMIj42Wv28D28oi0yDG4lBsWLKIT118S7UMh rw5uIvzTi7EW2UypRlFzedrt9G1HOGYSlbcyv59/bRD+2E/7xTa9BQItjYcolFwnawzS gHFzmNNwFcnqM1T/G+puoSm3Y7dkUuXiRup2G+hF7+Cuzol/dmN8jZ9uUXABswxMt6kR 7dJgwTbhD6JhS/KUMQHBqB0v8VmH/yXygByRAK1S/rhu2q5Xr5QtTcU5bLW86Hxlqb7I JdhSYLWy4aB40NasJXMN9KZUGW2Co2V1H0YYvGbqWbsjPr8kMRu2Yuvfcm6OuDfpwIOg h01g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695740636; x=1696345436; 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=Plk1Hu+6o5eXVj2RFLi33sYNLLeCeOc6UN77fu78+Xg=; b=rT35JwsPq3LMIH3VuSbUc/FQkazixQxpL1uEzQOjqwaUpY/DwYC/IDEciwWho4Jczs fxaLDSP/Ihwo3LX0I2dbeyTqqoeJqzU/Bt0gv2rSklPdex/ldTXAG5UBQ3rHCD+NaHDi xDcW0HQ7ovRBAPppbdJuMxbkBPQJhJOoUk7fQdbiw7QdNZC9IG8WFaen5Ul40h4kdo3M gS0XIMsVq6P2rIOgHvn7luajrWDMQ/aL9xWSYMl8y2K8gtBlG8nUtrN19NT0heqFZpym 87a7kG/EBM9Tzbf1TUNUTnxltSscLCHUl3cICTuPRjdWtl6jA2RZK5/9Ay5/AZh6tDUf S7Yg== X-Gm-Message-State: AOJu0YwiUtZ9T/Hm1CulGjeEXc9l1rjg/IWEkRvKUnYIHpbtP6JU+ZjG VXwZlJFa6AC8NAia9XvIX7tM5Q== X-Received: by 2002:adf:f3d0:0:b0:31a:e772:ddf9 with SMTP id g16-20020adff3d0000000b0031ae772ddf9mr8993186wrp.4.1695740636702; Tue, 26 Sep 2023 08:03:56 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:03:56 -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 Subject: [PATCH 5/7] riscv: add support for sysctl unaligned_enabled control Date: Tue, 26 Sep 2023 17:03:14 +0200 Message-Id: <20230926150316.1129648-6-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 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Tue, 26 Sep 2023 08:04:39 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778136901443490100 X-GMAIL-MSGID: 1778136901443490100 This sysctl tuning option allows the user to disable misaligned access handling globally on the system. This will also be used by misaligned detection code to temporarily disable misaligned access handling. Signed-off-by: Clément Léger --- arch/riscv/Kconfig | 1 + arch/riscv/kernel/traps_misaligned.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index d607ab0f7c6d..3515510fe418 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -157,6 +157,7 @@ config RISCV select RISCV_TIMER if RISCV_SBI select SIFIVE_PLIC select SPARSE_IRQ + select SYSCTL_ARCH_UNALIGN_ALLOW select SYSCTL_EXCEPTION_TRACE select THREAD_INFO_IN_TASK select TRACE_IRQFLAGS_SUPPORT diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index 041fd2dbd955..b5fb1ff078e3 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -396,6 +396,9 @@ union reg_data { u64 data_u64; }; +/* sysctl hooks */ +int unaligned_enabled __read_mostly = 1; /* Enabled by default */ + int handle_misaligned_load(struct pt_regs *regs) { union reg_data val; @@ -406,6 +409,9 @@ int handle_misaligned_load(struct pt_regs *regs) perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr); + if (!unaligned_enabled) + return -1; + if (get_insn(regs, epc, &insn)) return -1; @@ -502,6 +508,9 @@ int handle_misaligned_store(struct pt_regs *regs) perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr); + if (!unaligned_enabled) + return -1; + if (get_insn(regs, epc, &insn)) return -1; From patchwork Tue Sep 26 15:03:15 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: 144989 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2089208vqu; Tue, 26 Sep 2023 10:46:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEl14PET2vgU9fGjj2tlIkI1g95No3MUZ2lgaHAcWfOvyMGvIro60h33NNW4BYcrWhmEiuL X-Received: by 2002:a9d:7f06:0:b0:6bc:de62:86b with SMTP id j6-20020a9d7f06000000b006bcde62086bmr9954490otq.7.1695750380337; Tue, 26 Sep 2023 10:46:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695750380; cv=none; d=google.com; s=arc-20160816; b=q1ZnrBXVM66kt4kEpyEDn+fRXPrHDCtvFEDxHcFD5D/eB39TeUdq7pQvd0vfM/BypP wHj3TaOFOuGiW19HsrSb/ufd57K0N2NBOMophOIyP54auBki3ArBPy7XcVOPzpxu9baK NNaDtKNXdr52aMqICBkUzcDAJ9qeKC/eE9wA3ODcjvsvg5CIDBjb5863ZlCS41F2yfZU SoBLMkiMosxMhlBLWxWBiR2hHUwZfGb90ooNQ8JWac+JgSEN+smngGk6WoCRhspasRRQ zRm9EvFG7sX+vOMwdTl+te/RORx/D3pHsbhZiaJ6VnM3y2ieNFTTv6lUJTHc+YPJFnLC uVBA== 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=cTRkIgCqvbOvq5WltvgZniNquXqWMppNcycuOTnJ1ME=; fh=eI1w0vL8wMQYq+89KHAVQG8zXClfMmqUQ4ir9T7VotM=; b=cR9ICxUbobdRuNJsRpGSc0S85IDs80gbGftMfcD/GmY/gccDdhB8yiEu/7pYmXeEBb 1c5gnRBGouIbjW9rP7NqO4owbxcmXqi5JqDsxiuWnxqyc2J2QTlqWKdlFbph+GHdfnTu Km5XMHN+5DbcFSoYPaZkX+K+FNklS9+7akL2ZjIgRCW9mcFC/XgSPda1Bla7FEeaevug Kpn5hbO6anHw/CcI9BIg/UEYRY1lmF3uUMz0SsAuJMhO4JOmJGbHTf36DrBLY6lwjFde 1OH3b2SNjPXOtqzTAwk5VvtXjtG9j1i3itLw46ZHHbkE0juPLPJ7WG89M8zvQLh1LTP8 Ag+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=x79bgUMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id fc14-20020a056a002e0e00b0068ff333d768si13818798pfb.384.2023.09.26.10.46.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 10:46:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=x79bgUMJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id 0A562808E670; Tue, 26 Sep 2023 08:05:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235062AbjIZPEX (ORCPT + 28 others); Tue, 26 Sep 2023 11:04:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235006AbjIZPEG (ORCPT ); Tue, 26 Sep 2023 11:04:06 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 257BF10A for ; Tue, 26 Sep 2023 08:03:59 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-405d70d19bcso6057335e9.0 for ; Tue, 26 Sep 2023 08:03:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695740637; x=1696345437; 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=cTRkIgCqvbOvq5WltvgZniNquXqWMppNcycuOTnJ1ME=; b=x79bgUMJe5UqQP4AUlj+09IEnJWhjCCeCBQu2ZsIN/vtaylYZUGwDHtOkcr9mtmqws vHvrCQKLHay24gpPqrv6akdx6jbziOJj1q/vg/rZ/3/Niyuzk3o/ShaJ45tnYVSrWLVF mc0xJbHd1XN43TgAIUU6PVtnBFdZm/aDDtiD9t3LT+sIl0EL/RaoUX/eNi0a1Yinvnvn nu8dy5JNnd0GFnwA/av6f/GzRD7uK7+CiVvMg1ipkZToLlkJigglqzYwolqBpzgE5Bj1 H4cVUTkzTLATbTM9fewbYePqpHCOQhGVMqxwHNznNxMvDRJek4ZjjBsB/3L0g96JUdy6 ZBRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695740637; x=1696345437; 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=cTRkIgCqvbOvq5WltvgZniNquXqWMppNcycuOTnJ1ME=; b=KmGX7405fznQcHu5QYXPl94elu0JTRs8A+LJppmGFre5YnVpNt6RnqUGbmcUY+9qIW hy+TlHD4ioY2bWj/qYEF0HQlsm2TLDNvoAio3y6C36/tznEGKJMnrt0gsZ5WRc7wu+Xx GnkL75HngatuawGkO201n+P9Cxm21zWQ7QtX9FZuAl417Qe8/26e2IphOppX9vHn+N8h S9VgGR0UP+9bt+QN9MOMwHY52XGgsloAJDdqD7peC9ZMvEc2uzu5E+MPbxk7fgpoRJJF wvoNa3GRGY7hMOvNldZUJxyQOiasQ6v26SxwwkuvOt5VtJ77UMB2l7TkuFu1srvV9rwZ gNfQ== X-Gm-Message-State: AOJu0YytXLQ/FQU2pZKs1OW2RrmIkIX/xLKEYFwkm4yFmGX7HLaNyIql fyQXEO+2BrSwxa6amGbzT1Jm7g== X-Received: by 2002:adf:e945:0:b0:31a:e29f:5eab with SMTP id m5-20020adfe945000000b0031ae29f5eabmr8963359wrn.7.1695740637664; Tue, 26 Sep 2023 08:03:57 -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.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 08:03:57 -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 Subject: [PATCH 6/7] riscv: report misaligned accesses emulation to hwprobe Date: Tue, 26 Sep 2023 17:03:15 +0200 Message-Id: <20230926150316.1129648-7-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 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Tue, 26 Sep 2023 08:05:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778123150945226606 X-GMAIL-MSGID: 1778123150945226606 hwprobe provides a way to report if misaligned access are emulated. In order to correctly populate that feature, we can check if it actually traps when doing a misaligned access. This can be checked using an exception table entry which will actually be used when a misaligned access is done from kernel mode. Signed-off-by: Clément Léger --- arch/riscv/include/asm/cpufeature.h | 6 +++ arch/riscv/kernel/cpufeature.c | 6 ++- arch/riscv/kernel/setup.c | 1 + arch/riscv/kernel/traps_misaligned.c | 63 +++++++++++++++++++++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h index d0345bd659c9..c1f0ef02cd7d 100644 --- a/arch/riscv/include/asm/cpufeature.h +++ b/arch/riscv/include/asm/cpufeature.h @@ -8,6 +8,7 @@ #include #include +#include /* * These are probed via a device_initcall(), via either the SBI or directly @@ -32,4 +33,9 @@ extern struct riscv_isainfo hart_isa[NR_CPUS]; void check_unaligned_access(int cpu); +bool unaligned_ctl_available(void); + +bool check_unaligned_access_emulated(int cpu); +void unaligned_emulation_finish(void); + #endif diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 1cfbba65d11a..fbbde800bc21 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -568,6 +568,9 @@ void check_unaligned_access(int cpu) void *src; long speed = RISCV_HWPROBE_MISALIGNED_SLOW; + if (check_unaligned_access_emulated(cpu)) + return; + page = alloc_pages(GFP_NOWAIT, get_order(MISALIGNED_BUFFER_SIZE)); if (!page) { pr_warn("Can't alloc pages to measure memcpy performance"); @@ -645,9 +648,10 @@ void check_unaligned_access(int cpu) __free_pages(page, get_order(MISALIGNED_BUFFER_SIZE)); } -static int check_unaligned_access_boot_cpu(void) +static int __init check_unaligned_access_boot_cpu(void) { check_unaligned_access(0); + unaligned_emulation_finish(); return 0; } diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c index e600aab116a4..3af6ad4df7cf 100644 --- a/arch/riscv/kernel/setup.c +++ b/arch/riscv/kernel/setup.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/arch/riscv/kernel/traps_misaligned.c b/arch/riscv/kernel/traps_misaligned.c index b5fb1ff078e3..fa81f6952fa4 100644 --- a/arch/riscv/kernel/traps_misaligned.c +++ b/arch/riscv/kernel/traps_misaligned.c @@ -9,11 +9,14 @@ #include #include #include +#include #include #include #include #include +#include +#include #define INSN_MATCH_LB 0x3 #define INSN_MASK_LB 0x707f @@ -396,8 +399,10 @@ union reg_data { u64 data_u64; }; +static bool unaligned_ctl __read_mostly; + /* sysctl hooks */ -int unaligned_enabled __read_mostly = 1; /* Enabled by default */ +int unaligned_enabled __read_mostly; int handle_misaligned_load(struct pt_regs *regs) { @@ -412,6 +417,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; @@ -511,6 +519,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; @@ -585,3 +596,53 @@ int handle_misaligned_store(struct pt_regs *regs) return 0; } + +bool check_unaligned_access_emulated(int cpu) +{ + unsigned long emulated = 1, tmp_var; + + /* Use a fixup to detect if misaligned access triggered an exception */ + __asm__ __volatile__ ( + "1:\n" + " "REG_L" %[tmp], 1(%[ptr])\n" + " li %[emulated], 0\n" + "2:\n" + _ASM_EXTABLE(1b, 2b) + : [emulated] "+r" (emulated), [tmp] "=r" (tmp_var) + : [ptr] "r" (&tmp_var) + : "memory"); + + if (!emulated) + return false; + + per_cpu(misaligned_access_speed, cpu) = + RISCV_HWPROBE_MISALIGNED_EMULATED; + + return true; +} + +void __init unaligned_emulation_finish(void) +{ + int cpu; + + /* + * We can only support PR_UNALIGN controls if all CPUs have misaligned + * accesses emulated since tasks requesting such control can run on any + * CPU. + */ + for_each_possible_cpu(cpu) { + if (per_cpu(misaligned_access_speed, cpu) != + RISCV_HWPROBE_MISALIGNED_EMULATED) { + goto out; + } + } + unaligned_ctl = true; + +out: + unaligned_enabled = 1; +} + +bool unaligned_ctl_available(void) +{ + return unaligned_ctl; +} From patchwork Tue Sep 26 15:03:16 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: 144970 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2044997vqu; Tue, 26 Sep 2023 09:32:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEXu6tojSmetmFDlhOS4UbA+N1vMZjHJ2906oLbhHL13oqHxTlnttTSxJys3oFkU6H0BBGd X-Received: by 2002:a05:6a00:1888:b0:692:b6e8:ce7c with SMTP id x8-20020a056a00188800b00692b6e8ce7cmr12721140pfh.19.1695745956220; Tue, 26 Sep 2023 09:32:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695745956; cv=none; d=google.com; s=arc-20160816; b=fqs9HW5uni9xprKQ9FKDzoKa5+GAt/4wORygVvzMDhnfaxBCGpemxAej15G0yHQJdP OZ2xGQtbDodmDixhBrm0l5Vgg5VMYjAK7i/cpVQcgYZOYHBV0WlHmbTplGHUJNic3Lxa cTd+Gm3UVT7h4PLKVWeCZOriOEwqtmhTov5yVtkpe4HKF/HKbKWEbfnj0sLfiYPAIXG1 gWVTwBD/FTqCQhvvWjt/IqNoSRAC9p67Tp6QIelALkQOmJz9jD9iMiO8OaMbwuoTrxKx 1g2222sbLtrtyzmxBR1DuHCjhwkPc3VRYruSQnsgUhbrMblhpFyVhTRItzYihnmXX7Vv oOIQ== 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 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id u8-20020a056a00158800b00690f8ed35dbsi13747750pfk.345.2023.09.26.09.32.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 09:32:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; 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 2620:137:e000::3:6 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 E6649808691A; Tue, 26 Sep 2023 08:05:49 -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 S235078AbjIZPEV (ORCPT + 28 others); Tue, 26 Sep 2023 11:04:21 -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?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 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 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:05:50 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778118511841996031 X-GMAIL-MSGID: 1778118511841996031 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);