Re: [PATCH v1 2/4] x86/fred: Write to FRED MSRs with wrmsrns()

From: Andrew Cooper
Date: Wed Jul 03 2024 - 12:01:05 EST


On 03/07/2024 4:54 pm, Borislav Petkov wrote:
> Dave Hansen <dave.hansen@xxxxxxxxx> wrote:
>> On 7/3/24 01:54, Xin Li (Intel) wrote:
>> &gt; Do FRED MSR writes with wrmsrns() rather than wrmsrl().
>>
>> A longer changelog would be appreciated here. The wrmsrns() is
>> presumably to avoid the WRMSR serialization overhead and the CR4 write
>> provides all of the serialization that we need.
> Also, all those wrmsrns() writes better be behind a CPUID check.

They're not, in Linux.

For the $N'th time, here is the primitive that Linux wants to stea^w
borrow for this to be sane.

/* Non-serialising WRMSR, when available.  Falls back to a serialising
WRMSR. */
static inline void wrmsrns(uint32_t msr, uint32_t lo, uint32_t hi)
{
    /*
     * WRMSR is 2 bytes.  WRMSRNS is 3 bytes.  Pad WRMSR with a redundant CS
     * prefix to avoid a trailing NOP.
     */
    alternative_input(".byte 0x2e; wrmsr",
                      ".byte 0x0f,0x01,0xc6", X86_FEATURE_WRMSRNS,
                      "c" (msr), "a" (lo), "d" (hi));
}

~Andrew