150 likes | 364 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 • Implement system call in kernel/sys.c • 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
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 KERNELDIR=/usr/src/linux-2.4 Include $(KERNELDIR)/.config CFLAGS=-c –D__KERNEL__ -DMODULE –I$(KERNELDIR)/include –O2 –Wall Ifdef CONFIG_SMP CFLAGS += -D__SMP__ -DSMP Endif All: test2.o Clean: rm –f test2.o Install: /sbin/insmod test2.o Remove: /sbin/rmmod test2
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");
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 私房菜