<HTML dir=ltr><HEAD>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.5296.0" name=GENERATOR><BASE href=HEAD>
<BODY>
<DIV id=idOWAReplyText14380 dir=ltr>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2></FONT>&nbsp;</DIV></DIV>
<DIV id=idSignature28149 dir=ltr><FONT face=Arial color=#000000 size=2>
<DIV></FONT><FONT face=Arial color=#000000 size=2>Hello </FONT></DIV></DIV>
<DIV dir=ltr>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2>I am attempting to run some user code with kernel space permission. I am using the ppc64 kernel version 2.6.16-rc4-3-ppc64 for IBM Power5 processors. </FONT></DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2>In this kernel module I am trying to implement a function that can be called from user space. </FONT></FONT></DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2></FONT></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2>I have found through various posts that using unused system calls and replacing them temporarily can acheive this objective. </FONT></FONT></DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2></FONT></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2><FONT color=#000000>This is what I am doing, but its not working, please bear with the slightly long code that follows: </FONT></FONT></FONT></DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2><FONT color=#000000></FONT></FONT></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2><FONT color=#000000>1) since the 2.6 kernel does not export sys_call_table, I grep it from the boot image</FONT></FONT></FONT></DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2><FONT color=#000000></FONT></FONT></FONT><FONT face=Arial><FONT size=2><FONT color=#000000></FONT></FONT></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2><FONT color=#000000>2) Next I write the kernel module as : </FONT></FONT></FONT></DIV>
<DIV dir=ltr><FONT face=Arial><FONT size=2><FONT color=#000000>#include &lt;linux/kernel.h&gt;<BR>#include &lt;linux/module.h&gt;<BR>#include &lt;linux/sched.h&gt;<BR>#include &lt;linux/syscalls.h&gt;<BR>unsigned long **sctable;<BR>void *org_func;&nbsp; /***** Copy of the original calls address ********/<BR></FONT></FONT></FONT></DIV>
<DIV dir=ltr>asmlinkage int mitesh_func(void)&nbsp;&nbsp; <BR>{ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printk(KERN_ALERT "Executing mitesh_func...\n"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 2;<BR>} <BR></DIV>
<DIV dir=ltr>int init_module(void)<BR>{<BR>&nbsp;unsigned long ptr;<BR>&nbsp;unsigned long *p;<BR>&nbsp;ptr = 0x23203404;&nbsp; /*** some hard coded addresses from grepping for sys_call_table *****/</DIV>
<DIV dir=ltr>&nbsp; p = (unsigned long *)ptr;<BR>&nbsp; sctable = (unsigned long **)p;<BR>&nbsp; printk("The address of the system call table is: 0x%x\n",&amp;sctable[0]);<BR>&nbsp; printk("The address of syscall #137 is: 0x%x\n",sctable[137]);<BR></DIV>
<DIV dir=ltr>org_func = (void *) (sctable[137]);&nbsp; /**** Store the original sys call ****/<BR>&nbsp;printk("Original func address 0x%x stored \n",org_func);</DIV>
<DIV dir=ltr><PRE dir=ltr>&nbsp;sctable[137] = (void *) mitesh_func;&nbsp; /**** replace with mitesh_func ****/<BR></PRE></DIV>
<DIV dir=ltr>printk("The new sys call address is 0x%x and stored as : 0x%x\n",mitesh_func, sctable[137]);<BR><BR>&nbsp; return 0; <BR>}</DIV>
<DIV dir=ltr>void cleanup_module(void)<BR></DIV>
<DIV dir=ltr>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sctable[137] = (void *) org_func; </DIV>
<DIV dir=ltr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printk("Upon module unload the sctable #137 address is :0x%x\n",sctable[137]);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printk("Module is unloaded!\n");<BR>}<BR></DIV>
<DIV dir=ltr>3) My user app looks like this:</DIV>
<DIV dir=ltr>#include &lt;stdio.h&gt; <BR>#include &lt;errno.h&gt; <BR>#include &lt;asm-ppc64/unistd.h&gt; <BR>#define __NR_mitesh_func 137 <BR>&nbsp;<BR>_syscall0(int, mitesh_func); </DIV>
<DIV dir=ltr>void main() <BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int x=0; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x=mitesh_func(); </DIV>
<DIV dir=ltr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("mitesh_func returned %d\n",x);</DIV>
<DIV dir=ltr>}&nbsp; <BR></DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>4) I verify from the system logs that when I insmod the kernel module I get all the print statements. I verified from the logs&nbsp; that the address of the sys_call_table is correctly passed and from /proc/kallsysms I can see that my function mitesh_func has been defined and has the address as indicated in the logs. </DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>The problem is that when I execute my user app I expect to see two things: </DIV>
<DIV dir=ltr>&nbsp;a) I should see a message in the log "Executing mitesh_func..." and </DIV>
<DIV dir=ltr>&nbsp;b) A return value of 2 </DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>However I get an error value <FONT color=#ff0000>-1</FONT> returned. </DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>Any help and ideas are highly appreciated.&nbsp; </DIV>
<DIV dir=ltr>&nbsp;</DIV>
<DIV dir=ltr>Thank you in advance, </DIV>
<DIV dir=ltr>Mitesh </DIV>
<DIV dir=ltr>&nbsp;</DIV></DIV></BODY></HTML>