forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Dynamic Recompilation Question

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
cWeasle



Joined: 15 Aug 2008
Posts: 2

PostPosted: Fri Aug 15, 2008 10:06 am    Post subject: Dynamic Recompilation Question Reply with quote

First off as a new member of this community I'd like to say hi and that I hope to contribute quality homebrews for everyone to enjoy.

Now to the niddy-gritty:

I'm working on a project that would involve dynamic recompilation, but I have hit an early snag that I hope someone can help me with. I want to be able to load machine code from a file, load it in ram, jump to that memory address, then jump back. Sounds simple. I start in C to open the file and copy it to ram, then use its pointer address as an argument (addr) in the below inline asm.

Code:
asm(
   "la $a0, skip\n"
   "or %0, $a0, $zero\n"   //load the address of skip: into output variable b
   "j cont\n"
   "skip:\n"
   "jr $ra\n"
   "cont:\n"
   "jalr $a0\n"      //option A
//   "jalr %3\n"      //option B
   "lw %1, 0(%3)\n"   //load the value at addr into output variable c
   "lw %2, 0($a0)\n"   //load the value at skip: into output variable d
   :"=r" (b), "=r" (c), "=r" (d)
   :"r" (addr)
   :"%ra", "%sp", "%fp", "%at", "%a0", "%t0", "%t1", "%s0" //got lazy with the clobber list
);


By the way for testing purposes, the only thing in the file being loaded is
Code:
jr $ra
nop


Running what I referred to as option A in the above code where the program jumps to skip and back, i get this output.

skip address: 0x08900440 value: 0x03E00008
file address: 0x08918fd0 value: 0x03E00008

(So pretty much the same thing except the address.)

However, when I try option B where it tries to jump to the ram content of the file, it locks up. Why does this happen and how can I get it to run what i'm trying to inject?
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Fri Aug 15, 2008 4:34 pm    Post subject: Reply with quote

When you make code, you have to flush the data cache, and invalidate the code cache. If you don't the code might not really be there.
Back to top
View user's profile Send private message AIM Address
Mihawk



Joined: 03 Apr 2007
Posts: 29

PostPosted: Fri Aug 15, 2008 4:56 pm    Post subject: Reply with quote

Code:

...
   "j cont\n"
   "skip:\n"
   "jr $ra\n"
   "cont:\n"
   "jalr $a0\n"      //option A
...

Not sure if this is part of your problem, but what about the delay slots of those jump instructions? Are nops inserted automaticaly when compiling this or should you put at least a nop after each (cause if I see this right, in this case the first two jumps have another jump in their delay slot, that's not allowed IIRC)?
_________________
Ask and it will be given to you; seek and you will find; knock and the door will be opened to you.
Back to top
View user's profile Send private message
cWeasle



Joined: 15 Aug 2008
Posts: 2

PostPosted: Fri Aug 15, 2008 6:24 pm    Post subject: Reply with quote

Yep, that did it. Adding sceKernelDcacheWritebackInvalidateRange(const void *p, unsigned int size) right before the inline asm fixed it. Thanks J.F.

The nops are added automatically, and i had manually added one in my file.
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Fri Aug 15, 2008 8:43 pm    Post subject: Re: Dynamic Recompilation Question Reply with quote

i usually prefer not let asm reorder and insert "nop" instructions as you cannot control it. So i use ".set noreorder" and insert myself "nop" if necessary in delayslots. For someone who is used to read MIPS disassembly code, your code is too much disturbing to read.

cWeasle wrote:

Code:

la $a0, skip
or %0, $a0, $zero
j cont
skip:
jr $ra
cont:
jalr $a0
jalr %3
lw %1, 0(%3) <--- this is a delayslot or not !? this is unreadable !
lw %2, 0($a0)


==>
Code:

.set push
.set noreorder // don't let asm to reorder or insert nop
la $a0, skip
j cont
or %0, $a0, $zero // take advantage of a delay slot
skip:
jr $ra
nop
cont:
jalr $a0
nop
jalr %3
nop
lw %1, 0(%3) // If executed after call otherwise just remove previous "nop"
lw %2, 0($a0)
.set pop

[/quote]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group