1.将已经下载好的内核解压,我的目录是
/home/shangsongchao/LinuxKernel/testlinux-3.18.6/linux-3.18.6
2.添加系统调用表:
在/home/shangsongchao/LinuxKernel/testlinux-3.18.6/linux-3.18.6/arch/x86/syscalls目录下,打开syscall_32.tbl
添加三个系统调用,如图358,359,360
不需要像Linux 2.6的内核一样,在<asm/unistd.h>中添加类似于#define __NR_foo 358之类的宏定义了,3.x的内核会自动根据系统调用表的定义生成。
3.添加系统调用的服务例程
在/home/shangsongchao/LinuxKernel/testlinux-3.18.6/linux-3.18.6/arch/x86/kernel目录下,打开process_32.c
添加下面函数
/* * sys_foo * return each process kernel stack sizes * */ asmlinkage long sys_foo(void) { return 5; } /* * sys_my_oper * compute numbers + - * / */ asmlinkage long sys_my_oper(int * result , int num1,int num2, char *op) { if(*op==‘+‘) { *result=num1+num2; } else if(*op==‘-‘) { *result=num1-num2; } else if(*op==‘*‘) { *result=num1*num2; } else if(*op==‘/‘) { *result=num1/num2; } else return 0; return 0; } /* * sys_my_compute * compute numbers + - * / */ asmlinkage long sys_my_compute(int num1,int num2, char *op) { long result=0; if(*op==‘+‘) { result=num1+num2; } else if(*op==‘-‘) { result=num1-num2; } else if(*op==‘*‘) { result=num1*num2; } else if(*op==‘/‘) { result=num1/num2; } else result=0; return result; }
如图:
4.编译内核
在/home/shangsongchao/LinuxKernel/testlinux-3.18.6/linux-3.18.6目录下,执行一下命令:
make i386_defconfig make # 一般要编译很长时间,少则20分钟多则数小时
5.安装内核
sudo make modules sudo make modules_install sudo make install
6.重启查看内核版本
uname -r
7.编写C测试程序
#include<linux/unistd.h> #include<sys/syscall.h> #include<stdio.h> #include <time.h> #define _NR_my_oper 359 #define CALL_TIME 1000*1000*100 int main(){ int num1 = 10; int num2 = 5; int result; long i; time_t *t; long end; long begin = (long)(time(t)); for(i =0; i<CALL_TIME ;i++ ){ syscall(_NR_my_oper, &result, num1, num2, "+"); } printf("%d\n",result); end = (long)(time(t)); printf("syscall:%ld\n", end-begin); begin = (long)(time(t)); for(i =0; i<CALL_TIME ;i++ ){ result = num1+num2; } printf("%d\n",result); end = (long)(time(t)); printf("non-syscall:%ld\n", end-begin); return 0; }
参考网址:
http://www.myext.cn/other/a_16300.html
时间: 2024-10-28 20:15:44