pthread_md.h revision 1.17.2.2 1 1.17.2.2 bouyer /* $NetBSD: pthread_md.h,v 1.17.2.2 2011/03/05 15:09:22 bouyer Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.12 ad * Copyright (c) 2001, 2007, 2008 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.9 ad * by Nathan J. Williams, and by Andrew Doran.
9 1.2 thorpej *
10 1.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.2 thorpej * modification, are permitted provided that the following conditions
12 1.2 thorpej * are met:
13 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.2 thorpej *
19 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.2 thorpej */
31 1.2 thorpej
32 1.2 thorpej #ifndef _LIB_PTHREAD_I386_MD_H
33 1.2 thorpej #define _LIB_PTHREAD_I386_MD_H
34 1.2 thorpej
35 1.2 thorpej #include <sys/ucontext.h>
36 1.7 cube #include <ucontext.h>
37 1.2 thorpej
38 1.17.2.1 bouyer static inline unsigned long
39 1.2 thorpej pthread__sp(void)
40 1.2 thorpej {
41 1.17.2.1 bouyer unsigned long ret;
42 1.2 thorpej __asm("movl %%esp, %0" : "=g" (ret));
43 1.2 thorpej
44 1.2 thorpej return ret;
45 1.2 thorpej }
46 1.2 thorpej
47 1.2 thorpej #define pthread__uc_sp(ucp) ((ucp)->uc_mcontext.__gregs[_REG_UESP])
48 1.2 thorpej
49 1.2 thorpej /*
50 1.2 thorpej * Set initial, sane values for registers whose values aren't just
51 1.2 thorpej * "don't care".
52 1.7 cube *
53 1.7 cube * We use the current context instead of a guessed one because we cannot
54 1.7 cube * assume how the GDT entries are ordered: what is true on i386 is not
55 1.7 cube * true anymore on amd64.
56 1.2 thorpej */
57 1.2 thorpej #define _INITCONTEXT_U_MD(ucp) \
58 1.7 cube do { \
59 1.7 cube ucontext_t ucur; \
60 1.7 cube (void)getcontext(&ucur); \
61 1.7 cube (ucp)->uc_mcontext.__gregs[_REG_GS] = \
62 1.7 cube ucur.uc_mcontext.__gregs[_REG_GS], \
63 1.7 cube (ucp)->uc_mcontext.__gregs[_REG_FS] = \
64 1.7 cube ucur.uc_mcontext.__gregs[_REG_FS], \
65 1.7 cube (ucp)->uc_mcontext.__gregs[_REG_ES] = \
66 1.7 cube ucur.uc_mcontext.__gregs[_REG_ES], \
67 1.7 cube (ucp)->uc_mcontext.__gregs[_REG_DS] = \
68 1.7 cube ucur.uc_mcontext.__gregs[_REG_DS], \
69 1.7 cube (ucp)->uc_mcontext.__gregs[_REG_CS] = \
70 1.7 cube ucur.uc_mcontext.__gregs[_REG_CS], \
71 1.7 cube (ucp)->uc_mcontext.__gregs[_REG_SS] = \
72 1.7 cube ucur.uc_mcontext.__gregs[_REG_SS], \
73 1.7 cube (ucp)->uc_mcontext.__gregs[_REG_EFL] = \
74 1.7 cube ucur.uc_mcontext.__gregs[_REG_EFL]; \
75 1.7 cube } while (/*CONSTCOND*/0);
76 1.2 thorpej
77 1.8 ad #define pthread__smt_pause() __asm __volatile("rep; nop" ::: "memory")
78 1.11 ad
79 1.12 ad /* Don't need additional memory barriers. */
80 1.12 ad #define PTHREAD__ATOMIC_IS_MEMBAR
81 1.12 ad
82 1.13 ad static inline void *
83 1.13 ad _atomic_cas_ptr(volatile void *ptr, void *old, void *new)
84 1.13 ad {
85 1.13 ad volatile uintptr_t *cast = ptr;
86 1.13 ad void *ret;
87 1.13 ad
88 1.13 ad __asm __volatile ("lock; cmpxchgl %2, %1"
89 1.13 ad : "=a" (ret), "=m" (*cast)
90 1.13 ad : "r" (new), "m" (*cast), "0" (old));
91 1.13 ad
92 1.13 ad return ret;
93 1.13 ad }
94 1.13 ad
95 1.13 ad static inline void *
96 1.13 ad _atomic_cas_ptr_ni(volatile void *ptr, void *old, void *new)
97 1.13 ad {
98 1.13 ad volatile uintptr_t *cast = ptr;
99 1.13 ad void *ret;
100 1.13 ad
101 1.13 ad __asm __volatile ("cmpxchgl %2, %1"
102 1.13 ad : "=a" (ret), "=m" (*cast)
103 1.13 ad : "r" (new), "m" (*cast), "0" (old));
104 1.13 ad
105 1.13 ad return ret;
106 1.13 ad }
107 1.13 ad
108 1.2 thorpej #endif /* _LIB_PTHREAD_I386_MD_H */
109