修改Linux内核增加系统调用(转载)

来源:互联网 发布:业余足球数据app 编辑:程序博客网 时间:2024/05/17 07:07

 修改Linux内核增加系统调用

出处:xdkui 的 Blog

 

本文修改内核2.4.29,分两部分,第一部分修改内核并测试,第二部分解释从用户态调用新系统调用的过程。在Intel处理器上,可以通过调用门和软中断两种方式实现系统调用。Linux选择软中断的方式,内核通过软中断(int $0x80)给用户提供服务,即系统调用。本文参考了文后给出地址的文章,甚至系统调用代码也是从中而来,本文重在解释其调用过程。

 

一,修改内核

增加系统调用只修改/usr/src/linux-2.4.29/include/asm-i386/unistd.harch/i386/kernel/entry.S,系统调用函数一般在kernel/sys.c中,这里把增加的系统调用代码也加入这个文件中。

 

1.修改kernel/sys.c文件,加入自己的系统调用代码,同参考文献(见文后地址)中,

asmlinkage int sys_addtotal(int numdata)

{

int i=0,enddata=0;

while(i<=numdata)

enddata+=i++;

return enddata;

}

计算从0numdata的累加值。asmlinkage表示通过堆栈递参数。

 

2.然后把sys_addtotal(int )的入口地址添加到sys_call_table表中。该表依次存储所有系统调用的入口地址。

修改前为:

.long SYMBOL_NAME(sys_ni_syscall)    /* sys_set_tid_address 这是第258个系统调用* /

.rept NR_syscalls-(.-sys_call_table)/4

   .long SYMBOL_NAME(sys_ni_syscall)

修改后:

.long SYMBOL_NAME(sys_ni_syscall)       /* sys_set_tid_address *        /

.long SYMBOL_NAME(sys_addtotal)          /*这是增加的第259个系统调用*/

.rept NR_syscalls-(.-sys_call_table)/4-1      /*这里重复次数减少1*/

       .long SYMBOL_NAME(sys_ni_syscall)

 

3. 把增加的 sys_call_table 表项所对应的向量,include/asm-i386/unistd.h 中进行必要申明,以供用户进程和其他系统进程查询或调用:

#define __NR_exit_group         252

#define __NR_addtotal           259   /*这是增加的第259个系统调用*/

 

然后编译内核make bzImage,并用生成的新内核启动系统。

 

4.测试程序(test.c)如下:

#include <stdio.h>

#include <asm/unistd.h>

int errno;

 

_syscall1(int,addtotal,int,num);//_syscall1表示该系统调用有1个参数,同样_syscall2表示2个调用参数

 

main()

{

        int i,j;

        printf("Please input a number/n");

        while(scanf("%d",&i)==EOF);

        j=addtotal(i);

        printf("Total from 0 to %d is %d/n",i,j);

}

 

编译:gcc -I/usr/src/linux-2.4.29/include test.c

运行即可。

 

二,下面解释调用系统调用的过程

测试程序test.c中的_syscall1是定义在include/asm-i386/unistd.h中的宏:

#define _syscall1(type,name,type1,arg1) /

type name(type1 arg1) /

{ /

long __res; /

__asm__ volatile ("int $0x80" /

        : "=a" (__res) /

        : "0" (__NR_##name),"b" ((long)(arg1))); /

__syscall_return(type,__res); /

}

其中__syscall_return也定义在该文件中

#define __syscall_return(type, res) /

do { /

         if ((unsigned long)(res) >= (unsigned long)(-125)) { /

                errno = -(res); /

                 res = -1; /

        } /

        return (type) (res); /

} while (0)

所以test.c_syscall1(int,addtotal,int,num)展开后即:

int addtotal(int num)

{

long __res;

       __asm__ volatile(“int $0x80”

                                   :”=a”(__res)

                                   :”0”(__NR_addtotal),”b”((long)(num)));

       do {

         if ((unsigned long)(__res) >= (unsigned long)(-125)) {

                errno = -(__res);

                 __res = -1;

        }

        return (int) (__res);

} while (0)

}

通过软中断int $0x80,其中系统调用号为eax中的__NR_##name,这里也就是__NR_addtotal,在上面的步骤3中有#define __NR_addtotal 259,即259号系统调用。寄存器ebx中存第一个参数num

 

IDT中第0x80个门(其类型为15,即陷阱门)为系统启动(init/main.cstart_kernel调用i386/kernel/traps.ctrap_init)时设置的,trap_initset_system_gate(0x80,&system_call);int $0x80指令通过该系统门后转到内核的system_call处执行。

 

system_call定义在arch/i386/kernel/entry.S中:

ENTRY(system_call)                   //转到此处执行

        pushl %eax                      # save orig_eax

        SAVE_ALL                   //把寄存器压入堆栈

        GET_CURRENT(%ebx)

        testb $0x02,tsk_ptrace(%ebx)    # PT_TRACESYS

        jne tracesys

        cmpl $(NR_syscalls),%eax

        jae badsys

 call *SYMBOL_NAME(sys_call_table)(,%eax,4) //此时eax=系统调用号=__NR_addtotal=259

        movl %eax,EAX(%esp)             # save the return value

ENTRY(ret_from_sys_call)          //从系统调用返回

        cli                             # need_resched and signals atomic test          cmpl $0,need_resched(%ebx)

        jne reschedule       //如果需要重新调度则跳去调度

        cmpl $0,sigpending(%ebx)

        jne signal_return

restore_all:   

        RESTORE_ALL

 

sys_call_table即第一部分2中修改的部分,可以看成一个函数指针数组,按下标指向对应系统调用的函数地址,此处即call *SYMBOL_NAME(sys_call_table)(,%eax,4)调用我们asmlinkage int sys_addtotal(int numdata)

上面的SAVE_ALL是定义在arch/i386/kernel/entry.S中的宏:

#define SAVE_ALL /

        cld; / 

        pushl %es; /

        pushl %ds; /

        pushl %eax; /

        pushl %ebp; /

        pushl %edi; /

        pushl %esi; /

        pushl %edx; /

        pushl %ecx; /

        pushl %ebx; /

        movl $(__KERNEL_DS),%edx; /

        movl %edx,%ds; /

        movl %edx,%es;

所以进入系统调用函数后(本文是sys_addtotal),堆栈中参数对应SAVE_ALL中的ebx,即num

在系统调用时,Linux在用户空间通过寄存器而不是堆栈传递参数(可以从宏_systemcall0,_systemcall1,……_systemcall5看到),这些传递参数的寄存器是:eax系统调用号,如果有参数的话,ebx,ecx,edx,esi,edi依次存第一个到第五个参数。系统调用时,传递的参数最多5个。进入内核system_call后通过SAVE_ALL把寄存器压入堆栈,系统调用函数定义时asmlinkage int sys_addtotal(int numdata)asmlinkage表示通过堆栈传递参数。进入系统调用函数后看到堆栈中第一个参数即ebx,第二个参数为ecx,对应了用户空间5个参数的顺序。当然,如果系统调用定义时只有一个参数,则只会使用ebx(位于堆栈中)一个参数了。

 

系统调用返回时保存返回值在寄存器eax中。然后到ret_from_sys_call:从系统调用返回,如果需要调度或有信号待处理则去调度或处理,这超出了本文的范围,最后返回用户空间。

 

参考文献:http://joyfire.net/jln/kernel/9.html 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


作者Blog:http://blog.csdn.net/xdkui/