Source file src/runtime/preempt_xsave_amd64.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime
     6  
     7  import (
     8  	"internal/cpu"
     9  	"internal/goexperiment"
    10  	"unsafe"
    11  )
    12  
    13  // On amd64, asyncPreempt saves the vector and mask registers with XSAVEC if the
    14  // CPU supports it. XSAVEC saves only the state components that aren't in their
    15  // initial state, and records which ones it saved in the XSAVE header. Unlike
    16  // XGETBV(ECX=1), XSAVEC is an SSE/AVX instruction, so it sees the thread's
    17  // state even on OSes that load vector state lazily. asyncPreempt restores the
    18  // registers with XRSTOR, which returns the components that weren't saved to
    19  // their initial state.
    20  //
    21  // Without XSAVEC, asyncPreempt saves the registers that Go code may be using
    22  // with explicit moves, in an xRegsNoXSAVEC.
    23  
    24  // XSAVE state components that asyncPreempt saves. Component i corresponds to
    25  // bit i of XCR0 and of XSTATE_BV (see the xcr0_AX_* constants). Components 0
    26  // (x87) and 1 live in the legacy region, and the rest are in the extended
    27  // region.
    28  const (
    29  	xsaveSSE       = 1 // X0-X15, in the legacy region
    30  	xsaveYMM_Hi128 = 2 // Upper 128 bits of Y0-Y15
    31  	xsaveOpMask    = 5 // K0-K7
    32  	xsaveZMM_Hi256 = 6 // Upper 256 bits of Z0-Z15
    33  	xsaveHi16_ZMM  = 7 // Z16-Z31
    34  
    35  	xsaveFirstExtended = xsaveYMM_Hi128
    36  	xsaveNumComponents = xsaveHi16_ZMM + 1
    37  )
    38  
    39  // Architectural offsets in an XSAVE area.
    40  const (
    41  	xsaveXMM       = 160 // X0-X15, in the legacy region. We ignore bytes 0–159 entirely.
    42  	xsaveXSTATE_BV = 512 // XSTATE_BV, in the XSAVE header
    43  	xsaveExtended  = 576 // Start of the extended region
    44  
    45  	// xsaveMaxSize is the size of a compacted XSAVE area holding all of the
    46  	// state components that asyncPreempt saves, given their architectural
    47  	// sizes. Each of these sizes is a multiple of 64 bytes, so aligning
    48  	// components adds no padding.
    49  	xsaveMaxSize = (xsaveExtended +
    50  		16*16 + // YMM_Hi128: upper 128 bits of Y0-Y15
    51  		8*8 + // Opmask: K0-K7
    52  		16*32 + // ZMM_Hi256: upper 256 bits of Z0-Z15
    53  		16*64) // Hi16_ZMM: Z16-Z31
    54  )
    55  
    56  var (
    57  	// useXSAVEC indicates that asyncPreempt saves registers with XSAVEC.
    58  	useXSAVEC bool
    59  
    60  	// xsaveMask is the set of XSAVE state components that asyncPreempt
    61  	// saves with XSAVEC, as xcr0_AX_* bits.
    62  	xsaveMask uint32
    63  
    64  	// xsaveOff[i] and xsaveSize[i] are the offset and size of extended
    65  	// XSAVE state component i in a compacted XSAVE area holding xsaveMask.
    66  	xsaveOff, xsaveSize [xsaveNumComponents]uint16
    67  )
    68  
    69  // xRegInitCPU sets up the XSAVE area layout once CPU features are known.
    70  func xRegInitCPU() {
    71  	if !goexperiment.SIMDStable {
    72  		// Without SIMD enabled, Go code only uses X0-X15. Use the fixed
    73  		// xRegsNoXSAVEC layout, which is simpler.
    74  		return
    75  	}
    76  	if !cpu.X86.HasXSAVEC {
    77  		// Fall back to the fixed xRegsNoXSAVEC layout.
    78  		return
    79  	}
    80  	if GOOS == "darwin" && cpu.X86.HasAVX512 {
    81  		// darwin enables AVX-512 state per thread, on first use (see
    82  		// go.dev/issue/49233). An XSAVE area saved on a thread with
    83  		// AVX-512 enabled includes the AVX-512 components, and XRSTOR of
    84  		// it on a thread without AVX-512 enabled faults with #GP, rather
    85  		// than the #UD that darwin uses to enable AVX-512. Fall back to
    86  		// the fixed xRegsNoXSAVEC layout, which uses EVEX instructions.
    87  		return
    88  	}
    89  	// The vector state components that Go code may use.
    90  	mask := uint32(xcr0_AX_SSE)
    91  	if cpu.X86.HasAVX {
    92  		mask |= xcr0_AX_YMM
    93  	}
    94  	if cpu.X86.HasAVX512 {
    95  		mask |= xcr0_AX_OpMask | xcr0_AX_ZMM_Hi256 | xcr0_AX_Hi16_ZMM
    96  	}
    97  	// The compacted format lays out every requested extended component in
    98  	// order, whether or not it's in use, aligning some of them. So the
    99  	// offset of each component depends only on the requested components
   100  	// and on their sizes and alignment, and is the same for every XSAVE
   101  	// area in this process. XSAVEC doesn't write the components that
   102  	// aren't in use, leaving holes in the area.
   103  	off := uint32(xsaveExtended)
   104  	for i := uint32(xsaveFirstExtended); i < xsaveNumComponents; i++ {
   105  		if mask&(1<<i) == 0 {
   106  			continue
   107  		}
   108  		size, align64 := cpu.XSAVEComponent(i)
   109  		if align64 {
   110  			off = (off + 63) &^ 63
   111  		}
   112  		xsaveOff[i], xsaveSize[i] = uint16(off), uint16(size)
   113  		off += size
   114  	}
   115  	if off > xsaveMaxSize {
   116  		// Unexpected component sizes. Fall back to the fixed xRegsNoXSAVEC
   117  		// layout.
   118  		return
   119  	}
   120  	xsaveMask = mask
   121  	useXSAVEC = true
   122  }
   123  
   124  // base returns the address of x's XSAVE area.
   125  func (x *xRegs) base() uintptr {
   126  	return alignUp(uintptr(unsafe.Pointer(&x.buf)), 64)
   127  }
   128  
   129  // inUse returns the set of state components saved in x's XSAVE area, as
   130  // xcr0_AX_* bits.
   131  func (x *xRegs) inUse() uint32 {
   132  	return *(*uint32)(unsafe.Pointer(x.base() + xsaveXSTATE_BV))
   133  }
   134  
   135  func (dst *xRegs) copyFrom(src *xRegs) {
   136  	if !useXSAVEC {
   137  		memmove(unsafe.Pointer(&dst.buf), unsafe.Pointer(&src.buf), unsafe.Sizeof(xRegsNoXSAVEC{}))
   138  		return
   139  	}
   140  	// Copy the legacy region and the XSAVE header, and each extended
   141  	// component that's in use. XRSTOR ignores the other components.
   142  	d, s := dst.base(), src.base()
   143  	memmove(unsafe.Pointer(d), unsafe.Pointer(s), xsaveExtended)
   144  	inUse := src.inUse()
   145  	for i := xsaveFirstExtended; i < xsaveNumComponents; i++ {
   146  		if inUse&(1<<i) != 0 {
   147  			off := uintptr(xsaveOff[i])
   148  			memmove(unsafe.Pointer(d+off), unsafe.Pointer(s+off), uintptr(xsaveSize[i]))
   149  		}
   150  	}
   151  }
   152  
   153  func (x *xRegs) scanRanges(yield func(base, size uintptr)) {
   154  	if !useXSAVEC {
   155  		yield(uintptr(unsafe.Pointer(&x.buf)), unsafe.Sizeof(xRegsNoXSAVEC{}))
   156  		return
   157  	}
   158  	// Scan the vector registers that are in use. K0-K7 can't hold pointers.
   159  	b, inUse := x.base(), x.inUse()
   160  	if inUse&(1<<xsaveSSE) != 0 {
   161  		yield(b+xsaveXMM, 16*16)
   162  	}
   163  	for _, i := range [...]int{xsaveYMM_Hi128, xsaveZMM_Hi256, xsaveHi16_ZMM} {
   164  		if inUse&(1<<i) != 0 {
   165  			yield(b+uintptr(xsaveOff[i]), uintptr(xsaveSize[i]))
   166  		}
   167  	}
   168  }
   169  

View as plain text