pthread_md.h revision 132125
1/*-
2 * Copyright (c) 2002 Daniel Eischen <deischen@freebsd.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/lib/libkse/arch/i386/include/pthread_md.h 132125 2004-07-13 22:54:23Z davidxu $
27 */
28/*
29 * Machine-dependent thread prototypes/definitions for the thread kernel.
30 */
31#ifndef _PTHREAD_MD_H_
32#define	_PTHREAD_MD_H_
33
34#include <sys/kse.h>
35#include <ucontext.h>
36
37extern int _thr_setcontext(mcontext_t *, intptr_t, intptr_t *);
38extern int _thr_getcontext(mcontext_t *);
39
40#define	KSE_STACKSIZE		16384
41
42#define	THR_GETCONTEXT(ucp)	_thr_getcontext(&(ucp)->uc_mcontext)
43#define	THR_SETCONTEXT(ucp)	_thr_setcontext(&(ucp)->uc_mcontext, 0, NULL)
44
45#define	PER_KSE
46#undef	PER_THREAD
47
48struct kse;
49struct pthread;
50struct tdv;
51
52/*
53 * %gs points to a struct kcb.
54 */
55struct kcb {
56	struct tcb		*kcb_curtcb;
57	struct kcb		*kcb_self;	/* self reference */
58	int			kcb_ldt;
59	struct kse		*kcb_kse;
60	struct kse_mailbox	kcb_kmbx;
61};
62
63struct tcb {
64	struct tdv		*tcb_tdv;
65	struct pthread		*tcb_thread;
66	void			*tcb_addr;	/* allocated tcb address */
67	void			*tcb_spare;	/* align tcb_tmbx to 16 bytes */
68	struct kse_thr_mailbox	tcb_tmbx;
69};
70
71/*
72 * Evaluates to the byte offset of the per-kse variable name.
73 */
74#define	__kcb_offset(name)	__offsetof(struct kcb, name)
75
76/*
77 * Evaluates to the type of the per-kse variable name.
78 */
79#define	__kcb_type(name)	__typeof(((struct kcb *)0)->name)
80
81/*
82 * Evaluates to the value of the per-kse variable name.
83 */
84#define	KCB_GET32(name) ({					\
85	__kcb_type(name) __result;				\
86								\
87	u_int __i;						\
88	__asm __volatile("movl %%gs:%1, %0"			\
89	    : "=r" (__i)					\
90	    : "m" (*(u_int *)(__kcb_offset(name))));		\
91	__result = *(__kcb_type(name) *)&__i;			\
92								\
93	__result;						\
94})
95
96/*
97 * Sets the value of the per-kse variable name to value val.
98 */
99#define	KCB_SET32(name, val) ({					\
100	__kcb_type(name) __val = (val);				\
101								\
102	u_int __i;						\
103	__i = *(u_int *)&__val;					\
104	__asm __volatile("movl %1,%%gs:%0"			\
105	    : "=m" (*(u_int *)(__kcb_offset(name)))		\
106	    : "r" (__i));					\
107})
108
109static __inline u_long
110__kcb_readandclear32(volatile u_long *addr)
111{
112	u_long result;
113
114	__asm __volatile (
115	    "	xorl	%0, %0;"
116	    "	xchgl	%%gs:%1, %0;"
117	    "# __kcb_readandclear32"
118	    : "=&r" (result)
119	    : "m" (*addr));
120	return (result);
121}
122
123#define	KCB_READANDCLEAR32(name) ({				\
124	__kcb_type(name) __result;				\
125								\
126	__result = (__kcb_type(name))				\
127	    __kcb_readandclear32((u_long *)__kcb_offset(name)); \
128	__result;						\
129})
130
131
132#define	_kcb_curkcb()		KCB_GET32(kcb_self)
133#define	_kcb_curtcb()		KCB_GET32(kcb_curtcb)
134#define	_kcb_curkse()		((struct kse *)KCB_GET32(kcb_kmbx.km_udata))
135#define	_kcb_get_tmbx()		KCB_GET32(kcb_kmbx.km_curthread)
136#define	_kcb_set_tmbx(value)	KCB_SET32(kcb_kmbx.km_curthread, (void *)value)
137#define	_kcb_readandclear_tmbx() KCB_READANDCLEAR32(kcb_kmbx.km_curthread)
138
139
140/*
141 * The constructors.
142 */
143struct tcb	*_tcb_ctor(struct pthread *);
144void		_tcb_dtor(struct tcb *tcb);
145struct kcb	*_kcb_ctor(struct kse *);
146void		_kcb_dtor(struct kcb *);
147
148/* Called from the KSE to set its private data. */
149static __inline void
150_kcb_set(struct kcb *kcb)
151{
152	int val;
153
154	val = (kcb->kcb_ldt << 3) | 7;
155	__asm __volatile("movl %0, %%gs" : : "r" (val));
156}
157
158/* Get the current kcb. */
159static __inline struct kcb *
160_kcb_get(void)
161{
162	return (_kcb_curkcb());
163}
164
165static __inline struct kse_thr_mailbox *
166_kcb_critical_enter(void)
167{
168	struct kse_thr_mailbox *crit;
169
170	crit = _kcb_readandclear_tmbx();
171	return (crit);
172}
173
174static __inline void
175_kcb_critical_leave(struct kse_thr_mailbox *crit)
176{
177	_kcb_set_tmbx(crit);
178}
179
180static __inline int
181_kcb_in_critical(void)
182{
183	return (_kcb_get_tmbx() == NULL);
184}
185
186static __inline void
187_tcb_set(struct kcb *kcb, struct tcb *tcb)
188{
189	kcb->kcb_curtcb = tcb;
190}
191
192static __inline struct tcb *
193_tcb_get(void)
194{
195	return (_kcb_curtcb());
196}
197
198static __inline struct pthread *
199_get_curthread(void)
200{
201	struct tcb *tcb;
202
203	tcb = _kcb_curtcb();
204	if (tcb != NULL)
205		return (tcb->tcb_thread);
206	else
207		return (NULL);
208}
209
210static __inline struct kse *
211_get_curkse(void)
212{
213	return ((struct kse *)_kcb_curkse());
214}
215
216void	_i386_enter_uts(struct kse_mailbox *km, kse_func_t uts, void *stack,
217    size_t stacksz);
218
219static __inline int
220_thread_enter_uts(struct tcb *tcb, struct kcb *kcb)
221{
222	int ret;
223
224	ret = _thr_getcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext);
225	if (ret == 0) {
226		_i386_enter_uts(&kcb->kcb_kmbx, kcb->kcb_kmbx.km_func,
227		    kcb->kcb_kmbx.km_stack.ss_sp,
228		    kcb->kcb_kmbx.km_stack.ss_size);
229		/* We should not reach here. */
230		return (-1);
231	}
232	else if (ret < 0)
233		return (-1);
234	return (0);
235}
236
237static __inline int
238_thread_switch(struct kcb *kcb, struct tcb *tcb, int setmbox)
239{
240	extern int _libkse_debug;
241
242	if ((kcb == NULL) || (tcb == NULL))
243		return (-1);
244	kcb->kcb_curtcb = tcb;
245	if (_libkse_debug == 0) {
246		tcb->tcb_tmbx.tm_lwp = kcb->kcb_kmbx.km_lwp;
247		if (setmbox != 0)
248			_thr_setcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext,
249			    (intptr_t)&tcb->tcb_tmbx,
250			    (intptr_t *)&kcb->kcb_kmbx.km_curthread);
251		else
252			_thr_setcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext,
253				0, NULL);
254	} else {
255		if (setmbox)
256			kse_switchin(&tcb->tcb_tmbx, KSE_SWITCHIN_SETTMBX);
257		else
258			kse_switchin(&tcb->tcb_tmbx, 0);
259	}
260
261	/* We should not reach here. */
262	return (-1);
263}
264
265#endif
266