title: When implementing a system call, how do you expose the system call number to userland?
body: <p>I'm fiddling with system calls. I've added two new ones and verified they work using calls to <code>syscall</code>.</p>
  
  <p>I would like the syscall numbers to be in a header so that userspace does not have to know the syscall number explicitly. </p>
  
  <p>In <code>arch/x86/syscalls/syscall_64.tbl</code> I have:</p>
  
  <pre><code>317     64      krun_read_msrs                  sys_krun_read_msrs
  318     64      krun_reset_msrs                 sys_krun_reset_msrs
  </code></pre>
  
  <p>And some grepping suggests that kbuild has auto-generated the macros for the new syscalls:</p>
  
  <pre><code>$ ag __NR_krun *
  arch/x86/include/generated/uapi/asm/unistd_64.h
  321:#define __NR_krun_read_msrs 317
  322:#define __NR_krun_reset_msrs 318
  </code></pre>
  
  <p>The filename suggests that I don't need to add the entries manually, but this is contrary to what the Linux Kernel docs have to say:</p>
  
  <pre><code>Some architectures (e.g. x86) have their own architecture-specific syscall tables, but several other architectures share a generic syscall table. Add your new system call to the generic list by adding an entry to the list in include/uapi/asm-generic/unistd.h:
  
  #define __NR_xyzzy 292
  __SYSCALL(__NR_xyzzy, sys_xyzzy)
  </code></pre>
  
  <p>Well, my syscalls are x86_64 specific, since they read and write MSRs found only in Intel chips. So after this, I went digging to see if I could find the arch-specific header for my amd64 system.</p>
  
  <p>You would expect it to be under <code>arch/x86_64</code> but there are no includes at all in there. So I assume x86_64 inherits from <code>x86</code>. That being the case, the arch-specific header should be:</p>
  
  <pre><code>arch/x86/include/uapi/asm/unistd.h
  </code></pre>
  
  <p>If you open that up, it's just a little wrapper to dispatch based on arch:</p>
  
  <pre><code># ifdef __i386__                                                                
  #  include &lt;asm/unistd_32.h&gt;                                                    
  # elif defined(__ILP32__)                                                       
  #  include &lt;asm/unistd_x32.h&gt;                                                   
  # else                                                                          
  #  include &lt;asm/unistd_64.h&gt;                                                    
  # endif
  </code></pre>
  
  <p>So that's presumably designed to pick up <code>/usr/include/x86_64-linux-gnu/asm/unistd.h</code>, but this does not yet include my new syscall numbers.</p>
  
  <p>I'd expect the <code>headers_install</code> target to install the new headers (perhaps), but alas it does not.</p>
  
  <p>I'm confused. Should I add my new syscalls to a file manually or not? If so which file? If not, how do I expose the auto-generated <code>__NR_*</code> macros to userspace in a standard location? </p>
  
  <p>Thanks</p>