xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Aravind Gopalakrishnan <aravind.gopalakrishnan@amd.com>
To: Boris Ostrovsky <boris.ostrovsky@oracle.com>,
	JBeulich@suse.com, kevin.tian@intel.com,
	suravee.suthikulpanit@amd.com, dietmar.hahn@ts.fujitsu.com,
	dgdegra@tycho.nsa.gov, andrew.cooper3@citrix.com
Cc: xen-devel@lists.xen.org
Subject: Re: [PATCH v25 11/15] VPMU/AMD: Check MSR values before writing to hardware
Date: Wed, 8 Jul 2015 10:35:59 -0500	[thread overview]
Message-ID: <559D435F.1070708@amd.com> (raw)
In-Reply-To: <1434739486-1611-12-git-send-email-boris.ostrovsky@oracle.com>

On 6/19/2015 1:44 PM, Boris Ostrovsky wrote:
> A number of fields of PMU control MSRs are defined as Reserved. AMD
> documentation requires that such fields are preserved when the register
> is written by software.
>
> Add checks to amd_vpmu_do_wrmsr() to make sure that guests don't attempt
> to modify those bits.
>
> Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>

Reviewed-by: Aravind Gopalakrishnan <Aravind.Gopalakrishnan@amd.com>


> ---
>   xen/arch/x86/hvm/svm/vpmu.c | 49 +++++++++++++++++++++++++++++++++++++++------
>   1 file changed, 43 insertions(+), 6 deletions(-)
>
> diff --git a/xen/arch/x86/hvm/svm/vpmu.c b/xen/arch/x86/hvm/svm/vpmu.c
> index 74d03a5..934f1b7 100644
> --- a/xen/arch/x86/hvm/svm/vpmu.c
> +++ b/xen/arch/x86/hvm/svm/vpmu.c
> @@ -48,6 +48,7 @@ static bool_t __read_mostly k7_counters_mirrored;
>   
>   #define F10H_NUM_COUNTERS   4
>   #define F15H_NUM_COUNTERS   6
> +#define MAX_NUM_COUNTERS    F15H_NUM_COUNTERS
>   
>   /* PMU Counter MSRs. */
>   static const u32 AMD_F10H_COUNTERS[] = {
> @@ -83,6 +84,11 @@ static const u32 AMD_F15H_CTRLS[] = {
>       MSR_AMD_FAM15H_EVNTSEL5
>   };
>   
> +/* Bits [63:42], [39:36], 21 and 19 are reserved */
> +#define CTRL_RSVD_MASK ((-1ULL & (~((1ULL << 42) - 1))) | \
> +                        (0xfULL << 36) | (1ULL << 21) | (1ULL << 19))
> +static uint64_t __read_mostly ctrl_rsvd[MAX_NUM_COUNTERS];
> +
>   /* Use private context as a flag for MSR bitmap */
>   #define msr_bitmap_on(vpmu)    do {                                    \
>                                      (vpmu)->priv_context = (void *)-1L; \
> @@ -92,17 +98,24 @@ static const u32 AMD_F15H_CTRLS[] = {
>                                  } while (0)
>   #define is_msr_bitmap_on(vpmu) ((vpmu)->priv_context != NULL)
>   
> -static inline int get_pmu_reg_type(u32 addr)
> +static inline int get_pmu_reg_type(u32 addr, unsigned int *idx)
>   {
>       if ( (addr >= MSR_K7_EVNTSEL0) && (addr <= MSR_K7_EVNTSEL3) )
> +    {
> +        *idx = addr - MSR_K7_EVNTSEL0;
>           return MSR_TYPE_CTRL;
> +    }
>   
>       if ( (addr >= MSR_K7_PERFCTR0) && (addr <= MSR_K7_PERFCTR3) )
> +    {
> +        *idx = addr - MSR_K7_PERFCTR0;
>           return MSR_TYPE_COUNTER;
> +    }
>   
>       if ( (addr >= MSR_AMD_FAM15H_EVNTSEL0) &&
>            (addr <= MSR_AMD_FAM15H_PERFCTR5 ) )
>       {
> +        *idx = (addr - MSR_AMD_FAM15H_EVNTSEL0) >> 1;
>           if (addr & 1)
>               return MSR_TYPE_COUNTER;
>           else
> @@ -140,6 +153,16 @@ static inline u32 get_fam15h_addr(u32 addr)
>       return addr;
>   }
>   
> +static void amd_vpmu_init_regs(struct xen_pmu_amd_ctxt *ctxt)
> +{
> +    unsigned i;
> +    uint64_t *ctrl_regs = vpmu_reg_pointer(ctxt, ctrls);
> +
> +    memset(&ctxt->regs[0], 0, 2 * sizeof(uint64_t) * num_counters);
> +    for ( i = 0; i < num_counters; i++ )
> +        ctrl_regs[i] = ctrl_rsvd[i];
> +}
> +
>   static void amd_vpmu_set_msr_bitmap(struct vcpu *v)
>   {
>       unsigned int i;
> @@ -289,19 +312,24 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
>   {
>       struct vcpu *v = current;
>       struct vpmu_struct *vpmu = vcpu_vpmu(v);
> +    unsigned int idx = 0;
> +    int type = get_pmu_reg_type(msr, &idx);
>   
>       ASSERT(!supported);
>   
> +    if ( (type == MSR_TYPE_CTRL ) &&
> +         ((msr_content & CTRL_RSVD_MASK) != ctrl_rsvd[idx]) )
> +        return -EINVAL;
> +
>       /* For all counters, enable guest only mode for HVM guest */
> -    if ( has_hvm_container_vcpu(v) &&
> -         (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
> +    if ( has_hvm_container_vcpu(v) && (type == MSR_TYPE_CTRL) &&
>            !is_guest_mode(msr_content) )
>       {
>           set_guest_mode(msr_content);
>       }
>   
>       /* check if the first counter is enabled */
> -    if ( (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
> +    if ( (type == MSR_TYPE_CTRL) &&
>           is_pmu_enabled(msr_content) && !vpmu_is_set(vpmu, VPMU_RUNNING) )
>       {
>           if ( !acquire_pmu_ownership(PMU_OWNER_HVM) )
> @@ -313,7 +341,7 @@ static int amd_vpmu_do_wrmsr(unsigned int msr, uint64_t msr_content,
>       }
>   
>       /* stop saving & restore if guest stops first counter */
> -    if ( (get_pmu_reg_type(msr) == MSR_TYPE_CTRL) &&
> +    if ( (type == MSR_TYPE_CTRL) &&
>           (is_pmu_enabled(msr_content) == 0) && vpmu_is_set(vpmu, VPMU_RUNNING) )
>       {
>           vpmu_reset(vpmu, VPMU_RUNNING);
> @@ -433,7 +461,7 @@ int svm_vpmu_initialise(struct vcpu *v)
>       if ( !counters )
>           return -EINVAL;
>   
> -    ctxt = xzalloc_bytes(sizeof(*ctxt) +
> +    ctxt = xmalloc_bytes(sizeof(*ctxt) +
>                            2 * sizeof(uint64_t) * num_counters);
>       if ( !ctxt )
>       {
> @@ -445,6 +473,7 @@ int svm_vpmu_initialise(struct vcpu *v)
>   
>       ctxt->counters = sizeof(*ctxt);
>       ctxt->ctrls = ctxt->counters + sizeof(uint64_t) * num_counters;
> +    amd_vpmu_init_regs(ctxt);
>   
>       vpmu->context = ctxt;
>       vpmu->priv_context = NULL;
> @@ -457,6 +486,8 @@ int svm_vpmu_initialise(struct vcpu *v)
>   
>   int __init amd_vpmu_init(void)
>   {
> +    unsigned int i;
> +
>       switch ( current_cpu_data.x86 )
>       {
>       case 0x15:
> @@ -490,6 +521,12 @@ int __init amd_vpmu_init(void)
>           return -ENOSPC;
>       }
>   
> +    for ( i = 0; i < num_counters; i++ )
> +    {
> +        rdmsrl(ctrls[i], ctrl_rsvd[i]);
> +        ctrl_rsvd[i] &= CTRL_RSVD_MASK;
> +    }
> +
>       return 0;
>   }
>   

  reply	other threads:[~2015-07-08 15:35 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-19 18:44 [PATCH v25 00/15] x86/PMU: Xen PMU PV(H) support Boris Ostrovsky
2015-06-19 18:44 ` [PATCH v25 01/15] common/symbols: Export hypervisor symbols to privileged guest Boris Ostrovsky
2015-06-19 18:44 ` [PATCH v25 02/15] x86/VPMU: Add public xenpmu.h Boris Ostrovsky
2015-06-19 18:44 ` [PATCH v25 03/15] x86/VPMU: Make vpmu not HVM-specific Boris Ostrovsky
2015-06-19 18:44 ` [PATCH v25 04/15] x86/VPMU: Interface for setting PMU mode and flags Boris Ostrovsky
2015-06-22 15:10   ` Jan Beulich
2015-06-22 16:10     ` Boris Ostrovsky
2015-06-23  8:26       ` Jan Beulich
2015-06-24  2:17         ` Boris Ostrovsky
2015-07-07  7:16   ` Dietmar Hahn
2015-07-09  1:27   ` Tian, Kevin
2015-06-19 18:44 ` [PATCH v25 05/15] x86/VPMU: Initialize VPMUs with __initcall Boris Ostrovsky
2015-07-08 11:48   ` Dietmar Hahn
2015-07-09  6:04   ` Tian, Kevin
2015-06-19 18:44 ` [PATCH v25 06/15] x86/VPMU: Initialize PMU for PV(H) guests Boris Ostrovsky
2015-07-08 12:20   ` Dietmar Hahn
2015-06-19 18:44 ` [PATCH v25 07/15] x86/VPMU: Save VPMU state for PV guests during context switch Boris Ostrovsky
2015-06-22 15:14   ` Jan Beulich
2015-07-08  5:51   ` Dietmar Hahn
2015-06-19 18:44 ` [PATCH v25 08/15] x86/VPMU: When handling MSR accesses, leave fault injection to callers Boris Ostrovsky
2015-07-08  6:11   ` Dietmar Hahn
2015-06-19 18:44 ` [PATCH v25 09/15] x86/VPMU: Add support for PMU register handling on PV guests Boris Ostrovsky
2015-06-19 18:44 ` [PATCH v25 10/15] x86/VPMU: Use pre-computed masks when checking validity of MSRs Boris Ostrovsky
2015-07-08  6:49   ` Dietmar Hahn
2015-06-19 18:44 ` [PATCH v25 11/15] VPMU/AMD: Check MSR values before writing to hardware Boris Ostrovsky
2015-07-08 15:35   ` Aravind Gopalakrishnan [this message]
2015-06-19 18:44 ` [PATCH v25 12/15] x86/VPMU: Handle PMU interrupts for PV(H) guests Boris Ostrovsky
2015-06-22 15:20   ` Jan Beulich
2015-07-09 12:19   ` Dietmar Hahn
2015-06-19 18:44 ` [PATCH v25 13/15] x86/VPMU: Merge vpmu_rdmsr and vpmu_wrmsr Boris Ostrovsky
2015-07-09  6:13   ` Tian, Kevin
2015-06-19 18:44 ` [PATCH v25 14/15] x86/VPMU: Add privileged PMU mode Boris Ostrovsky
2015-07-09  6:13   ` Tian, Kevin
2015-07-09 12:38   ` Dietmar Hahn
2015-06-19 18:44 ` [PATCH v25 15/15] x86/VPMU: Move VPMU files up from hvm/ directory Boris Ostrovsky
2015-07-08  8:03 ` [PATCH v25 00/15] x86/PMU: Xen PMU PV(H) support Jan Beulich
2015-07-10  9:13 ` Dietmar Hahn

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=559D435F.1070708@amd.com \
    --to=aravind.gopalakrishnan@amd.com \
    --cc=JBeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=dgdegra@tycho.nsa.gov \
    --cc=dietmar.hahn@ts.fujitsu.com \
    --cc=kevin.tian@intel.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

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

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).