Re: [RFC PATCH 09/13] cpus: Add const-qualified CPU environment accessors
Richard Henderson <[email protected]>
| Newsgroups | org.nongnu.qemu-riscv,org.nongnu.qemu-arm,org.nongnu.qemu-devel |
|---|---|
| Message-ID | <[email protected]> |
On 8/20/26 03:47, Philippe Mathieu-Daudé wrote: > Introduce const-qualified variants of cpu_env() and > env_archcpu(), to safely access CPU architecture > state when it should not be modified. > > Signed-off-by: Philippe Mathieu-Daudé <[email protected]> > --- > include/exec/cpu-common.h | 11 +++++++++++ > include/hw/core/cpu.h | 6 ++++++ > 2 files changed, 17 insertions(+) > > diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h > index 6594f7fa1be..f3a3799f451 100644 > --- a/include/exec/cpu-common.h > +++ b/include/exec/cpu-common.h > @@ -79,6 +79,17 @@ static inline bool cpu_loop_exit_requested(const CPUState *cpu) > } > #endif /* CONFIG_TCG */ > > +/** > + * env_archcpu_const(env) > + * @env: The architecture environment (const). > + * > + * Return the const ArchCPU associated with the environment. > + */ > +static inline const ArchCPU *env_archcpu_const(const CPUArchState *env) > +{ > + return (const void *)env - sizeof(CPUState); > +} > + > /** > * env_archcpu(env) > * @env: The architecture environment > diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h > index 81af7b9ee1a..59f777f3f15 100644 > --- a/include/hw/core/cpu.h > +++ b/include/hw/core/cpu.h > @@ -594,6 +594,12 @@ struct CPUState { > QEMU_BUILD_BUG_ON(offsetof(CPUState, neg) != > sizeof(CPUState) - sizeof(CPUNegativeOffsetState)); > > +static inline const CPUArchState *cpu_env_const(const CPUState *cpu) > +{ > + /* We validate that CPUArchState follows CPUState in cpu-target.c */ > + return (const CPUArchState *)(cpu + 1); > +} > + > static inline CPUArchState *cpu_env(CPUState *cpu) > { > /* We validate that CPUArchState follows CPUState in cpu-target.c */ I wonder if we can use _Generic to make this automatic, rather than have to choose between two different function names. r~