OSDN Git Service

2011-05-04 Chris Demetriou <cgd@google.com>
[pf3gnuchains/gcc-fork.git] / libgcc / config / i386 / morestack.S
1 # x86/x86_64 support for -fsplit-stack.
2 # Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 # Contributed by Ian Lance Taylor <iant@google.com>.
4
5 # This file is part of GCC.
6
7 # GCC is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 3, or (at your option) any later
10 # version.
11
12 # GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 # for more details.
16
17 # Under Section 7 of GPL version 3, you are granted additional
18 # permissions described in the GCC Runtime Library Exception, version
19 # 3.1, as published by the Free Software Foundation.
20
21 # You should have received a copy of the GNU General Public License and
22 # a copy of the GCC Runtime Library Exception along with this program;
23 # see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 # <http://www.gnu.org/licenses/>.
25
26
27 # Support for allocating more stack space when using -fsplit-stack.
28 # When a function discovers that it needs more stack space, it will
29 # call __morestack with the size of the stack frame and the size of
30 # the parameters to copy from the old stack frame to the new one.
31 # The __morestack function preserves the parameter registers and
32 # calls __generic_morestack to actually allocate the stack space.
33
34 # When this is called stack space is very low, but we ensure that
35 # there is enough space to push the parameter registers and to call
36 # __generic_morestack.
37
38 # When calling __generic_morestack, FRAME_SIZE points to the size of
39 # the desired frame when the function is called, and the function
40 # sets it to the size of the allocated stack.  OLD_STACK points to
41 # the parameters on the old stack and PARAM_SIZE is the number of
42 # bytes of parameters to copy to the new stack.  These are the
43 # parameters of the function that called __morestack.  The
44 # __generic_morestack function returns the new stack pointer,
45 # pointing to the address of the first copied parameter.  The return
46 # value minus the returned *FRAME_SIZE will be the first address on
47 # the stack which we should not use.
48
49 # void *__generic_morestack (size_t *frame_size, void *old_stack,
50 #                            size_t param_size);
51
52 # The __morestack routine has to arrange for the caller to return to a
53 # stub on the new stack.  The stub is responsible for restoring the
54 # old stack pointer and returning to the caller's caller.  This calls
55 # __generic_releasestack to retrieve the old stack pointer and release
56 # the newly allocated stack.
57
58 # void *__generic_releasestack (size_t *available);
59
60 # We do a little dance so that the processor's call/return return
61 # address prediction works out.  The compiler arranges for the caller
62 # to look like this:
63 #   call __generic_morestack
64 #   ret
65 #  L:
66 #   // carry on with function
67 # After we allocate more stack, we call L, which is in our caller.
68 # When that returns (to the predicted instruction), we release the
69 # stack segment and reset the stack pointer.  We then return to the
70 # predicted instruction, namely the ret instruction immediately after
71 # the call to __generic_morestack.  That then returns to the caller of
72 # the original caller.
73
74
75 # The amount of extra space we ask for.  In general this has to be
76 # enough for the dynamic loader to find a symbol and for a signal
77 # handler to run.
78         
79 #ifndef __x86_64__
80 #define BACKOFF (1024)
81 #else
82 #define BACKOFF (1536)
83 #endif
84
85
86 # This entry point is for split-stack code which calls non-split-stack
87 # code.  When the linker sees this case, it converts the call to
88 # __morestack to call __morestack_non_split instead.  We just bump the
89 # requested stack space by 16K.
90
91         .global __morestack_non_split
92         .hidden __morestack_non_split
93
94 #ifdef __ELF__
95        .type    __morestack_non_split,@function
96 #endif
97
98 __morestack_non_split:
99
100 #ifndef __x86_64__
101         addl    $0x4000,4(%esp)
102 #else
103         addq    $0x4000,%r10
104 #endif
105
106 #ifdef __ELF__
107         .size   __morestack_non_split, . - __morestack_non_split
108 #endif
109
110 # __morestack_non_split falls through into __morestack.
111
112
113 # The __morestack function.
114
115         .global __morestack
116         .hidden __morestack
117
118 #ifdef __ELF__
119         .type   __morestack,@function
120 #endif
121
122 __morestack:
123 .LFB1:
124         .cfi_startproc
125
126
127 #ifndef __x86_64__
128
129
130 # The 32-bit __morestack function.
131
132         # We use a cleanup to restore the stack guard if an exception
133         # is thrown through this code.
134 #ifndef __PIC__
135         .cfi_personality 0,__gcc_personality_v0
136         .cfi_lsda 0,.LLSDA1
137 #else
138         .cfi_personality 0x9b,DW.ref.__gcc_personality_v0
139         .cfi_lsda 0x1b,.LLSDA1
140 #endif
141
142         # Set up a normal backtrace.
143         pushl   %ebp
144         .cfi_def_cfa_offset 8
145         .cfi_offset %ebp, -8
146         movl    %esp, %ebp
147         .cfi_def_cfa_register %ebp
148
149         # We return below with a ret $8.  We will return to a single
150         # return instruction, which will return to the caller of our
151         # caller.  We let the unwinder skip that single return
152         # instruction, and just return to the real caller.
153         .cfi_offset 8, 8
154         .cfi_escape 0x15, 4, 0x7d       # DW_CFA_val_offset_sf, %esp, 12/-4
155
156         # In 32-bit mode the parameters are pushed on the stack.  The
157         # argument size is pushed then the new stack frame size is
158         # pushed.
159
160         # In 32-bit mode the registers %eax, %edx, and %ecx may be
161         # used for parameters, depending on the regparm and fastcall
162         # attributes.
163
164         pushl   %eax
165         pushl   %edx
166         pushl   %ecx
167
168         call    __morestack_block_signals
169
170         pushl   12(%ebp)                # The size of the parameters.
171         leal    20(%ebp),%eax           # Address of caller's parameters.
172         pushl   %eax
173         addl    $BACKOFF,8(%ebp)        # Ask for backoff bytes.
174         leal    8(%ebp),%eax            # The address of the new frame size.
175         pushl   %eax
176
177         # Note that %esp is exactly 32 bytes below the CFA -- perfect for
178         # a 16-byte aligned stack.  That said, we still ought to compile
179         # generic-morestack.c with -mpreferred-stack-boundary=2.  FIXME.
180         call    __generic_morestack
181
182         movl    %eax,%esp               # Switch to the new stack.
183         subl    8(%ebp),%eax            # The end of the stack space.
184         addl    $BACKOFF,%eax           # Back off 512 bytes.
185
186 .LEHB0:
187         # FIXME: The offset must match
188         # TARGET_THREAD_SPLIT_STACK_OFFSET in
189         # gcc/config/i386/linux.h.
190         movl    %eax,%gs:0x30           # Save the new stack boundary.
191
192         call    __morestack_unblock_signals
193
194         movl    -8(%ebp),%edx           # Restore registers.
195         movl    -12(%ebp),%ecx
196
197         movl    4(%ebp),%eax            # Increment the return address
198         cmpb    $0xc3,(%eax)            # to skip the ret instruction;
199         je      1f                      # see above.
200         addl    $2,%eax
201 1:      inc     %eax
202
203         movl    %eax,-8(%ebp)           # Store return address in an
204                                         # unused slot.
205
206         movl    -4(%ebp),%eax           # Restore the last register.
207
208         call    *-8(%ebp)               # Call our caller!
209
210         # The caller will return here, as predicted.
211
212         # Save the registers which may hold a return value.  We
213         # assume that __generic_releasestack does not touch any
214         # floating point or vector registers.
215         pushl   %eax
216         pushl   %edx
217
218         # Push the arguments to __generic_releasestack now so that the
219         # stack is at a 16-byte boundary for
220         # __morestack_block_signals.
221         pushl   $0                      # Where the available space is returned.
222         leal    0(%esp),%eax            # Push its address.
223         push    %eax
224
225         call    __morestack_block_signals
226
227         call    __generic_releasestack
228
229         subl    4(%esp),%eax            # Subtract available space.
230         addl    $BACKOFF,%eax           # Back off 512 bytes.
231 .LEHE0:
232         movl    %eax,%gs:0x30           # Save the new stack boundary.
233
234         addl    $8,%esp                 # Remove values from stack.
235
236         # We need to restore the old stack pointer, which is in %rbp,
237         # before we unblock signals.  We also need to restore %eax and
238         # %edx after we unblock signals but before we return.  Do this
239         # by moving %eax and %edx from the current stack to the old
240         # stack.
241
242         popl    %edx                    # Pop return value from current stack.
243         popl    %eax
244
245         movl    %ebp,%esp               # Restore stack pointer.
246
247         pushl   %eax                    # Push return value on old stack.
248         pushl   %edx
249         subl    $8,%esp                 # Align stack to 16-byte boundary.
250
251         call    __morestack_unblock_signals
252
253         addl    $8,%esp
254         popl    %edx                    # Restore return value.
255         popl    %eax
256
257         .cfi_remember_state
258         popl    %ebp
259         .cfi_restore %ebp
260         .cfi_def_cfa %esp, 12
261         ret     $8                      # Return to caller, which will
262                                         # immediately return.  Pop
263                                         # arguments as we go.
264
265 # This is the cleanup code called by the stack unwinder when unwinding
266 # through the code between .LEHB0 and .LEHE0 above.
267         
268 .L1:
269         .cfi_restore_state
270         subl    $16,%esp                # Maintain 16 byte alignment.
271         movl    %eax,4(%esp)            # Save exception header.
272         movl    %ebp,(%esp)             # Stack pointer after resume.
273         call    __generic_findstack
274         movl    %ebp,%ecx               # Get the stack pointer.
275         subl    %eax,%ecx               # Subtract available space.
276         addl    $BACKOFF,%ecx           # Back off 512 bytes.
277         movl    %ecx,%gs:0x30           # Save new stack boundary.
278         movl    4(%esp),%eax            # Function argument.
279         movl    %eax,(%esp)
280 #ifdef __PIC__
281         call    __x86.get_pc_thunk.bx   # %ebx may not be set up for us.
282         addl    $_GLOBAL_OFFSET_TABLE_, %ebx
283         call    _Unwind_Resume@PLT      # Resume unwinding.
284 #else
285         call    _Unwind_Resume
286 #endif
287
288 #else /* defined(__x86_64__) */
289
290
291 # The 64-bit __morestack function.
292
293         # We use a cleanup to restore the stack guard if an exception
294         # is thrown through this code.
295 #ifndef __PIC__
296         .cfi_personality 0x3,__gcc_personality_v0
297         .cfi_lsda 0x3,.LLSDA1
298 #else
299         .cfi_personality 0x9b,DW.ref.__gcc_personality_v0
300         .cfi_lsda 0x1b,.LLSDA1
301 #endif
302
303         # Set up a normal backtrace.
304         pushq   %rbp
305         .cfi_def_cfa_offset 16
306         .cfi_offset %rbp, -16
307         movq    %rsp, %rbp
308         .cfi_def_cfa_register %rbp
309
310         # We will return a single return instruction, which will
311         # return to the caller of our caller.  Let the unwinder skip
312         # that single return instruction, and just return to the real
313         # caller.
314         .cfi_offset 16, 0
315         .cfi_escape 0x15, 7, 0x7f       # DW_CFA_val_offset_sf, %esp, 8/-8
316
317         # In 64-bit mode the new stack frame size is passed in r10
318         # and the argument size is passed in r11.
319
320         addq    $BACKOFF,%r10           # Ask for backoff bytes.
321         pushq   %r10                    # Save new frame size.
322
323         # In 64-bit mode the registers %rdi, %rsi, %rdx, %rcx, %r8,
324         # and %r9 may be used for parameters.  We also preserve %rax
325         # which the caller may use to hold %r10.
326
327         pushq   %rax
328         pushq   %rdi
329         pushq   %rsi
330         pushq   %rdx
331         pushq   %rcx
332         pushq   %r8
333         pushq   %r9
334
335         pushq   %r11
336         pushq   $0                      # For alignment.
337
338         call    __morestack_block_signals
339
340         leaq    -8(%rbp),%rdi           # Address of new frame size.
341         leaq    24(%rbp),%rsi           # The caller's parameters.
342         addq    $8,%rsp
343         popq    %rdx                    # The size of the parameters.
344
345         call    __generic_morestack
346
347         movq    -8(%rbp),%r10           # Reload modified frame size
348         movq    %rax,%rsp               # Switch to the new stack.
349         subq    %r10,%rax               # The end of the stack space.
350         addq    $BACKOFF,%rax           # Back off 1024 bytes.
351
352 .LEHB0:
353         # FIXME: The offset must match
354         # TARGET_THREAD_SPLIT_STACK_OFFSET in
355         # gcc/config/i386/linux64.h.
356         movq    %rax,%fs:0x70           # Save the new stack boundary.
357
358         call    __morestack_unblock_signals
359
360         movq    -24(%rbp),%rdi          # Restore registers.
361         movq    -32(%rbp),%rsi
362         movq    -40(%rbp),%rdx
363         movq    -48(%rbp),%rcx
364         movq    -56(%rbp),%r8
365         movq    -64(%rbp),%r9
366
367         movq    8(%rbp),%r10            # Increment the return address
368         incq    %r10                    # to skip the ret instruction;
369                                         # see above.
370
371         movq    -16(%rbp),%rax          # Restore caller's %rax.
372
373         call    *%r10                   # Call our caller!
374
375         # The caller will return here, as predicted.
376
377         # Save the registers which may hold a return value.  We
378         # assume that __generic_releasestack does not touch any
379         # floating point or vector registers.
380         pushq   %rax
381         pushq   %rdx
382
383         call    __morestack_block_signals
384
385         pushq   $0                      # For alignment.
386         pushq   $0                      # Where the available space is returned.
387         leaq    0(%rsp),%rdi            # Pass its address.
388
389         call    __generic_releasestack
390
391         subq    0(%rsp),%rax            # Subtract available space.
392         addq    $BACKOFF,%rax           # Back off 1024 bytes.
393 .LEHE0:
394         movq    %rax,%fs:0x70           # Save the new stack boundary.
395
396         addq    $16,%rsp                # Remove values from stack.
397
398         # We need to restore the old stack pointer, which is in %rbp,
399         # before we unblock signals.  We also need to restore %rax and
400         # %rdx after we unblock signals but before we return.  Do this
401         # by moving %rax and %rdx from the current stack to the old
402         # stack.
403
404         popq    %rdx                    # Pop return value from current stack.
405         popq    %rax
406
407         movq    %rbp,%rsp               # Restore stack pointer.
408
409         pushq   %rax                    # Push return value on old stack.
410         pushq   %rdx
411
412         call    __morestack_unblock_signals
413
414         popq    %rdx                    # Restore return value.
415         popq    %rax
416
417         .cfi_remember_state
418         popq    %rbp
419         .cfi_restore %rbp
420         .cfi_def_cfa %rsp, 8
421         ret                             # Return to caller, which will
422                                         # immediately return.
423
424 # This is the cleanup code called by the stack unwinder when unwinding
425 # through the code between .LEHB0 and .LEHE0 above.
426         
427 .L1:
428         .cfi_restore_state
429         subq    $16,%rsp                # Maintain 16 byte alignment.
430         movq    %rax,(%rsp)             # Save exception header.
431         movq    %rbp,%rdi               # Stack pointer after resume.
432         call    __generic_findstack
433         movq    %rbp,%rcx               # Get the stack pointer.
434         subq    %rax,%rcx               # Subtract available space.
435         addq    $BACKOFF,%rcx           # Back off 1024 bytes.
436         movq    %rcx,%fs:0x70           # Save new stack boundary.
437         movq    (%rsp),%rdi             # Restore exception data for call.
438 #ifdef __PIC__
439         call    _Unwind_Resume@PLT      # Resume unwinding.
440 #else
441         call    _Unwind_Resume          # Resume unwinding.
442 #endif
443
444 #endif /* defined(__x86_64__) */
445
446         .cfi_endproc
447 #ifdef __ELF__
448         .size   __morestack, . - __morestack
449 #endif
450
451 #if !defined(__x86_64__) && defined(__PIC__)
452 # Output the thunk to get PC into bx, since we use it above.
453         .section        .text.__x86.get_pc_thunk.bx,"axG",@progbits,__x86.get_pc_thunk.bx,comdat
454         .globl  __x86.get_pc_thunk.bx
455         .hidden __x86.get_pc_thunk.bx
456 #ifdef __ELF__
457         .type   __x86.get_pc_thunk.bx, @function
458 #endif
459 __x86.get_pc_thunk.bx:
460         .cfi_startproc
461         movl    (%esp), %ebx
462         ret
463         .cfi_endproc
464 #ifdef __ELF__
465         .size   __x86.get_pc_thunk.bx, . - __x86.get_pc_thunk.bx
466 #endif
467 #endif
468
469 # The exception table.  This tells the personality routine to execute
470 # the exception handler.
471
472         .section        .gcc_except_table,"a",@progbits
473         .align  4
474 .LLSDA1:
475         .byte   0xff    # @LPStart format (omit)
476         .byte   0xff    # @TType format (omit)
477         .byte   0x1     # call-site format (uleb128)
478         .uleb128 .LLSDACSE1-.LLSDACSB1  # Call-site table length
479 .LLSDACSB1:
480         .uleb128 .LEHB0-.LFB1   # region 0 start
481         .uleb128 .LEHE0-.LEHB0  # length
482         .uleb128 .L1-.LFB1      # landing pad
483         .uleb128 0              # action
484 .LLSDACSE1:
485
486
487         .global __gcc_personality_v0
488 #ifdef __PIC__
489         # Build a position independent reference to the basic
490         # personality function.
491         .hidden DW.ref.__gcc_personality_v0
492         .weak   DW.ref.__gcc_personality_v0
493         .section .data.DW.ref.__gcc_personality_v0,"awG",@progbits,DW.ref.__gcc_personality_v0,comdat
494         .type   DW.ref.__gcc_personality_v0, @object
495 DW.ref.__gcc_personality_v0:
496 #ifndef __x86_64
497         .align 4
498         .size   DW.ref.__gcc_personality_v0, 4
499         .long   __gcc_personality_v0
500 #else
501         .align 8
502         .size   DW.ref.__gcc_personality_v0, 8
503         .quad   __gcc_personality_v0
504 #endif
505 #endif
506
507 #ifdef __x86_64__
508
509 # This entry point is used for the large model.  With this entry point
510 # the upper 32 bits of %r10 hold the argument size and the lower 32
511 # bits hold the new stack frame size.  There doesn't seem to be a way
512 # to know in the assembler code that we are assembling for the large
513 # model, and there doesn't seem to be a large model multilib anyhow.
514 # If one is developed, then the non-PIC code is probably OK since we
515 # will probably be close to the morestack code, but the PIC code
516 # almost certainly needs to be changed.  FIXME.
517
518         .text
519         .global __morestack_large_model
520         .hidden __morestack_large_model
521
522 #ifdef __ELF__
523         .type   __morestack_large_model,@function
524 #endif
525
526 __morestack_large_model:
527
528         .cfi_startproc
529
530         movq    %r10, %r11
531         andl    $0xffffffff, %r10d
532         sarq    $32, %r11
533         jmp     __morestack
534
535         .cfi_endproc
536 #ifdef __ELF__
537        .size    __morestack_large_model, . - __morestack_large_model
538 #endif
539
540 #endif /* __x86_64__ */
541
542 # Initialize the stack test value when the program starts or when a
543 # new thread starts.  We don't know how large the main stack is, so we
544 # guess conservatively.  We might be able to use getrlimit here.
545
546         .text
547         .global __stack_split_initialize
548         .hidden __stack_split_initialize
549
550 #ifdef __ELF__
551         .type   __stack_split_initialize, @function
552 #endif
553
554 __stack_split_initialize:
555
556 #ifndef __x86_64__
557
558         leal    -16000(%esp),%eax       # We should have at least 16K.
559         movl    %eax,%gs:0x30
560         pushl   $16000
561         pushl   %esp
562 #ifdef __PIC__
563         call    __generic_morestack_set_initial_sp@PLT
564 #else
565         call    __generic_morestack_set_initial_sp
566 #endif
567         addl    $8,%esp
568         ret
569
570 #else /* defined(__x86_64__) */
571
572         leaq    -16000(%rsp),%rax       # We should have at least 16K.
573         movq    %rax,%fs:0x70
574         movq    %rsp,%rdi
575         movq    $16000,%rsi
576 #ifdef __PIC__
577         call    __generic_morestack_set_initial_sp@PLT
578 #else
579         call    __generic_morestack_set_initial_sp
580 #endif
581         ret
582
583 #endif /* defined(__x86_64__) */
584
585 #ifdef __ELF__
586         .size   __stack_split_initialize, . - __stack_split_initialize
587 #endif
588
589
590 # Make __stack_split_initialize a high priority constructor.  FIXME:
591 # This is ELF specific.
592
593         .section        .ctors.65535,"aw",@progbits
594
595 #ifndef __x86_64__
596         .align  4
597         .long   __stack_split_initialize
598         .long   __morestack_load_mmap
599 #else
600         .align  8
601         .quad   __stack_split_initialize
602         .quad   __morestack_load_mmap
603 #endif
604
605 #ifdef __ELF__
606         .section        .note.GNU-stack,"",@progbits
607         .section        .note.GNU-split-stack,"",@progbits
608         .section        .note.GNU-no-split-stack,"",@progbits
609 #endif