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 

Allocating Userspace from Kernel Module?

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



Joined: 08 Feb 2007
Posts: 155

PostPosted: Mon Mar 22, 2010 11:09 pm    Post subject: Allocating Userspace from Kernel Module? Reply with quote

I want to allocate some Userspace to save it from EBOOT.BIN devouring it whole.

As many people already know UMD games in general allocate ALL the available userspace minus 1MB...

Thing is, I need to be able to dynamically load user-modules while the game is already running... thus I want to allocate a specific ammount of user memory from within my kernel plugin before EBOOT.BIN kicks in and eats the remainder, so I can free the memory after EBOOT.BIN is done launching and thus free the necessary resources for usermodule loads.

Anyone can give me a hint?
_________________
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Back to top
View user's profile Send private message MSN Messenger
m0skit0



Joined: 02 Jun 2009
Posts: 226

PostPosted: Tue Mar 23, 2010 11:06 pm    Post subject: Reply with quote

But EBOOT.BIN assumes all that memory belongs to him. How would you change his mind? xD
_________________
The Incredible Bill Gates wrote:
The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers.
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Tue Mar 23, 2010 11:22 pm    Post subject: Reply with quote

sceKernelAlloc* functions with User partition paramater, from your kernel module. It should allocate before the game is loaded. Then free it once you know the game has loaded.
Back to top
View user's profile Send private message
Coldbird



Joined: 08 Feb 2007
Posts: 155

PostPosted: Wed Mar 24, 2010 4:33 am    Post subject: Reply with quote

I already tried that method Torch... but the problem is that I can't change the heap allocation of EBOOT.BIN...

The problem Im currently having using the method you described Torch...

1. My plugin gets executed... 24MB userspace are still available...
2. I allocated 2MB (just to test it)... this works...
3. EBOOT.BIN starts... and tries to allocate its usual heap... (24-1 MB -> 23MB) ---- this errors with a "cant allocate" error back to vsh... kinda obvious why... it can't allocate the necessary 23MB it requests...

So... now I want to figure out... just where and how EBOOT.BIN allocates this 23MB... and change the arguments somehow... so I can be sure it wont try allocating the whole 23MB...

I hope what I say is understandable... Im in a bit of a hurry right now.
So... anyone has a idea?
_________________
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Back to top
View user's profile Send private message MSN Messenger
m0skit0



Joined: 02 Jun 2009
Posts: 226

PostPosted: Wed Mar 24, 2010 5:00 am    Post subject: Reply with quote

Decrypt the EBOOT and prxtool it, this way you can figure out how it allocates the memory, and even patch it. That said, prepare yourself for strange errors.
_________________
The Incredible Bill Gates wrote:
The obvious mathematical breakthrough would be development of an easy way to factor large prime numbers.
Back to top
View user's profile Send private message
willow :--)



Joined: 13 Jan 2007
Posts: 126

PostPosted: Wed Mar 24, 2010 8:59 am    Post subject: Reply with quote

you can try to override the function sceKernelAlloc*with a hook. Then, when 24MB is requested by the game, answer "yes, here's your sceUID" even if you already allocated some space for your module.
The risk is that the game will think it has 24MB when it only has 22... so it might crash anyway

Edit: since you're a kernel plugin, isn't it possible to load your user modules in kernel space, to change your problem into a non-issue ?
_________________
Wagic. Play that card game against an AI on your PSP
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Thu Mar 25, 2010 4:02 am    Post subject: Reply with quote

Where did you learn that they only allocate 23MiB? They can allocate as much user memory (24MiB) as they want, whenever they want. This method will result in failure in so many ways.
Back to top
View user's profile Send private message
Davee



Joined: 22 Jun 2009
Posts: 59

PostPosted: Thu Mar 25, 2010 6:21 am    Post subject: Reply with quote

Torch wrote:
Where did you learn that they only allocate 23MiB? They can allocate as much user memory (24MiB) as they want, whenever they want. This method will result in failure in so many ways.


Don't be ridiculous. Where is the system going to store stack if it allocates 24 MB?
Back to top
View user's profile Send private message
SilverSpring



Joined: 27 Feb 2007
Posts: 115

PostPosted: Thu Mar 25, 2010 3:44 pm    Post subject: Reply with quote

Coldbird wrote:

So... now I want to figure out... just where and how EBOOT.BIN allocates this 23MB... and change the arguments somehow... so I can be sure it wont try allocating the whole 23MB...


They allocate the same way as PSPSDK does (well PSPSDK just copied the same way from debug symbols in Puzzle Bobble). Gets allocated by the crt0 before the main() function is called, specifically the "user_main" thread that module_start() creates. The "user_main" thread is then the one that calls your main() function.

It uses sce_newlib_heap_kb_size as a global var (PSPSDK makes it as a macro PSP_HEAP_SIZE_KB) to set the heap size.

Check _sbrk fuction in libcglue.c to see how sce_newlib_heap_kb_size is used in setting the size in sceKernelAllocPartitionMemory. The only difference is that offical games named the block "UserSbrk" whereas PSPSDK just names it "block".
_________________
PSP PRX LibDocs
Back to top
View user's profile Send private message Visit poster's website
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Fri Mar 26, 2010 3:39 am    Post subject: Reply with quote

Davee wrote:
Torch wrote:
Where did you learn that they only allocate 23MiB? They can allocate as much user memory (24MiB) as they want, whenever they want. This method will result in failure in so many ways.


Don't be ridiculous. Where is the system going to store stack if it allocates 24 MB?

Isn't it up to the developer to leave enough space for thread stacks?
Back to top
View user's profile Send private message
Davee



Joined: 22 Jun 2009
Posts: 59

PostPosted: Fri Mar 26, 2010 6:15 am    Post subject: Reply with quote

Torch wrote:
Davee wrote:
Torch wrote:
Where did you learn that they only allocate 23MiB? They can allocate as much user memory (24MiB) as they want, whenever they want. This method will result in failure in so many ways.


Don't be ridiculous. Where is the system going to store stack if it allocates 24 MB?

Isn't it up to the developer to leave enough space for thread stacks?


No.
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Fri Mar 26, 2010 10:49 pm    Post subject: Reply with quote

So in the official SDK they have no option to specify the initial heap and its always 23MiB?
Back to top
View user's profile Send private message
Coldbird



Joined: 08 Feb 2007
Posts: 155

PostPosted: Mon Mar 29, 2010 12:30 am    Post subject: Reply with quote

First... to get the question "how do you know its 23MB" out of the way...

I don't know it... But I kept track of the available userspace memory using the firmware api...

In the very beginning before eboot launches its a full 24mb... after it launches it leaves EXACT 1mb...

Thus my guess - that the sony sdk ALWAYS allocates 23mb from userspace memory... (atleast the 4 games i tested it with did...)

As Silverspring mentioned... yes - sony sdk created games name this block of memory UserSbrk, as for the homebrew pspsdk... no idea... I didn't get to check the allocation process source yet... but I sure will when I get back home.

As Ive said before in another thread, for some reason when I capture the call it only allocates about 100kb memory... which makes me wonder just where the other 22.9 mb are going...
_________________
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Back to top
View user's profile Send private message MSN Messenger
willow :--)



Joined: 13 Jan 2007
Posts: 126

PostPosted: Mon Mar 29, 2010 8:12 am    Post subject: Reply with quote

We've had the same issue recently where a game would never call sceKernelAlloc*, and yet use around 23MB of ram.
Unloading the game module from psplink would free roughly 20MB though, So we assumed that the game uses giant static buffers.

Regarding the homebrew PSPSDK, the first time you do a malloc, the sdk calls the maxfreemem function, then does a huge sceKernelAlloc with that value. The memory allocated that way is then managed with a home made heap.
_________________
Wagic. Play that card game against an AI on your PSP
Back to top
View user's profile Send private message
Coldbird



Joined: 08 Feb 2007
Posts: 155

PostPosted: Mon Mar 29, 2010 8:08 pm    Post subject: Reply with quote

I know as much, yeah... it's why I hate relying on malloc or the pspsdk provided newlib...

Usually I try to stay as lowlevel as I can on PSP... it tends to work better this way...

About the static buffers, I feared you'd say something of the sort... but isn't there a limit to the stack on the official PSPSDK?
_________________
Been gone for some time. Now I'm back. Someone mind getting me up-2-date?
Back to top
View user's profile Send private message MSN Messenger
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