Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Writing A System Call In Linux

AjayTeja Reddy Doma


920036609
Table contents


What is system call

Kernel

Downloading kernel

List of files to be modified

Writing new kernel files

Compiling kernel

Testing system call
What is System Call?


A System Call acts as an interface between kernel
and user application.
Kernel


Kernel is a program and it is the heart of an OS.

Kernel is responsible for low-level tasks such as disk
management, memory management, etc.
Types of kernel:
1) Monoithic
2) Micro
3) Hybrid
List of files to be modified


Lets Assume that we are in directory named linux-($version)/

Kernel files to be modified are listed below:

/Makefile

/arch/x86/entry/syscalls/syscall_64.tbl

/include/linux/syscalls.h
Downloading Kernel

• Download kernel form kernel.org


$wget
https://cdn.kenrel.org/pub/linux/kernel/v5.x/linux-5.5.tar.x
z

After Downloading kernel extract it:


$tar –xvf linux-5.5.tar.xz
New files or directories to be created:


Create directory /hello (or mycall)

Create header file /mycall/mycall.h

Create /mycall/mycall.c

Create /mycall/Makefile

In UserSpace

test.h

test.c
Kernel Files


Create source file mycall.c in linux/mycall/ directory

#include<linux/linkage.h>
#include<linux/kernel.h>
#include “mycall.h”
asmlinkage long sys_mycall(void)
{
printk(“Hello World!”);
return 0;
}

Create a header file /mycall/mycall.h


asmlinkage long sys_mycall(void);

After creating header file create Makefile

obj-y := mycall.o

Modifing Makefile in main directory i.e, /linux-($version)/

For this, open the kernel's Makefile (found in the linux-


($version) directory) and look for the following line:
core -y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/

Add the folder at the end of line and /.

core -y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/


mycall/
Modifying System call table


System Call table is located at
/arch/x86/entry/syscalls/syscall_64.tbl

(Note for 32-bit system edit syscall_32.tbl)

add system call in 64 bit system calls.
 330 common getrandom sys_getrandom
331 common memfd_create sys_memfd_create
332 common kexec_file_load sys_kexec_file_load
333 common bpf sys_bpf
334 64 execveat stub_execveat
335 64 mycall sys_mycall

After system call for sys_mycall function

Add function to syscalls.h header file located at
/include/linux/syscalls.h
asmlinkage long sys_mycall(void);
Compiling kernel


Most important part of writing kernel is compiling
it.

Before make a configuration file:
Make localmodconfig
Make oldconfig
Make menuconfig

There are two ways to compile kernel
 Method 1:
 Copy the modified directory to /src/usr/ folder
as root user
 sudo make -j4 && sudo make modules_install
 sudo make install

Method 2:
 Using debian packaging
 In linux/ directory use following cmds
 make -j8 deb-pkg
 Above cmd will build Debian installable software
packages
 .deb files will be stored in parent directory of linux-
($version)/.
 Now go back parent directory using cd .. Cmd and
install .deb pkgs
 Sudo dpkg -i linux-*.deb
Testing System Call


Create a “test.h” header file in your home or desktop folder.

#include<unistd.h>
#include<sys/syscall.h>
#define hello 336
long mycall(void)
{
return syscall(hello, void);
}
Testing System Call


Create a “test.c” program in your home or desktop
folder.
#include<stdio.h>
#include "test.h"
int main(void)
{
printf("%d\n", mycall());
}

After writing above programs compile and run the
program

$cc test.c

$./a.out

After successful execution use dmesg cmd to know
whether its successfully executed or not.

$dmesg
Thank You

You might also like