forked from tinygo-org/tinygo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_stack_cortexm.go
More file actions
79 lines (63 loc) · 2.37 KB
/
Copy pathtask_stack_cortexm.go
File metadata and controls
79 lines (63 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//go:build (scheduler.tasks || scheduler.cores) && cortexm
package task
// Note that this is almost the same as task_stack_arm.go, but it uses the MSP
// register to store the system stack pointer instead of a global variable. The
// big advantage of this is that interrupts always execute with MSP (and not
// PSP, which is used for goroutines) so that goroutines do not need extra stack
// space for interrupts.
import "C"
import (
"unsafe"
)
// calleeSavedRegs is the list of registers that must be saved and restored when
// switching between tasks. Also see task_stack_cortexm.S that relies on the
// exact layout of this struct.
type calleeSavedRegs struct {
r4 uintptr
r5 uintptr
r6 uintptr
r7 uintptr
r8 uintptr
r9 uintptr
r10 uintptr
r11 uintptr
pc uintptr
}
// archInit runs architecture-specific setup for the goroutine startup.
func (s *state) archInit(r *calleeSavedRegs, fn uintptr, args unsafe.Pointer) {
// Store the initial sp for the startTask function (implemented in assembly).
s.sp = uintptr(unsafe.Pointer(r))
// Initialize the registers.
// These will be popped off of the stack on the first resume of the goroutine.
// Start the function at tinygo_startTask (defined in src/internal/task/task_stack_cortexm.S).
// This assembly code calls a function (passed in r4) with a single argument (passed in r5).
// After the function returns, it calls Pause().
r.pc = uintptr(unsafe.Pointer(&startTask))
// Pass the function to call in r4.
// This function is a compiler-generated wrapper which loads arguments out of a struct pointer.
// See createGoroutineStartWrapper (defined in compiler/goroutine.go) for more information.
r.r4 = fn
// Pass the pointer to the arguments struct in r5.
r.r5 = uintptr(args)
}
func (s *state) resume() {
tinygo_switchToTask(s.sp)
}
//export tinygo_switchToTask
func tinygo_switchToTask(uintptr)
//export tinygo_switchToScheduler
func tinygo_switchToScheduler(*uintptr)
func (s *state) pause() {
tinygo_switchToScheduler(&s.sp)
}
// SystemStack returns the system stack pointer. On Cortex-M, it is always
// available.
//
//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