From: Andrew Jones <ajones@ventanamicro.com>
To: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org
Subject: Re: [PATCH v5 8/9] target/riscv/kvm: read/write KVM regs via env size
Date: Tue, 29 Apr 2025 17:01:15 +0200 [thread overview]
Message-ID: <20250429-a0df0d9ceb3eb53328bf909b@orel> (raw)
In-Reply-To: <20250429124421.223883-9-dbarboza@ventanamicro.com>
On Tue, Apr 29, 2025 at 09:44:20AM -0300, Daniel Henrique Barboza wrote:
> We're going to add support for scounteren in the next patch. KVM defines
> as a target_ulong CSR, while QEMU defines env->scounteren as a 32 bit
> field. This will cause the current code to read/write a 64 bit CSR in a
> 32 bit field when running in a 64 bit CPU.
>
> To prevent that, change the current logic to honor the size of the QEMU
> storage instead of the KVM CSR reg.
>
> Suggested-by: Andrew Jones <ajones@ventanamicro.com>
> Signed-off-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
> ---
> target/riscv/kvm/kvm-cpu.c | 12 +++++++-----
> 1 file changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
> index 5efee8adb2..d55361962d 100644
> --- a/target/riscv/kvm/kvm-cpu.c
> +++ b/target/riscv/kvm/kvm-cpu.c
> @@ -135,6 +135,7 @@ typedef struct KVMCPUConfig {
> const char *description;
> target_ulong offset;
> uint64_t kvm_reg_id;
> + uint32_t prop_size;
> bool user_set;
> bool supported;
> } KVMCPUConfig;
> @@ -237,6 +238,7 @@ static void kvm_riscv_update_cpu_misa_ext(RISCVCPU *cpu, CPUState *cs)
>
> #define KVM_CSR_CFG(_name, _env_prop, reg_id) \
> {.name = _name, .offset = ENV_CSR_OFFSET(_env_prop), \
> + .prop_size = sizeof(((CPURISCVState *)0)->_env_prop), \
> .kvm_reg_id = reg_id}
>
> static KVMCPUConfig kvm_csr_cfgs[] = {
> @@ -646,9 +648,9 @@ static int kvm_riscv_get_regs_csr(CPUState *cs)
> return ret;
> }
>
> - if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint32_t)) {
> - kvm_cpu_csr_set_u32(cpu, csr_cfg, reg);
> - } else if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint64_t)) {
> + if (csr_cfg->prop_size == sizeof(uint32_t)) {
> + kvm_cpu_csr_set_u32(cpu, csr_cfg, (uint32_t)reg);
> + } else if (csr_cfg->prop_size == sizeof(uint64_t)) {
> kvm_cpu_csr_set_u64(cpu, csr_cfg, reg);
> } else {
> g_assert_not_reached();
> @@ -671,9 +673,9 @@ static int kvm_riscv_put_regs_csr(CPUState *cs)
> continue;
> }
>
> - if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint32_t)) {
> + if (csr_cfg->prop_size == sizeof(uint32_t)) {
> reg = kvm_cpu_csr_get_u32(cpu, csr_cfg);
> - } else if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint64_t)) {
> + } else if (csr_cfg->prop_size == sizeof(uint64_t)) {
> reg = kvm_cpu_csr_get_u64(cpu, csr_cfg);
> } else {
> g_assert_not_reached();
> --
> 2.49.0
>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
next prev parent reply other threads:[~2025-04-29 15:01 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-29 12:44 [PATCH v5 0/9] target/riscv/kvm: CSR related fixes Daniel Henrique Barboza
2025-04-29 12:44 ` [PATCH v5 1/9] target/riscv/kvm: minor fixes/tweaks Daniel Henrique Barboza
2025-04-29 12:44 ` [PATCH v5 2/9] target/riscv/kvm: fix leak in kvm_riscv_init_multiext_cfg() Daniel Henrique Barboza
2025-04-29 12:44 ` [PATCH v5 3/9] target/riscv/kvm: turn u32/u64 reg functions into macros Daniel Henrique Barboza
2025-04-29 12:44 ` [PATCH v5 4/9] target/riscv/kvm: turn kvm_riscv_reg_id_ulong() into a macro Daniel Henrique Barboza
2025-04-29 12:44 ` [PATCH v5 5/9] target/riscv/kvm: add kvm_csr_cfgs[] Daniel Henrique Barboza
2025-04-29 12:44 ` [PATCH v5 6/9] target/riscv/kvm: do not read unavailable CSRs Daniel Henrique Barboza
2025-04-30 23:15 ` Alistair Francis
2025-04-29 12:44 ` [PATCH v5 7/9] target/riscv/kvm: add senvcfg CSR Daniel Henrique Barboza
2025-04-30 23:16 ` Alistair Francis
2025-04-29 12:44 ` [PATCH v5 8/9] target/riscv/kvm: read/write KVM regs via env size Daniel Henrique Barboza
2025-04-29 15:01 ` Andrew Jones [this message]
2025-04-30 23:18 ` Alistair Francis
2025-04-29 12:44 ` [PATCH v5 9/9] target/riscv/kvm: add scounteren CSR Daniel Henrique Barboza
2025-04-30 23:18 ` Alistair Francis
2025-04-30 23:41 ` [PATCH v5 0/9] target/riscv/kvm: CSR related fixes Alistair Francis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250429-a0df0d9ceb3eb53328bf909b@orel \
--to=ajones@ventanamicro.com \
--cc=dbarboza@ventanamicro.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).