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