From: "Jan Beulich" Subject: Re: [PATCH 2/5] xen/vm_access: Support for memory-content hiding Date: Fri, 08 May 2015 17:07:50 +0100 Message-ID: <554CFB760200007800078703@mail.emea.novell.com> References: <1430932352-4289-1-git-send-email-rcojocaru@bitdefender.com> <1430932352-4289-3-git-send-email-rcojocaru@bitdefender.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1430932352-4289-3-git-send-email-rcojocaru@bitdefender.com> Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Razvan Cojocaru Cc: tim@xen.org, kevin.tian@intel.com, wei.liu2@citrix.com, ian.campbell@citrix.com, stefano.stabellini@eu.citrix.com, jun.nakajima@intel.com, andrew.cooper3@citrix.com, ian.jackson@eu.citrix.com, xen-devel@lists.xen.org, eddie.dong@intel.com, Aravind.Gopalakrishnan@amd.com, suravee.suthikulpanit@amd.com, keir@xen.org, boris.ostrovsky@oracle.com List-Id: xen-devel@lists.xenproject.org >>> On 06.05.15 at 19:12, wrote: > --- a/xen/arch/x86/hvm/emulate.c > +++ b/xen/arch/x86/hvm/emulate.c > @@ -578,6 +578,25 @@ static int hvmemul_read( > container_of(ctxt, struct hvm_emulate_ctxt, ctxt)); > } > > +static int hvmemul_read_set_context( > + enum x86_segment seg, > + unsigned long offset, > + void *p_data, > + unsigned int bytes, > + struct x86_emulate_ctxt *ctxt) > +{ > + struct vcpu *curr = current; > + unsigned int len = > + (bytes > curr->arch.vm_event.emul_read_data.size ? > + curr->arch.vm_event.emul_read_data.size : bytes); min() > + if ( len ) > + memcpy(p_data, curr->arch.vm_event.emul_read_data.data, > + curr->arch.vm_event.emul_read_data.size); Not len? And what happens to the portion of the read beyond "bytes" (in case that got reduced above)? > @@ -970,20 +990,38 @@ static int hvmemul_rep_movs( > if ( df ) > dgpa -= bytes - bytes_per_rep; > > - /* Allocate temporary buffer. Fall back to slow emulation if this fails. */ > - buf = xmalloc_bytes(bytes); > - if ( buf == NULL ) > - return X86EMUL_UNHANDLEABLE; > + if ( unlikely(set_context) ) > + { > + struct vcpu* curr = current; * and blank need to switch places. > - /* > - * We do a modicum of checking here, just for paranoia's sake and to > - * definitely avoid copying an unitialised buffer into guest address space. > - */ > - rc = hvm_copy_from_guest_phys(buf, sgpa, bytes); > - if ( rc == HVMCOPY_okay ) > - rc = hvm_copy_to_guest_phys(dgpa, buf, bytes); > + bytes = bytes < curr->arch.vm_event.emul_read_data.size ? > + bytes : curr->arch.vm_event.emul_read_data.size; min() again. > - xfree(buf); > + rc = hvm_copy_to_guest_phys(dgpa, > + curr->arch.vm_event.emul_read_data.data, > + bytes); Again - what happens to the portion of the MOVS beyond "bytes" (in case that got reduced above)? I think you need to carefully update *reps (without losing any bytes), or otherwise make this fall through into what is the "else" branch below. > @@ -1000,6 +1038,32 @@ static int hvmemul_rep_movs( > return X86EMUL_OKAY; > } > > +static int hvmemul_rep_movs( > + enum x86_segment src_seg, > + unsigned long src_offset, > + enum x86_segment dst_seg, > + unsigned long dst_offset, > + unsigned int bytes_per_rep, > + unsigned long *reps, > + struct x86_emulate_ctxt *ctxt) > +{ > + return _hvmemul_rep_movs(src_seg, src_offset, dst_seg, dst_offset, > + bytes_per_rep, reps, ctxt, 0); > +} > + > +static int hvmemul_rep_movs_set_context( > + enum x86_segment src_seg, > + unsigned long src_offset, > + enum x86_segment dst_seg, > + unsigned long dst_offset, > + unsigned int bytes_per_rep, > + unsigned long *reps, > + struct x86_emulate_ctxt *ctxt) > +{ > + return _hvmemul_rep_movs(src_seg, src_offset, dst_seg, dst_offset, > + bytes_per_rep, reps, ctxt, 1); > +} To be honest, these kinds of wrappers for very special, niche purposes worry me. > @@ -1107,6 +1171,22 @@ static int hvmemul_rep_stos( > } > } > > +static int hvmemul_rep_stos_set_context( > + void *p_data, > + enum x86_segment seg, > + unsigned long offset, > + unsigned int bytes_per_rep, > + unsigned long *reps, > + struct x86_emulate_ctxt *ctxt) > +{ > + struct vcpu *curr = current; > + unsigned long local_reps = 1; > + > + return hvmemul_rep_stos(curr->arch.vm_event.emul_read_data.data, seg, > + offset, curr->arch.vm_event.emul_read_data.size, > + &local_reps, ctxt); > +} How come here you can get away by simply reducing *reps to 1? And considering this patch is about supplying read values - why is fiddling with STOS emulation necessary in the first place? > +static const struct x86_emulate_ops hvm_emulate_ops_set_context = { > + .read = hvmemul_read_set_context, > + .insn_fetch = hvmemul_insn_fetch, > + .write = hvmemul_write, > + .cmpxchg = hvmemul_cmpxchg, How about this one? > + .rep_ins = hvmemul_rep_ins, > + .rep_outs = hvmemul_rep_outs, And this? > + .rep_movs = hvmemul_rep_movs_set_context, > + .rep_stos = hvmemul_rep_stos_set_context, > + .read_segment = hvmemul_read_segment, > + .write_segment = hvmemul_write_segment, > + .read_io = hvmemul_read_io, And this? > +void hvm_mem_access_emulate_one(enum emul_kind kind, unsigned int trapnr, > unsigned int errcode) > { > struct hvm_emulate_ctxt ctx = {{ 0 }}; > - int rc; > + int rc = X86EMUL_UNHANDLEABLE; > > hvm_emulate_prepare(&ctx, guest_cpu_user_regs()); > > - if ( nowrite ) > + switch ( kind ) { > + case EMUL_KIND_NOWRITE: > rc = hvm_emulate_one_no_write(&ctx); > - else > + break; > + case EMUL_KIND_NORMAL: > rc = hvm_emulate_one(&ctx); Mind having "NORMAL" as the first case in the switch()? > --- a/xen/include/asm-x86/domain.h > +++ b/xen/include/asm-x86/domain.h > @@ -512,6 +513,7 @@ struct arch_vcpu > uint32_t emulate_flags; > unsigned long gpa; > unsigned long eip; > + struct vm_event_emul_read_data emul_read_data; Considering the size of this structure I don't think this should be there for all vCPU-s of all guests. IOW - please allocate this dynamically only on domains where it's needed. > @@ -193,6 +200,11 @@ struct vm_event_xsetbv { > uint64_t value; > }; > > +struct vm_event_emul_read_data { > + uint32_t size; > + uint8_t data[164]; This number needs an explanation. Jan