git: 1a62e9bc0046 - main - Add <machine/tls.h> header to hold MD constants and helpers for TLS.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 09 Dec 2021 21:23:44 UTC
The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=1a62e9bc0046bfe20f4dd785561e469ff73fd508 commit 1a62e9bc0046bfe20f4dd785561e469ff73fd508 Author: John Baldwin <jhb@FreeBSD.org> AuthorDate: 2021-12-09 21:17:13 +0000 Commit: John Baldwin <jhb@FreeBSD.org> CommitDate: 2021-12-09 21:17:13 +0000 Add <machine/tls.h> 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 <sys/_tls_variant_i.h> 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 <x86/tls.h> 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 <davidxu@freebsd.org>. + * 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 <sys/_tls_variant_i.h> +#include <machine/sysarch.h> + +#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 <davidxu@freebsd.org>. + * 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 <sys/_tls_variant_i.h> + +#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 <x86/tls.h> 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 <sys/_tls_variant_i.h> +#include <machine/sysarch.h> + /* * 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 <sys/_tls_variant_i.h> + +#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 <davidxu@freebsd.org> + * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com> + * 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 <sys/_tls_variant_i.h> + +#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 <davidxu@freebsd.org>. + * 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 <davidxu@freebsd.org> + * Copyright (c) 2001 Daniel Eischen <deischen@freebsd.org> + * 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 <x86/sysarch.h> + +#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_ */