Skip to content

Commit d68c016

Browse files
Wayne Rennashif
authored andcommitted
arch: arc: enable stack check when arc is in secure mode
when arc is in secure mode, SSC bit of sec_stat, not SC bit of status32,is used to enable stack check. Signed-off-by: Wayne Ren <[email protected]>
1 parent f3d2893 commit d68c016

File tree

6 files changed

+76
-10
lines changed

6 files changed

+76
-10
lines changed

arch/arc/core/fast_irq.S

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,19 @@ SECTION_FUNC(TEXT, _firq_enter)
6060
* This has already been done by _isr_wrapper.
6161
*/
6262
#ifdef CONFIG_ARC_STACK_CHECKING
63+
#ifdef CONFIG_ARC_HAS_SECURE
64+
lr r2, [_ARC_V2_SEC_STAT]
65+
bclr r2, r2, _ARC_V2_SEC_STAT_SSC_BIT
66+
/* sflag r2 */
67+
/* sflag instruction is not supported in current ARC GNU */
68+
.long 0x00bf302f
69+
#else
6370
/* disable stack checking */
6471
lr r2, [_ARC_V2_STATUS32]
6572
bclr r2, r2, _ARC_V2_STATUS32_SC_BIT
6673
kflag r2
6774
#endif
75+
#endif
6876

6977
#if CONFIG_RGF_NUM_BANKS != 1
7078
#ifndef CONFIG_FIRQ_NO_LPCC

arch/arc/core/regular_irq.S

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,18 @@ SECTION_FUNC(TEXT, _rirq_enter)
5353

5454

5555
#ifdef CONFIG_ARC_STACK_CHECKING
56+
#ifdef CONFIG_ARC_HAS_SECURE
57+
lr r2, [_ARC_V2_SEC_STAT]
58+
bclr r2, r2, _ARC_V2_SEC_STAT_SSC_BIT
59+
/* sflag r2 */
60+
/* sflag instruction is not supported in current ARC GNU */
61+
.long 0x00bf302f
62+
#else
5663
/* disable stack checking */
5764
lr r2, [_ARC_V2_STATUS32]
5865
bclr r2, r2, _ARC_V2_STATUS32_SC_BIT
5966
kflag r2
67+
#endif
6068
#endif
6169
clri
6270
ld r1, [exc_nest_count]

arch/arc/core/swap.S

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,25 @@ SECTION_FUNC(TEXT, __swap)
100100
*/
101101
lr r3, [_ARC_V2_STATUS32]
102102
push_s r3
103+
104+
#ifdef CONFIG_ARC_HAS_SECURE
105+
lr r3, [_ARC_V2_SEC_STAT]
106+
push_s r3
107+
#endif
108+
103109
#ifdef CONFIG_ARC_STACK_CHECKING
110+
#ifdef CONFIG_ARC_HAS_SECURE
111+
bclr r3, r3, _ARC_V2_SEC_STAT_SSC_BIT
112+
/* sflag r3 */
113+
/* sflag instruction is not supported in current ARC GNU */
114+
.long 0x00ff302f
115+
#else
104116
/* disable stack checking during swap */
105117
bclr r3, r3, _ARC_V2_STATUS32_SC_BIT
106118
kflag r3
107119
#endif
108-
109-
#ifdef CONFIG_ARC_HAS_SECURE
110-
lr r3, [_ARC_V2_SEC_STAT]
111-
push_s r3
112120
#endif
121+
113122
push_s blink
114123

115124
_save_callee_saved_regs
@@ -155,6 +164,15 @@ _swap_return_from_coop:
155164
pop_s blink /* pc into blink */
156165
#ifdef CONFIG_ARC_HAS_SECURE
157166
pop_s r3 /* pop SEC_STAT */
167+
#endif
168+
pop_s r2 /* status32 into r2 */
169+
/*
170+
* the above pop may happen in the unprivileged stack, but now it's
171+
* kernel mode, the write to status32/sec_stat before pop may cause
172+
* stack check fail.
173+
*/
174+
kflag r2 /* write status32 */
175+
#ifdef CONFIG_ARC_HAS_SECURE
158176
/* sflag r3 */
159177
/* sflag instruction is not supported in current ARC GNU */
160178
.long 0x00ff302f
@@ -164,9 +182,6 @@ _swap_return_from_coop:
164182
#endif
165183
return_loc:
166184

167-
pop_s r3 /* status32 into r3 */
168-
kflag r3 /* write status32 */
169-
170185
j_s.d [blink] /* always execute delay slot */
171186
seti r1 /* delay slot */
172187

arch/arc/core/thread.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,13 @@ void _new_thread(struct k_thread *thread, k_thread_stack_t *stack,
141141
* value.
142142
*/
143143
#ifdef CONFIG_ARC_STACK_CHECKING
144+
#ifdef CONFIG_ARC_HAS_SECURE
145+
pInitCtx->sec_stat |= _ARC_V2_SEC_STAT_SSC;
146+
pInitCtx->status32 = _ARC_V2_STATUS32_E(_ARC_V2_DEF_IRQ_LEVEL);
147+
#else
144148
pInitCtx->status32 = _ARC_V2_STATUS32_SC |
145149
_ARC_V2_STATUS32_E(_ARC_V2_DEF_IRQ_LEVEL);
150+
#endif
146151
#ifdef CONFIG_USERSPACE
147152
if (options & K_USER) {
148153
thread->arch.u_stack_top = (u32_t)pStackMem;

arch/arc/core/userspace.S

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SECTION_FUNC(TEXT, _user_thread_entry_wrapper)
6060
/* sp the user stack pointer, r0-r4 are in stack */
6161
mov r5, sp
6262
/* start of privilege stack */
63-
add blink, r5, CONFIG_PRIVILEGED_STACK_SIZE+16
63+
add blink, r5, CONFIG_PRIVILEGED_STACK_SIZE+STACK_GUARD_SIZE+16
6464

6565
/* r4<- start of user stack region */
6666
mov r0, _kernel
@@ -87,15 +87,23 @@ SECTION_FUNC(TEXT, _arc_userspace_enter)
8787
* In ARCv2, the U bit can only be set through exception return
8888
*/
8989
#ifdef CONFIG_ARC_STACK_CHECKING
90-
/* disable stack checking during swap */
90+
/* disable stack checking as the stack should be initialized */
91+
#ifdef CONFIG_ARC_HAS_SECURE
92+
lr blink, [_ARC_V2_SEC_STAT]
93+
bclr blink, blink, _ARC_V2_SEC_STAT_SSC_BIT
94+
/* sflag blink */
95+
/* sflag instruction is not supported in current ARC GNU */
96+
.long 0x07ff302f
97+
#else
9198
lr blink, [_ARC_V2_STATUS32]
9299
bclr blink, blink, _ARC_V2_STATUS32_SC_BIT
93100
kflag blink
101+
#endif
94102
#endif
95103
/* the end of user stack in r5 */
96104
add r5, r4, r5
97105
/* start of privilege stack */
98-
add blink, r5, CONFIG_PRIVILEGED_STACK_SIZE
106+
add blink, r5, CONFIG_PRIVILEGED_STACK_SIZE+STACK_GUARD_SIZE
99107
mov sp, r5
100108

101109
push_s r0
@@ -121,10 +129,18 @@ _clear_user_stack:
121129

122130
_load_stack_check_regs
123131

132+
#ifdef CONFIG_ARC_HAS_SECURE
133+
lr r0, [_ARC_V2_SEC_STAT]
134+
bset r0, r0, _ARC_V2_SEC_STAT_SSC_BIT
135+
/* sflag r0 */
136+
/* sflag instruction is not supported in current ARC GNU */
137+
.long 0x003f302f
138+
#else
124139
lr r0, [_ARC_V2_STATUS32]
125140
bset r0, r0, _ARC_V2_STATUS32_SC_BIT
126141
kflag r0
127142
#endif
143+
#endif
128144

129145
_arc_go_to_user_space:
130146
lr r0, [_ARC_V2_STATUS32]

include/arch/arc/v2/aux_regs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ extern "C" {
128128
#define _ARC_V2_STATUS32_US (1 << _ARC_V2_STATUS32_US_BIT)
129129
#define _ARC_V2_STATUS32_IE (1 << 31)
130130

131+
/* SEC_STAT bits */
132+
#define _ARC_V2_SEC_STAT_SSC_BIT 0
133+
#define _ARC_V2_SEC_STAT_SSC (1 << _ARC_V2_SEC_STAT_SSC_BIT)
134+
#define _ARC_V2_SEC_STAT_NSRT_BIT 1
135+
#define _ARC_V2_SEC_STAT_NSRT (1 << _ARC_V2_SEC_STAT_NSRT_BIT)
136+
#define _ARC_V2_SEC_STAT_NSRU_BIT 2
137+
#define _ARC_V2_SEC_STAT_NSRU (1 << _ARC_V2_SEC_STAT_NSRU_BIT)
138+
#define _ARC_V2_SEC_STAT_IRM_BIT 3
139+
#define _ARC_V2_SEC_STAT_IRM (1 << _ARC_V2_SEC_STAT_IRM_BIT)
140+
#define _ARC_V2_SEC_STAT_SUE_BIT 4
141+
#define _ARC_V2_SEC_STAT_SUE (1 << _ARC_V2_SEC_STAT_SUE_BIT)
142+
#define _ARC_V2_SEC_STAT_NIC_BIT 5
143+
#define _ARC_V2_SEC_STAT_NIC (1 << _ARC_V2_SEC_STAT_NIC_BIT)
144+
131145
/* interrupt related bits */
132146
#define _ARC_V2_IRQ_PRIORITY_SECURE 0x100
133147

0 commit comments

Comments
 (0)