添加系统调用
准备工作:
linux-2.6.33.tar.bz2
解压
cd linux-2.6.33
在kernel/sys.c中添加系统调用
1 SYSCALL_DEFINE2(mul, int, x, int, y)
2 {
3 int c;
4 printk("hello user's syscall\n");
5 printk("invoke sys_mul\n");
6 c = x*y;
7 printk("%d * %d = %d\n", x, y, c);
8 return (long)c;
9 }
2 {
3 int c;
4 printk("hello user's syscall\n");
5 printk("invoke sys_mul\n");
6 c = x*y;
7 printk("%d * %d = %d\n", x, y, c);
8 return (long)c;
9 }
在arch/x86/kernel/syscall_table_32.S种添加
1 .long sys_mul
修改完毕,开始编译
$ zcat /prog/config.gz > .config
$ make xconfig
$ make
# make modules_install
# cp -v arch/x86/boot/bzImage /boot/vmlinuz-2.6.33-ARCH
# mkinitcpio -k 2.6.33-ARCH -g /boot/kernel26-ARCH.img
$ make xconfig
$ make
# make modules_install
# cp -v arch/x86/boot/bzImage /boot/vmlinuz-2.6.33-ARCH
# mkinitcpio -k 2.6.33-ARCH -g /boot/kernel26-ARCH.img
make xconfig 可以直接保存
make 需要很长的时间
编译安装完毕
修改menu.lst
title MyArch
root (hd0,4)
kernel /boot/vmlinuz-2.6.33-ARCH root=/dev/sda5 ro
initrd /boot/kernel26-ARCH.img
root (hd0,4)
kernel /boot/vmlinuz-2.6.33-ARCH root=/dev/sda5 ro
initrd /boot/kernel26-ARCH.img
重启
--------------------- 我是分割线 ------------------
测试
1 #include <unistd.h>
2 #include <stdio.h>
3
4 #define __NR_mul 338
5
6 int main()
7 {
8 long c = 0;
9 int x = 4;
10 int y = 9;
11 c = syscall(__NR_mul, x, y);
12 printf("%d * %d = %d\n", x, y, c);
13 return 0;
14 }
15
2 #include <stdio.h>
3
4 #define __NR_mul 338
5
6 int main()
7 {
8 long c = 0;
9 int x = 4;
10 int y = 9;
11 c = syscall(__NR_mul, x, y);
12 printf("%d * %d = %d\n", x, y, c);
13 return 0;
14 }
15
运行结果
[chenwl@myhost ~]$ ./a.out
4 * 9 = 36
[chenwl@myhost ~]$ dmesg | tail
fuse init (API version 7.13)
NET: Registered protocol family 10
lo: Disabled Privacy Extensions
ADDRCONF(NETDEV_UP): wlan0: link is not ready
hello user's syscall
invoke sys_mul
4 * 9 = 36
hello user's syscall
invoke sys_mul
4 * 9 = 36
4 * 9 = 36
[chenwl@myhost ~]$ dmesg | tail
fuse init (API version 7.13)
NET: Registered protocol family 10
lo: Disabled Privacy Extensions
ADDRCONF(NETDEV_UP): wlan0: link is not ready
hello user's syscall
invoke sys_mul
4 * 9 = 36
hello user's syscall
invoke sys_mul
4 * 9 = 36
[chenwl@myhost ~]$ uname -r
2.6.33-ARCH