[kvm-unit-tests PATCH 04/16] x86: Use X86_PROPERTY_MAX_VIRT_ADDR in is_canonical()

Mi, Dapeng dapeng1.mi at linux.intel.com
Mon Jun 9 23:16:08 PDT 2025


On 5/30/2025 6:19 AM, Sean Christopherson wrote:
> Use X86_PROPERTY_MAX_VIRT_ADDR in is_canonical() instead of open coding a
> *very* rough equivalent.  Default to a maximum virtual address width of
> 48 bits instead of 64 bits to better match real x86 CPUs (and Intel and
> AMD architectures).
>
> Signed-off-by: Sean Christopherson <seanjc at google.com>
> ---
>  lib/x86/processor.h | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/lib/x86/processor.h b/lib/x86/processor.h
> index 6b61a38b..8c6f28a3 100644
> --- a/lib/x86/processor.h
> +++ b/lib/x86/processor.h
> @@ -1022,9 +1022,14 @@ static inline void write_pkru(u32 pkru)
>  
>  static inline bool is_canonical(u64 addr)
>  {
> -	int va_width = (raw_cpuid(0x80000008, 0).a & 0xff00) >> 8;
> -	int shift_amt = 64 - va_width;
> +	int va_width, shift_amt;
>  
> +	if (this_cpu_has_p(X86_PROPERTY_MAX_VIRT_ADDR))
> +		va_width = this_cpu_property(X86_PROPERTY_MAX_VIRT_ADDR);
> +	else
> +		va_width = 48;
> +
> +	shift_amt = 64 - va_width;
>  	return (s64)(addr << shift_amt) >> shift_amt == addr;
>  }
>  

Reviewed-by: Dapeng Mi <dapeng1.mi at linux.intel.com>





More information about the kvm-riscv mailing list