How to Add a System Call

Kernel : 2.6.18
編譯環境 : Fedora Core 6


假設要加的system call為 sys_project, 有一個int的輸入參數
一、在linux source code的部份(以 linux 代表source code的根目錄)

  1. linux/arch/i386/kernel/syscall_table.S的最後面加上要新增的system call名稱
    如果syscall_table.S的最後面長得像這樣

            .
            .
    .long sys_tee                   /* 315 */
    .long sys_vmsplice
    .long sys_move_pages

    那麼要加入sys_project就要把它改成這樣, 這裡的".long"是必須的, 它並不是代表回傳的型態, 而是Linux Assembly的一個語法

            .
            .
    .long sys_tee                   /* 315 */
    .long sys_vmsplice
    .long sys_move_pages
    .long sys_project             /* 318 */

    而所加的定義在檔案中的順序, 其實也就是這個system call的system call number, 此例中是318

  2. linux/include/asm/unistd.h裡面加上自己的define
    unistd.h裡面會有一段跟syscall_table.S很像的define, 不過在這裡system call是以"__NR_"開頭, 而其後跟著的數值則是system call number, 如果在它定義的最後一個system call附近像這樣
            .
            .
    #define __NR_vmsplice               316
    #define __NR_move_pages         317

    #ifdef __KERNEL__

    #define NR_syscalls 318
            .
            .

    因為 NR_syscalls 的值必須等於最大的system call加 1, 所以在根據syscall_table.S中的順序修改unistd.h之後, 不要忘了修改NR_syscalls的值

            .
            .
    #define __NR_vmsplice               316
    #define __NR_move_pages         317
    #define __NR_project                  318

    #ifdef __KERNEL__

    #define NR_syscalls 319
            .
            .

  3. linux/include/linux/syscalls.h裡面加上函式的定義
    函式定義前面必須加上asmlinkage以確保編譯時連結的正確性, 所以加完後的狀況大概像這樣
            .
            .
    asmlinkage long sys_set_robust_list(struct robust_list_head __user *head,
            size_t len);
    asmlinkage long sys_project( int i );

    #endif

  4. 將system call的實作檔放入source code tree中
    原則上, 實作檔應該根據 syscall 的類型放在相對應的資料夾中, 然後再將實作檔編譯後的.o檔檔名加入該資料夾下的Makefile的obj-y之中, 假設實作檔的source code為project.c, 放在linux/kernel/, 那linux/kernel/Makefile改完後大概長這樣
            .
            .
    obj-y = project.o sched.o fork.o exec_domain.o panic.o printk.o profile.o \
            .
            .

    而在撰寫實作檔的時候, 請記得將"#include <linux/linkage.h>"加進去, 否則編譯將會發生問題, 以下是一個實作檔的範例

    project.c

    #ifndef __LINUX_PROJECT
    #define __LINUX_PROJECT

    #include <linux/linkage.h>
    #include <linux/kernel.h>

    asmlinkage long sys_project( int i ){
            printk( "Success!! -- %d\n", i );
            return 0;
    }

    #endif

  5. 在/usr/include/asm/unistd.h中加入定義讓User能呼叫
    其實在上一步驟整個system call已經算是加好了, 但此時User只能以system call number呼叫(此例中是318), 為了讓User能以system call的名字呼叫, 所以要修改/usr/include/asm/unistd.h,改完結果像這樣
            .
            .
    #define __NR_vmsplice              316
    #define __NR_move_pages        317
    #define __NR_project                 318

    #endif /* _ASM_I386_UNISTD_H_ */

  6. 重新編譯Kernel---->大功告成!!!

二、如何呼叫System call
要呼叫system call, 必須加入"#include<syscall.h>", 並以syscall( __NR_[system call的名字], [參數1, 參數2,...] )來呼叫, 如果沒有做步驟 5, 則必須以syscall( [system call number], [參數1, 參數2,...] )來呼叫, 以下是一個範例

test.c

#include<syscall.h>

int main(){
        syscall( __NR_project, 2 );
        /* 如果沒有做步驟 5, 就用syscall( 318, 2 ); 代替 */
        return 0;
}

執行結果

[root@localhost test]# gcc test.c
[root@localhost test]# ./a.out
[root@localhost test]# dmesg
        .
        .
        .
Success!! -- 2
[root@localhost test]#
 
 
 
 

---------
linux/include/linux/syscalls.h
task_struct

asmlinkage long sys_project(char *test_array,char *test_array_tail,int operation);

----------
How to Add a System Call
假設要加的system call為 sys_project, 有一個int的輸入參數
注意有些地方該是sys_開頭(syscalls.h)

In source code 的根目錄:
arch/i386/kernel/syscall_table.S
include/asm-i386/unistd.h
include/linux/syscalls.h
kernel/Makefile
系統根目錄
/usr/include/asm/unistd.h

來源:

http://adl.csie.ncu.edu.tw/~ernieshu/syscall_2_6_35.htm
http://adl.csie.ncu.edu.tw/~ernieshu/syscall.htm

[Fw]How to Add a System Call(Fedora Core 6 Kernel : 2.6.18)的更多相关文章

  1. System.ArgumentException: 已添加了具有相同键的项。(An item with the same key has already been added) 在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 在 System.Web.Mvc.Js

    最近将一个项目从ASP.NET MVC 3升级至刚刚发布的ASP.NET MVC 5.1,升级后发现一个ajax请求出现了500错误,日志中记录的详细异常信息如下: System.ArgumentEx ...

  2. Entity Framework优化一:引发了“System.Data.Entity.Core.EntityCommandExecutionException”类型的异常

    错误信息: “System.Data.Entity.Core.EntityCommandExecutionException”类型的异常在 EntityFramework.SqlServer.dll ...

  3. 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误

    这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...

  4. System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    2017/8/15 20:55:21 [AgentPayQuery_205506102_1BBBB]系统异常:System.Data.Entity.Core.EntityException: The ...

  5. EF Core中关于System.Linq.Dynamic.Core的使用(转载)

    项目中经常用到组合条件查询,根据用户配置的查询条件进行搜索,拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦.有个System.Linq.Dynamic.Core用起来比 ...

  6. Fedora Core 11 Alpha试用视频(由于youtube问题暂时无法访问)

    1.系统安装: http://www.youtube.com/watch?v=QcDy5eRQZ20   2.重启后配置   3.体验Fedora 11 Gnome2.25和Kde 4.2 http: ...

  7. System.Data.Entity.Core.EntityException: 可能由于暂时性失败引发了异常。如果您在连接到 SQL Azure 数据库,请考虑使用 SqlAzureExecutionStrategy。

    代码异常描述  ************** 异常文本 **************System.Data.Entity.Core.EntityException: 可能由于暂时性失败引发了异常.如果 ...

  8. Error message: Failed to spawn: unable to access process with pid 413 due to system restrictions; try `sudo sysctl kernel.yama.ptrace_scope=0`, or run Frida as root

    Android 8.0 在frida中使用 -f 参数报错, Error message: Failed to spawn: unable to access process with pid 413 ...

  9. Add a system call on Ubuntu 13.04(x64) with x86_64

    We added a system call to modify idt table, then programed it in modify_idt.c 1. Put our modify_idt. ...

随机推荐

  1. thinkphp中的exp查询

    今天遇到一个问题,就是在vendor表中查询出vendor_id = vendor_f_id的数据,其实使用原生的sql语句是非常简单的: select * from vendor where ven ...

  2. luoguP1080 国王游戏 题解(NOIP2012)(贪心+高精)

    luoguP1080 国王游戏 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include& ...

  3. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  4. HDU 4886 TIANKENG’s restaurant(Ⅱ) ( 暴力+hash )

    TIANKENG’s restaurant(Ⅱ) Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Ja ...

  5. Swift——(六)Swift中的值类型

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/twlkyao/article/details/34855597     在Swift中,结构体和枚举 ...

  6. 后台执行循环(done &)

  7. shell使用reposync同步仓库

  8. 创建Uboot 环境变量 bin 文件

    As we know, the bootloader stores its configuration into an area of the flash called the environment ...

  9. Dev常用控件

    GridControl TreeView DEV GridControl小结.. https://blog.csdn.net/happy09li/article/details/7186829 Dev ...

  10. mysql安装与修改密码

    数据库基本概念:数据的仓库 数据库服务器-->数据库-->表-->记录-->属性(列,字段) unix下数据库服务安装: apt-get install -y mysql-se ...