Wednesday, 14 November 2012

Helloworld System Call in linux

Hello everyone,through this blog would like to share my knowledge in "how to create a helloworld system call in linux" .

I did this in virtualbox provided by oracle on an Ubuntu 12.04.1 LTS OS running on an 64bit machine.I downloaded the linux version of virtualbox.


During installation of virtualbox it will ask you to provide the image of Operation System that you want to install.I installed Linux Mint.
 
Following are steps that I followed to implement helloworld system call.
 
1.Download the source code of the kernel .
 
type "sudo apt-get install ncurses-dev " to Load additional Ubuntu packages needed to compile the kernel
 
type "sudo apt-get linux-source" or "sudo apt-get source linux" to get the source code

Now you need to Unpack the Linux source code. For that go to file " /usr/src "
then type " sudo bunzip2 linux-source-2.6.31.tar.bz2 " and " sudo tar -vxf linux-source-2.6.31.tar "

 
2.  Open the file arch/x86/kernel/syscall_table_32.S and add the following line
     
  " .long sys_helloworld "
 

3. Defining our system call

For that go to the file  " arch/x86/include/asm/unistd_32.h " .Open the file unistd_32.h in sudo mode.Then add " #define ___NR_helloworld 349 " at the end of macro definition.

After that you need to increment the value of the macro NR_SYSCALLS

"  #define NR_syscalls 350 "

4. Now add the definition to arch/x86/include/asm/unistd_64.h
 
  " asmlinkage long sys_helloworld(void); "
 
5. Go to the root directory of the kernel (my root directory is " usr/src/linux-source-2.6.31") then create a directory name "helloworld". Within that directory
create a file " helloworld.c " with the following code

#include <linux/kernel.h>

asmlinkage long sys_helloworld(void)
{
   printk("Helloworld !");
   return 0;
}

6. After creating the function definition, create a file named Makefile within the hello directory and the following content to the file 

obj-y := helloworld.o

7. Go to the MakeFile in root directory of the kernel and the edit the following line

 "core-y   +=kernel/ mm/ fs/ ipc/ security/ crypto/ block/ " to "core-y   +=kernel/ mm/ fs/ ipc/ security/ crypto/ block/ helloworld/"
 
 Note that there is a space between "/" & "helloworld"

8. Compile the kernel and then reboot the kernel

" sudo make " to compile the kernel (this may take 1 hr) and then reboot the kernel
 
9.Now write a the following C code in your home directory
 
 #include <stdio.h>
 #include <linux/kernel.h>
 #include<sys/syscall.h>
 #include<unistd.h>
 
 #define __NR_helloworld 312 //349 if you are running a 32 bit kernel
 
 
  long helloworld_syscall(void)
 {
 return syscall(__NR_helloworld);
 }

 int main()
 {

 long int a = helloworld_syscall();
 printf("System Call returned %ld \n",a);
 return 0;
 }
 
 
The output will be " System Call returned 0 ".The printk’s output get written to the kernel log. To view it, run the command
 
" dmesg "
 
you can see the helloworld on the last line of the kernel log

references:
 
Operating System concepts by silberschatz


2 comments:

  1. http://simplyeazy.com/how-to-add-a-system-call-to-linux-kernel/
    try this link its working perfectly for fedora...

    ReplyDelete