版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、中英文日报导航站 边干边学Linux内核指导,Chapter 5: 系统调用,为什么需要系统调用 相关数据和代码 例:系统调用getuid()的实现 添加一个系统调用mysyscall 再实现一个稍复杂的系统调用,中英文日报导航站 边干边学Linux内核指导,为什么需要系统调用(1),中英文日报导航站 边干边学Linux内核指导,为什么需要系统调用(2),中英文日报导航站 边干边学Linux内核指导,相关数据和代码,arch/i386/kernel/traps.c arch/i386/kernel/entry.S 系统调用时的内核栈 sys_call_table system_call和ret
2、_from_sys_call include/linux/unistd.h 系统调用编号 宏定义展开系统调用 glibc展开系统调用INLINE_SYSCALL (getuid, 0);,中英文日报导航站 边干边学Linux内核指导,系统调用时的内核栈,陷入内核时,系统自动从当前进程的TSS(任务状态段)中获得内核栈的SS和ESP,并完成栈切换,中英文日报导航站 边干边学Linux内核指导,系统调用时的内核栈,18 * Stack layout in ret_from_system_call: 19 * ptrace needs to have all regs on the stack. 2
3、0 * if the order here is changed, it needs to be 21 * updated in fork.c:copy_process, signal.c:do_signal, 22 * ptrace.c and ptrace.h 23 * 24 * 0(%esp) - %ebx 25 * 4(%esp) - %ecx 26 * 8(%esp) - %edx 27 * C(%esp) - %esi 28 * 10(%esp) - %edi 29 * 14(%esp) - %ebp 30 * 18(%esp) - %eax 31 * 1C(%esp) - %ds
4、 32 * 20(%esp) - %es 33 * 24(%esp) - orig_eax 34 * 28(%esp) - %eip 35 * 2C(%esp) - %cs 36 * 30(%esp) - %eflags 37 * 34(%esp) - %oldesp 38 * 38(%esp) - %oldss 39 * 40 * current is in register %ebx during any slow entries.,中英文日报导航站 边干边学Linux内核指导,系统调用时的内核栈,#define SAVE_ALL cld; pushl %es; pushl %ds; pu
5、shl %eax; pushl %ebp; pushl %edi; pushl %esi; pushl %edx; pushl %ecx; pushl %ebx; movl $(_KERNEL_DS),%edx; movl %edx,%ds; movl %edx,%es;,中英文日报导航站 边干边学Linux内核指导,系统调用时的内核栈,#define RESTORE_ALLpopl %ebx;popl %ecx;popl %edx;popl %esi;popl %edi;popl %ebp;popl %eax;1:popl %ds;2:popl %es;addl $4,%esp;3:iret
6、;,中英文日报导航站 边干边学Linux内核指导,sys_call_table,398 ENTRY(sys_call_table) 399 .long SYMBOL_NAME(sys_ni_syscall) 400 .long SYMBOL_NAME(sys_exit) 401 .long SYMBOL_NAME(sys_fork) 402 .long SYMBOL_NAME(sys_read) 403 .long SYMBOL_NAME(sys_write) 404 .long SYMBOL_NAME(sys_open) /* 5 */ 635 .long SYMBOL_NAME(sys_n
7、i_syscall) /* reserved for lremovexattr */ 636 .long SYMBOL_NAME(sys_ni_syscall) /* reserved for fremovexattr */ 637 638 .rept NR_syscalls-(.-sys_call_table)/4 639 .long SYMBOL_NAME(sys_ni_syscall) 640 .endr,中英文日报导航站 边干边学Linux内核指导,sys_call_table,中英文日报导航站 边干边学Linux内核指导,system_call,194 ENTRY(system_ca
8、ll) # int 0 x80后执行 195 pushl %eax # save orig_eax 196 SAVE_ALL 197 GET_CURRENT(%ebx) 198 testb $0 x02,tsk_ptrace(%ebx) # PT_TRACESYS 199 jne tracesys 200 cmpl $(NR_syscalls),%eax 201 jae badsys 202 call *SYMBOL_NAME(sys_call_table)(,%eax,4) 203 movl %eax,EAX(%esp) # save the return value 204 ENTRY(r
9、et_from_sys_call) 205 cli,中英文日报导航站 边干边学Linux内核指导,ret_from_sys_call,ENTRY(ret_from_sys_call)cli # need_resched and signals atomic testcmpl $0,need_resched(%ebx)jne reschedulecmpl $0,sigpending(%ebx)jne signal_returnrestore_all:RESTORE_ALL,中英文日报导航站 边干边学Linux内核指导,中英文日报导航站 边干边学Linux内核指导,arch/i386/kernel
10、/traps.c,916 void _init trap_init(void) /初始化中断描述符表IDT 923 set_trap_gate(0,中英文日报导航站 边干边学Linux内核指导,系统调用编号,include/asm-i386/unistd.h 8 #define _NR_exit 1 9 #define _NR_fork 2 10 #define _NR_read 3 11 #define NR_write 4 12 #define NR_open 5 13 #define NR_close 6 240 #define _NR_llistxattr 233 241 #defin
11、e _NR_flistxattr 234 242 #define _NR_removexattr 235 243 #define _NR_lremovexattr 236 244 #define _NR_fremovexattr 237,中英文日报导航站 边干边学Linux内核指导,宏定义展开系统调用,#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) type name(type1 arg1,type2 arg2,type3 arg3) long _res; _asm_ volatile (int $0 x80 : =a
12、 (_res) : 0 (_NR_#name),b (long)(arg1),c (long)(arg2), d (long)(arg3); _syscall_return(type,_res); ,中英文日报导航站 边干边学Linux内核指导,宏定义展开系统调用,movl$_NR_#name, %eax /先为输入参数分配寄存器 movlarg1, %ebx movl arg2, %ecx movlarg3, %edx #APP int$0 x80/汇编代码 #NO_APP movl%eax, _res/最后处理输出参数,中英文日报导航站 边干边学Linux内核指导,例:系统调用getuid
13、()的实现,一个简单的程序,但包含系统调用和库函数调用 #include /* all system calls need this header */ int main() inti = getuid(); printf(“Hello World! This is my uid: %dn”, i); 假定定义了“宏” _syscall0( int, getuid);,中英文日报导航站 边干边学Linux内核指导,例:系统调用getuid()的实现,这个“宏”被getuid()展开后 int getuid(void) long _res; _asm_ volatile (int $0 x80
14、: “=a” (_res) #输出 : “” (_NR_getuid); 输入 _syscall_return(int,_res); 显然,_NR_getuid(24)放入eax,并int 0 x80,中英文日报导航站 边干边学Linux内核指导,例:系统调用getuid()的实现,因为系统初始化时设定了 set_system_gate(SYSCALL_VECTOR, 所以进入system_call 194 ENTRY(system_call) 195 pushl %eax # save orig_eax 196 SAVE_ALL 197 GET_CURRENT(%ebx) 198 testb
15、 $0 x02,tsk_ptrace(%ebx) # PT_TRACESYS 199 jne tracesys 200 cmpl $(NR_syscalls),%eax 201 jae badsys 202 call *SYMBOL_NAME(sys_call_table)(,%eax,4) 203 movl %eax,EAX(%esp),中英文日报导航站 边干边学Linux内核指导,例:系统调用getuid()的实现,注意第202行,此时eax为24 查sys_call_table,得到call指令的操作数sys_getuid16 调用函数sys_getuid16() 145 asmlink
16、age long sys_getuid16(void) 146 147 return high2lowuid(current-uid); 148 ,中英文日报导航站 边干边学Linux内核指导,例:系统调用getuid()的实现,第202行完成后,接着执行第203行后面的指令 203 movl %eax,EAX(%esp) 204 ENTRY(ret_from_sys_call) 205 cli 206 cmpl $0,need_resched(%ebx) 207 jne reschedule 208 cmpl $0,sigpending(%ebx) 209 jne signal_return
17、 210 restore_all: 211 RESTORE_ALL,中英文日报导航站 边干边学Linux内核指导,例:系统调用getuid()的实现,第203行:返回值从eax移到堆栈中eax的位置 假设没有什么意外发生,于是ret_from_sys_call直接到RESTORE_ALL,从堆栈里面弹出保存的寄存器,堆栈切换(iret) 进程回到用户态,返回值保存在eax中 printf打印出 Hello World! This is my uid:551,中英文日报导航站 边干边学Linux内核指导,添加一个系统调用mysyscall,功能要求 调用这个mysyscall ,使用户的uid等
18、于0,中英文日报导航站 边干边学Linux内核指导,添加一个系统调用mysyscall,系统调用的编号名字 _NR_mysyscall 改写/usr/include/asm/unistd.h 240 #define _NR_llistxattr 233 241 #define _NR_flistxattr 234 242 #define _NR_removexattr 235 243 #define _NR_lremovexattr 236 244 #define _NR_fremovexattr 237 245 #define _NR_mysyscall238,中英文日报导航站 边干边学Linux内核指导,添加一个系统调用mysyscall,内核中实现该系统调用的程序的名字 sys_mysyscall 改写arch/i386/kernel/entry.S 398 ENTRY(sys_call_table) 399 .long SYMBOL_NAME(sys_ni_syscall) 636 .long SYMBOL_NAME(sys_
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 译林版(三起)四年级英语上册课件Unit What we wear第2课时(Cartoon time Sounds in focus)
- 矿业权评估师考试资产评估试题(完整版)
- 译林版(三起)三年级英语上册课件Unit 8 I can do this for you第1课时(Get ready)
- 2026导游证笔试全真模拟试题库
- 公路水运工程试验检测专业技术人员职业资格考试《水运材料》真题(含解析)
- 幼儿远离电子产品护眼科普课
- 自然养生顺应天时科普
- 2026 年汛期灾害风险分级防范科普讲解
- 2026 年暴雨山洪塌方常见错误避险方式科普
- 人工智能辅助金融机构决策
- 2026福建福州古厝运营服务有限公司招聘5人考试模拟试题及答案详解
- 湖北省黄冈市2026-2027学年八上数学期末教学质量检测模拟试题含解析
- 2026湖南娄底涟源市明宏水利电力开发有限公司招聘12人笔试参考题库及答案详解
- Q2桥门式起重机考试题题库及答案
- 矿井灾害预防与应急处理全流程培训
- 新版2026年高考地理(陕晋青宁卷)真题详细解读及评析
- 北海市2025广西北海市招聘县级政府统计机构统计协管员(协统员)6人笔试历年参考题库典型考点附带答案详解
- 零星维修施工方案
- 《工业管道安全技术规程》(TSG31-2025)监督检验规则培训
- GB/T 5782-2025紧固件六角头螺栓
- 关于会计的英文词汇
评论
0/150
提交评论