RDTSC (Re: timer resolution et al.)

Adrian Bocaniciu a.bocaniciu at computer.org
Wed Mar 10 10:18:14 UTC 2004


asm volatile(".byte 0x0F, 0x31")

is the encoding of the Intel instruction "RDTSC".

This instruction is supported by Pentium-compatible processors, i.e. it 
does not work on 486 or earlier processors.

On Pentium-compatible processors RDTSC reads a 64-bit counter into the 
EAX and EDX registers.  The TSC (Time-Stamp Counter) is a special 
register that is incremented at the processor clock frequency, so on 
Pentium processors this is the best way to get the current time.  Also 
on most Pentium-compatible processors there is a better way to get 
time-dependent interrupts, using the internal APIC timer, which is 
documented in the Intel manuals and there are also some open-source 
Linux drivers available on the Web (APIC = Advanced Programmable 
Interrupt Controller).

However, if you want your program to work on 386 or 486 processors, you 
must use either the RTC or the 8254 for reading the time and for timer 
interrupts.  Otherwise you can obtain much better time resolution using 
RDTSC for reading the current time (subnanosecond resolution on current 
processors) and using the 32-bit APIC timer (which increments at the 
processor bus frequency, typically at least 100 MHz and up to 800 MHz 
nowadays) for generating interrupts.

As far as I know (but I did not look at the latest version) RTEMS lacks 
the code needed to use the APIC timer for interrupts, but the existing 
Linux drivers could be easily modified for this purpose.

                                     Best regards !

More information about the users mailing list