pthread_md.h revision 137295
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 137295 2004-11-06 03:35:51Z peter $ 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 <stddef.h> 35#include <sys/types.h> 36#include <sys/kse.h> 37#include <machine/sysarch.h> 38#include <ucontext.h> 39 40extern int _thr_setcontext(mcontext_t *, intptr_t, intptr_t *); 41extern int _thr_getcontext(mcontext_t *); 42 43#define KSE_STACKSIZE 16384 44#define DTV_OFFSET offsetof(struct tcb, tcb_dtv) 45 46#define THR_GETCONTEXT(ucp) _thr_getcontext(&(ucp)->uc_mcontext) 47#define THR_SETCONTEXT(ucp) _thr_setcontext(&(ucp)->uc_mcontext, 0, NULL) 48 49#define PER_KSE 50#undef PER_THREAD 51 52struct kse; 53struct pthread; 54 55/* 56 * %gs points to a struct kcb. 57 */ 58struct kcb { 59 struct tcb *kcb_curtcb; 60 struct kcb *kcb_self; /* self reference */ 61 int kcb_ldt; 62 struct kse *kcb_kse; 63 struct kse_mailbox kcb_kmbx; 64}; 65 66struct tcb { 67 struct tcb *tcb_self; /* required by rtld */ 68 void *tcb_dtv; /* required by rtld */ 69 struct pthread *tcb_thread; 70 void *tcb_spare; /* align tcb_tmbx to 16 bytes */ 71 struct kse_thr_mailbox tcb_tmbx; 72}; 73 74/* 75 * Evaluates to the byte offset of the per-kse variable name. 76 */ 77#define __kcb_offset(name) __offsetof(struct kcb, name) 78 79/* 80 * Evaluates to the type of the per-kse variable name. 81 */ 82#define __kcb_type(name) __typeof(((struct kcb *)0)->name) 83 84/* 85 * Evaluates to the value of the per-kse variable name. 86 */ 87#define KCB_GET32(name) ({ \ 88 __kcb_type(name) __result; \ 89 \ 90 u_int __i; \ 91 __asm __volatile("movl %%gs:%1, %0" \ 92 : "=r" (__i) \ 93 : "m" (*(u_int *)(__kcb_offset(name)))); \ 94 __result = (__kcb_type(name))__i; \ 95 \ 96 __result; \ 97}) 98 99/* 100 * Sets the value of the per-kse variable name to value val. 101 */ 102#define KCB_SET32(name, val) ({ \ 103 __kcb_type(name) __val = (val); \ 104 \ 105 u_int __i; \ 106 __i = (u_int)__val; \ 107 __asm __volatile("movl %1,%%gs:%0" \ 108 : "=m" (*(u_int *)(__kcb_offset(name))) \ 109 : "r" (__i)); \ 110}) 111 112static __inline u_long 113__kcb_readandclear32(volatile u_long *addr) 114{ 115 u_long result; 116 117 __asm __volatile ( 118 " xorl %0, %0;" 119 " xchgl %%gs:%1, %0;" 120 "# __kcb_readandclear32" 121 : "=&r" (result) 122 : "m" (*addr)); 123 return (result); 124} 125 126#define KCB_READANDCLEAR32(name) ({ \ 127 __kcb_type(name) __result; \ 128 \ 129 __result = (__kcb_type(name)) \ 130 __kcb_readandclear32((u_long *)__kcb_offset(name)); \ 131 __result; \ 132}) 133 134 135#define _kcb_curkcb() KCB_GET32(kcb_self) 136#define _kcb_curtcb() KCB_GET32(kcb_curtcb) 137#define _kcb_curkse() ((struct kse *)KCB_GET32(kcb_kmbx.km_udata)) 138#define _kcb_get_tmbx() KCB_GET32(kcb_kmbx.km_curthread) 139#define _kcb_set_tmbx(value) KCB_SET32(kcb_kmbx.km_curthread, (void *)value) 140#define _kcb_readandclear_tmbx() KCB_READANDCLEAR32(kcb_kmbx.km_curthread) 141 142 143/* 144 * The constructors. 145 */ 146struct tcb *_tcb_ctor(struct pthread *, int); 147void _tcb_dtor(struct tcb *tcb); 148struct kcb *_kcb_ctor(struct kse *); 149void _kcb_dtor(struct kcb *); 150 151/* Called from the KSE to set its private data. */ 152static __inline void 153_kcb_set(struct kcb *kcb) 154{ 155#ifndef COMPAT_32BIT 156 int val; 157 158 val = (kcb->kcb_ldt << 3) | 7; 159 __asm __volatile("movl %0, %%gs" : : "r" (val)); 160#else 161 _amd64_set_gsbase(kcb); 162#endif 163 164} 165 166/* Get the current kcb. */ 167static __inline struct kcb * 168_kcb_get(void) 169{ 170 return (_kcb_curkcb()); 171} 172 173static __inline struct kse_thr_mailbox * 174_kcb_critical_enter(void) 175{ 176 struct kse_thr_mailbox *crit; 177 178 crit = _kcb_readandclear_tmbx(); 179 return (crit); 180} 181 182static __inline void 183_kcb_critical_leave(struct kse_thr_mailbox *crit) 184{ 185 _kcb_set_tmbx(crit); 186} 187 188static __inline int 189_kcb_in_critical(void) 190{ 191 return (_kcb_get_tmbx() == NULL); 192} 193 194static __inline void 195_tcb_set(struct kcb *kcb, struct tcb *tcb) 196{ 197 kcb->kcb_curtcb = tcb; 198} 199 200static __inline struct tcb * 201_tcb_get(void) 202{ 203 return (_kcb_curtcb()); 204} 205 206static __inline struct pthread * 207_get_curthread(void) 208{ 209 struct tcb *tcb; 210 211 tcb = _kcb_curtcb(); 212 if (tcb != NULL) 213 return (tcb->tcb_thread); 214 else 215 return (NULL); 216} 217 218static __inline struct kse * 219_get_curkse(void) 220{ 221 return ((struct kse *)_kcb_curkse()); 222} 223 224void _i386_enter_uts(struct kse_mailbox *km, kse_func_t uts, void *stack, 225 size_t stacksz); 226 227static __inline int 228_thread_enter_uts(struct tcb *tcb, struct kcb *kcb) 229{ 230 int ret; 231 232 ret = _thr_getcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext); 233 if (ret == 0) { 234 _i386_enter_uts(&kcb->kcb_kmbx, kcb->kcb_kmbx.km_func, 235 kcb->kcb_kmbx.km_stack.ss_sp, 236 kcb->kcb_kmbx.km_stack.ss_size); 237 /* We should not reach here. */ 238 return (-1); 239 } 240 else if (ret < 0) 241 return (-1); 242 return (0); 243} 244 245static __inline int 246_thread_switch(struct kcb *kcb, struct tcb *tcb, int setmbox) 247{ 248 extern int _libkse_debug; 249 250 if ((kcb == NULL) || (tcb == NULL)) 251 return (-1); 252 kcb->kcb_curtcb = tcb; 253 if (_libkse_debug == 0) { 254 tcb->tcb_tmbx.tm_lwp = kcb->kcb_kmbx.km_lwp; 255 if (setmbox != 0) 256 _thr_setcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext, 257 (intptr_t)&tcb->tcb_tmbx, 258 (intptr_t *)&kcb->kcb_kmbx.km_curthread); 259 else 260 _thr_setcontext(&tcb->tcb_tmbx.tm_context.uc_mcontext, 261 0, NULL); 262 } else { 263 if (setmbox) 264 kse_switchin(&tcb->tcb_tmbx, KSE_SWITCHIN_SETTMBX); 265 else 266 kse_switchin(&tcb->tcb_tmbx, 0); 267 } 268 269 /* We should not reach here. */ 270 return (-1); 271} 272 273#endif 274