大家都知道linux的应用程序要想访问内核必须使用系统调用从而实现从usr模式转到svc模式。下面咱们看看它的实现过程。
系统调用是os操作系统提供的服务,用户程序通过各种系统调用,来引用内核提供的各种服务,系统调用的执行让用户程序陷入内核,该陷入动作由swi软中断完成。
at91rm9200处理器对应的linux2.4.19内核系统调用对应的软中断定义如下:
#if defined(__thumb__) //thumb模式# O/ @8 C3 o3 k6 _9 v# f* K8 j/ T3 K* K
#define __syscall(name) /
"push {r7}/n/t" /& P7 o, U9 } i; \
"mov r7, #" __sys1(__NR_##name) "/n/t" /
"swi 0/n/t" /
"pop {r7}"1 I8 F. M3 r2 B( h
#else //arm模式
#define __syscall(name) "swi/t" __sys1(__NR_##name) "/n/t"
#endif- P% g2 Z+ L- Q, _1 E3 C( C
: E7 L) _: [5 Y: d! y- l" U
#define __sys2(x) #x
#define __sys1(x) __sys2(x)
#define __NR_SYSCALL_BASE 0x900000 //此为OS_NUMBER << 20运算值
#define __NR_open (__NR_SYSCALL_BASE+ 5) //0x900005
举一个例子来说: open系统调用,库函数最终会调用__syscall(open),宏展开之后为swi #__NR_open,即,swi #0x900005触发中断,中断号0x900005存放在[lr,#-4]地址中,处理器跳转到arch/arm/kernel/entry-common.S中vector_swi读取[lr,#-4]地址中的中断号,之后查询arch/arm/kernel/entry-common.S中的sys_call_table系统调用表,该表内容在arch/arm/kernel/calls.S中定义,__NR_open在表中对应的顺序号为
__syscall_start:/ e3 D3 o* u$ D( p7 |
...
.long SYMBOL_NAME(sys_open) //第5个
...
将sys_call_table[5]中内容传给pc,系统进入sys_open函数,处理实质的open动作
注:用到的一些函数数据所在文件,如下所示
arch/arm/kernel/calls.S声明了系统调用函数
include/asm-arm/unistd.h定义了系统调用的调用号规则4 L' A* p' `( ^3 a
vector_swi定义在arch/arm/kernel/entry-common.S% z4 M+ v4 f; s( b/ N& a* c
vector_IRQ定义在arch/arm/kernel/entry-armv.S
vector_FIQ定义在arch/arm/kernel/entry-armv.S
arch/arm/kernel/entry-common.S中对sys_call_table进行了定义:
.type sys_call_table, #object
ENTRY(sys_call_table), M/ ?3 k! g! P" c6 D' h
#include "calls.S" //将calls.S中的内容顺序链接到这里% C2 [+ l4 b0 @# v
源程序: R8 g6 D9 }: ^/ G8 _( I
ENTRY(vector_swi)
save_user_regs2 s/ K! ^6 h! U9 c T% f) S% F
zero_fp, [6 ^& B# ^- T, q2 m' d' I; \
get_scno //将[lr,#-4]中的中断号转储到scno(r7)
arm710_bug_check scno, ip! C L. z( E: b8 {$ @# P
#ifdef CONFIG_ALIGNMENT_TRAP
ldr ip, __cr_alignment: X$ [2 {& _. U/ `, T2 G0 z
ldr ip, [ip]
mcr p15, 0, ip, c1, c0 @ update control register
#endif
enable_irq ip7 L4 k' E: _. a" r' p. j4 i4 z
f8 J, \" X. d% r( S
str r4, [sp, #-S_OFF]! @ push fifth arg! \7 n' z* F: W' v+ w
get_current_task tsk) \4 x. f b+ E# C3 q: [
ldr ip, [tsk, #TSK_PTRACE] @ check for syscall tracing
bic scno, scno, #0xff000000 @ mask off SWI op-code
//#define OS_NUMBER 9[entry-header.S]
//所以对于上面示例中open系统调用号scno=0x9000059 `+ f4 z5 ], Z3 P, `! @7 t% Q) ^& S
//eor scno,scno,#0x900000$ Y$ C7 h2 p" ^& y& R. C) `
//之后scno=0x053 ]8 O1 k# q9 u
eor scno, scno, #OS_NUMBER << 20 @ check OS number. @. a H$ k5 [; O
//sys_call_table项为calls.S的内容8 r1 }/ @! e4 j
adr tbl, sys_call_table @ load syscall table pointer+ t* Y3 S8 C3 G
tst ip, #PT_TRACESYS @ are we tracing syscalls?
bne __sys_trace
adrsvc al, lr, ret_fast_syscall @ return address2 f% Q4 D- H: j* d. ^) W8 V8 U2 T
cmp scno, #NR_syscalls @ check upper syscall limit, [$ m% Q t6 p
//执行sys_open函数
ldrcc pc, [tbl, scno, lsl #2] @ call sys_* routine8 E% L" j, S: T! z# b. p$ J) M
add r1, sp, #S_OFF
2: mov why, #0 @ no longer a real syscall- X1 |1 i$ a# a2 U) A3 z, m
cmp scno, #ARMSWI_OFFSET: B7 G9 H, X' K- Q7 }- R- I
eor r0, scno, #OS_NUMBER << 20 @ put OS number back
bcs SYMBOL_NAME(arm_syscall) 7 F0 G% z2 _9 c. r1 w# J! k' J5 m
b SYMBOL_NAME(sys_ni_syscall) @ not private func
/*
* This is the really slow path. We're going to be doing# k, e7 T" y( h9 }9 N* D
* context switches, and waiting for our parent to respond.4 b# w6 w; D5 H) s
*/
__sys_trace:
add r1, sp, #S_OFF, j* [3 J% V" T( a3 D2 z% E
mov r0, #0 @ trace entry [IP = 0]: K2 D* {) i. \# C7 Z
bl SYMBOL_NAME(syscall_trace)$ S: m& ]) w% B$ t) z& H- s
/*
//2007-07-01 gliethttp [entry-header.S]
//Like adr, but force SVC mode (if required)9 D6 e0 X' o0 V( [& o4 F8 M
.macro adrsvc, cond, reg, label* K- R# G& a* C8 i- c' `! u% I5 B
adr/cond /reg, /label
.endm
//对应反汇编:; j7 m, S% F# {( y$ O
//add lr, pc, #16 ; lr = __sys_trace_return
*/
adrsvc al, lr, __sys_trace_return @ return address
add r1, sp, #S_R0 + S_OFF @ pointer to regs
cmp scno, #NR_syscalls @ check upper syscall limit
ldmccia r1, {r0 - r3} @ have to reload r0 - r3
ldrcc pc, [tbl, scno, lsl #2] @ call sys_* routine
b 2b
__sys_trace_return:
str r0, [sp, #S_R0 + S_OFF]! @ save returned r0
mov r1, sp
mov r0, #1 @ trace exit [IP = 1]
bl SYMBOL_NAME(syscall_trace)
b ret_disable_irq
.align 5" Z/ l S6 [( @% }
#ifdef CONFIG_ALIGNMENT_TRAP0 H$ I7 F' h* x$ N0 z
.type __cr_alignment, #object
__cr_alignment:$ h9 \; O" N% u- n+ Z1 I9 s; e4 }3 r$ a
.word SYMBOL_NAME(cr_alignment)
#endif5 J3 [. n" s) J4 ]- H
.type sys_call_table, #object
ENTRY(sys_call_table)
#include "calls.S"
| 欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) | Powered by Discuz! X3.2 |