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 | * Oracle 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, Oracle 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 |
|
---|
39 | /* no longer used, for backwards compatibility only */
|
---|
40 | struct wine_pthread_functions;
|
---|
41 | static void *pthread_functions[8];
|
---|
42 |
|
---|
43 | /***********************************************************************
|
---|
44 | * wine_pthread_get_functions
|
---|
45 | */
|
---|
46 | void wine_pthread_get_functions( struct wine_pthread_functions *functions, size_t size )
|
---|
47 | {
|
---|
48 | memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | /***********************************************************************
|
---|
53 | * wine_pthread_set_functions
|
---|
54 | */
|
---|
55 | void wine_pthread_set_functions( const struct wine_pthread_functions *functions, size_t size )
|
---|
56 | {
|
---|
57 | memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /***********************************************************************
|
---|
62 | * wine_switch_to_stack
|
---|
63 | *
|
---|
64 | * Switch to the specified stack and call the function.
|
---|
65 | */
|
---|
66 | #if defined(__sparc__) && defined(__GNUC__)
|
---|
67 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
68 | "mov %o0, %l0\n\t" /* store first argument */
|
---|
69 | "mov %o1, %l1\n\t" /* store second argument */
|
---|
70 | "sub %o2, 96, %sp\n\t" /* store stack */
|
---|
71 | "call %l0, 0\n\t" /* call func */
|
---|
72 | "mov %l1, %o0\n\t" /* delay slot: arg for func */
|
---|
73 | "ta 0x01") /* breakpoint - we never get here */
|
---|
74 | #elif defined(__powerpc__) && defined(__APPLE__)
|
---|
75 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
76 | "mtctr r3\n\t" /* func -> ctr */
|
---|
77 | "mr r3,r4\n\t" /* args -> function param 1 (r3) */
|
---|
78 | "mr r1,r5\n\t" /* stack */
|
---|
79 | "subi r1,r1,0x100\n\t" /* adjust stack pointer */
|
---|
80 | "bctrl\n" /* call ctr */
|
---|
81 | "1:\tb 1b") /* loop */
|
---|
82 | #elif defined(__ALPHA__) && defined(__GNUC__)
|
---|
83 | __ASM_GLOBAL_FUNC( wine_switch_to_stack,
|
---|
84 | "mov $16,$0\n\t" /* func */
|
---|
85 | "mov $17,$16\n\t" /* arg */
|
---|
86 | "mov $18,$30\n\t" /* stack */
|
---|
87 | "jsr $31,($0),0\n\t" /* call func */
|
---|
88 | "L1:\tbr $31,L1") /* loop */
|
---|
89 | #else
|
---|
90 | void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
|
---|
91 | {
|
---|
92 | wine_call_on_stack( (int (*)(void *))func, arg, stack );
|
---|
93 | abort();
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 |
|
---|
98 | /***********************************************************************
|
---|
99 | * wine_call_on_stack
|
---|
100 | *
|
---|
101 | * Switch to the specified stack to call the function and return.
|
---|
102 | */
|
---|
103 |
|
---|
104 | #if defined(__i386__) && defined(__GNUC__)
|
---|
105 | __ASM_GLOBAL_FUNC( wine_call_on_stack,
|
---|
106 | "pushl %ebp\n\t"
|
---|
107 | __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
|
---|
108 | __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
|
---|
109 | "pushl %esi\n\t"
|
---|
110 | __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
|
---|
111 | __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
|
---|
112 | "movl %esp,%esi\n\t"
|
---|
113 | __ASM_CFI(".cfi_def_cfa_register %esi\n\t")
|
---|
114 | "movl 12(%esp),%ecx\n\t" /* func */
|
---|
115 | "movl 16(%esp),%edx\n\t" /* arg */
|
---|
116 | "movl 20(%esp),%eax\n\t" /* stack */
|
---|
117 | "andl $~15,%eax\n\t"
|
---|
118 | "subl $12,%eax\n\t"
|
---|
119 | "movl %eax,%esp\n\t"
|
---|
120 | "pushl %edx\n\t"
|
---|
121 | "xorl %ebp,%ebp\n\t"
|
---|
122 | "call *%ecx\n\t"
|
---|
123 | "movl %esi,%esp\n\t"
|
---|
124 | "popl %esi\n\t"
|
---|
125 | __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
|
---|
126 | __ASM_CFI(".cfi_same_value %esi\n\t")
|
---|
127 | "popl %ebp\n\t"
|
---|
128 | __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
|
---|
129 | __ASM_CFI(".cfi_same_value %ebp\n\t")
|
---|
130 | "ret" )
|
---|
131 | #elif defined(__i386__) && defined(_MSC_VER)
|
---|
132 | __declspec(naked) int wine_call_on_stack( int (*func)(void *), void *arg, void *stack )
|
---|
133 | {
|
---|
134 | __asm push ebp;
|
---|
135 | __asm push esi;
|
---|
136 | __asm mov ecx, 12[esp];
|
---|
137 | __asm mov edx, 16[esp];
|
---|
138 | __asm mov esi, 20[esp];
|
---|
139 | __asm xchg esp, esi;
|
---|
140 | __asm push edx;
|
---|
141 | __asm xor ebp, ebp;
|
---|
142 | __asm call [ecx];
|
---|
143 | __asm mov esp, esi;
|
---|
144 | __asm pop esi;
|
---|
145 | __asm pop ebp
|
---|
146 | __asm ret;
|
---|
147 | }
|
---|
148 | #elif defined(__x86_64__) && defined(__GNUC__)
|
---|
149 | __ASM_GLOBAL_FUNC( wine_call_on_stack,
|
---|
150 | "pushq %rbp\n\t"
|
---|
151 | __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
|
---|
152 | __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
|
---|
153 | "movq %rsp,%rbp\n\t"
|
---|
154 | __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
|
---|
155 | "movq %rdi,%rax\n\t" /* func */
|
---|
156 | "movq %rsi,%rdi\n\t" /* arg */
|
---|
157 | "andq $~15,%rdx\n\t" /* stack */
|
---|
158 | "movq %rdx,%rsp\n\t"
|
---|
159 | "callq *%rax\n\t" /* call func */
|
---|
160 | "movq %rbp,%rsp\n\t"
|
---|
161 | __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
|
---|
162 | "popq %rbp\n\t"
|
---|
163 | __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
|
---|
164 | __ASM_CFI(".cfi_same_value %rbp\n\t")
|
---|
165 | "ret")
|
---|
166 | #elif defined(__powerpc__) && defined(__GNUC__)
|
---|
167 | __ASM_GLOBAL_FUNC( wine_call_on_stack,
|
---|
168 | "mflr 0\n\t" /* get return address */
|
---|
169 | "stw 0, 4(1)\n\t" /* save return address */
|
---|
170 | "subi 5, 5, 16\n\t" /* reserve space on new stack */
|
---|
171 | "stw 1, 12(5)\n\t" /* store old sp */
|
---|
172 | "mtctr 3\n\t" /* func -> ctr */
|
---|
173 | "mr 3,4\n\t" /* args -> function param 1 (r3) */
|
---|
174 | "mr 1,5\n\t" /* stack */
|
---|
175 | "li 0, 0\n\t" /* zero */
|
---|
176 | "stw 0, 0(1)\n\t" /* bottom of stack */
|
---|
177 | "stwu 1, -16(1)\n\t" /* create a frame for this function */
|
---|
178 | "bctrl\n\t" /* call ctr */
|
---|
179 | "lwz 1, 28(1)\n\t" /* fetch old sp */
|
---|
180 | "lwz 0, 4(1)\n\t" /* fetch return address */
|
---|
181 | "mtlr 0\n\t" /* return address -> lr */
|
---|
182 | "blr") /* return */
|
---|
183 | #elif defined(__arm__)
|
---|
184 | __ASM_GLOBAL_FUNC( wine_call_on_stack,
|
---|
185 | "str r14, [r13, #4]\n\t" /* save return address on stack */
|
---|
186 | "sub r2, r2, #16\n\t" /* reserve space on new stack */
|
---|
187 | "str r13, [r2, #12]\n\t" /* store old sp */
|
---|
188 | "mov r3, r0\n\t" /* func */
|
---|
189 | "mov r0, r1\n\t" /* arg */
|
---|
190 | "mov r13, r2\n\t" /* stack */
|
---|
191 | "mov r2, #0\n\t" /* zero */
|
---|
192 | "str r2, [r13]\n\t" /* bottom of stack */
|
---|
193 | "sub r13, r13, #16\n\t" /* create a frame for this function */
|
---|
194 | "blx r3\n\t" /* call func */
|
---|
195 | "ldr r13, [r13, #28]\n\t" /* fetch old sp */
|
---|
196 | "ldr r15, [r13, #4]") /* fetch return address and return */
|
---|
197 | #else
|
---|
198 | #error You must implement wine_switch_to_stack for your platform
|
---|
199 | #endif
|
---|