23
LOGO System Call

LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

Embed Size (px)

Citation preview

Page 1: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

LOGO System Call

Page 2: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

Kernel module

Our system call

/* Our system call */

asmlinkage int mysyscall(int n) {

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

return 2*n;

}

Page 8: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

Build in KernelAlso, you should increment the system call count.

// This means the total number of system calls

#define __NR_syscalls 319

Page 18: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

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: LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate

Q&A

Thanks for your attention.