Skip to content

Commit

Permalink
tests: userspace: add riscv support
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
amergnat authored and nashif committed Nov 9, 2020
1 parent 86c5cf1 commit 52e6b7f
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions tests/kernel/mem_protect/userspace/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
extern void arm_core_mpu_disable(void);
#endif

#if defined(CONFIG_RISCV)
#include <../arch/riscv/include/core_pmp.h>
#endif

#define INFO(fmt, ...) printk(fmt, ##__VA_ARGS__)
#define PIPE_LEN 1
#define BYTES_TO_READ_WRITE 1
Expand Down Expand Up @@ -143,6 +147,12 @@ static void test_write_control(void)
"lr %0, [0x402]\n"
: "=r" (er_status)::
);
#elif defined(CONFIG_RISCV)
unsigned int status;

set_fault(K_ERR_CPU_EXCEPTION);

__asm__ volatile("csrr %0, mstatus" : "=r" (status));
#else
#error "Not implemented for this architecture"
zassert_unreachable("Write to control register did not fault");
Expand Down Expand Up @@ -181,6 +191,10 @@ static void test_disable_mmu_mpu(void)
set_fault(K_ERR_CPU_EXCEPTION);

arc_core_mpu_disable();
#elif defined(CONFIG_RISCV)
set_fault(K_ERR_CPU_EXCEPTION);

z_riscv_pmp_clear_config();
#else
#error "Not implemented for this architecture"
#endif
Expand Down Expand Up @@ -305,7 +319,7 @@ static void test_read_priv_stack(void)

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

s[0] = 0;
priv_stack_ptr = (char *)&s[0] - size;
#elif defined(CONFIG_ARM) || defined(CONFIG_X86)
#elif defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_RISCV)
/* priv_stack_ptr set by test_main() */
#else
#error "Not implemented for this architecture"
Expand Down Expand Up @@ -619,7 +633,7 @@ static void test_init_and_access_other_memdomain(void)
spawn_user(&default_bool);
}

#if defined(CONFIG_ARM)
#if defined(CONFIG_ARM) || (defined(CONFIG_GEN_PRIV_STACKS) && defined(CONFIG_RISCV))
extern uint8_t *z_priv_stack_find(void *obj);
#endif
extern k_thread_stack_t ztest_thread_stack[];
Expand Down Expand Up @@ -903,6 +917,15 @@ void test_main(void)
hdr = ((struct z_x86_thread_stack_header *)ztest_thread_stack);
priv_stack_ptr = (((char *)&hdr->privilege_stack) +
(sizeof(hdr->privilege_stack) - 1));
#elif defined(CONFIG_RISCV)
#if defined(CONFIG_GEN_PRIV_STACKS)
priv_stack_ptr = (char *)z_priv_stack_find(ztest_thread_stack);
#else
struct _thread_arch *thread_struct;

thread_struct = ((struct _thread_arch *) ztest_thread_stack);
priv_stack_ptr = (char *)thread_struct->priv_stack_start + 1;
#endif
#endif
k_thread_access_grant(k_current_get(),
&test_thread, &test_stack,
Expand Down

0 comments on commit 52e6b7f

Please sign in to comment.