System call implementation System call for addition of

  • Slides: 10
Download presentation
System call implementation • System call for addition of two numbers.

System call implementation • System call for addition of two numbers.

Step 1: change to the kernel source directory using, cd /usr/src/linux-3. 19. 3/ Step

Step 1: change to the kernel source directory using, cd /usr/src/linux-3. 19. 3/ Step 2: Define a new system call sys_add() Create a directory hello in the kernel source directory: mkdir add Change into this directory cd add Step 3: Create a “add. c” file in this folder and add the definition of the system call to it as given below (you can use any text editor). gedit add. c

 • Add the following code: add. c • #include<linux/linkage. h> • asmlinkage long

• Add the following code: add. c • #include<linux/linkage. h> • asmlinkage long sys_add(int i, int j) • { • return i+j; • }

 • Step 4: • Create a “Makefile” in the add folder and add

• Step 4: • Create a “Makefile” in the add folder and add the given line to it. gedit Makefile • Add the following line to it: obj-y : = add. o This is to ensure that the add. c file is compiled and included in the kernel source code.

 • Step 5: • Add the add directory to the kernel’s Makefile •

• Step 5: • Add the add directory to the kernel’s Makefile • change back into the linux-3. 17. 7 folder and open Makefile gedit Makefile • goto line number 842 or any which says : “core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ ” • change this to “core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ add/” • This is to tell the compiler that the source files of our new system call (sys_add()) are in present in the hello directory.

 • Step 6: • Add the new system call (sys_add() ) into the

• Step 6: • Add the new system call (sys_add() ) into the system call table (syscall_32. tbl file) • If your system is a 64 bit system you will need to alter the syscall_64. tbl file. cd arch/x 86/syscalls gedit syscall_32. tbl • add the following line in the end of the file : 354 i 386 hello sys_add • 354 – It is the number of the system call. It should be one plus the number of the last system call. • (it was 354 in my system). This has to be noted down to make the system call in the userspace program.

 • Step 7: • Add the new system call(sys_add() ) in the system

• Step 7: • Add the new system call(sys_add() ) in the system call header file. 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_add(int i, int j); • 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.

 • Step 8: • Compile this kernel on your system. • Just like

• Step 8: • Compile this kernel on your system. • Just like kernel compilation assignment for new kernel 3. 19. 3 • Eg. • sudo make menuconfig • sudo make defconfig • Sudo make modules_install • Sudo reboot

STEP 9: To test the system call. • Create a “userspace. c” program in

STEP 9: To test the system call. • Create a “userspace. c” program in your home folder and type in the following code : • #include <stdio. h> • #include <linux/unistd. h> • #include <sys/syscall. h> • int main(void) • { • int a, b, c; • printf("Adding Two Numbers in Kernel Spacen"); • printf("Input a: "); • scanf("%d", &a); • printf("Input b: "); • scanf("%d", &b); • c = syscall(sys_add, a, b); • printf("System call returned %dn", c); • return 0; • }

 • Now run the program in home directory • gcc userspace. c •

• Now run the program in home directory • 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 It will show you addition of two numbers And sentence “System call returns ”