1144518Sdavidxu/*-
2144518Sdavidxu * Copyright (c) 2002 Daniel Eischen <deischen@freebsd.org>.
3144518Sdavidxu * Copyright (c) 2005 David Xu <davidxu@freebsd.org>.
4144518Sdavidxu * All rights reserved.
5144518Sdavidxu *
6144518Sdavidxu * Redistribution and use in source and binary forms, with or without
7144518Sdavidxu * modification, are permitted provided that the following conditions
8144518Sdavidxu * are met:
9144518Sdavidxu * 1. Redistributions of source code must retain the above copyright
10144518Sdavidxu *    notice, this list of conditions and the following disclaimer.
11144518Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright
12144518Sdavidxu *    notice, this list of conditions and the following disclaimer in the
13144518Sdavidxu *    documentation and/or other materials provided with the distribution.
14144518Sdavidxu *
15144518Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16144518Sdavidxu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17144518Sdavidxu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18144518Sdavidxu * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19144518Sdavidxu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20144518Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21144518Sdavidxu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22144518Sdavidxu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23144518Sdavidxu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24144518Sdavidxu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25144518Sdavidxu * SUCH DAMAGE.
26144518Sdavidxu *
27144518Sdavidxu * $FreeBSD$
28144518Sdavidxu */
29144518Sdavidxu
30144518Sdavidxu/*
31144518Sdavidxu * Machine-dependent thread prototypes/definitions.
32144518Sdavidxu */
33144518Sdavidxu#ifndef _PTHREAD_MD_H_
34144518Sdavidxu#define	_PTHREAD_MD_H_
35144518Sdavidxu
36144518Sdavidxu#include <stddef.h>
37144518Sdavidxu#include <sys/types.h>
38144518Sdavidxu#include <machine/sysarch.h>
39144518Sdavidxu
40165241Sdavidxu#define	CPU_SPINWAIT		__asm __volatile("pause")
41165241Sdavidxu
42144518Sdavidxu#define	DTV_OFFSET		offsetof(struct tcb, tcb_dtv)
43144518Sdavidxu
44144518Sdavidxu/*
45144518Sdavidxu * Variant II tcb, first two members are required by rtld,
46144518Sdavidxu * %gs points to the structure.
47144518Sdavidxu */
48144518Sdavidxustruct tcb {
49144518Sdavidxu	struct tcb		*tcb_self;	/* required by rtld */
50144518Sdavidxu	void			*tcb_dtv;	/* required by rtld */
51144518Sdavidxu	struct pthread		*tcb_thread;
52144518Sdavidxu};
53144518Sdavidxu
54144518Sdavidxu/*
55144518Sdavidxu * Evaluates to the byte offset of the per-tcb variable name.
56144518Sdavidxu */
57144518Sdavidxu#define	__tcb_offset(name)	__offsetof(struct tcb, name)
58144518Sdavidxu
59144518Sdavidxu/*
60144518Sdavidxu * Evaluates to the type of the per-tcb variable name.
61144518Sdavidxu */
62144518Sdavidxu#define	__tcb_type(name)	__typeof(((struct tcb *)0)->name)
63144518Sdavidxu
64144518Sdavidxu/*
65144518Sdavidxu * Evaluates to the value of the per-tcb variable name.
66144518Sdavidxu */
67144518Sdavidxu#define	TCB_GET32(name) ({					\
68144518Sdavidxu	__tcb_type(name) __result;				\
69144518Sdavidxu								\
70144518Sdavidxu	u_int __i;						\
71144518Sdavidxu	__asm __volatile("movl %%gs:%1, %0"			\
72144518Sdavidxu	    : "=r" (__i)					\
73228536Sdim	    : "m" (*(volatile u_int *)(__tcb_offset(name))));	\
74144518Sdavidxu	__result = (__tcb_type(name))__i;			\
75144518Sdavidxu								\
76144518Sdavidxu	__result;						\
77144518Sdavidxu})
78144518Sdavidxu
79144518Sdavidxu/* Called from the thread to set its private data. */
80144518Sdavidxustatic __inline void
81144518Sdavidxu_tcb_set(struct tcb *tcb)
82144518Sdavidxu{
83145437Sdavidxu 	i386_set_gsbase(tcb);
84144518Sdavidxu}
85144518Sdavidxu
86144518Sdavidxu/* Get the current kcb. */
87144518Sdavidxustatic __inline struct tcb *
88144518Sdavidxu_tcb_get(void)
89144518Sdavidxu{
90144518Sdavidxu	return (TCB_GET32(tcb_self));
91144518Sdavidxu}
92144518Sdavidxu
93144518Sdavidxu/* Get the current thread. */
94144518Sdavidxustatic __inline struct pthread *
95144518Sdavidxu_get_curthread(void)
96144518Sdavidxu{
97157461Sdavidxu	return (TCB_GET32(tcb_thread));
98144518Sdavidxu}
99177853Sdavidxu
100177853Sdavidxu#define HAS__UMTX_OP_ERR	1
101177853Sdavidxu
102144518Sdavidxu#endif
103