Linux添加系统调用【傻瓜版】

wamaker posted @ 2010年10月31日 18:53 in 操作系统 with tags 内核 , 7296 阅读

       刚回来就听说操作系统要上机,内容是给linux系统添加一个系统调用,依据参数是否大于0返回两个数。表示毫无思路,Google了一下,发现网上的教程很多(难道各学校的作业都一样么?),但是写的都比较烦而且用的内核都比较旧,有些步骤在新内核的编译是没必要的。按照他们写的来做把我纠结死了,所以搞成了以后打算写一个简易的新的教程,希望让后人能少走些弯路。

环境:Ubuntu 10.04

准备工作:Linux内核(在这里可以下载,我用的是2.6.36)。

具体步骤:

Step 1:使用root权限,没有设置root的可以执行以下命令来设置密码

 

sudo passwd root

输入su和密码获取root权限。

 

su

 

Step 2:把内核压缩包移至/usr/src,并解压

 

mv linux-2.6.36.tar.bz2 /usr/src
tar -jxvf linux-2.6.36.tar.bz2 

 

Step 3: 修改内核,共需修改3个文件。就如同小区里新搬进一家住户,首先去物业那里注册一下,获得门牌号(这号是连续的,在定义编号的时候杯具了一回)。

 

gedit /usr/src/linux-2.6.36/arch/x86/kernel/syscall_table_32.S

 

打开以后,在最后添加

 

.long sys_mysyscall

 

你的新调用的编号就是上一行的编号+1,记住这个编号,后面要用。

接下来写函数的实现,就相当于把家具搬进去。

 

gedit /usr/src/linux-2.6.36/kernel/sys.c

 

在最后添加

 

asmlinkage long sys_mysyscall(int number)
{
	if(number>0)return 123456
	else return 654321;
}

 

最后,为了方便访客,还需要到传达室的王大爷那儿登记一下,这样以后访客只要报上你的名字就可以知道你家的门牌号了。

 

gedit /usr/src/linux-2.6.36/arch/x86/include/asm/unistd_32.h

 

最后添加

 

#define __NR_mysyscall 341       //这341是之前记下的那个编号

 

Step 4:利用root权限进入/usr/src,清除以前编译的内核,第一次编译的话可以跳过

 

make mrproper

 

Step 5:定制内核。

 

make menuconfig

 

刚装的系统可能会缺少ncurse库而导致这一步失败(没有跳出一个蓝色的窗口),从网上下一个ncurse.tar.gz,解压后进入解压出来的文件夹,依次执行

 

./configure
make
make install

Step 6:编译内核!!!利用root权限在/usr/src/linux-2.6.36/下

 

make

漫长的等待(在虚拟机耗了一个多小时)

Step 7:生成文件并安装内核

 

make modules_install
make install

Step 8:生成引导文件

 

mkinitramfs -o initrd.img-2.6.36 2.6.36

完成以后将生成的initrd.img -2.6.36复制到/boot

 

mv initrd.img-2.6.36 /boot

Step 9:更新引导列表

 

update-grub

Step 10:重新启动

启动完以后在终端执行

 

uname -a

看看你的版本号是否已经变成2.6.36 。成了的话就恭喜了!

Step 11:测试

编一个测试程序:

 

#include<stdio.h>
#inlcude<unistd.h>

int main()
{
	printf(“%d\n%d\n”,syscall(341,1),syscall(341,0));//这里的341是之前的编号,请自行更改
	return 0;
}

编译执行一下,看结果对不?

终于写完了,貌似我也写得有些繁琐了,毕竟是修改内核嘛。随着版本号和RP函数的波动,以上步骤不能保证一定能出结果,但大体原理是相同的,如果有童鞋杯具了。。。多Google多试几次。。。。

  • 无匹配
Avatar_small
Kflayca 说:
2010年11月14日 04:17

……unistd.h的函数声明是怎么生成的……中间没有修改啊

Avatar_small
wamaker 说:
2010年11月17日 05:40

@Kflayca:
你指的是这一句吗?
“#define __NR_mysyscall 341 //这341是之前记下的那个编号”
在2.6的内核里要改的是unistd_32.h
只要在最后一行添上#define 两下划线NR下划线+函数名空格再加上之前记录的编号就好了啊?

Avatar_small
DDO Request 说:
2022年8月01日 15:20

Drawing and Disbursing Officer is a major and important role in today’s developing India. As every organization has come forward with an official online human resource and development system software. The salaries, payslip, and other important details of an employee taken care of by the Drawing and Disbursing Officer. They play an important role in the organization. DDO Request They reduce the manual work and with their individual effort, the department employees salaries may disbursed. The payment against working days and the leaves taken along with many other criteria observed by a DDO officer. Even an employee who wants to enjoy the services of fficial website will need to reach out to DDO officers. It is all to raise a request for their employee registration.

Avatar_small
Fred Meyer Hours 说:
2023年1月27日 16:14

Aiming to provide customers all across the United States of America with one-stop shopping, Meyer opened their stores in different locations in the USA states. Now it is one of the well-known superstores where you can find everything that you would need to get on with your day. Fred Meyer Hours They became super famous with their idea to provide customers with grocery, shopping, pharmacy, clothing, jewelry, home & decor, electrical, and many more.

Avatar_small
CBSE 7th Class Revi 说:
2023年9月02日 18:31

NCERT new Syllabus 2024 for Class 7 Contains Complete Information about Curriculum, course Structure Exam Pattern etc. The information Available in CBSE Class 7th new Syllabus 2024 is important for the Preparation CBSE 7th Class Revised Syllabus 2024 of CBSE Class 7th Board Exams 2024.NCERT 7th New Revised Syllabus 2024 Chapter Wise Pdf Download.Online Service Covers All Subjects Published by NCERT Syllabus 2024 for Standard VII in Hindi, English and Urdu medium Chapter Wise Pdf Format, our Website Providing the latest NCERT 7th Syllabus 2024 for all Important Subjects.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter