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 

stack pointer start address?

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



Joined: 26 Nov 2007
Posts: 35

PostPosted: Tue Apr 29, 2008 1:55 pm    Post subject: stack pointer start address? Reply with quote

In learning assembly language, I am still not quite sure how does the PSP allocation for a program ans its data.

Assume that I am in pre-3.7x fw, now if I have a memory dump from 0x08800000 - 0x08800000 + 0x01800000 = 0x098800000 (I am not sure if I am looking at the right memory region, correct me if I am wrong), what address does $sp point to just after all registers first have been initialized but before the first line of the assembly code get executed?

Since the stack is growing down, I suspect that the $sp will start at 0x098800000. Or will it be different across different types of application (i.e. ISO's vs homebrew apps)?
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Wed Apr 30, 2008 8:59 am    Post subject: Reply with quote

The stack will be different for every thread running, since they all share the same address map (there's no MMU in the PSP) - otherwise, they'd trample on each other's stacks.

If you have access to the thread info structures, you can query those as I believe they store the origin of $SP directly, for each thread. I'm afraid I don't remember where those are stored in RAM, although presumably it's in the kernel memory range. If you know the names of any threads present, you could probably work backwards from finding the addresses of those names in the RAM dump.

In theory you could probably predict the $SP origins for each thread, if you know how many threads are running and what their stack sizes are. But in practice, since about FW 2.5 or so, the stack origin is subject to a small random offset - roughly within the range +/- 512 bytes, IIRC, so that wouldn't be reliable. For that, you can probably blame those naughty people who try to exploit stack overflows ;)
_________________
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
Back to top
View user's profile Send private message
serige



Joined: 26 Nov 2007
Posts: 35

PostPosted: Wed Apr 30, 2008 11:41 am    Post subject: Reply with quote

Thanks for the clarification.

Assuming in the simplest possible situation, in which you only have the main thread running for the target application (I am not sure if this is the case for every real application out there), in this case, should the initial address of $sp start somewhere at the top of the main ram?
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Wed Apr 30, 2008 1:55 pm    Post subject: Reply with quote

No, you specify the amount of stack space a thread will use (there's a macro to set it beyond the default value), and it's allocated. So it could be anywhere depending on how memory is in use (other plugins, etc).

That was a bug in SDL I identified a few weeks back... the stack size the SDL_main was specifying was too small for many SDL apps (real apps, not demos). You should set the stack size for your threads to be big enough to allow for local stack usage.
Back to top
View user's profile Send private message AIM Address
TyRaNiD



Joined: 18 Jan 2004
Posts: 918

PostPosted: Wed Apr 30, 2008 3:55 pm    Post subject: Reply with quote

I am wondering why you need to worry? Let the PSP firmware do its job and handle memory allocation for you and not pay any attention to where you stack ends up :)
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Wed Apr 30, 2008 6:29 pm    Post subject: Reply with quote

TyRaNiD wrote:
I am wondering why you need to worry? Let the PSP firmware do its job and handle memory allocation for you and not pay any attention to where you stack ends up :)


I think the aim was to understand how to understand static memory dumps - where of course the firmware can't really help.

However the best way to learn is probably to write an app that will use the firmware funcs to iterate across all the threads, dumping their info, and then dumping the RAM, to get a feel for how it works.

serige wrote:
Thanks for the clarification.

Assuming in the simplest possible situation, in which you only have the main thread running for the target application (I am not sure if this is the case for every real application out there), in this case, should the initial address of $sp start somewhere at the top of the main ram?


On the PSP, we don't really talk about threads in the traditional sense - they're much closer to processes on a more familiar OS. So there's never just the main application thread running - there are plenty of OS processes running too.

IIRC there are options on thread creation to create the stack almost anywhere - but I think the default is to create it at the top of the available memory space.
_________________
Got a v2.0-v2.80 firmware PSP? Download the eLoader here to run homebrew on it!
The PSP Homebrew Database needs you!
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