1 | /*
|
---|
2 | * Wine portability routines
|
---|
3 | *
|
---|
4 | * Copyright 2000 Alexandre Julliard
|
---|
5 | *
|
---|
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.
|
---|
10 | *
|
---|
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.
|
---|
15 | *
|
---|
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
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
24 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
25 | * a choice of LGPL license versions is made available with the language indicating
|
---|
26 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the LGPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include "config.h"
|
---|
31 | #include "wine/port.h"
|
---|
32 |
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <string.h>
|
---|
35 | #include <sys/types.h>
|
---|
36 |
|
---|
37 | #include "wine/library.h"
|
---|
38 | #include "wine/pthread.h"
|
---|
39 |
|
---|
40 | static struct wine_pthread_functions pthread_functions;
|
---|
41 |
|
---|
42 | /***********************************************************************
|
---|
43 | * wine_pthread_get_functions
|
---|
44 | */
|
---|
45 | void wine_pthread_get_functions( struct wine_pthread_functions *functions, size_t size )
|
---|
46 | {
|
---|
47 | memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | /***********************************************************************
|
---|
52 | * wine_pthread_set_functions
|
---|
53 | */
|
---|
54 | void wine_pthread_set_functions( const struct wine_pthread_functions *functions, size_t size )
|
---|
55 | {
|
---|
56 | memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | /***********************************************************************
|
---|
61 | * wine_switch_to_stack
|
---|
62 | *
|
---|
63 | * Switch to the specified stack and call the function.
|
---|
64 | */
|
---|
65 | void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack );
|
---|
66 | #if defined(__i386__) && defined(__GNUC__)
|
---|
67 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
68 | "movl 4(%esp),%ecx\n\t" /* func */
|
---|
69 | "movl 8(%esp),%edx\n\t" /* arg */
|
---|
70 | "movl 12(%esp),%esp\n\t" /* stack */
|
---|
71 | "andl $~15,%esp\n\t"
|
---|
72 | "subl $12,%esp\n\t"
|
---|
73 | "pushl %edx\n\t"
|
---|
74 | "xorl %ebp,%ebp\n\t"
|
---|
75 | "call *%ecx\n\t"
|
---|
76 | "int $3" /* we never return here */ )
|
---|
77 | #elif defined(__i386__) && defined(_MSC_VER)
|
---|
78 | __declspec(naked) void wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
|
---|
79 | {
|
---|
80 | __asm mov ecx, 4[esp];
|
---|
81 | __asm mov edx, 8[esp];
|
---|
82 | __asm mov esp, 12[esp];
|
---|
83 | __asm push edx;
|
---|
84 | __asm xor ebp, ebp;
|
---|
85 | __asm call [ecx];
|
---|
86 | __asm int 3;
|
---|
87 | }
|
---|
88 | #elif defined(__sparc__) && defined(__GNUC__)
|
---|
89 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
90 | "mov %o0, %l0\n\t" /* store first argument */
|
---|
91 | "mov %o1, %l1\n\t" /* store second argument */
|
---|
92 | "sub %o2, 96, %sp\n\t" /* store stack */
|
---|
93 | "call %l0, 0\n\t" /* call func */
|
---|
94 | "mov %l1, %o0\n\t" /* delay slot: arg for func */
|
---|
95 | "ta 0x01") /* breakpoint - we never get here */
|
---|
96 | #elif defined(__powerpc__) && defined(__APPLE__)
|
---|
97 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
98 | "mtctr r3\n\t" /* func -> ctr */
|
---|
99 | "mr r3,r4\n\t" /* args -> function param 1 (r3) */
|
---|
100 | "mr r1,r5\n\t" /* stack */
|
---|
101 | "subi r1,r1,0x100\n\t" /* adjust stack pointer */
|
---|
102 | "bctrl\n" /* call ctr */
|
---|
103 | "1:\tb 1b") /* loop */
|
---|
104 | #elif defined(__powerpc__) && defined(__GNUC__)
|
---|
105 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
106 | "mtctr 3\n\t" /* func -> ctr */
|
---|
107 | "mr 3,4\n\t" /* args -> function param 1 (r3) */
|
---|
108 | "mr 1,5\n\t" /* stack */
|
---|
109 | "subi 1, 1, 16\n\t" /* allocate space for the callee */
|
---|
110 | "li 0, 0\n\t" /* load zero */
|
---|
111 | "stw 0, 0(1)\n\t" /* create a bottom stack frame */
|
---|
112 | "bctrl\n\t" /* call ctr */
|
---|
113 | "1:\tb 1b") /* loop */
|
---|
114 | #elif defined(__ALPHA__) && defined(__GNUC__)
|
---|
115 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
116 | "mov $16,$0\n\t" /* func */
|
---|
117 | "mov $17,$16\n\t" /* arg */
|
---|
118 | "mov $18,$30\n\t" /* stack */
|
---|
119 | "jsr $31,($0),0\n\t" /* call func */
|
---|
120 | "L1:\tbr $31,L1") /* loop */
|
---|
121 | #elif defined(__x86_64__) && defined(__GNUC__)
|
---|
122 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
123 | "movq %rdi,%rax\n\t" /* func */
|
---|
124 | "movq %rsi,%rdi\n\t" /* arg */
|
---|
125 | "andq $~15,%rdx\n\t" /* stack */
|
---|
126 | "movq %rdx,%rsp\n\t"
|
---|
127 | "xorq %rbp,%rbp\n\t"
|
---|
128 | "callq *%rax\n\t" /* call func */
|
---|
129 | "int $3")
|
---|
130 | #else
|
---|
131 | void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
|
---|
132 | {
|
---|
133 | wine_call_on_stack( (int (*)(void *))func, arg, stack );
|
---|
134 | abort();
|
---|
135 | }
|
---|
136 | #endif
|
---|
137 |
|
---|
138 |
|
---|
139 | /***********************************************************************
|
---|
140 | * wine_call_on_stack
|
---|
141 | *
|
---|
142 | * Switch to the specified stack to call the function and return.
|
---|
143 | */
|
---|
144 | int wine_call_on_stack( int (*func)(void *), void *arg, void *stack );
|
---|
145 | #if defined(__i386__) && defined(__GNUC__)
|
---|
146 | __ASM_GLOBAL_FUNC( wine_call_on_stack,
|
---|
147 | "pushl %ebp\n\t"
|
---|
148 | "pushl %esi\n\t"
|
---|
149 | "movl 12(%esp),%ecx\n\t" /* func */
|
---|
150 | "movl 16(%esp),%edx\n\t" /* arg */
|
---|
151 | "movl 20(%esp),%esi\n\t" /* stack */
|
---|
152 | "andl $~15,%esi\n\t"
|
---|
153 | "subl $12,%esi\n\t"
|
---|
154 | "xchgl %esi,%esp\n\t"
|
---|
155 | "pushl %edx\n\t"
|
---|
156 | "xorl %ebp,%ebp\n\t"
|
---|
157 | "call *%ecx\n\t"
|
---|
158 | "movl %esi,%esp\n\t"
|
---|
159 | "popl %esi\n\t"
|
---|
160 | "popl %ebp\n\t"
|
---|
161 | "ret" )
|
---|
162 | #elif defined(__i386__) && defined(_MSC_VER)
|
---|
163 | __declspec(naked) int wine_call_on_stack( int (*func)(void *), void *arg, void *stack )
|
---|
164 | {
|
---|
165 | __asm push ebp;
|
---|
166 | __asm push esi;
|
---|
167 | __asm mov ecx, 12[esp];
|
---|
168 | __asm mov edx, 16[esp];
|
---|
169 | __asm mov esi, 20[esp];
|
---|
170 | __asm xchg esp, esi;
|
---|
171 | __asm push edx;
|
---|
172 | __asm xor ebp, ebp;
|
---|
173 | __asm call [ecx];
|
---|
174 | __asm mov esp, esi;
|
---|
175 | __asm pop esi;
|
---|
176 | __asm pop ebp
|
---|
177 | __asm ret;
|
---|
178 | }
|
---|
179 | #elif defined(__x86_64__) && defined(__GNUC__)
|
---|
180 | __ASM_GLOBAL_FUNC( wine_call_on_stack,
|
---|
181 | "pushq %rbp\n\t"
|
---|
182 | "pushq %rbx\n\t"
|
---|
183 | "movq %rsp,%rbx\n\t"
|
---|
184 | "movq %rdi,%rax\n\t" /* func */
|
---|
185 | "movq %rsi,%rdi\n\t" /* arg */
|
---|
186 | "andq $~15,%rdx\n\t" /* stack */
|
---|
187 | "movq %rdx,%rsp\n\t"
|
---|
188 | "xorq %rbp,%rbp\n\t"
|
---|
189 | "callq *%rax\n\t" /* call func */
|
---|
190 | "movq %rbx,%rsp\n\t"
|
---|
191 | "popq %rbx\n\t"
|
---|
192 | "popq %rbp\n\t"
|
---|
193 | "ret")
|
---|
194 | #elif defined(__powerpc__) && defined(__GNUC__)
|
---|
195 | __ASM_GLOBAL_FUNC( wine_call_on_stack,
|
---|
196 | "mflr 0\n\t" /* get return address */
|
---|
197 | "stw 0, 4(1)\n\t" /* save return address */
|
---|
198 | "subi 5, 5, 16\n\t" /* reserve space on new stack */
|
---|
199 | "stw 1, 12(5)\n\t" /* store old sp */
|
---|
200 | "mtctr 3\n\t" /* func -> ctr */
|
---|
201 | "mr 3,4\n\t" /* args -> function param 1 (r3) */
|
---|
202 | "mr 1,5\n\t" /* stack */
|
---|
203 | "li 0, 0\n\t" /* zero */
|
---|
204 | "stw 0, 0(1)\n\t" /* bottom of stack */
|
---|
205 | "stwu 1, -16(1)\n\t" /* create a frame for this function */
|
---|
206 | "bctrl\n\t" /* call ctr */
|
---|
207 | "lwz 1, 28(1)\n\t" /* fetch old sp */
|
---|
208 | "lwz 0, 4(1)\n\t" /* fetch return address */
|
---|
209 | "mtlr 0\n\t" /* return address -> lr */
|
---|
210 | "blr") /* return */
|
---|
211 | #else
|
---|
212 | #error You must implement wine_switch_to_stack for your platform
|
---|
213 | #endif
|
---|