x86_64: Add x86_64 syscalls and tune gen scripts for x86_64

* Tune syscall stubs generator for 4th target: x86_64
* Update SYSCALLS.TXT with x86_64 syscalls:
 - Most of the x86 syscalls are equally supported
 - *32 syscalls are not supported on 64-bit
 - *64 syscalls are replaced accordingly without 64 suffix
 - Some syscalls are not supported, replaced with x86_64 analog

Syscalls are regenerated as separate patch for review convenience.

Change-Id: I4ea2e0f13759b0aa61f05208ca68da8d6bc7c048
Signed-off-by: Pavel Chupin <pavel.v.chupin@intel.com>
diff --git a/libc/tools/bionic_utils.py b/libc/tools/bionic_utils.py
index eed9001..a00080d 100644
--- a/libc/tools/bionic_utils.py
+++ b/libc/tools/bionic_utils.py
@@ -2,7 +2,7 @@
 
 import sys, os, commands, string
 
-all_arches = [ "arm", "mips", "x86" ]
+all_arches = [ "arm", "mips", "x86", "x86_64" ]
 
 # basic debugging trace support
 # call D_setlevel to set the verbosity level
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index ea60eec..a3c8450 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -1,12 +1,10 @@
 #!/usr/bin/python
-#
-# this tool is used to generate the syscall assembler templates
-# to be placed into arch-{arm,x86,mips}/syscalls, as well as the content
-# of arch-{arm,x86,mips}/linux/_syscalls.h
-#
+
+# This tool is used to generate the assembler system call stubs,
+# the header files listing all available system calls, and the
+# makefiles used to build all the stubs.
 
 import sys, os.path, glob, re, commands, filecmp, shutil
-import getpass
 
 from bionic_utils import *
 
@@ -15,7 +13,9 @@
 # temp directory where we store all intermediate files
 bionic_temp = "/tmp/bionic_gensyscalls/"
 
-def make_dir( path ):
+DRY_RUN = False
+
+def make_dir(path):
     path = os.path.abspath(path)
     if not os.path.exists(path):
         parent = os.path.dirname(path)
@@ -23,23 +23,25 @@
             make_dir(parent)
         os.mkdir(path)
 
-def create_file( relpath ):
-    dir = os.path.dirname( bionic_temp + relpath )
+def create_file(relpath):
+    dir = os.path.dirname(bionic_temp + relpath)
     make_dir(dir)
-    return open( bionic_temp + relpath, "w" )
+    return open(bionic_temp + relpath, "w")
+
+
+syscall_stub_header = """/* autogenerated by gensyscalls.py */
+#include <asm/unistd.h>
+#include <linux/err.h>
+#include <machine/asm.h>
+
+ENTRY(%(fname)s)
+"""
+
 
 #
 # x86 assembler templates for each syscall stub
 #
 
-x86_header = """/* autogenerated by gensyscalls.py */
-#include <linux/err.h>
-#include <machine/asm.h>
-#include <asm/unistd.h>
-
-ENTRY(%(fname)s)
-"""
-
 x86_registers = [ "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp" ]
 
 x86_call = """    movl    $%(idname)s, %%eax
@@ -59,18 +61,27 @@
 """
 
 #
+# x86_64 assembler templates for each syscall stub
+#
+
+x86_64_call = """    movl    $%(idname)s, %%eax
+    syscall
+    cmpq    $-MAX_ERRNO, %%rax
+    jb      1f
+    negl    %%eax
+    movl    %%eax, %%edi
+    call    __set_errno
+    orq     $-1, %%rax
+1:
+    ret
+END(%(fname)s)
+"""
+
+#
 # ARM assembler templates for each syscall stub
 #
 
-arm_header = """/* autogenerated by gensyscalls.py */
-#include <asm/unistd.h>
-#include <linux/err.h>
-#include <machine/asm.h>
-
-ENTRY(%(fname)s)
-"""
-
-arm_eabi_call_default = arm_header + """\
+arm_eabi_call_default = syscall_stub_header + """\
     mov     ip, r7
     ldr     r7, =%(idname)s
     swi     #0
@@ -82,7 +93,7 @@
 END(%(fname)s)
 """
 
-arm_eabi_call_long = arm_header + """\
+arm_eabi_call_long = syscall_stub_header + """\
     mov     ip, sp
     .save   {r4, r5, r6, r7}
     stmfd   sp!, {r4, r5, r6, r7}
@@ -178,6 +189,12 @@
             count += 1
     return count
 
+def count_generic_param_registers64(params):
+    count = 0
+    for param in params:
+        count += 1
+    return count
+
 # This lets us support regular system calls like __NR_write and also weird
 # ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
 def make__NR_name(name):
@@ -193,11 +210,22 @@
         self.other_files = []
         self.syscalls = []
 
+    def x86_64_genstub(self, fname, numparams, idname):
+        t = { "fname"  : fname, "idname" : idname }
+
+        result = syscall_stub_header % t
+        # rcx is used as 4th argument. Kernel wants it at r10.
+        if (numparams > 3):
+            result += "    movq    %rcx, %r10\n"
+
+        result += x86_64_call % t
+        return result
+
     def x86_genstub(self, fname, numparams, idname):
         t = { "fname"  : fname,
               "idname" : idname }
 
-        result     = x86_header % t
+        result     = syscall_stub_header % t
         stack_bias = 4
         for r in range(numparams):
             result     += "    pushl   " + x86_registers[r] + "\n"
@@ -222,7 +250,7 @@
         t = { "fname"  : fname,
               "idname" : idname }
 
-        result = x86_header % t
+        result = syscall_stub_header % t
         stack_bias = 4
 
         # save the regs we need
@@ -294,6 +322,9 @@
             if t.has_key("mips"):
                 t["asm-mips"] = self.mips_genstub(syscall_func, make__NR_name(syscall_name))
 
+            if t.has_key("x86_64"):
+                num_regs = count_generic_param_registers64(syscall_params)
+                t["asm-x86_64"] = self.x86_64_genstub(syscall_func, num_regs, __NR_name)
 
     # Scan a Linux kernel asm/unistd.h file containing __NR_* constants
     # and write out equivalent SYS_* constants for glibc source compatibility.
@@ -323,6 +354,8 @@
         self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-mips/asm/unistd.h")
         glibc_fp.write("#elif defined(__i386__)\n")
         self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-x86/asm/unistd_32.h")
+        glibc_fp.write("#elif defined(__x86_64__)\n")
+        self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-x86/asm/unistd_64.h")
         glibc_fp.write("#endif\n")
 
         glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
@@ -358,63 +391,62 @@
 
 
     def regenerate(self):
-        D( "scanning for existing architecture-specific stub files" )
+        D("scanning for existing architecture-specific stub files...")
 
         bionic_libc_root_len = len(bionic_libc_root)
 
         for arch in all_arches:
             arch_path = bionic_libc_root + "arch-" + arch
-            D( "scanning " + arch_path )
-            files = glob.glob( arch_path + "/syscalls/*.S" )
+            D("scanning " + arch_path)
+            files = glob.glob(arch_path + "/syscalls/*.S")
             for f in files:
-                self.old_stubs.append( f[bionic_libc_root_len:] )
+                self.old_stubs.append(f[bionic_libc_root_len:])
 
-        D( "found %d stub files" % len(self.old_stubs) )
+        D("found %d stub files" % len(self.old_stubs))
 
-        if not os.path.exists( bionic_temp ):
-            D( "creating %s" % bionic_temp )
-            make_dir( bionic_temp )
+        if not os.path.exists(bionic_temp):
+            D("creating %s..." % bionic_temp)
+            make_dir(bionic_temp)
 
-        D( "re-generating stubs and support files" )
+        D("re-generating stubs and support files...")
 
         self.gen_glibc_syscalls_h()
         for arch in all_arches:
             self.gen_arch_syscalls_mk(arch)
         self.gen_syscall_stubs()
 
-        D( "comparing files" )
+        D("comparing files...")
         adds    = []
         edits   = []
 
         for stub in self.new_stubs + self.other_files:
-            if not os.path.exists( bionic_libc_root + stub ):
+            if not os.path.exists(bionic_libc_root + stub):
                 # new file, git add it
-                D( "new file:     " + stub)
-                adds.append( bionic_libc_root + stub )
-                shutil.copyfile( bionic_temp + stub, bionic_libc_root + stub )
+                D("new file:     " + stub)
+                adds.append(bionic_libc_root + stub)
+                shutil.copyfile(bionic_temp + stub, bionic_libc_root + stub)
 
-            elif not filecmp.cmp( bionic_temp + stub, bionic_libc_root + stub ):
-                D( "changed file: " + stub)
-                edits.append( stub )
+            elif not filecmp.cmp(bionic_temp + stub, bionic_libc_root + stub):
+                D("changed file: " + stub)
+                edits.append(stub)
 
         deletes = []
         for stub in self.old_stubs:
             if not stub in self.new_stubs:
-                D( "deleted file: " + stub)
-                deletes.append( bionic_libc_root + stub )
+                D("deleted file: " + stub)
+                deletes.append(bionic_libc_root + stub)
 
+        if not DRY_RUN:
+            if adds:
+                commands.getoutput("git add " + " ".join(adds))
+            if deletes:
+                commands.getoutput("git rm " + " ".join(deletes))
+            if edits:
+                for file in edits:
+                    shutil.copyfile(bionic_temp + file, bionic_libc_root + file)
+                commands.getoutput("git add " + " ".join((bionic_libc_root + file) for file in edits))
 
-        if adds:
-            commands.getoutput("git add " + " ".join(adds))
-        if deletes:
-            commands.getoutput("git rm " + " ".join(deletes))
-        if edits:
-            for file in edits:
-                shutil.copyfile( bionic_temp + file, bionic_libc_root + file )
-            commands.getoutput("git add " +
-                               " ".join((bionic_libc_root + file) for file in edits))
-
-        commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT"))
+            commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT"))
 
         if (not adds) and (not deletes) and (not edits):
             D("no changes detected!")