Files
acrn-hypervisor/hypervisor/arch/riscv/lib/random.c
Jian Jun Chen 07afbce7bb hv: risc-v: implement arch_get_random_value using timing counters
Add RISC-V specific implementation of arch_get_random_value() that
combines entropy from rdcycle and rdtime counters. This provides a
portable solution since the Zkr entropy extension is optional in RVA23
profile.

Tracked-On: #8834
Signed-off-by: Jian Jun Chen <jian.jun.chen@intel.com>
Reviewed-by: Fei Li <fei1.li@intel.com>
Acked-by: Wang, Yu1 <yu1.wang@intel.com>
2025-10-29 17:45:44 +08:00

40 lines
1.3 KiB
C

/*
* Copyright (C) 2025 Intel Corporation.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <types.h>
/**
* @brief Generate a random 64-bit value using RISC-V timing counters
*
* This function provides a portable random number generation mechanism for
* RISC-V systems by combining entropy from hardware timing counters. Since
* the Zkr (Entropy Source) extension is optional in RVA23 profile and not
* universally available, this implementation uses standard timing counters
* that are present in all RISC-V systems.
*
* The implementation combines two entropy sources:
* - rdcycle: CPU cycle counter (high frequency, fast changing)
* - rdtime: Real-time counter (lower frequency, slower changing)
*
* The time counter value is left-shifted by 13 bits before XORing with the
* cycle counter. The shift value 13 is chosen to compensate for the frequency
* difference between counters.
*
* @return uint64_t A 64-bit pseudo-random value
*
* @fixme TODO: Detect Zkr extension availability and use CSR_SEED (0x015)
* when hardware entropy source is present for better randomness quality.
*/
uint64_t arch_get_random_value(void)
{
uint64_t cycle, time;
asm volatile ("rdcycle %0" : "=r"(cycle));
asm volatile ("rdtime %0" : "=r"(time));
return cycle ^ (time << 13U);
}