title: How to access the system call from user-space?
body: <p>I read some paragraphs in LKD<sup>1</sup>
  and I just cannot understand the contents below:</p>
  
  <p><blockquote></p>
  
  <p><strong>Accessing the System Call from User-Space</strong></p>
  
  <p>Generally, the C library provides support for system calls. User applications can pull in function prototypes from the standard headers and link with the C library to use your system call (or the library routine that, in turn, uses your syscall call). If you just wrote the system call, however, it is doubtful that glibc already supports it!</p>
  
  <p>Thankfully, Linux provides a set of macros for wrapping access to system calls. It sets up the register contents and issues the trap instructions. These macros are named <code>_syscall<i>n</i>()</code>, where <em><code>n</code></em> is between zero and six. The number corresponds to the number of parameters passed into the syscall because the macro needs to know how many parameters to expect and, consequently, push into registers. For example, consider the system call <code>open()</code>, defined as</p>
  
  <pre><code>long open(const char *filename, int flags, int mode)
  </code></pre>
  
  <p>The syscall macro to use this system call without explicit library support would be</p>
  
  <pre><code>#define __NR_open 5
  _syscall3(long, open, const char *, filename, int, flags, int, mode)
  </code></pre>
  
  <p>Then, the application can simply call <code>open()</code>.</p>
  
  <p>For each macro, there are 2+2&times;n parameters. The first parameter corresponds to the return type of the syscall. The second is the name of the system call. Next follows the type and name for each parameter in order of the system call. The <code>__NR_open</code> define is in <code>&lt;asm/unistd.h&gt;</code>; it is the system call number. The <code>_syscall3</code> macro expands into a C function with inline assembly; the assembly performs the steps discussed in the previous section to push the system call number and parameters into the correct registers and issue the software interrupt to trap into the kernel. Placing this macro in an application is all that is required to use the <code>open()</code> system call.</p>
  
  <p>Let's write the macro to use our splendid new <code>foo()</code> system call and then write some test code to show off our efforts.</p>
  
  <pre><code>#define __NR_foo 283
  __syscall0(long, foo)
  
  int main ()
  {
          long stack_size;
  
          stack_size = foo ();
          printf ("The kernel stack size is %ld\n", stack_size);
          return 0;
  }
  </code></pre>
  
  <p></blockquote></p>
  
  <p>What does <strong><em>the application can simply call <code>open()</code></em></strong> mean?</p>
  
  <p>Besides, for the last piece of code, where is the declaration of <code>foo()</code>? And how can I make this piece of code compilable and runnable? What are the header files I need to include?</p>
  
  <p>__________<br>
  <sup>1</sup> <em>Linux Kernel Development</em>, by Robert Love. 
  <a href="https://ignorelist.files.wordpress.com/2012/01/linux_kernel_development__3rd_edition_.pdf" rel="noreferrer">PDF file at wordpress.com</a> (go to page 81); <a href="https://books.google.com/books?id=5BwdBAAAQBAJ&amp;pg=PA81&amp;dq=%22Accessing+the+System+Call+from+User-Space%22&amp;hl=en&amp;sa=X&amp;ved=0ahUKEwiC5YbK3vDUAhUIez4KHYyQB0MQ6AEIJjAA#v=onepage&amp;q=%22Accessing%20the%20System%20Call%20from%20User-Space%22&amp;f=false" rel="noreferrer">Google Books result</a>.</p>