summaryrefslogtreecommitdiff
path: root/lib/libpthread/arch/sh3/_context_u.S
blob: 54cfe56c0b0dc51e05222d25fd66c140cd895797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*	$NetBSD: _context_u.S,v 1.4 2003/11/17 22:38:11 uwe Exp $ */

/*
 * Copyright (c) 2003 Christian P. Groessler
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <machine/asm.h>
#include "assym.h"


/*
 * Enter debugger or die with SIGTRAP if "notreached" code is reached
 */
#define NOTREACHED	trapa #0xc3


/*
 * Only save/restore registers that are callee saved, i.e for which
 * gcc has call_used_regs[reg] == 0.
 */

#define GETC(uc)							  \
	mov.l	.L_uc_gregs_offset, r1					; \
	sts	pr, r0							; \
	add	uc, r1		/* uc->uc_mcontext.__gregs */		; \
	mov.l	r0, @((_REG_PC * 4), r1)				; \
	mov.l	r0, @((_REG_PR * 4), r1)				; \
									  \
	mov.l	r8,  @((_REG_R8  * 4), r1)				; \
	mov.l	r9,  @((_REG_R9  * 4), r1)				; \
	mov.l	r10, @((_REG_R10 * 4), r1)				; \
	mov.l	r11, @((_REG_R11 * 4), r1)				; \
	mov.l	r12, @((_REG_R12 * 4), r1)				; \
	mov.l	r13, @((_REG_R13 * 4), r1)				; \
	mov.l	r14, @((_REG_R14 * 4), r1)				; \
	/* _REG_R15 is too far to use @(disp, Rn) */			  \
									  \
	/* in Hitachi calling convention mac regs are callee saved */	  \
	sts	mach, r0						; \
	mov.l	r0, @((_REG_MACH * 4), r1)				; \
	sts	macl, r0						; \
	mov.l	r0, @((_REG_MACL * 4), r1)				; \
									  \
	mov.l	.L_uc_flags, r0						; \
									  \
	add	#(_REG_R15 * 4), r1					; \
	mov.l	r15, @r1						; \
									  \
	/* XXX:	FP registers fr12..fr15? */				  \
									  \
	mov.l	r0, @(UC_FLAGS, uc)


#define SETC(uc)							  \
	mov.l	@(UC_FLAGS, uc), r1					; \
	mov.l	.L_uc_user, r0						; \
	tst	r0, r1		/* ands and sets T if zero(!) */	; \
	bt	1f							; \
									  \
	/* a _UC_USER context */					  \
	mov.l	.L_uc_gregs_offset, r1					; \
	add	uc, r1		/* uc->uc_mcontext.__gregs */		; \
									  \
	mov.l	@((_REG_R8  * 4), r1), r8				; \
	mov.l	@((_REG_R9  * 4), r1), r9				; \
	mov.l	@((_REG_R10 * 4), r1), r10				; \
	mov.l	@((_REG_R11 * 4), r1), r11				; \
	mov.l	@((_REG_R12 * 4), r1), r12				; \
	mov.l	@((_REG_R13 * 4), r1), r13				; \
	mov.l	@((_REG_R14 * 4), r1), r14				; \
	/* _REG_R15 is too far to use @(disp, Rn) */			  \
									  \
	/* in Hitachi calling convention mac regs are callee saved */	  \
	mov.l	@((_REG_MACH * 4), r1), r0				; \
	mov.l	@((_REG_MACL * 4), r1), r2				; \
	lds	r0, mach						; \
	lds	r2, macl						; \
									  \
	/* XXX:	FP registers fr12..fr15? */				  \
									  \
	mov.l	@((_REG_PR * 4), r1), r0				; \
	mov.l	@((_REG_PC * 4), r1), r2				; \
	lds	r0, pr							; \
	add	#(_REG_R15 * 4), r1					; \
	jmp	@r2							; \
	 mov.l	@r1, r15						; \
	NOTREACHED							; \
									  \
1:	/* not a _UC_USER context, pass to setcontext(2) */		  \
	CALL_SETCONTEXT(uc)						  \
	/* NOTREACHED */

#ifdef PIC
/*
 * For PIC code we need a per-call offset to the setcontext.
 * Fortunately, since setcontext(2) does not return, we can
 * put the offset right after the call.
 */
#define CALL_SETCONTEXT(uc)						  \
	mov.l	2f, r1							; \
1:	bsrf	r1							; \
	 MAYBE_MOV_UC_R4(uc)						; \
	NOTREACHED							; \
	.align 2							; \
2: .long _C_LABEL(setcontext)-(1b+4)
#else /* !PIC */
/*
 * For static code all calls to setcontext can share single locations
 * with the address of setcontext (see below).
 */
#define CALL_SETCONTEXT(uc)						  \
	mov.l	.L_setcontext, r1					; \
	jsr	@r1							; \
	 MAYBE_MOV_UC_R4(uc)						; \
	NOTREACHED
#endif

#define MAYBE_MOV_UC_R4(uc)						  \
.ifc "uc","r4"								; \
	 nop								; \
.else									; \
	 mov	uc, r4							; \
.endif



/*
 * int _getcontext_u(ucontext_t *ctx)
 *	Store the current context in the provided ctx structure.
 */
NENTRY(_getcontext_u)
	GETC(r4)		/* r4 - ctx */
	rts
	 mov	#0, r0


/*
 * int _swapcontext_u(ucontext_t *from, const ucontext_t *to)
 *	Save the current context in `from' before switching to the
 *	new context in `to'.
 */
NENTRY(_swapcontext_u)
	GETC(r4)		/* r4 - from */
	mov	r5, r4		/* r5 - to   */
	/* FALLTHROUGH */


/*
 * int _setcontext_u(const ucontext_t *ctx)
 *	Make the context stored in ctx current.
 */
NENTRY(_setcontext_u)
	SETC(r4)		/* r4 - ctx */
	/* NOTREACHED */

	.align	2
.L_uc_gregs_offset:	.long	UC_REGS
.L_uc_flags:		.long	_UC_USER | _UC_CPU
.L_uc_user:		.long	_UC_USER
#ifndef PIC
.L_setcontext:		.long	_C_LABEL(setcontext)
#endif