Home | History | Annotate | Line # | Download | only in i386
pthread_md.h revision 1.1.2.9
      1 /*	$NetBSD: pthread_md.h,v 1.1.2.9 2002/12/06 21:22:39 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #ifndef _LIB_PTHREAD_I386_MD_H
     40 #define _LIB_PTHREAD_I386_MD_H
     41 
     42 extern int (*_md_getcontext_u)(ucontext_t *);
     43 extern int (*_md_setcontext_u)(const ucontext_t *);
     44 extern int (*_md_swapcontext_u)(ucontext_t *, const ucontext_t *);
     45 
     46 #define _getcontext_u(uc) (*_md_getcontext_u)((uc))
     47 #define _setcontext_u(uc) (*_md_setcontext_u)((uc))
     48 #define _swapcontext_u(ouc, nuc) (*_md_swapcontext_u)((ouc), (nuc))
     49 
     50 int _getcontext_u_s87(ucontext_t *);
     51 int _setcontext_u_s87(const ucontext_t *);
     52 int _swapcontext_u_s87(ucontext_t *, const ucontext_t *);
     53 int _getcontext_u_xmm(ucontext_t *);
     54 int _setcontext_u_xmm(const ucontext_t *);
     55 int _swapcontext_u_xmm(ucontext_t *, const ucontext_t *);
     56 
     57 void pthread__i386_init(void);
     58 
     59 #define PTHREAD_MD_INIT pthread__i386_init();
     60 
     61 static __inline long
     62 pthread__sp(void)
     63 {
     64 	long ret;
     65 	__asm("movl %%esp, %0" : "=g" (ret));
     66 
     67 	return ret;
     68 }
     69 
     70 #define pthread__uc_sp(ucp) ((ucp)->uc_mcontext.__gregs[_REG_UESP])
     71 #define pthread__uc_pc(ucp) ((ucp)->uc_mcontext.__gregs[_REG_EIP])
     72 
     73 /*
     74  * Set initial, sane values for registers whose values aren't just
     75  * "don't care".
     76  * 0x2b is GSEL(GUDATA_SEL, SEL_UPL), and
     77  * 0x23 is GSEL(GUCODE_SEL, SEL_UPL), from arch/i386/include/segments.h.
     78  * 0x202 is PSL_USERSET from arch/i386/include/psl.h
     79  */
     80 #define _INITCONTEXT_U_MD(ucp)						\
     81 	(ucp)->uc_mcontext.__gregs[_REG_GS] = 0x2b;			\
     82 	(ucp)->uc_mcontext.__gregs[_REG_FS] = 0x2b;			\
     83 	(ucp)->uc_mcontext.__gregs[_REG_ES] = 0x2b;			\
     84 	(ucp)->uc_mcontext.__gregs[_REG_DS] = 0x2b;			\
     85 	(ucp)->uc_mcontext.__gregs[_REG_CS] = 0x23;			\
     86 	(ucp)->uc_mcontext.__gregs[_REG_SS] = 0x2b;			\
     87 	(ucp)->uc_mcontext.__gregs[_REG_EFL] = 0x202;
     88 
     89 /*
     90  * Usable stack space below the ucontext_t.
     91  * See comment in pthread_switch.S about STACK_SWITCH.
     92  */
     93 #define STACKSPACE	32	/* room for 8 integer values */
     94 
     95 /*
     96  * Conversions between struct reg and struct mcontext. Used by
     97  * libpthread_dbg.
     98  */
     99 
    100 #define PTHREAD_UCONTEXT_TO_REG(reg, uc) do {				\
    101 	(reg)->r_gs  = (uc)->uc_mcontext.__gregs[_REG_GS];		\
    102 	(reg)->r_fs  = (uc)->uc_mcontext.__gregs[_REG_FS];		\
    103 	(reg)->r_es  = (uc)->uc_mcontext.__gregs[_REG_ES];		\
    104 	(reg)->r_ds  = (uc)->uc_mcontext.__gregs[_REG_DS];		\
    105 	(reg)->r_edi = (uc)->uc_mcontext.__gregs[_REG_EDI];		\
    106 	(reg)->r_esi = (uc)->uc_mcontext.__gregs[_REG_ESI];		\
    107 	(reg)->r_ebp = (uc)->uc_mcontext.__gregs[_REG_EBP];		\
    108 	(reg)->r_ebx = (uc)->uc_mcontext.__gregs[_REG_EBX];		\
    109 	(reg)->r_edx = (uc)->uc_mcontext.__gregs[_REG_EDX];		\
    110 	(reg)->r_ecx = (uc)->uc_mcontext.__gregs[_REG_ECX];		\
    111 	(reg)->r_eax = (uc)->uc_mcontext.__gregs[_REG_EAX];		\
    112 	(reg)->r_eip = (uc)->uc_mcontext.__gregs[_REG_EIP];		\
    113 	(reg)->r_cs  = (uc)->uc_mcontext.__gregs[_REG_CS];		\
    114 	(reg)->r_eflags  = (uc)->uc_mcontext.__gregs[_REG_EFL];		\
    115 	(reg)->r_esp = (uc)->uc_mcontext.__gregs[_REG_UESP];		\
    116 	(reg)->r_ss  = (uc)->uc_mcontext.__gregs[_REG_SS];		\
    117 	} while (/*CONSTCOND*/0)
    118 
    119 #define PTHREAD_REG_TO_UCONTEXT(uc, reg) do {				\
    120 	(uc)->uc_mcontext.__gregs[_REG_GS]  = (reg)->r_gs;		\
    121 	(uc)->uc_mcontext.__gregs[_REG_FS]  = (reg)->r_fs; 		\
    122 	(uc)->uc_mcontext.__gregs[_REG_ES]  = (reg)->r_es; 		\
    123 	(uc)->uc_mcontext.__gregs[_REG_DS]  = (reg)->r_ds; 		\
    124 	(uc)->uc_mcontext.__gregs[_REG_EDI] = (reg)->r_edi; 		\
    125 	(uc)->uc_mcontext.__gregs[_REG_ESI] = (reg)->r_esi; 		\
    126 	(uc)->uc_mcontext.__gregs[_REG_EBP] = (reg)->r_ebp; 		\
    127 	(uc)->uc_mcontext.__gregs[_REG_EBX] = (reg)->r_ebx; 		\
    128 	(uc)->uc_mcontext.__gregs[_REG_EDX] = (reg)->r_edx; 		\
    129 	(uc)->uc_mcontext.__gregs[_REG_ECX] = (reg)->r_ecx; 		\
    130 	(uc)->uc_mcontext.__gregs[_REG_EAX] = (reg)->r_eax; 		\
    131 	(uc)->uc_mcontext.__gregs[_REG_EIP] = (reg)->r_eip; 		\
    132 	(uc)->uc_mcontext.__gregs[_REG_CS]  = (reg)->r_cs; 		\
    133 	(uc)->uc_mcontext.__gregs[_REG_EFL] = (reg)->r_eflags; 		\
    134 	(uc)->uc_mcontext.__gregs[_REG_UESP]= (reg)->r_esp;		\
    135 	(uc)->uc_mcontext.__gregs[_REG_SS]  = (reg)->r_ss; 		\
    136 	(uc)->uc_flags = ((uc)->uc_flags | _UC_CPU) & ~_UC_USER;       	\
    137 	} while (/*CONSTCOND*/0)
    138 
    139 #define PTHREAD_UCONTEXT_TO_FPREG(freg, uc)		       		\
    140 	memcpy((freg)->__data,						\
    141         (uc)->uc_mcontext.__fpregs.__fp_reg_set.__fpchip_state.__fp_state, \
    142 	sizeof(struct fpreg))
    143 
    144 #define PTHREAD_FPREG_TO_UCONTEXT(uc, freg) do {       	       		\
    145 	memcpy(								\
    146         (uc)->uc_mcontext.__fpregs.__fp_reg_set.__fpchip_state.__fp_state, \
    147 	(freg)->__data, sizeof(struct fpreg));				\
    148 	(uc)->uc_flags = ((uc)->uc_flags | _UC_FPU) & ~_UC_USER;       	\
    149 	} while (/*CONSTCOND*/0)
    150 
    151 
    152 #define PTHREAD_UCONTEXT_TO_SIGCONTEXT(uc, sc) do {			\
    153 	memcpy((sc), &(uc)->uc_mcontext.__gregs, 7 * sizeof (int));	\
    154 	memcpy(&(sc)->sc_ebx, &(uc)->uc_mcontext.__gregs[_REG_EBX],	\
    155 	    4 * sizeof (int));						\
    156 	(sc)->sc_eip    = (uc)->uc_mcontext.__gregs[_REG_EIP];		\
    157 	(sc)->sc_cs     = (uc)->uc_mcontext.__gregs[_REG_CS];		\
    158 	(sc)->sc_eflags = (uc)->uc_mcontext.__gregs[_REG_EFL];		\
    159 	(sc)->sc_esp    = (uc)->uc_mcontext.__gregs[_REG_UESP];		\
    160 	(sc)->sc_ss     = (uc)->uc_mcontext.__gregs[_REG_SS];		\
    161 	(sc)->sc_trapno = (uc)->uc_mcontext.__gregs[_REG_TRAPNO];      	\
    162 	(sc)->sc_err    = (uc)->uc_mcontext.__gregs[_REG_ERR];		\
    163 	} while (/*CONSTCOND*/0)
    164 
    165 #define PTHREAD_SIGCONTEXT_TO_UCONTEXT(uc, sc) do {			\
    166 	memcpy(&(uc)->uc_mcontext.__gregs, (sc), 7 * sizeof (int));	\
    167 	memcpy(&(uc)->uc_mcontext.__gregs[_REG_EBX], &(sc)->sc_ebx,	\
    168 	    4 * sizeof (int));						\
    169 	(uc)->uc_mcontext.__gregs[_REG_EIP]    = (sc)->sc_eip;		\
    170 	(uc)->uc_mcontext.__gregs[_REG_CS]     = (sc)->sc_cs;		\
    171 	(uc)->uc_mcontext.__gregs[_REG_EFL]    = (sc)->sc_eflags;	\
    172 	(uc)->uc_mcontext.__gregs[_REG_UESP]   = (sc)->sc_esp;		\
    173 	(uc)->uc_mcontext.__gregs[_REG_SS]     = (sc)->sc_ss;		\
    174 	(uc)->uc_mcontext.__gregs[_REG_TRAPNO] = (sc)->sc_trapno;	\
    175 	(uc)->uc_mcontext.__gregs[_REG_ERR]    = (sc)->sc_err;		\
    176 	(uc)->uc_flags &= ~_UC_USER;					\
    177 	} while (/*CONSTCOND*/0)
    178 
    179 #endif /* _LIB_PTHREAD_I386_MD_H */
    180