- 论坛徽章:
- 0
|
最近在学习关于Linux的Kernel以及modules方面的内容,今天刚试验成功了一次,特此写出具体过程,分享一下我的经验。
==========================================
Stage 1: To modify the kernel to add our own system call
==========================================
1. Download the Linux kernel 2.6.18 from kernel.org to /usr/src/
cd /usr/src/ && wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.18.tar.gz
2. Un-compress and un-archive
tar zxvf linux-2.6.18.tar.gz
3. Link it to directory linux, from this point on, there a several files that needs to be modified from and added to the kernel source tree.
ln -s linux-2.6.18 linux
4. Edit file /usr/src/linux/arch/i386/kernel/syscall_table.S
Add
.long sys_mysyscall
after the last line of the file.
5. Edit file /usr/src/linux/arch/i386/kernel/Makefile
Add
obj-y += mysyscall.o
6. Create a new file call mysyscall.c under /usr/src/linux/arch/i386/kernel/ directory.
Type the following into the file:
#include <linux/linkage.h>
#include <linux/kernel.h>
asmlinkage int sys_mysyscall(void)
{
printk("This is the my new system call build in the kernel!\n");
return 0;
}
7. Edit file /usr/src/linux/include/asm-i386/unistd.h
Add
#define __NR_mysyscall 318
after
#define __NR_move_pages 317
Modify
#define __NR_syscalls 318
to
#define __NR_syscalls 319
8. At this point, you can start building your new kernel.
make menuconfig && make && make modules_install && make install
9.After a successful build, reboot the machine and boot into the newly built kernel.
===========================
Stage 2: To test the new system call
===========================
10.Create a new file call testsyscall.c in your directory.
Type the following into the file:
#include <linux/unistd.h>
#define __NR_mysyscall 318
_syscall0(long, mysyscall);
int main ()
{
mysyscall();
return 0;
}
11.Compile the above testsyscall.c source code with :
gcci -Wall testsyscall.c -o testsyscall.o
12.If you arrive here without problems, you can now test the newly created system call just type
./testsyscall.o
13. To check if it works, you can use:
dmesg | tail -n 5
you will see there is "This is the my new system call build in the kernel!\n" in the last line. |
|