SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
Creating a system call in Linux
Tushar B. Kute,
http://tusharkute.com
x86 Protection Rings
Level 0
Level 1
Level 2
Level 3
Operating system
kernel
Operating system
services
Applications
Privileged instructions
Can be executed only
When current privileged
Level (CPL) is 0
Monolithic Kernel
• All kernel routines are together.
– A system call interface.
• Examples:
– Linux.
– Most Unix OS.
– NT.
Kernel
many many things
entry
User
program
User
program
call
return
Micro Kernel
• Micro-kernel is “micro”
– Services are
implemented as regular
process.
– Micro-kernel get
services on behalf of
users by messaging with
the service processes.
– Examples: Taos, Mach,
L4.
kernel
entry
User
program
Services
call
return
System call mechanism
• User code can be arbitrary.
• User code cannot modify
kernel memory.
• Makes a system call with
parameters.
• The call mechanism switches
code to kernel mode.
• Execute system call.
• Return with results.
Kernel in protected
memory
entry
User
program
User
program
call
return
HW Device
Interrupt
HW exceptions
SW exceptions
System Service
Call
Virtual address
exceptions
HW implementation of the
boundary
System
service
dispatcher
System
services
Interrupt
service
routines
Exception
dispatcher
Exception
handlers
VM manager’s
pager
Sys_call_table
OS Kernel : Trap Handler
Library function vs. System Call
strace and ltrace
strace
• Strace monitors the system calls and
signals of a specific program.
• It is helpful when you do not have the
source code and would like to debug the
execution of a program.
• strace provides you the execution
sequence of a binary from start to end.
– strace ls
ltrace
• ltrace monitors the library calls and
signals of a specific program.
• It is helpful when you do not have the
source code and would like to debug the
execution of a program.
• ltrace provides you the execution
sequence of a binary from start to end.
– ltrace printf
• Download the kernel source code from http://kernel.org
– Actual link:
https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.1.tar.xz
– Can be downloaded by command:
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.1.tar.xz
– Downloaded file is: linux-3.18.1.tar.xz
• Extract the file using command or GUI:
– tar -xvf linux-3.18.1.tar.xz
Download and extract Linux source code
Extraction using GUI
Use this option for
Extraction
Linux Source Tree Layout
/linux-3.18.7Documentation
arch
fs
init kernel
include
ipc
drivers
net
mmlib
scripts
alpha
arm
i386
ia64
m68k
mips
mips64
ppc
s390
sh
sparc
sparc64
acorn
atm
block
cdrom
char
dio
fc4
i2c
i2o
ide
ieee1394
isdn
macintosh
misc
net
…
adfs
affs
autofs
autofs4
bfs
code
cramfs
devfs
devpts
efs
ext2
fat
hfs
hpfs
…
asm-alpha
asm-arm
asm-generic
asm-i386
asm-ia64
asm-m68k
asm-mips
asm-mips64
linux
math-emu
net
pcmcia
scsi
video …
adfs
affs
autofs
autofs4
bfs
code
cramfs
devfs
devpts
efs
ext2
fat
hfs
hpfs …
802
appletalk
atm
ax25
bridge
core
decnet
econet
ethernet
ipv4
ipv6
ipx
irda
khttpd
lapb
…
• Create a directory hello in the kernel
source directory:
–mkdir hello
• Change into this directory
–cd hello
• Create a “hello.c” file in this folder and add
the definition of the system call to it as
given below (you can use any text editor ).
Define a new system call
#include <linux/kernel.h>
asmlinkage long sys_hello(void)
{
   printk(KERN_INFO “Hello worldn”);
   return 0;
}
hello.c
• Create a “Makefile” in the hello folder and
add the given line  to it.
– gedit Makefile
• add the following line to it:-
– obj­y := hello.o
• This is to ensure that the hello.c file is
compiled and included in the kernel
source code.
Create Makefile
• change back into the linux-3.16 folder and open
Makefile.
– gedit Makefile
– goto line number 842 which says :-   “core-y +=
kernel/ mm/ fs/ ipc/ security/ crypto/ block/ “
– change this to   “core-y += kernel/ mm/ fs/ ipc/
security/ crypto/ block/ hello/“
• This is to tell the compiler that the source files of
our new system call (sys_hello()) are in present in
the hello directory.
Add the hello directory to the kernel’s Makefile
• If your system is a 64 bit system you will need to alter
the syscall_64.tbl file else syscall_32.tbl.
– cd arch/x86/syscalls
– gedit syscall_32.tbl
• Add the following line in the end of the file :-
358    i386    hello    sys_hello
358 – It is the number of the system call . It should be
one plus the number of the last system call. (it was 358
in my system). This has to be noted down to make the
system call in the userspace program.
Add system call to the table
• cd  include/linux/
• gedit syscalls.h
– add the following line to the end of the file just
before the #endif  statement at the very bottom.
– asmlinkage long sys_hello(void);
• This defines the prototype of the function of our
system call.”asmlinkage” is a key word used to indicate
that all parameters of the function would be
available on the stack.
• Now compile the linux source code according to the
standard procedure.
Add system call to header file
• Create a “userspace.c” program in your home folder and type in the
following code :-
#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
    long int r = syscall(358);
    printf(“System call sys_hello returned %ldn”, r);
    return 0;
}
Test the system call
• Now compile this program using the following command.
– gcc userspace.c
• If all goes well you will not have any errors else, rectify the
errors. Now run the program using the following command.
– ./a.out
• You will see the following line getting printed in the
terminal if all the steps were followed correctly.
– “System call sys_hello returned 0“.
• Now to check the message of the kernel you can run the
following command.
– dmesg
Test the system call
tushar@tusharkute.com
Thank you
This presentation is created using LibreOffice Impress 4.2.7.2, can be used freely as per GNU General Public License
Blogs
http://digitallocha.blogspot.in
http://kyamputar.blogspot.in
Web Resources
http://tusharkute.com

Weitere ähnliche Inhalte

Was ist angesagt?

Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
mcganesh
 
Kernel compilation
Kernel compilationKernel compilation
Kernel compilation
mcganesh
 
Linux architecture
Linux architectureLinux architecture
Linux architecture
mcganesh
 

Was ist angesagt? (20)

System Calls
System CallsSystem Calls
System Calls
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
 
Implementation of Pipe in Linux
Implementation of Pipe in LinuxImplementation of Pipe in Linux
Implementation of Pipe in Linux
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOs
 
system management -shell programming by gaurav raikar
system management -shell programming by gaurav raikarsystem management -shell programming by gaurav raikar
system management -shell programming by gaurav raikar
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
 
LINUX Device Drivers
LINUX Device DriversLINUX Device Drivers
LINUX Device Drivers
 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating System
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
 
Board support package_on_linux
Board support package_on_linuxBoard support package_on_linux
Board support package_on_linux
 
Kernel compilation
Kernel compilationKernel compilation
Kernel compilation
 
Architecture Of The Linux Kernel
Architecture Of The Linux KernelArchitecture Of The Linux Kernel
Architecture Of The Linux Kernel
 
Introduction to Linux Kernel
Introduction to Linux KernelIntroduction to Linux Kernel
Introduction to Linux Kernel
 
UNIX Operating System ppt
UNIX Operating System pptUNIX Operating System ppt
UNIX Operating System ppt
 
Chapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux KernelChapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux Kernel
 
Linux architecture
Linux architectureLinux architecture
Linux architecture
 
Chapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System ConceptsChapter 1 Introduction to Operating System Concepts
Chapter 1 Introduction to Operating System Concepts
 

Ähnlich wie Part 04 Creating a System Call in Linux

Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
Sudharsan S
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityOMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
Andrew Case
 
Linux or unix interview questions
Linux or unix interview questionsLinux or unix interview questions
Linux or unix interview questions
Teja Bheemanapally
 

Ähnlich wie Part 04 Creating a System Call in Linux (20)

How To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OSHow To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OS
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Operating System Structure Part-I.pdf
Operating System Structure Part-I.pdfOperating System Structure Part-I.pdf
Operating System Structure Part-I.pdf
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityOMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
 
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with VolatlityOMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
OMFW 2012: Analyzing Linux Kernel Rootkits with Volatlity
 
Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra Solutions
 
Clear cache memory
Clear cache memoryClear cache memory
Clear cache memory
 
Rasperry pi Part 7
Rasperry pi Part 7Rasperry pi Part 7
Rasperry pi Part 7
 
Introduction to Assembly Language Programming
Introduction to Assembly Language ProgrammingIntroduction to Assembly Language Programming
Introduction to Assembly Language Programming
 
operating system
operating systemoperating system
operating system
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Linux or unix interview questions
Linux or unix interview questionsLinux or unix interview questions
Linux or unix interview questions
 
운영체제론 Ch20
운영체제론 Ch20운영체제론 Ch20
운영체제론 Ch20
 
(SAS) UNIX X Command Tips and Tricks
(SAS) UNIX X Command Tips and Tricks(SAS) UNIX X Command Tips and Tricks
(SAS) UNIX X Command Tips and Tricks
 
linux installation.pdf
linux installation.pdflinux installation.pdf
linux installation.pdf
 
Operating system concepts
Operating system conceptsOperating system concepts
Operating system concepts
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and Github
 

Mehr von Tushar B Kute

Mehr von Tushar B Kute (20)

Apache Pig: A big data processor
Apache Pig: A big data processorApache Pig: A big data processor
Apache Pig: A big data processor
 
01 Introduction to Android
01 Introduction to Android01 Introduction to Android
01 Introduction to Android
 
Ubuntu OS and it's Flavours
Ubuntu OS and it's FlavoursUbuntu OS and it's Flavours
Ubuntu OS and it's Flavours
 
Install Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. KuteInstall Drupal in Ubuntu by Tushar B. Kute
Install Drupal in Ubuntu by Tushar B. Kute
 
Install Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. KuteInstall Wordpress in Ubuntu Linux by Tushar B. Kute
Install Wordpress in Ubuntu Linux by Tushar B. Kute
 
Share File easily between computers using sftp
Share File easily between computers using sftpShare File easily between computers using sftp
Share File easily between computers using sftp
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Implementation of FIFO in Linux
Implementation of FIFO in LinuxImplementation of FIFO in Linux
Implementation of FIFO in Linux
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in Linux
 
Open source applications softwares
Open source applications softwaresOpen source applications softwares
Open source applications softwares
 
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
Introduction to Ubuntu Edge Operating System (Ubuntu Touch)
 
Technical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrcTechnical blog by Engineering Students of Sandip Foundation, itsitrc
Technical blog by Engineering Students of Sandip Foundation, itsitrc
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Module 02 Using Linux Command Shell
Module 02 Using Linux Command ShellModule 02 Using Linux Command Shell
Module 02 Using Linux Command Shell
 
Module 01 Introduction to Linux
Module 01 Introduction to LinuxModule 01 Introduction to Linux
Module 01 Introduction to Linux
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
See through C
See through CSee through C
See through C
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Part 04 Creating a System Call in Linux

  • 1. Creating a system call in Linux Tushar B. Kute, http://tusharkute.com
  • 2. x86 Protection Rings Level 0 Level 1 Level 2 Level 3 Operating system kernel Operating system services Applications Privileged instructions Can be executed only When current privileged Level (CPL) is 0
  • 3. Monolithic Kernel • All kernel routines are together. – A system call interface. • Examples: – Linux. – Most Unix OS. – NT. Kernel many many things entry User program User program call return
  • 4. Micro Kernel • Micro-kernel is “micro” – Services are implemented as regular process. – Micro-kernel get services on behalf of users by messaging with the service processes. – Examples: Taos, Mach, L4. kernel entry User program Services call return
  • 5. System call mechanism • User code can be arbitrary. • User code cannot modify kernel memory. • Makes a system call with parameters. • The call mechanism switches code to kernel mode. • Execute system call. • Return with results. Kernel in protected memory entry User program User program call return
  • 6. HW Device Interrupt HW exceptions SW exceptions System Service Call Virtual address exceptions HW implementation of the boundary System service dispatcher System services Interrupt service routines Exception dispatcher Exception handlers VM manager’s pager Sys_call_table OS Kernel : Trap Handler
  • 7. Library function vs. System Call
  • 9. strace • Strace monitors the system calls and signals of a specific program. • It is helpful when you do not have the source code and would like to debug the execution of a program. • strace provides you the execution sequence of a binary from start to end. – strace ls
  • 10. ltrace • ltrace monitors the library calls and signals of a specific program. • It is helpful when you do not have the source code and would like to debug the execution of a program. • ltrace provides you the execution sequence of a binary from start to end. – ltrace printf
  • 11. • Download the kernel source code from http://kernel.org – Actual link: https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.1.tar.xz – Can be downloaded by command: wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.1.tar.xz – Downloaded file is: linux-3.18.1.tar.xz • Extract the file using command or GUI: – tar -xvf linux-3.18.1.tar.xz Download and extract Linux source code
  • 12. Extraction using GUI Use this option for Extraction
  • 13. Linux Source Tree Layout /linux-3.18.7Documentation arch fs init kernel include ipc drivers net mmlib scripts alpha arm i386 ia64 m68k mips mips64 ppc s390 sh sparc sparc64 acorn atm block cdrom char dio fc4 i2c i2o ide ieee1394 isdn macintosh misc net … adfs affs autofs autofs4 bfs code cramfs devfs devpts efs ext2 fat hfs hpfs … asm-alpha asm-arm asm-generic asm-i386 asm-ia64 asm-m68k asm-mips asm-mips64 linux math-emu net pcmcia scsi video … adfs affs autofs autofs4 bfs code cramfs devfs devpts efs ext2 fat hfs hpfs … 802 appletalk atm ax25 bridge core decnet econet ethernet ipv4 ipv6 ipx irda khttpd lapb …
  • 14. • Create a directory hello in the kernel source directory: –mkdir hello • Change into this directory –cd hello • Create a “hello.c” file in this folder and add the definition of the system call to it as given below (you can use any text editor ). Define a new system call
  • 16. • Create a “Makefile” in the hello folder and add the given line  to it. – gedit Makefile • add the following line to it:- – obj­y := hello.o • This is to ensure that the hello.c file is compiled and included in the kernel source code. Create Makefile
  • 17. • change back into the linux-3.16 folder and open Makefile. – gedit Makefile – goto line number 842 which says :-   “core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ “ – change this to   “core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ hello/“ • This is to tell the compiler that the source files of our new system call (sys_hello()) are in present in the hello directory. Add the hello directory to the kernel’s Makefile
  • 18. • If your system is a 64 bit system you will need to alter the syscall_64.tbl file else syscall_32.tbl. – cd arch/x86/syscalls – gedit syscall_32.tbl • Add the following line in the end of the file :- 358    i386    hello    sys_hello 358 – It is the number of the system call . It should be one plus the number of the last system call. (it was 358 in my system). This has to be noted down to make the system call in the userspace program. Add system call to the table
  • 19. • cd  include/linux/ • gedit syscalls.h – add the following line to the end of the file just before the #endif  statement at the very bottom. – asmlinkage long sys_hello(void); • This defines the prototype of the function of our system call.”asmlinkage” is a key word used to indicate that all parameters of the function would be available on the stack. • Now compile the linux source code according to the standard procedure. Add system call to header file
  • 20. • Create a “userspace.c” program in your home folder and type in the following code :- #include <stdio.h> #include <linux/kernel.h> #include <sys/syscall.h> #include <unistd.h> int main() {     long int r = syscall(358);     printf(“System call sys_hello returned %ldn”, r);     return 0; } Test the system call
  • 21. • Now compile this program using the following command. – gcc userspace.c • If all goes well you will not have any errors else, rectify the errors. Now run the program using the following command. – ./a.out • You will see the following line getting printed in the terminal if all the steps were followed correctly. – “System call sys_hello returned 0“. • Now to check the message of the kernel you can run the following command. – dmesg Test the system call
  • 22. tushar@tusharkute.com Thank you This presentation is created using LibreOffice Impress 4.2.7.2, can be used freely as per GNU General Public License Blogs http://digitallocha.blogspot.in http://kyamputar.blogspot.in Web Resources http://tusharkute.com