Linux+PPC64, self-modifying code

Hollis Blanchard hollis at penguinppc.org
Thu Dec 8 15:10:36 EST 2005


On Dec 7, 2005, at 9:37 PM, Dennis Chua wrote:
>
>  Can anyone comment on the feasibility of writing self-modifying
>  code on Linux PPC64? Disregarding the motivations behind this,
>  is it possible for an executable program to
>
>  - access the instruction opcode of one of its functions.
>  - overwrite/alter the function opcode
>
>  and to do this all during runtime?

It's quite feasible. Many projects, including the kernel, do this.

>  Any insight is much appreciated! Thank you.

The main trick is that most PowerPC have L1 instruction caches that are 
incoherent with the L1 data caches. In other words, when you write the 
new code to memory, it lands in the dcache, and then the icache has 
stale instructions which it will happily execute.

The architected sequence you must execute for self-modifying code is 
documented, I believe in Book III of the PowerPC Architecture (see 
http://penguinppc.org/dev/#library). You basically flush the affected 
memory out of the L1 dcache, sync to make sure all that finished, 
invalidate the previous icache contents, then isync to discard 
partially-decoded instructions the processor may have already fetched 
out of the icache. See the Architecture book for the exact 
instructions...

-Hollis




More information about the Linuxppc64-dev mailing list