summaryrefslogtreecommitdiff
path: root/libgcc/config/rs6000/morestack.S
blob: 68ab9fdd1645b4501a386a6f82d168515aa91977 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#ifdef __powerpc64__
# PowerPC64 support for -fsplit-stack.
# Copyright (C) 2009-2015 Free Software Foundation, Inc.
# Contributed by Alan Modra <amodra@gmail.com>.

# This file is part of GCC.

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

# GCC 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 General Public License
# for more details.

# Under Section 7 of GPL version 3, you are granted additional
# permissions described in the GCC Runtime Library Exception, version
# 3.1, as published by the Free Software Foundation.

# You should have received a copy of the GNU General Public License and
# a copy of the GCC Runtime Library Exception along with this program;
# see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
# <http://www.gnu.org/licenses/>.

#if _CALL_ELF == 2
	.abiversion 2
#define PARAMS 32
#else
#define PARAMS 48
#endif
#define MORESTACK_FRAMESIZE	(PARAMS+96)
#define PARAMREG_SAVE		-MORESTACK_FRAMESIZE+PARAMS+0
#define STATIC_CHAIN_SAVE	-MORESTACK_FRAMESIZE+PARAMS+64
#define R29_SAVE		-MORESTACK_FRAMESIZE+PARAMS+72
#define LINKREG_SAVE		-MORESTACK_FRAMESIZE+PARAMS+80
#define NEWSTACKSIZE_SAVE	-MORESTACK_FRAMESIZE+PARAMS+88

# Excess space needed to call ld.so resolver for lazy plt
# resolution.  Go uses sigaltstack so this doesn't need to
# also cover signal frame size.
#define BACKOFF 4096
# Large excess allocated when calling non-split-stack code.
#define NON_SPLIT_STACK 0x100000


#if _CALL_ELF == 2

#define BODY_LABEL(name) name

#define ENTRY0(name)					\
	.global name;					\
	.hidden	name;					\
	.type name,@function;				\
name##:

#define ENTRY(name)					\
	ENTRY0(name);					\
0:	addis %r2,%r12,.TOC.-0b@ha;			\
        addi %r2,%r2,.TOC.-0b@l;			\
	.localentry name, .-name

#else

#define BODY_LABEL(name) .L.##name

#define ENTRY0(name)					\
	.global name;					\
	.hidden	name;					\
	.type name,@function;				\
	.pushsection ".opd","aw";			\
	.p2align 3;					\
name##: .quad BODY_LABEL (name), .TOC.@tocbase, 0;	\
	.popsection;					\
BODY_LABEL(name)##:

#define ENTRY(name) ENTRY0(name)

#endif

#define SIZE(name) .size name, .-BODY_LABEL(name)


	.text
# Just like __morestack, but with larger excess allocation
ENTRY0(__morestack_non_split)
.LFB1:
	.cfi_startproc
# We use a cleanup to restore the tcbhead_t.__private_ss if
# an exception is thrown through this code.
#ifdef __PIC__
	.cfi_personality 0x9b,DW.ref.__gcc_personality_v0
	.cfi_lsda 0x1b,.LLSDA1
#else
	.cfi_personality 0x3,__gcc_personality_v0
	.cfi_lsda 0x3,.LLSDA1
#endif
# LR is already saved by the split-stack prologue code.
# We may as well have the unwinder skip over the call in the
# prologue too.
	.cfi_offset %lr,16

	addis %r12,%r12,-NON_SPLIT_STACK@h
	SIZE (__morestack_non_split)
# Fall through into __morestack


# This function is called with non-standard calling conventions.
# On entry, r12 is the requested stack pointer.  One version of the
# split-stack prologue that calls __morestack looks like
#	ld %r0,-0x7000-64(%r13)
#	addis %r12,%r1,-allocate@ha
#	addi %r12,%r12,-allocate@l
#	cmpld %r12,%r0
#	bge+ enough
#	mflr %r0
#	std %r0,16(%r1)
#	bl __morestack
#	ld %r0,16(%r1)
#	mtlr %r0
#	blr
# enough:
# The normal function prologue follows here, with a small addition at
# the end to set up the arg pointer.  The arg pointer is set up with:
#	addi %r12,%r1,offset
#	bge %cr7,.+8
#	mr %r12,%r29
#
# Note that the lr save slot 16(%r1) has already been used.
# r3 thru r11 possibly contain arguments and a static chain
# pointer for the function we're calling, so must be preserved.
# cr7 must also be preserved.

ENTRY0(__morestack)
# Save parameter passing registers, our arguments, lr, r29
# and use r29 as a frame pointer.
	std %r3,PARAMREG_SAVE+0(%r1)
	sub %r3,%r1,%r12		# calculate requested stack size
	mflr %r12
	std %r4,PARAMREG_SAVE+8(%r1)
	std %r5,PARAMREG_SAVE+16(%r1)
	std %r6,PARAMREG_SAVE+24(%r1)
	std %r7,PARAMREG_SAVE+32(%r1)
	addi %r3,%r3,BACKOFF
	std %r8,PARAMREG_SAVE+40(%r1)
	std %r9,PARAMREG_SAVE+48(%r1)
	std %r10,PARAMREG_SAVE+56(%r1)
	std %r11,STATIC_CHAIN_SAVE(%r1)
	std %r29,R29_SAVE(%r1)
	std %r12,LINKREG_SAVE(%r1)
	std %r3,NEWSTACKSIZE_SAVE(%r1)	# new stack size
	mr %r29,%r1
	.cfi_offset %r29,R29_SAVE
	.cfi_def_cfa_register %r29
	stdu %r1,-MORESTACK_FRAMESIZE(%r1)

	# void __morestack_block_signals (void)
	bl __morestack_block_signals

	# void *__generic_morestack (size_t *pframe_size,
	#			     void *old_stack,
	#			     size_t param_size)
	addi %r3,%r29,NEWSTACKSIZE_SAVE
	mr %r4,%r29
	li %r5,0			# no copying from old stack
	bl __generic_morestack

# Start using new stack
	stdu %r29,-32(%r3)		# back-chain
	mr %r1,%r3

# Set __private_ss stack guard for the new stack.
	ld %r12,NEWSTACKSIZE_SAVE(%r29)	# modified size
	addi %r3,%r3,BACKOFF-32
	sub %r3,%r3,%r12
# Note that a signal frame has $pc pointing at the instruction
# where the signal occurred.  For something like a timer
# interrupt this means the instruction has already executed,
# thus the region starts at the instruction modifying
# __private_ss, not one instruction after.
.LEHB0:
	std %r3,-0x7000-64(%r13)	# tcbhead_t.__private_ss

	# void __morestack_unblock_signals (void)
	bl __morestack_unblock_signals

# Set up for a call to the target function, located 3
# instructions after __morestack's return address.
#
	ld %r12,LINKREG_SAVE(%r29)
	ld %r3,PARAMREG_SAVE+0(%r29)	# restore arg regs
	ld %r4,PARAMREG_SAVE+8(%r29)
	ld %r5,PARAMREG_SAVE+16(%r29)
	ld %r6,PARAMREG_SAVE+24(%r29)
	ld %r7,PARAMREG_SAVE+32(%r29)
	ld %r8,PARAMREG_SAVE+40(%r29)
	ld %r9,PARAMREG_SAVE+48(%r29)
	addi %r0,%r12,12		# add 3 instructions
	ld %r10,PARAMREG_SAVE+56(%r29)
	ld %r11,STATIC_CHAIN_SAVE(%r29)
	cmpld %cr7,%r12,%r0		# indicate we were called
	mtctr %r0
	bctrl				# call caller!

# On return, save regs possibly used to return a value, and
# possibly trashed by calls to __morestack_block_signals,
# __generic_releasestack and __morestack_unblock_signals.
# Assume those calls don't use vector or floating point regs.
	std %r3,PARAMREG_SAVE+0(%r29)
	std %r4,PARAMREG_SAVE+8(%r29)
	std %r5,PARAMREG_SAVE+16(%r29)
	std %r6,PARAMREG_SAVE+24(%r29)
#if _CALL_ELF == 2
	std %r7,PARAMREG_SAVE+32(%r29)
	std %r8,PARAMREG_SAVE+40(%r29)
	std %r9,PARAMREG_SAVE+48(%r29)
	std %r10,PARAMREG_SAVE+56(%r29)
#endif

	bl __morestack_block_signals

	# void *__generic_releasestack (size_t *pavailable)
	addi %r3,%r29,NEWSTACKSIZE_SAVE
	bl __generic_releasestack

# Reset __private_ss stack guard to value for old stack
	ld %r12,NEWSTACKSIZE_SAVE(%r29)
	addi %r3,%r3,BACKOFF
	sub %r3,%r3,%r12
.LEHE0:
	std %r3,-0x7000-64(%r13)	# tcbhead_t.__private_ss

	bl __morestack_unblock_signals

# Use old stack again.
	mr %r1,%r29

# Restore return value regs, and return.
	ld %r0,LINKREG_SAVE(%r29)
	mtlr %r0
	ld %r3,PARAMREG_SAVE+0(%r29)
	ld %r4,PARAMREG_SAVE+8(%r29)
	ld %r5,PARAMREG_SAVE+16(%r29)
	ld %r6,PARAMREG_SAVE+24(%r29)
#if _CALL_ELF == 2
	ld %r7,PARAMREG_SAVE+32(%r29)
	ld %r8,PARAMREG_SAVE+40(%r29)
	ld %r9,PARAMREG_SAVE+48(%r29)
	ld %r10,PARAMREG_SAVE+56(%r29)
#endif
	ld %r29,R29_SAVE(%r29)
	.cfi_def_cfa_register %r1
	blr

# This is the cleanup code called by the stack unwinder when
# unwinding through code between .LEHB0 and .LEHE0 above.
cleanup:
	.cfi_def_cfa_register %r29
	std %r3,PARAMREG_SAVE(%r29)	# Save exception header
	# size_t __generic_findstack (void *stack)
	mr %r3,%r29
	bl __generic_findstack
	sub %r3,%r29,%r3
	addi %r3,%r3,BACKOFF
	std %r3,-0x7000-64(%r13)	# tcbhead_t.__private_ss
	ld %r3,PARAMREG_SAVE(%r29)
	bl _Unwind_Resume
	nop
	.cfi_endproc
	SIZE (__morestack)


	.section .gcc_except_table,"a",@progbits
	.p2align 2
.LLSDA1:
	.byte	0xff	# @LPStart format (omit)
	.byte	0xff	# @TType format (omit)
	.byte	0x1	# call-site format (uleb128)
	.uleb128 .LLSDACSE1-.LLSDACSB1	# Call-site table length
.LLSDACSB1:
	.uleb128 .LEHB0-.LFB1	# region 0 start
	.uleb128 .LEHE0-.LEHB0	# length
	.uleb128 cleanup-.LFB1	# landing pad
	.uleb128 0		# no action, ie. a cleanup
.LLSDACSE1:


#ifdef __PIC__
# Build a position independent reference to the personality function.
	.hidden DW.ref.__gcc_personality_v0
	.weak DW.ref.__gcc_personality_v0
	.section .data.DW.ref.__gcc_personality_v0,"awG",@progbits,DW.ref.__gcc_personality_v0,comdat
	.p2align 3
DW.ref.__gcc_personality_v0:
	.quad __gcc_personality_v0
	.type DW.ref.__gcc_personality_v0, @object
	.size DW.ref.__gcc_personality_v0, 8
#endif


	.text
# Initialize the stack guard when the program starts or when a
# new thread starts.  This is called from a constructor.
# void __stack_split_initialize (void)
ENTRY(__stack_split_initialize)
	addi %r3,%r1,-0x4000		# We should have at least 16K.
	std %r3,-0x7000-64(%r13)	# tcbhead_t.__private_ss
	# void __generic_morestack_set_initial_sp (void *sp, size_t len)
	mr %r3,%r1
	li %r4, 0x4000
	b __generic_morestack_set_initial_sp
	SIZE (__stack_split_initialize)


# Return current __private_ss
# void *__morestack_get_guard (void)
ENTRY0(__morestack_get_guard)
	ld %r3,-0x7000-64(%r13)		# tcbhead_t.__private_ss
	blr
	SIZE (__morestack_get_guard)


# Set __private_ss
# void __morestack_set_guard (void *ptr)
ENTRY0(__morestack_set_guard)
	std %r3,-0x7000-64(%r13)	# tcbhead_t.__private_ss
	blr
	SIZE (__morestack_set_guard)


# Return the stack guard value for given stack
# void *__morestack_make_guard (void *stack, size_t size)
ENTRY0(__morestack_make_guard)
	sub %r3,%r3,%r4
	addi %r3,%r3,BACKOFF
	blr
	SIZE (__morestack_make_guard)


# Make __stack_split_initialize a high priority constructor.
	.section .ctors.65535,"aw",@progbits
	.p2align 3
	.quad __stack_split_initialize
	.quad __morestack_load_mmap

	.section .note.GNU-stack,"",@progbits
	.section .note.GNU-split-stack,"",@progbits
	.section .note.GNU-no-split-stack,"",@progbits
#endif /* __powerpc64__ */