Skip to content

Commit 6ba14ba

Browse files
danielhbMichael Tokarev
authored andcommitted
target/riscv/kvm: add kvm_csr_cfgs[]
At this moment we're not checking if the host has support for any specific CSR before doing get/put regs. This will cause problems if the host KVM doesn't support it (see [1] as an example). We'll use the same approach done with the CPU extensions: read all known KVM CSRs during init() to check for availability, then read/write them if they are present. This will be made by either using get-reglist or by directly reading the CSRs. For now we'll just convert the CSRs to use a kvm_csr_cfg[] array, reusing the same KVMCPUConfig abstraction we use for extensions, and use the array in (get|put)_csr_regs() instead of manually listing them. A lot of boilerplate will be added but at least we'll automate the get/put procedure for CSRs, i.e. adding a new CSR in the future will be a matter of adding it in kvm_csr_regs[] and everything else will be taken care of. Despite all the code changes no behavioral change is made. [1] https://lore.kernel.org/qemu-riscv/CABJz62OfUDHYkQ0T3rGHStQprf1c7_E0qBLbLKhfv=+jb0SYAw@mail.gmail.com/ Signed-off-by: Daniel Henrique Barboza <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Acked-by: Alistair Francis <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]> Cc: [email protected] (cherry picked from commit d3b6f17) Signed-off-by: Michael Tokarev <[email protected]>
1 parent 90f7e23 commit 6ba14ba

File tree

2 files changed

+86
-36
lines changed

2 files changed

+86
-36
lines changed

target/riscv/cpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const char *riscv_get_misa_ext_name(uint32_t bit);
7979
const char *riscv_get_misa_ext_description(uint32_t bit);
8080

8181
#define CPU_CFG_OFFSET(_prop) offsetof(struct RISCVCPUConfig, _prop)
82+
#define ENV_CSR_OFFSET(_csr) offsetof(CPURISCVState, _csr)
8283

8384
typedef struct riscv_cpu_profile {
8485
struct riscv_cpu_profile *u_parent;

target/riscv/kvm/kvm-cpu.c

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,6 @@ static uint64_t kvm_riscv_vector_reg_id(RISCVCPU *cpu,
114114
KVM_RISCV_REG_ID_ULONG(KVM_REG_RISCV_VECTOR, \
115115
KVM_REG_RISCV_VECTOR_CSR_REG(name))
116116

117-
#define KVM_RISCV_GET_CSR(cs, env, csr, reg) \
118-
do { \
119-
int _ret = kvm_get_one_reg(cs, RISCV_CSR_REG(csr), &reg); \
120-
if (_ret) { \
121-
return _ret; \
122-
} \
123-
} while (0)
124-
125-
#define KVM_RISCV_SET_CSR(cs, env, csr, reg) \
126-
do { \
127-
int _ret = kvm_set_one_reg(cs, RISCV_CSR_REG(csr), &reg); \
128-
if (_ret) { \
129-
return _ret; \
130-
} \
131-
} while (0)
132-
133117
#define KVM_RISCV_GET_TIMER(cs, name, reg) \
134118
do { \
135119
int ret = kvm_get_one_reg(cs, RISCV_TIMER_REG(name), &reg); \
@@ -251,6 +235,53 @@ static void kvm_riscv_update_cpu_misa_ext(RISCVCPU *cpu, CPUState *cs)
251235
}
252236
}
253237

238+
#define KVM_CSR_CFG(_name, _env_prop, reg_id) \
239+
{.name = _name, .offset = ENV_CSR_OFFSET(_env_prop), \
240+
.kvm_reg_id = reg_id}
241+
242+
static KVMCPUConfig kvm_csr_cfgs[] = {
243+
KVM_CSR_CFG("sstatus", mstatus, RISCV_CSR_REG(sstatus)),
244+
KVM_CSR_CFG("sie", mie, RISCV_CSR_REG(sie)),
245+
KVM_CSR_CFG("stvec", stvec, RISCV_CSR_REG(stvec)),
246+
KVM_CSR_CFG("sscratch", sscratch, RISCV_CSR_REG(sscratch)),
247+
KVM_CSR_CFG("sepc", sepc, RISCV_CSR_REG(sepc)),
248+
KVM_CSR_CFG("scause", scause, RISCV_CSR_REG(scause)),
249+
KVM_CSR_CFG("stval", stval, RISCV_CSR_REG(stval)),
250+
KVM_CSR_CFG("sip", mip, RISCV_CSR_REG(sip)),
251+
KVM_CSR_CFG("satp", satp, RISCV_CSR_REG(satp)),
252+
};
253+
254+
static void *kvmconfig_get_env_addr(RISCVCPU *cpu, KVMCPUConfig *csr_cfg)
255+
{
256+
return (void *)&cpu->env + csr_cfg->offset;
257+
}
258+
259+
static uint32_t kvm_cpu_csr_get_u32(RISCVCPU *cpu, KVMCPUConfig *csr_cfg)
260+
{
261+
uint32_t *val32 = kvmconfig_get_env_addr(cpu, csr_cfg);
262+
return *val32;
263+
}
264+
265+
static uint64_t kvm_cpu_csr_get_u64(RISCVCPU *cpu, KVMCPUConfig *csr_cfg)
266+
{
267+
uint64_t *val64 = kvmconfig_get_env_addr(cpu, csr_cfg);
268+
return *val64;
269+
}
270+
271+
static void kvm_cpu_csr_set_u32(RISCVCPU *cpu, KVMCPUConfig *csr_cfg,
272+
uint32_t val)
273+
{
274+
uint32_t *val32 = kvmconfig_get_env_addr(cpu, csr_cfg);
275+
*val32 = val;
276+
}
277+
278+
static void kvm_cpu_csr_set_u64(RISCVCPU *cpu, KVMCPUConfig *csr_cfg,
279+
uint64_t val)
280+
{
281+
uint64_t *val64 = kvmconfig_get_env_addr(cpu, csr_cfg);
282+
*val64 = val;
283+
}
284+
254285
#define KVM_EXT_CFG(_name, _prop, _reg_id) \
255286
{.name = _name, .offset = CPU_CFG_OFFSET(_prop), \
256287
.kvm_reg_id = _reg_id}
@@ -598,34 +629,52 @@ static int kvm_riscv_put_regs_core(CPUState *cs)
598629

599630
static int kvm_riscv_get_regs_csr(CPUState *cs)
600631
{
601-
CPURISCVState *env = &RISCV_CPU(cs)->env;
632+
RISCVCPU *cpu = RISCV_CPU(cs);
633+
uint64_t reg;
634+
int i, ret;
635+
636+
for (i = 0; i < ARRAY_SIZE(kvm_csr_cfgs); i++) {
637+
KVMCPUConfig *csr_cfg = &kvm_csr_cfgs[i];
602638

603-
KVM_RISCV_GET_CSR(cs, env, sstatus, env->mstatus);
604-
KVM_RISCV_GET_CSR(cs, env, sie, env->mie);
605-
KVM_RISCV_GET_CSR(cs, env, stvec, env->stvec);
606-
KVM_RISCV_GET_CSR(cs, env, sscratch, env->sscratch);
607-
KVM_RISCV_GET_CSR(cs, env, sepc, env->sepc);
608-
KVM_RISCV_GET_CSR(cs, env, scause, env->scause);
609-
KVM_RISCV_GET_CSR(cs, env, stval, env->stval);
610-
KVM_RISCV_GET_CSR(cs, env, sip, env->mip);
611-
KVM_RISCV_GET_CSR(cs, env, satp, env->satp);
639+
ret = kvm_get_one_reg(cs, csr_cfg->kvm_reg_id, &reg);
640+
if (ret) {
641+
return ret;
642+
}
643+
644+
if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint32_t)) {
645+
kvm_cpu_csr_set_u32(cpu, csr_cfg, reg);
646+
} else if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint64_t)) {
647+
kvm_cpu_csr_set_u64(cpu, csr_cfg, reg);
648+
} else {
649+
g_assert_not_reached();
650+
}
651+
}
612652

613653
return 0;
614654
}
615655

616656
static int kvm_riscv_put_regs_csr(CPUState *cs)
617657
{
618-
CPURISCVState *env = &RISCV_CPU(cs)->env;
658+
RISCVCPU *cpu = RISCV_CPU(cs);
659+
uint64_t reg;
660+
int i, ret;
661+
662+
for (i = 0; i < ARRAY_SIZE(kvm_csr_cfgs); i++) {
663+
KVMCPUConfig *csr_cfg = &kvm_csr_cfgs[i];
664+
665+
if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint32_t)) {
666+
reg = kvm_cpu_csr_get_u32(cpu, csr_cfg);
667+
} else if (KVM_REG_SIZE(csr_cfg->kvm_reg_id) == sizeof(uint64_t)) {
668+
reg = kvm_cpu_csr_get_u64(cpu, csr_cfg);
669+
} else {
670+
g_assert_not_reached();
671+
}
619672

620-
KVM_RISCV_SET_CSR(cs, env, sstatus, env->mstatus);
621-
KVM_RISCV_SET_CSR(cs, env, sie, env->mie);
622-
KVM_RISCV_SET_CSR(cs, env, stvec, env->stvec);
623-
KVM_RISCV_SET_CSR(cs, env, sscratch, env->sscratch);
624-
KVM_RISCV_SET_CSR(cs, env, sepc, env->sepc);
625-
KVM_RISCV_SET_CSR(cs, env, scause, env->scause);
626-
KVM_RISCV_SET_CSR(cs, env, stval, env->stval);
627-
KVM_RISCV_SET_CSR(cs, env, sip, env->mip);
628-
KVM_RISCV_SET_CSR(cs, env, satp, env->satp);
673+
ret = kvm_set_one_reg(cs, csr_cfg->kvm_reg_id, &reg);
674+
if (ret) {
675+
return ret;
676+
}
677+
}
629678

630679
return 0;
631680
}

0 commit comments

Comments
 (0)