1/* $NetBSD: mcontext.h,v 1.2 2018/02/15 15:53:56 kamil 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
39typedef	__int64_t	__greg_t;
40typedef	__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
82typedef 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_FP(uc)	((uc)->uc_mcontext.__gregs[_REG_R2])
92#define _UC_MACHINE_PC(uc)	((uc)->uc_mcontext.__gregs[_REG_PC])
93#define _UC_MACHINE_INTRV(uc)	((uc)->uc_mcontext.__gregs[_REG_RV])
94
95#define	_UC_MACHINE_SET_PC(uc, pc)	_UC_MACHINE_PC(uc) = (pc)
96
97#if defined(_RTLD_SOURCE) || defined(_LIBC_SOURCE) || defined(__LIBPTHREAD_SOURCE__)
98#include <sys/tls.h>
99
100/*
101 * On OpenRISC 1000, since displacements are signed 16-bit values, the TCB
102 * Pointer is biased by 0x7000 + sizeof(tcb) so that first thread datum can be
103 * addressed by -28672 thereby leaving 60KB available for use as thread data.
104 */
105#define	TLS_TP_OFFSET	0x7000
106#define	TLS_DTV_OFFSET	0x8000
107__CTASSERT(TLS_TP_OFFSET + sizeof(struct tls_tcb) < 0x8000);
108
109static __inline void *
110__lwp_getprivate_fast(void)
111{
112	void *__tp;
113	__asm("l.ori %0,r10,0" : "=r"(__tp));
114	return __tp;
115}
116
117static __inline void *
118__lwp_gettcb_fast(void)
119{
120	void *__tcb;
121
122	__asm __volatile(
123		"l.addi %[__tcb],r10,%[__offset]"
124	    :	[__tcb] "=r" (__tcb)
125	    :	[__offset] "n" (-(TLS_TP_OFFSET + sizeof(struct tls_tcb))));
126
127	return __tcb;
128}
129
130static __inline void
131__lwp_settcb(void *__tcb)
132{
133	__asm __volatile(
134		"l.addi r10,%[__tcb],%[__offset]"
135	    :
136	    :	[__tcb] "r" (__tcb),
137		[__offset] "n" (TLS_TP_OFFSET + sizeof(struct tls_tcb)));
138}
139#endif /* _RTLD_SOURCE || _LIBC_SOURCE || __LIBPTHREAD_SOURCE__ */
140
141#endif /* !_OR1K_MCONTEXT_H_ */
142