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