[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] x86emul: avoid using _PRE_EFLAGS() in a few cases


  • To: Jan Beulich <jbeulich@xxxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Mon, 28 Jun 2021 18:14:13 +0100
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BczKiF5MWHmpKlEjrCxziHQlmGDkA7m92trEKv3gdzw=; b=WU6P4gZQr4ZnLtr9bQOkYHyxkLmcnzS+HyaRm36Rz3C6vbJxSy0MaxVl52Oe2ZP08t+hAL70qcJPe5HODV6spGKUUthA3ETkSmEwt/Ys4BwJf0XPKNEBd/On4V85Mh8i8bj43bArM+2SCdDaB3wLbaVaHXY/O20/A75uDGN43mpS1zWbF0vsh4lXIcXBmE+B4x56VlF09oirmBAMXyCeg5Ff1ae8Z/03VsJouN0knu3nTDt4odUMYea8PaNKpHhjNoloo1+Rzma6UGL9u/gex49qVEwfWmTVE9TQNRb0cK65hF818jsjpZDu1BO4tCrP2wZ95ZmT3j6kre7gH4vGhw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FmlxCzG5XQHUI0eKQy8g448AbzU+XrpY0d3gUuIgDipX9UcbjBaRYvoMkwgl8wb3lVR627hF+fSatJK5PVOpTIFlwEsjCaNmKlDS9D9eb7RFD+guZ7ATPrDVRlpRwA3MMP7VgTKP3MVonO8wUZBbK9l9YrSkpsxgcx3Tz4DTTZ2BVwTr9DRQJwpPd2VGe6aI7m23OrQbK6nvnUtNX+xzV5lGgM0/4fc/XfAxPnvGe2XtF8DkSX2tc5bdgKDL08K9Kt5zkI8kskX7RvRP7ujh4IhwQJoWNO7AtXYcfXIjqeEyMkb1mHE6pWavl6vXzClW6aUDsDe69PczIYhOh57a5w==
  • Authentication-results: esa6.hc3370-68.iphmx.com; dkim=pass (signature verified) header.i=@citrix.onmicrosoft.com
  • Cc: Wei Liu <wl@xxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>
  • Delivery-date: Mon, 28 Jun 2021 17:14:38 +0000
  • Ironport-hdrordr: A9a23:wypO46uaBFoIBLy9WkNE8qIX7skClIMji2hC6mlwRA09TyXGra +TdaUguSMc1gx9ZJhBo7G90KnpewK4yXcH2/hvAV7EZnibhILIFvAe0WKG+VPd8kLFh5ZgPM tbAs9D4ZjLfCJHZKXBkXqF+rQbsaC6GcmT7I+0pRcdLnAeV0gj1XYcNu/yKDwHeOAsP+teKH Pz3Lskm9PtQwVtUiztbUN1LtQr6ue7267OUFojPVoK+QOOhTSn5PrTFAWZ5A4XV3dqza05+W bIvgTl7uH72svLiyP05iv21dB7idHhwtxMCIiljdUUECzljkKNaJ56U7OPkTgpqKWE6Uoskv PLvxA8Vv4DpU/5TyWQm1/AygPg2DEh5zvJ0lmDm0bupsT/WXYTF9dBrZgxSGqa12MQ+PVHlI 5b1WOQsJRaSTnamj7m2tTOXxZ20mKpvHsZl/IJhXA3a/pcVFZol/1awKppKuZGIMqjg7pXVt WGTfuspMq+SGnqKkww5QJUsYWRth1ZJGb1fqAA0vblmQS+0koJl3fxaaQk7z49HakGOu55Dt L/Q+9VfYF1P7srhJ1GdZE8qOuMeyHwqEH3QS6vyWqOLtBOB5ubke+I3Fxy3pDwRKA1
  • Ironport-sdr: //brKe2+Q44CV3Ulmk7QOh7UbeIuvP8u+SMcEBhB3qODbKEExiwm3qVdcwSJY6aWzNVLbopfZf cSUV46Jm8xEOA9Y+/T8wYhd3hfNo+Oc2hDMume67zcBXcW61eHSKnmywm5mTX5v2JdOc4Djlp7 ZXQD+gYqa2Ksd4fhsClm3edmANf9NXHUDRehXxmgq6z4lrotBQNiC3GqrMlIJVJwOVtr6mgJ5t nwHboE0aKZ3HKeG+ojShIHj5tFNA+dghU5SAnCxnBh9l8zPUshV+WYzmrpFcOB19sugXGKBLST Po0=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 02/06/2021 15:37, Jan Beulich wrote:
> The macro expanding to quite a few insns, replace its use by simply
> clearing the status flags when the to be executed insn doesn't depend
> on their initial state, in cases where this is easily possible. (There
> are more cases where the uses are hidden inside macros, and where some
> of the users of the macros want guest flags put in place before running
> the insn, i.e. the macros can't be updated as easily.)
>
> Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>

Honestly, this is the first time I've looked into _PRE/_POST_EFLAGS() in
detail.  Why is most of this not in C to begin with?

The only bits which need to be in asm are the popf to establish the
stub's flags context, and the pushf to save the resulting state. 
Everything else is better off done by the compiler especially as it
would remove a load of work on temporaries from the stack.

Nevertheless, ...

> --- a/xen/arch/x86/x86_emulate/x86_emulate.c
> +++ b/xen/arch/x86/x86_emulate/x86_emulate.c
> @@ -6863,7 +6863,8 @@ x86_emulate(
>          }
>          opc[2] = 0xc3;
>  
> -        invoke_stub(_PRE_EFLAGS("[eflags]", "[mask]", "[tmp]"),
> +        _regs.eflags &= ~EFLAGS_MASK;
> +        invoke_stub("",
>                      _POST_EFLAGS("[eflags]", "[mask]", "[tmp]"),
>                      [eflags] "+g" (_regs.eflags),
>                      [tmp] "=&r" (dummy), "+m" (*mmvalp)
> @@ -8111,7 +8112,8 @@ x86_emulate(
>          opc[2] = 0xc3;
>  
>          copy_VEX(opc, vex);
> -        invoke_stub(_PRE_EFLAGS("[eflags]", "[mask]", "[tmp]"),
> +        _regs.eflags &= ~EFLAGS_MASK;
> +        invoke_stub("",
>                      _POST_EFLAGS("[eflags]", "[mask]", "[tmp]"),
>                      [eflags] "+g" (_regs.eflags),
>                      "=a" (dst.val), [tmp] "=&r" (dummy)

... this hunk is k{,or}test, which only modified ZF and CF according to
the SDM.

The other flags are not listed as modified, which means they're
preserved, unless you're planning to have Intel issue a correction to
the SDM.

The flags logic for both instructions is custom, so it wouldn't be a
surprise to me if it really did deviate from the normal behaviour of a
test operation.

~Andrew

> @@ -11698,13 +11700,14 @@ int x86_emul_rmw(
>          break;
>  
>      case rmw_xadd:
> +        *eflags &= ~EFLAGS_MASK;
>          switch ( state->op_bytes )
>          {
>              unsigned long dummy;
>  
>  #define XADD(sz, cst, mod) \
>          case sz: \
> -            asm ( _PRE_EFLAGS("[efl]", "[msk]", "[tmp]") \
> +            asm ( "" \
>                    COND_LOCK(xadd) " %"#mod"[reg], %[mem]; " \
>                    _POST_EFLAGS("[efl]", "[msk]", "[tmp]") \
>                    : [reg] "+" #cst (state->ea.val), \
>





 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.