PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

A big Problem with VirtualAlloc and PocketPC 2003!

I'm curently Fighting With PocketPC 2003 and VirtualAlloc/protect.
I use them within the Dynamic Recompiler for FpseCE (upcomming Playstation Emulator).
First i Allocate a 2MB Buffer. This buffer is use to Store Recompiled Block.
I Declare this buffer with VirtualAlloc, then when i need to write a new block, i unprotect it with VirtualProtect, and at the end of this writing block i protect it.
On PocketPC 2002 that work very well, on PocketPC 2003, to make it working i need to add the NOCACHE flag when i protect it :( a Big slowdown is resulting....
Here are my routine:

UINT8 *win_code_alloc(int size)
{
LPCVOID lpAddress=VirtualAlloc(NULL,size,MEM_RESERVE,PAGE_EXECUTE_READWRITE);
return (UINT8 *)VirtualAlloc(lpAddress,size,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
}

void win_code_free(UINT8 *buf)
{
VirtualFree(buf,0,MEM_RELEASE);
}

void win_code_protect(UINT8 * lpPage,int dwSize){

DWORD dwOldProtect;

VirtualProtect (lpPage,dwSize,PAGE_EXECUTE_READ,&dwOldProtect);
}

void win_code_unprotect(UINT8 * lpPage,int dwSize){

DWORD dwOldProtect;

VirtualProtect (lpPage,dwSize,PAGE_EXECUTE_READWRITE,&dwOldProtect);
}


For now with my News PocketPC (ASUS A620 PPC2003), i can only use the Dynamic Interpreter , it's extremely Fast!! all game are already very playable, Ridge Racer run at 30 fps, Quake2 17-20 fps, Wipeout 30fps, Destruction Derby 30-40 fps etc.... I can't Imagine when my recompiler will run on PPC2003!! that will let me the possibility to add sound :) :) :)

thanks your any help!!

schtruck.
to make it working i need to add the NOCACHE flag when i protect it


What's the problem when you don't use the PAGE_NOCACHE flag? (Or "why must you use the PAGE_NOCACHE", if you prefer :))
the problem without NOCACHE is that it's like the Page would not be protected .... that crash when the code is running in the Compiled buffer because ARM registers are at anytime modified.
if i run the emulator in Debug mode, that crash for example at a time. If i put a BP just before this time, and i press GO , that continue until a new Crash but later...

I had this problem in PPC2002 because i used first the Malloc Function to allocate and at first i didn't used VirtualProtect etc...
when i have changed my Malloc to VirtualAlloc+ VirtualProtect , all worked well.

If you have any idear.

Thanks your any help.
i don't understand why you need to protect your re-compiled code. Are you trying to catch self-modifying code?

Cheers,

rcp
no, i don't use self modifying code.... but in PPC2002 it was needed coz my arm Reg use in recompilation where sometime modified.... it like MS don't respect the Registers Specification, and when an Interrupt comes registers aren't restored correctly or Directly not restored.....

if you have an idear
I was refering to the target PSX code self-modifying, not your code. I don't see why you need to protect memory. Protecting memory doesn't change the way CE deals with registers, so I don't see how that would help your situation. In addition, if CE INT service code was not following the specs for restoring ARM registers, then *no* code would ever run successfully. For that matter, INT service routines must restore *all* registers upon their return. My guess is that you have a problem with the recompiler that is trashing the code you generated. I had this same problem with my N64 emulator. I found it by protecting the RAM in which the recompiled code was located and then set a breakpoint for access to that area.

I would imagine that using VirtualAlloc+VirtualProtect is just giving your compiled code a different location in memory, one that isn't trashed as easily. For example, if you use malloc, then the address returned is going to be succesive from you other memory allocations. If one of the earlier allocation areas is overwritten, it will trash the recompiled code. When you changed to VirtualAlloc, the address you were given for your recompiled code was located in a "safer" area.

My suggestion is to go back to using malloc and look at the buffers allocated before the recompiled code. I bet one of those buffers is overflowing and trashing your compiled code.

Good luck.
Sergey Chaban · 07 Aug 2003, 11:02 UTC
In fact VirtualProtect is not even necessary, I've implemented 3 different JIT compilers for ARM (Small, Mophun, and ARM backend for Mono) and in all cases code buffer is allocated with a simple malloc/VirtualProtect pattern without any problems, but memory returned by malloc is already executable and VirtualProtect is only needed to play safe (malloced codebuffer works fine under CE 3.0 and 4.x).
Also, RR at 30 FPS and Q2 faster than native port sounds too good to be true 8)
ok we have perhaps found , it's surely a cache probleme, we tried to use the new Wince .net 4.2 function ! OEMCacheRangeFlush to flush the cache, but it's not available in our Kit 4.2 with PPC2003.... and what about you?

or do you know a faster method to flush Caches?
I have PSX Q2. It is a completely different engine. It uses the standard PSX 3D implementation (the same perspective-correction-every-8th-pixel is used as with other PSX 3D games, etc). The levels are also completely different, and redesigned specifically for the PSX engine (lower polycount, doors added to reduce polycounts of vis portals, etc). So comparison is not possible between PSX Q2 and true ports.

Dan East
Flushing caches...

On an ARM system, look to the "coprocessor" (MCR) commands.

On the StrongARM, you want coprocessor 7 for the cache control stuff (no idea about the XScale - should be the same but knowing Intel..).

mcr 15, 0, r0, c7, c5, 0 ; // flush the I cache
mcr 15, 0, r0, c7, c10, 4; // flush the write buffer

ISTR the data flush being horribly slow, so to flush the data just read a (contiguous in virtual) cache-sized block of data from RAM. Something like:

;r0 = address of your buffer
;r1 = number of 32-byte lines to flush

.flush
ldr r2,[r0],#32 // 32 is a cache line size
subs r1,r1,#1
bne flush

You can also flush single entries - it's all in the StrongARM developer manual at page 59.

Oh, and Microsoft ported the Q2 engine to their CLR:
http://msdn.microsoft.com/visualc/quake/default.aspx
Sergey Chaban · 08 Aug 2003, 12:39 UTC
I've seen the following code used to flush icache on SA (doesn't work on XScale):
Code:

flush_icache
mov r0, lr
ldr r1, =dummy_fn
mov lr, pc
& 0xF1A0F001 ; movnv pc, r1
mov pc, r0
dummy_fn
mov pc, lr


But under WinCE why don't you just call FlushInstructionCache? It's supposed to use the fastest method available (via OEM layer) and ensures compatibility with future CPUs.
Oooer, a movnv with the PC, which makes it "unpredictable", IIRC. :) I'm not surprised it doesn't work on XScale.
Sergey Chaban wrote:
But under WinCE why don't you just call FlushInstructionCache? It's supposed to use the fastest method available (via OEM layer) and ensures compatibility with future CPUs.


I also wonder... There is CacheSync() on all versions of PPC:

Code:

/* Flags for CacheSync */
#define CACHE_SYNC_DISCARD 0x001 /* write back & discard all cached data */
#define CACHE_SYNC_INSTRUCTIONS 0x002 /* discard all cached instructions */
#define CACHE_SYNC_WRITEBACK 0x004 /* write back but don't discard data cache*/
void CacheSync(int flags);


And WM2003 even has more control with CacheRangeFlush() or something like that.
OK i saw those Function on MSDN, but i can't find it , on PPC2003 neither PPC2002!
If you can help me, by explaining how to use it and where to find it? coz i searched in all PPC2002/2003 sdk , a nothing such were found...


TO EXPLAIN MORE ABOUT THE PROBLEM:
i work on my new PocketPC 2003 device since last monday. On PocketPC 2002 all worked Well, the Dynamic Recompiler where a lot of faster than Dynamic Interpreter. Since new OS, the recompiler doesn't work.What's happened? A lot of Instruction are compiled block by block and executed within the Buffer , within the code recompiled there is some call to external routine like the Hardware emulation (joypad,video,sound etc..) and Read/Write function .
The emulator run a few instruction (about 2000) and R3000A reg structure is modified by something. The problem is that if the emulator is slowed down by The fact that it's compiled in debug mode and trace to file mode is Activated, that run a lot of more instruction before at a time exiting due to Bad value in reg structure. First we thought it was our Allocation method, but as you answers here there is no reason, neither to protect memory.
So we thought to data cache. And today we had an Idear, is the Wince 4.2 thought that VirtualAllocation memory used by code is never changed until we DECOMMIT this range. So we have changed our Private allocation Block memory (we only allocated a Big Block and then we had our private allocation/free block memory)
to a new routine which use everytime we need a block a VirtualAlloc with commit, and every we don't need a Virtualalloc with decomit.
But it's the Same problem as Before, that crash due to modified Reg structure....

Thanks for your help.
How about putting the "reg structure" in a protected page, and picking up the exceptions when it's written to? That way at least you can find what's trashing it.
Reg struct is the base of the Emulator it's everytime changed.... within the block, an ARM register point to this Structure and every instruction this structure is most of time changed.....
Yeah!!! That Work!!!!! i have successfully added CacheSync()!!!

And now i use Malloc for My recompiled BLock!!! VirtualProtect is not necessary now!!! all work at least 30 percent faster now :) :)

Wipeout 40+++ fps!!
Destrution Derdy 40-50fps!!!


excellent!!!

thanks everybody for all suggestion!
sounds nice, but i have a question...
Is it 30% or 300% faster? All my dynarec cores ran at least 300% faster than normal interpretted cores.
hehe, yes it's normal, it's about 20 time faster than normal interpreter , but we use a dynamic interpreter instead.... and i compare with it....this work like a recompiler, it use just calls compiled in the compiled buffer.
Anyelse the Biggest CPU Waste time is the 3D CHIP emulation...especially the GPU which is use to Draw surface and effect.
A new GPU using last 3D technology (a lot faster) will come in some week. Our GPU emulation is already Fast but there will be a so big difference with the next....
We could write some specific ARM asm for our Current GPU code to boost it (like for filling routines), but we prefer to wait the next version....

if i got time this evening i'll add a Video capture from Current Version....
mmh.. "some week" - does this mean "this" week? or "next" week? or "several weeks"?
no , FpseCE will out when all coding about GAPI will be finished....to explain why it's so long , it's because we use a Drawing routine For each resolution(24), and a group of drawing routine for each GAPI specification....(4), so for drawing there is 4x24=96 routines...
every time we find a new trick to speedup those routines, every time we need to change those 96 routines... i found a new trick yesterday....hehe

Btw if anybody want to become Beta tester, please send me a private message with your Age,Name, address, phone number and detail of your Machine.
I'll choose one beta tester for each machine.
schtruck wrote:
and a group of drawing routine for each GAPI specification....(4)


Are you talking about 4 orientations? Because GAPI only has two (portrait/landscape). The interface let you believe otherwise, but internally, GAPI cannot handle the other two orientations.

This is the reason of the infamous IPAQ3800 "bug" with it's inverted-lanscape orientation. HP shouldn't have mounted the screen upside-down (but I guess there was a physical limitation that explains it). There was no way for them to expose the appropriate parameters to GAPI. So they want with a DRAM buffer that flips the image each frame. That code could have been optimized, but that's another matter.

So if you are indeed talking about 4 orientation, there is no need to implement all of them. 3 would suffice if you want to support the IPAQ 3800, 2 otherwise.
yep you're right Thierry, but here is explanation:

4 mode:
1) The one for IPAQ 38xx
2) Gapi in landscape Mode
3) Gapi in Portrait mode
4) Gapi in Portrait without GXenddraw()

i can reduce to 2 if i put a Test at this end of each Routines for DRAM...