当前位置: 首页 -  必威体育 - 正文

ARM Linux:usr模式转为svc模式的实现原理

2020-06-20 15:20 必威体育 熏染86°c
A+ A-

get_current_task tsk

//所以对于上面示例中open系统调用号scno=0x900005

cmp scno, #NR_syscalls @ check upper syscall limit

adrsvc al, lr, __sys_trace_return @ return address

//2007-07-01 gliethttp [entry-header.S]

mcr p15, 0, ip, c1, c0 @ update control register

adr/cond /reg, /label

#endif

bic scno, scno, #0xff000000 @ mask off SWI op-code

eor scno, scno, #OS_NUMBER 《《 20 @ check OS number

“swi 0/n/t” /

enable_irq ip

#include “calls.S”

adr tbl, sys_call_table @ load syscall table pointer

。。.

zero_fp

ldrcc pc, [tbl, scno, lsl #2] @ call sys_* routine

get_scno //将[lr,#-4]中的中断号转储到scno(r7)

bl SYMBOL_NAME(syscall_trace)

#include “calls.S” //将calls.S中的内容顺序链接到这里

“pop {r7}”

ENTRY(vector_swi)

#define __syscall(name) “swi/t” __sys1(__NR_##name) “/n/t”

#endif

add r1, sp, #S_OFF

#define __NR_SYSCALL_BASE 0x900000 //此为OS_NUMBER 《《 20运算值

//对应反汇编:

ldr ip, [tsk, #TSK_PTRACE] @ check for syscall tracing

cmp scno, #NR_syscalls @ check upper syscall limit

.type sys_call_table, #object

str r0, [sp, #S_R0 + S_OFF]! @ save returned r0

.type __cr_alignment, #object

add r1, sp, #S_OFF

b ret_disable_irq

include/asm-arm/unistd.h定义了系统调用的调用号规则

bcs SYMBOL_NAME(arm_syscall)

注:用到的一些函数数据所在文件,如下所示

tst ip, #PT_TRACESYS @ are we tracing syscalls?

__cr_alignment:

vector_swi定义在arch/arm/kernel/entry-common.S

#define __sys1(x) __sys2(x)

__sys_trace:

arch/arm/kernel/entry-common.S中对sys_call_table进行了定义:

save_user_regs

* context switches, and waiting for our parent to respond.

at91rm9200处理器对应的linux2.4.19内核系统调用对应的软中断定义如下:

#endif

“mov r7, #” __sys1(__NR_##name) “/n/t” /

#if defined(__thumb__) //thumb模式

* This is the really slow path. We‘re going to be doing

//之后scno=0x05

__sys_trace_return:

bne __sys_trace

cmp scno, #ARMSWI_OFFSET

2: mov why, #0 @ no longer a real syscall

//sys_call_table项为calls.S的内容

add r1, sp, #S_R0 + S_OFF @ pointer to regs

.type sys_call_table, #object

//#define OS_NUMBER 9[entry-header.S]

ENTRY(sys_call_table)

大家都知道linux的应用程序要想访问内核必须使用系统调用从而实现从usr模式转到svc模式。下面咱们看看它的实现过程。

arch/arm/kernel/calls.S声明了系统调用函数

//eor scno,scno,#0x900000

#else //arm模式

将sys_call_table[5]中内容传给pc,系统进入sys_open函数,处理实质的open动作

//add lr, pc, #16 ; lr = __sys_trace_return

arm710_bug_check scno, ip

bl SYMBOL_NAME(syscall_trace)

.long SYMBOL_NAME(sys_open) //第5个

*/

*/

//Like adr, but force SVC mode (if required)

__syscall_start:

b 2b

mov r1, sp

。。.

/*

ldr ip, [ip]

ldmccia r1, {r0 - r3} @ have to reload r0 - r3

vector_IRQ定义在arch/arm/kernel/entry-armv.S

//执行sys_open函数

“push {r7}/n/t” /

vector_FIQ定义在arch/arm/kernel/entry-armv.S

ldrcc pc, [tbl, scno, lsl #2] @ call sys_* routine

.align 5

.endm

#define __sys2(x) #x

mov r0, #1 @ trace exit [IP = 1]

str r4, [sp, #-S_OFF]! @ push fifth arg

ldr ip, __cr_alignment

eor r0, scno, #OS_NUMBER 《《 20 @ put OS number back

#ifdef CONFIG_ALIGNMENT_TRAP

#define __syscall(name) /

/*

adrsvc al, lr, ret_fast_syscall @ return address

源程序:

.macro adrsvc, cond, reg, label

#define __NR_open (__NR_SYSCALL_BASE+ 5) //0x900005

ENTRY(sys_call_table)

mov r0, #0 @ trace entry [IP = 0]

举一个例子来说: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在表中对应的顺序号为

#ifdef CONFIG_ALIGNMENT_TRAP

系统调用是os操作系统提供的服务,用户程序通过各种系统调用,来引用内核提供的各种服务,系统调用的执行让用户程序陷入内核,该陷入动作由swi软中断完成。

.word SYMBOL_NAME(cr_alignment)

b SYMBOL_NAME(sys_ni_syscall) @ not private func