2002-11-21 19:53:27

by Matt Young

[permalink] [raw]
Subject: Operations inside a module

Within the open routine of my module I need to open another device; and the
write routine needs to write to the other device.

User space system calls seem to be unavailable to module code.
I know about swapping the DS and ES to fake out other modules; then using
sys_write etc.

Can my code just include the standard syscall lib and use them from kernel
space?

My module code would also like to use user space malloc, is this a problem?

Matt



2002-11-21 20:21:51

by Richard B. Johnson

[permalink] [raw]
Subject: Re: Operations inside a module

On Thu, 21 Nov 2002, Matt Young wrote:

> Within the open routine of my module I need to open another device; and the
> write routine needs to write to the other device.
>
> User space system calls seem to be unavailable to module code.
> I know about swapping the DS and ES to fake out other modules; then using
> sys_write etc.
>
> Can my code just include the standard syscall lib and use them from kernel
> space?
>
> My module code would also like to use user space malloc, is this a problem?
>
> Matt
>

This is a FAQ by people who think a module is just some 'C' code that
you hack.

To use open/close/read/write files, you need a process context. You
can make one by creating a kernel thread. The kernel is some protected
code that performs tasks on behalf of the caller, in the context of
the caller. You can't call the kernel from within the kernel, i.e.,
sys-calls because you will never get back. It's like main() calling main()
in user-mode 'C'. And if you "know about swapping DS and ES", as you
state, then you know nothing and shouldn't get near kernel code.

If you need read or write from a device in the kernel, you just
read or write, just put your request in the queue for that device.
Of course if you really mean "files", then your module is broken by
design.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips).
Bush : The Fourth Reich of America


2002-11-21 20:17:51

by Rik van Riel

[permalink] [raw]
Subject: Re: Operations inside a module

On Thu, 21 Nov 2002, Matt Young wrote:

> Within the open routine of my module I need to open another device; and
> the write routine needs to write to the other device.
>
> User space system calls seem to be unavailable to module code. I know
> about swapping the DS and ES to fake out other modules; then using
> sys_write etc.

You don't need the system calls, you can just call the in-kernel
functions directly. If one of the functions you want isn't exported
you could either use an exported alternative or export it yourself
by patching your kernel. However, chances are that if it's not
exported it'll be changed or gone 10 kernels down the line, so you
don't want to use it anyway. ;)

> My module code would also like to use user space malloc, is this a
> problem?

Yes. This is absolutely impossible.

Think of the kernel as a shared library, a shared library that
causes the process that calls a function from it to switch into
supervisor mode.

regards,

Rik
--
A: No.
Q: Should I include quotations after my reply?

http://www.surriel.com/ http://distro.conectiva.com/