From patchwork Tue Jul 7 19:39:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649917 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 037866C1 for ; Tue, 7 Jul 2020 19:41:14 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DD440206BE for ; Tue, 7 Jul 2020 19:41:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DD440206BE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSI-0007bq-8P; Tue, 07 Jul 2020 19:40: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 1jstSH-0007ao-VE for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:40:37 +0000 X-Inumbo-ID: b7c80ff8-c089-11ea-bca7-bc764e2007e4 Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id b7c80ff8-c089-11ea-bca7-bc764e2007e4; Tue, 07 Jul 2020 19:40:32 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 0B140A2660; Tue, 7 Jul 2020 21:40:31 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id ECE7EA2657; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id qQ7tpZAsGLBM; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 637FBA264E; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 9XQj3sShKaPE; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 2F4DBA2489; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 0E1D12245A; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id QEW3aQAhvDPS; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 64D2522383; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) X-Quarantine-ID: <8m8VZJ10Geso> X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 8m8VZJ10Geso; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id 3049C222A1; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 01/11] memory: batch processing in acquire_resource() Date: Tue, 7 Jul 2020 21:39:40 +0200 Message-Id: <02415890e4e8211513b495228c790e1d16de767f.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Julien Grall , Stefano Stabellini , luwei.kang@intel.com, Wei Liu , Andrew Cooper , Michal Leszczynski , Ian Jackson , George Dunlap , Jan Beulich , tamas.lengyel@intel.com Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Allow to acquire large resources by allowing acquire_resource() to process items in batches, using hypercall continuation. Be aware that this modifies the behavior of acquire_resource call with frame_list=NULL. While previously it would return the size of internal array (32), with this patch it returns the maximal quantity of frames that could be requested at once, i.e. UINT_MAX >> MEMOP_EXTENT_SHIFT. Signed-off-by: Michal Leszczynski --- xen/common/memory.c | 49 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/xen/common/memory.c b/xen/common/memory.c index 714077c1e5..eb42f883df 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1046,10 +1046,12 @@ static int acquire_grant_table(struct domain *d, unsigned int id, } 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 = current->domain; xen_mem_acquire_resource_t xmar; + uint32_t total_frames; /* * 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 @@ -1069,7 +1071,7 @@ static int acquire_resource( if ( xmar.nr_frames ) return -EINVAL; - xmar.nr_frames = ARRAY_SIZE(mfn_list); + xmar.nr_frames = UINT_MAX >> MEMOP_EXTENT_SHIFT; if ( __copy_field_to_guest(arg, &xmar, nr_frames) ) return -EFAULT; @@ -1077,8 +1079,28 @@ static int acquire_resource( return 0; } + total_frames = xmar.nr_frames; + + /* Is the size too large for us to encode a continuation? */ + if ( unlikely(xmar.nr_frames > (UINT_MAX >> MEMOP_EXTENT_SHIFT)) ) + return -EINVAL; + + if ( *start_extent ) + { + /* + * Check whether start_extent is in bounds, as this + * value if visible to the calling domain. + */ + if ( *start_extent > xmar.nr_frames ) + return -EINVAL; + + xmar.frame += *start_extent; + xmar.nr_frames -= *start_extent; + guest_handle_add_offset(xmar.frame_list, *start_extent); + } + if ( xmar.nr_frames > ARRAY_SIZE(mfn_list) ) - return -E2BIG; + xmar.nr_frames = ARRAY_SIZE(mfn_list); rc = rcu_lock_remote_domain_by_id(xmar.domid, &d); if ( rc ) @@ -1135,6 +1157,14 @@ static int acquire_resource( } } + if ( !rc ) + { + *start_extent += xmar.nr_frames; + + if ( *start_extent != total_frames ) + rc = -ERESTART; + } + out: rcu_unlock_domain(d); @@ -1599,8 +1629,17 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) #endif case XENMEM_acquire_resource: - rc = acquire_resource( - guest_handle_cast(arg, xen_mem_acquire_resource_t)); + do { + rc = acquire_resource( + guest_handle_cast(arg, xen_mem_acquire_resource_t), + &start_extent); + + if ( hypercall_preempt_check() ) + return hypercall_create_continuation( + __HYPERVISOR_memory_op, "lh", + op | (start_extent << MEMOP_EXTENT_SHIFT), arg); + } while ( rc == -ERESTART ); + break; default: From patchwork Tue Jul 7 19:39:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649927 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9BC316C1 for ; Tue, 7 Jul 2020 19:41:52 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8183C206CD for ; Tue, 7 Jul 2020 19:41:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8183C206CD Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSK-0007d9-No; Tue, 07 Jul 2020 19:40:40 +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 1jstSJ-0007at-8A for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:40:39 +0000 X-Inumbo-ID: b7ba091c-c089-11ea-8de3-12813bfff9fa Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id b7ba091c-c089-11ea-8de3-12813bfff9fa; Tue, 07 Jul 2020 19:40:32 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id EEF6DA265A; Tue, 7 Jul 2020 21:40:30 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id E020EA268B; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id PinkwRJevqWr; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 66F75A2657; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id hshjaMDDlq4C; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 35734A261F; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 12F762245C; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id rQdVHyLH4fzM; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 70EEE2241B; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) X-Quarantine-ID: <2pmxzWWtxK_F> X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 2pmxzWWtxK_F; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id 45657222A3; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 02/11] x86/vmx: add Intel PT MSR definitions Date: Tue, 7 Jul 2020 21:39:41 +0200 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: luwei.kang@intel.com, Wei Liu , Andrew Cooper , Michal Leszczynski , Jan Beulich , tamas.lengyel@intel.com, =?utf-8?q?Roger_P?= =?utf-8?q?au_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Define constants related to Intel Processor Trace features. Signed-off-by: Michal Leszczynski Acked-by: Andrew Cooper --- xen/include/asm-x86/msr-index.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/xen/include/asm-x86/msr-index.h b/xen/include/asm-x86/msr-index.h index 0fe98af923..4fd54fb5c9 100644 --- a/xen/include/asm-x86/msr-index.h +++ b/xen/include/asm-x86/msr-index.h @@ -72,7 +72,31 @@ #define MSR_RTIT_OUTPUT_BASE 0x00000560 #define MSR_RTIT_OUTPUT_MASK 0x00000561 #define MSR_RTIT_CTL 0x00000570 +#define RTIT_CTL_TRACE_EN (_AC(1, ULL) << 0) +#define RTIT_CTL_CYC_EN (_AC(1, ULL) << 1) +#define RTIT_CTL_OS (_AC(1, ULL) << 2) +#define RTIT_CTL_USR (_AC(1, ULL) << 3) +#define RTIT_CTL_PWR_EVT_EN (_AC(1, ULL) << 4) +#define RTIT_CTL_FUP_ON_PTW (_AC(1, ULL) << 5) +#define RTIT_CTL_FABRIC_EN (_AC(1, ULL) << 6) +#define RTIT_CTL_CR3_FILTER (_AC(1, ULL) << 7) +#define RTIT_CTL_TOPA (_AC(1, ULL) << 8) +#define RTIT_CTL_MTC_EN (_AC(1, ULL) << 9) +#define RTIT_CTL_TSC_EN (_AC(1, ULL) << 10) +#define RTIT_CTL_DIS_RETC (_AC(1, ULL) << 11) +#define RTIT_CTL_PTW_EN (_AC(1, ULL) << 12) +#define RTIT_CTL_BRANCH_EN (_AC(1, ULL) << 13) +#define RTIT_CTL_MTC_FREQ (_AC(0xf, ULL) << 14) +#define RTIT_CTL_CYC_THRESH (_AC(0xf, ULL) << 19) +#define RTIT_CTL_PSB_FREQ (_AC(0xf, ULL) << 24) +#define RTIT_CTL_ADDR(n) (_AC(0xf, ULL) << (32 + 4 * (n))) #define MSR_RTIT_STATUS 0x00000571 +#define RTIT_STATUS_FILTER_EN (_AC(1, ULL) << 0) +#define RTIT_STATUS_CONTEXT_EN (_AC(1, ULL) << 1) +#define RTIT_STATUS_TRIGGER_EN (_AC(1, ULL) << 2) +#define RTIT_STATUS_ERROR (_AC(1, ULL) << 4) +#define RTIT_STATUS_STOPPED (_AC(1, ULL) << 5) +#define RTIT_STATUS_BYTECNT (_AC(0x1ffff, ULL) << 32) #define MSR_RTIT_CR3_MATCH 0x00000572 #define MSR_RTIT_ADDR_A(n) (0x00000580 + (n) * 2) #define MSR_RTIT_ADDR_B(n) (0x00000581 + (n) * 2) From patchwork Tue Jul 7 19:39:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649911 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1695B13B6 for ; Tue, 7 Jul 2020 19:41:07 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EBCCA206CD for ; Tue, 7 Jul 2020 19:41:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EBCCA206CD Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSG-0007b4-0G; Tue, 07 Jul 2020 19:40:36 +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 1jstSE-0007at-FX for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:40:34 +0000 X-Inumbo-ID: b7bf1741-c089-11ea-8de3-12813bfff9fa Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id b7bf1741-c089-11ea-8de3-12813bfff9fa; Tue, 07 Jul 2020 19:40:32 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 5FCBEA268B; Tue, 7 Jul 2020 21:40:31 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 5882EA264E; Tue, 7 Jul 2020 21:40:30 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 5VbTMOSLIUg9; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id B98B3A2660; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id g6H3xNpPY_Yw; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 7A071A265E; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 6232222477; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id HGbXudXT8yoR; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 9964922427; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id gZ2uXSNp0qxh; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id 510622236E; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 03/11] x86/vmx: add IPT cpu feature Date: Tue, 7 Jul 2020 21:39:42 +0200 Message-Id: <4d6eac657d082efaa0e7d141b5c9a07791b31f94.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Julien Grall , Kevin Tian , Stefano Stabellini , luwei.kang@intel.com, Jun Nakajima , Wei Liu , Andrew Cooper , Michal Leszczynski , Ian Jackson , George Dunlap , Jan Beulich , tamas.lengyel@intel.com, =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Check if Intel Processor Trace feature is supported by current processor. Define vmtrace_supported global variable. Signed-off-by: Michal Leszczynski Reviewed-by: Roger Pau Monné --- xen/arch/x86/hvm/vmx/vmcs.c | 15 ++++++++++++++- xen/common/domain.c | 2 ++ xen/include/asm-x86/cpufeature.h | 1 + xen/include/asm-x86/hvm/vmx/vmcs.h | 1 + xen/include/public/arch-x86/cpufeatureset.h | 1 + xen/include/xen/domain.h | 2 ++ 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/xen/arch/x86/hvm/vmx/vmcs.c b/xen/arch/x86/hvm/vmx/vmcs.c index ca94c2bedc..3a53553f10 100644 --- a/xen/arch/x86/hvm/vmx/vmcs.c +++ b/xen/arch/x86/hvm/vmx/vmcs.c @@ -291,6 +291,20 @@ static int vmx_init_vmcs_config(void) _vmx_cpu_based_exec_control &= ~(CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING); + rdmsrl(MSR_IA32_VMX_MISC, _vmx_misc_cap); + + /* Check whether IPT is supported in VMX operation. */ + if ( !smp_processor_id() ) + vmtrace_supported = cpu_has_ipt && + (_vmx_misc_cap & VMX_MISC_PROC_TRACE); + else if ( vmtrace_supported && + !(_vmx_misc_cap & VMX_MISC_PROC_TRACE) ) + { + printk("VMX: IPT capabilities fatally differ between CPU%u and CPU0\n", + smp_processor_id()); + return -EINVAL; + } + if ( _vmx_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS ) { min = 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 |= SECONDARY_EXEC_ENABLE_VMCS_SHADOWING; if ( opt_vpid_enabled ) diff --git a/xen/common/domain.c b/xen/common/domain.c index 7cc9526139..a45cf023f7 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -82,6 +82,8 @@ struct vcpu *idle_vcpu[NR_CPUS] __read_mostly; vcpu_info_t dummy_vcpu_info; +bool vmtrace_supported __read_mostly; + static void __domain_finalise_shutdown(struct domain *d) { struct vcpu *v; diff --git a/xen/include/asm-x86/cpufeature.h b/xen/include/asm-x86/cpufeature.h index f790d5c1f8..555f696a26 100644 --- a/xen/include/asm-x86/cpufeature.h +++ b/xen/include/asm-x86/cpufeature.h @@ -104,6 +104,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_ipt 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/vmx/vmcs.h index 906810592f..6153ba6769 100644 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -283,6 +283,7 @@ extern u32 vmx_secondary_exec_control; #define VMX_VPID_INVVPID_SINGLE_CONTEXT_RETAINING_GLOBAL 0x80000000000ULL extern u64 vmx_ept_vpid_cap; +#define VMX_MISC_PROC_TRACE 0x00004000 #define VMX_MISC_CR3_TARGET 0x01ff0000 #define VMX_MISC_VMWRITE_ALL 0x20000000 diff --git a/xen/include/public/arch-x86/cpufeatureset.h b/xen/include/public/arch-x86/cpufeatureset.h index fe7492a225..2c91862f2d 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 Tracing feature */ 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 Instrs */ diff --git a/xen/include/xen/domain.h b/xen/include/xen/domain.h index 7e51d361de..61ebc6c24d 100644 --- a/xen/include/xen/domain.h +++ b/xen/include/xen/domain.h @@ -130,4 +130,6 @@ struct vnuma_info { void vnuma_destroy(struct vnuma_info *vnuma); +extern bool vmtrace_supported; + #endif /* __XEN_DOMAIN_H__ */ From patchwork Tue Jul 7 19:39:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649915 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6474813B6 for ; Tue, 7 Jul 2020 19:41:13 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4A091206BE for ; Tue, 7 Jul 2020 19:41:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4A091206BE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSE-0007ay-OM; Tue, 07 Jul 2020 19:40: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 1jstSD-0007ao-01 for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:40:33 +0000 X-Inumbo-ID: b7d032be-c089-11ea-bb8b-bc764e2007e4 Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id b7d032be-c089-11ea-bb8b-bc764e2007e4; Tue, 07 Jul 2020 19:40:32 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 0E816A2675; Tue, 7 Jul 2020 21:40:31 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 06A92A2691; Tue, 7 Jul 2020 21:40:30 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id fcq4hya3Rxz7; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 8CFE0A261F; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id SD8nyO5q28Qn; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 5986AA2647; Tue, 7 Jul 2020 21:40:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 3AE5A22383; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id kvb5TwRL8e28; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id ACE342242F; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) X-Quarantine-ID: <8p2776uf7EM3> X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 8p2776uf7EM3; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id 6C40922303; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 04/11] common: add vmtrace_pt_size domain parameter Date: Tue, 7 Jul 2020 21:39:43 +0200 Message-Id: <036bc768bfb074269d9bd4530304a11170b7142d.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Julien Grall , Stefano Stabellini , luwei.kang@intel.com, Wei Liu , Andrew Cooper , Michal Leszczynski , Ian Jackson , George Dunlap , Jan Beulich , tamas.lengyel@intel.com, =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Add vmtrace_pt_size domain parameter in live domain and vmtrace_pt_order parameter in xen_domctl_createdomain. Signed-off-by: Michal Leszczynski --- xen/arch/x86/domain.c | 6 ++++++ xen/common/domain.c | 9 +++++++++ xen/include/public/domctl.h | 1 + xen/include/xen/sched.h | 3 +++ 4 files changed, 19 insertions(+) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index fee6c3931a..b75017b28b 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -499,6 +499,12 @@ int arch_sanitise_domain_config(struct xen_domctl_createdomain *config) */ config->flags |= XEN_DOMCTL_CDF_oos_off; + if ( !hvm && config->processor_trace_buf_kb ) + { + dprintk(XENLOG_INFO, "Processor trace is not supported on non-HVM\n"); + return -EINVAL; + } + return 0; } diff --git a/xen/common/domain.c b/xen/common/domain.c index a45cf023f7..e6e8f88da1 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -338,6 +338,12 @@ static int sanitise_domain_config(struct xen_domctl_createdomain *config) return -EINVAL; } + if ( config->processor_trace_buf_kb && !vmtrace_supported ) + { + dprintk(XENLOG_INFO, "Processor tracing is not supported\n"); + return -EINVAL; + } + return arch_sanitise_domain_config(config); } @@ -443,6 +449,9 @@ struct domain *domain_create(domid_t domid, d->nr_pirqs = min(d->nr_pirqs, nr_irqs); radix_tree_init(&d->pirq_tree); + + if ( config->processor_trace_buf_kb ) + d->processor_trace_buf_kb = config->processor_trace_buf_kb; } if ( (err = arch_domain_create(d, config)) != 0 ) diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 59bdc28c89..7681675a94 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -92,6 +92,7 @@ struct xen_domctl_createdomain { uint32_t max_evtchn_port; int32_t max_grant_frames; int32_t max_maptrack_frames; + uint32_t processor_trace_buf_kb; struct xen_arch_domainconfig arch; }; diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index ac53519d7f..c046e59886 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -457,6 +457,9 @@ struct domain unsigned pbuf_idx; spinlock_t pbuf_lock; + /* Used by vmtrace features */ + uint32_t processor_trace_buf_kb; + /* OProfile support. */ struct xenoprof *xenoprof; From patchwork Tue Jul 7 19:39:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649919 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 400C56C1 for ; Tue, 7 Jul 2020 19:41:38 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2531E206BE for ; Tue, 7 Jul 2020 19:41:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2531E206BE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSu-0007xX-0E; Tue, 07 Jul 2020 19:41:16 +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 1jstSt-0007no-0j for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:41:15 +0000 X-Inumbo-ID: c9b27082-c089-11ea-8de3-12813bfff9fa Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id c9b27082-c089-11ea-8de3-12813bfff9fa; Tue, 07 Jul 2020 19:41:02 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 2FD7FA2675; Tue, 7 Jul 2020 21:41:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 2BA35A26A3; Tue, 7 Jul 2020 21:41:00 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id xobwEcbRVpKw; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 76F41A269B; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id EX9po0wo_flD; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 534B2A2657; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id E9C0322303; Tue, 7 Jul 2020 21:40:04 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id Zc-6faRX1ri1; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id BA90C2243D; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id vL0WSPJJQuxu; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id 857A32237F; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 05/11] tools/libxl: add vmtrace_pt_size parameter Date: Tue, 7 Jul 2020 21:39:44 +0200 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: luwei.kang@intel.com, Wei Liu , Michal Leszczynski , Ian Jackson , George Dunlap , Anthony PERARD , tamas.lengyel@intel.com Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Allow to specify the size of per-vCPU trace buffer upon domain creation. This is zero by default (meaning: not enabled). Signed-off-by: Michal Leszczynski --- docs/man/xl.cfg.5.pod.in | 13 +++++++++++++ tools/golang/xenlight/helpers.gen.go | 2 ++ tools/golang/xenlight/types.gen.go | 1 + tools/libxl/libxl.h | 8 ++++++++ tools/libxl/libxl_create.c | 1 + tools/libxl/libxl_types.idl | 4 ++++ tools/xl/xl_parse.c | 22 ++++++++++++++++++++++ 7 files changed, 51 insertions(+) diff --git a/docs/man/xl.cfg.5.pod.in b/docs/man/xl.cfg.5.pod.in index 0532739c1f..ddef9b6014 100644 --- a/docs/man/xl.cfg.5.pod.in +++ b/docs/man/xl.cfg.5.pod.in @@ -683,6 +683,19 @@ If this option is not specified then it will default to B. =back +=item B + +Specifies the size of processor trace buffer that would be allocated +for each vCPU belonging to this domain. Disabled (i.e. +B by default. This must be set to +non-zero value in order to be able to use processor tracing features +with this domain. + +B: In order to use Intel Processor Trace feature, this value +must be between 8 kB and 4 GB and it must be a power of 2. + +=back + =head2 Devices The following options define the paravirtual, emulated and physical diff --git a/tools/golang/xenlight/helpers.gen.go b/tools/golang/xenlight/helpers.gen.go index 152c7e8e6b..3ce6f2374b 100644 --- a/tools/golang/xenlight/helpers.gen.go +++ b/tools/golang/xenlight/helpers.gen.go @@ -1117,6 +1117,7 @@ return fmt.Errorf("invalid union key '%v'", x.Type)} x.ArchArm.GicVersion = GicVersion(xc.arch_arm.gic_version) x.ArchArm.Vuart = VuartType(xc.arch_arm.vuart) x.Altp2M = Altp2MMode(xc.altp2m) +x.ProcessorTraceBufKb = int(xc.processor_trace_buf_kb) return nil} @@ -1592,6 +1593,7 @@ return fmt.Errorf("invalid union key '%v'", x.Type)} xc.arch_arm.gic_version = C.libxl_gic_version(x.ArchArm.GicVersion) xc.arch_arm.vuart = C.libxl_vuart_type(x.ArchArm.Vuart) xc.altp2m = C.libxl_altp2m_mode(x.Altp2M) +xc.processor_trace_buf_kb = C.int(x.ProcessorTraceBufKb) return nil } diff --git a/tools/golang/xenlight/types.gen.go b/tools/golang/xenlight/types.gen.go index 663c1e86b4..f4bc16c0fd 100644 --- a/tools/golang/xenlight/types.gen.go +++ b/tools/golang/xenlight/types.gen.go @@ -516,6 +516,7 @@ GicVersion GicVersion Vuart VuartType } Altp2M Altp2MMode +ProcessorTraceBufKb int } type domainBuildInfoTypeUnion interface { diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index 1cd6c38e83..fbf222967a 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -438,6 +438,14 @@ */ #define LIBXL_HAVE_CREATEINFO_PASSTHROUGH 1 +/* + * LIBXL_HAVE_PROCESSOR_TRACE_BUF_KB indicates that + * libxl_domain_create_info has a processor_trace_buf_kb parameter, which + * allows to enable pre-allocation of processor tracing buffers of given + * size. + */ +#define LIBXL_HAVE_PROCESSOR_TRACE_BUF_KB 1 + /* * libxl ABI compatibility * diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c index 2814818e34..4d6318124a 100644 --- a/tools/libxl/libxl_create.c +++ b/tools/libxl/libxl_create.c @@ -608,6 +608,7 @@ int libxl__domain_make(libxl__gc *gc, libxl_domain_config *d_config, .max_evtchn_port = b_info->event_channels, .max_grant_frames = b_info->max_grant_frames, .max_maptrack_frames = b_info->max_maptrack_frames, + .processor_trace_buf_kb = b_info->processor_trace_buf_kb, }; if (info->type != LIBXL_DOMAIN_TYPE_PV) { diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl index 9d3f05f399..748fde65ab 100644 --- a/tools/libxl/libxl_types.idl +++ b/tools/libxl/libxl_types.idl @@ -645,6 +645,10 @@ libxl_domain_build_info = Struct("domain_build_info",[ # supported by x86 HVM and ARM support is planned. ("altp2m", libxl_altp2m_mode), + # Size of preallocated processor trace buffers (in KBYTES). + # Use zero value to disable this feature. + ("processor_trace_buf_kb", integer), + ], dir=DIR_IN, copy_deprecated_fn="libxl__domain_build_info_copy_deprecated", ) diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index 61b4ef7b7e..87e373b413 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -1861,6 +1861,28 @@ void parse_config_data(const char *config_source, } } + if (!xlu_cfg_get_long(config, "processor_trace_buf_kb", &l, 1) && l) { + if (l & (l - 1)) { + fprintf(stderr, "ERROR: processor_trace_buf_kb" + " - must be a power of 2\n"); + exit(1); + } + + if (l < 8) { + fprintf(stderr, "ERROR: processor_trace_buf_kb" + " - value is too small\n"); + exit(1); + } + + if (l > 1024*1024*4) { + fprintf(stderr, "ERROR: processor_trace_buf_kb" + " - value is too large\n"); + exit(1); + } + + b_info->processor_trace_buf_kb = l; + } + if (!xlu_cfg_get_list(config, "ioports", &ioports, &num_ioports, 0)) { b_info->num_ioports = num_ioports; b_info->ioports = calloc(num_ioports, sizeof(*b_info->ioports)); From patchwork Tue Jul 7 19:39:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649931 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BF46513B6 for ; Tue, 7 Jul 2020 19:41:56 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A561F206BE for ; Tue, 7 Jul 2020 19:41:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A561F206BE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSp-0007t4-CS; Tue, 07 Jul 2020 19:41:11 +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 1jstSo-0007no-0c for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:41:10 +0000 X-Inumbo-ID: c9b27083-c089-11ea-8de3-12813bfff9fa Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id c9b27083-c089-11ea-8de3-12813bfff9fa; Tue, 07 Jul 2020 19:41:02 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 3F581A26B6; Tue, 7 Jul 2020 21:41:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 3CA2FA26B9; Tue, 7 Jul 2020 21:41:00 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id Edb0dHd9Z37g; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 8BB3EA2657; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 2Y_eDj3TRnSX; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 579C1A265A; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 1830E2243D; Tue, 7 Jul 2020 21:40:05 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id YPKWuYMmlrOk; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id D299522444; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) X-Quarantine-ID: <6kPGelPQA59y> X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 6kPGelPQA59y; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id 9DBA82242E; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 06/11] x86/hvm: processor trace interface in HVM Date: Tue, 7 Jul 2020 21:39:45 +0200 Message-Id: <1916e06793ffaaa70c471bcd6bcf168597793bd5.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Julien Grall , Stefano Stabellini , luwei.kang@intel.com, Wei Liu , Andrew Cooper , Michal Leszczynski , Ian Jackson , George Dunlap , Jan Beulich , tamas.lengyel@intel.com, =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Implement necessary changes in common code/HVM to support processor trace features. Define vmtrace_pt_* API and implement trace buffer allocation/deallocation in common code. Signed-off-by: Michal Leszczynski --- xen/arch/x86/domain.c | 21 +++++++++++++++++++++ xen/common/domain.c | 35 +++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/hvm.h | 20 ++++++++++++++++++++ xen/include/xen/sched.h | 4 ++++ 4 files changed, 80 insertions(+) diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index b75017b28b..8ce2ab6b8f 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -2205,6 +2205,27 @@ int domain_relinquish_resources(struct domain *d) altp2m_vcpu_disable_ve(v); } + for_each_vcpu ( d, v ) + { + unsigned int i; + uint64_t nr_pages = v->domain->processor_trace_buf_kb * KB(1); + nr_pages >>= PAGE_SHIFT; + + if ( !v->vmtrace.pt_buf ) + continue; + + for ( i = 0; i < nr_pages; i++ ) + { + struct page_info *pg = mfn_to_page( + mfn_add(page_to_mfn(v->vmtrace.pt_buf), i)); + + put_page_alloc_ref(pg); + put_page_and_type(pg); + } + + v->vmtrace.pt_buf = NULL; + } + if ( is_pv_domain(d) ) { for_each_vcpu ( d, v ) diff --git a/xen/common/domain.c b/xen/common/domain.c index e6e8f88da1..193099a2ab 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -137,6 +137,38 @@ static void vcpu_destroy(struct vcpu *v) free_vcpu_struct(v); } +static int vmtrace_alloc_buffers(struct vcpu *v) +{ + unsigned int i; + struct page_info *pg; + uint64_t size = v->domain->processor_trace_buf_kb * KB(1); + + pg = alloc_domheap_pages(v->domain, get_order_from_bytes(size), + MEMF_no_refcount); + + if ( !pg ) + return -ENOMEM; + + for ( i = 0; i < (size >> PAGE_SHIFT); i++ ) + { + struct page_info *pg_iter = mfn_to_page( + mfn_add(page_to_mfn(pg), i)); + + if ( !get_page_and_type(pg_iter, v->domain, PGT_writable_page) ) + { + /* + * The domain can't possibly know about this page yet, so failure + * here is a clear indication of something fishy going on. + */ + domain_crash(v->domain); + return -ENODATA; + } + } + + v->vmtrace.pt_buf = pg; + return 0; +} + struct vcpu *vcpu_create(struct domain *d, unsigned int vcpu_id) { struct vcpu *v; @@ -162,6 +194,9 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int vcpu_id) v->vcpu_id = vcpu_id; v->dirty_cpu = VCPU_CPU_CLEAN; + if ( d->processor_trace_buf_kb && vmtrace_alloc_buffers(v) != 0 ) + return NULL; + spin_lock_init(&v->virq_lock); tasklet_init(&v->continue_hypercall_tasklet, NULL, NULL); diff --git a/xen/include/asm-x86/hvm/hvm.h b/xen/include/asm-x86/hvm/hvm.h index 1eb377dd82..476a216205 100644 --- a/xen/include/asm-x86/hvm/hvm.h +++ b/xen/include/asm-x86/hvm/hvm.h @@ -214,6 +214,10 @@ struct hvm_function_table { bool_t (*altp2m_vcpu_emulate_ve)(struct vcpu *v); int (*altp2m_vcpu_emulate_vmfunc)(const struct cpu_user_regs *regs); + /* vmtrace */ + int (*vmtrace_control_pt)(struct vcpu *v, bool enable); + int (*vmtrace_get_pt_offset)(struct vcpu *v, uint64_t *offset, uint64_t *size); + /* * Parameters and callbacks for hardware-assisted TSC scaling, * which are valid only when the hardware feature is available. @@ -655,6 +659,22 @@ static inline bool altp2m_vcpu_emulate_ve(struct vcpu *v) return false; } +static inline int vmtrace_control_pt(struct vcpu *v, bool enable) +{ + if ( hvm_funcs.vmtrace_control_pt ) + return hvm_funcs.vmtrace_control_pt(v, enable); + + return -EOPNOTSUPP; +} + +static inline int vmtrace_get_pt_offset(struct vcpu *v, uint64_t *offset, uint64_t *size) +{ + if ( hvm_funcs.vmtrace_get_pt_offset ) + return hvm_funcs.vmtrace_get_pt_offset(v, offset, size); + + return -EOPNOTSUPP; +} + /* * This must be defined as a macro instead of an inline function, * because it uses 'struct vcpu' and 'struct domain' which have diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index c046e59886..b6f39233aa 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -253,6 +253,10 @@ struct vcpu /* vPCI per-vCPU area, used to store data for long running operations. */ struct vpci_vcpu vpci; + struct { + struct page_info *pt_buf; + } vmtrace; + struct arch_vcpu arch; }; From patchwork Tue Jul 7 19:39:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649923 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 56BD014B7 for ; Tue, 7 Jul 2020 19:41:47 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 35357206BE for ; Tue, 7 Jul 2020 19:41:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 35357206BE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSn-0007rG-2T; Tue, 07 Jul 2020 19:41:09 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSm-0007ml-4K for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:41:08 +0000 X-Inumbo-ID: cabb34d2-c089-11ea-bb8b-bc764e2007e4 Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id cabb34d2-c089-11ea-bb8b-bc764e2007e4; Tue, 07 Jul 2020 19:41:03 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 0D846A26BB; Tue, 7 Jul 2020 21:41:02 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id ADD5EA26A8; Tue, 7 Jul 2020 21:41:00 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id fXx8pdKVsSKH; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id A79E9A26AF; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id l2_NCbfU6I40; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 61B5DA2660; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 39C8D2247C; Tue, 7 Jul 2020 21:40:05 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id Lg6X8o5lhAGl; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 0020522452; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id OFjbTfOjfw3p; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id B5839223C8; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 07/11] x86/vmx: implement IPT in VMX Date: Tue, 7 Jul 2020 21:39:46 +0200 Message-Id: <7ddfc44d6ffde0fa307f0e074225f588c397aef0.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Kevin Tian , luwei.kang@intel.com, Jun Nakajima , Wei Liu , Andrew Cooper , Michal Leszczynski , Jan Beulich , tamas.lengyel@intel.com, =?utf-8?q?Roger_P?= =?utf-8?q?au_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Use Intel Processor Trace feature to provide vmtrace_pt_* interface for HVM/VMX. Signed-off-by: Michal Leszczynski --- xen/arch/x86/hvm/vmx/vmx.c | 110 +++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/vmx/vmcs.h | 3 + xen/include/asm-x86/hvm/vmx/vmx.h | 14 ++++ 3 files changed, 127 insertions(+) diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index cc6d4ece22..63a5a76e16 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -428,6 +428,56 @@ static void vmx_domain_relinquish_resources(struct domain *d) vmx_free_vlapic_mapping(d); } +static int vmx_init_pt(struct vcpu *v) +{ + int rc; + uint64_t size = v->domain->processor_trace_buf_kb * KB(1); + + if ( !v->vmtrace.pt_buf || !size ) + return -EINVAL; + + /* + * We don't accept trace buffer size smaller than single page + * and the upper bound is defined as 4GB in the specification. + * The buffer size must be also a power of 2. + */ + if ( size < PAGE_SIZE || size > GB(4) || (size & (size - 1)) ) + return -EINVAL; + + v->arch.hvm.vmx.ipt_state = xzalloc(struct ipt_state); + + if ( !v->arch.hvm.vmx.ipt_state ) + return -ENOMEM; + + v->arch.hvm.vmx.ipt_state->output_base = + page_to_maddr(v->vmtrace.pt_buf); + v->arch.hvm.vmx.ipt_state->output_mask.raw = size - 1; + + rc = vmx_add_host_load_msr(v, MSR_RTIT_CTL, 0); + + if ( rc ) + return rc; + + rc = vmx_add_guest_msr(v, MSR_RTIT_CTL, + RTIT_CTL_TRACE_EN | RTIT_CTL_OS | + RTIT_CTL_USR | RTIT_CTL_BRANCH_EN); + + if ( rc ) + return rc; + + return 0; +} + +static int vmx_destroy_pt(struct vcpu* v) +{ + if ( v->arch.hvm.vmx.ipt_state ) + xfree(v->arch.hvm.vmx.ipt_state); + + v->arch.hvm.vmx.ipt_state = NULL; + return 0; +} + + static int vmx_vcpu_initialise(struct vcpu *v) { int rc; @@ -471,6 +521,14 @@ static int vmx_vcpu_initialise(struct vcpu *v) vmx_install_vlapic_mapping(v); + if ( v->domain->processor_trace_buf_kb ) + { + rc = vmx_init_pt(v); + + if ( rc ) + return rc; + } + return 0; } @@ -483,6 +541,7 @@ static void vmx_vcpu_destroy(struct vcpu *v) * prior to vmx_domain_destroy so we need to disable PML for each vcpu * separately here. */ + vmx_destroy_pt(v); vmx_vcpu_disable_pml(v); vmx_destroy_vmcs(v); passive_domain_destroy(v); @@ -513,6 +572,18 @@ static void vmx_save_guest_msrs(struct vcpu *v) * be updated at any time via SWAPGS, which we cannot trap. */ v->arch.hvm.vmx.shadow_gs = rdgsshadow(); + + if ( unlikely(v->arch.hvm.vmx.ipt_state && + v->arch.hvm.vmx.ipt_state->active) ) + { + uint64_t rtit_ctl; + rdmsrl(MSR_RTIT_CTL, rtit_ctl); + BUG_ON(rtit_ctl & RTIT_CTL_TRACE_EN); + + rdmsrl(MSR_RTIT_STATUS, v->arch.hvm.vmx.ipt_state->status); + rdmsrl(MSR_RTIT_OUTPUT_MASK, + v->arch.hvm.vmx.ipt_state->output_mask.raw); + } } static void vmx_restore_guest_msrs(struct vcpu *v) @@ -524,6 +595,17 @@ static void vmx_restore_guest_msrs(struct vcpu *v) if ( cpu_has_msr_tsc_aux ) wrmsr_tsc_aux(v->arch.msrs->tsc_aux); + + if ( unlikely(v->arch.hvm.vmx.ipt_state && + v->arch.hvm.vmx.ipt_state->active) ) + { + wrmsrl(MSR_RTIT_OUTPUT_BASE, + v->arch.hvm.vmx.ipt_state->output_base); + wrmsrl(MSR_RTIT_OUTPUT_MASK, + v->arch.hvm.vmx.ipt_state->output_mask.raw); + wrmsrl(MSR_RTIT_STATUS, + v->arch.hvm.vmx.ipt_state->status); + } } void vmx_update_cpu_exec_control(struct vcpu *v) @@ -2240,6 +2322,25 @@ static bool vmx_get_pending_event(struct vcpu *v, struct x86_event *info) return true; } +static int vmx_control_pt(struct vcpu *v, bool enable) +{ + if ( !v->arch.hvm.vmx.ipt_state ) + return -EINVAL; + + v->arch.hvm.vmx.ipt_state->active = enable; + return 0; +} + +static int vmx_get_pt_offset(struct vcpu *v, uint64_t *offset, uint64_t *size) +{ + if ( !v->arch.hvm.vmx.ipt_state ) + return -EINVAL; + + *offset = v->arch.hvm.vmx.ipt_state->output_mask.offset; + *size = v->arch.hvm.vmx.ipt_state->output_mask.size + 1; + return 0; +} + static struct hvm_function_table __initdata vmx_function_table = { .name = "VMX", .cpu_up_prepare = vmx_cpu_up_prepare, @@ -2295,6 +2396,8 @@ static struct hvm_function_table __initdata vmx_function_table = { .altp2m_vcpu_update_vmfunc_ve = vmx_vcpu_update_vmfunc_ve, .altp2m_vcpu_emulate_ve = vmx_vcpu_emulate_ve, .altp2m_vcpu_emulate_vmfunc = vmx_vcpu_emulate_vmfunc, + .vmtrace_control_pt = vmx_control_pt, + .vmtrace_get_pt_offset = vmx_get_pt_offset, .tsc_scaling = { .max_ratio = VMX_TSC_MULTIPLIER_MAX, }, @@ -3674,6 +3777,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) hvm_invalidate_regs_fields(regs); + if ( unlikely(v->arch.hvm.vmx.ipt_state && + v->arch.hvm.vmx.ipt_state->active) ) + { + rdmsrl(MSR_RTIT_OUTPUT_MASK, + v->arch.hvm.vmx.ipt_state->output_mask.raw); + } + if ( paging_mode_hap(v->domain) ) { /* diff --git a/xen/include/asm-x86/hvm/vmx/vmcs.h b/xen/include/asm-x86/hvm/vmx/vmcs.h index 6153ba6769..65971fa6ad 100644 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -186,6 +186,9 @@ struct vmx_vcpu { * pCPU and wakeup the related vCPU. */ struct pi_blocking_vcpu pi_blocking; + + /* State of processor trace feature */ + struct ipt_state *ipt_state; }; int vmx_create_vmcs(struct vcpu *v); diff --git a/xen/include/asm-x86/hvm/vmx/vmx.h b/xen/include/asm-x86/hvm/vmx/vmx.h index 111ccd7e61..8d7c67e43d 100644 --- a/xen/include/asm-x86/hvm/vmx/vmx.h +++ b/xen/include/asm-x86/hvm/vmx/vmx.h @@ -689,4 +689,18 @@ typedef union ldt_or_tr_instr_info { }; } ldt_or_tr_instr_info_t; +/* Processor Trace state per vCPU */ +struct ipt_state { + bool active; + uint64_t status; + uint64_t output_base; + union { + uint64_t raw; + struct { + uint32_t size; + uint32_t offset; + }; + } output_mask; +}; + #endif /* __ASM_X86_HVM_VMX_VMX_H__ */ From patchwork Tue Jul 7 19:39:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649921 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7B09814B7 for ; Tue, 7 Jul 2020 19:41:38 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6137F206BE for ; Tue, 7 Jul 2020 19:41:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6137F206BE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSj-0007oG-Ja; Tue, 07 Jul 2020 19:41:05 +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 1jstSj-0007no-23 for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:41:05 +0000 X-Inumbo-ID: c98e3e7e-c089-11ea-8de3-12813bfff9fa Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id c98e3e7e-c089-11ea-8de3-12813bfff9fa; Tue, 07 Jul 2020 19:41:02 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id E789EA26AF; Tue, 7 Jul 2020 21:41:00 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id E4FA5A2675; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id tpwVciS3kWfD; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 79919A26A3; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id R9EPWexH40j8; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 4FFA3A261F; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id BEE562247F; Tue, 7 Jul 2020 21:40:04 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id fYEdaYTL948b; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 0B00922456; Tue, 7 Jul 2020 21:39:54 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id hw4v3GfJxCBR; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id CD26022303; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 08/11] x86/mm: add vmtrace_buf resource type Date: Tue, 7 Jul 2020 21:39:47 +0200 Message-Id: <2129d21e7ef7e960951a8baafab01d9392dff8f3.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Julien Grall , Stefano Stabellini , luwei.kang@intel.com, Wei Liu , Andrew Cooper , Michal Leszczynski , Ian Jackson , George Dunlap , Jan Beulich , tamas.lengyel@intel.com Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Allow to map processor trace buffer using acquire_resource(). Signed-off-by: Michal Leszczynski --- xen/common/memory.c | 31 +++++++++++++++++++++++++++++++ xen/include/public/memory.h | 1 + 2 files changed, 32 insertions(+) diff --git a/xen/common/memory.c b/xen/common/memory.c index eb42f883df..c0a22eb60f 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1007,6 +1007,32 @@ static long xatp_permission_check(struct domain *d, unsigned int space) return xsm_add_to_physmap(XSM_TARGET, current->domain, d); } +static int acquire_vmtrace_buf(struct domain *d, unsigned int id, + uint64_t frame, + uint64_t nr_frames, + xen_pfn_t mfn_list[]) +{ + mfn_t mfn; + unsigned int i; + uint64_t size; + struct vcpu *v = domain_vcpu(d, id); + + if ( !v || !v->vmtrace.pt_buf ) + return -EINVAL; + + mfn = page_to_mfn(v->vmtrace.pt_buf); + size = v->domain->processor_trace_buf_kb * KB(1); + + if ( (frame > (size >> PAGE_SHIFT)) || + (nr_frames > ((size >> PAGE_SHIFT) - frame)) ) + return -EINVAL; + + for ( i = 0; i < nr_frames; i++ ) + mfn_list[i] = mfn_x(mfn_add(mfn, frame + i)); + + return 0; +} + static int acquire_grant_table(struct domain *d, unsigned int id, unsigned long frame, unsigned int nr_frames, @@ -1117,6 +1143,11 @@ static int acquire_resource( mfn_list); break; + case XENMEM_resource_vmtrace_buf: + rc = acquire_vmtrace_buf(d, xmar.id, xmar.frame, xmar.nr_frames, + mfn_list); + break; + default: rc = arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame, xmar.nr_frames, mfn_list); diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index 21057ed78e..f4c905a10e 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -625,6 +625,7 @@ struct xen_mem_acquire_resource { #define XENMEM_resource_ioreq_server 0 #define XENMEM_resource_grant_table 1 +#define XENMEM_resource_vmtrace_buf 2 /* * IN - a type-specific resource identifier, which must be zero From patchwork Tue Jul 7 19:39:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649933 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D67A56C1 for ; Tue, 7 Jul 2020 19:42:14 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BBFBE206BE for ; Tue, 7 Jul 2020 19:42:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BBFBE206BE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSr-0007v9-Ll; Tue, 07 Jul 2020 19:41:13 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSr-0007ml-4n for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:41:13 +0000 X-Inumbo-ID: cc6135ca-c089-11ea-b7bb-bc764e2007e4 Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id cc6135ca-c089-11ea-b7bb-bc764e2007e4; Tue, 07 Jul 2020 19:41:06 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 7AB06A2675; Tue, 7 Jul 2020 21:41:05 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 77C2FA26BD; Tue, 7 Jul 2020 21:41:04 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id YWX_RmwuQhJ8; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 74F96A2695; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id i9YARP6nXRLI; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 51EF8A264E; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id D35E72248C; Tue, 7 Jul 2020 21:40:04 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 9gHZUn6OFfvJ; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 1ACA122303; Tue, 7 Jul 2020 21:39:54 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Q7F0amUjHudM; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id E408A22426; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 09/11] x86/domctl: add XEN_DOMCTL_vmtrace_op Date: Tue, 7 Jul 2020 21:39:48 +0200 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Julien Grall , Stefano Stabellini , luwei.kang@intel.com, Wei Liu , Andrew Cooper , Michal Leszczynski , Ian Jackson , George Dunlap , Jan Beulich , tamas.lengyel@intel.com, =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Implement domctl to manage the runtime state of processor trace feature. Signed-off-by: Michal Leszczynski --- xen/arch/x86/domctl.c | 50 +++++++++++++++++++++++++++++++++++++ xen/include/public/domctl.h | 28 +++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/xen/arch/x86/domctl.c b/xen/arch/x86/domctl.c index 6f2c69788d..6132499db4 100644 --- a/xen/arch/x86/domctl.c +++ b/xen/arch/x86/domctl.c @@ -322,6 +322,50 @@ void arch_get_domain_info(const struct domain *d, info->arch_config.emulation_flags = d->arch.emulation_flags; } +static int do_vmtrace_op(struct domain *d, struct xen_domctl_vmtrace_op *op, + XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) +{ + int rc; + struct vcpu *v; + + if ( op->pad1 || op->pad2 ) + return -EINVAL; + + if ( !vmtrace_supported ) + return -EOPNOTSUPP; + + if ( !is_hvm_domain(d) ) + return -EOPNOTSUPP; + + if ( op->vcpu >= d->max_vcpus ) + return -EINVAL; + + v = domain_vcpu(d, op->vcpu); + rc = 0; + + switch ( op->cmd ) + { + case XEN_DOMCTL_vmtrace_pt_enable: + case XEN_DOMCTL_vmtrace_pt_disable: + vcpu_pause(v); + rc = vmtrace_control_pt(v, op->cmd == XEN_DOMCTL_vmtrace_pt_enable); + vcpu_unpause(v); + break; + + case XEN_DOMCTL_vmtrace_pt_get_offset: + rc = vmtrace_get_pt_offset(v, &op->offset, &op->size); + + if ( !rc && d->is_dying ) + rc = ENODATA; + break; + + default: + rc = -EOPNOTSUPP; + } + + return rc; +} + #define MAX_IOPORTS 0x10000 long arch_do_domctl( @@ -337,6 +381,12 @@ long arch_do_domctl( switch ( domctl->cmd ) { + case XEN_DOMCTL_vmtrace_op: + ret = do_vmtrace_op(d, &domctl->u.vmtrace_op, u_domctl); + if ( !ret ) + copyback = true; + break; + case XEN_DOMCTL_shadow_op: ret = paging_domctl(d, &domctl->u.shadow_op, u_domctl, 0); if ( ret == -ERESTART ) diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h index 7681675a94..73c7ccbd16 100644 --- a/xen/include/public/domctl.h +++ b/xen/include/public/domctl.h @@ -1136,6 +1136,30 @@ struct xen_domctl_vuart_op { */ }; +/* XEN_DOMCTL_vmtrace_op: Perform VM tracing related operation */ +#if defined(__XEN__) || defined(__XEN_TOOLS__) + +struct xen_domctl_vmtrace_op { + /* IN variable */ + uint32_t cmd; +/* Enable/disable external vmtrace for given domain */ +#define XEN_DOMCTL_vmtrace_pt_enable 1 +#define XEN_DOMCTL_vmtrace_pt_disable 2 +#define XEN_DOMCTL_vmtrace_pt_get_offset 3 + domid_t domain; + uint16_t pad1; + uint32_t vcpu; + uint16_t pad2; + + /* OUT variable */ + uint64_aligned_t size; + uint64_aligned_t offset; +}; +typedef struct xen_domctl_vmtrace_op xen_domctl_vmtrace_op_t; +DEFINE_XEN_GUEST_HANDLE(xen_domctl_vmtrace_op_t); + +#endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */ + struct xen_domctl { uint32_t cmd; #define XEN_DOMCTL_createdomain 1 @@ -1217,6 +1241,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 @@ -1277,6 +1302,9 @@ struct xen_domctl { struct xen_domctl_monitor_op monitor_op; struct xen_domctl_psr_alloc psr_alloc; struct xen_domctl_vuart_op vuart_op; +#if defined(__XEN__) || defined(__XEN_TOOLS__) + struct xen_domctl_vmtrace_op vmtrace_op; +#endif uint8_t pad[128]; } u; }; From patchwork Tue Jul 7 19:39:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649929 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 490BE6C1 for ; Tue, 7 Jul 2020 19:41:55 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2E47B20738 for ; Tue, 7 Jul 2020 19:41:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2E47B20738 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSz-00082p-GW; Tue, 07 Jul 2020 19:41:21 +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 1jstSy-0007no-16 for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:41:20 +0000 X-Inumbo-ID: c9c1d00f-c089-11ea-8de3-12813bfff9fa Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id c9c1d00f-c089-11ea-8de3-12813bfff9fa; Tue, 07 Jul 2020 19:41:02 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 9D8B7A26B9; Tue, 7 Jul 2020 21:41:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 9461AA26BD; Tue, 7 Jul 2020 21:41:00 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id AHbW4AukT979; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id A0EAEA2691; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id ciAltz9WSXP5; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 7CD8CA26A8; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 68EDD2242F; Tue, 7 Jul 2020 21:40:05 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id XSkxh8RtH51F; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 367FC22426; Tue, 7 Jul 2020 21:39:54 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id R1Vij3rUE1XP; Tue, 7 Jul 2020 21:39:54 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id F013422450; Tue, 7 Jul 2020 21:39:53 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 10/11] tools/libxc: add xc_vmtrace_* functions Date: Tue, 7 Jul 2020 21:39:49 +0200 Message-Id: <476203bca92f1fb0e8de2be2bcfb88695a5688f8.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: luwei.kang@intel.com, Michal Leszczynski , tamas.lengyel@intel.com, Ian Jackson , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Add functions in libxc that use the new XEN_DOMCTL_vmtrace interface. Signed-off-by: Michal Leszczynski --- tools/libxc/Makefile | 1 + tools/libxc/include/xenctrl.h | 40 ++++++++++++++++ tools/libxc/xc_vmtrace.c | 87 +++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 tools/libxc/xc_vmtrace.c diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile index fae5969a73..605e44501d 100644 --- a/tools/libxc/Makefile +++ b/tools/libxc/Makefile @@ -27,6 +27,7 @@ CTRL_SRCS-y += xc_csched2.c CTRL_SRCS-y += xc_arinc653.c CTRL_SRCS-y += xc_rt.c CTRL_SRCS-y += xc_tbuf.c +CTRL_SRCS-y += xc_vmtrace.c CTRL_SRCS-y += xc_pm.c CTRL_SRCS-y += xc_cpu_hotplug.c CTRL_SRCS-y += xc_resume.c diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h index 4c89b7294c..491b2c3236 100644 --- a/tools/libxc/include/xenctrl.h +++ b/tools/libxc/include/xenctrl.h @@ -1585,6 +1585,46 @@ int xc_tbuf_set_cpu_mask(xc_interface *xch, xc_cpumap_t mask); int xc_tbuf_set_evt_mask(xc_interface *xch, uint32_t mask); +/** + * Enable processor trace for given vCPU in given DomU. + * Allocate the trace ringbuffer with given size. + * + * @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_pt_enable(xc_interface *xch, uint32_t domid, + uint32_t vcpu); + +/** + * Disable processor trace for given vCPU in given DomU. + * Deallocate the trace ringbuffer. + * + * @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_pt_disable(xc_interface *xch, uint32_t domid, + uint32_t vcpu); + +/** + * Get current offset inside the trace ringbuffer. + * This allows to determine how much data was written into the buffer. + * Once buffer overflows, the offset will reset to 0 and the previous + * data will be overriden. + * + * @parm xch a handle to an open hypervisor interface + * @parm domid domain identifier + * @parm vcpu vcpu identifier + * @parm offset current offset inside trace buffer will be written there + * @parm size the total size of the trace buffer (in bytes) + * @return 0 on success, -1 on failure + */ +int xc_vmtrace_pt_get_offset(xc_interface *xch, uint32_t domid, + uint32_t vcpu, uint64_t *offset, uint64_t *size); + int xc_domctl(xc_interface *xch, struct xen_domctl *domctl); int xc_sysctl(xc_interface *xch, struct xen_sysctl *sysctl); diff --git a/tools/libxc/xc_vmtrace.c b/tools/libxc/xc_vmtrace.c new file mode 100644 index 0000000000..ee034da8d3 --- /dev/null +++ b/tools/libxc/xc_vmtrace.c @@ -0,0 +1,87 @@ +/****************************************************************************** + * 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" +#include + +int xc_vmtrace_pt_enable( + xc_interface *xch, uint32_t domid, uint32_t vcpu) +{ + DECLARE_DOMCTL; + int rc; + + domctl.cmd = XEN_DOMCTL_vmtrace_op; + domctl.domain = domid; + domctl.u.vmtrace_op.cmd = XEN_DOMCTL_vmtrace_pt_enable; + domctl.u.vmtrace_op.vcpu = vcpu; + domctl.u.vmtrace_op.pad1 = 0; + domctl.u.vmtrace_op.pad2 = 0; + + rc = do_domctl(xch, &domctl); + return rc; +} + +int xc_vmtrace_pt_get_offset( + xc_interface *xch, uint32_t domid, uint32_t vcpu, + uint64_t *offset, uint64_t *size) +{ + DECLARE_DOMCTL; + int rc; + + domctl.cmd = XEN_DOMCTL_vmtrace_op; + domctl.domain = domid; + domctl.u.vmtrace_op.cmd = XEN_DOMCTL_vmtrace_pt_get_offset; + domctl.u.vmtrace_op.vcpu = vcpu; + domctl.u.vmtrace_op.pad1 = 0; + domctl.u.vmtrace_op.pad2 = 0; + + rc = do_domctl(xch, &domctl); + if ( !rc ) + { + if (offset) + *offset = domctl.u.vmtrace_op.offset; + + if (size) + *size = domctl.u.vmtrace_op.size; + } + + return rc; +} + +int xc_vmtrace_pt_disable(xc_interface *xch, uint32_t domid, uint32_t vcpu) +{ + DECLARE_DOMCTL; + int rc; + + domctl.cmd = XEN_DOMCTL_vmtrace_op; + domctl.domain = domid; + domctl.u.vmtrace_op.cmd = XEN_DOMCTL_vmtrace_pt_disable; + domctl.u.vmtrace_op.vcpu = vcpu; + domctl.u.vmtrace_op.pad1 = 0; + domctl.u.vmtrace_op.pad2 = 0; + + rc = do_domctl(xch, &domctl); + return rc; +} + From patchwork Tue Jul 7 19:39:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= X-Patchwork-Id: 11649925 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5436313B6 for ; Tue, 7 Jul 2020 19:41:47 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 354B8206E9 for ; Tue, 7 Jul 2020 19:41:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 354B8206E9 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=cert.pl Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSi-0007nF-AN; Tue, 07 Jul 2020 19:41:04 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jstSh-0007ml-5i for xen-devel@lists.xenproject.org; Tue, 07 Jul 2020 19:41:03 +0000 X-Inumbo-ID: c9c50350-c089-11ea-bca7-bc764e2007e4 Received: from bagnar.nask.net.pl (unknown [195.187.242.196]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id c9c50350-c089-11ea-bca7-bc764e2007e4; Tue, 07 Jul 2020 19:41:02 +0000 (UTC) Received: from bagnar.nask.net.pl (unknown [172.16.9.10]) by bagnar.nask.net.pl (Postfix) with ESMTP id 58FB1A26A3; Tue, 7 Jul 2020 21:41:01 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 5684AA26BB; Tue, 7 Jul 2020 21:41:00 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id cPr368eJ3i4J; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by bagnar.nask.net.pl (Postfix) with ESMTP id 83B62A26AD; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) X-Virus-Scanned: amavisd-new at bagnar.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from bagnar.nask.net.pl ([127.0.0.1]) by localhost (bagnar.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id zXXw5sv7UBpl; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from belindir.nask.net.pl (belindir-ext.nask.net.pl [195.187.242.210]) by bagnar.nask.net.pl (Postfix) with ESMTP id 63757A2675; Tue, 7 Jul 2020 21:40:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 531FB22467; Tue, 7 Jul 2020 21:40:05 +0200 (CEST) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id mD7MzktKtjP9; Tue, 7 Jul 2020 21:39:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by belindir.nask.net.pl (Postfix) with ESMTP id 412DD22459; Tue, 7 Jul 2020 21:39:54 +0200 (CEST) X-Quarantine-ID: X-Virus-Scanned: amavisd-new at belindir.nask.net.pl X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from belindir.nask.net.pl ([127.0.0.1]) by localhost (belindir.nask.net.pl [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id z977rWsuvJ92; Tue, 7 Jul 2020 21:39:54 +0200 (CEST) Received: from mq-desktop.cert.pl (unknown [195.187.238.217]) by belindir.nask.net.pl (Postfix) with ESMTPSA id 10B2F22454; Tue, 7 Jul 2020 21:39:54 +0200 (CEST) From: =?utf-8?q?Micha=C5=82_Leszczy=C5=84ski?= To: xen-devel@lists.xenproject.org Subject: [PATCH v6 11/11] tools/proctrace: add proctrace tool Date: Tue, 7 Jul 2020 21:39:50 +0200 Message-Id: <8bc5959478d6ba1c1873615b53628094da578688.1594150543.git.michal.leszczynski@cert.pl> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 In-Reply-To: References: X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: luwei.kang@intel.com, Michal Leszczynski , tamas.lengyel@intel.com, Ian Jackson , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Michal Leszczynski Add an demonstration tool that uses xc_vmtrace_* calls in order to manage external IPT monitoring for DomU. Signed-off-by: Michal Leszczynski --- tools/proctrace/Makefile | 45 +++++++++ tools/proctrace/proctrace.c | 179 ++++++++++++++++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 tools/proctrace/Makefile create mode 100644 tools/proctrace/proctrace.c diff --git a/tools/proctrace/Makefile b/tools/proctrace/Makefile new file mode 100644 index 0000000000..9c135229b9 --- /dev/null +++ b/tools/proctrace/Makefile @@ -0,0 +1,45 @@ +# Copyright (C) CERT Polska - NASK PIB +# Author: Michał Leszczyński +# +# 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. + +XEN_ROOT=$(CURDIR)/../.. +include $(XEN_ROOT)/tools/Rules.mk + +CFLAGS += -Werror +CFLAGS += $(CFLAGS_libxenevtchn) +CFLAGS += $(CFLAGS_libxenctrl) +LDLIBS += $(LDLIBS_libxenctrl) +LDLIBS += $(LDLIBS_libxenevtchn) +LDLIBS += $(LDLIBS_libxenforeignmemory) + +.PHONY: all +all: build + +.PHONY: build +build: proctrace + +.PHONY: install +install: build + $(INSTALL_DIR) $(DESTDIR)$(sbindir) + $(INSTALL_PROG) proctrace $(DESTDIR)$(sbindir)/proctrace + +.PHONY: uninstall +uninstall: + rm -f $(DESTDIR)$(sbindir)/proctrace + +.PHONY: clean +clean: + $(RM) -f proctrace $(DEPS_RM) + +.PHONY: distclean +distclean: clean + +-include $(DEPS_INCLUDE) diff --git a/tools/proctrace/proctrace.c b/tools/proctrace/proctrace.c new file mode 100644 index 0000000000..3c1ccccee8 --- /dev/null +++ b/tools/proctrace/proctrace.c @@ -0,0 +1,179 @@ +/****************************************************************************** + * tools/proctrace.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ł Leszczyński, 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 + +volatile int interrupted = 0; +volatile int domain_down = 0; + +void term_handler(int signum) { + interrupted = 1; +} + +int main(int argc, char* argv[]) { + xc_interface *xc; + uint32_t domid; + uint32_t vcpu_id; + uint64_t size; + + int rc = -1; + uint8_t *buf = NULL; + uint64_t last_offset = 0; + + xenforeignmemory_handle *fmem; + xenforeignmemory_resource_handle *fres; + + if (signal(SIGINT, term_handler) == SIG_ERR) + { + fprintf(stderr, "Failed to register signal handler\n"); + return 1; + } + + if (argc != 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, "It's recommended to redirect this" + "program's output to file\n"); + fprintf(stderr, "or to pipe it's output to xxd or other program.\n"); + return 1; + } + + domid = atoi(argv[1]); + vcpu_id = atoi(argv[2]); + + xc = xc_interface_open(0, 0, 0); + + fmem = xenforeignmemory_open(0, 0); + + if (!xc) { + fprintf(stderr, "Failed to open xc interface\n"); + return 1; + } + + rc = xc_vmtrace_pt_enable(xc, domid, vcpu_id); + + if (rc) { + fprintf(stderr, "Failed to call xc_vmtrace_pt_enable\n"); + return 1; + } + + rc = xc_vmtrace_pt_get_offset(xc, domid, vcpu_id, NULL, &size); + + if (rc) { + fprintf(stderr, "Failed to get trace buffer size\n"); + return 1; + } + + fres = xenforeignmemory_map_resource( + fmem, domid, XENMEM_resource_vmtrace_buf, + /* vcpu: */ vcpu_id, + /* frame: */ 0, + /* num_frames: */ size >> XC_PAGE_SHIFT, + (void **)&buf, + PROT_READ, 0); + + if (!buf) { + fprintf(stderr, "Failed to map trace buffer\n"); + return 1; + } + + while (!interrupted) { + uint64_t offset; + rc = xc_vmtrace_pt_get_offset(xc, domid, vcpu_id, &offset, NULL); + + if (rc == ENODATA) { + interrupted = 1; + domain_down = 1; + } else if (rc) { + fprintf(stderr, "Failed to call xc_vmtrace_pt_get_offset\n"); + return 1; + } + + if (offset > last_offset) + { + fwrite(buf + last_offset, offset - last_offset, 1, stdout); + } + else if (offset < last_offset) + { + // buffer wrapped + fwrite(buf + last_offset, size - last_offset, 1, stdout); + fwrite(buf, offset, 1, stdout); + } + + last_offset = offset; + usleep(1000 * 100); + } + + rc = xenforeignmemory_unmap_resource(fmem, fres); + + if (rc) { + fprintf(stderr, "Failed to unmap resource\n"); + return 1; + } + + rc = xenforeignmemory_close(fmem); + + if (rc) { + fprintf(stderr, "Failed to close fmem\n"); + return 1; + } + + /* + * Don't try to disable PT if the domain is already dying. + */ + if (!domain_down) { + rc = xc_vmtrace_pt_disable(xc, domid, vcpu_id); + + if (rc) { + fprintf(stderr, "Failed to call xc_vmtrace_pt_disable\n"); + return 1; + } + } + + rc = xc_interface_close(xc); + + if (rc) { + fprintf(stderr, "Failed to close xc interface\n"); + return 1; + } + + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: nil + * End: + */