📄 linux 内核解读入门.htm
字号:
<BR><BR>1.arch/i386/boot/bootsect.S <BR><BR>2.arch/i386/Kernel/setup.S
<BR><BR>3.arch/i386/boot/compressed/head.S
<BR><BR>4.arch/i386/kernel/head.S <BR><BR>5.init/main.c
<BR><BR>6.arch/i386/kernel/traps.c <BR><BR>7.arch/i386/kernel/entry.S
<BR><BR>8.arch/i386/kernel/irq.h <BR><BR>9.include/asm-386/unistd.h
<BR><BR>当然,这只是涉及到的几个主要文件。而事实上,增加系统调用真正要修改文件只有include/asm-386/unistd.h和arch/i386/kernel/entry.S两个;
<BR><BR>【三】 对内核源码的修改: <BR><BR>1.在kernel/sys.c中增加系统服务例程如下:
<BR><BR>asmlinkage int sys_addtotal(int numdata) <BR><BR>{ <BR><BR>int
i=0,enddata=0; <BR><BR>while(i<=numdata) <BR><BR>enddata+=i++;
<BR><BR>return enddata; <BR><BR>} <BR><BR>该函数有一个 int 型入口参数 numdata ,
并返回从 0 到 numdata 的累加值; 当然也可以把系统服务例程放在一个自己定义的文件或其他文件中,只是要在相应文件中作必要的说明;
<BR><BR>2.把 asmlinkage int sys_addtotal( int) 的入口地址加到sys_call_table表中:
<BR><BR>arch/i386/kernel/entry.S 中的最后几行源代码修改前为: <BR><BR>... ...
<BR><BR>.long SYMBOL_NAME(sys_sendfile) <BR><BR>.long
SYMBOL_NAME(sys_ni_syscall) /* streams1 */ <BR><BR>.long
SYMBOL_NAME(sys_ni_syscall) /* streams2 */ <BR><BR>.long
SYMBOL_NAME(sys_vfork) /* 190 */ <BR><BR>.rept NR_syscalls-190
<BR><BR>.long SYMBOL_NAME(sys_ni_syscall) <BR><BR>.endr <BR><BR>修改后为:
... ... <BR><BR>.long SYMBOL_NAME(sys_sendfile) <BR><BR>.long
SYMBOL_NAME(sys_ni_syscall) /* streams1 */ <BR><BR>.long
SYMBOL_NAME(sys_ni_syscall) /* streams2 */ <BR><BR>.long
SYMBOL_NAME(sys_vfork) /* 190 */ <BR><BR>/* add by I */ <BR><BR>.long
SYMBOL_NAME(sys_addtotal) <BR><BR>.rept NR_syscalls-191 <BR><BR>.long
SYMBOL_NAME(sys_ni_syscall) <BR><BR>.endr <BR><BR>3. 把增加的 sys_call_table
表项所对应的向量,在include/asm-386/unistd.h 中进行必要申明,以供用户进程和其他系统进程查询或调用:
<BR><BR>增加后的部分 /usr/src/linux/include/asm-386/unistd.h 文件如下: <BR><BR>...
... <BR><BR>#define __NR_sendfile 187 <BR><BR>#define __NR_getpmsg 188
<BR><BR>#define __NR_putpmsg 189 <BR><BR>#define __NR_vfork 190
<BR><BR>/* add by I */ <BR><BR>#define __NR_addtotal 191
<BR><BR>4.测试程序(test.c)如下: <BR><BR>#include <BR><BR>#include
<BR><BR>_syscall1(int,addtotal,int, num) <BR><BR>main() <BR><BR>{
<BR><BR>int i,j; <BR><BR><BR> do <BR><BR>printf(\"Please input a
number\\n\"); <BR><BR>while(scanf(\"%d\",&i)==EOF);
<BR><BR>if((j=addtotal(i))==-1) <BR><BR>printf(\"Error occurred in
syscall-addtotal();\\n\"); <BR><BR>printf(\"Total from 0 to %d is %d
\\n\",i,j); <BR><BR>}
<BR><BR>对修改后的新的内核进行编译,并引导它作为新的操作系统,运行几个程序后可以发现一切正常;在新的系统下对测试程序进行编译(*注:由于原内核并未提供此系统调用,所以只有在编译后的新内核下,此测试程序才能可能被编译通过),运行情况如下:
<BR><BR>$gcc -o test test.c <BR><BR>$./test <BR><BR>Please input a
number <BR><BR>36 <BR><BR>Total from 0 to 36 is 666 <BR><BR>可见,修改成功;
<BR><BR>而且,对相关源码的进一步分析可知,在此版本的内核中,从/usr/src/linux/arch/i386/kernel/entry.S
<BR><BR>文件中对 sys_call_table
表的设置可以看出,有好几个系统调用的服务例程都是定义在/usr/src/linux/kernel/sys.c 中的同一个函数:
<BR><BR>asmlinkage int sys_ni_syscall(void) <BR><BR>{ <BR><BR>return
-ENOSYS; <BR><BR>} <BR><BR>例如第188项和第189项就是如此: <BR><BR>... ...
<BR><BR>.long SYMBOL_NAME(sys_sendfile) <BR><BR>.long
SYMBOL_NAME(sys_ni_syscall) /* streams1 */ <BR><BR>.long
SYMBOL_NAME(sys_ni_syscall) /* streams2 */ <BR><BR>.long
SYMBOL_NAME(sys_vfork) /* 190 */ <BR><BR>... ... <BR><BR>而这两项在文件
/usr/src/linux/include/asm-386/unistd.h 中却申明如下: <BR><BR>... ...
<BR><BR>#define __NR_sendfile 187 <BR><BR>#define __NR_getpmsg 188 /*
some people actually want streams */ <BR><BR>#define __NR_putpmsg 189 /*
some people actually want streams */ <BR><BR>#define __NR_vfork 190
<BR><BR>由此可见,在此版本的内核源代码中,由于asmlinkage int sys_ni_syscall(void)
函数并不进行任何操作,所以包括 getpmsg, putpmsg 在内的好几个系统调用都是不进行任何操作的,即有待扩充的空调用;
但它们却仍然占用着sys_call_table表项,估计这是设计者们为了方便扩充系统调用而安排的;
所以只需增加相应服务例程(如增加服务例程getmsg或putpmsg),就可以达到增加系统调用的作用。 <BR><BR>结语:当然对于庞大复杂的
linux
内核而言,一篇文章远远不够,而且与系统调用相关的代码也只是内核中极其微小的一部分;但重要的是方法、掌握好的分析方法;所以上的分析只是起个引导的作用,而正真的分析还有待于读者自己的努力。:)
<BR><BR>
<DIV align=right>发布人:netbull 来自:JJ的Linux世界 </DIV><BR></UL><IMG
src="Linux 内核解读入门.files/line.jpg"><BR>
<FORM action=post.php?skin=reart&ID=678 method=post>
<UL>-- 发表评论 --<BR> 昵称:<INPUT name=name> Email:<INPUT
name=email><BR> 内容:<BR><TEXTAREA name=content rows=5 cols=56></TEXTAREA><BR> <INPUT type=submit value=确定回复> <INPUT type=reset value=清除></UL></FORM><IMG
src="Linux 内核解读入门.files/line.jpg"><BR>
<UL>
<LI>以下是对此文的评论:<BR><BR>昵称:Wonder Email:duke_richard@china.c<BR>我正在编写一个新的操作系统(并非借用Linux内核),分析Linux的内核代码使我解决了一些关键性的难题。
<HR width="100%" noShade SIZE=1>
昵称:guoguo Email:guoxinwei@sina.com.<BR>很好的文章
<HR width="100%" noShade SIZE=1>
昵称:鲨鱼 Email:reichtiger@yeah.net<BR>我喜欢这里!!太好了!!
<HR width="100%" noShade SIZE=1>
昵称:无帆 Email:t_hling@163.net<BR>谢谢你的文章!!!
<HR width="100%" noShade SIZE=1>
昵称:gaoshan Email:gaoshan96@sina.com<BR>这里的好文章太多了,我已经在这里好几天了,收获颇丰.
<HR width="100%" noShade SIZE=1>
昵称:weidy Email:ls_weidy@163.net<BR>难得有这么好的一篇文章,让我有路可走,从此可进入内核领地。
<HR width="100%" noShade SIZE=1>
</LI></UL></TD><!--第三列-->
<TD vAlign=top align=left width="25%"> <IMG
src="Linux 内核解读入门.files/online.jpg" border=0>
<UL>
<LI><A href="http://www.heblinux.org/addnews.php"
target=_blank>新闻发布</A><BR><BR>
<LI><A href="http://www.heblinux.org/addart.php"
target=_blank>文献发布</A><BR><BR>
<LI><A href="http://www.heblinux.org/addsoft.php"
target=_blank>软件发布</A><BR><BR>
<LI><A href="http://www.heblinux.org/manger.php"
target=_blank>软件管理</A><BR></LI></UL><IMG height=5
src="Linux 内核解读入门.files/tabledi2.jpg" width="100%"><!--下载排行--> <IMG
src="Linux 内核解读入门.files/download.jpg" border=0> <BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=676">Red Hat
Linux</A> (78904)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=93">星际译王1.31版</A> (36834)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=219">Oracle9i
Enterprise</A> (36781)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=785">kylix</A> (35589)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=287">AOL
server</A> (27226)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=969">RedHat
中文环境</A> (25499)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=647">Chinput</A> (23572)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=1041">中软Linux</A> (21135)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=946">RedHat Linux
7.1正式</A> (20829)<BR> <A
href="http://www.heblinux.org/view.php?skin=soft&id=134">XteamLinux</A> (20642)<BR><IMG
height=5 src="Linux 内核解读入门.files/tabledi2.jpg" width="100%">
<!--文摘分类--> <IMG src="Linux 内核解读入门.files/artstyle.jpg" border=0>
<BR> <A
href="http://www.heblinux.org/arttype.php?dno=1">内核分析</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=2">网络技术及应用</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=3">应用编程</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=4">硬件应用</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=5">软件应用</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=6">系统管理</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=7">数据库应用</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=8">系统安全</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=9">Linux中文化</A><BR> <A
href="http://www.heblinux.org/arttype.php?dno=10">市场与观点</A><BR><IMG
height=5 src="Linux 内核解读入门.files/tabledi2.jpg" width="100%"> <BR><!--合作伙伴--> <IMG src="Linux 内核解读入门.files/friends.jpg" border=0>
<BR> <A href="http://www.5ilinux.com/"
target=_blank>我爱linux</A><BR> <A
href="http://www.heblinux.org/"
target=_blank>河北LINUX协会</A><BR> <A
href="http://www.xteamlinux.com.cn/"
target=_blank>冲浪软件下载中心</A><BR> <A
href="http://cosoft.org.cn/html/"
target=_blank>共创联盟</A><BR> <A
href="http://www.coventive.com.cn/"
target=_blank>XLinux</A><BR> <A
href="http://www.it365.net/"
target=_blank>诺金软件电脑网络</A><BR> <A
href="http://www.ch2000.com.cn/"
target=_blank>中文2000软件</A><BR> <A
href="http://www.csuu.com/"
target=_blank>中国Unix联盟</A><BR> <A
href="http://freewares.cn/"
target=_blank>自由软件在中国</A><BR> <A
href="http://www.pconline.com.cn/pcedu"
target=_blank>太平洋电脑信息网</A><BR> <A
href="http://www.linuxaid.com.cn/"
target=_blank>LinuxAid</A><BR> <A
href="http://www.cosoft.org.cn/"
target=_blank>共创软件联盟</A><BR> <A
href="http://wsdn.org/"
target=_blank>WEB程序开发网络</A><BR> <A
href="http://linux.softhouse.com.cn/"
target=_blank>软件屋Linux之家</A><BR> <A
href="http://ljb.vpnet.cn/"
target=_blank>随意网络</A><BR> <A
href="http://linux.ccidnet.com/"
target=_blank>赛迪网Linux专区</A><BR></TD></TR></TBODY></TABLE>
<CENTER>Completed in 0.0557750463486 seconds</CENTER>
<CENTER>COPYRIGHT 2002-2003 <FONT color=#9b2626>LinuxByte.net</FONT> <A
href="mailto:oneteam@mail.linuxbyte.net">联系本站</A></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -