290 likes | 487 Vues
System Call. Introduction. System call is the mechanism used by an application program to request service from the OS. Users use it to communicate with kernel. Method of System Call. Here are two methods developing our own system calls Using kernel module
E N D
Introduction • System call is the mechanism used by an application program to request service from the OS. Users use it to communicate with kernel.
Method of System Call Here are two methods developing our own system calls • Using kernel module • Modify the source code of linux directly
Kernel module • Making system calls using kernel module • Building system calls in kernel module is more flexible than modifying kernel. When we want to use our system call, just install our kernel modules; and if we don’t need it right away, just remove modules. Modifying kernel is not necessary. (But you still need to modify your kernel for O.S. project one.)
Kernel module • Build your own module. You can put it in <top directory to your kernel sources>/drivers/misc/ ex: /usr/src/linux-2.6.xx/drivers/misc/ #cd /usr/src/linux-2.6.xx/drivers/misc/ #vim myservice.c
Kernel module #include <linux/kernel.h> /* We're doing kernel work */ #include <linux/module.h> #define __NR_mysyscall 253 /* define the number of our system call */ extern void *sys_call_table[]; /* system call table */ void (*orig_sys_call)(void); extern int errno;
Kernel module • Our system call /* Our system call */ asmlinkage int mysyscall(int n) { printk("enter mysyscall()\n"); return 2*n; }
Kernel module • Initial kernel module /* Initialize the module - replace the system call */ int init_module() { printk("Insert mysyscall module\n"); orig_sys_call = sys_call_table[__NR_mysyscall]; sys_call_table[__NR_mysyscall] = mysyscall; return 0; }
Kernel module • clean kernel module /* Cleanup - unregister the appropriate file from /proc */ void cleanup_module() { printk("Remove mysyscall module\n"); sys_call_table[__NR_mysyscall] = orig_sys_call; }
Kernel module • For sys_call_table, your should extern it in a file such as <top directory to the kernel sources>/arch/i386/kernel/i386_ksyms. extern void* sys_call_table[]; /*variable should be exported. */ EXPORT_SYMBOL(sys_call_table);
Kernel module • Compile module #cd /usr/src/linux-2.6.x/drivers/misc/ #vi Makefile obj-m += myservice.o add this #make -C /usr/src/linux-2.6.x SUBDIRS=$PWD modules #insmod myservice.ko #rmmod myservice.ko
Kernel module – User Pro. #include <linux/unistd.h> #include <sys/syscall.h> int mysyscall(int n){ return syscall( __NR_mysyscall, n); } int main() { mysyscall(0); return 0; }
Method of System Call Here are two methods developing our own system calls Using kernel module Modify the source code of linux directly
Build in Kernel • In /usr/src/linux-2.6.x/kernel/, Create a new file myservice.c to define your system call. ... #include <linux/linkage.h> //for linking a system call #include <linux/kernel.h> //for the printk long (*my_service)(int arg1 , char* arg2) = NULL; EXPORT_SYMBOL(my_service); asmlinkage int sys_myservice (int arg1, char* arg2) { printk(KERN_EMERG “my service is running”); //kernel messages logged to /var/log/kernel/warnings return(1); }
Build in Kernel • In /usr/src/linux-2.6.x/include/asm-i386/unistd.h, define an index for your system call. Your index should be the number after the last system call defined in the list. // This mean the end of the system call table. #define __NR_myservice 318
Build in Kernel • In /usr/src/linux-2.6.x/include/asm-’platform’/unistd.h, ( platform means your system, ex: asm-i386 ) define an index for your system call. Your index should be the number after the last system call defined in the list. // This mean the end of the system call table. #define __NR_myservice 318
Build in Kernel • Also, you should increment the system call count. // This means the total number of system calls #define __NR_syscalls 319
Build in Kernel • In /usr/src/linux-2.6.xxx/arch/’platform’/kernel/syscall_table.S, you should define a pointer to hold a reference to your system call routine. It is important that your data entry placement corresponds to the index you assigned to your system call.Linux version is 2.6.18 and each version may have different place. .long sys_myservice
Build in Kernel • Add your system call to the Makefile in /usr/src/Linux-x.x.x/kernel/Makefile. Add your object after the other kernel objects have been declared. obj-y += myservice.o
Build in Kernel • You also need to copy your edited unistd.h from /usr/src/linux-2.6.x/include/asm/ to /usr/include/kernel/ because it contains your system call’s index.
Build in Kernel – User Pro. #include <linux/errno.h> #include<sys/syscall.h> #include <linux/unistd.h> #define __NR_myservice 318 //this is the return code from the system call extern errno; //this is a macro defined in unistd.h to help prototype sys calls _syscall2(int, myservice, int, arg1, char*, arg2); int main() { int i; i = myservice(1, "hi"); printf("%d\n",i); }
Notification • If your module want to use the variable in kernel, you have to use the function EXPORT_SYMBOL() to export it. ex: int a; EXPORT_SYMBOL(a); • printk can print messages in kernel, use dmesg to check.
Reference • http://rswiki.csie.org/dokuwiki/documents:system_call:system_call_1 • http://rswiki.csie.org/dokuwiki/documents:system_call:modifysourcecode • ksyms -a
Q&A Thanks for your attention.