This site is no longer active and is available for archival purposes only. Registration and login is disabled.

A big Problem with VirtualAlloc and PocketPC 2003!


A big Problem with VirtualAlloc and PocketPC 2003!

Postby schtruck » Aug 6, 2003 @ 9:06am

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.
schtruck
pm Member
 
Posts: 77
Joined: May 24, 2002 @ 5:29pm


Postby refractor » Aug 6, 2003 @ 12:22pm

User avatar
refractor
pm Insider
 
Posts: 2304
Joined: Feb 5, 2002 @ 1:12pm
Location: Luxembourg


Postby schtruck » Aug 6, 2003 @ 1:38pm

schtruck
pm Member
 
Posts: 77
Joined: May 24, 2002 @ 5:29pm


Postby rcp » Aug 6, 2003 @ 6:19pm

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

Cheers,

rcp
User avatar
rcp
pm Member
 
Posts: 184
Joined: Jul 18, 2003 @ 2:12am
Location: Duluth, GA. (Southeast US)


Postby Guest » Aug 7, 2003 @ 6:52am

Guest
 


Postby rcp » Aug 7, 2003 @ 8:00am

User avatar
rcp
pm Member
 
Posts: 184
Joined: Jul 18, 2003 @ 2:12am
Location: Duluth, GA. (Southeast US)


Postby Sergey Chaban » Aug 7, 2003 @ 12:02pm

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)
Sergey Chaban
pm Member
 
Posts: 72
Joined: Nov 12, 2002 @ 1:45am


Postby schtruck » Aug 7, 2003 @ 8:37pm

schtruck
pm Member
 
Posts: 77
Joined: May 24, 2002 @ 5:29pm


Postby Dan East » Aug 8, 2003 @ 3:48am

User avatar
Dan East
Site Admin
 
Posts: 5264
Joined: Jan 25, 2001 @ 5:19pm
Location: Virginia, USA


Postby refractor » Aug 8, 2003 @ 9:01am

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
User avatar
refractor
pm Insider
 
Posts: 2304
Joined: Feb 5, 2002 @ 1:12pm
Location: Luxembourg


Postby Sergey Chaban » Aug 8, 2003 @ 1:39pm

Sergey Chaban
pm Member
 
Posts: 72
Joined: Nov 12, 2002 @ 1:45am


Postby refractor » Aug 8, 2003 @ 3:02pm

User avatar
refractor
pm Insider
 
Posts: 2304
Joined: Feb 5, 2002 @ 1:12pm
Location: Luxembourg


Postby Kzinti » Aug 8, 2003 @ 3:49pm

Kzinti
pm Member
 
Posts: 3238
Joined: Jan 13, 2002 @ 5:23am


Postby schtruck » Aug 8, 2003 @ 9:45pm

schtruck
pm Member
 
Posts: 77
Joined: May 24, 2002 @ 5:29pm


Postby refractor » Aug 8, 2003 @ 10:27pm

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.
User avatar
refractor
pm Insider
 
Posts: 2304
Joined: Feb 5, 2002 @ 1:12pm
Location: Luxembourg


Next

Return to Windows Mobile


Sort


Forum Description

A discussion forum for mobile device developers on the Windows Mobile platform. Any platform specific topics are welcome.

Moderators:

Dan East, sponge, Digby, David Horn, Kevin Gelso, RICoder

Forum permissions

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum