- Prevent the compiler from reordering accesses to debug regs which could cause

a #VC exception in SEV-ES guests at the wrong place in the NMI handling path
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEzv7L6UO9uDPlPSfHEsHwGGHeVUoFAmPfd8EACgkQEsHwGGHe
 VUrGQQ//W70YxFCmiKrwZEcxAtgXKn3XsGlwbkw/N5+NrSBVJRcQnD+ERJs3hysn
 CKpQ1t0LfXSd3B8+ZaIgI6TrMfPzRI5NAQJIe+K/5NsQ2sozrEgYRnOi5DOjiUIs
 QMz6mBoz1Y9pPHGzdf9uw7pyrltyqpmHnGW7Zm+k+Bb9D4V1CWy0QZJXtYxrsiUL
 dbaGgURGvg92aJ4agix4AjBPspgY+x07PEn0Aj+hGESwPXMKLJAuzkzU8l0Hj/cv
 u81voYV9eJnQ1YkRqDizCc/eS3oCur7ljY5ChGoEKNfEVvZN0ikcBX6oPhWTT9o0
 YuVuZwrZ/UHXRybZCpgoEAZSWzLlYIWUTpLl/FLorXgHNYvNMmYCK9AekgENpwpE
 QtpJWfW+gEDBXzIQ7LXkNmDqi+rdq3QtB0eBqtkDyRtkMaWu3CsBKIHRH1gsadYs
 vAXI+sWE7NQBSYeUsq2Pv7u2Wzx0VtSNzPMEhXPZ/j48nFkonJA3tjdEw00bwm2+
 0UPqucGLnjpwza0G34yIzhA48I4lxwJYGL27+0nwuVQvRgISO7chKtASNgRfvqHr
 AoIzl+ZgApmj2LLCtWLJ4KUVQQkqKNT9OfPiu6r7deGm14jHapi/no6py4wdurHW
 nhTWi+oKi4e8dEga7vIdF1HfxI07czANZjKTda9C03jIWx4CR4I=
 =/0kD
 -----END PGP SIGNATURE-----

Merge tag 'x86_urgent_for_v6.2_rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 fix from Borislav Petkov:

 - Prevent the compiler from reordering accesses to debug regs which
   could cause a #VC exception in SEV-ES guests at the wrong place in
   the NMI handling path

* tag 'x86_urgent_for_v6.2_rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  x86/debug: Fix stack recursion caused by wrongly ordered DR7 accesses
This commit is contained in:
Linus Torvalds 2023-02-05 11:28:42 -08:00
commit 9e482602c5
1 changed files with 24 additions and 2 deletions

View File

@ -39,7 +39,20 @@ static __always_inline unsigned long native_get_debugreg(int regno)
asm("mov %%db6, %0" :"=r" (val));
break;
case 7:
asm("mov %%db7, %0" :"=r" (val));
/*
* Apply __FORCE_ORDER to DR7 reads to forbid re-ordering them
* with other code.
*
* This is needed because a DR7 access can cause a #VC exception
* when running under SEV-ES. Taking a #VC exception is not a
* safe thing to do just anywhere in the entry code and
* re-ordering might place the access into an unsafe location.
*
* This happened in the NMI handler, where the DR7 read was
* re-ordered to happen before the call to sev_es_ist_enter(),
* causing stack recursion.
*/
asm volatile("mov %%db7, %0" : "=r" (val) : __FORCE_ORDER);
break;
default:
BUG();
@ -66,7 +79,16 @@ static __always_inline void native_set_debugreg(int regno, unsigned long value)
asm("mov %0, %%db6" ::"r" (value));
break;
case 7:
asm("mov %0, %%db7" ::"r" (value));
/*
* Apply __FORCE_ORDER to DR7 writes to forbid re-ordering them
* with other code.
*
* While is didn't happen with a DR7 write (see the DR7 read
* comment above which explains where it happened), add the
* __FORCE_ORDER here too to avoid similar problems in the
* future.
*/
asm volatile("mov %0, %%db7" ::"r" (value), __FORCE_ORDER);
break;
default:
BUG();