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 

Exporting functions from user mode prx to kernel mode prx

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



Joined: 09 Oct 2007
Posts: 409

PostPosted: Sun Nov 02, 2008 6:05 am    Post subject: Exporting functions from user mode prx to kernel mode prx Reply with quote

Is it possible or not? I have tried multiple ways and I can't seem to get it working :/

I really need to export a function from user mode as the kernel LIBC is not the same as the user LIBC and some of the functions I Need are not included in the kernel LIBC.

I have two options, create my own functions or export the function from user mode.
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Sun Nov 02, 2008 10:30 pm    Post subject: Reply with quote

I know that 0x0001 attrib is for export a function from kernel to kernel or user to user, and 0x4001 for export kernel to user!
I've never tried to export a function from a user to a kernel prx module...
Have you tried with these flags?
_________________
Get Xplora!
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Sun Nov 02, 2008 10:44 pm    Post subject: Reply with quote

Where do I use them?
The Only place I think of is the mode attribute
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
phobox



Joined: 24 Mar 2008
Posts: 140

PostPosted: Sun Nov 02, 2008 10:51 pm    Post subject: Reply with quote

in the exports file.
PSP_EXPORT_START(libName, 0, 0x4001)

the 3rd param is where to put your flags
_________________
Ciao! from Italy
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Nov 02, 2008 11:34 pm    Post subject: Reply with quote

phobox wrote:
in the exports file.
PSP_EXPORT_START(libName, 0, 0x4001)

the 3rd param is where to put your flags


Thats only useful if he wants to use functions exported by a PRX which he made. It looks like he wants to call other sce user mode functions from the firmware PRX, from inside a kernel PRX.

I think the only way will be to use setddrmemoryprotection function to make the kernel memory as user mode, and then call your user mode function by getting the function address or something. You can't use any kernel function in that time. I'm just guessing, I'm no expert at this.

Generally it only causes various problems. User mode function should be called from user mode prx only, thats it.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Sun Nov 02, 2008 11:36 pm    Post subject: Reply with quote

oops forgot about that.
Yeah it's currently set for kernel to user.

so 0x0001 is kernel > kernel and 0x4001 is kernel -> user, then 0x4000 should be user -> user ? tried it and did not work either

EDit:
torch I am exporting from a prx i made to another prx I made :)
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Mon Nov 03, 2008 12:00 am    Post subject: Reply with quote

I guess you could do it like this:

Code the original function in a kernel PRX as kernel to kernel export. Then make a wrapper function for it in the same PRX as a kernel to user export.

Then you can call the kernel version from other kernel PRXs, as well as the user wrapper from other user PRXs.

Its impossible to directly call a kernel function from a user prx, so I assume the reverse is true as well. When you have a kernel to user export, it uses syscalls. I don't know if user to kernel export is possible like the way you want..
Back to top
View user's profile Send private message
phobox



Joined: 24 Mar 2008
Posts: 140

PostPosted: Mon Nov 03, 2008 12:09 am    Post subject: Reply with quote

Pirata Nervo wrote:

so 0x0001 is kernel > kernel and 0x4001 is kernel -> user, then 0x4000 should be user -> user ?

not exactely..
i think everybody should have read http://ps2dev.org/psp/Tutorials/PSP_Modules,_Exports,_Imports_and_Patches.download
_________________
Ciao! from Italy
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Mon Nov 03, 2008 12:13 am    Post subject: Reply with quote

0x0001 means direct jump to address. It works for user to user or kernel to kernel I think.

0x4001 means use syscall. AFAIK this is only for calling a kernel to user export from a user prx.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 03, 2008 12:47 am    Post subject: Reply with quote

I think you haven't read my first pos torch.
kernel LIBC does not have fopen, fwrite, fclose, fprintf, etc and I need them for my function as the library I am using makes of those functions.

The only way to use that function is exporting from a user prx to a kernel prx (which needs kernel mode to work).

Of course I can the user prx to execute the function when it gets loaded but that's not what I want.

@phobox I already read that file some time ago :) but of course I don't remember everything
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Mon Nov 03, 2008 1:43 am    Post subject: Reply with quote

Does it compiles with USE_KERNEL_LIBC=0 ? That might provide the functions you need.

Are you writing a plugin or an EBOOT?
You only need to worry about user modules if you are writing a plugin.

If you are writing an EBOOT, the correct way IS to have the function executed from a user module. All your main working code should be user mode. You only need to load a kernel prx to do, well, kernel stuff.

If you are writing a plugin however, then you have no choice but to use various tricks to get user mode code working.

Have the user module continuously 'poll' the kernel prx for various commands. I mean export a function get_commands() from your kernel prx. The user prx should keep calling this function to see if the kernel prx needs something done.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 03, 2008 1:55 am    Post subject: Reply with quote

I am writing a prx of course, otherwise I would not need to export a user function from a user prx and I could do it a from a EBOOT.PBP (as it must be user mode to be executed).

graphics.prx is the user prx.
patchexit_driver.prx is the kernel prx.

patchexit_driver.prx works perfectly since NervOS 2.1 (it's a shell) but now for 2.3 I am working on some new features and I need patchexit_driver.prx to call a user mode function.
patchexit_driver.prx only works with KERNEL_LIBC and KERNEL_LIBS set to 1.
This means I cannot use the stdio.h functions. I have already created the stdio.c and stdio.h and malloc.c and malloc.h with the necessary functions but I still need time() and I couldn't find time.c anywhere so I gave up and deleted my new code.

I am trying graphics.prx in kernel mode with user LIBC but with kernel LIBS and the only error I am getting is:
Code:
/usr/local/pspdev/lib/gcc/psp/4.3.1/../../../../psp/lib/libc.a(_sbrk.o): In function `_sbrk':
../../../../../../newlib/libc/sys/psp/libcglue.c:549: undefined reference to `sceKernelMaxFreeMemSize'
collect2: ld returned 1 exit status
make: *** [graphics.elf] Error 1


I've tried adding -lpspsysmem_user/kernel to my LIBS and using the .a and .h that comes with the m33 sdk but didn't work.

Edit:
adding -lc as the first library worked. :) time to test the prx
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Mon Nov 03, 2008 1:58 am    Post subject: Reply with quote

Delete the output files every time you recompile. Usually when you change the makefile, it doesn't reflect the next time you compile it unless you clean up previous output files.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 03, 2008 2:01 am    Post subject: Reply with quote

that's why I use do make clean before make :P

It exports the function :now, it crashed when doing fopen I think but no problem, I will find a way to fix it.
Thank you all
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Mon Nov 03, 2008 2:14 am    Post subject: Reply with quote

Can you give me you're "clean" section of the makefile?
I've tried to write one before but failed.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 03, 2008 2:27 am    Post subject: Reply with quote

I am not using user mode anymore. I just managed to get graphics.prx (in kernel mode) using standard I/O functions and exporting functions at the same time.

Here's my makefile:
Code:
TARGET = graphics

OBJS = main.o exports.o

PSPLIBSDIR = $(PSPSDK)/..
USE_KERNEL_LIBS=1

INCDIR = ../../../sdk/include

CFLAGS = -Os -G0 -Wall -g

CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti

ASFLAGS = $(CFLAGS)

#LDFLAGS += -mno-crt0 -nostartfiles


BUILD_PRX = 1

PRX_EXPORTS = exports.exp



#USE_KERNEL_LIBC = 1
#USE_USER_LIBC = 1



PSP_FW_VERSION = 500



LIBDIR = ../../../sdk/lib

LIBS = -lc -ljpeg -lpspkernel  -lz -lm -lpsploadexec_kernel -lpspsystemctrl_kernel -lpspmodulemgr_kernel -lpspumd -lpspkubridge



PSPSDK=$(shell psp-config --pspsdk-path)

include $(PSPSDK)/lib/build_prx.mak


I am going to do some debug now to see where it is crashing in my code. It may be crashing due to STD I/O functions
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Mon Nov 03, 2008 3:29 am    Post subject: Reply with quote

I don't know if it works in latest firmwares but I remember long ago it worked using flags 0x0001, which causes functions to resolve as jumps. But that is very propense to cause inestability and finally crash, as the user functions are being executed in kernel mode and they are programmed as user functions.

I remember I did the first version of nethostfs like that, it was a kernel prx that called user functions (net ones), and that's probably the reason why it was so unstable. Later Ahman improved it and I think he used user mode.

Anyways, I don't know if since then Sony has made more checks to make this not possible.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 03, 2008 3:40 am    Post subject: Reply with quote

Ok thanks moonlight. Did Ahman ever released the source for NetHostFS PC and PSP ? I know you did but I don't know about him
I managed to get the standard I/O to work anyway :)
Using a custom stdio.c and stdio.h
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
KickinAezz



Joined: 03 Jun 2007
Posts: 328

PostPosted: Mon Nov 03, 2008 3:47 am    Post subject: Reply with quote

LOL!

pspiofilemgr_kernel.h.

LIBS = -lpspiofilemgr_kernel (not required since it's already in BUILD.MAK)
_________________
Intrigued by PSP system Since December 2006.
Use it more for Development than for Gaming.
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 03, 2008 3:59 am    Post subject: Reply with quote

KickinAezz wrote:
LOL!

pspiofilemgr_kernel.h.

LIBS = -lpspiofilemgr_kernel (not required since it's already in BUILD.MAK)


what the fu*k are you talking about?
I guess you did not read all posts otherwise you would have not said what you said.

THE STANDARD I/O FUNCTIONS ARE CRASHING. (this means it's stdio.c/h)
They use sceIo functions yes but they got a #ifdef before which makes the functions not being able to be used when LIBC or LIBC (i dont know) are set for kernel. So now I removed the necessary #ifdef #endif from the custom stdio.c and stdio.h I have here and it works.

I hope you can read it better now.
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
whistler



Joined: 04 Mar 2008
Posts: 40

PostPosted: Mon Nov 03, 2008 4:07 am    Post subject: Reply with quote

Torch wrote:
Can you give me you're "clean" section of the makefile?
I've tried to write one before but failed.


it's already in build.mak. you can also use make rebuild which is quicker than typing make clean, make
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Mon Nov 03, 2008 4:12 am    Post subject: Reply with quote

I thought he meant "clean" as makefile without unneeded things lol.
not the clean "section"
"make clean" is a linux command Torch. Sorry for misunderstanding

Edit:
you can also use this:
Code:
all: myfile.prx
   rm -f /home/username/project/*.o


this will clean all .o files in the specified path everytime you hit make.

rm means remove and the option -f means force. myfile.prx is your prx file.
_________________

Upgrade your PSP
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Mon Nov 03, 2008 2:25 pm    Post subject: Reply with quote

No I meant sometimes the makefile has a section "clean:' just like "all:" which is used to remove specific files if a general rm -f *.o isn't sufficient.
Back to top
View user's profile Send private message
ne0h



Joined: 21 Feb 2008
Posts: 386

PostPosted: Tue Nov 04, 2008 12:24 am    Post subject: Reply with quote

A simple make clean remove all the objs file, the target and the extra targets like pbp's and related sfo...

Last edited by ne0h on Tue Nov 04, 2008 12:25 am; edited 1 time in total
Back to top
View user's profile Send private message
Pirata Nervo



Joined: 09 Oct 2007
Posts: 409

PostPosted: Tue Nov 04, 2008 12:25 am    Post subject: Reply with quote

@torch I think you cannot use clean: anymore.
_________________

Upgrade your PSP
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