为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

添加一个系统调用

2018-02-02 6页 doc 21KB 3阅读

用户头像

is_180829

暂无简介

举报
添加一个系统调用添加一个系统调用 修改Linux内核增加系统调用 一,修改内核 增加系统调用只修改/usr/src/linux-2.4.29/include/asm-i386/unistd.h和arch/i386/kernel/entry.S,系统调用函数一般在kernel/sys.c中,这里把增加的系统调用代码也加入这个文件中。 1.修改kernel/sys.c文件,加入自己的系统调用代码,同参考文献(见文后地址)中, asmlinkage int sys_addtotal(int numdata) { int i=0,end...
添加一个系统调用
添加一个系统调用 修改Linux内核增加系统调用 一,修改内核 增加系统调用只修改/usr/src/linux-2.4.29/include/asm-i386/unistd.h和arch/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; } 计算从0到numdata的累加值。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/unist d.h 中进行必要申明,以供用户进程和其他系统进程查询或调用: #define __NR_exit_group 252 #define __NR_addtotal 259 /*这是增加的第259个系统调用*/ 然后编译内核make bzImage,并用生成的新内核启动系统。 4.测试程序(test.c)如下: #include #include 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.c 中start_kernel调用i386/kernel/traps.c中trap_init)时设置的,trap_init中 set_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 signa ls 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(s ys_call_table)(,%eax,4)调用我们asmlinkage int sys_addtotal(int numdat a)。 上面的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:从系统调用返回,如果需要调度或有信号待处理则去调度或处理,这超出了本文的范围,最后返回用户空间。
/
本文档为【添加一个系统调用】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索