riscv: riscv,isa fixes + check script
cover riscv: riscv,isa fixes + check script
Hi,
We have an array called isa_edata_arr[] in target/riscv/cpu.c which needs to be always kept in the RISC-V specification riscv,isa order. Easier said that done: as more and more extensions are added we're failing to keep up with the array ordering in the review process.
I have considered changing how we're retrieving riscv,isa to not rely on the array ordering (in fact I have code that does that). We would sort the enabled extensions using riscv,is ordering during init time, before writing it in the DT, ignoring the current isa_edata_arr ordering. When all was said and done that sounded a bit extreme and I think there's other stuff we can try first.
Patch 2 in this series is a python script I put together to, hopefully help us identify if we're messing up the isa_edata_arr ordering and fix it before pushing changes upstream. I used it to validate the changes I did in patch 1 (the usual 'fix isa_edata_arr ordering' patch) and I can tell that the script picked up *way* more ordering errors that I was able to identify on my own.
I didn't made any CI related changes w.r.t this new script but it is something to consider - if changes in target/riscv/cpu.c are made, run this script to validate the array again.
Patches based on alistair/riscv-to-apply.next.
Daniel Henrique Barboza (2):
target/riscv/cpu.c: isa_edata_arr[] ordering fixes
scripts: RISC-V python script to check isa_edata_arr[]
scripts/riscv-isaedata-check.py | 164 ++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 17 ++-- 2 files changed, 174 insertions(+), 7 deletions(-) create mode 100755 scripts/riscv-isaedata-check.py
Signature · 1 line
2.51.1
PATCH 1/2 target/riscv/cpu.c: isa_edata_arr[] ordering fixes
Yet another patch to fix the ordering of isa_edata_arr[].
The ordering errors was found by a python script that we'll be adding in the next patch to, hopefully, automate the ordering of this array for us.
--- target/riscv/cpu.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
target/riscv/cpu.c +7 -7
@@ -114,8 +114,8 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (ziccrse,PRIV_VERSION_1_11_0 , ext_ziccrse),ISA_EXT_DATA_ENTRY (zicfilp,PRIV_VERSION_1_12_0 , ext_zicfilp),ISA_EXT_DATA_ENTRY (zicfiss,PRIV_VERSION_1_13_0 , ext_zicfiss), -ISA_EXT_DATA_ENTRY (zicond,PRIV_VERSION_1_12_0 , ext_zicond),ISA_EXT_DATA_ENTRY (zicntr,PRIV_VERSION_1_12_0 , ext_zicntr), +ISA_EXT_DATA_ENTRY (zicond,PRIV_VERSION_1_12_0 , ext_zicond),ISA_EXT_DATA_ENTRY (zicsr,PRIV_VERSION_1_10_0 , ext_zicsr),ISA_EXT_DATA_ENTRY (zifencei,PRIV_VERSION_1_10_0 , ext_zifencei), ISA_EXT_DATA_ENTRY(zihintntl, PRIV_VERSION_1_10_0, ext_zihintntl),
@@ -138,9 +138,9 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zdinx,PRIV_VERSION_1_12_0 , ext_zdinx),ISA_EXT_DATA_ENTRY (zca,PRIV_VERSION_1_12_0 , ext_zca),ISA_EXT_DATA_ENTRY (zcb,PRIV_VERSION_1_12_0 , ext_zcb), -ISA_EXT_DATA_ENTRY (zcf,PRIV_VERSION_1_12_0 , ext_zcf),ISA_EXT_DATA_ENTRY (zcd,PRIV_VERSION_1_12_0 , ext_zcd),ISA_EXT_DATA_ENTRY (zce,PRIV_VERSION_1_12_0 , ext_zce), +ISA_EXT_DATA_ENTRY (zcf,PRIV_VERSION_1_12_0 , ext_zcf),ISA_EXT_DATA_ENTRY (zcmop,PRIV_VERSION_1_13_0 , ext_zcmop),ISA_EXT_DATA_ENTRY (zcmp,PRIV_VERSION_1_12_0 , ext_zcmp), ISA_EXT_DATA_ENTRY(zcmt, PRIV_VERSION_1_12_0, ext_zcmt),
@@ -166,8 +166,8 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zvbc,PRIV_VERSION_1_12_0 , ext_zvbc),ISA_EXT_DATA_ENTRY (zve32f,PRIV_VERSION_1_10_0 , ext_zve32f),ISA_EXT_DATA_ENTRY (zve32x,PRIV_VERSION_1_10_0 , ext_zve32x), -ISA_EXT_DATA_ENTRY (zve64f,PRIV_VERSION_1_10_0 , ext_zve64f),ISA_EXT_DATA_ENTRY (zve64d,PRIV_VERSION_1_10_0 , ext_zve64d), +ISA_EXT_DATA_ENTRY (zve64f,PRIV_VERSION_1_10_0 , ext_zve64f),ISA_EXT_DATA_ENTRY (zve64x,PRIV_VERSION_1_10_0 , ext_zve64x),ISA_EXT_DATA_ENTRY (zvfbfmin,PRIV_VERSION_1_12_0 , ext_zvfbfmin), ISA_EXT_DATA_ENTRY(zvfbfwma, PRIV_VERSION_1_12_0, ext_zvfbfwma),
@@ -190,8 +190,8 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zhinx,PRIV_VERSION_1_12_0 , ext_zhinx),ISA_EXT_DATA_ENTRY (zhinxmin,PRIV_VERSION_1_12_0 , ext_zhinxmin),ISA_EXT_DATA_ENTRY (sdtrig,PRIV_VERSION_1_12_0 , debug), -ISA_EXT_DATA_ENTRY (shcounterenw,PRIV_VERSION_1_12_0 , has_priv_1_12),ISA_EXT_DATA_ENTRY (sha,PRIV_VERSION_1_12_0 , ext_sha), +ISA_EXT_DATA_ENTRY (shcounterenw,PRIV_VERSION_1_12_0 , has_priv_1_12),ISA_EXT_DATA_ENTRY (shgatpa,PRIV_VERSION_1_12_0 , has_priv_1_12),ISA_EXT_DATA_ENTRY (shtvala,PRIV_VERSION_1_12_0 , has_priv_1_12), ISA_EXT_DATA_ENTRY(shvsatpa, PRIV_VERSION_1_12_0, has_priv_1_12),
@@ -201,11 +201,12 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (smcdeleg,PRIV_VERSION_1_13_0 , ext_smcdeleg),ISA_EXT_DATA_ENTRY (smcntrpmf,PRIV_VERSION_1_12_0 , ext_smcntrpmf),ISA_EXT_DATA_ENTRY (smcsrind,PRIV_VERSION_1_13_0 , ext_smcsrind), +ISA_EXT_DATA_ENTRY (smctr,PRIV_VERSION_1_12_0 , ext_smctr),ISA_EXT_DATA_ENTRY (smdbltrp,PRIV_VERSION_1_13_0 , ext_smdbltrp),ISA_EXT_DATA_ENTRY (smepmp,PRIV_VERSION_1_12_0 , ext_smepmp), -ISA_EXT_DATA_ENTRY (smrnmi,PRIV_VERSION_1_12_0 , ext_smrnmi),ISA_EXT_DATA_ENTRY (smmpm,PRIV_VERSION_1_13_0 , ext_smmpm),ISA_EXT_DATA_ENTRY (smnpm,PRIV_VERSION_1_13_0 , ext_smnpm), +ISA_EXT_DATA_ENTRY (smrnmi,PRIV_VERSION_1_12_0 , ext_smrnmi),ISA_EXT_DATA_ENTRY (smstateen,PRIV_VERSION_1_12_0 , ext_smstateen),ISA_EXT_DATA_ENTRY (ssaia,PRIV_VERSION_1_12_0 , ext_ssaia), ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
@@ -213,6 +214,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (sscofpmf,PRIV_VERSION_1_12_0 , ext_sscofpmf),ISA_EXT_DATA_ENTRY (sscounterenw,PRIV_VERSION_1_12_0 , has_priv_1_12),ISA_EXT_DATA_ENTRY (sscsrind,PRIV_VERSION_1_12_0 , ext_sscsrind), +ISA_EXT_DATA_ENTRY (ssctr,PRIV_VERSION_1_12_0 , ext_ssctr),ISA_EXT_DATA_ENTRY (ssdbltrp,PRIV_VERSION_1_13_0 , ext_ssdbltrp),ISA_EXT_DATA_ENTRY (ssnpm,PRIV_VERSION_1_13_0 , ext_ssnpm), ISA_EXT_DATA_ENTRY(sspm, PRIV_VERSION_1_13_0, ext_sspm),
@@ -224,8 +226,6 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (ssu64xl,PRIV_VERSION_1_12_0 , has_priv_1_12),ISA_EXT_DATA_ENTRY (supm,PRIV_VERSION_1_13_0 , ext_supm),ISA_EXT_DATA_ENTRY (svade,PRIV_VERSION_1_11_0 , ext_svade), -ISA_EXT_DATA_ENTRY (smctr,PRIV_VERSION_1_12_0 , ext_smctr), -ISA_EXT_DATA_ENTRY (ssctr,PRIV_VERSION_1_12_0 , ext_ssctr),ISA_EXT_DATA_ENTRY (svadu,PRIV_VERSION_1_12_0 , ext_svadu),ISA_EXT_DATA_ENTRY (svinval,PRIV_VERSION_1_12_0 , ext_svinval), ISA_EXT_DATA_ENTRY(svnapot, PRIV_VERSION_1_12_0, ext_svnapot),
Signature · 1 line
2.51.1
PATCH 2/2 scripts: RISC-V python script to check isa_edata_arr[]
In reply to Daniel Henrique BarbozaKeeping isa_edata_arr[] from target/riscv/cpu.c in order is not a fun time. The RISC-V specification rules are very convoluted (Z extensions first, then if it's a Z extension then there's the IMAFD subcategory S and X exts are always alphabetical ...), very hard to follow during the patch review process, and in the end this array keeps getting out of order constantly during development.
This script aims to ease the suffering of reviewers by reading the array from target/riscv/cpu.c and pointing ordering errors that were introduced in the file:
$ ./script/riscv-isaedata-check.py Wrong ordering: ssstateen must succeed ssctr
A successful run will give no output and retval 0.
--- scripts/riscv-isaedata-check.py | 164 ++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 3 + 2 files changed, 167 insertions(+) create mode 100755 scripts/riscv-isaedata-check.py
scripts/riscv-isaedata-check.py new file +164 -0
@@ -0,0 +1,164 @@ +#!/usr/bin/env python3 +# Copyright (c) 2025 Ventana Micro Systems Inc. +# SPDX-License-Identifier: GPL-2.0-or-later + +# +# Check if isa_edata_arr[] from target/riscv/cpu.c is +# ordered according to the RISC-V specification. +# + +import os + +""" +This script aims to check the ordering of isa_edata_arr[] array +from target/riscv/cpu.c. + +The RISC-V riscv,isa ordering has a lot of rules (see the comment +right before isa_edata_arr[] in target/riscv/cpu.c) and we're not +able to keep up with it during the review process. In the end +every new extension added has a good chance of breaking it. + +The idea with this script is to try to catch these errors earlier +by directly reading isa_edata_arr[] and pointing out discrepancies +found. E.g.: + +$ python3 riscv-isaedata-check.py +Wrong ordering: sspm must succeed ssctr + +This indicates that 'ssctr' must be put earlier that sspm in the +array. + +A successful run of this script has a retval = 0 and no output. +""" + +def ext_is_sorted(ext1: str, ext2: str) -> bool: + """Check if the RISC-V ISA extension 'ext1' comes after + the RISC-V ISA extension 'ext2' using the RISC-V sorting + rules. We're summing up these rules in 3 steps when + comparing isa_edata extensions: + + 1. If both extensions does not start with 'z', they're + sorted with regular alphabetical order; + + 2. A 'z' extension always precedes a non 'z' extension; + + 3. If both extensions starts with 'z', check the second + letter of both: + - if they're the same, sort it in alphabetical order; + - otherwise, sort it via the Z extension category + (IMAFDQLCBKJTPVH). + + Args: + ext1 (str): lower-case RISC-V isa extension name + ext2 (str): lower-case RISC-V isa extension name + + Returns: + bool: True ext1 precedes ext2, False otherwise. + """ + z_order = ['i','m','a','f','d','q', 'l', 'c', 'b', + 'k', 'j', 't', 'p', 'v', 'h'] + order1 = len(z_order) + order2 = len(z_order) + + if ext1[0] != 'z' and ext2[0] != 'z': + return ext1 < ext2 + + if ext1[0] == 'z' and ext2[0] != 'z': + return True + + if ext1[0] != 'z' and ext2[0] == 'z': + return False + + # At this point we know both starts with 'z'. If + # they're both the same z_order use alphabetical + # order. + if ext1[1] == ext2[1]: + return ext1 < ext2 + + # Get the order within the z category for each + for i in range(len(z_order)): + if ext1[1] == z_order[i]: + order1 = i; + if ext2[1] == z_order[i]: + order2 = i; + if order1 != len(z_order) and order2 != len(z_order): + break + + if order1 < order2: + return True + + return False +# end ext_is_sorted + + +def get_extension_name(line: str) -> str: + """Given a 'line' str in the format + + ISA_EXT_DATA_ENTRY(ext_name, ...) + + Return 'ext_name' if successful or None otherwise. + """ + match_str = "ISA_EXT_DATA_ENTRY(" + match_idx = line.rfind(match_str) + + if match_idx < 0: + return None + + last_idx = line.find(",") + if last_idx < 0: + return None + + match_idx += len(match_str) + + return line[match_idx:last_idx] +#end get_extension_name + + +def main(): + dir_path = os.path.dirname(os.path.realpath(__file__)) + filename = dir_path + "/../target/riscv/cpu.c" + + try: + with open(filename, 'r') as file: + isaEdataFound = False + ext1 = None + ext2 = None + + for line in file: + if "const RISCVIsaExtData isa_edata_arr[]" in line: + isaEdataFound = True + continue + + if not isaEdataFound: + continue + + if "{ }" in line: + break + + tmp = get_extension_name(line) + + if tmp is None: + continue + + if ext1 is None: + ext1 = tmp + continue + + if ext2 is None: + ext2 = tmp + else: + ext1 = ext2 + ext2 = tmp + + if not ext_is_sorted(ext1, ext2): + print(f"Wrong ordering: {ext1} must succeed {ext2}") + exit(1) + except FileNotFoundError: + print(f"Error: The file '{filename}' was not found.") + except Exception as e: + print(f"An error occurred: {e}") +# end main + + +if __name__ == '__main__': + main()
target/riscv/cpu.c +3 -0
@@ -102,6 +102,9 @@ static void riscv_cpu_cfg_merge(RISCVCPUConfig *dest, const RISCVCPUConfig *src)* * Single letter extensions are checked in riscv_cpu_validate_misa_priv() * instead. +* +* **NOTE**: please run ./scripts/riscv-isaedata-check.py every time a +* new entry is added in this array. */ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zic64b,PRIV_VERSION_1_12_0 , ext_zic64b),
Signature · 1 line
2.51.1
Quoted 7 lines · Daniel Henrique Barboza
Hi,
We have an array called isa_edata_arr[] in
target/riscv/cpu.cwhich needs to be always kept in the RISC-V specification riscv,isa order. Easier said that done: as more and more extensions are added we're failing to keep up with the array ordering in the review process.
Quoted 6 lines
I have considered changing how we're retrieving riscv,isa to not rely on the array ordering (in fact I have code that does that). We would sort the enabled extensions using riscv,is ordering during init time, before writing it in the DT, ignoring the current
isa_edata_arrordering. When all was said and done that sounded a bit extreme and I think there's other stuff we can try first.
FWIW, I have yet to actually see a parser for it in a "real" application that relied on the ordering. It probably makes a parser more complicated to write than one where the ordering is ignored. The only time I can really see ordering mattering is if something has a very simple bit of code and is looking for "rv##ima" or similar, and using a string comparison function.
Either way, my point basically is that you shouldn't have to go to any extreme effort to make sure it is perfect, particularly when it comes to the multiletter stuff as, at least for devicetree, the binding has never enforced ordering for multiletter extensions. I know ACPI cites spec order (and spec definitions, so GL there lol), and there could be relevant for some ACPI only code where the devicetree parser is not being reused.
Quoted 15 lines · Conor Dooley
Quoted 7 lines · Daniel Henrique Barboza
Hi,
We have an array called isa_edata_arr[] in
target/riscv/cpu.cwhich needs to be always kept in the RISC-V specification riscv,isa order. Easier said that done: as more and more extensions are added we're failing to keep up with the array ordering in the review process.Quoted 6 lines
I have considered changing how we're retrieving riscv,isa to not rely on the array ordering (in fact I have code that does that). We would sort the enabled extensions using riscv,is ordering during init time, before writing it in the DT, ignoring the current
isa_edata_arrordering. When all was said and done that sounded a bit extreme and I think there's other stuff we can try first.
Quoted 12 lines
FWIW, I have yet to actually see a parser for it in a "real" application that relied on the ordering. It probably makes a parser more complicated to write than one where the ordering is ignored. The only time I can really see ordering mattering is if something has a very simple bit of code and is looking for "rv##ima" or similar, and using a string comparison function.
Either way, my point basically is that you shouldn't have to go to any extreme effort to make sure it is perfect, particularly when it comes to the multiletter stuff as, at least for devicetree, the binding has never enforced ordering for multiletter extensions. I know ACPI cites spec order (and spec definitions, so GL there lol), and there could be relevant for some ACPI only code where the devicetree parser is not being reused.
Thanks for the insight. Yeah, I haven't seen software caring that much about the riscv,isa ordering either. In case it exists it would be broken in QEMU given that we're using the wrong order :D
Thanks,
Daniel