target/riscv: Add support for a custom CPU arch state
PATCH v2 1/1 target/riscv: Add support for a custom CPU arch state
From: Portia Stephens <[email protected]>
Custom vendor CSR implementations may require custom state information. This adds a custom_arch-state struct to the CPUArchState. It also adds a RISCVCPUDef function pointer for handling allocation and initialization of the custom_arch_state as well as a reset function pointer for setting the custom_arch_state fields to known values on reset.
--- target/riscv/cpu.c | 15 +++++++++++++++ target/riscv/cpu.h | 7 +++++++ 2 files changed, 22 insertions(+)
target/riscv/cpu.c +15 -0
@@ -819,6 +819,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)if (kvm_enabled ()) {kvm_riscv_reset_vcpu (cpu); } + +if (mcc->def->custom_arch_state_reset) { + mcc->def->custom_arch_state_reset (env); + }#endif }
@@ -1167,6 +1171,9 @@ static void riscv_cpu_init(Object *obj)if (mcc->def->custom_csrs) {riscv_register_custom_csrs (cpu, mcc->def->custom_csrs); } +if (mcc->def->custom_arch_state_init) { + mcc->def->custom_arch_state_init (&cpu->env); + }#endif accel_cpu_instance_init (CPU (obj));
@@ -2706,6 +2713,14 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data)assert (!mcc->def->custom_csrs); mcc->def->custom_csrs = def->custom_csrs; } +if (def->custom_arch_state_init) { +assert (!mcc->def->custom_arch_state_init); + mcc->def->custom_arch_state_init = def->custom_arch_state_init; + } +if (def->custom_arch_state_reset) { +assert (!mcc->def->custom_arch_state_reset); + mcc->def->custom_arch_state_reset = def->custom_arch_state_reset; + } }if (!object_class_is_abstract (c)) {
target/riscv/cpu.h +7 -0
@@ -213,6 +213,9 @@ typedef struct PMUFixedCtrState {uint64_t counter_virt_prev[2 ]; } PMUFixedCtrState; +typedef void (*riscv_csr_custom_init_fn )(CPURISCVState *env); +typedef void (*riscv_csr_custom_reset_fn )(CPURISCVState *env); +struct CPUArchState {target_ulong gpr[32 ];target_ulong gprh[32 ];/* 64 top bits of the 128-bit registers */
@@ -509,6 +512,8 @@ struct CPUArchState {uint64_t rnmip;uint64_t rnmi_irqvec;uint64_t rnmi_excpvec; + +const void *custom_arch_state; };/*
@@ -561,6 +566,8 @@ typedef struct RISCVCPUDef {RISCVCPUConfig cfg;bool bare;const RISCVCSR *custom_csrs; +riscv_csr_custom_init_fn custom_arch_state_init; +riscv_csr_custom_reset_fn custom_arch_state_reset; } RISCVCPUDef;/**
Signature · 1 line
2.43.0
Quoted 78 lines · On Wed, 27 May 2026, at 1:03 PM, [email protected] wrote:
From: Portia Stephens <[email protected]>
Custom vendor CSR implementations may require custom state information. This adds a custom_arch-state struct to the CPUArchState. It also adds a RISCVCPUDef function pointer for handling allocation and initialization of the
custom_arch_stateas well as a reset function pointer for setting thecustom_arch_statefields to known values on reset.Signed-off-by Portia Stephens <[email protected]>---
target/riscv/cpu.c| 15 +++++++++++++++target/riscv/cpu.h| 7 +++++++2 files changed, 22 insertions(+)target/riscv/cpu.c +15
-0@@ -819,6 +819,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)if (kvm_enabled ()) {kvm_riscv_reset_vcpu (cpu); } + +if (mcc->def->custom_arch_state_reset) { + mcc->def->custom_arch_state_reset (env); + }#endif }@@ -1167,6 +1171,9 @@ static void riscv_cpu_init(Object *obj)if (mcc->def->custom_csrs) {riscv_register_custom_csrs (cpu, mcc->def->custom_csrs); } +if (mcc->def->custom_arch_state_init) { + mcc->def->custom_arch_state_init (&cpu->env); + }#endif accel_cpu_instance_init (CPU (obj));@@ -2706,6 +2713,14 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data)assert (!mcc->def->custom_csrs); mcc->def->custom_csrs = def->custom_csrs; } +if (def->custom_arch_state_init) { +assert (!mcc->def->custom_arch_state_init); + mcc->def->custom_arch_state_init = def->custom_arch_state_init; + } +if (def->custom_arch_state_reset) { +assert (!mcc->def->custom_arch_state_reset); + mcc->def->custom_arch_state_reset = def->custom_arch_state_reset; + } }if (!object_class_is_abstract (c)) {target/riscv/cpu.h +5
-0@@ -213,6 +213,9 @@ typedef struct PMUFixedCtrState {uint64_t counter_virt_prev[2 ]; } PMUFixedCtrState; +typedef void (*riscv_csr_custom_init_fn )(CPURISCVState *env); +typedef void (*riscv_csr_custom_reset_fn )(CPURISCVState *env); +struct CPUArchState {target_ulong gpr[32 ];target_ulong gprh[32 ];/* 64 top bits of the 128-bit registers */ @@ -509,6 +512,8 @@ struct CPUArchState {uint64_t rnmip;uint64_t rnmi_irqvec;uint64_t rnmi_excpvec; + +const void *custom_arch_state; };/*
target/riscv/cpu.h typedef struct RISCVCPUDef { · line 570
Quoted 5 lines
target/riscv/cpu.h +2
-0@@ -561,6 +566,8 @@ typedef struct RISCVCPUDef {RISCVCPUConfig cfg;bool bare;const RISCVCSR *custom_csrs; +riscv_csr_custom_init_fn custom_arch_state_init; +riscv_csr_custom_reset_fn custom_arch_state_reset;
Why do we need an init and a reset? I assume init is to allocate the stuct, but that should probably be commented here.
How do you expect these to be set? Do you have an example?
Alistair
Quoted 8 lines
target/riscv/cpu.h +0
-0} RISCVCPUDef;/** Signature · 1 line
2.43.0
Hi,
Quoted 15 lines · Alistair
Quoted 14 lines · On Wed, 27 May 2026, at 1:03 PM, [email protected] wrote:
From: Portia Stephens <[email protected]>
Custom vendor CSR implementations may require custom state information. This adds a custom_arch-state struct to the CPUArchState. It also adds a RISCVCPUDef function pointer for handling allocation and initialization of the
custom_arch_stateas well as a reset function pointer for setting thecustom_arch_statefields to known values on reset.Signed-off-by Portia Stephens <[email protected]>---
target/riscv/cpu.c| 15 +++++++++++++++target/riscv/cpu.h| 7 +++++++2 files changed, 22 insertions(+)
Quoted 11 lines
target/riscv/cpu.c static void riscv_cpu_reset_hold(Object *obj, ResetType type) · line 824
Quoted 11 lines
target/riscv/cpu.c +3
-0@@ -819,6 +819,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)if (kvm_enabled ()) {kvm_riscv_reset_vcpu (cpu); } + +if (mcc->def->custom_arch_state_reset) { + mcc->def->custom_arch_state_reset (env);
ResetType shouldn't be discarded IMO.
Quoted 61 lines
Quoted 53 lines
target/riscv/cpu.c +12
-0+ }#endif }@@ -1167,6 +1171,9 @@ static void riscv_cpu_init(Object *obj)if (mcc->def->custom_csrs) {riscv_register_custom_csrs (cpu, mcc->def->custom_csrs); } +if (mcc->def->custom_arch_state_init) { + mcc->def->custom_arch_state_init (&cpu->env); + }#endif accel_cpu_instance_init (CPU (obj));@@ -2706,6 +2713,14 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data)assert (!mcc->def->custom_csrs); mcc->def->custom_csrs = def->custom_csrs; } +if (def->custom_arch_state_init) { +assert (!mcc->def->custom_arch_state_init); + mcc->def->custom_arch_state_init = def->custom_arch_state_init; + } +if (def->custom_arch_state_reset) { +assert (!mcc->def->custom_arch_state_reset); + mcc->def->custom_arch_state_reset = def->custom_arch_state_reset; + } }if (!object_class_is_abstract (c)) {target/riscv/cpu.h +5
-0@@ -213,6 +213,9 @@ typedef struct PMUFixedCtrState {uint64_t counter_virt_prev[2 ]; } PMUFixedCtrState; +typedef void (*riscv_csr_custom_init_fn )(CPURISCVState *env); +typedef void (*riscv_csr_custom_reset_fn )(CPURISCVState *env); +struct CPUArchState {target_ulong gpr[32 ];target_ulong gprh[32 ];/* 64 top bits of the 128-bit registers */ @@ -509,6 +512,8 @@ struct CPUArchState {uint64_t rnmip;uint64_t rnmi_irqvec;uint64_t rnmi_excpvec; + +const void *custom_arch_state; };/* target/riscv/cpu.h typedef struct RISCVCPUDef { · line 570
Quoted 5 lines
target/riscv/cpu.h +2
-0@@ -561,6 +566,8 @@ typedef struct RISCVCPUDef {RISCVCPUConfig cfg;bool bare;const RISCVCSR *custom_csrs; +riscv_csr_custom_init_fn custom_arch_state_init; +riscv_csr_custom_reset_fn custom_arch_state_reset;Why do we need an init and a reset? I assume init is to allocate the stuct, but that should probably be commented here.
Quoted 1 line
How do you expect these to be set? Do you have an example?
I don't see the point of upstreaming this patch without concrete example. So far it is maintenance burden without any value.
Quoted 12 lines
Alistair
Quoted 8 lines
target/riscv/cpu.h +0
-0} RISCVCPUDef;/** Signature · 1 line
2.43.0
Quoted 18 lines · Philippe Mathieu-Daudé
Hi,
Quoted 15 lines · Alistair
Quoted 14 lines · On Wed, 27 May 2026, at 1:03 PM, [email protected] wrote:
From: Portia Stephens <[email protected]>
Custom vendor CSR implementations may require custom state information. This adds a custom_arch-state struct to the CPUArchState. It also adds a RISCVCPUDef function pointer for handling allocation and initialization of the
custom_arch_stateas well as a reset function pointer for setting thecustom_arch_statefields to known values on reset.Signed-off-by Portia Stephens <[email protected]>---
target/riscv/cpu.c| 15 +++++++++++++++target/riscv/cpu.h| 7 +++++++2 files changed, 22 insertions(+)
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
target/riscv/cpu.c +0
-0Quoted 8 lines
target/riscv/cpu.c static void riscv_cpu_reset_hold(Object *obj, · line 825
Quoted 8 lines
target/riscv/cpu.c +3
-0@@ -819,6 +819,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) if (kvm_enabled()) { kvm_riscv_reset_vcpu(cpu); } + + if (mcc->def->custom_arch_state_reset) { + mcc->def->custom_arch_state_reset(env);ResetType shouldn't be discarded IMO.
Are you referring to Alistair's comment about why do we need reset? Yes it is necessary to set the registers to a known state after a reset. A finalize function is probably also needed since init will allocate the struct.
Quoted 60 lines
Quoted 60 lines
Quoted 52 lines
target/riscv/cpu.c +12
-0+ }#endif }@@ -1167,6 +1171,9 @@ static void riscv_cpu_init(Object *obj) if (mcc->def->custom_csrs) { riscv_register_custom_csrs(cpu, mcc->def->custom_csrs); } + if (mcc->def->custom_arch_state_init) { + mcc->def->custom_arch_state_init(&cpu->env); + }#endif accel_cpu_instance_init(CPU(obj));@@ -2706,6 +2713,14 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data) assert(!mcc->def->custom_csrs); mcc->def->custom_csrs = def->custom_csrs; } + if (def->custom_arch_state_init) { + assert(!mcc->def->custom_arch_state_init); + mcc->def->custom_arch_state_init = def- >custom_arch_state_init; + } + if (def->custom_arch_state_reset) { + assert(!mcc->def->custom_arch_state_reset); + mcc->def->custom_arch_state_reset = def- >custom_arch_state_reset; + }}
if (!object_class_is_abstract(c)) {target/riscv/cpu.h +3
-0@@ -213,6 +213,9 @@ typedef struct PMUFixedCtrState { uint64_t counter_virt_prev[2]; } PMUFixedCtrState; +typedef void (*riscv_csr_custom_init_fn )(CPURISCVState *env); +typedef void (*riscv_csr_custom_reset_fn )(CPURISCVState *env); +struct CPUArchState { target_ulong gpr[32];
target_ulonggprh[32]; /* 64 top bits of the 128-bit registers */target/riscv/cpu.h +2
-0@@ -509,6 +512,8 @@ struct CPUArchState { uint64_t rnmip; uint64_t rnmi_irqvec; uint64_t rnmi_excpvec; + + const void *custom_arch_state; };/* target/riscv/cpu.h typedef struct RISCVCPUDef { · line 570
Quoted 5 lines
target/riscv/cpu.h +2
-0@@ -561,6 +566,8 @@ typedef struct RISCVCPUDef { RISCVCPUConfig cfg; bool bare; const RISCVCSR *custom_csrs; + riscv_csr_custom_init_fn custom_arch_state_init; + riscv_csr_custom_reset_fn custom_arch_state_reset;Why do we need an init and a reset? I assume init is to allocate the stuct, but that should probably be commented here.
Quoted 4 lines
Quoted 1 line
How do you expect these to be set? Do you have an example?
I don't see the point of upstreaming this patch without concrete example. So far it is maintenance burden without any value.
Okay.
Quoted 13 lines
Quoted 12 lines
Alistair
Quoted 7 lines
target/riscv/cpu.h +0
-0} RISCVCPUDef;/** Signature · 1 line
2.43.0
Quoted 36 lines · Portia Stephens
Quoted 18 lines · Philippe Mathieu-Daudé
Hi,
Quoted 15 lines · Alistair
Quoted 14 lines · On Wed, 27 May 2026, at 1:03 PM, [email protected] wrote:
From: Portia Stephens <[email protected]>
Custom vendor CSR implementations may require custom state information. This adds a custom_arch-state struct to the CPUArchState. It also adds a RISCVCPUDef function pointer for handling allocation and initialization of the
custom_arch_stateas well as a reset function pointer for setting thecustom_arch_statefields to known values on reset.Signed-off-by Portia Stephens <[email protected]>---
target/riscv/cpu.c| 15 +++++++++++++++target/riscv/cpu.h| 7 +++++++2 files changed, 22 insertions(+)Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
target/riscv/cpu.c +0
-0Quoted 8 lines
target/riscv/cpu.c static void riscv_cpu_reset_hold(Object *obj, · line 825
Quoted 8 lines
target/riscv/cpu.c +3
-0@@ -819,6 +819,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) if (kvm_enabled()) { kvm_riscv_reset_vcpu(cpu); } + + if (mcc->def->custom_arch_state_reset) { + mcc->def->custom_arch_state_reset(env);ResetType shouldn't be discarded IMO.
Quoted 1 line
Are you referring to Alistair's comment about why do we need reset? Yes it is necessary to set the registers to a known state after a reset. A finalize function is probably also needed since init will allocate the struct.
No; you introduce the RISCVCPUDef::custom_arch_state_init() handler but discard the ResetType argument. Implementations could use it.
Quoted 82 lines
Quoted 61 lines
Quoted 61 lines
Quoted 53 lines
target/riscv/cpu.c +12
-0+ }#endif }@@ -1167,6 +1171,9 @@ static void riscv_cpu_init(Object *obj) if (mcc->def->custom_csrs) { riscv_register_custom_csrs(cpu, mcc->def->custom_csrs); } + if (mcc->def->custom_arch_state_init) { + mcc->def->custom_arch_state_init(&cpu->env); + }#endif accel_cpu_instance_init(CPU(obj));@@ -2706,6 +2713,14 @@ static void riscv_cpu_class_base_init(ObjectClass *c, const void *data) assert(!mcc->def->custom_csrs); mcc->def->custom_csrs = def->custom_csrs; } + if (def->custom_arch_state_init) { + assert(!mcc->def->custom_arch_state_init); + mcc->def->custom_arch_state_init = def- >custom_arch_state_init; + } + if (def->custom_arch_state_reset) { + assert(!mcc->def->custom_arch_state_reset); + mcc->def->custom_arch_state_reset = def- >custom_arch_state_reset; + }}
if (!object_class_is_abstract(c)) {target/riscv/cpu.h +3
-0@@ -213,6 +213,9 @@ typedef struct PMUFixedCtrState { uint64_t counter_virt_prev[2]; } PMUFixedCtrState; +typedef void (*riscv_csr_custom_init_fn )(CPURISCVState *env); +typedef void (*riscv_csr_custom_reset_fn )(CPURISCVState *env); +struct CPUArchState { target_ulong gpr[32];
target_ulonggprh[32]; /* 64 top bits of the 128-bit registers */target/riscv/cpu.h +2
-0@@ -509,6 +512,8 @@ struct CPUArchState { uint64_t rnmip; uint64_t rnmi_irqvec; uint64_t rnmi_excpvec; + + const void *custom_arch_state; };/* target/riscv/cpu.h typedef struct RISCVCPUDef { · line 570
Quoted 5 lines
target/riscv/cpu.h +2
-0@@ -561,6 +566,8 @@ typedef struct RISCVCPUDef { RISCVCPUConfig cfg; bool bare; const RISCVCSR *custom_csrs; + riscv_csr_custom_init_fn custom_arch_state_init; + riscv_csr_custom_reset_fn custom_arch_state_reset;Why do we need an init and a reset? I assume init is to allocate the stuct, but that should probably be commented here.
Quoted 4 lines
Quoted 1 line
How do you expect these to be set? Do you have an example?
I don't see the point of upstreaming this patch without concrete example. So far it is maintenance burden without any value.
Okay.
Quoted 13 lines
Quoted 12 lines
Alistair
Quoted 7 lines
target/riscv/cpu.h +0
-0} RISCVCPUDef;/** Signature · 1 line
2.43.0