-
Notifications
You must be signed in to change notification settings - Fork 6
/
mpx-m4.s
75 lines (66 loc) · 2.32 KB
/
mpx-m4.s
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
@@@ mpx-m4.s
@@@ Copyright (c) 2018 J. M. Spivey
@@@ Hardware multiplexing for the ARM Cortex-M4 (no floats)
.syntax unified
.text
@@@ __run -- enter process mode
.global __run
.thumb_func
__run:
msr psp, r1 @ Set up the stack
movs r2, #2 @ Use psp for stack pointer
msr control, r2
isb @ Drain the pipeline
bx r0 @ Jump to the task
@@@ Stack layout for interrupt frames (17 words, 68 bytes)
@@@ --------------------------------------
@@@ 16 PSR Status register
@@@ 15 PC Program counter
@@@ 14 LR Link register
@@@ 13 R12
@@@ 12 R3
@@@ 11 R2 (Saved by hardware)
@@@ 10 R1
@@@ 9 R0
@@@ --------------------------------------
@@@ 8 R11
@@@ 7 R10
@@@ 6 R9
@@@ 5 R8 (Saved manually)
@@@ 4 R7
@@@ 3 R6
@@@ 2 R5
@@@ 1 R4
@@@ 0 LR' Magic value <-- Stack pointer
@@@ --------------------------------------
@@@ The magic value for exception return is carefully preserved for each
@@@ process. On Cortex-M4F, it will encode info about the hardware-saved
@@@ frame layout.
@@@ isave -- save context for system call
.macro isave
mrs r0, psp @ Get thread stack pointer
mov r3, lr @ Preserve magic value 0xfffffffd
stmfd r0!, {r3-r11} @ Save registers
.endm @ Return new thread sp
@@@ irestore -- restore context after system call
.macro irestore @ Expect process sp in r0
ldmfd r0!, {r3-r11} @ Restore registers
msr psp, r0 @ Set stack pointer for thread
bx r3
.endm
@@@ svc_handler -- handler for SVC interrupt (system call)
.global svc_handler
.thumb_func
svc_handler:
isave @ Complete saving of state
@@ Argument in r0 is sp of old process
bl system_call @ Perform system call
@@ Result in r0 is sp of new process
irestore @ Restore manually saved state
@@@ pendsv_handler -- handler for PendSV interupt (context switch)
.global pendsv_handler
.thumb_func
pendsv_handler:
isave @ Complete saving of process state
bl cxt_switch @ Choose a new process
irestore @ Restore state for that process