beautypg.com

3 handling self- and cross-modifying code – Intel IA-32 User Manual

Page 274

background image

7-6 Vol. 3A

MULTIPLE-PROCESSOR MANAGEMENT

Locked instructions should not be used to insure that data written can be fetched as instructions.

NOTE

The locked instructions for the current versions of the Pentium 4, Intel Xeon,
P6 family, Pentium, and Intel486 processors allow data written to be fetched
as instructions. However, Intel recommends that developers who require the
use of self-modifying code use a different synchronizing mechanism,
described in the following sections.

7.1.3

Handling Self- and Cross-Modifying Code

The act of a processor writing data into a currently executing code segment with the intent of
executing that data as code is called self-modifying code. IA-32 processors exhibit model-
specific behavior when executing self-modified code, depending upon how far ahead of the
current execution pointer the code has been modified. As processor architectures become
more complex and start to speculatively execute code ahead of the retirement point (as in the
Pentium 4, Intel Xeon, and P6 family processors), 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 architecture, one of the
following two coding options must be chosen:

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

(* OPTION 2 *)
Store modified code (as data) into code segment;
Execute a serializing instruction; (* For example, CPUID instruction *)
Execute new code;

(The use of one of these options is not required for programs intended to run on the Pentium or
Intel486 processors, but are recommended to insure compatibility with the Pentium 4, Intel
Xeon, and P6 family processors.)

It should be noted that self-modifying code will execute at a lower level of performance than
non-self-modifying or normal code. The degree of the performance deterioration will depend
upon the frequency of modification and specific characteristics of the code.

The act of one processor writing data into the currently executing code segment of a second
processor with the intent of having the second processor execute that data as code is called
cross-modifying code. As with self-modifying code, IA-32 processors exhibit model-specific
behavior when executing cross-modifying code, depending upon how far ahead of the executing
processors current execution pointer the code has been modified.