system call

24
LOGO System Call

Upload: luboslaw-kozlowski

Post on 31-Dec-2015

35 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: System Call

LOGO System Call

Page 2: 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.

Page 3: System Call

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

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

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

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

Kernel module

Our system call

/* Our system call */

asmlinkage int mysyscall(int n) {

printk("enter mysyscall()\n");

return 2*n;

}

Page 8: System Call

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 24: System Call

Q&A

Thanks for your attention.