Skip to contentSkip to conversation

Operations inside a module

DISCUSSION Matt Young 3 participants 3 messages Nov 21, 2002

lore
Share & tools
lore

Matt Young

author
#0 lore raw

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

#1 lore raw
Quoted 6 lines · Matt Young

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. ;)

Quoted 2 lines

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

Signature · 4 lines
A: No.
Q: Should I include quotations after my reply?

http://www.surriel.com/		http://distro.conectiva.com/
#2 lore raw
In reply to Matt Young
Quoted 7 lines · Matt Young

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.

Quoted 7 lines

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