210 likes | 442 Views
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/( 交大資科 )
E N D
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/(交大資科) • http://ftp.nsysu.edu.tw/Linux/Kernel/linux/kernel(中山大學)
Compile your own Linux kernel • Steps: • Get the kernel source from ftp • Installing the kernel source code- kernel source is put in /usr/src- #cd /usr/src - #tar xvzf linux-2.4.x.tar.gz • make mroproper(Cleanup /usr/src/linux/目錄所有.o的object file , dependencies and kernel’s .config)
Compile your own Linux kernel • Steps: • Setup the kernel configurationmake 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 kernel • Edit BootloaderConfiguration File-- /etc/lilo.conf # lilo (完成)
Add a New System Call • Edit : /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 Call • Edit the file : • /usr/src/linux/arch/i386/kernel/entry.S .data ENTRY(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 Call • Definition your source code files • (hello.c and hello.h) • About Header file • Machine architecture independent system calls and functions are kept under linux/include/linux • Machine architecture dependent ones are kept under linux/include/asm
Add a New System Call • Modify the Makefile in the directory you placed your .c file so that your code gets compiled andlinked in properly • Modify the Makefile line to have a .o of yoursource code • For example . Adding hello.o • O_OBJS += …. Hello.o
Add a New System Call • Example: • hello.c and hello.h • hello.h(assuming hello.h is under inux/include/linux) • #ifndef __LINUX_HELLO_H • #define __LINUX_HELLO_H • #include <linux/linkage.h> • #endif
Add a New System Call • hello.c • hello.c (system call implementation) #include <linux/hello.h> #include <linux/kernel.h> Asmlinkage int sys_hello(){ printk(KERN_EMERG “hello\n”); return 0; }
Add a New System Call • User application • App.c #include <linux/unistd.h> _syscall0(int, hello); int main(){ hello(); return 0; } • p.s if compiler error • mv /usr/include/linux /usr/include/linux.bak • mv /usr/iinclude/asm /usr/include/asm.bak • ln –s /usr/src/linux/include/linux /usr/include/linux • ln –s /usr/src/linux/include/asm /usr/include/asm
Add a New System Call • There are some macros defined for this in <linux/unistd.h> • 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 Module • A Kernel Module must have at least two functions: • “start” (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 Modules • A kernel module should be compiled with the –c flag. • A kernel modules must be compiled with the optimization flag, -Obecause the kernel make extensive use of inline function • Define symbols using gcc’s –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 Module CC = gcc CFLAGS = -O -D__KERNEL__ -DMODULE -Wall all: hello.o hello.o: hello.c $(CC) $(CFLAGS) -c hello.c-o hello.o install: /sbin/insmod hello.o remove: /sbin/rmmod hello
Adding System Call by Module #include <linux/kernel.h> /* for kernel function */ #include <linux/module.h> /* for module */ #include <linux/unistd.h> /* 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_EMERG“hello 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_EMERG“hello 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 <linux/sys.h> /* 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
Reference • http://fossil.wpi.edu/docs/howto_add_systemcall.html • http://appsrv.cse.cuhk.edu.hk/~csc3150/tutnote/note1/syscall.html • http://www.study-area.org/ • The Linux Kernel Module Programming Guide • 鳥哥的 Linux 私房菜