Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1070695iob; Fri, 13 May 2022 21:59:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTi0JvfVPPv6DifCozxzMLAJzupoSc/Zt+gg/qXSxIyWk0X2cXrfRoHyfdcy612g40HDJP X-Received: by 2002:a5d:6489:0:b0:20c:6c3b:d49e with SMTP id o9-20020a5d6489000000b0020c6c3bd49emr6649308wri.75.1652504372180; Fri, 13 May 2022 21:59:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652504372; cv=none; d=google.com; s=arc-20160816; b=IcWcIFl8EQKkQynRJdxQHnu+KvgBFSYNu6fnoMzSZs1HAcu5uGq8cKhuMeDwTWzlUm d5AsYyAMZEnMKLh4l5sQAzPj+L76bO1zScK5/dCZmyWNEvUfXI00PUZIPaSQIbI+5kqj K0SkKUu5LU+VFyiuHcAMeIp28ZHt9pMWkepM4OCyqQrTdRBxSEtHErwZ0jkm+6ai0P2M T7y68Lp09iA5vc4Y4DOFSR8bvprtBShiNERHwM4A80OcO/QXNCZ7iv9iXhGxX4kcaYwY dW9hTUURPSubbg+bU1KlTreUHJOagOdV1KicuQGne58RkLTC1jvoukf9CgtHqPYSvDK5 Klfg== 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=F29OH6asB34VWoRlm7WLSnKtmcCuH6sg3zO/EYEp6Og=; b=kfooEjFFdHXWA3VZO86LUehfJ83PaKJXvcGEQPM/do4KdkT3DQgzr/2bQO627qpw57 Z77VVsXPTnzCAeQnbH0h2nE0SWUyOYkzM3BJDPmS14r64aDt68l3eG5DbGEsdXXXQj6g OzDqXgYF/fhX2498z1dq2MXYDXO2BoRI54axiGz0UjDthYhlMDY37s/cxNLCZVWj63rv ZSIMzZ8V1dbYEN5eG6wKtYClzyGzWzVwNUBXSYl8umgTJtU94oiYWZ+i26WyF0GlxMyU v5Bomq+ssQm3089v9IgrqM900B55IT3pEjagZ9+0QbG4pf/O2wc4IES/5HhOiA0tC0zr kUiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PnY1Jknh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id s5-20020adfea85000000b00207a5488326si3581289wrm.446.2022.05.13.21.59.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 21:59:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PnY1Jknh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 91751CFE15; Fri, 13 May 2022 20:35:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231400AbiENDfL (ORCPT + 99 others); Fri, 13 May 2022 23:35:11 -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-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: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, FROM_LOCAL_NOVOWEL,HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 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); } +static int check_div0(struct pt_regs *regs) +{ + u8 buf[7]; + void *p; + static const u8 pattern1[] = {0, 0, 0, 'D', 'I', 'V', '0'}; +#if defined(__XTENSA_EB__) + static const u8 pattern2[] = {0xd6, 0x0f, 'D', 'I', 'V', '0'}; +#elif defined(__XTENSA_EL__) + static const u8 pattern2[] = {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 = buf; + } else { + p = (void *)regs->pc; + } + + return memcmp(p, pattern1, sizeof(pattern1)) == 0 || + memcmp(p, pattern2, sizeof(pattern2)) == 0; +} + /* * Illegal instruction. Fatal if in kernel space. */ 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); /* If in user mode, send SIGILL signal to current process. */ -- 2.30.2