Index: kernel/arch/amd64/include/context_offset.h
===================================================================
--- kernel/arch/amd64/include/context_offset.h	(revision 1aede8263a0e694540ce87dab472d8abecb55698)
+++ kernel/arch/amd64/include/context_offset.h	(revision 1dbfe988113fb7d5276fcfd861f794b1971bb5bc)
@@ -38,5 +38,10 @@
 #define OFFSET_R14 0x30
 #define OFFSET_R15 0x38
-#define OFFSET_IPL 0x40
+
+#ifdef KERNEL
+# define OFFSET_IPL 0x40
+#else
+# define OFFSET_TLS 0x40
+#endif
 
 #ifdef __ASM__
@@ -45,8 +50,27 @@
 # pc: return address
 .macro CONTEXT_SAVE_ARCH_CORE ctx:req pc:req
+	movq \pc, OFFSET_PC(\ctx)
+	movq %rsp, OFFSET_SP(\ctx)
+	
+	movq %rbx, OFFSET_RBX(\ctx)
+	movq %rbp, OFFSET_RBP(\ctx)
+	movq %r12, OFFSET_R12(\ctx)
+	movq %r13, OFFSET_R13(\ctx)
+	movq %r14, OFFSET_R14(\ctx)
+	movq %r15, OFFSET_R15(\ctx)
 .endm
 
 # ctx: address of the structure with saved context 
 .macro CONTEXT_RESTORE_ARCH_CORE ctx:req pc:req
+	movq OFFSET_R15(\ctx), %r15
+	movq OFFSET_R14(\ctx), %r14
+	movq OFFSET_R13(\ctx), %r13
+	movq OFFSET_R12(\ctx), %r12
+	movq OFFSET_RBP(\ctx), %rbp
+	movq OFFSET_RBX(\ctx), %rbx	
+	
+	movq OFFSET_SP(\ctx), %rsp   # ctx->sp -> %rsp
+	
+	movq OFFSET_PC(\ctx), \pc
 .endm
 
Index: kernel/arch/amd64/src/context.S
===================================================================
--- kernel/arch/amd64/src/context.S	(revision 1aede8263a0e694540ce87dab472d8abecb55698)
+++ kernel/arch/amd64/src/context.S	(revision 1dbfe988113fb7d5276fcfd861f794b1971bb5bc)
@@ -41,14 +41,7 @@
 context_save_arch:
 	movq (%rsp), %rdx     # the caller's return %eip
+
 	# In %edi is passed 1st argument
-	movq %rdx, OFFSET_PC(%rdi)
-	movq %rsp, OFFSET_SP(%rdi)
-	
-	movq %rbx, OFFSET_RBX(%rdi)
-	movq %rbp, OFFSET_RBP(%rdi)
-	movq %r12, OFFSET_R12(%rdi)
-	movq %r13, OFFSET_R13(%rdi)
-	movq %r14, OFFSET_R14(%rdi)
-	movq %r15, OFFSET_R15(%rdi)
+	CONTEXT_SAVE_ARCH_CORE %rdi %rdx 
 	
 	xorq %rax,%rax		# context_save returns 1
@@ -63,14 +56,7 @@
 #
 context_restore_arch:	
-	movq OFFSET_R15(%rdi), %r15
-	movq OFFSET_R14(%rdi), %r14
-	movq OFFSET_R13(%rdi), %r13
-	movq OFFSET_R12(%rdi), %r12
-	movq OFFSET_RBP(%rdi), %rbp
-	movq OFFSET_RBX(%rdi), %rbx	
-	
-	movq OFFSET_SP(%rdi), %rsp   # ctx->sp -> %rsp
-	
-	movq OFFSET_PC(%rdi), %rdx
+
+	CONTEXT_RESTORE_ARCH_CORE %rdi %rdx
+
 	movq %rdx,(%rsp)
 
