博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux System Calls (Linux系统调用)
阅读量:6566 次
发布时间:2019-06-24

本文共 3355 字,大约阅读时间需要 11 分钟。

hot3.png

POSIX APIs and System Calls

The difference between an API and a system call

API: Function definition that specifies how to obtain a given service

System Call: an explicit request to the kernel made via a software interrupt

 

Wrapper routine: routine whose only purpose is to issue a system call

 

POSIX standard refer to a set of APIs and not to system calls.

 

 

System Call Handler and System Routine

The conventions for return values of system calls are different from those of wrapper routines.

0 or positive integers indicate a successful termination of the system call while negative integers indicate a failure. It is the wrapper routines’ responsibility to set errno.

 

System call number: specify the system call to invoke

System call handler: similar to exception handlers

Save registers -> call system call service routine -> load registers and switch back to user mode

 

Naming rules:

System call: xxxx()

System service routine: sys_xxxx()

 

System call dispatch table

System call number ßà service routine

 

 

Entering and Exiting a system call

Two ways to enter and exit a system call

Enter: int $0x80                 Exit: iret

Enter: sysenter                  Exit: sysexit

 

0x80 (128) in Interrupt Descriptor Table

Set_system_gate(0x80, &system_call)

(See interrupt, trap and system gates in chapter 4)

 

 

 

Parameter passing

System call numbers: (example)

#define __NR_restart_syscall                   (__NR_SYSCALL_BASE+  0)

#define __NR_exit                     (__NR_SYSCALL_BASE+  1)

#define __NR_fork                     (__NR_SYSCALL_BASE+  2)

#define __NR_read                    (__NR_SYSCALL_BASE+  3)

#define __NR_write                            (__NR_SYSCALL_BASE+  4)

#define __NR_open                            (__NR_SYSCALL_BASE+  5)

#define __NR_close                            (__NR_SYSCALL_BASE+  6)

                                               /* 7 was sys_waitpid */

#define __NR_creat                            (__NR_SYSCALL_BASE+  8)

#define __NR_link                      (__NR_SYSCALL_BASE+  9)

#define __NR_unlink                           (__NR_SYSCALL_BASE+ 10)

#define __NR_execve                         (__NR_SYSCALL_BASE+ 11)

#define __NR_chdir                            (__NR_SYSCALL_BASE+ 12)

#define __NR_time                    (__NR_SYSCALL_BASE+ 13)

#define __NR_mknod                         (__NR_SYSCALL_BASE+ 14)

#define __NR_chmod                         (__NR_SYSCALL_BASE+ 15)

 

The system call number is set by wrapper routines, so the programmer usually does not need to care about it.

è Set eax register to the system call number

è Write parameters into CPU registers (we cannot write parameters into stacks as usual because the system call cross both kernel stack and user mode stack.

è The kernel copies the parameters into the kernel mode stack

è Call int 0x80 or sysenter

 

(Ordinary C functions use parameters via a stack, either kernel mode stack or user mode stack.)

 

Because we use registers to pass parameters, two conditions must be satisfied:

1.       The length of the parameter cannot exceed the length of a register.

2.       The number of parameters cannot exceed six, besides the system call number passed by eax.

 

These two conditions implies to two things. One, large parameters must be passed by reference. Two, if more than six parameters are needed, a single register is used to point to a memory area in the process address space. Of course, the programmer need not care about this workaround. The wrapper routine will find the appropriate way to pass the parameters to the kernel.

Kernel Wrapper Routines

Although system calls are mainly used by User Mode processes, they can also be invoked by kernel threads, which cannot use library functions.

 

 

 References:

Understanding the Linux Kernel, 3rd

 

 

转载于:https://my.oschina.net/u/158589/blog/59065

你可能感兴趣的文章
MongoDB学习(五)使用Java驱动程序3.3操作MongoDB快速入门
查看>>
About DotNetNunk
查看>>
通用图形分析
查看>>
Python学习笔记(二):标准流与重定向
查看>>
Thrift - GeilThings
查看>>
潜移默化学会WPF--绘图 学习(一)
查看>>
strtok和strtok_r
查看>>
C语言使用正则表达式
查看>>
Ajax解决缓存的5种方法
查看>>
也许每个农村出来的码农都有个田园梦
查看>>
Linux kdb命令
查看>>
J2EE的13种核心技术
查看>>
VMware中安装CentOS7网络配置静态IP地址,常用配置和工具安装
查看>>
【JavaScript吉光片羽】遭遇IE8
查看>>
HTTP请求响应码
查看>>
http://www.fx114.net/qa-24-116329.aspx
查看>>
法总统:英国若“无协议脱欧” 将成最大输家
查看>>
阿里巴巴宣布开源限流降级中间件——Sentinel
查看>>
以OpenGL/ES视角介绍gfx-hal(Vulkan) Framebuffer接口使用
查看>>
我为什么选择使用容器?
查看>>