From nobody Thu Dec 09 21:23:44 2021 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 76E2A18D0D39; Thu, 9 Dec 2021 21:23:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4J96Tr57Kgz3KD1; Thu, 9 Dec 2021 21:23:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 79AF7615E; Thu, 9 Dec 2021 21:23:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1B9LNiDa047074; Thu, 9 Dec 2021 21:23:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1B9LNiiT047073; Thu, 9 Dec 2021 21:23:44 GMT (envelope-from git) Date: Thu, 9 Dec 2021 21:23:44 GMT Message-Id: <202112092123.1B9LNiiT047073@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: 1a62e9bc0046 - main - Add header to hold MD constants and helpers for TLS. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1a62e9bc0046bfe20f4dd785561e469ff73fd508 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639085025; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=i5vv0Z7RLz2zOeWWS/pWGRgk9YBJISwcWPsKOx4BlnY=; b=cTnPvspaE0S5hqS8I42pwGnx8twzDklbfUfV+1AAUqlkVbQU8ZFXfyvPxV/eFYj3uLm2gD qoAdHvCW+qDlKhLN8LwQhOcaFtNr4K4AO+hk3B6ev2LkaUqteF3fFMDDS/2neexZaTknHQ iJH33LSN78IJ+fMMkc5YACK9ygHY4Qv4OhHhS1De3BRu6k12Sz+rRtFjQOSrh+n2M6NnOT QsuJirWAHKzrAR9i98w7FcYuFMBRZkbnh7/F/3UBTxC4uI/ajhpyHosVUF31HMnz2Ka19W dMjKShln2QwqDY++kWEd+CVIFD+SBa05Dx0epc/nK517hxmBk6lCh4idbgcBgQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639085025; a=rsa-sha256; cv=none; b=j/JkInXaERsXKGOSJKW3kYDn+sj0dXJVRwkBybQwMtctosH75gqqfgWawMXbuKzh6BmgKm F9UvpQXg+HGIvTy7BUCQjM6saVBupR5J+5QHxxscJG75JZQMxKJ8k+Ms201Oy5ZwCUBIW8 X21tkaVQ7Z7VZESG1A7hdafacq6FVl+ZPhmMD7DfJlNJ4ZZBmEaaKp+jRDqbvIDZsp5OZi f0PAJphR77GvKxrKMwmzbqLwmwENZWeIGHtQF3O21oH8YAoiRrQTCw+N+Wlv4lR7Q5Aowd I7WGxweRv45lAnR0oivvb8mJtWFzT1BRrMQyvtTcG6o26t//vvSbRNtDI5rEew== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=1a62e9bc0046bfe20f4dd785561e469ff73fd508 commit 1a62e9bc0046bfe20f4dd785561e469ff73fd508 Author: John Baldwin AuthorDate: 2021-12-09 21:17:13 +0000 Commit: John Baldwin CommitDate: 2021-12-09 21:17:13 +0000 Add header to hold MD constants and helpers for TLS. The header exports the following: - Definition of struct tcb. - Helpers to get/set the tcb for the current thread. - TLS_TCB_SIZE (size of TCB) - TLS_TCB_ALIGN (alignment of TCB) - TLS_VARIANT_I or TLS_VARIANT_II - TLS_DTV_OFFSET (bias of pointers in dtv[]) - TLS_TP_OFFSET (bias of "thread pointer" relative to TCB) Note that TLS_TP_OFFSET does not account for if the unbiased thread pointer points to the start of the TCB (arm and x86) or the end of the TCB (MIPS, PowerPC, and RISC-V). Note also that for amd64, the struct tcb does not include the unused tcb_spare field included in the current structure in libthr. libthr does not use this field, and the existing calls in libc and rtld that allocate a TCB for amd64 assume it is the size of 3 Elf_Addr's (and thus do not allocate room for tcb_spare). A header is used by architectures using Variant I TLS which uses a common struct tcb. Reviewed by: kib (older version of x86/tls.h), jrtc27 Sponsored by: The University of Cambridge, Google Inc. Differential Revision: https://reviews.freebsd.org/D33351 --- sys/amd64/include/tls.h | 5 +++ sys/arm/include/tls.h | 55 ++++++++++++++++++++++++++++++++ sys/arm64/include/tls.h | 55 ++++++++++++++++++++++++++++++++ sys/i386/include/tls.h | 5 +++ sys/mips/include/tls.h | 44 +++++++++++++++++++++---- sys/powerpc/include/tls.h | 66 ++++++++++++++++++++++++++++++++++++++ sys/riscv/include/tls.h | 60 +++++++++++++++++++++++++++++++++++ sys/sys/_tls_variant_i.h | 47 +++++++++++++++++++++++++++ sys/x86/include/tls.h | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 412 insertions(+), 6 deletions(-) diff --git a/sys/amd64/include/tls.h b/sys/amd64/include/tls.h new file mode 100644 index 000000000000..15a4a407c0e0 --- /dev/null +++ b/sys/amd64/include/tls.h @@ -0,0 +1,5 @@ +/*- + * This file is in the public domain. + */ + +#include diff --git a/sys/arm/include/tls.h b/sys/arm/include/tls.h new file mode 100644 index 000000000000..b3466896eb13 --- /dev/null +++ b/sys/arm/include/tls.h @@ -0,0 +1,55 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2005 David Xu . + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _MACHINE_TLS_H_ +#define _MACHINE_TLS_H_ + +#include +#include + +#define TLS_DTV_OFFSET 0 +#define TLS_TCB_ALIGN 8 +#define TLS_TP_OFFSET 0 + +static __inline void +_tcb_set(struct tcb *tcb) +{ + sysarch(ARM_SET_TP, tcb); +} + +static __inline struct tcb * +_tcb_get(void) +{ + struct tcb *tcb; + + __asm __volatile("mrc p15, 0, %0, c13, c0, 3" \ + : "=r" (tcb)); + return (tcb); +} + +#endif /* !_MACHINE_TLS_H_ */ diff --git a/sys/arm64/include/tls.h b/sys/arm64/include/tls.h new file mode 100644 index 000000000000..a3ec7f7952e8 --- /dev/null +++ b/sys/arm64/include/tls.h @@ -0,0 +1,55 @@ +/*- + * Copyright (c) 2005 David Xu . + * Copyright (c) 2014 the FreeBSD Foundation + * All rights reserved. + * + * Portions of this software were developed by Andrew Turner + * under sponsorship from the FreeBSD Foundation + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _MACHINE_TLS_H_ +#define _MACHINE_TLS_H_ + +#include + +#define TLS_DTV_OFFSET 0 +#define TLS_TCB_ALIGN 16 +#define TLS_TP_OFFSET 0 + +static __inline void +_tcb_set(struct tcb *tcb) +{ + __asm __volatile("msr tpidr_el0, %x0" :: "r" (tcb)); +} + +static __inline struct tcb * +_tcb_get(void) +{ + struct tcb *tcb; + + __asm __volatile("mrs %x0, tpidr_el0" : "=r" (tcb)); + return (tcb); +} + +#endif /* !_MACHINE_TLS_H_ */ diff --git a/sys/i386/include/tls.h b/sys/i386/include/tls.h new file mode 100644 index 000000000000..15a4a407c0e0 --- /dev/null +++ b/sys/i386/include/tls.h @@ -0,0 +1,5 @@ +/*- + * This file is in the public domain. + */ + +#include diff --git a/sys/mips/include/tls.h b/sys/mips/include/tls.h index c71507b773b0..c209983027b1 100644 --- a/sys/mips/include/tls.h +++ b/sys/mips/include/tls.h @@ -34,20 +34,52 @@ #ifndef __MIPS_TLS_H__ #define __MIPS_TLS_H__ +#include +#include + /* * TLS parameters */ -#define TLS_TP_OFFSET 0x7000 -#define TLS_DTV_OFFSET 0x8000 +#define TLS_DTV_OFFSET 0x8000 +#define TLS_TCB_ALIGN 8 +#define TLS_TP_OFFSET 0x7000 -#ifdef __mips_n64 -#define TLS_TCB_SIZE 16 #ifdef COMPAT_FREEBSD32 -#define TLS_TCB_SIZE32 8 +#define TLS_TCB_SIZE32 8 #endif + +#ifndef _KERNEL + +static __inline void +_tcb_set(struct tcb *tcb) +{ + sysarch(MIPS_SET_TLS, tcb); +} + +static __inline struct tcb * +_tcb_get(void) +{ + struct tcb *tcb; + +#ifdef TLS_USE_SYSARCH + sysarch(MIPS_GET_TLS, &tcb); #else -#define TLS_TCB_SIZE 8 + __asm__ __volatile__ ( + ".set\tpush\n\t" +#ifdef __mips_n64 + ".set\tmips64r2\n\t" +#else + ".set\tmips32r2\n\t" +#endif + "rdhwr\t%0, $29\n\t" + ".set\tpop" + : "=r" (tcb)); + tcb = (struct tcb *)((uintptr_t)tcb - TLS_TP_OFFSET - TLS_TCB_SIZE); +#endif + return (tcb); +} + #endif #endif /* __MIPS_TLS_H__ */ diff --git a/sys/powerpc/include/tls.h b/sys/powerpc/include/tls.h new file mode 100644 index 000000000000..745a83a910c4 --- /dev/null +++ b/sys/powerpc/include/tls.h @@ -0,0 +1,66 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright 2004 by Peter Grehan. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _MACHINE_TLS_H_ +#define _MACHINE_TLS_H_ + +#include + +#define TLS_DTV_OFFSET 0x8000 +#define TLS_TCB_ALIGN TLS_TCB_SIZE +#define TLS_TP_OFFSET 0x7000 + +static __inline void +_tcb_set(struct tcb *tcb) +{ +#ifdef __powerpc64__ + __asm __volatile("mr 13,%0" :: + "r" ((uint8_t *)tcb + TLS_TP_OFFSET + TLS_TCB_SIZE)); +#else + __asm __volatile("mr 2,%0" :: + "r" ((uint8_t *)tcb + TLS_TP_OFFSET + TLS_TCB_SIZE)); +#endif +} + +static __inline struct tcb * +_tcb_get(void) +{ + struct tcb *tcb; + +#ifdef __powerpc64__ + __asm __volatile("addi %0,13,%1" : "=r" (tcb) : + "i" (-(TLS_TP_OFFSET + TLS_TCB_SIZE))); +#else + __asm __volatile("addi %0,2,%1" : "=r" (tcb) : + "i" (-(TLS_TP_OFFSET + TLS_TCB_SIZE))); +#endif + return (tcb); +} + +#endif /* !_MACHINE_TLS_H_ */ diff --git a/sys/riscv/include/tls.h b/sys/riscv/include/tls.h new file mode 100644 index 000000000000..4dd1fa8415ef --- /dev/null +++ b/sys/riscv/include/tls.h @@ -0,0 +1,60 @@ +/*- + * Copyright (c) 2005 David Xu + * Copyright (c) 2015 Ruslan Bukin + * All rights reserved. + * + * Portions of this software were developed by SRI International and the + * University of Cambridge Computer Laboratory under DARPA/AFRL contract + * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme. + * + * Portions of this software were developed by the University of Cambridge + * Computer Laboratory as part of the CTSRD Project, with support from the + * UK Higher Education Innovation Fund (HEIF). + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _MACHINE_TLS_H_ +#define _MACHINE_TLS_H_ + +#include + +#define TLS_DTV_OFFSET 0x800 +#define TLS_TCB_ALIGN 16 +#define TLS_TP_OFFSET 0 + +static __inline void +_tcb_set(struct tcb *tcb) +{ + __asm __volatile("addi tp, %0, %1" :: "r" (tcb), "I" (TLS_TCB_SIZE)); +} + +static __inline struct tcb * +_tcb_get(void) +{ + struct tcb *tcb; + + __asm __volatile("addi %0, tp, %1" : "=r" (tcb) : "I" (-TLS_TCB_SIZE)); + return (tcb); +} + +#endif /* !_MACHINE_TLS_H_ */ diff --git a/sys/sys/_tls_variant_i.h b/sys/sys/_tls_variant_i.h new file mode 100644 index 000000000000..0f83f121cc41 --- /dev/null +++ b/sys/sys/_tls_variant_i.h @@ -0,0 +1,47 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2005 David Xu . + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_TLS_VARIANT_I_H_ +#define _SYS_TLS_VARIANT_I_H_ + +/* + * Variant I TLS uses the same TCB layout across architectures. + */ + +#define TLS_VARIANT_I + +struct pthread; + +struct tcb { + uintptr_t *tcb_dtv; /* required by rtld */ + struct pthread *tcb_thread; +}; + +#define TLS_TCB_SIZE sizeof(struct tcb) + +#endif /* !_SYS_TLS_VARIANT_I_H_ */ diff --git a/sys/x86/include/tls.h b/sys/x86/include/tls.h new file mode 100644 index 000000000000..d4b631de10d0 --- /dev/null +++ b/sys/x86/include/tls.h @@ -0,0 +1,81 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (C) 2003 David Xu + * Copyright (c) 2001 Daniel Eischen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Neither the name of the author nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _MACHINE_TLS_H_ +#define _MACHINE_TLS_H_ + +#include + +#define TLS_VARIANT_II + +struct pthread; + +/* + * Variant II tcb, first two members are required by rtld, + * %fs (amd64) / %gs (i386) points to the structure. + */ +struct tcb { + struct tcb *tcb_self; /* required by rtld */ + uintptr_t *tcb_dtv; /* required by rtld */ + struct pthread *tcb_thread; +}; + +#define TLS_DTV_OFFSET 0 +#ifdef __amd64__ +#define TLS_TCB_ALIGN 16 +#else +#define TLS_TCB_ALIGN 4 +#endif +#define TLS_TCB_SIZE sizeof(struct tcb) +#define TLS_TP_OFFSET 0 + +static __inline void +_tcb_set(struct tcb *tcb) +{ +#ifdef __amd64__ + amd64_set_fsbase(tcb); +#else + i386_set_gsbase(tcb); +#endif +} + +static __inline struct tcb * +_tcb_get(void) +{ + struct tcb *tcb; + +#ifdef __amd64__ + __asm __volatile("movq %%fs:0, %0" : "=r" (tcb)); +#else + __asm __volatile("movl %%gs:0, %0" : "=r" (tcb)); +#endif + return (tcb); +} + +#endif /* !_MACHINE_TLS_H_ */