From: "Jan Beulich" Subject: [PATCH 1/7] x86emul: don't special case fetching the immediate of PUSH Date: Thu, 11 Aug 2016 06:03:36 -0600 Message-ID: <57AC85B8020000780010509E@prv-mh.provo.novell.com> References: <57AC83530200007800105078@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part72448488.1__=" Return-path: Received: from mail6.bemta6.messagelabs.com ([85.158.143.247]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bXoi6-0004px-7y for xen-devel@lists.xenproject.org; Thu, 11 Aug 2016 12:03:42 +0000 In-Reply-To: <57AC83530200007800105078@prv-mh.provo.novell.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel Cc: Andrew Cooper List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part72448488.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline These immediates follow the standard patterns in all modes, so they're better fetched by the generic source operand handling code. To facilitate testing, instead of adding yet another of these pretty convoluted individual test cases, simply introduce another blowfish run with -mno-accumulate-outgoing-args (the additional -Dstatic is to keep the compiler from converting the calling convention to "regparm(3)", which I did observe it does). To make this introduction of a new blowfish pass (and potential further ones later one) have less impact on the readability of the final code, abstract all such "binary blob" executions via a table to iterate through. The resulting native code execution adjustment also uncovered a lack of clobbers on the asm() in the 64-bit case, which is being fixed at once. Signed-off-by: Jan Beulich --- a/tools/tests/x86_emulator/Makefile +++ b/tools/tests/x86_emulator/Makefile @@ -11,21 +11,21 @@ all: $(TARGET) run: $(TARGET) ./$(TARGET) =20 -.PHONY: blowfish.h -blowfish.h: - rm -f blowfish.bin - XEN_TARGET_ARCH=3Dx86_32 make -f blowfish.mk all - (echo "static unsigned int blowfish32_code[] =3D {"; \ - od -v -t x blowfish.bin | sed 's/^[0-9]* /0x/' | sed 's/ /, 0x/g' = | sed 's/$$/,/';\ - echo "};") >$@ - rm -f blowfish.bin -ifeq ($(XEN_COMPILE_ARCH),x86_64) - XEN_TARGET_ARCH=3Dx86_64 make -f blowfish.mk all - (echo "static unsigned int blowfish64_code[] =3D {"; \ - od -v -t x blowfish.bin | sed 's/^[0-9]* /0x/' | sed 's/ /, 0x/g' = | sed 's/$$/,/';\ - echo "};") >>$@ - rm -f blowfish.bin -endif +cflags-x86_32 :=3D "-mno-accumulate-outgoing-args -Dstatic=3D" + +blowfish.h: blowfish.c blowfish.mk Makefile + rm -f $@.new blowfish.bin + $(foreach arch,$(filter-out $(XEN_COMPILE_ARCH),x86_32) $(XEN_COMPI= LE_ARCH), \ + for cflags in "" $(cflags-$(arch)); do \ + $(MAKE) -f blowfish.mk XEN_TARGET_ARCH=3D$(arch) BLOWFISH_C= FLAGS=3D"$$cflags" all; \ + flavor=3D$$(echo $${cflags} | sed -e 's, .*,,' -e = 'y,-=3D,__,') ; \ + (echo "static unsigned int blowfish_$(arch)$${flavor}[] = =3D {"; \ + od -v -t x blowfish.bin | sed -e 's/^[0-9]* /0x/' -e 's/ = /, 0x/g' -e 's/$$/,/'; \ + echo "};") >>$@.new; \ + rm -f blowfish.bin; \ + done; \ + ) + mv $@.new $@ =20 $(TARGET): x86_emulate.o test_x86_emulator.o $(HOSTCC) -o $@ $^ --- a/tools/tests/x86_emulator/blowfish.mk +++ b/tools/tests/x86_emulator/blowfish.mk @@ -5,7 +5,7 @@ include $(XEN_ROOT)/tools/Rules.mk =20 $(call cc-options-add,CFLAGS,CC,$(EMBEDDED_EXTRA_CFLAGS)) =20 -CFLAGS +=3D -fno-builtin -msoft-float +CFLAGS +=3D -fno-builtin -msoft-float $(BLOWFISH_CFLAGS) =20 .PHONY: all all: blowfish.bin --- a/tools/tests/x86_emulator/test_x86_emulator.c +++ b/tools/tests/x86_emulator/test_x86_emulator.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -12,6 +13,21 @@ #include "x86_emulate/x86_emulate.h" #include "blowfish.h" =20 +static const struct { + const void *code; + size_t size; + unsigned int bitness; + const char*name; +} blobs[] =3D { + { blowfish_x86_32, sizeof(blowfish_x86_32), 32, "blowfish" }, + { blowfish_x86_32_mno_accumulate_outgoing_args, + sizeof(blowfish_x86_32_mno_accumulate_outgoing_args), + 32, "blowfish (push)" }, +#ifdef __x86_64__ + { blowfish_x86_64, sizeof(blowfish_x86_64), 64, "blowfish" }, +#endif +}; + #define MMAP_SZ 16384 =20 /* EFLAGS bit definitions. */ @@ -943,25 +959,19 @@ int main(int argc, char **argv) else printf("skipped\n"); =20 - for ( j =3D 1; j <=3D 2; j++ ) + for ( j =3D 0; j < sizeof(blobs) / sizeof(*blobs); j++ ) { -#if defined(__i386__) - if ( j =3D=3D 2 ) break; - memcpy(res, blowfish32_code, sizeof(blowfish32_code)); -#else - ctxt.addr_size =3D 16 << j; - ctxt.sp_size =3D 16 << j; - memcpy(res, (j =3D=3D 1) ? blowfish32_code : blowfish64_code, - (j =3D=3D 1) ? sizeof(blowfish32_code) : sizeof(blowfish64_= code)); -#endif - printf("Testing blowfish %u-bit code sequence", j*32); + memcpy(res, blobs[j].code, blobs[j].size); + ctxt.addr_size =3D ctxt.sp_size =3D blobs[j].bitness; + + printf("Testing %s %u-bit code sequence", + blobs[j].name, ctxt.addr_size); regs.eax =3D 2; regs.edx =3D 1; regs.eip =3D (unsigned long)res; regs.esp =3D (unsigned long)res + MMAP_SZ - 4; - if ( j =3D=3D 2 ) + if ( ctxt.addr_size =3D=3D 64 ) { - ctxt.addr_size =3D ctxt.sp_size =3D 64; *(uint32_t *)(unsigned long)regs.esp =3D 0; regs.esp -=3D 4; } @@ -983,20 +993,27 @@ int main(int argc, char **argv) (regs.eax !=3D 2) || (regs.edx !=3D 1) ) goto fail; printf("okay\n"); - } =20 - printf("%-40s", "Testing blowfish native execution..."); =20 - asm volatile ( + if ( ctxt.addr_size !=3D sizeof(void *) * CHAR_BIT ) + continue; + + i =3D printf("Testing %s native execution...", blobs[j].name); + asm volatile ( #if defined(__i386__) - "movl $0x100000,%%ecx; call *%%ecx" + "movl $0x100000,%%ecx; call *%%ecx" #else - "movl $0x100000,%%ecx; call *%%rcx" + "movl $0x100000,%%ecx; call *%%rcx" #endif - : "=3Da" (regs.eax), "=3Dd" (regs.edx) - : "0" (2), "1" (1) : "ecx" ); - if ( (regs.eax !=3D 2) || (regs.edx !=3D 1) ) - goto fail; - printf("okay\n"); + : "=3Da" (regs.eax), "=3Dd" (regs.edx) + : "0" (2), "1" (1) : "ecx" +#ifdef __x86_64__ + , "rsi", "rdi", "r8", "r9", "r10", "r11" +#endif + ); + if ( (regs.eax !=3D 2) || (regs.edx !=3D 1) ) + goto fail; + printf("%*sokay\n", i < 40 ? 40 - i : 0, ""); + } =20 return 0; =20 --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -94,8 +94,8 @@ static uint8_t opcode_table[256] =3D { ImplicitOps, ImplicitOps, DstReg|SrcMem|ModRM, DstReg|SrcNone|ModRM|Mo= v, 0, 0, 0, 0, /* 0x68 - 0x6F */ - ImplicitOps|Mov, DstReg|SrcImm|ModRM|Mov, - ImplicitOps|Mov, DstReg|SrcImmByte|ModRM|Mov, + DstImplicit|SrcImm|Mov, DstReg|SrcImm|ModRM|Mov, + DstImplicit|SrcImmByte|Mov, DstReg|SrcImmByte|ModRM|Mov, ImplicitOps|Mov, ImplicitOps|Mov, ImplicitOps|Mov, ImplicitOps|Mov, /* 0x70 - 0x77 */ ImplicitOps, ImplicitOps, ImplicitOps, ImplicitOps, @@ -2335,10 +2335,15 @@ x86_emulate( break; =20 case 0x68: /* push imm{16,32,64} */ - src.val =3D ((op_bytes =3D=3D 2) - ? (int32_t)insn_fetch_type(int16_t) - : insn_fetch_type(int32_t)); - goto push; + case 0x6a: /* push imm8 */ + push: + d |=3D Mov; /* force writeback */ + dst.type =3D OP_MEM; + dst.bytes =3D mode_64bit() && (op_bytes =3D=3D 4) ? 8 : op_bytes; + dst.val =3D src.val; + dst.mem.seg =3D x86_seg_ss; + dst.mem.off =3D sp_pre_dec(dst.bytes); + break; =20 case 0x69: /* imul imm16/32 */ case 0x6b: /* imul imm8 */ @@ -2349,19 +2354,6 @@ x86_emulate( goto done; goto imul; =20 - case 0x6a: /* push imm8 */ - src.val =3D insn_fetch_type(int8_t); - push: - d |=3D Mov; /* force writeback */ - dst.type =3D OP_MEM; - dst.bytes =3D op_bytes; - if ( mode_64bit() && (dst.bytes =3D=3D 4) ) - dst.bytes =3D 8; - dst.val =3D src.val; - dst.mem.seg =3D x86_seg_ss; - dst.mem.off =3D sp_pre_dec(dst.bytes); - break; - case 0x6c ... 0x6d: /* ins %dx,%es:%edi */ { unsigned long nr_reps =3D get_rep_prefix(); unsigned int port =3D (uint16_t)_regs.edx; --=__Part72448488.1__= Content-Type: text/plain; name="x86emul-push-imm-generic.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86emul-push-imm-generic.patch" x86emul: don't special case fetching the immediate of PUSH=0A=0AThese = immediates follow the standard patterns in all modes, so they're=0Abetter = fetched by the generic source operand handling code.=0A=0ATo facilitate = testing, instead of adding yet another of these pretty=0Aconvoluted = individual test cases, simply introduce another blowfish run=0Awith = -mno-accumulate-outgoing-args (the additional -Dstatic is to=0Akeep the = compiler from converting the calling convention to=0A"regparm(3)", which I = did observe it does).=0A=0ATo make this introduction of a new blowfish = pass (and potential further=0Aones later one) have less impact on the = readability of the final code,=0Aabstract all such "binary blob" executions= via a table to iterate=0Athrough.=0A=0AThe resulting native code = execution adjustment also uncovered a lack of=0Aclobbers on the asm() in = the 64-bit case, which is being fixed at once.=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/tools/tests/x86_emulator/Makefile=0A= +++ b/tools/tests/x86_emulator/Makefile=0A@@ -11,21 +11,21 @@ all: = $(TARGET)=0A run: $(TARGET)=0A ./$(TARGET)=0A =0A-.PHONY: blowfish.h=0A-bl= owfish.h:=0A- rm -f blowfish.bin=0A- XEN_TARGET_ARCH=3Dx86_32 make -f = blowfish.mk all=0A- (echo "static unsigned int blowfish32_code[] =3D = {"; \=0A- od -v -t x blowfish.bin | sed 's/^[0-9]* /0x/' | sed 's/ = /, 0x/g' | sed 's/$$/,/';\=0A- echo "};") >$@=0A- rm -f blowfish.bin= =0A-ifeq ($(XEN_COMPILE_ARCH),x86_64)=0A- XEN_TARGET_ARCH=3Dx86_64 = make -f blowfish.mk all=0A- (echo "static unsigned int blowfish64_code[= ] =3D {"; \=0A- od -v -t x blowfish.bin | sed 's/^[0-9]* /0x/' | sed 's/ = /, 0x/g' | sed 's/$$/,/';\=0A- echo "};") >>$@=0A- rm -f blowfish.bin= =0A-endif=0A+cflags-x86_32 :=3D "-mno-accumulate-outgoing-args -Dstatic=3D"= =0A+=0A+blowfish.h: blowfish.c blowfish.mk Makefile=0A+ rm -f $@.new = blowfish.bin=0A+ $(foreach arch,$(filter-out $(XEN_COMPILE_ARCH),x86= _32) $(XEN_COMPILE_ARCH), \=0A+ for cflags in "" $(cflags-$(arch)); do = \=0A+ $(MAKE) -f blowfish.mk XEN_TARGET_ARCH=3D$(arch) BLOWFISH_C= FLAGS=3D"$$cflags" all; \=0A+ flavor=3D$$(echo $${cflags} | sed = -e 's, .*,,' -e 'y,-=3D,__,') ; \=0A+ (echo "static unsigned int = blowfish_$(arch)$${flavor}[] =3D {"; \=0A+ od -v -t x = blowfish.bin | sed -e 's/^[0-9]* /0x/' -e 's/ /, 0x/g' -e 's/$$/,/'; \=0A+ = echo "};") >>$@.new; \=0A+ rm -f blowfish.bin; \=0A+ = done; \=0A+ )=0A+ mv $@.new $@=0A =0A $(TARGET): x86_emulate.o = test_x86_emulator.o=0A $(HOSTCC) -o $@ $^=0A--- a/tools/tests/x86_emulator= /blowfish.mk=0A+++ b/tools/tests/x86_emulator/blowfish.mk=0A@@ -5,7 +5,7 = @@ include $(XEN_ROOT)/tools/Rules.mk=0A =0A $(call cc-options-add,CFLAGS,C= C,$(EMBEDDED_EXTRA_CFLAGS))=0A =0A-CFLAGS +=3D -fno-builtin -msoft-float=0A= +CFLAGS +=3D -fno-builtin -msoft-float $(BLOWFISH_CFLAGS)=0A =0A .PHONY: = all=0A all: blowfish.bin=0A--- a/tools/tests/x86_emulator/test_x86_emulator= .c=0A+++ b/tools/tests/x86_emulator/test_x86_emulator.c=0A@@ -1,4 +1,5 = @@=0A #include =0A+#include =0A #include =0A = #include =0A #include =0A@@ -12,6 +13,21 @@=0A #include = "x86_emulate/x86_emulate.h"=0A #include "blowfish.h"=0A =0A+static const = struct {=0A+ const void *code;=0A+ size_t size;=0A+ unsigned int = bitness;=0A+ const char*name;=0A+} blobs[] =3D {=0A+ { blowfish_x86_3= 2, sizeof(blowfish_x86_32), 32, "blowfish" },=0A+ { blowfish_x86_32_mno_= accumulate_outgoing_args,=0A+ sizeof(blowfish_x86_32_mno_accumulate_ou= tgoing_args),=0A+ 32, "blowfish (push)" },=0A+#ifdef __x86_64__=0A+ = { blowfish_x86_64, sizeof(blowfish_x86_64), 64, "blowfish" },=0A+#endif=0A= +};=0A+=0A #define MMAP_SZ 16384=0A =0A /* EFLAGS bit definitions. */=0A@@ = -943,25 +959,19 @@ int main(int argc, char **argv)=0A else=0A = printf("skipped\n");=0A =0A- for ( j =3D 1; j <=3D 2; j++ )=0A+ for = ( j =3D 0; j < sizeof(blobs) / sizeof(*blobs); j++ )=0A {=0A-#if = defined(__i386__)=0A- if ( j =3D=3D 2 ) break;=0A- memcpy(res= , blowfish32_code, sizeof(blowfish32_code));=0A-#else=0A- ctxt.addr_= size =3D 16 << j;=0A- ctxt.sp_size =3D 16 << j;=0A- = memcpy(res, (j =3D=3D 1) ? blowfish32_code : blowfish64_code,=0A- = (j =3D=3D 1) ? sizeof(blowfish32_code) : sizeof(blowfish64_code));=0A-= #endif=0A- printf("Testing blowfish %u-bit code sequence", = j*32);=0A+ memcpy(res, blobs[j].code, blobs[j].size);=0A+ = ctxt.addr_size =3D ctxt.sp_size =3D blobs[j].bitness;=0A+=0A+ = printf("Testing %s %u-bit code sequence",=0A+ blobs[j].name, = ctxt.addr_size);=0A regs.eax =3D 2;=0A regs.edx =3D 1;=0A = regs.eip =3D (unsigned long)res;=0A regs.esp =3D (unsigned = long)res + MMAP_SZ - 4;=0A- if ( j =3D=3D 2 )=0A+ if ( = ctxt.addr_size =3D=3D 64 )=0A {=0A- ctxt.addr_size =3D = ctxt.sp_size =3D 64;=0A *(uint32_t *)(unsigned long)regs.esp = =3D 0;=0A regs.esp -=3D 4;=0A }=0A@@ -983,20 +993,27 = @@ int main(int argc, char **argv)=0A (regs.eax !=3D 2) || = (regs.edx !=3D 1) )=0A goto fail;=0A printf("okay\n");= =0A- }=0A =0A- printf("%-40s", "Testing blowfish native execution..."= ); =0A- asm volatile (=0A+ if ( ctxt.addr_size !=3D sizeof(voi= d *) * CHAR_BIT )=0A+ continue;=0A+=0A+ i =3D printf("Tes= ting %s native execution...", blobs[j].name);=0A+ asm volatile (=0A = #if defined(__i386__)=0A- "movl $0x100000,%%ecx; call *%%ecx"=0A+ = "movl $0x100000,%%ecx; call *%%ecx"=0A #else=0A- "movl = $0x100000,%%ecx; call *%%rcx"=0A+ "movl $0x100000,%%ecx; call = *%%rcx"=0A #endif=0A- : "=3Da" (regs.eax), "=3Dd" (regs.edx)=0A- = : "0" (2), "1" (1) : "ecx" );=0A- if ( (regs.eax !=3D 2) || = (regs.edx !=3D 1) )=0A- goto fail;=0A- printf("okay\n");=0A+ = : "=3Da" (regs.eax), "=3Dd" (regs.edx)=0A+ : "0" (2), = "1" (1) : "ecx"=0A+#ifdef __x86_64__=0A+ , "rsi", "rdi", = "r8", "r9", "r10", "r11"=0A+#endif=0A+ );=0A+ if ( (regs.eax = !=3D 2) || (regs.edx !=3D 1) )=0A+ goto fail;=0A+ = printf("%*sokay\n", i < 40 ? 40 - i : 0, "");=0A+ }=0A =0A return = 0;=0A =0A--- a/xen/arch/x86/x86_emulate/x86_emulate.c=0A+++ b/xen/arch/x86/= x86_emulate/x86_emulate.c=0A@@ -94,8 +94,8 @@ static uint8_t opcode_table[2= 56] =3D {=0A ImplicitOps, ImplicitOps, DstReg|SrcMem|ModRM, DstReg|SrcN= one|ModRM|Mov,=0A 0, 0, 0, 0,=0A /* 0x68 - 0x6F */=0A- = ImplicitOps|Mov, DstReg|SrcImm|ModRM|Mov,=0A- ImplicitOps|Mov, = DstReg|SrcImmByte|ModRM|Mov,=0A+ DstImplicit|SrcImm|Mov, DstReg|SrcImm|M= odRM|Mov,=0A+ DstImplicit|SrcImmByte|Mov, DstReg|SrcImmByte|ModRM|Mov,= =0A ImplicitOps|Mov, ImplicitOps|Mov, ImplicitOps|Mov, ImplicitOps|Mov,= =0A /* 0x70 - 0x77 */=0A ImplicitOps, ImplicitOps, ImplicitOps, = ImplicitOps,=0A@@ -2335,10 +2335,15 @@ x86_emulate(=0A break;=0A = =0A case 0x68: /* push imm{16,32,64} */=0A- src.val =3D = ((op_bytes =3D=3D 2)=0A- ? (int32_t)insn_fetch_type(int16= _t)=0A- : insn_fetch_type(int32_t));=0A- goto = push;=0A+ case 0x6a: /* push imm8 */=0A+ push:=0A+ d |=3D = Mov; /* force writeback */=0A+ dst.type =3D OP_MEM;=0A+ = dst.bytes =3D mode_64bit() && (op_bytes =3D=3D 4) ? 8 : op_bytes;=0A+ = dst.val =3D src.val;=0A+ dst.mem.seg =3D x86_seg_ss;=0A+ = dst.mem.off =3D sp_pre_dec(dst.bytes);=0A+ break;=0A =0A case = 0x69: /* imul imm16/32 */=0A case 0x6b: /* imul imm8 */=0A@@ -2349,19 = +2354,6 @@ x86_emulate(=0A goto done;=0A goto imul;=0A = =0A- case 0x6a: /* push imm8 */=0A- src.val =3D insn_fetch_type(i= nt8_t);=0A- push:=0A- d |=3D Mov; /* force writeback */=0A- = dst.type =3D OP_MEM;=0A- dst.bytes =3D op_bytes;=0A- if ( = mode_64bit() && (dst.bytes =3D=3D 4) )=0A- dst.bytes =3D 8;=0A- = dst.val =3D src.val;=0A- dst.mem.seg =3D x86_seg_ss;=0A- = dst.mem.off =3D sp_pre_dec(dst.bytes);=0A- break;=0A-=0A = case 0x6c ... 0x6d: /* ins %dx,%es:%edi */ {=0A unsigned long = nr_reps =3D get_rep_prefix();=0A unsigned int port =3D (uint16_t)_r= egs.edx;=0A --=__Part72448488.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__Part72448488.1__=--