xref: /netbsd-src/sys/arch/or1k/include/mcontext.h (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* $NetBSD: mcontext.h,v 1.1 2014/09/03 19:34:26 matt Exp $ */
2 
3 /*-
4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matt Thomas of 3am Software Foundry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef _OR1K_MCONTEXT_H_
32 #define _OR1K_MCONTEXT_H_
33 
34 /*
35  */
36 
37 #define	_NGREG	33		/* GR1-31, FPCSR */
38 
39 typedef	__int64_t	__greg_t;
40 typedef	__greg_t	__gregset_t[_NGREG];
41 
42 #define	_REG_R1		0
43 #define	_REG_R2		1
44 #define	_REG_R3		2
45 #define	_REG_R4		3
46 #define	_REG_R5		4
47 #define	_REG_R6		5
48 #define	_REG_R7		6
49 #define	_REG_R8		7
50 #define	_REG_R9		8
51 #define	_REG_R10	9
52 #define	_REG_R11	10
53 #define	_REG_R12	11
54 #define	_REG_R13	12
55 #define	_REG_R14	13
56 #define	_REG_R15	14
57 #define	_REG_R16	15
58 #define	_REG_R17	16
59 #define	_REG_R18	17
60 #define	_REG_R19	18
61 #define	_REG_R20	19
62 #define	_REG_R21	20
63 #define	_REG_R22	21
64 #define	_REG_R23	22
65 #define	_REG_R24	23
66 #define	_REG_R25	24
67 #define	_REG_R26	25
68 #define	_REG_R27	26
69 #define	_REG_R28	27
70 #define	_REG_R29	28
71 #define	_REG_R30	29
72 #define	_REG_R31	30
73 #define	_REG_PC		31
74 #define	_REG_FPCSR	32
75 
76 #define	_REG_SP		_REG_R1
77 #define	_REG_LR		_REG_R9
78 #define	_REG_TP		_REG_R10
79 #define	_REG_RV		_REG_R11
80 #define	_REG_GP		_REG_R16
81 
82 typedef struct {
83 	__gregset_t	__gregs;	/* General Purpose Register set */
84 	__greg_t	__spare[8];	/* future proof */
85 } mcontext_t;
86 
87 /* Machine-dependent uc_flags */
88 #define	_UC_TLSBASE	0x00080000	/* see <sys/ucontext.h> */
89 
90 #define _UC_MACHINE_SP(uc)	((uc)->uc_mcontext.__gregs[_REG_SP])
91 #define _UC_MACHINE_PC(uc)	((uc)->uc_mcontext.__gregs[_REG_PC])
92 #define _UC_MACHINE_INTRV(uc)	((uc)->uc_mcontext.__gregs[_REG_RV])
93 
94 #define	_UC_MACHINE_SET_PC(uc, pc)	_UC_MACHINE_PC(uc) = (pc)
95 
96 #if defined(_RTLD_SOURCE) || defined(_LIBC_SOURCE) || defined(__LIBPTHREAD_SOURCE__)
97 #include <sys/tls.h>
98 
99 /*
100  * On OpenRISC 1000, since displacements are signed 16-bit values, the TCB
101  * Pointer is biased by 0x7000 + sizeof(tcb) so that first thread datum can be
102  * addressed by -28672 thereby leaving 60KB available for use as thread data.
103  */
104 #define	TLS_TP_OFFSET	0x7000
105 #define	TLS_DTV_OFFSET	0x8000
106 __CTASSERT(TLS_TP_OFFSET + sizeof(struct tls_tcb) < 0x8000);
107 
108 static __inline void *
109 __lwp_getprivate_fast(void)
110 {
111 	void *__tp;
112 	__asm("l.ori %0,r10,0" : "=r"(__tp));
113 	return __tp;
114 }
115 
116 static __inline void *
117 __lwp_gettcb_fast(void)
118 {
119 	void *__tcb;
120 
121 	__asm __volatile(
122 		"l.addi %[__tcb],r10,%[__offset]"
123 	    :	[__tcb] "=r" (__tcb)
124 	    :	[__offset] "n" (-(TLS_TP_OFFSET + sizeof(struct tls_tcb))));
125 
126 	return __tcb;
127 }
128 
129 static __inline void
130 __lwp_settcb(void *__tcb)
131 {
132 	__asm __volatile(
133 		"l.addi r10,%[__tcb],%[__offset]"
134 	    :
135 	    :	[__tcb] "r" (__tcb),
136 		[__offset] "n" (TLS_TP_OFFSET + sizeof(struct tls_tcb)));
137 }
138 #endif /* _RTLD_SOURCE || _LIBC_SOURCE || __LIBPTHREAD_SOURCE__ */
139 
140 #endif /* !_OR1K_MCONTEXT_H_ */
141