golang syscall 系统调用认知
1 本文整体结构
- C语言中syscall如何使用?
- golang中如何使用?
- syscall 手册
2 C语言中syscall如何使用?
#define _GNU_SOURCE#include <unistd.h>#include <sys/syscall.h>#include <sys/types.h>#include <signal.h>int main(int argc, char *argv[]){pid_t tid;tid = syscall(SYS_gettid);syscall(SYS_tgkill, getpid(), tid, SIGHUP);}
执行结果:fish: Job 1, './a.out' terminated by signal SIGHUP (Terminal hung up)
3 golang中如何使用
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDITfunc EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error) {_, _, e1 := RawSyscall6(SYS_EPOLL_CTL, uintptr(epfd), uintptr(op), uintptr(fd), uintptr(unsafe.Pointer(event)), 0, 0)if e1 != 0 {err = errnoErr(e1)}return
}
func syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) func syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) func syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) func rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno) func rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno) func syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err Errno)
Syscall在开始和结束的时候,会分别调用runtime中的进入系统和退出系统的函数,所以Syscall是受调度器控制的,因为调度器有开始和结束的的事件。而RawSyscall则不受调度器控制,
RawSyscall 可能会导致其他正在运行的线程(协程)阻塞,调度器可能会在一段时间后运行它们,但是也有可能不会。所以,我们在进行系统调用的时候,应该极力避免使用RawSyscall,除非你确定这个操作是非阻塞的。
4 syscall 手册
SYSCALL(2) Linux Programmer's Manual SYSCALL(2)NAMEsyscall - indirect system callSYNOPSIS#define _GNU_SOURCE /* See feature_test_macros(7) */#include <unistd.h>#include <sys/syscall.h> /* For SYS_xxx definitions */long syscall(long number, ...);DESCRIPTIONsyscall() is a small library function that invokes the system call whose assembly language interface has the specified number with the specified arguments. Employing syscall()is useful, for example, when invoking a system call that has no wrapper function in the C library.syscall() saves CPU registers before making the system call, restores the registers upon return from the system call, and stores any error code returned by the system call inerrno(3) if an error occurs.Symbolic constants for system call numbers can be found in the header file <sys/syscall.h>.RETURN VALUEThe return value is defined by the system call being invoked. In general, a 0 return value indicates success. A -1 return value indicates an error, and an error code is storedin errno.NOTESsyscall() first appeared in 4BSD.Architecture-specific requirementsEach architecture ABI has its own requirements on how system call arguments are passed to the kernel. For system calls that have a glibc wrapper (e.g., most system calls), glibchandles the details of copying arguments to the right registers in a manner suitable for the architecture. However, when using syscall() to make a system call, the caller mightneed to handle architecture-dependent details; this requirement is most commonly encountered on certain 32-bit architectures.For example, on the ARM architecture Embedded ABI (EABI), a 64-bit value (e.g., long long) must be aligned to an even register pair. Thus, using syscall() instead of the wrapperprovided by glibc, the readahead() system call would be invoked as follows on the ARM architecture with the EABI in little endian mode:syscall(SYS_readahead, fd, 0,(unsigned int) (offset & 0xFFFFFFFF),(unsigned int) (offset >> 32),count);Since the offset argument is 64 bits, and the first argument (fd) is passed in r0, the caller must manually split and align the 64-bit value so that it is passed in the r2/r3register pair. That means inserting a dummy value into r1 (the second argument of 0). Care also must be taken so that the split follows endian conventions (according to the CABI for the platform).Similar issues can occur on MIPS with the O32 ABI, on PowerPC with the 32-bit ABI, and on Xtensa.Note that while the parisc C ABI also uses aligned register pairs, it uses a shim layer to hide the issue from userspace.The affected system calls are fadvise64_64(2), ftruncate64(2), posix_fadvise(2), pread64(2), pwrite64(2), readahead(2), sync_file_range(2), and truncate64(2).This does not affect syscalls that manually split and assemble 64-bit values such as _llseek(2), preadv(2), preadv2(2), pwritev(2). and pwritev2(2). Welcome to the wonderfulworld of historical baggage.Architecture calling conventionsEvery architecture has its own way of invoking and passing arguments to the kernel. The details for various architectures are listed in the two tables below.The first table lists the instruction used to transition to kernel mode (which might not be the fastest or best way to transition to the kernel, so you might have to refer tovdso(7)), the register used to indicate the system call number, the register used to return the system call result, and the register used to signal an error.arch/ABI instruction syscall # retval error Notes────────────────────────────────────────────────────────────────────alpha callsys v0 a0 a3 [1]arc trap0 r8 r0 -arm/OABI swi NR - a1 - [2]arm/EABI swi 0x0 r7 r0 -arm64 svc #0 x8 x0 -blackfin excpt 0x0 P0 R0 -i386 int $0x80 eax eax -ia64 break 0x100000 r15 r8 r10 [1]m68k trap #0 d0 d0 -microblaze brki r14,8 r12 r3 -mips syscall v0 v0 a3 [1]nios2 trap r2 r2 r7parisc ble 0x100(%sr2, %r0) r20 r28 -powerpc sc r0 r3 r0 [1]s390 svc 0 r1 r2 - [3]s390x svc 0 r1 r2 - [3]superh trap #0x17 r3 r0 - [4]sparc/32 t 0x10 g1 o0 psr/csr [1]sparc/64 t 0x6d g1 o0 psr/csr [1]tile swint1 R10 R00 R01 [1]x86_64 syscall rax rax - [5]x32 syscall rax rax - [5]xtensa syscall a2 a2 -Notes:[1] On a few architectures, a register is used as a boolean (0 indicating no error, and -1 indicating an error) to signal that the system call failed. The actual error valueis still contained in the return register. On sparc, the carry bit (csr) in the processor status register (psr) is used instead of a full register.[2] NR is the system call number.[3] For s390 and s390x, NR (the system call number) may be passed directly with svc NR if it is less than 256.[4] On SuperH, the trap number controls the maximum number of arguments passed. A trap #0x10 can be used with only 0-argument system calls, a trap #0x11 can be used with 0-or 1-argument system calls, and so on up to trap #0x17 for 7-argument system calls.[5] The x32 ABI uses the same instruction as the x86_64 ABI and is used on the same processors. To differentiate between them, the bit mask __X32_SYSCALL_BIT is bitwise-ORedinto the system call number for system calls under the x32 ABI. Both system call tables are available though, so setting the bit is not a hard requirement.The second table shows the registers used to pass the system call arguments.arch/ABI arg1 arg2 arg3 arg4 arg5 arg6 arg7 Notes──────────────────────────────────────────────────────────────alpha a0 a1 a2 a3 a4 a5 -arc r0 r1 r2 r3 r4 r5 -arm/OABI a1 a2 a3 a4 v1 v2 v3arm/EABI r0 r1 r2 r3 r4 r5 r6arm64 x0 x1 x2 x3 x4 x5 -blackfin R0 R1 R2 R3 R4 R5 -i386 ebx ecx edx esi edi ebp -ia64 out0 out1 out2 out3 out4 out5 -m68k d1 d2 d3 d4 d5 a0 -microblaze r5 r6 r7 r8 r9 r10 -mips/o32 a0 a1 a2 a3 - - - [1]mips/n32,64 a0 a1 a2 a3 a4 a5 -nios2 r4 r5 r6 r7 r8 r9 -parisc r26 r25 r24 r23 r22 r21 -powerpc r3 r4 r5 r6 r7 r8 r9s390 r2 r3 r4 r5 r6 r7 -s390x r2 r3 r4 r5 r6 r7 -superh r4 r5 r6 r7 r0 r1 r2sparc/32 o0 o1 o2 o3 o4 o5 -sparc/64 o0 o1 o2 o3 o4 o5 -tile R00 R01 R02 R03 R04 R05 -x86_64 rdi rsi rdx r10 r8 r9 -x32 rdi rsi rdx r10 r8 r9 -xtensa a6 a3 a4 a5 a8 a9 -Notes:[1] The mips/o32 system call convention passes arguments 5 through 8 on the user stack.Note that these tables don't cover the entire calling convention—some architectures may indiscriminately clobber other registers not listed here.
golang syscall 系统调用认知相关推荐
- syscall 系统调用陷入_linux 系统调用open 篇一
内核源码:linux-4.4 目标平台:ARM体系结构 源码工具:source insight 4 说明: 文中由于 md 语法问题,无法在代码高亮的同时而忽略由于 __ 或者 * 造成斜体的 问题, ...
- 【Linux】syscall系统调用原理及实现
一.什么是系统调用 系统调用 跟用户自定义函数一样也是一个函数,不同的是 系统调用 运行在内核态,而用户自定义函数运行在用户态.由于某些指令(如设置时钟.关闭/打开中断和I/O操作等)只能运行在内核态 ...
- golang syscall原理
Golang System call 1.入口 2.系统调用管理 3.runtime 中的 SYSCALL 4.用户代码的系统调用和调度交互 entersyscall和exitsyscall的pipe ...
- linux内核syscall_define6,Syscall系统调用Linux内核跟踪
在Linux的用户空间,我们经常会调用系统调用,下面我们跟踪一下read系统调用,使用的Linux内核版本为Linux2.6.37.不同的Linux版本其中的实现略有不同. 在一些应用中我们可以看到下 ...
- syscall 系统调用陷入_trusty系统调用
trusty中,可以通过系统调用陷入kernel,获取kernel服务. 这里记录一下trusty的系统调用框架结构,代码基于google trusty源码 1.应用程序接口 在文件lib/inclu ...
- syscall 系统调用陷入_系统调用深度剖析(上)
原文:Anatomy of a system call, part 1 翻译:RobotCode俱乐部 系统调用是用户空间程序与Linux内核交互的主要机制.考虑到它们的重要性,不难发现内核包含了各种 ...
- syscall 系统调用
转自:http://blog.csdn.net/b02042236/article/details/6136598 5.1.5 如何使用系统调用 如图5.2所示,用户应用可以通过两种方式使用系统调用 ...
- Golang标准库-syscall(什么是系统调用/Go 语言中的系统调用)
文章目录 一.什么是系统调用 二.Golang标准库-syscall 1. syscall无处不在 2. syscall demo举例: go版本的strace Strace go版本的strace ...
- Golang通过syscall调用win32的Api
2019独角兽企业重金招聘Python工程师标准>>> What you are wasting today is tomorrow for those who died yeste ...
最新文章
- 集中配置管理工具puppet安装使用
- 阿里云云盾-智能风控(公测)发布
- 未来50年的神经科学会是什么样呢?
- 房价越高的地方资产越高?北京户均资产890万?
- ubuntu16.04終端補全忽略大小寫
- Mahout kmeans聚类
- ps学习1:去除图片上的文字
- 标签管理体系之业务应用
- 【uoj#174】新年的破栈 贪心
- MQTT协议学习笔记
- 第11章 支撑向量机 SVM 学习笔记 下 SVM思路解决回归问题
- network 节点label以及相关字体设置
- python增量爬虫_python网络爬虫——增量式爬虫
- 现代薄膜温室大棚五大优点,常用的经济作物都有哪些?
- seo技巧,seo技巧搜行者SEO
- 报名丨2017 GrowingIO 增长大会(北京)
- 安卓无线打印服务器,安卓 打印服务器
- Bose Soundlink Ⅲ 随机断电故障处理
- 3dMAX对电脑配置是怎么样的?
- 签名设计一笔教写过程
热门文章
- Maven构建项目PKIX路径构建失败,无法找到到请求目标的有效证书路径
- SpringBootTest单元测试—加入@Test无法启动测试方法,什么情况下需要加@RunWith(SpringRunner.class)?
- unity标准着色器入门-材质参数(二)
- 王学岗——钉钉视频会议实战,从零手写音视频会议项目
- iphone12锁屏密码忘记了怎么办 (苹果12如何找回密码)
- [AcWing] 1018. 最低通行费(C++实现)数字三角形模型
- 视频混剪素材哪里找?
- 论坛推广需要注意哪些
- 编程命名惯例 | 骆驼式命名法
- adb connection python 一键 wifi 连接