系統調用是內核實現的,內核向用戶態提供服務,內核向用戶態提供系統調用的接口是一個軟中斷。
也就是int 0x80.如果用戶直接調用系統調用會很麻煩。下面就舉個直接調用系統調用的例子。用匯編實現的。
- # hello.s ----> intel彙編的註釋用的; 而ATT用的#
- # display a string "Hello, world."
- .section .rodata
- msg:
- .ascii "Hello, world.\n"
- .section .text
- .globl _start
- _start:
- movl $2, %eax #調用fork系統調用
- int $0x80
- movl $4, %eax # system call 系統調用號(sys_write)
- movl $1, %ebx # file descriptor 參數一:文件描述符(stdout)
- movl $msg, %ecx # string address 參數二:要顯示的字符串
- movl $14, %edx # string length 參數三:字符串長度
- int $0x80 # 調用內核功能
- movl $1, %eax # 系統調用號(sys_exit)
- movl $0, %ebx # 參數一:退出代碼
- int $0x80 # 調用內核功能
都對系統調用進行了封裝。而且都是遵循POSIX標準的。
---------------------------------------------------------------------------------------------
2,什麼是POSIX?
POSIX是一套操作系統標準,它是隨着UNIX標準化而誕生的。linux是遵循POSIX標準的操作系統,如果
linux遵循POSIX標準的話,UNIX上的應用程序就可以順利移植到linux上來了。形象點說也就是linux和UNIX
很多函數名字及其參數都定義的是一樣的。
---------------------------------------------------------------------------------------------
3,舊方式封裝(淘汰了),__syscall0() ~ __syscall6()
這幾個宏實際上是利用了系統調用參數的個數不能超個6個來進行分類的。爲了內核的安全考慮。從內核2.6.18開始該宏就從頭文件中刪除了,其實完全也可以自己加入到 指定的頭文件中去。該宏被syscall()取代了,syscall()是glibc中的函數。而上面的_syscall()宏是不依賴於庫的。
- #define __syscall_return(type, res) \
- do { \
- if ((unsigned long)(res) >= (unsigned long)(-MAX_ERRNO)) { \
- res = -1; \
- } \
- return (type) (res); \
- } while (0)
- #define _syscall0(type,name) \
- type name(void) \
- { \
- long __res; \
- __asm__ volatile ("int $0x80" \
- : "=a" (__res) \
- : "0" (__NR_##name)); \
- __syscall_return(type,__res); \
- }
- #define _syscall1(type,name,type1,arg1) \
- type name(type1 arg1) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)) : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall2(type,name,type1,arg1,type2,arg2) \
- type name(type1 arg1,type2 arg2) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)) \
- : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
- type name(type1 arg1,type2 arg2,type3 arg3) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)) : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
- type name (type1 arg1, type2 arg2, type3 arg3, type4 arg4) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "0" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)),"S" ((long)(arg4)) : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
- type5,arg5) \
- type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
- { \
- long __res; \
- __asm__ volatile ("push %%ebx ; movl %2,%%ebx ; movl %1,%%eax ; " \
- "int $0x80 ; pop %%ebx" \
- : "=a" (__res) \
- : "i" (__NR_##name),"ri" ((long)(arg1)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
- : "memory"); \
- __syscall_return(type,__res); \
- }
- #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
- type5,arg5,type6,arg6) \
- type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \
- { \
- long __res; \
- struct { long __a1; long __a6; } __s = { (long)arg1, (long)arg6 }; \
- __asm__ volatile ("push %%ebp ; push %%ebx ; movl 4(%2),%%ebp ; " \
- "movl 0(%2),%%ebx ; movl %1,%%eax ; int $0x80 ; " \
- "pop %%ebx ; pop %%ebp" \
- : "=a" (__res) \
- : "i" (__NR_##name),"0" ((long)(&__s)),"c" ((long)(arg2)), \
- "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)) \
- : "memory"); \
- __syscall_return(type,__res); \
- }
4,新方式封裝。(現在的封裝方式)
現在對系統調用的封裝都是用的syscall函數,該函數是不定參數,很好用,可以調用所有的系統調用。
- 1 #include
<linux/unistd.h>
- 2 #include <syscall.h>
- 3 #include <sys/types.h>
- 4 #include <stdio.h>
- 6
- 7
- 8 int main(void)
- 9 {
- 10 long pid1;
- 11 pid1 = syscall(SYS_getpid);
//getpid
- 12 printf("pid = %ld\n",pid1);
- 13 return 0;
- 14 }
5,問題:我自己添加了一個系統調用,想在用戶態下測試下,代碼如下:
- 1 #include
<linux/unistd.h>
- 2 #include <syscall.h>
- 3 #include <sys/types.h>
- 4 #include <stdio.h>
- 5
- 6
- 7 int main(void)
- 8 {
- 9 long n = 0;
- 10 n = syscall(SYS_mysyscall,190);
- 11 return 0;
- 12 }
解決辦法:
1,/usr/include/bits/syscall.h文件中添加:#define SYS_mysyscall __NR_mysyscall
2,/usr/include/i386-linux-gnu/asm/unistd_32文件末尾添加:
#define __NR_mysyscall 345
這樣,你添加的系統調用和系統本身自帶的系統調用的用戶態使用上就一模一樣了。
---------------------------------------------------------------------------------------------