Page 1 of 1

Arm Self Modifying code

PostPosted: May 9, 2004 @ 10:25pm
by GlenCook
Hi,

Has anyone successfully got self modifying code to run on a PPC. I've been having a go at it, but keep getting access violations.

Here is a snippet of code that is causing the problems

3402F764 E3833A02 orr r3, r3, #2, 20
3402F768 E1CF31B4 strh r3, [pc, #0x14]
3402F76C E38CC040 orr r12, r12, #0x40
3402F770 E1A00086 mov r0, r6, lsl #1
3402F774 E1A02586 mov r2, r6, lsl #11
3402F778 E1800002 orr r0, r0, r2
3402F77C E1A02B86 mov r2, r6, lsl #23
3402F780 E1800002 orr r0, r0, r2
3402F784 E3A02000 mov r2, #0

The code at #3402f768 is storing a count value to modify the last mov r2,#number.

Due to paging, the PC is set to #0002f768

Anyone have any ideas?

Thanks

Glen.

PostPosted: May 9, 2004 @ 11:41pm
by refractor
Essentially you need an "IMB" (Instruction Memory Barrier) between your code modification and hitting the code that you've modified... well, unless the block of memory you're executing is uncached, and unbuffered (i.e. slow as hell).

If the memory is buffered (but not cached), you'll "only" have to flush the write buffer to get the data out and back into the right place, before you run it.

If the memory is cached, due to the fun of having a separate instruction and data cache, you have to flush the data cache, flush the write buffer, and then flush the instruction cache.

Thus, self-modification is costly, don't bother unless you're going to use the modified-code lots.

Apart from that the code looks reasonable to me - you've adjusted for the pipeline, which is what gets most people.

PostPosted: May 9, 2004 @ 11:52pm
by GlenCook

PostPosted: May 10, 2004 @ 1:53am
by Tala

PostPosted: May 10, 2004 @ 2:47am
by hm

PostPosted: May 10, 2004 @ 11:38am
by drgoldie

PostPosted: May 10, 2004 @ 5:01pm
by hm