PKU is not offered to guests, but a guest can still set CR4.PKE:
vmx_emulate_cr4_access() checks only the VMX fixed bits, and SVM does
not intercept CR4 writes. The guest can then execute RDPKRU and
WRPKRU, and neither VMX nor SVM switches PKRU on entry or exit.
The guest value was saved and loaded with the guest FPU state, but it
stayed in the register after an exit, so the host side of the vCPU
thread ran with it until the host FPU state was loaded again. Now
that PKRU is switched with the thread and left out of the kernel's
XSAVE mask, the value the guest writes becomes the PKRU of the vCPU
thread.
Save the host thread's PKRU and load the guest's after the guest FPU
state is loaded, and do the reverse before it is saved, as KVM does.
Both happen inside the critical section around vmmops_run(), so the
thread is not switched while the register holds the guest value.
The guest's PKRU is kept in struct vcpu and starts at zero. The
writes are skipped when the two values are equal, which they are for
every guest that leaves PKU alone.
Sponsored by: Rubicon Communications, LLC ("Netgate")