From patchwork Thu Dec 2 01:41:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12651451 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 22137C433EF for ; Thu, 2 Dec 2021 01:41:32 +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=sU1geLV2lMCxtr VJIjhFAIZIjueZXmMo5aMWgdU6KbxvZvN2UKx6viC4jhye0baWI49ZBI3WmIkrUyjMDJMZZdMAnKd qrDvAZf2S9+J9cIcJParnEbdFZBcsZuyZFeG1sqcy8v1lG2dPoVd3OydoRPKQR5s2DUk7dxo/Qt36 pr3tKag6QKaVIG16Z5uXZPLytj2GLCVZUTXSOcxqyJBkWeH4CBP+m+FKWTWlIVzbLdnFU+yELqqGr VFDdcm0qfp4hQMUUI2QaYdF1trwyNbjo6itPKEEc0uZNCTF0gbDgkZg8k27kdFUpoNwl1ByGJOI3M ef/m1EAc9U3A8uGBIOBQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1msb6B-00AYFi-2W; Thu, 02 Dec 2021 01:41:23 +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 1msb67-00AYDi-C7 for linux-riscv@lists.infradead.org; Thu, 02 Dec 2021 01:41:20 +0000 Received: by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id D1B4C30008A; Thu, 2 Dec 2021 01:41:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1638409278; bh=cl3Yajb8pHF7xTBnBrLc/N3QGlcY0whqHS4G6/ABpBg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Mime-Version:Content-Transfer-Encoding; b=IMoxL7ZuXiGlODj0OTQk+RbWUgBpWuHpLwJ1TCItKldE3TQv2xt7Z/wgyhxmMvQks lliqGoWETAe8dVKHJ75tzKTFbRbxEprkd12xI6VJdhIod4IETygze3WB9cnKBHRAAc GJB3KTDZWXfMPNrgxisyCXqL3qRVCWvwCE99dNBw= From: Tsukasa OI To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , =?utf-8?q?Heiko_St=C3=BCbner?= , Philipp Tomsich Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v3 1/4] riscv: cpufeature: Correctly print supported extensions Date: Thu, 2 Dec 2021 10:41:10 +0900 Message-Id: <4baf931a4f00679b4a2256cc970e733dbd35878a.1638408984.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-20211201_174119_587340_8133B197 X-CRM114-Status: GOOD ( 11.02 ) 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 Dec 2 01:41:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12651455 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 6DC5DC433F5 for ; Thu, 2 Dec 2021 01:41:35 +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=lmVUY3+PSaQgn5OqHy4NNl38jFHy2rknOnDUyqZYLWc=; b=Pe2+XQSrhlNH5Y aIMJg06IfhyPRyvu+X/fm0iI7xp6woXvM9ZSNUKV2sve+BPxqjVXcc+iIytS2wLgm2984833Zrdzz 7X36eTm16A8g72kSjUJb3u4qer0oNCHbecfF+csIZS27dfEsFzSj5oP8XdO22GH32ANz/PGihco0x ZXuWCRQfbv8tRgBmumrIeLQwvrpPMN+6Y3VP0xhjvBPzXE7XHDjo7BO8NKlVNJ5r9xr1oYXJhnPdy XsA9IQLZwT7Y/wH7lvq5nHbTErIIZhEa6hT5VlEdjBpI93MLui1Lu/aqTCnNGqfQ+qThqiu1lUw64 LvjtnPt2YZzt4+ts9rpw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1msb6F-00AYGu-Iz; Thu, 02 Dec 2021 01:41:27 +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 1msb67-00AYDl-Ia for linux-riscv@lists.infradead.org; Thu, 02 Dec 2021 01:41:22 +0000 Received: by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id 30F8430008B; Thu, 2 Dec 2021 01:41:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1638409278; bh=ZdGl0qalCS4I0KP/XDyrydd7SPj1NUBuXZ0Ct+X1Nyg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Mime-Version:Content-Transfer-Encoding; b=aVMzgTMNKdhFCguFBUQaru+NfZ093ZSdxskX42Gg7CraetSeXHht78jzoB3D7JCTn I61FoBz/m7RCTOLocPH7Rc/jXAaAURn5ICj+gJ8j8/wZ5YWn86ayFxVMPoIEu7ht3U I6ybGv4Rb1vhRHXxrypenL/jhgcarCiHyEawBfNA= From: Tsukasa OI To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , =?utf-8?q?Heiko_St=C3=BCbner?= , Philipp Tomsich Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v3 2/4] riscv: cpufeature: Minimal parser for "riscv, isa" strings Date: Thu, 2 Dec 2021 10:41:11 +0900 Message-Id: <6c98f35075a5f2b2a2b39b5b45ca369071aadfff.1638408984.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-20211201_174119_910662_AEB45FB2 X-CRM114-Status: GOOD ( 15.46 ) 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. Important Note: This commit does handle "H" as a single-letter extension. This handling is not compliant with current version of RISC-V ISA Manual but will going to be in a few months. Signed-off-by: Tsukasa OI --- arch/riscv/kernel/cpufeature.c | 59 +++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index dd3d57eb4eea..d4ff704db53b 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,59 @@ 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++; + bool ext_long, ext_err = false; + + switch (*ext) { + 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 = true; + /* ... but must be ignored. */ + break; + default: + ext_long = false; + if (!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')); } /* From patchwork Thu Dec 2 01:41:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12651457 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 C6B2AC433EF for ; Thu, 2 Dec 2021 01:41:35 +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=fyrMf/r2u4pzVoEulCFFNGgTnEcxmlGApGAji5atjDs=; b=WDs9bIFdOHBY9r 3xuZd3oddtgx/GAimlm2Jj6rIU4nppmFXp4R991D5nV/ZbKSfHbf+fOct5wlL0zgaYSl3tPgtNIcF hvGBoNnqyo2B7nyNNOSPlz4mGD1BtO2FS+o0zYwBH4SzsBS0cVKJ2yHYCw9SKaje2DImdDdKQuJRY p3pGW+l6xQor8dpS39IpH8GW9Cd9nG9MkL5Drlyi5kCQmr7jEJKEk4DCNOTtIbZc7T0QDC5jcFiz2 Q4UScZbEYNWWUAGUupvJ9v2YLNV3gIe4sDo5/N11BH5NCbvZaU7RxjKOS8V5WejEiOtlCCGa7v5i1 wo8ajsiWNd3PyhczCAbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1msb6H-00AYHg-QA; Thu, 02 Dec 2021 01:41:29 +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 1msb69-00AYDr-3y for linux-riscv@lists.infradead.org; Thu, 02 Dec 2021 01:41:22 +0000 Received: by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id 8562430008C; Thu, 2 Dec 2021 01:41:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1638409278; bh=tCMBtDoyFd+IH9AraQVrUP8QPKlxc09CeUEG/VxdJsg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Mime-Version:Content-Transfer-Encoding; b=LlGb91mI06Lq9AR7g4wpjb3Izl6Iocj8RUy6u1oFe8QBTUnPBh5teKOK2bby4kFZW Um2ArvC3zQIqUwvC4yiWL7SUAQckPV4l0VbKnrko5XSFHgTaHnRHjPzMA18kGnk0Ia pGdmhrfppoa0KXsO+iTFq6qi6GyaqIbV9DojC0SI= From: Tsukasa OI To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , =?utf-8?q?Heiko_St=C3=BCbner?= , Philipp Tomsich Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v3 3/4] riscv: cpufeature: Extract extension names from "riscv, isa" Date: Thu, 2 Dec 2021 10:41:12 +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-20211201_174121_323795_F269DAA6 X-CRM114-Status: GOOD ( 11.36 ) 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. Beware that the extension name is **not** null-terminated. Use `MATCH_EXT` macro to match extension name (the argument is lower-case constant string). Signed-off-by: Tsukasa OI --- arch/riscv/kernel/cpufeature.c | 35 +++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index d4ff704db53b..f52e15488a70 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; bool ext_long, ext_err = false; switch (*ext) { @@ -111,13 +112,28 @@ void __init riscv_fill_hwcap(void) ext_long = true; /* Multi-letter extension must be delimited */ for (; *isa && *isa != '_'; ++isa) - if (!islower(*isa) && !isdigit(*isa)) + if (unlikely(!islower(*isa) + && !isdigit(*isa))) ext_err = true; - /* ... but must be ignored. */ + /* Find end of the extension name backwards */ + ext_end = isa; + if (unlikely(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; - if (!islower(*ext)) { + if (unlikely(!islower(*ext))) { ext_err = true; break; } @@ -138,14 +154,15 @@ void __init riscv_fill_hwcap(void) } if (*isa != '_') --isa; - /* - * TODO: Full version-aware handling including - * multi-letter extensions will be added in-future. - */ - if (ext_err || ext_long) + +#define MATCH_EXT(name) (ext_end - ext == sizeof(name) - 1 \ + && !memcmp(ext, name, sizeof(name) - 1)) + if (unlikely(ext_err)) continue; this_hwcap |= isa2hwcap[(unsigned char)(*ext)]; - this_isa |= (1UL << (*ext - 'a')); + if (!ext_long) + this_isa |= (1UL << (*ext - 'a')); +#undef MATCH_EXT } /* From patchwork Thu Dec 2 01:41:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 12651459 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 0FCB4C433EF for ; Thu, 2 Dec 2021 01:41:41 +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=KMyLxTu1Nuw7ROdEIuLSjm698RHE71shQpHFeNI46xo=; b=3/dCR/991P03ZJ QB+Ab2AAHsSrRkdxjrjLl3s5eDeSFrmKcM0du2cNHdnK0IaaAyQdZ3oZBHvBGMY8RSCssl9Cc0Jrn 3tyXOUaFcyGJGbxVMmjuseoO9gTXNSUgerYl8EGsbAZb4ROAbQot03O05xK3tUXiISoU/uU83/bNq PpI6pA0z1Dsbx7aIl/UGh/dHh40AZ7oXftz16OZ0aXHkkd9NEivNsCKL1f/sWqUFJloIJSgCmZAYA 8TrgHcx+F87v4yFExhFlxZER/5u8rdTNOzHMtIIOj49Dx3//AsbYhgs+CMf9Es2NZmOn3k4C/0bMH gCsZfGMYAfgFSC4fPEYQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1msb6J-00AYIv-Ql; Thu, 02 Dec 2021 01:41:31 +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 1msb69-00AYDt-3w for linux-riscv@lists.infradead.org; Thu, 02 Dec 2021 01:41:23 +0000 Received: by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id D7BB330008D; Thu, 2 Dec 2021 01:41:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1638409279; bh=7m28+LuBhM5e7rQvW2rS3EYTOsKXkMFFoLQlpkav8Ys=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Mime-Version:Content-Transfer-Encoding; b=csuKorBnghi/vvwRM4ISSgxu8C0NU1ydC2o+xMZ+KtE2W3PSkB/pg7aAVKgm9lZ9/ yu51To3BARhBS/lCYlm9YkqBzUE2FQfiLBVUJoWKK+F+zBLJxXmFmM2Ct/6SoAkzEP 4ZdjZbf10PtHqdYTFvNLA3E+TtT4aE2snKTNdKQY= From: Tsukasa OI To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Atish Patra , =?utf-8?q?Heiko_St=C3=BCbner?= , Philipp Tomsich Cc: Tsukasa OI , linux-riscv@lists.infradead.org Subject: [RFC PATCH v3 4/4] riscv: cpufeature: Full parser for "riscv, isa" strings Date: Thu, 2 Dec 2021 10:41:13 +0900 Message-Id: <531a8daa35d206315367b2997615ee0618b319d6.1638408984.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-20211201_174121_446940_C556A33E X-CRM114-Status: GOOD ( 13.70 ) 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 | 42 ++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index f52e15488a70..a462ceb959d9 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,7 +119,10 @@ void __init riscv_fill_hwcap(void) for (; *isa; ++isa) { const char *ext = isa++; const char *ext_end = isa; - bool ext_long, ext_err = false; + unsigned int ext_major = UINT_MAX; /* default */ + unsigned int ext_minor = 0; + bool ext_long, ext_vpair, + ext_err = false, ext_err_ver = false; switch (*ext) { case 's': @@ -115,7 +134,7 @@ void __init riscv_fill_hwcap(void) if (unlikely(!islower(*isa) && !isdigit(*isa))) ext_err = true; - /* Find end of the extension name backwards */ + /* Parse backwards */ ext_end = isa; if (unlikely(ext_err)) break; @@ -123,13 +142,21 @@ 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_ver = true; + if (!ext_vpair) { ++ext_end; break; } + ext_minor = ext_major; while (isdigit(*--ext_end)) ; + if (_decimal_part_to_uint(++ext_end, &ext_major) + || ext_major == UINT_MAX) + ext_err_ver = true; break; default: ext_long = false; @@ -137,9 +164,12 @@ void __init riscv_fill_hwcap(void) ext_err = true; 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_ver = true; while (isdigit(*++isa)) ; if (*isa != 'p') @@ -148,6 +178,8 @@ void __init riscv_fill_hwcap(void) --isa; break; } + if (_decimal_part_to_uint(isa, &ext_minor)) + ext_err_ver = true; while (isdigit(*++isa)) ; break;