Linux添加一个新的系统调用

实验环境:Vmware 7.1.13 操作系统:Redhat 9 (内核为2.4.20-8)

 

一、实验目的

理解操作系统与应用程序的接口关系;加深对内核空间和用户空间的理解;学会增加新的系统调用。


二、实验内容与要求

首先增加一个系统调用函数,然后连接新的系统调用,重建新的Linux内核,用新的内核启动系统,使用新的系统调用(2.6.18内核)


三、实验指导(2.6.18版本)

(1)获得源代码,本次实验的内核版本是2.6.18。将内核源代码保存在/usr/src目录下

(2)打开终端,进入/usr/src目录,将源代码解压。会出现linux-2.6.18目录

(3)修改linux-2.6.18目录中的3个文件

             第一,编辑/usr/src/linux-2.6.18/kernel/sys.c文件,添加函数:

asmlinkage longsys_mycall(long number) 
{
      printk(“call number is %d\n”, number);
      return 0;
}

             第二,修改/usr/src/linux-2.6.18/include/asm-i386/unistd.h

添加一行#define__NR_mycall 324 到当前的最大系统调用号之后,比如原来最大的是323,在323的这一行之后,加上一行 #define __NR_mycall 324

修改 #define NR_systemcalls 的值,改成原来的值+1,比如原来是324 改成325

             第三,编辑/usr/src/linux-2.6.18/arch/i386/kernel/syscall_table.S,在文件最后加上一行:

.long sys_mycall

(4)重新编译内核

在终端输入命令,进入源代码文件夹,cd/usr/src/linux-2.6.18,依次执行如下命令:

make mrproper

make clean

make menuconfig 

需要配置的有:
模块:
Loadable ModulesupportàModule unloading
Loadable ModulesupportàAutomatic kernel module loading
 
设备:
DeviceDriversàBlock DevicesàLoopback device support 
DeviceDriversàMulti-device support(RAID and LVM)àdevice mapper  support
DeviceDriversàGraphics supportàSupport for frame buffer devices
DeviceDriversàUSB supportàUSB Mass Storage support
DeviceDriversàNetwork device supportàEthernet (10 or 100Mbit) à AMD PCnet32 PCIsuppor
DeviceDriversàSCSI device support
Device DriversàSCSI device support àSCSI disk support
DeviceDriversàSCSI device supportàSCSI low-level driversàBusLogic SCSI support
 
文件系统
File systemàSecond extended fs support
File systemàExt2 extended attributes
File systemàExt2 POSIX Access Control Lists
File systemàExt2 Security Labels
File systemàExt3 journalling file system support 
File systemàExt3 extended attributes
File systemàExt3 POSIX Access Control Lists
File systemàExt3 Security Labels
File systemà JBD(ext3) debugging support
FilesystemàDOS/FAT/NT FilesystemsàNTFS file system support

make bzImage

make modules

makemodules_install


cp./drivers/scsi/BusLogic.o /lib/modules/2.6.18/kernel/drivers/scsi/

mkinitrd/boot/xxxx.img 2.6.18

cp/usr/src/linux-2.6.18/arch/i386/boot/bzImage /boot/xxxx.vmlinuz

 

填写grub启动项目: vi/boot/grub/grub.conf

在grub.conf文件的最后添加如下:

title myLinux (2.6.18)
      root (hd0,0)
      kernel /xxxx.vmlinuz ro root=/dev/sda3
      initrd /xxxx.img

(5)重新启动系统,使用新生成的内核。

(6)编写测试程序,测试新系统的使用。

testcall.c

int main()
{
syscall(324,100);/*324是新添加的系统调用号,100是参数*/
return 0;
}

终端编译 gcc–o testcall testcall.c

运行 ./testcall

 

查看结果:终端运行命令dmesg 会看到在最后一行输出call number is 100

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值