All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Dmitry Isaykin <isaikin-dmitry@yandex.ru>,
	xen-devel@lists.xenproject.org
Cc: "Wei Liu" <wl@xen.org>,
	"Anthony PERARD" <anthony.perard@citrix.com>,
	"Juergen Gross" <jgross@suse.com>,
	"George Dunlap" <george.dunlap@citrix.com>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Julien Grall" <julien@xen.org>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Tamas K Lengyel" <tamas@tklengyel.com>,
	"Alexandru Isaila" <aisaila@bitdefender.com>,
	"Petre Pircalabu" <ppircalabu@bitdefender.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>,
	"Jun Nakajima" <jun.nakajima@intel.com>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Anton Belousov" <abelousov@ptsecurity.com>
Subject: Re: [XEN PATCH v2] x86/monitor: Add new monitor event to catch I/O instructions
Date: Fri, 17 Mar 2023 11:13:31 +0000	[thread overview]
Message-ID: <63ed49be-b7d3-3b7e-a406-f760252d55ed@citrix.com> (raw)
In-Reply-To: <aab9c8ae059d5f584516d0b6466e57ce0981dadc.1678904818.git.isaikin-dmitry@yandex.ru>

On 15/03/2023 6:54 pm, Dmitry Isaykin wrote:
> diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
> index a43bcf2e92..49225f48a7 100644
> --- a/xen/arch/x86/hvm/svm/svm.c
> +++ b/xen/arch/x86/hvm/svm/svm.c
> @@ -2939,17 +2939,26 @@ void svm_vmexit_handler(void)
>          break;
>  
>      case VMEXIT_IOIO:
> -        if ( (vmcb->exitinfo1 & (1u<<2)) == 0 )
> +     {
> +        uint16_t port = (vmcb->exitinfo1 >> 16) & 0xFFFF;
> +        int bytes = ((vmcb->exitinfo1 >> 4) & 0x07);
> +        int dir = (vmcb->exitinfo1 & 1) ? IOREQ_READ : IOREQ_WRITE;
> +        bool string_ins = (vmcb->exitinfo1 & (1u<<2));
> +        int rc = hvm_monitor_io(port, bytes, dir, string_ins);
> +        if ( rc < 0 )
> +            goto unexpected_exit_type;
> +        if ( !rc )
>          {
> -            uint16_t port = (vmcb->exitinfo1 >> 16) & 0xFFFF;
> -            int bytes = ((vmcb->exitinfo1 >> 4) & 0x07);
> -            int dir = (vmcb->exitinfo1 & 1) ? IOREQ_READ : IOREQ_WRITE;
> -            if ( handle_pio(port, bytes, dir) )
> -                __update_guest_eip(regs, vmcb->exitinfo2 - vmcb->rip);
> +            if ( !string_ins )
> +            {
> +                if ( handle_pio(port, bytes, dir) )
> +                    __update_guest_eip(regs, vmcb->exitinfo2 - vmcb->rip);
> +            }
> +            else if ( !hvm_emulate_one_insn(x86_insn_is_portio, "port I/O") )
> +                hvm_inject_hw_exception(TRAP_gp_fault, 0);
>          }
> -        else if ( !hvm_emulate_one_insn(x86_insn_is_portio, "port I/O") )
> -            hvm_inject_hw_exception(TRAP_gp_fault, 0);
>          break;
> +    }

There are a few style issues, but it's also a mess because of the manual
exitinfo decoding, so I went ahead and did

https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=df9369154aa010b2322e3f3e0727a242784cfd4f

to clean it up.  The rebased version of this hunk is now:

diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
index bfe03316def6..17ac99f6cd56 100644
--- a/xen/arch/x86/hvm/svm/svm.c
+++ b/xen/arch/x86/hvm/svm/svm.c
@@ -2939,6 +2939,15 @@ void svm_vmexit_handler(void)
         break;
 
     case VMEXIT_IOIO:
+        rc = hvm_monitor_io(vmcb->ei.io.port,
+                            vmcb->ei.io.bytes,
+                            vmcb->ei.io.in ? IOREQ_READ : IOREQ_WRITE,
+                            vmcb->ei.io.str);
+        if ( rc < 0 )
+            goto unexpected_exit_type;
+        if ( rc )
+            break;
+
         if ( !vmcb->ei.io.str )
         {
             if ( handle_pio(vmcb->ei.io.port,

which I hope you'll agree is much more simple to follow.

I'm also trying to sort out a similar cleanup on the Intel side, but
haven't managed to post that yet.

~Andrew


      parent reply	other threads:[~2023-03-17 11:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-15 18:54 [XEN PATCH v2] x86/monitor: Add new monitor event to catch I/O instructions Dmitry Isaykin
2023-03-16 12:32 ` Tamas K Lengyel
2023-03-17 11:13 ` Andrew Cooper [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=63ed49be-b7d3-3b7e-a406-f760252d55ed@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=abelousov@ptsecurity.com \
    --cc=aisaila@bitdefender.com \
    --cc=anthony.perard@citrix.com \
    --cc=george.dunlap@citrix.com \
    --cc=isaikin-dmitry@yandex.ru \
    --cc=jbeulich@suse.com \
    --cc=jgross@suse.com \
    --cc=julien@xen.org \
    --cc=jun.nakajima@intel.com \
    --cc=kevin.tian@intel.com \
    --cc=ppircalabu@bitdefender.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=tamas@tklengyel.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.