summery of adding system calls to linux kernel

原创 2004年11月01日 14:32:00

To add a system call into linux kernel, follow the steps:
1. Preparation:  insert a new entry into the interrupt table.
     edit the file: linux/arch/i386/kernel/entry.S   in which lines look like:   
    .long SYMBOL_NAME(sys_ni_call)           /* 0 */
    .long SYMBOL_NAME(sys_exit)
    .long SYMBOL_NAME(sys_fork)
    .long SYMBOL_NAME(sys_vfork)             /* 190 */
after the line “ long SYMBOL_NAME(sys_vfork) “ you can add your own entry for your system call:
           . long   SYMBOL_NAME(sys_myService)
     Next, we define the the Num for our system call as a “stub”
    edit the file: linux/include/asm/unistd.h  in which lines look like:
            * This file contains the system call numbers.

          #define __NR_exit                 1
          #define __NR_fork                 2
          #define __NR_vfork              190
  Add your num define: 
          #define __NR_myService          191
2 Place for your  system call source file

After inserting your new system call entry in the interrupt table and preparing a stub for it, you will need to define (or implement) the system call. It will be easiest to have the system call definitions in your own source code files, say myservice.h and myservice.c.

In general, header files for machine architecture independent system calls and functions are kept under linux/include/linux/ and machine architecture dependent ones are kept in linux/include/asm/. Therefore, it would be a good idea to follow this convention. For example, the header file for the system calls for your new synchronization method, of which the implementation is machine architecture specific, would be placed in linux/include/asm/, while the header file for your machine architecture independent system call that access the superblock of one or more of your file systems would be placed under linux/include/linux.

The place for actual implementation file (myservice.c in this example) could vary. For example, if you are implementing a new process synchronization method, linux/ipc/ would be the best place for it. If you are implementing a file system related one, linux/fs/ would be the best place.

Remember that you will need to modify the Makefile in the directory you placed your .c file so that your code gets compiled and linked in properly. Modify the Makefile line to have a .o of your source code. For example, adding myservice.o:

  O_OBJS += ... myservice.o

The rest of the Makefiles can remain untouched (Makefile changes will be similar if you choose to add your code elsewhere).

3 . Implement the system call source file: 

Here is an example myservice.h and myservice.c, assuming myservice.h is under linux/include/linux/

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

_syscall2(int, myservice, int, arg1, char*, arg2);
/*the “2“ in “_syscall2“indicates the para number
of the syscall
  here the first para is the return type of syscall
2nd para is the name of the syscall
the next 2 para are paras passed to the syscall
*/ #endif
#include <linux/myservice.h>
asmlinkage int sys_myservice (int arg1, char* arg2) {
here asmlinkage indicates the linkage style of the syscall
which is defined in linux/linkage.h

*/ }


project 1 Adding a System Call to the Linux Kernel(初版)

project 1 Adding a System Call to the Linux Kernel   一、实验环境: ubuntu 10.04系统。 原内核版本:   ...
  • d344498093
  • d344498093
  • 2012年06月20日 16:39
  • 510

Kernel System Calls

Reprinted with permission of Linux Magazine Kernel System Calls by Alessandro Rubini This artic...
  • harhy
  • harhy
  • 2013年09月17日 11:54
  • 1315

Linux Kernel Interrupts, Exceptions and System Calls

Author: Harold Wang Basic knowledge In interrupt is usually define...
  • hero7935
  • hero7935
  • 2011年05月07日 11:35
  • 1290

Kernel command using Linux system calls system call is an interface betw...
  • ztguang
  • ztguang
  • 2016年03月30日 10:17
  • 506

Linux System Calls

4. System Calls 4.1 System call numbers System ca...
  • maimang1001
  • maimang1001
  • 2013年05月06日 22:06
  • 980

linux File-Handling System Calls

 1.5.6. File-Handling System Calls     When  a user accesses the contents of either a regular file o...
  • plaris060504
  • plaris060504
  • 2008年09月15日 12:31
  • 606

linux 内核专题— drv术语

译者按:   在1991年,一个叫林纳斯·本纳第克特·托瓦兹的芬兰学生制作了一个现在非常流行的操作系统的内核。他于1991年9月发布了Linux 0.01,并且于1992年以GPL许可证的方式授...
  • u012719256
  • u012719256
  • 2016年10月19日 18:14
  • 2675

[OS] Xv6 System Calls

Part One: System call tracing Your first task is to modify the xv6 kernel to print out a line for ...
  • yuchunyu97
  • yuchunyu97
  • 2017年10月20日 18:12
  • 400

MIT6.828 HW3: xv6 system calls

hw3 syscall
  • bysui
  • bysui
  • 2016年05月23日 18:17
  • 2096

linux system 函数的实现源码

system函数的实现源码,看到父进程是在子进程执行完成之后才继续往下走,否则一直挂在那等的子进程执行完。   int system(const char * cmds...
  • wangwenxc
  • wangwenxc
  • 2013年11月08日 09:27
  • 978
您举报文章:summery of adding system calls to linux kernel