xref: /freebsd-src/sys/riscv/include/tls.h (revision 1a62e9bc0046bfe20f4dd785561e469ff73fd508)
1*1a62e9bcSJohn Baldwin /*-
2*1a62e9bcSJohn Baldwin  * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3*1a62e9bcSJohn Baldwin  * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
4*1a62e9bcSJohn Baldwin  * All rights reserved.
5*1a62e9bcSJohn Baldwin  *
6*1a62e9bcSJohn Baldwin  * Portions of this software were developed by SRI International and the
7*1a62e9bcSJohn Baldwin  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8*1a62e9bcSJohn Baldwin  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9*1a62e9bcSJohn Baldwin  *
10*1a62e9bcSJohn Baldwin  * Portions of this software were developed by the University of Cambridge
11*1a62e9bcSJohn Baldwin  * Computer Laboratory as part of the CTSRD Project, with support from the
12*1a62e9bcSJohn Baldwin  * UK Higher Education Innovation Fund (HEIF).
13*1a62e9bcSJohn Baldwin  *
14*1a62e9bcSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
15*1a62e9bcSJohn Baldwin  * modification, are permitted provided that the following conditions
16*1a62e9bcSJohn Baldwin  * are met:
17*1a62e9bcSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
18*1a62e9bcSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
19*1a62e9bcSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
20*1a62e9bcSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
21*1a62e9bcSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
22*1a62e9bcSJohn Baldwin  *
23*1a62e9bcSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24*1a62e9bcSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*1a62e9bcSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*1a62e9bcSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27*1a62e9bcSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*1a62e9bcSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*1a62e9bcSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*1a62e9bcSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*1a62e9bcSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*1a62e9bcSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*1a62e9bcSJohn Baldwin  * SUCH DAMAGE.
34*1a62e9bcSJohn Baldwin  */
35*1a62e9bcSJohn Baldwin 
36*1a62e9bcSJohn Baldwin #ifndef _MACHINE_TLS_H_
37*1a62e9bcSJohn Baldwin #define	_MACHINE_TLS_H_
38*1a62e9bcSJohn Baldwin 
39*1a62e9bcSJohn Baldwin #include <sys/_tls_variant_i.h>
40*1a62e9bcSJohn Baldwin 
41*1a62e9bcSJohn Baldwin #define	TLS_DTV_OFFSET	0x800
42*1a62e9bcSJohn Baldwin #define	TLS_TCB_ALIGN	16
43*1a62e9bcSJohn Baldwin #define	TLS_TP_OFFSET	0
44*1a62e9bcSJohn Baldwin 
45*1a62e9bcSJohn Baldwin static __inline void
_tcb_set(struct tcb * tcb)46*1a62e9bcSJohn Baldwin _tcb_set(struct tcb *tcb)
47*1a62e9bcSJohn Baldwin {
48*1a62e9bcSJohn Baldwin 	__asm __volatile("addi tp, %0, %1" :: "r" (tcb), "I" (TLS_TCB_SIZE));
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 	__asm __volatile("addi %0, tp, %1" : "=r" (tcb) : "I" (-TLS_TCB_SIZE));
57*1a62e9bcSJohn Baldwin 	return (tcb);
58*1a62e9bcSJohn Baldwin }
59*1a62e9bcSJohn Baldwin 
60*1a62e9bcSJohn Baldwin #endif /* !_MACHINE_TLS_H_ */
61