1 // Copyright 2013 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.
6 #include "arch_GOARCH.h"
18 // StackDebug == 0: no logging
19 // == 1: logging of per-stack operations
20 // == 2: logging of per-frame operations
21 // == 3: logging of per-word updates
22 // == 4: logging of per-word reads
24 StackFromSystem = 0, // allocate stacks from system memory instead of the heap
25 StackFaultOnFree = 0, // old stacks are mapped noaccess to detect use after free
26 StackPoisonCopy = 0, // fill stack that should not be accessed with garbage, to detect bad dereferences during copy
31 // Global pool of spans that have free stacks.
32 // Stacks are assigned an order according to size.
33 // order = log_2(size/FixedStack)
34 // There is a free list for each order.
35 MSpan runtime·stackpool[NumStackOrders];
36 Mutex runtime·stackpoolmu;
37 // TODO: one lock per order?
39 static Stack stackfreequeue;
42 runtime·stackinit(void)
46 if((StackCacheSize & PageMask) != 0)
47 runtime·throw("cache size must be a multiple of page size");
49 for(i = 0; i < NumStackOrders; i++)
50 runtime·MSpanList_Init(&runtime·stackpool[i]);
53 // Allocates a stack from the free pool. Must be called with
56 poolalloc(uint8 order)
63 list = &runtime·stackpool[order];
66 // no free stacks. Allocate another span worth.
67 s = runtime·MHeap_AllocStack(&runtime·mheap, StackCacheSize >> PageShift);
69 runtime·throw("out of memory");
71 runtime·throw("bad ref");
72 if(s->freelist != nil)
73 runtime·throw("bad freelist");
74 for(i = 0; i < StackCacheSize; i += FixedStack << order) {
75 x = (MLink*)((s->start << PageShift) + i);
76 x->next = s->freelist;
79 runtime·MSpanList_Insert(list, s);
83 runtime·throw("span has no free stacks");
84 s->freelist = x->next;
86 if(s->freelist == nil) {
87 // all stacks in s are allocated.
88 runtime·MSpanList_Remove(s);
93 // Adds stack x to the free pool. Must be called with stackpoolmu held.
95 poolfree(MLink *x, uint8 order)
99 s = runtime·MHeap_Lookup(&runtime·mheap, x);
100 if(s->state != MSpanStack)
101 runtime·throw("freeing stack not in a stack span");
102 if(s->freelist == nil) {
103 // s will now have a free stack
104 runtime·MSpanList_Insert(&runtime·stackpool[order], s);
106 x->next = s->freelist;
110 // span is completely free - return to heap
111 runtime·MSpanList_Remove(s);
113 runtime·MHeap_FreeStack(&runtime·mheap, s);
117 // stackcacherefill/stackcacherelease implement a global pool of stack segments.
118 // The pool is required to prevent unlimited growth of per-thread caches.
120 stackcacherefill(MCache *c, uint8 order)
126 runtime·printf("stackcacherefill order=%d\n", order);
128 // Grab some stacks from the global cache.
129 // Grab half of the allowed capacity (to prevent thrashing).
132 runtime·lock(&runtime·stackpoolmu);
133 while(size < StackCacheSize/2) {
134 x = poolalloc(order);
137 size += FixedStack << order;
139 runtime·unlock(&runtime·stackpoolmu);
141 c->stackcache[order].list = list;
142 c->stackcache[order].size = size;
146 stackcacherelease(MCache *c, uint8 order)
152 runtime·printf("stackcacherelease order=%d\n", order);
153 x = c->stackcache[order].list;
154 size = c->stackcache[order].size;
155 runtime·lock(&runtime·stackpoolmu);
156 while(size > StackCacheSize/2) {
160 size -= FixedStack << order;
162 runtime·unlock(&runtime·stackpoolmu);
163 c->stackcache[order].list = x;
164 c->stackcache[order].size = size;
168 runtime·stackcache_clear(MCache *c)
174 runtime·printf("stackcache clear\n");
175 runtime·lock(&runtime·stackpoolmu);
176 for(order = 0; order < NumStackOrders; order++) {
177 x = c->stackcache[order].list;
183 c->stackcache[order].list = nil;
184 c->stackcache[order].size = 0;
186 runtime·unlock(&runtime·stackpoolmu);
190 runtime·stackalloc(uint32 n)
199 // Stackalloc must be called on scheduler stack, so that we
200 // never try to grow the stack during the code that stackalloc runs.
201 // Doing so would cause a deadlock (issue 1547).
203 runtime·throw("stackalloc not on scheduler stack");
205 runtime·throw("stack size not a power of 2");
207 runtime·printf("stackalloc %d\n", n);
209 if(runtime·debug.efence || StackFromSystem) {
210 v = runtime·sysAlloc(ROUND(n, PageSize), &mstats.stacks_sys);
212 runtime·throw("out of memory (stackalloc)");
213 return (Stack){(uintptr)v, (uintptr)v+n};
216 // Small stacks are allocated with a fixed-size free-list allocator.
217 // If we need a stack of a bigger size, we fall back on allocating
219 if(StackCache && n < FixedStack << NumStackOrders && n < StackCacheSize) {
222 while(n2 > FixedStack) {
227 if(c == nil || g->m->gcing || g->m->helpgc) {
228 // c == nil can happen in the guts of exitsyscall or
229 // procresize. Just get a stack from the global pool.
230 // Also don't touch stackcache during gc
231 // as it's flushed concurrently.
232 runtime·lock(&runtime·stackpoolmu);
233 x = poolalloc(order);
234 runtime·unlock(&runtime·stackpoolmu);
236 x = c->stackcache[order].list;
238 stackcacherefill(c, order);
239 x = c->stackcache[order].list;
241 c->stackcache[order].list = x->next;
242 c->stackcache[order].size -= n;
246 s = runtime·MHeap_AllocStack(&runtime·mheap, ROUND(n, PageSize) >> PageShift);
248 runtime·throw("out of memory");
249 v = (byte*)(s->start<<PageShift);
253 runtime·racemalloc(v, n);
255 runtime·printf(" allocated %p\n", v);
256 return (Stack){(uintptr)v, (uintptr)v+n};
260 runtime·stackfree(Stack stk)
272 runtime·throw("stack not a power of 2");
273 if(StackDebug >= 1) {
274 runtime·printf("stackfree %p %d\n", v, (int32)n);
275 runtime·memclr(v, n); // for testing, clobber stack data
277 if(runtime·debug.efence || StackFromSystem) {
278 if(runtime·debug.efence || StackFaultOnFree)
279 runtime·SysFault(v, n);
281 runtime·SysFree(v, n, &mstats.stacks_sys);
284 if(StackCache && n < FixedStack << NumStackOrders && n < StackCacheSize) {
287 while(n2 > FixedStack) {
293 if(c == nil || g->m->gcing || g->m->helpgc) {
294 runtime·lock(&runtime·stackpoolmu);
296 runtime·unlock(&runtime·stackpoolmu);
298 if(c->stackcache[order].size >= StackCacheSize)
299 stackcacherelease(c, order);
300 x->next = c->stackcache[order].list;
301 c->stackcache[order].list = x;
302 c->stackcache[order].size += n;
305 s = runtime·MHeap_Lookup(&runtime·mheap, v);
306 if(s->state != MSpanStack) {
307 runtime·printf("%p %p\n", s->start<<PageShift, v);
308 runtime·throw("bad span state");
310 runtime·MHeap_FreeStack(&runtime·mheap, s);
314 uintptr runtime·maxstacksize = 1<<20; // enough until runtime.main sets it for real
324 // Stack frame layout
327 // +------------------+
328 // | args from caller |
329 // +------------------+ <- frame->argp
330 // | return address |
331 // +------------------+ <- frame->varp
333 // +------------------+
334 // | args to callee |
335 // +------------------+ <- frame->sp
338 // +------------------+
339 // | args from caller |
340 // +------------------+ <- frame->argp
341 // | caller's retaddr |
342 // +------------------+ <- frame->varp
344 // +------------------+
345 // | args to callee |
346 // +------------------+
347 // | return address |
348 // +------------------+ <- frame->sp
350 void runtime·main(void);
351 void runtime·switchtoM(void(*)(void));
353 typedef struct AdjustInfo AdjustInfo;
356 uintptr delta; // ptr distance from old to new stack (newbase - oldbase)
359 // Adjustpointer checks whether *vpp is in the old stack described by adjinfo.
360 // If so, it rewrites *vpp to point into the new stack.
362 adjustpointer(AdjustInfo *adjinfo, void *vpp)
369 runtime·printf(" %p:%p\n", pp, p);
370 if(adjinfo->old.lo <= (uintptr)p && (uintptr)p < adjinfo->old.hi) {
371 *pp = p + adjinfo->delta;
373 runtime·printf(" adjust ptr %p: %p -> %p\n", pp, p, *pp);
377 // bv describes the memory starting at address scanp.
378 // Adjust any pointers contained therein.
380 adjustpointers(byte **scanp, BitVector *bv, AdjustInfo *adjinfo, Func *f)
384 byte *p, *minp, *maxp;
386 minp = (byte*)adjinfo->old.lo;
387 maxp = (byte*)adjinfo->old.hi;
388 delta = adjinfo->delta;
389 num = bv->n / BitsPerPointer;
390 for(i = 0; i < num; i++) {
392 runtime·printf(" %p:%s:%p\n", &scanp[i], mapnames[bv->bytedata[i / (8 / BitsPerPointer)] >> (i * BitsPerPointer & 7) & 3], scanp[i]);
393 switch(bv->bytedata[i / (8 / BitsPerPointer)] >> (i * BitsPerPointer & 7) & 3) {
395 if(runtime·debug.gcdead)
396 scanp[i] = (byte*)PoisonStack;
402 if(f != nil && (byte*)0 < p && (p < (byte*)PageSize && runtime·invalidptr || (uintptr)p == PoisonGC || (uintptr)p == PoisonStack)) {
403 // Looks like a junk value in a pointer slot.
404 // Live analysis wrong?
406 runtime·printf("runtime: bad pointer in frame %s at %p: %p\n", runtime·funcname(f), &scanp[i], p);
407 runtime·throw("invalid stack pointer");
409 if(minp <= p && p < maxp) {
411 runtime·printf("adjust ptr %p %s\n", p, runtime·funcname(f));
412 scanp[i] = p + delta;
416 runtime·throw("adjustpointers: unexpected garbage collection bits");
421 // Note: the argument/return area is adjusted by the callee.
423 adjustframe(Stkframe *frame, void *arg)
430 uintptr targetpc, size, minsize;
433 targetpc = frame->continpc;
440 runtime·printf(" adjusting %s frame=[%p,%p] pc=%p continpc=%p\n", runtime·funcname(f), frame->sp, frame->fp, frame->pc, frame->continpc);
441 if(f->entry == (uintptr)runtime·switchtoM) {
442 // A special routine at the bottom of stack of a goroutine that does an onM call.
443 // We will allow it to be copied even though we don't
444 // have full GC info for it (because it is written in asm).
447 if(targetpc != f->entry)
449 pcdata = runtime·pcdatavalue(f, PCDATA_StackMapIndex, targetpc);
451 pcdata = 0; // in prologue
453 // Adjust local variables if stack frame has been allocated.
454 size = frame->varp - frame->sp;
455 if(thechar != '6' && thechar != '8')
456 minsize = sizeof(uintptr);
460 stackmap = runtime·funcdata(f, FUNCDATA_LocalsPointerMaps);
461 if(stackmap == nil || stackmap->n <= 0) {
462 runtime·printf("runtime: frame %s untyped locals %p+%p\n", runtime·funcname(f), (byte*)(frame->varp-size), size);
463 runtime·throw("missing stackmap");
465 // Locals bitmap information, scan just the pointers in locals.
466 if(pcdata < 0 || pcdata >= stackmap->n) {
467 // don't know where we are
468 runtime·printf("runtime: pcdata is %d and %d locals stack map entries for %s (targetpc=%p)\n",
469 pcdata, stackmap->n, runtime·funcname(f), targetpc);
470 runtime·throw("bad symbol table");
472 bv = runtime·stackmapdata(stackmap, pcdata);
473 size = (bv.n * PtrSize) / BitsPerPointer;
475 runtime·printf(" locals\n");
476 adjustpointers((byte**)(frame->varp - size), &bv, adjinfo, f);
480 if(frame->arglen > 0) {
481 if(frame->argmap != nil) {
484 stackmap = runtime·funcdata(f, FUNCDATA_ArgsPointerMaps);
485 if(stackmap == nil || stackmap->n <= 0) {
486 runtime·printf("runtime: frame %s untyped args %p+%p\n", runtime·funcname(f), frame->argp, (uintptr)frame->arglen);
487 runtime·throw("missing stackmap");
489 if(pcdata < 0 || pcdata >= stackmap->n) {
490 // don't know where we are
491 runtime·printf("runtime: pcdata is %d and %d args stack map entries for %s (targetpc=%p)\n",
492 pcdata, stackmap->n, runtime·funcname(f), targetpc);
493 runtime·throw("bad symbol table");
495 bv = runtime·stackmapdata(stackmap, pcdata);
498 runtime·printf(" args\n");
499 adjustpointers((byte**)frame->argp, &bv, adjinfo, nil);
506 adjustctxt(G *gp, AdjustInfo *adjinfo)
508 adjustpointer(adjinfo, &gp->sched.ctxt);
512 adjustdefers(G *gp, AdjustInfo *adjinfo)
515 bool (*cb)(Stkframe*, void*);
517 // Adjust defer argument blocks the same way we adjust active stack frames.
519 runtime·tracebackdefers(gp, &cb, adjinfo);
521 // Adjust pointers in the Defer structs.
522 // Defer structs themselves are never on the stack.
523 for(d = gp->defer; d != nil; d = d->link) {
524 adjustpointer(adjinfo, &d->fn);
525 adjustpointer(adjinfo, &d->argp);
526 adjustpointer(adjinfo, &d->panic);
531 adjustpanics(G *gp, AdjustInfo *adjinfo)
533 // Panics are on stack and already adjusted.
534 // Update pointer to head of list in G.
535 adjustpointer(adjinfo, &gp->panic);
539 adjustsudogs(G *gp, AdjustInfo *adjinfo)
543 // the data elements pointed to by a SudoG structure
544 // might be in the stack.
545 for(s = gp->waiting; s != nil; s = s->waitlink) {
546 adjustpointer(adjinfo, &s->elem);
547 adjustpointer(adjinfo, &s->selectdone);
551 // Copies gp's stack to a new stack of a different size.
552 // Caller must have changed gp status to Gcopystack.
554 copystack(G *gp, uintptr newsize)
559 bool (*cb)(Stkframe*, void*);
562 if(gp->syscallsp != 0)
563 runtime·throw("stack growth not allowed in system call");
566 runtime·throw("nil stackbase");
567 used = old.hi - gp->sched.sp;
569 // allocate new stack
570 new = runtime·stackalloc(newsize);
571 if(StackPoisonCopy) {
579 runtime·printf("copystack gp=%p [%p %p %p]/%d -> [%p %p %p]/%d\n", gp, old.lo, old.hi-used, old.hi, (int32)(old.hi-old.lo), new.lo, new.hi-used, new.hi, (int32)newsize);
581 // adjust pointers in the to-be-copied frames
583 adjinfo.delta = new.hi - old.hi;
585 runtime·gentraceback(~(uintptr)0, ~(uintptr)0, 0, gp, 0, nil, 0x7fffffff, &cb, &adjinfo, 0);
587 // adjust other miscellaneous things that have pointers into stacks.
588 adjustctxt(gp, &adjinfo);
589 adjustdefers(gp, &adjinfo);
590 adjustpanics(gp, &adjinfo);
591 adjustsudogs(gp, &adjinfo);
593 // copy the stack to the new location
594 if(StackPoisonCopy) {
600 runtime·memmove((byte*)new.hi - used, (byte*)old.hi - used, used);
602 // Swap out old stack for new one
604 gp->stackguard0 = new.lo + StackGuard; // NOTE: might clobber a preempt request
605 gp->sched.sp = new.hi - used;
608 if(StackPoisonCopy) {
614 if(newsize > old.hi-old.lo) {
615 // growing, free stack immediately
616 runtime·stackfree(old);
618 // shrinking, queue up free operation. We can't actually free the stack
619 // just yet because we might run into the following situation:
620 // 1) GC starts, scans a SudoG but does not yet mark the SudoG.elem pointer
621 // 2) The stack that pointer points to is shrunk
622 // 3) The old stack is freed
623 // 4) The containing span is marked free
624 // 5) GC attempts to mark the SudoG.elem pointer. The marking fails because
625 // the pointer looks like a pointer into a free span.
626 // By not freeing, we prevent step #4 until GC is done.
627 runtime·lock(&runtime·stackpoolmu);
628 *(Stack*)old.lo = stackfreequeue;
629 stackfreequeue = old;
630 runtime·unlock(&runtime·stackpoolmu);
634 // round x up to a power of 2.
636 runtime·round2(int32 x)
646 // Called from runtime·morestack when more stack is needed.
647 // Allocate larger stack and relocate to new stack.
648 // Stack growth is multiplicative, for constant amortized cost.
650 // g->atomicstatus will be Grunning or Gscanrunning upon entry.
651 // If the GC is trying to stop this g then it will set preemptscan to true.
653 runtime·newstack(void)
655 int32 oldsize, newsize;
661 if(g->m->morebuf.g->stackguard0 == (uintptr)StackFork)
662 runtime·throw("stack growth after fork");
663 if(g->m->morebuf.g != g->m->curg) {
664 runtime·printf("runtime: newstack called from g=%p\n"
665 "\tm=%p m->curg=%p m->g0=%p m->gsignal=%p\n",
666 g->m->morebuf.g, g->m, g->m->curg, g->m->g0, g->m->gsignal);
667 morebuf = g->m->morebuf;
668 runtime·traceback(morebuf.pc, morebuf.sp, morebuf.lr, morebuf.g);
669 runtime·throw("runtime: wrong goroutine in newstack");
671 if(g->m->curg->throwsplit)
672 runtime·throw("runtime: stack split at bad time");
674 // The goroutine must be executing in order to call newstack,
675 // so it must be Grunning or Gscanrunning.
678 morebuf = g->m->morebuf;
679 g->m->morebuf.pc = (uintptr)nil;
680 g->m->morebuf.lr = (uintptr)nil;
681 g->m->morebuf.sp = (uintptr)nil;
682 g->m->morebuf.g = (G*)nil;
684 runtime·casgstatus(gp, Grunning, Gwaiting);
685 gp->waitreason = runtime·gostringnocopy((byte*)"stack growth");
687 runtime·rewindmorestack(&gp->sched);
689 if(gp->stack.lo == 0)
690 runtime·throw("missing stack in newstack");
692 if(thechar == '6' || thechar == '8') {
693 // The call to morestack cost a word.
694 sp -= sizeof(uintreg);
696 if(StackDebug >= 1 || sp < gp->stack.lo) {
697 runtime·printf("runtime: newstack sp=%p stack=[%p, %p]\n"
698 "\tmorebuf={pc:%p sp:%p lr:%p}\n"
699 "\tsched={pc:%p sp:%p lr:%p ctxt:%p}\n",
700 sp, gp->stack.lo, gp->stack.hi,
701 g->m->morebuf.pc, g->m->morebuf.sp, g->m->morebuf.lr,
702 gp->sched.pc, gp->sched.sp, gp->sched.lr, gp->sched.ctxt);
704 if(sp < gp->stack.lo) {
705 runtime·printf("runtime: gp=%p, gp->status=%d\n ", (void*)gp, runtime·readgstatus(gp));
706 runtime·printf("runtime: split stack overflow: %p < %p\n", sp, gp->stack.lo);
707 runtime·throw("runtime: split stack overflow");
710 if(gp->sched.ctxt != nil) {
711 // morestack wrote sched.ctxt on its way in here,
712 // without a write barrier. Run the write barrier now.
713 // It is not possible to be preempted between then
714 // and now, so it's okay.
715 runtime·writebarrierptr_nostore(&gp->sched.ctxt, gp->sched.ctxt);
718 if(gp->stackguard0 == (uintptr)StackPreempt) {
720 runtime·throw("runtime: preempt g0");
721 if(g->m->p == nil && g->m->locks == 0)
722 runtime·throw("runtime: g is running but p is not");
723 if(gp->preemptscan) {
724 runtime·gcphasework(gp);
725 runtime·casgstatus(gp, Gwaiting, Grunning);
726 gp->stackguard0 = gp->stack.lo + StackGuard;
728 gp->preemptscan = false; // Tells the GC premption was successful.
729 runtime·gogo(&gp->sched); // never return
732 // Be conservative about where we preempt.
733 // We are interested in preempting user Go code, not runtime code.
734 if(g->m->locks || g->m->mallocing || g->m->gcing || g->m->p->status != Prunning) {
735 // Let the goroutine keep running for now.
736 // gp->preempt is set, so it will be preempted next time.
737 gp->stackguard0 = gp->stack.lo + StackGuard;
738 runtime·casgstatus(gp, Gwaiting, Grunning);
739 runtime·gogo(&gp->sched); // never return
741 // Act like goroutine called runtime.Gosched.
742 runtime·casgstatus(gp, Gwaiting, Grunning);
743 runtime·gosched_m(gp); // never return
746 // Allocate a bigger segment and move the stack.
747 oldsize = gp->stack.hi - gp->stack.lo;
748 newsize = oldsize * 2;
749 if(newsize > runtime·maxstacksize) {
750 runtime·printf("runtime: goroutine stack exceeds %D-byte limit\n", (uint64)runtime·maxstacksize);
751 runtime·throw("stack overflow");
754 oldstatus = runtime·readgstatus(gp);
756 runtime·casgstatus(gp, oldstatus, Gcopystack); // oldstatus is Gwaiting or Grunnable
757 // The concurrent GC will not scan the stack while we are doing the copy since
758 // the gp is in a Gcopystack status.
759 copystack(gp, newsize);
761 runtime·printf("stack grow done\n");
762 runtime·casgstatus(gp, Gcopystack, Grunning);
763 runtime·gogo(&gp->sched);
766 #pragma textflag NOSPLIT
768 runtime·nilfunc(void)
773 // adjust Gobuf as if it executed a call to fn
774 // and then did an immediate gosave.
776 runtime·gostartcallfn(Gobuf *gobuf, FuncVal *fv)
783 fn = runtime·nilfunc;
784 runtime·gostartcall(gobuf, fn, fv);
787 // Maybe shrink the stack being used by gp.
788 // Called at garbage collection time.
790 runtime·shrinkstack(G *gp)
792 uintptr used, oldsize, newsize;
795 if(runtime·readgstatus(gp) == Gdead) {
796 if(gp->stack.lo != 0) {
797 // Free whole stack - it will get reallocated
798 // if G is used again.
799 runtime·stackfree(gp->stack);
805 if(gp->stack.lo == 0)
806 runtime·throw("missing stack in shrinkstack");
808 oldsize = gp->stack.hi - gp->stack.lo;
809 newsize = oldsize / 2;
810 if(newsize < FixedStack)
811 return; // don't shrink below the minimum-sized stack
812 used = gp->stack.hi - gp->sched.sp;
813 if(used >= oldsize / 4)
814 return; // still using at least 1/4 of the segment.
816 // We can't copy the stack if we're in a syscall.
817 // The syscall might have pointers into the stack.
818 if(gp->syscallsp != 0)
822 if(gp->m != nil && gp->m->libcallsp != 0)
826 runtime·printf("shrinking stack %D->%D\n", (uint64)oldsize, (uint64)newsize);
827 // This is being done in a Gscan state and was initiated by the GC so no need to move to
829 // The world is stopped, so the goroutine must be Gwaiting or Grunnable,
830 // and what it is is not changing underfoot.
832 oldstatus = runtime·readgstatus(gp);
834 if(oldstatus != Gwaiting && oldstatus != Grunnable)
835 runtime·throw("status is not Gwaiting or Grunnable");
836 runtime·casgstatus(gp, oldstatus, Gcopystack);
837 copystack(gp, newsize);
838 runtime·casgstatus(gp, Gcopystack, oldstatus);
841 // Do any delayed stack freeing that was queued up during GC.
843 runtime·shrinkfinish(void)
847 runtime·lock(&runtime·stackpoolmu);
849 stackfreequeue = (Stack){0,0};
850 runtime·unlock(&runtime·stackpoolmu);
853 runtime·stackfree(s);
858 static void badc(void);
860 #pragma textflag NOSPLIT
862 runtime·morestackc(void)
873 runtime·throw("attempt to execute C code on Go stack");