From patchwork Sat Nov 20 08:53:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12630211 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C88B7C433F5 for ; Sat, 20 Nov 2021 08:54:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:From:Mime-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=2hhJv8U4V3JbTdD96tDvCVJIrd3RPg4zoervVZ0quwQ=; b=H1ZhQ3K5RQOr6W bqmIZQAlcl4yr1cVxJN5z5lJmfWSs+f2gL2d4Fkker7WKAhOk4LiY9b3vO8hcnHsFHLlo2HQEB25q 7HQW+NapNFeL5PpQKMpwSSVpyuUQT5DYoAbJGPahmUYPUo+OujGWJbPh6CSkOFiY1UKKeP4D1xGlQ sGwRn2HQKlPz0J55DQSy2IN3AkuOOc1QSpXjSq8V4CV4g3xOnvwBHt/4Hr4r+ToBhPHnW1W8a5x+U pJMj3meJZwLv2NrdFkoz1AdRJYEnVwPYHudE5Y8Zr79BXNE60QrRTFBH52dIEBGInbbTVpwNZwqd+ +a/V91BR0RHNAQMhALDA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1moM8P-00CCx4-Hv; Sat, 20 Nov 2021 08:54:09 +0000 Received: from mail-sender-0.a4lg.com ([2401:2500:203:30b:4000:6bfe:4757:0]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1moM8M-00CCvj-S7 for linux-riscv@lists.infradead.org; Sat, 20 Nov 2021 08:54:08 +0000 Message-ID: <2d536b65-e6fd-873f-ed34-4aab7cca4771@irq.a4lg.com> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1637398431; bh=czgyQFHR9/xiTMmJD9prCeQv3aFJRHv2xUZubw9E+SQ=; h=Date:Mime-Version:From:Subject:To:Cc:Content-Type: Content-Transfer-Encoding; b=E3Xt7jxbr1wjbwsBouGmkWYki7kvXi1GAs/BV2504+0W67B1urQ4KWszNMpsOwAVS wbLN+bXylc/g+zQ4x3rvpNvtX5Wrg+x6HtYYdY4wE2XjwRuPkpXnB/wUgqm3IJOl4O 30pTG+RRukjIIAOu5aYerxn4fxw5Xwxd97oYbsG0= Date: Sat, 20 Nov 2021 17:53:50 +0900 Mime-Version: 1.0 From: Tsukasa OI Subject: [RFC PATCH 1/3] riscv: Correctly print supported extensions Content-Language: en-US To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211120_005407_197887_724E635D X-CRM114-Status: UNSURE ( 9.28 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This commit replaces BITS_PER_LONG with magic number 26. Current ISA pretty-printing code expects extension 'a' (bit 0) through 'z' (bit 25). Although bit 26 and higher is not currently used (thus never cause an issue in practice), it will be an annoying problem if we start to use those in the future. This commit disables printing high bits for now. Signed-off-by: Tsukasa OI --- arch/riscv/kernel/cpufeature.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index d959d207a40d..6f2bf6ae4ae2 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -63,7 +63,7 @@ void __init riscv_fill_hwcap(void) { struct device_node *node; const char *isa; - char print_str[BITS_PER_LONG + 1]; + char print_str[26 + 1]; size_t i, j, isa_len; static unsigned long isa2hwcap[256] = {0}; @@ -133,13 +133,13 @@ void __init riscv_fill_hwcap(void) } memset(print_str, 0, sizeof(print_str)); - for (i = 0, j = 0; i < BITS_PER_LONG; i++) + for (i = 0, j = 0; i < 26; i++) if (riscv_isa[0] & BIT_MASK(i)) print_str[j++] = (char)('a' + i); pr_info("riscv: ISA extensions %s\n", print_str); memset(print_str, 0, sizeof(print_str)); - for (i = 0, j = 0; i < BITS_PER_LONG; i++) + for (i = 0, j = 0; i < 26; i++) if (elf_hwcap & BIT_MASK(i)) print_str[j++] = (char)('a' + i); pr_info("riscv: ELF capabilities %s\n", print_str); From patchwork Sat Nov 20 08:53:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12630209 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CB730C433FE for ; Sat, 20 Nov 2021 08:54:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:From:Mime-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=i1oMu1e2Q855PIC3ok+OYjlzV334wBH6/TBREXBAhv0=; b=oBLaUpM9jf0HVw 2LDHgsEftbsS76+Kt6ZHokzyETWtb9qn4jUMJqZ2y4/eORJE8Xf/Ed34LHHGDmuhFbdqU2s2/2+wE iYCKoOlwJwO++lq1/6BzkjApaLdc2d9GQn7+/7VoDDfTOPhht6HK7SpJIaU9v3AtC/zKl2jwHaHzN 3nIIukP/i6YHbFBJEwLvRgoQpa5xL76YNWuGSz5uH5CVoT/t4jLkknu2DSH2a4enYp/+B0TfZ452M xrD7IuZmCcx01+2psLO3PboogbGx4S5JsFutE0uvf96nD6AX7DfqfjlXeXZrFiNa523Hke3MzKTK/ RtUMtDXcMxN1l8mmbHug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1moM8S-00CCxa-Rk; Sat, 20 Nov 2021 08:54:12 +0000 Received: from mail-sender-0.a4lg.com ([2401:2500:203:30b:4000:6bfe:4757:0]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1moM8N-00CCvu-6b for linux-riscv@lists.infradead.org; Sat, 20 Nov 2021 08:54:09 +0000 Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1637398437; bh=XC+Bf8skcAPdUzc9K3K36aEUUJgqfxegbnm7hKZ8cEE=; h=Date:Mime-Version:From:Subject:To:Cc:Content-Type: Content-Transfer-Encoding; b=owE6zLmLKH1/1Db8LCrLYzLLvOhl8Z7h1HkHCK6i05dxVPkD0FsHSoPiqEEJfR2aj lVQZa2lsuKjw+twebCN+FgDn4Ga/x7YP327Pydhde3utU6WPWHdja/jYgokGVQ6fMP h3IjGF9X5/3U+eeKA+TkQkWGGU50F+gEqvztdoYk= Date: Sat, 20 Nov 2021 17:53:56 +0900 Mime-Version: 1.0 From: Tsukasa OI Subject: [RFC PATCH 2/3] riscv: Minimal parser for "riscv,isa" strings Content-Language: en-US To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211120_005407_425821_5DA62945 X-CRM114-Status: GOOD ( 13.13 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Current hart ISA ("riscv,isa") parser don't correctly parse: 1. Multi-letter extensions 2. Version numbers If we don't have those in "riscv,isa", that's fine. However, many of standardized multi-letter extensions are being frozen and ratified. 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. Signed-off-by: Tsukasa OI --- arch/riscv/kernel/cpufeature.c | 56 +++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 6f2bf6ae4ae2..e0d051516746 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -64,7 +65,7 @@ void __init riscv_fill_hwcap(void) struct device_node *node; const char *isa; char print_str[26 + 1]; - size_t i, j, isa_len; + int i, j; static unsigned long isa2hwcap[256] = {0}; isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I; @@ -90,23 +91,56 @@ 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; + int ext_err = 0; + bool ext_long = false; + + if (!('a' <= *isa && *isa <= 'z')) + continue; /* invalid character */ + switch (*isa++) { + case 'h': + case 's': + case 'x': + case 'z': + ext_long = true; + while ('a' <= *isa && *isa <= 'z') + ++isa; + break; + } + if (isdigit(*isa)) { + ext_nstr = isa; + do { + while (isdigit(*++isa)) + ; + if (*isa != 'p') + break; + if (!isdigit(*++isa)) { + ext_err = 2; + break; + } + while (isdigit(*++isa)) + ; + } while (0); + } + 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) { + this_hwcap |= isa2hwcap[(unsigned char)*ext]; + if (!ext_long) + this_isa |= (1UL << (*ext - 'a')); + } } /* From patchwork Sat Nov 20 08:54:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12630213 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0C44CC4332F for ; Sat, 20 Nov 2021 08:54:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:From:Mime-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=haYwkaYRphUm1PFZ8xq6OTl81XgWuo9/LYm21xTQ+B4=; b=CRthhL81xVKz6i eOw8OEI94AOpMh+thtBLhFkfb9CRKNJ6pXtFoFEWG1OXzdddy7KYljvJMb39owK276+XJUWlXPkpL xcU/dN7EcxhtrLvFwP7AmkrhkMRuxzttysnjslAAtmhX1eEOn6z3iq6ah+gbhGrcSFoUtnxnddoua 18nHpwfrm/b9+xIaZQKHfa5f1ZJFx3eRUBSsLnSZFIju3GbWPZvWBdfEr8wj2dKqJdYqlPMlAp/ek XSxWWvzc76EQbmybDNkX9hFtVw/0o7YYTV+Tr2PtOdEKjRA6VCNfIC8Sh3EULJ/oQlpRaXJMFjiks uyth16Y0Sh2yGiiqU1AA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1moM8R-00CCxN-7A; Sat, 20 Nov 2021 08:54:11 +0000 Received: from mail-sender.a4lg.com ([153.120.152.154] helo=mail-sender-0.a4lg.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1moM8M-00CCvv-Vq for linux-riscv@lists.infradead.org; Sat, 20 Nov 2021 08:54:08 +0000 Message-ID: <3f320d5a-c7c6-abf4-3047-cf8f0a089a42@irq.a4lg.com> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1637398441; bh=HqAt0FyYMgnAvZ1uw5UCxfW80Qi6T7x5TwxDWpXDW54=; h=Date:Mime-Version:From:Subject:To:Cc:Content-Type: Content-Transfer-Encoding; b=TY7POxygjJDZgMecxF+C/6XTaPYhNQzU4jgYC4Vo1/S6sjM7nDr87I6mXdIpcfx/B uVztc7z0BQhNTS2N3vDZ26GtlG2sdmXSH2r7OsFZG49V8GecGnOQluEhuNnkkn3E4Z isjA1Zgh9AQgcM7CdDq7KE1gwkW+n6Ykzz3HxT4M= Date: Sat, 20 Nov 2021 17:54:00 +0900 Mime-Version: 1.0 From: Tsukasa OI Subject: [RFC PATCH 3/3] riscv: Full parser for "riscv,isa" strings Content-Language: en-US To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211120_005407_205602_572EB974 X-CRM114-Status: UNSURE ( 9.94 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This commit implements full parser for "riscv,isa" strings. We haven't determined how do we represent multi-letter and/or versioned extensions in the ISA bitmap yet. So, this commit handles only single- letter extensions with no respect to version numbers (as before). Nevertheless, it can be a foundation for our future work. Note that major version of -1 represents non-versioned extension (in many cases, they should be handled as 1 with some exceptions). Signed-off-by: Tsukasa OI --- arch/riscv/kernel/cpufeature.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index e0d051516746..6012a1bb1d20 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -60,6 +60,22 @@ bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, int bit) } EXPORT_SYMBOL_GPL(__riscv_isa_extension_available); +static inline int _decimal_part_to_uint(const char *s, unsigned int *res) +{ + unsigned int value = 0, d; + + if (!isdigit(*s)) + return -EINVAL; + do { + d = *s - '0'; + if (value > (UINT_MAX - d) / 10) + return -ERANGE; + value = value * 10 + d; + } while (isdigit(*++s)); + *res = value; + return 0; +} + void __init riscv_fill_hwcap(void) { struct device_node *node; @@ -100,6 +116,10 @@ void __init riscv_fill_hwcap(void) #endif for (; *isa; ++isa) { const char *ext = isa; + const char *ext_end = isa + 1; + const char *ext_nstr; + unsigned int ext_major = -1; /* default */ + unsigned int ext_minor = 0; int ext_err = 0; bool ext_long = false; @@ -113,6 +133,7 @@ void __init riscv_fill_hwcap(void) ext_long = true; while ('a' <= *isa && *isa <= 'z') ++isa; + ext_end = isa; break; } if (isdigit(*isa)) { @@ -120,14 +141,25 @@ void __init riscv_fill_hwcap(void) do { while (isdigit(*++isa)) ; + if (_decimal_part_to_uint(ext_nstr, + &ext_major) || ext_major == -1) { + ext_err = 1; + break; + } if (*isa != 'p') break; if (!isdigit(*++isa)) { ext_err = 2; break; } + ext_nstr = isa; while (isdigit(*++isa)) ; + if (_decimal_part_to_uint(ext_nstr, + &ext_minor)) { + ext_err = 3; + break; + } } while (0); } if (*isa != '_')