mirror of
https://github.com/taigrr/gopher-os
synced 2025-01-18 04:43:13 -08:00
Since the actual size of each memory entry is not known in advance (bootloaders may append additional information to it) but needs to be queried off the memory map tag header we cannot reserve space for it as no memory allocation is yet available. Instead, a visitor pattern was implemented to allow the memory manager initialization block to easily mark the appropriate pages as reserved
189 lines
7.2 KiB
Go
189 lines
7.2 KiB
Go
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)
|
|
}
|
|
}
|
|
|
|
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, 85, 66, 32, 50, 46, 48, 50, 126, 98, 101, 116, 97, 50, 45,
|
|
57,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0, 0, 207, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 4, 28, 0, 184, 157, 10, 0, 252, 112, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 220, 0, 0, 0,
|
|
1, 0, 0, 0, 0, 0, 0, 0, 252, 116, 28, 0, 180, 14, 11, 0,
|
|
16, 0, 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,
|
|
}
|
|
)
|