Click here to load reader

Add a New System Call to Linux

  • Upload
    decker

  • View
    54

  • Download
    2

Embed Size (px)

DESCRIPTION

Add a New System Call to Linux. Hw1. Add a New System Call to Linux and Compile Kernel Add a New System Call to Linux by Kernel Module. Compile your own Linux kernel. Get the source The Linux Kernel Archives http://www.kernel.org ftp://linux.cis.nctu.edu.tw/kernel/( 交大資科 ) - PowerPoint PPT Presentation

Citation preview

  • Add aNew System Call to Linux

  • Hw1Add a New System Call to Linux and Compile Kernel

    Add a New System Call to Linux by Kernel Module

  • Compile your own Linux kernelGet the sourceThe Linux Kernel Archiveshttp://www.kernel.org ftp://linux.cis.nctu.edu.tw/kernel/() http://ftp.nsysu.edu.tw/Linux/Kernel/linux/kernel ()

  • Compile your own Linux kernelSteps:Get the kernel source from ftpInstalling the kernel source code - kernel source is put in /usr/src - #cd /usr/src - #tar xvzf linux-2.4.x.tar.gzmake mroproper (Cleanup /usr/src/linux/.oobject file , dependencies and kernels .config)

  • Compile your own Linux kernelSteps:Setup the kernel configuration make config or menuconfig or xconfig#make dep#make clean#make bzImage#make modules#make modules_install (Modules will be installed in /lib/modules/2.4.x)#make install

  • Compile your own Linux kernelEdit Bootloader Configuration File -- /etc/lilo.conf # lilo ()

  • Add a New System CallEdit : /usr/src/linux/include/asm/unistd.h #define __NR_exit 1 #define __NR_fork 2 #define __NR_lremovexattr 236 #define __NR_fremovexattr 237 #define __NR_hello 239 add #defines for you new system calls at the end

  • Add a New System CallEdit the file :/usr/src/linux/arch/i386/kernel/entry.S.dataENTRY(sys_call_table).long SYMBOL_NAME(sys_ni_syscall) /* 0.long SYMBOL_NAME(sys_exit).long SYMBOL_NAME(sys_fork).long SYMBOL_NAME(sys_ni_syscall).long SYMBOL_NAME(sys_hello).rept NR_syscalls-(.-sys_call_table)/4.long SYMBOL_NAME(sys_ni_syscall).endr

  • Add a New System CallDefinition your source code files(hello.c and hello.h)About Header fileMachine architecture independent system calls and functions are kept under linux/include/linuxMachine architecture dependent ones are kept under linux/include/asm

  • Add a New System CallModify 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 codeFor example . Adding hello.oO_OBJS += . Hello.o

  • Add a New System CallExamplehello.c and hello.hhello.h(assuming hello.h is under inux/include/linux)#ifndef __LINUX_HELLO_H#define __LINUX_HELLO_H#include #endif

  • Add a New System Callhello.chello.c (system call implementation)#include #include Asmlinkage int sys_hello(){printk(KERN_EMERG hello\n);return 0;}

  • Add a New System CallUser applicationApp.c#include _syscall0(int, hello);int main(){hello();return 0;}p.s if compiler errormv /usr/include/linux /usr/include/linux.bakmv /usr/iinclude/asm /usr/include/asm.bakln s /usr/src/linux/include/linux /usr/include/linuxln s /usr/src/linux/include/asm /usr/include/asm

  • Add a New System CallThere are some macros defined for this in The format is _syscallN(return type, function name,arg1 type,arg1 name) where N is the number of parameters.For example _syscall1(int, hello, int, a)

  • The Simple Kernel ModuleA Kernel Module must have at least two functionsstart (initialization) function called init_module() which is called when the module is insmoded into kernel.end (cleanup) function called clean_module() which is called just before it is rmmoded.

  • Compiling Kernel ModulesA kernel module should be compiled with the c flag.A kernel modules must be compiled with the optimization flag, -O because the kernel make extensive use of inline functionDefine symbols using gccs D option__KERNEL__tells the header files that the code will be run in kernel mode.MODULE: tells the header files to give the appropriate definitions for a kernel module.

  • Adding System Call by ModuleCC = gccCFLAGS = -O -D__KERNEL__ -DMODULE -Wallall: hello.ohello.o: hello.c$(CC) $(CFLAGS) -c hello.c-o hello.oinstall:/sbin/insmod hello.oremove:/sbin/rmmod hello

  • Adding System Call by Module#include /* for kernel function */#include /* for module */#include /* for system calls */#include hello.h

    extern void *sys_call_table[];void (*orig_sys_call)(void);/* my system call */int hello(unsignedlong arg){printk( KERN_EMERG "Hello System Call: %d\n", arg);return 0;}

  • Adding System Call by Module

    /* init function, called when loaded */int init_module(void){printk(KERN_EMERGhello module installed\n");orig_sys_call= sys_call_table[SYS_hello]; /* backup the original system call*/sys_call_table[SYS_hello] = hello ; /* replace with my system call */return 0;}void cleanup_module(void){printk(KERN_EMERGhello module uninstalled\n");sys_call_table[SYS_hello] = orig_sys_call; /* restore the original system call */}/* try to remove thieline */MODULE_LICENSE("GPL");

  • Adding System Call by Module#ifndef __HELLO_H__#define __HELLO_H__#include /*NR_syscalls is the system call table size, it is 256.Its definition is in linux/include/linux/sys.h.my system call uses the last one, its number is 255*/#define __NR_hello (NR_syscalls -1)#define SYS_hello (NR_syscalls -1)#endif

  • Referencehttp://fossil.wpi.edu/docs/howto_add_systemcall.htmlhttp://appsrv.cse.cuhk.edu.hk/~csc3150/tutnote/note1/syscall.htmlhttp://www.study-area.org/The Linux Kernel Module Programming Guide Linux