pthread_md.h revision 132400
1/* 2 * Copyright 2004 by Peter Grehan. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/lib/libkse/arch/powerpc/include/pthread_md.h 132400 2004-07-19 12:19:04Z grehan $ 28 */ 29 30/* 31 * Machine-dependent thread prototypes/definitions for the thread kernel. 32 */ 33#ifndef _PTHREAD_MD_H_ 34#define _PTHREAD_MD_H_ 35 36#include <sys/kse.h> 37#include <stddef.h> 38#include <ucontext.h> 39 40extern void _ppc32_enter_uts(struct kse_mailbox *, kse_func_t, void *, size_t); 41extern int _ppc32_setcontext(mcontext_t *, intptr_t, intptr_t *); 42extern int _ppc32_getcontext(mcontext_t *); 43 44#define KSE_STACKSIZE 16384 45 46#define THR_GETCONTEXT(ucp) _ppc32_getcontext(&(ucp)->uc_mcontext) 47#define THR_SETCONTEXT(ucp) _ppc32_setcontext(&(ucp)->uc_mcontext, 0, NULL) 48 49#define PER_THREAD 50 51struct kcb; 52struct kse; 53struct pthread; 54struct tcb; 55struct tdv; 56 57/* 58 * %r2 points to a struct kcb. 59 */ 60struct ppc32_tp { 61 struct tdv *tp_tdv; /* dynamic TLS */ 62 uint32_t _reserved_; 63 long double tp_tls[0]; /* static TLS */ 64}; 65 66struct tcb { 67 struct kse_thr_mailbox tcb_tmbx; 68 struct pthread *tcb_thread; 69 struct kcb *tcb_curkcb; 70 long tcb_isfake; 71 struct ppc32_tp tcb_tp; 72}; 73 74struct kcb { 75 struct kse_mailbox kcb_kmbx; 76 struct tcb kcb_faketcb; 77 struct tcb *kcb_curtcb; 78 struct kse *kcb_kse; 79}; 80 81/* 82 * From the PowerPC32 TLS spec: 83 * 84 * "r2 is the thread pointer, and points 0x7000 past the end of the 85 * thread control block." Or, 0x7008 past the start of the 8-byte tcb 86 */ 87#define TP_OFFSET 0x7008 88register uint8_t *_tpr __asm("%r2"); 89 90#define _tcb ((struct tcb *)(_tpr - TP_OFFSET - offsetof(struct tcb, tcb_tp))) 91 92/* 93 * The kcb and tcb constructors. 94 */ 95struct tcb *_tcb_ctor(struct pthread *); 96void _tcb_dtor(struct tcb *); 97struct kcb *_kcb_ctor(struct kse *kse); 98void _kcb_dtor(struct kcb *); 99 100/* Called from the KSE to set its private data. */ 101static __inline void 102_kcb_set(struct kcb *kcb) 103{ 104 /* There is no thread yet; use the fake tcb. */ 105 _tpr = (uint8_t *)&kcb->kcb_faketcb.tcb_tp + TP_OFFSET; 106} 107 108/* 109 * Get the current kcb. 110 * 111 * This can only be called while in a critical region; don't 112 * worry about having the kcb changed out from under us. 113 */ 114static __inline struct kcb * 115_kcb_get(void) 116{ 117 return (_tcb->tcb_curkcb); 118} 119 120/* 121 * Enter a critical region. 122 * 123 * Read and clear km_curthread in the kse mailbox. 124 */ 125static __inline struct kse_thr_mailbox * 126_kcb_critical_enter(void) 127{ 128 struct kse_thr_mailbox *crit; 129 uint32_t flags; 130 131 if (_tcb->tcb_isfake != 0) { 132 /* 133 * We already are in a critical region since 134 * there is no current thread. 135 */ 136 crit = NULL; 137 } else { 138 flags = _tcb->tcb_tmbx.tm_flags; 139 _tcb->tcb_tmbx.tm_flags |= TMF_NOUPCALL; 140 crit = _tcb->tcb_curkcb->kcb_kmbx.km_curthread; 141 _tcb->tcb_curkcb->kcb_kmbx.km_curthread = NULL; 142 _tcb->tcb_tmbx.tm_flags = flags; 143 } 144 return (crit); 145} 146 147static __inline void 148_kcb_critical_leave(struct kse_thr_mailbox *crit) 149{ 150 /* No need to do anything if this is a fake tcb. */ 151 if (_tcb->tcb_isfake == 0) 152 _tcb->tcb_curkcb->kcb_kmbx.km_curthread = crit; 153} 154 155static __inline int 156_kcb_in_critical(void) 157{ 158 uint32_t flags; 159 int ret; 160 161 if (_tcb->tcb_isfake != 0) { 162 /* 163 * We are in a critical region since there is no 164 * current thread. 165 */ 166 ret = 1; 167 } else { 168 flags = _tcb->tcb_tmbx.tm_flags; 169 _tcb->tcb_tmbx.tm_flags |= TMF_NOUPCALL; 170 ret = (_tcb->tcb_curkcb->kcb_kmbx.km_curthread == NULL); 171 _tcb->tcb_tmbx.tm_flags = flags; 172 } 173 return (ret); 174} 175 176static __inline void 177_tcb_set(struct kcb *kcb, struct tcb *tcb) 178{ 179 if (tcb == NULL) 180 tcb = &kcb->kcb_faketcb; 181 kcb->kcb_curtcb = tcb; 182 tcb->tcb_curkcb = kcb; 183 _tpr = (uint8_t *)&tcb->tcb_tp + TP_OFFSET; 184} 185 186static __inline struct tcb * 187_tcb_get(void) 188{ 189 return (_tcb); 190} 191 192static __inline struct pthread * 193_get_curthread(void) 194{ 195 return (_tcb->tcb_thread); 196} 197 198/* 199 * Get the current kse. 200 * 201 * Like _kcb_get(), this can only be called while in a critical region. 202 */ 203static __inline struct kse * 204_get_curkse(void) 205{ 206 return (_tcb->tcb_curkcb->kcb_kse); 207} 208 209static __inline int 210_thread_enter_uts(struct tcb *tcb, struct kcb *kcb) 211{ 212 if (_ppc32_getcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext) == 0) { 213 /* Make the fake tcb the current thread. */ 214 kcb->kcb_curtcb = &kcb->kcb_faketcb; 215 _tpr = (uint8_t *)&kcb->kcb_faketcb.tcb_tp + TP_OFFSET; 216 _ppc32_enter_uts(&kcb->kcb_kmbx, kcb->kcb_kmbx.km_func, 217 kcb->kcb_kmbx.km_stack.ss_sp, 218 kcb->kcb_kmbx.km_stack.ss_size - 32); 219 /* We should not reach here. */ 220 return (-1); 221 } 222 return (0); 223} 224 225static __inline int 226_thread_switch(struct kcb *kcb, struct tcb *tcb, int setmbox) 227{ 228 mcontext_t *mc; 229 extern int _libkse_debug; 230 231 _tcb_set(kcb, tcb); 232 mc = &tcb->tcb_tmbx.tm_context.uc_mcontext; 233 234 /* 235 * A full context needs a system call to restore, so use 236 * kse_switchin. Otherwise, the partial context can be 237 * restored with _ppc32_setcontext 238 */ 239 if (mc->mc_vers != _MC_VERSION_KSE && _libkse_debug != 0) { 240 if (setmbox) 241 kse_switchin(&tcb->tcb_tmbx, KSE_SWITCHIN_SETTMBX); 242 else 243 kse_switchin(&tcb->tcb_tmbx, 0); 244 } else { 245 tcb->tcb_tmbx.tm_lwp = kcb->kcb_kmbx.km_lwp; 246 if (setmbox) 247 _ppc32_setcontext(mc, (intptr_t)&tcb->tcb_tmbx, 248 (intptr_t *)&kcb->kcb_kmbx.km_curthread); 249 else 250 _ppc32_setcontext(mc, 0, NULL); 251 } 252 253 /* We should not reach here. */ 254 return (-1); 255} 256 257#endif /* _PTHREAD_MD_H_ */ 258