3

I'm trying to import new system call in the kernel 3.19. I've followed the tutorial given here!

This is my simple code to implement factorial calculation via system call.

 #include <linux/kernel.h>


 asmlinkage long sys_fact(int a)
 {
    int n;
    int c;
    for(n = 1;n <= a;n++)
    c = c * n;  
    printk(KERN_INFO "Factorial calculated!\n");


    return((long) c);
 }

I'm getting Undefined reference to sys_fact error when I try to compile the C code. The program in which I'm using this system call is as follows.

#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>

int main()
{
  int n;
  printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);
  printf("Factorial of %d = %d\n", n, sys_fact(9));

  return 0;
}

My system is 64 bit ubuntu 14.04 and I've followed above mentioned tutorial according to my system.

Also, I've combined following commands while installing kernel, and I think this is the reason it did not gave error while installation of kernel.

 make && make modules_install && make install

Kernel installation took 2-3 hours, and I'm frustrated now. Please help!!

Edits I made to syscall_64.tbl(final four entries).

 320    common  kexec_file_load     sys_kexec_file_load
 321    common  bpf                 sys_bpf
 322    64      execveat            stub_execveat
 323    common  fact                sys_fact
3
  • Show the changes you have done to linux-3.8/arch/x86/syscalls/syscall_32.tbl Commented Mar 19, 2015 at 4:45
  • @SantoshA updated my question. Have a look. Commented Mar 19, 2015 at 4:53
  • 2
    Refer this http://linuxseekernel.blogspot.in/2014/07/adding-system-call-in-x86-qemu.html Commented Mar 19, 2015 at 5:28

2 Answers 2

2

The cause is that although you may be running with a new kernel, the #include headers and the C library are still old so they don't know anything about your newly added system call. So you cannot expect sys_fact to be defined.

As suggested by @SantoshA, the site http://linuxseekernel.blogspot.in/2014/07/adding-system-call-in-x86-qemu.html suggests using the syscall() function with the system call number.

Sign up to request clarification or add additional context in comments.

1 Comment

syscall(323,n) returns 0 for any value of n (323 is system call number ofmy system call).
1

your new syscall doesn't have a libc api, try invoke it with syscall(323, args ...) like shown in the tutorial.

And just checking that you have booted with your new kernel? You can follow the installation steps here. Or it might be easier to use an emulator like Qemu so you don't have to reboot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.