歡迎您光臨本站 註冊首頁

如何在Linux Kernel?刃略鮃桓?ystem Call

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  本文作者: gpmoney
使用 system call 去呼叫系?的函式是非常好玩的,但是要如何??出一??自己的system call 呢???有以下???步?,如果你是自己想要呼叫 system call 那可以跳? (1) (2) 直接到第三???假?你己?熟悉 kernel 的 compile 和如何利用新 compile 的 kernel 重新??C


(1) ?定 include ?n?鵲 syscall function


首先,找到 /usr/inlcude/asm/unistd.h ????n案,在?一行



#define __NR_getdents64 220
#define __NR_fcntl64 221



的後面加上 :


#define __NR_myfunc 222



然後找到 /usr/include/bits/syscall.h ????n案,再加上一行 :


#define SYS_myfunc __NR_myfunc



找到 /usr/src/linux/arch/i386/kernel/entry.S ????n案也是在最後面加上?K修改?擻??榧t色的?二行



.long SYMBOL_NAME(sys_getdents64) /* 220 */
.long SYMBOL_NAME(sys_fcntl64)
.long SYMBOL_NAME(sys_myfunc) --> 增加?一行
#ifdef CONFIG_TUX
.long SYMBOL_NAME(__sys_tux)
#else
# ifdef CONFIG_TUX_MODULE
.long SYMBOL_NAME(sys_tux)
# endif
#endif

/*
* NOTE!! This doesn't have to be exact - we just have
* to make sure we have _enough_ of the "sys_ni_syscall"
* entries. Don't panic if you notice that this hasn't
* been shrunk every time we add a new system call.
*/
.rept NR_syscalls-222 ----> 改成 NR_syscalls-223
.long SYMBOL_NAME(sys_ni_syscall)
.endr




(2) 撰?? syscall 的?常式式


假?你的 linux kernel code 在 /usr/src/linux 下找到 /usr/src/linux/kernel/sys.c

加上以上??仔 :


asmlinkageintsys_myfunc(int input){
printk("<1> Input value is : %d \n",input);
return input*10;
}



改完以後,就可以重新 compile kernel ?K且重新??C了。


(3) 撰?? user space 的小程式



use_syscall.c
#include
#include
#include

static inline _syscall1(int,myfunc,int,a)

int main(void){
printf("Return Value: %d\n",myfunc(10));
}



???絛型暌葬幔?憔塗梢鑰吹竭@??程式?出 100

如果你?有?趣,可以使用 tail -f /var/log/message ??霈F?似的?息,表示你的程式有?由 printk 印到?面上


Sep 3 22:02:02 private kernel: Input value is : 10



_syscall1 是一?? macro 指令,事??上是 _syscallN 的指令 , N 代表系?呼叫所需要用到的??????br>
_syscallN(arg1,arg2,arg3,arg4) :

arg1 : 代表的是?骰刂
arg2 : 代表的是要呼叫的 syscall name
arg3 : 代表的是?魅??檔男?B
arg4 : 代表的是?魅??檔拿?Q


系??共定?了 6 ?? _syscallN , ? _syscall0 到 _syscall5 . 因?檫@是呼叫 int 0x80 的限制,各位大概??了一件事,???只是?f助各位去呼叫 int 0x80 ???系?中?嗪?劍?賄^ linux ?臀??包的很好


(4) ??程式



#gcc -O2 use_syscall.c use_syscall
#./use_syscall



Return Value: 100



[火星人 ] 如何在Linux Kernel?刃略鮃桓?ystem Call已經有474次圍觀

http://coctec.com/docs/program/show-post-72165.html