Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2707954pxm; Mon, 28 Feb 2022 04:48:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2HQWpsF8Rl59TA8rO9H8ZMIZAWNBxpadNGbjhY7WolUrW/rp44sc7LgCtZTK6ktyO3upr X-Received: by 2002:a17:906:d1c4:b0:6d5:83bb:f58a with SMTP id bs4-20020a170906d1c400b006d583bbf58amr15040109ejb.672.1646052526187; Mon, 28 Feb 2022 04:48:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646052526; cv=none; d=google.com; s=arc-20160816; b=Qekf6YEs+4x7JiXRSJJl4VXA9z0TRBLCA7lthPLkNOAsxsyxgkeSpkKC67el0rs/jv kXzRvSCtiggnmi/PjcgWqwkrBeZrB2rIc+w7tL6ms7jTpDim5frSpnTWhP4+3cw70DQq Np91nQFsv5N2idTqrFLhp1WCqOkpJpmjgPzoXmiMKBwXnJdQePybncVRmjDTIZZyQEak ykgR8gOkxdd+Ah8hBthF5LToJ5gI/XGca8h3YQ/wI+j0VxPIAj/DcyM5WLo7ciVMYFXQ q5LjWmdiIDw2NpNQd2cHey7/EkKACY5fjl/TrMVDuFoCx12gjbnt5YNRJGRfwTj4f7b2 huIg== 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=De2OyZDcwN8o9X6MlMOt7Z7zT/UnfJwRn/VzFf9UnSs=; b=QICR/Bmfvf3hih7P//EUO5PJ8BoHP9C4l61mQpco4S/xbzRJhO2zZdKJ9jmQX76OOS Yf4CkNBYTyIPAUynJZht+4teJb3pAS46S8bGtWWywxhCvUsNbH0vedk3JYszbDWaeCt4 qdzDGQXqW5/bMEtp1fnG0p3W0lUyIMevK5BKPYJdi2UnGu7Qu4irgxmuErs3KOT9UQdq j0klAAj0Y9qbHCNz3H2QFjZueKfOkxRxYXD6z6+1I43Jxa38JqT5qRwklBzFLpEEXX65 vvbhwDUFugHFaYV2hsCUma/DUD3QdwUFHXSVF+4BCU9QvkAye/+h8fv7M4B3Uxj6Xi6k wsmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=fJStaRDE; 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 i24-20020a1709067a5800b006cfe1cc4d9fsi5969488ejo.460.2022.02.28.04.48.23; Mon, 28 Feb 2022 04:48:46 -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=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=fJStaRDE; 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 S232952AbiB1KE2 (ORCPT + 99 others); Mon, 28 Feb 2022 05:04:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232842AbiB1KEZ (ORCPT ); Mon, 28 Feb 2022 05:04:25 -0500 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FB072B25B for ; Mon, 28 Feb 2022 02:03:44 -0800 (PST) Received: by mail-wr1-x435.google.com with SMTP id d17so14472670wrc.9 for ; Mon, 28 Feb 2022 02:03:44 -0800 (PST) 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=De2OyZDcwN8o9X6MlMOt7Z7zT/UnfJwRn/VzFf9UnSs=; b=fJStaRDEFlrEswt+fpTK2NKKLCysIBk9+9As1XpWnYiXYb6WSBa9HMnYv1oALWt90b FWgM/N0s7lfVF4sLe0v2KvjRyHhNeEIwIvYNQsYvW5Vo92TpqFg4oeKlyJvv+Ru/VVeE PIOle2bi4UKKxz8KUOAzet02s2pLc2xouIe4Sd5nd4sgiXp0u5P47NozZOMp1ZHITyEg eoqOONuIJbEE4jlWLGf5kC4J+nMw8v345/VYKZ2d9o2eV1+JJsxk+mmY9Sro15+rdDHs ZY0XJ+Tjpk6hWW9iDbEWHdtNd0Bwt7dQUFK1eTaz9eryP9rbmsRILoDtAkodgUGVolBc SEmg== 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=De2OyZDcwN8o9X6MlMOt7Z7zT/UnfJwRn/VzFf9UnSs=; b=d9V0vcD88VM4d0Q/Z/YR53AygTRu5iYzNm9TvgZVoI9isFijuvXX4wptYkI/T39I2h S+58maXMp/i7bxa0lYN4JwX0+oqaTrHkevzSDsYEX2ETCnQW/zvaxPkiMxnaQ6YSJ4O8 6n1mBNJNwQYX9EgyiKmacs1j3//rrFjHGU1yD9qqcdiAPjR2lhhjea4JoM30WpoApmKg /FbWOUEnFZGcxqsmJSN09PgQ4EbfTSXOuBCgEZdVevJEo2E7cvIx0YsYCzUwAf4tTAEj 4WlxBBoOm1HJhzMnF++UiaVUe+AcdeMsZgybNDK9IqLP6JgcZLgK9Eg++yQv78xoa/FY YXMg== X-Gm-Message-State: AOAM533XO9PR9qHOfCtm8XeCHmQezXh35pmoZh/BlikrlgFh1Mc5i3PV dVLqH7TE67U9sbgbeJlRh6MTxFxU+B2fzw9NlGUF0A== X-Received: by 2002:a5d:6c6b:0:b0:1ea:77ea:dde8 with SMTP id r11-20020a5d6c6b000000b001ea77eadde8mr15298582wrz.690.1646042623031; Mon, 28 Feb 2022 02:03:43 -0800 (PST) MIME-Version: 1.0 References: <20220222204811.2281949-1-atishp@rivosinc.com> <20220222204811.2281949-3-atishp@rivosinc.com> In-Reply-To: <20220222204811.2281949-3-atishp@rivosinc.com> From: Anup Patel Date: Mon, 28 Feb 2022 15:33:24 +0530 Message-ID: Subject: Re: [PATCH v5 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,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham 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 Wed, Feb 23, 2022 at 2:18 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 Looks good to me. 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 >