Skip to content

Commit fa9fb83

Browse files
Wayne Rennashif
authored andcommitted
arch: arc: re-orgnize the code in _new_thread
re-orginize the code in _new_thread to make it easier to understand and maintain Signed-off-by: Wayne Ren <[email protected]>
1 parent a1504c3 commit fa9fb83

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

arch/arc/core/thread.c

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,21 @@ void _new_thread(struct k_thread *thread, k_thread_stack_t *stack,
7575
struct init_stack_frame *pInitCtx;
7676

7777
#if CONFIG_USERSPACE
78+
/* adjust stack and stack size */
7879
#if CONFIG_ARC_MPU_VER == 2
7980
stackSize = POW2_CEIL(STACK_SIZE_ALIGN(stackSize));
8081
#elif CONFIG_ARC_MPU_VER == 3
8182
stackSize = ROUND_UP(stackSize, STACK_ALIGN);
82-
#endif
8383
#endif
8484
stackEnd = pStackMem + stackSize;
8585

86-
#if CONFIG_USERSPACE
86+
if (options & K_USER) {
87+
thread->arch.priv_stack_start =
88+
(u32_t)(stackEnd + STACK_GUARD_SIZE);
89+
thread->arch.priv_stack_size =
90+
(u32_t)(CONFIG_PRIVILEGED_STACK_SIZE);
91+
} else {
8792
/* for kernel thread, the privilege stack is merged into thread stack */
88-
if (!(options & K_USER)) {
8993
/* if MPU_STACK_GUARD is enabled, reserve the the stack area
9094
* |---------------------| |----------------|
9195
* | user stack | | stack guard |
@@ -98,8 +102,11 @@ void _new_thread(struct k_thread *thread, k_thread_stack_t *stack,
98102
pStackMem += STACK_GUARD_SIZE;
99103
stackSize = stackSize + CONFIG_PRIVILEGED_STACK_SIZE;
100104
stackEnd += CONFIG_PRIVILEGED_STACK_SIZE + STACK_GUARD_SIZE;
105+
106+
thread->arch.priv_stack_start = 0;
107+
thread->arch.priv_stack_size = 0;
101108
}
102-
#endif
109+
103110
_new_thread_init(thread, pStackMem, stackSize, priority, options);
104111

105112
stackAdjEnd = stackEnd;
@@ -111,20 +118,41 @@ void _new_thread(struct k_thread *thread, k_thread_stack_t *stack,
111118
thread->userspace_local_data =
112119
(struct _thread_userspace_local_data *)stackAdjEnd;
113120
#endif
121+
/* carve the thread entry struct from the "base" of
122+
the user stack */
123+
pInitCtx = (struct init_stack_frame *)(
124+
STACK_ROUND_DOWN(stackAdjEnd) -
125+
sizeof(struct init_stack_frame));
114126

115-
/* carve the thread entry struct from the "base" of the stack */
116-
pInitCtx = (struct init_stack_frame *)(STACK_ROUND_DOWN(stackAdjEnd) -
117-
sizeof(struct init_stack_frame));
118-
#if CONFIG_USERSPACE
127+
/* fill init context */
119128
if (options & K_USER) {
120-
pInitCtx->pc = ((u32_t)_user_thread_entry_wrapper);
121129
/* through exception return to user mode */
122130
pInitCtx->status32 = _ARC_V2_STATUS32_AE;
131+
pInitCtx->pc = ((u32_t)_user_thread_entry_wrapper);
123132
} else {
124133
pInitCtx->status32 = 0;
125134
pInitCtx->pc = ((u32_t)_thread_entry_wrapper);
126135
}
127-
#else
136+
137+
/*
138+
* enable US bit, US is read as zero in user mode. This will allow use
139+
* mode sleep instructions, and it enables a form of denial-of-service
140+
* attack by putting the processor in sleep mode, but since interrupt
141+
* level/mask can't be set from user space that's not worse than
142+
* executing a loop without yielding.
143+
*/
144+
pInitCtx->status32 |= _ARC_V2_STATUS32_US;
145+
#else /* For no USERSPACE feature */
146+
stackEnd = pStackMem + stackSize;
147+
148+
_new_thread_init(thread, pStackMem, stackSize, priority, options);
149+
150+
stackAdjEnd = stackEnd;
151+
152+
pInitCtx = (struct init_stack_frame *)(
153+
STACK_ROUND_DOWN(stackAdjEnd) -
154+
sizeof(struct init_stack_frame));
155+
128156
pInitCtx->status32 = 0;
129157
pInitCtx->pc = ((u32_t)_thread_entry_wrapper);
130158
#endif
@@ -137,20 +165,13 @@ void _new_thread(struct k_thread *thread, k_thread_stack_t *stack,
137165
pInitCtx->r1 = (u32_t)parameter1;
138166
pInitCtx->r2 = (u32_t)parameter2;
139167
pInitCtx->r3 = (u32_t)parameter3;
140-
/*
141-
* For now set the interrupt priority to 15
142-
* we can leave interrupt enable flag set to 0 as
143-
* seti instruction in the end of the _Swap() will
144-
* enable the interrupts based on intlock_key
145-
* value.
146-
*/
168+
169+
/* stack check configuration */
147170
#ifdef CONFIG_ARC_STACK_CHECKING
148171
#ifdef CONFIG_ARC_HAS_SECURE
149172
pInitCtx->sec_stat |= _ARC_V2_SEC_STAT_SSC;
150-
pInitCtx->status32 |= _ARC_V2_STATUS32_E(_ARC_V2_DEF_IRQ_LEVEL);
151173
#else
152-
pInitCtx->status32 |= _ARC_V2_STATUS32_SC |
153-
_ARC_V2_STATUS32_E(_ARC_V2_DEF_IRQ_LEVEL);
174+
pInitCtx->status32 |= _ARC_V2_STATUS32_SC;
154175
#endif
155176
#ifdef CONFIG_USERSPACE
156177
if (options & K_USER) {
@@ -170,31 +191,12 @@ void _new_thread(struct k_thread *thread, k_thread_stack_t *stack,
170191
thread->arch.k_stack_top = (u32_t) pStackMem;
171192
thread->arch.k_stack_base = (u32_t) stackEnd;
172193
#endif
173-
#else
174-
pInitCtx->status32 = _ARC_V2_STATUS32_E(_ARC_V2_DEF_IRQ_LEVEL);
175-
#endif
176-
177-
#if CONFIG_USERSPACE
178-
/*
179-
* enable US bit, US is read as zero in user mode. This will allow use
180-
* mode sleep instructions, and it enables a form of denial-of-service
181-
* attack by putting the processor in sleep mode, but since interrupt
182-
* level/mask can't be set from user space that's not worse than
183-
* executing a loop without yielding.
184-
*/
185-
pInitCtx->status32 |= _ARC_V2_STATUS32_US;
186-
187-
if (options & K_USER) {
188-
thread->arch.priv_stack_start =
189-
(u32_t)(stackEnd + STACK_GUARD_SIZE);
190-
thread->arch.priv_stack_size =
191-
(u32_t)CONFIG_PRIVILEGED_STACK_SIZE;
192-
} else {
193-
thread->arch.priv_stack_start = 0;
194-
thread->arch.priv_stack_size = 0;
195-
}
196194
#endif
197195
/*
196+
* seti instruction in the end of the _Swap() will
197+
* enable the interrupts based on intlock_key
198+
* value.
199+
*
198200
* intlock_key is constructed based on ARCv2 ISA Programmer's
199201
* Reference Manual CLRI instruction description:
200202
* dst[31:6] dst[5] dst[4] dst[3:0]

0 commit comments

Comments
 (0)