1/* $NetBSD: pthread_md.h,v 1.20 2012/03/02 23:19:47 joerg Exp $ */
2
3/*-
4 * Copyright (c) 2001, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _LIB_PTHREAD_I386_MD_H
33#define _LIB_PTHREAD_I386_MD_H
34
35#include <sys/ucontext.h>
36#include <ucontext.h>
37
38static inline unsigned long
39pthread__sp(void)
40{
41 unsigned long ret;
42 __asm("movl %%esp, %0" : "=g" (ret));
43
44 return ret;
45}
46
47#define pthread__uc_sp(ucp) ((ucp)->uc_mcontext.__gregs[_REG_UESP])
48
49static inline void
50_initcontext_u_md(ucontext_t *ucp)
51{
52 __asm ("pushfl; popl %0" : "=a" (ucp->uc_mcontext.__gregs[_REG_EFL]));
53 __asm ("pushl %%cs; popl %0" : "=a" (ucp->uc_mcontext.__gregs[_REG_CS]));
54 __asm ("movl %%ds, %0" : "=a" (ucp->uc_mcontext.__gregs[_REG_DS]));
55 __asm ("movl %%es, %0" : "=a" (ucp->uc_mcontext.__gregs[_REG_ES]));
56 __asm ("movl %%fs, %0" : "=a" (ucp->uc_mcontext.__gregs[_REG_FS]));
57 __asm ("movl %%gs, %0" : "=a" (ucp->uc_mcontext.__gregs[_REG_GS]));
58 __asm ("movl %%ss, %0" : "=a" (ucp->uc_mcontext.__gregs[_REG_SS]));
59}
60
61#define _INITCONTEXT_U_MD(ucp) _initcontext_u_md(ucp);
62
63#define pthread__smt_pause() __asm __volatile("rep; nop" ::: "memory")
64
65/* Don't need additional memory barriers. */
66#define PTHREAD__ATOMIC_IS_MEMBAR
67
68static inline void *
69_atomic_cas_ptr(volatile void *ptr, void *old, void *new)
70{
71 volatile uintptr_t *cast = ptr;
72 void *ret;
73
74 __asm __volatile ("lock; cmpxchgl %2, %1"
75 : "=a" (ret), "=m" (*cast)
76 : "r" (new), "m" (*cast), "0" (old));
77
78 return ret;
79}
80
81static inline void *
82_atomic_cas_ptr_ni(volatile void *ptr, void *old, void *new)
83{
84 volatile uintptr_t *cast = ptr;
85 void *ret;
86
87 __asm __volatile ("cmpxchgl %2, %1"
88 : "=a" (ret), "=m" (*cast)
89 : "r" (new), "m" (*cast), "0" (old));
90
91 return ret;
92}
93
94#endif /* _LIB_PTHREAD_I386_MD_H */
95