Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Add a New System Call to Linux

2 Hw1 Add a New System Call to Linux and Compile Kernel Add a New System Call to Linux by Kernel Module

3 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 ( 中山大學 ) http://ftp.nsysu.edu.tw/Linux/Kernel/linux/kernel

4 Compile your own Linux kernel Steps: 1. 1. Get the kernel source from ftp 2. 2. Installing the kernel source code - kernel source is put in /usr/src - #cd /usr/src - #tar xvzf linux-2.4.x.tar.gz 3. 3. make mroproper (Cleanup /usr/src/linux/ 目錄所有.o 的 object file, dependencies and kernel ’ s.config)

5 Compile your own Linux kernel Steps: 4. 4. Setup the kernel configuration make config or menuconfig or xconfig 5. 5. #make dep 6. 6. #make clean 7. 7. #make bzImage 8. 8. #make modules 9. 9. #make modules_install (Modules will be installed in /lib/modules/2.4.x) 10. 10. #make install

6 Compile your own Linux kernel Edit Bootloader Configuration File -- /etc/lilo.conf # lilo ( 完成 )

7 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

8 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

9 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

10 Add a New System Call 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 hello.o O_OBJS += …. Hello.o

11 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 #endif

12 Add a New System Call hello.c hello.c (system call implementation) #include Asmlinkage int sys_hello(){ printk(KERN_EMERG “ hello\n ” ); return 0; }

13 Add a New System Call User application App.c #include _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

14 Add a New System Call There 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)

15 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.

16 Compiling Kernel Modules A 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 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.

17 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

18 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; }

19 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");

20 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

21 Reference http://fossil.wpi.edu/docs/howto_add_systemc all.html http://fossil.wpi.edu/docs/howto_add_systemc all.html http://appsrv.cse.cuhk.edu.hk/~csc3150/tutnot e/note1/syscall.html http://appsrv.cse.cuhk.edu.hk/~csc3150/tutnot e/note1/syscall.html http://www.study-area.org/ The Linux Kernel Module Programming Guide 鳥哥的 Linux 私房菜 鳥哥的 Linux 私房菜


Download ppt "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."

Similar presentations


Ads by Google