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 <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Roland Shoemaker
2025-12-12 15:32:43 -08:00
parent b7db3246a6
commit 0bd25dc875
2 changed files with 11 additions and 3 deletions

View File

@@ -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()

View File

@@ -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 internalcpu·ARM64+const_offsetARM64HasSB(SB), R2
TBZ $0, R2, sbFallback
SB
ret:
RET
sbFallback:
DSB $7 // nsh
ISB $15 // sy
RET