0% found this document useful (0 votes)
25 views12 pages

System Call in Linux Os

Operating Systems Slides

Uploaded by

f20221209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views12 pages

System Call in Linux Os

Operating Systems Slides

Uploaded by

f20221209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SYSTEM CALL IN LINUX OS

WHAT IS A SYSTEM CALL?


• Linux has two modes of operation, kernel mode and
user mode.

• System calls can be taken as interface given to the user


mode process to access kernel or device specific data.

• System calls are explicit request to the kernel made via


software interrupts.

• Example : The data is stored in the hard disk device and


the user process reads the data in the device or file
through the read system call.
• the "read()" system call used to read data specific to a
file through the file descriptor.
HOW A SYSTEM CALL WORKS?
• When a process in user mode invokes a system call, the CPU
switches from user mode to kernel mode and starts executing
functions.

• In Linux, system calls are invoked using the "int 0x80" assembly
language instruction. "int 0x80" acts as an gateway to all the system
calls implemented by the Linux kernel.

• Kernel implements many system call. Parameters are passed to


identify the system call number and arguments to the system call.
This is done by storing values in the process register before calling
the "int 0x80" instruction.

• Each system call has a unique number identifying the system call.
You can find the system call numbers in the file
/usr/include/asm/unistd.h For example the fork system call has a
system call number of 2. You can find the system call numbers also
in the asm/unistd.h file in the Linux kernel source directory.
Example
• read(fd,buffer,size);
• This corresponds to a system call with three arguments. So this will be expanded by the
_syscall3 macro.
• A statement “static inline syscall3(int,read,int,fd,char *,buf,off_t,len)” has been added in the
header file for the macro expansion to take place.
• After the expansion the system call number will be in register 'zero' and the argument to the
system call will be in the general purpose registers of the processor.
• Also the macro will call the "int 0x80" instruction after loading the registers. So the kernel
mode is initiated and kernel will execute on behalf of the process initiated the system call.
• The "int 0x80" instruction will call the system call handler.
• Each system call will have a routine or program defined in the kernel. Address of each of
these routine are stored in the in array named "sys_call_table". You can find the code
corresponding to the system call handler and sys_call_table in the file “/usr/src/linux-
/arch/i386/kernel/entry.S”.
• The path name has to be filled accordingly for different kernel version. The system call
handler will call the service routine corresponding to the system call, by looking at the system
call number loaded in the register "zero". So the service routine corresponding to the read
system call will be executed. After executing the service routine the control comes back to
the system call handler and it will then give control back to the user process, also the mode
of operation is changed to user mode.
Number of system calls
• The kernel will have a limit on the number of
system calls. The value is stored in the
"NR_syscalls" variable.

• You can find what are the system call numbers


in use by looking at the entry.S file or unistd.h
file.
ADDING OUR OWN SYSTEM CALL
• DECLARING A SYSTEM CALL: You can add the
system call in two ways.
– One is to use an unassigned number as our system
call number.
– Other method is to increment the system call
number limit by one and use that new limit as our
number.

• DEFINING A SYSTEM CALL: a header file and a


source file.
myroutine.h
#ifndef __LINUX_MYROUTINE_H
#define __LINUX_MYROUTINE_H
#include <linux/linkage.h>
#endif
• place the header file in linux directory in the
kernel include directory
(/usr/src/{linux}/include/linux). You can place it
any where, what you have to do is to include that
header file in the source file. The linkage.h file
will resolve the “asmlinkage” keyword to be used
in the system call source file.
myroutine.c
#include <linux/myroutine.h>
asmlinkage int sys_myroutine(int arg1,int arg2){
return arg1+arg2;
}
• You can place the system call file in any
directory, only thing you have to do is add a
line in makefile corresponding to that
directory, such that the myroutine file gets
compiled.

• This is done by adding the line


obj-y += myroutine.o in the make file.
Recompile the kernel
Once you have completed changes in the kernel,
you have to compile the Linux kernel and make
the new compiled kernel as the kernel boot
image (by adding it to grub).
User Space
• to create a header file for your system call in
the user include directory(/usr/include/linux).
• myroutine.h
#include <linux/unistd.h>
_syscall2(int, myroutine, int, arg1, int , arg2);
• store this file in linux directory under the user
include directory.
• use the "_syscall2" macro as the number of
arguments are two. This macro will actually
define our user space myroutine function.
• add “__NR_myroutine number” line in both
kernel and user space.

• Now your coding for registering the system


call is over.

• You also have to make changes in


the entry.S file and the unistd.h files in the
kernel and user include directories for the
system call to work.
myroutine.c

#include <linux/myroutine.h>
main(){
int sum;
sum=myroutine(10,10);
printf(“%d”,sum);
}
You should get a 20 printed while you execute above program.
• Before running the program you have to compile the kernel
and boot the system with the compiled kernel.

You might also like