ldd3-chapter5-syscall

說明System Call


更新記錄

item note
20160613 第一版

目錄


system call

  • 通過library使用方式如下圖
  • Invoking the system call handler and executing a system call,來源

system call

  • 一般app都是通過library來呼叫kernl的system call
  • 也可以直接呼叫kernl的system call
    • ex.使用ioctl

linux kernel system call

於kernel, arch/arm/include/asm/unistd.h內有定義system call number

system call number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define __NR_OABI_SYSCALL_BASE  0x900000 

#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)
#define __NR_lchown (__NR_SYSCALL_BASE+ 16)

cpu system table">比對cpu system table

name register r7
restart_syscall 0x900000
exit 0x900001
fork 0x900002
read 0x900003
write 0x900004

通過ioctl的參數傳遞

  • 來源 ldd3 chapter5
  • 系統呼叫需檢查所有的參數,以確保它們是有效且合法的
  • 系統提供了兩個方法來進行必要的檢查,及在kernel space及user space之間進行所需要的複製

  • copy_to_user: Copy a block of data into user space.

1
2
3
unsigned long copy_to_user (	void __user *  	to,
const void * from,
unsigned long n)
;

parameter description
to Destination address,in user space
from Source address, in kernel space
n Number of bytes to copy.
  • copy_from_user: Copy a block of data from user space.
1
2
3
unsigned long copy_from_user (	void *  	to,
const void __user * from,
unsigned long n)
;

parameter description
to Destination address, in kernel space
from Source address, in user space
n Number of bytes to copy.

參考來源