port.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include "wine/asm.h"
  23. #ifdef __ASM_OBSOLETE
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #define WINE_UNICODE_INLINE /* nothing */
  28. #include "wine/unicode.h"
  29. #include "wine/library.h"
  30. /* functions from libwine_port that are also exported from libwine for backwards compatibility,
  31. * on platforms that require it */
  32. const void *libwine_port_functions[] =
  33. {
  34. strtolW,
  35. vsnprintfW,
  36. };
  37. /* no longer used, for backwards compatibility only */
  38. struct wine_pthread_functions;
  39. static void *pthread_functions[8];
  40. /***********************************************************************
  41. * wine_pthread_get_functions
  42. */
  43. void wine_pthread_get_functions_obsolete( struct wine_pthread_functions *functions, size_t size )
  44. {
  45. memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
  46. }
  47. /***********************************************************************
  48. * wine_pthread_set_functions
  49. */
  50. void wine_pthread_set_functions_obsolete( const struct wine_pthread_functions *functions, size_t size )
  51. {
  52. memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
  53. }
  54. /***********************************************************************
  55. * wine_call_on_stack
  56. *
  57. * Switch to the specified stack to call the function and return.
  58. */
  59. extern int wine_call_on_stack_obsolete( int (*func)(void *), void *arg, void *stack );
  60. #if defined(__i386__) && defined(__GNUC__)
  61. __ASM_GLOBAL_FUNC( wine_call_on_stack_obsolete,
  62. "pushl %ebp\n\t"
  63. __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
  64. __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
  65. "pushl %esi\n\t"
  66. __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
  67. __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
  68. "movl %esp,%esi\n\t"
  69. __ASM_CFI(".cfi_def_cfa_register %esi\n\t")
  70. "movl 12(%esp),%ecx\n\t" /* func */
  71. "movl 16(%esp),%edx\n\t" /* arg */
  72. "movl 20(%esp),%eax\n\t" /* stack */
  73. "andl $~15,%eax\n\t"
  74. "subl $12,%eax\n\t"
  75. "movl %eax,%esp\n\t"
  76. "pushl %edx\n\t"
  77. "xorl %ebp,%ebp\n\t"
  78. "call *%ecx\n\t"
  79. "movl %esi,%esp\n\t"
  80. "popl %esi\n\t"
  81. __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
  82. __ASM_CFI(".cfi_same_value %esi\n\t")
  83. "popl %ebp\n\t"
  84. __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
  85. __ASM_CFI(".cfi_same_value %ebp\n\t")
  86. "ret" )
  87. #elif defined(__x86_64__) && defined(__GNUC__)
  88. __ASM_GLOBAL_FUNC( wine_call_on_stack_obsolete,
  89. "pushq %rbp\n\t"
  90. __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
  91. __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
  92. "movq %rsp,%rbp\n\t"
  93. __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
  94. "movq %rdi,%rax\n\t" /* func */
  95. "movq %rsi,%rdi\n\t" /* arg */
  96. "andq $~15,%rdx\n\t" /* stack */
  97. "movq %rdx,%rsp\n\t"
  98. "callq *%rax\n\t" /* call func */
  99. "movq %rbp,%rsp\n\t"
  100. __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
  101. "popq %rbp\n\t"
  102. __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
  103. __ASM_CFI(".cfi_same_value %rbp\n\t")
  104. "ret")
  105. #elif defined(__powerpc__) && defined(__GNUC__)
  106. __ASM_GLOBAL_FUNC( wine_call_on_stack_obsolete,
  107. "mflr 0\n\t" /* get return address */
  108. "stw 0, 4(1)\n\t" /* save return address */
  109. "subi 5, 5, 16\n\t" /* reserve space on new stack */
  110. "stw 1, 12(5)\n\t" /* store old sp */
  111. "mtctr 3\n\t" /* func -> ctr */
  112. "mr 3,4\n\t" /* args -> function param 1 (r3) */
  113. "mr 1,5\n\t" /* stack */
  114. "li 0, 0\n\t" /* zero */
  115. "stw 0, 0(1)\n\t" /* bottom of stack */
  116. "stwu 1, -16(1)\n\t" /* create a frame for this function */
  117. "bctrl\n\t" /* call ctr */
  118. "lwz 1, 28(1)\n\t" /* fetch old sp */
  119. "lwz 0, 4(1)\n\t" /* fetch return address */
  120. "mtlr 0\n\t" /* return address -> lr */
  121. "blr") /* return */
  122. #elif defined(__arm__) && defined(__GNUC__)
  123. __ASM_GLOBAL_FUNC( wine_call_on_stack_obsolete,
  124. "push {r4,LR}\n\t" /* save return address on stack */
  125. "mov r4, sp\n\t" /* store old sp in local var */
  126. "mov sp, r2\n\t" /* stack */
  127. "mov r2, r0\n\t" /* func -> scratch register */
  128. "mov r0, r1\n\t" /* arg */
  129. "blx r2\n\t" /* call func */
  130. "mov sp, r4\n\t" /* restore old sp from local var */
  131. "pop {r4,PC}") /* fetch return address into pc */
  132. #elif defined(__aarch64__) && defined(__GNUC__)
  133. __ASM_GLOBAL_FUNC( wine_call_on_stack_obsolete,
  134. "stp x29, x30, [sp,#-32]!\n\t" /* save return address on stack */
  135. "str x19, [sp,#16]\n\t" /* save register on stack */
  136. "mov x19, sp\n\t" /* store old sp in local var */
  137. "mov sp, x2\n\t" /* stack */
  138. "mov x2, x0\n\t" /* func -> scratch register */
  139. "mov x0, x1\n\t" /* arg */
  140. "blr x2\n\t" /* call func */
  141. "mov sp, x19\n\t" /* restore old sp from local var */
  142. "ldr x19, [sp,#16]\n\t" /* restore register from stack */
  143. "ldp x29, x30, [sp],#32\n\t" /* restore return address */
  144. "ret") /* return */
  145. #endif
  146. /***********************************************************************
  147. * wine_switch_to_stack
  148. *
  149. * Switch to the specified stack and call the function.
  150. */
  151. void DECLSPEC_NORETURN wine_switch_to_stack_obsolete( void (*func)(void *), void *arg, void *stack )
  152. {
  153. wine_call_on_stack_obsolete( (int (*)(void *))func, arg, stack );
  154. abort();
  155. }
  156. __ASM_OBSOLETE(wine_pthread_get_functions);
  157. __ASM_OBSOLETE(wine_pthread_set_functions);
  158. __ASM_OBSOLETE(wine_call_on_stack);
  159. __ASM_OBSOLETE(wine_switch_to_stack);
  160. #endif /* __ASM_OBSOLETE */