From 9b4244e9eaab2b2b4bdf197462d336ac2e3a5284 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Tue, 10 Mar 2026 10:48:31 +0100 Subject: runtime: return the error code as a return value in syscallN_trampoline Getting errno from assembly code is cheap. There is no need to overcomplicate syscall_rawsyscalln to get errno from the cached errno address pointer stored in the M struct. This also better aligns syscallN_trampoline with the cgocall convention of returning the error code as a return value. Cq-Include-Trybots: luci.golang.try:gotip-darwin-amd64_15,gotip-darwin-arm64_26 Change-Id: I05d5177e7c1471942e8ecafb4fb05594b4b18e2b Reviewed-on: https://go-review.googlesource.com/c/go/+/753540 Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Pratt --- src/runtime/os_darwin.go | 6 ------ src/runtime/sys_darwin.go | 22 ++-------------------- src/runtime/sys_darwin_amd64.s | 5 +---- src/runtime/sys_darwin_arm64.s | 9 +++------ 4 files changed, 6 insertions(+), 36 deletions(-) (limited to 'src/runtime') diff --git a/src/runtime/os_darwin.go b/src/runtime/os_darwin.go index 19dcf3be8e..e0305545af 100644 --- a/src/runtime/os_darwin.go +++ b/src/runtime/os_darwin.go @@ -15,11 +15,6 @@ type mOS struct { mutex pthreadmutex cond pthreadcond count int - - // address of errno variable for this thread. - // This is an optimization to avoid calling libc_error - // on every syscall_rawsyscalln. - errnoAddr *int32 } func unimplemented(name string) { @@ -346,7 +341,6 @@ func minit() { } minitSignalMask() getg().m.procid = uint64(pthread_self()) - libc_error_addr(&getg().m.errnoAddr) } // Called from dropm to undo the effect of an minit. diff --git a/src/runtime/sys_darwin.go b/src/runtime/sys_darwin.go index 48ad5afd8a..9ef62ab4c0 100644 --- a/src/runtime/sys_darwin.go +++ b/src/runtime/sys_darwin.go @@ -10,17 +10,6 @@ import ( "unsafe" ) -func libc_error_trampoline() - -// libc_error_addr puts the libc error -// address into addr. -// -//go:nosplit -//go:cgo_unsafe_args -func libc_error_addr(addr **int32) { - libcCall(unsafe.Pointer(abi.FuncPCABI0(libc_error_trampoline)), unsafe.Pointer(&addr)) -} - // libcCallInfo is a structure used to pass parameters to the system call. type libcCallInfo struct { fn uintptr @@ -57,15 +46,8 @@ func syscall_rawsyscalln(fn uintptr, args ...uintptr) (r1, r2, err uintptr) { if c.n != 0 { c.args = uintptr(noescape(unsafe.Pointer(&args[0]))) } - libcCall(unsafe.Pointer(abi.FuncPCABI0(syscallN_trampoline)), unsafe.Pointer(c)) - if gp := getg(); gp != nil && gp.m != nil && gp.m.errnoAddr != nil { - err = uintptr(*gp.m.errnoAddr) - } else { - var errnoAddr *int32 - libc_error_addr(&errnoAddr) - err = uintptr(*errnoAddr) - } - return c.r1, c.r2, err + errno := libcCall(unsafe.Pointer(abi.FuncPCABI0(syscallN_trampoline)), unsafe.Pointer(c)) + return c.r1, c.r2, uintptr(errno) } func syscallN_trampoline() diff --git a/src/runtime/sys_darwin_amd64.s b/src/runtime/sys_darwin_amd64.s index 99d67a9cfd..e402fa631c 100644 --- a/src/runtime/sys_darwin_amd64.s +++ b/src/runtime/sys_darwin_amd64.s @@ -567,12 +567,9 @@ _0args: // Return result. MOVQ AX, libcCallInfo_r1(R13) MOVQ DX, libcCallInfo_r2(R13) - RET -TEXT runtime·libc_error_trampoline(SB),NOSPLIT,$0 - MOVQ 0(DI), R14 CALL libc_error(SB) - MOVQ AX, (R14) + MOVL (AX), AX // errno RET // syscall_x509 is for crypto/x509. It is like syscall6 but does not check for errors, diff --git a/src/runtime/sys_darwin_arm64.s b/src/runtime/sys_darwin_arm64.s index 7bbe965c15..368cb716ed 100644 --- a/src/runtime/sys_darwin_arm64.s +++ b/src/runtime/sys_darwin_arm64.s @@ -565,16 +565,13 @@ _0args: MOVD R0, libcCallInfo_r1(R19) MOVD R1, libcCallInfo_r2(R19) + BL libc_error(SB) + MOVW (R0), R0 // errno + // Restore callee-saved registers. LDP 16(RSP), (R19, R20) RET -TEXT runtime·libc_error_trampoline(SB),NOSPLIT,$0 - MOVD 0(R0), R20 - BL libc_error(SB) - MOVD R0, (R20) - RET - // syscall_x509 is for crypto/x509. It is like syscall6 but does not check for errors, // takes 5 uintptrs and 1 float64, and only returns one value, // for use with standard C ABI functions. -- cgit v1.3-5-g9baa