Skip to content

Commit 2ae8e12

Browse files
jones-drewMichael Tokarev
authored andcommitted
target/riscv/kvm: Fix exposure of Zkr
The Zkr extension may only be exposed to KVM guests if the VMM implements the SEED CSR. Use the same implementation as TCG. Without this patch, running with a KVM which does not forward the SEED CSR access to QEMU will result in an ILL exception being injected into the guest (this results in Linux guests crashing on boot). And, when running with a KVM which does forward the access, QEMU will crash, since QEMU doesn't know what to do with the exit. Fixes: 3108e2f ("target/riscv/kvm: update KVM exts to Linux 6.8") Signed-off-by: Andrew Jones <[email protected]> Reviewed-by: Daniel Henrique Barboza <[email protected]> Cc: qemu-stable <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]> (cherry picked from commit 8699777) Signed-off-by: Michael Tokarev <[email protected]>
1 parent 8d664e5 commit 2ae8e12

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

target/riscv/cpu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ void riscv_set_csr_ops(int csrno, riscv_csr_operations *ops);
821821

822822
void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
823823

824+
target_ulong riscv_new_csr_seed(target_ulong new_value,
825+
target_ulong write_mask);
826+
824827
uint8_t satp_mode_max_from_map(uint32_t map);
825828
const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
826829

target/riscv/csr.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4267,10 +4267,8 @@ static RISCVException write_upmbase(CPURISCVState *env, int csrno,
42674267
#endif
42684268

42694269
/* Crypto Extension */
4270-
static RISCVException rmw_seed(CPURISCVState *env, int csrno,
4271-
target_ulong *ret_value,
4272-
target_ulong new_value,
4273-
target_ulong write_mask)
4270+
target_ulong riscv_new_csr_seed(target_ulong new_value,
4271+
target_ulong write_mask)
42744272
{
42754273
uint16_t random_v;
42764274
Error *random_e = NULL;
@@ -4294,6 +4292,18 @@ static RISCVException rmw_seed(CPURISCVState *env, int csrno,
42944292
rval = random_v | SEED_OPST_ES16;
42954293
}
42964294

4295+
return rval;
4296+
}
4297+
4298+
static RISCVException rmw_seed(CPURISCVState *env, int csrno,
4299+
target_ulong *ret_value,
4300+
target_ulong new_value,
4301+
target_ulong write_mask)
4302+
{
4303+
target_ulong rval;
4304+
4305+
rval = riscv_new_csr_seed(new_value, write_mask);
4306+
42974307
if (ret_value) {
42984308
*ret_value = rval;
42994309
}

target/riscv/kvm/kvm-cpu.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,13 +1418,38 @@ static int kvm_riscv_handle_sbi(CPUState *cs, struct kvm_run *run)
14181418
return ret;
14191419
}
14201420

1421+
static int kvm_riscv_handle_csr(CPUState *cs, struct kvm_run *run)
1422+
{
1423+
target_ulong csr_num = run->riscv_csr.csr_num;
1424+
target_ulong new_value = run->riscv_csr.new_value;
1425+
target_ulong write_mask = run->riscv_csr.write_mask;
1426+
int ret = 0;
1427+
1428+
switch (csr_num) {
1429+
case CSR_SEED:
1430+
run->riscv_csr.ret_value = riscv_new_csr_seed(new_value, write_mask);
1431+
break;
1432+
default:
1433+
qemu_log_mask(LOG_UNIMP,
1434+
"%s: un-handled CSR EXIT for CSR %lx\n",
1435+
__func__, csr_num);
1436+
ret = -1;
1437+
break;
1438+
}
1439+
1440+
return ret;
1441+
}
1442+
14211443
int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
14221444
{
14231445
int ret = 0;
14241446
switch (run->exit_reason) {
14251447
case KVM_EXIT_RISCV_SBI:
14261448
ret = kvm_riscv_handle_sbi(cs, run);
14271449
break;
1450+
case KVM_EXIT_RISCV_CSR:
1451+
ret = kvm_riscv_handle_csr(cs, run);
1452+
break;
14281453
default:
14291454
qemu_log_mask(LOG_UNIMP, "%s: un-handled exit reason %d\n",
14301455
__func__, run->exit_reason);

0 commit comments

Comments
 (0)