forked from fjballest/nix.markII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmcio.c
586 lines (508 loc) · 11.1 KB
/
pmcio.c
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* Performance counters non portable part
*/
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"
#include "amd64.h"
#include "pmc.h"
typedef struct PmcCfg PmcCfg;
typedef struct PmcCore PmcCore;
enum {
PeUnk,
PeAmd,
/*
* See Vol 3B Intel
* 64 Architecture's Software Developer's manual
*/
PeIntel,
};
enum {
_PeUnk,
/* Non architectural */
PeIntelSandy,
PeIntelNehalem,
PeIntelWestmere,
/*
* See BKDG for AMD cfg.family 10 Processors
* section 2.16 and 3.14
*/
PeK10,
};
enum {
PeNregAmd = 4, /* Number of Pe/Pct regs for K10 */
};
enum { /* MSRs */
PerfCtrbaseIntel= 0x000000c1, /* Performance Counters */
PerfEvtbaseIntel= 0x00000186, /* Performance Event Select */
PerfGlobalCtr = 0x0000038f, /* Performance Event Global Ctrl, intel */
PerfEvtbaseAmd = 0xc0010000, /* Performance Event Select */
PerfCtrbaseAmd = 0xc0010004, /* Performance Counters */
};
enum { /* HW Performance Counters Event Selector */
PeHo = 0x0000020000000000ull,/* Host only */
PeGo = 0x0000010000000000ull,/* Guest only */
PeEvMskH = 0x0000000f00000000ull,/* Event mask H */
PeCtMsk = 0x00000000ff000000ull,/* Counter mask */
PeInMsk = 0x0000000000800000ull,/* Invert mask */
PeCtEna = 0x0000000000400000ull,/* Counter enable */
PeInEna = 0x0000000000100000ull,/* Interrupt enable */
PePnCtl = 0x0000000000080000ull,/* Pin control */
PeEdg = 0x0000000000040000ull,/* Edge detect */
PeOS = 0x0000000000020000ull,/* OS mode */
PeUsr = 0x0000000000010000ull,/* User mode */
PeUnMsk = 0x000000000000ff00ull,/* Unit Mask */
PeEvMskL = 0x00000000000000ffull,/* Event Mask L */
PeEvMsksh = 32ull, /* Event mask shift */
};
struct PmcCfg {
int nregs;
u32int ctrbase;
u32int evtbase;
int vendor;
int family;
PmcCtlCtrId *pmcidsarch;
PmcCtlCtrId *pmcids;
};
extern int pmcdebug;
static PmcCfg cfg;
static PmcCore pmccore[MACHMAX];
static void pmcmachupdate(void);
int
pmcnregs(void)
{
u32int info[4];
int nregs;
if(cfg.nregs != 0)
return cfg.nregs; /* don't call cpuid more than necessary */
switch(cfg.vendor){
case PeAmd:
nregs = PeNregAmd;
break;
case PeIntel:
cpuid(0xa, 0, info);
nregs = (info[0]>>8)&0xff;
break;
default:
nregs = 0;
}
if(nregs > PmcMaxCtrs)
nregs = PmcMaxCtrs;
return nregs;
}
static u64int
pmcmsk(void)
{
u32int info[4];
u64int msk;
msk = 0;
switch(cfg.vendor){
case PeAmd:
msk = ~0ULL;
break;
case PeIntel:
cpuid(0xa, 0, info);
msk = (1<<((info[0]>>16)&0xff)) - 1;
break;
}
return msk;
}
PmcCtlCtrId pmcidsk10[] = {
{"locked instr", "0x024 0x1"},
{"locked cycles nonspecul", "0x024 0x4"}, /* in cycles */
{"SMI intr", "0x02b 0x0"},
{"DC access", "0x040 0x0"},
{"DC miss", "0x041 0x0"},
{"DC refills", "0x042 0x1f"},
{"DC evicted", "0x042 0x3f"},
{"L1 DTLB miss", "0x045 0x7"}, /* DTLB L2 hits */
{"L2 DTLB miss", "0x046 0x7"},
{"L1 DTLB hit", "0x04d 0x3"},
{"global TLB flush", "0x054 0x0"},
{"L2 hit", "0x07d 0x3f"},
{"L2 miss", "0x07e 0xf"},
{"IC miss", "0x081 0x0"},
{"IC refill from L2", "0x082 0x0"},
{"IC refill from system", "0x083 0x0"},
{"L1 ITLB miss", "0x084 0x0"}, /* L2 ITLB hits */
{"L2 ITLB miss", "0x085 0x3"},
{"DRAM access", "0x0e0 0x3f"},
//{"L3 miss core 0", "0x4e1 0x13"},
//{"L3 miss core 1", "0x4e1 0x23"},
//{"L3 miss core 2", "0x4e1 0x43"},
//{"L3 miss core 3", "0x4e1 0x83"},
{"L3 miss", "0x4e1 0xf3"}, /* all cores in the socket */
{"", ""},
};
/*18.2.3 Intel Software Deveveloper's Manual */
PmcCtlCtrId pmcidsintel[] = {
{"unhalted cycles", "0x3c 0x0"},
{"instr", "0xc0 0x0"},
{"Llast misses", "0x2e 0x41"},
{"branch instr", "0xc4 0x0"},
{"branch misses", "0xc5 0x0 "},
{"", ""},
};
/* Table 19.7 Intel Software Deveveloper's Manual */
PmcCtlCtrId pmcidsandy[] = {
{"DTLB walk cycles", "0x49 0x4"}, /* all levels */
{"DTLB miss", "0x8 0x2"},
{"DTLB hit", "0x8 0x4"},
{"L2 hit", "0x24 0x4"},
{"L2 miss", "0x24 0x8"},
{"IL2 hit", "0x24 0x10"},
{"IL2 miss", "0x24 0x20"},
{"ITLB miss", "0x85 0x2"},
{"ITLB walk cycles", "0x85 0x4"},
{"ITLB flush", "0xae 0x1"},
{"mem loads", "0xd0 0xf1"}, /* counts μops */
{"mem stores", "0xd0 0xf2"},
{"mem ops", "0xd0 0xf3"},
{"", ""},
};
#define X86MODEL(x) ((((x)>>4) & 0x0F) | (((x)>>16) & 0x0F)<<4)
#define X86FAMILY(x) ((((x)>>8) & 0x0F) | (((x)>>20) & 0xFF)<<4)
static int
pmcintelfamily(void)
{
u32int info, fam, mod;
info = m->cpuinfo[1][0];
fam = X86FAMILY(info);
mod = X86MODEL(info);
if(fam != 0x6)
return PeUnk;
switch(mod){
case 0x2a:
return PeIntelSandy;
case 0x1a:
case 0x1e:
case 0x1f:
return PeIntelNehalem;
case 0x25:
case 0x2c:
return PeIntelWestmere;
}
return PeUnk;
}
void
pmcinitctl(PmcCtl *p)
{
memset(p, 0xff, sizeof(PmcCtl));
p->enab = PmcCtlNullval;
p->user = PmcCtlNullval;
p->os = PmcCtlNullval;
p->nodesc = 1;
}
void
pmcconfigure(void)
{
Mach *mach;
int i, j, isrecog;
isrecog = 0;
if(memcmp(&m->cpuinfo[0][1], "AuthcAMDenti", 12) == 0){
isrecog++;
cfg.ctrbase = PerfCtrbaseAmd;
cfg.evtbase = PerfEvtbaseAmd;
cfg.vendor = PeAmd;
cfg.family = PeUnk;
cfg.pmcidsarch = pmcidsk10;
}else if(memcmp(&m->cpuinfo[0][1], "GenuntelineI", 12) == 0){
isrecog++;
cfg.ctrbase = PerfCtrbaseIntel;
cfg.evtbase = PerfEvtbaseIntel;
cfg.vendor = PeIntel;
cfg.family = pmcintelfamily();
cfg.pmcidsarch = pmcidsintel;
switch(cfg.family){
case PeIntelSandy:
cfg.pmcids = pmcidsandy;
break;
case PeIntelNehalem:
case PeIntelWestmere:
break;
}
}else
cfg.vendor = PeUnk;
cfg.nregs = pmcnregs();
if(isrecog)
pmcupdate = pmcmachupdate;
for(i = 0; i < MACHMAX; i++) {
if((mach = sys->machptr[i]) != nil && mach->online != 0){
for(j = 0; j < cfg.nregs; j++)
pmcinitctl(&pmccore[i].ctr[j]);
}
}
}
static void
pmcenab(void)
{
switch(cfg.vendor){
case PeAmd:
return;
case PeIntel:
wrmsr(PerfGlobalCtr, pmcmsk());
break;
}
}
/* so they can be read from user space */
static int
pmcuserenab(int enable)
{
u64int cr4;
cr4 = cr4get();
if (enable){
cr4 |= Pce;
} else
cr4 &= ~Pce;
cr4put(cr4);
return cr4&Pce;
}
int
pmctrans(PmcCtl *p)
{
PmcCtlCtrId *pi;
int n;
n = 0;
if(cfg.pmcidsarch != nil)
for (pi = &cfg.pmcidsarch[0]; pi->portdesc[0] != '\0'; pi++){
if (strncmp(p->descstr, pi->portdesc, strlen(pi->portdesc)) == 0){
strncpy(p->descstr, pi->archdesc, strlen(pi->archdesc) + 1);
n = 1;
break;
}
}
/* this ones supersede the other ones */
if(cfg.pmcids != nil)
for (pi = &cfg.pmcids[0]; pi->portdesc[0] != '\0'; pi++){
if (strncmp(p->descstr, pi->portdesc, strlen(pi->portdesc)) == 0){
strncpy(p->descstr, pi->archdesc, strlen(pi->archdesc) + 1);
n = 1;
break;
}
}
if(pmcdebug != 0)
print("really setting %s\n", p->descstr);
return n;
}
//PeHo|PeGo
#define PeAll (PeOS|PeUsr)
#define SetEvMsk(v, e) ((v)|(((e)&PeEvMskL)|(((e)<<(PeEvMsksh-8))&PeEvMskH)))
#define SetUMsk(v, u) ((v)|(((u)<<8ull)&PeUnMsk))
#define GetEvMsk(e) (((e)&PeEvMskL)|(((e)&PeEvMskH)>>(PeEvMsksh-8)))
#define GetUMsk(u) (((u)&PeUnMsk)>>8ull)
static int
getctl(PmcCtl *p, u32int regno)
{
u64int r, e, u;
r = rdmsr(regno + cfg.evtbase);
p->enab = (r&PeCtEna) != 0;
p->user = (r&PeUsr) != 0;
p->os = (r&PeOS) != 0;
e = GetEvMsk(r);
u = GetUMsk(r);
/* TODO inverse translation */
snprint(p->descstr, KNAMELEN, "%#ullx %#ullx", e, u);
p->nodesc = 0;
return 0;
}
static int
pmcanyenab(void)
{
int i;
PmcCtl p;
for (i = 0; i < cfg.nregs; i++) {
if (getctl(&p, i) < 0)
return -1;
if (p.enab)
return 1;
}
return 0;
}
static int
setctl(PmcCtl *p, int regno)
{
u64int v, e, u;
char *toks[2];
char str[KNAMELEN];
v = rdmsr(regno + cfg.evtbase);
v &= PeEvMskH|PeEvMskL|PeCtEna|PeOS|PeUsr|PeUnMsk;
if (p->enab != PmcCtlNullval)
if (p->enab)
v |= PeCtEna;
else
v &= ~PeCtEna;
if (p->user != PmcCtlNullval)
if (p->user)
v |= PeUsr;
else
v &= ~PeUsr;
if (p->os != PmcCtlNullval)
if (p->os)
v |= PeOS;
else
v &= ~PeOS;
if (pmctrans(p) < 0)
return -1;
if (p->nodesc == 0) {
memmove(str, p->descstr, KNAMELEN);
if (tokenize(str, toks, 2) != 2)
return -1;
e = atoi(toks[0]);
u = atoi(toks[1]);
v &= ~(PeEvMskL|PeEvMskH|PeUnMsk);
v |= SetEvMsk(v, e);
v |= SetUMsk(v, u);
}
wrmsr(regno+ cfg.evtbase, v);
pmcuserenab(pmcanyenab());
if (pmcdebug) {
v = rdmsr(regno+ cfg.evtbase);
print("conf pmc[%#ux]: %#llux\n", regno, v);
}
return 0;
}
int
pmcdescstr(char *str, int nstr)
{
PmcCtlCtrId *pi;
int ns;
ns = 0;
if(pmcdebug != 0)
print("vendor %x family %x nregs %d pmcnregs %d\n", cfg.vendor, cfg.family, cfg.nregs, pmcnregs());
if(cfg.pmcidsarch == nil && cfg.pmcids == nil){
*str = 0;
return ns;
}
if(cfg.pmcidsarch != nil)
for (pi = &cfg.pmcidsarch[0]; pi->portdesc[0] != '\0'; pi++)
ns += snprint(str + ns, nstr - ns, "%s\n",pi->portdesc);
if(cfg.pmcids != nil)
for (pi = &cfg.pmcids[0]; pi->portdesc[0] != '\0'; pi++)
ns += snprint(str + ns, nstr - ns, "%s\n",pi->portdesc);
return ns;
}
static u64int
getctr(u32int regno)
{
return rdmsr(regno + cfg.ctrbase);
}
static int
setctr(u64int v, u32int regno)
{
wrmsr(regno + cfg.ctrbase, v);
return 0;
}
u64int
pmcgetctr(u32int coreno, u32int regno)
{
PmcCtr *p;
u64int ctr;
if (regno >= cfg.nregs)
error("invalid reg");
p = &pmccore[coreno].ctr[regno];
ilock(&pmccore[coreno]);
if(coreno == m->machno)
ctr = getctr(regno);
else
ctr = p->ctr;
iunlock(&pmccore[coreno]);
return ctr;
}
int
pmcsetctr(u32int coreno, u64int v, u32int regno)
{
PmcCtr *p;
int n;
if (regno >= cfg.nregs)
error("invalid reg");
p = &pmccore[coreno].ctr[regno];
ilock(&pmccore[coreno]);
if(coreno == m->machno)
n = setctr(v, regno);
else{
p->ctr = v;
p->ctrset |= PmcSet;
p->stale = 1;
n = 0;
}
iunlock(&pmccore[coreno]);
return n;
}
static void
ctl2ctl(PmcCtl *dctl, PmcCtl *sctl)
{
if(sctl->enab != PmcCtlNullval)
dctl->enab = sctl->enab;
if(sctl->user != PmcCtlNullval)
dctl->user = sctl->user;
if(sctl->os != PmcCtlNullval)
dctl->os = sctl->os;
if(sctl->nodesc == 0) {
memmove(dctl->descstr, sctl->descstr, KNAMELEN);
dctl->nodesc = 0;
}
}
int
pmcsetctl(u32int coreno, PmcCtl *pctl, u32int regno)
{
PmcCtr *p;
int n;
if (regno >= cfg.nregs)
error("invalid reg");
p = &pmccore[coreno].ctr[regno];
ilock(&pmccore[coreno]);
if(coreno == m->machno)
n = setctl(pctl, regno);
else{
ctl2ctl(&p->PmcCtl, pctl);
p->ctlset |= PmcSet;
p->stale = 1;
n = 0;
}
iunlock(&pmccore[coreno]);
return n;
}
int
pmcgetctl(u32int coreno, PmcCtl *pctl, u32int regno)
{
PmcCtr *p;
int n;
if (regno >= cfg.nregs)
error("invalid reg");
p = &pmccore[coreno].ctr[regno];
ilock(&pmccore[coreno]);
if(coreno == m->machno)
n = getctl(pctl, regno);
else{
memmove(pctl, &p->PmcCtl, sizeof(PmcCtl));
n = 0;
}
iunlock(&pmccore[coreno]);
return n;
}
static void
pmcmachupdate(void)
{
PmcCtr *p;
int coreno, i, maxct;
if((maxct = cfg.nregs) <= 0)
return;
coreno = m->machno;
ilock(&pmccore[coreno]);
for (i = 0; i < maxct; i++) {
p = &pmccore[coreno].ctr[i];
if(p->ctrset & PmcSet)
setctr(p->ctr, i);
if(p->ctlset & PmcSet)
setctl(p, i);
p->ctr = getctr(i);
getctl(p, i);
p->ctrset = PmcIgn;
p->ctlset = PmcIgn;
p->stale = 0;
}
iunlock(&pmccore[coreno]);
}