Much better x86-64 syscall macros

diff --git a/include/arch/x86_64/klibc/archsys.h b/include/arch/x86_64/klibc/archsys.h
index 3bd8b0a..2ec72cd 100644
--- a/include/arch/x86_64/klibc/archsys.h
+++ b/include/arch/x86_64/klibc/archsys.h
@@ -7,9 +7,88 @@
 #ifndef _KLIBC_ARCHSYS_H
 #define _KLIBC_ARCHSYS_H
 
-/* x86-64 seems to miss _syscall6() from its headers */
+/* The x86-64 syscall headers are needlessly inefficient */
 
-#ifndef _syscall6
+#undef _syscall0
+#undef _syscall1
+#undef _syscall2
+#undef _syscall3
+#undef _syscall4
+#undef _syscall5
+#undef _syscall6
+
+#define _syscall0(type,name) \
+type name (void) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall1(type,name,type1,arg1) \
+type name (type1 arg1) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall2(type,name,type1,arg1,type2,arg2) \
+type name (type1 arg1,type2 arg2) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
+type name (type1 arg1,type2 arg2,type3 arg3) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2), \
+          "d" (arg3) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
+{ \
+long __res; \
+register type4 __r10 asm("%r10") = arg4; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2), \
+          "d" (arg3),"r" (__r10) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
+          type5,arg5) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
+{ \
+long __res; \
+register type4 __r10 asm("%r10") = arg4; \
+register type5 __r8  asm("%r8")  = arg5; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2), \
+          "d" (arg3),"r" (__r10),"r" (__r8) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
 
 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
           type5,arg5,type6,arg6) \
@@ -27,6 +106,4 @@
 __syscall_return(type,__res); \
 }
 
-#endif /* _syscall6 missing */
-
 #endif /* _KLIBC_ARCHSYS_H */
diff --git a/klibc/arch/x86_64/include/klibc/archsys.h b/klibc/arch/x86_64/include/klibc/archsys.h
index 3bd8b0a..2ec72cd 100644
--- a/klibc/arch/x86_64/include/klibc/archsys.h
+++ b/klibc/arch/x86_64/include/klibc/archsys.h
@@ -7,9 +7,88 @@
 #ifndef _KLIBC_ARCHSYS_H
 #define _KLIBC_ARCHSYS_H
 
-/* x86-64 seems to miss _syscall6() from its headers */
+/* The x86-64 syscall headers are needlessly inefficient */
 
-#ifndef _syscall6
+#undef _syscall0
+#undef _syscall1
+#undef _syscall2
+#undef _syscall3
+#undef _syscall4
+#undef _syscall5
+#undef _syscall6
+
+#define _syscall0(type,name) \
+type name (void) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall1(type,name,type1,arg1) \
+type name (type1 arg1) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall2(type,name,type1,arg1,type2,arg2) \
+type name (type1 arg1,type2 arg2) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall3(type,name,type1,arg1,type2,arg2,type3,arg3) \
+type name (type1 arg1,type2 arg2,type3 arg3) \
+{ \
+long __res; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2), \
+          "d" (arg3) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \
+{ \
+long __res; \
+register type4 __r10 asm("%r10") = arg4; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2), \
+          "d" (arg3),"r" (__r10) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
+
+#define _syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
+          type5,arg5) \
+type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
+{ \
+long __res; \
+register type4 __r10 asm("%r10") = arg4; \
+register type5 __r8  asm("%r8")  = arg5; \
+__asm__ volatile (__syscall \
+        : "=a" (__res) \
+        : "0" (__NR_##name),"D" (arg1),"S" (arg2), \
+          "d" (arg3),"r" (__r10),"r" (__r8) \
+        : __syscall_clobber); \
+__syscall_return(type,__res); \
+}
 
 #define _syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \
           type5,arg5,type6,arg6) \
@@ -27,6 +106,4 @@
 __syscall_return(type,__res); \
 }
 
-#endif /* _syscall6 missing */
-
 #endif /* _KLIBC_ARCHSYS_H */