Download presentation
Presentation is loading. Please wait.
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 ftp://linux.cis.nctu.edu.tw/kernel/(交大資科) (中山大學)
4
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)
5
Compile your own Linux kernel
Steps: 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
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 .endr
9
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 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
10
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.
11
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.
12
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
13
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; }
14
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");
15
Reference http://fossil.wpi.edu/docs/howto_add_systemcall.html
The Linux Kernel Module Programming Guide 鳥哥的 Linux 私房菜
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.