blob: f56dac120f5f254f1be63654543b21651e67e554 [file] [log] [blame]
Russ Coxd9fd1142011-02-22 17:40:40 -05001// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5/*
6Stack layout parameters.
7Included both by runtime (compiled via 6c) and linkers (compiled via gcc).
8
9The per-goroutine g->stackguard is set to point StackGuard bytes
10above the bottom of the stack. Each function compares its stack
11pointer against g->stackguard to check for overflow. To cut one
12instruction from the check sequence for functions with tiny frames,
13the stack is allowed to protrude StackSmall bytes below the stack
14guard. Functions with large frames don't bother with the check and
15always call morestack. The sequences are (for amd64, others are
16similar):
17
18 guard = g->stackguard
19 frame = function's stack frame size
20 argsize = size of function arguments (call + return)
21
22 stack frame size <= StackSmall:
23 CMPQ guard, SP
24 JHI 3(PC)
25 MOVQ m->morearg, $(argsize << 32)
26 CALL morestack(SB)
27
28 stack frame size > StackSmall but < StackBig
29 LEAQ (frame-StackSmall)(SP), R0
30 CMPQ guard, R0
31 JHI 3(PC)
32 MOVQ m->morearg, $(argsize << 32)
33 CALL morestack(SB)
34
35 stack frame size >= StackBig:
36 MOVQ m->morearg, $((argsize << 32) | frame)
37 CALL morestack(SB)
38
39The bottom StackGuard - StackSmall bytes are important: there has
40to be enough room to execute functions that refuse to check for
41stack overflow, either because they need to be adjacent to the
42actual caller's frame (deferproc) or because they handle the imminent
43stack overflow (morestack).
44
45For example, deferproc might call malloc, which does one of the
46above checks (without allocating a full frame), which might trigger
47a call to morestack. This sequence needs to fit in the bottom
48section of the stack. On amd64, morestack's frame is 40 bytes, and
49deferproc's frame is 56 bytes. That fits well within the
50StackGuard - StackSmall = 128 bytes at the bottom.
51The linkers explore all possible call traces involving non-splitting
52functions to make sure that this limit cannot be violated.
53 */
54
55enum {
Alexey Borzenkovb701cf32011-05-16 16:57:49 -040056 // StackSystem is a number of additional bytes to add
57 // to each stack below the usual guard area for OS-specific
58 // purposes like signal handling. Used on Windows because
59 // it does not use a separate stack.
Russ Cox851f3012011-12-16 15:33:58 -050060#ifdef GOOS_windows
Hector Chu6bc03462011-09-17 20:39:29 +100061 StackSystem = 512 * sizeof(uintptr),
Alexey Borzenkovb701cf32011-05-16 16:57:49 -040062#else
63 StackSystem = 0,
64#endif
65
Russ Coxd9fd1142011-02-22 17:40:40 -050066 // The amount of extra stack to allocate beyond the size
67 // needed for the single frame that triggered the split.
68 StackExtra = 1024,
69
70 // The minimum stack segment size to allocate.
71 // If the amount needed for the splitting frame + StackExtra
72 // is less than this number, the stack will have this size instead.
73 StackMin = 4096,
Alex Brainmandde43552011-07-14 09:13:39 +100074 FixedStack = StackMin + StackSystem,
Russ Coxd9fd1142011-02-22 17:40:40 -050075
76 // Functions that need frames bigger than this call morestack
77 // unconditionally. That is, on entry to a function it is assumed
78 // that the amount of space available in the current stack segment
79 // couldn't possibly be bigger than StackBig. If stack segments
80 // do run with more space than StackBig, the space may not be
81 // used efficiently. As a result, StackBig should not be significantly
82 // smaller than StackMin or StackExtra.
83 StackBig = 4096,
84
85 // The stack guard is a pointer this many bytes above the
86 // bottom of the stack.
Alexey Borzenkovb701cf32011-05-16 16:57:49 -040087 StackGuard = 256 + StackSystem,
Russ Coxd9fd1142011-02-22 17:40:40 -050088
89 // After a stack split check the SP is allowed to be this
90 // many bytes below the stack guard. This saves an instruction
91 // in the checking sequence for tiny frames.
92 StackSmall = 128,
93
94 // The maximum number of bytes that a chain of NOSPLIT
95 // functions can use.
Alexey Borzenkovb701cf32011-05-16 16:57:49 -040096 StackLimit = StackGuard - StackSystem - StackSmall,
Russ Coxd9fd1142011-02-22 17:40:40 -050097};