Skip to content

Commit 1247f84

Browse files
Flavio Ceolincarlescufi
authored andcommitted
xtensa: userspace: Supports tls on userspace
Use thread local storage to check whether or not a thread is running in user mode. This allows to use threadptr to properly support tls. Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
1 parent a36e39c commit 1247f84

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

arch/xtensa/core/userspace.S

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,17 @@ _id_ok:
100100
* we are in a interruption we don't want the system
101101
* thinking it is possibly running in user mode.
102102
*/
103+
#ifdef CONFIG_THREAD_LOCAL_STORAGE
104+
movi a0, is_user_mode@tpoff
105+
rur.THREADPTR a3
106+
add a0, a3, a0
107+
108+
movi a3, 0
109+
s32i a3, a0, 0
110+
#else
103111
movi a0, 0
104112
wur.THREADPTR a0
113+
#endif
105114

106115
/* Set syscall parameters. We have an initial call4 to set up the
107116
* the stack and then a new call4 for the syscall function itself.
@@ -157,9 +166,17 @@ _syscall_returned:
157166
wsr a3, SCOMPARE1
158167
#endif
159168

169+
#ifdef CONFIG_THREAD_LOCAL_STORAGE
170+
l32i a3, a1, ___xtensa_irq_bsa_t_threadptr_OFFSET
171+
movi a0, is_user_mode@tpoff
172+
add a0, a3, a0
173+
movi a3, 1
174+
s32i a3, a0, 0
175+
#else
160176
rsr a3, ZSR_CPU
161177
l32i a3, a3, ___cpu_t_current_OFFSET
162178
wur.THREADPTR a3
179+
#endif
163180

164181
l32i a3, a1, ___xtensa_irq_bsa_t_ps_OFFSET
165182
wsr.ps a3
@@ -225,9 +242,17 @@ z_xtensa_userspace_enter:
225242
l32i a6, a1, 24
226243
call4 z_xtensa_swap_update_page_tables
227244

228-
/* Set threadptr with the thread address, we are going to user mode. */
229-
l32i a0, a1, 24
230-
wur.THREADPTR a0
245+
#ifdef CONFIG_THREAD_LOCAL_STORAGE
246+
rur.threadptr a3
247+
movi a0, is_user_mode@tpoff
248+
add a0, a3, a0
249+
movi a3, 1
250+
s32i a3, a0, 0
251+
#else
252+
rsr a3, ZSR_CPU
253+
l32i a3, a3, ___cpu_t_current_OFFSET
254+
wur.THREADPTR a3
255+
#endif
231256

232257
/* Set now z_thread_entry parameters, we are simulating a call4
233258
* call, so parameters start at a6, a7, ...

arch/xtensa/core/xtensa-asm2.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ Z_EXC_DECLARE(z_xtensa_user_string_nlen);
2828
static const struct z_exc_handle exceptions[] = {
2929
Z_EXC_HANDLE(z_xtensa_user_string_nlen)
3030
};
31+
32+
#ifdef CONFIG_THREAD_LOCAL_STORAGE
33+
/*
34+
* Per-thread (TLS) variable indicating whether execution is in user mode.
35+
*/
36+
__thread uint32_t is_user_mode;
37+
#endif
38+
3139
#endif /* CONFIG_USERSPACE */
3240

3341
void *xtensa_init_stack(struct k_thread *thread, int *stack_top,

include/zephyr/arch/xtensa/syscall.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,17 @@ static inline bool arch_is_user_context(void)
207207
"rur.THREADPTR %0\n\t"
208208
: "=a" (thread)
209209
);
210+
#ifdef CONFIG_THREAD_LOCAL_STORAGE
211+
extern __thread uint32_t is_user_mode;
210212

213+
if (!thread) {
214+
return false;
215+
}
216+
217+
return is_user_mode != 0;
218+
#else
211219
return !!thread;
220+
#endif
212221
}
213222

214223
#undef SYSINL

0 commit comments

Comments
 (0)