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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#ifndef P4PERFORMANCECOUNTERS_H
#define P4PERFORMANCECOUNTERS_H
#pragma once
// Pentium 4 support
/*
http://developer.intel.com/design/Pentium4/documentation.htm
IA-32 Intel Architecture Software Developer's Manual Volume 1: Basic Architecture
IA-32 Intel Architecture Software Developer's Manual Volume 2A: Instruction Set Reference, A-M
IA-32 Intel Architecture Software Developer's Manual Volume 2B: Instruction Set Reference, N-Z
IA-32 Intel Architecture Software Developer's Manual Volume 3: System Programming Guide
From Mikael Pettersson's perfctr:
http://user.it.uu.se/~mikpe/linux/perfctr/
* Known quirks:
- OVF_PMI+FORCE_OVF counters must have an ireset value of -1.
This allows the regular overflow check to also handle FORCE_OVF
counters. Not having this restriction would lead to MAJOR
complications in the driver's "detect overflow counters" code.
There is no loss of functionality since the ireset value doesn't
affect the counter's PMI rate for FORCE_OVF counters.
- In experiments with FORCE_OVF counters, and regular OVF_PMI
counters with small ireset values between -8 and -1, it appears
that the faulting instruction is subjected to a new PMI before
it can complete, ad infinitum. This occurs even though the driver
clears the CCCR (and in testing also the ESCR) and invokes a
user-space signal handler before restoring the CCCR and resuming
the instruction.
*/
#define NCOUNTERS 18
// The 18 counters
enum Counters
{
MSR_BPU_COUNTER0,
MSR_BPU_COUNTER1,
MSR_BPU_COUNTER2,
MSR_BPU_COUNTER3,
MSR_MS_COUNTER0,
MSR_MS_COUNTER1,
MSR_MS_COUNTER2,
MSR_MS_COUNTER3,
MSR_FLAME_COUNTER0,
MSR_FLAME_COUNTER1,
MSR_FLAME_COUNTER2,
MSR_FLAME_COUNTER3,
MSR_IQ_COUNTER0,
MSR_IQ_COUNTER1,
MSR_IQ_COUNTER2,
MSR_IQ_COUNTER3,
MSR_IQ_COUNTER4,
MSR_IQ_COUNTER5
};
// register base for counters
#define MSR_COUNTER_BASE 0x300
// register base for CCCR register
#define MSR_CCCR_BASE 0x360
#pragma pack(push, 1)
// access to these bits is through the methods
typedef union ESCR
{
struct
{
uint64 Reserved0_1 : 2; //
uint64 USR : 1; //
uint64 OS : 1; //
uint64 TagEnable : 1; //
uint64 TagValue : 4; //
uint64 EventMask : 16; // from event select
uint64 ESCREventSelect : 6; // 31:25 class of event
uint64 Reserved31 : 1; //
uint64 Reserved32_63 : 32; //
};
uint64 flat;
} ESCR;
typedef union CCCR
{
struct
{
uint64 Reserved0_11 : 12;// 0 -11
uint64 Enable : 1; // 12
uint64 CCCRSelect : 3; // 13-15
uint64 Reserved16_17 : 2; // 16 17
uint64 Compare : 1; // 18
uint64 Complement : 1; // 19
uint64 Threshold : 4; // 20-23
uint64 Edge : 1; // 24
uint64 FORCE_OVF : 1; // 25
uint64 OVF_PMI : 1; // 26
uint64 Reserved27_29 : 3; // 27-29
uint64 Cascade : 1; // 30
uint64 OVF : 1; // 31
uint64 Reserved32_63 : 32; //
};
uint64 flat;
} CCCR;
#pragma pack(pop)
extern const unsigned short cccr_escr_map[NCOUNTERS][8];
enum P4TagState
{
TagDisable, //
TagEnable, //
};
enum P4ForceOverflow
{
ForceOverflowDisable,
ForceOverflowEnable,
};
enum P4OverflowInterrupt
{
OverflowInterruptDisable,
OverflowInterruptEnable,
};
// Turn off the no return value warning in ReadCounter.
#pragma warning( disable : 4035 )
class P4BaseEvent
{
int m_counter;
protected:
void SetCounter(int counter)
{
m_counter = counter;
cccrPort = MSR_CCCR_BASE + m_counter;
counterPort = MSR_COUNTER_BASE + m_counter;
escrPort = cccr_escr_map[m_counter][cccr.CCCRSelect];
}
public:
unsigned short m_eventMask;
const tchar *description;
PME *pme;
ESCR escr;
CCCR cccr;
int counterPort;
int cccrPort;
int escrPort;
P4BaseEvent()
{
pme = PME::Instance();
m_eventMask = 0;
description = _T("");
escr.flat = 0;
cccr.flat = 0;
cccr.Reserved16_17 = 3; // must be set
escrPort = 0;
m_counter = -1;
}
void StartCounter()
{
cccr.Enable = 1;
pme->WriteMSR( cccrPort, cccr.flat );
}
void StopCounter()
{
cccr.Enable = 0;
pme->WriteMSR( cccrPort, cccr.flat );
}
void ClearCounter()
{
pme->WriteMSR( counterPort, 0ui64 ); // clear
}
void WriteCounter( int64 value )
{
pme->WriteMSR( counterPort, value ); // clear
}
int64 ReadCounter()
{
#if PME_DEBUG
if ( escr.USR == 0 && escr.OS == 0 )
return -1; // no area to collect, use SetCaptureMode
if ( escr.EventMask == 0 )
return -2; // no event mask set
if ( m_counter == -1 )
return -3; // counter not legal
#endif
// ReadMSR should work here too, but RDPMC should be faster
int64 value = 0;
pme->ReadMSR( counterPort, &value );
return value;
#if 0
// we need to copy this into a temp for some reason
int temp = m_counter;
_asm
{
mov ecx, temp
RDPMC
}
#endif
}
void SetCaptureMode( PrivilegeCapture priv )
{
switch ( priv )
{
case OS_Only:
{
escr.USR = 0;
escr.OS = 1;
break;
}
case USR_Only:
{
escr.USR = 1;
escr.OS = 0;
break;
}
case OS_and_USR:
{
escr.USR = 1;
escr.OS = 1;
break;
}
}
escr.EventMask = m_eventMask;
pme->WriteMSR( escrPort, escr.flat );
}
void SetTagging( P4TagState tagEnable, uint8 tagValue )
{
escr.TagEnable = tagEnable;
escr.TagValue = tagValue;
pme->WriteMSR( escrPort, escr.flat );
}
void SetFiltering( CompareState compareEnable, CompareMethod compareMethod, uint8 threshold, EdgeState edgeEnable )
{
cccr.Compare = compareEnable;
cccr.Complement = compareMethod;
cccr.Threshold = threshold;
cccr.Edge = edgeEnable;
pme->WriteMSR( cccrPort, cccr.flat );
}
void SetOverflowEnables( P4ForceOverflow overflowEnable, P4OverflowInterrupt overflowInterruptEnable )
{
cccr.FORCE_OVF = overflowEnable;
cccr.OVF_PMI = overflowInterruptEnable;
pme->WriteMSR( cccrPort, cccr.flat );
}
void SetOverflow()
{
cccr.OVF = 1;
pme->WriteMSR( cccrPort, cccr.flat );
}
void ClearOverflow()
{
cccr.OVF = 0;
pme->WriteMSR( cccrPort, cccr.flat );
}
bool isOverflow()
{
CCCR cccr_temp;
pme->ReadMSR( cccrPort, &cccr_temp.flat );
return cccr_temp.OVF;
}
void SetCascade()
{
cccr.Cascade = 1;
pme->WriteMSR( cccrPort, cccr.flat );
}
void ClearCascade()
{
cccr.Cascade = 0;
pme->WriteMSR( cccrPort, cccr.flat );
}
};
#pragma warning( default : 4035 )
#include "EventMasks.h"
#include "EventModes.h"
#endif // P4PERFORMANCECOUNTERS_H
|