xref: /aosp_15_r20/external/compiler-rt/lib/builtins/x86_64/chkstk2.S (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot// This file is dual licensed under the MIT and the University of Illinois Open
2*7c3d14c8STreehugger Robot// Source Licenses. See LICENSE.TXT for details.
3*7c3d14c8STreehugger Robot
4*7c3d14c8STreehugger Robot#include "../assembly.h"
5*7c3d14c8STreehugger Robot
6*7c3d14c8STreehugger Robot#ifdef __x86_64__
7*7c3d14c8STreehugger Robot
8*7c3d14c8STreehugger Robot// _chkstk (_alloca) routine - probe stack between %rsp and (%rsp-%rax) in 4k increments,
9*7c3d14c8STreehugger Robot// then decrement %rsp by %rax.  Preserves all registers except %rsp and flags.
10*7c3d14c8STreehugger Robot// This routine is windows specific
11*7c3d14c8STreehugger Robot// http://msdn.microsoft.com/en-us/library/ms648426.aspx
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robot.text
14*7c3d14c8STreehugger Robot.balign 4
15*7c3d14c8STreehugger RobotDEFINE_COMPILERRT_FUNCTION(__alloca)
16*7c3d14c8STreehugger Robot        mov    %rcx,%rax        // x64 _alloca is a normal function with parameter in rcx
17*7c3d14c8STreehugger Robot        // fallthrough
18*7c3d14c8STreehugger RobotDEFINE_COMPILERRT_FUNCTION(___chkstk)
19*7c3d14c8STreehugger Robot        push   %rcx
20*7c3d14c8STreehugger Robot        cmp    $0x1000,%rax
21*7c3d14c8STreehugger Robot        lea    16(%rsp),%rcx     // rsp before calling this routine -> rcx
22*7c3d14c8STreehugger Robot        jb     1f
23*7c3d14c8STreehugger Robot2:
24*7c3d14c8STreehugger Robot        sub    $0x1000,%rcx
25*7c3d14c8STreehugger Robot        test   %rcx,(%rcx)
26*7c3d14c8STreehugger Robot        sub    $0x1000,%rax
27*7c3d14c8STreehugger Robot        cmp    $0x1000,%rax
28*7c3d14c8STreehugger Robot        ja     2b
29*7c3d14c8STreehugger Robot1:
30*7c3d14c8STreehugger Robot        sub    %rax,%rcx
31*7c3d14c8STreehugger Robot        test   %rcx,(%rcx)
32*7c3d14c8STreehugger Robot
33*7c3d14c8STreehugger Robot        lea    8(%rsp),%rax     // load pointer to the return address into rax
34*7c3d14c8STreehugger Robot        mov    %rcx,%rsp        // install the new top of stack pointer into rsp
35*7c3d14c8STreehugger Robot        mov    -8(%rax),%rcx    // restore rcx
36*7c3d14c8STreehugger Robot        push   (%rax)           // push return address onto the stack
37*7c3d14c8STreehugger Robot        sub    %rsp,%rax        // restore the original value in rax
38*7c3d14c8STreehugger Robot        ret
39*7c3d14c8STreehugger RobotEND_COMPILERRT_FUNCTION(___chkstk)
40*7c3d14c8STreehugger RobotEND_COMPILERRT_FUNCTION(__alloca)
41*7c3d14c8STreehugger Robot
42*7c3d14c8STreehugger Robot#endif // __x86_64__
43