Release 1.5.29.
[wine] / libs / wine / port.c
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 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include "wine/library.h"
29
30 /* no longer used, for backwards compatibility only */
31 struct wine_pthread_functions;
32 static void *pthread_functions[8];
33
34 /***********************************************************************
35  *           wine_pthread_get_functions
36  */
37 void wine_pthread_get_functions( struct wine_pthread_functions *functions, size_t size )
38 {
39     memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
40 }
41
42
43 /***********************************************************************
44  *           wine_pthread_set_functions
45  */
46 void wine_pthread_set_functions( const struct wine_pthread_functions *functions, size_t size )
47 {
48     memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
49 }
50
51
52 /***********************************************************************
53  *           wine_switch_to_stack
54  *
55  * Switch to the specified stack and call the function.
56  */
57 void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
58 {
59     wine_call_on_stack( (int (*)(void *))func, arg, stack );
60     abort();
61 }
62
63
64 /***********************************************************************
65  *           wine_call_on_stack
66  *
67  * Switch to the specified stack to call the function and return.
68  */
69
70 #if defined(__i386__) && defined(__GNUC__)
71 __ASM_GLOBAL_FUNC( wine_call_on_stack,
72                    "pushl %ebp\n\t"
73                    __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
74                    __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
75                    "pushl %esi\n\t"
76                    __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
77                    __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
78                    "movl %esp,%esi\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 */
83                    "andl $~15,%eax\n\t"
84                    "subl $12,%eax\n\t"
85                    "movl %eax,%esp\n\t"
86                    "pushl %edx\n\t"
87                    "xorl %ebp,%ebp\n\t"
88                    "call *%ecx\n\t"
89                    "movl %esi,%esp\n\t"
90                    "popl %esi\n\t"
91                    __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
92                    __ASM_CFI(".cfi_same_value %esi\n\t")
93                    "popl %ebp\n\t"
94                    __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
95                    __ASM_CFI(".cfi_same_value %ebp\n\t")
96                    "ret" )
97 #elif defined(__i386__) && defined(_MSC_VER)
98 __declspec(naked) int wine_call_on_stack( int (*func)(void *), void *arg, void *stack )
99 {
100   __asm push ebp;
101   __asm push esi;
102   __asm mov ecx, 12[esp];
103   __asm mov edx, 16[esp];
104   __asm mov esi, 20[esp];
105   __asm xchg esp, esi;
106   __asm push edx;
107   __asm xor ebp, ebp;
108   __asm call [ecx];
109   __asm mov esp, esi;
110   __asm pop esi;
111   __asm pop ebp
112   __asm ret;
113 }
114 #elif defined(__x86_64__) && defined(__GNUC__)
115 __ASM_GLOBAL_FUNC( wine_call_on_stack,
116                    "pushq %rbp\n\t"
117                    __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
118                    __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
119                    "movq %rsp,%rbp\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 */
124                    "movq %rdx,%rsp\n\t"
125                    "callq *%rax\n\t"    /* call func */
126                    "movq %rbp,%rsp\n\t"
127                    __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
128                    "popq %rbp\n\t"
129                    __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
130                    __ASM_CFI(".cfi_same_value %rbp\n\t")
131                    "ret")
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 */
148                    "blr")               /* return */
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 */
171                    "ret")                            /* return */
172 #else
173 #error You must implement wine_call_on_stack for your platform
174 #endif