Thursday, 20 December 2012

Linux System Call for Arm Architecture

Implementation of System Calls for Arm Architecture

 I have used Linux 3.2 version for this as I was testing my system call in Mobile phone so this system call procedure may not work for the newer release of kernel.
Step 1:
Add a system call number
path : /arch/arm/include/asm/unistd.h
e.g. :
#define __NR_my_add     (__NR_SYSCALL_BASE +377 )

Step 2: 
Make an entry to calls.S with path given below
path: /arch/arm/kernel/calls.S
e.g. :
CALL(sys_my_add); (the position has to be same as the system call number ie 377)
 This file will be automatically added to entry-common.S as the file entry-common.S include it as follows.
#include "calls.S"

We can define a wrapper function in entry-common.S and then register this wrapper function in calls.S. In the entry-common.S then finally we can jump to our system call routine.
path: /arch/arm/kernel/entry-common.S
e.g. :
In calls.S
CALL(sys_my_add_wrapper);  (the position has to be same as the system call number ie 377)
In entry-common.S,
Add the following code
     b      sys_my_add

Step 3:
Writing the routine for system call. Again it can be done in two ways:
i. Directly it can be included in the existing files( so that it doesn't require us to modify the Makefile)
ii. Make a separate file and change the Make file to make sure it compiles.
asmlinkage long sys_my_add(int a,int b)
        printk(KERN_ALERT "The addition is %d ",a+b);
        return a+b;
If separate file (say my_add.c) is maintained we have to add the following line in the Makefile.
obj-y :=my_add.o

Step 4: 
Register the function with the prototype.
path: /include/linux/syscall.h
Add the following line:
asmlinkage long sys_my_add(int,int);

Implementation in User Space 

I have written a code in Assembly to call the function.
mov  r7,#377
mov  r0,#25
mov r1,#35
mov r7,#1
SWI  0

we can also replace the last three lines of code by using SWI 377 instead.

No comments :

Post a Comment