KEMBAR78
How To Add System Call In Ubuntu OS | PPTX
How To Add System Call In 
UBUNTU Operating System 
Presented 
by: 
Pratik 
Tambekar 
Guided 
by: 
Mr. T.R 
Ravi
CONTENTS 
WHY UBUNTU? 
INTRODUCTION 
WHAT IS KERNEL? 
WHAT IS SYSTEM CALL? 
HOW TO ADD SYSTEM CALL 
HOW TO REPLACES NEW KERNEL 
FILE WITH OLD KERNEL FILE 
REFRENCES
WHY UBUNTU? 
Free Ubuntu is and will always be free in the 
future. Ubuntu is developed by people all over the 
world embracing the principle of Free libre open-source 
software (FLOSS). This enables new 
software and updates to be available free of cost 
since they are written by volunteers and also the 
employees of Canonical, the parent company of 
Ubuntu. 
No Viruses While using Ubuntu, you do not need 
to worry about installing any anti-virus programs 
since Ubuntu is completely free of viruses.
CONT…. 
Community Support When you need help using 
your system, the community is available 
everywhere around you to support you at all times. 
All this are done voluntarily by people passionate 
about Ubuntu. 
Up to date software Ubuntu will always be up to 
date with updates released regularly to ensure that 
your system is secure and bug free. These regular 
updates will be always be available for free. 
Beautiful, Polished, Stable These are the goals of 
every Ubuntu release. Your Ubuntu is designed 
with the help of the community and experts after 
extensive discussion. Ubuntu is regularly user 
tested to ensure that it is easy and simple to use 
while preserving its elegance and polish.
INTRODUCTION 
 Ubuntu was founded by Mark Shuttleworth, a South 
African entrepreneur coming up with their first release 
Ubuntu 4.10 codenamed Warty Warthog in October 
2004. 
 Ubuntu is an African concept meaning “humanity 
toward others” 
 Sponsored by Canonical Ltd. Owned by South African 
billionaire Mark Shuttleworth. 
 Ubuntu is a Linux-based Operating System that is open 
sourced (free) 
 Pronounced (oo-BOON-too) 
 Strong focus on usability and ease of installation
Ubuntu Releases
Table of versions
Advantages and Disadvantages 
over windows. 
- Advantages: 
1) Not vulnerable to Windows malware/viruses 
2) Free 
3) Tends to run more efficiently than Windows 
4) Not subject to Microsoft's cripple ware. 
- Disadvantages: 
1) Won't run most Windows applications (especially 
games) 
2) Requires a higher than average nerd factor (not as 
user friendly) 
3) Doesn't work with all hardware (but neither does 
Windows!) 
4) Can be somewhat buggy
What is Kernel? 
The central module of an operating system. It is 
the part of the operating system that loads first, 
and it remains in main memory. Because it stays 
in memory, it is important for the kernel to be as 
small as possible while still providing all the 
essential services required by other parts of the 
operating system and applications. 
It acts as an interface between the user 
applications and the hardware. 
Typically, the kernel is responsible for memory 
management, process management, task 
management, I/O communication and disk 
management.
CONT.... 
Types of Kernels: 
Kernels may be classified mainly in two 
categories 
1) Monolithic 
2) Micro Kernel 
Linux follows the monolithic modular 
approach
CONT.... 
Monolithic kernel: 
In this type of kernel 
architecture, all the basic system services 
like process and memory management, 
interrupt handling etc were packaged into a 
single module in kernel space. This type of 
architecture led to some serious drawbacks 
like 
1) Size of kernel, which was huge. 
2) Poor maintainability, which means bug 
fixing or addition of new features resulted in 
recompilation of the whole kernel which could 
consume hours
CONT....
What is System Call? 
System calls are low level functions the 
operating system makes available to 
applications via a defined API (Application 
Programming Interface) 
System calls represent the interface the kernel 
presents to user applications. 
In Linux all low-level I/O is done by reading and 
writing file handles, regardless of what 
particular peripheral device is being 
accessed—a tape, a socket, even your 
terminal, they are all files. 
Low level I/O is performed by making system 
calls.
Anatomy of a System Call 
A System Call is an explicit request to the kernel 
made via a software interrupt. 
The interrupt call ‘0x80’ call to a system call 
handler (sometimes called the “call gate”). 
The system call handler in turns calls the system 
call interrupt service routine (ISR). 
To perform Linux system calls we have to do 
following: 
- Put the system call number in EAX register. 
- Set up the arguments to the system call in 
EBX,ECX, etc. 
- call the relevant interrupt (for DOS, 21h; for 
Linux, 80h). 
- The result is usually returned in EAX.
CONT…. 
There are six registers that are used for the 
arguments that the system call takes. The first 
argument goes in EBX, the second in ECX, then 
EDX, ESI, EDI, and finally EBP. If more then 6 
arguments needed (not likely), the EBX register 
must contain the memory location where the list 
of arguments is stored. 
basic system calls: 
◦ sys_open 
◦ sys_close 
◦ sys_read 
◦ sys_write 
◦ sys_Fork 
◦ sys_exit
CONT…. 
Sys_open - open a file 
system call number (in EAX): 5 
arguments: 
◦ EBX: The pathname of the file to open/create 
◦ ECX: set file access bits (can be OR’d togather): 
O_RDONLY open for reading only 
O_WRONLY open for writing only 
O_RDRW open for both reading and writing 
O_APPEND open for appending to the end of 
file 
O_TRUNC truncate to 0 length if file exists 
O_CREAT create the file if it doesn’t exist 
◦ EDX: set file permissions. 
Returns in EAX: file descriptor.
Error handling 
System calls set a global integer called 
errno on error. 
The constants that errno may be set to are 
(partial list): 
◦ EPERM operation not permitted. 
◦ ENOENT no such file or directory (not 
there). 
◦ EIO I/O error – EEXIST file already exists. 
◦ ENODEV no such device exists. 
◦ EINVAL invalid argument passed.
CONT…
Example
How to add a System Call 
Ubuntu 10.10 and using kernel version 
2.6.37.3 If you are using any other kernel 
version just replace 2.6.37.3 with your 
version. 
I am also assuming you have extracted the 
source code. 
Now let the new system call’s name be 
“add2”.
CONT…. 
1. Now you will find a “arch” folder in 
the source code folder. Open the file 
arch/x86/kernel/syscall_table_32.S in 
a text editor. Go to the end of the 
document and add this line 
.long sys_add2 /* my code */
CONT…. 
2. Now open 
arch/x86/include/asm/unistd_32.h 
and find out 
#define __NR_prlimit64 340 
 Add a new line after this: 
#define __NR_add2 341 
 Don’t just close yet. After 3-4 lines, 
you will find a line like
CONT…. 
#define NR_syscalls 341 
 Change it to 
#define NR_syscalls 342. 
3. Now edit 
arch/x86/include/asm/unistd_64.h 
 Find out: 
#define __NR_prlimit64 302 
__SYSCALL(__NR_prlimit64,sys_prli 
mit64)
CONT…. 
 Now after these two lines, add these 
two lines 
#define __NR_add2 303 
__SYSCALL(__NR_add2, sys_add2) 
4. Now again in the source folder you 
will find a folder named include. 
Open the file include/linux/syscalls.h 
and go to the end of the file. Before 
the line
CONT… 
#endif 
write this prototype definition line: 
asmlinkage long sys_add2(int i,int j); 
5. Now find out the kernel folder in the 
source directory. Create a new empty 
file in the kernel folder with the name 
“mysysteamcalls.c” . Add the following 
codes in the file:
CONT… 
#include<linux/linkage.h> 
asmlinkage long sys_add2(int i,int j) 
{ 
return i+j; 
} 
6. Now open the Makefile in this 
folder(/kernel/Makefile) and find out 
obj-y += groups.o
CONT… 
 Add a new line before this line : 
obj-y += mysysteamcalls.o 
 Ok, this is the edit you need to do to add 
a new system call. Now compile or 
recompile the source code and enjoy 
your new system call.
sample code to call the system 
call : 
#include <stdio.h> 
#include <linux/unistd.h> 
#include <sys/syscall.h> 
//comment the following line if you are using 
64 bit, this number is the same used 
previously 
#define sys_add2 341 
//comment the following line if you are using 
32 bit, this number is the same used 
previously
CONT… 
int main(void) 
{ 
int a,b,c; 
printf("Adding Two Numbers in Kernel Spacen"); 
printf("Input a: "); 
scanf("%d",&a); 
printf("Input b: "); 
scanf("%d", &b); 
c = syscall(sys_add2, a, b); 
printf("System call returned %dn", c); 
return 0; 
}
How to replaces new kernel file 
with old Kernel File: 
download the kernel from kernel.org 
http://www.kernel.org/pub/linux/kernel/ 
v3.0/linux-3.X.tar.bz2 
Extract the tarball to /usr/src/linux-3.X/ 
tar -xvf linux-3.X.tar.bz2 -C 
/usr/src/linux-3.X/ 
Change the directory to /usr/src/linux- 
3.x 
$ cd /usr/src/linux-3.x
CONT… 
Configure 
Lets configure our new Linux Kernel with the 
configuration of old kernel (which is already 
installed in our machine) 
$ sudo make oldconfig 
The new kernel is installed, however we need to 
say to the Ubuntu to use this new one. Just type in 
the shell: 
sudo update-initramfs -k 2.6.38.3 -u 
sudo update-grub 
Restart your computer. After loaded, type in the 
shell: 
uname -a
.deb Files 
linux-headers-2.6.34- 
02063415_2.6.34- 
02063415.201402101835_all.deb 
linux-headers-2.6.34-02063415- 
server_2.6.34- 
02063415.201402101835_amd64.deb 
linux-image-2.6.34-02063415- 
generic_2.6.34- 
02063415.201402101835_amd64.deb 
sudo dpkg -i linux*2.6.34-7.37*.deb 
sudo reboot
Refrences: 
http://www.kernel.org 
http://www.Ubuntu.com 
kernel.ubuntu.com/~kernel-ppa/ 
mainline/ 
https://wiki.ubuntu.com/Kernel/Mainlin 
eBuilds 
ubuntuforums.org 
www.ubuntu.com/support/community 
http://www.linuxquestions.org
Thank you

How To Add System Call In Ubuntu OS

  • 1.
    How To AddSystem Call In UBUNTU Operating System Presented by: Pratik Tambekar Guided by: Mr. T.R Ravi
  • 2.
    CONTENTS WHY UBUNTU? INTRODUCTION WHAT IS KERNEL? WHAT IS SYSTEM CALL? HOW TO ADD SYSTEM CALL HOW TO REPLACES NEW KERNEL FILE WITH OLD KERNEL FILE REFRENCES
  • 3.
    WHY UBUNTU? FreeUbuntu is and will always be free in the future. Ubuntu is developed by people all over the world embracing the principle of Free libre open-source software (FLOSS). This enables new software and updates to be available free of cost since they are written by volunteers and also the employees of Canonical, the parent company of Ubuntu. No Viruses While using Ubuntu, you do not need to worry about installing any anti-virus programs since Ubuntu is completely free of viruses.
  • 4.
    CONT…. Community SupportWhen you need help using your system, the community is available everywhere around you to support you at all times. All this are done voluntarily by people passionate about Ubuntu. Up to date software Ubuntu will always be up to date with updates released regularly to ensure that your system is secure and bug free. These regular updates will be always be available for free. Beautiful, Polished, Stable These are the goals of every Ubuntu release. Your Ubuntu is designed with the help of the community and experts after extensive discussion. Ubuntu is regularly user tested to ensure that it is easy and simple to use while preserving its elegance and polish.
  • 5.
    INTRODUCTION  Ubuntuwas founded by Mark Shuttleworth, a South African entrepreneur coming up with their first release Ubuntu 4.10 codenamed Warty Warthog in October 2004.  Ubuntu is an African concept meaning “humanity toward others”  Sponsored by Canonical Ltd. Owned by South African billionaire Mark Shuttleworth.  Ubuntu is a Linux-based Operating System that is open sourced (free)  Pronounced (oo-BOON-too)  Strong focus on usability and ease of installation
  • 6.
  • 7.
  • 8.
    Advantages and Disadvantages over windows. - Advantages: 1) Not vulnerable to Windows malware/viruses 2) Free 3) Tends to run more efficiently than Windows 4) Not subject to Microsoft's cripple ware. - Disadvantages: 1) Won't run most Windows applications (especially games) 2) Requires a higher than average nerd factor (not as user friendly) 3) Doesn't work with all hardware (but neither does Windows!) 4) Can be somewhat buggy
  • 9.
    What is Kernel? The central module of an operating system. It is the part of the operating system that loads first, and it remains in main memory. Because it stays in memory, it is important for the kernel to be as small as possible while still providing all the essential services required by other parts of the operating system and applications. It acts as an interface between the user applications and the hardware. Typically, the kernel is responsible for memory management, process management, task management, I/O communication and disk management.
  • 10.
    CONT.... Types ofKernels: Kernels may be classified mainly in two categories 1) Monolithic 2) Micro Kernel Linux follows the monolithic modular approach
  • 11.
    CONT.... Monolithic kernel: In this type of kernel architecture, all the basic system services like process and memory management, interrupt handling etc were packaged into a single module in kernel space. This type of architecture led to some serious drawbacks like 1) Size of kernel, which was huge. 2) Poor maintainability, which means bug fixing or addition of new features resulted in recompilation of the whole kernel which could consume hours
  • 12.
  • 13.
    What is SystemCall? System calls are low level functions the operating system makes available to applications via a defined API (Application Programming Interface) System calls represent the interface the kernel presents to user applications. In Linux all low-level I/O is done by reading and writing file handles, regardless of what particular peripheral device is being accessed—a tape, a socket, even your terminal, they are all files. Low level I/O is performed by making system calls.
  • 14.
    Anatomy of aSystem Call A System Call is an explicit request to the kernel made via a software interrupt. The interrupt call ‘0x80’ call to a system call handler (sometimes called the “call gate”). The system call handler in turns calls the system call interrupt service routine (ISR). To perform Linux system calls we have to do following: - Put the system call number in EAX register. - Set up the arguments to the system call in EBX,ECX, etc. - call the relevant interrupt (for DOS, 21h; for Linux, 80h). - The result is usually returned in EAX.
  • 15.
    CONT…. There aresix registers that are used for the arguments that the system call takes. The first argument goes in EBX, the second in ECX, then EDX, ESI, EDI, and finally EBP. If more then 6 arguments needed (not likely), the EBX register must contain the memory location where the list of arguments is stored. basic system calls: ◦ sys_open ◦ sys_close ◦ sys_read ◦ sys_write ◦ sys_Fork ◦ sys_exit
  • 16.
    CONT…. Sys_open -open a file system call number (in EAX): 5 arguments: ◦ EBX: The pathname of the file to open/create ◦ ECX: set file access bits (can be OR’d togather): O_RDONLY open for reading only O_WRONLY open for writing only O_RDRW open for both reading and writing O_APPEND open for appending to the end of file O_TRUNC truncate to 0 length if file exists O_CREAT create the file if it doesn’t exist ◦ EDX: set file permissions. Returns in EAX: file descriptor.
  • 17.
    Error handling Systemcalls set a global integer called errno on error. The constants that errno may be set to are (partial list): ◦ EPERM operation not permitted. ◦ ENOENT no such file or directory (not there). ◦ EIO I/O error – EEXIST file already exists. ◦ ENODEV no such device exists. ◦ EINVAL invalid argument passed.
  • 18.
  • 19.
  • 20.
    How to adda System Call Ubuntu 10.10 and using kernel version 2.6.37.3 If you are using any other kernel version just replace 2.6.37.3 with your version. I am also assuming you have extracted the source code. Now let the new system call’s name be “add2”.
  • 21.
    CONT…. 1. Nowyou will find a “arch” folder in the source code folder. Open the file arch/x86/kernel/syscall_table_32.S in a text editor. Go to the end of the document and add this line .long sys_add2 /* my code */
  • 22.
    CONT…. 2. Nowopen arch/x86/include/asm/unistd_32.h and find out #define __NR_prlimit64 340  Add a new line after this: #define __NR_add2 341  Don’t just close yet. After 3-4 lines, you will find a line like
  • 23.
    CONT…. #define NR_syscalls341  Change it to #define NR_syscalls 342. 3. Now edit arch/x86/include/asm/unistd_64.h  Find out: #define __NR_prlimit64 302 __SYSCALL(__NR_prlimit64,sys_prli mit64)
  • 24.
    CONT….  Nowafter these two lines, add these two lines #define __NR_add2 303 __SYSCALL(__NR_add2, sys_add2) 4. Now again in the source folder you will find a folder named include. Open the file include/linux/syscalls.h and go to the end of the file. Before the line
  • 25.
    CONT… #endif writethis prototype definition line: asmlinkage long sys_add2(int i,int j); 5. Now find out the kernel folder in the source directory. Create a new empty file in the kernel folder with the name “mysysteamcalls.c” . Add the following codes in the file:
  • 26.
    CONT… #include<linux/linkage.h> asmlinkagelong sys_add2(int i,int j) { return i+j; } 6. Now open the Makefile in this folder(/kernel/Makefile) and find out obj-y += groups.o
  • 27.
    CONT…  Adda new line before this line : obj-y += mysysteamcalls.o  Ok, this is the edit you need to do to add a new system call. Now compile or recompile the source code and enjoy your new system call.
  • 28.
    sample code tocall the system call : #include <stdio.h> #include <linux/unistd.h> #include <sys/syscall.h> //comment the following line if you are using 64 bit, this number is the same used previously #define sys_add2 341 //comment the following line if you are using 32 bit, this number is the same used previously
  • 29.
    CONT… int main(void) { int a,b,c; printf("Adding Two Numbers in Kernel Spacen"); printf("Input a: "); scanf("%d",&a); printf("Input b: "); scanf("%d", &b); c = syscall(sys_add2, a, b); printf("System call returned %dn", c); return 0; }
  • 30.
    How to replacesnew kernel file with old Kernel File: download the kernel from kernel.org http://www.kernel.org/pub/linux/kernel/ v3.0/linux-3.X.tar.bz2 Extract the tarball to /usr/src/linux-3.X/ tar -xvf linux-3.X.tar.bz2 -C /usr/src/linux-3.X/ Change the directory to /usr/src/linux- 3.x $ cd /usr/src/linux-3.x
  • 31.
    CONT… Configure Letsconfigure our new Linux Kernel with the configuration of old kernel (which is already installed in our machine) $ sudo make oldconfig The new kernel is installed, however we need to say to the Ubuntu to use this new one. Just type in the shell: sudo update-initramfs -k 2.6.38.3 -u sudo update-grub Restart your computer. After loaded, type in the shell: uname -a
  • 32.
    .deb Files linux-headers-2.6.34- 02063415_2.6.34- 02063415.201402101835_all.deb linux-headers-2.6.34-02063415- server_2.6.34- 02063415.201402101835_amd64.deb linux-image-2.6.34-02063415- generic_2.6.34- 02063415.201402101835_amd64.deb sudo dpkg -i linux*2.6.34-7.37*.deb sudo reboot
  • 33.
    Refrences: http://www.kernel.org http://www.Ubuntu.com kernel.ubuntu.com/~kernel-ppa/ mainline/ https://wiki.ubuntu.com/Kernel/Mainlin eBuilds ubuntuforums.org www.ubuntu.com/support/community http://www.linuxquestions.org
  • 34.