Skip to content

Commit

Permalink
[v1.04][RISCV] Make single-letter extension detection more robust. s …
Browse files Browse the repository at this point in the history
…and u are invalid but might appear (#200), so just skip them
  • Loading branch information
Dr-Noob committed Oct 29, 2023
1 parent bf89096 commit 9a578d3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/riscv/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ int parse_multi_letter_extension(struct extensions* ext, char* e) {
return multi_letter_extension_len;
}

bool valid_extension(char ext) {
bool found = false;
uint64_t idx = 0;

while(idx < sizeof(extension_list)/sizeof(extension_list[0]) && !found) {
found = (extension_list[idx].id == (ext - 'a'));
if(!found) idx++;
}

return found;
}

struct extensions* get_extensions_from_str(char* str) {
struct extensions* ext = emalloc(sizeof(struct extensions));
ext->mask = 0;
Expand Down Expand Up @@ -114,8 +126,22 @@ struct extensions* get_extensions_from_str(char* str) {
e += multi_letter_extension_len;
}
else {
int n = *e - 'a';
ext->mask |= 1UL << n;
// Single-letter extensions 's' and 'u' are invalid
// according to Linux kernel (arch/riscv/kernel/cpufeature.c:
// riscv_fill_hwcap). Optionally, we could opt for using
// hwcap instead of cpuinfo to avoid this
if (*e == 's' || *e == 'u') {
continue;
}
// Make sure that the extension is valid before
// adding it to the mask
if(valid_extension(*e)) {
int n = *e - 'a';
ext->mask |= 1UL << n;
}
else {
printBug("get_extensions_from_str: Invalid extension: '%c'", *e);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/riscv/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static const struct extension extension_list[] = {
{ 'v' - 'a', "(V) Vector Operations" },
{ 'n' - 'a', "(N) User-Level Interrupts" },
{ 'h' - 'a', "(H) Hypervisor" },
{ 's' - 'a', "(S) Supervisor-level Instructions" },
// multi-letter extensions
{ RISCV_ISA_EXT_SSCOFPMF, "(Sscofpmf) Count OverFlow and Privilege Mode Filtering" },
{ RISCV_ISA_EXT_SSTC, "(Sstc) S and VS level Time Compare" },
Expand Down

0 comments on commit 9a578d3

Please sign in to comment.