5 out of 7 rated this helpful - Rate this topic

FlushInstructionCache function

Applies to: desktop apps only

Flushes the instruction cache for the specified process.

Syntax

BOOL WINAPI FlushInstructionCache(
  __in  HANDLE hProcess,
  __in  LPCVOID lpBaseAddress,
  __in  SIZE_T dwSize
);

Parameters

hProcess [in]

A handle to a process whose instruction cache is to be flushed.

lpBaseAddress [in]

A pointer to the base of the region to be flushed. This parameter can be NULL.

dwSize [in]

The size of the region to be flushed if the lpBaseAddress parameter is not NULL, in bytes.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Applications should call FlushInstructionCache if they generate or modify code in memory. The CPU cannot detect the change, and may execute the old code it cached.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

Debugging Functions

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Use in Self- and Cross-Modifying Code
Note: When executing self-modifying code the use of FlushInstructionCache is required on CPU architectures that do not implement a transparent (self-snooping) I-cache.  These include PPC, MIPS, Alpha, and Itanium.  FlushInstructionCache is not necessary on x86 or x64 CPU architectures as these have a transparent cache.  According to the Intel 64 and IA-32 Architectures Software Deverloper's Manual, Volume 3A: System Programming Guide, Part 1, a jump instruction is sufficient to serialize the instruction prefetch queue when executing self-modifying code.

Cross-Modifying Code: Intel defines cross-modifying code as a scenario where Thread 1 on Processor 1 modifies instructions that are then immediately executed by Thread 2 on Processor 2.   The execution of a barrier instruction to serialize the data prefetch queue (LOCK prefix, XCHG, MFENCE, etc) will not serialize the instruction prefetch queue.  To serialize the instruction prefetch queue the other processors must do a context switch (e.g., SuspendThread followed by ResumeThread) or execute a serializing instruction such as CPUID.  See the topic "Handling Self- and Cross-Modifying Code", in the Intel Architecture manual.