title: Extending the Rasbian Kernel (Linux Kernel 3.10.28) for Arm / Raspberry PI - How to correctly add own system calls?
body: <p>I need to add an own system call to the Raspbian Linux Kernel. Now I am stuck after searching for about 2 days to find a solution.</p>
  
  <p>To add a system call, I am basically following the general outline 
  (<a href="http://elinux.org/RPi_Kernel_Compilation" rel="nofollow noreferrer">http://elinux.org/RPi_Kernel_Compilation</a>) using the kernel sources from the following git repo: </p>
  
  <p>git://github.com/raspberrypi/tools.git </p>
  
  <p>I have installed a cross-compile environment using crosstool-ng (<a href="http://www.kitware.com/blog/home/post/426" rel="nofollow noreferrer">http://www.kitware.com/blog/home/post/426</a>).</p>
  
  <p>All these above works. I am able to compile and deploy a new kernel. I am furthermore able to cross-compile for the Raspbian.</p>
  
  <p>I am trying to add a 'hello world' system call. The function resides in its own implementation files (kernel/helloworld.?) and are implemented as:</p>
  
  <p>helloworld.c:</p>
  
  <pre><code>#include &lt;linux/linkage.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/random.h&gt;
  #include "helloworld.h"
  
  asmlinkage long sys_helloworld(){
    printk (KERN_EMERG "hello world!");
    return get_random_int()*4;
  }
  </code></pre>
  
  <p>helloworld.h:</p>
  
  <pre><code>#ifndef HELLO_WORLD_H
  #define HELLO_WORLD_H
  asmlinkage long sys_helloworld(void);
  #endif
  </code></pre>
  
  <p>The Makefile is extended accordingly.</p>
  
  <p>I am now stuck in the error message</p>
  
  <pre><code>AS      arch/arm/kernel/entry-common.o
  arch/arm/kernel/entry-common.S: Assembler messages:
  arch/arm/kernel/entry-common.S:104: Error: __NR_syscalls is not equal to the size of the syscall table
  make[1]: *** [arch/arm/kernel/entry-common.o] Error 1
  </code></pre>
  
  <p>By following the advice in <a href="https://stackoverflow.com/questions/17751216/writing-a-new-system-call">Writing a new system call</a>, I added the following:</p>
  
  <ul>
  <li><p>arch/arm/kernel/calls.S</p>
  
  <pre><code>CALL(sys_helloworld)
  </code></pre></li>
  <li><p>arch/arm/include/uapi/asm/unistd.h</p>
  
  <pre><code>#define __NR_helloworld                 (__NR_SYSCALL_BASE+380)
  </code></pre></li>
  <li><p>include/uapi/asm-generic/unistd.h</p>
  
  <pre><code>#define __NR_helloworld 274
  __SYSCALL(__NR_helloworld, sys_helloworld)
  
  #define __NR_syscalls 275
  </code></pre></li>
  <li><p>arch/x86/syscalls/syscall_32.tbl</p>
  
  <pre><code>351     i386    helloworld              sys_helloworld
  </code></pre></li>
  </ul>
  
  <p>I am now stuck in solving the error.</p>
  
  <p>When removing the line in calls.S, the kernel compiles fine; though I can't invoke the system call. When adding the line stated above, I am getting the mentioned error.</p>
  
  <p>For reference: The client-code for testing the system call is:</p>
  
  <pre><code>#include &lt;linux/unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/syscall.h&gt;
  
  int main (int argc, char* argv[])
  {
      int i=atoi(argv[1]);
      int j=-1;
      printf("invocing kernel function %i\n", i);
      j=syscall(i); /* 350 is our system calls offset number */
      printf("invoked. Return is %i. Bye.\n", j);
  
      return 0;
  }
  </code></pre>
  
  <p>All other system calls (e.g., 1 == sys_exit) work fine.</p>
  
  <p>Any ideas what I am missing? E.g., I don't fully get how to implement rasens answer.</p>