@samitouri / QOSamiQemu / commits / 2dc5b9d4ef

target/riscv: remove csr.h from kvm-cpu.c

Move riscv_new_csr_seed from csr.c to cpu.c since this function is shared with KVM. With that we can remove the csr.h from kvm-cpu.c. Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Message-ID: <20260703180538.3346781-10-daniel.barboza@oss.qualcomm.com> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>

Daniel Henrique Barboza committed Jul 3, 2026 at 15:05 UTC 2dc5b9d4efdb867f5f13402d149fa0045181c39f
5 files changed +32 -34
target/riscv/cpu.c
+30
@@ -21,6 +21,7 @@
21 #include "qemu/qemu-print.h"
22 #include "qemu/ctype.h"
23 #include "qemu/log.h"
24 +#include "qemu/guest-random.h"
25 #include "cpu.h"
26 #include "cpu_vendorid.h"
27 #include "target/riscv/csr.h"
@@ -556,6 +557,35 @@ static void riscv_register_custom_csrs(RISCVCPU *cpu, const RISCVCSR *csr_list)
557 }
558 #endif
559
560 +/* Used by csr.c and the KVM driver */
561 +target_ulong riscv_new_csr_seed(target_ulong new_value,
562 + target_ulong write_mask)
563 +{
564 + uint16_t random_v;
565 + Error *random_e = NULL;
566 + int random_r;
567 + target_ulong rval;
568 +
569 + random_r = qemu_guest_getrandom(&random_v, 2, &random_e);
570 + if (unlikely(random_r < 0)) {
571 + /*
572 + * Failed, for unknown reasons in the crypto subsystem.
573 + * The best we can do is log the reason and return a
574 + * failure indication to the guest. There is no reason
575 + * we know to expect the failure to be transitory, so
576 + * indicate DEAD to avoid having the guest spin on WAIT.
577 + */
578 + qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
579 + __func__, error_get_pretty(random_e));
580 + error_free(random_e);
581 + rval = SEED_OPST_DEAD;
582 + } else {
583 + rval = random_v | SEED_OPST_ES16;
584 + }
585 +
586 + return rval;
587 +}
588 +
589 static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
590 {
591 ObjectClass *oc;
target/riscv/cpu.h
+2 -1
@@ -970,7 +970,8 @@ void riscv_add_satp_mode_properties(Object *obj);
970 bool riscv_cpu_accelerator_compatible(RISCVCPU *cpu);
971
972 void riscv_cpu_register_gdb_regs_for_features(CPUState *cs);
973 -
973 +target_ulong riscv_new_csr_seed(target_ulong new_value,
974 + target_ulong write_mask);
975 const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
976
977 const char *priv_spec_to_str(int priv_version);
target/riscv/csr.h
-3
@@ -12,9 +12,6 @@
12 #include "exec/target_long.h"
13 #include "cpu_bits.h"
14
15 -target_ulong riscv_new_csr_seed(target_ulong new_value,
16 - target_ulong write_mask);
17 -
15 RISCVException riscv_csrr(CPURISCVState *env, int csrno,
16 target_ulong *ret_value);
17
target/riscv/kvm/kvm-cpu.c
-1
@@ -31,7 +31,6 @@
31 #include "system/kvm.h"
32 #include "system/kvm_int.h"
33 #include "cpu.h"
34 -#include "target/riscv/csr.h"
34 #include "trace.h"
35 #include "accel/accel-cpu-target.h"
36 #include "hw/pci/pci.h"
target/riscv/tcg/csr.c
-29
@@ -29,7 +29,6 @@
29 #include "exec/icount.h"
30 #include "accel/tcg/cpu-loop.h"
31 #include "accel/tcg/getpc.h"
32 -#include "qemu/guest-random.h"
32 #include "qapi/error.h"
33 #include "tcg/insn-start-words.h"
34 #include "internals.h"
@@ -5572,34 +5571,6 @@ static RISCVException write_mnstatus(CPURISCVState *env, int csrno,
5571 #endif
5572
5573 /* Crypto Extension */
5575 -target_ulong riscv_new_csr_seed(target_ulong new_value,
5576 - target_ulong write_mask)
5577 -{
5578 - uint16_t random_v;
5579 - Error *random_e = NULL;
5580 - int random_r;
5581 - target_ulong rval;
5582 -
5583 - random_r = qemu_guest_getrandom(&random_v, 2, &random_e);
5584 - if (unlikely(random_r < 0)) {
5585 - /*
5586 - * Failed, for unknown reasons in the crypto subsystem.
5587 - * The best we can do is log the reason and return a
5588 - * failure indication to the guest. There is no reason
5589 - * we know to expect the failure to be transitory, so
5590 - * indicate DEAD to avoid having the guest spin on WAIT.
5591 - */
5592 - qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s",
5593 - __func__, error_get_pretty(random_e));
5594 - error_free(random_e);
5595 - rval = SEED_OPST_DEAD;
5596 - } else {
5597 - rval = random_v | SEED_OPST_ES16;
5598 - }
5599 -
5600 - return rval;
5601 -}
5602 -
5574 static RISCVException rmw_seed(CPURISCVState *env, int csrno,
5575 target_ulong *ret_value,
5576 target_ulong new_value,