Lots of fixes from rmk.  x86-64 fixes from Mats Petersson.

diff --git a/ash/Makefile b/ash/Makefile
index ca38182..39e03c9 100644
--- a/ash/Makefile
+++ b/ash/Makefile
@@ -15,9 +15,10 @@
 KLIBSRC = ../klibc
 include ../MCONFIG
 
-CFLAGS  = $(OPTFLAGS) $(REQFLAGS) -DSHELL
-LIBS    = $(KLIBC) $(LIBGCC)
-STRIP   = $(CROSS)strip -R .comment -R .note
+MAKEDEPS = -Wp,-MD,.$(subst /,-,$*).d
+CFLAGS   = $(MAKEDEPS) $(OPTFLAGS) $(REQFLAGS) -DSHELL
+LIBS     = $(KLIBC) $(LIBGCC)
+STRIP    = $(CROSS)strip -R .comment -R .note
 
 HOST_CFLAGS  = -g -I. -DSHELL
 
@@ -70,3 +71,5 @@
 clean:
 	rm -f core $(CLEANFILES) $(PROG) $(OBJS)
 
+spotless: clean
+	find . -name \*~ -not -type d -print0 | xargs -0rt rm -f
diff --git a/include/netinet/in.h b/include/netinet/in.h
index d2c2320..e4c2f88 100644
--- a/include/netinet/in.h
+++ b/include/netinet/in.h
@@ -5,6 +5,10 @@
 #ifndef _NETINET_IN_H
 #define _NETINET_IN_H
 
+/* added this include by Mats Petersson */
+#include <linux/socket.h>
+
+
 #include <stdint.h>
 #include <endian.h>		/* Must be included *before* <linux/in.h> */
 #include <linux/in.h>
diff --git a/include/setjmp.h b/include/setjmp.h
index 866d3a1..b504eb6 100644
--- a/include/setjmp.h
+++ b/include/setjmp.h
@@ -34,7 +34,7 @@
 #define sigsetjmp(__env, __save) \
 ({ \
   struct __sigjmp_buf *__e = (__env); \
-  sigprocmask(0, NULL, __e->__sigs); \
+  sigprocmask(0, NULL, &__e->__sigs); \
   setjmp(__e->__jmpbuf); \
 })
 
diff --git a/include/signal.h b/include/signal.h
index d417e40..c571a04 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -24,6 +24,7 @@
 
 __extern const char * const sys_siglist[];
 
+#if !defined(__x86_64__)
 /* This assumes sigset_t is either an unsigned long or an array of such,
    and that _NSIG_BPW in the kernel is always LONG_BIT */
 
@@ -55,6 +56,8 @@
   return (int)((lset[signum/LONG_BIT] >> (signum%LONG_BIT)) & 1);
 }
 
+#endif
+
 __extern __sighandler_t signal(int, __sighandler_t);
 __extern int sigaction(int, const struct sigaction *, struct sigaction *);
 __extern int sigprocmask(int, const sigset_t *, sigset_t *);
diff --git a/include/stdio.h b/include/stdio.h
index 466b95c..7bcf32e 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -97,4 +97,6 @@
 
 __extern void perror(const char *);
 
+__extern int rename(const char *, const char *);
+
 #endif /* _STDIO_H */
diff --git a/include/sys/mount.h b/include/sys/mount.h
index 4537248..313c90e 100644
--- a/include/sys/mount.h
+++ b/include/sys/mount.h
@@ -17,9 +17,11 @@
 #define MS_SYNCHRONOUS  16      /* Writes are synced at once */
 #define MS_REMOUNT      32      /* Alter flags of a mounted FS */
 #define MS_MANDLOCK     64      /* Allow mandatory locks on an FS */
+#define MS_DIRSYNC	128	/* Directory modifications are synchronous */
 #define MS_NOATIME      1024    /* Do not update access times. */
 #define MS_NODIRATIME   2048    /* Do not update directory access times */
 #define MS_BIND         4096
+#define MS_MOVE		8192
 #define MS_REC          16384
 #define MS_VERBOSE      32768
 #define MS_NOUSER       (1<<31)
diff --git a/klibc/MCONFIG b/klibc/MCONFIG
index 71dfc8d..b5bdd18 100644
--- a/klibc/MCONFIG
+++ b/klibc/MCONFIG
@@ -8,7 +8,7 @@
 include ../MCONFIG
 
 WARNFLAGS = -W -Wall -Wpointer-arith -Wwrite-strings -Wstrict-prototypes -Winline
-CFLAGS  = $(OPTFLAGS) $(REQFLAGS) $(WARNFLAGS)
+CFLAGS  = -Wp,-MD,$(dir $*).$(notdir $*).d $(OPTFLAGS) $(REQFLAGS) $(WARNFLAGS)
 
 SOFLAGS = -fPIC
 SOOBJS  = $(patsubst %.o,%.lo,$(LIBOBJS))
diff --git a/klibc/Makefile b/klibc/Makefile
index b03532d..bf49345 100644
--- a/klibc/Makefile
+++ b/klibc/Makefile
@@ -98,3 +98,7 @@
 
 spotless: clean
 	find . -name \*~ -not -type d -print0 | xargs -0rt rm -f
+
+ifneq ($(wildcard $(DIR)/.*.d),)
+include $(wildcard $(DIR)/.*.d)
+endif
diff --git a/klibc/SYSCALLS b/klibc/SYSCALLS
index 75b848a..5316e6b 100644
--- a/klibc/SYSCALLS
+++ b/klibc/SYSCALLS
@@ -70,6 +70,7 @@
 int link(const char *, const char *)
 int unlink(const char *)
 int chdir(const char *)
+int rename(const char *, const char *)
 int mknod(const char *, mode_t, dev_t)
 int chmod(const char *, mode_t)
 <!alpha> int utime(const char *, struct utimbuf *)
diff --git a/klibc/arch/x86_64/MCONFIG b/klibc/arch/x86_64/MCONFIG
new file mode 100644
index 0000000..422ae45
--- /dev/null
+++ b/klibc/arch/x86_64/MCONFIG
@@ -0,0 +1,14 @@
+# -*- makefile -*-
+#
+# arch/x86-64/MCONFIG
+#
+# Special rules for this architecture.  Note that this is actually
+# included from the main Makefile, and that pathnames should be
+# accordingly.
+#
+# Blatantly copied and modified from i386 version by Mats Petersson, AMD.
+#
+
+OPTFLAGS = -Os -fomit-frame-pointer \
+	   -falign-functions=0 -falign-jumps=0 -falign-loops=0
+BITSIZE  = 64
diff --git a/klibc/arch/x86_64/exits.S b/klibc/arch/x86_64/exits.S
index 2d49d75..618f4fb 100644
--- a/klibc/arch/x86_64/exits.S
+++ b/klibc/arch/x86_64/exits.S
@@ -28,7 +28,7 @@
 	.globl _exit
 	.type _exit,@function
 _exit:
-	movl $__NR_exit,%rax
+	movl $__NR_exit,%eax
 	/* The argument is already in %rdi */
 	syscall
 	.size _exit,.-exit
diff --git a/klibc/include/netinet/in.h b/klibc/include/netinet/in.h
index d2c2320..e4c2f88 100644
--- a/klibc/include/netinet/in.h
+++ b/klibc/include/netinet/in.h
@@ -5,6 +5,10 @@
 #ifndef _NETINET_IN_H
 #define _NETINET_IN_H
 
+/* added this include by Mats Petersson */
+#include <linux/socket.h>
+
+
 #include <stdint.h>
 #include <endian.h>		/* Must be included *before* <linux/in.h> */
 #include <linux/in.h>
diff --git a/klibc/include/setjmp.h b/klibc/include/setjmp.h
index 866d3a1..b504eb6 100644
--- a/klibc/include/setjmp.h
+++ b/klibc/include/setjmp.h
@@ -34,7 +34,7 @@
 #define sigsetjmp(__env, __save) \
 ({ \
   struct __sigjmp_buf *__e = (__env); \
-  sigprocmask(0, NULL, __e->__sigs); \
+  sigprocmask(0, NULL, &__e->__sigs); \
   setjmp(__e->__jmpbuf); \
 })
 
diff --git a/klibc/include/signal.h b/klibc/include/signal.h
index d417e40..c571a04 100644
--- a/klibc/include/signal.h
+++ b/klibc/include/signal.h
@@ -24,6 +24,7 @@
 
 __extern const char * const sys_siglist[];
 
+#if !defined(__x86_64__)
 /* This assumes sigset_t is either an unsigned long or an array of such,
    and that _NSIG_BPW in the kernel is always LONG_BIT */
 
@@ -55,6 +56,8 @@
   return (int)((lset[signum/LONG_BIT] >> (signum%LONG_BIT)) & 1);
 }
 
+#endif
+
 __extern __sighandler_t signal(int, __sighandler_t);
 __extern int sigaction(int, const struct sigaction *, struct sigaction *);
 __extern int sigprocmask(int, const sigset_t *, sigset_t *);
diff --git a/klibc/include/stdio.h b/klibc/include/stdio.h
index 466b95c..7bcf32e 100644
--- a/klibc/include/stdio.h
+++ b/klibc/include/stdio.h
@@ -97,4 +97,6 @@
 
 __extern void perror(const char *);
 
+__extern int rename(const char *, const char *);
+
 #endif /* _STDIO_H */
diff --git a/klibc/include/sys/mount.h b/klibc/include/sys/mount.h
index 4537248..313c90e 100644
--- a/klibc/include/sys/mount.h
+++ b/klibc/include/sys/mount.h
@@ -17,9 +17,11 @@
 #define MS_SYNCHRONOUS  16      /* Writes are synced at once */
 #define MS_REMOUNT      32      /* Alter flags of a mounted FS */
 #define MS_MANDLOCK     64      /* Allow mandatory locks on an FS */
+#define MS_DIRSYNC	128	/* Directory modifications are synchronous */
 #define MS_NOATIME      1024    /* Do not update access times. */
 #define MS_NODIRATIME   2048    /* Do not update directory access times */
 #define MS_BIND         4096
+#define MS_MOVE		8192
 #define MS_REC          16384
 #define MS_VERBOSE      32768
 #define MS_NOUSER       (1<<31)
diff --git a/klibc/inet/inet_ntoa.c b/klibc/inet/inet_ntoa.c
index e761f7b..5340aa6 100644
--- a/klibc/inet/inet_ntoa.c
+++ b/klibc/inet/inet_ntoa.c
@@ -8,9 +8,12 @@
 char *inet_ntoa(struct in_addr addr)
 {
   static char name[16];
-  uint32_t v = addr.s_addr;
+  union {
+    uint8_t  b[4];
+    uint32_t l;
+  } a;
+  a.l = addr.s_addr;
 
-  sprintf(name, "%u.%u.%u.%u",
-	  (v >> 24), (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff);
+  sprintf(name, "%u.%u.%u.%u", a.b[0], a.b[1], a.b[2], a.b[3]);
   return name;
 }
diff --git a/klibc/inet/inet_ntop.c b/klibc/inet/inet_ntop.c
index 1309feb..377bab7 100644
--- a/klibc/inet/inet_ntop.c
+++ b/klibc/inet/inet_ntop.c
@@ -15,10 +15,13 @@
   switch ( af ) {
   case AF_INET:
     {
-      uint32_t v = ((const struct in_addr *)cp)->s_addr;
+      union {
+        uint8_t  b[4];
+        uint32_t l;
+      } a;
+      a.l = ((const struct in_addr *)cp)->s_addr;
 
-      xlen = snprintf(buf, len, "%u.%u.%u.%u",
-		      (v >> 24), (v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff);
+      xlen = snprintf(buf, len, "%u.%u.%u.%u", a.b[0], a.b[1], a.b[2], a.b[3]);
     }
     break;
 
diff --git a/klibc/memcpy.c b/klibc/memcpy.c
index 0faab06..9f11cdd 100644
--- a/klibc/memcpy.c
+++ b/klibc/memcpy.c
@@ -14,7 +14,7 @@
 	       : "+c" (n), "+S" (p), "+D" (q) :: "edx");
 #elif defined(__x86_64__)
   asm volatile("cld ; movq %0,%%rdx ; shrq $3,%0 ; rep ; movsq ; "
-	       "movq %%rdx,%0 ; andl $7,%0 ; rep ; movsb"
+	       "movl %%edx,%0 ; andl $7,%0 ; rep ; movsb"
 	       : "+c" (n), "+S" (p), "+D" (q) :: "rdx");
 #else
   while ( n-- ) {
diff --git a/klibc/memset.c b/klibc/memset.c
index 07fbcd8..dcf307a 100644
--- a/klibc/memset.c
+++ b/klibc/memset.c
@@ -15,10 +15,10 @@
 	       : "a" ((unsigned char)c * 0x01010101U)
 	       : "edx");
 #elif defined(__x86_64__)
-  asm volatile("cld ; movl %0,%%rdx ; shrq $3,%0 ; rep ; stosq ; "
-	       "movl %%rdx,%0 ; andl $7,%0 ; rep ; stosb"
+  asm volatile("cld ; movl %0,%%edx ; shrq $3,%0 ; rep ; stosq ; "
+	       "movl %%edx,%0 ; andl $7,%0 ; rep ; stosb"
 	       : "+c" (n), "+D" (q)
-	       : "a" ((unsigned char)c * 0x01010101U)
+	       : "a" ((unsigned char)c * 0x0101010101010101U)
 	       : "rdx");
 #else
   while ( n-- ) {
diff --git a/klibc/siglist.c b/klibc/siglist.c
index 8e224c1..dce7355 100644
--- a/klibc/siglist.c
+++ b/klibc/siglist.c
@@ -76,9 +76,6 @@
 #ifdef SIGSTOP
   [SIGSTOP] = "Stopped (signal)",
 #endif
-#ifdef SIGSWI
-  [SIGSWI] = "Software interrupt",
-#endif
 #ifdef SIGSYS
   [SIGSYS] = "Bad system call",
 #endif
diff --git a/klibc/signal.c b/klibc/signal.c
index 9dd5abd..939011e 100644
--- a/klibc/signal.c
+++ b/klibc/signal.c
@@ -4,6 +4,18 @@
 
 #include <signal.h>
 
+#if __x86_64__
+/* Quick hack: Add sigaction to this file. Not a good solution, it should 
+   be in signal.h. 
+*/
+struct sigaction {
+	__sighandler_t sa_handler;
+	unsigned long sa_flags;
+	void (*sa_restorer)(void);
+	sigset_t sa_mask;		/* mask last for extensibility */
+};
+#endif
+
 __sighandler_t signal(int signum, __sighandler_t handler)
 {
   struct sigaction sa;
diff --git a/klibc/syscalls.pl b/klibc/syscalls.pl
index 1b9ca13..fdb8c78 100644
--- a/klibc/syscalls.pl
+++ b/klibc/syscalls.pl
@@ -46,6 +46,13 @@
 
 	open(OUT, "> syscalls/${fname}.c")
 	    or die "$0: Cannot open syscalls/${fname}.c\n";
+
+	if ( $fname eq "rt_sigaction") {
+	    print OUT "#ifdef __x86_64__\n\n";
+	    print OUT "struct sigaction;\n\n";
+            print OUT "#endif\n\n"
+	}
+
 	print OUT "#include \"syscommon.h\"\n\n";
 	
 	if ( $fname ne $sname ) {
@@ -54,6 +61,7 @@
 	}
 
 	print OUT "_syscall", scalar(@args), $stype, "(", $type, ',', $fname;
+
 	$i = 0;
 	foreach $arg ( @args ) {
 	    print OUT ",", $arg, ",a",$i++;