Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a zero change jump on x86 clear the instruction prefetch queue?

Tags:

x86

assembly

On the x86, can someone confirm, whether or not a zero displacement jump (i.e. a jump that doesn't alter the values in CS or IP) clears the Instruction Prefetch Queue?

like image 926
James Georgas Avatar asked Apr 05 '17 21:04

James Georgas


1 Answers

A jump to the next statement that would have been executed anyways does clear the instruction prefetch queue on any Intel x86 CPU that has one. It was a common to do so in self-modifying code in order to ensure that modified code was actually executed. Intel has gone so far as to document using a jump as means to ensure that self-modified code gets executed correctly even on modern CPUs.

From Intel 64 and IA-32 Architectures Software Developer’s Manual Volume 3: System Programming Guide:

8.1.3 Handling Self- and Cross-Modifying Code

...

As processor microarchitectures become more complex and start to speculatively execute code ahead of the retirement point (as in P6 and more recent processor families), the rules regarding which code should execute, pre- or post-modification, become blurred. To write self-modifying code and ensure that it is compliant with current and future versions of the IA-32 architectures, use one of the following coding options:

(* OPTION 1 *)
Store modified code (as data) into code segment;
Jump to new code or an intermediate location;
Execute new code;

(Option 2 is to use a serializing instruction instead of a jump, but these don't exist on early x86 CPUs.)

like image 200
Ross Ridge Avatar answered Sep 20 '22 00:09

Ross Ridge