xen/x86: Improve hypercall page writing
PATCH 1/1 xen/x86: Improve hypercall page writing
Use memcpy() rather than individual writes with explicit casts. The __builtin_memcpy() wrapper does a better job at combining adjacent writes into a larger word size.
This results in better generated assembly. No functional change.
---
CC: Jan Beulich <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
--- xen/arch/x86/hvm/svm/svm.c | 10 +++++----- xen/arch/x86/hvm/vmx/vmx.c | 10 +++++----- xen/arch/x86/x86_64/compat/traps.c | 15 +++++++++------ xen/arch/x86/x86_64/traps.c | 31 ++++++++++++++++++------------- 4 files changed, 37 insertions(+), 29 deletions(-)
xen/arch/x86/hvm/svm/svm.c +5 -5
@@ -888,12 +888,12 @@ static void svm_init_hypercall_page(struct domain *d, void *hypercall_page)continue ; p = (char *)(hypercall_page + (i *32 )); - *(u8 *)(p +0 ) =0xb8 ;/* mov imm32, %eax */ + *(u8 *)(p +0 ) =0xb8 ;/* mov $<i>, %eax */ *(u32 *)(p +1 ) = i; - *(u8 *)(p +5 ) =0x0f ;/* vmmcall */ - *(u8 *)(p +6 ) =0x01 ; - *(u8 *)(p +7 ) =0xd9 ; - *(u8 *)(p +8 ) =0xc3 ;/* ret */ +memcpy (p +5 , +"\x0f\x01\xd9" /* vmmcall */ +"\xc3" ,/* ret */ +4 ); }/* Don't support HYPERVISOR_iret at the moment */
xen/arch/x86/hvm/vmx/vmx.c +5 -5
@@ -1295,12 +1295,12 @@ static void vmx_init_hypercall_page(struct domain *d, void *hypercall_page)continue ; p = (char *)(hypercall_page + (i *32 )); - *(u8 *)(p +0 ) =0xb8 ;/* mov imm32, %eax */ + *(u8 *)(p +0 ) =0xb8 ;/* mov $<i>, %eax */ *(u32 *)(p +1 ) = i; - *(u8 *)(p +5 ) =0x0f ;/* vmcall */ - *(u8 *)(p +6 ) =0x01 ; - *(u8 *)(p +7 ) =0xc1 ; - *(u8 *)(p +8 ) =0xc3 ;/* ret */ +memcpy (p +5 , +"\x0f\x01\xc1" /* vmmcall */ +"\xc3" ,/* ret */ +4 ); }/* Don't support HYPERVISOR_iret at the moment */
xen/arch/x86/x86_64/compat/traps.c +9 -6
@@ -388,8 +388,10 @@ static void hypercall_page_initialise_ring1_kernel(void *hypercall_page) p = (char *)(hypercall_page + (i *32 )); *(u8 *)(p+0 ) =0xb8 ;/* mov $<i>,%eax */ *(u32 *)(p+1 ) = i; - *(u16 *)(p+5 ) = (HYPERCALL_VECTOR <<8 ) |0xcd ;/* int $xx */ - *(u8 *)(p+7 ) =0xc3 ;/* ret */ +memcpy (p +5 , +"\xcd\x82" /* int $HYPERCALL_VECTOR */ +"\xc3" ,/* ret */ +3 ); }/*
@@ -398,10 +400,11 @@ static void hypercall_page_initialise_ring1_kernel(void *hypercall_page)* calling it. */ p = (char *)(hypercall_page + (__HYPERVISOR_iret *32 )); - *(u8 *)(p+0 ) =0x50 ;/* push %eax */ - *(u8 *)(p+1 ) =0xb8 ;/* mov $__HYPERVISOR_iret,%eax */ - *(u32 *)(p+2 ) = __HYPERVISOR_iret; - *(u16 *)(p+6 ) = (HYPERCALL_VECTOR <<8 ) |0xcd ;/* int $xx */ +memcpy (p, +"\x50" /* push %eax */ +"\xb8\x17\x00\x00\x00" /* mov $__HYPERVISOR_iret, %eax */ +"\xcd\x82" ,/* int $HYPERCALL_VECTOR */ +8 ); }/*
xen/arch/x86/x86_64/traps.c +18 -13
@@ -591,14 +591,18 @@ static void hypercall_page_initialise_ring3_kernel(void *hypercall_page)continue ; p = (char *)(hypercall_page + (i *32 )); - *(u8 *)(p+0 ) =0x51 ;/* push %rcx */ - *(u16 *)(p+1 ) =0x5341 ;/* push %r11 */ - *(u8 *)(p+3 ) =0xb8 ;/* mov $<i>,%eax */ +memcpy (p, +"\x51" /* push %rcx */ +"\x41\x53" /* push %r11 */ +"\xb8" ,/* mov $<i>,%eax */ +4 ); *(u32 *)(p+4 ) = i; - *(u16 *)(p+8 ) =0x050f ;/* syscall */ - *(u16 *)(p+10 ) =0x5b41 ;/* pop %r11 */ - *(u8 *)(p+12 ) =0x59 ;/* pop %rcx */ - *(u8 *)(p+13 ) =0xc3 ;/* ret */ +memcpy (p +8 , +"\x0f\x05" /* syscall */ +"\x41\x5b" /* pop %r11 */ +"\x59" /* pop %rcx */ +"\xc3" ,/* ret */ +6 ); }/*
@@ -607,12 +611,13 @@ static void hypercall_page_initialise_ring3_kernel(void *hypercall_page)* calling it. */ p = (char *)(hypercall_page + (__HYPERVISOR_iret *32 )); - *(u8 *)(p+0 ) =0x51 ;/* push %rcx */ - *(u16 *)(p+1 ) =0x5341 ;/* push %r11 */ - *(u8 *)(p+3 ) =0x50 ;/* push %rax */ - *(u8 *)(p+4 ) =0xb8 ;/* mov $__HYPERVISOR_iret,%eax */ - *(u32 *)(p+5 ) = __HYPERVISOR_iret; - *(u16 *)(p+9 ) =0x050f ;/* syscall */ +memcpy (p, +"\x51" /* push %rcx */ +"\x41\x53" /* push %r11 */ +"\x50" /* push %rax */ +"\xb8\x17\x00\x00\x00" /* mov $__HYPERVISOR_iret,%eax */ +"\x0f\x05" ,/* syscall */ +11 ); }#include "compat/traps.c"
Signature · 7 lines
2.1.4 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 7 lines · Andrew Cooper
Use
memcpy()rather than individual writes with explicit casts. The__builtin_memcpy()wrapper does a better job at combining adjacent writes into a larger word size.This results in better generated assembly. No functional change.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 10 lines
From: Andrew Cooper [mailto:[email protected]]
Sent: Tuesday, December 20, 2016 12:55 AMUse
memcpy()rather than individual writes with explicit casts. The__builtin_memcpy()wrapper does a better job at combining adjacent writes into a larger word size.This results in better generated assembly. No functional change.
Signed-off-by Andrew Cooper <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.12.16 at 17:55, <[email protected]> wrote:
Use
memcpy()rather than individual writes with explicit casts. The__builtin_memcpy()wrapper does a better job at combining adjacent writes into a larger word size.This results in better generated assembly. No functional change.
I don't think generated code matters much for these functions, so I'd don't view open coding constants like ...
xen/arch/x86/x86_64/compat/traps.c static void hypercall_page_initialise_ring1_kernel(void *hypercall_page) · line 392
Quoted 10 lines
xen/arch/x86/x86_64/compat/traps.c +2
-2@@ -388,8 +388,10 @@ static void hypercall_page_initialise_ring1_kernel(void *hypercall_page) p = (char *)(hypercall_page + (i *32 )); *(u8 *)(p+0 ) =0xb8 ;/* mov $<i>,%eax */ *(u32 *)(p+1 ) = i; - *(u16 *)(p+5 ) = (HYPERCALL_VECTOR <<8 ) |0xcd ;/* int $xx */ - *(u8 *)(p+7 ) =0xc3 ;/* ret */ +memcpy (p +5 , + "\xcd\x82" /* int $HYPERCALL_VECTOR */
... here or ...
xen/arch/x86/x86_64/compat/traps.c static void hypercall_page_initialise_ring1_kernel(void *hypercall_page) · line 406
Quoted 12 lines
xen/arch/x86/x86_64/compat/traps.c +4
-4@@ -398,10 +400,11 @@ static void hypercall_page_initialise_ring1_kernel(void *hypercall_page)* calling it. */ p = (char *)(hypercall_page + (__HYPERVISOR_iret *32 )); - *(u8 *)(p+0 ) =0x50 ;/* push %eax */ - *(u8 *)(p+1 ) =0xb8 ;/* mov $__HYPERVISOR_iret,%eax */ - *(u32 *)(p+2 ) = __HYPERVISOR_iret; - *(u16 *)(p+6 ) = (HYPERCALL_VECTOR <<8 ) |0xcd ;/* int $xx */ + memcpy(p, + "\x50" /* push %eax */ + "\xb8\x17\x00\x00\x00" /* mov $__HYPERVISOR_iret, %eax */ + "\xcd\x82", /* int $HYPERCALL_VECTOR */
here as a good idea. If you used a static const uint8_t[] instead of a string literal (which even includes a pointless nul terminator), all of this could be avoided afaict.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 14 lines · Jan Beulich
xen/arch/x86/x86_64/compat/traps.c static void hypercall_page_initialise_ring1_kernel(void *hypercall_page) · line 406
Quoted 13 lines
xen/arch/x86/x86_64/compat/traps.c +4
-4@@ -398,10 +400,11 @@ static void hypercall_page_initialise_ring1_kernel(void *hypercall_page)* calling it. */ p = (char *)(hypercall_page + (__HYPERVISOR_iret *32 )); - *(u8 *)(p+0 ) =0x50 ;/* push %eax */ - *(u8 *)(p+1 ) =0xb8 ;/* mov $__HYPERVISOR_iret,%eax */ - *(u32 *)(p+2 ) = __HYPERVISOR_iret; - *(u16 *)(p+6 ) = (HYPERCALL_VECTOR <<8 ) |0xcd ;/* int $xx */ + memcpy(p, + "\x50" /* push %eax */ + "\xb8\x17\x00\x00\x00" /* mov $__HYPERVISOR_iret, %eax */ + "\xcd\x82", /* int $HYPERCALL_VECTOR */here as a good idea.
Well, they are fixed in the ABI. It is not as if we could ever change them.
Quoted 3 lines
If you used a static const uint8_t[] instead of
a string literal (which even includes a pointless nul terminator), all of this could be avoided afaict.
I can see how that would work for the int $0x82 case, but how do you propose fitting 4 bytes of __HYPERVISOR_iret into an initialiser for a uint8_t array?
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 20.12.16 at 13:21, <[email protected]> wrote:
Quoted 2 lines · Jan Beulich
Review static void hypercall_page_initialise_ring1_kernel(void
Quoted 2 lines
(quoted diff) +0
-0@@ -398,10 +400,11 @@ static void hypercall_page_initialise_ring1_kernel(void
*hypercall_page)Quoted 11 lines
xen/arch/x86/x86_64/compat/traps.c static void hypercall_page_initialise_ring1_kernel(void *hypercall_page) · line 406
Quoted 11 lines
xen/arch/x86/x86_64/compat/traps.c +4
-4* calling it. */ p = (char *)(hypercall_page + (__HYPERVISOR_iret *32 )); - *(u8 *)(p+0 ) =0x50 ;/* push %eax */ - *(u8 *)(p+1 ) =0xb8 ;/* mov $__HYPERVISOR_iret,%eax */ - *(u32 *)(p+2 ) = __HYPERVISOR_iret; - *(u16 *)(p+6 ) = (HYPERCALL_VECTOR <<8 ) |0xcd ;/* int $xx */ + memcpy(p, + "\x50" /* push %eax */ + "\xb8\x17\x00\x00\x00" /* mov $__HYPERVISOR_iret, %eax */ + "\xcd\x82", /* int $HYPERCALL_VECTOR */Quoted 1 line
here as a good idea.
Well, they are fixed in the ABI. It is not as if we could ever change them.
Quoted 7 lines
Quoted 3 lines
If you used a static const uint8_t[] instead of
a string literal (which even includes a pointless nul terminator), all of this could be avoided afaict.I can see how that would work for the
int $0x82case, but how do you propose fitting 4 bytes of__HYPERVISOR_iretinto an initialiser for auint8_tarray?
One byte __HYPERVISOR_iret, the other three zeros. perhaps accompanied by a BUILD_BUG_ON(). Another alternative would be a static const struct.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel