I. An Example: read( )
- User space: User process makes
read()
system call - Interrupt: This will trap into kernel by software interrupt
- System API: Kernel executes its
interrupt handler
, in whichsys_read() API
will be calledcopy_from_user()
the read_args(fd/buff/…)page_alloc()
a temporary buffer- call
do_read()
, andcopy_to_user()
the read bytes page_free()
that buffer- return the number of bytes actually read, or if anything goes wrong
- File system API: Specific file system function
do_read()
will be executed.- 注意:
do_read()
是 file system 的 system call
- 注意:
II. System API for file operation
1. System Read API
1 | int sys_read(read_args_t *arg) |
- 作为 interrupt routine 被调用;发生于 user process
read()
leading to trap as software interrupt into kernel 的时候 copy_from_user()
读取 user processread()
的传入参数- 在 kernel address space 分配一块buffer,作为临时接收数据的区域
- 调用 file system 的
do_read()
,从 disk 中 读取数据到 buffer copy_to_user()
,将buffer 中读取到的数据 copy 到 user address space(低地址空间 0xc0000000 - 0xc0000000)page_free()
,将 buffer 释放- 根据 file system 读取的结果,返回ERROR 或者 读取的字节数
2. System Write API
1 | int sys_write(write_args_t *arg) |
- 在kernel address space 开辟临时buffer,读取放置user 要写的数据
- 从 user address space 拷贝
read()
输入参数,包括要写的数据 - 调用 file system 的
do_write()
操作, write to disk - 释放 kernel address space 的临时 buffer
- 返回ERROR 或者 写入的字符数
3. System Get Directory Entry
1 | int do_getdent(int fd, struct dirent *dirp) |
copy_from_user()
从 user address space 读取参数到 kernel address space(从而知道用户都想干些啥?)- 循环调用
do_getdent()
以及copy_to_user()
,直到达到user process 指定的 字节数目do_getdent()
调用 file system API 读取目录,并将结果放在 dir 中copy_to_user()
将 dir 中的信息 copy 到 user address space 中去;这样就会在用户的 fd 下逐渐显示出读入的 sub dir entry 的名字、大小等信息- 读入的 count_bytes >= user_args.count 时,跳出循环
- 返回实际读取到的目录所占字节数 count_bytes
4. Other System APIs related to File System
1 | int sys_close(int fd) |
以上的 system API 都与 file system 有关。都经过以下过程:
- User process makes system call:
read()
- Traps into kernel as software interrupt:
interrupt handler
- Kernel execute corresponding API:
sys_read()
- Copy from user address space parameters into kernel address space
- Ask File System to complete relavent operation:
do_read()
- Copy to user address space with the result at kernel address space
III. Other System API related to Virtual Memory System
1 | int sys_munmap(munmap_args_t *args) |
这些函数与“用户态” Process/ Thread/ Virtual Memory 的操作有关:
- 由 user process 发起的 system call:
wait()
- 经过software interrupt trap into kernel
- kernel 在调用相应的 system API,代替 user process 执行任务:
sys_waitpid()
- 交给“进程系统” Process& Memory System,来具体处理任务:
do_waitpid()
VI. Interrupt handler for syscalls
1 | void syscall_handler(regs_t *regs) |