Subject: swapcontext: preserve r12 & r13
To: None <port-amd64@netbsd.org>
From: Antti Kantee <pooka@cs.hut.fi>
List: port-amd64
Date: 04/05/2007 19:36:09
Hello,

puffs was reported to not work on amd64 if libpuffs was compiled without
optimization flags.  I tracked this down to r12 getting clobbered when
doing a swapcontext() dance and memory accesses based on that register
being a bit off afterwards.  I managed to make it work, but could someone
eye the patch over, as I have zero knowledge of x86.

Please cc replies, as I'm not on the list.

Index: swapcontext.S
===================================================================
RCS file: /cvsroot/src/lib/libc/arch/x86_64/gen/swapcontext.S,v
retrieving revision 1.3
diff -u -r1.3 swapcontext.S
--- swapcontext.S       1 Dec 2004 01:08:18 -0000       1.3
+++ swapcontext.S       5 Apr 2007 16:25:05 -0000
@@ -48,28 +48,26 @@
  */
 
 ENTRY(swapcontext)
-       pushq   %r12
-       pushq   %r13
-       movq    %rdi,%r12               /* preserve oucp */
-       movq    %rsi,%r13               /* preserve ucp */
+       pushq   %rdi                    /* preserve oucp */
+       pushq   %rsi                    /* preserve ucp */
 #ifdef PIC
        call    PIC_PLT(_C_LABEL(_getcontext))
 #else
        call    _C_LABEL(_getcontext)
 #endif
+       popq    %rsi
+       popq    %rdi
        testl   %eax,%eax
        jnz     2f
-       movq    16(%rsp),%r11
-       movq    %r11,(56 + 21 * 8)(%r12)
-       leaq    24(%rsp),%r11
-       movq    %r11,(56 + 24 * 8)(%r12)
-       movq    %r13,%rdi
+       movq    0(%rsp),%r11
+       movq    %r11,(56 + 21 * 8)(%rdi)
+       leaq    8(%rsp),%r11
+       movq    %r11,(56 + 24 * 8)(%rdi)
+       movq    %rsi,%rdi
 #ifdef PIC
        call    PIC_PLT(_C_LABEL(setcontext))
 #else
        call    _C_LABEL(setcontext)
 #endif
 2:
-       popq    %r13
-       popq    %r12
        ret

-- 
Antti Kantee <pooka@iki.fi>                     Of course he runs NetBSD
http://www.iki.fi/pooka/                          http://www.NetBSD.org/
    "la qualité la plus indispensable du cuisinier est l'exactitude"