From nobody Thu Mar 28 17:51:49 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32D28C433F5 for ; Sat, 14 May 2022 03:35:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231510AbiENDfF (ORCPT ); Fri, 13 May 2022 23:35:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231383AbiENDe5 (ORCPT ); Fri, 13 May 2022 23:34:57 -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 EC58C93458 for ; Fri, 13 May 2022 20:34:55 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id w4so13546446wrg.12 for ; Fri, 13 May 2022 20:34:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LbqWA0ma3Gj0jvWCSxtRNJBD3Bf6tu8RmToQD/S/1tA=; b=gtq9z1Gj/PCfH+6jFgOvMnLRXGDONoKZuJaDEtvtAB+Pm/dztH9DvgrU6IAK4GHSvx 2QVOEMjMwI/eT5uc3gxko0aNgtLwRLxBEXKz950r/3llvBuFiuS54qlX3ymMogGVyUaC 6wcgTEom4pnkmhKZ39vK8KU1a7C/3oSZ4r8M61A05BaJLLEILmITjPQaOLc7nFsuywfr cuzbLh8GYTS9+50CUcFm/f953Evh84EpYvwInJlyRBVsB8/U1l2Vz/rrjsh2OlCKsIN+ YLiFJcUe7wVFO5O93J41chW+UnzT8KscgSDdJ5bLFAjA2toRqCggSV8FKgzHoQGfLv/+ 9EhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LbqWA0ma3Gj0jvWCSxtRNJBD3Bf6tu8RmToQD/S/1tA=; b=jU99lYWMoK+iAGHVe0kNieJffxzYeVMML/SDvlguVoc1n82UWl8LAtz3lGUPh2enSt M5XYZfVWAKTUJse9zGRLURzQxtEDMTPIupmhZSxx+zMt41xcdP/ZswaSLt0jcnXyXSwk TeJLXygHUqSzlZqL8YqoKVs4fw+pRAjF3HPvfn4H/wZWTbfMSecp204nRKO+dWTWEBEZ TK3lqOGM/qbnmpD8AY8H2MgP/1ZWXnqSY8lXvUVXuxI23vB4iqQEYe6qwB+Fqm8bZQhl 18tQWMEEvHRQiRs+M8YRBpDKbreFbzHy+DpwaxXalIZ3qK35a/k2r1JphsggBreC5v3c diJg== X-Gm-Message-State: AOAM532y2bwJY73WBpoFJHnjkRXNHuMphOXKeXgDJVElYmvAwANILdF0 nKroQ8iA1hlUik3LUuTI6er5MlBT8xGsAQ== X-Google-Smtp-Source: ABdhPJwb0OdUi32hRVoqlssaZ0HnR6jJ93O92ZO68kKEPXG+siOvohkKiv+j+dUCZ71ZdIGSCqzcfQ== X-Received: by 2002:adf:f691:0:b0:20c:d5a4:79f1 with SMTP id v17-20020adff691000000b0020cd5a479f1mr6368906wrp.380.1652499294508; Fri, 13 May 2022 20:34:54 -0700 (PDT) Received: from octofox.metropolis ([178.134.103.46]) by smtp.gmail.com with ESMTPSA id h1-20020adfaa81000000b0020c66310845sm3542638wrc.55.2022.05.13.20.34.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 20:34:54 -0700 (PDT) From: Max Filippov To: linux-xtensa@linux-xtensa.org Cc: Chris Zankel , linux-kernel@vger.kernel.org, Max Filippov Subject: [PATCH v2 1/3] xtensa: add trap handler for division by zero Date: Fri, 13 May 2022 20:34:35 -0700 Message-Id: <20220514033437.4182899-2-jcmvbkbc@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220514033437.4182899-1-jcmvbkbc@gmail.com> References: <20220514033437.4182899-1-jcmvbkbc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add c-level handler for the division by zero exception and kill the task if it was thrown from the kernel space or send SIGFPE otherwise. Signed-off-by: Max Filippov --- arch/xtensa/kernel/traps.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/xtensa/kernel/traps.c b/arch/xtensa/kernel/traps.c index 138a86fbe9d7..24d11b44fa57 100644 --- a/arch/xtensa/kernel/traps.c +++ b/arch/xtensa/kernel/traps.c @@ -49,6 +49,7 @@ */ =20 static void do_illegal_instruction(struct pt_regs *regs); +static void do_div0(struct pt_regs *regs); static void do_interrupt(struct pt_regs *regs); #if XTENSA_FAKE_NMI static void do_nmi(struct pt_regs *regs); @@ -95,7 +96,7 @@ static dispatch_init_table_t __initdata dispatch_init_tab= le[] =3D { #ifdef SUPPORT_WINDOWED { EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca }, #endif -/* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */ +{ EXCCAUSE_INTEGER_DIVIDE_BY_ZERO, 0, do_div0 }, /* EXCCAUSE_PRIVILEGED unhandled */ #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION #ifdef CONFIG_XTENSA_UNALIGNED_USER @@ -307,6 +308,11 @@ static void do_illegal_instruction(struct pt_regs *reg= s) force_sig(SIGILL); } =20 +static void do_div0(struct pt_regs *regs) +{ + __die_if_kernel("Unhandled division by 0 in kernel", regs, SIGKILL); + force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->pc); +} =20 /* * Handle unaligned memory accesses from user space. Kill task. --=20 2.30.2 From nobody Thu Mar 28 17:51:49 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CD71C433EF for ; Sat, 14 May 2022 03:35:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231523AbiENDfO (ORCPT ); Fri, 13 May 2022 23:35:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231440AbiENDe6 (ORCPT ); Fri, 13 May 2022 23:34:58 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EED9941B2 for ; Fri, 13 May 2022 20:34:57 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id v64-20020a1cac43000000b0038cfd1b3a6dso7841563wme.5 for ; Fri, 13 May 2022 20:34:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=F29OH6asB34VWoRlm7WLSnKtmcCuH6sg3zO/EYEp6Og=; b=PnY1JknhQyIvblKgS7FrBkhnOIrw+KP7j+q/299tAR9IJU7WR7v2oKP2ZBIUMWw04H 6IcYcpQ+IMl8BRMszHkVkewWJDbnJXNWIiLUDGX0FwDQlH8ewYDvgqiNzBANnxUDwBYA GKcACdjZVVm8UPiRjzKqp9ML7b6I7Ar2DpCyzhFtKdHga3SEQDloJIEQB2Sd6CH1CaDs JfN/Wvwaz9f/tJznk30sllAUnGLXYd95jwJpeiHt21HdsinY5uaru4U8oKM18SCCRmQx r75Glqv2ScLPEoI2uLoAhcls5C6haG5rdQxaTg0Nv5+SDh3TBFmzt0bDFRUPDdUTOQBb WYeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=F29OH6asB34VWoRlm7WLSnKtmcCuH6sg3zO/EYEp6Og=; b=H8Yel5+F4l0MFhsH57VHPnTu2qDcMC0WYk5lpuyannaj8/v1Kkf5EY7QI4HZKBlvVd OLu4hVbh3bdSLKUoOCBJWyIiwsIS7DmDFnQoHgNJdn/h/M+N5Cw+4WV99wLcbKuCybPk SFh/PPy0js91hdge8Cj6Pl6MEvpjsQVDM9HwK7hRNLbLdEf2boFOrMRZnPl61aKiAGvX uluZuiE/RxzFvQe36F1BCvLgtLdfhmXhNmlOUXg3itzFOXbw9BMNNvMdZ0i8czcPbxdZ N1LlfF+h+9g5ndkORZk0YCIDB33uHubx9mtOAkEShMwSRP7lwEYf5i8foD7u0cFEGQ0q rF8g== X-Gm-Message-State: AOAM5315j4HepWGWGRJg+tElTOVgygaPX8VGFwmYPBVmVnsxZHIcs2qW kWnE7Z8HNzRgF8xR5yoRKuw= X-Google-Smtp-Source: ABdhPJwSLfre31yJDnmspUtYnPK3meECsPoudta3nkrbepu5i498WfpC83okFqD0CUbXxea4HxQTSw== X-Received: by 2002:a05:600c:3b11:b0:394:57eb:c58b with SMTP id m17-20020a05600c3b1100b0039457ebc58bmr17454288wms.3.1652499295767; Fri, 13 May 2022 20:34:55 -0700 (PDT) Received: from octofox.metropolis ([178.134.103.46]) by smtp.gmail.com with ESMTPSA id h1-20020adfaa81000000b0020c66310845sm3542638wrc.55.2022.05.13.20.34.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 20:34:55 -0700 (PDT) From: Max Filippov To: linux-xtensa@linux-xtensa.org Cc: Chris Zankel , linux-kernel@vger.kernel.org, Max Filippov Subject: [PATCH v2 2/3] xtensa: support artificial division by 0 exception Date: Fri, 13 May 2022 20:34:36 -0700 Message-Id: <20220514033437.4182899-3-jcmvbkbc@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220514033437.4182899-1-jcmvbkbc@gmail.com> References: <20220514033437.4182899-1-jcmvbkbc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On xtensa cores wihout hardware division option division support functions from libgcc react to division by 0 attempt by executing illegal instruction followed by the characters 'DIV0'. Recognize this pattern in illegal instruction exception handler and convert it to division by 0. Signed-off-by: Max Filippov --- Changes v1->v2: - split ABI probing improvement from the artificial division by 0 - don't limit artificial division by 0 pattern detection to configurations without HW division opcodes, do it always arch/xtensa/kernel/traps.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/arch/xtensa/kernel/traps.c b/arch/xtensa/kernel/traps.c index 24d11b44fa57..47445b2d4217 100644 --- a/arch/xtensa/kernel/traps.c +++ b/arch/xtensa/kernel/traps.c @@ -293,12 +293,42 @@ static void do_interrupt(struct pt_regs *regs) set_irq_regs(old_regs); } =20 +static int check_div0(struct pt_regs *regs) +{ + u8 buf[7]; + void *p; + static const u8 pattern1[] =3D {0, 0, 0, 'D', 'I', 'V', '0'}; +#if defined(__XTENSA_EB__) + static const u8 pattern2[] =3D {0xd6, 0x0f, 'D', 'I', 'V', '0'}; +#elif defined(__XTENSA_EL__) + static const u8 pattern2[] =3D {0x6d, 0xf0, 'D', 'I', 'V', '0'}; +#else +#error Unsupported Xtensa endianness +#endif + + if (user_mode(regs)) { + if (copy_from_user(buf, (void __user *)regs->pc, 7)) + return 0; + p =3D buf; + } else { + p =3D (void *)regs->pc; + } + + return memcmp(p, pattern1, sizeof(pattern1)) =3D=3D 0 || + memcmp(p, pattern2, sizeof(pattern2)) =3D=3D 0; +} + /* * Illegal instruction. Fatal if in kernel space. */ =20 static void do_illegal_instruction(struct pt_regs *regs) { + if (check_div0(regs)) { + do_div0(regs); + return; + } + __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL); =20 /* If in user mode, send SIGILL signal to current process. */ --=20 2.30.2 From nobody Thu Mar 28 17:51:49 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3A03DC433EF for ; Sat, 14 May 2022 03:35:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231504AbiENDfU (ORCPT ); Fri, 13 May 2022 23:35:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231463AbiENDe7 (ORCPT ); Fri, 13 May 2022 23:34:59 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5389B95DCE for ; Fri, 13 May 2022 20:34:58 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id a5so9786411wrp.7 for ; Fri, 13 May 2022 20:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AvxCAVhEBFSALzFW5QU4eqtsfJ+K74cWo1IjNe8gKhU=; b=hhjaEh4q933w6AuKifPXLtD2S7TfCNdCj8QpH8AO9IxW5szpex5NDjVkBj7Rnfup8Y kPiGnYL2/ahfl9LkZjZc2Scb28RQpXCFJjj9hcSbwaEoV3xlvXxPDdnzC91OTJbsBmmX eYuJqs2MR2qvuvb84xdzKd0tWp51YGkHRcCsAtiX7DBDg5WjIEwlQBLZc5f8cBRDXcdP jG+nLWhoeNXCrZ/xcYN/BMPl8kB5NPX+vy78YnAfiSZhQbXG9LvLdI+N1TBEQGvW00Q5 6PmOcdeC8zQ0PWsQ78nCQA11C1oy6OXj+mLTgE4oVQz+CjxwbcKnglv3TzQu4kynXVfE JQbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AvxCAVhEBFSALzFW5QU4eqtsfJ+K74cWo1IjNe8gKhU=; b=JPeAooJRCNWYwMOKAUA3Ko7wVM3hY6hvi1qsKfJ2qM6owH27wFynwV75SmG4Zoakp2 x6+O4h3aP9Jdz4MLcg3b0rnTpwjmUOz//VVhCM0hOoxDceFjZ3PKBIYMMFz/OzhGnixQ rQ9oaROui13JeFB3E2G1jFpjm4jB29LPCRXwX/CUqTaAUM0684IK0TDx77TqVsvuwu5E jzbe2qQF4Jnsag5Dr8XRdSUG3hV8wID1Ui6pdOMZOLb5+BrCGKblEMGV19vYUFjtp57x SenLGfRGxLwsUHDhnV7ur74IjtOqpRKA6e4auGPu9mtxzfjB//qo8IxbZWpSdhjGMDh0 IQ5w== X-Gm-Message-State: AOAM532rKCyPznUwvdrwiDdxnUwB/QdaCpCB1Z9loqxN26mEOP0rIkAA uLz7C87D38kBykfh2TfM1K4= X-Google-Smtp-Source: ABdhPJwfMSYLdMz16J88+j7WK3C7LzuI8Xgae1g0LeGemd94qEf2RxkJ/dd5rCMRDloHLwtJFZjppg== X-Received: by 2002:a05:6000:81c:b0:20c:d4e7:400d with SMTP id bt28-20020a056000081c00b0020cd4e7400dmr6032737wrb.459.1652499296894; Fri, 13 May 2022 20:34:56 -0700 (PDT) Received: from octofox.metropolis ([178.134.103.46]) by smtp.gmail.com with ESMTPSA id h1-20020adfaa81000000b0020c66310845sm3542638wrc.55.2022.05.13.20.34.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 20:34:56 -0700 (PDT) From: Max Filippov To: linux-xtensa@linux-xtensa.org Cc: Chris Zankel , linux-kernel@vger.kernel.org, Max Filippov Subject: [PATCH v2 3/3] xtensa: improve call0 ABI probing Date: Fri, 13 May 2022 20:34:37 -0700 Message-Id: <20220514033437.4182899-4-jcmvbkbc@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220514033437.4182899-1-jcmvbkbc@gmail.com> References: <20220514033437.4182899-1-jcmvbkbc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When call0 userspace ABI support by probing is enabled instructions that cause illegal instruction exception when PS.WOE is clear are retried with PS.WOE set before calling c-level exception handler. Record user pc at which PS.WOE was set in the fast exception handler and clear PS.WOE in the c-level exception handler if we get there from the same address. Signed-off-by: Max Filippov --- Changes v1->v2: - split ABI probing improvement from the artificial division by 0 arch/xtensa/include/asm/thread_info.h | 4 ++++ arch/xtensa/kernel/asm-offsets.c | 3 +++ arch/xtensa/kernel/entry.S | 5 +++++ arch/xtensa/kernel/traps.c | 12 ++++++++++++ 4 files changed, 24 insertions(+) diff --git a/arch/xtensa/include/asm/thread_info.h b/arch/xtensa/include/as= m/thread_info.h index 52974317a6b6..326db1c1d5d8 100644 --- a/arch/xtensa/include/asm/thread_info.h +++ b/arch/xtensa/include/asm/thread_info.h @@ -56,6 +56,10 @@ struct thread_info { /* result of the most recent exclusive store */ unsigned long atomctl8; #endif +#ifdef CONFIG_USER_ABI_CALL0_PROBE + /* Address where PS.WOE was enabled by the ABI probing code */ + unsigned long ps_woe_fix_addr; +#endif =20 /* * If i-th bit is set then coprocessor state is loaded into the diff --git a/arch/xtensa/kernel/asm-offsets.c b/arch/xtensa/kernel/asm-offs= ets.c index 9a1db6ffcbf4..da38de20ae59 100644 --- a/arch/xtensa/kernel/asm-offsets.c +++ b/arch/xtensa/kernel/asm-offsets.c @@ -88,6 +88,9 @@ int main(void) OFFSET(TI_STSTUS, thread_info, status); OFFSET(TI_CPU, thread_info, cpu); OFFSET(TI_PRE_COUNT, thread_info, preempt_count); +#ifdef CONFIG_USER_ABI_CALL0_PROBE + OFFSET(TI_PS_WOE_FIX_ADDR, thread_info, ps_woe_fix_addr); +#endif =20 /* struct thread_info (offset from start_struct) */ DEFINE(THREAD_RA, offsetof (struct task_struct, thread.ra)); diff --git a/arch/xtensa/kernel/entry.S b/arch/xtensa/kernel/entry.S index 3224b4ceca34..e3eae648ba2e 100644 --- a/arch/xtensa/kernel/entry.S +++ b/arch/xtensa/kernel/entry.S @@ -1056,6 +1056,11 @@ ENTRY(fast_illegal_instruction_user) movi a3, PS_WOE_MASK or a0, a0, a3 wsr a0, ps +#ifdef CONFIG_USER_ABI_CALL0_PROBE + GET_THREAD_INFO(a3, a2) + rsr a0, epc1 + s32i a0, a3, TI_PS_WOE_FIX_ADDR +#endif l32i a3, a2, PT_AREG3 l32i a0, a2, PT_AREG0 rsr a2, depc diff --git a/arch/xtensa/kernel/traps.c b/arch/xtensa/kernel/traps.c index 47445b2d4217..21c2c30baf69 100644 --- a/arch/xtensa/kernel/traps.c +++ b/arch/xtensa/kernel/traps.c @@ -324,6 +324,18 @@ static int check_div0(struct pt_regs *regs) =20 static void do_illegal_instruction(struct pt_regs *regs) { +#ifdef CONFIG_USER_ABI_CALL0_PROBE + /* + * When call0 application encounters an illegal instruction fast + * exception handler will attempt to set PS.WOE and retry failing + * instruction. + * If we get here we know that that instruction is also illegal + * with PS.WOE set, so it's not related to the windowed option + * hence PS.WOE may be cleared. + */ + if (regs->pc =3D=3D current_thread_info()->ps_woe_fix_addr) + regs->ps &=3D ~PS_WOE_MASK; +#endif if (check_div0(regs)) { do_div0(regs); return; --=20 2.30.2