enable TSC for cpl!=0 (patch)

Stephan Meyer (sensei@wiesel.de)
Sat, 1 Mar 1997 08:41:32 +0100 (MET)


Due to popular demand :) I have hacked a tiny patch to enable the RDTSC
instruction for all privilege levels (hence for user level code).
This changes ./arch/i386/kernel/head.S. IMHO, this is the best place to do
this because control registers are changed all the time in this code.

SMP people: please enlighten me on how to do this for all CPUs. Is head.S
executed for each CPU? I have no idea about that. Please enlighten me.

I have attached both the patch (against 2.0.29 and (hopefully) 2.1.27) and
an example program to test this.

Cheers, Stephan

-------------- patch - cut here ------------------
--- arch/i386/kernel/head.S~ Mon Mar 18 12:15:00 1996
+++ arch/i386/kernel/head.S Sat Mar 1 08:27:07 1997
@@ -8,6 +8,11 @@
* head.S contains the 32-bit startup code.
*/

+/*
+ * clear TSC flag in cr4 to enable RDTSC for cpl!=0
+ * 01/03/97 by Stephan Meyer <Stephan.Meyer@munich.netsurf.de>
+ */
+
.text
#include <linux/tasks.h>
#include <linux/linkage.h>
@@ -153,7 +158,13 @@
movl %edx,SYMBOL_NAME(x86_vendor_id)+4 # next 4 chars
movl %ecx,SYMBOL_NAME(x86_vendor_id)+8 # last 4 chars

- movl %cr0,%eax # 486+
+ movl SYMBOL_NAME(x86_capability),%eax
+ andl $0x00000010,%eax # check for RDTSC instruction
+ jz 3f
+ movl %cr4,%eax
+ andl $0xFFFFFFFB,%eax # clear TSC flag
+ movl %eax,%cr4
+3: movl %cr0,%eax # 486+
andl $0x80000011,%eax # Save PG,PE,ET
orl $0x50022,%eax # set AM, WP, NE and MP
jmp 2f
--------------- cut here ---------------------------
--------------- example - cut here -----------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void main() {
unsigned long int i;
for(;;) {
__asm__ __volatile__ (
"rdtsc":
"=a" (i):
:
"%eax","%edx");
printf("%x\n",i);
sleep(1);
}
}
-------------------- cut here ------------------------
---------------------have fun -----------------------

-----------------------------------------------
Stephan Meyer
+49-89-4301114
Stephan.Meyer@munich.netsurf.de
http://fatman.mathematik.tu-muenchen.de/~meyer/
-----------------------------------------------