xref: /freebsd/sys/powerpc/include/tls.h (revision 1a62e9bc)
11a62e9bcSJohn Baldwin /*-
21a62e9bcSJohn Baldwin  * SPDX-License-Identifier: BSD-3-Clause
31a62e9bcSJohn Baldwin  *
41a62e9bcSJohn Baldwin  * Copyright 2004 by Peter Grehan. All rights reserved.
51a62e9bcSJohn Baldwin  *
61a62e9bcSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
71a62e9bcSJohn Baldwin  * modification, are permitted provided that the following conditions
81a62e9bcSJohn Baldwin  * are met:
91a62e9bcSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
101a62e9bcSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
111a62e9bcSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
121a62e9bcSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
131a62e9bcSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
141a62e9bcSJohn Baldwin  * 3. The name of the author may not be used to endorse or promote products
151a62e9bcSJohn Baldwin  *    derived from this software without specific prior written permission.
161a62e9bcSJohn Baldwin  *
171a62e9bcSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
181a62e9bcSJohn Baldwin  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191a62e9bcSJohn Baldwin  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201a62e9bcSJohn Baldwin  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211a62e9bcSJohn Baldwin  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
221a62e9bcSJohn Baldwin  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
231a62e9bcSJohn Baldwin  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
241a62e9bcSJohn Baldwin  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
251a62e9bcSJohn Baldwin  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261a62e9bcSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271a62e9bcSJohn Baldwin  * SUCH DAMAGE.
281a62e9bcSJohn Baldwin  */
291a62e9bcSJohn Baldwin 
301a62e9bcSJohn Baldwin #ifndef _MACHINE_TLS_H_
311a62e9bcSJohn Baldwin #define	_MACHINE_TLS_H_
321a62e9bcSJohn Baldwin 
331a62e9bcSJohn Baldwin #include <sys/_tls_variant_i.h>
341a62e9bcSJohn Baldwin 
351a62e9bcSJohn Baldwin #define	TLS_DTV_OFFSET	0x8000
361a62e9bcSJohn Baldwin #define	TLS_TCB_ALIGN	TLS_TCB_SIZE
371a62e9bcSJohn Baldwin #define	TLS_TP_OFFSET	0x7000
381a62e9bcSJohn Baldwin 
391a62e9bcSJohn Baldwin static __inline void
_tcb_set(struct tcb * tcb)401a62e9bcSJohn Baldwin _tcb_set(struct tcb *tcb)
411a62e9bcSJohn Baldwin {
421a62e9bcSJohn Baldwin #ifdef __powerpc64__
431a62e9bcSJohn Baldwin 	__asm __volatile("mr 13,%0" ::
441a62e9bcSJohn Baldwin 	    "r" ((uint8_t *)tcb + TLS_TP_OFFSET + TLS_TCB_SIZE));
451a62e9bcSJohn Baldwin #else
461a62e9bcSJohn Baldwin 	__asm __volatile("mr 2,%0" ::
471a62e9bcSJohn Baldwin 	    "r" ((uint8_t *)tcb + TLS_TP_OFFSET + TLS_TCB_SIZE));
481a62e9bcSJohn Baldwin #endif
491a62e9bcSJohn Baldwin }
501a62e9bcSJohn Baldwin 
511a62e9bcSJohn Baldwin static __inline struct tcb *
_tcb_get(void)521a62e9bcSJohn Baldwin _tcb_get(void)
531a62e9bcSJohn Baldwin {
541a62e9bcSJohn Baldwin 	struct tcb *tcb;
551a62e9bcSJohn Baldwin 
561a62e9bcSJohn Baldwin #ifdef __powerpc64__
571a62e9bcSJohn Baldwin 	__asm __volatile("addi %0,13,%1" : "=r" (tcb) :
581a62e9bcSJohn Baldwin 	    "i" (-(TLS_TP_OFFSET + TLS_TCB_SIZE)));
591a62e9bcSJohn Baldwin #else
601a62e9bcSJohn Baldwin 	__asm __volatile("addi %0,2,%1" : "=r" (tcb) :
611a62e9bcSJohn Baldwin 	    "i" (-(TLS_TP_OFFSET + TLS_TCB_SIZE)));
621a62e9bcSJohn Baldwin #endif
631a62e9bcSJohn Baldwin 	return (tcb);
641a62e9bcSJohn Baldwin }
651a62e9bcSJohn Baldwin 
661a62e9bcSJohn Baldwin #endif /* !_MACHINE_TLS_H_ */
67