標籤:cal 標頭檔 white read info 簡介 str ini linkage
上一篇詳解了linux系統調用的原理,接下來依據上一篇的原理簡介怎樣建立新的linux系統調用
向核心中加入新的系統調用,須要運行3個步驟:
1. 加入新的核心功能
2. 更新標頭檔unistd.h
3. 針對這個新函數更新系統調用表calls.S
1. 在kernel/sys.c中加入函數:
asmlinkage int sysMul(int a, int b)
{
int c;
c = a*b;
return c;
}
2.在arch/arm/include/asm/unistd.h中加入系統調用編號:加入例如以下
#define __NR_preadv(__NR_SYSCALL_BASE+361)
#define __NR_pwritev (__NR_SYSCALL_BASE+362)
#define __NR_rt_tgsigqueueinfo (__NR_SYSCALL_BASE+363)
#define __NR_perf_event_open (__NR_SYSCALL_BASE+364)
#define __NR_sysMul(__NR_SYSCALL_BASE+365)
備忘:在最後面加入
3.在arch/arm/kernel/calls.S中加入代碼,指向新實現的系統調用函數:
/* 360 */ CALL(sys_inotify_init1)
CALL(sys_preadv)
CALL(sys_pwritev)
CALL(sys_rt_tgsigqueueinfo)
CALL(sys_perf_event_open)
CALL(sysMul)
備忘:必須在最後面加入和unistd.h中的系統調用號一樣
4.又一次編譯核心
make uImage ARCH=arm CROSS_COMPILE=arm-linux-
5.把核心複製到tftp檔案夾以下
cp arch/arm/boot/uImage /tftpboot/
備忘:第5步能夠不用那個是為了通過tftp下載到開發板
6.使用系統調用
#include <stdio.h>
#include <linux/unistd.h>
main()
{
int result;
result = syscall(361,1, 2);//syscall過程 1、把系統調用號mov r7, #365 2、使用svc指令
//syscall(系統調用號。參數1,參數2) 當中參數1和參數2是sysMul的兩個參數
printf("result = ", result);
}
加入新的linux系統調用