kexec

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: BSD-3-Clause Imports: 21 Imported by: 60

Documentation

Overview

Package kexec implements kexec load and file load syscall API.

Index

Constants

View Source
const M1 = 1 << 20

M1 is 1 Megabyte in bits.

View Source
const MaxAddr = ^uintptr(0)

MaxAddr is the highest address in a 64bit address space.

Variables

View Source
var ErrNotEnoughSpace = fmt.Errorf("not enough space to allocate bytes")

ErrNotEnoughSpace is returned by the FindSpace family of functions if no range is large enough to accommodate the request.

Functions

func FileLoad

func FileLoad(kernel, ramfs *os.File, cmdline string) error

FileLoad loads the given kernel as the new kernel with the given ramfs and cmdline.

The kexec_file_load(2) syscall is x86-64 and arm64 only.

func Load

func Load(entry uintptr, segments Segments, flags uint64) error

Load loads the given segments into memory to be executed on a kexec-reboot.

It is assumed that segments is made up of the next kernel's code and text segments, and that `entry` is the entry point, either kernel entry point or trampoline.

Load will align segments to page boundaries and deduplicate overlapping ranges.

func Reboot

func Reboot() error

Reboot executes a kernel previously loaded with FileInit.

func SegmentEqual added in v0.13.0

func SegmentEqual(s, t Segment) bool

SegmentEqual returns whether s and t point at the same physical region and contain the same data.

func SegmentsEqual added in v0.13.0

func SegmentsEqual(s, t Segments) bool

SegmentsEqual returns whether the contents of all segments are the same, while pointing to the same physical memory region.

Types

type ErrKexec

type ErrKexec struct {
	Entry    uintptr
	Segments []Segment
	Flags    uint64
	Errno    syscall.Errno
}

ErrKexec is returned by Load if the kexec failed. It describes entry point, flags, errno and kernel layout.

func (ErrKexec) Error

func (e ErrKexec) Error() string

Error implements error.

type FindOptioner added in v0.13.0

type FindOptioner func(o *findSpaceOptions)

FindOptioner is a config option for FindSpace.

func WithAlignment added in v0.13.0

func WithAlignment(alignSize uint) FindOptioner

WithAlignment requires FindSpace to return a range with an address and size aligned to alignSize.

func WithMinimumAddr added in v0.13.0

func WithMinimumAddr(minAddr uintptr) FindOptioner

WithMinimumAddr requires FindSpace to return a range with an address above minAddr.

func WithStartAlignment added in v0.13.0

func WithStartAlignment(alignSize uint) FindOptioner

WithStartAlignment requires FindSpace to return a range with an address aligned to alignSize.

func WithinRange added in v0.13.0

func WithinRange(limit Range) FindOptioner

WithinRange requires FindSpace to return a range within the limit.

type Memory

type Memory struct {
	// Phys defines the layout of physical memory.
	//
	// Phys is used to tell loaded operating systems what memory is usable
	// as RAM, and what memory is reserved (for ACPI or other reasons).
	Phys MemoryMap

	// Segments are the segments used to load a new operating system.
	//
	// Each segment also contains a physical memory region it maps to.
	Segments Segments
}

Memory provides routines to work with physical memory ranges.

func (*Memory) AddKexecSegment

func (m *Memory) AddKexecSegment(d []byte) (Range, error)

AddKexecSegment adds d to a new kexec segment

func (*Memory) AddKexecSegmentExplicit added in v0.9.0

func (m *Memory) AddKexecSegmentExplicit(d []byte, sz, offset, alignSizeBytes uint) (Range, error)

AddKexecSegmentExplicit adds d to a new kexec segment, but allows asking for extra space, secifying alignment size, and setting text_offset.

func (*Memory) AddPhysSegment

func (m *Memory) AddPhysSegment(d []byte, limit Range) (Range, error)

AddPhysSegment reserves len(d) bytes in the physical memmap within limit and adds a kexec segment with d in that range.

func (Memory) AvailableRAM

func (m Memory) AvailableRAM() Ranges

AvailableRAM returns page-aligned unused regions of RAM.

AvailableRAM takes all RAM-marked pages in the memory map and subtracts the kexec segments already allocated. RAM segments begin at a page boundary.

E.g if page size is 4K and RAM segments are

[{start:0 size:8192} {start:8192 size:8000}]

and kexec segments are

[{start:40 size:50} {start:8000 size:2000}]

result should be

[{start:0 size:40} {start:4096 end:8000 - 4096}]

func (Memory) FindSpace

func (m Memory) FindSpace(sz, alignSizeBytes uint) (Range, error)

FindSpace returns pointer to the physical memory, where array of size sz can be stored during next AddKexecSegment call.

Align up to at least a page size if alignSizeBytes is smaller.

func (*Memory) LoadElfSegments

func (m *Memory) LoadElfSegments(r io.ReaderAt) (Object, error)

LoadElfSegments loads loadable ELF segments.

func (*Memory) ReservePhys

func (m *Memory) ReservePhys(sz uint, limit Range) (Range, error)

ReservePhys reserves page-aligned sz bytes in the physical memmap within the given limit address range.

type MemoryMap

type MemoryMap []TypedRange

MemoryMap defines the layout of physical memory.

MemoryMap defines which ranges in memory are usable RAM and which are reserved for various reasons.

func MemoryMapFromFDT added in v0.13.0

func MemoryMapFromFDT(fdt *dt.FDT) (MemoryMap, error)

MemoryMapFromFDT reads firmware provided memory map from an FDT.

func MemoryMapFromIOMem added in v0.13.0

func MemoryMapFromIOMem() (MemoryMap, error)

MemoryMapFromIOMem reads the kernel-maintained memory map from /proc/iomem.

func MemoryMapFromMemblock added in v0.13.0

func MemoryMapFromMemblock() (MemoryMap, error)

MemoryMapFromMemblock reads a kernel-maintained memory map from /sys/kernel/debug/memblock.

memblock is only available on kernels with CONFIG_ARCH_KEEP_MEMBLOCK (and debugfs). Without it, the kernel only maintains memblock early during init as its memory allocation mechanism.

func MemoryMapFromSysfsMemmap added in v0.13.0

func MemoryMapFromSysfsMemmap() (MemoryMap, error)

MemoryMapFromSysfsMemmap reads a firmware-provided memory map from /sys/firmware/memmap.

Linux support for this exists only on X86 at the time of this commit.

func (MemoryMap) FilterByType

func (mm MemoryMap) FilterByType(typ RangeType) Ranges

FilterByType only returns ranges of the given typ.

func (*MemoryMap) Insert

func (mm *MemoryMap) Insert(r TypedRange)

Insert a new TypedRange into the memory map, removing chunks of other ranges as necessary.

Assumes that TypedRange is a valid range -- no checking.

func (MemoryMap) RAM added in v0.13.0

func (mm MemoryMap) RAM() Ranges

RAM is an alias for FilterByType(RangeRAM) and returns unreserved physical memory in the memory map.

func (MemoryMap) String added in v0.13.0

func (mm MemoryMap) String() string

func (MemoryMap) ToUEFIPayloadMemoryMap added in v0.13.0

func (mm MemoryMap) ToUEFIPayloadMemoryMap() UEFIPayloadMemoryMap

ToUEFIPayloadMemoryMap converts MemoryMap to a UEFI payload memory map.

type Object added in v0.9.0

type Object interface {
	Progs() []*elf.Prog
	Entry() uint64
}

Object is an object file, specific to kexec uses. It is used to get Progs and Entry from an object. elf.Prog is generic enough that we can use it to define loadable segments for non-ELF objects, such as Plan 9 a.out

func ObjectNewFile added in v0.9.0

func ObjectNewFile(r io.ReaderAt) (Object, error)

ObjectNewFile reads from input stream and returns a specific object. It tries elf first, then plan9.

type Range

type Range struct {
	// Start is the inclusive start of the range.
	Start uintptr

	// Size is the number of elements in the range.
	//
	// Start+Size is the exclusive end of the range.
	Size uint
}

Range represents a contiguous uintptr interval [Start, Start+Size).

func RangeFromInclusiveInterval added in v0.13.0

func RangeFromInclusiveInterval(start, last uintptr) Range

RangeFromInclusiveInterval returns a Range representing [start, last].

func RangeFromInterval

func RangeFromInterval(start, end uintptr) Range

RangeFromInterval returns a Range representing [start, end).

func (Range) Adjacent

func (r Range) Adjacent(r2 Range) bool

Adjacent returns true if r and r2 do not overlap, but are immediately next to each other.

func (Range) Align added in v0.13.0

func (r Range) Align(alignSize uint) Range

Align aligns start and size by the given alignSize.

The resulting range is guaranteed to be superset of r.

func (Range) AlignPage added in v0.13.0

func (r Range) AlignPage() Range

AlignPage aligns start and size by page size.

The resulting range is guaranteed to be superset of r.

func (Range) Contains

func (r Range) Contains(p uintptr) bool

Contains returns true iff p is in the interval described by r.

func (Range) Disjunct

func (r Range) Disjunct(r2 Range) bool

Disjunct returns true if r and r2 do not overlap.

func (Range) End

func (r Range) End() uintptr

End returns the uintptr *after* the end of the interval.

func (Range) Intersect

func (r Range) Intersect(r2 Range) *Range

Intersect returns the continuous range of points common to r and r2 if there is one.

func (Range) IsSupersetOf

func (r Range) IsSupersetOf(r2 Range) bool

IsSupersetOf returns true if r2 in r.

func (Range) Last added in v0.13.0

func (r Range) Last() uintptr

Last returns last uintptr inside the interval.

func (Range) Minus

func (r Range) Minus(r2 Range) []Range

Minus removes all points in r2 from r.

func (Range) Overlaps

func (r Range) Overlaps(r2 Range) bool

Overlaps returns true if r and r2 overlap.

func (Range) String

func (r Range) String() string

String returns [Start, Start+Size) as a string.

func (Range) WithStart added in v0.13.0

func (r Range) WithStart(start uintptr) Range

WithStart returns a range that begins at start and ends at r.End().

type RangeType

type RangeType string

RangeType defines type of a TypedRange based on the Linux kernel string provided by firmware memory map.

const (
	RangeRAM      RangeType = "System RAM"
	RangeDefault  RangeType = "Default"
	RangeACPI     RangeType = "ACPI Tables"
	RangeNVS      RangeType = "ACPI Non-volatile Storage"
	RangeReserved RangeType = "Reserved"
)

These are the range types we know Linux uses.

func (RangeType) String

func (r RangeType) String() string

String implements fmt.Stringer.

type Ranges

type Ranges []Range

Ranges is a list of non-overlapping ranges.

func (Ranges) FindSpace

func (rs Ranges) FindSpace(sz uint, opts ...FindOptioner) (Range, error)

FindSpace finds a continuous piece of sz points within Ranges and the given options and returns the Range pointing to it.

func (Ranges) FindSpaceAbove

func (rs Ranges) FindSpaceAbove(sz uint, minAddr uintptr) (space Range, err error)

FindSpaceAbove finds a continuous piece of sz points within Ranges and returns a space.Start >= minAddr.

func (Ranges) FindSpaceIn

func (rs Ranges) FindSpaceIn(sz uint, limit Range) (space Range, err error)

FindSpaceIn finds a continuous piece of sz points within Ranges and returns a Range where space.Start >= limit.Start, with space.End() < limit.End().

func (Ranges) Minus

func (rs Ranges) Minus(r Range) Ranges

Minus removes all points in r from all ranges in rs.

func (Ranges) Sort

func (rs Ranges) Sort()

Sort sorts ranges by their start point.

type Segment

type Segment struct {
	// Buf is a buffer to map to Phys in kexec.
	Buf []byte

	// Phys is a physical address of kernel.
	Phys Range
}

Segment defines kernel memory layout.

func AlignPhysStart

func AlignPhysStart(s Segment) Segment

AlignPhysStart aligns s.Phys.Start to the page size. AlignPhysStart does not align the size of the segment.

func NewSegment

func NewSegment(buf []byte, phys Range) Segment

NewSegment creates new Segment. Segments should be created using NewSegment method to prevent data pointed by Segment.Buf to be collected by garbage collector.

func (Segment) String

func (s Segment) String() string

type Segments

type Segments []Segment

Segments is a collection of segments.

func AlignAndMerge

func AlignAndMerge(segs Segments) (Segments, error)

AlignAndMerge adjusts segs to the preconditions of kexec_load.

Pre-conditions: segs physical ranges are disjoint. Post-conditions: segs physical start addresses & size aligned to page size.

func (Segments) GetPhys

func (segs Segments) GetPhys(r Range) []byte

GetPhys gets the buffer corresponding to the physical address range r.

func (*Segments) Insert

func (segs *Segments) Insert(s Segment)

Insert inserts s assuming it does not overlap with an existing segment.

func (Segments) IsSupersetOf

func (segs Segments) IsSupersetOf(o Segments) error

IsSupersetOf checks whether all segments in o are present in s and contain the same buffer content.

func (Segments) Phys

func (segs Segments) Phys() Ranges

Phys returns all physical address ranges.

func (Segments) PhysContains

func (segs Segments) PhysContains(p uintptr) bool

PhysContains returns whether p exists in any of segs' physical memory ranges.

func (Segments) String added in v0.13.0

func (segs Segments) String() string

type TypedRange

type TypedRange struct {
	Range
	Type RangeType
}

TypedRange represents range of physical memory.

func (TypedRange) String

func (tr TypedRange) String() string

type UEFIPayloadMemType added in v0.13.0

type UEFIPayloadMemType uint32

UEFIPayloadMemType are types used with LinuxBoot UEFI payload memory maps.

const (
	UEFIPayloadTypeRAM      UEFIPayloadMemType = 1
	UEFIPayloadTypeDefault  UEFIPayloadMemType = 2
	UEFIPayloadTypeACPI     UEFIPayloadMemType = 3
	UEFIPayloadTypeNVS      UEFIPayloadMemType = 4
	UEFIPayloadTypeReserved UEFIPayloadMemType = 5
)

Payload memory type (PayloadMemType) in UEFI payload.

type UEFIPayloadMemoryMap added in v0.13.0

type UEFIPayloadMemoryMap []UEFIPayloadMemoryMapEntry

UEFIPayloadMemoryMap is a memory map used with LinuxBoot's UEFI payload.

type UEFIPayloadMemoryMapEntry added in v0.13.0

type UEFIPayloadMemoryMapEntry struct {
	Start uint64
	End   uint64
	Type  UEFIPayloadMemType
}

UEFIPayloadMemoryMapEntry represent a memory map entry for a LinuxBoot UEFI payload.

Jump to

Keyboard shortcuts

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