Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Part 04 Creating a System Call in Linux

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Nächste SlideShare
Introduction to System Calls
Introduction to System Calls
Wird geladen in …3
×

Hier ansehen

1 von 22 Anzeige

Part 04 Creating a System Call in Linux

Herunterladen, um offline zu lesen

Presentation on "System Call creation in Linux".
Presented at Army Institute of Technology, Pune for FDP on "Basics of Linux Kernel Programming". by Tushar B Kute (http://tusharkute.com).

Presentation on "System Call creation in Linux".
Presented at Army Institute of Technology, Pune for FDP on "Basics of Linux Kernel Programming". by Tushar B Kute (http://tusharkute.com).

Anzeige
Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

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

Anzeige

Weitere von Tushar B Kute (20)

Aktuellste (20)

Anzeige

Part 04 Creating a System Call in Linux

  1. 1. Creating a system call in Linux Tushar B. Kute, http://tusharkute.com
  2. 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. 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. 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. 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. 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. 7. Library function vs. System Call
  8. 8. strace and ltrace
  9. 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. 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. 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. 12. Extraction using GUI Use this option for Extraction
  13. 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. 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
  15. 15. #include <linux/kernel.h> asmlinkage long sys_hello(void) {    printk(KERN_INFO “Hello worldn”);    return 0; } hello.c
  16. 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. 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. 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. 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. 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. 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. 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

×