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
- func CopyPaddedSlice[T Paddable](slice []T) []T
- func InvalidateSlice[T Paddable](buf []T)
- func IsPadded[T Paddable](p []T) bool
- func MMIO[T any](ptr Addr) *T
- func MakePaddedSlice[T Paddable](size int) []T
- func MakePaddedSliceAligned[T Paddable](size int, align uintptr) []T
- func PadSlice[T Paddable](buf []T) ([]T, int, int)
- func Pads[T Paddable](buf []T) (int, int)
- func PinSlice[T any](p *Pinner, slice []T)
- func Uncached[T any](p *T) *T
- func UncachedSlice[T any](s []T) []T
- func WritebackSlice[T Paddable](buf []T)
- type Addr
- type Align16
- type Align64
- type Alignment
- type CacheLinePad
- type Cached
- type Paddable
- type Padded
- type Pinner
- type Pointer
Constants ¶
const ( KSEG0 uintptr = 0xffffffff_80000000 // unmapped, cached KSEG1 uintptr = 0xffffffff_a0000000 // unmapped, uncached )
Memory regions in 32bit Kernel mode
const CacheLineSize = 16
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 MakePaddedSlice ¶
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 ¶
Same as MakePaddedSlice with extra alignment requirements.
func PadSlice ¶
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 ¶
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 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 PhysicalAddress ¶
PhysicalAddress returns the physical address of a pointer.
func PhysicalAddressSlice ¶
Same as PhysicalAddress but for slices.
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 Padded ¶
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 (*Padded[T, A]) Invalidate ¶
func (p *Padded[T, A]) Invalidate()
Invalidate implements the Cached interface.