Intel 64 and IA-32 Architectures Software Developers Manual Volume 3B, System Programming Guide Part 2

Table Of Contents
Vol. 3 18-147
DEBUGGING AND PERFORMANCE MONITORING
18.25.4 Event and Time-Stamp Monitoring Software
To use the performance-monitoring counters and time-stamp counter, the operating
system needs to provide an event-monitoring device driver. This driver should
include procedures for handling the following operations:
Feature checking
Initialize and start counters
Stop counters
Read the event counters
Read the time-stamp counter
The event monitor feature determination procedure must check whether the current
processor supports the performance-monitoring counters and time-stamp counter.
This procedure compares the family and model of the processor returned by the
CPUID instruction with those of processors known to support performance moni-
toring. (The Pentium and P6 family processors support performance counters.) The
procedure also checks the MSR and TSC flags returned to register EDX by the CPUID
instruction to determine if the MSRs and the RDTSC instruction are supported.
The initialize and start counters procedure sets the PerfEvtSel0 and/or PerfEvtSel1
MSRs for the events to be counted and the method used to count them and initializes
the counter MSRs (PerfCtr0 and PerfCtr1) to starting counts. The stop counters
procedure stops the performance counters (see Section 18.25.3, “Starting and Stop-
ping the Performance-Monitoring Counters”).
The read counters procedure reads the values in the PerfCtr0 and PerfCtr1 MSRs, and
a read time-stamp counter procedure reads the time-stamp counter. These proce-
dures would be provided in lieu of enabling the RDTSC and RDPMC instructions that
allow application code to read the counters.
18.25.5 Monitoring Counter Overflow
The P6 family processors provide the option of generating a local APIC interrupt when
a performance-monitoring counter overflows. This mechanism is enabled by setting
the interrupt enable flag in either the PerfEvtSel0 or the PerfEvtSel1 MSR. The
primary use of this option is for statistical performance sampling.
To use this option, the operating system should do the following things on the
processor for which performance events are required to be monitored:
Provide an interrupt vector for handling the counter-overflow interrupt.
Initialize the APIC PERF local vector entry to enable handling of performance-
monitor counter overflow events.
Provide an entry in the IDT that points to a stub exception handler that returns
without executing any instructions.
Provide an event monitor driver that provides the actual interrupt handler and
modifies the reserved IDT entry to point to its interrupt routine.