stack.go 34.5 KB
Newer Older
1 2 3 4 5 6
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package runtime

7 8
import (
	"runtime/internal/atomic"
9
	"runtime/internal/sys"
10 11
	"unsafe"
)
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
Stack layout parameters.
Included both by runtime (compiled via 6c) and linkers (compiled via gcc).

The per-goroutine g->stackguard is set to point StackGuard bytes
above the bottom of the stack.  Each function compares its stack
pointer against g->stackguard to check for overflow.  To cut one
instruction from the check sequence for functions with tiny frames,
the stack is allowed to protrude StackSmall bytes below the stack
guard.  Functions with large frames don't bother with the check and
always call morestack.  The sequences are (for amd64, others are
similar):

	guard = g->stackguard
	frame = function's stack frame size
	argsize = size of function arguments (call + return)

	stack frame size <= StackSmall:
		CMPQ guard, SP
		JHI 3(PC)
		MOVQ m->morearg, $(argsize << 32)
		CALL morestack(SB)

	stack frame size > StackSmall but < StackBig
		LEAQ (frame-StackSmall)(SP), R0
		CMPQ guard, R0
		JHI 3(PC)
		MOVQ m->morearg, $(argsize << 32)
		CALL morestack(SB)

	stack frame size >= StackBig:
		MOVQ m->morearg, $((argsize << 32) | frame)
		CALL morestack(SB)

The bottom StackGuard - StackSmall bytes are important: there has
to be enough room to execute functions that refuse to check for
stack overflow, either because they need to be adjacent to the
actual caller's frame (deferproc) or because they handle the imminent
stack overflow (morestack).

For example, deferproc might call malloc, which does one of the
above checks (without allocating a full frame), which might trigger
a call to morestack.  This sequence needs to fit in the bottom
section of the stack.  On amd64, morestack's frame is 40 bytes, and
deferproc's frame is 56 bytes.  That fits well within the
StackGuard - StackSmall bytes at the bottom.
The linkers explore all possible call traces involving non-splitting
functions to make sure that this limit cannot be violated.
*/

const (
	// StackSystem is a number of additional bytes to add
	// to each stack below the usual guard area for OS-specific
	// purposes like signal handling. Used on Windows, Plan 9,
	// and Darwin/ARM because they do not use a separate stack.
68
	_StackSystem = sys.GoosWindows*512*sys.PtrSize + sys.GoosPlan9*512 + sys.GoosDarwin*sys.GoarchArm*1024
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

	// The minimum size of stack used by Go code
	_StackMin = 2048

	// The minimum stack size to allocate.
	// The hackery here rounds FixedStack0 up to a power of 2.
	_FixedStack0 = _StackMin + _StackSystem
	_FixedStack1 = _FixedStack0 - 1
	_FixedStack2 = _FixedStack1 | (_FixedStack1 >> 1)
	_FixedStack3 = _FixedStack2 | (_FixedStack2 >> 2)
	_FixedStack4 = _FixedStack3 | (_FixedStack3 >> 4)
	_FixedStack5 = _FixedStack4 | (_FixedStack4 >> 8)
	_FixedStack6 = _FixedStack5 | (_FixedStack5 >> 16)
	_FixedStack  = _FixedStack6 + 1

	// Functions that need frames bigger than this use an extra
	// instruction to do the stack split check, to avoid overflow
	// in case SP - framesize wraps below zero.
	// This value can be no bigger than the size of the unmapped
	// space at zero.
	_StackBig = 4096

	// The stack guard is a pointer this many bytes above the
	// bottom of the stack.
93
	_StackGuard = 880*sys.StackGuardMultiplier + _StackSystem
94 95

	// After a stack split check the SP is allowed to be this
96
	// many bytes below the stack guard. This saves an instruction
97 98 99 100 101 102 103 104
	// in the checking sequence for tiny frames.
	_StackSmall = 128

	// The maximum number of bytes that a chain of NOSPLIT
	// functions can use.
	_StackLimit = _StackGuard - _StackSystem - _StackSmall
)

105
const (
106
	// stackDebug == 0: no logging
107 108 109 110 111 112 113 114
	//            == 1: logging of per-stack operations
	//            == 2: logging of per-frame operations
	//            == 3: logging of per-word updates
	//            == 4: logging of per-word reads
	stackDebug       = 0
	stackFromSystem  = 0 // allocate stacks from system memory instead of the heap
	stackFaultOnFree = 0 // old stacks are mapped noaccess to detect use after free
	stackPoisonCopy  = 0 // fill stack that should not be accessed with garbage, to detect bad dereferences during copy
115
	stackNoCache     = 0 // disable per-P small stack caches
116 117 118

	// check the BP links during traceback.
	debugCheckBP = false
119 120 121
)

const (
122
	uintptrMask = 1<<(8*sys.PtrSize) - 1
123 124

	// Goroutine preemption request.
125
	// Stored into g->stackguard0 to cause split stack check failure.
126 127 128 129 130
	// Must be greater than any real sp.
	// 0xfffffade in hex.
	stackPreempt = uintptrMask & -1314

	// Thread is forking.
131
	// Stored into g->stackguard0 to cause split stack check failure.
132 133 134 135 136 137 138 139 140
	// Must be greater than any real sp.
	stackFork = uintptrMask & -1234
)

// Global pool of spans that have free stacks.
// Stacks are assigned an order according to size.
//     order = log_2(size/FixedStack)
// There is a free list for each order.
// TODO: one lock per order?
141
var stackpool [_NumStackOrders]mSpanList
142 143
var stackpoolmu mutex

144 145 146
// Global pool of large stack spans.
var stackLarge struct {
	lock mutex
147
	free [heapAddrBits - pageShift]mSpanList // free lists by log_2(s.npages)
148
}
149 150 151

func stackinit() {
	if _StackCacheSize&_PageMask != 0 {
152
		throw("cache size must be a multiple of page size")
153 154
	}
	for i := range stackpool {
155
		stackpool[i].init()
156
	}
157 158 159 160 161 162 163 164 165 166 167 168 169
	for i := range stackLarge.free {
		stackLarge.free[i].init()
	}
}

// stacklog2 returns ⌊log_2(n)⌋.
func stacklog2(n uintptr) int {
	log2 := 0
	for n > 1 {
		n >>= 1
		log2++
	}
	return log2
170 171
}

172
// Allocates a stack from the free pool. Must be called with
173
// stackpoolmu held.
174
func stackpoolalloc(order uint8) gclinkptr {
175
	list := &stackpool[order]
176 177
	s := list.first
	if s == nil {
178
		// no free stacks. Allocate another span worth.
179
		s = mheap_.allocManual(_StackCacheSize>>_PageShift, &memstats.stacks_inuse)
180
		if s == nil {
181
			throw("out of memory")
182
		}
183 184
		if s.allocCount != 0 {
			throw("bad allocCount")
185
		}
186 187
		if s.manualFreeList.ptr() != nil {
			throw("bad manualFreeList")
188
		}
189 190
		s.elemsize = _FixedStack << order
		for i := uintptr(0); i < _StackCacheSize; i += s.elemsize {
191
			x := gclinkptr(s.base() + i)
192 193
			x.ptr().next = s.manualFreeList
			s.manualFreeList = x
194
		}
195
		list.insert(s)
196
	}
197
	x := s.manualFreeList
198
	if x.ptr() == nil {
199
		throw("span has no free stacks")
200
	}
201
	s.manualFreeList = x.ptr().next
202
	s.allocCount++
203
	if s.manualFreeList.ptr() == nil {
204
		// all stacks in s are allocated.
205
		list.remove(s)
206 207 208 209
	}
	return x
}

210
// Adds stack x to the free pool. Must be called with stackpoolmu held.
211
func stackpoolfree(x gclinkptr, order uint8) {
212
	s := spanOfUnchecked(uintptr(x))
213
	if s.state != _MSpanManual {
214
		throw("freeing stack not in a stack span")
215
	}
216
	if s.manualFreeList.ptr() == nil {
217
		// s will now have a free stack
218
		stackpool[order].insert(s)
219
	}
220 221
	x.ptr().next = s.manualFreeList
	s.manualFreeList = x
222 223
	s.allocCount--
	if gcphase == _GCoff && s.allocCount == 0 {
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
		// Span is completely free. Return it to the heap
		// immediately if we're sweeping.
		//
		// If GC is active, we delay the free until the end of
		// GC to avoid the following type of situation:
		//
		// 1) GC starts, scans a SudoG but does not yet mark the SudoG.elem pointer
		// 2) The stack that pointer points to is copied
		// 3) The old stack is freed
		// 4) The containing span is marked free
		// 5) GC attempts to mark the SudoG.elem pointer. The
		//    marking fails because the pointer looks like a
		//    pointer into a free span.
		//
		// By not freeing, we prevent step #4 until GC is done.
239
		stackpool[order].remove(s)
240
		s.manualFreeList = 0
241
		mheap_.freeManual(s, &memstats.stacks_inuse)
242 243 244 245 246
	}
}

// stackcacherefill/stackcacherelease implement a global pool of stack segments.
// The pool is required to prevent unlimited growth of per-thread caches.
247 248
//
//go:systemstack
249 250 251 252 253 254 255
func stackcacherefill(c *mcache, order uint8) {
	if stackDebug >= 1 {
		print("stackcacherefill order=", order, "\n")
	}

	// Grab some stacks from the global cache.
	// Grab half of the allowed capacity (to prevent thrashing).
256
	var list gclinkptr
257 258 259 260
	var size uintptr
	lock(&stackpoolmu)
	for size < _StackCacheSize/2 {
		x := stackpoolalloc(order)
261
		x.ptr().next = list
262 263 264 265 266 267 268 269
		list = x
		size += _FixedStack << order
	}
	unlock(&stackpoolmu)
	c.stackcache[order].list = list
	c.stackcache[order].size = size
}

270
//go:systemstack
271 272 273 274 275 276 277 278
func stackcacherelease(c *mcache, order uint8) {
	if stackDebug >= 1 {
		print("stackcacherelease order=", order, "\n")
	}
	x := c.stackcache[order].list
	size := c.stackcache[order].size
	lock(&stackpoolmu)
	for size > _StackCacheSize/2 {
279
		y := x.ptr().next
280 281 282 283 284 285 286 287 288
		stackpoolfree(x, order)
		x = y
		size -= _FixedStack << order
	}
	unlock(&stackpoolmu)
	c.stackcache[order].list = x
	c.stackcache[order].size = size
}

289
//go:systemstack
290 291 292 293 294 295 296
func stackcache_clear(c *mcache) {
	if stackDebug >= 1 {
		print("stackcache clear\n")
	}
	lock(&stackpoolmu)
	for order := uint8(0); order < _NumStackOrders; order++ {
		x := c.stackcache[order].list
297 298
		for x.ptr() != nil {
			y := x.ptr().next
299 300 301
			stackpoolfree(x, order)
			x = y
		}
302
		c.stackcache[order].list = 0
303 304 305 306 307
		c.stackcache[order].size = 0
	}
	unlock(&stackpoolmu)
}

308 309 310 311 312 313
// stackalloc allocates an n byte stack.
//
// stackalloc must run on the system stack because it uses per-P
// resources and must not split the stack.
//
//go:systemstack
314
func stackalloc(n uint32) stack {
315 316 317 318 319
	// Stackalloc must be called on scheduler stack, so that we
	// never try to grow the stack during the code that stackalloc runs.
	// Doing so would cause a deadlock (issue 1547).
	thisg := getg()
	if thisg != thisg.m.g0 {
320
		throw("stackalloc not on scheduler stack")
321 322
	}
	if n&(n-1) != 0 {
323
		throw("stack size not a power of 2")
324 325 326 327 328 329
	}
	if stackDebug >= 1 {
		print("stackalloc ", n, "\n")
	}

	if debug.efence != 0 || stackFromSystem != 0 {
330 331
		n = uint32(round(uintptr(n), physPageSize))
		v := sysAlloc(uintptr(n), &memstats.stacks_sys)
332
		if v == nil {
333
			throw("out of memory (stackalloc)")
334
		}
335
		return stack{uintptr(v), uintptr(v) + uintptr(n)}
336 337 338 339 340 341
	}

	// Small stacks are allocated with a fixed-size free-list allocator.
	// If we need a stack of a bigger size, we fall back on allocating
	// a dedicated span.
	var v unsafe.Pointer
342
	if n < _FixedStack<<_NumStackOrders && n < _StackCacheSize {
343 344 345 346 347 348
		order := uint8(0)
		n2 := n
		for n2 > _FixedStack {
			order++
			n2 >>= 1
		}
349
		var x gclinkptr
350
		c := thisg.m.mcache
351
		if stackNoCache != 0 || c == nil || thisg.m.preemptoff != "" || thisg.m.helpgc != 0 {
352 353 354 355 356 357 358 359 360
			// c == nil can happen in the guts of exitsyscall or
			// procresize. Just get a stack from the global pool.
			// Also don't touch stackcache during gc
			// as it's flushed concurrently.
			lock(&stackpoolmu)
			x = stackpoolalloc(order)
			unlock(&stackpoolmu)
		} else {
			x = c.stackcache[order].list
361
			if x.ptr() == nil {
362 363 364
				stackcacherefill(c, order)
				x = c.stackcache[order].list
			}
365
			c.stackcache[order].list = x.ptr().next
366 367
			c.stackcache[order].size -= uintptr(n)
		}
368
		v = unsafe.Pointer(x)
369
	} else {
370 371 372 373 374 375 376 377 378 379 380 381
		var s *mspan
		npage := uintptr(n) >> _PageShift
		log2npage := stacklog2(npage)

		// Try to get a stack from the large stack cache.
		lock(&stackLarge.lock)
		if !stackLarge.free[log2npage].isEmpty() {
			s = stackLarge.free[log2npage].first
			stackLarge.free[log2npage].remove(s)
		}
		unlock(&stackLarge.lock)

382
		if s == nil {
383
			// Allocate a new stack from the heap.
384
			s = mheap_.allocManual(npage, &memstats.stacks_inuse)
385 386 387
			if s == nil {
				throw("out of memory")
			}
388
			s.elemsize = uintptr(n)
389
		}
390
		v = unsafe.Pointer(s.base())
391 392 393 394 395
	}

	if raceenabled {
		racemalloc(v, uintptr(n))
	}
396 397 398
	if msanenabled {
		msanmalloc(v, uintptr(n))
	}
399 400 401
	if stackDebug >= 1 {
		print("  allocated ", v, "\n")
	}
402
	return stack{uintptr(v), uintptr(v) + uintptr(n)}
403 404
}

405 406 407 408 409 410
// stackfree frees an n byte stack allocation at stk.
//
// stackfree must run on the system stack because it uses per-P
// resources and must not split the stack.
//
//go:systemstack
411
func stackfree(stk stack) {
412
	gp := getg()
413
	v := unsafe.Pointer(stk.lo)
414
	n := stk.hi - stk.lo
415
	if n&(n-1) != 0 {
416
		throw("stack not a power of 2")
417
	}
418 419 420
	if stk.lo+n < stk.hi {
		throw("bad stack size")
	}
421 422
	if stackDebug >= 1 {
		println("stackfree", v, n)
423
		memclrNoHeapPointers(v, n) // for testing, clobber stack data
424 425 426 427 428 429 430 431 432
	}
	if debug.efence != 0 || stackFromSystem != 0 {
		if debug.efence != 0 || stackFaultOnFree != 0 {
			sysFault(v, n)
		} else {
			sysFree(v, n, &memstats.stacks_sys)
		}
		return
	}
433 434 435
	if msanenabled {
		msanfree(v, n)
	}
436
	if n < _FixedStack<<_NumStackOrders && n < _StackCacheSize {
437 438 439 440 441 442
		order := uint8(0)
		n2 := n
		for n2 > _FixedStack {
			order++
			n2 >>= 1
		}
443
		x := gclinkptr(v)
444
		c := gp.m.mcache
445
		if stackNoCache != 0 || c == nil || gp.m.preemptoff != "" || gp.m.helpgc != 0 {
446 447 448 449 450 451 452
			lock(&stackpoolmu)
			stackpoolfree(x, order)
			unlock(&stackpoolmu)
		} else {
			if c.stackcache[order].size >= _StackCacheSize {
				stackcacherelease(c, order)
			}
453
			x.ptr().next = c.stackcache[order].list
454 455 456 457
			c.stackcache[order].list = x
			c.stackcache[order].size += n
		}
	} else {
458
		s := spanOfUnchecked(uintptr(v))
459
		if s.state != _MSpanManual {
460
			println(hex(s.base()), v)
461
			throw("bad span state")
462
		}
463 464 465
		if gcphase == _GCoff {
			// Free the stack immediately if we're
			// sweeping.
466
			mheap_.freeManual(s, &memstats.stacks_inuse)
467
		} else {
468 469 470 471 472 473 474 475 476
			// If the GC is running, we can't return a
			// stack span to the heap because it could be
			// reused as a heap span, and this state
			// change would race with GC. Add it to the
			// large stack cache instead.
			log2npage := stacklog2(s.npages)
			lock(&stackLarge.lock)
			stackLarge.free[log2npage].insert(s)
			unlock(&stackLarge.lock)
477
		}
478 479 480 481 482
	}
}

var maxstacksize uintptr = 1 << 20 // enough until runtime.main sets it for real

483 484 485
var ptrnames = []string{
	0: "scalar",
	1: "ptr",
486 487 488 489 490 491 492 493 494
}

// Stack frame layout
//
// (x86)
// +------------------+
// | args from caller |
// +------------------+ <- frame->argp
// |  return address  |
495 496
// +------------------+
// |  caller's BP (*) | (*) if framepointer_enabled && varp < sp
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
// +------------------+ <- frame->varp
// |     locals       |
// +------------------+
// |  args to callee  |
// +------------------+ <- frame->sp
//
// (arm)
// +------------------+
// | args from caller |
// +------------------+ <- frame->argp
// | caller's retaddr |
// +------------------+ <- frame->varp
// |     locals       |
// +------------------+
// |  args to callee  |
// +------------------+
// |  return address  |
// +------------------+ <- frame->sp

type adjustinfo struct {
	old   stack
	delta uintptr // ptr distance from old to new stack (newbase - oldbase)
519
	cache pcvalueCache
520 521 522

	// sghi is the highest sudog.elem on the stack.
	sghi uintptr
523 524 525 526 527
}

// Adjustpointer checks whether *vpp is in the old stack described by adjinfo.
// If so, it rewrites *vpp to point into the new stack.
func adjustpointer(adjinfo *adjustinfo, vpp unsafe.Pointer) {
528
	pp := (*uintptr)(vpp)
529 530
	p := *pp
	if stackDebug >= 4 {
531
		print("        ", pp, ":", hex(p), "\n")
532
	}
533 534
	if adjinfo.old.lo <= p && p < adjinfo.old.hi {
		*pp = p + adjinfo.delta
535
		if stackDebug >= 3 {
536
			print("        adjust ptr ", pp, ":", hex(p), " -> ", hex(*pp), "\n")
537 538 539 540
		}
	}
}

541 542 543 544 545 546
// Information from the compiler about the layout of stack frames.
type bitvector struct {
	n        int32 // # of bits
	bytedata *uint8
}

547 548 549 550 551 552 553 554 555 556 557 558
type gobitvector struct {
	n        uintptr
	bytedata []uint8
}

func gobv(bv bitvector) gobitvector {
	return gobitvector{
		uintptr(bv.n),
		(*[1 << 30]byte)(unsafe.Pointer(bv.bytedata))[:(bv.n+7)/8],
	}
}

559 560
func ptrbit(bv *gobitvector, i uintptr) uint8 {
	return (bv.bytedata[i/8] >> (i % 8)) & 1
561 562 563 564
}

// bv describes the memory starting at address scanp.
// Adjust any pointers contained therein.
565
func adjustpointers(scanp unsafe.Pointer, cbv *bitvector, adjinfo *adjustinfo, f funcInfo) {
566 567 568 569
	bv := gobv(*cbv)
	minp := adjinfo.old.lo
	maxp := adjinfo.old.hi
	delta := adjinfo.delta
570
	num := bv.n
571 572 573 574 575 576
	// If this frame might contain channel receive slots, use CAS
	// to adjust pointers. If the slot hasn't been received into
	// yet, it may contain stack pointers and a concurrent send
	// could race with adjusting those pointers. (The sent value
	// itself can never contain stack pointers.)
	useCAS := uintptr(scanp) < adjinfo.sghi
577 578
	for i := uintptr(0); i < num; i++ {
		if stackDebug >= 4 {
579
			print("        ", add(scanp, i*sys.PtrSize), ":", ptrnames[ptrbit(&bv, i)], ":", hex(*(*uintptr)(add(scanp, i*sys.PtrSize))), " # ", i, " ", bv.bytedata[i/8], "\n")
580
		}
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
		if ptrbit(&bv, i) != 1 {
			continue
		}
		pp := (*uintptr)(add(scanp, i*sys.PtrSize))
	retry:
		p := *pp
		if f.valid() && 0 < p && p < minLegalPointer && debug.invalidptr != 0 {
			// Looks like a junk value in a pointer slot.
			// Live analysis wrong?
			getg().m.traceback = 2
			print("runtime: bad pointer in frame ", funcname(f), " at ", pp, ": ", hex(p), "\n")
			throw("invalid pointer found on stack")
		}
		if minp <= p && p < maxp {
			if stackDebug >= 3 {
				print("adjust ptr ", hex(p), " ", funcname(f), "\n")
597
			}
598 599 600 601
			if useCAS {
				ppu := (*unsafe.Pointer)(unsafe.Pointer(pp))
				if !atomic.Casp1(ppu, unsafe.Pointer(p), unsafe.Pointer(p+delta)) {
					goto retry
602
				}
603 604
			} else {
				*pp = p + delta
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
			}
		}
	}
}

// Note: the argument/return area is adjusted by the callee.
func adjustframe(frame *stkframe, arg unsafe.Pointer) bool {
	adjinfo := (*adjustinfo)(arg)
	targetpc := frame.continpc
	if targetpc == 0 {
		// Frame is dead.
		return true
	}
	f := frame.fn
	if stackDebug >= 2 {
		print("    adjusting ", funcname(f), " frame=[", hex(frame.sp), ",", hex(frame.fp), "] pc=", hex(frame.pc), " continpc=", hex(frame.continpc), "\n")
	}
622
	if f.funcID == funcID_systemstack_switch {
623
		// A special routine at the bottom of stack of a goroutine that does an systemstack call.
624 625 626 627 628 629 630
		// We will allow it to be copied even though we don't
		// have full GC info for it (because it is written in asm).
		return true
	}
	if targetpc != f.entry {
		targetpc--
	}
631
	pcdata := pcdatavalue(f, _PCDATA_StackMapIndex, targetpc, &adjinfo.cache)
632 633 634 635 636 637 638
	if pcdata == -1 {
		pcdata = 0 // in prologue
	}

	// Adjust local variables if stack frame has been allocated.
	size := frame.varp - frame.sp
	var minsize uintptr
639 640
	switch sys.ArchFamily {
	case sys.ARM64:
641
		minsize = sys.SpAlign
642
	default:
643
		minsize = sys.MinFrameSize
644 645 646 647 648 649
	}
	if size > minsize {
		var bv bitvector
		stackmap := (*stackmap)(funcdata(f, _FUNCDATA_LocalsPointerMaps))
		if stackmap == nil || stackmap.n <= 0 {
			print("runtime: frame ", funcname(f), " untyped locals ", hex(frame.varp-size), "+", hex(size), "\n")
650
			throw("missing stackmap")
651 652 653 654 655
		}
		// Locals bitmap information, scan just the pointers in locals.
		if pcdata < 0 || pcdata >= stackmap.n {
			// don't know where we are
			print("runtime: pcdata is ", pcdata, " and ", stackmap.n, " locals stack map entries for ", funcname(f), " (targetpc=", targetpc, ")\n")
656
			throw("bad symbol table")
657 658
		}
		bv = stackmapdata(stackmap, pcdata)
659
		size = uintptr(bv.n) * sys.PtrSize
660
		if stackDebug >= 3 {
661
			print("      locals ", pcdata, "/", stackmap.n, " ", size/sys.PtrSize, " words ", bv.bytedata, "\n")
662 663 664 665
		}
		adjustpointers(unsafe.Pointer(frame.varp-size), &bv, adjinfo, f)
	}

666
	// Adjust saved base pointer if there is one.
667
	if sys.ArchFamily == sys.AMD64 && frame.argp-frame.varp == 2*sys.RegSize {
668
		if !framepointer_enabled {
669
			print("runtime: found space for saved base pointer, but no framepointer experiment\n")
670
			print("argp=", hex(frame.argp), " varp=", hex(frame.varp), "\n")
671 672 673 674 675
			throw("bad frame layout")
		}
		if stackDebug >= 3 {
			print("      saved bp\n")
		}
676 677 678 679 680 681 682 683 684 685
		if debugCheckBP {
			// Frame pointers should always point to the next higher frame on
			// the Go stack (or be nil, for the top frame on the stack).
			bp := *(*uintptr)(unsafe.Pointer(frame.varp))
			if bp != 0 && (bp < adjinfo.old.lo || bp >= adjinfo.old.hi) {
				println("runtime: found invalid frame pointer")
				print("bp=", hex(bp), " min=", hex(adjinfo.old.lo), " max=", hex(adjinfo.old.hi), "\n")
				throw("bad frame pointer")
			}
		}
686 687 688
		adjustpointer(adjinfo, unsafe.Pointer(frame.varp))
	}

689 690 691 692 693 694 695 696
	// Adjust arguments.
	if frame.arglen > 0 {
		var bv bitvector
		if frame.argmap != nil {
			bv = *frame.argmap
		} else {
			stackmap := (*stackmap)(funcdata(f, _FUNCDATA_ArgsPointerMaps))
			if stackmap == nil || stackmap.n <= 0 {
697
				print("runtime: frame ", funcname(f), " untyped args ", frame.argp, "+", frame.arglen, "\n")
698
				throw("missing stackmap")
699 700 701 702
			}
			if pcdata < 0 || pcdata >= stackmap.n {
				// don't know where we are
				print("runtime: pcdata is ", pcdata, " and ", stackmap.n, " args stack map entries for ", funcname(f), " (targetpc=", targetpc, ")\n")
703
				throw("bad symbol table")
704 705 706 707 708 709
			}
			bv = stackmapdata(stackmap, pcdata)
		}
		if stackDebug >= 3 {
			print("      args\n")
		}
710
		adjustpointers(unsafe.Pointer(frame.argp), &bv, adjinfo, funcInfo{})
711 712 713 714 715
	}
	return true
}

func adjustctxt(gp *g, adjinfo *adjustinfo) {
716
	adjustpointer(adjinfo, unsafe.Pointer(&gp.sched.ctxt))
717 718 719 720 721 722 723 724 725 726 727 728
	if !framepointer_enabled {
		return
	}
	if debugCheckBP {
		bp := gp.sched.bp
		if bp != 0 && (bp < adjinfo.old.lo || bp >= adjinfo.old.hi) {
			println("runtime: found invalid top frame pointer")
			print("bp=", hex(bp), " min=", hex(adjinfo.old.lo), " max=", hex(adjinfo.old.hi), "\n")
			throw("bad top frame pointer")
		}
	}
	adjustpointer(adjinfo, unsafe.Pointer(&gp.sched.bp))
729 730 731 732 733 734 735 736 737
}

func adjustdefers(gp *g, adjinfo *adjustinfo) {
	// Adjust defer argument blocks the same way we adjust active stack frames.
	tracebackdefers(gp, adjustframe, noescape(unsafe.Pointer(adjinfo)))

	// Adjust pointers in the Defer structs.
	// Defer structs themselves are never on the stack.
	for d := gp._defer; d != nil; d = d.link {
738 739 740
		adjustpointer(adjinfo, unsafe.Pointer(&d.fn))
		adjustpointer(adjinfo, unsafe.Pointer(&d.sp))
		adjustpointer(adjinfo, unsafe.Pointer(&d._panic))
741 742 743 744 745 746
	}
}

func adjustpanics(gp *g, adjinfo *adjustinfo) {
	// Panics are on stack and already adjusted.
	// Update pointer to head of list in G.
747
	adjustpointer(adjinfo, unsafe.Pointer(&gp._panic))
748 749 750 751 752 753
}

func adjustsudogs(gp *g, adjinfo *adjustinfo) {
	// the data elements pointed to by a SudoG structure
	// might be in the stack.
	for s := gp.waiting; s != nil; s = s.waitlink {
754
		adjustpointer(adjinfo, unsafe.Pointer(&s.elem))
755 756 757 758 759 760 761 762 763
	}
}

func fillstack(stk stack, b byte) {
	for p := stk.lo; p < stk.hi; p++ {
		*(*byte)(unsafe.Pointer(p)) = b
	}
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
func findsghi(gp *g, stk stack) uintptr {
	var sghi uintptr
	for sg := gp.waiting; sg != nil; sg = sg.waitlink {
		p := uintptr(sg.elem) + uintptr(sg.c.elemsize)
		if stk.lo <= p && p < stk.hi && p > sghi {
			sghi = p
		}
	}
	return sghi
}

// syncadjustsudogs adjusts gp's sudogs and copies the part of gp's
// stack they refer to while synchronizing with concurrent channel
// operations. It returns the number of bytes of stack copied.
func syncadjustsudogs(gp *g, used uintptr, adjinfo *adjustinfo) uintptr {
	if gp.waiting == nil {
		return 0
	}

	// Lock channels to prevent concurrent send/receive.
	// It's important that we *only* do this for async
	// copystack; otherwise, gp may be in the middle of
	// putting itself on wait queues and this would
	// self-deadlock.
788
	var lastc *hchan
789
	for sg := gp.waiting; sg != nil; sg = sg.waitlink {
790 791 792 793
		if sg.c != lastc {
			lock(&sg.c.lock)
		}
		lastc = sg.c
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	}

	// Adjust sudogs.
	adjustsudogs(gp, adjinfo)

	// Copy the part of the stack the sudogs point in to
	// while holding the lock to prevent races on
	// send/receive slots.
	var sgsize uintptr
	if adjinfo.sghi != 0 {
		oldBot := adjinfo.old.hi - used
		newBot := oldBot + adjinfo.delta
		sgsize = adjinfo.sghi - oldBot
		memmove(unsafe.Pointer(newBot), unsafe.Pointer(oldBot), sgsize)
	}

	// Unlock channels.
811
	lastc = nil
812
	for sg := gp.waiting; sg != nil; sg = sg.waitlink {
813 814 815 816
		if sg.c != lastc {
			unlock(&sg.c.lock)
		}
		lastc = sg.c
817 818 819 820 821
	}

	return sgsize
}

822
// Copies gp's stack to a new stack of a different size.
823
// Caller must have changed gp status to Gcopystack.
824 825 826 827 828 829
//
// If sync is true, this is a self-triggered stack growth and, in
// particular, no other G may be writing to gp's stack (e.g., via a
// channel operation). If sync is false, copystack protects against
// concurrent channel operations.
func copystack(gp *g, newsize uintptr, sync bool) {
830
	if gp.syscallsp != 0 {
831
		throw("stack growth not allowed in system call")
832 833 834
	}
	old := gp.stack
	if old.lo == 0 {
835
		throw("nil stackbase")
836 837 838 839
	}
	used := old.hi - gp.sched.sp

	// allocate new stack
840
	new := stackalloc(uint32(newsize))
841 842 843 844
	if stackPoisonCopy != 0 {
		fillstack(new, 0xfd)
	}
	if stackDebug >= 1 {
845
		print("copystack gp=", gp, " [", hex(old.lo), " ", hex(old.hi-used), " ", hex(old.hi), "]", " -> [", hex(new.lo), " ", hex(new.hi-used), " ", hex(new.hi), "]/", newsize, "\n")
846 847
	}

848
	// Compute adjustment.
849 850 851 852
	var adjinfo adjustinfo
	adjinfo.old = old
	adjinfo.delta = new.hi - old.hi

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	// Adjust sudogs, synchronizing with channel ops if necessary.
	ncopy := used
	if sync {
		adjustsudogs(gp, &adjinfo)
	} else {
		// sudogs can point in to the stack. During concurrent
		// shrinking, these areas may be written to. Find the
		// highest such pointer so we can handle everything
		// there and below carefully. (This shouldn't be far
		// from the bottom of the stack, so there's little
		// cost in handling everything below it carefully.)
		adjinfo.sghi = findsghi(gp, old)

		// Synchronize with channel ops and copy the part of
		// the stack they may interact with.
		ncopy -= syncadjustsudogs(gp, used, &adjinfo)
	}

	// Copy the stack (or the rest of it) to the new location
	memmove(unsafe.Pointer(new.hi-ncopy), unsafe.Pointer(old.hi-ncopy), ncopy)
873

874 875 876
	// Adjust remaining structures that have pointers into stacks.
	// We have to do most of these before we traceback the new
	// stack because gentraceback uses them.
877 878 879
	adjustctxt(gp, &adjinfo)
	adjustdefers(gp, &adjinfo)
	adjustpanics(gp, &adjinfo)
880 881 882
	if adjinfo.sghi != 0 {
		adjinfo.sghi += adjinfo.delta
	}
883 884 885

	// Swap out old stack for new one
	gp.stack = new
886
	gp.stackguard0 = new.lo + _StackGuard // NOTE: might clobber a preempt request
887
	gp.sched.sp = new.hi - used
888
	gp.stktopsp += adjinfo.delta
889

890 891 892
	// Adjust pointers in the new stack.
	gentraceback(^uintptr(0), ^uintptr(0), 0, gp, 0, nil, 0x7fffffff, adjustframe, noescape(unsafe.Pointer(&adjinfo)), 0)

893 894 895 896
	// free old stack
	if stackPoisonCopy != 0 {
		fillstack(old, 0xfc)
	}
897
	stackfree(old)
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
}

// round x up to a power of 2.
func round2(x int32) int32 {
	s := uint(0)
	for 1<<s < x {
		s++
	}
	return 1 << s
}

// Called from runtime·morestack when more stack is needed.
// Allocate larger stack and relocate to new stack.
// Stack growth is multiplicative, for constant amortized cost.
//
// g->atomicstatus will be Grunning or Gscanrunning upon entry.
// If the GC is trying to stop this g then it will set preemptscan to true.
915
//
916 917 918 919 920 921
// This must be nowritebarrierrec because it can be called as part of
// stack growth from other nowritebarrierrec functions, but the
// compiler doesn't check this.
//
//go:nowritebarrierrec
func newstack() {
922 923
	thisg := getg()
	// TODO: double check all gp. shouldn't be getg().
924
	if thisg.m.morebuf.g.ptr().stackguard0 == stackFork {
925
		throw("stack growth after fork")
926
	}
927
	if thisg.m.morebuf.g.ptr() != thisg.m.curg {
928
		print("runtime: newstack called from g=", hex(thisg.m.morebuf.g), "\n"+"\tm=", thisg.m, " m->curg=", thisg.m.curg, " m->g0=", thisg.m.g0, " m->gsignal=", thisg.m.gsignal, "\n")
929
		morebuf := thisg.m.morebuf
930
		traceback(morebuf.pc, morebuf.sp, morebuf.lr, morebuf.g.ptr())
931
		throw("runtime: wrong goroutine in newstack")
932
	}
933 934 935

	gp := thisg.m.curg

936 937 938 939 940
	if thisg.m.curg.throwsplit {
		// Update syscallsp, syscallpc in case traceback uses them.
		morebuf := thisg.m.morebuf
		gp.syscallsp = morebuf.sp
		gp.syscallpc = morebuf.pc
941 942 943 944 945 946 947 948
		pcname, pcoff := "(unknown)", uintptr(0)
		f := findfunc(gp.sched.pc)
		if f.valid() {
			pcname = funcname(f)
			pcoff = gp.sched.pc - f.entry
		}
		print("runtime: newstack at ", pcname, "+", hex(pcoff),
			" sp=", hex(gp.sched.sp), " stack=[", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n",
949 950
			"\tmorebuf={pc:", hex(morebuf.pc), " sp:", hex(morebuf.sp), " lr:", hex(morebuf.lr), "}\n",
			"\tsched={pc:", hex(gp.sched.pc), " sp:", hex(gp.sched.sp), " lr:", hex(gp.sched.lr), " ctxt:", gp.sched.ctxt, "}\n")
951

952
		thisg.m.traceback = 2 // Include runtime frames
953
		traceback(morebuf.pc, morebuf.sp, morebuf.lr, gp)
954
		throw("runtime: stack split at bad time")
955 956 957 958 959 960
	}

	morebuf := thisg.m.morebuf
	thisg.m.morebuf.pc = 0
	thisg.m.morebuf.lr = 0
	thisg.m.morebuf.sp = 0
961
	thisg.m.morebuf.g = 0
962

963 964 965
	// NOTE: stackguard0 may change underfoot, if another thread
	// is about to try to preempt gp. Read it just once and use that same
	// value now and below.
966
	preempt := atomic.Loaduintptr(&gp.stackguard0) == stackPreempt
967

968 969
	// Be conservative about where we preempt.
	// We are interested in preempting user Go code, not runtime code.
970 971
	// If we're holding locks, mallocing, or preemption is disabled, don't
	// preempt.
972 973 974 975 976 977 978 979
	// This check is very early in newstack so that even the status change
	// from Grunning to Gwaiting and back doesn't happen in this case.
	// That status change by itself can be viewed as a small preemption,
	// because the GC might change Gwaiting to Gscanwaiting, and then
	// this goroutine has to wait for the GC to finish before continuing.
	// If the GC is in some way dependent on this goroutine (for example,
	// it needs a lock held by the goroutine), that small preemption turns
	// into a real deadlock.
980
	if preempt {
981
		if thisg.m.locks != 0 || thisg.m.mallocing != 0 || thisg.m.preemptoff != "" || thisg.m.p.ptr().status != _Prunning {
982 983 984 985 986 987
			// Let the goroutine keep running for now.
			// gp->preempt is set, so it will be preempted next time.
			gp.stackguard0 = gp.stack.lo + _StackGuard
			gogo(&gp.sched) // never return
		}
	}
988 989

	if gp.stack.lo == 0 {
990
		throw("missing stack in newstack")
991 992
	}
	sp := gp.sched.sp
993
	if sys.ArchFamily == sys.AMD64 || sys.ArchFamily == sys.I386 {
994
		// The call to morestack cost a word.
995
		sp -= sys.PtrSize
996 997 998 999 1000 1001 1002
	}
	if stackDebug >= 1 || sp < gp.stack.lo {
		print("runtime: newstack sp=", hex(sp), " stack=[", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n",
			"\tmorebuf={pc:", hex(morebuf.pc), " sp:", hex(morebuf.sp), " lr:", hex(morebuf.lr), "}\n",
			"\tsched={pc:", hex(gp.sched.pc), " sp:", hex(gp.sched.sp), " lr:", hex(gp.sched.lr), " ctxt:", gp.sched.ctxt, "}\n")
	}
	if sp < gp.stack.lo {
1003
		print("runtime: gp=", gp, ", goid=", gp.goid, ", gp->status=", hex(readgstatus(gp)), "\n ")
1004
		print("runtime: split stack overflow: ", hex(sp), " < ", hex(gp.stack.lo), "\n")
1005
		throw("runtime: split stack overflow")
1006 1007
	}

1008
	if preempt {
1009
		if gp == thisg.m.g0 {
1010
			throw("runtime: preempt g0")
1011
		}
1012
		if thisg.m.p == 0 && thisg.m.locks == 0 {
1013
			throw("runtime: g is running but p is not")
1014
		}
1015 1016
		// Synchronize with scang.
		casgstatus(gp, _Grunning, _Gwaiting)
1017
		if gp.preemptscan {
1018
			for !castogscanstatus(gp, _Gwaiting, _Gscanwaiting) {
1019 1020 1021 1022 1023
				// Likely to be racing with the GC as
				// it sees a _Gwaiting and does the
				// stack scan. If so, gcworkdone will
				// be set and gcphasework will simply
				// return.
1024
			}
1025
			if !gp.gcscandone {
1026 1027 1028 1029 1030 1031 1032
				// gcw is safe because we're on the
				// system stack.
				gcw := &gp.m.p.ptr().gcw
				scanstack(gp, gcw)
				if gcBlackenPromptly {
					gcw.dispose()
				}
1033 1034 1035 1036
				gp.gcscandone = true
			}
			gp.preemptscan = false
			gp.preempt = false
1037
			casfrom_Gscanstatus(gp, _Gscanwaiting, _Gwaiting)
1038
			// This clears gcscanvalid.
1039
			casgstatus(gp, _Gwaiting, _Grunning)
1040
			gp.stackguard0 = gp.stack.lo + _StackGuard
1041
			gogo(&gp.sched) // never return
1042 1043 1044 1045
		}

		// Act like goroutine called runtime.Gosched.
		casgstatus(gp, _Gwaiting, _Grunning)
1046
		gopreempt_m(gp) // never return
1047 1048 1049
	}

	// Allocate a bigger segment and move the stack.
1050
	oldsize := gp.stack.hi - gp.stack.lo
1051
	newsize := oldsize * 2
1052
	if newsize > maxstacksize {
1053
		print("runtime: goroutine stack exceeds ", maxstacksize, "-byte limit\n")
1054
		throw("stack overflow")
1055 1056
	}

1057 1058 1059
	// The goroutine must be executing in order to call newstack,
	// so it must be Grunning (or Gscanrunning).
	casgstatus(gp, _Grunning, _Gcopystack)
1060 1061 1062

	// The concurrent GC will not scan the stack while we are doing the copy since
	// the gp is in a Gcopystack status.
1063
	copystack(gp, newsize, true)
1064 1065 1066
	if stackDebug >= 1 {
		print("stack grow done\n")
	}
1067
	casgstatus(gp, _Gcopystack, _Grunning)
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	gogo(&gp.sched)
}

//go:nosplit
func nilfunc() {
	*(*uint8)(nil) = 0
}

// adjust Gobuf as if it executed a call to fn
// and then did an immediate gosave.
func gostartcallfn(gobuf *gobuf, fv *funcval) {
	var fn unsafe.Pointer
	if fv != nil {
1081
		fn = unsafe.Pointer(fv.fn)
1082 1083 1084
	} else {
		fn = unsafe.Pointer(funcPC(nilfunc))
	}
1085
	gostartcall(gobuf, fn, unsafe.Pointer(fv))
1086 1087 1088 1089
}

// Maybe shrink the stack being used by gp.
// Called at garbage collection time.
1090
// gp must be stopped, but the world need not be.
1091
func shrinkstack(gp *g) {
1092 1093
	gstatus := readgstatus(gp)
	if gstatus&^_Gscan == _Gdead {
1094 1095 1096
		if gp.stack.lo != 0 {
			// Free whole stack - it will get reallocated
			// if G is used again.
1097
			stackfree(gp.stack)
1098 1099 1100 1101 1102 1103
			gp.stack.lo = 0
			gp.stack.hi = 0
		}
		return
	}
	if gp.stack.lo == 0 {
1104
		throw("missing stack in shrinkstack")
1105
	}
1106 1107 1108
	if gstatus&_Gscan == 0 {
		throw("bad status in shrinkstack")
	}
1109

1110 1111 1112
	if debug.gcshrinkstackoff > 0 {
		return
	}
1113 1114
	f := findfunc(gp.startpc)
	if f.valid() && f.funcID == funcID_gcBgMarkWorker {
1115 1116 1117 1118
		// We're not allowed to shrink the gcBgMarkWorker
		// stack (see gcBgMarkWorker for explanation).
		return
	}
1119

1120
	oldsize := gp.stack.hi - gp.stack.lo
1121
	newsize := oldsize / 2
1122 1123
	// Don't shrink the allocation below the minimum-sized stack
	// allocation.
1124
	if newsize < _FixedStack {
1125
		return
1126
	}
1127 1128 1129 1130 1131 1132 1133 1134
	// Compute how much of the stack is currently in use and only
	// shrink the stack if gp is using less than a quarter of its
	// current stack. The currently used stack includes everything
	// down to the SP plus the stack guard space that ensures
	// there's room for nosplit functions.
	avail := gp.stack.hi - gp.stack.lo
	if used := gp.stack.hi - gp.sched.sp + _StackLimit; used >= avail/4 {
		return
1135 1136 1137 1138 1139 1140 1141
	}

	// We can't copy the stack if we're in a syscall.
	// The syscall might have pointers into the stack.
	if gp.syscallsp != 0 {
		return
	}
1142
	if sys.GoosWindows != 0 && gp.m != nil && gp.m.libcallsp != 0 {
1143 1144 1145 1146 1147 1148
		return
	}

	if stackDebug > 0 {
		print("shrinking stack ", oldsize, "->", newsize, "\n")
	}
1149

1150
	copystack(gp, newsize, false)
1151 1152
}

1153 1154
// freeStackSpans frees unused stack spans at the end of GC.
func freeStackSpans() {
1155
	lock(&stackpoolmu)
1156 1157 1158 1159

	// Scan stack pools for empty stack spans.
	for order := range stackpool {
		list := &stackpool[order]
1160
		for s := list.first; s != nil; {
1161
			next := s.next
1162
			if s.allocCount == 0 {
1163
				list.remove(s)
1164
				s.manualFreeList = 0
1165
				mheap_.freeManual(s, &memstats.stacks_inuse)
1166 1167 1168
			}
			s = next
		}
1169
	}
1170 1171

	unlock(&stackpoolmu)
1172 1173 1174 1175 1176 1177 1178

	// Free large stack spans.
	lock(&stackLarge.lock)
	for i := range stackLarge.free {
		for s := stackLarge.free[i].first; s != nil; {
			next := s.next
			stackLarge.free[i].remove(s)
1179
			mheap_.freeManual(s, &memstats.stacks_inuse)
1180 1181 1182 1183
			s = next
		}
	}
	unlock(&stackLarge.lock)
1184
}
1185 1186 1187

//go:nosplit
func morestackc() {
1188
	throw("attempt to execute system stack code on user stack")
1189
}