1 / 24

System Call

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

Download Presentation

System Call

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Call

  2. Introduction • System call is the mechanism used by an application program to request service from the OS. Users use it to communicate with kernel.

  3. Method of System Call Here are two methods developing our own system calls • Using kernel module • Modify the source code of linux directly

  4. 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.)

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

  6. 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;

  7. Kernel module • Our system call /* Our system call */ asmlinkage int mysyscall(int n) { printk("enter mysyscall()\n"); return 2*n; }

  8. 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; }

  9. 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; }

  10. 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);

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

  12. 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; }

  13. Method of System Call Here are two methods developing our own system calls Using kernel module Modify the source code of linux directly

  14. 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); }

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

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

  17. Build in Kernel • Also, you should increment the system call count. // This means the total number of system calls #define __NR_syscalls 319

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

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

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

  21. 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); }

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

  23. Reference • http://rswiki.csie.org/dokuwiki/documents:system_call:system_call_1 • http://rswiki.csie.org/dokuwiki/documents:system_call:modifysourcecode • ksyms -a

  24. Q&A Thanks for your attention.

More Related