From: Paul Durrant <Paul.Durrant@citrix.com>
To: 'Jan Beulich' <JBeulich@suse.com>,
xen-devel <xen-devel@lists.xenproject.org>
Cc: Andrew Cooper <Andrew.Cooper3@citrix.com>,
George Dunlap <George.Dunlap@citrix.com>
Subject: Re: [PATCH v3 22/25] x86/HVM: do actual CMPXCHG in hvmemul_cmpxchg()
Date: Fri, 8 Dec 2017 10:38:45 +0000 [thread overview]
Message-ID: <a80df9444843470a913eeb68135e4262@AMSPEX02CL03.citrite.net> (raw)
In-Reply-To: <5A295B67020000780019598E@prv-mh.provo.novell.com>
> -----Original Message-----
> From: Jan Beulich [mailto:JBeulich@suse.com]
> Sent: 07 December 2017 14:17
> To: xen-devel <xen-devel@lists.xenproject.org>
> Cc: Andrew Cooper <Andrew.Cooper3@citrix.com>; Paul Durrant
> <Paul.Durrant@citrix.com>; George Dunlap <George.Dunlap@citrix.com>
> Subject: [PATCH v3 22/25] x86/HVM: do actual CMPXCHG in
> hvmemul_cmpxchg()
>
> ..., at least as far as currently possible, i.e. when a mapping can be
> obtained.
>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Paul Durrant <paul.durrant@citrix.com>
> ---
> v3: New.
>
> --- a/xen/arch/x86/hvm/emulate.c
> +++ b/xen/arch/x86/hvm/emulate.c
> @@ -1296,8 +1296,83 @@ static int hvmemul_cmpxchg(
> bool lock,
> struct x86_emulate_ctxt *ctxt)
> {
> - /* Fix this in case the guest is really relying on r-m-w atomicity. */
> - return hvmemul_write(seg, offset, p_new, bytes, ctxt);
> + struct hvm_emulate_ctxt *hvmemul_ctxt =
> + container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
> + struct vcpu *curr = current;
> + unsigned long addr, reps = 1;
> + uint32_t pfec = PFEC_page_present | PFEC_write_access;
> + struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
> + int rc;
> + void *mapping = NULL;
> +
> + rc = hvmemul_virtual_to_linear(
> + seg, offset, bytes, &reps, hvm_access_write, hvmemul_ctxt, &addr);
> + if ( rc != X86EMUL_OKAY )
> + return rc;
> +
> + if ( is_x86_system_segment(seg) )
> + pfec |= PFEC_implicit;
> + else if ( hvmemul_ctxt->seg_reg[x86_seg_ss].dpl == 3 )
> + pfec |= PFEC_user_mode;
> +
> + mapping = hvmemul_map_linear_addr(addr, bytes, pfec,
> hvmemul_ctxt);
> + if ( IS_ERR(mapping) )
> + return ~PTR_ERR(mapping);
> +
> + if ( !mapping )
> + {
> + /* Fix this in case the guest is really relying on r-m-w atomicity. */
> + return hvmemul_linear_mmio_write(addr, bytes, p_new, pfec,
> + hvmemul_ctxt,
> + vio->mmio_access.write_access &&
> + vio->mmio_gla == (addr & PAGE_MASK));
> + }
> +
> + switch ( bytes )
> + {
> + case 1: case 2: case 4: case 8:
> + {
> + unsigned long old = 0, new = 0, cur;
> +
> + memcpy(&old, p_old, bytes);
> + memcpy(&new, p_new, bytes);
> + if ( lock )
> + cur = __cmpxchg(mapping, old, new, bytes);
> + else
> + cur = cmpxchg_local_(mapping, old, new, bytes);
> + if ( cur != old )
> + {
> + memcpy(p_old, &cur, bytes);
> + rc = X86EMUL_CMPXCHG_FAILED;
> + }
> + break;
> + }
> +
> + case 16:
> + if ( cpu_has_cx16 )
> + {
> + __uint128_t *old = p_old, cur;
> +
> + if ( lock )
> + cur = __cmpxchg16b(mapping, old, p_new);
> + else
> + cur = cmpxchg16b_local_(mapping, old, p_new);
> + if ( cur != *old )
> + {
> + *old = cur;
> + rc = X86EMUL_CMPXCHG_FAILED;
> + }
> + break;
> + }
> + /* fall through */
> + default:
> + rc = X86EMUL_UNHANDLEABLE;
> + break;
> + }
> +
> + hvmemul_unmap_linear_addr(mapping, addr, bytes, hvmemul_ctxt);
> +
> + return rc;
> }
>
> static int hvmemul_validate(
> --- a/xen/include/asm-x86/system.h
> +++ b/xen/include/asm-x86/system.h
> @@ -110,6 +110,38 @@ static always_inline unsigned long __cmp
> return old;
> }
>
> +static always_inline unsigned long cmpxchg_local_(
> + void *ptr, unsigned long old, unsigned long new, unsigned int size)
> +{
> + unsigned long prev = ~old;
> +
> + switch ( size )
> + {
> + case 1:
> + asm volatile ( "cmpxchgb %b2, %1"
> + : "=a" (prev), "+m" (*(uint8_t *)ptr)
> + : "q" (new), "0" (old) );
> + break;
> + case 2:
> + asm volatile ( "cmpxchgw %w2, %1"
> + : "=a" (prev), "+m" (*(uint16_t *)ptr)
> + : "r" (new), "0" (old) );
> + break;
> + case 4:
> + asm volatile ( "cmpxchgl %k2, %1"
> + : "=a" (prev), "+m" (*(uint32_t *)ptr)
> + : "r" (new), "0" (old) );
> + break;
> + case 8:
> + asm volatile ( "cmpxchgq %2, %1"
> + : "=a" (prev), "+m" (*(uint64_t *)ptr)
> + : "r" (new), "0" (old) );
> + break;
> + }
> +
> + return prev;
> +}
> +
> #define cmpxchgptr(ptr,o,n) ({ \
> const __typeof__(**(ptr)) *__o = (o); \
> __typeof__(**(ptr)) *__n = (n); \
> --- a/xen/include/asm-x86/x86_64/system.h
> +++ b/xen/include/asm-x86/x86_64/system.h
> @@ -31,6 +31,24 @@ static always_inline __uint128_t __cmpxc
> return prev.raw;
> }
>
> +static always_inline __uint128_t cmpxchg16b_local_(
> + void *ptr, const __uint128_t *oldp, const __uint128_t *newp)
> +{
> + union {
> + struct { uint64_t lo, hi; };
> + __uint128_t raw;
> + } new = { .raw = *newp }, old = { .raw = *oldp }, prev;
> +
> + ASSERT(cpu_has_cx16);
> +
> + /* Don't use "=A" here - clang can't deal with that. */
> + asm volatile ( "cmpxchg16b %2"
> + : "=d" (prev.hi), "=a" (prev.lo), "+m" (*(__uint128_t *)ptr)
> + : "c" (new.hi), "b" (new.lo), "0" (old.hi), "1" (old.lo) );
> +
> + return prev.raw;
> +}
> +
> #define cmpxchg16b(ptr, o, n) ({ \
> volatile void *_p = (ptr); \
> ASSERT(!((unsigned long)_p & 0xf)); \
>
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel
next prev parent reply other threads:[~2017-12-08 10:39 UTC|newest]
Thread overview: 85+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-07 13:49 [PATCH v3 00/25] x86: emulator enhancements Jan Beulich
2017-12-07 13:58 ` [PATCH v3 01/25] x86emul: make decode_register() return unsigned long * Jan Beulich
2017-12-07 18:32 ` Andrew Cooper
2017-12-08 7:44 ` Jan Beulich
2017-12-07 13:59 ` [PATCH v3 02/25] x86emul: build SIMD tests with -Os Jan Beulich
2017-12-07 18:32 ` Andrew Cooper
2017-12-07 14:00 ` [PATCH v3 03/25] x86emul: support F16C insns Jan Beulich
2018-01-31 18:58 ` Andrew Cooper
2017-12-07 14:01 ` [PATCH v3 04/25] x86emul: support FMA4 insns Jan Beulich
2018-01-31 19:51 ` Andrew Cooper
2017-12-07 14:02 ` [PATCH v3 05/25] x86emul: support FMA insns Jan Beulich
2018-02-01 16:15 ` Andrew Cooper
2017-12-07 14:03 ` [PATCH v3 06/25] x86emul: support most remaining AVX2 insns Jan Beulich
2018-02-01 19:45 ` Andrew Cooper
2018-02-02 9:29 ` Jan Beulich
2017-12-07 14:03 ` [PATCH v3 07/25] x86emul: support AVX2 gather insns Jan Beulich
2018-02-01 20:53 ` Andrew Cooper
2018-02-02 9:44 ` Jan Beulich
2017-12-07 14:04 ` [PATCH v3 08/25] x86emul: add tables for XOP 08 and 09 extension spaces Jan Beulich
2018-02-02 11:43 ` Andrew Cooper
2018-02-02 15:15 ` Jan Beulich
2018-02-02 16:02 ` Andrew Cooper
2017-12-07 14:04 ` [PATCH v3 09/25] x86emul: support XOP insns Jan Beulich
2018-02-02 12:03 ` Andrew Cooper
2018-02-02 15:17 ` Jan Beulich
2018-02-05 13:01 ` Andrew Cooper
2017-12-07 14:05 ` [PATCH v3 10/25] x86emul: support 3DNow! insns Jan Beulich
2018-02-02 13:02 ` Andrew Cooper
2018-02-02 15:22 ` Jan Beulich
2018-02-02 16:04 ` Andrew Cooper
2017-12-07 14:06 ` [PATCH v3 11/25] x86emul: place test blobs in executable section Jan Beulich
2018-02-02 13:03 ` Andrew Cooper
2018-02-02 15:27 ` Jan Beulich
2018-02-05 13:11 ` Andrew Cooper
2018-02-05 13:55 ` Jan Beulich
2017-12-07 14:07 ` [PATCH v3 12/25] x86emul: abstract out XCRn accesses Jan Beulich
2018-02-02 13:29 ` Andrew Cooper
2018-02-02 17:05 ` Jan Beulich
2017-12-07 14:08 ` [PATCH v3 13/25] x86emul: adjust_bnd() should check XCR0 Jan Beulich
2018-02-02 13:30 ` Andrew Cooper
2018-02-02 16:19 ` Jan Beulich
2018-02-02 16:28 ` Andrew Cooper
2017-12-07 14:09 ` [PATCH v3 14/25] x86emul: make all FPU emulation use the stub Jan Beulich
2018-02-02 13:37 ` Andrew Cooper
2017-12-07 14:10 ` [PATCH v3 15/25] x86/HVM: eliminate custom #MF/#XM handling Jan Beulich
2018-02-02 13:38 ` Andrew Cooper
2017-12-07 14:11 ` [PATCH v3 16/25] x86emul: support SWAPGS Jan Beulich
2018-02-02 13:41 ` Andrew Cooper
2018-02-02 16:24 ` Jan Beulich
2017-12-07 14:11 ` [PATCH v3 17/25] x86emul: emulate {MONITOR, MWAIT}{, X} as no-op Jan Beulich
2018-02-02 14:05 ` Andrew Cooper
2017-12-07 14:12 ` [PATCH v3 18/25] x86emul: add missing suffixes in test harness Jan Beulich
2018-02-02 14:13 ` Andrew Cooper
2017-12-07 14:14 ` [PATCH v3 19/25] x86emul: tell cmpxchg hook whether LOCK is in effect Jan Beulich
2017-12-08 10:58 ` Paul Durrant
2018-02-02 14:13 ` Andrew Cooper
2017-12-07 14:15 ` [PATCH v3 20/25] x86emul: correctly handle CMPXCHG* comparison failures Jan Beulich
2018-02-02 14:49 ` Andrew Cooper
2018-02-05 8:07 ` Jan Beulich
2018-02-05 13:38 ` Andrew Cooper
2017-12-07 14:16 ` [PATCH v3 21/25] x86emul: add read-modify-write hook Jan Beulich
2018-02-02 16:13 ` Andrew Cooper
2018-02-05 8:22 ` Jan Beulich
2018-02-05 14:21 ` Andrew Cooper
2018-02-05 14:56 ` Jan Beulich
2017-12-07 14:16 ` [PATCH v3 22/25] x86/HVM: do actual CMPXCHG in hvmemul_cmpxchg() Jan Beulich
2017-12-07 14:38 ` Razvan Cojocaru
2017-12-08 10:38 ` Paul Durrant [this message]
2018-02-02 16:36 ` Andrew Cooper
2018-02-05 8:32 ` Jan Beulich
2018-02-05 16:09 ` Andrew Cooper
2018-02-05 16:49 ` Jan Beulich
2018-02-05 16:57 ` Andrew Cooper
2018-02-05 17:05 ` Jan Beulich
2017-12-07 14:17 ` [PATCH v3 23/25] x86/HVM: make use of new read-modify-write emulator hook Jan Beulich
2017-12-08 10:41 ` Paul Durrant
2018-02-02 16:37 ` Andrew Cooper
2018-02-05 8:34 ` Jan Beulich
2018-02-05 16:15 ` Andrew Cooper
2017-12-07 14:18 ` [PATCH v3 24/25] x86/shadow: fully move unmap-dest into common code Jan Beulich
2018-02-02 16:46 ` Andrew Cooper
2017-12-07 14:19 ` [PATCH v3 25/25] x86/shadow: fold sh_x86_emulate_{write, cmpxchg}() into their only callers Jan Beulich
2018-02-02 16:52 ` Andrew Cooper
2018-02-05 8:42 ` Jan Beulich
2018-02-05 12:16 ` Tim Deegan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a80df9444843470a913eeb68135e4262@AMSPEX02CL03.citrite.net \
--to=paul.durrant@citrix.com \
--cc=Andrew.Cooper3@citrix.com \
--cc=George.Dunlap@citrix.com \
--cc=JBeulich@suse.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).