system call
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/1.jpg)
LOGO System Call
![Page 2: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/2.jpg)
Introduction
System call is the mechanism used by an application program to request service from the OS. Users use it to communicate with kernel.
![Page 3: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/3.jpg)
Method of System Call
Here are two methods developing our own system calls
• Using kernel module
• Modify the source code of linux directly
![Page 4: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/4.jpg)
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.)
![Page 5: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/5.jpg)
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
![Page 6: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/6.jpg)
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;
![Page 7: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/7.jpg)
Kernel module
Our system call
/* Our system call */
asmlinkage int mysyscall(int n) {
printk("enter mysyscall()\n");
return 2*n;
}
![Page 8: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/8.jpg)
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;
}
![Page 9: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/9.jpg)
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;
}
![Page 10: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/10.jpg)
Kernel moduleFor 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);
![Page 11: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/11.jpg)
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
![Page 12: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/12.jpg)
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;
}
![Page 13: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/13.jpg)
Method of System Call
Here are two methods developing our own system calls
• Using kernel module
• Modify the source code of linux directly
![Page 14: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/14.jpg)
Build in KernelIn /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);
}
![Page 15: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/15.jpg)
Build in KernelIn /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
![Page 16: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/16.jpg)
Build in KernelIn /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
![Page 17: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/17.jpg)
Build in KernelAlso, you should increment the system call count.
// This means the total number of system calls
#define __NR_syscalls 319
![Page 18: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/18.jpg)
Build in KernelIn /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
![Page 19: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/19.jpg)
Build in KernelAdd 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
![Page 20: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/20.jpg)
Build in KernelYou 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.
![Page 21: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/21.jpg)
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);
}
![Page 22: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/22.jpg)
NotificationIf 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.
![Page 23: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/23.jpg)
Reference
http://rswiki.csie.org/dokuwiki/documents:system_call:system_call_1
http://rswiki.csie.org/dokuwiki/documents:system_call:modifysourcecode
ksyms -a
![Page 24: System Call](https://reader036.vdocuments.us/reader036/viewer/2022062517/56813028550346895d95b3e5/html5/thumbnails/24.jpg)
Q&A
Thanks for your attention.