cpu

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2025 License: BSD-3-Clause Imports: 5 Imported by: 2

Documentation

Overview

Package cpu provides helpers to share memory between the CPU and other hardware components.

To share memory managed by the Go runtime, the following requirements must be met:

  • Cache: Cached values must be written or invalidated before memory is handed over to another component.
  • Alignment: Most components require stricter memory alignment than Go has.
  • Memory Location: The shared memory must be allocated on the heap, since the stack might move if it has to grow.
  • Memory Layout: If sharing a pointer to a struct, include structs.HostLayout in the type definition. Go itself makes no guarantees regarding a structs memory layout.

All cache operations in this package refer to the data cache. Instruction cache won't be affected.

Index

Constants

View Source
const (
	KSEG0 uintptr = 0xffffffff_80000000 // unmapped, cached
	KSEG1 uintptr = 0xffffffff_a0000000 // unmapped, uncached
)

Memory regions in 32bit Kernel mode

View Source
const CacheLineSize = 16
View Source
const ClockSpeed = 93.75e6

The CPU's clock speed

Variables

This section is empty.

Functions

func CopyPaddedSlice

func CopyPaddedSlice[T Paddable](slice []T) []T

Ensure a slice is padded. Might copy the slice if necessary

func InvalidateSlice

func InvalidateSlice[T Paddable](buf []T)

Causes the cache to be read from RAM before next access. Call this before the address range is to be written by another component. If the specified address is currently not cached, this is a no-op.

func IsPadded

func IsPadded[T Paddable](p []T) bool

Returns true if p is safe for cache ops, i.e. padded and aligned to cache.

func MMIO

func MMIO[T any](ptr Addr) *T

MMIO returns a pointer to an object that is memory mapped.

func MakePaddedSlice

func MakePaddedSlice[T Paddable](size int) []T

A slice that is safe for cache ops. It's start is aligned to CacheLineSize and the end is padded to fill the cache line. Note that using append() might corrupt the padding. Aligning the slice start to CacheLineSize has the advantage that runtime validation is possible, see IsPadded().

func MakePaddedSliceAligned

func MakePaddedSliceAligned[T Paddable](size int, align uintptr) []T

Same as MakePaddedSlice with extra alignment requirements.

func PadSlice

func PadSlice[T Paddable](buf []T) ([]T, int, int)

Add padding to a given slice by shrinking it. Returns the number of discarded elements at the beginnning of the slice as second return value.

func Pads

func Pads[T Paddable](buf []T) (int, int)

Returns the size of necessary cacheline pads to get a padded slice, i.e. the slice buf[head:tail] is the part of buf which is safe for cache ops.

func PinSlice added in v0.1.1

func PinSlice[T any](p *Pinner, slice []T)

func Uncached

func Uncached[T any](p *T) *T

Uncached returns a pointer to p with caching disabled. The returned pointer is only valid as long as p exists, as it doesn't prevent the object from being garbage collected.

func UncachedSlice

func UncachedSlice[T any](s []T) []T

UncachedSlice returns a new slice with the same underlying data as s, with caching disabled. The returned slice is only valid as long as s exists, as it doesn't prevent the underlying array from being garbage collected.

func WritebackSlice

func WritebackSlice[T Paddable](buf []T)

Causes the cache to be written back to RAM. Call this before requesting another component to read from this address range. If the specified address is currently not cached, this is a no-op.

Types

type Addr

type Addr uint32

Addr represents a physical memory address

func PAddr

func PAddr(addr uintptr) Addr

PAddr returns the physical address of a virtual address.

func PhysicalAddress

func PhysicalAddress[T Pointer[Q], Q any](s T) Addr

PhysicalAddress returns the physical address of a pointer.

func PhysicalAddressSlice

func PhysicalAddressSlice[T any](s []T) Addr

Same as PhysicalAddress but for slices.

type Align16

type Align16 = [16]byte

type Align64

type Align64 = [64]byte

type Alignment

type Alignment interface {
	Align16 | Align64
}

type CacheLinePad

type CacheLinePad struct {
	// contains filtered or unexported fields
}

Cache operations always affect a whole cache line. To avoid invalidating unrelated data in a cache line, pad structs with CacheLinePad at the beginning and end.

type Cached

type Cached interface {
	// Causes the cache to be written back to RAM. Call this before
	// requesting another component to read from this address range. If the
	// specified address is currently not cached, this is a no-op.
	Writeback()

	// Causes the cache to be read from RAM before next access. Call this
	// before the address range is to be written by another component. If
	// the specified address is currently not cached, this is a no-op.
	Invalidate()
}

Cached is a datatype that provides cache operations.

type Paddable

type Paddable interface {
	~uint8 | ~uint16 | ~uint32 | ~uint64 | ~int8 | ~int16 | ~int32 | ~int64
}

Only types with CacheLineSize%unsafe.Sizeof(T) == 0

type Padded

type Padded[T any, A Alignment] struct {
	// contains filtered or unexported fields
}

Padded embeds T with cacheline pads around it to make it safe for cache operations. It also guarantees the specified memory alignment for T.

T must not hold any heap pointers.

func NewPadded

func NewPadded[T any, A Alignment]() *Padded[T, A]

func (*Padded[T, A]) Invalidate

func (p *Padded[T, A]) Invalidate()

Invalidate implements the Cached interface.

func (*Padded[T, A]) Value

func (p *Padded[T, A]) Value() *T

Value returns the padded value.

func (*Padded[T, A]) Writeback

func (p *Padded[T, A]) Writeback()

Writeback implements the Cached interface.

type Pinner added in v0.1.1

type Pinner struct {
	// contains filtered or unexported fields
}

Pinner is a lightweight version of runtime.Pinner. In contrast to runtime.Pinner it's not compatible with cgocheck=1.

func (*Pinner) Pin added in v0.1.1

func (p *Pinner) Pin(pointer any)

func (*Pinner) Unpin added in v0.1.1

func (p *Pinner) Unpin()

type Pointer

type Pointer[T any] interface{ *T }

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL