Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builder/sizes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestBinarySize(t *testing.T) {
// microcontrollers
{"hifive1b", "examples/echo", 3680, 280, 0, 2252},
{"microbit", "examples/serial", 2694, 342, 8, 2248},
{"wioterminal", "examples/pininterrupt", 7074, 1510, 120, 7248},
{"wioterminal", "examples/pininterrupt", 7150, 1534, 120, 7248},

// TODO: also check wasm. Right now this is difficult, because
// wasm binaries are run through wasm-opt and therefore the
Expand Down
5 changes: 5 additions & 0 deletions src/internal/task/task_none.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ func SystemStack() uintptr {
runtimePanic("scheduler is disabled")
return 0 // unreachable
}

func GoroutineStack() uintptr {
// No separate goroutine stack without a scheduler.
return 0
}
11 changes: 11 additions & 0 deletions src/internal/task/task_stack_cortexm.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ uintptr_t SystemStack() {
);
return sp;
}

uintptr_t GoroutineStack() {
uintptr_t sp;
asm volatile(
"mrs %0, PSP"
: "=r"(sp)
:
: "memory"
);
return sp;
}
7 changes: 7 additions & 0 deletions src/internal/task/task_stack_cortexm.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,10 @@ func (s *state) pause() {
//
//export SystemStack
func SystemStack() uintptr

// GoroutineStack returns the PSP (goroutine stack pointer). When a fault
// occurs in goroutine context, the hardware saves the exception frame to PSP;
// reading PSP+0x18 gives the actual faulting PC.
//
//export GoroutineStack
func GoroutineStack() uintptr
13 changes: 13 additions & 0 deletions src/runtime/runtime_cortexm_hardfault_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package runtime

import (
"device/arm"
"internal/task"
"unsafe"
)

Expand Down Expand Up @@ -106,6 +107,18 @@ func HardFault_Handler() {
print(" pc=", sp.PC)
}
}

// PSP holds the actual exception frame when the fault occurred in a
// goroutine (thread mode, PSP active). The MSP-based sp above is the
// scheduler's stack and its PC is garbage in that case.
pspVal := task.GoroutineStack()
if pspVal >= 0x20000000 && pspVal < 0x20040000 {
pspFrame := (*interruptStack)(unsafe.Pointer(pspVal))
print(" psp=", pspFrame)
print(" psp_pc=", pspFrame.PC)
print(" psp_lr=", pspFrame.LR)
}

println()
abort()
}
Expand Down