2 * Copyright (c) 2003 Jake Burkholder <jake@freebsd.org>.
3 * Copyright (c) 2003 Marcel Moolenaar
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Machine-dependent thread prototypes/definitions for the thread kernel.
33 #ifndef _PTHREAD_MD_H_
34 #define _PTHREAD_MD_H_
40 #define KSE_STACKSIZE 16384
41 #define DTV_OFFSET offsetof(struct tcb, tcb_tp.tp_tdv)
43 int _thr_setcontext(mcontext_t *, intptr_t, intptr_t *);
44 int _thr_getcontext(mcontext_t *);
46 #define THR_GETCONTEXT(ucp) _thr_getcontext(&(ucp)->uc_mcontext)
47 #define THR_SETCONTEXT(ucp) _thr_setcontext(&(ucp)->uc_mcontext, 0, NULL)
55 struct tdv; /* We don't know what this is yet? */
59 * %r6 points to one of these. We define the static TLS as an array
60 * of long double to enforce 16-byte alignment of the TLS memory.
62 * XXX - Both static and dynamic allocation of any of these structures
63 * will result in a valid, well-aligned thread pointer???
66 struct tdv *tp_tdv; /* dynamic TLS */
70 struct pthread *tcb_thread;
71 struct kcb *tcb_curkcb;
73 struct kse_thr_mailbox tcb_tmbx; /* needs 32-byte alignment */
78 struct kse_mailbox kcb_kmbx;
79 struct tcb kcb_faketcb;
80 struct tcb *kcb_curtcb;
84 extern struct arm_tp **arm_tp;
87 #define _tcb ((struct tcb*)((char*)(_tp) - offsetof(struct tcb, tcb_tp)))
90 * The kcb and tcb constructors.
92 struct tcb *_tcb_ctor(struct pthread *, int);
93 void _tcb_dtor(struct tcb *);
94 struct kcb *_kcb_ctor(struct kse *kse);
95 void _kcb_dtor(struct kcb *);
97 static __inline uint32_t
98 __kcb_swp(uint32_t val, void *ptr)
101 __asm __volatile("swp %0, %1, [%2]"
102 : "=r" (val) : "r" (val) , "r" (ptr) : "memory");
106 /* Called from the KSE to set its private data. */
108 _kcb_set(struct kcb *kcb)
110 /* There is no thread yet; use the fake tcb. */
111 __kcb_swp((uint32_t)&kcb->kcb_faketcb.tcb_tp, &_tp);
115 * Get the current kcb.
117 * This can only be called while in a critical region; don't
118 * worry about having the kcb changed out from under us.
120 static __inline struct kcb *
123 return (_tcb->tcb_curkcb);
127 * Enter a critical region.
129 * Read and clear km_curthread in the kse mailbox.
131 static __inline struct kse_thr_mailbox *
132 _kcb_critical_enter(void)
134 struct kse_thr_mailbox *crit;
136 if (_tcb->tcb_isfake)
138 crit = (struct kse_thr_mailbox *)__kcb_swp((uint32_t)NULL,
139 &_tcb->tcb_curkcb->kcb_kmbx.km_curthread);
144 _kcb_critical_leave(struct kse_thr_mailbox *crit)
147 if (_tcb->tcb_isfake == 0)
148 __kcb_swp((uint32_t)crit,
149 &_tcb->tcb_curkcb->kcb_kmbx.km_curthread);
153 _kcb_in_critical(void)
158 return (_tcb->tcb_curkcb->kcb_kmbx.km_curthread == NULL);
159 if (_tcb->tcb_isfake != 0) {
161 * We are in a critical region since there is no
166 flags = _tcb->tcb_tmbx.tm_flags;
167 _tcb->tcb_tmbx.tm_flags |= TMF_NOUPCALL;
168 ret = (_tcb->tcb_curkcb->kcb_kmbx.km_curthread == NULL);
169 _tcb->tcb_tmbx.tm_flags = flags;
175 _tcb_set(struct kcb *kcb, struct tcb *tcb)
178 tcb = &kcb->kcb_faketcb;
179 __kcb_swp((uint32_t)&tcb->tcb_tp, &_tp);
180 kcb->kcb_curtcb = tcb;
181 tcb->tcb_curkcb = kcb;
184 static __inline struct tcb *
190 static __inline struct pthread *
193 return (_tcb->tcb_thread);
197 * Get the current kse.
199 * Like _kcb_get(), this can only be called while in a critical region.
201 static __inline struct kse *
204 return (_tcb->tcb_curkcb->kcb_kse);
207 void _arm_enter_uts(struct kse_mailbox *km, kse_func_t uts, void *stack,
211 _thread_enter_uts(struct tcb *tcb, struct kcb *kcb)
215 if ((ret = _thr_getcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext))
217 kcb->kcb_curtcb = &kcb->kcb_faketcb;
218 __kcb_swp((int)&kcb->kcb_faketcb.tcb_tp, &_tp);
219 _arm_enter_uts(&kcb->kcb_kmbx, kcb->kcb_kmbx.km_func,
220 kcb->kcb_kmbx.km_stack.ss_sp,
221 kcb->kcb_kmbx.km_stack.ss_size);
222 /* We should not reach here. */
230 _thread_switch(struct kcb *kcb, struct tcb *tcb, int setmbox)
232 extern int _libkse_debug;
238 mc = &tcb->tcb_tmbx.tm_context.uc_mcontext;
239 if (_libkse_debug == 0) {
240 tcb->tcb_tmbx.tm_lwp = kcb->kcb_kmbx.km_lwp;
242 _thr_setcontext(mc, (intptr_t)&tcb->tcb_tmbx,
243 (intptr_t *)&kcb->kcb_kmbx.km_curthread);
245 _thr_setcontext(mc, 0, NULL);
248 kse_switchin(&tcb->tcb_tmbx, KSE_SWITCHIN_SETTMBX);
250 kse_switchin(&tcb->tcb_tmbx, 0);
253 /* We should not reach here. */
257 #endif /* _PTHREAD_MD_H_ */