diff mbox series

x86/HVM: use single (atomic) MOV for aligned emulated writes

Message ID 339bee59-b89b-b3fc-fb7d-114e8ffe1716@suse.com (mailing list archive)
State New, archived
Headers show
Series x86/HVM: use single (atomic) MOV for aligned emulated writes | expand

Commit Message

Jan Beulich Sept. 16, 2019, 9:40 a.m. UTC
Using memcpy() may result in multiple individual byte accesses
(dependening how memcpy() is implemented and how the resulting insns,
e.g. REP MOVSB, get carried out in hardware), which isn't what we
want/need for carrying out guest insns as correctly as possible. Fall
back to memcpy() only for accesses not 2, 4, or 8 bytes in size.

Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
TBD: Besides it still being open whether the linear_write() path also
     needs playing with), a question also continues to be whether we'd
     want to extend this to reads as well. linear_{read,write}()
     currently don't use hvmemul_map_linear_addr(), i.e. in both cases
     I'd need to also fiddle with __hvm_copy() (perhaps by making the
     construct below a helper function).

Comments

Jan Beulich Dec. 20, 2019, 4:23 p.m. UTC | #1
On 16.09.2019 11:40, Jan Beulich wrote:
> Using memcpy() may result in multiple individual byte accesses
> (dependening how memcpy() is implemented and how the resulting insns,
> e.g. REP MOVSB, get carried out in hardware), which isn't what we
> want/need for carrying out guest insns as correctly as possible. Fall
> back to memcpy() only for accesses not 2, 4, or 8 bytes in size.
> 
> Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> ---
> TBD: Besides it still being open whether the linear_write() path also
>      needs playing with, a question also continues to be whether we'd
>      want to extend this to reads as well. linear_{read,write}()
>      currently don't use hvmemul_map_linear_addr(), i.e. in both cases
>      I'd need to also fiddle with __hvm_copy() (perhaps by making the
>      construct below a helper function).
> 
> --- a/xen/arch/x86/hvm/emulate.c
> +++ b/xen/arch/x86/hvm/emulate.c
> @@ -1324,7 +1324,14 @@ static int hvmemul_write(
>      if ( !mapping )
>          return linear_write(addr, bytes, p_data, pfec, hvmemul_ctxt);
>  
> -    memcpy(mapping, p_data, bytes);
> +    /* Where possible use single (and hence generally atomic) MOV insns. */
> +    switch ( bytes )
> +    {
> +    case 2: write_u16_atomic(mapping, *(uint16_t *)p_data); break;
> +    case 4: write_u32_atomic(mapping, *(uint32_t *)p_data); break;
> +    case 8: write_u64_atomic(mapping, *(uint64_t *)p_data); break;
> +    default: memcpy(mapping, p_data, bytes);                break;
> +    }
>  
>      hvmemul_unmap_linear_addr(mapping, addr, bytes, hvmemul_ctxt);
>  
>
Andrew Cooper Dec. 27, 2019, 4:06 p.m. UTC | #2
On 20/12/2019 16:23, Jan Beulich wrote:
> On 16.09.2019 11:40, Jan Beulich wrote:
>> Using memcpy() may result in multiple individual byte accesses
>> (dependening how memcpy() is implemented and how the resulting insns,
>> e.g. REP MOVSB, get carried out in hardware), which isn't what we
>> want/need for carrying out guest insns as correctly as possible. Fall
>> back to memcpy() only for accesses not 2, 4, or 8 bytes in size.
>>
>> Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
>> Signed-off-by: Jan Beulich <jbeulich@suse.com>

Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
Jason Andryuk Jan. 13, 2020, 7:40 p.m. UTC | #3
On Fri, Dec 27, 2019 at 11:09 AM Andrew Cooper
<andrew.cooper3@citrix.com> wrote:
>
> On 20/12/2019 16:23, Jan Beulich wrote:
> > On 16.09.2019 11:40, Jan Beulich wrote:
> >> Using memcpy() may result in multiple individual byte accesses
> >> (dependening how memcpy() is implemented and how the resulting insns,
> >> e.g. REP MOVSB, get carried out in hardware), which isn't what we
> >> want/need for carrying out guest insns as correctly as possible. Fall
> >> back to memcpy() only for accesses not 2, 4, or 8 bytes in size.
> >>
> >> Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
> >> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>
> Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>

Should xen/arch/x86/mm/shadow/hvm.c:hvm_emulate_write() be similarly changed?

Thanks,
Jason
Jan Beulich Jan. 14, 2020, 9:28 a.m. UTC | #4
On 13.01.2020 20:40, Jason Andryuk wrote:
> On Fri, Dec 27, 2019 at 11:09 AM Andrew Cooper
> <andrew.cooper3@citrix.com> wrote:
>>
>> On 20/12/2019 16:23, Jan Beulich wrote:
>>> On 16.09.2019 11:40, Jan Beulich wrote:
>>>> Using memcpy() may result in multiple individual byte accesses
>>>> (dependening how memcpy() is implemented and how the resulting insns,
>>>> e.g. REP MOVSB, get carried out in hardware), which isn't what we
>>>> want/need for carrying out guest insns as correctly as possible. Fall
>>>> back to memcpy() only for accesses not 2, 4, or 8 bytes in size.
>>>>
>>>> Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
>>>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>>
>> Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
> 
> Should xen/arch/x86/mm/shadow/hvm.c:hvm_emulate_write() be similarly changed?

Probably. Care to make a patch?

Jan
Jason Andryuk Jan. 14, 2020, 1:51 p.m. UTC | #5
On Tue, Jan 14, 2020 at 4:28 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 13.01.2020 20:40, Jason Andryuk wrote:
> > On Fri, Dec 27, 2019 at 11:09 AM Andrew Cooper
> > <andrew.cooper3@citrix.com> wrote:
> >>
> >> On 20/12/2019 16:23, Jan Beulich wrote:
> >>> On 16.09.2019 11:40, Jan Beulich wrote:
> >>>> Using memcpy() may result in multiple individual byte accesses
> >>>> (dependening how memcpy() is implemented and how the resulting insns,
> >>>> e.g. REP MOVSB, get carried out in hardware), which isn't what we
> >>>> want/need for carrying out guest insns as correctly as possible. Fall
> >>>> back to memcpy() only for accesses not 2, 4, or 8 bytes in size.
> >>>>
> >>>> Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
> >>>> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> >>
> >> Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>
> >
> > Should xen/arch/x86/mm/shadow/hvm.c:hvm_emulate_write() be similarly changed?
>
> Probably. Care to make a patch?

Sure :)

-Jason
diff mbox series

Patch

--- a/xen/arch/x86/hvm/emulate.c
+++ b/xen/arch/x86/hvm/emulate.c
@@ -1324,7 +1324,14 @@  static int hvmemul_write(
     if ( !mapping )
         return linear_write(addr, bytes, p_data, pfec, hvmemul_ctxt);
 
-    memcpy(mapping, p_data, bytes);
+    /* Where possible use single (and hence generally atomic) MOV insns. */
+    switch ( bytes )
+    {
+    case 2: write_u16_atomic(mapping, *(uint16_t *)p_data); break;
+    case 4: write_u32_atomic(mapping, *(uint32_t *)p_data); break;
+    case 8: write_u64_atomic(mapping, *(uint64_t *)p_data); break;
+    default: memcpy(mapping, p_data, bytes);                break;
+    }
 
     hvmemul_unmap_linear_addr(mapping, addr, bytes, hvmemul_ctxt);