runtime: implement cheaper context switch on Linux/AMD64
Currently, goroutine switches are implemented with libc
getcontext/setcontext functions, which saves/restores the machine
register states and also the signal context. This does more than
what we need, and performs an expensive syscall.
This CL implements a simplified version of getcontext/setcontext,
in assembly, that only saves/restores the necessary part, i.e.
the callee-save registers, and the PC, SP. A simplified version
of makecontext, written in C, is also added. Currently this is
only implemented on Linux/AMD64.
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/178298
From-SVN: r271818
2019-05-31 19:56:36 +02:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
// This provides a simplified version of getcontext and
|
|
|
|
// setcontext. They are like the corresponding functions
|
|
|
|
// in libc, but we only save/restore the callee-save
|
|
|
|
// registers and PC, SP. Unlike the libc functions, we
|
|
|
|
// don't save/restore the signal masks and floating point
|
|
|
|
// environment.
|
|
|
|
|
|
|
|
#if defined(__x86_64__) && defined(__linux__) && !defined(__CET__)
|
|
|
|
|
|
|
|
#define RBP_OFF (0*8)
|
|
|
|
#define RBX_OFF (1*8)
|
|
|
|
#define R12_OFF (2*8)
|
|
|
|
#define R13_OFF (3*8)
|
|
|
|
#define R14_OFF (4*8)
|
|
|
|
#define R15_OFF (5*8)
|
|
|
|
#define SP_OFF (6*8)
|
|
|
|
#define PC_OFF (7*8)
|
|
|
|
|
|
|
|
.globl __go_getcontext
|
|
|
|
.text
|
|
|
|
__go_getcontext:
|
|
|
|
movq %rbx, RBX_OFF(%rdi)
|
|
|
|
movq %rbp, RBP_OFF(%rdi)
|
|
|
|
movq %r12, R12_OFF(%rdi)
|
|
|
|
movq %r13, R13_OFF(%rdi)
|
|
|
|
movq %r14, R14_OFF(%rdi)
|
|
|
|
movq %r15, R15_OFF(%rdi)
|
|
|
|
|
|
|
|
movq (%rsp), %rax // return PC
|
|
|
|
movq %rax, PC_OFF(%rdi)
|
|
|
|
leaq 8(%rsp), %rax // the SP before pushing return PC
|
|
|
|
movq %rax, SP_OFF(%rdi)
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.globl __go_setcontext
|
|
|
|
.text
|
|
|
|
__go_setcontext:
|
|
|
|
movq RBX_OFF(%rdi), %rbx
|
|
|
|
movq RBP_OFF(%rdi), %rbp
|
|
|
|
movq R12_OFF(%rdi), %r12
|
|
|
|
movq R13_OFF(%rdi), %r13
|
|
|
|
movq R14_OFF(%rdi), %r14
|
|
|
|
movq R15_OFF(%rdi), %r15
|
|
|
|
movq SP_OFF(%rdi), %rsp
|
|
|
|
movq PC_OFF(%rdi), %rdx
|
|
|
|
|
|
|
|
jmp *%rdx
|
|
|
|
|
|
|
|
.globl __go_makecontext
|
|
|
|
.text
|
|
|
|
__go_makecontext:
|
|
|
|
addq %rcx, %rdx
|
|
|
|
|
|
|
|
// Align the SP, and push a dummy return address.
|
2019-06-03 22:06:50 +02:00
|
|
|
andq $~0xf, %rdx
|
runtime: implement cheaper context switch on Linux/AMD64
Currently, goroutine switches are implemented with libc
getcontext/setcontext functions, which saves/restores the machine
register states and also the signal context. This does more than
what we need, and performs an expensive syscall.
This CL implements a simplified version of getcontext/setcontext,
in assembly, that only saves/restores the necessary part, i.e.
the callee-save registers, and the PC, SP. A simplified version
of makecontext, written in C, is also added. Currently this is
only implemented on Linux/AMD64.
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/178298
From-SVN: r271818
2019-05-31 19:56:36 +02:00
|
|
|
subq $8, %rdx
|
|
|
|
movq $0, (%rdx)
|
|
|
|
|
|
|
|
movq %rdx, SP_OFF(%rdi)
|
|
|
|
movq %rsi, PC_OFF(%rdi)
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2019-10-02 22:11:35 +02:00
|
|
|
.section .note.GNU-split-stack,"",@progbits
|
|
|
|
.section .note.GNU-no-split-stack,"",@progbits
|
|
|
|
|
runtime: implement cheaper context switch on Linux/AMD64
Currently, goroutine switches are implemented with libc
getcontext/setcontext functions, which saves/restores the machine
register states and also the signal context. This does more than
what we need, and performs an expensive syscall.
This CL implements a simplified version of getcontext/setcontext,
in assembly, that only saves/restores the necessary part, i.e.
the callee-save registers, and the PC, SP. A simplified version
of makecontext, written in C, is also added. Currently this is
only implemented on Linux/AMD64.
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/178298
From-SVN: r271818
2019-05-31 19:56:36 +02:00
|
|
|
#endif
|
2019-12-05 18:51:10 +01:00
|
|
|
|
|
|
|
.section .note.GNU-stack,"",@progbits
|