Linux+PPC64, self-modifying code
David Gibson
david at gibson.dropbear.id.au
Thu Dec 8 15:25:51 EST 2005
On Wed, Dec 07, 2005 at 10:10:36PM -0600, Hollis Blanchard wrote:
> 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...
I believe
<write code>
dcbst <all affected addresses>
sync
icbi <all affected addresses>
isync
<execute code>
is what you need.
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
More information about the Linuxppc64-dev
mailing list