From: "Jan Beulich" Subject: Re: [PATCH] x86: miscellaneous emulator adjustments Date: Tue, 18 Aug 2009 15:55:04 +0100 Message-ID: <4A8ADCE80200007800010505@vpn.id2.novell.com> References: <4A8AC65902000078000104BF@vpn.id2.novell.com> <200908181612.37582.Christoph.Egger@amd.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <200908181612.37582.Christoph.Egger@amd.com> Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Christoph Egger Cc: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org I didn't do significantly more testing than what the test utility does = plus bring up a couple of HVM guests. Do you see anything obviously wrong with it (after all, it's mainly code re-ordering)? Jan >>> Christoph Egger 18.08.09 16:12 >>> How did you test this patch ? Christoph On Tuesday 18 August 2009 15:18:49 Jan Beulich wrote: > Defer fail_if()-s as much as possible (in favor of possibly generating > exceptions), and avoid generating exceptions when not strictly > necessary. > > Avoid fail_if()-s for simple return code checks (making the code that > used them consistent with other, longer existing code). > > Eliminate redundant generate_exception_if()-s checking lock_prefix > (which is already covered by the general check prior to decoding > operands). > > Also fix the testing code to add PROT_EXEC for the mapping that is > intended to have instruction executed from. > > Signed-off-by: Jan Beulich > > --- 2009-08-18.orig/tools/tests/test_x86_emulator.c 2008-07-18 > 16:19:34.000000000 +0200 +++ > 2009-08-18/tools/tests/test_x86_emulator.c 2009-08-18 14:18:20.0000000= 00 > +0200 @@ -76,7 +76,7 @@ int main(int argc, char **argv) > ctxt.addr_size =3D 32; > ctxt.sp_size =3D 32; > > - res =3D mmap((void *)0x100000, MMAP_SZ, PROT_READ|PROT_WRITE, > + res =3D mmap((void *)0x100000, MMAP_SZ, PROT_READ|PROT_WRITE|PROT_EX= EC, > MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); > if ( res =3D=3D MAP_FAILED ) > { > --- 2009-08-18.orig/xen/arch/x86/x86_emulate/x86_emulate.c 2009-08-18 > 14:18:15.000000000 +0200 +++ > 2009-08-18/xen/arch/x86/x86_emulate/x86_emulate.c 2009-08-18 > 14:18:20.000000000 +0200 @@ -3583,21 +3583,17 @@ x86_emulate( > struct segment_register cs =3D { 0 }, ss =3D { 0 }; > int rc; > > - fail_if(ops->read_msr =3D=3D NULL); > - fail_if(ops->read_segment =3D=3D NULL); > - fail_if(ops->write_segment =3D=3D NULL); > - > generate_exception_if(in_realmode(ctxt, ops), EXC_UD, 0); > generate_exception_if(!in_protmode(ctxt, ops), EXC_UD, 0); > - generate_exception_if(lock_prefix, EXC_UD, 0); > > /* Inject #UD if syscall/sysret are disabled. */ > - rc =3D ops->read_msr(MSR_EFER, &msr_content, ctxt); > - fail_if(rc !=3D 0); > + fail_if(ops->read_msr =3D=3D NULL); > + if ( (rc =3D ops->read_msr(MSR_EFER, &msr_content, ctxt)) !=3D = 0 ) > + goto done; > generate_exception_if((msr_content & EFER_SCE) =3D=3D 0, = EXC_UD, 0); > > - rc =3D ops->read_msr(MSR_STAR, &msr_content, ctxt); > - fail_if(rc !=3D 0); > + if ( (rc =3D ops->read_msr(MSR_STAR, &msr_content, ctxt)) !=3D = 0 ) > + goto done; > > msr_content >>=3D 32; > cs.sel =3D (uint16_t)(msr_content & 0xfffc); > @@ -3617,27 +3613,27 @@ x86_emulate( > _regs.rcx =3D _regs.rip; > _regs.r11 =3D _regs.eflags & ~EFLG_RF; > > - rc =3D ops->read_msr(mode_64bit() ? MSR_LSTAR : MSR_CSTAR, > - &msr_content, ctxt); > - fail_if(rc !=3D 0); > - > + if ( (rc =3D ops->read_msr(mode_64bit() ? MSR_LSTAR : = MSR_CSTAR, > + &msr_content, ctxt)) !=3D 0 ) > + goto done; > _regs.rip =3D msr_content; > > - rc =3D ops->read_msr(MSR_FMASK, &msr_content, ctxt); > - fail_if(rc !=3D 0); > + if ( (rc =3D ops->read_msr(MSR_FMASK, &msr_content, ctxt)) = !=3D 0 > ) + goto done; > _regs.eflags &=3D ~(msr_content | EFLG_RF); > } > else > #endif > { > - rc =3D ops->read_msr(MSR_STAR, &msr_content, ctxt); > - fail_if(rc !=3D 0); > + if ( (rc =3D ops->read_msr(MSR_STAR, &msr_content, ctxt)) = !=3D 0 ) > + goto done; > > _regs.ecx =3D _regs.eip; > _regs.eip =3D (uint32_t)msr_content; > _regs.eflags &=3D ~(EFLG_VM | EFLG_IF | EFLG_RF); > } > > + fail_if(ops->write_segment =3D=3D NULL); > if ( (rc =3D ops->write_segment(x86_seg_cs, &cs, ctxt)) || > (rc =3D ops->write_segment(x86_seg_ss, &ss, ctxt)) ) > goto done; > @@ -3717,10 +3713,13 @@ x86_emulate( > case 0x31: /* rdtsc */ { > unsigned long cr4; > uint64_t val; > - fail_if(ops->read_cr =3D=3D NULL); > - if ( (rc =3D ops->read_cr(4, &cr4, ctxt)) ) > - goto done; > - generate_exception_if((cr4 & CR4_TSD) && !mode_ring0(), EXC_GP, > 0); + if ( !mode_ring0() ) > + { > + fail_if(ops->read_cr =3D=3D NULL); > + if ( (rc =3D ops->read_cr(4, &cr4, ctxt)) ) > + goto done; > + generate_exception_if(cr4 & CR4_TSD, EXC_GP, 0); > + } > fail_if(ops->read_msr =3D=3D NULL); > if ( (rc =3D ops->read_msr(MSR_TSC, &val, ctxt)) !=3D 0 ) > goto done; > @@ -3751,17 +3750,13 @@ x86_emulate( > struct segment_register cs, ss; > int rc; > > - fail_if(ops->read_msr =3D=3D NULL); > - fail_if(ops->read_segment =3D=3D NULL); > - fail_if(ops->write_segment =3D=3D NULL); > - > generate_exception_if(mode_ring0(), EXC_GP, 0); > generate_exception_if(in_realmode(ctxt, ops), EXC_GP, 0); > generate_exception_if(!in_protmode(ctxt, ops), EXC_GP, 0); > - generate_exception_if(lock_prefix, EXC_UD, 0); > > - rc =3D ops->read_msr(MSR_SYSENTER_CS, &msr_content, ctxt); > - fail_if(rc !=3D 0); > + fail_if(ops->read_msr =3D=3D NULL); > + if ( (rc =3D ops->read_msr(MSR_SYSENTER_CS, &msr_content, = ctxt)) !=3D > 0 ) + goto done; > > if ( mode_64bit() ) > generate_exception_if(msr_content =3D=3D 0, EXC_GP, 0); > @@ -3770,6 +3765,7 @@ x86_emulate( > > _regs.eflags &=3D ~(EFLG_VM | EFLG_IF | EFLG_RF); > > + fail_if(ops->read_segment =3D=3D NULL); > ops->read_segment(x86_seg_cs, &cs, ctxt); > cs.sel =3D (uint16_t)msr_content & ~3; /* SELECTOR_RPL_MASK */ > cs.base =3D 0; /* flat segment */ > @@ -3787,17 +3783,17 @@ x86_emulate( > cs.attr.fields.l =3D 1; > } > > - rc =3D ops->write_segment(x86_seg_cs, &cs, ctxt); > - fail_if(rc !=3D 0); > - rc =3D ops->write_segment(x86_seg_ss, &ss, ctxt); > - fail_if(rc !=3D 0); > + fail_if(ops->write_segment =3D=3D NULL); > + if ( (rc =3D ops->write_segment(x86_seg_cs, &cs, ctxt)) !=3D 0 = || > + (rc =3D ops->write_segment(x86_seg_ss, &ss, ctxt)) !=3D 0 = ) > + goto done; > > - rc =3D ops->read_msr(MSR_SYSENTER_EIP, &msr_content, ctxt); > - fail_if(rc !=3D 0); > + if ( (rc =3D ops->read_msr(MSR_SYSENTER_EIP, &msr_content, = ctxt)) !=3D > 0 ) + goto done; > _regs.eip =3D msr_content; > > - rc =3D ops->read_msr(MSR_SYSENTER_ESP, &msr_content, ctxt); > - fail_if(rc !=3D 0); > + if ( (rc =3D ops->read_msr(MSR_SYSENTER_ESP, &msr_content, = ctxt)) !=3D > 0 ) + goto done; > _regs.esp =3D msr_content; > > break; > @@ -3809,19 +3805,13 @@ x86_emulate( > int user64 =3D !!(rex_prefix & 8); /* REX.W */ > int rc; > > - fail_if(ops->read_msr =3D=3D NULL); > - fail_if(ops->read_segment =3D=3D NULL); > - fail_if(ops->write_segment =3D=3D NULL); > - > generate_exception_if(!mode_ring0(), EXC_GP, 0); > generate_exception_if(in_realmode(ctxt, ops), EXC_GP, 0); > generate_exception_if(!in_protmode(ctxt, ops), EXC_GP, 0); > - generate_exception_if(lock_prefix, EXC_UD, 0); > > - rc =3D ops->read_msr(MSR_SYSENTER_CS, &msr_content, ctxt); > - fail_if(rc !=3D 0); > - rc =3D ops->read_segment(x86_seg_cs, &cs, ctxt); > - fail_if(rc !=3D 0); > + fail_if(ops->read_msr =3D=3D NULL); > + if ( (rc =3D ops->read_msr(MSR_SYSENTER_CS, &msr_content, = ctxt)) !=3D > 0 ) + goto done; > > if ( user64 ) > { > @@ -3852,10 +3842,10 @@ x86_emulate( > cs.attr.fields.l =3D 1; > } > > - rc =3D ops->write_segment(x86_seg_cs, &cs, ctxt); > - fail_if(rc !=3D 0); > - rc =3D ops->write_segment(x86_seg_ss, &ss, ctxt); > - fail_if(rc !=3D 0); > + fail_if(ops->write_segment =3D=3D NULL); > + if ( (rc =3D ops->write_segment(x86_seg_cs, &cs, ctxt)) !=3D 0 = || > + (rc =3D ops->write_segment(x86_seg_ss, &ss, ctxt)) !=3D 0 = ) > + goto done; > > _regs.eip =3D _regs.edx; > _regs.esp =3D _regs.ecx; > > > > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xensource.com=20 > http://lists.xensource.com/xen-devel=20 --=20 ---to satisfy European Law for business letters: Advanced Micro Devices GmbH Karl-Hammerschmidt-Str. 34, 85609 Dornach b. Muenchen Geschaeftsfuehrer: Thomas M. McCoy, Giuliano Meroni Sitz: Dornach, Gemeinde Aschheim, Landkreis Muenchen Registergericht Muenchen, HRB Nr. 43632