Skip to contentSkip to conversation

target/riscv/cpu.c: do not run 'host' CPU with TCG

PATCH Applied Daniel Henrique Barboza 3 participants 5 messages Jul 21, 2023 – Jul 23, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv/cpu.c: do not run 'host' CPU with TCG

The 'host' CPU is available in a CONFIG_KVM build and it's currently available for all accels, but is a KVM only CPU. This means that in a RISC-V KVM capable host we can do things like this:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: H extension requires priv spec 1.12.0

This CPU does not have a priv spec because we don't filter its extensions via priv spec. We shouldn't be reaching riscv_cpu_realize_tcg() at all with the 'host' CPU.

We don't have a way to filter the 'host' CPU out of the available CPU options (-cpu help) if the build includes both KVM and TCG. What we can do is to error out during riscv_cpu_realize_tcg() if the user chooses the 'host' CPU with accel=tcg:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: 'host' CPU is not compatible with TCG acceleration
---
 target/riscv/cpu.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/cpu.c +5 -0
index 6b93b04453..08db3d613f 100644
@@ -1395,6 +1395,11 @@ static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)
     CPURISCVState *env = &cpu->env;
     Error *local_err = NULL;
 
+    if (object_dynamic_cast(OBJECT(dev), TYPE_RISCV_CPU_HOST)) {
+        error_setg(errp, "'host' CPU is not compatible with TCG acceleration");
+        return;
+    }
+
     riscv_cpu_validate_misa_mxl(cpu, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#1 lore raw

target/riscv/cpu.c static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)

Quoted 29 lines · Daniel Henrique Barboza

The 'host' CPU is available in a CONFIG_KVM build and it's currently available for all accels, but is a KVM only CPU. This means that in a RISC-V KVM capable host we can do things like this:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: H extension requires priv spec 1.12.0

This CPU does not have a priv spec because we don't filter its extensions via priv spec. We shouldn't be reaching riscv_cpu_realize_tcg() at all with the 'host' CPU.

We don't have a way to filter the 'host' CPU out of the available CPU options (-cpu help) if the build includes both KVM and TCG. What we can do is to error out during riscv_cpu_realize_tcg() if the user chooses the 'host' CPU with accel=tcg:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: 'host' CPU is not compatible with TCG acceleration
---
  target/riscv/cpu.c | 5 +++++
  1 file changed, 5 insertions(+)
target/riscv/cpu.c +0 -0
index 6b93b04453..08db3d613f 100644
@@ -1395,6 +1395,11 @@ static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)

/me wonders why this method isn't used as AccelCPUClass:cpu_realizefn().

target/riscv/cpu.c static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp) · line 1401

Quoted 7 lines
target/riscv/cpu.c +4 -0
      CPURISCVState *env = &cpu->env;
      Error *local_err = NULL;
 
+    if (object_dynamic_cast(OBJECT(dev), TYPE_RISCV_CPU_HOST)) {
+        error_setg(errp, "'host' CPU is not compatible with TCG acceleration");
+        return;
+    }

We should manage that generically, likely in CPUClass.
We could define riscv CPU with a DEFINE_CPU_WITH_CLASS() macro to pass the class_init() handler. Anyhow meanwhile your patch is sufficient, so

#2 lore raw
Quoted 40 lines · Philippe Mathieu-Daudé

target/riscv/cpu.c static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)

Quoted 29 lines · Daniel Henrique Barboza

The 'host' CPU is available in a CONFIG_KVM build and it's currently available for all accels, but is a KVM only CPU. This means that in a RISC-V KVM capable host we can do things like this:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: H extension requires priv spec 1.12.0

This CPU does not have a priv spec because we don't filter its extensions via priv spec. We shouldn't be reaching riscv_cpu_realize_tcg() at all with the 'host' CPU.

We don't have a way to filter the 'host' CPU out of the available CPU options (-cpu help) if the build includes both KVM and TCG. What we can do is to error out during riscv_cpu_realize_tcg() if the user chooses the 'host' CPU with accel=tcg:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: 'host' CPU is not compatible with TCG acceleration

---
  target/riscv/cpu.c | 5 +++++

  1 file changed, 5 insertions(+)
target/riscv/cpu.c +0 -0
index 6b93b04453..08db3d613f 100644
@@ -1395,6 +1395,11 @@ static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)

/me wonders why this method isn't used as AccelCPUClass:cpu_realizefn().

target/riscv/cpu.c static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp) · line 1401

Quoted 6 lines
target/riscv/cpu.c +4 -0
       CPURISCVState *env = &cpu->env;
       Error *local_err = NULL;
+    if (object_dynamic_cast(OBJECT(dev), TYPE_RISCV_CPU_HOST)) {
+        error_setg(errp, "'host' CPU is not compatible with TCG acceleration");
+        return;
+    }
Quoted 5 lines

We should manage that generically, likely in CPUClass.
We could define riscv CPU with a DEFINE_CPU_WITH_CLASS() macro to pass the class_init() handler. Anyhow meanwhile your patch is sufficient, so

Thanks. I'll mark all your suggestions as todo for 8.2.

Daniel

Quoted 1 line
Reviewed-by on v1 1/1
#3 lore raw
In reply to Daniel Henrique Barboza
Quoted 17 lines · Daniel Henrique Barboza

The 'host' CPU is available in a CONFIG_KVM build and it's currently available for all accels, but is a KVM only CPU. This means that in a RISC-V KVM capable host we can do things like this:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: H extension requires priv spec 1.12.0

This CPU does not have a priv spec because we don't filter its extensions via priv spec. We shouldn't be reaching riscv_cpu_realize_tcg() at all with the 'host' CPU.

We don't have a way to filter the 'host' CPU out of the available CPU options (-cpu help) if the build includes both KVM and TCG. What we can do is to error out during riscv_cpu_realize_tcg() if the user chooses the 'host' CPU with accel=tcg:

Quoted 4 lines
$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: 'host' CPU is not compatible with TCG acceleration

Alistair

Quoted 24 lines
---
 target/riscv/cpu.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/cpu.c +5 -1
index 6b93b04453..08db3d613f 100644
@@ -1395,6 +1395,11 @@ static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)
     CPURISCVState *env = &cpu->env;
     Error *local_err = NULL;
 
+    if (object_dynamic_cast(OBJECT(dev), TYPE_RISCV_CPU_HOST)) {
+        error_setg(errp, "'host' CPU is not compatible with TCG acceleration");
+        return;
+    }
+
     riscv_cpu_validate_misa_mxl(cpu, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
--

2.41.0

#4 lore raw
In reply to Daniel Henrique Barboza
Quoted 17 lines · Daniel Henrique Barboza

The 'host' CPU is available in a CONFIG_KVM build and it's currently available for all accels, but is a KVM only CPU. This means that in a RISC-V KVM capable host we can do things like this:

$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: H extension requires priv spec 1.12.0

This CPU does not have a priv spec because we don't filter its extensions via priv spec. We shouldn't be reaching riscv_cpu_realize_tcg() at all with the 'host' CPU.

We don't have a way to filter the 'host' CPU out of the available CPU options (-cpu help) if the build includes both KVM and TCG. What we can do is to error out during riscv_cpu_realize_tcg() if the user chooses the 'host' CPU with accel=tcg:

Quoted 4 lines
$ ./build/qemu-system-riscv64 -M virt,accel=tcg -cpu host --nographic
qemu-system-riscv64: 'host' CPU is not compatible with TCG acceleration

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 24 lines
---
 target/riscv/cpu.c | 5 +++++
 1 file changed, 5 insertions(+)
target/riscv/cpu.c +5 -1
index 6b93b04453..08db3d613f 100644
@@ -1395,6 +1395,11 @@ static void riscv_cpu_realize_tcg(DeviceState *dev, Error **errp)
     CPURISCVState *env = &cpu->env;
     Error *local_err = NULL;
 
+    if (object_dynamic_cast(OBJECT(dev), TYPE_RISCV_CPU_HOST)) {
+        error_setg(errp, "'host' CPU is not compatible with TCG acceleration");
+        return;
+    }
+
     riscv_cpu_validate_misa_mxl(cpu, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
--

2.41.0