Home | History | Annotate | Line # | Download | only in x86_64
      1  1.13  riastrad /*	$NetBSD: pthread_md.h,v 1.13 2023/05/25 14:30:03 riastradh 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.12  christos static inline unsigned long
     40   1.1      fvdl pthread__sp(void)
     41   1.1      fvdl {
     42  1.12  christos 	unsigned 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 
     50   1.1      fvdl /*
     51   1.1      fvdl  * Set initial, sane values for registers whose values aren't just
     52   1.1      fvdl  * "don't care".
     53   1.1      fvdl  * 0x23 is GSEL(GUDATA_SEL, SEL_UPL), and
     54   1.1      fvdl  * 0x1b is GSEL(GUCODE_SEL, SEL_UPL).
     55   1.1      fvdl  * 0x202 is PSL_USERSET.
     56   1.1      fvdl  */
     57   1.1      fvdl #define _INITCONTEXT_U_MD(ucp)						\
     58   1.1      fvdl 	(ucp)->uc_mcontext.__gregs[_REG_GS] = 0x23,			\
     59   1.1      fvdl 	(ucp)->uc_mcontext.__gregs[_REG_FS] = 0x23,			\
     60   1.1      fvdl 	(ucp)->uc_mcontext.__gregs[_REG_ES] = 0x23,			\
     61   1.1      fvdl 	(ucp)->uc_mcontext.__gregs[_REG_DS] = 0x23,			\
     62   1.1      fvdl 	(ucp)->uc_mcontext.__gregs[_REG_CS] = 0x1b,			\
     63   1.1      fvdl 	(ucp)->uc_mcontext.__gregs[_REG_SS] = 0x23,			\
     64   1.1      fvdl 	(ucp)->uc_mcontext.__gregs[_REG_RFL] = 0x202;
     65   1.1      fvdl 
     66   1.5        ad #define	pthread__smt_pause()	__asm __volatile("rep; nop" ::: "memory")
     67  1.13  riastrad #define	pthread__smt_wait()	__asm __volatile("rep; nop" ::: "memory")
     68   1.5        ad 
     69   1.8        ad /* Don't need additional memory barriers. */
     70   1.8        ad #define	PTHREAD__ATOMIC_IS_MEMBAR
     71   1.7        ad 
     72   1.9        ad static inline void *
     73   1.9        ad _atomic_cas_ptr(volatile void *ptr, void *old, void *new)
     74   1.9        ad {
     75   1.9        ad 	volatile uintptr_t *cast = ptr;
     76   1.9        ad 	void *ret;
     77   1.9        ad 
     78   1.9        ad 	__asm __volatile ("lock; cmpxchgq %2, %1"
     79   1.9        ad 		: "=a" (ret), "=m" (*cast)
     80   1.9        ad 		: "r" (new), "m" (*cast), "0" (old));
     81   1.9        ad 
     82   1.9        ad 	return ret;
     83   1.9        ad }
     84   1.9        ad 
     85   1.9        ad static inline void *
     86   1.9        ad _atomic_cas_ptr_ni(volatile void *ptr, void *old, void *new)
     87   1.9        ad {
     88   1.9        ad 	volatile uintptr_t *cast = ptr;
     89   1.9        ad 	void *ret;
     90   1.9        ad 
     91   1.9        ad 	__asm __volatile ("cmpxchgq %2, %1"
     92   1.9        ad 		: "=a" (ret), "=m" (*cast)
     93   1.9        ad 		: "r" (new), "m" (*cast), "0" (old));
     94   1.9        ad 
     95   1.9        ad 	return ret;
     96   1.9        ad }
     97   1.9        ad 
     98   1.1      fvdl #endif /* _LIB_PTHREAD_X86_64_MD_H */
     99