2003-02-18 12:41:02

by Sudharsan Vijayaraghavan

[permalink] [raw]
Subject: Help !! calling function in module from a user program

Hi,

Am a new bee to linux internals.
I am trying to make a simple program witch will call a function from a
module. I made a module compiled it and INSMOD-it into kernel, that works
fine. I would like to call from my user program a function defined in my
kernel module.

Please suggest any method thro' which this could be accomplished.
The only way i did it was by running my new module as insmod mymodule.o and
get my job done.

Thanks,
Sudharsan.


2003-02-18 12:58:51

by Alex Bennee

[permalink] [raw]
Subject: Re: Help !! calling function in module from a user program

On Tue, 2003-02-18 at 12:50, Sudharsan Vijayaraghavan wrote:
> Hi,
>
> Am a new bee to linux internals.
> I am trying to make a simple program witch will call a function from a
> module. I made a module compiled it and INSMOD-it into kernel, that works
> fine. I would like to call from my user program a function defined in my
> kernel module.

You never call functions directly from user-mode. You generally use a
syscall to interface between user-mode and kernel-mode. Most drivers
implement operations that map via these syscalls (i.e.
open/close/read/write etc).

What exactly are you trying to achieve?
--
Alex, homepage: http://www.bennee.com/~alex/

How much does it cost to entice a dope-smoking UNIX system guru to Dayton?
-- Brian Boyle, UNIX/WORLD's First Annual Salary Survey

2003-02-18 13:33:50

by Richard B. Johnson

[permalink] [raw]
Subject: Re: Help !! calling function in module from a user program

On Tue, 18 Feb 2003, Sudharsan Vijayaraghavan wrote:

> Hi,
>
> Am a new bee to linux internals.
> I am trying to make a simple program witch will call a function from a
> module. I made a module compiled it and INSMOD-it into kernel, that works
> fine. I would like to call from my user program a function defined in my
> kernel module.
>
> Please suggest any method thro' which this could be accomplished.
> The only way i did it was by running my new module as insmod mymodule.o and
> get my job done.
>
> Thanks,
> Sudharsan.

Unix/Linux uses open() close() read() write() and ioctl() (plus a few
others) to interface with modules or any kind of driver. To 'call' some
module function from user-mode, you impliment open() and close(). That
will provide a file-descriptor for subsequent operations. Then you
impliment either read() write() or ioctl() or all, whichever is
most appropriate for the function your module is going to provide.

You never 'call' a kernel function directly from user-mode. One of
the kernel's primary functions is to make this impossible. Kernel
code is protected from direct user-mode access.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Why is the government concerned about the lunatic fringe? Think about it.


2003-02-18 13:33:22

by Srinivas Chinta

[permalink] [raw]
Subject: Re: Help !! calling function in module from a user program

#include <stdio.h>
#include <errno.h>
#include <asm/unistd.h>
#define __NR_my_func 250

_syscall0(void, my_func);

main()
{
my_func();
}


Attachments:
module.c (463.00 B)
module.c
user_space.c (145.00 B)
user_space.c
Download all attachments

2003-02-18 14:00:58

by Alex Bennee

[permalink] [raw]
Subject: Re: Help !! calling function in module from a user program

On Tue, 2003-02-18 at 13:43, Srinivas Chinta wrote:
> Hi,
> One way of doing this is , by hooking up your function
> inside the module as a system call.
> Here i'm sending two files, module.c and user_space.c.
> first do "insmod module.o" and then run
> "./user_space".
> As i'm also a newbee, i'm not aware of the
> disadvantages of this approach.

The main disadvantage is your driver/module becomes a specialized device
that can only be accessed by special syscall incantaion by one
application. The Un*x way is about keeping every thing as standard as
possible so there is easy interaction between user-mode programs
accessing the device even if they don't know the details of the
internals.

Most devices can be treated as char devices or block devices and are
coded as such. This way you can dump the contents of your hard-drive by
doing "cat /dev/hda1 > dump" without cat having intimate knowledge of
what a hard drive is and how to access one.

Of course there will always be times when new system calls need to be
added but generally this should be done sparinginly.

--
Alex, homepage: http://www.bennee.com/~alex/

He missed an invaluable opportunity to hold his tongue.
-- Andrew Lang