[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding a new system call



Thank you very much,
I hope now it will work.

On 6/5/05, Kirk True <kernel@xxxxxxxxxxxxxxxxx> wrote:
> Hi,
> 
> > I tried to implement a new system call. I used the instructions of
> > http://user-mode-linux.sourceforge.net/lksct/index.html
> 
> > The coak() is supposed to print a message, we could see it using
> > dmesg. But, it doesnt happen.
> 
> I don't know about UML, but here's a little document I wrote up for
> myself (I'm so forgetful). Perhaps it will work for you :)
> 
> -------------
> 
> Adding a system call is easy. Just follow the following steps:
> 
>     1. Create the new system call
>             1. If the system call is in a new file, add it to the
>                appropriate Makefile
>     2. Add the system call to arch/i386/kernel/syscall_table.S
>     3. Add a #define to include/asm-i386/unistd.h
>     4. Create a user-space application that calls the system call
> 
> Let's take these one by one…
> 
> 1. Create the new system call
> 
> The first order of business is to define the system call itself. I'm not
> sure if there's a particular function signature format to follow or not.
> It does seem, however, that most – if not all – system calls include a
> prefix of "sys_" though this may just be a naming convention.
> 
> Here's an example:
> 
>    asmlinkage int sys_helloworld(void);
> 
> Note that the source file in which the system call is included must
> #include linux/linkage.h (for "asmlinkage").
> 
> 1.1. If the system call is in a new file, add it to the appropriate
> Makefile
> 
> If we added our new system call to a new file, naturally that file must
> be compiled into the kernel image. Therefore it should be added into the
> Makefile in the directory. If you forget this step you may get no-ops
> with your user-space program.
> 
> 2. Add the system call to arch/i386/kernel/syscall_table.S
> 
> This one's really easy: just add a line with the format to the end of
> the file:
> 
>    .long <function_name>
> 
> Example:
> 
>    .long sys_helloworld
> 
> That's it!
> 
> 3. Add a #define to include/asm-i386/unistd.h
> 
> Find the last #define __NR_<system_call_function> and add the new system
> call incrementing the number. Also, the NR_syscalls value should be
> incremented too.
> 
> For instance:
> 
>    #define __NR_keyctl             288
> 
>    #define NR_syscalls 289
> 
> Would become:
> 
>    #define __NR_keyctl             288
>    #define __NR_helloworld         289
> 
>    #define NR_syscalls 290
> 
> 4. Create a user-space application that calls the system call
> 
> To benefit from the system call, you have to invoke it in a user-space
> application. For custom system calls, since there is no nice C library
> wrapper, we have to perform an archaic trick. The _syscallN macro will
> expand into a declaration of our new system call.
> 
> For instance, to call our helloworld system call as defined above, we'd
> need to declare the function signature thusly:
> 
>    _syscall0(int, helloworld);
> 
> The number after _syscall is the number of parameters. I'm assuming the
> first parameter to the macro is the return type. The name provided as
> the second argument is then prepended with the string "__NR_" and looked
> for in include/asm-i386/unistd.h.
> 
> The function can then be invoked thusly:
> 
>    int main(void) {
>        return helloworld();
>    }
> 
> Note: compiling the user-space application when running on a kernel that
> doesn't have the system call will fail to find the entry in include/asm-
> i386/unistd.h. You have to instead tell gcc where to find the
> appropriate include file instead. For me that was done thusly:
> 
>    gcc --include=/usr/src/<your linux kernel>/include/asm-i386/unistd.h <src.c>
> 
> -------------
> 
> Hope this helps...
> 
> Kirk
> 
> 


-- 
Karane.
ž®w¥?ì‰ëzZ^iÈh¶«•æ«?¦èºÛax¸§»®w¥·!Š÷¡¶Úþf¢–yeŠ{±¢¸?‘êçzYÞÁ¸ž³ñ@Bm§ÿäz¹Þ–w°n'¬¢¸?}ª¿