port.c revision b955672b950093ff7416d1269dd4d3b69983bd8f
45e9809aff7304721fddb95654901b32195c9c7avboxsync/*
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Wine portability routines
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Copyright 2000 Alexandre Julliard
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * This library is free software; you can redistribute it and/or
45e9809aff7304721fddb95654901b32195c9c7avboxsync * modify it under the terms of the GNU Lesser General Public
45e9809aff7304721fddb95654901b32195c9c7avboxsync * License as published by the Free Software Foundation; either
45e9809aff7304721fddb95654901b32195c9c7avboxsync * version 2.1 of the License, or (at your option) any later version.
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * This library is distributed in the hope that it will be useful,
45e9809aff7304721fddb95654901b32195c9c7avboxsync * but WITHOUT ANY WARRANTY; without even the implied warranty of
45e9809aff7304721fddb95654901b32195c9c7avboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Lesser General Public License for more details.
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * You should have received a copy of the GNU Lesser General Public
45e9809aff7304721fddb95654901b32195c9c7avboxsync * License along with this library; if not, write to the Free Software
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync/*
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
45e9809aff7304721fddb95654901b32195c9c7avboxsync * other than GPL or LGPL is available it will apply instead, Sun elects to use only
45e9809aff7304721fddb95654901b32195c9c7avboxsync * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
45e9809aff7304721fddb95654901b32195c9c7avboxsync * a choice of LGPL license versions is made available with the language indicating
45e9809aff7304721fddb95654901b32195c9c7avboxsync * that LGPLv2 or any later version may be used, or where a choice of which version
45e9809aff7304721fddb95654901b32195c9c7avboxsync * of the LGPL is applied is otherwise unspecified.
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include "config.h"
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include "wine/port.h"
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include <stdlib.h>
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include <string.h>
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include <sys/types.h>
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include "wine/library.h"
45e9809aff7304721fddb95654901b32195c9c7avboxsync#include "wine/pthread.h"
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsyncstatic struct wine_pthread_functions pthread_functions;
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync/***********************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync * wine_pthread_get_functions
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsyncvoid wine_pthread_get_functions( struct wine_pthread_functions *functions, size_t size )
45e9809aff7304721fddb95654901b32195c9c7avboxsync{
45e9809aff7304721fddb95654901b32195c9c7avboxsync memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
45e9809aff7304721fddb95654901b32195c9c7avboxsync}
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync/***********************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync * wine_pthread_set_functions
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsyncvoid wine_pthread_set_functions( const struct wine_pthread_functions *functions, size_t size )
45e9809aff7304721fddb95654901b32195c9c7avboxsync{
45e9809aff7304721fddb95654901b32195c9c7avboxsync memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
45e9809aff7304721fddb95654901b32195c9c7avboxsync}
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync/***********************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync * wine_switch_to_stack
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Switch to the specified stack and call the function.
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsyncvoid DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack );
45e9809aff7304721fddb95654901b32195c9c7avboxsync#if defined(__i386__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_switch_to_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movl 4(%esp),%ecx\n\t" /* func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movl 8(%esp),%edx\n\t" /* arg */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movl 12(%esp),%esp\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "andl $~15,%esp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "subl $12,%esp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "pushl %edx\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "xorl %ebp,%ebp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "call *%ecx\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "int $3" /* we never return here */ )
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__i386__) && defined(_MSC_VER)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__declspec(naked) void wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
45e9809aff7304721fddb95654901b32195c9c7avboxsync{
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm mov ecx, 4[esp];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm mov edx, 8[esp];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm mov esp, 12[esp];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm push edx;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm xor ebp, ebp;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm call [ecx];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm int 3;
45e9809aff7304721fddb95654901b32195c9c7avboxsync}
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__sparc__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_switch_to_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mov %o0, %l0\n\t" /* store first argument */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mov %o1, %l1\n\t" /* store second argument */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "sub %o2, 96, %sp\n\t" /* store stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "call %l0, 0\n\t" /* call func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mov %l1, %o0\n\t" /* delay slot: arg for func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "ta 0x01") /* breakpoint - we never get here */
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__powerpc__) && defined(__APPLE__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_switch_to_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mtctr r3\n\t" /* func -> ctr */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mr r3,r4\n\t" /* args -> function param 1 (r3) */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mr r1,r5\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "subi r1,r1,0x100\n\t" /* adjust stack pointer */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "bctrl\n" /* call ctr */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "1:\tb 1b") /* loop */
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__powerpc__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_switch_to_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mtctr 3\n\t" /* func -> ctr */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mr 3,4\n\t" /* args -> function param 1 (r3) */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mr 1,5\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "subi 1, 1, 16\n\t" /* allocate space for the callee */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "li 0, 0\n\t" /* load zero */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "stw 0, 0(1)\n\t" /* create a bottom stack frame */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "bctrl\n\t" /* call ctr */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "1:\tb 1b") /* loop */
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__ALPHA__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_switch_to_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mov $16,$0\n\t" /* func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mov $17,$16\n\t" /* arg */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mov $18,$30\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "jsr $31,($0),0\n\t" /* call func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "L1:\tbr $31,L1") /* loop */
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__x86_64__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_switch_to_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rdi,%rax\n\t" /* func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rsi,%rdi\n\t" /* arg */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "andq $~15,%rdx\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rdx,%rsp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "xorq %rbp,%rbp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "callq *%rax\n\t" /* call func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "int $3")
45e9809aff7304721fddb95654901b32195c9c7avboxsync#else
45e9809aff7304721fddb95654901b32195c9c7avboxsyncvoid DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
45e9809aff7304721fddb95654901b32195c9c7avboxsync{
45e9809aff7304721fddb95654901b32195c9c7avboxsync wine_call_on_stack( (int (*)(void *))func, arg, stack );
45e9809aff7304721fddb95654901b32195c9c7avboxsync abort();
45e9809aff7304721fddb95654901b32195c9c7avboxsync}
45e9809aff7304721fddb95654901b32195c9c7avboxsync#endif
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync/***********************************************************************
45e9809aff7304721fddb95654901b32195c9c7avboxsync * wine_call_on_stack
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Switch to the specified stack to call the function and return.
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsyncint wine_call_on_stack( int (*func)(void *), void *arg, void *stack );
45e9809aff7304721fddb95654901b32195c9c7avboxsync#if defined(__i386__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_call_on_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "pushl %ebp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "pushl %esi\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movl 12(%esp),%ecx\n\t" /* func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movl 16(%esp),%edx\n\t" /* arg */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movl 20(%esp),%esi\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "andl $~15,%esi\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "subl $12,%esi\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "xchgl %esi,%esp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "pushl %edx\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "xorl %ebp,%ebp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "call *%ecx\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movl %esi,%esp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "popl %esi\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "popl %ebp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "ret" )
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__i386__) && defined(_MSC_VER)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__declspec(naked) int wine_call_on_stack( int (*func)(void *), void *arg, void *stack )
45e9809aff7304721fddb95654901b32195c9c7avboxsync{
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm push ebp;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm push esi;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm mov ecx, 12[esp];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm mov edx, 16[esp];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm mov esi, 20[esp];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm xchg esp, esi;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm push edx;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm xor ebp, ebp;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm call [ecx];
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm mov esp, esi;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm pop esi;
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm pop ebp
45e9809aff7304721fddb95654901b32195c9c7avboxsync __asm ret;
45e9809aff7304721fddb95654901b32195c9c7avboxsync}
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__x86_64__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_call_on_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "pushq %rbp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "pushq %rbx\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rsp,%rbx\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rdi,%rax\n\t" /* func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rsi,%rdi\n\t" /* arg */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "andq $~15,%rdx\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rdx,%rsp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "xorq %rbp,%rbp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "callq *%rax\n\t" /* call func */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "movq %rbx,%rsp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "popq %rbx\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "popq %rbp\n\t"
45e9809aff7304721fddb95654901b32195c9c7avboxsync "ret")
45e9809aff7304721fddb95654901b32195c9c7avboxsync#elif defined(__powerpc__) && defined(__GNUC__)
45e9809aff7304721fddb95654901b32195c9c7avboxsync__ASM_GLOBAL_FUNC( wine_call_on_stack,
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mflr 0\n\t" /* get return address */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "stw 0, 4(1)\n\t" /* save return address */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "subi 5, 5, 16\n\t" /* reserve space on new stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "stw 1, 12(5)\n\t" /* store old sp */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mtctr 3\n\t" /* func -> ctr */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mr 3,4\n\t" /* args -> function param 1 (r3) */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mr 1,5\n\t" /* stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "li 0, 0\n\t" /* zero */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "stw 0, 0(1)\n\t" /* bottom of stack */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "stwu 1, -16(1)\n\t" /* create a frame for this function */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "bctrl\n\t" /* call ctr */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "lwz 1, 28(1)\n\t" /* fetch old sp */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "lwz 0, 4(1)\n\t" /* fetch return address */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "mtlr 0\n\t" /* return address -> lr */
45e9809aff7304721fddb95654901b32195c9c7avboxsync "blr") /* return */
45e9809aff7304721fddb95654901b32195c9c7avboxsync#else
45e9809aff7304721fddb95654901b32195c9c7avboxsync#error You must implement wine_switch_to_stack for your platform
45e9809aff7304721fddb95654901b32195c9c7avboxsync#endif
45e9809aff7304721fddb95654901b32195c9c7avboxsync