Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WBINVD instruction usage

I'm trying to use the WBINV instruction on linux to clear the processor's L1 cache.

The following program compiles, but produces a segmentation fault when I try to run it.

int main() {asm ("wbinvd"); return 1;}

I'm using gcc 4.4.3 and run Linux kernel 2.6.32-33 on my x86 box.

Processor info: Intel(R) Core(TM)2 Duo CPU T5270 @ 1.40GHz

I built the program as follows:

$ gcc

$ ./a.out

Segmentation Fault

Can somebody tell me what I'm doing wrong? How do I get this to run?

P.S: I'm running a few performance tests and want to ensure that the previous content of the processor cache does not influence the results.

like image 219
roelf Avatar asked Jul 19 '11 10:07

roelf


People also ask

What is Wbinvd?

Valid. Write back and flush Internal caches; initiate writing-back and flushing of external caches.

What is Clflush?

Description ¶ Invalidates from every level of the cache hierarchy in the cache coherence domain the cache line that contains the linear address specified with the memory operand. If that cache line contains modified data at any level of the cache hierarchy, that data is written back to memory.


1 Answers

Quoting from Intel® 64 and IA-32 Architectures Software Developer's Manual Combined Volumes 2A and 2B: Instruction Set Reference, A-Z:

The WBINVD instruction is a privileged instruction. When the processor is running in protected mode, the CPL of a program or procedure must be 0 to execute this instruction.

In other words only kernel mode code is allowed to execute it.

EDIT: Previous SO discussion on clearing caches:

"C" programmatically clear L2 cache on Linux machines

How can I do a CPU cache flush in x86 Windows?

How to clear CPU L1 and L2 cache

https://stackoverflow.com/questions/3443130/how-to-clear-cpu-l1-and-l2-cache

like image 94
user786653 Avatar answered Sep 30 '22 17:09

user786653