Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1877943rdb; Mon, 9 Oct 2023 06:03:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGW+4rZev2ToO6sgZN1tauK6VF/3TsgLTDQ//qARNUvfAV8Yztq2+VFkZbpvv1vHJxfOlCM X-Received: by 2002:a05:6358:e49c:b0:135:499a:382b with SMTP id by28-20020a056358e49c00b00135499a382bmr15879940rwb.7.1696856596566; Mon, 09 Oct 2023 06:03:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696856596; cv=none; d=google.com; s=arc-20160816; b=TNTq/H2qOkAH+QUu0aQP/oy9pZd/v6I9gyFFN2Vic2SHWXdyjfT8c3QcDyZKOSAlsX RuKOOiREeBF++MrPnyYYoWZllwhZ++KA9DPJ729M2BCe6HiU26TJDW2QuNVV36DIZDNJ nIugXuYfP8+816ftgBBnK8J0XgZDF3ytiaBb6OQvRS2YiwwpoelZebGFf8ZrvJQFKCVC bn52BNAvT9VDPnnYdmolSYqdaUgD+kpYlaeRnSoGeGV/TwFFlR/hWRAvdcJ4+/uowpsX 1EORZyY7SzHtTOsa60dcm82/sZrO/VZIyu5l3Oh4nRbogUUvFW7A3t1qljVgY705K7Pk 663Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=DyEATBYxN/EkL89zxBGiBE8n5rmmGde+J0DkSI2hwB0=; fh=N77ISLmOZhPFzfp2VQK2HcJUql62D893mdpdnyW63wE=; b=qA1OJT+VBofJojGewWmh2y0A0t6+ptGjWwabrl+dmxXAyHf0wgVmid+VLIg0LbM/ME q2l3Bm/PFcu7eYgRa9aFQA/KMJdkoTtwr0cr/s9qGMXfXyG08Xf70nfX4/3bNQOGiAN3 35GdyNVz6uFYzzfizpq3sS5PY6nnztG7W/Z1ePAWjkhyfRymZ8CJGSZm1huGlwlz/h/d unaxhl2XxQbd+zJUiAY4CzOFNAd6LZd1qbqbkJ0bj4sUQtJI7W+SNaI45o0G+s5ZsKtX pWUddTyXLowk887P2uxhCZ2ghUWSwYp8PKifGB9gcDEbj/drGVuNyN6shietSRW13V6p 2OFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YAApl5xi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id t23-20020a656097000000b005898e10f9cbsi7110736pgu.572.2023.10.09.06.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 06:03:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YAApl5xi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 6BFB6801F9AA; Mon, 9 Oct 2023 06:03:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376637AbjJINC7 (ORCPT + 99 others); Mon, 9 Oct 2023 09:02:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376530AbjJINC6 (ORCPT ); Mon, 9 Oct 2023 09:02:58 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C642C91 for ; Mon, 9 Oct 2023 06:02:56 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-406aaccb41dso12397825e9.0 for ; Mon, 09 Oct 2023 06:02:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1696856575; x=1697461375; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=DyEATBYxN/EkL89zxBGiBE8n5rmmGde+J0DkSI2hwB0=; b=YAApl5xiL6APn6jeS/fYn5iro0wwsDL1H8zVI0i/+8ZA5pMo0MlnFlnnDdBjf96Cbk BDFaFKHo40hZ+XACz9DCtPF8tLu4AtynxNj8z0WQdEidyeqtEXuz/4ILdAJ4RYDjnoYo qd3paIFburkFoTChw4ReLlROrGOiDpj4UIb/ZecRpfqwBlhzUt5+OgnC6CJXEDv+ij2E 2bNtz2vWCQZe++G0OJ2O7guW3DBmbuAt/YOo4KNlRfFD1gSWmjK80QMP1DBgVbLtXUih ny2dw2/iynb43phR2k52GksJBLVsgsSdpQ4PAH3MrIm9d6VXO4iky0Xud0q3joSBjuNw Fc4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696856575; x=1697461375; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DyEATBYxN/EkL89zxBGiBE8n5rmmGde+J0DkSI2hwB0=; b=F9ZDdLjLpv9GHr9ryCyWXB0hypVBql58grFuQPhM/m5ZTMhNXcnGsGmVwvrAMg/M+c /E8NPFe7Xc0K1FmrXQynzM5XihDVpAmpU304SfU+GZckbpVxsJ+1MgTD9px3qorH7cZ2 0asKQZVd2uJdHUJc88hoeWG/liws68jxOTRb74lhSJsmUxFMcfCmjoeUF0UXQmM7n7Wk X7bMx6oOz2M7eQrJJMX6EPaif143oJ2hF/jgki+d9O+HYULczl/302xpN7EaVTeUS80s cezrt0jvOvgN9Wt7ReBA3EaFgQJ7bn3GQDyBwdmNCs1Jh0TflscOnPFJUmoJnvEe+Nyu oL6A== X-Gm-Message-State: AOJu0YxIbqZsjVhbNt4k93EA8C57Y4Do0CUyBZwKV35taDz3fhgkztex hLkV9wZqnbku+3DNl8yBZBZ9xA== X-Received: by 2002:a05:600c:3ca1:b0:405:3cc1:e115 with SMTP id bg33-20020a05600c3ca100b004053cc1e115mr13919016wmb.3.1696856574428; Mon, 09 Oct 2023 06:02:54 -0700 (PDT) Received: from ?IPV6:2a01:e0a:999:a3a0:b2f5:bf53:6bd1:414e? ([2a01:e0a:999:a3a0:b2f5:bf53:6bd1:414e]) by smtp.gmail.com with ESMTPSA id v15-20020a5d4b0f000000b00325aca09ad1sm9678417wrq.24.2023.10.09.06.02.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Oct 2023 06:02:54 -0700 (PDT) Message-ID: Date: Mon, 9 Oct 2023 15:02:52 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/8] riscv: add support for misaligned trap handling in S-mode Content-Language: en-US To: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Atish Patra , Andrew Jones , Evan Green , =?UTF-8?Q?Bj=C3=B6rn_Topel?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Ron Minnich , Daniel Maslowski , Conor Dooley References: <20231004151405.521596-1-cleger@rivosinc.com> <20231004151405.521596-3-cleger@rivosinc.com> <871qeagw69.fsf@all.your.base.are.belong.to.us> From: =?UTF-8?B?Q2zDqW1lbnQgTMOpZ2Vy?= In-Reply-To: <871qeagw69.fsf@all.your.base.are.belong.to.us> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Mon, 09 Oct 2023 06:03:13 -0700 (PDT) X-Spam-Level: ** On 04/10/2023 19:00, Björn Töpel wrote: > Clément Léger writes: > >> Misalignment trap handling is only supported for M-mode and uses direct >> accesses to user memory. In S-mode, when handling usermode fault, this >> requires to use the get_user()/put_user() accessors. Implement >> load_u8(), store_u8() and get_insn() using these accessors for >> userspace and direct text access for kernel. >> >> Signed-off-by: Clément Léger >> --- >> arch/riscv/Kconfig | 8 ++ >> arch/riscv/include/asm/entry-common.h | 14 +++ >> arch/riscv/kernel/Makefile | 2 +- >> arch/riscv/kernel/traps.c | 9 -- >> arch/riscv/kernel/traps_misaligned.c | 119 +++++++++++++++++++++++--- >> 5 files changed, 129 insertions(+), 23 deletions(-) >> >> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig >> index d607ab0f7c6d..6e167358a897 100644 >> --- a/arch/riscv/Kconfig >> +++ b/arch/riscv/Kconfig >> @@ -636,6 +636,14 @@ config THREAD_SIZE_ORDER >> Specify the Pages of thread stack size (from 4KB to 64KB), which also >> affects irq stack size, which is equal to thread stack size. >> >> +config RISCV_MISALIGNED >> + bool "Support misaligned load/store traps for kernel and userspace" >> + default y >> + help >> + Say Y here if you want the kernel to embed support for misaligned >> + load/store for both kernel and userspace. When disable, misaligned >> + accesses will generate SIGBUS in userspace and panic in kernel. >> + >> endmenu # "Platform type" >> >> menu "Kernel features" >> diff --git a/arch/riscv/include/asm/entry-common.h b/arch/riscv/include/asm/entry-common.h >> index 6e4dee49d84b..7ab5e34318c8 100644 >> --- a/arch/riscv/include/asm/entry-common.h >> +++ b/arch/riscv/include/asm/entry-common.h >> @@ -8,4 +8,18 @@ >> void handle_page_fault(struct pt_regs *regs); >> void handle_break(struct pt_regs *regs); >> >> +#ifdef CONFIG_RISCV_MISALIGNED >> +int handle_misaligned_load(struct pt_regs *regs); >> +int handle_misaligned_store(struct pt_regs *regs); >> +#else >> +static inline int handle_misaligned_load(struct pt_regs *regs) >> +{ >> + return -1; >> +} >> +static inline int handle_misaligned_store(struct pt_regs *regs) >> +{ >> + return -1; >> +} >> +#endif >> + >> #endif /* _ASM_RISCV_ENTRY_COMMON_H */ >> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile >> index 95cf25d48405..0d874fb24b51 100644 >> --- a/arch/riscv/kernel/Makefile >> +++ b/arch/riscv/kernel/Makefile >> @@ -59,7 +59,7 @@ obj-y += patch.o >> obj-y += probes/ >> obj-$(CONFIG_MMU) += vdso.o vdso/ >> >> -obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o >> +obj-$(CONFIG_RISCV_MISALIGNED) += 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; >> + } > > One nit (...well two) ;-) > > If you're respinning I'd get rid of the "inlines",and personally I > think early exit is easier to read. Applies to the whole patch. Noted, I'll fix that (checkpatch suggested it so that makes sense). Clément > > | { > | if (user_mode(regs)) > | return __get_user(*r_val, addr); > | > | *r_val = *addr; > | return 0; > | } > > > Regardless if you change or not, > > Reviewed-by: Björn Töpel