From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222090; cv=none; d=zohomail.com; s=zohoarc; b=I4TCA2lIRoMR8KewHduxUAntZ0WWD3tSMSplVT95pnl/83o9ipjPjEvIPOissRkvl8Ut6YELBXZm2DheLl4AYFzWgnPDiLccgvVPHsC6ndJIAYrzAbqkvSA6oxEkTGNDC5NtbLKCLfqfxpt1gUMMJplu2KgE391qbXrKWMcesIQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222090; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6XHlV8fRq0ZhgLVtu4Ru3v9j/liBZs/SeVcg/XmBqrs=; b=f4MzruLp7MKTeNdBqdkw4qqJYxQ1EFP3F+qHuf9p+tjjtRsBQSIEHKVK7sSTs3XFHY7g8YAiDfxyM3Hl2+adAwBh274Id69bLtLhXyBfX9h1Syvdn2vudaFMEE8TBJcR2KNjmEZeovEzYE+FNHKa+iI45k3hks6VLRe40HWb3Ws= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222090750925.20591083377; Mon, 1 Feb 2021 15:28:10 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80196.146578 (Exim 4.92) (envelope-from ) id 1l6ibq-0007tk-98; Mon, 01 Feb 2021 23:27:54 +0000 Received: by outflank-mailman (output) from mailman id 80196.146578; Mon, 01 Feb 2021 23:27:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibq-0007tY-4m; Mon, 01 Feb 2021 23:27:54 +0000 Received: by outflank-mailman (input) for mailman id 80196; Mon, 01 Feb 2021 23:27:52 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibo-0007P6-RR for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:52 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 4974a30f-43f4-47f0-bfa7-ad5966e08e58; Mon, 01 Feb 2021 23:27:35 +0000 (UTC) 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: 4974a30f-43f4-47f0-bfa7-ad5966e08e58 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222055; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ooOeFTFX01JiGDLs2zOj6QNxJzo/EvnmXjy1QpJOiP4=; b=a+NT2DtUex+pSs0j0IktlIAZRKnsKiiEVDrDBiSiBjUQRJkMY1VS7Toz upWRHN2MSqbcsR8p6Z3l67d05V4qo/zQ6wCb2T5YAc7NuxR88ebW0UQQR MwRBLXl/njKInQefWZca0Xf7VE2DKx7/Ci6yDNqwejWJ6znqEGi36vImI s=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: GkpqpvXlBS0xQRr0Iymb1U4xQHvVP8qukJrzdmUmf5j2EtFLx/ZR8st79ap0XebBopMenwStB5 YK4Nc2TFUMM7caQwQ9KmRudisXuuPmgAMGDyWHmyj4kR0TI2fncs39KX6OYO15Myz6g4Uukf5D j+IW0IFwoqJ1NWMxWZqsHsCLZJueQJr3k4gV+0BIfQCpahOKXaVIujqLCKUAeSe9xR9eL4g8kO vNQXK/w3Hil2eZd/f0n2wixXZ9LeuawymIziKwkEdS1rUXuUemWdeYzeJvtVaiVWhIP0bH/HY1 Leo= X-SBRS: 5.1 X-MesageID: 36319806 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36319806" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , George Dunlap , Ian Jackson , Jan Beulich , Stefano Stabellini , Wei Liu , Julien Grall , Paul Durrant , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Hubert Jasudowicz , Tamas K Lengyel Subject: [PATCH v9 01/11] xen/memory: Fix mapping grant tables with XENMEM_acquire_resource Date: Mon, 1 Feb 2021 23:26:53 +0000 Message-ID: <20210201232703.29275-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) A guest's default number of grant frames is 64, and XENMEM_acquire_resource will reject an attempt to map more than 32 frames. This limit is caused by the size of mfn_list[] on the stack. Fix mapping of arbitrary size requests by looping over batches of 32 in acquire_resource(), and using hypercall continuations when necessary. To start with, break _acquire_resource() out of acquire_resource() to cope with type-specific dispatching, and update the return semantics to indicate the number of mfns returned. Update gnttab_acquire_resource() and x86's arch_acquire_resource() to match these new semantics. Have do_memory_op() pass start_extent into acquire_resource() so it can pick up where it left off after a continuation, and loop over batches of 32 until all the work is done, or a continuation needs to occur. compat_memory_op() is a bit more complicated, because it also has to marshal frame_list in the XLAT buffer. Have it account for continuation information itself and hide details from the upper layer, so it can marshal the buffer = in chunks if necessary. With these fixes in place, it is now possible to map the whole grant table = for a guest. Signed-off-by: Andrew Cooper Reviewed-by: Roger Pau Monn=C3=A9 --- CC: George Dunlap CC: Ian Jackson CC: Jan Beulich CC: Stefano Stabellini CC: Wei Liu CC: Julien Grall CC: Paul Durrant CC: Micha=C5=82 Leszczy=C5=84ski CC: Hubert Jasudowicz CC: Tamas K Lengyel v9: * Crash domain rather than returning late with -ERANGE/-EFAULT. v8: * nat =3D> cmp change in the start_extent check. * Rebase over 'frame' and ARM/IOREQ series. v3: * Spelling fixes --- xen/common/compat/memory.c | 114 +++++++++++++++++++++++++++++++++-------- xen/common/grant_table.c | 3 ++ xen/common/memory.c | 124 +++++++++++++++++++++++++++++++++--------= ---- 3 files changed, 187 insertions(+), 54 deletions(-) diff --git a/xen/common/compat/memory.c b/xen/common/compat/memory.c index 834c5e19d1..c43fa97cf1 100644 --- a/xen/common/compat/memory.c +++ b/xen/common/compat/memory.c @@ -402,23 +402,10 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HAND= LE_PARAM(void) compat) case XENMEM_acquire_resource: { xen_pfn_t *xen_frame_list =3D NULL; - unsigned int max_nr_frames; =20 if ( copy_from_guest(&cmp.mar, compat, 1) ) return -EFAULT; =20 - /* - * The number of frames handled is currently limited to a - * small number by the underlying implementation, so the - * scratch space should be sufficient for bouncing the - * frame addresses. - */ - max_nr_frames =3D (COMPAT_ARG_XLAT_SIZE - sizeof(*nat.mar)) / - sizeof(*xen_frame_list); - - if ( cmp.mar.nr_frames > max_nr_frames ) - return -E2BIG; - /* Marshal the frame list in the remainder of the xlat space. = */ if ( !compat_handle_is_null(cmp.mar.frame_list) ) xen_frame_list =3D (xen_pfn_t *)(nat.mar + 1); @@ -432,6 +419,28 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HANDL= E_PARAM(void) compat) =20 if ( xen_frame_list && cmp.mar.nr_frames ) { + unsigned int xlat_max_frames =3D + (COMPAT_ARG_XLAT_SIZE - sizeof(*nat.mar)) / + sizeof(*xen_frame_list); + + if ( start_extent >=3D cmp.mar.nr_frames ) + return -EINVAL; + + /* + * Adjust nat to account for work done on previous + * continuations, leaving cmp pristine. Hide the continau= tion + * from the native code to prevent double accounting. + */ + nat.mar->nr_frames -=3D start_extent; + nat.mar->frame +=3D start_extent; + cmd &=3D MEMOP_CMD_MASK; + + /* + * If there are two many frames to fit within the xlat buf= fer, + * we'll need to loop to marshal them all. + */ + nat.mar->nr_frames =3D min(nat.mar->nr_frames, xlat_max_fr= ames); + /* * frame_list is an input for translated guests, and an ou= tput * for untranslated guests. Only copy in for translated g= uests. @@ -444,14 +453,14 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HAND= LE_PARAM(void) compat) cmp.mar.nr_frames) || __copy_from_compat_offset( compat_frame_list, cmp.mar.frame_list, - 0, cmp.mar.nr_frames) ) + start_extent, nat.mar->nr_frames) ) return -EFAULT; =20 /* * Iterate backwards over compat_frame_list[] expanding * compat_pfn_t to xen_pfn_t in place. */ - for ( int x =3D cmp.mar.nr_frames - 1; x >=3D 0; --x ) + for ( int x =3D nat.mar->nr_frames - 1; x >=3D 0; --x ) xen_frame_list[x] =3D compat_frame_list[x]; } } @@ -600,9 +609,11 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HANDL= E_PARAM(void) compat) case XENMEM_acquire_resource: { DEFINE_XEN_GUEST_HANDLE(compat_mem_acquire_resource_t); + unsigned int done; =20 if ( compat_handle_is_null(cmp.mar.frame_list) ) { + ASSERT(split =3D=3D 0 && rc =3D=3D 0); if ( __copy_field_to_guest( guest_handle_cast(compat, compat_mem_acquire_resource_t), @@ -611,6 +622,21 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HANDL= E_PARAM(void) compat) break; } =20 + if ( split < 0 ) + { + /* Continuation occurred. */ + ASSERT(rc !=3D XENMEM_acquire_resource); + done =3D cmd >> MEMOP_EXTENT_SHIFT; + } + else + { + /* No continuation. */ + ASSERT(rc =3D=3D 0); + done =3D nat.mar->nr_frames; + } + + ASSERT(done <=3D nat.mar->nr_frames); + /* * frame_list is an input for translated guests, and an output= for * untranslated guests. Only copy out for untranslated guests. @@ -626,21 +652,67 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HAND= LE_PARAM(void) compat) */ BUILD_BUG_ON(sizeof(compat_pfn_t) > sizeof(xen_pfn_t)); =20 - for ( i =3D 0; i < cmp.mar.nr_frames; i++ ) + rc =3D 0; + for ( i =3D 0; i < done; i++ ) { compat_pfn_t frame =3D xen_frame_list[i]; =20 if ( frame !=3D xen_frame_list[i] ) - return -ERANGE; + { + rc =3D -ERANGE; + break; + } =20 compat_frame_list[i] =3D frame; } =20 - if ( __copy_to_compat_offset(cmp.mar.frame_list, 0, - compat_frame_list, - cmp.mar.nr_frames) ) - return -EFAULT; + if ( !rc && __copy_to_compat_offset( + cmp.mar.frame_list, start_extent, + compat_frame_list, done) ) + rc =3D -EFAULT; + + if ( rc ) + { + if ( split < 0 ) + { + gdprintk(XENLOG_ERR, + "Cannot cancel continuation: %ld\n", rc); + domain_crash(current->domain); + } + return rc; + } + } + + start_extent +=3D done; + + /* Completely done. */ + if ( start_extent =3D=3D cmp.mar.nr_frames ) + break; + + /* + * Done a "full" batch, but we were limited by space in the xl= at + * area. Go around the loop again without necesserily returni= ng + * to guest context. + */ + if ( done =3D=3D nat.mar->nr_frames ) + { + split =3D 1; + break; } + + /* Explicit continuation request from a higher level. */ + if ( done < nat.mar->nr_frames ) + return hypercall_create_continuation( + __HYPERVISOR_memory_op, "ih", + op | (start_extent << MEMOP_EXTENT_SHIFT), compat); + + /* + * Well... Somethings gone wrong with the two levels of chunki= ng. + * My condolences to whomever next has to debug this mess. + */ + ASSERT_UNREACHABLE(); + domain_crash(current->domain); + split =3D 0; break; } =20 diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c index 280b7969b6..b95403695f 100644 --- a/xen/common/grant_table.c +++ b/xen/common/grant_table.c @@ -4053,6 +4053,9 @@ int gnttab_acquire_resource( for ( i =3D 0; i < nr_frames; ++i ) mfn_list[i] =3D virt_to_mfn(vaddrs[frame + i]); =20 + /* Success. Passed nr_frames back to the caller. */ + rc =3D nr_frames; + out: grant_write_unlock(gt); =20 diff --git a/xen/common/memory.c b/xen/common/memory.c index 01cab7e493..128718b31c 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1118,23 +1118,41 @@ static int acquire_ioreq_server(struct domain *d, mfn_list[i] =3D mfn_x(mfn); } =20 - return 0; + /* Success. Passed nr_frames back to the caller. */ + return nr_frames; #else return -EOPNOTSUPP; #endif } =20 +/* + * Returns -errno on error, or positive in the range [1, nr_frames] on + * success. Returning less than nr_frames contitutes a request for a + * continuation. Callers can depend on frame + nr_frames not overflowing. + */ +static int _acquire_resource( + struct domain *d, unsigned int type, unsigned int id, unsigned int fra= me, + unsigned int nr_frames, xen_pfn_t mfn_list[]) +{ + switch ( type ) + { + case XENMEM_resource_grant_table: + return gnttab_acquire_resource(d, id, frame, nr_frames, mfn_list); + + case XENMEM_resource_ioreq_server: + return acquire_ioreq_server(d, id, frame, nr_frames, mfn_list); + + default: + return -EOPNOTSUPP; + } +} + static int acquire_resource( - XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg) + XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg, + unsigned long start_extent) { struct domain *d, *currd =3D current->domain; xen_mem_acquire_resource_t xmar; - /* - * The mfn_list and gfn_list (below) arrays are ok on stack for the - * moment since they are small, but if they need to grow in future - * use-cases then per-CPU arrays or heap allocations may be required. - */ - xen_pfn_t mfn_list[32]; unsigned int max_frames; int rc; =20 @@ -1147,9 +1165,6 @@ static int acquire_resource( if ( xmar.pad !=3D 0 ) return -EINVAL; =20 - if ( xmar.nr_frames > ARRAY_SIZE(mfn_list) ) - return -E2BIG; - /* * The ABI is rather unfortunate. nr_frames (and therefore the total = size * of the resource) is 32bit, while frame (the offset within the resou= rce @@ -1179,7 +1194,7 @@ static int acquire_resource( =20 if ( guest_handle_is_null(xmar.frame_list) ) { - if ( xmar.nr_frames ) + if ( xmar.nr_frames || start_extent ) goto out; =20 xmar.nr_frames =3D max_frames; @@ -1187,30 +1202,47 @@ static int acquire_resource( goto out; } =20 - do { - switch ( xmar.type ) - { - case XENMEM_resource_grant_table: - rc =3D gnttab_acquire_resource(d, xmar.id, xmar.frame, xmar.nr= _frames, - mfn_list); - break; + /* + * Limiting nr_frames at (UINT_MAX >> MEMOP_EXTENT_SHIFT) isn't ideal.= If + * it ever becomes a practical problem, we can switch to mutating + * xmar.{frame,nr_frames,frame_list} in guest memory. + */ + rc =3D -EINVAL; + if ( start_extent >=3D xmar.nr_frames || + xmar.nr_frames > (UINT_MAX >> MEMOP_EXTENT_SHIFT) ) + goto out; =20 - case XENMEM_resource_ioreq_server: - rc =3D acquire_ioreq_server(d, xmar.id, xmar.frame, xmar.nr_fr= ames, - mfn_list); - break; + /* Adjust for work done on previous continuations. */ + xmar.nr_frames -=3D start_extent; + xmar.frame +=3D start_extent; + guest_handle_add_offset(xmar.frame_list, start_extent); =20 - default: - rc =3D -EOPNOTSUPP; - break; - } + do { + /* + * Arbitrary size. Not too much stack space, and a reasonable str= ide + * for continuation checks. + */ + xen_pfn_t mfn_list[32]; + unsigned int todo =3D MIN(ARRAY_SIZE(mfn_list), xmar.nr_frames), d= one; =20 - if ( rc ) + rc =3D _acquire_resource(d, xmar.type, xmar.id, xmar.frame, + todo, mfn_list); + if ( rc < 0 ) + goto out; + + done =3D rc; + rc =3D 0; + if ( done =3D=3D 0 || done > todo ) + { + ASSERT_UNREACHABLE(); + rc =3D -EINVAL; goto out; + } =20 + /* Adjust guest frame_list appropriately. */ if ( !paging_mode_translate(currd) ) { - if ( copy_to_guest(xmar.frame_list, mfn_list, xmar.nr_frames) ) + if ( copy_to_guest(xmar.frame_list, mfn_list, done) ) rc =3D -EFAULT; } else @@ -1218,10 +1250,10 @@ static int acquire_resource( xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)]; unsigned int i; =20 - if ( copy_from_guest(gfn_list, xmar.frame_list, xmar.nr_frames= ) ) + if ( copy_from_guest(gfn_list, xmar.frame_list, done) ) rc =3D -EFAULT; =20 - for ( i =3D 0; !rc && i < xmar.nr_frames; i++ ) + for ( i =3D 0; !rc && i < done; i++ ) { rc =3D set_foreign_p2m_entry(currd, d, gfn_list[i], _mfn(mfn_list[i])); @@ -1230,7 +1262,32 @@ static int acquire_resource( rc =3D -EIO; } } - } while ( 0 ); + + if ( rc ) + goto out; + + xmar.nr_frames -=3D done; + xmar.frame +=3D done; + guest_handle_add_offset(xmar.frame_list, done); + start_extent +=3D done; + + /* + * Explicit continuation request from _acquire_resource(), or we've + * still got more work to do. + */ + if ( done < todo || + (xmar.nr_frames && hypercall_preempt_check()) ) + { + rc =3D hypercall_create_continuation( + __HYPERVISOR_memory_op, "lh", + XENMEM_acquire_resource | (start_extent << MEMOP_EXTENT_SH= IFT), + arg); + goto out; + } + + } while ( xmar.nr_frames ); + + rc =3D 0; =20 out: rcu_unlock_domain(d); @@ -1697,7 +1754,8 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE= _PARAM(void) arg) =20 case XENMEM_acquire_resource: rc =3D acquire_resource( - guest_handle_cast(arg, xen_mem_acquire_resource_t)); + guest_handle_cast(arg, xen_mem_acquire_resource_t), + start_extent); break; =20 default: --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222074; cv=none; d=zohomail.com; s=zohoarc; b=P6i9y0wVZ8NnZTVyl74r/0WjsSf7xepPTB+eLqHac6NnWbY2qSFU+NGu7jDc3sjYqdm/EamupBysiGIqCwfn//I7zyfwVlDJ6nTtmSSMVjlHOOighS3rnSAK2j+SrACWAaoxRfyOEck31WKTid0U35YsuMvQP54lydCSrs+kM6o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222074; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=xPCIV1WHCR/6ew8HNr6AwsVRA6ttLPvVnjSxpM66xBM=; b=CWhk28dlQl7LYPzn5qaHperbrhTql4BlvcVzhkTbVP4YqRbhog5s0xncDtT7kCVDILZK+GGeRO/xdxPIg0z8uGppWHzoYSX6VtIOtn7kYarRcdQTmQMv9Is8JImfFDKHc5TlCijIk2QuZBdQnaJyBKJ4MGDw+DKFLKY/40lkrF0= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222074227908.874645950125; Mon, 1 Feb 2021 15:27:54 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80191.146512 (Exim 4.92) (envelope-from ) id 1l6ibb-0007UN-VW; Mon, 01 Feb 2021 23:27:39 +0000 Received: by outflank-mailman (output) from mailman id 80191.146512; Mon, 01 Feb 2021 23:27:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibb-0007UB-P4; Mon, 01 Feb 2021 23:27:39 +0000 Received: by outflank-mailman (input) for mailman id 80191; Mon, 01 Feb 2021 23:27:38 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6iba-0007PB-S1 for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:38 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 530ec581-18cd-4167-be04-6ebe22ae1b1a; Mon, 01 Feb 2021 23:27:33 +0000 (UTC) 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: 530ec581-18cd-4167-be04-6ebe22ae1b1a DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222053; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ndDeJS/U1dfYy3KhxJrt2mDw759yVCsV1w6A2jxC4Vc=; b=ONwkkz4bp/Rs/aDmWEzl3ejg2gfVuUGHPmueZ8g+Y0eohiCZ9YSzCSvy x6RI8SyEU9dObXAgtHppbolQJk2VYQCkKWu6zRvbggnNtXpZUySDJOsRC AIVTcG3E8V/LhFACCqWdwyTKXCE8O3bxv9jD6ZZuh6tM0r50meMvFiW75 Q=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: qPaTmDrkP1LKm8wqBC3XVmHClBioSLKitKOvQ8gdeQSj8QZTBSD0gl6IF4SE3Jk6eaauqodLPf XeAAr0udY8Y/XhFkyeew+ppbCt3K4DrTK8SQVhInP7O29toYZdCSQagGB67guO7cxm+C0NKqp/ rv8MFkIvoCSWNEEw0sPtuejPeiHoMs7vhYjvPXAgOwdoLcySy2iAShIBmLfgwwFvjlkqoIlD3C bKWkohFeKuCfe5aJbhnBrE+7bhd1hGuI44pzxwYMICvfuAEnZORRz+WBWlQPkaA8gsO5S8UJzD 6hc= X-SBRS: 5.1 X-MesageID: 36319797 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36319797" From: Andrew Cooper To: Xen-devel CC: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Anthony PERARD , Tamas K Lengyel Subject: [PATCH v9 02/11] xen/domain: Add vmtrace_size domain creation parameter Date: Mon, 1 Feb 2021 23:26:54 +0000 Message-ID: <20210201232703.29275-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Micha=C5=82 Leszczy=C5=84ski To use vmtrace, buffers of a suitable size need allocating, and different tasks will want different sizes. Add a domain creation parameter, and audit it appropriately in the {arch_,}sanitise_domain_config() functions. For now, the x86 specific auditing is tuned to Processor Trace running in Single Output mode, which requires a single contiguous range of memory. The size is given an arbitrary limit of 64M which is expected to be enough = for anticipated usecases, but not large enough to get into long-running-hyperca= ll problems. Signed-off-by: Micha=C5=82 Leszczy=C5=84ski Signed-off-by: Andrew Cooper Reviewed-by: Roger Pau Monn=C3=A9 Acked-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu CC: Anthony PERARD CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel When support for later generations of IPT get added, we can in principle st= art to use ToTP which is a scatter list of smaller trace regions to use, if we need to massively up the buffer size available. v9: * Drop misleading comments in vmtrace_alloc_buffer(). Memory still gets leaked in theoretical corner cases, but the pattern needs fixing across = the board when we figure out a solution. v8: * Rename vmtrace_frames to vmtrace_size. Reposition to fill a hole. * Rename vmtrace.buf to vmtrace.pg. * Rework the refcounting logic and comment it *very* clearly. v7: * Major chop&change within the series. * Use the name 'vmtrace' consistently. * Use the (new) common vcpu_teardown() functionality, rather than leaving a latent memory leak on ARM. --- xen/arch/x86/domain.c | 23 ++++++++++++++++ xen/common/domain.c | 64 +++++++++++++++++++++++++++++++++++++++++= ++++ xen/include/public/domctl.h | 3 +++ xen/include/xen/sched.h | 6 +++++ 4 files changed, 96 insertions(+) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index b9ba04633e..6c7ee25f3b 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -660,6 +660,29 @@ int arch_sanitise_domain_config(struct xen_domctl_crea= tedomain *config) return -EINVAL; } =20 + if ( config->vmtrace_size ) + { + unsigned int size =3D config->vmtrace_size; + + ASSERT(vmtrace_available); /* Checked by common code. */ + + /* + * For now, vmtrace is restricted to HVM guests, and using a + * power-of-2 buffer between 4k and 64M in size. + */ + if ( !hvm ) + { + dprintk(XENLOG_INFO, "vmtrace not supported for PV\n"); + return -EINVAL; + } + + if ( size < PAGE_SIZE || size > MB(64) || (size & (size - 1)) ) + { + dprintk(XENLOG_INFO, "Unsupported vmtrace size: %#x\n", size); + return -EINVAL; + } + } + return 0; } =20 diff --git a/xen/common/domain.c b/xen/common/domain.c index d1e94d88cf..b6f8d2f536 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -132,6 +132,56 @@ static void vcpu_info_reset(struct vcpu *v) v->vcpu_info_mfn =3D INVALID_MFN; } =20 +static void vmtrace_free_buffer(struct vcpu *v) +{ + const struct domain *d =3D v->domain; + struct page_info *pg =3D v->vmtrace.pg; + unsigned int i; + + if ( !pg ) + return; + + v->vmtrace.pg =3D NULL; + + for ( i =3D 0; i < (d->vmtrace_size >> PAGE_SHIFT); i++ ) + { + put_page_alloc_ref(&pg[i]); + put_page_and_type(&pg[i]); + } +} + +static int vmtrace_alloc_buffer(struct vcpu *v) +{ + struct domain *d =3D v->domain; + struct page_info *pg; + unsigned int i; + + if ( !d->vmtrace_size ) + return 0; + + pg =3D alloc_domheap_pages(d, get_order_from_bytes(d->vmtrace_size), + MEMF_no_refcount); + if ( !pg ) + return -ENOMEM; + + for ( i =3D 0; i < (d->vmtrace_size >> PAGE_SHIFT); i++ ) + if ( unlikely(!get_page_and_type(&pg[i], d, PGT_writable_page)) ) + goto refcnt_err; + + /* + * We must only let vmtrace_free_buffer() take any action in the succe= ss + * case when we've taken all the refs it intends to drop. + */ + v->vmtrace.pg =3D pg; + return 0; + + refcnt_err: + while ( i-- ) + put_page_and_type(&pg[i]); + + return -ENODATA; +} + /* * Release resources held by a vcpu. There may or may not be live referen= ces * to the vcpu, and it may or may not be fully constructed. @@ -140,6 +190,8 @@ static void vcpu_info_reset(struct vcpu *v) */ static int vcpu_teardown(struct vcpu *v) { + vmtrace_free_buffer(v); + return 0; } =20 @@ -201,6 +253,9 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int= vcpu_id) if ( sched_init_vcpu(v) !=3D 0 ) goto fail_wq; =20 + if ( vmtrace_alloc_buffer(v) !=3D 0 ) + goto fail_wq; + if ( arch_vcpu_create(v) !=3D 0 ) goto fail_sched; =20 @@ -449,6 +504,12 @@ static int sanitise_domain_config(struct xen_domctl_cr= eatedomain *config) } } =20 + if ( config->vmtrace_size && !vmtrace_available ) + { + dprintk(XENLOG_INFO, "vmtrace requested but not available\n"); + return -EINVAL; + } + return arch_sanitise_domain_config(config); } =20 @@ -474,7 +535,10 @@ struct domain *domain_create(domid_t domid, ASSERT(is_system_domain(d) ? config =3D=3D NULL : config !=3D NULL); =20 if ( config ) + { d->options =3D config->flags; + d->vmtrace_size =3D config->vmtrace_size; + } =20 /* Sort out our idea of is_control_domain(). */ d->is_privileged =3D is_priv; diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 666aeb71bf..88a5b1ef5d 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -95,6 +95,9 @@ struct xen_domctl_createdomain { int32_t max_grant_frames; int32_t max_maptrack_frames; =20 + /* Per-vCPU buffer size in bytes. 0 to disable. */ + uint32_t vmtrace_size; + struct xen_arch_domainconfig arch; }; =20 diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 06dba1a397..bc78a09a53 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -272,6 +272,10 @@ struct vcpu /* vPCI per-vCPU area, used to store data for long running operations.= */ struct vpci_vcpu vpci; =20 + struct { + struct page_info *pg; /* One contiguous allocation of d->vmtrace_s= ize */ + } vmtrace; + struct arch_vcpu arch; =20 #ifdef CONFIG_IOREQ_SERVER @@ -547,6 +551,8 @@ struct domain unsigned int guest_request_sync : 1; } monitor; =20 + unsigned int vmtrace_size; /* Buffer size in bytes, or 0 to disable. */ + #ifdef CONFIG_ARGO /* Argo interdomain communication support */ struct argo_domain *argo; --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222077; cv=none; d=zohomail.com; s=zohoarc; b=KBg2lt50JoBmb0LeZpKjI0JdY4fTQ9xWhPa/6ba0FPzuu8nEDdTEZqolSbBn+SRuaBQ51wlNvco75QONIdRcaI3D51nwG6UPgJdzheWwTa5h1+9unZHR9n1d+7yg0pAreE+n9VE9pY/4HNLYGpFlzwUYzZ7QjQBLmyNI0PdvZkM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222077; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9NJ6fB3k3OyUFzlBxQcOZx7H19U8d+toY9voeEAA/sw=; b=Riqb351rXAP+072OgRnRldRNhAyOX9MVRgMs3vGm6oF6A8gDirY7tYf5TQnaPxVLBW08q/P1yRU0aV07HRIeB3RAExUpyh9WEFjhyQFTtpGxRbSWq7oupaBIrHTHJHKWWfjYFvwF+beRgeJfU/oHp2ruO1VvWx8EKJGaHmtjm5A= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222076920351.7064600660158; Mon, 1 Feb 2021 15:27:56 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80188.146482 (Exim 4.92) (envelope-from ) id 1l6ibW-0007PJ-4w; Mon, 01 Feb 2021 23:27:34 +0000 Received: by outflank-mailman (output) from mailman id 80188.146482; Mon, 01 Feb 2021 23:27:34 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibV-0007PC-Vb; Mon, 01 Feb 2021 23:27:33 +0000 Received: by outflank-mailman (input) for mailman id 80188; Mon, 01 Feb 2021 23:27:33 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibV-0007P6-0R for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:33 +0000 Received: from esa6.hc3370-68.iphmx.com (unknown [216.71.155.175]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 913af046-38af-40b3-83dc-937cf1f68d92; Mon, 01 Feb 2021 23:27:31 +0000 (UTC) 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: 913af046-38af-40b3-83dc-937cf1f68d92 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222050; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yyB7IEX91e5BgUihcc8rw4F6xaxM9TfCWmmgF4h+/LI=; b=CFvMY5+F9R9VND5Itfx6LGX5uIasTK2hc2ad+UkucMAcUUe+FEFu6D9b 8HXi0UYtHqaogOfgJIqLMdgGdKcfPM7GflrX86NOYqZGt3V0iwxsjrQ3/ sANxYNBPDMTccchXDbqgDqusqZH6s7Lm0eZTa0ynYQPrei31+D4ifyL5Q M=; Authentication-Results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: YEENkP44gzMZtTh5zS4/cGoSBfGzd33S8ERdyX2jzq/bBs4QGabWaOEJ/mFSIEyph8x50neiVi KAqLlL3R861Wfcm5JFExOEOYhMujndOEyXK/FYmoAN/ZIwaMmS5mLNN/gjg/RFORH1EhPyU8Yr eissbn/PAIMQh+HA2acPq90Wn6tiyhpPlnyhnW+ucL00bIE/+mUSRI2mt1Kj0ZIo2VvECq5F+e HwR2ZNjl995nSiQ+ZIN+Gej3vnjNVHHc56sxbh4D5y06SN1JBILuqUy5xsp83leAHnbuFm8Wih 44E= X-SBRS: 5.1 X-MesageID: 36523025 X-Ironport-Server: esa6.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36523025" From: Andrew Cooper To: Xen-devel CC: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Andrew Cooper , Ian Jackson , Wei Liu , Anthony PERARD , "Tamas K Lengyel" Subject: [PATCH v9 03/11] tools/[lib]xl: Add vmtrace_buf_size parameter Date: Mon, 1 Feb 2021 23:26:55 +0000 Message-ID: <20210201232703.29275-4-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Micha=C5=82 Leszczy=C5=84ski Allow to specify the size of per-vCPU trace buffer upon domain creation. This is zero by default (meaning: not enabled). Signed-off-by: Micha=C5=82 Leszczy=C5=84ski Signed-off-by: Andrew Cooper Acked-by: Ian Jackson --- CC: Ian Jackson CC: Wei Liu CC: Anthony PERARD CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v8: * Rebase over vmtrace_size change. v7: * Use the name 'vmtrace' consistently. --- docs/man/xl.cfg.5.pod.in | 9 +++++++++ tools/golang/xenlight/helpers.gen.go | 2 ++ tools/golang/xenlight/types.gen.go | 1 + tools/include/libxl.h | 7 +++++++ tools/libs/light/libxl_create.c | 1 + tools/libs/light/libxl_types.idl | 4 ++++ tools/xl/xl_parse.c | 4 ++++ 7 files changed, 28 insertions(+) diff --git a/docs/man/xl.cfg.5.pod.in b/docs/man/xl.cfg.5.pod.in index 7cdb8595d3..040374dcd6 100644 --- a/docs/man/xl.cfg.5.pod.in +++ b/docs/man/xl.cfg.5.pod.in @@ -681,6 +681,15 @@ Windows). =20 If this option is not specified then it will default to B. =20 +=3Ditem B + +Specifies the size of vmtrace buffer that would be allocated for each +vCPU belonging to this domain. Disabled (i.e. B) by +default. + +B: Acceptable values are platform specific. For Intel Processor +Trace, this value must be a power of 2 between 4k and 16M. + =3Dback =20 =3Dhead2 Devices diff --git a/tools/golang/xenlight/helpers.gen.go b/tools/golang/xenlight/h= elpers.gen.go index 63e2876463..4c60d27a9c 100644 --- a/tools/golang/xenlight/helpers.gen.go +++ b/tools/golang/xenlight/helpers.gen.go @@ -1114,6 +1114,7 @@ return fmt.Errorf("invalid union key '%v'", x.Type)} x.ArchArm.GicVersion =3D GicVersion(xc.arch_arm.gic_version) x.ArchArm.Vuart =3D VuartType(xc.arch_arm.vuart) x.Altp2M =3D Altp2MMode(xc.altp2m) +x.VmtraceBufKb =3D int(xc.vmtrace_buf_kb) =20 return nil} =20 @@ -1589,6 +1590,7 @@ return fmt.Errorf("invalid union key '%v'", x.Type)} xc.arch_arm.gic_version =3D C.libxl_gic_version(x.ArchArm.GicVersion) xc.arch_arm.vuart =3D C.libxl_vuart_type(x.ArchArm.Vuart) xc.altp2m =3D C.libxl_altp2m_mode(x.Altp2M) +xc.vmtrace_buf_kb =3D C.int(x.VmtraceBufKb) =20 return nil } diff --git a/tools/golang/xenlight/types.gen.go b/tools/golang/xenlight/typ= es.gen.go index 5851c38057..cb13002fdb 100644 --- a/tools/golang/xenlight/types.gen.go +++ b/tools/golang/xenlight/types.gen.go @@ -514,6 +514,7 @@ GicVersion GicVersion Vuart VuartType } Altp2M Altp2MMode +VmtraceBufKb int } =20 type domainBuildInfoTypeUnion interface { diff --git a/tools/include/libxl.h b/tools/include/libxl.h index f48d0c5e8a..a7b673e89d 100644 --- a/tools/include/libxl.h +++ b/tools/include/libxl.h @@ -489,6 +489,13 @@ #define LIBXL_HAVE_PHYSINFO_CAP_VMTRACE 1 =20 /* + * LIBXL_HAVE_VMTRACE_BUF_KB indicates that libxl_domain_create_info has a + * vmtrace_buf_kb parameter, which allows to enable pre-allocation of + * processor tracing buffers of given size. + */ +#define LIBXL_HAVE_VMTRACE_BUF_KB 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility diff --git a/tools/libs/light/libxl_create.c b/tools/libs/light/libxl_creat= e.c index 9848d65f36..46f68da697 100644 --- a/tools/libs/light/libxl_create.c +++ b/tools/libs/light/libxl_create.c @@ -607,6 +607,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_conf= ig *d_config, .max_evtchn_port =3D b_info->event_channels, .max_grant_frames =3D b_info->max_grant_frames, .max_maptrack_frames =3D b_info->max_maptrack_frames, + .vmtrace_size =3D ROUNDUP(b_info->vmtrace_buf_kb << 10, XC_PAG= E_SHIFT), }; =20 if (info->type !=3D LIBXL_DOMAIN_TYPE_PV) { diff --git a/tools/libs/light/libxl_types.idl b/tools/libs/light/libxl_type= s.idl index dacb7df6b7..5b85a7419f 100644 --- a/tools/libs/light/libxl_types.idl +++ b/tools/libs/light/libxl_types.idl @@ -648,6 +648,10 @@ libxl_domain_build_info =3D Struct("domain_build_info"= ,[ # supported by x86 HVM and ARM support is planned. ("altp2m", libxl_altp2m_mode), =20 + # Size of preallocated vmtrace trace buffers (in KBYTES). + # Use zero value to disable this feature. + ("vmtrace_buf_kb", integer), + ], dir=3DDIR_IN, copy_deprecated_fn=3D"libxl__domain_build_info_copy_deprecated", ) diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index 867e4d068a..1893cfc086 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -1863,6 +1863,10 @@ void parse_config_data(const char *config_source, } } =20 + if (!xlu_cfg_get_long(config, "vmtrace_buf_kb", &l, 1) && l) { + b_info->vmtrace_buf_kb =3D l; + } + if (!xlu_cfg_get_list(config, "ioports", &ioports, &num_ioports, 0)) { b_info->num_ioports =3D num_ioports; b_info->ioports =3D calloc(num_ioports, sizeof(*b_info->ioports)); --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222075; cv=none; d=zohomail.com; s=zohoarc; b=NG3+NGM++RE+5MdYcg5x4F+Cl8sjf5eM6MV6/vwxezZ85giezhp2+Qv/lWZ9OR/SWRg/EFnOoM18DdZgZ/CaqTK1OBZtS4A2KfnmIWUu/vNqd5XBfYOluZajkR+MCI7M5hr7efXkfQl0Txru5eyEeA9OZR+5UN73yrq7AokxEjk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222075; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=3Nt6E5JJR324KoMH4EN/uUCckRwz0YAQ+VrcLJEOtHU=; b=HFq4gY2OMl2K9PUU9aliE8o1y6dv/C2mx17oC2LmUD17yWm55hwzE/eBv9pVuAreMpDcThy9HME+omHghl/u4P+3O1e2Xt3tKM6Zu1+HOmhYwT2ejNVWjjFtVp2MgR6H98zMdGbKvNJ9nZGevTY4netrwINcAcobawA+gehNqCQ= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222075663322.87035459117465; Mon, 1 Feb 2021 15:27:55 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80190.146506 (Exim 4.92) (envelope-from ) id 1l6ibb-0007Tb-JZ; Mon, 01 Feb 2021 23:27:39 +0000 Received: by outflank-mailman (output) from mailman id 80190.146506; Mon, 01 Feb 2021 23:27:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibb-0007TU-Fr; Mon, 01 Feb 2021 23:27:39 +0000 Received: by outflank-mailman (input) for mailman id 80190; Mon, 01 Feb 2021 23:27:37 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibZ-0007P6-Qt for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:37 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 3d35c18d-68bf-48f9-993b-8fdff7993348; Mon, 01 Feb 2021 23:27:31 +0000 (UTC) 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: 3d35c18d-68bf-48f9-993b-8fdff7993348 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222051; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SJRgavZh5UboqFIes1Vgd8L9o/aFp0WpSJ+Ons+P7bw=; b=VJ+XdxhDljjq4eO4QmvqjbxS6JuTELmP9M433yE/rB9ZrmQT8UYelJug isRTKaF5D1SX4t3QJGklg22yWPQ9ZyTZ09Yvj8Udk0zK1ngsZvJtKWFp9 80doVvdWnbGYSliXDz8y15BscDyES05ImsSU1K6z+VkdanKe/n9Q+/l4b 4=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: vJwJmHhGesYA1E5eg5NT9NMRm0hNc4DFSfh8WkGBNeByiHoRntitHyhtv7W48wl75fZNCSFR3B l9eRQSWp4qQmrLqYrLWODTXN9UNTiUjDrEp7G0kq+sHrEWRc+ICtme/0/aUzmrfSgDIEz2rMLk lUXlRYWLzIsS0rcb+lNOvTEZTm+srPwcMS1jXr2ZSvEWFy1KkNuqy72JYfcR3GpQ+EfCejWROa r7n1y1Z7WeO62A8bLBkWM+tzoB0vu7Rvg0cL80hwuqN0PLvA/vBjziyx34mzBD28nm+4uWukyf JHQ= X-SBRS: 5.1 X-MesageID: 36319798 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36319798" From: Andrew Cooper To: Xen-devel CC: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Tamas K Lengyel Subject: [PATCH v9 04/11] xen/memory: Add a vmtrace_buf resource type Date: Mon, 1 Feb 2021 23:26:56 +0000 Message-ID: <20210201232703.29275-5-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Micha=C5=82 Leszczy=C5=84ski Allow to map processor trace buffer using acquire_resource(). Signed-off-by: Micha=C5=82 Leszczy=C5=84ski Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v8: * Rebase over 'fault' and ARM/IOREQ series. v7: * Rebase over changes elsewhere in the series --- xen/common/memory.c | 29 +++++++++++++++++++++++++++++ xen/include/public/memory.h | 1 + 2 files changed, 30 insertions(+) diff --git a/xen/common/memory.c b/xen/common/memory.c index 128718b31c..fada97a79f 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1085,6 +1085,9 @@ static unsigned int resource_max_frames(const struct = domain *d, case XENMEM_resource_ioreq_server: return ioreq_server_max_frames(d); =20 + case XENMEM_resource_vmtrace_buf: + return d->vmtrace_size >> PAGE_SHIFT; + default: return -EOPNOTSUPP; } @@ -1125,6 +1128,29 @@ static int acquire_ioreq_server(struct domain *d, #endif } =20 +static int acquire_vmtrace_buf( + struct domain *d, unsigned int id, unsigned int frame, + unsigned int nr_frames, xen_pfn_t mfn_list[]) +{ + const struct vcpu *v =3D domain_vcpu(d, id); + unsigned int i; + mfn_t mfn; + + if ( !v ) + return -ENOENT; + + if ( !v->vmtrace.pg || + (frame + nr_frames) > (d->vmtrace_size >> PAGE_SHIFT) ) + return -EINVAL; + + mfn =3D page_to_mfn(v->vmtrace.pg); + + for ( i =3D 0; i < nr_frames; i++ ) + mfn_list[i] =3D mfn_x(mfn) + frame + i; + + return nr_frames; +} + /* * Returns -errno on error, or positive in the range [1, nr_frames] on * success. Returning less than nr_frames contitutes a request for a @@ -1142,6 +1168,9 @@ static int _acquire_resource( case XENMEM_resource_ioreq_server: return acquire_ioreq_server(d, id, frame, nr_frames, mfn_list); =20 + case XENMEM_resource_vmtrace_buf: + return acquire_vmtrace_buf(d, id, frame, nr_frames, mfn_list); + default: return -EOPNOTSUPP; } diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index 020c79d757..50e73eef98 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -625,6 +625,7 @@ struct xen_mem_acquire_resource { =20 #define XENMEM_resource_ioreq_server 0 #define XENMEM_resource_grant_table 1 +#define XENMEM_resource_vmtrace_buf 2 =20 /* * IN - a type-specific resource identifier, which must be zero --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222079; cv=none; d=zohomail.com; s=zohoarc; b=E/JiMLQDuMNwkC8bDXEo/oVL8cXMP6iypRrMZAMJgc2Eth3JTbw2jRHFXn4Hqto1izo8+v5CpZ6gPTDQVs6EsPdRA13icV6Zzfl1zoyLSl/FbvGund16nQnpwvMliwIioIi/L5jHtAdOP4JoawXqS9kD3LIhyuhKw8Xy616xywA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222079; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5dRYX+n9P32MqGDg3g3rXKDdlJHWQWvUttC5B+kHVEE=; b=HflU3rd1n5jv/99SHHe5BkBSDDyyVR3x6hOK+tM38MVfry3vW2vqKSmumBa7Y938sfOv5d5qm7Q3dtHbBoKj7mduBB4xZ2aUxvZNh+/1Y/XwuoZySM4rBZx5NZViNi6jfdpI9xOpnxSO8k3zWghkD0GljxqOS+YQUuAJvWCLaPA= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222079022260.26586402640635; Mon, 1 Feb 2021 15:27:59 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80193.146542 (Exim 4.92) (envelope-from ) id 1l6ibh-0007dl-Oy; Mon, 01 Feb 2021 23:27:45 +0000 Received: by outflank-mailman (output) from mailman id 80193.146542; Mon, 01 Feb 2021 23:27:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibh-0007dY-Ki; Mon, 01 Feb 2021 23:27:45 +0000 Received: by outflank-mailman (input) for mailman id 80193; Mon, 01 Feb 2021 23:27:43 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibf-0007PB-SJ for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:43 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 537d15c5-bb30-4d98-9b4f-c825c6125c33; Mon, 01 Feb 2021 23:27:35 +0000 (UTC) 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: 537d15c5-bb30-4d98-9b4f-c825c6125c33 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222054; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gjwIZ7tSRC53L46IQqr59AJJ4+W6spW+dFwdwkZvhUU=; b=B4oSTp8MF/rSgYgtGoYEqfIsPSii1Uw4J8TOdFTR3YyEoNLwLoWZFVQl Q0Mt96rOY2luWb+YSDCwBb9aF9UItv1aiYjfZXJSLvW099t6ddCETjOoQ XcTco8cLwssuxYMf051xfX7vfuKRO2w6ZOGU9kLgyfgfyHrzeGbHl7TFP g=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: +u4gKq1njIZq4gAJqR+TaWr7sQ2k0elD+4u8/m4xGO9B+F/HKIDmaKjAha0VOR3wqvIR/5cYZ3 HvzpU6TRS/zfPlKF4jrk2IEeWU8DAIuAomqHevt8rpYcdr0M7w29hvrVQ+VxpS+AWdu0QG7wnL EVCHxvgJseACWphXAxAcP7nKsUUEdOaAFvfsrQXNraCsRts3nk68+TjOogGjHLOuy2YTatXmMX t3z1vpFA2iu2M0Gu7qZfkTqVnrD9q7Nm/6jTmg5FS4BWLkjTg6kPuGtXu3nnoVM3jjvIsxO//2 3CE= X-SBRS: 5.1 X-MesageID: 36319802 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36319802" From: Andrew Cooper To: Xen-devel CC: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Jun Nakajima , Kevin Tian , Tamas K Lengyel Subject: [PATCH v9 05/11] x86/vmx: Add Intel Processor Trace support Date: Mon, 1 Feb 2021 23:26:57 +0000 Message-ID: <20210201232703.29275-6-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Micha=C5=82 Leszczy=C5=84ski Add CPUID/MSR enumeration details for Processor Trace. For now, we will on= ly support its use inside VMX operation. Fill in the vmtrace_available boolean to activate the newly introduced common infrastructure for allocating trace buffers. For now, Processor Trace is going to be operated in Single Output mode behi= nd the guests back. Add the MSRs to struct vcpu_msrs, and set up the buffer limit in vmx_init_ipt() as it is fixed for the lifetime of the domain. Context switch the most of the MSRs in and out of vCPU context, but the main control register needs to reside in the MSR load/save lists. Explicitly pu= ll the msrs pointer out into a local variable, because the optimiser cannot ke= ep it live across the memory clobbers in the MSR accesses. Signed-off-by: Micha=C5=82 Leszczy=C5=84ski Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu CC: Jun Nakajima CC: Kevin Tian CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v8: * Plumb bsp boolean down into vmx_init_vmcs_config() * Rename vmx_init_pt() to vmx_init_ipt() * Rebase over vmtrace_size/pg renames. v7: * Major chop&change within the series. * Move MSRs to vcpu_msrs, which is where they'll definitely want to live w= hen we offer PT to VMs for their own use. --- tools/libs/light/libxl_cpuid.c | 1 + tools/misc/xen-cpuid.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 19 +++++++++++++--- xen/arch/x86/hvm/vmx/vmx.c | 34 +++++++++++++++++++++++++= +++- xen/include/asm-x86/cpufeature.h | 1 + xen/include/asm-x86/hvm/vmx/vmcs.h | 4 ++++ xen/include/asm-x86/msr.h | 32 +++++++++++++++++++++++++= ++ xen/include/public/arch-x86/cpufeatureset.h | 1 + 8 files changed, 89 insertions(+), 5 deletions(-) diff --git a/tools/libs/light/libxl_cpuid.c b/tools/libs/light/libxl_cpuid.c index 259612834e..289c59c742 100644 --- a/tools/libs/light/libxl_cpuid.c +++ b/tools/libs/light/libxl_cpuid.c @@ -188,6 +188,7 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *c= puid, const char* str) {"avx512-ifma", 0x00000007, 0, CPUID_REG_EBX, 21, 1}, {"clflushopt", 0x00000007, 0, CPUID_REG_EBX, 23, 1}, {"clwb", 0x00000007, 0, CPUID_REG_EBX, 24, 1}, + {"proc-trace", 0x00000007, 0, CPUID_REG_EBX, 25, 1}, {"avx512pf", 0x00000007, 0, CPUID_REG_EBX, 26, 1}, {"avx512er", 0x00000007, 0, CPUID_REG_EBX, 27, 1}, {"avx512cd", 0x00000007, 0, CPUID_REG_EBX, 28, 1}, diff --git a/tools/misc/xen-cpuid.c b/tools/misc/xen-cpuid.c index c81aa93055..2d04162d8d 100644 --- a/tools/misc/xen-cpuid.c +++ b/tools/misc/xen-cpuid.c @@ -106,7 +106,7 @@ static const char *const str_7b0[32] =3D [18] =3D "rdseed", [19] =3D "adx", [20] =3D "smap", [21] =3D "avx512-ifma", [22] =3D "pcommit", [23] =3D "clflushopt", - [24] =3D "clwb", [25] =3D "pt", + [24] =3D "clwb", [25] =3D "proc-trace", [26] =3D "avx512pf", [27] =3D "avx512er", [28] =3D "avx512cd", [29] =3D "sha", [30] =3D "avx512bw", [31] =3D "avx512vl", diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index 164535f8f0..f9f9bc18cd 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -243,7 +243,7 @@ static bool_t cap_check(const char *name, u32 expected,= u32 saw) return saw !=3D expected; } =20 -static int vmx_init_vmcs_config(void) +static int vmx_init_vmcs_config(bool bsp) { u32 vmx_basic_msr_low, vmx_basic_msr_high, min, opt; u32 _vmx_pin_based_exec_control; @@ -291,6 +291,20 @@ static int vmx_init_vmcs_config(void) _vmx_cpu_based_exec_control &=3D ~(CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING); =20 + rdmsrl(MSR_IA32_VMX_MISC, _vmx_misc_cap); + + /* Check whether IPT is supported in VMX operation. */ + if ( bsp ) + vmtrace_available =3D cpu_has_proc_trace && + (_vmx_misc_cap & VMX_MISC_PROC_TRACE); + else if ( vmtrace_available && + !(_vmx_misc_cap & VMX_MISC_PROC_TRACE) ) + { + printk("VMX: IPT capabilities differ between CPU%u and BSP\n", + smp_processor_id()); + return -EINVAL; + } + if ( _vmx_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTRO= LS ) { min =3D 0; @@ -305,7 +319,6 @@ static int vmx_init_vmcs_config(void) SECONDARY_EXEC_ENABLE_VIRT_EXCEPTIONS | SECONDARY_EXEC_XSAVES | SECONDARY_EXEC_TSC_SCALING); - rdmsrl(MSR_IA32_VMX_MISC, _vmx_misc_cap); if ( _vmx_misc_cap & VMX_MISC_VMWRITE_ALL ) opt |=3D SECONDARY_EXEC_ENABLE_VMCS_SHADOWING; if ( opt_vpid_enabled ) @@ -715,7 +728,7 @@ static int _vmx_cpu_up(bool bsp) wrmsr(MSR_IA32_FEATURE_CONTROL, eax, 0); } =20 - if ( (rc =3D vmx_init_vmcs_config()) !=3D 0 ) + if ( (rc =3D vmx_init_vmcs_config(bsp)) !=3D 0 ) return rc; =20 INIT_LIST_HEAD(&this_cpu(active_vmcs_list)); diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 2d4475ee3d..12b961113e 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -428,6 +428,20 @@ static void vmx_domain_relinquish_resources(struct dom= ain *d) vmx_free_vlapic_mapping(d); } =20 +static void vmx_init_ipt(struct vcpu *v) +{ + unsigned int size =3D v->domain->vmtrace_size; + + if ( !size ) + return; + + /* Checked by domain creation logic. */ + ASSERT(v->vmtrace.pg); + ASSERT(size >=3D PAGE_SIZE && (size & (size - 1)) =3D=3D 0); + + v->arch.msrs->rtit.output_limit =3D size - 1; +} + static int vmx_vcpu_initialise(struct vcpu *v) { int rc; @@ -470,6 +484,7 @@ static int vmx_vcpu_initialise(struct vcpu *v) } =20 vmx_install_vlapic_mapping(v); + vmx_init_ipt(v); =20 return 0; } @@ -508,22 +523,39 @@ static void vmx_restore_host_msrs(void) =20 static void vmx_save_guest_msrs(struct vcpu *v) { + struct vcpu_msrs *msrs =3D v->arch.msrs; + /* * We cannot cache SHADOW_GS_BASE while the VCPU runs, as it can * be updated at any time via SWAPGS, which we cannot trap. */ v->arch.hvm.vmx.shadow_gs =3D read_gs_shadow(); + + if ( v->arch.hvm.vmx.ipt_active ) + { + rdmsrl(MSR_RTIT_OUTPUT_MASK, msrs->rtit.output_mask); + rdmsrl(MSR_RTIT_STATUS, msrs->rtit.status); + } } =20 static void vmx_restore_guest_msrs(struct vcpu *v) { + const struct vcpu_msrs *msrs =3D v->arch.msrs; + write_gs_shadow(v->arch.hvm.vmx.shadow_gs); wrmsrl(MSR_STAR, v->arch.hvm.vmx.star); wrmsrl(MSR_LSTAR, v->arch.hvm.vmx.lstar); wrmsrl(MSR_SYSCALL_MASK, v->arch.hvm.vmx.sfmask); =20 if ( cpu_has_msr_tsc_aux ) - wrmsr_tsc_aux(v->arch.msrs->tsc_aux); + wrmsr_tsc_aux(msrs->tsc_aux); + + if ( v->arch.hvm.vmx.ipt_active ) + { + wrmsrl(MSR_RTIT_OUTPUT_BASE, page_to_maddr(v->vmtrace.pg)); + wrmsrl(MSR_RTIT_OUTPUT_MASK, msrs->rtit.output_mask); + wrmsrl(MSR_RTIT_STATUS, msrs->rtit.status); + } } =20 void vmx_update_cpu_exec_control(struct vcpu *v) diff --git a/xen/include/asm-x86/cpufeature.h b/xen/include/asm-x86/cpufeat= ure.h index f62e526a96..33b2257888 100644 --- a/xen/include/asm-x86/cpufeature.h +++ b/xen/include/asm-x86/cpufeature.h @@ -105,6 +105,7 @@ #define cpu_has_clwb boot_cpu_has(X86_FEATURE_CLWB) #define cpu_has_avx512er boot_cpu_has(X86_FEATURE_AVX512ER) #define cpu_has_avx512cd boot_cpu_has(X86_FEATURE_AVX512CD) +#define cpu_has_proc_trace boot_cpu_has(X86_FEATURE_PROC_TRACE) #define cpu_has_sha boot_cpu_has(X86_FEATURE_SHA) #define cpu_has_avx512bw boot_cpu_has(X86_FEATURE_AVX512BW) #define cpu_has_avx512vl boot_cpu_has(X86_FEATURE_AVX512VL) diff --git a/xen/include/asm-x86/hvm/vmx/vmcs.h b/xen/include/asm-x86/hvm/v= mx/vmcs.h index 906810592f..8073af323b 100644 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -156,6 +156,9 @@ struct vmx_vcpu { /* Do we need to tolerate a spurious EPT_MISCONFIG VM exit? */ bool_t ept_spurious_misconfig; =20 + /* Processor Trace configured and enabled for the vcpu. */ + bool ipt_active; + /* Is the guest in real mode? */ uint8_t vmx_realmode; /* Are we emulating rather than VMENTERing? */ @@ -283,6 +286,7 @@ extern u32 vmx_secondary_exec_control; #define VMX_VPID_INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 0x80000000000ULL extern u64 vmx_ept_vpid_cap; =20 +#define VMX_MISC_PROC_TRACE 0x00004000 #define VMX_MISC_CR3_TARGET 0x01ff0000 #define VMX_MISC_VMWRITE_ALL 0x20000000 =20 diff --git a/xen/include/asm-x86/msr.h b/xen/include/asm-x86/msr.h index 16f95e7344..1d3eca9063 100644 --- a/xen/include/asm-x86/msr.h +++ b/xen/include/asm-x86/msr.h @@ -306,6 +306,38 @@ struct vcpu_msrs }; } misc_features_enables; =20 + /* + * 0x00000560 ... 57x - MSR_RTIT_* + * + * "Real Time Instruction Trace", now called Processor Trace. + * + * These MSRs are not exposed to guests. They are controlled by Xen + * behind the scenes, when vmtrace is enabled for the domain. + * + * MSR_RTIT_OUTPUT_BASE not stored here. It is fixed per vcpu, and + * derived from v->vmtrace.buf. + */ + struct { + /* + * Placed in the MSR load/save lists. Only modified by hypercall = in + * the common case. + */ + uint64_t ctl; + + /* + * Updated by hardware in non-root mode. Synchronised here on vcpu + * context switch. + */ + uint64_t status; + union { + uint64_t output_mask; + struct { + uint32_t output_limit; + uint32_t output_offset; + }; + }; + } rtit; + /* 0x00000da0 - MSR_IA32_XSS */ struct { uint64_t raw; diff --git a/xen/include/public/arch-x86/cpufeatureset.h b/xen/include/publ= ic/arch-x86/cpufeatureset.h index 6f7efaad6d..a501479820 100644 --- a/xen/include/public/arch-x86/cpufeatureset.h +++ b/xen/include/public/arch-x86/cpufeatureset.h @@ -217,6 +217,7 @@ XEN_CPUFEATURE(SMAP, 5*32+20) /*S Supervisor = Mode Access Prevention */ XEN_CPUFEATURE(AVX512_IFMA, 5*32+21) /*A AVX-512 Integer Fused Multiply= Add */ XEN_CPUFEATURE(CLFLUSHOPT, 5*32+23) /*A CLFLUSHOPT instruction */ XEN_CPUFEATURE(CLWB, 5*32+24) /*A CLWB instruction */ +XEN_CPUFEATURE(PROC_TRACE, 5*32+25) /* Processor Trace */ XEN_CPUFEATURE(AVX512PF, 5*32+26) /*A AVX-512 Prefetch Instructions = */ XEN_CPUFEATURE(AVX512ER, 5*32+27) /*A AVX-512 Exponent & Reciprocal = Instrs */ XEN_CPUFEATURE(AVX512CD, 5*32+28) /*A AVX-512 Conflict Detection Ins= trs */ --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222086; cv=none; d=zohomail.com; s=zohoarc; b=XkLF+bFNAVBCCNzs/nsL2m0xodCofX5G2s3WFP45JRqVdNHsK7V1p3lhAqBfWHZQwDtjk70IBtinQQWdT88k9Aatam6/U9/gBckzKISf0v58n/Mq4g9SZFyXIz1NMTfrMxyRvVAppzNIovby2TmVT4Xqzqd71lK3bF4njjABv1Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222086; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9GlW08/8W4kBaNMesf1eoy5qJgxhaGGAAS8/Wj/bnLY=; b=NUvBUFuUy1CF1+fzdpbt1VExj1vBP1IkuDUoRd/2m9lPZ2ze7L4Apd5r8aKTIvujYmJttvTQyO78BSpCg4jT9PXB5Jy1BIRNh/KmDzUOS6TOYvN7dGCawAxBR5PLsXnNY1h9axM746PQhO657sX/PXghw3oaIUOEkJ/sNEqrqKE= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222086764586.5456889870737; Mon, 1 Feb 2021 15:28:06 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80194.146554 (Exim 4.92) (envelope-from ) id 1l6ibl-0007je-74; Mon, 01 Feb 2021 23:27:49 +0000 Received: by outflank-mailman (output) from mailman id 80194.146554; Mon, 01 Feb 2021 23:27:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibl-0007jR-2F; Mon, 01 Feb 2021 23:27:49 +0000 Received: by outflank-mailman (input) for mailman id 80194; Mon, 01 Feb 2021 23:27:47 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibj-0007P6-R7 for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:47 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id c06dd5f6-d926-4d97-b9e7-c3c1e9e49841; Mon, 01 Feb 2021 23:27:33 +0000 (UTC) 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: c06dd5f6-d926-4d97-b9e7-c3c1e9e49841 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222053; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TDLzcFIKoSOVt0T44fLv76oX9OLOeZU1LgI0QoM4uAM=; b=br7pFkh3Yxfga1vsyta+ZOBhdcbIx/sVkdnoQRXdqLZsvDV2hJsgSznC lvscJ8DtUlvsY6yofYg50pOg2YnO+oDu9emseILjH+xUag6uczt9jnBXd OHNylUS56fBramPCa6tv//Ii6NEtHlN4MmB+p2D3E9u0jDsNiMVTemq6p I=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: mNnxg+ANT/zlDaBh9WfHRXCilKPBs3sVM8W70uTCXls00Pxz1RVTGxZKZf2zugHUSi7fZZlsSy xFXIeUqtRd0scZ5Unh5CP3+us5n+2eJqpEaN8TcBk+yQrm/yNVJzmRVS5QwSTsqnvk+4Q+6aMu 6HVmbgb3qG+lSg1Ejo0yJjLWTwGOO8KcZQ8fuZ9ZAtAOS88HIzxQXgscE4uH4GinvWkShfH5Ol fYtlm6BCE434KxkSJzvp667yuUrtbo2QqVlp8RE4r7sEU7T6HhIhEB2eI8WsC12Z1DCrD20HyP OUg= X-SBRS: 5.1 X-MesageID: 36319801 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36319801" From: Andrew Cooper To: Xen-devel CC: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Jun Nakajima , Kevin Tian , Tamas K Lengyel Subject: [PATCH v9 06/11] xen/domctl: Add XEN_DOMCTL_vmtrace_op Date: Mon, 1 Feb 2021 23:26:58 +0000 Message-ID: <20210201232703.29275-7-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Micha=C5=82 Leszczy=C5=84ski Implement an interface to configure and control tracing operations. Reuse = the existing SETDEBUGGING flask vector rather than inventing a new one. Userspace using this interface is going to need platform specific knowledge anyway to interpret the contents of the trace buffer. While some operations (e.g. enable/disable) can reasonably be generic, others cannot. Provide an explicitly-platform specific pair of get/set operations to reduce API churn= as new options get added/enabled. For the VMX specific Processor Trace implementation, tolerate reading and modifying a safe subset of bits in CTL, STATUS and OUTPUT_MASK. This permi= ts userspace to control the content which gets logged, but prevents modificati= on of details such as the position/size of the output buffer. Signed-off-by: Micha=C5=82 Leszczy=C5=84ski Signed-off-by: Andrew Cooper Reviewed-by: Roger Pau Monn=C3=A9 --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu CC: Jun Nakajima CC: Kevin Tian CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v9: * Drop platform-specific access to MSR_RTIT_OUTPUT_MASK. It's not necessa= ry for current usecases, and will simplify adding ToPA support in the futur= e. v8: * Reposition mask constants. v7: * Major chop&change within the series. --- xen/arch/x86/domctl.c | 55 +++++++++++++++++ xen/arch/x86/hvm/vmx/vmx.c | 135 ++++++++++++++++++++++++++++++++++++++= ++++ xen/include/asm-x86/hvm/hvm.h | 63 ++++++++++++++++++++ xen/include/public/domctl.h | 35 +++++++++++ xen/xsm/flask/hooks.c | 1 + 5 files changed, 289 insertions(+) diff --git a/xen/arch/x86/domctl.c b/xen/arch/x86/domctl.c index b28cfe9817..b464465230 100644 --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -155,6 +155,55 @@ void arch_get_domain_info(const struct domain *d, info->arch_config.emulation_flags =3D d->arch.emulation_flags; } =20 +static int do_vmtrace_op(struct domain *d, struct xen_domctl_vmtrace_op *o= p, + XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) +{ + struct vcpu *v; + int rc; + + if ( !d->vmtrace_size || d =3D=3D current->domain /* No vcpu_pause() *= / ) + return -EINVAL; + + ASSERT(is_hvm_domain(d)); /* Restricted by domain creation logic. */ + + v =3D domain_vcpu(d, op->vcpu); + if ( !v ) + return -ENOENT; + + vcpu_pause(v); + switch ( op->cmd ) + { + case XEN_DOMCTL_vmtrace_enable: + case XEN_DOMCTL_vmtrace_disable: + case XEN_DOMCTL_vmtrace_reset_and_enable: + rc =3D hvm_vmtrace_control( + v, op->cmd !=3D XEN_DOMCTL_vmtrace_disable, + op->cmd =3D=3D XEN_DOMCTL_vmtrace_reset_and_enable); + break; + + case XEN_DOMCTL_vmtrace_output_position: + rc =3D hvm_vmtrace_output_position(v, &op->value); + if ( rc >=3D 0 ) + rc =3D 0; + break; + + case XEN_DOMCTL_vmtrace_get_option: + rc =3D hvm_vmtrace_get_option(v, op->key, &op->value); + break; + + case XEN_DOMCTL_vmtrace_set_option: + rc =3D hvm_vmtrace_set_option(v, op->key, op->value); + break; + + default: + rc =3D -EOPNOTSUPP; + break; + } + vcpu_unpause(v); + + return rc; +} + #define MAX_IOPORTS 0x10000 =20 long arch_do_domctl( @@ -1320,6 +1369,12 @@ long arch_do_domctl( domain_unpause(d); break; =20 + case XEN_DOMCTL_vmtrace_op: + ret =3D do_vmtrace_op(d, &domctl->u.vmtrace_op, u_domctl); + if ( !ret ) + copyback =3D true; + break; + default: ret =3D iommu_do_domctl(domctl, d, u_domctl); break; diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 12b961113e..beb5692b8b 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -2261,6 +2261,137 @@ static bool vmx_get_pending_event(struct vcpu *v, s= truct x86_event *info) return true; } =20 +/* + * We only let vmtrace agents see and modify a subset of bits in MSR_RTIT_= CTL. + * These all pertain to data-emitted into the trace buffer(s). Must not + * include controls pertaining to the structure/position of the trace + * buffer(s). + */ +#define RTIT_CTL_MASK \ + (RTIT_CTL_TRACE_EN | RTIT_CTL_OS | RTIT_CTL_USR | RTIT_CTL_TSC_EN | \ + RTIT_CTL_DIS_RETC | RTIT_CTL_BRANCH_EN) + +/* + * Status bits restricted to the first-gen subset (i.e. no further CPUID + * requirements.) + */ +#define RTIT_STATUS_MASK \ + (RTIT_STATUS_FILTER_EN | RTIT_STATUS_CONTEXT_EN | RTIT_STATUS_TRIGGER_= EN | \ + RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED) + +static int vmtrace_get_option(struct vcpu *v, uint64_t key, uint64_t *outp= ut) +{ + const struct vcpu_msrs *msrs =3D v->arch.msrs; + + switch ( key ) + { + case MSR_RTIT_CTL: + *output =3D msrs->rtit.ctl & RTIT_CTL_MASK; + break; + + case MSR_RTIT_STATUS: + *output =3D msrs->rtit.status & RTIT_STATUS_MASK; + break; + + default: + *output =3D 0; + return -EINVAL; + } + return 0; +} + +static int vmtrace_set_option(struct vcpu *v, uint64_t key, uint64_t value) +{ + struct vcpu_msrs *msrs =3D v->arch.msrs; + bool new_en, old_en =3D msrs->rtit.ctl & RTIT_CTL_TRACE_EN; + + switch ( key ) + { + case MSR_RTIT_CTL: + if ( value & ~RTIT_CTL_MASK ) + return -EINVAL; + + msrs->rtit.ctl &=3D ~RTIT_CTL_MASK; + msrs->rtit.ctl |=3D (value & RTIT_CTL_MASK); + break; + + case MSR_RTIT_STATUS: + if ( value & ~RTIT_STATUS_MASK ) + return -EINVAL; + + msrs->rtit.status &=3D ~RTIT_STATUS_MASK; + msrs->rtit.status |=3D (value & RTIT_STATUS_MASK); + break; + + default: + return -EINVAL; + } + + new_en =3D msrs->rtit.ctl & RTIT_CTL_TRACE_EN; + + /* ctl.trace_en changed =3D> update MSR load/save lists appropriately.= */ + if ( !old_en && new_en ) + { + if ( vmx_add_guest_msr(v, MSR_RTIT_CTL, msrs->rtit.ctl) || + vmx_add_host_load_msr(v, MSR_RTIT_CTL, 0) ) + { + /* + * The only failure cases here are failing the + * singleton-per-domain memory allocation, or exceeding the sp= ace + * in the allocation. We could unwind in principle, but there= is + * nothing userspace can usefully do to continue using this VM. + */ + domain_crash(v->domain); + return -ENXIO; + } + } + else if ( old_en && !new_en ) + { + vmx_del_msr(v, MSR_RTIT_CTL, VMX_MSR_GUEST); + vmx_del_msr(v, MSR_RTIT_CTL, VMX_MSR_HOST); + } + + return 0; +} + +static int vmtrace_control(struct vcpu *v, bool enable, bool reset) +{ + struct vcpu_msrs *msrs =3D v->arch.msrs; + uint64_t new_ctl; + int rc; + + /* + * Absolutely nothing good will come of Xen's and userspace's idea of + * whether ipt is enabled getting out of sync. + */ + if ( v->arch.hvm.vmx.ipt_active =3D=3D enable ) + return -EINVAL; + + if ( reset ) + { + msrs->rtit.status =3D 0; + msrs->rtit.output_offset =3D 0; + } + + new_ctl =3D msrs->rtit.ctl & ~RTIT_CTL_TRACE_EN; + if ( enable ) + new_ctl |=3D RTIT_CTL_TRACE_EN; + + rc =3D vmtrace_set_option(v, MSR_RTIT_CTL, new_ctl); + if ( rc ) + return rc; + + v->arch.hvm.vmx.ipt_active =3D enable; + + return 0; +} + +static int vmtrace_output_position(struct vcpu *v, uint64_t *pos) +{ + *pos =3D v->arch.msrs->rtit.output_offset; + return v->arch.hvm.vmx.ipt_active; +} + static struct hvm_function_table __initdata vmx_function_table =3D { .name =3D "VMX", .cpu_up_prepare =3D vmx_cpu_up_prepare, @@ -2316,6 +2447,10 @@ static struct hvm_function_table __initdata vmx_func= tion_table =3D { .altp2m_vcpu_update_vmfunc_ve =3D vmx_vcpu_update_vmfunc_ve, .altp2m_vcpu_emulate_ve =3D vmx_vcpu_emulate_ve, .altp2m_vcpu_emulate_vmfunc =3D vmx_vcpu_emulate_vmfunc, + .vmtrace_control =3D vmtrace_control, + .vmtrace_output_position =3D vmtrace_output_position, + .vmtrace_set_option =3D vmtrace_set_option, + .vmtrace_get_option =3D vmtrace_get_option, .tsc_scaling =3D { .max_ratio =3D VMX_TSC_MULTIPLIER_MAX, }, diff --git a/xen/include/asm-x86/hvm/hvm.h b/xen/include/asm-x86/hvm/hvm.h index 334bd573b9..960ec03917 100644 --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -214,6 +214,12 @@ struct hvm_function_table { bool_t (*altp2m_vcpu_emulate_ve)(struct vcpu *v); int (*altp2m_vcpu_emulate_vmfunc)(const struct cpu_user_regs *regs); =20 + /* vmtrace */ + int (*vmtrace_control)(struct vcpu *v, bool enable, bool reset); + int (*vmtrace_output_position)(struct vcpu *v, uint64_t *pos); + int (*vmtrace_set_option)(struct vcpu *v, uint64_t key, uint64_t value= ); + int (*vmtrace_get_option)(struct vcpu *v, uint64_t key, uint64_t *valu= e); + /* * Parameters and callbacks for hardware-assisted TSC scaling, * which are valid only when the hardware feature is available. @@ -655,6 +661,41 @@ static inline bool altp2m_vcpu_emulate_ve(struct vcpu = *v) return false; } =20 +static inline int hvm_vmtrace_control(struct vcpu *v, bool enable, bool re= set) +{ + if ( hvm_funcs.vmtrace_control ) + return hvm_funcs.vmtrace_control(v, enable, reset); + + return -EOPNOTSUPP; +} + +/* Returns -errno, or a boolean of whether tracing is currently active. */ +static inline int hvm_vmtrace_output_position(struct vcpu *v, uint64_t *po= s) +{ + if ( hvm_funcs.vmtrace_output_position ) + return hvm_funcs.vmtrace_output_position(v, pos); + + return -EOPNOTSUPP; +} + +static inline int hvm_vmtrace_set_option( + struct vcpu *v, uint64_t key, uint64_t value) +{ + if ( hvm_funcs.vmtrace_set_option ) + return hvm_funcs.vmtrace_set_option(v, key, value); + + return -EOPNOTSUPP; +} + +static inline int hvm_vmtrace_get_option( + struct vcpu *v, uint64_t key, uint64_t *value) +{ + if ( hvm_funcs.vmtrace_get_option ) + return hvm_funcs.vmtrace_get_option(v, key, value); + + return -EOPNOTSUPP; +} + /* * This must be defined as a macro instead of an inline function, * because it uses 'struct vcpu' and 'struct domain' which have @@ -751,6 +792,28 @@ static inline bool hvm_has_set_descriptor_access_exiti= ng(void) return false; } =20 +static inline int hvm_vmtrace_control(struct vcpu *v, bool enable, bool re= set) +{ + return -EOPNOTSUPP; +} + +static inline int hvm_vmtrace_output_position(struct vcpu *v, uint64_t *po= s) +{ + return -EOPNOTSUPP; +} + +static inline int hvm_vmtrace_set_option( + struct vcpu *v, uint64_t key, uint64_t value) +{ + return -EOPNOTSUPP; +} + +static inline int hvm_vmtrace_get_option( + struct vcpu *v, uint64_t key, uint64_t *value) +{ + return -EOPNOTSUPP; +} + #define is_viridian_domain(d) ((void)(d), false) #define is_viridian_vcpu(v) ((void)(v), false) #define has_viridian_time_ref_count(d) ((void)(d), false) diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 88a5b1ef5d..4dbf107785 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -1135,6 +1135,39 @@ struct xen_domctl_vuart_op { */ }; =20 +/* XEN_DOMCTL_vmtrace_op: Perform VM tracing operations. */ +struct xen_domctl_vmtrace_op { + uint32_t cmd; /* IN */ + uint32_t vcpu; /* IN */ + uint64_aligned_t key; /* IN - @cmd specific data. */ + uint64_aligned_t value; /* IN/OUT - @cmd specific data. */ + + /* + * General enable/disable of tracing. + * + * XEN_DOMCTL_vmtrace_reset_and_enable is provided as optimisation for + * common usecases, which want to reset status and position information + * when turning tracing back on. + */ +#define XEN_DOMCTL_vmtrace_enable 1 +#define XEN_DOMCTL_vmtrace_disable 2 +#define XEN_DOMCTL_vmtrace_reset_and_enable 3 + + /* Obtain the current output position within the buffer. Fills @value= . */ +#define XEN_DOMCTL_vmtrace_output_position 4 + + /* + * Get/Set platform specific configuration. + * + * For Intel Processor Trace, @key/@value are interpreted as MSR + * reads/writes to MSR_RTIT_*, filtered to a safe subset. + */ +#define XEN_DOMCTL_vmtrace_get_option 5 +#define XEN_DOMCTL_vmtrace_set_option 6 +}; +typedef struct xen_domctl_vmtrace_op xen_domctl_vmtrace_op_t; +DEFINE_XEN_GUEST_HANDLE(xen_domctl_vmtrace_op_t); + struct xen_domctl { uint32_t cmd; #define XEN_DOMCTL_createdomain 1 @@ -1219,6 +1252,7 @@ struct xen_domctl { #define XEN_DOMCTL_vuart_op 81 #define XEN_DOMCTL_get_cpu_policy 82 #define XEN_DOMCTL_set_cpu_policy 83 +#define XEN_DOMCTL_vmtrace_op 84 #define XEN_DOMCTL_gdbsx_guestmemio 1000 #define XEN_DOMCTL_gdbsx_pausevcpu 1001 #define XEN_DOMCTL_gdbsx_unpausevcpu 1002 @@ -1279,6 +1313,7 @@ struct xen_domctl { struct xen_domctl_monitor_op monitor_op; struct xen_domctl_psr_alloc psr_alloc; struct xen_domctl_vuart_op vuart_op; + struct xen_domctl_vmtrace_op vmtrace_op; uint8_t pad[128]; } u; }; diff --git a/xen/xsm/flask/hooks.c b/xen/xsm/flask/hooks.c index 11784d7425..3b7313b949 100644 --- a/xen/xsm/flask/hooks.c +++ b/xen/xsm/flask/hooks.c @@ -703,6 +703,7 @@ static int flask_domctl(struct domain *d, int cmd) return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__VM_EVENT); =20 case XEN_DOMCTL_debug_op: + case XEN_DOMCTL_vmtrace_op: case XEN_DOMCTL_gdbsx_guestmemio: case XEN_DOMCTL_gdbsx_pausevcpu: case XEN_DOMCTL_gdbsx_unpausevcpu: --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222073; cv=none; d=zohomail.com; s=zohoarc; b=RQCE/ENRzMH8WqNfk7XfY669d+TQNjnHorywA/OoaeJV4+RhLGYV1nFwSj0NgzA75JP7eeX2OBdhVBSa3+uYNXk9t6MBe2DkXksTojQajQJjBXP2BaEIVFsX6aQUEr/0AKZVkeXb6/8t+5zWi8e96XmshtAdw/8ytSPxfjRo80Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222073; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=OOl+6dOz/GKWfeNJufTbWMNfyQTCiliRW7mWG3IpAtU=; b=CDsd99zbfZTSmbYnr604gWpjOKcuswO5AE8AdbtH8g6I6BBzLm//VmvneAREbGohpt3Z4+aSC8Ryx2QvqVWxJ9+f+0kxZn3APxLkYiJkjRbaQGhRY9zLFfcuqXMQAOcgDN+3dxP6oi5VncaXMFmOOX9l4yJzB1Hav1La+qAwMfE= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222073249179.6249732457071; Mon, 1 Feb 2021 15:27:53 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80189.146493 (Exim 4.92) (envelope-from ) id 1l6ibX-0007QH-AZ; Mon, 01 Feb 2021 23:27:35 +0000 Received: by outflank-mailman (output) from mailman id 80189.146493; Mon, 01 Feb 2021 23:27:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibX-0007QA-7Z; Mon, 01 Feb 2021 23:27:35 +0000 Received: by outflank-mailman (input) for mailman id 80189; Mon, 01 Feb 2021 23:27:34 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibW-0007PB-0l for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:34 +0000 Received: from esa6.hc3370-68.iphmx.com (unknown [216.71.155.175]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 5bda2d77-d5a5-4294-9fd1-d4c2be43d2e3; Mon, 01 Feb 2021 23:27:32 +0000 (UTC) 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: 5bda2d77-d5a5-4294-9fd1-d4c2be43d2e3 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222052; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EvfjqR50MUjZfV+wJxte5vJxLsTztJ8urztBWoZwrE4=; b=a64ywfGPWzZrgGIZ9nz5YSx5JWVPNBK9O+cLzYjkjQ0XabURaXYhTRRE YqibgUDpj+qcAeC6ObSohMbGGtMzC8aLnsjcasW6qcG5UbLQWwA/3t6// llOg3BnNbSBN1hAWoUx8FNUPmD1VV/efdFecmW76lA7vZvtXdcpEcBfqR c=; Authentication-Results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: p+bwOS9yhAo0dKZ1+LmmyuytD6afidLE/c6n6GRnIcBSgmr0eDty1Gtj6MxTt4mjmczbooMOYP U8rfJeV3iX3LGukV840Cc9jx8pL50wshDUT/vR4nxdOdMCYbLVAWa8066eaRYUDU7k1WhQ3m2U Vb5TxY/FC4fFeJR7F1oB40hFd4ohKIziSaAhvJPZPjvW1FiFy1rSNEJEhd4BoXWMtXZQQTi/mx Zt9tJLlcB6Xfx/vz09iN1F0ae8yvRdQnQwJ8Qpo6/KP5xliDfdWxsWlsW3WIh+TnTn+txqSZvu +jk= X-SBRS: 5.1 X-MesageID: 36523027 X-Ironport-Server: esa6.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36523027" From: Andrew Cooper To: Xen-devel CC: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Andrew Cooper , Ian Jackson , Wei Liu , Tamas K Lengyel Subject: [PATCH v9 07/11] tools/libxc: Add xc_vmtrace_* functions Date: Mon, 1 Feb 2021 23:26:59 +0000 Message-ID: <20210201232703.29275-8-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Micha=C5=82 Leszczy=C5=84ski Add functions in libxc that use the new XEN_DOMCTL_vmtrace interface. Signed-off-by: Micha=C5=82 Leszczy=C5=84ski Signed-off-by: Andrew Cooper Acked-by: Ian Jackson --- CC: Ian Jackson CC: Wei Liu CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v7: * Use the name 'vmtrace' consistently. --- tools/include/xenctrl.h | 73 ++++++++++++++++++++++++ tools/libs/ctrl/Makefile | 1 + tools/libs/ctrl/xc_vmtrace.c | 128 +++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 202 insertions(+) create mode 100644 tools/libs/ctrl/xc_vmtrace.c diff --git a/tools/include/xenctrl.h b/tools/include/xenctrl.h index 3796425e1e..0efcdae8b4 100644 --- a/tools/include/xenctrl.h +++ b/tools/include/xenctrl.h @@ -1583,6 +1583,79 @@ int xc_tbuf_set_cpu_mask(xc_interface *xch, xc_cpuma= p_t mask); =20 int xc_tbuf_set_evt_mask(xc_interface *xch, uint32_t mask); =20 +/** + * Enable vmtrace for given vCPU. + * + * @parm xch a handle to an open hypervisor interface + * @parm domid domain identifier + * @parm vcpu vcpu identifier + * @return 0 on success, -1 on failure + */ +int xc_vmtrace_enable(xc_interface *xch, uint32_t domid, uint32_t vcpu); + +/** + * Enable vmtrace for given vCPU. + * + * @parm xch a handle to an open hypervisor interface + * @parm domid domain identifier + * @parm vcpu vcpu identifier + * @return 0 on success, -1 on failure + */ +int xc_vmtrace_disable(xc_interface *xch, uint32_t domid, uint32_t vcpu); + +/** + * Enable vmtrace for a given vCPU, along with resetting status/offset + * details. + * + * @parm xch a handle to an open hypervisor interface + * @parm domid domain identifier + * @parm vcpu vcpu identifier + * @return 0 on success, -1 on failure + */ +int xc_vmtrace_reset_and_enable(xc_interface *xch, uint32_t domid, + uint32_t vcpu); + +/** + * Get current output position inside the trace buffer. + * + * Repeated calls will return different values if tracing is enabled. It = is + * platform specific what happens when the buffer fills completely. + * + * @parm xch a handle to an open hypervisor interface + * @parm domid domain identifier + * @parm vcpu vcpu identifier + * @parm pos current output position in bytes + * @return 0 on success, -1 on failure + */ +int xc_vmtrace_output_position(xc_interface *xch, uint32_t domid, + uint32_t vcpu, uint64_t *pos); + +/** + * Get platform specific vmtrace options. + * + * @parm xch a handle to an open hypervisor interface + * @parm domid domain identifier + * @parm vcpu vcpu identifier + * @parm key platform-specific input + * @parm value platform-specific output + * @return 0 on success, -1 on failure + */ +int xc_vmtrace_get_option(xc_interface *xch, uint32_t domid, + uint32_t vcpu, uint64_t key, uint64_t *value); + +/** + * Set platform specific vntvmtrace options. + * + * @parm xch a handle to an open hypervisor interface + * @parm domid domain identifier + * @parm vcpu vcpu identifier + * @parm key platform-specific input + * @parm value platform-specific input + * @return 0 on success, -1 on failure + */ +int xc_vmtrace_set_option(xc_interface *xch, uint32_t domid, + uint32_t vcpu, uint64_t key, uint64_t value); + int xc_domctl(xc_interface *xch, struct xen_domctl *domctl); int xc_sysctl(xc_interface *xch, struct xen_sysctl *sysctl); =20 diff --git a/tools/libs/ctrl/Makefile b/tools/libs/ctrl/Makefile index 6106e36c49..ce9ecae710 100644 --- a/tools/libs/ctrl/Makefile +++ b/tools/libs/ctrl/Makefile @@ -22,6 +22,7 @@ SRCS-y +=3D xc_pm.c SRCS-y +=3D xc_cpu_hotplug.c SRCS-y +=3D xc_resume.c SRCS-y +=3D xc_vm_event.c +SRCS-y +=3D xc_vmtrace.c SRCS-y +=3D xc_monitor.c SRCS-y +=3D xc_mem_paging.c SRCS-y +=3D xc_mem_access.c diff --git a/tools/libs/ctrl/xc_vmtrace.c b/tools/libs/ctrl/xc_vmtrace.c new file mode 100644 index 0000000000..602502367f --- /dev/null +++ b/tools/libs/ctrl/xc_vmtrace.c @@ -0,0 +1,128 @@ +/*************************************************************************= ***** + * xc_vmtrace.c + * + * API for manipulating hardware tracing features + * + * Copyright (c) 2020, Michal Leszczynski + * + * Copyright 2020 CERT Polska. All rights reserved. + * Use is subject to license terms. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; If not, see . + */ + +#include "xc_private.h" + +int xc_vmtrace_enable( + xc_interface *xch, uint32_t domid, uint32_t vcpu) +{ + struct xen_domctl domctl =3D { + .cmd =3D XEN_DOMCTL_vmtrace_op, + .domain =3D domid, + .u.vmtrace_op =3D { + .cmd =3D XEN_DOMCTL_vmtrace_enable, + .vcpu =3D vcpu, + }, + }; + + return do_domctl(xch, &domctl); +} + +int xc_vmtrace_disable( + xc_interface *xch, uint32_t domid, uint32_t vcpu) +{ + struct xen_domctl domctl =3D { + .cmd =3D XEN_DOMCTL_vmtrace_op, + .domain =3D domid, + .u.vmtrace_op =3D { + .cmd =3D XEN_DOMCTL_vmtrace_disable, + .vcpu =3D vcpu, + }, + }; + + return do_domctl(xch, &domctl); +} + +int xc_vmtrace_reset_and_enable( + xc_interface *xch, uint32_t domid, uint32_t vcpu) +{ + struct xen_domctl domctl =3D { + .cmd =3D XEN_DOMCTL_vmtrace_op, + .domain =3D domid, + .u.vmtrace_op =3D { + .cmd =3D XEN_DOMCTL_vmtrace_reset_and_enable, + .vcpu =3D vcpu, + }, + }; + + return do_domctl(xch, &domctl); +} + +int xc_vmtrace_output_position( + xc_interface *xch, uint32_t domid, uint32_t vcpu, uint64_t *pos) +{ + struct xen_domctl domctl =3D { + .cmd =3D XEN_DOMCTL_vmtrace_op, + .domain =3D domid, + .u.vmtrace_op =3D { + .cmd =3D XEN_DOMCTL_vmtrace_output_position, + .vcpu =3D vcpu, + }, + }; + int rc =3D do_domctl(xch, &domctl); + + if ( !rc ) + *pos =3D domctl.u.vmtrace_op.value; + + return rc; +} + +int xc_vmtrace_get_option( + xc_interface *xch, uint32_t domid, uint32_t vcpu, + uint64_t key, uint64_t *value) +{ + struct xen_domctl domctl =3D { + .cmd =3D XEN_DOMCTL_vmtrace_op, + .domain =3D domid, + .u.vmtrace_op =3D { + .cmd =3D XEN_DOMCTL_vmtrace_get_option, + .vcpu =3D vcpu, + .key =3D key, + }, + }; + int rc =3D do_domctl(xch, &domctl); + + if ( !rc ) + *value =3D domctl.u.vmtrace_op.value; + + return rc; +} + +int xc_vmtrace_set_option( + xc_interface *xch, uint32_t domid, uint32_t vcpu, + uint64_t key, uint64_t value) +{ + struct xen_domctl domctl =3D { + .cmd =3D XEN_DOMCTL_vmtrace_op, + .domain =3D domid, + .u.vmtrace_op =3D { + .cmd =3D XEN_DOMCTL_vmtrace_set_option, + .vcpu =3D vcpu, + .key =3D key, + .value =3D value, + }, + }; + + return do_domctl(xch, &domctl); +} --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222090; cv=none; d=zohomail.com; s=zohoarc; b=hiNwllJNl1sFSZ3CZkQUm07aiFTajrelSu3gs5vnOpG8GqsTERLXyL6qCi7ptYNrG/7GyjEGNDPTtFMAqTyg4oQN+Q17JbEnIeDX+g6KeEX3IPzsqx1Fn8LORdTNs8WoRaM9xGMRqHmshJf0a8lQSzQPhBlUne0QU4rzL0uhXHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222090; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=NQ437ZLZzE8cNHqCoIjkB5ihCvJS79v8488SFAegv1E=; b=VSC2TGcWA+pr2+tZNcbpb7cyLGBGmYMGv2I3VmHbzkKOmwHr+gJbzgaQYxZZ7+zUBVEs3vOOqOYoVRoW9co/z6syQ26c+VtlmqjlzDOuk4tJGqjL0li4Rdk24lYAI+03G4Q/UX5T7lua2gJH0gpONcL3old+dgl80NsEgdLg7cE= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222090090945.7258456796253; Mon, 1 Feb 2021 15:28:10 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80197.146590 (Exim 4.92) (envelope-from ) id 1l6ibr-0007wv-Oq; Mon, 01 Feb 2021 23:27:55 +0000 Received: by outflank-mailman (output) from mailman id 80197.146590; Mon, 01 Feb 2021 23:27:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibr-0007wh-Gx; Mon, 01 Feb 2021 23:27:55 +0000 Received: by outflank-mailman (input) for mailman id 80197; Mon, 01 Feb 2021 23:27:53 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibp-0007PB-Sp for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:53 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id c308b7bc-9a82-4428-9e41-2107868277a2; Mon, 01 Feb 2021 23:27:36 +0000 (UTC) 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: c308b7bc-9a82-4428-9e41-2107868277a2 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222056; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=njVGh0BG2zEjGyol+qwxOLZnJzEyLf5QMa4qeRMwOaI=; b=J3Zc47eOJRMQTCgV3iIkux1S4SyWV86QbIvq6euaYaN+crmQntJDYHpX IdB/qGgIAwLdCJaI/CIftkJrnCFVyz5v8Sa9IBljosnh45OlnhgkLBv4L L7y7ZheU0QOpS57/ou9Yrus8yigPi480YvNh0wlp33mAcxQ207jxYBvkH 4=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: SgNfmBguvZHKQHBqSDQjqZXWEQLQaZg26ryWUzmAlwkA4p7i4zv+Ivvf4NSSC0BQ//Zp0QQfZG Et15UDHnHhEAjHmzK+4iAVTf+hbKTX4EFhA+h84fmFgcL+N4FySXpL37FYol1YUzbsrJsfDBnl TlKbVVWjZr6s9gp0PYWL3Mwz4j1OrI6pLxc0bYFv/FqGtKyLpLq3iKnSsICVbBh59iYysXCRZv 0tjVSppl+zZGA6hBdZo1kh7BYFKhzYmCHdOk8ho5VWD3s14FxzwPPPosztVuLDpXWqmW2DdplT yO0= X-SBRS: 5.1 X-MesageID: 36319807 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36319807" From: Andrew Cooper To: Xen-devel CC: =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Andrew Cooper , Ian Jackson , Wei Liu , Tamas K Lengyel Subject: [PATCH v9 08/11] tools/misc: Add xen-vmtrace tool Date: Mon, 1 Feb 2021 23:27:00 +0000 Message-ID: <20210201232703.29275-9-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Micha=C5=82 Leszczy=C5=84ski Add an demonstration tool that uses xc_vmtrace_* calls in order to manage external IPT monitoring for DomU. Signed-off-by: Micha=C5=82 Leszczy=C5=84ski Signed-off-by: Andrew Cooper Acked-by: Ian Jackson --- CC: Ian Jackson CC: Wei Liu CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v9: * Fix truncated data on clean exit v8: * Switch to being a build-only target --- tools/misc/.gitignore | 1 + tools/misc/Makefile | 7 ++ tools/misc/xen-vmtrace.c | 166 +++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 174 insertions(+) create mode 100644 tools/misc/xen-vmtrace.c diff --git a/tools/misc/.gitignore b/tools/misc/.gitignore index b2c3b21f57..ce6f937d0c 100644 --- a/tools/misc/.gitignore +++ b/tools/misc/.gitignore @@ -1,3 +1,4 @@ xen-access xen-memshare xen-ucode +xen-vmtrace diff --git a/tools/misc/Makefile b/tools/misc/Makefile index 912c5d4f0e..2b683819d4 100644 --- a/tools/misc/Makefile +++ b/tools/misc/Makefile @@ -50,6 +50,10 @@ TARGETS_COPY +=3D xenpvnetboot # Everything which needs to be built TARGETS_BUILD :=3D $(filter-out $(TARGETS_COPY),$(TARGETS_ALL)) =20 +# ... including build-only targets +TARGETS_BUILD-$(CONFIG_X86) +=3D xen-vmtrace +TARGETS_BUILD +=3D $(TARGETS_BUILD-y) + .PHONY: all build all build: $(TARGETS_BUILD) =20 @@ -90,6 +94,9 @@ xen-hvmcrash: xen-hvmcrash.o xen-memshare: xen-memshare.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) =20 +xen-vmtrace: xen-vmtrace.o + $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenforeignmemo= ry) $(APPEND_LDFLAGS) + xenperf: xenperf.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) =20 diff --git a/tools/misc/xen-vmtrace.c b/tools/misc/xen-vmtrace.c new file mode 100644 index 0000000000..cc58a0707b --- /dev/null +++ b/tools/misc/xen-vmtrace.c @@ -0,0 +1,166 @@ +/*************************************************************************= ***** + * tools/vmtrace.c + * + * Demonstrative tool for collecting Intel Processor Trace data from Xen. + * Could be used to externally monitor a given vCPU in given DomU. + * + * Copyright (C) 2020 by CERT Polska - NASK PIB + * + * Authors: Micha=C5=82 Leszczy=C5=84ski, michal.leszczynski@cert.pl + * Date: June, 2020 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#define MSR_RTIT_CTL 0x00000570 +#define RTIT_CTL_OS (1 << 2) +#define RTIT_CTL_USR (1 << 3) +#define RTIT_CTL_BRANCH_EN (1 << 13) + +static xc_interface *xch; +static xenforeignmemory_handle *fh; +static uint32_t domid, vcpu; +static size_t size; +static char *buf; + +static sig_atomic_t interrupted =3D 0; +static void int_handler(int signum) +{ + interrupted =3D 1; +} + +static int get_more_data(void) +{ + static uint64_t last_pos; + uint64_t pos; + + if ( xc_vmtrace_output_position(xch, domid, vcpu, &pos) ) + { + perror("xc_vmtrace_output_position()"); + return -1; + } + + if ( pos > last_pos ) + fwrite(buf + last_pos, pos - last_pos, 1, stdout); + else if ( pos < last_pos ) + { + /* buffer wrapped */ + fwrite(buf + last_pos, size - last_pos, 1, stdout); + fwrite(buf, pos, 1, stdout); + } + + last_pos =3D pos; + return 0; +} + +int main(int argc, char **argv) +{ + int rc, exit =3D 1; + xenforeignmemory_resource_handle *fres =3D NULL; + + if ( signal(SIGINT, int_handler) =3D=3D SIG_ERR ) + err(1, "Failed to register signal handler\n"); + + if ( argc !=3D 3 ) + { + fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, "It's recommended to redirect thisprogram's output= to file\n"); + fprintf(stderr, "or to pipe it's output to xxd or other program.\n= "); + return 1; + } + + domid =3D atoi(argv[1]); + vcpu =3D atoi(argv[2]); + + xch =3D xc_interface_open(NULL, NULL, 0); + fh =3D xenforeignmemory_open(NULL, 0); + + if ( !xch ) + err(1, "xc_interface_open()"); + if ( !fh ) + err(1, "xenforeignmemory_open()"); + + rc =3D xenforeignmemory_resource_size( + fh, domid, XENMEM_resource_vmtrace_buf, vcpu, &size); + if ( rc ) + err(1, "xenforeignmemory_resource_size()"); + + fres =3D xenforeignmemory_map_resource( + fh, domid, XENMEM_resource_vmtrace_buf, vcpu, + 0, size >> XC_PAGE_SHIFT, (void **)&buf, PROT_READ, 0); + if ( !fres ) + err(1, "xenforeignmemory_map_resource()"); + + if ( xc_vmtrace_set_option( + xch, domid, vcpu, MSR_RTIT_CTL, + RTIT_CTL_BRANCH_EN | RTIT_CTL_USR | RTIT_CTL_OS) ) + { + perror("xc_vmtrace_set_option()"); + goto out; + } + + if ( xc_vmtrace_enable(xch, domid, vcpu) ) + { + perror("xc_vmtrace_enable()"); + goto out; + } + + while ( !interrupted ) + { + xc_dominfo_t dominfo; + + if ( get_more_data() ) + goto out; + + usleep(1000 * 100); + + if ( xc_domain_getinfo(xch, domid, 1, &dominfo) !=3D 1 || + dominfo.domid !=3D domid || dominfo.shutdown ) + { + if ( get_more_data() ) + goto out; + break; + } + } + + exit =3D 0; + + out: + if ( xc_vmtrace_disable(xch, domid, vcpu) ) + perror("xc_vmtrace_disable()"); + + if ( fres && xenforeignmemory_unmap_resource(fh, fres) ) + perror("xenforeignmemory_unmap_resource()"); + + return exit; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */ --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612222080; cv=none; d=zohomail.com; s=zohoarc; b=hy3vF6eZvs6596KSvxT/XWyOR1CeJbkakB+/uSPtabiGCmxLCoIyTPpbZYoPd7inKM4QGwDrB3oV9Lgi+fKFEKbgp18zrAIxzEdifL6Sog/c3riQJgzl+gfz88BWNPvMidUBnwhqkcUHrShinsKaermiflB8Ijdkk9AvtL8/E5g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612222080; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2gGw3o2aMJZd504hAfObj5UWikMzdiQF5XDlG+P6WJ0=; b=EePYRoYPrpQITJqDDt4/ZHv3MHTDuDEdDylKU9xES1V677QBikxjMpyJlGbOr4dX9xfTXays4VEQhSJ6UN1j0d8wqOXcmAbkBYbibFlSnNoUFGzpj0KuIFghC5XQ/i01Y/KfBMWWNXS+1A9dnnCO13+y4/NiiQ50pEVntX8oFGE= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612222080138581.373519906156; Mon, 1 Feb 2021 15:28:00 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80192.146530 (Exim 4.92) (envelope-from ) id 1l6ibg-0007az-EX; Mon, 01 Feb 2021 23:27:44 +0000 Received: by outflank-mailman (output) from mailman id 80192.146530; Mon, 01 Feb 2021 23:27:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibg-0007ao-AW; Mon, 01 Feb 2021 23:27:44 +0000 Received: by outflank-mailman (input) for mailman id 80192; Mon, 01 Feb 2021 23:27:42 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6ibe-0007P6-Qz for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:27:42 +0000 Received: from esa6.hc3370-68.iphmx.com (unknown [216.71.155.175]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 38cdcfa4-521c-43b8-a188-6a21d06b37d7; Mon, 01 Feb 2021 23:27:32 +0000 (UTC) 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: 38cdcfa4-521c-43b8-a188-6a21d06b37d7 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612222052; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FBfo6N9u3Sukj0MKNcU3Z9nO9VLLQT0Q251N7HMr1t4=; b=W+AMHjrZtM0JdgByJWxIl+Q2o2kC0Hp+Z9c35pgJCUOdxofBHtd98ugt RyZDO/x/pM4EgnPbFfZCGkc55tUQmV4eFGJoOUA6CBObgj8HKNCgP4qOR kcP4/TbGNemXwKraGvRA8t4gwVByqsLzui2w2iE3zM4UD6yoANyCv7Ovu c=; Authentication-Results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: 12MhwBfguQk0bLxFSPpqPdTqs4n6Fxn3l25XKaRAqX09hFa34kLWoantVeiJPyoIOd9c4ScBOK XdoOLk0DmbE9RXIcx5/P8DrSsQjR2Z5G7N8+/5fREE/G/xf2Hs2kZMC7PP0dz0TRULbtnT9GDN UeuaOsfeX6ntwJ9t/j1Sier4gog6YGqKhufTg89OcsascmaJjD595nU4buluHAhcmM/AbDDKaG VEwpqi+7FCZgnV4tL58dn2dChMjNxvo2jrOSElEgx+2fxxPWFzJCgsl0j7HNderxoke4jPvCPH Y+4= X-SBRS: 5.1 X-MesageID: 36523028 X-Ironport-Server: esa6.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36523028" From: Andrew Cooper To: Xen-devel CC: Tamas K Lengyel , Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Jun Nakajima , Kevin Tian , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Tamas K Lengyel Subject: [PATCH v9 09/11] xen/vmtrace: support for VM forks Date: Mon, 1 Feb 2021 23:27:01 +0000 Message-ID: <20210201232703.29275-10-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Tamas K Lengyel Implement vmtrace_reset_pt function. Properly set IPT state for VM forks. Signed-off-by: Tamas K Lengyel Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu CC: Jun Nakajima CC: Kevin Tian CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v7: * New --- tools/misc/xen-vmtrace.c | 2 +- xen/arch/x86/hvm/vmx/vmx.c | 11 +++++++++++ xen/arch/x86/mm/mem_sharing.c | 3 +++ xen/include/asm-x86/hvm/hvm.h | 9 +++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/misc/xen-vmtrace.c b/tools/misc/xen-vmtrace.c index cc58a0707b..7572e880c5 100644 --- a/tools/misc/xen-vmtrace.c +++ b/tools/misc/xen-vmtrace.c @@ -43,7 +43,7 @@ static uint32_t domid, vcpu; static size_t size; static char *buf; =20 -static sig_atomic_t interrupted =3D 0; +static sig_atomic_t interrupted; static void int_handler(int signum) { interrupted =3D 1; diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index beb5692b8b..faba95d057 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -2392,6 +2392,16 @@ static int vmtrace_output_position(struct vcpu *v, u= int64_t *pos) return v->arch.hvm.vmx.ipt_active; } =20 +static int vmtrace_reset(struct vcpu *v) +{ + if ( !v->arch.hvm.vmx.ipt_active ) + return -EINVAL; + + v->arch.msrs->rtit.output_offset =3D 0; + v->arch.msrs->rtit.status =3D 0; + return 0; +} + static struct hvm_function_table __initdata vmx_function_table =3D { .name =3D "VMX", .cpu_up_prepare =3D vmx_cpu_up_prepare, @@ -2451,6 +2461,7 @@ static struct hvm_function_table __initdata vmx_funct= ion_table =3D { .vmtrace_output_position =3D vmtrace_output_position, .vmtrace_set_option =3D vmtrace_set_option, .vmtrace_get_option =3D vmtrace_get_option, + .vmtrace_reset =3D vmtrace_reset, .tsc_scaling =3D { .max_ratio =3D VMX_TSC_MULTIPLIER_MAX, }, diff --git a/xen/arch/x86/mm/mem_sharing.c b/xen/arch/x86/mm/mem_sharing.c index adaeab4612..00ada05c10 100644 --- a/xen/arch/x86/mm/mem_sharing.c +++ b/xen/arch/x86/mm/mem_sharing.c @@ -1632,6 +1632,8 @@ static int copy_vcpu_settings(struct domain *cd, cons= t struct domain *d) copy_domain_page(new_vcpu_info_mfn, vcpu_info_mfn); } =20 + hvm_vmtrace_reset(cd_vcpu); + /* * TODO: to support VMs with PV interfaces copy additional * settings here, such as PV timers. @@ -1782,6 +1784,7 @@ static int fork(struct domain *cd, struct domain *d) cd->max_pages =3D d->max_pages; *cd->arch.cpuid =3D *d->arch.cpuid; *cd->arch.msr =3D *d->arch.msr; + cd->vmtrace_size =3D d->vmtrace_size; cd->parent =3D d; } =20 diff --git a/xen/include/asm-x86/hvm/hvm.h b/xen/include/asm-x86/hvm/hvm.h index 960ec03917..150746de66 100644 --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -219,6 +219,7 @@ struct hvm_function_table { int (*vmtrace_output_position)(struct vcpu *v, uint64_t *pos); int (*vmtrace_set_option)(struct vcpu *v, uint64_t key, uint64_t value= ); int (*vmtrace_get_option)(struct vcpu *v, uint64_t key, uint64_t *valu= e); + int (*vmtrace_reset)(struct vcpu *v); =20 /* * Parameters and callbacks for hardware-assisted TSC scaling, @@ -696,6 +697,14 @@ static inline int hvm_vmtrace_get_option( return -EOPNOTSUPP; } =20 +static inline int hvm_vmtrace_reset(struct vcpu *v) +{ + if ( hvm_funcs.vmtrace_reset ) + return hvm_funcs.vmtrace_reset(v); + + return -EOPNOTSUPP; +} + /* * This must be defined as a macro instead of an inline function, * because it uses 'struct vcpu' and 'struct domain' which have --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612223574; cv=none; d=zohomail.com; s=zohoarc; b=BFYRNnFk7caC6bHsaI2zGgFrqX8CP0QomYxb2WgK/K6IMZLu1zlDgeNZLonJD+sGQOJ3OS4pe6/ZnrJrY6M/2LUNblUzbMIadvu6s0YfJXNxMY695JmUG9JVvKQA9U/3pQr1kNcsxojm9pImCdvXiZePBl4TbjTU/X3bI4LGqY0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612223574; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Yk9Y7xMAdipcTwS30PsKv7uOUnXJocjE+r0o59RofzE=; b=ZNbHDbWvyY6ARnvqbJvuOOscdKhGqQSUUe9YgNDy3vLWQ7lCaso9tsbMtXjFD97aPdXcgS+nT53Dd7TrEPxr+OfVz8EAZ+JB0/r61L1uFVaAra9c8PazF0Nzld/kWWkHYuf5qQrs57661d1dDN++pT+HXnjkbb/tY+HzFws4IyE= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612223574269584.8650079759847; Mon, 1 Feb 2021 15:52:54 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80222.146638 (Exim 4.92) (envelope-from ) id 1l6izk-00035E-ME; Mon, 01 Feb 2021 23:52:36 +0000 Received: by outflank-mailman (output) from mailman id 80222.146638; Mon, 01 Feb 2021 23:52:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6izk-000357-IP; Mon, 01 Feb 2021 23:52:36 +0000 Received: by outflank-mailman (input) for mailman id 80222; Mon, 01 Feb 2021 23:52:34 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6izi-00033V-Os for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:52:34 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 7098a47b-7da7-49cb-a644-40ccc243acb2; Mon, 01 Feb 2021 23:52:29 +0000 (UTC) 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: 7098a47b-7da7-49cb-a644-40ccc243acb2 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612223549; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FaRlj2zjPcVI+8D5Vii4wydz4lnh2fPaipEPLCTPTI0=; b=SZjlb4NuwRAMCtg82JY9sAJv2tOki2WqnQUHWovDr2ooY3Ft0PTDOSQQ COwLrqjkSslcangajo0ZfJ7X8p12lcrgYDl/2cxv1jw0AtKV1wpKOQ3YG bte92Ap7myYnCkskdApBzthBgj1WPle0fv3ROrvZX4D6KKQCewtjyM3N8 4=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: VGG41gzWPGgR1D9d6Nqyc5pFf0yGaeZWBxJlOgIReLPjTnRPrebnQEN95TkhRZyBAujKjfDekZ YzR7jscQ4lQCpafFPq03Kf8N17oWuQskY9kytiNqWjA4Mao4Fufa4p6VULuaK/2I3XzMUB6l/Q +ozcBcfxVVjVIstbc/gqrhgmsQiWuZ92j5vJPITm10QwRJELJ1iDZlLUnKn6iZ518xad3Eh59P ttUhk37grwqnotT97cEkWRuYKGh60sNigdU/BnWUA15/8ZctB0k6yKTU514RXRbLuKPAqlkxwG DdE= X-SBRS: 5.1 X-MesageID: 36320760 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36320760" From: Andrew Cooper To: Xen-devel CC: Tamas K Lengyel , Andrew Cooper , Jan Beulich , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , Jun Nakajima , Kevin Tian , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Tamas K Lengyel Subject: [PATCH v9 10/11] x86/vm_event: Carry the vmtrace buffer position in vm_event Date: Mon, 1 Feb 2021 23:27:02 +0000 Message-ID: <20210201232703.29275-11-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) From: Tamas K Lengyel Add vmtrace_pos field to x86 regs in vm_event. Initialized to ~0 if vmtrace is not in use. Signed-off-by: Tamas K Lengyel Signed-off-by: Andrew Cooper Reviewed-by: Jan Beulich --- CC: Jan Beulich CC: Roger Pau Monn=C3=A9 CC: Wei Liu CC: Jun Nakajima CC: Kevin Tian CC: Micha=C5=82 Leszczy=C5=84ski CC: Tamas K Lengyel v8: * Use 'vmtrace' consistently. v7: * New --- xen/arch/x86/vm_event.c | 3 +++ xen/include/public/vm_event.h | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/xen/arch/x86/vm_event.c b/xen/arch/x86/vm_event.c index 848d69c1b0..36272e9316 100644 --- a/xen/arch/x86/vm_event.c +++ b/xen/arch/x86/vm_event.c @@ -251,6 +251,9 @@ void vm_event_fill_regs(vm_event_request_t *req) =20 req->data.regs.x86.shadow_gs =3D ctxt.shadow_gs; req->data.regs.x86.dr6 =3D ctxt.dr6; + + if ( hvm_vmtrace_output_position(curr, &req->data.regs.x86.vmtrace_pos= ) !=3D 1 ) + req->data.regs.x86.vmtrace_pos =3D ~0; #endif } =20 diff --git a/xen/include/public/vm_event.h b/xen/include/public/vm_event.h index 141ea024a3..147dc3ea73 100644 --- a/xen/include/public/vm_event.h +++ b/xen/include/public/vm_event.h @@ -223,6 +223,13 @@ struct vm_event_regs_x86 { */ uint64_t npt_base; =20 + /* + * Current position in the vmtrace buffer, or ~0 if vmtrace is not act= ive. + * + * For Intel Processor Trace, it is the upper half of MSR_RTIT_OUTPUT_= MASK. + */ + uint64_t vmtrace_pos; + uint32_t cs_base; uint32_t ss_base; uint32_t ds_base; --=20 2.11.0 From nobody Fri May 3 04:49:59 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=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1612223572; cv=none; d=zohomail.com; s=zohoarc; b=NybczqWxv2lTl3YOMmweuIg7caHU6B0tvG4GxFz0owTpa/APH9/jaAwSTotED3a9aQ6PhzsuTH1CoHlUjRkY7aPOct2GQiSBowePCcpkmR9aX/NYSLxRREZ45oZFXPxg4tBTCQggFBcOl53gJ1rqhWpvV+25n2JWEtN8CYUg5Yc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612223572; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=jmwYWx67jnwa2obXGVMV1T5F3jyb/o6P9CRB/g8BcJE=; b=MPerPhU79AI2sbHhtWqm9fOJQuyayy8K82YE6C+OaY1nRkkAxjnkaJFrsOdk26ct1XHy035di3ao3wqbVA8T2MBQCQHwOfuI3fcfLsNuXaDnTAav3KKJVCYqeaR9xWIg0KyK2xpg50t06LQYyMo5hYkYIHilQ2aEsltLH+NG8YE= 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=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1612223572933523.0936961363811; Mon, 1 Feb 2021 15:52:52 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.80221.146625 (Exim 4.92) (envelope-from ) id 1l6izf-00033h-Dg; Mon, 01 Feb 2021 23:52:31 +0000 Received: by outflank-mailman (output) from mailman id 80221.146625; Mon, 01 Feb 2021 23:52:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6izf-00033a-AO; Mon, 01 Feb 2021 23:52:31 +0000 Received: by outflank-mailman (input) for mailman id 80221; Mon, 01 Feb 2021 23:52:30 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l6izd-00033V-UV for xen-devel@lists.xenproject.org; Mon, 01 Feb 2021 23:52:29 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 7c1e5e0b-07ee-49dd-ac0c-ef390b2db081; Mon, 01 Feb 2021 23:52:27 +0000 (UTC) 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: 7c1e5e0b-07ee-49dd-ac0c-ef390b2db081 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1612223547; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=E5JUPU1j2pT/veM8SWF/+k6jtm1Vy8PzPBbksm2vA/E=; b=O6IMRZtqj7HX/MQqtncNB9oTXGohww3OwNZqSf0m5lT2KpyrCYAFot/H U68ResoE33UL6dzR1TEM3rumW2TQaN0bi0LF6IzGq9SQzicsSwCUGzOyz f9B09zAV3CqnqSp89MZrnap0VEtwxz6bKPmfEunwjCH9DqVx9BnKkgRgw g=; Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: 47SmiOIP1y9uF8049fSXl693rHNEwXfXK3cqeRsTSw1Sgj369cMxOTtxcf4mvOSU3MeW8dhel+ fpgs/MMEY38E0ReJm8aqZ0TESzIYiNji4o3AJoKTXbRq1I5w4sCnPau4RISHaYRArAyWwUNkj1 JRf1mZyMPraBr16a8A8Dy91DcYmvcx4YOJyNhmw2Tn7QvWF7V8NVmp9hNP1JcSJtSgKh6vK6f8 EzMZ8NCs5weUZGuFLHVZo6K58Z5RjFw2MEQ9Y0G08431PB5z3jLKNc3DN6rGdzxn7oEVSXModI lDM= X-SBRS: 5.1 X-MesageID: 36363241 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,393,1602561600"; d="scan'208";a="36363241" From: Andrew Cooper To: Xen-devel CC: Tamas K Lengyel Subject: [PATCH v9 11/11] x86/vm_event: add response flag to reset vmtrace buffer Date: Mon, 1 Feb 2021 23:27:03 +0000 Message-ID: <20210201232703.29275-12-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210201232703.29275-1-andrew.cooper3@citrix.com> References: <20210201232703.29275-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 X-ZohoMail-DKIM: pass (identity @citrix.com) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Tamas K Lengyel Allow resetting the vmtrace buffer in response to a vm_event. This can be u= sed to optimize a use-case where detecting a looped vmtrace buffer is important. Signed-off-by: Tamas K Lengyel Acked-by: Andrew Cooper --- xen/arch/x86/vm_event.c | 7 +++++++ xen/common/vm_event.c | 3 +++ xen/include/asm-arm/vm_event.h | 6 ++++++ xen/include/asm-x86/vm_event.h | 2 ++ xen/include/public/vm_event.h | 4 ++++ 5 files changed, 22 insertions(+) diff --git a/xen/arch/x86/vm_event.c b/xen/arch/x86/vm_event.c index 36272e9316..8f73a73e2e 100644 --- a/xen/arch/x86/vm_event.c +++ b/xen/arch/x86/vm_event.c @@ -300,6 +300,13 @@ void vm_event_emulate_check(struct vcpu *v, vm_event_r= esponse_t *rsp) }; } =20 +void vm_event_reset_vmtrace(struct vcpu *v) +{ +#ifdef CONFIG_HVM + hvm_vmtrace_reset(v); +#endif +} + /* * Local variables: * mode: C diff --git a/xen/common/vm_event.c b/xen/common/vm_event.c index 127f2d58f1..44d542f23e 100644 --- a/xen/common/vm_event.c +++ b/xen/common/vm_event.c @@ -424,6 +424,9 @@ static int vm_event_resume(struct domain *d, struct vm_= event_domain *ved) if ( rsp.flags & VM_EVENT_FLAG_GET_NEXT_INTERRUPT ) vm_event_monitor_next_interrupt(v); =20 + if ( rsp.flags & VM_EVENT_FLAG_RESET_VMTRACE ) + vm_event_reset_vmtrace(v); + if ( rsp.flags & VM_EVENT_FLAG_VCPU_PAUSED ) vm_event_vcpu_unpause(v); } diff --git a/xen/include/asm-arm/vm_event.h b/xen/include/asm-arm/vm_event.h index 14d1d341cc..abe7db1970 100644 --- a/xen/include/asm-arm/vm_event.h +++ b/xen/include/asm-arm/vm_event.h @@ -58,4 +58,10 @@ void vm_event_sync_event(struct vcpu *v, bool value) /* Not supported on ARM. */ } =20 +static inline +void vm_event_reset_vmtrace(struct vcpu *v) +{ + /* Not supported on ARM. */ +} + #endif /* __ASM_ARM_VM_EVENT_H__ */ diff --git a/xen/include/asm-x86/vm_event.h b/xen/include/asm-x86/vm_event.h index 785e741fba..0756124075 100644 --- a/xen/include/asm-x86/vm_event.h +++ b/xen/include/asm-x86/vm_event.h @@ -54,4 +54,6 @@ void vm_event_emulate_check(struct vcpu *v, vm_event_resp= onse_t *rsp); =20 void vm_event_sync_event(struct vcpu *v, bool value); =20 +void vm_event_reset_vmtrace(struct vcpu *v); + #endif /* __ASM_X86_VM_EVENT_H__ */ diff --git a/xen/include/public/vm_event.h b/xen/include/public/vm_event.h index 147dc3ea73..36135ba4f1 100644 --- a/xen/include/public/vm_event.h +++ b/xen/include/public/vm_event.h @@ -123,6 +123,10 @@ * Set if the event comes from a nested VM and thus npt_base is valid. */ #define VM_EVENT_FLAG_NESTED_P2M (1 << 12) +/* + * Reset the vmtrace buffer (if vmtrace is enabled) + */ +#define VM_EVENT_FLAG_RESET_VMTRACE (1 << 13) =20 /* * Reasons for the vm event request --=20 2.11.0