Conghao97's picture tcftrees's picture
ProgramGen release
d086025
|
Raw History Blame Contribute Delete
3.41 kB

go-linux-lowlevel-hw — Usage Documentation

Cleanroom usage docs extracted from the repository README of 9elements/go-linux-lowlevel-hw.

go-linux-lowlevel-hw provides low level access to common hardware on UNIX-like platforms.

Description

This package provides low level access to certain hardware typically found on modern x86 PCs. Some information is only available when run as the most privileged user. Thus this library is to be used in preproduction and testing environments with relaxed kernel security.

Be warned, you could brick your system.

How to use this library

package main

import (
    "github.com/9elements/go-linux-lowlevel-hw/pkg/hwapi"
)

func main() {
    h := hwapi.GetAPI()

    //...
}

Import path: github.com/9elements/go-linux-lowlevel-hw/pkg/hwapi Entry point: hwapi.GetAPI() returns the API interface described below.

Interfaces

The GetAPI() call returns an interface providing the following methods (public API reference):

    // cpuid
    VersionString() string
    HasSMX() bool
    HasVMX() bool
    HasMTRR() bool
    ProcessorBrandName() string
    CPUSignature() uint32
    CPULogCount() uint32

    // e820
    IsReservedInE820(start uint64, end uint64) (bool, error)

    // iommu
    LookupIOAddress(addr uint64, regs VTdRegisters) ([]uint64, error)
    AddressRangesIsDMAProtected(first, end uint64) (bool, error)

    // msr
    ReadMSR(msr int64) (uint64, error)
    ReadMSRAllCores(msr int64) (uint64, error)

    // msr_intel
    HasSMRR() (bool, error)
    GetSMRRInfo() (SMRR, error)
    IA32FeatureControlIsLocked() (bool, error)
    IA32PlatformID() (uint64, error)
    AllowsVMXInSMX() (bool, error)
    TXTLeavesAreEnabled() (bool, error)
    IA32DebugInterfaceEnabledOrLocked() (*IA32Debug, error)

    // pci
    PCIEnumerateVisibleDevices(cb func(d PCIDevice) (abort bool)) (err error)
    PCIReadConfig8(d PCIDevice, off int) (uint8, error)
    PCIReadConfig16(d PCIDevice, off int) (uint16, error)
    PCIReadConfig32(d PCIDevice, off int) (uint32, error)
    PCIWriteConfig8(d PCIDevice, off int, val uint8) error
    PCIWriteConfig16(d PCIDevice, off int, val uint16) error
    PCIWriteConfig32(d PCIDevice, off int, val uint32) error
    PCIReadVendorID(d PCIDevice) (uint16, error)
    PCIReadDeviceID(d PCIDevice) (uint16, error)

    // hostbridge
    ReadHostBridgeTseg() (uint32, uint32, error)
    ReadHostBridgeDPR() (DMAProtectedRange, error)

    // phys
    ReadPhys(addr int64, data UintN) error
    ReadPhysBuf(addr int64, buf []byte) error
    WritePhys(addr int64, data UintN) error

    // tpm
    NewTPM() (*TPM, error)
    NVLocked(tpmCon *TPM) (bool, error)
    ReadNVPublic(tpmCon *TPM, index uint32) ([]byte, error)
    NVReadValue(tpmCon *TPM, index uint32, password string, size, offhandle uint32) ([]byte, error)
    ReadPCR(tpmCon *TPM, pcr uint32) ([]byte, error)

    // acpi
    GetACPITableDevMem(n string) ([]byte, error)
    GetACPITableSysFS(n string) ([]byte, error)

    // smbios
    IterateOverSMBIOSTables(n uint8, callback func(s *smbios.Structure) bool) (ret bool, err error)
    IterateOverSMBIOSTablesType0(callback func(t0 *SMBIOSType0) bool) (ret bool, err error)

Requirements / notes

  • Go module: github.com/9elements/go-linux-lowlevel-hw (see go.mod in the repository for the exact Go version and dependencies).
  • Many operations (MSR reads, physical memory access, PCI config space, TPM access) require root or otherwise elevated privileges on Linux.
  • Intended for preproduction and testing environments only.