From: "Jan Beulich" Subject: [PATCH] nested SVM: adjust guest handling of structure mappings Date: Mon, 11 Nov 2013 08:33:24 +0000 Message-ID: <5280A4640200007800101A29@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part685BB744.2__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1Vfmw7-0003uf-Hv for xen-devel@lists.xenproject.org; Mon, 11 Nov 2013 08:33:31 +0000 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: Boris Ostrovsky , Christoph Egger , suravee.suthikulpanit@amd.com 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. --=__Part685BB744.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline For one, nestedsvm_vmcb_map() error checking must not consist of using assertions: Global (permanent) mappings can fail, and hence failure needs to be dealt with properly. And non-global (transient) mappings can't fail anyway. And then the I/O port access bitmap handling was broken: It checked only to first of the accessed ports rather than each of them. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -360,7 +360,6 @@ static int nsvm_vmrun_permissionmap(stru svm->ns_iomap_pa =3D ns_vmcb->_iopm_base_pa; =20 ns_viomap =3D hvm_map_guest_frame_ro(svm->ns_iomap_pa >> PAGE_SHIFT, = 0); - ASSERT(ns_viomap !=3D NULL); ioport_80 =3D test_bit(0x80, ns_viomap); ioport_ed =3D test_bit(0xed, ns_viomap); hvm_unmap_guest_frame(ns_viomap, 0); @@ -866,40 +865,45 @@ nsvm_vmcb_guest_intercepts_msr(unsigned=20 static int nsvm_vmcb_guest_intercepts_ioio(paddr_t iopm_pa, uint64_t exitinfo1) { - unsigned long iopm_gfn =3D iopm_pa >> PAGE_SHIFT; - unsigned long *io_bitmap =3D NULL; + unsigned long gfn =3D iopm_pa >> PAGE_SHIFT; + unsigned long *io_bitmap; ioio_info_t ioinfo; uint16_t port; + unsigned int size; bool_t enabled; - unsigned long gfn =3D 0; /* gcc ... */ =20 ioinfo.bytes =3D exitinfo1; port =3D ioinfo.fields.port; + size =3D ioinfo.fields.sz32 ? 4 : ioinfo.fields.sz16 ? 2 : 1; =20 - switch (port) { - case 0 ... 32767: /* first 4KB page */ - gfn =3D iopm_gfn; + switch ( port ) + { + case 0 ... 8 * PAGE_SIZE - 1: /* first 4KB page */ break; - case 32768 ... 65535: /* second 4KB page */ - port -=3D 32768; - gfn =3D iopm_gfn + 1; + case 8 * PAGE_SIZE ... 2 * 8 * PAGE_SIZE - 1: /* second 4KB page */ + port -=3D 8 * PAGE_SIZE; + ++gfn; break; default: BUG(); break; } =20 - io_bitmap =3D hvm_map_guest_frame_ro(gfn, 0); - if (io_bitmap =3D=3D NULL) { - gdprintk(XENLOG_ERR, - "IOIO intercept: mapping of permission map failed\n"); - return NESTEDHVM_VMEXIT_ERROR; + for ( io_bitmap =3D hvm_map_guest_frame_ro(gfn, 0); ; ) + { + enabled =3D test_bit(port, io_bitmap); + if ( !enabled || !--size ) + break; + if ( unlikely(++port =3D=3D 8 * PAGE_SIZE) ) + { + hvm_unmap_guest_frame(io_bitmap, 0); + io_bitmap =3D hvm_map_guest_frame_ro(++gfn, 0); + port -=3D 8 * PAGE_SIZE; + } } - - enabled =3D test_bit(port, io_bitmap); hvm_unmap_guest_frame(io_bitmap, 0); =20 - if (!enabled) + if ( !enabled ) return NESTEDHVM_VMEXIT_HOST; =20 return NESTEDHVM_VMEXIT_INJECT; @@ -966,8 +970,8 @@ nsvm_vmcb_guest_intercepts_exitcode(stru switch (exitcode) { case VMEXIT_MSR: ASSERT(regs !=3D NULL); - nestedsvm_vmcb_map(v, nv->nv_vvmcxaddr); - ASSERT(nv->nv_vvmcx !=3D NULL); + if ( !nestedsvm_vmcb_map(v, nv->nv_vvmcxaddr) ) + break; ns_vmcb =3D nv->nv_vvmcx; vmexits =3D nsvm_vmcb_guest_intercepts_msr(svm->ns_cached_msrpm, regs->ecx, ns_vmcb->exitinfo1 !=3D 0); @@ -975,8 +979,8 @@ nsvm_vmcb_guest_intercepts_exitcode(stru return 0; break; case VMEXIT_IOIO: - nestedsvm_vmcb_map(v, nv->nv_vvmcxaddr); - ASSERT(nv->nv_vvmcx !=3D NULL); + if ( !nestedsvm_vmcb_map(v, nv->nv_vvmcxaddr) ) + break; ns_vmcb =3D nv->nv_vvmcx; vmexits =3D nsvm_vmcb_guest_intercepts_ioio(ns_vmcb->_iopm_base_pa= , ns_vmcb->exitinfo1); --=__Part685BB744.2__= Content-Type: text/plain; name="nSVM-map-errors.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="nSVM-map-errors.patch" nested SVM: adjust guest handling of structure mappings=0A=0AFor one, = nestedsvm_vmcb_map() error checking must not consist of using=0Aassertions:= Global (permanent) mappings can fail, and hence failure=0Aneeds to be = dealt with properly. And non-global (transient) mappings=0Acan't fail = anyway.=0A=0AAnd then the I/O port access bitmap handling was broken: It = checked=0Aonly to first of the accessed ports rather than each of = them.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/xen/arch/x86/hvm/svm/nestedsvm.c=0A+++ b/xen/arch/x86/hvm/svm/nestedsvm.c= =0A@@ -360,7 +360,6 @@ static int nsvm_vmrun_permissionmap(stru=0A = svm->ns_iomap_pa =3D ns_vmcb->_iopm_base_pa;=0A =0A ns_viomap =3D = hvm_map_guest_frame_ro(svm->ns_iomap_pa >> PAGE_SHIFT, 0);=0A- = ASSERT(ns_viomap !=3D NULL);=0A ioport_80 =3D test_bit(0x80, ns_viomap)= ;=0A ioport_ed =3D test_bit(0xed, ns_viomap);=0A hvm_unmap_guest_fr= ame(ns_viomap, 0);=0A@@ -866,40 +865,45 @@ nsvm_vmcb_guest_intercepts_msr(u= nsigned =0A static int=0A nsvm_vmcb_guest_intercepts_ioio(paddr_t iopm_pa, = uint64_t exitinfo1)=0A {=0A- unsigned long iopm_gfn =3D iopm_pa >> = PAGE_SHIFT;=0A- unsigned long *io_bitmap =3D NULL;=0A+ unsigned long = gfn =3D iopm_pa >> PAGE_SHIFT;=0A+ unsigned long *io_bitmap;=0A = ioio_info_t ioinfo;=0A uint16_t port;=0A+ unsigned int size;=0A = bool_t enabled;=0A- unsigned long gfn =3D 0; /* gcc ... */=0A =0A = ioinfo.bytes =3D exitinfo1;=0A port =3D ioinfo.fields.port;=0A+ = size =3D ioinfo.fields.sz32 ? 4 : ioinfo.fields.sz16 ? 2 : 1;=0A =0A- = switch (port) {=0A- case 0 ... 32767: /* first 4KB page */=0A- = gfn =3D iopm_gfn;=0A+ switch ( port )=0A+ {=0A+ case 0 ... 8 * = PAGE_SIZE - 1: /* first 4KB page */=0A break;=0A- case 32768 = ... 65535: /* second 4KB page */=0A- port -=3D 32768;=0A- = gfn =3D iopm_gfn + 1;=0A+ case 8 * PAGE_SIZE ... 2 * 8 * PAGE_SIZE - 1: = /* second 4KB page */=0A+ port -=3D 8 * PAGE_SIZE;=0A+ = ++gfn;=0A break;=0A default:=0A BUG();=0A = break;=0A }=0A =0A- io_bitmap =3D hvm_map_guest_frame_ro(gfn, = 0);=0A- if (io_bitmap =3D=3D NULL) {=0A- gdprintk(XENLOG_ERR,=0A-= "IOIO intercept: mapping of permission map failed\n");=0A- = return NESTEDHVM_VMEXIT_ERROR;=0A+ for ( io_bitmap =3D hvm_map_guest_= frame_ro(gfn, 0); ; )=0A+ {=0A+ enabled =3D test_bit(port, = io_bitmap);=0A+ if ( !enabled || !--size )=0A+ break;=0A+= if ( unlikely(++port =3D=3D 8 * PAGE_SIZE) )=0A+ {=0A+ = hvm_unmap_guest_frame(io_bitmap, 0);=0A+ io_bitmap =3D = hvm_map_guest_frame_ro(++gfn, 0);=0A+ port -=3D 8 * PAGE_SIZE;= =0A+ }=0A }=0A-=0A- enabled =3D test_bit(port, io_bitmap);=0A= hvm_unmap_guest_frame(io_bitmap, 0);=0A =0A- if (!enabled)=0A+ = if ( !enabled )=0A return NESTEDHVM_VMEXIT_HOST;=0A =0A return = NESTEDHVM_VMEXIT_INJECT;=0A@@ -966,8 +970,8 @@ nsvm_vmcb_guest_intercepts_e= xitcode(stru=0A switch (exitcode) {=0A case VMEXIT_MSR:=0A = ASSERT(regs !=3D NULL);=0A- nestedsvm_vmcb_map(v, nv->nv_vvmcxaddr);= =0A- ASSERT(nv->nv_vvmcx !=3D NULL);=0A+ if ( !nestedsvm_vmcb= _map(v, nv->nv_vvmcxaddr) )=0A+ break;=0A ns_vmcb =3D = nv->nv_vvmcx;=0A vmexits =3D nsvm_vmcb_guest_intercepts_msr(svm->ns= _cached_msrpm,=0A regs->ecx, ns_vmcb->exitinfo1 !=3D 0);=0A@@ = -975,8 +979,8 @@ nsvm_vmcb_guest_intercepts_exitcode(stru=0A = return 0;=0A break;=0A case VMEXIT_IOIO:=0A- nestedsvm_v= mcb_map(v, nv->nv_vvmcxaddr);=0A- ASSERT(nv->nv_vvmcx !=3D = NULL);=0A+ if ( !nestedsvm_vmcb_map(v, nv->nv_vvmcxaddr) )=0A+ = break;=0A ns_vmcb =3D nv->nv_vvmcx;=0A vmexits =3D = nsvm_vmcb_guest_intercepts_ioio(ns_vmcb->_iopm_base_pa,=0A = ns_vmcb->exitinfo1);=0A --=__Part685BB744.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 --=__Part685BB744.2__=--