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
22 #include "wine/port.h"
26 #include <sys/types.h>
28 #include "wine/library.h"
30 /* no longer used, for backwards compatibility only */
31 struct wine_pthread_functions
;
32 static void *pthread_functions
[8];
34 /***********************************************************************
35 * wine_pthread_get_functions
37 void wine_pthread_get_functions( struct wine_pthread_functions
*functions
, size_t size
)
39 memcpy( functions
, &pthread_functions
, min( size
, sizeof(pthread_functions
) ));
43 /***********************************************************************
44 * wine_pthread_set_functions
46 void wine_pthread_set_functions( const struct wine_pthread_functions
*functions
, size_t size
)
48 memcpy( &pthread_functions
, functions
, min( size
, sizeof(pthread_functions
) ));
52 /***********************************************************************
53 * wine_switch_to_stack
55 * Switch to the specified stack and call the function.
57 void DECLSPEC_NORETURN
wine_switch_to_stack( void (*func
)(void *), void *arg
, void *stack
)
59 wine_call_on_stack( (int (*)(void *))func
, arg
, stack
);
64 /***********************************************************************
67 * Switch to the specified stack to call the function and return.
70 #if defined(__i386__) && defined(__GNUC__)
71 __ASM_GLOBAL_FUNC( wine_call_on_stack
,
73 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
74 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
76 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
77 __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
79 __ASM_CFI(".cfi_def_cfa_register %esi\n\t")
80 "movl 12(%esp),%ecx\n\t" /* func */
81 "movl 16(%esp),%edx\n\t" /* arg */
82 "movl 20(%esp),%eax\n\t" /* stack */
91 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
92 __ASM_CFI(".cfi_same_value %esi\n\t")
94 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
95 __ASM_CFI(".cfi_same_value %ebp\n\t")
97 #elif defined(__i386__) && defined(_MSC_VER)
98 __declspec(naked
) int wine_call_on_stack( int (*func
)(void *), void *arg
, void *stack
)
102 __asm mov ecx
, 12[esp
];
103 __asm mov edx
, 16[esp
];
104 __asm mov esi
, 20[esp
];
114 #elif defined(__x86_64__) && defined(__GNUC__)
115 __ASM_GLOBAL_FUNC( wine_call_on_stack
,
117 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
118 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
120 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
121 "movq %rdi,%rax\n\t" /* func */
122 "movq %rsi,%rdi\n\t" /* arg */
123 "andq $~15,%rdx\n\t" /* stack */
125 "callq *%rax\n\t" /* call func */
127 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
129 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
130 __ASM_CFI(".cfi_same_value %rbp\n\t")
132 #elif defined(__powerpc__) && defined(__GNUC__)
133 __ASM_GLOBAL_FUNC( wine_call_on_stack
,
134 "mflr 0\n\t" /* get return address */
135 "stw 0, 4(1)\n\t" /* save return address */
136 "subi 5, 5, 16\n\t" /* reserve space on new stack */
137 "stw 1, 12(5)\n\t" /* store old sp */
138 "mtctr 3\n\t" /* func -> ctr */
139 "mr 3,4\n\t" /* args -> function param 1 (r3) */
140 "mr 1,5\n\t" /* stack */
141 "li 0, 0\n\t" /* zero */
142 "stw 0, 0(1)\n\t" /* bottom of stack */
143 "stwu 1, -16(1)\n\t" /* create a frame for this function */
144 "bctrl\n\t" /* call ctr */
145 "lwz 1, 28(1)\n\t" /* fetch old sp */
146 "lwz 0, 4(1)\n\t" /* fetch return address */
147 "mtlr 0\n\t" /* return address -> lr */
149 #elif defined(__arm__) && defined(__GNUC__)
150 __ASM_GLOBAL_FUNC( wine_call_on_stack
,
151 "push {r4,LR}\n\t" /* save return address on stack */
152 "mov r4, sp\n\t" /* store old sp in local var */
153 "mov sp, r2\n\t" /* stack */
154 "mov r2, r0\n\t" /* func -> scratch register */
155 "mov r0, r1\n\t" /* arg */
156 "blx r2\n\t" /* call func */
157 "mov sp, r4\n\t" /* restore old sp from local var */
158 "pop {r4,PC}") /* fetch return address into pc */
159 #elif defined(__aarch64__) && defined(__GNUC__)
160 __ASM_GLOBAL_FUNC( wine_call_on_stack
,
161 "stp x29, x30, [sp,#-32]!\n\t" /* save return address on stack */
162 "str x19, [sp,#16]\n\t" /* save register on stack */
163 "mov x19, sp\n\t" /* store old sp in local var */
164 "mov sp, x2\n\t" /* stack */
165 "mov x2, x0\n\t" /* func -> scratch register */
166 "mov x0, x1\n\t" /* arg */
167 "blr x2\n\t" /* call func */
168 "mov sp, x19\n\t" /* restore old sp from local var */
169 "ldr x19, [sp,#16]\n\t" /* restore register from stack */
170 "ldp x29, x30, [sp],#32\n\t" /* restore return address */
172 #elif defined(__sparc__) && defined(__GNUC__)
173 __ASM_GLOBAL_FUNC( wine_call_on_stack
,
174 "save %sp, -96, %sp\n\t" /* push: change register window */
175 "mov %sp, %l2\n\t" /* store old sp in local var */
176 "mov %i0, %l0\n\t" /* func */
177 "mov %i1, %l1\n\t" /* arg */
178 "sub %i2, 96, %sp\n\t" /* stack */
179 "call %l0, 0\n\t" /* call func */
180 "mov %l1, %o0\n\t" /* delay slot: arg for func */
181 "mov %l2, %sp\n\t" /* restore old sp from local var */
182 "mov %o0, %i0\n\t" /* move return value to right register window */
183 "ret\n\t" /* return */
184 "restore\n\t") /* delay slot: pop */
186 #error You must implement wine_call_on_stack for your platform