Skip to contentSkip to conversation

target/riscv: Add isa extenstion strings to the device tree

PATCH Superseded Atish Patra 3 participants 5 messages Feb 15, 2022 RISC-V

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: Add isa extenstion strings to the device tree

Append the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

---
 target/riscv/cpu.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
target/riscv/cpu.c +22 -1
index b0a40b83e7a8..c70260d0df15 100644
@@ -34,6 +34,9 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +884,26 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+
+    if (cpu->cfg.ext_svpbmt) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svpbmt");
+    }
+    if ((offset < max_str_len) && cpu->cfg.ext_svinval) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svinval");
+    }
+    if ((offset < max_str_len) && (cpu->cfg.ext_svnapot)) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svnapot");
+    }
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +912,8 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
+
     return isa_str;
 }
 
Signature · 1 line
2.30.2
#1 lore raw

Hi Atish,

Am Dienstag, 15. Februar 2022, 10:05:30 CET schrieb Atish Patra:

Quoted 2 lines

Append the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

For people glancing at these patches, a bit more insight into how the isa string comes together might be helpful.

See question below for the reason ;-)

Quoted 20 lines
---
 target/riscv/cpu.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
target/riscv/cpu.c +3 -0
index b0a40b83e7a8..c70260d0df15 100644
@@ -34,6 +34,9 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 892

Quoted 9 lines
target/riscv/cpu.c +6 -0
@@ -881,10 +884,26 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+
+    if (cpu->cfg.ext_svpbmt) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svpbmt");

is a double-"__" the correct divider for isa extensions?

I.e. you have "_%s" as well as "_svpbmt" here, which creates a double underscore in the ISA string.

Quoted 19 lines
target/riscv/cpu.c +11 -1
+    }
+    if ((offset < max_str_len) && cpu->cfg.ext_svinval) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svinval");
+    }
+    if ((offset < max_str_len) && (cpu->cfg.ext_svnapot)) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svnapot");
+    }
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {

target/riscv/cpu.c char *riscv_isa_string(RISCVCPU *cpu) · line 914

Quoted 3 lines
target/riscv/cpu.c +0 -0
@@ -893,6 +912,8 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';

blank line here?

target/riscv/cpu.c char *riscv_isa_string(RISCVCPU *cpu) · line 918

Quoted 4 lines
target/riscv/cpu.c +2 -0
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
+
     return isa_str;
 }

Heiko

#2 lore raw
In reply to Atish Patra

Am Dienstag, 15. Februar 2022, 10:05:30 CET schrieb Atish Patra:

Quoted 24 lines

Append the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

---
 target/riscv/cpu.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
target/riscv/cpu.c +3 -0
index b0a40b83e7a8..c70260d0df15 100644
@@ -34,6 +34,9 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +884,26 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 894

Quoted 10 lines
target/riscv/cpu.c +8 -0
 }
 
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+
+    if (cpu->cfg.ext_svpbmt) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svpbmt");
+    }
+    if ((offset < max_str_len) && cpu->cfg.ext_svinval) {

shouldn't offset + strlen("svinval") +1 be < max_str_len? snprintf will write partial strings but this would throw off a qemu client completely I guess.

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 899

Quoted 5 lines
target/riscv/cpu.c +5 -0
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svinval");
+    }
+    if ((offset < max_str_len) && (cpu->cfg.ext_svnapot)) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svnapot");
+    }

wouldn't it make more sense to do something like:

+ struct {
+ const char *value;
+ bool enabled;
+ } extensions[] = {

+        { "svpbmt", cpu->cfg.ext_svpbmt },
+        { "svinval", cpu->cfg.ext_svinval },
+        { "svnapot", cpu->cfg.ext_svnapot },

+ };
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(extensions); i++) {
+ if (!extensions[i].enabled)
+ continue;
+
+ /* check available space */
+ if (offset + strlen(extensions[i].value) + 1 > max_str_len) {
+ //do warn about exceeded length
+ return;
+ }
+
+ offset += snprintf(isa_str + offset, max_str_len, "_%s",
+ extensions[i].value);
+ }

instead?

Because that list will get longer over time and repeating checks and snprintf calls will get harder to keep in sync over time?

Heiko

Quoted 21 lines
target/riscv/cpu.c +6 -1
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +912,8 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
+
     return isa_str;
 }
 
#3 lore raw
Quoted 28 lines · Heiko Stübner

Am Dienstag, 15. Februar 2022, 10:05:30 CET schrieb Atish Patra:

Quoted 26 lines

Append the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

---
 target/riscv/cpu.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
target/riscv/cpu.c +3 -0
index b0a40b83e7a8..c70260d0df15 100644
@@ -34,6 +34,9 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +884,26 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
Quoted 13 lines

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 894

Quoted 8 lines
target/riscv/cpu.c +8 -0
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+
+    if (cpu->cfg.ext_svpbmt) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svpbmt");
+    }
+    if ((offset < max_str_len) && cpu->cfg.ext_svinval) {

shouldn't offset + strlen("svinval") +1 be < max_str_len? snprintf will write partial strings but this would throw off a qemu client completely I guess.

We need that check to put out warnings to the user.

Quoted 33 lines

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 899

Quoted 6 lines
target/riscv/cpu.c +5 -0
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svinval");
+    }
+    if ((offset < max_str_len) && (cpu->cfg.ext_svnapot)) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svnapot");
+    }

wouldn't it make more sense to do something like:

(quoted diff) +23 -0
+    struct {
+        const char *value;
+        bool enabled;
+    } extensions[] = {
+        { "svpbmt", cpu->cfg.ext_svpbmt },
+        { "svinval", cpu->cfg.ext_svinval },
+        { "svnapot", cpu->cfg.ext_svnapot },
+    };
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(extensions); i++) {
+        if (!extensions[i].enabled)
+            continue;
+
+        /* check available space */
+        if (offset + strlen(extensions[i].value) + 1 > max_str_len) {
+            //do warn about exceeded length
+            return;
+        }
+
+        offset += snprintf(isa_str + offset, max_str_len, "_%s",
+                                                          extensions[i].value);
+    }
Quoted 5 lines

instead?

Because that list will get longer over time and repeating checks and snprintf calls will get harder to keep in sync over time?

Yeah. This is much better.

Quoted 32 lines

Heiko

Quoted 21 lines
target/riscv/cpu.c +6 -1
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +912,8 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
+
     return isa_str;
 }
 
Signature · 2 lines
Regards,
Atish
#4 lore raw

Am Dienstag, 15. Februar 2022, 20:39:10 CET schrieb Atish Patra:

Quoted 43 lines · Heiko Stübner
Quoted 28 lines

Am Dienstag, 15. Februar 2022, 10:05:30 CET schrieb Atish Patra:

Quoted 26 lines

Append the available ISA extensions to the "riscv,isa" string if it is enabled so that kernel can process it.

---
 target/riscv/cpu.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
target/riscv/cpu.c +3 -0
index b0a40b83e7a8..c70260d0df15 100644
@@ -34,6 +34,9 @@
 
 /* RISC-V CPU definitions */
 
+/* This includes the null terminated character '\0' */
+#define MAX_ISA_EXT_LEN 256
+
 static const char riscv_exts[26] = "IEMAFDQCLBJTPVNSUHKORWXYZG";
 
 const char * const riscv_int_regnames[] = {
@@ -881,10 +884,26 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)
     device_class_set_props(dc, riscv_cpu_properties);
 }
 
Quoted 13 lines

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 894

Quoted 8 lines
target/riscv/cpu.c +8 -0
+static void riscv_isa_string_ext(RISCVCPU *cpu, char *isa_str, int max_str_len)
+{
+    int offset = strnlen(isa_str, max_str_len);
+
+    if (cpu->cfg.ext_svpbmt) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svpbmt");
+    }
+    if ((offset < max_str_len) && cpu->cfg.ext_svinval) {

shouldn't offset + strlen("svinval") +1 be < max_str_len? snprintf will write partial strings but this would throw off a qemu client completely I guess.

Quoted 1 line

We need that check to put out warnings to the user.

That is what I meant :-)

The current check above is for "there is still space for _something_" but it doesn't check if the whole extension name fits.

That is what I'm trying to do in my example below check (and warn if not) if the whole name fits into the remaining space.

Quoted 77 lines
Quoted 34 lines

target/riscv/cpu.c static void riscv_cpu_class_init(ObjectClass *c, void *data) · line 899

Quoted 7 lines
target/riscv/cpu.c +5 -0
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svinval");
+    }
+    if ((offset < max_str_len) && (cpu->cfg.ext_svnapot)) {
+        offset += snprintf(isa_str + offset, max_str_len, "_%s", "_svnapot");
+    }

wouldn't it make more sense to do something like:

(quoted diff) +23 -0
+    struct {
+        const char *value;
+        bool enabled;
+    } extensions[] = {
+        { "svpbmt", cpu->cfg.ext_svpbmt },
+        { "svinval", cpu->cfg.ext_svinval },
+        { "svnapot", cpu->cfg.ext_svnapot },
+    };
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(extensions); i++) {
+        if (!extensions[i].enabled)
+            continue;
+
+        /* check available space */
+        if (offset + strlen(extensions[i].value) + 1 > max_str_len) {
+            //do warn about exceeded length
+            return;
+        }
+
+        offset += snprintf(isa_str + offset, max_str_len, "_%s",
+                                                          extensions[i].value);
+    }
Quoted 5 lines

instead?

Because that list will get longer over time and repeating checks and snprintf calls will get harder to keep in sync over time?

Yeah. This is much better.

Quoted 32 lines

Heiko

Quoted 21 lines
target/riscv/cpu.c +6 -1
+}
+
 char *riscv_isa_string(RISCVCPU *cpu)
 {
     int i;
-    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) + 1;
+    const size_t maxlen = sizeof("rv128") + sizeof(riscv_exts) +
+                          MAX_ISA_EXT_LEN;
     char *isa_str = g_new(char, maxlen);
     char *p = isa_str + snprintf(isa_str, maxlen, "rv%d", TARGET_LONG_BITS);
     for (i = 0; i < sizeof(riscv_exts); i++) {
@@ -893,6 +912,8 @@ char *riscv_isa_string(RISCVCPU *cpu)
         }
     }
     *p = '\0';
+    riscv_isa_string_ext(cpu, isa_str, maxlen);
+
     return isa_str;
 }