annotate libgo/runtime/stack.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2009 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 // Stack scanning code for the garbage collector.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 #include "runtime.h"
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 #ifdef USING_SPLIT_STACK
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 extern void * __splitstack_find (void *, void *, size_t *, void **, void **,
kono
parents:
diff changeset
12 void **);
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 extern void * __splitstack_find_context (void *context[10], size_t *, void **,
kono
parents:
diff changeset
15 void **, void **);
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 #endif
kono
parents:
diff changeset
18
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
19 bool runtime_usestackmaps;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
20
111
kono
parents:
diff changeset
21 // Calling unwind_init in doscanstack only works if it does not do a
kono
parents:
diff changeset
22 // tail call to doscanstack1.
kono
parents:
diff changeset
23 #pragma GCC optimize ("-fno-optimize-sibling-calls")
kono
parents:
diff changeset
24
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
25 extern void scanstackblock(uintptr addr, uintptr size, void *gcw)
111
kono
parents:
diff changeset
26 __asm__("runtime.scanstackblock");
kono
parents:
diff changeset
27
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
28 static bool doscanstack1(G*, void*)
111
kono
parents:
diff changeset
29 __attribute__ ((noinline));
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 // Scan gp's stack, passing stack chunks to scanstackblock.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
32 bool doscanstack(G *gp, void* gcw) {
111
kono
parents:
diff changeset
33 // Save registers on the stack, so that if we are scanning our
kono
parents:
diff changeset
34 // own stack we will see them.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
35 if (!runtime_usestackmaps) {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
36 __builtin_unwind_init();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
37 flush_registers_to_secondary_stack();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
38 }
111
kono
parents:
diff changeset
39
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
40 return doscanstack1(gp, gcw);
111
kono
parents:
diff changeset
41 }
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 // Scan gp's stack after saving registers.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
44 static bool doscanstack1(G *gp, void *gcw) {
111
kono
parents:
diff changeset
45 #ifdef USING_SPLIT_STACK
kono
parents:
diff changeset
46 void* sp;
kono
parents:
diff changeset
47 size_t spsize;
kono
parents:
diff changeset
48 void* next_segment;
kono
parents:
diff changeset
49 void* next_sp;
kono
parents:
diff changeset
50 void* initial_sp;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
51 G* _g_;
111
kono
parents:
diff changeset
52
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
53 _g_ = runtime_g();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
54 if (runtime_usestackmaps) {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
55 // If stack map is enabled, we get here only when we can unwind
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
56 // the stack being scanned. That is, either we are scanning our
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
57 // own stack, or we are scanning through a signal handler.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
58 __go_assert((_g_ == gp) || ((_g_ == gp->m->gsignal) && (gp == gp->m->curg)));
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
59 return scanstackwithmap(gcw);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
60 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
61 if (_g_ == gp) {
111
kono
parents:
diff changeset
62 // Scanning our own stack.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
63 // If we are on a signal stack, it can unwind through the signal
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
64 // handler and see the g stack, so just scan our own stack.
111
kono
parents:
diff changeset
65 sp = __splitstack_find(nil, nil, &spsize, &next_segment,
kono
parents:
diff changeset
66 &next_sp, &initial_sp);
kono
parents:
diff changeset
67 } else {
kono
parents:
diff changeset
68 // Scanning another goroutine's stack.
kono
parents:
diff changeset
69 // The goroutine is usually asleep (the world is stopped).
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 // The exception is that if the goroutine is about to enter or might
kono
parents:
diff changeset
72 // have just exited a system call, it may be executing code such
kono
parents:
diff changeset
73 // as schedlock and may have needed to start a new stack segment.
kono
parents:
diff changeset
74 // Use the stack segment and stack pointer at the time of
kono
parents:
diff changeset
75 // the system call instead, since that won't change underfoot.
kono
parents:
diff changeset
76 if(gp->gcstack != 0) {
kono
parents:
diff changeset
77 sp = (void*)(gp->gcstack);
kono
parents:
diff changeset
78 spsize = gp->gcstacksize;
kono
parents:
diff changeset
79 next_segment = (void*)(gp->gcnextsegment);
kono
parents:
diff changeset
80 next_sp = (void*)(gp->gcnextsp);
kono
parents:
diff changeset
81 initial_sp = (void*)(gp->gcinitialsp);
kono
parents:
diff changeset
82 } else {
kono
parents:
diff changeset
83 sp = __splitstack_find_context((void**)(&gp->stackcontext[0]),
kono
parents:
diff changeset
84 &spsize, &next_segment,
kono
parents:
diff changeset
85 &next_sp, &initial_sp);
kono
parents:
diff changeset
86 }
kono
parents:
diff changeset
87 }
kono
parents:
diff changeset
88 if(sp != nil) {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
89 scanstackblock((uintptr)(sp), (uintptr)(spsize), gcw);
111
kono
parents:
diff changeset
90 while((sp = __splitstack_find(next_segment, next_sp,
kono
parents:
diff changeset
91 &spsize, &next_segment,
kono
parents:
diff changeset
92 &next_sp, &initial_sp)) != nil)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
93 scanstackblock((uintptr)(sp), (uintptr)(spsize), gcw);
111
kono
parents:
diff changeset
94 }
kono
parents:
diff changeset
95 #else
kono
parents:
diff changeset
96 byte* bottom;
kono
parents:
diff changeset
97 byte* top;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
98 byte* nextsp2;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
99 byte* initialsp2;
111
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 if(gp == runtime_g()) {
kono
parents:
diff changeset
102 // Scanning our own stack.
kono
parents:
diff changeset
103 bottom = (byte*)&gp;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
104 nextsp2 = secondary_stack_pointer();
111
kono
parents:
diff changeset
105 } else {
kono
parents:
diff changeset
106 // Scanning another goroutine's stack.
kono
parents:
diff changeset
107 // The goroutine is usually asleep (the world is stopped).
kono
parents:
diff changeset
108 bottom = (void*)gp->gcnextsp;
kono
parents:
diff changeset
109 if(bottom == nil)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
110 return true;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
111 nextsp2 = (void*)gp->gcnextsp2;
111
kono
parents:
diff changeset
112 }
kono
parents:
diff changeset
113 top = (byte*)(void*)(gp->gcinitialsp) + gp->gcstacksize;
kono
parents:
diff changeset
114 if(top > bottom)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
115 scanstackblock((uintptr)(bottom), (uintptr)(top - bottom), gcw);
111
kono
parents:
diff changeset
116 else
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
117 scanstackblock((uintptr)(top), (uintptr)(bottom - top), gcw);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
118 if (nextsp2 != nil) {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
119 initialsp2 = (byte*)(void*)(gp->gcinitialsp2);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
120 if(initialsp2 > nextsp2)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
121 scanstackblock((uintptr)(nextsp2), (uintptr)(initialsp2 - nextsp2), gcw);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
122 else
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
123 scanstackblock((uintptr)(initialsp2), (uintptr)(nextsp2 - initialsp2), gcw);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
124 }
111
kono
parents:
diff changeset
125 #endif
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
126 return true;
111
kono
parents:
diff changeset
127 }