pthread_md.h revision 165241
1144518Sdavidxu/*- 2144518Sdavidxu * Copyright (C) 2003 David Xu <davidxu@freebsd.org> 3144518Sdavidxu * Copyright (c) 2001 Daniel Eischen <deischen@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. Neither the name of the author nor the names of its contributors 12144518Sdavidxu * may be used to endorse or promote products derived from this software 13144518Sdavidxu * without specific prior written permission. 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: head/lib/libthr/arch/amd64/include/pthread_md.h 165241 2006-12-15 11:52:01Z davidxu $ 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 * %fs 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 void *tcb_spare[1]; 53144518Sdavidxu}; 54144518Sdavidxu 55144518Sdavidxu/* 56144518Sdavidxu * Evaluates to the byte offset of the per-tcb variable name. 57144518Sdavidxu */ 58144518Sdavidxu#define __tcb_offset(name) __offsetof(struct tcb, name) 59144518Sdavidxu 60144518Sdavidxu/* 61144518Sdavidxu * Evaluates to the type of the per-tcb variable name. 62144518Sdavidxu */ 63144518Sdavidxu#define __tcb_type(name) __typeof(((struct tcb *)0)->name) 64144518Sdavidxu 65144518Sdavidxu/* 66144518Sdavidxu * Evaluates to the value of the per-tcb variable name. 67144518Sdavidxu */ 68144518Sdavidxu#define TCB_GET64(name) ({ \ 69144518Sdavidxu __tcb_type(name) __result; \ 70144518Sdavidxu \ 71144518Sdavidxu u_long __i; \ 72144518Sdavidxu __asm __volatile("movq %%fs:%1, %0" \ 73144518Sdavidxu : "=r" (__i) \ 74144518Sdavidxu : "m" (*(u_long *)(__tcb_offset(name)))); \ 75144518Sdavidxu __result = (__tcb_type(name))__i; \ 76144518Sdavidxu \ 77144518Sdavidxu __result; \ 78144518Sdavidxu}) 79144518Sdavidxu 80144518Sdavidxustruct tcb *_tcb_ctor(struct pthread *, int); 81144518Sdavidxuvoid _tcb_dtor(struct tcb *tcb); 82144518Sdavidxu 83144518Sdavidxustatic __inline void 84144518Sdavidxu_tcb_set(struct tcb *tcb) 85144518Sdavidxu{ 86144518Sdavidxu amd64_set_fsbase(tcb); 87144518Sdavidxu} 88144518Sdavidxu 89144518Sdavidxustatic __inline struct tcb * 90144518Sdavidxu_tcb_get(void) 91144518Sdavidxu{ 92144518Sdavidxu return (TCB_GET64(tcb_self)); 93144518Sdavidxu} 94144518Sdavidxu 95144518Sdavidxustatic __inline struct pthread * 96144518Sdavidxu_get_curthread(void) 97144518Sdavidxu{ 98157461Sdavidxu return (TCB_GET64(tcb_thread)); 99144518Sdavidxu} 100144518Sdavidxu#endif 101