Home | History | Annotate | Line # | Download | only in i386
pthread_md.h revision 1.11
      1  1.11        ad /*	$NetBSD: pthread_md.h,v 1.11 2007/11/13 17:20:10 ad Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4   1.9        ad  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
      5   1.2   thorpej  * All rights reserved.
      6   1.2   thorpej  *
      7   1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.9        ad  * by Nathan J. Williams, and by Andrew Doran.
      9   1.2   thorpej  *
     10   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.2   thorpej  * modification, are permitted provided that the following conditions
     12   1.2   thorpej  * are met:
     13   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     19   1.2   thorpej  *    must display the following acknowledgement:
     20   1.2   thorpej  *        This product includes software developed by the NetBSD
     21   1.2   thorpej  *        Foundation, Inc. and its contributors.
     22   1.2   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.2   thorpej  *    contributors may be used to endorse or promote products derived
     24   1.2   thorpej  *    from this software without specific prior written permission.
     25   1.2   thorpej  *
     26   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37   1.2   thorpej  */
     38   1.2   thorpej 
     39   1.2   thorpej #ifndef _LIB_PTHREAD_I386_MD_H
     40   1.2   thorpej #define _LIB_PTHREAD_I386_MD_H
     41   1.2   thorpej 
     42   1.2   thorpej #include <sys/ucontext.h>
     43   1.7      cube #include <ucontext.h>
     44   1.2   thorpej 
     45   1.2   thorpej extern int (*_md_getcontext_u)(ucontext_t *);
     46   1.2   thorpej extern int (*_md_setcontext_u)(const ucontext_t *);
     47   1.2   thorpej extern int (*_md_swapcontext_u)(ucontext_t *, const ucontext_t *);
     48   1.2   thorpej 
     49   1.2   thorpej #define _getcontext_u(uc) (*_md_getcontext_u)((uc))
     50   1.2   thorpej #define _setcontext_u(uc) (*_md_setcontext_u)((uc))
     51   1.2   thorpej #define _swapcontext_u(ouc, nuc) (*_md_swapcontext_u)((ouc), (nuc))
     52   1.2   thorpej 
     53   1.2   thorpej int _getcontext_u_s87(ucontext_t *);
     54   1.2   thorpej int _setcontext_u_s87(const ucontext_t *);
     55   1.2   thorpej int _swapcontext_u_s87(ucontext_t *, const ucontext_t *);
     56   1.2   thorpej int _getcontext_u_xmm(ucontext_t *);
     57   1.2   thorpej int _setcontext_u_xmm(const ucontext_t *);
     58   1.2   thorpej int _swapcontext_u_xmm(ucontext_t *, const ucontext_t *);
     59   1.2   thorpej 
     60   1.2   thorpej void pthread__i386_init(void);
     61   1.2   thorpej 
     62   1.2   thorpej #define PTHREAD_MD_INIT pthread__i386_init();
     63   1.2   thorpej 
     64   1.6     perry static inline long
     65   1.2   thorpej pthread__sp(void)
     66   1.2   thorpej {
     67   1.2   thorpej 	long ret;
     68   1.2   thorpej 	__asm("movl %%esp, %0" : "=g" (ret));
     69   1.2   thorpej 
     70   1.2   thorpej 	return ret;
     71   1.2   thorpej }
     72   1.2   thorpej 
     73   1.2   thorpej #define pthread__uc_sp(ucp) ((ucp)->uc_mcontext.__gregs[_REG_UESP])
     74   1.2   thorpej #define pthread__uc_pc(ucp) ((ucp)->uc_mcontext.__gregs[_REG_EIP])
     75   1.2   thorpej 
     76   1.2   thorpej /*
     77   1.2   thorpej  * Set initial, sane values for registers whose values aren't just
     78   1.2   thorpej  * "don't care".
     79   1.7      cube  *
     80   1.7      cube  * We use the current context instead of a guessed one because we cannot
     81   1.7      cube  * assume how the GDT entries are ordered:  what is true on i386 is not
     82   1.7      cube  * true anymore on amd64.
     83   1.2   thorpej  */
     84   1.2   thorpej #define _INITCONTEXT_U_MD(ucp)						\
     85   1.7      cube 	do {								\
     86   1.7      cube 		ucontext_t ucur;					\
     87   1.7      cube 		(void)getcontext(&ucur);				\
     88   1.7      cube 		(ucp)->uc_mcontext.__gregs[_REG_GS] =			\
     89   1.7      cube 		    ucur.uc_mcontext.__gregs[_REG_GS],			\
     90   1.7      cube 		(ucp)->uc_mcontext.__gregs[_REG_FS] =			\
     91   1.7      cube 		    ucur.uc_mcontext.__gregs[_REG_FS],			\
     92   1.7      cube 		(ucp)->uc_mcontext.__gregs[_REG_ES] =			\
     93   1.7      cube 		    ucur.uc_mcontext.__gregs[_REG_ES],			\
     94   1.7      cube 		(ucp)->uc_mcontext.__gregs[_REG_DS] =			\
     95   1.7      cube 		    ucur.uc_mcontext.__gregs[_REG_DS],			\
     96   1.7      cube 		(ucp)->uc_mcontext.__gregs[_REG_CS] =			\
     97   1.7      cube 		    ucur.uc_mcontext.__gregs[_REG_CS],			\
     98   1.7      cube 		(ucp)->uc_mcontext.__gregs[_REG_SS] =			\
     99   1.7      cube 		    ucur.uc_mcontext.__gregs[_REG_SS],			\
    100   1.7      cube 		(ucp)->uc_mcontext.__gregs[_REG_EFL] =			\
    101   1.7      cube 		    ucur.uc_mcontext.__gregs[_REG_EFL];			\
    102   1.7      cube 	} while (/*CONSTCOND*/0);
    103   1.2   thorpej 
    104   1.2   thorpej /*
    105   1.2   thorpej  * Usable stack space below the ucontext_t.
    106   1.2   thorpej  * See comment in pthread_switch.S about STACK_SWITCH.
    107   1.2   thorpej  */
    108   1.2   thorpej #define STACKSPACE	32	/* room for 8 integer values */
    109   1.2   thorpej 
    110   1.2   thorpej /*
    111   1.2   thorpej  * Conversions between struct reg and struct mcontext. Used by
    112   1.2   thorpej  * libpthread_dbg.
    113   1.2   thorpej  */
    114   1.2   thorpej 
    115   1.2   thorpej #define PTHREAD_UCONTEXT_TO_REG(reg, uc) do {				\
    116   1.2   thorpej 	(reg)->r_gs  = (uc)->uc_mcontext.__gregs[_REG_GS];		\
    117   1.2   thorpej 	(reg)->r_fs  = (uc)->uc_mcontext.__gregs[_REG_FS];		\
    118   1.2   thorpej 	(reg)->r_es  = (uc)->uc_mcontext.__gregs[_REG_ES];		\
    119   1.2   thorpej 	(reg)->r_ds  = (uc)->uc_mcontext.__gregs[_REG_DS];		\
    120   1.2   thorpej 	(reg)->r_edi = (uc)->uc_mcontext.__gregs[_REG_EDI];		\
    121   1.2   thorpej 	(reg)->r_esi = (uc)->uc_mcontext.__gregs[_REG_ESI];		\
    122   1.2   thorpej 	(reg)->r_ebp = (uc)->uc_mcontext.__gregs[_REG_EBP];		\
    123   1.2   thorpej 	(reg)->r_ebx = (uc)->uc_mcontext.__gregs[_REG_EBX];		\
    124   1.2   thorpej 	(reg)->r_edx = (uc)->uc_mcontext.__gregs[_REG_EDX];		\
    125   1.2   thorpej 	(reg)->r_ecx = (uc)->uc_mcontext.__gregs[_REG_ECX];		\
    126   1.2   thorpej 	(reg)->r_eax = (uc)->uc_mcontext.__gregs[_REG_EAX];		\
    127   1.2   thorpej 	(reg)->r_eip = (uc)->uc_mcontext.__gregs[_REG_EIP];		\
    128   1.2   thorpej 	(reg)->r_cs  = (uc)->uc_mcontext.__gregs[_REG_CS];		\
    129   1.2   thorpej 	(reg)->r_eflags  = (uc)->uc_mcontext.__gregs[_REG_EFL];		\
    130   1.2   thorpej 	(reg)->r_esp = (uc)->uc_mcontext.__gregs[_REG_UESP];		\
    131   1.2   thorpej 	(reg)->r_ss  = (uc)->uc_mcontext.__gregs[_REG_SS];		\
    132   1.2   thorpej 	} while (/*CONSTCOND*/0)
    133   1.2   thorpej 
    134   1.2   thorpej #define PTHREAD_REG_TO_UCONTEXT(uc, reg) do {				\
    135   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_GS]  = (reg)->r_gs;		\
    136   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_FS]  = (reg)->r_fs; 		\
    137   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_ES]  = (reg)->r_es; 		\
    138   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_DS]  = (reg)->r_ds; 		\
    139   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_EDI] = (reg)->r_edi; 		\
    140   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_ESI] = (reg)->r_esi; 		\
    141   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_EBP] = (reg)->r_ebp; 		\
    142   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_EBX] = (reg)->r_ebx; 		\
    143   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_EDX] = (reg)->r_edx; 		\
    144   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_ECX] = (reg)->r_ecx; 		\
    145   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_EAX] = (reg)->r_eax; 		\
    146   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_EIP] = (reg)->r_eip; 		\
    147   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_CS]  = (reg)->r_cs; 		\
    148   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_EFL] = (reg)->r_eflags; 		\
    149   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_UESP]= (reg)->r_esp;		\
    150   1.2   thorpej 	(uc)->uc_mcontext.__gregs[_REG_SS]  = (reg)->r_ss; 		\
    151   1.4  christos 	/*LINTED precision loss */					\
    152   1.4  christos 	(uc)->uc_flags = ((uc)->uc_flags | _UC_CPU) & ~_UC_USER; 	\
    153   1.2   thorpej 	} while (/*CONSTCOND*/0)
    154   1.2   thorpej 
    155   1.4  christos 
    156   1.2   thorpej #define PTHREAD_UCONTEXT_TO_FPREG(freg, uc)		       		\
    157   1.3  christos 	(void)memcpy((freg)->__data,					\
    158   1.2   thorpej         (uc)->uc_mcontext.__fpregs.__fp_reg_set.__fpchip_state.__fp_state, \
    159   1.2   thorpej 	sizeof(struct fpreg))
    160   1.2   thorpej 
    161   1.2   thorpej #define PTHREAD_FPREG_TO_UCONTEXT(uc, freg) do {       	       		\
    162   1.3  christos 	(void)memcpy(							\
    163   1.2   thorpej         (uc)->uc_mcontext.__fpregs.__fp_reg_set.__fpchip_state.__fp_state, \
    164   1.2   thorpej 	(freg)->__data, sizeof(struct fpreg));				\
    165   1.4  christos 	/*LINTED precision loss */					\
    166   1.4  christos 	(uc)->uc_flags = ((uc)->uc_flags | _UC_FPU) & ~_UC_USER;	\
    167   1.2   thorpej 	} while (/*CONSTCOND*/0)
    168   1.2   thorpej 
    169   1.5   nathanw #define PTHREAD_UCONTEXT_XREG_FLAG	_UC_FXSAVE
    170   1.5   nathanw 
    171   1.5   nathanw #define PTHREAD_UCONTEXT_TO_XREG(xreg, uc)				\
    172   1.5   nathanw 	(void)memcpy((xreg),						\
    173   1.5   nathanw 	(uc)->uc_mcontext.__fpregs.__fp_reg_set.__fp_xmm_state.__fp_xmm, \
    174   1.5   nathanw 	sizeof(struct xmmregs))
    175   1.5   nathanw 
    176   1.5   nathanw #define PTHREAD_XREG_TO_UCONTEXT(uc, xreg) do {				\
    177   1.5   nathanw 	(void)memcpy(							\
    178   1.5   nathanw 	(uc)->uc_mcontext.__fpregs.__fp_reg_set.__fp_xmm_state.__fp_xmm, \
    179   1.5   nathanw 	(xreg),								\
    180   1.5   nathanw 	sizeof(struct xmmregs));					\
    181   1.5   nathanw 	(uc)->uc_flags = ((uc)->uc_flags | _UC_FXSAVE) & ~_UC_USER;	\
    182   1.5   nathanw 	} while (/*CONSTCOND*/0)
    183   1.5   nathanw 
    184   1.8        ad #define	pthread__smt_pause()	__asm __volatile("rep; nop" ::: "memory")
    185   1.8        ad #define	PTHREAD__HAVE_ATOMIC
    186  1.11        ad /* #define	PTHREAD__HAVE_THREADREG */
    187  1.11        ad 
    188  1.11        ad void	pthread__threadreg_set(pthread_t);
    189  1.11        ad 
    190  1.11        ad static inline pthread_t
    191  1.11        ad pthread__threadreg_get(void)
    192  1.11        ad {
    193  1.11        ad 	pthread_t self;
    194  1.11        ad 
    195  1.11        ad 	__asm volatile("movl %%gs:0, %0"
    196  1.11        ad 		: "=r" (self)
    197  1.11        ad 		:);
    198  1.11        ad 
    199  1.11        ad 	return self;
    200  1.11        ad }
    201  1.11        ad 
    202  1.11        ad static inline void *
    203  1.11        ad pthread__atomic_cas_ptr(volatile void *ptr, const void *old, const void *new)
    204  1.11        ad {
    205  1.11        ad 	volatile uintptr_t *cast = ptr;
    206  1.11        ad 	void *ret;
    207  1.11        ad 
    208  1.11        ad 	__asm __volatile ("lock; cmpxchgl %2, %1"
    209  1.11        ad 		: "=a" (ret), "=m" (*cast)
    210  1.11        ad 		: "r" (new), "m" (*cast), "0" (old));
    211  1.11        ad 
    212  1.11        ad 	return ret;
    213  1.11        ad }
    214  1.11        ad 
    215  1.11        ad static inline void *
    216  1.11        ad pthread__atomic_cas_ptr_ni(volatile void *ptr, const void *old, const void *new)
    217  1.11        ad {
    218  1.11        ad 	volatile uintptr_t *cast = ptr;
    219  1.11        ad 	void *ret;
    220  1.11        ad 
    221  1.11        ad 	__asm __volatile ("cmpxchgl %2, %1"
    222  1.11        ad 		: "=a" (ret), "=m" (*cast)
    223  1.11        ad 		: "r" (new), "m" (*cast), "0" (old));
    224  1.11        ad 
    225  1.11        ad 	return ret;
    226  1.11        ad }
    227   1.8        ad 
    228   1.2   thorpej #endif /* _LIB_PTHREAD_I386_MD_H */
    229