From: "Jan Beulich" Subject: [PATCH v2] nested SVM: adjust guest handling of structure mappings Date: Mon, 11 Nov 2013 11:07:31 +0000 Message-ID: <5280C8830200007800101BCD@nat28.tlf.novell.com> References: <5280A4640200007800101A29@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part6350BC63.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 1VfpLG-0004bo-Q2 for xen-devel@lists.xenproject.org; Mon, 11 Nov 2013 11:07:39 +0000 In-Reply-To: <5280A4640200007800101A29@nat28.tlf.novell.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: 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. --=__Part6350BC63.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 --- v2: Refine the change to nsvm_vmrun_permissionmap() - 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/svm/nestedsvm.c +++ b/xen/arch/x86/hvm/svm/nestedsvm.c @@ -342,7 +342,7 @@ static int nsvm_vmrun_permissionmap(stru unsigned int i; enum hvm_copy_result ret; unsigned long *ns_viomap; - bool_t ioport_80, ioport_ed; + bool_t ioport_80 =3D 1, ioport_ed =3D 1; =20 ns_msrpm_ptr =3D (unsigned long *)svm->ns_cached_msrpm; =20 @@ -360,10 +360,12 @@ 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); + if ( ns_viomap ) + { + ioport_80 =3D test_bit(0x80, ns_viomap); + ioport_ed =3D test_bit(0xed, ns_viomap); + hvm_unmap_guest_frame(ns_viomap, 0); + } =20 svm->ns_iomap =3D nestedhvm_vcpu_iomap_get(ioport_80, ioport_ed); =20 @@ -866,40 +868,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 +973,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 +982,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); --=__Part6350BC63.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---=0Av2: = Refine the change to nsvm_vmrun_permissionmap() -=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/svm/nestedsvm.c=0A+++ = b/xen/arch/x86/hvm/svm/nestedsvm.c=0A@@ -342,7 +342,7 @@ static int = nsvm_vmrun_permissionmap(stru=0A unsigned int i;=0A enum hvm_copy_r= esult ret;=0A unsigned long *ns_viomap;=0A- bool_t ioport_80, = ioport_ed;=0A+ bool_t ioport_80 =3D 1, ioport_ed =3D 1;=0A =0A = ns_msrpm_ptr =3D (unsigned long *)svm->ns_cached_msrpm;=0A =0A@@ -360,10 = +360,12 @@ 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_r= o(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_frame(ns_viomap, = 0);=0A+ if ( ns_viomap )=0A+ {=0A+ ioport_80 =3D test_bit(0x80= , ns_viomap);=0A+ ioport_ed =3D test_bit(0xed, ns_viomap);=0A+ = hvm_unmap_guest_frame(ns_viomap, 0);=0A+ }=0A =0A svm->ns_iomap = =3D nestedhvm_vcpu_iomap_get(ioport_80, ioport_ed);=0A =0A@@ -866,40 = +868,45 @@ nsvm_vmcb_guest_intercepts_msr(unsigned =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.field= s.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 +973,8 @@ nsvm_vmcb_guest_intercepts_exitcode(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_v= vmcx !=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 +982,8 @@ = nsvm_vmcb_guest_intercepts_exitcode(stru=0A return 0;=0A = break;=0A case VMEXIT_IOIO:=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_inte= rcepts_ioio(ns_vmcb->_iopm_base_pa,=0A ns_vmcb->exitinfo1);=0A --=__Part6350BC63.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 --=__Part6350BC63.2__=--