Top Banner
How To Add System Call In UBUNTU Operating System Presented by: Pratik Tambekar Guided by: Mr. T.R Ravi
34

How To Add System Call In Ubuntu OS

Jun 09, 2015

Download

Engineering

Pratik Tambekar

Pratik Tambekar
M.TECH
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How To Add System Call In Ubuntu OS

How To Add System Call In UBUNTU Operating System

Presented by:

Pratik Tambekar

Guided by:

Mr. T.R Ravi

Page 2: How To Add System Call In Ubuntu OS

CONTENTS

WHY UBUNTU?INTRODUCTIONWHAT IS KERNEL?WHAT IS SYSTEM CALL?HOW TO ADD SYSTEM CALLHOW TO REPLACES NEW KERNEL

FILE WITH OLD KERNEL FILEREFRENCES

Page 3: How To Add System Call In Ubuntu OS

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.

Page 4: How To Add System Call In Ubuntu OS

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.

Page 5: How To Add System Call In Ubuntu OS

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

Page 6: How To Add System Call In Ubuntu OS

Ubuntu Releases

Page 7: How To Add System Call In Ubuntu OS

Table of versions

Page 8: How To Add System Call In Ubuntu OS

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

Page 9: How To Add System Call In Ubuntu OS

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.

Page 10: How To Add System Call In Ubuntu OS

CONT....

Types of Kernels: Kernels may be classified mainly in two categories

1) Monolithic 2) Micro Kernel

Linux follows the monolithic modular approach

Page 11: How To Add System Call In Ubuntu OS

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

Page 12: How To Add System Call In Ubuntu OS

CONT....

Page 13: How To Add System Call In Ubuntu OS

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.

Page 14: How To Add System Call In Ubuntu OS

Anatomy of a System CallA 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.

Page 15: How To Add System Call In Ubuntu OS

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

Page 16: How To Add System Call In Ubuntu OS

CONT…. Sys_open - open a filesystem call number (in EAX): 5arguments:

◦ 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.On errors: -1.

Page 17: How To Add System Call In Ubuntu OS

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.

Page 18: How To Add System Call In Ubuntu OS

CONT…

Page 19: How To Add System Call In Ubuntu OS

Example

Page 20: How To Add System Call In Ubuntu OS

How to add a System CallUbuntu 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”.

Page 21: How To Add System Call In Ubuntu OS

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….

Page 22: How To Add System Call In Ubuntu OS

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

Page 23: How To Add System Call In Ubuntu OS

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_

prlimit64)

Page 24: How To Add System Call In Ubuntu OS

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

Page 25: How To Add System Call In Ubuntu OS

CONT…

#endifwrite 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:

Page 26: How To Add System Call In Ubuntu OS

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

Page 27: How To Add System Call In Ubuntu OS

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.

Page 28: How To Add System Call In Ubuntu OS

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

#define sys_add2 303

Page 29: How To Add System Call In Ubuntu OS

CONT…

int main(void){ int a,b,c; printf("Adding Two Numbers in Kernel Space\n"); printf("Input a: "); scanf("%d",&a); printf("Input b: "); scanf("%d", &b); c = syscall(sys_add2, a, b); printf("System call returned %d\n", c); return 0;}

Page 30: How To Add System Call In Ubuntu OS

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.bz2Extract 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

Page 31: How To Add System Call In Ubuntu OS

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

Page 32: How To Add System Call In Ubuntu OS

.deb Fileslinux-headers-2.6.34-02063415_2.6.34-

02063415.201402101835_all.deblinux-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

Page 33: How To Add System Call In Ubuntu OS

Refrences:

http://www.kernel.orghttp://www.Ubuntu.comkernel.ubuntu.com/~kernel-ppa/mainline/

https://wiki.ubuntu.com/Kernel/MainlineBuilds

ubuntuforums.orgwww.ubuntu.com/support/

communityhttp://www.linuxquestions.org

Page 34: How To Add System Call In Ubuntu OS

Thank you