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 

A question about thread manipulation in homebrew programming

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



Joined: 10 Sep 2007
Posts: 85

PostPosted: Wed Jan 09, 2008 2:05 pm    Post subject: A question about thread manipulation in homebrew programming Reply with quote

Hi everybody. Is there a way to manipulate the execution flow of a thread externally? I mean, if I've got a thread A which is currently running at address 0x11111111. But then I wanna suspend it, and when it resumes, I want it to start running the code from address 0x22222222 (note that the code at 0x11111111 has no correlation with the code at 0x22222222 at all, thus it is not like the code at 0x11111111 will jump to the new location voluntarily in thread A). Can this kind of manipulation be done by a separate thread B or an interrupt handler (which is not a native interrupt handler to the CPU but more like an installable handler function normally used by interrupt manager)?

To my understanding the difficulty of this is that it lacks a way to return from the code that performs scheduling (thread B or interrupt handler) to the destination that the scheduler desires. In a normal system, we usually have the exception-return instruction to take care of this (like ERET in MIPS architecture), which sort of connects the interrupt context with the non-interrupt context. But if the non-interrupt context is just a single thread inside an operating system and the scheduler is merely a routine that can not directly end the interrupt processing and jump to the new location ('cos it's just an installable function that still needs to return to the true system interrupt handler for further process), how can this job of ERET be done, or at least be simulated? If it was a linux system, I think I could probably just modify the TCB structure of thread A to achieve this. But how can this be done in PSP firmware?
Back to top
View user's profile Send private message Send e-mail Visit poster's website
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Thu Jan 10, 2008 4:24 am    Post subject: Reply with quote

Well overly wordy, why didn't you just ask for how to get the thread context ;)

It can be done in kernel mode but you have to be very careful about it because you need to fix the _actual_ EPC and not necessarily the one in the context :P

Look at thctx.c in psplinkusb, it uses ThreadManForKernel_2D69D086 to get the context from a UID (ensure you define your FW version when compiling as the context structure is different above around 3.X). Then you need to either fix the EPC if there is no syscall context or fix the syscall context EPC otherwise.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Jan 10, 2008 5:21 am    Post subject: Reply with quote

User mode cannot get a thread context? I guess that means you'd need to make an external kernel-mode prx where the user app calls it with the thread uid and it passes back a pointer to the context, then another call to set the context after it's been modified (if that is what is needed after modifying the context structure).
Back to top
View user's profile Send private message AIM Address
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Thu Jan 10, 2008 7:58 am    Post subject: Reply with quote

Nope it is kernel mode only. You would obviously have to ensure the thread is suspended before playing with the context otherwise who knows what would happen ;)
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Jan 10, 2008 10:10 am    Post subject: Reply with quote

TyRaNiD wrote:
Nope it is kernel mode only. You would obviously have to ensure the thread is suspended before playing with the context otherwise who knows what would happen ;)


That's EASY to figure out... BOOOOOOOOOMMMMMM!!!!!!! ;) :)

Yes, on the Amiga you'd do a Forbid()/Permit() around the code in question. Some people preferred to do Disable()/Enable()... just to be extra safe. I've seen int disable/enable used in code on the PSP. Personally, I've yet to do anything on the PSP that requires more than remembering the PSP uses cooperative multitasking.
Back to top
View user's profile Send private message AIM Address
M.Jackson



Joined: 10 Sep 2007
Posts: 85

PostPosted: Thu Jan 10, 2008 12:49 pm    Post subject: Reply with quote

thanks, guys. thanks for all the details :)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
M.Jackson



Joined: 10 Sep 2007
Posts: 85

PostPosted: Fri Jan 18, 2008 3:19 pm    Post subject: Reply with quote

I know this may sound stupid, but where can I get the source of psplink? Everywhere I found via google only provided the prebuilt version for download.

Thanks!
Back to top
View user's profile Send private message Send e-mail Visit poster's website
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Fri Jan 18, 2008 4:11 pm    Post subject: Reply with quote

Urm subversion perhaps ;)

svn://svn.pspdev.org/psp/trunk/psplinkusb
Back to top
View user's profile Send private message
M.Jackson



Joined: 10 Sep 2007
Posts: 85

PostPosted: Mon Jan 21, 2008 4:19 pm    Post subject: Reply with quote

TyRaNiD wrote:
Urm subversion perhaps ;)

svn://svn.pspdev.org/psp/trunk/psplinkusb

I guess the url above only works on the svn type of access. But since my pc is behind a firewall which i don't have any authority to config (otherwise I think I could have changed the NAT table allowing me to pass through), the only way out for reaching the internet is through a http proxy server. So I modified the config of subversion specifying the proxy, and attempted to access this url http://svn.pspdev.org/psp/trunk/psplinkusb instead, but got the following response:

$ svn co http://svn.pspdev.org/psp/trunk/psplinkusb .
(I tried "svn export http://svn.pspdev.org/psp/trunk/psplinkusb ." too, but got the same result)
svn: PROPFIND request failed on '/psp/trunk/psplinkusb'
svn: PROPFIND of '/psp/trunk/psplinkusb': 405 Method Not Allowed (http://svn.pspdev.org)

Does this mean the subversion server was not configured to serve http requests? If that's the case, I will try to find a way to get the source outside the firewall.

Or, could it just be me using a wrong way to access the repository? because I can view all the files in /psp/trunk/psplinkusb through a web brower normally, which probably suggests the server are willing to offer its content through http protocol as well.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
M.Jackson



Joined: 10 Sep 2007
Posts: 85

PostPosted: Mon Jan 21, 2008 5:11 pm    Post subject: Reply with quote

ok, forget about the trouble I had with subversion. when reading thctx.c, I found that there seemed to be a little bit of overlay inside the structure of SceKernelThreadKInfo. For example, the filed of EPC exists in both thContext (of type SceThreadContext) and scContext (of type SceSCContext). And there is also a field of retAddr with its name reasonable enough to make me believe it is also for preserving the location where the thread should be resumed from. So which field actually tells the "true" return address if the thread is interrupted by a syscall for instance?

In addition, thContext saves all 31 GPRs which naturally includes sp, ra and k1 ($29, $31 and $27), but scContext also has members of sp, ra and k1. So which one actually tells the valid values when a h/w interrupt or syscall occurs? What are the differences between thContext and scContext in general?

PS: I did notice how the code in thctx.c selects one value over another between thContext and scContext. But I would like understand the reason behind it, the grounds backing those selections, instead of just blindly mimicking the code in future :)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Tue Jan 22, 2008 9:28 am    Post subject: Reply with quote

M.Jackson wrote:
TyRaNiD wrote:
Urm subversion perhaps ;)

svn://svn.pspdev.org/psp/trunk/psplinkusb

I guess the url above only works on the svn type of access. But since my pc is behind a firewall


HTTP mirror here
http://forums.ps2dev.org/viewtopic.php?p=34778#34778
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