summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/mips/makecontext.S
blob: 6427339a66c2236a5d5ebb037a49a1c8a34355a8 (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
186
187
188
/* Modify saved context.
   Copyright (C) 2009 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Maciej W. Rozycki <macro@codesourcery.com>.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include <sysdep.h>
#include <sys/asm.h>
#include <sys/fpregdef.h>
#include <sys/regdef.h>

#include "ucontext_i.h"

/* int makecontext (ucontext_t *ucp, (void *func) (), int argc, ...) */

	.text
LOCALSZ = 0
ARGSZ = 0
MASK = 0x00000000
#ifdef __PIC__
LOCALSZ = 1						/* save gp */
#endif
#if _MIPS_SIM != _ABIO32
ARGSZ = 5						/* save a3-a7 */
# ifdef __PIC__
MASK = 0x10000000
# endif
#endif
FRAMESZ = (((ARGSZ + LOCALSZ) * SZREG) + ALSZ) & ALMASK
GPOFF = FRAMESZ - ((ARGSZ + 1) * SZREG)
#if _MIPS_SIM != _ABIO32
A3OFF = FRAMESZ - (5 * SZREG)				/* callee-allocated */
A4OFF = FRAMESZ - (4 * SZREG)
A5OFF = FRAMESZ - (3 * SZREG)
A6OFF = FRAMESZ - (2 * SZREG)
A7OFF = FRAMESZ - (1 * SZREG)
NARGREGS = 8
#else
A3OFF = FRAMESZ + (3 * SZREG)				/* caller-allocated */
NARGREGS = 4
#endif

NESTED (__makecontext, FRAMESZ, ra)
	.mask	MASK, -(ARGSZ * SZREG)
	.fmask	0x00000000, 0

98:
#ifdef __PIC__
	SETUP_GP
#endif

	PTR_ADDIU sp, -FRAMESZ

#ifdef __PIC__
	SETUP_GP64 (GPOFF, __makecontext)
	SAVE_GP (GPOFF)
#endif

#ifdef PROF
	.set	noat
	move	AT, ra
	jal	_mcount
	.set	at
#endif

	/* Store args to be passed.  */
	REG_S	a3, A3OFF(sp)
#if _MIPS_SIM != _ABIO32
	REG_S	a4, A4OFF(sp)
	REG_S	a5, A5OFF(sp)
	REG_S	a6, A6OFF(sp)
	REG_S	a7, A7OFF(sp)
#endif

	/* Store a magic flag.  */
	li	v1, 1
	REG_S	v1, (0 * SZREG + MCONTEXT_GREGS)(a0)	/* zero */

	/* Set up the stack.  */
	PTR_L	t0, STACK_SP(a0)
	PTR_L	t2, STACK_SIZE(a0)
	PTR_ADDIU t1, sp, A3OFF
	PTR_ADDU t0, t2
	and	t0, ALMASK
	blez	a2, 2f					/* no arguments */

	/* Store register arguments.  */
	PTR_ADDIU t2, a0, MCONTEXT_GREGS + 4 * SZREG
	move	t3, zero
0:
	addiu	t3, 1
	REG_L	v1, (t1)
	PTR_ADDIU t1, SZREG
	REG_S	v1, (t2)
	PTR_ADDIU t2, SZREG
	bgeu	t3, a2, 2f				/* all done */
	bltu	t3, NARGREGS, 0b			/* next */

	/* Make room for stack arguments.  */
	PTR_SUBU t2, a2, t3
	PTR_SLL	t2, 3
	PTR_SUBU t0, t2
	and	t0, ALMASK

	/* Store stack arguments.  */
	move	t2, t0
1:
	addiu	t3, 1
	REG_L	v1, (t1)
	PTR_ADDIU t1, SZREG
	REG_S	v1, (t2)
	PTR_ADDIU t2, SZREG
	bltu	t3, a2, 1b				/* next */

2:
#if _MIPS_SIM == _ABIO32
	/* Make room for a0-a3 storage.  */
	PTR_ADDIU t0, -(NARGSAVE * SZREG)
#endif
	PTR_L	v1, UCONTEXT_LINK(a0)
#ifdef __PIC__
	PTR_ADDIU t9, 99f - 98b
#else
	PTR_LA	t9, 99f
#endif
	REG_S	t0, (29 * SZREG + MCONTEXT_GREGS)(a0)	/* sp */
	REG_S	v1, (16 * SZREG + MCONTEXT_GREGS)(a0)	/* s0 */
#ifdef __PIC__
	REG_S	gp, (17 * SZREG + MCONTEXT_GREGS)(a0)	/* s1 */
#endif
	REG_S	t9, (31 * SZREG + MCONTEXT_GREGS)(a0)	/* ra */
	REG_S	a1, MCONTEXT_PC(a0)

#ifdef __PIC__
	RESTORE_GP64
	PTR_ADDIU sp, FRAMESZ
#endif
	jr	ra

99:
#ifdef __PIC__
	move	gp, s1
#endif
	move	a0, zero
	beqz	s0, 0f

	/* setcontext (ucp) */
	move	a0, s0
#ifdef __PIC__
	PTR_LA	t9, JUMPTARGET (__setcontext)
	jalr	t9
# if _MIPS_SIM == _ABIO32
	move	gp, s1
# endif
#else
	jal	JUMPTARGET (__setcontext)
#endif
	move	a0, v0

0:
	/* exit (a0) */
#ifdef __PIC__
	PTR_LA	t9, HIDDEN_JUMPTARGET (exit)
	jalr	t9
#else
	jal	HIDDEN_JUMPTARGET (exit)
#endif

	/* You don't exist, you won't feel anything.  */
1:
	lb	zero, (zero)
	b	1b
PSEUDO_END (__makecontext)

weak_alias (__makecontext, makecontext)