From 101c51e06b785655bf76f4c1e3812abe0ab1905c Mon Sep 17 00:00:00 2001 From: Jaden Weiss Date: Fri, 7 Feb 2020 17:17:38 -0500 Subject: [PATCH 1/3] generalize -scheduler=none to support most platforms --- src/runtime/runtime_arm7tdmi.go | 12 ++++-------- src/runtime/runtime_atsamd21.go | 6 +++--- src/runtime/runtime_atsamd51.go | 6 +++--- src/runtime/runtime_avr.go | 4 +--- src/runtime/runtime_cortexm_qemu.go | 6 +++--- src/runtime/runtime_fe310.go | 6 +++--- src/runtime/runtime_nrf.go | 6 +++--- src/runtime/runtime_stm32.go | 6 +++--- src/runtime/runtime_unix.go | 11 +++-------- src/runtime/runtime_wasm.go | 6 +++--- src/runtime/scheduler_any.go | 11 +++++++++++ src/runtime/scheduler_none.go | 8 ++++++++ 12 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/runtime/runtime_arm7tdmi.go b/src/runtime/runtime_arm7tdmi.go index cf2ff68183..d7629ad6da 100644 --- a/src/runtime/runtime_arm7tdmi.go +++ b/src/runtime/runtime_arm7tdmi.go @@ -30,20 +30,16 @@ var _sidata [0]byte //go:extern _edata var _edata [0]byte +func postinit() {} + // Entry point for Go. Initialize all packages and call main.main(). //go:export main func main() { // Initialize .data and .bss sections. preinit() - // Run initializers of all packages. - initAll() - - // Compiler-generated call to main.main(). - go callMain() - - // Run the scheduler. - scheduler() + // Run program. + run() } func preinit() { diff --git a/src/runtime/runtime_atsamd21.go b/src/runtime/runtime_atsamd21.go index a3aa6c9739..5c535cf1d2 100644 --- a/src/runtime/runtime_atsamd21.go +++ b/src/runtime/runtime_atsamd21.go @@ -13,12 +13,12 @@ import ( type timeUnit int64 +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_atsamd51.go b/src/runtime/runtime_atsamd51.go index 23d91109c9..a605eee589 100644 --- a/src/runtime/runtime_atsamd51.go +++ b/src/runtime/runtime_atsamd51.go @@ -12,12 +12,12 @@ import ( type timeUnit int64 +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_avr.go b/src/runtime/runtime_avr.go index 09f91f7ad6..09d4b5991b 100644 --- a/src/runtime/runtime_avr.go +++ b/src/runtime/runtime_avr.go @@ -39,9 +39,7 @@ var _ebss [0]byte //go:export main func main() { preinit() - initAll() - postinit() - callMain() + run() abort() } diff --git a/src/runtime/runtime_cortexm_qemu.go b/src/runtime/runtime_cortexm_qemu.go index 973c20ac2d..80365a7d28 100644 --- a/src/runtime/runtime_cortexm_qemu.go +++ b/src/runtime/runtime_cortexm_qemu.go @@ -17,12 +17,12 @@ const tickMicros = 1 var timestamp timeUnit +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() arm.SemihostingCall(arm.SemihostingReportException, arm.SemihostingApplicationExit) abort() } diff --git a/src/runtime/runtime_fe310.go b/src/runtime/runtime_fe310.go index 88295c9c0e..62673ad352 100644 --- a/src/runtime/runtime_fe310.go +++ b/src/runtime/runtime_fe310.go @@ -31,6 +31,8 @@ var _sidata [0]byte //go:extern _edata var _edata [0]byte +func postinit() {} + //go:export main func main() { // Zero the PLIC enable bits on startup: they are not zeroed at reset. @@ -51,9 +53,7 @@ func main() { preinit() initPeripherals() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_nrf.go b/src/runtime/runtime_nrf.go index 9b01ded705..9cfa457a67 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -17,13 +17,13 @@ const tickMicros = 1024 * 32 //go:linkname systemInit SystemInit func systemInit() +func postinit() {} + //go:export Reset_Handler func main() { systemInit() preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_stm32.go b/src/runtime/runtime_stm32.go index 11e686f06a..bd529cebd1 100644 --- a/src/runtime/runtime_stm32.go +++ b/src/runtime/runtime_stm32.go @@ -4,11 +4,11 @@ package runtime type timeUnit int64 +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index a88f243f73..bb34a0d5bb 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -45,17 +45,12 @@ type timespec struct { const CLOCK_MONOTONIC_RAW = 4 +func postinit() {} + // Entry point for Go. Initialize all packages and call main.main(). //go:export main func main() int { - // Run initializers of all packages. - initAll() - - // Compiler-generated call to main.main(). - go callMain() - - // Run scheduler. - scheduler() + run() // For libc compatibility. return 0 diff --git a/src/runtime/runtime_wasm.go b/src/runtime/runtime_wasm.go index c01fa93265..90a25a82cb 100644 --- a/src/runtime/runtime_wasm.go +++ b/src/runtime/runtime_wasm.go @@ -18,11 +18,11 @@ type wasiIOVec struct { //export fd_write func fd_write(id uint32, iovs *wasiIOVec, iovs_len uint, nwritten *uint) (errno uint) +func postinit() {} + //export _start func _start() { - initAll() - go callMain() - scheduler() + run() } // Using global variables to avoid heap allocation. diff --git a/src/runtime/scheduler_any.go b/src/runtime/scheduler_any.go index d541dc6a3c..b96a4eba93 100644 --- a/src/runtime/scheduler_any.go +++ b/src/runtime/scheduler_any.go @@ -10,3 +10,14 @@ func sleep(duration int64) { addSleepTask(task.Current(), duration) task.Pause() } + +// run is called by the program entry point to execute the go program. +// With a scheduler, init and the main function are invoked in a goroutine before starting the scheduler. +func run() { + initAll() + postinit() + go func() { + callMain() + }() + scheduler() +} diff --git a/src/runtime/scheduler_none.go b/src/runtime/scheduler_none.go index 222867dd0a..a86110f2d9 100644 --- a/src/runtime/scheduler_none.go +++ b/src/runtime/scheduler_none.go @@ -12,3 +12,11 @@ func sleep(duration int64) { func getSystemStackPointer() uintptr { return getCurrentStackPointer() } + +// run is called by the program entry point to execute the go program. +// With the "none" scheduler, init and the main function are invoked directly. +func run() { + initAll() + postinit() + callMain() +} From 6a13a085f55a4d003c25c0244d2d369fe4c82cc8 Mon Sep 17 00:00:00 2001 From: Jaden Weiss Date: Fri, 7 Feb 2020 19:20:36 -0500 Subject: [PATCH 2/3] run init in a goroutine --- src/runtime/gc_conservative.go | 2 +- src/runtime/gc_leaking.go | 4 ++++ src/runtime/gc_none.go | 4 ++++ src/runtime/runtime_unix.go | 8 ++++---- src/runtime/runtime_wasm.go | 4 ++++ src/runtime/scheduler_any.go | 5 +++-- src/runtime/scheduler_none.go | 1 + 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/runtime/gc_conservative.go b/src/runtime/gc_conservative.go index 41a27523e1..319e68b55e 100644 --- a/src/runtime/gc_conservative.go +++ b/src/runtime/gc_conservative.go @@ -180,7 +180,7 @@ func (b gcBlock) unmark() { // No memory may be allocated before this is called. That means the runtime and // any packages the runtime depends upon may not allocate memory during package // initialization. -func init() { +func initHeap() { totalSize := heapEnd - heapStart // Allocate some memory to keep 2 bits of information about every block. diff --git a/src/runtime/gc_leaking.go b/src/runtime/gc_leaking.go index fb0f9086ed..f5e3595174 100644 --- a/src/runtime/gc_leaking.go +++ b/src/runtime/gc_leaking.go @@ -45,3 +45,7 @@ func KeepAlive(x interface{}) { func SetFinalizer(obj interface{}, finalizer interface{}) { // Unimplemented. } + +func initHeap() { + // Nothing to initialize. +} diff --git a/src/runtime/gc_none.go b/src/runtime/gc_none.go index b7fc628de0..c69d2a2426 100644 --- a/src/runtime/gc_none.go +++ b/src/runtime/gc_none.go @@ -27,3 +27,7 @@ func KeepAlive(x interface{}) { func SetFinalizer(obj interface{}, finalizer interface{}) { // Unimplemented. } + +func initHeap() { + // Nothing to initialize. +} diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index bb34a0d5bb..daf934ea35 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -26,10 +26,7 @@ func clock_gettime(clk_id int32, ts *timespec) const heapSize = 1 * 1024 * 1024 // 1MB to start -var ( - heapStart = uintptr(malloc(heapSize)) - heapEnd = heapStart + heapSize -) +var heapStart, heapEnd uintptr type timeUnit int64 @@ -50,6 +47,9 @@ func postinit() {} // Entry point for Go. Initialize all packages and call main.main(). //go:export main func main() int { + heapStart = uintptr(malloc(heapSize)) + heapEnd = heapStart + heapSize + run() // For libc compatibility. diff --git a/src/runtime/runtime_wasm.go b/src/runtime/runtime_wasm.go index 90a25a82cb..8042161b10 100644 --- a/src/runtime/runtime_wasm.go +++ b/src/runtime/runtime_wasm.go @@ -22,6 +22,10 @@ func postinit() {} //export _start func _start() { + // These need to be initialized early so that the heap can be initialized. + heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) + heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize) + run() } diff --git a/src/runtime/scheduler_any.go b/src/runtime/scheduler_any.go index b96a4eba93..41a904535d 100644 --- a/src/runtime/scheduler_any.go +++ b/src/runtime/scheduler_any.go @@ -14,9 +14,10 @@ func sleep(duration int64) { // run is called by the program entry point to execute the go program. // With a scheduler, init and the main function are invoked in a goroutine before starting the scheduler. func run() { - initAll() - postinit() + initHeap() go func() { + initAll() + postinit() callMain() }() scheduler() diff --git a/src/runtime/scheduler_none.go b/src/runtime/scheduler_none.go index a86110f2d9..d462ca15c5 100644 --- a/src/runtime/scheduler_none.go +++ b/src/runtime/scheduler_none.go @@ -16,6 +16,7 @@ func getSystemStackPointer() uintptr { // run is called by the program entry point to execute the go program. // With the "none" scheduler, init and the main function are invoked directly. func run() { + initHeap() initAll() postinit() callMain() From 90d6b5efae81d0e44f28b6286af8f147838c5468 Mon Sep 17 00:00:00 2001 From: Jaden Weiss Date: Tue, 17 Mar 2020 10:27:01 -0400 Subject: [PATCH 3/3] internal/task: fix build tags on src/internal/task/task_stack_cortexm.go --- src/internal/task/task_stack_cortexm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/task/task_stack_cortexm.go b/src/internal/task/task_stack_cortexm.go index b91e5f041d..a7927d040b 100644 --- a/src/internal/task/task_stack_cortexm.go +++ b/src/internal/task/task_stack_cortexm.go @@ -1,4 +1,4 @@ -// +build scheduler.tasks, cortexm +// +build scheduler.tasks,cortexm package task