Skip to content

Commit 1bccd9d

Browse files
[nrf fromlist] arch: arm: cortex_m: pm_s2ram: Rework S2RAM mark functions
The S2RAM procedure requires marker checking after reset. Such checking is performed on very early stage of the system initialization and must ensure that the stack is not used due to the TLS pointer which is not initialized yet. Upstream PR: zephyrproject-rtos/zephyr#80039 Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent 2542e3c commit 1bccd9d

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

arch/arm/core/cortex_m/pm_s2ram.S

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,47 @@ GTEXT(pm_s2ram_mark_set)
2020
GTEXT(pm_s2ram_mark_check_and_clear)
2121
GDATA(_cpu_context)
2222

23+
#if !(CONFIG_PM_S2RAM_CUSTOM_MARKING)
24+
#define MAGIC (0xDABBAD00)
25+
GDATA(marker)
26+
27+
SECTION_FUNC(TEXT, pm_s2ram_mark_set)
28+
/*
29+
* Set the marker to MAGIC value
30+
*/
31+
ldr r0, =marker
32+
ldr r2, =MAGIC
33+
str r2, [r0]
34+
35+
bx lr
36+
37+
SECTION_FUNC(TEXT, pm_s2ram_mark_check_and_clear)
38+
/* Set return value to 0 */
39+
mov r0, #0
40+
41+
/*
42+
* Check the marker
43+
*/
44+
ldr r2, =marker
45+
ldr r5, [r2]
46+
ldr r3, =MAGIC
47+
cmp r5, r3
48+
bne exit
49+
50+
/*
51+
* Reset the marker
52+
*/
53+
str r0, [r2]
54+
55+
/*
56+
* Set return value to 1
57+
*/
58+
mov r0, #1
59+
60+
exit:
61+
bx lr
62+
#endif
63+
2364
SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
2465
/*
2566
* Save the CPU context
@@ -70,7 +111,9 @@ SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
70111
/*
71112
* Mark entering suspend to RAM.
72113
*/
73-
bl pm_s2ram_mark_set
114+
mov r1, lr
115+
bl pm_s2ram_mark_set
116+
mov lr, r1
74117

75118
/*
76119
* Call the system_off function passed as parameter. This should never
@@ -86,7 +129,9 @@ SECTION_FUNC(TEXT, arch_pm_s2ram_suspend)
86129
/*
87130
* Reset the marking of suspend to RAM, return is ignored.
88131
*/
89-
bl pm_s2ram_mark_check_and_clear
132+
mov r1, lr
133+
bl pm_s2ram_mark_check_and_clear
134+
mov lr, r1
90135

91136
/* Move system_off back to r0 as return value */
92137
mov r0, r4
@@ -99,16 +144,13 @@ GTEXT(arch_pm_s2ram_resume)
99144
SECTION_FUNC(TEXT, arch_pm_s2ram_resume)
100145
/*
101146
* Check if reset occurred after suspending to RAM.
102-
* Store LR to ensure we can continue boot when we are not suspended
103-
* to RAM. In addition to LR, R0 is pushed too, to ensure "SP mod 8 = 0",
104-
* as stated by ARM rule 6.2.1.2 for AAPCS32.
105147
*/
106-
push {r0, lr}
107-
bl pm_s2ram_mark_check_and_clear
108-
cmp r0, #0x1
109-
pop {r0, lr}
110-
beq resume
111-
bx lr
148+
mov r1, lr
149+
bl pm_s2ram_mark_check_and_clear
150+
mov lr, r1
151+
cmp r0, #0x1
152+
beq resume
153+
bx lr
112154

113155
resume:
114156
/*

arch/arm/core/cortex_m/pm_s2ram.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#include <zephyr/arch/common/pm_s2ram.h>
1111

12-
#define MAGIC (0xDABBAD00)
13-
1412
/**
1513
* CPU context for S2RAM
1614
*/
@@ -20,22 +18,6 @@ __noinit _cpu_context_t _cpu_context;
2018
/**
2119
* S2RAM Marker
2220
*/
23-
static __noinit uint32_t marker;
24-
25-
void pm_s2ram_mark_set(void)
26-
{
27-
marker = MAGIC;
28-
}
29-
30-
bool pm_s2ram_mark_check_and_clear(void)
31-
{
32-
if (marker == MAGIC) {
33-
marker = 0;
34-
35-
return true;
36-
}
37-
38-
return false;
39-
}
21+
__noinit uint32_t marker;
4022

4123
#endif /* CONFIG_PM_S2RAM_CUSTOM_MARKING */

include/zephyr/arch/common/pm_s2ram.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ int arch_pm_s2ram_suspend(pm_s2ram_system_off_fn_t system_off);
6565
*
6666
* Default implementation is setting a magic word in RAM. CONFIG_PM_S2RAM_CUSTOM_MARKING
6767
* allows custom implementation.
68+
* The following requirements must be fulfilled:
69+
* - the function cannot use stack (most likely asm function)
70+
* - the content of the R1, R4 and LR registers must remain unchanged
71+
* - the last instruction restores the PC stored in R1 register
72+
*
6873
*/
6974
void pm_s2ram_mark_set(void);
7075

@@ -76,6 +81,11 @@ void pm_s2ram_mark_set(void);
7681
*
7782
* Default implementation is checking a magic word in RAM. CONFIG_PM_S2RAM_CUSTOM_MARKING
7883
* allows custom implementation.
84+
* The following requirements must be fulfilled:
85+
* - the function cannot use stack (most likely asm function)
86+
* - the content of the R1, R4 and LR registers must remain unchanged
87+
* - the function's return value is passed by R0
88+
* - the last instruction restores the PC stored in R1 register
7989
*
8090
* @retval true if marking is found which indicates resuming after suspend-to-RAM.
8191
* @retval false if marking is not found which indicates standard boot.

0 commit comments

Comments
 (0)