访问内核空间,linux提供了系统调用,分为多类,
1. 文件系统相关的接口open,read,等file_operations中的接口(具体有什么,看具体文件系统)
2. 进程控制
3. 进程通信
4. 内存管理
5. 系统控制 reboot
6. 用户管理
如何查看当前运行或者编译的 linux系统中有多少个 系统调用, 以及分别是什么
编译时 : cat System. mmap | grep " sys_" , 这里面不仅包括系统调用, 还有一些其他的东西, 例如 sys_mmap_pgoff
编译时 : arch/ arm/ kernel/ entry- common. S 中的 syscall_table_start 和 syscall_table_end 间的 calls- eabi. S 和 calls- oabi. S 文件 , 这里面就是所有的系统调用
运行时 : sys_call_table 数组
系统调用号 不能超过 444
每个系统调用的入口都在
只不过里面的内容分为两种情况
1. 配置为n, 对应shmget的处理函数 为 sys_ni_syscall
2. 配置为y, 对应shmget的处理函数 为 sys_shmget
对于 arm- linux , 系统调用号 总共有 444 ( 442 经过4 字节对齐) 个, 也就是 有 444 个入口
入口0 为 系统调用 syscall
入口1 为 系统调用 exit
. . .
入口441 为 系统调用 epoll_pwait2
入口442 为 系统调用 ni_syscall
入口443 为 系统调用 ni_syscall
arch/ arm/ kernel/ entry- common. S 中 定义的 sys_call_table 数组
# define NATIVE ( nr, func) syscall nr, func
syscall_table_start sys_call_table
# define COMPAT ( nr, native, compat) syscall nr, native
# ifdef CONFIG_AEABI
# include <calls-eabi.S>
# else
# include <calls-oabi.S>
# endif
# undef COMPAT
syscall_table_end sys_call_table
用户空间 glibc 封装了 swi 指令, 实现了 系统调用, 在实现中, 采用
r7 存 系统调用号码 , r0 - r6 存参数
并用swi 进入 异常
linux中
arch/ arm/ kernel/ entry- armv. S 中的 . L__vectors_start 中的 W ( ldr) pc, . L__vectors_start + 0x1000 是 linux 的第一行代码
vector_swi 是 第二行代码
开始设置 sp , 保存 r0 - r12 到 sp , 保存 sp lr 到 sp , 保存 cspr
设置 tbl 为 sys_call_table 的地址
如果CONFIG_OABI_COMPAT= y, 考虑是不是走的 old ABI , 重新设置 tbl
设置 scno
设置栈
设置r10
invoke_syscall tbl, scno, r10, __ret_fast_syscall
sys_xxx
__ret_fast_syscall
disable_irq_notrace
bne fast_work_pending
fast_work_pending:
slow_work_pending:
mov r0, sp
mov r2, why
bl do_work_pending
beq no_work_pending
no_work_pending:
arch_ret_to_user r1, lr
ct_user_enter save = 0
restore_user_regs fast = 0 , offset = 0
其他
sh scripts/ checksyscalls. sh arm- linux- gnueabi- gcc - Wp, - MMD, . / . missing- syscalls. d
- nostdinc - isystem / usr/ lib/ gcc- cross/ arm- linux- gnueabi/ 5 / include - I. / arch/ arm/ include - I. / arch/ arm/ include/ generated
- I. / include - I. / arch/ arm/ include/ uapi - I. / arch/ arm/ include/ generated/ uapi
- I. / include/ uapi - I. / include/ generated/ uapi - include . / include/ linux/ kconfig. h - include . / include/ linux/ compiler_types. h
- D__KERNEL__ - mlittle- endian - Wall - Wundef - Werror= strict- prototypes - Wno- trigraphs - fno- strict- aliasing - fno- common - fshort- wchar
- fno- PIE - Werror= implicit- function- declaration - Werror= implicit- int - Werror= return - type - Wno- format- security - std= gnu89 - fno- dwarf2- cfi- asm
- fno- omit- frame- pointer - mapcs - mno- sched- prolog - fno- ipa- sra - mabi= aapcs- linux - mfpu= vfp - marm - Wa, - mno- warn- deprecated - D__LINUX_ARM_ARCH__= 7
- march= armv7- a - msoft- float - Uarm - fno- delete- null- pointer- checks - O2 -- param= allow- store- data- races= 0 - Wframe- larger- than= 1024 - fstack- protector- strong
- Wno- unused- but- set- variable - fno- omit- frame- pointer - fno- optimize- sibling- calls - g - pg - Wdeclaration- after- statement - Wvla - Wno- pointer- sign
- Wno- array- bounds - Wno- maybe- uninitialized - fno- strict- overflow - fno- stack- check - fconserve- stack - Werror= date- time - Werror= incompatible- pointer- types
- Werror= designated- init - DKBUILD_MODFILE= '"./missing-syscalls"' - DKBUILD_BASENAME= '"missing_syscalls"'
- DKBUILD_MODNAME= '"missing_syscalls"'
其实就是
以 ignore_list 和 arch/ x86/ entry/ syscalls/ syscall_32. tbl 中的内容 为参考, 做出一个 . c 文件
然后 arm- linux- gnueabi- gcc - E 预处理这个 . c 文件, 得到的结果输出到 / dev/ null 中
结论是
这个文件用到了, 但是过程毫无意义
arch/arm/include/generated/calls-eabi.S 的生成过程
arch/ arm/ tools/ Makefile
quiet_cmd_systbl = SYSTBL $@
cmd_systbl = $( CONFIG_SHELL) '$(systbl)' '$<' '$@' \
'$(systbl_abi_$(basetarget))'
systbl_abi_calls- oabi : = common, oabi
$( gen) / calls- oabi. S: $( syscall) $( systbl) FORCE
$( call if_changed, systbl)
cmd_arch/ arm/ include/ generated/ calls- eabi. S : = sh './arch/arm/tools/syscalltbl.sh' 'arch/arm/tools/syscall.tbl' 'arch/ arm/ include/ generated/ calls- eabi. S' ' common, eabi'
syscall 0 , sys_restart_syscall
syscall 1 , sys_exit
. . .
syscall 12 , sys_chdir
syscall 14 , sys_mknod