Home | History | Annotate | Line # | Download | only in x86_64
pthread_md.h revision 1.10.8.2
      1  1.10.8.2  martin /*	$NetBSD: pthread_md.h,v 1.10.8.2 2008/04/28 20:23:03 martin Exp $	*/
      2  1.10.8.2  martin 
      3  1.10.8.2  martin /*-
      4  1.10.8.2  martin  * Copyright (c) 2001, 2007, 2008 The NetBSD Foundation, Inc.
      5  1.10.8.2  martin  * All rights reserved.
      6  1.10.8.2  martin  *
      7  1.10.8.2  martin  * This code is derived from software contributed to The NetBSD Foundation
      8  1.10.8.2  martin  * by Nathan J. Williams.
      9  1.10.8.2  martin  *
     10  1.10.8.2  martin  * Redistribution and use in source and binary forms, with or without
     11  1.10.8.2  martin  * modification, are permitted provided that the following conditions
     12  1.10.8.2  martin  * are met:
     13  1.10.8.2  martin  * 1. Redistributions of source code must retain the above copyright
     14  1.10.8.2  martin  *    notice, this list of conditions and the following disclaimer.
     15  1.10.8.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.10.8.2  martin  *    notice, this list of conditions and the following disclaimer in the
     17  1.10.8.2  martin  *    documentation and/or other materials provided with the distribution.
     18  1.10.8.2  martin  *
     19  1.10.8.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.10.8.2  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.10.8.2  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.10.8.2  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.10.8.2  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.10.8.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.10.8.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.10.8.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.10.8.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.10.8.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.10.8.2  martin  * POSSIBILITY OF SUCH DAMAGE.
     30  1.10.8.2  martin  *
     31  1.10.8.2  martin  * Adapted for x86_64 by fvdl (at) NetBSD.org
     32  1.10.8.2  martin  */
     33  1.10.8.2  martin 
     34  1.10.8.2  martin #ifndef _LIB_PTHREAD_X86_64_MD_H
     35  1.10.8.2  martin #define _LIB_PTHREAD_X86_64_MD_H
     36  1.10.8.2  martin 
     37  1.10.8.2  martin #include <sys/ucontext.h>
     38  1.10.8.2  martin 
     39  1.10.8.2  martin static inline long
     40  1.10.8.2  martin pthread__sp(void)
     41  1.10.8.2  martin {
     42  1.10.8.2  martin 	long ret;
     43  1.10.8.2  martin 	__asm("movq %%rsp, %0" : "=g" (ret));
     44  1.10.8.2  martin 
     45  1.10.8.2  martin 	return ret;
     46  1.10.8.2  martin }
     47  1.10.8.2  martin 
     48  1.10.8.2  martin #define pthread__uc_sp(ucp) ((ucp)->uc_mcontext.__gregs[_REG_URSP])
     49  1.10.8.2  martin #define pthread__uc_pc(ucp) ((ucp)->uc_mcontext.__gregs[_REG_RIP])
     50  1.10.8.2  martin 
     51  1.10.8.2  martin /*
     52  1.10.8.2  martin  * Set initial, sane values for registers whose values aren't just
     53  1.10.8.2  martin  * "don't care".
     54  1.10.8.2  martin  * 0x23 is GSEL(GUDATA_SEL, SEL_UPL), and
     55  1.10.8.2  martin  * 0x1b is GSEL(GUCODE_SEL, SEL_UPL).
     56  1.10.8.2  martin  * 0x202 is PSL_USERSET.
     57  1.10.8.2  martin  */
     58  1.10.8.2  martin #define _INITCONTEXT_U_MD(ucp)						\
     59  1.10.8.2  martin 	(ucp)->uc_mcontext.__gregs[_REG_GS] = 0x23,			\
     60  1.10.8.2  martin 	(ucp)->uc_mcontext.__gregs[_REG_FS] = 0x23,			\
     61  1.10.8.2  martin 	(ucp)->uc_mcontext.__gregs[_REG_ES] = 0x23,			\
     62  1.10.8.2  martin 	(ucp)->uc_mcontext.__gregs[_REG_DS] = 0x23,			\
     63  1.10.8.2  martin 	(ucp)->uc_mcontext.__gregs[_REG_CS] = 0x1b,			\
     64  1.10.8.2  martin 	(ucp)->uc_mcontext.__gregs[_REG_SS] = 0x23,			\
     65  1.10.8.2  martin 	(ucp)->uc_mcontext.__gregs[_REG_RFL] = 0x202;
     66  1.10.8.2  martin 
     67  1.10.8.2  martin /*
     68  1.10.8.2  martin  * Usable stack space below the ucontext_t.
     69  1.10.8.2  martin  * See comment in pthread_switch.S about STACK_SWITCH.
     70  1.10.8.2  martin  */
     71  1.10.8.2  martin #define STACKSPACE	64	/* room for 8 long values */
     72  1.10.8.2  martin 
     73  1.10.8.2  martin /*
     74  1.10.8.2  martin  * Conversions between struct reg and struct mcontext. Used by
     75  1.10.8.2  martin  * libpthread_dbg.
     76  1.10.8.2  martin  */
     77  1.10.8.2  martin 
     78  1.10.8.2  martin #define PTHREAD_UCONTEXT_TO_REG(reg, uc) \
     79  1.10.8.2  martin 	memcpy(reg, (uc)->uc_mcontext.__gregs, _NGREG * sizeof (long));
     80  1.10.8.2  martin 
     81  1.10.8.2  martin #define PTHREAD_REG_TO_UCONTEXT(uc, reg) do {				\
     82  1.10.8.2  martin 	memcpy((uc)->uc_mcontext.__gregs, reg, _NGREG * sizeof (long)); \
     83  1.10.8.2  martin 	(uc)->uc_flags = ((uc)->uc_flags | _UC_CPU) & ~_UC_USER; 	\
     84  1.10.8.2  martin 	} while (/*CONSTCOND*/0)
     85  1.10.8.2  martin 
     86  1.10.8.2  martin 
     87  1.10.8.2  martin #define PTHREAD_UCONTEXT_TO_FPREG(freg, uc)		       		\
     88  1.10.8.2  martin 	(void)memcpy(&(freg)->fxstate,					\
     89  1.10.8.2  martin         (uc)->uc_mcontext.__fpregs, sizeof(struct fpreg))
     90  1.10.8.2  martin 
     91  1.10.8.2  martin #define PTHREAD_FPREG_TO_UCONTEXT(uc, freg) do {       	       		\
     92  1.10.8.2  martin 	(void)memcpy(							\
     93  1.10.8.2  martin         (uc)->uc_mcontext.__fpregs,					\
     94  1.10.8.2  martin 	&(freg)->fxstate, sizeof(struct fpreg));			\
     95  1.10.8.2  martin 	/*LINTED precision loss */					\
     96  1.10.8.2  martin 	(uc)->uc_flags = ((uc)->uc_flags | _UC_FPU) & ~_UC_USER;	\
     97  1.10.8.2  martin 	} while (/*CONSTCOND*/0)
     98  1.10.8.2  martin 
     99  1.10.8.2  martin #define	pthread__smt_pause()	__asm __volatile("rep; nop" ::: "memory")
    100  1.10.8.2  martin 
    101  1.10.8.2  martin /* Don't need additional memory barriers. */
    102  1.10.8.2  martin #define	PTHREAD__ATOMIC_IS_MEMBAR
    103  1.10.8.2  martin 
    104  1.10.8.2  martin static inline void *
    105  1.10.8.2  martin _atomic_cas_ptr(volatile void *ptr, void *old, void *new)
    106  1.10.8.2  martin {
    107  1.10.8.2  martin 	volatile uintptr_t *cast = ptr;
    108  1.10.8.2  martin 	void *ret;
    109  1.10.8.2  martin 
    110  1.10.8.2  martin 	__asm __volatile ("lock; cmpxchgq %2, %1"
    111  1.10.8.2  martin 		: "=a" (ret), "=m" (*cast)
    112  1.10.8.2  martin 		: "r" (new), "m" (*cast), "0" (old));
    113  1.10.8.2  martin 
    114  1.10.8.2  martin 	return ret;
    115  1.10.8.2  martin }
    116  1.10.8.2  martin 
    117  1.10.8.2  martin static inline void *
    118  1.10.8.2  martin _atomic_cas_ptr_ni(volatile void *ptr, void *old, void *new)
    119  1.10.8.2  martin {
    120  1.10.8.2  martin 	volatile uintptr_t *cast = ptr;
    121  1.10.8.2  martin 	void *ret;
    122  1.10.8.2  martin 
    123  1.10.8.2  martin 	__asm __volatile ("cmpxchgq %2, %1"
    124  1.10.8.2  martin 		: "=a" (ret), "=m" (*cast)
    125  1.10.8.2  martin 		: "r" (new), "m" (*cast), "0" (old));
    126  1.10.8.2  martin 
    127  1.10.8.2  martin 	return ret;
    128  1.10.8.2  martin }
    129  1.10.8.2  martin 
    130  1.10.8.2  martin #endif /* _LIB_PTHREAD_X86_64_MD_H */
    131