xref: /freebsd/sys/x86/include/tls.h (revision e87ec409fa9b21abf79895837fe375ab3d7e408a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 
37 struct pthread;
38 
39 /*
40  * Variant II tcb, first two members are required by rtld,
41  * %fs (amd64) / %gs (i386) points to the structure.
42  */
43 struct tcb {
44 	struct tcb		*tcb_self;	/* required by rtld */
45 	uintptr_t		*tcb_dtv;	/* required by rtld */
46 	struct pthread		*tcb_thread;
47 };
48 
49 #define	TLS_DTV_OFFSET	0
50 #ifdef __amd64__
51 #define	TLS_TCB_ALIGN	16
52 #else
53 #define	TLS_TCB_ALIGN	4
54 #endif
55 #define	TLS_TCB_SIZE	sizeof(struct tcb)
56 #define	TLS_TP_OFFSET	0
57 
58 static __inline void
59 _tcb_set(struct tcb *tcb)
60 {
61 #ifdef __amd64__
62 	amd64_set_fsbase(tcb);
63 #else
64  	i386_set_gsbase(tcb);
65 #endif
66 }
67 
68 static __inline struct tcb *
69 _tcb_get(void)
70 {
71 	struct tcb *tcb;
72 
73 #ifdef __amd64__
74 	__asm __volatile("movq %%fs:0, %0" : "=r" (tcb));
75 #else
76 	__asm __volatile("movl %%gs:0, %0" : "=r" (tcb));
77 #endif
78 	return (tcb);
79 }
80 
81 #endif /* !_MACHINE_TLS_H_ */
82