From 0bd25dc875a3157974c06ba8d4fb33f95ac2dfdd Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Fri, 12 Dec 2025 15:32:43 -0800 Subject: internal/runtime/sys: improve DIT assembly In EnableDIT, if DIT is already enabled, return early instead of executing MSR and DSB/ISB, since they are not particularly cheap instructions. Also, if we have support for the SB (Speculation Barrier) instruction, use it instead of DSB+ISB when enabling DIT, since SB is cheaper. Change-Id: I1b3ecbd95ed42bfd10d646125704abf4e80b6d2e Reviewed-on: https://go-review.googlesource.com/c/go/+/729800 Reviewed-by: Keith Randall Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- src/internal/runtime/sys/dit_arm64.go | 3 +++ src/internal/runtime/sys/dit_arm64.s | 11 ++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src/internal/runtime/sys') diff --git a/src/internal/runtime/sys/dit_arm64.go b/src/internal/runtime/sys/dit_arm64.go index 643fd770d5..c83df081a0 100644 --- a/src/internal/runtime/sys/dit_arm64.go +++ b/src/internal/runtime/sys/dit_arm64.go @@ -8,10 +8,13 @@ package sys import ( "internal/cpu" + "unsafe" ) var DITSupported = cpu.ARM64.HasDIT +const offsetARM64HasSB = unsafe.Offsetof(cpu.ARM64.HasSB) + func EnableDIT() bool func DITEnabled() bool func DisableDIT() diff --git a/src/internal/runtime/sys/dit_arm64.s b/src/internal/runtime/sys/dit_arm64.s index 408b60c8c0..cc5642f838 100644 --- a/src/internal/runtime/sys/dit_arm64.s +++ b/src/internal/runtime/sys/dit_arm64.s @@ -2,16 +2,21 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +#include "go_asm.h" #include "textflag.h" TEXT ·EnableDIT(SB),$0-1 MRS DIT, R0 UBFX $24, R0, $1, R1 MOVB R1, ret+0(FP) + TBNZ $0, R1, ret MSR $1, DIT - // TODO(roland): the SB instruction is significantly more - // performant when available. We should detect its availability - // and use it when we can. + MOVBU internal∕cpu·ARM64+const_offsetARM64HasSB(SB), R2 + TBZ $0, R2, sbFallback + SB +ret: + RET +sbFallback: DSB $7 // nsh ISB $15 // sy RET -- cgit v1.3-5-g9baa