xref: /netbsd-src/sys/arch/riscv/include/lwp_private.h (revision 16543c49052c820334cffc5c69b2afde18f02458)
1*16543c49Schristos /* $NetBSD: lwp_private.h,v 1.1 2024/11/30 01:04:13 christos Exp $ */
2*16543c49Schristos 
3*16543c49Schristos /*-
4*16543c49Schristos  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*16543c49Schristos  * All rights reserved.
6*16543c49Schristos  *
7*16543c49Schristos  * This code is derived from software contributed to The NetBSD Foundation
8*16543c49Schristos  * by Matt Thomas of 3am Software Foundry.
9*16543c49Schristos  *
10*16543c49Schristos  * Redistribution and use in source and binary forms, with or without
11*16543c49Schristos  * modification, are permitted provided that the following conditions
12*16543c49Schristos  * are met:
13*16543c49Schristos  * 1. Redistributions of source code must retain the above copyright
14*16543c49Schristos  *    notice, this list of conditions and the following disclaimer.
15*16543c49Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16*16543c49Schristos  *    notice, this list of conditions and the following disclaimer in the
17*16543c49Schristos  *    documentation and/or other materials provided with the distribution.
18*16543c49Schristos  *
19*16543c49Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*16543c49Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*16543c49Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*16543c49Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*16543c49Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*16543c49Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*16543c49Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*16543c49Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*16543c49Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*16543c49Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*16543c49Schristos  * POSSIBILITY OF SUCH DAMAGE.
30*16543c49Schristos  */
31*16543c49Schristos #ifndef _RISCV_LWP_PRIVATE_H_
32*16543c49Schristos #define _RISCV_LWP_PRIVATE_H_
33*16543c49Schristos 
34*16543c49Schristos #include <sys/cdefs.h>
35*16543c49Schristos #include <sys/tls.h>
36*16543c49Schristos 
37*16543c49Schristos /*
38*16543c49Schristos  * On RISCV, since displacements are signed 12-bit values, the TCB Pointer
39*16543c49Schristos  * is biased by sizeof(tcb) so that first thread datum can be addressed by
40*16543c49Schristos  * -sizeof(tcb).
41*16543c49Schristos  */
42*16543c49Schristos 
43*16543c49Schristos #define	TLS_TP_OFFSET	0x0
44*16543c49Schristos #define	TLS_TCB_ALIGN	16
45*16543c49Schristos #define	TLS_DTV_OFFSET	0x800
46*16543c49Schristos __CTASSERT(TLS_TP_OFFSET + sizeof(struct tls_tcb) < 0x800);
47*16543c49Schristos 
48*16543c49Schristos __BEGIN_DECLS
49*16543c49Schristos 
50*16543c49Schristos static __inline void *
51*16543c49Schristos __lwp_getprivate_fast(void)
52*16543c49Schristos {
53*16543c49Schristos 	void *__tp;
54*16543c49Schristos 	__asm("mv %0, tp" : "=r"(__tp));
55*16543c49Schristos 	return __tp;
56*16543c49Schristos }
57*16543c49Schristos 
58*16543c49Schristos static __inline void *
59*16543c49Schristos __lwp_gettcb_fast(void)
60*16543c49Schristos {
61*16543c49Schristos 	void *__tcb;
62*16543c49Schristos 
63*16543c49Schristos 	__asm __volatile(
64*16543c49Schristos 		"addi %[__tcb], tp, %[__offset]"
65*16543c49Schristos 	    :	[__tcb] "=r" (__tcb)
66*16543c49Schristos 	    :	[__offset] "n" (-(TLS_TP_OFFSET + sizeof(struct tls_tcb))));
67*16543c49Schristos 
68*16543c49Schristos 	return __tcb;
69*16543c49Schristos }
70*16543c49Schristos 
71*16543c49Schristos static __inline void
72*16543c49Schristos __lwp_settcb(void *__tcb)
73*16543c49Schristos {
74*16543c49Schristos 	__asm __volatile(
75*16543c49Schristos 		"addi tp, %[__tcb], %[__offset]"
76*16543c49Schristos 	    :
77*16543c49Schristos 	    :	[__tcb] "r" (__tcb),
78*16543c49Schristos 		[__offset] "n" (TLS_TP_OFFSET + sizeof(struct tls_tcb)));
79*16543c49Schristos }
80*16543c49Schristos 
81*16543c49Schristos __END_DECLS
82*16543c49Schristos 
83*16543c49Schristos #endif /* !_RISCV_LWP_PRIVATE_H_ */
84