pthread_md.h revision 1.1 1 1.1 fvdl /* $NetBSD: pthread_md.h,v 1.1 2003/01/30 02:10:32 fvdl 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.1 fvdl * 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.1 fvdl 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.1 fvdl #define PTHREAD_UCONTEXT_TO_REG(reg, uc) do { \
86 1.1 fvdl (reg)->r_gs = (uc)->uc_mcontext.__gregs[_REG_GS]; \
87 1.1 fvdl (reg)->r_fs = (uc)->uc_mcontext.__gregs[_REG_FS]; \
88 1.1 fvdl (reg)->r_es = (uc)->uc_mcontext.__gregs[_REG_ES]; \
89 1.1 fvdl (reg)->r_ds = (uc)->uc_mcontext.__gregs[_REG_DS]; \
90 1.1 fvdl (reg)->r_rdi = (uc)->uc_mcontext.__gregs[_REG_RDI]; \
91 1.1 fvdl (reg)->r_rsi = (uc)->uc_mcontext.__gregs[_REG_RSI]; \
92 1.1 fvdl (reg)->r_rbp = (uc)->uc_mcontext.__gregs[_REG_RBP]; \
93 1.1 fvdl (reg)->r_rbx = (uc)->uc_mcontext.__gregs[_REG_RBX]; \
94 1.1 fvdl (reg)->r_rdx = (uc)->uc_mcontext.__gregs[_REG_RDX]; \
95 1.1 fvdl (reg)->r_rcx = (uc)->uc_mcontext.__gregs[_REG_RCX]; \
96 1.1 fvdl (reg)->r_rax = (uc)->uc_mcontext.__gregs[_REG_RAX]; \
97 1.1 fvdl (reg)->r_r8 = (uc)->uc_mcontext.__gregs[_REG_R8]; \
98 1.1 fvdl (reg)->r_r9 = (uc)->uc_mcontext.__gregs[_REG_R9]; \
99 1.1 fvdl (reg)->r_r10 = (uc)->uc_mcontext.__gregs[_REG_R10]; \
100 1.1 fvdl (reg)->r_r11 = (uc)->uc_mcontext.__gregs[_REG_R11]; \
101 1.1 fvdl (reg)->r_r12 = (uc)->uc_mcontext.__gregs[_REG_R12]; \
102 1.1 fvdl (reg)->r_r13 = (uc)->uc_mcontext.__gregs[_REG_R13]; \
103 1.1 fvdl (reg)->r_r14 = (uc)->uc_mcontext.__gregs[_REG_R14]; \
104 1.1 fvdl (reg)->r_r15 = (uc)->uc_mcontext.__gregs[_REG_R15]; \
105 1.1 fvdl (reg)->r_rip = (uc)->uc_mcontext.__gregs[_REG_RIP]; \
106 1.1 fvdl (reg)->r_cs = (uc)->uc_mcontext.__gregs[_REG_CS]; \
107 1.1 fvdl (reg)->r_rflags = (uc)->uc_mcontext.__gregs[_REG_RFL]; \
108 1.1 fvdl (reg)->r_rsp = (uc)->uc_mcontext.__gregs[_REG_URSP]; \
109 1.1 fvdl (reg)->r_ss = (uc)->uc_mcontext.__gregs[_REG_SS]; \
110 1.1 fvdl } while (/*CONSTCOND*/0)
111 1.1 fvdl
112 1.1 fvdl #define PTHREAD_REG_TO_UCONTEXT(uc, reg) do { \
113 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_GS] = (reg)->r_gs; \
114 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_FS] = (reg)->r_fs; \
115 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_ES] = (reg)->r_es; \
116 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_DS] = (reg)->r_ds; \
117 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RDI] = (reg)->r_rdi; \
118 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RSI] = (reg)->r_rsi; \
119 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RBP] = (reg)->r_rbp; \
120 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RBX] = (reg)->r_rbx; \
121 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RDX] = (reg)->r_rdx; \
122 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RCX] = (reg)->r_rcx; \
123 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RAX] = (reg)->r_rax; \
124 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R8] = (reg)->r_r8; \
125 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R9] = (reg)->r_r9; \
126 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R10] = (reg)->r_r10; \
127 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R11] = (reg)->r_r11; \
128 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R12] = (reg)->r_r12; \
129 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R13] = (reg)->r_r13; \
130 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R14] = (reg)->r_r14; \
131 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_R15] = (reg)->r_r15; \
132 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RIP] = (reg)->r_rip; \
133 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_CS] = (reg)->r_cs; \
134 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_RFL] = (reg)->r_rflags; \
135 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_URSP]= (reg)->r_rsp; \
136 1.1 fvdl (uc)->uc_mcontext.__gregs[_REG_SS] = (reg)->r_ss; \
137 1.1 fvdl /*LINTED precision loss */ \
138 1.1 fvdl (uc)->uc_flags = ((uc)->uc_flags | _UC_CPU) & ~_UC_USER; \
139 1.1 fvdl } while (/*CONSTCOND*/0)
140 1.1 fvdl
141 1.1 fvdl
142 1.1 fvdl #define PTHREAD_UCONTEXT_TO_FPREG(freg, uc) \
143 1.1 fvdl (void)memcpy(&(freg)->fxstate, \
144 1.1 fvdl (uc)->uc_mcontext.__fpregs, sizeof(struct fpreg))
145 1.1 fvdl
146 1.1 fvdl #define PTHREAD_FPREG_TO_UCONTEXT(uc, freg) do { \
147 1.1 fvdl (void)memcpy( \
148 1.1 fvdl (uc)->uc_mcontext.__fpregs, \
149 1.1 fvdl &(freg)->fxstate, sizeof(struct fpreg)); \
150 1.1 fvdl /*LINTED precision loss */ \
151 1.1 fvdl (uc)->uc_flags = ((uc)->uc_flags | _UC_FPU) & ~_UC_USER; \
152 1.1 fvdl } while (/*CONSTCOND*/0)
153 1.1 fvdl
154 1.1 fvdl #endif /* _LIB_PTHREAD_X86_64_MD_H */
155