From 9567f259bd6f61a4a0440b0086acc0e05c22b96b Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Fri, 14 Jul 2017 07:45:56 +0100 Subject: [PATCH 1/3] Set m.curg = g0 --- src/arch/x86_64/asm/rt0_64.s | 1 + 1 file changed, 1 insertion(+) diff --git a/src/arch/x86_64/asm/rt0_64.s b/src/arch/x86_64/asm/rt0_64.s index efffa3f..bf1f115 100644 --- a/src/arch/x86_64/asm/rt0_64.s +++ b/src/arch/x86_64/asm/rt0_64.s @@ -95,6 +95,7 @@ _rt0_64_setup_go_runtime_structs: ; Link m0 to the g0 extern runtime.m0 mov rbx, runtime.m0 + mov qword [rbx+GO_M_CURG], rsi ; m.curg = g0 mov qword [rbx+GO_M_G0], rsi ; m.g0 = g0 mov qword [rsi+GO_G_M], rbx ; g.m = m From ccba8877ce9c3a85a7948f0c707b4f5f3a6c8c5a Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Fri, 14 Jul 2017 07:49:20 +0100 Subject: [PATCH 2/3] Enable support for deferred calls --- src/gopheros/kernel/goruntime/bootstrap.go | 13 +++++++++++++ src/gopheros/kernel/goruntime/bootstrap_test.go | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/gopheros/kernel/goruntime/bootstrap.go b/src/gopheros/kernel/goruntime/bootstrap.go index 1010a18..e51fedc 100644 --- a/src/gopheros/kernel/goruntime/bootstrap.go +++ b/src/gopheros/kernel/goruntime/bootstrap.go @@ -21,6 +21,7 @@ var ( typeLinksInitFn = typeLinksInit itabsInitFn = itabsInit initGoPackagesFn = initGoPackages + procResizeFn = procResize // A seed for the pseudo-random number generator used by getRandomData prngSeed = 0xdeadc0de @@ -44,6 +45,9 @@ func mallocInit() //go:linkname mSysStatInc runtime.mSysStatInc func mSysStatInc(*uint64, uintptr) +//go:linkname procResize runtime.procresize +func procResize(int32) uintptr + // initGoPackages is an alias to main.init which recursively calls the init() // methods in all imported packages. Unless this function is called, things like // package errors will not be properly initialized causing various problems when @@ -184,11 +188,20 @@ func Init() *kernel.Error { typeLinksInitFn() // uses maps, activeModules itabsInitFn() // uses activeModules + // Set processor count to 1. This initializes the p pointer in the + // currently active m allowing the kernel to register defer functions + SetCPUCount(1) + initGoPackagesFn() return nil } +// SetCPUCount registers the number of available CPUs with the Go runtime. +func SetCPUCount(numCPUs int32) { + procResizeFn(numCPUs) +} + func init() { // Dummy calls so the compiler does not optimize away the functions in // this file. diff --git a/src/gopheros/kernel/goruntime/bootstrap_test.go b/src/gopheros/kernel/goruntime/bootstrap_test.go index 37d1287..bf2aca6 100644 --- a/src/gopheros/kernel/goruntime/bootstrap_test.go +++ b/src/gopheros/kernel/goruntime/bootstrap_test.go @@ -267,6 +267,7 @@ func TestInit(t *testing.T) { typeLinksInitFn = typeLinksInit itabsInitFn = itabsInit initGoPackagesFn = initGoPackages + procResizeFn = procResize }() mallocInitFn = func() {} @@ -275,7 +276,7 @@ func TestInit(t *testing.T) { typeLinksInitFn = func() {} itabsInitFn = func() {} initGoPackagesFn = func() {} - + procResizeFn = func(_ int32) uintptr { return 0 } if err := Init(); err != nil { t.Fatal(t) } From 66d471f4426abad8175ee818421fc92e21e69c49 Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Fri, 14 Jul 2017 07:51:04 +0100 Subject: [PATCH 3/3] Defer kernel panic when Kmain returns --- src/gopheros/kernel/kmain/kmain.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/gopheros/kernel/kmain/kmain.go b/src/gopheros/kernel/kmain/kmain.go index 2e3f2df..736a511 100644 --- a/src/gopheros/kernel/kmain/kmain.go +++ b/src/gopheros/kernel/kmain/kmain.go @@ -39,10 +39,13 @@ func Kmain(multibootInfoPtr, kernelStart, kernelEnd, kernelPageOffset uintptr) { panic(err) } + // After goruntime.Init returns we can safely use defer + defer func() { + // Use kfmt.Panic instead of panic to prevent the compiler from + // treating kernel.Panic as dead-code and eliminating it. + kfmt.Panic(errKmainReturned) + }() + // Detect and initialize hardware hal.DetectHardware() - - // Use kfmt.Panic instead of panic to prevent the compiler from - // treating kernel.Panic as dead-code and eliminating it. - kfmt.Panic(errKmainReturned) }