Skip to content

Commit 277fa9e

Browse files
dcpleungkartben
authored andcommitted
xtensa: userspace: swap page tables via assembly code
Since the necessary register values are now pre-computed and stored in the memory domain struct, we can use them directly in various assembly locations, thus replacing the function call to xtensa_swap_update_page_tables(). Signed-off-by: Daniel Leung <daniel.leung@intel.com>
1 parent 689e112 commit 277fa9e

File tree

8 files changed

+77
-13
lines changed

8 files changed

+77
-13
lines changed

arch/xtensa/core/offsets/offsets.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ GEN_OFFSET_SYM(_thread_arch_t, return_ps);
7171
GEN_OFFSET_SYM(_thread_t, switch_handle);
7272
#ifdef CONFIG_XTENSA_MMU
7373
GEN_OFFSET_SYM(_thread_arch_t, ptables);
74+
75+
GEN_OFFSET_SYM(_thread_t, mem_domain_info);
76+
GEN_OFFSET_SYM(_mem_domain_info_t, mem_domain);
77+
GEN_OFFSET_SYM(k_mem_domain_t, arch);
78+
79+
GEN_OFFSET_SYM(arch_mem_domain_t, reg_asid);
80+
GEN_OFFSET_SYM(arch_mem_domain_t, reg_ptevaddr);
81+
GEN_OFFSET_SYM(arch_mem_domain_t, reg_ptepin_as);
82+
GEN_OFFSET_SYM(arch_mem_domain_t, reg_ptepin_at);
83+
GEN_OFFSET_SYM(arch_mem_domain_t, reg_vecpin_as);
84+
GEN_OFFSET_SYM(arch_mem_domain_t, reg_vecpin_at);
85+
7486
#endif
7587
#ifdef CONFIG_XTENSA_MPU
7688
GEN_OFFSET_SYM(_thread_arch_t, mpu_map);

arch/xtensa/core/ptables.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,4 @@ int arch_buffer_validate(const void *addr, size_t size, int write)
11221122
return mem_buffer_validate(addr, size, write, XTENSA_MMU_USER_RING);
11231123
}
11241124

1125-
void xtensa_swap_update_page_tables(struct k_thread *incoming)
1126-
{
1127-
struct arch_mem_domain *domain =
1128-
&(incoming->mem_domain_info.mem_domain->arch);
1129-
1130-
xtensa_mmu_set_paging(domain);
1131-
}
1132-
11331125
#endif /* CONFIG_USERSPACE */

arch/xtensa/core/userspace.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ xtensa_userspace_enter:
271271

272272
l32i a6, a1, 24
273273
#ifdef CONFIG_XTENSA_MMU
274-
call4 xtensa_swap_update_page_tables
274+
SWAP_PAGE_TABLE a6, a3, a7
275275
#endif
276276
#ifdef CONFIG_XTENSA_MPU
277277
call4 xtensa_mpu_map_write

arch/xtensa/core/xtensa_asm2_util.S

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,10 @@ xtensa_switch:
248248
s32i a6, a1, 8
249249
s32i a7, a1, 12
250250

251-
/* Switch page tables */
252251
rsr a6, ZSR_CPU
253252
l32i a6, a6, ___cpu_t_current_OFFSET
254253
#ifdef CONFIG_XTENSA_MMU
255-
call4 xtensa_swap_update_page_tables
254+
SWAP_PAGE_TABLE a6, a4, a7
256255
#endif
257256
#ifdef CONFIG_XTENSA_MPU
258257
call4 xtensa_mpu_map_write

arch/xtensa/include/offsets_short_arch.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@
1717

1818
#define _thread_offset_to_ptables \
1919
(___thread_t_arch_OFFSET + ___thread_arch_t_ptables_OFFSET)
20+
21+
#define _thread_offset_to_mem_domain \
22+
(___thread_t_mem_domain_info_OFFSET + ___mem_domain_info_t_mem_domain_OFFSET)
23+
24+
#define _k_mem_domain_offset_to_arch_reg_asid \
25+
(__k_mem_domain_t_arch_OFFSET + __arch_mem_domain_t_reg_asid_OFFSET)
26+
27+
#define _k_mem_domain_offset_to_arch_reg_ptevaddr \
28+
(__k_mem_domain_t_arch_OFFSET + __arch_mem_domain_t_reg_ptevaddr_OFFSET)
29+
30+
#define _k_mem_domain_offset_to_arch_reg_ptepin_as \
31+
(__k_mem_domain_t_arch_OFFSET + __arch_mem_domain_t_reg_ptepin_as_OFFSET)
32+
33+
#define _k_mem_domain_offset_to_arch_reg_ptepin_at \
34+
(__k_mem_domain_t_arch_OFFSET + __arch_mem_domain_t_reg_ptepin_at_OFFSET)
35+
36+
#define _k_mem_domain_offset_to_arch_reg_vecpin_as \
37+
(__k_mem_domain_t_arch_OFFSET + __arch_mem_domain_t_reg_vecpin_as_OFFSET)
38+
39+
#define _k_mem_domain_offset_to_arch_reg_vecpin_at \
40+
(__k_mem_domain_t_arch_OFFSET + __arch_mem_domain_t_reg_vecpin_at_OFFSET)
41+
2042
#endif /* CONFIG_USERSPACE */
2143

2244
#endif /* ZEPHYR_ARCH_XTENSA_INCLUDE_OFFSETS_SHORT_ARCH_H_ */

arch/xtensa/include/xtensa_asm2_s.h

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,44 @@
225225

226226
.endm
227227

228+
#if defined(CONFIG_XTENSA_MMU) && defined(CONFIG_USERSPACE)
229+
/*
230+
* SWAP_PAGE_TABLE
231+
*
232+
* This swaps the page tables by using the pre-computed register values
233+
* inside the architecture-specific memory domain struct.
234+
*
235+
* THREAD_PTR_REG is input containing pointer to the incoming thread struct.
236+
* SC1_REG and SC2_REG are scratch registers.
237+
*
238+
* Note that all THREAD_PTR_REG, SC1_REG and SC2_REG are all clobbered.
239+
* Restore the thread pointer after this if necessary.
240+
*/
241+
.macro SWAP_PAGE_TABLE THREAD_PTR_REG, SC1_REG, SC2_REG
242+
l32i \THREAD_PTR_REG, \THREAD_PTR_REG, _thread_offset_to_mem_domain
243+
244+
j _swap_page_table_\@
245+
246+
.align 16
247+
_swap_page_table_\@:
248+
l32i \SC1_REG, \THREAD_PTR_REG, _k_mem_domain_offset_to_arch_reg_ptevaddr
249+
l32i \SC2_REG, \THREAD_PTR_REG, _k_mem_domain_offset_to_arch_reg_asid
250+
wsr \SC1_REG, PTEVADDR
251+
wsr \SC2_REG, RASID
252+
253+
l32i \SC1_REG, \THREAD_PTR_REG, _k_mem_domain_offset_to_arch_reg_ptepin_as
254+
l32i \SC2_REG, \THREAD_PTR_REG, _k_mem_domain_offset_to_arch_reg_ptepin_at
255+
wdtlb \SC2_REG, \SC1_REG
256+
257+
l32i \SC1_REG, \THREAD_PTR_REG, _k_mem_domain_offset_to_arch_reg_vecpin_as
258+
l32i \SC2_REG, \THREAD_PTR_REG, _k_mem_domain_offset_to_arch_reg_vecpin_at
259+
wdtlb \SC2_REG, \SC1_REG
260+
261+
isync
262+
.endm
263+
264+
#endif /* CONFIG_XTENSA_MMU && CONFIG_USERSPACE */
265+
228266
/*
229267
* CROSS_STACK_CALL
230268
*
@@ -357,7 +395,7 @@ _xstack_call0_\@:
357395
l32i a6, a6, ___cpu_t_current_OFFSET
358396

359397
#ifdef CONFIG_XTENSA_MMU
360-
call4 xtensa_swap_update_page_tables
398+
SWAP_PAGE_TABLE a6, a3, a7
361399
#endif
362400
#ifdef CONFIG_XTENSA_MPU
363401
call4 xtensa_mpu_map_write

include/zephyr/arch/xtensa/arch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct arch_mem_domain {
8181
sys_snode_t node;
8282
};
8383

84+
typedef struct arch_mem_domain arch_mem_domain_t;
85+
8486
/**
8587
* @brief Generate hardware exception.
8688
*

soc/cdns/dc233c/include/xtensa-dc233c.ld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ SECTIONS
162162

163163
/* Userspace related stuff */
164164
LIB_OBJ_FUNC_IN_SECT(libarch__xtensa__core.a,userspace.S.obj,xtensa_do_syscall)
165-
LIB_OBJ_FUNC_IN_SECT(libarch__xtensa__core.a,ptables.c.obj,xtensa_swap_update_page_tables)
166165

167166
/* Below are to speed up execution by avoiding TLB misses
168167
* on frequently used functions.

0 commit comments

Comments
 (0)