1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2003 David Xu <davidxu@freebsd.org>
5 * Copyright (c) 2001 Daniel Eischen <deischen@freebsd.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Neither the name of the author nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef _MACHINE_TLS_H_
31#define _MACHINE_TLS_H_
32
33#include <x86/sysarch.h>
34
35#define TLS_VARIANT_II
36
37struct pthread;
38
39struct dtv_slot {
40 char *dtvs_tls;
41};
42
43struct dtv {
44 uintptr_t dtv_gen;
45 uintptr_t dtv_size;
46 struct dtv_slot dtv_slots[];
47};
48
49/*
50 * Variant II tcb, first two members are required by rtld,
51 * %fs (amd64) / %gs (i386) points to the structure.
52 */
53struct tcb {
54 struct tcb *tcb_self; /* required by rtld */
55 struct dtv *tcb_dtv; /* required by rtld */
56 struct pthread *tcb_thread;
57};
58
59#define TLS_DTV_OFFSET 0
60#ifdef __amd64__
61#define TLS_TCB_ALIGN 16
62#else
63#define TLS_TCB_ALIGN 4
64#endif
65#define TLS_TCB_SIZE sizeof(struct tcb)
66#define TLS_TP_OFFSET 0
67
68static __inline void
69_tcb_set(struct tcb *tcb)
70{
71#ifdef __amd64__
72 amd64_set_tlsbase(tcb);
73#else
74 i386_set_gsbase(tcb);
75#endif
76}
77
78static __inline struct tcb *
79_tcb_get(void)
80{
81 struct tcb *tcb;
82
83#ifdef __amd64__
84 __asm __volatile("movq %%fs:0, %0" : "=r" (tcb));
85#else
86 __asm __volatile("movl %%gs:0, %0" : "=r" (tcb));
87#endif
88 return (tcb);
89}
90
91#endif /* !_MACHINE_TLS_H_ */