From patchwork Thu Nov 25 10:02:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12638815 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 85AD8C433EF for ; Thu, 25 Nov 2021 10:11:11 +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:Mime-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fLptuDxBhAzUZKnP5qOqs2tEh5B9vpxGLRKU0cB/4yw=; b=TA1VQ+JxSNmVkc E/24PyRXh47FQ67WCIWgtPh1KzgPPkRsRlBe+mB+wDsWvtuJW+D5RPi/PaYRvenfpU+PxkMccelYP gqfT1z3iiOHGr6VKqC6yVjOXz/0VFPSKflyZJQ2+LPPuN1q+Rx8spHn/WrGSDWgjWaVxtyZHTekRJ qzmMazPM6zOjnTuKOOizigPBH0DyB4S9poGdLJQ81I2aI52QKKwDW7LKYSHfVZUqJJeyPkNzdgJUO AZoDIrjyRJXiAjZJZQXTFZwHjf4qltG/WCfhcS/IzgZAWUV1msoaIQXoLeX5BUXwImQddq843vHCE v2Vi9Q57whTejkieO2Kg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqBiU-0072GI-Bf; Thu, 25 Nov 2021 10:10:58 +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 1mqBiR-0072E2-Ph for linux-riscv@lists.infradead.org; Thu, 25 Nov 2021 10:10:57 +0000 From: Tsukasa OI Authentication-Results: mail-sender-0.a4lg.com; dkim=permerror (bad message/signature format) To: Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v2 1/4] riscv: cpufeature: Correctly print supported extensions Date: Thu, 25 Nov 2021 19:02:52 +0900 Message-Id: <4baf931a4f00679b4a2256cc970e733dbd35878a.1637834060.git.research_trasio@irq.a4lg.com> In-Reply-To: References: Mime-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211125_021055_970007_58A902FD X-CRM114-Status: GOOD ( 10.31 ) 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 number of alphabet letters. 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 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index d959d207a40d..dd3d57eb4eea 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -13,6 +13,8 @@ #include #include +#define NUM_ALPHA_EXTS ('z' - 'a' + 1) + unsigned long elf_hwcap __read_mostly; /* Host ISA bitmap */ @@ -63,7 +65,7 @@ void __init riscv_fill_hwcap(void) { struct device_node *node; const char *isa; - char print_str[BITS_PER_LONG + 1]; + char print_str[NUM_ALPHA_EXTS + 1]; size_t i, j, isa_len; static unsigned long isa2hwcap[256] = {0}; @@ -133,13 +135,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 < NUM_ALPHA_EXTS; 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 < NUM_ALPHA_EXTS; i++) if (elf_hwcap & BIT_MASK(i)) print_str[j++] = (char)('a' + i); pr_info("riscv: ELF capabilities %s\n", print_str); From patchwork Thu Nov 25 10:02:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12638819 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 781CCC433FE for ; Thu, 25 Nov 2021 10:11:13 +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:Mime-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=5LIX1adSyLeGwHPPR41jxeKiU2sthiKfTg+YnHUTiAI=; b=ACW4uiURuuVtDd R1h9dLVxJ/pi3xuS96gsjz5eSHQL60xZKUkBn0u+IqxqiDyqVJAN6ZL65ZM8Cu/lPQC+H3q7Ek9oL XJGO/iNnYpmyO8EySYYtNwjkIwX3GpegGcZ7ije4DCU1boUEaVZhiz7u36p5MlWSX0cpUhiC2usqi 2eIf6T/ZIyWL5qJX1jhZhX7g1zZcwBXZYvwKCe3h++FHrlMmKXFj2Ogtk8g2P/Uq4PSQC/rj8mZXa 1b56PyzBXs/j2bukFIjQ6kEEEMj1fzvCizAXCZtg5EdWy0xn2BJuwfK0ejiddkRWD8qHMln1fDd6j CUH23x7HMvzcALrJPHTA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqBib-0072Jb-7D; Thu, 25 Nov 2021 10:11:05 +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 1mqBiR-0072E3-Pk for linux-riscv@lists.infradead.org; Thu, 25 Nov 2021 10:10:58 +0000 From: Tsukasa OI Authentication-Results: mail-sender-0.a4lg.com; dkim=permerror (bad message/signature format) To: Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v2 2/4] riscv: cpufeature: Minimal parser for "riscv, isa" strings Date: Thu, 25 Nov 2021 19:02:53 +0900 Message-Id: <63b22e69b3089224ed2ff489af8b873bbb18ebb4.1637834060.git.research_trasio@irq.a4lg.com> In-Reply-To: References: Mime-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211125_021055_972410_41CDA2C3 X-CRM114-Status: GOOD ( 14.30 ) 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 | 62 ++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index dd3d57eb4eea..93b436addd90 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,62 @@ 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++; + unsigned short ext_err = 0; + bool ext_long; + + switch (*ext) { + case 'h': + case 's': + case 'x': + case 'z': + ext_long = true; + /* Multi-letter extension must be delimited */ + for (; *isa && *isa != '_'; ++isa) + if (!islower(*isa) && !isdigit(*isa)) + ext_err = 1; + /* ... but must be ignored. */ + break; + default: + ext_long = false; + if (!islower(*ext)) { + ext_err = 1; + 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_long && !ext_err) { + this_hwcap |= isa2hwcap[(unsigned char)(*ext)]; + if (!ext_long) + this_isa |= (1UL << (*ext - 'a')); + } } /* From patchwork Thu Nov 25 10:02:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12638813 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 8CB82C433F5 for ; Thu, 25 Nov 2021 10:11:10 +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:Mime-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Rhn2mWLaN/LFxYPr42eegR/bfFLbWYJEkQNN/MrJanw=; b=wNER/KBnI2GkHq QyIq/zFgZw/7+zzmHKTHrkauXiSr9JycWpdPafQn9SZEDj+P6wda8aTIoo5IABntRx406Xi6/RBrC RUBU8of1A0JD7zYI0pXAr5f/Yt29NSXVXU51zXp1nIQz2E1jJBPKK0gqw8Rev6Eh2dy8Jy/lIdIaA 4jT0GZdHqYbT8YBTNh6I893MRTBN5YPCM+ROs7gL7kUN3/1l5ziYxsCzLStF2rQg+KWV6JDbsoF/9 W5K01t3qJ2QLDWdU5gVIeOdcSlCKPmEFg+axcaWYrN09F/w4JvuTRlbbJhi+9mcAeqxD0TySirTr/ akEomOP2w7fcYrPiWABg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqBiW-0072Hc-Lu; Thu, 25 Nov 2021 10:11:00 +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 1mqBiR-0072E5-Oq for linux-riscv@lists.infradead.org; Thu, 25 Nov 2021 10:10:57 +0000 From: Tsukasa OI Authentication-Results: mail-sender-0.a4lg.com; dkim=permerror (bad message/signature format) To: Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v2 3/4] riscv: cpufeature: Extract extension names from "riscv, isa" Date: Thu, 25 Nov 2021 19:02:54 +0900 Message-Id: In-Reply-To: References: Mime-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211125_021055_938698_9BA7BBD4 X-CRM114-Status: UNSURE ( 9.99 ) 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 It's possible that we only need extension names implemented but not version numbers. This commit doesn't parse version numbers but does extract implemented extension names. `ext_end-1` points at the last character of the extension name and `ext_end-ext` is the length of the extension name. Beware that the extension name is **not** null-terminated. Signed-off-by: Tsukasa OI --- arch/riscv/kernel/cpufeature.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 93b436addd90..61bc326d15b0 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -102,6 +102,7 @@ void __init riscv_fill_hwcap(void) #endif for (; *isa; ++isa) { const char *ext = isa++; + const char *ext_end = isa; unsigned short ext_err = 0; bool ext_long; @@ -115,7 +116,21 @@ void __init riscv_fill_hwcap(void) for (; *isa && *isa != '_'; ++isa) if (!islower(*isa) && !isdigit(*isa)) ext_err = 1; - /* ... but must be ignored. */ + /* Find end of the extension name backwards */ + ext_end = isa; + if (ext_err) + break; + if (!isdigit(ext_end[-1])) + break; + while (isdigit(*--ext_end)) + ; + if (ext_end[0] != 'p' + || !isdigit(ext_end[-1])) { + ++ext_end; + break; + } + while (isdigit(*--ext_end)) + ; break; default: ext_long = false; @@ -144,7 +159,7 @@ void __init riscv_fill_hwcap(void) * TODO: Full version-aware handling including * multi-letter extensions will be added in-future. */ - if (!ext_long && !ext_err) { + if (!ext_err) { this_hwcap |= isa2hwcap[(unsigned char)(*ext)]; if (!ext_long) this_isa |= (1UL << (*ext - 'a')); From patchwork Thu Nov 25 10:02:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12638817 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 7B95BC433F5 for ; Thu, 25 Nov 2021 10:11:12 +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:Mime-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=K7AHkou1vpX2Ma/SkMwXncxWcD0QjLnD4c8xodBQats=; b=ZWs9lO9R78ScKt CjdXSx4CCE4gmHl+Jyz/IrSGEO/Z1uJ+d8GHoExrP/8iaXd4qpWuwaWq3qAvZtnDyO8ko/6FOZ+OI 1sDY+0ckQP/fhtjWnwC55WazBUr8aREWGs2pjk1FLE6Ie6HUEqrvOzb9A6XsSrIimwPiQmCsNE8fU zOtI1ZS79MQ/qQiDvUUK62O3E+l9KMUV7YyTVvnkOXA91mc3gscCWvKaQUkUgEJ2hyKS7ppIicqN4 3SwkSVSUF3U66qV67Hop2N3yC0IdBmrhbyEyw/+GdWHY24o/wNsBBVuJseUyJDdfvoUrASPtZc+yi /jZ6UT/0bDaf6pplz8AA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mqBiZ-0072IV-0v; Thu, 25 Nov 2021 10:11:03 +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 1mqBiR-0072E1-Oi for linux-riscv@lists.infradead.org; Thu, 25 Nov 2021 10:10:58 +0000 From: Tsukasa OI Authentication-Results: mail-sender-0.a4lg.com; dkim=permerror (bad message/signature format) To: Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v2 4/4] riscv: cpufeature: Full parser for "riscv, isa" strings Date: Thu, 25 Nov 2021 19:02:55 +0900 Message-Id: In-Reply-To: References: Mime-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211125_021056_078341_E52F7E93 X-CRM114-Status: GOOD ( 13.47 ) 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 UINT_MAX 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 | 44 ++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 61bc326d15b0..8c64f63a686c 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -62,6 +62,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; @@ -103,8 +119,10 @@ void __init riscv_fill_hwcap(void) for (; *isa; ++isa) { const char *ext = isa++; const char *ext_end = isa; + unsigned int ext_major = UINT_MAX; /* default */ + unsigned int ext_minor = 0; unsigned short ext_err = 0; - bool ext_long; + bool ext_long, ext_vpair; switch (*ext) { case 'h': @@ -116,7 +134,7 @@ void __init riscv_fill_hwcap(void) for (; *isa && *isa != '_'; ++isa) if (!islower(*isa) && !isdigit(*isa)) ext_err = 1; - /* Find end of the extension name backwards */ + /* Parse backwards */ ext_end = isa; if (ext_err) break; @@ -124,13 +142,23 @@ void __init riscv_fill_hwcap(void) break; while (isdigit(*--ext_end)) ; - if (ext_end[0] != 'p' - || !isdigit(ext_end[-1])) { + ext_vpair = (ext_end[0] == 'p') + && isdigit(ext_end[-1]); + if (_decimal_part_to_uint(ext_end + 1, + &ext_major)) + ext_err = ext_vpair ? 3 : 2; + if (!ext_vpair) { ++ext_end; + if (ext_major == UINT_MAX) + ext_err = 2; break; } + ext_minor = ext_major; while (isdigit(*--ext_end)) ; + if (_decimal_part_to_uint(++ext_end, &ext_major) + || ext_major == UINT_MAX) + ext_err = 2; break; default: ext_long = false; @@ -138,9 +166,12 @@ void __init riscv_fill_hwcap(void) ext_err = 1; break; } - /* Find next extension */ + /* Parse forwards finding next extension */ if (!isdigit(*isa)) break; + _decimal_part_to_uint(isa, &ext_major); + if (ext_major == UINT_MAX) + ext_err = 2; while (isdigit(*++isa)) ; if (*isa != 'p') @@ -149,6 +180,9 @@ void __init riscv_fill_hwcap(void) --isa; break; } + if (!ext_err && + _decimal_part_to_uint(isa, &ext_minor)) + ext_err = 3; while (isdigit(*++isa)) ; break;