diff --git a/lib/libc/x86_64/gen/mcontext.S b/lib/libc/x86_64/gen/mcontext.S
index ea31674..7ab5eb9 100644
--- a/lib/libc/x86_64/gen/mcontext.S
+++ b/lib/libc/x86_64/gen/mcontext.S
@@ -66,8 +66,8 @@ ENTRY(_get_mcontext)
 	 * Saved stack pointer as if we had returned from this
 	 * procedure.
 	 */
-	movq	%rsp,MC_RSP(%eax)
-	addq	$8,MC_RSP(%eax)
+	movq	%rsp,MC_RSP(%rdi)
+	addq	$8,MC_RSP(%rdi)
 
 	/*
 	 * Save eflags
@@ -89,7 +89,7 @@ ENTRY(_get_mcontext)
 	/*
 	 * Set MC_LEN
 	 */
-	movl    $SIZEOF_MCONTEXT_T,MC_LEN(%eax)
+	movl    $SIZEOF_MCONTEXT_T,MC_LEN(%rdi)
 
 	/*
 	 * Return 0
diff --git a/lib/libc/x86_64/sys/Makefile.inc b/lib/libc/x86_64/sys/Makefile.inc
index 5145e76..4ba301a 100644
--- a/lib/libc/x86_64/sys/Makefile.inc
+++ b/lib/libc/x86_64/sys/Makefile.inc
@@ -3,7 +3,7 @@
 
 SRCS+=	amd64_get_fsbase.c amd64_get_gsbase.c amd64_set_fsbase.c amd64_set_gsbase.c
 
-MDASM=	vfork.S brk.S cerror.S exect.S pipe.S ptrace.S \
+MDASM=	vfork.S brk.S cerror.S exect.S getcontext.S pipe.S ptrace.S \
 	reboot.S sbrk.S setlogin.S sigreturn.S
 
 # Don't generate default code for these syscalls:
diff --git a/lib/libc/x86_64/sys/getcontext.S b/lib/libc/x86_64/sys/getcontext.S
new file mode 100644
index 0000000..ec2bd18
--- /dev/null
+++ b/lib/libc/x86_64/sys/getcontext.S
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
+ * 
+ * This code is derived from software contributed to The DragonFly Project
+ * by Matthew Dillon <dillon@backplane.com>
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * 
+ * $DragonFly: src/lib/libc/i386/sys/getcontext.S,v 1.2 2007/02/04 20:28:21 corecode Exp $
+ */
+
+#include <machine/asm.h>
+#include <asmcontext.h>
+
+	/*
+	 * This function is special-cased because the context it saves
+	 * includes a stale stack context (because it returns before the
+	 * caller presumably makes the call to setcontext()).  
+	 */
+	.weak	getcontext
+	.set	getcontext,_getcontext
+ENTRY(_getcontext)
+	/*
+	 * Retrieve the current signal mask and save it in &ucp->uc_sigmask.
+	 */
+	movq	%rdi,%r9		/* save ucontext_t in %r9 */
+	movq	%rdi,%rdx		/* ucontext_t pointer */
+	addq	$UC_SIGMASK,%rdx	/* pointer to signal mask */
+	movq	$0,%rsi
+	movl	$SIG_BLOCK,%edi
+	call    PIC_PLT(CNAME(_sigprocmask))
+	/*
+	 * Save what we need because our stack context is going stale.
+	 */
+	movq	%r9,%rdi
+	movq	(%rsp),%r8		/* save return PC in %rdx */
+	addq	$UC_MCONTEXT,%rdi
+	call	PIC_PLT(CNAME(get_mcontext))	/* returns non-zero on resume */
+	cmpq	$0,%rax
+	je	2f
+	/*
+	 * On resume, resave the stale return pc and restore the signal
+	 * mask (signals are blocked right now from the setcontext call).
+	 */
+	movq	%r8,(%rsp)		/* re-save the return PC */
+	movq	%r9,%rsi
+	addq	$UC_SIGMASK,%rsi
+        movq    $0,%rdx
+	movl	$SIG_SETMASK,%edi
+	movq	$0,%r8
+	call	PIC_PLT(CNAME(_sigprocmask))	/* retrieve & save signal mask */
+2:
+	movq	$0,%rax			/* return success */
+	ret
+
+1:
+	pushq	%rsi
+#ifdef PIC
+	movq	PIC_GOT(HIDENAME(cerror)),%rdx
+	jmp	*%rdx
+#else
+	jmp	HIDENAME(cerror)
+#endif
+END(_getcontext)
