[MIPS] Add support for MIPS syscalls

Change-Id: I4deba67e15c865c4c2db03064c04098a09828ea6
Signed-off-by: Raghu Gandham <raghu@mips.com>
Signed-off-by: Chris Dearman <chris@mips.com>
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index bed9445..947e444 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -1,11 +1,12 @@
 #!/usr/bin/python
 #
-# this tool is used to generate the syscall assmbler templates
-# to be placed into arch-x86/syscalls, as well as the content
-# of arch-x86/linux/_syscalls.h
+# 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
 #
 
 import sys, os.path, glob, re, commands, filecmp, shutil
+import getpass
 
 from bionic_utils import *
 
@@ -31,9 +32,10 @@
 bionic_temp = "/tmp/bionic_gensyscalls/"
 
 # all architectures, update as you see fit
-all_archs = [ "arm", "x86" ]
+all_archs = [ "arm", "x86", "mips" ]
 
 def make_dir( path ):
+    path = os.path.abspath(path)
     if not os.path.exists(path):
         parent = os.path.dirname(path)
         if parent:
@@ -183,6 +185,32 @@
     .fnend
 """
 
+# mips assembler templates for each syscall stub
+#
+mips_call = """/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+    .text
+    .globl %(fname)s
+    .align 4
+    .ent %(fname)s
+
+%(fname)s:
+    .set noreorder
+    .cpload $t9
+    li $v0, %(idname)s
+    syscall
+    bnez $a3, 1f
+    move $a0, $v0
+    j $ra
+    nop
+1:
+    la $t9,__set_errno
+    j $t9
+    nop
+    .set reorder
+    .end %(fname)s
+"""
+
 def param_uses_64bits(param):
     """Returns True iff a syscall parameter description corresponds
        to a 64-bit type."""
@@ -331,6 +359,10 @@
                 return thumb_call_long % t
         return thumb_call_default % t
 
+    def mips_genstub(self,fname, idname):
+        t = { "fname"  : fname,
+              "idname" : idname }
+        return mips_call % t
 
     def superh_genstub(self, fname, flags, idname):
         numargs = int(flags)
@@ -360,7 +392,7 @@
             syscall_params = t["params"]
             syscall_name   = t["name"]
 
-            if t["id"] >= 0:
+            if t["common"] >= 0 or t["armid"] >= 0:
                 num_regs = count_arm_param_registers(syscall_params)
                 if gen_thumb_stubs:
                     t["asm-thumb"] = self.thumb_genstub(syscall_func,num_regs,"__NR_"+syscall_name)
@@ -370,7 +402,7 @@
                     else:
                         t["asm-arm"]   = self.arm_genstub(syscall_func,num_regs,"__NR_"+syscall_name)
 
-            if t["id2"] >= 0:
+            if t["common"] >= 0 or t["x86id"] >= 0:
                 num_regs = count_generic_param_registers(syscall_params)
                 if t["cid"] >= 0:
                     t["asm-x86"] = self.x86_genstub_cid(syscall_func, num_regs, "__NR_"+syscall_name, t["cid"])
@@ -380,62 +412,70 @@
                 E("cid for dispatch syscalls is only supported for x86 in "
                   "'%s'" % syscall_name)
                 return
+            if t["common"] >= 0 or t["mipsid"] >= 0:
+                t["asm-mips"] = self.mips_genstub(syscall_func,"__NR_"+syscall_name)
 
 
     def gen_NR_syscall(self,fp,name,id):
         fp.write( "#define __NR_%-25s    (__NR_SYSCALL_BASE + %d)\n" % (name,id) )
 
-    # now dump the content of linux/_syscalls.h
+    # now dump the content of linux-syscalls.h
     def gen_linux_syscalls_h(self):
         path = "include/sys/linux-syscalls.h"
         D( "generating "+path )
         fp = create_file( path )
         fp.write( "/* auto-generated by gensyscalls.py, do not touch */\n" )
         fp.write( "#ifndef _BIONIC_LINUX_SYSCALLS_H_\n\n" )
-        fp.write( "#if !defined __ASM_ARM_UNISTD_H && !defined __ASM_I386_UNISTD_H\n" )
+        fp.write( "#if !defined __ASM_ARM_UNISTD_H && !defined __ASM_I386_UNISTD_H && !defined __ASM_MIPS_UNISTD_H\n" )
         fp.write( "#if defined __arm__ && !defined __ARM_EABI__ && !defined __thumb__\n" )
-        fp.write( "  #  define __NR_SYSCALL_BASE  0x900000\n" )
-        fp.write( "  #else\n" )
-        fp.write( "  #  define  __NR_SYSCALL_BASE  0\n" )
-        fp.write( "  #endif\n\n" )
+        fp.write( "  #  define __NR_SYSCALL_BASE 0x900000\n" )
+        fp.write( "#elif defined(__mips__)\n" )
+        fp.write( "  #  define __NR_SYSCALL_BASE 4000\n" )
+        fp.write( "#else\n" )
+        fp.write( "  #  define __NR_SYSCALL_BASE 0\n" )
+        fp.write( "#endif\n\n" )
 
         # first, all common syscalls
-        for sc in self.syscalls:
-            sc_id  = sc["id"]
-            sc_id2 = sc["id2"]
+        for sc in sorted(self.syscalls,key=lambda x:x["common"]):
+            sc_id  = sc["common"]
             sc_name = sc["name"]
-            if sc_id == sc_id2 and sc_id >= 0:
+            if sc_id >= 0:
                 self.gen_NR_syscall( fp, sc_name, sc_id )
 
         # now, all arm-specific syscalls
         fp.write( "\n#ifdef __arm__\n" );
         for sc in self.syscalls:
-            sc_id  = sc["id"]
-            sc_id2 = sc["id2"]
+            sc_id  = sc["armid"]
             sc_name = sc["name"]
-            if sc_id != sc_id2 and sc_id >= 0:
+            if sc_id >= 0:
                 self.gen_NR_syscall( fp, sc_name, sc_id )
         fp.write( "#endif\n" );
 
         gen_syscalls = {}
         # finally, all i386-specific syscalls
         fp.write( "\n#ifdef __i386__\n" );
-        for sc in self.syscalls:
-            sc_id  = sc["id"]
-            sc_id2 = sc["id2"]
+        for sc in sorted(self.syscalls,key=lambda x:x["x86id"]):
+            sc_id  = sc["x86id"]
             sc_name = sc["name"]
-            if sc_id != sc_id2 and sc_id2 >= 0 and sc_name not in gen_syscalls:
-                self.gen_NR_syscall( fp, sc_name, sc_id2 )
+            if sc_id >= 0 and sc_name not in gen_syscalls:
+                self.gen_NR_syscall( fp, sc_name, sc_id )
                 gen_syscalls[sc_name] = True
         fp.write( "#endif\n" );
 
+        # all mips-specific syscalls
+        fp.write( "\n#ifdef __mips__\n" );
+        for sc in sorted(self.syscalls,key=lambda x:x["mipsid"]):
+            sc_id = sc["mipsid"]
+            if sc_id >= 0:
+                self.gen_NR_syscall( fp, sc["name"], sc_id )
+        fp.write( "#endif\n" );
+
         fp.write( "\n#endif\n" )
         fp.write( "\n#endif /* _BIONIC_LINUX_SYSCALLS_H_ */\n" );
         fp.close()
         self.other_files.append( path )
 
-
-    # now dump the content of linux/_syscalls.h
+    # now dump the content of linux-syscalls.h
     def gen_linux_unistd_h(self):
         path = "include/sys/linux-unistd.h"
         D( "generating "+path )
@@ -462,6 +502,7 @@
         arch_test = {
             "arm": lambda x: x.has_key("asm-arm") or x.has_key("asm-thumb"),
             "x86": lambda x: x.has_key("asm-x86"),
+            "mips": lambda x: x.has_key("asm-mips")
         }
 
         for sc in self.syscalls:
@@ -471,6 +512,7 @@
         fp.close()
         self.other_files.append( path )
 
+
     # now generate each syscall stub
     def gen_syscall_stubs(self):
         for sc in self.syscalls:
@@ -498,6 +540,13 @@
                 fp.close()
                 self.new_stubs.append( fname )
 
+            if sc.has_key("asm-mips") and 'mips' in all_archs:
+                fname = "arch-mips/syscalls/%s.S" % sc["func"]
+                D2( ">>> generating "+fname )
+                fp = create_file( fname )
+                fp.write(sc["asm-mips"])
+                fp.close()
+                self.new_stubs.append( fname )
 
     def  regenerate(self):
         D( "scanning for existing architecture-specific stub files" )
@@ -515,7 +564,7 @@
 
         if not os.path.exists( bionic_temp ):
             D( "creating %s" % bionic_temp )
-            os.mkdir( bionic_temp )
+            make_dir( bionic_temp )
 
 #        D( "p4 editing source files" )
 #        for arch in all_archs: