Skip to content

Commit 52e6b7f

Browse files
amergnatnashif
authored andcommitted
tests: userspace: add riscv support
Add support for the following tests: - test_write_control - test_disable_mmu_mpu - test_read_priv_stack - test_write_priv_stack Signed-off-by: Alexandre Mergnat <[email protected]>
1 parent 86c5cf1 commit 52e6b7f

File tree

1 file changed

+26
-3
lines changed
  • tests/kernel/mem_protect/userspace/src

1 file changed

+26
-3
lines changed

tests/kernel/mem_protect/userspace/src/main.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
extern void arm_core_mpu_disable(void);
2727
#endif
2828

29+
#if defined(CONFIG_RISCV)
30+
#include <../arch/riscv/include/core_pmp.h>
31+
#endif
32+
2933
#define INFO(fmt, ...) printk(fmt, ##__VA_ARGS__)
3034
#define PIPE_LEN 1
3135
#define BYTES_TO_READ_WRITE 1
@@ -143,6 +147,12 @@ static void test_write_control(void)
143147
"lr %0, [0x402]\n"
144148
: "=r" (er_status)::
145149
);
150+
#elif defined(CONFIG_RISCV)
151+
unsigned int status;
152+
153+
set_fault(K_ERR_CPU_EXCEPTION);
154+
155+
__asm__ volatile("csrr %0, mstatus" : "=r" (status));
146156
#else
147157
#error "Not implemented for this architecture"
148158
zassert_unreachable("Write to control register did not fault");
@@ -181,6 +191,10 @@ static void test_disable_mmu_mpu(void)
181191
set_fault(K_ERR_CPU_EXCEPTION);
182192

183193
arc_core_mpu_disable();
194+
#elif defined(CONFIG_RISCV)
195+
set_fault(K_ERR_CPU_EXCEPTION);
196+
197+
z_riscv_pmp_clear_config();
184198
#else
185199
#error "Not implemented for this architecture"
186200
#endif
@@ -305,7 +319,7 @@ static void test_read_priv_stack(void)
305319

306320
s[0] = 0;
307321
priv_stack_ptr = (char *)&s[0] - size;
308-
#elif defined(CONFIG_ARM) || defined(CONFIG_X86)
322+
#elif defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_RISCV)
309323
/* priv_stack_ptr set by test_main() */
310324
#else
311325
#error "Not implemented for this architecture"
@@ -329,7 +343,7 @@ static void test_write_priv_stack(void)
329343

330344
s[0] = 0;
331345
priv_stack_ptr = (char *)&s[0] - size;
332-
#elif defined(CONFIG_ARM) || defined(CONFIG_X86)
346+
#elif defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_RISCV)
333347
/* priv_stack_ptr set by test_main() */
334348
#else
335349
#error "Not implemented for this architecture"
@@ -619,7 +633,7 @@ static void test_init_and_access_other_memdomain(void)
619633
spawn_user(&default_bool);
620634
}
621635

622-
#if defined(CONFIG_ARM)
636+
#if defined(CONFIG_ARM) || (defined(CONFIG_GEN_PRIV_STACKS) && defined(CONFIG_RISCV))
623637
extern uint8_t *z_priv_stack_find(void *obj);
624638
#endif
625639
extern k_thread_stack_t ztest_thread_stack[];
@@ -903,6 +917,15 @@ void test_main(void)
903917
hdr = ((struct z_x86_thread_stack_header *)ztest_thread_stack);
904918
priv_stack_ptr = (((char *)&hdr->privilege_stack) +
905919
(sizeof(hdr->privilege_stack) - 1));
920+
#elif defined(CONFIG_RISCV)
921+
#if defined(CONFIG_GEN_PRIV_STACKS)
922+
priv_stack_ptr = (char *)z_priv_stack_find(ztest_thread_stack);
923+
#else
924+
struct _thread_arch *thread_struct;
925+
926+
thread_struct = ((struct _thread_arch *) ztest_thread_stack);
927+
priv_stack_ptr = (char *)thread_struct->priv_stack_start + 1;
928+
#endif
906929
#endif
907930
k_thread_access_grant(k_current_get(),
908931
&test_thread, &test_stack,

0 commit comments

Comments
 (0)