1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* Copyright (c) 2026 Advanced Micro Devices, Inc.
*
* SPDX-License-Identifier: BSD-2-Clause
*
* AMD/Intel MPERF and APERF MSRs counters exposed as an hwpmc(4) PMC class.
*
* Read-only, system-scope (PMC_MODE_SC), 64-bit counters reporting
* MPERF and APERF MSR values.
*/
#include <sys/param.h>
#include <sys/pmc.h>
#include <sys/pmckern.h>
#include <sys/priv.h>
#include <machine/specialreg.h>
#define PERF_CAPS PMC_CAP_READ
struct perf_descr {
struct pmc_descr pm_descr; /* "base class" */
};
static const struct perf_descr perf_pmcdesc[PERF_NPMCS] = {
{
.pm_descr = {
.pd_name = "MPERF",
.pd_class = PMC_CLASS_PERF,
.pd_caps = PERF_CAPS,
.pd_width = 64
},
},
{
.pm_descr = {
.pd_name = "APERF",
.pd_class = PMC_CLASS_PERF,
.pd_caps = PERF_CAPS,
.pd_width = 64
}
}
};
struct perf_cpu {
struct pmc_hw tc_hw[PERF_NPMCS];
};
static struct perf_cpu **perf_pcpu;
static int perf_classindex;
static int
perf_allocate_pmc(int cpu __diagused, int ri __diagused,
struct pmc *pm __unused, const struct pmc_op_pmcallocate *a)
{
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU value %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS,
("[perf,%d] illegal row index %d", __LINE__, ri));
if (a->pm_class != PMC_CLASS_PERF)
return (EINVAL);
if ((a->pm_ev < PMC_EV_PERF_FIRST || a->pm_ev > PMC_EV_PERF_LAST) ||
a->pm_mode != PMC_MODE_SC)
return (EINVAL);
/*
* Allows the PERF class to be accessed only by privileged users.
* Frequency is an indirect power proxy and could be abused by
* attacks like Hertzbleed.
*/
if (priv_check(curthread, PRIV_PMC_SYSTEM) != 0)
return (EPERM);
if ((a->pm_caps & PERF_CAPS) == 0)
return (EINVAL);
if ((a->pm_caps & ~PERF_CAPS) != 0)
return (EPERM);
switch (ri) {
case PERF_MPERF:
if (a->pm_ev != PMC_EV_PERF_MPERF)
return (EINVAL);
break;
case PERF_APERF:
if (a->pm_ev != PMC_EV_PERF_APERF)
return (EINVAL);
break;
default:
return (EINVAL);
}
return (0);
}
static int
perf_config_pmc(int cpu, int ri, struct pmc *pm)
{
struct pmc_hw *phw;
PMCDBG3(MDP, CFG, 1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU value %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS, ("[perf,%d] illegal row-index %d",
__LINE__, ri));
phw = &perf_pcpu[cpu]->tc_hw[ri];
KASSERT(pm == NULL || phw->phw_pmc == NULL,
("[perf,%d] pm=%p phw->pm=%p hwpmc not unconfigured", __LINE__,
pm, phw->phw_pmc));
phw->phw_pmc = pm;
return (0);
}
static int
perf_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
{
const struct perf_descr *pd;
struct pmc_hw *phw;
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS, ("[perf,%d] illegal row-index %d",
__LINE__, ri));
phw = &perf_pcpu[cpu]->tc_hw[ri];
pd = &perf_pmcdesc[ri];
strlcpy(pi->pm_name, pd->pm_descr.pd_name, sizeof(pi->pm_name));
pi->pm_class = pd->pm_descr.pd_class;
if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
pi->pm_enabled = TRUE;
*ppmc = phw->phw_pmc;
} else {
pi->pm_enabled = FALSE;
*ppmc = NULL;
}
return (0);
}
static int
perf_get_config(int cpu, int ri, struct pmc **ppm)
{
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS, ("[perf,%d] illegal row-index %d",
__LINE__, ri));
*ppm = perf_pcpu[cpu]->tc_hw[ri].phw_pmc;
return (0);
}
static int
perf_get_msr(int ri __diagused, uint32_t *msr __unused)
{
KASSERT(ri >= 0 && ri < PERF_NPMCS,
("[perf,%d] ri %d out of range", __LINE__, ri));
return (EINVAL);
}
static int
perf_pcpu_fini(struct pmc_mdep *md, int cpu)
{
int i, ri;
struct pmc_cpu *pc;
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal cpu %d", __LINE__, cpu));
KASSERT(perf_pcpu[cpu] != NULL, ("[perf,%d] null pcpu", __LINE__));
free(perf_pcpu[cpu], M_PMC);
perf_pcpu[cpu] = NULL;
ri = md->pmd_classdep[perf_classindex].pcd_ri;
pc = pmc_pcpu[cpu];
for (i = 0; i < PERF_NPMCS; i++) {
pc->pc_hwpmcs[i + ri] = NULL;
}
return (0);
}
static int
perf_pcpu_init(struct pmc_mdep *md, int cpu)
{
int i, ri;
struct pmc_cpu *pc;
struct perf_cpu *perf_pc;
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal cpu %d", __LINE__, cpu));
KASSERT(perf_pcpu, ("[perf,%d] null pcpu", __LINE__));
KASSERT(perf_pcpu[cpu] == NULL, ("[perf,%d] non-null per-cpu",
__LINE__));
perf_pc = malloc(sizeof(struct perf_cpu), M_PMC, M_WAITOK | M_ZERO);
perf_pcpu[cpu] = perf_pc;
ri = md->pmd_classdep[perf_classindex].pcd_ri;
KASSERT(pmc_pcpu, ("[perf,%d] null generic pcpu", __LINE__));
pc = pmc_pcpu[cpu];
KASSERT(pc, ("[perf,%d] null generic per-cpu", __LINE__));
for (i = 0; i < PERF_NPMCS; i++) {
perf_pc->tc_hw[i].phw_state = PMC_PHW_FLAG_IS_ENABLED |
PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(i) |
PMC_PHW_FLAG_IS_SHAREABLE;
pc->pc_hwpmcs[i + ri] = &perf_pc->tc_hw[i];
}
return (0);
}
static int
perf_read_pmc(int cpu __diagused, int ri, struct pmc *pm, pmc_value_t *v)
{
enum pmc_mode mode __diagused;
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU value %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS, ("[perf,%d] illegal ri %d",
__LINE__, ri));
mode = PMC_TO_MODE(pm);
KASSERT(mode == PMC_MODE_SC,
("[perf,%d] illegal pmc mode %d", __LINE__, mode));
PMCDBG1(MDP, REA, 1, "perf-read id=%d", ri);
switch (ri) {
case PERF_MPERF:
*v = rdmsr(MSR_MPERF);
break;
case PERF_APERF:
*v = rdmsr(MSR_APERF);
break;
default:
return (EINVAL);
}
return (0);
}
static int
perf_write_pmc(int cpu __diagused, int ri __diagused, struct pmc *pm __unused,
pmc_value_t v __unused)
{
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU value %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS, ("[perf,%d] illegal row-index %d",
__LINE__, ri));
return (0);
}
static int
perf_release_pmc(int cpu __diagused, int ri __diagused, struct pmc *pmc __unused)
{
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU value %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS,
("[perf,%d] illegal row-index %d", __LINE__, ri));
KASSERT(perf_pcpu[cpu]->tc_hw[ri].phw_pmc == NULL,
("[perf,%d] PHW pmc non-NULL", __LINE__));
return (0);
}
static int
perf_start_pmc(int cpu __diagused, int ri __diagused, struct pmc *pm __unused)
{
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU value %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS, ("[perf,%d] illegal row-index %d",
__LINE__, ri));
return (0);
}
static int
perf_stop_pmc(int cpu __diagused, int ri __diagused, struct pmc *pm __unused)
{
KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
("[perf,%d] illegal CPU value %d", __LINE__, cpu));
KASSERT(ri >= 0 && ri < PERF_NPMCS, ("[perf,%d] illegal row-index %d",
__LINE__, ri));
return (0);
}
int
pmc_perf_initialize(struct pmc_mdep *md, int maxcpu, int classindex)
{
struct pmc_classdep *pcd;
KASSERT(md != NULL, ("[perf,%d] md is NULL", __LINE__));
KASSERT(md->pmd_nclass >= 1, ("[perf,%d] dubious md->nclass %d",
__LINE__, md->pmd_nclass));
if ((cpu_power_ecx & CPUID_PERF_STAT) && (tsc_perf_stat == 1)) {
perf_pcpu = malloc(sizeof(struct perf_cpu *) * maxcpu, M_PMC,
M_ZERO | M_WAITOK);
perf_classindex = classindex;
pcd = &md->pmd_classdep[classindex];
pcd->pcd_caps = PMC_CAP_READ;
pcd->pcd_class = PMC_CLASS_PERF;
pcd->pcd_num = PERF_NPMCS;
pcd->pcd_ri = md->pmd_npmc;
pcd->pcd_width = 64;
pcd->pcd_allocate_pmc = perf_allocate_pmc;
pcd->pcd_config_pmc = perf_config_pmc;
pcd->pcd_describe = perf_describe;
pcd->pcd_get_config = perf_get_config;
pcd->pcd_get_msr = perf_get_msr;
pcd->pcd_pcpu_init = perf_pcpu_init;
pcd->pcd_pcpu_fini = perf_pcpu_fini;
pcd->pcd_read_pmc = perf_read_pmc;
pcd->pcd_write_pmc = perf_write_pmc;
pcd->pcd_release_pmc = perf_release_pmc;
pcd->pcd_start_pmc = perf_start_pmc;
pcd->pcd_stop_pmc = perf_stop_pmc;
md->pmd_npmc += PERF_NPMCS;
}
return (0);
}
void pmc_perf_finalize(struct pmc_mdep *md)
{
PMCDBG0(MDP, INI, 1, "perf-finalize");
if (perf_pcpu != NULL) {
for (int i = 0; i < pmc_cpu_max(); i++)
KASSERT(perf_pcpu[i] == NULL,
("[perf,%d] non-null pcpu cpu %d", __LINE__, i));
free(perf_pcpu, M_PMC);
perf_pcpu = NULL;
}
}
|