From nobody Mon May 6 02:32:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=yandex.ru ARC-Seal: i=1; a=rsa-sha256; t=1679349417; cv=none; d=zohomail.com; s=zohoarc; b=Yuvi4664NDiNPXrwQ6t/blf9o1buKiOVjVxzk/i1RACg+dxpoKo/1T/nXrlsp4xG9gkXEd2egR/S8Xer/2+f/k5e2UQ58awssZqtz76JYeTk+olDi8WnFGUfau73opBUbMU3jPDD53vMnRSadPFVGREfHbDoTO707e1reDPv1Kw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1679349417; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=cPHulWu7xT33kLucFe1NjIiDqlIPl0UWOtkZ4jCoB7k=; b=Z9xS1/SFzPsPCEuF1ZvUtVI0bo/c+ExvueDvWTeyXkjciFrFy9V/yCzAUUwTW34M7LkyaPuGZ5mN6Dw/16qEMnkoQPAd9xBnGfi0BurLzf5c2OEdVbJ01Kdza8UgIBVG1ZWl95TPiwnVRDGgatZAZU5VqCf9eK395rsGSEqXGVc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 167934941753314.161573628155224; Mon, 20 Mar 2023 14:56:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.512145.791878 (Exim 4.92) (envelope-from ) id 1peNUP-0002Z7-3c; Mon, 20 Mar 2023 21:56:25 +0000 Received: by outflank-mailman (output) from mailman id 512145.791878; Mon, 20 Mar 2023 21:56:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1peNUP-0002Z0-0p; Mon, 20 Mar 2023 21:56:25 +0000 Received: by outflank-mailman (input) for mailman id 512145; Mon, 20 Mar 2023 21:56:23 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1peNUN-0002Yu-A7 for xen-devel@lists.xenproject.org; Mon, 20 Mar 2023 21:56:23 +0000 Received: from forward200b.mail.yandex.net (forward200b.mail.yandex.net [178.154.239.157]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 0c3db831-c76a-11ed-87f5-c1b5be75604c; Mon, 20 Mar 2023 22:56:21 +0100 (CET) Received: from mail-nwsmtp-smtp-production-main-45.sas.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-45.sas.yp-c.yandex.net [IPv6:2a02:6b8:c08:6413:0:640:1bc4:0]) by forward200b.mail.yandex.net (Yandex) with ESMTP id 8A920600C0; Tue, 21 Mar 2023 00:56:18 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-45.sas.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id EuwiEDUWkuQ0-aa6KDLgg; Tue, 21 Mar 2023 00:56:17 +0300 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 0c3db831-c76a-11ed-87f5-c1b5be75604c X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1679349377; bh=cPHulWu7xT33kLucFe1NjIiDqlIPl0UWOtkZ4jCoB7k=; h=Message-Id:Date:Cc:Subject:To:From; b=RtNcE1cm4wLdFk3oVuWqQ2Kc1+CjjCamkZ5Aa9+dCth+XKY1D63opTOWe80fNtMrg Qex1LVdn3b4rAyRkE0fXdJVaWr5JZoKwR1lEDmtxQZbm8tEcaWdB59cTVIo/YC8QQo j3Dso3h4GA3x/RLgpbXn/jk8I9jccWoCWlXYRGRw= Authentication-Results: mail-nwsmtp-smtp-production-main-45.sas.yp-c.yandex.net; dkim=pass header.i=@yandex.ru From: Dmitry Isaykin To: xen-devel@lists.xenproject.org Cc: Dmitry Isaykin , Wei Liu , Anthony PERARD , Juergen Gross , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Tamas K Lengyel , Alexandru Isaila , Petre Pircalabu , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jun Nakajima , Kevin Tian , Anton Belousov Subject: [XEN PATCH v4] x86/monitor: Add new monitor event to catch I/O instructions Date: Tue, 21 Mar 2023 00:56:12 +0300 Message-Id: <1d3e2081fba9a1ce07c3e2a28eddcd6f51d52854.1679348946.git.isaikin-dmitry@yandex.ru> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @yandex.ru) X-ZM-MESSAGEID: 1679349418616100005 Content-Type: text/plain; charset="utf-8" Adds monitor support for I/O instructions. Signed-off-by: Dmitry Isaykin Signed-off-by: Anton Belousov --- Changes in v4: * Avoid the use of fixed-width types * Document vm_event_io structure fields * Untie vm-event interface from ioreq one Changes in v3: * Rebase on staging * Refactor branch logic on monitor_traps response Changes in v2: * Handled INS and OUTS instructions too * Added I/O monitoring support for AMD * Rename functions and structures (remove "_instruction" part) * Reorder parameters of hvm_monitor_io to match handle_pio's order * Change type of string_ins parameter to bool * Change vm_event_io structure * Handle monitor_traps's return status --- tools/include/xenctrl.h | 1 + tools/libs/ctrl/xc_monitor.c | 13 +++++++++++++ xen/arch/x86/hvm/monitor.c | 21 +++++++++++++++++++++ xen/arch/x86/hvm/svm/svm.c | 9 +++++++++ xen/arch/x86/hvm/vmx/vmx.c | 24 +++++++++++++++++++----- xen/arch/x86/include/asm/domain.h | 1 + xen/arch/x86/include/asm/hvm/monitor.h | 3 +++ xen/arch/x86/include/asm/monitor.h | 3 ++- xen/arch/x86/monitor.c | 13 +++++++++++++ xen/include/public/domctl.h | 1 + xen/include/public/vm_event.h | 10 ++++++++++ 11 files changed, 93 insertions(+), 6 deletions(-) diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h index 23037874d3..05967ecc92 100644 --- a/tools/include/xenctrl.h +++ b/tools/include/xenctrl.h @@ -2102,6 +2102,7 @@ int xc_monitor_emul_unimplemented(xc_interface *xch, = uint32_t domain_id, bool enable); int xc_monitor_vmexit(xc_interface *xch, uint32_t domain_id, bool enable, bool sync); +int xc_monitor_io(xc_interface *xch, uint32_t domain_id, bool enable); /** * This function enables / disables emulation for each REP for a * REP-compatible instruction. diff --git a/tools/libs/ctrl/xc_monitor.c b/tools/libs/ctrl/xc_monitor.c index c5fa62ff30..3cb96f444f 100644 --- a/tools/libs/ctrl/xc_monitor.c +++ b/tools/libs/ctrl/xc_monitor.c @@ -261,6 +261,19 @@ int xc_monitor_vmexit(xc_interface *xch, uint32_t doma= in_id, bool enable, return do_domctl(xch, &domctl); } =20 +int xc_monitor_io(xc_interface *xch, uint32_t domain_id, bool enable) +{ + DECLARE_DOMCTL; + + domctl.cmd =3D XEN_DOMCTL_monitor_op; + domctl.domain =3D domain_id; + domctl.u.monitor_op.op =3D enable ? XEN_DOMCTL_MONITOR_OP_ENABLE + : XEN_DOMCTL_MONITOR_OP_DISABLE; + domctl.u.monitor_op.event =3D XEN_DOMCTL_MONITOR_EVENT_IO; + + return do_domctl(xch, &domctl); +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/hvm/monitor.c b/xen/arch/x86/hvm/monitor.c index a11cd76f4d..4f500beaf5 100644 --- a/xen/arch/x86/hvm/monitor.c +++ b/xen/arch/x86/hvm/monitor.c @@ -346,6 +346,27 @@ int hvm_monitor_vmexit(unsigned long exit_reason, return monitor_traps(curr, ad->monitor.vmexit_sync, &req); } =20 +int hvm_monitor_io(unsigned int port, unsigned int bytes, + bool in, bool str) +{ + struct vcpu *curr =3D current; + struct arch_domain *ad =3D &curr->domain->arch; + vm_event_request_t req =3D { + .reason =3D VM_EVENT_REASON_IO_INSTRUCTION, + .u.io.bytes =3D bytes, + .u.io.port =3D port, + .u.io.in =3D in, + .u.io.str =3D str, + }; + + if ( !ad->monitor.io_enabled ) + return 0; + + set_npt_base(curr, &req); + + return monitor_traps(curr, true, &req); +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index bfe03316de..02563e4b70 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; =20 case VMEXIT_IOIO: + rc =3D hvm_monitor_io(vmcb->ei.io.port, + vmcb->ei.io.bytes, + vmcb->ei.io.in, + 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, diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 00b531f76c..0b7a302928 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -4560,8 +4560,24 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) break; =20 case EXIT_REASON_IO_INSTRUCTION: + { + unsigned int port, bytes; + bool in, str; + int rc; + __vmread(EXIT_QUALIFICATION, &exit_qualification); - if ( exit_qualification & 0x10 ) + + port =3D (exit_qualification >> 16) & 0xFFFF; + bytes =3D (exit_qualification & 0x07) + 1; + in =3D (exit_qualification & 0x08); + str =3D (exit_qualification & 0x10); + rc =3D hvm_monitor_io(port, bytes, in, str); + if ( rc < 0 ) + goto exit_and_crash; + if ( rc ) + break; + + if ( str ) { /* INS, OUTS */ if ( !hvm_emulate_one_insn(x86_insn_is_portio, "port I/O") ) @@ -4570,13 +4586,11 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) else { /* IN, OUT */ - uint16_t port =3D (exit_qualification >> 16) & 0xFFFF; - int bytes =3D (exit_qualification & 0x07) + 1; - int dir =3D (exit_qualification & 0x08) ? IOREQ_READ : IOREQ_W= RITE; - if ( handle_pio(port, bytes, dir) ) + if ( handle_pio(port, bytes, in ? IOREQ_READ : IOREQ_WRITE) ) update_guest_eip(); /* Safe: IN, OUT */ } break; + } =20 case EXIT_REASON_INVD: case EXIT_REASON_WBINVD: diff --git a/xen/arch/x86/include/asm/domain.h b/xen/arch/x86/include/asm/d= omain.h index 7bc126587d..29027ffd29 100644 --- a/xen/arch/x86/include/asm/domain.h +++ b/xen/arch/x86/include/asm/domain.h @@ -435,6 +435,7 @@ struct arch_domain unsigned int descriptor_access_enabled = : 1; unsigned int guest_request_userspace_enabled = : 1; unsigned int emul_unimplemented_enabled = : 1; + unsigned int io_enabled = : 1; /* * By default all events are sent. * This is used to filter out pagefaults. diff --git a/xen/arch/x86/include/asm/hvm/monitor.h b/xen/arch/x86/include/= asm/hvm/monitor.h index 639f6dfa37..6884f38d73 100644 --- a/xen/arch/x86/include/asm/hvm/monitor.h +++ b/xen/arch/x86/include/asm/hvm/monitor.h @@ -54,6 +54,9 @@ bool hvm_monitor_check_p2m(unsigned long gla, gfn_t gfn, = uint32_t pfec, int hvm_monitor_vmexit(unsigned long exit_reason, unsigned long exit_qualification); =20 +int hvm_monitor_io(unsigned int port, unsigned int bytes, + bool in, bool str); + #endif /* __ASM_X86_HVM_MONITOR_H__ */ =20 /* diff --git a/xen/arch/x86/include/asm/monitor.h b/xen/arch/x86/include/asm/= monitor.h index d8d54c5f23..96e6a9d0d8 100644 --- a/xen/arch/x86/include/asm/monitor.h +++ b/xen/arch/x86/include/asm/monitor.h @@ -90,7 +90,8 @@ static inline uint32_t arch_monitor_get_capabilities(stru= ct domain *d) (1U << XEN_DOMCTL_MONITOR_EVENT_WRITE_CTRLREG) | (1U << XEN_DOMCTL_MONITOR_EVENT_EMUL_UNIMPLEMENTED) | (1U << XEN_DOMCTL_MONITOR_EVENT_INGUEST_PAGEFAULT) | - (1U << XEN_DOMCTL_MONITOR_EVENT_VMEXIT)); + (1U << XEN_DOMCTL_MONITOR_EVENT_VMEXIT) | + (1U << XEN_DOMCTL_MONITOR_EVENT_IO)); =20 if ( hvm_is_singlestep_supported() ) capabilities |=3D (1U << XEN_DOMCTL_MONITOR_EVENT_SINGLESTEP); diff --git a/xen/arch/x86/monitor.c b/xen/arch/x86/monitor.c index 30ca71432c..d4857faf8a 100644 --- a/xen/arch/x86/monitor.c +++ b/xen/arch/x86/monitor.c @@ -346,6 +346,19 @@ int arch_monitor_domctl_event(struct domain *d, break; } =20 + case XEN_DOMCTL_MONITOR_EVENT_IO: + { + bool old_status =3D ad->monitor.io_enabled; + + if ( unlikely(old_status =3D=3D requested_status) ) + return -EEXIST; + + domain_pause(d); + ad->monitor.io_enabled =3D requested_status; + domain_unpause(d); + break; + } + default: /* * Should not be reached unless arch_monitor_get_capabilities() is diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 51be28c3de..7280e9f968 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -1063,6 +1063,7 @@ struct xen_domctl_psr_cmt_op { /* Enabled by default */ #define XEN_DOMCTL_MONITOR_EVENT_INGUEST_PAGEFAULT 11 #define XEN_DOMCTL_MONITOR_EVENT_VMEXIT 12 +#define XEN_DOMCTL_MONITOR_EVENT_IO 13 =20 struct xen_domctl_monitor_op { uint32_t op; /* XEN_DOMCTL_MONITOR_OP_* */ diff --git a/xen/include/public/vm_event.h b/xen/include/public/vm_event.h index 0035c26e12..3a86f0e208 100644 --- a/xen/include/public/vm_event.h +++ b/xen/include/public/vm_event.h @@ -160,6 +160,8 @@ #define VM_EVENT_REASON_EMUL_UNIMPLEMENTED 14 /* VMEXIT */ #define VM_EVENT_REASON_VMEXIT 15 +/* IN/OUT Instruction executed */ +#define VM_EVENT_REASON_IO_INSTRUCTION 16 =20 /* Supported values for the vm_event_write_ctrlreg index. */ #define VM_EVENT_X86_CR0 0 @@ -388,6 +390,13 @@ struct vm_event_vmexit { } arch; }; =20 +struct vm_event_io { + uint32_t bytes; /* size of access */ + uint16_t port; /* port number */ + uint8_t in; /* direction (0 =3D OUT, 1 =3D IN) */ + uint8_t str; /* string instruction (0 =3D not string, 1 =3D string)= */ +}; + typedef struct vm_event_st { uint32_t version; /* VM_EVENT_INTERFACE_VERSION */ uint32_t flags; /* VM_EVENT_FLAG_* */ @@ -409,6 +418,7 @@ typedef struct vm_event_st { struct vm_event_debug debug_exception; struct vm_event_cpuid cpuid; struct vm_event_vmexit vmexit; + struct vm_event_io io; union { struct vm_event_interrupt_x86 x86; } interrupt; --=20 2.39.2