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

Initialize Go memory allocator

This commit is contained in:
Achilleas Anagnostopoulos 2017-06-30 08:04:10 +01:00
parent b4f4a9a738
commit effc6710d9
3 changed files with 44 additions and 0 deletions

View File

@ -5,6 +5,7 @@ package goruntime
import (
"unsafe"
"github.com/achilleasa/gopher-os/kernel"
"github.com/achilleasa/gopher-os/kernel/mem"
"github.com/achilleasa/gopher-os/kernel/mem/pmm/allocator"
"github.com/achilleasa/gopher-os/kernel/mem/vmm"
@ -14,8 +15,12 @@ var (
mapFn = vmm.Map
earlyReserveRegionFn = vmm.EarlyReserveRegion
frameAllocFn = allocator.AllocFrame
mallocInitFn = mallocInit
)
//go:linkname mallocInit runtime.mallocinit
func mallocInit()
//go:linkname mSysStatInc runtime.mSysStatInc
func mSysStatInc(*uint64, uintptr)
@ -100,6 +105,30 @@ func sysAlloc(size uintptr, sysStat *uint64) unsafe.Pointer {
return unsafe.Pointer(regionStartAddr)
}
// nanotime returns a monotonically increasing clock value. This is a dummy
// implementation and will be replaced when the timekeeper package is
// implemented.
//
// This function replaces runtime.nanotime and is invoked by the Go allocator
// when a span allocation is performed.
//
//go:redirect-from runtime.nanotime
//go:nosplit
func nanotime() uint64 {
// Use a dummy loop to prevent the compiler from inlining this function.
for i := 0; i < 100; i++ {
}
return 1
}
// Init enables support for various Go runtime features. After a call to init
// the following runtime features become available for use:
// - heap memory allocation (new, make e.t.c)
func Init() *kernel.Error {
mallocInitFn()
return nil
}
func init() {
// Dummy calls so the compiler does not optimize away the functions in
// this file.
@ -112,4 +141,5 @@ func init() {
sysReserve(zeroPtr, 0, &reserved)
sysMap(zeroPtr, 0, reserved, &stat)
sysAlloc(0, &stat)
stat = nanotime()
}

View File

@ -235,3 +235,14 @@ func TestSysAlloc(t *testing.T) {
}
})
}
func TestInit(t *testing.T) {
defer func() {
mallocInitFn = mallocInit
}()
mallocInitFn = func() {}
if err := Init(); err != nil {
t.Fatal(t)
}
}

View File

@ -2,6 +2,7 @@ package kmain
import (
"github.com/achilleasa/gopher-os/kernel"
"github.com/achilleasa/gopher-os/kernel/goruntime"
"github.com/achilleasa/gopher-os/kernel/hal"
"github.com/achilleasa/gopher-os/kernel/hal/multiboot"
"github.com/achilleasa/gopher-os/kernel/mem/pmm/allocator"
@ -34,6 +35,8 @@ func Kmain(multibootInfoPtr, kernelStart, kernelEnd uintptr) {
panic(err)
} else if err = vmm.Init(); err != nil {
panic(err)
} else if err = goruntime.Init(); err != nil {
panic(err)
}
// Use kernel.Panic instead of panic to prevent the compiler from