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.
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.
Hi mate,
Thanks for the advice, think I'll just stick to using a memory variable to store the count, it's only in the outer loop of the renderer, so it's not that important.
It's just that I ran out of registers for the blitting function it was a part of. I suppose I can put up with an extra couple of cycles for each scanline.
Vtune is a wonderfull tool for getting rid of any pipeline stalls, I use the static module viewer, and now all of my renderers have no stalls whatsoever.
Cheers
Glen.
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.
The code page is read only, therefore you get access violations. But even if it were read/write you would have to make sure, that the caches are coherent, like refractor pointed out.
@refractor
Are you sure one can clean/invalidate the cache from user mode? Directly accessing the coprocessor registers for cache control seems not to work properly. Is there an API call for this purpose?
Tala wrote: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.
The code page is read only, therefore you get access violations. But even if it were read/write you would have to make sure, that the caches are coherent, like refractor pointed out.
@refractor
Are you sure one can clean/invalidate the cache from user mode? Directly accessing the coprocessor registers for cache control seems not to work properly. Is there an API call for this purpose?
I use self-modifying code a lot for the renderer I am working on. Memory in which the generated code resides is allocated through
- Code:
m_Code = reinterpret_cast<U8 *>(VirtualAlloc(0, totalSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE));
and after changes of the code
- Code:
CacheSync(CACHE_SYNC_INSTRUCTIONS | CACHE_SYNC_WRITEBACK);
is being called to flush/sync. the two caches. Seems to work pretty reliably. Any problem beyond that is plain' old bugs... at least in my case.
VTune: Anybody an idea which devices can be used for *dynamic* profiling using VTune?
- HM
VTune: Anybody an idea which devices can be used for *dynamic* profiling using VTune?
for PPC2002 you can use the ViewSonic 35.
for PPC2003 you can use the Dell Axim X3.
the current intel performance optimizing toolkit comes with the dell axim. that's why i ordered such a device. they are quite cheap. i already got mine but did not have the time to try it out yet.
one more information: you need an additional DLL which is not on the dell axim OS image by default. this DLL exposes the PMU functions which are needed by the client sampling tool of VTune. intel was so nice to send me that DLL...
bye,
Daniel
drgoldie wrote:VTune: Anybody an idea which devices can be used for *dynamic* profiling using VTune?
for PPC2002 you can use the ViewSonic 35.
for PPC2003 you can use the Dell Axim X3.
the current intel performance optimizing toolkit comes with the dell axim. that's why i ordered such a device. they are quite cheap. i already got mine but did not have the time to try it out yet.
Let me know if/when you got it working.
Cheers,
HM