Received: by 2002:a05:6512:23a5:0:0:0:0 with SMTP id c37csp499996lfv; Tue, 15 Feb 2022 07:29:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJzAdsa7IAzD/mBiZqd8HE0YyIwKu0lk7uYIw+TiYEm0pvchQBgxiB2xmg6S5oU5+jZhWOJk X-Received: by 2002:a05:6402:35cf:: with SMTP id z15mr4515668edc.13.1644938946027; Tue, 15 Feb 2022 07:29:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644938946; cv=none; d=google.com; s=arc-20160816; b=ghzO8VK6EWTaTPpDypRvpYNXyMAkiTLhgL7pW6emR6jJ7HA15Z0vMndwRgyvAK9jXF +RObO0lOG0Y9jJfQt7kQ9tiYAzSQOHxbeSIq0YlGyWXjwUbPe2nzK8/3tutR7HIvJvGo +RKyDERwSBQbUY1ZrCO0w/FZ+cJPoZEMpJxsN+d26oBEqzvlMOmbyTO0p1jNr4bijnSe otxlnSHebqIfJ09XEukEb9MZ98eqlg10vno1yRdybb5dAlw1LHbRebF4esNDTw1WrAZa a3Wclb1x1qAQfTMh2Fs+uxW+YkOtyfTUOj8b4uYaBdPJr+nTXRZRKysNbuAF6AQ20oLP 9nsg== 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=jKIMNTCTSaMpDoiqtlH3Q9QXsNmu9SOq71EHGRiXKQg=; b=F0tQpqcB117AbxOfwMOsBsj5OjQP1NZc+EbrGZKYmM6/hnmQ3a8YjvKri5rZThBcJP CO+ZGNZJcuzOQCekGMi5XKipufEdzFj9istkfHCPp73zcydvt7zHVyj93zitlytH44ob z1PtxVn95XugBizeJi9GKosp7QoAOUHFATUPflFjrunyQ7pX38ixn1q0V22U4elkGCbc 1PNlzSpzOWmh3/PWXZhsZr3E6qCpYZAese2x6EaiMhHcZ0YUcvtRQbP7F4l5ABu6U7Yt 43htD8WfylH5rwaH73xO8fD4mRVDCBvOxJ1hYZonC05frrYoQl3vQ+MbcL283BQmKWBH r7dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b="IKo/im9v"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hb13si2999926ejc.932.2022.02.15.07.28.40; Tue, 15 Feb 2022 07:29:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b="IKo/im9v"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235552AbiBOJCv (ORCPT + 99 others); Tue, 15 Feb 2022 04:02:51 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:41486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235537AbiBOJCq (ORCPT ); Tue, 15 Feb 2022 04:02:46 -0500 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6554110610A for ; Tue, 15 Feb 2022 01:02:35 -0800 (PST) Received: by mail-qt1-x836.google.com with SMTP id e16so17958716qtq.6 for ; Tue, 15 Feb 2022 01:02:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jKIMNTCTSaMpDoiqtlH3Q9QXsNmu9SOq71EHGRiXKQg=; b=IKo/im9vNvmw3DDbv7pl3Jy9rQHe0pf4uWP6c70xfG0w+L4au40G42ELqNNlEIA5t5 lotcK9NYVkKEcMFnpug+isg/KHOOChYTsJtA16RR+TbVa03ziUCKC/Fo5mw1PqrqzhtA Blvot4EgBJLkRgNAM+hVnd9AC/c8iSPBO6+Wy5D7qQm5/01SKrw/DRSMKXYJu1vS78tD dbUlx/AVYIoYfRA+tjbRv2MapUG0BRXUVnK9MmPN46SmiBQg14AHV6WYM5FNl4gbaw/D NAdbSrlv+ILXXpa7B/0Fm8qCNJBeWr0VxgnfpvbXtOuvdc2gbPza0pR0RnP4ttCQXTrd K76A== 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=jKIMNTCTSaMpDoiqtlH3Q9QXsNmu9SOq71EHGRiXKQg=; b=4wlA0h70FO8/eTsYY+cxZxsz2133MVbwBLchwDOETDDik82l95HahDuW82b5Pv5BxY Nqob9byf2nprOsKAlq/QOAe2Sev80n3lVy19cQDi31Sw6YFHZTRVpYAbk9Cgm96+6570 PpQfqHn+oH+NvbHcN5bjFP/k02yA8NVd4HNzQG/cuaYuv1laxx3I1Ln+JqUAWCawSUE3 B+5tFq+VlWXvQOBIOcqFUchebBFK4F+097EscHHY/5JwKbB2FzmJLWGkEFdLcy+Sl1AR ra2OKGtHzG9bub1+/Q8U93Fs64ol3PreY5DwZeE1JYHh0aSUQ/slip8ezM6tu1xKZ9yF BGYQ== X-Gm-Message-State: AOAM533jW1RtLqzAW8FBQSX6qffK7P7kciZ15VRQQMFQWdFBWwEYP6hw RFFV/yMVesT6k49R1T8VlQo0HROF6xz7ihQP X-Received: by 2002:a05:622a:1351:: with SMTP id w17mr1961454qtk.447.1644915754311; Tue, 15 Feb 2022 01:02:34 -0800 (PST) Received: from rivos-atish.. (adsl-70-228-75-190.dsl.akrnoh.ameritech.net. [70.228.75.190]) by smtp.gmail.com with ESMTPSA id w4sm17711158qko.123.2022.02.15.01.02.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 01:02:33 -0800 (PST) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Tsukasa OI , Atish Patra , Heiko Stuebner , Albert Ou , Atish Patra , Anup Patel , Damien Le Moal , devicetree@vger.kernel.org, Jisheng Zhang , Krzysztof Kozlowski , linux-riscv@lists.infradead.org, Palmer Dabbelt , Paul Walmsley , Rob Herring Subject: [PATCH v3 2/6] RISC-V: Minimal parser for "riscv, isa" strings Date: Tue, 15 Feb 2022 01:02:07 -0800 Message-Id: <20220215090211.911366-3-atishp@rivosinc.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220215090211.911366-1-atishp@rivosinc.com> References: <20220215090211.911366-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Tsukasa OI Current hart ISA ("riscv,isa") parser don't correctly parse: 1. Multi-letter extensions 2. Version numbers All ISA extensions ratified recently has multi-letter extensions (except 'H'). The current "riscv,isa" parser that is easily confused by multi-letter extensions and "p" in version numbers can be a huge problem for adding new extensions through the device tree. Leaving it would create incompatible hacks and would make "riscv,isa" value unreliable. This commit implements minimal parser for "riscv,isa" strings. With this, we can safely ignore multi-letter extensions and version numbers. [Improved commit text and fixed a bug around 's' in base extension] Signed-off-by: Atish Patra [Fixed workaround for QEMU] Signed-off-by: Tsukasa OI Tested-by: Heiko Stuebner --- arch/riscv/kernel/cpufeature.c | 66 ++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index dd3d57eb4eea..9d5448542226 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -66,7 +67,7 @@ void __init riscv_fill_hwcap(void) struct device_node *node; const char *isa; char print_str[NUM_ALPHA_EXTS + 1]; - size_t i, j, isa_len; + int i, j; static unsigned long isa2hwcap[256] = {0}; isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I; @@ -92,23 +93,66 @@ void __init riscv_fill_hwcap(void) continue; } - i = 0; - isa_len = strlen(isa); #if IS_ENABLED(CONFIG_32BIT) if (!strncmp(isa, "rv32", 4)) - i += 4; + isa += 4; #elif IS_ENABLED(CONFIG_64BIT) if (!strncmp(isa, "rv64", 4)) - i += 4; + isa += 4; #endif - for (; i < isa_len; ++i) { - this_hwcap |= isa2hwcap[(unsigned char)(isa[i])]; + for (; *isa; ++isa) { + const char *ext = isa++; + const char *ext_end = isa; + bool ext_long = false, ext_err = false; + + switch (*ext) { + case 's': + case 'x': + case 'z': + /** + * Workaround for invalid single-letter 's' (QEMU). + * It works until multi-letter extension starting + * with "Su" appears. + */ + if (*ext == 's' && ext[-1] != '_' && ext[1] == 'u') + break; + ext_long = true; + /* Multi-letter extension must be delimited */ + for (; *isa && *isa != '_'; ++isa) + if (!islower(*isa) && !isdigit(*isa)) + ext_err = true; + /* ... but must be ignored. */ + break; + default: + if (unlikely(!islower(*ext))) { + ext_err = true; + break; + } + /* Find next extension */ + if (!isdigit(*isa)) + break; + while (isdigit(*++isa)) + ; + if (*isa != 'p') + break; + if (!isdigit(*++isa)) { + --isa; + break; + } + while (isdigit(*++isa)) + ; + break; + } + if (*isa != '_') + --isa; /* - * TODO: X, Y and Z extension parsing for Host ISA - * bitmap will be added in-future. + * TODO: Full version-aware handling including + * multi-letter extensions will be added in-future. */ - if ('a' <= isa[i] && isa[i] < 'x') - this_isa |= (1UL << (isa[i] - 'a')); + if (ext_err || ext_long) + continue; + this_hwcap |= isa2hwcap[(unsigned char)(*ext)]; + this_isa |= (1UL << (*ext - 'a')); } /* -- 2.30.2