libwine: Add unwind annotations to the stack switching functions for x86_64.
[wine.git] / libs / wine / port.c
blob63070693082e4915caf9fca653f8ac49a42ac8fe
1 /*
2 * Wine portability routines
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
28 #include "wine/library.h"
29 #include "wine/pthread.h"
31 static struct wine_pthread_functions pthread_functions;
33 /***********************************************************************
34 * wine_pthread_get_functions
36 void wine_pthread_get_functions( struct wine_pthread_functions *functions, size_t size )
38 memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
42 /***********************************************************************
43 * wine_pthread_set_functions
45 void wine_pthread_set_functions( const struct wine_pthread_functions *functions, size_t size )
47 memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
51 /***********************************************************************
52 * wine_switch_to_stack
54 * Switch to the specified stack and call the function.
56 #if defined(__sparc__) && defined(__GNUC__)
57 __ASM_GLOBAL_FUNC( wine_switch_to_stack,
58 "mov %o0, %l0\n\t" /* store first argument */
59 "mov %o1, %l1\n\t" /* store second argument */
60 "sub %o2, 96, %sp\n\t" /* store stack */
61 "call %l0, 0\n\t" /* call func */
62 "mov %l1, %o0\n\t" /* delay slot: arg for func */
63 "ta 0x01") /* breakpoint - we never get here */
64 #elif defined(__powerpc__) && defined(__APPLE__)
65 __ASM_GLOBAL_FUNC( wine_switch_to_stack,
66 "mtctr r3\n\t" /* func -> ctr */
67 "mr r3,r4\n\t" /* args -> function param 1 (r3) */
68 "mr r1,r5\n\t" /* stack */
69 "subi r1,r1,0x100\n\t" /* adjust stack pointer */
70 "bctrl\n" /* call ctr */
71 "1:\tb 1b") /* loop */
72 #elif defined(__ALPHA__) && defined(__GNUC__)
73 __ASM_GLOBAL_FUNC( wine_switch_to_stack,
74 "mov $16,$0\n\t" /* func */
75 "mov $17,$16\n\t" /* arg */
76 "mov $18,$30\n\t" /* stack */
77 "jsr $31,($0),0\n\t" /* call func */
78 "L1:\tbr $31,L1") /* loop */
79 #else
80 void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
82 wine_call_on_stack( (int (*)(void *))func, arg, stack );
83 abort();
85 #endif
88 /***********************************************************************
89 * wine_call_on_stack
91 * Switch to the specified stack to call the function and return.
94 #if defined(__i386__) && defined(__GNUC__)
95 __ASM_GLOBAL_FUNC( wine_call_on_stack,
96 "pushl %ebp\n\t"
97 "pushl %esi\n\t"
98 "movl 12(%esp),%ecx\n\t" /* func */
99 "movl 16(%esp),%edx\n\t" /* arg */
100 "movl 20(%esp),%esi\n\t" /* stack */
101 "andl $~15,%esi\n\t"
102 "subl $12,%esi\n\t"
103 "xchgl %esi,%esp\n\t"
104 "pushl %edx\n\t"
105 "xorl %ebp,%ebp\n\t"
106 "call *%ecx\n\t"
107 "movl %esi,%esp\n\t"
108 "popl %esi\n\t"
109 "popl %ebp\n\t"
110 "ret" )
111 #elif defined(__i386__) && defined(_MSC_VER)
112 __declspec(naked) int wine_call_on_stack( int (*func)(void *), void *arg, void *stack )
114 __asm push ebp;
115 __asm push esi;
116 __asm mov ecx, 12[esp];
117 __asm mov edx, 16[esp];
118 __asm mov esi, 20[esp];
119 __asm xchg esp, esi;
120 __asm push edx;
121 __asm xor ebp, ebp;
122 __asm call [ecx];
123 __asm mov esp, esi;
124 __asm pop esi;
125 __asm pop ebp
126 __asm ret;
128 #elif defined(__x86_64__) && defined(__GNUC__)
129 __ASM_GLOBAL_FUNC( wine_call_on_stack,
130 "pushq %rbp\n\t"
131 ".cfi_adjust_cfa_offset 8\n\t"
132 ".cfi_rel_offset %rbp,0\n\t"
133 "movq %rsp,%rbp\n\t"
134 ".cfi_def_cfa_register %rbp\n\t"
135 "movq %rdi,%rax\n\t" /* func */
136 "movq %rsi,%rdi\n\t" /* arg */
137 "andq $~15,%rdx\n\t" /* stack */
138 "movq %rdx,%rsp\n\t"
139 "callq *%rax\n\t" /* call func */
140 "movq %rbp,%rsp\n\t"
141 ".cfi_def_cfa_register %rsp\n\t"
142 "popq %rbp\n\t"
143 ".cfi_adjust_cfa_offset -8\n\t"
144 ".cfi_same_value %rbp\n\t"
145 "ret")
146 #elif defined(__powerpc__) && defined(__GNUC__)
147 __ASM_GLOBAL_FUNC( wine_call_on_stack,
148 "mflr 0\n\t" /* get return address */
149 "stw 0, 4(1)\n\t" /* save return address */
150 "subi 5, 5, 16\n\t" /* reserve space on new stack */
151 "stw 1, 12(5)\n\t" /* store old sp */
152 "mtctr 3\n\t" /* func -> ctr */
153 "mr 3,4\n\t" /* args -> function param 1 (r3) */
154 "mr 1,5\n\t" /* stack */
155 "li 0, 0\n\t" /* zero */
156 "stw 0, 0(1)\n\t" /* bottom of stack */
157 "stwu 1, -16(1)\n\t" /* create a frame for this function */
158 "bctrl\n\t" /* call ctr */
159 "lwz 1, 28(1)\n\t" /* fetch old sp */
160 "lwz 0, 4(1)\n\t" /* fetch return address */
161 "mtlr 0\n\t" /* return address -> lr */
162 "blr") /* return */
163 #else
164 #error You must implement wine_switch_to_stack for your platform
165 #endif