1 /*
2  * Permission is hereby granted, free of charge, to any person obtaining a copy
3  * of this software and associated documentation files (the "Software"), to
4  * deal in the Software without restriction, including without limitation the
5  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6  * sell copies of the Software, and to permit persons to whom the Software is
7  * furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18  * DEALINGS IN THE SOFTWARE.
19  *
20  * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
21  */
22 
23 #ifndef __XEN_PUBLIC_PMU_H__
24 #define __XEN_PUBLIC_PMU_H__
25 
26 #include "xen.h"
27 #if defined(__i386__) || defined(__x86_64__)
28 #include "arch-x86/pmu.h"
29 #elif defined (__arm__) || defined (__aarch64__)
30 #include "arch-arm.h"
31 #else
32 #error "Unsupported architecture"
33 #endif
34 
35 #define XENPMU_VER_MAJ    0
36 #define XENPMU_VER_MIN    1
37 
38 /*
39  * ` enum neg_errnoval
40  * ` HYPERVISOR_xenpmu_op(enum xenpmu_op cmd, struct xenpmu_params *args);
41  *
42  * @cmd  == XENPMU_* (PMU operation)
43  * @args == struct xenpmu_params
44  */
45 /* ` enum xenpmu_op { */
46 #define XENPMU_mode_get        0 /* Also used for getting PMU version */
47 #define XENPMU_mode_set        1
48 #define XENPMU_feature_get     2
49 #define XENPMU_feature_set     3
50 #define XENPMU_init            4
51 #define XENPMU_finish          5
52 #define XENPMU_lvtpc_set       6
53 #define XENPMU_flush           7 /* Write cached MSR values to HW     */
54 /* ` } */
55 
56 /* Parameters structure for HYPERVISOR_xenpmu_op call */
57 struct xen_pmu_params {
58     /* IN/OUT parameters */
59     struct {
60         uint32_t maj;
61         uint32_t min;
62     } version;
63     uint64_t val;
64 
65     /* IN parameters */
66     uint32_t vcpu;
67     uint32_t pad;
68 };
69 typedef struct xen_pmu_params xen_pmu_params_t;
70 DEFINE_XEN_GUEST_HANDLE(xen_pmu_params_t);
71 
72 /* PMU modes:
73  * - XENPMU_MODE_OFF:   No PMU virtualization
74  * - XENPMU_MODE_SELF:  Guests can profile themselves
75  * - XENPMU_MODE_HV:    Guests can profile themselves, dom0 profiles
76  *                      itself and Xen
77  * - XENPMU_MODE_ALL:   Only dom0 has access to VPMU and it profiles
78  *                      everyone: itself, the hypervisor and the guests.
79  */
80 #define XENPMU_MODE_OFF           0
81 #define XENPMU_MODE_SELF          (1<<0)
82 #define XENPMU_MODE_HV            (1<<1)
83 #define XENPMU_MODE_ALL           (1<<2)
84 
85 /*
86  * PMU features:
87  * - XENPMU_FEATURE_INTEL_BTS:  Intel BTS support (ignored on AMD)
88  * - XENPMU_FEATURE_IPC_ONLY:   Restrict PMCs to the most minimum set possible.
89  *                              Instructions, cycles, and ref cycles. Can be
90  *                              used to calculate instructions-per-cycle (IPC)
91  *                              (ignored on AMD).
92  * - XENPMU_FEATURE_ARCH_ONLY:  Restrict PMCs to the Intel Pre-Defined
93  *                              Architectural Performance Events exposed by
94  *                              cpuid and listed in the Intel developer's manual
95  *                              (ignored on AMD).
96  */
97 #define XENPMU_FEATURE_INTEL_BTS  (1<<0)
98 #define XENPMU_FEATURE_IPC_ONLY   (1<<1)
99 #define XENPMU_FEATURE_ARCH_ONLY  (1<<2)
100 
101 /*
102  * Shared PMU data between hypervisor and PV(H) domains.
103  *
104  * The hypervisor fills out this structure during PMU interrupt and sends an
105  * interrupt to appropriate VCPU.
106  * Architecture-independent fields of xen_pmu_data are WO for the hypervisor
107  * and RO for the guest but some fields in xen_pmu_arch can be writable
108  * by both the hypervisor and the guest (see arch-$arch/pmu.h).
109  */
110 struct xen_pmu_data {
111     /* Interrupted VCPU */
112     uint32_t vcpu_id;
113 
114     /*
115      * Physical processor on which the interrupt occurred. On non-privileged
116      * guests set to vcpu_id;
117      */
118     uint32_t pcpu_id;
119 
120     /*
121      * Domain that was interrupted. On non-privileged guests set to DOMID_SELF.
122      * On privileged guests can be DOMID_SELF, DOMID_XEN, or, when in
123      * XENPMU_MODE_ALL mode, domain ID of another domain.
124      */
125     domid_t  domain_id;
126 
127     uint8_t pad[6];
128 
129     /* Architecture-specific information */
130     struct xen_pmu_arch pmu;
131 };
132 
133 #endif /* __XEN_PUBLIC_PMU_H__ */
134 
135 /*
136  * Local variables:
137  * mode: C
138  * c-file-style: "BSD"
139  * c-basic-offset: 4
140  * tab-width: 4
141  * indent-tabs-mode: nil
142  * End:
143  */
144