From fcd88356dbef8c77b5ba4c3cd387955d01e58013 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 1 Jan 2022 22:54:29 +0100 Subject: [PATCH] avr: fix time.Sleep() in init code In the early days of TinyGo, the idea of `postinit` was to enable interrupts only after initializers have run. Which kind of makes sense... except that `time.Sleep` is allowed in init code and `time.Sleep` requires interrupts to be enabled. Therefore, interrupts must be enabled while initializers are being run. This commit simply moves the enabling of interrupts to a point right before running package initializers. It also removes `runtime.postinit`, which is not necessary anymore (and was only used on AVR). --- main_test.go | 3 --- src/runtime/runtime_arm7tdmi.go | 2 -- src/runtime/runtime_atsamd21.go | 2 -- src/runtime/runtime_atsamd51.go | 2 -- src/runtime/runtime_avr.go | 11 +++++------ src/runtime/runtime_cortexm_qemu.go | 2 -- src/runtime/runtime_esp32xx.go | 2 -- src/runtime/runtime_esp8266.go | 2 -- src/runtime/runtime_fe310.go | 2 -- src/runtime/runtime_k210.go | 2 -- src/runtime/runtime_mimxrt1062.go | 2 -- src/runtime/runtime_nintendoswitch.go | 2 -- src/runtime/runtime_nrf.go | 2 -- src/runtime/runtime_nxpmk66f18.go | 2 -- src/runtime/runtime_rp2040.go | 2 -- src/runtime/runtime_stm32.go | 2 -- src/runtime/runtime_tinygoriscv_qemu.go | 2 -- src/runtime/runtime_tinygowasm.go | 2 -- src/runtime/runtime_unix.go | 2 -- src/runtime/runtime_windows.go | 2 -- src/runtime/scheduler_any.go | 1 - src/runtime/scheduler_none.go | 1 - 22 files changed, 5 insertions(+), 47 deletions(-) diff --git a/main_test.go b/main_test.go index 58ad63b99..607f109ba 100644 --- a/main_test.go +++ b/main_test.go @@ -171,9 +171,6 @@ func TestCompiler(t *testing.T) { case "float.go", "math.go", "print.go": // Stuck in runtime.printfloat64. - case "goroutines.go": - // The main() never runs. - case "interface.go": // Several comparison tests fail. diff --git a/src/runtime/runtime_arm7tdmi.go b/src/runtime/runtime_arm7tdmi.go index 3938506bd..3b383c326 100644 --- a/src/runtime/runtime_arm7tdmi.go +++ b/src/runtime/runtime_arm7tdmi.go @@ -28,8 +28,6 @@ var _sidata [0]byte //go:extern _edata var _edata [0]byte -func postinit() {} - // Entry point for Go. Initialize all packages and call main.main(). //export main func main() { diff --git a/src/runtime/runtime_atsamd21.go b/src/runtime/runtime_atsamd21.go index 32ed1cea4..c4147c802 100644 --- a/src/runtime/runtime_atsamd21.go +++ b/src/runtime/runtime_atsamd21.go @@ -13,8 +13,6 @@ import ( type timeUnit int64 -func postinit() {} - //export Reset_Handler func main() { preinit() diff --git a/src/runtime/runtime_atsamd51.go b/src/runtime/runtime_atsamd51.go index ec704471c..3561050bf 100644 --- a/src/runtime/runtime_atsamd51.go +++ b/src/runtime/runtime_atsamd51.go @@ -12,8 +12,6 @@ import ( type timeUnit int64 -func postinit() {} - //export Reset_Handler func main() { arm.SCB.CPACR.Set(0) // disable FPU if it is enabled diff --git a/src/runtime/runtime_avr.go b/src/runtime/runtime_avr.go index 72b141065..751020443 100644 --- a/src/runtime/runtime_avr.go +++ b/src/runtime/runtime_avr.go @@ -42,6 +42,7 @@ var _ebss [0]byte //export main func main() { preinit() + initHardware() run() exit(0) } @@ -55,15 +56,13 @@ func preinit() { } } -func postinit() { - // Enable interrupts after initialization. - avr.Asm("sei") -} - -func init() { +func initHardware() { initUART() machine.InitMonotonicTimer() nextTimerRecalibrate = ticks() + timerRecalibrateInterval + + // Enable interrupts after initialization. + avr.Asm("sei") } func ticksToNanoseconds(ticks timeUnit) int64 { diff --git a/src/runtime/runtime_cortexm_qemu.go b/src/runtime/runtime_cortexm_qemu.go index 2d7e78f33..07eef7065 100644 --- a/src/runtime/runtime_cortexm_qemu.go +++ b/src/runtime/runtime_cortexm_qemu.go @@ -15,8 +15,6 @@ type timeUnit int64 var timestamp timeUnit -func postinit() {} - //export Reset_Handler func main() { preinit() diff --git a/src/runtime/runtime_esp32xx.go b/src/runtime/runtime_esp32xx.go index 54dadab8e..b3270ac39 100644 --- a/src/runtime/runtime_esp32xx.go +++ b/src/runtime/runtime_esp32xx.go @@ -14,8 +14,6 @@ func putchar(c byte) { machine.Serial.WriteByte(c) } -func postinit() {} - // Initialize .bss: zero-initialized global variables. // The .data section has already been loaded by the ROM bootloader. func clearbss() { diff --git a/src/runtime/runtime_esp8266.go b/src/runtime/runtime_esp8266.go index aa698aeda..936b99a4e 100644 --- a/src/runtime/runtime_esp8266.go +++ b/src/runtime/runtime_esp8266.go @@ -23,8 +23,6 @@ func putchar(c byte) { //export rom_i2c_writeReg func rom_i2c_writeReg(block, host_id, reg_add, data uint8) -func postinit() {} - //export main func main() { // Clear .bss section. .data has already been loaded by the ROM bootloader. diff --git a/src/runtime/runtime_fe310.go b/src/runtime/runtime_fe310.go index 33d8e4834..c09dfe81c 100644 --- a/src/runtime/runtime_fe310.go +++ b/src/runtime/runtime_fe310.go @@ -16,8 +16,6 @@ import ( type timeUnit int64 -func postinit() {} - //export main func main() { // Zero the PLIC enable bits on startup: they are not zeroed at reset. diff --git a/src/runtime/runtime_k210.go b/src/runtime/runtime_k210.go index dc8ee0445..2fb4fec65 100644 --- a/src/runtime/runtime_k210.go +++ b/src/runtime/runtime_k210.go @@ -15,8 +15,6 @@ import ( type timeUnit int64 -func postinit() {} - //export main func main() { diff --git a/src/runtime/runtime_mimxrt1062.go b/src/runtime/runtime_mimxrt1062.go index 6c1212cba..8a9f2b01b 100644 --- a/src/runtime/runtime_mimxrt1062.go +++ b/src/runtime/runtime_mimxrt1062.go @@ -16,8 +16,6 @@ var _svectors [0]byte //go:extern _flexram_cfg var _flexram_cfg [0]byte -func postinit() {} - //export Reset_Handler func main() { diff --git a/src/runtime/runtime_nintendoswitch.go b/src/runtime/runtime_nintendoswitch.go index 99cb3b0b6..0267b5410 100644 --- a/src/runtime/runtime_nintendoswitch.go +++ b/src/runtime/runtime_nintendoswitch.go @@ -43,8 +43,6 @@ var ( totalHeap = uint64(0) ) -func postinit() {} - func preinit() { // Unsafe to use heap here setupEnv() diff --git a/src/runtime/runtime_nrf.go b/src/runtime/runtime_nrf.go index 551078b8b..dfbc76e10 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -15,8 +15,6 @@ type timeUnit int64 //go:linkname systemInit SystemInit func systemInit() -func postinit() {} - //export Reset_Handler func main() { if nrf.FPUPresent { diff --git a/src/runtime/runtime_nxpmk66f18.go b/src/runtime/runtime_nxpmk66f18.go index c54ab3771..02a9bb594 100644 --- a/src/runtime/runtime_nxpmk66f18.go +++ b/src/runtime/runtime_nxpmk66f18.go @@ -226,8 +226,6 @@ func initInternal() { // analog_init(); } -func postinit() {} - func putchar(c byte) { machine.PutcharUART(machine.UART0, c) } diff --git a/src/runtime/runtime_rp2040.go b/src/runtime/runtime_rp2040.go index 4d615a142..38f6d3041 100644 --- a/src/runtime/runtime_rp2040.go +++ b/src/runtime/runtime_rp2040.go @@ -53,8 +53,6 @@ func init() { machine.Serial.Configure(machine.UARTConfig{}) } -func postinit() {} - //export Reset_Handler func main() { preinit() diff --git a/src/runtime/runtime_stm32.go b/src/runtime/runtime_stm32.go index 08dde0dd6..aabffa2ab 100644 --- a/src/runtime/runtime_stm32.go +++ b/src/runtime/runtime_stm32.go @@ -6,8 +6,6 @@ import "device/arm" type timeUnit int64 -func postinit() {} - //export Reset_Handler func main() { preinit() diff --git a/src/runtime/runtime_tinygoriscv_qemu.go b/src/runtime/runtime_tinygoriscv_qemu.go index 04e41b812..8558f41e8 100644 --- a/src/runtime/runtime_tinygoriscv_qemu.go +++ b/src/runtime/runtime_tinygoriscv_qemu.go @@ -15,8 +15,6 @@ type timeUnit int64 var timestamp timeUnit -func postinit() {} - //export main func main() { preinit() diff --git a/src/runtime/runtime_tinygowasm.go b/src/runtime/runtime_tinygowasm.go index 80eaa8d3e..a5b1b3c86 100644 --- a/src/runtime/runtime_tinygowasm.go +++ b/src/runtime/runtime_tinygowasm.go @@ -22,8 +22,6 @@ func fd_write(id uint32, iovs *__wasi_iovec_t, iovs_len uint, nwritten *uint) (e //export proc_exit func proc_exit(exitcode uint32) -func postinit() {} - const ( putcharBufferSize = 120 stdout = 1 diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index 5b9161ddb..fe8c0cac2 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -65,8 +65,6 @@ type timespec struct { var stackTop uintptr -func postinit() {} - // Entry point for Go. Initialize all packages and call main.main(). //export main func main(argc int32, argv *unsafe.Pointer) int { diff --git a/src/runtime/runtime_windows.go b/src/runtime/runtime_windows.go index 6872992da..851f40f7a 100644 --- a/src/runtime/runtime_windows.go +++ b/src/runtime/runtime_windows.go @@ -41,8 +41,6 @@ func __p___argc() *int32 //export __p___argv func __p___argv() **unsafe.Pointer -func postinit() {} - //export mainCRTStartup func mainCRTStartup() int { preinit() diff --git a/src/runtime/scheduler_any.go b/src/runtime/scheduler_any.go index e0597dc30..d80979151 100644 --- a/src/runtime/scheduler_any.go +++ b/src/runtime/scheduler_any.go @@ -22,7 +22,6 @@ func run() { initHeap() go func() { initAll() - postinit() callMain() schedulerDone = true }() diff --git a/src/runtime/scheduler_none.go b/src/runtime/scheduler_none.go index b037dae05..2086e4225 100644 --- a/src/runtime/scheduler_none.go +++ b/src/runtime/scheduler_none.go @@ -23,7 +23,6 @@ func getSystemStackPointer() uintptr { func run() { initHeap() initAll() - postinit() callMain() }