Documentation
¶
Overview ¶
Package platform includes runtime-specific code needed for the compiler or otherwise.
Index ¶
- Constants
- Variables
- func CompilerSupported() bool
- func CompilerSupports(features api.CoreFeatures) bool
- func JCCErratumWorkaroundEnabled() bool
- func MakeStack(n int) []byte
- func MapCodeSegmentFromFile(f *os.File, offset int64, size int) ([]byte, error)
- func MmapCodeSegment(size int) ([]byte, error)
- func MprotectCodeSegment(b []byte) (err error)
- func MunmapCodeSegment(code []byte) error
- func Nanosleep(ns int64)
- func Nanotime() int64
- func NewFakeNanotime() sys.Nanotime
- func NewFakeRandSource() io.Reader
- func NewFakeWalltime() sys.Walltime
- func ToPosixPath(in string) string
- func Walltime() (sec int64, nsec int32)
- type CpuFeatureFlags
Constants ¶
const ( // CpuFeatureAmd64SSE4_1 is the flag to query CpuFeatureFlags.Has for SSEv4.1 capabilities on amd64 CpuFeatureAmd64SSE4_1 = 1 << iota // CpuFeatureAmd64BMI1 is the flag to query CpuFeatureFlags.Has for Bit Manipulation Instruction Set 1 (e.g. TZCNT) on amd64 CpuFeatureAmd64BMI1 // CpuExtraFeatureABM is the flag to query CpuFeatureFlags.Has for Advanced Bit Manipulation capabilities (e.g. LZCNT) on amd64 CpuFeatureAmd64ABM // CpuFeatureAmd64JCCErratum is the flag to query CpuFeatureFlags.Has for a Skylake-family // amd64 core subject to Intel erratum SKX102, the "JCC erratum". Unlike the others this is // not a capability but a defect the code layout has to route around; see cpuid_jcc.go and // JCCErratumWorkaroundEnabled. CpuFeatureAmd64JCCErratum )
const CodeFileAlignment = 64 << 10
CodeFileAlignment is the alignment MapCodeSegmentFromFile needs of a file offset: a multiple of the page size on every platform wazy runs on.
const (
// FakeEpochNanos is midnight UTC 2022-01-01 and exposed for testing
FakeEpochNanos = 1640995200000 * ms
)
Variables ¶
var CpuFeatures = loadCpuFeatureFlags()
CpuFeatures exposes the capabilities for this CPU, queried via the Has method.
var FakeNanosleep = sys.Nanosleep(func(int64) {})
FakeNanosleep implements sys.Nanosleep by returning without sleeping.
var FakeOsyield = sys.Osyield(func() {})
FakeOsyield implements sys.Osyield by returning without yielding.
Functions ¶
func CompilerSupported ¶
func CompilerSupported() bool
CompilerSupported includes constraints here and also the assembler.
func CompilerSupports ¶
func CompilerSupports(features api.CoreFeatures) bool
func JCCErratumWorkaroundEnabled ¶ added in v0.4.0
func JCCErratumWorkaroundEnabled() bool
JCCErratumWorkaroundEnabled reports whether compiled code must be laid out to avoid Intel erratum SKX102 (see above). It is the single source of truth for that decision: the amd64 encoder consults it to decide whether to pad before a branch, and the engine consults it to decide whether to align compiled functions to 32 bytes instead of 16 (which is what makes a buffer offset and the final address agree modulo 32 in the first place).
The decision changes the machine code that is generated, so it is also part of the compilation cache key -- it rides CpuFeatures, which fileCacheKey already hashes, so a module compiled on a Skylake host is never loaded back on an Ice Lake one (or the reverse) out of a shared cache directory.
The GOARCH test makes the arm64 (and every other) build a compile-time no-op: arm64 has fixed-width 32-bit instructions and no such erratum, and the amd64 backend package is still compiled there for its cross-architecture tests.
func MakeStack ¶ added in v0.4.0
MakeStack returns a zeroed buffer of at least n bytes for JIT code to use as its stack. Only OpenBSD needs more than a heap allocation; see stack_openbsd.go.
func MapCodeSegmentFromFile ¶ added in v0.4.0
MapCodeSegmentFromFile maps size bytes of f, starting at offset, as read-only executable memory, in place of MmapCodeSegment followed by a copy and MprotectCodeSegment. offset must be a multiple of CodeFileAlignment.
Pages are read from the file on first use and shared, through the page cache, with every process mapping the same file, so code that never runs never becomes resident. The mapping outlives f: the caller may close f, and the file may be renamed over or unlinked, as long as it is not truncated or rewritten in place. Release it with MunmapCodeSegment.
This fails, and the caller must copy the code instead, where a file can't be mapped executable: a noexec mount, or macOS, Windows and platforms without mmap (see mmap_file_unsupported.go).
func MmapCodeSegment ¶
MmapCodeSegment allocates and returns a byte slice to copy executable code into.
See https://man7.org/linux/man-pages/man2/mmap.2.html for mmap API and flags.
func MprotectCodeSegment ¶
MprotectCodeSegment is like unix.Mprotect with RX permission.
func MunmapCodeSegment ¶
MunmapCodeSegment unmaps the given memory region.
func Nanotime ¶
func Nanotime() int64
Nanotime implements sys.Nanotime with runtime.nanotime() if CGO is available and time.Since if not.
func NewFakeNanotime ¶
NewFakeNanotime implements sys.Nanotime that increases by 1ms each reading. See /RATIONALE.md
func NewFakeRandSource ¶
NewFakeRandSource returns a deterministic source of random values.
Construction is lazy: math/rand's generator seeds a 607-int64 lagged-Fibonacci array (~4.9 KB + ~1800 iterations), and this source is created on *every* instantiate that doesn't set WithRandSource -- yet the common guest never reads random bytes at all. Deferring the generator to the first Read makes those instantiations pay nothing while keeping the output byte-identical (same algorithm, same seed) for guests that do read.
func NewFakeWalltime ¶
NewFakeWalltime implements sys.Walltime with FakeEpochNanos that increases by 1ms each reading. See /RATIONALE.md
func ToPosixPath ¶
ToPosixPath returns the input, as only windows might return backslashes.
func Walltime ¶
Walltime implements sys.Walltime with time.Now.
Note: This is only notably less efficient than it could be is reading runtime.walltime(). time.Now defensively reads nanotime also, just in case time.Since is used. This doubles the performance impact. However, wall time is likely to be read less frequently than Nanotime. Also, doubling the cost matters less on fast platforms that can return both in <=100ns.
Types ¶
type CpuFeatureFlags ¶
type CpuFeatureFlags uint64
CpuFeatureFlags exposes methods for querying CPU capabilities.
The flags describe everything about the host CPU that changes the machine code the compiler emits -- capabilities it may use, and errata it must work around. Raw is folded into the compilation cache key (see fileCacheKey), so anything recorded here is automatically prevented from leaking across a heterogeneous fleet sharing one cache directory.
const ( // CpuFeatureArm64Atomic is the flag to query CpuFeatureFlags.Has for Large System Extensions capabilities on arm64 CpuFeatureArm64Atomic CpuFeatureFlags = 1 << iota )
func (CpuFeatureFlags) Has ¶
func (c CpuFeatureFlags) Has(f CpuFeatureFlags) bool
func (CpuFeatureFlags) Raw ¶
func (c CpuFeatureFlags) Raw() uint64