xref: /freebsd/sys/powerpc/include/tls.h (revision 1a62e9bc0046bfe20f4dd785561e469ff73fd508)
1*1a62e9bcSJohn Baldwin /*-
2*1a62e9bcSJohn Baldwin  * SPDX-License-Identifier: BSD-3-Clause
3*1a62e9bcSJohn Baldwin  *
4*1a62e9bcSJohn Baldwin  * Copyright 2004 by Peter Grehan. All rights reserved.
5*1a62e9bcSJohn Baldwin  *
6*1a62e9bcSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
7*1a62e9bcSJohn Baldwin  * modification, are permitted provided that the following conditions
8*1a62e9bcSJohn Baldwin  * are met:
9*1a62e9bcSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
10*1a62e9bcSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
11*1a62e9bcSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
12*1a62e9bcSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
13*1a62e9bcSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
14*1a62e9bcSJohn Baldwin  * 3. The name of the author may not be used to endorse or promote products
15*1a62e9bcSJohn Baldwin  *    derived from this software without specific prior written permission.
16*1a62e9bcSJohn Baldwin  *
17*1a62e9bcSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*1a62e9bcSJohn Baldwin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*1a62e9bcSJohn Baldwin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*1a62e9bcSJohn Baldwin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*1a62e9bcSJohn Baldwin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22*1a62e9bcSJohn Baldwin  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23*1a62e9bcSJohn Baldwin  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24*1a62e9bcSJohn Baldwin  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25*1a62e9bcSJohn Baldwin  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*1a62e9bcSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*1a62e9bcSJohn Baldwin  * SUCH DAMAGE.
28*1a62e9bcSJohn Baldwin  */
29*1a62e9bcSJohn Baldwin 
30*1a62e9bcSJohn Baldwin #ifndef _MACHINE_TLS_H_
31*1a62e9bcSJohn Baldwin #define	_MACHINE_TLS_H_
32*1a62e9bcSJohn Baldwin 
33*1a62e9bcSJohn Baldwin #include <sys/_tls_variant_i.h>
34*1a62e9bcSJohn Baldwin 
35*1a62e9bcSJohn Baldwin #define	TLS_DTV_OFFSET	0x8000
36*1a62e9bcSJohn Baldwin #define	TLS_TCB_ALIGN	TLS_TCB_SIZE
37*1a62e9bcSJohn Baldwin #define	TLS_TP_OFFSET	0x7000
38*1a62e9bcSJohn Baldwin 
39*1a62e9bcSJohn Baldwin static __inline void
_tcb_set(struct tcb * tcb)40*1a62e9bcSJohn Baldwin _tcb_set(struct tcb *tcb)
41*1a62e9bcSJohn Baldwin {
42*1a62e9bcSJohn Baldwin #ifdef __powerpc64__
43*1a62e9bcSJohn Baldwin 	__asm __volatile("mr 13,%0" ::
44*1a62e9bcSJohn Baldwin 	    "r" ((uint8_t *)tcb + TLS_TP_OFFSET + TLS_TCB_SIZE));
45*1a62e9bcSJohn Baldwin #else
46*1a62e9bcSJohn Baldwin 	__asm __volatile("mr 2,%0" ::
47*1a62e9bcSJohn Baldwin 	    "r" ((uint8_t *)tcb + TLS_TP_OFFSET + TLS_TCB_SIZE));
48*1a62e9bcSJohn Baldwin #endif
49*1a62e9bcSJohn Baldwin }
50*1a62e9bcSJohn Baldwin 
51*1a62e9bcSJohn Baldwin static __inline struct tcb *
_tcb_get(void)52*1a62e9bcSJohn Baldwin _tcb_get(void)
53*1a62e9bcSJohn Baldwin {
54*1a62e9bcSJohn Baldwin 	struct tcb *tcb;
55*1a62e9bcSJohn Baldwin 
56*1a62e9bcSJohn Baldwin #ifdef __powerpc64__
57*1a62e9bcSJohn Baldwin 	__asm __volatile("addi %0,13,%1" : "=r" (tcb) :
58*1a62e9bcSJohn Baldwin 	    "i" (-(TLS_TP_OFFSET + TLS_TCB_SIZE)));
59*1a62e9bcSJohn Baldwin #else
60*1a62e9bcSJohn Baldwin 	__asm __volatile("addi %0,2,%1" : "=r" (tcb) :
61*1a62e9bcSJohn Baldwin 	    "i" (-(TLS_TP_OFFSET + TLS_TCB_SIZE)));
62*1a62e9bcSJohn Baldwin #endif
63*1a62e9bcSJohn Baldwin 	return (tcb);
64*1a62e9bcSJohn Baldwin }
65*1a62e9bcSJohn Baldwin 
66*1a62e9bcSJohn Baldwin #endif /* !_MACHINE_TLS_H_ */
67