From: "Jan Beulich" Subject: [PATCH 2/5] x86/HVM: fix direct PCI port I/O emulation retry and error handling Date: Mon, 30 Sep 2013 13:57:52 +0100 Message-ID: <5249917002000078000F807B@nat28.tlf.novell.com> References: <52498FFC02000078000F8068@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part46755040.0__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VQd30-0003nl-4A for xen-devel@lists.xenproject.org; Mon, 30 Sep 2013 12:57:58 +0000 In-Reply-To: <52498FFC02000078000F8068@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: Keir Fraser 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. --=__Part46755040.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline dpci_ioport_{read,write}() guest memory access failure handling should be modelled after process_portio_intercept()'s (and others): Upon encountering an error on other than the first iteration, the count successfully handled needs to be stored and X86EMUL_OKAY returned, in order for the generic instruction emulator to update register state correctly before reporting failure or retrying (both of which would only happen after re-invoking emulation). Further we leverage (and slightly extend, due to the above mentioned need to return X86EMUL_OKAY) the "large MMIO" retry model. Note that there is still a special case not explicitly taken care of here: While the first retry on the last iteration of a "rep ins" correctly recovers the already read data, an eventual subsequent retry is being handled by the pre-existing mmio-large logic (through hvmemul_do_io() storing the [recovered] data [again], also taking into consideration that the emulator converts a single iteration "ins" to ->read_io() plus ->write()). Also fix an off-by-one in the mmio-large-read logic, and slightly simplify the copying of the data. Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/emulate.c +++ b/xen/arch/x86/hvm/emulate.c @@ -173,6 +173,13 @@ static int hvmemul_do_io( (p_data =3D=3D NULL) ? HVMIO_dispatched : HVMIO_awaiting_completio= n; vio->io_size =3D size; =20 + /* + * When retrying a repeated string instruction, force exit to guest = after + * completion of the retried iteration to allow handling of interrupts= . + */ + if ( vio->mmio_retrying ) + *reps =3D 1; + p->dir =3D dir; p->data_is_ptr =3D value_is_ptr; p->type =3D is_mmio ? IOREQ_TYPE_COPY : IOREQ_TYPE_PIO; @@ -202,8 +209,14 @@ static int hvmemul_do_io( case X86EMUL_RETRY: *reps =3D p->count; p->state =3D STATE_IORESP_READY; - hvm_io_assist(); - vio->io_state =3D HVMIO_none; + if ( !vio->mmio_retry ) + { + hvm_io_assist(); + vio->io_state =3D HVMIO_none; + } + else + /* Defer hvm_io_assist() invocation to hvm_do_resume(). */ + vio->io_state =3D HVMIO_handle_mmio_awaiting_completion; break; case X86EMUL_UNHANDLEABLE: rc =3D X86EMUL_RETRY; @@ -249,10 +262,9 @@ static int hvmemul_do_io( if ( bytes =3D=3D 0 ) pa =3D vio->mmio_large_read_pa =3D addr; if ( (addr =3D=3D (pa + bytes)) && - ((bytes + size) < - sizeof(vio->mmio_large_read)) ) + ((bytes + size) <=3D sizeof(vio->mmio_large_read)) ) { - memcpy(&vio->mmio_large_read[addr - pa], p_data, size); + memcpy(&vio->mmio_large_read[bytes], p_data, size); vio->mmio_large_read_bytes +=3D size; } } @@ -1151,9 +1163,13 @@ int hvm_emulate_one( ? sizeof(hvmemul_ctxt->insn_buf) : 0; =20 hvmemul_ctxt->exn_pending =3D 0; + vio->mmio_retrying =3D vio->mmio_retry; + vio->mmio_retry =3D 0; =20 rc =3D x86_emulate(&hvmemul_ctxt->ctxt, &hvm_emulate_ops); =20 + if ( rc =3D=3D X86EMUL_OKAY && vio->mmio_retry ) + rc =3D X86EMUL_RETRY; if ( rc !=3D X86EMUL_RETRY ) vio->mmio_large_read_bytes =3D vio->mmio_large_write_bytes =3D 0; =20 --- a/xen/arch/x86/hvm/io.c +++ b/xen/arch/x86/hvm/io.c @@ -294,12 +294,21 @@ void hvm_io_assist(void) =20 static int dpci_ioport_read(uint32_t mport, ioreq_t *p) { - int i, step =3D p->df ? -p->size : p->size; + struct hvm_vcpu_io *vio =3D ¤t->arch.hvm_vcpu.hvm_io; + int rc =3D X86EMUL_OKAY, i, step =3D p->df ? -p->size : p->size; uint32_t data =3D 0; =20 for ( i =3D 0; i < p->count; i++ ) { - switch ( p->size ) + if ( vio->mmio_retrying ) + { + if ( vio->mmio_large_read_bytes !=3D p->size ) + return X86EMUL_UNHANDLEABLE; + memcpy(&data, vio->mmio_large_read, p->size); + vio->mmio_large_read_bytes =3D 0; + vio->mmio_retrying =3D 0; + } + else switch ( p->size ) { case 1: data =3D inb(mport); @@ -316,22 +325,51 @@ static int dpci_ioport_read(uint32_t mpo =20 if ( p->data_is_ptr ) { - int ret; - ret =3D hvm_copy_to_guest_phys(p->data + step * i, &data, = p->size); - if ( (ret =3D=3D HVMCOPY_gfn_paged_out) || - (ret =3D=3D HVMCOPY_gfn_shared) ) - return X86EMUL_RETRY; + switch ( hvm_copy_to_guest_phys(p->data + step * i, + &data, p->size) ) + { + case HVMCOPY_okay: + break; + case HVMCOPY_gfn_paged_out: + case HVMCOPY_gfn_shared: + rc =3D X86EMUL_RETRY; + break; + case HVMCOPY_bad_gfn_to_mfn: + /* Drop the write as real hardware would. */ + continue; + case HVMCOPY_bad_gva_to_gfn: + ASSERT(0); + /* fall through */ + default: + rc =3D X86EMUL_UNHANDLEABLE; + break; + } + if ( rc !=3D X86EMUL_OKAY) + break; } else p->data =3D data; } =20 - return X86EMUL_OKAY; + if ( rc =3D=3D X86EMUL_RETRY ) + { + vio->mmio_retry =3D 1; + vio->mmio_large_read_bytes =3D p->size; + memcpy(vio->mmio_large_read, &data, p->size); + } + + if ( i !=3D 0 ) + { + p->count =3D i; + rc =3D X86EMUL_OKAY; + } + + return rc; } =20 static int dpci_ioport_write(uint32_t mport, ioreq_t *p) { - int i, step =3D p->df ? -p->size : p->size; + int rc =3D X86EMUL_OKAY, i, step =3D p->df ? -p->size : p->size; uint32_t data; =20 for ( i =3D 0; i < p->count; i++ ) @@ -346,7 +384,8 @@ static int dpci_ioport_write(uint32_t mp break; case HVMCOPY_gfn_paged_out: case HVMCOPY_gfn_shared: - return X86EMUL_RETRY; + rc =3D X86EMUL_RETRY; + break; case HVMCOPY_bad_gfn_to_mfn: data =3D ~0; break; @@ -354,8 +393,11 @@ static int dpci_ioport_write(uint32_t mp ASSERT(0); /* fall through */ default: - return X86EMUL_UNHANDLEABLE; + rc =3D X86EMUL_UNHANDLEABLE; + break; } + if ( rc !=3D X86EMUL_OKAY) + break; } =20 switch ( p->size ) @@ -374,7 +416,16 @@ static int dpci_ioport_write(uint32_t mp } } =20 - return X86EMUL_OKAY; + if ( rc =3D=3D X86EMUL_RETRY ) + current->arch.hvm_vcpu.hvm_io.mmio_retry =3D 1; + + if ( i !=3D 0 ) + { + p->count =3D i; + rc =3D X86EMUL_OKAY; + } + + return rc; } =20 int dpci_ioport_intercept(ioreq_t *p) --- a/xen/include/asm-x86/hvm/vcpu.h +++ b/xen/include/asm-x86/hvm/vcpu.h @@ -66,6 +66,11 @@ struct hvm_vcpu_io { /* We may write up to m256 as a number of device-model transactions. = */ unsigned int mmio_large_write_bytes; paddr_t mmio_large_write_pa; + /* + * For string instruction emulation we need to be able to signal a + * necessary retry through other than function return codes. + */ + bool_t mmio_retry, mmio_retrying; }; =20 #define VMCX_EADDR (~0ULL) --=__Part46755040.0__= Content-Type: text/plain; name="x86-HVM-dpci-string-io.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HVM-dpci-string-io.patch" x86/HVM: fix direct PCI port I/O emulation retry and error handling=0A=0Adp= ci_ioport_{read,write}() guest memory access failure handling should=0Abe = modelled after process_portio_intercept()'s (and others): Upon=0Aencounteri= ng an error on other than the first iteration, the count=0Asuccessfully = handled needs to be stored and X86EMUL_OKAY returned, in=0Aorder for the = generic instruction emulator to update register state=0Acorrectly before = reporting failure or retrying (both of which would=0Aonly happen after = re-invoking emulation).=0A=0AFurther we leverage (and slightly extend, due = to the above mentioned=0Aneed to return X86EMUL_OKAY) the "large MMIO" = retry model.=0A=0ANote that there is still a special case not explicitly = taken care of=0Ahere: While the first retry on the last iteration of a = "rep ins"=0Acorrectly recovers the already read data, an eventual = subsequent retry=0Ais being handled by the pre-existing mmio-large logic = (through=0Ahvmemul_do_io() storing the [recovered] data [again], also = taking into=0Aconsideration that the emulator converts a single iteration = "ins" to=0A->read_io() plus ->write()).=0A=0AAlso fix an off-by-one in the = mmio-large-read logic, and slightly=0Asimplify the copying of the = data.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/xen/arch/x86/hvm/emulate.c=0A+++ b/xen/arch/x86/hvm/emulate.c=0A@@ = -173,6 +173,13 @@ static int hvmemul_do_io(=0A (p_data =3D=3D = NULL) ? HVMIO_dispatched : HVMIO_awaiting_completion;=0A vio->io_size = =3D size;=0A =0A+ /*=0A+ * When retrying a repeated string = instruction, force exit to guest after=0A+ * completion of the retried = iteration to allow handling of interrupts.=0A+ */=0A+ if ( = vio->mmio_retrying )=0A+ *reps =3D 1;=0A+=0A p->dir =3D dir;=0A = p->data_is_ptr =3D value_is_ptr;=0A p->type =3D is_mmio ? = IOREQ_TYPE_COPY : IOREQ_TYPE_PIO;=0A@@ -202,8 +209,14 @@ static int = hvmemul_do_io(=0A case X86EMUL_RETRY:=0A *reps =3D p->count;=0A= p->state =3D STATE_IORESP_READY;=0A- hvm_io_assist();=0A- = vio->io_state =3D HVMIO_none;=0A+ if ( !vio->mmio_retry )=0A+ = {=0A+ hvm_io_assist();=0A+ vio->io_state =3D = HVMIO_none;=0A+ }=0A+ else=0A+ /* Defer hvm_io_ass= ist() invocation to hvm_do_resume(). */=0A+ vio->io_state =3D = HVMIO_handle_mmio_awaiting_completion;=0A break;=0A case = X86EMUL_UNHANDLEABLE:=0A rc =3D X86EMUL_RETRY;=0A@@ -249,10 +262,9 = @@ static int hvmemul_do_io(=0A if ( bytes =3D=3D 0 )=0A = pa =3D vio->mmio_large_read_pa =3D addr;=0A if ( = (addr =3D=3D (pa + bytes)) &&=0A- ((bytes + size) <=0A- = sizeof(vio->mmio_large_read)) )=0A+ ((bytes = + size) <=3D sizeof(vio->mmio_large_read)) )=0A {=0A- = memcpy(&vio->mmio_large_read[addr - pa], p_data, size);=0A+ = memcpy(&vio->mmio_large_read[bytes], p_data, size);=0A = vio->mmio_large_read_bytes +=3D size;=0A }=0A }=0A@@ = -1151,9 +1163,13 @@ int hvm_emulate_one(=0A ? sizeof(hvmemul_ctxt->= insn_buf) : 0;=0A =0A hvmemul_ctxt->exn_pending =3D 0;=0A+ = vio->mmio_retrying =3D vio->mmio_retry;=0A+ vio->mmio_retry =3D 0;=0A = =0A rc =3D x86_emulate(&hvmemul_ctxt->ctxt, &hvm_emulate_ops);=0A =0A+ = if ( rc =3D=3D X86EMUL_OKAY && vio->mmio_retry )=0A+ rc =3D = X86EMUL_RETRY;=0A if ( rc !=3D X86EMUL_RETRY )=0A vio->mmio_lar= ge_read_bytes =3D vio->mmio_large_write_bytes =3D 0;=0A =0A--- a/xen/arch/x= 86/hvm/io.c=0A+++ b/xen/arch/x86/hvm/io.c=0A@@ -294,12 +294,21 @@ void = hvm_io_assist(void)=0A =0A static int dpci_ioport_read(uint32_t mport, = ioreq_t *p)=0A {=0A- int i, step =3D p->df ? -p->size : p->size;=0A+ = struct hvm_vcpu_io *vio =3D ¤t->arch.hvm_vcpu.hvm_io;=0A+ int rc = =3D X86EMUL_OKAY, i, step =3D p->df ? -p->size : p->size;=0A uint32_t = data =3D 0;=0A =0A for ( i =3D 0; i < p->count; i++ )=0A {=0A- = switch ( p->size )=0A+ if ( vio->mmio_retrying )=0A+ = {=0A+ if ( vio->mmio_large_read_bytes !=3D p->size )=0A+ = return X86EMUL_UNHANDLEABLE;=0A+ memcpy(&data, = vio->mmio_large_read, p->size);=0A+ vio->mmio_large_read_bytes = =3D 0;=0A+ vio->mmio_retrying =3D 0;=0A+ }=0A+ = else switch ( p->size )=0A {=0A case 1:=0A = data =3D inb(mport);=0A@@ -316,22 +325,51 @@ static int dpci_ioport_read(ui= nt32_t mpo=0A =0A if ( p->data_is_ptr )=0A {=0A- = int ret;=0A- ret =3D hvm_copy_to_guest_phys(p->data + step * = i, &data, p->size);=0A- if ( (ret =3D=3D HVMCOPY_gfn_paged_out) = ||=0A- (ret =3D=3D HVMCOPY_gfn_shared) )=0A- = return X86EMUL_RETRY;=0A+ switch ( hvm_copy_to_guest_phys(p->da= ta + step * i,=0A+ &data, = p->size) )=0A+ {=0A+ case HVMCOPY_okay:=0A+ = break;=0A+ case HVMCOPY_gfn_paged_out:=0A+ = case HVMCOPY_gfn_shared:=0A+ rc =3D X86EMUL_RETRY;=0A+ = break;=0A+ case HVMCOPY_bad_gfn_to_mfn:=0A+ = /* Drop the write as real hardware would. */=0A+ = continue;=0A+ case HVMCOPY_bad_gva_to_gfn:=0A+ = ASSERT(0);=0A+ /* fall through */=0A+ default:=0A= + rc =3D X86EMUL_UNHANDLEABLE;=0A+ break;=0A+= }=0A+ if ( rc !=3D X86EMUL_OKAY)=0A+ = break;=0A }=0A else=0A p->data =3D data;=0A = }=0A =0A- return X86EMUL_OKAY;=0A+ if ( rc =3D=3D X86EMUL_RETRY = )=0A+ {=0A+ vio->mmio_retry =3D 1;=0A+ vio->mmio_large_rea= d_bytes =3D p->size;=0A+ memcpy(vio->mmio_large_read, &data, = p->size);=0A+ }=0A+=0A+ if ( i !=3D 0 )=0A+ {=0A+ p->count = =3D i;=0A+ rc =3D X86EMUL_OKAY;=0A+ }=0A+=0A+ return rc;=0A = }=0A =0A static int dpci_ioport_write(uint32_t mport, ioreq_t *p)=0A {=0A- = int i, step =3D p->df ? -p->size : p->size;=0A+ int rc =3D X86EMUL_OK= AY, i, step =3D p->df ? -p->size : p->size;=0A uint32_t data;=0A =0A = for ( i =3D 0; i < p->count; i++ )=0A@@ -346,7 +384,8 @@ static int = dpci_ioport_write(uint32_t mp=0A break;=0A = case HVMCOPY_gfn_paged_out:=0A case HVMCOPY_gfn_shared:=0A- = return X86EMUL_RETRY;=0A+ rc =3D X86EMUL_RETRY;= =0A+ break;=0A case HVMCOPY_bad_gfn_to_mfn:=0A = data =3D ~0;=0A break;=0A@@ -354,8 +393,11 = @@ static int dpci_ioport_write(uint32_t mp=0A ASSERT(0);= =0A /* fall through */=0A default:=0A- = return X86EMUL_UNHANDLEABLE;=0A+ rc =3D X86EMUL_UNHAN= DLEABLE;=0A+ break;=0A }=0A+ if ( rc = !=3D X86EMUL_OKAY)=0A+ break;=0A }=0A =0A = switch ( p->size )=0A@@ -374,7 +416,16 @@ static int dpci_ioport_write(uint= 32_t mp=0A }=0A }=0A =0A- return X86EMUL_OKAY;=0A+ if ( = rc =3D=3D X86EMUL_RETRY )=0A+ current->arch.hvm_vcpu.hvm_io.mmio_ret= ry =3D 1;=0A+=0A+ if ( i !=3D 0 )=0A+ {=0A+ p->count =3D = i;=0A+ rc =3D X86EMUL_OKAY;=0A+ }=0A+=0A+ return rc;=0A }=0A = =0A int dpci_ioport_intercept(ioreq_t *p)=0A--- a/xen/include/asm-x86/hvm/v= cpu.h=0A+++ b/xen/include/asm-x86/hvm/vcpu.h=0A@@ -66,6 +66,11 @@ struct = hvm_vcpu_io {=0A /* We may write up to m256 as a number of device-model= transactions. */=0A unsigned int mmio_large_write_bytes;=0A = paddr_t mmio_large_write_pa;=0A+ /*=0A+ * For string instruction = emulation we need to be able to signal a=0A+ * necessary retry through = other than function return codes.=0A+ */=0A+ bool_t mmio_retry, = mmio_retrying;=0A };=0A =0A #define VMCX_EADDR (~0ULL)=0A --=__Part46755040.0__= 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 --=__Part46755040.0__=--