go[runtime.rt0_go]: why should 104 be subtracted from g0 stack 64k?

114 views
Skip to first unread message

liiux...@gmail.com

unread,
Nov 3, 2022, 10:46:55 AM11/3/22
to golang-nuts

asm_amd64.s

Please explain why 104 should be subtracted from g0 stack?
```go

TEXT runtime·rt0_go(SB),NOSPLIT|TOPFRAME,$0

   // copy arguments forward on an even stack

   MOVQ    DI, AX        // argc

   MOVQ    SI, BX        // argv

   SUBQ    $(5*8), SP        // 3args 2auto

   ANDQ    $~15, SP

   MOVQ    AX, 24(SP)

   MOVQ    BX, 32(SP)



   // create istack out of the given (operating system) stack.

   // _cgo_init may update stackguard.

   MOVQ    $runtime·g0(SB), DI

   LEAQ    (-64*1024+104)(SP), BX

   MOVQ    BX, g_stackguard0(DI)

   MOVQ    BX, g_stackguard1(DI)

   MOVQ    BX, (g_stack+stack_lo)(DI)

   MOVQ    SP, (g_stack+stack_hi)(DI)
```

but asm_arm64.s is 64kb:

```go

   // create istack out of the given (operating system) stack.

   // _cgo_init may update stackguard.

   MOVD    $runtime·g0(SB), g

   MOVD    RSP, R7

   MOVD    $(-64*1024)(R7), R0

   MOVD    R0, g_stackguard0(g)

   MOVD    R0, g_stackguard1(g)

   MOVD    R0, (g_stack+stack_lo)(g)

   MOVD    R7, (g_stack+stack_hi)(g)
```

Ian Lance Taylor

unread,
Nov 4, 2022, 6:01:53 PM11/4/22
to liiux...@gmail.com, golang-nuts
On Thu, Nov 3, 2022 at 7:46 AM liiux...@gmail.com <liiux...@gmail.com> wrote:
>
> asm_amd64.s
>
> Please explain why 104 should be subtracted from g0 stack?

Interesting question. The number 104 appears to date back to the
first implementation of split stacks in
https://go.googlesource.com/go/+/b987f7a757f53f460973622a36eebb696f9b5060.
That change introduces a 104 byte stack guard, and I believe that that
stack guard has been carried forward over the years. I don't really
know whether it still makes any sense today. Probably it doesn't.

Ian

liiu liiu

unread,
Nov 5, 2022, 3:35:24 AM11/5/22
to Ian Lance Taylor, golang-nuts
Thanks for the info. I submit a pull request to remove it.

Ian Lance Taylor <ia...@golang.org> 于2022年11月5日周六 06:01写道:
Reply all
Reply to author
Forward
0 new messages