Hello
I'm trying to implement a module which would define new syscall in a precompiled Linux 2.6.26 kernel. The goal is to get infos from CPU registers and have access to these info at user level.
I'm running the following kernel: Linux envy 2.6.26-1-xen-686 #1 SMP Sat Nov 8 23:35:27 UTC 2008 i686 GNU/Linux
I wrote the module and the functions i would like to be syscalls. The module compiles, i can load it and the infos my 2 "syscalls" are correct.
My idea was to add the syscalls in the sys_call_table when the module is loaded, and remove the entries when it is unloaded. I located the sys_call_table, and asm/unistd.h told me that the last used syscall number was 326. I wrote the following header
#include
#define __NR_hypgetpid 327
#define __NR_hypgetcomm 328
extern long hypgetpid(unsigned long reg);
extern char * hypgetcomm(unsigned long reg);
And the test.c file to check the access to these syscalls
#include
#include "hypgetpid.h"
int main(void)
{
printf("%ld\n", hypgetpid(0x00000000));
return 0;
}
When compiling it, i have linking errors
% gcc -Wall -O2 test.c -o test
/tmp/ccKDAf4D.o: In function `main':
test.c:(.text+0x19): undefined reference to `hypgetpid'
collect2: ld returned 1 exit status
Which does not really surprises me. I used the macro EXPORT_SYMBOL in the module for my 2 new syscalls. However, i guess i'm missing something so that user level programs can find my syscalls.
Anyone has a clue ? (without recompiling the kernel would be great, the idea was to have something as dynamic as possible).
Thanks
Fred
sysfs
static information is better presented in a virtual filesystem such as sysfs or debugfs. filesystem entries are easier to add at runtime.
That won't work as expected.
EXPORT_SYMBOL and friends make symbols available to other kernel modules, not userspace. Use syscall(2) to invoke your custom syscall.
And there's no easy way to do this within a module, without recompiling the kernel. You could look here if you still want to do it: http://foss.in/2005/slides/Fun_With_LKMs.ppt
You can't add a syscall at
You can't add a syscall at runtime, first off. Take a look at arch/x86/kernel/syscall_table_32.S - there's just no room to add anything.
Second, kernel symbols aren't visible to userspace. Syscalls are numbered, and userspace invokes the syscall gate with the syscall number.
Anyway, use a /proc entry, /sys entry, or /dev device to expose your data - very few things can't be put in this model.
Understanding little by little
Thanks for the comments
I was digging a little bit on my side also, and your comments confirmed what i thought.
I put everything in the module's init function, and managed to find my offsets that way. I took then a look at xenaccess, and realized they did the same thing i did... I learned some thing, but it did not help much in the end.
Now the tough part will be to understand how Xen's memory is organized (if i paste the same code into the hypervisor it does not work). I'll dig into xenaccess to do so
thanks for the help!
When u r using the above
When u r using the above method u have compile the complete kernel.
Please refer to the following link for your intended purpose.
http://tldp.org/LDP/lkmpg/2.6/html/x978.html
Add a syscall
System calls must me compiled into the core kernel image. Also you need to wrap your code with _syscalln macro.