Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp950299pxp; Wed, 16 Mar 2022 22:22:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyI1mNg4HqTXMNOdh7SpyqCt2nuAgVctb7RolnyruxTHcBogkH+rioxo2xdG91cCKNgzHsE X-Received: by 2002:a17:90a:a510:b0:1bc:5887:d957 with SMTP id a16-20020a17090aa51000b001bc5887d957mr13785471pjq.38.1647494554805; Wed, 16 Mar 2022 22:22:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647494554; cv=none; d=google.com; s=arc-20160816; b=gghxN5qfxLMyLUuQ8Weafc9LruaUGfurmnSWGCIXnxHd99eXKtnRXuJjlEl8/nW6Pg fgj4QBWKUf52bZaEWNRdCDxv7r0jRRFhL+9J5tvtzWlfnCVDpehPs3EJgdq/9d4MyYUe 9bTUQrMrtAbBBgyGyz0fLh8CiI2h/FqSlk5rwKjD+N+Fv77v1rhqQGEyxnRT2NUa72lg QeunYiq1sjbcealnUTWNHHHRY+iYoIsGvfR3dr/24Wt2x+gncYKT64Fr1f+XktLihoeg 3wCHxqmnyNnQXQ+HGyuO4gO+JMYCNzOkpbumQEcsY2NuWueFc/xneonWiLEby/Zr2GTd 1zyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=YlAUUoV15oIq0bfUkEEQvam2TX7UDNlQRyUb6o8Uzlc=; b=R/Gt4t7wjRppEOQS2vSy3C6o0A6/tXCOKvnx+C2UAygZNU5VPdkh5ZLR3xgb6yLVnv yfpqRwksbmeQMTQQekkpOQIZW4LAePxrxgdjzDQ4iP8ul8ccUQfByTZ0rgUIyPLbitQq OEj2KD5M1sOOUEFwxfy49HOFZHfd/yXS4HVQI1of65FkCK1XK2cjpDvo/t9qNpUSkfRp gIHJdC02d2xwqpOP3kGTq4/BzLHCzxmyNNOLr1fWs1GTKUYlPpkQnVOlu8fxtuRnqnBV uEa0Xf1L3+n4HnCe5wdSqiwm/MUc475txTHXH6G9zL8FxM7ixlDF45f8BtuDvu1K6Ayo pntw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b="3D0/VuzR"; 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 Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id s3-20020a170902ea0300b00151f80de2e5si3733116plg.606.2022.03.16.22.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 22:22:34 -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=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b="3D0/VuzR"; 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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 47F6C216A7B; Wed, 16 Mar 2022 21:28:33 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344563AbiCODKQ (ORCPT + 99 others); Mon, 14 Mar 2022 23:10:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344555AbiCODKO (ORCPT ); Mon, 14 Mar 2022 23:10:14 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C473F12771 for ; Mon, 14 Mar 2022 20:09:00 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id n33-20020a05600c3ba100b003832caf7f3aso717509wms.0 for ; Mon, 14 Mar 2022 20:09:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=YlAUUoV15oIq0bfUkEEQvam2TX7UDNlQRyUb6o8Uzlc=; b=3D0/VuzRE5jVE1gU0WP/Oy/EhATOtCMCyTpruJzRr8ydYVT0gTX/boRnslD4WQy256 pY16gkSplQ4WewRozBZ6bp3Rsh97puqjVxsahRK/RJKAWgQdH2Jper+1W6hIqF0sB4MV pYPh/XG4b9G6cwrtClJq0co3NTwj/1JolR5lihHLRvKUXUp74PAmn5SgCQ9YDFJFR31z WRsxDRbkQk3nn6V6nC1+rk/13pI48GkNP4cGiNOAV5i9FhsQXTL0Xix+GUQgBtLA8nnH tuux+3XqjRSlbwacu9iNpov5T3uRPFHY2j1uyz5AHHLunssgnHKYEYzhIu3E4OCDxHxc GkgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=YlAUUoV15oIq0bfUkEEQvam2TX7UDNlQRyUb6o8Uzlc=; b=Jsri+rsgEMBHPAVdu+RbS1StyqIRRoenyXfnvJYqEiwieCdExEFwXFOom8QS4/rUO9 JMz7AJW6J06qZeGL2dOfHVDgd0Hkz4gE4FEQ2LOhh6OGvHcSpqfTGtPVTss04zirEFar QFwHEeIPxkizSDfEoD/6LDpIqtvtt2crU5uzuNdpQHwglCpg1YJmN8NPwiBRYOn9lusA eoG9oblNzng7AI5fSYiDvsOa0/c+Zr8MIDI/LayTGZE/G2fR0iQn7UOVRx1KdOc657cv +hQhUVEPy3HNso2F0JTeiPtlOTV7dLVJmOkV9Uei693BwzfImJZmAVQ3317dcokzRwdp Sxhg== X-Gm-Message-State: AOAM5339tsW6Ux/tVOHDwJSpHjpDxV7GjX8EhSk5XSTcg1U9vP7YO48J bnZhhDw5mWupPX1hAvlPT7Gw6urvV6KeLIsH55DeBQ== X-Received: by 2002:a05:600c:2c49:b0:384:7202:358e with SMTP id r9-20020a05600c2c4900b003847202358emr1556102wmg.108.1647313739201; Mon, 14 Mar 2022 20:08:59 -0700 (PDT) MIME-Version: 1.0 References: <20220314203845.832648-1-atishp@rivosinc.com> <20220314203845.832648-3-atishp@rivosinc.com> In-Reply-To: <20220314203845.832648-3-atishp@rivosinc.com> From: Anup Patel Date: Tue, 15 Mar 2022 08:38:46 +0530 Message-ID: Subject: Re: [PATCH v6 2/6] RISC-V: Minimal parser for "riscv, isa" strings To: Atish Patra Cc: "linux-kernel@vger.kernel.org List" , Tsukasa OI , Heiko Stuebner , Albert Ou , Atish Patra , Damien Le Moal , DTML , Jisheng Zhang , Krzysztof Kozlowski , linux-riscv , Palmer Dabbelt , Paul Walmsley , Rob Herring Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,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 Tue, Mar 15, 2022 at 2:09 AM Atish Patra wrote: > > 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 I think I have reviewed most of the patches in this series. Reviewed-by: Anup Patel Regards, Anup > --- > arch/riscv/kernel/cpufeature.c | 72 ++++++++++++++++++++++++++++------ > 1 file changed, 61 insertions(+), 11 deletions(-) > > diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c > index dd3d57eb4eea..72c5f6ef56b5 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,72 @@ 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': > + /** > + * Workaround for invalid single-letter 's' & 'u'(QEMU). > + * No need to set the bit in riscv_isa as 's' & 'u' are > + * not valid ISA extensions. It works until multi-letter > + * extension starting with "Su" appears. > + */ > + if (ext[-1] != '_' && ext[1] == 'u') { > + ++isa; > + ext_err = true; > + break; > + } > + fallthrough; > + 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; > + break; > + default: > + if (unlikely(!islower(*ext))) { > + ext_err = true; > + break; > + } > + /* Find next extension */ > + if (!isdigit(*isa)) > + break; > + /* Skip the minor version */ > + while (isdigit(*++isa)) > + ; > + if (*isa != 'p') > + break; > + if (!isdigit(*++isa)) { > + --isa; > + break; > + } > + /* Skip the major version */ > + 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 >