1 /**
2 * \file
3 * \brief Perfomance Monitoring using P5/P6 Measurement Counters.
4 *
5 * Define either CPU_PENTIUM or CPU_P6
6 */
7 /*
8 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
9 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
10 * economic rights: Technische Universität Dresden (Germany)
11 * This file is part of TUD:OS and distributed under the terms of the
12 * GNU Lesser General Public License 2.1.
13 * Please see the COPYING-LGPL-2.1 file for details.
14 */
15 #ifndef __L4UTIL_PERFORM_H
16 #define __L4UTIL_PERFORM_H
17
18 #include <l4/sys/types.h>
19 #include <l4/sys/compiler.h>
20
21 EXTERN_C_BEGIN
22
23 extern const char*strp6pmc_event(l4_uint32_t event);
24
25 #ifndef CONFIG_PERFORM_ONLY_PROTOTYPES
26
27 #if ! (defined CPU_PENTIUM ^ defined CPU_P6 ^ defined CPU_K7)
28
29 #error You must define your target architecture.
30 #error Define EITHER CPU_PENTIUM for Intel Pentium or CPU_P6 for Intel PPro/PII/PIII.
31
32 #else
33
34 /* P5/P6/K7 section */
35
36 /* Makros for access to model specific registers (MSR) */
37
38 /* Write the 64-Bit Model Specific Register. First argument is the register,
39 second the 64-Bit value. This can only be called at priviledge level 0.
40 With L4, the kernel emulates the WRMSR when calling in PL 3.
41 */
l4_i586_wrmsr(unsigned reg,unsigned long long * val)42 static inline void l4_i586_wrmsr(unsigned reg,unsigned long long*val){
43 unsigned long dummyeax, dummyecx, dummyedx;
44
45 asm volatile(
46 ".byte 0xf; .byte 0x30\n" /* wrmsr */
47 : "=a" (dummyeax), "=d" (dummyedx), "=c" (dummyecx)
48 : "2" (reg), "0" (*(unsigned *)val), "1" (*((unsigned *)val+1))
49 );
50 }
51
52 /* Read the 64-Bit Model Specific Register. First argument is the register,
53 second the address to a 64-Bit value. This can only be called at
54 priviledge level 0. With L4, the kernel emulates the RDMSR when calling
55 in PL 3.
56 */
l4_i586_rdmsr(unsigned reg,unsigned long long * val)57 static inline void l4_i586_rdmsr(unsigned reg,unsigned long long*val){
58 unsigned dummy;
59
60 asm volatile(
61 ".byte 0xf; .byte 0x32\n" /* rdmsr */
62 : "=a" (*(unsigned *)val), "=d" (*((unsigned *)val+1)), "=c" (dummy)
63 : "2" (reg)
64 );
65 }
66
67
68 #ifdef CPU_PENTIUM
69 /* Pentium section */
70
71 /* functions and events defined here are only usable at Pentium
72 Processors. P6 architecture does NOT support this kind of measuring and
73 these events. P6 architecture has its own counters and its own events.
74 See P6-section for details. */
75
76 /* from l4linux/arch/l4-i386/include/perform.h */
77
78 static inline void
l4_i586_reset_event_counter(void)79 l4_i586_reset_event_counter(void){
80 asm volatile("xor %%rax, %%rax\n"
81 "xor %%rdx, %%rdx\n"
82 "mov $0x12, %%rcx\n"
83 ".byte 0x0f, 0x30\n"
84 "movl $0x13, %%rcx\n"
85 ".byte 0x0f, 0x30\n"
86 : : : "cx", "ax", "dx"
87 );
88 };
89
90 static inline void
l4_i586_read_event_counter_long(long long * counter0,long long * counter1)91 l4_i586_read_event_counter_long(long long *counter0, long long *counter1)
92 {
93 asm volatile(
94 /* "movl $0, %%eax\n"
95 "movl $0x11, %%ecx\n"
96 ".byte 0x0f, 0x30\n" *//* stop event counting */
97 "mov $0x12, %%rcx\n"
98 ".byte 0x0f, 0x32\n"
99 "mov %%rax, (%%rbx)\n"
100 "mov %%rdx, 4(%%rbx)\n"
101 "mov $0x13, %%ecx\n"
102 ".byte 0x0f, 0x32\n"
103 "mov %%rax, (%%rsi)\n"
104 "mov %%rdx, 4(%%rsi)\n"
105 : /* no output */
106 : "b" (counter0), "S" (counter1)
107 : "ax", "cx", "dx"
108 );
109 }
110
111 static inline void
l4_i586_read_event_counter(int * counter0,int * counter1)112 l4_i586_read_event_counter(int *counter0, int *counter1)
113 {
114 asm volatile("push %%rdx \n"
115 ".byte 0x0f, 0x30 \n"
116 "mov $0x12, %%rcx \n"
117 ".byte 0x0f, 0x32 \n"
118 "mov %%rax, %%rbx \n"
119 "movl $0x13, %%rcx \n"
120 ".byte 0x0f, 0x32\n"
121 "popl %%edx\n"
122 : "=b" (*counter0), "=a" (*counter1)
123 : "1" (0), "c" (0x11)
124 );
125 }
126
127 static inline void
l4_i586_select_event(int event0,int event1)128 l4_i586_select_event(int event0, int event1)
129 {
130 asm volatile(".byte 0x0f, 0x30\n"
131 :
132 :
133 "a" (event0 + (event1 << 16)),
134 "d" (0),
135 "c" (0x11)
136 );
137 };
138
139 #define P5_RD_MISS 0x003 /* 000011B */
140 #define P5_WR_MISS 0x008 /* 000100B */
141 #define P5_RW_MISS 0x029 /* 101001B */
142 #define P5_EX_MISS 0x00e /* 001110B */
143
144 #define P5_D_WBACK 0x006 /* 000110B */
145
146 #define P5_RW_TLB 0x002 /* 00010B */
147 #define P5_EX_TLB 0x00d /* 01101B */
148
149 #define P5_A_STALL 0x01f /* 11111B */
150 #define P5_W_STALL 0x019 /* 11001B */
151 #define P5_R_STALL 0x01a /* 11010B */
152 #define P5_X_STALL 0x01b /* 11011B */
153
154 #define P5_AGI_STALL 0x01f /* 11111B */
155
156 #define P5_PIPLINE_FLUSH 0x015 /* 10101B */
157
158 #define P5_NON_CACHE_RD 0x01e /* 11110B */
159 #define P5_NCACHE_REFS 0x01e /* 11110B */
160 #define P5_LOCKED_BUS 0x01c /* 11100B */
161
162 #define P5_MEM2PIPE 0x009 /* 01001B */
163 #define P5_BANK_CONF 0x00a /* 01010B */
164
165
166 #define P5_INSTRS_EX 0x016 /* 10110B */
167 #define P5_INSTRS_EX_V 0x017 /* 10111B */
168
169
170 #define P5_CNT_NOTHING (0x00 << 6) /* 00B << 6 */
171 #define P5_CNT_EVENT_PL0 (0x01 << 6) /* 01B << 6 */
172 #define P5_CNT_EVENT_PL3 (0x02 << 6) /* 10B << 6 */
173 #define P5_CNT_EVENT (0x03 << 6) /* 11B << 6 */
174 #define P5_CNT_CLOCKS_PL0 (0x05 << 6) /* 101B << 6 */
175 #define P5_CNT_CLOCKS_PL3 (0x06 << 6) /* 110B << 6 */
176 #define P5_CNT_CLOCKS (0x07 << 6) /* 111B << 6 */
177
178
179 #else
180 #if defined CPU_P6
181 /* PPro/PII/PIII section */
182
183 /*-
184 * Copyright (c) 1997 The President and Fellows of Harvard College.
185 * All rights reserved.
186 * Copyright (c) 1997 Aaron B. Brown.
187 *
188 * Redistribution and use in source and binary forms, with or without
189 * modification, are permitted provided that the following conditions
190 * are met:
191 * 1. Redistributions of source code must retain the above copyright
192 * notice, this list of conditions and the following disclaimer.
193 * 2. Redistributions in binary form must reproduce the above copyright
194 * notice, this list of conditions and the following disclaimer in the
195 * documentation and/or other materials provided with the distribution.
196 * 3. All advertising materials mentioning features or use of this software
197 * must display the following acknowledgement:
198 * This product includes software developed by Harvard University
199 * and its contributors.
200 * 4. Neither the name of the University nor the names of its contributors
201 * may be used to endorse or promote products derived from this software
202 * without specific prior written permission.
203 *
204 * THIS SOFTWARE IS PROVIDED BY HARVARD AND CONTRIBUTORS ``AS IS'' AND
205 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
206 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
207 * ARE DISCLAIMED. IN NO EVENT SHALL HARVARD UNIVERSITY OR CONTRIBUTORS BE
208 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
209 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
210 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
211 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
212 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
213 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
214 * POSSIBILITY OF SUCH DAMAGE.
215 */
216
217 /*********************************************************************
218 ** Symbolic names for counter numbers (used in select_p6counter()) **
219 *********************************************************************
220 *
221 * These correspond in order to the Pentium Pro counters. Add new counters at
222 * the end. These agree with the mneumonics in the Pentium Pro Family
223 * Developer's Manual, vol 3.
224 *
225 * Those events marked with a $ require a MESI unit field; those marked with
226 * a @ require a self/any unit field. Those marked with a 0 are only supported
227 * in counter 0; those marked with 1 are only supported in counter 1.
228 */
229
230 /* Data cache unit */
231 #define P6_DATA_MEM_REFS 0x43 /* total memory refs */
232 #define P6_DCU_LINES_IN 0x45 /* all lines allocated in cache unit */
233 #define P6_DCU_M_LINES_IN 0x46 /* M lines allocated in cache unit */
234 #define P6_DCU_M_LINES_OUT 0x47 /* M lines evicted from cache */
235 #define P6_DCU_MISS_OUTSTANDING 0x48 /* #cycles a miss is outstanding */
236
237 /* Instruction fetch unit */
238 #define P6_IFU_IFETCH 0x80 /* instruction fetches */
239 #define P6_IFU_IFETCH_MISS 0x81 /* instruction fetch misses */
240 #define P6_ITLB_MISS 0x85 /* ITLB misses */
241 #define P6_IFU_MEM_STALL 0x86 /* number of cycles IFU is stalled */
242 #define P6_ILD_STALL 0x87 /* #stalls in instr length decode */
243
244 /* L2 Cache */
245 #define P6_L2_IFETCH 0x28 /* ($) l2 ifetches */
246 #define P6_L2_LD 0x29 /* ($) l2 data loads */
247 #define P6_L2_ST 0x2a /* ($) l2 data stores */
248 #define P6_L2_LINES_IN 0x24 /* lines allocated in l2 */
249 #define P6_L2_LINES_OUT 0x26 /* lines removed from l2 */
250 #define P6_L2_M_LINES_INM 0x25 /* modified lines allocated in L2 */
251 #define P6_L2_M_LINES_OUTM 0x27 /* modified lines removed from L2 */
252 #define P6_L2_RQSTS 0x2e /* ($) number of l2 requests */
253 #define P6_L2_ADS 0x21 /* number of l2 addr strobes */
254 #define P6_L2_DBUS_BUSY 0x22 /* number of data bus busy cycles */
255 #define P6_L2_DBUS_BUSY_RD 0x23 /* #bus cycles xferring l2->cpu */
256
257 /* External bus logic */
258 #define P6_BUS_DRDY_CLOCKS 0x62 /* (@) #clocks DRDY is asserted */
259 #define P6_BUS_LOCK_CLOCKS 0x63 /* (@) #clocks LOCK is asserted */
260 #define P6_BUS_REQ_OUTSTANDING 0x60 /* #bus requests outstanding */
261 #define P6_BUS_TRAN_BRD 0x65 /* (@) bus burst read txns */
262 #define P6_BUS_TRAN_RFO 0x66 /* (@) bus read for ownership txns */
263 #define P6_BUS_TRAN_WB 0x67 /* (@) bus writeback txns */
264 #define P6_BUS_TRAN_IFETCH 0x68 /* (@) bus instr fetch txns */
265 #define P6_BUS_TRAN_INVAL 0x69 /* (@) bus invalidate txns */
266 #define P6_BUS_TRAN_PWR 0x6a /* (@) bus partial write txns */
267 #define P6_BUS_TRANS_P 0x6b /* (@) bus partial txns */
268 #define P6_BUS_TRANS_IO 0x6c /* (@) bus I/O txns */
269 #define P6_BUS_TRAN_DEF 0x6d /* (@) bus deferred txns */
270 #define P6_BUS_TRAN_BURST 0x6e /* (@) bus burst txns */
271 #define P6_BUS_TRAN_ANY 0x70 /* (@) total bus txns */
272 #define P6_BUS_TRAN_MEM 0x6f /* (@) total memory txns */
273 #define P6_BUS_DATA_RCV 0x64 /* #busclocks CPU is receiving data */
274 #define P6_BUS_BNR_DRV 0x61 /* #busclocks CPU is driving BNR pin */
275 #define P6_BUS_HIT_DRV 0x7a /* #busclocks CPU is driving HIT pin */
276 #define P6_BUS_HITM_DRV 0x7b /* #busclocks CPU is driving HITM pin*/
277 #define P6_BUS_SNOOP_STALL 0x7e /* #clkcycles bus is snoop-stalled */
278
279 /* FPU */
280 #define P6_FLOPS 0xc1 /* (0) number of FP ops retired */
281 #define P6_FP_COMP_OPS 0x10 /* (0) computational FPOPS exec'd */
282 #define P6_FP_ASSIST 0x11 /* (1) FP excep's handled in ucode */
283 #define P6_MUL 0x12 /* (1) number of FP multiplies */
284 #define P6_DIV 0x13 /* (1) number of FP divides */
285 #define P6_CYCLES_DIV_BUSY 0x14 /* (0) number of cycles divider busy */
286
287 /* Memory ordering */
288 #define P6_LD_BLOCKS 0x03 /* number of store buffer blocks */
289 #define P6_SB_DRAINS 0x04 /* # of store buffer drain cycles */
290 #define P6_MISALING_MEM_REF 0x05 /* # misaligned data memory refs */
291
292 /* Instruction decoding and retirement */
293 #define P6_INST_RETIRED 0xc0 /* number of instrs retired */
294 #define P6_UOPS_RETIRED 0xc2 /* number of micro-ops retired */
295 #define P6_INST_DECODER 0xd0 /* number of instructions decoded */
296
297 /* Interrupts */
298 #define P6_HW_INT_RX 0xc8 /* number of hardware interrupts */
299 #define P6_CYCLES_INT_MASKED 0xc6 /* number of cycles hardints masked */
300 #define P6_CYCLES_INT_PENDING_AND_MASKED 0xc7 /* #cycles masked but pending */
301
302 /* Branches */
303 #define P6_BR_INST_RETIRED 0xc4 /* number of branch instrs retired */
304 #define P6_BR_MISS_PRED_RETIRED 0xc5 /* number of mispred'd brs retired */
305 #define P6_BR_TAKEN_RETIRED 0xc9 /* number of taken branches retired */
306 #define P6_BR_MISS_PRED_TAKEN_RET 0xca /* #taken mispredictions br's retired*/
307 #define P6_BR_INST_DECODED 0xe0 /* number of branch instrs decoded */
308 #define P6_BTB_MISSES 0xe2 /* # of branches that missed in BTB */
309 #define P6_BR_BOGUS 0xe4 /* number of bogus branches */
310 #define P6_BACLEARS 0xe6 /* # times BACLEAR is asserted */
311
312 /* Stalls */
313 #define P6_RESOURCE_STALLS 0xa2 /* # resource-related stall cycles */
314 #define P6_PARTIAL_RAT_STALLS 0xd2 /* # cycles/events for partial stalls*/
315
316 /* Segment register loads */
317 #define P6_SEGMENT_REG_LOADS 0x06 /* number of segment register loads */
318
319 /* Clocks */
320 #define P6_CPU_CLK_UNHALTED 0x79 /* #clocks CPU is not halted */
321
322 /* Unit field tags */
323 #define P6_UNIT_M 0x0800
324 #define P6_UNIT_E 0x0400
325 #define P6_UNIT_S 0x0200
326 #define P6_UNIT_I 0x0100
327 #define P6_UNIT_MESI 0x0f00
328
329 #define P6_UNIT_SELF 0x0000
330 #define P6_UNIT_ANY 0x2000
331
332 /****************************************************************************
333 ** Flag bit definitions (used for the 'flag' field in select_p6counter()) **
334 ****************************************************************************
335 *
336 * The driver accepts fully-formed counter specifications from user-level.
337 * The following flags are mneumonics for the bits that get set in the
338 * PerfEvtSel0 and PerfEvtSel1 MSR's
339 *
340 */
341 #define P6CNT_U 0x010000 /* Monitor user-level events */
342 #define P6CNT_K 0x020000 /* Monitor kernel-level events */
343 #define P6CNT_E 0x040000 /* Edge detect: count state transitions */
344 #define P6CNT_PC 0x080000 /* Pin control: ?? */
345 #define P6CNT_IE 0x100000 /* Int enable: enable interrupt on overflow */
346 #define P6CNT_F 0x200000 /* Freeze counter (handled in software) */
347 #define P6CNT_EN 0x400000 /* enable counters (in PerfEvtSel0) */
348 #define P6CNT_IV 0x800000 /* Invert counter mask comparison result */
349
350 /*****************************
351 ** Miscellaneous constants **
352 *****************************
353 *
354 * Number of Pentium Pro programable hardware counters.
355 */
356 #define NUM_P6HWC 2
357
358 /*****************************************************************************
359 *
360 * End of Copyright by Harvard College
361 *
362 *****************************************************************************/
363
364
365 #define MSR_P6_EVNTSEL0 0x186
366 #define MSR_P6_EVNTSEL1 0x187
367 #define MSR_P6_PERFCTR0 0xc1
368 #define MSR_P6_PERFCTR1 0xc2
369
370 /* P6-specific Makros to manipulate and read counters */
371
372 /* Read the 40 bit performance monitoring counter. This requires
373 the PCE-flag in CR4 to be set. Otherwise GP0 is raised. Works only
374 at P6.
375 */
376 #define l4_i686_rdpmc(cntr, res_p) \
377 __asm __volatile( \
378 "mov %2, %%rcx # put counter number in \n\
379 .byte 0xf; .byte 0x33 # RDPMC instruction \n\
380 mov %%rdx, %1 # High order 32 bits \n\
381 mov %%rax, %0 # Low order 32 bits" \
382 : "=g" (*(int *)(res_p)), "=g" (*(((int *)res_p)+1)) \
383 : "g" (cntr) \
384 : "ecx", "eax", "edx")
385
l4_i686_rdpmc_32(int cntr)386 static inline l4_uint32_t l4_i686_rdpmc_32(int cntr){
387 l4_uint32_t x;
388
389 __asm__ __volatile__(
390 ".byte 0xf; .byte 0x33 # RDPMC instruction"
391 : "=a" (x)
392 : "c" (cntr)
393 : "rcx", "rax", "rdx");
394 return x;
395 }
396
l4_i686_select_perfctr_event(int counter,unsigned long long val)397 static inline void l4_i686_select_perfctr_event(int counter,
398 unsigned long long val){
399 l4_i586_wrmsr(MSR_P6_EVNTSEL0+counter, &val);
400 }
401
l4_i686_select_perfctr0_event(long long * val)402 static inline void l4_i686_select_perfctr0_event(long long *val){
403 asm volatile(
404 "mov $MSR_P6_EVNTSEL0, %%rcx\n"
405 "mov (%%rbx), %%rax\n"
406 "mov 4(%%rbx), %%rdx\n"
407 //".byte 0xcc, 0xeb, 0x01, 0x21\n"
408 ".byte 0x0f, 0x30\n" // wrmsr
409 //".byte 0xcc, 0xeb, 0x01, 0x21\n"
410 : /* no output */
411 : "b" (val)
412 : "ax", "cx", "dx", "bx"
413 );
414
415 }
416
417 /* end of P6 section */
418 #else
419
420 #define K7CNT_U 0x010000 /* Monitor user-level events */
421 #define K7CNT_K 0x020000 /* Monitor kernel-level events */
422 #define K7CNT_E 0x040000 /* Edge detect: count state transitions */
423 #define K7CNT_PC 0x080000 /* Pin control: ?? */
424 #define K7CNT_IE 0x100000 /* Int enable: enable interrupt on overflow */
425 #define K7CNT_F 0x200000 /* Freeze counter (handled in software) */
426 #define K7CNT_EN 0x400000 /* enable counters (in PerfEvtSel0) */
427 #define K7CNT_IV 0x800000 /* Invert counter mask comparison result */
428
429 #define MSR_K7_EVNTSEL0 0xC0010000
430 #define MSR_K7_EVNTSEL1 0xC0010001
431 #define MSR_K7_EVNTSEL2 0xC0010002
432 #define MSR_K7_EVNTSEL3 0xC0010003
433 #define MSR_K7_PERFCTR0 0xC0010004
434 #define MSR_K7_PERFCTR1 0xC0010005
435 #define MSR_K7_PERFCTR2 0xC0010006
436 #define MSR_K7_PERFCTR3 0xC0010007
437
438 #endif
439
440 #endif
441
442 /* end of P5/P6/K7 section*/
443 #endif
444
445 /* end of not only lib-prototypes section */
446 #endif
447
448 EXTERN_C_END
449
450 #endif
451