From: "Jan Beulich" Subject: [PATCH v2] nested VMX: don't ignore mapping errors Date: Mon, 11 Nov 2013 11:06:54 +0000 Message-ID: <5280C85E0200007800101BC9@nat28.tlf.novell.com> References: <5280A3080200007800101A18@nat28.tlf.novell.com> <5280B368.6030404@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part5E6D815E.2__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VfpKe-0004Za-J2 for xen-devel@lists.xenproject.org; Mon, 11 Nov 2013 11:07:00 +0000 In-Reply-To: <5280B368.6030404@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Andrew Cooper , Eddie Dong , Jun Nakajima 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. --=__Part5E6D815E.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Rather than ignoring failures to map the virtual VMCS as well as MSR or I/O port bitmaps, convert those into failures of the respective instructions (avoiding to dereference NULL pointers). Ultimately such failures should be handled transparently (by using transient mappings when they actually need to be accessed, just like nested SVM does). Once at it, also eliminate a pointless (and incomplete) result check: Non-global (i.e. non-permanent) map operations can't fail. Signed-off-by: Jan Beulich --- v2: Remove the change to nvmx_handle_vmclear() - hvm_map_guest_frame_*() can return NULL for reasons other than map_domain_page_global() doing so (pointed out by Andrew Cooper). That leaves the function broken though (it ought to handle the shared/paged out cases in an appropriate way). --- a/xen/arch/x86/hvm/vmx/vvmx.c +++ b/xen/arch/x86/hvm/vmx/vvmx.c @@ -747,7 +747,7 @@ static void __clear_current_vvmcs(struct __vmpclear(virt_to_maddr(nvcpu->nv_n2vmcx)); } =20 -static void __map_msr_bitmap(struct vcpu *v) +static bool_t __must_check _map_msr_bitmap(struct vcpu *v) { struct nestedvmx *nvmx =3D &vcpu_2_nvmx(v); unsigned long gpa; @@ -756,9 +756,11 @@ static void __map_msr_bitmap(struct vcpu hvm_unmap_guest_frame(nvmx->msrbitmap, 1); gpa =3D __get_vvmcs(vcpu_nestedhvm(v).nv_vvmcx, MSR_BITMAP); nvmx->msrbitmap =3D hvm_map_guest_frame_ro(gpa >> PAGE_SHIFT, 1); + + return nvmx->msrbitmap !=3D NULL; } =20 -static void __map_io_bitmap(struct vcpu *v, u64 vmcs_reg) +static bool_t __must_check _map_io_bitmap(struct vcpu *v, u64 vmcs_reg) { struct nestedvmx *nvmx =3D &vcpu_2_nvmx(v); unsigned long gpa; @@ -769,12 +771,14 @@ static void __map_io_bitmap(struct vcpu=20 hvm_unmap_guest_frame(nvmx->iobitmap[index], 1); gpa =3D __get_vvmcs(vcpu_nestedhvm(v).nv_vvmcx, vmcs_reg); nvmx->iobitmap[index] =3D hvm_map_guest_frame_ro(gpa >> PAGE_SHIFT, = 1); + + return nvmx->iobitmap[index] !=3D NULL; } =20 -static inline void map_io_bitmap_all(struct vcpu *v) +static inline bool_t __must_check map_io_bitmap_all(struct vcpu *v) { - __map_io_bitmap (v, IO_BITMAP_A); - __map_io_bitmap (v, IO_BITMAP_B); + return _map_io_bitmap(v, IO_BITMAP_A) && + _map_io_bitmap(v, IO_BITMAP_B); } =20 static void nvmx_purge_vvmcs(struct vcpu *v) @@ -1610,9 +1614,15 @@ int nvmx_handle_vmptrld(struct cpu_user_ if ( nvcpu->nv_vvmcxaddr =3D=3D VMCX_EADDR ) { nvcpu->nv_vvmcx =3D hvm_map_guest_frame_rw(gpa >> PAGE_SHIFT, 1); - nvcpu->nv_vvmcxaddr =3D gpa; - map_io_bitmap_all (v); - __map_msr_bitmap(v); + if ( nvcpu->nv_vvmcx ) + nvcpu->nv_vvmcxaddr =3D gpa; + if ( !nvcpu->nv_vvmcx || + !map_io_bitmap_all(v) || + !_map_msr_bitmap(v) ) + { + vmreturn(regs, VMFAIL_VALID); + goto out; + } } =20 if ( cpu_has_vmx_vmcs_shadowing ) @@ -1724,6 +1734,7 @@ int nvmx_handle_vmwrite(struct cpu_user_ struct nestedvcpu *nvcpu =3D &vcpu_nestedhvm(v); unsigned long operand;=20 u64 vmcs_encoding; + bool_t okay =3D 1; =20 if ( decode_vmx_inst(regs, &decode, &operand, 0) !=3D X86EMUL_OKAY ) @@ -1732,16 +1743,21 @@ int nvmx_handle_vmwrite(struct cpu_user_ vmcs_encoding =3D reg_read(regs, decode.reg2); __set_vvmcs(nvcpu->nv_vvmcx, vmcs_encoding, operand); =20 - if ( vmcs_encoding =3D=3D IO_BITMAP_A || vmcs_encoding =3D=3D = IO_BITMAP_A_HIGH ) - __map_io_bitmap (v, IO_BITMAP_A); - else if ( vmcs_encoding =3D=3D IO_BITMAP_B ||=20 - vmcs_encoding =3D=3D IO_BITMAP_B_HIGH ) - __map_io_bitmap (v, IO_BITMAP_B); + switch ( vmcs_encoding ) + { + case IO_BITMAP_A: case IO_BITMAP_A_HIGH: + okay =3D _map_io_bitmap(v, IO_BITMAP_A); + break; + case IO_BITMAP_B: case IO_BITMAP_B_HIGH: + okay =3D _map_io_bitmap(v, IO_BITMAP_B); + break; + case MSR_BITMAP: case MSR_BITMAP_HIGH: + okay =3D _map_msr_bitmap(v); + break; + } =20 - if ( vmcs_encoding =3D=3D MSR_BITMAP || vmcs_encoding =3D=3D = MSR_BITMAP_HIGH ) - __map_msr_bitmap(v); + vmreturn(regs, okay ? VMSUCCEED : VMFAIL_VALID); =20 - vmreturn(regs, VMSUCCEED); return X86EMUL_OKAY; } =20 --=__Part5E6D815E.2__= Content-Type: text/plain; name="nVMX-map-errors.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="nVMX-map-errors.patch" nested VMX: don't ignore mapping errors=0A=0ARather than ignoring failures = to map the virtual VMCS as well as MSR or=0AI/O port bitmaps, convert = those into failures of the respective=0Ainstructions (avoiding to = dereference NULL pointers). Ultimately such=0Afailures should be handled = transparently (by using transient mappings=0Awhen they actually need to be = accessed, just like nested SVM does).=0A=0AOnce at it, also eliminate a = pointless (and incomplete) result check:=0ANon-global (i.e. non-permanent) = map operations can't fail.=0A=0ASigned-off-by: Jan Beulich =0A---=0Av2: Remove the change to nvmx_handle_vmclear() -=0A = hvm_map_guest_frame_*() can return NULL for reasons other than=0A = map_domain_page_global() doing so (pointed out by Andrew Cooper).=0A = That leaves the function broken though (it ought to handle the=0A = shared/paged out cases in an appropriate way).=0A=0A--- a/xen/arch/x86/hvm/= vmx/vvmx.c=0A+++ b/xen/arch/x86/hvm/vmx/vvmx.c=0A@@ -747,7 +747,7 @@ = static void __clear_current_vvmcs(struct=0A __vmpclear(virt_to_madd= r(nvcpu->nv_n2vmcx));=0A }=0A =0A-static void __map_msr_bitmap(struct vcpu = *v)=0A+static bool_t __must_check _map_msr_bitmap(struct vcpu *v)=0A {=0A = struct nestedvmx *nvmx =3D &vcpu_2_nvmx(v);=0A unsigned long = gpa;=0A@@ -756,9 +756,11 @@ static void __map_msr_bitmap(struct vcpu=0A = hvm_unmap_guest_frame(nvmx->msrbitmap, 1);=0A gpa =3D __get_vvmcs(= vcpu_nestedhvm(v).nv_vvmcx, MSR_BITMAP);=0A nvmx->msrbitmap =3D = hvm_map_guest_frame_ro(gpa >> PAGE_SHIFT, 1);=0A+=0A+ return nvmx->msrbi= tmap !=3D NULL;=0A }=0A =0A-static void __map_io_bitmap(struct vcpu *v, = u64 vmcs_reg)=0A+static bool_t __must_check _map_io_bitmap(struct vcpu *v, = u64 vmcs_reg)=0A {=0A struct nestedvmx *nvmx =3D &vcpu_2_nvmx(v);=0A = unsigned long gpa;=0A@@ -769,12 +771,14 @@ static void __map_io_bitmap(st= ruct vcpu =0A hvm_unmap_guest_frame(nvmx->iobitmap[index], 1);=0A = gpa =3D __get_vvmcs(vcpu_nestedhvm(v).nv_vvmcx, vmcs_reg);=0A = nvmx->iobitmap[index] =3D hvm_map_guest_frame_ro(gpa >> PAGE_SHIFT, = 1);=0A+=0A+ return nvmx->iobitmap[index] !=3D NULL;=0A }=0A =0A-static = inline void map_io_bitmap_all(struct vcpu *v)=0A+static inline bool_t = __must_check map_io_bitmap_all(struct vcpu *v)=0A {=0A- __map_io_bitmap = (v, IO_BITMAP_A);=0A- __map_io_bitmap (v, IO_BITMAP_B);=0A+ return = _map_io_bitmap(v, IO_BITMAP_A) &&=0A+ _map_io_bitmap(v, IO_BITMAP_= B);=0A }=0A =0A static void nvmx_purge_vvmcs(struct vcpu *v)=0A@@ -1610,9 = +1614,15 @@ int nvmx_handle_vmptrld(struct cpu_user_=0A if ( nvcpu->nv_= vvmcxaddr =3D=3D VMCX_EADDR )=0A {=0A nvcpu->nv_vvmcx =3D = hvm_map_guest_frame_rw(gpa >> PAGE_SHIFT, 1);=0A- nvcpu->nv_vvmcxadd= r =3D gpa;=0A- map_io_bitmap_all (v);=0A- __map_msr_bitmap(v)= ;=0A+ if ( nvcpu->nv_vvmcx )=0A+ nvcpu->nv_vvmcxaddr =3D = gpa;=0A+ if ( !nvcpu->nv_vvmcx ||=0A+ !map_io_bitmap_all= (v) ||=0A+ !_map_msr_bitmap(v) )=0A+ {=0A+ = vmreturn(regs, VMFAIL_VALID);=0A+ goto out;=0A+ }=0A = }=0A =0A if ( cpu_has_vmx_vmcs_shadowing )=0A@@ -1724,6 +1734,7 @@ int = nvmx_handle_vmwrite(struct cpu_user_=0A struct nestedvcpu *nvcpu =3D = &vcpu_nestedhvm(v);=0A unsigned long operand; =0A u64 vmcs_encoding= ;=0A+ bool_t okay =3D 1;=0A =0A if ( decode_vmx_inst(regs, &decode, = &operand, 0)=0A !=3D X86EMUL_OKAY )=0A@@ -1732,16 +1743,21 @@ = int nvmx_handle_vmwrite(struct cpu_user_=0A vmcs_encoding =3D = reg_read(regs, decode.reg2);=0A __set_vvmcs(nvcpu->nv_vvmcx, vmcs_encod= ing, operand);=0A =0A- if ( vmcs_encoding =3D=3D IO_BITMAP_A || = vmcs_encoding =3D=3D IO_BITMAP_A_HIGH )=0A- __map_io_bitmap (v, = IO_BITMAP_A);=0A- else if ( vmcs_encoding =3D=3D IO_BITMAP_B || =0A- = vmcs_encoding =3D=3D IO_BITMAP_B_HIGH )=0A- __map_io_bitma= p (v, IO_BITMAP_B);=0A+ switch ( vmcs_encoding )=0A+ {=0A+ case = IO_BITMAP_A: case IO_BITMAP_A_HIGH:=0A+ okay =3D _map_io_bitmap(v, = IO_BITMAP_A);=0A+ break;=0A+ case IO_BITMAP_B: case IO_BITMAP_B_H= IGH:=0A+ okay =3D _map_io_bitmap(v, IO_BITMAP_B);=0A+ = break;=0A+ case MSR_BITMAP: case MSR_BITMAP_HIGH:=0A+ okay =3D = _map_msr_bitmap(v);=0A+ break;=0A+ }=0A =0A- if ( vmcs_encodin= g =3D=3D MSR_BITMAP || vmcs_encoding =3D=3D MSR_BITMAP_HIGH )=0A- = __map_msr_bitmap(v);=0A+ vmreturn(regs, okay ? VMSUCCEED : VMFAIL_VALID)= ;=0A =0A- vmreturn(regs, VMSUCCEED);=0A return X86EMUL_OKAY;=0A = }=0A =0A --=__Part5E6D815E.2__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part5E6D815E.2__=--