1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

Move multiboot package under the hal package

This commit is contained in:
Achilleas Anagnostopoulos
2017-03-30 07:34:00 +01:00
parent 04c3987219
commit 80f7980e74
4 changed files with 1 additions and 1 deletions

0
kernel/hal/hal.go Normal file
View File

View File

@@ -0,0 +1,192 @@
package multiboot
import "unsafe"
type tagType uint32
const (
tagMbSectionEnd tagType = iota
tagBootCmdLine
tagBootLoaderName
tagModules
tagBasicMemoryInfo
tagBiosBootDevice
tagMemoryMap
tagVbeInfo
tagFramebufferInfo
tagElfSymbols
tagApmTable
)
// info describes the multiboot info section header.
type info struct {
// Total size of multiboot info section.
totalSize uint32
// Always set to zero; reserved for future use
reserved uint32
}
// tagHeader describes the header the preceedes each tag.
type tagHeader struct {
// The type of the tag
tagType tagType
// The size of the tag including the header but *not* including any
// padding. According to the spec, each tag starts at a 8-byte aligned
// address.
size uint32
}
// mmapHeader describes the header for a memory map specification.
type mmapHeader struct {
// The size of each entry.
entrySize uint32
// The version of the entries that follow.
entryVersion uint32
}
// FramebufferType defines the type of the initialized framebuffer.
type FramebufferType uint8
const (
// FrameBufferTypeIndexed specifies a 256-color palette.
FrameBufferTypeIndexed FramebufferType = iota
// FramebufferTypeRGB specifies direct RGB mode.
FramebufferTypeRGB
// FramebufferTypeEGA specifies EGA text mode.
FramebufferTypeEGA
)
// FramebufferInfo provides information about the initialized framebuffer.
type FramebufferInfo struct {
// The framebuffer physical address.
PhysAddr uint64
// Row pitch in bytes.
Pitch uint32
// Width and height in pixels (or characters if Type = FramebufferTypeEGA)
Width, Height uint32
// Bits per pixel (non EGA modes only).
Bpp uint8
// Framebuffer type.
Type FramebufferType
}
// MemoryEntryType defines the type of a MemoryMapEntry.
type MemoryEntryType uint32
const (
// MemAvailable indicates that the memory region is available for use.
MemAvailable MemoryEntryType = iota + 1
// MemReserved indicates that the memory region is not available for use.
MemReserved
// MemAcpiReclaimable indicates a memory region that holds ACPI info that
// can be reused by the OS.
MemAcpiReclaimable
// MemNvs indicates memory that must be preserved when hibernating.
MemNvs
// Any value >= memUnknown will be mapped to MemReserved.
memUnknown
)
/// MemoryMapEntry describes a memory region entry, namely its physical address,
// its length and its type.
type MemoryMapEntry struct {
// The physical address for this memory region.
PhysAddress uint64
// The length of the memory region.
Length uint64
// The type of this entry.
Type MemoryEntryType
}
var (
infoData uintptr
)
// MemRegionVisitor defies a visitor function that gets invoked by VisitMemRegions
// for each memory region provided by the boot loader.
type MemRegionVisitor func(entry *MemoryMapEntry)
// SetInfoPtr updates the internal multiboot information pointer to the given
// value. This function must be invoked before invoking any other function
// exported by this package.
func SetInfoPtr(ptr uintptr) {
infoData = ptr
}
// VisitMemRegions will invoke the supplied visitor for each memory region that
// is defined by the multiboot info data that we received from the bootloader.
func VisitMemRegions(visitor MemRegionVisitor) {
curPtr, size := findTagByType(tagMemoryMap)
if size == 0 {
return
}
// curPtr points to the memory map header (2 dwords long)
ptrMapHeader := (*mmapHeader)(unsafe.Pointer(curPtr))
endPtr := curPtr + uintptr(size)
curPtr += 8
var entry *MemoryMapEntry
for curPtr != endPtr {
entry = (*MemoryMapEntry)(unsafe.Pointer(curPtr))
// Mark unknown entry types as reserved
if entry.Type == 0 || entry.Type > memUnknown {
entry.Type = MemReserved
}
visitor(entry)
curPtr += uintptr(ptrMapHeader.entrySize)
}
}
// GetFramebufferInfo returns information about the framebuffer initialized by the
// bootloader. This function returns nil if no framebuffer info is available.
func GetFramebufferInfo() *FramebufferInfo {
var info *FramebufferInfo
curPtr, size := findTagByType(tagFramebufferInfo)
if size != 0 {
info = (*FramebufferInfo)(unsafe.Pointer(curPtr))
}
return info
}
// findTagByType scans the multiboot info data looking for the start of of the
// specified type. It returns a pointer to the tag contents start offset and
// the content length exluding the tag header.
//
// If the tag is not present in the multiboot info, findTagSection will return
// back (0,0).
func findTagByType(tagType tagType) (uintptr, uint32) {
var ptrTagHeader *tagHeader
curPtr := infoData + 8
for ptrTagHeader = (*tagHeader)(unsafe.Pointer(curPtr)); ptrTagHeader.tagType != tagMbSectionEnd; ptrTagHeader = (*tagHeader)(unsafe.Pointer(curPtr)) {
if ptrTagHeader.tagType == tagType {
return curPtr + 8, ptrTagHeader.size - 8
}
// Tags are aligned at 8-byte aligned addresses
curPtr += uintptr(int32(ptrTagHeader.size+7) & ^7)
}
return 0, 0
}

View File

@@ -0,0 +1,215 @@
package multiboot
import (
"testing"
"unsafe"
)
func TestFindTagByType(t *testing.T) {
specs := []struct {
tagType tagType
expSize uint32
}{
{tagBootCmdLine, 1},
{tagBootLoaderName, 27},
{tagBasicMemoryInfo, 8},
{tagBiosBootDevice, 12},
{tagMemoryMap, 152},
{tagFramebufferInfo, 24},
{tagElfSymbols, 972},
{tagApmTable, 20},
}
SetInfoPtr(uintptr(unsafe.Pointer(&multibootInfoTestData[0])))
for specIndex, spec := range specs {
_, size := findTagByType(spec.tagType)
if size != spec.expSize {
t.Errorf("[spec %d] expected tag size for tag type %d to be %d; got %d", specIndex, spec.tagType, spec.expSize, size)
}
}
}
func TestFindTagByTypeWithMissingTag(t *testing.T) {
SetInfoPtr(uintptr(unsafe.Pointer(&multibootInfoTestData[0])))
if offset, size := findTagByType(tagModules); offset != 0 || size != 0 {
t.Fatalf("expected findTagByType to return (0,0) for missing tag; got (%d, %d)", offset, size)
}
}
func TestVisitMemRegion(t *testing.T) {
specs := []struct {
expPhys uint64
expLen uint64
expType MemoryEntryType
}{
// This region type is actually MemAvailable but we patch it to
// a bogus value to test whether it gets flagged as reserved
{0, 654336, MemReserved},
{654336, 1024, MemReserved},
{983040, 65536, MemReserved},
{1048576, 133038080, MemAvailable},
{134086656, 131072, MemReserved},
{4294705152, 262144, MemReserved},
}
var visitCount int
SetInfoPtr(uintptr(unsafe.Pointer(&emptyInfoData[0])))
VisitMemRegions(func(_ *MemoryMapEntry) {
visitCount++
})
if visitCount != 0 {
t.Fatal("expected visitor not to be invoked when no memory map tag is present")
}
// Set a bogus type for the first entry in the map
SetInfoPtr(uintptr(unsafe.Pointer(&multibootInfoTestData[0])))
multibootInfoTestData[128] = 0xFF
VisitMemRegions(func(entry *MemoryMapEntry) {
if entry.PhysAddress != specs[visitCount].expPhys {
t.Errorf("[visit %d] expected physical address to be %x; got %x", visitCount, specs[visitCount].expPhys, entry.PhysAddress)
}
if entry.Length != specs[visitCount].expLen {
t.Errorf("[visit %d] expected region len to be %x; got %x", visitCount, specs[visitCount].expLen, entry.Length)
}
if entry.Type != specs[visitCount].expType {
t.Errorf("[visit %d] expected region type to be %d; got %d", visitCount, specs[visitCount].expType, entry.Type)
}
visitCount++
})
if visitCount != len(specs) {
t.Errorf("expected the visitor func to be invoked %d times; got %d", len(specs), visitCount)
}
}
func TestGetFramebufferInfo(t *testing.T) {
SetInfoPtr(uintptr(unsafe.Pointer(&emptyInfoData[0])))
if GetFramebufferInfo() != nil {
t.Fatalf("expected GetFramebufferInfo() to return nil when no framebuffer tag is present")
}
SetInfoPtr(uintptr(unsafe.Pointer(&multibootInfoTestData[0])))
fbInfo := GetFramebufferInfo()
if fbInfo.Type != FramebufferTypeEGA {
t.Errorf("expected framebuffer type to be %d; got %d", FramebufferTypeEGA, fbInfo.Type)
}
if fbInfo.PhysAddr != 0xB8000 {
t.Errorf("expected physical address for EGA text mode to be 0xB8000; got %x", fbInfo.PhysAddr)
}
if fbInfo.Width != 80 || fbInfo.Height != 25 {
t.Errorf("expected framebuffer dimensions to be 80x25; got %dx%d", fbInfo.Width, fbInfo.Height)
}
if fbInfo.Pitch != 160 {
t.Errorf("expected pitch to be 160; got %x", fbInfo.Pitch)
}
}
var (
emptyInfoData = []byte{
0, 0, 0, 0, // size
0, 0, 0, 0, // reserved
0, 0, 0, 0, // tag with type zero and length zero
0, 0, 0, 0,
}
// A dump of multiboot data when running under qemu.
multibootInfoTestData = []byte{
72, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 9, 0, 0, 0,
0, 171, 253, 7, 118, 119, 123, 0, 2, 0, 0, 0, 35, 0, 0, 0,
71, 82,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 231, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
12, 117, 28, 0, 196, 14, 11, 0, 239, 79, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0,
3, 0, 0, 0, 0, 0, 0, 0, 251, 196, 28, 0, 179, 94, 11, 0,
247, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
244, 197, 28, 0, 108, 99, 11, 0, 80, 77, 0, 0, 23, 0, 0, 0,
210, 4, 0, 0, 4, 0, 0, 0, 16, 0, 0, 0, 9, 0, 0, 0,
3, 0, 0, 0, 0, 0, 0, 0, 68, 19, 29, 0, 188, 176, 11, 0,
107, 104, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 16, 0, 0, 0,
127, 2, 0, 0, 128, 251, 1, 0, 5, 0, 0, 0, 20, 0, 0, 0,
224, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0,
8, 0, 0, 0, 32, 0, 0, 0, 0, 128, 11, 0, 0, 0, 0, 0,
160, 0, 0, 0, 80, 0, 0, 0, 25, 0, 0, 0, 16, 2, 0, 0,
14, 0, 0, 0, 28, 0, 0, 0, 82, 83, 68, 32, 80, 84, 82, 32,
89, 66, 79, 67, 72, 83, 32, 0, 220, 24, 254, 7, 0, 0, 0, 0,
0, 0, 0, 0, 8, 0, 0, 0,
}
)