You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 系统调用的说明以及调用方式
系统调用号存放在eax寄存器中, 参数一般不超过3个分别储存在ebx、ecx、edx寄存器中。
返回值保存在eax中。
主要参考了Linux 5.10 syscalls详细请参见https://man7.org/linux/man-pages/man2/syscalls.2.html 也可以在实验用的Ubuntu虚拟机中用man命令查看如man getdents。
## 系统调用execve2
```
#define __NR_execve2 87
int execve2(const char *path, char * argv[], char * envp[]);
```
* 功能:以立即加载方式执行一个指定的程序。此系统调用结束后,进程运行时不应再发生代码段和数据段中的缺页故障。
* 输入:
- path: 待执行程序路径名称,
- argv: 程序的参数,
- envp: 环境变量的数组指针
* 返回值:成功不返回,失败返回-1
## 系统调用getdents
```
#define __NR_getdents 88
int getdents(unsigned int fd, struct linux_dirent *dirp, unsigned int count);
```
* 功能:获取目录的目录项。
* 输入:
- fd所要读取目录的文件描述符。
- dirp一个缓存区用于保存所读取目录的信息。缓存区的结构如下
```
struct linux_dirent {
long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[14];
};
```
- countdirp的大小。
* 返回值成功执行返回读取的字节数。当到目录结尾则返回0。失败则返回-1。
* current->filp[fd]->f_inode
* 目录文件描述符表
## 系统调用pipe2
```
#define __NR_pipe2 89
int pipe2(int fd[2], int flags);
```
* 功能:创建管道;
* 输入:
- fd[2]: 用于保存2个文件描述符。其中fd[0]为管道的读出端fd[1]为管道的写入端。
- flags: 标志如果为0则pipe2的行为与系统调用pipe相同。
* 返回值成功执行返回0。失败返回-1。
## 系统调用sleep
```
#define __NR_sleep 90
int sleep(unsigned int seconds);
```
* 功能:执行进程睡眠;
* 输入:睡眠的时间间隔;
- seconds: 秒
* 返回值成功返回0失败返回-1;
## 系统调用getcwd
```
#define __NR_getcwd 91
long getcwd(char * buf, size_t size);
```
* 功能:获取当前工作目录;
* 输入:
- char *buf一块缓存区用于保存当前工作目录的字符串。当buf设为NULL由系统来分配缓存区。
- sizebuf缓存区的大小。
* 返回值成功执行则返回当前工作目录的字符串的指针。失败则返回NULL。
## 系统调用mmap
```
#define __NR_mmap 92
long mmap(void *start, size_t len, int prot, int flags,
int fd, off_t off);
```
* 功能:将文件或设备映射到内存中;
* 输入:
- start: 映射起始位置
- len: 长度
- prot: 映射的内存保护方式可取PROT_EXEC, PROT_READ, PROT_WRITE, PROT_NONE
- flags: 映射是否与其他进程共享的标志可取MAP_FILE, MAP_SHARED, MAP_PRIVATE
- fd: 文件句柄
- off: 文件偏移量
* 返回值:成功返回已映射区域的指针,失败返回-1;
## 系统调用munmap
```
#define __NR_munmap 93
int munmap(void * start, size_t len);
```
* 功能:将文件或设备取消映射到内存中;
* 输入:
- start: 映射的指定地址
- len: 区间长度
* 返回值成功返回0失败返回-1;
## 系统调用clone
```
#define __NR_clone 94
int clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
```
* 功能:创建一个子进程;
* 输入:
- fn: 子进程的主函数
- child_stack: 指定新进程的栈可为0。
- flags: 创建的标志如SIGCHLD、CLONE_VM其中CLONE_VM表示子进程共享父进程的地址空间除栈以外
- arg: 主函数的参数
* 返回值成功则返回子进程的线程ID失败返回-1